Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 470882

Summary: dev-libs/libelf: ELF_C_RDWR doesn't allow you to change ELF Program headers.
Product: Gentoo Linux Reporter: Anthony Basile <blueness>
Component: [OLD] LibraryAssignee: Anthony Basile <blueness>
Status: CONFIRMED ---    
Severity: normal CC: embedded
Priority: Normal    
Version: unspecified   
Hardware: All   
OS: Linux   
See Also: https://bugs.gentoo.org/show_bug.cgi?id=465238
https://bugs.gentoo.org/show_bug.cgi?id=470884
Whiteboard:
Package list:
Runtime testing required: ---

Description Anthony Basile gentoo-dev 2013-05-21 14:42:12 UTC
This bug is a result of an analysis of bug #465238.  There sys-apps/elfix doesn't compile against dev-libs/libelf because the latter lacks ELF_C_RDWR_MMAP.  This is a problem on uclibc systems where one cannot build dev-libs/elfutils due to numerous glibc-isms (some easily avoidable, others not).

libelf does provide ELF_C_RDWR, but it doesn't work.  (Interestingly enough, it also does not work with elfutils, although there ELF_C_RDWR_MMAP does.)

Here's a little poc to demonstrate the problem --- its reduced code with no sanity checks.  It simply toggles the SEGMEXEC flag on any program header marked PT_PAX_FLAGS.  Replace ELF_C_RDWR_MMAP with ELF_C_RDWR as needed to demonstrate the above.


#include <stdio.h>
#include <stdlib.h>
#include <gelf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PF_SEGMEXEC (1 << 6)

int main (int argc, char *argv[])
{
  Elf *elf;
  GElf_Phdr phdr;
  size_t i, phnum;
  int fd = open (argv[1], O_RDWR);

  elf_version(EV_CURRENT);
  elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);

  elf_getphdrnum (elf, &phnum);

  for (i=0; i<phnum; i++)
    {
      gelf_getphdr (elf, i, &phdr);
      if (phdr.p_type == PT_PAX_FLAGS)
        {
          phdr.p_flags ^= PF_SEGMEXEC;
          gelf_update_phdr (elf, i, &phdr);
        }
  }

  elf_end (elf);
  close (fd);
}

Reproducible: Always
Comment 1 Anthony Basile gentoo-dev 2013-05-31 10:28:57 UTC
My attempt to get elfutils to build on uclibc are in bug #470884