2020-10-20 08:54:35 +03:00
|
|
|
#include "mold.h"
|
2020-10-02 07:28:26 +03:00
|
|
|
|
2020-09-29 09:05:29 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
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-10-02 10:47:51 +03:00
|
|
|
Config config;
|
2020-10-02 07:28:26 +03:00
|
|
|
|
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) {
|
|
|
|
unsigned missingIndex;
|
|
|
|
unsigned missingCount;
|
|
|
|
SmallVector<const char *, 256> vec(argv, argv + argc);
|
|
|
|
|
|
|
|
InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
|
|
|
|
if (missingCount)
|
|
|
|
error(Twine(args.getArgString(missingIndex)) + ": missing argument");
|
|
|
|
|
|
|
|
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-10-10 06:47:12 +03:00
|
|
|
MemoryBufferRef mb = readFile(path);
|
|
|
|
|
|
|
|
switch (identify_magic(mb.getBuffer())) {
|
|
|
|
case file_magic::archive:
|
2020-10-14 13:36:06 +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-10-22 11:26:23 +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-26 07:36:56 +03:00
|
|
|
static void bin_sections(std::vector<ObjectFile *> files) {
|
|
|
|
#if 1
|
|
|
|
typedef std::vector<std::vector<InputSection *>> T;
|
|
|
|
|
|
|
|
auto fn = [&](const tbb::blocked_range<int> &range, const T &init) {
|
|
|
|
T vec = init;
|
|
|
|
for (int i = range.begin(); i < range.end(); i++) {
|
|
|
|
ObjectFile *file = files[i];
|
|
|
|
for (InputSection *isec : file->sections) {
|
|
|
|
if (!isec)
|
|
|
|
continue;
|
|
|
|
OutputSection *osec = isec->output_section;
|
|
|
|
vec[osec->idx].push_back(isec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto reduce = [](const T &x, const T &y) {
|
|
|
|
T ret(x.size());
|
|
|
|
for (int i = 0; i < x.size(); i++)
|
|
|
|
ret[i] = x[i];
|
|
|
|
for (int i = 0; i < x.size(); i++)
|
|
|
|
ret[i].insert(ret[i].end(), y[i].begin(), y[i].end());
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::vector<InputSection *>> vec =
|
|
|
|
tbb::parallel_reduce(tbb::blocked_range<int>(0, files.size()),
|
|
|
|
T(OutputSection::all_instances.size()),
|
|
|
|
fn, reduce);
|
|
|
|
|
|
|
|
for (int i = 0; i < vec.size(); i++)
|
2020-10-26 08:32:36 +03:00
|
|
|
OutputSection::all_instances[i]->sections = std::move(vec[i]);
|
2020-10-26 07:36:56 +03:00
|
|
|
|
|
|
|
#else
|
|
|
|
for (ObjectFile *file : files) {
|
|
|
|
for (InputSection *isec : file->sections) {
|
|
|
|
if (!isec)
|
|
|
|
continue;
|
|
|
|
OutputSection *osec = isec->output_section;
|
2020-10-26 08:32:36 +03:00
|
|
|
osec->sections.push_back(isec);
|
2020-10-26 07:36:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-10-22 17:19:48 +03:00
|
|
|
|
2020-10-26 08:18:00 +03:00
|
|
|
static void set_isec_offsets() {
|
2020-10-26 10:12:56 +03:00
|
|
|
#if 1
|
2020-10-26 09:39:57 +03:00
|
|
|
for_each(OutputSection::all_instances, [&](OutputSection *osec) {
|
2020-10-27 07:52:10 +03:00
|
|
|
if (osec->sections.empty())
|
|
|
|
return;
|
|
|
|
|
2020-10-26 10:12:35 +03:00
|
|
|
int unit = 100000;
|
|
|
|
int num_slices = (osec->sections.size() + unit - 1) / unit;
|
|
|
|
|
|
|
|
std::vector<uint64_t> start(num_slices);
|
|
|
|
std::vector<uint64_t> size(num_slices);
|
|
|
|
std::vector<uint32_t> alignments(num_slices);
|
|
|
|
|
|
|
|
std::vector<ArrayRef<InputSection *>> slices;
|
|
|
|
ArrayRef<InputSection *> sections = makeArrayRef(osec->sections);
|
|
|
|
|
|
|
|
while (!sections.empty()) {
|
|
|
|
int end = std::min<int>(sections.size(), unit);
|
|
|
|
slices.push_back(sections.slice(0, end));
|
|
|
|
sections = sections.slice(end);
|
2020-10-26 08:18:00 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 10:12:35 +03:00
|
|
|
tbb::parallel_for(0, num_slices, [&](int i) {
|
|
|
|
uint64_t off = 0;
|
|
|
|
uint32_t align = 1;
|
|
|
|
|
|
|
|
for (InputSection *isec : slices[i]) {
|
|
|
|
off = align_to(off, isec->shdr.sh_addralign);
|
|
|
|
isec->offset = off;
|
|
|
|
off += isec->shdr.sh_size;
|
|
|
|
align = std::max<uint32_t>(align, isec->shdr.sh_addralign);
|
|
|
|
}
|
|
|
|
|
|
|
|
size[i] = off;
|
|
|
|
alignments[i] = align;
|
|
|
|
});
|
|
|
|
|
|
|
|
uint32_t align = *std::max_element(alignments.begin(), alignments.end());
|
|
|
|
|
2020-10-26 10:58:49 +03:00
|
|
|
for (int i = 1; i < num_slices; i++)
|
|
|
|
start[i] = align_to(start[i - 1] + size[i], align);
|
|
|
|
|
2020-10-26 10:12:35 +03:00
|
|
|
tbb::parallel_for(1, num_slices, [&](int i) {
|
|
|
|
for (InputSection *isec : slices[i])
|
|
|
|
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-10-26 10:12:56 +03:00
|
|
|
#else
|
2020-10-26 15:57:25 +03:00
|
|
|
for_each(OutputSection::all_instances, [&](OutputSection *osec) {
|
2020-10-27 07:52:10 +03:00
|
|
|
if (osec->sections.empty())
|
|
|
|
return;
|
|
|
|
|
2020-10-26 10:13:45 +03:00
|
|
|
uint64_t off = 0;
|
|
|
|
uint32_t align = 0;
|
|
|
|
|
|
|
|
for (InputSection *isec : osec->sections) {
|
|
|
|
off = align_to(off, isec->shdr.sh_addralign);
|
|
|
|
isec->offset = off;
|
|
|
|
off += isec->shdr.sh_size;
|
|
|
|
align = std::max<uint32_t>(align, isec->shdr.sh_addralign);
|
|
|
|
}
|
|
|
|
|
|
|
|
osec->shdr.sh_size = off;
|
2020-10-26 15:57:25 +03:00
|
|
|
osec->shdr.sh_addralign = align;
|
|
|
|
});
|
2020-10-26 10:12:56 +03:00
|
|
|
#endif
|
2020-10-26 08:18:00 +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-10-22 13:03:31 +03:00
|
|
|
static int get_rank(OutputSection *x) {
|
2020-10-25 07:17:10 +03:00
|
|
|
bool alloc = x->shdr.sh_flags & SHF_ALLOC;
|
|
|
|
bool writable = x->shdr.sh_flags & SHF_WRITE;
|
|
|
|
bool exec = x->shdr.sh_flags & SHF_EXECINSTR;
|
|
|
|
bool tls = x->shdr.sh_flags & SHF_TLS;
|
|
|
|
bool nobits = x->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-26 11:30:19 +03:00
|
|
|
static bool is_osec_empty(OutputSection *osec) {
|
|
|
|
if (osec->sections.empty())
|
|
|
|
return true;
|
|
|
|
for (InputSection *isec : osec->sections)
|
|
|
|
if (isec->shdr.sh_size)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-22 12:54:51 +03:00
|
|
|
static std::vector<OutputSection *> get_output_sections() {
|
|
|
|
std::vector<OutputSection *> vec;
|
|
|
|
for (OutputSection *osec : OutputSection::all_instances)
|
2020-10-26 11:30:19 +03:00
|
|
|
if (!is_osec_empty(osec))
|
2020-10-22 12:54:51 +03:00
|
|
|
vec.push_back(osec);
|
|
|
|
|
2020-10-22 13:03:31 +03:00
|
|
|
std::sort(vec.begin(), vec.end(), [](OutputSection *a, OutputSection *b) {
|
|
|
|
int x = get_rank(a);
|
|
|
|
int y = get_rank(b);
|
|
|
|
if (x != y)
|
|
|
|
return x > y;
|
|
|
|
|
|
|
|
// Tie-break to make output deterministic.
|
2020-10-25 07:17:10 +03:00
|
|
|
if (a->shdr.sh_flags != b->shdr.sh_flags)
|
|
|
|
return a->shdr.sh_flags < b->shdr.sh_flags;
|
|
|
|
if (a->shdr.sh_type != b->shdr.sh_type)
|
|
|
|
return a->shdr.sh_type < b->shdr.sh_type;
|
2020-10-22 13:03:31 +03:00
|
|
|
return a->name < b->name;
|
|
|
|
});
|
2020-10-22 12:54:51 +03:00
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:01:10 +03:00
|
|
|
static std::vector<ELF64LE::Shdr *>
|
2020-10-20 13:09:18 +03:00
|
|
|
create_shdrs(ArrayRef<OutputChunk *> output_chunks) {
|
2020-10-22 14:40:23 +03:00
|
|
|
static ELF64LE::Shdr null_entry = {};
|
2020-10-22 14:01:10 +03:00
|
|
|
|
|
|
|
std::vector<ELF64LE::Shdr *> vec;
|
|
|
|
vec.push_back(&null_entry);
|
|
|
|
|
2020-10-27 11:44:05 +03:00
|
|
|
int idx = 1;
|
|
|
|
for (OutputChunk *chunk : output_chunks) {
|
|
|
|
if (!chunk->name.empty()) {
|
2020-10-25 07:17:10 +03:00
|
|
|
vec.push_back(&chunk->shdr);
|
2020-10-27 11:44:05 +03:00
|
|
|
chunk->idx = idx++;
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 13:09:18 +03:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:01:10 +03:00
|
|
|
static void fill_shdrs(ArrayRef<OutputChunk *> output_chunks) {
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
for (OutputChunk *chunk : output_chunks) {
|
|
|
|
if (chunk->name.empty())
|
|
|
|
continue;
|
2020-10-26 05:37:35 +03:00
|
|
|
chunk->shdr.sh_size = chunk->get_size();
|
2020-10-22 14:01:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 08:18:00 +03:00
|
|
|
static uint64_t set_osec_offsets(ArrayRef<OutputChunk *> output_chunks) {
|
2020-10-26 08:16:13 +03:00
|
|
|
uint64_t fileoff = 0;
|
2020-10-26 11:09:43 +03:00
|
|
|
uint64_t vaddr = 0x400000;
|
2020-10-26 08:16:13 +03:00
|
|
|
|
|
|
|
for (OutputChunk *chunk : output_chunks) {
|
|
|
|
if (chunk->starts_new_ptload) {
|
2020-10-26 11:31:13 +03:00
|
|
|
fileoff = align_to(fileoff, PAGE_SIZE);
|
2020-10-26 08:16:13 +03:00
|
|
|
vaddr = align_to(vaddr, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chunk->is_bss())
|
|
|
|
fileoff = align_to(fileoff, chunk->shdr.sh_addralign);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!chunk->is_bss())
|
|
|
|
fileoff += chunk->get_size();
|
|
|
|
vaddr += chunk->get_size();
|
|
|
|
}
|
|
|
|
return fileoff;
|
|
|
|
}
|
|
|
|
|
2020-10-24 12:58:21 +03:00
|
|
|
static void unlink_async(tbb::task_group &tg, StringRef path) {
|
|
|
|
if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
if (std::error_code ec = sys::fs::openFileForRead(path, fd))
|
|
|
|
return;
|
|
|
|
sys::fs::remove(path);
|
|
|
|
tg.run([=]() { close(fd); });
|
|
|
|
}
|
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
class MyTimer {
|
2020-10-25 03:34:57 +03:00
|
|
|
public:
|
2020-10-25 03:38:53 +03:00
|
|
|
MyTimer(StringRef name) {
|
|
|
|
timer = new Timer(name, name);
|
|
|
|
timer->startTimer();
|
2020-10-25 03:34:57 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
MyTimer(StringRef name, llvm::TimerGroup &tg) {
|
|
|
|
timer = new Timer(name, name, tg);
|
|
|
|
timer->startTimer();
|
2020-10-25 03:34:57 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
~MyTimer() { timer->stopTimer(); }
|
2020-10-25 03:34:57 +03:00
|
|
|
|
|
|
|
private:
|
2020-10-25 03:38:53 +03:00
|
|
|
llvm::Timer *timer;
|
2020-10-25 03:34:57 +03:00
|
|
|
};
|
|
|
|
|
2020-09-29 09:05:29 +03:00
|
|
|
int main(int argc, char **argv) {
|
2020-10-26 16:02:10 +03:00
|
|
|
tbb::global_control tbb_cont(tbb::global_control::max_allowed_parallelism, 64);
|
2020-10-24 12:58:21 +03:00
|
|
|
tbb::task_group tg;
|
2020-10-23 06:09:27 +03:00
|
|
|
|
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-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-09 14:47:45 +03:00
|
|
|
std::vector<ObjectFile *> files;
|
|
|
|
|
2020-10-22 18:02:33 +03:00
|
|
|
llvm::TimerGroup before_copy("before_copy", "before_copy");
|
|
|
|
|
2020-10-13 14:35:35 +03:00
|
|
|
// Open input files
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
2020-10-26 06:16:41 +03:00
|
|
|
MyTimer t("parse");
|
2020-10-25 03:38:53 +03:00
|
|
|
for (auto *arg : args)
|
|
|
|
if (arg->getOption().getID() == OPT_INPUT)
|
|
|
|
read_file(files, arg->getValue());
|
|
|
|
|
|
|
|
// Parse input files
|
|
|
|
for_each(files, [](ObjectFile *file) { file->parse(); });
|
|
|
|
}
|
2020-10-18 13:17:44 +03:00
|
|
|
|
2020-10-18 13:05:28 +03:00
|
|
|
// Set priorities to files
|
2020-10-18 13:25:39 +03:00
|
|
|
for (int i = 0; i < files.size(); i++)
|
|
|
|
files[i]->priority = files[i]->is_in_archive() ? i + (1 << 31) : i;
|
2020-10-18 13:05:28 +03:00
|
|
|
|
2020-10-18 15:03:51 +03:00
|
|
|
// Resolve symbols
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
2020-10-26 10:45:53 +03:00
|
|
|
MyTimer t("resolve_symbols", before_copy);
|
2020-10-25 03:38:53 +03:00
|
|
|
for_each(files, [](ObjectFile *file) { file->register_defined_symbols(); });
|
|
|
|
for_each(files, [](ObjectFile *file) { file->register_undefined_symbols(); });
|
|
|
|
}
|
2020-10-19 15:50:33 +03:00
|
|
|
|
2020-10-19 16:04:24 +03:00
|
|
|
// Eliminate unused archive members.
|
|
|
|
files.erase(std::remove_if(files.begin(), files.end(),
|
|
|
|
[](ObjectFile *file){ return !file->is_alive; }),
|
|
|
|
files.end());
|
|
|
|
|
2020-10-19 15:50:33 +03:00
|
|
|
// Eliminate duplicate comdat groups.
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
|
|
|
MyTimer t("comdat", before_copy);
|
|
|
|
for_each(files, [](ObjectFile *file) { file->eliminate_duplicate_comdat_groups(); });
|
|
|
|
}
|
2020-10-10 06:47:12 +03:00
|
|
|
|
2020-10-27 06:50:25 +03:00
|
|
|
// Create .bss sections for common symbols.
|
|
|
|
{
|
|
|
|
MyTimer t("common", before_copy);
|
|
|
|
for_each(files, [](ObjectFile *file) { file->convert_common_symbols(); });
|
|
|
|
}
|
|
|
|
|
2020-10-26 05:34:26 +03:00
|
|
|
// Bin input sections into output sections
|
|
|
|
{
|
|
|
|
MyTimer t("bin_sections", before_copy);
|
2020-10-26 07:36:56 +03:00
|
|
|
bin_sections(files);
|
2020-10-26 05:34:26 +03:00
|
|
|
}
|
2020-10-23 04:27:11 +03:00
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
2020-10-26 05:34:26 +03:00
|
|
|
MyTimer t("isec_offsets", before_copy);
|
2020-10-26 08:18:00 +03:00
|
|
|
set_isec_offsets();
|
2020-10-25 03:38:53 +03:00
|
|
|
}
|
2020-10-22 10:35:17 +03:00
|
|
|
|
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-10-25 03:38:53 +03:00
|
|
|
{
|
|
|
|
MyTimer t("scan_rel", before_copy);
|
2020-10-26 05:34:26 +03:00
|
|
|
for_each(files, [](ObjectFile *file) { file->scan_relocations(); });
|
2020-10-25 03:38:53 +03:00
|
|
|
}
|
2020-10-23 03:21:40 +03:00
|
|
|
|
2020-10-27 15:14:33 +03:00
|
|
|
// Create linker-synthesized sections.
|
|
|
|
out::ehdr = new OutputEhdr;
|
|
|
|
out::phdr = new OutputPhdr;
|
|
|
|
out::shdr = new OutputShdr;
|
|
|
|
// out::interp = new InterpSection;
|
|
|
|
out::shstrtab = new ShstrtabSection;
|
|
|
|
out::symtab = new SymtabSection;
|
|
|
|
out::strtab = new StrtabSection;
|
|
|
|
|
|
|
|
// Compute .symtab and .strtab sizes
|
2020-10-27 14:24:28 +03:00
|
|
|
{
|
2020-10-27 14:58:28 +03:00
|
|
|
MyTimer t("symtab_size", before_copy);
|
|
|
|
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-10-27 16:08:49 +03:00
|
|
|
out::symtab->size += file->local_symtab_size + file->global_symtab_size;
|
|
|
|
out::strtab->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-10-22 13:41:27 +03:00
|
|
|
// Add ELF and program header to the output.
|
|
|
|
std::vector<OutputChunk *> output_chunks;
|
2020-10-20 08:37:17 +03:00
|
|
|
output_chunks.push_back(out::ehdr);
|
|
|
|
output_chunks.push_back(out::phdr);
|
|
|
|
|
2020-10-22 13:41:27 +03:00
|
|
|
// Add .interp section.
|
2020-10-26 11:15:01 +03:00
|
|
|
// output_chunks.push_back(out::interp);
|
2020-10-22 10:11:28 +03:00
|
|
|
|
2020-10-22 13:41:27 +03:00
|
|
|
// Add other output sections.
|
2020-10-27 11:55:44 +03:00
|
|
|
std::vector<OutputSection *> output_sections = get_output_sections();
|
|
|
|
for (OutputSection *osec : output_sections)
|
2020-10-22 12:54:51 +03:00
|
|
|
output_chunks.push_back(osec);
|
2020-10-19 12:13:55 +03:00
|
|
|
|
2020-10-22 13:41:27 +03:00
|
|
|
// Add a string table for section names.
|
2020-10-22 16:43:23 +03:00
|
|
|
output_chunks.push_back(out::shstrtab);
|
2020-10-27 11:36:55 +03:00
|
|
|
|
|
|
|
// Add a section header.
|
|
|
|
output_chunks.push_back(out::shdr);
|
|
|
|
|
|
|
|
// Add .symtab and .strtab.
|
|
|
|
output_chunks.push_back(out::symtab);
|
|
|
|
output_chunks.push_back(out::strtab);
|
|
|
|
|
|
|
|
// Fix .shstrtab contents.
|
2020-10-22 14:53:20 +03:00
|
|
|
for (OutputChunk *chunk : output_chunks)
|
|
|
|
if (!chunk->name.empty())
|
2020-10-25 07:17:10 +03:00
|
|
|
chunk->shdr.sh_name = out::shstrtab->add_string(chunk->name);
|
2020-10-20 13:09:18 +03:00
|
|
|
|
2020-10-27 11:36:55 +03:00
|
|
|
// Create section header and program header contents.
|
2020-10-25 06:41:31 +03:00
|
|
|
out::shdr->entries = create_shdrs(output_chunks);
|
2020-10-25 04:47:57 +03:00
|
|
|
out::phdr->construct(output_chunks);
|
2020-10-27 11:44:05 +03:00
|
|
|
out::symtab->shdr.sh_link = out::strtab->idx;
|
2020-10-22 14:53:20 +03:00
|
|
|
|
2020-10-25 14:12:48 +03:00
|
|
|
// Fill section header.
|
|
|
|
fill_shdrs(output_chunks);
|
|
|
|
|
2020-10-20 03:20:52 +03:00
|
|
|
// Assign offsets to input sections
|
|
|
|
uint64_t filesize = 0;
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
2020-10-26 08:36:39 +03:00
|
|
|
MyTimer t("osec_offset", before_copy);
|
2020-10-26 08:18:00 +03:00
|
|
|
filesize = set_osec_offsets(output_chunks);
|
2020-10-25 09:17:43 +03:00
|
|
|
}
|
2020-10-19 17:37:29 +03:00
|
|
|
|
2020-10-26 08:38:43 +03:00
|
|
|
{
|
|
|
|
MyTimer t("sym_addr");
|
2020-10-26 10:58:49 +03:00
|
|
|
for_each(files, [](ObjectFile *file) { file->fix_sym_addrs(); });
|
2020-10-26 08:38:43 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
2020-10-26 08:35:13 +03:00
|
|
|
MyTimer t("unlink");
|
2020-10-25 03:38:53 +03:00
|
|
|
unlink_async(tg, config.output);
|
|
|
|
}
|
2020-10-24 12:58:21 +03:00
|
|
|
|
2020-10-20 03:20:52 +03:00
|
|
|
// Create an output file
|
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> buf_or_err =
|
2020-10-26 11:08:36 +03:00
|
|
|
FileOutputBuffer::create(config.output, filesize, FileOutputBuffer::F_executable);
|
2020-10-20 03:20:52 +03:00
|
|
|
|
|
|
|
if (!buf_or_err)
|
|
|
|
error("failed to open " + config.output + ": " +
|
|
|
|
llvm::toString(buf_or_err.takeError()));
|
|
|
|
|
|
|
|
std::unique_ptr<FileOutputBuffer> output_buffer = std::move(*buf_or_err);
|
|
|
|
uint8_t *buf = output_buffer->getBufferStart();
|
|
|
|
|
2020-10-27 15:38:52 +03:00
|
|
|
// Fill .symtab and .strtab
|
2020-10-27 16:14:10 +03:00
|
|
|
tbb::task_group tg_symtab;
|
|
|
|
tg_symtab.run([&]() {
|
2020-10-27 15:43:33 +03:00
|
|
|
MyTimer t("write_symtab");
|
2020-10-27 15:38:52 +03:00
|
|
|
|
2020-10-27 16:08:49 +03:00
|
|
|
std::vector<uint64_t> symtab_off(files.size());
|
|
|
|
std::vector<uint64_t> strtab_off(files.size());
|
|
|
|
strtab_off[0] = 1;
|
2020-10-27 15:45:19 +03:00
|
|
|
|
2020-10-27 16:08:49 +03:00
|
|
|
for (int i = 1; i < files.size(); i++) {
|
|
|
|
symtab_off[i] = symtab_off[i - 1] + files[i - 1]->local_symtab_size;
|
|
|
|
strtab_off[i] = strtab_off[i - 1] + files[i - 1]->local_strtab_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
out::symtab->shdr.sh_info = strtab_off.back() / sizeof(ELF64LE::Sym);
|
|
|
|
|
|
|
|
tbb::parallel_for((size_t)0, files.size(),
|
|
|
|
[&](size_t i) {
|
|
|
|
files[i]->write_local_symtab(buf, symtab_off[i], strtab_off[i]);
|
|
|
|
});
|
|
|
|
|
2020-10-27 16:09:16 +03:00
|
|
|
symtab_off[0] = symtab_off.back() + files.back()->local_symtab_size;
|
|
|
|
strtab_off[0] = strtab_off.back() + files.back()->local_strtab_size;
|
2020-10-27 16:08:49 +03:00
|
|
|
|
|
|
|
for (int i = 1; i < files.size(); i++) {
|
|
|
|
symtab_off[i] = symtab_off[i - 1] + files[i - 1]->global_symtab_size;
|
|
|
|
strtab_off[i] = strtab_off[i - 1] + files[i - 1]->global_strtab_size;
|
|
|
|
}
|
2020-10-27 15:51:09 +03:00
|
|
|
|
2020-10-27 16:12:06 +03:00
|
|
|
assert(symtab_off.back() + files.back()->global_symtab_size == out::symtab->size);
|
|
|
|
assert(strtab_off.back() + files.back()->global_strtab_size == out::strtab->size);
|
2020-10-27 15:38:52 +03:00
|
|
|
|
2020-10-27 16:08:49 +03:00
|
|
|
tbb::parallel_for((size_t)0, files.size(),
|
|
|
|
[&](size_t i) {
|
|
|
|
files[i]->write_global_symtab(buf, symtab_off[i], strtab_off[i]);
|
|
|
|
});
|
2020-10-27 16:14:10 +03:00
|
|
|
});
|
2020-10-27 15:38:52 +03:00
|
|
|
|
2020-10-20 03:20:52 +03:00
|
|
|
// Copy input sections to the output file
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
|
|
|
MyTimer t("copy");
|
|
|
|
for_each(output_chunks, [&](OutputChunk *chunk) { chunk->copy_to(buf); });
|
|
|
|
}
|
2020-10-20 04:13:46 +03:00
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
|
|
|
MyTimer t("reloc");
|
|
|
|
for_each(output_chunks, [&](OutputChunk *chunk) { chunk->relocate(buf); });
|
|
|
|
}
|
2020-10-20 04:13:46 +03:00
|
|
|
|
2020-10-27 16:14:10 +03:00
|
|
|
{
|
|
|
|
MyTimer t("symtab_wait");
|
|
|
|
tg_symtab.wait();
|
|
|
|
}
|
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
|
|
|
MyTimer t("commit");
|
|
|
|
if (auto e = output_buffer->commit())
|
|
|
|
error("failed to write to the output file: " + toString(std::move(e)));
|
|
|
|
}
|
2020-10-14 12:41:09 +03:00
|
|
|
|
2020-10-22 09:20:48 +03:00
|
|
|
int num_input_chunks = 0;
|
|
|
|
for (ObjectFile *file : files)
|
|
|
|
num_input_chunks += file->sections.size();
|
2020-10-22 10:11:28 +03:00
|
|
|
|
2020-10-25 03:38:53 +03:00
|
|
|
{
|
|
|
|
MyTimer t("wait");
|
|
|
|
tg.wait();
|
|
|
|
}
|
2020-10-24 12:58:21 +03:00
|
|
|
|
2020-10-22 09:20:48 +03:00
|
|
|
llvm::outs() << " input_chunks=" << num_input_chunks << "\n"
|
|
|
|
<< "output_chunks=" << output_chunks.size() << "\n"
|
|
|
|
<< " files=" << files.size() << "\n"
|
2020-10-20 08:27:00 +03:00
|
|
|
<< " filesize=" << filesize << "\n"
|
2020-10-23 07:17:21 +03:00
|
|
|
<< " num_all_syms=" << num_all_syms << "\n"
|
2020-10-20 08:27:00 +03:00
|
|
|
<< " num_defined=" << num_defined << "\n"
|
|
|
|
<< "num_undefined=" << num_undefined << "\n"
|
2020-10-22 19:14:11 +03:00
|
|
|
<< " num_comdats=" << num_comdats << "\n"
|
2020-10-23 07:17:21 +03:00
|
|
|
<< "num_regular_sections=" << num_regular_sections << "\n"
|
2020-10-22 18:52:36 +03:00
|
|
|
<< " num_relocs=" << num_relocs << "\n"
|
2020-10-23 07:23:12 +03:00
|
|
|
<< "num_relocs_alloc=" << num_relocs_alloc << "\n"
|
2020-10-22 18:52:36 +03:00
|
|
|
<< " num_str=" << num_string_pieces << "\n";
|
2020-10-20 08:27:00 +03:00
|
|
|
|
2020-10-18 10:21:17 +03:00
|
|
|
llvm::TimerGroup::printAll(llvm::outs());
|
|
|
|
llvm::outs().flush();
|
|
|
|
_exit(0);
|
2020-09-29 09:05:29 +03:00
|
|
|
}
|