HP-UX Linker and Libraries User's Guide

Using dladdr to get the symbolic information for an address
The following example shows how to use dladdr to:
#include <dlfcn.h>
typedef int (*fptr_t) ();
int func1() {return 11;}
int main()
{
D1_info di;
char* symname = (char*)strdup("func1");
if (dladdr(&func1, &di) == 0) {
printf("dladdr() failed for %s\n", symname);
return 1;
}
if (di.dli_sname) {
fptr_t tmp = (fptr_t)dlsym(di.dli_fbase, symname);
if (di.dli_saddr !=void *)&func1)
puts("approx match");
if (tmp != (fptr_t)di.dli_saddr) {
printf("dlsym(%s): %s\n", symname, dlerror());
}
}
printf("%s: %s(%d %d %d)\n", di.dli_sname? di.dli_sname:"nil",
di.dli_fname, di.dli_type, di.dli_bind, di.dli_size);
return 0;
}
The dlmodadd Routine
The dlmodadd routine registers information about dynamically-generated functions.
Syntax
void* dlmodadd(void *associate handle,
void *func_start,
size_t func_size,
void *linkage_ptr,
void *unwind_info);
Table 31 Parameters
DescriptionParameter
The module handle for an existing module with which the new function is associated. When the
dynamic loader is called from the new function, it behaves as if it had been called from the
associate
handle
associated module. This handle must be a handle returned by the dlopen() routine or the
dlget routine.
The starting address of the generated machine code for the function. It is NOT the address of
a function descriptor.
func_start
The size, in bytes, of the generated machine code.func_size
The global pointer (gp) value to be used for the function. It could be the gp of the associated
module. It must be set to a valid value, even if the generated code doesn't actually use gp, as
it is required at a minimum for following the personality routine pointer in the unwind information.
linkage_ptr
A pointer to the beginning of the unwind info block for the function. The unwind info block
contains the header word, unwind descriptors, personality routine pointer, and language-specific
unwind_info
data, as described in the Itanium-based system Software Conventions and Runtime Architecture
document.
Return Values
If successful, dlmodadd returns a handle as identification of the newly-added function. NULL is
returned otherwise.
The dlopen Shared Library Management Routines 165