blob: d3791787c3b10aabf7d45fe472c0eb327120ebd4 [file] [log] [blame]
Joe Damatof0bd98b2010-11-08 15:47:42 -08001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Juan Cespedesd914a202004-11-10 00:15:33 +01003#include <endian.h>
Juan Cespedes96935a91997-08-09 23:45:39 +02004#include <errno.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01005#include <error.h>
Juan Cespedes96935a91997-08-09 23:45:39 +02006#include <fcntl.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01007#include <gelf.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -08008#include <inttypes.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01009#include <stdint.h>
10#include <stdlib.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020011#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010012#include <unistd.h>
Petr Machatafe1c1712010-10-27 16:57:34 +020013#include <assert.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020014
Juan Cespedesf7281232009-06-25 16:11:21 +020015#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010016#include "proc.h"
Juan Cespedes96935a91997-08-09 23:45:39 +020017
Joe Damato7a2bdf82010-11-08 15:47:41 -080018void do_close_elf(struct ltelf *lte);
19void add_library_symbol(GElf_Addr addr, const char *name,
20 struct library_symbol **library_symbolspp,
21 enum toplt type_of_plt, int is_weak);
22int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym);
Ian Wienand4bfcedd2006-08-07 04:03:15 +020023static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr addr);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020024
Joe Damato7a2bdf82010-11-08 15:47:41 -080025struct library_symbol *library_symbols = NULL;
Joe Damatof0bd98b2010-11-08 15:47:42 -080026struct ltelf main_lte;
27
Paul Gilliambe320772006-04-24 22:06:23 +020028#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010029extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020030#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010031
Petr Machatafe1c1712010-10-27 16:57:34 +020032#ifndef DT_PPC_GOT
33# define DT_PPC_GOT (DT_LOPROC + 0)
34#endif
35
36#define PPC_PLT_STUB_SIZE 16
37
38static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
39{
40 Elf_Data *data = elf_getdata(scn, NULL);
41 if (data == NULL || elf_getdata(scn, data) != NULL
42 || data->d_off || data->d_size != shdr->sh_size)
43 return NULL;
44 return data;
45}
46
47static int inside(GElf_Addr addr, GElf_Shdr *shdr)
48{
49 return addr >= shdr->sh_addr
50 && addr < shdr->sh_addr + shdr->sh_size;
51}
52
53static int maybe_pick_section(GElf_Addr addr,
54 Elf_Scn *in_sec, GElf_Shdr *in_shdr,
55 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
56{
57 if (inside (addr, in_shdr)) {
58 *tgt_sec = in_sec;
59 *tgt_shdr = *in_shdr;
60 return 1;
61 }
62 return 0;
63}
64
65static int get_section_covering(struct ltelf *lte, GElf_Addr addr,
66 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
67{
68 int i;
69 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
70 Elf_Scn *scn;
71 GElf_Shdr shdr;
72
73 scn = elf_getscn(lte->elf, i);
74 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
75 debug(1, "Couldn't read section or header.");
76 return 0;
77 }
78
79 if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr))
80 return 1;
81 }
82
83 return 0;
84}
85
86static GElf_Addr read32be(Elf_Data *data, size_t offset)
87{
88 if (data->d_size < offset + 4) {
89 debug(1, "Not enough data to read 32bit value at offset %zd.",
90 offset);
91 return 0;
92 }
93
94 unsigned char const *buf = data->d_buf + offset;
95 return ((Elf32_Word)buf[0] << 24)
96 | ((Elf32_Word)buf[1] << 16)
97 | ((Elf32_Word)buf[2] << 8)
98 | ((Elf32_Word)buf[3]);
99}
100
101static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot,
102 Elf_Data *plt_data)
103{
104 Elf_Scn *ppcgot_sec = NULL;
105 GElf_Shdr ppcgot_shdr;
106 if (ppcgot != 0
107 && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr))
108 // xxx should be the log out
109 fprintf(stderr,
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800110 "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n",
Petr Machatafe1c1712010-10-27 16:57:34 +0200111 ppcgot);
112
113 if (ppcgot_sec != NULL) {
114 Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr);
115 if (data == NULL
116 || data->d_size < 8 )
117 debug(1, "Couldn't read GOT data.");
118 else {
119 // where PPCGOT begins in .got
120 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
121 GElf_Addr glink_vma = read32be(data, offset + 4);
122 if (glink_vma != 0) {
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800123 debug(1, "PPC GOT glink_vma address: %#" PRIx64,
Petr Machatafe1c1712010-10-27 16:57:34 +0200124 glink_vma);
125 return glink_vma;
126 }
127 }
128 }
129
130 if (plt_data != NULL) {
131 GElf_Addr glink_vma = read32be(plt_data, 0);
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800132 debug(1, ".plt glink_vma address: %#" PRIx64, glink_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200133 return glink_vma;
134 }
135
136 return 0;
137}
138
Petr Machata1974dbc2011-08-19 18:58:01 +0200139int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200140open_elf(struct ltelf *lte, const char *filename)
141{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100142 lte->fd = open(filename, O_RDONLY);
143 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200144 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200145
Petr Machata02bd9ec2011-09-21 17:38:59 +0200146 elf_version(EV_CURRENT);
147
Juan Cespedesd914a202004-11-10 00:15:33 +0100148#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100149 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200150#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200152#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200153
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
155 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200156
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100157 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
158 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
159 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200160
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100161 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
162 error(EXIT_FAILURE, 0,
163 "\"%s\" is not an ELF executable nor shared library",
164 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200165
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100166 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
167 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100168#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100169 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
170 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100171#endif
172#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100173 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
174 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100175#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 )
177 error(EXIT_FAILURE, 0,
178 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100179
Petr Machata02bd9ec2011-09-21 17:38:59 +0200180 return 0;
181}
182
183int
184do_init_elf(struct ltelf *lte, const char *filename) {
185 int i;
186 GElf_Addr relplt_addr = 0;
187 size_t relplt_size = 0;
188
189 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
190 debug(1, "Reading ELF from %s...", filename);
191
192 if (open_elf(lte, filename) < 0)
193 return -1;
194
Petr Machatafe1c1712010-10-27 16:57:34 +0200195 Elf_Data *plt_data = NULL;
196 GElf_Addr ppcgot = 0;
197
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
199 Elf_Scn *scn;
200 GElf_Shdr shdr;
201 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100202
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100203 scn = elf_getscn(lte->elf, i);
204 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
205 error(EXIT_FAILURE, 0,
206 "Couldn't get section header from \"%s\"",
207 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100208
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
210 if (name == NULL)
211 error(EXIT_FAILURE, 0,
212 "Couldn't get section header from \"%s\"",
213 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100214
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100215 if (shdr.sh_type == SHT_SYMTAB) {
216 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100217
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100218 lte->symtab = elf_getdata(scn, NULL);
219 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
220 if ((lte->symtab == NULL
221 || elf_getdata(scn, lte->symtab) != NULL)
222 && opt_x != NULL)
223 error(EXIT_FAILURE, 0,
224 "Couldn't get .symtab data from \"%s\"",
225 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100226
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 scn = elf_getscn(lte->elf, shdr.sh_link);
228 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
229 error(EXIT_FAILURE, 0,
230 "Couldn't get section header from \"%s\"",
231 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100232
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 data = elf_getdata(scn, NULL);
234 if (data == NULL || elf_getdata(scn, data) != NULL
235 || shdr.sh_size != data->d_size || data->d_off)
236 error(EXIT_FAILURE, 0,
237 "Couldn't get .strtab data from \"%s\"",
238 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100239
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100240 lte->strtab = data->d_buf;
241 } else if (shdr.sh_type == SHT_DYNSYM) {
242 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100243
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100244 lte->dynsym = elf_getdata(scn, NULL);
245 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
246 if (lte->dynsym == NULL
247 || elf_getdata(scn, lte->dynsym) != NULL)
248 error(EXIT_FAILURE, 0,
249 "Couldn't get .dynsym data from \"%s\"",
250 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100251
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100252 scn = elf_getscn(lte->elf, shdr.sh_link);
253 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
254 error(EXIT_FAILURE, 0,
255 "Couldn't get section header from \"%s\"",
256 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100257
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 data = elf_getdata(scn, NULL);
259 if (data == NULL || elf_getdata(scn, data) != NULL
260 || shdr.sh_size != data->d_size || data->d_off)
261 error(EXIT_FAILURE, 0,
262 "Couldn't get .dynstr data from \"%s\"",
263 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100264
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 lte->dynstr = data->d_buf;
266 } else if (shdr.sh_type == SHT_DYNAMIC) {
267 Elf_Data *data;
268 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100269
Joe Damato87f4f582010-11-08 15:47:36 -0800270 lte->dyn_addr = shdr.sh_addr;
271 lte->dyn_sz = shdr.sh_size;
272
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100273 data = elf_getdata(scn, NULL);
274 if (data == NULL || elf_getdata(scn, data) != NULL)
275 error(EXIT_FAILURE, 0,
276 "Couldn't get .dynamic data from \"%s\"",
277 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100278
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
280 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100281
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 if (gelf_getdyn(data, j, &dyn) == NULL)
283 error(EXIT_FAILURE, 0,
284 "Couldn't get .dynamic data from \"%s\"",
285 filename);
Eric Vaitl1228a912006-12-28 16:16:56 +0100286#ifdef __mips__
287/**
288 MIPS ABI Supplement:
Ian Wienand9a2ad352006-02-20 22:44:45 +0100289
Juan Cespedesf1350522008-12-16 18:19:58 +0100290 DT_PLTGOT This member holds the address of the .got section.
Eric Vaitl1228a912006-12-28 16:16:56 +0100291
292 DT_MIPS_SYMTABNO This member holds the number of entries in the
293 .dynsym section.
294
295 DT_MIPS_LOCAL_GOTNO This member holds the number of local global
296 offset table entries.
297
298 DT_MIPS_GOTSYM This member holds the index of the first dyamic
299 symbol table entry that corresponds to an entry in the gobal offset
300 table.
301
302 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200303 if(dyn.d_tag==DT_PLTGOT){
Juan Cespedesf1350522008-12-16 18:19:58 +0100304 lte->pltgot_addr=dyn.d_un.d_ptr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200305 }
306 if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){
307 lte->mips_local_gotno=dyn.d_un.d_val;
308 }
309 if(dyn.d_tag==DT_MIPS_GOTSYM){
310 lte->mips_gotsym=dyn.d_un.d_val;
311 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100312#endif // __mips__
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100313 if (dyn.d_tag == DT_JMPREL)
314 relplt_addr = dyn.d_un.d_ptr;
315 else if (dyn.d_tag == DT_PLTRELSZ)
316 relplt_size = dyn.d_un.d_val;
Petr Machatafe1c1712010-10-27 16:57:34 +0200317 else if (dyn.d_tag == DT_PPC_GOT) {
318 ppcgot = dyn.d_un.d_val;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800319 debug(1, "ppcgot %#" PRIx64, ppcgot);
Petr Machatafe1c1712010-10-27 16:57:34 +0200320 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100321 }
322 } else if (shdr.sh_type == SHT_HASH) {
323 Elf_Data *data;
324 size_t j;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100325
Petr Machata35fe5182006-07-18 12:58:12 +0200326 lte->hash_type = SHT_HASH;
327
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 data = elf_getdata(scn, NULL);
329 if (data == NULL || elf_getdata(scn, data) != NULL
330 || data->d_off || data->d_size != shdr.sh_size)
331 error(EXIT_FAILURE, 0,
332 "Couldn't get .hash data from \"%s\"",
333 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100334
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 if (shdr.sh_entsize == 4) {
336 /* Standard conforming ELF. */
337 if (data->d_type != ELF_T_WORD)
338 error(EXIT_FAILURE, 0,
339 "Couldn't get .hash data from \"%s\"",
340 filename);
341 lte->hash = (Elf32_Word *) data->d_buf;
342 } else if (shdr.sh_entsize == 8) {
343 /* Alpha or s390x. */
344 Elf32_Word *dst, *src;
345 size_t hash_count = data->d_size / 8;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100346
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100347 lte->hash = (Elf32_Word *)
348 malloc(hash_count * sizeof(Elf32_Word));
349 if (lte->hash == NULL)
350 error(EXIT_FAILURE, 0,
351 "Couldn't convert .hash section from \"%s\"",
352 filename);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200353 lte->lte_flags |= LTE_HASH_MALLOCED;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 dst = lte->hash;
355 src = (Elf32_Word *) data->d_buf;
356 if ((data->d_type == ELF_T_WORD
357 && __BYTE_ORDER == __BIG_ENDIAN)
358 || (data->d_type == ELF_T_XWORD
359 && lte->ehdr.e_ident[EI_DATA] ==
360 ELFDATA2MSB))
361 ++src;
362 for (j = 0; j < hash_count; ++j, src += 2)
363 *dst++ = *src;
364 } else
365 error(EXIT_FAILURE, 0,
366 "Unknown .hash sh_entsize in \"%s\"",
367 filename);
Petr Machata35fe5182006-07-18 12:58:12 +0200368 } else if (shdr.sh_type == SHT_GNU_HASH
369 && lte->hash == NULL) {
370 Elf_Data *data;
Petr Machata35fe5182006-07-18 12:58:12 +0200371
372 lte->hash_type = SHT_GNU_HASH;
373
374 if (shdr.sh_entsize != 0
375 && shdr.sh_entsize != 4) {
376 error(EXIT_FAILURE, 0,
Zachary T Welch3ba522f2010-12-14 15:12:47 -0800377 ".gnu.hash sh_entsize in \"%s\" "
378 "should be 4, but is %#" PRIx64,
379 filename, shdr.sh_entsize);
Petr Machata35fe5182006-07-18 12:58:12 +0200380 }
381
Petr Machatafe1c1712010-10-27 16:57:34 +0200382 data = loaddata(scn, &shdr);
383 if (data == NULL)
Petr Machata35fe5182006-07-18 12:58:12 +0200384 error(EXIT_FAILURE, 0,
385 "Couldn't get .gnu.hash data from \"%s\"",
386 filename);
387
388 lte->hash = (Elf32_Word *) data->d_buf;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100389 } else if (shdr.sh_type == SHT_PROGBITS
390 || shdr.sh_type == SHT_NOBITS) {
391 if (strcmp(name, ".plt") == 0) {
392 lte->plt_addr = shdr.sh_addr;
393 lte->plt_size = shdr.sh_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200394 if (shdr.sh_flags & SHF_EXECINSTR) {
395 lte->lte_flags |= LTE_PLT_EXECUTABLE;
396 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200397 if (lte->ehdr.e_machine == EM_PPC) {
398 plt_data = loaddata(scn, &shdr);
399 if (plt_data == NULL)
400 fprintf(stderr,
401 "Can't load .plt data\n");
402 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100403 }
404#ifdef ARCH_SUPPORTS_OPD
405 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200406 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100407 lte->opd_size = shdr.sh_size;
408 lte->opd = elf_rawdata(scn, NULL);
409 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100410#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100411 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100412 }
413
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100414 if (lte->dynsym == NULL || lte->dynstr == NULL)
415 error(EXIT_FAILURE, 0,
416 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100417
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100418 if (!relplt_addr || !lte->plt_addr) {
419 debug(1, "%s has no PLT relocations", filename);
420 lte->relplt = NULL;
421 lte->relplt_count = 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200422 } else if (relplt_size == 0) {
423 debug(1, "%s has unknown PLT size", filename);
424 lte->relplt = NULL;
425 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200427 if (lte->ehdr.e_machine == EM_PPC) {
428 GElf_Addr glink_vma
429 = get_glink_vma(lte, ppcgot, plt_data);
430
431 assert (relplt_size % 12 == 0);
432 size_t count = relplt_size / 12; // size of RELA entry
433 lte->plt_stub_vma = glink_vma
434 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800435 debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200436 }
437
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
439 Elf_Scn *scn;
440 GElf_Shdr shdr;
441
442 scn = elf_getscn(lte->elf, i);
443 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
444 error(EXIT_FAILURE, 0,
445 "Couldn't get section header from \"%s\"",
446 filename);
447 if (shdr.sh_addr == relplt_addr
448 && shdr.sh_size == relplt_size) {
449 lte->relplt = elf_getdata(scn, NULL);
450 lte->relplt_count =
451 shdr.sh_size / shdr.sh_entsize;
452 if (lte->relplt == NULL
453 || elf_getdata(scn, lte->relplt) != NULL)
454 error(EXIT_FAILURE, 0,
455 "Couldn't get .rel*.plt data from \"%s\"",
456 filename);
457 break;
458 }
459 }
460
461 if (i == lte->ehdr.e_shnum)
462 error(EXIT_FAILURE, 0,
463 "Couldn't find .rel*.plt section in \"%s\"",
464 filename);
465
466 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
467 }
Petr Machata1974dbc2011-08-19 18:58:01 +0200468 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100469}
470
Joe Damato7a2bdf82010-11-08 15:47:41 -0800471void
Juan Cespedesf1350522008-12-16 18:19:58 +0100472do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200473 debug(DEBUG_FUNCTION, "do_close_elf()");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200474 if (lte->lte_flags & LTE_HASH_MALLOCED)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100475 free((char *)lte->hash);
476 elf_end(lte->elf);
477 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200478}
479
Petr Machata534e00f2011-09-27 17:58:38 +0200480static struct library_symbol *
481create_library_symbol(const char * name, GElf_Addr addr)
482{
483 size_t namel = strlen(name) + 1;
484 struct library_symbol * sym = calloc(sizeof(*sym) + namel, 1);
485 if (sym == NULL) {
486 perror("create_library_symbol");
487 return NULL;
488 }
489 sym->name = (char *)(sym + 1);
490 memcpy(sym->name, name, namel);
491 sym->enter_addr = (void *)(uintptr_t) addr;
492 return sym;
493}
494
Joe Damato7a2bdf82010-11-08 15:47:41 -0800495void
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100496add_library_symbol(GElf_Addr addr, const char *name,
497 struct library_symbol **library_symbolspp,
Petr Machata534e00f2011-09-27 17:58:38 +0200498 enum toplt type_of_plt, int is_weak)
499{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100500 struct library_symbol *s;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200501
502 debug(DEBUG_FUNCTION, "add_library_symbol()");
503
Petr Machata534e00f2011-09-27 17:58:38 +0200504 s = create_library_symbol(name, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100505 if (s == NULL)
506 error(EXIT_FAILURE, errno, "add_library_symbol failed");
507
508 s->needs_init = 1;
509 s->is_weak = is_weak;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200510 s->plt_type = type_of_plt;
Petr Machata534e00f2011-09-27 17:58:38 +0200511
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 s->next = *library_symbolspp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 *library_symbolspp = s;
514
515 debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name);
Juan Cespedesd914a202004-11-10 00:15:33 +0100516}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200517
Petr Machata534e00f2011-09-27 17:58:38 +0200518struct library_symbol *
519clone_library_symbol(struct library_symbol * sym)
520{
521 struct library_symbol * copy
522 = create_library_symbol(sym->name,
523 (GElf_Addr)(uintptr_t)sym->enter_addr);
524 if (copy == NULL)
525 return NULL;
526
527 copy->needs_init = sym->needs_init;
528 copy->is_weak = sym->is_weak;
529 copy->plt_type = sym->plt_type;
530
531 return copy;
532}
533
534void
535destroy_library_symbol(struct library_symbol * sym)
536{
537 free(sym);
538}
539
540void
541destroy_library_symbol_chain(struct library_symbol * sym)
542{
Petr Machata9120e5f2011-09-27 20:24:17 +0200543 while (sym != NULL) {
Petr Machata534e00f2011-09-27 17:58:38 +0200544 struct library_symbol * next = sym->next;
545 destroy_library_symbol(sym);
546 sym = next;
547 }
548}
549
Olaf Hering03c087b2006-09-25 02:31:27 +0200550/* stolen from elfutils-0.123 */
Juan Cespedesf1350522008-12-16 18:19:58 +0100551static unsigned long
552private_elf_gnu_hash(const char *name) {
Olaf Hering03c087b2006-09-25 02:31:27 +0200553 unsigned long h = 5381;
554 const unsigned char *string = (const unsigned char *)name;
555 unsigned char c;
556 for (c = *string; c; c = *++string)
557 h = h * 33 + c;
558 return h & 0xffffffff;
559}
560
Juan Cespedesf1350522008-12-16 18:19:58 +0100561static int
Joe Damato3268c5a2010-11-08 15:47:38 -0800562symbol_matches(struct ltelf *lte, size_t lte_i, GElf_Sym *sym,
563 size_t symidx, const char *name)
564{
565 GElf_Sym tmp_sym;
566 GElf_Sym *tmp;
567
568 tmp = (sym) ? (sym) : (&tmp_sym);
569
570 if (gelf_getsym(lte[lte_i].dynsym, symidx, tmp) == NULL)
571 error(EXIT_FAILURE, 0, "Couldn't get symbol from .dynsym");
572 else {
573 tmp->st_value += lte[lte_i].base_addr;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800574 debug(2, "symbol found: %s, %zd, %#" PRIx64,
Joe Damato3268c5a2010-11-08 15:47:38 -0800575 name, lte_i, tmp->st_value);
576 }
577 return tmp->st_value != 0
578 && tmp->st_shndx != SHN_UNDEF
579 && strcmp(name, lte[lte_i].dynstr + tmp->st_name) == 0;
580}
581
Joe Damato7a2bdf82010-11-08 15:47:41 -0800582int
Joe Damato3268c5a2010-11-08 15:47:38 -0800583in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100584 size_t i;
585 unsigned long hash;
Petr Machata35fe5182006-07-18 12:58:12 +0200586 unsigned long gnu_hash;
Juan Cespedesd914a202004-11-10 00:15:33 +0100587
Joe Damato3268c5a2010-11-08 15:47:38 -0800588 if (!count)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100589 return 1;
Juan Cespedesd914a202004-11-10 00:15:33 +0100590
Petr Machata144a9382012-02-07 01:52:56 +0100591#ifdef ELF_HASH_TAKES_CHARP
Zachary T Welch0a43b322010-12-08 18:55:10 -0800592 hash = elf_hash(name);
593#else
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200594 hash = elf_hash((const unsigned char *)name);
Zachary T Welch0a43b322010-12-08 18:55:10 -0800595#endif
Petr Machata07bc4d12006-11-30 14:47:40 +0100596 gnu_hash = private_elf_gnu_hash(name);
Joe Damato3268c5a2010-11-08 15:47:38 -0800597
598 for (i = 0; i < count; ++i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 if (lte[i].hash == NULL)
600 continue;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200601
Petr Machata35fe5182006-07-18 12:58:12 +0200602 if (lte[i].hash_type == SHT_GNU_HASH) {
603 Elf32_Word * hashbase = lte[i].hash;
604 Elf32_Word nbuckets = *hashbase++;
605 Elf32_Word symbias = *hashbase++;
606 Elf32_Word bitmask_nwords = *hashbase++;
Petr Machata35fe5182006-07-18 12:58:12 +0200607 Elf32_Word * buckets;
608 Elf32_Word * chain_zero;
609 Elf32_Word bucket;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200610
Petr Machatab3f8fef2006-11-30 14:45:07 +0100611 // +1 for skipped `shift'
612 hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1;
Petr Machata35fe5182006-07-18 12:58:12 +0200613 buckets = hashbase;
614 hashbase += nbuckets;
615 chain_zero = hashbase - symbias;
616 bucket = buckets[gnu_hash % nbuckets];
Juan Cespedesd914a202004-11-10 00:15:33 +0100617
Petr Machata35fe5182006-07-18 12:58:12 +0200618 if (bucket != 0) {
619 const Elf32_Word *hasharr = &chain_zero[bucket];
620 do
621 if ((*hasharr & ~1u) == (gnu_hash & ~1u)) {
622 int symidx = hasharr - chain_zero;
Joe Damato3268c5a2010-11-08 15:47:38 -0800623 if (symbol_matches(lte, i,
624 sym, symidx,
625 name))
Petr Machata35fe5182006-07-18 12:58:12 +0200626 return 1;
627 }
628 while ((*hasharr++ & 1u) == 0);
629 }
Olaf Hering03c087b2006-09-25 02:31:27 +0200630 } else {
Petr Machata35fe5182006-07-18 12:58:12 +0200631 Elf32_Word nbuckets, symndx;
632 Elf32_Word *buckets, *chain;
633 nbuckets = lte[i].hash[0];
634 buckets = &lte[i].hash[2];
635 chain = &lte[i].hash[2 + nbuckets];
636
637 for (symndx = buckets[hash % nbuckets];
Joe Damato3268c5a2010-11-08 15:47:38 -0800638 symndx != STN_UNDEF; symndx = chain[symndx])
639 if (symbol_matches(lte, i, sym, symndx, name))
Petr Machata35fe5182006-07-18 12:58:12 +0200640 return 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100641 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200642 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100643 return 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100644}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200645
Juan Cespedesf1350522008-12-16 18:19:58 +0100646static GElf_Addr
647opd2addr(struct ltelf *lte, GElf_Addr addr) {
Petr Machatab3f8fef2006-11-30 14:45:07 +0100648#ifdef ARCH_SUPPORTS_OPD
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200649 unsigned long base, offset;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200650
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100651 if (!lte->opd)
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200652 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100653
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200654 base = (unsigned long)lte->opd->d_buf;
655 offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100656 if (offset > lte->opd_size)
657 error(EXIT_FAILURE, 0, "static plt not in .opd");
658
Olaf Heringa841f652006-09-15 01:57:49 +0200659 return *(GElf_Addr*)(base + offset);
Petr Machatab3f8fef2006-11-30 14:45:07 +0100660#else //!ARCH_SUPPORTS_OPD
661 return addr;
662#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100663}
664
Juan Cespedesf1350522008-12-16 18:19:58 +0100665struct library_symbol *
Petr Machatae84fa002012-02-07 13:43:03 +0100666read_elf(Process *proc, GElf_Addr *entryp)
667{
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200668 struct ltelf lte[MAX_LIBRARIES + 1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100669 size_t i;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100670 struct opt_x_t *xptr;
Petr Machata9c9c6052011-08-08 17:39:49 +0200671 struct opt_x_t *opt_x_loc = opt_x;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100672 struct library_symbol **lib_tail = NULL;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100673 int exit_out = 0;
Joe Damato3268c5a2010-11-08 15:47:38 -0800674 int count = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100675
Juan Cespedescd8976d2009-05-14 13:47:58 +0200676 debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename);
677
Petr Machatadf8f1ac2011-05-20 10:13:14 +0200678 memset(lte, 0, sizeof(lte));
Joe Damato7a2bdf82010-11-08 15:47:41 -0800679 library_symbols = NULL;
680 library_num = 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800681 proc->libdl_hooked = 0;
682
Petr Machata1974dbc2011-08-19 18:58:01 +0200683 if (do_init_elf(lte, proc->filename))
684 return NULL;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800685
686 memcpy(&main_lte, lte, sizeof(struct ltelf));
687
688 if (opt_p && opt_p->pid > 0) {
689 linkmap_init(proc, lte);
690 proc->libdl_hooked = 1;
691 }
692
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100693 proc->e_machine = lte->ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800694
695 for (i = 0; i < library_num; ++i) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200696 if (do_init_elf(&lte[i + 1], library[i]))
697 error(EXIT_FAILURE, errno, "Can't open \"%s\"",
Petr Machata993f4122011-09-15 17:49:25 +0200698 library[i]);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800699 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700700
701 if (!options.no_plt) {
Eric Vaitl1228a912006-12-28 16:16:56 +0100702#ifdef __mips__
Joe Damatofa2aefc2010-10-30 19:56:50 -0700703 // MIPS doesn't use the PLT and the GOT entries get changed
704 // on startup.
Joe Damatofa2aefc2010-10-30 19:56:50 -0700705 for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){
706 GElf_Sym sym;
707 const char *name;
708 GElf_Addr addr = arch_plt_sym_val(lte, i, 0);
709 if (gelf_getsym(lte->dynsym, i, &sym) == NULL){
710 error(EXIT_FAILURE, 0,
711 "Couldn't get relocation from \"%s\"",
712 proc->filename);
713 }
714 name=lte->dynstr+sym.st_name;
Arnaud Patard95623b22010-01-08 08:40:02 -0500715 if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){
716 debug(2,"sym %s not a function",name);
717 continue;
718 }
719 add_library_symbol(addr, name, &library_symbols, 0,
720 ELF64_ST_BIND(sym.st_info) != 0);
721 if (!lib_tail)
722 lib_tail = &(library_symbols->next);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200723 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100724#else
Joe Damatofa2aefc2010-10-30 19:56:50 -0700725 for (i = 0; i < lte->relplt_count; ++i) {
726 GElf_Rel rel;
727 GElf_Rela rela;
728 GElf_Sym sym;
729 GElf_Addr addr;
730 void *ret;
731 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100732
Joe Damatofa2aefc2010-10-30 19:56:50 -0700733 if (lte->relplt->d_type == ELF_T_REL) {
734 ret = gelf_getrel(lte->relplt, i, &rel);
735 rela.r_offset = rel.r_offset;
736 rela.r_info = rel.r_info;
737 rela.r_addend = 0;
738 } else
739 ret = gelf_getrela(lte->relplt, i, &rela);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100740
Joe Damatofa2aefc2010-10-30 19:56:50 -0700741 if (ret == NULL
742 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
743 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
744 &sym) == NULL)
745 error(EXIT_FAILURE, 0,
746 "Couldn't get relocation from \"%s\"",
747 proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100748
Joe Damato3268c5a2010-11-08 15:47:38 -0800749 name = lte->dynstr + sym.st_name;
750 count = library_num ? library_num+1 : 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200751
Joe Damato3268c5a2010-11-08 15:47:38 -0800752 if (in_load_libraries(name, lte, count, NULL)) {
753 enum toplt pltt;
754 if (sym.st_value == 0 && lte->plt_stub_vma != 0) {
755 pltt = LS_TOPLT_EXEC;
756 addr = lte->plt_stub_vma + PPC_PLT_STUB_SIZE * i;
757 }
758 else {
759 pltt = PLTS_ARE_EXECUTABLE(lte)
760 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
761 addr = arch_plt_sym_val(lte, i, &rela);
762 }
763
764 add_library_symbol(addr, name, &library_symbols, pltt,
765 ELF64_ST_BIND(sym.st_info) == STB_WEAK);
766 if (!lib_tail)
767 lib_tail = &(library_symbols->next);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700768 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100769 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100770#endif // !__mips__
Joe Damatofa2aefc2010-10-30 19:56:50 -0700771 } else {
772 lib_tail = &library_symbols;
773 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100774
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100775 for (i = 0; i < lte->symtab_count; ++i) {
776 GElf_Sym sym;
777 GElf_Addr addr;
778 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100779
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100780 if (gelf_getsym(lte->symtab, i, &sym) == NULL)
781 error(EXIT_FAILURE, 0,
782 "Couldn't get symbol from \"%s\"",
783 proc->filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100784
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100785 name = lte->strtab + sym.st_name;
786 addr = sym.st_value;
787 if (!addr)
788 continue;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100789
Petr Machata9c9c6052011-08-08 17:39:49 +0200790 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100791 if (xptr->name && strcmp(xptr->name, name) == 0) {
792 /* FIXME: Should be able to use &library_symbols as above. But
793 when you do, none of the real library symbols cause breaks. */
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200794 add_library_symbol(opd2addr(lte, addr),
Paul Gilliam76c61f12006-06-14 06:55:21 +0200795 name, lib_tail, LS_TOPLT_NONE, 0);
Paul Gilliam24e643a2006-03-13 18:43:13 +0100796 xptr->found = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100797 break;
798 }
799 }
Joe Damatoe2a8f572010-11-08 15:47:40 -0800800
Zachary T Welchba6aca22010-12-08 18:55:09 -0800801 unsigned found_count = 0;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800802
Petr Machata9c9c6052011-08-08 17:39:49 +0200803 for (xptr = opt_x_loc; xptr; xptr = xptr->next) {
Joe Damatoe2a8f572010-11-08 15:47:40 -0800804 if (xptr->found)
805 continue;
806
807 GElf_Sym sym;
808 GElf_Addr addr;
809 if (in_load_libraries(xptr->name, lte, library_num+1, &sym)) {
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800810 debug(2, "found symbol %s @ %#" PRIx64 ", adding it.",
811 xptr->name, sym.st_value);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800812 addr = sym.st_value;
813 if (ELF32_ST_TYPE (sym.st_info) == STT_FUNC) {
814 add_library_symbol(addr, xptr->name, lib_tail, LS_TOPLT_NONE, 0);
815 xptr->found = 1;
816 found_count++;
817 }
818 }
819 if (found_count == opt_x_cnt){
820 debug(2, "done, found everything: %d\n", found_count);
821 break;
822 }
823 }
824
Petr Machatae84fa002012-02-07 13:43:03 +0100825 if (lte->ehdr.e_entry != 0) {
Petr Machata990655a2012-02-07 18:25:28 +0100826 *entryp = opd2addr(lte, lte->ehdr.e_entry);
Petr Machatae84fa002012-02-07 13:43:03 +0100827 } else {
828 }
829
Petr Machata9c9c6052011-08-08 17:39:49 +0200830 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Paul Gilliam24e643a2006-03-13 18:43:13 +0100831 if ( ! xptr->found) {
832 char *badthing = "WARNING";
Paul Gilliambe320772006-04-24 22:06:23 +0200833#ifdef PLT_REINITALISATION_BP
834 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) {
835 if (lte->ehdr.e_entry) {
Paul Gilliambe320772006-04-24 22:06:23 +0200836 fprintf (stderr, "WARNING: Using e_ent"
837 "ry from elf header (%p) for "
838 "address of \"%s\"\n", (void*)
839 (long) lte->ehdr.e_entry,
840 PLTs_initialized_by_here);
841 continue;
842 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100843 badthing = "ERROR";
844 exit_out = 1;
845 }
Paul Gilliambe320772006-04-24 22:06:23 +0200846#endif
Paul Gilliam24e643a2006-03-13 18:43:13 +0100847 fprintf (stderr,
Joe Damatof0bd98b2010-11-08 15:47:42 -0800848 "%s: Couldn't find symbol \"%s\" in file \"%s\" assuming it will be loaded by libdl!"
849 "\n", badthing, xptr->name, proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100850 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100851 if (exit_out) {
852 exit (1);
853 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100854
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100855 for (i = 0; i < library_num + 1; ++i)
856 do_close_elf(&lte[i]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100857
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100858 return library_symbols;
Juan Cespedes96935a91997-08-09 23:45:39 +0200859}