public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Hot to test for _FORTIFY_SOURCE with Autoconf
@ 2020-03-17 22:13 Jeffrey Walton
  2020-03-18  0:11 ` Jonathan Wakely
  0 siblings, 1 reply; 8+ messages in thread
From: Jeffrey Walton @ 2020-03-17 22:13 UTC (permalink / raw)
  To: gcc-help

Hi Everyone,

I'm not sure if this is a GCC or Autoconf question. Please point me in
the right direction.

I'm trying to test for availability of _FORTIFY_SOURCE=2. Setting a
CPPFLAG is not enough since the compiler will happily pass it down.

Does the compiler make the information available anywhere?

Here's a small Autoconf test, but I'm not sure how to craft the program.

  OLD_CPPFLAGS="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2"

  AC_MSG_CHECKING([for -D_FORTIFY_SOURCE=2])
  AC_LINK_IFELSE(
    [AC_LANG_SOURCE([int main(void) {return 0;}])],
    [AC_MSG_RESULT([yes]); MY_CPPFLAGS="$MY_CPPFLAGS -D_FORTIFY_SOURCE=2"],
    [AC_MSG_RESULT([no])]
  )

How do I check for _FORTIFY_SOURCE=2?

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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-17 22:13 Hot to test for _FORTIFY_SOURCE with Autoconf Jeffrey Walton
@ 2020-03-18  0:11 ` Jonathan Wakely
  2020-03-18  0:16   ` Jeffrey Walton
  0 siblings, 1 reply; 8+ messages in thread
From: Jonathan Wakely @ 2020-03-18  0:11 UTC (permalink / raw)
  To: Jeffrey Walton; +Cc: gcc-help

On Tue, 17 Mar 2020 at 22:15, Jeffrey Walton via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> I'm trying to test for availability of _FORTIFY_SOURCE=2.

What does that mean?

If you mean you're trying to test whether setting that has any effect,
that's determined by Glibc not GCC.

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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-18  0:11 ` Jonathan Wakely
@ 2020-03-18  0:16   ` Jeffrey Walton
  2020-03-18  0:27     ` Jonathan Wakely
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Jeffrey Walton @ 2020-03-18  0:16 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

On Tue, Mar 17, 2020 at 8:11 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Tue, 17 Mar 2020 at 22:15, Jeffrey Walton via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> >
> > I'm trying to test for availability of _FORTIFY_SOURCE=2.
>
> What does that mean?

I'm trying to determine if the compiler supports FORTIFY_SOURCE.

When _FORTIFY_SOURCE=2 is used, *_chk functions are used when the
compiler can determine the destination buffer size. Effectively the
compiler inserts those Microsoft safer functions that the libc folks
rejected.

I think it is related to object size checking.

Jeff

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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-18  0:16   ` Jeffrey Walton
@ 2020-03-18  0:27     ` Jonathan Wakely
  2020-03-18  1:49     ` Martin Sebor
  2020-03-18 22:17     ` Jim Wilson
  2 siblings, 0 replies; 8+ messages in thread
From: Jonathan Wakely @ 2020-03-18  0:27 UTC (permalink / raw)
  To: Jeffrey Walton; +Cc: gcc-help

On Wed, 18 Mar 2020 at 00:16, Jeffrey Walton <noloader@gmail.com> wrote:
>
> On Tue, Mar 17, 2020 at 8:11 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> >
> > On Tue, 17 Mar 2020 at 22:15, Jeffrey Walton via Gcc-help
> > <gcc-help@gcc.gnu.org> wrote:
> > >
> > > I'm trying to test for availability of _FORTIFY_SOURCE=2.
> >
> > What does that mean?
>
> I'm trying to determine if the compiler supports FORTIFY_SOURCE.
>
> When _FORTIFY_SOURCE=2 is used, *_chk functions are used when the
> compiler can determine the destination buffer size. Effectively the
> compiler inserts those Microsoft safer functions that the libc folks
> rejected.

I don't think the compiler inserts anything. _FORTIFY_SOURCE enables
features in Glibc, not GCC.

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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-18  0:16   ` Jeffrey Walton
  2020-03-18  0:27     ` Jonathan Wakely
