From OSDev Wiki
Jump to: navigation, search

This article is a stub! This page or section is a stub. You can help the wiki by accurately contributing to it.

__alloca, ___main

alloca() is a "semi-standard" C function (from BSD?, but supported by most C implementations) that is used to allocate memory from the stack. On Windows this function is also used for stack probing. As such, alloca() is referenced in PE binaries built, for example, by Cygwin GCC. You can use the Cygwin GCC argument -mno-stack-arg-probe to suppress those references.

Another "specialty" of PE binaries is that, if you define int main(), a function void __main() is called first thing after entering main(). You can either define that function, or omit main() from your kernel code, using a different function as entry point.

This explanation of alloca() comes from Chris Giese, posted to

>> I think _alloca() is for stack-probing, which is required by Windows.
> What is stack probing?
By default, Windows reserves 1 meg of virtual memory for the stack. No page of stack memory is actually allocated (committed) until the page is accessed. This is demand-allocation. The page beyond the top of the stack is the guard page. If this page is accessed, memory will be allocated for it, and the guard page moved downward by 4K (one page). Thus, the stack can grow beyond the initial 1MB. Windows will not, however, let you grow the stack by accessing discontiguous pages of memory. Going beyond the guard page causes an exception. Stack-probing code prevents this.

Some more information about stack-probing:

alloca() is not only used for stack-probing, but also as a sort of malloc(), to dynamically allocate memory for variables/buffers without the need to manually free the reserved memory (with free(), as you normally should).

If you search the man pages for alloca on a UNIX OS you'll find something like this:

The alloca() function allocates space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the function from which alloca() is called returns.

On Windows:

_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits (not when the allocation merely passes out of scope). Therefore, do not pass the pointer value returned by _alloca as an argument to free.

Note that "a stack overflow exception is generated if the space cannot be allocated", so alloca() might cause the stack to grow.

Unfortunately, there are all kinds of caveats with using alloca(). Notably, Turbo-C/C++ had the limitation that functions calling alloca() needed to have at least one local variable, and Linux man-page warns the following:

The alloca function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged.
On many systems alloca cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca would appear on the stack in the middle of the space for the function arguments.
Personal tools