xref: /freebsd/contrib/llvm-project/lld/COFF/MinGW.cpp (revision 1ac55f4cb0001fed92329746c730aa9a947c09a5)
10b57cec5SDimitry Andric //===- MinGW.cpp ----------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "MinGW.h"
10349cc55cSDimitry Andric #include "COFFLinkerContext.h"
11e8d8bef9SDimitry Andric #include "Driver.h"
12e8d8bef9SDimitry Andric #include "InputFiles.h"
130b57cec5SDimitry Andric #include "SymbolTable.h"
14e8d8bef9SDimitry Andric #include "llvm/ADT/DenseMap.h"
15e8d8bef9SDimitry Andric #include "llvm/ADT/DenseSet.h"
160b57cec5SDimitry Andric #include "llvm/Object/COFF.h"
17e8d8bef9SDimitry Andric #include "llvm/Support/Parallel.h"
180b57cec5SDimitry Andric #include "llvm/Support/Path.h"
190b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric using namespace llvm::COFF;
235ffd83dbSDimitry Andric using namespace lld;
245ffd83dbSDimitry Andric using namespace lld::coff;
2585868e8aSDimitry Andric 
2661cfbce3SDimitry Andric AutoExporter::AutoExporter(
27bdd1243dSDimitry Andric     COFFLinkerContext &ctx,
2861cfbce3SDimitry Andric     const llvm::DenseSet<StringRef> &manualExcludeSymbols)
29bdd1243dSDimitry Andric     : manualExcludeSymbols(manualExcludeSymbols), ctx(ctx) {
300b57cec5SDimitry Andric   excludeLibs = {
310b57cec5SDimitry Andric       "libgcc",
320b57cec5SDimitry Andric       "libgcc_s",
330b57cec5SDimitry Andric       "libstdc++",
340b57cec5SDimitry Andric       "libmingw32",
350b57cec5SDimitry Andric       "libmingwex",
360b57cec5SDimitry Andric       "libg2c",
370b57cec5SDimitry Andric       "libsupc++",
380b57cec5SDimitry Andric       "libobjc",
390b57cec5SDimitry Andric       "libgcj",
400b57cec5SDimitry Andric       "libclang_rt.builtins",
410b57cec5SDimitry Andric       "libclang_rt.builtins-aarch64",
420b57cec5SDimitry Andric       "libclang_rt.builtins-arm",
430b57cec5SDimitry Andric       "libclang_rt.builtins-i386",
440b57cec5SDimitry Andric       "libclang_rt.builtins-x86_64",
45979e22ffSDimitry Andric       "libclang_rt.profile",
46979e22ffSDimitry Andric       "libclang_rt.profile-aarch64",
47979e22ffSDimitry Andric       "libclang_rt.profile-arm",
48979e22ffSDimitry Andric       "libclang_rt.profile-i386",
49979e22ffSDimitry Andric       "libclang_rt.profile-x86_64",
500b57cec5SDimitry Andric       "libc++",
510b57cec5SDimitry Andric       "libc++abi",
52*1ac55f4cSDimitry Andric       "libFortran_main",
53*1ac55f4cSDimitry Andric       "libFortranRuntime",
54*1ac55f4cSDimitry Andric       "libFortranDecimal",
550b57cec5SDimitry Andric       "libunwind",
560b57cec5SDimitry Andric       "libmsvcrt",
570b57cec5SDimitry Andric       "libucrtbase",
580b57cec5SDimitry Andric   };
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   excludeObjects = {
610b57cec5SDimitry Andric       "crt0.o",    "crt1.o",  "crt1u.o", "crt2.o",  "crt2u.o",    "dllcrt1.o",
620b57cec5SDimitry Andric       "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o",
630b57cec5SDimitry Andric   };
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   excludeSymbolPrefixes = {
660b57cec5SDimitry Andric       // Import symbols
670b57cec5SDimitry Andric       "__imp_",
680b57cec5SDimitry Andric       "__IMPORT_DESCRIPTOR_",
690b57cec5SDimitry Andric       // Extra import symbols from GNU import libraries
700b57cec5SDimitry Andric       "__nm_",
710b57cec5SDimitry Andric       // C++ symbols
720b57cec5SDimitry Andric       "__rtti_",
730b57cec5SDimitry Andric       "__builtin_",
7485868e8aSDimitry Andric       // Artificial symbols such as .refptr
750b57cec5SDimitry Andric       ".",
76979e22ffSDimitry Andric       // profile generate symbols
77979e22ffSDimitry Andric       "__profc_",
78979e22ffSDimitry Andric       "__profd_",
79979e22ffSDimitry Andric       "__profvp_",
800b57cec5SDimitry Andric   };
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   excludeSymbolSuffixes = {
830b57cec5SDimitry Andric       "_iname",
840b57cec5SDimitry Andric       "_NULL_THUNK_DATA",
850b57cec5SDimitry Andric   };
860b57cec5SDimitry Andric 
87bdd1243dSDimitry Andric   if (ctx.config.machine == I386) {
880b57cec5SDimitry Andric     excludeSymbols = {
890b57cec5SDimitry Andric         "__NULL_IMPORT_DESCRIPTOR",
900b57cec5SDimitry Andric         "__pei386_runtime_relocator",
910b57cec5SDimitry Andric         "_do_pseudo_reloc",
920b57cec5SDimitry Andric         "_impure_ptr",
930b57cec5SDimitry Andric         "__impure_ptr",
940b57cec5SDimitry Andric         "__fmode",
950b57cec5SDimitry Andric         "_environ",
960b57cec5SDimitry Andric         "___dso_handle",
970b57cec5SDimitry Andric         // These are the MinGW names that differ from the standard
980b57cec5SDimitry Andric         // ones (lacking an extra underscore).
990b57cec5SDimitry Andric         "_DllMain@12",
1000b57cec5SDimitry Andric         "_DllEntryPoint@12",
1010b57cec5SDimitry Andric         "_DllMainCRTStartup@12",
1020b57cec5SDimitry Andric     };
1030b57cec5SDimitry Andric     excludeSymbolPrefixes.insert("__head_");
1040b57cec5SDimitry Andric   } else {
1050b57cec5SDimitry Andric     excludeSymbols = {
1060b57cec5SDimitry Andric         "__NULL_IMPORT_DESCRIPTOR",
1070b57cec5SDimitry Andric         "_pei386_runtime_relocator",
1080b57cec5SDimitry Andric         "do_pseudo_reloc",
1090b57cec5SDimitry Andric         "impure_ptr",
1100b57cec5SDimitry Andric         "_impure_ptr",
1110b57cec5SDimitry Andric         "_fmode",
1120b57cec5SDimitry Andric         "environ",
1130b57cec5SDimitry Andric         "__dso_handle",
1140b57cec5SDimitry Andric         // These are the MinGW names that differ from the standard
1150b57cec5SDimitry Andric         // ones (lacking an extra underscore).
1160b57cec5SDimitry Andric         "DllMain",
1170b57cec5SDimitry Andric         "DllEntryPoint",
1180b57cec5SDimitry Andric         "DllMainCRTStartup",
1190b57cec5SDimitry Andric     };
1200b57cec5SDimitry Andric     excludeSymbolPrefixes.insert("_head_");
1210b57cec5SDimitry Andric   }
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric void AutoExporter::addWholeArchive(StringRef path) {
1250b57cec5SDimitry Andric   StringRef libName = sys::path::filename(path);
1260b57cec5SDimitry Andric   // Drop the file extension, to match the processing below.
1270b57cec5SDimitry Andric   libName = libName.substr(0, libName.rfind('.'));
1280b57cec5SDimitry Andric   excludeLibs.erase(libName);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
13161cfbce3SDimitry Andric void AutoExporter::addExcludedSymbol(StringRef symbol) {
13261cfbce3SDimitry Andric   excludeSymbols.insert(symbol);
13361cfbce3SDimitry Andric }
13461cfbce3SDimitry Andric 
135bdd1243dSDimitry Andric bool AutoExporter::shouldExport(Defined *sym) const {
136fe6060f1SDimitry Andric   if (!sym || !sym->getChunk())
1370b57cec5SDimitry Andric     return false;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   // Only allow the symbol kinds that make sense to export; in particular,
1400b57cec5SDimitry Andric   // disallow import symbols.
1410b57cec5SDimitry Andric   if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym))
1420b57cec5SDimitry Andric     return false;
14361cfbce3SDimitry Andric   if (excludeSymbols.count(sym->getName()) || manualExcludeSymbols.count(sym->getName()))
1440b57cec5SDimitry Andric     return false;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   for (StringRef prefix : excludeSymbolPrefixes.keys())
1470b57cec5SDimitry Andric     if (sym->getName().startswith(prefix))
1480b57cec5SDimitry Andric       return false;
1490b57cec5SDimitry Andric   for (StringRef suffix : excludeSymbolSuffixes.keys())
1500b57cec5SDimitry Andric     if (sym->getName().endswith(suffix))
1510b57cec5SDimitry Andric       return false;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   // If a corresponding __imp_ symbol exists and is defined, don't export it.
154349cc55cSDimitry Andric   if (ctx.symtab.find(("__imp_" + sym->getName()).str()))
1550b57cec5SDimitry Andric     return false;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   // Check that file is non-null before dereferencing it, symbols not
1580b57cec5SDimitry Andric   // originating in regular object files probably shouldn't be exported.
1590b57cec5SDimitry Andric   if (!sym->getFile())
1600b57cec5SDimitry Andric     return false;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   StringRef libName = sys::path::filename(sym->getFile()->parentName);
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   // Drop the file extension.
1650b57cec5SDimitry Andric   libName = libName.substr(0, libName.rfind('.'));
1660b57cec5SDimitry Andric   if (!libName.empty())
1670b57cec5SDimitry Andric     return !excludeLibs.count(libName);
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   StringRef fileName = sys::path::filename(sym->getFile()->getName());
1700b57cec5SDimitry Andric   return !excludeObjects.count(fileName);
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric 
173bdd1243dSDimitry Andric void lld::coff::writeDefFile(StringRef name,
174bdd1243dSDimitry Andric                              const std::vector<Export> &exports) {
1750b57cec5SDimitry Andric   std::error_code ec;
17685868e8aSDimitry Andric   raw_fd_ostream os(name, ec, sys::fs::OF_None);
1770b57cec5SDimitry Andric   if (ec)
1780b57cec5SDimitry Andric     fatal("cannot open " + name + ": " + ec.message());
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   os << "EXPORTS\n";
181bdd1243dSDimitry Andric   for (const Export &e : exports) {
1820b57cec5SDimitry Andric     os << "    " << e.exportName << " "
1830b57cec5SDimitry Andric        << "@" << e.ordinal;
1840b57cec5SDimitry Andric     if (auto *def = dyn_cast_or_null<Defined>(e.sym)) {
1850b57cec5SDimitry Andric       if (def && def->getChunk() &&
1860b57cec5SDimitry Andric           !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
1870b57cec5SDimitry Andric         os << " DATA";
1880b57cec5SDimitry Andric     }
1890b57cec5SDimitry Andric     os << "\n";
1900b57cec5SDimitry Andric   }
1910b57cec5SDimitry Andric }
192e8d8bef9SDimitry Andric 
193bdd1243dSDimitry Andric static StringRef mangle(Twine sym, MachineTypes machine) {
194bdd1243dSDimitry Andric   assert(machine != IMAGE_FILE_MACHINE_UNKNOWN);
195bdd1243dSDimitry Andric   if (machine == I386)
19604eeddc0SDimitry Andric     return saver().save("_" + sym);
19704eeddc0SDimitry Andric   return saver().save(sym);
198e8d8bef9SDimitry Andric }
199e8d8bef9SDimitry Andric 
200e8d8bef9SDimitry Andric // Handles -wrap option.
201e8d8bef9SDimitry Andric //
202e8d8bef9SDimitry Andric // This function instantiates wrapper symbols. At this point, they seem
203e8d8bef9SDimitry Andric // like they are not being used at all, so we explicitly set some flags so
204e8d8bef9SDimitry Andric // that LTO won't eliminate them.
205e8d8bef9SDimitry Andric std::vector<WrappedSymbol>
206349cc55cSDimitry Andric lld::coff::addWrappedSymbols(COFFLinkerContext &ctx, opt::InputArgList &args) {
207e8d8bef9SDimitry Andric   std::vector<WrappedSymbol> v;
208e8d8bef9SDimitry Andric   DenseSet<StringRef> seen;
209e8d8bef9SDimitry Andric 
210e8d8bef9SDimitry Andric   for (auto *arg : args.filtered(OPT_wrap)) {
211e8d8bef9SDimitry Andric     StringRef name = arg->getValue();
212e8d8bef9SDimitry Andric     if (!seen.insert(name).second)
213e8d8bef9SDimitry Andric       continue;
214e8d8bef9SDimitry Andric 
215349cc55cSDimitry Andric     Symbol *sym = ctx.symtab.findUnderscore(name);
216e8d8bef9SDimitry Andric     if (!sym)
217e8d8bef9SDimitry Andric       continue;
218e8d8bef9SDimitry Andric 
219bdd1243dSDimitry Andric     Symbol *real =
220bdd1243dSDimitry Andric         ctx.symtab.addUndefined(mangle("__real_" + name, ctx.config.machine));
221bdd1243dSDimitry Andric     Symbol *wrap =
222bdd1243dSDimitry Andric         ctx.symtab.addUndefined(mangle("__wrap_" + name, ctx.config.machine));
223e8d8bef9SDimitry Andric     v.push_back({sym, real, wrap});
224e8d8bef9SDimitry Andric 
225e8d8bef9SDimitry Andric     // These symbols may seem undefined initially, but don't bail out
226349cc55cSDimitry Andric     // at symtab.reportUnresolvable() due to them, but let wrapSymbols
227e8d8bef9SDimitry Andric     // below sort things out before checking finally with
228349cc55cSDimitry Andric     // symtab.resolveRemainingUndefines().
229e8d8bef9SDimitry Andric     sym->deferUndefined = true;
230e8d8bef9SDimitry Andric     real->deferUndefined = true;
231e8d8bef9SDimitry Andric     // We want to tell LTO not to inline symbols to be overwritten
232e8d8bef9SDimitry Andric     // because LTO doesn't know the final symbol contents after renaming.
233e8d8bef9SDimitry Andric     real->canInline = false;
234e8d8bef9SDimitry Andric     sym->canInline = false;
235e8d8bef9SDimitry Andric 
236e8d8bef9SDimitry Andric     // Tell LTO not to eliminate these symbols.
237e8d8bef9SDimitry Andric     sym->isUsedInRegularObj = true;
238e8d8bef9SDimitry Andric     if (!isa<Undefined>(wrap))
239e8d8bef9SDimitry Andric       wrap->isUsedInRegularObj = true;
240e8d8bef9SDimitry Andric   }
241e8d8bef9SDimitry Andric   return v;
242e8d8bef9SDimitry Andric }
243e8d8bef9SDimitry Andric 
244e8d8bef9SDimitry Andric // Do renaming for -wrap by updating pointers to symbols.
245e8d8bef9SDimitry Andric //
246e8d8bef9SDimitry Andric // When this function is executed, only InputFiles and symbol table
247e8d8bef9SDimitry Andric // contain pointers to symbol objects. We visit them to replace pointers,
248e8d8bef9SDimitry Andric // so that wrapped symbols are swapped as instructed by the command line.
249349cc55cSDimitry Andric void lld::coff::wrapSymbols(COFFLinkerContext &ctx,
250349cc55cSDimitry Andric                             ArrayRef<WrappedSymbol> wrapped) {
251e8d8bef9SDimitry Andric   DenseMap<Symbol *, Symbol *> map;
252e8d8bef9SDimitry Andric   for (const WrappedSymbol &w : wrapped) {
253e8d8bef9SDimitry Andric     map[w.sym] = w.wrap;
254e8d8bef9SDimitry Andric     map[w.real] = w.sym;
255e8d8bef9SDimitry Andric     if (Defined *d = dyn_cast<Defined>(w.wrap)) {
256349cc55cSDimitry Andric       Symbol *imp = ctx.symtab.find(("__imp_" + w.sym->getName()).str());
257e8d8bef9SDimitry Andric       // Create a new defined local import for the wrap symbol. If
258e8d8bef9SDimitry Andric       // no imp prefixed symbol existed, there's no need for it.
259e8d8bef9SDimitry Andric       // (We can't easily distinguish whether any object file actually
260e8d8bef9SDimitry Andric       // referenced it or not, though.)
261e8d8bef9SDimitry Andric       if (imp) {
262e8d8bef9SDimitry Andric         DefinedLocalImport *wrapimp = make<DefinedLocalImport>(
263bdd1243dSDimitry Andric             ctx, saver().save("__imp_" + w.wrap->getName()), d);
264349cc55cSDimitry Andric         ctx.symtab.localImportChunks.push_back(wrapimp->getChunk());
265e8d8bef9SDimitry Andric         map[imp] = wrapimp;
266e8d8bef9SDimitry Andric       }
267e8d8bef9SDimitry Andric     }
268e8d8bef9SDimitry Andric   }
269e8d8bef9SDimitry Andric 
270e8d8bef9SDimitry Andric   // Update pointers in input files.
271349cc55cSDimitry Andric   parallelForEach(ctx.objFileInstances, [&](ObjFile *file) {
272e8d8bef9SDimitry Andric     MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
273e8d8bef9SDimitry Andric     for (size_t i = 0, e = syms.size(); i != e; ++i)
274e8d8bef9SDimitry Andric       if (Symbol *s = map.lookup(syms[i]))
275e8d8bef9SDimitry Andric         syms[i] = s;
276e8d8bef9SDimitry Andric   });
277e8d8bef9SDimitry Andric }
278