Go to main content
Oracle® Developer Studio 12.5: C User's Guide

Exit Print View

Updated: June 2017
 
 

2.18 Compiling in Free-Standing Environments

The Oracle Developer Studio C compiler supports compilation of programs that link with the standard C library and execute in a runtime environment that includes the standard C library and other runtime support libraries. The C standard terms such an environment a hosted environment. An environment that does not provide the standard library functions is termed a free-standing environment.

The C compiler does not support the general case of compilation for free-standing environments because certain runtime support functions that might be called from compiled code are generally available only in the standard C library. The problem is that source code translation by the compiler may introduce calls to runtime support functions in source code constructs that do not contain function calls and these functions are generally not available for use in a freestanding environment. Consider the following example:

% cat -n lldiv.c
	 1	void
	 2	lldiv(
	 3	    long long *x,
	 4	    long long *y,
	 5	    long long *z)
	 6	{
	 7	    *z = *x / *y ;
	 8	}
% cc -c -m32 lldiv.c
% nm lldiv.o | grep " U "
 0x00000000 U __div64
% cc -c -m64 lldiv.c
% nm lldiv.o | grep " U "

In this example, when the source file lldiv.c is compiled to run on a 32–bit platform using the -m32 option, translation of the statement at line 7 results in an external reference to a runtime support function named __div64, which is only available in the 32–bit version of the standard C library.

When the same source file is compiled to run on a 64–bit platform using the -m64 option, the compiler uses the target machine's 64–bit arithmetic instruction set, obviating the need for a runtime support function in the 64–bit version of the standard C library.

Although the use of the C compiler to target a free-standing environment is not supported in the general case, the compiler can be used, with caveats, to compile code for a particular freestanding environment, namely the Oracle Solaris kernel and device drivers.

Code that runs in the Oracle Solaris kernel, including device drivers, must be written so that external function calls reference only functions that are available within the kernel. To make this possible, the following guidelines are recommended:

  • Do not include header files for libraries that run only in user mode.

  • Do not call functions in the standard C library or in other user mode libraries unless the same function is known to exist in the kernel.

  • Do not use floating point or complex types.

  • Do not use compiler options associated with runtime support libraries (such as, -xprofile, -xopenmp, and -xautopar).

    Relocatable object files associated with particular compiler options are documented in the FILES section of the cc(1) man page. Runtime support libraries associated with C compiler options are documented under the descriptions of the associated options.

As noted previously, the compiler might generate calls to runtime support functions as a result of source code translation. For the specific case of the Oracle Solaris kernel, the set of runtime support functions that might be called is smaller than the general case, since the kernel does not use floating-point or complex types, math library functions, or compiler options associated with runtime support libraries.

The following table lists runtime support functions that may be called in code compiled to run in the Oracle Solaris kernel as a result of source code translation by the C compiler. The table lists platforms on which source code translation generates calls, names of called functions, and source constructs or compiler features that cause generation of function calls. Only 64–bit platforms are listed, since all versions of Oracle Solaris that support the C compiler run a 64–bit kernel.

When compiling for 32–bit instruction sets, additional machine-specific support functions may be called, due to specific limitations of the instruction set.

Table 5  Runtime Support Functions
Function
64–bit Platform
Reference From
__align_cpy_n
SPARC
returning large structs; n is 1,2,4,8, or 16
_memcpy
x86
returning large structs
_memcpy
x86 and SPARC
vectorization
_memmove
x86 and SPARC
vectorization
_memset
x86 and SPARC
vectorization

Note that some versions of the kernel do not provide _memmove(), _memcpy(), or _memset(), but do provide kernel mode analogues of the user mode routines memmove(), memcpy(), and memset().

It is important to note that when compiling Oracle Solaris kernel code for x86 platforms, the option –xvector=%none must be used. By default, the C compiler generates code using XMM registers on x86 platforms to improve performance of general user applications, including applications that do not use C floating point arithmetic types. Use of XMM registers is inappropriate for kernel code.

Additional information can be found in the Writing Device Drivers Guide, and the SPARC Compliance Definition, version 2.4.