Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hello world在那邊?背景說明 0

Wen_Liao
January 24, 2015

Hello world在那邊?背景說明 0

部落格文尋找Hello world在背景說明

26/Jan/2015 增加prolog/epilogue說明
29/Jan/2015 增加%eax設成0的說明,感謝Scott Tasi大大的補充

Wen_Liao

January 24, 2015
Tweet

More Decks by Wen_Liao

Other Decks in Technology

Transcript

  1. 先從API談起 引用Wikipedia: • Application programming interface的縮寫 • 一組讓開發者開發應用程式的 routine、通訊 協定、或是工具

    • 使用相同的API,在不同平台上面重新編譯,應 該可以編譯成功,並且正確執行。
  2. 仔細看看男人 $ man man The table below shows the section

    numbers of the manual followed by the types of pages they contain. 1 Executable programs or shell commands 2 System calls (functions provided by the kernel) ...
  3. strace, 追蹤system call和signal的工具 使用了write system call 有人還記得 stdout fd是 1

    嗎? 帶入要寫到 stdout的字 串 寫入12個 bytes 回傳已經寫 了12 bytes 印出來了嘿 嘿
  4. 回到ABI • Application Binary Interface的縮寫 • 規範binary,也就是你的執行程式和函式庫 ◦ 呼叫函數行為 ▪

    機械碼執行的時候哪些暫存器放參數,哪些暫存器 放回傳值... ◦ Data type alignment ◦ System call呼叫規範 ◦ ... • 理想的世界 ◦ 同樣的硬體條件和同樣的ABI,不同compiler編出來的 binary甚至在不同OS都可以互通 ◦ 現實上...嘛。看起來太多細節需要處理,所以看運氣。
  5. 這次Demo要知道的ABI規範 • 單純就是如何傳遞參數給一個函數就好 • 我的電腦是Intel 64-bit,使用的ABI是 ▪ System V AMD64

    ABI • Calling convention ◦ 正整數和指標參數傳遞順序 ▪ RDI, RSI, RDX, RCX, … ▪ demo的printf最多用到兩個參數,demo的都是指標
  6. demo會用到的objdump指令 • objdump -d objfile ◦ 反組譯binary檔案 • objdump -t

    objfile ◦ 列出binary檔案的symbol table ▪ 不知道symbol table?問估狗囉 • objdump -h objfile ◦ 列出binary的section資訊 ▪ 不知道section,請找我以前linker script的投影片或 文章 • objdump -s -j section名稱 objfile ◦ 列出section的內容 資料
  7. 補充無關的資料 • function prolog ◦ 函數被呼叫的時候,實際上機械碼準備的事 ▪ 儲存相關status ▪ 處理和設定call

    stack ▪ 處理要用的暫存器 • function epilogue ◦ 離開函數的時候,實際上機械碼準備的事 ▪ 恢復和設定call stack ▪ 恢復用過的暫存器 ▪ 恢復前面儲存的status
  8. 打完收工,參考資料如下 • man ◦ 最man的男人 • API • ABI •

    System V AMD64 ABI • Wikipedia: Function prologue