blob: 4beb32a3bc6704dd7c86e2973c832acc74bbff2c [file] [log] [blame]
Michael J. Spencera55e37f2013-03-01 00:03:36 +00001//===- lib/ReaderWriter/ReaderLinkerScript.cpp ----------------------------===//
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/ReaderWriter/ReaderLinkerScript.h"
11
12#include "lld/Core/Error.h"
13#include "lld/Core/File.h"
14#include "lld/Core/LinkerOptions.h"
15#include "lld/ReaderWriter/LinkerScript.h"
16
17using namespace lld;
18using namespace script;
19
20namespace {
21class LinkerScriptFile : public File {
22public:
23 static ErrorOr<std::unique_ptr<LinkerScriptFile> >
24 create(const TargetInfo &ti, std::unique_ptr<llvm::MemoryBuffer> mb) {
25 std::unique_ptr<LinkerScriptFile> file(
26 new LinkerScriptFile(ti, std::move(mb)));
27 file->_script = file->_parser.parse();
28 if (!file->_script)
29 return linker_script_reader_error::parse_error;
30 return std::move(file);
31 }
32
Michael J. Spencera55e37f2013-03-01 00:03:36 +000033 static inline bool classof(const File *f) {
34 return f->kind() == kindLinkerScript;
35 }
36
37 virtual void setOrdinalAndIncrement(uint64_t &ordinal) const {
38 _ordinal = ordinal++;
39 }
40
41 virtual const TargetInfo &getTargetInfo() const { return _targetInfo; }
42
43 virtual const atom_collection<DefinedAtom> &defined() const {
44 return _definedAtoms;
45 }
46
47 virtual const atom_collection<UndefinedAtom> &undefined() const {
48 return _undefinedAtoms;
49 }
50
51 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
52 return _sharedLibraryAtoms;
53 }
54
55 virtual const atom_collection<AbsoluteAtom> &absolute() const {
56 return _absoluteAtoms;
57 }
58
59 const LinkerScript *getScript() {
60 return _script;
61 }
62
63private:
64 LinkerScriptFile(const TargetInfo &ti, std::unique_ptr<llvm::MemoryBuffer> mb)
Michael J. Spencer0f3dd612013-03-20 18:57:27 +000065 : File(mb->getBufferIdentifier(), kindLinkerScript),
Michael J. Spencera55e37f2013-03-01 00:03:36 +000066 _targetInfo(ti),
67 _lexer(std::move(mb)),
68 _parser(_lexer),
69 _script(nullptr) {}
70
71 const TargetInfo &_targetInfo;
72 atom_collection_vector<DefinedAtom> _definedAtoms;
73 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
74 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
75 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
76 Lexer _lexer;
77 Parser _parser;
78 const LinkerScript *_script;
79};
80} // end anon namespace
81
82namespace lld {
83error_code
84ReaderLinkerScript::parseFile(std::unique_ptr<llvm::MemoryBuffer> mb,
85 std::vector<std::unique_ptr<File> > &result) {
86 auto lsf = LinkerScriptFile::create(_targetInfo, std::move(mb));
87 if (!lsf)
88 return lsf;
89 const LinkerScript *ls = (*lsf)->getScript();
90 result.push_back(std::move(*lsf));
91 for (const auto &c : ls->_commands) {
92 if (auto group = dyn_cast<Group>(c))
93 for (const auto &path : group->getPaths()) {
94 auto reader = _getReader(LinkerInput(path._path));
95 if (!reader)
96 return reader;
97 if (error_code ec = reader->readFile(path._path, result))
98 return ec;
99 }
100 }
101 return error_code::success();
102}
103} // end namespace lld