c - 修改 fs/binfmt_elf.c 以获取自定义添加的部分内容

标签 c linux linux-kernel elf

我正在尝试修改 fs/binfmt_elf.c 以获取我添加到示例 C 程序中的新 ELF 部分的内容。

示例 C 程序:

#include <stdio.h>
    
/* Adding my own ELF section*/
char my_custom_section[128][2] __attribute__ ((section (".mysection"))) = { 0 };
 
int main() {
      return 0;
}

这是 readelf -l a.out 的输出:

Elf file type is EXEC (Executable file)
Entry point 0x4003d0
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000067c 0x000000000000067c  R E    200000
  LOAD           0x0000000000000e28 0x0000000000600e28 0x0000000000600e28
                 0x00000000000002f8 0x0000000000000308  RW     200000
  DYNAMIC        0x0000000000000e50 0x0000000000600e50 0x0000000000600e50
                 0x0000000000000190 0x0000000000000190  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000000005ac 0x00000000004005ac 0x00000000004005ac
                 0x000000000000002c 0x000000000000002c  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     8
  GNU_RELRO      0x0000000000000e28 0x0000000000600e28 0x0000000000600e28
                 0x00000000000001d8 0x00000000000001d8  R      1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .ctors .dtors .jcr .dynamic .got .got.plt .data .mysection .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .ctors .dtors .jcr .dynamic .got 

因此 .mysection 被分配给 LOAD 类型的段 03。

但是,通过 source fs/binfmt.c,我无法理解如何才能获取 .mysection 的内容/值。似乎 load_elf_binary() 只遍历程序头和段,而不是节头或节。 (这大概是有道理的,因为它是一个加载器,因此将 ELF 文件视为段而不是节)。

但即使它遍历程序段,我也不明白它是如何遍历段内的各个部分的。

基本上,我需要通过修改 fs/binfmt.c 来获取我自己的部分 .mysection 的值,这样每当一个带有 的 ELF 可执行文件时.mynewsection 加载到我的内核中,它的值存储在一些变量/CPU 寄存器中。非常感谢任何有助于完成这项工作的指示。

编辑: 我的主要目标是将 ELF 文件格式扩展为我自己的自定义文件格式,我计划将其用于扩展 x86 架构的研究项目。该扩展架构中的可执行文件需要具有唯一的身份 key ,用于对其代码/数据段执行加密操作。因此,文件中的一个新部分可以存储该 key ,内核加载程序可以获取它以进行进一步的加密处理。

最佳答案

But even when it iterates through the program segments, I don't understand how is it iterating through the sections within a segment.

由于 load_elf_binary() mmap 将所有 PT_LOAD 段放入内存,并且这些段包含所有需要的部分(例如段 03 包含部分 .ctors .dtors .jcr .dynamic .got .got.plt .data .mysection .bss),无需遍历这些部分。

关于c - 修改 fs/binfmt_elf.c 以获取自定义添加的部分内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20729143/

相关文章:

c - scanf ("%d",&变量);当我在一些空格键后单击 Enter 后,整个程序正确运行时,即使按下空格键也不会停止

结构中的 C 字符串

php - sh : 1: laravel: not found while laravel command works

linux - 无法在 Ubuntu ./webupdater 上执行二进制文件

linux - LD_PRELOAD 栈和数据段内存分配

c - 监听多个端口并接受连接

c - 原始套接字 : Receiver printing garbage values

linux - Linux 中的 $PATH 是什么以及如何修改它

Linux内核 panic

c - 将模块移植到更新的 Linux 内核 : Cannot allocate memory