如何从共享库中导出符号 [英] How to export symbols from a shared library

查看:28
本文介绍了如何从共享库中导出符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 主机上使用 RVDS 编译器使用 *.o 目标代码文件(C 源代码)创建了一个共享库(*.so).

I created a shared library (*.so) using the *.o object code files (C source code) using RVDS compiler on Windows Host.

我将此共享对象与应用程序链接(使用 gcc 用于 Linux 主机上的 ARM 目标)并获得一个可执行文件,该文件在运行时会产生分段错误.(我知道我必须调试它!)

I link this shared object with a application (using gcc for ARM target on Linux host) and obtain a executable, which on running generates segmentation fault. (I know I have to debug it!)

如果我创建一个具有相同源文件的静态库,然后与应用程序链接,然后执行应用程序,它会按预期正常工作,而不是创建共享库.

Instead of creating shared library, if I create a static library with same source files, and then link with the application, and then execute the application it works fine as expected.

所以我的问题是:

  1. 我是否需要使用某些构造在我的源文件中明确导出符号(导出到应用程序的函数)或任何其他符号,以便在与应用程序链接时正常工作?需要什么,我该怎么做?

  1. Do I need to export symbols(functions exported to application) or any other symbols, explicitly, in my source file using some constructs so that it works fine when linked with an application? What is needed and how do I do that?

共享库是如何工作的?即函数将被加载和运行的地址将在创建库时在库中给出.应用程序( main() )如何解析要执行库函数的地址?

How does shared library work?, i.e. will the addresses where the functions will be loaded and run, will be given in the library be given when library is created. How does the application ( main() ) resolve the addresses where the library functions are to be executed?

静态库是如何工作的,即如果是静态库,这个地址规范和解析是如何发生的?

How does a static library work, i.e. how does this address specification and resolving happen in case of static library?

推荐答案

这是它在 linux 上的工作方式:

This is how it works on linux:

1) 不,你什么都不用做.但是,您可以使用 gcc -fvisibility 命令行参数限制导出变量,并使用可见性属性显式标记导出的条目.

1) No, you needn't do anything. You can, however, restrict exporting variables with gcc -fvisibility command line argument and explicitly flag exported entries with the visibility attribute.

2) 可执行文件将有一个包含它导入的所有函数的表(这些都是具有默认可见性的函数).加载器/链接器将选择一个地址来加载库并在运行之前填充该表,对这些函数的调用是间接调用.(请注意,这也适用于共享对象)

2) The executable will have a table of all functions it imports (these are all functions with default visibility). The loader/linker will pick an address to load the libraries to and fill this table just before running, the calls to those functions are indirect calls. (Note that this holds for shared objects as well)

3) 静态链接是在链接时(即编译之后)执行的.实际地址在程序集中被替换,它们是直接调用.

3) Static linking is performed on link-time (which is after you compile). The actual addresses are substituted in the assembly, and they are direct calls.

注意:有一种叫做PIC(位置无关代码)的东西.AFAIK,这处理对同一共享对象中的数据/函数的引用,因此链接器在加载库时无需覆盖库的一半代码,因为代码不会对其进行任何绝对引用自己的数据.您可以尝试尝试一下.

Note: There is the thing called PIC (position independent code). AFAIK, this deals with references to data/functions in the same shared object, so the linker needn't overwrite half of the code of the library when loading the library, in the way that the code doesn't make any absolute references to its own data. You might try to experiment with it.

这篇关于如何从共享库中导出符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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