【发布时间】:2020-01-02 15:54:31
【问题描述】:
对于我自己的malloc动态内存分配实现,我想在运行时访问堆基地址和堆限制地址。我知道它们在我的 startup.s 文件中被导出为全局变量。我怎样才能访问这些地址? 目标平台是带有 GCC 的 ARM Cortex-M4。
用 .globl 声明的符号对链接器来说是已知的,并且应该可以使用 c 代码中的 extern 符号访问。但是使用这种方法,我得到的值与生成的 .map 文件中的值不同。
下面是 startup.s 文件中定义的堆符号:
#ifdef __HEAP_SIZE
.equ Heap_Size, __HEAP_SIZE
#else
.equ Heap_Size, 0x00000C00
#endif
.globl __HeapBase
.globl __HeapLimit
__HeapBase:
.if Heap_Size
.space Heap_Size
.endif
.size __HeapBase, . - __HeapBase
__HeapLimit:
.size __HeapLimit, . - __HeapLimit
.section .vectors
.align 2
.globl __Vectors
这是我在 c 函数中的访问权限:
extern volatile __HeapBase;
extern volatile __HeapLimit;
static void heaptest(void) {
uint32_t limit;
uint32_t base;
base = __HeapBase;
limit = __HeapLimit;
}
调试这段代码,我得到了生成的 .map 文件中指定的其他地址:
.heap 0x200317a0 0x40000
0x200317a0 __HeapBase = .
...
0x200717a0 __HeapLimit = .
...
所以我希望 __HeapBase 的输出为 0x200317a0,__HeapLimit 的输出为 0x200717a0,但实际输出我得到的是 0xf377aaa2 和 0xac69254。
【问题讨论】:
-
你是如何编译、链接和运行它的?添加所需的其余代码以获得minimal and reproducible example(强调reproducible)。