包含Linux GCC链接器 [英] Includes with the Linux GCC Linker

查看:103
本文介绍了包含Linux GCC链接器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白GCC在Linux下的工作原理。在源文件中,当我做一个时:

  #include< math.h> 

编译器是否提取相应的二进制代码并将其插入编译的可执行文件,或者编译器是否插入引用外部二进制文件(a-la Windows DLL?)

我猜这个问题的一个通用版本是:在* nix下有没有与Windows DLL相同的概念?

解决方案

好吧。当包含 math.h 时,编译器将读取包含可用函数和宏声明的文件。如果您调用在该文件中声明的函数( header ),那么编译器会将一个调用指令插入到您的目标文件中的该地方,该文件将由您编译的文件创建(我们称之为 test.c 和创建的对象文件 test.o )。它还会在该对象文件的重定位表中添加条目:

 重定位部分'.rel.text'偏移量为0x308包含1个条目:
偏移信息类型Sym.Value Sym。名称
0000001c 00000902 R_386_PC32 00000000 bar

这将是一个功能栏的重定位条目。在符号表中的一个条目将被标记为函数尚未定义:

  9:00000000 0 NOTYPE GLOBAL DEFAULT UND bar 

链接 test.o 目标文件到程序中,您需要链接到名为 libm.so 的数学库。 so 扩展名与Windows的 .dll 扩展名相似。这意味着它是共享对象文件。编译器在链接时将修正出现在 test.o 重定位表中的所有位置,并将其条目替换为bar函数的正确地址。根据你使用库的共享版本还是静态库(它被称为 libm.a ),编译器会在编译之后或之后进行修复,在运行时,当你真正开始你的程序。完成后,它将在该程序所需的共享库表中注入一个条目。 (可显示为 readelf -d ./test ):

 偏移量为0x498的动态部分包含22个条目:
标签类型名称/值
0x00000001(需要)共享库:[libm.so.6]
0x00000001(需要)共享库:[libc。 so.6]
... ... ...

现在,如果您启动你的程序,动态链接器将查找该库,并将该库链接到你的可执行映像。在Linux中,执行此操作的程序称为 ld.so 。静态库在动态部分中没有位置,因为它们只是链接到其他对象文件,然后它们被遗忘;他们是从那时起的可执行文件的一部分。



实际上它实际上要复杂得多,我也不明白这一点。尽管这是粗略的计划。

I don't understand how GCC works under Linux. In a source file, when I do a:

#include <math.h>

Does the compiler extract the appropriate binary code and insert it into the compiled executable OR does the compiler insert a reference to an external binary file (a-la Windows DLL?)

I guess a generic version of this question is: Is there an equivalent concept to Windows DLLs under *nix?

解决方案

Well. When you include math.h the compiler will read the file that contains declarations of the functions and macros that can be used. If you call a function declared in that file (header), then the compiler inserts a call instruction into that place in your object file that will be made from the file you compile (let's call it test.c and the object file created test.o). It also adds an entry into the relocation table of that object-file:

Relocation section '.rel.text' at offset 0x308 contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000001c  00000902 R_386_PC32        00000000   bar

This would be a relocation entry for a function bar. An entry in the symbol table will be made noting the function is yet undefined:

9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND bar

When you link the test.o object file into a program, you need to link against the math library called libm.so . The so extension is similar to the .dll extension for windows. It means it is a shared object file. The compiler, when linking, will fix-up all the places that appear in the relocation table of test.o, replacing its entries with the proper address of the bar function. Depending on whether you use the shared version of the library or the static one (it's called libm.a then), the compiler will do that fix-up after compiling, or later, at runtime when you actually start your program. When finished, it will inject an entry in the table of shared libraries needed for that program. (can be shown with readelf -d ./test):

Dynamic section at offset 0x498 contains 22 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 ... ... ...

Now, if you start your program, the dynamic linker will lookup that library, and will link that library to your executable image. In Linux, the program doing this is called ld.so. Static libraries don't have a place in the dynamic section, as they are just linked to the other object files and then they are forgotten about; they are part of the executable from then on.

In reality it is actually much more complex and i also don't understand this in detail. That's the rough plan, though.

这篇关于包含Linux GCC链接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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