hash-histogram.test 13.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
## Here we test the --elf-hash-histogram command line option.

## This test case checks how we built histograms for hash sections.

# RUN: yaml2obj --docnum=1 -D BITS=32 %s -o %t1-32.o
# RUN: llvm-readelf --elf-hash-histogram %t1-32.o | FileCheck %s --check-prefix=HIST

## Test --histogram and -I aliases.
# RUN: llvm-readelf --histogram %t1-32.o | FileCheck %s --check-prefix=HIST
# RUN: llvm-readelf -I %t1-32.o | FileCheck %s --check-prefix=HIST

# RUN: yaml2obj --docnum=1 -D BITS=64 %s -o %t1-64.o
# RUN: llvm-readelf --elf-hash-histogram %t1-64.o | FileCheck %s --check-prefix=HIST

# HIST:      Histogram for bucket list length (total of 3 buckets)
# HIST-NEXT:  Length  Number     % of total  Coverage
# HIST-NEXT:       0  2          ( 66.7%)       0.0%
# HIST-NEXT:       1  0          (  0.0%)       0.0%
# HIST-NEXT:       2  0          (  0.0%)       0.0%
# HIST-NEXT:       3  1          ( 33.3%)     100.0%
# HIST-NEXT: Histogram for `.gnu.hash' bucket list length (total of 3 buckets)
# HIST-NEXT:  Length  Number     % of total  Coverage
# HIST-NEXT:       0  1          ( 33.3%)       0.0%
# HIST-NEXT:       1  1          ( 33.3%)      25.0%
# HIST-NEXT:       2  0          (  0.0%)      25.0%
# HIST-NEXT:       3  1          ( 33.3%)     100.0%
# HIST-NOT:  {{.}}

--- !ELF
FileHeader:
  Class: ELFCLASS[[BITS]]
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:   .hash
    Type:   SHT_HASH
    Flags:  [ SHF_ALLOC ]
    Bucket: [ 6, 4, 5 ]
    Chain:  [ 0, 0, 1, 0, 2 ]
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x1
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x00000001, 0x00000004, 0x00000000 ]
    HashValues:  [ 0x0B887388, 0xECD54542, 0x7C92E3BB, 0x1C5871D9 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_WRITE, SHF_ALLOC ]
    Entries:
      - Tag:   DT_HASH
        Value: 0x0
      - Tag:   DT_GNU_HASH
## sizeof(.hash) == 0x28.
        Value: 0x28
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols:
  - Name: a
  - Name: b
  - Name: c
  - Name: d
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .hash
      - Section: .gnu.hash
      - Section: .dynamic

## Show that we report a warning for a hash table which contains an entry of
## the bucket array pointing to a cycle.

# RUN: yaml2obj --docnum=2 %s -o %t2.o
# RUN: llvm-readelf --elf-hash-histogram 2>&1 %t2.o | FileCheck -DFILE=%t2.o %s --check-prefix=BROKEN

# BROKEN:       warning: '[[FILE]]': .hash section is invalid: bucket 1: a cycle was detected in the linked chain
# BROKEN:       Histogram for bucket list length (total of 1 buckets)
# BROKEN-NEXT:  Length  Number     % of total  Coverage
# BROKEN-NEXT:       0  0          (  0.0%)       0.0%
# BROKEN-NEXT:       1  1          (100.0%)     100.0%

--- !ELF
FileHeader:
  Class: ELFCLASS32
  Data:  ELFDATA2LSB
  Type:  ET_REL
