blob: 5925e9284da472513c3cda3039221b30578881cd [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,
Petr Machata1be22912012-03-27 03:11:33 +020038 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010039 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;
Petr Machata1be22912012-03-27 03:11:33 +020050 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010051
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);
Petr Machatabb790602012-03-25 01:41:59 +010057
58 /* The logic behind this conditional translation is as
59 * follows. PLT entries do not typically need custom TOC
60 * pointer, and therefore aren't redirected via OPD. POINT
61 * PLT, on the other hand, most likely contains addresses of
62 * target functions, not PLT entries themselves, and would
63 * need the OPD redirection. */
64 if (pltt == LS_TOPLT_POINT
65 && arch_translate_address(proc, taddr, &taddr) < 0) {
Petr Machatae6523e62012-03-24 04:54:06 +010066 free(libsym);
67 goto fail;
68 }
69
70 library_symbol_init(libsym, taddr, name, 1, pltt);
71 *ret = libsym;
72 return 0;
73}
74
75#ifndef ARCH_HAVE_ADD_PLT_ENTRY
76enum plt_status
77arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020078 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010079 struct library_symbol **ret)
80{
81 return plt_default;
82}
83#endif
84
Petr Machatae67635d2012-03-21 03:37:39 +010085Elf_Data *
86elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020087{
88 Elf_Data *data = elf_getdata(scn, NULL);
89 if (data == NULL || elf_getdata(scn, data) != NULL
90 || data->d_off || data->d_size != shdr->sh_size)
91 return NULL;
92 return data;
93}
94
Petr Machatae67635d2012-03-21 03:37:39 +010095static int
Petr Machataffd5aab2012-03-24 02:03:33 +010096elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
97 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
98 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020099{
100 int i;
101 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
102 Elf_Scn *scn;
103 GElf_Shdr shdr;
104
105 scn = elf_getscn(lte->elf, i);
106 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
107 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100108 return -1;
109 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100110 if (predicate(scn, &shdr, data)) {
111 *tgt_sec = scn;
112 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200113 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100114 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200115 }
Petr Machatae67635d2012-03-21 03:37:39 +0100116 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100117
118}
119
120static int
121inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
122{
123 GElf_Addr addr = *(GElf_Addr *)data;
124 return addr >= shdr->sh_addr
125 && addr < shdr->sh_addr + shdr->sh_size;
126}
127
128int
129elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
130 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
131{
132 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
133 &inside_p, &addr);
134}
135
136static int
137type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
138{
139 GElf_Word type = *(GElf_Word *)data;
140 return shdr->sh_type == type;
141}
142
143int
144elf_get_section_type(struct ltelf *lte, GElf_Word type,
145 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
146{
147 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
148 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100149}
150
151static int
152need_data(Elf_Data *data, size_t offset, size_t size)
153{
154 assert(data != NULL);
155 if (data->d_size < size || offset > data->d_size - size) {
156 debug(1, "Not enough data to read %zd-byte value"
157 " at offset %zd.", size, offset);
158 return -1;
159 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200160 return 0;
161}
162
Petr Machatae67635d2012-03-21 03:37:39 +0100163#define DEF_READER(NAME, SIZE) \
164 int \
165 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
166 { \
167 if (!need_data(data, offset, SIZE / 8) < 0) \
168 return -1; \
169 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200170 if (data->d_buf == NULL) /* NODATA section */ { \
171 *retp = 0; \
172 return 0; \
173 } \
174 \
Petr Machatae67635d2012-03-21 03:37:39 +0100175 union { \
176 uint##SIZE##_t dst; \
177 char buf[0]; \
178 } u; \
179 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
180 *retp = u.dst; \
181 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200182 }
183
Petr Machatae67635d2012-03-21 03:37:39 +0100184DEF_READER(elf_read_u16, 16)
185DEF_READER(elf_read_u32, 32)
186DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200187
Petr Machatae67635d2012-03-21 03:37:39 +0100188#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200189
Petr Machata1974dbc2011-08-19 18:58:01 +0200190int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200191open_elf(struct ltelf *lte, const char *filename)
192{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 lte->fd = open(filename, O_RDONLY);
194 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200195 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200196
Petr Machata02bd9ec2011-09-21 17:38:59 +0200197 elf_version(EV_CURRENT);
198
Juan Cespedesd914a202004-11-10 00:15:33 +0100199#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200201#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100202 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200203#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200204
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
206 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200207
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
209 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
210 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200211
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
213 error(EXIT_FAILURE, 0,
214 "\"%s\" is not an ELF executable nor shared library",
215 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200216
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
218 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100219#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
221 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100222#endif
223#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
225 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100226#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 )
228 error(EXIT_FAILURE, 0,
229 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100230
Petr Machata02bd9ec2011-09-21 17:38:59 +0200231 return 0;
232}
233
Petr Machatae67635d2012-03-21 03:37:39 +0100234static int
235do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200237 int i;
238 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100239 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200240
241 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
242 debug(1, "Reading ELF from %s...", filename);
243
244 if (open_elf(lte, filename) < 0)
245 return -1;
246
Petr Machatab120fdf2012-03-21 05:05:46 +0100247 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100248 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249 GElf_Phdr phdr;
250 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
251 if (phdr.p_type == PT_LOAD) {
Petr Machatab120fdf2012-03-21 05:05:46 +0100252 lte->base_addr = phdr.p_vaddr - bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100253 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100254 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
255 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 break;
257 }
258 }
259 }
260
Petr Machatab120fdf2012-03-21 05:05:46 +0100261 if (lte->base_addr == 0) {
262 fprintf(stderr, "Couldn't determine base address of %s\n",
263 filename);
264 return -1;
265 }
266
267 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100268 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100270 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
271 Elf_Scn *scn;
272 GElf_Shdr shdr;
273 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100274
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 scn = elf_getscn(lte->elf, i);
276 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
277 error(EXIT_FAILURE, 0,
278 "Couldn't get section header from \"%s\"",
279 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100280
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100281 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
282 if (name == NULL)
283 error(EXIT_FAILURE, 0,
284 "Couldn't get section header from \"%s\"",
285 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100286
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 if (shdr.sh_type == SHT_SYMTAB) {
288 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100289
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100290 lte->symtab = elf_getdata(scn, NULL);
291 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
292 if ((lte->symtab == NULL
293 || elf_getdata(scn, lte->symtab) != NULL)
294 && opt_x != NULL)
295 error(EXIT_FAILURE, 0,
296 "Couldn't get .symtab data from \"%s\"",
297 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100298
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 scn = elf_getscn(lte->elf, shdr.sh_link);
300 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
301 error(EXIT_FAILURE, 0,
302 "Couldn't get section header from \"%s\"",
303 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100304
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100305 data = elf_getdata(scn, NULL);
306 if (data == NULL || elf_getdata(scn, data) != NULL
307 || shdr.sh_size != data->d_size || data->d_off)
308 error(EXIT_FAILURE, 0,
309 "Couldn't get .strtab data from \"%s\"",
310 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100311
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 lte->strtab = data->d_buf;
313 } else if (shdr.sh_type == SHT_DYNSYM) {
314 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100315
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 lte->dynsym = elf_getdata(scn, NULL);
317 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
318 if (lte->dynsym == NULL
319 || elf_getdata(scn, lte->dynsym) != NULL)
320 error(EXIT_FAILURE, 0,
321 "Couldn't get .dynsym data from \"%s\"",
322 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100323
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 scn = elf_getscn(lte->elf, shdr.sh_link);
325 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
326 error(EXIT_FAILURE, 0,
327 "Couldn't get section header from \"%s\"",
328 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100329
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 data = elf_getdata(scn, NULL);
331 if (data == NULL || elf_getdata(scn, data) != NULL
332 || shdr.sh_size != data->d_size || data->d_off)
333 error(EXIT_FAILURE, 0,
334 "Couldn't get .dynstr data from \"%s\"",
335 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100336
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100337 lte->dynstr = data->d_buf;
338 } else if (shdr.sh_type == SHT_DYNAMIC) {
339 Elf_Data *data;
340 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100341
Joe Damato87f4f582010-11-08 15:47:36 -0800342 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
344 extern void *dyn_addr;
345 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800346 lte->dyn_sz = shdr.sh_size;
347
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 data = elf_getdata(scn, NULL);
349 if (data == NULL || elf_getdata(scn, data) != NULL)
350 error(EXIT_FAILURE, 0,
351 "Couldn't get .dynamic data from \"%s\"",
352 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100353
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
355 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100356
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 if (gelf_getdyn(data, j, &dyn) == NULL)
358 error(EXIT_FAILURE, 0,
359 "Couldn't get .dynamic data from \"%s\"",
360 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 if (dyn.d_tag == DT_JMPREL)
362 relplt_addr = dyn.d_un.d_ptr;
363 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100364 lte->relplt_size = dyn.d_un.d_val;
365 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100366 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 } else if (shdr.sh_type == SHT_PROGBITS
369 || shdr.sh_type == SHT_NOBITS) {
370 if (strcmp(name, ".plt") == 0) {
371 lte->plt_addr = shdr.sh_addr;
372 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100373 lte->plt_data = elf_loaddata(scn, &shdr);
374 if (lte->plt_data == NULL)
375 fprintf(stderr,
376 "Can't load .plt data\n");
377 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200378 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100379 }
380#ifdef ARCH_SUPPORTS_OPD
381 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200382 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100383 lte->opd_size = shdr.sh_size;
384 lte->opd = elf_rawdata(scn, NULL);
385 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100386#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100387 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100388 }
389
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 if (lte->dynsym == NULL || lte->dynstr == NULL)
391 error(EXIT_FAILURE, 0,
392 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100393
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100394 if (!relplt_addr || !lte->plt_addr) {
395 debug(1, "%s has no PLT relocations", filename);
396 lte->relplt = NULL;
397 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100398 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200399 debug(1, "%s has unknown PLT size", filename);
400 lte->relplt = NULL;
401 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100402 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200403
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100404 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
405 Elf_Scn *scn;
406 GElf_Shdr shdr;
407
408 scn = elf_getscn(lte->elf, i);
409 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
410 error(EXIT_FAILURE, 0,
411 "Couldn't get section header from \"%s\"",
412 filename);
413 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100414 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100415 lte->relplt = elf_getdata(scn, NULL);
416 lte->relplt_count =
417 shdr.sh_size / shdr.sh_entsize;
418 if (lte->relplt == NULL
419 || elf_getdata(scn, lte->relplt) != NULL)
420 error(EXIT_FAILURE, 0,
421 "Couldn't get .rel*.plt data from \"%s\"",
422 filename);
423 break;
424 }
425 }
426
427 if (i == lte->ehdr.e_shnum)
428 error(EXIT_FAILURE, 0,
429 "Couldn't find .rel*.plt section in \"%s\"",
430 filename);
431
432 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
433 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100434
435 if (soname_offset != 0)
436 lte->soname = lte->dynstr + soname_offset;
437
Petr Machata644d6692012-03-24 02:06:48 +0100438 if (arch_elf_init(lte) < 0) {
439 fprintf(stderr, "Backend initialization failed.\n");
440 return -1;
441 }
442
Petr Machata1974dbc2011-08-19 18:58:01 +0200443 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100444}
445
Petr Machata2b46cfc2012-02-18 11:17:29 +0100446/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800447void
Juan Cespedesf1350522008-12-16 18:19:58 +0100448do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200449 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100450 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100451 elf_end(lte->elf);
452 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200453}
454
Petr Machata2b46cfc2012-02-18 11:17:29 +0100455struct library *
Petr Machatab120fdf2012-03-21 05:05:46 +0100456ltelf_read_library(struct Process *proc, const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100457{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100458 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100459 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100460 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200461 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100462 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800463
Petr Machata2b46cfc2012-02-18 11:17:29 +0100464 struct library *lib = malloc(sizeof(*lib));
Petr Machata5e463e02012-02-20 13:06:24 +0100465 char *libname = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100466 if (lib == NULL) {
467 fail:
Petr Machata5e463e02012-02-20 13:06:24 +0100468 free(libname);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100469 library_destroy(lib);
470 free(lib);
471 lib = NULL;
472 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800473 }
474
Petr Machata5e463e02012-02-20 13:06:24 +0100475 if (lte.soname != NULL)
476 libname = strdup(lte.soname);
477 else
478 libname = strdup(filename);
479 if (libname == NULL)
480 goto fail;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700481
Petr Machatab120fdf2012-03-21 05:05:46 +0100482 target_address_t entry = (target_address_t)lte.entry_addr;
483 if (arch_translate_address(proc, entry + lte.bias, &entry) < 0)
484 goto fail;
485
Petr Machata5e463e02012-02-20 13:06:24 +0100486 library_init(lib, libname, 1);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100487 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100488 lib->entry = entry;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100489
Petr Machata2b46cfc2012-02-18 11:17:29 +0100490 size_t i;
491 for (i = 0; i < lte.relplt_count; ++i) {
492 GElf_Rel rel;
493 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100494 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100495 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100496
Petr Machata2b46cfc2012-02-18 11:17:29 +0100497 if (lte.relplt->d_type == ELF_T_REL) {
498 ret = gelf_getrel(lte.relplt, i, &rel);
499 rela.r_offset = rel.r_offset;
500 rela.r_info = rel.r_info;
501 rela.r_addend = 0;
502 } else {
503 ret = gelf_getrela(lte.relplt, i, &rela);
504 }
505
506 if (ret == NULL
507 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
508 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
509 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100511 "Couldn't get relocation from \"%s\"",
512 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100513
Petr Machatae6523e62012-03-24 04:54:06 +0100514 char const *name = lte.dynstr + sym.st_name;
515 struct library_symbol *libsym;
516 switch (arch_elf_add_plt_entry(proc, &lte, name,
517 &rela, i, &libsym)) {
518 case plt_default:
519 if (default_elf_add_plt_entry(proc, &lte, name,
520 &rela, i, &libsym) < 0)
521 case plt_fail:
522 goto fail;
523 case plt_ok:
524 if (libsym != NULL)
525 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800526 }
Joe Damatoe2a8f572010-11-08 15:47:40 -0800527 }
528
Petr Machata2b46cfc2012-02-18 11:17:29 +0100529done:
530 do_close_elf(&lte);
531 return lib;
532}
Petr Machatae84fa002012-02-07 13:43:03 +0100533
Petr Machata2b46cfc2012-02-18 11:17:29 +0100534struct library *
535ltelf_read_main_binary(struct Process *proc, const char *path)
536{
537 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
538 char *fname = pid2name(proc->pid);
Petr Machatab120fdf2012-03-21 05:05:46 +0100539 struct library *lib = ltelf_read_library(proc, fname, 0);
540 if (lib != NULL)
541 library_set_name(lib, path, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100542 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200543}