Skip to content

Commit c57810a

Browse files
authored
[LLD][COFF] Sort base relocations (#121699)
This change ensures that base relocations are sorted in the output, aligning with MSVC linker behavior. While input files typically provide sorted relocations, this update guarantees correct sorting even if the input relocations are unordered.
1 parent d723587 commit c57810a

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lld/COFF/Writer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,8 @@ void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
25602560
const uint32_t mask = ~uint32_t(pageSize - 1);
25612561
uint32_t page = v[0].rva & mask;
25622562
size_t i = 0, j = 1;
2563+
llvm::sort(v,
2564+
[](const Baserel &x, const Baserel &y) { return x.rva < y.rva; });
25632565
for (size_t e = v.size(); j < e; ++j) {
25642566
uint32_t p = v[j].rva & mask;
25652567
if (p == page)

lld/test/COFF/baserel-sort.yaml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Verify that lld-link outputs sorted base relocations, even if the input file has unsorted entries.
2+
3+
# RUN: yaml2obj %s -o %t.obj
4+
# RUN: lld-link -dll -entry:sym -machine:amd64 -out:%t.dll %t.obj
5+
# RUN: llvm-readobj --coff-basereloc %t.dll | FileCheck %s
6+
7+
# CHECK: BaseReloc [
8+
# CHECK-NEXT: Entry {
9+
# CHECK-NEXT: Type: DIR64
10+
# CHECK-NEXT: Address: 0x2000
11+
# CHECK-NEXT: }
12+
# CHECK-NEXT: Entry {
13+
# CHECK-NEXT: Type: DIR64
14+
# CHECK-NEXT: Address: 0x2008
15+
# CHECK-NEXT: }
16+
# CHECK-NEXT: Entry {
17+
# CHECK-NEXT: Type: DIR64
18+
# CHECK-NEXT: Address: 0x2010
19+
# CHECK-NEXT: }
20+
21+
--- !COFF
22+
header:
23+
Machine: IMAGE_FILE_MACHINE_AMD64
24+
Characteristics: [ ]
25+
sections:
26+
- Name: .text
27+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
28+
Alignment: 4
29+
SectionData: C3
30+
SizeOfRawData: 1
31+
- Name: .test
32+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
33+
Alignment: 1
34+
SectionData: '000000000000000000000000000000000000000000000000'
35+
SizeOfRawData: 24
36+
Relocations:
37+
- VirtualAddress: 16
38+
SymbolName: sym
39+
Type: IMAGE_REL_AMD64_ADDR64
40+
- VirtualAddress: 0
41+
SymbolName: sym
42+
Type: IMAGE_REL_AMD64_ADDR64
43+
- VirtualAddress: 8
44+
SymbolName: sym
45+
Type: IMAGE_REL_AMD64_ADDR64
46+
symbols:
47+
- Name: .text
48+
Value: 0
49+
SectionNumber: 1
50+
SimpleType: IMAGE_SYM_TYPE_NULL
51+
ComplexType: IMAGE_SYM_DTYPE_NULL
52+
StorageClass: IMAGE_SYM_CLASS_STATIC
53+
SectionDefinition:
54+
Length: 1
55+
NumberOfRelocations: 0
56+
NumberOfLinenumbers: 0
57+
CheckSum: 40735498
58+
Number: 1
59+
- Name: .test
60+
Value: 0
61+
SectionNumber: 2
62+
SimpleType: IMAGE_SYM_TYPE_NULL
63+
ComplexType: IMAGE_SYM_DTYPE_NULL
64+
StorageClass: IMAGE_SYM_CLASS_STATIC
65+
SectionDefinition:
66+
Length: 24
67+
NumberOfRelocations: 3
68+
NumberOfLinenumbers: 0
69+
CheckSum: 0
70+
Number: 2
71+
- Name: sym
72+
Value: 0
73+
SectionNumber: 1
74+
SimpleType: IMAGE_SYM_TYPE_NULL
75+
ComplexType: IMAGE_SYM_DTYPE_NULL
76+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
77+
...

0 commit comments

Comments
 (0)