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 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame^] | 63 | // Searches a given library from input search paths, which are filled |
| 64 | // from -L command line switches. Returns a path to an existent library file. |
| 65 | static std::string searchLibrary(StringRef Path) { |
| 66 | std::vector<std::string> Names; |
| 67 | if (Path[0] == ':') { |
| 68 | Names.push_back(Path.drop_front().str()); |
| 69 | } else { |
| 70 | Names.push_back((Twine("lib") + Path + ".so").str()); |
| 71 | Names.push_back((Twine("lib") + Path + ".a").str()); |
| 72 | } |
| 73 | SmallString<128> FullPath; |
| 74 | for (StringRef Dir : Config->InputSearchPaths) { |
| 75 | for (const std::string &Name : Names) { |
| 76 | FullPath = Dir; |
| 77 | sys::path::append(FullPath, Name); |
| 78 | if (sys::fs::exists(FullPath.str())) |
| 79 | return FullPath.str().str(); |
| 80 | } |
| 81 | } |
| 82 | error(Twine("Unable to find library -l") + Path); |
| 83 | } |
| 84 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 85 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
| 86 | // Parse command line options. |
| 87 | opt::InputArgList Args = Parser.parse(ArgsArr); |
| 88 | |
| 89 | // Handle -o |
| 90 | if (auto *Arg = Args.getLastArg(OPT_output)) |
| 91 | Config->OutputFile = Arg->getValue(); |
| 92 | if (Config->OutputFile.empty()) |
| 93 | error("-o must be specified."); |
| 94 | |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 95 | // Handle -dynamic-linker |
| 96 | if (auto *Arg = Args.getLastArg(OPT_dynamic_linker)) |
| 97 | Config->DynamicLinker = Arg->getValue(); |
| 98 | |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 99 | std::vector<StringRef> RPaths; |
| 100 | for (auto *Arg : Args.filtered(OPT_rpath)) |
| 101 | RPaths.push_back(Arg->getValue()); |
| 102 | if (!RPaths.empty()) |
| 103 | Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); |
| 104 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame^] | 105 | for (auto *Arg : Args.filtered(OPT_L)) |
| 106 | Config->InputSearchPaths.push_back(Arg->getValue()); |
| 107 | |
Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 108 | if (Args.hasArg(OPT_shared)) |
| 109 | Config->Shared = true; |
| 110 | |
Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 111 | if (Args.hasArg(OPT_discard_all)) |
| 112 | Config->DiscardAll = true; |
| 113 | |
Davide Italiano | 5445b2de | 2015-09-20 21:58:12 +0000 | [diff] [blame] | 114 | if (Args.hasArg(OPT_discard_locals)) |
| 115 | Config->DiscardLocals = true; |
| 116 | |
Davide Italiano | d75d3b9 | 2015-09-24 15:08:23 +0000 | [diff] [blame] | 117 | if (Args.hasArg(OPT_discard_none)) |
| 118 | Config->DiscardNone = true; |
| 119 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 120 | if (Args.hasArg(OPT_export_dynamic)) |
| 121 | Config->ExportDynamic = true; |
| 122 | |
Rafael Espindola | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 123 | if (Args.hasArg(OPT_noinhibit_exec)) |
| 124 | Config->NoInhibitExec = true; |
| 125 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 126 | // Create a list of input files. |
| 127 | std::vector<MemoryBufferRef> Inputs; |
| 128 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame^] | 129 | for (auto *Arg : Args.filtered(OPT_l, OPT_INPUT)) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 130 | StringRef Path = Arg->getValue(); |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame^] | 131 | if (Arg->getOption().getID() == OPT_l) { |
| 132 | Inputs.push_back(openFile(searchLibrary(Path))); |
| 133 | continue; |
| 134 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 135 | Inputs.push_back(openFile(Path)); |
| 136 | } |
| 137 | |
| 138 | if (Inputs.empty()) |
| 139 | error("no input files."); |
| 140 | |
| 141 | // Create a symbol table. |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 142 | SymbolTable Symtab; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 143 | |
| 144 | // Parse all input files and put all symbols to the symbol table. |
| 145 | // The symbol table will take care of name resolution. |
| 146 | for (MemoryBufferRef MB : Inputs) { |
| 147 | std::unique_ptr<InputFile> File = createFile(MB); |
| 148 | Symtab.addFile(std::move(File)); |
| 149 | } |
| 150 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 151 | // Write the result. |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 152 | const ELFFileBase *FirstObj = Symtab.getFirstELF(); |
| 153 | switch (FirstObj->getELFKind()) { |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 154 | case ELF32LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 155 | writeResult<object::ELF32LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 156 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 157 | case ELF32BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 158 | writeResult<object::ELF32BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 159 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 160 | case ELF64LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 161 | writeResult<object::ELF64LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 162 | return; |
Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 163 | case ELF64BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 164 | writeResult<object::ELF64BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 167 | } |