blob: b4395e8b8166e09359332155e692df0b3f3285fe [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000015#include "Writer.h"
16#include "llvm/ADT/STLExtras.h"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000017#include "llvm/ADT/StringExtras.h"
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000018#include "llvm/Support/FileSystem.h"
Rafael Espindolaabb7b282015-09-28 12:52:21 +000019#include "llvm/Support/Path.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
21using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000022using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
24using namespace lld;
25using namespace lld::elf2;
26
Rui Ueyama983ed2b2015-10-01 15:23:09 +000027Configuration *lld::elf2::Config;
28LinkerDriver *lld::elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000029
Rui Ueyama983ed2b2015-10-01 15:23:09 +000030void lld::elf2::link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000031 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000032 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000033 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000034 Driver = &D;
35 Driver->link(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000036}
37
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000038static void setELFType(StringRef Emul) {
39 if (Emul == "elf_i386") {
40 Config->ElfKind = ELF32LEKind;
41 Config->EMachine = EM_386;
42 return;
43 }
44 if (Emul == "elf_x86_64") {
45 Config->ElfKind = ELF64LEKind;
46 Config->EMachine = EM_X86_64;
47 return;
48 }
Simon Atanasyan456bd052015-10-08 12:13:38 +000049 if (Emul == "elf32ltsmip") {
50 Config->ElfKind = ELF32LEKind;
51 Config->EMachine = EM_MIPS;
52 return;
53 }
54 if (Emul == "elf32btsmip") {
55 Config->ElfKind = ELF32BEKind;
56 Config->EMachine = EM_MIPS;
57 return;
58 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000059 if (Emul == "elf32ppc") {
60 Config->ElfKind = ELF32BEKind;
61 Config->EMachine = EM_PPC;
62 return;
63 }
64 if (Emul == "elf64ppc") {
65 Config->ElfKind = ELF64BEKind;
66 Config->EMachine = EM_PPC64;
67 return;
68 }
69 error(Twine("Unknown emulation: ") + Emul);
70}
71
Igor Kudrin1309fc02015-09-28 15:01:59 +000072// Makes a path by concatenating Dir and File.
Igor Kudrinf03d2b42015-09-30 10:39:37 +000073// If Dir starts with '=' the result will be preceded by Sysroot,
Igor Kudrin1309fc02015-09-28 15:01:59 +000074// which can be set with --sysroot command line switch.
75static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
76 SmallString<128> Path;
Igor Kudrinf03d2b42015-09-30 10:39:37 +000077 if (Dir.startswith("="))
78 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
79 else
80 sys::path::append(Path, Dir, File);
Igor Kudrin1309fc02015-09-28 15:01:59 +000081 return Path.str().str();
82}
83
Rafael Espindolaabb7b282015-09-28 12:52:21 +000084// 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.
86static 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 Kudrind912ee92015-10-01 16:42:03 +000091 if (!Config->Static)
92 Names.push_back((Twine("lib") + Path + ".so").str());
Rafael Espindolaabb7b282015-09-28 12:52:21 +000093 Names.push_back((Twine("lib") + Path + ".a").str());
94 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +000095 for (StringRef Dir : Config->InputSearchPaths) {
96 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +000097 std::string FullPath = buildSysrootedPath(Dir, Name);
98 if (sys::fs::exists(FullPath))
99 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000100 }
101 }
102 error(Twine("Unable to find library -l") + Path);
103}
104
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000105template <template <class> class T>
106std::unique_ptr<ELFFileBase>
107LinkerDriver::createELFInputFile(MemoryBufferRef MB) {
108 std::unique_ptr<ELFFileBase> File = createELFFile<T>(MB);
109 const ELFKind ElfKind = File->getELFKind();
110 const uint16_t EMachine = File->getEMachine();
111
112 // Grab target from the first input file if wasn't set by -m option.
113 if (Config->ElfKind == ELFNoneKind) {
114 Config->ElfKind = ElfKind;
115 Config->EMachine = EMachine;
116 return File;
117 }
118 if (ElfKind == Config->ElfKind && EMachine == Config->EMachine)
119 return File;
120
121 if (const ELFFileBase *First = Symtab.getFirstELF())
122 error(MB.getBufferIdentifier() + " is incompatible with " +
123 First->getName());
124 error(MB.getBufferIdentifier() + " is incompatible with target architecture");
125}
126
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000127// Opens and parses a file. Path has to be resolved already.
128// Newly created memory buffers are owned by this driver.
129void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000130 using namespace llvm::sys::fs;
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000131 auto MBOrErr = MemoryBuffer::getFile(Path);
132 error(MBOrErr, Twine("cannot open ") + Path);
133 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
134 MemoryBufferRef MBRef = MB->getMemBufferRef();
135 OwningMBs.push_back(std::move(MB)); // take MB ownership
136
137 switch (identify_magic(MBRef.getBuffer())) {
138 case file_magic::unknown:
139 readLinkerScript(MBRef);
140 return;
141 case file_magic::archive:
142 Symtab.addFile(make_unique<ArchiveFile>(MBRef));
143 return;
144 case file_magic::elf_shared_object:
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000145 Symtab.addFile(createELFInputFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000146 return;
147 default:
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000148 Symtab.addFile(createELFInputFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000149 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000150}
151
Rui Ueyama2cac5842015-10-07 19:34:51 +0000152static StringRef
153getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
154 if (auto *Arg = Args.getLastArg(Key))
155 return Arg->getValue();
156 return Default;
157}
158
Michael J. Spencer84487f12015-07-24 21:03:07 +0000159void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000160 initSymbols();
161
Michael J. Spencer84487f12015-07-24 21:03:07 +0000162 // Parse command line options.
163 opt::InputArgList Args = Parser.parse(ArgsArr);
164
Rui Ueyama2cac5842015-10-07 19:34:51 +0000165 for (auto *Arg : Args.filtered(OPT_L))
166 Config->InputSearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000167
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000168 std::vector<StringRef> RPaths;
169 for (auto *Arg : Args.filtered(OPT_rpath))
170 RPaths.push_back(Arg->getValue());
171 if (!RPaths.empty())
172 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
173
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000174 if (auto *Arg = Args.getLastArg(OPT_m))
175 setELFType(Arg->getValue());
176
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000177 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
178 Config->DiscardAll = Args.hasArg(OPT_discard_all);
179 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
180 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000181 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000182 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
183 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000184 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000185 Config->Shared = Args.hasArg(OPT_shared);
186
Rui Ueyama2cac5842015-10-07 19:34:51 +0000187 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
188 Config->Entry = getString(Args, OPT_entry);
189 Config->Fini = getString(Args, OPT_fini, "_fini");
190 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000191 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000192 Config->SoName = getString(Args, OPT_soname);
193 Config->Sysroot = getString(Args, OPT_sysroot);
194
Rui Ueyama58d7d702015-10-07 18:22:46 +0000195 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000196 if (Arg->getValue() == StringRef("now"))
197 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000198
Igor Kudrind912ee92015-10-01 16:42:03 +0000199 for (auto *Arg : Args) {
200 switch (Arg->getOption().getID()) {
201 case OPT_l:
202 addFile(searchLibrary(Arg->getValue()));
203 break;
204 case OPT_INPUT:
205 addFile(Arg->getValue());
206 break;
207 case OPT_Bstatic:
208 Config->Static = true;
209 break;
210 case OPT_Bdynamic:
211 Config->Static = false;
212 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000213 case OPT_whole_archive:
214 Config->WholeArchive = true;
215 break;
216 case OPT_no_whole_archive:
217 Config->WholeArchive = false;
218 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000219 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000220 }
221
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000222 if (Symtab.getObjectFiles().empty())
223 error("no input files.");
224
Denis Protivensky22220d52015-10-05 09:43:57 +0000225 for (auto *Arg : Args.filtered(OPT_undefined))
226 Symtab.addUndefinedSym(Arg->getValue());
227
Rui Ueyamaee592822015-10-07 00:25:09 +0000228 if (Config->OutputFile.empty())
229 Config->OutputFile = "a.out";
230
Rui Ueyama75230392015-10-07 18:29:51 +0000231 // Write the result to the file.
232 writeResult(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000233}