1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 09:07:10 +03:00
mold/mold.h

964 lines
22 KiB
C
Raw Normal View History

2020-10-04 12:00:33 +03:00
#pragma once
2020-11-09 06:30:13 +03:00
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
2020-10-04 12:00:33 +03:00
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Twine.h"
2020-10-10 06:47:12 +03:00
#include "llvm/Object/Archive.h"
2020-10-09 14:47:45 +03:00
#include "llvm/Object/ELF.h"
2020-10-04 12:00:33 +03:00
#include "llvm/Object/ELFTypes.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
2020-10-10 10:49:02 +03:00
#include "llvm/Support/Timer.h"
2020-10-09 14:47:45 +03:00
#include "tbb/concurrent_hash_map.h"
2020-10-26 13:51:06 +03:00
#include "tbb/global_control.h"
2020-10-10 10:49:02 +03:00
#include "tbb/parallel_for_each.h"
2020-11-06 04:11:19 +03:00
#include "tbb/parallel_invoke.h"
2020-11-06 02:49:41 +03:00
#include "tbb/parallel_reduce.h"
2020-10-28 13:49:58 +03:00
#include "tbb/spin_mutex.h"
2020-10-24 12:58:21 +03:00
#include "tbb/task_group.h"
2020-10-04 12:00:33 +03:00
2020-10-19 17:37:29 +03:00
#include <algorithm>
2020-10-10 13:15:16 +03:00
#include <atomic>
2020-10-04 12:00:33 +03:00
#include <cstdint>
2020-10-21 05:55:03 +03:00
#include <mutex>
2020-10-04 12:00:33 +03:00
#include <string>
2020-11-03 14:45:45 +03:00
#define SECTOR_SIZE 512
#define PAGE_SIZE 4096
2020-11-11 10:53:41 +03:00
#define GOT_SIZE 8
#define PLT_SIZE 16
2020-11-03 14:45:45 +03:00
2020-11-05 02:36:10 +03:00
#define LIKELY(x) __builtin_expect((x), 1)
#define UNLIKELY(x) __builtin_expect((x), 0)
2020-11-03 14:45:45 +03:00
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
using llvm::ArrayRef;
using llvm::ErrorOr;
using llvm::Error;
using llvm::Expected;
using llvm::MemoryBufferRef;
using llvm::SmallVector;
using llvm::StringRef;
using llvm::Twine;
using llvm::object::ELF64LE;
using llvm::object::ELFFile;
2020-11-08 06:26:40 +03:00
class InputChunk;
2020-11-24 10:22:32 +03:00
class InputFile;
2020-11-03 14:45:45 +03:00
class InputSection;
2020-11-08 07:01:46 +03:00
class MergeableSection;
2020-11-24 10:22:32 +03:00
class MergedSection;
2020-11-03 14:45:45 +03:00
class ObjectFile;
2020-11-04 08:03:01 +03:00
class OutputChunk;
2020-11-03 14:45:45 +03:00
class OutputSection;
2020-11-24 10:22:32 +03:00
class SharedFile;
class Symbol;
2020-10-08 11:01:54 +03:00
2020-10-04 12:00:33 +03:00
struct Config {
2020-11-11 03:02:36 +03:00
StringRef dynamic_linker = "/lib64/ld-linux-x86-64.so.2";
2020-10-04 12:00:33 +03:00
StringRef output;
2020-11-30 10:43:47 +03:00
bool as_needed = false;
2020-12-01 15:43:30 +03:00
bool export_dynamic = false;
2020-11-04 12:47:13 +03:00
bool is_static = false;
2020-11-11 03:02:36 +03:00
bool print_map = false;
2020-11-12 07:33:57 +03:00
int filler = -1;
2020-11-20 13:27:39 +03:00
std::string sysroot;
2020-11-19 11:38:07 +03:00
std::vector<StringRef> library_paths;
2020-11-30 11:57:58 +03:00
std::vector<StringRef> rpaths;
2020-11-17 09:05:53 +03:00
u64 image_base = 0x200000;
2020-10-04 12:00:33 +03:00
};
2020-11-29 05:06:11 +03:00
inline Config config;
2020-10-04 12:00:33 +03:00
[[noreturn]] inline void error(const Twine &msg) {
2020-10-27 09:10:34 +03:00
static std::mutex mu;
2020-10-28 13:51:49 +03:00
std::lock_guard lock(mu);
2020-10-04 12:00:33 +03:00
llvm::errs() << msg << "\n";
exit(1);
}
2020-10-09 14:47:45 +03:00
template <class T> T check(ErrorOr<T> e) {
if (auto ec = e.getError())
error(ec.message());
return std::move(*e);
}
template <class T> T check(Expected<T> e) {
if (!e)
error(llvm::toString(e.takeError()));
return std::move(*e);
}
template <class T>
T check2(ErrorOr<T> e, llvm::function_ref<std::string()> prefix) {
if (auto ec = e.getError())
error(prefix() + ": " + ec.message());
return std::move(*e);
}
template <class T>
T check2(Expected<T> e, llvm::function_ref<std::string()> prefix) {
if (!e)
error(prefix() + ": " + toString(e.takeError()));
return std::move(*e);
}
#define CHECK(E, S) check2((E), [&] { return toString(S); })
2020-11-30 11:52:08 +03:00
#define unreachable() \
error(Twine("internal error at ") + __FILE__ + ":" + Twine(__LINE__))
2020-11-24 10:22:32 +03:00
std::string toString(InputFile *);
2020-10-19 15:32:57 +03:00
2020-10-12 07:30:34 +03:00
//
2020-10-19 15:32:57 +03:00
// Interned string
2020-10-12 07:30:34 +03:00
//
2020-10-19 12:13:55 +03:00
namespace tbb {
template<>
struct tbb_hash_compare<StringRef> {
static size_t hash(const StringRef& k) {
return llvm::hash_value(k);
}
static bool equal(const StringRef& k1, const StringRef& k2) {
return k1 == k2;
}
};
}
2020-10-19 15:32:57 +03:00
template<typename ValueT>
class ConcurrentMap {
public:
typedef tbb::concurrent_hash_map<StringRef, ValueT> MapT;
ValueT *insert(StringRef key, const ValueT &val) {
2020-10-28 03:39:31 +03:00
typename MapT::const_accessor acc;
2020-10-19 15:32:57 +03:00
map.insert(acc, std::make_pair(key, val));
2020-10-28 03:39:31 +03:00
return const_cast<ValueT *>(&acc->second);
2020-10-19 15:32:57 +03:00
}
2020-11-07 16:07:19 +03:00
size_t size() const { return map.size(); }
2020-10-19 15:32:57 +03:00
private:
MapT map;
};
//
// Symbol
//
2020-11-07 04:49:34 +03:00
struct StringPiece {
2020-11-08 08:13:59 +03:00
StringPiece(StringRef data) : data(data) {}
2020-11-07 04:49:34 +03:00
StringPiece(const StringPiece &other)
2020-11-07 15:53:21 +03:00
: data(other.data), isec(other.isec.load()),
2020-11-07 13:23:14 +03:00
output_offset(other.output_offset) {}
2020-11-07 04:49:34 +03:00
2020-11-07 14:05:51 +03:00
inline u64 get_addr() const;
2020-11-07 04:49:34 +03:00
StringRef data;
2020-11-08 08:13:59 +03:00
std::atomic<MergeableSection *> isec = ATOMIC_VAR_INIT(nullptr);
2020-11-10 11:18:10 +03:00
u32 output_offset = -1;
2020-11-07 04:49:34 +03:00
};
struct StringPieceRef {
2020-11-07 16:21:41 +03:00
StringPiece *piece = nullptr;
u32 input_offset = 0;
2020-11-24 13:26:26 +03:00
i32 addend = 0;
2020-11-07 04:49:34 +03:00
};
2020-10-12 07:30:34 +03:00
class Symbol {
public:
2020-11-29 04:22:14 +03:00
Symbol(StringRef name)
2020-11-29 11:47:51 +03:00
: name(name), is_placeholder(false), is_imported(false),
2020-11-06 05:44:27 +03:00
is_weak(false), is_undef_weak(false), traced(false) {}
2020-11-03 15:51:40 +03:00
2020-11-29 04:22:14 +03:00
Symbol(const Symbol &other) : Symbol(other.name) {}
2020-10-12 07:30:34 +03:00
2020-10-25 08:42:44 +03:00
static Symbol *intern(StringRef name) {
static ConcurrentMap<Symbol> map;
2020-11-13 06:30:27 +03:00
return map.insert(name, Symbol(name));
2020-10-25 08:42:44 +03:00
}
2020-11-06 06:50:26 +03:00
inline u64 get_addr() const;
2020-11-15 07:01:38 +03:00
inline u64 get_got_addr() const;
inline u64 get_gotplt_addr() const;
2020-11-21 04:48:51 +03:00
inline u64 get_gottpoff_addr() const;
2020-11-21 04:48:23 +03:00
inline u64 get_tlsgd_addr() const;
inline u64 get_tlsld_addr() const;
2020-11-15 07:01:38 +03:00
inline u64 get_plt_addr() const;
2020-11-06 06:50:26 +03:00
2020-10-18 14:19:57 +03:00
StringRef name;
2020-11-24 10:22:32 +03:00
InputFile *file = nullptr;
2020-11-29 11:47:51 +03:00
const ELF64LE::Sym *esym = nullptr;
2020-11-08 09:56:17 +03:00
InputSection *input_section = nullptr;
2020-11-07 16:20:49 +03:00
StringPieceRef piece_ref;
2020-10-23 06:09:27 +03:00
2020-11-26 07:34:42 +03:00
u64 value = -1;
2020-11-15 07:01:38 +03:00
u32 got_idx = -1;
u32 gotplt_idx = -1;
2020-11-21 04:48:51 +03:00
u32 gottpoff_idx = -1;
2020-11-21 04:48:23 +03:00
u32 tlsgd_idx = -1;
u32 tlsld_idx = -1;
2020-11-15 07:01:38 +03:00
u32 plt_idx = -1;
u32 relplt_idx = -1;
2020-11-15 08:07:40 +03:00
u32 dynsym_idx = -1;
2020-11-16 16:25:58 +03:00
u32 dynstr_offset = -1;
2020-11-25 11:20:48 +03:00
u32 copyrel_offset = -1;
2020-11-04 08:41:40 +03:00
u32 shndx = 0;
2020-11-28 16:04:23 +03:00
u16 ver_idx = 0;
2020-10-30 11:59:45 +03:00
2020-11-03 16:24:50 +03:00
tbb::spin_mutex mu;
2020-11-05 07:05:27 +03:00
u8 is_placeholder : 1;
2020-11-20 02:45:58 +03:00
u8 is_imported : 1;
2020-11-03 16:21:55 +03:00
u8 is_weak : 1;
u8 is_undef_weak : 1;
2020-11-05 02:31:32 +03:00
u8 traced : 1;
2020-11-03 15:51:40 +03:00
2020-11-17 13:34:37 +03:00
enum {
NEEDS_GOT = 1 << 0,
2020-11-20 03:46:49 +03:00
NEEDS_PLT = 1 << 1,
2020-11-17 13:34:37 +03:00
NEEDS_GOTTPOFF = 1 << 2,
NEEDS_TLSGD = 1 << 3,
NEEDS_TLSLD = 1 << 4,
2020-11-25 11:20:48 +03:00
NEEDS_COPYREL = 1 << 5,
2020-11-17 13:34:37 +03:00
};
std::atomic_uint8_t flags = ATOMIC_VAR_INIT(0);
2020-11-01 09:27:04 +03:00
u8 type = llvm::ELF::STT_NOTYPE;
2020-10-12 07:30:34 +03:00
};
//
2020-11-06 02:49:41 +03:00
// input_sections.cc
2020-10-12 07:30:34 +03:00
//
2020-11-08 06:26:40 +03:00
class InputChunk {
2020-10-08 11:01:54 +03:00
public:
2020-11-17 07:56:40 +03:00
virtual void copy_buf() {}
2020-11-29 13:58:02 +03:00
inline u64 get_addr() const;
2020-10-23 03:21:40 +03:00
2020-10-19 17:37:29 +03:00
ObjectFile *file;
2020-11-08 06:10:08 +03:00
const ELF64LE::Shdr &shdr;
2020-11-08 06:26:40 +03:00
OutputSection *output_section = nullptr;
2020-11-07 06:14:37 +03:00
2020-10-25 03:26:51 +03:00
StringRef name;
2020-11-07 15:53:21 +03:00
u32 offset;
2020-11-08 08:13:59 +03:00
protected:
2020-11-29 11:47:51 +03:00
InputChunk(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name);
2020-11-08 06:26:40 +03:00
};
class InputSection : public InputChunk {
public:
InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name)
2020-11-29 11:47:51 +03:00
: InputChunk(file, shdr, name) {}
2020-11-08 06:26:40 +03:00
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-26 12:09:32 +03:00
void scan_relocations();
void report_undefined_symbols();
2020-11-08 06:26:40 +03:00
ArrayRef<ELF64LE::Rela> rels;
std::vector<StringPieceRef> rel_pieces;
2020-11-08 09:56:17 +03:00
MergeableSection *mergeable = nullptr;
2020-12-01 15:07:00 +03:00
bool is_comdat_member = false;
2020-12-01 11:16:28 +03:00
bool is_alive = true;
2020-11-08 08:13:59 +03:00
};
class MergeableSection : public InputChunk {
public:
MergeableSection(InputSection *isec, ArrayRef<u8> contents);
2020-11-07 15:53:21 +03:00
2020-11-08 08:13:59 +03:00
MergedSection &parent;
2020-11-07 15:53:21 +03:00
std::vector<StringPieceRef> pieces;
2020-11-08 08:13:59 +03:00
u32 size = 0;
2020-10-08 11:01:54 +03:00
};
2020-11-08 06:36:08 +03:00
std::string toString(InputChunk *isec);
2020-10-19 17:37:29 +03:00
2020-10-12 08:17:34 +03:00
//
2020-10-20 09:33:40 +03:00
// output_chunks.cc
2020-10-12 08:17:34 +03:00
//
2020-10-15 12:30:06 +03:00
class OutputChunk {
2020-10-15 11:06:01 +03:00
public:
2020-11-08 10:09:01 +03:00
enum Kind : u8 { HEADER, REGULAR, SYNTHETIC };
2020-11-08 10:05:36 +03:00
OutputChunk(Kind kind) : kind(kind) { shdr.sh_addralign = 1; }
2020-10-25 08:17:05 +03:00
2020-11-17 07:56:40 +03:00
virtual void copy_buf() {}
2020-11-25 11:20:48 +03:00
virtual void update_shdr() {}
2020-10-25 09:17:43 +03:00
2020-10-22 13:35:16 +03:00
StringRef name;
2020-10-29 09:30:41 +03:00
int shndx = 0;
2020-11-29 13:58:02 +03:00
Kind kind;
2020-10-26 04:03:17 +03:00
bool starts_new_ptload = false;
2020-10-25 07:17:10 +03:00
ELF64LE::Shdr shdr = {};
2020-10-12 08:17:34 +03:00
};
2020-11-16 18:17:01 +03:00
// ELF header
class OutputEhdr : public OutputChunk {
public:
OutputEhdr() : OutputChunk(HEADER) {
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_size = sizeof(ELF64LE::Ehdr);
}
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-16 18:17:01 +03:00
};
2020-11-16 18:23:51 +03:00
// Section header
class OutputShdr : public OutputChunk {
public:
OutputShdr() : OutputChunk(HEADER) {
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
}
void update_shdr() override;
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-16 18:23:51 +03:00
};
2020-11-16 18:33:41 +03:00
// Program header
class OutputPhdr : public OutputChunk {
2020-10-15 11:06:01 +03:00
public:
2020-11-16 18:33:41 +03:00
OutputPhdr() : OutputChunk(HEADER) {
2020-11-08 10:05:36 +03:00
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
}
2020-11-16 18:33:41 +03:00
void update_shdr() override;
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-10-15 11:06:01 +03:00
};
2020-11-16 19:05:01 +03:00
class InterpSection : public OutputChunk {
public:
InterpSection() : OutputChunk(SYNTHETIC) {
name = ".interp";
shdr.sh_type = llvm::ELF::SHT_PROGBITS;
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_size = config.dynamic_linker.size() + 1;
}
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-16 19:05:01 +03:00
};
2020-10-16 10:38:03 +03:00
// Sections
2020-10-15 12:30:06 +03:00
class OutputSection : public OutputChunk {
public:
static OutputSection *get_instance(StringRef name, u64 flags, u32 type);
2020-10-22 10:35:17 +03:00
2020-11-13 06:43:59 +03:00
OutputSection(StringRef name, u32 type, u64 flags)
2020-11-08 10:09:01 +03:00
: OutputChunk(REGULAR) {
2020-10-22 13:35:16 +03:00
this->name = name;
2020-10-25 07:17:10 +03:00
shdr.sh_type = type;
2020-11-13 06:43:59 +03:00
shdr.sh_flags = flags;
2020-10-28 08:06:35 +03:00
idx = instances.size();
instances.push_back(this);
2020-10-21 05:28:43 +03:00
}
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-10-26 07:34:15 +03:00
2020-11-06 15:17:35 +03:00
static inline std::vector<OutputSection *> instances;
2020-10-29 12:31:06 +03:00
2020-11-08 10:09:01 +03:00
std::vector<InputChunk *> members;
2020-10-29 12:31:06 +03:00
u32 idx;
2020-10-15 12:30:06 +03:00
};
2020-11-11 02:36:44 +03:00
class SpecialSection : public OutputChunk {
public:
2020-11-11 15:32:41 +03:00
SpecialSection(StringRef name, u32 type, u64 flags, u32 align = 1, u32 entsize = 0)
2020-11-11 02:36:44 +03:00
: OutputChunk(SYNTHETIC) {
2020-11-11 03:47:05 +03:00
this->name = name;
2020-11-11 02:36:44 +03:00
shdr.sh_type = type;
2020-11-13 06:43:59 +03:00
shdr.sh_flags = flags;
2020-11-11 02:36:44 +03:00
shdr.sh_addralign = align;
shdr.sh_entsize = entsize;
}
};
2020-11-18 11:11:58 +03:00
class GotSection : public OutputChunk {
public:
GotSection() : OutputChunk(SYNTHETIC) {
name = ".got";
shdr.sh_type = llvm::ELF::SHT_PROGBITS;
shdr.sh_flags = llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE;
shdr.sh_addralign = GOT_SIZE;
}
2020-11-18 14:29:24 +03:00
2020-11-21 06:49:28 +03:00
void add_got_symbol(Symbol *sym);
2020-11-21 04:48:51 +03:00
void add_gottpoff_symbol(Symbol *sym);
2020-11-21 04:48:23 +03:00
void add_tlsgd_symbol(Symbol *sym);
void add_tlsld_symbol(Symbol *sym);
2020-11-18 14:29:24 +03:00
void copy_buf() override;
std::vector<Symbol *> got_syms;
2020-11-21 04:48:51 +03:00
std::vector<Symbol *> gottpoff_syms;
2020-11-21 04:48:23 +03:00
std::vector<Symbol *> tlsgd_syms;
std::vector<Symbol *> tlsld_syms;
2020-11-18 11:11:58 +03:00
};
2020-11-13 06:43:59 +03:00
class GotPltSection : public OutputChunk {
public:
GotPltSection() : OutputChunk(SYNTHETIC) {
2020-11-16 19:05:01 +03:00
name = ".got.plt";
2020-11-13 06:43:59 +03:00
shdr.sh_type = llvm::ELF::SHT_PROGBITS;
shdr.sh_flags = llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE;
shdr.sh_addralign = GOT_SIZE;
2020-11-17 15:02:14 +03:00
shdr.sh_size = GOT_SIZE * INIT_SIZE;
2020-11-13 06:43:59 +03:00
}
2020-11-17 15:02:14 +03:00
enum { INIT_SIZE = 3 };
2020-11-18 15:45:49 +03:00
void copy_buf() override;
2020-11-13 06:43:59 +03:00
};
2020-11-01 11:46:08 +03:00
class PltSection : public OutputChunk {
public:
2020-11-08 10:05:36 +03:00
PltSection() : OutputChunk(SYNTHETIC) {
2020-11-16 19:05:01 +03:00
name = ".plt";
2020-11-01 11:46:08 +03:00
shdr.sh_type = llvm::ELF::SHT_PROGBITS;
2020-11-13 06:43:59 +03:00
shdr.sh_flags = llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR;
2020-11-01 11:46:08 +03:00
shdr.sh_addralign = 8;
2020-11-13 06:30:27 +03:00
shdr.sh_size = PLT_SIZE;
2020-11-01 11:46:08 +03:00
}
2020-11-18 15:45:49 +03:00
void add_symbol(Symbol *sym);
void copy_buf() override;
2020-11-17 15:02:14 +03:00
2020-11-18 15:45:49 +03:00
std::vector<Symbol *> symbols;
2020-11-01 11:46:08 +03:00
};
2020-11-01 10:22:47 +03:00
class RelPltSection : public OutputChunk {
2020-11-01 02:55:13 +03:00
public:
2020-11-08 10:05:36 +03:00
RelPltSection() : OutputChunk(SYNTHETIC) {
2020-11-16 19:05:01 +03:00
name = ".rela.plt";
2020-11-01 02:55:13 +03:00
shdr.sh_type = llvm::ELF::SHT_RELA;
2020-11-13 06:43:59 +03:00
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
2020-11-01 11:53:57 +03:00
shdr.sh_entsize = sizeof(ELF64LE::Rela);
2020-11-01 02:55:13 +03:00
shdr.sh_addralign = 8;
}
2020-11-17 07:34:02 +03:00
void update_shdr() override;
2020-11-18 15:45:49 +03:00
void copy_buf() override;
2020-11-01 02:55:13 +03:00
};
2020-11-17 07:30:33 +03:00
class RelDynSection : public OutputChunk {
public:
RelDynSection() : OutputChunk(SYNTHETIC) {
name = ".rela.dyn";
shdr.sh_type = llvm::ELF::SHT_RELA;
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_entsize = sizeof(ELF64LE::Rela);
shdr.sh_addralign = 8;
}
void update_shdr() override;
2020-11-18 14:29:24 +03:00
void copy_buf() override;
2020-11-17 07:30:33 +03:00
};
2020-11-12 16:10:47 +03:00
class StrtabSection : public OutputChunk {
public:
2020-11-19 10:13:19 +03:00
StrtabSection() : OutputChunk(SYNTHETIC) {
name = ".strtab";
2020-11-12 16:10:47 +03:00
shdr.sh_type = llvm::ELF::SHT_STRTAB;
shdr.sh_addralign = 1;
shdr.sh_size = 1;
}
2020-11-29 12:58:36 +03:00
void update_shdr() override;
2020-11-12 16:10:47 +03:00
};
2020-11-17 06:20:56 +03:00
class ShstrtabSection : public OutputChunk {
public:
ShstrtabSection() : OutputChunk(SYNTHETIC) {
name = ".shstrtab";
shdr.sh_type = llvm::ELF::SHT_STRTAB;
shdr.sh_addralign = 1;
}
void update_shdr() override;
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-17 06:20:56 +03:00
};
2020-11-17 07:19:54 +03:00
class DynstrSection : public OutputChunk {
public:
DynstrSection() : OutputChunk(SYNTHETIC) {
name = ".dynstr";
shdr.sh_type = llvm::ELF::SHT_STRTAB;
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_size = 1;
shdr.sh_addralign = 1;
}
u32 add_string(StringRef str);
2020-11-30 11:52:08 +03:00
u32 find_string(StringRef str);
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-17 07:19:54 +03:00
private:
std::vector<StringRef> contents;
};
2020-11-16 18:43:32 +03:00
class DynamicSection : public OutputChunk {
public:
DynamicSection() : OutputChunk(SYNTHETIC) {
2020-11-16 19:05:01 +03:00
name = ".dynamic";
2020-11-16 18:43:32 +03:00
shdr.sh_type = llvm::ELF::SHT_DYNAMIC;
shdr.sh_flags = llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE;
shdr.sh_addralign = 8;
shdr.sh_entsize = sizeof(ELF64LE::Dyn);
}
void update_shdr() override;
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-16 18:43:32 +03:00
};
2020-10-27 12:59:56 +03:00
class SymtabSection : public OutputChunk {
public:
2020-11-17 07:32:22 +03:00
SymtabSection() : OutputChunk(SYNTHETIC) {
name = ".symtab";
shdr.sh_type = llvm::ELF::SHT_SYMTAB;
2020-10-27 12:59:56 +03:00
shdr.sh_entsize = sizeof(ELF64LE::Sym);
shdr.sh_addralign = 8;
2020-11-04 01:14:49 +03:00
shdr.sh_size = sizeof(ELF64LE::Sym);
2020-10-27 12:59:56 +03:00
}
2020-11-17 07:32:22 +03:00
void update_shdr() override;
2020-11-17 08:28:53 +03:00
void copy_buf() override;
2020-10-27 12:59:56 +03:00
};
2020-11-16 17:40:01 +03:00
class DynsymSection : public OutputChunk {
public:
DynsymSection() : OutputChunk(SYNTHETIC) {
2020-11-16 19:05:01 +03:00
name = ".dynsym";
2020-11-16 17:40:01 +03:00
shdr.sh_type = llvm::ELF::SHT_DYNSYM;
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_entsize = sizeof(ELF64LE::Sym);
shdr.sh_addralign = 8;
shdr.sh_size = sizeof(ELF64LE::Sym);
shdr.sh_info = 1;
}
2020-11-17 14:22:52 +03:00
void add_symbol(Symbol *sym);
2020-11-16 19:30:24 +03:00
void update_shdr() override;
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-16 17:40:01 +03:00
2020-11-16 17:48:20 +03:00
std::vector<Symbol *> symbols;
2020-11-16 17:40:01 +03:00
};
2020-11-11 15:12:35 +03:00
class HashSection : public OutputChunk {
public:
HashSection() : OutputChunk(SYNTHETIC) {
2020-11-16 19:05:01 +03:00
name = ".hash";
2020-11-11 15:12:35 +03:00
shdr.sh_type = llvm::ELF::SHT_HASH;
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_entsize = 4;
shdr.sh_addralign = 4;
}
2020-11-16 18:10:38 +03:00
void update_shdr() override;
2020-11-17 07:56:40 +03:00
void copy_buf() override;
2020-11-12 11:15:59 +03:00
2020-11-12 14:38:46 +03:00
private:
2020-11-16 17:58:40 +03:00
static u32 hash(StringRef name);
2020-11-11 15:12:35 +03:00
};
2020-11-07 15:53:21 +03:00
class MergedSection : public OutputChunk {
public:
2020-11-07 15:53:21 +03:00
static MergedSection *get_instance(StringRef name, u64 flags, u32 type);
2020-11-07 14:29:06 +03:00
2020-11-07 15:53:21 +03:00
static inline std::vector<MergedSection *> instances;
2020-11-07 04:45:28 +03:00
ConcurrentMap<StringPiece> map;
2020-11-07 04:13:19 +03:00
private:
2020-11-08 10:05:36 +03:00
MergedSection(StringRef name, u64 flags, u32 type)
: OutputChunk(SYNTHETIC) {
this->name = name;
shdr.sh_flags = flags;
shdr.sh_type = type;
shdr.sh_addralign = 1;
}
};
2020-11-25 11:20:48 +03:00
class CopyrelSection : public OutputChunk {
public:
CopyrelSection() : OutputChunk(SYNTHETIC) {
name = ".bss";
shdr.sh_type = llvm::ELF::SHT_NOBITS;
shdr.sh_flags = llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE;
shdr.sh_addralign = 32;
}
void add_symbol(Symbol *sym);
std::vector<Symbol *> symbols;
};
2020-11-27 11:32:25 +03:00
class VersymSection : public OutputChunk {
public:
VersymSection() : OutputChunk(SYNTHETIC) {
name = ".gnu.version";
2020-11-27 12:10:09 +03:00
shdr.sh_type = llvm::ELF::SHT_GNU_versym;
2020-11-27 11:32:25 +03:00
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
2020-11-29 10:37:14 +03:00
shdr.sh_entsize = 2;
2020-11-27 11:32:25 +03:00
shdr.sh_addralign = 2;
}
void update_shdr() override;
void copy_buf() override;
2020-11-29 05:40:57 +03:00
std::vector<u16> contents;
2020-11-27 11:32:25 +03:00
};
class VerneedSection : public OutputChunk {
public:
VerneedSection() : OutputChunk(SYNTHETIC) {
name = ".gnu.version_r";
2020-11-27 12:10:09 +03:00
shdr.sh_type = llvm::ELF::SHT_GNU_verneed;
2020-11-27 11:32:25 +03:00
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
2020-11-29 07:42:27 +03:00
shdr.sh_addralign = 8;
2020-11-27 11:32:25 +03:00
}
void update_shdr() override;
void copy_buf() override;
2020-11-29 05:40:57 +03:00
std::vector<u8> contents;
2020-11-27 11:32:25 +03:00
};
2020-11-04 04:43:05 +03:00
bool is_c_identifier(StringRef name);
2020-11-29 14:19:59 +03:00
std::vector<ELF64LE::Phdr> create_phdr();
2020-11-04 04:43:05 +03:00
2020-10-12 07:30:34 +03:00
//
2020-11-03 04:54:10 +03:00
// object_file.cc
2020-10-12 07:30:34 +03:00
//
struct ComdatGroup {
ComdatGroup(ObjectFile *file, u32 i)
: file(file), section_idx(i) {}
ComdatGroup(const ComdatGroup &other)
2020-10-23 06:43:22 +03:00
: file(other.file.load()), section_idx(other.section_idx) {}
2020-10-23 06:43:22 +03:00
std::atomic<ObjectFile *> file;
u32 section_idx;
};
2020-11-24 10:22:32 +03:00
class InputFile {
public:
2020-11-24 15:15:14 +03:00
InputFile(MemoryBufferRef mb, bool is_dso)
: mb(mb), name(mb.getBufferIdentifier()), is_dso(is_dso),
2020-11-24 10:22:32 +03:00
obj(check(ELFFile<ELF64LE>::create(mb.getBuffer()))) {}
std::string name;
2020-11-24 15:15:14 +03:00
bool is_dso;
2020-11-24 10:22:32 +03:00
u32 priority;
MemoryBufferRef mb;
ELFFile<ELF64LE> obj;
std::vector<Symbol *> symbols;
std::atomic_bool is_alive = ATOMIC_VAR_INIT(false);
};
class ObjectFile : public InputFile {
2020-10-04 12:00:33 +03:00
public:
2020-10-14 12:42:54 +03:00
ObjectFile(MemoryBufferRef mb, StringRef archive_name);
2020-10-04 12:00:33 +03:00
2020-10-09 14:47:45 +03:00
void parse();
2020-11-07 12:06:09 +03:00
void initialize_mergeable_sections();
2020-11-05 06:20:16 +03:00
void resolve_symbols();
2020-11-30 10:43:47 +03:00
void mark_live_objects(tbb::parallel_do_feeder<ObjectFile *> &feeder);
2020-11-19 10:09:07 +03:00
void handle_undefined_weak_symbols();
2020-11-08 12:17:24 +03:00
void resolve_comdat_groups();
2020-10-19 15:50:33 +03:00
void eliminate_duplicate_comdat_groups();
2020-11-07 15:53:21 +03:00
void assign_mergeable_string_offsets();
2020-10-27 06:50:25 +03:00
void convert_common_symbols();
2020-10-27 14:58:28 +03:00
void compute_symtab();
2020-11-29 12:31:19 +03:00
void write_symtab();
2020-10-27 13:03:57 +03:00
2020-11-17 07:48:11 +03:00
static ObjectFile *create_internal_file();
2020-11-03 13:33:46 +03:00
2020-10-14 12:42:54 +03:00
StringRef archive_name;
2020-11-08 06:10:08 +03:00
std::vector<InputSection *> sections;
2020-11-01 06:41:09 +03:00
ArrayRef<ELF64LE::Sym> elf_syms;
2020-11-07 05:41:14 +03:00
int first_global = 0;
2020-11-05 06:34:59 +03:00
const bool is_in_archive;
2020-11-26 12:09:32 +03:00
std::atomic_bool has_error = ATOMIC_VAR_INIT(false);
2020-10-09 17:26:26 +03:00
2020-11-29 12:45:33 +03:00
u64 local_symtab_offset = 0;
u64 local_symtab_size = 0;
2020-11-29 12:45:33 +03:00
u64 global_symtab_offset = 0;
u64 global_symtab_size = 0;
2020-11-29 12:45:33 +03:00
u64 strtab_offset = 0;
u64 strtab_size = 0;
2020-10-27 14:58:28 +03:00
2020-11-08 08:13:59 +03:00
std::vector<MergeableSection> mergeable_sections;
2020-11-07 15:53:21 +03:00
2020-10-09 14:47:45 +03:00
private:
2020-10-19 14:05:34 +03:00
void initialize_sections();
void initialize_symbols();
2020-11-08 08:13:59 +03:00
std::vector<StringPieceRef> read_string_pieces(InputSection *isec);
2020-11-24 10:22:32 +03:00
void maybe_override_symbol(Symbol &sym, int symidx);
2020-10-19 14:05:34 +03:00
2020-11-08 12:20:48 +03:00
std::vector<std::pair<ComdatGroup *, ArrayRef<ELF64LE::Word>>> comdat_groups;
2020-11-03 08:53:32 +03:00
std::vector<Symbol> local_symbols;
2020-11-07 06:14:37 +03:00
std::vector<StringPieceRef> sym_pieces;
2020-10-27 02:45:20 +03:00
bool has_common_symbol;
2020-10-26 08:52:55 +03:00
2020-10-19 14:05:34 +03:00
ArrayRef<ELF64LE::Shdr> elf_sections;
2020-10-26 15:44:08 +03:00
StringRef symbol_strtab;
2020-10-19 14:17:32 +03:00
const ELF64LE::Shdr *symtab_sec;
2020-10-04 12:00:33 +03:00
};
2020-11-24 10:22:32 +03:00
class SharedFile : public InputFile {
public:
2020-11-30 10:43:47 +03:00
SharedFile(MemoryBufferRef mb, bool as_needed) : InputFile(mb, true) {
is_alive = !as_needed;
}
2020-11-24 10:22:32 +03:00
void parse();
void resolve_symbols();
2020-11-25 14:35:04 +03:00
ArrayRef<Symbol *> find_aliases(Symbol *sym);
2020-11-24 10:22:32 +03:00
StringRef soname;
2020-11-27 12:10:09 +03:00
2020-11-28 16:08:46 +03:00
std::vector<StringRef> version_strings;
2020-11-24 10:22:32 +03:00
private:
StringRef get_soname(ArrayRef<ELF64LE::Shdr> elf_sections);
void maybe_override_symbol(Symbol &sym, const ELF64LE::Sym &esym);
2020-11-28 14:43:48 +03:00
std::vector<StringRef> read_verdef();
2020-11-24 10:22:32 +03:00
2020-11-28 14:43:48 +03:00
std::vector<const ELF64LE::Sym *> elf_syms;
2020-11-28 16:08:46 +03:00
std::vector<u16> versyms;
2020-11-24 10:22:32 +03:00
StringRef symbol_strtab;
const ELF64LE::Shdr *symtab_sec;
};
2020-11-29 04:31:04 +03:00
//
// linker_script.cc
//
void parse_linker_script(StringRef path, StringRef input);
//
// perf.cc
//
class Counter {
public:
Counter(StringRef name, u32 value = 0) : name(name), value(value) {
static std::mutex mu;
std::lock_guard lock(mu);
instances.push_back(this);
}
void inc(u32 delta = 1) {
if (enabled)
value += delta;
}
void set(u32 value) {
this->value = value;
}
static void print();
static bool enabled;
private:
StringRef name;
std::atomic_uint32_t value;
static std::vector<Counter *> instances;
};
//
// mapfile.cc
//
void print_map();
//
// main.cc
//
MemoryBufferRef find_library(const Twine &path);
MemoryBufferRef *open_input_file(const Twine &path);
MemoryBufferRef must_open_input_file(const Twine &path);
void read_file(MemoryBufferRef mb);
//
// Inline objects and functions
//
namespace out {
using namespace llvm::ELF;
inline std::vector<ObjectFile *> objs;
inline std::vector<SharedFile *> dsos;
inline std::vector<OutputChunk *> chunks;
inline u8 *buf;
inline OutputEhdr *ehdr;
inline OutputShdr *shdr;
inline OutputPhdr *phdr;
inline InterpSection *interp;
inline GotSection *got;
inline GotPltSection *gotplt;
inline RelPltSection *relplt;
inline RelDynSection *reldyn;
inline DynamicSection *dynamic;
inline StrtabSection *strtab;
inline DynstrSection *dynstr;
inline HashSection *hash;
inline ShstrtabSection *shstrtab;
inline PltSection *plt;
inline SymtabSection *symtab;
inline DynsymSection *dynsym;
inline CopyrelSection *copyrel;
2020-11-29 05:57:58 +03:00
inline VersymSection *versym;
inline VerneedSection *verneed;
2020-11-29 04:31:04 +03:00
inline u64 tls_end;
inline Symbol *__bss_start;
inline Symbol *__ehdr_start;
inline Symbol *__rela_iplt_start;
inline Symbol *__rela_iplt_end;
inline Symbol *__init_array_start;
inline Symbol *__init_array_end;
inline Symbol *__fini_array_start;
inline Symbol *__fini_array_end;
inline Symbol *__preinit_array_start;
inline Symbol *__preinit_array_end;
inline Symbol *_DYNAMIC;
inline Symbol *_GLOBAL_OFFSET_TABLE_;
inline Symbol *_end;
inline Symbol *_etext;
inline Symbol *_edata;
}
inline void message(const Twine &msg) {
static std::mutex mu;
std::lock_guard lock(mu);
llvm::outs() << msg << "\n";
}
inline std::string toString(const Twine &s) { return s.str(); }
inline std::string toString(Symbol sym) {
return (StringRef(sym.name) + "(" + toString(sym.file) + ")").str();
}
inline u64 align_to(u64 val, u64 align) {
assert(__builtin_popcount(align) == 1);
return (val + align - 1) & ~(align - 1);
}
2020-11-15 08:07:40 +03:00
inline u64 Symbol::get_addr() const {
if (piece_ref.piece)
return piece_ref.piece->get_addr() + piece_ref.addend;
2020-12-01 15:43:30 +03:00
2020-11-25 11:20:48 +03:00
if (copyrel_offset != -1)
return out::copyrel->shdr.sh_addr + copyrel_offset;
2020-12-01 15:43:30 +03:00
2020-12-01 11:16:28 +03:00
if (input_section) {
2020-12-01 15:43:30 +03:00
if (!input_section->is_alive) {
// The control can reach here if there's a relocation that refers
// a local symbol belonging to a comdat group section. This is a
// violation of the spec, as all relocations should use only global
// symbols of comdat members. However, .eh_frame tends to have such
// relocations.
return 0;
}
2020-11-25 09:13:06 +03:00
return input_section->get_addr() + value;
2020-12-01 11:16:28 +03:00
}
2020-12-01 15:43:30 +03:00
2020-11-26 07:34:42 +03:00
if (file && file->is_dso && copyrel_offset == -1)
return get_plt_addr();
2020-12-01 15:43:30 +03:00
2020-11-15 08:07:40 +03:00
return value;
}
inline u64 Symbol::get_got_addr() const {
assert(got_idx != -1);
2020-11-17 14:47:22 +03:00
return out::got->shdr.sh_addr + got_idx * GOT_SIZE;
2020-11-15 08:07:40 +03:00
}
inline u64 Symbol::get_gotplt_addr() const {
assert(gotplt_idx != -1);
2020-11-17 14:47:22 +03:00
return out::gotplt->shdr.sh_addr + gotplt_idx * GOT_SIZE;
2020-11-15 08:07:40 +03:00
}
2020-11-21 04:48:51 +03:00
inline u64 Symbol::get_gottpoff_addr() const {
assert(gottpoff_idx != -1);
return out::got->shdr.sh_addr + gottpoff_idx * GOT_SIZE;
2020-11-15 08:07:40 +03:00
}
2020-11-21 04:48:23 +03:00
inline u64 Symbol::get_tlsgd_addr() const {
assert(tlsgd_idx != -1);
return out::got->shdr.sh_addr + tlsgd_idx * GOT_SIZE;
2020-11-15 08:07:40 +03:00
}
2020-11-21 04:48:23 +03:00
inline u64 Symbol::get_tlsld_addr() const {
assert(tlsld_idx != -1);
return out::got->shdr.sh_addr + tlsld_idx * GOT_SIZE;
2020-11-15 08:07:40 +03:00
}
inline u64 Symbol::get_plt_addr() const {
assert(plt_idx != -1);
2020-11-17 14:47:22 +03:00
return out::plt->shdr.sh_addr + plt_idx * PLT_SIZE;
2020-11-15 08:07:40 +03:00
}
inline u64 StringPiece::get_addr() const {
MergeableSection *is = isec.load();
return is->parent.shdr.sh_addr + is->offset + output_offset;
}
2020-11-25 09:13:06 +03:00
inline u64 InputChunk::get_addr() const {
return output_section->shdr.sh_addr + offset;
}
2020-11-29 06:59:08 +03:00
inline u32 elf_hash(StringRef name) {
u32 h = 0;
2020-11-29 15:05:39 +03:00
for (u8 c : name) {
2020-11-29 06:59:08 +03:00
h = (h << 4) + c;
u32 g = h & 0xf0000000;
if (g != 0)
h ^= g >> 24;
h &= ~g;
}
return h;
}
2020-11-15 08:07:40 +03:00
inline void write_string(u8 *buf, StringRef str) {
memcpy(buf, str.data(), str.size());
buf[str.size()] = '\0';
}
2020-11-16 18:43:32 +03:00
template <typename T>
inline void write_vector(u8 *buf, const std::vector<T> &vec) {
memcpy(buf, vec.data(), vec.size() * sizeof(T));
}
2020-11-24 08:31:05 +03:00
template <typename T>
2020-11-17 13:56:02 +03:00
inline std::vector<T> flatten(std::vector<std::vector<T>> &vec) {
std::vector<T> ret;
for (std::vector<T> &v : vec)
ret.insert(ret.end(), v.begin(), v.end());
return ret;
}