Skip to content

Commit

Permalink
include: Declare getopt function on old GNU libc
Browse files Browse the repository at this point in the history
On GNU libc <= 2.25, <unistd.h> includes <getopt.h> with __need_getopt macro
defined.  That <getopt.h> is intended to be a part of GNU libc but
<unistd.h> actually includes include/getopt.h in this project.

If HAVE_DECL_GETOPT is defined to 1 and include/getopt.h is included from
GNU libc's <unistd.h>, declaration of getopt is suppressed, causing errors
on getopt callers.  This issue is possibly hidden so long because there are
not so many true getopt callers in Binutils, GDB and GCC.  Still, this issue
needs to be fixed for following components:

-   Binutils: gprofng
    (not currently affected due to the configuration script but will be)
-   GDB (sim): M32C simulator
-   GDB (sim): RL78 simulator

To avoid not defining proper getopt declaration, we have to check
__need_getopt macro to detect this include path.  With this commit, even if
HAVE_DECL_GETOPT is 1, getopt is declared if:

-   The standard C library is GNU libc and
-   __need_getopt macro is defined (<unistd.h> includes <getopt.h> to
    declare getopt function).

include/ChangeLog:

	* getopt.h: Detect special include path on GNU libc 2.25 or older
	to prevent not declaring getopt function when necessary.
  • Loading branch information
a4lg committed Oct 16, 2022
1 parent 45685a2 commit 810a22f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ struct option
declaration without arguments. If it is 0, we checked and failed
to find the declaration so provide a fully prototyped one. If it
is 1, we found it so don't provide any declaration at all. */
#if !HAVE_DECL_GETOPT
/* On GNU libc <= 2,25, <unistd.h> includes <getopt.h> with __need_getopt
macro defined. That <getopt.h> is intended to be a part of GNU libc
but <unistd.h> actually includes THIS getopt.h. If HAVE_DECL_GETOPT is
defined to 1 and this file is included from GNU libc's <unistd.h>,
declaration of getopt is suppressed, causing errors on getopt callers.
To avoid not defining proper getopt declaration, we have to check
__need_getopt macro when built with GNU libc to detect this include path. */
#if !HAVE_DECL_GETOPT || (defined (__GNU_LIBRARY__) && defined (__need_getopt))
#if defined (__GNU_LIBRARY__) || defined (HAVE_DECL_GETOPT)
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in unistd.h. To avoid compilation
Expand Down

0 comments on commit 810a22f

Please sign in to comment.