You are here

AIX and dladdr

Hi!

This is a post that is relevant to about 0.000000% (o, I forgot a "1"!) of the readers: how to implement the missing dladdr() in AIX5. I could not find it anywhere, nobody seemed to have an implementation - and CINT needs it. So when porting v5.28.00 to AIX5 I needed to deal with it.

To save other people from going mad, here's the code:

#include "sys/ldr.h"

struct Dl_info {
  const char* dli_fname;
};
int dladdr(void* s, Dl_info* i) {
   static const size_t bufSize = 4096;
   G__FastAllocString buf(bufSize);
   char* pldi = buf;
   int r = loadquery(L_GETINFO,  pldi,  bufSize);
   if (r == -1) {
      i->dli_fname = 0;
      return 0;
   }
   // First is main(), skip.
   ld_info* ldi = (ld_info*)pldi;
   while (ldi->ldinfo_next) {
     pldi += ldi->ldinfo_next;
     ldi = (ld_info*)pldi;
     char* textBegin = (char*)ldi->ldinfo_textorg;
     if (textBegin < s) {
        char* textEnd = textBegin + ldi->ldinfo_textsize;
        if (textEnd > s) {
           i->dli_fname = ldi->ldinfo_filename;
           return 1;
        }
     }
   }
   i->dli_fname = 0;
   return 0;
}

The next post will be super mainstream again, I promise ;-)

Cheers,

Comments

This works, but it seems like it only returns the name of the file where the symbol resides not either a complete or relative path.

Hi Alex,

I was trying to get this code to work for me, but it seems the addresses functions I test with don't fall within the range of the 'text' addresses. Also it causes a SEGV when compiled as a 64-bit binary.

Did the code above work OK for you?

Thanks, Graham.

Hi Graham,

It worked for me, for the issue I was solving, on AIX 5.something - I believe 5.3, and yes, 32bit. I remember the original dladdr() has several bits of functionality; the code above only implements one: determine which library a given function (extern "C") is in. Sorry to hear that it doesn't work for you! You're very welcome to post an improved version if you find one. Good luck!

Cheers, Axel

Hi Axel,

The 64-bit SEGV issue turns out to be a problem with the particular machine I was using. I don't get that problem when compiling elsewhere.

Also I found I had to dereference the function pointer in AIX. To keep the implementation consistent across platforms I dereferenced it inside the implementation of dladdr().

Cheers, Graham.

Hi Graham,

Congrats, and thanks for letting us know! Would you mind sending (axel at cern dot ch) or posting the revised code, so I can update the code above? I think I know which change it needs, but seeing the code involves less guessing :-)

Cheers, Axel