2 <URL:http://gcc.gnu.org/ml/gcc/1999-04n/msg01105.html>
4 Re: changing embedded RPATH in existing executables.
6 To: geoffk@ozemail.com.au
7 Subject: Re: changing embedded RPATH in existing executables.
8 From: <peeter_joot@VNET.IBM.COM> (peeter joot)
9 Date: Fri, 30 Apr 1999 16:14:44 -0400 (EDT)
10 Cc: peeterj@ca.ibm.com, egcs@cygnus.com, libc-hacker@cygnus.com, linux-gcc@vger.rutgers.edu
11 Reply-To: <peeter_joot@VNET.IBM.COM>
13 > _Changing_ is a little tricky, but the attached program strips rpaths
14 > from executables (I find it essential for debugging the binutils).
15 > It's endian-dependent, if you want this for x86 you can just change
16 > the occurrences of 'MSB' to 'LSB' and compile (I should really fix
21 With your program as a guide (and some peeks into libbfd, elf.h, a bit
22 of the glibc dynamic loader code, objdump, and a hex-editor) I was able to
23 figure out enough to find and change the rpath string. That was fun!
25 This program assumes (unlike your original program) that there is only
26 one DT_RPATH tag in the dynamic section as even with multiple '-Wl,-rpath,'
27 commands in the link this seems to occur (they all get concatonated into
30 Thanks for your help. If you want to use this on non-x86 you have to change
31 the occurances of LSB back to MSB:)
45 #if defined(HAVE_LINK_H)
47 #endif /* HAVE_LINK_H */
54 * Reads an ELF file, and reads or alters the RPATH setting.
57 * modify to add RPATH setting if none exists.
62 chrpath(const char *filename, const char *newpath, int convert)
73 unsigned int rpathlen;
77 if (NULL == newpath && 0 == convert)
82 fd = elf_open(filename, oflags, &ehdr);
89 if (0 != elf_find_dynamic_section(fd, &ehdr, &phdr))
91 perror("found no dynamic section");
95 dyns = malloc(phdr.p_filesz);
98 perror ("allocating memory for dynamic section");
101 memset(dyns, 0, phdr.p_filesz);
102 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
103 || read(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
105 perror ("reading dynamic section");
111 for ( rpath_dyns_index = 0; dyns[rpath_dyns_index].d_tag != DT_NULL;
114 if ( elf_dynpath_tag(dyns[rpath_dyns_index].d_tag) )
116 rpathoff = dyns[rpath_dyns_index].d_un.d_ptr;
122 printf("%s: no rpath or runpath tag found.\n", filename);
127 if (lseek(fd, ehdr.e_shoff, SEEK_SET) == -1)
129 perror ("positioning for sections");
134 for (i = 0; i < ehdr.e_shnum; i++)
136 if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
138 perror ("reading section header");
142 if (shdr.sh_type == SHT_STRTAB)
145 if (i == ehdr.e_shnum)
147 fprintf (stderr, "No string table found.\n");
151 strtab = (char *)malloc(shdr.sh_size);
154 perror ("allocating memory for string table");
158 memset(strtab, 0, shdr.sh_size);
160 if (lseek(fd, shdr.sh_offset, SEEK_SET) == -1)
162 perror ("positioning for string table");
167 if (read(fd, strtab, shdr.sh_size) != (int)shdr.sh_size)
169 perror ("reading string table");
175 if ((int)shdr.sh_size < rpathoff)
177 fprintf(stderr, "%s string offset not contained in string table",
178 elf_tagname(dyns[rpath_dyns_index].d_tag));
183 rpath = strtab+rpathoff;
185 #if defined(DT_RUNPATH)
186 if (convert && dyns[rpath_dyns_index].d_tag == DT_RPATH)
188 dyns[rpath_dyns_index].d_tag = DT_RUNPATH;
189 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1
190 || write(fd, dyns, phdr.p_filesz) != (int)phdr.p_filesz)
192 perror ("converting RPATH to RUNPATH");
195 printf("%s: RPATH converted to RUNPATH\n", filename);
197 #endif /* DT_RUNPATH */
199 printf("%s: %s=%s\n", filename, elf_tagname(dyns[rpath_dyns_index].d_tag),
209 rpathlen = strlen(rpath);
212 * Calculate the maximum rpath length (will be equal to rpathlen unless
213 * we have previously truncated it).
215 for ( i = rpathoff + rpathlen ; (i < (int)shdr.sh_size
216 && strtab[i] == '\0') ; i++ )
220 if (i > (int)(rpathoff + rpathlen))
221 rpathlen = i - rpathoff;
223 if (strlen(newpath) > rpathlen)
225 fprintf(stderr, "new rpath '%s' too large; maximum length %i\n",
232 memset(rpath, 0, rpathlen);
233 strcpy(rpath, newpath);
235 if (lseek(fd, shdr.sh_offset+rpathoff, SEEK_SET) == -1)
237 perror ("positioning for RPATH");
242 if (write(fd, rpath, rpathlen) != (int)rpathlen)
244 perror ("writing RPATH");
249 printf("%s: new %s: %s\n", filename,
250 elf_tagname(dyns[rpath_dyns_index].d_tag), rpath);