Check library built flags or options (-frecord-gcc-switches)

1

I'd like to check the library under investigation what flags and options were built with?

I'm trying to use gcc compiler with option -frecord-gcc-switches to understand on my simple shared library but with no luck.

The following is the piece of code that form a shared library :

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

shared.h looks like :

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

I run the following commands to create a shared library :

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

The readelf -n libshared.so gives me

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

How to check what options/flag or additional libraries were used to build the resulting lib?

BTW I use

$ 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
asked on Stack Overflow May 26, 2020 by user3428154 • edited May 26, 2020 by user3428154

1 Answer

0

The option -frecord-gcc-switches is effective only in compilation, not in linkage, so you may omit it from:

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

In ELF object files at least, the compilation options are written as a list of strings into a section of the object file called .GCC.command.line. Default compilation options are recorded as well those explicitly given.

If multiple object files containing this section are linked into a shared library or program then all the input.GCC.command.line sections are merged into one output section of the same name, apparently with deduplication of the strings. So if different source files were compiled with different options, the options that applied to each source file may not be obvious.

To retrieve the recorded options from libshared.so, run:

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

Output will be similar to:

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

Since linkage adds nothing to this information, it makes better sense to retrieve it straight from the object files rather than the program or shared library they are linked into:

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

since then there can be no mistaking which compilation the recorded options refer to.

Note this caveat in the manual

This switch is only implemented on some targets and the exact format of the recording is target and binary file format dependent

answered on Stack Overflow May 26, 2020 by Mike Kinghan • edited May 27, 2020 by Mike Kinghan

User contributions licensed under CC BY-SA 3.0