public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add -nolibc option
@ 2017-07-27 14:22 Tristan Gingold
  2017-08-02 11:33 ` Joseph Myers
  0 siblings, 1 reply; 7+ messages in thread
From: Tristan Gingold @ 2017-07-27 14:22 UTC (permalink / raw)
  To: gcc-patches

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  <gingold@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

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

* Re: [PATCH] Add -nolibc option
  2017-07-27 14:22 [PATCH] Add -nolibc option Tristan Gingold
@ 2017-08-02 11:33 ` Joseph Myers
  0 siblings, 0 replies; 7+ messages in thread
From: Joseph Myers @ 2017-08-02 11:33 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: gcc-patches

On Thu, 27 Jul 2017, Tristan Gingold wrote:

> 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

Help texts should end with ".".  This may not cause a test failure in this 
particular case because help texts for Driver options aren't actually used 
(and the test for help text formatting checks the --help output, not the 
.opt files); you actually need to update gcc.c:display_help to get a help 
text in the driver's --help output.

Since none of the related options such as -nostdlib are mentioned in that 
--help output, either removing the help text or adding "." would seem 
appropriate.  Or add all those options to --help output separately.

> 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"

There are lots of other LINK_GCC_C_SEQUENCE_SPEC definitions for 
particular targets I'd expect to be updated.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [patch] add -nolibc option
  2018-06-22 20:39     ` Jeff Law
@ 2018-06-24 21:42       ` Olivier Hainque
  0 siblings, 0 replies; 7+ messages in thread
From: Olivier Hainque @ 2018-06-24 21:42 UTC (permalink / raw)
  To: Jeff Law; +Cc: Olivier Hainque, Joseph Myers, GCC Patches



> On 22 Jun 2018, at 22:39, Jeff Law <law@redhat.com> wrote:
>> +@item -nolibc
>> +@opindex nolibc
>> +Do not use the C library or system libraries tightly couple with it when
> s/tightly couple/ that tightly couple/
> 
> or
> 
> s/couple/coupled/
> 
> OK with either of those trivial fixes.

Great, Thanks Jeff (and Joseph again for
the first sets of comments) !

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

* Re: [patch] add -nolibc option
  2018-06-21 17:06   ` Olivier Hainque
@ 2018-06-22 20:39     ` Jeff Law
  2018-06-24 21:42       ` Olivier Hainque
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff Law @ 2018-06-22 20:39 UTC (permalink / raw)
  To: Olivier Hainque, Joseph Myers; +Cc: GCC Patches

On 06/21/2018 11:06 AM, Olivier Hainque wrote:
> Hello Joseph,
> 
> Thanks for getting back to me on this!
> 
>> On 19 Jun 2018, at 17:50, Joseph Myers <joseph@codesourcery.com> wrote:
>>
>> On Thu, 7 Jun 2018, Olivier Hainque wrote:
>>
>>> An updated version of the patch is attached, accounting for
>>> your two comments and expanding on the .texi documentation a
>>> bit. 
>> I see you're not changing LINK_GCC_C_SEQUENCE_SPEC in arc/elf.h.  That's a 
>> slightly odd case in that it isn't actually using %L, but is using -lc 
>> directly, whereas there's an empty definition of LIB_SPEC.
> Indeed. I hadn't changed it because it was only added very recently
> (like last week or so) and so wasn't there when I shaped the patch.
> 
>> I'd expect the documentation to say something about libraries added only 
>> for particular languages by the front-end drivers (-lstdc++ -lm, 
>> -lgfortran -lm, etc.).  It may be that the option isn't particularly 
>> meaningful for code using such front-end drivers that add those libraries, 
>> because those libraries depend on libc (and code in those languages will 
>> generally depend on their libraries), but it should still say what the 
>> effects are.
> Agreed.
> 
> Attached is an updated version with the doc reworded to this
> effect, referring to "the system C library or system libraries tightly
> coupled with it", as opposed to "toolchain provided language support
> libraries".
> 
> -lm is a bit annoying. There are very few explicit occurrences as
> this is usually to be added by users. Still, among the few that are there,
> some are in LIB_SPEC and some are not. I have qualified this with a
> "in some configurations" which has the advantage on conveying honestly
> that the effect isn't very precisely defined.
> 
> It has been working well for the uses we had, indeed on bareboard
> configurations which is the stated intent.
> 
> Reboostrapping on x86_64-linux now. If there's meaningful extra
> testing you think I could make, I'll be happy to comply.
> 
> Thanks again for your input.
> 
> With Kind Regards,
> 
> Olivier
> 
> 
> nolibc2.diff

> +@item -nolibc
> +@opindex nolibc
> +Do not use the C library or system libraries tightly couple with it when
s/tightly couple/ that tightly couple/

or

s/couple/coupled/

OK with either of those trivial fixes.

jeff

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

* Re: [patch] add -nolibc option
  2018-06-19 15:50 ` Joseph Myers
