tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

dl_iterate_phdr(3) include the ELF loader

dl_iterate_phdr(3) as of now skips the ELF loader.

This behavior differs to Linux, FreeBSD and OpenBSD (other OSs untested).

This shortage has been unveiled as it affects sanitizers (at least LSan).

This patch fixes the problem for me:

Test case:

$ cat tls.c

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/param.h>
#include <link.h>
#include <stdio.h>
#include <dlfcn.h>
#include <elf.h>
#include <unistd.h>
#include <stdlib.h>
#include <link.h>
#include <errno.h>

int GetPhdr(struct dl_phdr_info *info, size_t size, void *data) {
  printf("addr=%p name=%s\n", info->dlpi_addr, info->dlpi_name);

  const Elf_Phdr *hdr = info->dlpi_phdr;
  const Elf_Phdr *last_hdr = hdr + info->dlpi_phnum;

  printf("dlpi_phdr=%p dlpi_phnum=%u\n", info->dlpi_phdr, info->dlpi_phnum);

  for (; hdr != last_hdr; ++hdr) {
    printf("p_type=%llx p_vaddr=%p p_paddr=%p\n",
           hdr->p_type, hdr->p_vaddr, hdr->p_paddr);
  return 0;

main(int argc, char **argv)
  dl_iterate_phdr(GetPhdr, NULL);

  return 0;

Output with the ELF loader applied reports correctly the loader entries.

The patch has been inspired from the FreeBSD approach.

I would like to get this functionality merged and backported to -9.

# ./a.out

addr=0x0 name=./a.out
dlpi_phdr=0x400040 dlpi_phnum=7
p_type=6 p_vaddr=0x400040 p_paddr=0x400040
p_type=3 p_vaddr=0x4001c8 p_paddr=0x4001c8
p_type=1 p_vaddr=0x400000 p_paddr=0x400000
p_type=1 p_vaddr=0x600ca8 p_paddr=0x600ca8
p_type=2 p_vaddr=0x600cd0 p_paddr=0x600cd0
p_type=4 p_vaddr=0x4001e0 p_paddr=0x4001e0
p_type=6474e550 p_vaddr=0x400b24 p_paddr=0x400b24
addr=0x7f7ff7600000 name=/usr/lib/
dlpi_phdr=0x7f7ff7600040 dlpi_phnum=8
p_type=6 p_vaddr=0x40 p_paddr=0x40
p_type=1 p_vaddr=0x0 p_paddr=0x0
p_type=1 p_vaddr=0x3c34a8 p_paddr=0x3c34a8
p_type=2 p_vaddr=0x3cd780 p_paddr=0x3cd780
p_type=4 p_vaddr=0x1c330c p_paddr=0x1c330c
p_type=7 p_vaddr=0x3c34a8 p_paddr=0x3c34a8
p_type=6474e550 p_vaddr=0x191c48 p_paddr=0x191c48
p_type=6474e552 p_vaddr=0x3c34a8 p_paddr=0x3c34a8
addr=0x7f7ff7c00000 name=/usr/libexec/ld.elf_so
dlpi_phdr=0x7f7ff7c00040 dlpi_phnum=7
p_type=6 p_vaddr=0x40 p_paddr=0x40
p_type=1 p_vaddr=0x0 p_paddr=0x0
p_type=1 p_vaddr=0x211720 p_paddr=0x211720
p_type=2 p_vaddr=0x211f00 p_paddr=0x211f00
p_type=4 p_vaddr=0x11108 p_paddr=0x11108
p_type=6474e550 p_vaddr=0xf564 p_paddr=0xf564
p_type=6474e552 p_vaddr=0x211720 p_paddr=0x211720

Attachment: signature.asc
Description: OpenPGP digital signature

Home | Main Index | Thread Index | Old Index