Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Hello world在那邊?背景說明 0
Search
Wen_Liao
January 24, 2015
Technology
0
740
Hello world在那邊?背景說明 0
部落格文尋找Hello world在背景說明
26/Jan/2015 增加prolog/epilogue說明
29/Jan/2015 增加%eax設成0的說明,感謝Scott Tasi大大的補充
Wen_Liao
January 24, 2015
Tweet
Share
More Decks by Wen_Liao
See All by Wen_Liao
COSCUP-2020-Linux 軟體組裝工和他的工具們
wen_liao
2
1.5k
開放街圖 自助旅行的好幫手
wen_liao
0
1.2k
GNU AS簡介
wen_liao
0
1.2k
UPnP 1.0 簡介
wen_liao
0
1.1k
自由軟體和 Richard Stallman
wen_liao
0
2.3k
A successful Git branching model 導讀
wen_liao
0
670
GNU ld的linker script簡介
wen_liao
0
1.3k
Trace 程式碼之皮
wen_liao
0
890
淺談Debian套件打包
wen_liao
0
600
Other Decks in Technology
See All in Technology
A2Aのクライアントを自作する
rynsuke
1
170
Observability infrastructure behind the trillion-messages scale Kafka platform
lycorptech_jp
PRO
0
140
Witchcraft for Memory
pocke
1
300
AWS アーキテクチャ作図入門/aws-architecture-diagram-101
ma2shita
29
11k
強化されたAmazon Location Serviceによる新機能と開発者体験
dayjournal
2
210
Postman AI エージェントビルダー最新情報
nagix
0
110
AIエージェント最前線! Amazon Bedrock、Amazon Q、そしてMCPを使いこなそう
minorun365
PRO
14
5.1k
Абьюзим random_bytes(). Фёдор Кулаков, разработчик Lamoda Tech
lamodatech
0
340
データプラットフォーム技術におけるメダリオンアーキテクチャという考え方/DataPlatformWithMedallionArchitecture
smdmts
5
630
AIの最新技術&テーマをつまんで紹介&フリートークするシリーズ #1 量子機械学習の入門
tkhresk
0
140
より良いプロダクトの開発を目指して - 情報を中心としたプロダクト開発 #phpcon #phpcon2025
bengo4com
1
3.1k
Agentic Workflowという選択肢を考える
tkikuchi1002
1
500
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
330
24k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
Six Lessons from altMBA
skipperchong
28
3.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Agile that works and the tools we love
rasmusluckow
329
21k
KATA
mclloyd
29
14k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
Why Our Code Smells
bkeepers
PRO
337
57k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
What's in a price? How to price your products and services
michaelherold
246
12k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Transcript
Wen Liao Hello world在那邊? 背景說明 嘉義,民雄
Disclaimer 投影片資料為作者整理資料及個人意見,沒有經 過嚴謹確認,請讀者自行斟酌
目標 說明分享會中live demo時的背景知識, 包含 • ABI • objdump使用方式 無法看live demo的朋友可以去看:尋找"Hello
World\n",實地操作。
先從API談起 引用Wikipedia: • Application programming interface的縮寫 • 一組讓開發者開發應用程式的 routine、通訊 協定、或是工具
• 使用相同的API,在不同平台上面重新編譯,應 該可以編譯成功,並且正確執行。
不懂?來個範例
不就是叫printf印出Hello World,有什麼稀奇。
你在Windows, Linux, ARM Linux, MIPS Linux上編譯都可 以印出Hello World
另外printf幹了什事? 不要忘記OS提供的服務稱為 system call
仔細看看男人 $ 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) ...
翻譯米糕 男人根據是依據數字編號的section來存放不同 種類的手冊 • Section 1:執行檔 • Section 2:System call
• Section 3:函式庫 • ...
那麼printf是system call嘛? 看來不是,那他是啥?
問男人,他說section 1和3都有printf,1 是執行檔的手冊,所以只有3可以挑
那麼printf到底怎麼讓螢幕印 出Hello World?
strace, 追蹤system call和signal的工具 使用了write system call 有人還記得 stdout fd是 1
嗎? 帶入要寫到 stdout的字 串 寫入12個 bytes 回傳已經寫 了12 bytes 印出來了嘿 嘿
結論:printf是 Standard I/O library提供的 API,讓使用者更方便的使用 stream,以及增進效能。 $ man stdio #
男人萬歲!
在不同的OS搞不好沒有File descriptor,寫入的system也 許也不叫write
但是只要你或是廠商能夠把 Standard I/O library移植到它 的OS,printf照樣印給你看。
回到ABI • Application Binary Interface的縮寫 • 規範binary,也就是你的執行程式和函式庫 ◦ 呼叫函數行為 ▪
機械碼執行的時候哪些暫存器放參數,哪些暫存器 放回傳值... ◦ Data type alignment ◦ System call呼叫規範 ◦ ... • 理想的世界 ◦ 同樣的硬體條件和同樣的ABI,不同compiler編出來的 binary甚至在不同OS都可以互通 ◦ 現實上...嘛。看起來太多細節需要處理,所以看運氣。
這次Demo要知道的ABI規範 • 單純就是如何傳遞參數給一個函數就好 • 我的電腦是Intel 64-bit,使用的ABI是 ▪ System V AMD64
ABI • Calling convention ◦ 正整數和指標參數傳遞順序 ▪ RDI, RSI, RDX, RCX, … ▪ demo的printf最多用到兩個參數,demo的都是指標
範例:不要問我plt是啥,我還沒搞清楚那 是三小 為什麼不是RSI而是ESI呢?kerker 有沒有很眼 熟?比對一 下上一頁
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的內容 資料
補充無關的資料 • function prolog ◦ 函數被呼叫的時候,實際上機械碼準備的事 ▪ 儲存相關status ▪ 處理和設定call
stack ▪ 處理要用的暫存器 • function epilogue ◦ 離開函數的時候,實際上機械碼準備的事 ▪ 恢復和設定call stack ▪ 恢復用過的暫存器 ▪ 恢復前面儲存的status
來看這個函 數
Function prolog Function epilogue 呼叫puts,為何用 edi?ABI規定。 為何設定0x4005e4? 可以用objdump看看
打完收工,參考資料如下 • man ◦ 最man的男人 • API • ABI •
System V AMD64 ABI • Wikipedia: Function prologue