blob: 5e65b4864247620c0a3fe8999179633f04fdca34 [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
53inside(GElf_Addr addr, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020054{
55 return addr >= shdr->sh_addr
56 && addr < shdr->sh_addr + shdr->sh_size;
57}
58
Petr Machatae67635d2012-03-21 03:37:39 +010059static int
60section_covers(GElf_Addr addr,
61 Elf_Scn *in_sec, GElf_Shdr *in_shdr,
62 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020063{
Petr Machata2b46cfc2012-02-18 11:17:29 +010064 if (inside(addr, in_shdr)) {
Petr Machatafe1c1712010-10-27 16:57:34 +020065 *tgt_sec = in_sec;
66 *tgt_shdr = *in_shdr;
67 return 1;
68 }
69 return 0;
70}
71
Petr Machatae67635d2012-03-21 03:37:39 +010072int
73elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
74 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020075{
76 int i;
77 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
78 Elf_Scn *scn;
79 GElf_Shdr shdr;
80
81 scn = elf_getscn(lte->elf, i);
82 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
83 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +010084 return -1;
85 }
86
87 if (section_covers(addr, scn, &shdr, tgt_sec, tgt_shdr))
Petr Machatafe1c1712010-10-27 16:57:34 +020088 return 0;
Petr Machatafe1c1712010-10-27 16:57:34 +020089 }
90
Petr Machatae67635d2012-03-21 03:37:39 +010091 return -1;
92}
93
94static int
95need_data(Elf_Data *data, size_t offset, size_t size)
96{
97 assert(data != NULL);
98 if (data->d_size < size || offset > data->d_size - size) {
99 debug(1, "Not enough data to read %zd-byte value"
100 " at offset %zd.", size, offset);
101 return -1;
102 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200103 return 0;
104}
105
Petr Machatae67635d2012-03-21 03:37:39 +0100106#define DEF_READER(NAME, SIZE) \
107 int \
108 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
109 { \
110 if (!need_data(data, offset, SIZE / 8) < 0) \
111 return -1; \
112 \
113 union { \
114 uint##SIZE##_t dst; \
115 char buf[0]; \
116 } u; \
117 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
118 *retp = u.dst; \
119 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200120 }
121
Petr Machatae67635d2012-03-21 03:37:39 +0100122DEF_READER(elf_read_u16, 16)
123DEF_READER(elf_read_u32, 32)
124DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200125
Petr Machatae67635d2012-03-21 03:37:39 +0100126#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200127
Petr Machata1974dbc2011-08-19 18:58:01 +0200128int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200129open_elf(struct ltelf *lte, const char *filename)
130{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 lte->fd = open(filename, O_RDONLY);
132 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200133 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200134
Petr Machata02bd9ec2011-09-21 17:38:59 +0200135 elf_version(EV_CURRENT);
136
Juan Cespedesd914a202004-11-10 00:15:33 +0100137#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100138 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200139#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200141#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200142
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100143 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
144 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200145
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100146 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
147 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
148 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200149
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100150 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
151 error(EXIT_FAILURE, 0,
152 "\"%s\" is not an ELF executable nor shared library",
153 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200154
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100155 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
156 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100157#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
159 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100160#endif
161#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100162 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
163 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100164#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100165 )
166 error(EXIT_FAILURE, 0,
167 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100168
Petr Machata02bd9ec2011-09-21 17:38:59 +0200169 return 0;
170}
171
Petr Machatae67635d2012-03-21 03:37:39 +0100172static int
173do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100174{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200175 int i;
176 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200178
179 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
180 debug(1, "Reading ELF from %s...", filename);
181
182 if (open_elf(lte, filename) < 0)
183 return -1;
184
Petr Machatab120fdf2012-03-21 05:05:46 +0100185 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100186 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100187 GElf_Phdr phdr;
188 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
189 if (phdr.p_type == PT_LOAD) {
Petr Machatab120fdf2012-03-21 05:05:46 +0100190 lte->base_addr = phdr.p_vaddr - bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100191 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100192 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
193 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100194 break;
195 }
196 }
197 }
198
Petr Machatab120fdf2012-03-21 05:05:46 +0100199 if (lte->base_addr == 0) {
200 fprintf(stderr, "Couldn't determine base address of %s\n",
201 filename);
202 return -1;
203 }
204
205 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100206 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100207
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
209 Elf_Scn *scn;
210 GElf_Shdr shdr;
211 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100212
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 scn = elf_getscn(lte->elf, i);
214 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
215 error(EXIT_FAILURE, 0,
216 "Couldn't get section header from \"%s\"",
217 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100218
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
220 if (name == NULL)
221 error(EXIT_FAILURE, 0,
222 "Couldn't get section header from \"%s\"",
223 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100224
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100225 if (shdr.sh_type == SHT_SYMTAB) {
226 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100227
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 lte->symtab = elf_getdata(scn, NULL);
229 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
230 if ((lte->symtab == NULL
231 || elf_getdata(scn, lte->symtab) != NULL)
232 && opt_x != NULL)
233 error(EXIT_FAILURE, 0,
234 "Couldn't get .symtab data from \"%s\"",
235 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100236
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100237 scn = elf_getscn(lte->elf, shdr.sh_link);
238 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
239 error(EXIT_FAILURE, 0,
240 "Couldn't get section header from \"%s\"",
241 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100242
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 data = elf_getdata(scn, NULL);
244 if (data == NULL || elf_getdata(scn, data) != NULL
245 || shdr.sh_size != data->d_size || data->d_off)
246 error(EXIT_FAILURE, 0,
247 "Couldn't get .strtab data from \"%s\"",
248 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100249
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 lte->strtab = data->d_buf;
251 } else if (shdr.sh_type == SHT_DYNSYM) {
252 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100253
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 lte->dynsym = elf_getdata(scn, NULL);
255 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
256 if (lte->dynsym == NULL
257 || elf_getdata(scn, lte->dynsym) != NULL)
258 error(EXIT_FAILURE, 0,
259 "Couldn't get .dynsym data from \"%s\"",
260 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100261
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 scn = elf_getscn(lte->elf, shdr.sh_link);
263 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
264 error(EXIT_FAILURE, 0,
265 "Couldn't get section header from \"%s\"",
266 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100267
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 data = elf_getdata(scn, NULL);
269 if (data == NULL || elf_getdata(scn, data) != NULL
270 || shdr.sh_size != data->d_size || data->d_off)
271 error(EXIT_FAILURE, 0,
272 "Couldn't get .dynstr data from \"%s\"",
273 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100274
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 lte->dynstr = data->d_buf;
276 } else if (shdr.sh_type == SHT_DYNAMIC) {
277 Elf_Data *data;
278 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100279
Joe Damato87f4f582010-11-08 15:47:36 -0800280 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100281 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
282 extern void *dyn_addr;
283 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800284 lte->dyn_sz = shdr.sh_size;
285
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 data = elf_getdata(scn, NULL);
287 if (data == NULL || elf_getdata(scn, data) != NULL)
288 error(EXIT_FAILURE, 0,
289 "Couldn't get .dynamic data from \"%s\"",
290 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100291
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100292 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
293 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100294
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 if (gelf_getdyn(data, j, &dyn) == NULL)
296 error(EXIT_FAILURE, 0,
297 "Couldn't get .dynamic data from \"%s\"",
298 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 if (dyn.d_tag == DT_JMPREL)
300 relplt_addr = dyn.d_un.d_ptr;
301 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100302 lte->relplt_size = dyn.d_un.d_val;
303 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100304 soname_offset = dyn.d_un.d_val;
Petr Machatae67635d2012-03-21 03:37:39 +0100305 else if (arch_elf_dynamic_tag(lte, dyn) < 0)
306 goto backend_fail;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100307 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100308 } else if (shdr.sh_type == SHT_PROGBITS
309 || shdr.sh_type == SHT_NOBITS) {
310 if (strcmp(name, ".plt") == 0) {
311 lte->plt_addr = shdr.sh_addr;
312 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100313 lte->plt_data = elf_loaddata(scn, &shdr);
314 if (lte->plt_data == NULL)
315 fprintf(stderr,
316 "Can't load .plt data\n");
317 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200318 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100319 }
320#ifdef ARCH_SUPPORTS_OPD
321 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200322 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100323 lte->opd_size = shdr.sh_size;
324 lte->opd = elf_rawdata(scn, NULL);
325 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100326#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100327 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100328 }
329
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 if (lte->dynsym == NULL || lte->dynstr == NULL)
331 error(EXIT_FAILURE, 0,
332 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100333
Petr Machatae67635d2012-03-21 03:37:39 +0100334 if (arch_elf_init(lte) < 0) {
335 backend_fail:
336 fprintf(stderr, "Backend initialization failed.\n");
337 return -1;
338 }
339
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 Machata1974dbc2011-08-19 18:58:01 +0200384 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100385}
386
Petr Machata2b46cfc2012-02-18 11:17:29 +0100387/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800388void
Juan Cespedesf1350522008-12-16 18:19:58 +0100389do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200390 debug(DEBUG_FUNCTION, "do_close_elf()");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 elf_end(lte->elf);
392 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200393}
394
Petr Machata2b46cfc2012-02-18 11:17:29 +0100395struct library *
Petr Machatab120fdf2012-03-21 05:05:46 +0100396ltelf_read_library(struct Process *proc, const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100397{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100398 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100399 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100400 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200401 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100402 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800403
Petr Machata2b46cfc2012-02-18 11:17:29 +0100404 struct library *lib = malloc(sizeof(*lib));
405 char *soname = NULL;
406 if (lib == NULL) {
407 fail:
408 free(soname);
409 library_destroy(lib);
410 free(lib);
411 lib = NULL;
412 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800413 }
414
Petr Machata2b46cfc2012-02-18 11:17:29 +0100415 if (lte.soname != NULL) {
416 soname = strdup(lte.soname);
417 if (soname == NULL)
418 goto fail;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800419 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700420
Petr Machatab120fdf2012-03-21 05:05:46 +0100421 target_address_t entry = (target_address_t)lte.entry_addr;
422 if (arch_translate_address(proc, entry + lte.bias, &entry) < 0)
423 goto fail;
424
Petr Machata2b46cfc2012-02-18 11:17:29 +0100425 library_init(lib, soname, soname != NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100427 lib->entry = entry;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100428
Petr Machata2b46cfc2012-02-18 11:17:29 +0100429 size_t i;
430 for (i = 0; i < lte.relplt_count; ++i) {
431 GElf_Rel rel;
432 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100433 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100434 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100435
Petr Machata2b46cfc2012-02-18 11:17:29 +0100436 if (lte.relplt->d_type == ELF_T_REL) {
437 ret = gelf_getrel(lte.relplt, i, &rel);
438 rela.r_offset = rel.r_offset;
439 rela.r_info = rel.r_info;
440 rela.r_addend = 0;
441 } else {
442 ret = gelf_getrela(lte.relplt, i, &rela);
443 }
444
445 if (ret == NULL
446 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
447 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
448 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100449 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100450 "Couldn't get relocation from \"%s\"",
451 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100452
Petr Machata2b46cfc2012-02-18 11:17:29 +0100453 /* We will destroy the ELF object at the end of the
454 * scope. We need to copy the name for our purposes.
455 * XXX consider just keeping the ELF around. */
456 char *name = strdup(lte.dynstr + sym.st_name);
457 if (name == NULL) {
458 fail2:
459 free(name);
460 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800461 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100462
Petr Machatae67635d2012-03-21 03:37:39 +0100463 enum toplt pltt = PLTS_ARE_EXECUTABLE(&lte)
464 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
465 GElf_Addr addr = arch_plt_sym_val(&lte, i, &rela);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100466
467 struct library_symbol *libsym = malloc(sizeof(*libsym));
468 if (libsym == NULL)
469 goto fail2;
Petr Machatab120fdf2012-03-21 05:05:46 +0100470
471 target_address_t taddr = (target_address_t)(addr + lte.bias);
472 if (libsym == NULL
473 || arch_translate_address(proc, taddr, &taddr) < 0) {
474 free(libsym);
475 goto fail2;
476 }
477
Petr Machatabe04d6b2012-03-24 02:01:45 +0100478 library_symbol_init(libsym, lib, taddr, name, 1, pltt);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100479 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800480 }
481
Petr Machata2b46cfc2012-02-18 11:17:29 +0100482done:
483 do_close_elf(&lte);
484 return lib;
485}
Petr Machatae84fa002012-02-07 13:43:03 +0100486
Petr Machata2b46cfc2012-02-18 11:17:29 +0100487struct library *
488ltelf_read_main_binary(struct Process *proc, const char *path)
489{
490 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
491 char *fname = pid2name(proc->pid);
Petr Machatab120fdf2012-03-21 05:05:46 +0100492 struct library *lib = ltelf_read_library(proc, fname, 0);
493 if (lib != NULL)
494 library_set_name(lib, path, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100495 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200496}