Sama Azari
Sama Azari

Reputation: 489

compile multiple C codes to one single assembly file

I want to compile multiple C files to a single assembly file (they are interconnected and when you compile it you will have a binary file). Is it possible in GCC?

For example take a look at this repo. The make file will generate a single file (a binary one).

However I want to compile them to assembly, instrument the code and then compile them to binary. Obviously it will be easier for me if I have all the assembly files in a single *.s file.

Any idea?

Upvotes: 2

Views: 1165

Answers (2)

fuz
fuz

Reputation: 93172

I see three approaches:

  • Use cat to concatenate the assembly files. This should work unless two or more translation units use a static variable with the same name. This might be problematic if labels are used twice. You can preprocess the individal files with a sed-script like this to make the labels unique:

    s/\.L\([[:alnum:]]*\)/.L$ident\1/
    

    where $ident is a unique string for each assembly file. This turns .Lfoo into .Lidentfoo.

  • Make a C source file that looks like this and compile it, same caveat as before:

    #include "module1.c"
    #include "module2.c"
    ...
    #include "modulen.c"
    
  • Use ld -r to perform a partial link on a set of object files. This gives you one large object file containing the content of the other files. You can then use a tool like objconv to disassemble the object file, instrument it and reassemble. Note that this might not what you need.

Upvotes: 7

Sergio
Sergio

Reputation: 8209

You can try to glue all sources together with Amalgamate and then generate assembly output.

Upvotes: 2

Related Questions