Skip to content

Commit 1737cc7

Browse files
committed
[LLD] [COFF] Don't error out on duplicate absolute symbols with the same value
Both MS link.exe and GNU ld.bfd handle it this way; one can have multiple object files defining the same absolute symbols, as long as it defines it to the same value. But if there are multiple absolute symbols with differing values, it is treated as an error. Differential Revision: https://reviews.llvm.org/D71981
1 parent 2306f43 commit 1737cc7

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

lld/COFF/SymbolTable.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,10 @@ Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
591591
s->isUsedInRegularObj = true;
592592
if (wasInserted || isa<Undefined>(s) || s->isLazy())
593593
replaceSymbol<DefinedAbsolute>(s, n, sym);
594-
else if (!isa<DefinedCOFF>(s))
594+
else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
595+
if (!da->isEqual(sym))
596+
reportDuplicate(s, nullptr);
597+
} else if (!isa<DefinedCOFF>(s))
595598
reportDuplicate(s, nullptr);
596599
return s;
597600
}
@@ -603,7 +606,10 @@ Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
603606
s->isUsedInRegularObj = true;
604607
if (wasInserted || isa<Undefined>(s) || s->isLazy())
605608
replaceSymbol<DefinedAbsolute>(s, n, va);
606-
else if (!isa<DefinedCOFF>(s))
609+
else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
610+
if (!da->isEqual(va))
611+
reportDuplicate(s, nullptr);
612+
} else if (!isa<DefinedCOFF>(s))
607613
reportDuplicate(s, nullptr);
608614
return s;
609615
}

lld/COFF/Symbols.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ class DefinedAbsolute : public Defined {
229229
uint64_t getRVA() { return va - config->imageBase; }
230230
void setVA(uint64_t v) { va = v; }
231231

232+
bool isEqual(COFFSymbolRef s) const {
233+
return va == s.getValue();
234+
}
235+
236+
bool isEqual(uint64_t otherVa) const {
237+
return va == otherVa;
238+
}
239+
232240
// Section index relocations against absolute symbols resolve to
233241
// this 16 bit number, and it is the largest valid section index
234242
// plus one. This variable keeps it.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: x86
2+
// RUN: llvm-mc -triple x86_64-windows-msvc -filetype obj -o %t.obj %s
3+
// RUN: echo -e ".globl myabsolute\nmyabsolute = 0" > %t.dupl.s
4+
// RUN: llvm-mc -triple x86_64-windows-msvc -filetype obj -o %t.dupl.obj %t.dupl.s
5+
// RUN: lld-link /out:%t.exe %t.obj %t.dupl.obj -subsystem:console -entry:entry 2>&1 | FileCheck --allow-empty %s
6+
7+
// CHECK-NOT: error: duplicate symbol: myabsolute
8+
9+
.globl myabsolute
10+
myabsolute = 0
11+
12+
.globl entry
13+
entry:
14+
ret

lld/test/COFF/duplicate-absolute.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: x86
22
// RUN: llvm-mc -triple x86_64-windows-msvc -filetype obj -o %t.obj %s
3-
// RUN: echo -e ".globl myabsolute\nmyabsolute = 0" > %t.dupl.s
3+
// RUN: echo -e ".globl myabsolute\nmyabsolute = 1" > %t.dupl.s
44
// RUN: llvm-mc -triple x86_64-windows-msvc -filetype obj -o %t.dupl.obj %t.dupl.s
55
// RUN: not lld-link /out:%t.exe %t.obj %t.dupl.obj 2>&1 | FileCheck %s
66

0 commit comments

Comments
 (0)