broken-dynamic-reloc.test 13.1 KB
## Test how we handle cases when dynamic relocations or corresponding dynamic tags are broken.

## Check that llvm-readobj/llvm-readelf reports an error when dumping relocations if a dynamic
## symbol name offset is broken (goes past the end of the dynamic symbol string table).

# RUN: yaml2obj --docnum=1 %s -o %t1
# RUN: llvm-readobj --dyn-relocations %t1 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=LLVM
# RUN: llvm-readelf --dyn-relocations %t1 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=GNU

# LLVM:      Dynamic Relocations {
# LLVM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 1: st_name (0x1234) is past the end of the string table of size 0x1
# LLVM-NEXT:   0x0 R_X86_64_NONE <corrupt> 0x0
# LLVM-NEXT: }

# GNU:      'RELA' relocation section at offset {{.+}} contains 24 bytes:
# GNU-NEXT: Offset           Info             Type          Symbol's Value   Symbol's Name + Addend
# GNU-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 1: st_name (0x1234) is past the end of the string table of size 0x1
# GNU-NEXT: 0000000000000000 0000000100000000 R_X86_64_NONE 0000000000000000 <corrupt> + 0

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_X86_64
Sections:
  - Name:    .rela.dyn
    Type:    SHT_RELA
    Link:    .dynsym
    Relocations:
      - Symbol: 1 ## Index of a dynamic symbol with a broken st_name.
        Type:   R_X86_64_NONE
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_RELA
        Value: 0x0000000000000000
      - Tag:   DT_RELASZ
        Value: 0x0000000000000018
      - Tag:   DT_RELAENT
        Value: 0x0000000000000018
      - Tag:   DT_NULL
        Value: 0x0000000000000000
DynamicSymbols:
  - StName: 0x1234
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .rela.dyn
      - Section: .dynamic

## Show we print a warning for an invalid relocation table size stored in a DT_RELASZ entry.
# RUN: yaml2obj --docnum=2 -DRELTYPE=RELA -DTAG1=DT_RELASZ -DTAG1VAL=0xFF -DTAG2=DT_RELAENT %s -o %t2
# RUN: llvm-readobj --dyn-relocations %t2 2>&1 | FileCheck %s -DFILE=%t2 --check-prefix=INVALID-DT-RELASZ
# RUN: llvm-readelf --dyn-relocations %t2 2>&1 | FileCheck %s -DFILE=%t2 --check-prefix=INVALID-DT-RELASZ

# INVALID-DT-RELASZ: warning: '[[FILE]]': invalid DT_RELASZ value (0xff) or DT_RELAENT value (0x18)

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .relx.dyn
    Type:  SHT_[[RELTYPE]]
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_[[RELTYPE]]
        Value: 0x0
      - Tag:   [[TAG1]]
        Value: [[TAG1VAL=0x18]]
      - Tag:   [[TAG2]]
        Value: [[TAG2VAL=0x18]]
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .relx.dyn
      - Section: .dynamic

## Show we print a warning for an invalid relocation table entry size stored in a DT_RELAENT entry.
# RUN: yaml2obj --docnum=2 -DRELTYPE=RELA -DTAG1=DT_RELASZ -DTAG2=DT_RELAENT -DTAG2VAL=0xFF %s -o %t3
# RUN: llvm-readobj --dyn-relocations %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=INVALID-DT-RELAENT
# RUN: llvm-readelf --dyn-relocations %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=INVALID-DT-RELAENT

## INVALID-DT-RELAENT: warning: '[[FILE]]': invalid DT_RELASZ value (0x18) or DT_RELAENT value (0xff)

## Show we print a warning for an invalid relocation table size stored in a DT_RELSZ entry.
# RUN: yaml2obj --docnum=2 -DRELTYPE=REL -DTAG1=DT_RELSZ -DTAG1VAL=0xFF -DTAG2=DT_RELENT %s -o %t4
# RUN: llvm-readobj --dyn-relocations %t4 2>&1 | FileCheck %s -DFILE=%t4 --check-prefix=INVALID-DT-RELSZ
# RUN: llvm-readelf --dyn-relocations %t4 2>&1 | FileCheck %s -DFILE=%t4 --check-prefix=INVALID-DT-RELSZ

