Chapter 9. C and math library overview

eCos provides compatibility with the ISO 9899:1990 specification for the standard C library, which is essentially the same as the better-known ANSI C3.159-1989 specification (C-89).

There are three aspects of this compatibility supplied by eCos. First there is a C library which implements the functions defined by the ISO standard, except for the mathematical functions. This is provided by the eCos C library packages.

Then eCos provides a math library, which implements the mathematical functions from the ISO C library. This distinction between C and math libraries is frequently drawn — most standard C library implementations provide separate linkable files for the two, and the math library contains all the functions from the math.h header file.

There is a third element to the ISO C library, which is the environment in which applications run when they use the standard C library. This environment is set up by the C library startup procedure (Section 9.5, “C library startup”>) and it provides (among other things) a main() entry point function, an exit() function that does the cleanup required by the standard (including handlers registered using the atexit() function), and an environment that can be read with getenv().

The description in this manual focuses on the eCos-specific aspects of the C library (mostly related to eCos's configurability) as well as mentioning the omissions from the standard in this release. We do not attempt to define the semantics of each function, since that information can be found in the ISO, ANSI, POSIX and IEEE standards, and the many good books that have been written about the standard C library, that cover usage of these functions in a more general and useful way.

9.1. Included non-ISO functions

The following functions from the POSIX specification are included for convenience:

extern char **environ variable (for setting up the environment for use with getenv())

eCos provides the following additional implementation-specific functions within the standard C library to adjust the date and time settings:

void cyg_libc_time_setdst(
    cyg_libc_time_dst state

This function sets the state of Daylight Savings Time. The values for state are:


These values will be reflected in the tm_isdst member of a struct tm. No other meaning is given to CYG_LIBC_TIME_DSTNA, and in particular it is not interpreted as any sort of "auto-detect" value, as eCos does not have the extensive timezone information that would be required in order to provide this. A call to mktime() with tm_isdst set to -1 (which corresponds to CYG_LIBC_TIME_DSTNA) will be treated as if the supplied time is in UTC, i.e. with neither standard time nor Daylight Savings Time offsets applied.

void cyg_libc_time_setzoneoffsets(
    time_t stdoffset, time_t dstoffset

This function sets the offsets from UTC used when Daylight Savings Time is enabled or disabled. The offsets are in time_t‚s, which are seconds in the current inplementation.

Cyg_libc_time_dst cyg_libc_time_getzoneoffsets(
    time_t *stdoffset, time_t *dstoffset

This function retrieves the current setting for Daylight Savings Time along with the offsets used for both STD and DST. The offsets are both in time_t‚s, which are seconds in the current implementation.

cyg_bool cyg_libc_time_settime(
      time_t utctime

This function sets the current time for the system The time is specified as a time_t in UTC. It returns non-zero on error.

9.2. Math library compatibility modes

This math library is capable of being operated in several different compatibility modes. These options deal solely with how errors are handled.

There are 4 compatibility modes: ANSI/POSIX 1003.1; IEEE-754; X/Open Portability Guide issue 3 (XPG3); and System V Interface Definition Edition 3.

In IEEE mode, the matherr() function (see below) is never called, no warning messages are printed on the stderr output stream, and errno is never set.

In ANSI/POSIX mode, errno is set correctly, but matherr() is never called and no warning messages are printed on the stderr output stream.

In X/Open mode, errno is set correctly, matherr() is called, but no warning messages are printed on the stderr output stream.

In SVID mode, functions which overflow return a value HUGE (defined in math.h), which is the maximum single precision floating point value (as opposed to HUGE_VAL which is meant to stand for infinity). errno is set correctly and matherr() is called. If matherr() returns 0, warning messages are printed on the stderr output stream for some errors.

The mode can be compiled-in as IEEE-only, or any one of the above methods settable at run-time.


This math library assumes that the hardware (or software floating point emulation) supports IEEE-754 style arithmetic, 32-bit 2's complement integer arithmetic, doubles are in 64-bit IEEE-754 format.

9.2.1.  matherr()

As mentioned above, in X/Open or SVID modes, the user can supply a function matherr() of the form:

int matherr( struct exception *e )

where struct exception is defined as:

struct exception {
  int type;
  char *name;
  double arg1, arg2, retval;

type is the exception type and is one of:

argument domain exception
argument singularity
overflow range exception
underflow range exception
total loss of significance
partial loss of significance

name is a string containing the name of the function

arg1 and arg2 are the arguments passed to the function

retval is the default value that will be returned by the function, and can be changed by matherr()


matherr must have “C” linkage, not “C++” linkage.

If matherr returns zero, or the user doesn't supply their own matherr, then the following usually happens in SVID mode:

Table 9.1. Behavior of math exception handling

DOMAIN 0.0 returned, errno=EDOM, and a message printed on stderr
SING HUGE of appropriate sign is returned, errno=EDOM, and a message is printed on stderr
OVERFLOWHUGE of appropriate sign is returned, and errno=ERANGE
UNDERFLOW0.0 is returned and errno=ERANGE
TLOSS0.0 is returned, errno=ERANGE, and a message is printed on stderr
PLOSSThe current implementation doesn't return this type

X/Open mode is similar except that the message is not printed on stderr and HUGE_VAL is used in place of HUGE

9.2.2. Thread-safety and re-entrancy

With the appropriate configuration options set below, the math library is fully thread-safe if:

  • Depending on the compatibility mode, the setting of the errno variable from the C library is thread-safe
  • Depending on the compatibility mode, sending error messages to the stderr output stream using the C library fputs() function is thread-safe
  • Depending on the compatibility mode, the user-supplied matherr() function and anything it depends on are thread-safe

In addition, with the exception of the gamma*() and lgamma*() functions, the math library is reentrant (and thus safe to use from interrupt handlers) if the Math library is always in IEEE mode.

9.3. Some implementation details

Here are some details about the implementation which might be interesting, although they do not affect the ISO-defined semantics of the library.

  • It is possible to configure eCos to have the standard C library without the kernel. You might want to do this to use less memory.
  • The opaque type returned by clock() is called clock_t, and is implemented as a 64 bit integer. The value returned by clock() is only correct if the kernel is configured with real-time clock support, as determined by the CYGVAR_KERNEL_COUNTERS_CLOCK configuration option in kernel.h .
  • The FILE type is not implemented as a structure, but rather as a CYG_ADDRESS.
  • The GNU C compiler will replace its own built-in implementations instead of calls to some C library functions. This can be turned off with the -fno-builtin option. But it is recommended for normal use to leave compiler builtins enabled. The functions affected by this are described in the documentation associated with the particular GNU compiler version you are using, but include at least:

    abs() labs() sin() strcpy()
    cos() memcmp() sqrt() strlen()
    fabs() memcpy() strcmp()  
  • memcpy() and memset() are located in the infrastructure package, not in the C library package. This is because the compiler calls these functions, and the kernel needs to resolve them even if the C library is not configured.
  • Error codes such as EDOM and ERANGE, as well as strerror() , are implemented in the error package. The error package is separate from the rest of the C and math libraries so that the rest of eCos can use these error handling facilities even if the C library is not configured.
  • The memory allocation package CYGPKG_MEMALLOC is responsible for providing the various heap management functions such as malloc(), free(), etc.
  • Signals, as implemented by <signal.h>, are guaranteed to work correctly if raised using the raise() function from a normal working program context. Using signals from within an ISR or DSR context is not expected to work. Also, it is not guaranteed that if CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS is set, that handling a signal using signal() will necessarily catch that form of exception. For example, it may be expected that a divide-by-zero error would be caught by handling SIGFPE. However it depends on the underlying HAL implementation to implement the required hardware exception. And indeed the hardware itself may not be capable of detecting these exceptions so it may not be possible for the HAL implementer to do this in any case. Despite this lack of guarantees in this respect, the signals implementation is still ISO C compliant since ISO C does not offer any such guarantees either.
  • If you include the POSIX compatibility layer in your configuration, by default it will present a conflict if the C library signals implementation is also present. Only one signals implementation may be present.
  • The getenv() function is implemented (as long as the CYGPKG_LIBC_STDLIB package is present in your configuration), but there is no shell or putenv() function to set the environment dynamically. The environment is set in a global variable environ, declared as:

    extern char **environ; // Standard environment definition

    If the "ISO environment startup/termination" (CYGPKG_LIBC_STARTUP) package is included in your configuration, the environment can be statically initialized at startup time using the CYGDAT_LIBC_DEFAULT_ENVIRONMENT option. If so, remember that the final entry of the array initializer must be NULL.

Here is a minimal eCos program which demonstrates the use of environments (see also the test case in language/c/libc/VERSION/tests/stdlib/getenv.c):

#include <stdio.h>
#include <stdlib.h> // Main header for stdlib functions

extern char **environ; // Standard environment definition

main( int argc, char *argv[] )
    char *str;
    char *env[] = { "PATH=/usr/local/bin:/usr/bin",
    NULL };

    printf("Display the current PATH environment variable\n");

    environ = (char **)&env;

    str = getenv("PATH");

    if (str==NULL) {
        printf("The current PATH is unset\n");
    } else {
        printf("The current PATH is \"%s\"\n", str);
    return 0;

9.4. Thread safety

The ISO C library has configuration options that control thread safety, i.e. working behavior if multiple threads call the same function at the same time.

The following functionality has to be configured correctly, or used carefully in a multi-threaded environment:

  • mblen (void);

  • mbtowc (void);

  • wctomb (void);

  • printf (void);

    and all standard I/O functions except for

    sprintf() (void);

    sscanf (void);

  • strtok (void);

  • rand (void);

    srand (void);

  • signal (void);

    raise (void);

  • asctime (void);

    ctime (void);

    gmtime (void);

    localtime (void);

  • the errno variable
  • the environ variable
  • date and time settings

In some cases, to make eCos development easier, functions are provided (as specified by POSIX 1003.1) that define re-entrant alternatives, i.e. rand_r(), strtok_r(), asctime_r(), ctime_r(), gmtime_r(), and localtime_r(). In other cases, configuration options are provided that control either locking of functions or their shared data, such as with standard I/O streams, or by using per-thread data, such as with the errno variable.

In some other cases, like the setting of date and time, no re-entrant or thread-safe alternative or configuration is provided as it is simply not a worthwhile addition (date and time should rarely need to be set.)

9.5. C library startup

The C library includes a function declared as:

void cyg_iso_c_start( void )

This function is used to start an environment in which an ISO C style program can run in the most compatible way.

What this function does is to create a thread which will invoke main() — normally considered a program's entry point. In particular, it can supply arguments to main() using the CYGDAT_LIBC_ARGUMENTS configuration option, and when returning from main(), or calling exit(), pending stdio file output is flushed and any functions registered with atexit() are invoked. This is all compliant with the ISO C standard in this respect.

This thread starts execution when the eCos scheduler is started. If the eCos kernel package is not available (and hence there is no scheduler), then cyg_iso_c_start() will invoke the main() function directly, i.e. it will not return until the main() function returns.

The main() function should be defined as the following, and if defined in a C++ file, should have “C” linkage:

extern int main(
    int argc,
    char *argv[] )

The thread that is started by cyg_iso_c_start() can be manipulated directly, if you wish. For example you can suspend it. The kernel C API needs a handle to do this, which is available by including the following in your source code.

extern cyg_handle_t cyg_libc_main_thread;

Then for example, you can suspend the thread with the line:

cyg_thread_suspend( cyg_libc_main_thread );

If you call cyg_iso_c_start() and do not provide your own main() function, the system will provide a main() for you which will simply return immediately.

In the default configuration, cyg_iso_c_start() is invoked automatically by the cyg_package_start() function in the infrastructure configuration. This means that in the simplest case, your program can indeed consist of simply:

int main( int argc, char *argv[] )
    printf("Hello eCos\n");

If you override cyg_package_start() or cyg_start(), or disable the infrastructure configuration option CYGSEM_START_ISO_C_COMPATIBILITY then you must ensure that you call cyg_iso_c_start() yourself if you want to be able to have your program start at the entry point of main() automatically.