0

I'm attempting to strip away all symbols from a shared object. I know that this kinda goes against the norm when it comes to so's, but I'm making a self contained addon to another program. I made myself a little toy program to test which looks like this;

#include <fstream>
#include <iostream>

void PrintMessage(std::string msg)
{
    std::cout << msg << std::endl;
}

void HelloWorld()
{
    std::string s = "HelloWorld!";
    std::cout << s << std::endl;
}

extern "C" void __attribute__((constructor)) SoMain() {
    std::ofstream file("HelloWorld");
    file.close();
    HelloWorld();
}

And I compiled it like so g++ -shared -fPIC -o test.so test.cpp

I'm fine with SoMain() being a known symbol, but everything from there should be "hidden". I.e., one should not be able to open a disassembler such as IDA and see the function names such as "HelloWorld" or "PrintMessage".

Is there a way to achieve this behavior for shared objects?

MegaMagnum
  • 43
  • 9
  • 1
    Use `-fvisibility=hidden` to make symbols hidden by default. Use `__attribute__((visibility("default")))` on a function to make it public again. See [this](https://gcc.gnu.org/wiki/Visibility). This still includes symbols but prevents them from being linked against. You can then strip hidden symbols; I think `strip` will just do the right thing. See also https://stackoverflow.com/q/58535079. – Jonathon Reinhart Jul 15 '23 at 17:15
  • Thanks Jonathon Reinhart! Compiling with `g++ -shared -fvisibility=hidden -fPIC -o test.so test.cpp` and strip with `strip --strip-all --discard-all --verbose test.so` seems to have solved it :D The constructor is still in init_array section so it works :) – MegaMagnum Jul 15 '23 at 17:28

0 Answers0