Skip to main content

LM_EnumSymbols

Enumerates symbols in a module and calls a callback function for each symbol found.

Signature

LM_API lm_bool_t LM_CALL
LM_EnumSymbols(const lm_module_t  *module,
               lm_bool_t (LM_CALL *callback)(lm_symbol_t *symbol,
                                             lm_void_t   *arg),
               lm_void_t          *arg);

Parameters

module
const lm_module_t*
required
The module where the symbols will be enumerated from.
callback
function pointer
required
A function pointer that will receive each symbol in the enumeration and an extra argument. The callback function should return LM_TRUE to continue the enumeration or LM_FALSE to stop it.Callback Signature:
lm_bool_t callback(lm_symbol_t *symbol, lm_void_t *arg)
arg
lm_void_t*
A pointer to user-defined data that can be passed to the callback function. It allows you to provide additional information or context.

Return Value

Returns LM_TRUE if the enumeration succeeds, LM_FALSE otherwise.

Symbol Structure

The lm_symbol_t structure contains the following fields:
typedef struct lm_symbol_t {
    lm_string_t  name;     // Symbol name
    lm_address_t address;  // Symbol address
} lm_symbol_t;
name
lm_string_t
The name of the symbol.
address
lm_address_t
The memory address of the symbol.

Example

#include <libmem/libmem.h>
#include <stdio.h>

lm_bool_t symbol_callback(lm_symbol_t *symbol, lm_void_t *arg)
{
    printf("Symbol: %s -> %p\n", symbol->name, (void *)symbol->address);
    return LM_TRUE; // Continue enumeration
}

int main()
{
    lm_module_t module;
    
    // Find a module
    if (!LM_FindModule("libc.so.6", &module)) {
        printf("Failed to find module\n");
        return 1;
    }
    
    printf("Enumerating symbols in %s:\n", module.name);
    
    // Enumerate all symbols
    if (LM_EnumSymbols(&module, symbol_callback, LM_NULLPTR)) {
        printf("Symbol enumeration completed\n");
    } else {
        printf("Symbol enumeration failed\n");
    }
    
    return 0;
}

LM_EnumSymbolsDemangled

Enumerates symbols in a module with demangled names and calls a provided callback function for each symbol found.

Signature

LM_API lm_bool_t LM_CALL
LM_EnumSymbolsDemangled(const lm_module_t  *module,
                        lm_bool_t (LM_CALL *callback)(lm_symbol_t *symbol,
                                                      lm_void_t   *arg),
                        lm_void_t          *arg);

Parameters

module
const lm_module_t*
required
The module where the symbols will be enumerated from.
callback
function pointer
required
A function pointer that will receive each demangled symbol in the enumeration and an extra argument. The callback function should return LM_TRUE to continue the enumeration or LM_FALSE to stop it.Callback Signature:
lm_bool_t callback(lm_symbol_t *symbol, lm_void_t *arg)
arg
lm_void_t*
A pointer to user-defined data that can be passed to the callback function. It allows you to provide additional information or context.

Return Value

Returns LM_TRUE if the enumeration succeeds, LM_FALSE otherwise.

Description

This function works similarly to LM_EnumSymbols, but automatically demangles C++ symbol names to their human-readable form. This is particularly useful when working with C++ binaries where symbol names are mangled by the compiler.

Example

#include <libmem/libmem.h>
#include <stdio.h>

lm_bool_t demangled_callback(lm_symbol_t *symbol, lm_void_t *arg)
{
    printf("Demangled Symbol: %s -> %p\n", symbol->name, (void *)symbol->address);
    return LM_TRUE;
}

int main()
{
    lm_module_t module;
    
    // Find a C++ module/library
    if (!LM_FindModule("libstdc++.so.6", &module)) {
        printf("Failed to find module\n");
        return 1;
    }
    
    printf("Enumerating demangled symbols in %s:\n", module.name);
    
    // Enumerate all symbols with demangled names
    if (LM_EnumSymbolsDemangled(&module, demangled_callback, LM_NULLPTR)) {
        printf("Demangled symbol enumeration completed\n");
    } else {
        printf("Demangled symbol enumeration failed\n");
    }
    
    return 0;
}

Notes

  • This function is particularly useful for C++ libraries where mangled names like _ZN3foo3barEv are automatically converted to readable forms like foo::bar().
  • The demangling process may add some overhead compared to LM_EnumSymbols.
  • If a symbol cannot be demangled, it will be returned in its mangled form.

Build docs developers (and LLMs) love