[llvm-dev] C++ program crashes while build with compiler-rt and static-pie.

50 views
Skip to first unread message

Youren Shen via llvm-dev

unread,
Dec 17, 2019, 12:40:31 AM12/17/19
to llvm-dev
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.

--
Best Regards.
Youren Shen.
download_and_build_toolchain.sh
Reply all
Reply to author
Forward
0 new messages