Index: lld/ELF/Arch/PPC64.cpp =================================================================== --- lld/ELF/Arch/PPC64.cpp +++ lld/ELF/Arch/PPC64.cpp @@ -1037,8 +1037,7 @@ // 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"); + return true; // If a function is in the Plt it needs to be called with a call-stub. if (s.isInPlt()) Index: lld/ELF/Thunks.cpp =================================================================== --- lld/ELF/Thunks.cpp +++ lld/ELF/Thunks.cpp @@ -1004,6 +1004,9 @@ assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 || type == R_PPC64_REL24_NOTOC) && "unexpected relocation type for thunk"); + if (type == R_PPC64_REL24_NOTOC && s.isInPlt()) + return make(s); + if (s.isInPlt()) return make(s); Index: lld/test/ELF/Inputs/ppc64-callee-global.s =================================================================== --- /dev/null +++ lld/test/ELF/Inputs/ppc64-callee-global.s @@ -0,0 +1,27 @@ +func_extern: + blr + +.globl callee_global_stother0 +callee_global_stother0: + blr + +.globl callee_global_stother1 +callee_global_stother1: + .localentry callee_global_stother1, 1 + ## nop is not needed after bl for R_PPC64_REL24_NOTOC + bl func_extern@notoc + blr + +.globl callee_global_TOC +callee_global_TOC: +.Lfunc_gep1: + addis 2, 12, .TOC.-.Lfunc_gep1@ha + addi 2, 2, .TOC.-.Lfunc_gep1@l +.Lfunc_lep1: + .localentry callee_global_TOC, .Lfunc_lep1-.Lfunc_gep1 + addis 4, 2, global@toc@ha + lwz 3, global@toc@l(4) + blr +global: + .long 0 + .size global, 4 Index: lld/test/ELF/ppc64-pcrel-call-to-extern.s =================================================================== --- /dev/null +++ lld/test/ELF/ppc64-pcrel-call-to-extern.s @@ -0,0 +1,86 @@ +# REQUIRES: ppc +# RUN: echo 'SECTIONS { \ +# RUN: .text_caller1 0x10010000 : { *(.text_caller1) } \ +# RUN: .text_caller2 0x10020000 : { *(.text_caller2) } \ +# RUN: .text_caller3 0x10030000 : { *(.text_caller3) } \ +# 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.s -o %t2.o +# RUN: ld.lld --shared %t2.o -o %t2.so +# RUN: ld.lld -T %t.script %t1.o %t2.so -o %t +# RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=SYMBOL +# RUN: llvm-objdump -d --no-show-raw-insn --mcpu=pwr10 %t | 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.s -o %t2.o +# RUN: ld.lld --shared %t2.o -o %t2.so +# RUN: ld.lld -T %t.script %t1.o %t2.so -o %t +# RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=SYMBOL +# RUN: llvm-objdump -d --no-show-raw-insn --mcpu=pwr10 %t | FileCheck %s + +## The test is created to check that when a function without TOC access an +## external function, a r12 setup stub is inserted. + +# SYMBOL: Symbol table '.dynsym' contains 4 entries: +# SYMBOL: 1: 0000000000000000 0 NOTYPE GLOBAL DEFAULT [] UND callee_global_TOC +# SYMBOL: 2: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND callee_global_stother0 +# SYMBOL: 3: 0000000000000000 0 NOTYPE GLOBAL DEFAULT [] UND callee_global_stother1 + +# SYMBOL: Symbol table '.symtab' contains 11 entries: +# SYMBOL: 1: 0000000010010000 0 NOTYPE LOCAL DEFAULT [] 6 caller1 +# SYMBOL: 2: 0000000010020000 0 NOTYPE LOCAL DEFAULT [] 7 caller2 +# SYMBOL: 3: 0000000010030000 0 NOTYPE LOCAL DEFAULT [] 8 caller3 +# SYMBOL: 5: 0000000010010008 16 FUNC LOCAL DEFAULT 6 __global_entry_point_setup_callee_global_stother0 +# SYMBOL: 6: 0000000010020008 16 FUNC LOCAL DEFAULT 7 __global_entry_point_setup_callee_global_stother1 +# SYMBOL: 7: 0000000010030008 16 FUNC LOCAL DEFAULT 8 __global_entry_point_setup_callee_global_TOC +# SYMBOL: 8: 0000000000000000 0 NOTYPE GLOBAL DEFAULT [] UND callee_global_TOC +# SYMBOL: 9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND callee_global_stother0 +# SYMBOL: 10: 0000000000000000 0 NOTYPE GLOBAL DEFAULT [] UND callee_global_stother1 + + +# CHECK-LABEL: caller1 +# CHECK: bl 0x10010008 +# CHECK-NEXT: blr + +# CHECK-LABEL: __global_entry_point_setup_callee_global_stother0 +# CHECK: pld 12 +# CHECK-NEXT: mtctr 12 +# CHECK-NEXT: bctr + +# CHECK-LABEL: caller2 +# CHECK: bl 0x10020008 +# CHECK-NEXT: blr + +# CHECK-LABEL: __global_entry_point_setup_callee_global_stother1 +# CHECK: pld 12 +# CHECK-NEXT: mtctr 12 +# CHECK-NEXT: bctr + +# CHECK-LABEL: caller3 +# CHECK: bl 0x10030008 +# CHECK-NEXT: blr + +# CHECK-LABEL: __global_entry_point_setup_callee_global_TOC +# CHECK: pld 12 +# CHECK-NEXT: mtctr 12 +# CHECK-NEXT: bctr + + +.section .text_caller1, "ax", %progbits +caller1: + .localentry caller1, 1 + bl callee_global_stother0@notoc + blr + +.section .text_caller2, "ax", %progbits +caller2: + .localentry caller2, 1 + bl callee_global_stother1@notoc + blr + +.section .text_caller3, "ax", %progbits +caller3: + .localentry caller3, 1 + bl callee_global_TOC@notoc + blr