## Ensures that implicitly added sections can be ordered within Sections.

# RUN: yaml2obj --docnum=1 %s -o %t1
# RUN: llvm-readobj --sections %t1 | FileCheck %s --check-prefix=SECTION
# RUN: llvm-nm --dynamic %t1 | FileCheck %s --check-prefix=SYMBOL

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name: .data
    Type: SHT_PROGBITS
    Flags: [ SHF_ALLOC, SHF_WRITE ]
DynamicSymbols:
  - Name: dynlocal
    Type: STT_OBJECT
    Section: .data
    Binding: STB_LOCAL
  - Name: dynglobal
    Type: STT_OBJECT
    Section: .data
    Binding: STB_GLOBAL
  - Name: dynweak
    Type: STT_OBJECT
    Section: .data
    Binding: STB_WEAK

# SECTION: Name: .dynsym
# SECTION-NEXT: Type: SHT_DYNSYM
# SECTION-NEXT: Flags
# SECTION-NEXT: SHF_ALLOC
# SECTION: Name: .dynstr
# SECTION-NEXT: Type: SHT_STRTAB
# SECTION-NEXT: Flags
# SECTION-NEXT: SHF_ALLOC

# SYMBOL-DAG: d dynlocal
# SYMBOL-DAG: D dynglobal
# SYMBOL-DAG: V dynweak

## Check we can use numeric values to refer to sections.

# RUN: yaml2obj --docnum=2 -DSEC=0xff %s -o %t2
# RUN: llvm-readobj --dyn-symbols %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=NUM

# NUM:      Name: foo
# NUM:      Section:
# NUM-SAME: .data (0x1)

# NUM:      Name: bar
# NUM:      Section:
# NUM-SAME: .dynsym (0x2)

# NUM:      Name: zed1
# NUM: warning: '[[FILE]]': invalid section index: 255
# NUM:      Section:
# NUM-SAME: <?> (0xFF)

# NUM:      Name: zed2
# NUM:      Section:
# NUM-SAME: Undefined (0x0)

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name: .data
    Type: SHT_PROGBITS
DynamicSymbols:
  - Name:    foo
    Section: 1
  - Name:    bar
    Section: 2
  - Name:    zed1
    Section: [[SEC=<none>]]
## The case when no "Section" key is set.
  - Name: zed2

## Check that by default no section is set.

# RUN: yaml2obj --docnum=2 %s -o %t2.none
# RUN: llvm-readobj --dyn-symbols %t2.none 2>&1 | \
# RUN:   FileCheck -DFILE=%t2.none %s --check-prefix=NONE

# NONE:      Name: zed1
# NONE:      Section:
# NONE-SAME: Undefined (0x0)
# NONE:      Name: zed2
# NONE:      Section:
# NONE-SAME: Undefined (0x0)

## Check we report errors when unknown sections are referenced by dynamic symbols.

# RUN: not yaml2obj --docnum=3 %s 2>&1 | FileCheck -DFILE=%t3 %s --check-prefix=ERR

# ERR: error: unknown section referenced: '.sec1' by YAML symbol 'foo'
# ERR: error: unknown section referenced: '.sec2' by YAML symbol 'bar'
# ERR: error: unknown section referenced: '' by YAML symbol 'zed'

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name: .data
    Type: SHT_PROGBITS
DynamicSymbols:
  - Name:    foo
    Section: .sec1
  - Name:    bar
    Section: .sec2
  - Name:    zed
    Section: ''