## INVALID-DT-RELSZ: warning: '[[FILE]]': invalid DT_RELSZ value (0xff) or DT_RELENT value (0x18)

## Show we print a warning for an invalid relocation table entry size stored in a DT_RELENT entry.
# RUN: yaml2obj --docnum=2 -DRELTYPE=REL -DTAG1=DT_RELSZ -DTAG2=DT_RELENT -DTAG2VAL=0xFF %s -o %t5
# RUN: llvm-readobj --dyn-relocations %t5 2>&1 | FileCheck %s -DFILE=%t5 --check-prefix=INVALID-DT-RELENT
# RUN: llvm-readelf --dyn-relocations %t5 2>&1 | FileCheck %s -DFILE=%t5 --check-prefix=INVALID-DT-RELENT

## INVALID-DT-RELENT: warning: '[[FILE]]': invalid DT_RELSZ value (0x18) or DT_RELENT value (0xff)

## Show we print a warning for an invalid relocation table size stored in a DT_RELRSZ/DT_ANDROID_RELRSZ entry.
# RUN: yaml2obj --docnum=2 -DRELTYPE=RELR -DTAG1=DT_RELRSZ -DTAG1VAL=0xFF -DTAG2=DT_RELRENT %s -o %t6
# RUN: llvm-readobj --dyn-relocations %t6 2>&1 | FileCheck %s -DFILE=%t6 --check-prefix=INVALID-DT-RELRSZ
# RUN: llvm-readelf --dyn-relocations %t6 2>&1 | FileCheck %s -DFILE=%t6 --check-prefix=INVALID-DT-RELRSZ

# RUN: yaml2obj --docnum=2 -DRELTYPE=RELR -DTAG1=DT_ANDROID_RELRSZ -DTAG1VAL=0xFF -DTAG2=DT_ANDROID_RELRENT %s -o %t7
# RUN: llvm-readobj --dyn-relocations %t7 2>&1 | FileCheck %s -DFILE=%t7 --check-prefix=INVALID-DT-ANDROID-RELRSZ
# RUN: llvm-readelf --dyn-relocations %t7 2>&1 | FileCheck %s -DFILE=%t7 --check-prefix=INVALID-DT-ANDROID-RELRSZ

## INVALID-DT-RELRSZ:         warning: '[[FILE]]': invalid DT_RELRSZ value (0xff) or DT_RELRENT value (0x18)
## INVALID-DT-ANDROID-RELRSZ: warning: '[[FILE]]': invalid DT_ANDROID_RELRSZ value (0xff) or DT_ANDROID_RELRENT value (0x18)

## Show we print a warning for an invalid relocation table entry size stored in a DT_RELRENT/DT_ANDROID_RELRENT entry.
# RUN: yaml2obj --docnum=2 -DRELTYPE=RELR -DTAG1=DT_RELRSZ -DTAG2=DT_RELRENT -DTAG2VAL=0xFF %s -o %t8
# RUN: llvm-readobj --dyn-relocations %t8 2>&1 | FileCheck %s -DFILE=%t8 --check-prefix=INVALID-DT-RELRENT
# RUN: llvm-readelf --dyn-relocations %t8 2>&1 | FileCheck %s -DFILE=%t8 --check-prefix=INVALID-DT-RELRENT
# RUN: yaml2obj --docnum=2 -DRELTYPE=RELR -DTAG1=DT_ANDROID_RELRSZ -DTAG2=DT_ANDROID_RELRENT -DTAG2VAL=0xFF %s -o %t9
# RUN: llvm-readobj --dyn-relocations %t9 2>&1 | FileCheck %s -DFILE=%t9 --check-prefix=INVALID-DT-ANDROID-RELRENT
# RUN: llvm-readelf --dyn-relocations %t9 2>&1 | FileCheck %s -DFILE=%t9 --check-prefix=INVALID-DT-ANDROID-RELRENT

