对于我在Atmel SAM4E16C上的嵌入式应用程序,我需要在.hex文件的末尾放置一个包含固件信息的数组。我正在使用Atmel Studio 7和GCC。
我已经为Atmega168PB做了这个,但不知何故它对这个项目不起作用。
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
SEARCH_DIR(.)
/* Memory Spaces Definitions */
MEMORY
{
rom (rx) : ORIGIN = 0x00420000, LENGTH = 0x000E0000 /* changed to leave space for 128KB Bootloader -> was ORIGIN = 0x00400000, LENGTH = 0x00100000 */
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00020000
}
/* The stack size used by the application. NOTE: you need to adjust according to your application. */
__stack_size__ = DEFINED(__stack_size__) ? __stack_size__ : 0x3000;
__ram_end__ = ORIGIN(ram) + LENGTH(ram) - 4;
/* Firmware Info - 8 Bytes long at the end of ROM */
__FWInfo_start__ = ORIGIN(rom) + LENGTH(rom) - 8;
SECTIONS
{
.text :
{
. = ALIGN(4);
_sfixed = .;
KEEP(*(.vectors .vectors.*))
*(.text .text.* .gnu.linkonce.t.*)
*(.glue_7t) *(.glue_7)
*(.rodata .rodata* .gnu.linkonce.r.*)
*(.ARM.extab* .gnu.linkonce.armextab.*)
/* Support C constructors, and C destructors in both user code
and the C library. This also provides support for C++ code. */
. = ALIGN(4);
KEEP(*(.init))
. = ALIGN(4);
__preinit_array_start = .;
KEEP (*(.preinit_array))
__preinit_array_end = .;
. = ALIGN(4);
__init_array_start = .;
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
__init_array_end = .;
. = ALIGN(0x4);
KEEP (*crtbegin.o(.ctors))
KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
KEEP (*(SORT(.ctors.*)))
KEEP (*crtend.o(.ctors))
. = ALIGN(4);
KEEP(*(.fini))
. = ALIGN(4);
__fini_array_start = .;
KEEP (*(.fini_array))
KEEP (*(SORT(.fini_array.*)))
__fini_array_end = .;
KEEP (*crtbegin.o(.dtors))
KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
KEEP (*(SORT(.dtors.*)))
KEEP (*crtend.o(.dtors))
. = ALIGN(4);
_efixed = .; /* End of text section */
} > rom
/* .ARM.exidx is sorted, so has to go in its own output section. */
PROVIDE_HIDDEN (__exidx_start = .);
.ARM.exidx :
{
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
} > rom
PROVIDE_HIDDEN (__exidx_end = .);
. = ALIGN(4);
_etext = .;
.relocate : AT (_etext)
{
. = ALIGN(4);
_srelocate = .;
*(.ramfunc .ramfunc.*);
*(.data .data.*);
. = ALIGN(4);
_erelocate = .;
} > ram
/* .bss section which is used for uninitialized data */
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = . ;
_szero = .;
*(.bss .bss.*)
*(COMMON)
. = ALIGN(4);
_ebss = . ;
_ezero = .;
} > ram
/* stack section */
.stack (NOLOAD):
{
. = ALIGN(8);
_sstack = .;
. = . + __stack_size__;
. = ALIGN(8);
_estack = .;
} > ram
. = ALIGN(4);
_end = . ;
/* 8 Byte Firmware Info Section */
.FWInfo : AT (__FWInfo_start__)
{
*(.FWInfo)
} > rom
}
这是我正在使用的链接器脚本。我添加了__FWInfo_start__和.FWInfo部分。
在我的应用程序中,我尝试使用属性部分.FWInfo定义固件信息块,但我无法在我的.hex文件中找到数据。
#define SIZE_OF_FWINFO 8
const uint8_t nFirmwareInfoBlock[SIZE_OF_FWINFO] __attribute__((section(".FWInfo"))) = {
0xff, // reserved for future
0xff, // reserved for future
DEVICE_TYPE, // DeviceType
BUILD_NR, // BuildNr of Firmware
VERSION_MINOR, // VersionMinor of Firmware
VERSION_MAJOR, // VersionMajor of Firmware
0xFF, // Checksum
0xFF // Checksum
};
我希望有人可以帮助我,为什么这不起作用。提前致谢。
编辑:以下是.map文件中的条目:
.data 0x00000000 0x0 src/main.o
.FWInfo 0x00000000 0x8 src/main.o
.debug_macro 0x00000000 0x8b0 src/main.o
和..
*fill* 0x200133b0 0x3000
0x200163b0 . = ALIGN (0x8)
0x200163b0 _estack = .
0x200163b0 . = ALIGN (0x4)
0x200163b0 _end = .
.FWInfo
*(.FWInfo)
OUTPUT(Dali4Net.elf elf32-littlearm)
至于我可以从第二个块的上下文中读取,应该有一个在.FWInfo之后写的地址或?
来自海湾合作委员会variable attribute documentation:
used
此属性附加到具有静态存储的变量,意味着即使看起来未引用该变量,也必须发出该变量。
因此,为了防止链接器删除未使用的数据,请使用:
const uint8_t nFirmwareInfoBlock[SIZE_OF_FWINFO]
__attribute__((used,section(".FWInfo"))) =
{ ... } ;
一个替代但可能不太有吸引力的解决方案是声明数组volatile
然后执行虚拟读取,即:
const uint8_t nFirmwareInfoBlock[SIZE_OF_FWINFO]
__attribute__((section(".FWInfo"))) =
{ ... } ;
int main()
{
uint8_t dummy = nFirmwareInfoBlock[0] ;
...
}
第三种方法是完全避免工具链依赖,并使用优秀的,如果有些神秘的SRecord实用程序将数据直接修补为hex文件作为构建后操作。这具有独立于工具链的优点,但是您可能需要一个步骤来生成要修补的数据,但为此编写生成器可能是微不足道的。
我知道这篇文章很老了,不管怎么说......
如前所述,可能需要使用KEEP指令来保持该部分即使未使用,也可以确保无论如何都始终保持该部分。
更重要的是,我想为STM32做类似的事情。似乎段的顺序也很重要(即使明确给出了地址)。我不得不在RAM部分开始之前放置我的额外部分。
在这里,你有一些部分转到ROM > rom
和RAM > ram
。您可能还有一些初始化数据的其他部分,表示为>RAM AT> FLASH
(对于STM32 gcc)。在这个例子中,似乎它被表示为.relocate : AT (_etext)
。
无论如何,如果它以相同的方式工作,你应该在.bss
之前放置你自己的部分,这是未初始化数据的开始(仅与RAM相关的第一部分):
...
_erelocate = .;
} > ram
/* 8 Byte Firmware Info Section */
.FWInfo : AT (__FWInfo_start__)
{
KEEP(*(.FWInfo))
} > rom
/* .bss section which is used for uninitialized data */
.bss (NOLOAD) :
...
如果仍然没有按预期工作,您可以尝试将此部分放在.relocate : AT (_etext)
部分之前;它位于最新提到的rom部分之后,但是由于该部分从ROM重新定位到RAM,即使在符号中有暧昧,我认为第一个猜测应该有效。