RuntimeError: relocation R_X86_64_32S against `.bss' can not be used when making a shared object in _linux_shared

I follow setup and installation steps according to
https://docs.tvm.ai/install/from_source.html

Outline

  • System
  • config.cmake
  • Related code
  • Similar errors

System

Linux x86-64

4.15.0-43-generic

Ubuntu 16.04

config.cmake

My config.cmake is

set(USE_CUDA /usr/local/cuda-10.0)
...
set(USE_LLVM /home/wxf/llvm_download/clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04/bin/llvm-config)
#--------------------------------------------------------------------
#  Template custom cmake configuration for compiling
#
#  This file is used to override the build options in build.
#  If you want to change the configuration, please use the following
#  steps. Assume you are on the root directory. First copy the this
#  file so that any local changes will be ignored by git
#
#  $ mkdir build
#  $ cp cmake/config.cmake build
#
#  Next modify the according entries, and then compile by
#
#  $ cd build
#  $ cmake ..
#
#  Then buld in parallel with 8 threads
#
#  $ make -j8
#--------------------------------------------------------------------

#---------------------------------------------
# Backend runtimes.
#---------------------------------------------

# Whether enable CUDA during compile,
#
# Possible values:
# - ON: enable CUDA with cmake's auto search
# - OFF: disbale CUDA
# - /path/to/cuda: use specific path to cuda toolkit
set(USE_CUDA /usr/local/cuda-10.0)

# Whether enable ROCM runtime
#
# Possible values:
# - ON: enable ROCM with cmake's auto search
# - OFF: disbale ROCM
# - /path/to/rocm: use specific path to rocm
set(USE_ROCM OFF)

# Whether enable SDAccel runtime
set(USE_SDACCEL OFF)

# Whether enable Intel FPGA SDK for OpenCL (AOCL) runtime
set(USE_AOCL OFF)

# Whether enable OpenCL runtime
set(USE_OPENCL OFF)

# Whether enable Metal runtime
set(USE_METAL OFF)

# Whether enable Vulkan runtime
#
# Possible values:
# - ON: enable Vulkan with cmake's auto search
# - OFF: disbale vulkan
# - /path/to/vulkan-sdk: use specific path to vulkan-sdk
set(USE_VULKAN OFF)

# Whether enable OpenGL runtime
set(USE_OPENGL OFF)

# Whether to enable SGX runtime
#
# Possible values for USE_SGX:
# - /path/to/sgxsdk: path to Intel SGX SDK
# - OFF: disable SGX
#
# SGX_MODE := HW|SIM
set(USE_SGX OFF)
set(SGX_MODE "SIM")
set(RUST_SGX_SDK "/path/to/rust-sgx-sdk")

# Whether enable RPC runtime
set(USE_RPC ON)

# Whether embed stackvm into the runtime
set(USE_STACKVM_RUNTIME OFF)

# Whether enable tiny embedded graph runtime.
set(USE_GRAPH_RUNTIME ON)

# Whether enable additional graph debug functions
set(USE_GRAPH_RUNTIME_DEBUG OFF)

# Whether build with LLVM support
# Requires LLVM version >= 4.0
#
# Possible values:
# - ON: enable llvm with cmake's find search
# - OFF: disbale llvm
# - /path/to/llvm-config: enable specific LLVM when multiple llvm-dev is available.
set(USE_LLVM /home/wxf/llvm_download/clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04/bin/llvm-config)

#---------------------------------------------
# Contrib libraries
#---------------------------------------------
# Whether use BLAS, choices: openblas, mkl, atlas, apple
set(USE_BLAS none)

# /path/to/mkl: mkl root path when use mkl blas library
# set(USE_MKL_PATH /opt/intel/mkl) for UNIX
# set(USE_MKL_PATH ../IntelSWTools/compilers_and_libraries_2018/windows/mkl) for WIN32
set(USE_MKL_PATH none)

# Whether use contrib.random in runtime
set(USE_RANDOM OFF)

# Whether use NNPack
set(USE_NNPACK OFF)

# Whether use CuDNN
set(USE_CUDNN OFF)

# Whether use cuBLAS
set(USE_CUBLAS OFF)

# Whether use MIOpen
set(USE_MIOPEN OFF)

# Whether use MPS
set(USE_MPS OFF)

# Whether use rocBlas
set(USE_ROCBLAS OFF)

# Whether use contrib sort
set(USE_SORT OFF)

# Build ANTLR parser for Relay text format
set(USE_ANTLR OFF)

errors

When I run a simple test from:

https://docs.tvm.ai/tutorials/nnvm_quick_start.html#sphx-glr-tutorials-nnvm-quick-start-py

I get some errors in the end:

Symbol Outputs:
	output[0]=softmax(0)
Variable:data
Variable:bn_data_gamma
Variable:bn_data_beta
Variable:bn_data_moving_mean
Variable:bn_data_moving_var
--------------------
Op:batch_norm, Name=bn_data
Inputs:
	arg[0]=data(0) version=0
	arg[1]=bn_data_gamma(0) version=0
	arg[2]=bn_data_beta(0) version=0
	arg[3]=bn_data_moving_mean(0) version=1
	arg[4]=bn_data_moving_var(0) version=1
Attrs:
	epsilon=2e-05
	scale=False
Variable:conv0_weight
--------------------
...                           # normal output 
--------------------
Op:dense, Name=fc1
Inputs:
	arg[0]=flatten0(0)
	arg[1]=fc1_weight(0) version=0
	arg[2]=fc1_bias(0) version=0
Attrs:
	units=1000
--------------------
Op:softmax, Name=softmax
Inputs:
	arg[0]=fc1(0)

[0.00089283 0.00103331 0.0009094  0.00102275 0.00108751 0.00106737
 0.00106262 0.00095838 0.00110792 0.00113151]



['deploy_graph.json', 'deploy_lib.tar', 'deploy_param.params']
Traceback (most recent call last):
  File "00_nnvm_quick_start.py", line 132, in <module>
    loaded_lib = tvm.module.load(path_lib)
  File "/home/wxf/tvm_prj/tvm/python/tvm/module.py", line 241, in load
    _cc.create_shared(path + ".so", files)
  File "/home/wxf/tvm_prj/tvm/python/tvm/contrib/cc.py", line 33, in create_shared
    _linux_shared(output, objects, options, cc)
  File "/home/wxf/tvm_prj/tvm/python/tvm/contrib/cc.py", line 58, in _linux_shared
    raise RuntimeError(msg)
RuntimeError: Compilation error:
/usr/bin/ld: /tmp/tmpqgibc8e4/lib.o: relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
/tmp/tmpqgibc8e4/lib.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status


Related code

Related code is in /tvm/python/tvm/contrib/cc.py

https://docs.python.org/3/library/subprocess.html#popen-constructor

...
 12 def create_shared(output,
 13                   objects,
 14                   options=None,
 15                   cc="g++"):
 16     """Create shared library.
 17  
 18     Parameters
 19     ----------
 20     output : str
 21         The target shared library.
 22  
 23     objects : list
 24         List of object files.
 25  
 26     options : list
 27         The list of additional options string.
 28  
 29     cc : str, optional
 30         The compile string.
 31     """
 32     if sys.platform == "darwin" or sys.platform.startswith('linux'):
 33         _linux_shared(output, objects, options, cc)
 34     elif sys.platform == "win32":
 35         _windows_shared(output, objects, options)
 36     else:
 37         raise ValueError("Unsupported platform")
...
 40 def _linux_shared(output, objects, options, cc="g++"):
 41     cmd = [cc]
 42     cmd += ["-shared", "-fPIC"]
 43     if sys.platform == "darwin":
 44         cmd += ["-undefined", "dynamic_lookup"]
 45     cmd += ["-o", output]
 46     if isinstance(objects, str):
 47         cmd += [objects]
 48     else:
 49         cmd += objects
 50     if options:
 51         cmd += options
 52     proc = subprocess.Popen(
 53         cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 54     (out, _) = proc.communicate()
 55     if proc.returncode != 0:
 56         msg = "Compilation error:\n"
 57         msg += py_str(out)
 58         raise RuntimeError(msg)

CAUSED BY:

(out, _) = proc.communicate()
RuntimeError: Compilation error:
/usr/bin/ld: /tmp/tmpwl0pygoi/lib.o: relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
/tmp/tmpwl0pygoi/lib.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

Similar situations in other tutorial examples

When I run another tutorial example, I also get similar error information.
https://docs.tvm.ai/tutorials/autotvm/tune_nnvm_cuda.html

DEBUG:autotvm:No: 217	GFLOPS: 0.00/0.00	result: MeasureResult(costs=(RuntimeError('Except caught from RPC call: TVMCall CFunc Error:

Traceback (most recent call last):

  File "/home/wxf/tvm_prj/tvm/python/tvm/_ffi/_ctypes/function.py", line 55, in cfun

    rv = local_pyfunc(*pyargs)

  File "/home/wxf/tvm_prj/tvm/python/tvm/rpc/server.py", line 50, in load_module

    m = _load_module(path)

  File "/home/wxf/tvm_prj/tvm/python/tvm/module.py", line 241, in load

    _cc.create_shared(path + ".so", files)

  File "/home/wxf/tvm_prj/tvm/python/tvm/contrib/cc.py", line 33, in create_shared

    _linux_shared(output, objects, options, cc)

  File "/home/wxf/tvm_prj/tvm/python/tvm/contrib/cc.py", line 58, in _linux_shared

    raise RuntimeError(msg)

RuntimeError: Compilation error:

/usr/bin/ld: /tmp/tmpgqcx3hzt/lib.o: relocation R_X86_64_32S against `.bss\' can not be used when making a shared object; recompile with -fPIC

