如何生成C源$ C ​​$ C一NASM编译装配code在Linux上? [英] How to generate a nasm compilable assembly code from c source code on Linux?

查看:135
本文介绍了如何生成C源$ C ​​$ C一NASM编译装配code在Linux上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试平台是32位Linux。

Test platform is 32 bit Linux.

基本上,我知道GCC可以用来生成英特尔和AT& T公司的风格
装配code,但似乎您无法直接使用NASM / TASM编译
英特尔的风格组装code GCC产生的。

Basically, I know gcc can be used to generate both Intel and At&T style assembly code, but it seems that you can not directly use nasm/tasm to compile the Intel style assembly code gcc generated.

我进行关于Windows和Linux平台项目分析ASM code,所以我想,如果他们可以通过像NASM \\ YASM独立于平台的汇编编译两个,我能有一个更容易的时间...

I am conducting a project analysis asm code on both windows and Linux platform, so I am thinking if they can be both compiled by platform independent assembler like nasm\yasm, I could have a much easier time...

所以我的问题是如何产生的C源$ C ​​$ C一NASM编译装配code在Linux上?

So my question is how to generate a nasm compilable assembly code from c source code on Linux?

推荐答案

我觉得这是一个更好的方法来分解目标文件,而不是由GCC生成使用汇编code。

I find it's a better approach to disassemble the object files rather than use assembly code generated by gcc.


  1. 首先,从源$ C ​​$ C生成目标文件:

  1. First, generate an object file from your source code:

gcc -fno-asynchronous-unwind-tables -s -c -o main.o main.c

-fno异步退绕桌:不会产生像 .eh_frame

-s :让更小的可执行文件(带)

-s: make smaller executable (strip)

-c -o :生成一个名为目标文件 main.o中

-c -o: generate an object file called main.o

使用 objconv 生成 NASM code:

objconv -fnasm main.o

结果将被存储在 main.asm中

结果将非常接近NASM语法。然而,你可能需要做一些小的调整,eliminiate警告/错误。简单地尝试与NASM编译它

The result will be very close to Nasm syntax. However you might need to make some minor tweaks to eliminiate warnings/errors. Simply try to compile it with Nasm

nasm -f elf32 main.asm

和手工修复错误/警告。例如:

and fix the errors/warnings by hand. For example:


  • 删除对齐= N 执行 / NOEXECUTE .SECTION 行字。

  • 删除文本:函数全球声明

  • 删除默认REL

  • 删除空的部分,如果你想等

  • remove the align=N and execute/noexecute words from .SECTION lines.
  • remove the text : function from global declarations
  • remove the default rel line
  • remove empty sections if you wish etc

链接即可 main.o中它通过NASM使用gcc产生的第3步:

Link the resulting main.o which generated by Nasm in step 3 using gcc:

gcc main.o

您还可以链接使用LD它,但它是更难。

You can also link it using ld but it's much harder.

这篇关于如何生成C源$ C ​​$ C一NASM编译装配code在Linux上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