Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

gcc - version-script and hidden visibility

When using gcc to build a shared library, it's possible to limit the visibility of the symbols using -fvisibility=hidden. I also just learned you can limit visibility using the version-script option to ld.

Now I want to know if it's possible to combine these. Say I have a program with the following:

void foobar() {}
void say_hello() {}

Then I have the version script file with:

{
  global:
    foobar;
}

And I compile this with:

gcc -fvisibility=hidden -Wl,--version-script=<version-script> test.c -shared -o libtest.so

When I run nm on this afterwards, I find that no symbols are exported. Is there anyway that I can set the default visibility to hidden and use the version-script (or something else) to export symbols?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your question makes no sense: why fight -fvisibility with a linker script, when you can use the linker script to export exactly what you need, and hide everything else:

{
  global: foobar;
  local: *;
};

Update:

Because the code I need to use this on uses __attribute__((visibility("default"))) ...

The linker script works just fine with symbols so marked. Example:

// t.c
int __attribute__((visibility("default"))) foo() { return 1; }
int bar() { return 2; }
int __attribute__((visibility("default"))) exported() { return 3; }

// t.lds
{
  global: exported;
  local: *;
};

gcc t.c -Wl,--version-script=t.lds -fPIC -shared -o t.so && nm -D t.so
                 w _Jv_RegisterClasses
                 w __cxa_finalize
                 w __gmon_start__
00000000000004f2 T exported

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...