简体   繁体   English

如何检查linux二进制文件是否具有新添加的代码

[英]How to check if the linux binary file has newly added code or not

I have a certain a project consisting of multiple *.cpp files which I compile using g++. 我有一个由多个使用g ++编译的*.cpp文件组成的项目。 I changed one source file to add some code. 我更改了一个源文件以添加一些代码。 Built the project again . 再次建立该项目。 Just to check I did 只是检查我做了

diff -s origbinaryfile changedbinaryfile

It showed me that files are identical, which was strange as I have added a extra code and was hoping to see a difference in the generated linux binaries. 它告诉我文件是相同的,这很奇怪,因为我添加了额外的代码,并希望看到生成的linux二进制文件之间的差异。

I have added a code which is a extra case statement in a existing switch case code .eg Earlier I had 我在现有的switch case代码中添加了一个额外的案例声明代码。例如,我之前有

switch(x) {
case DEV1:
 ...
case DEV2:
 ...

Changed one is 改一个是

switch(x) {
    case DEV1:
     ...
    case DEV2:
     ...
    case DEV3:

DEV1 , DEV2 , DEV3 are enumerators of a enum declaration. DEV1DEV2DEV3是一个enum声明的枚举。

How can I verify using nm command or any other command to see if that added code is really present in the binary? 如何使用nm命令或其他任何命令验证二进制文件中是否确实存在所添加的代码?

Any other way? 还有其他方法吗?

[It might sound silly but I did it carefully to make sure I was not making any error in compiling] [听起来可能很愚蠢,但是我仔细地做以确保在编译时没有出现任何错误]

You can not. 你不能。
Of course you can disassemble your files and compare asm code, but it's so heavily optimized by compiler that you'll hardly get any meaningful results by such method. 当然,您可以分解文件并比较asm代码,但是编译器对它进行了很大的优化,以至于您几乎无法通过这种方法获得任何有意义的结果。

Alternatively you can compare your files using cmp command, it's intended for binary files and will likely be more adequate than diff . 或者,您可以使用cmp命令比较文件,该命令适用于二进制文件,可能比diff更合适。

However binary difference is not 100% guarantee that any code was actually changed. 但是,二进制差异并不是100%保证实际上已更改任何代码。 Compilers usually add debug info and compile timestamps, so same code compiled twice in a row will result in two binaries that have different bytes. 编译器通常会添加调试信息并编译时间戳,因此同一代码连续编译两次将导致两个二进制文件具有不同的字节。

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

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