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