1//===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
14#include <algorithm>
15
16namespace lld {
17
18LinkingContext::LinkingContext() = default;
19
20LinkingContext::~LinkingContext() = default;
21
22bool LinkingContext::validate() {
23  return validateImpl();
24}
25
26llvm::Error LinkingContext::writeFile(const File &linkedFile) const {
27  return this->writer().writeFile(linkedFile, _outputPath);
28}
29
30std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
31  return createEntrySymbolFile("<command line option -e>");
32}
33
34std::unique_ptr<File>
35LinkingContext::createEntrySymbolFile(StringRef filename) const {
36  if (entrySymbolName().empty())
37    return nullptr;
38  std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename,
39                                                       File::kindEntryObject));
40  entryFile->addAtom(
41      *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
42  return std::move(entryFile);
43}
44
45std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
46  return createUndefinedSymbolFile("<command line option -u or --defsym>");
47}
48
49std::unique_ptr<File>
50LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
51  if (_initialUndefinedSymbols.empty())
52    return nullptr;
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);
59}
60
61void 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));
67}
68
69} // end namespace lld
70