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 ...
随机推荐
- iOS对象(字典或数组)转化为JSon字符串
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; [dictionary setValue:@"he ...
- DB2 时间操作
1. SQL语句得到当前的日期,时间和时间戳 SELECT current date FROM sysibm.sysdummy1; SELECT current time FROM s ...
- SQLite集成与用法
本文转载至 http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/fra ...
- JVM相关小结
对JVM中分层模型.垃圾回收期.垃圾回收算法趁着周末小结一下.有不对的地方,还请指正和讨论~ 1.JVM内存模型 2.JVM垃圾回收期 3.JVM垃圾回收算法 ------------------- ...
- [原]js获取dom元素的实际位置及相对坐标
关键API: Element.getBoundingClientRect() mdn:https://developer.mozilla.org/en-US/docs/Web/API/Element/ ...
- 九度OJ 1052:找x (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7335 解决:3801 题目描述: 输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数 ...
- 字符串的朴素模式和KMP模式匹配
先复习一下字符串指针: #include <iostream> #include <string.h> using namespace std; int main() { ch ...
- [容易]在O(1)时间复杂度删除链表节点
题目来源:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/
- Qt & MySQL
Qt中如何进行MySQL连接与操作步骤: 1.向工程中的.pro文件增加QT += sql; 2.写一个通用的数据库连接类(Connect),一个static方法(CreateConnection), ...
- ubuntu下安装redis以及redis客户端在mac下的使用
ubuntu下安装redis http://blog.fens.me/linux-redis-install/ 此方式利用brew安装包去获取最新的rdm客户端 资源失效了 https://www.j ...