[binutils-gdb] x86/COFF: support section-index relocations in insn operands

Jan Beulich jbeulich@sourceware.org
Mon Dec 2 08:40:00 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=64aafe3e86cb5575173344bb66a242d15e2271ee

commit 64aafe3e86cb5575173344bb66a242d15e2271ee
Author: Jan Beulich <jbeulich@suse.com>
Date:   Mon Dec 2 09:38:15 2024 +0100

    x86/COFF: support section-index relocations in insn operands
    
    On the grounds of the principle put down near the bottom of [1], along
    with image and section relative operations, let's also support as insn
    operands what .secidx is for on the data side (of course like elsewhere
    the reloc operator can then also be used for data generation, albeit a
    small tweak to x86_cons() is needed for this to work).
    
    [1] https://sourceware.org/pipermail/binutils/2024-November/137617.html

Diff:
---
 gas/config/tc-i386.c                   |  9 ++++++++-
 gas/testsuite/gas/i386/secidx.d        | 11 ++++++++++-
 gas/testsuite/gas/i386/secidx.s        |  6 ++++++
 gas/testsuite/gas/i386/x86-64-secidx.d | 25 +++++++++++++++++++++++++
 gas/testsuite/gas/i386/x86-64-secidx.s | 13 +++++++++++++
 gas/testsuite/gas/i386/x86-64.exp      |  1 +
 6 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index c1c0d0fdd91..6720bc8e38b 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -1411,6 +1411,9 @@ gotrel[] =
     { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
 				       BFD_RELOC_32_SECREL },
     OPERAND_TYPE_IMM32_32S_DISP32, false },
+    { STRING_COMMA_LEN ("SECIDX16"), { BFD_RELOC_16_SECIDX,
+				       BFD_RELOC_16_SECIDX },
+    { .bitfield = { .imm16 = 1, .disp16 = 1 } }, false },
     { STRING_COMMA_LEN ("RVA"), { BFD_RELOC_RVA,
 				       BFD_RELOC_RVA },
     OPERAND_TYPE_IMM32_32S_DISP32, false },
@@ -13133,7 +13136,11 @@ x86_cons (expressionS *exp, int size)
   expr_mode = expr_operator_none;
 
 #if defined (OBJ_ELF) || defined (TE_PE)