@ 2018-06-21 17:06   ` Olivier Hainque
  2018-06-22 20:39     ` Jeff Law
  0 siblings, 1 reply; 7+ messages in thread
From: Olivier Hainque @ 2018-06-21 17:06 UTC (permalink / raw)
  To: Joseph Myers; +Cc: GCC Patches

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

Hello Joseph,

Thanks for getting back to me on this!

> On 19 Jun 2018, at 17:50, Joseph Myers <joseph@codesourcery.com> wrote:
> 
> On Thu, 7 Jun 2018, Olivier Hainque wrote:
> 
>> An updated version of the patch is attached, accounting for
>> your two comments and expanding on the .texi documentation a
>> bit. 
> 
> I see you're not changing LINK_GCC_C_SEQUENCE_SPEC in arc/elf.h.  That's a 
> slightly odd case in that it isn't actually using %L, but is using -lc 
> directly, whereas there's an empty definition of LIB_SPEC.

Indeed. I hadn't changed it because it was only added very recently
(like last week or so) and so wasn't there when I shaped the patch.

> I'd expect the documentation to say something about libraries added only 
> for particular languages by the front-end drivers (-lstdc++ -lm, 
> -lgfortran -lm, etc.).  It may be that the option isn't particularly 
> meaningful for code using such front-end drivers that add those libraries, 
> because those libraries depend on libc (and code in those languages will 
> generally depend on their libraries), but it should still say what the 
> effects are.

Agreed.

Attached is an updated version with the doc reworded to this
effect, referring to "the system C library or system libraries tightly
coupled with it", as opposed to "toolchain provided language support
libraries".

-lm is a bit annoying. There are very few explicit occurrences as
this is usually to be added by users. Still, among the few that are there,
some are in LIB_SPEC and some are not. I have qualified this with a
"in some configurations" which has the advantage on conveying honestly
that the effect isn't very precisely defined.

It has been working well for the uses we had, indeed on bareboard
configurations which is the stated intent.

Reboostrapping on x86_64-linux now. If there's meaningful extra
testing you think I could make, I'll be happy to comply.

Thanks again for your input.

With Kind Regards,

Olivier


[-- Attachment #2: nolibc2.diff --]
[-- Type: application/octet-stream, Size: 11453 bytes --]

diff --git a/gcc/common.opt b/gcc/common.opt
index 0d1445b..14e79c0 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -3044,6 +3044,9 @@ Driver
 nostartfiles
 Driver
 
+nolibc
+Driver
+
 nostdlib
 Driver
 
diff --git a/gcc/config/alpha/linux.h b/gcc/config/alpha/linux.h
index 2bf52fe..6d12d90 100644
--- a/gcc/config/alpha/linux.h
+++ b/gcc/config/alpha/linux.h
@@ -105,7 +105,7 @@ along with GCC; see the file COPYING3.  If not see
    %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"
 
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
diff --git a/gcc/config/arc/elf.h b/gcc/config/arc/elf.h
index 66ee5b6..3472fd2 100644
--- a/gcc/config/arc/elf.h
+++ b/gcc/config/arc/elf.h
@@ -77,4 +77,4 @@ along with GCC; see the file COPYING3.  If not see
 /* If no specs file is enforced, default to nosys libarary.  */
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC				\
-  "--start-group %G %{!specs=*:-lc -lnosys} --end-group"
+  "--start-group %G %{!specs=*:%{!nolibc:-lc -lnosys}} --end-group"
diff --git a/gcc/config/arm/uclinux-elf.h b/gcc/config/arm/uclinux-elf.h
index 54879c7..a003fab 100644
--- a/gcc/config/arm/uclinux-elf.h
+++ b/gcc/config/arm/uclinux-elf.h
@@ -70,8 +70,8 @@
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
-   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G %L}}"
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
+   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G %{!nolibc:%L}}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
 #ifdef HAVE_LD_AS_NEEDED
