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 lld::coff;
19 using namespace llvm::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",
45 "crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o",
46 "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o",
49 excludeSymbolPrefixes = {
52 "__IMPORT_DESCRIPTOR_",
53 // Extra import symbols from GNU import libraries
58 // Artifical symbols such as .refptr
62 excludeSymbolSuffixes = {
67 if (config->machine == I386) {
69 "__NULL_IMPORT_DESCRIPTOR",
70 "__pei386_runtime_relocator",
77 // These are the MinGW names that differ from the standard
78 // ones (lacking an extra underscore).
81 "_DllMainCRTStartup@12",
83 excludeSymbolPrefixes.insert("__head_");
86 "__NULL_IMPORT_DESCRIPTOR",
87 "_pei386_runtime_relocator",
94 // These are the MinGW names that differ from the standard
95 // ones (lacking an extra underscore).
100 excludeSymbolPrefixes.insert("_head_");
104 void AutoExporter::addWholeArchive(StringRef path) {
105 StringRef libName = sys::path::filename(path);
106 // Drop the file extension, to match the processing below.
107 libName = libName.substr(0, libName.rfind('.'));
108 excludeLibs.erase(libName);
111 bool AutoExporter::shouldExport(Defined *sym) const {
112 if (!sym || !sym->isLive() || !sym->getChunk())
115 // Only allow the symbol kinds that make sense to export; in particular,
116 // disallow import symbols.
117 if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym))
119 if (excludeSymbols.count(sym->getName()))
122 for (StringRef prefix : excludeSymbolPrefixes.keys())
123 if (sym->getName().startswith(prefix))
125 for (StringRef suffix : excludeSymbolSuffixes.keys())
126 if (sym->getName().endswith(suffix))
129 // If a corresponding __imp_ symbol exists and is defined, don't export it.
130 if (symtab->find(("__imp_" + sym->getName()).str()))
133 // Check that file is non-null before dereferencing it, symbols not
134 // originating in regular object files probably shouldn't be exported.
138 StringRef libName = sys::path::filename(sym->getFile()->parentName);
140 // Drop the file extension.
141 libName = libName.substr(0, libName.rfind('.'));
142 if (!libName.empty())
143 return !excludeLibs.count(libName);
145 StringRef fileName = sys::path::filename(sym->getFile()->getName());
146 return !excludeObjects.count(fileName);
149 void coff::writeDefFile(StringRef name) {
151 raw_fd_ostream os(name, ec, sys::fs::F_None);
153 fatal("cannot open " + name + ": " + ec.message());
156 for (Export &e : config->exports) {
157 os << " " << e.exportName << " "
159 if (auto *def = dyn_cast_or_null<Defined>(e.sym)) {
160 if (def && def->getChunk() &&
161 !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))