blob: b9f55613f55839bc00557a4414ecdb5200b10078 [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 Ueyamaff777682015-10-09 21:12:40 +000057static TargetInfo *createTarget() {
58 switch (Config->EMachine) {
59 case EM_386:
60 return new X86TargetInfo();
61 case EM_AARCH64:
62 return new AArch64TargetInfo();
63 case EM_ARM:
64 return new ARMTargetInfo();
65 case EM_MIPS:
66 return new MipsTargetInfo();
67 case EM_PPC:
68 return new PPCTargetInfo();
69 case EM_PPC64:
70 return new PPC64TargetInfo();
71 case EM_X86_64:
72 return new X86_64TargetInfo();
73 }
74 error("Unknown target machine");
75}
76
Rui Ueyama983ed2b2015-10-01 15:23:09 +000077// Opens and parses a file. Path has to be resolved already.
78// Newly created memory buffers are owned by this driver.
79void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000080 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +000081 if (Config->Verbose)
82 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +000083 auto MBOrErr = MemoryBuffer::getFile(Path);
84 error(MBOrErr, Twine("cannot open ") + Path);
85 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
86 MemoryBufferRef MBRef = MB->getMemBufferRef();
87 OwningMBs.push_back(std::move(MB)); // take MB ownership
88
89 switch (identify_magic(MBRef.getBuffer())) {
90 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000091 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000092 return;
93 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000094 if (WholeArchive) {
95 auto File = make_unique<ArchiveFile>(MBRef);
96 for (MemoryBufferRef &MB : File->getMembers())
97 Files.push_back(createELFFile<ObjectFile>(MB));
98 OwningArchives.emplace_back(std::move(File));
99 return;
100 }
101 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000102 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000103 case file_magic::elf_shared_object:
104 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000105 return;
106 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000107 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000108 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000109}
110
Rui Ueyama2cac5842015-10-07 19:34:51 +0000111static StringRef
112getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
113 if (auto *Arg = Args.getLastArg(Key))
114 return Arg->getValue();
115 return Default;
116}
117
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000118void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000119 initSymbols();
120
Rui Ueyamac6e1b972015-10-11 18:19:01 +0000121 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000122 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000123
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000124 switch (Config->ElfKind) {
125 case ELF32LEKind:
126 link<ELF32LE>(Args);
127 return;
128 case ELF32BEKind:
129 link<ELF32BE>(Args);
130 return;
131 case ELF64LEKind:
132 link<ELF64LE>(Args);
133 return;
134 case ELF64BEKind:
135 link<ELF64BE>(Args);
136 return;
137 default:
138 error("-m or at least a .o file required");
139 }
140}
141
142void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000143 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000144 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000145
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000146 std::vector<StringRef> RPaths;
147 for (auto *Arg : Args.filtered(OPT_rpath))
148 RPaths.push_back(Arg->getValue());
149 if (!RPaths.empty())
150 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
151
Rui Ueyamacacf9672015-10-11 02:22:31 +0000152 if (auto *Arg = Args.getLastArg(OPT_m)) {
153 std::pair<ELFKind, uint16_t> P = parseEmulation(Arg->getValue());
154 Config->ElfKind = P.first;
155 Config->EMachine = P.second;
156 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000157
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000158 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
159 Config->DiscardAll = Args.hasArg(OPT_discard_all);
160 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
161 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000162 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000163 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
164 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000165 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000166 Config->Shared = Args.hasArg(OPT_shared);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000167 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000168
Rui Ueyama2cac5842015-10-07 19:34:51 +0000169 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
170 Config->Entry = getString(Args, OPT_entry);
171 Config->Fini = getString(Args, OPT_fini, "_fini");
172 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000173 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000174 Config->SoName = getString(Args, OPT_soname);
175 Config->Sysroot = getString(Args, OPT_sysroot);
176
Rui Ueyama58d7d702015-10-07 18:22:46 +0000177 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000178 if (Arg->getValue() == StringRef("now"))
179 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000180
Igor Kudrind912ee92015-10-01 16:42:03 +0000181 for (auto *Arg : Args) {
182 switch (Arg->getOption().getID()) {
183 case OPT_l:
184 addFile(searchLibrary(Arg->getValue()));
185 break;
186 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000187 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000188 addFile(Arg->getValue());
189 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000190 case OPT_as_needed:
191 Config->AsNeeded = true;
192 break;
193 case OPT_no_as_needed:
194 Config->AsNeeded = false;
195 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000196 case OPT_Bstatic:
197 Config->Static = true;
198 break;
199 case OPT_Bdynamic:
200 Config->Static = false;
201 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000202 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000203 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000204 break;
205 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000206 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000207 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000208 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000209 }
210
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000211 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000212 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000213}
214
215template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
216 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000217 Target.reset(createTarget());
218
219 if (!Config->Shared) {
220 // Add entry symbol.
Rui Ueyama0cef6892015-10-12 15:23:54 +0000221 if (Config->Entry.empty())
222 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
223 Config->EntrySym = Symtab.addUndefined(Config->Entry);
Rui Ueyamaff777682015-10-09 21:12:40 +0000224
225 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
226 // is magical and is used to produce a R_386_GOTPC relocation.
227 // The R_386_GOTPC relocation value doesn't actually depend on the
228 // symbol value, so it could use an index of STN_UNDEF which, according
229 // to the spec, means the symbol value is 0.
230 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
231 // the object file.
232 // The situation is even stranger on x86_64 where the assembly doesn't
233 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
234 // an undefined symbol in the .o files.
235 // Given that the symbol is effectively unused, we just create a dummy
236 // hidden one to avoid the undefined symbol error.
237 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
238 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000239
240 for (std::unique_ptr<InputFile> &F : Files)
241 Symtab.addFile(std::move(F));
242
Denis Protivensky22220d52015-10-05 09:43:57 +0000243 for (auto *Arg : Args.filtered(OPT_undefined))
Rui Ueyamaff777682015-10-09 21:12:40 +0000244 Symtab.addUndefinedOpt(Arg->getValue());
Denis Protivensky22220d52015-10-05 09:43:57 +0000245
Rui Ueyamaee592822015-10-07 00:25:09 +0000246 if (Config->OutputFile.empty())
247 Config->OutputFile = "a.out";
248
Rui Ueyama75230392015-10-07 18:29:51 +0000249 // Write the result to the file.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000250 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000251}