Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <626996737.38853.1506938734029@office.mailbox.org>
Date: Mon, 2 Oct 2017 12:05:33 +0200 (CEST)
From: Paul Kaletta <pk@...lkaletta.org>
To: musl@...ts.openwall.com
Subject: C++ program with no dependencies except musl

Hi,

I'm just getting started with musl, and have two questions.

On Arch Linux I installed the packages for musl (vesion 1.1.16-2), and
gcc (version 7.2.0-2).

I have this program:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("Hello!\n");
    exit(0);
}

I have stored it in justmain.c, and justmain.cpp.

I can easily compile it with:

$ musl-gcc -o justmain_static_c justmain.c -static

However, when I do not want a dynamically linked library, I get an
error:

$ musl-gcc -o justmain_dynamic_c justmain.c
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

The error goes away when I append the -pie, or -no-pie linker option
to the build command line.  What is happening here, and why do both
options work?

For a little project of mine I would like to use C++ language
features, but leave out the default C++ standard library.  I was also
thinking about using musl together with tinystl
(https://github.com/mendsley/tinystl), which is header-only.

I found some good sources (http://www.avabodh.com/cxxin/nostdlib.html,
http://wiki.osdev.org/C%2B%2B,
http://wiki.osdev.org/Calling_Global_Constructors), but I'm still
figuring out how to apply the information to my use case.

I have a hard time with the basics already.  I can take the program
from above and compile it as C++.

$ musl-gcc -std=c++11 -g -nostdlib -fno-exceptions -fno-rtti justmain.cpp -o justmain_cpp --entry main -L /usr/lib/musl/lib/ -lc

The resulting binary works.  When I add -static, I can compile
succesfully, but the resulting binary gives me a segmentation fault.

$ musl-gcc -std=c++11 -g -nostdlib -fno-exceptions -fno-rtti justmain.cpp -o justmain_static_cpp --entry main -L /usr/lib/musl/lib/ -lc -static
$ ./justmain_static_cpp 
Segmentation fault (core dumped)

Let's step through the binary, instruction by instruction, so that we
see where the segfault occurs:

$ gdb ./justmain_static_cpp 
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./justmain_static_cpp...done.
(gdb) b main
Breakpoint 1 at 0x400148: file justmain.cpp, line 6.
(gdb) run
Starting program: /home/pkaletta/justmain_static_cpp 

Breakpoint 1, main () at justmain.cpp:6
6	    printf("Hello!\n");
(gdb) stepi
0x000000000040014f	6	    printf("Hello!\n");
(gdb) 
0x0000000000400182 in puts ()
(gdb) 
0x0000000000400189 in puts ()
(gdb) 
0x000000000040018b in puts ()
(gdb) 
0x000000000040018e in puts ()
(gdb) 
0x000000000040018f in puts ()
(gdb) 
0x0000000000400190 in puts ()
(gdb) 
0x0000000000400192 in puts ()
(gdb) 
0x0000000000400195 in puts ()
(gdb) 
0x000000000040019b in puts ()
(gdb) 
0x000000000040019d in puts ()
(gdb) 
0x00000000004001a9 in puts ()
(gdb) 
0x00000000004001ac in puts ()
(gdb) 
0x00000000004001af in puts ()
(gdb) 
0x0000000000400442 in fputs_unlocked ()
(gdb) 
0x0000000000400444 in fputs_unlocked ()
(gdb) 
0x0000000000400445 in fputs_unlocked ()
(gdb) 
0x0000000000400448 in fputs_unlocked ()
(gdb) 
0x0000000000400449 in fputs_unlocked ()
(gdb) 
0x000000000040044c in fputs_unlocked ()
(gdb) 
0x00000000004005b0 in strlen ()
(gdb) 
0x00000000004005b4 in strlen ()
(gdb) 
0x00000000004005b7 in strlen ()
(gdb) 
0x00000000004005b9 in strlen ()
(gdb) 
0x00000000004005bc in strlen ()
(gdb) 
0x00000000004005c5 in strlen ()
(gdb) 
0x00000000004005c9 in strlen ()
(gdb) 
0x00000000004005cd in strlen ()
(gdb) 
0x00000000004005c0 in strlen ()
(gdb) 
0x00000000004005c3 in strlen ()
(gdb) 
0x00000000004005c5 in strlen ()
(gdb) 
0x00000000004005c9 in strlen ()
(gdb) 
0x00000000004005cd in strlen ()
(gdb) 
0x00000000004005c0 in strlen ()
(gdb) 
0x00000000004005c3 in strlen ()
(gdb) 
0x00000000004005c5 in strlen ()
(gdb) 
0x00000000004005c9 in strlen ()
(gdb) 
0x00000000004005cd in strlen ()
(gdb) 
0x00000000004005c0 in strlen ()
(gdb) 
0x00000000004005c3 in strlen ()
(gdb) 
0x00000000004005c5 in strlen ()
(gdb) 
0x00000000004005c9 in strlen ()
(gdb) 
0x00000000004005cd in strlen ()
(gdb) 
0x00000000004005c0 in strlen ()
(gdb) 
0x00000000004005c3 in strlen ()
(gdb) 
0x00000000004005c5 in strlen ()
(gdb) 
0x00000000004005c9 in strlen ()
(gdb) 
0x00000000004005cd in strlen ()
(gdb) 
0x00000000004005c0 in strlen ()
(gdb) 
0x00000000004005c3 in strlen ()
(gdb) 
0x00000000004005c5 in strlen ()
(gdb) 
0x00000000004005c9 in strlen ()
(gdb) 
0x00000000004005cd in strlen ()
(gdb) 
0x00000000004005cf in strlen ()
(gdb) 
0x00000000004005d2 in strlen ()
(gdb) 
0x00000000004005dc in strlen ()
(gdb) 
0x00000000004005e6 in strlen ()
(gdb) 
0x00000000004005ea in strlen ()
(gdb) 
0x00000000004005ed in strlen ()
(gdb) 
0x00000000004005f0 in strlen ()
(gdb) 
0x00000000004005f3 in strlen ()
(gdb) 
0x0000000000400614 in strlen ()
(gdb) 
0x0000000000400617 in strlen ()
(gdb) 
0x0000000000400619 in strlen ()
(gdb) 
0x000000000040061c in strlen ()
(gdb) 
0x000000000040061f in strlen ()
(gdb) 
0x0000000000400451 in fputs_unlocked ()
(gdb) 
0x0000000000400454 in fputs_unlocked ()
(gdb) 
0x0000000000400457 in fputs_unlocked ()
(gdb) 
0x000000000040045a in fputs_unlocked ()
(gdb) 
0x000000000040045d in fputs_unlocked ()
(gdb) 
0x0000000000400462 in fputs_unlocked ()
(gdb) 
0x0000000000400522 in fwrite_unlocked ()
(gdb) 
0x0000000000400524 in fwrite_unlocked ()
(gdb) 
0x0000000000400526 in fwrite_unlocked ()
(gdb) 
0x000000000040052b in fwrite_unlocked ()
(gdb) 
0x000000000040052d in fwrite_unlocked ()
(gdb) 
0x000000000040052f in fwrite_unlocked ()
(gdb) 
0x0000000000400532 in fwrite_unlocked ()
(gdb) 
0x0000000000400533 in fwrite_unlocked ()
(gdb) 
0x0000000000400534 in fwrite_unlocked ()
(gdb) 
0x0000000000400537 in fwrite_unlocked ()
(gdb) 
0x000000000040053b in fwrite_unlocked ()
(gdb) 
0x000000000040053e in fwrite_unlocked ()
(gdb) 
0x0000000000400541 in fwrite_unlocked ()
(gdb) 
0x0000000000400545 in fwrite_unlocked ()
(gdb) 
0x0000000000400548 in fwrite_unlocked ()
(gdb) 
0x000000000040054b in fwrite_unlocked ()
(gdb) 
0x000000000040054f in fwrite_unlocked ()
(gdb) 
0x0000000000400555 in fwrite_unlocked ()
(gdb) 
0x0000000000400558 in fwrite_unlocked ()
(gdb) 
0x000000000040055a in fwrite_unlocked ()
(gdb) 
0x0000000000400567 in fwrite_unlocked ()
(gdb) 
0x000000000040056a in fwrite_unlocked ()
(gdb) 
0x000000000040056d in fwrite_unlocked ()
(gdb) 
0x0000000000400570 in fwrite_unlocked ()
(gdb) 
0x0000000000400477 in __fwritex ()
(gdb) 
0x0000000000400479 in __fwritex ()
(gdb) 
0x000000000040047b in __fwritex ()
(gdb) 
0x000000000040047e in __fwritex ()
(gdb) 
0x000000000040047f in __fwritex ()
(gdb) 
0x0000000000400480 in __fwritex ()
(gdb) 
0x0000000000400483 in __fwritex ()
(gdb) 
0x0000000000400486 in __fwritex ()
(gdb) 
0x000000000040048a in __fwritex ()
(gdb) 
0x000000000040048f in __fwritex ()
(gdb) 
0x00000000004004b4 in __fwritex ()
(gdb) 
0x00000000004004b7 in __fwritex ()
(gdb) 
0x00000000004003f6 in __towrite ()
(gdb) 
0x00000000004003fc in __towrite ()
(gdb) 
0x00000000004003ff in __towrite ()
(gdb) 
0x0000000000400401 in __towrite ()
(gdb) 
0x0000000000400407 in __towrite ()
(gdb) 
0x0000000000400409 in __towrite ()
(gdb) 
0x000000000040040b in __towrite ()
(gdb) 
0x0000000000400416 in __towrite ()
(gdb) 
0x000000000040041a in __towrite ()
(gdb) 
0x0000000000400422 in __towrite ()
(gdb) 
0x000000000040042a in __towrite ()
(gdb) 
0x000000000040042e in __towrite ()
(gdb) 
0x0000000000400432 in __towrite ()
(gdb) 
0x0000000000400436 in __towrite ()
(gdb) 
0x000000000040043a in __towrite ()
(gdb) 
0x000000000040043c in __towrite ()
(gdb) 
0x00000000004004bc in __fwritex ()
(gdb) 
0x00000000004004be in __fwritex ()
(gdb) 
0x0000000000400491 in __fwritex ()
(gdb) 
0x0000000000400495 in __fwritex ()
(gdb) 
0x0000000000400499 in __fwritex ()
(gdb) 
0x000000000040049c in __fwritex ()
(gdb) 
0x00000000004004c4 in __fwritex ()
(gdb) 
0x00000000004004cb in __fwritex ()
(gdb) 
0x00000000004004ce in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004d5 in __fwritex ()
(gdb) 
0x00000000004004db in __fwritex ()
(gdb) 
0x00000000004004df in __fwritex ()
(gdb) 
0x00000000004004e1 in __fwritex ()
(gdb) 
0x00000000004004e4 in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004d5 in __fwritex ()
(gdb) 
0x00000000004004db in __fwritex ()
(gdb) 
0x00000000004004df in __fwritex ()
(gdb) 
0x00000000004004e1 in __fwritex ()
(gdb) 
0x00000000004004e4 in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004d5 in __fwritex ()
(gdb) 
0x00000000004004db in __fwritex ()
(gdb) 
0x00000000004004df in __fwritex ()
(gdb) 
0x00000000004004e1 in __fwritex ()
(gdb) 
0x00000000004004e4 in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004d5 in __fwritex ()
(gdb) 
0x00000000004004db in __fwritex ()
(gdb) 
0x00000000004004df in __fwritex ()
(gdb) 
0x00000000004004e1 in __fwritex ()
(gdb) 
0x00000000004004e4 in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004d5 in __fwritex ()
(gdb) 
0x00000000004004db in __fwritex ()
(gdb) 
0x00000000004004df in __fwritex ()
(gdb) 
0x00000000004004e1 in __fwritex ()
(gdb) 
0x00000000004004e4 in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004d5 in __fwritex ()
(gdb) 
0x00000000004004db in __fwritex ()
(gdb) 
0x00000000004004df in __fwritex ()
(gdb) 
0x00000000004004e1 in __fwritex ()
(gdb) 
0x00000000004004e4 in __fwritex ()
(gdb) 
0x00000000004004d0 in __fwritex ()
(gdb) 
0x00000000004004d3 in __fwritex ()
(gdb) 
0x00000000004004e8 in __fwritex ()
(gdb) 
0x00000000004004ec in __fwritex ()
(gdb) 
0x00000000004004ef in __fwritex ()
(gdb) 
0x00000000004004f2 in __fwritex ()
(gdb) 
0x0000000000400623 in memcpy ()
(gdb) 
0x0000000000400626 in memcpy ()
(gdb) 
0x000000000040062a in memcpy ()
(gdb) 
0x0000000000400640 in memcpy ()
(gdb) 
0x0000000000400643 in memcpy ()
(gdb) 
0x0000000000400647 in memcpy ()
(gdb) 
0x000000000040064a in memcpy ()
(gdb) 
0x000000000040064d in memcpy ()
(gdb) 
0x000000000040064f in memcpy ()
(gdb) 
0x0000000000400650 in memcpy ()
(gdb) 
0x0000000000400652 in memcpy ()
(gdb) 
0x000000000040064f in memcpy ()
(gdb) 
0x0000000000400650 in memcpy ()
(gdb) 
0x0000000000400652 in memcpy ()
(gdb) 
0x000000000040064f in memcpy ()
(gdb) 
0x0000000000400650 in memcpy ()
(gdb) 
0x0000000000400652 in memcpy ()
(gdb) 
0x000000000040064f in memcpy ()
(gdb) 
0x0000000000400650 in memcpy ()
(gdb) 
0x0000000000400652 in memcpy ()
(gdb) 
0x000000000040064f in memcpy ()
(gdb) 
0x0000000000400650 in memcpy ()
(gdb) 
0x0000000000400652 in memcpy ()
(gdb) 
0x000000000040064f in memcpy ()
(gdb) 
0x0000000000400650 in memcpy ()
(gdb) 
0x0000000000400652 in memcpy ()
(gdb) 
0x0000000000400654 in memcpy ()
(gdb) 
0x00000000004004f7 in __fwritex ()
(gdb) 
0x00000000004004fb in __fwritex ()
(gdb) 
0x00000000004004ff in __fwritex ()
(gdb) 
0x000000000040051a in __fwritex ()
(gdb) 
0x000000000040051b in __fwritex ()
(gdb) 
0x000000000040051c in __fwritex ()
(gdb) 
0x000000000040051d in __fwritex ()
(gdb) 
0x000000000040051f in __fwritex ()
(gdb) 
0x0000000000400521 in __fwritex ()
(gdb) 
0x0000000000400575 in fwrite_unlocked ()
(gdb) 
0x0000000000400578 in fwrite_unlocked ()
(gdb) 
0x000000000040058c in fwrite_unlocked ()
(gdb) 
0x000000000040058f in fwrite_unlocked ()
(gdb) 
0x0000000000400599 in fwrite_unlocked ()
(gdb) 
0x000000000040059d in fwrite_unlocked ()
(gdb) 
0x00000000004005a0 in fwrite_unlocked ()
(gdb) 
0x00000000004005a1 in fwrite_unlocked ()
(gdb) 
0x00000000004005a2 in fwrite_unlocked ()
(gdb) 
0x00000000004005a4 in fwrite_unlocked ()
(gdb) 
0x00000000004005a6 in fwrite_unlocked ()
(gdb) 
0x00000000004005a8 in fwrite_unlocked ()
(gdb) 
0x00000000004005aa in fwrite_unlocked ()
(gdb) 
0x0000000000400467 in fputs_unlocked ()
(gdb) 
0x000000000040046a in fputs_unlocked ()
(gdb) 
0x000000000040046d in fputs_unlocked ()
(gdb) 
0x0000000000400470 in fputs_unlocked ()
(gdb) 
0x0000000000400471 in fputs_unlocked ()
(gdb) 
0x0000000000400473 in fputs_unlocked ()
(gdb) 
0x0000000000400474 in fputs_unlocked ()
(gdb) 
0x0000000000400476 in fputs_unlocked ()
(gdb) 
0x00000000004001b4 in puts ()
(gdb) 
0x00000000004001b6 in puts ()
(gdb) 
0x00000000004001bb in puts ()
(gdb) 
0x00000000004001bd in puts ()
(gdb) 
0x00000000004001c4 in puts ()
(gdb) 
0x00000000004001df in puts ()
(gdb) 
0x00000000004001e4 in puts ()
(gdb) 
0x00000000004001e7 in puts ()
(gdb) 
0x00000000004002b9 in __overflow ()
(gdb) 
0x00000000004002ba in __overflow ()
(gdb) 
0x00000000004002bd in __overflow ()
(gdb) 
0x00000000004002c1 in __overflow ()
(gdb) 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004002c1 in __overflow ()
(gdb)

I'm not sure what exactly is happening, but I suspect that some global
init function for musl was not called, which results in the observed
behavior.

My goal is a minimal example of a C++ program that depends on musl and
nothing else.  It would be nice if it could be linked both statically
and dynamically.  I realize that some C++ language features will need
some infrastructure that is normally provided by the C++ runtime
library.  But as a first step: what is required to get the above
program to compile (with a C++ compiler) to a statically linked binary
that actually works?

Can I use the musl-gcc wrapper with -std=c++11 for my purpose, or do I
need a more sophisticated approach?

Best regards,
Paul

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.