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
750
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.3k
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
680
GNU ld的linker script簡介
wen_liao
0
1.3k
Trace 程式碼之皮
wen_liao
0
900
淺談Debian套件打包
wen_liao
0
620
Other Decks in Technology
See All in Technology
Performance Insights 廃止から Database Insights 利用へ/transition-from-performance-insights-to-database-insights
emiki
0
170
Access-what? why and how, A11Y for All - Nordic.js 2025
gdomiciano
1
130
Trust as Infrastructure
bcantrill
1
370
GoでもGUIアプリを作りたい!
kworkdev
PRO
0
120
Vibe Coding Year in Review. From Karpathy to Real-World Agents by Niels Rolland, CEO Paatch
vcoisne
0
120
【Kaigi on Rails 事後勉強会LT】MeはどうしてGirlsに? 私とRubyを繋いだRail(s)
joyfrommasara
0
230
『OCI で学ぶクラウドネイティブ 実践 × 理論ガイド』 書籍概要
oracle4engineer
PRO
3
190
AWS 잘하는 개발자 되기 - AWS 시작하기: 클라우드 개념부터 IAM까지
kimjaewook
0
130
職種別ミートアップで社内から盛り上げる アウトプット文化の醸成と関係強化/ #DevRelKaigi
nishiuma
2
160
許しとアジャイル
jnuank
1
140
Large Vision Language Modelを用いた 文書画像データ化作業自動化の検証、運用 / shibuya_AI
sansan_randd
0
130
Developer Advocate / Community Managerなるには?
tsho
0
120
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
95
14k
Balancing Empowerment & Direction
lara
4
680
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Context Engineering - Making Every Token Count
addyosmani
5
220
Testing 201, or: Great Expectations
jmmastey
45
7.7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Mobile First: as difficult as doing things right
swwweet
224
10k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Practical Orchestrator
shlominoach
190
11k
4 Signs Your Business is Dying
shpigford
185
22k
How to Ace a Technical Interview
jacobian
280
24k
Unsuck your backbone
ammeep
671
58k
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