Remove building with NOCRYPTO option
[minix.git] / external / bsd / elftoolchain / dist / libelf / elf_strptr.c
blob80e6d823beb9fd321e30c13170ae56e44d7e71a6
1 /* $NetBSD: elf_strptr.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
3 /*-
4 * Copyright (c) 2006,2008 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 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
33 #include <sys/param.h>
35 #include <assert.h>
36 #include <gelf.h>
38 #include "_libelf.h"
40 __RCSID("$NetBSD: elf_strptr.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
41 ELFTC_VCSID("Id: elf_strptr.c 2271 2011-12-03 17:06:35Z jkoshy ");
44 * Convert an ELF section#,offset pair to a string pointer.
47 char *
48 elf_strptr(Elf *e, size_t scndx, size_t offset)
50 Elf_Scn *s;
51 Elf_Data *d;
52 size_t alignment, count;
53 GElf_Shdr shdr;
55 if (e == NULL || e->e_kind != ELF_K_ELF) {
56 LIBELF_SET_ERROR(ARGUMENT, 0);
57 return (NULL);
60 if ((s = elf_getscn(e, scndx)) == NULL ||
61 gelf_getshdr(s, &shdr) == NULL)
62 return (NULL);
64 if (shdr.sh_type != SHT_STRTAB ||
65 offset >= shdr.sh_size) {
66 LIBELF_SET_ERROR(ARGUMENT, 0);
67 return (NULL);
70 d = NULL;
71 if (e->e_flags & ELF_F_LAYOUT) {
74 * The application is taking responsibility for the
75 * ELF object's layout, so we can directly translate
76 * an offset to a `char *' address using the `d_off'
77 * members of Elf_Data descriptors.
79 while ((d = elf_getdata(s, d)) != NULL) {
81 if (d->d_buf == 0 || d->d_size == 0)
82 continue;
84 if (d->d_type != ELF_T_BYTE) {
85 LIBELF_SET_ERROR(DATA, 0);
86 return (NULL);
89 if (offset >= d->d_off &&
90 offset < d->d_off + d->d_size)
91 return ((char *) d->d_buf + offset - d->d_off);
93 } else {
95 * Otherwise, the `d_off' members are not useable and
96 * we need to compute offsets ourselves, taking into
97 * account 'holes' in coverage of the section introduced
98 * by alignment requirements.
100 count = (size_t) 0; /* cumulative count of bytes seen */
101 while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
103 if (d->d_buf == NULL || d->d_size == 0)
104 continue;
106 if (d->d_type != ELF_T_BYTE) {
107 LIBELF_SET_ERROR(DATA, 0);
108 return (NULL);
111 if ((alignment = d->d_align) > 1) {
112 if ((alignment & (alignment - 1)) != 0) {
113 LIBELF_SET_ERROR(DATA, 0);
114 return (NULL);
116 count = roundup2(count, alignment);
119 if (offset < count) {
120 /* offset starts in the 'hole' */
121 LIBELF_SET_ERROR(ARGUMENT, 0);
122 return (NULL);
125 if (offset < count + d->d_size) {
126 if (d->d_buf != NULL)
127 return ((char *) d->d_buf +
128 offset - count);
129 LIBELF_SET_ERROR(DATA, 0);
130 return (NULL);
133 count += d->d_size;
137 LIBELF_SET_ERROR(ARGUMENT, 0);
138 return (NULL);