From a4430f2fbe5339c256b595c79be00e043ea0458a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 15 May 2021 13:57:12 -0500 Subject: Promote readelf to other. --- toys/other/readelf.c | 640 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 640 insertions(+) create mode 100644 toys/other/readelf.c (limited to 'toys/other') diff --git a/toys/other/readelf.c b/toys/other/readelf.c new file mode 100644 index 00000000..45afd83a --- /dev/null +++ b/toys/other/readelf.c @@ -0,0 +1,640 @@ +/* readelf.c - display information about ELF files. + * + * Copyright 2019 The Android Open Source Project + * + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/nm.html + +USE_READELF(NEWTOY(readelf, "<1(dyn-syms)adehlnp:SsWx:", TOYFLAG_USR|TOYFLAG_BIN)) + +config READELF + bool "readelf" + default y + help + usage: readelf [-adehlnSs] [-p SECTION] [-x SECTION] [file...] + + Displays information about ELF files. + + -a Equivalent to -dhlnSs + -d Show dynamic section + -e Headers (equivalent to -hlS) + -h Show ELF header + -l Show program headers + -n Show notes + -p S Dump strings found in named/numbered section + -S Show section headers + -s Show symbol tables (.dynsym and .symtab) + -x S Hex dump of named/numbered section + + --dyn-syms Show just .dynsym symbol table +*/ + +#define FOR_readelf +#include "toys.h" + +GLOBALS( + char *x, *p; + + char *elf, *shstrtab, *f; + unsigned long long shoff, phoff, size, shstrtabsz; + int bits, endian, shnum, shentsize, phentsize; +) + +// Section header. +struct sh { + unsigned type, link, info; + unsigned long long flags, addr, offset, size, addralign, entsize; + char *name; +}; + +// Program header. +struct ph { + unsigned type, flags; + unsigned long long offset, vaddr, paddr, filesz, memsz, align; +}; + +static long long elf_get(char **p, int len) +{ + long long result; + + if (*p+len-TT.elf>TT.size) + perror_exit("Access off end: %ld[%d] of %lld\n", *p-TT.elf, len, TT.size); + + result = ((TT.endian == 2) ? peek_be : peek_le)(*p, len); + *p += len; + return result; +} + +static unsigned long long elf_long(char **p) +{ + return elf_get(p, 4*(TT.bits+1)); +} + +static unsigned elf_int(char **p) +{ + return elf_get(p, 4); +} + +static unsigned short elf_short(char **p) +{ + return elf_get(p, 2); +} + +static int get_sh(unsigned i, struct sh *s) +{ + char *shdr = TT.elf+TT.shoff+i*TT.shentsize; + unsigned name_offset; + + if (i >= TT.shnum || shdr > TT.elf+TT.size-TT.shentsize) { + printf("No shdr %d\n", i); + return 0; + } + + name_offset = elf_int(&shdr); + s->type = elf_int(&shdr); + s->flags = elf_long(&shdr); + s->addr = elf_long(&shdr); + s->offset = elf_long(&shdr); + s->size = elf_long(&shdr); + s->link = elf_int(&shdr); + s->info = elf_int(&shdr); + s->addralign = elf_long(&shdr); + s->entsize = elf_long(&shdr); + + if (s->type != 8) { + if (s->offset>TT.size || s->size>TT.size || s->offset>TT.size-s->size) { + printf("Bad offset/size %llu/%llu for sh %d\n", s->offset, s->size, i); + return 0; + } + } + + if (!TT.shstrtab) s->name = "?"; + else { + s->name = TT.shstrtab + name_offset; + if (name_offset > TT.shstrtabsz || s->name >= TT.elf+TT.size) { + printf("Bad name for sh %d\n", i); + return 0; + } + } + + return 1; +} + +static int find_section(char *spec, struct sh *s) +{ + char *end; + unsigned i; + + // Valid section number? + i = estrtol(spec, &end, 0); + if (!errno && !*end && iname, spec)) return 1; + + error_msg("%s: no section '%s", TT.f, spec); + return 0; +} + +static int get_ph(int i, struct ph *ph) +{ + char *phdr = TT.elf+TT.phoff+i*TT.phentsize; + + if (phdr > TT.elf+TT.size-TT.phentsize) { + printf("Bad phdr %d\n", i); + return 0; + } + + // Elf64_Phdr reordered fields. + ph->type = elf_int(&phdr); + if (TT.bits) { + ph->flags = elf_int(&phdr); + ph->offset = elf_long(&phdr); + ph->vaddr = elf_long(&phdr); + ph->paddr = elf_long(&phdr); + ph->filesz = elf_long(&phdr); + ph->memsz = elf_long(&phdr); + ph->align = elf_long(&phdr); + } else { + ph->offset = elf_int(&phdr); + ph->vaddr = elf_int(&phdr); + ph->paddr = elf_int(&phdr); + ph->filesz = elf_int(&phdr); + ph->memsz = elf_int(&phdr); + ph->flags = elf_int(&phdr); + ph->align = elf_int(&phdr); + } + + if (ph->offset >= TT.size-ph->filesz) { + printf("phdr %d has bad offset/size %llu/%llu", i, ph->offset, ph->filesz); + return 0; + } + + return 1; +} + +#define MAP(...) __VA_ARGS__ +#define DECODER(name, values) \ + static char *name(int type) { \ + static char unknown[20]; \ + struct {int v; char *s;} a[] = values; \ + int i; \ + \ + for (i=0; ioffset, *ndx; + int numsym = table->size/(TT.bits ? 24 : 16), i; + + if (!numsym) return; + + xputc('\n'); + printf("Symbol table '%s' contains %d entries:\n" + " Num: %*s Size Type Bind Vis Ndx Name\n", + table->name, numsym, 5+8*TT.bits, "Value"); + for (i=0; ioffset + st_name; + if (name >= TT.elf+TT.size) name = "???"; + + if (!st_shndx) ndx = "UND"; + else if (st_shndx==0xfff1) ndx = "ABS"; + else sprintf(ndx = buf, "%d", st_shndx); + + // TODO: look up and show any symbol versions with @ or @@. + + printf("%6d: %0*x %5lu %-7s %-6s %-9s%3s %s\n", i, 8*(TT.bits+1), + st_value, st_size, stt_type(st_info & 0xf), stb_type(st_info >> 4), + stv_type(st_other & 3), ndx, name); + } +} + +static int notematch(int namesz, char **p, char *expected) +{ + if (namesz!=strlen(expected)+1 || strcmp(*p, expected)) return 0; + *p += namesz; + + return 1; +} + +static void show_notes(unsigned long offset, unsigned long size) +{ + char *note = TT.elf + offset; + + if (size > TT.size || offset > TT.size-size) { + printf("Bad note bounds %lu/%lu\n", offset, size); + + return; + } + + printf(" %-20s%11s\tDescription\n", "Owner", "Data size"); + while (note < TT.elf+offset+size) { + char *p = note, *desc; + unsigned namesz=elf_int(&p), descsz=elf_int(&p), type=elf_int(&p), j=0; + + if (namesz > size || descsz > size) + return error_msg("%s: bad note @%lu", TT.f, offset); + printf(" %-20.*s 0x%08x\t", namesz, p, descsz); + if (notematch(namesz, &p, "GNU")) { + if (type == 1) { + printf("NT_GNU_ABI_TAG\tOS: %s, ABI: %u.%u.%u", + !elf_int(&p)?"Linux":"?", elf_int(&p), elf_int(&p), elf_int(&p)), j=1; + } else if (type == 3) { +// TODO should this set j=1? + printf("NT_GNU_BUILD_ID\t"); + for (;j=132) printf(", NDK %.64s (%.64s)", p, p+64); + } else p -= 8; + } else if (notematch(namesz, &p, "CORE")) { + if (*(desc = nt_type_core(type)) != '0') printf("%s", desc), j=1; +// TODO else p -= 5? + } else if (notematch(namesz, &p, "LINUX")) { + if (*(desc = nt_type_linux(type)) != '0') printf("%s", desc), j=1; +// TODO else p -= 6? + } + + // If we didn't do custom output above, show a hex dump. + if (!j) { + printf("0x%x\t", type); + for (;j1 || TT.endian<1 || TT.endian>2 || hdr[6]!=1) + return error_msg("%s: bad ELF", TT.f); + + hdr += 16; // EI_NIDENT + type = elf_short(&hdr); + machine = elf_short(&hdr); + version = elf_int(&hdr); + entry = elf_long(&hdr); + TT.phoff = elf_long(&hdr); + TT.shoff = elf_long(&hdr); + flags = elf_int(&hdr); + ehsize = elf_short(&hdr); + TT.phentsize = elf_short(&hdr); + phnum = elf_short(&hdr); + TT.shentsize = elf_short(&hdr); + TT.shnum = elf_short(&hdr); + shstrndx = elf_short(&hdr); + + if (toys.optc > 1) printf("\nFile: %s\n", TT.f); + + if (FLAG(h)) { + printf("ELF Header:\n"); + printf(" Magic: "); + for (i=0; i<16; i++) printf("%02x%c", TT.elf[i], (i==15) ? '\n' : ' '); + printf(" Class: ELF%d\n", TT.bits?64:32); + printf(" Data: 2's complement, %s endian\n", + (TT.endian==2)?"big":"little"); + printf(" Version: 1 (current)\n"); + printf(" OS/ABI: %s\n", os_abi(TT.elf[7])); + printf(" ABI Version: %d\n", TT.elf[8]); + printf(" Type: %s\n", et_type(type)); + printf(" Machine: %s\n", elf_arch_name(machine)); + printf(" Version: 0x%x\n", version); + printf(" Entry point address: 0x%x\n", entry); + printf(" Start of program headers: %llu (bytes into file)\n", + TT.phoff); + printf(" Start of section headers: %llu (bytes into file)\n", + TT.shoff); + printf(" Flags: 0x%x\n", flags); + printf(" Size of this header: %d (bytes)\n", ehsize); + printf(" Size of program headers: %d (bytes)\n", TT.phentsize); + printf(" Number of program headers: %d\n", phnum); + printf(" Size of section headers: %d (bytes)\n", TT.shentsize); + printf(" Number of section headers: %d\n", TT.shnum); + printf(" Section header string table index: %d\n", shstrndx); + } + if (TT.phoff > TT.size) return error_msg("%s: bad phoff", TT.f); + if (TT.shoff > TT.size) return error_msg("%s: bad shoff", TT.f); + + // Set up the section header string table so we can use section header names. + // Core files have shstrndx == 0. + TT.shstrtab = 0; + TT.shstrtabsz = 0; + if (shstrndx) { + if (!get_sh(shstrndx, &shstr) || shstr.type != 3 /*SHT_STRTAB*/) + return error_msg("%s: bad shstrndx", TT.f); + TT.shstrtab = TT.elf+shstr.offset; + TT.shstrtabsz = shstr.size; + } + + w = 8<= ph.offset && s.offset+s.size <= ph.offset+ph.filesz) + printf("%s ", s.name); + } + xputc('\n'); + } + } + } + + // binutils ld emits a bunch of extra DT_NULL entries, so binutils readelf + // uses two passes here! We just tell the truth, which matches -h. + if (FLAG(d)) { + char *dyn = TT.elf+dynamic.offset, *end = dyn+dynamic.size; + + xputc('\n'); + if (!dynamic.size) printf("There is no dynamic section in this file.\n"); + else if (!dynamic.entsize) printf("Bad dynamic entry size 0!\n"); + else { + printf("Dynamic section at offset 0x%llx contains %lld entries:\n" + " %-*s %-20s %s\n", dynamic.offset, dynamic.size/dynamic.entsize, + w+2, "Tag", "Type", "Name/Value"); + while (dyn < end) { + unsigned long long tag = elf_long(&dyn), val = elf_long(&dyn); + char *type = dt_type(tag); + + printf(" 0x%0*llx %-20s ", w, tag, type+(*type!='0')); + if (*type == 'd') printf("%lld\n", val); + else if (*type == 'b') printf("%lld (bytes)\n", val); +// TODO: trusting this %s to be null terminated + else if (*type == 's') printf("%s\n", TT.elf+dynstr.offset+val); + else if (*type == 'f' || *type == 'F') { + struct bitname { int bit; char *s; } + df_names[] = {{0, "ORIGIN"},{1,"SYMBOLIC"},{2,"TEXTREL"}, + {3,"BIND_NOW"},{4,"STATIC_TLS"},{}}, + df_1_names[]={{0,"NOW"},{1,"GLOBAL"},{2,"GROUP"},{3,"NODELETE"}, + {5,"INITFIRST"},{27,"PIE"},{}}, + *names = *type == 'f' ? df_names : df_1_names; + int mask; + + if (*type == 'F') printf("Flags: "); + for (j=0; names[j].s; j++) + if (val & (mask=(1<TT.size || val>TT.size || dynstr.offset>TT.size-val) + s = "???"; + printf("%s: [%s]\n", *type=='N' ? "Shared library" : + (*type=='R' ? "Library runpath" : "Library soname"), s); + } else if (*type == 'P') { + j = strlen(type = dt_type(val)); + if (*type != '0') type += 2, j -= 3; + printf("%*.*s\n", j, j, type); + } else printf("0x%llx\n", val); + } + } + } + + if (FLAG(dyn_syms)) show_symbols(&dynsym, &dynstr); + if (FLAG(s)) show_symbols(&symtab, &strtab); + + if (FLAG(n)) { + int found = 0; + + for (i=0; i=' ' && *p<='~') ? *p : '.'); + xputc('\n'); + } + xputc('\n'); + } + + if (FLAG(p) && find_section(TT.p, &s)) { + char *begin = TT.elf+s.offset, *end = begin + s.size, *p = begin; + int any = 0; + + printf("\nString dump of section '%s':\n", s.name); + for (; p < end; p++) { + if (isprint(*p)) { + printf(" [%6tx] ", p-begin); + while (p < end && isprint(*p)) putchar(*p++); + xputc('\n'); + any=1; + } + } + if (!any) printf(" No strings found in this section.\n"); + xputc('\n'); + } +} + +void readelf_main(void) +{ + char **arg; + int all = FLAG_d|FLAG_h|FLAG_l|FLAG_n|FLAG_S|FLAG_s|FLAG_dyn_syms; + + if (FLAG(a)) toys.optflags |= all; + if (FLAG(e)) toys.optflags |= FLAG_h|FLAG_l|FLAG_S; + if (FLAG(s)) toys.optflags |= FLAG_dyn_syms; + if (!(toys.optflags & (all|FLAG_p|FLAG_x))) help_exit("needs a flag"); + + for (arg = toys.optargs; *arg; arg++) { + int fd = open(TT.f = *arg, O_RDONLY); + struct stat sb; + + if (fd == -1) perror_msg("%s", TT.f); + else { + if (fstat(fd, &sb)) perror_msg("%s", TT.f); + else if (!sb.st_size) error_msg("%s: empty", TT.f); + else if (!S_ISREG(sb.st_mode)) error_msg("%s: not a regular file",TT.f); + else { + TT.elf = xmmap(0, TT.size=sb.st_size, PROT_READ, MAP_SHARED, fd, 0); + scan_elf(); + munmap(TT.elf, TT.size); + } + close(fd); + } + } +} -- cgit v1.2.3