Search code examples
assemblyavrstatic-linkingobject-filesmemory-layout

Where to put and how to access static, constant data?


I have some static, constant data that I need to be able to retrieve at runtime. Where do I need to put that data and how can I access it?

I've tried putting the data in .text and in .data, and using ld r24, X. I've also tried using GDB's print command. However, with all of these approaches, I am always seeing a 0 result.

Try 1: .data:

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         00000010  00800100  000000d2  00000146  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  1 .text         000000d2  00000000  00000000  00000074  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .stab         000006cc  00000000  00000000  00000158  2**2
                  CONTENTS, READONLY, DEBUGGING
  3 .stabstr      00000054  00000000  00000000  00000824  2**0
                  CONTENTS, READONLY, DEBUGGING
Contents of section .data:
 800100 01010101 01000000 00000000 00000100  ................

Disassembly of section .text:
...
  c2:   a0 50           subi    r26, 0x00       ; 0
  c4:   bf 4f           sbci    r27, 0xFF       ; 255
  c6:   8c 91           ld      r24, X

At runtime, this gives me

=> 0x000000c6 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+18>: 8c 91   ld  r24, X
(gdb) i r r26 r27
r26            0x3  3
r27            0x1  1
(gdb) ni
0x000000c8 in external::decode::hbbb21fc0c1cdbaf3 ()
=> 0x000000c8 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+20>: 08 95   ret
(gdb) i r r24
r24            0x0  0
(gdb) p *0x103
$8 = 0
(gdb) p *0x800103
$9 = 0

Try 2: .text:

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         00000000  00800100  000000e1  00000155  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  1 .text         000000e1  00000000  00000000  00000074  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .stab         000006cc  00000000  00000000  00000158  2**2
                  CONTENTS, READONLY, DEBUGGING
  3 .stabstr      00000054  00000000  00000000  00000824  2**0
                  CONTENTS, READONLY, DEBUGGING

Disassembly of section .text:
...
  c2:   ae 52           subi    r26, 0x2E       ; 46
  c4:   bf 4f           sbci    r27, 0xFF       ; 255
  c6:   8c 91           ld      r24, X
...
000000d2 <_etext>:
  d2:   01 01           movw    r0, r2
  d4:   01 01           movw    r0, r2
  d6:   01 00           .word   0x0001  ; ????
        ...
  e0:   01 00           Address 0x00000000000000e0 is out of bounds.
.word   0xffff  ; ????

and then at runtime:

0x000000c6 in external::decode::hbbb21fc0c1cdbaf3 ()
=> 0x000000c6 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+18>: 8c 91   ld  r24, X
(gdb) i r r26 r27
r26            0xd5 213
r27            0x0  0
(gdb) ni
0x000000c8 in external::decode::hbbb21fc0c1cdbaf3 ()
=> 0x000000c8 <_ZN8external6decode17hbbb21fc0c1cdbaf3E+20>: 08 95   ret
(gdb) i r r24
r24            0x0  0
(gdb) p *0xd5
$1 = 0
(gdb) p *0x8000d5
$2 = 0

Solution

  • I managed to get this working by putting the static data in .text (i.e. this is "approach 2" from my original question), and using lpm instead of ld to load it.