2015-08-14 22:12:54 +08:00
|
|
|
//===- InputFiles.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_INPUT_FILES_H
|
|
|
|
#define LLD_COFF_INPUT_FILES_H
|
|
|
|
|
|
|
|
#include "lld/Core/LLVM.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2015-06-02 04:10:10 +08:00
|
|
|
#include "llvm/LTO/LTOModule.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
2015-06-13 20:50:13 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
using llvm::LTOModule;
|
2015-07-10 03:54:13 +08:00
|
|
|
using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
using llvm::COFF::MachineTypes;
|
2015-05-29 03:09:30 +08:00
|
|
|
using llvm::object::Archive;
|
|
|
|
using llvm::object::COFFObjectFile;
|
2015-06-30 02:50:11 +08:00
|
|
|
using llvm::object::COFFSymbolRef;
|
2015-07-25 07:51:14 +08:00
|
|
|
using llvm::object::coff_section;
|
2015-06-30 02:50:11 +08:00
|
|
|
|
|
|
|
class Chunk;
|
2015-06-30 06:16:21 +08:00
|
|
|
class Defined;
|
2015-08-17 15:27:45 +08:00
|
|
|
class DefinedImportData;
|
|
|
|
class DefinedImportThunk;
|
2015-07-01 03:35:21 +08:00
|
|
|
class Lazy;
|
2015-06-30 02:50:11 +08:00
|
|
|
class SymbolBody;
|
2015-06-30 06:16:21 +08:00
|
|
|
class Undefined;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// The root class of input files.
|
|
|
|
class InputFile {
|
|
|
|
public:
|
2015-06-02 04:10:10 +08:00
|
|
|
enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
|
2015-05-29 03:09:30 +08:00
|
|
|
Kind kind() const { return FileKind; }
|
|
|
|
virtual ~InputFile() {}
|
|
|
|
|
|
|
|
// Returns the filename.
|
2015-06-08 11:27:57 +08:00
|
|
|
StringRef getName() { return MB.getBufferIdentifier(); }
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Returns symbols defined by this file.
|
|
|
|
virtual std::vector<SymbolBody *> &getSymbols() = 0;
|
|
|
|
|
2015-08-07 03:57:21 +08:00
|
|
|
// Reads a file (the constructor doesn't do that).
|
2015-08-06 22:58:50 +08:00
|
|
|
virtual void parse() = 0;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-07-10 03:54:13 +08:00
|
|
|
// Returns the CPU type this file was compiled to.
|
|
|
|
virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Returns a short, human-friendly filename. If this is a member of
|
|
|
|
// an archive file, a returned value includes parent's filename.
|
|
|
|
// Used for logging or debugging.
|
|
|
|
std::string getShortName();
|
|
|
|
|
|
|
|
// Sets a parent filename if this file is created from an archive.
|
|
|
|
void setParentName(StringRef N) { ParentName = N; }
|
|
|
|
|
2015-06-08 14:00:10 +08:00
|
|
|
// Returns .drectve section contents if exist.
|
2015-06-21 07:10:05 +08:00
|
|
|
StringRef getDirectives() { return StringRef(Directives).trim(); }
|
2015-06-08 14:00:10 +08:00
|
|
|
|
2015-07-03 04:33:48 +08:00
|
|
|
// Each file has a unique index. The index number is used to
|
|
|
|
// resolve ties in symbol resolution.
|
|
|
|
int Index;
|
|
|
|
static int NextIndex;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
protected:
|
2015-07-03 04:33:48 +08:00
|
|
|
InputFile(Kind K, MemoryBufferRef M)
|
|
|
|
: Index(NextIndex++), MB(M), FileKind(K) {}
|
|
|
|
|
2015-06-08 11:27:57 +08:00
|
|
|
MemoryBufferRef MB;
|
2015-06-21 07:10:05 +08:00
|
|
|
std::string Directives;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Kind FileKind;
|
|
|
|
StringRef ParentName;
|
|
|
|
};
|
|
|
|
|
|
|
|
// .lib or .a file.
|
|
|
|
class ArchiveFile : public InputFile {
|
|
|
|
public:
|
2015-06-08 11:27:57 +08:00
|
|
|
explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
|
2015-05-29 03:09:30 +08:00
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
|
2015-08-06 22:58:50 +08:00
|
|
|
void parse() override;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Returns a memory buffer for a given symbol. An empty memory buffer
|
|
|
|
// is returned if we have already returned the same memory buffer.
|
|
|
|
// (So that we don't instantiate same members more than once.)
|
2015-08-06 22:58:50 +08:00
|
|
|
MemoryBufferRef getMember(const Archive::Symbol *Sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-09-03 00:07:11 +08:00
|
|
|
llvm::MutableArrayRef<Lazy> getLazySymbols() { return LazySymbols; }
|
2015-07-01 03:35:21 +08:00
|
|
|
|
|
|
|
// All symbols returned by ArchiveFiles are of Lazy type.
|
|
|
|
std::vector<SymbolBody *> &getSymbols() override {
|
|
|
|
llvm_unreachable("internal error");
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Archive> File;
|
2015-05-29 04:16:25 +08:00
|
|
|
std::string Filename;
|
2015-09-03 00:07:11 +08:00
|
|
|
std::vector<Lazy> LazySymbols;
|
2015-07-15 05:28:07 +08:00
|
|
|
std::map<uint64_t, std::atomic_flag> Seen;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// .obj or .o file. This may be a member of an archive file.
|
|
|
|
class ObjectFile : public InputFile {
|
|
|
|
public:
|
2015-06-08 11:27:57 +08:00
|
|
|
explicit ObjectFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
|
2015-05-29 03:09:30 +08:00
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
|
2015-08-06 22:58:50 +08:00
|
|
|
void parse() override;
|
2015-07-10 03:54:13 +08:00
|
|
|
MachineTypes getMachineType() override;
|
2015-05-29 03:09:30 +08:00
|
|
|
std::vector<Chunk *> &getChunks() { return Chunks; }
|
|
|
|
std::vector<SymbolBody *> &getSymbols() override { return SymbolBodies; }
|
|
|
|
|
|
|
|
// Returns a SymbolBody object for the SymbolIndex'th symbol in the
|
|
|
|
// underlying object file.
|
2015-06-27 11:40:10 +08:00
|
|
|
SymbolBody *getSymbolBody(uint32_t SymbolIndex) {
|
2015-06-30 02:50:11 +08:00
|
|
|
return SparseSymbolBodies[SymbolIndex];
|
2015-06-27 11:40:10 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Returns the underying COFF file.
|
|
|
|
COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
|
|
|
|
|
2015-07-25 07:51:14 +08:00
|
|
|
// True if this object file is compatible with SEH.
|
|
|
|
// COFF-specific and x86-only.
|
|
|
|
bool SEHCompat = false;
|
|
|
|
|
|
|
|
// The list of safe exception handlers listed in .sxdata section.
|
|
|
|
// COFF-specific and x86-only.
|
|
|
|
std::set<SymbolBody *> SEHandlers;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
private:
|
2015-08-06 22:58:50 +08:00
|
|
|
void initializeChunks();
|
|
|
|
void initializeSymbols();
|
|
|
|
void initializeSEH();
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-06-30 06:16:21 +08:00
|
|
|
Defined *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
|
|
|
|
Undefined *createUndefined(COFFSymbolRef Sym);
|
|
|
|
Undefined *createWeakExternal(COFFSymbolRef Sym, const void *Aux);
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
std::unique_ptr<COFFObjectFile> COFFObj;
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2015-07-25 07:51:14 +08:00
|
|
|
const coff_section *SXData = nullptr;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// List of all chunks defined by this file. This includes both section
|
|
|
|
// chunks and non-section chunks for common symbols.
|
|
|
|
std::vector<Chunk *> Chunks;
|
|
|
|
|
|
|
|
// This vector contains the same chunks as Chunks, but they are
|
|
|
|
// indexed such that you can get a SectionChunk by section index.
|
|
|
|
// Nonexistent section indices are filled with null pointers.
|
|
|
|
// (Because section number is 1-based, the first slot is always a
|
|
|
|
// null pointer.)
|
|
|
|
std::vector<Chunk *> SparseChunks;
|
|
|
|
|
|
|
|
// List of all symbols referenced or defined by this file.
|
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
|
|
|
|
|
|
|
// This vector contains the same symbols as SymbolBodies, but they
|
|
|
|
// are indexed such that you can get a SymbolBody by symbol
|
|
|
|
// index. Nonexistent indices (which are occupied by auxiliary
|
|
|
|
// symbols in the real symbol table) are filled with null pointers.
|
|
|
|
std::vector<SymbolBody *> SparseSymbolBodies;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This type represents import library members that contain DLL names
|
|
|
|
// and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
|
|
|
|
// for details about the format.
|
|
|
|
class ImportFile : public InputFile {
|
|
|
|
public:
|
2015-06-13 20:50:13 +08:00
|
|
|
explicit ImportFile(MemoryBufferRef M)
|
|
|
|
: InputFile(ImportKind, M), StringAlloc(StringAllocAux) {}
|
2015-05-29 03:09:30 +08:00
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
|
|
|
|
std::vector<SymbolBody *> &getSymbols() override { return SymbolBodies; }
|
|
|
|
|
2015-08-17 15:27:45 +08:00
|
|
|
DefinedImportData *ImpSym = nullptr;
|
|
|
|
DefinedImportThunk *ThunkSym = nullptr;
|
2015-08-17 16:30:31 +08:00
|
|
|
std::string DLLName;
|
2015-08-17 15:27:45 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
private:
|
2015-08-06 22:58:50 +08:00
|
|
|
void parse() override;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2015-06-13 20:50:13 +08:00
|
|
|
llvm::BumpPtrAllocator StringAllocAux;
|
2015-08-13 09:07:08 +08:00
|
|
|
llvm::StringSaver StringAlloc;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
// Used for LTO.
|
|
|
|
class BitcodeFile : public InputFile {
|
|
|
|
public:
|
2015-06-08 11:27:57 +08:00
|
|
|
explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
|
2015-06-02 04:10:10 +08:00
|
|
|
static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
|
|
|
|
std::vector<SymbolBody *> &getSymbols() override { return SymbolBodies; }
|
2015-07-10 03:54:13 +08:00
|
|
|
MachineTypes getMachineType() override;
|
2015-06-02 04:10:10 +08:00
|
|
|
|
|
|
|
LTOModule *getModule() const { return M.get(); }
|
2015-08-25 06:22:58 +08:00
|
|
|
std::unique_ptr<LTOModule> takeModule() { return std::move(M); }
|
2015-06-02 04:10:10 +08:00
|
|
|
|
|
|
|
private:
|
2015-08-06 22:58:50 +08:00
|
|
|
void parse() override;
|
2015-06-02 04:10:10 +08:00
|
|
|
|
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
|
|
|
std::unique_ptr<LTOModule> M;
|
|
|
|
};
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|