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