Skip to content

[X86][MC,LLD][NFC] Rename R_X86_64_REX2_GOTPCRELX #116737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/test/Driver/relax.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// RUN: llvm-readobj -r %t | FileCheck --check-prefix=REL %s

// REL: R_X86_64_REX_GOTPCRELX foo
// REL: R_X86_64_REX2_GOTPCRELX foo
// REL: R_X86_64_CODE_4_GOTPCRELX foo

movq foo@GOTPCREL(%rip), %rax
movq foo@GOTPCREL(%rip), %r16
13 changes: 7 additions & 6 deletions lld/ELF/Arch/X86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ RelExpr X86_64::getRelExpr(RelType type, const Symbol &s,
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_REX2_GOTPCRELX:
case R_X86_64_CODE_4_GOTPCRELX:
case R_X86_64_GOTTPOFF:
return R_GOT_PC;
case R_X86_64_GOTOFF64:
Expand Down Expand Up @@ -738,7 +738,7 @@ int64_t X86_64::getImplicitAddend(const uint8_t *buf, RelType type) const {
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_REX2_GOTPCRELX:
case R_X86_64_CODE_4_GOTPCRELX:
case R_X86_64_PC32:
case R_X86_64_GOTTPOFF:
case R_X86_64_PLT32:
Expand Down Expand Up @@ -821,7 +821,7 @@ void X86_64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
break;
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_REX2_GOTPCRELX:
case R_X86_64_CODE_4_GOTPCRELX:
if (rel.expr != R_GOT_PC) {
relaxGot(loc, rel, val);
} else {
Expand Down Expand Up @@ -873,13 +873,13 @@ void X86_64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {

RelExpr X86_64::adjustGotPcExpr(RelType type, int64_t addend,
const uint8_t *loc) const {
// Only R_X86_64_[REX_]|[REX2_]GOTPCRELX can be relaxed. GNU as may emit
// Only R_X86_64_[REX_]|[CODE_4_]GOTPCRELX can be relaxed. GNU as may emit
// GOTPCRELX with addend != -4. Such an instruction does not load the full GOT
// entry, so we cannot relax the relocation. E.g. movl x@GOTPCREL+4(%rip),
// %rax (addend=0) loads the high 32 bits of the GOT entry.
if (!ctx.arg.relax || addend != -4 ||
(type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX &&
type != R_X86_64_REX2_GOTPCRELX))
type != R_X86_64_CODE_4_GOTPCRELX))
return R_GOT_PC;
const uint8_t op = loc[-2];
const uint8_t modRm = loc[-1];
Expand Down Expand Up @@ -1002,7 +1002,8 @@ static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) {
// We are relaxing a rip relative to an absolute, so compensate
// for the old -4 addend.
assert(!rel.sym->file || !rel.sym->file->ctx.arg.isPic);
relaxGotNoPic(loc, val + 4, op, modRm, rel.type == R_X86_64_REX2_GOTPCRELX);
relaxGotNoPic(loc, val + 4, op, modRm,
rel.type == R_X86_64_CODE_4_GOTPCRELX);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/x86-64-gotpc-no-relax-err.s
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# CHECK-NEXT: error: {{.*}}:(.text+0x9): relocation R_X86_64_REX_GOTPCRELX out of range: 2147483659 is not in [-2147483648, 2147483647]; references '__stop_data'
# CHECK-NEXT: >>> defined in <internal>
# CHECK-EMPTY:
# CHECK-NEXT: error: {{.*}}:(.text+0x11): relocation R_X86_64_REX2_GOTPCRELX out of range: 2147483651 is not in [-2147483648, 2147483647]; references '__stop_data'
# CHECK-NEXT: error: {{.*}}:(.text+0x11): relocation R_X86_64_CODE_4_GOTPCRELX out of range: 2147483651 is not in [-2147483648, 2147483647]; references '__stop_data'
# CHECK-NEXT: >>> defined in <internal>

#--- a.s
Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/x86-64-gotpc-relax-nopic.s
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ _start:
xorq bar@GOTPCREL(%rip), %r8
testq %r15, bar@GOTPCREL(%rip)

## R_X86_64_REX2_GOTPCRELX
## R_X86_64_CODE_4_GOTPCRELX
adcq bar@GOTPCREL(%rip), %r16
addq bar@GOTPCREL(%rip), %r17
andq bar@GOTPCREL(%rip), %r18
Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/x86-64-gotpc-relax.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# REQUIRES: x86
## Test R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX/R_X86_64_REX2_GOTPCRELX GOT optimization.
## Test R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX/R_X86_64_CODE_4_GOTPCRELX GOT optimization.

# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
# RUN: ld.lld %t.o -o %t1 --no-apply-dynamic-relocs
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ ELF_RELOC(R_X86_64_TLSDESC, 36)
ELF_RELOC(R_X86_64_IRELATIVE, 37)
ELF_RELOC(R_X86_64_GOTPCRELX, 41)
ELF_RELOC(R_X86_64_REX_GOTPCRELX, 42)
ELF_RELOC(R_X86_64_REX2_GOTPCRELX, 43)
ELF_RELOC(R_X86_64_CODE_4_GOTPCRELX, 43)
2 changes: 1 addition & 1 deletion llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {

static cl::opt<bool> X86RelaxRelocations(
"x86-relax-relocations",
cl::desc("Emit GOTPCRELX/REX_GOTPCRELX/REX2_GOTPCRELX instead of "
cl::desc("Emit GOTPCRELX/REX_GOTPCRELX/CODE_4_GOTPCRELX instead of "
"GOTPCREL on x86-64 ELF"),
cl::init(true));
MCBINDOPT(X86RelaxRelocations);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
case MCSymbolRefExpr::VK_GOTPCREL:
checkIs32(Ctx, Loc, Type);
// Older versions of ld.bfd/ld.gold/lld
// do not support GOTPCRELX/REX_GOTPCRELX/REX2_GOTPCRELX,
// do not support GOTPCRELX/REX_GOTPCRELX/CODE_4_GOTPCRELX,
// and we want to keep back-compatibility.
if (!Ctx.getTargetOptions()->X86RelaxRelocations)
return ELF::R_X86_64_GOTPCREL;
Expand All @@ -221,7 +221,7 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
return ELF::R_X86_64_REX_GOTPCRELX;
case X86::reloc_riprel_4byte_relax_rex2:
case X86::reloc_riprel_4byte_movq_load_rex2:
return ELF::R_X86_64_REX2_GOTPCRELX;
return ELF::R_X86_64_CODE_4_GOTPCRELX;
}
llvm_unreachable("unexpected relocation type!");
case MCSymbolRefExpr::VK_GOTPCREL_NORELAX:
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
case X86::MOV64rm:
// movq loads is a subset of reloc_riprel_4byte_relax_rex/rex2. It is a
// special case because COFF and Mach-O don't support ELF's more
// flexible R_X86_64_REX_GOTPCRELX/R_X86_64_REX2_GOTPCRELX relaxation.
// flexible R_X86_64_REX_GOTPCRELX/R_X86_64_CODE_4_GOTPCRELX relaxation.
return Kind == REX2 ? X86::reloc_riprel_4byte_movq_load_rex2
: X86::reloc_riprel_4byte_movq_load;
case X86::ADC32rm:
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/MC/ELF/relocation-alias.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ movabsq $memcpy+2, %rax
# CHECK: movq (%rip), %rax
# CHECK-NEXT: R_X86_64_REX_GOTPCRELX abs-0x4
# CHECK: movq (%rip), %r16
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX abs-0x4
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX abs-0x4
movq abs@GOTPCREL(%rip), %rax
movq abs@GOTPCREL(%rip), %r16
abs = 42
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/MC/X86/gotpcrelx.s
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
# CHECK-NEXT: R_X86_64_REX_GOTPCRELX sbb
# CHECK-NEXT: R_X86_64_REX_GOTPCRELX sub
# CHECK-NEXT: R_X86_64_REX_GOTPCRELX xor
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX mov
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX test
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX adc
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX add
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX and
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX cmp
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX or
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX sbb
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX sub
# CHECK-NEXT: R_X86_64_REX2_GOTPCRELX xor
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX mov
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX test
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX adc
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX add
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX and
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX cmp
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX or
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX sbb
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX sub
# CHECK-NEXT: R_X86_64_CODE_4_GOTPCRELX xor
# CHECK-NEXT: }

# NORELAX-NEXT: R_X86_64_GOTPCREL mov
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/MC/X86/reloc-directive-elf-64.s
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# PRINT-NEXT: .reloc 0, R_X86_64_64, .data+2
# PRINT-NEXT: .reloc 0, R_X86_64_GOTPCRELX, foo+3
# PRINT-NEXT: .reloc 0, R_X86_64_REX_GOTPCRELX, 5
# PRINT-NEXT: .reloc 0, R_X86_64_REX2_GOTPCRELX, 7
# PRINT-NEXT: .reloc 0, R_X86_64_CODE_4_GOTPCRELX, 7
# PRINT: .reloc 0, BFD_RELOC_NONE, 9
# PRINT-NEXT: .reloc 0, BFD_RELOC_8, 9
# PRINT-NEXT: .reloc 0, BFD_RELOC_16, 9
Expand All @@ -22,7 +22,7 @@
# CHECK-NEXT: 0x0 R_X86_64_64 .data 0x2
# CHECK-NEXT: 0x0 R_X86_64_GOTPCRELX foo 0x3
# CHECK-NEXT: 0x0 R_X86_64_REX_GOTPCRELX - 0x5
# CHECK-NEXT: 0x0 R_X86_64_REX2_GOTPCRELX - 0x7
# CHECK-NEXT: 0x0 R_X86_64_CODE_4_GOTPCRELX - 0x7
# CHECK-NEXT: 0x0 R_X86_64_NONE - 0x9
# CHECK-NEXT: 0x0 R_X86_64_8 - 0x9
# CHECK-NEXT: 0x0 R_X86_64_16 - 0x9
Expand All @@ -39,7 +39,7 @@
.reloc 0, R_X86_64_64, .data+2
.reloc 0, R_X86_64_GOTPCRELX, foo+3
.reloc 0, R_X86_64_REX_GOTPCRELX, 5
.reloc 0, R_X86_64_REX2_GOTPCRELX, 7
.reloc 0, R_X86_64_CODE_4_GOTPCRELX, 7

.reloc 0, BFD_RELOC_NONE, 9
.reloc 0, BFD_RELOC_8, 9
Expand Down
Loading