如何防止 arm-none-eabi 编译器生成主符号

how can I prevent arm-none-eabi compiler generating main symbol

我正在使用 arm-none-eabi 编译源文件。 编译生成elf文件后。我使用 nm 命令得到以下符号

00021da8 T ISR_Init
         U main
         U malloc
010008b0 D MASTER_AHB_MAP

我正在使用 gdb 进行调试,但我遇到 main 未定义符号的问题。 gdb 生成以下错误:

Function "main" not defined.

当我将入口点更改为 main 时,它工作正常。 我正在开发裸机程序,所以我没有在程序中的任何地方定义 main

我将我的程序与以下库相关联

(GNU_ARM_TOOL)/lib/gcc/arm-none-eabi/4.8.4/armv7-ar/thumb/fpu
(GNU_ARM_TOOL)/arm-none-eabi/lib/armv7-ar/thumb/fpu

根据我的理解,main 符号是从上述库之一生成的。我的问题是如何避免编译器生成未定义的符号 main,或者至少删除最终 elf 文件中的未定义的 main 符号以避免 gdb 错误。

为避免 gcc 生成对 main 的引用,link 您的程序使用 -nostdlib gcc option:

-nostdlib: Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines which GCC uses to overcome shortcomings of particular machines, or special needs for some languages. (See Interfacing to GCC Output, for more discussion of libgcc.a.) In most cases, you need libgcc.a even when you want to avoid other standard libraries. In other words, when you specify -nostdlib or -nodefaultlibs you should usually specify -lgcc as well. This ensures that you have no unresolved references to internal GCC library subroutines.

为了避免 gcc 生成对 memcmpmemsetmemcpy 等的调用,请使用 gcc 的 -ffreestanding 选项进行编译。或者使用 "function attributes" 语法,例如:

/* defined in the linker script gcc.ld */
extern int __etext, __data_start__, __data_end__, __bss_start__, __bss_end__;

/* make gcc not translate the data copy loop into a memcpy() call
 *
 * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56888
 * Note that just passing optimize("freestanding", "no-builtin")
 * as a function attribute here doesn't work on
 * gcc-arm-embedded 2014 (gcc 4.9.3) */
__attribute__((optimize("freestanding", "no-builtin",
                        "no-tree-loop-distribute-patterns")))
void Reset_Handler()
{
        int *src, *dst;
        for (src = &__etext, dst = &__data_start__;
                        dst != &__data_end__;
                        src++, dst++)
                *dst = *src;
        for (dst = &__bss_start__; dst < &__bss_end__; dst++)
            *dst = 0;

        main();
}