Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fixed flash address calculation (alternative version) #49

Merged
merged 1 commit into from
Sep 9, 2022

Conversation

bradjc
Copy link
Contributor

@bradjc bradjc commented Sep 8, 2022

We want to use the address of the first section in the segment, regardless of its name.

Rather than looking for .start, we just look for whatever the first section is in the segment as use that address, not the segment address.

We want to use the address of the first section in the segment,
regardless of its name.
Copy link
Contributor

@hudson-ayers hudson-ayers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good. I tried to reason through any scenario where not filtering for the .start section would be a problem and could not think of one. I assume that if a segment is an executable segment, all sections in it should be executable?

@bradjc
Copy link
Contributor Author

bradjc commented Sep 8, 2022

I don't even understand what .start is. This is for accel_leds in libtock-c:

❯ readelf.py -lS build/rv32imac/rv32imac.0x20040060.0x80002800.elf
There are 25 section headers, starting at offset 0x3c87c

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        80003000 00306c 000000 00  WA  0   0  1
  [ 2] .crt0_header      PROGBITS        20040060 001060 000028 00   A  0   0  1
  [ 3] .data             PROGBITS        80003000 003000 00006c 00  WA  0   0  4
  [ 4] .bss              NOBITS          8000306c 00306c 000170 00  WA  0   0  4
  [ 5] .GCC.command.line PROGBITS        00000000 00306c 0000a4 01  MS  0   0  1
  [ 6] .comment          PROGBITS        00000000 003110 000012 01  MS  0   0  1
  [ 7] .riscv.attributes ARM_ATTRIBUTES  00000000 003122 00002b 00      0   0  1
  [ 8] .wfr.app_state    PROGBITS        20040088 00314d 000000 00   W  0   0  1
  [ 9] .text             PROGBITS        20040088 001088 0013b8 00  AX  0   0  4
  [10] .stack            NOBITS          80002800 003800 000800 00  WA  0   0  1
  [11] .debug_info       PROGBITS        00000000 00314d 0171af 00      0   0  1
  [12] .debug_abbrev     PROGBITS        00000000 01a2fc 005875 00      0   0  1
  [13] .debug_loc        PROGBITS        00000000 01fb71 001c0c 00      0   0  1
  [14] .debug_aranges    PROGBITS        00000000 021780 000930 00      0   0  8
  [15] .debug_ranges     PROGBITS        00000000 0220b0 0002e8 00      0   0  1
  [16] .debug_line       PROGBITS        00000000 022398 00d24e 00      0   0  1
  [17] .debug_str        PROGBITS        00000000 02f5e6 00205a 01  MS  0   0  1
  [18] .debug_frame      PROGBITS        00000000 031640 0013c4 00      0   0  4
  [19] .debug_loclists   PROGBITS        00000000 032a04 006cc6 00      0   0  1
  [20] .debug_rnglists   PROGBITS        00000000 0396ca 000f21 00      0   0  1
  [21] .debug_line_str   PROGBITS        00000000 03a5eb 00116b 01  MS  0   0  1
  [22] .symtab           SYMTAB          00000000 03b758 000a70 10     23  83  4
  [23] .strtab           STRTAB          00000000 03c1c8 00059c 00      0   0  1
  [24] .shstrtab         STRTAB          00000000 03c764 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)

Elf file type is EXEC (Executable file)
Entry point is 0x20040088
There are 3 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x001060 0x20040060 0x20040060 0x013e0 0x013e0 R E 0x1000
  LOAD           0x003000 0x80003000 0x20041440 0x0006c 0x001dc RW  0x1000
  LOAD           0x000800 0x80002800 0x80002800 0x00000 0x00800 RW  0x1000

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

and for arm:

