Skip to content

Commit

Permalink
[PowerPC][LLD] Change PPC64R2SaveStub to only use non-PC-relative code
Browse files Browse the repository at this point in the history
Currently the PPC64R2SaveStub thunk will produce Power 10 code by default.
This produced an issue when linking older code that made use of the st_other=1
bit but was never meant to be linked or run on Power 10.

This patch makes it so that only the R_PPC64_REL24_NOTOC relocation can produce
Power 10 code.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D129580
  • Loading branch information
stefanp-ibm committed Jul 14, 2022
1 parent 1d7e58c commit c1f3cff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
29 changes: 11 additions & 18 deletions lld/ELF/Thunks.cpp
Expand Up @@ -916,25 +916,18 @@ void PPC64R2SaveStub::writeTo(uint8_t *buf) {
write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b <offset>
} else if (isInt<34>(offset)) {
int nextInstOffset;
if (!config->power10Stubs) {
uint64_t tocOffset = destination.getVA() - getPPC64TocBase();
if (tocOffset >> 16 > 0) {
const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff);
const uint64_t addis = ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff);
write32(buf + 4, addis); // addis r12, r2 , top of offset
write32(buf + 8, addi); // addi r12, r12, bottom of offset
nextInstOffset = 12;
} else {
const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff);
write32(buf + 4, addi); // addi r12, r2, offset
nextInstOffset = 8;
}
} else {
const uint64_t paddi = PADDI_R12_NO_DISP |
(((offset >> 16) & 0x3ffff) << 32) |
(offset & 0xffff);
writePrefixedInstruction(buf + 4, paddi); // paddi r12, 0, func@pcrel, 1
uint64_t tocOffset = destination.getVA() - getPPC64TocBase();
if (tocOffset >> 16 > 0) {
const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff);
const uint64_t addis =
ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff);
write32(buf + 4, addis); // addis r12, r2 , top of offset
write32(buf + 8, addi); // addi r12, r12, bottom of offset
nextInstOffset = 12;
} else {
const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff);
write32(buf + 4, addi); // addi r12, r2, offset
nextInstOffset = 8;
}
write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
write32(buf + nextInstOffset + 4, BCTR); // bctr
Expand Down
15 changes: 3 additions & 12 deletions lld/test/ELF/ppc64-toc-call-to-pcrel-long-jump.s
Expand Up @@ -13,7 +13,7 @@

# RUN: llvm-mc -filetype=obj -triple=powerpc64le %t/asm -o %t.o
# RUN: ld.lld -T %t/lts %t.o -o %t_le --no-power10-stubs
# RUN: llvm-objdump --no-show-raw-insn -d %t_le | FileCheck %s --check-prefix=NoP10
# RUN: llvm-objdump --no-show-raw-insn -d %t_le | FileCheck %s
# RUN: llvm-readelf -s %t_le | FileCheck %s --check-prefix=SYM

# SYM: Symbol table '.symtab' contains 9 entries:
Expand Down Expand Up @@ -74,20 +74,11 @@ caller_close:
# CHECK-NEXT: blr
# CHECK-LABEL: <__toc_save_callee>:
# CHECK: std 2, 24(1)
# CHECK-NEXT: paddi 12, 0, -268501028, 1
# CHECK-NEXT: addis 12, 2, -4098
# CHECK-NEXT: addi 12, 12, 32704
# CHECK-NEXT: mtctr 12
# CHECK-NEXT: bctr

# NoP10-LABEL: <caller>:
# NoP10: bl 0x20020020
# NoP10-NEXT: ld 2, 24(1)
# NoP10-NEXT: blr
# NoP10-LABEL: <__toc_save_callee>:
# NoP10-NEXT: std 2, 24(1)
# NoP10-NEXT: addis 12, 2, -4098
# NoP10-NEXT: addi 12, 12, 32704
# NoP10-NEXT: mtctr 12
# NoP10-NEXT: bctr
.section .text_caller, "ax", %progbits
.Lfunc_toc2:
.quad .TOC.-.Lfunc_gep2
Expand Down

0 comments on commit c1f3cff

Please sign in to comment.