检查库内置标志或选项(-frecord-gcc-switches)

问题描述 投票:1回答:1

我想检查所调查的库中使用了哪些标记和选项?

我正在尝试使用带有选项-frecord-gcc-switches的gcc编译器来理解我的简单共享库,但是没有运气。

以下是构成共享库的代码:

#include "shared.h"
unsigned int add(unsigned int a, unsigned int b)
{
    printf("\n Inside add()\n");
    return (a+b);
}

shared.h看起来像:

#include<stdio.h>
extern unsigned int add(unsigned int a, unsigned int b);

我运行以下命令来创建共享库:

gcc -c -Wall -Werror -fPIC -frecord-gcc-switches shared.c
gcc -shared -frecord-gcc-switches -o libshared.so shared.o

readelf -n libshared.so给我

Displaying notes found in: .note.gnu.build-id
  Owner                 Data size       Description
  GNU                  0x00000014       NT_GNU_BUILD_ID (unique build ID bitstring)
    Build ID: 8466741f5849aac95570ec68d172f9077f175f89

如何检查使用了哪些选项/标志或其他库来生成结果库?

我使用的顺便说一句

$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
linux gcc dynamic-library
1个回答
0
投票

选项-frecord-gcc-switches仅在编译时有效,没有联系,因此您可以从以下位置忽略它:

$ gcc -shared -frecord-gcc-switches -o libshared.so shared.o

至少在ELF目标文件中,编译选项被写为字符串插入目标文件的名为.GCC.command.line的部分。默认记录编译选项以及明确给出的选项。

如果将包含此部分的多个目标文件链接到共享库或程序,然后所有输入.GCC.command.line部分合并到一个同名的输出部分中,显然与字符串的重复数据删除有关。所以如果来源不同文件使用不同的选项进行编译,这些选项适用每个源文件可能并不明显。

要从libshared.so中检索记录的选项,请运行:

$ readelf --string-dump=.GCC.command.line libshared.so

输出将类似于:

String dump of section '.GCC.command.line':
  [     0]  -I .
  [     5]  -imultiarch x86_64-linux-gnu
  [    22]  -D_GNU_SOURCE
  [    30]  file1.cpp
  [    3a]  -mtune=generic
  [    49]  -march=x86-64
  [    57]  -Wextra
  [    5f]  -frecord-gcc-switches
  [    75]  -fasynchronous-unwind-tables
  [    92]  -fstack-protector-strong
  [    ab]  -Wformat
  [    b4]  -Wformat-security
  [    c6]  -fstack-clash-protection
  [    df]  -fcf-protection
  [    ef]  file2.cpp
  [    f9]  file3.cpp
  [   103]  -Wpedantic

由于链接没有为该信息添加任何内容,因此直接从目标文件而不是程序中检索它,或者它们链接到的共享库:

$ readelf --string-dump=.GCC.command.line file.o

自那时以来,不会误会哪个编译记录的选项参考。

请注意the manual中的此警告

此开关仅在某些目标上实现,并且录制取决于目标文件和二进制文件格式]

© www.soinside.com 2019 - 2024. All rights reserved.