[PATCH] D84606: [llvm-readelf] Symbol index in symbol table printing is not reset

Mikhail Kalashnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 13:53:30 PDT 2020


mikkqu created this revision.
mikkqu added reviewers: lattner, jhenderson.
Herald added subscribers: llvm-commits, rupprecht, arphaman, MaskRay, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

Stop using static variables for keeping track of symbol indices.

Bugfix for: https://bugs.llvm.org/show_bug.cgi?id=46777


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84606

Files:
  llvm/test/tools/llvm-readobj/ELF/section-symbols-index.test
  llvm/tools/llvm-readobj/ELFDumper.cpp


Index: llvm/tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- llvm/tools/llvm-readobj/ELFDumper.cpp
+++ llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -3929,21 +3929,12 @@
                                  const Elf_Sym *FirstSym,
                                  Optional<StringRef> StrTable, bool IsDynamic,
                                  bool NonVisibilityBitsUsed) {
-  static int Idx = 0;
-  static bool Dynamic = true;
-
-  // If this function was called with a different value from IsDynamic
-  // from last call, happens when we move from dynamic to static symbol
-  // table, "Num" field should be reset.
-  if (!Dynamic != !IsDynamic) {
-    Idx = 0;
-    Dynamic = false;
-  }
+  int Idx = Symbol - FirstSym;
 
   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
   Field Fields[8] = {0,         8,         17 + Bias, 23 + Bias,
                      31 + Bias, 38 + Bias, 48 + Bias, 51 + Bias};
-  Fields[0].Str = to_string(format_decimal(Idx++, 6)) + ":";
+  Fields[0].Str = to_string(format_decimal(Idx, 6)) + ":";
   Fields[1].Str = to_string(
       format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
   Fields[2].Str = to_string(format_decimal(Symbol->st_size, 5));
Index: llvm/test/tools/llvm-readobj/ELF/section-symbols-index.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-readobj/ELF/section-symbols-index.test
@@ -0,0 +1,30 @@
+## This test verifies that the Num index starts from zero at every new Symbol table.
+
+# RUN: yaml2obj %s -o %t1
+# RUN: llvm-readelf -s %t1 %t1 | FileCheck %s
+
+# CHECK:      Symbol table '.symtab' contains 3 entries:
+# CHECK-NEXT:    Num: {{.*}}
+# CHECK-NEXT:      0: {{.*}}
+# CHECK-NEXT:      1: {{.*}}
+# CHECK-NEXT:      2: {{.*}}
+# CHECK-EMPTY:
+# CHECK:      File: {{.*}}
+# CHECK-EMPTY:
+# CHECK:      Symbol table '.symtab' contains 3 entries:
+# CHECK-NEXT:    Num: {{.*}}
+# CHECK-NEXT:      0: {{.*}}
+# CHECK-NEXT:      1: {{.*}}
+# CHECK-NEXT:      2: {{.*}}
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS32
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_386
+Symbols:
+  - Name:  foo
+    Value: 0x1
+  - Name:  bar
+    Value: 0x2


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84606.280750.patch
Type: text/x-patch
Size: 2236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200726/61c103e0/attachment.bin>


More information about the llvm-commits mailing list