blob: 0774252fc607591405572b3842167a4a5d4149f1 [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 }
49 if (Emul == "elf32ppc") {
50 Config->ElfKind = ELF32BEKind;
51 Config->EMachine = EM_PPC;
52 return;
53 }
54 if (Emul == "elf64ppc") {
55 Config->ElfKind = ELF64BEKind;
56 Config->EMachine = EM_PPC64;
57 return;
58 }
59 error(Twine("Unknown emulation: ") + Emul);
60}
61
Igor Kudrin1309fc02015-09-28 15:01:59 +000062// Makes a path by concatenating Dir and File.
Igor Kudrinf03d2b42015-09-30 10:39:37 +000063// If Dir starts with '=' the result will be preceded by Sysroot,
Igor Kudrin1309fc02015-09-28 15:01:59 +000064// which can be set with --sysroot command line switch.
65static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
66 SmallString<128> Path;
Igor Kudrinf03d2b42015-09-30 10:39:37 +000067 if (Dir.startswith("="))
68 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
69 else
70 sys::path::append(Path, Dir, File);
Igor Kudrin1309fc02015-09-28 15:01:59 +000071 return Path.str().str();
72}
73
Rafael Espindolaabb7b282015-09-28 12:52:21 +000074// Searches a given library from input search paths, which are filled
75// from -L command line switches. Returns a path to an existent library file.
76static std::string searchLibrary(StringRef Path) {
77 std::vector<std::string> Names;
78 if (Path[0] == ':') {
79 Names.push_back(Path.drop_front().str());
80 } else {
Igor Kudrind912ee92015-10-01 16:42:03 +000081 if (!Config->Static)
82 Names.push_back((Twine("lib") + Path + ".so").str());
Rafael Espindolaabb7b282015-09-28 12:52:21 +000083 Names.push_back((Twine("lib") + Path + ".a").str());
84 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +000085 for (StringRef Dir : Config->InputSearchPaths) {
86 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +000087 std::string FullPath = buildSysrootedPath(Dir, Name);
88 if (sys::fs::exists(FullPath))
89 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +000090 }
91 }
92 error(Twine("Unable to find library -l") + Path);
93}
94
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000095template <template <class> class T>
96std::unique_ptr<ELFFileBase>
97LinkerDriver::createELFInputFile(MemoryBufferRef MB) {
98 std::unique_ptr<ELFFileBase> File = createELFFile<T>(MB);
99 const ELFKind ElfKind = File->getELFKind();
100 const uint16_t EMachine = File->getEMachine();
101
102 // Grab target from the first input file if wasn't set by -m option.
103 if (Config->ElfKind == ELFNoneKind) {
104 Config->ElfKind = ElfKind;
105 Config->EMachine = EMachine;
106 return File;
107 }
108 if (ElfKind == Config->ElfKind && EMachine == Config->EMachine)
109 return File;
110
111 if (const ELFFileBase *First = Symtab.getFirstELF())
112 error(MB.getBufferIdentifier() + " is incompatible with " +
113 First->getName());
114 error(MB.getBufferIdentifier() + " is incompatible with target architecture");
115}
116
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000117// Opens and parses a file. Path has to be resolved already.
118// Newly created memory buffers are owned by this driver.
119void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000120 using namespace llvm::sys::fs;
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000121 auto MBOrErr = MemoryBuffer::getFile(Path);
122 error(MBOrErr, Twine("cannot open ") + Path);
123 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
124 MemoryBufferRef MBRef = MB->getMemBufferRef();
125 OwningMBs.push_back(std::move(MB)); // take MB ownership
126
127 switch (identify_magic(MBRef.getBuffer())) {
128 case file_magic::unknown:
129 readLinkerScript(MBRef);
130 return;
131 case file_magic::archive:
132 Symtab.addFile(make_unique<ArchiveFile>(MBRef));
133 return;
134 case file_magic::elf_shared_object:
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000135 Symtab.addFile(createELFInputFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000136 return;
137 default:
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000138 Symtab.addFile(createELFInputFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000139 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000140}
141
Michael J. Spencer84487f12015-07-24 21:03:07 +0000142void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
143 // Parse command line options.
144 opt::InputArgList Args = Parser.parse(ArgsArr);
145
Michael J. Spencer84487f12015-07-24 21:03:07 +0000146 if (auto *Arg = Args.getLastArg(OPT_output))
147 Config->OutputFile = Arg->getValue();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000148
Rafael Espindola70107762015-09-11 18:49:42 +0000149 if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
150 Config->DynamicLinker = Arg->getValue();
151
Igor Kudrin1309fc02015-09-28 15:01:59 +0000152 if (auto *Arg = Args.getLastArg(OPT_sysroot))
153 Config->Sysroot = Arg->getValue();
154
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000155 std::vector<StringRef> RPaths;
156 for (auto *Arg : Args.filtered(OPT_rpath))
157 RPaths.push_back(Arg->getValue());
158 if (!RPaths.empty())
159 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
160
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000161 for (auto *Arg : Args.filtered(OPT_L))
162 Config->InputSearchPaths.push_back(Arg->getValue());
163
Rui Ueyama9d4c6d72015-09-29 16:40:13 +0000164 if (auto *Arg = Args.getLastArg(OPT_entry))
165 Config->Entry = Arg->getValue();
166
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000167 if (auto *Arg = Args.getLastArg(OPT_fini))
168 Config->Fini = Arg->getValue();
169
170 if (auto *Arg = Args.getLastArg(OPT_init))
171 Config->Init = Arg->getValue();
172
Rui Ueyama7de3f372015-10-01 19:36:04 +0000173 if (auto *Arg = Args.getLastArg(OPT_soname))
174 Config->SoName = Arg->getValue();
175
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000176 if (auto *Arg = Args.getLastArg(OPT_m))
177 setELFType(Arg->getValue());
178
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000179 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
180 Config->DiscardAll = Args.hasArg(OPT_discard_all);
181 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
182 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000183 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000184 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
185 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000186 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000187 Config->Shared = Args.hasArg(OPT_shared);
188
Rui Ueyama58d7d702015-10-07 18:22:46 +0000189 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000190 if (Arg->getValue() == StringRef("now"))
191 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000192
Igor Kudrind912ee92015-10-01 16:42:03 +0000193 for (auto *Arg : Args) {
194 switch (Arg->getOption().getID()) {
195 case OPT_l:
196 addFile(searchLibrary(Arg->getValue()));
197 break;
198 case OPT_INPUT:
199 addFile(Arg->getValue());
200 break;
201 case OPT_Bstatic:
202 Config->Static = true;
203 break;
204 case OPT_Bdynamic:
205 Config->Static = false;
206 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000207 case OPT_whole_archive:
208 Config->WholeArchive = true;
209 break;
210 case OPT_no_whole_archive:
211 Config->WholeArchive = false;
212 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000213 default:
214 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000215 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000216 }
217
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000218 if (Symtab.getObjectFiles().empty())
219 error("no input files.");
220
Denis Protivensky22220d52015-10-05 09:43:57 +0000221 for (auto *Arg : Args.filtered(OPT_undefined))
222 Symtab.addUndefinedSym(Arg->getValue());
223
Rui Ueyamaee592822015-10-07 00:25:09 +0000224 if (Config->OutputFile.empty())
225 Config->OutputFile = "a.out";
226
Rui Ueyama75230392015-10-07 18:29:51 +0000227 // Write the result to the file.
228 writeResult(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000229}