From 36dd3d39360137a73332afe5db3c4a7b40b3a50c Mon Sep 17 00:00:00 2001 From: George Rimar Date: Fri, 22 Feb 2019 08:45:21 +0000 Subject: [PATCH] [obj2yaml] - Do not miss section index for special symbols. This fixes https://bugs.llvm.org/show_bug.cgi?id=40786 ("obj2yaml symbol output missing section index for SHN_ABS and SHN_COMMON symbols") Since SHN_ABS and SHN_COMMON symbols are special, we should preserve the st_shndx for them. The patch does this for them and the other special symbols. The test case is based on the test provided by James Henderson at the bug page! Differential revision: https://reviews.llvm.org/D58498 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354661 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/obj2yaml/Inputs/shn_xindex.o | Bin 0 -> 403 bytes .../obj2yaml/special-symbol-indices.yaml | 46 ++++++++++++++++++ tools/obj2yaml/elf2yaml.cpp | 7 +++ 3 files changed, 53 insertions(+) create mode 100644 test/tools/obj2yaml/Inputs/shn_xindex.o create mode 100644 test/tools/obj2yaml/special-symbol-indices.yaml diff --git a/test/tools/obj2yaml/Inputs/shn_xindex.o b/test/tools/obj2yaml/Inputs/shn_xindex.o new file mode 100644 index 0000000000000000000000000000000000000000..73e2b60ba1e24c4909d30ecf81ba3b8741cd2e5b GIT binary patch literal 403 zcmb<-^>JfjWMqH=CI&kO1doBi0V)BbL0kt03oyyTzzpJ21cZUQnXs7P0Cj``R1TyD zB*X#bOF(IEAdSrx1yKDk^-$#~eglbtlws3f0o5-J(+H)Z!f2wHT#z~ehX4ODWGXWA aQc^1z81#xWic5-05|e-|7+skQ;sXH55fWJd literal 0 HcmV?d00001 diff --git a/test/tools/obj2yaml/special-symbol-indices.yaml b/test/tools/obj2yaml/special-symbol-indices.yaml new file mode 100644 index 00000000000..127dc25a0df --- /dev/null +++ b/test/tools/obj2yaml/special-symbol-indices.yaml @@ -0,0 +1,46 @@ +# RUN: yaml2obj %s > %t +# RUN: obj2yaml %t | FileCheck %s + +## Test checks that we are able to handle symbols with special/reserved indices. + +# CHECK: Symbols: +# CHECK-NEXT: Global: +# CHECK-NEXT: - Name: absolute +# CHECK-NEXT: Index: SHN_ABS +# CHECK-NEXT: Value: 0x0000000000001234 +# CHECK-NEXT: - Name: common +# CHECK-NEXT: Index: SHN_COMMON +# CHECK-NEXT: - Name: valid_index +# CHECK-NEXT: Section: .text +# CHECK-NEXT: - Name: processor_specific_index +# CHECK-NEXT: Index: SHN_HEXAGON_SCOMMON_1 +# CHECK-NEXT: - Name: unknown_index +# CHECK-NEXT: Index: 0x0000FFFE + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_HEXAGON +Sections: + - Name: .text + Type: SHT_PROGBITS +Symbols: + Global: + - Name: absolute + Index: SHN_ABS + Value: 0x1234 + - Name: common + Index: SHN_COMMON + - Name: valid_index + Index: 0x1 + - Name: processor_specific_index + Index: SHN_HEXAGON_SCOMMON_1 + - Name: unknown_index + Index: 0xfffe + +## shn_xindex.o contains a symbol with st_shndx == SHN_XINDEX. +## We do not support it at this moment. +# RUN: not obj2yaml %S/Inputs/shn_xindex.o 2>&1 | FileCheck %s --check-prefix=ERR +# ERR: Error reading file: {{.*}}shn_xindex.o: Feature not yet implemented. diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp index b02c25ad153..7f5e9a28d72 100644 --- a/tools/obj2yaml/elf2yaml.cpp +++ b/tools/obj2yaml/elf2yaml.cpp @@ -284,6 +284,13 @@ ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, return errorToErrorCode(SymbolNameOrErr.takeError()); S.Name = SymbolNameOrErr.get(); + if (Sym->st_shndx >= ELF::SHN_LORESERVE) { + if (Sym->st_shndx == ELF::SHN_XINDEX) + return obj2yaml_error::not_implemented; + S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx; + return obj2yaml_error::success; + } + auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); if (!ShdrOrErr) return errorToErrorCode(ShdrOrErr.takeError()); -- 2.40.0