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; |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 22 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lld; |
| 25 | using namespace lld::elf2; |
| 26 | |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 27 | Configuration *lld::elf2::Config; |
| 28 | LinkerDriver *lld::elf2::Driver; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 29 | |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 30 | void lld::elf2::link(ArrayRef<const char *> Args) { |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 31 | Configuration C; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 32 | LinkerDriver D; |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 33 | Config = &C; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 34 | Driver = &D; |
| 35 | Driver->link(Args.slice(1)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 38 | static void setELFType(StringRef Emul) { |
| 39 | if (Emul == "elf_i386") { |
| 40 | Config->ElfKind = ELF32LEKind; |
| 41 | Config->EMachine = EM_386; |
| 42 | return; |
| 43 | } |
| 44 | if (Emul == "elf_x86_64") { |
| 45 | Config->ElfKind = ELF64LEKind; |
| 46 | Config->EMachine = EM_X86_64; |
| 47 | return; |
| 48 | } |
| 49 | if (Emul == "elf32ppc") { |
| 50 | Config->ElfKind = ELF32BEKind; |
| 51 | Config->EMachine = EM_PPC; |
| 52 | return; |
| 53 | } |
| 54 | if (Emul == "elf64ppc") { |
| 55 | Config->ElfKind = ELF64BEKind; |
| 56 | Config->EMachine = EM_PPC64; |
| 57 | return; |
| 58 | } |
| 59 | error(Twine("Unknown emulation: ") + Emul); |
| 60 | } |
| 61 | |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 62 | // Makes a path by concatenating Dir and File. |
Igor Kudrin | f03d2b4 | 2015-09-30 10:39:37 +0000 | [diff] [blame] | 63 | // If Dir starts with '=' the result will be preceded by Sysroot, |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 64 | // which can be set with --sysroot command line switch. |
| 65 | static std::string buildSysrootedPath(StringRef Dir, StringRef File) { |
| 66 | SmallString<128> Path; |
Igor Kudrin | f03d2b4 | 2015-09-30 10:39:37 +0000 | [diff] [blame] | 67 | if (Dir.startswith("=")) |
| 68 | sys::path::append(Path, Config->Sysroot, Dir.substr(1), File); |
| 69 | else |
| 70 | sys::path::append(Path, Dir, File); |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 71 | return Path.str().str(); |
| 72 | } |
| 73 | |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 74 | // Searches a given library from input search paths, which are filled |
| 75 | // from -L command line switches. Returns a path to an existent library file. |
| 76 | static std::string searchLibrary(StringRef Path) { |
| 77 | std::vector<std::string> Names; |
| 78 | if (Path[0] == ':') { |
| 79 | Names.push_back(Path.drop_front().str()); |
| 80 | } else { |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 81 | if (!Config->Static) |
| 82 | Names.push_back((Twine("lib") + Path + ".so").str()); |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 83 | Names.push_back((Twine("lib") + Path + ".a").str()); |
| 84 | } |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 85 | for (StringRef Dir : Config->InputSearchPaths) { |
| 86 | for (const std::string &Name : Names) { |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 87 | std::string FullPath = buildSysrootedPath(Dir, Name); |
| 88 | if (sys::fs::exists(FullPath)) |
| 89 | return FullPath; |
Rafael Espindola | abb7b28 | 2015-09-28 12:52:21 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | error(Twine("Unable to find library -l") + Path); |
| 93 | } |
| 94 | |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 95 | template <template <class> class T> |
| 96 | std::unique_ptr<ELFFileBase> |
| 97 | LinkerDriver::createELFInputFile(MemoryBufferRef MB) { |
| 98 | std::unique_ptr<ELFFileBase> File = createELFFile<T>(MB); |
| 99 | const ELFKind ElfKind = File->getELFKind(); |
| 100 | const uint16_t EMachine = File->getEMachine(); |
| 101 | |
| 102 | // Grab target from the first input file if wasn't set by -m option. |
| 103 | if (Config->ElfKind == ELFNoneKind) { |
| 104 | Config->ElfKind = ElfKind; |
| 105 | Config->EMachine = EMachine; |
| 106 | return File; |
| 107 | } |
| 108 | if (ElfKind == Config->ElfKind && EMachine == Config->EMachine) |
| 109 | return File; |
| 110 | |
| 111 | if (const ELFFileBase *First = Symtab.getFirstELF()) |
| 112 | error(MB.getBufferIdentifier() + " is incompatible with " + |
| 113 | First->getName()); |
| 114 | error(MB.getBufferIdentifier() + " is incompatible with target architecture"); |
| 115 | } |
| 116 | |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 117 | // Opens and parses a file. Path has to be resolved already. |
| 118 | // Newly created memory buffers are owned by this driver. |
| 119 | void LinkerDriver::addFile(StringRef Path) { |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 120 | using namespace llvm::sys::fs; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 121 | auto MBOrErr = MemoryBuffer::getFile(Path); |
| 122 | error(MBOrErr, Twine("cannot open ") + Path); |
| 123 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
| 124 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 125 | OwningMBs.push_back(std::move(MB)); // take MB ownership |
| 126 | |
| 127 | switch (identify_magic(MBRef.getBuffer())) { |
| 128 | case file_magic::unknown: |
| 129 | readLinkerScript(MBRef); |
| 130 | return; |
| 131 | case file_magic::archive: |
| 132 | Symtab.addFile(make_unique<ArchiveFile>(MBRef)); |
| 133 | return; |
| 134 | case file_magic::elf_shared_object: |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 135 | Symtab.addFile(createELFInputFile<SharedFile>(MBRef)); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 136 | return; |
| 137 | default: |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 138 | Symtab.addFile(createELFInputFile<ObjectFile>(MBRef)); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 139 | } |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame^] | 142 | static StringRef |
| 143 | getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") { |
| 144 | if (auto *Arg = Args.getLastArg(Key)) |
| 145 | return Arg->getValue(); |
| 146 | return Default; |
| 147 | } |
| 148 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 149 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
| 150 | // Parse command line options. |
| 151 | opt::InputArgList Args = Parser.parse(ArgsArr); |
| 152 | |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame^] | 153 | for (auto *Arg : Args.filtered(OPT_L)) |
| 154 | Config->InputSearchPaths.push_back(Arg->getValue()); |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 155 | |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 156 | std::vector<StringRef> RPaths; |
| 157 | for (auto *Arg : Args.filtered(OPT_rpath)) |
| 158 | RPaths.push_back(Arg->getValue()); |
| 159 | if (!RPaths.empty()) |
| 160 | Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); |
| 161 | |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 162 | if (auto *Arg = Args.getLastArg(OPT_m)) |
| 163 | setELFType(Arg->getValue()); |
| 164 | |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 165 | Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); |
| 166 | Config->DiscardAll = Args.hasArg(OPT_discard_all); |
| 167 | Config->DiscardLocals = Args.hasArg(OPT_discard_locals); |
| 168 | Config->DiscardNone = Args.hasArg(OPT_discard_none); |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 169 | Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 170 | Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); |
| 171 | Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec); |
George Rimar | 57e40de | 2015-10-01 20:14:45 +0000 | [diff] [blame] | 172 | Config->NoUndefined = Args.hasArg(OPT_no_undefined); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 173 | Config->Shared = Args.hasArg(OPT_shared); |
| 174 | |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame^] | 175 | Config->DynamicLinker = getString(Args, OPT_dynamic_linker); |
| 176 | Config->Entry = getString(Args, OPT_entry); |
| 177 | Config->Fini = getString(Args, OPT_fini, "_fini"); |
| 178 | Config->Init = getString(Args, OPT_init, "_init"); |
| 179 | Config->OutputFile = getString(Args, OPT_output); |
| 180 | Config->SoName = getString(Args, OPT_soname); |
| 181 | Config->Sysroot = getString(Args, OPT_sysroot); |
| 182 | |
Rui Ueyama | 58d7d70 | 2015-10-07 18:22:46 +0000 | [diff] [blame] | 183 | for (auto *Arg : Args.filtered(OPT_z)) |
George Rimar | 97aad17 | 2015-10-07 15:00:21 +0000 | [diff] [blame] | 184 | if (Arg->getValue() == StringRef("now")) |
| 185 | Config->ZNow = true; |
George Rimar | 97aad17 | 2015-10-07 15:00:21 +0000 | [diff] [blame] | 186 | |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 187 | for (auto *Arg : Args) { |
| 188 | switch (Arg->getOption().getID()) { |
| 189 | case OPT_l: |
| 190 | addFile(searchLibrary(Arg->getValue())); |
| 191 | break; |
| 192 | case OPT_INPUT: |
| 193 | addFile(Arg->getValue()); |
| 194 | break; |
| 195 | case OPT_Bstatic: |
| 196 | Config->Static = true; |
| 197 | break; |
| 198 | case OPT_Bdynamic: |
| 199 | Config->Static = false; |
| 200 | break; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 201 | case OPT_whole_archive: |
| 202 | Config->WholeArchive = true; |
| 203 | break; |
| 204 | case OPT_no_whole_archive: |
| 205 | Config->WholeArchive = false; |
| 206 | break; |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 207 | default: |
| 208 | break; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 209 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 212 | if (Symtab.getObjectFiles().empty()) |
| 213 | error("no input files."); |
| 214 | |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 215 | for (auto *Arg : Args.filtered(OPT_undefined)) |
| 216 | Symtab.addUndefinedSym(Arg->getValue()); |
| 217 | |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 218 | if (Config->OutputFile.empty()) |
| 219 | Config->OutputFile = "a.out"; |
| 220 | |
Rui Ueyama | 7523039 | 2015-10-07 18:29:51 +0000 | [diff] [blame] | 221 | // Write the result to the file. |
| 222 | writeResult(&Symtab); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 223 | } |