Skip to content

Commit 7668182

Browse files
committed
[COFF] Don't let /def override /out filename
Summary: This also delays setting the output filename based on the first input argument until after processing /def. Fixes PR32354 Reviewers: ruiu, pcc Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D31152 llvm-svn: 298327
1 parent 5821a3b commit 7668182

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

lld/COFF/Driver.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ void LinkerDriver::enqueuePath(StringRef Path) {
137137
fatal(MBOrErr.second, "could not open " + PathStr);
138138
Driver->addBuffer(std::move(MBOrErr.first));
139139
});
140-
141-
if (Config->OutputFile == "")
142-
Config->OutputFile = getOutputPath(Path);
143140
}
144141

145142
void LinkerDriver::addArchiveBuffer(MemoryBufferRef MB, StringRef SymName,
@@ -887,6 +884,12 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
887884
}
888885
}
889886

887+
// Set default image name if neither /out or /def set it.
888+
if (Config->OutputFile.empty()) {
889+
Config->OutputFile =
890+
getOutputPath((*Args.filtered_begin(OPT_INPUT))->getValue());
891+
}
892+
890893
// Set default image base if /base is not given.
891894
if (Config->ImageBase == uint64_t(-1))
892895
Config->ImageBase = getDefaultImageBase();

lld/COFF/ModuleDef.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,25 @@ class Parser {
163163
case KwHeapsize:
164164
parseNumbers(&Config->HeapReserve, &Config->HeapCommit);
165165
return;
166-
case KwLibrary:
167-
parseName(&Config->OutputFile, &Config->ImageBase);
168-
if (!StringRef(Config->OutputFile).endswith_lower(".dll"))
169-
Config->OutputFile += ".dll";
170-
return;
171166
case KwStacksize:
172167
parseNumbers(&Config->StackReserve, &Config->StackCommit);
173168
return;
174-
case KwName:
175-
parseName(&Config->OutputFile, &Config->ImageBase);
169+
case KwLibrary:
170+
case KwName: {
171+
bool IsDll = Tok.K == KwLibrary; // Check before parseName.
172+
std::string Name;
173+
parseName(&Name, &Config->ImageBase);
174+
175+
// Append the appropriate file extension if not already present.
176+
StringRef Ext = IsDll ? ".dll" : ".exe";
177+
if (!StringRef(Name).endswith_lower(Ext))
178+
Name += Ext;
179+
180+
// Set the output file, but don't override /out if it was already passed.
181+
if (Config->OutputFile.empty())
182+
Config->OutputFile = Name;
176183
return;
184+
}
177185
case KwVersion:
178186
parseVersion(&Config->MajorImageVersion, &Config->MinorImageVersion);
179187
return;

lld/test/COFF/def-name.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# REQUIRES: winres
2+
3+
# RUN: rm -rf %t
4+
# RUN: mkdir -p %t
5+
# RUN: cd %t
6+
# RUN: yaml2obj < %p/Inputs/ret42.yaml > in.obj
7+
8+
# RUN: lld-link /entry:main in.obj
9+
# RUN: lld-link /entry:main /dll in.obj
10+
11+
# RUN: echo -e "NAME foo\n" > fooexe.def
12+
# RUN: echo -e "LIBRARY foo\n" > foodll.def
13+
# RUN: lld-link /entry:main /def:fooexe.def in.obj
14+
# RUN: lld-link /entry:main /def:foodll.def /dll in.obj
15+
16+
# RUN: lld-link /entry:main /out:bar.exe /def:fooexe.def in.obj
17+
# RUN: lld-link /entry:main /out:bar.dll /def:foodll.def /dll in.obj
18+
19+
# RUN: llvm-readobj in.exe | FileCheck %s
20+
# RUN: llvm-readobj in.dll | FileCheck %s
21+
22+
# RUN: llvm-readobj foo.exe | FileCheck %s
23+
# RUN: llvm-readobj foo.dll | FileCheck %s
24+
25+
# RUN: llvm-readobj bar.exe | FileCheck %s
26+
# RUN: llvm-readobj bar.dll | FileCheck %s
27+
28+
CHECK: File:

0 commit comments

Comments
 (0)