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