diff --git a/gcc/config/arm/unknown-elf.h b/gcc/config/arm/unknown-elf.h
index 12dd093..6eeb0eb 100644
--- a/gcc/config/arm/unknown-elf.h
+++ b/gcc/config/arm/unknown-elf.h
@@ -93,4 +93,4 @@
    udivmoddi4, which will depend on the exception unwind routines,
    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"
diff --git a/gcc/config/avr/avrlibc.h b/gcc/config/avr/avrlibc.h
index 95a1d7c..f4a41a1 100644
--- a/gcc/config/avr/avrlibc.h
+++ b/gcc/config/avr/avrlibc.h
@@ -37,4 +37,4 @@ along with GCC; see the file COPYING3.  If not see
 
 #undef  LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "--start-group %G %L --end-group"
+  "--start-group %G %{!nolibc:%L} --end-group"
diff --git a/gcc/config/bfin/bfin.h b/gcc/config/bfin/bfin.h
index 4709282..a503d57 100644
--- a/gcc/config/bfin/bfin.h
+++ b/gcc/config/bfin/bfin.h
@@ -204,7 +204,7 @@
 #endif
 
 #define LINK_GCC_C_SEQUENCE_SPEC "\
-  %{mfast-fp:-lbffastfp} %G %L %{mfast-fp:-lbffastfp} %G \
+  %{mfast-fp:-lbffastfp} %G %{!nolibc:%L} %{mfast-fp:-lbffastfp} %G \
 "
 
 #undef  ASM_SPEC
diff --git a/gcc/config/bfin/linux.h b/gcc/config/bfin/linux.h
index 9d317d1..b1592ee 100644
--- a/gcc/config/bfin/linux.h
+++ b/gcc/config/bfin/linux.h
@@ -35,7 +35,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %{mfast-fp:-lbffastfp} %G %L \
+  "%{static|static-pie:--start-group} %{mfast-fp:-lbffastfp} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group} \
    %{!static:%{!static-pie:%{mfast-fp:-lbffastfp} %G}}"
 
diff --git a/gcc/config/bfin/uclinux.h b/gcc/config/bfin/uclinux.h
index 7ef3b16..beb6d45 100644
--- a/gcc/config/bfin/uclinux.h
+++ b/gcc/config/bfin/uclinux.h
@@ -29,7 +29,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC "\
-  %{mfast-fp:-lbffastfp} %G %L %{mfast-fp:-lbffastfp} %G \
+  %{mfast-fp:-lbffastfp} %G %{!nolibc:%L} %{mfast-fp:-lbffastfp} %G \
 "
 
 #undef TARGET_SUPPORTS_SYNC_CALLS
diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h
index 16c3b60..7fc412b 100644
--- a/gcc/config/darwin.h
+++ b/gcc/config/darwin.h
@@ -213,7 +213,7 @@ extern GTY(()) int darwin_ms_struct;
 /* We only want one instance of %G, since libSystem (Darwin's -lc) does not depend
    on libgcc.  */
 #undef  LINK_GCC_C_SEQUENCE_SPEC
-#define LINK_GCC_C_SEQUENCE_SPEC "%G %L"
+#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L}"
 
 /* ld64 supports a sysroot, it just has a different name and there's no easy
    way to check for it at config time.  */
