CORE-V

We have a static relocation in bfd/elfnn-riscv.c (perform_relocation) which switches on the instruction type and encodes the instruction.

The way we were doing was we would have a R_RISCV_RELOCID as a case which would trigger a function, riscv_elf_custom_relocid() (the one similar to add/sub).

riscv_elf_custom_relocid() lives in bfd/elfxx-riscv.c and it is a bfd_reloc_status_type function, which is the same type as perform relocation.

Therefore, I think it is breaking because we shouldn’t call custom_relocid() from perform_relocation() as they don’t exist in the same space.

../bfd/.libs/libbfd.a(elf32-riscv.o): In function `perform_relocation':
/home/pietraferreira/corev/build/binutils-gdb/bfd/elfnn-riscv.c:1653: undefined reference to `riscv_elf_custom_relocid'

BFD_RELOC_RISCV_CVPCREL_UI12

  • PC relative.
  • Local.
  • Requires a 1-bit right shift.
  • Relocation number: 224
  • dst_mask: ENCODE_ITYPE_IMM (-1U)

It is a 12-bit relocation for 12-bit immediates found in cv.starti and cv.endi.

  • bfd_reloc_status_type: if this is non-null, then the supplied function is called rather than the normal function. This allows really strange relocation methods to be accommodated.

  • riscv_reloc_name_lookup and riscv_elf_rtype_to_howto both get howto_table elements, so maybe I need to have something looking into the SECONDARYhowto_table.

GDB

To run it:

gdb --args ~/corev/build/binutils-gdb/ld/ld-new -plugin /home/pietraferreira/corev/install/libexec/gcc/riscv32-corev-elf/12.0.0/liblto_plugin.so -plugin-opt=/home/pietraferreira/corev/install/libexec/gcc/riscv32-corev-elf/12.0.0/lto-wrapper -plugin-opt=-fresolution=test-03.o.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgloss -plugin-opt=-pass-through=-lgcc --sysroot=/home/pietraferreira/corev/install/riscv32-corev-elf -melf32lriscv -o test-03.o -L/home/pietraferreira/corev/install/lib/gcc/riscv32-corev-elf/12.0.0 -L/home/pietraferreira/corev/install/lib/gcc/riscv32-corev-elf/12.0.0/../../../../riscv32-corev-elf/lib -L/home/pietraferreira/corev/install/riscv32-corev-elf/lib test-03.o -lgcc --start-group -lc -lgloss --end-group -lgcc

To compile:

riscv32-corev-elf-gcc -march=rv32imc_xcorevhwlp1p0 -g test-addi.s -o test-03.o -save-temps -nostartfiles -v

To add missing directories to GDB:

# in GDB:
dir ~/projects/corev/binutils-gdb/bfd/

To get debug information:

make clean
make all-ld CFLAGS='-O0 -g3'

BFD

  • Sections: each section contains the name of the section, the section’s original address in the object file, size and alignment information, various flags, and pointers into other BFD data structures.

  • Symbols: they each contain a pointer to the information for the object file which originally defined it, its name, its value and various flag bits. When a BFD back end reads in a symbol table, it relocates all symbols to make them relative to the base of the section where they were defined.

Sanitisers

int atoi(const char *str);
 
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 
int main(int argc, char **argv) {
return arr[atoi(argv[1])];
}

If we enable fsanitize=address, it runs about 3x slower but it “catches overflows”.

  • daily
  • work/relocation-prototype programming-languages: created: 2022-07-04

2022-07-04


CORE-V

We have a static relocation in bfd/elfnn-riscv.c (perform_relocation) which switches on the instruction type and encodes the instruction.

The way we were doing was we would have a R_RISCV_RELOCID as a case which would trigger a function, riscv_elf_custom_relocid() (the one similar to add/sub).

riscv_elf_custom_relocid() lives in bfd/elfxx-riscv.c and it is a bfd_reloc_status_type function, which is the same type as perform relocation.

Therefore, I think it is breaking because we shouldn’t call custom_relocid() from perform_relocation() as they don’t exist in the same space.

../bfd/.libs/libbfd.a(elf32-riscv.o): In function `perform_relocation':
/home/pietraferreira/corev/build/binutils-gdb/bfd/elfnn-riscv.c:1653: undefined reference to `riscv_elf_custom_relocid'

BFD_RELOC_RISCV_CVPCREL_UI12

  • PC relative.
  • Local.
  • Requires a 1-bit right shift.
  • Relocation number: 224
  • dst_mask: ENCODE_ITYPE_IMM (-1U)

It is a 12-bit relocation for 12-bit immediates found in cv.starti and cv.endi.

  • bfd_reloc_status_type: if this is non-null, then the supplied function is called rather than the normal function. This allows really strange relocation methods to be accommodated.

  • riscv_reloc_name_lookup and riscv_elf_rtype_to_howto both get howto_table elements, so maybe I need to have something looking into the SECONDARYhowto_table.

GDB

To run it:

gdb --args ~/corev/build/binutils-gdb/ld/ld-new -plugin /home/pietraferreira/corev/install/libexec/gcc/riscv32-corev-elf/12.0.0/liblto_plugin.so -plugin-opt=/home/pietraferreira/corev/install/libexec/gcc/riscv32-corev-elf/12.0.0/lto-wrapper -plugin-opt=-fresolution=test-03.o.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgloss -plugin-opt=-pass-through=-lgcc --sysroot=/home/pietraferreira/corev/install/riscv32-corev-elf -melf32lriscv -o test-03.o -L/home/pietraferreira/corev/install/lib/gcc/riscv32-corev-elf/12.0.0 -L/home/pietraferreira/corev/install/lib/gcc/riscv32-corev-elf/12.0.0/../../../../riscv32-corev-elf/lib -L/home/pietraferreira/corev/install/riscv32-corev-elf/lib test-03.o -lgcc --start-group -lc -lgloss --end-group -lgcc

To compile:

riscv32-corev-elf-gcc -march=rv32imc_xcorevhwlp1p0 -g test-addi.s -o test-03.o -save-temps -nostartfiles -v

To add missing directories to GDB:

# in GDB:
dir ~/projects/corev/binutils-gdb/bfd/

To get debug information:

make clean
make all-ld CFLAGS='-O0 -g3'

BFD

  • Sections: each section contains the name of the section, the section’s original address in the object file, size and alignment information, various flags, and pointers into other BFD data structures.

  • Symbols: they each contain a pointer to the information for the object file which originally defined it, its name, its value and various flag bits. When a BFD back end reads in a symbol table, it relocates all symbols to make them relative to the base of the section where they were defined.

Sanitisers

int atoi(const char *str);
 
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 
int main(int argc, char **argv) {
return arr[atoi(argv[1])];
}

If we enable fsanitize=address, it runs about 3x slower but it “catches overflows”.