*你应该先google:C,ojbect code, linker, disassemble* **目标:用C语言做底层汇编语言做的那些事** ## [Compile](https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#compile) 我们得研究研究C编译器如何编译代码,并且比较它与汇编器生成的机器码两者是否有所差别。 写一个只有一个简单函数的程序`function.c`。打开[function.c](https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c/function.c)看一眼。 ``` int my_function() { return 0xbaba; } ``` 编译与系统无关的代码时需要加`-ffreestanding`: `i386-elf-gcc -ffreestanding -c function.c -o function.o` 研究研究编译器生成的机器码: `i386-elf-objdump -d function.o` ``` function.o: file format elf32-i386 Disassembly of section .text: 00000000 <my_function>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: b8 ba ba 00 00 mov $0xbaba,%eax 8: 5d pop %ebp 9: c3 ret ``` 是不是有点似曾相识? ## [Link](https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#link) 最后,如果要生成二进制文件,我们需要使用 `linker`,中文也叫链接器。在这个时候,我们一定要搞明白高级语言是如何调用函数的标签的。其实我们不知道,程序在内存中的偏移地址(offset)是多少。我们将offset定为`0x0`并且使用`binary(二进制)`格式生成二进制码,而不包含任何标签或链接器需要的数据。 `i386-elf-ld -o function.bin -Ttext 0x0 --oformat binary function.o` *注意:会有一个 `warning`(i386-elf-ld: warning: cannot find entry symbol _start; defaulting to 0000000000000000),淡定的忽略掉它!* 使用`xxd`命令查看两个文件,我们发现`.bin`文件是机器码,而`.o`文件包含很多调试信息。 ```bash geyu@geyu-All-Series:~/workdir/os-dev/os-tutorial/12-kernel-c$ xxd function.bin 00000000: 5589 e5b8 baba 0000 5dc3 0000 1400 0000 U.......]....... 00000010: 0000 0000 017a 5200 017c 0801 1b0c 0404 .....zR..|...... 00000020: 8801 0000 1c00 0000 1c00 0000 d4ff ffff ................ 00000030: 0a00 0000 0041 0e08 8502 420d 0546 c50c .....A....B..F.. 00000040: 0404 0000 ``` ``` geyu@geyu-All-Series:~/workdir/os-dev/os-tutorial/12-kernel-c$ xxd function.o 00000000: 7f45 4c46 0101 0100 0000 0000 0000 0000 .ELF............ 00000010: 0100 0300 0100 0000 0000 0000 0000 0000 ................ 00000020: cc00 0000 0000 0000 3400 0000 0000 2800 ........4.....(. 00000030: 0a00 0700 5589 e5b8 baba 0000 5dc3 0047 ....U.......]..G 00000040: 4343 3a20 2847 4e55 2920 342e 392e 3100 CC: (GNU) 4.9.1. 00000050: 1400 0000 0000 0000 017a 5200 017c 0801 .........zR..|.. 00000060: 1b0c 0404 8801 0000 1c00 0000 1c00 0000 ................ 00000070: 0000 0000 0a00 0000 0041 0e08 8502 420d .........A....B. 00000080: 0546 c50c 0404 0000 002e 7379 6d74 6162 .F........symtab 00000090: 002e 7374 7274 6162 002e 7368 7374 7274 ..strtab..shstrt 000000a0: 6162 002e 7465 7874 002e 6461 7461 002e ab..text..data.. 000000b0: 6273 7300 2e63 6f6d 6d65 6e74 002e 7265 bss..comment..re 000000c0: 6c2e 6568 5f66 7261 6d65 0000 0000 0000 l.eh_frame...... 000000d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 000000e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 000000f0: 0000 0000 1b00 0000 0100 0000 0600 0000 ................ 00000100: 0000 0000 3400 0000 0a00 0000 0000 0000 ....4........... 00000110: 0000 0000 0100 0000 0000 0000 2100 0000 ............!... 00000120: 0100 0000 0300 0000 0000 0000 3e00 0000 ............>... 00000130: 0000 0000 0000 0000 0000 0000 0100 0000 ................ 00000140: 0000 0000 2700 0000 0800 0000 0300 0000 ....'........... 00000150: 0000 0000 3e00 0000 0000 0000 0000 0000 ....>........... 00000160: 0000 0000 0100 0000 0000 0000 2c00 0000 ............,... 00000170: 0100 0000 3000 0000 0000 0000 3e00 0000 ....0.......>... 00000180: 1200 0000 0000 0000 0000 0000 0100 0000 ................ 00000190: 0100 0000 3900 0000 0100 0000 0200 0000 ....9........... 000001a0: 0000 0000 5000 0000 3800 0000 0000 0000 ....P...8....... 000001b0: 0000 0000 0400 0000 0000 0000 3500 0000 ............5... 000001c0: 0900 0000 0000 0000 0000 0000 f402 0000 ................ 000001d0: 0800 0000 0800 0000 0500 0000 0400 0000 ................ 000001e0: 0800 0000 1100 0000 0300 0000 0000 0000 ................ 000001f0: 0000 0000 8800 0000 4300 0000 0000 0000 ........C....... 00000200: 0000 0000 0100 0000 0000 0000 0100 0000 ................ 00000210: 0200 0000 0000 0000 0000 0000 5c02 0000 ............\... 00000220: 8000 0000 0900 0000 0700 0000 0400 0000 ................ 00000230: 1000 0000 0900 0000 0300 0000 0000 0000 ................ 00000240: 0000 0000 dc02 0000 1800 0000 0000 0000 ................ 00000250: 0000 0000 0100 0000 0000 0000 0000 0000 ................ 00000260: 0000 0000 0000 0000 0000 0000 0100 0000 ................ 00000270: 0000 0000 0000 0000 0400 f1ff 0000 0000 ................ 00000280: 0000 0000 0000 0000 0300 0100 0000 0000 ................ 00000290: 0000 0000 0000 0000 0300 0200 0000 0000 ................ 000002a0: 0000 0000 0000 0000 0300 0300 0000 0000 ................ 000002b0: 0000 0000 0000 0000 0300 0500 0000 0000 ................ 000002c0: 0000 0000 0000 0000 0300 0400 0c00 0000 ................ 000002d0: 0000 0000 0a00 0000 1200 0100 0066 756e .............fun 000002e0: 6374 696f 6e2e 6300 6d79 5f66 756e 6374 ction.c.my_funct 000002f0: 696f 6e00 2000 0000 0202 0000 ion. ....... ``` ## [Decompile](https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#decompile) 好奇不好奇机器码对应着怎样的汇编代码? `ndisasm -b 32 function.bin` ## [More](https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#more) 强烈建议多谢几个小程序,就像以下这样: * Local variables `localvars.c` * Function calls `functioncalls.c` * Pointers `pointers.c` 仔细看一看os-dev.pdf相关的章节,编译并反汇编上面的代码,对比研究它们的机器码。最后回答这个问题,`pointers.c`编译后为什么与我们预期不一致?"Hello"的ASCII码`0x48656c6c6f`在哪里? [THE ORIGIN ARTICALE IN GITHUB:](https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c/README.md)[^1] ------------------ >参考资料: >[1]:https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c 版权注明:本文所有涉及到:`https://github.com/cfenollosa/os-tutorial/` git仓库的内容,全部对应以下开源协议声明: [BSD 3-Clause License Copyright (c) 2018, Carlos Fenollosa](https://github.com/cfenollosa/os-tutorial/blob/master/LICENSE)
author | geyu | ||||||
---|---|---|---|---|---|---|---|
permlink | 11-create-an-os-from-scratch-11 | ||||||
category | esteem | ||||||
json_metadata | {"links":["https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#compile","https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c/function.c","https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#link","https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#decompile","https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c#more","https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c/README.md","https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c","https://github.com/cfenollosa/os-tutorial/","https://github.com/cfenollosa/os-tutorial/blob/master/LICENSE"],"tags":["esteem","esteem-cn","steemstem","cn-stem","cn"],"app":"esteem/2.0.7-surfer","format":"markdown+html","community":"esteem.app"} | ||||||
created | 2019-04-16 08:31:42 | ||||||
last_update | 2019-04-17 13:58:33 | ||||||
depth | 0 | ||||||
children | 3 | ||||||
last_payout | 2019-04-23 08:31:42 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 HBD | ||||||
curator_payout_value | 0.000 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 5,955 | ||||||
author_reputation | 1,811,446,112,774 | ||||||
root_title | "从头写一个操作系统 11 (create an OS from scratch 11)" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 83,187,481 | ||||||
net_rshares | 9,083,680,833 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
stomatolog2 | 0 | 40,786,741 | 100% | ||
cheetah | 0 | 19,441,308 | 0.08% | ||
yehey | 0 | 2,520,864,265 | 10% | ||
pinoy | 0 | 90,311,557 | 10% | ||
geyu | 0 | 1,445,257,403 | 100% | ||
ratticus | 0 | 4,279,733,042 | 10% | ||
acknowledgement | 0 | 170,144,407 | 2.5% | ||
eringa | 0 | 517,142,110 | 100% |
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in: https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c
author | cheetah |
---|---|
permlink | cheetah-re-geyu11-create-an-os-from-scratch-11 |
category | esteem |
json_metadata | "" |
created | 2019-04-16 08:31:54 |
last_update | 2019-04-16 08:31:54 |
depth | 1 |
children | 0 |
last_payout | 2019-04-23 08:31:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 164 |
author_reputation | 942,693,160,055,713 |
root_title | "从头写一个操作系统 11 (create an OS from scratch 11)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,187,488 |
net_rshares | 0 |
[Source](https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c) Copying/Pasting full or partial texts without adding anything original is frowned upon by the community. Repeated copy/paste posts could be considered spam. Spam is discouraged by the community, and may result in action from the [cheetah bot](https://steemit.com/faq.html#What_is__cheetah). [More information and tips on sharing content.](https://steemcleaners.org/copy-paste-plagiarism/) If you believe this comment is in error, please contact us in [#disputes on Discord](https://discord.gg/YR2Wy5A)
author | steemcleaners |
---|---|
permlink | re-geyu-11-create-an-os-from-scratch-11-20190417t130519032z |
category | esteem |
json_metadata | {"app":"steemcleaners/0.3","format":"markdown+html","community":"steemcleaners"} |
created | 2019-04-17 13:05:21 |
last_update | 2019-04-17 13:05:21 |
depth | 1 |
children | 0 |
last_payout | 2019-04-24 13:05:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.890 HBD |
curator_payout_value | 0.279 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 585 |
author_reputation | 2,789,224,428,782,668 |
root_title | "从头写一个操作系统 11 (create an OS from scratch 11)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,258,208 |
net_rshares | 1,953,489,181,560 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
adm | 0 | 1,811,538,786,288 | 9.5% | ||
jadabug | 0 | 1,891,058,529 | 1% | ||
ezravandi | 0 | 3,461,627,879 | 1% | ||
sivehead | 0 | 453,648,798 | 2% | ||
abcor | 0 | 5,743,649,254 | 3% | ||
sc-fund | 0 | 126,471,093,122 | 13% | ||
mixr | 0 | 1,708,425,379 | 8% | ||
steamsteam | 0 | 529,296,740 | 100% | ||
votes4minnows | 0 | 556,211,277 | 1% | ||
hdu | 0 | 1,135,384,294 | 1% | ||
i-c-e | 0 | 0 | 2% |
Congratulations @geyu! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) : <table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@geyu/voted.png?201904151935</td><td>You received more than 1000 upvotes. Your next target is to reach 2000 upvotes.</td></tr> </table> <sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@geyu) and compare to others on the [Steem Ranking](http://steemitboard.com/ranking/index.php?name=geyu)_</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> ###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
author | steemitboard |
---|---|
permlink | steemitboard-notify-geyu-20190416t084725000z |
category | esteem |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2019-04-16 08:47:24 |
last_update | 2019-04-16 08:47:24 |
depth | 1 |
children | 0 |
last_payout | 2019-04-23 08:47:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 816 |
author_reputation | 38,975,615,169,260 |
root_title | "从头写一个操作系统 11 (create an OS from scratch 11)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,188,079 |
net_rshares | 0 |