1 //===- MinGW.cpp ----------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "COFFLinkerContext.h"
12 #include "InputFiles.h"
13 #include "SymbolTable.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/Object/COFF.h"
17 #include "llvm/Support/Parallel.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/TimeProfiler.h"
20 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm::COFF
;
25 using namespace lld::coff
;
27 AutoExporter::AutoExporter(
28 COFFLinkerContext
&ctx
,
29 const llvm::DenseSet
<StringRef
> &manualExcludeSymbols
)
30 : manualExcludeSymbols(manualExcludeSymbols
), ctx(ctx
) {
41 "libclang_rt.builtins",
42 "libclang_rt.builtins-aarch64",
43 "libclang_rt.builtins-arm",
44 "libclang_rt.builtins-i386",
45 "libclang_rt.builtins-x86_64",
46 "libclang_rt.profile",
47 "libclang_rt.profile-aarch64",
48 "libclang_rt.profile-arm",
49 "libclang_rt.profile-i386",
50 "libclang_rt.profile-x86_64",
61 "crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o",
62 "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o",
65 excludeSymbolPrefixes
= {
68 "__IMPORT_DESCRIPTOR_",
69 // Extra import symbols from GNU import libraries
74 // Artificial symbols such as .refptr
76 // profile generate symbols
82 excludeSymbolSuffixes
= {
87 if (ctx
.config
.machine
== I386
) {
89 "__NULL_IMPORT_DESCRIPTOR",
90 "__pei386_runtime_relocator",
97 // These are the MinGW names that differ from the standard
98 // ones (lacking an extra underscore).
101 "_DllMainCRTStartup@12",
103 excludeSymbolPrefixes
.insert("__head_");
106 "__NULL_IMPORT_DESCRIPTOR",
107 "_pei386_runtime_relocator",
114 // These are the MinGW names that differ from the standard
115 // ones (lacking an extra underscore).
120 excludeSymbolPrefixes
.insert("_head_");
124 void AutoExporter::addWholeArchive(StringRef path
) {
125 StringRef libName
= sys::path::filename(path
);
126 // Drop the file extension, to match the processing below.
127 libName
= libName
.substr(0, libName
.rfind('.'));
128 excludeLibs
.erase(libName
);
131 void AutoExporter::addExcludedSymbol(StringRef symbol
) {
132 excludeSymbols
.insert(symbol
);
135 bool AutoExporter::shouldExport(Defined
*sym
) const {
136 if (!sym
|| !sym
->getChunk())
139 // Only allow the symbol kinds that make sense to export; in particular,
140 // disallow import symbols.
141 if (!isa
<DefinedRegular
>(sym
) && !isa
<DefinedCommon
>(sym
))
143 if (excludeSymbols
.count(sym
->getName()) || manualExcludeSymbols
.count(sym
->getName()))
146 for (StringRef prefix
: excludeSymbolPrefixes
.keys())
147 if (sym
->getName().starts_with(prefix
))
149 for (StringRef suffix
: excludeSymbolSuffixes
.keys())
150 if (sym
->getName().ends_with(suffix
))
153 // If a corresponding __imp_ symbol exists and is defined, don't export it.
154 if (ctx
.symtab
.find(("__imp_" + sym
->getName()).str()))
157 // Check that file is non-null before dereferencing it, symbols not
158 // originating in regular object files probably shouldn't be exported.
162 StringRef libName
= sys::path::filename(sym
->getFile()->parentName
);
164 // Drop the file extension.
165 libName
= libName
.substr(0, libName
.rfind('.'));
166 if (!libName
.empty())
167 return !excludeLibs
.count(libName
);
169 StringRef fileName
= sys::path::filename(sym
->getFile()->getName());
170 return !excludeObjects
.count(fileName
);
173 void lld::coff::writeDefFile(StringRef name
,
174 const std::vector
<Export
> &exports
) {
175 llvm::TimeTraceScope
timeScope("Write .def file");
177 raw_fd_ostream
os(name
, ec
, sys::fs::OF_None
);
179 fatal("cannot open " + name
+ ": " + ec
.message());
182 for (const Export
&e
: exports
) {
183 os
<< " " << e
.exportName
<< " "
185 if (auto *def
= dyn_cast_or_null
<Defined
>(e
.sym
)) {
186 if (def
&& def
->getChunk() &&
187 !(def
->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE
))
194 static StringRef
mangle(Twine sym
, MachineTypes machine
) {
195 assert(machine
!= IMAGE_FILE_MACHINE_UNKNOWN
);
197 return saver().save("_" + sym
);
198 return saver().save(sym
);
201 // Handles -wrap option.
203 // This function instantiates wrapper symbols. At this point, they seem
204 // like they are not being used at all, so we explicitly set some flags so
205 // that LTO won't eliminate them.
206 std::vector
<WrappedSymbol
>
207 lld::coff::addWrappedSymbols(COFFLinkerContext
&ctx
, opt::InputArgList
&args
) {
208 std::vector
<WrappedSymbol
> v
;
209 DenseSet
<StringRef
> seen
;
211 for (auto *arg
: args
.filtered(OPT_wrap
)) {
212 StringRef name
= arg
->getValue();
213 if (!seen
.insert(name
).second
)
216 Symbol
*sym
= ctx
.symtab
.findUnderscore(name
);
221 ctx
.symtab
.addUndefined(mangle("__real_" + name
, ctx
.config
.machine
));
223 ctx
.symtab
.addUndefined(mangle("__wrap_" + name
, ctx
.config
.machine
));
224 v
.push_back({sym
, real
, wrap
});
226 // These symbols may seem undefined initially, but don't bail out
227 // at symtab.reportUnresolvable() due to them, but let wrapSymbols
228 // below sort things out before checking finally with
229 // symtab.resolveRemainingUndefines().
230 sym
->deferUndefined
= true;
231 real
->deferUndefined
= true;
232 // We want to tell LTO not to inline symbols to be overwritten
233 // because LTO doesn't know the final symbol contents after renaming.
234 real
->canInline
= false;
235 sym
->canInline
= false;
237 // Tell LTO not to eliminate these symbols.
238 sym
->isUsedInRegularObj
= true;
239 if (!isa
<Undefined
>(wrap
))
240 wrap
->isUsedInRegularObj
= true;
245 // Do renaming for -wrap by updating pointers to symbols.
247 // When this function is executed, only InputFiles and symbol table
248 // contain pointers to symbol objects. We visit them to replace pointers,
249 // so that wrapped symbols are swapped as instructed by the command line.
250 void lld::coff::wrapSymbols(COFFLinkerContext
&ctx
,
251 ArrayRef
<WrappedSymbol
> wrapped
) {
252 DenseMap
<Symbol
*, Symbol
*> map
;
253 for (const WrappedSymbol
&w
: wrapped
) {
256 if (Defined
*d
= dyn_cast
<Defined
>(w
.wrap
)) {
257 Symbol
*imp
= ctx
.symtab
.find(("__imp_" + w
.sym
->getName()).str());
258 // Create a new defined local import for the wrap symbol. If
259 // no imp prefixed symbol existed, there's no need for it.
260 // (We can't easily distinguish whether any object file actually
261 // referenced it or not, though.)
263 DefinedLocalImport
*wrapimp
= make
<DefinedLocalImport
>(
264 ctx
, saver().save("__imp_" + w
.wrap
->getName()), d
);
265 ctx
.symtab
.localImportChunks
.push_back(wrapimp
->getChunk());
271 // Update pointers in input files.
272 parallelForEach(ctx
.objFileInstances
, [&](ObjFile
*file
) {
273 MutableArrayRef
<Symbol
*> syms
= file
->getMutableSymbols();
274 for (auto &sym
: syms
)
275 if (Symbol
*s
= map
.lookup(sym
))