Extracting archive file in linker script

ぐ巨炮叔叔 提交于 2021-02-19 06:47:06

问题


I am trying to deal with a problem like the following one:

Assume that I have a library libxyz.a created from:

/* main.c */
int main(void)
{
  int a;
}

compiled and archived with:

gcc -c main.c -o abc.o && ar cr libxyz.a abc.o

How do I have to write linker script in order to put abc.o exactly where it is expected to be?

I was trying to handle it in such way:

/* script.ld */
SEARCH_DIR(.)
INPUT(-lxyz)
SECTIONS
{
   .text : { xyz:abc(.text) }
   .data : { xyz:abc(.data) }
   .bss  : { xyz:abc(.bss) }
}

but after running:

ld -T script.ld

I get:

ld: cannot find xyz:abc

I couldn't find any example of extracting archives in linker files on forums. The only thing I have found was the linker's documentation, which only contains information about archive:file construction.


回答1:


Just had to deal with same problem today, so if someone else bumps into this question: some file extensions are missing. Something like works fine:

SECTIONS
{
   .text : { libxyz.a:abc.o(.text) }
   .data : { libxyz.a:abc.o(.data) }
   .bss  : { libxyz.a:abc.o(.bss) }
}

One also needs to deal with paths (if library is actually somewhere/libxyz.a).




回答2:


Probably there is problem with your ld because I tried your script on my machine it ran correctly without error. my ld version is GNU ld (GNU Binutils for Ubuntu) 2.22



来源:https://stackoverflow.com/questions/14215958/extracting-archive-file-in-linker-script

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