1 //===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lld/Core/LinkingContext.h"
10 #include "lld/Core/File.h"
11 #include "lld/Core/Node.h"
12 #include "lld/Core/Simple.h"
13 #include "lld/Core/Writer.h"
18 LinkingContext::LinkingContext() = default;
20 LinkingContext::~LinkingContext() = default;
22 bool LinkingContext::validate() {
23 return validateImpl();
26 llvm::Error LinkingContext::writeFile(const File &linkedFile) const {
27 return this->writer().writeFile(linkedFile, _outputPath);
30 std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
31 return createEntrySymbolFile("<command line option -e>");
35 LinkingContext::createEntrySymbolFile(StringRef filename) const {
36 if (entrySymbolName().empty())
38 std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename,
39 File::kindEntryObject));
41 *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
42 return std::move(entryFile);
45 std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
46 return createUndefinedSymbolFile("<command line option -u or --defsym>");
50 LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
51 if (_initialUndefinedSymbols.empty())
53 std::unique_ptr<SimpleFile> undefinedSymFile(
54 new SimpleFile(filename, File::kindUndefinedSymsObject));
55 for (StringRef undefSym : _initialUndefinedSymbols)
56 undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
57 *undefinedSymFile, undefSym)));
58 return std::move(undefinedSymFile);
61 void LinkingContext::createInternalFiles(
62 std::vector<std::unique_ptr<File>> &result) const {
63 if (std::unique_ptr<File> file = createEntrySymbolFile())
64 result.push_back(std::move(file));
65 if (std::unique_ptr<File> file = createUndefinedSymbolFile())
66 result.push_back(std::move(file));
69 } // end namespace lld