[yaml2obj] - Add a support for defining null sections in YAMLs.
ELF spec shows (Figure 4-10: Section Header Table Entry:Index 0,
http://www.sco.com/developers/gabi/latest/ch4.sheader.html)
that section header at index 0 (null section) can have sh_size and
sh_link fields set to non-zero values.
It says (https://docs.oracle.com/cd/E19683-01/817-3677/6mj8mbtc9/index.html):
"If the number of sections is greater than or equal to SHN_LORESERVE (0xff00),
this member has the value zero and the actual number of section header table
entries is contained in the sh_size field of the section header at index 0.
Otherwise, the sh_size member of the initial entry contains 0."
and:
"If the section name string table section index is greater than or equal to SHN_LORESERVE
(0xff00), this member has the value SHN_XINDEX (0xffff) and the actual index of the section
name string table section is contained in the sh_link field of the section header at index 0.
Otherwise, the sh_link member of the initial entry contains 0."
At this moment it is not possible to create custom section headers at index 0 using yaml2obj.
This patch implements this.
Differential revision: https://reviews.llvm.org/D64913
llvm-svn: 366794
diff --git a/llvm/tools/yaml2obj/yaml2elf.cpp b/llvm/tools/yaml2obj/yaml2elf.cpp
index 64f84f3..f3a8a91 100644
--- a/llvm/tools/yaml2obj/yaml2elf.cpp
+++ b/llvm/tools/yaml2obj/yaml2elf.cpp
@@ -192,10 +192,11 @@
if (!D->Name.empty())
DocSections.insert(D->Name);
- // Insert SHT_NULL section implicitly.
- Doc.Sections.insert(
- Doc.Sections.begin(),
- llvm::make_unique<ELFYAML::Section>(
+ // Insert SHT_NULL section implicitly when it is not defined in YAML.
+ if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
+ Doc.Sections.insert(
+ Doc.Sections.begin(),
+ llvm::make_unique<ELFYAML::Section>(
ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
@@ -325,10 +326,27 @@
// valid SHN_UNDEF entry since SHT_NULL == 0.
SHeaders.resize(Doc.Sections.size());
- for (size_t I = 1; I < Doc.Sections.size(); ++I) {
+ for (size_t I = 0; I < Doc.Sections.size(); ++I) {
Elf_Shdr &SHeader = SHeaders[I];
ELFYAML::Section *Sec = Doc.Sections[I].get();
+ if (I == 0) {
+ if (Sec->IsImplicit)
+ continue;
+
+ if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec))
+ if (S->Size)
+ SHeader.sh_size = *S->Size;
+
+ if (!Sec->Link.empty()) {
+ unsigned Index;
+ if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
+ return false;
+ SHeader.sh_link = Index;
+ }
+ continue;
+ }
+
// We have a few sections like string or symbol tables that are usually
// added implicitly to the end. However, if they are explicitly specified
// in the YAML, we need to write them here. This ensures the file offset