blob: a2d7022603aae561b2c81e5144ac55c3c1b25dd8 [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000019
20using namespace llvm;
21
22using namespace lld;
23using namespace lld::elf2;
24
25namespace lld {
26namespace elf2 {
27Configuration *Config;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29void link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000030 Configuration C;
31 Config = &C;
Rui Ueyama880632c2015-08-11 21:45:55 +000032 LinkerDriver().link(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000033}
34
Michael J. Spencer84487f12015-07-24 21:03:07 +000035}
36}
37
38// Opens a file. Path has to be resolved already.
39// Newly created memory buffers are owned by this driver.
40MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
41 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
42 error(MBOrErr, Twine("cannot open ") + Path);
43 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
44 MemoryBufferRef MBRef = MB->getMemBufferRef();
45 OwningMBs.push_back(std::move(MB)); // take ownership
46 return MBRef;
47}
48
49static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000050 using namespace llvm::sys::fs;
51 file_magic Magic = identify_magic(MB.getBuffer());
52
Michael J. Spencer1b348a62015-09-04 22:28:10 +000053 if (Magic == file_magic::archive)
54 return make_unique<ArchiveFile>(MB);
Rafael Espindolaaefd5c12015-08-04 15:45:54 +000055
Michael J. Spencer1b348a62015-09-04 22:28:10 +000056 if (Magic == file_magic::elf_shared_object)
57 return createELFFile<SharedFile>(MB);
58
59 return createELFFile<ObjectFile>(MB);
Michael J. Spencer84487f12015-07-24 21:03:07 +000060}
61
62void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
63 // Parse command line options.
64 opt::InputArgList Args = Parser.parse(ArgsArr);
65
66 // Handle -o
67 if (auto *Arg = Args.getLastArg(OPT_output))
68 Config->OutputFile = Arg->getValue();
69 if (Config->OutputFile.empty())
70 error("-o must be specified.");
71
Rafael Espindola70107762015-09-11 18:49:42 +000072 // Handle -dynamic-linker
73 if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
74 Config->DynamicLinker = Arg->getValue();
75
Rafael Espindola2e9eac12015-09-11 21:18:56 +000076 std::vector<StringRef> RPaths;
77 for (auto *Arg : Args.filtered(OPT_rpath))
78 RPaths.push_back(Arg->getValue());
79 if (!RPaths.empty())
80 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
81
Michael J. Spencer84487f12015-07-24 21:03:07 +000082 // Create a list of input files.
83 std::vector<MemoryBufferRef> Inputs;
84
85 for (auto *Arg : Args.filtered(OPT_INPUT)) {
86 StringRef Path = Arg->getValue();
87 Inputs.push_back(openFile(Path));
88 }
89
90 if (Inputs.empty())
91 error("no input files.");
92
93 // Create a symbol table.
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000094 SymbolTable Symtab;
Michael J. Spencer84487f12015-07-24 21:03:07 +000095
96 // Parse all input files and put all symbols to the symbol table.
97 // The symbol table will take care of name resolution.
98 for (MemoryBufferRef MB : Inputs) {
99 std::unique_ptr<InputFile> File = createFile(MB);
100 Symtab.addFile(std::move(File));
101 }
102
Michael J. Spencer84487f12015-07-24 21:03:07 +0000103 // Write the result.
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000104 const ELFFileBase *FirstObj = Symtab.getFirstELF();
105 switch (FirstObj->getELFKind()) {
Rafael Espindola905ad342015-09-02 20:43:43 +0000106 case ELF32LEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000107 writeResult<object::ELF32LE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000108 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000109 case ELF32BEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000110 writeResult<object::ELF32BE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000111 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000112 case ELF64LEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000113 writeResult<object::ELF64LE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000114 return;
Rafael Espindola905ad342015-09-02 20:43:43 +0000115 case ELF64BEKind:
Rui Ueyamacb8474ed2015-08-05 23:51:50 +0000116 writeResult<object::ELF64BE>(&Symtab);
Rafael Espindola4b7c2fc2015-08-05 15:08:40 +0000117 return;
118 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000119}