blob: 48ee6ce5b552d29bbdca22b074ec1380ce4bc91f [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
Petr Machatafe1c1712010-10-27 16:57:34 +020027
Petr Machatae67635d2012-03-21 03:37:39 +010028#ifndef ARCH_HAVE_LTELF_DATA
29int
Petr Machatae67635d2012-03-21 03:37:39 +010030arch_elf_init(struct ltelf *lte)
31{
32 return 0;
33}
34#endif
35
Petr Machatae6523e62012-03-24 04:54:06 +010036int
37default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
38 const char *a_name, GElf_Rela *rela, size_t i,
39 struct library_symbol **ret)
40{
41 char *name = strdup(a_name);
42 if (name == NULL) {
43 fail:
44 free(name);
45 return -1;
46 }
47
48 enum toplt pltt = PLTS_ARE_EXECUTABLE(lte)
49 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
50 GElf_Addr addr = arch_plt_sym_val(lte, i, rela);
51
52 struct library_symbol *libsym = malloc(sizeof(*libsym));
53 if (libsym == NULL)
54 goto fail;
55
56 target_address_t taddr = (target_address_t)(addr + lte->bias);
57 if (libsym == NULL
58 || arch_translate_address(proc, taddr, &taddr) < 0) {
59 free(libsym);
60 goto fail;
61 }
62
63 library_symbol_init(libsym, taddr, name, 1, pltt);
64 *ret = libsym;
65 return 0;
66}
67
68#ifndef ARCH_HAVE_ADD_PLT_ENTRY
69enum plt_status
70arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
71 const char *a_name, GElf_Rela *rela, size_t i,
72 struct library_symbol **ret)
73{
74 return plt_default;
75}
76#endif
77
Petr Machatae67635d2012-03-21 03:37:39 +010078Elf_Data *
79elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020080{
81 Elf_Data *data = elf_getdata(scn, NULL);
82 if (data == NULL || elf_getdata(scn, data) != NULL
83 || data->d_off || data->d_size != shdr->sh_size)
84 return NULL;
85 return data;
86}
87
Petr Machatae67635d2012-03-21 03:37:39 +010088static int
Petr Machataffd5aab2012-03-24 02:03:33 +010089elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
90 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
91 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020092{
93 int i;
94 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
95 Elf_Scn *scn;
96 GElf_Shdr shdr;
97
98 scn = elf_getscn(lte->elf, i);
99 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
100 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100101 return -1;
102 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100103 if (predicate(scn, &shdr, data)) {
104 *tgt_sec = scn;
105 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200106 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100107 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200108 }
Petr Machatae67635d2012-03-21 03:37:39 +0100109 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100110
111}
112
113static int
114inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
115{
116 GElf_Addr addr = *(GElf_Addr *)data;
117 return addr >= shdr->sh_addr
118 && addr < shdr->sh_addr + shdr->sh_size;
119}
120
121int
122elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
123 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
124{
125 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
126 &inside_p, &addr);
127}
128
129static int
130type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
131{
132 GElf_Word type = *(GElf_Word *)data;
133 return shdr->sh_type == type;
134}
135
136int
137elf_get_section_type(struct ltelf *lte, GElf_Word type,
138 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
139{
140 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
141 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100142}
143
144static int
145need_data(Elf_Data *data, size_t offset, size_t size)
146{
147 assert(data != NULL);
148 if (data->d_size < size || offset > data->d_size - size) {
149 debug(1, "Not enough data to read %zd-byte value"
150 " at offset %zd.", size, offset);
151 return -1;
152 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200153 return 0;
154}
155
Petr Machatae67635d2012-03-21 03:37:39 +0100156#define DEF_READER(NAME, SIZE) \
157 int \
158 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
159 { \
160 if (!need_data(data, offset, SIZE / 8) < 0) \
161 return -1; \
162 \
163 union { \
164 uint##SIZE##_t dst; \
165 char buf[0]; \
166 } u; \
167 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
168 *retp = u.dst; \
169 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200170 }
171
Petr Machatae67635d2012-03-21 03:37:39 +0100172DEF_READER(elf_read_u16, 16)
173DEF_READER(elf_read_u32, 32)
174DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200175
Petr Machatae67635d2012-03-21 03:37:39 +0100176#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200177
Petr Machata1974dbc2011-08-19 18:58:01 +0200178int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200179open_elf(struct ltelf *lte, const char *filename)
180{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 lte->fd = open(filename, O_RDONLY);
182 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200183 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200184
Petr Machata02bd9ec2011-09-21 17:38:59 +0200185 elf_version(EV_CURRENT);
186
Juan Cespedesd914a202004-11-10 00:15:33 +0100187#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100188 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200189#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100190 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200191#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200192
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
194 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200195
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
197 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
198 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200199
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
201 error(EXIT_FAILURE, 0,
202 "\"%s\" is not an ELF executable nor shared library",
203 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200204
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
206 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100207#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
209 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100210#endif
211#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
213 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100214#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100215 )
216 error(EXIT_FAILURE, 0,
217 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100218
Petr Machata02bd9ec2011-09-21 17:38:59 +0200219 return 0;
220}
221
Petr Machatae67635d2012-03-21 03:37:39 +0100222static int
223do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100224{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200225 int i;
226 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200228
229 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
230 debug(1, "Reading ELF from %s...", filename);
231
232 if (open_elf(lte, filename) < 0)
233 return -1;
234
Petr Machatab120fdf2012-03-21 05:05:46 +0100235 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100236 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237 GElf_Phdr phdr;
238 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
239 if (phdr.p_type == PT_LOAD) {
Petr Machatab120fdf2012-03-21 05:05:46 +0100240 lte->base_addr = phdr.p_vaddr - bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100241 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100242 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
243 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 break;
245 }
246 }
247 }
248
Petr Machatab120fdf2012-03-21 05:05:46 +0100249 if (lte->base_addr == 0) {
250 fprintf(stderr, "Couldn't determine base address of %s\n",
251 filename);
252 return -1;
253 }
254
255 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100256 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100257
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
259 Elf_Scn *scn;
260 GElf_Shdr shdr;
261 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100262
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100263 scn = elf_getscn(lte->elf, i);
264 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
265 error(EXIT_FAILURE, 0,
266 "Couldn't get section header from \"%s\"",
267 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100268
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
270 if (name == NULL)
271 error(EXIT_FAILURE, 0,
272 "Couldn't get section header from \"%s\"",
273 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100274
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 if (shdr.sh_type == SHT_SYMTAB) {
276 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100277
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 lte->symtab = elf_getdata(scn, NULL);
279 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
280 if ((lte->symtab == NULL
281 || elf_getdata(scn, lte->symtab) != NULL)
282 && opt_x != NULL)
283 error(EXIT_FAILURE, 0,
284 "Couldn't get .symtab data from \"%s\"",
285 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100286
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 scn = elf_getscn(lte->elf, shdr.sh_link);
288 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
289 error(EXIT_FAILURE, 0,
290 "Couldn't get section header from \"%s\"",
291 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100292
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 data = elf_getdata(scn, NULL);
294 if (data == NULL || elf_getdata(scn, data) != NULL
295 || shdr.sh_size != data->d_size || data->d_off)
296 error(EXIT_FAILURE, 0,
297 "Couldn't get .strtab data from \"%s\"",
298 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100299
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100300 lte->strtab = data->d_buf;
301 } else if (shdr.sh_type == SHT_DYNSYM) {
302 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100303
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100304 lte->dynsym = elf_getdata(scn, NULL);
305 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
306 if (lte->dynsym == NULL
307 || elf_getdata(scn, lte->dynsym) != NULL)
308 error(EXIT_FAILURE, 0,
309 "Couldn't get .dynsym data from \"%s\"",
310 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100311
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 scn = elf_getscn(lte->elf, shdr.sh_link);
313 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
314 error(EXIT_FAILURE, 0,
315 "Couldn't get section header from \"%s\"",
316 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100317
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100318 data = elf_getdata(scn, NULL);
319 if (data == NULL || elf_getdata(scn, data) != NULL
320 || shdr.sh_size != data->d_size || data->d_off)
321 error(EXIT_FAILURE, 0,
322 "Couldn't get .dynstr data from \"%s\"",
323 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100324
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100325 lte->dynstr = data->d_buf;
326 } else if (shdr.sh_type == SHT_DYNAMIC) {
327 Elf_Data *data;
328 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100329
Joe Damato87f4f582010-11-08 15:47:36 -0800330 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100331 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
332 extern void *dyn_addr;
333 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800334 lte->dyn_sz = shdr.sh_size;
335
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100336 data = elf_getdata(scn, NULL);
337 if (data == NULL || elf_getdata(scn, data) != NULL)
338 error(EXIT_FAILURE, 0,
339 "Couldn't get .dynamic data from \"%s\"",
340 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100341
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
343 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100344
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 if (gelf_getdyn(data, j, &dyn) == NULL)
346 error(EXIT_FAILURE, 0,
347 "Couldn't get .dynamic data from \"%s\"",
348 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 if (dyn.d_tag == DT_JMPREL)
350 relplt_addr = dyn.d_un.d_ptr;
351 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100352 lte->relplt_size = dyn.d_un.d_val;
353 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100354 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100356 } else if (shdr.sh_type == SHT_PROGBITS
357 || shdr.sh_type == SHT_NOBITS) {
358 if (strcmp(name, ".plt") == 0) {
359 lte->plt_addr = shdr.sh_addr;
360 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100361 lte->plt_data = elf_loaddata(scn, &shdr);
362 if (lte->plt_data == NULL)
363 fprintf(stderr,
364 "Can't load .plt data\n");
365 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200366 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100367 }
368#ifdef ARCH_SUPPORTS_OPD
369 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200370 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 lte->opd_size = shdr.sh_size;
372 lte->opd = elf_rawdata(scn, NULL);
373 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100374#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100376 }
377
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100378 if (lte->dynsym == NULL || lte->dynstr == NULL)
379 error(EXIT_FAILURE, 0,
380 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100381
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100382 if (!relplt_addr || !lte->plt_addr) {
383 debug(1, "%s has no PLT relocations", filename);
384 lte->relplt = NULL;
385 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100386 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200387 debug(1, "%s has unknown PLT size", filename);
388 lte->relplt = NULL;
389 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200391
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
393 Elf_Scn *scn;
394 GElf_Shdr shdr;
395
396 scn = elf_getscn(lte->elf, i);
397 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
398 error(EXIT_FAILURE, 0,
399 "Couldn't get section header from \"%s\"",
400 filename);
401 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100402 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100403 lte->relplt = elf_getdata(scn, NULL);
404 lte->relplt_count =
405 shdr.sh_size / shdr.sh_entsize;
406 if (lte->relplt == NULL
407 || elf_getdata(scn, lte->relplt) != NULL)
408 error(EXIT_FAILURE, 0,
409 "Couldn't get .rel*.plt data from \"%s\"",
410 filename);
411 break;
412 }
413 }
414
415 if (i == lte->ehdr.e_shnum)
416 error(EXIT_FAILURE, 0,
417 "Couldn't find .rel*.plt section in \"%s\"",
418 filename);
419
420 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
421 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100422
423 if (soname_offset != 0)
424 lte->soname = lte->dynstr + soname_offset;
425
Petr Machata644d6692012-03-24 02:06:48 +0100426 if (arch_elf_init(lte) < 0) {
427 fprintf(stderr, "Backend initialization failed.\n");
428 return -1;
429 }
430
Petr Machata1974dbc2011-08-19 18:58:01 +0200431 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432}
433
Petr Machata2b46cfc2012-02-18 11:17:29 +0100434/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800435void
Juan Cespedesf1350522008-12-16 18:19:58 +0100436do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200437 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100438 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 elf_end(lte->elf);
440 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200441}
442
Petr Machata2b46cfc2012-02-18 11:17:29 +0100443struct library *
Petr Machatab120fdf2012-03-21 05:05:46 +0100444ltelf_read_library(struct Process *proc, const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100445{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100446 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100447 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100448 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200449 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100450 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800451
Petr Machata2b46cfc2012-02-18 11:17:29 +0100452 struct library *lib = malloc(sizeof(*lib));
453 char *soname = NULL;
454 if (lib == NULL) {
455 fail:
456 free(soname);
457 library_destroy(lib);
458 free(lib);
459 lib = NULL;
460 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800461 }
462
Petr Machata2b46cfc2012-02-18 11:17:29 +0100463 if (lte.soname != NULL) {
464 soname = strdup(lte.soname);
465 if (soname == NULL)
466 goto fail;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800467 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700468
Petr Machatab120fdf2012-03-21 05:05:46 +0100469 target_address_t entry = (target_address_t)lte.entry_addr;
470 if (arch_translate_address(proc, entry + lte.bias, &entry) < 0)
471 goto fail;
472
Petr Machata2b46cfc2012-02-18 11:17:29 +0100473 library_init(lib, soname, soname != NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100474 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100475 lib->entry = entry;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100476
Petr Machata2b46cfc2012-02-18 11:17:29 +0100477 size_t i;
478 for (i = 0; i < lte.relplt_count; ++i) {
479 GElf_Rel rel;
480 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100481 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100482 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100483
Petr Machata2b46cfc2012-02-18 11:17:29 +0100484 if (lte.relplt->d_type == ELF_T_REL) {
485 ret = gelf_getrel(lte.relplt, i, &rel);
486 rela.r_offset = rel.r_offset;
487 rela.r_info = rel.r_info;
488 rela.r_addend = 0;
489 } else {
490 ret = gelf_getrela(lte.relplt, i, &rela);
491 }
492
493 if (ret == NULL
494 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
495 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
496 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100497 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100498 "Couldn't get relocation from \"%s\"",
499 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100500
Petr Machatae6523e62012-03-24 04:54:06 +0100501 char const *name = lte.dynstr + sym.st_name;
502 struct library_symbol *libsym;
503 switch (arch_elf_add_plt_entry(proc, &lte, name,
504 &rela, i, &libsym)) {
505 case plt_default:
506 if (default_elf_add_plt_entry(proc, &lte, name,
507 &rela, i, &libsym) < 0)
508 case plt_fail:
509 goto fail;
510 case plt_ok:
511 if (libsym != NULL)
512 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800513 }
Joe Damatoe2a8f572010-11-08 15:47:40 -0800514 }
515
Petr Machata2b46cfc2012-02-18 11:17:29 +0100516done:
517 do_close_elf(&lte);
518 return lib;
519}
Petr Machatae84fa002012-02-07 13:43:03 +0100520
Petr Machata2b46cfc2012-02-18 11:17:29 +0100521struct library *
522ltelf_read_main_binary(struct Process *proc, const char *path)
523{
524 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
525 char *fname = pid2name(proc->pid);
Petr Machatab120fdf2012-03-21 05:05:46 +0100526 struct library *lib = ltelf_read_library(proc, fname, 0);
527 if (lib != NULL)
528 library_set_name(lib, path, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100529 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200530}