[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

11. C Library

This chapter describes the C library usually provided with vbcc.


11.1 Introduction

To execute code compiled by vbcc, a library is needed. It provides basic interfaces to the underlying operating system or hardware as well as a set of often used functions.

A big part of the library is portable across all architectures. However, some functions (e.g. for input/output or memory allocation) are naturally dependent on the operating system or hardware. There are several sections in this chapter dealing with different versions of the library.

The library itself often is split into several parts. A startup-code will do useful initializations, like setting up IO, parsing the command line or initializing variables and hardware.

The biggest part of the functions will usually be stored in one library file. The name and format of this file depends on the conventions of the underlying system (e.g. `vc.lib' or `libvc.a').

Often, floating point code (if available) is stored in a different file (e.g. `m.lib' or `libm.a'). If floating point is used in an application, it might be necessary to explicitly link with this library (e.g. by specifying `-lm').

In many cases, the include files provide special inline-code or similar optimizations. Therefore, it is recommended to always include the corresponding include file when using a library function. Even if it is not necessary in all cases, it may affect the quality of the generated code.

The library implements the functions specified by ISO9899:1989 as well as a small number of the new functions from ISO9899:1999.


11.2 Legal

Most parts of this library are public domain. However, for some systems, parts may be under a different license. Please consult the system specific documentation. Usually, linking against this library will not put any restrictions on the created executable unless otherwise mentioned.


11.3 Embedded Systems

This section describes specifics of the C library for embedded systems.


11.3.1 Startup

The startup is usually split into two parts. The first part is done by assembly code that produces the object file `lib/startup.o'. This assembly code is usually provided with vbcc and may have to be adapted to the hardware you are using. The key actions that have to be performed by this code are:

@minus{hardware initialization}
It may be necessary to perform some hardware initialization right at the beginning, e.g. to configure the memory system. This has to be modified by the user.

@minus{variable initializations}
When running code from ROM, some memory sections have to be initialized. Usually, the init-values of initialized variables have to be copied from ROM to the data segment and the values of un-initialized variables have to be cleared in the bss segment. This code is usually provided in the startup code.

@minus{stack pointer}
The stack pointer has to be set to a suitable memory area. The startup code will set the stack pointer to the value of the pointer __stack. There is a default stack provided in the C library which will be used unless the application defines its own stack using, for example, the following code (assuming that the stack grows downwards):
 
#define STACKSIZE <whatever>

static long mystack[STACKSIZE/sizeof(long)];
char *__stack=((char*)mystack)+STACKSIZE;

@minus{calling __main}
After all the above initializations have been performed, the function __main() has to be called. This function is provided by the library and performs high-level initializations, if necessary (mainly it calls constructors created by the linker) and will then call the user main() function. Note that the library may not work correctly if the user main() function is called directly from the startup code.


11.3.2 Heap

When dynamic memory management is used (e.g. by using the malloc() function), a heap memory area is needed to allocate memory from. The malloc() function assumes that __heapptr is a variable pointing to the beginning of the heap memory and that __heapsize specifies the size of the heap area in bytes. The library will provide a default heap memory area that can be replaced by adding, for example, the following file to the application:
 
#define HEAPSIZE <whatever>

char __heap[HEAPSIZE],*__heapptr=__heap;
size_t __heapsize=HEAPSIZE;


11.3.3 Input/Output

The standard C input/output functions are provided also for embedded systems. Reading/writing to a stream will be directed to void unless the following low-level I/O-functions are provided by the application:
 
int __open(const char *name,const char *mode);
void __close(int h);
size_t __read(int h,char *p,size_t l);
size_t __write(int h,const char *p,size_t l);

The __open() function receives a name and a mode string (as in the C fopen() function) as arguments and has to return a file-descriptor if it is possible to open this file. The other functions are equivalent to the corresponding POSIX functions.

Also, stdin, stdout and stderr can be used with the standard descriptors.


11.3.4 Floating Point

Whether floating point is supported, depends on the target architecture and chip. If it is supported, there will usually be a math-library that has to be linked (using option `-lm') when floating point is used.


11.3.5 Useless Functions

Of course, some of the C library functions can not be implemented reasonably on embedded systems. These functions are contained in the library but will always return an error value. Mainly affected are:

@minus{locale}
@minus{time}
@minus{signal}
@minus{filesystem functions}

Depending on the hardware provided by a system it is possible to implement these functions and add them to the application. In this case, the new functions will be used rather than the default ones returning only error values.


11.3.6 Linking/Locating

To produce ROM images (e.g. in the form of absolute ELF executables, Intel Hex files or Motorola S-Records), the linker is called with a linker script. This script can be used to join together different sections of the input files and locate them to suitable absolute memory areas. Also, this linker script can be used to set symbols that may be used by the application or the startup code, e.g. addresses of data sections, initialization values or small data pointers.

Code or data that has to reside at special locations can be put into a special section using the __section attribute. This section can then be placed at the desired location using the linker script.

Usually, an example linker script will be provided. While this is often not suitable for different chips, it may serve as a starting point.


11.4 AmigaOS/68k

This section describes specifics of the C library for AmigaOS/68k. The relevant files are `startup.o', `minstart.o', `vc.lib', `vcs.lib', `mieee.lib', `mieees.lib', `m881.lib', `m881s.lib', `m040.lib', `m040s.lib' `amiga.lib', `amigas.lib', `auto.lib' and `autos.lib'.

Note that `extra.lib' is no longer part of the vbcc distribution. It was replaced by 'PosixLib', available on Aminet `dev/c/vbcc_PosixLib.lha', which has a much more comprehensive support for POSIX and Unix functions.


11.4.1 Startup

The startup code currently consists of a slightly modified standard Amiga startup (`startup.o'). The startup code sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.


11.4.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only. At the moment there are three math libraries:

`mieee.lib'
This one uses the C= math libraries. The startup code will always open MathIeeeSingBas.library, MathIeeeDoubBas.library and MathIeeeDoubTrans.library. Float return values are passed in d0, double return values are passed in d0/d1. A 68000 is sufficient to use this library. You must not specify `-fpu=...' when you use this library.
`m881.lib'
This one uses direct FPU instructions and function return values are passed in fp0. You must have a 68020 or higher and an FPU to use this library. You also have to specify `-fpu=68881'. Several FPU instructions that have to be emulated on 040/060 may be used.
`m040.lib'
This one uses only direct FPU instructions that do not have to be emulated on a 040/060. Other functions use the Motorola emulation routines modified by Aki M Laukkanen. It should be used for programs compiled for 040 or 060 with FPU. Return values are passed in fp0.

Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


11.4.3 Stack

An application can specify the stack-size needed by defining a variable __stack (of type size_t) with external linkage, e.g.

 
size_t __stack=65536; /* 64KB stack-size */

The startup code will check whether the stack-size specified is larger than the default stack-size (as set in the shell) and switch to a new stack of appropriate size, if necessary.

If the `-stack-check' option is specified when compiling, the library will check for a stack overflow and abort the program, if the stack overflows. Note, however, that only code compiled with this option will be checked. Calls to libraries which have not been compiled with `-stack-check' or calls to OS function may cause a stack overflow which is not noticed.

Additionally, if `-stack-check' is used, the maximum stack-size used can be read by querying the external variable __stack_usage.

 
#include <stdio.h>

extern size_t __stack_usage;

main()
{
    do_program();
    printf("stack used: %lu\n",(unsigned long)__stack_usage);
}

Like above, the stack used by functions not compiled using `-stack-check' or OS functions is ignored.


11.4.4 Small data model

When using the small data model of the 68k series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries.

To compile and link a program using the small data model a command like

 
vc test.c -o test -sd -lvcs -lamigas

might be used.


11.4.5 Restrictions

The following list contains some restrictions of this version of the library:

tmpfile()
The tmpfile() function always returns an error.

clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process.


11.4.6 Minimal startup

If you want to write programs that use only Amiga functions and none from vc.lib you can use `minstart.o' instead of `startup.o' and produce smaller executables. This is only useful for people who know enough about the Amiga shared libraries, the stubs in amiga.lib etc. If you do not know enough about those things better forget minstart at all. This startup code does not set up all the things needed by vc.lib, so you must not use most of those functions (string and ctype funtions are ok, but most other functions - especially I/O and memory handling - must not be used). exit() is supplied by minstart and can be used. The command line is not parsed, but passed to main() as a single string, so you can declare main as int main(char *command) or int main(void). Also no Amiga libraries are opened (but SysBase ist set up), so you have to define and open DOSBase yourself if you need it. If you want to use floating point with the IEEE libraries you have to define and open MathIeeeSingBas.library, MathIeeeDoubBas.library and MathIeeeDoubTrans.library (in this order!) and link with mieee.lib (if compiled for FPU this is not needed). A hello world using minstart could look like this:

 
#include <proto/exec.h>
#include <proto/dos.h>
 
struct DosLibrary *DOSBase;
 
main()
{
    if(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0)){
        Write(Output(),"Hello, world!\n",14);
        CloseLibrary((struct Library *)DOSBase);
    }
    return 0;
}

This can yield an executable of under 300 bytes when compiled with `-sc -sd -O2' and linked with `minstart.o' and amigas.lib (using vlink - may not work with other linkers).


11.4.7 amiga.lib

To write programs accessing AmigaOS (rather than standard C functions only), a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement is adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by the original `amiga.lib' and is available in small data. It is recommended to always use this library rather than the original version.

Additionally, there are header files (in the `proto'- and `inline'-subdirectories) which cause inlined calls to Amiga library functions.

For AmigaOS/68k, `amiga.lib' is linked by default.


11.4.8 auto.lib

To link with `auto.lib' (or the small data version `autos.lib') specify the `-lauto' or `-lautos' option to vc. When you are calling a standard Amiga library function and do not have defined the corresponding library base then the library base as well as code to open/close it will be taken from `auto.lib'. By default, `auto.lib' will try to open any library version. If you need at least a certain version you can define and set a variable _<library-base>Ver with external linkage, e.g. (on file-scope):

 
        int _IntuitionBaseVer = 39;

Note that your program will abort before reaching main() if one of the libraries cannot be opened. Also note that the dos.library will be openened by the standard startup code, not by auto.lib. This means you have to open dos.library yourself, when linking with `minstart.o'.


