annotate chrpath.c @ 2:7bf4a164d5bb default tip

fix bug: long_options have to be zero terminated
author Peter Meerwald <p.meerwald@bct-electronic.com>
date Fri, 20 Jul 2012 11:28:30 +0200
parents bbbfb3f97919
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
1 /*
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
2 <URL:http://gcc.gnu.org/ml/gcc/1999-04n/msg01105.html>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
3
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
4 Re: changing embedded RPATH in existing executables.
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
5
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
6 To: geoffk@ozemail.com.au
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
7 Subject: Re: changing embedded RPATH in existing executables.
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
8 From: <peeter_joot@VNET.IBM.COM> (peeter joot)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
9 Date: Fri, 30 Apr 1999 16:14:44 -0400 (EDT)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
10 Cc: peeterj@ca.ibm.com, egcs@cygnus.com, libc-hacker@cygnus.com, linux-gcc@vger.rutgers.edu
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
11 Reply-To: <peeter_joot@VNET.IBM.COM>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
12
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
13 > _Changing_ is a little tricky, but the attached program strips rpaths
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
14 > from executables (I find it essential for debugging the binutils).
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
15 > It's endian-dependent, if you want this for x86 you can just change
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
16 > the occurrences of 'MSB' to 'LSB' and compile (I should really fix
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
17 > that).
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
18
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
19 Hi Geoff,
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
20
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
21 With your program as a guide (and some peeks into libbfd, elf.h, a bit
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
22 of the glibc dynamic loader code, objdump, and a hex-editor) I was able to
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
23 figure out enough to find and change the rpath string. That was fun!
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
24
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
25 This program assumes (unlike your original program) that there is only
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
26 one DT_RPATH tag in the dynamic section as even with multiple '-Wl,-rpath,'
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
27 commands in the link this seems to occur (they all get concatonated into
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
28 a : separated path).
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
29
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
30 Thanks for your help. If you want to use this on non-x86 you have to change
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
31 the occurances of LSB back to MSB:)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
32
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
33 Peeter
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
34 --
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
35 */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
36
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
37 #ifdef HAVE_CONFIG_H
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
38 # include "config.h"
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
39 #endif
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
40
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
41 #include <stdio.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
42 #include <unistd.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
43 #include <fcntl.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
44 #include <elf.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
45 #if defined(HAVE_LINK_H)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
46 # include <link.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
47 #endif /* HAVE_LINK_H */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
48 #include <stdlib.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
49 #include <string.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
50 #include <sys/stat.h>
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
51 #include "protos.h"
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
52
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
53 /**
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
54 * Reads an ELF file, and reads or alters the RPATH setting.
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
55 *
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
56 * TODO:
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
57 * modify to add RPATH setting if none exists.
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
58 */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
59
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
60
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
61 static int
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
62 chrpath32(int fd, const char *filename, const char *newpath, Elf32_Ehdr *ehdr, int convert) {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
63 Elf32_Phdr phdr;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
64 Elf32_Shdr shdr;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
65 Elf32_Dyn *dyns;
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
66 int i;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
67 int rpathoff;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
68 char * strtab;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
69 char * rpath;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
70 unsigned int rpathlen;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
71 int rpath_dyns_index;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
72
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
73 if (0 != elf32_find_dynamic_section(fd, ehdr, &phdr))
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
74 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
75 perror("found no dynamic section");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
76 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
77 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
78
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
79 dyns = malloc(phdr.p_filesz);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
80 if (dyns == NULL)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
81 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
82 perror ("allocating memory for dynamic section");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
83 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
84 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
85 memset(dyns, 0, phdr.p_filesz);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
86 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
87 || read(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
88 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
89 perror ("reading dynamic section");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
90 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
91 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
92 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
93
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
94 rpathoff = -1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
95 for ( rpath_dyns_index = 0; dyns[rpath_dyns_index].d_tag != DT_NULL;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
96 ++rpath_dyns_index )
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
97 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
98 if ( elf_dynpath_tag(dyns[rpath_dyns_index].d_tag) )
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
99 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
100 rpathoff = dyns[rpath_dyns_index].d_un.d_ptr;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
101 break;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
102 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
103 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
104 if (rpathoff == -1)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
105 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
106 printf("%s: no rpath or runpath tag found.\n", filename);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
107 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
108 return 2;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
109 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
110
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
111 if (lseek(fd, ehdr->e_shoff, SEEK_SET) == -1)
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
112 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
113 perror ("positioning for sections");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
114 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
115 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
116 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
117
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
118 for (i = 0; i < ehdr->e_shnum; i++)
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
119 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
120 if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
121 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
122 perror ("reading section header");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
123 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
124 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
125 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
126 if (shdr.sh_type == SHT_STRTAB)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
127 break;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
128 }
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
129 if (i == ehdr->e_shnum)
0
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
130 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
131 fprintf (stderr, "No string table found.\n");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
132 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
133 return 2;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
134 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
135 strtab = (char *)malloc(shdr.sh_size);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
136 if (strtab == NULL)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
137 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
138 perror ("allocating memory for string table");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
139 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
140 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
141 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
142 memset(strtab, 0, shdr.sh_size);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
143
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
144 if (lseek(fd, shdr.sh_offset, SEEK_SET) == -1)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
145 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
146 perror ("positioning for string table");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
147 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
148 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
149 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
150 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
151 if (read(fd, strtab, shdr.sh_size) != (int)shdr.sh_size)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
152 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
153 perror ("reading string table");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
154 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
155 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
156 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
157 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
158
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
159 if ((int)shdr.sh_size < rpathoff)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
160 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
161 fprintf(stderr, "%s string offset not contained in string table",
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
162 elf_tagname(dyns[rpath_dyns_index].d_tag));
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
163 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
164 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
165 return 5;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
166 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
167 rpath = strtab+rpathoff;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
168
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
169 #if defined(DT_RUNPATH)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
170 if (convert && dyns[rpath_dyns_index].d_tag == DT_RPATH)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
171 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
172 dyns[rpath_dyns_index].d_tag = DT_RUNPATH;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
173 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
174 || write(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
175 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
176 perror ("converting RPATH to RUNPATH");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
177 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
178 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
179 printf("%s: RPATH converted to RUNPATH\n", filename);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
180 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
181 #endif /* DT_RUNPATH */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
182
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
183 printf("%s: %s=%s\n", filename, elf_tagname(dyns[rpath_dyns_index].d_tag),
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
184 rpath);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
185
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
186 if (NULL == newpath)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
187 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
188 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
189 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
190 return 0;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
191 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
192
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
193 rpathlen = strlen(rpath);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
194
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
195 /*
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
196 * Calculate the maximum rpath length (will be equal to rpathlen unless
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
197 * we have previously truncated it).
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
198 */
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
199 for ( i = rpathoff + rpathlen ; (i < (int)shdr.sh_size
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
200 && strtab[i] == '\0') ; i++ )
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
201 ;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
202 i--;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
203
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
204 if (i > (int)(rpathoff + rpathlen))
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
205 rpathlen = i - rpathoff;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
206
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
207 if (strlen(newpath) > rpathlen)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
208 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
209 fprintf(stderr, "new rpath '%s' too large; maximum length %i\n",
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
210 newpath, rpathlen);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
211 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
212 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
213 return 7;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
214 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
215
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
216 memset(rpath, 0, rpathlen);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
217 strcpy(rpath, newpath);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
218
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
219 if (lseek(fd, shdr.sh_offset+rpathoff, SEEK_SET) == -1)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
220 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
221 perror ("positioning for RPATH");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
222 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
223 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
224 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
225 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
226 if (write(fd, rpath, rpathlen) != (int)rpathlen)
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
227 {
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
228 perror ("writing RPATH");
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
229 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
230 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
231 return 1;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
232 }
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
233 printf("%s: new %s: %s\n", filename,
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
234 elf_tagname(dyns[rpath_dyns_index].d_tag), rpath);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
235
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
236 elf_close(fd);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
237
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
238 free(dyns);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
239 dyns = NULL;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
240
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
241 free(strtab);
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
242
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
243 return 0;
b8f7423e385c import 0.13
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff changeset
244 }
1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
245
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
246 static int
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
247 chrpath64(int fd, const char *filename, const char *newpath, Elf64_Ehdr *ehdr, int convert) {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
248 Elf64_Phdr phdr;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
249 Elf64_Shdr shdr;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
250 Elf64_Dyn *dyns;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
251 int i;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
252 int rpathoff;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
253 char * strtab;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
254 char * rpath;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
255 unsigned int rpathlen;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
256 int rpath_dyns_index;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
257
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
258 if (0 != elf64_find_dynamic_section(fd, ehdr, &phdr))
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
259 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
260 perror("found no dynamic section");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
261 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
262 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
263
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
264 dyns = malloc(phdr.p_filesz);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
265 if (dyns == NULL)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
266 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
267 perror ("allocating memory for dynamic section");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
268 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
269 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
270 memset(dyns, 0, phdr.p_filesz);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
271 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
272 || read(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
273 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
274 perror ("reading dynamic section");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
275 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
276 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
277 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
278
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
279 rpathoff = -1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
280 for ( rpath_dyns_index = 0; dyns[rpath_dyns_index].d_tag != DT_NULL;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
281 ++rpath_dyns_index )
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
282 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
283 if ( elf_dynpath_tag(dyns[rpath_dyns_index].d_tag) )
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
284 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
285 rpathoff = dyns[rpath_dyns_index].d_un.d_ptr;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
286 break;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
287 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
288 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
289 if (rpathoff == -1)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
290 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
291 printf("%s: no rpath or runpath tag found.\n", filename);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
292 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
293 return 2;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
294 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
295
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
296 if (lseek(fd, ehdr->e_shoff, SEEK_SET) == -1)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
297 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
298 perror ("positioning for sections");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
299 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
300 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
301 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
302
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
303 for (i = 0; i < ehdr->e_shnum; i++)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
304 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
305 if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
306 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
307 perror ("reading section header");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
308 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
309 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
310 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
311 if (shdr.sh_type == SHT_STRTAB)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
312 break;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
313 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
314 if (i == ehdr->e_shnum)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
315 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
316 fprintf (stderr, "No string table found.\n");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
317 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
318 return 2;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
319 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
320 strtab = (char *)malloc(shdr.sh_size);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
321 if (strtab == NULL)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
322 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
323 perror ("allocating memory for string table");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
324 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
325 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
326 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
327 memset(strtab, 0, shdr.sh_size);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
328
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
329 if (lseek(fd, shdr.sh_offset, SEEK_SET) == -1)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
330 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
331 perror ("positioning for string table");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
332 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
333 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
334 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
335 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
336 if (read(fd, strtab, shdr.sh_size) != (int)shdr.sh_size)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
337 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
338 perror ("reading string table");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
339 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
340 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
341 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
342 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
343
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
344 if ((int)shdr.sh_size < rpathoff)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
345 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
346 fprintf(stderr, "%s string offset not contained in string table",
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
347 elf_tagname(dyns[rpath_dyns_index].d_tag));
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
348 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
349 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
350 return 5;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
351 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
352 rpath = strtab+rpathoff;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
353
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
354 #if defined(DT_RUNPATH)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
355 if (convert && dyns[rpath_dyns_index].d_tag == DT_RPATH)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
356 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
357 dyns[rpath_dyns_index].d_tag = DT_RUNPATH;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
358 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
359 || write(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
360 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
361 perror ("converting RPATH to RUNPATH");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
362 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
363 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
364 printf("%s: RPATH converted to RUNPATH\n", filename);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
365 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
366 #endif /* DT_RUNPATH */
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
367
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
368 printf("%s: %s=%s\n", filename, elf_tagname(dyns[rpath_dyns_index].d_tag),
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
369 rpath);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
370
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
371 if (NULL == newpath)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
372 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
373 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
374 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
375 return 0;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
376 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
377
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
378 rpathlen = strlen(rpath);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
379
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
380 /*
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
381 * Calculate the maximum rpath length (will be equal to rpathlen unless
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
382 * we have previously truncated it).
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
383 */
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
384 for ( i = rpathoff + rpathlen ; (i < (int)shdr.sh_size
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
385 && strtab[i] == '\0') ; i++ )
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
386 ;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
387 i--;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
388
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
389 if (i > (int)(rpathoff + rpathlen))
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
390 rpathlen = i - rpathoff;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
391
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
392 if (strlen(newpath) > rpathlen)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
393 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
394 fprintf(stderr, "new rpath '%s' too large; maximum length %i\n",
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
395 newpath, rpathlen);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
396 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
397 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
398 return 7;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
399 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
400
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
401 memset(rpath, 0, rpathlen);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
402 strcpy(rpath, newpath);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
403
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
404 if (lseek(fd, shdr.sh_offset+rpathoff, SEEK_SET) == -1)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
405 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
406 perror ("positioning for RPATH");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
407 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
408 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
409 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
410 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
411 if (write(fd, rpath, rpathlen) != (int)rpathlen)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
412 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
413 perror ("writing RPATH");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
414 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
415 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
416 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
417 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
418 printf("%s: new %s: %s\n", filename,
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
419 elf_tagname(dyns[rpath_dyns_index].d_tag), rpath);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
420
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
421 elf_close(fd);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
422
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
423 free(dyns);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
424 dyns = NULL;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
425
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
426 free(strtab);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
427
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
428 return 0;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
429 }
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
430
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
431 int
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
432 chrpath(const char *filename, const char *newpath, int convert)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
433 {
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
434 int fd;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
435 Elf32_Ehdr ehdr32;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
436 Elf64_Ehdr ehdr64;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
437 int oflags;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
438
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
439 if (NULL == newpath && 0 == convert)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
440 oflags = O_RDONLY;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
441 else
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
442 oflags = O_RDWR;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
443
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
444 fd = elf32_open(filename, oflags, &ehdr32);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
445 if (fd >= 0)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
446 return chrpath32(fd, filename, newpath, &ehdr32, convert);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
447
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
448 fd = elf64_open(filename, oflags, &ehdr64);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
449 if (fd >= 0)
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
450 return chrpath64(fd, filename, newpath, &ehdr64, convert);
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
451
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
452 perror ("elf_open");
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
453 return 1;
bbbfb3f97919 implement 32 and 64 bit support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
454 }

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