Add source code to elf file

和自甴很熟 提交于 2019-12-23 12:34:53

问题


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?


回答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



回答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.



来源:https://stackoverflow.com/questions/1818846/add-source-code-to-elf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!