diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp --- a/lld/ELF/Arch/PPC64.cpp +++ b/lld/ELF/Arch/PPC64.cpp @@ -681,6 +681,8 @@ case R_PPC64_REL14: case R_PPC64_REL24: return R_PPC64_CALL_PLT; + case R_PPC64_REL24_NOTOC: + return R_PLT_PC; case R_PPC64_REL16_LO: case R_PPC64_REL16_HA: case R_PPC64_REL16_HI: @@ -993,7 +995,8 @@ write32(loc, (read32(loc) & ~mask) | (val & mask)); break; } - case R_PPC64_REL24: { + case R_PPC64_REL24: + case R_PPC64_REL24_NOTOC: { uint32_t mask = 0x03FFFFFC; checkInt(loc, val, 26, rel); checkAlignment(loc, val, 4, rel); @@ -1032,13 +1035,28 @@ bool PPC64::needsThunk(RelExpr expr, RelType type, const InputFile *file, uint64_t branchAddr, const Symbol &s, int64_t a) const { - if (type != R_PPC64_REL14 && type != R_PPC64_REL24) + if (type != R_PPC64_REL14 && type != R_PPC64_REL24 && + type != R_PPC64_REL24_NOTOC) return false; + // FIXME: Remove the fatal error once the call protocol is implemented. + if (type == R_PPC64_REL24_NOTOC && s.isInPlt()) + fatal("unimplemented feature: external function call with the reltype" + " R_PPC64_REL24_NOTOC"); + // If a function is in the Plt it needs to be called with a call-stub. if (s.isInPlt()) return true; + // FIXME: Remove the fatal errors once the call protocols are implemented. + if (type == R_PPC64_REL24_NOTOC && (s.stOther >> 5) > 1) + fatal("unimplemented feature: local function call with the reltype" + " R_PPC64_REL24_NOTOC and the callee needs toc-pointer setup"); + + if (type != R_PPC64_REL24_NOTOC && (s.stOther >> 5) == 1) + fatal("unimplemented feature: local function call with the reltype" + " is not R_PPC64_REL24_NOTOC and the callee tramples the toc"); + // If a symbol is a weak undefined and we are compiling an executable // it doesn't need a range-extending thunk since it can't be called. if (s.isUndefWeak() && !config->shared) @@ -1064,7 +1082,7 @@ int64_t offset = dst - src; if (type == R_PPC64_REL14) return isInt<16>(offset); - if (type == R_PPC64_REL24) + if (type == R_PPC64_REL24 || type == R_PPC64_REL24_NOTOC) return isInt<26>(offset); llvm_unreachable("unsupported relocation type used in branch"); } diff --git a/lld/test/ELF/Inputs/ppc64-callee-global-hidden.s b/lld/test/ELF/Inputs/ppc64-callee-global-hidden.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/Inputs/ppc64-callee-global-hidden.s @@ -0,0 +1,15 @@ +func_extern: + blr + +.hidden callee3_stother0_extern +.globl callee3_stother0_extern +callee3_stother0_extern: + blr + +.hidden callee4_stother1_extern +.globl callee4_stother1_extern +callee4_stother1_extern: + .localentry callee4_stother1_extern, 1 + bl func_extern@notoc + mullw 3, 3, 30 + blr diff --git a/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s b/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s @@ -0,0 +1,105 @@ +# REQUIRES: ppc +# RUN: echo 'SECTIONS { \ +# RUN: .text_local_stother0 0x10010000: { *(.text_local_stother0) } \ +# RUN: .text_local_stother1 0x10020000: { *(.text_local_stother1) } \ +# RUN: .text_extern_stother0 0x10030000: { *(.text_extern_stother0) } \ +# RUN: .text_extern_stother1 0x10040000: { *(.text_extern_stother1) } \ +# RUN: }' > %t.script + +# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t1.o +# RUN: llvm-mc -filetype=obj -triple=powerpc64le %p/Inputs/ppc64-callee-global-hidden.s -o %t2.o +# RUN: ld.lld -T %t.script -shared %t1.o %t2.o -o %t.so +# RUN: llvm-readelf -s %t.so | FileCheck %s --check-prefix=SYMBOL +# RUN: llvm-objdump -d --no-show-raw-insn --mcpu=pwr10 %t.so | FileCheck %s + +# RUN: llvm-mc -filetype=obj -triple=powerpc64 %s -o %t1.o +# RUN: llvm-mc -filetype=obj -triple=powerpc64 %p/Inputs/ppc64-callee-global-hidden.s -o %t2.o +# RUN: ld.lld -T %t.script -shared %t1.o %t2.o -o %t.so +# RUN: llvm-readelf -s %t.so | FileCheck %s --check-prefix=SYMBOL +# RUN: llvm-objdump -d --no-show-raw-insn --mcpu=pwr10 %t.so | FileCheck %s + +# SYMBOL: 1: 0000000010010000 0 NOTYPE LOCAL DEFAULT 5 callee1_stother0_local +# SYMBOL-NEXT: 2: 0000000010020004 0 NOTYPE LOCAL DEFAULT [] 6 callee2_stother1_local +# SYMBOL-NEXT: 3: 0000000010010004 0 NOTYPE LOCAL DEFAULT [] 5 caller1 +# SYMBOL-NEXT: 4: 0000000010020010 0 NOTYPE LOCAL DEFAULT [] 6 caller2 +# SYMBOL-NEXT: 5: 0000000010030000 0 NOTYPE LOCAL DEFAULT [] 7 caller3 +# SYMBOL-NEXT: 6: 0000000010040000 0 NOTYPE LOCAL DEFAULT [] 8 caller4 +# SYMBOL-NEXT: 7: 0000000010020000 0 NOTYPE LOCAL DEFAULT 6 func_local +# SYMBOL-NEXT: 8: 000000001004000c 0 NOTYPE LOCAL DEFAULT 9 func_extern +# SYMBOL-NEXT: 9: 0000000010040010 0 NOTYPE LOCAL HIDDEN 9 callee3_stother0_extern +# SYMBOL-NEXT: 10: 0000000010040014 0 NOTYPE LOCAL HIDDEN [] 9 callee4_stother1_extern + +# CHECK-LABEL: : +# CHECK-NEXT: 10010000: blr + +# CHECK-LABEL: : +# CHECK: 10010004: bl 0x10010000 +# CHECK-NEXT: 10010008: add 3, 3, 30 +# CHECK-NEXT: 1001000c: b 0x10010000 +.section .text_local_stother0, "ax", %progbits +callee1_stother0_local: + blr +caller1: + .localentry caller1, 1 + bl callee1_stother0_local@notoc + add 3, 3, 30 + b callee1_stother0_local@notoc + +# CHECK-LABEL: : +# CHECK-NEXT: 10020000: blr + +# CHECK-LABEL: : +# CHECK-NEXT: 10020004: bl 0x10020000 +# CHECK-NEXT: 10020008: mullw 3, 3, 30 +# CHECK-NEXT: 1002000c: blr + +# CHECK-LABEL: : +# CHECK: 10020010: bl 0x10020004 +# CHECK-NEXT: 10020014: add 3, 3, 30 +# CHECK-NEXT: 10020018: b 0x10020004 +.section .text_local_stother1, "ax", %progbits +func_local: + blr +callee2_stother1_local: + .localentry callee2_stother1_local, 1 + bl func_local@notoc + mullw 3, 3, 30 + blr +caller2: + .localentry caller2, 1 + bl callee2_stother1_local@notoc + add 3, 3, 30 + b callee2_stother1_local@notoc + +# CHECK-LABEL: : +# CHECK: 10030000: bl 0x10040010 +# CHECK-NEXT: 10030004: add 3, 3, 30 +# CHECK-NEXT: 10030008: b 0x10040010 + +# CHECK-LABEL: : +# CHECK: 10040000: bl 0x10040014 +# CHECK-NEXT: 10040004: add 3, 3, 30 +# CHECK-NEXT: 10040008: b 0x10040014 + +# CHECK-LABEL: : +# CHECK-NEXT: 1004000c: blr + +# CHECK-LABEL: : +# CHECK-NEXT: 10040010: blr + +# CHECK-LABEL: : +# CHECK-NEXT: 10040014: bl 0x1004000c +# CHECK-NEXT: 10040018: mullw 3, 3, 30 +.section .text_extern_stother0, "ax", %progbits +caller3: + .localentry caller3, 1 + bl callee3_stother0_extern@notoc + add 3, 3, 30 + b callee3_stother0_extern@notoc + +.section .text_extern_stother1, "ax", %progbits +caller4: + .localentry caller4, 1 + bl callee4_stother1_extern@notoc + add 3, 3, 30 + b callee4_stother1_extern@notoc