Hi,
I was trying to compile a buildroot-2019.02.4 system for an aarch64
platform using gcc 8.3.0 and uClibc-ng-1.0.31. Everything seemed to boot
fine, but when I added the ntpd daemon, the daemon kept segfaulting.
Some debugging showed, that the segfault happened inside the res_init
function, so i made the following test program:
----test_res_init.c----
#include <resolv.h>
void main(void)
{
res_init();
}
-----------------------
This program always segfaults as soon as res_init tries to access the
_res structure. Some more debugging revealed, that there seems to be a
general problem with accessing thread local storage from a dynamicly
linked library, so I wrote the following test code:
----test_lib.c----
#include <stdio.h>
int ii;
__thread int *iip = ⅈ
void print_ptrs(void)
{
printf("print_ptrs:\n");
printf("&iip = %p\n", &iip);
printf("&ii = %p\n", &ii);
printf("iip = %p\n", iip);
}
----test_tls.c----
#include <stdio.h>
void print_ptrs(void);
extern int ii;
extern __thread int *iip;
void main(void)
{
printf("main:\n");
printf("&iip = %p\n", &iip);
printf("&ii = %p\n", &ii);
printf("iip = %p\n", iip);
print_ptrs();
}
------------------
When compiling the source linke this:
# aarch64-linux-gcc -g -fPIC -c -o test_lib.o test_lib.c
# aarch64-linux-gcc -shared -o test_lib.so test_lib.o
# aarch64-linux-gcc -g test_tls.c test_lib.so -o test_tls
the result looks like this:
-------------------------------
# LD_LIBRARY_PATH=/root /root/test_tls
main:
&iip = 0x7fa0d756d0
&ii = 0x411048
iip = 0x411048
print_ptrs:
&iip = 0xff41ac4d90
&ii = 0x411048
[ 7654.009770] test_tls[1942]: unhandled level 0 translation fault (11)
at 0xff41ac4d90, esr 0x92000004
[ 7654.018844] pgd = ffffffc07d48c000
[ 7654.027775] [ff41ac4d90] *pgd=0000000000000000
[ 7654.036007] , *pud=0000000000000000
[ 7654.039488]
[ 7654.040951]
[ 7654.042441] CPU: 2 PID: 1942 Comm: test_tls Not tainted
4.9.0-g196f33d0-dirty #6
[ 7654.049820] Hardware name: xlnx,zynqmp (DT)
[ 7654.053981] task: ffffffc05e71ad00 task.stack: ffffffc05d9d4000
[ 7654.059884] PC is at 0x7fa0d4f8f4
[ 7654.063186] LR is at 0x7fa0d4f8ec
[ 7654.066478] pc : [<0000007fa0d4f8f4>] lr : [<0000007fa0d4f8ec>]
pstate: 60000000
[ 7654.073864] sp : 0000007fe5405170
[ 7654.077148] x29: 0000007fe5405170 x28: 0000000000000000
[ 7654.082442] x27: 0000000000000000 x26: 0000000000000000
[ 7654.087736] x25: 0000000000000000 x24: 0000000000000000
[ 7654.093031] x23: 0000000000000000 x22: 0000000000000000
[ 7654.098326] x21: 0000000000400740 x20: 0000000000000000
[ 7654.103620] x19: 0000000000000000 x18: 0000000000000000
[ 7654.108915] x17: 0000007fa0c8f698 x16: 0000007fa0d60000
[ 7654.114210] x15: 0000000000000001 x14: 0000000000000000
[ 7654.119505] x13: 0000007fa0d77a18 x12: 0000000000000018
[ 7654.124800] x11: 0000000000000001 x10: 0000007fa0d4f517
[ 7654.130095] x9 : 0000007fa0d77a08 x8 : 0000000000000040
[ 7654.135389] x7 : 000000000000000a x6 : 000000000000000a
[ 7654.140684] x5 : 0000007fa0d36b6f x4 : 0000007fa0d4f951
[ 7654.145979] x3 : 0000000000000000 x2 : 36bc912578986b49
[ 7654.151274] x1 : 0000007fa0d756c0 x0 : 000000ff41ac4d90
[ 7654.156568]
Segmentation fault
-------------------------------
Note that the program shows two different addresses for &iip with the
latter pointing outside the normal memory map. However the test_tls
program works as expected, if you set the LD_BIND_NOW environment variable:
-------------------------------
# LD_BIND_NOW=1 LD_LIBRARY_PATH=/root /root/test_tls
main:
&iip = 0x7f99b606d0
&ii = 0x411048
iip = 0x411048
print_ptrs:
&iip = 0x7f99b606d0
&ii = 0x411048
iip = 0x411048
-------------------------------
Unfortunately this trick does not seem to work for either the
test_res_init program or the ntpd.
Regards,
Christoph Mammitzsch