1 //===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lld/Core/LinkingContext.h"
11 #include "lld/Core/File.h"
12 #include "lld/Core/Node.h"
13 #include "lld/Core/Simple.h"
14 #include "lld/Core/Writer.h"
19 LinkingContext::LinkingContext() = default;
21 LinkingContext::~LinkingContext() = default;
23 bool LinkingContext::validate() {
24 return validateImpl();
27 llvm::Error LinkingContext::writeFile(const File &linkedFile) const {
28 return this->writer().writeFile(linkedFile, _outputPath);
31 std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
32 return createEntrySymbolFile("<command line option -e>");
36 LinkingContext::createEntrySymbolFile(StringRef filename) const {
37 if (entrySymbolName().empty())
39 std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename,
40 File::kindEntryObject));
42 *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
43 return std::move(entryFile);
46 std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
47 return createUndefinedSymbolFile("<command line option -u or --defsym>");
51 LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
52 if (_initialUndefinedSymbols.empty())
54 std::unique_ptr<SimpleFile> undefinedSymFile(
55 new SimpleFile(filename, File::kindUndefinedSymsObject));
56 for (StringRef undefSym : _initialUndefinedSymbols)
57 undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
58 *undefinedSymFile, undefSym)));
59 return std::move(undefinedSymFile);
62 void LinkingContext::createInternalFiles(
63 std::vector<std::unique_ptr<File>> &result) const {
64 if (std::unique_ptr<File> file = createEntrySymbolFile())
65 result.push_back(std::move(file));
66 if (std::unique_ptr<File> file = createUndefinedSymbolFile())
67 result.push_back(std::move(file));
70 } // end namespace lld