将外部静态库的段放置到特定位置 [英] Place segments of external static library to specific locations

查看:484
本文介绍了将外部静态库的段放置到特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序调用了一些放置在外部静态库中的函数.我将外部静态库链接到我的应用程序,并且一切正常(在这种情况下,我使用的是GCC).

My application calls some functions which are placed in an external static library. I link the external static library to my application and everything works (in this case I'm using GCC).

尽管如此,库的文本,.data和.bss节的位置(地址)是由链接器选择的.我可以通过修改链接描述文件来选择/更改它们的位置,但这很麻烦,因为我必须指定库的所有函数,变量等.我的意思是这样的:

Nevertheless, the locations (addresses) of text, .data and .bss sections of the library are chosen by the linker. I can choose/change their locations by modifying the linker script, but it's tedious as I have to specify all the functions, variables, etc. of the library. What I mean it's something like:

. = 0x1000; /* new location */
KEEP(*(.text.library_function1));
KEEP(*(.text.library_function2));
[...]

另一种解决方案是通过为每个函数/变量放置一个 section属性来构建外部库,然后通过重新放置整个节来修改链接器.像这样:

An alternative solution is to build the external library by placing a section attribute for each function/variable, and then modifying the linker by re-locating the whole section. Something like:

/* C source file */
unsigned char __attribute__((section (".myLibrarySection"))) variable1[10];
unsigned char __attribute__((section (".myLibrarySection"))) variable2[10];

/* Linker script */
. = 0x1000;
KEEP(*(.myLibrarySection))

但是,我希望能够重定位外部静态库的整个.text,.data和.bss段,而无需使用这些技巧.

However, I'd like to be able to relocate entire .text, .data and .bss segments of an external static library without the need of using these tricks.

我想要这样的东西(在链接脚本中):

I'd like something like this (in linker script):

. = 0x1000;
KEEP(*(.text.library_file_name))

是否可以使用GCC工具链?

Is it possible using GCC toolchain?

是否可以使用其他工具链(IAR,Keil等)?

Is it possible using other toolchains (IAR, Keil, etc.)?

推荐答案

您可以使用 archive:filename 语法.

You can use the archive:filename syntax in ld.

首先将外部库中的所有.o文件放入静态库.a文件(如果尚未创建).这是分发静态库二进制文件的正常方法.

First place all the .o files from your external library into a static library .a file, if they aren't already. That is the normal way static library binaries are distributed.

然后在链接描述文件中指定:

Then in the linker script, specify:

.text.special : {
    . = 0x1000;
    *libspecial.a:*(.text .text.*)
}

.text {
    *(.text .text.*)
}

通配符将选择所有来自libspecial.a的文件,并将它们放在第一部分中.稍后的通配符将选择剩余的任何内容.如果需要将.text.special部分放在普通部分之后,则可以类似的方式使用EXCLUDE_FILE指令.

The wildcard will pick all the files coming from libspecial.a and place them in the first section. The later wildcard will then pick anything left over. If there is a need to place the .text.special section after the normal section, you can use EXCLUDE_FILE directive in a similar way.

这篇关于将外部静态库的段放置到特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