File: chrpath.c

package info (click to toggle)
chrpath 0.18-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: sh: 3,169; ansic: 684; makefile: 43
file content (362 lines) | stat: -rw-r--r-- 8,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
<URL:http://gcc.gnu.org/ml/gcc/1999-04n/msg01105.html>

Re: changing embedded RPATH in existing executables.

  To: geoffk@ozemail.com.au
  Subject: Re: changing embedded RPATH in existing executables.
  From: <peeter_joot@VNET.IBM.COM> (peeter joot)
  Date: Fri, 30 Apr 1999 16:14:44 -0400 (EDT)
  Cc: peeterj@ca.ibm.com, egcs@cygnus.com, libc-hacker@cygnus.com, linux-gcc@vger.rutgers.edu
  Reply-To: <peeter_joot@VNET.IBM.COM>

> _Changing_ is a little tricky, but the attached program strips rpaths
> from executables (I find it essential for debugging the binutils).
> It's endian-dependent, if you want this for x86 you can just change
> the occurrences of 'MSB' to 'LSB' and compile (I should really fix
> that).

Hi Geoff,

With your program as a guide (and some peeks into libbfd, elf.h, a bit
of the glibc dynamic loader code, objdump, and a hex-editor) I was able to
figure out enough to find and change the rpath string.  That was fun!

This program assumes (unlike your original program) that there is only
one DT_RPATH tag in the dynamic section as even with multiple '-Wl,-rpath,'
commands in the link this seems to occur (they all get concatonated into
a : separated path).

Thanks for your help.  If you want to use this on non-x86 you have to change
the occurances of LSB back to MSB:)

Peeter
--
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <elf.h>
#if defined(HAVE_LINK_H)
#  include <link.h>
#endif /* HAVE_LINK_H */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "protos.h"


/**
 * Reads the section names table from an ELF file into memory
 */
char*
read_section_names(int fd, Elf_Ehdr ehdr)
{
  Elf_Shdr shdr;

  const size_t sz_shdr = is_e32() ? sizeof(Elf32_Shdr) : sizeof(Elf64_Shdr);
  const size_t sh_off = EHDRWU(e_shoff);
  const size_t sections_off = EHDRWU(e_shstrndx) * sz_shdr;

  if (lseek(fd, sh_off + sections_off, SEEK_SET) == -1)
  {
    perror ("positioning for section table");
    return NULL;
  }

  if (read(fd, &shdr, sz_shdr) != (ssize_t)sz_shdr)
  {
    perror ("reading section header");
    return NULL;
  }

  /* +1 for forced trailing null */
  char* strtab = (char *)calloc(1, SHDR_O(sh_size)+1);
  if (strtab == NULL)
    {
      perror ("allocating memory for string table");
      return NULL;
    }

  if (lseek(fd, SHDR_O(sh_offset), SEEK_SET) == -1)
  {
    perror ("positioning for string table");
    return NULL;
  }
  if (read(fd, strtab, SHDR_O(sh_size)) != (ssize_t)SHDR_O(sh_size))
  {
    perror ("reading string table");
    return NULL;
  }
  strtab[SHDR_O(sh_size)] = 0; /* make sure printed string is null terminated */
  return strtab;
}

/**
 * Looks up the name for a section, using its offset in the table
 */
char*
get_section_name(int name_off, char* section_names) {
  return section_names + name_off;
}

/**
 * Reads an ELF file, and reads or alters the RPATH setting.
 *
 * TODO:
 *  modify to add RPATH setting if none exists.
 */


