【问题标题】:Adding values to array of structs causes Seg Fault向结构数组添加值会导致 Seg Fault
【发布时间】: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


【解决方案1】:

你 malloc Section_t 表就可以了,

sections = malloc(elf_shnum * sizeof(Section_t));

但是这个结构包含下一个指针Header

typedef struct
{
    Elf_Big_Shdr_t *header;
    unsigned char *data;
} Section_t;

在你使用它之前,你应该为它分配内存。

例如:

sections[i].header = malloc(sizeof(Elf_Big_Shdr_t));
sections[i].header->sh_name = sh_name_index;

或者,您可以将结构定义更改为

typedef struct
{
    Elf_Big_Shdr_t header;
    unsigned char *data;
} Section_t;

【讨论】:

    【解决方案2】:

    线

    sections = malloc(elf_shnum * sizeof(Section_t));
    

    分配一堆数据,并将其存储到sections。分配的内存中的实际数据是不确定的。然后,在你的下一行

    sections[i].header->sh_name = sh_name_index;
    

    您尝试将部分内存 (sections[0].header) 视为指针并取消引用它。但是,由于该值是不确定的,因此这是未定义的行为。最可能且问题最少的结果是段错误。

    相反,您需要在使用之前为每个Section_t 分配有用的值。您可以通过 malloc 为 Elf 标头提供足够的空间并将结果分配给 sections[i].header 来做到这一点,但除非 Elf 部分可以有多个标头或零标头,否则最好让 header 成员具有输入 Elf_Big_Shdr_t 而不是 Elf_Big_Shdr_t *

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2020-02-22
      • 2020-05-12
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多