blob: 307bb08cb683fd3197565b34b4e5fff0011d5531 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
//===- Driver.cpp ---------------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Driver.h"
#include "Config.h"
#include "Error.h"
#include "InputFiles.h"
#include "SymbolTable.h"
#include "Writer.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FileSystem.h"
using namespace llvm;
using namespace lld;
using namespace lld::elf2;
namespace lld {
namespace elf2 {
Configuration *Config;
void link(ArrayRef<const char *> Args) {
Configuration C;
Config = &C;
LinkerDriver().link(Args.slice(1));
}
}
}
// Opens a file. Path has to be resolved already.
// Newly created memory buffers are owned by this driver.
MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
error(MBOrErr, Twine("cannot open ") + Path);
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
MemoryBufferRef MBRef = MB->getMemBufferRef();
OwningMBs.push_back(std::move(MB)); // take ownership
return MBRef;
}
static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
using namespace llvm::sys::fs;
file_magic Magic = identify_magic(MB.getBuffer());
if (Magic == file_magic::archive)
return make_unique<ArchiveFile>(MB);
if (Magic == file_magic::elf_shared_object)
return createELFFile<SharedFile>(MB);
return createELFFile<ObjectFile>(MB);
}
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
// Parse command line options.
opt::InputArgList Args = Parser.parse(ArgsArr);
// Handle -o
if (auto *Arg = Args.getLastArg(OPT_output))
Config->OutputFile = Arg->getValue();
if (Config->OutputFile.empty())
error("-o must be specified.");
// Handle -dynamic-linker
if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
Config->DynamicLinker = Arg->getValue();
std::vector<StringRef> RPaths;
for (auto *Arg : Args.filtered(OPT_rpath))
RPaths.push_back(Arg->getValue());
if (!RPaths.empty())
Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
if (Args.hasArg(OPT_shared))
Config->Shared = true;
if (Args.hasArg(OPT_discard_all))
Config->DiscardAll = true;
if (Args.hasArg(OPT_discard_locals))
Config->DiscardLocals = true;
if (Args.hasArg(OPT_discard_none))
Config->DiscardNone = true;
if (Args.hasArg(OPT_export_dynamic))
Config->ExportDynamic = true;
if (Args.hasArg(OPT_noinhibit_exec))
Config->NoInhibitExec = true;
// Create a list of input files.
std::vector<MemoryBufferRef> Inputs;
for (auto *Arg : Args.filtered(OPT_INPUT)) {
StringRef Path = Arg->getValue();
Inputs.push_back(openFile(Path));
}
if (Inputs.empty())
error("no input files.");
// Create a symbol table.
SymbolTable Symtab;
// Parse all input files and put all symbols to the symbol table.
// The symbol table will take care of name resolution.
for (MemoryBufferRef MB : Inputs) {
std::unique_ptr<InputFile> File = createFile(MB);
Symtab.addFile(std::move(File));
}
// Write the result.
const ELFFileBase *FirstObj = Symtab.getFirstELF();
switch (FirstObj->getELFKind()) {
case ELF32LEKind:
writeResult<object::ELF32LE>(&Symtab);
return;
case ELF32BEKind:
writeResult<object::ELF32BE>(&Symtab);
return;
case ELF64LEKind:
writeResult<object::ELF64LE>(&Symtab);
return;
case ELF64BEKind:
writeResult<object::ELF64BE>(&Symtab);
return;
}
}
|