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 3474 2016-05-17 20:44:53Z 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 --- 196 unchanged lines hidden (view full) --- 258 case EM_AARCH64:return "EM_AARCH64"; 259 case EM_RISCV: return "EM_RISCV"; 260 } 261 snprintf(machdesc, sizeof(machdesc), 262 "(unknown machine) -- type 0x%x", mach); 263 return (machdesc); 264} 265 |
266static const char * 267elf_type_str(unsigned int type) 268{ 269 static char s_type[32]; |
270 |
271 switch (type) 272 { 273 case ET_NONE: return "ET_NONE"; 274 case ET_REL: return "ET_REL"; 275 case ET_EXEC: return "ET_EXEC"; 276 case ET_DYN: return "ET_DYN"; 277 case ET_CORE: return "ET_CORE"; 278 } 279 if (type >= ET_LOPROC) 280 snprintf(s_type, sizeof(s_type), "<proc: %#x>", type); 281 else if (type >= ET_LOOS && type <= ET_HIOS) 282 snprintf(s_type, sizeof(s_type), "<os: %#x>", type); 283 else 284 snprintf(s_type, sizeof(s_type), "<unknown: %#x", type); 285 return (s_type); 286} |
287 |
288static const char * 289elf_version_str(unsigned int ver) 290{ 291 static char s_ver[32]; |
292 |
293 switch (ver) { 294 case EV_NONE: return "EV_NONE"; 295 case EV_CURRENT: return "EV_CURRENT"; 296 } 297 snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", ver); 298 return (s_ver); 299} |
300 |
301static const char * 302elf_class_str(unsigned int class) 303{ 304 static char s_class[32]; 305 306 switch (class) { 307 case ELFCLASSNONE: return "ELFCLASSNONE"; 308 case ELFCLASS32: return "ELFCLASS32"; 309 case ELFCLASS64: return "ELFCLASS64"; 310 } 311 snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class); 312 return (s_class); 313} 314 315static const char * 316elf_data_str(unsigned int data) 317{ 318 static char s_data[32]; 319 320 switch (data) { 321 case ELFDATANONE: return "ELFDATANONE"; 322 case ELFDATA2LSB: return "ELFDATA2LSB"; 323 case ELFDATA2MSB: return "ELFDATA2MSB"; 324 } 325 snprintf(s_data, sizeof(s_data), "<unknown: %#x>", data); 326 return (s_data); 327} 328 |
329static const char *ei_abis[256] = { 330 "ELFOSABI_NONE", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX", 331 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS", "ELFOSABI_AIX", 332 "ELFOSABI_IRIX", "ELFOSABI_FREEBSD", "ELFOSABI_TRU64", 333 "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD", |
334 [17] = "ELFOSABI_CLOUDABI", |
335 [255] = "ELFOSABI_STANDALONE" 336}; 337 |
338static const char * 339elf_phdr_type_str(unsigned int type) 340{ 341 static char s_type[32]; |
342 |
343 switch (type) { 344 case PT_NULL: return "PT_NULL"; 345 case PT_LOAD: return "PT_LOAD"; 346 case PT_DYNAMIC: return "PT_DYNAMIC"; 347 case PT_INTERP: return "PT_INTERP"; 348 case PT_NOTE: return "PT_NOTE"; 349 case PT_SHLIB: return "PT_SHLIB"; 350 case PT_PHDR: return "PT_PHDR"; 351 case PT_TLS: return "PT_TLS"; 352 case PT_GNU_EH_FRAME: return "PT_GNU_EH_FRAME"; 353 case PT_GNU_STACK: return "PT_GNU_STACK"; 354 case PT_GNU_RELRO: return "PT_GNU_RELRO"; 355 } 356 snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type); 357 return (s_type); 358} 359 |
360static const char *p_flags[] = { 361 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R", 362 "PF_X|PF_W|PF_R" 363}; 364 365static const char * 366sh_name(struct elfdump *ed, int ndx) 367{ --- 99 unchanged lines hidden (view full) --- 467 DEFINE_SHF(ALLOC) \ 468 DEFINE_SHF(EXECINSTR) \ 469 DEFINE_SHF(MERGE) \ 470 DEFINE_SHF(STRINGS) \ 471 DEFINE_SHF(INFO_LINK) \ 472 DEFINE_SHF(LINK_ORDER) \ 473 DEFINE_SHF(OS_NONCONFORMING) \ 474 DEFINE_SHF(GROUP) \ |
475 DEFINE_SHF(TLS) \ 476 DEFINE_SHF(COMPRESSED) |
477 478#undef DEFINE_SHF 479#define DEFINE_SHF(F) "SHF_" #F "|" 480#define ALLSHFLAGS DEFINE_SHFLAGS() 481 482static const char * 483sh_flags(uint64_t shf) 484{ --- 94 unchanged lines hidden (view full) --- 579 return (s_sbind); 580 } 581} 582 583static unsigned char st_others[] = { 584 'D', 'I', 'H', 'P' 585}; 586 |
587static void add_name(struct elfdump *ed, const char *name); 588static void elf_print_object(struct elfdump *ed); 589static void elf_print_elf(struct elfdump *ed); 590static void elf_print_ehdr(struct elfdump *ed); 591static void elf_print_phdr(struct elfdump *ed); 592static void elf_print_shdr(struct elfdump *ed); 593static void elf_print_symtab(struct elfdump *ed, int i); 594static void elf_print_symtabs(struct elfdump *ed); --- 16 unchanged lines hidden (view full) --- 611static void elf_print_svr4_hash64(struct elfdump *ed, struct section *s); 612static void elf_print_gnu_hash(struct elfdump *ed, struct section *s); 613static void elf_print_hash(struct elfdump *ed); 614static void elf_print_checksum(struct elfdump *ed); 615static void find_gotrel(struct elfdump *ed, struct section *gs, 616 struct rel_entry *got); 617static struct spec_name *find_name(struct elfdump *ed, const char *name); 618static int get_ent_count(const struct section *s, int *ent_count); |
619static const char *get_symbol_name(struct elfdump *ed, uint32_t symtab, int i); |
620static const char *get_string(struct elfdump *ed, int strtab, size_t off); 621static void get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs); 622static void load_sections(struct elfdump *ed); 623static void unload_sections(struct elfdump *ed); 624static void usage(void); 625#ifdef USE_LIBARCHIVE_AR 626static int ac_detect_ar(int fd); 627static void ac_print_ar(struct elfdump *ed, int fd); --- 162 unchanged lines hidden (view full) --- 790{ 791 struct archive *a; 792 struct archive_entry *entry; 793 struct arsym_entry *arsym; 794 const char *name; 795 char idx[10], *b; 796 void *buff; 797 size_t size; |
798 uint32_t cnt, i; 799 int r; |
800 801 if (lseek(fd, 0, SEEK_SET) == -1) 802 err(EXIT_FAILURE, "lseek failed"); 803 if ((a = archive_read_new()) == NULL) 804 errx(EXIT_FAILURE, "%s", archive_error_string(a)); 805 archive_read_support_format_ar(a); 806 AC(archive_read_open_fd(a, fd, 10240)); 807 for(;;) { --- 32 unchanged lines hidden (view full) --- 840 if (cnt == 0) { 841 free(buff); 842 continue; 843 } 844 arsym = calloc(cnt, sizeof(*arsym)); 845 if (arsym == NULL) 846 err(EXIT_FAILURE, "calloc failed"); 847 b += sizeof(uint32_t); |
848 for (i = 0; i < cnt; i++) { |
849 arsym[i].off = be32dec(b); 850 b += sizeof(uint32_t); 851 } |
852 for (i = 0; i < cnt; i++) { |
853 arsym[i].sym_name = b; 854 b += strlen(b) + 1; 855 } 856 if (ed->flags & SOLARIS_FMT) { 857 PRT("\nSymbol Table: (archive)\n"); 858 PRT(" index offset symbol\n"); 859 } else 860 PRT("\nsymbol table (archive):\n"); |
861 for (i = 0; i < cnt; i++) { |
862 if (ed->flags & SOLARIS_FMT) { 863 snprintf(idx, sizeof(idx), "[%d]", i); 864 PRT("%10s ", idx); 865 PRT("0x%8.8jx ", 866 (uintmax_t)arsym[i].off); 867 PRT("%s\n", arsym[i].sym_name); 868 } else { 869 PRT("\nentry: %d\n", i); --- 39 unchanged lines hidden (view full) --- 909static void 910elf_print_ar(struct elfdump *ed, int fd) 911{ 912 Elf *e; 913 Elf_Arhdr *arh; 914 Elf_Arsym *arsym; 915 Elf_Cmd cmd; 916 char idx[10]; |
917 size_t cnt, i; |
918 919 ed->ar = ed->elf; 920 921 if (ed->flags & PRINT_ARSYM) { 922 cnt = 0; 923 if ((arsym = elf_getarsym(ed->ar, &cnt)) == NULL) { 924 warnx("elf_getarsym failed: %s", elf_errmsg(-1)); 925 goto print_members; 926 } 927 if (cnt == 0) 928 goto print_members; 929 if (ed->flags & SOLARIS_FMT) { 930 PRT("\nSymbol Table: (archive)\n"); 931 PRT(" index offset member name and symbol\n"); 932 } else 933 PRT("\nsymbol table (archive):\n"); |
934 for (i = 0; i < cnt - 1; i++) { |
935 if (elf_rand(ed->ar, arsym[i].as_off) != 936 arsym[i].as_off) { 937 warnx("elf_rand failed: %s", elf_errmsg(-1)); 938 break; 939 } 940 if ((e = elf_begin(fd, ELF_C_READ, ed->ar)) == NULL) { 941 warnx("elf_begin failed: %s", elf_errmsg(-1)); 942 break; 943 } 944 if ((arh = elf_getarhdr(e)) == NULL) { 945 warnx("elf_getarhdr failed: %s", 946 elf_errmsg(-1)); 947 break; 948 } 949 if (ed->flags & SOLARIS_FMT) { |
950 snprintf(idx, sizeof(idx), "[%zu]", i); |
951 PRT("%10s ", idx); 952 PRT("0x%8.8jx ", 953 (uintmax_t)arsym[i].as_off); 954 PRT("(%s):%s\n", arh->ar_name, 955 arsym[i].as_name); 956 } else { |
957 PRT("\nentry: %zu\n", i); |
958 PRT("\toffset: %#jx\n", 959 (uintmax_t)arsym[i].as_off); 960 PRT("\tmember: %s\n", arh->ar_name); 961 PRT("\tsymbol: %s\n", arsym[i].as_name); 962 } 963 elf_end(e); 964 } 965 --- 244 unchanged lines hidden (view full) --- 1210 return (NULL); 1211} 1212 1213/* 1214 * Retrieve the name of a symbol using the section index of the symbol 1215 * table and the index of the symbol within that table. 1216 */ 1217static const char * |
1218get_symbol_name(struct elfdump *ed, uint32_t symtab, int i) |
1219{ 1220 static char sname[64]; 1221 struct section *s; 1222 const char *name; 1223 GElf_Sym sym; 1224 Elf_Data *data; 1225 int elferr; 1226 |
1227 if (symtab >= ed->shnum) 1228 return (""); |
1229 s = &ed->sl[symtab]; 1230 if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) 1231 return (""); 1232 (void) elf_errno(); 1233 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1234 elferr = elf_errno(); 1235 if (elferr != 0) 1236 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); --- 40 unchanged lines hidden (view full) --- 1277 return; 1278 1279 if (ed->flags & SOLARIS_FMT) { 1280 PRT("\nELF Header\n"); 1281 PRT(" ei_magic: { %#x, %c, %c, %c }\n", 1282 ed->ehdr.e_ident[0], ed->ehdr.e_ident[1], 1283 ed->ehdr.e_ident[2], ed->ehdr.e_ident[3]); 1284 PRT(" ei_class: %-18s", |
1285 elf_class_str(ed->ehdr.e_ident[EI_CLASS])); 1286 PRT(" ei_data: %s\n", 1287 elf_data_str(ed->ehdr.e_ident[EI_DATA])); |
1288 PRT(" e_machine: %-18s", e_machines(ed->ehdr.e_machine)); |
1289 PRT(" e_version: %s\n", 1290 elf_version_str(ed->ehdr.e_version)); 1291 PRT(" e_type: %s\n", elf_type_str(ed->ehdr.e_type)); |
1292 PRT(" e_flags: %18d\n", ed->ehdr.e_flags); 1293 PRT(" e_entry: %#18jx", (uintmax_t)ed->ehdr.e_entry); 1294 PRT(" e_ehsize: %6d", ed->ehdr.e_ehsize); 1295 PRT(" e_shstrndx:%5d\n", ed->ehdr.e_shstrndx); 1296 PRT(" e_shoff: %#18jx", (uintmax_t)ed->ehdr.e_shoff); 1297 PRT(" e_shentsize: %3d", ed->ehdr.e_shentsize); 1298 PRT(" e_shnum: %5d\n", ed->ehdr.e_shnum); 1299 PRT(" e_phoff: %#18jx", (uintmax_t)ed->ehdr.e_phoff); 1300 PRT(" e_phentsize: %3d", ed->ehdr.e_phentsize); 1301 PRT(" e_phnum: %5d\n", ed->ehdr.e_phnum); 1302 } else { 1303 PRT("\nelf header:\n"); 1304 PRT("\n"); 1305 PRT("\te_ident: %s %s %s\n", |
1306 elf_class_str(ed->ehdr.e_ident[EI_CLASS]), 1307 elf_data_str(ed->ehdr.e_ident[EI_DATA]), |
1308 ei_abis[ed->ehdr.e_ident[EI_OSABI]]); |
1309 PRT("\te_type: %s\n", elf_type_str(ed->ehdr.e_type)); |
1310 PRT("\te_machine: %s\n", e_machines(ed->ehdr.e_machine)); |
1311 PRT("\te_version: %s\n", elf_version_str(ed->ehdr.e_version)); |
1312 PRT("\te_entry: %#jx\n", (uintmax_t)ed->ehdr.e_entry); 1313 PRT("\te_phoff: %ju\n", (uintmax_t)ed->ehdr.e_phoff); 1314 PRT("\te_shoff: %ju\n", (uintmax_t) ed->ehdr.e_shoff); 1315 PRT("\te_flags: %u\n", ed->ehdr.e_flags); 1316 PRT("\te_ehsize: %u\n", ed->ehdr.e_ehsize); 1317 PRT("\te_phentsize: %u\n", ed->ehdr.e_phentsize); 1318 PRT("\te_phnum: %u\n", ed->ehdr.e_phnum); 1319 PRT("\te_shentsize: %u\n", ed->ehdr.e_shentsize); --- 4 unchanged lines hidden (view full) --- 1324 1325/* 1326 * Dump the ELF Program Header Table. 1327 */ 1328static void 1329elf_print_phdr(struct elfdump *ed) 1330{ 1331 GElf_Phdr ph; |
1332 size_t phnum, i; 1333 int header; |
1334 1335 if (elf_getphnum(ed->elf, &phnum) == 0) { 1336 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 1337 return; 1338 } 1339 header = 0; |
1340 for (i = 0; i < phnum; i++) { |
1341 if (gelf_getphdr(ed->elf, i, &ph) != &ph) { 1342 warnx("elf_getphdr failed: %s", elf_errmsg(-1)); 1343 continue; 1344 } 1345 if (!STAILQ_EMPTY(&ed->snl) && |
1346 find_name(ed, elf_phdr_type_str(ph.p_type)) == NULL) |
1347 continue; 1348 if (ed->flags & SOLARIS_FMT) { |
1349 PRT("\nProgram Header[%zu]:\n", i); |
1350 PRT(" p_vaddr: %#-14jx", (uintmax_t)ph.p_vaddr); |
1351 PRT(" p_flags: [ %s ]\n", 1352 p_flags[ph.p_flags & 0x7]); |
1353 PRT(" p_paddr: %#-14jx", (uintmax_t)ph.p_paddr); |
1354 PRT(" p_type: [ %s ]\n", 1355 elf_phdr_type_str(ph.p_type)); |
1356 PRT(" p_filesz: %#-14jx", 1357 (uintmax_t)ph.p_filesz); 1358 PRT(" p_memsz: %#jx\n", (uintmax_t)ph.p_memsz); 1359 PRT(" p_offset: %#-14jx", 1360 (uintmax_t)ph.p_offset); 1361 PRT(" p_align: %#jx\n", (uintmax_t)ph.p_align); 1362 } else { 1363 if (!header) { 1364 PRT("\nprogram header:\n"); 1365 header = 1; 1366 } 1367 PRT("\n"); |
1368 PRT("entry: %zu\n", i); 1369 PRT("\tp_type: %s\n", elf_phdr_type_str(ph.p_type)); |
1370 PRT("\tp_offset: %ju\n", (uintmax_t)ph.p_offset); 1371 PRT("\tp_vaddr: %#jx\n", (uintmax_t)ph.p_vaddr); 1372 PRT("\tp_paddr: %#jx\n", (uintmax_t)ph.p_paddr); 1373 PRT("\tp_filesz: %ju\n", (uintmax_t)ph.p_filesz); 1374 PRT("\tp_memsz: %ju\n", (uintmax_t)ph.p_memsz); |
1375 PRT("\tp_flags: %s\n", p_flags[ph.p_flags & 0x7]); |
1376 PRT("\tp_align: %ju\n", (uintmax_t)ph.p_align); 1377 } 1378 } 1379} 1380 1381/* 1382 * Dump the ELF Section Header Table. 1383 */ 1384static void 1385elf_print_shdr(struct elfdump *ed) 1386{ 1387 struct section *s; |
1388 size_t i; |
1389 1390 if (!STAILQ_EMPTY(&ed->snl)) 1391 return; 1392 1393 if ((ed->flags & SOLARIS_FMT) == 0) 1394 PRT("\nsection header:\n"); |
1395 for (i = 0; i < ed->shnum; i++) { |
1396 s = &ed->sl[i]; 1397 if (ed->flags & SOLARIS_FMT) { 1398 if (i == 0) 1399 continue; |
1400 PRT("\nSection Header[%zu]:", i); |
1401 PRT(" sh_name: %s\n", s->name); 1402 PRT(" sh_addr: %#-14jx", (uintmax_t)s->addr); 1403 if (s->flags != 0) 1404 PRT(" sh_flags: [ %s ]\n", sh_flags(s->flags)); 1405 else 1406 PRT(" sh_flags: 0\n"); 1407 PRT(" sh_size: %#-14jx", (uintmax_t)s->sz); 1408 PRT(" sh_type: [ %s ]\n", --- 43 unchanged lines hidden (view full) --- 1452 * Retrieve the content of the corresponding SHT_SUNW_versym section for 1453 * a symbol table section. 1454 */ 1455static void 1456get_versym(struct elfdump *ed, int i, uint16_t **vs, int *nvs) 1457{ 1458 struct section *s; 1459 Elf_Data *data; |
1460 size_t j; 1461 int elferr; |
1462 1463 s = NULL; |
1464 for (j = 0; j < ed->shnum; j++) { |
1465 s = &ed->sl[j]; 1466 if (s->type == SHT_SUNW_versym && s->link == (uint32_t)i) 1467 break; 1468 } |
1469 if (j >= ed->shnum) { |
1470 *vs = NULL; 1471 return; 1472 } 1473 (void) elf_errno(); 1474 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1475 elferr = elf_errno(); 1476 if (elferr != 0) 1477 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); --- 88 unchanged lines hidden (view full) --- 1566} 1567 1568/* 1569 * Dump the symbol tables. (.dynsym and .symtab) 1570 */ 1571static void 1572elf_print_symtabs(struct elfdump *ed) 1573{ |
1574 size_t i; |
1575 |
1576 for (i = 0; i < ed->shnum; i++) |
1577 if ((ed->sl[i].type == SHT_SYMTAB || 1578 ed->sl[i].type == SHT_DYNSYM) && 1579 (STAILQ_EMPTY(&ed->snl) || find_name(ed, ed->sl[i].name))) 1580 elf_print_symtab(ed, i); 1581} 1582 1583/* 1584 * Dump the content of .dynamic section. --- 46 unchanged lines hidden (view full) --- 1631 PRT("\n"); 1632 PRT("entry: %d\n", i); 1633 PRT("\td_tag: %s\n", d_tags(dyn.d_tag)); 1634 } 1635 switch(dyn.d_tag) { 1636 case DT_NEEDED: 1637 case DT_SONAME: 1638 case DT_RPATH: |
1639 case DT_RUNPATH: |
1640 if ((name = elf_strptr(ed->elf, s->link, 1641 dyn.d_un.d_val)) == NULL) 1642 name = ""; 1643 if (ed->flags & SOLARIS_FMT) 1644 PRT("%#-16jx %s\n", (uintmax_t)dyn.d_un.d_val, 1645 name); 1646 else 1647 PRT("\td_val: %s\n", name); --- 50 unchanged lines hidden (view full) --- 1698 * Dump a .rel/.rela section entry. 1699 */ 1700static void 1701elf_print_rel_entry(struct elfdump *ed, struct section *s, int j, 1702 struct rel_entry *r) 1703{ 1704 1705 if (ed->flags & SOLARIS_FMT) { |
1706 PRT(" %-23s ", elftc_reloc_type_str(ed->ehdr.e_machine, |
1707 GELF_R_TYPE(r->u_r.rel.r_info))); 1708 PRT("%#12jx ", (uintmax_t)r->u_r.rel.r_offset); 1709 if (r->type == SHT_RELA) 1710 PRT("%10jd ", (intmax_t)r->u_r.rela.r_addend); 1711 else 1712 PRT(" "); 1713 PRT("%-14s ", s->name); 1714 PRT("%s\n", r->symn); --- 77 unchanged lines hidden (view full) --- 1792/* 1793 * Dump relocation sections. 1794 */ 1795static void 1796elf_print_reloc(struct elfdump *ed) 1797{ 1798 struct section *s; 1799 Elf_Data *data; |
1800 size_t i; 1801 int elferr; |
1802 |
1803 for (i = 0; i < ed->shnum; i++) { |
1804 s = &ed->sl[i]; 1805 if ((s->type == SHT_REL || s->type == SHT_RELA) && 1806 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) { 1807 (void) elf_errno(); 1808 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1809 elferr = elf_errno(); 1810 if (elferr != 0) 1811 warnx("elf_getdata failed: %s", --- 11 unchanged lines hidden (view full) --- 1823/* 1824 * Dump the content of PT_INTERP segment. 1825 */ 1826static void 1827elf_print_interp(struct elfdump *ed) 1828{ 1829 const char *s; 1830 GElf_Phdr phdr; |
1831 size_t filesize, i, phnum; |
1832 1833 if (!STAILQ_EMPTY(&ed->snl) && find_name(ed, "PT_INTERP") == NULL) 1834 return; 1835 |
1836 if ((s = elf_rawfile(ed->elf, &filesize)) == NULL) { |
1837 warnx("elf_rawfile failed: %s", elf_errmsg(-1)); 1838 return; 1839 } 1840 if (!elf_getphnum(ed->elf, &phnum)) { 1841 warnx("elf_getphnum failed: %s", elf_errmsg(-1)); 1842 return; 1843 } |
1844 for (i = 0; i < phnum; i++) { |
1845 if (gelf_getphdr(ed->elf, i, &phdr) != &phdr) { 1846 warnx("elf_getphdr failed: %s", elf_errmsg(-1)); 1847 continue; 1848 } 1849 if (phdr.p_type == PT_INTERP) { |
1850 if (phdr.p_offset >= filesize) { 1851 warnx("invalid phdr offset"); 1852 continue; 1853 } |
1854 PRT("\ninterp:\n"); 1855 PRT("\t%s\n", s + phdr.p_offset); 1856 } 1857 } 1858} 1859 1860/* |
1861 * Search the relocation sections for entries referring to the .got section. |
1862 */ 1863static void 1864find_gotrel(struct elfdump *ed, struct section *gs, struct rel_entry *got) 1865{ 1866 struct section *s; 1867 struct rel_entry r; 1868 Elf_Data *data; |
1869 size_t i; 1870 int elferr, j, k, len; |
1871 |
1872 for(i = 0; i < ed->shnum; i++) { |
1873 s = &ed->sl[i]; 1874 if (s->type != SHT_REL && s->type != SHT_RELA) 1875 continue; 1876 (void) elf_errno(); 1877 if ((data = elf_getdata(s->scn, NULL)) == NULL) { 1878 elferr = elf_errno(); 1879 if (elferr != 0) 1880 warnx("elf_getdata failed: %s", --- 60 unchanged lines hidden (view full) --- 1941 elferr = elf_errno(); 1942 if (elferr != 0) 1943 warnx("elf_getdata failed: %s", elf_errmsg(elferr)); 1944 return; 1945 } 1946 1947 /* 1948 * GOT section has section type SHT_PROGBITS, thus libelf treats it as |
1949 * byte stream and will not perform any translation on it. As a result, |
1950 * an exlicit call to gelf_xlatetom is needed here. Depends on arch, 1951 * GOT section should be translated to either WORD or XWORD. 1952 */ 1953 if (ed->ec == ELFCLASS32) 1954 data->d_type = ELF_T_WORD; 1955 else 1956 data->d_type = ELF_T_XWORD; 1957 memcpy(&dst, data, sizeof(Elf_Data)); --- 26 unchanged lines hidden (view full) --- 1984 (uintmax_t) (s->addr + i * s->entsize)); 1985 PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i)); 1986 } else { 1987 PRT("%-16.16jx ", 1988 (uintmax_t) (s->addr + i * s->entsize)); 1989 PRT("%-16.16jx ", 1990 (uintmax_t) *((uint64_t *)dst.d_buf + i)); 1991 } |
1992 PRT("%-18s ", elftc_reloc_type_str(ed->ehdr.e_machine, |
1993 GELF_R_TYPE(got[i].u_r.rel.r_info))); 1994 if (ed->ec == ELFCLASS32) 1995 PRT("%-8.8jd ", 1996 (intmax_t)got[i].u_r.rela.r_addend); 1997 else 1998 PRT("%-12.12jd ", 1999 (intmax_t)got[i].u_r.rela.r_addend); 2000 if (got[i].symn == NULL) --- 15 unchanged lines hidden (view full) --- 2016 2017/* 2018 * Dump the content of Global Offset Table section. 2019 */ 2020static void 2021elf_print_got(struct elfdump *ed) 2022{ 2023 struct section *s; |
2024 size_t i; |
2025 2026 if (!STAILQ_EMPTY(&ed->snl)) 2027 return; 2028 2029 s = NULL; |
2030 for (i = 0; i < ed->shnum; i++) { |
2031 s = &ed->sl[i]; 2032 if (s->name && !strncmp(s->name, ".got", 4) && 2033 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) 2034 elf_print_got_section(ed, s); 2035 } 2036} 2037 2038/* --- 5 unchanged lines hidden (view full) --- 2044 struct section *s; 2045 Elf_Data *data; 2046 Elf_Note *en; 2047 uint32_t namesz; 2048 uint32_t descsz; 2049 uint32_t desc; 2050 size_t count; 2051 int elferr, i; |
2052 uint8_t *src; 2053 char idx[10]; |
2054 2055 s = NULL; 2056 for (i = 0; (size_t)i < ed->shnum; i++) { 2057 s = &ed->sl[i]; 2058 if (s->type == SHT_NOTE && s->name && 2059 !strcmp(s->name, ".note.ABI-tag") && 2060 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) 2061 break; --- 14 unchanged lines hidden (view full) --- 2076 src = data->d_buf; 2077 count = data->d_size; 2078 while (count > sizeof(Elf_Note)) { 2079 en = (Elf_Note *) (uintptr_t) src; 2080 namesz = en->n_namesz; 2081 descsz = en->n_descsz; 2082 src += sizeof(Elf_Note); 2083 count -= sizeof(Elf_Note); |
2084 if (roundup2(namesz, 4) + roundup2(descsz, 4) > count) { 2085 warnx("truncated note section"); 2086 return; 2087 } |
2088 if (ed->flags & SOLARIS_FMT) { 2089 PRT("\n type %#x\n", en->n_type); 2090 PRT(" namesz %#x:\n", en->n_namesz); 2091 PRT("%s\n", src); 2092 } else 2093 PRT("\t%s ", src); 2094 src += roundup2(namesz, 4); 2095 count -= roundup2(namesz, 4); --- 34 unchanged lines hidden (view full) --- 2130static void 2131elf_print_svr4_hash(struct elfdump *ed, struct section *s) 2132{ 2133 Elf_Data *data; 2134 uint32_t *buf; 2135 uint32_t *bucket, *chain; 2136 uint32_t nbucket, nchain; 2137 uint32_t *bl, *c, maxl, total; |
2138 uint32_t i, j; 2139 int first, elferr; |
2140 char idx[10]; 2141 2142 if (ed->flags & SOLARIS_FMT) 2143 PRT("\nHash Section: %s\n", s->name); 2144 else 2145 PRT("\nhash table (%s):\n", s->name); 2146 (void) elf_errno(); 2147 if ((data = elf_getdata(s->scn, NULL)) == NULL) { --- 9 unchanged lines hidden (view full) --- 2157 } 2158 buf = data->d_buf; 2159 nbucket = buf[0]; 2160 nchain = buf[1]; 2161 if (nbucket <= 0 || nchain <= 0) { 2162 warnx("Malformed .hash section"); 2163 return; 2164 } |
2165 if (data->d_size != 2166 ((uint64_t)nbucket + (uint64_t)nchain + 2) * sizeof(uint32_t)) { |
2167 warnx("Malformed .hash section"); 2168 return; 2169 } 2170 bucket = &buf[2]; 2171 chain = &buf[2 + nbucket]; 2172 2173 if (ed->flags & SOLARIS_FMT) { 2174 maxl = 0; 2175 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 2176 err(EXIT_FAILURE, "calloc failed"); |
2177 for (i = 0; i < nbucket; i++) 2178 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) |
2179 if (++bl[i] > maxl) 2180 maxl = bl[i]; 2181 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 2182 err(EXIT_FAILURE, "calloc failed"); |
2183 for (i = 0; i < nbucket; i++) |
2184 c[bl[i]]++; 2185 PRT(" bucket symndx name\n"); |
2186 for (i = 0; i < nbucket; i++) { |
2187 first = 1; |
2188 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) { |
2189 if (first) { 2190 PRT("%10d ", i); 2191 first = 0; 2192 } else 2193 PRT(" "); 2194 snprintf(idx, sizeof(idx), "[%d]", j); 2195 PRT("%-10s ", idx); 2196 PRT("%s\n", get_symbol_name(ed, s->link, j)); 2197 } 2198 } 2199 PRT("\n"); 2200 total = 0; |
2201 for (i = 0; i <= maxl; i++) { |
2202 total += c[i] * i; 2203 PRT("%10u buckets contain %8d symbols\n", c[i], i); 2204 } 2205 PRT("%10u buckets %8u symbols (globals)\n", nbucket, 2206 total); 2207 } else { 2208 PRT("\nnbucket: %u\n", nbucket); 2209 PRT("nchain: %u\n\n", nchain); |
2210 for (i = 0; i < nbucket; i++) |
2211 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]); |
2212 for (i = 0; i < nchain; i++) |
2213 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]); 2214 } 2215} 2216 2217/* 2218 * Dump a 64bit hash table. 2219 */ 2220static void 2221elf_print_svr4_hash64(struct elfdump *ed, struct section *s) 2222{ 2223 Elf_Data *data, dst; 2224 uint64_t *buf; 2225 uint64_t *bucket, *chain; 2226 uint64_t nbucket, nchain; 2227 uint64_t *bl, *c, maxl, total; |
2228 uint64_t i, j; 2229 int elferr, first; |
2230 char idx[10]; 2231 2232 if (ed->flags & SOLARIS_FMT) 2233 PRT("\nHash Section: %s\n", s->name); 2234 else 2235 PRT("\nhash table (%s):\n", s->name); 2236 2237 /* --- 33 unchanged lines hidden (view full) --- 2271 } 2272 bucket = &buf[2]; 2273 chain = &buf[2 + nbucket]; 2274 2275 if (ed->flags & SOLARIS_FMT) { 2276 maxl = 0; 2277 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 2278 err(EXIT_FAILURE, "calloc failed"); |
2279 for (i = 0; i < nbucket; i++) 2280 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) |
2281 if (++bl[i] > maxl) 2282 maxl = bl[i]; 2283 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 2284 err(EXIT_FAILURE, "calloc failed"); |
2285 for (i = 0; i < nbucket; i++) |
2286 c[bl[i]]++; 2287 PRT(" bucket symndx name\n"); |
2288 for (i = 0; i < nbucket; i++) { |
2289 first = 1; |
2290 for (j = bucket[i]; j > 0 && j < nchain; j = chain[j]) { |
2291 if (first) { |
2292 PRT("%10zu ", i); |
2293 first = 0; 2294 } else 2295 PRT(" "); |
2296 snprintf(idx, sizeof(idx), "[%zu]", (size_t)j); |
2297 PRT("%-10s ", idx); 2298 PRT("%s\n", get_symbol_name(ed, s->link, j)); 2299 } 2300 } 2301 PRT("\n"); 2302 total = 0; |
2303 for (i = 0; i <= maxl; i++) { |
2304 total += c[i] * i; |
2305 PRT("%10ju buckets contain %8zu symbols\n", |
2306 (uintmax_t)c[i], i); 2307 } 2308 PRT("%10ju buckets %8ju symbols (globals)\n", 2309 (uintmax_t)nbucket, (uintmax_t)total); 2310 } else { 2311 PRT("\nnbucket: %ju\n", (uintmax_t)nbucket); 2312 PRT("nchain: %ju\n\n", (uintmax_t)nchain); |
2313 for (i = 0; i < nbucket; i++) 2314 PRT("bucket[%zu]:\n\t%ju\n\n", i, (uintmax_t)bucket[i]); 2315 for (i = 0; i < nchain; i++) 2316 PRT("chain[%zu]:\n\t%ju\n\n", i, (uintmax_t)chain[i]); |
2317 } 2318 2319} 2320 2321/* 2322 * Dump a GNU hash table. 2323 */ 2324static void 2325elf_print_gnu_hash(struct elfdump *ed, struct section *s) 2326{ 2327 struct section *ds; 2328 Elf_Data *data; 2329 uint32_t *buf; 2330 uint32_t *bucket, *chain; 2331 uint32_t nbucket, nchain, symndx, maskwords, shift2; 2332 uint32_t *bl, *c, maxl, total; |
2333 uint32_t i, j; 2334 int first, elferr, dynsymcount; |
2335 char idx[10]; 2336 2337 if (ed->flags & SOLARIS_FMT) 2338 PRT("\nGNU Hash Section: %s\n", s->name); 2339 else 2340 PRT("\ngnu hash table (%s):\n", s->name); 2341 (void) elf_errno(); 2342 if ((data = elf_getdata(s->scn, NULL)) == NULL) { --- 8 unchanged lines hidden (view full) --- 2351 return; 2352 } 2353 buf = data->d_buf; 2354 nbucket = buf[0]; 2355 symndx = buf[1]; 2356 maskwords = buf[2]; 2357 shift2 = buf[3]; 2358 buf += 4; |
2359 if (s->link >= ed->shnum) { 2360 warnx("Malformed .gnu.hash section"); 2361 return; 2362 } |
2363 ds = &ed->sl[s->link]; 2364 if (!get_ent_count(ds, &dynsymcount)) 2365 return; |
2366 if (symndx >= (uint32_t)dynsymcount) { 2367 warnx("Malformed .gnu.hash section"); 2368 return; 2369 } |
2370 nchain = dynsymcount - symndx; 2371 if (data->d_size != 4 * sizeof(uint32_t) + maskwords * 2372 (ed->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) + |
2373 ((uint64_t)nbucket + (uint64_t)nchain) * sizeof(uint32_t)) { |
2374 warnx("Malformed .gnu.hash section"); 2375 return; 2376 } 2377 bucket = buf + (ed->ec == ELFCLASS32 ? maskwords : maskwords * 2); 2378 chain = bucket + nbucket; 2379 2380 if (ed->flags & SOLARIS_FMT) { 2381 maxl = 0; 2382 if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) 2383 err(EXIT_FAILURE, "calloc failed"); |
2384 for (i = 0; i < nbucket; i++) 2385 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) { |
2386 if (++bl[i] > maxl) 2387 maxl = bl[i]; 2388 if (chain[j - symndx] & 1) 2389 break; 2390 } 2391 if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) 2392 err(EXIT_FAILURE, "calloc failed"); |
2393 for (i = 0; i < nbucket; i++) |
2394 c[bl[i]]++; 2395 PRT(" bucket symndx name\n"); |
2396 for (i = 0; i < nbucket; i++) { |
2397 first = 1; |
2398 for (j = bucket[i]; j > 0 && j - symndx < nchain; j++) { |
2399 if (first) { 2400 PRT("%10d ", i); 2401 first = 0; 2402 } else 2403 PRT(" "); 2404 snprintf(idx, sizeof(idx), "[%d]", j ); 2405 PRT("%-10s ", idx); 2406 PRT("%s\n", get_symbol_name(ed, s->link, j)); 2407 if (chain[j - symndx] & 1) 2408 break; 2409 } 2410 } 2411 PRT("\n"); 2412 total = 0; |
2413 for (i = 0; i <= maxl; i++) { |
2414 total += c[i] * i; 2415 PRT("%10u buckets contain %8d symbols\n", c[i], i); 2416 } 2417 PRT("%10u buckets %8u symbols (globals)\n", nbucket, 2418 total); 2419 } else { 2420 PRT("\nnbucket: %u\n", nbucket); 2421 PRT("symndx: %u\n", symndx); 2422 PRT("maskwords: %u\n", maskwords); 2423 PRT("shift2: %u\n", shift2); 2424 PRT("nchain: %u\n\n", nchain); |
2425 for (i = 0; i < nbucket; i++) |
2426 PRT("bucket[%d]:\n\t%u\n\n", i, bucket[i]); |
2427 for (i = 0; i < nchain; i++) |
2428 PRT("chain[%d]:\n\t%u\n\n", i, chain[i]); 2429 } 2430} 2431 2432/* 2433 * Dump hash tables. 2434 */ 2435static void 2436elf_print_hash(struct elfdump *ed) 2437{ 2438 struct section *s; |
2439 size_t i; |
2440 |
2441 for (i = 0; i < ed->shnum; i++) { |
2442 s = &ed->sl[i]; 2443 if ((s->type == SHT_HASH || s->type == SHT_GNU_HASH) && 2444 (STAILQ_EMPTY(&ed->snl) || find_name(ed, s->name))) { 2445 if (s->type == SHT_GNU_HASH) 2446 elf_print_gnu_hash(ed, s); 2447 else if (ed->ehdr.e_machine == EM_ALPHA && 2448 s->entsize == 8) 2449 elf_print_svr4_hash64(ed, s); --- 166 unchanged lines hidden (view full) --- 2616 2617/* 2618 * Dump the symbol-versioning sections. 2619 */ 2620static void 2621elf_print_symver(struct elfdump *ed) 2622{ 2623 struct section *s; |
2624 size_t i; |
2625 |
2626 for (i = 0; i < ed->shnum; i++) { |
2627 s = &ed->sl[i]; 2628 if (!STAILQ_EMPTY(&ed->snl) && !find_name(ed, s->name)) 2629 continue; 2630 if (s->type == SHT_SUNW_verdef) 2631 elf_print_verdef(ed, s); 2632 if (s->type == SHT_SUNW_verneed) 2633 elf_print_verneed(ed, s); 2634 } --- 44 unchanged lines hidden --- |