簡體   English   中英

編譯動態鏈接內核

[英]Compile Dynamically Linked Kernel

我正在嘗試在xv6內核中實現KASLR,因此我需要以動態方式(重定位表)重新編譯/鏈接內核。 由於內核當前是硬編碼的,因此即使我更改了內核代碼和數據映射,指令仍將引用硬編碼的虛擬地址。 我需要一種說明方式來引用沒有硬編碼地址的elf其他部分。 下面是一個示例:這是當前編譯后的外觀示例:

8010018e:   83 ec 0c                sub    $0xc,%esp
80100191:   68 a7 79 10 80          push   $0x801079a7
80100196:   e8 b5 01 00 00          call   80100350 <panic>
8010019b:   90                      nop
8010019c:   8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi.

如您所見,諸如push指令之類的指令引用了硬編碼的v地址($ 0x801079a7)。 這是因為此elf以靜態方式編譯而沒有重定位:(readelf打印輸出)

 Program Headers:
    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
    LOAD           0x001000 0x80100000 0x00100000 0x0b516 0x15668 RWE 0x1000
    GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x10
    Section to Segment mapping:
    Segment Sections...
    00     .text .rodata .stab .stabstr .data .bss
    01
    There is no dynamic section in this file.
    There are no relocations in this file.

我需要有關如何通過重定位將靜態鏈接的elf二進制文件重新編譯為一個二進制文件,然后能夠將內核映射到虛擬內存中任何位置的建議。 另外,為了解析elf以重定位正確加載它,我需要在引導加載程序中進行哪些修改? 我是否需要自定義解析內核小精靈中包含的GOT?

以下是一個鏈接器腳本,用於編譯內核並從本質上設置其基本硬編碼虛擬地址:0x80100000

OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(_start)

SECTIONS
{
    /* Link the kernel at this address: "." means the current address */
        /* Must be equal to KERNLINK */
    . = 0x80100000;

    .text : AT(0x100000) {
        *(.text .stub .text.* .gnu.linkonce.t.*)
    }

    PROVIDE(etext = .); /* Define the 'etext' symbol to this value */

    .rodata : {
        *(.rodata .rodata.* .gnu.linkonce.r.*)
    }

    /* Include debugging information in kernel memory */
    .stab : {
        PROVIDE(__STAB_BEGIN__ = .);
        *(.stab);
        PROVIDE(__STAB_END__ = .);
        BYTE(0)     /* Force the linker to allocate space
                   for this section */
    }

    .stabstr : {
        PROVIDE(__STABSTR_BEGIN__ = .);
        *(.stabstr);
        PROVIDE(__STABSTR_END__ = .);
        BYTE(0)     /* Force the linker to allocate space
                   for this section */
    }

    /* Adjust the address for the data segment to the next page */
    . = ALIGN(0x1000);

    /* Conventionally, Unix linkers provide pseudo-symbols
     * etext, edata, and end, at the end of the text, data, and bss.
     * For the kernel mapping, we need the address at the beginning
     * of the data section, but that's not one of the conventional
     * symbols, because the convention started before there was a
     * read-only rodata section between text and data. */
    PROVIDE(data = .);

    /* The data segment */
    .data : {
        *(.data)
    }

    PROVIDE(edata = .);

    .bss : {
        *(.bss)
    }

    PROVIDE(end = .);

    /DISCARD/ : {
        *(.eh_frame .note.GNU-stack)
    }
}

看來我必須修改xv6引導程序並重寫許多操作系統才能完成動態內核。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM