[yaml2obj][obj2yaml] - Do not create a symbol table by default.
This patch tries to resolve problems faced in D68943
and uses some of the code written by Konrad Wilhelm Kleine
in that patch.
Previously, yaml2obj tool always created a .symtab section.
This patch changes that. With it we only create it when
have a "Symbols:" tag in the YAML document or when
we need to create it because it is used by another section(s).
obj2yaml follows the new behavior and does not print "Symbols:"
anymore when there is no symbol table.
Differential revision: https://reviews.llvm.org/D69041
llvm-svn: 375361
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index f9c31f3..e0faed2 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -200,10 +200,17 @@
ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
: Doc(D), ErrHandler(EH) {
StringSet<> DocSections;
- for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
+ for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections) {
if (!D->Name.empty())
DocSections.insert(D->Name);
+ // Some sections wants to link to .symtab by default.
+ // That means we want to create the symbol table for them.
+ if (D->Type == llvm::ELF::SHT_REL || D->Type == llvm::ELF::SHT_RELA)
+ if (!Doc.Symbols && D->Link.empty())
+ Doc.Symbols.emplace();
+ }
+
// 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(
@@ -211,7 +218,11 @@
std::make_unique<ELFYAML::Section>(
ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
- std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
+ std::vector<StringRef> ImplicitSections;
+ if (Doc.Symbols)
+ ImplicitSections.push_back(".symtab");
+ ImplicitSections.insert(ImplicitSections.end(), {".strtab", ".shstrtab"});
+
if (!Doc.DynamicSymbols.empty())
ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
@@ -508,7 +519,11 @@
ELFYAML::Section *YAMLSec) {
bool IsStatic = STType == SymtabType::Static;
- const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
+ ArrayRef<ELFYAML::Symbol> Symbols;
+ if (IsStatic && Doc.Symbols)
+ Symbols = *Doc.Symbols;
+ else if (!IsStatic)
+ Symbols = Doc.DynamicSymbols;
ELFYAML::RawContentSection *RawSec =
dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
@@ -1044,14 +1059,16 @@
}
};
- Build(Doc.Symbols, SymN2I);
+ if (Doc.Symbols)
+ Build(*Doc.Symbols, SymN2I);
Build(Doc.DynamicSymbols, DynSymN2I);
}
template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
// Add the regular symbol names to .strtab section.
- for (const ELFYAML::Symbol &Sym : Doc.Symbols)
- DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
+ if (Doc.Symbols)
+ for (const ELFYAML::Symbol &Sym : *Doc.Symbols)
+ DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
DotStrtab.finalize();
// Add the dynamic symbol names to .dynstr section.