[llvm] 6bf989b - [llvm-readelf] Fix emitting incorrect number of spaces in '--hex-dump'.

Xing GUO via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 27 18:54:59 PDT 2020


Author: Xing GUO
Date: 2020-07-28T09:54:33+08:00
New Revision: 6bf989b9474ace6a35021e6123d13b7fd59bf9f4

URL: https://github.com/llvm/llvm-project/commit/6bf989b9474ace6a35021e6123d13b7fd59bf9f4
DIFF: https://github.com/llvm/llvm-project/commit/6bf989b9474ace6a35021e6123d13b7fd59bf9f4.diff

LOG: [llvm-readelf] Fix emitting incorrect number of spaces in '--hex-dump'.

This patch helps teach llvm-readelf to emit a correct number spaces when
dumping in hex format.

Before this patch, when the hex data doesn't fill the 4th column, some
spaces are missing.

```
Hex dump of section '.sec':
0x00000000 00000000 00000000 00000000 00000000 ................
0x00000010 00000000 00000000 00000000 0000 ..............
```

After this patch:

```
Hex dump of section '.sec':
0x00000000 00000000 00000000 00000000 00000000 ................
0x00000010 00000000 00000000 00000000 0000     ..............
```

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D84640

Added: 
    

Modified: 
    llvm/test/tools/llvm-readobj/ELF/hex-dump.test
    llvm/tools/llvm-readobj/ObjDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-readobj/ELF/hex-dump.test b/llvm/test/tools/llvm-readobj/ELF/hex-dump.test
index b1f54009110e..5954153c6345 100644
--- a/llvm/test/tools/llvm-readobj/ELF/hex-dump.test
+++ b/llvm/test/tools/llvm-readobj/ELF/hex-dump.test
@@ -1,6 +1,6 @@
 ## Test that the -x alias can be used flexibly. Create a baseline and ensure
 ## all other combinations are identical.
-# RUN: yaml2obj %s -o %t
+# RUN: yaml2obj --docnum=1 %s -o %t
 # RUN: llvm-readelf --file-header --hex-dump=.shstrtab %t > %t.hexdump.out
 # RUN: llvm-readelf -h --hex-dump .shstrtab %t > %t.hexdump.1
 # RUN: llvm-readelf -h -x .shstrtab %t > %t.hexdump.2
@@ -48,3 +48,44 @@ FileHeader:
   Data:    ELFDATA2LSB
   Type:    ET_DYN
   Machine: EM_386
+
+## Test that llvm-readelf emits a correct amount of spaces between the hex data
+## and its ascii representation.
+
+## a) When the hex data doesn't fill the column whose index isn't 4.
+# RUN: yaml2obj --docnum=2 -DSIZE=18 %s -o %t2.out1
+# RUN: llvm-readelf --hex-dump=.sec %t2.out1 | \
+# RUN:   FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SPACES1
+
+#      SPACES1:Hex dump of section '.sec':
+# SPACES1-NEXT:0x00000000 00000000 00000000 00000000 00000000 ................
+# SPACES1-NEXT:0x00000010 0000                                ..
+
+## b) When the hex data doesn't fill the column whose index is 4.
+# RUN: yaml2obj --docnum=2 -DSIZE=30 %s -o %t2.out2
+# RUN: llvm-readelf --hex-dump=.sec %t2.out2 | \
+# RUN:   FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SPACES2
+
+#      SPACES2:Hex dump of section '.sec':
+# SPACES2-NEXT:0x00000000 00000000 00000000 00000000 00000000 ................
+# SPACES2-NEXT:0x00000010 00000000 00000000 00000000 0000     ..............
+
+## c) When the hex data fills the column.
+# RUN: yaml2obj --docnum=2 -DSIZE=28 %s -o %t2.out3
+# RUN: llvm-readelf --hex-dump=.sec %t2.out3 | \
+# RUN:   FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SPACES3
+
+#      SPACES3:Hex dump of section '.sec':
+# SPACES3-NEXT:0x00000000 00000000 00000000 00000000 00000000 ................
+# SPACES3-NEXT:0x00000010 00000000 00000000 00000000          ............
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS32
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_386
+Sections:
+  - Name: .sec
+    Type: SHT_PROGBITS
+    Size: [[SIZE]]

diff  --git a/llvm/tools/llvm-readobj/ObjDumper.cpp b/llvm/tools/llvm-readobj/ObjDumper.cpp
index ce61f1c53a4d..7b7c9553827f 100644
--- a/llvm/tools/llvm-readobj/ObjDumper.cpp
+++ b/llvm/tools/llvm-readobj/ObjDumper.cpp
@@ -155,8 +155,9 @@ void ObjDumper::printSectionsAsHex(const object::ObjectFile *Obj,
       // Least, if we cut in a middle of a row, we add the remaining characters,
       // which is (8 - (k * 2)).
       if (i < 4)
-        W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)),
-                                ' ');
+        W.startLine() << format("%*c", (4 - i) * 8 + (4 - i), ' ');
+      if (k < 4)
+        W.startLine() << format("%*c", 8 - k * 2, ' ');
 
       TmpSecPtr = SecPtr;
       for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)


        


More information about the llvm-commits mailing list