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

179 lines
4.6 KiB
C++
Raw Normal View History

2020-10-08 11:47:49 +03:00
#include "chibild.h"
2020-10-02 07:28:26 +03:00
2020-09-29 09:05:29 +03:00
#include <iostream>
2020-10-10 06:47:12 +03:00
using llvm::file_magic;
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-12 07:30:34 +03:00
SymbolTable symbol_table;
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-11 12:18:19 +03:00
static std::vector<ObjectFile *> read_file(StringRef path) {
2020-10-10 06:47:12 +03:00
MemoryBufferRef mb = readFile(path);
2020-10-11 12:18:19 +03:00
std::vector<ObjectFile *> vec;
2020-10-10 06:47:12 +03:00
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-14 12:42:54 +03:00
vec.push_back(new ObjectFile(member, path));
2020-10-10 06:47:12 +03:00
break;
case file_magic::elf_relocatable:
2020-10-14 12:42:54 +03:00
vec.push_back(new ObjectFile(mb, ""));
2020-10-10 06:47:12 +03:00
break;
default:
error(path + ": unknown file type");
}
2020-10-11 12:18:19 +03:00
return vec;
2020-10-10 06:47:12 +03:00
}
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-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-11 12:18:19 +03:00
for (ObjectFile *file : read_file(arg->getValue()))
files.push_back(file);
2020-10-17 15:30:16 +03:00
open_timer.stopTimer();
2020-10-11 12:18:19 +03:00
2020-10-18 13:17:44 +03:00
// Resolve symbols
parse_timer.startTimer();
tbb::parallel_for_each(
files.begin(), files.end(),
[&](ObjectFile *file) { file->parse(); });
parse_timer.stopTimer();
2020-10-18 13:05:28 +03:00
// Set priorities to files
int priority = 1;
for (ObjectFile *file : files)
if (!file->is_in_archive())
file->priority = priority++;
for (ObjectFile *file : files)
if (file->is_in_archive())
file->priority = priority++;
2020-10-17 15:30:16 +03:00
add_symbols_timer.startTimer();
2020-10-13 14:35:35 +03:00
tbb::parallel_for_each(
files.begin(), files.end(),
2020-10-18 13:00:39 +03:00
[&](ObjectFile *file) { file->register_defined_symbols(); });
2020-10-17 15:30:16 +03:00
add_symbols_timer.stopTimer();
2020-10-10 06:47:12 +03:00
2020-10-13 14:35:35 +03:00
// Create output sections
std::vector<OutputSection *> output_sections;
for (ObjectFile *file : files) {
for (InputSection *isec : file->sections) {
auto *osec = new OutputSection(isec->name);
osec->sections.push_back(isec);
output_sections.push_back(osec);
}
}
2020-10-14 12:41:09 +03:00
int64_t filesize = 0;
for (OutputSection *sec : output_sections) {
2020-10-15 13:27:35 +03:00
sec->set_offset(filesize);
filesize += sec->get_size();
2020-10-14 12:41:09 +03:00
}
2020-10-17 07:42:55 +03:00
out::ehdr = new OutputEhdr;
out::shdr = new OutputShdr;
out::phdr = new OutputPhdr;
2020-10-18 13:00:39 +03:00
llvm::outs() << "num_defined=" << num_defined << "\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
}