diff --git a/gcc/config/darwin10.h b/gcc/config/darwin10.h
index 7da3291..d61eb40 100644
--- a/gcc/config/darwin10.h
+++ b/gcc/config/darwin10.h
@@ -28,7 +28,7 @@ along with GCC; see the file COPYING3.  If not see
    %{!static:%{!static-libgcc: \
       %:version-compare(>= 10.6 mmacosx-version-min= -lSystem) } } \
    %{fno-pic|fno-PIC|fno-pie|fno-PIE|fapple-kext|mkernel|static|mdynamic-no-pic: \
-      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %L"
+      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %{!nolibc:%L}"
 
 #undef DEF_MIN_OSX_VERSION
 #define DEF_MIN_OSX_VERSION "10.6"
diff --git a/gcc/config/darwin12.h b/gcc/config/darwin12.h
index f1562c5..e1e1eb0 100644
--- a/gcc/config/darwin12.h
+++ b/gcc/config/darwin12.h
@@ -24,7 +24,7 @@ along with GCC; see the file COPYING3.  If not see
    %{!static:%{!static-libgcc: \
       %:version-compare(>= 10.6 mmacosx-version-min= -lSystem) } } \
    %{fno-pic|fno-PIC|fno-pie|fno-PIE|fapple-kext|mkernel|static|mdynamic-no-pic: \
-      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %L"
+      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %{!nolibc:%L}"
 
 #undef DEF_MIN_OSX_VERSION
 #define DEF_MIN_OSX_VERSION "10.8"
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index cba3c0b..43641d5 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -138,7 +138,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
diff --git a/gcc/config/lm32/uclinux-elf.h b/gcc/config/lm32/uclinux-elf.h
index f6413c4..1b44e08 100644
--- a/gcc/config/lm32/uclinux-elf.h
+++ b/gcc/config/lm32/uclinux-elf.h
@@ -72,7 +72,7 @@
 #define TARGET_OS_CPP_BUILTINS() GNU_USER_TARGET_OS_CPP_BUILTINS()
 
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 #undef  CC1_SPEC
diff --git a/gcc/config/pa/pa-hpux11.h b/gcc/config/pa/pa-hpux11.h
index 8adfe18..0e960c3 100644
--- a/gcc/config/pa/pa-hpux11.h
+++ b/gcc/config/pa/pa-hpux11.h
@@ -157,7 +157,7 @@ along with GCC; see the file COPYING3.  If not see
 /* The libgcc_stub.a library needs to come last.  */
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%G %L %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}}}"
+  "%G %{!nolibc:%L} %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}}}"
 
 #undef STARTFILE_SPEC
 #define STARTFILE_SPEC \
diff --git a/gcc/config/pa/pa64-hpux.h b/gcc/config/pa/pa64-hpux.h
index 6f70767..2c24238 100644
--- a/gcc/config/pa/pa64-hpux.h
+++ b/gcc/config/pa/pa64-hpux.h
@@ -106,7 +106,7 @@ along with GCC; see the file COPYING3.  If not see
 /* The libgcc_stub.a and milli.a libraries need to come last.  */
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC "\
-  %G %L %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}\
+  %G %{!nolibc:%L} %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}\
   milli.a%s}}"
 
 /* Under hpux11, the normal location of the `ld' and `as' programs is the
diff --git a/gcc/config/sparc/sparc.h b/gcc/config/sparc/sparc.h
index 4199954..4868a80 100644
--- a/gcc/config/sparc/sparc.h
+++ b/gcc/config/sparc/sparc.h
@@ -420,7 +420,7 @@ extern enum cmodel sparc_cmodel;
 
 /* Because libgcc can generate references back to libc (via .umul etc.) we have
    to list libc again after the second libgcc.  */
-#define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G %L"
+#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L} %G %{!nolibc:%L}"
 
 \f
 #define PTRDIFF_TYPE (TARGET_ARCH64 ? "long int" : "int")
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 9bb1a49..1e630e9 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -496,7 +496,8 @@ Objective-C and Objective-C++ Dialects}.
 @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
+-nostartfiles  -nodefaultlibs  -nolibc  -nostdlib @gol
+-pie  -pthread  -rdynamic @gol
 -s  -static -static-pie -static-libgcc  -static-libstdc++ @gol
 -static-libasan  -static-libtsan  -static-liblsan  -static-libubsan @gol
 -shared  -shared-libgcc  -symbolic @gol
@@ -12242,8 +12243,8 @@ link an Objective-C or Objective-C++ program.
 @item -nostartfiles
 @opindex nostartfiles
 Do not use the standard system startup files when linking.
-The standard system libraries are used normally, unless @option{-nostdlib}
-or @option{-nodefaultlibs} is used.
+The standard system libraries are used normally, unless @option{-nostdlib},
+@option{-nolibc}, or @option{-nodefaultlibs} is used.
 
 @item -nodefaultlibs
 @opindex nodefaultlibs
@@ -12260,6 +12261,18 @@ These entries are usually resolved by entries in
 libc.  These entry points should be supplied through some other
 mechanism when this option is specified.
 
+@item -nolibc
+@opindex nolibc
+Do not use the C library or system libraries tightly couple with it when
+linking.  Still link with the startup files, @file{libgcc} or toolchain
+provided language support libraries such as @file{libgnat}, @file{libgfortran}
+or @file{libstdc++} unless options preventing their inclusion are used as
+well.  This typically removes @option{-lc} from the link command line, as well
+as system libraries that normally go with it and become meaningless when
+absence of a C library is assumed, for example @option{-lpthread} or
+@option{-lm} in some configurations.  This is intended for bare-board
+targets when there is indeed no C library available.
+
 @item -nostdlib
 @opindex nostdlib
 Do not use the standard system startup files or libraries when linking.
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 405d2e3..1ebcb09 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -867,7 +867,7 @@ proper position among the other output files.  */
    -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

