[SLUG] Finding .so's path from inside the .so at runtime?
Hello, Is there a standard way for a .so file to find where it was loaded from? Background: I ported an ISAPI module from windows to Linux (to connect to Zeus web server). The original code on Windows relays on the .DLL's ability to find its path on the disk in order to lookup a configuration file relative to it. On Linux I just used an anvironment variable but later I noticed that the Java Plugin for Mozilla relays on being symbolically-linked (rather than copied) in order to run, and I suspect that maybe it needs this in order to find the rest of the files at runtime (e.g. the runtime class library, the zoneinfo files etc). I'd like to use the same trick myself. Currently I think of immitating the ld.so algorithm for finding files but that could get unreliable if the algorithm changes one day, and involves duplicating of the work already done by the loader itself. I'd rather try to get the real info from the dynamic loader. Any ideas? Thanks, --Amos (BTW - the ISAPI module is Caucho's Resin and is under GPL, the port is expected to be included in the next release of Resin (3.0.12), and I'll be happy to provide the patch to anyone who asks). -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Finding .so's path from inside the .so at runtime?
On Fri, Feb 25, 2005 at 11:17:48AM +1100, [EMAIL PROTECTED] wrote: Is there a standard way for a .so file to find where it was loaded from? from man dl_iterate_phdr The info argument is a structure of the following type: struct dl_phdr_info { ElfW(Addr)dlpi_addr; /* Base address of object */ const char *dlpi_name; /* (Null-terminated) name of object const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of ELF program headers for this object */ ElfW(Half)dlpi_phnum; /* # of items in 'dlpi_phdr' */ }; The dlpi_name field is a null-terminated string giving the path-name from which the shared object was loaded. -i [EMAIL PROTECTED] http://www.gelato.unsw.edu.au signature.asc Description: Digital signature -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Finding .so's path from inside the .so at runtime?
Hi, Thanks. Looks like just what I was looking for. I'll probably iterate through the output and look for my .so's name in the dlpi_name base. It would probably be more accurate to look for a segment which contains the current function's address, but that'll require more investigation of the returned data. Cheers, --Amos On Fri, 25 Feb 2005 12:40:11 +1100, Ian Wienand [EMAIL PROTECTED] wrote: On Fri, Feb 25, 2005 at 11:17:48AM +1100, [EMAIL PROTECTED] wrote: Is there a standard way for a .so file to find where it was loaded from? from man dl_iterate_phdr The info argument is a structure of the following type: struct dl_phdr_info { ElfW(Addr)dlpi_addr; /* Base address of object */ const char *dlpi_name; /* (Null-terminated) name of object const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of ELF program headers for this object */ ElfW(Half)dlpi_phnum; /* # of items in 'dlpi_phdr' */ }; The dlpi_name field is a null-terminated string giving the path-name from which the shared object was loaded. -i [EMAIL PROTECTED] http://www.gelato.unsw.edu.au -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html