非.bss未初始化数据部分

发布于 2024-10-04 01:32:37 字数 1283 浏览 7 评论 0原文

我正在使用自定义链接器脚本将内核映像分为两部分。第一个是普通代码和数据,第二个是初始化代码和不再需要时将被丢弃的数据。初始化部分也不像内核本身那样在地址空间之间共享,因此如果 fork() 仍然存在(处于开发的早期阶段),则任何内容都会被复制到 fork() 上。

我已经分配了一个小的内核堆栈在启动时使用,但从我所看到的来看,我只能将它放在 .bss 部分中,在地址空间之间共享 em> 在 init 区域,不能将其存储为未初始化的数据。我想将其作为未初始化数据存储在图像的初始化部分中,以便每个进程都有自己的副本。

我可以想到两种可能的方法来做到这一点,但我无法确定它们是否可行,或者我将如何告诉链接器执行它们。第一种是将未初始化的区域放在非 .bss 部分中,但我不确定这是否可能 - 我认为你不能混合这样的部分。第二个是创建第二个类似 .bss 的部分,该部分仅存储未初始化的数据,我可以将其放入链接器脚本的初始化部分中。

有什么想法吗?为了完整起见,这是我正在使用的链接器脚本:

ENTRY(_start)

_kernel_offset = _start_kernel - _start_kernel_phys;

SECTIONS {
    _start_init = 0x100000;

    .init _start_init : AT(ADDR(.init)) { *(.mboot .init*) }
    .ctors : {
        __CTOR_NUM__ = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4)
        __CTOR_LIST__ = .; *(.ctors*)
        __CTOR_END__ = .;
    }

    _end_init = .;

    . = ALIGN(4M);
    _start_kernel_phys = .;
    _start_kernel = 0xF0000000;

    .text _start_kernel : AT(ADDR(.text) - _kernel_offset) { *(.text*) }
    .data ALIGN(4K) : AT(ADDR(.data) - _kernel_offset) { *(.rodata* .data*) }
    .bss ALIGN(4K) : AT(ADDR(.bss) - _kernel_offset) { *(.bss) *(COMMON) }

    _end_kernel = .;
    _end_kernel_phys = _end_kernel - _kernel_offset;

    /DISCARD/ : { *(.eh_frame .comment) }
}

I'm using a custom linker script to split a kernel image into two parts. The first is normal code and data, and the second is initialization code and data to be discarded when it's no longer needed. The initialization part is also not shared between address spaces the way the kernel proper is, so anything there gets copied on fork() if it's still around (it is in these early phases of development).

I have allocated a small kernel stack to use while booting, but from what I can see, I can only put it either in the .bss section where it gets shared between address spaces or in the init area where it can't be stored as uninitialized data. I would like to store it in the init part of the image as uninitialized data so that each process gets its own copy.

I can think of two potential ways to do this, but I haven't been able to find out if they're possible or how I would tell the linker to do them. The first would be to put uninitialized regions in non-.bss sections, but I'm not sure that's possible- I don't think you can mix sections like that. The second would be to create a second .bss-like section that only stores uninitialized data, which I could put in the initialization pat of the linker script.

Any ideas? For completeness, here's the linker script I'm using:

ENTRY(_start)

_kernel_offset = _start_kernel - _start_kernel_phys;

SECTIONS {
    _start_init = 0x100000;

    .init _start_init : AT(ADDR(.init)) { *(.mboot .init*) }
    .ctors : {
        __CTOR_NUM__ = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4)
        __CTOR_LIST__ = .; *(.ctors*)
        __CTOR_END__ = .;
    }

    _end_init = .;

    . = ALIGN(4M);
    _start_kernel_phys = .;
    _start_kernel = 0xF0000000;

    .text _start_kernel : AT(ADDR(.text) - _kernel_offset) { *(.text*) }
    .data ALIGN(4K) : AT(ADDR(.data) - _kernel_offset) { *(.rodata* .data*) }
    .bss ALIGN(4K) : AT(ADDR(.bss) - _kernel_offset) { *(.bss) *(COMMON) }

    _end_kernel = .;
    _end_kernel_phys = _end_kernel - _kernel_offset;

    /DISCARD/ : { *(.eh_frame .comment) }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-10-11 01:32:37

(呃,再次回答我自己的问题)

创建一个没有 CONTENTS 属性的新部分是可行的;它在程序集中声明如下:

.section .init.bss, "aw", @nobits

(ugh, answering my own question again)

Making a new section without the CONTENTS attribute works; it's declared in assembly like this:

.section .init.bss, "aw", @nobits
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文