public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How to properly link libgcc in -ffreestanding mode?
@ 2021-01-27 11:54 Josef Wolf
  2021-01-27 12:07 ` Alexander Monakov
  0 siblings, 1 reply; 4+ messages in thread
From: Josef Wolf @ 2021-01-27 11:54 UTC (permalink / raw)
  To: gcc-help

Hello,

I am trying to follow the steps described in
http://cs107e.github.io/guides/gcc/
to build a freestanding application which is using functions from libgcc

The code I am trying to compile can be found at
https://github.com/ESultanik/mtwister

I guess the problem comes from the usage of the "double" type in thise code.

This is how I invoke compileer and linker:

$ m68k-unknown-elf-gcc -ansi -std=c89 -pedantic -Wall -Wcast-align \
                       -Wstrict-prototypes \
                       -Wmissing-prototypes -Wnull-dereference \
                       -O2 -g -fno-toplevel-reorder -mcpu32 \
                       -ffreestanding -static-libgcc -lgcc \
                       -c -o mtwister.o mtwister.c

$ m68k-unknown-elf-gcc -ansi -std=c89 -pedantic -Wall -Wcast-align \
                       -Wstrict-prototypes \
                       -Wmissing-prototypes -Wnull-dereference \
                       -O2 -g -fno-toplevel-reorder -mcpu32 \
                       -ffreestanding -static-libgcc -lgcc \
                       -Wl,--cref,--section-start=vectors=0 \
                       -nostdlib -Wl,-Ttext=0x400,--entry=entry \
                       -Wl,--oformat,elf32-m68k \
                       -Wl,--cref,-Map,bestd.map \
                       -Wl,-T,ldscript.ld \
                       -o output.elf `cat file.objs`

The linker complains that it can't find __floatunsidf and __divdf3

I have tried to use -Wl,-lgcc instead, but this did not help.

The map file says that libgcc is loaded:

$ grep libgcc *.map
LOAD /m68k-unknown-elf/lib/gcc/m68k-unknown-elf/9.3.0/mcpu32/libgcc.a
LOAD /m68k-unknown-elf/lib/gcc/m68k-unknown-elf/9.3.0/mcpu32/libgcc.a


And I can see that those functions are provided by libgcc:

$ nm /m68k-unknown-elf/lib/gcc/m68k-unknown-elf/9.3.0/mcpu32/libgcc.a|egrep '(__divdf3|__floatunsidf)'
000005a4 T __divdf3
         U __divdf3
         U __divdf3
         U __floatunsidf
         U __floatunsidf
         U __floatunsidf
         U __floatunsidf
         U __floatunsidf
         U __floatunsidf
000000ba T __floatunsidf
         U __divdf3
         U __floatunsidf

What am I missinghere? Any help?

BTW: This is gcc-9.3.0 and binutils-2.34

-- 
Josef Wolf
jw@raven.inka.de

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

* Re: How to properly link libgcc in -ffreestanding mode?
  2021-01-27 11:54 How to properly link libgcc in -ffreestanding mode? Josef Wolf
@ 2021-01-27 12:07 ` Alexander Monakov
  2021-01-27 12:42   ` Jonathan Wakely
  2021-01-27 12:43   ` Josef Wolf
  0 siblings, 2 replies; 4+ messages in thread
From: Alexander Monakov @ 2021-01-27 12:07 UTC (permalink / raw)
  To: Josef Wolf; +Cc: gcc-help

On Wed, 27 Jan 2021, Josef Wolf wrote:

> $ m68k-unknown-elf-gcc -ansi -std=c89 -pedantic -Wall -Wcast-align \
>                        -Wstrict-prototypes \
>                        -Wmissing-prototypes -Wnull-dereference \
>                        -O2 -g -fno-toplevel-reorder -mcpu32 \
>                        -ffreestanding -static-libgcc -lgcc \
>                        -Wl,--cref,--section-start=vectors=0 \
>                        -nostdlib -Wl,-Ttext=0x400,--entry=entry \
>                        -Wl,--oformat,elf32-m68k \
>                        -Wl,--cref,-Map,bestd.map \
>                        -Wl,-T,ldscript.ld \
>                        -o output.elf `cat file.objs`
> 
[...]
> What am I missinghere? Any help?

On the command line, libraries need to come after object files that need them,
because when inspecting the library, the linker unpacks only those archived
object files that would satisfy some undefined symbol already required by
previously linked .o files.

Therefore, in your command line above, -lgcc needs to come after `cat
file.objs`, not before.

Alexander

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

* Re: How to properly link libgcc in -ffreestanding mode?
  2021-01-27 12:07 ` Alexander Monakov
@ 2021-01-27 12:42   ` Jonathan Wakely
  2021-01-27 12:43   ` Josef Wolf
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-01-27 12:42 UTC (permalink / raw)
  To: Josef Wolf; +Cc: gcc-help

On Wed, 27 Jan 2021 at 12:40, Alexander Monakov via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> On Wed, 27 Jan 2021, Josef Wolf wrote:
>
> > $ m68k-unknown-elf-gcc -ansi -std=c89 -pedantic -Wall -Wcast-align \
> >                        -Wstrict-prototypes \
> >                        -Wmissing-prototypes -Wnull-dereference \
> >                        -O2 -g -fno-toplevel-reorder -mcpu32 \
> >                        -ffreestanding -static-libgcc -lgcc \
> >                        -Wl,--cref,--section-start=vectors=0 \
> >                        -nostdlib -Wl,-Ttext=0x400,--entry=entry \
> >                        -Wl,--oformat,elf32-m68k \
> >                        -Wl,--cref,-Map,bestd.map \
> >                        -Wl,-T,ldscript.ld \
> >                        -o output.elf `cat file.objs`
> >
> [...]
> > What am I missinghere? Any help?
>
> On the command line, libraries need to come after object files that need them,
> because when inspecting the library, the linker unpacks only those archived
> object files that would satisfy some undefined symbol already required by
> previously linked .o files.
>
> Therefore, in your command line above, -lgcc needs to come after `cat
> file.objs`, not before.

See http://c-faq.com/lib/libsearch.html

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

* Re: How to properly link libgcc in -ffreestanding mode?
  2021-01-27 12:07 ` Alexander Monakov
  2021-01-27 12:42   ` Jonathan Wakely
@ 2021-01-27 12:43   ` Josef Wolf
  1 sibling, 0 replies; 4+ messages in thread
From: Josef Wolf @ 2021-01-27 12:43 UTC (permalink / raw)
  To: gcc-help

On Wed, Jan 27, 2021 at 03:07:45PM +0300, Alexander Monakov wrote:
> On Wed, 27 Jan 2021, Josef Wolf wrote:
> [...]
> > What am I missinghere? Any help?
> 
> On the command line, libraries need to come after object files that need them,

Umm! I knew that but managed to forget it :-/

Thanks a lot! Woks now.

-- 
Josef Wolf
jw@raven.inka.de

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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-27 11:54 How to properly link libgcc in -ffreestanding mode? Josef Wolf
2021-01-27 12:07 ` Alexander Monakov
2021-01-27 12:42   ` Jonathan Wakely
2021-01-27 12:43   ` Josef Wolf

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 read-only IMAP folder(s) and NNTP newsgroup(s).