[yaml2obj]Re-allow dynamic sections to have raw content
Recently, support was added to yaml2obj to allow dynamic sections to
have a list of entries, to make it easier to write tests with dynamic
sections. However, this change also removed the ability to provide
custom contents to the dynamic section, making it hard to test
malformed contents (e.g. because the section is not a valid size to
contain an array of entries). This change reinstates this. An error is
emitted if raw content and dynamic entries are both specified.
Reviewed by: grimar, ruiu
Differential Review: https://reviews.llvm.org/D58543
llvm-svn: 354770
diff --git a/llvm/tools/yaml2obj/yaml2elf.cpp b/llvm/tools/yaml2obj/yaml2elf.cpp
index 351b734..d651c92 100644
--- a/llvm/tools/yaml2obj/yaml2elf.cpp
+++ b/llvm/tools/yaml2obj/yaml2elf.cpp
@@ -169,7 +169,7 @@
bool writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::MipsABIFlags &Section,
ContiguousBlobAccumulator &CBA);
- void writeSectionContent(Elf_Shdr &SHeader,
+ bool writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::DynamicSection &Section,
ContiguousBlobAccumulator &CBA);
bool hasDynamicSymbols() const;
@@ -309,7 +309,8 @@
// so just to setup the section offset.
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
} else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec.get())) {
- writeSectionContent(SHeader, *S, CBA);
+ if (!writeSectionContent(SHeader, *S, CBA))
+ return false;
} else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec.get())) {
writeSectionContent(SHeader, *S, CBA);
} else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
@@ -713,7 +714,7 @@
}
template <class ELFT>
-void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
+bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::DynamicSection &Section,
ContiguousBlobAccumulator &CBA) {
typedef typename ELFT::uint uintX_t;
@@ -721,7 +722,18 @@
assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
"Section type is not SHT_DYNAMIC");
- SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
+ if (!Section.Entries.empty() && Section.Content) {
+ WithColor::error()
+ << "Cannot specify both raw content and explicit entries "
+ "for dynamic section '"
+ << Section.Name << "'.\n";
+ return false;
+ }
+
+ if (Section.Content)
+ SHeader.sh_size = Section.Content->binary_size();
+ else
+ SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
if (Section.EntSize)
SHeader.sh_entsize = *Section.EntSize;
else
@@ -732,6 +744,10 @@
support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
}
+ if (Section.Content)
+ Section.Content->writeAsBinary(OS);
+
+ return true;
}
template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {