Ticket #2818: 20081022-dladdr.diff

File 20081022-dladdr.diff, 2.5 KB (added by romain, 16 years ago)

Patch update based on axel's remarks

  • src/system/libroot/posix/dlfcn.c

     
    7272}
    7373
    7474
     75int
     76dladdr(void *addr, Dl_info *info)
     77{
     78    char curSymName[NAME_MAX];
     79    static char symName[NAME_MAX];
     80    static char imageName[MAXPATHLEN];
     81    void *symLocation;
     82    int32 cookie;
     83    int32 symType, symNameLength;
     84    uint32 symIndex;
     85    image_info imageInfo;
     86
     87    if(info == NULL)
     88        return 0;
     89
     90    imageName[0] = '\0';
     91    symName[0] = '\0';
     92    info->dli_fname = imageName;
     93    info->dli_saddr = NULL;
     94    info->dli_sname = symName;
     95
     96    cookie = 0;
     97    while (get_next_image_info(0, &cookie, &imageInfo) == B_OK) {
     98        // check if the image holds the symbol
     99        if ((addr_t)addr >= (addr_t)imageInfo.text
     100            && (addr_t)addr < (addr_t)imageInfo.text + imageInfo.text_size)  {
     101            strlcpy(imageName, imageInfo.name, MAXPATHLEN);
     102            info->dli_fbase = imageInfo.text;
     103            symIndex = 0;
     104            symNameLength = NAME_MAX;
     105
     106            while (get_nth_image_symbol(imageInfo.id,
     107                symIndex, curSymName, &symNameLength,
     108                &symType, &symLocation) == B_OK) {
     109                // check if symbol is the nearest until now
     110                if (symType == B_SYMBOL_TYPE_TEXT
     111                    && symLocation <= addr
     112                    && symLocation >= info->dli_saddr) {
     113                    strlcpy(symName, curSymName, NAME_MAX);
     114                    info->dli_saddr = symLocation;
     115
     116                    // stop here if exact match
     117                    if (info->dli_saddr == addr)
     118                        return 1;
     119                }
     120                symIndex++;
     121                symNameLength = NAME_MAX;
     122            }
     123            break;
     124        }
     125
     126    }
     127
     128    if (info->dli_saddr != NULL)
     129        return 1;
     130    else
     131        return 0;
     132}
     133
     134
    75135// __libc_dl*** wrappers
    76136// We use a mixed glibc / bsd libc, and glibc wants these
    77137void *__libc_dlopen(const char *name);
  • headers/posix/dlfcn.h

     
    1717extern "C" {
    1818#endif
    1919
     20/* This is a gnu extension for the dladdr function */
     21typedef struct {
     22   const char *dli_fname;  /* Filename of defining object */
     23   void *dli_fbase;        /* Load address of that object */
     24   const char *dli_sname;  /* Name of nearest lower symbol */
     25   void *dli_saddr;        /* Exact value of nearest symbol */
     26} Dl_info;
     27
    2028extern int  dlclose(void *image);
    2129extern char *dlerror(void);
    2230extern void *dlopen(const char *path, int mode);
    2331extern void *dlsym(void *image, const char *symbolName);
     32extern int dladdr(void *addr, Dl_info *info);
    2433
    2534#ifdef __cplusplus
    2635}