2015-08-14 22:12:54 +08:00
|
|
|
//===- SymbolTable.h --------------------------------------------*- C++ -*-===//
|
2015-05-29 03:09:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_COFF_SYMBOL_TABLE_H
|
|
|
|
#define LLD_COFF_SYMBOL_TABLE_H
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
2017-02-03 07:58:14 +08:00
|
|
|
#include "LTO.h"
|
2016-12-12 06:15:30 +08:00
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2015-06-27 10:05:40 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2015-06-27 02:58:24 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-06-10 01:52:17 +08:00
|
|
|
namespace llvm {
|
|
|
|
struct LTOCodeGenerator;
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-06-30 02:50:11 +08:00
|
|
|
class Chunk;
|
2016-12-10 05:55:24 +08:00
|
|
|
class CommonChunk;
|
2015-06-30 02:50:11 +08:00
|
|
|
class Defined;
|
2016-12-09 04:20:22 +08:00
|
|
|
class DefinedAbsolute;
|
|
|
|
class DefinedRelative;
|
2015-06-30 02:50:11 +08:00
|
|
|
class Lazy;
|
2016-11-22 01:22:35 +08:00
|
|
|
class SectionChunk;
|
2017-11-04 05:21:47 +08:00
|
|
|
class Symbol;
|
2015-06-30 02:50:11 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// SymbolTable is a bucket of all known symbols, including defined,
|
|
|
|
// undefined, or lazy symbols (the last one is symbols in archive
|
|
|
|
// files whose archive members are not yet loaded).
|
|
|
|
//
|
|
|
|
// We put all symbols of all files to a SymbolTable, and the
|
|
|
|
// SymbolTable selects the "best" symbols if there are name
|
|
|
|
// conflicts. For example, obviously, a defined symbol is better than
|
|
|
|
// an undefined symbol. Or, if there's a conflict between a lazy and a
|
|
|
|
// undefined, it'll read an archive member to read a real definition
|
2016-12-10 05:55:24 +08:00
|
|
|
// to replace the lazy symbol. The logic is implemented in the
|
|
|
|
// add*() functions, which are called by input files as they are parsed.
|
|
|
|
// There is one add* function per symbol type.
|
2015-05-29 03:09:30 +08:00
|
|
|
class SymbolTable {
|
|
|
|
public:
|
2016-12-09 04:20:22 +08:00
|
|
|
void addFile(InputFile *File);
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
// Try to resolve any undefined symbols and update the symbol table
|
|
|
|
// accordingly, then print an error message for any remaining undefined
|
|
|
|
// symbols.
|
|
|
|
void reportRemainingUndefines();
|
2015-05-29 03:09:30 +08:00
|
|
|
|
[COFF] Support MinGW automatic dllimport of data
Normally, in order to reference exported data symbols from a different
DLL, the declarations need to have the dllimport attribute, in order to
use the __imp_<var> symbol (which contains an address to the actual
variable) instead of the variable itself directly. This isn't an issue
in the same way for functions, since any reference to the function without
the dllimport attribute will end up as a reference to a thunk which loads
the actual target function from the import address table (IAT).
GNU ld, in MinGW environments, supports automatically importing data
symbols from DLLs, even if the references didn't have the appropriate
dllimport attribute. Since the PE/COFF format doesn't support the kind
of relocations that this would require, the MinGW's CRT startup code
has an custom framework of their own for manually fixing the missing
relocations once module is loaded and the target addresses in the IAT
are known.
For this to work, the linker (originall in GNU ld) creates a list of
remaining references needing fixup, which the runtime processes on
startup before handing over control to user code.
While this feature is rather controversial, it's one of the main features
allowing unix style libraries to be used on windows without any extra
porting effort.
Some sort of automatic fixing of data imports is also necessary for the
itanium C++ ABI on windows (as clang implements it right now) for importing
vtable pointers in certain cases, see D43184 for some discussion on that.
The runtime pseudo relocation handler supports 8/16/32/64 bit addresses,
either PC relative references (like IMAGE_REL_*_REL32*) or absolute
references (IMAGE_REL_AMD64_ADDR32, IMAGE_REL_AMD64_ADDR32,
IMAGE_REL_I386_DIR32). On linking, the relocation is handled as a
relocation against the corresponding IAT slot. For the absolute references,
a normal base relocation is created, to update the embedded address
in case the image is loaded at a different address.
The list of runtime pseudo relocations contains the RVA of the
imported symbol (the IAT slot), the RVA of the location the relocation
should be applied to, and a size of the memory location. When the
relocations are fixed at runtime, the difference between the actual
IAT slot value and the IAT slot address is added to the reference,
doing the right thing for both absolute and relative references.
With this patch alone, things work fine for i386 binaries, and mostly
for x86_64 binaries, with feature parity with GNU ld. Despite this,
there are a few gotchas:
- References to data from within code works fine on both x86 architectures,
since their relocations consist of plain 32 or 64 bit absolute/relative
references. On ARM and AArch64, references to data doesn't consist of
a plain 32 or 64 bit embedded address or offset in the code. On ARMNT,
it's usually a MOVW+MOVT instruction pair represented by a
IMAGE_REL_ARM_MOV32T relocation, each instruction containing 16 bit of
the target address), on AArch64, it's usually an ADRP+ADD/LDR/STR
instruction pair with an even more complex encoding, storing a PC
relative address (with a range of +/- 4 GB). This could theoretically
be remedied by extending the runtime pseudo relocation handler with new
relocation types, to support these instruction encodings. This isn't an
issue for GCC/GNU ld since they don't support windows on ARMNT/AArch64.
- For x86_64, if references in code are encoded as 32 bit PC relative
offsets, the runtime relocation will fail if the target turns out to be
out of range for a 32 bit offset.
- Fixing up the relocations at runtime requires making sections writable
if necessary, with the VirtualProtect function. In Windows Store/UWP apps,
this function is forbidden.
These limitations are addressed by a few later patches in lld and
llvm.
Differential Revision: https://reviews.llvm.org/D50917
llvm-svn: 340726
2018-08-27 16:43:31 +08:00
|
|
|
void loadMinGWAutomaticImports();
|
|
|
|
bool handleMinGWAutomaticImport(Symbol *Sym, StringRef Name);
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Returns a list of chunks of selected symbols.
|
|
|
|
std::vector<Chunk *> getChunks();
|
|
|
|
|
2015-07-02 11:59:04 +08:00
|
|
|
// Returns a symbol for a given name. Returns a nullptr if not found.
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *find(StringRef Name);
|
|
|
|
Symbol *findUnderscore(StringRef Name);
|
2015-05-31 11:34:08 +08:00
|
|
|
|
2015-07-02 08:04:14 +08:00
|
|
|
// Occasionally we have to resolve an undefined symbol to its
|
|
|
|
// mangled symbol. This function tries to find a mangled name
|
|
|
|
// for U from the symbol table, and if found, set the symbol as
|
|
|
|
// a weak alias for U.
|
2017-11-04 05:21:47 +08:00
|
|
|
void mangleMaybe(Symbol *B);
|
2015-07-14 10:58:13 +08:00
|
|
|
StringRef findMangle(StringRef Name);
|
2015-06-29 06:16:41 +08:00
|
|
|
|
2015-08-29 06:16:09 +08:00
|
|
|
// Build a set of COFF objects representing the combined contents of
|
|
|
|
// BitcodeFiles and add them to the symbol table. Called after all files are
|
|
|
|
// added and before the writer writes results to a file.
|
|
|
|
void addCombinedLTOObjects();
|
2017-02-07 04:47:55 +08:00
|
|
|
std::vector<StringRef> compileBitcodeFiles();
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2015-06-01 03:55:40 +08:00
|
|
|
// Creates an Undefined symbol for a given name.
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *addUndefined(StringRef Name);
|
2016-12-10 05:55:24 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *addSynthetic(StringRef N, Chunk *C);
|
|
|
|
Symbol *addAbsolute(StringRef N, uint64_t VA);
|
2016-12-10 05:55:24 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
|
2016-12-10 05:55:24 +08:00
|
|
|
void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
|
2017-11-28 09:30:07 +08:00
|
|
|
Symbol *addRegular(InputFile *F, StringRef N,
|
2017-11-04 05:21:47 +08:00
|
|
|
const llvm::object::coff_symbol_generic *S = nullptr,
|
|
|
|
SectionChunk *C = nullptr);
|
2017-11-28 09:30:07 +08:00
|
|
|
std::pair<Symbol *, bool>
|
|
|
|
addComdat(InputFile *F, StringRef N,
|
|
|
|
const llvm::object::coff_symbol_generic *S = nullptr);
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
|
|
|
|
const llvm::object::coff_symbol_generic *S = nullptr,
|
|
|
|
CommonChunk *C = nullptr);
|
2018-07-10 18:40:11 +08:00
|
|
|
Symbol *addImportData(StringRef N, ImportFile *F);
|
|
|
|
Symbol *addImportThunk(StringRef Name, DefinedImportData *S,
|
|
|
|
uint16_t Machine);
|
2016-12-10 05:55:24 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
void reportDuplicate(Symbol *Existing, InputFile *NewFile);
|
2015-06-01 03:55:40 +08:00
|
|
|
|
2015-06-25 11:31:47 +08:00
|
|
|
// A list of chunks which to be added to .rdata.
|
|
|
|
std::vector<Chunk *> LocalImportChunks;
|
|
|
|
|
2017-07-28 02:25:59 +08:00
|
|
|
// Iterates symbols in non-determinstic hash table order.
|
|
|
|
template <typename T> void forEachSymbol(T Callback) {
|
2017-11-28 07:16:06 +08:00
|
|
|
for (auto &Pair : SymMap)
|
2017-07-28 02:25:59 +08:00
|
|
|
Callback(Pair.second);
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
private:
|
2018-09-07 04:23:56 +08:00
|
|
|
/// Inserts symbol if not already present.
|
|
|
|
std::pair<Symbol *, bool> insert(StringRef Name);
|
|
|
|
/// Same as insert(Name), but also sets IsUsedInRegularObj.
|
2018-08-03 04:39:19 +08:00
|
|
|
std::pair<Symbol *, bool> insert(StringRef Name, InputFile *F);
|
2015-07-14 10:58:13 +08:00
|
|
|
StringRef findByPrefix(StringRef Prefix);
|
2015-07-01 03:35:21 +08:00
|
|
|
|
2017-11-28 07:16:06 +08:00
|
|
|
llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
|
2017-02-03 07:58:14 +08:00
|
|
|
std::unique_ptr<BitcodeCompiler> LTO;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
extern SymbolTable *Symtab;
|
|
|
|
|
2015-06-13 05:37:55 +08:00
|
|
|
} // namespace coff
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|