2

i am trying to override predefined functions such as strcmp, getenv, etc. i already override some of them using LD_PRELOAD options. but i can't override string class, how can i override c++ string class

i already tried something like this;

class string{
    char* str;
public:
    string (char* str2){
        str = str2;
        std::cout << str2 << std::endl ;
    }
    bool operator==(char* str2)  {
        std::cout << "c++ => " << str << "==" << str2 << std::endl;
        return false; 
    }
    compare(char* str2)  {
        std::cout << "c++ => " << str << "==" << str2 << std::endl;
        return 0;
    }
};  

but it doesn't work i can't print the string that compared or even defined.

compile;

g++ -g -Wall -shared -fPIC -ldl -o bypasscpp.so bypass.cpp

And running program with;

LD_PRELOAD="./bypasscpp.so" ./app
6
  • 3
    It seems you're trying to achieve something else and I feel this may not be the best way to achieve it. You may want to consider describing your end goal and look for alternative solutions. – NirIzr Jun 4 '18 at 21:14
  • @NirIzr i already tried many different solutions this is my last solution. I need to override these classes. – Mustafa Akdeniz Jun 4 '18 at 22:50
  • @Mustafa Adeniz Are these class methods virtual ? – w s Jun 5 '18 at 6:07
  • 1
    @MustafaAkdeniz: seriously, Nirlzr is right. If you (also!) described what you want to achieve rather than prescribing a particular method, we would be in a better position to help. Just because this is your last straw doesn't mean there aren't better ways to achieve what you want. Why else would you be asking here? – 0xC0000022L Jun 5 '18 at 8:49
  • 1
    @malikcjm can you please put it in the answer box for future visitors? – Igor Skochinsky Jun 7 '18 at 7:46
0

Are you trying to override std::string? Please check https://en.cppreference.com/w/cpp/string/basic_string. std::string is just a typedef for std::basic_string<char> which means that std::string is not dynamically linked. In other words those methods you try to override are statically linked into you ./app.

1
  • std::string is a name for the char-specialization of the std::basic_string template class. And while it can be statically linked, it is also provided as a function by the libstdc++6.so as evidenced by running readelf --dyn-syms /usr/lib/x86_64-linux-gnu/libstdc++.so.6|awk '$4 ~ /^FUNC/ {print $8}'|cut -d @ -f 1|c++filt|grep 'std::basic_string<char' (tested on Ubuntu 16.04). – 0xC0000022L Jun 7 '18 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.