blob: 21350dac01a117974287cd539f45bc6565705f3e [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"
Juan Cespedes96935a91997-08-09 23:45:39 +020016
Joe Damato7a2bdf82010-11-08 15:47:41 -080017void do_close_elf(struct ltelf *lte);
18void add_library_symbol(GElf_Addr addr, const char *name,
19 struct library_symbol **library_symbolspp,
20 enum toplt type_of_plt, int is_weak);
21int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym);
Ian Wienand4bfcedd2006-08-07 04:03:15 +020022static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr addr);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020023
Joe Damato7a2bdf82010-11-08 15:47:41 -080024struct library_symbol *library_symbols = NULL;
Joe Damatof0bd98b2010-11-08 15:47:42 -080025struct ltelf main_lte;
26
Paul Gilliambe320772006-04-24 22:06:23 +020027#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010028extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020029#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010030
Petr Machatafe1c1712010-10-27 16:57:34 +020031#ifndef DT_PPC_GOT
32# define DT_PPC_GOT (DT_LOPROC + 0)
33#endif
34
35#define PPC_PLT_STUB_SIZE 16
36
37static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
38{
39 Elf_Data *data = elf_getdata(scn, NULL);
40 if (data == NULL || elf_getdata(scn, data) != NULL
41 || data->d_off || data->d_size != shdr->sh_size)
42 return NULL;
43 return data;
44}
45
46static int inside(GElf_Addr addr, GElf_Shdr *shdr)
47{
48 return addr >= shdr->sh_addr
49 && addr < shdr->sh_addr + shdr->sh_size;
50}
51
52static int maybe_pick_section(GElf_Addr addr,
53 Elf_Scn *in_sec, GElf_Shdr *in_shdr,
54 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
55{
56 if (inside (addr, in_shdr)) {
57 *tgt_sec = in_sec;
58 *tgt_shdr = *in_shdr;
59 return 1;
60 }
61 return 0;
62}
63
64static int get_section_covering(struct ltelf *lte, GElf_Addr addr,
65 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
66{
67 int i;
68 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
69 Elf_Scn *scn;
70 GElf_Shdr shdr;
71
72 scn = elf_getscn(lte->elf, i);
73 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
74 debug(1, "Couldn't read section or header.");
75 return 0;
76 }
77
78 if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr))
79 return 1;
80 }
81
82 return 0;
83}
84
85static GElf_Addr read32be(Elf_Data *data, size_t offset)
86{
87 if (data->d_size < offset + 4) {
88 debug(1, "Not enough data to read 32bit value at offset %zd.",
89 offset);
90 return 0;
91 }
92
93 unsigned char const *buf = data->d_buf + offset;
94 return ((Elf32_Word)buf[0] << 24)
95 | ((Elf32_Word)buf[1] << 16)
96 | ((Elf32_Word)buf[2] << 8)
97 | ((Elf32_Word)buf[3]);
98}
99
100static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot,
101 Elf_Data *plt_data)
102{
103 Elf_Scn *ppcgot_sec = NULL;
104 GElf_Shdr ppcgot_shdr;
105 if (ppcgot != 0
106 && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr))
107 // xxx should be the log out
108 fprintf(stderr,
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800109 "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n",
Petr Machatafe1c1712010-10-27 16:57:34 +0200110 ppcgot);
111
112 if (ppcgot_sec != NULL) {
113 Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr);
114 if (data == NULL
115 || data->d_size < 8 )
116 debug(1, "Couldn't read GOT data.");
117 else {
118 // where PPCGOT begins in .got
119 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
120 GElf_Addr glink_vma = read32be(data, offset + 4);
121 if (glink_vma != 0) {
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800122 debug(1, "PPC GOT glink_vma address: %#" PRIx64,
Petr Machatafe1c1712010-10-27 16:57:34 +0200123 glink_vma);
124 return glink_vma;
125 }
126 }
127 }
128
129 if (plt_data != NULL) {
130 GElf_Addr glink_vma = read32be(plt_data, 0);
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800131 debug(1, ".plt glink_vma address: %#" PRIx64, glink_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200132 return glink_vma;
133 }
134
135 return 0;
136}
137
Petr Machata1974dbc2011-08-19 18:58:01 +0200138int
Juan Cespedesf1350522008-12-16 18:19:58 +0100139do_init_elf(struct ltelf *lte, const char *filename) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 int i;
141 GElf_Addr relplt_addr = 0;
142 size_t relplt_size = 0;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200143
Juan Cespedescd8976d2009-05-14 13:47:58 +0200144 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100145 debug(1, "Reading ELF from %s...", filename);
Juan Cespedes1afec691997-08-23 21:31:46 +0200146
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147 lte->fd = open(filename, O_RDONLY);
148 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200149 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200150
Juan Cespedesd914a202004-11-10 00:15:33 +0100151#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100152 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200153#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200155#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200156
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100157 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
158 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200159
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
161 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
162 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200163
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100164 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
165 error(EXIT_FAILURE, 0,
166 "\"%s\" is not an ELF executable nor shared library",
167 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200168
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100169 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
170 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100171#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
173 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100174#endif
175#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
177 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100178#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100179 )
180 error(EXIT_FAILURE, 0,
181 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100182
Petr Machatafe1c1712010-10-27 16:57:34 +0200183 Elf_Data *plt_data = NULL;
184 GElf_Addr ppcgot = 0;
185
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
187 Elf_Scn *scn;
188 GElf_Shdr shdr;
189 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100190
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100191 scn = elf_getscn(lte->elf, i);
192 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
193 error(EXIT_FAILURE, 0,
194 "Couldn't get section header from \"%s\"",
195 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100196
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
198 if (name == NULL)
199 error(EXIT_FAILURE, 0,
200 "Couldn't get section header from \"%s\"",
201 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100202
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100203 if (shdr.sh_type == SHT_SYMTAB) {
204 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100205
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 lte->symtab = elf_getdata(scn, NULL);
207 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
208 if ((lte->symtab == NULL
209 || elf_getdata(scn, lte->symtab) != NULL)
210 && opt_x != NULL)
211 error(EXIT_FAILURE, 0,
212 "Couldn't get .symtab data from \"%s\"",
213 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100214
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100215 scn = elf_getscn(lte->elf, shdr.sh_link);
216 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
217 error(EXIT_FAILURE, 0,
218 "Couldn't get section header from \"%s\"",
219 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100220
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 data = elf_getdata(scn, NULL);
222 if (data == NULL || elf_getdata(scn, data) != NULL
223 || shdr.sh_size != data->d_size || data->d_off)
224 error(EXIT_FAILURE, 0,
225 "Couldn't get .strtab data from \"%s\"",
226 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100227
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 lte->strtab = data->d_buf;
229 } else if (shdr.sh_type == SHT_DYNSYM) {
230 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100231
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 lte->dynsym = elf_getdata(scn, NULL);
233 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
234 if (lte->dynsym == NULL
235 || elf_getdata(scn, lte->dynsym) != NULL)
236 error(EXIT_FAILURE, 0,
237 "Couldn't get .dynsym data from \"%s\"",
238 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100239
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100240 scn = elf_getscn(lte->elf, shdr.sh_link);
241 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
242 error(EXIT_FAILURE, 0,
243 "Couldn't get section header from \"%s\"",
244 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100245
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100246 data = elf_getdata(scn, NULL);
247 if (data == NULL || elf_getdata(scn, data) != NULL
248 || shdr.sh_size != data->d_size || data->d_off)
249 error(EXIT_FAILURE, 0,
250 "Couldn't get .dynstr data from \"%s\"",
251 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100252
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100253 lte->dynstr = data->d_buf;
254 } else if (shdr.sh_type == SHT_DYNAMIC) {
255 Elf_Data *data;
256 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100257
Joe Damato87f4f582010-11-08 15:47:36 -0800258 lte->dyn_addr = shdr.sh_addr;
259 lte->dyn_sz = shdr.sh_size;
260
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100261 data = elf_getdata(scn, NULL);
262 if (data == NULL || elf_getdata(scn, data) != NULL)
263 error(EXIT_FAILURE, 0,
264 "Couldn't get .dynamic data from \"%s\"",
265 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100266
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100267 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
268 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100269
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100270 if (gelf_getdyn(data, j, &dyn) == NULL)
271 error(EXIT_FAILURE, 0,
272 "Couldn't get .dynamic data from \"%s\"",
273 filename);
Eric Vaitl1228a912006-12-28 16:16:56 +0100274#ifdef __mips__
275/**
276 MIPS ABI Supplement:
Ian Wienand9a2ad352006-02-20 22:44:45 +0100277
Juan Cespedesf1350522008-12-16 18:19:58 +0100278 DT_PLTGOT This member holds the address of the .got section.
Eric Vaitl1228a912006-12-28 16:16:56 +0100279
280 DT_MIPS_SYMTABNO This member holds the number of entries in the
281 .dynsym section.
282
283 DT_MIPS_LOCAL_GOTNO This member holds the number of local global
284 offset table entries.
285
286 DT_MIPS_GOTSYM This member holds the index of the first dyamic
287 symbol table entry that corresponds to an entry in the gobal offset
288 table.
289
290 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200291 if(dyn.d_tag==DT_PLTGOT){
Juan Cespedesf1350522008-12-16 18:19:58 +0100292 lte->pltgot_addr=dyn.d_un.d_ptr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200293 }
294 if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){
295 lte->mips_local_gotno=dyn.d_un.d_val;
296 }
297 if(dyn.d_tag==DT_MIPS_GOTSYM){
298 lte->mips_gotsym=dyn.d_un.d_val;
299 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100300#endif // __mips__
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100301 if (dyn.d_tag == DT_JMPREL)
302 relplt_addr = dyn.d_un.d_ptr;
303 else if (dyn.d_tag == DT_PLTRELSZ)
304 relplt_size = dyn.d_un.d_val;
Petr Machatafe1c1712010-10-27 16:57:34 +0200305 else if (dyn.d_tag == DT_PPC_GOT) {
306 ppcgot = dyn.d_un.d_val;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800307 debug(1, "ppcgot %#" PRIx64, ppcgot);
Petr Machatafe1c1712010-10-27 16:57:34 +0200308 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 }
310 } else if (shdr.sh_type == SHT_HASH) {
311 Elf_Data *data;
312 size_t j;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100313
Petr Machata35fe5182006-07-18 12:58:12 +0200314 lte->hash_type = SHT_HASH;
315
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 data = elf_getdata(scn, NULL);
317 if (data == NULL || elf_getdata(scn, data) != NULL
318 || data->d_off || data->d_size != shdr.sh_size)
319 error(EXIT_FAILURE, 0,
320 "Couldn't get .hash data from \"%s\"",
321 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100322
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100323 if (shdr.sh_entsize == 4) {
324 /* Standard conforming ELF. */
325 if (data->d_type != ELF_T_WORD)
326 error(EXIT_FAILURE, 0,
327 "Couldn't get .hash data from \"%s\"",
328 filename);
329 lte->hash = (Elf32_Word *) data->d_buf;
330 } else if (shdr.sh_entsize == 8) {
331 /* Alpha or s390x. */
332 Elf32_Word *dst, *src;
333 size_t hash_count = data->d_size / 8;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100334
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 lte->hash = (Elf32_Word *)
336 malloc(hash_count * sizeof(Elf32_Word));
337 if (lte->hash == NULL)
338 error(EXIT_FAILURE, 0,
339 "Couldn't convert .hash section from \"%s\"",
340 filename);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200341 lte->lte_flags |= LTE_HASH_MALLOCED;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 dst = lte->hash;
343 src = (Elf32_Word *) data->d_buf;
344 if ((data->d_type == ELF_T_WORD
345 && __BYTE_ORDER == __BIG_ENDIAN)
346 || (data->d_type == ELF_T_XWORD
347 && lte->ehdr.e_ident[EI_DATA] ==
348 ELFDATA2MSB))
349 ++src;
350 for (j = 0; j < hash_count; ++j, src += 2)
351 *dst++ = *src;
352 } else
353 error(EXIT_FAILURE, 0,
354 "Unknown .hash sh_entsize in \"%s\"",
355 filename);
Petr Machata35fe5182006-07-18 12:58:12 +0200356 } else if (shdr.sh_type == SHT_GNU_HASH
357 && lte->hash == NULL) {
358 Elf_Data *data;
Petr Machata35fe5182006-07-18 12:58:12 +0200359
360 lte->hash_type = SHT_GNU_HASH;
361
362 if (shdr.sh_entsize != 0
363 && shdr.sh_entsize != 4) {
364 error(EXIT_FAILURE, 0,
Zachary T Welch3ba522f2010-12-14 15:12:47 -0800365 ".gnu.hash sh_entsize in \"%s\" "
366 "should be 4, but is %#" PRIx64,
367 filename, shdr.sh_entsize);
Petr Machata35fe5182006-07-18 12:58:12 +0200368 }
369
Petr Machatafe1c1712010-10-27 16:57:34 +0200370 data = loaddata(scn, &shdr);
371 if (data == NULL)
Petr Machata35fe5182006-07-18 12:58:12 +0200372 error(EXIT_FAILURE, 0,
373 "Couldn't get .gnu.hash data from \"%s\"",
374 filename);
375
376 lte->hash = (Elf32_Word *) data->d_buf;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100377 } else if (shdr.sh_type == SHT_PROGBITS
378 || shdr.sh_type == SHT_NOBITS) {
379 if (strcmp(name, ".plt") == 0) {
380 lte->plt_addr = shdr.sh_addr;
381 lte->plt_size = shdr.sh_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200382 if (shdr.sh_flags & SHF_EXECINSTR) {
383 lte->lte_flags |= LTE_PLT_EXECUTABLE;
384 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200385 if (lte->ehdr.e_machine == EM_PPC) {
386 plt_data = loaddata(scn, &shdr);
387 if (plt_data == NULL)
388 fprintf(stderr,
389 "Can't load .plt data\n");
390 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100391 }
392#ifdef ARCH_SUPPORTS_OPD
393 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200394 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395 lte->opd_size = shdr.sh_size;
396 lte->opd = elf_rawdata(scn, NULL);
397 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100398#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100399 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100400 }
401
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100402 if (lte->dynsym == NULL || lte->dynstr == NULL)
403 error(EXIT_FAILURE, 0,
404 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100405
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 if (!relplt_addr || !lte->plt_addr) {
407 debug(1, "%s has no PLT relocations", filename);
408 lte->relplt = NULL;
409 lte->relplt_count = 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200410 } else if (relplt_size == 0) {
411 debug(1, "%s has unknown PLT size", filename);
412 lte->relplt = NULL;
413 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100414 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200415 if (lte->ehdr.e_machine == EM_PPC) {
416 GElf_Addr glink_vma
417 = get_glink_vma(lte, ppcgot, plt_data);
418
419 assert (relplt_size % 12 == 0);
420 size_t count = relplt_size / 12; // size of RELA entry
421 lte->plt_stub_vma = glink_vma
422 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800423 debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200424 }
425
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
427 Elf_Scn *scn;
428 GElf_Shdr shdr;
429
430 scn = elf_getscn(lte->elf, i);
431 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
432 error(EXIT_FAILURE, 0,
433 "Couldn't get section header from \"%s\"",
434 filename);
435 if (shdr.sh_addr == relplt_addr
436 && shdr.sh_size == relplt_size) {
437 lte->relplt = elf_getdata(scn, NULL);
438 lte->relplt_count =
439 shdr.sh_size / shdr.sh_entsize;
440 if (lte->relplt == NULL
441 || elf_getdata(scn, lte->relplt) != NULL)
442 error(EXIT_FAILURE, 0,
443 "Couldn't get .rel*.plt data from \"%s\"",
444 filename);
445 break;
446 }
447 }
448
449 if (i == lte->ehdr.e_shnum)
450 error(EXIT_FAILURE, 0,
451 "Couldn't find .rel*.plt section in \"%s\"",
452 filename);
453
454 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
455 }
Petr Machata1974dbc2011-08-19 18:58:01 +0200456 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457}
458
Joe Damato7a2bdf82010-11-08 15:47:41 -0800459void
Juan Cespedesf1350522008-12-16 18:19:58 +0100460do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200461 debug(DEBUG_FUNCTION, "do_close_elf()");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200462 if (lte->lte_flags & LTE_HASH_MALLOCED)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100463 free((char *)lte->hash);
464 elf_end(lte->elf);
465 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200466}
467
Joe Damato7a2bdf82010-11-08 15:47:41 -0800468void
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100469add_library_symbol(GElf_Addr addr, const char *name,
470 struct library_symbol **library_symbolspp,
Juan Cespedesf1350522008-12-16 18:19:58 +0100471 enum toplt type_of_plt, int is_weak) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100472 struct library_symbol *s;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200473
474 debug(DEBUG_FUNCTION, "add_library_symbol()");
475
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100476 s = malloc(sizeof(struct library_symbol) + strlen(name) + 1);
477 if (s == NULL)
478 error(EXIT_FAILURE, errno, "add_library_symbol failed");
479
480 s->needs_init = 1;
481 s->is_weak = is_weak;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200482 s->plt_type = type_of_plt;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483 s->next = *library_symbolspp;
484 s->enter_addr = (void *)(uintptr_t) addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100485 s->name = (char *)(s + 1);
486 strcpy(s->name, name);
487 *library_symbolspp = s;
488
489 debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name);
Juan Cespedesd914a202004-11-10 00:15:33 +0100490}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200491
Olaf Hering03c087b2006-09-25 02:31:27 +0200492/* stolen from elfutils-0.123 */
Juan Cespedesf1350522008-12-16 18:19:58 +0100493static unsigned long
494private_elf_gnu_hash(const char *name) {
Olaf Hering03c087b2006-09-25 02:31:27 +0200495 unsigned long h = 5381;
496 const unsigned char *string = (const unsigned char *)name;
497 unsigned char c;
498 for (c = *string; c; c = *++string)
499 h = h * 33 + c;
500 return h & 0xffffffff;
501}
502
Juan Cespedesf1350522008-12-16 18:19:58 +0100503static int
Joe Damato3268c5a2010-11-08 15:47:38 -0800504symbol_matches(struct ltelf *lte, size_t lte_i, GElf_Sym *sym,
505 size_t symidx, const char *name)
506{
507 GElf_Sym tmp_sym;
508 GElf_Sym *tmp;
509
510 tmp = (sym) ? (sym) : (&tmp_sym);
511
512 if (gelf_getsym(lte[lte_i].dynsym, symidx, tmp) == NULL)
513 error(EXIT_FAILURE, 0, "Couldn't get symbol from .dynsym");
514 else {
515 tmp->st_value += lte[lte_i].base_addr;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800516 debug(2, "symbol found: %s, %zd, %#" PRIx64,
Joe Damato3268c5a2010-11-08 15:47:38 -0800517 name, lte_i, tmp->st_value);
518 }
519 return tmp->st_value != 0
520 && tmp->st_shndx != SHN_UNDEF
521 && strcmp(name, lte[lte_i].dynstr + tmp->st_name) == 0;
522}
523
Joe Damato7a2bdf82010-11-08 15:47:41 -0800524int
Joe Damato3268c5a2010-11-08 15:47:38 -0800525in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100526 size_t i;
527 unsigned long hash;
Petr Machata35fe5182006-07-18 12:58:12 +0200528 unsigned long gnu_hash;
Juan Cespedesd914a202004-11-10 00:15:33 +0100529
Joe Damato3268c5a2010-11-08 15:47:38 -0800530 if (!count)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100531 return 1;
Juan Cespedesd914a202004-11-10 00:15:33 +0100532
Zachary T Welch0a43b322010-12-08 18:55:10 -0800533#ifdef ELF_HASH_TAKES_SIGNED_CHAR
534 hash = elf_hash(name);
535#else
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200536 hash = elf_hash((const unsigned char *)name);
Zachary T Welch0a43b322010-12-08 18:55:10 -0800537#endif
Petr Machata07bc4d12006-11-30 14:47:40 +0100538 gnu_hash = private_elf_gnu_hash(name);
Joe Damato3268c5a2010-11-08 15:47:38 -0800539
540 for (i = 0; i < count; ++i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100541 if (lte[i].hash == NULL)
542 continue;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200543
Petr Machata35fe5182006-07-18 12:58:12 +0200544 if (lte[i].hash_type == SHT_GNU_HASH) {
545 Elf32_Word * hashbase = lte[i].hash;
546 Elf32_Word nbuckets = *hashbase++;
547 Elf32_Word symbias = *hashbase++;
548 Elf32_Word bitmask_nwords = *hashbase++;
Petr Machata35fe5182006-07-18 12:58:12 +0200549 Elf32_Word * buckets;
550 Elf32_Word * chain_zero;
551 Elf32_Word bucket;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200552
Petr Machatab3f8fef2006-11-30 14:45:07 +0100553 // +1 for skipped `shift'
554 hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1;
Petr Machata35fe5182006-07-18 12:58:12 +0200555 buckets = hashbase;
556 hashbase += nbuckets;
557 chain_zero = hashbase - symbias;
558 bucket = buckets[gnu_hash % nbuckets];
Juan Cespedesd914a202004-11-10 00:15:33 +0100559
Petr Machata35fe5182006-07-18 12:58:12 +0200560 if (bucket != 0) {
561 const Elf32_Word *hasharr = &chain_zero[bucket];
562 do
563 if ((*hasharr & ~1u) == (gnu_hash & ~1u)) {
564 int symidx = hasharr - chain_zero;
Joe Damato3268c5a2010-11-08 15:47:38 -0800565 if (symbol_matches(lte, i,
566 sym, symidx,
567 name))
Petr Machata35fe5182006-07-18 12:58:12 +0200568 return 1;
569 }
570 while ((*hasharr++ & 1u) == 0);
571 }
Olaf Hering03c087b2006-09-25 02:31:27 +0200572 } else {
Petr Machata35fe5182006-07-18 12:58:12 +0200573 Elf32_Word nbuckets, symndx;
574 Elf32_Word *buckets, *chain;
575 nbuckets = lte[i].hash[0];
576 buckets = &lte[i].hash[2];
577 chain = &lte[i].hash[2 + nbuckets];
578
579 for (symndx = buckets[hash % nbuckets];
Joe Damato3268c5a2010-11-08 15:47:38 -0800580 symndx != STN_UNDEF; symndx = chain[symndx])
581 if (symbol_matches(lte, i, sym, symndx, name))
Petr Machata35fe5182006-07-18 12:58:12 +0200582 return 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100583 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200584 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100585 return 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100586}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200587
Juan Cespedesf1350522008-12-16 18:19:58 +0100588static GElf_Addr
589opd2addr(struct ltelf *lte, GElf_Addr addr) {
Petr Machatab3f8fef2006-11-30 14:45:07 +0100590#ifdef ARCH_SUPPORTS_OPD
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200591 unsigned long base, offset;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200592
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100593 if (!lte->opd)
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200594 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100595
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200596 base = (unsigned long)lte->opd->d_buf;
597 offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100598 if (offset > lte->opd_size)
599 error(EXIT_FAILURE, 0, "static plt not in .opd");
600
Olaf Heringa841f652006-09-15 01:57:49 +0200601 return *(GElf_Addr*)(base + offset);
Petr Machatab3f8fef2006-11-30 14:45:07 +0100602#else //!ARCH_SUPPORTS_OPD
603 return addr;
604#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100605}
606
Juan Cespedesf1350522008-12-16 18:19:58 +0100607struct library_symbol *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200608read_elf(Process *proc) {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200609 struct ltelf lte[MAX_LIBRARIES + 1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610 size_t i;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100611 struct opt_x_t *xptr;
Petr Machata9c9c6052011-08-08 17:39:49 +0200612 struct opt_x_t *opt_x_loc = opt_x;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100613 struct library_symbol **lib_tail = NULL;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100614 int exit_out = 0;
Joe Damato3268c5a2010-11-08 15:47:38 -0800615 int count = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100616
Juan Cespedescd8976d2009-05-14 13:47:58 +0200617 debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename);
618
Petr Machatadf8f1ac2011-05-20 10:13:14 +0200619 memset(lte, 0, sizeof(lte));
Joe Damato7a2bdf82010-11-08 15:47:41 -0800620 library_symbols = NULL;
621 library_num = 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800622 proc->libdl_hooked = 0;
623
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100624 elf_version(EV_CURRENT);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100625
Petr Machata1974dbc2011-08-19 18:58:01 +0200626 if (do_init_elf(lte, proc->filename))
627 return NULL;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800628
629 memcpy(&main_lte, lte, sizeof(struct ltelf));
630
631 if (opt_p && opt_p->pid > 0) {
632 linkmap_init(proc, lte);
633 proc->libdl_hooked = 1;
634 }
635
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100636 proc->e_machine = lte->ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800637
638 for (i = 0; i < library_num; ++i) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200639 if (do_init_elf(&lte[i + 1], library[i]))
640 error(EXIT_FAILURE, errno, "Can't open \"%s\"",
641 proc->filename);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800642 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700643
644 if (!options.no_plt) {
Eric Vaitl1228a912006-12-28 16:16:56 +0100645#ifdef __mips__
Joe Damatofa2aefc2010-10-30 19:56:50 -0700646 // MIPS doesn't use the PLT and the GOT entries get changed
647 // on startup.
648 proc->need_to_reinitialize_breakpoints = 1;
649 for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){
650 GElf_Sym sym;
651 const char *name;
652 GElf_Addr addr = arch_plt_sym_val(lte, i, 0);
653 if (gelf_getsym(lte->dynsym, i, &sym) == NULL){
654 error(EXIT_FAILURE, 0,
655 "Couldn't get relocation from \"%s\"",
656 proc->filename);
657 }
658 name=lte->dynstr+sym.st_name;
Arnaud Patard95623b22010-01-08 08:40:02 -0500659 if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){
660 debug(2,"sym %s not a function",name);
661 continue;
662 }
663 add_library_symbol(addr, name, &library_symbols, 0,
664 ELF64_ST_BIND(sym.st_info) != 0);
665 if (!lib_tail)
666 lib_tail = &(library_symbols->next);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200667 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100668#else
Joe Damatofa2aefc2010-10-30 19:56:50 -0700669 for (i = 0; i < lte->relplt_count; ++i) {
670 GElf_Rel rel;
671 GElf_Rela rela;
672 GElf_Sym sym;
673 GElf_Addr addr;
674 void *ret;
675 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100676
Joe Damatofa2aefc2010-10-30 19:56:50 -0700677 if (lte->relplt->d_type == ELF_T_REL) {
678 ret = gelf_getrel(lte->relplt, i, &rel);
679 rela.r_offset = rel.r_offset;
680 rela.r_info = rel.r_info;
681 rela.r_addend = 0;
682 } else
683 ret = gelf_getrela(lte->relplt, i, &rela);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100684
Joe Damatofa2aefc2010-10-30 19:56:50 -0700685 if (ret == NULL
686 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
687 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
688 &sym) == NULL)
689 error(EXIT_FAILURE, 0,
690 "Couldn't get relocation from \"%s\"",
691 proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100692
Paul Gilliambe320772006-04-24 22:06:23 +0200693#ifdef PLT_REINITALISATION_BP
Joe Damatofa2aefc2010-10-30 19:56:50 -0700694 if (!sym.st_value && PLTs_initialized_by_here)
695 proc->need_to_reinitialize_breakpoints = 1;
Paul Gilliambe320772006-04-24 22:06:23 +0200696#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100697
Joe Damato3268c5a2010-11-08 15:47:38 -0800698 name = lte->dynstr + sym.st_name;
699 count = library_num ? library_num+1 : 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200700
Joe Damato3268c5a2010-11-08 15:47:38 -0800701 if (in_load_libraries(name, lte, count, NULL)) {
702 enum toplt pltt;
703 if (sym.st_value == 0 && lte->plt_stub_vma != 0) {
704 pltt = LS_TOPLT_EXEC;
705 addr = lte->plt_stub_vma + PPC_PLT_STUB_SIZE * i;
706 }
707 else {
708 pltt = PLTS_ARE_EXECUTABLE(lte)
709 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
710 addr = arch_plt_sym_val(lte, i, &rela);
711 }
712
713 add_library_symbol(addr, name, &library_symbols, pltt,
714 ELF64_ST_BIND(sym.st_info) == STB_WEAK);
715 if (!lib_tail)
716 lib_tail = &(library_symbols->next);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700717 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100718 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100719#endif // !__mips__
Paul Gilliambe320772006-04-24 22:06:23 +0200720#ifdef PLT_REINITALISATION_BP
Joe Damatofa2aefc2010-10-30 19:56:50 -0700721 struct opt_x_t *main_cheat;
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200722
Joe Damatofa2aefc2010-10-30 19:56:50 -0700723 if (proc->need_to_reinitialize_breakpoints) {
724 /* Add "PLTs_initialized_by_here" to opt_x list, if not
725 already there. */
726 main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t));
727 if (main_cheat == NULL)
728 error(EXIT_FAILURE, 0, "Couldn't allocate memory");
Petr Machata9c9c6052011-08-08 17:39:49 +0200729 main_cheat->next = opt_x_loc;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700730 main_cheat->found = 0;
731 main_cheat->name = PLTs_initialized_by_here;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100732
Petr Machata9c9c6052011-08-08 17:39:49 +0200733 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Joe Damatofa2aefc2010-10-30 19:56:50 -0700734 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0
735 && main_cheat) {
736 free(main_cheat);
737 main_cheat = NULL;
738 break;
739 }
740 if (main_cheat)
Petr Machata9c9c6052011-08-08 17:39:49 +0200741 opt_x_loc = main_cheat;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700742 }
Paul Gilliambe320772006-04-24 22:06:23 +0200743#endif
Joe Damatofa2aefc2010-10-30 19:56:50 -0700744 } else {
745 lib_tail = &library_symbols;
746 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100747
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100748 for (i = 0; i < lte->symtab_count; ++i) {
749 GElf_Sym sym;
750 GElf_Addr addr;
751 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100752
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100753 if (gelf_getsym(lte->symtab, i, &sym) == NULL)
754 error(EXIT_FAILURE, 0,
755 "Couldn't get symbol from \"%s\"",
756 proc->filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100757
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100758 name = lte->strtab + sym.st_name;
759 addr = sym.st_value;
760 if (!addr)
761 continue;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100762
Petr Machata9c9c6052011-08-08 17:39:49 +0200763 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100764 if (xptr->name && strcmp(xptr->name, name) == 0) {
765 /* FIXME: Should be able to use &library_symbols as above. But
766 when you do, none of the real library symbols cause breaks. */
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200767 add_library_symbol(opd2addr(lte, addr),
Paul Gilliam76c61f12006-06-14 06:55:21 +0200768 name, lib_tail, LS_TOPLT_NONE, 0);
Paul Gilliam24e643a2006-03-13 18:43:13 +0100769 xptr->found = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100770 break;
771 }
772 }
Joe Damatoe2a8f572010-11-08 15:47:40 -0800773
Zachary T Welchba6aca22010-12-08 18:55:09 -0800774 unsigned found_count = 0;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800775
Petr Machata9c9c6052011-08-08 17:39:49 +0200776 for (xptr = opt_x_loc; xptr; xptr = xptr->next) {
Joe Damatoe2a8f572010-11-08 15:47:40 -0800777 if (xptr->found)
778 continue;
779
780 GElf_Sym sym;
781 GElf_Addr addr;
782 if (in_load_libraries(xptr->name, lte, library_num+1, &sym)) {
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800783 debug(2, "found symbol %s @ %#" PRIx64 ", adding it.",
784 xptr->name, sym.st_value);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800785 addr = sym.st_value;
786 if (ELF32_ST_TYPE (sym.st_info) == STT_FUNC) {
787 add_library_symbol(addr, xptr->name, lib_tail, LS_TOPLT_NONE, 0);
788 xptr->found = 1;
789 found_count++;
790 }
791 }
792 if (found_count == opt_x_cnt){
793 debug(2, "done, found everything: %d\n", found_count);
794 break;
795 }
796 }
797
Petr Machata9c9c6052011-08-08 17:39:49 +0200798 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Paul Gilliam24e643a2006-03-13 18:43:13 +0100799 if ( ! xptr->found) {
800 char *badthing = "WARNING";
Paul Gilliambe320772006-04-24 22:06:23 +0200801#ifdef PLT_REINITALISATION_BP
802 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) {
803 if (lte->ehdr.e_entry) {
804 add_library_symbol (
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200805 opd2addr (lte, lte->ehdr.e_entry),
Paul Gilliambe320772006-04-24 22:06:23 +0200806 PLTs_initialized_by_here,
807 lib_tail, 1, 0);
808 fprintf (stderr, "WARNING: Using e_ent"
809 "ry from elf header (%p) for "
810 "address of \"%s\"\n", (void*)
811 (long) lte->ehdr.e_entry,
812 PLTs_initialized_by_here);
813 continue;
814 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100815 badthing = "ERROR";
816 exit_out = 1;
817 }
Paul Gilliambe320772006-04-24 22:06:23 +0200818#endif
Paul Gilliam24e643a2006-03-13 18:43:13 +0100819 fprintf (stderr,
Joe Damatof0bd98b2010-11-08 15:47:42 -0800820 "%s: Couldn't find symbol \"%s\" in file \"%s\" assuming it will be loaded by libdl!"
821 "\n", badthing, xptr->name, proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100822 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100823 if (exit_out) {
824 exit (1);
825 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100826
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100827 for (i = 0; i < library_num + 1; ++i)
828 do_close_elf(&lte[i]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100829
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100830 return library_symbols;
Juan Cespedes96935a91997-08-09 23:45:39 +0200831}