Advertisement
xerpi

RISC-V relocs

Feb 6th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 KB | None | 0 0
  1. typedef void (*fptr)(void);
  2.  
  3. void func1(void) {}
  4. void func2(void) {}
  5.  
  6. const fptr table[] = {
  7.     func1,
  8.     func2,
  9. };
  10.  
  11. int _start(int i)
  12. {
  13.     table[i]();
  14. }
  15.  
  16. -------------------------------------------------------------------------------
  17. $ riscv64-unknown-elf-gcc -nostdlib -nostartfiles -T linker.ld -fPIE -static-pie simple.c
  18.  
  19. $ riscv64-unknown-elf-readelf -a a.out -W
  20. ELF Header:
  21.   Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  22.   Class:                             ELF64
  23.   Data:                              2's complement, little endian
  24.  Version:                           1 (current)
  25.  OS/ABI:                            UNIX - System V
  26.  ABI Version:                       0
  27.  Type:                              EXEC (Executable file)
  28.  Machine:                           RISC-V
  29.  Version:                           0x1
  30.  Entry point address:               0x1038
  31.  Start of program headers:          64 (bytes into file)
  32.  Start of section headers:          4584 (bytes into file)
  33.  Flags:                             0x2, single-float ABI
  34.  Size of this header:               64 (bytes)
  35.  Size of program headers:           56 (bytes)
  36.  Number of program headers:         1
  37.  Size of section headers:           64 (bytes)
  38.  Number of section headers:         7
  39.  Section header string table index: 6
  40.  
  41. Section Headers:
  42.  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  43.  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  44.  [ 1] .text             PROGBITS        0000000000001000 001000 000084 00  AX  0   0  4
  45.  [ 2] .data.rel.ro.local PROGBITS        0000000000001088 001088 000010 00  WA  0   0  8
  46.  [ 3] .comment          PROGBITS        0000000000000000 001098 000011 01  MS  0   0  1
  47.  [ 4] .symtab           SYMTAB          0000000000000000 0010b0 0000d8 18      5   5  8
  48.  [ 5] .strtab           STRTAB          0000000000000000 001188 000023 00      0   0  1
  49.  [ 6] .shstrtab         STRTAB          0000000000000000 0011ab 00003d 00      0   0  1
  50. Key to Flags:
  51.  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  52.  L (link order), O (extra OS processing required), G (group), T (TLS),
  53.  C (compressed), x (unknown), o (OS specific), E (exclude),
  54.  p (processor specific)
  55.  
  56. There are no section groups in this file.
  57.  
  58. Program Headers:
  59.  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  60.  LOAD           0x001000 0x0000000000001000 0x0000000000001000 0x000098 0x000098 RWE 0x1000
  61.  
  62. Section to Segment mapping:
  63.  Segment Sections...
  64.   00     .text .data.rel.ro.local
  65.  
  66. There is no dynamic section in this file.
  67.  
  68. There are no relocations in this file.
  69.  
  70. The decoding of unwind sections for machine type RISC-V is not currently supported.
  71.  
  72. Symbol table '.symtab' contains 9 entries:
  73.   Num:    Value          Size Type    Bind   Vis      Ndx Name
  74.     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
  75.     1: 0000000000001000     0 SECTION LOCAL  DEFAULT    1
  76.     2: 0000000000001088     0 SECTION LOCAL  DEFAULT    2
  77.     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3
  78.     4: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS simple.c
  79.     5: 0000000000001088    16 OBJECT  GLOBAL DEFAULT    2 table
  80.     6: 0000000000001000    28 FUNC    GLOBAL DEFAULT    1 func1
  81.     7: 0000000000001038    76 FUNC    GLOBAL DEFAULT    1 _start
  82.     8: 000000000000101c    28 FUNC    GLOBAL DEFAULT    1 func2
  83.  
  84. No version information found in this file.
  85.  
  86. $ riscv64-unknown-elf-readelf -r a.out -W
  87. There are no relocations in this file.
  88.  
  89. $ riscv64-unknown-elf-objdump -r -D -S a.out
  90.  
  91.     1050:   00000717            auipc   a4,0x0
  92.     1054:   03870713            addi    a4,a4,56 # 1088 <table>
  93.     1058:   fec42783            lw  a5,-20(s0)
  94.     105c:   00379793            slli    a5,a5,0x3
  95.     1060:   00f707b3            add a5,a4,a5
  96.     1064:   0007b783            ld  a5,0(a5)
  97.     1068:   000780e7            jalr    a5
  98. Disassembly of section .data.rel.ro.local:
  99.  
  100. 0000000000001088 <table>:
  101.     1088:   1000                    addi    s0,sp,32
  102.     108a:   0000                    unimp
  103.     108c:   0000                    unimp
  104.     108e:   0000                    unimp
  105.     1090:   101c                    addi    a5,sp,32
  106.     1092:   0000                    unimp
  107.     1094:   0000                    unimp
  108.     ...
  109.  
  110. -------------------------------------------------------------------------------
  111.  
  112. $ riscv64-unknown-elf-gcc -g3 -nostdlib -nostartfiles -T linker.ld -fPIE -static-pie -c simple.c
  113.  
  114. $ riscv64-unknown-elf-readelf -r simple.o -W
  115.  
  116. Relocation section '.rela.text' at offset 0x6108 contains 4 entries:
  117.    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
  118. 0000000000000050  0000018100000017 R_RISCV_PCREL_HI20     0000000000000000 table + 0
  119. 0000000000000050  0000000000000033 R_RISCV_RELAX                             0
  120. 0000000000000054  0000001e00000018 R_RISCV_PCREL_LO12_I   0000000000000050 .L0  + 0
  121. 0000000000000054  0000000000000033 R_RISCV_RELAX                             0
  122.  
  123. Relocation section '.rela.data.rel.ro.local' at offset 0x6168 contains 2 entries:
  124.    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
  125. 0000000000000000  0000017f00000002 R_RISCV_64             0000000000000000 func1 + 0
  126. 0000000000000008  0000018000000002 R_RISCV_64             000000000000001c func2 + 0
  127.  
  128.  
  129. $ riscv64-unknown-elf-objdump -r -D -S simple.o
  130.  
  131. 0000000000000050 <.L0 >:
  132.    table[i]();
  133.  50:   00000717                auipc   a4,0x0
  134.                        50: R_RISCV_PCREL_HI20  table
  135.                        50: R_RISCV_RELAX       *ABS*
  136.  54:   00070713                mv      a4,a4
  137.                        54: R_RISCV_PCREL_LO12_I        .L0
  138.                        54: R_RISCV_RELAX       *ABS*
  139.  58:   fec42783                lw      a5,-20(s0)
  140.  5c:   00379793                slli    a5,a5,0x3
  141.  60:   00f707b3                add     a5,a4,a5
  142.  64:   0007b783                ld      a5,0(a5)
  143.  
  144. 0000000000000068 <.L0 >:
  145.  68:   000780e7                jalr    a5
  146.  
  147.  
  148. Disassembly of section .data.rel.ro.local:
  149.  
  150. 0000000000000000 <table>:
  151.        ...
  152.                        0: R_RISCV_64   func1
  153.                        8: R_RISCV_64   func2
  154.  
  155. --------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement