diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -631,6 +631,22 @@ return DotShStrtab.getOffset(Name); } +static uint64_t writeContent(ContiguousBlobAccumulator &CBA, + const Optional &Content, + const Optional &Size) { + size_t ContentSize = 0; + if (Content) { + CBA.writeAsBinary(*Content); + ContentSize = Content->binary_size(); + } + + if (!Size) + return ContentSize; + + CBA.writeZeros(*Size - ContentSize); + return *Size; +} + template void ELFState::initSectionHeaders(std::vector &SHeaders, ContiguousBlobAccumulator &CBA) { @@ -690,7 +706,16 @@ } if (Sec->EntSize) SHeader.sh_entsize = *Sec->EntSize; - } else if (auto S = dyn_cast(Sec)) { + + LocationCounter += SHeader.sh_size; + overrideFields(Sec, SHeader); + continue; + } + + if (!isa(Sec) && (Sec->Content || Sec->Size)) + SHeader.sh_size = writeContent(CBA, Sec->Content, Sec->Size); + + if (auto S = dyn_cast(Sec)) { writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { writeSectionContent(SHeader, *S, CBA); @@ -769,22 +794,6 @@ return Symbols.size(); } -static uint64_t writeContent(ContiguousBlobAccumulator &CBA, - const Optional &Content, - const Optional &Size) { - size_t ContentSize = 0; - if (Content) { - CBA.writeAsBinary(*Content); - ContentSize = Content->binary_size(); - } - - if (!Size) - return ContentSize; - - CBA.writeZeros(*Size - ContentSize); - return *Size; -} - template std::vector ELFState::toELFSymbols(ArrayRef Symbols, @@ -1124,9 +1133,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::NoBitsSection &S, ContiguousBlobAccumulator &CBA) { - // SHT_NOBITS sections do not have any content to write. - SHeader.sh_entsize = 0; - if (!S.Size) return; @@ -1143,8 +1149,6 @@ void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - if (Section.EntSize) SHeader.sh_entsize = *Section.EntSize; @@ -1181,11 +1185,6 @@ if (!Section.RelocatableSec.empty()) SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Relocations) return; @@ -1220,11 +1219,6 @@ SHeader.sh_entsize = Section.EntSize ? uint64_t(*Section.EntSize) : sizeof(Elf_Relr); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1275,11 +1269,6 @@ SHeader.sh_info = toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Members) return; @@ -1300,11 +1289,6 @@ ContiguousBlobAccumulator &CBA) { SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1317,10 +1301,8 @@ void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.Entries) return; - } if (!Section.Entries) return; @@ -1335,11 +1317,6 @@ void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Options) return; @@ -1356,11 +1333,6 @@ void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Libs) return; @@ -1409,11 +1381,6 @@ SN2I.lookup(".symtab", Link)) SHeader.sh_link = Link; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1442,10 +1409,8 @@ else SHeader.sh_entsize = sizeof(typename ELFT::Word); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.Bucket) return; - } if (!Section.Bucket) return; @@ -1474,11 +1439,6 @@ SHeader.sh_info = Section.Info; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1524,11 +1484,6 @@ SHeader.sh_info = Section.Info; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.VerneedV) return; @@ -1572,11 +1527,6 @@ void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::ARMIndexTableSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1625,11 +1575,6 @@ else SHeader.sh_entsize = 2 * sizeof(uintX_t); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1649,10 +1594,8 @@ SN2I.lookup(".symtab", Link)) SHeader.sh_link = Link; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.Symbols) return; - } if (!Section.Symbols) return; @@ -1666,15 +1609,10 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::NoteSection &Section, ContiguousBlobAccumulator &CBA) { - uint64_t Offset = CBA.tell(); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Notes) return; + uint64_t Offset = CBA.tell(); for (const ELFYAML::NoteEntry &NE : *Section.Notes) { // Write name size. if (NE.Name.empty()) @@ -1717,10 +1655,8 @@ SN2I.lookup(".dynsym", Link)) SHeader.sh_link = Link; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.HashBuckets) return; - } if (!Section.Header) return;