blob: 68a67f47cef655b3b9e9ca6918dcdbe82a7cd44c [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
Petr Machata02bd9ec2011-09-21 17:38:59 +0200139open_elf(struct ltelf *lte, const char *filename)
140{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141 lte->fd = open(filename, O_RDONLY);
142 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200143 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200144
Petr Machata02bd9ec2011-09-21 17:38:59 +0200145 elf_version(EV_CURRENT);
146
Juan Cespedesd914a202004-11-10 00:15:33 +0100147#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100148 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200149#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100150 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200151#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200152
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100153 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
154 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200155
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100156 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
157 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
158 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200159
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
161 error(EXIT_FAILURE, 0,
162 "\"%s\" is not an ELF executable nor shared library",
163 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200164
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100165 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
166 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100167#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100168 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
169 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100170#endif
171#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
173 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100174#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100175 )
176 error(EXIT_FAILURE, 0,
177 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100178
Petr Machata02bd9ec2011-09-21 17:38:59 +0200179 return 0;
180}
181
182int
183do_init_elf(struct ltelf *lte, const char *filename) {
184 int i;
185 GElf_Addr relplt_addr = 0;
186 size_t relplt_size = 0;
187
188 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
189 debug(1, "Reading ELF from %s...", filename);
190
191 if (open_elf(lte, filename) < 0)
192 return -1;
193
Petr Machatafe1c1712010-10-27 16:57:34 +0200194 Elf_Data *plt_data = NULL;
195 GElf_Addr ppcgot = 0;
196
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
198 Elf_Scn *scn;
199 GElf_Shdr shdr;
200 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100201
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100202 scn = elf_getscn(lte->elf, i);
203 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
204 error(EXIT_FAILURE, 0,
205 "Couldn't get section header from \"%s\"",
206 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100207
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
209 if (name == NULL)
210 error(EXIT_FAILURE, 0,
211 "Couldn't get section header from \"%s\"",
212 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100213
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214 if (shdr.sh_type == SHT_SYMTAB) {
215 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100216
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 lte->symtab = elf_getdata(scn, NULL);
218 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
219 if ((lte->symtab == NULL
220 || elf_getdata(scn, lte->symtab) != NULL)
221 && opt_x != NULL)
222 error(EXIT_FAILURE, 0,
223 "Couldn't get .symtab data from \"%s\"",
224 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100225
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 scn = elf_getscn(lte->elf, shdr.sh_link);
227 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
228 error(EXIT_FAILURE, 0,
229 "Couldn't get section header from \"%s\"",
230 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100231
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 data = elf_getdata(scn, NULL);
233 if (data == NULL || elf_getdata(scn, data) != NULL
234 || shdr.sh_size != data->d_size || data->d_off)
235 error(EXIT_FAILURE, 0,
236 "Couldn't get .strtab data from \"%s\"",
237 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100238
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 lte->strtab = data->d_buf;
240 } else if (shdr.sh_type == SHT_DYNSYM) {
241 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100242
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 lte->dynsym = elf_getdata(scn, NULL);
244 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
245 if (lte->dynsym == NULL
246 || elf_getdata(scn, lte->dynsym) != NULL)
247 error(EXIT_FAILURE, 0,
248 "Couldn't get .dynsym data from \"%s\"",
249 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100250
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 scn = elf_getscn(lte->elf, shdr.sh_link);
252 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
253 error(EXIT_FAILURE, 0,
254 "Couldn't get section header from \"%s\"",
255 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100256
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 data = elf_getdata(scn, NULL);
258 if (data == NULL || elf_getdata(scn, data) != NULL
259 || shdr.sh_size != data->d_size || data->d_off)
260 error(EXIT_FAILURE, 0,
261 "Couldn't get .dynstr data from \"%s\"",
262 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100263
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100264 lte->dynstr = data->d_buf;
265 } else if (shdr.sh_type == SHT_DYNAMIC) {
266 Elf_Data *data;
267 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100268
Joe Damato87f4f582010-11-08 15:47:36 -0800269 lte->dyn_addr = shdr.sh_addr;
270 lte->dyn_sz = shdr.sh_size;
271
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100272 data = elf_getdata(scn, NULL);
273 if (data == NULL || elf_getdata(scn, data) != NULL)
274 error(EXIT_FAILURE, 0,
275 "Couldn't get .dynamic data from \"%s\"",
276 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100277
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
279 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100280
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100281 if (gelf_getdyn(data, j, &dyn) == NULL)
282 error(EXIT_FAILURE, 0,
283 "Couldn't get .dynamic data from \"%s\"",
284 filename);
Eric Vaitl1228a912006-12-28 16:16:56 +0100285#ifdef __mips__
286/**
287 MIPS ABI Supplement:
Ian Wienand9a2ad352006-02-20 22:44:45 +0100288
Juan Cespedesf1350522008-12-16 18:19:58 +0100289 DT_PLTGOT This member holds the address of the .got section.
Eric Vaitl1228a912006-12-28 16:16:56 +0100290
291 DT_MIPS_SYMTABNO This member holds the number of entries in the
292 .dynsym section.
293
294 DT_MIPS_LOCAL_GOTNO This member holds the number of local global
295 offset table entries.
296
297 DT_MIPS_GOTSYM This member holds the index of the first dyamic
298 symbol table entry that corresponds to an entry in the gobal offset
299 table.
300
301 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200302 if(dyn.d_tag==DT_PLTGOT){
Juan Cespedesf1350522008-12-16 18:19:58 +0100303 lte->pltgot_addr=dyn.d_un.d_ptr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200304 }
305 if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){
306 lte->mips_local_gotno=dyn.d_un.d_val;
307 }
308 if(dyn.d_tag==DT_MIPS_GOTSYM){
309 lte->mips_gotsym=dyn.d_un.d_val;
310 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100311#endif // __mips__
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 if (dyn.d_tag == DT_JMPREL)
313 relplt_addr = dyn.d_un.d_ptr;
314 else if (dyn.d_tag == DT_PLTRELSZ)
315 relplt_size = dyn.d_un.d_val;
Petr Machatafe1c1712010-10-27 16:57:34 +0200316 else if (dyn.d_tag == DT_PPC_GOT) {
317 ppcgot = dyn.d_un.d_val;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800318 debug(1, "ppcgot %#" PRIx64, ppcgot);
Petr Machatafe1c1712010-10-27 16:57:34 +0200319 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100320 }
321 } else if (shdr.sh_type == SHT_HASH) {
322 Elf_Data *data;
323 size_t j;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100324
Petr Machata35fe5182006-07-18 12:58:12 +0200325 lte->hash_type = SHT_HASH;
326
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100327 data = elf_getdata(scn, NULL);
328 if (data == NULL || elf_getdata(scn, data) != NULL
329 || data->d_off || data->d_size != shdr.sh_size)
330 error(EXIT_FAILURE, 0,
331 "Couldn't get .hash data from \"%s\"",
332 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100333
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100334 if (shdr.sh_entsize == 4) {
335 /* Standard conforming ELF. */
336 if (data->d_type != ELF_T_WORD)
337 error(EXIT_FAILURE, 0,
338 "Couldn't get .hash data from \"%s\"",
339 filename);
340 lte->hash = (Elf32_Word *) data->d_buf;
341 } else if (shdr.sh_entsize == 8) {
342 /* Alpha or s390x. */
343 Elf32_Word *dst, *src;
344 size_t hash_count = data->d_size / 8;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100345
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 lte->hash = (Elf32_Word *)
347 malloc(hash_count * sizeof(Elf32_Word));
348 if (lte->hash == NULL)
349 error(EXIT_FAILURE, 0,
350 "Couldn't convert .hash section from \"%s\"",
351 filename);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200352 lte->lte_flags |= LTE_HASH_MALLOCED;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100353 dst = lte->hash;
354 src = (Elf32_Word *) data->d_buf;
355 if ((data->d_type == ELF_T_WORD
356 && __BYTE_ORDER == __BIG_ENDIAN)
357 || (data->d_type == ELF_T_XWORD
358 && lte->ehdr.e_ident[EI_DATA] ==
359 ELFDATA2MSB))
360 ++src;
361 for (j = 0; j < hash_count; ++j, src += 2)
362 *dst++ = *src;
363 } else
364 error(EXIT_FAILURE, 0,
365 "Unknown .hash sh_entsize in \"%s\"",
366 filename);
Petr Machata35fe5182006-07-18 12:58:12 +0200367 } else if (shdr.sh_type == SHT_GNU_HASH
368 && lte->hash == NULL) {
369 Elf_Data *data;
Petr Machata35fe5182006-07-18 12:58:12 +0200370
371 lte->hash_type = SHT_GNU_HASH;
372
373 if (shdr.sh_entsize != 0
374 && shdr.sh_entsize != 4) {
375 error(EXIT_FAILURE, 0,
Zachary T Welch3ba522f2010-12-14 15:12:47 -0800376 ".gnu.hash sh_entsize in \"%s\" "
377 "should be 4, but is %#" PRIx64,
378 filename, shdr.sh_entsize);
Petr Machata35fe5182006-07-18 12:58:12 +0200379 }
380
Petr Machatafe1c1712010-10-27 16:57:34 +0200381 data = loaddata(scn, &shdr);
382 if (data == NULL)
Petr Machata35fe5182006-07-18 12:58:12 +0200383 error(EXIT_FAILURE, 0,
384 "Couldn't get .gnu.hash data from \"%s\"",
385 filename);
386
387 lte->hash = (Elf32_Word *) data->d_buf;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 } else if (shdr.sh_type == SHT_PROGBITS
389 || shdr.sh_type == SHT_NOBITS) {
390 if (strcmp(name, ".plt") == 0) {
391 lte->plt_addr = shdr.sh_addr;
392 lte->plt_size = shdr.sh_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200393 if (shdr.sh_flags & SHF_EXECINSTR) {
394 lte->lte_flags |= LTE_PLT_EXECUTABLE;
395 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200396 if (lte->ehdr.e_machine == EM_PPC) {
397 plt_data = loaddata(scn, &shdr);
398 if (plt_data == NULL)
399 fprintf(stderr,
400 "Can't load .plt data\n");
401 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100402 }
403#ifdef ARCH_SUPPORTS_OPD
404 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200405 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 lte->opd_size = shdr.sh_size;
407 lte->opd = elf_rawdata(scn, NULL);
408 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100409#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100410 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100411 }
412
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100413 if (lte->dynsym == NULL || lte->dynstr == NULL)
414 error(EXIT_FAILURE, 0,
415 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100416
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 if (!relplt_addr || !lte->plt_addr) {
418 debug(1, "%s has no PLT relocations", filename);
419 lte->relplt = NULL;
420 lte->relplt_count = 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200421 } else if (relplt_size == 0) {
422 debug(1, "%s has unknown PLT size", filename);
423 lte->relplt = NULL;
424 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100425 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200426 if (lte->ehdr.e_machine == EM_PPC) {
427 GElf_Addr glink_vma
428 = get_glink_vma(lte, ppcgot, plt_data);
429
430 assert (relplt_size % 12 == 0);
431 size_t count = relplt_size / 12; // size of RELA entry
432 lte->plt_stub_vma = glink_vma
433 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800434 debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200435 }
436
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
438 Elf_Scn *scn;
439 GElf_Shdr shdr;
440
441 scn = elf_getscn(lte->elf, i);
442 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
443 error(EXIT_FAILURE, 0,
444 "Couldn't get section header from \"%s\"",
445 filename);
446 if (shdr.sh_addr == relplt_addr
447 && shdr.sh_size == relplt_size) {
448 lte->relplt = elf_getdata(scn, NULL);
449 lte->relplt_count =
450 shdr.sh_size / shdr.sh_entsize;
451 if (lte->relplt == NULL
452 || elf_getdata(scn, lte->relplt) != NULL)
453 error(EXIT_FAILURE, 0,
454 "Couldn't get .rel*.plt data from \"%s\"",
455 filename);
456 break;
457 }
458 }
459
460 if (i == lte->ehdr.e_shnum)
461 error(EXIT_FAILURE, 0,
462 "Couldn't find .rel*.plt section in \"%s\"",
463 filename);
464
465 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
466 }
Petr Machata1974dbc2011-08-19 18:58:01 +0200467 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100468}
469
Joe Damato7a2bdf82010-11-08 15:47:41 -0800470void
Juan Cespedesf1350522008-12-16 18:19:58 +0100471do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200472 debug(DEBUG_FUNCTION, "do_close_elf()");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200473 if (lte->lte_flags & LTE_HASH_MALLOCED)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100474 free((char *)lte->hash);
475 elf_end(lte->elf);
476 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200477}
478
Petr Machata534e00f2011-09-27 17:58:38 +0200479static struct library_symbol *
480create_library_symbol(const char * name, GElf_Addr addr)
481{
482 size_t namel = strlen(name) + 1;
483 struct library_symbol * sym = calloc(sizeof(*sym) + namel, 1);
484 if (sym == NULL) {
485 perror("create_library_symbol");
486 return NULL;
487 }
488 sym->name = (char *)(sym + 1);
489 memcpy(sym->name, name, namel);
490 sym->enter_addr = (void *)(uintptr_t) addr;
491 return sym;
492}
493
Joe Damato7a2bdf82010-11-08 15:47:41 -0800494void
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100495add_library_symbol(GElf_Addr addr, const char *name,
496 struct library_symbol **library_symbolspp,
Petr Machata534e00f2011-09-27 17:58:38 +0200497 enum toplt type_of_plt, int is_weak)
498{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100499 struct library_symbol *s;
Juan Cespedescd8976d2009-05-14 13:47:58 +0200500
501 debug(DEBUG_FUNCTION, "add_library_symbol()");
502
Petr Machata534e00f2011-09-27 17:58:38 +0200503 s = create_library_symbol(name, addr);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100504 if (s == NULL)
505 error(EXIT_FAILURE, errno, "add_library_symbol failed");
506
507 s->needs_init = 1;
508 s->is_weak = is_weak;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200509 s->plt_type = type_of_plt;
Petr Machata534e00f2011-09-27 17:58:38 +0200510
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 s->next = *library_symbolspp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 *library_symbolspp = s;
513
514 debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name);
Juan Cespedesd914a202004-11-10 00:15:33 +0100515}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200516
Petr Machata534e00f2011-09-27 17:58:38 +0200517struct library_symbol *
518clone_library_symbol(struct library_symbol * sym)
519{
520 struct library_symbol * copy
521 = create_library_symbol(sym->name,
522 (GElf_Addr)(uintptr_t)sym->enter_addr);
523 if (copy == NULL)
524 return NULL;
525
526 copy->needs_init = sym->needs_init;
527 copy->is_weak = sym->is_weak;
528 copy->plt_type = sym->plt_type;
529
530 return copy;
531}
532
533void
534destroy_library_symbol(struct library_symbol * sym)
535{
536 free(sym);
537}
538
539void
540destroy_library_symbol_chain(struct library_symbol * sym)
541{
542 for (; sym != NULL; sym = sym->next) {
543 struct library_symbol * next = sym->next;
544 destroy_library_symbol(sym);
545 sym = next;
546 }
547}
548
Olaf Hering03c087b2006-09-25 02:31:27 +0200549/* stolen from elfutils-0.123 */
Juan Cespedesf1350522008-12-16 18:19:58 +0100550static unsigned long
551private_elf_gnu_hash(const char *name) {
Olaf Hering03c087b2006-09-25 02:31:27 +0200552 unsigned long h = 5381;
553 const unsigned char *string = (const unsigned char *)name;
554 unsigned char c;
555 for (c = *string; c; c = *++string)
556 h = h * 33 + c;
557 return h & 0xffffffff;
558}
559
Juan Cespedesf1350522008-12-16 18:19:58 +0100560static int
Joe Damato3268c5a2010-11-08 15:47:38 -0800561symbol_matches(struct ltelf *lte, size_t lte_i, GElf_Sym *sym,
562 size_t symidx, const char *name)
563{
564 GElf_Sym tmp_sym;
565 GElf_Sym *tmp;
566
567 tmp = (sym) ? (sym) : (&tmp_sym);
568
569 if (gelf_getsym(lte[lte_i].dynsym, symidx, tmp) == NULL)
570 error(EXIT_FAILURE, 0, "Couldn't get symbol from .dynsym");
571 else {
572 tmp->st_value += lte[lte_i].base_addr;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800573 debug(2, "symbol found: %s, %zd, %#" PRIx64,
Joe Damato3268c5a2010-11-08 15:47:38 -0800574 name, lte_i, tmp->st_value);
575 }
576 return tmp->st_value != 0
577 && tmp->st_shndx != SHN_UNDEF
578 && strcmp(name, lte[lte_i].dynstr + tmp->st_name) == 0;
579}
580
Joe Damato7a2bdf82010-11-08 15:47:41 -0800581int
Joe Damato3268c5a2010-11-08 15:47:38 -0800582in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100583 size_t i;
584 unsigned long hash;
Petr Machata35fe5182006-07-18 12:58:12 +0200585 unsigned long gnu_hash;
Juan Cespedesd914a202004-11-10 00:15:33 +0100586
Joe Damato3268c5a2010-11-08 15:47:38 -0800587 if (!count)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 return 1;
Juan Cespedesd914a202004-11-10 00:15:33 +0100589
Zachary T Welch0a43b322010-12-08 18:55:10 -0800590#ifdef ELF_HASH_TAKES_SIGNED_CHAR
591 hash = elf_hash(name);
592#else
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200593 hash = elf_hash((const unsigned char *)name);
Zachary T Welch0a43b322010-12-08 18:55:10 -0800594#endif
Petr Machata07bc4d12006-11-30 14:47:40 +0100595 gnu_hash = private_elf_gnu_hash(name);
Joe Damato3268c5a2010-11-08 15:47:38 -0800596
597 for (i = 0; i < count; ++i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100598 if (lte[i].hash == NULL)
599 continue;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200600
Petr Machata35fe5182006-07-18 12:58:12 +0200601 if (lte[i].hash_type == SHT_GNU_HASH) {
602 Elf32_Word * hashbase = lte[i].hash;
603 Elf32_Word nbuckets = *hashbase++;
604 Elf32_Word symbias = *hashbase++;
605 Elf32_Word bitmask_nwords = *hashbase++;
Petr Machata35fe5182006-07-18 12:58:12 +0200606 Elf32_Word * buckets;
607 Elf32_Word * chain_zero;
608 Elf32_Word bucket;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200609
Petr Machatab3f8fef2006-11-30 14:45:07 +0100610 // +1 for skipped `shift'
611 hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1;
Petr Machata35fe5182006-07-18 12:58:12 +0200612 buckets = hashbase;
613 hashbase += nbuckets;
614 chain_zero = hashbase - symbias;
615 bucket = buckets[gnu_hash % nbuckets];
Juan Cespedesd914a202004-11-10 00:15:33 +0100616
Petr Machata35fe5182006-07-18 12:58:12 +0200617 if (bucket != 0) {
618 const Elf32_Word *hasharr = &chain_zero[bucket];
619 do
620 if ((*hasharr & ~1u) == (gnu_hash & ~1u)) {
621 int symidx = hasharr - chain_zero;
Joe Damato3268c5a2010-11-08 15:47:38 -0800622 if (symbol_matches(lte, i,
623 sym, symidx,
624 name))
Petr Machata35fe5182006-07-18 12:58:12 +0200625 return 1;
626 }
627 while ((*hasharr++ & 1u) == 0);
628 }
Olaf Hering03c087b2006-09-25 02:31:27 +0200629 } else {
Petr Machata35fe5182006-07-18 12:58:12 +0200630 Elf32_Word nbuckets, symndx;
631 Elf32_Word *buckets, *chain;
632 nbuckets = lte[i].hash[0];
633 buckets = &lte[i].hash[2];
634 chain = &lte[i].hash[2 + nbuckets];
635
636 for (symndx = buckets[hash % nbuckets];
Joe Damato3268c5a2010-11-08 15:47:38 -0800637 symndx != STN_UNDEF; symndx = chain[symndx])
638 if (symbol_matches(lte, i, sym, symndx, name))
Petr Machata35fe5182006-07-18 12:58:12 +0200639 return 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100640 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200641 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100642 return 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100643}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200644
Juan Cespedesf1350522008-12-16 18:19:58 +0100645static GElf_Addr
646opd2addr(struct ltelf *lte, GElf_Addr addr) {
Petr Machatab3f8fef2006-11-30 14:45:07 +0100647#ifdef ARCH_SUPPORTS_OPD
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200648 unsigned long base, offset;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200649
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100650 if (!lte->opd)
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200651 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100652
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200653 base = (unsigned long)lte->opd->d_buf;
654 offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100655 if (offset > lte->opd_size)
656 error(EXIT_FAILURE, 0, "static plt not in .opd");
657
Olaf Heringa841f652006-09-15 01:57:49 +0200658 return *(GElf_Addr*)(base + offset);
Petr Machatab3f8fef2006-11-30 14:45:07 +0100659#else //!ARCH_SUPPORTS_OPD
660 return addr;
661#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100662}
663
Juan Cespedesf1350522008-12-16 18:19:58 +0100664struct library_symbol *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200665read_elf(Process *proc) {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200666 struct ltelf lte[MAX_LIBRARIES + 1];
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100667 size_t i;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100668 struct opt_x_t *xptr;
Petr Machata9c9c6052011-08-08 17:39:49 +0200669 struct opt_x_t *opt_x_loc = opt_x;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100670 struct library_symbol **lib_tail = NULL;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100671 int exit_out = 0;
Joe Damato3268c5a2010-11-08 15:47:38 -0800672 int count = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100673
Juan Cespedescd8976d2009-05-14 13:47:58 +0200674 debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename);
675
Petr Machatadf8f1ac2011-05-20 10:13:14 +0200676 memset(lte, 0, sizeof(lte));
Joe Damato7a2bdf82010-11-08 15:47:41 -0800677 library_symbols = NULL;
678 library_num = 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800679 proc->libdl_hooked = 0;
680
Petr Machata1974dbc2011-08-19 18:58:01 +0200681 if (do_init_elf(lte, proc->filename))
682 return NULL;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800683
684 memcpy(&main_lte, lte, sizeof(struct ltelf));
685
686 if (opt_p && opt_p->pid > 0) {
687 linkmap_init(proc, lte);
688 proc->libdl_hooked = 1;
689 }
690
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100691 proc->e_machine = lte->ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800692
693 for (i = 0; i < library_num; ++i) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200694 if (do_init_elf(&lte[i + 1], library[i]))
695 error(EXIT_FAILURE, errno, "Can't open \"%s\"",
Petr Machata993f4122011-09-15 17:49:25 +0200696 library[i]);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800697 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700698
699 if (!options.no_plt) {
Eric Vaitl1228a912006-12-28 16:16:56 +0100700#ifdef __mips__
Joe Damatofa2aefc2010-10-30 19:56:50 -0700701 // MIPS doesn't use the PLT and the GOT entries get changed
702 // on startup.
703 proc->need_to_reinitialize_breakpoints = 1;
704 for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){
705 GElf_Sym sym;
706 const char *name;
707 GElf_Addr addr = arch_plt_sym_val(lte, i, 0);
708 if (gelf_getsym(lte->dynsym, i, &sym) == NULL){
709 error(EXIT_FAILURE, 0,
710 "Couldn't get relocation from \"%s\"",
711 proc->filename);
712 }
713 name=lte->dynstr+sym.st_name;
Arnaud Patard95623b22010-01-08 08:40:02 -0500714 if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){
715 debug(2,"sym %s not a function",name);
716 continue;
717 }
718 add_library_symbol(addr, name, &library_symbols, 0,
719 ELF64_ST_BIND(sym.st_info) != 0);
720 if (!lib_tail)
721 lib_tail = &(library_symbols->next);
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200722 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100723#else
Joe Damatofa2aefc2010-10-30 19:56:50 -0700724 for (i = 0; i < lte->relplt_count; ++i) {
725 GElf_Rel rel;
726 GElf_Rela rela;
727 GElf_Sym sym;
728 GElf_Addr addr;
729 void *ret;
730 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100731
Joe Damatofa2aefc2010-10-30 19:56:50 -0700732 if (lte->relplt->d_type == ELF_T_REL) {
733 ret = gelf_getrel(lte->relplt, i, &rel);
734 rela.r_offset = rel.r_offset;
735 rela.r_info = rel.r_info;
736 rela.r_addend = 0;
737 } else
738 ret = gelf_getrela(lte->relplt, i, &rela);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100739
Joe Damatofa2aefc2010-10-30 19:56:50 -0700740 if (ret == NULL
741 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
742 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
743 &sym) == NULL)
744 error(EXIT_FAILURE, 0,
745 "Couldn't get relocation from \"%s\"",
746 proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100747
Paul Gilliambe320772006-04-24 22:06:23 +0200748#ifdef PLT_REINITALISATION_BP
Joe Damatofa2aefc2010-10-30 19:56:50 -0700749 if (!sym.st_value && PLTs_initialized_by_here)
750 proc->need_to_reinitialize_breakpoints = 1;
Paul Gilliambe320772006-04-24 22:06:23 +0200751#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100752
Joe Damato3268c5a2010-11-08 15:47:38 -0800753 name = lte->dynstr + sym.st_name;
754 count = library_num ? library_num+1 : 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200755
Joe Damato3268c5a2010-11-08 15:47:38 -0800756 if (in_load_libraries(name, lte, count, NULL)) {
757 enum toplt pltt;
758 if (sym.st_value == 0 && lte->plt_stub_vma != 0) {
759 pltt = LS_TOPLT_EXEC;
760 addr = lte->plt_stub_vma + PPC_PLT_STUB_SIZE * i;
761 }
762 else {
763 pltt = PLTS_ARE_EXECUTABLE(lte)
764 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
765 addr = arch_plt_sym_val(lte, i, &rela);
766 }
767
768 add_library_symbol(addr, name, &library_symbols, pltt,
769 ELF64_ST_BIND(sym.st_info) == STB_WEAK);
770 if (!lib_tail)
771 lib_tail = &(library_symbols->next);
Joe Damatofa2aefc2010-10-30 19:56:50 -0700772 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100773 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100774#endif // !__mips__
Paul Gilliambe320772006-04-24 22:06:23 +0200775#ifdef PLT_REINITALISATION_BP
Joe Damatofa2aefc2010-10-30 19:56:50 -0700776 struct opt_x_t *main_cheat;
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200777
Joe Damatofa2aefc2010-10-30 19:56:50 -0700778 if (proc->need_to_reinitialize_breakpoints) {
779 /* Add "PLTs_initialized_by_here" to opt_x list, if not
780 already there. */
781 main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t));
782 if (main_cheat == NULL)
783 error(EXIT_FAILURE, 0, "Couldn't allocate memory");
Petr Machata9c9c6052011-08-08 17:39:49 +0200784 main_cheat->next = opt_x_loc;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700785 main_cheat->found = 0;
786 main_cheat->name = PLTs_initialized_by_here;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100787
Petr Machata9c9c6052011-08-08 17:39:49 +0200788 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Joe Damatofa2aefc2010-10-30 19:56:50 -0700789 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0
790 && main_cheat) {
791 free(main_cheat);
792 main_cheat = NULL;
793 break;
794 }
795 if (main_cheat)
Petr Machata9c9c6052011-08-08 17:39:49 +0200796 opt_x_loc = main_cheat;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700797 }
Paul Gilliambe320772006-04-24 22:06:23 +0200798#endif
Joe Damatofa2aefc2010-10-30 19:56:50 -0700799 } else {
800 lib_tail = &library_symbols;
801 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100802
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100803 for (i = 0; i < lte->symtab_count; ++i) {
804 GElf_Sym sym;
805 GElf_Addr addr;
806 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100807
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100808 if (gelf_getsym(lte->symtab, i, &sym) == NULL)
809 error(EXIT_FAILURE, 0,
810 "Couldn't get symbol from \"%s\"",
811 proc->filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100812
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100813 name = lte->strtab + sym.st_name;
814 addr = sym.st_value;
815 if (!addr)
816 continue;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100817
Petr Machata9c9c6052011-08-08 17:39:49 +0200818 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100819 if (xptr->name && strcmp(xptr->name, name) == 0) {
820 /* FIXME: Should be able to use &library_symbols as above. But
821 when you do, none of the real library symbols cause breaks. */
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200822 add_library_symbol(opd2addr(lte, addr),
Paul Gilliam76c61f12006-06-14 06:55:21 +0200823 name, lib_tail, LS_TOPLT_NONE, 0);
Paul Gilliam24e643a2006-03-13 18:43:13 +0100824 xptr->found = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100825 break;
826 }
827 }
Joe Damatoe2a8f572010-11-08 15:47:40 -0800828
Zachary T Welchba6aca22010-12-08 18:55:09 -0800829 unsigned found_count = 0;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800830
Petr Machata9c9c6052011-08-08 17:39:49 +0200831 for (xptr = opt_x_loc; xptr; xptr = xptr->next) {
Joe Damatoe2a8f572010-11-08 15:47:40 -0800832 if (xptr->found)
833 continue;
834
835 GElf_Sym sym;
836 GElf_Addr addr;
837 if (in_load_libraries(xptr->name, lte, library_num+1, &sym)) {
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800838 debug(2, "found symbol %s @ %#" PRIx64 ", adding it.",
839 xptr->name, sym.st_value);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800840 addr = sym.st_value;
841 if (ELF32_ST_TYPE (sym.st_info) == STT_FUNC) {
842 add_library_symbol(addr, xptr->name, lib_tail, LS_TOPLT_NONE, 0);
843 xptr->found = 1;
844 found_count++;
845 }
846 }
847 if (found_count == opt_x_cnt){
848 debug(2, "done, found everything: %d\n", found_count);
849 break;
850 }
851 }
852
Petr Machata9c9c6052011-08-08 17:39:49 +0200853 for (xptr = opt_x_loc; xptr; xptr = xptr->next)
Paul Gilliam24e643a2006-03-13 18:43:13 +0100854 if ( ! xptr->found) {
855 char *badthing = "WARNING";
Paul Gilliambe320772006-04-24 22:06:23 +0200856#ifdef PLT_REINITALISATION_BP
857 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) {
858 if (lte->ehdr.e_entry) {
859 add_library_symbol (
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200860 opd2addr (lte, lte->ehdr.e_entry),
Paul Gilliambe320772006-04-24 22:06:23 +0200861 PLTs_initialized_by_here,
862 lib_tail, 1, 0);
863 fprintf (stderr, "WARNING: Using e_ent"
864 "ry from elf header (%p) for "
865 "address of \"%s\"\n", (void*)
866 (long) lte->ehdr.e_entry,
867 PLTs_initialized_by_here);
868 continue;
869 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100870 badthing = "ERROR";
871 exit_out = 1;
872 }
Paul Gilliambe320772006-04-24 22:06:23 +0200873#endif
Paul Gilliam24e643a2006-03-13 18:43:13 +0100874 fprintf (stderr,
Joe Damatof0bd98b2010-11-08 15:47:42 -0800875 "%s: Couldn't find symbol \"%s\" in file \"%s\" assuming it will be loaded by libdl!"
876 "\n", badthing, xptr->name, proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100877 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100878 if (exit_out) {
879 exit (1);
880 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100881
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100882 for (i = 0; i < library_num + 1; ++i)
883 do_close_elf(&lte[i]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100884
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100885 return library_symbols;
Juan Cespedes96935a91997-08-09 23:45:39 +0200886}