[-- Attachment #3: Type: text/plain, Size: 2 bytes --]




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

* Re: [patch] add -nolibc option
  2018-06-07 19:57 [patch] add " Olivier Hainque
@ 2018-06-19 15:50 ` Joseph Myers
  2018-06-21 17:06   ` Olivier Hainque
  0 siblings, 1 reply; 7+ messages in thread
From: Joseph Myers @ 2018-06-19 15:50 UTC (permalink / raw)
  To: Olivier Hainque; +Cc: GCC Patches

On Thu, 7 Jun 2018, Olivier Hainque wrote:

> An updated version of the patch is attached, accounting for
> your two comments and expanding on the .texi documentation a
> bit. 

I see you're not changing LINK_GCC_C_SEQUENCE_SPEC in arc/elf.h.  That's a 
slightly odd case in that it isn't actually using %L, but is using -lc 
directly, whereas there's an empty definition of LIB_SPEC.

I'd expect the documentation to say something about libraries added only 
for particular languages by the front-end drivers (-lstdc++ -lm, 
-lgfortran -lm, etc.).  It may be that the option isn't particularly 
meaningful for code using such front-end drivers that add those libraries, 
because those libraries depend on libc (and code in those languages will 
generally depend on their libraries), but it should still say what the 
effects are.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [patch] add -nolibc option
@ 2018-06-07 19:57 Olivier Hainque
  2018-06-19 15:50 ` Joseph Myers
  0 siblings, 1 reply; 7+ messages in thread
From: Olivier Hainque @ 2018-06-07 19:57 UTC (permalink / raw)
  To: Joseph Myers; +Cc: GCC Patches, Olivier Hainque

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

Hello Joseph,

This is a follow up on an exchange you had with Tristan Gingold
back in Aug. 2017, starting from

  https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01824.html

with your answer on his proposal at:

  https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00166.html

(Tristan has moved to other horizons since then.)

An updated version of the patch is attached, accounting for
your two comments and expanding on the .texi documentation a
bit. 

As it is, the option guards the expansion of %L within
LINK_GCC_C_SEQUENCE_SPECs, and despite what the macro name
seems to imply, %L expands LIB_SPEC which includes more than
just "-lc" or alike on some targets.

This actually corresponds well to the use we have on baremetal
targets. We build and exercise quite a few such targets with this
patch in for gcc-7 based toolchains, for a variety of cpu families
(sparc/leon, arm, ppc, aarch64, e500v2, visium).

The patch applied almost as-is on mainline, where I just
re-bootstrapped and reg-tested on x86_64-linux. I can perform some
extra testing if necessary.

Thoughts ?

Thanks in advance!

With Kind Regards,

Olivier


2018-06-07  Tristan Gingold  <gingold@adacore.com>
            Olivier Hainque  <hainque@adacore.com>

        * common.opt (nolibc): New option.
        * doc/invoke.texi (Link Options): Document it.
        * gcc.c (LINK_GCC_C_SEQUENCE_SPEC): Honor nolibc.
        * config/alpha/linux.h: Likewise.
        * config/arm/uclinux-elf.h: Likewise.
        * config/arm/unknown-elf.h: Likewise.
        * config/avr/avrlibc.h: Likewise.
        * config/bfin/bfin.h: Likewise.
        * config/bfin/linux.h: Likewise.
        * config/bfin/uclinux.h: Likewise.
        * config/darwin.h: Likewise.
        * config/darwin10.h: Likewise.
        * config/darwin12.h: Likewise.
        * config/gnu-user.h: Likewise.
        * config/lm32/uclinux-elf.h: Likewise.
        * config/pa/pa-hpux11.h: Likewise.
        * config/pa/pa64-hpux.h: Likewise.
        * config/sparc/sparc.h: Likewise.


[-- Attachment #2: 0001-Add-nolibc-driver-switch.patch --]
[-- Type: application/octet-stream, Size: 10770 bytes --]

diff --git a/gcc/common.opt b/gcc/common.opt
index 4aebcaf..7dfaefd 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -3040,6 +3040,9 @@ Driver
 nostartfiles
 Driver
 
+nolibc
+Driver
+
 nostdlib
 Driver
 
diff --git a/gcc/config/alpha/linux.h b/gcc/config/alpha/linux.h
index 9ef3206..4c753d3 100644
--- a/gcc/config/alpha/linux.h
+++ b/gcc/config/alpha/linux.h
@@ -79,7 +79,7 @@ along with GCC; see the file COPYING3.  If not see
 #define TARGET_POSIX_IO
 
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
diff --git a/gcc/config/arm/uclinux-elf.h b/gcc/config/arm/uclinux-elf.h
index 54879c7..a003fab 100644
--- a/gcc/config/arm/uclinux-elf.h
+++ b/gcc/config/arm/uclinux-elf.h
@@ -70,8 +70,8 @@
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
-   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G %L}}"
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
+   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G %{!nolibc:%L}}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
 #ifdef HAVE_LD_AS_NEEDED
diff --git a/gcc/config/arm/unknown-elf.h b/gcc/config/arm/unknown-elf.h
index 12dd093..6eeb0eb 100644
--- a/gcc/config/arm/unknown-elf.h
+++ b/gcc/config/arm/unknown-elf.h
@@ -93,4 +93,4 @@
    udivmoddi4, which will depend on the exception unwind routines,
    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"
diff --git a/gcc/config/avr/avrlibc.h b/gcc/config/avr/avrlibc.h
index 95a1d7c..f4a41a1 100644
--- a/gcc/config/avr/avrlibc.h
+++ b/gcc/config/avr/avrlibc.h
@@ -37,4 +37,4 @@ along with GCC; see the file COPYING3.  If not see
 
 #undef  LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "--start-group %G %L --end-group"
+  "--start-group %G %{!nolibc:%L} --end-group"
diff --git a/gcc/config/bfin/bfin.h b/gcc/config/bfin/bfin.h
index 4709282..a503d57 100644
--- a/gcc/config/bfin/bfin.h
+++ b/gcc/config/bfin/bfin.h
@@ -204,7 +204,7 @@
 #endif
 
 #define LINK_GCC_C_SEQUENCE_SPEC "\
-  %{mfast-fp:-lbffastfp} %G %L %{mfast-fp:-lbffastfp} %G \
+  %{mfast-fp:-lbffastfp} %G %{!nolibc:%L} %{mfast-fp:-lbffastfp} %G \
 "
 
 #undef  ASM_SPEC
diff --git a/gcc/config/bfin/linux.h b/gcc/config/bfin/linux.h
index 9d317d1..b1592ee 100644
--- a/gcc/config/bfin/linux.h
+++ b/gcc/config/bfin/linux.h
@@ -35,7 +35,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %{mfast-fp:-lbffastfp} %G %L \
+  "%{static|static-pie:--start-group} %{mfast-fp:-lbffastfp} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group} \
    %{!static:%{!static-pie:%{mfast-fp:-lbffastfp} %G}}"
 
diff --git a/gcc/config/bfin/uclinux.h b/gcc/config/bfin/uclinux.h
index 7ef3b16..beb6d45 100644
--- a/gcc/config/bfin/uclinux.h
+++ b/gcc/config/bfin/uclinux.h
@@ -29,7 +29,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC "\
-  %{mfast-fp:-lbffastfp} %G %L %{mfast-fp:-lbffastfp} %G \
+  %{mfast-fp:-lbffastfp} %G %{!nolibc:%L} %{mfast-fp:-lbffastfp} %G \
 "
 
 #undef TARGET_SUPPORTS_SYNC_CALLS
diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h
index 16c3b60..7fc412b 100644
--- a/gcc/config/darwin.h
+++ b/gcc/config/darwin.h
@@ -213,7 +213,7 @@ extern GTY(()) int darwin_ms_struct;
 /* We only want one instance of %G, since libSystem (Darwin's -lc) does not depend
    on libgcc.  */
 #undef  LINK_GCC_C_SEQUENCE_SPEC
-#define LINK_GCC_C_SEQUENCE_SPEC "%G %L"
+#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L}"
 
 /* ld64 supports a sysroot, it just has a different name and there's no easy
    way to check for it at config time.  */
diff --git a/gcc/config/darwin10.h b/gcc/config/darwin10.h
index 7da3291..d61eb40 100644
--- a/gcc/config/darwin10.h
+++ b/gcc/config/darwin10.h
@@ -28,7 +28,7 @@ along with GCC; see the file COPYING3.  If not see
    %{!static:%{!static-libgcc: \
       %:version-compare(>= 10.6 mmacosx-version-min= -lSystem) } } \
    %{fno-pic|fno-PIC|fno-pie|fno-PIE|fapple-kext|mkernel|static|mdynamic-no-pic: \
-      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %L"
+      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %{!nolibc:%L}"
 
 #undef DEF_MIN_OSX_VERSION
 #define DEF_MIN_OSX_VERSION "10.6"
diff --git a/gcc/config/darwin12.h b/gcc/config/darwin12.h
index f1562c5..e1e1eb0 100644
--- a/gcc/config/darwin12.h
+++ b/gcc/config/darwin12.h
@@ -24,7 +24,7 @@ along with GCC; see the file COPYING3.  If not see
    %{!static:%{!static-libgcc: \
       %:version-compare(>= 10.6 mmacosx-version-min= -lSystem) } } \
    %{fno-pic|fno-PIC|fno-pie|fno-PIE|fapple-kext|mkernel|static|mdynamic-no-pic: \
-      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %L"
+      %:version-compare(>= 10.7 mmacosx-version-min= -no_pie) } %G %{!nolibc:%L}"
 
 #undef DEF_MIN_OSX_VERSION
 #define DEF_MIN_OSX_VERSION "10.8"
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index cba3c0b..43641d5 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -138,7 +138,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
diff --git a/gcc/config/lm32/uclinux-elf.h b/gcc/config/lm32/uclinux-elf.h
index f6413c4..1b44e08 100644
--- a/gcc/config/lm32/uclinux-elf.h
+++ b/gcc/config/lm32/uclinux-elf.h
@@ -72,7 +72,7 @@
 #define TARGET_OS_CPP_BUILTINS() GNU_USER_TARGET_OS_CPP_BUILTINS()
 
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %L \
+  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 #undef  CC1_SPEC
diff --git a/gcc/config/pa/pa-hpux11.h b/gcc/config/pa/pa-hpux11.h
index 8adfe18..0e960c3 100644
--- a/gcc/config/pa/pa-hpux11.h
+++ b/gcc/config/pa/pa-hpux11.h
@@ -157,7 +157,7 @@ along with GCC; see the file COPYING3.  If not see
 /* The libgcc_stub.a library needs to come last.  */
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%G %L %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}}}"
+  "%G %{!nolibc:%L} %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}}}"
 
 #undef STARTFILE_SPEC
 #define STARTFILE_SPEC \
diff --git a/gcc/config/pa/pa64-hpux.h b/gcc/config/pa/pa64-hpux.h
index 6f70767..2c24238 100644
--- a/gcc/config/pa/pa64-hpux.h
+++ b/gcc/config/pa/pa64-hpux.h
@@ -106,7 +106,7 @@ along with GCC; see the file COPYING3.  If not see
 /* The libgcc_stub.a and milli.a libraries need to come last.  */
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC "\
-  %G %L %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}\
+  %G %{!nolibc:%L} %G %{!nostdlib:%{!nodefaultlibs:%{!shared:-lgcc_stub}\
   milli.a%s}}"
 
 /* Under hpux11, the normal location of the `ld' and `as' programs is the
diff --git a/gcc/config/sparc/sparc.h b/gcc/config/sparc/sparc.h
index 4199954..4868a80 100644
--- a/gcc/config/sparc/sparc.h
+++ b/gcc/config/sparc/sparc.h
@@ -420,7 +420,7 @@ extern enum cmodel sparc_cmodel;
 
 /* Because libgcc can generate references back to libc (via .umul etc.) we have
    to list libc again after the second libgcc.  */
-#define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G %L"
+#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L} %G %{!nolibc:%L}"
 
 \f
 #define PTRDIFF_TYPE (TARGET_ARCH64 ? "long int" : "int")
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 169dd44..cdb3766 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -505,7 +505,8 @@ Objective-C and Objective-C++ Dialects}.
 @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
