readelf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* readelf.c - display information about ELF files.
  2. *
  3. * Copyright 2019 The Android Open Source Project
  4. *
  5. * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/nm.html
  6. USE_READELF(NEWTOY(readelf, "<1(dyn-syms)adehlnp:SsWx:", TOYFLAG_USR|TOYFLAG_BIN))
  7. config READELF
  8. bool "readelf"
  9. default y
  10. help
  11. usage: readelf [-adehlnSs] [-p SECTION] [-x SECTION] [file...]
  12. Displays information about ELF files.
  13. -a Equivalent to -dhlnSs
  14. -d Show dynamic section
  15. -e Headers (equivalent to -hlS)
  16. -h Show ELF header
  17. -l Show program headers
  18. -n Show notes
  19. -p S Dump strings found in named/numbered section
  20. -S Show section headers
  21. -s Show symbol tables (.dynsym and .symtab)
  22. -x S Hex dump of named/numbered section
  23. --dyn-syms Show just .dynsym symbol table
  24. */
  25. #define FOR_readelf
  26. #include "toys.h"
  27. GLOBALS(
  28. char *x, *p;
  29. char *elf, *shstrtab, *f;
  30. unsigned long long shoff, phoff, size, shstrtabsz;
  31. int bits, endian, shnum, shentsize, phentsize;
  32. )
  33. // Section header.
  34. struct sh {
  35. unsigned type, link, info;
  36. unsigned long long flags, addr, offset, size, addralign, entsize;
  37. char *name;
  38. };
  39. // Program header.
  40. struct ph {
  41. unsigned type, flags;
  42. unsigned long long offset, vaddr, paddr, filesz, memsz, align;
  43. };
  44. static long long elf_get(char **p, int len)
  45. {
  46. long long result;
  47. if (*p+len-TT.elf>TT.size)
  48. perror_exit("Access off end: %td[%d] of %lld\n", *p-TT.elf, len, TT.size);
  49. result = ((TT.endian == 2) ? peek_be : peek_le)(*p, len);
  50. *p += len;
  51. return result;
  52. }
  53. static unsigned long long elf_long(char **p)
  54. {
  55. return elf_get(p, 4*(TT.bits+1));
  56. }
  57. static unsigned elf_int(char **p)
  58. {
  59. return elf_get(p, 4);
  60. }
  61. static unsigned short elf_short(char **p)
  62. {
  63. return elf_get(p, 2);
  64. }
  65. static int get_sh(unsigned i, struct sh *s)
  66. {
  67. char *shdr = TT.elf+TT.shoff+i*TT.shentsize;
  68. unsigned name_offset;
  69. if (i >= TT.shnum || shdr > TT.elf+TT.size-TT.shentsize) {
  70. printf("No shdr %d\n", i);
  71. return 0;
  72. }
  73. name_offset = elf_int(&shdr);
  74. s->type = elf_int(&shdr);
  75. s->flags = elf_long(&shdr);
  76. s->addr = elf_long(&shdr);
  77. s->offset = elf_long(&shdr);
  78. s->size = elf_long(&shdr);
  79. s->link = elf_int(&shdr);
  80. s->info = elf_int(&shdr);
  81. s->addralign = elf_long(&shdr);
  82. s->entsize = elf_long(&shdr);
  83. if (s->type != 8) {
  84. if (s->offset>TT.size || s->size>TT.size || s->offset>TT.size-s->size) {
  85. printf("Bad offset/size %llu/%llu for sh %d\n", s->offset, s->size, i);
  86. return 0;
  87. }
  88. }
  89. if (!TT.shstrtab) s->name = "?";
  90. else {
  91. s->name = TT.shstrtab + name_offset;
  92. if (name_offset > TT.shstrtabsz || s->name >= TT.elf+TT.size) {
  93. printf("Bad name for sh %d\n", i);
  94. return 0;
  95. }
  96. }
  97. return 1;
  98. }
  99. static int find_section(char *spec, struct sh *s)
  100. {
  101. char *end;
  102. unsigned i;
  103. // Valid section number?
  104. i = estrtol(spec, &end, 0);
  105. if (!errno && !*end && i<TT.shnum) return get_sh(i, s);
  106. // Search the section names.
  107. for (i=0; i<TT.shnum; i++)
  108. if (get_sh(i, s) && !strcmp(s->name, spec)) return 1;
  109. error_msg("%s: no section '%s", TT.f, spec);
  110. return 0;
  111. }
  112. static int get_ph(int i, struct ph *ph)
  113. {
  114. char *phdr = TT.elf+TT.phoff+i*TT.phentsize;
  115. if (phdr > TT.elf+TT.size-TT.phentsize) {
  116. printf("Bad phdr %d\n", i);
  117. return 0;
  118. }
  119. // Elf64_Phdr reordered fields.
  120. ph->type = elf_int(&phdr);
  121. if (TT.bits) {
  122. ph->flags = elf_int(&phdr);
  123. ph->offset = elf_long(&phdr);
  124. ph->vaddr = elf_long(&phdr);
  125. ph->paddr = elf_long(&phdr);
  126. ph->filesz = elf_long(&phdr);
  127. ph->memsz = elf_long(&phdr);
  128. ph->align = elf_long(&phdr);
  129. } else {
  130. ph->offset = elf_int(&phdr);
  131. ph->vaddr = elf_int(&phdr);
  132. ph->paddr = elf_int(&phdr);
  133. ph->filesz = elf_int(&phdr);
  134. ph->memsz = elf_int(&phdr);
  135. ph->flags = elf_int(&phdr);
  136. ph->align = elf_int(&phdr);
  137. }
  138. if (ph->offset >= TT.size-ph->filesz) {
  139. printf("phdr %d has bad offset/size %llu/%llu", i, ph->offset, ph->filesz);
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. #define MAP(...) __VA_ARGS__
  145. #define DECODER(name, values) \
  146. static char *name(int type) { \
  147. static char unknown[20]; \
  148. struct {int v; char *s;} a[] = values; \
  149. int i; \
  150. \
  151. for (i=0; i<ARRAY_LEN(a); i++) if (type==a[i].v) return a[i].s; \
  152. sprintf(unknown, "0x%x", type); \
  153. return unknown; \
  154. }
  155. DECODER(dt_type, MAP({{0,"x(NULL)"},{1,"N(NEEDED)"},{2,"b(PLTRELSZ)"},
  156. {3,"x(PLTGOT)"},{4,"x(HASH)"},{5,"x(STRTAB)"},{6,"x(SYMTAB)"},{7,"x(RELA)"},
  157. {8,"b(RELASZ)"},{9,"b(RELAENT)"},{10,"b(STRSZ)"},{11,"b(SYMENT)"},
  158. {12,"x(INIT)"},{13,"x(FINI)"},{14,"S(SONAME)"},{15,"R(RPATH)"},
  159. {16,"x(SYMBOLIC)"},{17,"x(REL)"},{18,"b(RELSZ)"},{19,"b(RELENT)"},
  160. {20,"P(PLTREL)"},{21,"x(DEBUG)"},{22,"x(TEXTREL)"},{23,"x(JMPREL)"},
  161. {24,"d(BIND_NOW)"},{25,"x(INIT_ARRAY)"},{26,"x(FINI_ARRAY)"},
  162. {27,"b(INIT_ARRAYSZ)"},{28,"b(FINI_ARRAYSZ)"},{29,"R(RUNPATH)"},
  163. {30,"f(FLAGS)"},{32,"x(PREINIT_ARRAY)"},{33,"x(PREINIT_ARRAYSZ)"},
  164. {35,"b(RELRSZ)"},{36,"x(RELR)"},{37,"b(RELRENT)"},
  165. {0x6000000f,"x(ANDROID_REL)"},{0x60000010,"b(ANDROID_RELSZ)"},
  166. {0x60000011,"x(ANDROID_RELA)"},{0x60000012,"b(ANDROID_RELASZ)"},
  167. {0x6fffe000,"x(ANDROID_RELR)"},{0x6fffe001,"b(ANDROID_RELRSZ)"},
  168. {0x6fffe003,"x(ANDROID_RELRENT)"},{0x6ffffef5,"x(GNU_HASH)"},
  169. {0x6ffffef6,"x(TLSDESC_PLT)"},{0x6ffffef7,"x(TLSDESC_GOT)"},
  170. {0x6ffffff0,"x(VERSYM)"},{0x6ffffff9,"d(RELACOUNT)"},
  171. {0x6ffffffa,"d(RELCOUNT)"},{0x6ffffffb,"F(FLAGS_1)"},
  172. {0x6ffffffc," (VERDEF)"},{0x6ffffffd,"d(VERDEFNUM)"},
  173. {0x6ffffffe,"x(VERNEED)"},{0x6fffffff,"d(VERNEEDNUM)"}}))
  174. DECODER(et_type, MAP({{0,"NONE (None)"},{1,"REL (Relocatable file)"},
  175. {2,"EXEC (Executable file)"},{3,"DYN (Shared object file)"},
  176. {4,"CORE (Core file)"}}))
  177. DECODER(nt_type_core, MAP({{1,"NT_PRSTATUS"},{2,"NT_FPREGSET"},
  178. {3,"NT_PRPSINFO"},{5,"NT_PLATFORM"},{6,"NT_AUXV"},
  179. {0x46494c45,"NT_FILE"},{0x53494749,"NT_SIGINFO"}}))
  180. DECODER(nt_type_linux, MAP({{0x200,"NT_386_TLS"},{0x202, "NT_X86_XSTATE"},
  181. {0x400,"NT_ARM_VFP"},{0x401,"NT_ARM_TLS"},{0x405,"NT_ARM_SVE"}}))
  182. DECODER(os_abi, MAP({{0,"UNIX - System V"}}))
  183. DECODER(ph_type, MAP({{0,"NULL"},{1,"LOAD"},{2,"DYNAMIC"},{3,"INTERP"},
  184. {4,"NOTE"},{5,"SHLIB"},{6,"PHDR"},{7,"TLS"},{0x6474e550,"GNU_EH_FRAME"},
  185. {0x6474e551,"GNU_STACK"},{0x6474e552,"GNU_RELRO"},{0x70000001,"EXIDX"}}))
  186. DECODER(sh_type, MAP({{0,"NULL"},{1,"PROGBITS"},{2,"SYMTAB"},{3,"STRTAB"},
  187. {4,"RELA"},{5,"HASH"},{6,"DYNAMIC"},{7,"NOTE"},{8,"NOBITS"},{9,"REL"},
  188. {10,"SHLIB"},{11,"DYNSYM"},{14,"INIT_ARRAY"},{15,"FINI_ARRAY"},
  189. {16,"PREINIT_ARRAY"},{17,"GROUP"},{18,"SYMTAB_SHNDX"},{19,"RELR"},
  190. {0x60000001,"ANDROID_REL"},{0x60000002,"ANDROID_RELA"},
  191. {0x6fffff00,"ANDROID_RELR"},{0x6ffffff6,"GNU_HASH"},
  192. {0x6ffffffd,"VERDEF"},{0x6ffffffe,"VERNEED"},
  193. {0x6fffffff,"VERSYM"},{0x70000001,"ARM_EXIDX"},
  194. {0x70000003,"ARM_ATTRIBUTES"}}))
  195. DECODER(stb_type, MAP({{0,"LOCAL"},{1,"GLOBAL"},{2,"WEAK"}}))
  196. DECODER(stt_type, MAP({{0,"NOTYPE"},{1,"OBJECT"},{2,"FUNC"},{3,"SECTION"},
  197. {4,"FILE"},{5,"COMMON"},{6,"TLS"},{10,"GNU_IFUNC"}}))
  198. DECODER(stv_type, MAP({{0,"DEFAULT"},{1,"INTERNAL"},{2,"HIDDEN"},
  199. {3,"PROTECTED"}}))
  200. static void show_symbols(struct sh *table, struct sh *strtab)
  201. {
  202. char *symtab = TT.elf+table->offset, *ndx;
  203. int numsym = table->size/(TT.bits ? 24 : 16), i;
  204. if (!numsym) return;
  205. xputc('\n');
  206. printf("Symbol table '%s' contains %d entries:\n"
  207. " Num: %*s Size Type Bind Vis Ndx Name\n",
  208. table->name, numsym, 5+8*TT.bits, "Value");
  209. for (i=0; i<numsym; i++) {
  210. unsigned st_name = elf_int(&symtab), st_value, st_shndx, st_info, st_other;
  211. unsigned long st_size;
  212. char *name, buf[16];
  213. // The various fields were moved around for 64-bit.
  214. if (TT.bits) {
  215. st_info = *symtab++;
  216. st_other = *symtab++;
  217. st_shndx = elf_short(&symtab);
  218. st_value = elf_long(&symtab);
  219. st_size = elf_long(&symtab);
  220. } else {
  221. st_value = elf_int(&symtab);
  222. st_size = elf_int(&symtab);
  223. st_info = *symtab++;
  224. st_other = *symtab++;
  225. st_shndx = elf_short(&symtab);
  226. }
  227. // TODO: why do we trust name to be null terminated?
  228. name = TT.elf + strtab->offset + st_name;
  229. if (name >= TT.elf+TT.size) name = "???";
  230. if (!st_shndx) ndx = "UND";
  231. else if (st_shndx==0xfff1) ndx = "ABS";
  232. else sprintf(ndx = buf, "%d", st_shndx);
  233. // TODO: look up and show any symbol versions with @ or @@.
  234. printf("%6d: %0*x %5lu %-7s %-6s %-9s%3s %s\n", i, 8*(TT.bits+1),
  235. st_value, st_size, stt_type(st_info & 0xf), stb_type(st_info >> 4),
  236. stv_type(st_other & 3), ndx, name);
  237. }
  238. }
  239. static int notematch(int namesz, char **p, char *expected)
  240. {
  241. if (namesz!=strlen(expected)+1 || strcmp(*p, expected)) return 0;
  242. *p += namesz;
  243. return 1;
  244. }
  245. static void show_notes(unsigned long offset, unsigned long size)
  246. {
  247. char *note = TT.elf + offset;
  248. if (size > TT.size || offset > TT.size-size) {
  249. printf("Bad note bounds %lu/%lu\n", offset, size);
  250. return;
  251. }
  252. printf(" %-20s%11s\tDescription\n", "Owner", "Data size");
  253. while (note < TT.elf+offset+size) {
  254. char *p = note, *desc;
  255. unsigned namesz=elf_int(&p), descsz=elf_int(&p), type=elf_int(&p), j=0;
  256. if (namesz > size || descsz > size)
  257. return error_msg("%s: bad note @%lu", TT.f, offset);
  258. printf(" %-20.*s 0x%08x\t", namesz, p, descsz);
  259. if (notematch(namesz, &p, "GNU")) {
  260. if (type == 1) {
  261. printf("NT_GNU_ABI_TAG\tOS: %s, ABI: %u.%u.%u",
  262. !elf_int(&p)?"Linux":"?", elf_int(&p), elf_int(&p), elf_int(&p)), j=1;
  263. } else if (type == 3) {
  264. // TODO should this set j=1?
  265. printf("NT_GNU_BUILD_ID\t");
  266. for (;j<descsz;j++) printf("%02x", *p++);
  267. } else if (type == 4) {
  268. printf("NT_GNU_GOLD_VERSION\t%.*s", descsz, p), j=1;
  269. } else p -= 4;
  270. } else if (notematch(namesz, &p, "Android")) {
  271. if (type == 1) {
  272. printf("NT_VERSION\tAPI level %u", elf_int(&p)), j=1;
  273. if (descsz>=132) printf(", NDK %.64s (%.64s)", p, p+64);
  274. } else p -= 8;
  275. } else if (notematch(namesz, &p, "CORE")) {
  276. if (*(desc = nt_type_core(type)) != '0') printf("%s", desc), j=1;
  277. // TODO else p -= 5?
  278. } else if (notematch(namesz, &p, "LINUX")) {
  279. if (*(desc = nt_type_linux(type)) != '0') printf("%s", desc), j=1;
  280. // TODO else p -= 6?
  281. }
  282. // If we didn't do custom output above, show a hex dump.
  283. if (!j) {
  284. printf("0x%x\t", type);
  285. for (;j<descsz;j++) printf("%c%02x", j ? ' ' : '\t', *p++/*note[16+j]*/);
  286. }
  287. xputc('\n');
  288. note += 3*4 + ((namesz+3)&~3) + ((descsz+3)&~3);
  289. }
  290. }
  291. static void scan_elf()
  292. {
  293. struct sh dynamic = {}, dynstr = {}, dynsym = {}, shstr = {}, strtab = {},
  294. symtab = {}, s;
  295. struct ph ph;
  296. char *hdr = TT.elf;
  297. int type, machine, version, flags, entry, ehsize, phnum, shstrndx, i, j, w;
  298. if (TT.size < 45 || memcmp(hdr, "\177ELF", 4))
  299. return error_msg("%s: not ELF", TT.f);
  300. TT.bits = hdr[4] - 1;
  301. TT.endian = hdr[5];
  302. if (TT.bits<0 || TT.bits>1 || TT.endian<1 || TT.endian>2 || hdr[6]!=1)
  303. return error_msg("%s: bad ELF", TT.f);
  304. hdr += 16; // EI_NIDENT
  305. type = elf_short(&hdr);
  306. machine = elf_short(&hdr);
  307. version = elf_int(&hdr);
  308. entry = elf_long(&hdr);
  309. TT.phoff = elf_long(&hdr);
  310. TT.shoff = elf_long(&hdr);
  311. flags = elf_int(&hdr);
  312. ehsize = elf_short(&hdr);
  313. TT.phentsize = elf_short(&hdr);
  314. phnum = elf_short(&hdr);
  315. TT.shentsize = elf_short(&hdr);
  316. TT.shnum = elf_short(&hdr);
  317. shstrndx = elf_short(&hdr);
  318. if (toys.optc > 1) printf("\nFile: %s\n", TT.f);
  319. if (FLAG(h)) {
  320. printf("ELF Header:\n");
  321. printf(" Magic: ");
  322. for (i=0; i<16; i++) printf("%02x%c", TT.elf[i], (i==15) ? '\n' : ' ');
  323. printf(" Class: ELF%d\n", TT.bits?64:32);
  324. printf(" Data: 2's complement, %s endian\n",
  325. (TT.endian==2)?"big":"little");
  326. printf(" Version: 1 (current)\n");
  327. printf(" OS/ABI: %s\n", os_abi(TT.elf[7]));
  328. printf(" ABI Version: %d\n", TT.elf[8]);
  329. printf(" Type: %s\n", et_type(type));
  330. printf(" Machine: %s\n", elf_arch_name(machine));
  331. printf(" Version: 0x%x\n", version);
  332. printf(" Entry point address: 0x%x\n", entry);
  333. printf(" Start of program headers: %llu (bytes into file)\n",
  334. TT.phoff);
  335. printf(" Start of section headers: %llu (bytes into file)\n",
  336. TT.shoff);
  337. printf(" Flags: 0x%x\n", flags);
  338. printf(" Size of this header: %d (bytes)\n", ehsize);
  339. printf(" Size of program headers: %d (bytes)\n", TT.phentsize);
  340. printf(" Number of program headers: %d\n", phnum);
  341. printf(" Size of section headers: %d (bytes)\n", TT.shentsize);
  342. printf(" Number of section headers: %d\n", TT.shnum);
  343. printf(" Section header string table index: %d\n", shstrndx);
  344. }
  345. if (TT.phoff > TT.size) return error_msg("%s: bad phoff", TT.f);
  346. if (TT.shoff > TT.size) return error_msg("%s: bad shoff", TT.f);
  347. // Set up the section header string table so we can use section header names.
  348. // Core files have shstrndx == 0.
  349. TT.shstrtab = 0;
  350. TT.shstrtabsz = 0;
  351. if (shstrndx) {
  352. if (!get_sh(shstrndx, &shstr) || shstr.type != 3 /*SHT_STRTAB*/)
  353. return error_msg("%s: bad shstrndx", TT.f);
  354. TT.shstrtab = TT.elf+shstr.offset;
  355. TT.shstrtabsz = shstr.size;
  356. }
  357. w = 8<<TT.bits;
  358. if (FLAG(S)) {
  359. if (!TT.shnum) printf("\nThere are no sections in this file.\n");
  360. else {
  361. if (!FLAG(h))
  362. printf("There are %d section headers, starting at offset %#llx:\n",
  363. TT.shnum, TT.shoff);
  364. printf("\nSection Headers:\n"
  365. " [Nr] %-17s %-15s %-*s %-6s %-6s ES Flg Lk Inf Al\n",
  366. "Name", "Type", w, "Address", "Off", "Size");
  367. }
  368. }
  369. // We need to iterate through the section headers even if we're not
  370. // dumping them, to find specific sections.
  371. for (i=0; i<TT.shnum; i++) {
  372. if (!get_sh(i, &s)) continue;
  373. if (s.type == 2 /*SHT_SYMTAB*/) symtab = s;
  374. else if (s.type == 6 /*SHT_DYNAMIC*/) dynamic = s;
  375. else if (s.type == 11 /*SHT_DYNSYM*/) dynsym = s;
  376. else if (s.type == 3 /*SHT_STRTAB*/) {
  377. if (!strcmp(s.name, ".strtab")) strtab = s;
  378. else if (!strcmp(s.name, ".dynstr")) dynstr = s;
  379. }
  380. if (FLAG(S)) {
  381. char sh_flags[12] = {}, *p = sh_flags;
  382. for (j=0; j<12; j++) if (s.flags&(1<<j)) *p++ = "WAXxMSILOTC"[j];
  383. printf(" [%2d] %-17s %-15s %0*llx %06llx %06llx %02llx %3s %2d %2d %2lld\n",
  384. i, s.name, sh_type(s.type), w, s.addr, s.offset, s.size,
  385. s.entsize, sh_flags, s.link, s.info, s.addralign);
  386. }
  387. }
  388. if (FLAG(S) && TT.shnum)
  389. printf("Key:\n (W)rite, (A)lloc, e(X)ecute, (M)erge, (S)trings, (I)nfo\n"
  390. " (L)ink order, (O)S, (G)roup, (T)LS, (C)ompressed, x=unknown\n");
  391. if (FLAG(l)) {
  392. xputc('\n');
  393. if (!phnum) printf("There are no program headers in this file.\n");
  394. else {
  395. if (!FLAG(h))
  396. printf("Elf file type is %s\nEntry point %#x\n"
  397. "There are %d program headers, starting at offset %lld\n\n",
  398. et_type(type), entry, phnum, TT.phoff);
  399. printf("Program Headers:\n"
  400. " %-14s %-8s %-*s %-*s %-7s %-7s Flg Align\n", "Type",
  401. "Offset", w, "VirtAddr", w, "PhysAddr", "FileSiz", "MemSiz");
  402. for (i = 0; i<phnum; i++) {
  403. if (!get_ph(i, &ph)) continue;
  404. printf(" %-14s 0x%06llx 0x%0*llx 0x%0*llx 0x%05llx 0x%05llx %c%c%c %#llx\n",
  405. ph_type(ph.type), ph.offset, w, ph.vaddr, w, ph.paddr,
  406. ph.filesz, ph.memsz, (ph.flags&4)?'R':' ', (ph.flags&2)?'W':' ',
  407. (ph.flags&1)?'E':' ', ph.align);
  408. if (ph.type == 3 /*PH_INTERP*/ && ph.filesz<TT.size &&
  409. ph.offset<TT.size && ph.filesz - 1 < TT.size - ph.offset) {
  410. // TODO: ph.filesz of 0 prints unlimited length string
  411. printf(" [Requesting program interpreter: %*s]\n",
  412. (int) ph.filesz-1, TT.elf+ph.offset);
  413. }
  414. }
  415. printf("\n Section to Segment mapping:\n Segment Sections...\n");
  416. for (i=0; i<phnum; i++) {
  417. if (!get_ph(i, &ph)) continue;
  418. printf(" %02d ", i);
  419. for (j=0; j<TT.shnum; j++) {
  420. if (!get_sh(j, &s)) continue;
  421. if (!*s.name) continue;
  422. if (s.offset >= ph.offset && s.offset+s.size <= ph.offset+ph.filesz)
  423. printf("%s ", s.name);
  424. }
  425. xputc('\n');
  426. }
  427. }
  428. }
  429. // binutils ld emits a bunch of extra DT_NULL entries, so binutils readelf
  430. // uses two passes here! We just tell the truth, which matches -h.
  431. if (FLAG(d)) {
  432. char *dyn = TT.elf+dynamic.offset, *end = dyn+dynamic.size;
  433. xputc('\n');
  434. if (!dynamic.size) printf("There is no dynamic section in this file.\n");
  435. else if (!dynamic.entsize) printf("Bad dynamic entry size 0!\n");
  436. else {
  437. printf("Dynamic section at offset 0x%llx contains %lld entries:\n"
  438. " %-*s %-20s %s\n", dynamic.offset, dynamic.size/dynamic.entsize,
  439. w+2, "Tag", "Type", "Name/Value");
  440. while (dyn < end) {
  441. unsigned long long tag = elf_long(&dyn), val = elf_long(&dyn);
  442. char *type = dt_type(tag);
  443. printf(" 0x%0*llx %-20s ", w, tag, type+(*type!='0'));
  444. if (*type == 'd') printf("%lld\n", val);
  445. else if (*type == 'b') printf("%lld (bytes)\n", val);
  446. // TODO: trusting this %s to be null terminated
  447. else if (*type == 's') printf("%s\n", TT.elf+dynstr.offset+val);
  448. else if (*type == 'f' || *type == 'F') {
  449. struct bitname { int bit; char *s; }
  450. df_names[] = {{0, "ORIGIN"},{1,"SYMBOLIC"},{2,"TEXTREL"},
  451. {3,"BIND_NOW"},{4,"STATIC_TLS"},{}},
  452. df_1_names[]={{0,"NOW"},{1,"GLOBAL"},{2,"GROUP"},{3,"NODELETE"},
  453. {5,"INITFIRST"},{27,"PIE"},{}},
  454. *names = *type == 'f' ? df_names : df_1_names;
  455. int mask;
  456. if (*type == 'F') printf("Flags: ");
  457. for (j=0; names[j].s; j++)
  458. if (val & (mask=(1<<names[j].bit)))
  459. printf("%s%s", names[j].s, (val &= ~mask) ? " " : "");
  460. if (val) printf("0x%llx", val);
  461. xputc('\n');
  462. } else if (*type == 'N' || *type == 'R' || *type == 'S') {
  463. char *s = TT.elf+dynstr.offset+val;
  464. if (dynstr.offset>TT.size || val>TT.size || dynstr.offset>TT.size-val)
  465. s = "???";
  466. printf("%s: [%s]\n", *type=='N' ? "Shared library" :
  467. (*type=='R' ? "Library runpath" : "Library soname"), s);
  468. } else if (*type == 'P') {
  469. j = strlen(type = dt_type(val));
  470. if (*type != '0') type += 2, j -= 3;
  471. printf("%*.*s\n", j, j, type);
  472. } else printf("0x%llx\n", val);
  473. }
  474. }
  475. }
  476. if (FLAG(dyn_syms)) show_symbols(&dynsym, &dynstr);
  477. if (FLAG(s)) show_symbols(&symtab, &strtab);
  478. if (FLAG(n)) {
  479. int found = 0;
  480. for (i=0; i<TT.shnum; i++) {
  481. if (!get_sh(i, &s)) continue;
  482. if (s.type == 7 /*SHT_NOTE*/) {
  483. printf("\nDisplaying notes found in: %s\n", s.name);
  484. show_notes(s.offset, s.size);
  485. found = 1;
  486. }
  487. }
  488. for (i=0; !found && i<phnum; i++) {
  489. if (!get_ph(i, &ph)) continue;
  490. if (ph.type == 4 /*PT_NOTE*/) {
  491. printf("\n"
  492. "Displaying notes found at file offset 0x%llx with length 0x%llx:\n",
  493. ph.offset, ph.filesz);
  494. show_notes(ph.offset, ph.filesz);
  495. }
  496. }
  497. }
  498. if (FLAG(x) && find_section(TT.x, &s)) {
  499. char *p = TT.elf+s.offset;
  500. long offset = 0;
  501. printf("\nHex dump of section '%s':\n", s.name);
  502. while (offset < s.size) {
  503. int space = 2*16 + 16/4;
  504. printf(" 0x%08lx ", offset);
  505. for (i=0; i<16 && offset < s.size; offset++)
  506. space -= printf("%02x%s", *p++, " "+!!(++i%4));
  507. printf("%*s", space, "");
  508. for (p -= i; i; i--, p++) putchar((*p>=' ' && *p<='~') ? *p : '.');
  509. xputc('\n');
  510. }
  511. xputc('\n');
  512. }
  513. if (FLAG(p) && find_section(TT.p, &s)) {
  514. char *begin = TT.elf+s.offset, *end = begin + s.size, *p = begin;
  515. int any = 0;
  516. printf("\nString dump of section '%s':\n", s.name);
  517. for (; p < end; p++) {
  518. if (isprint(*p)) {
  519. printf(" [%6tx] ", p-begin);
  520. while (p < end && isprint(*p)) putchar(*p++);
  521. xputc('\n');
  522. any=1;
  523. }
  524. }
  525. if (!any) printf(" No strings found in this section.\n");
  526. xputc('\n');
  527. }
  528. }
  529. void readelf_main(void)
  530. {
  531. char **arg;
  532. int all = FLAG_d|FLAG_h|FLAG_l|FLAG_n|FLAG_S|FLAG_s|FLAG_dyn_syms;
  533. if (FLAG(a)) toys.optflags |= all;
  534. if (FLAG(e)) toys.optflags |= FLAG_h|FLAG_l|FLAG_S;
  535. if (FLAG(s)) toys.optflags |= FLAG_dyn_syms;
  536. if (!(toys.optflags & (all|FLAG_p|FLAG_x))) help_exit("needs a flag");
  537. for (arg = toys.optargs; *arg; arg++) {
  538. int fd = open(TT.f = *arg, O_RDONLY);
  539. struct stat sb;
  540. if (fd == -1) perror_msg("%s", TT.f);
  541. else {
  542. if (fstat(fd, &sb)) perror_msg("%s", TT.f);
  543. else if (!sb.st_size) error_msg("%s: empty", TT.f);
  544. else if (!S_ISREG(sb.st_mode)) error_msg("%s: not a regular file",TT.f);
  545. else {
  546. TT.elf = xmmap(0, TT.size=sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  547. scan_elf();
  548. munmap(TT.elf, TT.size);
  549. }
  550. close(fd);
  551. }
  552. }
  553. }