1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-11 16:58:12 +03:00
mold/main.cc

323 lines
9.3 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-09-29 09:05:29 +03:00
#include <iostream>
2020-10-21 05:28:43 +03:00
using namespace llvm::ELF;
2020-10-20 03:20:52 +03:00
using llvm::FileOutputBuffer;
2020-10-10 06:47:12 +03:00
using llvm::file_magic;
2020-10-21 05:28:43 +03:00
using llvm::makeArrayRef;
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-22 17:19:48 +03:00
thread_local int foo;
thread_local int bar = 5;
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) {
bool alloc = x->hdr.sh_flags & SHF_ALLOC;
bool writable = x->hdr.sh_flags & SHF_WRITE;
bool exec = x->hdr.sh_flags & SHF_EXECINSTR;
bool tls = x->hdr.sh_flags & SHF_TLS;
bool nobits = x->hdr.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
}
static std::vector<OutputSection *> get_output_sections() {
std::vector<OutputSection *> vec;
for (OutputSection *osec : OutputSection::all_instances)
if (!osec->chunks.empty())
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.
if (a->hdr.sh_flags != b->hdr.sh_flags)
return a->hdr.sh_flags < b->hdr.sh_flags;
if (a->hdr.sh_type != b->hdr.sh_type)
return a->hdr.sh_type < b->hdr.sh_type;
return a->name < b->name;
});
2020-10-22 12:54:51 +03:00
return vec;
}
2020-10-20 13:09:18 +03:00
static std::vector<ELF64LE::Phdr> create_phdrs() {
2020-10-22 10:11:28 +03:00
return {};
2020-10-20 13:09:18 +03:00
}
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-20 13:09:18 +03:00
for (OutputChunk *chunk : output_chunks)
2020-10-22 13:42:52 +03:00
if (!chunk->name.empty())
2020-10-22 14:01:10 +03:00
vec.push_back(&chunk->hdr);
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;
chunk->hdr.sh_offset = chunk->get_offset();
chunk->hdr.sh_size = chunk->get_size();
chunk->index = i++;
}
}
2020-09-29 09:05:29 +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-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-18 10:21:17 +03:00
llvm::Timer parse_timer("parse", "parse");
2020-10-22 18:02:33 +03:00
llvm::Timer add_symbols_timer("add_symbols", "add_symbols", before_copy);
llvm::Timer comdat_timer("comdat", "comdat", before_copy);
llvm::Timer bin_sections_timer("bin_sections", "bin_sections", before_copy);
llvm::Timer file_offset_timer("file_offset", "file_offset", before_copy);
2020-10-20 03:20:52 +03:00
llvm::Timer copy_timer("copy", "copy");
2020-10-20 04:13:46 +03:00
llvm::Timer reloc_timer("reloc", "reloc");
llvm::Timer commit_timer("commit", "commit");
2020-10-17 15:30:16 +03:00
2020-10-13 14:35:35 +03:00
// Open input files
2020-10-22 15:09:23 +03:00
parse_timer.startTimer();
2020-10-10 06:47:12 +03:00
for (auto *arg : args)
if (arg->getOption().getID() == OPT_INPUT)
2020-10-22 11:26:23 +03:00
read_file(files, arg->getValue());
2020-10-11 12:18:19 +03:00
2020-10-18 15:03:51 +03:00
// Parse input files
2020-10-19 08:14:17 +03:00
for_each(files, [](ObjectFile *file) { file->parse(); });
2020-10-18 13:17:44 +03:00
parse_timer.stopTimer();
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-17 15:30:16 +03:00
add_symbols_timer.startTimer();
2020-10-19 08:14:17 +03:00
for_each(files, [](ObjectFile *file) { file->register_defined_symbols(); });
for_each(files, [](ObjectFile *file) { file->register_undefined_symbols(); });
2020-10-17 15:30:16 +03:00
add_symbols_timer.stopTimer();
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.
comdat_timer.startTimer();
for (ObjectFile *file : files)
file->eliminate_duplicate_comdat_groups();
comdat_timer.stopTimer();
2020-10-10 06:47:12 +03:00
2020-10-22 10:35:17 +03:00
// Bin input sections into output sections
2020-10-22 10:56:15 +03:00
bin_sections_timer.startTimer();
2020-10-22 10:57:50 +03:00
for (ObjectFile *file : files)
for (InputSection *isec : file->sections)
if (isec)
isec->output_section->chunks.push_back(isec);
2020-10-22 10:56:15 +03:00
bin_sections_timer.stopTimer();
2020-10-22 10:35:17 +03:00
2020-10-22 13:41:27 +03:00
// Create linker-synthesized sections.
2020-10-20 08:37:17 +03:00
out::ehdr = new OutputEhdr;
out::phdr = new OutputPhdr;
2020-10-22 15:09:45 +03:00
out::shdr = new OutputShdr;
2020-10-22 13:41:27 +03:00
out::shstrtab = new StringTableSection(".shstrtab");
// 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-22 13:35:16 +03:00
output_chunks.push_back(new InterpSection);
2020-10-22 10:11:28 +03:00
2020-10-22 13:41:27 +03:00
// Add other output sections.
2020-10-22 12:54:51 +03:00
for (OutputSection *osec : get_output_sections())
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-22 14:53:20 +03:00
for (OutputChunk *chunk : output_chunks)
if (!chunk->name.empty())
chunk->hdr.sh_name = out::shstrtab->add_string(chunk->name);
2020-10-20 13:09:18 +03:00
2020-10-22 15:09:45 +03:00
// Add a section header.
2020-10-20 13:09:18 +03:00
out::shdr->hdr = create_shdrs(output_chunks);
output_chunks.push_back(out::shdr);
2020-10-22 14:53:20 +03:00
// Create program header contents.
out::phdr->hdr = create_phdrs();
2020-10-20 03:20:52 +03:00
// Assign offsets to input sections
2020-10-19 17:37:29 +03:00
file_offset_timer.startTimer();
2020-10-20 03:20:52 +03:00
uint64_t filesize = 0;
2020-10-20 08:37:17 +03:00
for (OutputChunk *chunk : output_chunks) {
chunk->set_offset(filesize);
filesize += chunk->get_size();
2020-10-19 17:57:08 +03:00
}
2020-10-19 17:37:29 +03:00
file_offset_timer.stopTimer();
2020-10-22 14:01:10 +03:00
// Fill section header.
fill_shdrs(output_chunks);
2020-10-20 03:20:52 +03:00
// Create an output file
Expected<std::unique_ptr<FileOutputBuffer>> buf_or_err =
FileOutputBuffer::create(config.output, filesize, 0);
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();
// Copy input sections to the output file
copy_timer.startTimer();
2020-10-20 08:37:17 +03:00
for_each(output_chunks, [&](OutputChunk *chunk) { chunk->copy_to(buf); });
2020-10-20 04:13:46 +03:00
copy_timer.stopTimer();
reloc_timer.startTimer();
2020-10-20 08:37:17 +03:00
for_each(output_chunks, [&](OutputChunk *chunk) { chunk->relocate(buf); });
2020-10-20 04:13:46 +03:00
reloc_timer.stopTimer();
commit_timer.startTimer();
2020-10-20 03:20:52 +03:00
if (auto e = output_buffer->commit())
error("failed to write to the output file: " + toString(std::move(e)));
2020-10-20 04:13:46 +03:00
commit_timer.stopTimer();
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-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"
<< " num_defined=" << num_defined << "\n"
<< "num_undefined=" << num_undefined << "\n"
<< " num_relocs=" << num_relocs << "\n";
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
}