jmp $
in intel x86 instruction set, "jmp $" means jump to this instruction location, thus falling into an infinite loop.
https://defuse.ca/online-x86-assembler.htm#disassembly
the instruction is "0xfeeb".
Based on this instruction, we can create possibly the shortest C program that can compile and run successfully on x86 platform.
main=0xfeeb;
1, the variable main has no type here, and will be defaulted to integer (int). this reminds us of the good old K&R days. This is still allowed by latest C standards (i.e. C99). therefore it's actually
int main=0xfeeb;
2, the variable main is a global variable, therefore the symbol "main" will be exported in this compilation unit. for example, if the file is named "shortest_c_program.c" and we execute the following commands:
$ gcc -std=c99 shortest_c_program.c -c
shortest_c_program.c::: warning: data definition has no type or storage class [enabled by default]
shortest_c_program.c::: warning: type defaults to ‘int’ in declaration of ‘main’ [enabled by default] $ objdump --syms shortest_c_program.o shortest_c_program.o: file format pe-i386 SYMBOL TABLE:
[ ](sec -)(fl 0x00)(ty )(scl ) (nx ) 0x00000000 shortest_c_program.c
File
[ ](sec )(fl 0x00)(ty )(scl ) (nx ) 0x00000000 .text
AUX scnlen 0x0 nreloc nlnno
[ ](sec )(fl 0x00)(ty )(scl ) (nx ) 0x00000000 .data
AUX scnlen 0x4 nreloc nlnno
[ ](sec )(fl 0x00)(ty )(scl ) (nx ) 0x00000000 .bss
AUX scnlen 0x0 nreloc nlnno
[ ](sec )(fl 0x00)(ty )(scl ) (nx ) 0x00000000 _main
it's confirmed that the symbol "_main" is exported.
3, when this object file is linked against the compiler attached crt stub (part of the library e.g. glibc), by default the entry point is the symbol "_start". the symbol "_start" points to some code that will call a symbol "_main". typically the symbol _main points to the main function which is the compiled version of C main function. In this case, main actually points to a location where the value of the main variable is stored.
http://ftp.gnu.org/pub/old-gnu/Manuals/ld-2.9.1/html_node/ld_24.html
4, when _start calls _main, the cpu actually takes 0xfeeb as an instruction which is "jmp $" on x86, therefore it executes the instruction again and again.
another point, what's the shortest legitimate C program? i.e. which can compile successfully (but might not run successfully)
Answer:
main;
because main is a global variable, it's initialised to 0, therefore the program will crash on segfault (null pointer dereference).
jmp $的更多相关文章
- 汇编指令mov、add、sub、jmp
mov:寄存器,数据 mov:寄存器,寄存器 mov:寄存器,内存单元 mov:段寄存器,内存单元 mov:内存单元,寄存器 mov:内存单元,段寄存器 mov:段寄存器,寄存器 mov:寄存器,段寄 ...
- jmp使用
jps -l jmap 36429 jmap -heap 36429 jmap -histo:live 36429 jmap -clstats 36429 jmap -finalizerinfo 3 ...
- Hack Programming
计算机由ROM(instruction memory).RAM(data memory).CPU组成,其关系如下图 在计算中存在3种寄存器:D.A.M.其中D是data register,A是addr ...
随机推荐
- C#抓取网面上的html内容(JS动态生成的无法抓取)
抓取内容的代码: /// </summary> /// <param name="url">路径URL</param> /// <para ...
- MySQL 事务1
本人应用的MySQL的版本为:5.6.22
- GS与数据库打交道
GS与数据库打交道 link_stat stat = (link_stat)rPkt.size; if (stat == link_stat::link_connected) { GameChanne ...
- 前后端分离之fiddler前端开发代理 autoresponder 正则表达式 regex:(?insx) 修正符详解
regex:(?isx)^http://127.0.0.1:3000(/dlscene)?/order/(\w*) http://127.0.0.1:8080/dlscene/order/$2 上面这 ...
- Brotli
https://engineering.linkedin.com/blog/2017/05/boosting-site-speed-using-brotli-compression?utm_sourc ...
- Linux安装mariadb详细步骤
1.安装mariadb yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./configure --preifx=软件安装的绝对路径 2.yum仓库的软件,版本 ...
- Ubuntu12.04如何修改窗口背景色为眼睛保护色来保护眼睛,强力推荐!!
最近突然发现盯着屏幕看的久了,眼睛会非常的痛苦,因此想改变一下系统的窗口背景颜色.其实看代码主要是在Eclipse里面察看,因此一开始我就想改变Eclipse的文本编辑框的背景颜色,效果如下图所示. ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- Excel图表转成图片
关于excel 图表转成图片 知识点:excel 生成的图表不是图片 尝试. 通过Java调用POI接口挺难把excel生成的图表转成图片导出来 ps. 其它生成图表的工具,如jfre ...
- 【LeetCode】种花问题
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...