blob: 6bc3d754bbc77ffb4fcdb2ec5313f01daefb3200 [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
Rafael Espindolaabb7b282015-09-28 12:52:21 +000063// Searches a given library from input search paths, which are filled
64// from -L command line switches. Returns a path to an existent library file.
65static std::string searchLibrary(StringRef Path) {
66 std::vector<std::string> Names;
67 if (Path[0] == ':') {
68 Names.push_back(Path.drop_front().str());
69 } else {
70 Names.push_back((Twine("lib") + Path + ".so").str());
71 Names.push_back((Twine("lib") + Path + ".a").str());
72 }
73 SmallString<128> FullPath;
74 for (StringRef Dir : Config->InputSearchPaths) {
75 for (const std::string &Name : Names) {
76 FullPath = Dir;
77 sys::path::append(FullPath, Name);
78 if (sys::fs::exists(FullPath.str()))
79 return FullPath.str().str();
80 }
81 }
82 error(Twine("Unable to find library -l") + Path);
83}
84
Michael J. Spencer84487f12015-07-24 21:03:07 +000085void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
86 // Parse command line options.
87 opt::InputArgList Args = Parser.parse(ArgsArr);
88
89 // Handle -o
90 if (auto *Arg = Args.getLastArg(OPT_output))
91 Config->OutputFile = Arg->getValue();
92 if (Config->OutputFile.empty())
93 error("-o must be specified.");
94
Rafael Espindola70107762015-09-11 18:49:42 +000095 // Handle -dynamic-linker
96 if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
97 Config->DynamicLinker = Arg->getValue();
98
Rafael Espindola2e9eac12015-09-11 21:18:56 +000099 std::vector<StringRef> RPaths;
100 for (auto *Arg : Args.filtered(OPT_rpath))
101 RPaths.push_back(Arg->getValue());
102 if (!RPaths.empty())
103 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
104
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000105 for (auto *Arg : Args.filtered(OPT_L))
106 Config->InputSearchPaths.push_back(Arg->getValue());
107
Rafael Espindola4340aad2015-09-11 22:42:45 +0000108 if (Args.hasArg(OPT_shared))
109 Config->Shared = true;
110
Davide Italiano6d328d32015-09-16 20:45:57 +0000111 if (Args.hasArg(OPT_discard_all))
112 Config->DiscardAll = true;
113
Davide Italiano5445b2de2015-09-20 21:58:12 +0000114 if (Args.hasArg(OPT_discard_locals))
115 Config->DiscardLocals = true;
116
Davide Italianod75d3b92015-09-24 15:08:23 +0000117 if (Args.hasArg(OPT_discard_none))
118 Config->DiscardNone = true;
119
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000120 if (Args.hasArg(OPT_export_dynamic))
121 Config->ExportDynamic = true;
122
Rafael Espindola551dfd82015-09-25 19:24:57 +0000123 if (Args.hasArg(OPT_noinhibit_exec))
124 Config->NoInhibitExec = true;
125
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126 // Create a list of input files.
127 std::vector<MemoryBufferRef> Inputs;
128
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000129 for (auto *Arg : Args.filtered(OPT_l, OPT_INPUT)) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000130 StringRef Path = Arg->getValue();
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000131 if (Arg->getOption().getID() == OPT_l) {
132 Inputs.push_back(openFile(searchLibrary(Path)));
133 continue;
134 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000135 Inputs.push_back(openFile(Path));
136 }
137
138 if (Inputs.empty())
139 error("no input files.");
140
141 // Create a symbol table.
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +0000142 SymbolTable Symtab;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000143
144 // Parse all input files and put all symbols to the symbol table.
145 // The symbol table will take care of name resolution.
146 for (MemoryBufferRef MB : Inputs) {
147 std::unique_ptr<InputFile> File = createFile(MB);
148 Symtab.addFile(std::move(File));
149 }
150
Michael J. Spencer84487f12015-07-24 21:03:07 +0000151 // Write the result.
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000152 const ELFFileBase *FirstObj = Symtab.getFirstELF();
153 switch (FirstObj->getELFKind()) {
Rafael Espindola905ad342015-09-02 20:43:43 +0000154 case ELF32LEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000155 writeResult<object::ELF32LE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000156 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000157 case ELF32BEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000158 writeResult<object::ELF32BE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000159 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000160 case ELF64LEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000161 writeResult<object::ELF64LE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000162 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000163 case ELF64BEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000164 writeResult<object::ELF64BE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000165 return;
166 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000167}