3 ms·
This reminds me of working with C++ and the Pebble SDK. The Pebble builds ELF binaries but doesn't have an ELF loader. It also uses position independent code.
by wvenable 8y ago
This reminds me of working with C++ and the Pebble SDK. The Pebble builds ELF binaries but doesn't have an ELF loader. It also uses position independent code. The SDK uses objtools to strip the code out of the ELF and the Pebble OS just copies the code into memory and jumps to it.
In C, this works fine. But in C++, gcc generates the GOT for virtual method calls but there is no ELF loader to fill it.
I accidentally found out if my code contains a destructor that makes an assignment to a static variable then no GOT is generated and virtual method calls work for the entire project! I'm not sure the logic behind that but this is the code that is needed:
class SampleObject
{
public:
//! Destroys the underlying Pebble window
virtual ~SampleObject()
{
// The assignment to a static variable makes the virtual methods work for subclasses
// I have no idea why!
dummy() = 0;
}
private:
// Dummy static variable needed for virutal method pointers to work (see destructor)
inline static int& dummy() { static int dummy = 0; return dummy; }
}