GCC链接器 – 在.text部分内的特定地址定位一个部分/常量。

huangapple go评论54阅读模式
英文:

GCC Linker - Locate a section/constant at a specific address within the .text section

问题

以下是您要翻译的内容:

"I would like to locate a 32bit constant value at a specific address (0x080017FC) within the .text (code) section.

To be honest, when it comes to modifying the linker script to this extent I'm naïve and feel like I do not have a clue what to do.

I've modified my linker script to contain this new section (.systemid) within the .text section.

  .text :
  {    
    . = ALIGN(4);
    KEEP(*(.systemid))
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)

    KEEP (*(.init))
    KEEP (*(.fini))

    . = ALIGN(4);
    _etext = .;        /* define a global symbols at end of code */
  } >FLASH

To ensure it does not get optimized away, I used KEEP.

I then declared my constant in the new section (.systemid). This is where I start to wonder what am I supposed to do. If .systemid was a section on its own, I would have declared the constant as follows:

const uint32_t __attribute__((used, section (".systemid"))) SYSTEM_ID_U32 = 0x11223344;

But since this is a section within a section, should it not be?:

uint32_t __attribute__((used, section (".text.systemid"))) SYSTEM_ID_U32 = 0x11223344;

So the linker will locate the constant at the beginning of the .text section (0x000001A0). Great, it is inside the text section but not at the correct address. I would like to locate the constant at 0x08001F7C.

To try and achieve this, I pass the following to the linker:

 -Wl,--section-start=.text.systemid=0x080017FC

Again I'm not sure if it should be .systemid or .text.systemid

Either way, it does not locate the constant at 0x080017FC

How do I get my constant to be located at 0x080017FC within the .text (code) section without any overlap errors?"

英文:

I would like to locate a 32bit constant value at a specific address (0x080017FC) within the .text (code) section.

To be honest, when it comes to modifying the linker script to this extent I'm naïve and feel like I do not have a clue what to do.

I've modified my linker script to contain this new section (.systemid) within the .text section.

  .text :
  {    
    . = ALIGN(4);
    KEEP(*(.systemid))
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)

    KEEP (*(.init))
    KEEP (*(.fini))

    . = ALIGN(4);
    _etext = .;        /* define a global symbols at end of code */
  } >FLASH

To ensure it does not get optimized away, I used KEEP.

I then declared my constant in the new section (.systemid). This is where I start to wonder what am I supposed to do. If .systemid was a section on its own, I would have declared the constant as follows:

const uint32_t __attribute__((used, section (".systemid"))) SYSTEM_ID_U32 = 0x11223344;

But since this is a section within a section, should it not be?:

uint32_t __attribute__((used, section (".text.systemid"))) SYSTEM_ID_U32 = 0x11223344;

So the linker will locate the constant at the beginning of the .text section (0x000001A0). Great, it is inside the text section but not at the correct address. I would like to locate the constant at 0x08001F7C.

To try and achieve this, I pass the following to the linker:

 -Wl,--section-start=.text.systemid=0x080017FC

Again I'm not sure if it should be .systemid or .text.systemid

Either way, it does not locate the constant at 0x080017FC

How do I get my constant to be located at 0x080017FC within the .text (code) section without any overlap errors?

答案1

得分: 1

这种方式行不通。我不知道有什么方法可以在不引起链接器问题的情况下将部分放置在特定地址。链接器是一个相当简单的程序,不会优化内存以避免您指定的位置。

我使用两种方法:

  1. 将此ID放在FLASH的末尾。不能在开头这样做,因为那里有向量表。
const uint32_t __attribute__((used, section (".systemid"))) SYSTEM_ID_U32 = 0x11223344;

在FLASH的所有其他部分之后放置(可以是最后一个部分定义)

.systemid :
{
  . = ORIGIN(FLASH) + LENGTH(FLASH) - 4;
  KEEP(*(.systemid))
} >FLASH

或者

.systemid ORIGIN(FLASH) + LENGTH(FLASH) - 4:
{
  KEEP(*(.systemid))
} >FLASH
英文:

It will not work this way. There is no way I am aware of placing section at the particular address without problems from the linker if it is part of another section. Linker is quite a simple program and will not optimize the memory to avoid your location.

I use two methods:

  1. Place this id at the end of the FLASH. You cant do this at the beginning as there is the vector table.
const uint32_t __attribute__((used, section (".systemid"))) SYSTEM_ID_U32 = 0x11223344;

Place after all other sections in FLASH (it can be the last section definition

  .systemid :
  {
    . = ORIGIN(FLASH) + LENGTH(FLASH) - 4;
    KEEP(*(.systemid))
  } >FLASH

or

  .systemid ORIGIN(FLASH) + LENGTH(FLASH) - 4:
  {
    KEEP(*(.systemid))
  } >FLASH

huangapple
  • 本文由 发表于 2023年2月19日 01:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定