blob: e341a886eac92378965fbc21067376afb145c63c [file] [log] [blame]
Rui Ueyama0ca149f2013-08-06 22:31:59 +00001//===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
Michael J. Spencerd68d6192013-01-22 02:15:30 +00002//
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
Rui Ueyama0ca149f2013-08-06 22:31:59 +000010#include "lld/Core/LinkingContext.h"
Shankar Easwarand26c8e32013-08-31 05:27:38 +000011#include "lld/Core/InputFiles.h"
Nick Kledzikc314b462013-04-04 18:59:24 +000012#include "lld/ReaderWriter/Writer.h"
Shankar Easwarand26c8e32013-08-31 05:27:38 +000013#include "lld/ReaderWriter/Simple.h"
Michael J. Spencerd68d6192013-01-22 02:15:30 +000014
15#include "llvm/ADT/Triple.h"
16
17namespace lld {
Nick Kledzikc314b462013-04-04 18:59:24 +000018
Rui Ueyama0ca149f2013-08-06 22:31:59 +000019LinkingContext::LinkingContext()
Shankar Easwaraneeee23e2013-04-11 02:56:30 +000020 : Reader(*this), _deadStrip(false), _globalsAreDeadStripRoots(false),
21 _searchArchivesToOverrideTentativeDefinitions(false),
22 _searchSharedLibrariesToOverrideTentativeDefinitions(false),
23 _warnIfCoalesableAtomsHaveDifferentCanBeNull(false),
24 _warnIfCoalesableAtomsHaveDifferentLoadName(false),
25 _forceLoadAllArchives(false), _printRemainingUndefines(true),
26 _allowRemainingUndefines(false), _logInputFiles(false),
27 _allowShlibUndefines(false) {}
Nick Kledzikc314b462013-04-04 18:59:24 +000028
Rui Ueyama0ca149f2013-08-06 22:31:59 +000029LinkingContext::~LinkingContext() {}
Michael J. Spencerd68d6192013-01-22 02:15:30 +000030
Rui Ueyama0ca149f2013-08-06 22:31:59 +000031bool LinkingContext::validate(raw_ostream &diagnostics) {
Rafael Espindolac1b32682013-06-11 12:36:05 +000032 _yamlReader = createReaderYAML(*this);
33 return validateImpl(diagnostics);
34}
35
Rui Ueyama0ca149f2013-08-06 22:31:59 +000036error_code
37LinkingContext::readFile(StringRef path,
38 std::vector<std::unique_ptr<File>> &result) const {
Nick Kledzikc314b462013-04-04 18:59:24 +000039 OwningPtr<llvm::MemoryBuffer> opmb;
40 if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(path, opmb))
41 return ec;
Michael J. Spencerce1e53e2013-04-05 21:08:30 +000042
Michael J. Spencere6d56092013-04-05 22:04:44 +000043 std::unique_ptr<MemoryBuffer> mb(opmb.take());
44 return this->parseFile(mb, result);
Michael J. Spencerd68d6192013-01-22 02:15:30 +000045}
46
Rui Ueyama0ca149f2013-08-06 22:31:59 +000047error_code LinkingContext::writeFile(const File &linkedFile) const {
48 return this->writer().writeFile(linkedFile, _outputPath);
Michael J. Spencerd68d6192013-01-22 02:15:30 +000049}
50
Rui Ueyama0ca149f2013-08-06 22:31:59 +000051void LinkingContext::addImplicitFiles(InputFiles &inputs) const {
52 this->writer().addFiles(inputs);
Michael J. Spencerd68d6192013-01-22 02:15:30 +000053}
54
Shankar Easwarand26c8e32013-08-31 05:27:38 +000055std::unique_ptr<File> LinkingContext::createEntrySymbolFile() {
56 if (entrySymbolName().empty())
57 return nullptr;
58 std::unique_ptr<SimpleFile> entryFile(
59 new SimpleFile(*this, "command line option -entry"));
60 entryFile->addAtom(
61 *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
62 return std::move(entryFile);
63}
64
65std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() {
66 if (_initialUndefinedSymbols.empty())
67 return nullptr;
68 std::unique_ptr<SimpleFile> undefinedSymFile(
69 new SimpleFile(*this, "command line option -u"));
70 for (auto undefSymStr : _initialUndefinedSymbols)
71 undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
72 *undefinedSymFile, undefSymStr)));
73 return std::move(undefinedSymFile);
74}
75
76std::vector<std::unique_ptr<File> > LinkingContext::createInternalFiles() {
77 std::vector<std::unique_ptr<File> > result;
78 std::unique_ptr<File> internalFile;
79 internalFile = createEntrySymbolFile();
80 if (internalFile)
81 result.push_back(std::move(internalFile));
82 internalFile = createUndefinedSymbolFile();
83 if (internalFile)
84 result.push_back(std::move(internalFile));
85 return result;
86}
87
Rui Ueyama0ca149f2013-08-06 22:31:59 +000088void LinkingContext::addPasses(PassManager &pm) const {}
Nick Kledzikc314b462013-04-04 18:59:24 +000089
Michael J. Spencerd68d6192013-01-22 02:15:30 +000090} // end namespace lld