blob: b20ea7cedf7859e497a6a43fb8ebce3bb7d81717 [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}
Petr Machatac67a6e62012-03-28 02:39:49 +020034
35void
36arch_elf_destroy(struct ltelf *lte)
37{
38}
Petr Machatae67635d2012-03-21 03:37:39 +010039#endif
40
Petr Machatae6523e62012-03-24 04:54:06 +010041int
42default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020043 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010044 struct library_symbol **ret)
45{
46 char *name = strdup(a_name);
47 if (name == NULL) {
48 fail:
49 free(name);
50 return -1;
51 }
52
53 enum toplt pltt = PLTS_ARE_EXECUTABLE(lte)
54 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
Petr Machata1be22912012-03-27 03:11:33 +020055 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010056
57 struct library_symbol *libsym = malloc(sizeof(*libsym));
58 if (libsym == NULL)
59 goto fail;
60
61 target_address_t taddr = (target_address_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010062
63 /* The logic behind this conditional translation is as
64 * follows. PLT entries do not typically need custom TOC
65 * pointer, and therefore aren't redirected via OPD. POINT
66 * PLT, on the other hand, most likely contains addresses of
67 * target functions, not PLT entries themselves, and would
68 * need the OPD redirection. */
69 if (pltt == LS_TOPLT_POINT
70 && arch_translate_address(proc, taddr, &taddr) < 0) {
Petr Machatae6523e62012-03-24 04:54:06 +010071 free(libsym);
72 goto fail;
73 }
74
75 library_symbol_init(libsym, taddr, name, 1, pltt);
76 *ret = libsym;
77 return 0;
78}
79
80#ifndef ARCH_HAVE_ADD_PLT_ENTRY
81enum plt_status
82arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020083 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010084 struct library_symbol **ret)
85{
86 return plt_default;
87}
88#endif
89
Petr Machatae67635d2012-03-21 03:37:39 +010090Elf_Data *
91elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020092{
93 Elf_Data *data = elf_getdata(scn, NULL);
94 if (data == NULL || elf_getdata(scn, data) != NULL
95 || data->d_off || data->d_size != shdr->sh_size)
96 return NULL;
97 return data;
98}
99
Petr Machatae67635d2012-03-21 03:37:39 +0100100static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100101elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
102 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
103 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200104{
105 int i;
106 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
107 Elf_Scn *scn;
108 GElf_Shdr shdr;
109
110 scn = elf_getscn(lte->elf, i);
111 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
112 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100113 return -1;
114 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100115 if (predicate(scn, &shdr, data)) {
116 *tgt_sec = scn;
117 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200118 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100119 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200120 }
Petr Machatae67635d2012-03-21 03:37:39 +0100121 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100122
123}
124
125static int
126inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
127{
128 GElf_Addr addr = *(GElf_Addr *)data;
129 return addr >= shdr->sh_addr
130 && addr < shdr->sh_addr + shdr->sh_size;
131}
132
133int
134elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
135 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
136{
137 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
138 &inside_p, &addr);
139}
140
141static int
142type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
143{
144 GElf_Word type = *(GElf_Word *)data;
145 return shdr->sh_type == type;
146}
147
148int
149elf_get_section_type(struct ltelf *lte, GElf_Word type,
150 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
151{
152 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
153 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100154}
155
156static int
157need_data(Elf_Data *data, size_t offset, size_t size)
158{
159 assert(data != NULL);
160 if (data->d_size < size || offset > data->d_size - size) {
161 debug(1, "Not enough data to read %zd-byte value"
162 " at offset %zd.", size, offset);
163 return -1;
164 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200165 return 0;
166}
167
Petr Machatae67635d2012-03-21 03:37:39 +0100168#define DEF_READER(NAME, SIZE) \
169 int \
170 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
171 { \
172 if (!need_data(data, offset, SIZE / 8) < 0) \
173 return -1; \
174 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200175 if (data->d_buf == NULL) /* NODATA section */ { \
176 *retp = 0; \
177 return 0; \
178 } \
179 \
Petr Machatae67635d2012-03-21 03:37:39 +0100180 union { \
181 uint##SIZE##_t dst; \
182 char buf[0]; \
183 } u; \
184 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
185 *retp = u.dst; \
186 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200187 }
188
Petr Machatae67635d2012-03-21 03:37:39 +0100189DEF_READER(elf_read_u16, 16)
190DEF_READER(elf_read_u32, 32)
191DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200192
Petr Machatae67635d2012-03-21 03:37:39 +0100193#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200194
Petr Machata1974dbc2011-08-19 18:58:01 +0200195int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200196open_elf(struct ltelf *lte, const char *filename)
197{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 lte->fd = open(filename, O_RDONLY);
199 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200200 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200201
Petr Machata02bd9ec2011-09-21 17:38:59 +0200202 elf_version(EV_CURRENT);
203
Juan Cespedesd914a202004-11-10 00:15:33 +0100204#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200206#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200208#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200209
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100210 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
211 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200212
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
214 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
215 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200216
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
218 error(EXIT_FAILURE, 0,
219 "\"%s\" is not an ELF executable nor shared library",
220 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200221
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100222 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
223 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100224#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100225 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
226 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100227#endif
228#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
230 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100231#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 )
233 error(EXIT_FAILURE, 0,
234 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100235
Petr Machata02bd9ec2011-09-21 17:38:59 +0200236 return 0;
237}
238
Petr Machatae67635d2012-03-21 03:37:39 +0100239static int
240do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200242 int i;
243 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200245
246 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
247 debug(1, "Reading ELF from %s...", filename);
248
249 if (open_elf(lte, filename) < 0)
250 return -1;
251
Petr Machatab120fdf2012-03-21 05:05:46 +0100252 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100253 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254 GElf_Phdr phdr;
255 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
256 if (phdr.p_type == PT_LOAD) {
Petr Machatab120fdf2012-03-21 05:05:46 +0100257 lte->base_addr = phdr.p_vaddr - bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100258 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100259 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
260 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100261 break;
262 }
263 }
264 }
265
Petr Machatab120fdf2012-03-21 05:05:46 +0100266 if (lte->base_addr == 0) {
267 fprintf(stderr, "Couldn't determine base address of %s\n",
268 filename);
269 return -1;
270 }
271
272 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100273 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100274
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
276 Elf_Scn *scn;
277 GElf_Shdr shdr;
278 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100279
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100280 scn = elf_getscn(lte->elf, i);
281 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
282 error(EXIT_FAILURE, 0,
283 "Couldn't get section header from \"%s\"",
284 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100285
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
287 if (name == NULL)
288 error(EXIT_FAILURE, 0,
289 "Couldn't get section header from \"%s\"",
290 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100291
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100292 if (shdr.sh_type == SHT_SYMTAB) {
293 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100294
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 lte->symtab = elf_getdata(scn, NULL);
296 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
297 if ((lte->symtab == NULL
298 || elf_getdata(scn, lte->symtab) != NULL)
299 && opt_x != NULL)
300 error(EXIT_FAILURE, 0,
301 "Couldn't get .symtab data from \"%s\"",
302 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100303
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100304 scn = elf_getscn(lte->elf, shdr.sh_link);
305 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
306 error(EXIT_FAILURE, 0,
307 "Couldn't get section header from \"%s\"",
308 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100309
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100310 data = elf_getdata(scn, NULL);
311 if (data == NULL || elf_getdata(scn, data) != NULL
312 || shdr.sh_size != data->d_size || data->d_off)
313 error(EXIT_FAILURE, 0,
314 "Couldn't get .strtab data from \"%s\"",
315 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100316
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100317 lte->strtab = data->d_buf;
318 } else if (shdr.sh_type == SHT_DYNSYM) {
319 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100320
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100321 lte->dynsym = elf_getdata(scn, NULL);
322 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
323 if (lte->dynsym == NULL
324 || elf_getdata(scn, lte->dynsym) != NULL)
325 error(EXIT_FAILURE, 0,
326 "Couldn't get .dynsym data from \"%s\"",
327 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100328
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100329 scn = elf_getscn(lte->elf, shdr.sh_link);
330 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
331 error(EXIT_FAILURE, 0,
332 "Couldn't get section header from \"%s\"",
333 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100334
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 data = elf_getdata(scn, NULL);
336 if (data == NULL || elf_getdata(scn, data) != NULL
337 || shdr.sh_size != data->d_size || data->d_off)
338 error(EXIT_FAILURE, 0,
339 "Couldn't get .dynstr data from \"%s\"",
340 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100341
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 lte->dynstr = data->d_buf;
343 } else if (shdr.sh_type == SHT_DYNAMIC) {
344 Elf_Data *data;
345 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100346
Joe Damato87f4f582010-11-08 15:47:36 -0800347 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100348 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
Joe Damato87f4f582010-11-08 15:47:36 -0800349 lte->dyn_sz = shdr.sh_size;
350
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 data = elf_getdata(scn, NULL);
352 if (data == NULL || elf_getdata(scn, data) != NULL)
353 error(EXIT_FAILURE, 0,
354 "Couldn't get .dynamic data from \"%s\"",
355 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100356
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
358 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100359
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100360 if (gelf_getdyn(data, j, &dyn) == NULL)
361 error(EXIT_FAILURE, 0,
362 "Couldn't get .dynamic data from \"%s\"",
363 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 if (dyn.d_tag == DT_JMPREL)
365 relplt_addr = dyn.d_un.d_ptr;
366 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100367 lte->relplt_size = dyn.d_un.d_val;
368 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100369 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 } else if (shdr.sh_type == SHT_PROGBITS
372 || shdr.sh_type == SHT_NOBITS) {
373 if (strcmp(name, ".plt") == 0) {
374 lte->plt_addr = shdr.sh_addr;
375 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100376 lte->plt_data = elf_loaddata(scn, &shdr);
377 if (lte->plt_data == NULL)
378 fprintf(stderr,
379 "Can't load .plt data\n");
380 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200381 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100382 }
383#ifdef ARCH_SUPPORTS_OPD
384 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200385 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386 lte->opd_size = shdr.sh_size;
387 lte->opd = elf_rawdata(scn, NULL);
388 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100389#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100391 }
392
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100393 if (lte->dynsym == NULL || lte->dynstr == NULL)
394 error(EXIT_FAILURE, 0,
395 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100396
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100397 if (!relplt_addr || !lte->plt_addr) {
398 debug(1, "%s has no PLT relocations", filename);
399 lte->relplt = NULL;
400 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100401 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200402 debug(1, "%s has unknown PLT size", filename);
403 lte->relplt = NULL;
404 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200406
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100407 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
408 Elf_Scn *scn;
409 GElf_Shdr shdr;
410
411 scn = elf_getscn(lte->elf, i);
412 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
413 error(EXIT_FAILURE, 0,
414 "Couldn't get section header from \"%s\"",
415 filename);
416 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100417 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100418 lte->relplt = elf_getdata(scn, NULL);
419 lte->relplt_count =
420 shdr.sh_size / shdr.sh_entsize;
421 if (lte->relplt == NULL
422 || elf_getdata(scn, lte->relplt) != NULL)
423 error(EXIT_FAILURE, 0,
424 "Couldn't get .rel*.plt data from \"%s\"",
425 filename);
426 break;
427 }
428 }
429
430 if (i == lte->ehdr.e_shnum)
431 error(EXIT_FAILURE, 0,
432 "Couldn't find .rel*.plt section in \"%s\"",
433 filename);
434
435 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
436 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100437
438 if (soname_offset != 0)
439 lte->soname = lte->dynstr + soname_offset;
440
Petr Machata644d6692012-03-24 02:06:48 +0100441 if (arch_elf_init(lte) < 0) {
442 fprintf(stderr, "Backend initialization failed.\n");
443 return -1;
444 }
445
Petr Machata1974dbc2011-08-19 18:58:01 +0200446 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100447}
448
Petr Machata2b46cfc2012-02-18 11:17:29 +0100449/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800450void
Juan Cespedesf1350522008-12-16 18:19:58 +0100451do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200452 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100453 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100454 elf_end(lte->elf);
455 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200456}
457
Petr Machata2b46cfc2012-02-18 11:17:29 +0100458struct library *
Petr Machatab120fdf2012-03-21 05:05:46 +0100459ltelf_read_library(struct Process *proc, const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100460{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100461 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100462 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100463 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200464 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100465 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800466
Petr Machata2b46cfc2012-02-18 11:17:29 +0100467 struct library *lib = malloc(sizeof(*lib));
Petr Machata5e463e02012-02-20 13:06:24 +0100468 char *libname = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100469 if (lib == NULL) {
470 fail:
Petr Machata5e463e02012-02-20 13:06:24 +0100471 free(libname);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100472 library_destroy(lib);
473 free(lib);
474 lib = NULL;
475 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800476 }
477
Petr Machata5e463e02012-02-20 13:06:24 +0100478 if (lte.soname != NULL)
479 libname = strdup(lte.soname);
480 else
481 libname = strdup(filename);
482 if (libname == NULL)
483 goto fail;
Joe Damatofa2aefc2010-10-30 19:56:50 -0700484
Petr Machatab120fdf2012-03-21 05:05:46 +0100485 target_address_t entry = (target_address_t)lte.entry_addr;
486 if (arch_translate_address(proc, entry + lte.bias, &entry) < 0)
487 goto fail;
488
Petr Machata5e463e02012-02-20 13:06:24 +0100489 library_init(lib, libname, 1);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100490 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100491 lib->entry = entry;
Petr Machata52dbfb12012-03-29 16:38:26 +0200492 lib->dyn_addr = (target_address_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100493
Petr Machata2b46cfc2012-02-18 11:17:29 +0100494 size_t i;
495 for (i = 0; i < lte.relplt_count; ++i) {
496 GElf_Rel rel;
497 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100498 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100499 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100500
Petr Machata2b46cfc2012-02-18 11:17:29 +0100501 if (lte.relplt->d_type == ELF_T_REL) {
502 ret = gelf_getrel(lte.relplt, i, &rel);
503 rela.r_offset = rel.r_offset;
504 rela.r_info = rel.r_info;
505 rela.r_addend = 0;
506 } else {
507 ret = gelf_getrela(lte.relplt, i, &rela);
508 }
509
510 if (ret == NULL
511 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
512 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
513 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100514 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100515 "Couldn't get relocation from \"%s\"",
516 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100517
Petr Machatae6523e62012-03-24 04:54:06 +0100518 char const *name = lte.dynstr + sym.st_name;
519 struct library_symbol *libsym;
520 switch (arch_elf_add_plt_entry(proc, &lte, name,
521 &rela, i, &libsym)) {
522 case plt_default:
523 if (default_elf_add_plt_entry(proc, &lte, name,
524 &rela, i, &libsym) < 0)
525 case plt_fail:
526 goto fail;
527 case plt_ok:
528 if (libsym != NULL)
529 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800530 }
Joe Damatoe2a8f572010-11-08 15:47:40 -0800531 }
532
Petr Machata2b46cfc2012-02-18 11:17:29 +0100533done:
534 do_close_elf(&lte);
535 return lib;
536}
Petr Machatae84fa002012-02-07 13:43:03 +0100537
Petr Machata2b46cfc2012-02-18 11:17:29 +0100538struct library *
539ltelf_read_main_binary(struct Process *proc, const char *path)
540{
541 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
542 char *fname = pid2name(proc->pid);
Petr Machatab120fdf2012-03-21 05:05:46 +0100543 struct library *lib = ltelf_read_library(proc, fname, 0);
544 if (lib != NULL)
545 library_set_name(lib, path, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100546 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200547}