/tmp/tmpgqcx3hzt/lib.o: error adding symbols: Bad value

collect2: error: ld returned 1 exit status



',),), error_no=4, all_cost=7.192363262176514, timestamp=1547500859.3370917)	[('tile_b', [16, 1, 1, 1]), ('tile_y', [1, 128, 2, 2]), ('tile_x', [2, 1, 2, 4]), ('tile_rc', [64, 8]), ('auto_unroll_max_step', 1500), ('unroll_explicit', 0)],winograd,None,183331

Another example code shows error:

<class 'tvm.tensor.Tensor'>
-----GPU code-----
extern "C" __global__ void myadd_kernel0( float* __restrict__ C,  float* __restrict__ A,  float* __restrict__ B, int n) {
  if (((int)blockIdx.x) < (n / 64)) {
    C[((((int)blockIdx.x) * 64) + ((int)threadIdx.x))] = (A[((((int)blockIdx.x) * 64) + ((int)threadIdx.x))] + B[((((int)blockIdx.x) * 64) + ((int)threadIdx.x))]);
  } else {
    if ((((int)blockIdx.x) * 64) < (n - ((int)threadIdx.x))) {
      C[((((int)blockIdx.x) * 64) + ((int)threadIdx.x))] = (A[((((int)blockIdx.x) * 64) + ((int)threadIdx.x))] + B[((((int)blockIdx.x) * 64) + ((int)threadIdx.x))]);
    }
  }
}