## INVALID-DT-RELRENT:         invalid DT_RELRSZ value (0x18) or DT_RELRENT value (0xff)
## INVALID-DT-ANDROID-RELRENT: invalid DT_ANDROID_RELRSZ value (0x18) or DT_ANDROID_RELRENT value (0xff)

## Show we print a warning for an invalid value of DT_PLTRELSZ, which describes the total size
## of the relocation entries associated with the procedure linkage table.
# RUN: yaml2obj --docnum=3 %s -o %t10
# RUN: llvm-readobj --dyn-relocations %t10 2>&1 | FileCheck %s -DFILE=%t10 --check-prefix=INVALID-DT-PLTRELSZ
# RUN: llvm-readelf --dyn-relocations %t10 2>&1 | FileCheck %s -DFILE=%t10 --check-prefix=INVALID-DT-PLTRELSZ

# INVALID-DT-PLTRELSZ: warning: '[[FILE]]': invalid DT_PLTRELSZ value (0xff){{$}}

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_EXEC
  Machine: EM_X86_64
Sections:
  - Name:  .rela.plt
    Type:  SHT_RELA
    Relocations:
      - Type:   R_X86_64_NONE
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Entries:
      - Tag:   DT_JMPREL
        Value: 0x0
      - Tag:   DT_PLTRELSZ
        Value: 0xFF ## The valid value would be 0x18.
      - Tag:   DT_PLTREL
        Value: 0x7 ## DT_RELA
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .rela.plt
      - Section: .dynamic

## Show we print a warning when dumping dynamic relocations if there is no dynamic symbol table.
# RUN: yaml2obj --docnum=4 %s -o %t11
# RUN: llvm-readobj --dyn-relocations %t11 2>&1 | FileCheck %s -DFILE=%t11 --check-prefix=LLVM-NO-DYNSYM
# RUN: llvm-readelf --dyn-relocations %t11 2>&1 | FileCheck %s -DFILE=%t11 --check-prefix=GNU-NO-DYNSYM

# LLVM-NO-DYNSYM:      Dynamic Relocations {
# LLVM-NO-DYNSYM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 0: no dynamic symbol table found
# LLVM-NO-DYNSYM-NEXT:   0x0 R_X86_64_NONE <corrupt> 0x0
# LLVM-NO-DYNSYM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 1: no dynamic symbol table found
# LLVM-NO-DYNSYM-NEXT:   0x0 R_X86_64_NONE <corrupt> 0x0
# LLVM-NO-DYNSYM-NEXT: }

# GNU-NO-DYNSYM:      'RELA' relocation section at offset 0x78 contains 48 bytes:
# GNU-NO-DYNSYM-NEXT: Offset            Info             Type             Symbol's Value   Symbol's Name + Addend
# GNU-NO-DYNSYM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 0: no dynamic symbol table found
# GNU-NO-DYNSYM-NEXT: 0000000000000000  0000000000000000 R_X86_64_NONE                     <corrupt> + 0
# GNU-NO-DYNSYM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 1: no dynamic symbol table found
# GNU-NO-DYNSYM-NEXT: 0000000000000000  0000000100000000 R_X86_64_NONE                     <corrupt> + 0

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_X86_64
Sections:
  - Name:    .rela.dyn
    Type:    SHT_RELA
    Relocations:
      - Type:   R_X86_64_NONE
      - Type:   R_X86_64_NONE
        Symbol: 0x1
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_RELA
        Value: 0x0
      - Tag:   DT_RELASZ
        Value: 0x30
      - Tag:   DT_RELAENT
        Value: 0x18
      - Tag:   DT_NULL
        Value: 0x0
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .rela.dyn
      - Section: .dynamic

## Show we print a warning when the symbol index of a dynamic relocation is too
## large (goes past the end of the dynamic symbol table).
# RUN: yaml2obj --docnum=5 %s -o %t12
# RUN: llvm-readobj --dyn-relocations %t12 2>&1 | FileCheck %s -DFILE=%t12 --check-prefix=LLVM-INVALID-DYNSYM
# RUN: llvm-readelf --dyn-relocations %t12 2>&1 | FileCheck %s -DFILE=%t12 --check-prefix=GNU-INVALID-DYNSYM

