elf: Set p_memsz to p_filesz for loadable PT_NOTE segment
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 14 Apr 2021 12:30:59 +0000 (05:30 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 14 Apr 2021 12:31:15 +0000 (05:31 -0700)
Since p_memsz of the loadable PT_NOTE segment should be the same as
p_filesz, set p_memsz on the loadable PT_NOTE segment when updating
p_filesz.

bfd/

PR binutils/27708
* elf.c (assign_file_positions_for_non_load_sections): Set
p_memsz to p_filesz for the loadable PT_NOTE segment.

binutils/

PR binutils/27708
* testsuite/binutils-all/x86-64/pr27708.dump: New file.
* testsuite/binutils-all/x86-64/pr27708.exe.bz2: Likewise.
* testsuite/binutils-all/x86-64/x86-64.exp: Run binutils/27708
test.

bfd/ChangeLog
bfd/elf.c
binutils/ChangeLog
binutils/testsuite/binutils-all/x86-64/pr27708.dump [new file with mode: 0644]
binutils/testsuite/binutils-all/x86-64/pr27708.exe.bz2 [new file with mode: 0644]
binutils/testsuite/binutils-all/x86-64/x86-64.exp

index d03929ae619548b13f5746289f201fd76609df14..6c89655664f50bc29b29fd92b591547d39debebe 100644 (file)
@@ -1,3 +1,9 @@
+2021-04-14  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR binutils/27708
+       * elf.c (assign_file_positions_for_non_load_sections): Set
+       p_memsz to p_filesz for the loadable PT_NOTE segment.
+
 2021-04-14  Frederic Cambus  <fred@statdns.com>
 
        * elf.c (elfcore_grok_netbsd_note): Remove unneeded #ifdef
index ef2193b07e99fd97baeb28688ca8cdb1f885f2fc..11ae2f3a81b83dbee4fd6574ddf4ee46d14a5136 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6300,6 +6300,11 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
                    {
                      p->p_filesz = (sect->filepos - m->sections[0]->filepos
                                     + hdr->sh_size);
+                     /* NB: p_memsz of the loadable PT_NOTE segment
+                        should be the same as p_filesz.  */
+                     if (p->p_type == PT_NOTE
+                         && (hdr->sh_flags & SHF_ALLOC) != 0)
+                       p->p_memsz = p->p_filesz;
                      break;
                    }
                }
index 8929c0700fa563cace45d33dfceadc9a10a4e51d..b109e3e5c2a71c8239c5a391cfce1f2878af24db 100644 (file)
@@ -1,3 +1,11 @@
+2021-04-14  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR binutils/27708
+       * testsuite/binutils-all/x86-64/pr27708.dump: New file.
+       * testsuite/binutils-all/x86-64/pr27708.exe.bz2: Likewise.
+       * testsuite/binutils-all/x86-64/x86-64.exp: Run binutils/27708
+       test.
+
 2021-04-14  Mark Harmstone  <mark@harmstone.com>
 
        PR 27686
diff --git a/binutils/testsuite/binutils-all/x86-64/pr27708.dump b/binutils/testsuite/binutils-all/x86-64/pr27708.dump
new file mode 100644 (file)
index 0000000..e9123df
--- /dev/null
@@ -0,0 +1,33 @@
+
+Elf file type is DYN (Shared object file)
+Entry point 0x5f0
+There are 11 program headers, starting at offset 64
+
+Program Headers:
+  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
+  PHDR           0x000040 0x0000000000000040 0x0000000000000040 0x000268 0x000268 R   0x8
+  INTERP         0x0002a8 0x00000000000002a8 0x00000000000002a8 0x00001c 0x00001c R   0x1
+      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
+  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x000938 0x000938 R E 0x1000
+  LOAD           0x000da0 0x0000000000001da0 0x0000000000001da0 0x000278 0x000279 RW  0x1000
+  DYNAMIC        0x000db0 0x0000000000001db0 0x0000000000001db0 0x000210 0x000210 RW  0x8
+  NOTE           0x0002e8 0x00000000000002e8 0x00000000000002e8 0x000030 0x000030 R   0x8
+  NOTE           0x0002c4 0x00000000000002c4 0x00000000000002c4 0x000078 0x000078 R   0x4
+  GNU_EH_FRAME   0x0008ec 0x00000000000008ec 0x00000000000008ec 0x00004c 0x00004c R   0x4
+  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10
+  TLS            0x000da0 0x0000000000001da0 0x0000000000001da0 0x000000 0x000009 R   0x8
+  GNU_RELRO      0x000da0 0x0000000000001da0 0x0000000000001da0 0x000260 0x000260 RW  0x8
+
+ Section to Segment mapping:
+  Segment Sections...
+   00     
+   01     .interp 
+   02     .interp .note.ABI-tag .note.gnu.property .note.gnu.build-id .dynsym .dynstr .gnu.hash .hash .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame .eh_frame_hdr 
+   03     .fini_array .init_array .dynamic .got .got.plt .data .bss 
+   04     .dynamic 
+   05     .note.gnu.property 
+   06     .note.ABI-tag .note.gnu.property .note.gnu.build-id 
+   07     .eh_frame_hdr 
+   08     
+   09     .tbss 
+   10     .fini_array .init_array .dynamic .got 
diff --git a/binutils/testsuite/binutils-all/x86-64/pr27708.exe.bz2 b/binutils/testsuite/binutils-all/x86-64/pr27708.exe.bz2
new file mode 100644 (file)
index 0000000..94063c6
Binary files /dev/null and b/binutils/testsuite/binutils-all/x86-64/pr27708.exe.bz2 differ
index 90fcc84c4f5c356be30178e12a8ab28f8d35f4aa..29ba1f029c4cb1599b163cde6ddc51b81b561365 100644 (file)
@@ -58,3 +58,41 @@ if {[catch "system \"bzip2 -dc $t > $tempfile\""] != 0} {
        }
     }
 }
+
+set t $srcdir/$subdir/pr27708.exe.bz2
+# We need to strip the ".bz2", but can leave the dirname.
+set test $subdir/[file tail $t]
+set testname [file rootname $test]
+verbose $testname
+if {[catch "system \"bzip2 -dc $t > $tempfile\""] != 0} {
+  untested "bzip2 -dc ($testname)"
+} else {
+    send_log "$STRIP $tempfile -o ${tempfile}.strip\n"
+    verbose "$STRIP $tempfile -o ${tempfile}.strip" 1
+    set got [catch "system \"$STRIP $tempfile -o ${tempfile}.strip\""]
+    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
+       fail $testname
+    } else {
+       send_log "$READELF -lW ${tempfile}.strip > tmpdir/pr27708.out 2> /dev/null\n"
+       verbose "$READELF -lW ${tempfile}.strip > tmpdir/pr27708.out 2> /dev/null" 1
+       set got [catch "system \"$READELF -lW ${tempfile}.strip > tmpdir/pr27708.out 2> /dev/null\""]
+
+       if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
+           fail $testname
+       } else {
+           send_log "cmp tmpdir/pr27708.out $srcdir/$subdir/pr27708.dump\n"
+           verbose "cmp tmpdir/pr27708.out $srcdir/$subdir/pr26808.dump" 1
+           set status [remote_exec build cmp "tmpdir/pr27708.out $srcdir/$subdir/pr27708.dump"]
+           set exec_output [lindex $status 1]
+           set exec_output [prune_warnings $exec_output]
+
+           if [string match "" $exec_output] then {
+               pass "strip ($testname)"
+           } else {
+               send_log "$exec_output\n"
+               verbose "$exec_output" 1
+               fail "strip ($testname)"
+           }
+       }
+    }
+}