-  if (size == 4 || (object_64bit && size == 8))
+  if (size == 4
+# ifdef TE_PE
+      || (size == 2)
+# endif
+      || (object_64bit && size == 8))
     {
       /* Handle @GOTOFF and the like in an expression.  */
       char *save;
diff --git a/gas/testsuite/gas/i386/secidx.d b/gas/testsuite/gas/i386/secidx.d
index 2db427bb908..09ee013d23f 100644
--- a/gas/testsuite/gas/i386/secidx.d
+++ b/gas/testsuite/gas/i386/secidx.d
@@ -3,6 +3,10 @@
 
 .*: +file format pe-i386
 
+RELOCATION RECORDS FOR \[\.text\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+22 secidx            \.text
+
 RELOCATION RECORDS FOR \[\.data\]:
 OFFSET[ 	]+TYPE[ 	]+VALUE
 0+24 secidx            \.text
@@ -22,9 +26,14 @@ OFFSET[ 	]+TYPE[ 	]+VALUE
 0+72 secidx            ext36
 0+75 secidx            ext3f
 
+RELOCATION RECORDS FOR \[\.rdata\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+24 secidx            \.rdata
+
 Contents of section \.text:
  0000 3e3e3e3e 3c3c3c3c 3e3e3e3e 3e3c3c3c  >>>><<<<>>>>><<<
  0010 3e3e3e3e 3e3e3c3c 3e3e3e3e 3e3e3e3c  >>>>>><<>>>>>>><
+ 0020 66b80000 .*
 Contents of section \.data:
  0000 3e3e3e3e 3c3c3c3c 3e3e3e3e 3e3c3c3c  >>>><<<<>>>>><<<
  0010 3e3e3e3e 3e3e3c3c 3e3e3e3e 3e3e3e3c  >>>>>><<>>>>>>><
@@ -37,4 +46,4 @@ Contents of section \.data:
 Contents of section \.rdata:
  0000 3e3e3e3e 3c3c3c3c 3e3e3e3e 3e3c3c3c  >>>><<<<>>>>><<<
  0010 3e3e3e3e 3e3e3c3c 3e3e3e3e 3e3e3e3c  >>>>>><<>>>>>>><
- 0020 3e3e3e3e 00000000 00000000 00000000  >>>>............
+ 0020 3e3e3e3e 24003c3c 3c3c0000 00000000  >>>>..<<<<......
diff --git a/gas/testsuite/gas/i386/secidx.s b/gas/testsuite/gas/i386/secidx.s
index 7ac1d2c7036..2cc52a9844c 100644
--- a/gas/testsuite/gas/i386/secidx.s
+++ b/gas/testsuite/gas/i386/secidx.s
@@ -9,6 +9,8 @@ pre16:	.ascii "<<"
 	.ascii ">>>>>>>"
 pre1f:	.ascii "<"
 
+	mov	$.text@secidx16, %ax
+
 .data
 
 	.ascii ">>>>"
@@ -76,4 +78,8 @@ nex16:	.ascii "<<"
 nex1f:	.ascii "<"
 	.ascii ">>>>"
 
+	.word	.@secidx16
+
+	.ascii "<<<<"
+
 	.p2align 4,0
diff --git a/gas/testsuite/gas/i386/x86-64-secidx.d b/gas/testsuite/gas/i386/x86-64-secidx.d
new file mode 100644
index 00000000000..8affc0a4f51
--- /dev/null
+++ b/gas/testsuite/gas/i386/x86-64-secidx.d
@@ -0,0 +1,25 @@
+#objdump: -rs
+#name: x86-64 imgrel (RVA) reloc
+
+.*: +file format pe-x86-64
+
+RELOCATION RECORDS FOR \[\.text\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+02 IMAGE_REL_AMD64_SECTION  \.text
+0+07 IMAGE_REL_AMD64_SECTION  \.data
+0+0c IMAGE_REL_AMD64_SECTION  \.text
+0+11 IMAGE_REL_AMD64_SECTION  Xtrn
+
+RELOCATION RECORDS FOR \[\.data\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+00 IMAGE_REL_AMD64_SECTION  \.text
+0+02 IMAGE_REL_AMD64_SECTION  \.data
+0+04 IMAGE_REL_AMD64_SECTION  \.data
+0+06 IMAGE_REL_AMD64_SECTION  Xtrn
+
+Contents of section \.text:
+ 0000 66b90000 6681c100 006681e9 09006681  .*
+ 0010 f10000.*
+
+Contents of section \.data:
+ 0000 00000000 04000000 00000000 00000000  .*
diff --git a/gas/testsuite/gas/i386/x86-64-secidx.s b/gas/testsuite/gas/i386/x86-64-secidx.s
new file mode 100644
index 00000000000..d2d8ef0bf6a
--- /dev/null
+++ b/gas/testsuite/gas/i386/x86-64-secidx.s
@@ -0,0 +1,13 @@
+	.text
+Text:
+	mov	$Text@secidx16, %cx
+	add	$Data@secidx16, %cx
+	sub	$.@secidx16, %cx
+	xor	$Xtrn@secidx16, %cx
+
+	.data
+Data:
+	.word	Text@secidx16
+	.word	Data@secidx16
+	.word	.@secidx16
+	.word	Xtrn@secidx16
diff --git a/gas/testsuite/gas/i386/x86-64.exp b/gas/testsuite/gas/i386/x86-64.exp
index cbeef6ea223..e59daec5514 100644
--- a/gas/testsuite/gas/i386/x86-64.exp
+++ b/gas/testsuite/gas/i386/x86-64.exp
@@ -60,6 +60,7 @@ if { ![istarget "*-*-*cygwin*"]
 } else {
     run_dump_test "x86-64-w64-pcrel"
     run_dump_test "x86-64-imgrel"
+    run_dump_test "x86-64-secidx"
 }
 run_list_test "pcrel64" "-al"
 run_dump_test "x86-64-rip"


More information about the Binutils-cvs mailing list