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( 27*bdd1243dSDimitry Andric COFFLinkerContext &ctx, 2861cfbce3SDimitry Andric const llvm::DenseSet<StringRef> &manualExcludeSymbols) 29*bdd1243dSDimitry 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", 520b57cec5SDimitry Andric "libunwind", 530b57cec5SDimitry Andric "libmsvcrt", 540b57cec5SDimitry Andric "libucrtbase", 550b57cec5SDimitry Andric }; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric excludeObjects = { 580b57cec5SDimitry Andric "crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o", 590b57cec5SDimitry Andric "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o", 600b57cec5SDimitry Andric }; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric excludeSymbolPrefixes = { 630b57cec5SDimitry Andric // Import symbols 640b57cec5SDimitry Andric "__imp_", 650b57cec5SDimitry Andric "__IMPORT_DESCRIPTOR_", 660b57cec5SDimitry Andric // Extra import symbols from GNU import libraries 670b57cec5SDimitry Andric "__nm_", 680b57cec5SDimitry Andric // C++ symbols 690b57cec5SDimitry Andric "__rtti_", 700b57cec5SDimitry Andric "__builtin_", 7185868e8aSDimitry Andric // Artificial symbols such as .refptr 720b57cec5SDimitry Andric ".", 73979e22ffSDimitry Andric // profile generate symbols 74979e22ffSDimitry Andric "__profc_", 75979e22ffSDimitry Andric "__profd_", 76979e22ffSDimitry Andric "__profvp_", 770b57cec5SDimitry Andric }; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric excludeSymbolSuffixes = { 800b57cec5SDimitry Andric "_iname", 810b57cec5SDimitry Andric "_NULL_THUNK_DATA", 820b57cec5SDimitry Andric }; 830b57cec5SDimitry Andric 84*bdd1243dSDimitry Andric if (ctx.config.machine == I386) { 850b57cec5SDimitry Andric excludeSymbols = { 860b57cec5SDimitry Andric "__NULL_IMPORT_DESCRIPTOR", 870b57cec5SDimitry Andric "__pei386_runtime_relocator", 880b57cec5SDimitry Andric "_do_pseudo_reloc", 890b57cec5SDimitry Andric "_impure_ptr", 900b57cec5SDimitry Andric "__impure_ptr", 910b57cec5SDimitry Andric "__fmode", 920b57cec5SDimitry Andric "_environ", 930b57cec5SDimitry Andric "___dso_handle", 940b57cec5SDimitry Andric // These are the MinGW names that differ from the standard 950b57cec5SDimitry Andric // ones (lacking an extra underscore). 960b57cec5SDimitry Andric "_DllMain@12", 970b57cec5SDimitry Andric "_DllEntryPoint@12", 980b57cec5SDimitry Andric "_DllMainCRTStartup@12", 990b57cec5SDimitry Andric }; 1000b57cec5SDimitry Andric excludeSymbolPrefixes.insert("__head_"); 1010b57cec5SDimitry Andric } else { 1020b57cec5SDimitry Andric excludeSymbols = { 1030b57cec5SDimitry Andric "__NULL_IMPORT_DESCRIPTOR", 1040b57cec5SDimitry Andric "_pei386_runtime_relocator", 1050b57cec5SDimitry Andric "do_pseudo_reloc", 1060b57cec5SDimitry Andric "impure_ptr", 1070b57cec5SDimitry Andric "_impure_ptr", 1080b57cec5SDimitry Andric "_fmode", 1090b57cec5SDimitry Andric "environ", 1100b57cec5SDimitry Andric "__dso_handle", 1110b57cec5SDimitry Andric // These are the MinGW names that differ from the standard 1120b57cec5SDimitry Andric // ones (lacking an extra underscore). 1130b57cec5SDimitry Andric "DllMain", 1140b57cec5SDimitry Andric "DllEntryPoint", 1150b57cec5SDimitry Andric "DllMainCRTStartup", 1160b57cec5SDimitry Andric }; 1170b57cec5SDimitry Andric excludeSymbolPrefixes.insert("_head_"); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric void AutoExporter::addWholeArchive(StringRef path) { 1220b57cec5SDimitry Andric StringRef libName = sys::path::filename(path); 1230b57cec5SDimitry Andric // Drop the file extension, to match the processing below. 1240b57cec5SDimitry Andric libName = libName.substr(0, libName.rfind('.')); 1250b57cec5SDimitry Andric excludeLibs.erase(libName); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 12861cfbce3SDimitry Andric void AutoExporter::addExcludedSymbol(StringRef symbol) { 12961cfbce3SDimitry Andric excludeSymbols.insert(symbol); 13061cfbce3SDimitry Andric } 13161cfbce3SDimitry Andric 132*bdd1243dSDimitry Andric bool AutoExporter::shouldExport(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; 14061cfbce3SDimitry 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 170*bdd1243dSDimitry Andric void lld::coff::writeDefFile(StringRef name, 171*bdd1243dSDimitry Andric const std::vector<Export> &exports) { 1720b57cec5SDimitry Andric std::error_code ec; 17385868e8aSDimitry Andric raw_fd_ostream os(name, ec, sys::fs::OF_None); 1740b57cec5SDimitry Andric if (ec) 1750b57cec5SDimitry Andric fatal("cannot open " + name + ": " + ec.message()); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric os << "EXPORTS\n"; 178*bdd1243dSDimitry Andric for (const Export &e : exports) { 1790b57cec5SDimitry Andric os << " " << e.exportName << " " 1800b57cec5SDimitry Andric << "@" << e.ordinal; 1810b57cec5SDimitry Andric if (auto *def = dyn_cast_or_null<Defined>(e.sym)) { 1820b57cec5SDimitry Andric if (def && def->getChunk() && 1830b57cec5SDimitry Andric !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)) 1840b57cec5SDimitry Andric os << " DATA"; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric os << "\n"; 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric } 189e8d8bef9SDimitry Andric 190*bdd1243dSDimitry Andric static StringRef mangle(Twine sym, MachineTypes machine) { 191*bdd1243dSDimitry Andric assert(machine != IMAGE_FILE_MACHINE_UNKNOWN); 192*bdd1243dSDimitry Andric if (machine == I386) 19304eeddc0SDimitry Andric return saver().save("_" + sym); 19404eeddc0SDimitry Andric return saver().save(sym); 195e8d8bef9SDimitry Andric } 196e8d8bef9SDimitry Andric 197e8d8bef9SDimitry Andric // Handles -wrap option. 198e8d8bef9SDimitry Andric // 199e8d8bef9SDimitry Andric // This function instantiates wrapper symbols. At this point, they seem 200e8d8bef9SDimitry Andric // like they are not being used at all, so we explicitly set some flags so 201e8d8bef9SDimitry Andric // that LTO won't eliminate them. 202e8d8bef9SDimitry Andric std::vector<WrappedSymbol> 203349cc55cSDimitry Andric lld::coff::addWrappedSymbols(COFFLinkerContext &ctx, opt::InputArgList &args) { 204e8d8bef9SDimitry Andric std::vector<WrappedSymbol> v; 205e8d8bef9SDimitry Andric DenseSet<StringRef> seen; 206e8d8bef9SDimitry Andric 207e8d8bef9SDimitry Andric for (auto *arg : args.filtered(OPT_wrap)) { 208e8d8bef9SDimitry Andric StringRef name = arg->getValue(); 209e8d8bef9SDimitry Andric if (!seen.insert(name).second) 210e8d8bef9SDimitry Andric continue; 211e8d8bef9SDimitry Andric 212349cc55cSDimitry Andric Symbol *sym = ctx.symtab.findUnderscore(name); 213e8d8bef9SDimitry Andric if (!sym) 214e8d8bef9SDimitry Andric continue; 215e8d8bef9SDimitry Andric 216*bdd1243dSDimitry Andric Symbol *real = 217*bdd1243dSDimitry Andric ctx.symtab.addUndefined(mangle("__real_" + name, ctx.config.machine)); 218*bdd1243dSDimitry Andric Symbol *wrap = 219*bdd1243dSDimitry Andric ctx.symtab.addUndefined(mangle("__wrap_" + name, ctx.config.machine)); 220e8d8bef9SDimitry Andric v.push_back({sym, real, wrap}); 221e8d8bef9SDimitry Andric 222e8d8bef9SDimitry Andric // These symbols may seem undefined initially, but don't bail out 223349cc55cSDimitry Andric // at symtab.reportUnresolvable() due to them, but let wrapSymbols 224e8d8bef9SDimitry Andric // below sort things out before checking finally with 225349cc55cSDimitry Andric // symtab.resolveRemainingUndefines(). 226e8d8bef9SDimitry Andric sym->deferUndefined = true; 227e8d8bef9SDimitry Andric real->deferUndefined = true; 228e8d8bef9SDimitry Andric // We want to tell LTO not to inline symbols to be overwritten 229e8d8bef9SDimitry Andric // because LTO doesn't know the final symbol contents after renaming. 230e8d8bef9SDimitry Andric real->canInline = false; 231e8d8bef9SDimitry Andric sym->canInline = false; 232e8d8bef9SDimitry Andric 233e8d8bef9SDimitry Andric // Tell LTO not to eliminate these symbols. 234e8d8bef9SDimitry Andric sym->isUsedInRegularObj = true; 235e8d8bef9SDimitry Andric if (!isa<Undefined>(wrap)) 236e8d8bef9SDimitry Andric wrap->isUsedInRegularObj = true; 237e8d8bef9SDimitry Andric } 238e8d8bef9SDimitry Andric return v; 239e8d8bef9SDimitry Andric } 240e8d8bef9SDimitry Andric 241e8d8bef9SDimitry Andric // Do renaming for -wrap by updating pointers to symbols. 242e8d8bef9SDimitry Andric // 243e8d8bef9SDimitry Andric // When this function is executed, only InputFiles and symbol table 244e8d8bef9SDimitry Andric // contain pointers to symbol objects. We visit them to replace pointers, 245e8d8bef9SDimitry Andric // so that wrapped symbols are swapped as instructed by the command line. 246349cc55cSDimitry Andric void lld::coff::wrapSymbols(COFFLinkerContext &ctx, 247349cc55cSDimitry Andric ArrayRef<WrappedSymbol> wrapped) { 248e8d8bef9SDimitry Andric DenseMap<Symbol *, Symbol *> map; 249e8d8bef9SDimitry Andric for (const WrappedSymbol &w : wrapped) { 250e8d8bef9SDimitry Andric map[w.sym] = w.wrap; 251e8d8bef9SDimitry Andric map[w.real] = w.sym; 252e8d8bef9SDimitry Andric if (Defined *d = dyn_cast<Defined>(w.wrap)) { 253349cc55cSDimitry Andric Symbol *imp = ctx.symtab.find(("__imp_" + w.sym->getName()).str()); 254e8d8bef9SDimitry Andric // Create a new defined local import for the wrap symbol. If 255e8d8bef9SDimitry Andric // no imp prefixed symbol existed, there's no need for it. 256e8d8bef9SDimitry Andric // (We can't easily distinguish whether any object file actually 257e8d8bef9SDimitry Andric // referenced it or not, though.) 258e8d8bef9SDimitry Andric if (imp) { 259e8d8bef9SDimitry Andric DefinedLocalImport *wrapimp = make<DefinedLocalImport>( 260*bdd1243dSDimitry Andric ctx, saver().save("__imp_" + w.wrap->getName()), d); 261349cc55cSDimitry Andric ctx.symtab.localImportChunks.push_back(wrapimp->getChunk()); 262e8d8bef9SDimitry Andric map[imp] = wrapimp; 263e8d8bef9SDimitry Andric } 264e8d8bef9SDimitry Andric } 265e8d8bef9SDimitry Andric } 266e8d8bef9SDimitry Andric 267e8d8bef9SDimitry Andric // Update pointers in input files. 268349cc55cSDimitry Andric parallelForEach(ctx.objFileInstances, [&](ObjFile *file) { 269e8d8bef9SDimitry Andric MutableArrayRef<Symbol *> syms = file->getMutableSymbols(); 270e8d8bef9SDimitry Andric for (size_t i = 0, e = syms.size(); i != e; ++i) 271e8d8bef9SDimitry Andric if (Symbol *s = map.lookup(syms[i])) 272e8d8bef9SDimitry Andric syms[i] = s; 273e8d8bef9SDimitry Andric }); 274e8d8bef9SDimitry Andric } 275