# LLVM-INVALID-DYNSYM:      Dynamic Relocations {
# LLVM-INVALID-DYNSYM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 2: index is greater than or equal to the number of dynamic symbols (2)
# LLVM-INVALID-DYNSYM-NEXT:   0x0 R_X86_64_NONE <corrupt> 0x0
# LLVM-INVALID-DYNSYM-NEXT: }

# GNU-INVALID-DYNSYM:      'RELA' relocation section at offset 0x78 contains 24 bytes:
# GNU-INVALID-DYNSYM-NEXT: Offset            Info             Type             Symbol's Value   Symbol's Name + Addend
# GNU-INVALID-DYNSYM-NEXT: warning: '[[FILE]]': unable to get name of the dynamic symbol with index 2: index is greater than or equal to the number of dynamic symbols (2)
# GNU-INVALID-DYNSYM-NEXT: 0000000000000000  0000000200000000 R_X86_64_NONE                     <corrupt> + 0

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_X86_64
Sections:
  - Name:    .rela.dyn
    Type:    SHT_RELA
    Relocations:
      - Type:   R_X86_64_NONE
        Symbol: 0x2
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_RELA
        Value: 0x0
      - Tag:   DT_RELASZ
        Value: 0x18
      - Tag:   DT_RELAENT
        Value: 0x18
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols:
  - Name: foo
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .rela.dyn
      - Section: .dynamic

## Show that when we have both REL and RELA relocations, we dump both sets.
# RUN: yaml2obj --docnum=6 %s -o %t13
# RUN: llvm-readobj --dyn-relocations %t13 2>&1 | FileCheck %s -DFILE=%t13 --check-prefix=BOTH-RELA-REL-LLVM
# RUN: llvm-readelf --dyn-relocations %t13 2>&1 | FileCheck %s -DFILE=%t13 --check-prefix=BOTH-RELA-REL-GNU

# BOTH-RELA-REL-LLVM:      Dynamic Relocations {
# BOTH-RELA-REL-LLVM-NEXT:   0x1 R_X86_64_NONE - 0x0
# BOTH-RELA-REL-LLVM-NEXT:   0x2 R_X86_64_NONE - 0x0
# BOTH-RELA-REL-LLVM-NEXT: }

# BOTH-RELA-REL-GNU:       'RELA' relocation section at offset 0x78 contains 24 bytes:
# BOTH-RELA-REL-GNU-NEXT:    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
# BOTH-RELA-REL-GNU-NEXT:  0000000000000001  0000000000000000 R_X86_64_NONE                     0
# BOTH-RELA-REL-GNU-EMPTY:
# BOTH-RELA-REL-GNU:       'REL' relocation section at offset 0x90 contains 16 bytes:
# BOTH-RELA-REL-GNU-NEXT:    Offset             Info             Type               Symbol's Value  Symbol's Name
# BOTH-RELA-REL-GNU-NEXT:  0000000000000002  0000000000000000 R_X86_64_NONE

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_X86_64
Sections:
  - Name: .rela.dyn
    Type: SHT_RELA
    Relocations:
      - Type:   R_X86_64_NONE
        Offset: 0x1
  - Name: .rel.dyn
    Type: SHT_REL
    Relocations:
      - Type:   R_X86_64_NONE
        Offset: 0x2
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_RELA
        Value: 0x0
      - Tag:   DT_RELASZ
        Value: 0x18
      - Tag:   DT_RELAENT
        Value: 0x18
## 0x18 == offset of .rel.dyn == size of .rela.dyn section.
      - Tag:   DT_REL
        Value: 0x18
      - Tag:   DT_RELSZ
        Value: 0x10
      - Tag:   DT_RELENT
        Value: 0x10
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type:  PT_LOAD
    Sections:
      - Section: .rela.dyn
      - Section: .rel.dyn
      - Section: .dynamic