On Tuesday 09 December 2008 14:39, Bob Paddock wrote: > I had hoped that using OS_main would get rid of the call to main, > wastes stack space, and all of the dead _exit code, which is not allowed > under some design guidelines (as well as taking space), that will > never be executed, but it doesn't. > Is there an attribute that would?
I am aware of two "solutions". First, and most rigorous, is writing your own startup code and link with option -nostartfiles. Please note this requires that you generate a vector table yourself, and has other drawbacks. (for example no .data section possible) Second, and this trick works in some situations: http://www.mail-archive.com/avr-gcc-list@nongnu.org/msg02390.html For me the latter was not reliable enough, so i use the first option most of the time. If you exactly know which interrupts to expect, you may even utilize part of the vector table for code. But the most beautiful would be to have a compiler option like -embedded or so, which gets rid of all junk. I would be more that happy to hear how others solved this. Below three examples (gcc 4.3.2 with patches): 1) Standard compile 2) Overwriting __methodes (Bjoern's trick) 3) with -nostartfiles + custom startup code Note that, since the data section was empty in this example the whole data copy loop is wasted space two. Btw, if you compile with -Wno-main then you can use: void main(void) __attribute__ (( OS_main )) which reduces code somewhat more 1) Standard compile ======================================= 00000000 <__vectors>: 0: 12 c0 rjmp .+36 ; 0x26 <__ctors_end> 2: 2c c0 rjmp .+88 ; 0x5c <__bad_interrupt> 4: 2b c0 rjmp .+86 ; 0x5c <__bad_interrupt> 6: 2a c0 rjmp .+84 ; 0x5c <__bad_interrupt> 8: 29 c0 rjmp .+82 ; 0x5c <__bad_interrupt> a: 28 c0 rjmp .+80 ; 0x5c <__bad_interrupt> c: 27 c0 rjmp .+78 ; 0x5c <__bad_interrupt> e: 26 c0 rjmp .+76 ; 0x5c <__bad_interrupt> 10: 25 c0 rjmp .+74 ; 0x5c <__bad_interrupt> 12: 24 c0 rjmp .+72 ; 0x5c <__bad_interrupt> 14: 23 c0 rjmp .+70 ; 0x5c <__bad_interrupt> 16: 22 c0 rjmp .+68 ; 0x5c <__bad_interrupt> 18: 21 c0 rjmp .+66 ; 0x5c <__bad_interrupt> 1a: 20 c0 rjmp .+64 ; 0x5c <__bad_interrupt> 1c: df c0 rjmp .+446 ; 0x1dc <__vector_14> 1e: 1e c0 rjmp .+60 ; 0x5c <__bad_interrupt> 20: 1d c0 rjmp .+58 ; 0x5c <__bad_interrupt> 22: 1c c0 rjmp .+56 ; 0x5c <__bad_interrupt> 24: 1b c0 rjmp .+54 ; 0x5c <__bad_interrupt> 00000026 <__ctors_end>: 26: 11 24 eor r1, r1 28: 1f be out 0x3f, r1 ; 63 2a: cf e6 ldi r28, 0x6F ; 111 2c: d0 e0 ldi r29, 0x00 ; 0 2e: de bf out 0x3e, r29 ; 62 30: cd bf out 0x3d, r28 ; 61 00000032 <__do_copy_data>: 32: 10 e0 ldi r17, 0x00 ; 0 34: a0 e6 ldi r26, 0x60 ; 96 36: b0 e0 ldi r27, 0x00 ; 0 38: e8 e3 ldi r30, 0x38 ; 56 3a: f2 e0 ldi r31, 0x02 ; 2 3c: 02 c0 rjmp .+4 ; 0x42 <.do_copy_data_start> 0000003e <.do_copy_data_loop>: 3e: 05 90 lpm r0, Z+ 40: 0d 92 st X+, r0 00000042 <.do_copy_data_start>: 42: a0 36 cpi r26, 0x60 ; 96 44: b1 07 cpc r27, r17 46: d9 f7 brne .-10 ; 0x3e <__SP_H__> 00000048 <__do_clear_bss>: 48: 10 e0 ldi r17, 0x00 ; 0 4a: a0 e6 ldi r26, 0x60 ; 96 4c: b0 e0 ldi r27, 0x00 ; 0 4e: 01 c0 rjmp .+2 ; 0x52 <.do_clear_bss_start> 00000050 <.do_clear_bss_loop>: 50: 1d 92 st X+, r1 00000052 <.do_clear_bss_start>: 52: a3 37 cpi r26, 0x73 ; 115 54: b1 07 cpc r27, r17 56: e1 f7 brne .-8 ; 0x50 <.do_clear_bss_loop> 58: 5c d0 rcall .+184 ; 0x112 <main> 5a: ec c0 rjmp .+472 ; 0x234 <_exit> 00000112 <main>: 112: 0a d0 rcall .+20 ; 0x128 <portInit> .... 00000234 <_exit>: 234: f8 94 cli 00000236 <__stop_program>: 236: ff cf rjmp .-2 ; 0x236 <__stop_program> 2) Overwriting __methodes (Bjoern's trick) ================ 00000000 <__vectors>: 0: 12 c0 rjmp .+36 ; 0x26 <__ctors_end> 2: 26 c0 rjmp .+76 ; 0x50 <__bad_interrupt> 4: 25 c0 rjmp .+74 ; 0x50 <__bad_interrupt> 6: 24 c0 rjmp .+72 ; 0x50 <__bad_interrupt> 8: 23 c0 rjmp .+70 ; 0x50 <__bad_interrupt> a: 22 c0 rjmp .+68 ; 0x50 <__bad_interrupt> c: 21 c0 rjmp .+66 ; 0x50 <__bad_interrupt> e: 20 c0 rjmp .+64 ; 0x50 <__bad_interrupt> 10: 1f c0 rjmp .+62 ; 0x50 <__bad_interrupt> 12: 1e c0 rjmp .+60 ; 0x50 <__bad_interrupt> 14: 1d c0 rjmp .+58 ; 0x50 <__bad_interrupt> 16: 1c c0 rjmp .+56 ; 0x50 <__bad_interrupt> 18: 1b c0 rjmp .+54 ; 0x50 <__bad_interrupt> 1a: 1a c0 rjmp .+52 ; 0x50 <__bad_interrupt> 1c: d9 c0 rjmp .+434 ; 0x1d0 <__vector_14> 1e: 18 c0 rjmp .+48 ; 0x50 <__bad_interrupt> 20: 17 c0 rjmp .+46 ; 0x50 <__bad_interrupt> 22: 16 c0 rjmp .+44 ; 0x50 <__bad_interrupt> 24: 15 c0 rjmp .+42 ; 0x50 <__bad_interrupt> 00000026 <__ctors_end>: 26: 11 24 eor r1, r1 00000028 <__do_clear_bss>: 28: a0 e6 ldi r26, 0x60 ; 96 2a: b2 e0 ldi r27, 0x02 ; 2 2c: 80 e0 ldi r24, 0x00 ; 0 0000002e <__do_clear_bss_loop>: 2e: 1e 92 st -X, r1 30: a0 36 cpi r26, 0x60 ; 96 32: b8 07 cpc r27, r24 34: e1 f7 brne .-8 ; 0x2e <__do_clear_bss_loop> 00000036 <__do_stack_def>: 36: af e6 ldi r26, 0x6F ; 111 38: b0 e0 ldi r27, 0x00 ; 0 3a: ad bf out 0x3d, r26 ; 61 3c: be bf out 0x3e, r27 ; 62 0000003e <__do_copy_data>: 3e: 63 c0 rjmp .+198 ; 0x106 <main> { Below the junk which can never be reached starts ... } 40: 11 24 eor r1, r1 42: 1f be out 0x3f, r1 ; 63 44: cf e6 ldi r28, 0x6F ; 111 46: d0 e0 ldi r29, 0x00 ; 0 48: de bf out 0x3e, r29 ; 62 4a: cd bf out 0x3d, r28 ; 61 4c: 5c d0 rcall .+184 ; 0x106 <main> 4e: ec c0 rjmp .+472 ; 0x228 <_exit> { but at least the non modified methodes are gone ...} 00000106 <main>: 106: 0a d0 rcall .+20 ; 0x11c <portInit> ... { More junk which can never be reached ... } 00000228 <_exit>: 228: f8 94 cli 0000022a <__stop_program>: 22a: ff cf rjmp .-2 ; 0x22a <__stop_program> 3) with -nostartfiles + custom startup code =============== { squeeze your code in the vector table ... } 00000000 <__init>: 0: 11 24 eor r1, r1 00000002 <__do_watchdog_reset>: 2: 14 be out 0x34, r1 ; 52 4: 88 e1 ldi r24, 0x18 ; 24 6: 81 bd out 0x21, r24 ; 33 8: 11 bc out 0x21, r1 ; 33 0000000a <__do_clear_bss>: a: a0 e6 ldi r26, 0x60 ; 96 c: b2 e0 ldi r27, 0x02 ; 2 0000000e <__do_clear_bss_loop>: e: 1e 92 st -X, r1 10: a0 36 cpi r26, 0x60 ; 96 12: b1 05 cpc r27, r1 14: e1 f7 brne .-8 ; 0xe <__do_clear_bss_loop> 16: 03 c0 rjmp .+6 ; 0x1e <__do_device_init> 00000018 <__interrupt_vector_definitions>: 18: 68 94 set 1a: 18 95 reti 1c: 45 c0 rjmp .+138 ; 0xa8 <privTickYield> 0000001e <__do_device_init>: 1e: 80 e8 ldi r24, 0x80 ; 128 20: 88 bd out 0x28, r24 ; 40 22: 18 bc out 0x28, r1 ; 40 00000024 <__do_stack_def>: 24: e8 e6 ldi r30, 0x68 ; 104 26: f0 e0 ldi r31, 0x00 ; 0 28: ed bf out 0x3d, r30 ; 61 2a: fe bf out 0x3e, r31 ; 62 0000002c <__do_copy_data>: 2c: 39 c0 rjmp .+114 ; 0xa0 <main> 000000a0 <main>: { portInit not needed anymore ... } a0: 47 d0 rcall .+142 ; 0x130 <appBoot> { no junk ...} Ruud. _______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list