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 26*61cfbce3SDimitry Andric AutoExporter::AutoExporter( 27*61cfbce3SDimitry Andric const llvm::DenseSet<StringRef> &manualExcludeSymbols) 28*61cfbce3SDimitry Andric : manualExcludeSymbols(manualExcludeSymbols) { 290b57cec5SDimitry Andric excludeLibs = { 300b57cec5SDimitry Andric "libgcc", 310b57cec5SDimitry Andric "libgcc_s", 320b57cec5SDimitry Andric "libstdc++", 330b57cec5SDimitry Andric "libmingw32", 340b57cec5SDimitry Andric "libmingwex", 350b57cec5SDimitry Andric "libg2c", 360b57cec5SDimitry Andric "libsupc++", 370b57cec5SDimitry Andric "libobjc", 380b57cec5SDimitry Andric "libgcj", 390b57cec5SDimitry Andric "libclang_rt.builtins", 400b57cec5SDimitry Andric "libclang_rt.builtins-aarch64", 410b57cec5SDimitry Andric "libclang_rt.builtins-arm", 420b57cec5SDimitry Andric "libclang_rt.builtins-i386", 430b57cec5SDimitry Andric "libclang_rt.builtins-x86_64", 44979e22ffSDimitry Andric "libclang_rt.profile", 45979e22ffSDimitry Andric "libclang_rt.profile-aarch64", 46979e22ffSDimitry Andric "libclang_rt.profile-arm", 47979e22ffSDimitry Andric "libclang_rt.profile-i386", 48979e22ffSDimitry Andric "libclang_rt.profile-x86_64", 490b57cec5SDimitry Andric "libc++", 500b57cec5SDimitry Andric "libc++abi", 510b57cec5SDimitry Andric "libunwind", 520b57cec5SDimitry Andric "libmsvcrt", 530b57cec5SDimitry Andric "libucrtbase", 540b57cec5SDimitry Andric }; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric excludeObjects = { 570b57cec5SDimitry Andric "crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o", 580b57cec5SDimitry Andric "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o", 590b57cec5SDimitry Andric }; 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric excludeSymbolPrefixes = { 620b57cec5SDimitry Andric // Import symbols 630b57cec5SDimitry Andric "__imp_", 640b57cec5SDimitry Andric "__IMPORT_DESCRIPTOR_", 650b57cec5SDimitry Andric // Extra import symbols from GNU import libraries 660b57cec5SDimitry Andric "__nm_", 670b57cec5SDimitry Andric // C++ symbols 680b57cec5SDimitry Andric "__rtti_", 690b57cec5SDimitry Andric "__builtin_", 7085868e8aSDimitry Andric // Artificial symbols such as .refptr 710b57cec5SDimitry Andric ".", 72979e22ffSDimitry Andric // profile generate symbols 73979e22ffSDimitry Andric "__profc_", 74979e22ffSDimitry Andric "__profd_", 75979e22ffSDimitry Andric "__profvp_", 760b57cec5SDimitry Andric }; 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric excludeSymbolSuffixes = { 790b57cec5SDimitry Andric "_iname", 800b57cec5SDimitry Andric "_NULL_THUNK_DATA", 810b57cec5SDimitry Andric }; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric if (config->machine == I386) { 840b57cec5SDimitry Andric excludeSymbols = { 850b57cec5SDimitry Andric "__NULL_IMPORT_DESCRIPTOR", 860b57cec5SDimitry Andric "__pei386_runtime_relocator", 870b57cec5SDimitry Andric "_do_pseudo_reloc", 880b57cec5SDimitry Andric "_impure_ptr", 890b57cec5SDimitry Andric "__impure_ptr", 900b57cec5SDimitry Andric "__fmode", 910b57cec5SDimitry Andric "_environ", 920b57cec5SDimitry Andric "___dso_handle", 930b57cec5SDimitry Andric // These are the MinGW names that differ from the standard 940b57cec5SDimitry Andric // ones (lacking an extra underscore). 950b57cec5SDimitry Andric "_DllMain@12", 960b57cec5SDimitry Andric "_DllEntryPoint@12", 970b57cec5SDimitry Andric "_DllMainCRTStartup@12", 980b57cec5SDimitry Andric }; 990b57cec5SDimitry Andric excludeSymbolPrefixes.insert("__head_"); 1000b57cec5SDimitry Andric } else { 1010b57cec5SDimitry Andric excludeSymbols = { 1020b57cec5SDimitry Andric "__NULL_IMPORT_DESCRIPTOR", 1030b57cec5SDimitry Andric "_pei386_runtime_relocator", 1040b57cec5SDimitry Andric "do_pseudo_reloc", 1050b57cec5SDimitry Andric "impure_ptr", 1060b57cec5SDimitry Andric "_impure_ptr", 1070b57cec5SDimitry Andric "_fmode", 1080b57cec5SDimitry Andric "environ", 1090b57cec5SDimitry Andric "__dso_handle", 1100b57cec5SDimitry Andric // These are the MinGW names that differ from the standard 1110b57cec5SDimitry Andric // ones (lacking an extra underscore). 1120b57cec5SDimitry Andric "DllMain", 1130b57cec5SDimitry Andric "DllEntryPoint", 1140b57cec5SDimitry Andric "DllMainCRTStartup", 1150b57cec5SDimitry Andric }; 1160b57cec5SDimitry Andric excludeSymbolPrefixes.insert("_head_"); 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric void AutoExporter::addWholeArchive(StringRef path) { 1210b57cec5SDimitry Andric StringRef libName = sys::path::filename(path); 1220b57cec5SDimitry Andric // Drop the file extension, to match the processing below. 1230b57cec5SDimitry Andric libName = libName.substr(0, libName.rfind('.')); 1240b57cec5SDimitry Andric excludeLibs.erase(libName); 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 127*61cfbce3SDimitry Andric void AutoExporter::addExcludedSymbol(StringRef symbol) { 128*61cfbce3SDimitry Andric excludeSymbols.insert(symbol); 129*61cfbce3SDimitry Andric } 130*61cfbce3SDimitry Andric 131349cc55cSDimitry Andric bool AutoExporter::shouldExport(const COFFLinkerContext &ctx, 132349cc55cSDimitry Andric Defined *sym) const { 133fe6060f1SDimitry Andric if (!sym || !sym->getChunk()) 1340b57cec5SDimitry Andric return false; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric // Only allow the symbol kinds that make sense to export; in particular, 1370b57cec5SDimitry Andric // disallow import symbols. 1380b57cec5SDimitry Andric if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym)) 1390b57cec5SDimitry Andric return false; 140*61cfbce3SDimitry Andric if (excludeSymbols.count(sym->getName()) || manualExcludeSymbols.count(sym->getName())) 1410b57cec5SDimitry Andric return false; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric for (StringRef prefix : excludeSymbolPrefixes.keys()) 1440b57cec5SDimitry Andric if (sym->getName().startswith(prefix)) 1450b57cec5SDimitry Andric return false; 1460b57cec5SDimitry Andric for (StringRef suffix : excludeSymbolSuffixes.keys()) 1470b57cec5SDimitry Andric if (sym->getName().endswith(suffix)) 1480b57cec5SDimitry Andric return false; 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // If a corresponding __imp_ symbol exists and is defined, don't export it. 151349cc55cSDimitry Andric if (ctx.symtab.find(("__imp_" + sym->getName()).str())) 1520b57cec5SDimitry Andric return false; 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric // Check that file is non-null before dereferencing it, symbols not 1550b57cec5SDimitry Andric // originating in regular object files probably shouldn't be exported. 1560b57cec5SDimitry Andric if (!sym->getFile()) 1570b57cec5SDimitry Andric return false; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric StringRef libName = sys::path::filename(sym->getFile()->parentName); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric // Drop the file extension. 1620b57cec5SDimitry Andric libName = libName.substr(0, libName.rfind('.')); 1630b57cec5SDimitry Andric if (!libName.empty()) 1640b57cec5SDimitry Andric return !excludeLibs.count(libName); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric StringRef fileName = sys::path::filename(sym->getFile()->getName()); 1670b57cec5SDimitry Andric return !excludeObjects.count(fileName); 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1705ffd83dbSDimitry Andric void lld::coff::writeDefFile(StringRef name) { 1710b57cec5SDimitry Andric std::error_code ec; 17285868e8aSDimitry Andric raw_fd_ostream os(name, ec, sys::fs::OF_None); 1730b57cec5SDimitry Andric if (ec) 1740b57cec5SDimitry Andric fatal("cannot open " + name + ": " + ec.message()); 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric os << "EXPORTS\n"; 1770b57cec5SDimitry Andric for (Export &e : config->exports) { 1780b57cec5SDimitry Andric os << " " << e.exportName << " " 1790b57cec5SDimitry Andric << "@" << e.ordinal; 1800b57cec5SDimitry Andric if (auto *def = dyn_cast_or_null<Defined>(e.sym)) { 1810b57cec5SDimitry Andric if (def && def->getChunk() && 1820b57cec5SDimitry Andric !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)) 1830b57cec5SDimitry Andric os << " DATA"; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric os << "\n"; 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric } 188e8d8bef9SDimitry Andric 189e8d8bef9SDimitry Andric static StringRef mangle(Twine sym) { 190e8d8bef9SDimitry Andric assert(config->machine != IMAGE_FILE_MACHINE_UNKNOWN); 191e8d8bef9SDimitry Andric if (config->machine == I386) 19204eeddc0SDimitry Andric return saver().save("_" + sym); 19304eeddc0SDimitry Andric return saver().save(sym); 194e8d8bef9SDimitry Andric } 195e8d8bef9SDimitry Andric 196e8d8bef9SDimitry Andric // Handles -wrap option. 197e8d8bef9SDimitry Andric // 198e8d8bef9SDimitry Andric // This function instantiates wrapper symbols. At this point, they seem 199e8d8bef9SDimitry Andric // like they are not being used at all, so we explicitly set some flags so 200e8d8bef9SDimitry Andric // that LTO won't eliminate them. 201e8d8bef9SDimitry Andric std::vector<WrappedSymbol> 202349cc55cSDimitry Andric lld::coff::addWrappedSymbols(COFFLinkerContext &ctx, opt::InputArgList &args) { 203e8d8bef9SDimitry Andric std::vector<WrappedSymbol> v; 204e8d8bef9SDimitry Andric DenseSet<StringRef> seen; 205e8d8bef9SDimitry Andric 206e8d8bef9SDimitry Andric for (auto *arg : args.filtered(OPT_wrap)) { 207e8d8bef9SDimitry Andric StringRef name = arg->getValue(); 208e8d8bef9SDimitry Andric if (!seen.insert(name).second) 209e8d8bef9SDimitry Andric continue; 210e8d8bef9SDimitry Andric 211349cc55cSDimitry Andric Symbol *sym = ctx.symtab.findUnderscore(name); 212e8d8bef9SDimitry Andric if (!sym) 213e8d8bef9SDimitry Andric continue; 214e8d8bef9SDimitry Andric 215349cc55cSDimitry Andric Symbol *real = ctx.symtab.addUndefined(mangle("__real_" + name)); 216349cc55cSDimitry Andric Symbol *wrap = ctx.symtab.addUndefined(mangle("__wrap_" + name)); 217e8d8bef9SDimitry Andric v.push_back({sym, real, wrap}); 218e8d8bef9SDimitry Andric 219e8d8bef9SDimitry Andric // These symbols may seem undefined initially, but don't bail out 220349cc55cSDimitry Andric // at symtab.reportUnresolvable() due to them, but let wrapSymbols 221e8d8bef9SDimitry Andric // below sort things out before checking finally with 222349cc55cSDimitry Andric // symtab.resolveRemainingUndefines(). 223e8d8bef9SDimitry Andric sym->deferUndefined = true; 224e8d8bef9SDimitry Andric real->deferUndefined = true; 225e8d8bef9SDimitry Andric // We want to tell LTO not to inline symbols to be overwritten 226e8d8bef9SDimitry Andric // because LTO doesn't know the final symbol contents after renaming. 227e8d8bef9SDimitry Andric real->canInline = false; 228e8d8bef9SDimitry Andric sym->canInline = false; 229e8d8bef9SDimitry Andric 230e8d8bef9SDimitry Andric // Tell LTO not to eliminate these symbols. 231e8d8bef9SDimitry Andric sym->isUsedInRegularObj = true; 232e8d8bef9SDimitry Andric if (!isa<Undefined>(wrap)) 233e8d8bef9SDimitry Andric wrap->isUsedInRegularObj = true; 234e8d8bef9SDimitry Andric } 235e8d8bef9SDimitry Andric return v; 236e8d8bef9SDimitry Andric } 237e8d8bef9SDimitry Andric 238e8d8bef9SDimitry Andric // Do renaming for -wrap by updating pointers to symbols. 239e8d8bef9SDimitry Andric // 240e8d8bef9SDimitry Andric // When this function is executed, only InputFiles and symbol table 241e8d8bef9SDimitry Andric // contain pointers to symbol objects. We visit them to replace pointers, 242e8d8bef9SDimitry Andric // so that wrapped symbols are swapped as instructed by the command line. 243349cc55cSDimitry Andric void lld::coff::wrapSymbols(COFFLinkerContext &ctx, 244349cc55cSDimitry Andric ArrayRef<WrappedSymbol> wrapped) { 245e8d8bef9SDimitry Andric DenseMap<Symbol *, Symbol *> map; 246e8d8bef9SDimitry Andric for (const WrappedSymbol &w : wrapped) { 247e8d8bef9SDimitry Andric map[w.sym] = w.wrap; 248e8d8bef9SDimitry Andric map[w.real] = w.sym; 249e8d8bef9SDimitry Andric if (Defined *d = dyn_cast<Defined>(w.wrap)) { 250349cc55cSDimitry Andric Symbol *imp = ctx.symtab.find(("__imp_" + w.sym->getName()).str()); 251e8d8bef9SDimitry Andric // Create a new defined local import for the wrap symbol. If 252e8d8bef9SDimitry Andric // no imp prefixed symbol existed, there's no need for it. 253e8d8bef9SDimitry Andric // (We can't easily distinguish whether any object file actually 254e8d8bef9SDimitry Andric // referenced it or not, though.) 255e8d8bef9SDimitry Andric if (imp) { 256e8d8bef9SDimitry Andric DefinedLocalImport *wrapimp = make<DefinedLocalImport>( 25704eeddc0SDimitry Andric saver().save("__imp_" + w.wrap->getName()), d); 258349cc55cSDimitry Andric ctx.symtab.localImportChunks.push_back(wrapimp->getChunk()); 259e8d8bef9SDimitry Andric map[imp] = wrapimp; 260e8d8bef9SDimitry Andric } 261e8d8bef9SDimitry Andric } 262e8d8bef9SDimitry Andric } 263e8d8bef9SDimitry Andric 264e8d8bef9SDimitry Andric // Update pointers in input files. 265349cc55cSDimitry Andric parallelForEach(ctx.objFileInstances, [&](ObjFile *file) { 266e8d8bef9SDimitry Andric MutableArrayRef<Symbol *> syms = file->getMutableSymbols(); 267e8d8bef9SDimitry Andric for (size_t i = 0, e = syms.size(); i != e; ++i) 268e8d8bef9SDimitry Andric if (Symbol *s = map.lookup(syms[i])) 269e8d8bef9SDimitry Andric syms[i] = s; 270e8d8bef9SDimitry Andric }); 271e8d8bef9SDimitry Andric } 272