2015-08-14 22:12:54 +08:00
|
|
|
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_INPUT_FILES_H
|
|
|
|
#define LLD_ELF_INPUT_FILES_H
|
|
|
|
|
2015-10-07 17:13:03 +08:00
|
|
|
#include "Config.h"
|
2015-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "Error.h"
|
2015-08-28 10:40:04 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Object/Archive.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
using llvm::object::Archive;
|
|
|
|
|
2015-10-01 01:06:09 +08:00
|
|
|
class InputFile;
|
2015-09-05 06:28:10 +08:00
|
|
|
class Lazy;
|
2015-07-25 05:03:07 +08:00
|
|
|
class SymbolBody;
|
|
|
|
|
|
|
|
// The root class of input files.
|
|
|
|
class InputFile {
|
|
|
|
public:
|
2015-09-05 06:28:10 +08:00
|
|
|
enum Kind { ObjectKind, SharedKind, ArchiveKind };
|
2015-07-25 05:03:07 +08:00
|
|
|
Kind kind() const { return FileKind; }
|
|
|
|
|
2015-08-05 20:03:34 +08:00
|
|
|
StringRef getName() const { return MB.getBufferIdentifier(); }
|
|
|
|
|
2016-02-02 16:22:41 +08:00
|
|
|
// Filename of .a which contained this file. If this file was
|
|
|
|
// not in an archive file, it is the empty string. We use this
|
|
|
|
// string for creating error messages.
|
2016-02-03 04:24:31 +08:00
|
|
|
StringRef ArchiveName;
|
2016-02-02 16:22:41 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
protected:
|
2015-09-30 10:06:17 +08:00
|
|
|
InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
MemoryBufferRef MB;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Kind FileKind;
|
|
|
|
};
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <typename ELFT> class ELFFileBase : public InputFile {
|
2015-09-04 04:03:54 +08:00
|
|
|
public:
|
2015-10-12 09:55:32 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
2015-11-03 22:13:40 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
2015-10-12 09:55:32 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
|
2015-10-13 09:17:02 +08:00
|
|
|
ELFFileBase(Kind K, MemoryBufferRef M);
|
2015-09-04 04:03:54 +08:00
|
|
|
static bool classof(const InputFile *F) {
|
|
|
|
Kind K = F->kind();
|
|
|
|
return K == ObjectKind || K == SharedKind;
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:10:52 +08:00
|
|
|
static ELFKind getELFKind();
|
2015-10-12 09:55:32 +08:00
|
|
|
const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; }
|
|
|
|
llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }
|
|
|
|
|
|
|
|
uint16_t getEMachine() const { return getObj().getHeader()->e_machine; }
|
|
|
|
uint8_t getOSABI() const {
|
|
|
|
return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getStringTable() const { return StringTable; }
|
2015-09-23 00:53:55 +08:00
|
|
|
|
2015-11-03 22:13:40 +08:00
|
|
|
uint32_t getSectionIndex(const Elf_Sym &Sym) const;
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
protected:
|
2015-10-12 23:15:45 +08:00
|
|
|
llvm::object::ELFFile<ELFT> ELFObj;
|
|
|
|
const Elf_Shdr *Symtab = nullptr;
|
2015-11-03 22:13:40 +08:00
|
|
|
ArrayRef<Elf_Word> SymtabSHNDX;
|
2015-10-12 23:15:45 +08:00
|
|
|
StringRef StringTable;
|
|
|
|
void initStringTable();
|
|
|
|
Elf_Sym_Range getNonLocalSymbols();
|
|
|
|
Elf_Sym_Range getSymbolsHelper(bool);
|
2015-10-12 20:14:30 +08:00
|
|
|
};
|
|
|
|
|
2015-10-12 10:22:58 +08:00
|
|
|
// .o file.
|
|
|
|
template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
|
|
|
|
typedef ELFFileBase<ELFT> Base;
|
2015-07-25 05:03:07 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
2015-08-25 05:43:25 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
2015-10-20 05:00:02 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-01-07 04:30:02 +08:00
|
|
|
// uint32 in ELFT's byte order
|
2015-10-10 03:25:07 +08:00
|
|
|
typedef llvm::support::detail::packed_endian_specific_integral<
|
2016-01-07 04:30:02 +08:00
|
|
|
uint32_t, ELFT::TargetEndianness, 2>
|
|
|
|
uint32_X;
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
StringRef getShtGroupSignature(const Elf_Shdr &Sec);
|
2016-01-07 04:30:02 +08:00
|
|
|
ArrayRef<uint32_X> getShtGroupEntries(const Elf_Shdr &Sec);
|
2015-10-10 03:25:07 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
2015-09-03 04:43:43 +08:00
|
|
|
static bool classof(const InputFile *F) {
|
2015-10-13 09:17:02 +08:00
|
|
|
return F->kind() == Base::ObjectKind;
|
2015-09-03 04:43:43 +08:00
|
|
|
}
|
2015-08-05 20:03:34 +08:00
|
|
|
|
2016-01-06 09:14:11 +08:00
|
|
|
ArrayRef<SymbolBody *> getSymbols() { return SymbolBodies; }
|
2015-10-12 10:22:58 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
explicit ObjectFile(MemoryBufferRef M);
|
2016-01-06 10:06:33 +08:00
|
|
|
void parse(llvm::DenseSet<StringRef> &ComdatGroups);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
|
|
|
|
InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
|
2015-08-05 21:55:34 +08:00
|
|
|
|
2015-09-18 22:40:19 +08:00
|
|
|
SymbolBody *getSymbolBody(uint32_t SymbolIndex) const {
|
2015-09-08 23:50:05 +08:00
|
|
|
uint32_t FirstNonLocal = this->Symtab->sh_info;
|
2015-08-28 07:15:56 +08:00
|
|
|
if (SymbolIndex < FirstNonLocal)
|
|
|
|
return nullptr;
|
2016-01-06 09:14:11 +08:00
|
|
|
return SymbolBodies[SymbolIndex - FirstNonLocal];
|
2015-08-28 07:15:56 +08:00
|
|
|
}
|
|
|
|
|
2015-09-17 04:45:57 +08:00
|
|
|
Elf_Sym_Range getLocalSymbols();
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
const Elf_Sym *getLocalSymbol(uintX_t SymIndex);
|
2015-09-17 04:45:57 +08:00
|
|
|
|
2015-09-19 02:28:08 +08:00
|
|
|
const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
|
2015-09-18 09:08:17 +08:00
|
|
|
|
2015-12-25 21:02:13 +08:00
|
|
|
// Get MIPS GP0 value defined by this file. This value represents the gp value
|
|
|
|
// used to create the relocatable object and required to support
|
|
|
|
// R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
|
|
|
|
uint32_t getMipsGp0() const;
|
|
|
|
|
2016-01-29 09:24:25 +08:00
|
|
|
// The number is the offset in the string table. It will be used as the
|
|
|
|
// st_name of the symbol.
|
|
|
|
std::vector<std::pair<const Elf_Sym *, unsigned>> KeptLocalSyms;
|
2016-01-28 02:04:26 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
private:
|
2016-01-06 10:06:33 +08:00
|
|
|
void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups);
|
2015-07-25 05:03:07 +08:00
|
|
|
void initializeSymbols();
|
2015-12-24 16:41:12 +08:00
|
|
|
InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-01-21 10:10:12 +08:00
|
|
|
SymbolBody *createSymbolBody(const Elf_Sym *Sym);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// List of all sections defined by this file.
|
2015-10-20 05:00:02 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *> Sections;
|
2015-08-10 23:12:17 +08:00
|
|
|
|
2015-10-12 10:22:58 +08:00
|
|
|
// List of all symbols referenced or defined by this file.
|
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
|
|
|
|
2015-12-25 21:02:13 +08:00
|
|
|
// MIPS .reginfo section defined by this file.
|
|
|
|
MipsReginfoInputSection<ELFT> *MipsReginfo = nullptr;
|
|
|
|
|
2015-10-12 10:22:58 +08:00
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2015-12-23 09:18:40 +08:00
|
|
|
llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
|
2015-12-23 11:40:17 +08:00
|
|
|
llvm::SpecificBumpPtrAllocator<EHInputSection<ELFT>> EHAlloc;
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
class ArchiveFile : public InputFile {
|
|
|
|
public:
|
|
|
|
explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
|
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
|
2015-10-10 03:25:07 +08:00
|
|
|
void parse();
|
2015-09-05 06:28:10 +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.)
|
|
|
|
MemoryBufferRef getMember(const Archive::Symbol *Sym);
|
|
|
|
|
|
|
|
llvm::MutableArrayRef<Lazy> getLazySymbols() { return LazySymbols; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Archive> File;
|
|
|
|
std::vector<Lazy> LazySymbols;
|
|
|
|
llvm::DenseSet<uint64_t> Seen;
|
|
|
|
};
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
// .so file.
|
2015-10-12 10:22:58 +08:00
|
|
|
template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
|
2015-10-12 09:55:32 +08:00
|
|
|
typedef ELFFileBase<ELFT> Base;
|
2015-09-08 23:50:05 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-11-03 22:13:40 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
2015-09-08 23:50:05 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
|
|
|
|
std::vector<SharedSymbol<ELFT>> SymbolBodies;
|
2015-10-14 00:34:14 +08:00
|
|
|
std::vector<StringRef> Undefs;
|
2015-10-12 10:22:58 +08:00
|
|
|
StringRef SoName;
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
public:
|
2015-10-12 10:22:58 +08:00
|
|
|
StringRef getSoName() const { return SoName; }
|
2015-09-08 23:50:05 +08:00
|
|
|
llvm::MutableArrayRef<SharedSymbol<ELFT>> getSharedSymbols() {
|
|
|
|
return SymbolBodies;
|
|
|
|
}
|
2015-11-03 22:13:40 +08:00
|
|
|
const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
|
2015-10-14 00:34:14 +08:00
|
|
|
llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
static bool classof(const InputFile *F) {
|
2015-10-13 09:17:02 +08:00
|
|
|
return F->kind() == Base::SharedKind;
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
explicit SharedFile(MemoryBufferRef M);
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2015-10-12 10:22:58 +08:00
|
|
|
void parseSoName();
|
2016-01-06 09:56:36 +08:00
|
|
|
void parseRest();
|
2015-10-12 10:22:58 +08:00
|
|
|
|
|
|
|
// Used for --as-needed
|
|
|
|
bool AsNeeded = false;
|
|
|
|
bool IsUsed = false;
|
|
|
|
bool isNeeded() const { return !AsNeeded || IsUsed; }
|
2015-09-04 04:03:54 +08:00
|
|
|
};
|
|
|
|
|
2016-02-02 16:22:41 +08:00
|
|
|
std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB,
|
|
|
|
StringRef ArchiveName = "");
|
2016-01-06 08:09:43 +08:00
|
|
|
std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB);
|
2015-09-05 06:28:10 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|