1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-20 17:39:56 +03:00
This commit is contained in:
Rui Ueyama 2022-05-16 19:26:11 +08:00
parent f392b4aeb3
commit 0326d66b24
3 changed files with 48 additions and 43 deletions

View File

@ -1,6 +1,7 @@
#include "lto.h"
#include "mold.h"
#include <algorithm>
#include <dlfcn.h>
namespace mold::macho {
@ -71,19 +72,8 @@ void load_lto_plugin(Context<E> &ctx) {
std:call_once(ctx.lto_plugin_loaded, [&]() { do_load_plugin(ctx); });
}
template <typename E>
static bool has_lto_obj(Context<E> &ctx) {
for (ObjectFile<E> *file : ctx.objs)
if (file->lto_module)
return true;
return false;
}
template <typename E>
void do_lto(Context<E> &ctx) {
if (!has_lto_obj(ctx))
return;
LTOCodeGen *cg = ctx.lto.codegen_create();
for (ObjectFile<E> *file : ctx.objs)
if (file->lto_module)
@ -107,9 +97,6 @@ void do_lto(Context<E> &ctx) {
if (sym->file == file && sym->is_extern)
ctx.lto.codegen_add_must_preserve_symbol(cg, sym->name.data());
size_t size;
u8 *data = (u8 *)ctx.lto.codegen_compile(cg, &size);
for (ObjectFile<E> *file : ctx.objs) {
if (file->lto_module) {
file->clear_symbols();
@ -117,6 +104,9 @@ void do_lto(Context<E> &ctx) {
}
}
size_t size;
u8 *data = (u8 *)ctx.lto.codegen_compile(cg, &size);
MappedFile<Context<E>> *mf = new MappedFile<Context<E>>;
mf->name = "<LTO>";
mf->data = data;
@ -125,8 +115,9 @@ void do_lto(Context<E> &ctx) {
ObjectFile<E> *obj = ObjectFile<E>::create(ctx, mf, "");
obj->parse(ctx);
obj->resolve_symbols(ctx);
obj->is_alive = true;
obj->priority = 100; // regular file starts with priority 10,000
obj->resolve_symbols(ctx);
ctx.objs.push_back(obj);
}

View File

@ -20,6 +20,41 @@ split_string(std::string_view str, char sep) {
return {str.substr(0, pos), str.substr(pos + 1)};
}
template <typename E>
static bool has_lto_obj(Context<E> &ctx) {
for (ObjectFile<E> *file : ctx.objs)
if (file->lto_module)
return true;
return false;
}
template <typename E>
static void resolve_symbols(Context<E> &ctx) {
std::vector<InputFile<E> *> files;
append(files, ctx.objs);
append(files, ctx.dylibs);
for (InputFile<E> *file : files)
file->resolve_symbols(ctx);
std::vector<ObjectFile<E> *> live_objs;
for (ObjectFile<E> *file : ctx.objs)
if (file->is_alive)
live_objs.push_back(file);
for (i64 i = 0; i < live_objs.size(); i++) {
live_objs[i]->mark_live_objects(ctx, [&](ObjectFile<E> *file) {
live_objs.push_back(file);
});
}
for (InputFile<E> *file : files)
file->resolve_symbols(ctx);
if (has_lto_obj(ctx))
do_lto(ctx);
}
template <typename E>
static void create_internal_file(Context<E> &ctx) {
ObjectFile<E> *obj = new ObjectFile<E>;
@ -445,11 +480,10 @@ static int do_main(int argc, char **argv) {
read_input_files(ctx, file_args);
i64 priority = 1;
for (ObjectFile<E> *file : ctx.objs)
file->priority = priority++;
file->priority = ctx.file_priority++;
for (DylibFile<E> *dylib : ctx.dylibs)
dylib->priority = priority++;
dylib->priority = ctx.file_priority++;
for (i64 i = 0; i < ctx.dylibs.size(); i++)
ctx.dylibs[i]->dylib_idx = i + 1;
@ -465,29 +499,7 @@ static int do_main(int argc, char **argv) {
if (!file->archive_name.empty() && file->is_objc_object(ctx))
file->is_alive = true;
// Resolve symbols
for (ObjectFile<E> *file : ctx.objs)
file->resolve_symbols(ctx);
for (DylibFile<E> *dylib : ctx.dylibs)
dylib->resolve_symbols(ctx);
std::vector<ObjectFile<E> *> live_objs;
for (ObjectFile<E> *file : ctx.objs)
if (file->is_alive)
live_objs.push_back(file);
for (i64 i = 0; i < live_objs.size(); i++) {
live_objs[i]->mark_live_objects(ctx, [&](ObjectFile<E> *file) {
live_objs.push_back(file);
});
}
for (ObjectFile<E> *file : ctx.objs)
file->resolve_symbols(ctx);
for (DylibFile<E> *dylib : ctx.dylibs)
dylib->resolve_symbols(ctx);
do_lto(ctx);
resolve_symbols(ctx);
if (ctx.output_type == MH_EXECUTE && !get_symbol(ctx, ctx.arg.entry)->file)
Error(ctx) << "undefined entry point symbol: " << ctx.arg.entry;

View File

@ -84,6 +84,7 @@ class InputFile {
public:
virtual ~InputFile() = default;
void clear_symbols();
virtual void resolve_symbols(Context<E> &ctx) = 0;
MappedFile<Context<E>> *mf = nullptr;
std::vector<Symbol<E> *> syms;
@ -107,7 +108,7 @@ public:
void parse(Context<E> &ctx);
Subsection<E> *find_subsection(Context<E> &ctx, u32 addr);
void parse_compact_unwind(Context<E> &ctx, MachSection &hdr);
void resolve_symbols(Context<E> &ctx);
void resolve_symbols(Context<E> &ctx) override;
bool is_objc_object(Context<E> &ctx);
void mark_live_objects(Context<E> &ctx,
std::function<void(ObjectFile<E> *)> feeder);
@ -149,7 +150,7 @@ public:
static DylibFile *create(Context<E> &ctx, MappedFile<Context<E>> *mf);
void parse(Context<E> &ctx);
void resolve_symbols(Context<E> &ctx);
void resolve_symbols(Context<E> &ctx) override;
std::string_view install_name;
i64 dylib_idx = 0;
@ -848,6 +849,7 @@ struct Context {
std::vector<std::string_view> cmdline_args;
u32 output_type = MH_EXECUTE;
i64 file_priority = 10000;
bool all_load = false;
bool needed_l = false;
bool hidden_l = false;