35 class ContiguousBlobAccumulator {
36 const uint64_t InitialOffset;
41 ContiguousBlobAccumulator(uint64_t InitialOffset_)
42 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
44 template <
class Integer>
46 Offset = padToAlignment(Align);
51 uint64_t padToAlignment(
unsigned Align) {
54 uint64_t CurrentOffset = InitialOffset + OS.
tell();
55 uint64_t AlignedOffset =
alignTo(CurrentOffset, Align);
75 auto I = Map.
find(Name);
86 assert(
false &&
"Expected section not found in index");
89 unsigned size()
const {
return Map.
size(); }
104 template <
class ELFT>
class ELFState {
105 typedef typename ELFT::Ehdr Elf_Ehdr;
106 typedef typename ELFT::Phdr Elf_Phdr;
107 typedef typename ELFT::Shdr Elf_Shdr;
108 typedef typename ELFT::Sym Elf_Sym;
109 typedef typename ELFT::Rel Elf_Rel;
110 typedef typename ELFT::Rela Elf_Rela;
111 typedef typename ELFT::Relr Elf_Relr;
112 typedef typename ELFT::Dyn Elf_Dyn;
113 typedef typename ELFT::uint uintX_t;
115 enum class SymtabType {
Static, Dynamic };
128 NameToIdxMap DynSymN2I;
131 bool HasError =
false;
140 void buildSectionIndex();
141 void buildSymbolIndexes();
142 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
143 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
145 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
146 ContiguousBlobAccumulator &CBA);
147 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
148 ContiguousBlobAccumulator &CBA,
150 void initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
152 ContiguousBlobAccumulator &CBA,
154 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
155 std::vector<Elf_Shdr> &SHeaders);
157 std::vector<Fragment>
161 void finalizeStrings();
162 void writeELFHeader(ContiguousBlobAccumulator &CBA,
raw_ostream &OS);
163 void writeSectionContent(Elf_Shdr &SHeader,
165 ContiguousBlobAccumulator &CBA);
166 void writeSectionContent(Elf_Shdr &SHeader,
168 ContiguousBlobAccumulator &CBA);
169 void writeSectionContent(Elf_Shdr &SHeader,
171 ContiguousBlobAccumulator &CBA);
172 void writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::Group &Group,
173 ContiguousBlobAccumulator &CBA);
174 void writeSectionContent(Elf_Shdr &SHeader,
176 ContiguousBlobAccumulator &CBA);
177 void writeSectionContent(Elf_Shdr &SHeader,
179 ContiguousBlobAccumulator &CBA);
180 void writeSectionContent(Elf_Shdr &SHeader,
182 ContiguousBlobAccumulator &CBA);
183 void writeSectionContent(Elf_Shdr &SHeader,
185 ContiguousBlobAccumulator &CBA);
186 void writeSectionContent(Elf_Shdr &SHeader,
188 ContiguousBlobAccumulator &CBA);
189 void writeSectionContent(Elf_Shdr &SHeader,
191 ContiguousBlobAccumulator &CBA);
192 void writeSectionContent(Elf_Shdr &SHeader,
194 ContiguousBlobAccumulator &CBA);
195 void writeSectionContent(Elf_Shdr &SHeader,
197 ContiguousBlobAccumulator &CBA);
198 void writeSectionContent(Elf_Shdr &SHeader,
200 ContiguousBlobAccumulator &CBA);
201 void writeSectionContent(Elf_Shdr &SHeader,
203 ContiguousBlobAccumulator &CBA);
204 void writeSectionContent(Elf_Shdr &SHeader,
206 ContiguousBlobAccumulator &CBA);
207 void writeSectionContent(Elf_Shdr &SHeader,
209 ContiguousBlobAccumulator &CBA);
210 void writeSectionContent(Elf_Shdr &SHeader,
212 ContiguousBlobAccumulator &CBA);
214 void writeFill(
ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
225 return A.
size() *
sizeof(
T);
232 template <
class T>
static void zero(
T &Obj) { memset(&Obj, 0,
sizeof(Obj)); }
234 template <
class ELFT>
236 : Doc(D), ErrHandler(EH) {
237 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
240 if (!Sec->Name.empty())
241 DocSections.
insert(Sec->Name);
244 if (Sections.empty() || Sections.front()->Type !=
ELF::SHT_NULL)
247 std::make_unique<ELFYAML::Section>(
250 std::vector<StringRef> ImplicitSections;
252 ImplicitSections.push_back(
".symtab");
253 ImplicitSections.insert(ImplicitSections.end(), {
".strtab",
".shstrtab"});
255 if (Doc.DynamicSymbols)
256 ImplicitSections.insert(ImplicitSections.end(), {
".dynsym",
".dynstr"});
260 for (
StringRef SecName : ImplicitSections) {
261 if (DocSections.
count(SecName))
264 std::unique_ptr<ELFYAML::Chunk> Sec = std::make_unique<ELFYAML::Section>(
267 Doc.Chunks.push_back(std::move(Sec));
271 template <
class ELFT>
272 void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA,
raw_ostream &OS) {
277 Header.e_ident[
EI_MAG0] = 0x7f;
282 Header.e_ident[
EI_DATA] = Doc.Header.Data;
284 Header.e_ident[
EI_OSABI] = Doc.Header.OSABI;
286 Header.e_type = Doc.Header.Type;
287 Header.e_machine = Doc.Header.Machine;
289 Header.e_entry = Doc.Header.Entry;
290 Header.e_phoff = Doc.ProgramHeaders.size() ?
sizeof(Header) : 0;
291 Header.e_flags = Doc.Header.Flags;
292 Header.e_ehsize =
sizeof(Elf_Ehdr);
293 Header.e_phentsize = Doc.ProgramHeaders.size() ?
sizeof(Elf_Phdr) : 0;
294 Header.e_phnum = Doc.ProgramHeaders.size();
297 Doc.Header.SHEntSize ? (
uint16_t)*Doc.Header.SHEntSize :
sizeof(Elf_Shdr);
301 CBA.getOSAndAlignedOffset(SHOff,
sizeof(
typename ELFT::uint));
303 Doc.Header.SHOff ?
typename ELFT::uint(*Doc.Header.SHOff) : SHOff;
305 Doc.Header.SHNum ? (
uint16_t)*Doc.Header.SHNum : Doc.getSections().size();
306 Header.e_shstrndx = Doc.Header.SHStrNdx ? (
uint16_t)*Doc.Header.SHStrNdx
307 : SN2I.get(
".shstrtab");
309 OS.
write((
const char *)&Header,
sizeof(Header));
312 template <
class ELFT>
313 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
314 for (
const auto &YamlPhdr : Doc.ProgramHeaders) {
316 Phdr.p_type = YamlPhdr.Type;
317 Phdr.p_flags = YamlPhdr.Flags;
318 Phdr.p_vaddr = YamlPhdr.VAddr;
319 Phdr.p_paddr = YamlPhdr.PAddr;
320 PHeaders.push_back(Phdr);
324 template <
class ELFT>
328 if (SN2I.lookup(S, Index) ||
to_integer(S, Index))
333 reportError(
"unknown section referenced: '" + S +
"' by YAML symbol '" +
336 reportError(
"unknown section referenced: '" + S +
"' by YAML section '" +
341 template <
class ELFT>
344 const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
348 if (!SymMap.lookup(S, Index) && !
to_integer(S, Index)) {
349 reportError(
"unknown symbol referenced: '" + S +
"' by YAML section '" +
356 template <
class ELFT>
363 To.sh_name = *From->
ShName;
367 To.sh_size = *From->
ShSize;
370 template <
class ELFT>
371 bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
375 if (Header.sh_offset)
378 if (SecName ==
".symtab")
380 else if (SecName ==
".strtab")
381 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
382 else if (SecName ==
".shstrtab")
383 initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec);
384 else if (SecName ==
".dynsym")
385 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
386 else if (SecName ==
".dynstr")
387 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
392 overrideFields<ELFT>(YAMLSec, Header);
397 size_t SuffixPos = S.
rfind(
" [");
400 return S.
substr(0, SuffixPos);
403 template <
class ELFT>
404 void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
405 ContiguousBlobAccumulator &CBA) {
408 SHeaders.resize(Doc.getSections().size());
411 for (
const std::unique_ptr<ELFYAML::Chunk> &D : Doc.
Chunks) {
412 if (
auto S = dyn_cast<ELFYAML::Fill>(D.get())) {
426 Elf_Shdr &SHeader = SHeaders[SecNdx];
427 if (initImplicitHeader(CBA, SHeader, Sec->
Name,
431 assert(Sec &&
"It can't be null unless it is an implicit section. But all " 432 "implicit sections should already have been handled above.");
436 SHeader.sh_type = Sec->
Type;
438 SHeader.sh_flags = *Sec->
Flags;
439 SHeader.sh_addr = Sec->
Address;
443 SHeader.sh_link = toSectionIndex(Sec->
Link, Sec->
Name);
446 if (
auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
449 SHeader.sh_size = *RawSec->Size;
451 SHeader.sh_info = *RawSec->Info;
454 SHeader.sh_entsize = *Sec->
EntSize;
455 }
else if (
auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
456 writeSectionContent(SHeader, *S, CBA);
457 }
else if (
auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
458 writeSectionContent(SHeader, *S, CBA);
459 }
else if (
auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
460 writeSectionContent(SHeader, *S, CBA);
461 }
else if (
auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
462 writeSectionContent(SHeader, *S, CBA);
463 }
else if (
auto S = dyn_cast<ELFYAML::Group>(Sec)) {
464 writeSectionContent(SHeader, *S, CBA);
465 }
else if (
auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
466 writeSectionContent(SHeader, *S, CBA);
467 }
else if (
auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
468 SHeader.sh_entsize = 0;
469 SHeader.sh_size = S->Size;
472 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
473 }
else if (
auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
474 writeSectionContent(SHeader, *S, CBA);
475 }
else if (
auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
476 writeSectionContent(SHeader, *S, CBA);
477 }
else if (
auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
478 writeSectionContent(SHeader, *S, CBA);
479 }
else if (
auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
480 writeSectionContent(SHeader, *S, CBA);
481 }
else if (
auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
482 writeSectionContent(SHeader, *S, CBA);
483 }
else if (
auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
484 writeSectionContent(SHeader, *S, CBA);
485 }
else if (
auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
486 writeSectionContent(SHeader, *S, CBA);
487 }
else if (
auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
488 writeSectionContent(SHeader, *S, CBA);
489 }
else if (
auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
490 writeSectionContent(SHeader, *S, CBA);
491 }
else if (
auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
492 writeSectionContent(SHeader, *S, CBA);
493 }
else if (
auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
494 writeSectionContent(SHeader, *S, CBA);
500 overrideFields<ELFT>(Sec, SHeader);
505 for (
size_t I = 0;
I < Symbols.
size(); ++
I)
508 return Symbols.
size();
514 size_t ContentSize = 0;
527 template <
class ELFT>
528 std::vector<typename ELFT::Sym>
531 std::vector<Elf_Sym>
Ret;
532 Ret.resize(Symbols.
size() + 1);
542 Symbol.st_name = *Sym.NameIndex;
543 else if (!Sym.Name.empty())
546 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
547 if (!Sym.Section.empty())
548 Symbol.st_shndx = toSectionIndex(Sym.Section,
"", Sym.Name);
550 Symbol.st_shndx = *Sym.Index;
552 Symbol.st_value = Sym.Value;
553 Symbol.st_other = Sym.Other ? *Sym.Other : 0;
554 Symbol.st_size = Sym.Size;
560 template <
class ELFT>
561 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
563 ContiguousBlobAccumulator &CBA,
568 if (IsStatic && Doc.Symbols)
569 Symbols = *Doc.Symbols;
570 else if (!IsStatic && Doc.DynamicSymbols)
571 Symbols = *Doc.DynamicSymbols;
574 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
576 bool HasSymbolsDescription =
577 (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
578 if (HasSymbolsDescription) {
579 StringRef Property = (IsStatic ?
"`Symbols`" :
"`DynamicSymbols`");
581 reportError(
"cannot specify both `Content` and " + Property +
582 " for symbol table section '" + RawSec->
Name +
"'");
584 reportError(
"cannot specify both `Size` and " + Property +
585 " for symbol table section '" + RawSec->
Name +
"'");
591 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ?
".symtab" :
".dynsym");
594 SHeader.sh_type = YAMLSec->
Type;
601 SHeader.sh_link = toSectionIndex(RawSec->
Link, RawSec->
Name);
609 Link = SN2I.get(
".strtab");
611 SN2I.lookup(
".dynstr", Link);
612 SHeader.sh_link =
Link;
615 if (YAMLSec && YAMLSec->
Flags)
616 SHeader.sh_flags = *YAMLSec->
Flags;
622 SHeader.sh_info = (RawSec && RawSec->
Info) ? (
unsigned)(*RawSec->
Info)
624 SHeader.sh_entsize = (YAMLSec && YAMLSec->
EntSize)
625 ? (uint64_t)(*YAMLSec->
EntSize)
627 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->
AddressAlign : 8;
628 SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->
Address : 0;
630 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
637 std::vector<Elf_Sym> Syms =
638 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
643 template <
class ELFT>
644 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
646 ContiguousBlobAccumulator &CBA,
649 SHeader.sh_name = DotShStrtab.getOffset(Name);
651 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->
AddressAlign : 1;
654 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
656 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
657 if (RawSec && (RawSec->Content || RawSec->Size)) {
658 SHeader.sh_size =
writeContent(OS, RawSec->Content, RawSec->Size);
661 SHeader.sh_size = STB.
getSize();
664 if (YAMLSec && YAMLSec->
EntSize)
665 SHeader.sh_entsize = *YAMLSec->
EntSize;
667 if (RawSec && RawSec->Info)
668 SHeader.sh_info = *RawSec->Info;
670 if (YAMLSec && YAMLSec->
Flags)
671 SHeader.sh_flags = *YAMLSec->
Flags;
672 else if (Name ==
".dynstr")
678 SHeader.sh_addr = YAMLSec->
Address;
686 template <
class ELFT>
687 std::vector<Fragment>
691 for (
const std::unique_ptr<ELFYAML::Chunk> &D : Doc.
Chunks)
692 if (
auto S = dyn_cast<ELFYAML::Fill>(D.get()))
693 NameToFill[S->Name] = S;
695 std::vector<Fragment>
Ret;
698 if (SN2I.lookup(SecName.
Section, Index)) {
699 const typename ELFT::Shdr &
H = SHeaders[
Index];
700 Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
711 "' by program header");
717 template <
class ELFT>
718 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
719 std::vector<Elf_Shdr> &SHeaders) {
721 for (
auto &YamlPhdr : Doc.ProgramHeaders) {
722 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
723 std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
725 if (YamlPhdr.Offset) {
726 PHeader.p_offset = *YamlPhdr.Offset;
728 if (YamlPhdr.Sections.size())
729 PHeader.p_offset = UINT32_MAX;
731 PHeader.p_offset = 0;
734 for (
const Fragment &
F : Fragments)
735 PHeader.p_offset = std::min((uint64_t)PHeader.p_offset,
F.Offset);
741 uint64_t FileOffset = PHeader.p_offset, MemOffset = PHeader.p_offset;
742 for (
const Fragment &
F : Fragments) {
743 uint64_t End =
F.Offset +
F.Size;
744 MemOffset =
std::max(MemOffset, End);
747 FileOffset =
std::max(FileOffset, End);
751 PHeader.p_filesz = YamlPhdr.FileSize ? uint64_t(*YamlPhdr.FileSize)
752 : FileOffset - PHeader.p_offset;
753 PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
754 : MemOffset - PHeader.p_offset;
756 if (YamlPhdr.Align) {
757 PHeader.p_align = *YamlPhdr.Align;
763 for (
const Fragment &
F : Fragments)
764 PHeader.p_align =
std::max((uint64_t)PHeader.p_align,
F.AddrAlign);
769 template <
class ELFT>
770 void ELFState<ELFT>::writeSectionContent(
772 ContiguousBlobAccumulator &CBA) {
774 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
778 SHeader.sh_entsize = *Section.
EntSize;
781 SHeader.sh_info = *Section.
Info;
790 template <
class ELFT>
791 void ELFState<ELFT>::writeSectionContent(
793 ContiguousBlobAccumulator &CBA) {
796 "Section type is not SHT_REL nor SHT_RELA");
799 SHeader.sh_entsize = IsRela ?
sizeof(Elf_Rela) :
sizeof(Elf_Rel);
800 SHeader.sh_size = SHeader.sh_entsize * Section.
Relocations.size();
804 if (Section.
Link.
empty() && SN2I.lookup(
".symtab", Link))
805 SHeader.sh_link = Link;
810 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
812 unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.
Name,
813 Section.
Link ==
".dynsym")
818 REntry.r_offset = Rel.Offset;
819 REntry.r_addend = Rel.Addend;
820 REntry.setSymbolAndType(SymIdx, Rel.Type,
isMips64EL(Doc));
821 OS.
write((
const char *)&REntry,
sizeof(REntry));
825 REntry.r_offset = Rel.Offset;
826 REntry.setSymbolAndType(SymIdx, Rel.Type,
isMips64EL(Doc));
827 OS.
write((
const char *)&REntry,
sizeof(REntry));
832 template <
class ELFT>
833 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
835 ContiguousBlobAccumulator &CBA) {
837 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
849 for (llvm::yaml::Hex64
E : *Section.
Entries) {
850 if (!ELFT::Is64Bits && E > UINT32_MAX)
851 reportError(Section.
Name +
": the value is too large for 32-bits: 0x" +
853 support::endian::write<uintX_t>(OS,
E, ELFT::TargetEndianness);
856 SHeader.sh_size =
sizeof(uintX_t) * Section.
Entries->size();
859 template <
class ELFT>
860 void ELFState<ELFT>::writeSectionContent(
862 ContiguousBlobAccumulator &CBA) {
864 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
867 support::endian::write<uint32_t>(OS,
E, ELFT::TargetEndianness);
869 SHeader.sh_entsize = Shndx.
EntSize ? (uint64_t)*Shndx.
EntSize : 4;
870 SHeader.sh_size = Shndx.
Entries.size() * SHeader.sh_entsize;
873 template <
class ELFT>
874 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
876 ContiguousBlobAccumulator &CBA) {
878 "Section type is not SHT_GROUP");
881 if (Section.
Link.
empty() && SN2I.lookup(
".symtab", Link))
882 SHeader.sh_link = Link;
884 SHeader.sh_entsize = 4;
885 SHeader.sh_size = SHeader.sh_entsize * Section.
Members.size();
892 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
895 unsigned int SectionIndex = 0;
900 support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
904 template <
class ELFT>
905 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
907 ContiguousBlobAccumulator &CBA) {
909 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
911 support::endian::write<uint16_t>(OS,
Version, ELFT::TargetEndianness);
913 SHeader.sh_entsize = Section.
EntSize ? (uint64_t)*Section.
EntSize : 2;
914 SHeader.sh_size = Section.
Entries.size() * SHeader.sh_entsize;
917 template <
class ELFT>
918 void ELFState<ELFT>::writeSectionContent(
920 ContiguousBlobAccumulator &CBA) {
922 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
930 support::endian::write<uintX_t>(OS, E.
Address, ELFT::TargetEndianness);
935 template <
class ELFT>
936 void ELFState<ELFT>::writeSectionContent(
938 ContiguousBlobAccumulator &CBA) {
940 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
959 template <
class ELFT>
960 void ELFState<ELFT>::writeSectionContent(
962 ContiguousBlobAccumulator &CBA) {
964 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
977 SHeader.sh_size += Lib.
size() + 1;
981 template <
class ELFT>
982 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
984 ContiguousBlobAccumulator &CBA) {
986 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
989 if (Section.
Link.
empty() && SN2I.lookup(
".dynsym", Link))
990 SHeader.sh_link = Link;
997 support::endian::write<uint32_t>(OS, Section.
Bucket->size(),
998 ELFT::TargetEndianness);
999 support::endian::write<uint32_t>(OS, Section.
Chain->size(),
1000 ELFT::TargetEndianness);
1002 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1004 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1006 SHeader.sh_size = (2 + Section.
Bucket->size() + Section.
Chain->size()) * 4;
1009 template <
class ELFT>
1010 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1012 ContiguousBlobAccumulator &CBA) {
1013 typedef typename ELFT::Verdef Elf_Verdef;
1014 typedef typename ELFT::Verdaux Elf_Verdaux;
1016 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1018 SHeader.sh_info = Section.
Info;
1028 uint64_t AuxCnt = 0;
1029 for (
size_t I = 0;
I < Section.
Entries->size(); ++
I) {
1033 VerDef.vd_version = E.
Version;
1034 VerDef.vd_flags = E.
Flags;
1036 VerDef.vd_hash = E.
Hash;
1037 VerDef.vd_aux =
sizeof(Elf_Verdef);
1039 if (
I == Section.
Entries->size() - 1)
1043 sizeof(Elf_Verdef) + E.
VerNames.size() *
sizeof(Elf_Verdaux);
1044 OS.
write((
const char *)&VerDef,
sizeof(Elf_Verdef));
1046 for (
size_t J = 0; J < E.
VerNames.size(); ++J, ++AuxCnt) {
1047 Elf_Verdaux VernAux;
1048 VernAux.vda_name = DotDynstr.getOffset(E.
VerNames[J]);
1050 VernAux.vda_next = 0;
1052 VernAux.vda_next =
sizeof(Elf_Verdaux);
1053 OS.
write((
const char *)&VernAux,
sizeof(Elf_Verdaux));
1057 SHeader.sh_size = Section.
Entries->size() *
sizeof(Elf_Verdef) +
1058 AuxCnt *
sizeof(Elf_Verdaux);
1061 template <
class ELFT>
1062 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1064 ContiguousBlobAccumulator &CBA) {
1065 typedef typename ELFT::Verneed Elf_Verneed;
1066 typedef typename ELFT::Vernaux Elf_Vernaux;
1068 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1069 SHeader.sh_info = Section.
Info;
1079 uint64_t AuxCnt = 0;
1080 for (
size_t I = 0;
I < Section.
VerneedV->size(); ++
I) {
1083 Elf_Verneed VerNeed;
1084 VerNeed.vn_version = VE.
Version;
1085 VerNeed.vn_file = DotDynstr.getOffset(VE.
File);
1087 VerNeed.vn_next = 0;
1090 sizeof(Elf_Verneed) + VE.
AuxV.size() *
sizeof(Elf_Vernaux);
1091 VerNeed.vn_cnt = VE.
AuxV.size();
1092 VerNeed.vn_aux =
sizeof(Elf_Verneed);
1093 OS.
write((
const char *)&VerNeed,
sizeof(Elf_Verneed));
1095 for (
size_t J = 0; J < VE.
AuxV.size(); ++J, ++AuxCnt) {
1098 Elf_Vernaux VernAux;
1099 VernAux.vna_hash = VAuxE.
Hash;
1100 VernAux.vna_flags = VAuxE.
Flags;
1101 VernAux.vna_other = VAuxE.
Other;
1102 VernAux.vna_name = DotDynstr.getOffset(VAuxE.
Name);
1103 if (J == VE.
AuxV.size() - 1)
1104 VernAux.vna_next = 0;
1106 VernAux.vna_next =
sizeof(Elf_Vernaux);
1107 OS.
write((
const char *)&VernAux,
sizeof(Elf_Vernaux));
1111 SHeader.sh_size = Section.
VerneedV->size() *
sizeof(Elf_Verneed) +
1112 AuxCnt *
sizeof(Elf_Vernaux);
1115 template <
class ELFT>
1116 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1118 ContiguousBlobAccumulator &CBA) {
1120 "Section type is not SHT_MIPS_ABIFLAGS");
1124 SHeader.sh_entsize =
sizeof(Flags);
1125 SHeader.sh_size = SHeader.sh_entsize;
1127 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1139 OS.
write((
const char *)&Flags,
sizeof(Flags));
1142 template <
class ELFT>
1143 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1145 ContiguousBlobAccumulator &CBA) {
1147 "Section type is not SHT_DYNAMIC");
1150 reportError(
"cannot specify both raw content and explicit entries " 1151 "for dynamic section '" +
1152 Section.
Name +
"'");
1155 SHeader.sh_size = Section.
Content->binary_size();
1157 SHeader.sh_size = 2 *
sizeof(uintX_t) * Section.
Entries.size();
1159 SHeader.sh_entsize = *Section.
EntSize;
1161 SHeader.sh_entsize =
sizeof(Elf_Dyn);
1164 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1166 support::endian::write<uintX_t>(OS, DE.
Tag, ELFT::TargetEndianness);
1167 support::endian::write<uintX_t>(OS, DE.
Val, ELFT::TargetEndianness);
1170 Section.
Content->writeAsBinary(OS);
1173 template <
class ELFT>
1174 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1176 ContiguousBlobAccumulator &CBA) {
1178 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1181 if (Section.
Link.
empty() && SN2I.lookup(
".symtab", Link))
1182 SHeader.sh_link = Link;
1191 Sym.
Name ? toSymbolIndex(*Sym.
Name, Section.
Name,
false)
1197 template <
class ELFT>
1198 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1200 ContiguousBlobAccumulator &CBA) {
1202 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1213 support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness);
1215 support::endian::write<uint32_t>(OS, NE.
Name.
size() + 1,
1216 ELFT::TargetEndianness);
1220 support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness);
1223 ELFT::TargetEndianness);
1226 support::endian::write<uint32_t>(OS, NE.
Type, ELFT::TargetEndianness);
1231 ELFT::TargetEndianness);
1232 support::endian::write<uint8_t>(OS, 0, ELFT::TargetEndianness);
1233 CBA.padToAlignment(4);
1239 CBA.padToAlignment(4);
1246 template <
class ELFT>
1247 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1249 ContiguousBlobAccumulator &CBA) {
1251 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
1254 if (Section.
Link.
empty() && SN2I.lookup(
".dynsym", Link))
1255 SHeader.sh_link = Link;
1266 if (Section.
Header->NBuckets)
1267 support::endian::write<uint32_t>(OS, *Section.
Header->NBuckets,
1268 ELFT::TargetEndianness);
1270 support::endian::write<uint32_t>(OS, Section.
HashBuckets->size(),
1271 ELFT::TargetEndianness);
1275 support::endian::write<uint32_t>(OS, Section.
Header->SymNdx,
1276 ELFT::TargetEndianness);
1280 if (Section.
Header->MaskWords)
1281 support::endian::write<uint32_t>(OS, *Section.
Header->MaskWords,
1282 ELFT::TargetEndianness);
1284 support::endian::write<uint32_t>(OS, Section.
BloomFilter->size(),
1285 ELFT::TargetEndianness);
1288 support::endian::write<uint32_t>(OS, Section.
Header->Shift2,
1289 ELFT::TargetEndianness);
1292 for (llvm::yaml::Hex64 Val : *Section.
BloomFilter)
1293 support::endian::write<typename ELFT::uint>(OS, Val,
1294 ELFT::TargetEndianness);
1297 for (llvm::yaml::Hex32 Val : *Section.
HashBuckets)
1298 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1301 for (llvm::yaml::Hex32 Val : *Section.
HashValues)
1302 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1304 SHeader.sh_size = 16 +
1305 Section.
BloomFilter->size() *
sizeof(
typename ELFT::uint) +
1310 template <
class ELFT>
1312 ContiguousBlobAccumulator &CBA) {
1315 size_t PatternSize = Fill.
Pattern ? Fill.
Pattern->binary_size() : 0;
1322 uint64_t Written = 0;
1323 for (; Written + PatternSize <= Fill.
Size; Written += PatternSize)
1324 Fill.
Pattern->writeAsBinary(OS);
1325 Fill.
Pattern->writeAsBinary(OS, Fill.
Size - Written);
1328 template <
class ELFT>
void ELFState<ELFT>::buildSectionIndex() {
1331 for (
size_t I = 0;
I < Doc.Chunks.size(); ++
I) {
1332 const std::unique_ptr<ELFYAML::Chunk> &
C = Doc.Chunks[
I];
1333 bool IsSection = isa<ELFYAML::Section>(C.get());
1337 if (C->Name.empty())
1340 if (!Seen.
insert(C->Name).second)
1341 reportError(
"repeated section/fill name: '" + C->Name +
1342 "' at YAML section/fill number " +
Twine(
I));
1343 if (!IsSection || HasError)
1346 if (!SN2I.addName(C->Name, SecNdx))
1351 DotShStrtab.finalize();
1354 template <
class ELFT>
void ELFState<ELFT>::buildSymbolIndexes() {
1356 for (
size_t I = 0, S = V.
size();
I < S; ++
I) {
1364 Build(*Doc.Symbols, SymN2I);
1365 if (Doc.DynamicSymbols)
1366 Build(*Doc.DynamicSymbols, DynSymN2I);
1369 template <
class ELFT>
void ELFState<ELFT>::finalizeStrings() {
1374 DotStrtab.finalize();
1377 if (Doc.DynamicSymbols)
1384 if (
auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
1385 if (VerNeed->VerneedV) {
1387 DotDynstr.add(VE.
File);
1389 DotDynstr.add(Aux.
Name);
1392 }
else if (
auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
1393 if (VerDef->Entries)
1396 DotDynstr.add(Name);
1400 DotDynstr.finalize();
1403 template <
class ELFT>
1406 ELFState<ELFT> State(Doc, EH);
1411 State.finalizeStrings();
1413 State.buildSectionIndex();
1417 State.buildSymbolIndexes();
1419 std::vector<Elf_Phdr> PHeaders;
1420 State.initProgramHeaders(PHeaders);
1424 const size_t SectionContentBeginOffset =
1426 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
1428 std::vector<Elf_Shdr> SHeaders;
1429 State.initSectionHeaders(SHeaders, CBA);
1432 State.setProgramHeaderLayout(PHeaders, SHeaders);
1437 State.writeELFHeader(CBA, OS);
1439 CBA.writeBlobToStream(OS);
1452 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH);
1453 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH);
1456 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH);
1457 return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH);
This file declares classes for handling the YAML representation of ELF.
Optional< ELF_SHF > Flags
static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To)
Optional< llvm::yaml::Hex64 > Size
This class represents lattice values for constants.
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base.
Optional< std::vector< llvm::yaml::Hex64 > > BloomFilter
Optional< yaml::BinaryRef > Content
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Optional< yaml::BinaryRef > Content
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Optional< llvm::yaml::Hex64 > Size
An efficient, type-erasing, non-owning reference to a callable.
iterator find(StringRef Key)
A raw_ostream that writes to an SmallVector or SmallString.
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
llvm::yaml::Hex16 Version
Optional< llvm::yaml::Hex64 > Size
llvm::yaml::Hex64 Address
Optional< llvm::yaml::Hex64 > Size
llvm::yaml::Hex64 AddressAlign
Optional< llvm::yaml::Hex32 > Index
Optional< std::vector< llvm::yaml::Hex32 > > HashValues
std::vector< DynamicEntry > Entries
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Optional< StringRef > Signature
Optional< std::vector< StackSizeEntry > > Entries
Optional< std::vector< uint32_t > > Chain
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
StringRef sectionNameOrType
void write(raw_ostream &OS) const
Utility for building string tables with deduplicated suffixes.
Optional< llvm::yaml::Hex64 > EntSize
static void writeArrayData(raw_ostream &OS, ArrayRef< T > A)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
std::vector< std::unique_ptr< Chunk > > Chunks
Optional< GnuHashHeader > Header
Optional< llvm::yaml::Hex64 > ShSize
std::vector< ProgramHeader > ProgramHeaders
void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
std::vector< VernauxEntry > AuxV
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
The instances of the Type class are immutable: once they are created, they are never changed...
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::vector< uint32_t > Entries
Optional< llvm::yaml::Hex64 > Size
std::vector< StringRef > VerNames
Optional< yaml::BinaryRef > Content
Optional< std::vector< ELFYAML::NoteEntry > > Notes
Optional< yaml::BinaryRef > Content
Optional< yaml::BinaryRef > Content
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< typename base::iterator, bool > insert(StringRef Key)
This struct is a compact representation of a valid (non-zero power of two) alignment.
llvm::yaml::Hex8 ISARevision
Optional< std::vector< LinkerOption > > Options
raw_ostream & write(unsigned char C)
static uint64_t writeContent(raw_ostream &OS, const Optional< yaml::BinaryRef > &Content, const Optional< llvm::yaml::Hex64 > &Size)
static bool isMips64EL(const ELFYAML::Object &Doc)
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
Optional< std::vector< llvm::yaml::Hex64 > > Entries
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
StringRef dropUniqueSuffix(StringRef S)
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
BlockVerifier::State From
static Error reportError(StringRef Message)
Align max(MaybeAlign Lhs, Align Rhs)
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
std::vector< SectionOrType > Members
Optional< llvm::yaml::Hex64 > ShOffset
Optional< StringRef > Name
Common declarations for yaml2obj.
static Twine utohexstr(const uint64_t &Val)
Optional< yaml::BinaryRef > Content
StringRef str()
Return a StringRef for the vector contents.
Optional< std::vector< VerneedEntry > > VerneedV
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Optional< llvm::yaml::Hex64 > Info
Optional< llvm::yaml::Hex64 > ShName
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
bool yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
Optional< llvm::yaml::Hex64 > ShFlags
llvm::yaml::Hex64 Address
Optional< yaml::BinaryRef > Pattern
Optional< yaml::BinaryRef > Content
MIPS_AFL_EXT ISAExtension
Optional< std::vector< uint32_t > > Bucket
Optional< std::vector< llvm::yaml::Hex32 > > HashBuckets
static size_t findFirstNonGlobal(ArrayRef< ELFYAML::Symbol > Symbols)
Optional< std::vector< AddrsigSymbol > > Symbols
Optional< yaml::BinaryRef > Content
Optional< yaml::BinaryRef > Content
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< yaml::BinaryRef > Content
StringSet - A wrapper for StringMap that provides set-like functionality.
uint64_t tell() const
tell - Return the current offset with the file.
Optional< std::vector< VerdefEntry > > Entries
This class implements an extremely fast bulk output stream that can only output to a stream...
Optional< std::vector< YAMLFlowString > > Libs
StringRef - Represent a constant reference to a string, i.e.
Optional< yaml::BinaryRef > Content
static size_t arrayDataSize(ArrayRef< T > A)
std::vector< uint16_t > Entries
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
Optional< yaml::BinaryRef > Content
std::vector< Relocation > Relocations
bool empty() const
empty - Check if the array is empty.