Mercurial > hg > chrpath
diff killrpath.c @ 1:bbbfb3f97919
implement 32 and 64 bit support
author | Peter Meerwald <p.meerwald@bct-electronic.com> |
---|---|
date | Fri, 20 Jul 2012 11:26:24 +0200 |
parents | b8f7423e385c |
children |
line wrap: on
line diff
--- a/killrpath.c Fri Jul 20 01:51:24 2012 +0200 +++ b/killrpath.c Fri Jul 20 11:26:24 2012 +0200 @@ -26,27 +26,14 @@ #include "protos.h" #include <string.h> -/* Reads an ELF file, nukes all the RPATH entries. */ - -int -killrpath(const char *filename) -{ - int fd; - Elf_Ehdr ehdr; +static int +killrpath32(int fd, Elf32_Ehdr *ehdr) { int i; - Elf_Phdr phdr; - Elf_Dyn *dyns; + Elf32_Phdr phdr; + Elf32_Dyn *dyns; int dynpos; - fd = elf_open(filename, O_RDWR, &ehdr); - - if (fd == -1) - { - perror ("elf_open"); - return 1; - } - - if (0 != elf_find_dynamic_section(fd, &ehdr, &phdr)) + if (0 != elf32_find_dynamic_section(fd, ehdr, &phdr)) { perror("found no dynamic section"); return 1; @@ -87,3 +74,77 @@ return 0; } + +static int +killrpath64(int fd, Elf64_Ehdr *ehdr) { + int i; + Elf64_Phdr phdr; + Elf64_Dyn *dyns; + int dynpos; + + if (0 != elf64_find_dynamic_section(fd, ehdr, &phdr)) + { + perror("found no dynamic section"); + return 1; + } + + dyns = malloc(phdr.p_memsz); + if (dyns == NULL) + { + perror ("allocating memory for dynamic section"); + return 1; + } + memset(dyns, 0, phdr.p_memsz); + if (lseek(fd, phdr.p_offset, SEEK_SET) == -1 + || read(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz) + { + perror ("reading dynamic section"); + return 1; + } + + dynpos = 0; + for (i = 0; dyns[i].d_tag != DT_NULL; i++) + { + dyns[dynpos] = dyns[i]; + if ( ! elf_dynpath_tag(dyns[i].d_tag) ) + dynpos++; + } + for (; dynpos < i; dynpos++) + dyns[dynpos].d_tag = DT_NULL; + + if (lseek(fd, phdr.p_offset, SEEK_SET) == -1 + || write(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz) + { + perror ("writing dynamic section"); + return 1; + } + + elf_close(fd); + + return 0; +} + +/* Reads an ELF file, nukes all the RPATH entries. */ + +int +killrpath(const char *filename) +{ + int fd; + Elf32_Ehdr ehdr32; + Elf64_Ehdr ehdr64; + + fd = elf32_open(filename, O_RDWR, &ehdr32); + if (fd >= 0) + { + return killrpath32(fd, &ehdr32); + } + + fd = elf64_open(filename, O_RDWR, &ehdr64); + if (fd >= 0) + { + return killrpath64(fd, &ehdr64); + } + + perror ("elf_open"); + return 1; +}