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 10 #include "Driver.h" 11 #include "Config.h" 12 #include "Error.h" 13 #include "InputFiles.h" 14 #include "SymbolTable.h" 15 #include "Target.h" 16 #include "Writer.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/StringExtras.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <utility> 21 22 using namespace llvm; 23 using namespace llvm::ELF; 24 using namespace llvm::object; 25 26 using namespace lld; 27 using namespace lld::elf2; 28 29 Configuration *lld::elf2::Config; 30 LinkerDriver *lld::elf2::Driver; 31 32 void lld::elf2::link(ArrayRef<const char *> Args) { 33 Configuration C; 34 LinkerDriver D; 35 Config = &C; 36 Driver = &D; 37 Driver->main(Args.slice(1)); 38 } 39 40 static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) { 41 if (S == "elf32btsmip") 42 return {ELF32BEKind, EM_MIPS}; 43 if (S == "elf32ltsmip") 44 return {ELF32LEKind, EM_MIPS}; 45 if (S == "elf32ppc") 46 return {ELF32BEKind, EM_PPC}; 47 if (S == "elf64ppc") 48 return {ELF64BEKind, EM_PPC64}; 49 if (S == "elf_i386") 50 return {ELF32LEKind, EM_386}; 51 if (S == "elf_x86_64") 52 return {ELF64LEKind, EM_X86_64}; 53 if (S == "aarch64linux") 54 return {ELF64LEKind, EM_AARCH64}; 55 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe") 56 error("Windows targets are not supported on the ELF frontend: " + S); 57 error("Unknown emulation: " + S); 58 } 59 60 // Opens and parses a file. Path has to be resolved already. 61 // Newly created memory buffers are owned by this driver. 62 void LinkerDriver::addFile(StringRef Path) { 63 using namespace llvm::sys::fs; 64 if (Config->Verbose) 65 llvm::outs() << Path << "\n"; 66 auto MBOrErr = MemoryBuffer::getFile(Path); 67 error(MBOrErr, "cannot open " + Path); 68 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 69 MemoryBufferRef MBRef = MB->getMemBufferRef(); 70 OwningMBs.push_back(std::move(MB)); // take MB ownership 71 72 switch (identify_magic(MBRef.getBuffer())) { 73 case file_magic::unknown: 74 readLinkerScript(&Alloc, MBRef); 75 return; 76 case file_magic::archive: 77 if (WholeArchive) { 78 auto File = make_unique<ArchiveFile>(MBRef); 79 for (MemoryBufferRef &MB : File->getMembers()) 80 Files.push_back(createELFFile<ObjectFile>(MB)); 81 OwningArchives.emplace_back(std::move(File)); 82 return; 83 } 84 Files.push_back(make_unique<ArchiveFile>(MBRef)); 85 return; 86 case file_magic::elf_shared_object: 87 Files.push_back(createELFFile<SharedFile>(MBRef)); 88 return; 89 default: 90 Files.push_back(createELFFile<ObjectFile>(MBRef)); 91 } 92 } 93 94 static StringRef 95 getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") { 96 if (auto *Arg = Args.getLastArg(Key)) 97 return Arg->getValue(); 98 return Default; 99 } 100 101 static bool hasZOption(opt::InputArgList &Args, StringRef Key) { 102 for (auto *Arg : Args.filtered(OPT_z)) 103 if (Key == Arg->getValue()) 104 return true; 105 return false; 106 } 107 108 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) { 109 initSymbols(); 110 111 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr); 112 createFiles(Args); 113 114 // Traditional linkers can generate re-linkable object files instead 115 // of executables or DSOs. We don't support that since the feature 116 // does not seem to provide more value than the static archiver. 117 if (Args.hasArg(OPT_relocatable)) 118 error("-r option is not supported. Use 'ar' command instead."); 119 120 switch (Config->EKind) { 121 case ELF32LEKind: 122 link<ELF32LE>(Args); 123 return; 124 case ELF32BEKind: 125 link<ELF32BE>(Args); 126 return; 127 case ELF64LEKind: 128 link<ELF64LE>(Args); 129 return; 130 case ELF64BEKind: 131 link<ELF64BE>(Args); 132 return; 133 default: 134 error("-m or at least a .o file required"); 135 } 136 } 137 138 void LinkerDriver::createFiles(opt::InputArgList &Args) { 139 for (auto *Arg : Args.filtered(OPT_L)) 140 Config->SearchPaths.push_back(Arg->getValue()); 141 142 std::vector<StringRef> RPaths; 143 for (auto *Arg : Args.filtered(OPT_rpath)) 144 RPaths.push_back(Arg->getValue()); 145 if (!RPaths.empty()) 146 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); 147 148 if (auto *Arg = Args.getLastArg(OPT_m)) { 149 StringRef S = Arg->getValue(); 150 std::pair<ELFKind, uint16_t> P = parseEmulation(S); 151 Config->EKind = P.first; 152 Config->EMachine = P.second; 153 Config->Emulation = S; 154 } 155 156 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); 157 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); 158 Config->DiscardAll = Args.hasArg(OPT_discard_all); 159 Config->DiscardLocals = Args.hasArg(OPT_discard_locals); 160 Config->DiscardNone = Args.hasArg(OPT_discard_none); 161 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); 162 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); 163 Config->GcSections = Args.hasArg(OPT_gc_sections); 164 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec); 165 Config->NoUndefined = Args.hasArg(OPT_no_undefined); 166 Config->Shared = Args.hasArg(OPT_shared); 167 Config->StripAll = Args.hasArg(OPT_strip_all); 168 Config->Verbose = Args.hasArg(OPT_verbose); 169 170 Config->DynamicLinker = getString(Args, OPT_dynamic_linker); 171 Config->Entry = getString(Args, OPT_entry); 172 Config->Fini = getString(Args, OPT_fini, "_fini"); 173 Config->Init = getString(Args, OPT_init, "_init"); 174 Config->OutputFile = getString(Args, OPT_o); 175 Config->SoName = getString(Args, OPT_soname); 176 Config->Sysroot = getString(Args, OPT_sysroot); 177 178 Config->ZExecStack = hasZOption(Args, "execstack"); 179 Config->ZNodelete = hasZOption(Args, "nodelete"); 180 Config->ZNow = hasZOption(Args, "now"); 181 Config->ZOrigin = hasZOption(Args, "origin"); 182 Config->ZRelro = !hasZOption(Args, "norelro"); 183 184 if (auto *Arg = Args.getLastArg(OPT_O)) { 185 StringRef Val = Arg->getValue(); 186 if (Val.getAsInteger(10, Config->Optimize)) 187 error("Invalid optimization level"); 188 } 189 190 if (auto *Arg = Args.getLastArg(OPT_hash_style)) { 191 StringRef S = Arg->getValue(); 192 if (S == "gnu") { 193 Config->GnuHash = true; 194 Config->SysvHash = false; 195 } else if (S == "both") { 196 Config->GnuHash = true; 197 } else if (S != "sysv") 198 error("Unknown hash style: " + S); 199 } 200 201 for (auto *Arg : Args.filtered(OPT_undefined)) 202 Config->Undefined.push_back(Arg->getValue()); 203 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 case OPT_script: 211 addFile(Arg->getValue()); 212 break; 213 case OPT_as_needed: 214 Config->AsNeeded = true; 215 break; 216 case OPT_no_as_needed: 217 Config->AsNeeded = false; 218 break; 219 case OPT_Bstatic: 220 Config->Static = true; 221 break; 222 case OPT_Bdynamic: 223 Config->Static = false; 224 break; 225 case OPT_whole_archive: 226 WholeArchive = true; 227 break; 228 case OPT_no_whole_archive: 229 WholeArchive = false; 230 break; 231 } 232 } 233 234 if (Files.empty()) 235 error("no input files."); 236 237 if (Config->GnuHash && Config->EMachine == EM_MIPS) 238 error("The .gnu.hash section is not compatible with the MIPS target."); 239 } 240 241 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { 242 SymbolTable<ELFT> Symtab; 243 Target.reset(createTarget()); 244 245 if (!Config->Shared) { 246 // Add entry symbol. 247 if (Config->Entry.empty()) 248 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; 249 250 // Set either EntryAddr (if S is a number) or EntrySym (otherwise). 251 StringRef S = Config->Entry; 252 if (S.getAsInteger(0, Config->EntryAddr)) 253 Config->EntrySym = Symtab.addUndefined(S); 254 255 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol 256 // is magical and is used to produce a R_386_GOTPC relocation. 257 // The R_386_GOTPC relocation value doesn't actually depend on the 258 // symbol value, so it could use an index of STN_UNDEF which, according 259 // to the spec, means the symbol value is 0. 260 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in 261 // the object file. 262 // The situation is even stranger on x86_64 where the assembly doesn't 263 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as 264 // an undefined symbol in the .o files. 265 // Given that the symbol is effectively unused, we just create a dummy 266 // hidden one to avoid the undefined symbol error. 267 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_"); 268 } 269 270 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer 271 // so that it points to an absolute address which is relative to GOT. 272 // See "Global Data Symbols" in Chapter 6 in the following document: 273 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 274 if (Config->EMachine == EM_MIPS) 275 Symtab.addAbsoluteSym("_gp", DefinedAbsolute<ELFT>::MipsGp); 276 277 for (std::unique_ptr<InputFile> &F : Files) 278 Symtab.addFile(std::move(F)); 279 280 for (StringRef S : Config->Undefined) 281 Symtab.addUndefinedOpt(S); 282 283 if (Config->OutputFile.empty()) 284 Config->OutputFile = "a.out"; 285 286 // Write the result to the file. 287 Symtab.scanShlibUndefined(); 288 if (Config->GcSections) 289 markLive<ELFT>(&Symtab); 290 writeResult<ELFT>(&Symtab); 291 } 292