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