C++ program crashes while build with compiler-rt and static-pie.

Hello.

I’m working on building a non-gun toolchain with musl, clang, llvm, libunwind, libcxx, libcxxabi and compiler-rt for our project. While static and pie compilation are very useful in our project, C++ programs crash while running with the crtbegin from the compiler-rt in static-pie compilation.

I tested different combinations on both c program and c++ program: static-pie with/without compiler-rt and pie with/without compiler-rt. While all other tests passed but only C++ program crashed when enabling static-pie and compiler-rt.
To figured out why it crashes, I used GDB and it shows that the program crashed because at the _dlstart_c function in musl, rel[0] in line 140 is an address out of address space.

136 rel = (void *)(base+dyn[DT_RELA]);
137 rel_size = dyn[DT_RELASZ];
138 for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
139 if (!IS_RELATIVE(rel[1], 0)) continue;
140 size_t *rel_addr = (void *)(base + rel[0]);
141 *rel_addr = base + rel[2];
142 }

Here[1] is my script to build a toolchain based on musl and llvm for ubuntu 16.04(I also attached it below). It will install the toolchain into /usr/local/occlum/.
To compiler a c++ program, use : musl-clang hello.cc -I /usr/local/occlum/include/c++/v1/ -static-pie -lc++ -lc++abi -fPIC -o hello --rtlib=compiler-rt

Is it looks like a bug in llvm or compiler-rt? Or did I make some stupid mistake in my script?

Im really appreciate for any response.

[1]. https://gist.github.com/Yourens/0794bfad63bc54e5890d5ee828120ebf

download_and_build_toolchain.sh (7.21 KB)