1

I am using LLVM's LLD8 to replace GNU-LD to improve link speed. But when I make .so file, there is a strange error in the release mode. After streamlining, I reproduced this problem with a simple example

test.h:

#ifndef TEST_H
#define TEST_H

inline int func(){
  static __thread int i = 1;
  return i;
}

void handle();

#endif

test.cpp

#include <iostream>
#include <stdio.h>
#include "test.h"

void handle() {
  std::cout << func() << std::endl;
}

main.cpp

#include "test.h"

void x(){
  handle();
}

following cmds copied from make V=1 stdout in my project, i save those cmds into a script and compile my example file

compile lib:

$dpath/bin/g++ -DLT_OBJDIR=\".libs/\" -I. -I$dpath/include/ -w -std=gnu++11 -g -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_NO_EXCEPTION -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -Wall -Werror -Wextra -Wunused-parameter -Wformat -Wconversion -Wno-deprecated -Wno-invalid-offsetof -finline-functions -fno-strict-aliasing -mtune=core2 -Wno-psabi -Wno-sign-compare -Wno-literal-suffix -DGCC_52 -DUSE_POSIX_FALLOCATE -DSUPPORT_SSE4_2 -DHAVE_SCHED_GETCPU -DHAVE_REALTIME_COARSE -DHAVE_FALLOCATE -c test.cpp  -fPIC -DPIC -o .libs/test.o

link lib

ar cru .libs/lib.a .libs/test.o
ranlib .libs/lib.a

compile files

$dpath/bin/g++ -DLT_OBJDIR=\".libs/\" -I. -I$dpath/include -std=gnu++11 -g -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_NO_EXCEPTION -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -Wall -Werror -Wextra -Wunused-parameter -Wformat -Wconversion -Wno-deprecated -Wno-invalid-offsetof -finline-functions -fno-strict-aliasing -mtune=core2 -Wno-psabi -Wno-sign-compare -Wno-literal-suffix -DGCC_52 -DUSE_POSIX_FALLOCATE -DSUPPORT_SSE4_2 -DHAVE_SCHED_GETCPU -DHAVE_REALTIME_COARSE -DHAVE_FALLOCATE -c main.cpp  -fPIC -DPIC -o .libs/main.o

link so

$dpath/bin/ld.lld -shared -nostdlib .libs/main.o -l:lib.a -L./.libs -L/usr/lib64 -L$glib/gcc/x86_64-unknown-linux-gnu/5.2.0 -L$glib/gcc -L$glib/../lib64 -L$glib -L$dpath/lib/libstdc++.so --allow-shlib-undefined -soname server.so.0 -o ./server.so.0.0.0

finally i got this error

ld.lld: error: can't create dynamic relocation R_X86_64_DTPOFF32 against symbol: func()::i in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in ./.libs/lib.a(test.o)
>>> referenced by test.cpp:6
>>>               test.o:(handle()) in archive ./.libs/lib.a

as you seen, I have already pass -fPIC in my cmds, so i pass -znotext to lld but i got another error

ld.lld: error: relocation R_X86_64_DTPOFF32 cannot be used against symbol func()::i; recompile with -fPIC
>>> defined in ./.libs/lib.a(test.o)
>>> referenced by test.cpp:6`enter code here`
>>>               test.o:(handle()) in archive ./.libs/lib.a

This error only reported when I compile my project in release mode, when i remove -O2, it links well. But when i use the so/bin which linked in debug mode to run testcases, i got a lot of unexcept core dumps around codes which use function scoped static tls.

CChip
  • 11
  • 3

0 Answers0