This is similar to what we did earlier for fields of the Section class.
When a field is optional we can use the =<none> syntax in macros.
This was splitted from D92478.
Differential revision: https://reviews.llvm.org/D92565
struct Symbol {
StringRef Name;
ELF_STT Type;
- StringRef Section;
+ Optional<StringRef> Section;
Optional<ELF_SHN> Index;
ELF_STB Binding;
llvm::yaml::Hex64 Value;
Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
Symbol.setBindingAndType(Sym.Binding, Sym.Type);
- if (!Sym.Section.empty())
- Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name);
+ if (Sym.Section)
+ Symbol.st_shndx = toSectionIndex(*Sym.Section, "", Sym.Name);
else if (Sym.Index)
Symbol.st_shndx = *Sym.Index;
IO.mapOptional("Name", Symbol.Name, StringRef());
IO.mapOptional("StName", Symbol.StName);
IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
- IO.mapOptional("Section", Symbol.Section, StringRef());
+ IO.mapOptional("Section", Symbol.Section);
IO.mapOptional("Index", Symbol.Index);
IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
IO.mapOptional("Value", Symbol.Value, Hex64(0));
std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
ELFYAML::Symbol &Symbol) {
- if (Symbol.Index && Symbol.Section.data())
+ if (Symbol.Index && Symbol.Section)
return "Index and Section cannot both be specified for Symbol";
return "";
}
## Check we can use numeric values to refer to sections.
-# RUN: yaml2obj --docnum=2 %s -o %t2
+# RUN: yaml2obj --docnum=2 -DSEC=0xff %s -o %t2
# RUN: llvm-readobj --dyn-symbols %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=NUM
# NUM: Name: foo
# NUM: Section:
# NUM-SAME: .dynsym (0x2)
-# NUM: Name: zed
+# NUM: Name: zed1
# NUM: warning: '[[FILE]]': invalid section index: 255
# NUM: Section:
# NUM-SAME: <?> (0xFF)
+# NUM: Name: zed2
+# NUM: Section:
+# NUM-SAME: Undefined (0x0)
+
--- !ELF
FileHeader:
Class: ELFCLASS64
- Name: .data
Type: SHT_PROGBITS
DynamicSymbols:
- - Name: foo
+ - Name: foo
Section: 1
- - Name: bar
+ - Name: bar
Section: 2
- - Name: zed
- Section: 0xff
+ - Name: zed1
+ Section: [[SEC=<none>]]
+## The case when no "Section" key is set.
+ - Name: zed2
+
+## Check that by default no section is set.
+
+# RUN: yaml2obj --docnum=2 %s -o %t2.none
+# RUN: llvm-readobj --dyn-symbols %t2.none 2>&1 | \
+# RUN: FileCheck -DFILE=%t2.none %s --check-prefix=NONE
+
+# NONE: Name: zed1
+# NONE: Section:
+# NONE-SAME: Undefined (0x0)
+# NONE: Name: zed2
+# NONE: Section:
+# NONE-SAME: Undefined (0x0)
## Check we report errors when unknown sections are referenced by dynamic symbols.
# ERR: error: unknown section referenced: '.sec1' by YAML symbol 'foo'
# ERR: error: unknown section referenced: '.sec2' by YAML symbol 'bar'
+# ERR: error: unknown section referenced: '' by YAML symbol 'zed'
--- !ELF
FileHeader:
- Name: .data
Type: SHT_PROGBITS
DynamicSymbols:
- - Name: foo
+ - Name: foo
Section: .sec1
- - Name: bar
+ - Name: bar
Section: .sec2
+ - Name: zed
+ Section: ''