mirror of
https://github.com/rui314/mold.git
synced 2024-10-05 17:17:40 +03:00
Refactor
This commit is contained in:
parent
08fada7de0
commit
7c8ed4d2e1
@ -117,7 +117,7 @@ read_thin_archive_members(Context &ctx, MappedFile *mf) {
|
||||
|
||||
std::string path = name.starts_with('/') ?
|
||||
name : (filepath(mf->name).parent_path() / name).string();
|
||||
vec.push_back(MappedFile::must_open(ctx, path));
|
||||
vec.push_back(must_open_file(ctx, path));
|
||||
vec.back()->thin_parent = mf;
|
||||
data = body;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ read_response_file(Context &ctx, std::string_view path, i64 depth) {
|
||||
Fatal(ctx) << path << ": response file nesting too deep";
|
||||
|
||||
std::vector<std::string_view> vec;
|
||||
MappedFile<Context> *mf = MappedFile<Context>::must_open(ctx, std::string(path));
|
||||
MappedFile *mf = must_open_file(ctx, std::string(path));
|
||||
std::string_view data((char *)mf->data, mf->size);
|
||||
|
||||
while (!data.empty()) {
|
||||
|
@ -954,15 +954,12 @@ private:
|
||||
|
||||
// MappedFile represents an mmap'ed input file.
|
||||
// mold uses mmap-IO only.
|
||||
template <typename Context>
|
||||
class MappedFile {
|
||||
public:
|
||||
static MappedFile *open(Context &ctx, std::string path);
|
||||
static MappedFile *must_open(Context &ctx, std::string path);
|
||||
|
||||
~MappedFile() { unmap(); }
|
||||
void unmap();
|
||||
|
||||
template <typename Context>
|
||||
MappedFile *slice(Context &ctx, std::string name, u64 start, u64 size);
|
||||
|
||||
std::string_view get_contents() {
|
||||
@ -997,13 +994,14 @@ public:
|
||||
MappedFile *parent = nullptr;
|
||||
MappedFile *thin_parent = nullptr;
|
||||
int fd = -1;
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE file_handle = INVALID_HANDLE_VALUE;
|
||||
#endif
|
||||
};
|
||||
|
||||
template <typename Context>
|
||||
MappedFile<Context> *MappedFile<Context>::open(Context &ctx, std::string path) {
|
||||
MappedFile *open_file(Context &ctx, std::string path) {
|
||||
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
||||
path = ctx.arg.chroot + "/" + path_clean(path);
|
||||
|
||||
@ -1082,16 +1080,15 @@ MappedFile<Context> *MappedFile<Context>::open(Context &ctx, std::string path) {
|
||||
}
|
||||
|
||||
template <typename Context>
|
||||
MappedFile<Context> *
|
||||
MappedFile<Context>::must_open(Context &ctx, std::string path) {
|
||||
if (MappedFile *mf = MappedFile::open(ctx, path))
|
||||
MappedFile *must_open_file(Context &ctx, std::string path) {
|
||||
if (MappedFile *mf = open_file(ctx, path))
|
||||
return mf;
|
||||
Fatal(ctx) << "cannot open " << path << ": " << errno_string();
|
||||
}
|
||||
|
||||
template <typename Context>
|
||||
MappedFile<Context> *
|
||||
MappedFile<Context>::slice(Context &ctx, std::string name, u64 start, u64 size) {
|
||||
MappedFile *
|
||||
MappedFile::slice(Context &ctx, std::string name, u64 start, u64 size) {
|
||||
MappedFile *mf = new MappedFile;
|
||||
mf->name = name;
|
||||
mf->data = data + start;
|
||||
@ -1102,8 +1099,7 @@ MappedFile<Context>::slice(Context &ctx, std::string name, u64 start, u64 size)
|
||||
return mf;
|
||||
}
|
||||
|
||||
template <typename Context>
|
||||
void MappedFile<Context>::unmap() {
|
||||
inline void MappedFile::unmap() {
|
||||
if (size == 0 || parent || !data)
|
||||
return;
|
||||
|
||||
|
@ -297,8 +297,7 @@ split_by_comma_or_colon(std::string_view str) {
|
||||
|
||||
template <typename E>
|
||||
static void read_retain_symbols_file(Context<E> &ctx, std::string_view path) {
|
||||
MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::must_open(ctx, std::string(path));
|
||||
MappedFile *mf = must_open_file(ctx, std::string(path));
|
||||
std::string_view data((char *)mf->data, mf->size);
|
||||
|
||||
ctx.arg.retain_symbols_file.reset(new std::unordered_set<std::string_view>);
|
||||
|
@ -53,7 +53,7 @@ std::string_view demangle(const Symbol<E> &sym) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
InputFile<E>::InputFile(Context<E> &ctx, MappedFile<Context<E>> *mf)
|
||||
InputFile<E>::InputFile(Context<E> &ctx, MappedFile *mf)
|
||||
: mf(mf), filename(mf->name) {
|
||||
if (mf->size < sizeof(ElfEhdr<E>))
|
||||
Fatal(ctx) << *this << ": file too small";
|
||||
@ -124,7 +124,7 @@ std::string_view InputFile<E>::get_source_name() const {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
ObjectFile<E>::ObjectFile(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
ObjectFile<E>::ObjectFile(Context<E> &ctx, MappedFile *mf,
|
||||
std::string archive_name, bool is_in_lib)
|
||||
: InputFile<E>(ctx, mf), archive_name(archive_name), is_in_lib(is_in_lib) {
|
||||
this->is_alive = !is_in_lib;
|
||||
@ -132,7 +132,7 @@ ObjectFile<E>::ObjectFile(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
|
||||
template <typename E>
|
||||
ObjectFile<E> *
|
||||
ObjectFile<E>::create(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
ObjectFile<E>::create(Context<E> &ctx, MappedFile *mf,
|
||||
std::string archive_name, bool is_in_lib) {
|
||||
ObjectFile<E> *obj = new ObjectFile<E>(ctx, mf, archive_name, is_in_lib);
|
||||
ctx.obj_pool.emplace_back(obj);
|
||||
@ -1313,14 +1313,14 @@ std::ostream &operator<<(std::ostream &out, const InputFile<E> &file) {
|
||||
|
||||
template <typename E>
|
||||
SharedFile<E> *
|
||||
SharedFile<E>::create(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
SharedFile<E>::create(Context<E> &ctx, MappedFile *mf) {
|
||||
SharedFile<E> *obj = new SharedFile(ctx, mf);
|
||||
ctx.dso_pool.emplace_back(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
SharedFile<E>::SharedFile(Context<E> &ctx, MappedFile<Context<E>> *mf)
|
||||
SharedFile<E>::SharedFile(Context<E> &ctx, MappedFile *mf)
|
||||
: InputFile<E>(ctx, mf) {
|
||||
this->is_alive = !ctx.as_needed;
|
||||
}
|
||||
|
@ -150,13 +150,13 @@ static bool is_in_sysroot(Context<E> &ctx, std::string path) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static MappedFile<Context<E>> *resolve_path(Context<E> &ctx, std::string_view tok) {
|
||||
static MappedFile *resolve_path(Context<E> &ctx, std::string_view tok) {
|
||||
std::string str(unquote(tok));
|
||||
|
||||
// GNU ld prepends the sysroot if a pathname starts with '/' and the
|
||||
// script being processed is in the sysroot. We do the same.
|
||||
if (str.starts_with('/') && is_in_sysroot(ctx, ctx.script_file->name))
|
||||
return MappedFile<Context<E>>::must_open(ctx, ctx.arg.sysroot + str);
|
||||
return must_open_file(ctx, ctx.arg.sysroot + str);
|
||||
|
||||
if (str.starts_with('=')) {
|
||||
std::string path;
|
||||
@ -164,23 +164,23 @@ static MappedFile<Context<E>> *resolve_path(Context<E> &ctx, std::string_view to
|
||||
path = str.substr(1);
|
||||
else
|
||||
path = ctx.arg.sysroot + str.substr(1);
|
||||
return MappedFile<Context<E>>::must_open(ctx, path);
|
||||
return must_open_file(ctx, path);
|
||||
}
|
||||
|
||||
if (str.starts_with("-l"))
|
||||
return find_library(ctx, str.substr(2));
|
||||
|
||||
if (!str.starts_with('/'))
|
||||
if (MappedFile<Context<E>> *mf =
|
||||
if (MappedFile *mf =
|
||||
open_library(ctx, path_clean(ctx.script_file->name + "/../" + str)))
|
||||
return mf;
|
||||
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, str))
|
||||
if (MappedFile *mf = open_library(ctx, str))
|
||||
return mf;
|
||||
|
||||
for (std::string_view dir : ctx.arg.library_paths) {
|
||||
std::string path = std::string(dir) + "/" + str;
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, path))
|
||||
if (MappedFile *mf = open_library(ctx, path))
|
||||
return mf;
|
||||
}
|
||||
|
||||
@ -201,7 +201,7 @@ read_group(Context<E> &ctx, std::span<std::string_view> tok) {
|
||||
continue;
|
||||
}
|
||||
|
||||
MappedFile<Context<E>> *mf = resolve_path(ctx, tok[0]);
|
||||
MappedFile *mf = resolve_path(ctx, tok[0]);
|
||||
read_file(ctx, mf);
|
||||
tok = tok.subspan(1);
|
||||
}
|
||||
@ -212,7 +212,7 @@ read_group(Context<E> &ctx, std::span<std::string_view> tok) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void parse_linker_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
void parse_linker_script(Context<E> &ctx, MappedFile *mf) {
|
||||
ctx.script_file = mf;
|
||||
|
||||
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
|
||||
@ -242,7 +242,7 @@ void parse_linker_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
|
||||
template <typename E>
|
||||
std::string_view
|
||||
get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
get_script_output_type(Context<E> &ctx, MappedFile *mf) {
|
||||
ctx.script_file = mf;
|
||||
|
||||
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
|
||||
@ -257,8 +257,8 @@ get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
|
||||
if (tok.size() >= 3 && (tok[0] == "INPUT" || tok[0] == "GROUP") &&
|
||||
tok[1] == "(")
|
||||
if (MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::open(ctx, std::string(unquote(tok[2]))))
|
||||
if (MappedFile *mf =
|
||||
open_file(ctx, std::string(unquote(tok[2]))))
|
||||
return get_machine_type(ctx, mf);
|
||||
|
||||
return "";
|
||||
@ -358,7 +358,7 @@ void read_version_script(Context<E> &ctx, std::span<std::string_view> &tok) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void parse_version_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
void parse_version_script(Context<E> &ctx, MappedFile *mf) {
|
||||
ctx.script_file = mf;
|
||||
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
|
||||
std::span<std::string_view> tok = vec;
|
||||
@ -400,7 +400,7 @@ template <typename E>
|
||||
std::vector<DynamicPattern>
|
||||
parse_dynamic_list(Context<E> &ctx, std::string_view path) {
|
||||
std::string_view contents =
|
||||
MappedFile<Context<E>>::must_open(ctx, std::string(path))->get_contents();
|
||||
must_open_file(ctx, std::string(path))->get_contents();
|
||||
std::vector<std::string_view> vec = tokenize(ctx, contents);
|
||||
std::span<std::string_view> tok = vec;
|
||||
std::vector<DynamicPattern> result;
|
||||
@ -421,9 +421,9 @@ parse_dynamic_list(Context<E> &ctx, std::string_view path) {
|
||||
|
||||
using E = MOLD_TARGET;
|
||||
|
||||
template void parse_linker_script(Context<E> &, MappedFile<Context<E>> *);
|
||||
template std::string_view get_script_output_type(Context<E> &, MappedFile<Context<E>> *);
|
||||
template void parse_version_script(Context<E> &, MappedFile<Context<E>> *);
|
||||
template void parse_linker_script(Context<E> &, MappedFile *);
|
||||
template std::string_view get_script_output_type(Context<E> &, MappedFile *);
|
||||
template void parse_version_script(Context<E> &, MappedFile *);
|
||||
template std::vector<DynamicPattern> parse_dynamic_list(Context<E> &, std::string_view);
|
||||
|
||||
|
||||
|
@ -176,7 +176,7 @@ static PluginStatus add_input_file(const char *path) {
|
||||
Context<E> &ctx = *gctx<E>;
|
||||
static i64 file_priority = 100;
|
||||
|
||||
MappedFile<Context<E>> *mf = MappedFile<Context<E>>::must_open(ctx, path);
|
||||
MappedFile *mf = must_open_file(ctx, path);
|
||||
|
||||
ObjectFile<E> *file = ObjectFile<E>::create(ctx, mf, "", false);
|
||||
ctx.obj_pool.emplace_back(file);
|
||||
@ -582,9 +582,9 @@ static bool supports_v3_api(Context<E> &ctx) {
|
||||
|
||||
template <typename E>
|
||||
static PluginInputFile
|
||||
create_plugin_input_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
create_plugin_input_file(Context<E> &ctx, MappedFile *mf) {
|
||||
PluginInputFile file;
|
||||
MappedFile<Context<E>> *mf2 = mf->parent ? mf->parent : mf;
|
||||
MappedFile *mf2 = mf->parent ? mf->parent : mf;
|
||||
|
||||
file.name = save_string(ctx, mf2->name).data();
|
||||
file.offset = mf->get_offset();
|
||||
@ -600,7 +600,7 @@ create_plugin_input_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile *mf) {
|
||||
load_lto_plugin(ctx);
|
||||
|
||||
// V0 API's claim_file is not thread-safe.
|
||||
@ -644,7 +644,7 @@ ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
// LLVM needs it and takes the ownership of fd. To prevent "too many
|
||||
// open files" issue, we close fd only for GCC. This is ugly, though.
|
||||
if (!is_llvm(ctx)) {
|
||||
MappedFile<Context<E>> *mf2 = mf->parent ? mf->parent : mf;
|
||||
MappedFile *mf2 = mf->parent ? mf->parent : mf;
|
||||
close(mf2->fd);
|
||||
mf2->fd = -1;
|
||||
}
|
||||
@ -746,7 +746,7 @@ void lto_cleanup(Context<E> &ctx) {
|
||||
|
||||
using E = MOLD_TARGET;
|
||||
|
||||
template ObjectFile<E> *read_lto_object(Context<E> &, MappedFile<Context<E>> *);
|
||||
template ObjectFile<E> *read_lto_object(Context<E> &, MappedFile *);
|
||||
template std::vector<ObjectFile<E> *> do_lto(Context<E> &);
|
||||
template void lto_cleanup(Context<E> &);
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile *mf) {
|
||||
Fatal(ctx) << "LTO is not supported on Windows";
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ void lto_cleanup(Context<E> &ctx) {}
|
||||
|
||||
using E = MOLD_TARGET;
|
||||
|
||||
template ObjectFile<E> *read_lto_object(Context<E> &, MappedFile<Context<E>> *);
|
||||
template ObjectFile<E> *read_lto_object(Context<E> &, MappedFile *);
|
||||
template std::vector<ObjectFile<E> *> do_lto(Context<E> &);
|
||||
template void lto_cleanup(Context<E> &);
|
||||
|
||||
|
46
elf/main.cc
46
elf/main.cc
@ -27,7 +27,7 @@ namespace mold::elf {
|
||||
// Read the beginning of a given file and returns its machine type
|
||||
// (e.g. EM_X86_64 or EM_386).
|
||||
template <typename E>
|
||||
std::string_view get_machine_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
std::string_view get_machine_type(Context<E> &ctx, MappedFile *mf) {
|
||||
auto get_elf_type = [&](u8 *buf) -> std::string_view {
|
||||
bool is_le = (((ElfEhdr<I386> *)buf)->e_ident[EI_DATA] == ELFDATA2LSB);
|
||||
bool is_64;
|
||||
@ -83,12 +83,12 @@ std::string_view get_machine_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
case FileType::GCC_LTO_OBJ:
|
||||
return get_elf_type(mf->data);
|
||||
case FileType::AR:
|
||||
for (MappedFile<Context<E>> *child : read_fat_archive_members(ctx, mf))
|
||||
for (MappedFile *child : read_fat_archive_members(ctx, mf))
|
||||
if (get_file_type(ctx, child) == FileType::ELF_OBJ)
|
||||
return get_elf_type(child->data);
|
||||
return "";
|
||||
case FileType::THIN_AR:
|
||||
for (MappedFile<Context<E>> *child : read_thin_archive_members(ctx, mf))
|
||||
for (MappedFile *child : read_thin_archive_members(ctx, mf))
|
||||
if (get_file_type(ctx, child) == FileType::ELF_OBJ)
|
||||
return get_elf_type(child->data);
|
||||
return "";
|
||||
@ -101,7 +101,7 @@ std::string_view get_machine_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
|
||||
template <typename E>
|
||||
static void
|
||||
check_file_compatibility(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
check_file_compatibility(Context<E> &ctx, MappedFile *mf) {
|
||||
std::string_view target = get_machine_type(ctx, mf);
|
||||
if (target != ctx.arg.emulation)
|
||||
Fatal(ctx) << mf->name << ": incompatible file type: "
|
||||
@ -109,7 +109,7 @@ check_file_compatibility(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static ObjectFile<E> *new_object_file(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
static ObjectFile<E> *new_object_file(Context<E> &ctx, MappedFile *mf,
|
||||
std::string archive_name) {
|
||||
static Counter count("parsed_objs");
|
||||
count++;
|
||||
@ -126,7 +126,7 @@ static ObjectFile<E> *new_object_file(Context<E> &ctx, MappedFile<Context<E>> *m
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static ObjectFile<E> *new_lto_obj(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
static ObjectFile<E> *new_lto_obj(Context<E> &ctx, MappedFile *mf,
|
||||
std::string archive_name) {
|
||||
static Counter count("parsed_lto_objs");
|
||||
count++;
|
||||
@ -146,7 +146,7 @@ static ObjectFile<E> *new_lto_obj(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
|
||||
template <typename E>
|
||||
static SharedFile<E> *
|
||||
new_shared_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
new_shared_file(Context<E> &ctx, MappedFile *mf) {
|
||||
check_file_compatibility(ctx, mf);
|
||||
|
||||
SharedFile<E> *file = SharedFile<E>::create(ctx, mf);
|
||||
@ -158,7 +158,7 @@ new_shared_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void read_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
void read_file(Context<E> &ctx, MappedFile *mf) {
|
||||
if (ctx.visited.contains(mf->name))
|
||||
return;
|
||||
|
||||
@ -172,7 +172,7 @@ void read_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
return;
|
||||
case FileType::AR:
|
||||
case FileType::THIN_AR:
|
||||
for (MappedFile<Context<E>> *child : read_archive_members(ctx, mf)) {
|
||||
for (MappedFile *child : read_archive_members(ctx, mf)) {
|
||||
switch (get_file_type(ctx, child)) {
|
||||
case FileType::ELF_OBJ:
|
||||
ctx.objs.push_back(new_object_file(ctx, child, mf->name));
|
||||
@ -211,7 +211,7 @@ static std::string_view
|
||||
deduce_machine_type(Context<E> &ctx, std::span<std::string> args) {
|
||||
for (std::string_view arg : args)
|
||||
if (!arg.starts_with('-'))
|
||||
if (auto *mf = MappedFile<Context<E>>::open(ctx, std::string(arg)))
|
||||
if (auto *mf = open_file(ctx, std::string(arg)))
|
||||
if (std::string_view target = get_machine_type(ctx, mf);
|
||||
!target.empty())
|
||||
return target;
|
||||
@ -219,8 +219,8 @@ deduce_machine_type(Context<E> &ctx, std::span<std::string> args) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path) {
|
||||
MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path);
|
||||
MappedFile *open_library(Context<E> &ctx, std::string path) {
|
||||
MappedFile *mf = open_file(ctx, path);
|
||||
if (!mf)
|
||||
return nullptr;
|
||||
|
||||
@ -233,11 +233,11 @@ MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path) {
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string name) {
|
||||
MappedFile *find_library(Context<E> &ctx, std::string name) {
|
||||
if (name.starts_with(':')) {
|
||||
for (std::string_view dir : ctx.arg.library_paths) {
|
||||
std::string path = std::string(dir) + "/" + name.substr(1);
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, path))
|
||||
if (MappedFile *mf = open_library(ctx, path))
|
||||
return mf;
|
||||
}
|
||||
Fatal(ctx) << "library not found: " << name;
|
||||
@ -246,22 +246,22 @@ MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string name) {
|
||||
for (std::string_view dir : ctx.arg.library_paths) {
|
||||
std::string stem = std::string(dir) + "/lib" + name;
|
||||
if (!ctx.is_static)
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, stem + ".so"))
|
||||
if (MappedFile *mf = open_library(ctx, stem + ".so"))
|
||||
return mf;
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, stem + ".a"))
|
||||
if (MappedFile *mf = open_library(ctx, stem + ".a"))
|
||||
return mf;
|
||||
}
|
||||
Fatal(ctx) << "library not found: " << name;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *find_from_search_paths(Context<E> &ctx, std::string name) {
|
||||
if (MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, name))
|
||||
MappedFile *find_from_search_paths(Context<E> &ctx, std::string name) {
|
||||
if (MappedFile *mf = open_file(ctx, name))
|
||||
return mf;
|
||||
|
||||
for (std::string_view dir : ctx.arg.library_paths)
|
||||
if (MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::open(ctx, std::string(dir) + "/" + name))
|
||||
if (MappedFile *mf =
|
||||
open_file(ctx, std::string(dir) + "/" + name))
|
||||
return mf;
|
||||
return nullptr;
|
||||
}
|
||||
@ -294,7 +294,7 @@ static void read_input_files(Context<E> &ctx, std::span<std::string> args) {
|
||||
} else if (arg == "--end-lib") {
|
||||
ctx.in_lib = false;
|
||||
} else if (remove_prefix(arg, "--version-script=")) {
|
||||
MappedFile<Context<E>> *mf = find_from_search_paths(ctx, std::string(arg));
|
||||
MappedFile *mf = find_from_search_paths(ctx, std::string(arg));
|
||||
if (!mf)
|
||||
Fatal(ctx) << "--version-script: file not found: " << arg;
|
||||
parse_version_script(ctx, mf);
|
||||
@ -308,11 +308,11 @@ static void read_input_files(Context<E> &ctx, std::span<std::string> args) {
|
||||
state.back();
|
||||
state.pop_back();
|
||||
} else if (remove_prefix(arg, "-l")) {
|
||||
MappedFile<Context<E>> *mf = find_library(ctx, std::string(arg));
|
||||
MappedFile *mf = find_library(ctx, std::string(arg));
|
||||
mf->given_fullpath = false;
|
||||
read_file(ctx, mf);
|
||||
} else {
|
||||
read_file(ctx, MappedFile<Context<E>>::must_open(ctx, std::string(arg)));
|
||||
read_file(ctx, must_open_file(ctx, std::string(arg)));
|
||||
}
|
||||
}
|
||||
|
||||
|
32
elf/mold.h
32
elf/mold.h
@ -1077,7 +1077,7 @@ struct MergeableSection {
|
||||
template <typename E>
|
||||
class InputFile {
|
||||
public:
|
||||
InputFile(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
InputFile(Context<E> &ctx, MappedFile *mf);
|
||||
InputFile() : filename("<internal>") {}
|
||||
|
||||
virtual ~InputFile() = default;
|
||||
@ -1106,7 +1106,7 @@ public:
|
||||
std::span<Symbol<E> *> get_global_syms();
|
||||
std::string_view get_source_name() const;
|
||||
|
||||
MappedFile<Context<E>> *mf = nullptr;
|
||||
MappedFile *mf = nullptr;
|
||||
std::span<ElfShdr<E>> elf_sections;
|
||||
std::span<ElfSym<E>> elf_syms;
|
||||
std::vector<Symbol<E> *> symbols;
|
||||
@ -1155,7 +1155,7 @@ class ObjectFile : public InputFile<E> {
|
||||
public:
|
||||
ObjectFile() = default;
|
||||
|
||||
static ObjectFile<E> *create(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
static ObjectFile<E> *create(Context<E> &ctx, MappedFile *mf,
|
||||
std::string archive_name, bool is_in_lib);
|
||||
|
||||
void parse(Context<E> &ctx);
|
||||
@ -1213,7 +1213,7 @@ public:
|
||||
[[no_unique_address]] ObjectFileExtras<E> extra;
|
||||
|
||||
private:
|
||||
ObjectFile(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
ObjectFile(Context<E> &ctx, MappedFile *mf,
|
||||
std::string archive_name, bool is_in_lib);
|
||||
|
||||
void initialize_sections(Context<E> &ctx);
|
||||
@ -1235,7 +1235,7 @@ private:
|
||||
template <typename E>
|
||||
class SharedFile : public InputFile<E> {
|
||||
public:
|
||||
static SharedFile<E> *create(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
static SharedFile<E> *create(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
void parse(Context<E> &ctx);
|
||||
void resolve_symbols(Context<E> &ctx) override;
|
||||
@ -1254,7 +1254,7 @@ public:
|
||||
std::vector<ElfSym<E>> elf_syms2;
|
||||
|
||||
private:
|
||||
SharedFile(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
SharedFile(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
std::string get_soname(Context<E> &ctx);
|
||||
void maybe_override_symbol(Symbol<E> &sym, const ElfSym<E> &esym);
|
||||
@ -1273,14 +1273,14 @@ private:
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
void parse_linker_script(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
void parse_linker_script(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
template <typename E>
|
||||
std::string_view
|
||||
get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
get_script_output_type(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
template <typename E>
|
||||
void parse_version_script(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
void parse_version_script(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
struct DynamicPattern {
|
||||
std::string_view pattern;
|
||||
@ -1297,7 +1297,7 @@ parse_dynamic_list(Context<E> &ctx, std::string_view path);
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile<Context<E>> *mb);
|
||||
ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile *mb);
|
||||
|
||||
template <typename E>
|
||||
std::vector<ObjectFile<E> *> do_lto(Context<E> &ctx);
|
||||
@ -1770,7 +1770,7 @@ struct Context {
|
||||
bool is_static;
|
||||
bool in_lib = false;
|
||||
i64 file_priority = 10000;
|
||||
MappedFile<Context<E>> *script_file = nullptr;
|
||||
MappedFile *script_file = nullptr;
|
||||
std::unordered_set<std::string_view> visited;
|
||||
tbb::task_group tg;
|
||||
|
||||
@ -1789,7 +1789,7 @@ struct Context {
|
||||
tbb::concurrent_vector<std::unique_ptr<ObjectFile<E>>> obj_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<SharedFile<E>>> dso_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<u8[]>> string_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<MappedFile<Context<E>>>> mf_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<MappedFile>> mf_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<Chunk<E>>> chunk_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<OutputSection<E>>> osec_pool;
|
||||
|
||||
@ -1897,16 +1897,16 @@ struct Context {
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::string_view get_machine_type(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
std::string_view get_machine_type(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path);
|
||||
MappedFile *open_library(Context<E> &ctx, std::string path);
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string path);
|
||||
MappedFile *find_library(Context<E> &ctx, std::string path);
|
||||
|
||||
template <typename E>
|
||||
void read_file(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
void read_file(Context<E> &ctx, MappedFile *mf);
|
||||
|
||||
template <typename E>
|
||||
int elf_main(int argc, char **argv);
|
||||
|
@ -999,12 +999,11 @@ void write_repro_file(Context<E> &ctx) {
|
||||
|
||||
std::unordered_set<std::string_view> seen;
|
||||
|
||||
for (std::unique_ptr<MappedFile<Context<E>>> &mf : ctx.mf_pool) {
|
||||
for (std::unique_ptr<MappedFile> &mf : ctx.mf_pool) {
|
||||
if (!mf->parent && seen.insert(mf->name).second) {
|
||||
// We reopen a file because we may have modified the contents of mf
|
||||
// in memory, which is mapped with PROT_WRITE and MAP_PRIVATE.
|
||||
MappedFile<Context<E>> *mf2 =
|
||||
MappedFile<Context<E>>::must_open(ctx, mf->name);
|
||||
MappedFile *mf2 = must_open_file(ctx, mf->name);
|
||||
tar->append(path, mf2->get_contents());
|
||||
mf2->unmap();
|
||||
}
|
||||
@ -2882,7 +2881,7 @@ void write_dependency_file(Context<E> &ctx) {
|
||||
std::vector<std::string> deps;
|
||||
std::unordered_set<std::string> seen;
|
||||
|
||||
for (std::unique_ptr<MappedFile<Context<E>>> &mf : ctx.mf_pool)
|
||||
for (std::unique_ptr<MappedFile> &mf : ctx.mf_pool)
|
||||
if (!mf->parent)
|
||||
if (std::string path = path_clean(mf->name); seen.insert(path).second)
|
||||
deps.push_back(path);
|
||||
@ -2946,7 +2945,7 @@ void show_stats(Context<E> &ctx) {
|
||||
}
|
||||
|
||||
static Counter num_bytes("total_input_bytes");
|
||||
for (std::unique_ptr<MappedFile<Context<E>>> &mf : ctx.mf_pool)
|
||||
for (std::unique_ptr<MappedFile> &mf : ctx.mf_pool)
|
||||
num_bytes += mf->size;
|
||||
|
||||
static Counter num_input_sections("input_sections");
|
||||
|
Loading…
Reference in New Issue
Block a user