blob: de1b0fc49da433c3f2f1c4a535ec96cbb2fc143c [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 Machatafe1c1712010-10-27 16:57:34 +0200185 Elf_Data *plt_data = NULL;
186 GElf_Addr ppcgot = 0;
187
Petr Machata29add4f2012-02-18 16:38:05 +0100188 /* Find out the bias. For DSOs, this will be just BASE,
189 * unless the DSO is pre-linked. For ET_EXEC files, this will
190 * turn out to be 0. */
191 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100192 GElf_Phdr phdr;
193 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
194 if (phdr.p_type == PT_LOAD) {
Petr Machata11639da2012-03-02 00:10:51 +0100195 if (base == 0)
196 base = phdr.p_vaddr;
197 lte->base_addr = base;
Petr Machata29add4f2012-02-18 16:38:05 +0100198 lte->bias = lte->base_addr - phdr.p_vaddr;
199 fprintf(stderr,
200 " + vaddr=%#lx, base=%#lx, bias=%#lx\n",
201 lte->base_addr, base, lte->bias);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100202 break;
203 }
204 }
205 }
206
Petr Machata29add4f2012-02-18 16:38:05 +0100207 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100208
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
210 Elf_Scn *scn;
211 GElf_Shdr shdr;
212 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100213
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214 scn = elf_getscn(lte->elf, i);
215 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
216 error(EXIT_FAILURE, 0,
217 "Couldn't get section header from \"%s\"",
218 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100219
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
221 if (name == NULL)
222 error(EXIT_FAILURE, 0,
223 "Couldn't get section header from \"%s\"",
224 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100225
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 if (shdr.sh_type == SHT_SYMTAB) {
227 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100228
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 lte->symtab = elf_getdata(scn, NULL);
230 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
231 if ((lte->symtab == NULL
232 || elf_getdata(scn, lte->symtab) != NULL)
233 && opt_x != NULL)
234 error(EXIT_FAILURE, 0,
235 "Couldn't get .symtab data from \"%s\"",
236 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100237
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100238 scn = elf_getscn(lte->elf, shdr.sh_link);
239 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
240 error(EXIT_FAILURE, 0,
241 "Couldn't get section header from \"%s\"",
242 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100243
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100244 data = elf_getdata(scn, NULL);
245 if (data == NULL || elf_getdata(scn, data) != NULL
246 || shdr.sh_size != data->d_size || data->d_off)
247 error(EXIT_FAILURE, 0,
248 "Couldn't get .strtab data from \"%s\"",
249 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100250
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 lte->strtab = data->d_buf;
252 } else if (shdr.sh_type == SHT_DYNSYM) {
253 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100254
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 lte->dynsym = elf_getdata(scn, NULL);
256 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
257 if (lte->dynsym == NULL
258 || elf_getdata(scn, lte->dynsym) != NULL)
259 error(EXIT_FAILURE, 0,
260 "Couldn't get .dynsym data from \"%s\"",
261 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100262
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100263 scn = elf_getscn(lte->elf, shdr.sh_link);
264 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
265 error(EXIT_FAILURE, 0,
266 "Couldn't get section header from \"%s\"",
267 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100268
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 data = elf_getdata(scn, NULL);
270 if (data == NULL || elf_getdata(scn, data) != NULL
271 || shdr.sh_size != data->d_size || data->d_off)
272 error(EXIT_FAILURE, 0,
273 "Couldn't get .dynstr data from \"%s\"",
274 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100275
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 lte->dynstr = data->d_buf;
277 } else if (shdr.sh_type == SHT_DYNAMIC) {
278 Elf_Data *data;
279 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100280
Joe Damato87f4f582010-11-08 15:47:36 -0800281 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100282 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
283 extern void *dyn_addr;
284 dyn_addr = (void *)lte->dyn_addr;
Joe Damato87f4f582010-11-08 15:47:36 -0800285 lte->dyn_sz = shdr.sh_size;
286
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 data = elf_getdata(scn, NULL);
288 if (data == NULL || elf_getdata(scn, data) != NULL)
289 error(EXIT_FAILURE, 0,
290 "Couldn't get .dynamic data from \"%s\"",
291 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100292
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
294 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100295
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100296 if (gelf_getdyn(data, j, &dyn) == NULL)
297 error(EXIT_FAILURE, 0,
298 "Couldn't get .dynamic data from \"%s\"",
299 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100300 if (dyn.d_tag == DT_JMPREL)
301 relplt_addr = dyn.d_un.d_ptr;
302 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100303 lte->relplt_size = dyn.d_un.d_val;
304 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305 soname_offset = dyn.d_un.d_val;
Petr Machatae67635d2012-03-21 03:37:39 +0100306 else if (arch_elf_dynamic_tag(lte, dyn) < 0)
307 goto backend_fail;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100308 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 } else if (shdr.sh_type == SHT_PROGBITS
310 || shdr.sh_type == SHT_NOBITS) {
311 if (strcmp(name, ".plt") == 0) {
312 lte->plt_addr = shdr.sh_addr;
313 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100314 lte->plt_data = elf_loaddata(scn, &shdr);
315 if (lte->plt_data == NULL)
316 fprintf(stderr,
317 "Can't load .plt data\n");
318 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200319 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100320 }
321#ifdef ARCH_SUPPORTS_OPD
322 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200323 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 lte->opd_size = shdr.sh_size;
325 lte->opd = elf_rawdata(scn, NULL);
326 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100327#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100329 }
330
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 if (lte->dynsym == NULL || lte->dynstr == NULL)
332 error(EXIT_FAILURE, 0,
333 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100334
Petr Machatae67635d2012-03-21 03:37:39 +0100335 if (arch_elf_init(lte) < 0) {
336 backend_fail:
337 fprintf(stderr, "Backend initialization failed.\n");
338 return -1;
339 }
340
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 if (!relplt_addr || !lte->plt_addr) {
342 debug(1, "%s has no PLT relocations", filename);
343 lte->relplt = NULL;
344 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100345 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200346 debug(1, "%s has unknown PLT size", filename);
347 lte->relplt = NULL;
348 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200350
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
352 Elf_Scn *scn;
353 GElf_Shdr shdr;
354
355 scn = elf_getscn(lte->elf, i);
356 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
357 error(EXIT_FAILURE, 0,
358 "Couldn't get section header from \"%s\"",
359 filename);
360 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100361 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 lte->relplt = elf_getdata(scn, NULL);
363 lte->relplt_count =
364 shdr.sh_size / shdr.sh_entsize;
365 if (lte->relplt == NULL
366 || elf_getdata(scn, lte->relplt) != NULL)
367 error(EXIT_FAILURE, 0,
368 "Couldn't get .rel*.plt data from \"%s\"",
369 filename);
370 break;
371 }
372 }
373
374 if (i == lte->ehdr.e_shnum)
375 error(EXIT_FAILURE, 0,
376 "Couldn't find .rel*.plt section in \"%s\"",
377 filename);
378
379 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
380 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100381
382 if (soname_offset != 0)
383 lte->soname = lte->dynstr + soname_offset;
384
Petr Machata1974dbc2011-08-19 18:58:01 +0200385 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386}
387
Petr Machata2b46cfc2012-02-18 11:17:29 +0100388/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800389void
Juan Cespedesf1350522008-12-16 18:19:58 +0100390do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200391 debug(DEBUG_FUNCTION, "do_close_elf()");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 elf_end(lte->elf);
393 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200394}
395
Petr Machata2b46cfc2012-02-18 11:17:29 +0100396/* XXX non-static for now, as it's not called anywhere. But we want
397 * this code around. */
398GElf_Addr
Juan Cespedesf1350522008-12-16 18:19:58 +0100399opd2addr(struct ltelf *lte, GElf_Addr addr) {
Petr Machatab3f8fef2006-11-30 14:45:07 +0100400#ifdef ARCH_SUPPORTS_OPD
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200401 unsigned long base, offset;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200402
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100403 if (!lte->opd)
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200404 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100405
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200406 base = (unsigned long)lte->opd->d_buf;
407 offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 if (offset > lte->opd_size)
409 error(EXIT_FAILURE, 0, "static plt not in .opd");
410
Olaf Heringa841f652006-09-15 01:57:49 +0200411 return *(GElf_Addr*)(base + offset);
Petr Machatab3f8fef2006-11-30 14:45:07 +0100412#else //!ARCH_SUPPORTS_OPD
413 return addr;
414#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100415}
416
Petr Machata2b46cfc2012-02-18 11:17:29 +0100417struct library *
418ltelf_read_library(const char *filename, GElf_Addr base)
Petr Machatae84fa002012-02-07 13:43:03 +0100419{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420 // XXX we leak LTE contents
Petr Machata29add4f2012-02-18 16:38:05 +0100421 struct ltelf lte = {};
422 if (do_init_elf(&lte, filename, base) < 0)
Petr Machata1974dbc2011-08-19 18:58:01 +0200423 return NULL;
Petr Machatae67635d2012-03-21 03:37:39 +0100424 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800425
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426 struct library *lib = malloc(sizeof(*lib));
427 char *soname = NULL;
428 if (lib == NULL) {
429 fail:
430 free(soname);
431 library_destroy(lib);
432 free(lib);
433 lib = NULL;
434 goto done;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800435 }
436
Petr Machata2b46cfc2012-02-18 11:17:29 +0100437 if (lte.soname != NULL) {
438 soname = strdup(lte.soname);
439 if (soname == NULL)
440 goto fail;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800441 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700442
Petr Machata2b46cfc2012-02-18 11:17:29 +0100443 library_init(lib, soname, soname != NULL);
444 lib->entry = (target_address_t)lte.entry_addr;
445 lib->base = (target_address_t)lte.base_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100446
Petr Machata2b46cfc2012-02-18 11:17:29 +0100447 size_t i;
448 for (i = 0; i < lte.relplt_count; ++i) {
449 GElf_Rel rel;
450 GElf_Rela rela;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100451 GElf_Sym sym;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100452 void *ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100453
Petr Machata2b46cfc2012-02-18 11:17:29 +0100454 if (lte.relplt->d_type == ELF_T_REL) {
455 ret = gelf_getrel(lte.relplt, i, &rel);
456 rela.r_offset = rel.r_offset;
457 rela.r_info = rel.r_info;
458 rela.r_addend = 0;
459 } else {
460 ret = gelf_getrela(lte.relplt, i, &rela);
461 }
462
463 if (ret == NULL
464 || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
465 || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
466 &sym) == NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100467 error(EXIT_FAILURE, 0,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100468 "Couldn't get relocation from \"%s\"",
469 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100470
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471 /* We will destroy the ELF object at the end of the
472 * scope. We need to copy the name for our purposes.
473 * XXX consider just keeping the ELF around. */
474 char *name = strdup(lte.dynstr + sym.st_name);
475 if (name == NULL) {
476 fail2:
477 free(name);
478 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800479 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100480
Petr Machatae67635d2012-03-21 03:37:39 +0100481 enum toplt pltt = PLTS_ARE_EXECUTABLE(&lte)
482 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
483 GElf_Addr addr = arch_plt_sym_val(&lte, i, &rela);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100484
485 struct library_symbol *libsym = malloc(sizeof(*libsym));
486 if (libsym == NULL)
487 goto fail2;
Petr Machata29add4f2012-02-18 16:38:05 +0100488 library_symbol_init(libsym, lib, addr + lte.bias, name, 1, pltt,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100489 ELF64_ST_BIND(sym.st_info) == STB_WEAK);
490 library_add_symbol(lib, libsym);
Joe Damatoe2a8f572010-11-08 15:47:40 -0800491 }
492
Petr Machata2b46cfc2012-02-18 11:17:29 +0100493done:
494 do_close_elf(&lte);
495 return lib;
496}
Petr Machatae84fa002012-02-07 13:43:03 +0100497
Petr Machata2b46cfc2012-02-18 11:17:29 +0100498struct library *
499ltelf_read_main_binary(struct Process *proc, const char *path)
500{
501 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
502 char *fname = pid2name(proc->pid);
503 struct library *lib = ltelf_read_library(fname, 0);
504 library_set_name(lib, path, 0);
505 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200506}