mailing list of musl libc
 help / color / mirror / Atom feed
* [musl] printf segfaults on recent musl versions
@ 2021-01-11 17:01 Sagar Tiwari
  2021-01-11 18:13 ` Rich Felker
  0 siblings, 1 reply; 5+ messages in thread
From: Sagar Tiwari @ 2021-01-11 17:01 UTC (permalink / raw)
  To: musl

Hi,

Apologies if this isn't a support forum for musl. I couldn't find any
such portal online.

I'm trying to compile a program with musl libc. Dynamically linked
version of this program works fine, but the statically linked version
segfaults whenever `printf` is invoked. This is how a debugging
session looks like:

<pre>
$ gdb out/sm
GNU gdb (GDB) 9.2
Copyright (C) 2020 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-unknown-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 out/sm...
(gdb) b printf
Breakpoint 1 at 0x100ac: file src/stdio/printf.c, line 5.
(gdb) r
Starting program: /home/fctorial/src/pgs/c/out/sm

Breakpoint 1, printf (fmt=0x1004f <msg1> "hello world from printf\n")
at src/stdio/printf.c:5
5       {
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x000000000001010a in printf (fmt=0x1004f <msg1> "hello world from
printf\n") at src/stdio/printf.c:5
5       {
(gdb) quit
A debugging session is active.

        Inferior 1 [process 25984] will be killed.

Quit anyway? (y or n) EOF [assumed Y]

</pre>

This issue appears when linking with recent musl versions (within an
year). 1.0.0 doesn't have this issue. I can call other functions like
'rand', 'strlen' without any issue. `printf` and `puts` are
segfaulting.

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

* Re: [musl] printf segfaults on recent musl versions
  2021-01-11 17:01 [musl] printf segfaults on recent musl versions Sagar Tiwari
@ 2021-01-11 18:13 ` Rich Felker
  2021-01-11 19:16   ` Sagar Tiwari
  0 siblings, 1 reply; 5+ messages in thread
From: Rich Felker @ 2021-01-11 18:13 UTC (permalink / raw)
  To: Sagar Tiwari; +Cc: musl

On Mon, Jan 11, 2021 at 10:31:21PM +0530, Sagar Tiwari wrote:
> Hi,
> 
> Apologies if this isn't a support forum for musl. I couldn't find any
> such portal online.
> 
> I'm trying to compile a program with musl libc. Dynamically linked
> version of this program works fine, but the statically linked version
> segfaults whenever `printf` is invoked. This is how a debugging
> session looks like:
> 
> <pre>
> $ gdb out/sm
> GNU gdb (GDB) 9.2
> Copyright (C) 2020 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-unknown-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 out/sm...
> (gdb) b printf
> Breakpoint 1 at 0x100ac: file src/stdio/printf.c, line 5.
> (gdb) r
> Starting program: /home/fctorial/src/pgs/c/out/sm
> 
> Breakpoint 1, printf (fmt=0x1004f <msg1> "hello world from printf\n")
> at src/stdio/printf.c:5
> 5       {
> (gdb) n
> 
> Program received signal SIGSEGV, Segmentation fault.
> 0x000000000001010a in printf (fmt=0x1004f <msg1> "hello world from
> printf\n") at src/stdio/printf.c:5
> 5       {
> (gdb) quit
> A debugging session is active.
> 
>         Inferior 1 [process 25984] will be killed.
> 
> Quit anyway? (y or n) EOF [assumed Y]
> 
> </pre>
> 
> This issue appears when linking with recent musl versions (within an
> year). 1.0.0 doesn't have this issue. I can call other functions like
> 'rand', 'strlen' without any issue. `printf` and `puts` are
> segfaulting.

Can you elaborate on how you compiled/linked (using musl-gcc wrapper?
mcm-built or distro-provided toolchain or one you built yourself?
etc.)

Judging from the very low address in gdb output, my guess is that you
have a broken toolchain that's trying to link static pie without using
the right link options or start files.

Rich

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

* Re: [musl] printf segfaults on recent musl versions
  2021-01-11 18:13 ` Rich Felker
@ 2021-01-11 19:16   ` Sagar Tiwari
  2021-01-11 19:24     ` Rich Felker
  0 siblings, 1 reply; 5+ messages in thread
From: Sagar Tiwari @ 2021-01-11 19:16 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

I'm assembling a file with clang and linking it to custom compiled
musl with ld. This is the project I'm using:

https://drive.google.com/file/d/1RiKZTf_nJUak14fz7kSXbh7egJuDthDk/view?usp=sharing

musl 1.1.8 is the last version that works with this project. I used
clang 11.0.0 to compile both musl and this project.

