RRON
RRON

Reputation: 1125

What is riscv arch attributes in object file?

.section .text
.global main
main:
      addi t0, a0, 100; # loop counter t0 = 100
    
    loop_start: 
        beq t0, x0, loop_end; # exit loop if counter expires
        addi t0, t0, -1;      # decrement counter
        j loop_start;
    loop_end:
        addi t1, x0, 0; # dummy instruction

when I assembled the entry.S above,

entry.o: file format elf64-littleriscv

Disassembly of section .text:

0000000000000000 <main>:
   0:   06450293                addi    t0,a0,100

0000000000000004 <loop_start>:
   4:   00028663                beqz    t0,10 <loop_end>
   8:   fff28293                addi    t0,t0,-1
   c:   ff9ff06f                j       4 <loop_start>

0000000000000010 <loop_end>:
  10:   00000313                li      t1,0

Disassembly of section .riscv.attributes:

0000000000000000 <.riscv.attributes>:
   0:   2d41                    addiw   s10,s10,16
   2:   0000                    unimp
   4:   7200                    ld      s0,32(a2)
   6:   7369                    lui     t1,0xffffa
   8:   01007663                bgeu    zero,a6,14 <.riscv.attributes+0x14>
   c:   00000023                sb      zero,0(zero) # 0 <.riscv.attributes>
  10:   7205                    lui     tp,0xfffe1
  12:   3676                    fld     fa2,376(sp)
  14:   6934                    ld      a3,80(a0)
  16:   7032                    0x7032
  18:   5f30                    lw      a2,120(a4)
  1a:   326d                    addiw   tp,tp,-5
  1c:   3070                    fld     fa2,224(s0)
  1e:   615f 7032 5f30          0x5f307032615f
  24:   3266                    fld     ft4,120(sp)
  26:   3070                    fld     fa2,224(s0)
  28:   645f 7032 0030          0x307032645f

What is the riscv.attributes in the object file and what is its significance?

Upvotes: 2

Views: 1358

Answers (1)

Olsonist
Olsonist

Reputation: 2403

An ELF object file is composed of sections. Each section will have a Elf64_Shdr section header which will have a sh_type section type field. The value for that field in your section is 0x70000003U which could be SHT_ARM_ATTRIBUTES, SHT_MSP430_ATTRIBUTES or SHT_RISCV_ATTRIBUTES.

Which is it? Well, it is processor specific and the specific processor is EM_RISCV. FWIW, the 7 means the start of processor specific attributes but processors can share these numbers since em_machine disambiguates them.

Parsing starts with the ELF file header which says it's an object file but also says e_machine == EM_RISCV. So since this is a RISCV object file, the type of that section (sh_type) is interpreted as being SHT_RISCV_ATTRIBUTES and the disassembler does you the favor of saying it's riscv.attributes.

Long story short, the attribute just labels it as a 'RISCV attribute section'. Do you notice how it doesn't make any sense as RISCV code? That's because it's an array of attributes and the disassembly is therefore just garbage.

The 0x41 is AttrMagic from ELFAttributes.h But you're going to have to do more work with objdump or whichever disassembler you are using to properly format that. objdump -x should dump all the headers.

For LLVM, all of the gory detail can be found here:

https://reviews.llvm.org/D74023

and here:

https://llvm.org/doxygen/BinaryFormat_2ELF_8h_source.html https://llvm.org/doxygen/ELFAttributes_8h_source.html

Upvotes: 4

Related Questions