blob: aee6c2eba7da427f2488b605a9f703c6b7e65496 [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
30arch_elf_dynamic_tag(struct ltelf *lte, GElf_Dyn dyn)
31{
32 return 0;
33}
34
35int
36arch_elf_init(struct ltelf *lte)
37{
38 return 0;
39}
40#endif
41
42Elf_Data *
43elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020044{
45 Elf_Data *data = elf_getdata(scn, NULL);
46 if (data == NULL || elf_getdata(scn, data) != NULL
47 || data->d_off || data->d_size != shdr->sh_size)
48 return NULL;
49 return data;
50}
51
Petr Machatae67635d2012-03-21 03:37:39 +010052static int
Petr Machataffd5aab2012-03-24 02:03:33 +010053elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
54 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
55 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020056{
57 int i;
58 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
59 Elf_Scn *scn;
60 GElf_Shdr shdr;
61
62 scn = elf_getscn(lte->elf, i);
63 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
64 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +010065 return -1;
66 }
Petr Machataffd5aab2012-03-24 02:03:33 +010067 if (predicate(scn, &shdr, data)) {
68 *tgt_sec = scn;
69 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +020070 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +010071 }
Petr Machatafe1c1712010-10-27 16:57:34 +020072 }
Petr Machatae67635d2012-03-21 03:37:39 +010073 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +010074
75}
76
77static int
78inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
79{
80 GElf_Addr addr = *(GElf_Addr *)data;
81 return addr >= shdr->sh_addr
82 && addr < shdr->sh_addr + shdr->sh_size;
83}
84
85int
86elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
87 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
88{
89 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
90 &inside_p, &addr);
91}
92
93static int
94type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
95{
96 GElf_Word type = *(GElf_Word *)data;
97 return shdr->sh_type == type;
98}
99
100int
101elf_get_section_type(struct ltelf *lte, GElf_Word type,
102 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
103{
104 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
105 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100106}
107
108static int
109need_data(Elf_Data *data, size_t offset, size_t size)
110{
111 assert(data != NULL);
112 if (data->d_size < size || offset > data->d_size - size) {
113 debug(1, "Not enough data to read %zd-byte value"
114 " at offset %zd.", size, offset);
115 return -1;
116 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200117 return 0;
118}
119
Petr Machatae67635d2012-03-21 03:37:39 +0100120#define DEF_READER(NAME, SIZE) \
121 int \
122 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
123 { \
124 if (!need_data(data, offset, SIZE / 8) < 0) \
125 return -1; \
126 \
127 union { \
128 uint##SIZE##_t dst; \
129 char buf[0]; \
130 } u; \
131 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
132 *retp = u.dst; \
133 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200134 }
135
Petr Machatae67635d2012-03-21 03:37:39 +0100136DEF_READER(elf_read_u16, 16)
137DEF_READER(elf_read_u32, 32)
138DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200139
Petr Machatae67635d2012-03-21 03:37:39 +0100140#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200141
Petr Machata1974dbc2011-08-19 18:58:01 +0200142int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200143open_elf(struct ltelf *lte, const char *filename)
144{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100145 lte->fd = open(filename, O_RDONLY);
146 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200147 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200148
Petr Machata02bd9ec2011-09-21 17:38:59 +0200149 elf_version(EV_CURRENT);
150
Juan Cespedesd914a202004-11-10 00:15:33 +0100151#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100152 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200153#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200155#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200156
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100157 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
158 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200159
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
161 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
162 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200163
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100164 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
165 error(EXIT_FAILURE, 0,
166 "\"%s\" is not an ELF executable nor shared library",
167 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200168
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100169 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
170 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100171#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
173 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100174#endif
175#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
177 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100178#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100179 )
180 error(EXIT_FAILURE, 0,
181 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100182
Petr Machata02bd9ec2011-09-21 17:38:59 +0200183 return 0;
184}
185
Petr Machatae67635d2012-03-21 03:37:39 +0100186static int
187do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100188{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200189 int i;
190 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100191 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200192
193 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
194 debug(1, "Reading ELF from %s...", filename);
195
196 if (open_elf(lte, filename) < 0)
197 return -1;
198
Petr Machatab120fdf2012-03-21 05:05:46 +0100199 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100200 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100201 GElf_Phdr phdr;
202 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
203 if (phdr.p_type == PT_LOAD) {
Petr Machatab120fdf2012-03-21 05:05:46 +0100204 lte->base_addr = phdr.p_vaddr - bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100205 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100206 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
207 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100208 break;
209 }
210 }
211 }
212
Petr Machatab120fdf2012-03-21 05:05:46 +0100213 if (lte->base_addr == 0) {
214 fprintf(stderr, "Couldn't determine base address of %s\n",
215 filename);
216 return -1;
217 }
218
219 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100220 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100221
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100222 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
223 Elf_Scn *scn;
224 GElf_Shdr shdr;
225 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100226
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 scn = elf_getscn(lte->elf, i);
228 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
229 error(EXIT_FAILURE, 0,
230 "Couldn't get section header from \"%s\"",
231 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100232
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
234 if (name == NULL)
235 error(EXIT_FAILURE, 0,
236 "Couldn't get section header from \"%s\"",
237 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100238
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 if (shdr.sh_type == SHT_SYMTAB) {
240 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100241
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100242 lte->symtab = elf_getdata(scn, NULL);
243 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
244 if ((lte->symtab == NULL
245 || elf_getdata(scn, lte->symtab) != NULL)
246 && opt_x != NULL)
247 error(EXIT_FAILURE, 0,
248 "Couldn't get .symtab 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 .strtab data from \"%s\"",
262 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100263
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100264 lte->strtab = data->d_buf;
265 } else if (shdr.sh_type == SHT_DYNSYM) {
266 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100267
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 lte->dynsym = elf_getdata(scn, NULL);
269 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
270 if (lte->dynsym == NULL
271 || elf_getdata(scn, lte->dynsym) != NULL)
272 error(EXIT_FAILURE, 0,
273 "Couldn't get .dynsym data from \"%s\"",
274 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100275
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 scn = elf_getscn(lte->elf, shdr.sh_link);
277 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
278 error(EXIT_FAILURE, 0,
279 "Couldn't get section header from \"%s\"",
280 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100281
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 data = elf_getdata(scn, NULL);
283 if (data == NULL || elf_getdata(scn, data) != NULL
284 || shdr.sh_size != data->d_size || data->d_off)
285 error(EXIT_FAILURE, 0,
286 "Couldn't get .dynstr data from \"%s\"",
287 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100288
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100289 lte->dynstr = data->d_buf;
290 } else if (shdr.sh_type == SHT_DYNAMIC) {
291 Elf_Data *data;
292 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100293
Joe Damato87f4f582010-11-08 15:47:36 -0800294 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100295 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
296 extern void *dyn_addr;
297 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800298 lte->dyn_sz = shdr.sh_size;
299
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100300 data = elf_getdata(scn, NULL);
301 if (data == NULL || elf_getdata(scn, data) != NULL)
302 error(EXIT_FAILURE, 0,
303 "Couldn't get .dynamic data from \"%s\"",
304 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100305
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
307 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100308
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 if (gelf_getdyn(data, j, &dyn) == NULL)
310 error(EXIT_FAILURE, 0,
311 "Couldn't get .dynamic data from \"%s\"",
312 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100313 if (dyn.d_tag == DT_JMPREL)
314 relplt_addr = dyn.d_un.d_ptr;
315 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100316 lte->relplt_size = dyn.d_un.d_val;
317 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 soname_offset = dyn.d_un.d_val;
Petr Machatae67635d2012-03-21 03:37:39 +0100319 else if (arch_elf_dynamic_tag(lte, dyn) < 0)
320 goto backend_fail;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100321 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100322 } else if (shdr.sh_type == SHT_PROGBITS
323 || shdr.sh_type == SHT_NOBITS) {
324 if (strcmp(name, ".plt") == 0) {
325 lte->plt_addr = shdr.sh_addr;
326 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100327 lte->plt_data = elf_loaddata(scn, &shdr);
328 if (lte->plt_data == NULL)
329 fprintf(stderr,
330 "Can't load .plt data\n");
331 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200332 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100333 }
334#ifdef ARCH_SUPPORTS_OPD
335 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200336 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100337 lte->opd_size = shdr.sh_size;
338 lte->opd = elf_rawdata(scn, NULL);
339 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100340#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100342 }
343
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 if (lte->dynsym == NULL || lte->dynstr == NULL)
345 error(EXIT_FAILURE, 0,
346 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100347
Petr Machatae67635d2012-03-21 03:37:39 +0100348 if (arch_elf_init(lte) < 0) {
349 backend_fail:
350 fprintf(stderr, "Backend initialization failed.\n");
351 return -1;
352 }
353
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 if (!relplt_addr || !lte->plt_addr) {
355 debug(1, "%s has no PLT relocations", filename);
356 lte->relplt = NULL;
357 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100358 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200359 debug(1, "%s has unknown PLT size", filename);
360 lte->relplt = NULL;
361 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200363
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
365 Elf_Scn *scn;
366 GElf_Shdr shdr;
367
368 scn = elf_getscn(lte->elf, i);
369 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
370 error(EXIT_FAILURE, 0,
371 "Couldn't get section header from \"%s\"",
372 filename);
373 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100374 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 lte->relplt = elf_getdata(scn, NULL);
376 lte->relplt_count =
377 shdr.sh_size / shdr.sh_entsize;
378 if (lte->relplt == NULL
379 || elf_getdata(scn, lte->relplt) != NULL)
380 error(EXIT_FAILURE, 0,
381 "Couldn't get .rel*.plt data from \"%s\"",
382 filename);
383 break;
384 }
385 }
386
387 if (i == lte->ehdr.e_shnum)
388 error(EXIT_FAILURE, 0,
389 "Couldn't find .rel*.plt section in \"%s\"",
390 filename);
391
392 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
393 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100394
395 if (soname_offset != 0)
396 lte->soname = lte->dynstr + soname_offset;
397
Petr Machata1974dbc2011-08-19 18:58:01 +0200398 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100399}
400
Petr Machata2b46cfc2012-02-18 11:17:29 +0100401/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800402void
Juan Cespedesf1350522008-12-16 18:19:58 +0100403do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200404 debug(DEBUG_FUNCTION, "do_close_elf()");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 elf_end(lte->elf);
406 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200407}
408
Petr Machata2b46cfc2012-02-18 11:17:29 +0100409struct library *
Petr Machatab120fdf2012-03-21 05:05:46 +0100410ltelf_read_library(struct Process *proc, const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100411{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100412 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100413 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100414 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200415 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100416 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800417
Petr Machata2b46cfc2012-02-18 11:17:29 +0100418 struct library *lib = malloc(sizeof(*lib));
419 char *soname = NULL;
420 if (lib == NULL) {
421 fail:
422 free(soname);
423 library_destroy(lib);
424 free(lib);
425 lib = NULL;
426 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800427 }
428
Petr Machata2b46cfc2012-02-18 11:17:29 +0100429 if (lte.soname != NULL) {
430 soname = strdup(lte.soname);
431 if (soname == NULL)
432 goto fail;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800433 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700434
Petr Machatab120fdf2012-03-21 05:05:46 +0100435 target_address_t entry = (target_address_t)lte.entry_addr;
436 if (arch_translate_address(proc, entry + lte.bias, &entry) < 0)
437 goto fail;
438
Petr Machata2b46cfc2012-02-18 11:17:29 +0100439 library_init(lib, soname, soname != NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100440 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100441 lib->entry = entry;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100442
Petr Machata2b46cfc2012-02-18 11:17:29 +0100443 size_t i;
444 for (i = 0; i < lte.relplt_count; ++i) {
445 GElf_Rel rel;
446 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100447 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100448 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100449
Petr Machata2b46cfc2012-02-18 11:17:29 +0100450 if (lte.relplt->d_type == ELF_T_REL) {
451 ret = gelf_getrel(lte.relplt, i, &rel);
452 rela.r_offset = rel.r_offset;
453 rela.r_info = rel.r_info;
454 rela.r_addend = 0;
455 } else {
456 ret = gelf_getrela(lte.relplt, i, &rela);
457 }
458
459 if (ret == NULL
460 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
461 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
462 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100463 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100464 "Couldn't get relocation from \"%s\"",
465 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100466
Petr Machata2b46cfc2012-02-18 11:17:29 +0100467 /* We will destroy the ELF object at the end of the
468 * scope. We need to copy the name for our purposes.
469 * XXX consider just keeping the ELF around. */
470 char *name = strdup(lte.dynstr + sym.st_name);
471 if (name == NULL) {
472 fail2:
473 free(name);
474 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800475 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100476
Petr Machatae67635d2012-03-21 03:37:39 +0100477 enum toplt pltt = PLTS_ARE_EXECUTABLE(&lte)
478 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
479 GElf_Addr addr = arch_plt_sym_val(&lte, i, &rela);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100480
481 struct library_symbol *libsym = malloc(sizeof(*libsym));
482 if (libsym == NULL)
483 goto fail2;
Petr Machatab120fdf2012-03-21 05:05:46 +0100484
485 target_address_t taddr = (target_address_t)(addr + lte.bias);
486 if (libsym == NULL
487 || arch_translate_address(proc, taddr, &taddr) < 0) {
488 free(libsym);
489 goto fail2;
490 }
491
Petr Machatabe04d6b2012-03-24 02:01:45 +0100492 library_symbol_init(libsym, lib, taddr, name, 1, pltt);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100493 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800494 }
495
Petr Machata2b46cfc2012-02-18 11:17:29 +0100496done:
497 do_close_elf(&lte);
498 return lib;
499}
Petr Machatae84fa002012-02-07 13:43:03 +0100500
Petr Machata2b46cfc2012-02-18 11:17:29 +0100501struct library *
502ltelf_read_main_binary(struct Process *proc, const char *path)
503{
504 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
505 char *fname = pid2name(proc->pid);
Petr Machatab120fdf2012-03-21 05:05:46 +0100506 struct library *lib = ltelf_read_library(proc, fname, 0);
507 if (lib != NULL)
508 library_set_name(lib, path, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100509 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200510}