Mercurial > hg > chrpath
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 0:b8f7423e385c | 1:bbbfb3f97919 |
|---|---|
| 24 #endif /* HAVE_LINK_H */ | 24 #endif /* HAVE_LINK_H */ |
| 25 #include <stdlib.h> | 25 #include <stdlib.h> |
| 26 #include "protos.h" | 26 #include "protos.h" |
| 27 #include <string.h> | 27 #include <string.h> |
| 28 | 28 |
| 29 /* Reads an ELF file, nukes all the RPATH entries. */ | 29 static int |
| 30 | 30 killrpath32(int fd, Elf32_Ehdr *ehdr) { |
| 31 int | |
| 32 killrpath(const char *filename) | |
| 33 { | |
| 34 int fd; | |
| 35 Elf_Ehdr ehdr; | |
| 36 int i; | 31 int i; |
| 37 Elf_Phdr phdr; | 32 Elf32_Phdr phdr; |
| 38 Elf_Dyn *dyns; | 33 Elf32_Dyn *dyns; |
| 39 int dynpos; | 34 int dynpos; |
| 40 | 35 |
| 41 fd = elf_open(filename, O_RDWR, &ehdr); | 36 if (0 != elf32_find_dynamic_section(fd, ehdr, &phdr)) |
| 42 | |
| 43 if (fd == -1) | |
| 44 { | |
| 45 perror ("elf_open"); | |
| 46 return 1; | |
| 47 } | |
| 48 | |
| 49 if (0 != elf_find_dynamic_section(fd, &ehdr, &phdr)) | |
| 50 { | 37 { |
| 51 perror("found no dynamic section"); | 38 perror("found no dynamic section"); |
| 52 return 1; | 39 return 1; |
| 53 } | 40 } |
| 54 | 41 |
| 85 | 72 |
| 86 elf_close(fd); | 73 elf_close(fd); |
| 87 | 74 |
| 88 return 0; | 75 return 0; |
| 89 } | 76 } |
| 77 | |
| 78 static int | |
| 79 killrpath64(int fd, Elf64_Ehdr *ehdr) { | |
| 80 int i; | |
| 81 Elf64_Phdr phdr; | |
| 82 Elf64_Dyn *dyns; | |
| 83 int dynpos; | |
| 84 | |
| 85 if (0 != elf64_find_dynamic_section(fd, ehdr, &phdr)) | |
| 86 { | |
| 87 perror("found no dynamic section"); | |
| 88 return 1; | |
| 89 } | |
| 90 | |
| 91 dyns = malloc(phdr.p_memsz); | |
| 92 if (dyns == NULL) | |
| 93 { | |
| 94 perror ("allocating memory for dynamic section"); | |
| 95 return 1; | |
| 96 } | |
| 97 memset(dyns, 0, phdr.p_memsz); | |
| 98 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1 | |
| 99 || read(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz) | |
| 100 { | |
| 101 perror ("reading dynamic section"); | |
| 102 return 1; | |
| 103 } | |
| 104 | |
| 105 dynpos = 0; | |
| 106 for (i = 0; dyns[i].d_tag != DT_NULL; i++) | |
| 107 { | |
| 108 dyns[dynpos] = dyns[i]; | |
| 109 if ( ! elf_dynpath_tag(dyns[i].d_tag) ) | |
| 110 dynpos++; | |
| 111 } | |
| 112 for (; dynpos < i; dynpos++) | |
| 113 dyns[dynpos].d_tag = DT_NULL; | |
| 114 | |
| 115 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1 | |
| 116 || write(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz) | |
| 117 { | |
| 118 perror ("writing dynamic section"); | |
| 119 return 1; | |
| 120 } | |
| 121 | |
| 122 elf_close(fd); | |
| 123 | |
| 124 return 0; | |
| 125 } | |
| 126 | |
| 127 /* Reads an ELF file, nukes all the RPATH entries. */ | |
| 128 | |
| 129 int | |
| 130 killrpath(const char *filename) | |
| 131 { | |
| 132 int fd; | |
| 133 Elf32_Ehdr ehdr32; | |
| 134 Elf64_Ehdr ehdr64; | |
| 135 | |
| 136 fd = elf32_open(filename, O_RDWR, &ehdr32); | |
| 137 if (fd >= 0) | |
| 138 { | |
| 139 return killrpath32(fd, &ehdr32); | |
| 140 } | |
| 141 | |
| 142 fd = elf64_open(filename, O_RDWR, &ehdr64); | |
| 143 if (fd >= 0) | |
| 144 { | |
| 145 return killrpath64(fd, &ehdr64); | |
| 146 } | |
| 147 | |
| 148 perror ("elf_open"); | |
| 149 return 1; | |
| 150 } |
