'Why are zero-initialized globals defined outside of bss using this linker script?
I have the following two variables, KMEM_END and MEM_TOP:
extern char _kernel_mem_end[];
extern char _physical_mem_end[];
uint64_t KMEM_END;
uint64_t MEM_TOP;
void kmem_init()
{
kprintf("\nkmem_end: %p\n", (uint64_t) _kernel_mem_end);
kprintf("phys_top: %p\n", (uint64_t) _physical_mem_end);
kprintf("&KMEM_END: %p\n", &KMEM_END);
kprintf("&MEM_TOP: %p\n", &MEM_TOP);
// ...
}
I have _kernel_mem_end and _physical_mem_end provided by the linker script, here:
OUTPUT_ARCH("riscv");
ENTRY(start);
MEMORY {
/* The -kernel QEMU option copies the image to that specific address. */
ram (wxa) : ORIGIN = 0x80000000, LENGTH = 128M
}
SECTIONS {
/* Starts at the address 0x80000000. */
.text : ALIGN(4K) {
PROVIDE(_text_start = .);
*(.init)
*(.text .text.*)
. = ALIGN(4K);
PROVIDE(_text_end = .);
} >ram
.data : ALIGN(4K) {
PROVIDE(_data_start = .);
*(.data .data.*)
. = ALIGN(16);
*(.sdata .sdata.*)
. = ALIGN(4K);
PROVIDE(_data_end = .);
} >ram
.rodata : ALIGN(4K) {
PROVIDE(_rodata_start = .);
*(.rodata .rodata.*)
. = ALIGN(16);
*(.srodata .srodata.*)
. = ALIGN(4K);
PROVIDE(_rodata_end = .);
} >ram
.bss : ALIGN(4K) {
PROVIDE(_bss_start = .);
*(.bss .bss.*)
. = ALIGN(4K);
PROVIDE(_stack_start = .);
. += 0x10000;
PROVIDE(_stack_end = .);
PROVIDE(_bss_end = .);
} >ram
. = ALIGN(4K);
PROVIDE(_kernel_mem_end = .);
PROVIDE(_physical_mem_end = ORIGIN(ram) + LENGTH(ram));
}
As the variables are globals and uninitialized (default-initialized to zero), I expect them to reside in the BSS section. However, the output of kmem_init is as follows:
kmem_end: 80015000
phys_top: 88000000
&KMEM_END: 80015008
&MEM_TOP: 80015000
This means that the two variables reside between _kernel_mem_end and _physical_mem_end, which is not a "standard" section at all (most importantly, not bss.)
I get them to reside in the data section by initializing them to something other than zero, and that's fine for my purpose. I can initialize them to whatever as I will just override the initial value. However, I want to know why does this happen?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
