blob: c85cf4c924f0a7638c4be4bfaed1ec2d3570367a [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
Will Dietz0b48c732013-10-12 21:29:16 +000014#include "llvm/ADT/ArrayRef.h"
Alex Brachetc22d9662019-08-07 02:44:49 +000015#include "llvm/ADT/StringSet.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"
Alex Brachetc22d9662019-08-07 02:44:49 +000020#include "llvm/ObjectYAML/yaml2obj.h"
George Rimard063c7d2019-02-20 13:58:43 +000021#include "llvm/Support/EndianStream.h"
George Rimar1a219aa2019-09-24 14:22:37 +000022#include "llvm/Support/LEB128.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000023#include "llvm/Support/MemoryBuffer.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000024#include "llvm/Support/WithColor.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000025#include "llvm/Support/YAMLTraits.h"
26#include "llvm/Support/raw_ostream.h"
27
28using namespace llvm;
29
Sean Silva46dffff2013-06-13 22:20:01 +000030// This class is used to build up a contiguous binary blob while keeping
31// track of an offset in the output (which notionally begins at
32// `InitialOffset`).
Sean Silva2a74f702013-06-15 00:31:46 +000033namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000034class ContiguousBlobAccumulator {
35 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000036 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000037 raw_svector_ostream OS;
38
Sean Silvad93323f2013-06-22 00:47:43 +000039 /// \returns The new offset.
40 uint64_t padToAlignment(unsigned Align) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000041 if (Align == 0)
42 Align = 1;
Sean Silvad93323f2013-06-22 00:47:43 +000043 uint64_t CurrentOffset = InitialOffset + OS.tell();
Rui Ueyamada00f2f2016-01-14 21:06:47 +000044 uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
George Rimarbd8bfd32019-04-29 12:05:53 +000045 OS.write_zeros(AlignedOffset - CurrentOffset);
Sean Silvad93323f2013-06-22 00:47:43 +000046 return AlignedOffset; // == CurrentOffset;
47 }
48
Sean Silva46dffff2013-06-13 22:20:01 +000049public:
Sean Silvabd3bc692013-06-20 19:11:41 +000050 ContiguousBlobAccumulator(uint64_t InitialOffset_)
51 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000052 template <class Integer>
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000053 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
Sean Silvad93323f2013-06-22 00:47:43 +000054 Offset = padToAlignment(Align);
55 return OS;
56 }
Sean Silva46dffff2013-06-13 22:20:01 +000057 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
58};
59
Simon Atanasyan35babf92014-04-06 09:02:55 +000060// Used to keep track of section and symbol names, so that in the YAML file
George Rimar09746882019-05-07 12:10:51 +000061// sections and symbols can be referenced by name instead of by index.
George Rimar09746882019-05-07 12:10:51 +000062class NameToIdxMap {
Puyan Lotfia10f0162019-05-11 17:03:36 +000063 StringMap<unsigned> Map;
64
George Rimar09746882019-05-07 12:10:51 +000065public:
Puyan Lotfia10f0162019-05-11 17:03:36 +000066 /// \Returns false if name is already present in the map.
67 bool addName(StringRef Name, unsigned Ndx) {
68 return Map.insert({Name, Ndx}).second;
George Rimar09746882019-05-07 12:10:51 +000069 }
Puyan Lotfia10f0162019-05-11 17:03:36 +000070 /// \Returns false if name is not present in the map.
George Rimar09746882019-05-07 12:10:51 +000071 bool lookup(StringRef Name, unsigned &Idx) const {
Puyan Lotfia10f0162019-05-11 17:03:36 +000072 auto I = Map.find(Name);
George Rimar09746882019-05-07 12:10:51 +000073 if (I == Map.end())
Puyan Lotfia10f0162019-05-11 17:03:36 +000074 return false;
George Rimar09746882019-05-07 12:10:51 +000075 Idx = I->getValue();
Puyan Lotfia10f0162019-05-11 17:03:36 +000076 return true;
George Rimar09746882019-05-07 12:10:51 +000077 }
Puyan Lotfia10f0162019-05-11 17:03:36 +000078 /// Asserts if name is not present in the map.
George Rimar09746882019-05-07 12:10:51 +000079 unsigned get(StringRef Name) const {
Puyan Lotfia10f0162019-05-11 17:03:36 +000080 unsigned Idx;
81 if (lookup(Name, Idx))
82 return Idx;
83 assert(false && "Expected section not found in index");
84 return 0;
George Rimar09746882019-05-07 12:10:51 +000085 }
86 unsigned size() const { return Map.size(); }
87};
Sean Silvaa6423eb2013-06-15 00:25:26 +000088
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000089/// "Single point of truth" for the ELF file construction.
Sean Silva08a75ae2013-06-20 19:11:44 +000090/// TODO: This class still has a ways to go before it is truly a "single
91/// point of truth".
Alex Brachetc22d9662019-08-07 02:44:49 +000092template <class ELFT> class ELFState {
Rui Ueyama478d6352018-01-12 02:28:31 +000093 typedef typename ELFT::Ehdr Elf_Ehdr;
94 typedef typename ELFT::Phdr Elf_Phdr;
95 typedef typename ELFT::Shdr Elf_Shdr;
96 typedef typename ELFT::Sym Elf_Sym;
97 typedef typename ELFT::Rel Elf_Rel;
98 typedef typename ELFT::Rela Elf_Rela;
Jake Ehrlich0f440d82018-06-28 21:07:34 +000099 typedef typename ELFT::Relr Elf_Relr;
Paul Semel1dbbfba2018-07-23 18:49:04 +0000100 typedef typename ELFT::Dyn Elf_Dyn;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000101
Dave Lee67b49662017-11-16 18:10:15 +0000102 enum class SymtabType { Static, Dynamic };
103
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000104 /// The future ".strtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000105 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva08a75ae2013-06-20 19:11:44 +0000106
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000107 /// The future ".shstrtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000108 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000109
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000110 /// The future ".dynstr" section.
Dave Lee67b49662017-11-16 18:10:15 +0000111 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
112
Simon Atanasyan35babf92014-04-06 09:02:55 +0000113 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000114 NameToIdxMap SymN2I;
George Rimar91208442019-08-22 12:39:56 +0000115 NameToIdxMap DynSymN2I;
George Rimar13a364e2019-07-22 12:01:52 +0000116 ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000117
George Rimar3212ecf2019-09-09 09:43:03 +0000118 bool HasError = false;
George Rimar85011022019-09-13 16:00:16 +0000119 yaml::ErrorHandler ErrHandler;
120 void reportError(const Twine &Msg);
George Rimar3212ecf2019-09-09 09:43:03 +0000121
122 std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
123 const StringTableBuilder &Strtab);
124 unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = "");
125 unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic);
George Rimar3212ecf2019-09-09 09:43:03 +0000126
127 void buildSectionIndex();
128 void buildSymbolIndexes();
Petr Hosekeb04da32017-07-19 20:38:46 +0000129 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
George Rimar33b1a0e2019-09-05 08:59:28 +0000130 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
131 StringRef SecName, ELFYAML::Section *YAMLSec);
George Rimar3212ecf2019-09-09 09:43:03 +0000132 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000133 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000134 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
George Rimar66296dc2019-06-05 13:16:53 +0000135 ContiguousBlobAccumulator &CBA,
136 ELFYAML::Section *YAMLSec);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000137 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
138 StringTableBuilder &STB,
George Rimar66296dc2019-06-05 13:16:53 +0000139 ContiguousBlobAccumulator &CBA,
140 ELFYAML::Section *YAMLSec);
Petr Hosekeb04da32017-07-19 20:38:46 +0000141 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
142 std::vector<Elf_Shdr> &SHeaders);
George Rimar33b1a0e2019-09-05 08:59:28 +0000143 void finalizeStrings();
Fangrui Songc3bc6972019-09-05 14:25:57 +0000144 void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS);
George Rimar3212ecf2019-09-09 09:43:03 +0000145 void writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000146 const ELFYAML::RawContentSection &Section,
147 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000148 void writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000149 const ELFYAML::RelocationSection &Section,
150 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000151 void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000152 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000153 void writeSectionContent(Elf_Shdr &SHeader,
George Rimard3963052019-08-08 09:49:05 +0000154 const ELFYAML::SymtabShndxSection &Shndx,
155 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000156 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar646af082019-02-19 15:29:07 +0000157 const ELFYAML::SymverSection &Section,
158 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000159 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar0621b792019-02-19 14:53:48 +0000160 const ELFYAML::VerneedSection &Section,
161 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000162 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar623ae722019-02-21 12:21:43 +0000163 const ELFYAML::VerdefSection &Section,
164 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000165 void writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000166 const ELFYAML::MipsABIFlags &Section,
167 ContiguousBlobAccumulator &CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000168 void writeSectionContent(Elf_Shdr &SHeader,
George Rimar0e7ed912019-02-09 11:34:28 +0000169 const ELFYAML::DynamicSection &Section,
170 ContiguousBlobAccumulator &CBA);
George Rimar1a219aa2019-09-24 14:22:37 +0000171 void writeSectionContent(Elf_Shdr &SHeader,
172 const ELFYAML::StackSizesSection &Section,
173 ContiguousBlobAccumulator &CBA);
George Rimare5163eb2019-10-01 09:45:59 +0000174 void writeSectionContent(Elf_Shdr &SHeader,
175 const ELFYAML::HashSection &Section,
176 ContiguousBlobAccumulator &CBA);
George Rimar85011022019-09-13 16:00:16 +0000177 ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
178
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000179public:
George Rimar85011022019-09-13 16:00:16 +0000180 static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
181 yaml::ErrorHandler EH);
Sean Silva08a75ae2013-06-20 19:11:44 +0000182};
183} // end anonymous namespace
184
Alex Brachetc22d9662019-08-07 02:44:49 +0000185template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
186 return A.size() * sizeof(T);
187}
188
189template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
190 OS.write((const char *)A.data(), arrayDataSize(A));
191}
192
193template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
194
George Rimar85011022019-09-13 16:00:16 +0000195template <class ELFT>
196ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
197 : Doc(D), ErrHandler(EH) {
George Rimar13a364e2019-07-22 12:01:52 +0000198 StringSet<> DocSections;
199 for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
200 if (!D->Name.empty())
201 DocSections.insert(D->Name);
202
George Rimar1957d682019-07-23 11:03:37 +0000203 // Insert SHT_NULL section implicitly when it is not defined in YAML.
204 if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
205 Doc.Sections.insert(
206 Doc.Sections.begin(),
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000207 std::make_unique<ELFYAML::Section>(
Alex Brachetc22d9662019-08-07 02:44:49 +0000208 ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
George Rimarab658f42019-07-23 07:38:44 +0000209
George Rimar13a364e2019-07-22 12:01:52 +0000210 std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
211 if (!Doc.DynamicSymbols.empty())
212 ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
213
214 // Insert placeholders for implicit sections that are not
215 // defined explicitly in YAML.
216 for (StringRef SecName : ImplicitSections) {
217 if (DocSections.count(SecName))
218 continue;
219
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000220 std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
George Rimar13a364e2019-07-22 12:01:52 +0000221 ELFYAML::Section::SectionKind::RawContent, true /*IsImplicit*/);
222 Sec->Name = SecName;
223 Doc.Sections.push_back(std::move(Sec));
224 }
225}
226
Fangrui Songc3bc6972019-09-05 14:25:57 +0000227template <class ELFT>
228void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000229 using namespace llvm::ELF;
Fangrui Songc3bc6972019-09-05 14:25:57 +0000230
231 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000232 zero(Header);
233 Header.e_ident[EI_MAG0] = 0x7f;
234 Header.e_ident[EI_MAG1] = 'E';
235 Header.e_ident[EI_MAG2] = 'L';
236 Header.e_ident[EI_MAG3] = 'F';
237 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
George Rimara5a0a0f2019-03-07 12:09:19 +0000238 Header.e_ident[EI_DATA] = Doc.Header.Data;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000239 Header.e_ident[EI_VERSION] = EV_CURRENT;
240 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
George Rimar6367d7a2018-12-20 10:43:49 +0000241 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000242 Header.e_type = Doc.Header.Type;
243 Header.e_machine = Doc.Header.Machine;
244 Header.e_version = EV_CURRENT;
245 Header.e_entry = Doc.Header.Entry;
Alex Brachet0b69c592019-09-06 02:27:55 +0000246 Header.e_phoff = Doc.ProgramHeaders.size() ? sizeof(Header) : 0;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000247 Header.e_flags = Doc.Header.Flags;
248 Header.e_ehsize = sizeof(Elf_Ehdr);
Alex Brachet0b69c592019-09-06 02:27:55 +0000249 Header.e_phentsize = Doc.ProgramHeaders.size() ? sizeof(Elf_Phdr) : 0;
Petr Hosekeb04da32017-07-19 20:38:46 +0000250 Header.e_phnum = Doc.ProgramHeaders.size();
George Rimar687d47c2019-06-27 11:08:42 +0000251
252 Header.e_shentsize =
253 Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000254 // Immediately following the ELF header and program headers.
Fangrui Songc3bc6972019-09-05 14:25:57 +0000255 // Align the start of the section header and write the ELF header.
Fangrui Songd20c41d2019-09-06 09:23:17 +0000256 uint64_t SHOff;
257 CBA.getOSAndAlignedOffset(SHOff, sizeof(typename ELFT::uint));
258 Header.e_shoff =
259 Doc.Header.SHOff ? typename ELFT::uint(*Doc.Header.SHOff) : SHOff;
George Rimar687d47c2019-06-27 11:08:42 +0000260 Header.e_shnum =
George Rimar14802292019-07-25 10:19:23 +0000261 Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size();
George Rimar687d47c2019-06-27 11:08:42 +0000262 Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
263 : SN2I.get(".shstrtab");
Fangrui Songc3bc6972019-09-05 14:25:57 +0000264
265 OS.write((const char *)&Header, sizeof(Header));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000266}
267
268template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000269void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
270 for (const auto &YamlPhdr : Doc.ProgramHeaders) {
271 Elf_Phdr Phdr;
272 Phdr.p_type = YamlPhdr.Type;
273 Phdr.p_flags = YamlPhdr.Flags;
274 Phdr.p_vaddr = YamlPhdr.VAddr;
275 Phdr.p_paddr = YamlPhdr.PAddr;
276 PHeaders.push_back(Phdr);
277 }
278}
George Rimar09746882019-05-07 12:10:51 +0000279
George Rimar3212ecf2019-09-09 09:43:03 +0000280template <class ELFT>
281unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
282 StringRef LocSym) {
283 unsigned Index;
284 if (SN2I.lookup(S, Index) || to_integer(S, Index))
285 return Index;
286
287 assert(LocSec.empty() || LocSym.empty());
288 if (!LocSym.empty())
289 reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
290 LocSym + "'");
291 else
292 reportError("unknown section referenced: '" + S + "' by YAML section '" +
293 LocSec + "'");
294 return 0;
295}
296
297template <class ELFT>
298unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec,
299 bool IsDynamic) {
300 const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
301 unsigned Index;
302 // Here we try to look up S in the symbol table. If it is not there,
303 // treat its value as a symbol index.
304 if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) {
305 reportError("unknown symbol referenced: '" + S + "' by YAML section '" +
306 LocSec + "'");
307 return 0;
Xing GUO98228352018-12-04 14:27:51 +0000308 }
George Rimar3212ecf2019-09-09 09:43:03 +0000309 return Index;
Xing GUO98228352018-12-04 14:27:51 +0000310}
311
Petr Hosekeb04da32017-07-19 20:38:46 +0000312template <class ELFT>
George Rimar33b1a0e2019-09-05 08:59:28 +0000313bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
George Rimar66296dc2019-06-05 13:16:53 +0000314 Elf_Shdr &Header, StringRef SecName,
315 ELFYAML::Section *YAMLSec) {
316 // Check if the header was already initialized.
317 if (Header.sh_offset)
318 return false;
319
320 if (SecName == ".symtab")
George Rimar33b1a0e2019-09-05 08:59:28 +0000321 initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000322 else if (SecName == ".strtab")
George Rimar33b1a0e2019-09-05 08:59:28 +0000323 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000324 else if (SecName == ".shstrtab")
George Rimar33b1a0e2019-09-05 08:59:28 +0000325 initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000326 else if (SecName == ".dynsym")
George Rimar33b1a0e2019-09-05 08:59:28 +0000327 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000328 else if (SecName == ".dynstr")
George Rimar33b1a0e2019-09-05 08:59:28 +0000329 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
George Rimar66296dc2019-06-05 13:16:53 +0000330 else
331 return false;
George Rimar9df825f2019-07-02 10:20:12 +0000332
George Rimar86cc7362019-09-02 09:47:17 +0000333 // Override the fields if requested.
George Rimareb41f7f2019-07-11 12:59:29 +0000334 if (YAMLSec) {
George Rimar86cc7362019-09-02 09:47:17 +0000335 if (YAMLSec->ShName)
336 Header.sh_name = *YAMLSec->ShName;
George Rimareb41f7f2019-07-11 12:59:29 +0000337 if (YAMLSec->ShOffset)
338 Header.sh_offset = *YAMLSec->ShOffset;
339 if (YAMLSec->ShSize)
340 Header.sh_size = *YAMLSec->ShSize;
341 }
George Rimar9df825f2019-07-02 10:20:12 +0000342
George Rimar66296dc2019-06-05 13:16:53 +0000343 return true;
344}
345
George Rimarcfc2bcc2019-09-25 12:09:30 +0000346StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
George Rimar60dc5d42019-06-25 08:22:57 +0000347 size_t SuffixPos = S.rfind(" [");
348 if (SuffixPos == StringRef::npos)
349 return S;
350 return S.substr(0, SuffixPos);
351}
352
George Rimar66296dc2019-06-05 13:16:53 +0000353template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000354void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000355 ContiguousBlobAccumulator &CBA) {
356 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
357 // valid SHN_UNDEF entry since SHT_NULL == 0.
George Rimarab658f42019-07-23 07:38:44 +0000358 SHeaders.resize(Doc.Sections.size());
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000359
George Rimar1957d682019-07-23 11:03:37 +0000360 for (size_t I = 0; I < Doc.Sections.size(); ++I) {
George Rimarab658f42019-07-23 07:38:44 +0000361 ELFYAML::Section *Sec = Doc.Sections[I].get();
George Rimar14802292019-07-25 10:19:23 +0000362 if (I == 0 && Sec->IsImplicit)
George Rimar1957d682019-07-23 11:03:37 +0000363 continue;
George Rimar1957d682019-07-23 11:03:37 +0000364
George Rimareb394e92019-06-07 08:31:36 +0000365 // We have a few sections like string or symbol tables that are usually
366 // added implicitly to the end. However, if they are explicitly specified
367 // in the YAML, we need to write them here. This ensures the file offset
368 // remains correct.
George Rimar14802292019-07-25 10:19:23 +0000369 Elf_Shdr &SHeader = SHeaders[I];
George Rimar33b1a0e2019-09-05 08:59:28 +0000370 if (initImplicitHeader(CBA, SHeader, Sec->Name,
George Rimar13a364e2019-07-22 12:01:52 +0000371 Sec->IsImplicit ? nullptr : Sec))
George Rimareb394e92019-06-07 08:31:36 +0000372 continue;
373
374 assert(Sec && "It can't be null unless it is an implicit section. But all "
375 "implicit sections should already have been handled above.");
376
George Rimarcfc2bcc2019-09-25 12:09:30 +0000377 SHeader.sh_name =
378 DotShStrtab.getOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000379 SHeader.sh_type = Sec->Type;
George Rimarcfa1a622019-06-14 11:01:14 +0000380 if (Sec->Flags)
381 SHeader.sh_flags = *Sec->Flags;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000382 SHeader.sh_addr = Sec->Address;
383 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000384
George Rimar3212ecf2019-09-09 09:43:03 +0000385 if (!Sec->Link.empty())
386 SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000387
George Rimar14802292019-07-25 10:19:23 +0000388 if (I == 0) {
389 if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
390 // We do not write any content for special SHN_UNDEF section.
391 if (RawSec->Size)
392 SHeader.sh_size = *RawSec->Size;
393 if (RawSec->Info)
394 SHeader.sh_info = *RawSec->Info;
395 }
396 if (Sec->EntSize)
397 SHeader.sh_entsize = *Sec->EntSize;
398 } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000399 writeSectionContent(SHeader, *S, CBA);
George Rimard3963052019-08-08 09:49:05 +0000400 } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000401 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000402 } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000403 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000404 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000405 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000406 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000407 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000408 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
Simon Atanasyan5db02762015-07-03 23:00:54 +0000409 SHeader.sh_entsize = 0;
410 SHeader.sh_size = S->Size;
411 // SHT_NOBITS section does not have content
412 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000413 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimareb394e92019-06-07 08:31:36 +0000414 } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000415 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000416 } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000417 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000418 } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000419 writeSectionContent(SHeader, *S, CBA);
George Rimareb394e92019-06-07 08:31:36 +0000420 } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000421 writeSectionContent(SHeader, *S, CBA);
George Rimar1a219aa2019-09-24 14:22:37 +0000422 } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
George Rimare5163eb2019-10-01 09:45:59 +0000423 writeSectionContent(SHeader, *S, CBA);
424 } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
425 writeSectionContent(SHeader, *S, CBA);
George Rimar3212ecf2019-09-09 09:43:03 +0000426 } else {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000427 llvm_unreachable("Unknown section type");
George Rimar3212ecf2019-09-09 09:43:03 +0000428 }
George Rimar9df825f2019-07-02 10:20:12 +0000429
George Rimar86cc7362019-09-02 09:47:17 +0000430 // Override the fields if requested.
George Rimareb41f7f2019-07-11 12:59:29 +0000431 if (Sec) {
George Rimar86cc7362019-09-02 09:47:17 +0000432 if (Sec->ShName)
433 SHeader.sh_name = *Sec->ShName;
George Rimareb41f7f2019-07-11 12:59:29 +0000434 if (Sec->ShOffset)
435 SHeader.sh_offset = *Sec->ShOffset;
436 if (Sec->ShSize)
437 SHeader.sh_size = *Sec->ShSize;
438 }
George Rimar66296dc2019-06-05 13:16:53 +0000439 }
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000440}
441
George Rimar6da44ad2019-04-03 14:53:42 +0000442static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
443 for (size_t I = 0; I < Symbols.size(); ++I)
444 if (Symbols[I].Binding.value != ELF::STB_LOCAL)
445 return I;
446 return Symbols.size();
447}
448
George Rimarf3024362019-09-25 11:40:11 +0000449static uint64_t writeContent(raw_ostream &OS,
450 const Optional<yaml::BinaryRef> &Content,
451 const Optional<llvm::yaml::Hex64> &Size) {
George Rimar1e410072019-06-10 12:43:18 +0000452 size_t ContentSize = 0;
George Rimarf3024362019-09-25 11:40:11 +0000453 if (Content) {
454 Content->writeAsBinary(OS);
455 ContentSize = Content->binary_size();
George Rimar1e410072019-06-10 12:43:18 +0000456 }
457
George Rimarf3024362019-09-25 11:40:11 +0000458 if (!Size)
George Rimar1e410072019-06-10 12:43:18 +0000459 return ContentSize;
460
George Rimarf3024362019-09-25 11:40:11 +0000461 OS.write_zeros(*Size - ContentSize);
462 return *Size;
George Rimar1e410072019-06-10 12:43:18 +0000463}
464
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000465template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000466std::vector<typename ELFT::Sym>
467ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
468 const StringTableBuilder &Strtab) {
George Rimar30ea0c42019-06-20 14:44:48 +0000469 std::vector<Elf_Sym> Ret;
470 Ret.resize(Symbols.size() + 1);
471
472 size_t I = 0;
473 for (const auto &Sym : Symbols) {
474 Elf_Sym &Symbol = Ret[++I];
475
476 // If NameIndex, which contains the name offset, is explicitly specified, we
477 // use it. This is useful for preparing broken objects. Otherwise, we add
478 // the specified Name to the string table builder to get its offset.
479 if (Sym.NameIndex)
480 Symbol.st_name = *Sym.NameIndex;
481 else if (!Sym.Name.empty())
George Rimarcfc2bcc2019-09-25 12:09:30 +0000482 Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
George Rimar30ea0c42019-06-20 14:44:48 +0000483
484 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
George Rimar3212ecf2019-09-09 09:43:03 +0000485 if (!Sym.Section.empty())
486 Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name);
487 else if (Sym.Index)
George Rimar30ea0c42019-06-20 14:44:48 +0000488 Symbol.st_shndx = *Sym.Index;
George Rimar3212ecf2019-09-09 09:43:03 +0000489
George Rimar30ea0c42019-06-20 14:44:48 +0000490 Symbol.st_value = Sym.Value;
George Rimar4e717022019-08-30 13:39:22 +0000491 Symbol.st_other = Sym.Other ? *Sym.Other : 0;
George Rimar30ea0c42019-06-20 14:44:48 +0000492 Symbol.st_size = Sym.Size;
493 }
494
495 return Ret;
496}
497
498template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000499void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
Dave Lee67b49662017-11-16 18:10:15 +0000500 SymtabType STType,
George Rimar66296dc2019-06-05 13:16:53 +0000501 ContiguousBlobAccumulator &CBA,
502 ELFYAML::Section *YAMLSec) {
George Rimar1e410072019-06-10 12:43:18 +0000503
Dave Lee67b49662017-11-16 18:10:15 +0000504 bool IsStatic = STType == SymtabType::Static;
George Rimar1e410072019-06-10 12:43:18 +0000505 const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
506
507 ELFYAML::RawContentSection *RawSec =
508 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
509 if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) {
510 if (RawSec->Content)
George Rimar3212ecf2019-09-09 09:43:03 +0000511 reportError("cannot specify both `Content` and " +
512 (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) +
513 " for symbol table section '" + RawSec->Name + "'");
George Rimar1e410072019-06-10 12:43:18 +0000514 if (RawSec->Size)
George Rimar3212ecf2019-09-09 09:43:03 +0000515 reportError("cannot specify both `Size` and " +
516 (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) +
517 " for symbol table section '" + RawSec->Name + "'");
518 return;
George Rimar1e410072019-06-10 12:43:18 +0000519 }
520
521 zero(SHeader);
Dave Lee67b49662017-11-16 18:10:15 +0000522 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
George Rimar0aecaba2019-06-14 14:25:34 +0000523
524 if (YAMLSec)
525 SHeader.sh_type = YAMLSec->Type;
526 else
527 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
George Rimar379aa182019-06-10 11:38:06 +0000528
George Rimarffb3c722019-06-11 10:00:51 +0000529 if (RawSec && !RawSec->Link.empty()) {
530 // If the Link field is explicitly defined in the document,
531 // we should use it.
George Rimar3212ecf2019-09-09 09:43:03 +0000532 SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name);
George Rimarffb3c722019-06-11 10:00:51 +0000533 } else {
534 // When we describe the .dynsym section in the document explicitly, it is
535 // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
536 // added implicitly and we should be able to leave the Link zeroed if
537 // .dynstr is not defined.
538 unsigned Link = 0;
539 if (IsStatic)
540 Link = SN2I.get(".strtab");
541 else
542 SN2I.lookup(".dynstr", Link);
543 SHeader.sh_link = Link;
544 }
George Rimar379aa182019-06-10 11:38:06 +0000545
George Rimarcfa1a622019-06-14 11:01:14 +0000546 if (YAMLSec && YAMLSec->Flags)
547 SHeader.sh_flags = *YAMLSec->Flags;
548 else if (!IsStatic)
549 SHeader.sh_flags = ELF::SHF_ALLOC;
George Rimarc1da1492019-04-26 12:45:54 +0000550
George Rimara7ba1a02019-03-01 10:18:16 +0000551 // If the symbol table section is explicitly described in the YAML
552 // then we should set the fields requested.
George Rimarb6e20932019-06-19 08:57:38 +0000553 SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
554 : findFirstNonGlobal(Symbols) + 1;
George Rimar66296dc2019-06-05 13:16:53 +0000555 SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
556 ? (uint64_t)(*YAMLSec->EntSize)
557 : sizeof(Elf_Sym);
558 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
559 SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
560
George Rimar1e410072019-06-10 12:43:18 +0000561 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
562 if (RawSec && (RawSec->Content || RawSec->Size)) {
563 assert(Symbols.empty());
George Rimarf3024362019-09-25 11:40:11 +0000564 SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size);
George Rimar1e410072019-06-10 12:43:18 +0000565 return;
George Rimar6947acd2019-02-19 12:15:04 +0000566 }
George Rimar1e410072019-06-10 12:43:18 +0000567
George Rimar30ea0c42019-06-20 14:44:48 +0000568 std::vector<Elf_Sym> Syms =
George Rimar3212ecf2019-09-09 09:43:03 +0000569 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
George Rimar1e410072019-06-10 12:43:18 +0000570 writeArrayData(OS, makeArrayRef(Syms));
571 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000572}
573
574template <class ELFT>
575void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
576 StringTableBuilder &STB,
George Rimar66296dc2019-06-05 13:16:53 +0000577 ContiguousBlobAccumulator &CBA,
578 ELFYAML::Section *YAMLSec) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000579 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000580 SHeader.sh_name = DotShStrtab.getOffset(Name);
George Rimar0aecaba2019-06-14 14:25:34 +0000581 SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
George Rimar66296dc2019-06-05 13:16:53 +0000582 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
583
584 ELFYAML::RawContentSection *RawSec =
585 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
George Rimar1e410072019-06-10 12:43:18 +0000586
587 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
588 if (RawSec && (RawSec->Content || RawSec->Size)) {
George Rimarf3024362019-09-25 11:40:11 +0000589 SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size);
George Rimar66296dc2019-06-05 13:16:53 +0000590 } else {
George Rimar1e410072019-06-10 12:43:18 +0000591 STB.write(OS);
George Rimar66296dc2019-06-05 13:16:53 +0000592 SHeader.sh_size = STB.getSize();
593 }
594
595 if (YAMLSec && YAMLSec->EntSize)
596 SHeader.sh_entsize = *YAMLSec->EntSize;
George Rimar6947acd2019-02-19 12:15:04 +0000597
George Rimarb6e20932019-06-19 08:57:38 +0000598 if (RawSec && RawSec->Info)
599 SHeader.sh_info = *RawSec->Info;
600
George Rimarcfa1a622019-06-14 11:01:14 +0000601 if (YAMLSec && YAMLSec->Flags)
602 SHeader.sh_flags = *YAMLSec->Flags;
603 else if (Name == ".dynstr")
604 SHeader.sh_flags = ELF::SHF_ALLOC;
605
George Rimar43f62ff2019-06-14 11:13:32 +0000606 // If the section is explicitly described in the YAML
George Rimar6947acd2019-02-19 12:15:04 +0000607 // then we want to use its section address.
George Rimar43f62ff2019-06-14 11:13:32 +0000608 if (YAMLSec)
George Rimarcfa1a622019-06-14 11:01:14 +0000609 SHeader.sh_addr = YAMLSec->Address;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000610}
611
George Rimar3212ecf2019-09-09 09:43:03 +0000612template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
George Rimar85011022019-09-13 16:00:16 +0000613 ErrHandler(Msg);
George Rimar3212ecf2019-09-09 09:43:03 +0000614 HasError = true;
615}
616
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000617template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000618void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
619 std::vector<Elf_Shdr> &SHeaders) {
620 uint32_t PhdrIdx = 0;
621 for (auto &YamlPhdr : Doc.ProgramHeaders) {
George Rimarf5345a32019-05-01 09:45:55 +0000622 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
623
Puyan Lotfia10f0162019-05-11 17:03:36 +0000624 std::vector<Elf_Shdr *> Sections;
625 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
626 unsigned Index;
627 if (!SN2I.lookup(SecName.Section, Index)) {
George Rimar3212ecf2019-09-09 09:43:03 +0000628 reportError("unknown section referenced: '" + SecName.Section +
629 "' by program header");
630 continue;
George Rimarf5345a32019-05-01 09:45:55 +0000631 }
632 Sections.push_back(&SHeaders[Index]);
633 }
Petr Hosekeb04da32017-07-19 20:38:46 +0000634
James Hendersonb10f48b2019-03-15 10:35:27 +0000635 if (YamlPhdr.Offset) {
636 PHeader.p_offset = *YamlPhdr.Offset;
637 } else {
638 if (YamlPhdr.Sections.size())
639 PHeader.p_offset = UINT32_MAX;
Petr Hosekeb04da32017-07-19 20:38:46 +0000640 else
James Hendersonb10f48b2019-03-15 10:35:27 +0000641 PHeader.p_offset = 0;
642
643 // Find the minimum offset for the program header.
George Rimarf5345a32019-05-01 09:45:55 +0000644 for (Elf_Shdr *SHeader : Sections)
645 PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
Petr Hosekeb04da32017-07-19 20:38:46 +0000646 }
647
Fangrui Songc28f3e62019-09-09 16:45:17 +0000648 // Find the maximum offset of the end of a section in order to set p_filesz
649 // and p_memsz. When setting p_filesz, trailing SHT_NOBITS sections are not
650 // counted.
651 uint64_t FileOffset = PHeader.p_offset, MemOffset = PHeader.p_offset;
652 for (Elf_Shdr *SHeader : Sections) {
653 uint64_t End = SHeader->sh_offset + SHeader->sh_size;
654 MemOffset = std::max(MemOffset, End);
655
656 if (SHeader->sh_type != llvm::ELF::SHT_NOBITS)
657 FileOffset = std::max(FileOffset, End);
James Hendersonb10f48b2019-03-15 10:35:27 +0000658 }
659
Fangrui Songc28f3e62019-09-09 16:45:17 +0000660 // Set the file size and the memory size if not set explicitly.
661 PHeader.p_filesz = YamlPhdr.FileSize ? uint64_t(*YamlPhdr.FileSize)
662 : FileOffset - PHeader.p_offset;
663 PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
664 : MemOffset - PHeader.p_offset;
Petr Hosekeb04da32017-07-19 20:38:46 +0000665
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000666 if (YamlPhdr.Align) {
667 PHeader.p_align = *YamlPhdr.Align;
668 } else {
Fangrui Song1da4f472019-09-10 09:16:34 +0000669 // Set the alignment of the segment to be the maximum alignment of the
670 // sections so that by default the segment has a valid and sensible
671 // alignment.
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000672 PHeader.p_align = 1;
George Rimarf5345a32019-05-01 09:45:55 +0000673 for (Elf_Shdr *SHeader : Sections)
Fangrui Song1da4f472019-09-10 09:16:34 +0000674 PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign);
Petr Hosekeb04da32017-07-19 20:38:46 +0000675 }
676 }
677}
678
679template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000680void ELFState<ELFT>::writeSectionContent(
George Rimarb49e1922019-04-24 13:02:15 +0000681 Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
682 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000683 raw_ostream &OS =
684 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimarf3024362019-09-25 11:40:11 +0000685 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
George Rimarb49e1922019-04-24 13:02:15 +0000686
George Rimar65a68282018-08-07 08:11:38 +0000687 if (Section.EntSize)
688 SHeader.sh_entsize = *Section.EntSize;
689 else if (Section.Type == llvm::ELF::SHT_RELR)
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000690 SHeader.sh_entsize = sizeof(Elf_Relr);
691 else
692 SHeader.sh_entsize = 0;
George Rimarb6e20932019-06-19 08:57:38 +0000693
694 if (Section.Info)
695 SHeader.sh_info = *Section.Info;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000696}
697
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000698static bool isMips64EL(const ELFYAML::Object &Doc) {
699 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
700 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
701 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
702}
703
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000704template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000705void ELFState<ELFT>::writeSectionContent(
Alex Brachetc22d9662019-08-07 02:44:49 +0000706 Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
707 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000708 assert((Section.Type == llvm::ELF::SHT_REL ||
709 Section.Type == llvm::ELF::SHT_RELA) &&
710 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000711
712 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
713 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
714 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
715
George Rimarda1b3ab2019-04-26 12:15:32 +0000716 // For relocation section set link to .symtab by default.
717 if (Section.Link.empty())
George Rimard71017b2019-06-10 09:57:29 +0000718 SHeader.sh_link = SN2I.get(".symtab");
George Rimarda1b3ab2019-04-26 12:15:32 +0000719
George Rimar3212ecf2019-09-09 09:43:03 +0000720 if (!Section.RelocatableSec.empty())
721 SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
George Rimarda1b3ab2019-04-26 12:15:32 +0000722
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000723 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000724 for (const auto &Rel : Section.Relocations) {
George Rimar3212ecf2019-09-09 09:43:03 +0000725 unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name,
726 Section.Link == ".dynsym")
727 : 0;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000728 if (IsRela) {
729 Elf_Rela REntry;
730 zero(REntry);
731 REntry.r_offset = Rel.Offset;
732 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000733 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000734 OS.write((const char *)&REntry, sizeof(REntry));
735 } else {
736 Elf_Rel REntry;
737 zero(REntry);
738 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000739 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000740 OS.write((const char *)&REntry, sizeof(REntry));
741 }
742 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000743}
744
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000745template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000746void ELFState<ELFT>::writeSectionContent(
George Rimard3963052019-08-08 09:49:05 +0000747 Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
748 ContiguousBlobAccumulator &CBA) {
749 raw_ostream &OS =
750 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
751
752 for (uint32_t E : Shndx.Entries)
753 support::endian::write<uint32_t>(OS, E, ELFT::TargetEndianness);
754
755 SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4;
756 SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize;
George Rimard3963052019-08-08 09:49:05 +0000757}
758
759template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000760void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000761 const ELFYAML::Group &Section,
762 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000763 assert(Section.Type == llvm::ELF::SHT_GROUP &&
764 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000765
George Rimard063c7d2019-02-20 13:58:43 +0000766 SHeader.sh_entsize = 4;
George Rimar09746882019-05-07 12:10:51 +0000767 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
George Rimar3212ecf2019-09-09 09:43:03 +0000768 SHeader.sh_info =
769 toSymbolIndex(Section.Signature, Section.Name, /*IsDynamic=*/false);
George Rimarda1b3ab2019-04-26 12:15:32 +0000770
George Rimard063c7d2019-02-20 13:58:43 +0000771 raw_ostream &OS =
772 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000773
George Rimarfb7780a2019-04-26 12:20:51 +0000774 for (const ELFYAML::SectionOrType &Member : Section.Members) {
775 unsigned int SectionIndex = 0;
776 if (Member.sectionNameOrType == "GRP_COMDAT")
777 SectionIndex = llvm::ELF::GRP_COMDAT;
George Rimar3212ecf2019-09-09 09:43:03 +0000778 else
779 SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
George Rimarfb7780a2019-04-26 12:20:51 +0000780 support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000781 }
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000782}
783
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000784template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000785void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar646af082019-02-19 15:29:07 +0000786 const ELFYAML::SymverSection &Section,
787 ContiguousBlobAccumulator &CBA) {
George Rimar646af082019-02-19 15:29:07 +0000788 raw_ostream &OS =
789 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimardac37fb2019-02-20 14:01:02 +0000790 for (uint16_t Version : Section.Entries)
791 support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
George Rimar646af082019-02-19 15:29:07 +0000792
George Rimar149aa2f2019-08-05 13:54:35 +0000793 SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
George Rimard063c7d2019-02-20 13:58:43 +0000794 SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
George Rimar646af082019-02-19 15:29:07 +0000795}
796
797template <class ELFT>
George Rimar1a219aa2019-09-24 14:22:37 +0000798void ELFState<ELFT>::writeSectionContent(
799 Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section,
800 ContiguousBlobAccumulator &CBA) {
801 using uintX_t = typename ELFT::uint;
802 raw_ostream &OS =
803 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
804
George Rimarf3024362019-09-25 11:40:11 +0000805 if (Section.Content || Section.Size) {
806 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
George Rimar1a219aa2019-09-24 14:22:37 +0000807 return;
808 }
809
810 for (const ELFYAML::StackSizeEntry &E : *Section.Entries) {
811 support::endian::write<uintX_t>(OS, E.Address, ELFT::TargetEndianness);
812 SHeader.sh_size += sizeof(uintX_t) + encodeULEB128(E.Size, OS);
813 }
814}
815
816template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000817void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimare5163eb2019-10-01 09:45:59 +0000818 const ELFYAML::HashSection &Section,
819 ContiguousBlobAccumulator &CBA) {
820 raw_ostream &OS =
821 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
822
823 unsigned Link = 0;
George Rimar0210a1a2019-10-01 09:54:40 +0000824 if (Section.Link.empty() && SN2I.lookup(".dynsym", Link))
George Rimare5163eb2019-10-01 09:45:59 +0000825 SHeader.sh_link = Link;
826
George Rimar6fa696f2019-10-02 13:52:37 +0000827 if (Section.Content || Section.Size) {
828 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
George Rimare5163eb2019-10-01 09:45:59 +0000829 return;
830 }
831
832 support::endian::write<uint32_t>(OS, Section.Bucket->size(),
833 ELFT::TargetEndianness);
834 support::endian::write<uint32_t>(OS, Section.Chain->size(),
835 ELFT::TargetEndianness);
836 for (uint32_t Val : *Section.Bucket)
837 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
838 for (uint32_t Val : *Section.Chain)
839 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
840
841 SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4;
842}
843
844template <class ELFT>
845void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar623ae722019-02-21 12:21:43 +0000846 const ELFYAML::VerdefSection &Section,
847 ContiguousBlobAccumulator &CBA) {
848 typedef typename ELFT::Verdef Elf_Verdef;
849 typedef typename ELFT::Verdaux Elf_Verdaux;
850 raw_ostream &OS =
851 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
852
853 uint64_t AuxCnt = 0;
854 for (size_t I = 0; I < Section.Entries.size(); ++I) {
855 const ELFYAML::VerdefEntry &E = Section.Entries[I];
856
857 Elf_Verdef VerDef;
858 VerDef.vd_version = E.Version;
859 VerDef.vd_flags = E.Flags;
860 VerDef.vd_ndx = E.VersionNdx;
861 VerDef.vd_hash = E.Hash;
862 VerDef.vd_aux = sizeof(Elf_Verdef);
863 VerDef.vd_cnt = E.VerNames.size();
864 if (I == Section.Entries.size() - 1)
865 VerDef.vd_next = 0;
866 else
867 VerDef.vd_next =
868 sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
869 OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
870
871 for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
872 Elf_Verdaux VernAux;
873 VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
874 if (J == E.VerNames.size() - 1)
875 VernAux.vda_next = 0;
876 else
877 VernAux.vda_next = sizeof(Elf_Verdaux);
878 OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
879 }
880 }
881
882 SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) +
883 AuxCnt * sizeof(Elf_Verdaux);
884 SHeader.sh_info = Section.Info;
George Rimar623ae722019-02-21 12:21:43 +0000885}
886
887template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000888void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar0621b792019-02-19 14:53:48 +0000889 const ELFYAML::VerneedSection &Section,
890 ContiguousBlobAccumulator &CBA) {
George Rimarda1b3ab2019-04-26 12:15:32 +0000891 typedef typename ELFT::Verneed Elf_Verneed;
892 typedef typename ELFT::Vernaux Elf_Vernaux;
George Rimar0621b792019-02-19 14:53:48 +0000893
George Rimarda1b3ab2019-04-26 12:15:32 +0000894 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0621b792019-02-19 14:53:48 +0000895
George Rimarda1b3ab2019-04-26 12:15:32 +0000896 uint64_t AuxCnt = 0;
897 for (size_t I = 0; I < Section.VerneedV.size(); ++I) {
898 const ELFYAML::VerneedEntry &VE = Section.VerneedV[I];
George Rimar0621b792019-02-19 14:53:48 +0000899
George Rimarda1b3ab2019-04-26 12:15:32 +0000900 Elf_Verneed VerNeed;
901 VerNeed.vn_version = VE.Version;
902 VerNeed.vn_file = DotDynstr.getOffset(VE.File);
903 if (I == Section.VerneedV.size() - 1)
904 VerNeed.vn_next = 0;
905 else
906 VerNeed.vn_next =
907 sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
908 VerNeed.vn_cnt = VE.AuxV.size();
909 VerNeed.vn_aux = sizeof(Elf_Verneed);
910 OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
George Rimar0621b792019-02-19 14:53:48 +0000911
George Rimarda1b3ab2019-04-26 12:15:32 +0000912 for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
913 const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
George Rimar0621b792019-02-19 14:53:48 +0000914
George Rimarda1b3ab2019-04-26 12:15:32 +0000915 Elf_Vernaux VernAux;
916 VernAux.vna_hash = VAuxE.Hash;
917 VernAux.vna_flags = VAuxE.Flags;
918 VernAux.vna_other = VAuxE.Other;
919 VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
920 if (J == VE.AuxV.size() - 1)
921 VernAux.vna_next = 0;
922 else
923 VernAux.vna_next = sizeof(Elf_Vernaux);
924 OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
925 }
926 }
George Rimar0621b792019-02-19 14:53:48 +0000927
George Rimarda1b3ab2019-04-26 12:15:32 +0000928 SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) +
929 AuxCnt * sizeof(Elf_Vernaux);
930 SHeader.sh_info = Section.Info;
George Rimar0621b792019-02-19 14:53:48 +0000931}
932
933template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000934void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000935 const ELFYAML::MipsABIFlags &Section,
936 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000937 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
938 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000939
940 object::Elf_Mips_ABIFlags<ELFT> Flags;
941 zero(Flags);
942 SHeader.sh_entsize = sizeof(Flags);
943 SHeader.sh_size = SHeader.sh_entsize;
944
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000945 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000946 Flags.version = Section.Version;
947 Flags.isa_level = Section.ISALevel;
948 Flags.isa_rev = Section.ISARevision;
949 Flags.gpr_size = Section.GPRSize;
950 Flags.cpr1_size = Section.CPR1Size;
951 Flags.cpr2_size = Section.CPR2Size;
952 Flags.fp_abi = Section.FpABI;
953 Flags.isa_ext = Section.ISAExtension;
954 Flags.ases = Section.ASEs;
955 Flags.flags1 = Section.Flags1;
956 Flags.flags2 = Section.Flags2;
957 OS.write((const char *)&Flags, sizeof(Flags));
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000958}
959
George Rimar0e7ed912019-02-09 11:34:28 +0000960template <class ELFT>
George Rimar3212ecf2019-09-09 09:43:03 +0000961void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
George Rimar0e7ed912019-02-09 11:34:28 +0000962 const ELFYAML::DynamicSection &Section,
963 ContiguousBlobAccumulator &CBA) {
George Rimard063c7d2019-02-20 13:58:43 +0000964 typedef typename ELFT::uint uintX_t;
965
George Rimar0e7ed912019-02-09 11:34:28 +0000966 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
967 "Section type is not SHT_DYNAMIC");
968
George Rimar3212ecf2019-09-09 09:43:03 +0000969 if (!Section.Entries.empty() && Section.Content)
970 reportError("cannot specify both raw content and explicit entries "
971 "for dynamic section '" +
972 Section.Name + "'");
James Hendersonfd997802019-02-25 11:02:24 +0000973
974 if (Section.Content)
975 SHeader.sh_size = Section.Content->binary_size();
976 else
977 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
George Rimar0e7ed912019-02-09 11:34:28 +0000978 if (Section.EntSize)
979 SHeader.sh_entsize = *Section.EntSize;
980 else
981 SHeader.sh_entsize = sizeof(Elf_Dyn);
982
Alex Brachetc22d9662019-08-07 02:44:49 +0000983 raw_ostream &OS =
984 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
George Rimar0e7ed912019-02-09 11:34:28 +0000985 for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
George Rimard063c7d2019-02-20 13:58:43 +0000986 support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
987 support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
George Rimar0e7ed912019-02-09 11:34:28 +0000988 }
James Hendersonfd997802019-02-25 11:02:24 +0000989 if (Section.Content)
990 Section.Content->writeAsBinary(OS);
George Rimar0e7ed912019-02-09 11:34:28 +0000991}
992
George Rimar3212ecf2019-09-09 09:43:03 +0000993template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
George Rimar14802292019-07-25 10:19:23 +0000994 for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) {
George Rimarab658f42019-07-23 07:38:44 +0000995 StringRef Name = Doc.Sections[I]->Name;
George Rimar14802292019-07-25 10:19:23 +0000996 if (Name.empty())
997 continue;
998
George Rimarcfc2bcc2019-09-25 12:09:30 +0000999 DotShStrtab.add(ELFYAML::dropUniqueSuffix(Name));
George Rimar3212ecf2019-09-09 09:43:03 +00001000 if (!SN2I.addName(Name, I))
1001 reportError("repeated section name: '" + Name +
1002 "' at YAML section number " + Twine(I));
Sean Silvaaff51252013-06-21 00:27:50 +00001003 }
Dave Lee17307d92017-11-09 14:53:43 +00001004
Dave Lee17307d92017-11-09 14:53:43 +00001005 DotShStrtab.finalize();
Sean Silva6b083882013-06-18 23:14:03 +00001006}
1007
George Rimar3212ecf2019-09-09 09:43:03 +00001008template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
1009 auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) {
1010 for (size_t I = 0, S = V.size(); I < S; ++I) {
1011 const ELFYAML::Symbol &Sym = V[I];
1012 if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1))
1013 reportError("repeated symbol name: '" + Sym.Name + "'");
1014 }
1015 };
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001016
George Rimar3212ecf2019-09-09 09:43:03 +00001017 Build(Doc.Symbols, SymN2I);
1018 Build(Doc.DynamicSymbols, DynSymN2I);
George Rimar91208442019-08-22 12:39:56 +00001019}
1020
George Rimar0621b792019-02-19 14:53:48 +00001021template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
George Rimar0621b792019-02-19 14:53:48 +00001022 // Add the regular symbol names to .strtab section.
George Rimar6da44ad2019-04-03 14:53:42 +00001023 for (const ELFYAML::Symbol &Sym : Doc.Symbols)
George Rimarcfc2bcc2019-09-25 12:09:30 +00001024 DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
George Rimar0621b792019-02-19 14:53:48 +00001025 DotStrtab.finalize();
1026
George Rimar0621b792019-02-19 14:53:48 +00001027 // Add the dynamic symbol names to .dynstr section.
George Rimar6da44ad2019-04-03 14:53:42 +00001028 for (const ELFYAML::Symbol &Sym : Doc.DynamicSymbols)
George Rimarcfc2bcc2019-09-25 12:09:30 +00001029 DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name));
George Rimar0621b792019-02-19 14:53:48 +00001030
George Rimar623ae722019-02-21 12:21:43 +00001031 // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1032 // add strings to .dynstr section.
George Rimar0621b792019-02-19 14:53:48 +00001033 for (const std::unique_ptr<ELFYAML::Section> &Sec : Doc.Sections) {
George Rimar623ae722019-02-21 12:21:43 +00001034 if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
1035 for (const ELFYAML::VerneedEntry &VE : VerNeed->VerneedV) {
1036 DotDynstr.add(VE.File);
1037 for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1038 DotDynstr.add(Aux.Name);
1039 }
1040 } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec.get())) {
1041 for (const ELFYAML::VerdefEntry &E : VerDef->Entries)
1042 for (StringRef Name : E.VerNames)
1043 DotDynstr.add(Name);
George Rimar0621b792019-02-19 14:53:48 +00001044 }
1045 }
1046
1047 DotDynstr.finalize();
1048}
1049
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001050template <class ELFT>
George Rimar85011022019-09-13 16:00:16 +00001051bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
1052 yaml::ErrorHandler EH) {
1053 ELFState<ELFT> State(Doc, EH);
George Rimar0621b792019-02-19 14:53:48 +00001054
1055 // Finalize .strtab and .dynstr sections. We do that early because want to
1056 // finalize the string table builders before writing the content of the
1057 // sections that might want to use them.
1058 State.finalizeStrings();
1059
George Rimar3212ecf2019-09-09 09:43:03 +00001060 State.buildSectionIndex();
1061 State.buildSymbolIndexes();
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +00001062
Petr Hosekeb04da32017-07-19 20:38:46 +00001063 std::vector<Elf_Phdr> PHeaders;
1064 State.initProgramHeaders(PHeaders);
Sean Silva38205932013-06-13 22:19:48 +00001065
Sean Silva08a75ae2013-06-20 19:11:44 +00001066 // XXX: This offset is tightly coupled with the order that we write
1067 // things to `OS`.
Fangrui Songc3bc6972019-09-05 14:25:57 +00001068 const size_t SectionContentBeginOffset =
1069 sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
Sean Silva08a75ae2013-06-20 19:11:44 +00001070 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +00001071
Sean Silva38205932013-06-13 22:19:48 +00001072 std::vector<Elf_Shdr> SHeaders;
George Rimar3212ecf2019-09-09 09:43:03 +00001073 State.initSectionHeaders(SHeaders, CBA);
Sean Silva38205932013-06-13 22:19:48 +00001074
Petr Hosekeb04da32017-07-19 20:38:46 +00001075 // Now we can decide segment offsets
1076 State.setProgramHeaderLayout(PHeaders, SHeaders);
1077
George Rimar3212ecf2019-09-09 09:43:03 +00001078 if (State.HasError)
George Rimar7da559f2019-09-13 09:12:38 +00001079 return false;
George Rimar3212ecf2019-09-09 09:43:03 +00001080
Fangrui Songc3bc6972019-09-05 14:25:57 +00001081 State.writeELFHeader(CBA, OS);
Petr Hosekeb04da32017-07-19 20:38:46 +00001082 writeArrayData(OS, makeArrayRef(PHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +00001083 CBA.writeBlobToStream(OS);
Fangrui Songc3bc6972019-09-05 14:25:57 +00001084 writeArrayData(OS, makeArrayRef(SHeaders));
George Rimar7da559f2019-09-13 09:12:38 +00001085 return true;
Sean Silvaf99309c2013-06-10 23:44:15 +00001086}
1087
Alex Brachetc22d9662019-08-07 02:44:49 +00001088namespace llvm {
1089namespace yaml {
1090
George Rimar85011022019-09-13 16:00:16 +00001091bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
George Rimarbc4d3c42019-04-29 12:25:01 +00001092 bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1093 bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1094 if (Is64Bit) {
1095 if (IsLE)
George Rimar85011022019-09-13 16:00:16 +00001096 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH);
1097 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH);
Sean Silvaf99309c2013-06-10 23:44:15 +00001098 }
George Rimarbc4d3c42019-04-29 12:25:01 +00001099 if (IsLE)
George Rimar85011022019-09-13 16:00:16 +00001100 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH);
1101 return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH);
Sean Silvaf99309c2013-06-10 23:44:15 +00001102}
Alex Brachetc22d9662019-08-07 02:44:49 +00001103
1104} // namespace yaml
1105} // namespace llvm