Woodrow Barlow
Woodrow Barlow

Reputation: 9057

What are these function and parameter annotations?

The source code for busybox's syslogd implementation contains some annotations I'm unfamiliar with. The language is C, not C++.

int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int syslogd_main(int argc UNUSED_PARAM, char **argv)

Specifically, MAIN_EXTERNALLY_VISIBLE and UNUSED_PARAM.

  1. What exactly are these annotations doing? Where can I read more about them and other annotations?
  2. Are these part of the C standard, or are they compiler extensions? If they are compiler extensions, how widely supported are they?
  3. I assume the first one is why this file doesn't have a main() function. If these are compiler extensions rather than part of the standard, does this mean this file can't be meaningfully compiled as-is by a compiler that adheres only to the C standard?
  4. Why did they declare a prototype of the syslogd_main function immediately before the full definition? Can the MAIN_EXTERNALLY_VISIBLE annotation only be applied to function prototypes?

Upvotes: 1

Views: 1998

Answers (1)

sergej
sergej

Reputation: 17999

1. What exactly are these annotations doing?

See include/platform.h and include/libbb.h

UNUSED_PARAM expands to __attribute__ ((__unused__)). It specifies the variable (argc in your example) as "possibly unused" and disables the "unused variable" warning.

From the GCC manual [Specifying Attributes of Variables]:

unused This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.

MAIN_EXTERNALLY_VISIBLE expands to EXTERNALLY_VISIBLE and then to __attribute__(( visibility("default") )). It controls the visibility of the function.

From the GCC manual [Declaring Attributes of Functions]:

... On ELF, default visibility means that the declaration is visible to other modules and, in shared libraries, means that the declared entity may be overridden.

From include/libbb.h:

/* We need to export XXX_main from libbusybox
 * only if we build "individual" binaries
 */
#if ENABLE_FEATURE_INDIVIDUAL
#define MAIN_EXTERNALLY_VISIBLE EXTERNALLY_VISIBLE
#else
#define MAIN_EXTERNALLY_VISIBLE
#endif

2. Are these part of the C standard, or ...?

No, those are macros defined in the BusyBox project.

3. I assume the first one is why this file doesn't have a main() function. ...

No. BusyBox combines many utilities into a single executable. That explains the "lack of a main() function" in syslogd.c.

4. Why did they declare a prototype of the syslogd_main function immediately before the full definition? ...

From the GCC manual [Declaring Attributes of Functions]:

The keyword __attribute__ allows you to specify special attributes when making a declaration.

Upvotes: 7

Related Questions