blob: 3be2fc72aa0f11305b23ee7d0866ac0afe30d37a [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
36Elf_Data *
37elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020038{
39 Elf_Data *data = elf_getdata(scn, NULL);
40 if (data == NULL || elf_getdata(scn, data) != NULL
41 || data->d_off || data->d_size != shdr->sh_size)
42 return NULL;
43 return data;
44}
45
Petr Machatae67635d2012-03-21 03:37:39 +010046static int
Petr Machataffd5aab2012-03-24 02:03:33 +010047elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
48 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
49 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020050{
51 int i;
52 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
53 Elf_Scn *scn;
54 GElf_Shdr shdr;
55
56 scn = elf_getscn(lte->elf, i);
57 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
58 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +010059 return -1;
60 }
Petr Machataffd5aab2012-03-24 02:03:33 +010061 if (predicate(scn, &shdr, data)) {
62 *tgt_sec = scn;
63 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +020064 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +010065 }
Petr Machatafe1c1712010-10-27 16:57:34 +020066 }
Petr Machatae67635d2012-03-21 03:37:39 +010067 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +010068
69}
70
71static int
72inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
73{
74 GElf_Addr addr = *(GElf_Addr *)data;
75 return addr >= shdr->sh_addr
76 && addr < shdr->sh_addr + shdr->sh_size;
77}
78
79int
80elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
81 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
82{
83 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
84 &inside_p, &addr);
85}
86
87static int
88type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
89{
90 GElf_Word type = *(GElf_Word *)data;
91 return shdr->sh_type == type;
92}
93
94int
95elf_get_section_type(struct ltelf *lte, GElf_Word type,
96 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
97{
98 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
99 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100100}
101
102static int
103need_data(Elf_Data *data, size_t offset, size_t size)
104{
105 assert(data != NULL);
106 if (data->d_size < size || offset > data->d_size - size) {
107 debug(1, "Not enough data to read %zd-byte value"
108 " at offset %zd.", size, offset);
109 return -1;
110 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200111 return 0;
112}
113
Petr Machatae67635d2012-03-21 03:37:39 +0100114#define DEF_READER(NAME, SIZE) \
115 int \
116 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
117 { \
118 if (!need_data(data, offset, SIZE / 8) < 0) \
119 return -1; \
120 \
121 union { \
122 uint##SIZE##_t dst; \
123 char buf[0]; \
124 } u; \
125 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
126 *retp = u.dst; \
127 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200128 }
129
Petr Machatae67635d2012-03-21 03:37:39 +0100130DEF_READER(elf_read_u16, 16)
131DEF_READER(elf_read_u32, 32)
132DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200133
Petr Machatae67635d2012-03-21 03:37:39 +0100134#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200135
Petr Machata1974dbc2011-08-19 18:58:01 +0200136int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200137open_elf(struct ltelf *lte, const char *filename)
138{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 lte->fd = open(filename, O_RDONLY);
140 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200141 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200142
Petr Machata02bd9ec2011-09-21 17:38:59 +0200143 elf_version(EV_CURRENT);
144
Juan Cespedesd914a202004-11-10 00:15:33 +0100145#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100146 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200147#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100148 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200149#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200150
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100151 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
152 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200153
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
155 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
156 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200157
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
159 error(EXIT_FAILURE, 0,
160 "\"%s\" is not an ELF executable nor shared library",
161 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200162
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100163 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
164 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100165#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100166 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
167 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100168#endif
169#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100170 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
171 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100172#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100173 )
174 error(EXIT_FAILURE, 0,
175 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100176
Petr Machata02bd9ec2011-09-21 17:38:59 +0200177 return 0;
178}
179
Petr Machatae67635d2012-03-21 03:37:39 +0100180static int
181do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100182{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200183 int i;
184 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100185 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200186
187 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
188 debug(1, "Reading ELF from %s...", filename);
189
190 if (open_elf(lte, filename) < 0)
191 return -1;
192
Petr Machatab120fdf2012-03-21 05:05:46 +0100193 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100194 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100195 GElf_Phdr phdr;
196 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
197 if (phdr.p_type == PT_LOAD) {
Petr Machatab120fdf2012-03-21 05:05:46 +0100198 lte->base_addr = phdr.p_vaddr - bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100199 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100200 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
201 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100202 break;
203 }
204 }
205 }
206
Petr Machatab120fdf2012-03-21 05:05:46 +0100207 if (lte->base_addr == 0) {
208 fprintf(stderr, "Couldn't determine base address of %s\n",
209 filename);
210 return -1;
211 }
212
213 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100214 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100216 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
217 Elf_Scn *scn;
218 GElf_Shdr shdr;
219 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100220
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 scn = elf_getscn(lte->elf, i);
222 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
223 error(EXIT_FAILURE, 0,
224 "Couldn't get section header from \"%s\"",
225 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100226
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
228 if (name == 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 if (shdr.sh_type == SHT_SYMTAB) {
234 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100235
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100236 lte->symtab = elf_getdata(scn, NULL);
237 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
238 if ((lte->symtab == NULL
239 || elf_getdata(scn, lte->symtab) != NULL)
240 && opt_x != NULL)
241 error(EXIT_FAILURE, 0,
242 "Couldn't get .symtab data from \"%s\"",
243 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100244
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100245 scn = elf_getscn(lte->elf, shdr.sh_link);
246 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
247 error(EXIT_FAILURE, 0,
248 "Couldn't get section header from \"%s\"",
249 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100250
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 data = elf_getdata(scn, NULL);
252 if (data == NULL || elf_getdata(scn, data) != NULL
253 || shdr.sh_size != data->d_size || data->d_off)
254 error(EXIT_FAILURE, 0,
255 "Couldn't get .strtab data from \"%s\"",
256 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100257
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 lte->strtab = data->d_buf;
259 } else if (shdr.sh_type == SHT_DYNSYM) {
260 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100261
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 lte->dynsym = elf_getdata(scn, NULL);
263 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
264 if (lte->dynsym == NULL
265 || elf_getdata(scn, lte->dynsym) != NULL)
266 error(EXIT_FAILURE, 0,
267 "Couldn't get .dynsym data from \"%s\"",
268 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100269
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100270 scn = elf_getscn(lte->elf, shdr.sh_link);
271 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
272 error(EXIT_FAILURE, 0,
273 "Couldn't get section header from \"%s\"",
274 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100275
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 data = elf_getdata(scn, NULL);
277 if (data == NULL || elf_getdata(scn, data) != NULL
278 || shdr.sh_size != data->d_size || data->d_off)
279 error(EXIT_FAILURE, 0,
280 "Couldn't get .dynstr data from \"%s\"",
281 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100282
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 lte->dynstr = data->d_buf;
284 } else if (shdr.sh_type == SHT_DYNAMIC) {
285 Elf_Data *data;
286 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100287
Joe Damato87f4f582010-11-08 15:47:36 -0800288 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
290 extern void *dyn_addr;
291 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800292 lte->dyn_sz = shdr.sh_size;
293
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 data = elf_getdata(scn, NULL);
295 if (data == NULL || elf_getdata(scn, data) != NULL)
296 error(EXIT_FAILURE, 0,
297 "Couldn't get .dynamic data from \"%s\"",
298 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100299
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100300 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
301 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100302
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100303 if (gelf_getdyn(data, j, &dyn) == NULL)
304 error(EXIT_FAILURE, 0,
305 "Couldn't get .dynamic data from \"%s\"",
306 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100307 if (dyn.d_tag == DT_JMPREL)
308 relplt_addr = dyn.d_un.d_ptr;
309 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100310 lte->relplt_size = dyn.d_un.d_val;
311 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100313 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100314 } else if (shdr.sh_type == SHT_PROGBITS
315 || shdr.sh_type == SHT_NOBITS) {
316 if (strcmp(name, ".plt") == 0) {
317 lte->plt_addr = shdr.sh_addr;
318 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100319 lte->plt_data = elf_loaddata(scn, &shdr);
320 if (lte->plt_data == NULL)
321 fprintf(stderr,
322 "Can't load .plt data\n");
323 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200324 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100325 }
326#ifdef ARCH_SUPPORTS_OPD
327 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200328 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100329 lte->opd_size = shdr.sh_size;
330 lte->opd = elf_rawdata(scn, NULL);
331 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100332#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100333 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100334 }
335
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100336 if (lte->dynsym == NULL || lte->dynstr == NULL)
337 error(EXIT_FAILURE, 0,
338 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100339
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100340 if (!relplt_addr || !lte->plt_addr) {
341 debug(1, "%s has no PLT relocations", filename);
342 lte->relplt = NULL;
343 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100344 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200345 debug(1, "%s has unknown PLT size", filename);
346 lte->relplt = NULL;
347 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200349
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
351 Elf_Scn *scn;
352 GElf_Shdr shdr;
353
354 scn = elf_getscn(lte->elf, i);
355 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
356 error(EXIT_FAILURE, 0,
357 "Couldn't get section header from \"%s\"",
358 filename);
359 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100360 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 lte->relplt = elf_getdata(scn, NULL);
362 lte->relplt_count =
363 shdr.sh_size / shdr.sh_entsize;
364 if (lte->relplt == NULL
365 || elf_getdata(scn, lte->relplt) != NULL)
366 error(EXIT_FAILURE, 0,
367 "Couldn't get .rel*.plt data from \"%s\"",
368 filename);
369 break;
370 }
371 }
372
373 if (i == lte->ehdr.e_shnum)
374 error(EXIT_FAILURE, 0,
375 "Couldn't find .rel*.plt section in \"%s\"",
376 filename);
377
378 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
379 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100380
381 if (soname_offset != 0)
382 lte->soname = lte->dynstr + soname_offset;
383
Petr Machata644d6692012-03-24 02:06:48 +0100384 if (arch_elf_init(lte) < 0) {
385 fprintf(stderr, "Backend initialization failed.\n");
386 return -1;
387 }
388
Petr Machata1974dbc2011-08-19 18:58:01 +0200389 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390}
391
Petr Machata2b46cfc2012-02-18 11:17:29 +0100392/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800393void
Juan Cespedesf1350522008-12-16 18:19:58 +0100394do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200395 debug(DEBUG_FUNCTION, "do_close_elf()");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100396 elf_end(lte->elf);
397 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200398}
399
Petr Machata2b46cfc2012-02-18 11:17:29 +0100400struct library *
Petr Machatab120fdf2012-03-21 05:05:46 +0100401ltelf_read_library(struct Process *proc, const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100402{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100403 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100404 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100405 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200406 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100407 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800408
Petr Machata2b46cfc2012-02-18 11:17:29 +0100409 struct library *lib = malloc(sizeof(*lib));
410 char *soname = NULL;
411 if (lib == NULL) {
412 fail:
413 free(soname);
414 library_destroy(lib);
415 free(lib);
416 lib = NULL;
417 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800418 }
419
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420 if (lte.soname != NULL) {
421 soname = strdup(lte.soname);
422 if (soname == NULL)
423 goto fail;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800424 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700425
Petr Machatab120fdf2012-03-21 05:05:46 +0100426 target_address_t entry = (target_address_t)lte.entry_addr;
427 if (arch_translate_address(proc, entry + lte.bias, &entry) < 0)
428 goto fail;
429
Petr Machata2b46cfc2012-02-18 11:17:29 +0100430 library_init(lib, soname, soname != NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100431 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100432 lib->entry = entry;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100433
Petr Machata2b46cfc2012-02-18 11:17:29 +0100434 size_t i;
435 for (i = 0; i < lte.relplt_count; ++i) {
436 GElf_Rel rel;
437 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100439 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100440
Petr Machata2b46cfc2012-02-18 11:17:29 +0100441 if (lte.relplt->d_type == ELF_T_REL) {
442 ret = gelf_getrel(lte.relplt, i, &rel);
443 rela.r_offset = rel.r_offset;
444 rela.r_info = rel.r_info;
445 rela.r_addend = 0;
446 } else {
447 ret = gelf_getrela(lte.relplt, i, &rela);
448 }
449
450 if (ret == NULL
451 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
452 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
453 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100454 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100455 "Couldn't get relocation from \"%s\"",
456 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100457
Petr Machata2b46cfc2012-02-18 11:17:29 +0100458 /* We will destroy the ELF object at the end of the
459 * scope. We need to copy the name for our purposes.
460 * XXX consider just keeping the ELF around. */
461 char *name = strdup(lte.dynstr + sym.st_name);
462 if (name == NULL) {
463 fail2:
464 free(name);
465 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800466 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100467
Petr Machatae67635d2012-03-21 03:37:39 +0100468 enum toplt pltt = PLTS_ARE_EXECUTABLE(&lte)
469 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
470 GElf_Addr addr = arch_plt_sym_val(&lte, i, &rela);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471
472 struct library_symbol *libsym = malloc(sizeof(*libsym));
473 if (libsym == NULL)
474 goto fail2;
Petr Machatab120fdf2012-03-21 05:05:46 +0100475
476 target_address_t taddr = (target_address_t)(addr + lte.bias);
477 if (libsym == NULL
478 || arch_translate_address(proc, taddr, &taddr) < 0) {
479 free(libsym);
480 goto fail2;
481 }
482
Petr Machatabe04d6b2012-03-24 02:01:45 +0100483 library_symbol_init(libsym, lib, taddr, name, 1, pltt);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100484 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800485 }
486
Petr Machata2b46cfc2012-02-18 11:17:29 +0100487done:
488 do_close_elf(&lte);
489 return lib;
490}
Petr Machatae84fa002012-02-07 13:43:03 +0100491
Petr Machata2b46cfc2012-02-18 11:17:29 +0100492struct library *
493ltelf_read_main_binary(struct Process *proc, const char *path)
494{
495 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
496 char *fname = pid2name(proc->pid);
Petr Machatab120fdf2012-03-21 05:05:46 +0100497 struct library *lib = ltelf_read_library(proc, fname, 0);
498 if (lib != NULL)
499 library_set_name(lib, path, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100500 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200501}