]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Core/LinkingContext.cpp
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / lib / Core / LinkingContext.cpp
1 //===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
2 //
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
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"
16
17 namespace lld {
18
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) {}
29
30 LinkingContext::~LinkingContext() {}
31
32 bool LinkingContext::validate(raw_ostream &diagnostics) {
33   return validateImpl(diagnostics);
34 }
35
36 std::error_code LinkingContext::writeFile(const File &linkedFile) const {
37   return this->writer().writeFile(linkedFile, _outputPath);
38 }
39
40 bool LinkingContext::createImplicitFiles(
41     std::vector<std::unique_ptr<File> > &result) {
42   return this->writer().createImplicitFiles(result);
43 }
44
45 std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
46   return createEntrySymbolFile("<command line option -e>");
47 }
48
49 std::unique_ptr<File>
50 LinkingContext::createEntrySymbolFile(StringRef filename) const {
51   if (entrySymbolName().empty())
52     return nullptr;
53   std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename));
54   entryFile->addAtom(
55       *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
56   return std::move(entryFile);
57 }
58
59 std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
60   return createUndefinedSymbolFile("<command line option -u or --defsym>");
61 }
62
63 std::unique_ptr<File>
64 LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
65   if (_initialUndefinedSymbols.empty())
66     return nullptr;
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);
72 }
73
74 std::unique_ptr<File> LinkingContext::createAliasSymbolFile() const {
75   if (getAliases().empty())
76     return nullptr;
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,
85                            0, toAtom, 0);
86     file->addAtom(*fromAtom);
87     file->addAtom(*toAtom);
88   }
89   return std::move(file);
90 }
91
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));
100 }
101
102 void LinkingContext::addPasses(PassManager &pm) {}
103
104 } // end namespace lld