[llvm-readelf] - Implement --section-details option.

--section-details/-t is a GNU readelf option that produce
an output that is an alternative to --sections.

Differential revision: https://reviews.llvm.org/D89304
diff --git a/llvm/test/tools/llvm-readobj/ELF/section-details.test b/llvm/test/tools/llvm-readobj/ELF/section-details.test
new file mode 100644
index 0000000..fe1f184
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/section-details.test
@@ -0,0 +1,256 @@
+## Check how llvm-readelf prints section details with --section-details.
+
+## Check the output for the 64-bit case.
+# RUN: yaml2obj %s -DBITS=64 -o %t64.o
+# RUN: llvm-readelf %t64.o --section-details | \
+# RUN:   FileCheck %s --strict-whitespace --match-full-lines --check-prefix=GNU64
+
+#       GNU64:There are 19 section headers, starting at offset 0xf8:
+# GNU64-EMPTY:
+#  GNU64-NEXT:Section Headers:
+#  GNU64-NEXT:  [Nr] Name
+#  GNU64-NEXT:       Type            Address          Off    Size   ES   Lk Inf Al
+#  GNU64-NEXT:       Flags
+#  GNU64-NEXT:  [ 0] {{$}}
+#  GNU64-NEXT:       NULL            0000000000000000 000000 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000000]: {{$}}
+#  GNU64-NEXT:  [ 1] allflags_and_long_fields
+#  GNU64-NEXT:       PROGBITS        1111111111111111 2222222222222222 4444444444444444 5555555555555555 858993459 1717986918 8608480567731124087
+#  GNU64-NEXT:       [ffffffffffffffff]: WRITE, ALLOC, EXEC, MERGE, STRINGS, INFO LINK, LINK ORDER, OS NONCONF, GROUP, TLS, COMPRESSED, EXCLUDE, OS (000000000ff00000), PROC (0000000070000000), UNKNOWN (ffffffff000ff008)
+#  GNU64-NEXT:  [ 2] noflags
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000000]: {{$}}
+#  GNU64-NEXT:  [ 3] write
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000001]: WRITE
+#  GNU64-NEXT:  [ 4] alloc
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000002]: ALLOC
+#  GNU64-NEXT:  [ 5] exec
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000004]: EXEC
+#  GNU64-NEXT:  [ 6] merge
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000010]: MERGE
+#  GNU64-NEXT:  [ 7] strings
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000020]: STRINGS
+#  GNU64-NEXT:  [ 8] infolink
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000040]: INFO LINK
+#  GNU64-NEXT:  [ 9] linkorder
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000080]: LINK ORDER
+#  GNU64-NEXT:  [10] nonconforming
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000100]: OS NONCONF
+#  GNU64-NEXT:  [11] group
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000200]: GROUP
+#  GNU64-NEXT:  [12] tls
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000400]: TLS
+#  GNU64-NEXT:  [13] compressed
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000000000800]: COMPRESSED
+#  GNU64-NEXT:  [14] exclude
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [0000000080000000]: EXCLUDE
+#  GNU64-NEXT:  [15] known_and_unknown
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [00000000000f0003]: WRITE, ALLOC, UNKNOWN (00000000000f0000)
+#  GNU64-NEXT:  [16] only_unknown
+#  GNU64-NEXT:       PROGBITS        0000000000000000 000040 000000 00   0   0  0
+#  GNU64-NEXT:       [00000000000f0000]: UNKNOWN (00000000000f0000)
+#  GNU64-NEXT:  [17] .strtab
+#  GNU64-NEXT:       STRTAB          0000000000000000 000040 000001 00   0   0  1
+#  GNU64-NEXT:       [0000000000000000]: {{$}}
+#  GNU64-NEXT:  [18] .shstrtab
+#  GNU64-NEXT:       STRTAB          0000000000000000 000041 0000b0 00   0   0  1
+#  GNU64-NEXT:       [0000000000000000]: {{$}}
+#  GNU64-NOT:{{.}}
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS[[BITS]]
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+  Flags: []
+Sections:
+  - Name:        allflags_and_long_fields
+    Type:        SHT_PROGBITS
+    ShFlags:     0xffffffffffffffff
+    Address:     0x1111111111111111
+    ShOffset:    0x2222222222222222
+    Link:        0x33333333
+    ShSize:      0x4444444444444444
+    EntSize:     0x5555555555555555
+    Info:        0x66666666
+    ShAddrAlign: 0x7777777777777777
+  - Name:  noflags
+    Type:  SHT_PROGBITS
+    Flags: [ ]
+  - Name:  write
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_WRITE ]
+  - Name:  alloc
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_ALLOC ]
+  - Name:  exec
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_EXECINSTR ]
+  - Name:  merge
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_MERGE ]
+  - Name:  strings
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_STRINGS ]
+  - Name:  infolink
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_INFO_LINK ]
+  - Name:  linkorder
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_LINK_ORDER ]
+  - Name:  nonconforming
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_OS_NONCONFORMING ]
+  - Name:  group
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_GROUP ]
+  - Name:  tls
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_TLS ]
+  - Name:  compressed
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_COMPRESSED ]
+  - Name:  exclude
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_EXCLUDE ]
+  - Name:  known_and_unknown
+    Type:  SHT_PROGBITS
+    ShFlags: 0x000f0003
+  - Name:  only_unknown
+    Type:  SHT_PROGBITS
+    ShFlags: 0x000f0000
+
+## Check the output for the 32-bit case.
+# RUN: yaml2obj %s -DBITS=32 -o %t32.o
+# RUN: llvm-readelf %t32.o --section-details | \
+# RUN:   FileCheck %s --strict-whitespace --match-full-lines --check-prefix=GNU32
+
+#       GNU32:There are 19 section headers, starting at offset 0xe8:
+# GNU32-EMPTY:
+#  GNU32-NEXT:Section Headers:
+#  GNU32-NEXT:  [Nr] Name
+#  GNU32-NEXT:       Type            Addr     Off    Size   ES   Lk Inf Al
+#  GNU32-NEXT:       Flags
+#  GNU32-NEXT:  [ 0] {{$}}
+#  GNU32-NEXT:       NULL            00000000 000000 000000 00   0   0  0
+#  GNU32-NEXT:       [00000000]: {{$}}
+#  GNU32-NEXT:  [ 1] allflags_and_long_fields
+#  GNU32-NEXT:       PROGBITS        11111111 22222222 44444444 55555555 858993459 1717986918 2004318071
+#  GNU32-NEXT:       [ffffffff]: WRITE, ALLOC, EXEC, MERGE, STRINGS, INFO LINK, LINK ORDER, OS NONCONF, GROUP, TLS, COMPRESSED, EXCLUDE, OS (0ff00000), PROC (70000000), UNKNOWN (000ff008)
+#  GNU32-NEXT:  [ 2] noflags
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000000]: {{$}}
+#  GNU32-NEXT:  [ 3] write
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000001]: WRITE
+#  GNU32-NEXT:  [ 4] alloc
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000002]: ALLOC
+#  GNU32-NEXT:  [ 5] exec
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000004]: EXEC
+#  GNU32-NEXT:  [ 6] merge
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000010]: MERGE
+#  GNU32-NEXT:  [ 7] strings
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000020]: STRINGS
+#  GNU32-NEXT:  [ 8] infolink
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000040]: INFO LINK
+#  GNU32-NEXT:  [ 9] linkorder
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000080]: LINK ORDER
+#  GNU32-NEXT:  [10] nonconforming
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000100]: OS NONCONF
+#  GNU32-NEXT:  [11] group
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000200]: GROUP
+#  GNU32-NEXT:  [12] tls
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000400]: TLS
+#  GNU32-NEXT:  [13] compressed
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [00000800]: COMPRESSED
+#  GNU32-NEXT:  [14] exclude
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [80000000]: EXCLUDE
+#  GNU32-NEXT:  [15] known_and_unknown
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [000f0003]: WRITE, ALLOC, UNKNOWN (000f0000)
+#  GNU32-NEXT:  [16] only_unknown
+#  GNU32-NEXT:       PROGBITS        00000000 000034 000000 00   0   0  0
+#  GNU32-NEXT:       [000f0000]: UNKNOWN (000f0000)
+#  GNU32-NEXT:  [17] .strtab
+#  GNU32-NEXT:       STRTAB          00000000 000034 000001 00   0   0  1
+#  GNU32-NEXT:       [00000000]: {{$}}
+#  GNU32-NEXT:  [18] .shstrtab
+#  GNU32-NEXT:       STRTAB          00000000 000035 0000b0 00   0   0  1
+#  GNU32-NEXT:       [00000000]: {{$}}
+#  GNU32-NOT:{{.}}
+
+## When --section-details and --sections are both specified, --sections is ignored.
+# RUN: llvm-readelf %t64.o --section-details --sections | FileCheck %s --check-prefix=GNU64
+# RUN: llvm-readelf %t64.o --sections --section-details | FileCheck %s --check-prefix=GNU64
+
+## Check that we produce the same output with -t (alias).
+# RUN: llvm-readelf --section-details %t64.o > %t.readelf.full
+# RUN: llvm-readelf -t %t64.o > %t.readelf.alias
+# RUN: cmp %t.readelf.full %t.readelf.alias
+
+## Check how we dump sections when the section header string table can't be read.
+
+# RUN: yaml2obj --docnum=2 %s -o %tshstrndx.o
+# RUN: llvm-readelf %tshstrndx.o --section-details 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%tshstrndx.o --check-prefix=SHSTRNDX
+
+# SHSTRNDX:      Section Headers:
+# SHSTRNDX-NEXT:   [Nr] Name
+# SHSTRNDX-NEXT:        Type            Address          Off    Size   ES   Lk Inf Al
+# SHSTRNDX-NEXT:        Flags
+# SHSTRNDX-NEXT: warning: '[[FILE]]': section header string table index 65279 does not exist
+# SHSTRNDX-NEXT:   [ 0]
+# SHSTRNDX-NEXT:        NULL            0000000000000000 000000 000000 00   0   0  0
+# SHSTRNDX-NEXT:        [0000000000000000]:
+# SHSTRNDX-NEXT: warning: '[[FILE]]': a section [index 1] has an invalid sh_name (0x5) offset which goes past the end of the section name string table
+# SHSTRNDX-NEXT:   [ 1] <?>
+# SHSTRNDX-NEXT:        PROGBITS        0000000000000000 000040 000000 00   0   0  0
+# SHSTRNDX-NEXT:        [0000000000000000]:
+# SHSTRNDX-NEXT: warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0x1) offset which goes past the end of the section name string table
+# SHSTRNDX-NEXT:   [ 2] <?>
+# SHSTRNDX-NEXT:        PROGBITS        0000000000000000 000040 000000 00   0   0  0
+# SHSTRNDX-NEXT:        [0000000000000000]:
+# SHSTRNDX-NEXT: warning: '[[FILE]]': a section [index 3] has an invalid sh_name (0x13) offset which goes past the end of the section name string table
+# SHSTRNDX-NEXT:   [ 3] <?>
+# SHSTRNDX-NEXT:        STRTAB          0000000000000000 000040 000001 00   0   0  1
+# SHSTRNDX-NEXT:        [0000000000000000]:
+# SHSTRNDX-NEXT: warning: '[[FILE]]': a section [index 4] has an invalid sh_name (0x9) offset which goes past the end of the section name string table
+# SHSTRNDX-NEXT:   [ 4] <?>
+# SHSTRNDX-NEXT:        STRTAB          0000000000000000 000041 00001b 00   0   0  1
+# SHSTRNDX-NEXT:        [0000000000000000]:
+
+--- !ELF
+FileHeader:
+  Class:     ELFCLASS64
+  Data:      ELFDATA2LSB
+  Type:      ET_REL
+  EShStrNdx: 0xfeff
+Sections:
+  - Name: foo
+    Type: SHT_PROGBITS
+  - Name: bar
+    Type: SHT_PROGBITS
diff --git a/llvm/test/tools/llvm-readobj/ELF/symbols.test b/llvm/test/tools/llvm-readobj/ELF/symbols.test
index ba8c898..486718e 100644
--- a/llvm/test/tools/llvm-readobj/ELF/symbols.test
+++ b/llvm/test/tools/llvm-readobj/ELF/symbols.test
@@ -78,9 +78,7 @@
 # RUN: llvm-readelf --symbols %t64 > %t.symbols.gnu
 # RUN: llvm-readelf --syms %t64 > %t.syms.gnu
 # RUN: cmp %t.symbols.gnu %t.syms.gnu
-## There is no -t option in llvm-readelf.
-# RUN: not llvm-readelf -t %t64 2>&1 | FileCheck %s --check-prefix=NO-T-ERR
-# NO-T-ERR: Unknown command line argument '-t'
+
 ## -s is an llvm-readobj option to dump sections.
 # RUN: llvm-readobj -s --elf-output-style=GNU %t64 | FileCheck %s --implicit-check-not="Symbol table"
 
diff --git a/llvm/test/tools/llvm-readobj/basic.test b/llvm/test/tools/llvm-readobj/basic.test
index 5b31795..ddf6acb 100644
--- a/llvm/test/tools/llvm-readobj/basic.test
+++ b/llvm/test/tools/llvm-readobj/basic.test
@@ -41,5 +41,5 @@
 OBJ: -s - Alias for --section-headers
 OBJ: -t - Alias for --symbols
 ELF: -s - Alias for --symbols
-ELF-NOT: {{ }}-t{{ }}
+ELF: -t - Alias for --section-details
 HELP: @FILE