在哪里放置以及如何访问静态,恒定的数据?

时间:2017-05-06 13:35:21

标签: assembly avr static-linking object-files memory-layout

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

我已尝试将数据放入.text.data,并使用ld r24, X。我也尝试过使用GDB&#39 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
$8 = 0
(gdb) p *0x800103
$9 = 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
$1 = 0
(gdb) p *0x8000d5
$2 = 0

1 个答案:

答案 0 :(得分:2)

我设法通过将静态数据放在.text中来实现这一点(即,这是&#34;从原始问题开始的第2和第34步),并使用lpm代替{{ 3}}加载它。