简体   繁体   English

如何在链接存档文件和所有存档文件时将库版本信息添加到elf文件中有其版本信息?

[英]How to add library version information to elf file while linking archive files and all archive files has their version info?

I am creating an ELF file by linking object files with some archive files. 我通过链接目标文件和一些存档文件来创建ELF文件。 But when I run the "what" command on my ELF file I can see lib info (version) for some archive files only, not all. 但是当我在我的ELF文件上运行“what”命令时,我只能看到一些存档文件的lib信息(版本),而不是全部。

Why does the ELF file have only info for some files even it linked to all archive files? 为什么ELF文件只包含某些文件的信息,即使它链接到所有存档文件? (PS: All archive files have their version info, check in below example) (PS:所有存档文件都有其版本信息,请在下面的示例中查看)

Example: 例:

$ ld -o bos_epb.ppc.elf a.o b.o c.o -L/home/xrava/lib/powerpc \
  --start-group -lgcc -lm -lcrt -lsslcrypto -lssh --end-group

When I run "what" on the ELF file I can see lib info about libcrt only, not all. 当我在ELF文件上运行“what”时,我只能看到有关libcrt的lib信息,而不是全部。

$ what bos_epb.ppc.elf
bos_epb.ppc.elf:
    Lib crt swfp version BL910288
    C Run Time Library
    Built Fri 22 Apr 2016 23:31:21 +0200 by tefo@
    Copyright 2016 XXXXXXX

All my archives have their version info but not writing this to elf except libcrt. 我的所有档案都有他们的版本信息,但除了libcrt之外没有写给elf。

>>what libssh.a 
libssh.a: 
Lib ssh swfp version BL910291 
SSH Built Wed 27 Apr 2016 23:36:24 +0200 by tefo@ 
Copyright 2016 XXXXXXXX

All my archives have their version info but not writing this to elf except libcrt. 我的所有档案都有他们的版本信息,但除了libcrt之外没有写给elf。

In order to understand this result, you need to understand 为了理解这个结果,您需要了解

  1. How the what command works and what命令如何工作和
  2. How the linker works. 链接器的工作原理。

On with the show. 随着节目。 The what command is very simple: it scans an arbitrary binary looking for and ASCII string starting with "special" symbol sequence @(#) and prints any string that follows that sequence (ending with the NUL character). what命令非常简单:它扫描任意二进制文件,并查找以“特殊”符号序列@(#)开头的ASCII字符串,并打印该序列NUL任何字符串(以NUL字符结尾)。 Documentation . 文档

In order for the string @(#) Lib ssh swfp version BL910291 to appear in the linked executable bos_epb.ppc.elf , the object file containing that string must be selected from libssh.a to become part of the executable. 为了使字符串@(#) Lib ssh swfp version BL910291出现在链接的可执行文件bos_epb.ppc.elf ,必须从libssh.a选择包含该字符串的目标文件,以成为可执行文件的一部分。 Which brings us to issue #2 above. 这让我们发现了上面的问题#2。

Just because such an object is present in libssh.a , you can't assume that it will be linked into the final binary. 仅仅因为libssh.a存在这样的对象,您不能假设它将链接到最终的二进制文件中。 The algorithm that the linker uses to decide whether to include an object into the final executable or not is described here or here . 此处此处描述了链接器用于决定是否将对象包括到最终可执行文件中的算法。

You can garantee that the entire libssh.a is included in the final binary by using -Wl,--whole-archive -lssh -Wl,--no-whole-archive , but this is ill -advised. 你可以保证整个libssh.a包含在最后的二进制文件中,使用-Wl,--whole-archive -lssh -Wl,--no-whole-archive ,但这是不明智的 It may cause your binary to fail to link, and is guaranteed to make it larger than it should be. 它可能导致您的二进制文件无法链接,并保证使其大于它应该的大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM