]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
i386: Don't allow GOTOFF relocation against IFUNC symbol for PIC
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 21 Jul 2022 17:35:58 +0000 (10:35 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 22 Jul 2022 21:46:22 +0000 (14:46 -0700)
We can't use the PLT entry as the function address for PIC since the PIC
register may not be set up properly for indirect call.

bfd/

PR ld/27998
* elf32-i386.c (elf_i386_relocate_section): Don't allow GOTOFF
relocation against IFUNC symbol for PIC.

ld/

PR ld/27998
* testsuite/ld-i386/pr27998a.d: Replace -shared with -e bar.
* testsuite/ld-i386/pr27998b.d: Expect a linker error.
* testsuite/ld-ifunc/ifunc-2-i386-now.d: Updated.
* testsuite/ld-ifunc/ifunc-2-local-i386-now.d: Likewise.
* testsuite/ld-ifunc/ifunc-2-i386.s: Replace @GOTOFF with @GOT.
* testsuite/ld-ifunc/ifunc-2-local-i386.s: Likewise.

(cherry picked from commit 8f29211c3f0a6335c17e0a90396c146facf6dba4)

bfd/elf32-i386.c
ld/testsuite/ld-i386/pr27998a.d
ld/testsuite/ld-i386/pr27998b.d
ld/testsuite/ld-ifunc/ifunc-2-i386-now.d
ld/testsuite/ld-ifunc/ifunc-2-i386.s
ld/testsuite/ld-ifunc/ifunc-2-local-i386-now.d
ld/testsuite/ld-ifunc/ifunc-2-local-i386.s

index 94a23bf30a0b0ca1c5bdba73167145b025fe3637..6666871c40e7f9b60416119c412e34445eef6332 100644 (file)
@@ -2461,6 +2461,11 @@ elf_i386_relocate_section (bfd *output_bfd,
              goto do_relocation;
 
            case R_386_GOTOFF:
+             /* NB: We can't use the PLT entry as the function address
+                for PIC since the PIC register may not be set up
+                properly for indirect call. */
+             if (bfd_link_pic (info))
+               goto bad_ifunc_reloc;
              relocation -= (gotplt->output_section->vma
                             + gotplt->output_offset);
              goto do_relocation;
index ca3c9205fa6120bac31c7e373ca91f91bd27237a..a8019730ec1fbb985fbd962f7ee84c61221cb743 100644 (file)
@@ -1,5 +1,5 @@
 #as: --32
-#ld: -shared -melf_i386
+#ld: -e bar -melf_i386
 #readelf: -r --wide
 
 Relocation section '.rel.plt' at offset 0x[0-9a-f]+ contains 1 entry:
index ca3c9205fa6120bac31c7e373ca91f91bd27237a..8f81edbe8dfc577bd66d147cbc6dae430a66c2bd 100644 (file)
@@ -1,7 +1,3 @@
 #as: --32
 #ld: -shared -melf_i386
-#readelf: -r --wide
-
-Relocation section '.rel.plt' at offset 0x[0-9a-f]+ contains 1 entry:
- Offset     Info    Type                Sym. Value  Symbol's Name
-[0-9a-f]+ +[0-9a-f]+ +R_386_IRELATIVE +
+#error: relocation R_386_GOTOFF against STT_GNU_IFUNC symbol `foo' isn't supported
index aae24b2809d124614ddfaf59392b425443793225..3a744d19f7e34f7e03cf79540af13b610aa5c890 100644 (file)
@@ -31,6 +31,6 @@ Disassembly of section .text:
  +[a-f0-9]+:   5b                      pop    %ebx
  +[a-f0-9]+:   81 c3 9e 10 00 00       add    \$0x109e,%ebx
  +[a-f0-9]+:   e8 de ff ff ff          call   100 <\*ABS\*@plt>
- +[a-f0-9]+:   8d 83 4c ef ff ff       lea    -0x10b4\(%ebx\),%eax
+ +[a-f0-9]+:   8b 83 0c 00 00 00       mov    0xc\(%ebx\),%eax
  +[a-f0-9]+:   c3                      ret    
 #pass
index e84d6b7b5cc05a74d953d24e925f285c2af9f468..1acf6847e0bde9f86b8cd1ac4c804f20e69108f4 100644 (file)
@@ -16,6 +16,6 @@ bar:
        popl    %ebx
        addl    $_GLOBAL_OFFSET_TABLE_+[.-.L6], %ebx
        call    __GI_foo@PLT
-       leal    __GI_foo@GOTOFF(%ebx), %eax
+       movl    __GI_foo@GOT(%ebx), %eax
        ret
        .size   bar, .-bar
index 86083c12a080e0fe95c889ad4a726d8b3151a066..739058b5ca46f78610c5b06a2eff65375a0e95b8 100644 (file)
@@ -31,6 +31,6 @@ Disassembly of section .text:
  +[a-f0-9]+:   5b                      pop    %ebx
  +[a-f0-9]+:   81 c3 9e 10 00 00       add    \$0x109e,%ebx
  +[a-f0-9]+:   e8 de ff ff ff          call   f0 <\*ABS\*@plt>
- +[a-f0-9]+:   8d 83 4c ef ff ff       lea    -0x10b4\(%ebx\),%eax
+ +[a-f0-9]+:   8b 83 0c 00 00 00       mov    0xc\(%ebx\),%eax
  +[a-f0-9]+:   c3                      ret    
 #pass
index a69e060ddc0bad362e2cd15a65851ccba4918697..54e0e179551b60a83dce84b4b5187bfda14ec0aa 100644 (file)
@@ -13,6 +13,6 @@ bar:
        popl    %ebx
        addl    $_GLOBAL_OFFSET_TABLE_+[.-.L6], %ebx
        call    __GI_foo@PLT
-       leal    __GI_foo@GOTOFF(%ebx), %eax
+       movl    __GI_foo@GOT(%ebx), %eax
        ret
        .size   bar, .-bar