Skip to content

[X86][MC] Support R_X86_64_CODE_4_GOTPC32_TLSDESC #116908

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 3 commits into from
Nov 22, 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
1 change: 1 addition & 0 deletions llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ ELF_RELOC(R_X86_64_GOTPCRELX, 41)
ELF_RELOC(R_X86_64_REX_GOTPCRELX, 42)
ELF_RELOC(R_X86_64_CODE_4_GOTPCRELX, 43)
ELF_RELOC(R_X86_64_CODE_4_GOTTPOFF, 44)
ELF_RELOC(R_X86_64_CODE_4_GOTPC32_TLSDESC, 45)
4 changes: 3 additions & 1 deletion llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
case MCSymbolRefExpr::VK_TLSCALL:
return ELF::R_X86_64_TLSDESC_CALL;
case MCSymbolRefExpr::VK_TLSDESC:
return ELF::R_X86_64_GOTPC32_TLSDESC;
return ((unsigned)Kind == X86::reloc_riprel_4byte_relax_rex2)
? ELF::R_X86_64_CODE_4_GOTPC32_TLSDESC
: ELF::R_X86_64_GOTPC32_TLSDESC;
case MCSymbolRefExpr::VK_TLSGD:
checkIs32(Ctx, Loc, Type);
return ELF::R_X86_64_TLSGD;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
case X86::SBB64rm:
case X86::SUB64rm:
case X86::XOR64rm:
case X86::LEA64r:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need LEA64r before REX2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I don't understand. We need to check LEA64r with REX2 prefix and add R_X86_64_CODE_4_GOTPC32_TLSDESC relocation for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question is: Why we don't need to check LEA64r when REX prefix was added? Is LEA64r special to REX2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not special to REX2. Here we need to distinguish REX2 prefix and others and set corresponding relocation type (R_X86_64_CODE_4_GOTPC32_TLSDESC) if it's REX2 prefix. For REX prefix, we didn't have to distinguish it and others in the past.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

return Kind == REX2 ? X86::reloc_riprel_4byte_relax_rex2
: Kind == REX ? X86::reloc_riprel_4byte_relax_rex
: X86::reloc_riprel_4byte_relax;
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/MC/X86/tlsdesc-64.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@
leaq a@tlsdesc(%rip), %rax
call *a@tlscall(%rax)
addq %fs:0, %rax

# PRINT: leaq a@tlsdesc(%rip), %r16
# PRINT-NEXT: callq *a@tlscall(%r16)

# CHECK: 12: leaq (%rip), %r16 # 0x1a <{{.*}}>
# CHECK-NEXT: 0000000000000016: R_X86_64_CODE_4_GOTPC32_TLSDESC a-0x4
# CHECK-NEXT: 1a: callq *(%r16)
# CHECK-NEXT: 000000000000001a: R_X86_64_TLSDESC_CALL a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is R_X86_64_TLSDESC_CALL expected? Or we need to check R_X86_64_CODE_4_TLSDESC_CALL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. R_X86_64_TLSDESC_CALL is expected. There is no R_X86_64_CODE_4_TLSDESC_CALL type. As I understand, we don't need to rewrite/change the instruction so we don't need to know how many bytes the instruction starts before relocation offset.


leaq a@tlsdesc(%rip), %r16
call *a@tlscall(%r16)
addq %fs:0, %r16
Loading