简体   繁体   English

从linux中的C文件生成汇编代码

[英]Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. 我想知道如何使用Unix从C程序生成汇编代码。 I tried the gcc: gcc -c file.c 我试过gcc: gcc -c file.c

I also used firstly cpp and then try as but I'm getting errors. 我也首先使用cpp然后尝试as但我收到错误。

I'm trying to build an assembler program from 3 different programs 我正在尝试从3个不同的程序构建汇编程序

prog1.c prog2.c prog.h prog1.c prog2.c prog.h

Is it correct to do gcc -S prog1.c prog2.c prog.h ? gcc -S prog1.c prog2.c prog.h是否正确? Seems that is not correct. 似乎不正确。 I don't know if I have to generate the assembler from each of them and then link them 我不知道是否必须从它们中生成汇编程序然后将它们链接起来

Thanks 谢谢

According the manual: 根据手册:

`-S'
     Stop after the stage of compilation proper; do not assemble.  The
     output is in the form of an assembler code file for each
     non-assembler input file specified.

     By default, the assembler file name for a source file is made by
     replacing the suffix `.c', `.i', etc., with `.s'.

     Input files that don't require compilation are ignored.

so try gcc -S file.c . 所以试试gcc -S file.c

From man gcc : 来自man gcc

   -S     Stop  after the stage of compilation proper; do not
          assemble.  The output is an assembler code file for
          each non-assembler input file specified.

          By default, GCC makes the assembler file name for a
          source file by replacing  the  suffix  `.c',  `.i',
          etc., with `.s'.  Use -o to select another name.

          GCC ignores any input files that don't require com-
          pilation.

If you're using gcc (as it seems) it's gcc -S. 如果你正在使用gcc(看起来像)它是gcc -S。

Don't forget to specify the include paths with -I if needed. 如果需要,请不要忘记使用-I指定包含路径。

gcc -I ../my_includes -S my_file.c

and you'll get my_file.s with the Assembler instructions. 并且您将使用Assembler指令获取my_file.s。

objdump -d also works very nicely, and will give you the assembly listing for the whole binary (exe or shared lib). objdump -d也很好用,并且会给你整个二进制文件(exe或共享库)的汇编列表。

This can be a lot clearer than using the compiler generated asm since calls to functions within the same source file can show up not yet resolved to their final locations. 这比使用编译器生成的asm要清楚得多,因为对同一源文件中的函数的调用可能会显示尚未解析到它们的最终位置。

Build your code with -g and you can also add --line and/or --source to the objdump flags. 使用-g构建代码,您还可以将-line和/或--source添加到objdump标志中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM