Skip to content

Commit da97a56

Browse files
int3memfrob
authored andcommitted
[lld-macho][re-land] Support X86_64_RELOC_UNSIGNED
This reverts commit db8559eee4a07c44babcc4618eef3d185cf79cc6.
1 parent 9565bf5 commit da97a56

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

lld/MachO/Arch/X86_64.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ uint64_t X86_64::getImplicitAddend(const uint8_t *loc, uint8_t type) const {
4747
case X86_64_RELOC_SIGNED_4:
4848
case X86_64_RELOC_GOT_LOAD:
4949
return read32le(loc);
50+
case X86_64_RELOC_UNSIGNED:
51+
return read64le(loc);
5052
default:
5153
error("TODO: Unhandled relocation type " + std::to_string(type));
5254
return 0;
@@ -65,6 +67,9 @@ void X86_64::relocateOne(uint8_t *loc, uint8_t type, uint64_t val) const {
6567
// since the RIP has advanced by 4 at this point.
6668
write32le(loc, val - 4);
6769
break;
70+
case X86_64_RELOC_UNSIGNED:
71+
write64le(loc, val);
72+
break;
6873
default:
6974
llvm_unreachable(
7075
"getImplicitAddend should have flagged all unhandled relocation types");

lld/MachO/InputFiles.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ void InputFile::parseRelocations(const section_64 &sec,
173173
fatal("TODO: Scattered relocations not supported");
174174

175175
auto rel = reinterpret_cast<const relocation_info &>(anyRel);
176-
if (!rel.r_pcrel)
177-
fatal("TODO: Only pcrel relocations are supported");
178176

179177
Reloc r;
180178
r.type = rel.r_type;
179+
r.pcrel = rel.r_pcrel;
181180
uint32_t secRelOffset = rel.r_address;
182181
uint64_t rawAddend =
183182
target->getImplicitAddend(buf + sec.offset + secRelOffset, r.type);
@@ -186,6 +185,9 @@ void InputFile::parseRelocations(const section_64 &sec,
186185
r.target = symbols[rel.r_symbolnum];
187186
r.addend = rawAddend;
188187
} else {
188+
if (!rel.r_pcrel)
189+
fatal("TODO: Only pcrel section relocations are supported");
190+
189191
if (rel.r_symbolnum == 0 || rel.r_symbolnum > subsections.size())
190192
fatal("invalid section index in relocation for offset " +
191193
std::to_string(r.offset) + " in section " + sec.sectname +

lld/MachO/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void InputSection::writeTo(uint8_t *buf) {
4343
}
4444

4545
uint64_t val = va + r.addend;
46-
if (1) // TODO: handle non-pcrel relocations
46+
if (r.pcrel)
4747
val -= getVA() + r.offset;
4848
target->relocateOne(buf + r.offset, r.type, val);
4949
}

lld/MachO/InputSection.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class Symbol;
2424

2525
struct Reloc {
2626
uint8_t type;
27-
// Adding this offset to the address of the target symbol or subsection gives
28-
// the destination that this relocation refers to.
29-
uint32_t addend;
27+
bool pcrel;
3028
// The offset from the start of the subsection that this relocation belongs
3129
// to.
3230
uint32_t offset;
31+
// Adding this offset to the address of the target symbol or subsection gives
32+
// the destination that this relocation refers to.
33+
uint64_t addend;
3334
llvm::PointerUnion<Symbol *, InputSection *> target;
3435
};
3536

lld/MachO/Target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ enum {
2929
class TargetInfo {
3030
public:
3131
virtual ~TargetInfo() = default;
32+
3233
virtual uint64_t getImplicitAddend(const uint8_t *loc,
3334
uint8_t type) const = 0;
3435
virtual void relocateOne(uint8_t *loc, uint8_t type, uint64_t val) const = 0;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
3+
# RUN: lld -flavor darwinnew -o %t %t.o
4+
# RUN: llvm-objdump --full-contents %t | FileCheck %s
5+
# CHECK: Contents of section foo:
6+
# CHECK: 2000 08200000 00000000
7+
# CHECK: Contents of section bar:
8+
# CHECK: 2008 11311111 01000000
9+
10+
.globl _main, _foo, _bar
11+
12+
.section __DATA,foo
13+
_foo:
14+
.quad _bar
15+
16+
.section __DATA,bar
17+
_bar:
18+
## The unsigned relocation should support 64-bit addends
19+
.quad _foo + 0x111111111
20+
21+
.text
22+
_main:
23+
mov $0, %rax
24+
ret

0 commit comments

Comments
 (0)