+-nostartfiles  -nodefaultlibs  -nolibc  -nostdlib @gol
+-pie  -pthread  -rdynamic @gol
 -s  -static -static-pie -static-libgcc  -static-libstdc++ @gol
 -static-libasan  -static-libtsan  -static-liblsan  -static-libubsan @gol
 -static-libmpx  -static-libmpxwrappers @gol
@@ -12388,8 +12389,8 @@ link an Objective-C or Objective-C++ program.
 @item -nostartfiles
 @opindex nostartfiles
 Do not use the standard system startup files when linking.
-The standard system libraries are used normally, unless @option{-nostdlib}
-or @option{-nodefaultlibs} is used.
+The standard system libraries are used normally, unless @option{-nostdlib},
+@option{-nolibc}, or @option{-nodefaultlibs} is used.
 
 @item -nodefaultlibs
 @opindex nodefaultlibs
@@ -12406,6 +12407,15 @@ These entries are usually resolved by entries in
 libc.  These entry points should be supplied through some other
 mechanism when this option is specified.
 
+@item -nolibc
+@opindex nolibc
+Assume no C library is available when linking, but still link with the startup
+files and @file{libgcc} unless options preventing their inclusion are used as
+well.  This typically removes @option{-lc} from the link command line, as well
+as options that normally go with it and become meaningless when absence
+of a C library is assumed, for example @option{-lpthread}.  This is useful
+mainly on bare-board targets when there is indeed no C library available.
+
 @item -nostdlib
 @opindex nostdlib
 Do not use the standard system startup files or libraries when linking.
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 2c597ec..c440fcd 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -864,7 +864,7 @@ proper position among the other output files.  */
    -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
-- 
1.7.10.4


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





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

end of thread, other threads:[~2018-06-24 21:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-27 14:22 [PATCH] Add -nolibc option Tristan Gingold
2017-08-02 11:33 ` Joseph Myers
2018-06-07 19:57 [patch] add " Olivier Hainque
2018-06-19 15:50 ` Joseph Myers
2018-06-21 17:06   ` Olivier Hainque
2018-06-22 20:39     ` Jeff Law
2018-06-24 21:42       ` Olivier Hainque

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).