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 "SymbolTable.h"
11 #include "lld/Common/ErrorHandler.h"
12 #include "llvm/Object/COFF.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/raw_ostream.h"
17 using namespace llvm::COFF;
19 using namespace lld::coff;
21 AutoExporter::AutoExporter() {
32 "libclang_rt.builtins",
33 "libclang_rt.builtins-aarch64",
34 "libclang_rt.builtins-arm",
35 "libclang_rt.builtins-i386",
36 "libclang_rt.builtins-x86_64",
37 "libclang_rt.profile",
38 "libclang_rt.profile-aarch64",
39 "libclang_rt.profile-arm",
40 "libclang_rt.profile-i386",
41 "libclang_rt.profile-x86_64",
50 "crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o",
51 "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o",
54 excludeSymbolPrefixes = {
57 "__IMPORT_DESCRIPTOR_",
58 // Extra import symbols from GNU import libraries
63 // Artificial symbols such as .refptr
65 // profile generate symbols
71 excludeSymbolSuffixes = {
76 if (config->machine == I386) {
78 "__NULL_IMPORT_DESCRIPTOR",
79 "__pei386_runtime_relocator",
86 // These are the MinGW names that differ from the standard
87 // ones (lacking an extra underscore).
90 "_DllMainCRTStartup@12",
92 excludeSymbolPrefixes.insert("__head_");
95 "__NULL_IMPORT_DESCRIPTOR",
96 "_pei386_runtime_relocator",
103 // These are the MinGW names that differ from the standard
104 // ones (lacking an extra underscore).
109 excludeSymbolPrefixes.insert("_head_");
113 void AutoExporter::addWholeArchive(StringRef path) {
114 StringRef libName = sys::path::filename(path);
115 // Drop the file extension, to match the processing below.
116 libName = libName.substr(0, libName.rfind('.'));
117 excludeLibs.erase(libName);
120 bool AutoExporter::shouldExport(Defined *sym) const {
121 if (!sym || !sym->isLive() || !sym->getChunk())
124 // Only allow the symbol kinds that make sense to export; in particular,
125 // disallow import symbols.
126 if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym))
128 if (excludeSymbols.count(sym->getName()))
131 for (StringRef prefix : excludeSymbolPrefixes.keys())
132 if (sym->getName().startswith(prefix))
134 for (StringRef suffix : excludeSymbolSuffixes.keys())
135 if (sym->getName().endswith(suffix))
138 // If a corresponding __imp_ symbol exists and is defined, don't export it.
139 if (symtab->find(("__imp_" + sym->getName()).str()))
142 // Check that file is non-null before dereferencing it, symbols not
143 // originating in regular object files probably shouldn't be exported.
147 StringRef libName = sys::path::filename(sym->getFile()->parentName);
149 // Drop the file extension.
150 libName = libName.substr(0, libName.rfind('.'));
151 if (!libName.empty())
152 return !excludeLibs.count(libName);
154 StringRef fileName = sys::path::filename(sym->getFile()->getName());
155 return !excludeObjects.count(fileName);
158 void lld::coff::writeDefFile(StringRef name) {
160 raw_fd_ostream os(name, ec, sys::fs::OF_None);
162 fatal("cannot open " + name + ": " + ec.message());
165 for (Export &e : config->exports) {
166 os << " " << e.exportName << " "
168 if (auto *def = dyn_cast_or_null<Defined>(e.sym)) {
169 if (def && def->getChunk() &&
170 !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))