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;
22 AutoExporter::AutoExporter() {
33 "libclang_rt.builtins",
34 "libclang_rt.builtins-aarch64",
35 "libclang_rt.builtins-arm",
36 "libclang_rt.builtins-i386",
37 "libclang_rt.builtins-x86_64",
46 "crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o",
47 "dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o",
50 excludeSymbolPrefixes = {
53 "__IMPORT_DESCRIPTOR_",
54 // Extra import symbols from GNU import libraries
59 // Artificial symbols such as .refptr
63 excludeSymbolSuffixes = {
68 if (config->machine == I386) {
70 "__NULL_IMPORT_DESCRIPTOR",
71 "__pei386_runtime_relocator",
78 // These are the MinGW names that differ from the standard
79 // ones (lacking an extra underscore).
82 "_DllMainCRTStartup@12",
84 excludeSymbolPrefixes.insert("__head_");
87 "__NULL_IMPORT_DESCRIPTOR",
88 "_pei386_runtime_relocator",
95 // These are the MinGW names that differ from the standard
96 // ones (lacking an extra underscore).
101 excludeSymbolPrefixes.insert("_head_");
105 void AutoExporter::addWholeArchive(StringRef path) {
106 StringRef libName = sys::path::filename(path);
107 // Drop the file extension, to match the processing below.
108 libName = libName.substr(0, libName.rfind('.'));
109 excludeLibs.erase(libName);
112 bool AutoExporter::shouldExport(Defined *sym) const {
113 if (!sym || !sym->isLive() || !sym->getChunk())
116 // Only allow the symbol kinds that make sense to export; in particular,
117 // disallow import symbols.
118 if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym))
120 if (excludeSymbols.count(sym->getName()))
123 for (StringRef prefix : excludeSymbolPrefixes.keys())
124 if (sym->getName().startswith(prefix))
126 for (StringRef suffix : excludeSymbolSuffixes.keys())
127 if (sym->getName().endswith(suffix))
130 // If a corresponding __imp_ symbol exists and is defined, don't export it.
131 if (symtab->find(("__imp_" + sym->getName()).str()))
134 // Check that file is non-null before dereferencing it, symbols not
135 // originating in regular object files probably shouldn't be exported.
139 StringRef libName = sys::path::filename(sym->getFile()->parentName);
141 // Drop the file extension.
142 libName = libName.substr(0, libName.rfind('.'));
143 if (!libName.empty())
144 return !excludeLibs.count(libName);
146 StringRef fileName = sys::path::filename(sym->getFile()->getName());
147 return !excludeObjects.count(fileName);
150 void writeDefFile(StringRef name) {
152 raw_fd_ostream os(name, ec, sys::fs::OF_None);
154 fatal("cannot open " + name + ": " + ec.message());
157 for (Export &e : config->exports) {
158 os << " " << e.exportName << " "
160 if (auto *def = dyn_cast_or_null<Defined>(e.sym)) {
161 if (def && def->getChunk() &&
162 !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))