From 4789e0fb92b03c3d8de548489c871d17f8f35cd0 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Fri, 9 Nov 2018 08:18:22 +0000 Subject: [PATCH] libelf: Explicitly update section data after (de)compression. We need to explictly trigger a section data reload after updating the ELF section rawdata to make sure it gets written out to disk on an elf_update. Doing this showed one bug/inefficiently when the underlying file has a different endianness. In that case for debug sections we would convert by allocating a new buffer and just copying over the raw data into a new buffer. This is not really necessary and would hide any relocations done on the rawdata by libdwfl. Added a couple of new ppc64 big endian testfiles that show the issue. Signed-off-by: Mark Wielaard --- libelf/ChangeLog | 7 +++ libelf/elf_compress.c | 6 ++ libelf/elf_getdata.c | 3 +- tests/ChangeLog | 10 ++++ tests/Makefile.am | 5 +- tests/run-readelf-zdebug-rel.sh | 106 +++++++++++++++++++++++++++++++++ tests/run-strip-reloc.sh | 4 ++ tests/testfile-debug-rel-ppc64-g.o.bz2 | Bin 0 -> 1400 bytes tests/testfile-debug-rel-ppc64-z.o.bz2 | Bin 0 -> 1420 bytes tests/testfile-debug-rel-ppc64.o.bz2 | Bin 0 -> 1103 bytes 10 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 tests/testfile-debug-rel-ppc64-g.o.bz2 create mode 100644 tests/testfile-debug-rel-ppc64-z.o.bz2 create mode 100644 tests/testfile-debug-rel-ppc64.o.bz2 diff --git a/libelf/ChangeLog b/libelf/ChangeLog index af56503..53da9a6 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,10 @@ +2018-11-09 Mark Wielaard + + * elf_compress.c (__libelf_reset_rawdata): Make rawdata change + explicit by calling __libelf_set_data_list. + * elf_getdata.c (convert_data): Don't convert if type is ELF_T_BYTE + even if endianness is different. + 2018-10-18 Mark Wielaard * libelf.h (Elf_Type): Add ELF_T_NHDR8. diff --git a/libelf/elf_compress.c b/libelf/elf_compress.c index fd412e8..d96245d 100644 --- a/libelf/elf_compress.c +++ b/libelf/elf_compress.c @@ -326,6 +326,12 @@ __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size, size_t align, scn->rawdata_base = buf; scn->flags |= ELF_F_MALLOCED; + + /* Pretend we (tried to) read the data from the file and setup the + data (might have to convert the Chdr to native format). */ + scn->data_read = 1; + scn->flags |= ELF_F_FILEDATA; + __libelf_set_data_list_rdlock (scn, 1); } int diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c index 4f80aaf..2043bba 100644 --- a/libelf/elf_getdata.c +++ b/libelf/elf_getdata.c @@ -146,7 +146,8 @@ convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass, { const size_t align = __libelf_type_align (eclass, type); - if (data == MY_ELFDATA) + /* Do we need to convert the data and/or adjust for alignment? */ + if (data == MY_ELFDATA || type == ELF_T_BYTE) { if (((((size_t) (char *) scn->rawdata_base)) & (align - 1)) == 0) /* No need to copy, we can use the raw data. */ diff --git a/tests/ChangeLog b/tests/ChangeLog index 23e9113..7ce3980 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,13 @@ +2018-11-09 Mark Wielaard + + * testfile-debug-rel-ppc64-g.o.bz2: New test file. + * testfile-debug-rel-ppc64-z.o.bz2: Likewise. + * testfile-debug-rel-ppc64.o.bz2: Likewise. + * Makefile.am (EXTRA_DIST): Add testfile-debug-rel-ppc64-g.o.bz2, + testfile-debug-rel-ppc64-z.o.bz2 and testfile-debug-rel-ppc64.o.bz2. + * run-strip-reloc.sh: Also test on testfile-debug-rel-ppc64.o. + * run-readelf-zdebug-rel.sh: Also test on testfile-debug-rel-ppc64*.o. + 2018-10-26 Mark Wielaard * run-strip-reloc.sh: Add a test for --reloc-debug-sections-only. diff --git a/tests/Makefile.am b/tests/Makefile.am index a2a381a..d3ac345 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -412,7 +412,10 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \ testfile-riscv64.bz2 testfile-riscv64-s.bz2 \ testfile-riscv64-core.bz2 \ run-copyadd-sections.sh run-copymany-sections.sh \ - run-typeiter-many.sh run-strip-test-many.sh + run-typeiter-many.sh run-strip-test-many.sh \ + testfile-debug-rel-ppc64-g.o.bz2 \ + testfile-debug-rel-ppc64-z.o.bz2 \ + testfile-debug-rel-ppc64.o.bz2 if USE_VALGRIND valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1' diff --git a/tests/run-readelf-zdebug-rel.sh b/tests/run-readelf-zdebug-rel.sh index 3f20078..53fa42a 100755 --- a/tests/run-readelf-zdebug-rel.sh +++ b/tests/run-readelf-zdebug-rel.sh @@ -146,4 +146,110 @@ cat loc.out | sed -e "s/'.debug_loc' at offset 0x185/'.zdebug_loc' at offset 0x1 cat loc.out | sed -e "s/at offset 0x185/at offset 0x150/" | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc testfile-debug-rel-z.o +# Same as above, but on ppc64 +testfiles testfile-debug-rel-ppc64.o +testfiles testfile-debug-rel-ppc64-g.o testfile-debug-rel-ppc64-z.o + +cat > info.out << \EOF + +DWARF section [ 6] '.debug_info' at offset 0x80: + [Offset] + Compilation unit at offset 0: + Version: 4, Abbreviation section offset: 0, Address size: 8, Offset size: 4 + [ b] compile_unit abbrev: 1 + producer (strp) "GNU C11 7.3.1 20180712 (Red Hat 7.3.1-6) -Asystem=linux -Asystem=unix -Asystem=posix -msecure-plt -g -Og" + language (data1) C99 (12) + name (strp) "testfile-zdebug-rel.c" + comp_dir (strp) "/home/mjw" + low_pc (addr) 000000000000000000 + high_pc (data8) 44 (0x000000000000002c) + stmt_list (sec_offset) 0 + [ 2d] subprogram abbrev: 2 + external (flag_present) yes + name (strp) "main" + decl_file (data1) testfile-zdebug-rel.c (1) + decl_line (data1) 4 + prototyped (flag_present) yes + type (ref4) [ 82] + low_pc (addr) 000000000000000000 + high_pc (data8) 44 (0x000000000000002c) + frame_base (exprloc) + [ 0] call_frame_cfa + GNU_all_call_sites (flag_present) yes + sibling (ref4) [ 82] + [ 4e] formal_parameter abbrev: 3 + name (strp) "argc" + decl_file (data1) testfile-zdebug-rel.c (1) + decl_line (data1) 4 + type (ref4) [ 82] + location (sec_offset) location list [ 0] + [ 5d] formal_parameter abbrev: 4 + name (strp) "argv" + decl_file (data1) testfile-zdebug-rel.c (1) + decl_line (data1) 4 + type (ref4) [ 89] + location (exprloc) + [ 0] reg4 + [ 6a] variable abbrev: 5 + name (string) "a" + decl_file (data1) testfile-zdebug-rel.c (1) + decl_line (data1) 6 + type (ref4) [ 9c] + const_value (sdata) 18446744073709551607 (-9) + [ 74] variable abbrev: 6 + name (string) "b" + decl_file (data1) testfile-zdebug-rel.c (1) + decl_line (data1) 7 + type (ref4) [ 9c] + location (sec_offset) location list [ 4e] + [ 82] base_type abbrev: 7 + byte_size (data1) 4 + encoding (data1) signed (5) + name (string) "int" + [ 89] pointer_type abbrev: 8 + byte_size (data1) 8 + type (ref4) [ 8f] + [ 8f] pointer_type abbrev: 8 + byte_size (data1) 8 + type (ref4) [ 95] + [ 95] base_type abbrev: 9 + byte_size (data1) 1 + encoding (data1) unsigned_char (8) + name (strp) "char" + [ 9c] base_type abbrev: 9 + byte_size (data1) 8 + encoding (data1) unsigned (7) + name (strp) "long unsigned int" +EOF + +cat info.out | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=info testfile-debug-rel-ppc64.o + +cat info.out | sed -e "s/'.debug_info'/'.zdebug_info'/" | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=info testfile-debug-rel-ppc64-g.o + +cat info.out | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=info testfile-debug-rel-ppc64-z.o + +cat > loc.out << \EOF + +DWARF section [ 9] '.debug_loc' at offset 0x1af: + + CU [ b] base: 000000000000000000 + [ 0] range 0, 4 + [ 0] reg3 + range 4, 14 + [ 0] breg3 -42 + [ 2] stack_value + range 14, 2c + [ 0] GNU_entry_value: + [ 0] reg3 + [ 3] stack_value + [ 4e] range 8, 18 + [ 0] reg3 +EOF + +cat loc.out | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc testfile-debug-rel-ppc64.o + +cat loc.out | sed -e "s/'.debug_loc' at offset 0x1af/'.zdebug_loc' at offset 0x15f/" | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc testfile-debug-rel-ppc64-g.o + +cat loc.out | sed -e "s/at offset 0x1af/at offset 0x177/" | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc testfile-debug-rel-ppc64-z.o + exit 0 diff --git a/tests/run-strip-reloc.sh b/tests/run-strip-reloc.sh index 6e54ab4..6f299ab 100755 --- a/tests/run-strip-reloc.sh +++ b/tests/run-strip-reloc.sh @@ -135,4 +135,8 @@ testrun ${abs_top_builddir}/src/elfcompress -o strip-compressed.o -t zlib \ runtest strip-uncompressed.o 1 runtest strip-compressed.o 1 +# See run-readelf-zdebug-rel.sh +testfiles testfile-debug-rel-ppc64.o +runtest testfile-debug-rel-ppc64.o 1 + exit $status diff --git a/tests/testfile-debug-rel-ppc64-g.o.bz2 b/tests/testfile-debug-rel-ppc64-g.o.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..8c5ec99e7d413fcf6b2109dc5af751c4cecff508 GIT binary patch literal 1400 zcmV-;1&8`VT4*^jL0KkKS->=g0RRKT|NsC0|L*($fBpae_y7Oz|M&Xu-~awu{hE3E za&G(df2hy{o7{jmZM(SdK~brxqtZ0fO-4=X1i&&z$ueev8&lF~VrjH`gHh-Krip+7 z9+8msFief9>SSbTk?EjJ8bfMr05q6JX^2uN$kRXu(gr{SBSwLvKmZ1S13&-(4FCYp z0000000000007k09#c~^^rxzRsphC;+MZL=Y2s>njSo=J&;jZI3?_{RhL2DHVgLXQ z05Td40MKcsOrEA7XaE2pXlMY?(?)=3WHJCU41j3RWCjQWMnD>ApazD3XaS%A02u%P zGzNgs7$j0gr>Tf#G;KmQf_qVnZsMG!P&NB3PoA z5K{p>VBe_Z@#F|6nlfdK+EEaIi4Y2S9HN1wk%{+Hj*Y>#z!V9>1h~#44wp24BTd}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03z=4`m)Oq8j3i^VBP(H*Gp~VOIFHt#wfiboD006>d50o zdTjBqYJZ1y>{cKTf2UvF!{TN&CUB!I!m4#9;P&r+%Ca^J{+iHyOBpAJ#}dhJs@l!%KvH?zAN!Xe!f*lB`b&NIn;}0l@hq=X`>L) zqy!tW^s_{HHeD972boDL4vnuWZ-~m%=DQTjn0S>x3J!bYuS}P@qF}e*qrI5=x@;Cm a;M(q!sx2UDCc(J>@pmLsg$V{<#X{hSoOrqb literal 0 HcmV?d00001 diff --git a/tests/testfile-debug-rel-ppc64.o.bz2 b/tests/testfile-debug-rel-ppc64.o.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..8340d0cee0e0f509b96a230e47473f1318211fb4 GIT binary patch literal 1103 zcmV-V1hD%;T4*^jL0KkKStF?k-2enHfBgUd|Lphw{r~&z-y#41|Mt=-!sgB~ga|+Y zXvok5njQsd?Vgd|AdxXmpfx>BG&Bt~GzN@AAZXE`83uun01X2`4FRCZpa@AdHjE}} zO@wNCo~D{KQZu7$KkoMnDY#rkXM|8feH2AbOf;$Yf*^B$Fh`Jxx6m(1w6AX_25b z007VcXaE2KpwKiK0LW-FOy3cj)iglUBM0)p^`T|3D=Z>;BIYsNdsZw$RI$T{$p}-= zaI02~LIsD;NDsax!Vyg7Y|Culj^shU8+ya;h!_(h0U}s1LVQ7sSNhrcC__bx{(STC(pNXq69;7Y+;n009n~Hx#MJ3KV7{037m{#%5%MQwYdl(@wf> zCH*laQLTe)K@3LYjO#figw1+WLoiGzi`^S{_cSCJEJVx*XgP5JR$u|gl+slknSj*I zB1|eo6gX)lHVut~X(rMG9f(Ky(E3e!v0++S#p6p6p&jCx2oZ9`by$m6Lr7Y8mHUJs z=bRWq0*=o*MuCg4?XoQLE_U*x9~2H=69S^?{TcxeM<#7GDQ=piP+3#Qg70?4Hn9#bSW`?Q25 z?kYkT6{FgUq&Wlu7|^?x^<;m#9=U>JG{PN|Lax)5JjJZE(}5<~o;)UnDFFe6IF=C* zf&{^69^mxC2Ocg36kbbKYL!`vNd!UX7&oX!kR*&V>w@%@`U;;M;v9gSgkGTL88;33 z?N8sVjgV3SuxMM$MP~|&j|gIf)jP?oAZfI&nKoctd!w#b-Ieg*$gD`1wkl2=`qo~8 zl;KR8RL-WZ#o-OswV4!Ts5B$2XoG|zCMU{K9mJ(aFk`JY=n4ULLWL<17>20{@*qG5 z(grc^uM(wkfp&(!FFbOLnXV48t<4R{noiGHU5o>jCNRN1sDQaew8&Km zEeZwzlmr%Slv>_32;B%%*1`Y) literal 0 HcmV?d00001 -- 2.7.4