Skip to content

Commit 195029f

Browse files
committed
[lld] Add support for relocations in x86_64 objects on Arm64EC targets.
Since EC targets may combine various object types, we need to pick relocation format based on chunk type instead of global config.
1 parent 1ebe738 commit 195029f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

lld/COFF/Chunks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void SectionChunk::applyRelocation(uint8_t *off,
437437
// Compute the RVA of the relocation for relative relocations.
438438
uint64_t p = rva + rel.VirtualAddress;
439439
uint64_t imageBase = file->ctx.config.imageBase;
440-
switch (file->ctx.config.machine) {
440+
switch (getMachine()) {
441441
case AMD64:
442442
applyRelX64(off, rel.Type, os, s, p, imageBase);
443443
break;
@@ -551,7 +551,7 @@ static uint8_t getBaserelType(const coff_relocation &rel,
551551
// Only called when base relocation is enabled.
552552
void SectionChunk::getBaserels(std::vector<Baserel> *res) {
553553
for (const coff_relocation &rel : getRelocs()) {
554-
uint8_t ty = getBaserelType(rel, file->ctx.config.machine);
554+
uint8_t ty = getBaserelType(rel, getMachine());
555555
if (ty == IMAGE_REL_BASED_ABSOLUTE)
556556
continue;
557557
Symbol *target = file->getSymbol(rel.SymbolTableIndex);

lld/COFF/Chunks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class SectionChunk final : public Chunk {
219219
ArrayRef<uint8_t> getContents() const;
220220
void writeTo(uint8_t *buf) const;
221221

222+
MachineTypes getMachine() const { return file->getMachineType(); }
223+
222224
// Defend against unsorted relocations. This may be overly conservative.
223225
void sortRelocations();
224226

lld/test/COFF/arm64ec-reloc.test

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
REQUIRES: aarch64, x86
2+
RUN: split-file %s %t.dir && cd %t.dir
3+
4+
Link a mix of ARM64EC and x86_64 data and check that relocations work.
5+
6+
RUN: llvm-mc -filetype=obj -triple=arm64ec-windows arm64ec-data-sym.s -o arm64ec-data-sym.obj
7+
RUN: llvm-mc -filetype=obj -triple=x86_64-windows x86_64-data-sym.s -o x86_64-data-sym.obj
8+
RUN: lld-link -out:test.dll -machine:arm64ec arm64ec-data-sym.obj x86_64-data-sym.obj -dll -noentry
9+
10+
RUN: llvm-readobj --hex-dump=.data test.dll | FileCheck -check-prefix=ARM64EC-DATA %s
11+
ARM64EC-DATA: 0x180001000 00100080 01000000 08100080 01000000
12+
13+
RUN: llvm-readobj --coff-basereloc test.dll | FileCheck -check-prefix=RELOCS %s
14+
RELOCS: BaseReloc [
15+
RELOCS-NEXT: Entry {
16+
RELOCS-NEXT: Type: DIR64
17+
RELOCS-NEXT: Address: 0x1000
18+
RELOCS-NEXT: }
19+
RELOCS-NEXT: Entry {
20+
RELOCS-NEXT: Type: DIR64
21+
RELOCS-NEXT: Address: 0x1008
22+
RELOCS-NEXT: }
23+
RELOCS-NEXT: ]
24+
25+
#--- arm64ec-data-sym.s
26+
.data
27+
.globl arm64ec_data_sym
28+
.p2align 2, 0x0
29+
arm64ec_data_sym:
30+
.xword arm64ec_data_sym
31+
32+
#--- x86_64-data-sym.s
33+
.data
34+
.globl x86_64_data_sym
35+
.p2align 2, 0x0
36+
x86_64_data_sym:
37+
.quad x86_64_data_sym

0 commit comments

Comments
 (0)