Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libelf / dist / libelf_ehdr.c
blobf623b42c5283ee733790820dcd851af1ae91ba8b
1 /* $NetBSD: libelf_ehdr.c,v 1.4 2009/12/20 23:23:46 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.
28 * $FreeBSD: src/lib/libelf/libelf_ehdr.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $
31 #include <sys/cdefs.h>
32 /* __FBSDID("$FreeBSD: src/lib/libelf/libelf_ehdr.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
34 #include <assert.h>
35 #include <gelf.h>
36 #include <libelf.h>
37 #include <limits.h>
38 #include <stdlib.h>
40 #include "_libelf.h"
43 * Retrieve counts for sections, phdrs and the section string table index
44 * from section header #0 of the ELF object.
46 static int
47 _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
48 uint16_t strndx)
50 Elf_Scn *scn;
51 size_t fsz;
52 void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
53 uint32_t shtype;
55 assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
57 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
58 assert(fsz > 0);
60 if (e->e_rawsize < shoff + fsz) { /* raw file too small */
61 LIBELF_SET_ERROR(HEADER, 0);
62 return (0);
65 if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
66 return (0);
68 if (shoff > SSIZE_MAX) {
69 LIBELF_SET_ERROR(HEADER, 0);
70 return (0);
73 xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
74 (*xlator)((void *) &scn->s_shdr, e->e_rawfile + (ssize_t)shoff,
75 (size_t) 1, e->e_byteorder != _libelf_host_byteorder());
77 #define GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
78 scn->s_shdr.s_shdr64.M)
80 if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
81 LIBELF_SET_ERROR(SECTION, 0);
82 return (0);
85 if (GET_SHDR_MEMBER(sh_size) > UINT_MAX) {
86 LIBELF_SET_ERROR(HEADER, 0);
87 return (0);
90 e->e_u.e_elf.e_nscn = (unsigned int)GET_SHDR_MEMBER(sh_size);
91 e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
92 GET_SHDR_MEMBER(sh_info);
93 e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
94 GET_SHDR_MEMBER(sh_link);
95 #undef GET_SHDR_MEMBER
97 return (1);
100 #define EHDR_INIT(E,SZ) do { \
101 Elf##SZ##_Ehdr *eh = (E); \
102 eh->e_ident[EI_MAG0] = ELFMAG0; \
103 eh->e_ident[EI_MAG1] = ELFMAG1; \
104 eh->e_ident[EI_MAG2] = ELFMAG2; \
105 eh->e_ident[EI_MAG3] = ELFMAG3; \
106 eh->e_ident[EI_CLASS] = ELFCLASS##SZ; \
107 eh->e_ident[EI_DATA] = ELFDATANONE; \
108 eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version); \
109 eh->e_machine = EM_NONE; \
110 eh->e_type = ELF_K_NONE; \
111 eh->e_version = LIBELF_PRIVATE(version); \
112 } while (/*CONSTCOND*/0)
114 void *
115 _libelf_ehdr(Elf *e, int ec, int allocate)
117 void *ehdr;
118 size_t fsz, msz;
119 uint16_t phnum, shnum, strndx;
120 uint64_t shoff;
121 void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
123 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
125 if (e == NULL || e->e_kind != ELF_K_ELF) {
126 LIBELF_SET_ERROR(ARGUMENT, 0);
127 return (NULL);
130 if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
131 LIBELF_SET_ERROR(CLASS, 0);
132 return (NULL);
135 if (e->e_version != EV_CURRENT) {
136 LIBELF_SET_ERROR(VERSION, 0);
137 return (NULL);
140 if (e->e_class == ELFCLASSNONE)
141 e->e_class = ec;
143 if (ec == ELFCLASS32)
144 ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
145 else
146 ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
148 if (ehdr != NULL) /* already have a translated ehdr */
149 return (ehdr);
151 fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
152 assert(fsz > 0);
154 if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < fsz) {
155 LIBELF_SET_ERROR(HEADER, 0);
156 return (NULL);
159 msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT);
161 assert(msz > 0);
163 if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
164 LIBELF_SET_ERROR(RESOURCE, 0);
165 return (NULL);
168 if (ec == ELFCLASS32) {
169 e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
170 EHDR_INIT(ehdr,32);
171 } else {
172 e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
173 EHDR_INIT(ehdr,64);
176 if (allocate)
177 e->e_flags |= ELF_F_DIRTY;
179 if (e->e_cmd == ELF_C_WRITE)
180 return (ehdr);
182 xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
183 (*xlator)(ehdr, e->e_rawfile, (size_t) 1,
184 e->e_byteorder != _libelf_host_byteorder());
187 * If extended numbering is being used, read the correct
188 * number of sections and program header entries.
190 if (ec == ELFCLASS32) {
191 phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
192 shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
193 shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
194 strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
195 } else {
196 phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
197 shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
198 shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
199 strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
202 if (shnum >= SHN_LORESERVE ||
203 (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
204 strndx == SHN_XINDEX))) {
205 LIBELF_SET_ERROR(HEADER, 0);
206 return (NULL);
209 if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */
210 e->e_u.e_elf.e_nphdr = phnum;
211 e->e_u.e_elf.e_nscn = shnum;
212 e->e_u.e_elf.e_strndx = strndx;
213 } else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
214 return (NULL);
216 return (ehdr);