jnq

jnq

0个粉丝

11

问答

0

专栏

0

资料

jnq  发布于  2012-12-04 13:11:08
采纳率 0%
11个问答
3193

使用WIN32环境下的gcc及GNU 1(转载)

使用WIN32环境下的gcc及GNU Binary Utilities(GBU)进行c语言与汇编语言(i386)的
交叉编译与调试
一、要进行编译的例子程序
cprogram.c
----------------------------------------------------------------------------
----
main(){
int inputvalue;
printf("please input an integer:");
scanf("%d",&inputvalue);
printf(" the result is:%d",addition(inputvalue));
//addition()函数是由下面的汇编语言程序实现的。实现从0到inputvlue的求和。
}
----------------------------------------------------------------------------
2 assem.s (i386汇编程序)
----------------------------------------------------------------------------
----
#注以"#"打头的行为i386汇编的注释行。
# 函数申明为全局:(addition前面_为与c接口标准需要)
.globl _addition;
_addition:
#下面为该函数的汇编代码实现,其中 4(%esp)指向第一个函数参数,esp指向返回地址
# eax 存放最终的返回值。该值返回给调用者。该函数实现从1到inputvalue的累加。
movl 4(%esp),%eax
movl %eax ,%ecx
movl $0,%eax
addloop: addl %ecx,%eax
loop addloop
ret
 二、使用gcc进行编译成可执行文件
假定assem.s,cprogram.c都在工作目录(d:\)下。
敲入如下命令行便得到可执行文件(a.out),
● gcc -v assem.s cprogram.c
下面是选择的一些重要编译过程中输出屏上的信息(...表省略 //为添加的注释行)
  ...\cpp.exe -lang-c -v... cprogram.c D:\TEMP\cc3QZqHA.i
GNU CPP version egcs-2.91.57 19980901 (egcs-1.1 release) (80386, BSD syntax)
// 上面由cpp 把cporgram.c预处理成cc3QZqHA.i中间文件
...\cc1.exe D:\TEMP\cc3QZqHA. i-quiet -dumpbase cprogram.c -version -o D:\TE
MP\ccsYymVt.s
GNU C version egcs-2.91.57 19980901 (egcs-1.1 release) (i586-cygwin32) compi
led by GNU C version egcs
-2.91.57 19980901 (egcs-1.1 release).
//上面由cc1把cc3QZqHA.i 编译成汇编代码文件ccsYymVt.s
...\as.exe -o D:\TEMP\cchtZi1u.o D:\TEMP\ccsYymVt.s
//as把ccsYymVt.s汇编成目标代码文件 cchtZi1u.o,从而把.c文件翻译成.o文件
//as对assem.s的汇编:
...\as.exe -o D:\TEMP\ccc3YQ7A.o assem.s
assem.s: Assembler messages:...
----------------------------------------------------------------------------
----
//collect2完成link,从而形成a.exe的可执行文件。
...collect2.exe ... \lib\crt0.o -LC:\... D:\TEMP\cchtZi1u.o D:\TEMP\ccc3YQ7A
.o
-lgcc -lcygwin -lkernel32 -ladvapi32 -lshell32 -lgcc
三、使用GBU分析编译过程
为了分析的方便,不妨先用命令行 gcc -c cprogram.c 及gcc -c assem.s 生成目标文
件cprogram.o
和assem.o,再用命令行gcc -o cprogram.exe cprogram.o assem.o 生成可执行文件cp
rogram.exe。
1、用nm对obj文件进行符号分析
执行命令行 nm -g cprgram.o可以得到cprogram.o的全局符号:
//offset type symbol
U ___main
U _addition
00000030 T _main
U _printf
U _scanf
类型U表示未定义的全局符号,链接时需重定位,T表示定义在代码段中的全局符号。从
中可以看出,编译cprogram.c时addition函数处理与对printf、scanf的处理一样,先标
记为外部引用,待链接时再重定位,当然这时它们的相对地址也无从知晓。
● 执行命令行 nm -g cprogram.exe 可看到_addition重定向之后的绝对地址为004
01040。
  00401040 T _addition
  004011d4 T _cygwin_crt0
  00401274 T _dll_crt0__FP11per_process
  0040126c T _dll_dllcrt0
  00401264 T _dll_noncygwin_dllcrt0
  00402008 D _environ
  0040128c T _free
  00401080 T _main
  00401000 T _mainCRTStartup
  00401294 T _malloc
  0040125c T _printf
  00401284 T _realloc
  00401254 T _scanf
  00000000 A end
  004012b4 T etext

2、使用objdump分析目标文件及可执行文件:
 
用以下命令行可查看相应目标文件及重定位信息。
● objdump -dr assem.o cprogram.o
下面为该命令行部分输出信息:
assem.o: file format pe-i386
Disassembly of section .text:
00000000 <_addition>:
0: 8b 44 24 04 mov 0x4(%esp,1),%eax
4: 89 c1 mov %eax,%ecx
6: b8 00 00 00 00 mov $0x0,%eax
0000000b :
b: 01 c8 add %ecx,%eax
d: e2 fc loop b
f: c3 ret
cprogram.o: file format pe-i386
// cprogram.o
// cprogram.o
Disassembly of section .text:
00000000 <.text>:
0: 70 6c jo 6e <_main+0x3e>
...
 00000030 <_main>:
 30: 55 push %ebp
 31: 89 e5 mov %esp,%ebp
 33: 83 ec 10 sub $0x10,%esp
36: e8 00 00 00 00 call 3b <_main+0xb>
37: DISP32 ___main //需重定位
3b: 68 00 00 00 00 push $0x0
3c: dir32 .text  //需重定位
40: e8 00 00 00 00 call 45 <_main+0x15>
41: DISP32 _printf //需重定位
45: 83 c4 04 add $0x4,%esp
48: 8d 45 fc lea 0xfffffffc(%ebp),%eax
4b: 50 push %eax
4c: 68 19 00 00 00 push $0x19
4d: dir32 .text  //需重定位
51: e8 00 00 00 00 call 56 <_main+0x26>
52: DISP32 _scanf  //需重定位
56: 83 c4 08 add $0x8,%esp
59: 8b 45 fc mov 0xfffffffc(%ebp),%eax
5c: 50 push %eax
5d: e8 00 00 00 00 call 62 <_main+0x32>
5e: DISP32 _addition //需重定位addition
62: 83 c4 04 add $0x4,%esp
65: 89 c0 mov %eax,%eax
67: 50 push %eax
68: 68 1c 00 00 00 push $0x1c
69: dir32 .text //需重定位
6d: e8 00 00 00 00 call 72 <_main+0x42>
6e: DISP32 _printf //需重定位
72: 83 c4 08 add $0x8,%esp
75: 89 ec mov %ebp,%esp
77: 5d pop %ebp
78: c3 ret
79: 00 00 add %al,(%eax)
...
)
...
● 也可用命令行 objdump -r assem.o cprogram.o得到两个文件中需重定位的信息:
 assem.o: file format pe-i386
 cprogram.o: file format pe-i386
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
00000037 DISP32 ___main
0000003c dir32 .text
00000041 DISP32 _printf
0000004d dir32 .text
00000052 DISP32 _scanf
0000005e DISP32 _addition
00000069 dir32 .text
0000006e DISP32 _printf
 
