Hello,
this patch adds a new option -nolibc to supress -lc in the link command.
This refines -nostdlib/-nostartfiles/nodefaultlibs, so that it is
possible to link with libgcc but without libc.
Our main use case is for embedded targets when we use the GNAT compiler
without an installed libc. Of course, in that case the gnat library has
to provide its own memcpy/memset/memmove/memcmp if needed.
No regressions on x86_64-linux-gnu.
Ok to commit ?
Tristan.
2017-07-27 gingold <ging...@adacore.com>
* common.opt (nolibc): New option.
* doc/invoke.texi (Link Options): Document it.
* gcc.c (LINK_GCC_C_SEQUENCE_SPEC): Consider nolibc.
* config/arm/unknown-elf.h (LINK_GCC_C_SEQUENCE_SPEC): Likewise.
Index: gcc/common.opt
===================================================================
--- gcc/common.opt (revision 250563)
+++ gcc/common.opt (working copy)
@@ -2956,6 +2956,10 @@
nostdlib
Driver
+nolibc
+Driver
+Do not link with libc
+
o
Common Driver Joined Separate Var(asm_file_name)
MissingArgError(missing filename after %qs)
-o <file> Place output into <file>.
Index: gcc/config/arm/unknown-elf.h
===================================================================
--- gcc/config/arm/unknown-elf.h (revision 250563)
+++ gcc/config/arm/unknown-elf.h (working copy)
@@ -91,6 +91,6 @@
/* The libgcc udivmod functions may throw exceptions. If newlib is
configured to support long longs in I/O, then printf will depend on
udivmoddi4, which will depend on the exception unwind routines,
- which will depend on abort, which is defined in libc. */
+ which will depend on abort, which is defined in libc. */
#undef LINK_GCC_C_SEQUENCE_SPEC
-#define LINK_GCC_C_SEQUENCE_SPEC "--start-group %G %L --end-group"
+#define LINK_GCC_C_SEQUENCE_SPEC "--start-group %G %{!nolibc:%L}
--end-group"
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi (revision 250563)
+++ gcc/doc/invoke.texi (working copy)
@@ -495,8 +495,8 @@
@item Linker Options
@xref{Link Options,,Options for Linking}.
@gccoptlist{@var{object-file-name} -fuse-ld=@var{linker}
-l@var{library} @gol
--nostartfiles -nodefaultlibs -nostdlib -pie -pthread -rdynamic @gol
--s -static -static-libgcc -static-libstdc++ @gol
+-nostartfiles -nodefaultlibs -nostdlib -nolibc -pie -pthread @gol
+-rdynamic -s -static -static-libgcc -static-libstdc++ @gol
-static-libasan -static-libtsan -static-liblsan -static-libubsan @gol
-static-libmpx -static-libmpxwrappers @gol
-shared -shared-libgcc -symbolic @gol
@@ -11760,6 +11760,12 @@
constructors are called; @pxref{Collect2,,@code{collect2}, gccint,
GNU Compiler Collection (GCC) Internals}.)
+@item -nolibc
+@opindex nolibc
+Do not use the standard C library when linking, but still link with
+start files and @file{libgcc.a}. This is useful mainly on bare-board
+targets in the case there is no C library available.
+
@item -pie
@opindex pie
Produce a position independent executable on targets that support it.
Index: gcc/gcc.c
===================================================================
--- gcc/gcc.c (revision 250563)
+++ gcc/gcc.c (working copy)
@@ -863,7 +863,7 @@
-lgcc and -lc order specially, yet not require them to override all
of LINK_COMMAND_SPEC. */
#ifndef LINK_GCC_C_SEQUENCE_SPEC
-#define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G"
+#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L %G}"
#endif
#ifndef LINK_SSP_SPEC