- 浏览: 74407 次
- 性别:
- 来自: 杭州
-
最新评论
参考链接:http://timelessname.com/elfbin/
环境要求:linux gcc nasm hexcurse(用来修改elf文件内容)
先尝试用C语言写"Hello,World"程序(名为chello.c):
#include <stdio.h> int main(void) { printf("Hello,World\n"); return 0; }
使用下面命令编译并运行:
[host@myhost linker]$ gcc -o chello chello.c [host@myhost linker]$ ./chello
输出结果:
Hello,World
可以用下面命令查看chello的ELF头部分:
readelf -h chello
输出结果:
ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Intel 80386 Version: 0x1 Entry point address: 0x8048310 Start of program headers: 52 (bytes into file) Start of section headers: 1932 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 8 Size of section headers: 40 (bytes) Number of section headers: 30 Section header string table index: 27
使用下面命令查看chello链接的动态链接库:
ldd chello
输出结果为:
linux-gate.so.1 => (0xb7857000) libc.so.6 => /lib/libc.so.6 (0xb76d2000) /lib/ld-linux.so.2 (0xb7858000)
使用下面命令查看文件类型:
file chello
输出结果:
chello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.27, not stripped
使用下面命令查看文件大小,并使用strip取出符号表,然后查看文件大小:
[host@myhost linker]$ ls -l chello [host@myhost linker]$ strip -s chello [host@myhost linker]$ ls -l chello
输出结果为:
-rwxr-xr-x 1 host users 4746 11月 6 23:07 chello -rwxr-xr-x 1 host users 3036 11月 6 23:15 chello
下面使用汇编代码编写该程序(hello.asm),调用linux中断来实现:
SECTION .data msg: db "Hello,World",10 len: equ $-msg SECTION .text global main main: mov edx,len mov ecx,msg mov ebx,1 mov eax,4 int 0x80 mov ebx,0 mov eax,1 int 0x80
使用下面的命令编译链接并取出生成文件的符号表:
[host@myhost linker]$ nasm -f elf hello.asm [host@myhost linker]$ gcc -o hello hello.o -nostartfiles -nostdlib -nodefaultlibs [host@myhost linker]$ strip -s hello [host@myhost linker]$ ./hello
输出结果为:
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080 Hello,World
再用gcc命令时会产生一个警告,但该文件仍然能够执行。此时文件大小为360个字节。
此处链接命令“gcc -o hello hello.o -nostartfiles -nostdlib -nodefaultlibs"中几个选项英文注解如下(链接 ):
-nostartfiles Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib or -nodefaultlibs is used. -nodefaultlibs Do not use the standard system libraries when linking. Only the libraries you specify will be passed to the linker, options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, will be ignored. The standard startup files are used normally, unless -nostartfiles is used. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified. -nostdlib Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed to the linker, options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, will be ignored. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified. One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines that GCC uses to overcome shortcomings of particular machines, or special needs for some languages. (See Interfacing to GCC Output, for more discussion of libgcc.a.) In most cases, you need libgcc.a even when you want to avoid other standard libraries. In other words, when you specify -nostdlib or -nodefaultlibs you should usually specify -lgcc as well. This ensures that you have no unresolved references to internal GCC library subroutines. (For example, `__main', used to ensure C++ constructors will be called;
此处startupfiles指的是crt0.o(/lib/crt0.o),crtbegin.o,crtend.o。据说crt0.o包含调用main函数(windows下调用WinMainCRTStartup (参考链接 ))的代码,这里使用汇编代码(如果是c程序,则需要链接crt0.o),所以不用链接crt0.o.crtbegin.o和crtend.o据说是用来对c++构造和析构函数进行处理。有一个帖子(Is main required for a c program? also see Why are DJGPP .exe files so large? )说明了不用main函数来编写c程序(其实只是把入口名称换换而已,就像Window应用程序使用WinMain来作为入口函数,只不过使用自己的入口函数,相关的一些处理都需要自己来解决)
使用下面命令得到hello的16进制描述:
hexdump -x hello
输出结果:
0000000 457f 464c 0101 0001 0000 0000 0000 0000 0000010 0002 0003 0001 0000 8080 0804 0034 0000 0000020 00c8 0000 0000 0000 0034 0020 0002 0028 0000030 0004 0003 0001 0000 0000 0000 8000 0804 0000040 8000 0804 00a2 0000 00a2 0000 0005 0000 0000050 1000 0000 0001 0000 00a4 0000 90a4 0804 0000060 90a4 0804 000c 0000 000c 0000 0006 0000 0000070 1000 0000 0000 0000 0000 0000 0000 0000 0000080 0cba 0000 b900 90a4 0804 01bb 0000 b800 0000090 0004 0000 80cd 00bb 0000 b800 0001 0000 00000a0 80cd 0000 6548 6c6c 2c6f 6f57 6c72 0a64 00000b0 2e00 6873 7473 7472 6261 2e00 6574 7478 00000c0 2e00 6164 6174 0000 0000 0000 0000 0000 00000d0 0000 0000 0000 0000 0000 0000 0000 0000 * 00000f0 000b 0000 0001 0000 0006 0000 8080 0804 0000100 0080 0000 0022 0000 0000 0000 0000 0000 0000110 0010 0000 0000 0000 0011 0000 0001 0000 0000120 0003 0000 90a4 0804 00a4 0000 000c 0000 0000130 0000 0000 0000 0000 0004 0000 0000 0000 0000140 0001 0000 0003 0000 0000 0000 0000 0000 0000150 00b0 0000 0017 0000 0000 0000 0000 0000 0000160 0001 0000 0000 0000
分析该文件头(前52个字节)可以知道两个比较重要表的内容,第一个是程序头表(54(0x34)个字节开始,大小为2*32(0x20)个字节),另一个是段头表(200(0xc8)个字节开始,大小为2*40(0x28)个字节),然后根据段头表可以知道代码段(0x80开始34个字节内容)和数据段(0xa4开始12个字节内容)相关信息。比较重要的是前176个字节内容,这部分内容可以分为文件头(52个字节),程序头表(64个字节),空白内容(12个字节),代码段(34个字节),数据段(12个字节,"Hello,World\n")。
先使用下面命令提取hello中前176个字节内容修改权限为可执行:
dd if=hello of=hello.new bs=176 count=1 chmod u+x hello.new
得到文件hello.new,执行该文件可以得到"Hello,World".该文件二进制内容:
0000000 457f 464c 0101 0001 0000 0000 0000 0000 0000010 0002 0003 0001 0000 8080 0804 0034 0000 0000020 00c8 0000 0000 0000 0034 0020 0002 0028 0000030 0004 0003 0001 0000 0000 0000 8000 0804 0000040 8000 0804 00a2 0000 00a2 0000 0005 0000 0000050 1000 0000 0001 0000 00a4 0000 90a4 0804 0000060 90a4 0804 000c 0000 000c 0000 0006 0000 0000070 1000 0000 0000 0000 0000 0000 0000 0000 0000080 0cba 0000 b900 90a4 0804 01bb 0000 b800 0000090 0004 0000 80cd 00bb 0000 b800 0001 0000 00000a0 80cd 0000 6548 6c6c 2c6f 6f57 6c72 0a64
由于数据段大小刚好为12,而文件中有刚好有12个字节空白,可以将数据段(0xa4开始的12个字解)迁移到空白处(0x74开始的12个字节),并将0x86地址的0xa4改为0x74。可以使用hexcurse hello.new修改文件内容,然后用快捷键crtl+s保存,并改名为hello.res.此时执行hello.res可以得到"Hello,World"。
然后我们可以删除最后的12个字节(使用命令dd if=hello.res of=hello.out bs=164 count=1),得到hello.out即为最后的结果,其大小为164个字节。按照原文中描述,该文件应该可以进一步的压缩,不过这需要对代码段中部分做一些改动,有空时再详细研究)
而文件头中还有一个e_entry(0x18地址开始的4个字节,值为0x08048080(0x08048000+0x80(代码段偏移地址))表示程序入口,即从这个地址开始执行指令。代码段中(0x86开始的4个字节(小端法表示,9074 0804)即0x08049074,这个地址是0x08048000+0x1000(代码段虚拟地址所占的空间大小,因为段对齐为0x1000,所以最小为4k大小(分页机制中每个页面的大小))+0x74,这个地址是从原来的地址0x080490a4(这个地址也分别存在于原来的0x0000005c和0x00000060开始的4个字节。但是好像0x0000005c和0x00000060中的值不改业能正常运行,但为了使得数据保持一致,最好还是改掉。)。
另外,hello.out只包含ELF头,程序头,代码段和数据段,并且仍然能正常运行,这也证明了可执行文件中段头表(section Header table)是可选项。
发表评论
-
最小c编译器
2011-11-08 14:09 1551最小c编译器(来源 (最好在linux下操作))代码有好几个 ... -
the development of c language(转)
2011-11-08 09:25 1428c语言之父Dennis Ritchie 写的关于c语言开发历 ... -
C语言,你真的弄懂了么?
2011-11-07 12:42 1804程序(来源 ): #include <stdi ... -
pe文件格式实例解析
2011-11-07 10:05 0环境:windows xp 速龙3000+(即x86兼容32位 ... -
elf文件格式实例解析
2011-11-05 23:00 6411试验环境:archlinux 速龙3000+(即x86兼 ... -
高质量的c源代码
2011-11-03 10:18 1221现在自由软件及开源软件越来越流行,有大量的附带源程序 ... -
fltk 库
2011-09-26 19:47 1889fltk是一个小型、开源、支持OpenGL 、跨平台(win ... -
《Introduction to Computing Systems: From bits and gates to C and beyond》
2011-09-25 23:33 2228很好的一本计算机的入门书,被很多学校采纳作为教材,作者Yale ... -
csapp bufbomb实验
2011-09-16 14:21 4669csapp (《深入理解计算机系统》)一书中有一个关于缓冲区 ... -
the blocks problem(uva 101 or poj 1208)
2011-09-11 20:57 1857题目描述见:uva 101 or poj 1208 ... -
the blocks problem(uva 101 or poj 1208)
2011-09-11 20:56 0题目描述见:uva 101 or poj 1208 ... -
部分排序算法c语言实现
2011-09-02 14:51 1039代码比较粗糙,主要是用于对排序算法的理解,因而忽略了边界和容错 ... -
编译器开发相关资源
2011-08-31 08:40 1243开发编译器相关的一些网络资源: how difficu ... -
zoj 1025 Wooden Sticks
2011-07-23 20:25 985题目见:zoj 1025 先对木棒按照长度进行排序,然后再计 ... -
zoj 1088 System Overload
2011-07-23 17:30 1189约瑟夫环 (josephus problem )问题, ... -
zoj 1091 Knight Moves
2011-07-23 09:05 870题目见zoj 1091 使用宽度搜索优先来求解, ... -
zoj 1078 palindrom numbers
2011-07-22 19:31 1167题目见zoj 1078 主要是判断一个整数在基数为2 ... -
zoj 1006 do the untwist
2011-07-22 13:24 960题目见zoj 1006 或poj 1317 简单 ... -
zoj 3488 conic section
2011-07-22 12:23 1040题目见zoj 3488 很简单的题目,却没能一次搞定,因 ... -
zoj 1005 jugs
2011-07-22 11:43 876题目内容见zoj1005 由于A,B互素且A的容 ...
相关推荐
在本工程模板中,我们将会探讨ESP8266的基本使用、RTOS环境的搭建以及如何实现一个简单的"Hello World"串口打印程序。 1. ESP8266概述: ESP8266是一款经济高效的Wi-Fi芯片,由乐鑫科技(Espressif Systems)制造,...
1. 编写汇编程序:使用文本编辑器创建ASM文件,如hello.asm,编写简单的“Hello, World!”程序。 ```assembly section .data msg db 'Hello, World!',0 section .text global _start _start: mov eax, 4 ; 系统...
5. **实例分析**:提供实际的代码示例,展示如何用汇编语言编写Linux下的简单程序,如“Hello, World!”、读写文件、创建进程等。 6. **性能优化**:讨论如何使用汇编语言来优化C/C++代码中的关键部分,提高程序...
- **第一个程序**: 通常以“Hello World”作为入门程序。 **1.2 常量、变量和表达式** - **常量**: 不会改变其值的数据项。 - **变量**: 可以在程序运行过程中改变的值。 - **赋值**: 将一个值赋给一个变量的操作。...
const char* message = "Hello, World!"; libasm_putstr(message, strlen(message)); // 调用libasm的函数打印字符串 return 0; } ``` 编译时,需要链接`libasm`库,例如: ```bash gcc main.c -o main -lasm `...