最后可通过命令行
● objdump -d cprogram.exe
可看到最终的执行代码及相应的反汇编代码及重定位信息的回填情况:
绝对内存地址 机器代码     反汇编代码
00401000 <_mainCRTStartup>:
401000: 55 push %ebp
...
00401040 <_addition>:
401040: 8b 44 24 04 mov 0x4(%esp,1),%eax
401044: 89 c1 mov %eax,%ecx
401046: b8 00 00 00 00 mov $0x0,%eax
...
00401080 <_main>:
401080: 55 push %ebp
401081: 89 e5 mov %esp,%ebp
401083: 83 ec 10 sub $0x10,%esp
401086: e8 c1 01 00 00 call 40124c <___main>
40108b: 68 50 10 40 00 push $0x401050
401090: e8 c7 01 00 00 call 40125c <_printf>
401095: 83 c4 04 add $0x4,%esp
401098: 8d 45 fc lea 0xfffffffc(%ebp),%eax
40109b: 50 push %eax
40109c: 68 69 10 40 00 push $0x401069
4010a1: e8 ae 01 00 00 call 401254 <_scanf>
4010a6: 83 c4 08 add $0x8,%esp
4010a9: 8b 45 fc mov 0xfffffffc(%ebp),%eax
4010ac: 50 push %eax
4010ad: e8 8e ff ff ff call 401040 <_addition> //4010b2 +ffffff8e=401040
4010b2: 83 c4 04 add $0x4,%esp
4010b5: 89 c0 mov %eax,%eax
4010b7: 50 push %eax
4010b8: 68 6c 10 40 00 push $0x40106c
4010bd: e8 9a 01 00 00 call 40125c <_printf>
4010c2: 83 c4 08 add $0x8,%esp
4010c5: 89 ec mov %ebp,%esp
4010c7: 5d pop %ebp
4010c8: c3 ret
4010c9: 00 00 add %al,(%eax)
...
0040125c <_printf>:
40125c: ff 25 98 40 40 jmp *0x404098
401261: 00
401262: 90 nop
401263: 90 nop
我来回答
回答0个
时间排序
认可量排序
易百纳技术社区暂无数据
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区