blob: 231b56e717fb3c64e110f280d15d733747d397de [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"
Petr Machata2b46cfc2012-02-18 11:17:29 +010017#include "library.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080018
Paul Gilliambe320772006-04-24 22:06:23 +020019#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010020extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020021#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010022
Petr Machatafe1c1712010-10-27 16:57:34 +020023#ifndef DT_PPC_GOT
24# define DT_PPC_GOT (DT_LOPROC + 0)
25#endif
26
27#define PPC_PLT_STUB_SIZE 16
28
29static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
30{
31 Elf_Data *data = elf_getdata(scn, NULL);
32 if (data == NULL || elf_getdata(scn, data) != NULL
33 || data->d_off || data->d_size != shdr->sh_size)
34 return NULL;
35 return data;
36}
37
38static int inside(GElf_Addr addr, GElf_Shdr *shdr)
39{
40 return addr >= shdr->sh_addr
41 && addr < shdr->sh_addr + shdr->sh_size;
42}
43
44static int maybe_pick_section(GElf_Addr addr,
45 Elf_Scn *in_sec, GElf_Shdr *in_shdr,
46 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
47{
Petr Machata2b46cfc2012-02-18 11:17:29 +010048 if (inside(addr, in_shdr)) {
Petr Machatafe1c1712010-10-27 16:57:34 +020049 *tgt_sec = in_sec;
50 *tgt_shdr = *in_shdr;
51 return 1;
52 }
53 return 0;
54}
55
56static int get_section_covering(struct ltelf *lte, GElf_Addr addr,
57 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
58{
59 int i;
60 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
61 Elf_Scn *scn;
62 GElf_Shdr shdr;
63
64 scn = elf_getscn(lte->elf, i);
65 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
66 debug(1, "Couldn't read section or header.");
67 return 0;
68 }
69
70 if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr))
71 return 1;
72 }
73
74 return 0;
75}
76
77static GElf_Addr read32be(Elf_Data *data, size_t offset)
78{
79 if (data->d_size < offset + 4) {
80 debug(1, "Not enough data to read 32bit value at offset %zd.",
81 offset);
82 return 0;
83 }
84
85 unsigned char const *buf = data->d_buf + offset;
86 return ((Elf32_Word)buf[0] << 24)
87 | ((Elf32_Word)buf[1] << 16)
88 | ((Elf32_Word)buf[2] << 8)
89 | ((Elf32_Word)buf[3]);
90}
91
92static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot,
93 Elf_Data *plt_data)
94{
95 Elf_Scn *ppcgot_sec = NULL;
96 GElf_Shdr ppcgot_shdr;
97 if (ppcgot != 0
98 && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr))
99 // xxx should be the log out
100 fprintf(stderr,
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800101 "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n",
Petr Machatafe1c1712010-10-27 16:57:34 +0200102 ppcgot);
103
104 if (ppcgot_sec != NULL) {
105 Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr);
106 if (data == NULL
107 || data->d_size < 8 )
108 debug(1, "Couldn't read GOT data.");
109 else {
110 // where PPCGOT begins in .got
111 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
112 GElf_Addr glink_vma = read32be(data, offset + 4);
113 if (glink_vma != 0) {
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800114 debug(1, "PPC GOT glink_vma address: %#" PRIx64,
Petr Machatafe1c1712010-10-27 16:57:34 +0200115 glink_vma);
116 return glink_vma;
117 }
118 }
119 }
120
121 if (plt_data != NULL) {
122 GElf_Addr glink_vma = read32be(plt_data, 0);
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800123 debug(1, ".plt glink_vma address: %#" PRIx64, glink_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200124 return glink_vma;
125 }
126
127 return 0;
128}
129
Petr Machata1974dbc2011-08-19 18:58:01 +0200130int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200131open_elf(struct ltelf *lte, const char *filename)
132{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100133 lte->fd = open(filename, O_RDONLY);
134 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200135 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200136
Petr Machata02bd9ec2011-09-21 17:38:59 +0200137 elf_version(EV_CURRENT);
138
Juan Cespedesd914a202004-11-10 00:15:33 +0100139#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200141#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100142 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200143#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200144
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100145 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
146 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200147
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100148 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
149 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
150 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200151
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100152 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
153 error(EXIT_FAILURE, 0,
154 "\"%s\" is not an ELF executable nor shared library",
155 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200156
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100157 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
158 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100159#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
161 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100162#endif
163#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100164 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
165 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100166#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100167 )
168 error(EXIT_FAILURE, 0,
169 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100170
Petr Machata02bd9ec2011-09-21 17:38:59 +0200171 return 0;
172}
173
Petr Machata2b46cfc2012-02-18 11:17:29 +0100174/* XXX temporarily non-static */
Petr Machata02bd9ec2011-09-21 17:38:59 +0200175int
Petr Machata29add4f2012-02-18 16:38:05 +0100176do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr base)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200178 int i;
179 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100180 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200181 size_t relplt_size = 0;
182
183 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
184 debug(1, "Reading ELF from %s...", filename);
185
186 if (open_elf(lte, filename) < 0)
187 return -1;
188
Petr Machatafe1c1712010-10-27 16:57:34 +0200189 Elf_Data *plt_data = NULL;
190 GElf_Addr ppcgot = 0;
191
Petr Machata29add4f2012-02-18 16:38:05 +0100192 /* Find out the bias. For DSOs, this will be just BASE,
193 * unless the DSO is pre-linked. For ET_EXEC files, this will
194 * turn out to be 0. */
195 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100196 GElf_Phdr phdr;
197 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
198 if (phdr.p_type == PT_LOAD) {
Petr Machata11639da2012-03-02 00:10:51 +0100199 if (base == 0)
200 base = phdr.p_vaddr;
201 lte->base_addr = base;
Petr Machata29add4f2012-02-18 16:38:05 +0100202 lte->bias = lte->base_addr - phdr.p_vaddr;
203 fprintf(stderr,
204 " + vaddr=%#lx, base=%#lx, bias=%#lx\n",
205 lte->base_addr, base, lte->bias);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206 break;
207 }
208 }
209 }
210
Petr Machata29add4f2012-02-18 16:38:05 +0100211 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100212
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
214 Elf_Scn *scn;
215 GElf_Shdr shdr;
216 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100217
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100218 scn = elf_getscn(lte->elf, i);
219 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
220 error(EXIT_FAILURE, 0,
221 "Couldn't get section header from \"%s\"",
222 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100223
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
225 if (name == NULL)
226 error(EXIT_FAILURE, 0,
227 "Couldn't get section header from \"%s\"",
228 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100229
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100230 if (shdr.sh_type == SHT_SYMTAB) {
231 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100232
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 lte->symtab = elf_getdata(scn, NULL);
234 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
235 if ((lte->symtab == NULL
236 || elf_getdata(scn, lte->symtab) != NULL)
237 && opt_x != NULL)
238 error(EXIT_FAILURE, 0,
239 "Couldn't get .symtab data from \"%s\"",
240 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100241
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100242 scn = elf_getscn(lte->elf, shdr.sh_link);
243 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
244 error(EXIT_FAILURE, 0,
245 "Couldn't get section header from \"%s\"",
246 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100247
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 data = elf_getdata(scn, NULL);
249 if (data == NULL || elf_getdata(scn, data) != NULL
250 || shdr.sh_size != data->d_size || data->d_off)
251 error(EXIT_FAILURE, 0,
252 "Couldn't get .strtab data from \"%s\"",
253 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100254
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 lte->strtab = data->d_buf;
256 } else if (shdr.sh_type == SHT_DYNSYM) {
257 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100258
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100259 lte->dynsym = elf_getdata(scn, NULL);
260 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
261 if (lte->dynsym == NULL
262 || elf_getdata(scn, lte->dynsym) != NULL)
263 error(EXIT_FAILURE, 0,
264 "Couldn't get .dynsym data from \"%s\"",
265 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100266
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100267 scn = elf_getscn(lte->elf, shdr.sh_link);
268 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
269 error(EXIT_FAILURE, 0,
270 "Couldn't get section header from \"%s\"",
271 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100272
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100273 data = elf_getdata(scn, NULL);
274 if (data == NULL || elf_getdata(scn, data) != NULL
275 || shdr.sh_size != data->d_size || data->d_off)
276 error(EXIT_FAILURE, 0,
277 "Couldn't get .dynstr data from \"%s\"",
278 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100279
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100280 lte->dynstr = data->d_buf;
281 } else if (shdr.sh_type == SHT_DYNAMIC) {
282 Elf_Data *data;
283 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100284
Joe Damato87f4f582010-11-08 15:47:36 -0800285 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
287 extern void *dyn_addr;
288 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800289 lte->dyn_sz = shdr.sh_size;
290
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100291 data = elf_getdata(scn, NULL);
292 if (data == NULL || elf_getdata(scn, data) != NULL)
293 error(EXIT_FAILURE, 0,
294 "Couldn't get .dynamic data from \"%s\"",
295 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100296
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100297 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
298 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100299
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100300 if (gelf_getdyn(data, j, &dyn) == NULL)
301 error(EXIT_FAILURE, 0,
302 "Couldn't get .dynamic data from \"%s\"",
303 filename);
Eric Vaitl1228a912006-12-28 16:16:56 +0100304#ifdef __mips__
305/**
306 MIPS ABI Supplement:
Ian Wienand9a2ad352006-02-20 22:44:45 +0100307
Juan Cespedesf1350522008-12-16 18:19:58 +0100308 DT_PLTGOT This member holds the address of the .got section.
Eric Vaitl1228a912006-12-28 16:16:56 +0100309
310 DT_MIPS_SYMTABNO This member holds the number of entries in the
311 .dynsym section.
312
313 DT_MIPS_LOCAL_GOTNO This member holds the number of local global
314 offset table entries.
315
316 DT_MIPS_GOTSYM This member holds the index of the first dyamic
317 symbol table entry that corresponds to an entry in the gobal offset
318 table.
319
320 */
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200321 if(dyn.d_tag==DT_PLTGOT){
Juan Cespedesf1350522008-12-16 18:19:58 +0100322 lte->pltgot_addr=dyn.d_un.d_ptr;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200323 }
324 if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){
325 lte->mips_local_gotno=dyn.d_un.d_val;
326 }
327 if(dyn.d_tag==DT_MIPS_GOTSYM){
328 lte->mips_gotsym=dyn.d_un.d_val;
329 }
Eric Vaitl1228a912006-12-28 16:16:56 +0100330#endif // __mips__
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 if (dyn.d_tag == DT_JMPREL)
332 relplt_addr = dyn.d_un.d_ptr;
333 else if (dyn.d_tag == DT_PLTRELSZ)
334 relplt_size = dyn.d_un.d_val;
Petr Machatafe1c1712010-10-27 16:57:34 +0200335 else if (dyn.d_tag == DT_PPC_GOT) {
336 ppcgot = dyn.d_un.d_val;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800337 debug(1, "ppcgot %#" PRIx64, ppcgot);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100338 } else if (dyn.d_tag == DT_SONAME) {
339 soname_offset = dyn.d_un.d_val;
Petr Machatafe1c1712010-10-27 16:57:34 +0200340 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 } else if (shdr.sh_type == SHT_PROGBITS
343 || shdr.sh_type == SHT_NOBITS) {
344 if (strcmp(name, ".plt") == 0) {
345 lte->plt_addr = shdr.sh_addr;
346 lte->plt_size = shdr.sh_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200347 if (shdr.sh_flags & SHF_EXECINSTR) {
348 lte->lte_flags |= LTE_PLT_EXECUTABLE;
349 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200350 if (lte->ehdr.e_machine == EM_PPC) {
351 plt_data = loaddata(scn, &shdr);
352 if (plt_data == NULL)
353 fprintf(stderr,
354 "Can't load .plt data\n");
355 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100356 }
357#ifdef ARCH_SUPPORTS_OPD
358 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200359 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 lte->opd_size = shdr.sh_size;
361 lte->opd = elf_rawdata(scn, NULL);
362 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100363#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100365 }
366
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367 if (lte->dynsym == NULL || lte->dynstr == NULL)
368 error(EXIT_FAILURE, 0,
369 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100370
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 if (!relplt_addr || !lte->plt_addr) {
372 debug(1, "%s has no PLT relocations", filename);
373 lte->relplt = NULL;
374 lte->relplt_count = 0;
Petr Machatafe1c1712010-10-27 16:57:34 +0200375 } else if (relplt_size == 0) {
376 debug(1, "%s has unknown PLT size", filename);
377 lte->relplt = NULL;
378 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100379 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200380 if (lte->ehdr.e_machine == EM_PPC) {
381 GElf_Addr glink_vma
382 = get_glink_vma(lte, ppcgot, plt_data);
383
384 assert (relplt_size % 12 == 0);
385 size_t count = relplt_size / 12; // size of RELA entry
386 lte->plt_stub_vma = glink_vma
387 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
Zachary T Welchbfb26c72010-12-06 23:21:00 -0800388 debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma);
Petr Machatafe1c1712010-10-27 16:57:34 +0200389 }
390
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
392 Elf_Scn *scn;
393 GElf_Shdr shdr;
394
395 scn = elf_getscn(lte->elf, i);
396 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
397 error(EXIT_FAILURE, 0,
398 "Couldn't get section header from \"%s\"",
399 filename);
400 if (shdr.sh_addr == relplt_addr
401 && shdr.sh_size == relplt_size) {
402 lte->relplt = elf_getdata(scn, NULL);
403 lte->relplt_count =
404 shdr.sh_size / shdr.sh_entsize;
405 if (lte->relplt == NULL
406 || elf_getdata(scn, lte->relplt) != NULL)
407 error(EXIT_FAILURE, 0,
408 "Couldn't get .rel*.plt data from \"%s\"",
409 filename);
410 break;
411 }
412 }
413
414 if (i == lte->ehdr.e_shnum)
415 error(EXIT_FAILURE, 0,
416 "Couldn't find .rel*.plt section in \"%s\"",
417 filename);
418
419 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
420 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421
422 if (soname_offset != 0)
423 lte->soname = lte->dynstr + soname_offset;
424
Petr Machata1974dbc2011-08-19 18:58:01 +0200425 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426}
427
Petr Machata2b46cfc2012-02-18 11:17:29 +0100428/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800429void
Juan Cespedesf1350522008-12-16 18:19:58 +0100430do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200431 debug(DEBUG_FUNCTION, "do_close_elf()");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432 elf_end(lte->elf);
433 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200434}
435
Petr Machata2b46cfc2012-02-18 11:17:29 +0100436/* XXX non-static for now, as it's not called anywhere. But we want
437 * this code around. */
438GElf_Addr
Juan Cespedesf1350522008-12-16 18:19:58 +0100439opd2addr(struct ltelf *lte, GElf_Addr addr) {
Petr Machatab3f8fef2006-11-30 14:45:07 +0100440#ifdef ARCH_SUPPORTS_OPD
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200441 unsigned long base, offset;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200442
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 if (!lte->opd)
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200444 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100445
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200446 base = (unsigned long)lte->opd->d_buf;
447 offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100448 if (offset > lte->opd_size)
449 error(EXIT_FAILURE, 0, "static plt not in .opd");
450
Olaf Heringa841f652006-09-15 01:57:49 +0200451 return *(GElf_Addr*)(base + offset);
Petr Machatab3f8fef2006-11-30 14:45:07 +0100452#else //!ARCH_SUPPORTS_OPD
453 return addr;
454#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100455}
456
Petr Machata2b46cfc2012-02-18 11:17:29 +0100457struct library *
458ltelf_read_library(const char *filename, GElf_Addr base)
Petr Machatae84fa002012-02-07 13:43:03 +0100459{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100460 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100461 struct ltelf lte = {};
462 if (do_init_elf(&lte, filename, base) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200463 return NULL;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800464
Petr Machata2b46cfc2012-02-18 11:17:29 +0100465 struct library *lib = malloc(sizeof(*lib));
466 char *soname = NULL;
467 if (lib == NULL) {
468 fail:
469 free(soname);
470 library_destroy(lib);
471 free(lib);
472 lib = NULL;
473 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800474 }
475
Petr Machata2b46cfc2012-02-18 11:17:29 +0100476 if (lte.soname != NULL) {
477 soname = strdup(lte.soname);
478 if (soname == NULL)
479 goto fail;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800480 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700481
Petr Machata2b46cfc2012-02-18 11:17:29 +0100482 library_init(lib, soname, soname != NULL);
483 lib->entry = (target_address_t)lte.entry_addr;
484 lib->base = (target_address_t)lte.base_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100485
Petr Machata2b46cfc2012-02-18 11:17:29 +0100486 size_t i;
487 for (i = 0; i < lte.relplt_count; ++i) {
488 GElf_Rel rel;
489 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100490 GElf_Sym sym;
491 GElf_Addr addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100492 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100493
Petr Machata2b46cfc2012-02-18 11:17:29 +0100494 if (lte.relplt->d_type == ELF_T_REL) {
495 ret = gelf_getrel(lte.relplt, i, &rel);
496 rela.r_offset = rel.r_offset;
497 rela.r_info = rel.r_info;
498 rela.r_addend = 0;
499 } else {
500 ret = gelf_getrela(lte.relplt, i, &rela);
501 }
502
503 if (ret == NULL
504 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
505 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
506 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100507 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508 "Couldn't get relocation from \"%s\"",
509 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100510
Petr Machata2b46cfc2012-02-18 11:17:29 +0100511 /* We will destroy the ELF object at the end of the
512 * scope. We need to copy the name for our purposes.
513 * XXX consider just keeping the ELF around. */
514 char *name = strdup(lte.dynstr + sym.st_name);
515 if (name == NULL) {
516 fail2:
517 free(name);
518 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800519 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100520
521 enum toplt pltt;
522 if (sym.st_value == 0 && lte.plt_stub_vma != 0) {
523 pltt = LS_TOPLT_EXEC;
524 addr = lte.plt_stub_vma + PPC_PLT_STUB_SIZE * i;
525 } else {
526 pltt = PLTS_ARE_EXECUTABLE(&lte)
527 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
528 addr = arch_plt_sym_val(&lte, i, &rela);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800529 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100530
531 struct library_symbol *libsym = malloc(sizeof(*libsym));
532 if (libsym == NULL)
533 goto fail2;
Petr Machata29add4f2012-02-18 16:38:05 +0100534 library_symbol_init(libsym, lib, addr + lte.bias, name, 1, pltt,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100535 ELF64_ST_BIND(sym.st_info) == STB_WEAK);
536 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800537 }
538
Petr Machata2b46cfc2012-02-18 11:17:29 +0100539done:
540 do_close_elf(&lte);
541 return lib;
542}
Petr Machatae84fa002012-02-07 13:43:03 +0100543
Petr Machata2b46cfc2012-02-18 11:17:29 +0100544struct library *
545ltelf_read_main_binary(struct Process *proc, const char *path)
546{
547 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
548 char *fname = pid2name(proc->pid);
549 struct library *lib = ltelf_read_library(fname, 0);
550 library_set_name(lib, path, 0);
551 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200552}