Traceback (most recent call last):
  File "01_get_started.py", line 214, in <module>
    cc.create_shared(temp.relpath("myadd.so"), [temp.relpath("myadd.o")])
  File "/home/wxf/tvm_prj/tvm/python/tvm/contrib/cc.py", line 33, in create_shared
    _linux_shared(output, objects, options, cc)
  File "/home/wxf/tvm_prj/tvm/python/tvm/contrib/cc.py", line 58, in _linux_shared
    raise RuntimeError(msg)
RuntimeError: Compilation error:
/usr/bin/ld: /tmp/tmpfijjmtj5/myadd.o: relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
/tmp/tmpfijjmtj5/myadd.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

The hint shows recompile with -fPIC, but I think the Makefile has already set the -fPIC flag.

How to fix the problem? Thanks in advance!

Suggest to use a bit latest Ubuntu if possible.

How about gcc version (try with GCC-5.x)?

@shizukanaskytree have you solved this problem?

I recently encounter the same problem as you post.

I just follow the tutorial:
1.https://docs.tvm.ai/tutorials/nnvm_quick_start.html#sphx-glr-tutorials-nnvm-quick-start-py
2.https://docs.tvm.ai/tutorials/get_started.html#sphx-glr-tutorials-get-started-py

everything goes well until I execute this line code "cc.create_shared(temp.relpath(“myadd.so”), [temp.relpath(“myadd.o”)]) " ,it seems produce the .so, link error:

“/usr/bin/x86_64-linux-gnu-ld: /tmp/tmphk243coy/myadd.o: relocation R_X86_64_32S against `.bss’ can not be used when making a shared object; recompile with -fPIC”

but in ‘create_shared’ function it already have default ‘-fPIC’ optional.

System:
ubuntu 18.04
cuda-9.0
gcc version 6.5.0
LLVM 7.0.0(Pre-Built Binaries:Ubuntu 16.04 (.sig) )
cmake version 3.10.2

as @srkreddy1238 suggested, I will try gcc-g.x later.

@shizukanaskytree this is interesting error, because myadd.o generates from assembler generated by llvm from tvm with position independent code

But it in your case, it seems that, the code were generated with llvm::Reloc::Static.
Could you check the generated code, in case pic code all calls should go through plt https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/feowg/index.html

print(fadd.get_source("asm"))



python get_started.py > dump.s
grep GOTPCREL dump.s | wc -l

or yor can save directrly myadd.o

readelf -r myadd.o | grep GOTPCREL | wc -l

I have solved this problem.Those are my steps:

tvm install from source ,you must notice LLVM version.

“Since LLVM takes long time to build from source, you can download pre-built version of LLVM from LLVM Download Page.”

when you download pre-built version of LLVM,you should download LLVM correspond to you local environment of gcc and g++ version.

For example,I used download the LLVM 7.0.0,but my local ubuntu18.04 is gcc 6.x and g++6.x, so I got this error.However,when I changed to LLVM 6.0.0, everything goes well.

System:
ubuntu 18.04
cuda-9.0
gcc version 6.5.0
g++ version 6.5.0
LLVM 6.0.0(Pre-Built Binaries:Ubuntu 16.04 (.sig) )
cmake version 3.10.2

good luck.

I find the solution from this post.

CMAKE_C_COMPILER
CMAKE_CXX_COMPILER

need to be explicitly set.