Skip to content

Conversation

pekangas
Copy link

Take read-only data segments into account when determining where flash contents start. This is specifically important if the Tock CRT0 header is in a separate data section before the start of .text.

The change is trivial and fixes #92 for me, but would benefit from someone else thinking the logic through in case I missed some unintended consequences. I am not entirely clear on why elf2tab does some of the things it does...

Take read-only data segments into account when determining where flash
contents start. This is specifically important if the Tock CRT0 header
is in a separate data section before the start of `.text`.
@bradjc
Copy link
Contributor

bradjc commented Jun 20, 2025

I'm looking at blink compiled for riscv:

readelf.py -e examples/blink/build/rv32imc/rv32imc.0x00080080.0x40008000.elf
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:                           <unknown>
  Version:                           0x1
  Entry point address:               0x800a8
  Start of program headers:          52 (bytes into file)
  Start of section headers:          241876 (bytes into file)
  Flags:                             0x1
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         4
  Size of section headers:           40 (bytes)
  Number of section headers:         25
  Section header string table index: 24

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .got              PROGBITS        40008800 00485c 000000 00  WA  0   0  1
  [ 2] .crt0_header      PROGBITS        00080080 001080 000028 00   A  0   0  1
  [ 3] .data             PROGBITS        40008800 004800 00005c 00  WA  0   0  4
  [ 4] .GCC.command.line PROGBITS        00000000 00485c 0000a7 01  MS  0   0  1
  [ 5] .comment          PROGBITS        00000000 004903 000051 01  MS  0   0  1
  [ 6] .riscv.attributes ARM_ATTRIBUTES  00000000 004954 00002f 00      0   0  1
  [ 7] .wfr.app_state    PROGBITS        000800a8 004983 000000 00   W  0   0  1
  [ 8] .text             PROGBITS        000800a8 0010a8 002ca8 00  AX  0   0  4
  [ 9] .stack            NOBITS          40008000 005000 000800 00  WA  0   0  1
  [10] .bss              NOBITS          4000885c 00485c 0002ac 00  WA  0   0  4
  [11] .debug_info       PROGBITS        00000000 004983 017764 00      0   0  1
  [12] .debug_abbrev     PROGBITS        00000000 01c0e7 005bf8 00      0   0  1
  [13] .debug_loc        PROGBITS        00000000 021cdf 002e32 00      0   0  1
  [14] .debug_aranges    PROGBITS        00000000 024b18 000a40 00      0   0  8
  [15] .debug_ranges     PROGBITS        00000000 025558 000580 00      0   0  1
  [16] .debug_line       PROGBITS        00000000 025ad8 00aec1 00      0   0  1
  [17] .debug_str        PROGBITS        00000000 030999 002374 01  MS  0   0  1
  [18] .debug_frame      PROGBITS        00000000 032d10 001598 00      0   0  4
  [19] .debug_line_str   PROGBITS        00000000 0342a8 000d1e 01  MS  0   0  1
  [20] .debug_loclists   PROGBITS        00000000 034fc6 003c46 00      0   0  1
  [21] .debug_rnglists   PROGBITS        00000000 038c0c 000829 00      0   0  1
  [22] .symtab           SYMTAB          00000000 039438 0012f0 10     23 195  4
  [23] .strtab           STRTAB          00000000 03a728 000892 00      0   0  1
  [24] .shstrtab         STRTAB          00000000 03afba 000117 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  <unknown>      0x004954 0x00000000 0x00000000 0x0002f 0x00000 R   0x1
  LOAD           0x001080 0x00080080 0x00080080 0x02cd0 0x02cd0 R E 0x1000
  LOAD           0x004800 0x40008800 0x00082d50 0x0005c 0x00308 RW  0x1000
  LOAD           0x000000 0x40008000 0x40008000 0x00000 0x00800 RW  0x1000

 Section to Segment mapping:
  Segment Sections...
   00     .riscv.attributes
   01     .crt0_header .text
   02     .data .bss
   03     .stack

There is this "unknown" segment which is only R but isn't anything we want.

@bradjc
Copy link
Contributor

bradjc commented Jun 20, 2025

This is specifically important if the Tock CRT0 header is in a separate data section before the start of .text.

Do you mean in a separate data segment? I think the crt0 header has always been in its own section before .text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider read-only data when determining where flash starts

2 participants