From cc4a1f4d520f090bd563647df2614d09b66bd54a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 17 Feb 2016 14:01:19 -0600 Subject: Add dynamic/static checking to file (printing dynamic linker if found). --- toys/pending/file.c | 103 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 35 deletions(-) (limited to 'toys/pending/file.c') diff --git a/toys/pending/file.c b/toys/pending/file.c index 2c2f2864..1d09471d 100644 --- a/toys/pending/file.c +++ b/toys/pending/file.c @@ -3,6 +3,8 @@ * Copyright 2016 The Android Open Source Project * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html + * + * TODO: ar USE_FILE(NEWTOY(file, "<1", TOYFLAG_USR|TOYFLAG_BIN)) @@ -22,9 +24,11 @@ GLOBALS( int max_name_len; ) -static void do_elf_file() +// We don't trust elf.h to be there, and two codepaths for 32/64 is awkward +// anyway, so calculate struct offsets manually. (It's a fixed ABI.) +static void do_elf_file(int fd) { - int elf_endian = toybuf[5], e_type, e_machine, i; + int endian = toybuf[5], bits = toybuf[4], i, j; int64_t (*elf_int)(void *ptr, unsigned size) = peek_le; // Values from include/linux/elf-em.h (plus arch/*/include/asm/elf.h) // Names are linux/arch/ directory name @@ -40,43 +44,74 @@ static void do_elf_file() xprintf("ELF "); // "64-bit" - if (toybuf[4] == 1) xprintf("32-bit "); - else if (toybuf[4] == 2) xprintf("64-bit "); - else xprintf("(bad class %d)", toybuf[4]); + if (bits == 1) xprintf("32-bit "); + else if (bits == 2) xprintf("64-bit "); + else { + xprintf("(bad class %d) ", bits); + bits = 0; + } + + // e_machine, ala "x86", from big table above + j = elf_int(toybuf+18, 2); + for (i = 0; isizeof(toybuf)-128 + || dlpos!=lseek(fd, dlpos, SEEK_SET) + || dllen!=readall(fd, toybuf+128, dllen)) break; + printf(", dynamic (%.*s)", (int)dllen, toybuf+128); + } + if (!lib) printf(", static"); + else printf(", needs %d lib%s", lib, lib>1 ? "s" : ""); + munmap(map, phsize*phnum); + } + } // TODO: we'd need to actually parse the ELF file to report the rest... // ", dynamically linked" @@ -84,7 +119,6 @@ static void do_elf_file() // ", for Linux 2.6.24" // ", BuildID[sha1]=SHA" // ", stripped" - xputc('\n'); } @@ -95,9 +129,8 @@ static void do_regular_file(int fd, char *name) if (len<0) perror_msg("%s", name); - if (len>20 && strstart(&s, "\177ELF")) { - do_elf_file(len); - } else if (len>28 && strstart(&s, "\x89PNG\x0d\x0a\x1a\x0a")) { + if (len>40 && strstart(&s, "\177ELF")) do_elf_file(fd); + else if (len>28 && strstart(&s, "\x89PNG\x0d\x0a\x1a\x0a")) { // PNG is big-endian: https://www.w3.org/TR/PNG/#7Integers-and-byte-order int chunk_length = peek_be(s, 4); -- cgit v1.2.3