Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- Driver.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 10 | #include "Driver.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 11 | #include "Config.h" |
| 12 | #include "Error.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
| 14 | #include "SymbolTable.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 15 | #include "Writer.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | using namespace lld; |
| 24 | using namespace lld::elf2; |
| 25 | |
| 26 | namespace lld { |
| 27 | namespace elf2 { |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 28 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | Configuration *Config; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 30 | std::vector<std::unique_ptr<MemoryBuffer>> *MemoryBufferPool; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | |
| 32 | void link(ArrayRef<const char *> Args) { |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 33 | Configuration C; |
| 34 | Config = &C; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 35 | std::vector<std::unique_ptr<MemoryBuffer>> V; |
| 36 | MemoryBufferPool = &V; |
Rui Ueyama | 880632c | 2015-08-11 21:45:55 +0000 | [diff] [blame] | 37 | LinkerDriver().link(Args.slice(1)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 40 | // Opens a file. Path has to be resolved already. |
| 41 | // Newly created memory buffers are owned by this driver. |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 42 | MemoryBufferRef openFile(StringRef Path) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 43 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); |
| 44 | error(MBOrErr, Twine("cannot open ") + Path); |
| 45 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
| 46 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 47 | MemoryBufferPool->push_back(std::move(MB)); // transfer ownership |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 48 | return MBRef; |
| 49 | } |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 50 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 53 | // Makes a path by concatenating Dir and File. |
Igor Kudrin | f03d2b4 | 2015-09-30 10:39:37 +0000 | [diff] [blame] | 54 | // If Dir starts with '=' the result will be preceded by Sysroot, |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 55 | // which can be set with --sysroot command line switch. |
| 56 | static std::string buildSysrootedPath(StringRef Dir, StringRef File) { |
| 57 | SmallString<128> Path; |
Igor Kudrin | f03d2b4 | 2015-09-30 10:39:37 +0000 | [diff] [blame] | 58 | if (Dir.startswith("=")) |
| 59 | sys::path::append(Path, Config->Sysroot, Dir.substr(1), File); |
| 60 | else |
| 61 | sys::path::append(Path, Dir, File); |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 62 | return Path.str().str(); |
| 63 | } |
| 64 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 65 | // Searches a given library from input search paths, which are filled |
| 66 | // from -L command line switches. Returns a path to an existent library file. |
| 67 | static std::string searchLibrary(StringRef Path) { |
| 68 | std::vector<std::string> Names; |
| 69 | if (Path[0] == ':') { |
| 70 | Names.push_back(Path.drop_front().str()); |
| 71 | } else { |
| 72 | Names.push_back((Twine("lib") + Path + ".so").str()); |
| 73 | Names.push_back((Twine("lib") + Path + ".a").str()); |
| 74 | } |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 75 | for (StringRef Dir : Config->InputSearchPaths) { |
| 76 | for (const std::string &Name : Names) { |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 77 | std::string FullPath = buildSysrootedPath(Dir, Name); |
| 78 | if (sys::fs::exists(FullPath)) |
| 79 | return FullPath; |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | error(Twine("Unable to find library -l") + Path); |
| 83 | } |
| 84 | |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 85 | // Returns true if MB looks like a linker script. |
| 86 | static bool isLinkerScript(MemoryBufferRef MB) { |
| 87 | using namespace llvm::sys::fs; |
| 88 | return identify_magic(MB.getBuffer()) == file_magic::unknown; |
| 89 | } |
| 90 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 91 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
| 92 | // Parse command line options. |
| 93 | opt::InputArgList Args = Parser.parse(ArgsArr); |
| 94 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 95 | if (auto *Arg = Args.getLastArg(OPT_output)) |
| 96 | Config->OutputFile = Arg->getValue(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 97 | |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 98 | if (auto *Arg = Args.getLastArg(OPT_dynamic_linker)) |
| 99 | Config->DynamicLinker = Arg->getValue(); |
| 100 | |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 101 | if (auto *Arg = Args.getLastArg(OPT_sysroot)) |
| 102 | Config->Sysroot = Arg->getValue(); |
| 103 | |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 104 | std::vector<StringRef> RPaths; |
| 105 | for (auto *Arg : Args.filtered(OPT_rpath)) |
| 106 | RPaths.push_back(Arg->getValue()); |
| 107 | if (!RPaths.empty()) |
| 108 | Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); |
| 109 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 110 | for (auto *Arg : Args.filtered(OPT_L)) |
| 111 | Config->InputSearchPaths.push_back(Arg->getValue()); |
| 112 | |
Rui Ueyama | 9d4c6d7 | 2015-09-29 16:40:13 +0000 | [diff] [blame] | 113 | if (auto *Arg = Args.getLastArg(OPT_entry)) |
| 114 | Config->Entry = Arg->getValue(); |
| 115 | |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 116 | Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); |
| 117 | Config->DiscardAll = Args.hasArg(OPT_discard_all); |
| 118 | Config->DiscardLocals = Args.hasArg(OPT_discard_locals); |
| 119 | Config->DiscardNone = Args.hasArg(OPT_discard_none); |
| 120 | Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); |
| 121 | Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec); |
| 122 | Config->Shared = Args.hasArg(OPT_shared); |
| 123 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 124 | // Create a symbol table. |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 125 | SymbolTable Symtab; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 126 | |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 127 | for (auto *Arg : Args.filtered(OPT_l, OPT_INPUT)) { |
| 128 | std::string Path = Arg->getValue(); |
| 129 | if (Arg->getOption().getID() == OPT_l) |
| 130 | Path = searchLibrary(Path); |
| 131 | MemoryBufferRef MB = openFile(Path); |
| 132 | if (isLinkerScript(MB)) { |
| 133 | // readLinkerScript may add files to the symbol table. |
| 134 | readLinkerScript(&Symtab, MB); |
| 135 | continue; |
| 136 | } |
| 137 | Symtab.addFile(createFile(MB)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 140 | if (Symtab.getObjectFiles().empty()) |
| 141 | error("no input files."); |
| 142 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 143 | // Write the result. |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 144 | const ELFFileBase *FirstObj = Symtab.getFirstELF(); |
| 145 | switch (FirstObj->getELFKind()) { |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 146 | case ELF32LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 147 | writeResult<object::ELF32LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 148 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 149 | case ELF32BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 150 | writeResult<object::ELF32BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 151 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 152 | case ELF64LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 153 | writeResult<object::ELF64LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 154 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 155 | case ELF64BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 156 | writeResult<object::ELF64BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 159 | } |