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/Alias.h"
11 #include "lld/Core/LinkingContext.h"
12 #include "lld/Core/Resolver.h"
13 #include "lld/Core/Simple.h"
14 #include "lld/Core/Writer.h"
15 #include "llvm/ADT/Triple.h"
19 LinkingContext::LinkingContext()
20 : _deadStrip(false), _allowDuplicates(false),
21 _globalsAreDeadStripRoots(false),
22 _searchArchivesToOverrideTentativeDefinitions(false),
23 _searchSharedLibrariesToOverrideTentativeDefinitions(false),
24 _warnIfCoalesableAtomsHaveDifferentCanBeNull(false),
25 _warnIfCoalesableAtomsHaveDifferentLoadName(false),
26 _printRemainingUndefines(true), _allowRemainingUndefines(false),
27 _logInputFiles(false), _allowShlibUndefines(false),
28 _outputFileType(OutputFileType::Default), _nextOrdinal(0) {}
30 LinkingContext::~LinkingContext() {}
32 bool LinkingContext::validate(raw_ostream &diagnostics) {
33 return validateImpl(diagnostics);
36 std::error_code LinkingContext::writeFile(const File &linkedFile) const {
37 return this->writer().writeFile(linkedFile, _outputPath);
40 bool LinkingContext::createImplicitFiles(
41 std::vector<std::unique_ptr<File> > &result) {
42 return this->writer().createImplicitFiles(result);
45 std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
46 return createEntrySymbolFile("<command line option -e>");
50 LinkingContext::createEntrySymbolFile(StringRef filename) const {
51 if (entrySymbolName().empty())
53 std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename));
55 *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
56 return std::move(entryFile);
59 std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
60 return createUndefinedSymbolFile("<command line option -u or --defsym>");
64 LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
65 if (_initialUndefinedSymbols.empty())
67 std::unique_ptr<SimpleFile> undefinedSymFile(new SimpleFile(filename));
68 for (StringRef undefSym : _initialUndefinedSymbols)
69 undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
70 *undefinedSymFile, undefSym)));
71 return std::move(undefinedSymFile);
74 std::unique_ptr<File> LinkingContext::createAliasSymbolFile() const {
75 if (getAliases().empty())
77 std::unique_ptr<SimpleFile> file(new SimpleFile("<alias>"));
78 for (const auto &i : getAliases()) {
79 StringRef from = i.first;
80 StringRef to = i.second;
81 SimpleDefinedAtom *fromAtom = new (_allocator) AliasAtom(*file, from);
82 UndefinedAtom *toAtom = new (_allocator) SimpleUndefinedAtom(*file, to);
83 fromAtom->addReference(Reference::KindNamespace::all,
84 Reference::KindArch::all, Reference::kindLayoutAfter,
86 file->addAtom(*fromAtom);
87 file->addAtom(*toAtom);
89 return std::move(file);
92 void LinkingContext::createInternalFiles(
93 std::vector<std::unique_ptr<File> > &result) const {
94 if (std::unique_ptr<File> file = createEntrySymbolFile())
95 result.push_back(std::move(file));
96 if (std::unique_ptr<File> file = createUndefinedSymbolFile())
97 result.push_back(std::move(file));
98 if (std::unique_ptr<File> file = createAliasSymbolFile())
99 result.push_back(std::move(file));
102 void LinkingContext::addPasses(PassManager &pm) {}
104 } // end namespace lld