mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Static linking is broken after creation of DT_TEXTREL segment
@ 2020-01-29 18:41 Андрей Аладьев
  2020-01-29 19:19 ` Markus Wichmann
  2020-01-29 20:53 ` Rich Felker
  0 siblings, 2 replies; 26+ messages in thread
From: Андрей Аладьев @ 2020-01-29 18:41 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 3516 bytes --]

Hello. Please use the following docker image
"puchuu/test_x86_64-gentoo-linux-musl". I will write here complete steps so
everyone can reproduce this issue.

docker run -it puchuu/test_x86_64-gentoo-linux-musl bash
env-update && source /etc/profile
echo "dev-libs/gmp static-libs" > /etc/portage/package.use/gmp
MAKEOPTS='-j16' emerge -v dev-libs/gmp
cd /tmp && wget "
https://raw.githubusercontent.com/andrew-aladev/lzws/master/cmake/checks/GMP/main.c
"

gcc main.c -static -lgmp -o main && ./main
We can see that "-static -lgmp" works perfect.

gcc main.c /usr/lib/libgmp.a -o main && ./main
/usr/lib/gcc/x86_64-gentoo-linux-musl/9.2.0/../../../../x86_64-gentoo-linux-musl/bin/ld:
/usr/lib/libgmp.a(bdiv_q_1.o): warning: relocation against
`__gmp_binvert_limb_table' in read-only section `.text'
/usr/lib/gcc/x86_64-gentoo-linux-musl/9.2.0/../../../../x86_64-gentoo-linux-musl/bin/ld:
warning: creating a DT_TEXTREL in object
Segmentation fault (core dumped)
We can see that direct usage of "/usr/lib/libgmp.a" provided DT_TEXTREL
segment.

MAKEOPTS='-j16' emerge -v gdb dev-vcs/git
CFLAGS='-O0 -g -ggdb -ggdb3' CXXFLAGS='-O0 -g -ggdb -ggdb3'
FEATURES='nostrip' MAKEOPTS='-j16' emerge -v musl gmp
git clone git://git.musl-libc.org/musl --depth=1 --single-branch -b
"v1.1.24"
gcc -O0 -g -ggdb -ggdb3 main.c /usr/lib/libgmp.a -o main && gdb -ex=run -d
musl ./main

Program received signal SIGSEGV, Segmentation fault.
0x00007fe2f8c4f231 in do_relocs (dso=0x7fe2f8c8eb40 <app>,
rel=0x55af0a0cd568, rel_size=456, stride=3) at ldso/dynlink.c:423
423                             *reloc_addr = (size_t)base + addend;
(gdb) where
#0  0x00007fe2f8c4f231 in do_relocs (dso=0x7fe2f8c8eb40 <app>,
rel=0x55af0a0cd568, rel_size=456, stride=3) at ldso/dynlink.c:423
#1  0x00007fe2f8c51e60 in reloc_all (p=0x7fe2f8c8eb40 <app>) at
ldso/dynlink.c:1328
#2  0x00007fe2f8c53a03 in __dls3 (sp=0x7ffc82457260) at ldso/dynlink.c:1906
#3  0x00007fe2f8c52de6 in __dls2b (sp=0x7ffc82457260) at ldso/dynlink.c:1672
#4  0x00007fe2f8c52d4e in __dls2 (base=0x7fe2f8bba000
"\177ELF\002\001\001", sp=0x7ffc82457260) at ldso/dynlink.c:1650
#5  0x00007fe2f8c4e5a0 in _dlstart_c (sp=0x7ffc82457260,
dynv=0x7fe2f8c8be20) at ldso/dlstart.c:147
#6  0x00007fe2f8c4e246 in _dlstart () from /lib/ld-musl-x86_64.so.1
#7  0x0000000000000001 in ?? ()
#8  0x00007ffc82458635 in ?? ()
#9  0x0000000000000000 in ?? ()

(gdb) info locals
base = 0x561041462000 "\177ELF\002\001\001"
syms = 0x5610414622c8
strings = 0x561041462478 ""
sym = 0x0
name = 0x5610414624f8 "free"
ctx = 0x7f9eea640b40 <app>
type = 8
sym_index = 0
def = {sym = 0x0, dso = 0x7f9eea640b40 <app>}
reloc_addr = 0x56104147cf79 <__gmpn_bdiv_q_1+25>
sym_val = 0
tls_val = 0
addend = 131264
skip_relative = 0
reuse_addends = 0
save_slot = 0

(gdb) p laddr(dso, rel[0])
$27 = (void *) 0x56104147cf79 <__gmpn_bdiv_q_1+25>

(gdb) p dso->loadmap
$28 = (struct fdpic_loadmap *) 0x0

(gdb) p (dso->base + rel[0])
$29 = (unsigned char *) 0x56104147cf79 <__gmpn_bdiv_q_1+25> "\300"

We can see that "laddr" provided pointer to "dso->base + rel[0]", than
switch tried to override it's value and segfault appeared. Pointer is
wrong, most likely readonly.

You can replace "gcc" with "clang" and everything will be the same. Updated
binutils to most recent version 2.33.1 and rebuilt toolchain - nothing
changed. Pointer is still invalid and SEGV_ACCERR appears.

So I think that bug is inside musl itself. Glibc container is the same
situation works fine. I see no way to create a workaround for this issue.

[-- Attachment #2: Type: text/html, Size: 4589 bytes --]

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2020-02-03  4:40 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-29 18:41 [musl] Static linking is broken after creation of DT_TEXTREL segment Андрей Аладьев
2020-01-29 19:19 ` Markus Wichmann
2020-01-29 19:38   ` Markus Wichmann
2020-01-29 20:48     ` Rich Felker
2020-01-29 20:08   ` Андрей Аладьев
2020-01-30 17:02     ` Markus Wichmann
2020-01-31  4:24       ` Rich Felker
2020-01-31 14:47         ` Markus Wichmann
2020-01-31 16:35           ` Rich Felker
2020-01-31 15:16       ` Андрей Аладьев
2020-01-31 16:40         ` Rich Felker
2020-01-31 17:51           ` Андрей Аладьев
2020-01-31 18:01             ` Rich Felker
2020-01-31 19:11               ` Андрей Аладьев
2020-02-03  3:10       ` Rich Felker
2020-02-03  4:05         ` Rich Felker
2020-02-03  4:32         ` Markus Wichmann
2020-02-03  4:40           ` Rich Felker
2020-01-29 20:53 ` Rich Felker
2020-01-29 21:10   ` Szabolcs Nagy
2020-01-29 21:35     ` Андрей Аладьев
2020-01-29 21:46       ` Rich Felker
2020-01-29 23:10         ` Андрей Аладьев
2020-01-29 23:20       ` Szabolcs Nagy
2020-01-29 21:14   ` Андрей Аладьев
2020-01-29 21:43     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).