【发布时间】:2018-05-13 18:38:09
【问题描述】:
所以我有一个程序,我在其中创建了一个结构数组,然后我遍历每个结构并将值插入到每个结构中。唯一的问题是,当我尝试插入这些值时,我遇到了分段错误。原谅我,我是一个新手 C 程序员,但我环顾四周,找不到我的问题的答案。
这是代码(为简单起见进行了重构):
#include "readelf.h"
int main(int ac, char **av)
{
int elf_shnum, sh_name_index, i;
Section_t *sections;
i = 0;
elf_shnum = 12;
sh_name_index = 24;
sections = malloc(elf_shnum * sizeof(Section_t));
sections[i].header->sh_name = sh_name_index;
return (0);
}
包含文件:
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdint.h>
#include <elf.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
typedef struct
{
uint64_t sh_name; /* Section name (string tbl index) */
uint64_t sh_type; /* Section type */
uint64_t sh_flags; /* Section flags */
uint64_t sh_addr; /* Section virtual addr at execution */
uint64_t sh_offset; /* Section file offset */
uint64_t sh_size; /* Section size in bytes */
uint64_t sh_link; /* Link to another section */
uint64_t sh_info; /* Additional section information */
uint64_t sh_addralign; /* Section alignment */
uint64_t sh_entsize; /* Entry size if section holds table */
} Elf_Big_Shdr_t;
typedef union
{
Elf32_Shdr Elf32;
Elf64_Shdr Elf64;
} Elf_Shdr_t;
typedef struct
{
Elf_Big_Shdr_t *header;
unsigned char *data;
} Section_t;
【问题讨论】:
-
我认为制作minimal reproducible example 会很有帮助。
-
谢谢@Yunnosch,我刚刚添加了数据结构。我不太愿意提供整个代码,因为它太多了。
-
我会尝试以更小的外形重新创建它
-
是的,“更小的外形尺寸”可能正是这个想法。祝你好运......实际上我坚信制作一个好的 MCVE 是非常幸运的,并且几乎可以保证自己找到问题或在这里找到有用的答案。
-
@Yunnosch 几乎每次我想在这里问一个问题时,我都会开始制作一个 MCVE,然后我找到答案并且实际上不需要问任何东西。
标签: c pointers data-structures segmentation-fault elf