gpt4 book ai didi

c - 是什么导致我出现段错误(核心已转储)

转载 作者:太空狗 更新时间:2023-10-29 12:08:59 25 4
gpt4 key购买 nike

我正在阅读一本关于编写操作系统代码的书,我正在编写他们在书中提供的 C 代码示例,并在终端中编译和测试代码,但我遇到了此代码的问题。

包含此代码的文件名为“hello.c”。我用“gcc hello.c -o hello”编译文件然后用“./hello”运行它。

我收到消息 Segmentation fault (core dumped),而且我不确定自己做错了什么。

#include <stdio.h>

void preinit1() {
printf("%s\n", __FUNCTION__);
}

void preinit2() {
printf("%s\n", __FUNCTION__);
}

void init1() {
printf("%s\n", __FUNCTION__);
}

void init2() {
printf("%s\n", __FUNCTION__);
}

typedef void (*preinit)();
typedef void (*init)();

__attribute__((section(".init_array"))) preinit
preinit_arr[2] = {preinit1, preinit2};

__attribute__((section(".init_array"))) init
init_arr[2] = {init1, init2};

int main(int argc, char *argv[])
{
printf("hello world!\n");

return 0;
}

最佳答案

我认为您不应该向该部分添加数组(您在示例中出错,初始化 .init_array 两次)。

__attribute__((section(".preinit_array"))) preinit preinit_arr1 = preinit1;
__attribute__((section(".preinit_array"))) preinit preinit_arr2 = preinit2;

__attribute__((section(".init_array"))) init init_arr1 = init1;
__attribute__((section(".init_array"))) init init_arr2 = init2;

这是使用数组声明的部分

objdump -s -j .init_array hello.orig 

hello.orig: file format elf64-x86-64

Contents of section .init_array:
3dc0 30110000 00000000 00000000 00000000 0...............
3dd0 35110000 00000000 48110000 00000000 5.......H.......
3de0 5b110000 00000000 6e110000 00000000 [.......n.......

这是有效的部分

objdump -s -j .init_array hello

hello: file format elf64-x86-64

Contents of section .init_array:
3dc8 30110000 00000000 35110000 00000000 0.......5.......
3dd8 48110000 00000000 5b110000 00000000 H.......[.......
3de8 6e110000 00000000 n.......

前者在 3dc8 处有空指针,这可能导致分段(见下文),我不知道为什么有人可以解释。

(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x00005555555551f5 in __libc_csu_init ()
#2 0x00007ffff7dec02a in __libc_start_main (main=0x555555555181 <main>, argc=1, argv=0x7fffffffe1f8, init=0x5555555551b0 <__libc_csu_init>,
fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe1e8) at ../csu/libc-start.c:264
#3 0x000055555555507a in _start ()

仅修复有关 .preinit_array 的拼写错误,使 preinit 调用有效,但 init 无效

关于c - 是什么导致我出现段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55328961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com