comparison killrpath.c @ 0:b8f7423e385c

import 0.13
author Peter Meerwald <pmeerw@pmeerw.net>
date Fri, 20 Jul 2012 01:51:24 +0200
parents
children bbbfb3f97919
comparison
equal deleted inserted replaced
-1:000000000000 0:b8f7423e385c
1 /*
2 Taken from another list:
3
4 _Changing_ is a little tricky, but the attached program strips rpaths
5 from executables (I find it essential for debugging the binutils).
6 It's endian-dependent, if you want this for x86 you can just change
7 the occurrences of 'MSB' to 'LSB' and compile (I should really fix
8 that).
9
10 --
11 Geoffrey Keating <geoffk@ozemail.com.au>
12 */
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <elf.h>
22 #if defined(HAVE_LINK_H)
23 # include <link.h>
24 #endif /* HAVE_LINK_H */
25 #include <stdlib.h>
26 #include "protos.h"
27 #include <string.h>
28
29 /* Reads an ELF file, nukes all the RPATH entries. */
30
31 int
32 killrpath(const char *filename)
33 {
34 int fd;
35 Elf_Ehdr ehdr;
36 int i;
37 Elf_Phdr phdr;
38 Elf_Dyn *dyns;
39 int dynpos;
40
41 fd = elf_open(filename, O_RDWR, &ehdr);
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 {
51 perror("found no dynamic section");
52 return 1;
53 }
54
55 dyns = malloc(phdr.p_memsz);
56 if (dyns == NULL)
57 {
58 perror ("allocating memory for dynamic section");
59 return 1;
60 }
61 memset(dyns, 0, phdr.p_memsz);
62 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
63 || read(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
64 {
65 perror ("reading dynamic section");
66 return 1;
67 }
68
69 dynpos = 0;
70 for (i = 0; dyns[i].d_tag != DT_NULL; i++)
71 {
72 dyns[dynpos] = dyns[i];
73 if ( ! elf_dynpath_tag(dyns[i].d_tag) )
74 dynpos++;
75 }
76 for (; dynpos < i; dynpos++)
77 dyns[dynpos].d_tag = DT_NULL;
78
79 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
80 || write(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
81 {
82 perror ("writing dynamic section");
83 return 1;
84 }
85
86 elf_close(fd);
87
88 return 0;
89 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.