Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- 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 Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 10 | #include "Driver.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 11 | #include "Config.h" |
| 12 | #include "Error.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
| 14 | #include "SymbolTable.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 15 | #include "Writer.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | using namespace lld; |
| 21 | using namespace lld::elf2; |
| 22 | |
| 23 | namespace lld { |
| 24 | namespace elf2 { |
| 25 | Configuration *Config; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | |
| 27 | void link(ArrayRef<const char *> Args) { |
| 28 | auto C = make_unique<Configuration>(); |
| 29 | Config = C.get(); |
Rui Ueyama | 880632c | 2015-08-11 21:45:55 +0000 | [diff] [blame^] | 30 | LinkerDriver().link(Args.slice(1)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | // Opens a file. Path has to be resolved already. |
| 37 | // Newly created memory buffers are owned by this driver. |
| 38 | MemoryBufferRef LinkerDriver::openFile(StringRef Path) { |
| 39 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); |
| 40 | error(MBOrErr, Twine("cannot open ") + Path); |
| 41 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
| 42 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 43 | OwningMBs.push_back(std::move(MB)); // take ownership |
| 44 | return MBRef; |
| 45 | } |
| 46 | |
| 47 | static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) { |
Rafael Espindola | aefd5c1 | 2015-08-04 15:45:54 +0000 | [diff] [blame] | 48 | std::pair<unsigned char, unsigned char> Type = |
| 49 | object::getElfArchType(MB.getBuffer()); |
| 50 | if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB) |
| 51 | error("Invalid data encoding"); |
| 52 | |
| 53 | if (Type.first == ELF::ELFCLASS32) { |
| 54 | if (Type.second == ELF::ELFDATA2LSB) |
| 55 | return make_unique<ObjectFile<object::ELF32LE>>(MB); |
| 56 | return make_unique<ObjectFile<object::ELF32BE>>(MB); |
| 57 | } |
| 58 | if (Type.first == ELF::ELFCLASS64) { |
| 59 | if (Type.second == ELF::ELFDATA2LSB) |
| 60 | return make_unique<ObjectFile<object::ELF64LE>>(MB); |
| 61 | return make_unique<ObjectFile<object::ELF64BE>>(MB); |
| 62 | } |
| 63 | error("Invalid file class"); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
| 67 | // Parse command line options. |
| 68 | opt::InputArgList Args = Parser.parse(ArgsArr); |
| 69 | |
| 70 | // Handle -o |
| 71 | if (auto *Arg = Args.getLastArg(OPT_output)) |
| 72 | Config->OutputFile = Arg->getValue(); |
| 73 | if (Config->OutputFile.empty()) |
| 74 | error("-o must be specified."); |
| 75 | |
| 76 | // Create a list of input files. |
| 77 | std::vector<MemoryBufferRef> Inputs; |
| 78 | |
| 79 | for (auto *Arg : Args.filtered(OPT_INPUT)) { |
| 80 | StringRef Path = Arg->getValue(); |
| 81 | Inputs.push_back(openFile(Path)); |
| 82 | } |
| 83 | |
| 84 | if (Inputs.empty()) |
| 85 | error("no input files."); |
| 86 | |
| 87 | // Create a symbol table. |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 88 | SymbolTable Symtab; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 89 | |
| 90 | // Parse all input files and put all symbols to the symbol table. |
| 91 | // The symbol table will take care of name resolution. |
| 92 | for (MemoryBufferRef MB : Inputs) { |
| 93 | std::unique_ptr<InputFile> File = createFile(MB); |
| 94 | Symtab.addFile(std::move(File)); |
| 95 | } |
| 96 | |
| 97 | // Make sure we have resolved all symbols. |
| 98 | Symtab.reportRemainingUndefines(); |
| 99 | |
| 100 | // Write the result. |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 101 | ObjectFileBase &FirstObj = *Symtab.ObjectFiles[0]; |
| 102 | switch (FirstObj.kind()) { |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 103 | case InputFile::Object32LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 104 | writeResult<object::ELF32LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 105 | return; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 106 | case InputFile::Object32BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 107 | writeResult<object::ELF32BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 108 | return; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 109 | case InputFile::Object64LEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 110 | writeResult<object::ELF64LE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 111 | return; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 112 | case InputFile::Object64BEKind: |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 113 | writeResult<object::ELF64BE>(&Symtab); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 114 | return; |
| 115 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 116 | } |