許雅雯
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 載入,連結與重配置 延續[學習實作小型作業系統筆記](https://hackmd.io/s/S1Ab0A9pX#ELF-%E8%A7%A3%E6%9E%90),目標是學習 [Implementing Loadable Kernel Modules for Linux](http://collaboration.cmc.ec.gc.ca/science/rpn/biblio/ddj/Website/articles/DDJ/1995/9505/9505a/9505a.htm) 這份 Journal, 並實做出能動態載入的 component --- - [ ] 讓載入的 relocation ELF 拿到系統內的 symbol 地址。 * 先利用簡單的程式碼說明, `printf` 這個功能在 kernel 裡面,我們先編譯但不連結,使用指令: `aarch64-linux-gnu-gcc -c -I./include ksym.c -o ksym.o` * `ksym.c` ~~~c= #include<printf.h> void main(void){ printf("Read kernel module!"); } ~~~ * `printf.h`: 在前處理時載入 ~~~c= #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 解析](https://hackmd.io/s/H1G5-o83N)講的, `.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 ~~~ * 此階段先將[機器碼部份](https://hackmd.io/s/SkwnZNp2N#%E7%B5%84%E8%AA%9E%E5%92%8C%E6%A9%9F%E5%99%A8%E7%A2%BC-adrp-add-bl)用懂,因此先直接填入數字,等到確認功能沒問題,在重寫流程, adrp 的 `90000000` 如果不改,就是由當前 page `00091000` 出發,也就是 add 要填要找的 `.rodata` offset 為 3e8, `910fa000` <3e8 * 4 = fa0>, bl 為相對位置填入 `97fdd405`([bl 解說](https://hackmd.io/s/SkwnZNp2N#bl)) * 理解要如何 relocate 時,利用指標先填入機器碼 ~~~ c= 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 去 `printf` 出 `Read kernel module!` ![](https://i.imgur.com/iQVcly8.png) * 然後回[原文](https://hackmd.io/s/S1Ab0A9pX#ELF-%E8%A7%A3%E6%9E%90)將此流程寫成程式。 ### 組語和機器碼 adrp, add, bl #### adrp * 一開始對於 `adrp` 反組譯後的 objcode (機器碼) 不太理解,因為他的 offset 總跟我算起來的不太一樣,因此去查了[Armv8 規格書 p225](https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf?_ga=2.109811589.1077926107.1543714027-1596023854.1506794914), 發現 immediate 被分成 `immhi:immlow` 並分散於 `23-5` bits, `30-29` bits,是相對於當前的 page 數,並乘上 4096: ![](https://i.imgur.com/st1Fh4c.png) ~~~ 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](https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf?_ga=2.109811589.1077926107.1543714027-1596023854.1506794914),寫組語來看機器碼(object code) ![](https://i.imgur.com/oRbIh6l.png) * 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](https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf?_ga=2.109811589.1077926107.1543714027-1596023854.1506794914),如果 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` ![](https://i.imgur.com/z7ULu0j.png) --- ### 完整 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 ~~~

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully