【问题标题】:How can I get the architecture of a '.a' file?如何获得“.a”文件的架构?
【发布时间】:2011-04-14 00:19:01
【问题描述】:

我有一个 .a 文件,我想从中获取架构信息。运行file myFile.a 会产生file.a: current ar archive。如何获得有关文件包含的架构的更多信息?

【问题讨论】:

    标签: linux elf


    【解决方案1】:

    您也可以跳过ar 命令并使用readelf,通过以下方式:

    readelf -h <archive>.a | grep 'Class\|File\|Machine'

    [00:32:15] /usr/lib $ readelf -h libxslt.a | grep 'Class\|File\|Machine'
    File: libxslt.a(attrvt.o)
      Class:                             ELF32
      Machine:                           Intel 80386
    File: libxslt.a(xslt.o)
      Class:                             ELF32
      Machine:                           Intel 80386
    ... #Trimmed this, it goes on a bit
    File: libxslt.a(transform.o)
      Class:                             ELF32
      Machine:                           Intel 80386
    File: libxslt.a(security.o)
      Class:                             ELF32
      Machine:                           Intel 80386
    [00:32:24] /usr/lib $
    

    如果相关,这里是您可以从readelf -h 获得的其他信息。我只是用grep 修剪了上面的内容,显然:

    File: libxslt.a(security.o)
    ELF Header:
      Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
      Class:                             ELF32
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              REL (Relocatable file)
      Machine:                           Intel 80386
      Version:                           0x1
      Entry point address:               0x0
      Start of program headers:          0 (bytes into file)
      Start of section headers:          2548 (bytes into file)
      Flags:                             0x0
      Size of this header:               52 (bytes)
      Size of program headers:           0 (bytes)
      Number of program headers:         0
      Size of section headers:           40 (bytes)
      Number of section headers:         16
      Section header string table index: 13
    

    该输出是针对libxslt.a 中的一个 对象文件,但它为每个文件提供了相同的信息。

    【讨论】:

    • 建议将Machine 添加到那里的列表中。这将包括有关创建目标文件的特定 CPU 的信息,而不仅仅是它是 32 位还是 64 位或其他。
    • @cHao:是的,我也是这么想的。我正在做另一个编辑(我添加了readelf -h 的整个输出)。
    • 谢谢!这正是我想要的。
    【解决方案2】:

    使用

    lipo -info libExample.a

    它将为谁构建架构。 otool 或 file 等其他函数没有给出确切的答案,有时它会很冗长以获取正确的信息。

    【讨论】:

    • lipo 似乎是特定于 OS X 的。
    【解决方案3】:

    objdump 是另一种选择:

    objdump -a file.a|grep 'file format'
    

    【讨论】:

    • 无法通过此解决方案获取架构信息(例如arm、x86等)。
    【解决方案4】:

    http://linux.die.net/man/1/ar

    从存档中提取目标文件并使用 file(1)、nm(1) 等检查它们。

    【讨论】:

    • 有没有办法在不提取其内容的情况下获取架构信息(至少不留下任何残留物)?
    • @jer,实际上,您可以使用readelf 或objdump。
    • @Matthew,对不起,我的思绪处于 OSX/iOS 模式,我整晚都在回答这些问题,但没有仔细阅读标签。是的,你是对的。 osx 的开发工具中都不存在这些工具(无论如何,在那个平台上没有任何意义)。
    【解决方案5】:

    我建议使用 objdump 而不是 lipo。 objdump 提供的信息比 lipo 更详细。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 2019-12-30
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多