gnu linker script symbol value got reassigned

389 views Asked by At

I use a linker script to describe the memory map of my os kernel. In the linker script I defined many symbols to record location of section start/end. The linker script is as follow:

OUTPUT_FORMAT(binary)

SECTIONS {
    /DISCARD/ : {
        *(.comment)
        *(.note.GNU-stack)
        *(.eh_frame)
    }

    . = 0x100000;
    kernel_load_addr = .;

    .text : {
        kernel_text_start = .;
        *(.boot)
        *(.text*)
        kernel_text_end = .;
    } = 0x90

    .data : {
        kernel_data_start = .;
        *(.rodata*)
        *(.data*)
        kernel_data_end = .;
    } = 0

    .bss : {
        kernel_bss_start = .;
        *(COMMON)
        *(.bss)
        kernel_bss_end = .;
    } = 0

    kernel_end = .;
}

But if I print the address of these symbols out, the *_start symbol becomes same as *_end symbol.

I'm printing its value with following code:

extern char kernel_text_start;
extern char kernel_text_end;
extern char kernel_data_start;
extern char kernel_data_end;
extern char kernel_bss_start;
extern char kernel_bss_end;
...
raw_write(u64_to_str((uint64_t) &kernel_text_start, buf, 16), 0x0f, 0);
raw_write(u64_to_str((uint64_t) &kernel_text_end, buf, 16), 0x0f, 40);
raw_write(u64_to_str((uint64_t) &kernel_data_start, buf, 16), 0x0f, 80);
raw_write(u64_to_str((uint64_t) &kernel_data_end, buf, 16), 0x0f, 120);
raw_write(u64_to_str((uint64_t) &kernel_bss_start, buf, 16), 0x0f, 160);
raw_write(u64_to_str((uint64_t) &kernel_bss_end, buf, 16), 0x0f, 200);

and result is

enter image description here

