blob: 5d5d34fb14920fcb7c5037763f92793ded2c9bfc [file] [log] [blame]
Rui Ueyama0ca149f2013-08-06 22:31:59 +00001//===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
Michael J. Spencerd68d6192013-01-22 02:15:30 +00002//
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 Ueyama0ca149f2013-08-06 22:31:59 +000010#include "lld/Core/LinkingContext.h"
Shankar Easwarana96f3a32013-10-07 02:47:09 +000011#include "lld/Core/Resolver.h"
Nick Kledzikc314b462013-04-04 18:59:24 +000012#include "lld/ReaderWriter/Writer.h"
Shankar Easwarand26c8e32013-08-31 05:27:38 +000013#include "lld/ReaderWriter/Simple.h"
Michael J. Spencerd68d6192013-01-22 02:15:30 +000014
15#include "llvm/ADT/Triple.h"
16
17namespace lld {
Nick Kledzikc314b462013-04-04 18:59:24 +000018
Rui Ueyama0ca149f2013-08-06 22:31:59 +000019LinkingContext::LinkingContext()
Shankar Easwarana96f3a32013-10-07 02:47:09 +000020 : _deadStrip(false), _globalsAreDeadStripRoots(false),
Shankar Easwaraneeee23e2013-04-11 02:56:30 +000021 _searchArchivesToOverrideTentativeDefinitions(false),
22 _searchSharedLibrariesToOverrideTentativeDefinitions(false),
23 _warnIfCoalesableAtomsHaveDifferentCanBeNull(false),
24 _warnIfCoalesableAtomsHaveDifferentLoadName(false),
Shankar Easwarana96f3a32013-10-07 02:47:09 +000025 _printRemainingUndefines(true), _allowRemainingUndefines(false),
26 _logInputFiles(false), _allowShlibUndefines(false),
27 _outputFileType(OutputFileType::Default), _currentInputElement(nullptr) {}
Nick Kledzikc314b462013-04-04 18:59:24 +000028
Rui Ueyama0ca149f2013-08-06 22:31:59 +000029LinkingContext::~LinkingContext() {}
Michael J. Spencerd68d6192013-01-22 02:15:30 +000030
Rui Ueyama0ca149f2013-08-06 22:31:59 +000031bool LinkingContext::validate(raw_ostream &diagnostics) {
Rafael Espindolac1b32682013-06-11 12:36:05 +000032 _yamlReader = createReaderYAML(*this);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000033 _nativeReader = createReaderNative(*this);
Rafael Espindolac1b32682013-06-11 12:36:05 +000034 return validateImpl(diagnostics);
35}
36
Rui Ueyama0ca149f2013-08-06 22:31:59 +000037error_code LinkingContext::writeFile(const File &linkedFile) const {
38 return this->writer().writeFile(linkedFile, _outputPath);
Michael J. Spencerd68d6192013-01-22 02:15:30 +000039}
40
Shankar Easwarana96f3a32013-10-07 02:47:09 +000041bool LinkingContext::createImplicitFiles(
42 std::vector<std::unique_ptr<File> > &result) const {
43 return this->writer().createImplicitFiles(result);
Michael J. Spencerd68d6192013-01-22 02:15:30 +000044}
45
Shankar Easwarana96f3a32013-10-07 02:47:09 +000046std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +000047 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 Easwarana96f3a32013-10-07 02:47:09 +000056std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +000057 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 Easwarana96f3a32013-10-07 02:47:09 +000067bool LinkingContext::createInternalFiles(
68 std::vector<std::unique_ptr<File> > &result) const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +000069 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 Easwarana96f3a32013-10-07 02:47:09 +000076 return true;
77}
78
Rui Ueyama8b8e5212013-10-08 03:59:45 +000079void LinkingContext::setResolverState(uint32_t state) const {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000080 _currentInputElement->setResolverState(state);
81}
82
83ErrorOr<File &> LinkingContext::nextFile() const {
Rui Ueyama09454682013-10-08 00:43:53 +000084 // When nextFile() is called for the first time, _currentInputElement is not
85 // initialized. Initialize it with the first element of the input graph.
Shankar Easwarana96f3a32013-10-07 02:47:09 +000086 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 Ueyama09454682013-10-08 00:43:53 +000092
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 Easwarana96f3a32013-10-07 02:47:09 +000098 ErrorOr<File &> nextFile = _currentInputElement->getNextFile();
Rui Ueyama09454682013-10-08 00:43:53 +000099 if (error_code(nextFile) != input_graph_error::no_more_files)
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000100 return std::move(nextFile);
Rui Ueyama09454682013-10-08 00:43:53 +0000101
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 Easwarand26c8e32013-08-31 05:27:38 +0000108}
109
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000110void LinkingContext::addPasses(PassManager &pm) const {}
Nick Kledzikc314b462013-04-04 18:59:24 +0000111
Michael J. Spencerd68d6192013-01-22 02:15:30 +0000112} // end namespace lld