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

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

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

我尝试将数据放入 .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
$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

最佳答案

我设法通过将静态数据放入 .text 中(即这是我原来问题中的“方法 2”)并使用 lpm 来实现此工作。而不是ld加载它。

关于assembly - 在哪里放置以及如何访问静态、常量数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43821081/

相关文章:

c - 在 AVR Atmega88-PA 上设置 UART 时遇到问题

c++ - 如何优化 C++ avr 代码

c++ - mingw下Qt 5静态编译失败引用off64_t

c++ - 链接到 boost 1.63 静态库时引用未定义

assembly - 为什么表达式 `10 + 32` 不使用堆栈而 `10 + -32` 使用堆栈?

assembly - 当您写入标签时,Assembly 是否会创建内存位置?

c - 使用保存的 EBP 值识别堆栈中的堆栈帧

c - 如何将十六进制字符串从 uart 发送到微 Controller 并将其存储为整数以供将来的 "if"语句使用

gcc - 二进制 AND (&) 的无效操作数

c++ - 是否可以编译具有在编译时无法解析的外部依赖项的静态库?