The map file is:

    Allocating common symbols
    Common symbol       size              file

    buddy_num           0x40              build/memory/page_alloc.c.o
    buddy_map           0x40              build/memory/page_alloc.c.o

    Discarded input sections

     .comment       0x0000000000000000       0x4e build/kernel/main.c.o
     .note.GNU-stack
                    0x0000000000000000        0x0 build/kernel/main.c.o
     .eh_frame      0x0000000000000000       0x70 build/kernel/main.c.o
     .comment       0x0000000000000000       0x4e build/memory/page_alloc.c.o
     .note.GNU-stack
                    0x0000000000000000        0x0 build/memory/page_alloc.c.o
     .eh_frame      0x0000000000000000       0xa8 build/memory/page_alloc.c.o
     .comment       0x0000000000000000       0x4e build/memory/virt_alloc.c.o
     .note.GNU-stack
                    0x0000000000000000        0x0 build/memory/virt_alloc.c.o
     .comment       0x0000000000000000       0x4e build/library/bitmap.c.o
     .note.GNU-stack
                    0x0000000000000000        0x0 build/library/bitmap.c.o
     .eh_frame      0x0000000000000000       0x50 build/library/bitmap.c.o
     .comment       0x0000000000000000       0x4e build/library/string.c.o
     .note.GNU-stack
                    0x0000000000000000        0x0 build/library/string.c.o
     .eh_frame      0x0000000000000000      0x150 build/library/string.c.o

    Memory Configuration

    Name             Origin             Length             Attributes
    *default*        0x0000000000000000 0xffffffffffffffff

    Linker script and memory map


    /DISCARD/
     *(.comment)
     *(.note.GNU-stack)
     *(.eh_frame)
     *(.comment)
     *(.note.GNU-stack)
     *(.eh_frame)
                    0x0000000000100000                . = 0x100000
                    0x0000000000100000                kernel_load_addr = .
                    0x0000000000100000                text_start_addr = .

    .text           0x0000000000100000     0x262f
                    0x0000000000100000                kernel_text_start = .
     *(.boot)
     .boot          0x0000000000100000       0x29 build/boot/boot.asm.o
     *(.text*)
     *fill*         0x0000000000100029        0x7 90
     .text          0x0000000000100030        0x0 build/boot/boot.asm.o
     .text          0x0000000000100030      0x14f build/boot/init.asm.o
                    0x0000000000100030                wheel_init
     *fill*         0x000000000010017f        0x1 90
     .text          0x0000000000100180      0x73c build/kernel/main.c.o
                    0x0000000000100180                raw_write
                    0x0000000000100210                read_info
                    0x00000000001005e0                wheel_main
     *fill*         0x00000000001008bc        0x4 90
     .text          0x00000000001008c0      0x974 build/memory/page_alloc.c.o
                    0x00000000001008c0                page_alloc_init
                    0x0000000000100cb0                find_free_pages
                    0x0000000000100ef0                alloc_pages
                    0x00000000001010c0                free_pages
     .text          0x0000000000101234        0x0 build/memory/virt_alloc.c.o
     *fill*         0x0000000000101234        0xc 90
     .text          0x0000000000101240      0x2ff build/library/bitmap.c.o
                    0x0000000000101240                bitmap_set
                    0x00000000001013c0                bitmap_clear
     *fill*         0x000000000010153f        0x1 90
     .text          0x0000000000101540     0x10ef build/library/string.c.o
                    0x0000000000101540                strlen
                    0x0000000000101650                strcpy
                    0x00000000001016c0                strncpy
                    0x0000000000101770                memcpy
                    0x0000000000101960                memset
                    0x0000000000101b60                u32_to_str
                    0x0000000000101c90                u64_to_str
                    0x0000000000101dd0                i32_to_str
                    0x0000000000101e60                i64_to_str
                    0x0000000000101ef0                vsprintf
                    0x00000000001025d0                sprintf
                    0x000000000010262f                kernel_text_end = .
                    0x000000000010262f                kernel_text_start = .
     *(.boot)
     *(.text*)
                    0x000000000010262f                kernel_text_end = .

    .data           0x0000000000102630      0x174
                    0x0000000000102630                kernel_data_start = .
     *(.rodata*)
     .rodata        0x0000000000102630        0x8 build/kernel/main.c.o
     .rodata.str1.1
                    0x0000000000102638       0x82 build/kernel/main.c.o
     *fill*         0x00000000001026ba        0x6 00000000
     .rodata        0x00000000001026c0       0xa5 build/library/string.c.o
     *(.data*)
     *fill*         0x0000000000102765        0x3 00000000
     .data          0x0000000000102768       0x3a build/boot/init.asm.o
     *fill*         0x00000000001027a2        0x2 00000000
     .data          0x00000000001027a4        0x0 build/kernel/main.c.o
     .data          0x00000000001027a4        0x0 build/memory/page_alloc.c.o
     .data          0x00000000001027a4        0x0 build/memory/virt_alloc.c.o
     .data          0x00000000001027a4        0x0 build/library/bitmap.c.o
     .data          0x00000000001027a4        0x0 build/library/string.c.o
                    0x00000000001027a4                kernel_data_end = .
                    0x00000000001027a4                kernel_data_start = .
     *(.rodata*)
     *(.data*)
                    0x00000000001027a4                kernel_data_end = .
                    0x00000000001027a4                data_end_addr = .

    .bss            0x0000000000103000     0x6000
                    0x0000000000103000                kernel_bss_start = .
     *(COMMON)
     COMMON         0x0000000000103000       0x80 build/memory/page_alloc.c.o
                    0x0000000000103000                buddy_num
                    0x0000000000103040                buddy_map
     *(.bss)
     *fill*         0x0000000000103080      0xf80 00000000
     .bss           0x0000000000104000     0x5000 build/boot/init.asm.o
                    0x0000000000104000                kernel_stack
                    0x0000000000105000                pml4t
     .bss           0x0000000000109000        0x0 build/kernel/main.c.o
     .bss           0x0000000000109000        0x0 build/memory/page_alloc.c.o
     .bss           0x0000000000109000        0x0 build/memory/virt_alloc.c.o
     .bss           0x0000000000109000        0x0 build/library/bitmap.c.o
     .bss           0x0000000000109000        0x0 build/library/string.c.o
                    0x0000000000109000                kernel_bss_end = .
                    0x0000000000109000                kernel_bss_start = .
     *(COMMON)
     *(.bss)
                    0x0000000000109000                kernel_bss_end = .
                    0x0000000000109000                bss_end_addr = .
                    0x0000000000109000                kernel_end = .
    LOAD build/boot/boot.asm.o
    LOAD build/boot/init.asm.o
    LOAD build/kernel/main.c.o
    LOAD build/memory/page_alloc.c.o
    LOAD build/memory/virt_alloc.c.o
    LOAD build/library/bitmap.c.o
    LOAD build/library/string.c.o
    LOAD link.lds
                    0x0000000000100000                . = 0x100000
                    0x0000000000100000                kernel_load_addr = .
                    0x0000000000100000                text_start_addr = .
                    0x0000000000100000                data_end_addr = .
                    0x0000000000100000                bss_end_addr = .
                    0x0000000000100000                kernel_end = .
    OUTPUT(build/kernel.bin binary)

You can see the value of these symbols got reassigned.

So how can I avoid this and get correct address in my program?

0

There are 0 answers