blob: 11b97eddc6af7217124c5032110bb865a7f7427c [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"
Petr Machatab5f80ac2012-04-04 01:46:18 +020018#include "filter.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080019
Paul Gilliambe320772006-04-24 22:06:23 +020020#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010021extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020022#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010023
Petr Machatafe1c1712010-10-27 16:57:34 +020024#ifndef DT_PPC_GOT
25# define DT_PPC_GOT (DT_LOPROC + 0)
26#endif
27
Petr Machatafe1c1712010-10-27 16:57:34 +020028
Petr Machatae67635d2012-03-21 03:37:39 +010029#ifndef ARCH_HAVE_LTELF_DATA
30int
Petr Machatae67635d2012-03-21 03:37:39 +010031arch_elf_init(struct ltelf *lte)
32{
33 return 0;
34}
Petr Machatac67a6e62012-03-28 02:39:49 +020035
36void
37arch_elf_destroy(struct ltelf *lte)
38{
39}
Petr Machatae67635d2012-03-21 03:37:39 +010040#endif
41
Petr Machatae6523e62012-03-24 04:54:06 +010042int
43default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020044 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010045 struct library_symbol **ret)
46{
47 char *name = strdup(a_name);
48 if (name == NULL) {
49 fail:
50 free(name);
51 return -1;
52 }
53
54 enum toplt pltt = PLTS_ARE_EXECUTABLE(lte)
55 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
Petr Machata1be22912012-03-27 03:11:33 +020056 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010057
58 struct library_symbol *libsym = malloc(sizeof(*libsym));
59 if (libsym == NULL)
60 goto fail;
61
62 target_address_t taddr = (target_address_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010063
64 /* The logic behind this conditional translation is as
65 * follows. PLT entries do not typically need custom TOC
66 * pointer, and therefore aren't redirected via OPD. POINT
67 * PLT, on the other hand, most likely contains addresses of
68 * target functions, not PLT entries themselves, and would
69 * need the OPD redirection. */
70 if (pltt == LS_TOPLT_POINT
71 && arch_translate_address(proc, taddr, &taddr) < 0) {
Petr Machatae6523e62012-03-24 04:54:06 +010072 free(libsym);
73 goto fail;
74 }
75
76 library_symbol_init(libsym, taddr, name, 1, pltt);
77 *ret = libsym;
78 return 0;
79}
80
81#ifndef ARCH_HAVE_ADD_PLT_ENTRY
82enum plt_status
83arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020084 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010085 struct library_symbol **ret)
86{
87 return plt_default;
88}
89#endif
90
Petr Machatae67635d2012-03-21 03:37:39 +010091Elf_Data *
92elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020093{
94 Elf_Data *data = elf_getdata(scn, NULL);
95 if (data == NULL || elf_getdata(scn, data) != NULL
96 || data->d_off || data->d_size != shdr->sh_size)
97 return NULL;
98 return data;
99}
100
Petr Machatae67635d2012-03-21 03:37:39 +0100101static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100102elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
103 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
104 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200105{
106 int i;
107 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
108 Elf_Scn *scn;
109 GElf_Shdr shdr;
110
111 scn = elf_getscn(lte->elf, i);
112 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
113 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100114 return -1;
115 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100116 if (predicate(scn, &shdr, data)) {
117 *tgt_sec = scn;
118 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200119 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100120 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200121 }
Petr Machatae67635d2012-03-21 03:37:39 +0100122 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100123
124}
125
126static int
127inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
128{
129 GElf_Addr addr = *(GElf_Addr *)data;
130 return addr >= shdr->sh_addr
131 && addr < shdr->sh_addr + shdr->sh_size;
132}
133
134int
135elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
136 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
137{
138 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
139 &inside_p, &addr);
140}
141
142static int
143type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
144{
145 GElf_Word type = *(GElf_Word *)data;
146 return shdr->sh_type == type;
147}
148
149int
150elf_get_section_type(struct ltelf *lte, GElf_Word type,
151 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
152{
153 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
154 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100155}
156
157static int
158need_data(Elf_Data *data, size_t offset, size_t size)
159{
160 assert(data != NULL);
161 if (data->d_size < size || offset > data->d_size - size) {
162 debug(1, "Not enough data to read %zd-byte value"
163 " at offset %zd.", size, offset);
164 return -1;
165 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200166 return 0;
167}
168
Petr Machatae67635d2012-03-21 03:37:39 +0100169#define DEF_READER(NAME, SIZE) \
170 int \
171 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
172 { \
173 if (!need_data(data, offset, SIZE / 8) < 0) \
174 return -1; \
175 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200176 if (data->d_buf == NULL) /* NODATA section */ { \
177 *retp = 0; \
178 return 0; \
179 } \
180 \
Petr Machatae67635d2012-03-21 03:37:39 +0100181 union { \
182 uint##SIZE##_t dst; \
183 char buf[0]; \
184 } u; \
185 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
186 *retp = u.dst; \
187 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200188 }
189
Petr Machatae67635d2012-03-21 03:37:39 +0100190DEF_READER(elf_read_u16, 16)
191DEF_READER(elf_read_u32, 32)
192DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200193
Petr Machatae67635d2012-03-21 03:37:39 +0100194#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200195
Petr Machata1974dbc2011-08-19 18:58:01 +0200196int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200197open_elf(struct ltelf *lte, const char *filename)
198{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100199 lte->fd = open(filename, O_RDONLY);
200 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200201 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200202
Petr Machata02bd9ec2011-09-21 17:38:59 +0200203 elf_version(EV_CURRENT);
204
Juan Cespedesd914a202004-11-10 00:15:33 +0100205#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200207#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200209#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200210
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100211 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
212 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200213
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
215 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
216 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200217
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100218 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
219 error(EXIT_FAILURE, 0,
220 "\"%s\" is not an ELF executable nor shared library",
221 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200222
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
224 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100225#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
227 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100228#endif
229#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100230 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
231 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100232#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 )
234 error(EXIT_FAILURE, 0,
235 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100236
Petr Machata02bd9ec2011-09-21 17:38:59 +0200237 return 0;
238}
239
Petr Machatae67635d2012-03-21 03:37:39 +0100240static int
241do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100242{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200243 int i;
244 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100245 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200246
247 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
248 debug(1, "Reading ELF from %s...", filename);
249
250 if (open_elf(lte, filename) < 0)
251 return -1;
252
Petr Machatab120fdf2012-03-21 05:05:46 +0100253 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100254 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 GElf_Phdr phdr;
256 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
257 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200258 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100259 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100260 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
261 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100262 break;
263 }
264 }
265 }
266
Petr Machatab120fdf2012-03-21 05:05:46 +0100267 if (lte->base_addr == 0) {
268 fprintf(stderr, "Couldn't determine base address of %s\n",
269 filename);
270 return -1;
271 }
272
273 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100274 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
277 Elf_Scn *scn;
278 GElf_Shdr shdr;
279 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100280
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100281 scn = elf_getscn(lte->elf, i);
282 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
283 error(EXIT_FAILURE, 0,
284 "Couldn't get section header from \"%s\"",
285 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100286
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
288 if (name == NULL)
289 error(EXIT_FAILURE, 0,
290 "Couldn't get section header from \"%s\"",
291 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100292
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 if (shdr.sh_type == SHT_SYMTAB) {
294 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100295
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100296 lte->symtab = elf_getdata(scn, NULL);
297 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
298 if ((lte->symtab == NULL
299 || elf_getdata(scn, lte->symtab) != NULL)
Petr Machatada3edbf2012-04-04 02:20:21 +0200300 && options.static_filter != NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100301 error(EXIT_FAILURE, 0,
302 "Couldn't get .symtab data from \"%s\"",
303 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100304
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100305 scn = elf_getscn(lte->elf, shdr.sh_link);
306 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
307 error(EXIT_FAILURE, 0,
308 "Couldn't get section header from \"%s\"",
309 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100310
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100311 data = elf_getdata(scn, NULL);
312 if (data == NULL || elf_getdata(scn, data) != NULL
313 || shdr.sh_size != data->d_size || data->d_off)
314 error(EXIT_FAILURE, 0,
315 "Couldn't get .strtab data from \"%s\"",
316 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100317
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100318 lte->strtab = data->d_buf;
319 } else if (shdr.sh_type == SHT_DYNSYM) {
320 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100321
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100322 lte->dynsym = elf_getdata(scn, NULL);
323 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
324 if (lte->dynsym == NULL
325 || elf_getdata(scn, lte->dynsym) != NULL)
326 error(EXIT_FAILURE, 0,
327 "Couldn't get .dynsym data from \"%s\"",
328 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100329
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 scn = elf_getscn(lte->elf, shdr.sh_link);
331 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
332 error(EXIT_FAILURE, 0,
333 "Couldn't get section header from \"%s\"",
334 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100335
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100336 data = elf_getdata(scn, NULL);
337 if (data == NULL || elf_getdata(scn, data) != NULL
338 || shdr.sh_size != data->d_size || data->d_off)
339 error(EXIT_FAILURE, 0,
340 "Couldn't get .dynstr data from \"%s\"",
341 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100342
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 lte->dynstr = data->d_buf;
344 } else if (shdr.sh_type == SHT_DYNAMIC) {
345 Elf_Data *data;
346 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100347
Joe Damato87f4f582010-11-08 15:47:36 -0800348 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
Joe Damato87f4f582010-11-08 15:47:36 -0800350 lte->dyn_sz = shdr.sh_size;
351
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100352 data = elf_getdata(scn, NULL);
353 if (data == NULL || elf_getdata(scn, data) != NULL)
354 error(EXIT_FAILURE, 0,
355 "Couldn't get .dynamic data from \"%s\"",
356 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100357
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
359 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100360
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 if (gelf_getdyn(data, j, &dyn) == NULL)
362 error(EXIT_FAILURE, 0,
363 "Couldn't get .dynamic data from \"%s\"",
364 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 if (dyn.d_tag == DT_JMPREL)
366 relplt_addr = dyn.d_un.d_ptr;
367 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100368 lte->relplt_size = dyn.d_un.d_val;
369 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100370 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100371 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372 } else if (shdr.sh_type == SHT_PROGBITS
373 || shdr.sh_type == SHT_NOBITS) {
374 if (strcmp(name, ".plt") == 0) {
375 lte->plt_addr = shdr.sh_addr;
376 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100377 lte->plt_data = elf_loaddata(scn, &shdr);
378 if (lte->plt_data == NULL)
379 fprintf(stderr,
380 "Can't load .plt data\n");
381 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200382 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100383 }
384#ifdef ARCH_SUPPORTS_OPD
385 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200386 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100387 lte->opd_size = shdr.sh_size;
388 lte->opd = elf_rawdata(scn, NULL);
389 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100390#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100392 }
393
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100394 if (lte->dynsym == NULL || lte->dynstr == NULL)
395 error(EXIT_FAILURE, 0,
396 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100397
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100398 if (!relplt_addr || !lte->plt_addr) {
399 debug(1, "%s has no PLT relocations", filename);
400 lte->relplt = NULL;
401 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100402 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200403 debug(1, "%s has unknown PLT size", filename);
404 lte->relplt = NULL;
405 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200407
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
409 Elf_Scn *scn;
410 GElf_Shdr shdr;
411
412 scn = elf_getscn(lte->elf, i);
413 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
414 error(EXIT_FAILURE, 0,
415 "Couldn't get section header from \"%s\"",
416 filename);
417 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100418 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100419 lte->relplt = elf_getdata(scn, NULL);
420 lte->relplt_count =
421 shdr.sh_size / shdr.sh_entsize;
422 if (lte->relplt == NULL
423 || elf_getdata(scn, lte->relplt) != NULL)
424 error(EXIT_FAILURE, 0,
425 "Couldn't get .rel*.plt data from \"%s\"",
426 filename);
427 break;
428 }
429 }
430
431 if (i == lte->ehdr.e_shnum)
432 error(EXIT_FAILURE, 0,
433 "Couldn't find .rel*.plt section in \"%s\"",
434 filename);
435
436 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
437 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100438
439 if (soname_offset != 0)
440 lte->soname = lte->dynstr + soname_offset;
441
Petr Machata644d6692012-03-24 02:06:48 +0100442 if (arch_elf_init(lte) < 0) {
443 fprintf(stderr, "Backend initialization failed.\n");
444 return -1;
445 }
446
Petr Machata1974dbc2011-08-19 18:58:01 +0200447 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100448}
449
Petr Machata2b46cfc2012-02-18 11:17:29 +0100450/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800451void
Juan Cespedesf1350522008-12-16 18:19:58 +0100452do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200453 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100454 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100455 elf_end(lte->elf);
456 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200457}
458
Petr Machatab5f80ac2012-04-04 01:46:18 +0200459static int
460populate_plt(struct Process *proc, const char *filename,
461 struct ltelf *lte, struct library *lib)
462{
463 size_t i;
464 for (i = 0; i < lte->relplt_count; ++i) {
465 GElf_Rel rel;
466 GElf_Rela rela;
467 GElf_Sym sym;
468 void *ret;
469
470 if (lte->relplt->d_type == ELF_T_REL) {
471 ret = gelf_getrel(lte->relplt, i, &rel);
472 rela.r_offset = rel.r_offset;
473 rela.r_info = rel.r_info;
474 rela.r_addend = 0;
475 } else {
476 ret = gelf_getrela(lte->relplt, i, &rela);
477 }
478
479 if (ret == NULL
480 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
481 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
482 &sym) == NULL)
483 error(EXIT_FAILURE, 0,
484 "Couldn't get relocation from \"%s\"",
485 filename);
486
487 char const *name = lte->dynstr + sym.st_name;
488
489 if (!filter_matches_symbol(options.plt_filter, name, lib))
490 continue;
491
492 fprintf(stderr, "%s@%s matches\n", name, lib->soname);
493
494 struct library_symbol *libsym;
495 switch (arch_elf_add_plt_entry(proc, lte, name,
496 &rela, i, &libsym)) {
497 case plt_default:
498 if (default_elf_add_plt_entry(proc, lte, name,
499 &rela, i, &libsym) < 0)
500 case plt_fail:
501 return -1;
502 case plt_ok:
503 if (libsym != NULL)
504 library_add_symbol(lib, libsym);
505 }
506 }
507 return 0;
508}
509
Petr Machatada3edbf2012-04-04 02:20:21 +0200510static int
511populate_this_symtab(struct Process *proc, const char *filename,
512 struct ltelf *lte, struct library *lib,
513 Elf_Data *symtab, const char *strtab, size_t size)
514{
515 size_t lib_len = strlen(lib->soname);
516 size_t i;
517 for (i = 0; i < size; ++i) {
518 GElf_Sym sym;
519 if (gelf_getsym(lte->symtab, i, &sym) == NULL) {
520 fail:
521 error(0, errno, "couldn't get symbol #%zd from %s: %s",
522 i, filename, elf_errmsg(-1));
523 continue;
524 }
525
526 if (sym.st_value == 0)
527 continue;
528
529 const char *name = strtab + sym.st_name;
530 if (!filter_matches_symbol(options.static_filter, name, lib))
531 continue;
532 fprintf(stderr, "%s@%s matches\n", name, lib->soname);
533
534 char *full_name = malloc(strlen(name) + 1 + lib_len + 1);
535 if (full_name == NULL)
536 goto fail;
537 sprintf(full_name, "%s@%s", name, lib->soname);
538
539 target_address_t addr
540 = (target_address_t)(sym.st_value + lte->bias);
541 target_address_t naddr;
542 if (arch_translate_address(proc, addr, &naddr) < 0) {
543 error(0, errno, "couldn't translate address of %s@%s",
544 name, lib->soname);
545 continue;
546 }
547 if (addr != naddr)
548 naddr += lte->bias;
549
550 struct library_symbol *libsym = malloc(sizeof(*libsym));
551 if (libsym == NULL)
552 goto fail;
553
554 library_symbol_init(libsym, naddr, full_name, 1, LS_TOPLT_NONE);
555 library_add_symbol(lib, libsym);
556 }
557 return 0;
558}
559
560static int
561populate_symtab(struct Process *proc, const char *filename,
562 struct ltelf *lte, struct library *lib)
563{
564 if (lte->symtab != NULL && lte->strtab != NULL)
565 return populate_this_symtab(proc, filename, lte, lib,
566 lte->symtab, lte->strtab,
567 lte->symtab_count);
568 else
569 return populate_this_symtab(proc, filename, lte, lib,
570 lte->dynsym, lte->dynstr,
571 lte->dynsym_count);
572}
573
Petr Machatab5f80ac2012-04-04 01:46:18 +0200574int
575ltelf_read_library(struct library *lib, struct Process *proc,
576 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100577{
Petr Machata29add4f2012-02-18 16:38:05 +0100578 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100579 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200580 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100581 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800582
Petr Machatab5f80ac2012-04-04 01:46:18 +0200583 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200584 if (lib == NULL)
585 goto fail;
586
587 /* Note that we set soname and pathname as soon as they are
588 * allocated, so in case of further errors, this get released
589 * when LIB is release, which should happen in the caller when
590 * we return error. */
591
592 if (lib->pathname == NULL) {
593 char *pathname = strdup(filename);
594 if (pathname == NULL)
595 goto fail;
596 library_set_pathname(lib, filename, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800597 }
598
Petr Machata0b55b582012-04-02 00:38:46 +0200599 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200600 char *soname = strdup(lte.soname);
601 if (soname == NULL)
602 goto fail;
603 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200604 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200605 const char *soname = rindex(lib->pathname, '/') + 1;
606 if (soname == NULL)
607 soname = lib->pathname;
608 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200609 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700610
Petr Machatab120fdf2012-03-21 05:05:46 +0100611 target_address_t entry = (target_address_t)lte.entry_addr;
Petr Machata49275b02012-04-03 12:38:51 +0200612 if (arch_translate_address(proc, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100613 goto fail;
614
Petr Machata2b46cfc2012-02-18 11:17:29 +0100615 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100616 lib->entry = entry;
Petr Machata52dbfb12012-03-29 16:38:26 +0200617 lib->dyn_addr = (target_address_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100618
Petr Machatab5f80ac2012-04-04 01:46:18 +0200619 if (filter_matches_library(options.plt_filter, lib)
620 && populate_plt(proc, filename, &lte, lib) < 0)
621 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800622
Petr Machatada3edbf2012-04-04 02:20:21 +0200623 if (filter_matches_library(options.static_filter, lib)
624 && populate_symtab(proc, filename, &lte, lib) < 0)
625 goto fail;
626
Petr Machata2b46cfc2012-02-18 11:17:29 +0100627done:
628 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200629 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200630
631fail:
632 status = -1;
633 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100634}
Petr Machatae84fa002012-02-07 13:43:03 +0100635
Petr Machata2b46cfc2012-02-18 11:17:29 +0100636struct library *
637ltelf_read_main_binary(struct Process *proc, const char *path)
638{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200639 struct library *lib = malloc(sizeof(*lib));
640 if (lib == NULL)
641 return NULL;
642 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200643 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200644
Petr Machata2b46cfc2012-02-18 11:17:29 +0100645 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
Petr Machatafc6ff182012-04-04 13:11:50 +0200646
647 /* There is a race between running the process and reading its
648 * binary for internal consumption. So open the binary from
649 * the /proc filesystem. XXX Note that there is similar race
650 * for libraries, but there we don't have a nice answer like
651 * that. Presumably we could read the DSOs from the process
652 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100653 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200654 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
655 library_destroy(lib);
656 free(lib);
657 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200658 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200659
Petr Machata2b46cfc2012-02-18 11:17:29 +0100660 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200661}