blob: 5198a751cfbd4df54642661c17814606d14c924c [file] [log] [blame]
Sean Silvaf99309c2013-06-10 23:44:15 +00001//===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Silvaf99309c2013-06-10 23:44:15 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// The ELF component of yaml2obj.
Sean Silvaf99309c2013-06-10 23:44:15 +000011///
12//===----------------------------------------------------------------------===//
13
14#include "yaml2obj.h"
Will Dietz0b48c732013-10-12 21:29:16 +000015#include "llvm/ADT/ArrayRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/ELF.h"
Rafael Espindola97de4742014-07-03 02:01:39 +000017#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000018#include "llvm/Object/ELFObjectFile.h"
Rafael Espindolaebd91932016-03-01 19:15:06 +000019#include "llvm/ObjectYAML/ELFYAML.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000020#include "llvm/Support/MemoryBuffer.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000021#include "llvm/Support/WithColor.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000022#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/raw_ostream.h"
24
25using namespace llvm;
26
Sean Silva46dffff2013-06-13 22:20:01 +000027// This class is used to build up a contiguous binary blob while keeping
28// track of an offset in the output (which notionally begins at
29// `InitialOffset`).
Sean Silva2a74f702013-06-15 00:31:46 +000030namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000031class ContiguousBlobAccumulator {
32 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000033 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000034 raw_svector_ostream OS;
35
Sean Silvad93323f2013-06-22 00:47:43 +000036 /// \returns The new offset.
37 uint64_t padToAlignment(unsigned Align) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000038 if (Align == 0)
39 Align = 1;
Sean Silvad93323f2013-06-22 00:47:43 +000040 uint64_t CurrentOffset = InitialOffset + OS.tell();
Rui Ueyamada00f2f2016-01-14 21:06:47 +000041 uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
Sean Silvad93323f2013-06-22 00:47:43 +000042 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
43 OS.write('\0');
44 return AlignedOffset; // == CurrentOffset;
45 }
46
Sean Silva46dffff2013-06-13 22:20:01 +000047public:
Sean Silvabd3bc692013-06-20 19:11:41 +000048 ContiguousBlobAccumulator(uint64_t InitialOffset_)
49 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000050 template <class Integer>
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000051 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
Sean Silvad93323f2013-06-22 00:47:43 +000052 Offset = padToAlignment(Align);
53 return OS;
54 }
Sean Silva46dffff2013-06-13 22:20:01 +000055 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
56};
Sean Silva2a74f702013-06-15 00:31:46 +000057} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000058
Simon Atanasyan35babf92014-04-06 09:02:55 +000059// Used to keep track of section and symbol names, so that in the YAML file
60// sections and symbols can be referenced by name instead of by index.
Sean Silva2a74f702013-06-15 00:31:46 +000061namespace {
Simon Atanasyan35babf92014-04-06 09:02:55 +000062class NameToIdxMap {
Sean Silvaa6423eb2013-06-15 00:25:26 +000063 StringMap<int> Map;
64public:
65 /// \returns true if name is already present in the map.
Simon Atanasyan35babf92014-04-06 09:02:55 +000066 bool addName(StringRef Name, unsigned i) {
David Blaikie5106ce72014-11-19 05:49:42 +000067 return !Map.insert(std::make_pair(Name, (int)i)).second;
Sean Silvaa6423eb2013-06-15 00:25:26 +000068 }
69 /// \returns true if name is not present in the map
Simon Atanasyan35babf92014-04-06 09:02:55 +000070 bool lookup(StringRef Name, unsigned &Idx) const {
71 StringMap<int>::const_iterator I = Map.find(Name);
Sean Silvaa6423eb2013-06-15 00:25:26 +000072 if (I == Map.end())
73 return true;
74 Idx = I->getValue();
75 return false;
76 }
Dave Lee17307d92017-11-09 14:53:43 +000077 /// asserts if name is not present in the map
78 unsigned get(StringRef Name) const {
79 unsigned Idx = 0;
80 auto missing = lookup(Name, Idx);
81 (void)missing;
82 assert(!missing && "Expected section not found in index");
83 return Idx;
84 }
85 unsigned size() const { return Map.size(); }
Sean Silvaa6423eb2013-06-15 00:25:26 +000086};
Sean Silva2a74f702013-06-15 00:31:46 +000087} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +000088
Sean Silva38205932013-06-13 22:19:48 +000089template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000090static size_t arrayDataSize(ArrayRef<T> A) {
91 return A.size() * sizeof(T);
Sean Silva38205932013-06-13 22:19:48 +000092}
93
94template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000095static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
96 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva38205932013-06-13 22:19:48 +000097}
98
99template <class T>
100static void zero(T &Obj) {
101 memset(&Obj, 0, sizeof(Obj));
102}
103
Sean Silva08a75ae2013-06-20 19:11:44 +0000104namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000105/// "Single point of truth" for the ELF file construction.
Sean Silva08a75ae2013-06-20 19:11:44 +0000106/// TODO: This class still has a ways to go before it is truly a "single
107/// point of truth".
108template <class ELFT>
109class ELFState {
Rui Ueyama478d6352018-01-12 02:28:31 +0000110 typedef typename ELFT::Ehdr Elf_Ehdr;
111 typedef typename ELFT::Phdr Elf_Phdr;
112 typedef typename ELFT::Shdr Elf_Shdr;
113 typedef typename ELFT::Sym Elf_Sym;
114 typedef typename ELFT::Rel Elf_Rel;
115 typedef typename ELFT::Rela Elf_Rela;
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000116 typedef typename ELFT::Relr Elf_Relr;
Paul Semel1dbbfba2018-07-23 18:49:04 +0000117 typedef typename ELFT::Dyn Elf_Dyn;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000118
Dave Lee67b49662017-11-16 18:10:15 +0000119 enum class SymtabType { Static, Dynamic };
120
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000121 /// The future ".strtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000122 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva08a75ae2013-06-20 19:11:44 +0000123
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000124 /// The future ".shstrtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000125 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000126
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000127 /// The future ".dynstr" section.
Dave Lee67b49662017-11-16 18:10:15 +0000128 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
129
Simon Atanasyan35babf92014-04-06 09:02:55 +0000130 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000131 NameToIdxMap SymN2I;
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000132 const ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000133
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000134 bool buildSectionIndex();
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000135 bool buildSymbolIndex(std::size_t &StartIndex,
136 const std::vector<ELFYAML::Symbol> &Symbols);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000137 void initELFHeader(Elf_Ehdr &Header);
Petr Hosekeb04da32017-07-19 20:38:46 +0000138 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000139 bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
140 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000141 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000142 ContiguousBlobAccumulator &CBA);
143 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
144 StringTableBuilder &STB,
145 ContiguousBlobAccumulator &CBA);
Petr Hosekeb04da32017-07-19 20:38:46 +0000146 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
147 std::vector<Elf_Shdr> &SHeaders);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000148 void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
Dave Lee67b49662017-11-16 18:10:15 +0000149 std::vector<Elf_Sym> &Syms, unsigned SymbolBinding,
150 const StringTableBuilder &Strtab);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000151 void writeSectionContent(Elf_Shdr &SHeader,
152 const ELFYAML::RawContentSection &Section,
153 ContiguousBlobAccumulator &CBA);
154 bool writeSectionContent(Elf_Shdr &SHeader,
155 const ELFYAML::RelocationSection &Section,
156 ContiguousBlobAccumulator &CBA);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000157 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
158 ContiguousBlobAccumulator &CBA);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000159 bool writeSectionContent(Elf_Shdr &SHeader,
160 const ELFYAML::MipsABIFlags &Section,
161 ContiguousBlobAccumulator &CBA);
George Rimar0e7ed912019-02-09 11:34:28 +0000162 void writeSectionContent(Elf_Shdr &SHeader,
163 const ELFYAML::DynamicSection &Section,
164 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000165 bool hasDynamicSymbols() const;
166 SmallVector<const char *, 5> implicitSectionNames() const;
Sean Silva08a75ae2013-06-20 19:11:44 +0000167
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000168 // - SHT_NULL entry (placed first, i.e. 0'th entry)
Dave Lee67b49662017-11-16 18:10:15 +0000169 // - symbol table (.symtab) (defaults to after last yaml section)
170 // - string table (.strtab) (defaults to after .symtab)
171 // - section header string table (.shstrtab) (defaults to after .strtab)
172 // - dynamic symbol table (.dynsym) (defaults to after .shstrtab)
173 // - dynamic string table (.dynstr) (defaults to after .dynsym)
Dave Lee17307d92017-11-09 14:53:43 +0000174 unsigned getDotSymTabSecNo() const { return SN2I.get(".symtab"); }
175 unsigned getDotStrTabSecNo() const { return SN2I.get(".strtab"); }
176 unsigned getDotShStrTabSecNo() const { return SN2I.get(".shstrtab"); }
Dave Lee67b49662017-11-16 18:10:15 +0000177 unsigned getDotDynSymSecNo() const { return SN2I.get(".dynsym"); }
178 unsigned getDotDynStrSecNo() const { return SN2I.get(".dynstr"); }
Dave Lee17307d92017-11-09 14:53:43 +0000179 unsigned getSectionCount() const { return SN2I.size() + 1; }
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000180
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000181 ELFState(const ELFYAML::Object &D) : Doc(D) {}
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000182
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000183public:
184 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
Sean Silva08a75ae2013-06-20 19:11:44 +0000185};
186} // end anonymous namespace
187
Sean Silva37e817c2013-06-21 00:33:01 +0000188template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000189void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
190 using namespace llvm::ELF;
191 zero(Header);
192 Header.e_ident[EI_MAG0] = 0x7f;
193 Header.e_ident[EI_MAG1] = 'E';
194 Header.e_ident[EI_MAG2] = 'L';
195 Header.e_ident[EI_MAG3] = 'F';
196 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
197 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
198 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
199 Header.e_ident[EI_VERSION] = EV_CURRENT;
200 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
George Rimar6367d7a2018-12-20 10:43:49 +0000201 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000202 Header.e_type = Doc.Header.Type;
203 Header.e_machine = Doc.Header.Machine;
204 Header.e_version = EV_CURRENT;
205 Header.e_entry = Doc.Header.Entry;
Petr Hosekeb04da32017-07-19 20:38:46 +0000206 Header.e_phoff = sizeof(Header);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000207 Header.e_flags = Doc.Header.Flags;
208 Header.e_ehsize = sizeof(Elf_Ehdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000209 Header.e_phentsize = sizeof(Elf_Phdr);
210 Header.e_phnum = Doc.ProgramHeaders.size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000211 Header.e_shentsize = sizeof(Elf_Shdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000212 // Immediately following the ELF header and program headers.
213 Header.e_shoff =
214 sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000215 Header.e_shnum = getSectionCount();
216 Header.e_shstrndx = getDotShStrTabSecNo();
217}
218
219template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000220void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
221 for (const auto &YamlPhdr : Doc.ProgramHeaders) {
222 Elf_Phdr Phdr;
223 Phdr.p_type = YamlPhdr.Type;
224 Phdr.p_flags = YamlPhdr.Flags;
225 Phdr.p_vaddr = YamlPhdr.VAddr;
226 Phdr.p_paddr = YamlPhdr.PAddr;
227 PHeaders.push_back(Phdr);
228 }
229}
230
Xing GUO98228352018-12-04 14:27:51 +0000231static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
232 StringRef IndexSrc, unsigned &IndexDest) {
233 if (SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
234 WithColor::error() << "Unknown section referenced: '" << IndexSrc
235 << "' at YAML section '" << SecName << "'.\n";
236 return false;
237 }
238 return true;
239}
240
Petr Hosekeb04da32017-07-19 20:38:46 +0000241template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000242bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
243 ContiguousBlobAccumulator &CBA) {
244 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
245 // valid SHN_UNDEF entry since SHT_NULL == 0.
246 Elf_Shdr SHeader;
247 zero(SHeader);
248 SHeaders.push_back(SHeader);
249
250 for (const auto &Sec : Doc.Sections) {
251 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000252 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000253 SHeader.sh_type = Sec->Type;
254 SHeader.sh_flags = Sec->Flags;
255 SHeader.sh_addr = Sec->Address;
256 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000257
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000258 if (!Sec->Link.empty()) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000259 unsigned Index;
Xing GUO98228352018-12-04 14:27:51 +0000260 if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000261 return false;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000262 SHeader.sh_link = Index;
263 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000264
265 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
266 writeSectionContent(SHeader, *S, CBA);
267 else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
268 if (S->Link.empty())
269 // For relocation section set link to .symtab by default.
270 SHeader.sh_link = getDotSymTabSecNo();
271
272 unsigned Index;
George Rimarb87ea732019-02-12 09:08:59 +0000273 if (!convertSectionIndex(SN2I, S->Name, S->RelocatableSec, Index))
George Rimar7f2df7d2018-08-16 12:23:22 +0000274 return false;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000275 SHeader.sh_info = Index;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000276 if (!writeSectionContent(SHeader, *S, CBA))
277 return false;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000278 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
279 unsigned SymIdx;
George Rimarb87ea732019-02-12 09:08:59 +0000280 if (SymN2I.lookup(S->Signature, SymIdx) &&
281 !to_integer(S->Signature, SymIdx)) {
282 WithColor::error() << "Unknown symbol referenced: '" << S->Signature
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000283 << "' at YAML section '" << S->Name << "'.\n";
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000284 return false;
285 }
286 SHeader.sh_info = SymIdx;
287 if (!writeSectionContent(SHeader, *S, CBA))
288 return false;
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000289 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
290 if (!writeSectionContent(SHeader, *S, CBA))
291 return false;
Simon Atanasyan5db02762015-07-03 23:00:54 +0000292 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
293 SHeader.sh_entsize = 0;
294 SHeader.sh_size = S->Size;
295 // SHT_NOBITS section does not have content
296 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000297 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0e7ed912019-02-09 11:34:28 +0000298 } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec.get())) {
299 writeSectionContent(SHeader, *S, CBA);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000300 } else
301 llvm_unreachable("Unknown section type");
302
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000303 SHeaders.push_back(SHeader);
304 }
305 return true;
306}
307
308template <class ELFT>
309void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
Dave Lee67b49662017-11-16 18:10:15 +0000310 SymtabType STType,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000311 ContiguousBlobAccumulator &CBA) {
312 zero(SHeader);
Dave Lee67b49662017-11-16 18:10:15 +0000313 bool IsStatic = STType == SymtabType::Static;
314 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
315 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
316 SHeader.sh_link = IsStatic ? getDotStrTabSecNo() : getDotDynStrSecNo();
317 const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
318 auto &Strtab = IsStatic ? DotStrtab : DotDynstr;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000319 // One greater than symbol table index of the last local symbol.
Dave Lee67b49662017-11-16 18:10:15 +0000320 SHeader.sh_info = Symbols.Local.size() + 1;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000321 SHeader.sh_entsize = sizeof(Elf_Sym);
Simon Atanasyan3a120922015-07-09 18:23:02 +0000322 SHeader.sh_addralign = 8;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000323
324 std::vector<Elf_Sym> Syms;
325 {
326 // Ensure STN_UNDEF is present
327 Elf_Sym Sym;
328 zero(Sym);
329 Syms.push_back(Sym);
330 }
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000331
Dave Lee67b49662017-11-16 18:10:15 +0000332 // Add symbol names to .strtab or .dynstr.
333 for (const auto &Sym : Symbols.Local)
334 Strtab.add(Sym.Name);
335 for (const auto &Sym : Symbols.Global)
336 Strtab.add(Sym.Name);
337 for (const auto &Sym : Symbols.Weak)
338 Strtab.add(Sym.Name);
339 Strtab.finalize();
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000340
Dave Lee67b49662017-11-16 18:10:15 +0000341 addSymbols(Symbols.Local, Syms, ELF::STB_LOCAL, Strtab);
342 addSymbols(Symbols.Global, Syms, ELF::STB_GLOBAL, Strtab);
343 addSymbols(Symbols.Weak, Syms, ELF::STB_WEAK, Strtab);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000344
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000345 writeArrayData(
346 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
347 makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000348 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
349}
350
351template <class ELFT>
352void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
353 StringTableBuilder &STB,
354 ContiguousBlobAccumulator &CBA) {
355 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000356 SHeader.sh_name = DotShStrtab.getOffset(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000357 SHeader.sh_type = ELF::SHT_STRTAB;
Rafael Espindola39751af2016-10-04 22:43:25 +0000358 STB.write(CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign));
359 SHeader.sh_size = STB.getSize();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000360 SHeader.sh_addralign = 1;
361}
362
363template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000364void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
365 std::vector<Elf_Shdr> &SHeaders) {
366 uint32_t PhdrIdx = 0;
367 for (auto &YamlPhdr : Doc.ProgramHeaders) {
368 auto &PHeader = PHeaders[PhdrIdx++];
369
370 if (YamlPhdr.Sections.size())
371 PHeader.p_offset = UINT32_MAX;
372 else
373 PHeader.p_offset = 0;
374
375 // Find the minimum offset for the program header.
376 for (auto SecName : YamlPhdr.Sections) {
377 uint32_t Index = 0;
378 SN2I.lookup(SecName.Section, Index);
379 const auto &SHeader = SHeaders[Index];
380 PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset);
381 }
382
383 // Find the maximum offset of the end of a section in order to set p_filesz.
384 PHeader.p_filesz = 0;
385 for (auto SecName : YamlPhdr.Sections) {
386 uint32_t Index = 0;
387 SN2I.lookup(SecName.Section, Index);
388 const auto &SHeader = SHeaders[Index];
389 uint64_t EndOfSection;
390 if (SHeader.sh_type == llvm::ELF::SHT_NOBITS)
391 EndOfSection = SHeader.sh_offset;
392 else
393 EndOfSection = SHeader.sh_offset + SHeader.sh_size;
394 uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
395 EndOfSegment = std::max(EndOfSegment, EndOfSection);
396 PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
397 }
398
399 // Find the memory size by adding the size of sections at the end of the
400 // segment. These should be empty (size of zero) and NOBITS sections.
401 PHeader.p_memsz = PHeader.p_filesz;
402 for (auto SecName : YamlPhdr.Sections) {
403 uint32_t Index = 0;
404 SN2I.lookup(SecName.Section, Index);
405 const auto &SHeader = SHeaders[Index];
406 if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz)
407 PHeader.p_memsz += SHeader.sh_size;
408 }
409
410 // Set the alignment of the segment to be the same as the maximum alignment
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000411 // of the sections with the same offset so that by default the segment
Petr Hosekeb04da32017-07-19 20:38:46 +0000412 // has a valid and sensible alignment.
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000413 if (YamlPhdr.Align) {
414 PHeader.p_align = *YamlPhdr.Align;
415 } else {
416 PHeader.p_align = 1;
417 for (auto SecName : YamlPhdr.Sections) {
418 uint32_t Index = 0;
419 SN2I.lookup(SecName.Section, Index);
420 const auto &SHeader = SHeaders[Index];
421 if (SHeader.sh_offset == PHeader.p_offset)
422 PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign);
423 }
Petr Hosekeb04da32017-07-19 20:38:46 +0000424 }
425 }
426}
427
428template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000429void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
430 std::vector<Elf_Sym> &Syms,
Dave Lee67b49662017-11-16 18:10:15 +0000431 unsigned SymbolBinding,
432 const StringTableBuilder &Strtab) {
Simon Atanasyan048baca2014-03-14 06:53:30 +0000433 for (const auto &Sym : Symbols) {
Sean Silva6b083882013-06-18 23:14:03 +0000434 Elf_Sym Symbol;
435 zero(Symbol);
436 if (!Sym.Name.empty())
Dave Lee67b49662017-11-16 18:10:15 +0000437 Symbol.st_name = Strtab.getOffset(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000438 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000439 if (!Sym.Section.empty()) {
440 unsigned Index;
Simon Atanasyan35babf92014-04-06 09:02:55 +0000441 if (SN2I.lookup(Sym.Section, Index)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000442 WithColor::error() << "Unknown section referenced: '" << Sym.Section
443 << "' by YAML symbol " << Sym.Name << ".\n";
Sean Silvac4afa6d2013-06-21 01:11:48 +0000444 exit(1);
445 }
446 Symbol.st_shndx = Index;
Petr Hosek5c469a32017-09-07 20:44:16 +0000447 } else if (Sym.Index) {
448 Symbol.st_shndx = *Sym.Index;
449 }
450 // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000451 Symbol.st_value = Sym.Value;
Simon Atanasyan60e1a792014-11-06 22:46:24 +0000452 Symbol.st_other = Sym.Other;
Sean Silva05001b92013-06-20 20:59:47 +0000453 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000454 Syms.push_back(Symbol);
455 }
Sean Silvaaff51252013-06-21 00:27:50 +0000456}
457
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000458template <class ELFT>
459void
460ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
461 const ELFYAML::RawContentSection &Section,
462 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000463 assert(Section.Size >= Section.Content.binary_size() &&
464 "Section size and section content are inconsistent");
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000465 raw_ostream &OS =
466 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000467 Section.Content.writeAsBinary(OS);
468 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
469 OS.write(0);
George Rimar65a68282018-08-07 08:11:38 +0000470 if (Section.EntSize)
471 SHeader.sh_entsize = *Section.EntSize;
472 else if (Section.Type == llvm::ELF::SHT_RELR)
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000473 SHeader.sh_entsize = sizeof(Elf_Relr);
474 else
475 SHeader.sh_entsize = 0;
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000476 SHeader.sh_size = Section.Size;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000477}
478
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000479static bool isMips64EL(const ELFYAML::Object &Doc) {
480 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
481 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
482 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
483}
484
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000485template <class ELFT>
486bool
487ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
488 const ELFYAML::RelocationSection &Section,
489 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000490 assert((Section.Type == llvm::ELF::SHT_REL ||
491 Section.Type == llvm::ELF::SHT_RELA) &&
492 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000493
494 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
495 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
496 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
497
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000498 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000499
500 for (const auto &Rel : Section.Relocations) {
Adhemerval Zanella9f3dbff2015-04-22 15:26:43 +0000501 unsigned SymIdx = 0;
502 // Some special relocation, R_ARM_v4BX for instance, does not have
503 // an external reference. So it ignores the return value of lookup()
504 // here.
Petr Hosek5aa80f12017-08-30 23:13:31 +0000505 if (Rel.Symbol)
506 SymN2I.lookup(*Rel.Symbol, SymIdx);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000507
508 if (IsRela) {
509 Elf_Rela REntry;
510 zero(REntry);
511 REntry.r_offset = Rel.Offset;
512 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000513 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000514 OS.write((const char *)&REntry, sizeof(REntry));
515 } else {
516 Elf_Rel REntry;
517 zero(REntry);
518 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000519 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000520 OS.write((const char *)&REntry, sizeof(REntry));
521 }
522 }
523 return true;
524}
525
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000526template <class ELFT>
527bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
528 const ELFYAML::Group &Section,
529 ContiguousBlobAccumulator &CBA) {
Rui Ueyama478d6352018-01-12 02:28:31 +0000530 typedef typename ELFT::Word Elf_Word;
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000531 assert(Section.Type == llvm::ELF::SHT_GROUP &&
532 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000533
534 SHeader.sh_entsize = sizeof(Elf_Word);
535 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
536
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000537 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000538
539 for (auto member : Section.Members) {
540 Elf_Word SIdx;
541 unsigned int sectionIndex = 0;
542 if (member.sectionNameOrType == "GRP_COMDAT")
543 sectionIndex = llvm::ELF::GRP_COMDAT;
Xing GUO98228352018-12-04 14:27:51 +0000544 else if (!convertSectionIndex(SN2I, Section.Name, member.sectionNameOrType,
545 sectionIndex))
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000546 return false;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000547 SIdx = sectionIndex;
548 OS.write((const char *)&SIdx, sizeof(SIdx));
549 }
550 return true;
551}
552
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000553template <class ELFT>
554bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
555 const ELFYAML::MipsABIFlags &Section,
556 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000557 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
558 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000559
560 object::Elf_Mips_ABIFlags<ELFT> Flags;
561 zero(Flags);
562 SHeader.sh_entsize = sizeof(Flags);
563 SHeader.sh_size = SHeader.sh_entsize;
564
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000565 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000566 Flags.version = Section.Version;
567 Flags.isa_level = Section.ISALevel;
568 Flags.isa_rev = Section.ISARevision;
569 Flags.gpr_size = Section.GPRSize;
570 Flags.cpr1_size = Section.CPR1Size;
571 Flags.cpr2_size = Section.CPR2Size;
572 Flags.fp_abi = Section.FpABI;
573 Flags.isa_ext = Section.ISAExtension;
574 Flags.ases = Section.ASEs;
575 Flags.flags1 = Section.Flags1;
576 Flags.flags2 = Section.Flags2;
577 OS.write((const char *)&Flags, sizeof(Flags));
578
579 return true;
580}
581
George Rimar0e7ed912019-02-09 11:34:28 +0000582template <class ELFT>
583void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
584 const ELFYAML::DynamicSection &Section,
585 ContiguousBlobAccumulator &CBA) {
George Rimar5cb31732019-02-10 08:35:38 +0000586 typedef typename ELFT::Addr Elf_Addr;
George Rimar0e7ed912019-02-09 11:34:28 +0000587 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
588 "Section type is not SHT_DYNAMIC");
589
George Rimar5cb31732019-02-10 08:35:38 +0000590 SHeader.sh_size = 2 * sizeof(Elf_Addr) * Section.Entries.size();
George Rimar0e7ed912019-02-09 11:34:28 +0000591 if (Section.EntSize)
592 SHeader.sh_entsize = *Section.EntSize;
593 else
594 SHeader.sh_entsize = sizeof(Elf_Dyn);
595
596 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
597 for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
George Rimar5cb31732019-02-10 08:35:38 +0000598 Elf_Addr Tag = (Elf_Addr)DE.Tag;
599 OS.write((const char *)&Tag, sizeof(Elf_Addr));
600 Elf_Addr Val = (Elf_Addr)DE.Val;
601 OS.write((const char *)&Val, sizeof(Elf_Addr));
George Rimar0e7ed912019-02-09 11:34:28 +0000602 }
603}
604
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000605template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000606 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000607 StringRef Name = Doc.Sections[i]->Name;
Dave Lee17307d92017-11-09 14:53:43 +0000608 DotShStrtab.add(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000609 // "+ 1" to take into account the SHT_NULL entry.
610 if (SN2I.addName(Name, i + 1)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000611 WithColor::error() << "Repeated section name: '" << Name
612 << "' at YAML section number " << i << ".\n";
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000613 return false;
614 }
Sean Silvaaff51252013-06-21 00:27:50 +0000615 }
Dave Lee17307d92017-11-09 14:53:43 +0000616
617 auto SecNo = 1 + Doc.Sections.size();
618 // Add special sections after input sections, if necessary.
Dave Lee67b49662017-11-16 18:10:15 +0000619 for (const auto &Name : implicitSectionNames())
Dave Lee17307d92017-11-09 14:53:43 +0000620 if (!SN2I.addName(Name, SecNo)) {
621 // Account for this section, since it wasn't in the Doc
622 ++SecNo;
623 DotShStrtab.add(Name);
624 }
625
626 DotShStrtab.finalize();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000627 return true;
Sean Silva6b083882013-06-18 23:14:03 +0000628}
629
Sean Silvaf99309c2013-06-10 23:44:15 +0000630template <class ELFT>
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000631bool
632ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
633 const std::vector<ELFYAML::Symbol> &Symbols) {
634 for (const auto &Sym : Symbols) {
635 ++StartIndex;
636 if (Sym.Name.empty())
637 continue;
638 if (SymN2I.addName(Sym.Name, StartIndex)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000639 WithColor::error() << "Repeated symbol name: '" << Sym.Name << "'.\n";
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000640 return false;
641 }
642 }
643 return true;
644}
645
646template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000647int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000648 ELFState<ELFT> State(Doc);
649 if (!State.buildSectionIndex())
650 return 1;
651
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000652 std::size_t StartSymIndex = 0;
653 if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
654 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
655 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
656 return 1;
657
Sean Silva38205932013-06-13 22:19:48 +0000658 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000659 State.initELFHeader(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000660
Sean Silva38205932013-06-13 22:19:48 +0000661 // TODO: Flesh out section header support.
Petr Hosekeb04da32017-07-19 20:38:46 +0000662
663 std::vector<Elf_Phdr> PHeaders;
664 State.initProgramHeaders(PHeaders);
Sean Silva38205932013-06-13 22:19:48 +0000665
Sean Silva08a75ae2013-06-20 19:11:44 +0000666 // XXX: This offset is tightly coupled with the order that we write
667 // things to `OS`.
Petr Hosekeb04da32017-07-19 20:38:46 +0000668 const size_t SectionContentBeginOffset = Header.e_ehsize +
669 Header.e_phentsize * Header.e_phnum +
670 Header.e_shentsize * Header.e_shnum;
Sean Silva08a75ae2013-06-20 19:11:44 +0000671 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +0000672
Sean Silva38205932013-06-13 22:19:48 +0000673 std::vector<Elf_Shdr> SHeaders;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000674 if(!State.initSectionHeaders(SHeaders, CBA))
675 return 1;
Sean Silva38205932013-06-13 22:19:48 +0000676
Dave Lee17307d92017-11-09 14:53:43 +0000677 // Populate SHeaders with implicit sections not present in the Doc
Dave Lee67b49662017-11-16 18:10:15 +0000678 for (const auto &Name : State.implicitSectionNames())
Dave Lee17307d92017-11-09 14:53:43 +0000679 if (State.SN2I.get(Name) >= SHeaders.size())
680 SHeaders.push_back({});
Sean Silva82177572013-06-22 01:38:00 +0000681
Dave Lee17307d92017-11-09 14:53:43 +0000682 // Initialize the implicit sections
683 auto Index = State.SN2I.get(".symtab");
Dave Lee67b49662017-11-16 18:10:15 +0000684 State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Static, CBA);
Dave Lee17307d92017-11-09 14:53:43 +0000685 Index = State.SN2I.get(".strtab");
686 State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA);
687 Index = State.SN2I.get(".shstrtab");
688 State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000689 if (State.hasDynamicSymbols()) {
690 Index = State.SN2I.get(".dynsym");
691 State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Dynamic, CBA);
692 SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
693 Index = State.SN2I.get(".dynstr");
694 State.initStrtabSectionHeader(SHeaders[Index], ".dynstr", State.DotDynstr, CBA);
695 SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
696 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000697
Petr Hosekeb04da32017-07-19 20:38:46 +0000698 // Now we can decide segment offsets
699 State.setProgramHeaderLayout(PHeaders, SHeaders);
700
Sean Silvaf99309c2013-06-10 23:44:15 +0000701 OS.write((const char *)&Header, sizeof(Header));
Petr Hosekeb04da32017-07-19 20:38:46 +0000702 writeArrayData(OS, makeArrayRef(PHeaders));
Will Dietz0b48c732013-10-12 21:29:16 +0000703 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +0000704 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000705 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000706}
707
Dave Lee67b49662017-11-16 18:10:15 +0000708template <class ELFT> bool ELFState<ELFT>::hasDynamicSymbols() const {
709 return Doc.DynamicSymbols.Global.size() > 0 ||
710 Doc.DynamicSymbols.Weak.size() > 0 ||
711 Doc.DynamicSymbols.Local.size() > 0;
712}
713
Xing GUObac78642018-12-07 11:04:22 +0000714template <class ELFT>
715SmallVector<const char *, 5> ELFState<ELFT>::implicitSectionNames() const {
Dave Lee67b49662017-11-16 18:10:15 +0000716 if (!hasDynamicSymbols())
717 return {".symtab", ".strtab", ".shstrtab"};
718 return {".symtab", ".strtab", ".shstrtab", ".dynsym", ".dynstr"};
719}
720
Sean Silva11caeba2013-06-22 01:03:35 +0000721static bool is64Bit(const ELFYAML::Object &Doc) {
722 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
723}
724
725static bool isLittleEndian(const ELFYAML::Object &Doc) {
726 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
727}
728
Chris Bieneman8ff0c112016-06-27 19:53:53 +0000729int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
Sean Silva11caeba2013-06-22 01:03:35 +0000730 if (is64Bit(Doc)) {
731 if (isLittleEndian(Doc))
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000732 return ELFState<object::ELF64LE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000733 else
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000734 return ELFState<object::ELF64BE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000735 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000736 if (isLittleEndian(Doc))
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000737 return ELFState<object::ELF32LE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000738 else
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000739 return ELFState<object::ELF32BE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000740 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000741}