免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1820 | 回复: 4
打印 上一主题 下一主题

[其他] gcc -S 汇编 [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-26 23:25 |只看该作者 |倒序浏览
本帖最后由 shihyu 于 2015-08-26 23:27 编辑
  1. #include <unistd.h>

  2. char msg[14] = "Hello, world!\n";
  3. #define len 14

  4. int main(void)
  5. {
  6.         write(1, msg, len);
  7.         _exit(0);
  8. }
复制代码
gcc -S hello.c
as -o hello.o hello.s
ld -o hello hello.o


ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
hello.o: In function `main':
hello.c:8: undefined reference to `write'
hello.c:9: undefined reference to `_exit'


gcc -S 产生的hello.s 是无法透过 as & ld  产生执行文件?
有办法让 gcc -S 产生的 xxx.s 透过 as & ld 产生执行文件?

谢谢

论坛徽章:
7
天秤座
日期:2014-08-07 13:56:30丑牛
日期:2014-08-27 20:34:21双鱼座
日期:2014-08-27 22:02:21天秤座
日期:2014-08-30 10:39:11双鱼座
日期:2014-09-21 20:07:532015年亚洲杯之日本
日期:2015-02-06 14:00:282015亚冠之大阪钢巴
日期:2015-11-02 14:50:19
2 [报告]
发表于 2015-08-27 09:33 |只看该作者
/usr/libexec/gcc/x86_64-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crtn.o

你少连接了一大堆东西,库和runtime,最少最少要lc吧
全程用gcc就可以了
gcc test.c -S -o test.s
gcc test.s -c test.o
gcc test.o -o test

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
3 [报告]
发表于 2015-08-27 10:29 |只看该作者
本帖最后由 shihyu 于 2015-08-27 10:42 编辑
  1. .data                                        # section declaration

  2. msg:
  3.         .ascii        "Hello, world!\n"        # our dear string
  4.         len = . - msg                        # length of our dear string

  5. .text                                        # section declaration

  6.                         # we must export the entry point to the ELF linker or
  7.     .global _start        # loader. They conventionally recognize _start as their
  8.                         # entry point. Use ld -e foo to override the default.

  9. _start:

  10. # write our string to stdout

  11.         movl        $len,%edx        # third argument: message length
  12.         movl        $msg,%ecx        # second argument: pointer to message to write
  13.         movl        $1,%ebx                # first argument: file handle (stdout)
  14.         movl        $4,%eax                # system call number (sys_write)
  15.         int        $0x80                # call kernel

  16. # and exit

  17.         movl        $0,%ebx                # first argument: exit code
  18.         movl        $1,%eax                # system call number (sys_exit)
  19.         int        $0x80                # call kernel
复制代码
$ as -o hello.o hello.s
$ ld -o hello hello.o
$ ./hello
Hello, world!

我好奇是有办法 gcc -S 反汇编后产生的产生的hello.s 是无法透过 as & ld  产生执行文件?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2015-08-27 11:05 |只看该作者
shihyu 发表于 2015-08-27 10:29
我好奇是有办法 gcc -S 反汇编后产生的产生的hello.s 是无法透过 as & ld  产生执行文件?


可以, 只是你没弄对而已. 用 gcc -v 看看 gcc 本身是怎么做的吧.

BTW, gcc -S 这个是生成汇编, 不是反汇编.

论坛徽章:
6
2015年辞旧岁徽章
日期:2015-03-05 16:13:092015年迎新春徽章
日期:2015-03-05 16:13:092015小元宵徽章
日期:2015-03-06 15:58:1815-16赛季CBA联赛之浙江
日期:2016-11-05 14:38:4115-16赛季CBA联赛之新疆
日期:2016-11-11 18:38:06
5 [报告]
发表于 2015-08-28 09:30 |只看该作者
如果代码里调用了C函数库的函数,那么链接时就得加上C函数库,链接过程比较复杂,一般由链接脚本控制,简单处理的话可以交给gcc包干。

不链接C函数库也是可以的,C函数库不是必须的,当然你代码里就不能再调用C函数库了,或者说得自己写出一个C函数库的实现,可以查询一下nostdlib选项的用法。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP