blob: acb9d9b36d43c51c9a14a3c30042a5d64842f118 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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 Ueyamaafff74e22015-08-05 23:24:46 +000010#include "Driver.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000011#include "Config.h"
12#include "Error.h"
Rui Ueyamaafff74e22015-08-05 23:24:46 +000013#include "InputFiles.h"
14#include "SymbolTable.h"
Rui Ueyamaff777682015-10-09 21:12:40 +000015#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "Writer.h"
17#include "llvm/ADT/STLExtras.h"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000018#include "llvm/ADT/StringExtras.h"
Rui Ueyamaa4672402015-10-11 02:03:03 +000019#include "llvm/Support/raw_ostream.h"
Rui Ueyamacacf9672015-10-11 02:22:31 +000020#include <utility>
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
22using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000023using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000024using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace lld;
27using namespace lld::elf2;
28
Rui Ueyama983ed2b2015-10-01 15:23:09 +000029Configuration *lld::elf2::Config;
30LinkerDriver *lld::elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000031
Rui Ueyama983ed2b2015-10-01 15:23:09 +000032void lld::elf2::link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000033 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000034 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000035 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000036 Driver = &D;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000037 Driver->main(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000038}
39
Rui Ueyamacacf9672015-10-11 02:22:31 +000040static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
Rafael Espindola525914d2015-10-11 03:36:49 +000041 Config->Emulation = S;
Rui Ueyamaa561e782015-10-11 19:46:00 +000042 if (S == "elf32btsmip")
43 return {ELF32BEKind, EM_MIPS};
44 if (S == "elf32ltsmip")
45 return {ELF32LEKind, EM_MIPS};
46 if (S == "elf32ppc")
47 return {ELF32BEKind, EM_PPC};
48 if (S == "elf64ppc")
49 return {ELF64BEKind, EM_PPC64};
50 if (S == "elf_i386")
51 return {ELF32LEKind, EM_386};
52 if (S == "elf_x86_64")
53 return {ELF64LEKind, EM_X86_64};
Rui Ueyamacacf9672015-10-11 02:22:31 +000054 error("Unknown emulation: " + S);
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000055}
56
Rui Ueyama983ed2b2015-10-01 15:23:09 +000057// Opens and parses a file. Path has to be resolved already.
58// Newly created memory buffers are owned by this driver.
59void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000060 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +000061 if (Config->Verbose)
62 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +000063 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama1c42afc2015-10-12 15:49:06 +000064 error(MBOrErr, "cannot open " + Path);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000065 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
66 MemoryBufferRef MBRef = MB->getMemBufferRef();
67 OwningMBs.push_back(std::move(MB)); // take MB ownership
68
69 switch (identify_magic(MBRef.getBuffer())) {
70 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000071 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000072 return;
73 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000074 if (WholeArchive) {
75 auto File = make_unique<ArchiveFile>(MBRef);
76 for (MemoryBufferRef &MB : File->getMembers())
77 Files.push_back(createELFFile<ObjectFile>(MB));
78 OwningArchives.emplace_back(std::move(File));
79 return;
80 }
81 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000082 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +000083 case file_magic::elf_shared_object:
84 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000085 return;
86 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000087 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000088 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000089}
90
Rui Ueyama2cac5842015-10-07 19:34:51 +000091static StringRef
92getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
93 if (auto *Arg = Args.getLastArg(Key))
94 return Arg->getValue();
95 return Default;
96}
97
Rui Ueyama3ce825e2015-10-09 21:07:25 +000098void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +000099 initSymbols();
100
Rui Ueyamac6e1b972015-10-11 18:19:01 +0000101 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000102 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000103
Rui Ueyamae717a712015-10-13 16:20:50 +0000104 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000105 case ELF32LEKind:
106 link<ELF32LE>(Args);
107 return;
108 case ELF32BEKind:
109 link<ELF32BE>(Args);
110 return;
111 case ELF64LEKind:
112 link<ELF64LE>(Args);
113 return;
114 case ELF64BEKind:
115 link<ELF64BE>(Args);
116 return;
117 default:
118 error("-m or at least a .o file required");
119 }
120}
121
122void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000123 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000124 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000125
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000126 std::vector<StringRef> RPaths;
127 for (auto *Arg : Args.filtered(OPT_rpath))
128 RPaths.push_back(Arg->getValue());
129 if (!RPaths.empty())
130 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
131
Rui Ueyamacacf9672015-10-11 02:22:31 +0000132 if (auto *Arg = Args.getLastArg(OPT_m)) {
133 std::pair<ELFKind, uint16_t> P = parseEmulation(Arg->getValue());
Rui Ueyamae717a712015-10-13 16:20:50 +0000134 Config->EKind = P.first;
Rui Ueyamacacf9672015-10-11 02:22:31 +0000135 Config->EMachine = P.second;
136 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000137
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000138 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000139 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000140 Config->DiscardAll = Args.hasArg(OPT_discard_all);
141 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
142 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000143 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000144 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000145 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000146 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000147 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000148 Config->Shared = Args.hasArg(OPT_shared);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000149 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000150
Rui Ueyama2cac5842015-10-07 19:34:51 +0000151 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
152 Config->Entry = getString(Args, OPT_entry);
153 Config->Fini = getString(Args, OPT_fini, "_fini");
154 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000155 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000156 Config->SoName = getString(Args, OPT_soname);
157 Config->Sysroot = getString(Args, OPT_sysroot);
158
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000159 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
160 StringRef S = Arg->getValue();
161 if (S == "gnu") {
162 Config->GnuHash = true;
163 Config->SysvHash = false;
164 } else if (S == "both") {
165 Config->GnuHash = true;
166 } else if (S != "sysv")
167 error("Unknown hash style: " + S);
168 }
169
George Rimar83f406c2015-10-19 17:35:12 +0000170 for (auto *Arg : Args.filtered(OPT_undefined))
171 Config->Undefined.push_back(Arg->getValue());
172
Davide Italiano88f476b2015-10-20 00:20:20 +0000173 for (auto *Arg : Args.filtered(OPT_z)) {
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000174 StringRef S = Arg->getValue();
175 if (S == "nodelete")
Davide Italiano88f476b2015-10-20 00:20:20 +0000176 Config->ZNodelete = true;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000177 else if (S == "now")
George Rimar97aad172015-10-07 15:00:21 +0000178 Config->ZNow = true;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000179 else if (S == "origin")
Davide Italiano6e91c592015-10-21 17:09:47 +0000180 Config->ZOrigin = true;
Davide Italiano88f476b2015-10-20 00:20:20 +0000181 }
George Rimar97aad172015-10-07 15:00:21 +0000182
Igor Kudrind912ee92015-10-01 16:42:03 +0000183 for (auto *Arg : Args) {
184 switch (Arg->getOption().getID()) {
185 case OPT_l:
186 addFile(searchLibrary(Arg->getValue()));
187 break;
188 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000189 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000190 addFile(Arg->getValue());
191 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000192 case OPT_as_needed:
193 Config->AsNeeded = true;
194 break;
195 case OPT_no_as_needed:
196 Config->AsNeeded = false;
197 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000198 case OPT_Bstatic:
199 Config->Static = true;
200 break;
201 case OPT_Bdynamic:
202 Config->Static = false;
203 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000204 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000205 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000206 break;
207 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000208 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000209 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000210 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000211 }
212
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000213 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000214 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000215}
216
217template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
218 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000219 Target.reset(createTarget());
220
221 if (!Config->Shared) {
222 // Add entry symbol.
Rui Ueyama0cef6892015-10-12 15:23:54 +0000223 if (Config->Entry.empty())
224 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000225
226 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
227 StringRef S = Config->Entry;
228 if (S.getAsInteger(0, Config->EntryAddr))
229 Config->EntrySym = Symtab.addUndefined(S);
Rui Ueyamaff777682015-10-09 21:12:40 +0000230
231 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
232 // is magical and is used to produce a R_386_GOTPC relocation.
233 // The R_386_GOTPC relocation value doesn't actually depend on the
234 // symbol value, so it could use an index of STN_UNDEF which, according
235 // to the spec, means the symbol value is 0.
236 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
237 // the object file.
238 // The situation is even stranger on x86_64 where the assembly doesn't
239 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
240 // an undefined symbol in the .o files.
241 // Given that the symbol is effectively unused, we just create a dummy
242 // hidden one to avoid the undefined symbol error.
243 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
244 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000245
246 for (std::unique_ptr<InputFile> &F : Files)
247 Symtab.addFile(std::move(F));
248
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000249 for (StringRef S : Config->Undefined)
250 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000251
Rui Ueyamaee592822015-10-07 00:25:09 +0000252 if (Config->OutputFile.empty())
253 Config->OutputFile = "a.out";
254
Rui Ueyama75230392015-10-07 18:29:51 +0000255 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000256 Symtab.scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000257 if (Config->GcSections)
258 markLive<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000259 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000260}