1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 18:40:59 +03:00
mold/main.cc

264 lines
7.9 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 10:35:17 +03:00
static OutputSection *create_interp_section() {
2020-10-22 12:29:35 +03:00
static char path[] = "/lib64/ld-linux-x86-64.so.2";
2020-10-22 10:35:17 +03:00
auto *isec = new GenericSection(".interp",
2020-10-22 12:29:35 +03:00
makeArrayRef((uint8_t *)path, sizeof(path)),
SHF_ALLOC, SHT_PROGBITS);
auto *osec = new OutputSection(".interp", PF_R, PT_INTERP);
2020-10-22 10:35:17 +03:00
osec->chunks.push_back(isec);
return osec;
2020-10-20 10:00:26 +03:00
}
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
}
static std::vector<ELF64LE::Shdr>
create_shdrs(ArrayRef<OutputChunk *> output_chunks) {
std::vector<ELF64LE::Shdr> vec;
2020-10-21 03:50:21 +03:00
vec.push_back({});
2020-10-20 13:09:18 +03:00
for (OutputChunk *chunk : output_chunks)
2020-10-21 03:50:21 +03:00
if (const ELF64LE::Shdr *hdr = chunk->get_shdr())
2020-10-20 13:09:18 +03:00
vec.push_back(*hdr);
return vec;
}
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-18 10:21:17 +03:00
llvm::Timer open_timer("opne", "open");
llvm::Timer parse_timer("parse", "parse");
llvm::Timer add_symbols_timer("add_symbols", "add_symbols");
2020-10-19 15:50:33 +03:00
llvm::Timer comdat_timer("comdat", "comdat");
2020-10-22 10:56:15 +03:00
llvm::Timer bin_sections_timer("bin_sections", "bin_sections");
2020-10-19 17:37:29 +03:00
llvm::Timer file_offset_timer("file_offset", "file_offset");
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-17 15:30:16 +03:00
open_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-17 15:30:16 +03:00
open_timer.stopTimer();
2020-10-11 12:18:19 +03:00
2020-10-18 15:03:51 +03:00
// Parse input files
2020-10-18 13:17:44 +03:00
parse_timer.startTimer();
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-20 08:37:17 +03:00
// Create an ELF header, a section header and a program header.
std::vector<OutputChunk *> output_chunks;
out::ehdr = new OutputEhdr;
out::phdr = new OutputPhdr;
output_chunks.push_back(out::ehdr);
output_chunks.push_back(out::phdr);
2020-10-20 10:00:26 +03:00
// Add .interp section
2020-10-22 10:35:17 +03:00
output_chunks.push_back(create_interp_section());
2020-10-22 10:11:28 +03:00
2020-10-22 10:35:17 +03:00
// Add other output sections
for (OutputSection *osec : OutputSection::all_instances)
if (!osec->chunks.empty())
output_chunks.push_back(osec);
2020-10-19 12:13:55 +03:00
2020-10-20 13:09:18 +03:00
// Create program header contents.
out::phdr->hdr = create_phdrs();
// A section header is added to the end of the file by convention.
out::shdr = new OutputShdr;
out::shdr->hdr = create_shdrs(output_chunks);
output_chunks.push_back(out::shdr);
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-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
}