From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2082 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: PATCH: dl_iterate_phdr() Date: Thu, 11 Oct 2012 19:42:55 -0400 Message-ID: <20121011234255.GB254@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1349999498 674 80.91.229.3 (11 Oct 2012 23:51:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 11 Oct 2012 23:51:38 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-2083-gllmg-musl=m.gmane.org@lists.openwall.com Fri Oct 12 01:51:45 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1TMSXY-0007lL-Ko for gllmg-musl@plane.gmane.org; Fri, 12 Oct 2012 01:51:44 +0200 Original-Received: (qmail 7730 invoked by uid 550); 11 Oct 2012 23:51:37 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 7721 invoked from network); 11 Oct 2012 23:51:36 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:2082 Archived-At: On Thu, Oct 11, 2012 at 09:29:28AM -0500, Alex Caudill wrote: > Hi! > > With a little help on IRC I hacked together a basic version of > dl_iterate_phdr(), which I dropped into src/ldso/dynlink.c in my tree. > This introduces a new include file: > > Line #9 of dl_iterate_phdr.c throws a warning that I'm not sure how to silence. > > I've attached both, and a test program. No copyright. > > Thanks! > int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data) > { > struct dso *current; > Ehdr *ehdr; > struct dl_phdr_info info; > int ret = 0; > for(current = head; current; current = current->next) { > ehdr = (Ehdr *)current->map; > info.dlpi_addr = (ehdr->e_type == ET_EXEC) ? 0 : current->map; This is wrong; it should be current->base. See the man page: The dlpi_addr field indicates the base address of the shared object (i.e., the difference between the virtual memory address of the shared object and the offset of that object in the file from which it was loaded). > info.dlpi_name = current->shortname; This too: The dlpi_name field is a null-terminated string giving the pathname from which the shared object was loaded. i.e. it needs to be using name, not shortname. The only place shortname is to use is to match libraries loaded by name without a pathname provided. It's NULL for libraries explicitly loaded by pathname. > info.dlpi_phdr = (Phdr *)(current->map + ehdr->e_phoff); It might make more sense to save the phdr address during library loading instead of reading the Ehdr again, especially since there's no fundamental guarantee the Ehdr is available anymore. (It could be in an unmapped page at the beginning of the library prior to the mapped portion. This usage is ugly and may not even work with musl right now, but there's no reason to add more gratuitous reasons it should fail.) > info.dlpi_phnum = ehdr->e_phnum; > > ret = (callback)(&info, sizeof (info), data); > if (ret != 0) > break; > } > return ret; > } > #ifndef LINK_H > #define LINK_H Missing underscore. Not absolutely essential since this is a nonstandard header, but it should be there. > > #include > > #define __NEED_size_t > #include > > #ifdef _LP64 Should use UINTPTR_MAX. > #define ElfW(type) Elf64_ ## type > #else > #define ElfW(type) Elf32_ ## type > #endif > > struct dl_phdr_info { > ElfW(Addr) dlpi_addr; > const char *dlpi_name; > const ElfW(Phdr) *dlpi_phdr; > ElfW(Half) dlpi_phnum; > }; We need to provide the "ElfW" macro since calling code might use it, but I'd rather it not be used internally. Just use the correct types (e.g. uintptr_t or size_t, etc.). Otherwise, looks okay! Rich