The following example shows how to use dlsym with RTLD_NEXT to add functionality to an existing interface. (Error checking has been omitted.)

extern void record_malloc(void *, size_t); void *

malloc(size_t sz)

{

void *ptr;

void *(*real_malloc)(size_t);

real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");

ptr = (*real_malloc)(sz); record_malloc(ptr, sz); return ptr;

}

The dlget Routine

The dlget routine retrieves information about a loaded module (program or shared library).

Syntax

 

void *dlget(int index,

 

 

struct load_module_desc *desc,

 

 

size_t desc_size);

Table 26 Parameters

 

 

 

Parameter

 

Definition

 

 

 

index

 

Specifies the requested shared library by its placement on the dynamic loader's search list. An

 

 

index of -1 requests information about the dynamic loader. An index of -2 requests information

 

 

about the program file itself.

 

 

 

desc

 

Must be preallocated by the user. The structure members are filled in by the dynamic loader with

 

 

information about the requested shared library.

 

 

 

desc_size

 

Specifies the size in bytes of the load_module_desc structure sent in by the user.

 

 

 

Return Values

If successful, dlget returns a handle for the shared library as defined by the return value from dlopen(). If a call to dlget is unsuccessful, a NULL pointer is returned and desc remains unchanged.

Description

The dlget routine is one of a family of routines that give the user direct access to the dynamic linking facilities. The dlget routine retrieves information about a load module from an index specifying the placement of a load module in the dynamic loader's search list. A load_module_desc structure has the following members:

struct load_module_desc { unsigned long text_base; unsigned long text_size; unsigned long data_base; unsigned long data_size; unsigned long unwind_base; unsigned long linkage_ptr; unsigned long phdr_base; unsigned long tls_size; unsigned long tls_start_addr;

}

The dlopen Shared Library Management Routines 159