elf/x86-64: Adjust R_AMD64_DIR64/R_AMD64_DIR32 for PE/x86-64 inputs
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 12 Jan 2021 13:10:58 +0000 (05:10 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 12 Jan 2021 13:19:24 +0000 (05:19 -0800)
Subtract the value of the section contents for R_AMD64_DIR64 and
R_AMD64_DIR32 relocations when generating ELF output from PE/x86-64
inputs.

bfd/

PR ld/27171
* reloc.c (bfd_perform_relocation): Adjust R_AMD64_DIR64 and
R_AMD64_DIR32 relocations for PE/x86-64 inputs.

ld/

PR ld/27171
* testsuite/ld-x86-64/pe-x86-64-5.obj.bz2: New file.
* testsuite/ld-x86-64/pe-x86-64-5.od: Likewise.
* testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise.
* testsuite/ld-x86-64/pe-x86-64.exp: Run PR ld/27171 test.

bfd/ChangeLog
bfd/reloc.c
ld/ChangeLog
ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2 [new file with mode: 0644]
ld/testsuite/ld-x86-64/pe-x86-64-5.od [new file with mode: 0644]
ld/testsuite/ld-x86-64/pe-x86-64-5.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/pe-x86-64.exp

index 38123b44407bc378b24d3b57a28d80020859a873..eaeb47d1662dcc22b840aebe270cb500f6792c6d 100644 (file)
@@ -1,3 +1,9 @@
+2021-01-12  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27171
+       * reloc.c (bfd_perform_relocation): Adjust R_AMD64_DIR64 and
+       R_AMD64_DIR32 relocations for PE/x86-64 inputs.
+
 2021-01-11  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/27173
index 46c996ffbea70e2048bb7c99a38070636cd3af70..4f4b95a0b7ff11bd24ab0e0b9550801730cac6f9 100644 (file)
@@ -921,6 +921,13 @@ space consuming.  For each target:
       if (howto->type >= R_AMD64_PCRLONG_1
          && howto->type <= R_AMD64_PCRLONG_5)
        relocation -= (bfd_vma)(howto->type - R_AMD64_PCRLONG);
+      else if (howto->type == R_AMD64_DIR64
+              || howto->type == R_AMD64_DIR32)
+       {
+         bfd_vma val = read_reloc (abfd, (bfd_byte *) data + octets,
+                                   howto);
+         relocation -= val & howto->src_mask;
+       }
     }
 
   /* FIXME: This overflow checking is incomplete, because the value
index bedd0fa326f68730ec01766a8ee462cf9bf14d12..a4b0ea6e56bbb7a42111ac57aeca38f7b265ccc5 100644 (file)
@@ -1,3 +1,11 @@
+2021-01-12  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27171
+       * testsuite/ld-x86-64/pe-x86-64-5.obj.bz2: New file.
+       * testsuite/ld-x86-64/pe-x86-64-5.od: Likewise.
+       * testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise.
+       * testsuite/ld-x86-64/pe-x86-64.exp: Run PR ld/27171 test.
+
 2021-01-11  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/27173
diff --git a/ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2 b/ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2
new file mode 100644 (file)
index 0000000..bb0d38c
Binary files /dev/null and b/ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2 differ
diff --git a/ld/testsuite/ld-x86-64/pe-x86-64-5.od b/ld/testsuite/ld-x86-64/pe-x86-64-5.od
new file mode 100644 (file)
index 0000000..8a4f4a6
--- /dev/null
@@ -0,0 +1,34 @@
+
+.*: +file format .*
+
+SYMBOL TABLE:
+0+402014 g       .bss  0000000000000000 non_initdummy
+0+402010 g       .data 0000000000000000 initdummy
+0+401000 g       .text\$mn     0000000000000000 begin
+0+402012 g       .bss  0000000000000000 __bss_start
+0+402000 g       .data 0000000000000000 Struct
+0+402011 g       .data 0000000000000000 initializedVar
+0+402012 g       .data 0000000000000000 _edata
+0+402018 g       .bss  0000000000000000 _end
+0+402015 g       .bss  0000000000000000 non_initialVar
+
+
+
+Disassembly of section .text\$mn:
+
+0+401000 <begin>:
+ +[a-f0-9]+:   66 90                   xchg   %ax,%ax
+ +[a-f0-9]+:   cc                      int3   
+ +[a-f0-9]+:   48 8d 05 07 10 00 00    lea    0x1007\(%rip\),%rax        # 402011 <initializedVar>
+ +[a-f0-9]+:   48 3b 05 ef 0f 00 00    cmp    0xfef\(%rip\),%rax        # 402000 <Struct>
+ +[a-f0-9]+:   74 01                   je     401014 <begin\+0x14>
+ +[a-f0-9]+:   cc                      int3   
+ +[a-f0-9]+:   48 8d 05 fa 0f 00 00    lea    0xffa\(%rip\),%rax        # 402015 <non_initialVar>
+ +[a-f0-9]+:   48 3b 05 e6 0f 00 00    cmp    0xfe6\(%rip\),%rax        # 402008 <Struct\+0x8>
+ +[a-f0-9]+:   74 01                   je     401025 <begin\+0x25>
+ +[a-f0-9]+:   cc                      int3   
+ +[a-f0-9]+:   66 ba 80 00             mov    \$0x80,%dx
+ +[a-f0-9]+:   b0 12                   mov    \$0x12,%al
+ +[a-f0-9]+:   ee                      out    %al,\(%dx\)
+ +[a-f0-9]+:   c3                      ret    
+#pass
diff --git a/ld/testsuite/ld-x86-64/pe-x86-64-5.rd b/ld/testsuite/ld-x86-64/pe-x86-64-5.rd
new file mode 100644 (file)
index 0000000..8370665
--- /dev/null
@@ -0,0 +1,19 @@
+
+Symbol table '.symtab' contains 10 entries:
+   Num:    Value          Size Type    Bind   Vis      Ndx Name
+ +[a-f0-9]+: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
+ +[a-f0-9]+: 0000000000402014     0 NOTYPE  GLOBAL DEFAULT    3 non_initdummy
+ +[a-f0-9]+: 0000000000402010     0 NOTYPE  GLOBAL DEFAULT    2 initdummy
+ +[a-f0-9]+: 0000000000401000     0 NOTYPE  GLOBAL DEFAULT    1 begin
+ +[a-f0-9]+: 0000000000402012     0 NOTYPE  GLOBAL DEFAULT    3 __bss_start
+ +[a-f0-9]+: 0000000000402000     0 NOTYPE  GLOBAL DEFAULT    2 Struct
+ +[a-f0-9]+: 0000000000402011     0 NOTYPE  GLOBAL DEFAULT    2 initializedVar
+ +[a-f0-9]+: 0000000000402012     0 NOTYPE  GLOBAL DEFAULT    2 _edata
+ +[a-f0-9]+: 0000000000402018     0 NOTYPE  GLOBAL DEFAULT    3 _end
+ +[a-f0-9]+: 0000000000402015     0 NOTYPE  GLOBAL DEFAULT    3 non_initialVar
+
+Hex dump of section '.data':
+  0x00402000 11204000 00000000 15204000 00000000 . @...... @.....
+  0x00402010 aa55                                .U
+
+#pass
index e81a0b4ae081b4163480d911854d34a07e805d7a..ccfcdfaddfbaa92686429ce3be7b45f9a5bc5d5b 100644 (file)
@@ -63,4 +63,14 @@ run_ld_link_tests [list \
        {{objdump {-dw --sym} pe-x86-64-4.od}} \
        "pe-x86-64-4" \
     ] \
+    [list \
+       "Build pe-x86-64-5" \
+       "-m elf_x86_64 --entry=begin" \
+       "" \
+       "" \
+       {pe-x86-64-5.obj.bz2 } \
+       {{objdump {-dw --sym} pe-x86-64-5.od} \
+        {readelf {-s -x .data} pe-x86-64-5.rd}} \
+       "pe-x86-64-5" \
+    ] \
 ]