Gcc 带有GNU ld的TDD(2.24 vs 2.26)

Gcc 带有GNU ld的TDD(2.24 vs 2.26),gcc,linker,ubuntu-14.04,ld,ubuntu-16.04,Gcc,Linker,Ubuntu 14.04,Ld,Ubuntu 16.04,我最近从Ubuntu14.04迁移到Ubuntu16.04来处理代码。这使我从GCC 4.8.4/GNU ld 2.24迁移到GCC 5.3.1/GNU ld 2.26 我一直在为代码编写测试用例,有人决定重写默认的time()函数进行测试,但这两个版本之间的链接行为似乎有所改变。示例代码如下: nothing.c: #include <stdio.h> #include <time.h> void nothing( ) { printf( "%lld\n", (

我最近从Ubuntu14.04迁移到Ubuntu16.04来处理代码。这使我从GCC 4.8.4/GNU ld 2.24迁移到GCC 5.3.1/GNU ld 2.26

我一直在为代码编写测试用例,有人决定重写默认的time()函数进行测试,但这两个版本之间的链接行为似乎有所改变。示例代码如下:

nothing.c:
#include <stdio.h>
#include <time.h>
void nothing( )
{
    printf( "%lld\n", (long long)time( NULL ) );
}

something.c:
#include <stdio.h>
#include <time.h>
extern void nothing( void );
int main( )
{
    printf( "%lld\n", (long long)time( NULL ) );
    nothing( );
    return 0;
}

faketime.c:
#include <time.h>
time_t time( time_t * ptr )
{
    return 42;
}
在尝试运行可执行文件的每个环境中,我都会得到两个不同的结果

Ubuntu 14.04说答案是“42/42”,而Ubuntu 16.04说答案是“42/1464287587”(插入当前的Unix时间戳)

我很好奇这是否是一种有意识的改变,或者我是否做错了什么?我有点认为“新方法”实际上更正确,因为它似乎是一个漏洞,让我覆盖一个库希望进行的系统调用。然而,也许这是正常的,也许现在这种“链接器替换”的方法在新的编译器/链接器中被破坏了

gcc -fPIC -c -o nothing.lo nothing.c
gcc -shared -rdynamic -o libnothing.so nothing.lo
gcc -o something something.c faketime.c -Wl,-rpath=. -L. -lnothing