From ee158a031d8b33d80528d93149798dffa44ffaa7 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 22 Oct 2021 16:34:49 +0900 Subject: [PATCH] [Mach-O] wip --- macho/main.cc | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/macho/main.cc b/macho/main.cc index f8428d34..4bfbf389 100644 --- a/macho/main.cc +++ b/macho/main.cc @@ -117,7 +117,7 @@ static void fix_synthetic_symbol_values(Context &ctx) { intern(ctx, "__dyld_private")->value = ctx.data.hdr.addr; } -void read_file(Context &ctx, MappedFile *mf) { +static void read_file(Context &ctx, MappedFile *mf) { switch (get_file_type(mf)) { case FileType::MACH_OBJ: ctx.objs.push_back(ObjectFile::create(ctx, mf)); @@ -127,6 +127,29 @@ void read_file(Context &ctx, MappedFile *mf) { } } +MappedFile *find_library(Context &ctx, std::string name) { + for (std::string dir : ctx.arg.library_paths) { + for (std::string ext : {".tbd", ".dylib", ".a"}) { + std::string path = path_clean(dir + "/" + name + ext); + if (MappedFile *mf = MappedFile::open(ctx, path)) + return mf; + } + } + return nullptr; +} + +static void read_input_files(Context &ctx, std::span args) { + for (std::string &arg : args) { + if (arg.starts_with("-l")) { + MappedFile *mf = find_library(ctx, arg.substr(2)); + if (!mf) + Fatal(ctx) << "library not found: " << arg; + } else { + read_file(ctx, MappedFile::must_open(ctx, arg)); + } + } +} + int main(int argc, char **argv) { Context ctx; @@ -156,8 +179,7 @@ int main(int argc, char **argv) { std::vector file_args; parse_nonpositional_args(ctx, file_args); - for (std::string_view arg : file_args) - read_file(ctx, MappedFile::must_open(ctx, std::string(arg))); + read_input_files(ctx, file_args); for (ObjectFile *obj : ctx.objs) obj->parse(ctx);