简体   繁体   English

为什么C程序针对C ++库编译并与C编译器链接,然后在运行时进行SIGILL?

[英]Why would a C program compile and link with a C compiler against C++ libraries then SIGILL at runtime?

I recently had a problem with a 3rd party plugin that I wrote on IBM AIX. 最近,我在IBM AIX上编写的3rd party插件出现问题。 It took considerable time to track down. 跟踪花费了大量时间。 The plugin was in the form of a C executable. 该插件采用C可执行文件的形式。 The executable was compiled against 3rd party libraries. 该可执行文件是根据第三方库进行编译的。 A new version of those libraries was provided for a mandatory upgrade. 提供了这些库的新版本以进行强制升级。

When compiled and linked with the IBM C compiler 12.1 my existing code would produce a binary that would work if the timezone was set using OLSON and crash with SIGILL and no backtrace available if the timezone was set using POSIX. 与IBM C编译器12.1编译并链接后,如果使用OLSON设置时区,并且SIGILL崩溃,则现有代码将生成一个二进制文件,如果使用POSIX设置时区,则二进制文件将不可用。

I was able to track down the crash to a call to the 3rd party API fairly quickly using debug printfs to a log file and flushing the log file. 使用调试printfs到日志文件并刷新日志文件,我能够相当迅速地跟踪到对第三方API调用的崩溃。 But it took some time for the 3rd party provider to mention that the new version of the API introduced the use of C++ libraries in their own code. 但是第三方供应商花了一些时间才提到新版本的API在其自己的代码中引入了C ++库的使用。

The problem was resolved by compiling with the IBM C compiler but linking with the IBM C++ linker (xlC). 通过使用IBM C编译器进行编译,但与IBM C ++链接器(xlC)链接,解决了该问题。

So my questions are: 所以我的问题是:

1) Why didn't the C linker fail to produce a valid executable? 1)为什么C链接器没有生成有效的可执行文件?

A semi-valid binary must have been produced or the code would not have worked under OLSON timezone. 必须生成了半有效的二进制文件,否则代码将在OLSON时区下无法正常工作。 That means all the symbols were present and any name mangling was handled (though possibly not correctly) 这意味着所有符号都存在,并且处理了所有名称更改(尽管可能不正确)

2) How do I determine if the binary produced by compiler and linker is valid? 2)如何确定编译器和链接器生成的二进制文件是否有效?

The only way I can think to do this is to fully excercise the code as much as possible with unit testing. 我认为做到这一点的唯一方法是在单元测试中尽可能地充分执行代码。

3) How do I prevent similar from occuring for different code? 3)如何防止不同代码出现类似情况?

Should I always link with a C++ linker? 我应该始终与C ++链接程序链接吗? That doesn't seem right to me. 这对我来说似乎不对。

I apologize that I can't post code, but I am not at liberty to do this. 抱歉,我无法发布代码,但我不能随意这样做。

Zsigmond has a lot of good statements. Zsigmond有很多不错的陈述。 We need at least to see the link line and its options to help you much. 我们至少需要查看链接线及其选项,对您有很大帮助。 Very often, open source links with "ignore unresolved symbols" because they don't understand the import files. 带有“忽略未解析的符号”的开源链接通常不理解,因为它们不理解导入文件。 So, the link will succeed in producing an utterly useless binary -- cause that is why it was asked to do. 因此,链接将成功生成完全无用的二进制文件-原因就是为什么要这样做。

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

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