| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 1 | //===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===// |
| Michael J. Spencer | d68d619 | 2013-01-22 02:15:30 +0000 | [diff] [blame] | 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 | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 10 | #include "lld/Core/LinkingContext.h" |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 11 | #include "lld/Core/Resolver.h" |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 12 | #include "lld/ReaderWriter/Writer.h" |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 13 | #include "lld/ReaderWriter/Simple.h" |
| Michael J. Spencer | d68d619 | 2013-01-22 02:15:30 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm/ADT/Triple.h" |
| 16 | |
| 17 | namespace lld { |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 18 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 19 | LinkingContext::LinkingContext() |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 20 | : _deadStrip(false), _globalsAreDeadStripRoots(false), |
| Shankar Easwaran | eeee23e | 2013-04-11 02:56:30 +0000 | [diff] [blame] | 21 | _searchArchivesToOverrideTentativeDefinitions(false), |
| 22 | _searchSharedLibrariesToOverrideTentativeDefinitions(false), |
| 23 | _warnIfCoalesableAtomsHaveDifferentCanBeNull(false), |
| 24 | _warnIfCoalesableAtomsHaveDifferentLoadName(false), |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 25 | _printRemainingUndefines(true), _allowRemainingUndefines(false), |
| 26 | _logInputFiles(false), _allowShlibUndefines(false), |
| 27 | _outputFileType(OutputFileType::Default), _currentInputElement(nullptr) {} |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 28 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 29 | LinkingContext::~LinkingContext() {} |
| Michael J. Spencer | d68d619 | 2013-01-22 02:15:30 +0000 | [diff] [blame] | 30 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 31 | bool LinkingContext::validate(raw_ostream &diagnostics) { |
| Rafael Espindola | c1b3268 | 2013-06-11 12:36:05 +0000 | [diff] [blame] | 32 | _yamlReader = createReaderYAML(*this); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 33 | _nativeReader = createReaderNative(*this); |
| Rafael Espindola | c1b3268 | 2013-06-11 12:36:05 +0000 | [diff] [blame] | 34 | return validateImpl(diagnostics); |
| 35 | } |
| 36 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 37 | error_code LinkingContext::writeFile(const File &linkedFile) const { |
| 38 | return this->writer().writeFile(linkedFile, _outputPath); |
| Michael J. Spencer | d68d619 | 2013-01-22 02:15:30 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 41 | bool LinkingContext::createImplicitFiles( |
| 42 | std::vector<std::unique_ptr<File> > &result) const { |
| 43 | return this->writer().createImplicitFiles(result); |
| Michael J. Spencer | d68d619 | 2013-01-22 02:15:30 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 46 | std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const { |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 47 | if (entrySymbolName().empty()) |
| 48 | return nullptr; |
| 49 | std::unique_ptr<SimpleFile> entryFile( |
| 50 | new SimpleFile(*this, "command line option -entry")); |
| 51 | entryFile->addAtom( |
| 52 | *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName()))); |
| 53 | return std::move(entryFile); |
| 54 | } |
| 55 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 56 | std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const { |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 57 | if (_initialUndefinedSymbols.empty()) |
| 58 | return nullptr; |
| 59 | std::unique_ptr<SimpleFile> undefinedSymFile( |
| 60 | new SimpleFile(*this, "command line option -u")); |
| 61 | for (auto undefSymStr : _initialUndefinedSymbols) |
| 62 | undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom( |
| 63 | *undefinedSymFile, undefSymStr))); |
| 64 | return std::move(undefinedSymFile); |
| 65 | } |
| 66 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 67 | bool LinkingContext::createInternalFiles( |
| 68 | std::vector<std::unique_ptr<File> > &result) const { |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 69 | std::unique_ptr<File> internalFile; |
| 70 | internalFile = createEntrySymbolFile(); |
| 71 | if (internalFile) |
| 72 | result.push_back(std::move(internalFile)); |
| 73 | internalFile = createUndefinedSymbolFile(); |
| 74 | if (internalFile) |
| 75 | result.push_back(std::move(internalFile)); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
| Rui Ueyama | 8b8e521 | 2013-10-08 03:59:45 +0000 | [diff] [blame^] | 79 | void LinkingContext::setResolverState(uint32_t state) const { |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 80 | _currentInputElement->setResolverState(state); |
| 81 | } |
| 82 | |
| 83 | ErrorOr<File &> LinkingContext::nextFile() const { |
| Rui Ueyama | 0945468 | 2013-10-08 00:43:53 +0000 | [diff] [blame] | 84 | // When nextFile() is called for the first time, _currentInputElement is not |
| 85 | // initialized. Initialize it with the first element of the input graph. |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 86 | if (_currentInputElement == nullptr) { |
| 87 | ErrorOr<InputElement *> elem = inputGraph().getNextInputElement(); |
| 88 | if (error_code(elem) == input_graph_error::no_more_elements) |
| 89 | return make_error_code(input_graph_error::no_more_files); |
| 90 | _currentInputElement = *elem; |
| 91 | } |
| Rui Ueyama | 0945468 | 2013-10-08 00:43:53 +0000 | [diff] [blame] | 92 | |
| 93 | // Otherwise, try to get the next file of _currentInputElement. If the current |
| 94 | // input element points to an archive file, and there's a file left in the |
| 95 | // archive, it will succeed. If not, try to get the next file in the input |
| 96 | // graph. |
| 97 | for (;;) { |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 98 | ErrorOr<File &> nextFile = _currentInputElement->getNextFile(); |
| Rui Ueyama | 0945468 | 2013-10-08 00:43:53 +0000 | [diff] [blame] | 99 | if (error_code(nextFile) != input_graph_error::no_more_files) |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 100 | return std::move(nextFile); |
| Rui Ueyama | 0945468 | 2013-10-08 00:43:53 +0000 | [diff] [blame] | 101 | |
| 102 | ErrorOr<InputElement *> elem = inputGraph().getNextInputElement(); |
| 103 | if (error_code(elem) == input_graph_error::no_more_elements || |
| 104 | *elem == nullptr) |
| 105 | return make_error_code(input_graph_error::no_more_files); |
| 106 | _currentInputElement = *elem; |
| 107 | } |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 110 | void LinkingContext::addPasses(PassManager &pm) const {} |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 111 | |
| Michael J. Spencer | d68d619 | 2013-01-22 02:15:30 +0000 | [diff] [blame] | 112 | } // end namespace lld |