[En-Nut-Discussion] Back to standard main() for avr-gcc 4.1

Harald Kipp harald.kipp at egnite.de
Tue May 1 13:07:36 CEST 2007


Hi developers,

For some time now GCC for AVR devices handles main() in a special way by 
setting the stack pointer upon entry. As you may know, main() is started 
as a thread in Nut/OS and the stack pointer modification messed up the 
data space. With help from Joerg Wunsch a workaround had been 
implemented, which

1. redefines main to NutAppMain using a preprocessor macro
2. sets the symbol main to 0 using a linker option

I remember, that sometimes debuggers became confused by the missing main 
symbol. Also C++ doesn't like it.

The patch

gcc-4.1-new-devices-m256X.patch
http://sourceware.org/ml/binutils/2006-05/msg00239.html

eliminates GCC's special handling of main() and initializes the stack 
pointer gcrt1.S, section .init2.

If not already done, for UNIX environments, it should be no problem to 
apply this patch. It had been applied to WinAVR-20070122 as well.

Switching back to standard main with

#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
#define USE_NUTAPPMAIN
#endif

and

#ifdef USE_NUTAPPMAIN
#define main(..)    NutAppMain(__VA_ARGS__)
#endif

for AVR targets will make things look clean again. AFAIK, the macro 
USE_NUTAPPMAIN needs to be defined for the UNIX emulation as well.

Comments are welcome.

Thanks,

Harald




More information about the En-Nut-Discussion mailing list