1 //===- MinGW.cpp ----------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MinGW.h"
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"
15 
16 using namespace llvm;
17 using namespace llvm::COFF;
18 
19 namespace lld {
20 namespace coff {
21 
22 AutoExporter::AutoExporter() {
23   excludeLibs = {
24       "libgcc",
25       "libgcc_s",
26       "libstdc++",
27       "libmingw32",
28       "libmingwex",
29       "libg2c",
30       "libsupc++",
31       "libobjc",
32       "libgcj",
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",
38       "libc++",
39       "libc++abi",
40       "libunwind",
41       "libmsvcrt",
42       "libucrtbase",
43   };
44 
45   excludeObjects = {
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",
48   };
49 
50   excludeSymbolPrefixes = {
51       // Import symbols
52       "__imp_",
53       "__IMPORT_DESCRIPTOR_",
54       // Extra import symbols from GNU import libraries
55       "__nm_",
56       // C++ symbols
57       "__rtti_",
58       "__builtin_",
59       // Artificial symbols such as .refptr
60       ".",
61   };
62 
63   excludeSymbolSuffixes = {
64       "_iname",
65       "_NULL_THUNK_DATA",
66   };
67 
68   if (config->machine == I386) {
69     excludeSymbols = {
70         "__NULL_IMPORT_DESCRIPTOR",
71         "__pei386_runtime_relocator",
72         "_do_pseudo_reloc",
73         "_impure_ptr",
74         "__impure_ptr",
75         "__fmode",
76         "_environ",
77         "___dso_handle",
78         // These are the MinGW names that differ from the standard
79         // ones (lacking an extra underscore).
80         "_DllMain@12",
81         "_DllEntryPoint@12",
82         "_DllMainCRTStartup@12",
83     };
84     excludeSymbolPrefixes.insert("__head_");
85   } else {
86     excludeSymbols = {
87         "__NULL_IMPORT_DESCRIPTOR",
88         "_pei386_runtime_relocator",
89         "do_pseudo_reloc",
90         "impure_ptr",
91         "_impure_ptr",
92         "_fmode",
93         "environ",
94         "__dso_handle",
95         // These are the MinGW names that differ from the standard
96         // ones (lacking an extra underscore).
97         "DllMain",
98         "DllEntryPoint",
99         "DllMainCRTStartup",
100     };
101     excludeSymbolPrefixes.insert("_head_");
102   }
103 }
104 
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);
110 }
111 
112 bool AutoExporter::shouldExport(Defined *sym) const {
113   if (!sym || !sym->isLive() || !sym->getChunk())
114     return false;
115 
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))
119     return false;
120   if (excludeSymbols.count(sym->getName()))
121     return false;
122 
123   for (StringRef prefix : excludeSymbolPrefixes.keys())
124     if (sym->getName().startswith(prefix))
125       return false;
126   for (StringRef suffix : excludeSymbolSuffixes.keys())
127     if (sym->getName().endswith(suffix))
128       return false;
129 
130   // If a corresponding __imp_ symbol exists and is defined, don't export it.
131   if (symtab->find(("__imp_" + sym->getName()).str()))
132     return false;
133 
134   // Check that file is non-null before dereferencing it, symbols not
135   // originating in regular object files probably shouldn't be exported.
136   if (!sym->getFile())
137     return false;
138 
139   StringRef libName = sys::path::filename(sym->getFile()->parentName);
140 
141   // Drop the file extension.
142   libName = libName.substr(0, libName.rfind('.'));
143   if (!libName.empty())
144     return !excludeLibs.count(libName);
145 
146   StringRef fileName = sys::path::filename(sym->getFile()->getName());
147   return !excludeObjects.count(fileName);
148 }
149 
150 void writeDefFile(StringRef name) {
151   std::error_code ec;
152   raw_fd_ostream os(name, ec, sys::fs::OF_None);
153   if (ec)
154     fatal("cannot open " + name + ": " + ec.message());
155 
156   os << "EXPORTS\n";
157   for (Export &e : config->exports) {
158     os << "    " << e.exportName << " "
159        << "@" << e.ordinal;
160     if (auto *def = dyn_cast_or_null<Defined>(e.sym)) {
161       if (def && def->getChunk() &&
162           !(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
163         os << " DATA";
164     }
165     os << "\n";
166   }
167 }
168 
169 } // namespace coff
170 } // namespace lld
171