Try   HackMD

載入,連結與重配置

延續學習實作小型作業系統筆記,目標是學習 Implementing Loadable Kernel Modules for Linux 這份 Journal, 並實做出能動態載入的 component


  • 讓載入的 relocation ELF 拿到系統內的 symbol 地址。
  • 先利用簡單的程式碼說明, printf 這個功能在 kernel 裡面,我們先編譯但不連結,使用指令: aarch64-linux-gnu-gcc -c -I./include ksym.c -o ksym.o
    • ksym.c
    ​​​​#include<printf.h> ​​​​void main(void){printf("Read kernel module!"); ​​​​}
    • printf.h: 在前處理時載入
    ​​​​#ifndef __TFP_PRINTF__ ​​​​#define __TFP_PRINTF__ ​​​​#include <stdarg.h> ​​​​void init_printf(void* putp,void (*putf) (void*,char)); ​​​​void tfp_printf(char *fmt, ...); ​​​​void tfp_sprintf(char* s,char *fmt, ...); ​​​​void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va); ​​​​#define printf tfp_printf ​​​​#define sprintf tfp_sprintf ​​​​#endif
    • 編譯完後,如 ELF 解析講的, .rodata 的字串和 tfp_printf 還未填入值。
      ​​​​​​​​Disassembly of section .text:
      
      ​​​​​​​​0000000000000000 <main>:
      ​​​​​​​​   0:	a9bf7bfd 	stp	x29, x30, [sp,#-16]!
      ​​​​​​​​   4:	910003fd 	mov	x29, sp
      ​​​​​​​​   8:	90000000 	adrp	x0, 0 <main>
      ​​​​​​​​   c:	91000000 	add	x0, x0, #0x0
      ​​​​​​​​  10:	94000000 	bl	0 <tfp_printf>
      ​​​​​​​​  14:	d503201f 	nop
      ​​​​​​​​  18:	a8c17bfd 	ldp	x29, x30, [sp],#16
      ​​​​​​​​  1c:	d65f03c0 	ret
      
      

  • 步驟
    1. 利用 elf.h 的格式,取出 header 的資訊
    2. 得知 string table 的 index 去查 section
    3. 紀錄 section 的 size 和起始位置
      • .text .rodata .data .bss 段重新配置
      ​​​​​​​​    +----------+
      ​​​​​​​​    |  .text   |
      ​​​​​​​​    +----------+
      ​​​​​​​​    |  .rodata |
      ​​​​​​​​    +----------+
      ​​​​​​​​    |  .data   |
      ​​​​​​​​    +----------+
      ​​​​​​​​    |  .bss    |
      ​​​​​​​​    +----------+
      
      • 目前把搬上來 ksym.o 檔的以上 4個 section ,放置在 913c8 ,因為這份裡只有 .text.rodata, com 是我寫的指令,主要是做搬到 kernel mode 的記憶體空間並執行。
      ​​​​​​​​tkernel@user_name:root$com KSYM    O  
      ​​​​​​​​Component: read file OK!
      ​​​​​​​​000913C8: FD 7B BF A9  FD 03 00 91  00 00 00 90  00 00 00 91  .{..............
      ​​​​​​​​000913D8: 00 00 00 94  1F 20 03 D5  FD 7B C1 A8  C0 03 5F D6  ..... ...{...._.
      ​​​​​​​​000913E8: 52 65 61 64  20 6B 65 72  6E 65 6C 20  6D 6F 64 75  Read kernel modu
      ​​​​​​​​000913F8: 6C 65 21 00  00 00 00 00  00 00 00 00  00 00 00 00  le!.............
      
    4. 重填 .rela.text 中的 data
      • 表示一開始核心內有 table 會提供 kernel 裡的 symbol
      • .rela.text 裡有三個 entry 要重填,為了方邊觀看,利用 readelf -r file
        ​​​​​​​​​​​​Relocation section '.rela.text' at offset 0x1f0 contains 3 entries:
        ​​​​​​​​​​​​  Offset          Info           Type           Sym. Value    Sym. Name + Addend
        ​​​​​​​​​​​​000000000008  000500000113 R_AARCH64_ADR_PRE 0000000000000000 .rodata + 0
        ​​​​​​​​​​​​00000000000c  000500000115 R_AARCH64_ADD_ABS 0000000000000000 .rodata + 0
        ​​​​​​​​​​​​000000000010  000b0000011b R_AARCH64_CALL26  0000000000000000 tfp_printf + 0
        
      • 此階段先將機器碼部份用懂,因此先直接填入數字,等到確認功能沒問題,在重寫流程, adrp 的 90000000 如果不改,就是由當前 page 00091000 出發,也就是 add 要填要找的 .rodata offset 為 3e8, 910fa000 <3e8 * 4 = fa0>, bl 為相對位置填入 97fdd405bl 解說
      • 理解要如何 relocate 時,利用指標先填入機器碼
        ​​​unsigned char* ch_test = &map_array[com_start]+0xd; ​​​*ch_test = 0xa0; ​​​ch_test = &map_array[com_start]+ 0xe; ​​​*ch_test = 0x0f; ​​​ch_test = &map_array[com_start]+ 0x10; ​​​*ch_test = 0x05; ​​​ch_test = &map_array[com_start]+ 0x11; ​​​*ch_test = 0xd4; ​​​ch_test = &map_array[com_start]+ 0x12; ​​​*ch_test = 0xfd; ​​​ch_test = &map_array[com_start]+ 0x13; ​​​*ch_test = 0x97;
      • 以下是成功讓新載入的程式,成功拿到 kernel 已存在的 function,所以用 uart 去 printfRead kernel module!
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
      • 然後回原文將此流程寫成程式。

組語和機器碼 adrp, add, bl

adrp

  • 一開始對於 adrp 反組譯後的 objcode (機器碼) 不太理解,因為他的 offset 總跟我算起來的不太一樣,因此去查了Armv8 規格書 p225, 發現 immediate 被分成 immhi:immlow 並分散於 23-5 bits, 30-29 bits,是相對於當前的 page 數,並乘上 4096:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
Assembler symbols:
    -<Xd>Is the 64-bit name of the general-purpose destination register, 
            encoded in the "Rd" field.

    -<label>Is the program label whose 4KB page address is to be calculated. 
            Its offset from the page address of this instruction, in the range +/-4GB,
            is encoded as "immhi:immlo" times 4096.
  • adrp 舉例,例子來自於我 kernel 內的反組譯的程式碼,有開 MMU 因此為地址base(0xffff000000000000) + offset,而在 ffff000000007224 行,相對的 Page 為 0x93(目標page) - 0x7(pc所在page) = 0x8c, 46(immhi):0(immlo)也等於 8c,0-4 bits 為通用暫存器,這裡使用x0,及填入為 90000460
ffff000000007220:	540000e0 	b.eq	ffff00000000723c <sd_readblock+0x54>
ffff000000007224:	90000460 	adrp	x0, ffff000000093000 <sd_ocr>
ffff000000007228:	91002000 	add	x0, x0, #0x8
ffff00000000722c:	92800001 	mov	x1, #0xffffffffffffffff    	// #-1

add

  • add 的部份較為簡單,並沒有牽涉 page,因此直接看規格書 armv8 page.531,寫組語來看機器碼(object code)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • immidiate 從 10 bit 開始填寫, 以下填入的是 0x20, 0x20*4 = 0x80, 因此機器碼 91008000,
tina@tina-X550VB:~/Desktop$ aarch64-linux-gnu-objdump -D test_arm.o
test_arm.o:     file format elf64-littleaarch64


Disassembly of section .text:

0000000000000000 <test>:
   0:	91008000 	add	x0, x0, #0x20

bl

  • bl 的規格書 armv8 page.572,如果 pc 目前在913d8,而要 jump 的數在 0x63ec <tfp_printf>,因為是往回跳,會用到 2補數,且此指令也是 pc-relative 所以用減的,913d8 - 63ec = 8afec, 指令 4 byte:8afec/4 = 22bfb, 二補數: 3ffffff (0-25 bit 都為 1) - 22bfb = 3fdd404, 3fdd404 + 1 =3fdd405,再把 imidiate 放進以下格式,為 97fdda05
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

完整 objcode

---header---
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000010 0001 00b7 0001 0000 0000 0000 0000 0000
0000020 0000 0000 0000 0000 0290 0000 0000 0000
0000030 0000 0000 0040 0000 0000 0040 000b 0008
---
0000040 7bfd a9bf 03fd 9100 0000 9000 0000 9100
0000050 0000 9400 201f d503 7bfd a8c1 03c0 d65f
-.rodata
0000060 6552 6461 6b20 7265 656e 206c 6f6d 7564
0000070 656c 0021 4700 4343 203a 5528 7562 746e
0000080 2f75 694c 616e 6f72 3520 342e 302e 362d
0000090 6275 6e75 7574 7e31 3631 302e 2e34 2939
00000a0 3520 342e 302e 3220 3130 3036 3036 0039
-.symbt
00000b0 0000 0000 0000 0000 0000 0000 0000 0000
00000c0 0000 0000 0000 0000 0001 0000 0004 fff1
00000d0 0000 0000 0000 0000 0000 0000 0000 0000
00000e0 0000 0000 0003 0001 0000 0000 0000 0000
00000f0 0000 0000 0000 0000 0000 0000 0003 0003
0000100 0000 0000 0000 0000 0000 0000 0000 0000
0000110 0000 0000 0003 0004 0000 0000 0000 0000
0000120 0000 0000 0000 0000 0000 0000 0003 0005
0000130 0000 0000 0000 0000 0000 0000 0000 0000
0000140 0008 0000 0000 0005 0000 0000 0000 0000
0000150 0000 0000 0000 0000 000b 0000 0000 0001
0000160 0000 0000 0000 0000 0000 0000 0000 0000
0000170 0000 0000 0003 0007 0000 0000 0000 0000
0000180 0000 0000 0000 0000 0000 0000 0003 0006
0000190 0000 0000 0000 0000 0000 0000 0000 0000
00001a0 000e 0000 0012 0001 0000 0000 0000 0000
00001b0 0020 0000 0000 0000 0013 0000 0010 0000
00001c0 0000 0000 0000 0000 0000 0000 0000 0000
- strtab
00001d0 6b00 7973 2e6d 0063 6424 2400 0078 616d
00001e0 6e69 7400 7066 705f 6972 746e 0066 0000
---.rela.text
00001f0 0008 0000 0000 0000 0113 0000 0005 0000
0000200 0000 0000 0000 0000 000c 0000 0000 0000
0000210 0115 0000 0005 0000 0000 0000 0000 0000
0000220 0010 0000 0000 0000 011b 0000 000b 0000
0000230 0000 0000 0000 0000 
--
                            2e00 7973 746d 6261   ..........symtab
0000240 2e00 7473 7472 6261 2e00 6873 7473 7472   ..strtab..shstrt
0000250 6261 2e00 6572 616c 742e 7865 0074 642e   ab..rela.text..d
0000260 7461 0061 622e 7373 2e00 6f72 6164 6174   ata..bss..rodata
0000270 2e00 6f63 6d6d 6e65 0074 6e2e 746f 2e65   ..comment..note.
0000280 4e47 2d55 7473 6361 006b 0000 0000 0000   GNU-stack.......
---section header table
-0
0000290 0000 0000 0000 0000 0000 0000 0000 0000
*
-1 .text
00002d0 0020 0000 0001 0000 0006 0000 0000 0000
00002e0 0000 0000 0000 0000 0040 0000 0000 0000
00002f0 0020 0000 0000 0000 0000 0000 0000 0000
0000300 0004 0000 0000 0000 0000 0000 0000 0000
-2 .rela.text
0000310 001b 0000 0004 0000 0040 0000 0000 0000
0000320 0000 0000 0000 0000 01f0 0000 0000 0000
0000330 0048 0000 0000 0000 0009 0000 0001 0000
0000340 0008 0000 0000 0000 0018 0000 0000 0000
-3 .data
0000350 0026 0000 0001 0000 0003 0000 0000 0000
0000360 0000 0000 0000 0000 0060 0000 0000 0000
0000370 0000 0000 0000 0000 0000 0000 0000 0000
0000380 0001 0000 0000 0000 0000 0000 0000 0000
-4 .bss
0000390 002c 0000 0008 0000 0003 0000 0000 0000
00003a0 0000 0000 0000 0000 0060 0000 0000 0000
00003b0 0000 0000 0000 0000 0000 0000 0000 0000
00003c0 0001 0000 0000 0000 0000 0000 0000 0000
-5 .rodata
00003d0 0031 0000 0001 0000 0002 0000 0000 0000
00003e0 0000 0000 0000 0000 0060 0000 0000 0000
00003f0 0014 0000 0000 0000 0000 0000 0000 0000
0000400 0008 0000 0000 0000 0000 0000 0000 0000
-6 .comment
0000410 0039 0000 0001 0000 0030 0000 0000 0000
0000420 0000 0000 0000 0000 0074 0000 0000 0000
0000430 003c 0000 0000 0000 0000 0000 0000 0000
0000440 0001 0000 0000 0000 0001 0000 0000 0000
-7 .note.GNU-stack
0000450 0042 0000 0001 0000 0000 0000 0000 0000
0000460 0000 0000 0000 0000 00b0 0000 0000 0000
0000470 0000 0000 0000 0000 0000 0000 0000 0000
0000480 0001 0000 0000 0000 0000 0000 0000 0000
-8 .shstrtab
0000490 0011 0000 0003 0000 0000 0000 0000 0000
00004a0 0000 0000 0000 0000 0238 0000 0000 0000
00004b0 0052 0000 0000 0000 0000 0000 0000 0000
00004c0 0001 0000 0000 0000 0000 0000 0000 0000
-9 .symtab 
00004d0 0001 0000 0002 0000 0000 0000 0000 0000
00004e0 0000 0000 0000 0000 00b0 0000 0000 0000
00004f0 0120 0000 0000 0000 000a 0000 000a 0000
0000500 0008 0000 0000 0000 0018 0000 0000 0000
-10 .strtab
0000510 0009 0000 0003 0000 0000 0000 0000 0000
0000520 0000 0000 0000 0000 01d0 0000 0000 0000
0000530 001e 0000 0000 0000 0000 0000 0000 0000
0000540 0001 0000 0000 0000 0000 0000 0000 0000