0

I make an elf file from an intel-hex file using riscv64-unknown-elf-objcopy.

exact command:

riscv64-unknown-elf-objcopy -O elf32-littleriscv --set-start 0x10000000 --rename-section .sec1=.text image32.ihex image32.elf

the result looks like this:

$ riscv64-unknown-elf-readelf image32.elf -h
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           RISC-V
  Version:                           0x1
  Entry point address:               0x10000000
  Start of program headers:          0 (bytes into file)
  Start of section headers:          136148 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         8
  Section header string table index: 7

My purpose is to execute it using 'Spike'.

Spike does not accept my elf file:

spike --isa=RV32IM --priv=MU -m0x10000000:0x00200000 --real-time-clint image32.elf
spike: ../fesvr/elfloader.cc:36: std::map<std::__cxx11::basic_string<char>, long unsigned int> load_elf(const char*, memif_t*, reg_t*): Assertion `IS_ELF_EXEC(*eh64)' failed.

It does accept elf files with the following header:

$ riscv64-unknown-elf-readelf ok.elf -h
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           RISC-V
  Version:                           0x1
  Entry point address:               0x10000000
  Start of program headers:          52 (bytes into file)
  Start of section headers:          54672 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         3
  Size of section headers:           40 (bytes)
  Number of section headers:         23
  Section header string table index: 22

I guess I need to change the "Type" field from "REL" to "EXEC", but I do not see any option for that in objcopy.

acapola
  • 1,078
  • 1
  • 12
  • 23
  • 1
    For reasons explained here: https://stackoverflow.com/a/22188584 you should _never_ use `objdump` on ELF files. Please edit your question with output from `readelf`. – Employed Russian Oct 24 '22 at 04:43
  • @EmployedRussian thanks for your feedback, I have put the output of readelf now – acapola Oct 27 '22 at 08:45
  • The `image32.elf` is an `ET_REL`, but you want `ET_EXEC`. What was the `objcopy` command you used to convert `ihex` to `ELF`? – Employed Russian Oct 27 '22 at 15:13
  • @EmployedRussian you are correct about my goal. I added the exact command which I use to produce the elf. – acapola Oct 28 '22 at 08:54

1 Answers1

0

This post suggests that the following sequence might do what you want:

riscv64-unknown-elf-objcopy  -I ihex   -O binary image32.ihex image32.bin
riscv64-unknown-elf-objcopy  -I binary -O riscv64-unknown-elf image32.bin image32.elf

But I am just guessing :-(

Employed Russian
  • 199,314
  • 34
  • 295
  • 362