Sections:
  - Name:   .hash
    Type:   SHT_HASH
    Link:   .dynsym
    Bucket: [ 1 ]
    Chain:  [ 0, 1 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Entries:
## llvm-readelf will read the hash table from the file offset
## p_offset + (p_vaddr - DT_HASH) = p_offset + (0 - 0) = p_offset,
## which is the start of PT_LOAD, i.e. the file offset of .hash.
      - Tag:   DT_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0
DynamicSymbols:
  - Name: foo
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .hash
      - Section: .dynamic

## Each SHT_HASH section starts with two 32-bit fields: nbucket and nchain.
## Check we report an error when a DT_HASH value points to data that has size less than 8 bytes.

# RUN: yaml2obj --docnum=3 %s -o %t3.o
# RUN: llvm-readelf --elf-hash-histogram %t3.o 2>&1 | FileCheck %s --check-prefix=ERR1 -DFILE=%t3.o

# ERR1: warning: '[[FILE]]': the hash table at offset 0x2b1 goes past the end of the file (0x2b8){{$}}

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:   .hash
    Type:   SHT_HASH
    Flags:  [ SHF_ALLOC ]
    Bucket: [ 0 ]
    Chain:  [ 0 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_WRITE, SHF_ALLOC ]
    Entries:
      - Tag:   DT_HASH
        Value: 0x239
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type:     PT_LOAD
    FileSize: 0x23a
    Sections:
      - Section: .hash
      - Section: .dynamic

## Check we report a warning when the hash table goes past the end of the file.

## Case A.1: the hash table ends right before the EOF. We have a broken nbucket
##           field that has a value larger than the number of buckets.
# RUN: yaml2obj --docnum=4 %s -o %t4.1.o -DNBUCKET=0x5d -DNCHAIN=0x1
# RUN: llvm-readelf --elf-hash-histogram %t4.1.o 2>&1 | \
# RUN:   FileCheck %s --implicit-check-not={{.}} --allow-empty

## Case A.2: the hash table ends 1 byte past the EOF. We have a broken nbucket
##           field that has a value larger than the number of buckets.
# RUN: yaml2obj --docnum=4 %s -o %t4.2.o -DNBUCKET=0x5e -DNCHAIN=0x1
# RUN: llvm-readelf --elf-hash-histogram %t4.2.o 2>&1 | \
# RUN:   FileCheck %s --check-prefix=ERR2 -DFILE=%t4.2.o --implicit-check-not="warning:"
# ERR2: warning: '[[FILE]]': the hash table at offset 0x54 goes past the end of the file (0x1d4), nbucket = 94, nchain = 1{{$}}

## Case B.1: the hash table ends right before the EOF. We have a broken nchain
##           field that has a value larger than the number of chains.
# RUN: yaml2obj --docnum=4 %s -o %t4.3.o -DNBUCKET=0x1 -DNCHAIN=0x5d
# RUN: llvm-readelf --elf-hash-histogram %t4.3.o 2>&1 | \
# RUN:   FileCheck %s --check-prefix=ERR3 -DFILE=%t4.3.o --implicit-check-not="warning:"
# ERR3: warning: '[[FILE]]': hash table nchain (93) differs from symbol count derived from SHT_DYNSYM section header (1){{$}}
# ERR3: warning: '[[FILE]]': the size (0x5d0) of the dynamic symbol table at 0x78, derived from the hash table, goes past the end of the file (0x1d4) and will be ignored

## Case B.2: the hash table ends 1 byte past the EOF. We have a broken nchain
##           field that has a value larger than the number of chains.
# RUN: yaml2obj --docnum=4 %s -o %t4.4.o -DNBUCKET=0x1 -DNCHAIN=0x5e
# RUN: llvm-readelf --elf-hash-histogram %t4.4.o 2>&1 | \
# RUN:   FileCheck %s --check-prefix=ERR4 -DFILE=%t4.4.o --implicit-check-not="warning:"
# ERR4: warning: '[[FILE]]': hash table nchain (94) differs from symbol count derived from SHT_DYNSYM section header (1){{$}}
# ERR4: warning: '[[FILE]]': the size (0x5e0) of the dynamic symbol table at 0x78, derived from the hash table, goes past the end of the file (0x1d4) and will be ignored
# ERR4: warning: '[[FILE]]': the hash table at offset 0x54 goes past the end of the file (0x1d4), nbucket = 1, nchain = 94{{$}}

--- !ELF
FileHeader:
  Class: ELFCLASS32
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:    .hash
    Type:    SHT_HASH
    Flags:   [ SHF_ALLOC ]
    Bucket:  [ 0 ]
    NBucket: [[NBUCKET]]
    Chain:   [ 0 ]
    NChain:  [[NCHAIN]]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_WRITE, SHF_ALLOC ]
    Entries:
      - Tag:   DT_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type: PT_LOAD
    Sections:
      - Section: .hash
      - Section: .dynamic

## Check we dump a histogram for the .gnu.hash table even when the .hash table is skipped.

## Case A: the .hash table has no data to build histogram and it is skipped.
# RUN: yaml2obj --docnum=5 %s -o %t5.o
# RUN: llvm-readelf --elf-hash-histogram %t5.o 2>&1 | \
# RUN:   FileCheck %s --check-prefix=GNU-HASH --implicit-check-not="Histogram"

## Case B: the .hash table has a broken nbucket field. We report a warning
##         and skip dumping of the .hash table.
# RUN: yaml2obj --docnum=5 -DNBUCKET=0xffffffff %s -o %t6.o
# RUN: llvm-readelf --elf-hash-histogram %t6.o 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t6.o --check-prefixes=WARN,GNU-HASH

# WARN:     warning: '[[FILE]]': the hash table at offset 0x78 goes past the end of the file (0x358), nbucket = 4294967295, nchain = 2
# GNU-HASH: Histogram for `.gnu.hash' bucket list length (total of 3 buckets)

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:    .hash
    Type:    SHT_HASH
    Flags:   [ SHF_ALLOC ]
    Bucket:  [ 0 ]
