放在哪里以及如何访问静态常量数据?

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

我有一些静态的常量数据需要能够在运行时检索。 我需要将该数据放在哪里以及如何访问它?

我试过将数据放入 .text.data,并使用 ld r24, X。我也试过使用 GDB 的 print 命令。但是,使用所有这些方法,我总是看到 0 结果。

尝试 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

在运行时,这给了我

=> 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
 = 0
(gdb) p *0x800103
 = 0

尝试 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  ; ????

然后在运行时:

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
 = 0
(gdb) p *0x8000d5
 = 0

我设法通过将静态数据放入 .text(即这是我最初问题中的 "approach 2")并使用 lpm instead of ld 加载它来实现此功能。