Index: llvm/include/llvm/ObjectYAML/ELFYAML.h =================================================================== --- llvm/include/llvm/ObjectYAML/ELFYAML.h +++ llvm/include/llvm/ObjectYAML/ELFYAML.h @@ -103,7 +103,7 @@ struct Symbol { StringRef Name; ELF_STT Type; - StringRef Section; + Optional Section; Optional Index; ELF_STB Binding; llvm::yaml::Hex64 Value; Index: llvm/lib/ObjectYAML/ELFEmitter.cpp =================================================================== --- llvm/lib/ObjectYAML/ELFEmitter.cpp +++ llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -832,8 +832,8 @@ 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; Index: llvm/lib/ObjectYAML/ELFYAML.cpp =================================================================== --- llvm/lib/ObjectYAML/ELFYAML.cpp +++ llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1086,7 +1086,7 @@ 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)); @@ -1104,7 +1104,7 @@ std::string MappingTraits::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 ""; } Index: llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml +++ llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml @@ -42,7 +42,7 @@ ## 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 @@ -53,11 +53,15 @@ # 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 @@ -67,12 +71,27 @@ - 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=]] +## 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.