diff --git a/main.cc b/main.cc index fe3a3793..91644d39 100644 --- a/main.cc +++ b/main.cc @@ -3,6 +3,7 @@ #include "llvm/BinaryFormat/Magic.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/FileSystem.h" #include #include @@ -14,6 +15,7 @@ using namespace llvm; using namespace llvm::ELF; +using namespace llvm::sys; using llvm::object::Archive; using llvm::opt::InputArgList; @@ -587,6 +589,17 @@ static int parse_filler(opt::InputArgList &args) { return (u8)ret; } +static std::string find_library(StringRef name) { + for (StringRef dir : config.library_paths) { + if (std::string path = (dir + "/lib" + name + ".a").str(); fs::exists(path)) + return path; + if (!config.is_static) + if (std::string path = (dir + "/lib" + name + ".so").str(); fs::exists(path)) + return path; + } + error("library not found: " + name); +} + int main(int argc, char **argv) { // Parse command line options MyOptTable opt_table; @@ -613,9 +626,16 @@ int main(int argc, char **argv) { // Open input files { MyTimer t("open", parse_timer); - for (auto *arg : args) - if (arg->getOption().getID() == OPT_INPUT) + for (auto *arg : args) { + switch (arg->getOption().getID()) { + case OPT_INPUT: read_file(arg->getValue()); + break; + case OPT_library: + read_file(find_library(arg->getValue())); + break; + } + } } // Parse input files diff --git a/test/library-path-static.s b/test/library-path-static.s new file mode 100644 index 00000000..0c814d4c --- /dev/null +++ b/test/library-path-static.s @@ -0,0 +1,36 @@ +// RUN: cc -o %t.o -c %s +// RUN: mold -static -o %t.exe \ +// RUN: /usr/lib/x86_64-linux-gnu/crt1.o \ +// RUN: /usr/lib/x86_64-linux-gnu/crti.o \ +// RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o \ +// RUN: -L/usr/lib/gcc/x86_64-linux-gnu/9 \ +// RUN: -L/usr/lib/x86_64-linux-gnu \ +// RUN: -L/usr/lib64 \ +// RUN: -L/lib/x86_64-linux-gnu \ +// RUN: -L/lib64 \ +// RUN: -L/usr/lib/x86_64-linux-gnu \ +// RUN: -L/usr/lib64 \ +// RUN: -L/usr/lib64 \ +// RUN: -L/usr/lib \ +// RUN: -L/lib \ +// RUN: -L/usr/lib \ +// RUN: %t.o \ +// RUN: -lgcc \ +// RUN: -lgcc_eh \ +// RUN: -lc \ +// RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o \ +// RUN: /usr/lib/x86_64-linux-gnu/crtn.o +// RUN: %t.exe | grep 'Hello world' + + .text + .globl main +main: + lea msg(%rip), %rdi + xor %rax, %rax + call printf + xor %rax, %rax + ret + + .data +msg: + .string "Hello world\n"