]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/ReaderWriter/ELF/ExecutableWriter.h
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / lib / ReaderWriter / ELF / ExecutableWriter.h
1 //===- lib/ReaderWriter/ELF/ExecutableWriter.h ----------------------------===//
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 #ifndef LLD_READER_WRITER_ELF_EXECUTABLE_WRITER_H
10 #define LLD_READER_WRITER_ELF_EXECUTABLE_WRITER_H
11
12 #include "OutputELFWriter.h"
13
14 namespace lld {
15 namespace elf {
16 using namespace llvm;
17 using namespace llvm::object;
18
19 template<class ELFT>
20 class ExecutableWriter;
21
22 //===----------------------------------------------------------------------===//
23 //  ExecutableWriter Class
24 //===----------------------------------------------------------------------===//
25 template<class ELFT>
26 class ExecutableWriter : public OutputELFWriter<ELFT> {
27 public:
28   ExecutableWriter(ELFLinkingContext &context, TargetLayout<ELFT> &layout)
29       : OutputELFWriter<ELFT>(context, layout),
30         _runtimeFile(new RuntimeFile<ELFT>(context, "C runtime")) {}
31
32 protected:
33   virtual void buildDynamicSymbolTable(const File &file);
34   virtual void addDefaultAtoms();
35   virtual bool createImplicitFiles(std::vector<std::unique_ptr<File> > &);
36   virtual void finalizeDefaultAtomValues();
37   virtual void createDefaultSections();
38
39   virtual bool isNeededTagRequired(const SharedLibraryAtom *sla) const {
40     return this->_layout.isCopied(sla);
41   }
42
43   unique_bump_ptr<InterpSection<ELFT>> _interpSection;
44   std::unique_ptr<RuntimeFile<ELFT> > _runtimeFile;
45 };
46
47 //===----------------------------------------------------------------------===//
48 //  ExecutableWriter
49 //===----------------------------------------------------------------------===//
50 template<class ELFT>
51 void ExecutableWriter<ELFT>::buildDynamicSymbolTable(const File &file) {
52   for (auto sec : this->_layout.sections())
53     if (auto section = dyn_cast<AtomSection<ELFT>>(sec))
54       for (const auto &atom : section->atoms()) {
55         const DefinedAtom *da = dyn_cast<const DefinedAtom>(atom->_atom);
56         if (!da)
57           continue;
58         if (da->dynamicExport() != DefinedAtom::dynamicExportAlways &&
59             !this->_context.isDynamicallyExportedSymbol(da->name()) &&
60             !(this->_context.shouldExportDynamic() &&
61               da->scope() == Atom::Scope::scopeGlobal))
62           continue;
63         this->_dynamicSymbolTable->addSymbol(atom->_atom, section->ordinal(),
64                                              atom->_virtualAddr, atom);
65       }
66
67   // Put weak symbols in the dynamic symbol table.
68   if (this->_context.isDynamic()) {
69     for (const UndefinedAtom *a : file.undefined()) {
70       if (this->_layout.isReferencedByDefinedAtom(a) &&
71           a->canBeNull() != UndefinedAtom::canBeNullNever)
72         this->_dynamicSymbolTable->addSymbol(a, ELF::SHN_UNDEF);
73     }
74   }
75
76   OutputELFWriter<ELFT>::buildDynamicSymbolTable(file);
77 }
78
79 /// \brief Add absolute symbols by default. These are linker added
80 /// absolute symbols
81 template<class ELFT>
82 void ExecutableWriter<ELFT>::addDefaultAtoms() {
83   OutputELFWriter<ELFT>::addDefaultAtoms();
84   _runtimeFile->addUndefinedAtom(this->_context.entrySymbolName());
85   _runtimeFile->addAbsoluteAtom("__bss_start");
86   _runtimeFile->addAbsoluteAtom("__bss_end");
87   _runtimeFile->addAbsoluteAtom("_end");
88   _runtimeFile->addAbsoluteAtom("end");
89   _runtimeFile->addAbsoluteAtom("__preinit_array_start");
90   _runtimeFile->addAbsoluteAtom("__preinit_array_end");
91   _runtimeFile->addAbsoluteAtom("__init_array_start");
92   _runtimeFile->addAbsoluteAtom("__init_array_end");
93   if (this->_context.isRelaOutputFormat()) {
94     _runtimeFile->addAbsoluteAtom("__rela_iplt_start");
95     _runtimeFile->addAbsoluteAtom("__rela_iplt_end");
96   } else {
97     _runtimeFile->addAbsoluteAtom("__rel_iplt_start");
98     _runtimeFile->addAbsoluteAtom("__rel_iplt_end");
99   }
100   _runtimeFile->addAbsoluteAtom("__fini_array_start");
101   _runtimeFile->addAbsoluteAtom("__fini_array_end");
102 }
103
104 /// \brief Hook in lld to add CRuntime file
105 template <class ELFT>
106 bool ExecutableWriter<ELFT>::createImplicitFiles(
107     std::vector<std::unique_ptr<File> > &result) {
108   // Add the default atoms as defined by executables
109   ExecutableWriter<ELFT>::addDefaultAtoms();
110   OutputELFWriter<ELFT>::createImplicitFiles(result);
111   result.push_back(std::move(_runtimeFile));
112   return true;
113 }
114
115 template <class ELFT> void ExecutableWriter<ELFT>::createDefaultSections() {
116   OutputELFWriter<ELFT>::createDefaultSections();
117   if (this->_context.isDynamic()) {
118     _interpSection.reset(new (this->_alloc) InterpSection<ELFT>(
119         this->_context, ".interp", DefaultLayout<ELFT>::ORDER_INTERP,
120         this->_context.getInterpreter()));
121     this->_layout.addSection(_interpSection.get());
122   }
123 }
124
125 /// Finalize the value of all the absolute symbols that we
126 /// created
127 template <class ELFT> void ExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
128   OutputELFWriter<ELFT>::finalizeDefaultAtomValues();
129   auto bssStartAtomIter = this->_layout.findAbsoluteAtom("__bss_start");
130   auto bssEndAtomIter = this->_layout.findAbsoluteAtom("__bss_end");
131   auto underScoreEndAtomIter = this->_layout.findAbsoluteAtom("_end");
132   auto endAtomIter = this->_layout.findAbsoluteAtom("end");
133
134   auto startEnd = [&](StringRef sym, StringRef sec) -> void {
135     std::string start = ("__" + sym + "_start").str();
136     std::string end = ("__" + sym + "_end").str();
137     auto s = this->_layout.findAbsoluteAtom(start);
138     auto e = this->_layout.findAbsoluteAtom(end);
139     auto section = this->_layout.findOutputSection(sec);
140     if (section) {
141       (*s)->_virtualAddr = section->virtualAddr();
142       (*e)->_virtualAddr = section->virtualAddr() + section->memSize();
143     } else {
144       (*s)->_virtualAddr = 0;
145       (*e)->_virtualAddr = 0;
146     }
147   };
148
149   startEnd("preinit_array", ".preinit_array");
150   startEnd("init_array", ".init_array");
151   if (this->_context.isRelaOutputFormat())
152     startEnd("rela_iplt", ".rela.plt");
153   else
154     startEnd("rel_iplt", ".rel.plt");
155   startEnd("fini_array", ".fini_array");
156
157   assert(!(bssStartAtomIter == this->_layout.absoluteAtoms().end() ||
158            bssEndAtomIter == this->_layout.absoluteAtoms().end() ||
159            underScoreEndAtomIter == this->_layout.absoluteAtoms().end() ||
160            endAtomIter == this->_layout.absoluteAtoms().end()) &&
161          "Unable to find the absolute atoms that have been added by lld");
162
163   auto bssSection = this->_layout.findOutputSection(".bss");
164
165   // If we don't find a bss section, then don't set these values
166   if (bssSection) {
167     (*bssStartAtomIter)->_virtualAddr = bssSection->virtualAddr();
168     (*bssEndAtomIter)->_virtualAddr =
169         bssSection->virtualAddr() + bssSection->memSize();
170     (*underScoreEndAtomIter)->_virtualAddr = (*bssEndAtomIter)->_virtualAddr;
171     (*endAtomIter)->_virtualAddr = (*bssEndAtomIter)->_virtualAddr;
172   } else if (auto dataSection = this->_layout.findOutputSection(".data")) {
173     (*underScoreEndAtomIter)->_virtualAddr =
174         dataSection->virtualAddr() + dataSection->memSize();
175     (*endAtomIter)->_virtualAddr = (*underScoreEndAtomIter)->_virtualAddr;
176   }
177 }
178
179 } // namespace elf
180 } // namespace lld
181
182 #endif // LLD_READER_WRITER_ELF_EXECUTABLE_WRITER_H