2 * cook.c - read and translate ELF files.
3 * Copyright (C) 1995 - 2006 Michael Riepe
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 static const char rcsid[] = "@(#) $Id: cook.c,v 1.29 2008/05/23 08:15:34 michael Exp $";
26 const Elf_Scn _elf_scn_init = INIT_SCN;
27 const Scn_Data _elf_data_init = INIT_DATA;
30 _elf_scn_type(unsigned t) {
32 case SHT_DYNAMIC: return ELF_T_DYN;
33 case SHT_DYNSYM: return ELF_T_SYM;
34 case SHT_HASH: return ELF_T_WORD;
35 case SHT_REL: return ELF_T_REL;
36 case SHT_RELA: return ELF_T_RELA;
37 case SHT_SYMTAB: return ELF_T_SYM;
38 case SHT_SYMTAB_SHNDX: return ELF_T_WORD; /* XXX: really? */
39 #if __LIBELF_SYMBOL_VERSIONS
40 #if __LIBELF_SUN_SYMBOL_VERSIONS
41 case SHT_SUNW_verdef: return ELF_T_VDEF;
42 case SHT_SUNW_verneed: return ELF_T_VNEED;
43 case SHT_SUNW_versym: return ELF_T_HALF;
44 #else /* __LIBELF_SUN_SYMBOL_VERSIONS */
45 case SHT_GNU_verdef: return ELF_T_VDEF;
46 case SHT_GNU_verneed: return ELF_T_VNEED;
47 case SHT_GNU_versym: return ELF_T_HALF;
48 #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */
49 #endif /* __LIBELF_SYMBOL_VERSIONS */
55 * Check for overflow on 32-bit systems
57 #define overflow(a,b,t) (sizeof(a) < sizeof(t) && (t)(a) != (b))
59 #define truncerr(t) ((t)==ELF_T_EHDR?ERROR_TRUNC_EHDR: \
60 ((t)==ELF_T_PHDR?ERROR_TRUNC_PHDR: \
62 #define memerr(t) ((t)==ELF_T_EHDR?ERROR_MEM_EHDR: \
63 ((t)==ELF_T_PHDR?ERROR_MEM_PHDR: \
67 _elf_xlatetom(const Elf *elf, Elf_Data *dst, const Elf_Data *src) {
68 if (elf->e_class == ELFCLASS32) {
69 return elf32_xlatetom(dst, src, elf->e_encoding);
72 else if (elf->e_class == ELFCLASS64) {
73 return elf64_xlatetom(dst, src, elf->e_encoding);
75 #endif /* __LIBELF64 */
76 seterr(ERROR_UNIMPLEMENTED);
81 _elf_item(void *buf, Elf *elf, Elf_Type type, size_t off) {
84 elf_assert(valid_type(type));
85 if (off < 0 || off > elf->e_size) {
86 seterr(ERROR_OUTSIDE);
91 src.d_version = elf->e_version;
92 src.d_size = _fsize(elf->e_class, src.d_version, type);
93 elf_assert(src.d_size);
94 if ((elf->e_size - off) < src.d_size) {
95 seterr(truncerr(type));
99 dst.d_version = _elf_version;
100 dst.d_size = _msize(elf->e_class, dst.d_version, type);
101 elf_assert(dst.d_size);
103 if (!(dst.d_buf = buf) && !(dst.d_buf = malloc(dst.d_size))) {
104 seterr(memerr(type));
108 elf_assert(elf->e_data);
109 if (elf->e_rawdata) {
110 src.d_buf = elf->e_rawdata + off;
113 src.d_buf = elf->e_data + off;
116 if (_elf_xlatetom(elf, &dst, &src)) {
117 return (char*)dst.d_buf;
119 if (dst.d_buf != buf) {
126 _elf_cook_phdr(Elf *elf) {
127 size_t num, off, entsz;
129 if (elf->e_class == ELFCLASS32) {
130 num = ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum;
131 off = ((Elf32_Ehdr*)elf->e_ehdr)->e_phoff;
132 entsz = ((Elf32_Ehdr*)elf->e_ehdr)->e_phentsize;
135 else if (elf->e_class == ELFCLASS64) {
136 num = ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum;
137 off = ((Elf64_Ehdr*)elf->e_ehdr)->e_phoff;
138 entsz = ((Elf64_Ehdr*)elf->e_ehdr)->e_phentsize;
140 * Check for overflow on 32-bit systems
142 if (overflow(off, ((Elf64_Ehdr*)elf->e_ehdr)->e_phoff, Elf64_Off)) {
143 seterr(ERROR_OUTSIDE);
147 #endif /* __LIBELF64 */
149 seterr(ERROR_UNIMPLEMENTED);
158 if (num == PN_XNUM) {
160 * Overflow in ehdr->e_phnum.
161 * Get real value from first SHDR.
163 if (!(scn = elf->e_scn_1)) {
164 seterr(ERROR_NOSUCHSCN);
167 if (elf->e_class == ELFCLASS32) {
168 num = scn->s_shdr32.sh_info;
171 else if (elf->e_class == ELFCLASS64) {
172 num = scn->s_shdr64.sh_info;
174 #endif /* __LIBELF64 */
175 /* we already had this
177 seterr(ERROR_UNIMPLEMENTED);
183 size = _fsize(elf->e_class, elf->e_version, ELF_T_PHDR);
185 #if ENABLE_EXTENDED_FORMAT
187 #else /* ENABLE_EXTENDED_FORMAT */
189 #endif /* ENABLE_EXTENDED_FORMAT */
190 seterr(ERROR_EHDR_PHENTSIZE);
193 size = _msize(elf->e_class, _elf_version, ELF_T_PHDR);
195 if (!(p = malloc(num * size))) {
196 seterr(memerr(ELF_T_PHDR));
199 for (i = 0; i < num; i++) {
200 if (!_elf_item(p + i * size, elf, ELF_T_PHDR, off + i * entsz)) {
212 _elf_cook_shdr(Elf *elf) {
213 size_t num, off, entsz;
215 if (elf->e_class == ELFCLASS32) {
216 num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum;
217 off = ((Elf32_Ehdr*)elf->e_ehdr)->e_shoff;
218 entsz = ((Elf32_Ehdr*)elf->e_ehdr)->e_shentsize;
221 else if (elf->e_class == ELFCLASS64) {
222 num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum;
223 off = ((Elf64_Ehdr*)elf->e_ehdr)->e_shoff;
224 entsz = ((Elf64_Ehdr*)elf->e_ehdr)->e_shentsize;
226 * Check for overflow on 32-bit systems
228 if (overflow(off, ((Elf64_Ehdr*)elf->e_ehdr)->e_shoff, Elf64_Off)) {
229 seterr(ERROR_OUTSIDE);
233 #endif /* __LIBELF64 */
235 seterr(ERROR_UNIMPLEMENTED);
248 if (off < 0 || off > elf->e_size) {
249 seterr(ERROR_OUTSIDE);
253 src.d_type = ELF_T_SHDR;
254 src.d_version = elf->e_version;
255 src.d_size = _fsize(elf->e_class, src.d_version, ELF_T_SHDR);
256 elf_assert(src.d_size);
257 #if ENABLE_EXTENDED_FORMAT
258 if (entsz < src.d_size) {
259 #else /* ENABLE_EXTENDED_FORMAT */
260 if (entsz != src.d_size) {
261 #endif /* ENABLE_EXTENDED_FORMAT */
262 seterr(ERROR_EHDR_SHENTSIZE);
265 dst.d_version = EV_CURRENT;
272 #endif /* __LIBELF64 */
276 * Overflow in ehdr->e_shnum.
277 * Get real value from first SHDR.
279 if (elf->e_size - off < entsz) {
280 seterr(ERROR_TRUNC_SHDR);
283 if (elf->e_rawdata) {
284 src.d_buf = elf->e_rawdata + off;
287 src.d_buf = elf->e_data + off;
290 dst.d_size = sizeof(u);
291 if (!_elf_xlatetom(elf, &dst, &src)) {
294 elf_assert(dst.d_size == _msize(elf->e_class, EV_CURRENT, ELF_T_SHDR));
295 elf_assert(dst.d_type == ELF_T_SHDR);
296 if (elf->e_class == ELFCLASS32) {
297 num = u.sh32.sh_size;
300 else if (elf->e_class == ELFCLASS64) {
301 num = u.sh64.sh_size;
303 * Check for overflow on 32-bit systems
305 if (overflow(num, u.sh64.sh_size, Elf64_Xword)) {
306 seterr(ERROR_OUTSIDE);
310 #endif /* __LIBELF64 */
313 if ((elf->e_size - off) / entsz < num) {
314 seterr(ERROR_TRUNC_SHDR);
318 if (!(head = (struct tmp*)malloc(num * sizeof(struct tmp)))) {
319 seterr(ERROR_MEM_SCN);
322 for (scn = NULL, i = num; i-- > 0; ) {
323 head[i].scn = _elf_scn_init;
324 head[i].data = _elf_data_init;
325 head[i].scn.s_link = scn;
327 elf->e_scn_n = &head[i].scn;
332 if (elf->e_rawdata) {
333 src.d_buf = elf->e_rawdata + off + i * entsz;
336 src.d_buf = elf->e_data + off + i * entsz;
338 dst.d_buf = &scn->s_uhdr;
339 dst.d_size = sizeof(scn->s_uhdr);
340 if (!_elf_xlatetom(elf, &dst, &src)) {
345 elf_assert(dst.d_size == _msize(elf->e_class, EV_CURRENT, ELF_T_SHDR));
346 elf_assert(dst.d_type == ELF_T_SHDR);
355 if (elf->e_class == ELFCLASS32) {
356 Elf32_Shdr *shdr = &scn->s_shdr32;
358 scn->s_type = shdr->sh_type;
359 scn->s_size = shdr->sh_size;
360 scn->s_offset = shdr->sh_offset;
361 sd->sd_data.d_align = shdr->sh_addralign;
362 sd->sd_data.d_type = _elf_scn_type(scn->s_type);
365 else if (elf->e_class == ELFCLASS64) {
366 Elf64_Shdr *shdr = &scn->s_shdr64;
368 scn->s_type = shdr->sh_type;
369 scn->s_size = shdr->sh_size;
370 scn->s_offset = shdr->sh_offset;
371 sd->sd_data.d_align = shdr->sh_addralign;
373 * Check for overflow on 32-bit systems
375 if (overflow(scn->s_size, shdr->sh_size, Elf64_Xword)
376 || overflow(scn->s_offset, shdr->sh_offset, Elf64_Off)
377 || overflow(sd->sd_data.d_align, shdr->sh_addralign, Elf64_Xword)) {
378 seterr(ERROR_OUTSIDE);
381 sd->sd_data.d_type = _elf_scn_type(scn->s_type);
385 * Some 64-bit architectures use 64-bit entries in the
386 * .hash section. This violates the ELF standard, and
387 * should be fixed. It's mostly harmless as long as the
388 * binary and the machine running your program have the
389 * same byte order, but you're in trouble if they don't,
390 * and if the entry size is wrong.
392 * As a workaround, I let libelf guess the right size
393 * for the binary. This relies pretty much on the fact
394 * that the binary provides correct data in the section
395 * headers. If it doesn't, it's probably broken anyway.
396 * Therefore, libelf uses a standard conforming value
397 * when it's not absolutely sure.
399 if (scn->s_type == SHT_HASH) {
403 * sh_entsize must reflect the entry size
405 if (shdr->sh_entsize == ELF64_FSZ_ADDR) {
409 * sh_size must be a multiple of sh_entsize
411 if (shdr->sh_size % ELF64_FSZ_ADDR == 0) {
415 * There must be room for at least 2 entries
417 if (shdr->sh_size >= 2 * ELF64_FSZ_ADDR) {
421 * sh_addralign must be correctly set
423 if (shdr->sh_addralign == ELF64_FSZ_ADDR) {
427 * The section must be properly aligned
429 if (shdr->sh_offset % ELF64_FSZ_ADDR == 0) {
432 /* XXX: also look at the data? */
434 * Make a conservative decision...
437 sd->sd_data.d_type = ELF_T_ADDR;
444 #endif /* __LIBELF64 */
445 /* we already had this
447 seterr(ERROR_UNIMPLEMENTED);
452 sd->sd_data.d_size = scn->s_size;
453 sd->sd_data.d_version = _elf_version;
455 elf_assert(scn == &head[0].scn);
456 elf->e_scn_1 = &head[0].scn;
457 head[0].scn.s_freeme = 1;
463 _elf_cook_file(Elf *elf) {
464 elf->e_ehdr = _elf_item(NULL, elf, ELF_T_EHDR, 0);
469 * Note: _elf_cook_phdr may require the first section header!
471 if (!_elf_cook_shdr(elf)) {
474 if (!_elf_cook_phdr(elf)) {
481 _elf_cook(Elf *elf) {
482 elf_assert(_elf_scn_init.s_magic == SCN_MAGIC);
483 elf_assert(_elf_data_init.sd_magic == DATA_MAGIC);
485 elf_assert(elf->e_magic == ELF_MAGIC);
486 elf_assert(elf->e_kind == ELF_K_ELF);
487 elf_assert(!elf->e_ehdr);
488 if (!valid_version(elf->e_version)) {
489 seterr(ERROR_UNKNOWN_VERSION);
491 else if (!valid_encoding(elf->e_encoding)) {
492 seterr(ERROR_UNKNOWN_ENCODING);
494 else if (valid_class(elf->e_class)) {
495 return _elf_cook_file(elf);
498 seterr(ERROR_UNKNOWN_CLASS);