2

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?

23
  • 5
    #include all those files into a single .c file and compile it Commented Jul 5, 2016 at 14:30
  • 3
    Smell like XY problem Commented Jul 5, 2016 at 14:30
  • 1
    @mvidelgauz It won't work without modifications in most cases. Commented Jul 5, 2016 at 14:31
  • 2
    So... What do you really need to do? Commented Jul 5, 2016 at 14:32
  • 2
    @SamaAzari We are as gentle as possible. But it is likely that you ask from us to help you solve a problem you don't really have. Commented Jul 5, 2016 at 14:51

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.