Collective Library Symbol Visibility Limit on Solaris

With GCC (on Linux), I can easily limit the visibility of characters in shared libraries.

What are my options on Solaris (10)?

Are these GCC features supported with GCC on Solaris (especially with GCC that uses the Solaris linker / assembler)?

And does the Solaris Studio C compiler / linker do similar attributes / pragmas to control the visibility of characters (i.e., to set the default to hidden and explicitly designating characters as visible)?

+4
source share
3 answers

Cannot respond to gcc visibility in Solaris, but

Sun Studio has

-xldscope compiler option and _global / _hidden code attributes

according to http://lists.qt.nokia.com/public/qt-interest/2010-January/017893.html

http://qt.gitorious.org/qt/qt/merge_requests/433 :

SunStudio since version 8 (CC version 5.5) has the ability to control the visibility of characters in ABI through the linker option

-xldscope=[global|symbolic|hidden] 

and using the __global, __symbolic, and __hidden attributes of the code.

 -xldscope=global maps to GCC -fvisibility=default -xldscope=symbolic maps to GCC -fvisibility=protected -xldscope=hidden maps to GCC -fvisibility=hidden __global maps to GCC __attribute__((visibility("default")) __symbolic maps to GCC __attribute__((visibility("protected")) __hidden maps to GCC __attribute__((visibility("hidden")) 

And there is a review from Sun: http://www.oracle.com/technetwork/server-storage/solaris/symbol-scope-140496.html

Character Reduction with Sun Studio C / C ++ by Giri Mandalika, May 2005 (Revised March 22, 2006)

+2
source

I tested some annotation annotation methods on a Solaris 10 machine. Surprisingly, the Solaris Studio C compiler also supports the GCC hidden function attribute.

GCC configured with Solaris as / ld correctly implements the visibiltiy function attribute.

Therefore, using the GCC function attribute syntax should be the most convenient / portable method since it works on Linux / GCC, Solaris / GCC, and Solaris / Sol-Studio.

The table below provides an overview of the effects that hide the visibility of a function.

results

  .dynsym | .symtab
 System Compiler Visibility nm readelf link-error
                                                      elfdump
 ―――――――――――――――――――――――――――――――――――――――――――――――――― ―――――――――――――――――――――――――――――――
 Linux Fedora 17 gcc-4.7.2 not specified T GLOBAL DEFAULT no
 Linux Fedora 17 gcc-4.7.2 attr-hidden t - | LOCAL DEFAULT yes
 Solaris 10 gcc-4.8 not specified GLOB GLOB D no
 Solaris 10 gcc-4.8 attr-hidden LOCL - | LOCL H yes
 Solaris 10 cc-12.3 attr-hidden LOCL - | LOCL H yes
 Solaris 10 cc-12.3 __hidden LOCL - | LOCL H yes

Methods

main.c:

 #include <stdio.h> #include <stdlib.h> #include "power3.h" #include "power2.h" int main(int argc, char **argv) { printf("Result: %d\n", power3(atoi(argv[1]))); // should result in a link error when symbol is hidden printf("Result: %d\n", power2(atoi(argv[1]))); return 0; } 

power2.h:

 #ifndef POWER2_H #define POWER2_H #if !defined(NO_HIDE) #if defined(__GNUC__) || defined(FORCE_GCC) __attribute__((visibility("hidden"))) #warning Using GNU-C function attribute #elif defined(__SUNPRO_C) __hidden #warning Using SUNPRO-C qualifier #endif #endif int // GCC attribute also possible here power2(int x); #endif 

power3.h:

 #ifndef POWER3_H #define POWER3_H int power3(int x); #endif 

power3.c

 #include "power3.h" #include "power2.h" int power3(int x) { return power2(x)*x; } 

Build Commands:

 cc -g -c -o main.o main.c cc -g -fpic -c -o power3.po power3.c cc -g -fpic -c -o power2.po power2.c cc -shared -fpic -o libpower.so power3.po power2.po cc -L$PWD -Wl,-R$PWD 

Introspection:

On Linux:

 nm libpower.so | grep power readelf --dyn-sym libpower.so | grep power readelf -s libpower.so | grep 'FUNC.*power' 

In Solaris:

 /usr/ccs/bin/nm libpower.so | grep 'FUNC.*power' /usr/ccs/bin/elfdump -N .dynsym libpower.so | grep 'FUNC.*power' elfdump -N .symtab libpower.so | grep 'FUNC.*power' 

System Information:

The Solaris 10 system is a SPARC machine, and GCC uses as / ld from /usr/ccs/bin . Solaris Studio version 12.3 hotfix (2013/02/04).

Sources

Global switches

For completeness of visibility of functions (and other symbols) other means may also influence:

  GCC-method Sol equivalent effect
 ―――――――――――――――――――――――――――――――――――――――――――――――――― ――――――――――――――――――――――――――――――――――――
 #pragma GCC visibility push (hidden) - everything between push / pop
 #pragma GCC visibility pop - has default visibility hidden
 #pragma GCC visibility push (default) - ~ default to default-visibility
 #pragma GCC visibility pop -
 -fvisibility = hidden -xldscope = hidden sets default visibility of
 -fvisibility = default -xldscope = global a translation unit

The ELF standard also defines the internal and protected visibility of characters, which are also understood by compilers, but which are generally less useful.

+4
source

Another option is to use script version files. This works on Linux, Solaris / GCC, and Solaris / CC.

Example

Consider a shared library where only the power3() function is available. It uses power2() , which is defined in a different translation unit, and power2() must be hidden.

The next version of the script states the following:

 $ cat vscript { global: power3; local: *; }; 

You can use one file to connect to Linux and Solaris - Linux / Solaris seems to understand the same syntax constructs.

Build Commands

  System compiler link command
 ―――――――――――――――――――――――――――――――――――――――――――――――――― ―――――――――――――――――――――――――――――――――――――――――――
 Linux Fedora 17 gcc-4.7.2 cc -shared -Wl, - version-script, vscript -fpic -o libpower.so ...
 Solaris 10 gcc-4.8 * gcc -shared -Wl, -M, vscript -fpic -o libpower.so ...
 Solaris 10 cc 12.3 cc -shared -M vscript -fpic -o libpower.so ...

Note that GCC on Solaris is configured to use ld / as from /usr/ccs/bin .

+3
source

Source: https://habr.com/ru/post/1482935/


All Articles