Source of main.cpp:
typedef void (* fun_t)( void ); static const fun_t fun = [] { for (;;); }; int main( void ) { fun(); }
Listing:
... Region$$Table$$Base Region$$Table$$Limit main ** Section #2 '.debug_frame' (SHT_PROGBITS) Size : 68 bytes ...
Map file:
... Removing main.o(.text), (0 bytes). Removing main.o(.ARM.exidx.text.main), (8 bytes). Removing main.o(.ARM.use_no_argv), (4 bytes). ...
Why main function was removed?
It does nothing?
It was in-lined as a " B . "
... $d.1 __arm_cp.0_0 0x08000228: 40023800 .8.@ DCD 1073887232 __arm_cp.0_1 0x0800022c: 07405408 .T@. DCD 121656328 Region$$Table$$Base Region$$Table$$Limit main ** Section #2 '.debug_frame' (SHT_PROGBITS) Size : 68 bytes ** Section #3 '.symtab' (SHT_SYMTAB) Size : 2800 bytes (alignment 4) String table #4 '.strtab' Last local symbol no. 45 ** Section #4 '.strtab' (SHT_STRTAB) Size : 3040 bytes ** Section #5 '.note' (SHT_NOTE) Size : 20 bytes (alignment 4) ** Section #6 '.comment' (SHT_PROGBITS) Size : 1308 bytes ** Section #7 '.shstrtab' (SHT_STRTAB) Size : 84 bytes ======================================================================== ** Object/Image Component Sizes Code (inc. data) RO Data RW Data ZI Data Debug Object Name 168 32 392 0 0 68 _test.axf 168 32 392 0 0 0 ROM Totals for _test.axf
There is nothing. There is no main function!
ARM Compiler version: 6.7 (clang) Optimization: -Oz Common options: --target=arm-arm-none-eabi -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffast-math -ffunction-sections -fdata-sections Additional compiler options: -std=gnu++11 -fno-rtti -fno-exceptions Additional linker options: --strict --scatter=$(SCRIPT) --symbols --list_mapping_symbols
Full makefile project on github: github.com/.../clang-bug
Do a FromELF disassembly of _test.axf
Full listing. Part of the code has been removed to reduce the listing size.
======================================================================== ** ELF Header Information File Name: _test.axf Machine class: ELFCLASS32 (32-bit) Data encoding: ELFDATA2LSB (Little endian) Header version: EV_CURRENT (Current version) Operating System ABI: none ABI Version: 0 File Type: ET_EXEC (Executable) (2) Machine: EM_ARM (ARM) Image Entry point: 0x08000189 Flags: EF_ARM_HASENTRY + EF_ARM_ABI_FLOAT_HARD (0x05000402) ARM ELF revision: 5 (ABI version 2) Conforms to Hard float procedure-call standard Built with Component: ARM Compiler 6.7 Tool: armlink [5c9ef700] Header size: 52 bytes (0x34) Program header entry size: 32 bytes (0x20) Section header entry size: 40 bytes (0x28) Program header entries: 1 Section header entries: 8 Program header offset: 7932 (0x00001efc) Section header offset: 7964 (0x00001f1c) Section header string table index: 7 ======================================================================== ** Program header #0 (PT_LOAD) [PF_X + PF_R + PF_ARM_ENTRY] Size : 560 bytes Virtual address: 0x08000000 (Alignment 4) ======================================================================== ** Section #1 'ROM' (SHT_PROGBITS) [SHF_ALLOC + SHF_EXECINSTR] Size : 560 bytes (alignment 4) Address: 0x08000000 $d.realdata vectors 0x08000000: 20020000 ... DCD 537001984 ... REMOVED ... 0x08000184: 080001cd .... DCD 134218189 $t .ARM.Collect$$$$00000000 .ARM.Collect$$$$00000001 __main _main_stk 0x08000188: f8dfd018 .... LDR sp,__lit__00000000 ; [0x80001a4] = 0x20020000 .ARM.Collect$$$$00000004 _main_scatterload 0x0800018c: f000f80c .... BL __scatterload ; 0x80001a8 .ARM.Collect$$$$00000008 .ARM.Collect$$$$0000000A .ARM.Collect$$$$0000000C __main_after_scatterload _main_clock _main_cpp_init _main_init 0x08000190: 4801 .H LDR r0,[pc,#4] ; [0x8000198] = 0x80001a1 0x08000192: 4686 .F MOV lr,r0 0x08000194: 4801 .H LDR r0,[pc,#4] ; [0x800019c] = 0x8000231 0x08000196: 4700 .G BX r0 $d 0x08000198: 080001a1 .... DCD 134218145 0x0800019c: 08000231 1... DCD 134218289 $t __rt_final .ARM.Collect$$$$0000000D .ARM.Collect$$$$00000010 __rt_final_cpp __rt_final_exit 0x080001a0: f000f814 .... BL ADC_IRQHandler ; 0x80001cc $d .ARM.Collect$$$$00002712 __lit__00000000 0x080001a4: 20020000 ... DCD 537001984 $t .text __scatterload __scatterload_rt2 0x080001a8: 4c06 .L LDR r4,[pc,#24] ; [0x80001c4] = 0x8000230 0x080001aa: 4d07 .M LDR r5,[pc,#28] ; [0x80001c8] = 0x8000230 0x080001ac: e006 .. B 0x80001bc ; __scatterload + 20 0x080001ae: 68e0 .h LDR r0,[r4,#0xc] 0x080001b0: f0400301 @... ORR r3,r0,#1 0x080001b4: e8940007 .... LDM r4,{r0-r2} 0x080001b8: 4798 .G BLX r3 0x080001ba: 3410 .4 ADDS r4,r4,#0x10 0x080001bc: 42ac .B CMP r4,r5 0x080001be: d3f6 .. BCC 0x80001ae ; __scatterload + 6 0x080001c0: f7ffffe6 .... BL __main_after_scatterload ; 0x8000190 $d 0x080001c4: 08000230 0... DCD 134218288 0x080001c8: 08000230 0... DCD 134218288 $t.0 ADC_IRQHandler ... REMOVED ... WWDG_IRQHandler _microlib_exit 0x080001cc: e7fe .. B ADC_IRQHandler ; 0x80001cc 0x080001ce: 0000 .. MOVS r0,r0 Reset_Handler 0x080001d0: b580 .. PUSH {r7,lr} 0x080001d2: 4804 .H LDR r0,[pc,#16] ; [0x80001e4] = 0xe000ed88 0x080001d4: f44f0170 O.p. MOV r1,#0xf00000 0x080001d8: 6001 .' STR r1,[r0,#0] 0x080001da: f000f805 .... BL SystemInit ; 0x80001e8 0x080001de: f7ffffd3 .... BL __main ; 0x8000188 0x080001e2: bf00 .. NOP $d.3 __arm_cp.1_0 0x080001e4: e000ed88 .... DCD 3758157192 $t.0 SystemInit 0x080001e8: 480f .H LDR r0,[pc,#60] ; [0x8000228] = 0x40023800 ... REMOVED ... 0x08000226: 4770 pG BX lr $d.1 __arm_cp.0_0 0x08000228: 40023800 .8.@ DCD 1073887232 __arm_cp.0_1 0x0800022c: 07405408 .T@. DCD 121656328 Region$$Table$$Base Region$$Table$$Limit main ** Section #2 '.debug_frame' (SHT_PROGBITS) Size : 68 bytes ** Section #3 '.symtab' (SHT_SYMTAB) Size : 2800 bytes (alignment 4) String table #4 '.strtab' Last local symbol no. 45 ** Section #4 '.strtab' (SHT_STRTAB) Size : 3040 bytes ** Section #5 '.note' (SHT_NOTE) Size : 20 bytes (alignment 4) ** Section #6 '.comment' (SHT_PROGBITS) Size : 1308 bytes ** Section #7 '.shstrtab' (SHT_STRTAB) Size : 84 bytes ======================================================================== ** Object/Image Component Sizes Code (inc. data) RO Data RW Data ZI Data Debug Object Name 168 32 392 0 0 68 _test.axf 168 32 392 0 0 0 ROM Totals for _test.axf
Ok, so some more opportunities for dead code removal, definitely seems larger than necessary.
If foo uses a volatile loop variable it might distraction the compiler enough to keep it.
Code
is equivalent to the code below
int main( void ) { for (;;); }
So I think that main function has not dead code, it has infinite loop. The second version is compiled correctly.
main 0x08000230: e7fe .. B main ; 0x8000230 0x08000232: 0000 .. MOVS r0,r0
Is it bug of the compiler?
C++11, Lambda?
static const fun_t fun = [] { for (;;); };
Yes, but ARM Compiler 5 and GCC 6.3.1 do it properly.