1 //===- MinGW.cpp ----------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "SymbolTable.h"
12 #include "lld/Common/ErrorHandler.h"
13 #include "llvm/Object/COFF.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/raw_ostream.h"
18 using namespace lld::coff;
20 using namespace llvm::COFF;
22 AutoExporter::AutoExporter() {
23 if (Config->Machine == I386) {
25 "__NULL_IMPORT_DESCRIPTOR",
26 "__pei386_runtime_relocator",
33 // These are the MinGW names that differ from the standard
34 // ones (lacking an extra underscore).
37 "_DllMainCRTStartup@12",
41 "_NULL_IMPORT_DESCRIPTOR",
42 "_pei386_runtime_relocator",
49 // These are the MinGW names that differ from the standard
50 // ones (lacking an extra underscore).
67 "libclang_rt.builtins-aarch64",
68 "libclang_rt.builtins-arm",
69 "libclang_rt.builtins-i386",
70 "libclang_rt.builtins-x86_64",
93 bool AutoExporter::shouldExport(Defined *Sym) const {
94 if (!Sym || !Sym->isLive() || !Sym->getChunk())
97 // Only allow the symbol kinds that make sense to export; in particular,
98 // disallow import symbols.
99 if (!isa<DefinedRegular>(Sym) && !isa<DefinedCommon>(Sym))
101 if (ExcludeSymbols.count(Sym->getName()))
104 // Don't export anything that looks like an import symbol (which also can be
105 // a manually defined data symbol with such a name).
106 if (Sym->getName().startswith("__imp_"))
109 // If a corresponding __imp_ symbol exists and is defined, don't export it.
110 if (Symtab->find(("__imp_" + Sym->getName()).str()))
113 // Check that file is non-null before dereferencing it, symbols not
114 // originating in regular object files probably shouldn't be exported.
118 StringRef LibName = sys::path::filename(Sym->getFile()->ParentName);
120 // Drop the file extension.
121 LibName = LibName.substr(0, LibName.rfind('.'));
122 if (!LibName.empty())
123 return !ExcludeLibs.count(LibName);
125 StringRef FileName = sys::path::filename(Sym->getFile()->getName());
126 return !ExcludeObjects.count(FileName);
129 void coff::writeDefFile(StringRef Name) {
131 raw_fd_ostream OS(Name, EC, sys::fs::F_None);
133 fatal("cannot open " + Name + ": " + EC.message());
136 for (Export &E : Config->Exports) {
137 OS << " " << E.ExportName << " "
139 if (auto *Def = dyn_cast_or_null<Defined>(E.Sym)) {
140 if (Def && Def->getChunk() &&
141 !(Def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))