On 11/01/2021, Rich Felker <dalias@libc.org> wrote:
> On Mon, Jan 11, 2021 at 10:31:21PM +0530, Sagar Tiwari wrote:
>> Hi,
>>
>> Apologies if this isn't a support forum for musl. I couldn't find any
>> such portal online.
>>
>> I'm trying to compile a program with musl libc. Dynamically linked
>> version of this program works fine, but the statically linked version
>> segfaults whenever `printf` is invoked. This is how a debugging
>> session looks like:
>>
>> <pre>
>> $ gdb out/sm
>> GNU gdb (GDB) 9.2
>> Copyright (C) 2020 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-unknown-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 out/sm...
>> (gdb) b printf
>> Breakpoint 1 at 0x100ac: file src/stdio/printf.c, line 5.
>> (gdb) r
>> Starting program: /home/fctorial/src/pgs/c/out/sm
>>
>> Breakpoint 1, printf (fmt=0x1004f <msg1> "hello world from printf\n")
>> at src/stdio/printf.c:5
>> 5       {
>> (gdb) n
>>
>> Program received signal SIGSEGV, Segmentation fault.
>> 0x000000000001010a in printf (fmt=0x1004f <msg1> "hello world from
>> printf\n") at src/stdio/printf.c:5
>> 5       {
>> (gdb) quit
>> A debugging session is active.
>>
>>         Inferior 1 [process 25984] will be killed.
>>
>> Quit anyway? (y or n) EOF [assumed Y]
>>
>> </pre>
>>
>> This issue appears when linking with recent musl versions (within an
>> year). 1.0.0 doesn't have this issue. I can call other functions like
>> 'rand', 'strlen' without any issue. `printf` and `puts` are
>> segfaulting.
>
> Can you elaborate on how you compiled/linked (using musl-gcc wrapper?
> mcm-built or distro-provided toolchain or one you built yourself?
> etc.)
>
> Judging from the very low address in gdb output, my guess is that you
> have a broken toolchain that's trying to link static pie without using
> the right link options or start files.
>
> Rich
>

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

* Re: [musl] printf segfaults on recent musl versions
  2021-01-11 19:16   ` Sagar Tiwari
@ 2021-01-11 19:24     ` Rich Felker
       [not found]       ` <CABitn4OQEwVRugNHSHLDjTiMLdyh+KZBO6qSMorC8=CUSv5smg@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Rich Felker @ 2021-01-11 19:24 UTC (permalink / raw)
  To: Sagar Tiwari; +Cc: musl

On Tue, Jan 12, 2021 at 12:46:40AM +0530, Sagar Tiwari wrote:
> I'm assembling a file with clang and linking it to custom compiled
> musl with ld. This is the project I'm using:
> 
> https://drive.google.com/file/d/1RiKZTf_nJUak14fz7kSXbh7egJuDthDk/view?usp=sharing

In the future, please send attachments not Google Drive links.

> musl 1.1.8 is the last version that works with this project. I used
> clang 11.0.0 to compile both musl and this project.

You can't call libc if you didn't enter the program through its entry
point (or an ABI-compatible one). If you name the mn function main,
link crt1.o (or the appropriate variant thereof), and drop the linker
script (which may be omitting other necessary items), it should work
as expected.

Rich

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

* Re: [musl] printf segfaults on recent musl versions
       [not found]       ` <CABitn4OQEwVRugNHSHLDjTiMLdyh+KZBO6qSMorC8=CUSv5smg@mail.gmail.com>
@ 2021-01-12  3:29         ` Rich Felker
  0 siblings, 0 replies; 5+ messages in thread
From: Rich Felker @ 2021-01-12  3:29 UTC (permalink / raw)
  To: Sagar Tiwari; +Cc: musl

On Tue, Jan 12, 2021 at 06:33:10AM +0530, Sagar Tiwari wrote:
> These changes fix the issue.
> 
> Another alternative is to manually call '__init_libc' with two args
> pointing to zeroed memory and
>  '__libc_start_init'. Don't know if it's safe?

It's not. __init_libc is not a public function. You can call
__libc_start_main (which is the ABI boundary) from your own entry
point file if you like though but I don't see any reason to prefer
that over just using [S]crt1.o.

Rich

> On 12/01/2021, Rich Felker <dalias@libc.org> wrote:
> > On Tue, Jan 12, 2021 at 12:46:40AM +0530, Sagar Tiwari wrote:
> >> I'm assembling a file with clang and linking it to custom compiled
> >> musl with ld. This is the project I'm using:
> >>
> >> https://drive.google.com/file/d/1RiKZTf_nJUak14fz7kSXbh7egJuDthDk/view?usp=sharing
> >
> > In the future, please send attachments not Google Drive links.
> >
> >> musl 1.1.8 is the last version that works with this project. I used
> >> clang 11.0.0 to compile both musl and this project.
> >
> > You can't call libc if you didn't enter the program through its entry
> > point (or an ABI-compatible one). If you name the mn function main,
> > link crt1.o (or the appropriate variant thereof), and drop the linker
> > script (which may be omitting other necessary items), it should work
> > as expected.

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

end of thread, other threads:[~2021-01-12  3:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-11 17:01 [musl] printf segfaults on recent musl versions Sagar Tiwari
2021-01-11 18:13 ` Rich Felker
2021-01-11 19:16   ` Sagar Tiwari
2021-01-11 19:24     ` Rich Felker
     [not found]       ` <CABitn4OQEwVRugNHSHLDjTiMLdyh+KZBO6qSMorC8=CUSv5smg@mail.gmail.com>
2021-01-12  3:29         ` Rich Felker

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ http://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git