[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
This commit is contained in:
Xing GUO 2020-07-28 09:53:59 +08:00
parent 930fc0b300
commit 6bf989b947
2 changed files with 45 additions and 3 deletions

View File

@ -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]]

View File

@ -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)