Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libelf / dist / gelf_syminfo.c
blobf8adf5cebe322ede09cedf80351de6eb62c75ac7
1 /* $NetBSD: gelf_syminfo.c,v 1.3 2009/12/19 07:31:04 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2006 Joseph Koshy
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/lib/libelf/gelf_syminfo.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <assert.h>
33 #include <limits.h>
34 #include <gelf.h>
36 #include "_libelf.h"
38 #if defined(__LIBELF_HAVE_ELF_SYMINFO)
40 GElf_Syminfo *
41 gelf_getsyminfo(Elf_Data *d, int ndx, GElf_Syminfo *dst)
43 int ec;
44 Elf *e;
45 Elf_Scn *scn;
46 Elf32_Syminfo *syminfo32;
47 Elf64_Syminfo *syminfo64;
48 size_t msz;
49 uint32_t sh_type;
51 if (d == NULL || ndx < 0 || dst == NULL ||
52 (scn = d->d_scn) == NULL ||
53 (e = scn->s_elf) == NULL) {
54 LIBELF_SET_ERROR(ARGUMENT, 0);
55 return (NULL);
58 ec = e->e_class;
59 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
61 if (ec == ELFCLASS32)
62 sh_type = scn->s_shdr.s_shdr32.sh_type;
63 else
64 sh_type = scn->s_shdr.s_shdr64.sh_type;
66 if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) {
67 LIBELF_SET_ERROR(ARGUMENT, 0);
68 return (NULL);
71 msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version);
73 assert(msz > 0);
75 if (msz * ndx >= d->d_size) {
76 LIBELF_SET_ERROR(ARGUMENT, 0);
77 return (NULL);
80 if (ec == ELFCLASS32) {
82 syminfo32 = (Elf32_Syminfo *) d->d_buf + ndx;
84 dst->si_boundto = syminfo32->si_boundto;
85 dst->si_flags = syminfo32->si_flags;
87 } else {
89 syminfo64 = (Elf64_Syminfo *) d->d_buf + ndx;
91 *dst = *syminfo64;
94 return (dst);
97 int
98 gelf_update_syminfo(Elf_Data *d, int ndx, GElf_Syminfo *gs)
100 int ec;
101 Elf *e;
102 Elf_Scn *scn;
103 Elf32_Syminfo *syminfo32;
104 Elf64_Syminfo *syminfo64;
105 size_t msz;
106 uint32_t sh_type;
108 if (d == NULL || ndx < 0 || gs == NULL ||
109 (scn = d->d_scn) == NULL ||
110 (e = scn->s_elf) == NULL) {
111 LIBELF_SET_ERROR(ARGUMENT, 0);
112 return (0);
115 ec = e->e_class;
116 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
118 if (ec == ELFCLASS32)
119 sh_type = scn->s_shdr.s_shdr32.sh_type;
120 else
121 sh_type = scn->s_shdr.s_shdr64.sh_type;
123 if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) {
124 LIBELF_SET_ERROR(ARGUMENT, 0);
125 return (0);
128 msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version);
129 assert(msz > 0);
131 if (msz * ndx >= d->d_size) {
132 LIBELF_SET_ERROR(ARGUMENT, 0);
133 return (0);
136 if (ec == ELFCLASS32) {
137 syminfo32 = (Elf32_Syminfo *) d->d_buf + ndx;
139 syminfo32->si_boundto = gs->si_boundto;
140 syminfo32->si_flags = gs->si_flags;
142 } else {
143 syminfo64 = (Elf64_Syminfo *) d->d_buf + ndx;
145 *syminfo64 = *gs;
148 return (1);
151 #endif /* __LIBELF_HAVE_ELF_SYMINFO */