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