## 0x2 is a no-op: it does not change the number of buckets described by the "Bucket" key
    NBucket: [[NBUCKET=0x2]]
    Chain:   [ 0, 0 ]
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x1
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x00000001, 0x00000004, 0x00000000 ]
    HashValues:  [ 0x0B887388 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_WRITE, SHF_ALLOC ]
    Entries:
      - Tag:   DT_HASH
        Value: 0x0
      - Tag:   DT_GNU_HASH
## sizeof(.hash) == 0x14.
        Value: 0x14
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols:
  - Name: foo
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .hash
      - Section: .gnu.hash
      - Section: .dynamic

## Check we report a proper warning when the GNU hash table goes past the end of the file.

## Case A: the 'maskwords' field is set so that the GNU hash table goes past the end of the file.
# RUN: yaml2obj --docnum=6 -D MASKWORDS=0x80000000 %s -o %t7
# RUN: llvm-readelf --elf-hash-histogram %t7 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t7 --check-prefix=ERR5 --implicit-check-not="Histogram"

# ERR5: warning: '[[FILE]]': unable to dump the SHT_GNU_HASH section at 0x78: it goes past the end of the file

## Case B: the 'nbuckets' field is set so that the GNU hash table goes past the end of the file.
# RUN: yaml2obj --docnum=6 -D NBUCKETS=0x80000000 %s -o %t8
# RUN: llvm-readelf --elf-hash-histogram %t8 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t8 --check-prefix=ERR5 --implicit-check-not="Histogram"

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x0
      Shift2: 0x0
## The number of words in the Bloom filter. The value of 1 is no-op.
      MaskWords: [[MASKWORDS=1]]
## The number of hash buckets. The value of 1 is no-op.
      NBuckets:  [[NBUCKETS=1]]
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x0 ]
    HashValues:  [ 0x0 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Link:  .dynstr
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type: PT_LOAD
    Sections:
      - Section: .gnu.hash
      - Section: .dynamic

## Linkers might produce an empty no-op SHT_GNU_HASH section when
## there are no dynamic symbols or when all dynamic symbols are undefined.
## Such sections normally have a single zero entry in the bloom
## filter, a single zero entry in the hash bucket and no values.
##
## The index of the first symbol in the dynamic symbol table
## included in the hash table can be set to the number of dynamic symbols,
## which is one larger than the index of the last dynamic symbol.
## For empty tables however, this value is unimportant and can be ignored.

## Check the case when a 'symndx' index of the first symbol in the dynamic symbol
## table is larger than the number of dynamic symbols.

## Case A: when the buckets array is not empty and has a non-zero value we report a warning.
# RUN: yaml2obj --docnum=7 -DVAL=0x1 %s -o %t9
# RUN: llvm-readelf --elf-hash-histogram %t9 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t9 --check-prefix=ERR6

# ERR6: warning: '[[FILE]]': unable to print the GNU hash table histogram: the first hashed symbol index (16) is larger than the number of dynamic symbols (1)

## Case B: we do not report a warning when the buckets array contains only zero values.
# RUN: yaml2obj --docnum=7 -DVAL=0x0 %s -o %t10
# RUN: llvm-readelf --elf-hash-histogram %t10 2>&1 | \
# RUN:   FileCheck %s --allow-empty --implicit-check-not="Histogram"

## Case C: we do not report a warning when the buckets array is empty.
# RUN: yaml2obj --docnum=7 -DVAL="" %s -o %t11
# RUN: llvm-readelf --elf-hash-histogram %t11 2>&1 | \
# RUN:   FileCheck %s --allow-empty --implicit-check-not="Histogram"

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x10
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ [[VAL]] ]
    HashValues:  [ 0x0 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Link:  .dynstr
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type: PT_LOAD
    Sections:
      - Section: .gnu.hash
      - Section: .dynamic

## Check we report warnings when the dynamic symbol table is absent or empty.

## The code locates the dynamic symbol table by the section type. Use SHT_PROGBITS to hide it.
# RUN: yaml2obj --docnum=8 -DTYPE=SHT_PROGBITS %s -o %t12
# RUN: llvm-readelf --elf-hash-histogram %t12 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t12 --check-prefix=ERR7

# ERR7: warning: '[[FILE]]': unable to print the GNU hash table histogram: no dynamic symbol table found

# RUN: yaml2obj --docnum=8 -DTYPE=SHT_DYNSYM %s -o %t13
# RUN: llvm-readelf --elf-hash-histogram %t13 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t13 --check-prefix=ERR8

# ERR8: warning: '[[FILE]]': unable to print the GNU hash table histogram: the dynamic symbol table is empty

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x0
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x0 ]
    HashValues:  [ 0x0 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
  - Name: .dynsym
    Type: [[TYPE]]
    Size: 0
ProgramHeaders:
  - Type: PT_LOAD
    Sections:
      - Section: .gnu.hash