Why is everyone convinced I'm trying to screw you all over? If you're seriously idiotic enough to think that executables store the memory address of all external functions statically, try compiling these 2 things:
Compile with: g++ lib.cpp -shared -o libfoo.so
lib.cpp
Code:
#include "lib.h"
#include <stdio.h>
void lib::foo(int i)
{
printf("%d\n", i);
}
void lib::del()
{
delete this;
}
lib *new_lib()
{
return new lib;
}
lib.h
Code:
class lib
{
public:
void foo(int i);
void del();
};
lib *new_lib();
Compile with: g++ foo.cpp -o foo -lfoo -L.
foo.cpp
Code:
#include "lib.h"
int main()
{
lib *l = new_lib();
l->foo(10);
l->foo(20);
l->del();
return 0;
}
then run LD_LIBRARY_PATH=. ./foo
it'll output:
now let's throw some shit around:
change lib.h to:
Code:
class lib
{
private:
int j;
public:
lib();
~lib();
void foo(int i);
virtual void add(int i);
void del();
};
lib *new_lib();
and change lib.cpp to:
Code:
#include "lib.h"
#include <stdio.h>
lib::lib() : j(5)
{
printf("constructor\n");
}
lib::~lib()
{
printf("destructor\n");
}
void lib::foo(int i)
{
printf("%d\n", j);
add(i);
}
void lib::add(int i)
{
j += i;
}
void lib::del()
{
delete this;
}
lib *new_lib()
{
return new lib;
}
compile the lib again.
run the same, untouched executable from before and you'll see:
Code:
constructor
5
15
destructor
oh, and just for arguments sake, try removing a function from the lib and see what the program outputs. I removed lib::del and got:
Code:
[zhasha@ztoshiba Desktop]$ LD_LIBRARY_PATH=. ./foo
constructor
5
15
./foo: symbol lookup error: ./foo: undefined symbol: _ZN3lib3delEv
EDIT: Now to put the final nail in the coffin for the myth you've started here. Try adding -Bsymbolic to the library compile line. What this does is resolve as many function pointers as close as it can on link time, or as the ld man page puts it:
Code:
-Bsymbolic
When creating a shared library, bind references to global symbols to the definition within the shared library, if any. Normally,
it is possible for a program linked against a shared library to override the definition within the shared library. This option is
only meaningful on ELF platforms which support shared libraries.
NOW STOP BLASTING YOUR UNFOUNDED CLAIMS AROUND.