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 $的更多相关文章

  1. 汇编指令mov、add、sub、jmp

    mov:寄存器,数据 mov:寄存器,寄存器 mov:寄存器,内存单元 mov:段寄存器,内存单元 mov:内存单元,寄存器 mov:内存单元,段寄存器 mov:段寄存器,寄存器 mov:寄存器,段寄 ...

  2. jmp使用

    jps -l jmap 36429 jmap -heap 36429 jmap -histo:live 36429 jmap -clstats 36429 jmap  -finalizerinfo 3 ...

  3. Hack Programming

    计算机由ROM(instruction memory).RAM(data memory).CPU组成,其关系如下图 在计算中存在3种寄存器:D.A.M.其中D是data register,A是addr ...

随机推荐

  1. JVM内存布局及GC知识回顾

    注:本文篇幅较长,且需要有一定的java基础,建议各位看官,备好瓜子.饮料.小板凳,摆个让自己舒服的姿势,慢慢细看^_^, 文中所有素材,均来自互联网,本人只是详细梳理了一遍,形成此文. 一.JVM运 ...

  2. 37、pendingIntent 点击通知栏进入页面

    转载: http://blog.csdn.net/yuzhiboyi/article/details/8484771 https://my.oschina.net/youranhongcha/blog ...

  3. 小程序WePY入门(一)

    全局安装或更新WePY命令行工具 npm install wepy-cli -g 在开发目录中生成Demo开发项目 wepy new myproject 切换至项目目录 cd myproject 开启 ...

  4. (图解)Description Resource Path Location Type Java compiler level does not match the version of

    Description Resource Path Location Type Java compiler level does not match the version of project 编译 ...

  5. argument python 参数 举例

    举例 例1:def multipute(x,y): x = 2 y[0] = ['spam'] return x,y X = 1 L = [1,2] X,L = multipute(X, L) pri ...

  6. eclipse 安装tomcat

  7. [容易] A + B 问题

    题目来源:http://www.lintcode.com/zh-cn/problem/a-b-problem/

  8. Oracle | PL/SQL Check约束用法详解

    1. 目标 实例讲解在Oracle中如何使用CHECK约束(创建.启用.禁用和删除) 2. 什么是Check约束? CHECK约束指在表的列中增加额外的限制条件. 注: CHECK约束不能在VIEW中 ...

  9. debian7 amd64版本添加对x86包的支持

    dpkg --add-architecture i386apt-get updateapt-get install ia32-libs ia32-libs-gtk

  10. 51Nod 1486 大大走格子 —— 组合数学

    题目链接:https://vjudge.net/problem/51Nod-1486 1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: ...