Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Mar 20, 2024
1 parent 08fada7 commit 7c8ed4d
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 89 deletions.
2 changes: 1 addition & 1 deletion common/archive-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion common/cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
20 changes: 8 additions & 12 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -954,15 +954,12 @@ class TarWriter {

// 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() {
Expand Down Expand Up @@ -997,13 +994,14 @@ class MappedFile {
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);

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
Expand Down
10 changes: 5 additions & 5 deletions elf/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -124,15 +124,15 @@ 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;
}

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);
Expand Down Expand Up @@ -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;
}
Expand Down
32 changes: 16 additions & 16 deletions elf/linker-script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,37 +150,37 @@ 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;
if (ctx.arg.sysroot.empty())
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;
}

Expand All @@ -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);
}
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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 "";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);


Expand Down
12 changes: 6 additions & 6 deletions elf/lto-unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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> &);

Expand Down
4 changes: 2 additions & 2 deletions elf/lto-win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand All @@ -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> &);

Expand Down
Loading

0 comments on commit 7c8ed4d

Please sign in to comment.