mailing list of musl libc
 help / color / mirror / code / Atom feed
* PATCH: dl_iterate_phdr()
@ 2012-10-11 14:29 Alex Caudill
  2012-10-11 23:42 ` Rich Felker
  0 siblings, 1 reply; 14+ messages in thread
From: Alex Caudill @ 2012-10-11 14:29 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 329 bytes --]

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: <link.h>

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!

[-- Attachment #2: dl_iterate_phdr.c --]
[-- Type: text/x-csrc, Size: 632 bytes --]

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;
        info.dlpi_name = current->shortname;
        info.dlpi_phdr = (Phdr *)(current->map + ehdr->e_phoff);
        info.dlpi_phnum = ehdr->e_phnum;

        ret = (callback)(&info, sizeof (info), data);
        if (ret != 0)
            break;
    }
    return ret;
}

[-- Attachment #3: link.h --]
[-- Type: text/x-chdr, Size: 480 bytes --]

#ifndef LINK_H
#define LINK_H

#include <elf.h>

#define __NEED_size_t
#include <bits/alltypes.h>

#ifdef _LP64
#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;
};
 
int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *), void *);

#endif

[-- Attachment #4: dltest.c --]
[-- Type: text/x-csrc, Size: 528 bytes --]

#define _GNU_SOURCE
#include <link.h>
#include <stdlib.h>
#include <stdio.h>

static int
callback(struct dl_phdr_info *info, size_t size, void *data)
{
    int j;

   printf("name=%s (%d segments)\n", info->dlpi_name,
        info->dlpi_phnum);

   for (j = 0; j < info->dlpi_phnum; j++)
         printf("\t\t header %2d: address=%10p\n", j,
             (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
    return 0;
}

int
main(int argc, char *argv[])
{
    dl_iterate_phdr(callback, NULL);

   exit(EXIT_SUCCESS);
}

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2012-11-01  1:33 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-11 14:29 PATCH: dl_iterate_phdr() Alex Caudill
2012-10-11 23:42 ` Rich Felker
2012-10-12  0:00   ` Rich Felker
2012-10-12 13:28     ` Alex Caudill
2012-10-12 15:43       ` Rich Felker
2012-10-13  1:04         ` Alex Caudill
2012-10-13  1:24           ` Alex Caudill
2012-10-13  1:31             ` Rich Felker
2012-10-15  3:30             ` Rich Felker
2012-10-15  4:47               ` Alex Caudill
2012-10-15 12:41                 ` Rich Felker
2012-10-18 20:45                 ` Rich Felker
2012-10-31 18:35                   ` Alex Caudill
2012-11-01  1:33                     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).