11.4.9 ixemul


11.4.9.1 Introduction

ixemul.library is a shared Amiga library which is covered by the GNU license and includes standard ANSI and POSIX functions as well as some functions common in Unix, BSD and similar operating systems. Have a look at the ixemul-archives if you want to find out more. The link library ixemul.lib provided with vbcc only contains stub functions which call the functions in the shared library plus some auxiliary functions for vbcc. What are the main differences between vc.lib and ixemul.lib?

Things you should note:


11.4.9.2 Legal

The `crt0.o' and `ixemul.lib' in this archive are public domain. Have a look at the ixemul-archives to find out about ixemul.library.

11.4.9.3 Usage

First you must have the ixemul.library and the ixemul-includes (they should e.g. be on aminet in dev/gcc/ixemul-sdk.lha) and assign ixinclude: to the directory where they can be found. To compile a program to use ixemul you must make sure the proper config-file (`ixemul') is used, e.g.
 
        vc +ixemul hello.c
or

The small data model is currently not supported with `ixemul.lib', but the small code model is. If your program needs a certain version of the ixemul.library you can define a variable __ixemulVer with external linkage which specifies the minimum required version, e.g.
 
        int __ixemulVer = 45;


11.5 PowerUp/PPC

This section describes specifics of the C library for PowerUp/PPC. The relevant files are `startup.o', `minstart.o', `libvc.a', `libvcs.a', `libm.a', `libms.a' `libamiga.a', `libamigas.a', `libauto.a' and `libautos.a'.

Note that `libextra.a' is no longer part of the vbcc distribution. It was replaced by 'PosixLib', available on Aminet `dev/c/vbcc_PosixLib.lha', which has a much more comprehensive support for POSIX and Unix functions.


11.5.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.


11.5.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only.

The math library (`libm.a') is linked against the floating point library libmoto by Motorola.

Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


11.5.3 Stack

Stack-handling is similar to AmigaOS/68k (See section 11.4.3 Stack). The only difference is that stack-swapping cannot be done. If the default stack-size is less than the stack-size specified with __stack the program will abort.


11.5.4 Small data model

When using the small data model of the PPC series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries. To compile and link a program using the small data model a command like
 
vc test.c -o test -sd -lvcs -lamigas
might be used.


11.5.5 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process.


11.5.6 Minimal startup

The provided minimal startup code (`minstart.o') is used similarly like the one for 68k (See section 11.4.6 Minimal startup). Only use it if you know what you are doing.


11.5.7 libamiga.a

To write programs accessing AmigaOS (rather than standard C functions only), a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement (`libamiga.a') automatically performs a necessary context switch to the 68k to execute the system call. Furthermore, it is adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by the original `amiga.lib' and is available in small data.

Specify `-lamiga' to link with `libamiga.a'.


11.5.8 libauto.a

This library corresponds to the AmigaOS/68k version (See section 11.4.8 auto.lib).


11.6 WarpOS/PPC

This section describes specifics of the C library for WarpOS/PPC. The relevant files are `startup.o', `vc.lib', `m.lib', `amiga.lib' and `auto.lib'.

Note that `extra.lib' is no longer part of the vbcc distribution. It was replaced by 'PosixLib', available on Aminet `dev/c/vbcc_PosixLib.lha', which has a much more comprehensive support for POSIX and Unix functions.


11.6.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.


11.6.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only.

The math library (`m.lib') contains functions from Sun's portable floating point library. Additionally, there is a vbcc version of Andreas Heumann's `ppcmath.lib'. These routines are linked against Motorola's floating point routines optimized for PowerPC and therefore are much faster.

To make use of this library, link with `ppcmath.lib' before `m.lib', e.g.

 
    vc test.c -lppcmath -lm

Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


11.6.3 Stack

Stack-handling is similar to AmigaOS/68k (See section 11.4.3 Stack).


11.6.4 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process.


11.6.5 amiga.lib

To write programs accessing AmigaOS (rather than standard C functions only), a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement automatically performs a necessary context switch to the 68k to execute the system call. Furthermore, it is adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by the original `amiga.lib' and is available in small data. Specify `-lamiga' to link with `amiga.lib'.


11.6.6 auto.lib

This library corresponds to the AmigaOS/68k version (See section 11.4.8 auto.lib).


11.7 MorphOS/PPC

This section describes specifics of the C library for MorphOS/PPC. The relevant files are `startup.o', `minstart.o', `libvc.a', `libvcs.a', `libm.a', `libms.a' `libamiga.a', `libamigas.a', `libauto.a' and `libautos.a'.

Note that `libextra.a' is no longer part of the vbcc distribution. It was replaced by 'PosixLib', available on Aminet `dev/c/vbcc_PosixLib.lha', which has a much more comprehensive support for POSIX and Unix functions.


11.7.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.

11.7.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only. The math library (`libm.a') is linked against the floating point library libmoto by Motorola. Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


11.7.3 Stack

Stack-handling is similar to AmigaOS/68k (See section 11.4.3 Stack).


11.7.4 Small data model

When using the small data model of the PPC series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries. To compile and link a program using the small data model a command like
 
vc test.c -o test -sd -lvcs -lamigas
might be used.


11.7.5 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on MorphOS it is not possible to obtain the time used by the calling process.


11.7.6 libamiga.a

To write programs using AmigaOS compatible functions, a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement (`libamiga.a') will invoke the MorphOS 68k emulator to execute the system function. Furthermore, it is adapted to vbcc and does not cause collisions with some functions (e.g. sprintf) and is available in small data.

Specify `-lamiga' to link with `libamiga.a'.


11.7.7 libauto.a

This library corresponds to the AmigaOS/68k version (See section 11.4.8 auto.lib).


11.8 AmigaOS4/PPC

This section describes specifics of the C library for AmigaOS4/PPC. The relevant files are `startup.o', `minstart.o', `libvc.a', `libvcs.a', `libm.a', `libms.a' `libamiga.a', `libamigas.a', `libauto.a' and `libautos.a'.

Note that `libextra.a' is no longer part of the vbcc distribution. It was replaced by 'PosixLib', available on Aminet `dev/c/vbcc_PosixLib.lha', which has a much more comprehensive support for POSIX and Unix functions.


11.8.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.

11.8.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only. The math library (`libm.a') is linked against the floating point library libmoto by Motorola. Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


11.8.3 Stack

There is no automatic stack extension for AmigaOS 4! This should be done automatically by the operating system.


11.8.4 Small data model

When using the small data model of the PPC series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries. To compile and link a program using the small data model a command like
 
vc test.c -o test -sd -lvcs -lamigas
might be used.


11.8.5 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process.


11.8.6 libamiga.a

In contrast to other amigalibs the OS4 `libamiga.a' doesn't contain any stubs for calling system functions. AmigaOS 4 system calls are done through special macros in the SDK's interface header files.

The library only includes some remaining amigalib functions, not already integrated into the OS, like CreateIO(), but its use is discouraged.

Specify `-lamiga' to link with `libamiga.a'.


11.8.7 libauto.a

Auto-open -close functions for the following libraries are included:

Asl, CyberGfx, DataTypes, Dos, GadTools, Graphics, Icon, IFFParse, Intuition, Locale, LowLevel, Picasso96, BSDSocket, Utility, Workbench

Note that gcc's `libauto.a' doesn't include CyberGfx.


11.8.8 newlib


11.8.8.1 Introduction

newlib.library is a shared AmigaOS4 library, which is covered by several BSD like licenes, and includes standard ANSI and POSIX functions as well as some functions common in Unix, BSD and similar operating systems. It is part of the OS4 SDK.

The config file `newlib' will be created on installation to use the paths for header files and libraries pointing to the newlib from the SDK.

What are the main differences between vclib and newlib?

Things you should note:


11.8.8.2 Usage

To compile a program to use newlib for OS4 you must make sure the proper config-file (`newlib') is used, e.g.
 
        vc +newlib hello.c


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Frank Wille on June, 5 2006 using texi2html