Skip to content

Commit 90a08fb

Browse files
authored
[LLD][COFF] Update nodefaultlibs after updating search paths (#128813)
Fix #107346
1 parent d921bf2 commit 90a08fb

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lld/COFF/Driver.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,13 @@ void LinkerDriver::addWinSysRootLibSearchPaths() {
786786
path))
787787
searchPaths.push_back(saver().save(path));
788788
}
789+
790+
// Libraries specified by `/nodefaultlib:` may not be found in incomplete
791+
// search paths before lld infers a machine type from input files.
792+
std::set<std::string> noDefaultLibs;
793+
for (const std::string &path : ctx.config.noDefaultLibs)
794+
noDefaultLibs.insert(findLib(path).lower());
795+
ctx.config.noDefaultLibs = noDefaultLibs;
789796
}
790797

791798
// Parses LIB environment which contains a list of search paths.

lld/test/COFF/Inputs/defaultlib.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--- !COFF
2+
header:
3+
Machine: IMAGE_FILE_MACHINE_AMD64
4+
Characteristics: [ ]
5+
sections:
6+
- Name: .drectve
7+
Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
8+
Alignment: 1
9+
SectionData: 202F44454641554C544C49423A64656661756C742E6C6962
10+
SizeOfRawData: 24
11+
symbols:
12+
- Name: .drectve
13+
Value: 0
14+
SectionNumber: 1
15+
SimpleType: IMAGE_SYM_TYPE_NULL
16+
ComplexType: IMAGE_SYM_DTYPE_NULL
17+
StorageClass: IMAGE_SYM_CLASS_STATIC
18+
SectionDefinition:
19+
Length: 24
20+
NumberOfRelocations: 0
21+
NumberOfLinenumbers: 0
22+
CheckSum: 2526009418
23+
Number: 1
24+
...

lld/test/COFF/nodefaultlib.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ MSVC stamps uppercase references in OBJ directives, thus ensure that passing low
3636

3737
UPPERCASE-NOT: OLDNAMES
3838
UPPERCASE-NOT: LIBCMT
39+
40+
# RUN: yaml2obj -o %T/defaultlib.obj %p/Inputs/defaultlib.yaml
41+
# RUN: mkdir -p %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64
42+
# RUN: cp %p/Inputs/ret42.lib %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64/default.lib
43+
44+
# RUN: lld-link /winsysroot:%t.dir/sysroot /out:%t.exe /entry:main /subsystem:console %T/defaultlib.obj
45+
# RUN: not lld-link /winsysroot:%t.dir/sysroot /out:%t.exe /entry:main /subsystem:console /nodefaultlib:default.lib %T/defaultlib.obj 2>&1 | FileCheck -check-prefix=CHECK4 %s
46+
47+
CHECK4: error: <root>: undefined symbol: main

0 commit comments

Comments
 (0)