jw_
jw_

Reputation: 1785

How to know the visibility of a symbol in an object file

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.

Upvotes: 1

Views: 1070

Answers (3)

naituw
naituw

Reputation: 1147

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.

Upvotes: 0

Aura
Aura

Reputation: 41

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

Upvotes: 0

walnut
walnut

Reputation: 22152

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).

Upvotes: 3

Related Questions