首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >elf.h:使用x86_64-w64-mingw32-gcc交叉编译器时没有这样的文件或目录

elf.h:使用x86_64-w64-mingw32-gcc交叉编译器时没有这样的文件或目录
EN

Stack Overflow用户
提问于 2021-08-15 03:22:28
回答 1查看 136关注 0票数 0

许多开发人员告诉我使用交叉编译器来编译我的操作系统。问题是当我从使用gcc切换到使用x86_64-w64-mingw32-gcc时,它在这篇文章的标题中显示了错误。

我在使用WSL的Windows上,我检查了/usr/include,看看elf.h是否在那里,它确实在那里。因此,我尝试在使用-I/usr/include编译时包含该目录,但仍然不起作用。

PS:我正试图在kernel.cpp中调用我的__main函数,我需要elf.h来获取头条目(除非有一种方法可以在没有elf.h的情况下完成,我不知道)。

完整的引导加载程序Makefile:

代码语言:javascript
复制
all:
    x86_64-w64-mingw32-gcc -Ignu-efi/inc -Ignu-efi/inc/x86_64 -Ignu-efi/inc/protocol -c -o build/main.o c/main.c
    x86_64-w64-mingw32-gcc -Ignu-efi/inc -Ignu-efi/inc/x86_64 -Ignu-efi/inc/protocol -c -o build/data.o gnu-efi/lib/data.c
    x86_64-w64-mingw32-gcc -nostdlib -Wl,-dll -shared -Wl,--subsystem,10 -e efi_main -o essentials/main.efi build/main.o build/data.o

我正在使用UEFI Bare Bones (仅限GNU-EFI标头)。

有没有办法停止这个错误,或者有没有使用elf.h的替代方法?

感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-16 00:11:44

我通过手动将结构添加到我的main.c文件中修复了此问题:

代码语言:javascript
复制
typedef uint64_t Elf64_Addr;

#define ELFMAG      "\177ELF"
#define SELFMAG     4
#define EI_CLASS    4       /* File class byte index */
#define ELFCLASS64  2       /* 64-bit objects */
#define EI_DATA     5       /* Data encoding byte index */
#define ELFDATA2LSB 1       /* 2's complement, little endian */
#define ET_EXEC     2       /* Executable file */
#define PT_LOAD     1       /* Loadable program segment */
#ifdef __x86_64__
#define EM_MACH     62      /* AMD x86-64 architecture */
#endif
#ifdef __aarch64__
#define EM_MACH     183     /* ARM aarch64 architecture */
#endif


typedef struct
{
    uint8_t  e_ident[16];   /* Magic number and other info */
    uint16_t e_type;        /* Object file type */
    uint16_t e_machine;     /* Architecture */
    uint32_t e_version;     /* Object file version */
    uint64_t e_entry;       /* Entry point virtual address */
    uint64_t e_phoff;       /* Program header table file offset */
    uint64_t e_shoff;       /* Section header table file offset */
    uint32_t e_flags;       /* Processor-specific flags */
    uint16_t e_ehsize;      /* ELF header size in bytes */
    uint16_t e_phentsize;   /* Program header table entry size */
    uint16_t e_phnum;       /* Program header table entry count */
    uint16_t e_shentsize;   /* Section header table entry size */
    uint16_t e_shnum;       /* Section header table entry count */
    uint16_t e_shstrndx;    /* Section header string table index */
} Elf64_Ehdr;

typedef struct
{
    uint32_t p_type;        /* Segment type */
    uint32_t p_flags;       /* Segment flags */
    uint64_t p_offset;      /* Segment file offset */
    uint64_t p_vaddr;       /* Segment virtual address */
    uint64_t p_paddr;       /* Segment physical address */
    uint64_t p_filesz;      /* Segment size in file */
    uint64_t p_memsz;       /* Segment size in memory */
    uint64_t p_align;       /* Segment alignment */
} Elf64_Phdr;

我发现这些结构的地方:https://gitlab.com/bztsrc/posix-uefi/-/blob/master/examples/0F

如果/usr/include目录中有elf.h文件,也可以从该文件中获取这些结构。

您可以将文件复制到您的首选文件夹,如下所示:

代码语言:javascript
复制
cd some_dir/preferred_dir
cp /usr/include/elf.h .

然后将其中的结构复制并粘贴到项目中。

如果您使用的是Windows,则可以使用WSL来执行此操作。

这就是我所学到的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68788469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档