1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 10:27:48 +03:00
mold/main.cc

1225 lines
37 KiB
C++
Raw Normal View History

2020-10-20 08:54:35 +03:00
#include "mold.h"
2020-10-02 07:28:26 +03:00
2020-11-03 14:49:30 +03:00
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/FileOutputBuffer.h"
2020-11-09 05:58:48 +03:00
#include <fcntl.h>
2020-09-29 09:05:29 +03:00
#include <iostream>
2020-11-09 06:30:13 +03:00
#include <libgen.h>
2020-11-09 05:58:48 +03:00
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
2020-09-29 09:05:29 +03:00
2020-10-24 12:58:21 +03:00
using namespace llvm;
2020-10-21 05:28:43 +03:00
using namespace llvm::ELF;
2020-10-10 06:47:12 +03:00
using llvm::object::Archive;
2020-10-02 10:47:51 +03:00
using llvm::opt::InputArgList;
2020-10-02 07:28:26 +03:00
2020-11-08 02:39:13 +03:00
class MyTimer {
public:
MyTimer(StringRef name) {
timer = new Timer(name, name);
timer->startTimer();
}
MyTimer(StringRef name, llvm::TimerGroup &tg) {
timer = new Timer(name, name, tg);
timer->startTimer();
}
~MyTimer() { timer->stopTimer(); }
private:
llvm::Timer *timer;
};
2020-11-11 04:42:26 +03:00
llvm::TimerGroup parse_timer("parse", "parse");
llvm::TimerGroup before_copy_timer("before_copy", "before_copy");
llvm::TimerGroup copy_timer("copy", "copy");
2020-10-04 12:00:33 +03:00
//
// Command-line option processing
//
2020-10-02 07:28:26 +03:00
enum {
OPT_INVALID = 0,
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
#include "options.inc"
#undef OPTION
};
2020-10-02 10:47:51 +03:00
// Create prefix string literals used in Options.td
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
#include "options.inc"
#undef PREFIX
// Create table mapping all options defined in Options.td
static const llvm::opt::OptTable::Info opt_info[] = {
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
{X1, X2, X10, X11, OPT_##ID, llvm::opt::Option::KIND##Class, \
X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
#include "options.inc"
#undef OPTION
};
class MyOptTable : llvm::opt::OptTable {
public:
MyOptTable() : OptTable(opt_info) {}
InputArgList parse(int argc, char **argv);
};
InputArgList MyOptTable::parse(int argc, char **argv) {
2020-11-01 02:55:13 +03:00
unsigned missing_index = 0;
unsigned missing_count = 0;
2020-10-02 10:47:51 +03:00
SmallVector<const char *, 256> vec(argv, argv + argc);
2020-11-01 02:55:13 +03:00
InputArgList args = this->ParseArgs(vec, missing_index, missing_count);
if (missing_count)
error(Twine(args.getArgString(missing_index)) + ": missing argument");
2020-10-02 10:47:51 +03:00
for (auto *arg : args.filtered(OPT_UNKNOWN))
error("unknown argument '" + arg->getAsString(args) + "'");
return args;
}
2020-10-04 12:00:33 +03:00
//
// Main
//
2020-10-14 13:36:06 +03:00
static std::vector<MemoryBufferRef> get_archive_members(MemoryBufferRef mb) {
2020-10-10 06:47:12 +03:00
std::unique_ptr<Archive> file =
CHECK(Archive::create(mb), mb.getBufferIdentifier() + ": failed to parse archive");
std::vector<MemoryBufferRef> vec;
Error err = Error::success();
for (const Archive::Child &c : file->children(err)) {
MemoryBufferRef mbref =
CHECK(c.getMemoryBufferRef(),
mb.getBufferIdentifier() +
": could not get the buffer for a child of the archive");
vec.push_back(mbref);
}
if (err)
error(mb.getBufferIdentifier() + ": Archive::children failed: " +
toString(std::move(err)));
2020-10-10 12:48:38 +03:00
file.release(); // leak
2020-10-10 06:47:12 +03:00
return vec;
}
2020-10-22 11:26:23 +03:00
static void read_file(std::vector<ObjectFile *> &files, StringRef path) {
2020-11-09 05:58:48 +03:00
int fd = open(path.str().c_str(), O_RDONLY);
if (fd == -1)
error("cannot open " + path);
2020-10-10 06:47:12 +03:00
2020-11-09 05:58:48 +03:00
struct stat st;
if (fstat(fd, &st) == -1)
error(path + ": stat failed");
2020-11-03 14:42:50 +03:00
2020-11-09 05:58:48 +03:00
void *addr = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
error(path + ": mmap failed: " + strerror(errno));
close(fd);
auto &mb = *new MemoryBufferRef(StringRef((char *)addr, st.st_size), path);
switch (identify_magic(mb.getBuffer())) {
2020-10-10 06:47:12 +03:00
case file_magic::archive:
2020-11-09 05:58:48 +03:00
for (MemoryBufferRef member : get_archive_members(mb))
2020-10-22 11:26:23 +03:00
files.push_back(new ObjectFile(member, path));
2020-10-10 06:47:12 +03:00
break;
case file_magic::elf_relocatable:
2020-11-04 09:39:31 +03:00
case file_magic::elf_shared_object:
2020-11-09 05:58:48 +03:00
files.push_back(new ObjectFile(mb, ""));
2020-10-10 06:47:12 +03:00
break;
default:
error(path + ": unknown file type");
}
}
2020-10-28 07:42:05 +03:00
template <typename T>
static std::vector<ArrayRef<T>> split(const std::vector<T> &input, int unit) {
ArrayRef<T> arr(input);
std::vector<ArrayRef<T>> vec;
while (arr.size() >= unit) {
vec.push_back(arr.slice(0, unit));
arr = arr.slice(unit);
}
if (!arr.empty())
vec.push_back(arr);
return vec;
}
2020-11-11 04:42:26 +03:00
static void resolve_symbols(std::vector<ObjectFile *> &files) {
MyTimer t("resolve_symbols", before_copy_timer);
2020-11-11 04:51:30 +03:00
// Register defined symbols
2020-11-11 04:42:26 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) { file->resolve_symbols(); });
2020-11-11 04:51:30 +03:00
// Mark archive members we include into the final output.
2020-11-11 04:42:26 +03:00
std::vector<ObjectFile *> root;
for (ObjectFile *file : files)
if (file->is_alive)
root.push_back(file);
tbb::parallel_do(
root,
[&](ObjectFile *file, tbb::parallel_do_feeder<ObjectFile *> &feeder) {
file->mark_live_archive_members(feeder);
});
// Eliminate unused archive members.
files.erase(std::remove_if(files.begin(), files.end(),
[](ObjectFile *file){ return !file->is_alive; }),
files.end());
// Convert weak symbols to absolute symbols with value 0.
tbb::parallel_for_each(files, [](ObjectFile *file) {
2020-11-11 04:51:30 +03:00
file->hanlde_undefined_weak_symbols();
});
2020-11-11 04:42:26 +03:00
}
2020-11-08 12:17:24 +03:00
static void eliminate_comdats(std::vector<ObjectFile *> &files) {
2020-11-11 04:42:26 +03:00
MyTimer t("comdat", before_copy_timer);
2020-11-08 12:17:24 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) {
file->resolve_comdat_groups();
});
tbb::parallel_for_each(files, [](ObjectFile *file) {
file->eliminate_duplicate_comdat_groups();
});
}
2020-11-07 15:53:21 +03:00
static void handle_mergeable_strings(std::vector<ObjectFile *> &files) {
2020-11-11 04:42:26 +03:00
MyTimer t("resolve_strings", before_copy_timer);
2020-11-07 15:53:21 +03:00
// Resolve mergeable string pieces
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) {
2020-11-08 08:13:59 +03:00
for (MergeableSection &isec : file->mergeable_sections) {
for (StringPieceRef &ref : isec.pieces) {
MergeableSection *cur = ref.piece->isec;
while (!cur || cur->file->priority > isec.file->priority)
if (ref.piece->isec.compare_exchange_strong(cur, &isec))
2020-11-07 15:53:21 +03:00
break;
}
}
});
// Calculate the total bytes of mergeable strings for each input section.
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) {
2020-11-08 08:13:59 +03:00
for (MergeableSection &isec : file->mergeable_sections) {
2020-11-07 15:53:21 +03:00
u32 offset = 0;
2020-11-08 08:13:59 +03:00
for (StringPieceRef &ref : isec.pieces) {
2020-11-10 11:18:10 +03:00
StringPiece &piece = *ref.piece;
if (piece.isec == &isec && piece.output_offset == -1) {
2020-11-07 15:53:21 +03:00
ref.piece->output_offset = offset;
2020-11-10 09:03:40 +03:00
offset += ref.piece->data.size();
2020-11-07 15:53:21 +03:00
}
}
2020-11-08 08:13:59 +03:00
isec.size = offset;
2020-11-07 15:53:21 +03:00
}
});
// Assign each mergeable input section a unique index.
for (ObjectFile *file : files) {
2020-11-08 08:13:59 +03:00
for (MergeableSection &isec : file->mergeable_sections) {
MergedSection &osec = isec.parent;
isec.offset = osec.shdr.sh_size;
osec.shdr.sh_size += isec.size;
2020-11-07 15:53:21 +03:00
}
}
2020-11-08 07:01:46 +03:00
static Counter counter("merged_strings");
for (MergedSection *osec : MergedSection::instances)
counter.inc(osec->map.size());
2020-11-07 15:53:21 +03:00
}
2020-11-11 04:51:30 +03:00
// So far, each input section has a pointer to its corresponding
// output section, but there's no reverse edge to get a list of
// input sections from an output section. This function creates it.
//
// An output section may contain millions of input sections.
// So, we append input sections to output sections in parallel.
2020-10-27 18:21:41 +03:00
static void bin_sections(std::vector<ObjectFile *> &files) {
2020-11-11 04:42:26 +03:00
MyTimer t("bin_sections", before_copy_timer);
2020-10-28 08:06:35 +03:00
int unit = (files.size() + 127) / 128;
std::vector<ArrayRef<ObjectFile *>> slices = split(files, unit);
2020-10-26 07:36:56 +03:00
2020-11-08 04:05:59 +03:00
int num_osec = OutputSection::instances.size();
2020-11-08 06:36:08 +03:00
std::vector<std::vector<std::vector<InputChunk *>>> groups(slices.size());
2020-10-28 08:22:25 +03:00
for (int i = 0; i < groups.size(); i++)
2020-11-08 04:05:59 +03:00
groups[i].resize(num_osec);
2020-10-28 08:06:35 +03:00
tbb::parallel_for(0, (int)slices.size(), [&](int i) {
for (ObjectFile *file : slices[i]) {
for (InputSection *isec : file->sections) {
if (!isec)
continue;
OutputSection *osec = isec->output_section;
2020-10-28 08:22:25 +03:00
groups[i][osec->idx].push_back(isec);
2020-10-28 08:06:35 +03:00
}
}
});
2020-11-08 04:05:59 +03:00
std::vector<int> sizes(num_osec);
2020-10-26 07:36:56 +03:00
2020-11-08 06:36:08 +03:00
for (ArrayRef<std::vector<InputChunk *>> group : groups)
2020-10-28 08:22:25 +03:00
for (int i = 0; i < group.size(); i++)
sizes[i] += group[i].size();
2020-11-08 03:44:27 +03:00
2020-11-08 04:05:59 +03:00
tbb::parallel_for(0, num_osec, [&](int j) {
2020-11-08 06:42:40 +03:00
OutputSection::instances[j]->members.reserve(sizes[j]);
2020-11-08 04:06:36 +03:00
2020-11-08 04:05:59 +03:00
for (int i = 0; i < groups.size(); i++) {
2020-11-08 06:42:40 +03:00
std::vector<InputChunk *> &sections = OutputSection::instances[j]->members;
2020-11-08 04:05:59 +03:00
sections.insert(sections.end(), groups[i][j].begin(), groups[i][j].end());
2020-10-28 08:06:35 +03:00
}
2020-11-08 04:05:59 +03:00
});
2020-10-26 07:36:56 +03:00
}
2020-10-22 17:19:48 +03:00
2020-10-26 08:18:00 +03:00
static void set_isec_offsets() {
2020-11-11 04:42:26 +03:00
MyTimer t("isec_offsets", before_copy_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(OutputSection::instances, [&](OutputSection *osec) {
2020-11-08 06:42:40 +03:00
if (osec->members.empty())
2020-10-27 07:52:10 +03:00
return;
2020-11-08 06:42:40 +03:00
std::vector<ArrayRef<InputChunk *>> slices = split(osec->members, 100000);
std::vector<u64> size(slices.size());
std::vector<u32> alignments(slices.size());
2020-10-26 08:18:00 +03:00
2020-10-28 07:42:05 +03:00
tbb::parallel_for(0, (int)slices.size(), [&](int i) {
u64 off = 0;
u32 align = 1;
2020-10-26 10:12:35 +03:00
2020-11-08 06:36:08 +03:00
for (InputChunk *isec : slices[i]) {
2020-10-26 10:12:35 +03:00
off = align_to(off, isec->shdr.sh_addralign);
isec->offset = off;
off += isec->shdr.sh_size;
align = std::max<u32>(align, isec->shdr.sh_addralign);
2020-10-26 10:12:35 +03:00
}
size[i] = off;
alignments[i] = align;
});
u32 align = *std::max_element(alignments.begin(), alignments.end());
2020-10-26 10:12:35 +03:00
std::vector<u64> start(slices.size());
2020-10-28 07:42:05 +03:00
for (int i = 1; i < slices.size(); i++)
2020-11-10 06:23:14 +03:00
start[i] = align_to(start[i - 1] + size[i - 1], align);
2020-10-26 10:58:49 +03:00
2020-10-28 07:42:05 +03:00
tbb::parallel_for(1, (int)slices.size(), [&](int i) {
2020-11-08 06:36:08 +03:00
for (InputChunk *isec : slices[i])
2020-10-26 10:12:35 +03:00
isec->offset += start[i];
});
osec->shdr.sh_size = start.back() + size.back();
2020-10-26 08:18:00 +03:00
osec->shdr.sh_addralign = align;
});
}
2020-11-03 10:19:21 +03:00
static void scan_rels(ArrayRef<ObjectFile *> files) {
2020-11-11 04:42:26 +03:00
MyTimer t("scan_rels", before_copy_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [&](ObjectFile *file) { file->scan_relocations(); });
2020-11-06 06:01:52 +03:00
2020-11-06 06:32:38 +03:00
for (ObjectFile *file : files) {
2020-11-11 15:32:41 +03:00
file->got_offset = out::got->shdr.sh_size;
out::got->shdr.sh_size += file->got_size;
2020-11-06 07:54:37 +03:00
2020-11-11 15:32:41 +03:00
file->gotplt_offset = out::gotplt->shdr.sh_size;
out::gotplt->shdr.sh_size += file->gotplt_size;
2020-11-06 07:54:37 +03:00
2020-11-11 15:32:41 +03:00
file->plt_offset = out::plt->shdr.sh_size;
out::plt->shdr.sh_size += file->plt_size;
2020-11-06 07:54:37 +03:00
2020-11-11 15:32:41 +03:00
file->relplt_offset = out::relplt->shdr.sh_size;
out::relplt->shdr.sh_size += file->relplt_size;
2020-11-11 10:46:14 +03:00
2020-11-11 15:32:41 +03:00
if (out::dynsym) {
file->dynsym_offset = out::dynsym->shdr.sh_size;
out::dynsym->shdr.sh_size += file->dynsym_size;
}
2020-11-11 10:46:14 +03:00
2020-11-11 15:32:41 +03:00
if (out::dynstr) {
file->dynstr_offset = out::dynstr->shdr.sh_size;
out::dynstr->shdr.sh_size += file->dynstr_size;
}
2020-11-06 06:32:38 +03:00
}
}
2020-11-06 06:42:03 +03:00
static void assign_got_offsets(ArrayRef<ObjectFile *> files) {
2020-11-11 04:42:26 +03:00
MyTimer t("assign_got_offsets", before_copy_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [&](ObjectFile *file) {
2020-11-06 06:32:38 +03:00
u32 got_offset = file->got_offset;
u32 gotplt_offset = file->gotplt_offset;
u32 plt_offset = file->plt_offset;
u32 relplt_offset = file->relplt_offset;
2020-11-11 11:21:23 +03:00
u32 dynsym_offset = file->dynsym_offset;
2020-11-06 06:32:38 +03:00
2020-11-03 10:18:30 +03:00
for (Symbol *sym : file->symbols) {
if (sym->file != file)
continue;
2020-11-06 10:40:37 +03:00
u8 flags = sym->flags.load(std::memory_order_relaxed);
if (flags & Symbol::NEEDS_GOT) {
2020-11-03 10:18:30 +03:00
sym->got_offset = got_offset;
2020-11-11 10:53:41 +03:00
got_offset += GOT_SIZE;
2020-11-03 10:18:30 +03:00
}
2020-11-06 10:40:37 +03:00
if (flags & Symbol::NEEDS_GOTTP) {
2020-11-03 10:18:30 +03:00
sym->gottp_offset = got_offset;
2020-11-11 10:53:41 +03:00
got_offset += GOT_SIZE;
2020-11-03 10:18:30 +03:00
}
2020-11-06 10:40:37 +03:00
if (flags & Symbol::NEEDS_PLT) {
2020-11-04 14:11:32 +03:00
// Write a .got.plt entry
2020-11-03 10:18:30 +03:00
sym->gotplt_offset = gotplt_offset;
2020-11-11 10:53:41 +03:00
gotplt_offset += GOT_SIZE;
2020-11-03 10:18:30 +03:00
2020-11-04 14:11:32 +03:00
// Write a .plt entry
2020-11-03 10:18:30 +03:00
sym->plt_offset = plt_offset;
2020-11-11 10:53:41 +03:00
plt_offset += PLT_SIZE;
2020-11-06 06:42:03 +03:00
// Write a .rela.dyn entry
sym->relplt_offset = relplt_offset;
relplt_offset += sizeof(ELF64LE::Rela);
}
2020-11-11 11:21:23 +03:00
if (flags & Symbol::NEEDS_DYNSYM) {
sym->dynsym_offset = dynsym_offset;
dynsym_offset += sizeof(ELF64LE::Sym);
}
2020-11-06 06:42:03 +03:00
}
});
}
static void write_got(u8 *buf, ArrayRef<ObjectFile *> files) {
2020-11-11 04:42:26 +03:00
MyTimer t("write_synthetic", copy_timer);
2020-11-11 15:32:41 +03:00
u8 *got = buf + out::got->shdr.sh_offset;
u8 *plt = buf + out::plt->shdr.sh_offset;
u8 *relplt = buf + out::relplt->shdr.sh_offset;
2020-11-12 08:40:39 +03:00
u8 *dynsym = buf + out::dynsym->shdr.sh_offset;
u8 *dynstr = buf + out::dynstr->shdr.sh_offset;
2020-11-06 06:42:03 +03:00
2020-11-11 15:32:41 +03:00
memset(buf + out::gotplt->shdr.sh_offset, 0, out::gotplt->shdr.sh_size);
2020-11-10 08:58:50 +03:00
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [&](ObjectFile *file) {
2020-11-11 13:04:00 +03:00
u32 dynstr_offset = file->dynstr_offset;
2020-11-06 06:42:03 +03:00
for (Symbol *sym : file->symbols) {
if (sym->file != file)
continue;
2020-11-06 10:40:37 +03:00
u8 flags = sym->flags.load(std::memory_order_relaxed);
if (flags & Symbol::NEEDS_GOT)
2020-11-06 06:50:26 +03:00
*(u64 *)(got + sym->got_offset) = sym->get_addr();
2020-11-06 06:42:03 +03:00
2020-11-06 10:40:37 +03:00
if (flags & Symbol::NEEDS_GOTTP)
2020-11-06 06:50:26 +03:00
*(u64 *)(got + sym->gottp_offset) = sym->get_addr() - out::tls_end;
2020-11-06 06:42:03 +03:00
2020-11-06 10:40:37 +03:00
if (flags & Symbol::NEEDS_PLT) {
2020-11-06 06:42:03 +03:00
// Write a .plt entry
2020-11-11 15:32:41 +03:00
u64 S = out::gotplt->shdr.sh_addr + sym->gotplt_offset;
u64 P = out::plt->shdr.sh_addr + sym->plt_offset;
out::plt->write_entry(plt + sym->plt_offset, S - P - 6);
2020-11-04 14:11:32 +03:00
// Write a .rela.dyn entry
2020-11-06 06:42:03 +03:00
auto *rel = (ELF64LE::Rela *)(relplt + sym->relplt_offset);
2020-11-10 07:39:39 +03:00
memset(rel, 0, sizeof(*rel));
2020-11-11 15:32:41 +03:00
rel->r_offset = out::gotplt->shdr.sh_addr + sym->gotplt_offset;
2020-11-04 14:11:32 +03:00
rel->setType(R_X86_64_IRELATIVE, false);
2020-11-06 06:50:26 +03:00
rel->r_addend = sym->get_addr();
2020-11-03 10:18:30 +03:00
}
2020-11-11 11:25:00 +03:00
2020-11-12 15:21:14 +03:00
if (flags & Symbol::NEEDS_DYNSYM) {
// Write to .dynsym
auto &esym = *(ELF64LE::Sym *)buf;
memset(&esym, 0, sizeof(esym));
esym.st_name = dynstr_offset;
esym.setType(sym->type);
esym.setBinding(STB_GLOBAL);
// Write to .dynstr
write_string(dynstr + dynstr_offset, sym->name);
dynstr_offset += sym->name.size() + 1;
// Write to .hash
if (out::hash)
out::hash->write_symbol(buf, sym);
}
2020-11-03 10:18:30 +03:00
}
2020-11-06 06:32:38 +03:00
});
2020-11-03 10:18:30 +03:00
}
2020-11-12 16:22:46 +03:00
static void write_shstrtab(u8 *buf, ArrayRef<OutputChunk *> chunks) {
2020-11-12 16:23:53 +03:00
int offset = out::shstrtab->shdr.sh_offset + 1;
2020-11-12 16:22:46 +03:00
for (OutputChunk *chunk : chunks) {
if (!chunk->name.empty()) {
2020-11-12 16:23:53 +03:00
write_string(buf + offset, chunk->name);
offset += chunk->name.size() + 1;
2020-11-12 16:22:46 +03:00
}
}
}
2020-11-12 18:02:33 +03:00
static void write_dso_paths(u8 *buf, ArrayRef<ObjectFile *> files) {
int offset = out::dynstr->shdr.sh_offset + 1;
for (ObjectFile *file : files) {
if (!file->soname.empty()) {
write_string(buf + offset, file->soname);
offset += file->soname.size() + 1;
}
}
}
2020-11-08 04:31:49 +03:00
static void write_merged_strings(u8 *buf, ArrayRef<ObjectFile *> files) {
2020-11-11 04:42:26 +03:00
MyTimer t("write_merged_strings", copy_timer);
2020-11-08 04:31:49 +03:00
tbb::parallel_for_each(files, [&](ObjectFile *file) {
2020-11-08 08:13:59 +03:00
for (MergeableSection &isec : file->mergeable_sections) {
u8 *base = buf + isec.parent.shdr.sh_offset + isec.offset;
2020-11-08 04:31:49 +03:00
2020-11-08 08:13:59 +03:00
for (StringPieceRef &ref : isec.pieces) {
2020-11-08 04:31:49 +03:00
StringPiece &piece = *ref.piece;
2020-11-10 09:03:40 +03:00
if (piece.isec == &isec)
2020-11-08 04:31:49 +03:00
memcpy(base + piece.output_offset, piece.data.data(), piece.data.size());
}
}
});
}
2020-11-11 08:13:39 +03:00
static void clear_padding(u8 *buf, ArrayRef<OutputChunk *> chunks, u64 filesize) {
2020-11-11 04:42:26 +03:00
MyTimer t("clear_padding", copy_timer);
2020-11-09 15:50:47 +03:00
auto zero = [&](OutputChunk *chunk, u64 next_start) {
2020-11-10 11:32:41 +03:00
u64 pos = chunk->shdr.sh_offset;
if (chunk->shdr.sh_type != SHT_NOBITS)
pos += chunk->shdr.sh_size;
memset(buf + pos, 0, next_start - pos);
2020-11-09 15:50:47 +03:00
};
2020-11-11 08:13:39 +03:00
for (int i = 1; i < chunks.size(); i++)
zero(chunks[i - 1], chunks[i]->shdr.sh_offset);
zero(chunks.back(), filesize);
2020-11-09 15:50:47 +03:00
}
2020-10-22 12:54:51 +03:00
// We want to sort output sections in the following order.
//
2020-10-22 17:19:48 +03:00
// alloc readonly data
// alloc readonly code
// alloc writable tdata
// alloc writable tbss
// alloc writable data
// alloc writable bss
// nonalloc
2020-11-12 09:25:05 +03:00
static int get_section_rank(const ELF64LE::Shdr &shdr) {
2020-10-29 12:31:06 +03:00
bool alloc = shdr.sh_flags & SHF_ALLOC;
bool writable = shdr.sh_flags & SHF_WRITE;
bool exec = shdr.sh_flags & SHF_EXECINSTR;
bool tls = shdr.sh_flags & SHF_TLS;
2020-10-30 06:47:35 +03:00
bool nobits = shdr.sh_type == SHT_NOBITS;
2020-10-22 17:19:48 +03:00
return (alloc << 5) | (!writable << 4) | (!exec << 3) | (tls << 2) | !nobits;
2020-10-22 12:54:51 +03:00
}
2020-10-29 12:31:06 +03:00
static void sort_output_chunks(std::vector<OutputChunk *> &chunks) {
2020-10-22 12:54:51 +03:00
}
2020-11-11 03:02:36 +03:00
static std::vector<u8> create_ehdr() {
ELF64LE::Ehdr hdr = {};
memcpy(&hdr.e_ident, "\177ELF", 4);
hdr.e_ident[EI_CLASS] = ELFCLASS64;
hdr.e_ident[EI_DATA] = ELFDATA2LSB;
hdr.e_ident[EI_VERSION] = EV_CURRENT;
hdr.e_ident[EI_OSABI] = 0;
hdr.e_ident[EI_ABIVERSION] = 0;
hdr.e_type = ET_EXEC;
hdr.e_machine = EM_X86_64;
hdr.e_version = EV_CURRENT;
hdr.e_entry = Symbol::intern("_start")->get_addr();
2020-11-11 15:32:41 +03:00
hdr.e_phoff = out::phdr->shdr.sh_offset;
hdr.e_shoff = out::shdr->shdr.sh_offset;
2020-11-11 03:02:36 +03:00
hdr.e_flags = 0;
hdr.e_ehsize = sizeof(ELF64LE::Ehdr);
hdr.e_phentsize = sizeof(ELF64LE::Phdr);
2020-11-11 15:32:41 +03:00
hdr.e_phnum = out::phdr->shdr.sh_size / sizeof(ELF64LE::Phdr);
2020-11-11 03:02:36 +03:00
hdr.e_shentsize = sizeof(ELF64LE::Shdr);
2020-11-11 15:32:41 +03:00
hdr.e_shnum = out::shdr->shdr.sh_size / sizeof(ELF64LE::Shdr);
hdr.e_shstrndx = out::shstrtab->shndx;
2020-11-11 03:02:36 +03:00
std::vector<u8> ret(sizeof(hdr));
memcpy(ret.data(), &hdr, sizeof(hdr));
return ret;
}
2020-11-10 15:54:11 +03:00
template<typename T>
static std::vector<u8> to_u8vector(const std::vector<T> &vec) {
std::vector<u8> ret(vec.size() * sizeof(T));
2020-11-10 17:31:47 +03:00
memcpy(ret.data(), vec.data(), ret.size());
2020-11-10 15:54:11 +03:00
return ret;
}
2020-10-22 14:01:10 +03:00
2020-11-11 08:13:39 +03:00
static std::vector<u8> create_shdr(ArrayRef<OutputChunk *> chunks) {
2020-11-10 15:54:11 +03:00
std::vector<ELF64LE::Shdr> vec(1);
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks)
2020-11-10 17:31:47 +03:00
if (chunk->kind != OutputChunk::HEADER)
2020-11-10 15:54:11 +03:00
vec.push_back(chunk->shdr);
return to_u8vector(vec);
2020-10-20 13:09:18 +03:00
}
2020-11-10 16:59:39 +03:00
static u32 to_phdr_flags(OutputChunk *chunk) {
2020-11-03 14:26:22 +03:00
u32 ret = PF_R;
2020-11-10 16:59:39 +03:00
if (chunk->shdr.sh_flags & SHF_WRITE)
2020-11-03 14:26:22 +03:00
ret |= PF_W;
2020-11-10 16:59:39 +03:00
if (chunk->shdr.sh_flags & SHF_EXECINSTR)
2020-11-03 14:26:22 +03:00
ret |= PF_X;
return ret;
}
2020-11-10 18:24:17 +03:00
static std::vector<u8> create_phdr(ArrayRef<OutputChunk *> chunks) {
std::vector<ELF64LE::Phdr> vec;
2020-11-03 14:26:22 +03:00
2020-11-10 18:24:17 +03:00
auto define = [&](u32 type, u32 flags, u32 align, OutputChunk *chunk) {
2020-11-11 03:25:43 +03:00
vec.push_back({});
ELF64LE::Phdr &phdr = vec.back();
2020-11-03 14:26:22 +03:00
phdr.p_type = type;
phdr.p_flags = flags;
2020-11-10 18:24:17 +03:00
phdr.p_align = std::max<u64>(align, chunk->shdr.sh_addralign);
phdr.p_offset = chunk->shdr.sh_offset;
phdr.p_filesz = (chunk->shdr.sh_type == SHT_NOBITS) ? 0 : chunk->shdr.sh_size;
phdr.p_vaddr = chunk->shdr.sh_addr;
phdr.p_memsz = chunk->shdr.sh_size;
if (type == PT_LOAD)
chunk->starts_new_ptload = true;
};
auto append = [&](OutputChunk *chunk) {
ELF64LE::Phdr &phdr = vec.back();
phdr.p_align = std::max<u64>(phdr.p_align, chunk->shdr.sh_addralign);
phdr.p_filesz = (chunk->shdr.sh_type == SHT_NOBITS)
? chunk->shdr.sh_offset - phdr.p_offset
: chunk->shdr.sh_offset + chunk->shdr.sh_size - phdr.p_offset;
phdr.p_memsz = chunk->shdr.sh_addr + chunk->shdr.sh_size - phdr.p_vaddr;
2020-11-03 14:26:22 +03:00
};
2020-11-10 16:59:39 +03:00
auto is_bss = [](OutputChunk *chunk) {
return chunk->shdr.sh_type == SHT_NOBITS && !(chunk->shdr.sh_flags & SHF_TLS);
};
2020-11-03 14:26:22 +03:00
// Create a PT_PHDR for the program header itself.
2020-11-11 15:32:41 +03:00
define(PT_PHDR, PF_R, 8, out::phdr);
2020-11-03 14:26:22 +03:00
2020-11-11 03:27:16 +03:00
// Create an PT_INTERP.
2020-11-11 15:32:41 +03:00
if (out::interp)
define(PT_INTERP, PF_R, 1, out::interp);
2020-11-03 14:26:22 +03:00
// Create PT_LOAD segments.
2020-11-10 16:59:39 +03:00
for (int i = 0, end = chunks.size(); i < end;) {
OutputChunk *first = chunks[i++];
if (!(first->shdr.sh_flags & SHF_ALLOC))
2020-11-03 14:26:22 +03:00
break;
2020-11-10 16:59:39 +03:00
u32 flags = to_phdr_flags(first);
2020-11-10 18:24:17 +03:00
define(PT_LOAD, flags, PAGE_SIZE, first);
2020-11-03 14:26:22 +03:00
2020-11-10 16:59:39 +03:00
if (!is_bss(first))
2020-11-11 03:27:16 +03:00
while (i < end && !is_bss(chunks[i]) && to_phdr_flags(chunks[i]) == flags)
2020-11-10 18:24:17 +03:00
append(chunks[i++]);
2020-11-03 14:26:22 +03:00
2020-11-11 03:27:16 +03:00
while (i < end && is_bss(chunks[i]) && to_phdr_flags(chunks[i]) == flags)
2020-11-10 18:24:17 +03:00
append(chunks[i++]);
2020-11-03 14:26:22 +03:00
}
// Create a PT_TLS.
2020-11-10 16:59:39 +03:00
for (int i = 0; i < chunks.size(); i++) {
if (chunks[i]->shdr.sh_flags & SHF_TLS) {
2020-11-10 18:24:17 +03:00
define(PT_TLS, to_phdr_flags(chunks[i]), 1, chunks[i]);
2020-11-11 03:27:16 +03:00
i++;
while (i < chunks.size() && (chunks[i]->shdr.sh_flags & SHF_TLS))
append(chunks[i++]);
2020-11-03 14:26:22 +03:00
}
}
2020-11-10 13:39:04 +03:00
// Add PT_DYNAMIC
2020-11-11 15:32:41 +03:00
if (out::dynamic)
define(PT_DYNAMIC, PF_R | PF_W, out::dynamic->shdr.sh_addralign, out::dynamic);
2020-11-03 14:26:22 +03:00
2020-11-10 18:24:17 +03:00
return to_u8vector(vec);
2020-11-03 14:26:22 +03:00
}
2020-11-11 07:45:09 +03:00
static std::vector<u8>
2020-11-11 08:13:39 +03:00
create_dynamic_section(ArrayRef<OutputChunk *> chunks) {
2020-11-10 15:45:42 +03:00
std::vector<u64> vec;
2020-11-10 13:33:27 +03:00
2020-11-10 15:45:42 +03:00
auto define = [&](u64 tag, u64 val) {
vec.push_back(tag);
vec.push_back(val);
2020-11-10 13:33:27 +03:00
};
2020-11-11 15:32:41 +03:00
define(DT_RELA, out::reldyn->shdr.sh_addr);
define(DT_RELASZ, out::reldyn->shdr.sh_size);
2020-11-10 15:45:42 +03:00
define(DT_RELAENT, sizeof(ELF64LE::Rela));
2020-11-11 15:32:41 +03:00
define(DT_JMPREL, out::relplt->shdr.sh_addr);
define(DT_PLTRELSZ, out::relplt->shdr.sh_size);
define(DT_PLTGOT, out::gotplt->shdr.sh_addr);
2020-11-11 05:20:22 +03:00
define(DT_PLTREL, DT_RELA);
2020-11-11 15:32:41 +03:00
define(DT_SYMTAB, out::dynsym->shdr.sh_addr);
2020-11-11 05:20:22 +03:00
define(DT_SYMENT, sizeof(ELF64LE::Sym));
2020-11-11 15:32:41 +03:00
define(DT_STRTAB, out::dynstr->shdr.sh_addr);
define(DT_STRSZ, out::dynstr->shdr.sh_size);
2020-11-12 05:28:26 +03:00
define(DT_HASH, out::hash->shdr.sh_addr);
2020-11-11 08:55:23 +03:00
define(DT_INIT_ARRAY, out::__init_array_start->value);
define(DT_INIT_ARRAYSZ, out::__init_array_end->value - out::__init_array_start->value);
define(DT_FINI_ARRAY, out::__fini_array_start->value);
define(DT_FINI_ARRAYSZ, out::__fini_array_end->value - out::__fini_array_start->value);
2020-11-10 15:45:42 +03:00
define(DT_NULL, 0);
2020-11-10 15:54:11 +03:00
return to_u8vector(vec);
2020-11-10 13:20:27 +03:00
}
2020-11-11 08:13:39 +03:00
static u64 set_osec_offsets(ArrayRef<OutputChunk *> chunks) {
2020-11-11 04:42:26 +03:00
MyTimer t("osec_offset", before_copy_timer);
u64 fileoff = 0;
u64 vaddr = 0x200000;
2020-10-26 08:16:13 +03:00
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-10-30 05:40:38 +03:00
if (chunk->starts_new_ptload)
2020-10-26 08:16:13 +03:00
vaddr = align_to(vaddr, PAGE_SIZE);
2020-11-03 14:37:27 +03:00
bool is_bss = chunk->shdr.sh_type == SHT_NOBITS;
if (!is_bss) {
2020-10-30 05:45:10 +03:00
if (vaddr % PAGE_SIZE > fileoff % PAGE_SIZE)
fileoff += vaddr % PAGE_SIZE - fileoff % PAGE_SIZE;
else if (vaddr % PAGE_SIZE < fileoff % PAGE_SIZE)
fileoff = align_to(fileoff, PAGE_SIZE) + vaddr % PAGE_SIZE;
}
2020-10-30 05:40:38 +03:00
fileoff = align_to(fileoff, chunk->shdr.sh_addralign);
2020-10-26 08:16:13 +03:00
vaddr = align_to(vaddr, chunk->shdr.sh_addralign);
chunk->shdr.sh_offset = fileoff;
if (chunk->shdr.sh_flags & SHF_ALLOC)
chunk->shdr.sh_addr = vaddr;
2020-11-03 14:37:27 +03:00
if (!is_bss)
2020-11-03 14:13:03 +03:00
fileoff += chunk->shdr.sh_size;
2020-10-30 05:40:38 +03:00
2020-11-03 14:37:27 +03:00
bool is_tbss = is_bss && (chunk->shdr.sh_flags & SHF_TLS);
2020-10-30 05:40:38 +03:00
if (!is_tbss)
2020-11-03 14:13:03 +03:00
vaddr += chunk->shdr.sh_size;
2020-10-26 08:16:13 +03:00
}
return fileoff;
}
2020-11-11 08:13:39 +03:00
static void fix_synthetic_symbols(ArrayRef<OutputChunk *> chunks) {
2020-11-04 08:23:39 +03:00
auto start = [&](OutputChunk *chunk, Symbol *sym) {
2020-11-11 04:45:52 +03:00
if (sym) {
sym->shndx = chunk->shndx;
sym->value = chunk->shdr.sh_addr;
}
};
2020-11-04 08:23:39 +03:00
auto stop = [&](OutputChunk *chunk, Symbol *sym) {
2020-11-11 04:45:52 +03:00
if (sym) {
sym->shndx = chunk->shndx;
sym->value = chunk->shdr.sh_addr + chunk->shdr.sh_size;
}
};
2020-11-04 08:23:39 +03:00
// __bss_start
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-11-08 10:09:01 +03:00
if (chunk->kind == OutputChunk::REGULAR && chunk->name == ".bss") {
2020-11-04 08:23:39 +03:00
start(chunk, out::__bss_start);
break;
}
}
// __ehdr_start
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-11-04 08:23:39 +03:00
if (chunk->shndx == 1) {
2020-11-04 08:41:40 +03:00
out::__ehdr_start->shndx = 1;
2020-11-12 07:15:29 +03:00
out::__ehdr_start->value = out::ehdr->shdr.sh_addr;
2020-11-04 08:23:39 +03:00
break;
}
}
// __rela_iplt_start and __rela_iplt_end
2020-11-11 15:32:41 +03:00
start(out::relplt, out::__rela_iplt_start);
stop(out::relplt, out::__rela_iplt_end);
2020-11-04 08:23:39 +03:00
// __{init,fini}_array_{start,end}
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-11-04 08:23:39 +03:00
switch (chunk->shdr.sh_type) {
case SHT_INIT_ARRAY:
start(chunk, out::__init_array_start);
stop(chunk, out::__init_array_end);
break;
case SHT_FINI_ARRAY:
start(chunk, out::__fini_array_start);
stop(chunk, out::__fini_array_end);
break;
}
}
// _end, end, _etext, etext, _edata and edata
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-11-08 10:09:01 +03:00
if (chunk->kind == OutputChunk::HEADER)
2020-11-04 08:23:39 +03:00
continue;
2020-11-12 07:19:19 +03:00
if (chunk->shdr.sh_flags & SHF_ALLOC)
2020-11-04 08:23:39 +03:00
stop(chunk, out::_end);
2020-11-12 07:19:19 +03:00
if (chunk->shdr.sh_flags & SHF_EXECINSTR)
2020-11-04 08:23:39 +03:00
stop(chunk, out::_etext);
2020-11-12 07:19:19 +03:00
if (chunk->shdr.sh_type != SHT_NOBITS && chunk->shdr.sh_flags & SHF_ALLOC)
2020-11-04 08:23:39 +03:00
stop(chunk, out::_edata);
}
// __start_ and __stop_ symbols
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-11-11 08:45:17 +03:00
if (is_c_identifier(chunk->name)) {
start(chunk, Symbol::intern(("__start_" + chunk->name).str()));
stop(chunk, Symbol::intern(("__stop_" + chunk->name).str()));
}
2020-11-04 08:23:39 +03:00
}
}
2020-11-09 11:38:12 +03:00
static u8 *open_output_file(u64 filesize) {
int fd = open(config.output.str().c_str(), O_RDWR | O_CREAT, 0777);
2020-11-09 06:30:13 +03:00
if (fd == -1)
error("cannot open " + config.output + ": " + strerror(errno));
2020-11-03 14:29:24 +03:00
2020-11-09 11:38:12 +03:00
if (ftruncate(fd, filesize))
error("ftruncate");
2020-11-03 14:29:24 +03:00
2020-11-09 11:38:12 +03:00
void *buf = mmap(nullptr, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED)
2020-11-09 06:30:13 +03:00
error(config.output + ": mmap failed: " + strerror(errno));
2020-11-09 11:38:12 +03:00
close(fd);
2020-11-12 07:33:57 +03:00
if (config.filler != -1)
memset(buf, config.filler, filesize);
2020-11-09 11:38:12 +03:00
return (u8 *)buf;
2020-11-03 14:29:24 +03:00
}
2020-10-29 16:32:55 +03:00
static void write_symtab(u8 *buf, std::vector<ObjectFile *> files) {
2020-11-11 04:42:26 +03:00
MyTimer t("write_symtab", copy_timer);
2020-11-08 11:55:48 +03:00
std::vector<u64> local_symtab_off(files.size() + 1);
std::vector<u64> local_strtab_off(files.size() + 1);
local_symtab_off[0] = sizeof(ELF64LE::Sym);
local_strtab_off[0] = 1;
2020-10-27 18:21:41 +03:00
2020-10-28 10:06:39 +03:00
for (int i = 1; i < files.size() + 1; i++) {
2020-11-08 11:55:48 +03:00
local_symtab_off[i] = local_symtab_off[i - 1] + files[i - 1]->local_symtab_size;
local_strtab_off[i] = local_strtab_off[i - 1] + files[i - 1]->local_strtab_size;
2020-10-27 18:21:41 +03:00
}
2020-11-11 15:32:41 +03:00
out::symtab->shdr.sh_info = local_symtab_off.back() / sizeof(ELF64LE::Sym);
2020-10-27 18:21:41 +03:00
2020-11-08 11:55:48 +03:00
std::vector<u64> global_symtab_off(files.size() + 1);
std::vector<u64> global_strtab_off(files.size() + 1);
global_symtab_off[0] = local_symtab_off.back();
global_strtab_off[0] = local_strtab_off.back();
2020-10-27 18:21:41 +03:00
2020-10-28 10:06:39 +03:00
for (int i = 1; i < files.size() + 1; i++) {
2020-11-08 11:55:48 +03:00
global_symtab_off[i] = global_symtab_off[i - 1] + files[i - 1]->global_symtab_size;
global_strtab_off[i] = global_strtab_off[i - 1] + files[i - 1]->global_strtab_size;
2020-10-27 18:21:41 +03:00
}
2020-11-11 15:32:41 +03:00
assert(global_symtab_off.back() == out::symtab->shdr.sh_size);
assert(global_strtab_off.back() == out::strtab->shdr.sh_size);
2020-10-27 18:21:41 +03:00
2020-11-08 11:55:48 +03:00
tbb::parallel_for((size_t)0, files.size(), [&](size_t i) {
files[i]->write_local_symtab(buf, local_symtab_off[i], local_strtab_off[i]);
files[i]->write_global_symtab(buf, global_symtab_off[i], global_strtab_off[i]);
});
2020-11-10 08:58:50 +03:00
2020-10-27 18:21:41 +03:00
}
2020-10-30 07:47:51 +03:00
static int get_thread_count(InputArgList &args) {
if (auto *arg = args.getLastArg(OPT_thread_count)) {
int n;
if (!llvm::to_integer(arg->getValue(), n) || n <= 0)
error(arg->getSpelling() + ": expected a positive integer, but got '" +
arg->getValue() + "'");
return n;
}
return tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism);
}
2020-10-23 06:09:27 +03:00
2020-11-10 15:54:11 +03:00
static void write_vector(u8 *buf, ArrayRef<u8> vec) {
memcpy(buf, vec.data(), vec.size());
}
2020-11-12 07:33:57 +03:00
static int parse_filler(opt::InputArgList &args) {
auto *arg = args.getLastArg(OPT_filler);
if (!arg)
return -1;
StringRef val = arg->getValue();
if (!val.startswith("0x"))
error("invalid argument: " + arg->getAsString(args));
int ret;
if (!to_integer(val.substr(2), ret, 16))
error("invalid argument: " + arg->getAsString(args));
return (u8)ret;
}
2020-10-30 07:47:51 +03:00
int main(int argc, char **argv) {
2020-10-14 13:59:51 +03:00
// Parse command line options
2020-10-02 10:47:51 +03:00
MyOptTable opt_table;
2020-10-09 15:10:12 +03:00
InputArgList args = opt_table.parse(argc - 1, argv + 1);
2020-10-02 10:47:51 +03:00
2020-10-30 07:47:51 +03:00
tbb::global_control tbb_cont(tbb::global_control::max_allowed_parallelism,
get_thread_count(args));
2020-11-03 12:02:28 +03:00
Counter::enabled = args.hasArg(OPT_stat);
2020-10-04 12:00:33 +03:00
if (auto *arg = args.getLastArg(OPT_o))
config.output = arg->getValue();
else
error("-o option is missing");
2020-10-29 06:24:54 +03:00
config.print_map = args.hasArg(OPT_print_map);
2020-11-04 12:47:13 +03:00
config.is_static = args.hasArg(OPT_static);
2020-11-12 07:33:57 +03:00
config.filler = parse_filler(args);
2020-10-29 06:24:54 +03:00
2020-11-05 02:31:32 +03:00
for (auto *arg : args.filtered(OPT_trace_symbol))
Symbol::intern(arg->getValue())->traced = true;
2020-10-09 14:47:45 +03:00
std::vector<ObjectFile *> files;
2020-10-13 14:35:35 +03:00
// Open input files
2020-10-25 03:38:53 +03:00
{
2020-11-11 04:42:26 +03:00
MyTimer t("open", parse_timer);
2020-10-25 03:38:53 +03:00
for (auto *arg : args)
if (arg->getOption().getID() == OPT_INPUT)
read_file(files, arg->getValue());
2020-11-07 17:00:01 +03:00
}
2020-10-25 03:38:53 +03:00
2020-11-07 17:00:01 +03:00
// Parse input files
{
2020-11-11 04:42:26 +03:00
MyTimer t("parse", parse_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) { file->parse(); });
2020-10-25 03:38:53 +03:00
}
2020-10-18 13:17:44 +03:00
2020-11-07 12:06:09 +03:00
{
2020-11-11 04:42:26 +03:00
MyTimer t("merge", parse_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) {
file->initialize_mergeable_sections();
});
2020-11-07 12:06:09 +03:00
}
2020-11-06 10:58:13 +03:00
Timer total_timer("total", "total");
total_timer.startTimer();
2020-11-11 15:32:41 +03:00
out::ehdr = new OutputHeader;
out::shdr = new OutputHeader;
out::phdr = new OutputHeader;
out::got = new SpecialSection(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 8);
out::gotplt = new SpecialSection(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 8);
out::relplt = new SpecialSection(".rela.plt", SHT_RELA, SHF_ALLOC,
8, sizeof(ELF64LE::Rela));
2020-11-12 16:10:47 +03:00
out::strtab = new StrtabSection(".strtab", 0);
2020-11-12 16:22:46 +03:00
out::shstrtab = new StrtabSection(".shstrtab", 0);
2020-11-11 15:32:41 +03:00
out::plt = new PltSection;
out::symtab = new SymtabSection(".symtab", 0);
2020-11-12 08:40:39 +03:00
out::dynsym = new SymtabSection(".dynsym", SHF_ALLOC);
2020-11-12 16:10:47 +03:00
out::dynstr = new StrtabSection(".dynstr", SHF_ALLOC);
2020-11-12 08:40:39 +03:00
out::dynsym->shdr.sh_size = sizeof(ELF64LE::Sym);
2020-11-11 15:32:41 +03:00
2020-11-10 13:33:27 +03:00
if (!config.is_static) {
2020-11-11 15:32:41 +03:00
out::interp = new SpecialSection(".interp", SHT_PROGBITS, SHF_ALLOC);
out::dynamic = new SpecialSection(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE,
2020-11-12 06:01:06 +03:00
8, sizeof(ELF64LE::Dyn));
out::reldyn = new SpecialSection(".rela.dyn", SHT_RELA, SHF_ALLOC, 8,
sizeof(ELF64LE::Rela));
2020-11-11 15:32:41 +03:00
out::hash = new HashSection;
2020-11-11 16:14:12 +03:00
out::interp->shdr.sh_size = config.dynamic_linker.size() + 1;
2020-11-10 13:33:27 +03:00
}
2020-10-18 13:05:28 +03:00
// Set priorities to files
2020-10-28 04:15:05 +03:00
int priority = 1;
for (ObjectFile *file : files)
2020-11-05 06:34:59 +03:00
if (!file->is_in_archive)
2020-10-28 04:15:05 +03:00
file->priority = priority++;
for (ObjectFile *file : files)
2020-11-05 06:34:59 +03:00
if (file->is_in_archive)
2020-10-28 04:15:05 +03:00
file->priority = priority++;
2020-10-18 13:05:28 +03:00
2020-11-11 04:42:26 +03:00
// Resolve symbols and fix the set of object files that are
// included to the final output.
resolve_symbols(files);
2020-10-19 15:50:33 +03:00
2020-11-05 03:24:47 +03:00
if (args.hasArg(OPT_trace))
for (ObjectFile *file : files)
llvm::outs() << toString(file) << "\n";
2020-11-11 04:42:26 +03:00
// Remove redundant comdat sections (e.g. duplicate inline functions).
eliminate_comdats(files);
2020-10-10 06:47:12 +03:00
2020-11-11 04:42:26 +03:00
// Merge strings constants in SHF_MERGE sections.
handle_mergeable_strings(files);
2020-11-07 14:29:06 +03:00
2020-10-27 06:50:25 +03:00
// Create .bss sections for common symbols.
{
2020-11-11 04:42:26 +03:00
MyTimer t("common", before_copy_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files,
[](ObjectFile *file) { file->convert_common_symbols(); });
2020-10-27 06:50:25 +03:00
}
2020-10-26 05:34:26 +03:00
// Bin input sections into output sections
2020-11-11 04:42:26 +03:00
bin_sections(files);
2020-10-23 04:27:11 +03:00
2020-10-29 12:19:10 +03:00
// Assign offsets within an output section to input sections.
2020-11-11 04:42:26 +03:00
set_isec_offsets();
2020-10-22 10:35:17 +03:00
2020-11-09 04:06:50 +03:00
// Create a list of output sections.
2020-11-11 08:13:39 +03:00
std::vector<OutputChunk *> chunks;
2020-11-07 14:29:06 +03:00
2020-11-12 10:09:17 +03:00
// Sections are added to the section lists in an arbitrary order because
// they are created in parallel. Sor them to to make the output deterministic.
2020-11-12 09:19:30 +03:00
auto section_compare = [](OutputChunk *x, OutputChunk *y) {
return std::make_tuple(x->name, (u32)x->shdr.sh_type, (u64)x->shdr.sh_flags) <
std::make_tuple(y->name, (u32)y->shdr.sh_type, (u64)y->shdr.sh_flags);
};
std::stable_sort(OutputSection::instances.begin(), OutputSection::instances.end(),
section_compare);
std::stable_sort(MergedSection::instances.begin(), MergedSection::instances.end(),
section_compare);
2020-11-12 10:09:17 +03:00
// Add sections to the section lists
2020-11-04 04:39:17 +03:00
for (OutputSection *osec : OutputSection::instances)
2020-11-11 16:14:12 +03:00
if (osec->shdr.sh_size)
chunks.push_back(osec);
2020-11-07 15:53:21 +03:00
for (MergedSection *osec : MergedSection::instances)
2020-11-11 16:14:12 +03:00
if (osec->shdr.sh_size)
chunks.push_back(osec);
2020-11-07 14:31:09 +03:00
2020-11-04 04:39:17 +03:00
// Create a dummy file containing linker-synthesized symbols
// (e.g. `__bss_start`).
2020-11-11 08:13:39 +03:00
ObjectFile *internal_file = ObjectFile::create_internal_file(chunks);
2020-11-04 08:49:30 +03:00
internal_file->priority = priority++;
files.push_back(internal_file);
2020-11-04 04:39:17 +03:00
2020-11-09 05:31:00 +03:00
// Beyond this point, no new symbols will be added to the result.
2020-10-23 03:21:40 +03:00
// Scan relocations to fix the sizes of .got, .plt, .got.plt, .dynstr,
// .rela.dyn, .rela.plt.
2020-11-11 04:42:26 +03:00
scan_rels(files);
2020-10-27 15:14:33 +03:00
// Compute .symtab and .strtab sizes
2020-10-27 14:24:28 +03:00
{
2020-11-11 04:42:26 +03:00
MyTimer t("symtab_size", before_copy_timer);
2020-11-07 18:47:34 +03:00
tbb::parallel_for_each(files, [](ObjectFile *file) { file->compute_symtab(); });
2020-10-27 14:34:45 +03:00
2020-10-27 14:58:28 +03:00
for (ObjectFile *file : files) {
2020-11-11 15:32:41 +03:00
out::symtab->shdr.sh_size += file->local_symtab_size + file->global_symtab_size;
out::strtab->shdr.sh_size += file->local_strtab_size + file->global_strtab_size;
2020-10-27 14:58:28 +03:00
}
2020-10-27 14:24:28 +03:00
}
2020-11-09 03:47:58 +03:00
// Add synthetic sections.
2020-11-11 15:32:41 +03:00
chunks.push_back(out::got);
chunks.push_back(out::plt);
chunks.push_back(out::gotplt);
chunks.push_back(out::relplt);
chunks.push_back(out::reldyn);
chunks.push_back(out::dynamic);
chunks.push_back(out::dynsym);
chunks.push_back(out::dynstr);
chunks.push_back(out::shstrtab);
chunks.push_back(out::symtab);
chunks.push_back(out::strtab);
chunks.push_back(out::hash);
2020-11-11 08:13:39 +03:00
chunks.erase(std::remove_if(chunks.begin(), chunks.end(),
2020-11-11 16:14:12 +03:00
[](OutputChunk *c){ return !c; }),
chunks.end());
2020-11-09 03:47:58 +03:00
// Sort the sections by section flags so that we'll have to create
// as few segments as possible.
2020-11-12 09:25:05 +03:00
std::stable_sort(chunks.begin(), chunks.end(), [](OutputChunk *a, OutputChunk *b) {
return get_section_rank(a->shdr) > get_section_rank(b->shdr);
});
2020-10-29 12:31:06 +03:00
2020-11-09 03:47:58 +03:00
// Add headers and sections that have to be at the beginning
// or the ending of a file.
2020-11-11 15:32:41 +03:00
chunks.insert(chunks.begin(), out::ehdr);
chunks.insert(chunks.begin() + 1, out::phdr);
if (out::interp)
chunks.insert(chunks.begin() + 2, out::interp);
chunks.push_back(out::shdr);
2020-10-27 11:36:55 +03:00
// Fix .shstrtab contents.
2020-11-12 16:22:46 +03:00
for (OutputChunk *chunk : chunks) {
if (!chunk->name.empty()) {
chunk->shdr.sh_name = out::shstrtab->shdr.sh_size;
out::shstrtab->shdr.sh_size += chunk->name.size() + 1;
}
}
2020-10-20 13:09:18 +03:00
2020-11-12 17:46:36 +03:00
// Reserve space in .dynsym for DT_NEEDED strings.
for (ObjectFile *file : files)
if (file->is_alive && file->is_dso)
2020-11-12 18:02:33 +03:00
out::dynstr->shdr.sh_size += file->soname.size() + 1;
2020-11-12 17:46:36 +03:00
2020-11-10 17:31:47 +03:00
// Set section indices.
2020-11-11 08:13:39 +03:00
for (int i = 0, shndx = 1; i < chunks.size(); i++)
if (chunks[i]->kind != OutputChunk::HEADER)
chunks[i]->shndx = shndx++;
2020-11-10 17:31:47 +03:00
2020-11-10 13:20:27 +03:00
// Initialize synthetic section contents
2020-11-11 15:32:41 +03:00
out::ehdr->shdr.sh_size = sizeof(ELF64LE::Ehdr);
out::shdr->shdr.sh_size = create_shdr(chunks).size();
out::phdr->shdr.sh_size = create_phdr(chunks).size();
if (out::dynamic)
out::dynamic->shdr.sh_size = create_dynamic_section(chunks).size();
2020-11-12 14:38:46 +03:00
if (out::hash)
out::hash->set_num_dynsym(out::dynsym->shdr.sh_size / sizeof(ELF64LE::Sym));
2020-11-11 15:12:35 +03:00
2020-11-11 15:32:41 +03:00
out::symtab->shdr.sh_link = out::strtab->shndx;
if (out::dynsym) {
out::dynsym->shdr.sh_info = 1;
out::dynsym->shdr.sh_link = out::dynstr->shndx;
}
2020-10-22 14:53:20 +03:00
2020-11-12 06:13:25 +03:00
if (out::hash && out::dynsym)
out::hash->shdr.sh_link = out::dynsym->shndx;
if (out::dynamic && out::dynstr)
out::dynamic->shdr.sh_link = out::dynstr->shndx;
2020-10-30 10:55:59 +03:00
// Assign offsets to output sections
2020-11-11 08:13:39 +03:00
u64 filesize = set_osec_offsets(chunks);
2020-10-19 17:37:29 +03:00
2020-11-06 06:42:03 +03:00
// Assign symbols to GOT offsets
2020-11-11 04:42:26 +03:00
assign_got_offsets(files);
2020-11-06 06:42:03 +03:00
2020-11-03 10:51:28 +03:00
// Fix linker-synthesized symbol addresses.
2020-11-11 08:13:39 +03:00
fix_synthetic_symbols(chunks);
2020-11-01 07:05:51 +03:00
2020-11-09 03:58:35 +03:00
// At this point, file layout is fixed. Beyond this, you can assume
// that symbol addresses including their GOT/PLT/etc addresses have
// a correct final value.
2020-11-11 04:42:26 +03:00
// Some types of relocations for TLS symbols need the ending address
// of the TLS section. Find it out now.
2020-11-11 08:13:39 +03:00
for (OutputChunk *chunk : chunks) {
2020-11-06 06:50:26 +03:00
ELF64LE::Shdr &shdr = chunk->shdr;
if (shdr.sh_flags & SHF_TLS)
out::tls_end = align_to(shdr.sh_addr + shdr.sh_size, shdr.sh_addralign);
}
2020-10-26 08:38:43 +03:00
2020-11-09 10:41:26 +03:00
// Create an output file
2020-11-09 11:38:12 +03:00
u8 *buf;
{
2020-11-11 04:42:26 +03:00
MyTimer t("open_file", before_copy_timer);
2020-11-09 11:38:12 +03:00
buf = open_output_file(filesize);
}
2020-11-09 10:41:26 +03:00
2020-11-12 16:06:47 +03:00
// Initialize the output buffer.
{
MyTimer t("copy", copy_timer);
tbb::parallel_for_each(chunks, [&](OutputChunk *chunk) {
chunk->initialize(buf);
});
}
// Copy input sections to the output file
2020-10-25 03:38:53 +03:00
{
2020-11-11 04:42:26 +03:00
MyTimer t("copy", copy_timer);
2020-11-11 08:13:39 +03:00
tbb::parallel_for_each(chunks, [&](OutputChunk *chunk) {
chunk->copy_to(buf);
});
2020-10-30 05:40:38 +03:00
}
2020-10-20 03:20:52 +03:00
2020-11-06 04:12:05 +03:00
// Fill .symtab and .strtab
2020-11-11 04:42:26 +03:00
write_symtab(buf, files);
2020-11-06 04:12:05 +03:00
2020-11-12 16:22:46 +03:00
// Fill .shstrtab
write_shstrtab(buf, chunks);
2020-11-12 18:02:33 +03:00
// Write DT_NEEDED paths to .dynstr.
write_dso_paths(buf, files);
2020-11-10 15:54:11 +03:00
// Fill .plt, .got, got.plt, .rela.plt sections
2020-11-11 04:42:26 +03:00
write_got(buf, files);
2020-11-06 06:42:03 +03:00
2020-11-07 16:54:07 +03:00
// Fill mergeable string sections
2020-11-11 04:42:26 +03:00
write_merged_strings(buf, files);
2020-11-07 16:54:07 +03:00
2020-11-10 15:54:11 +03:00
// Write headers and synthetic sections.
2020-11-11 15:32:41 +03:00
write_vector(buf + out::ehdr->shdr.sh_offset, create_ehdr());
write_vector(buf + out::shdr->shdr.sh_offset, create_shdr(chunks));
write_vector(buf + out::phdr->shdr.sh_offset, create_phdr(chunks));
if (out::interp)
write_string(buf + out::interp->shdr.sh_offset, config.dynamic_linker);
if (out::dynamic)
write_vector(buf + out::dynamic->shdr.sh_offset, create_dynamic_section(chunks));
2020-11-10 15:54:11 +03:00
2020-11-09 15:50:47 +03:00
// Zero-clear paddings between sections
2020-11-11 08:13:39 +03:00
clear_padding(buf, chunks, filesize);
2020-11-09 15:50:47 +03:00
2020-11-09 10:41:26 +03:00
// Commit
2020-10-25 03:38:53 +03:00
{
2020-11-11 04:42:26 +03:00
MyTimer t("munmap", copy_timer);
2020-11-09 10:41:26 +03:00
munmap(buf, filesize);
2020-10-25 03:38:53 +03:00
}
2020-10-14 12:41:09 +03:00
2020-11-06 10:58:13 +03:00
total_timer.stopTimer();
2020-10-29 06:24:54 +03:00
if (config.print_map) {
MyTimer t("print_map");
2020-11-11 08:13:39 +03:00
print_map(files, chunks);
2020-10-29 06:24:54 +03:00
}
2020-10-28 13:34:32 +03:00
#if 0
2020-10-28 12:29:31 +03:00
for (ObjectFile *file : files)
for (InputSection *isec : file->sections)
if (isec)
llvm::outs() << toString(isec) << "\n";
2020-10-28 13:34:32 +03:00
#endif
2020-10-28 12:29:31 +03:00
2020-11-03 11:36:43 +03:00
// Show stat numbers
2020-11-03 11:54:40 +03:00
Counter num_input_sections("input_sections");
2020-11-03 11:36:43 +03:00
for (ObjectFile *file : files)
2020-11-03 11:54:40 +03:00
num_input_sections.inc(file->sections.size());
2020-11-03 11:36:43 +03:00
2020-11-11 08:13:39 +03:00
Counter num_output_chunks("output_chunks", chunks.size());
2020-11-03 11:43:06 +03:00
Counter num_files("files", files.size());
Counter filesize_counter("filesize", filesize);
2020-11-03 11:36:43 +03:00
2020-11-03 11:28:06 +03:00
Counter::print();
2020-10-18 10:21:17 +03:00
llvm::TimerGroup::printAll(llvm::outs());
2020-10-28 13:27:23 +03:00
llvm::outs().flush();
2020-10-18 10:21:17 +03:00
_exit(0);
2020-09-29 09:05:29 +03:00
}