assembly - 如何将 libc6 中的函数导入 ELF 可执行文件?

标签 assembly x86 file-format elf

我正在创建一个 i386 ELF 可执行文件,需要从 libc6 导入函数。 (顺便说一句,这是 printf。)

我创建了一个非常小的 ELF 可执行文件,可以打印“Hello, world!”使用 Linux 内核中断 0x80 到控制台。这不是最佳选择,我希望应用程序使用 libc。

这是我到目前为止所得到的:(大部分结构对齐代码归功于this页面。)

BITS 32

            org     0x08048000

ehdr:                                                 ; Elf32_Ehdr
            db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
    times 8 db      0
            dw      2                               ;   e_type
            dw      3                               ;   e_machine
            dd      1                               ;   e_version
            dd      _start                          ;   e_entry
            dd      52
            dd      0                               ;   e_shoff
            dd      0                               ;   e_flags
            dw      52
            dw      32
            dw      1                               ;   e_phnum
            dw      0                               ;   e_shentsize
            dw      0                               ;   e_shnum
            dw      0                               ;   e_shstrndx

;   this is the header for the code section

            dd      1                               ;   p_type
            dd      0                               ;   p_offset
            dd      $$                              ;   p_vaddr
            dd      $$                              ;   p_paddr
            dd      filesize                        ;   p_filesz
            dd      filesize                        ;   p_memsz
            dd      5                               ;   p_flags
            dd      0x1000                          ;   p_align

_start:

            ; We want to print the string

    mov eax,4            ; 'write' system call
    mov ebx,1            ; file descriptor 1 = screen
    mov ecx,teststr      ; string to write
    mov edx,14           ; length of string to write
    int 80h              ; call the kernel

    ; Terminate program
    mov eax,1            ; 'exit' system call
    mov ebx,0            ; exit with error code 0
    int 80h              ; call the kernel

_stringtable:

            teststr db "Hello, world!",10,0

filesize      equ     $ - $$

我认为我需要添加另一个程序头用于导入,但我真的不知道 - 我也不熟悉该部分内容的格式。

最佳答案

如果您愿意让链接器完成其工作,这就足够了(GAS 语法;使用 gcc hello.s 编译):

        .text
        .globl main
        .type main, @function
main:
        leal    .LC0, %eax
        pushl   %eax
        call    puts
        xorl    %eax, %eax
        ret
        .size main, .-main
.LC0:
        .asciz  "Hello, world!"

(从技术上讲,.LC0 应该放入 .rodata 中,但我不记得具体是如何做到的,而且我现在不在我的 Linux 机器上。另外,这不会保持任何堆栈指针对齐,这对于像这样的玩具程序来说不应该成为问题,但我不做任何 promise 。)

如果你真的想手动构建整个可执行文件,请首先阅读这本书:http://www.iecc.com/linker/然后,您需要阅读 ELF 规范、System V 通用 ABI 和 x86-32 psABI 补充;这些都可以在这里找到:http://refspecs.freestandards.org/ 。然后,使用 readelf、objdump 和/或 hexdump 拆解编译上述代码得到的可执行镜像,并了解其构造方式。

关于assembly - 如何将 libc6 中的函数导入 ELF 可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3835245/

相关文章:

c - gdb "info frame"中没有 epb/eip 寄存器

c - 如何将反汇编的C代码划分为函数?

c - 为什么使用 "register"关键字会使我的代码更快?

c# - C# 中的协议(protocol)抽象

程序集:文件描述符0或2用于读取用户输入

performance - 紧挨着放置使用的代码和只读数据是一个好主意吗?

tags - 有哪些标签导出格式?

3d - 无法理解 .inr 文件格式

assembly - $ 在 MIPS 程序集中是什么意思?

c - x86_64 上无用的 jp/jnp 汇编指令