esp32-elfloader icon indicating copy to clipboard operation
esp32-elfloader copied to clipboard

Elf compiled in c++

Open smersh1307n2 opened this issue 2 years ago • 2 comments

Hello. Is it possible to use C++ to create an elf file using classes? All attempts to load the file were unsuccessful.

smersh1307n2 avatar Oct 18 '23 06:10 smersh1307n2

@smersh1307n2 please provide more details about the issue you faced. Error logs will be helpful to resolve this.

shvass avatar Dec 22 '24 18:12 shvass

hi @smersh1307n2, I don't know if this is still useful, but I looked into this a bit more. This is what I came up with.

TL;DR

Add this to your exports:

extern "C" {
extern void *__dso_handle;

extern int __cxa_atexit(void (*destructor)(void *), void *arg, void *dso);

extern void __cxa_pure_virtual() {
  Serial.println("ERROR: Pure virtual function called!");
  while (1)
    delay(1000);
}

extern int __cxa_guard_acquire(uint64_t *guard);

extern void __cxa_guard_release(uint64_t *guard);

extern void __cxa_guard_abort(uint64_t *guard);
}

static const ELFLoaderSymbol_t exports[] = {
    {"print", (void *)print},
    {"digitalRead", (void *)digitalRead},
    {"digitalWrite", (void *)digitalWrite},
    {"pinMode", (void *)pinMode},
    {"delay", (void *)delay},

    {"__dso_handle", (void *)&__dso_handle},
    {"__cxa_atexit", (void *)__cxa_atexit},
    {"__cxa_pure_virtual", (void *)__cxa_pure_virtual},
    {"__cxa_guard_acquire", (void *)__cxa_guard_acquire},
    {"__cxa_guard_release", (void *)__cxa_guard_release},
    {"__cxa_guard_abort", (void *)__cxa_guard_abort},

    // C++ new/delete operators
    {"_Znwj", (void *)malloc}, // operator new(unsigned int)
    {"_ZdlPv", (void *)free},  // operator delete(void*)
    {"_ZdlPvj", (void *)free}, // operator delete(void*, unsigned int)
    {"_Znaj", (void *)malloc}, // operator new[](unsigned int)
    {"_ZdaPv", (void *)free},  // operator delete[](void*)
    {"_ZdaPvj", (void *)free}, // operator delete[](void*, unsigned int)
};
static const ELFLoaderEnv_t env = {exports, sizeof(exports) / sizeof(*exports)};

ironlungx avatar Nov 10 '25 20:11 ironlungx