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"
|
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"
|
2017-10-20 03:49:38 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::COFF;
|
|
|
|
|
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",
|
|
|
|
"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_",
|
|
|
|
// Artifical symbols such as .refptr
|
|
|
|
".",
|
|
|
|
};
|
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 {
|
|
|
|
if (!Sym || !Sym->isLive() || !Sym->getChunk())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void coff::writeDefFile(StringRef Name) {
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Name, EC, sys::fs::F_None);
|
|
|
|
if (EC)
|
|
|
|
fatal("cannot open " + Name + ": " + EC.message());
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|