@ 2020-03-18  1:49     ` Martin Sebor
  2020-03-18 22:21       ` Jim Wilson
  2020-03-18 22:17     ` Jim Wilson
  2 siblings, 1 reply; 8+ messages in thread
From: Martin Sebor @ 2020-03-18  1:49 UTC (permalink / raw)
  To: noloader, Jonathan Wakely; +Cc: gcc-help

On 3/17/20 6:16 PM, Jeffrey Walton via Gcc-help wrote:
> On Tue, Mar 17, 2020 at 8:11 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>>
>> On Tue, 17 Mar 2020 at 22:15, Jeffrey Walton via Gcc-help
>> <gcc-help@gcc.gnu.org> wrote:
>>>
>>> I'm trying to test for availability of _FORTIFY_SOURCE=2.
>>
>> What does that mean?
> 
> I'm trying to determine if the compiler supports FORTIFY_SOURCE.
> 
> When _FORTIFY_SOURCE=2 is used, *_chk functions are used when the
> compiler can determine the destination buffer size. Effectively the
> compiler inserts those Microsoft safer functions that the libc folks
> rejected.
> 
> I think it is related to object size checking.

In GCC 10 you can test __has_builtin (__builtin_object_size).  In GCC
versions that don't support __has_builtin using __builtin_object_size
will either compile or not, depending on whether it's supported.  It
doesn't depend on any macros.

Martin


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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-18  0:16   ` Jeffrey Walton
  2020-03-18  0:27     ` Jonathan Wakely
  2020-03-18  1:49     ` Martin Sebor
@ 2020-03-18 22:17     ` Jim Wilson
  2020-03-19  0:06       ` Segher Boessenkool
  2 siblings, 1 reply; 8+ messages in thread
From: Jim Wilson @ 2020-03-18 22:17 UTC (permalink / raw)
  To: noloader; +Cc: Jonathan Wakely, gcc-help

On Tue, Mar 17, 2020 at 5:16 PM Jeffrey Walton via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
> When _FORTIFY_SOURCE=2 is used, *_chk functions are used when the
> compiler can determine the destination buffer size. Effectively the
> compiler inserts those Microsoft safer functions that the libc folks
> rejected.

Then that is how you test for it in a configure script.  Just write a
trivial program that will generate a call to a *_chk function if
_FORTIFY_SOURCE=2 is supported, compile it, and then check for that,
either by greping the assembly source or running nm on the object
file.

Jim

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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-18  1:49     ` Martin Sebor
@ 2020-03-18 22:21       ` Jim Wilson
  0 siblings, 0 replies; 8+ messages in thread
From: Jim Wilson @ 2020-03-18 22:21 UTC (permalink / raw)
  To: Martin Sebor; +Cc: noloader, Jonathan Wakely, gcc-help

On Tue, Mar 17, 2020 at 6:49 PM Martin Sebor via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
> > On Tue, Mar 17, 2020 at 8:11 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> > I think it is related to object size checking.
>
> In GCC 10 you can test __has_builtin (__builtin_object_size).  In GCC
> versions that don't support __has_builtin using __builtin_object_size
> will either compile or not, depending on whether it's supported.  It
> doesn't depend on any macros.

That is a good way to test if the compiler has object size checking
support, but it is no guarantee that the C library has FORTIFY_SOURCE
support.  If it is fortify source support that you need, then you need
to explicitly check for that.  If it is just the object size checking
support you need, then you can do the check this way.

Jim

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

* Re: Hot to test for _FORTIFY_SOURCE with Autoconf
  2020-03-18 22:17     ` Jim Wilson
@ 2020-03-19  0:06       ` Segher Boessenkool
  0 siblings, 0 replies; 8+ messages in thread
From: Segher Boessenkool @ 2020-03-19  0:06 UTC (permalink / raw)
  To: Jim Wilson; +Cc: noloader, gcc-help

On Wed, Mar 18, 2020 at 03:17:27PM -0700, Jim Wilson wrote:
> On Tue, Mar 17, 2020 at 5:16 PM Jeffrey Walton via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> > When _FORTIFY_SOURCE=2 is used, *_chk functions are used when the
> > compiler can determine the destination buffer size. Effectively the
> > compiler inserts those Microsoft safer functions that the libc folks
> > rejected.
> 
> Then that is how you test for it in a configure script.  Just write a
> trivial program that will generate a call to a *_chk function if
> _FORTIFY_SOURCE=2 is supported, compile it, and then check for that,
> either by greping the assembly source or running nm on the object
> file.

glibc itself uses

#if defined _FORTIFY_SOURCE && _FORTIFY_SOURCE > 1


Segher

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

end of thread, other threads:[~2020-03-19  0:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 22:13 Hot to test for _FORTIFY_SOURCE with Autoconf Jeffrey Walton
2020-03-18  0:11 ` Jonathan Wakely
2020-03-18  0:16   ` Jeffrey Walton
2020-03-18  0:27     ` Jonathan Wakely
2020-03-18  1:49     ` Martin Sebor
2020-03-18 22:21       ` Jim Wilson
2020-03-18 22:17     ` Jim Wilson
2020-03-19  0:06       ` Segher Boessenkool

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