Driver.cpp revision 292934
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 22using namespace llvm; 23using namespace llvm::ELF; 24using namespace llvm::object; 25 26using namespace lld; 27using namespace lld::elf2; 28 29Configuration *lld::elf2::Config; 30LinkerDriver *lld::elf2::Driver; 31 32void 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 40static 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. 62void 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 94static StringRef 95getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") { 96 if (auto *Arg = Args.getLastArg(Key)) 97 return Arg->getValue(); 98 return Default; 99} 100 101static 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 108void 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 138void 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->PrintGcSections = Args.hasArg(OPT_print_gc_sections); 167 Config->Shared = Args.hasArg(OPT_shared); 168 Config->StripAll = Args.hasArg(OPT_strip_all); 169 Config->Verbose = Args.hasArg(OPT_verbose); 170 171 Config->DynamicLinker = getString(Args, OPT_dynamic_linker); 172 Config->Entry = getString(Args, OPT_entry); 173 Config->Fini = getString(Args, OPT_fini, "_fini"); 174 Config->Init = getString(Args, OPT_init, "_init"); 175 Config->OutputFile = getString(Args, OPT_o); 176 Config->SoName = getString(Args, OPT_soname); 177 Config->Sysroot = getString(Args, OPT_sysroot); 178 179 Config->ZExecStack = hasZOption(Args, "execstack"); 180 Config->ZNodelete = hasZOption(Args, "nodelete"); 181 Config->ZNow = hasZOption(Args, "now"); 182 Config->ZOrigin = hasZOption(Args, "origin"); 183 Config->ZRelro = !hasZOption(Args, "norelro"); 184 185 if (auto *Arg = Args.getLastArg(OPT_O)) { 186 StringRef Val = Arg->getValue(); 187 if (Val.getAsInteger(10, Config->Optimize)) 188 error("Invalid optimization level"); 189 } 190 191 if (auto *Arg = Args.getLastArg(OPT_hash_style)) { 192 StringRef S = Arg->getValue(); 193 if (S == "gnu") { 194 Config->GnuHash = true; 195 Config->SysvHash = false; 196 } else if (S == "both") { 197 Config->GnuHash = true; 198 } else if (S != "sysv") 199 error("Unknown hash style: " + S); 200 } 201 202 for (auto *Arg : Args.filtered(OPT_undefined)) 203 Config->Undefined.push_back(Arg->getValue()); 204 205 for (auto *Arg : Args) { 206 switch (Arg->getOption().getID()) { 207 case OPT_l: 208 addFile(searchLibrary(Arg->getValue())); 209 break; 210 case OPT_INPUT: 211 case OPT_script: 212 addFile(Arg->getValue()); 213 break; 214 case OPT_as_needed: 215 Config->AsNeeded = true; 216 break; 217 case OPT_no_as_needed: 218 Config->AsNeeded = false; 219 break; 220 case OPT_Bstatic: 221 Config->Static = true; 222 break; 223 case OPT_Bdynamic: 224 Config->Static = false; 225 break; 226 case OPT_whole_archive: 227 WholeArchive = true; 228 break; 229 case OPT_no_whole_archive: 230 WholeArchive = false; 231 break; 232 } 233 } 234 235 if (Files.empty()) 236 error("no input files."); 237 238 if (Config->GnuHash && Config->EMachine == EM_MIPS) 239 error("The .gnu.hash section is not compatible with the MIPS target."); 240} 241 242template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { 243 SymbolTable<ELFT> Symtab; 244 Target.reset(createTarget()); 245 246 if (!Config->Shared) { 247 // Add entry symbol. 248 if (Config->Entry.empty()) 249 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; 250 251 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol 252 // is magical and is used to produce a R_386_GOTPC relocation. 253 // The R_386_GOTPC relocation value doesn't actually depend on the 254 // symbol value, so it could use an index of STN_UNDEF which, according 255 // to the spec, means the symbol value is 0. 256 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in 257 // the object file. 258 // The situation is even stranger on x86_64 where the assembly doesn't 259 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as 260 // an undefined symbol in the .o files. 261 // Given that the symbol is effectively unused, we just create a dummy 262 // hidden one to avoid the undefined symbol error. 263 Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_"); 264 } 265 266 if (!Config->Entry.empty()) { 267 // Set either EntryAddr (if S is a number) or EntrySym (otherwise). 268 StringRef S = Config->Entry; 269 if (S.getAsInteger(0, Config->EntryAddr)) 270 Config->EntrySym = Symtab.addUndefined(S); 271 } 272 273 if (Config->EMachine == EM_MIPS) { 274 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between 275 // start of function and gp pointer into GOT. 276 Config->MipsGpDisp = Symtab.addIgnored("_gp_disp"); 277 278 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer 279 // so that it points to an absolute address which is relative to GOT. 280 // See "Global Data Symbols" in Chapter 6 in the following document: 281 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 282 Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp); 283 } 284 285 for (std::unique_ptr<InputFile> &F : Files) 286 Symtab.addFile(std::move(F)); 287 288 for (StringRef S : Config->Undefined) 289 Symtab.addUndefinedOpt(S); 290 291 if (Config->OutputFile.empty()) 292 Config->OutputFile = "a.out"; 293 294 // Write the result to the file. 295 Symtab.scanShlibUndefined(); 296 if (Config->GcSections) 297 markLive<ELFT>(&Symtab); 298 writeResult<ELFT>(&Symtab); 299} 300