【发布时间】:2015-01-15 11:13:10
【问题描述】:
我希望标题足够描述性。所以这就是我想要做的和我玩过的。
等等,首先,这是一个嵌入式应用程序。 Atmel SAM4L 微控制器,使用 Atmel Studio IDE 和 GCC 编译器/链接器。
是的,我正在编写一个引导加载程序,但想将引导加载程序版本保存到程序内存到引导加载程序分配空间的末尾(比如说 0x3FF0)。这样,应用程序还可以通过查看特定地址来检查引导加载程序版本。 目前我正忙于应用程序更新引导加载程序本身的实用程序,但我不希望应用程序或引导加载程序使用命令或代码更新 0x3FF0 处的版本,我希望它作为 . bin/.hex 文件,所以当我打开引导加载程序时,版本也会随之闪烁。 所以目前我有一个用于引导加载程序类型、主要版本和次要版本的#define,它们都在项目的 globals.h 文件中。基本上我只想在点击编译时将这 3 个字节写入 0x3FF0。
据我所知,我可以使用链接器获得很多技巧,但直到昨天才玩过链接器脚本,并且已经能够用它做一些事情,但还不是我想要的。 该项目还创建了一个非常密集的链接器脚本,所以我也有点担心在哪里跳转和转储我的三个字节。我知道你不能把地址指针往回移动。
这是项目生成的链接描述文件
/**
* \file
*
* \brief Flash Linker script for SAM.
*
* Copyright (c) 2013 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
SEARCH_DIR(.)
/*
* NOTE: to keep binary compatibility with SAM4L4 device on SAM4L Xplained Pro,
* we use SAM4L4 memory space here instead SAM4L8. You may change it if you are
* using SAM4L8 device.
*/
/* Memory Spaces Definitions */
MEMORY
{
rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000 /* flash, 256K */
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000 /* sram, 32K */
/* rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00080000 */ /* flash, 512K */
/* ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00010000 */ /* sram, 64K */
}
/* The stack size used by the application. NOTE: you need to adjust according to your application. */
__stack_size__ = DEFINED(__stack_size__) ? __stack_size__ : 0x1000;
__ram_end__ = ORIGIN(ram) + LENGTH(ram) - 4;
/* Section Definitions */
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 = . ;
}
据我了解,.ARM.exidx 是放在 ROM 中的最后一部分,.relocate 将根据区域和 MEMORY 声明放在 RAM 中(从 0x20000000 开始),所以我的三个字节应该介于两者之间那两个。
然后,至于如何,我已经尝试过这个例子 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0803a/BABDJCAA.html 但我没有看到它反映在我的 .bin 或 .hex 文件中。我猜它只分配内存,实际上并没有加载任何东西,因为它只是一个变量。 我也发现了类似的东西 gcc linker description file force symbol to be at specific address ,但我没有,因为它不是我试图加载到特定地址的实际代码,所以我不知道如何使用这种方法。
我仍在尝试操作链接器脚本并查看我可以实现的目标,但我们将不胜感激。
如果需要任何进一步的信息,请询问,我会提供。 (或者如果我需要更改标题或标签以获得更好的点击率。)
【问题讨论】:
-
我不知道 gcc 是如何处理这个问题的,但是对于其他工具链来说,函数和变量之间并没有太大的区别。所以通常放置都是以同样的方式完成的。
-
此信息的最佳参考是The ld Manual (sourceware)。对于嵌入式系统或程序集本身,了解
ld与了解编译器本身一样重要。看看你是否可以缩小你的问题范围。 -
谢谢,我去看看。
标签: c gcc linker embedded microcontroller