Page MenuHomePhabricator

[Darwin] Introduce a new flag, -fapple-link-rtlib that forces linking of the builtins library.
Needs ReviewPublic

Authored by aemerson on Feb 15 2019, 10:25 PM.

Details

Summary

This driver flag is useful when users want to link against the compiler's builtins, but nothing else, and so use flags like -nostdlib.

Darwin can't use -nolibc & nostdlib++ like other platforms on because we disables all runtime lib linking with -static, which we still want to have an option to link with the builtins.

Diff Detail

Event Timeline

aemerson created this revision.Feb 15 2019, 10:25 PM

The implementation changes in the Darwin toolchain look fine to me, although with respect to the command line option I think Petr Hosek's message on cfe-dev is interesting:

GCC implements -nolibc which could be used to achieve the same effect when combined with -nostartfiles (and -nostdlib++ when compiling C++). I'd prefer that approach not only because it improves compatibility with with GCC, but also because it matches existing flag scheme which is subtractive rather than additive (i.e. -nodefaultlibs, -nostdlib, -nostdlib++, -nostartfiles). Clang already defines this flag but the only toolchain that currently supports it is DragonFly.

Looking at https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html (quoted here for convenience)

-nostartfiles
Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib, -nolibc, or -nodefaultlibs is used.
-nolibc
Do not use the C library or system libraries tightly coupled with it when linking. Still link with the startup files, libgcc or toolchain provided language support libraries such as libgnat, libgfortran or libstdc++ unless options preventing their inclusion are used as well. This typically removes -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 -lpthread or -lm in some configurations. This is intended for bare-board targets when there is indeed no C library available.

It does seem like these options accomplish what -flink_builtins_rt do with the added advantage of being more portable with gcc. If they don't work for you it will be worth double checking with Petr.

The implementation changes in the Darwin toolchain look fine to me, although with respect to the command line option I think Petr Hosek's message on cfe-dev is interesting:

GCC implements -nolibc which could be used to achieve the same effect when combined with -nostartfiles (and -nostdlib++ when compiling C++). I'd prefer that approach not only because it improves compatibility with with GCC, but also because it matches existing flag scheme which is subtractive rather than additive (i.e. -nodefaultlibs, -nostdlib, -nostdlib++, -nostartfiles). Clang already defines this flag but the only toolchain that currently supports it is DragonFly.

Looking at https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html (quoted here for convenience)

-nostartfiles
Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib, -nolibc, or -nodefaultlibs is used.
-nolibc
Do not use the C library or system libraries tightly coupled with it when linking. Still link with the startup files, libgcc or toolchain provided language support libraries such as libgnat, libgfortran or libstdc++ unless options preventing their inclusion are used as well. This typically removes -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 -lpthread or -lm in some configurations. This is intended for bare-board targets when there is indeed no C library available.

It does seem like these options accomplish what -flink_builtins_rt do with the added advantage of being more portable with gcc. If they don't work for you it will be worth double checking with Petr.

Thanks for taking a look. I've replied on the thread for the problem that approach presents.

aemerson abandoned this revision.Feb 20 2019, 11:19 AM

After discussion on the thread, we can implement this requirement with -nolibc -nostdlib++ -nostartfiles

aemerson updated this revision to Diff 189237.Mar 4 2019, 5:05 PM
aemerson retitled this revision from [Darwin] Introduce a new flag, -flink-builtins-rt that forces linking of the builtins library. to [Darwin] Introduce a new flag, -fapple-link-rtlib that forces linking of the builtins library..
aemerson edited the summary of this revision. (Show Details)

Since we can't use the proposed alternatives of -nolibc and -nostdlib++ reviving this patch as an Apple specific flag.

I've no objections to adding the command line as a Darwin only option. Implementation looks fine to me although I've not got any prior experience with Darwin.

I've no objections to adding the command line as a Darwin only option. Implementation looks fine to me although I've not got any prior experience with Darwin.

Thanks, @ab ok for Darwin?

ab added a comment.Mar 6 2019, 4:06 PM

Please check the embedded thing (the other comments are minor). Otherwise, LGTM; if you have found this flag to be necessary, this looks like a reasonable way to implement it

clang/include/clang/Driver/Options.td
1252

Hmm, maybe move "Apple specific" to a parenthesis at the end?

Also, I'm guessing this belonged here when it was "-flink-rtlib", it doesn't anymore.

clang/lib/Driver/ToolChains/Darwin.cpp
573

Maybe sink this to the AddLinkRuntimeLibArgs implementations (as an extra parameter or directly checking the args)? The flags don't bypass the whole thing anymore, might as well treat it like -mkernel and whatnot.

2084

This is different from 'builtins'. Are you OK with the difference? Otherwise maybe this should be an error for now; I wouldn't be surprised if we never hit this path (this part, I'm not familiar with)