❯ readelf.py -lS build/cortex-m4/cortex-m4.elf
There are 30 section headers, starting at offset 0x92920

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        00000800 020800 00006c 04  WA  0   0  4
  [ 2] .crt0_header      PROGBITS        80000000 010000 000028 00  WA  0   0  1
  [ 3] .data             PROGBITS        0000086c 02086c 000070 00  WA  0   0  4
  [ 4] .rel.data         REL             00000000 06e9e8 000028 08   I 27   3  4
  [ 5] .bss              NOBITS          000008dc 0208dc 000170 00  WA  0   0  4
  [ 6] .GCC.command.line PROGBITS        00000000 0208dc 0007d0 01  MS  0   0  1
  [ 7] .comment          PROGBITS        00000000 0210ac 00008b 01  MS  0   0  1
  [ 8] .ARM.attributes   ARM_ATTRIBUTES  00000000 021137 00002e 00      0   0  1
  [ 9] .wfr.app_state    PROGBITS        80000028 021165 000000 00   W  0   0  1
  [10] .text             PROGBITS        80000028 010028 001000 00  AX  0   0  4
  [11] .rel.text         REL             00000000 06ea10 0004f8 08   I 27  10  4
  [12] .stack            NOBITS          00000000 010000 000800 00  WA  0   0  1
  [13] .debug_info       PROGBITS        00000000 021165 026ca5 00      0   0  1
  [14] .rel.debug_info   REL             00000000 06ef08 014aa0 08   I 27  13  4
  [15] .debug_abbrev     PROGBITS        00000000 047e0a 0071ef 00      0   0  1
  [16] .debug_loc        PROGBITS        00000000 04eff9 00c16a 00      0   0  1
  [17] .rel.debug_loc    REL             00000000 0839a8 00bfb8 08   I 27  16  4
  [18] .debug_aranges    PROGBITS        00000000 05b168 0009d8 00      0   0  8
  [19] .rel.debug_arange REL             00000000 08f960 000648 08   I 27  18  4
  [20] .debug_ranges     PROGBITS        00000000 05bb40 0011c8 00      0   0  1
  [21] .rel.debug_ranges REL             00000000 08ffa8 001ae0 08   I 27  20  4
  [22] .debug_line       PROGBITS        00000000 05cd08 00be27 00      0   0  1
  [23] .rel.debug_line   REL             00000000 091a88 000480 08   I 27  22  4
  [24] .debug_str        PROGBITS        00000000 068b2f 00323f 01  MS  0   0  1
  [25] .debug_frame      PROGBITS        00000000 06bd70 0012b4 00      0   0  4
  [26] .rel.debug_frame  REL             00000000 091f08 000910 08   I 27  25  4
  [27] .symtab           SYMTAB          00000000 06d024 001350 10     28 220  4
  [28] .strtab           STRTAB          00000000 06e374 000671 00      0   0  1
  [29] .shstrtab         STRTAB          00000000 092818 000105 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)

Elf file type is EXEC (Executable file)
Entry point is 0x80000029
There are 3 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x010000 0x00000000 0x00000000 0x00000 0x00800 RW  0x10000
  LOAD           0x010000 0x80000000 0x80000000 0x01028 0x01028 RWE 0x10000
  LOAD           0x020800 0x00000800 0x80001028 0x000dc 0x0024c RW  0x10000

 Section to Segment mapping:
  Segment Sections...
   00     .stack
   01     .crt0_header .text
   02     .got .data .bss

@hudson-ayers
Copy link
Contributor

ah... https://github.com/tock/libtock-rs/blob/1d785a043a95d83b410f6a099a6121fc101ca3b7/runtime/libtock_layout.ld#L59

#39 was written under the assumption that libtock-c used the same section name to store the entry point as libtock-rs does

@bradjc
Copy link
Contributor Author

bradjc commented Sep 9, 2022

We should fix this bug, is this fix ok?

Copy link
Member

@lschuermann lschuermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#39 was written under the assumption that libtock-c used the same section name to store the entry point as libtock-rs does

With that reasoning, I'm sufficiently convinced that this fix is obviously a strict improvement over what we have upstream now, probably not breaking libtock-rs apps and better than #48. I will not say that this might not break on some other binaries which rely on diverging layouts.

@bradjc
Copy link
Contributor Author

bradjc commented Sep 9, 2022

With that reasoning, I'm sufficiently convinced that this fix is obviously a strict improvement over what we have upstream now, probably not breaking libtock-rs apps and better than #48. I will not say that this might not break on some other binaries which rely on diverging layouts.

If you know anyone who wants to do some really tedious coding, I think the only real fix is to actually properly parse ELF files, instead of using the ad-hoc methods we have currently.

@hudson-ayers
Copy link
Contributor

I think let's merge this

@bradjc bradjc merged commit 03987b3 into master Sep 9, 2022
@bradjc bradjc deleted the fixed-flash-address-use-first-section branch August 29, 2023 02:25
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.

4 participants