Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LD script for symbol hidding in c++

I would like to use an GNU LD version script to hide unwanted symbols in c++ shared library. Say my header file looks like this:

int a();
int a(int);

class B {
    B(){}
    ~B(){}
    int x(int);
};

std::ostream& operator<< (std::ostream& out, const B& b );

I would like to hide everything which is not stated in the header file.

How would a version script for this look like?

like image 344
Allan Avatar asked Dec 08 '11 14:12

Allan


1 Answers

Something like this should do the trick:

{
global:
    extern "C++" {
        "a()";
        "a(int)";
        B::*;
        "operator<<(std::ostream&, B const&)";
    };
local:
    *;
};

If you saved this file as foo.map, pass -Wl,--version-script,foo.map as an argument to the linker. A quick rundown of the syntax:

  • Since we didn't specify a version label at the top level of the script, the symbols in the library won't have versions attached: the effect of the script is simply to pick which symbols are visible.

  • Everything matched by the global section will be visible, while everything remaining that matches the local section (in this case, the glob *) will be hidden.

  • The extern "C++" { ... }; block says the linker should demangle symbols according to the C++ ABI before trying to match against the enclosed patterns.

  • Patterns in quotes are matched directly, while unquoted patterns are treated as glob patterns.

More details of the version script file format can be found here: https://sourceware.org/binutils/docs/ld/VERSION.html

like image 110
James Henstridge Avatar answered Nov 12 '22 19:11

James Henstridge