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 { |
| 28 | Configuration *Config; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | |
| 30 | void link(ArrayRef<const char *> Args) { |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 31 | Configuration C; |
| 32 | Config = &C; |
Rui Ueyama | 880632c | 2015-08-11 21:45:55 +0000 | [diff] [blame] | 33 | LinkerDriver().link(Args.slice(1)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | // Opens a file. Path has to be resolved already. |
| 40 | // Newly created memory buffers are owned by this driver. |
| 41 | MemoryBufferRef LinkerDriver::openFile(StringRef Path) { |
| 42 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); |
| 43 | error(MBOrErr, Twine("cannot open ") + Path); |
| 44 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
| 45 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 46 | OwningMBs.push_back(std::move(MB)); // take ownership |
| 47 | return MBRef; |
| 48 | } |
| 49 | |
| 50 | static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) { |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 51 | using namespace llvm::sys::fs; |
| 52 | file_magic Magic = identify_magic(MB.getBuffer()); |
| 53 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 54 | if (Magic == file_magic::archive) |
| 55 | return make_unique<ArchiveFile>(MB); |
Rafael Espindola | aefd5c1 | 2015-08-04 15:45:54 +0000 | [diff] [blame] | 56 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 57 | if (Magic == file_magic::elf_shared_object) |
| 58 | return createELFFile<SharedFile>(MB); |
| 59 | |
| 60 | return createELFFile<ObjectFile>(MB); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 63 | // Makes a path by concatenating Dir and File. |
| 64 | // If Dir starts with "=" the result will be preceded by SysRoot, |
| 65 | // which can be set with --sysroot command line switch. |
| 66 | static std::string buildSysrootedPath(StringRef Dir, StringRef File) { |
| 67 | SmallString<128> Path; |
| 68 | if (Dir.startswith("=")) { |
| 69 | Path.assign(Config->Sysroot); |
| 70 | sys::path::append(Path, Dir.substr(1), File); |
| 71 | } else { |
| 72 | Path.assign(Dir); |
| 73 | sys::path::append(Path, File); |
| 74 | } |
| 75 | return Path.str().str(); |
| 76 | } |
| 77 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 78 | // Searches a given library from input search paths, which are filled |
| 79 | // from -L command line switches. Returns a path to an existent library file. |
| 80 | static std::string searchLibrary(StringRef Path) { |
| 81 | std::vector<std::string> Names; |
| 82 | if (Path[0] == ':') { |
| 83 | Names.push_back(Path.drop_front().str()); |
| 84 | } else { |
| 85 | Names.push_back((Twine("lib") + Path + ".so").str()); |
| 86 | Names.push_back((Twine("lib") + Path + ".a").str()); |
| 87 | } |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 88 | for (StringRef Dir : Config->InputSearchPaths) { |
| 89 | for (const std::string &Name : Names) { |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 90 | std::string FullPath = buildSysrootedPath(Dir, Name); |
| 91 | if (sys::fs::exists(FullPath)) |
| 92 | return FullPath; |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | error(Twine("Unable to find library -l") + Path); |
| 96 | } |
| 97 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 98 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
| 99 | // Parse command line options. |
| 100 | opt::InputArgList Args = Parser.parse(ArgsArr); |
| 101 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 102 | if (auto *Arg = Args.getLastArg(OPT_output)) |
| 103 | Config->OutputFile = Arg->getValue(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 104 | |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 105 | if (auto *Arg = Args.getLastArg(OPT_dynamic_linker)) |
| 106 | Config->DynamicLinker = Arg->getValue(); |
| 107 | |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 108 | if (auto *Arg = Args.getLastArg(OPT_sysroot)) |
| 109 | Config->Sysroot = Arg->getValue(); |
| 110 | |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 111 | std::vector<StringRef> RPaths; |
| 112 | for (auto *Arg : Args.filtered(OPT_rpath)) |
| 113 | RPaths.push_back(Arg->getValue()); |
| 114 | if (!RPaths.empty()) |
| 115 | Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); |
| 116 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 117 | for (auto *Arg : Args.filtered(OPT_L)) |
| 118 | Config->InputSearchPaths.push_back(Arg->getValue()); |
| 119 | |
Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 120 | if (Args.hasArg(OPT_shared)) |
| 121 | Config->Shared = true; |
| 122 | |
Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 123 | if (Args.hasArg(OPT_discard_all)) |
| 124 | Config->DiscardAll = true; |
| 125 | |
Davide Italiano | 5445b2de | 2015-09-20 21:58:12 +0000 | [diff] [blame] | 126 | if (Args.hasArg(OPT_discard_locals)) |
| 127 | Config->DiscardLocals = true; |
| 128 | |
Davide Italiano | d75d3b9 | 2015-09-24 15:08:23 +0000 | [diff] [blame] | 129 | if (Args.hasArg(OPT_discard_none)) |
| 130 | Config->DiscardNone = true; |
| 131 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 132 | if (Args.hasArg(OPT_export_dynamic)) |
| 133 | Config->ExportDynamic = true; |
| 134 | |
Rafael Espindola | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 135 | if (Args.hasArg(OPT_noinhibit_exec)) |
| 136 | Config->NoInhibitExec = true; |
| 137 | |
Rafael Espindola | 4b2ca85 | 2015-09-28 20:30:11 +0000 | [diff] [blame] | 138 | if (Args.hasArg(OPT_allow_multiple_definition)) |
| 139 | Config->AllowMultipleDefinition = true; |
| 140 | |
Rui Ueyama | 9d4c6d7 | 2015-09-29 16:40:13 +0000 | [diff] [blame] | 141 | if (auto *Arg = Args.getLastArg(OPT_entry)) |
| 142 | Config->Entry = Arg->getValue(); |
| 143 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 144 | // Create a list of input files. |
| 145 | std::vector<MemoryBufferRef> Inputs; |
| 146 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 147 | for (auto *Arg : Args.filtered(OPT_l, OPT_INPUT)) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 148 | StringRef Path = Arg->getValue(); |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 149 | if (Arg->getOption().getID() == OPT_l) { |
| 150 | Inputs.push_back(openFile(searchLibrary(Path))); |
| 151 | continue; |
| 152 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 153 | Inputs.push_back(openFile(Path)); |
| 154 | } |
| 155 | |
| 156 | if (Inputs.empty()) |
| 157 | error("no input files."); |
| 158 | |
| 159 | // Create a symbol table. |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 160 | SymbolTable Symtab; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 161 | |
| 162 | // Parse all input files and put all symbols to the symbol table. |
| 163 | // The symbol table will take care of name resolution. |
| 164 | for (MemoryBufferRef MB : Inputs) { |
| 165 | std::unique_ptr<InputFile> File = createFile(MB); |
| 166 | Symtab.addFile(std::move(File)); |
| 167 | } |
| 168 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 169 | // Write the result. |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 170 | const ELFFileBase *FirstObj = Symtab.getFirstELF(); |
| 171 | switch (FirstObj->getELFKind()) { |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 172 | case ELF32LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 173 | writeResult<object::ELF32LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 174 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 175 | case ELF32BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 176 | writeResult<object::ELF32BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 177 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 178 | case ELF64LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 179 | writeResult<object::ELF64LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 180 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 181 | case ELF64BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 182 | writeResult<object::ELF64BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 183 | return; |
| 184 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 185 | } |