[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
vbcc
.
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.
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.
This section describes specifics of the C library for embedded systems.
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:
__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; |
__main
}
__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.
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; |
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.
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.
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:
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.
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.
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.
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.
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:
Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.
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.
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.
The following list contains some restrictions of this version of the library:
tmpfile()
tmpfile()
function always returns an error.
clock()
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.
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).
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.
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'.
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:
vc +ixemul hello.c |
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; |
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.
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.
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.
vc test.c -o test -sd -lvcs -lamigas |
tmpfile()
tmpfile()
function always returns an error.
clock()
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.
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.
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'.
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.
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.
Stack-handling is similar to AmigaOS/68k (See section 11.4.3 Stack).
tmpfile()
tmpfile()
function always returns an error.
clock()
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.
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'.
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.
Stack-handling is similar to AmigaOS/68k (See section 11.4.3 Stack).
vc test.c -o test -sd -lvcs -lamigas |
tmpfile()
tmpfile()
function always returns an error.
clock()
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.
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'.
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.
There is no automatic stack extension for AmigaOS 4! This should be done automatically by the operating system.
vc test.c -o test -sd -lvcs -lamigas |
tmpfile()
tmpfile()
function always returns an error.
clock()
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.
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'.
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.
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:
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] | [ ? ] |