Linker Script: Put a particular file at a later position

浪尽此生 提交于 2019-12-23 06:06:38

问题


I'd like to write a linker script looking something like this:

SECTIONS {
  . = 0x0;
  .startup . : { startup.o(.text) }
  .text : { *(.text) }
  .data : { *(.data) }
  .bss : { *(.bss COMMON) }
  . = 0x4000;
  other.text : { other.o(.text) }
  other.data : { other.o(.data) }
  other.bss : { other.o(.bss) }
}

My intention here is to have, in this order:

  • a section with the .text from startup.o
  • .text, .data and .bss containing those sections from all other input files besides other.o
  • the .text, .data and .bss sections from other.o

Of course there is a problem with the script I've given: other.o is included in the * wildcards used previously, so it doesn't get put in the output section other.

Besides manually listing all the input object files bar other.o in place of the *s, is there a way I can achieve what I want here?


回答1:


You can use section attributes to solve this. Suppose you add the following attribute when declaring functions in that file (or any other file):

void foo() __attribute__ ((section(".specialmem")));

And a similar section definition in your linker script:

.specialmem:
{
    *(.specialmem)
}

You can do the same thing with data/bss (global variables) as well. Assuming you want some files/functions to end up in a specific memory location, it is a good practice to define these memory blocks in your linker file as well and then place them there like:

.specialmem:
{
    *(.specialmem)
} >specialMemBlock



回答2:


You may want to try EXCLUDE_FILE

Something like this: *(EXCLUDE_FILE (other.o) .text .text. )




回答3:


Partial solution: * wildcards are fully-fledged file wildcards. If you're lucky enough that other.o is in a different directory from the other input files, this will work:

SECTIONS {
  . = 0x0;
  .startup . : { foo/startup.o(.text) }
  .text : { foo/*(.text) }
  .data : { foo/*(.data) }
  .bss : { foo/*(.bss COMMON) }
  . = 0x4000;
  other.text : { bar/other.o(.text) }
  other.data : { bar/other.o(.data) }
  other.bss : { bar/other.o(.bss) }
}


来源:https://stackoverflow.com/questions/21418593/linker-script-put-a-particular-file-at-a-later-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!