Deleted Added
sdiff udiff text old ( 289071 ) new ( 295577 )
full compact
1/*-
2 * Copyright (c) 2007-2012 Kai Wang
3 * Copyright (c) 2003 David O'Brien. All rights reserved.
4 * Copyright (c) 2001 Jake Burkholder
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 36 unchanged lines hidden (view full) ---

45
46#ifdef USE_LIBARCHIVE_AR
47#include <archive.h>
48#include <archive_entry.h>
49#endif
50
51#include "_elftc.h"
52
53ELFTC_VCSID("$Id: elfdump.c 3391 2016-02-05 19:43:01Z emaste $");
54
55#if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
56#include "native-elf-format.h"
57#if ELFTC_CLASS == ELFCLASS32
58typedef Elf32_Nhdr Elf_Note;
59#else
60typedef Elf64_Nhdr Elf_Note;
61#endif

--- 88 unchanged lines hidden (view full) ---

150 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
151}
152#endif
153
154/* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
155static const char *
156d_tags(uint64_t tag)
157{
158 static char unknown_buf[64];
159
160 switch (tag) {
161 case DT_NULL: return "DT_NULL";
162 case DT_NEEDED: return "DT_NEEDED";
163 case DT_PLTRELSZ: return "DT_PLTRELSZ";
164 case DT_PLTGOT: return "DT_PLTGOT";
165 case DT_HASH: return "DT_HASH";
166 case DT_STRTAB: return "DT_STRTAB";
167 case DT_SYMTAB: return "DT_SYMTAB";
168 case DT_RELA: return "DT_RELA";
169 case DT_RELASZ: return "DT_RELASZ";
170 case DT_RELAENT: return "DT_RELAENT";
171 case DT_STRSZ: return "DT_STRSZ";
172 case DT_SYMENT: return "DT_SYMENT";
173 case DT_INIT: return "DT_INIT";
174 case DT_FINI: return "DT_FINI";
175 case DT_SONAME: return "DT_SONAME";
176 case DT_RPATH: return "DT_RPATH";
177 case DT_SYMBOLIC: return "DT_SYMBOLIC";
178 case DT_REL: return "DT_REL";
179 case DT_RELSZ: return "DT_RELSZ";
180 case DT_RELENT: return "DT_RELENT";
181 case DT_PLTREL: return "DT_PLTREL";
182 case DT_DEBUG: return "DT_DEBUG";
183 case DT_TEXTREL: return "DT_TEXTREL";
184 case DT_JMPREL: return "DT_JMPREL";
185 case DT_BIND_NOW: return "DT_BIND_NOW";
186 case DT_INIT_ARRAY: return "DT_INIT_ARRAY";
187 case DT_FINI_ARRAY: return "DT_FINI_ARRAY";
188 case DT_INIT_ARRAYSZ: return "DT_INIT_ARRAYSZ";
189 case DT_FINI_ARRAYSZ: return "DT_FINI_ARRAYSZ";
190 case DT_RUNPATH: return "DT_RUNPATH";
191 case DT_FLAGS: return "DT_FLAGS";
192 case DT_PREINIT_ARRAY: return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */
193 case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ";
194 /* 0x6000000D - 0x6ffff000 operating system-specific semantics */
195 case 0x6ffffdf5: return "DT_GNU_PRELINKED";
196 case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ";
197 case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ";
198 case 0x6ffffdf8: return "DT_SUNW_CHECKSUM";
199 case DT_PLTPADSZ: return "DT_PLTPADSZ";
200 case DT_MOVEENT: return "DT_MOVEENT";
201 case DT_MOVESZ: return "DT_MOVESZ";
202 case 0x6ffffdfc: return "DT_FEATURE";
203 case DT_POSFLAG_1: return "DT_POSFLAG_1";
204 case DT_SYMINSZ: return "DT_SYMINSZ";
205 case DT_SYMINENT: return "DT_SYMINENT (DT_VALRNGHI)";
206 case DT_ADDRRNGLO: return "DT_ADDRRNGLO";
207 case DT_GNU_HASH: return "DT_GNU_HASH";
208 case 0x6ffffef8: return "DT_GNU_CONFLICT";
209 case 0x6ffffef9: return "DT_GNU_LIBLIST";
210 case 0x6ffffefa: return "DT_CONFIG";
211 case 0x6ffffefb: return "DT_DEPAUDIT";
212 case 0x6ffffefc: return "DT_AUDIT";
213 case 0x6ffffefd: return "DT_PLTPAD";
214 case 0x6ffffefe: return "DT_MOVETAB";
215 case DT_SYMINFO: return "DT_SYMINFO (DT_ADDRRNGHI)";
216 case DT_RELACOUNT: return "DT_RELACOUNT";
217 case DT_RELCOUNT: return "DT_RELCOUNT";
218 case DT_FLAGS_1: return "DT_FLAGS_1";
219 case DT_VERDEF: return "DT_VERDEF";
220 case DT_VERDEFNUM: return "DT_VERDEFNUM";
221 case DT_VERNEED: return "DT_VERNEED";
222 case DT_VERNEEDNUM: return "DT_VERNEEDNUM";
223 case 0x6ffffff0: return "DT_GNU_VERSYM";
224 /* 0x70000000 - 0x7fffffff processor-specific semantics */
225 case 0x70000000: return "DT_IA_64_PLT_RESERVE";
226 case 0x7ffffffd: return "DT_SUNW_AUXILIARY";
227 case 0x7ffffffe: return "DT_SUNW_USED";
228 case 0x7fffffff: return "DT_SUNW_FILTER";
229 }
230
231 snprintf(unknown_buf, sizeof(unknown_buf),
232 "<unknown: %#llx>", (unsigned long long)tag);
233 return (unknown_buf);
234}
235
236static const char *
237e_machines(unsigned int mach)
238{
239 static char machdesc[64];
240
241 switch (mach) {

--- 71 unchanged lines hidden (view full) ---

313 snprintf(num, sizeof(num), "%d", ndx);
314 return (num);
315 }
316 }
317}
318
319/* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
320static const char *
321sh_types(uint64_t mach, uint64_t sht) {
322 static char unknown_buf[64];
323
324 if (sht < 0x60000000) {
325 switch (sht) {
326 case SHT_NULL: return "SHT_NULL";
327 case SHT_PROGBITS: return "SHT_PROGBITS";
328 case SHT_SYMTAB: return "SHT_SYMTAB";
329 case SHT_STRTAB: return "SHT_STRTAB";
330 case SHT_RELA: return "SHT_RELA";
331 case SHT_HASH: return "SHT_HASH";
332 case SHT_DYNAMIC: return "SHT_DYNAMIC";
333 case SHT_NOTE: return "SHT_NOTE";
334 case SHT_NOBITS: return "SHT_NOBITS";
335 case SHT_REL: return "SHT_REL";
336 case SHT_SHLIB: return "SHT_SHLIB";
337 case SHT_DYNSYM: return "SHT_DYNSYM";
338 case SHT_INIT_ARRAY: return "SHT_INIT_ARRAY";
339 case SHT_FINI_ARRAY: return "SHT_FINI_ARRAY";
340 case SHT_PREINIT_ARRAY: return "SHT_PREINIT_ARRAY";
341 case SHT_GROUP: return "SHT_GROUP";
342 case SHT_SYMTAB_SHNDX: return "SHT_SYMTAB_SHNDX";
343 }
344 } else if (sht < 0x70000000) {
345 /* 0x60000000-0x6fffffff operating system-specific semantics */
346 switch (sht) {
347 case 0x6ffffff0: return "XXX:VERSYM";
348 case SHT_SUNW_dof: return "SHT_SUNW_dof";
349 case SHT_GNU_HASH: return "SHT_GNU_HASH";
350 case 0x6ffffff7: return "SHT_GNU_LIBLIST";
351 case 0x6ffffffc: return "XXX:VERDEF";
352 case SHT_SUNW_verdef: return "SHT_SUNW(GNU)_verdef";
353 case SHT_SUNW_verneed: return "SHT_SUNW(GNU)_verneed";
354 case SHT_SUNW_versym: return "SHT_SUNW(GNU)_versym";
355 }
356 } else if (sht < 0x80000000) {
357 /* 0x70000000 - 0x7fffffff processor-specific semantics */
358 switch (mach) {
359 case EM_ARM:
360 switch (sht) {
361 case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
362 case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
363 case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
364 case SHT_ARM_DEBUGOVERLAY:
365 return "SHT_ARM_DEBUGOVERLAY";
366 case SHT_ARM_OVERLAYSECTION:
367 return "SHT_ARM_OVERLAYSECTION";
368 }
369 break;
370 case EM_IA_64:
371 switch (sht) {
372 case 0x70000000: return "SHT_IA_64_EXT";
373 case 0x70000001: return "SHT_IA_64_UNWIND";
374 }
375 break;
376 case EM_MIPS:
377 switch (sht) {
378 case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
379 case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
380 case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
381 }
382 break;
383 }
384 switch (sht) {
385 case 0x7ffffffd: return "XXX:AUXILIARY";
386 case 0x7fffffff: return "XXX:FILTER";
387 }
388 }
389 /* 0x80000000 - 0xffffffff application programs */
390
391 snprintf(unknown_buf, sizeof(unknown_buf),
392 "<unknown: %#llx>", (unsigned long long)sht);
393 return (unknown_buf);
394}
395
396/*
397 * Define known section flags. These flags are defined in the order
398 * they are to be printed out.
399 */
400#define DEFINE_SHFLAGS() \
401 DEFINE_SHF(WRITE) \

