2017-10-20 03:49:38 +08:00
|
|
|
//===- MinGW.cpp ----------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-10-20 03:49:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MinGW.h"
|
2020-10-06 18:54:49 +08:00
|
|
|
#include "Driver.h"
|
|
|
|
#include "InputFiles.h"
|
2017-11-29 13:50:49 +08:00
|
|
|
#include "SymbolTable.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2020-10-06 18:54:49 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2017-10-20 03:49:38 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2020-10-06 18:54:49 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2017-10-20 03:49:38 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::COFF;
|
2020-02-20 09:05:42 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
2019-10-10 19:27:58 +08:00
|
|
|
|
2019-02-20 06:06:44 +08:00
|
|
|
AutoExporter::AutoExporter() {
|
|
|
|
excludeLibs = {
|
|
|
|
"libgcc",
|
|
|
|
"libgcc_s",
|
|
|
|
"libstdc++",
|
|
|
|
"libmingw32",
|
|
|
|
"libmingwex",
|
|
|
|
"libg2c",
|
|
|
|
"libsupc++",
|
|
|
|
"libobjc",
|
|
|
|
"libgcj",
|
|
|
|
"libclang_rt.builtins",
|
|
|
|
"libclang_rt.builtins-aarch64",
|
|
|
|
"libclang_rt.builtins-arm",
|
|
|
|
"libclang_rt.builtins-i386",
|
|
|
|
"libclang_rt.builtins-x86_64",
|
2020-07-31 04:32:37 +08:00
|
|
|
"libclang_rt.profile",
|
|
|
|
"libclang_rt.profile-aarch64",
|
|
|
|
"libclang_rt.profile-arm",
|
|
|
|
"libclang_rt.profile-i386",
|
|
|
|
"libclang_rt.profile-x86_64",
|
2019-02-20 06:06:44 +08:00
|
|
|
"libc++",
|
|
|
|
"libc++abi",
|
|
|
|
"libunwind",
|
|
|
|
"libmsvcrt",
|
|
|
|
"libucrtbase",
|
|
|
|
};
|
|
|
|
|
|
|
|
excludeObjects = {
|
|
|
|
"crt0.o", "crt1.o", "crt1u.o", "crt2.o", "crt2u.o", "dllcrt1.o",
|
|
|
|
"dllcrt2.o", "gcrt0.o", "gcrt1.o", "gcrt2.o", "crtbegin.o", "crtend.o",
|
|
|
|
};
|
|
|
|
|
2018-09-26 14:13:47 +08:00
|
|
|
excludeSymbolPrefixes = {
|
|
|
|
// Import symbols
|
|
|
|
"__imp_",
|
|
|
|
"__IMPORT_DESCRIPTOR_",
|
|
|
|
// Extra import symbols from GNU import libraries
|
|
|
|
"__nm_",
|
|
|
|
// C++ symbols
|
|
|
|
"__rtti_",
|
|
|
|
"__builtin_",
|
2019-10-10 10:04:56 +08:00
|
|
|
// Artificial symbols such as .refptr
|
2018-09-26 14:13:47 +08:00
|
|
|
".",
|
2020-07-31 04:32:37 +08:00
|
|
|
// profile generate symbols
|
|
|
|
"__profc_",
|
|
|
|
"__profd_",
|
|
|
|
"__profvp_",
|
2018-09-26 14:13:47 +08:00
|
|
|
};
|
2019-02-20 06:06:44 +08:00
|
|
|
|
2018-09-26 14:13:47 +08:00
|
|
|
excludeSymbolSuffixes = {
|
|
|
|
"_iname",
|
|
|
|
"_NULL_THUNK_DATA",
|
|
|
|
};
|
2019-02-20 06:06:44 +08:00
|
|
|
|
2017-11-22 17:06:27 +08:00
|
|
|
if (config->machine == I386) {
|
2017-10-20 03:49:38 +08:00
|
|
|
excludeSymbols = {
|
|
|
|
"__NULL_IMPORT_DESCRIPTOR",
|
|
|
|
"__pei386_runtime_relocator",
|
|
|
|
"_do_pseudo_reloc",
|
|
|
|
"_impure_ptr",
|
|
|
|
"__impure_ptr",
|
|
|
|
"__fmode",
|
|
|
|
"_environ",
|
|
|
|
"___dso_handle",
|
|
|
|
// These are the MinGW names that differ from the standard
|
|
|
|
// ones (lacking an extra underscore).
|
|
|
|
"_DllMain@12",
|
|
|
|
"_DllEntryPoint@12",
|
|
|
|
"_DllMainCRTStartup@12",
|
|
|
|
};
|
2018-09-26 14:13:47 +08:00
|
|
|
excludeSymbolPrefixes.insert("__head_");
|
2017-11-22 17:06:27 +08:00
|
|
|
} else {
|
2017-10-20 03:49:38 +08:00
|
|
|
excludeSymbols = {
|
2018-09-18 15:22:05 +08:00
|
|
|
"__NULL_IMPORT_DESCRIPTOR",
|
2017-10-20 03:49:38 +08:00
|
|
|
"_pei386_runtime_relocator",
|
|
|
|
"do_pseudo_reloc",
|
|
|
|
"impure_ptr",
|
|
|
|
"_impure_ptr",
|
|
|
|
"_fmode",
|
|
|
|
"environ",
|
|
|
|
"__dso_handle",
|
|
|
|
// These are the MinGW names that differ from the standard
|
|
|
|
// ones (lacking an extra underscore).
|
|
|
|
"DllMain",
|
|
|
|
"DllEntryPoint",
|
|
|
|
"DllMainCRTStartup",
|
|
|
|
};
|
2018-09-26 14:13:47 +08:00
|
|
|
excludeSymbolPrefixes.insert("_head_");
|
2017-11-22 17:06:27 +08:00
|
|
|
}
|
2018-09-05 04:56:56 +08:00
|
|
|
}
|
2017-10-20 03:49:38 +08:00
|
|
|
|
2018-09-05 04:56:56 +08:00
|
|
|
void AutoExporter::addWholeArchive(StringRef path) {
|
|
|
|
StringRef libName = sys::path::filename(path);
|
|
|
|
// Drop the file extension, to match the processing below.
|
|
|
|
libName = libName.substr(0, libName.rfind('.'));
|
|
|
|
excludeLibs.erase(libName);
|
|
|
|
}
|
|
|
|
|
2017-10-20 03:49:38 +08:00
|
|
|
bool AutoExporter::shouldExport(Defined *sym) const {
|
2021-04-29 16:57:33 +08:00
|
|
|
if (!sym || !sym->getChunk())
|
2017-10-20 03:49:38 +08:00
|
|
|
return false;
|
2017-11-22 17:06:27 +08:00
|
|
|
|
2017-11-16 15:22:44 +08:00
|
|
|
// Only allow the symbol kinds that make sense to export; in particular,
|
|
|
|
// disallow import symbols.
|
|
|
|
if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym))
|
|
|
|
return false;
|
2017-10-20 03:49:38 +08:00
|
|
|
if (excludeSymbols.count(sym->getName()))
|
|
|
|
return false;
|
2017-11-22 17:06:27 +08:00
|
|
|
|
2018-09-26 14:13:47 +08:00
|
|
|
for (StringRef prefix : excludeSymbolPrefixes.keys())
|
|
|
|
if (sym->getName().startswith(prefix))
|
|
|
|
return false;
|
|
|
|
for (StringRef suffix : excludeSymbolSuffixes.keys())
|
|
|
|
if (sym->getName().endswith(suffix))
|
|
|
|
return false;
|
2017-11-29 13:50:49 +08:00
|
|
|
|
|
|
|
// If a corresponding __imp_ symbol exists and is defined, don't export it.
|
|
|
|
if (symtab->find(("__imp_" + sym->getName()).str()))
|
|
|
|
return false;
|
|
|
|
|
2017-11-16 15:22:44 +08:00
|
|
|
// Check that file is non-null before dereferencing it, symbols not
|
|
|
|
// originating in regular object files probably shouldn't be exported.
|
|
|
|
if (!sym->getFile())
|
|
|
|
return false;
|
2017-11-22 17:06:27 +08:00
|
|
|
|
2017-10-20 03:49:38 +08:00
|
|
|
StringRef libName = sys::path::filename(sym->getFile()->parentName);
|
2017-11-22 17:06:27 +08:00
|
|
|
|
2017-10-20 03:49:38 +08:00
|
|
|
// Drop the file extension.
|
|
|
|
libName = libName.substr(0, libName.rfind('.'));
|
2017-11-22 17:06:42 +08:00
|
|
|
if (!libName.empty())
|
|
|
|
return !excludeLibs.count(libName);
|
2017-11-22 17:06:27 +08:00
|
|
|
|
2017-10-20 03:49:38 +08:00
|
|
|
StringRef fileName = sys::path::filename(sym->getFile()->getName());
|
2017-11-22 17:06:42 +08:00
|
|
|
return !excludeObjects.count(fileName);
|
2017-10-20 03:49:38 +08:00
|
|
|
}
|
|
|
|
|
2020-02-20 09:05:42 +08:00
|
|
|
void lld::coff::writeDefFile(StringRef name) {
|
2017-10-20 03:49:38 +08:00
|
|
|
std::error_code ec;
|
2019-08-05 13:43:48 +08:00
|
|
|
raw_fd_ostream os(name, ec, sys::fs::OF_None);
|
2017-10-20 03:49:38 +08:00
|
|
|
if (ec)
|
|
|
|
fatal("cannot open " + name + ": " + ec.message());
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-10-20 03:49:38 +08:00
|
|
|
os << "EXPORTS\n";
|
|
|
|
for (Export &e : config->exports) {
|
|
|
|
os << " " << e.exportName << " "
|
|
|
|
<< "@" << e.ordinal;
|
|
|
|
if (auto *def = dyn_cast_or_null<Defined>(e.sym)) {
|
|
|
|
if (def && def->getChunk() &&
|
2018-04-20 04:03:24 +08:00
|
|
|
!(def->getChunk()->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
|
2017-10-20 03:49:38 +08:00
|
|
|
os << " DATA";
|
|
|
|
}
|
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 18:54:49 +08:00
|
|
|
|
|
|
|
static StringRef mangle(Twine sym) {
|
|
|
|
assert(config->machine != IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
if (config->machine == I386)
|
|
|
|
return saver.save("_" + sym);
|
|
|
|
return saver.save(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles -wrap option.
|
|
|
|
//
|
|
|
|
// This function instantiates wrapper symbols. At this point, they seem
|
|
|
|
// like they are not being used at all, so we explicitly set some flags so
|
|
|
|
// that LTO won't eliminate them.
|
|
|
|
std::vector<WrappedSymbol>
|
|
|
|
lld::coff::addWrappedSymbols(opt::InputArgList &args) {
|
|
|
|
std::vector<WrappedSymbol> v;
|
|
|
|
DenseSet<StringRef> seen;
|
|
|
|
|
|
|
|
for (auto *arg : args.filtered(OPT_wrap)) {
|
|
|
|
StringRef name = arg->getValue();
|
|
|
|
if (!seen.insert(name).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol *sym = symtab->findUnderscore(name);
|
|
|
|
if (!sym)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol *real = symtab->addUndefined(mangle("__real_" + name));
|
|
|
|
Symbol *wrap = symtab->addUndefined(mangle("__wrap_" + name));
|
|
|
|
v.push_back({sym, real, wrap});
|
|
|
|
|
|
|
|
// These symbols may seem undefined initially, but don't bail out
|
|
|
|
// at symtab->reportUnresolvable() due to them, but let wrapSymbols
|
|
|
|
// below sort things out before checking finally with
|
|
|
|
// symtab->resolveRemainingUndefines().
|
|
|
|
sym->deferUndefined = true;
|
|
|
|
real->deferUndefined = true;
|
|
|
|
// We want to tell LTO not to inline symbols to be overwritten
|
|
|
|
// because LTO doesn't know the final symbol contents after renaming.
|
|
|
|
real->canInline = false;
|
|
|
|
sym->canInline = false;
|
|
|
|
|
|
|
|
// Tell LTO not to eliminate these symbols.
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
if (!isa<Undefined>(wrap))
|
|
|
|
wrap->isUsedInRegularObj = true;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do renaming for -wrap by updating pointers to symbols.
|
|
|
|
//
|
|
|
|
// When this function is executed, only InputFiles and symbol table
|
|
|
|
// contain pointers to symbol objects. We visit them to replace pointers,
|
|
|
|
// so that wrapped symbols are swapped as instructed by the command line.
|
|
|
|
void lld::coff::wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
|
|
|
|
DenseMap<Symbol *, Symbol *> map;
|
|
|
|
for (const WrappedSymbol &w : wrapped) {
|
|
|
|
map[w.sym] = w.wrap;
|
|
|
|
map[w.real] = w.sym;
|
2020-11-17 22:05:29 +08:00
|
|
|
if (Defined *d = dyn_cast<Defined>(w.wrap)) {
|
|
|
|
Symbol *imp = symtab->find(("__imp_" + w.sym->getName()).str());
|
|
|
|
// Create a new defined local import for the wrap symbol. If
|
|
|
|
// no imp prefixed symbol existed, there's no need for it.
|
|
|
|
// (We can't easily distinguish whether any object file actually
|
|
|
|
// referenced it or not, though.)
|
|
|
|
if (imp) {
|
|
|
|
DefinedLocalImport *wrapimp = make<DefinedLocalImport>(
|
|
|
|
saver.save("__imp_" + w.wrap->getName()), d);
|
|
|
|
symtab->localImportChunks.push_back(wrapimp->getChunk());
|
|
|
|
map[imp] = wrapimp;
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 18:54:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update pointers in input files.
|
|
|
|
parallelForEach(ObjFile::instances, [&](ObjFile *file) {
|
|
|
|
MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
|
|
|
|
for (size_t i = 0, e = syms.size(); i != e; ++i)
|
|
|
|
if (Symbol *s = map.lookup(syms[i]))
|
|
|
|
syms[i] = s;
|
|
|
|
});
|
|
|
|
}
|