From 3ef788829c0c314b5a6f1476c8275703222c7f13 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 5 Nov 2026 15:48:23 -0600 Subject: [PATCH] Adjust readelf and tests to make TEST_HOST pass agains binutils 2.40. --- tests/readelf.test | 18 +++++++----------- toys/other/readelf.c | 28 ++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/tests/readelf.test b/tests/readelf.test index 2f0a4712..6e3622da 100755 --- a/tests/readelf.test +++ b/tests/readelf.test @@ -71,11 +71,11 @@ Section Headers: " "" "" # Verify many section header flags display properly. -NOSPACE=1 testing "-S flags" "readelf -SW $elf-shflags | head -32" \ +NOSPACE=1 testing "-S flags" "readelf -SW $elf-shflags | head -32 | sed 's/LOOS+0x1 /ANDROID_REL/'" \ "There are 28 section headers, starting at offset 0xc74: Section Headers: - [Nr] Name Type Address Off Size ES Flg Lk Inf Al + [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .interp PROGBITS 000001b4 0001b4 000013 00 A 0 0 1 [ 2] .note.android.ident NOTE 000001c8 0001c8 000018 00 A 0 0 4 @@ -98,7 +98,7 @@ Section Headers: [19] .got.plt PROGBITS 000026a0 0006a0 00001c 00 WA 0 0 4 [20] .data PROGBITS 000036bc 0006bc 00000c 00 WA 0 0 4 [21] .comment PROGBITS 00000000 0006c8 0000cc 01 MS 0 0 1 - [22] .ARM.attributes ATTRIBUTES 00000000 000794 000042 00 0 0 1 + [22] .ARM.attributes ARM_ATTRIBUTES 00000000 000794 000042 00 0 0 1 [23] .debug_frame PROGBITS 00000000 0007d8 00007a 00 C 0 0 4 [24] .symtab SYMTAB 00000000 000854 000220 10 26 27 4 [25] .shstrtab STRTAB 00000000 000a74 00010e 00 0 0 1 @@ -141,14 +141,14 @@ Program Headers: # binutils doesn't line up the column headers for 64-bit ELF files. # TODO: binutils readelf lies about trailing NULL entires binutils ld produces -NOSPACE=1 toyonly testing "-d" "readelf -dW $elf-full" " -Dynamic section at offset 0xd98 contains 33 entries: +NOSPACE=1 testing "-d" "readelf -dW $elf-full" " +Dynamic section at offset 0xd98 contains 29 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libc.so] 0x0000000000000001 (NEEDED) Shared library: [libm.so] 0x0000000000000001 (NEEDED) Shared library: [libdl.so] 0x0000000000000020 (PREINIT_ARRAY) 0x10d68 - 0x0000000000000021 (PREINIT_ARRAYSZ) 0x10 + 0x0000000000000021 (PREINIT_ARRAYSZ) 16 (bytes) 0x0000000000000019 (INIT_ARRAY) 0x10d78 0x000000000000001b (INIT_ARRAYSZ) 16 (bytes) 0x000000000000001a (FINI_ARRAY) 0x10d88 @@ -173,10 +173,6 @@ Dynamic section at offset 0xd98 contains 33 entries: 0x000000006ffffff0 (VERSYM) 0x528 0x000000006ffffff9 (RELACOUNT) 4 0x0000000000000000 (NULL) 0x0 - 0x0000000000000000 (NULL) 0x0 - 0x0000000000000000 (NULL) 0x0 - 0x0000000000000000 (NULL) 0x0 - 0x0000000000000000 (NULL) 0x0 " "" "" # toybox does a better job of decoding Android's ELF notes than binutils. @@ -239,7 +235,7 @@ Hex dump of section '.shstrtab': " "" "" # TODO: remove the sed when we handle symbol versions -testing "-s" "readelf -s $elf-short | sed s/@.*//" " +testing "-s" "readelf -Ws $elf-short | sed s/@.*//" " Symbol table '.dynsym' contains 11 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND diff --git a/toys/other/readelf.c b/toys/other/readelf.c index 9bc93dba..d6cc5d76 100644 --- a/toys/other/readelf.c +++ b/toys/other/readelf.c @@ -204,7 +204,7 @@ DECODER(dt_type, MAP({{0,"x(NULL)"},{1,"N(NEEDED)"},{2,"b(PLTRELSZ)"}, {20,"P(PLTREL)"},{21,"x(DEBUG)"},{22,"x(TEXTREL)"},{23,"x(JMPREL)"}, {24,"d(BIND_NOW)"},{25,"x(INIT_ARRAY)"},{26,"x(FINI_ARRAY)"}, {27,"b(INIT_ARRAYSZ)"},{28,"b(FINI_ARRAYSZ)"},{29,"R(RUNPATH)"}, - {30,"f(FLAGS)"},{32,"x(PREINIT_ARRAY)"},{33,"x(PREINIT_ARRAYSZ)"}, + {30,"f(FLAGS)"},{32,"x(PREINIT_ARRAY)"},{33,"b(PREINIT_ARRAYSZ)"}, {35,"b(RELRSZ)"},{36,"x(RELR)"},{37,"b(RELRENT)"}, {0x6000000f,"x(ANDROID_REL)"},{0x60000010,"b(ANDROID_RELSZ)"}, {0x60000011,"x(ANDROID_RELA)"},{0x60000012,"b(ANDROID_RELASZ)"}, @@ -241,7 +241,7 @@ DECODER(sh_type, MAP({{0,"NULL"},{1,"PROGBITS"},{2,"SYMTAB"},{3,"STRTAB"}, {0x6fffff00,"ANDROID_RELR"},{0x6ffffff6,"GNU_HASH"}, {0x6ffffffd,"VERDEF"},{0x6ffffffe,"VERNEED"}, {0x6fffffff,"VERSYM"},{0x70000001,"ARM_EXIDX"}, - {0x70000003,"ATTRIBUTES"}})) + {0x70000003,"ARM_ATTRIBUTES"}})) DECODER(stb_type, MAP({{0,"LOCAL"},{1,"GLOBAL"},{2,"WEAK"}})) @@ -547,7 +547,7 @@ static void scan_elf() TT.shnum, TT.shoff); printf("\nSection Headers:\n" " [Nr] %-17s %-15s %-*s %-6s %-6s ES Flg Lk Inf Al\n", - "Name", "Type", w, "Address", "Off", "Size"); + "Name", "Type", w, TT.bits-1 ? "Addr": "Address", "Off", "Size"); } } // We need to iterate through the section headers even if we're not @@ -625,18 +625,28 @@ static void scan_elf() // 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; + char *dyn = TT.elf+dynamic.offset, *end = dyn+dynamic.size, *type = dyn; + unsigned long long tag, val; + unsigned len = 0; + + // Count to first NUL entry + while (type