--- 25 unchanged lines hidden (view full) ---

427
428 DEFINE_SHFLAGS()
429
430 flg[strlen(flg) - 1] = '\0'; /* Remove the trailing "|". */
431
432 return (flg);
433}
434
435static const char *
436st_type(unsigned int mach, unsigned int type)
437{
438 static char s_type[32];
439
440 switch (type) {
441 case STT_NOTYPE: return "STT_NOTYPE";
442 case STT_OBJECT: return "STT_OBJECT";
443 case STT_FUNC: return "STT_FUNC";
444 case STT_SECTION: return "STT_SECTION";
445 case STT_FILE: return "STT_FILE";
446 case STT_COMMON: return "STT_COMMON";
447 case STT_TLS: return "STT_TLS";
448 case 13:
449 if (mach == EM_SPARCV9)
450 return "STT_SPARC_REGISTER";
451 break;
452 }
453 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
454 return (s_type);
455}
456
457static const char *
458st_type_S(unsigned int type)
459{
460 static char s_type[32];
461
462 switch (type) {
463 case STT_NOTYPE: return "NOTY";
464 case STT_OBJECT: return "OBJT";
465 case STT_FUNC: return "FUNC";
466 case STT_SECTION: return "SECT";
467 case STT_FILE: return "FILE";
468 }
469 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
470 return (s_type);
471}
472
473static const char *
474st_bindings(unsigned int sbind)
475{
476 static char s_sbind[32];
477
478 switch (sbind) {
479 case STB_LOCAL: return "STB_LOCAL";
480 case STB_GLOBAL: return "STB_GLOBAL";
481 case STB_WEAK: return "STB_WEAK";
482 case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
483 default:
484 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
485 return "OS";
486 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
487 return "PROC";
488 else
489 snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
490 sbind);
491 return (s_sbind);
492 }
493}
494
495static const char *
496st_bindings_S(unsigned int sbind)
497{
498 static char s_sbind[32];
499
500 switch (sbind) {
501 case STB_LOCAL: return "LOCL";
502 case STB_GLOBAL: return "GLOB";
503 case STB_WEAK: return "WEAK";
504 case STB_GNU_UNIQUE: return "UNIQ";
505 default:
506 if (sbind >= STB_LOOS && sbind <= STB_HIOS)
507 return "OS";
508 else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
509 return "PROC";
510 else
511 snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
512 sbind);
513 return (s_sbind);
514 }
515}
516
517static unsigned char st_others[] = {
518 'D', 'I', 'H', 'P'
519};
520
521static const char *
522r_type(unsigned int mach, unsigned int type)
523{
524 switch(mach) {
525 case EM_NONE: return "";
526 case EM_386:
527 case EM_IAMCU:
528 switch(type) {
529 case 0: return "R_386_NONE";
530 case 1: return "R_386_32";
531 case 2: return "R_386_PC32";
532 case 3: return "R_386_GOT32";
533 case 4: return "R_386_PLT32";
534 case 5: return "R_386_COPY";
535 case 6: return "R_386_GLOB_DAT";
536 case 7: return "R_386_JUMP_SLOT";
537 case 8: return "R_386_RELATIVE";
538 case 9: return "R_386_GOTOFF";
539 case 10: return "R_386_GOTPC";
540 case 14: return "R_386_TLS_TPOFF";
541 case 15: return "R_386_TLS_IE";
542 case 16: return "R_386_TLS_GOTIE";
543 case 17: return "R_386_TLS_LE";
544 case 18: return "R_386_TLS_GD";

--- 326 unchanged lines hidden (view full) ---

871 switch(type) {
872 case 0: return "R_X86_64_NONE";
873 case 1: return "R_X86_64_64";
874 case 2: return "R_X86_64_PC32";
875 case 3: return "R_X86_64_GOT32";
876 case 4: return "R_X86_64_PLT32";
877 case 5: return "R_X86_64_COPY";
878 case 6: return "R_X86_64_GLOB_DAT";
879 case 7: return "R_X86_64_JUMP_SLOT";
880 case 8: return "R_X86_64_RELATIVE";
881 case 9: return "R_X86_64_GOTPCREL";
882 case 10: return "R_X86_64_32";
883 case 11: return "R_X86_64_32S";
884 case 12: return "R_X86_64_16";
885 case 13: return "R_X86_64_PC16";
886 case 14: return "R_X86_64_8";
887 case 15: return "R_X86_64_PC8";

--- 822 unchanged lines hidden (view full) ---

1710 PRT("\nSection Header[%d]:", i);
1711 PRT(" sh_name: %s\n", s->name);
1712 PRT(" sh_addr: %#-14jx", (uintmax_t)s->addr);
1713 if (s->flags != 0)
1714 PRT(" sh_flags: [ %s ]\n", sh_flags(s->flags));
1715 else
1716 PRT(" sh_flags: 0\n");
1717 PRT(" sh_size: %#-14jx", (uintmax_t)s->sz);
1718 PRT(" sh_type: [ %s ]\n",
1719 sh_types(ed->ehdr.e_machine, s->type));
1720 PRT(" sh_offset: %#-14jx", (uintmax_t)s->off);
1721 PRT(" sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1722 PRT(" sh_link: %-14u", s->link);
1723 PRT(" sh_info: %u\n", s->info);
1724 PRT(" sh_addralign: %#jx\n", (uintmax_t)s->align);
1725 } else {
1726 PRT("\n");
1727 PRT("entry: %ju\n", (uintmax_t)i);
1728 PRT("\tsh_name: %s\n", s->name);
1729 PRT("\tsh_type: %s\n",
1730 sh_types(ed->ehdr.e_machine, s->type));
1731 PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1732 PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1733 PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
1734 PRT("\tsh_size: %ju\n", (uintmax_t)s->sz);
1735 PRT("\tsh_link: %u\n", s->link);
1736 PRT("\tsh_info: %u\n", s->info);
1737 PRT("\tsh_addralign: %ju\n", (uintmax_t)s->align);
1738 PRT("\tsh_entsize: %ju\n", (uintmax_t)s->entsize);

--- 110 unchanged lines hidden (view full) ---

1849 PRT("%10s ", idx);
1850 else
1851 PRT("%10s ", idx);
1852 PRT("0x%8.8jx ", (uintmax_t)sym.st_value);
1853 if (ed->ec == ELFCLASS32)
1854 PRT("0x%8.8jx ", (uintmax_t)sym.st_size);
1855 else
1856 PRT("0x%12.12jx ", (uintmax_t)sym.st_size);
1857 PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1858 PRT("%s ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
1859 PRT("%c ", st_others[sym.st_other]);
1860 PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1861 PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
1862 PRT("%s\n", name);
1863 } else {
1864 PRT("\nentry: %d\n", j);
1865 PRT("\tst_name: %s\n", name);
1866 PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1867 PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1868 PRT("\tst_info: %s %s\n",
1869 st_type(ed->ehdr.e_machine,
1870 GELF_ST_TYPE(sym.st_info)),
1871 st_bindings(GELF_ST_BIND(sym.st_info)));
1872 PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1873 }
1874 }
1875}
1876
1877/*
1878 * Dump the symbol tables. (.dynsym and .symtab)
1879 */

--- 398 unchanged lines hidden (view full) ---

2278 PRT("addend symbol\n");
2279 } else {
2280 PRT(" ndx addr value ");
2281 PRT("reloc addend symbol\n");
2282 }
2283 for(i = 0; i < len; i++) {
2284 PRT("[%5.5d] ", i);
2285 if (ed->ec == ELFCLASS32) {
2286 PRT("%-8.8jx ",
2287 (uintmax_t) (s->addr + i * s->entsize));
2288 PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
2289 } else {
2290 PRT("%-16.16jx ",
2291 (uintmax_t) (s->addr + i * s->entsize));
2292 PRT("%-16.16jx ",
2293 (uintmax_t) *((uint64_t *)dst.d_buf + i));
2294 }
2295 PRT("%-18s ", r_type(ed->ehdr.e_machine,
2296 GELF_R_TYPE(got[i].u_r.rel.r_info)));
2297 if (ed->ec == ELFCLASS32)
2298 PRT("%-8.8jd ",
2299 (intmax_t)got[i].u_r.rela.r_addend);
2300 else
2301 PRT("%-12.12jd ",

--- 4 unchanged lines hidden (view full) ---

2306 }
2307 free(got);
2308 } else {
2309 for(i = 0; i < len; i++) {
2310 PRT("\nentry: %d\n", i);
2311 if (ed->ec == ELFCLASS32)
2312 PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2313 else
2314 PRT("\t%#jx\n",
2315 (uintmax_t) *((uint64_t *)dst.d_buf + i));
2316 }
2317 }
2318}
2319
2320/*
2321 * Dump the content of Global Offset Table section.
2322 */
2323static void

--- 649 unchanged lines hidden ---