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
|
|
|
|
|
2017-05-25 06:30:06 +08:00
|
|
|
#include "Config.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2016-12-12 11:16:14 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2017-02-03 07:58:14 +08:00
|
|
|
#include "llvm/LTO/LTO.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>
|
|
|
|
|
2017-06-13 23:49:13 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace pdb {
|
|
|
|
class DbiModuleDescriptorBuilder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2017-08-31 04:55:18 +08:00
|
|
|
std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File);
|
|
|
|
|
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;
|
2016-12-10 05:55:24 +08:00
|
|
|
using llvm::object::coff_import_header;
|
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;
|
2016-11-22 01:22:35 +08:00
|
|
|
class SectionChunk;
|
2017-11-04 05:21:47 +08:00
|
|
|
class Symbol;
|
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.
|
2017-12-06 00:50:46 +08:00
|
|
|
StringRef getName() const { return MB.getBufferIdentifier(); }
|
2015-05-29 03:09:30 +08:00
|
|
|
|
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; }
|
|
|
|
|
2017-04-22 05:38:01 +08:00
|
|
|
MemoryBufferRef MB;
|
|
|
|
|
2016-12-08 07:17:02 +08:00
|
|
|
// An archive file name if this file is created from an archive.
|
|
|
|
StringRef ParentName;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
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-05-29 03:09:30 +08:00
|
|
|
protected:
|
2016-12-10 05:55:24 +08:00
|
|
|
InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
|
2015-07-03 04:33:48 +08:00
|
|
|
|
2015-06-21 07:10:05 +08:00
|
|
|
std::string Directives;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Kind FileKind;
|
|
|
|
};
|
|
|
|
|
|
|
|
// .lib or .a file.
|
|
|
|
class ArchiveFile : public InputFile {
|
|
|
|
public:
|
2016-09-16 06:24:51 +08:00
|
|
|
explicit ArchiveFile(MemoryBufferRef 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
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
// Enqueues an archive member load for the given symbol. If we've already
|
|
|
|
// enqueued a load for the same archive member, this function does nothing,
|
|
|
|
// which ensures that we don't load the same member more than once.
|
|
|
|
void addMember(const Archive::Symbol *Sym);
|
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;
|
2016-12-12 11:16:14 +08:00
|
|
|
llvm::DenseSet<uint64_t> Seen;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// .obj or .o file. This may be a member of an archive file.
|
2017-07-27 07:05:24 +08:00
|
|
|
class ObjFile : public InputFile {
|
2015-05-29 03:09:30 +08:00
|
|
|
public:
|
2017-07-27 07:05:24 +08:00
|
|
|
explicit ObjFile(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;
|
2017-12-08 09:09:21 +08:00
|
|
|
ArrayRef<Chunk *> getChunks() { return Chunks; }
|
|
|
|
ArrayRef<SectionChunk *> getDebugChunks() { return DebugChunks; }
|
2018-02-06 09:58:26 +08:00
|
|
|
ArrayRef<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
|
|
|
|
ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
|
2018-02-14 04:32:53 +08:00
|
|
|
ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
|
2017-12-08 09:09:21 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() { return Symbols; }
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
// Returns a Symbol object for the SymbolIndex'th symbol in the
|
2015-05-29 03:09:30 +08:00
|
|
|
// underlying object file.
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *getSymbol(uint32_t SymbolIndex) {
|
2017-11-21 02:52:53 +08:00
|
|
|
return Symbols[SymbolIndex];
|
2015-06-27 11:40:10 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Returns the underying COFF file.
|
2016-12-09 02:49:04 +08:00
|
|
|
COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2017-07-27 08:45:26 +08:00
|
|
|
static std::vector<ObjFile *> Instances;
|
|
|
|
|
2018-02-06 09:58:26 +08:00
|
|
|
// Flags in the absolute @feat.00 symbol if it is present. These usually
|
|
|
|
// indicate if an object was compiled with certain security features enabled
|
|
|
|
// like stack guard, safeseh, /guard:cf, or other things.
|
|
|
|
uint32_t Feat00Flags = 0;
|
2015-07-25 07:51:14 +08:00
|
|
|
|
2018-02-06 09:58:26 +08:00
|
|
|
// True if this object file is compatible with SEH. COFF-specific and
|
|
|
|
// x86-only. COFF spec 5.10.1. The .sxdata section.
|
|
|
|
bool hasSafeSEH() { return Feat00Flags & 0x1; }
|
|
|
|
|
|
|
|
// True if this file was compiled with /guard:cf.
|
|
|
|
bool hasGuardCF() { return Feat00Flags & 0x800; }
|
2015-07-25 07:51:14 +08:00
|
|
|
|
2017-06-13 23:49:13 +08:00
|
|
|
// Pointer to the PDB module descriptor builder. Various debug info records
|
|
|
|
// will reference object files by "module index", which is here. Things like
|
|
|
|
// source files and section contributions are also recorded here. Will be null
|
|
|
|
// if we are not producing a PDB.
|
|
|
|
llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
private:
|
2015-08-06 22:58:50 +08:00
|
|
|
void initializeChunks();
|
|
|
|
void initializeSymbols();
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
SectionChunk *
|
|
|
|
readSection(uint32_t SectionNumber,
|
2018-03-16 05:14:02 +08:00
|
|
|
const llvm::object::coff_aux_section_definition *Def,
|
|
|
|
StringRef LeaderName);
|
2017-11-28 09:30:07 +08:00
|
|
|
|
|
|
|
void readAssociativeDefinition(
|
|
|
|
COFFSymbolRef COFFSym,
|
|
|
|
const llvm::object::coff_aux_section_definition *Def);
|
|
|
|
|
|
|
|
llvm::Optional<Symbol *>
|
|
|
|
createDefined(COFFSymbolRef Sym,
|
|
|
|
std::vector<const llvm::object::coff_aux_section_definition *>
|
|
|
|
&ComdatDefs);
|
|
|
|
Symbol *createRegular(COFFSymbolRef Sym);
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *createUndefined(COFFSymbolRef Sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2016-12-09 02:49:04 +08:00
|
|
|
std::unique_ptr<COFFObjectFile> COFFObj;
|
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;
|
|
|
|
|
2016-11-22 01:22:35 +08:00
|
|
|
// CodeView debug info sections.
|
|
|
|
std::vector<SectionChunk *> DebugChunks;
|
|
|
|
|
2018-02-06 09:58:26 +08:00
|
|
|
// Chunks containing symbol table indices of exception handlers. Only used for
|
|
|
|
// 32-bit x86.
|
|
|
|
std::vector<SectionChunk *> SXDataChunks;
|
|
|
|
|
2018-02-14 04:32:53 +08:00
|
|
|
// Chunks containing symbol table indices of address taken symbols and longjmp
|
|
|
|
// targets. These are not linked into the final binary when /guard:cf is set.
|
2018-02-06 09:58:26 +08:00
|
|
|
std::vector<SectionChunk *> GuardFidChunks;
|
2018-02-14 04:32:53 +08:00
|
|
|
std::vector<SectionChunk *> GuardLJmpChunks;
|
2018-02-06 09:58:26 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// 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.)
|
2017-11-20 13:31:23 +08:00
|
|
|
std::vector<SectionChunk *> SparseChunks;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2017-11-21 02:52:53 +08:00
|
|
|
// This vector contains a list of all symbols defined or referenced by this
|
|
|
|
// file. They are indexed such that you can get a Symbol by symbol
|
2015-05-29 03:09:30 +08:00
|
|
|
// index. Nonexistent indices (which are occupied by auxiliary
|
|
|
|
// symbols in the real symbol table) are filled with null pointers.
|
2017-11-21 02:52:53 +08:00
|
|
|
std::vector<Symbol *> Symbols;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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:
|
2018-05-11 03:01:28 +08:00
|
|
|
explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
|
2017-05-25 06:30:06 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
|
|
|
|
|
2017-07-27 08:45:26 +08:00
|
|
|
static std::vector<ImportFile *> Instances;
|
|
|
|
|
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
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
public:
|
|
|
|
StringRef ExternalName;
|
|
|
|
const coff_import_header *Hdr;
|
|
|
|
Chunk *Location = nullptr;
|
2017-05-25 06:30:06 +08:00
|
|
|
|
|
|
|
// We want to eliminate dllimported symbols if no one actually refers them.
|
2018-05-11 03:01:28 +08:00
|
|
|
// These "Live" bits are used to keep track of which import library members
|
2017-05-25 06:30:06 +08:00
|
|
|
// are actually in use.
|
|
|
|
//
|
|
|
|
// If the Live bit is turned off by MarkLive, Writer will ignore dllimported
|
2018-05-11 03:01:28 +08:00
|
|
|
// symbols provided by this import library member. We also track whether the
|
|
|
|
// imported symbol is used separately from whether the thunk is used in order
|
|
|
|
// to avoid creating unnecessary thunks.
|
|
|
|
bool Live = !Config->DoGC;
|
|
|
|
bool ThunkLive = !Config->DoGC;
|
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; }
|
2018-04-12 03:52:53 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() { return Symbols; }
|
2015-07-10 03:54:13 +08:00
|
|
|
MachineTypes getMachineType() override;
|
2017-07-27 08:45:26 +08:00
|
|
|
static std::vector<BitcodeFile *> Instances;
|
2017-02-03 07:58:14 +08:00
|
|
|
std::unique_ptr<llvm::lto::InputFile> Obj;
|
2016-04-15 05:41:44 +08:00
|
|
|
|
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
|
|
|
|
2018-04-12 03:52:53 +08:00
|
|
|
std::vector<Symbol *> Symbols;
|
2015-06-02 04:10:10 +08:00
|
|
|
};
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
2017-01-06 18:04:08 +08:00
|
|
|
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string toString(const coff::InputFile *File);
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|