Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libelf / dist / elf_strptr.c
blob062a41b168683ce82464b6c9a07b2f4c2f32bc5b
1 /* $NetBSD$ */
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/elf_strptr.c,v 1.2.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <sys/param.h>
34 #include <assert.h>
35 #include <gelf.h>
37 #include "_libelf.h"
40 * Convert an ELF section#,offset pair to a string pointer.
43 char *
44 elf_strptr(Elf *e, size_t scndx, size_t offset)
46 Elf_Scn *s;
47 Elf_Data *d;
48 size_t alignment, count;
49 GElf_Shdr shdr;
51 if (e == NULL || e->e_kind != ELF_K_ELF) {
52 LIBELF_SET_ERROR(ARGUMENT, 0);
53 return (NULL);
56 if ((s = elf_getscn(e, scndx)) == NULL ||
57 gelf_getshdr(s, &shdr) == NULL)
58 return (NULL);
60 if (/*shdr.sh_type != SHT_STRTAB || */
61 offset >= shdr.sh_size) {
62 LIBELF_SET_ERROR(ARGUMENT, 0);
63 return (NULL);
66 d = NULL;
67 if (e->e_flags & ELF_F_LAYOUT) {
70 * The application is taking responsibility for the
71 * ELF object's layout, so we can directly translate
72 * an offset to a `char *' address using the `d_off'
73 * members of Elf_Data descriptors.
75 while ((d = elf_getdata(s, d)) != NULL) {
77 if (d->d_buf == 0 || d->d_size == 0)
78 continue;
80 if (d->d_type != ELF_T_BYTE) {
81 LIBELF_SET_ERROR(DATA, 0);
82 return (NULL);
85 if (offset >= d->d_off &&
86 offset < d->d_off + d->d_size)
87 return ((char *) d->d_buf + offset - d->d_off);
89 } else {
91 * Otherwise, the `d_off' members are not useable and
92 * we need to compute offsets ourselves, taking into
93 * account 'holes' in coverage of the section introduced
94 * by alignment requirements.
96 count = (size_t) 0; /* cumulative count of bytes seen */
97 while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
99 if (d->d_buf == NULL || d->d_size == 0)
100 continue;
102 if (d->d_type != ELF_T_BYTE) {
103 LIBELF_SET_ERROR(DATA, 0);
104 return (NULL);
107 if ((alignment = d->d_align) > 1) {
108 if ((alignment & (alignment - 1)) != 0) {
109 LIBELF_SET_ERROR(DATA, 0);
110 return (NULL);
112 count = roundup2(count, alignment);
115 if (offset < count) {
116 /* offset starts in the 'hole' */
117 LIBELF_SET_ERROR(ARGUMENT, 0);
118 return (NULL);
121 if (offset < count + d->d_size) {
122 if (d->d_buf != NULL)
123 return ((char *) d->d_buf +
124 offset - count);
125 LIBELF_SET_ERROR(DATA, 0);
126 return (NULL);
129 count += d->d_size;
133 LIBELF_SET_ERROR(ARGUMENT, 0);
134 return (NULL);