0

The visibility (from __ attribute __(visibility("...")) and -fvisibility) of a symbol can be known from a so file

nm -C lib.so

t is hidden, T is exported(i.e. default). But how to get this information from an object file directly?

nm -C lib.o

Will always print T for non C-static symbols whatever the visibility is.

walnut
  • 21,629
  • 4
  • 23
  • 59
jw_
  • 1,663
  • 18
  • 32

3 Answers3

2

The visibility is different from whether the symbol is local or global (which is what the lower-case/upper-case letters describe). A hidden symbol still can have external linkage, i.e. it is not limited to a translation unit.

I don't think nm has an option to show visibility, but you can use either

objdump -Ct lib.o

which will show an attribute .hidden if the symbol is hidden or

readelf -s lib.o

which has a column for the visibility (DEFAULT/HIDDEN).

walnut
  • 21,629
  • 4
  • 23
  • 59
0

I think visibility is a concept for shared library, not for object file.

The following is a test to verify it.

//math.cpp
__attribute__((visibility("default"))) int bbbb;
__attribute__((visibility("hidden"))) int aaaa;
  • If we compile it to a object file, the two symbol are global(the S is upper case to indicate that it is global)
> g++ -c math.c
> nm math.o
000000000000007c S _aaaa
0000000000000078 S _bbbb
  • If we compile it to a shared library, we will see the hidden symbol(aaaa) is marker as local.
> g++ -shared -fPIC -o libmath.so math.cpp
> nm libmath.so
0000000000004004 s _aaaa
0000000000004000 S _bbbb
Aura
  • 31
  • 3
0

If you work on Apple platforms, and objdump can't recognize your object file (file format not recognized), It may due to the object file is actually bitcode file.

You can try llvm-nm, which supports more formats including bitcode files:

llvm-nm main.o

or

xcrun llvm-nm main.o

if you have Xcode installed.

naituw
  • 1,087
  • 10
  • 14