blob: d16db1db1df6d018f573cdbb8c6226d9bda2bca1 [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;
22
23using namespace lld;
24using namespace lld::elf2;
25
26namespace lld {
27namespace elf2 {
28Configuration *Config;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30void link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000031 Configuration C;
32 Config = &C;
Rui Ueyama880632c2015-08-11 21:45:55 +000033 LinkerDriver().link(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000034}
35
Michael J. Spencer84487f12015-07-24 21:03:07 +000036}
37}
38
39// Opens a file. Path has to be resolved already.
40// Newly created memory buffers are owned by this driver.
41MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
42 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
43 error(MBOrErr, Twine("cannot open ") + Path);
44 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
45 MemoryBufferRef MBRef = MB->getMemBufferRef();
46 OwningMBs.push_back(std::move(MB)); // take ownership
47 return MBRef;
48}
49
50static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000051 using namespace llvm::sys::fs;
52 file_magic Magic = identify_magic(MB.getBuffer());
53
Michael J. Spencer1b348a62015-09-04 22:28:10 +000054 if (Magic == file_magic::archive)
55 return make_unique<ArchiveFile>(MB);
Rafael Espindolaaefd5c12015-08-04 15:45:54 +000056
Michael J. Spencer1b348a62015-09-04 22:28:10 +000057 if (Magic == file_magic::elf_shared_object)
58 return createELFFile<SharedFile>(MB);
59
60 return createELFFile<ObjectFile>(MB);
Michael J. Spencer84487f12015-07-24 21:03:07 +000061}
62
Igor Kudrin1309fc02015-09-28 15:01:59 +000063// Makes a path by concatenating Dir and File.
64// If Dir starts with "=" the result will be preceded by SysRoot,
65// which can be set with --sysroot command line switch.
66static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
67 SmallString<128> Path;
68 if (Dir.startswith("=")) {
69 Path.assign(Config->Sysroot);
70 sys::path::append(Path, Dir.substr(1), File);
71 } else {
72 Path.assign(Dir);
73 sys::path::append(Path, File);
74 }
75 return Path.str().str();
76}
77
Rafael Espindolaabb7b282015-09-28 12:52:21 +000078// Searches a given library from input search paths, which are filled
79// from -L command line switches. Returns a path to an existent library file.
80static std::string searchLibrary(StringRef Path) {
81 std::vector<std::string> Names;
82 if (Path[0] == ':') {
83 Names.push_back(Path.drop_front().str());
84 } else {
85 Names.push_back((Twine("lib") + Path + ".so").str());
86 Names.push_back((Twine("lib") + Path + ".a").str());
87 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +000088 for (StringRef Dir : Config->InputSearchPaths) {
89 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +000090 std::string FullPath = buildSysrootedPath(Dir, Name);
91 if (sys::fs::exists(FullPath))
92 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +000093 }
94 }
95 error(Twine("Unable to find library -l") + Path);
96}
97
Michael J. Spencer84487f12015-07-24 21:03:07 +000098void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
99 // Parse command line options.
100 opt::InputArgList Args = Parser.parse(ArgsArr);
101
102 // Handle -o
103 if (auto *Arg = Args.getLastArg(OPT_output))
104 Config->OutputFile = Arg->getValue();
105 if (Config->OutputFile.empty())
106 error("-o must be specified.");
107
Rafael Espindola70107762015-09-11 18:49:42 +0000108 // Handle -dynamic-linker
109 if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
110 Config->DynamicLinker = Arg->getValue();
111
Igor Kudrin1309fc02015-09-28 15:01:59 +0000112 if (auto *Arg = Args.getLastArg(OPT_sysroot))
113 Config->Sysroot = Arg->getValue();
114
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000115 std::vector<StringRef> RPaths;
116 for (auto *Arg : Args.filtered(OPT_rpath))
117 RPaths.push_back(Arg->getValue());
118 if (!RPaths.empty())
119 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
120
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000121 for (auto *Arg : Args.filtered(OPT_L))
122 Config->InputSearchPaths.push_back(Arg->getValue());
123
Rafael Espindola4340aad2015-09-11 22:42:45 +0000124 if (Args.hasArg(OPT_shared))
125 Config->Shared = true;
126
Davide Italiano6d328d32015-09-16 20:45:57 +0000127 if (Args.hasArg(OPT_discard_all))
128 Config->DiscardAll = true;
129
Davide Italiano5445b2de2015-09-20 21:58:12 +0000130 if (Args.hasArg(OPT_discard_locals))
131 Config->DiscardLocals = true;
132
Davide Italianod75d3b92015-09-24 15:08:23 +0000133 if (Args.hasArg(OPT_discard_none))
134 Config->DiscardNone = true;
135
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000136 if (Args.hasArg(OPT_export_dynamic))
137 Config->ExportDynamic = true;
138
Rafael Espindola551dfd82015-09-25 19:24:57 +0000139 if (Args.hasArg(OPT_noinhibit_exec))
140 Config->NoInhibitExec = true;
141
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000142 if (Args.hasArg(OPT_allow_multiple_definition))
143 Config->AllowMultipleDefinition = true;
144
Rui Ueyama9d4c6d72015-09-29 16:40:13 +0000145 if (auto *Arg = Args.getLastArg(OPT_entry))
146 Config->Entry = Arg->getValue();
147
Michael J. Spencer84487f12015-07-24 21:03:07 +0000148 // Create a list of input files.
149 std::vector<MemoryBufferRef> Inputs;
150
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000151 for (auto *Arg : Args.filtered(OPT_l, OPT_INPUT)) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000152 StringRef Path = Arg->getValue();
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000153 if (Arg->getOption().getID() == OPT_l) {
154 Inputs.push_back(openFile(searchLibrary(Path)));
155 continue;
156 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000157 Inputs.push_back(openFile(Path));
158 }
159
160 if (Inputs.empty())
161 error("no input files.");
162
163 // Create a symbol table.
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +0000164 SymbolTable Symtab;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000165
166 // Parse all input files and put all symbols to the symbol table.
167 // The symbol table will take care of name resolution.
168 for (MemoryBufferRef MB : Inputs) {
169 std::unique_ptr<InputFile> File = createFile(MB);
170 Symtab.addFile(std::move(File));
171 }
172
Michael J. Spencer84487f12015-07-24 21:03:07 +0000173 // Write the result.
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000174 const ELFFileBase *FirstObj = Symtab.getFirstELF();
175 switch (FirstObj->getELFKind()) {
Rafael Espindola905ad342015-09-02 20:43:43 +0000176 case ELF32LEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000177 writeResult<object::ELF32LE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000178 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000179 case ELF32BEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000180 writeResult<object::ELF32BE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000181 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000182 case ELF64LEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000183 writeResult<object::ELF64LE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000184 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000185 case ELF64BEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000186 writeResult<object::ELF64BE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000187 return;
188 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000189}