int
chrpath(const char *filename, const char *newpath, int convert)
{
  int fd;
  Elf_Ehdr ehdr;
  int i;
  Elf_Phdr phdr;
  Elf_Shdr shdr;
  void *dyns;
  int rpathoff;
  char * strtab;
  char * rpath;
  unsigned int rpathlen;
  int oflags;
  int rpath_dyns_index;

  if (NULL == newpath && 0 == convert)
     oflags = O_RDONLY;
  else
     oflags = O_RDWR;

  fd = elf_open(filename, oflags, &ehdr);
  if (fd == -1)
  {
    perror ("elf_open");
    return 1;
  }

   if (0 != elf_find_dynamic_section(fd, &ehdr, &phdr))
   {
     perror("found no dynamic section");
     elf_close(fd);
     return 1;
   }

  dyns = malloc(PHDR(p_filesz));
  if (dyns == NULL)
    {
      perror ("allocating memory for dynamic section");
      elf_close(fd);
      return 1;
    }
  memset(dyns, 0, PHDR(p_filesz));
  if (lseek(fd, PHDR(p_offset), SEEK_SET) == -1
      || read(fd, dyns, PHDR(p_filesz)) != (ssize_t)PHDR(p_filesz))
    {
      perror ("reading dynamic section");
      free(dyns);
      elf_close(fd);
      return 1;
    }

  rpathoff = -1;
  for ( rpath_dyns_index = 0; DYNSS(rpath_dyns_index, d_tag) != DT_NULL;
        ++rpath_dyns_index )
    {
      if ( elf_dynpath_tag(DYNSS(rpath_dyns_index, d_tag)) )
      {
         rpathoff = DYNSU(rpath_dyns_index, d_un.d_ptr);
         break;
      }
    }
  if (rpathoff == -1)
    {
      printf("%s: no rpath or runpath tag found.\n", filename);
      free(dyns);
      elf_close(fd);
      return 2;
    }

  char* section_names = read_section_names(fd, ehdr);
  if (section_names == NULL) {
    free(dyns);
    elf_close(fd);
    return 1;
  }

  if (lseek(fd, EHDRWU(e_shoff), SEEK_SET) == -1)
  {
    perror ("positioning for sections");
    free(dyns);
    free(section_names);
    elf_close(fd);
    return 1;
  }

  const size_t sz_shdr = is_e32() ? sizeof(Elf32_Shdr) : sizeof(Elf64_Shdr);
  for (i = 0; i < EHDRHU(e_shnum); i++)
  {
    if (read(fd, &shdr, sz_shdr) != (ssize_t)sz_shdr)
    {
      perror ("reading section header");
      free(dyns);
      free(section_names);
      elf_close(fd);
      return 1;
    }
    if (SHDR_W(sh_type) != SHT_STRTAB)
      continue;
    char* name = get_section_name(SHDR_W(sh_name), section_names);
    if (strcmp(name, ".dynstr") == 0)
      break;
  }
  if (i == EHDRHU(e_shnum))
    {
      fprintf (stderr, "No string table found.\n");
      free(dyns);
      free(section_names);
      elf_close(fd);
      return 2;
    }
  /* +1 for forced trailing null */
  strtab = (char *)calloc(1, SHDR_O(sh_size)+1);
  if (strtab == NULL)
    {
      perror ("allocating memory for string table");
      free(dyns);
      free(section_names);
      elf_close(fd);
      return 1;
    }

  if (lseek(fd, SHDR_O(sh_offset), SEEK_SET) == -1)
  {
    perror ("positioning for string table");
    free(strtab);
    free(dyns);
    free(section_names);
    elf_close(fd);
    return 1;
  }
  if (read(fd, strtab, SHDR_O(sh_size)) != (ssize_t)SHDR_O(sh_size))
  {
    perror ("reading string table");
    free(strtab);
    free(dyns);
    free(section_names);
    elf_close(fd);
    return 1;
  }
  strtab[SHDR_O(sh_size)] = 0; /* make sure printed string is null terminated */

  if ((int)SHDR_O(sh_size) < rpathoff)
  {
    fprintf(stderr, "%s string offset not contained in string table\n",
            elf_tagname(DYNSS(rpath_dyns_index, d_tag)));
    free(strtab);
    free(dyns);
    free(section_names);
    elf_close(fd);
    return 5;
  }
  rpath = strtab+rpathoff;

#if defined(DT_RUNPATH)
  if (convert && DYNSS(rpath_dyns_index, d_tag) == DT_RPATH)
  {
    if (is_e32())
      ((Elf32_Dyn *)dyns)[rpath_dyns_index].d_tag = swap_bytes() ?
        bswap_32(DT_RUNPATH) : DT_RUNPATH;
    else
      ((Elf64_Dyn *)dyns)[rpath_dyns_index].d_tag = swap_bytes() ?
        bswap_64(DT_RUNPATH) : DT_RUNPATH;
    if (lseek(fd, PHDR(p_offset), SEEK_SET) == -1
        || write(fd, dyns, PHDR(p_filesz)) != (int)PHDR(p_filesz))
    {
      perror ("converting RPATH to RUNPATH");
      free(strtab);
      free(dyns);
      elf_close(fd);
      return 1;
    }
    printf("%s: RPATH converted to RUNPATH\n", filename);
  }
#endif /* DT_RUNPATH */

  printf("%s: %s=%s\n", filename, elf_tagname(DYNSS(rpath_dyns_index, d_tag)),
         rpath);

  if (NULL == newpath)
  {
    free(dyns);
    free(strtab);
    elf_close(fd);
    return 0;
  }

  rpathlen = strlen(rpath);

  /*
   * Calculate the maximum rpath length (will be equal to rpathlen unless
   * we have previously truncated it).
   */
  for ( i = rpathoff + rpathlen ; (i < (int)SHDR_O(sh_size)
                                   && strtab[i] == '\0') ; i++ )
    ;
  i--;

  if (i > (int)(rpathoff + rpathlen))
     rpathlen = i - rpathoff;

  if (strlen(newpath) > rpathlen)
  {
    fprintf(stderr, "new rpath '%s' too large; maximum length %i\n",
            newpath, rpathlen);
    free(dyns);
    free(strtab);
    elf_close(fd);
    return 7;
  }

  memset(rpath, 0, rpathlen);
  strcpy(rpath, newpath);

  if (lseek(fd, SHDR_O(sh_offset)+rpathoff, SEEK_SET) == -1)
  {
    perror ("positioning for RPATH");
    free(dyns);
    free(strtab);
    elf_close(fd);
    return 1;
  }
  if (write(fd, rpath, rpathlen) != (int)rpathlen)
  {
    perror ("writing RPATH");
    free(dyns);
    free(strtab);
    elf_close(fd);
    return 1;
  }
  printf("%s: new %s: %s\n", filename,
         elf_tagname(DYNSS(rpath_dyns_index, d_tag)), rpath);

  elf_close(fd);

  free(dyns);
  dyns = NULL;

  free(strtab);

  return 0;
}