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/raw_ostream.h"
22 using namespace llvm::COFF
;
24 using namespace lld::coff
;
26 AutoExporter::AutoExporter(
27 COFFLinkerContext
&ctx
,
28 const llvm::DenseSet
<StringRef
> &manualExcludeSymbols
)
29 : manualExcludeSymbols(manualExcludeSymbols
), ctx(ctx
) {
40 "libclang_rt.builtins",
41 "libclang_rt.builtins-aarch64",
42 "libclang_rt.builtins-arm",
43 "libclang_rt.builtins-i386",
44 "libclang_rt.builtins-x86_64",
45 "libclang_rt.profile",
46 "libclang_rt.profile-aarch64",
47 "libclang_rt.profile-arm",
48 "libclang_rt.profile-i386",
49 "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
) {
176 raw_fd_ostream
os(name
, ec
, sys::fs::OF_None
);
178 fatal("cannot open " + name
+ ": " + ec
.message());
181 for (const Export
&e
: exports
) {
182 os
<< " " << e
.exportName
<< " "
184 if (auto *def
= dyn_cast_or_null
<Defined
>(e
.sym
)) {
185 if (def
&& def
->getChunk() &&
186 !(def
->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE
))
193 static StringRef
mangle(Twine sym
, MachineTypes machine
) {
194 assert(machine
!= IMAGE_FILE_MACHINE_UNKNOWN
);
196 return saver().save("_" + sym
);
197 return saver().save(sym
);
200 // Handles -wrap option.
202 // This function instantiates wrapper symbols. At this point, they seem
203 // like they are not being used at all, so we explicitly set some flags so
204 // that LTO won't eliminate them.
205 std::vector
<WrappedSymbol
>
206 lld::coff::addWrappedSymbols(COFFLinkerContext
&ctx
, opt::InputArgList
&args
) {
207 std::vector
<WrappedSymbol
> v
;
208 DenseSet
<StringRef
> seen
;
210 for (auto *arg
: args
.filtered(OPT_wrap
)) {
211 StringRef name
= arg
->getValue();
212 if (!seen
.insert(name
).second
)
215 Symbol
*sym
= ctx
.symtab
.findUnderscore(name
);
220 ctx
.symtab
.addUndefined(mangle("__real_" + name
, ctx
.config
.machine
));
222 ctx
.symtab
.addUndefined(mangle("__wrap_" + name
, ctx
.config
.machine
));
223 v
.push_back({sym
, real
, wrap
});
225 // These symbols may seem undefined initially, but don't bail out
226 // at symtab.reportUnresolvable() due to them, but let wrapSymbols
227 // below sort things out before checking finally with
228 // symtab.resolveRemainingUndefines().
229 sym
->deferUndefined
= true;
230 real
->deferUndefined
= true;
231 // We want to tell LTO not to inline symbols to be overwritten
232 // because LTO doesn't know the final symbol contents after renaming.
233 real
->canInline
= false;
234 sym
->canInline
= false;
236 // Tell LTO not to eliminate these symbols.
237 sym
->isUsedInRegularObj
= true;
238 if (!isa
<Undefined
>(wrap
))
239 wrap
->isUsedInRegularObj
= true;
244 // Do renaming for -wrap by updating pointers to symbols.
246 // When this function is executed, only InputFiles and symbol table
247 // contain pointers to symbol objects. We visit them to replace pointers,
248 // so that wrapped symbols are swapped as instructed by the command line.
249 void lld::coff::wrapSymbols(COFFLinkerContext
&ctx
,
250 ArrayRef
<WrappedSymbol
> wrapped
) {
251 DenseMap
<Symbol
*, Symbol
*> map
;
252 for (const WrappedSymbol
&w
: wrapped
) {
255 if (Defined
*d
= dyn_cast
<Defined
>(w
.wrap
)) {
256 Symbol
*imp
= ctx
.symtab
.find(("__imp_" + w
.sym
->getName()).str());
257 // Create a new defined local import for the wrap symbol. If
258 // no imp prefixed symbol existed, there's no need for it.
259 // (We can't easily distinguish whether any object file actually
260 // referenced it or not, though.)
262 DefinedLocalImport
*wrapimp
= make
<DefinedLocalImport
>(
263 ctx
, saver().save("__imp_" + w
.wrap
->getName()), d
);
264 ctx
.symtab
.localImportChunks
.push_back(wrapimp
->getChunk());
270 // Update pointers in input files.
271 parallelForEach(ctx
.objFileInstances
, [&](ObjFile
*file
) {
272 MutableArrayRef
<Symbol
*> syms
= file
->getMutableSymbols();
273 for (auto &sym
: syms
)
274 if (Symbol
*s
= map
.lookup(sym
))