Skip to content

[LLD][COFF] Update nodefaultlibs after updating search paths #128813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,13 @@ void LinkerDriver::addWinSysRootLibSearchPaths() {
path))
searchPaths.push_back(saver().save(path));
}

// Libraries specified by `/nodefaultlib:` may not be found in incomplete
// search paths before lld infers a machine type from input files.
std::set<std::string> noDefaultLibs;
for (const std::string &path : ctx.config.noDefaultLibs)
noDefaultLibs.insert(findLib(path).lower());
ctx.config.noDefaultLibs = noDefaultLibs;
}

// Parses LIB environment which contains a list of search paths.
Expand Down
24 changes: 24 additions & 0 deletions lld/test/COFF/Inputs/defaultlib.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .drectve
Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
Alignment: 1
SectionData: 202F44454641554C544C49423A64656661756C742E6C6962
SizeOfRawData: 24
symbols:
- Name: .drectve
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 24
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 2526009418
Number: 1
...
9 changes: 9 additions & 0 deletions lld/test/COFF/nodefaultlib.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ MSVC stamps uppercase references in OBJ directives, thus ensure that passing low

UPPERCASE-NOT: OLDNAMES
UPPERCASE-NOT: LIBCMT

# RUN: yaml2obj -o %T/defaultlib.obj %p/Inputs/defaultlib.yaml
# RUN: mkdir -p %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64
# RUN: cp %p/Inputs/ret42.lib %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64/default.lib

# RUN: lld-link /winsysroot:%t.dir/sysroot /out:%t.exe /entry:main /subsystem:console %T/defaultlib.obj
# 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

CHECK4: error: <root>: undefined symbol: main