4

I want to add my C++ source code to the corresponding elf binary file and I'm looking for the best way to do this. (I'm using several versions of my code and not every version should be committed into svn).

Can I just append the source code without destroying the elf file using bash's >> operator? Or is objcopy --add-section a way to do this?

By the way, is there a better idea that just grep'ing all #include lines recursively from the source code files to determine ALL source code files that are used?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Manuel
  • 43
  • 1
  • 3
  • 1
    I'm not quite sure exactly what you're asking here. (a) Why are you trying to append *source code* to an *elf binary*? (b) What does this have to do with keeping versions in Subversion? (c) Even if you could insert source code with objcopy, what do you intend to do with it once it's there? (d) Are you trying to determine which source files get compiled into a particular elf binary? – Greg Hewgill Nov 30 '09 at 10:02
  • (a) because it's an scientific program and equations are modified slightly very often. So one must be able to comprehend which equation was used. (b) nothing really. I just wanted to mention that using svn does not work here to keep trace of the versions. (c) Then you can have a look which type of equations was used (d) because of (c) – Manuel Nov 30 '09 at 11:15
  • Did you find a suitable answer. It doesn't appear the listed answer works very well. – Lime Mar 01 '15 at 19:43

2 Answers2

2

You can get the output of the preprocessor from most C/C++ compilers by using the "-E" option on the command line, e.g.,

g++ -E my_file.c -o my_file_preproc.c

objcopy is an easy bet, but I ran across the ELF resource compiler today, and it might be useful to you. It allows you to embed anything you want in an ELF file, and will even generate C/C++ code to allow you to extra it. Thus, you could even create a library that your code could link with that would print out the source for the executable straight from the executable.

Which brings up another idea...rather than just include all the preprocessed source code, you could have the executable offer an option to print out the equation(s) that are used.

bandie
  • 173
  • 1
  • 11
Chris Cleeland
  • 4,760
  • 3
  • 26
  • 28
1

If you're using gcc you could use the -M flag to get a file with a list of all included files. It's written to the file specified with -o, like this:

gcc -M -c my_file.c -o list_of_included_files.txt
Puppe
  • 4,995
  • 26
  • 27