[cfe-dev] Why clang may not be compatible with gcc for .a file generation?

Peng Yu via cfe-dev cfe-dev at lists.llvm.org
Wed Jan 23 19:00:20 PST 2019


Hi,

I have the following trace.c file compiled with the following command
with clang.

$ clang -Wall -pedantic -c -o trace.o trace.c
$ ar cr  libtrace.a trace.o
$ ranlib libtrace.a

Then, I run configure from a GNU package, which gives the following
error. It asks to recompile the .a with -fPIC. Is it because gcc and
clang has different default on whether -fPIC is enabled? Could anybody
help explain why there is such a difference? Thanks.

configure:3772: gcc -g -finstrument-functions
-L/root/linux/bin/wrappercomposite/src/xplat/llvmxplat/src/mkvar/bin/Linux
conftest.c -ltrace >&5
/usr/bin/ld: /root/linux/bin/wrappercomposite/src/xplat/llvmxplat/src/mkvar/bin/Linux/libtrace.a(trace.o):
relocation R_X86_64_32S against `.bss' can not be used when making a
PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output

//trace.c
#include <stdio.h>
#include <stdlib.h>
//#include <time.h>

static FILE *fp_trace;

void __attribute__ ((constructor)) trace_begin (void) {
    const char *s=getenv("TRACE_OUTFILE");
    if(s) {
        fp_trace = fopen(s, "w");
    } else {
        fputs(
                "\x1b[31;1mThe environment variable TRACE_OUTFILE must
be set.\x1b[m\n"
                , stderr
                );
        exit(1);
    }
}

void __attribute__ ((destructor)) trace_end (void) {
    if(fp_trace != NULL) {
        fclose(fp_trace);
    }
}

void __cyg_profile_func_enter (void *func,  void *caller) {
    if(fp_trace != NULL) {
        //fprintf(fp_trace, "e %p %p %lu\n", func, caller, time(NULL) );
        fprintf(fp_trace, "%p\t%p\n", func, caller);
    }
}

void __cyg_profile_func_exit (void *func, void *caller) { }


-- 
Regards,
Peng



More information about the cfe-dev mailing list