blob: 60d3851771250003db9fdd9d317b0c0d6a7d7feb [file] [log] [blame]
Joe Damatof0bd98b2010-11-08 15:47:42 -08001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Petr Machata157cc4d2012-04-04 19:00:34 +02003#include <assert.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01004#include <endian.h>
Juan Cespedes96935a91997-08-09 23:45:39 +02005#include <errno.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01006#include <error.h>
Juan Cespedes96935a91997-08-09 23:45:39 +02007#include <fcntl.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01008#include <gelf.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -08009#include <inttypes.h>
Petr Machata157cc4d2012-04-04 19:00:34 +020010#include <search.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010011#include <stdint.h>
12#include <stdlib.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020013#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010014#include <unistd.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020015
Juan Cespedesf7281232009-06-25 16:11:21 +020016#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010017#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010018#include "library.h"
Petr Machatab5f80ac2012-04-04 01:46:18 +020019#include "filter.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080020
Paul Gilliambe320772006-04-24 22:06:23 +020021#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010022extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020023#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010024
Petr Machatafe1c1712010-10-27 16:57:34 +020025#ifndef DT_PPC_GOT
26# define DT_PPC_GOT (DT_LOPROC + 0)
27#endif
28
Petr Machatafe1c1712010-10-27 16:57:34 +020029
Petr Machatae67635d2012-03-21 03:37:39 +010030#ifndef ARCH_HAVE_LTELF_DATA
31int
Petr Machatae67635d2012-03-21 03:37:39 +010032arch_elf_init(struct ltelf *lte)
33{
34 return 0;
35}
Petr Machatac67a6e62012-03-28 02:39:49 +020036
37void
38arch_elf_destroy(struct ltelf *lte)
39{
40}
Petr Machatae67635d2012-03-21 03:37:39 +010041#endif
42
Petr Machatae6523e62012-03-24 04:54:06 +010043int
44default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020045 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010046 struct library_symbol **ret)
47{
48 char *name = strdup(a_name);
49 if (name == NULL) {
50 fail:
51 free(name);
52 return -1;
53 }
54
55 enum toplt pltt = PLTS_ARE_EXECUTABLE(lte)
56 ? LS_TOPLT_EXEC : LS_TOPLT_POINT;
Petr Machata1be22912012-03-27 03:11:33 +020057 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010058
59 struct library_symbol *libsym = malloc(sizeof(*libsym));
60 if (libsym == NULL)
61 goto fail;
62
63 target_address_t taddr = (target_address_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010064
65 /* The logic behind this conditional translation is as
66 * follows. PLT entries do not typically need custom TOC
67 * pointer, and therefore aren't redirected via OPD. POINT
68 * PLT, on the other hand, most likely contains addresses of
69 * target functions, not PLT entries themselves, and would
70 * need the OPD redirection. */
71 if (pltt == LS_TOPLT_POINT
72 && arch_translate_address(proc, taddr, &taddr) < 0) {
Petr Machatae6523e62012-03-24 04:54:06 +010073 free(libsym);
74 goto fail;
75 }
76
77 library_symbol_init(libsym, taddr, name, 1, pltt);
78 *ret = libsym;
79 return 0;
80}
81
82#ifndef ARCH_HAVE_ADD_PLT_ENTRY
83enum plt_status
84arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020085 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010086 struct library_symbol **ret)
87{
88 return plt_default;
89}
90#endif
91
Petr Machatae67635d2012-03-21 03:37:39 +010092Elf_Data *
93elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020094{
95 Elf_Data *data = elf_getdata(scn, NULL);
96 if (data == NULL || elf_getdata(scn, data) != NULL
97 || data->d_off || data->d_size != shdr->sh_size)
98 return NULL;
99 return data;
100}
101
Petr Machatae67635d2012-03-21 03:37:39 +0100102static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100103elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
104 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
105 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200106{
107 int i;
108 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
109 Elf_Scn *scn;
110 GElf_Shdr shdr;
111
112 scn = elf_getscn(lte->elf, i);
113 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
114 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100115 return -1;
116 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100117 if (predicate(scn, &shdr, data)) {
118 *tgt_sec = scn;
119 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200120 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100121 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200122 }
Petr Machatae67635d2012-03-21 03:37:39 +0100123 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100124
125}
126
127static int
128inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
129{
130 GElf_Addr addr = *(GElf_Addr *)data;
131 return addr >= shdr->sh_addr
132 && addr < shdr->sh_addr + shdr->sh_size;
133}
134
135int
136elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
137 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
138{
139 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
140 &inside_p, &addr);
141}
142
143static int
144type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
145{
146 GElf_Word type = *(GElf_Word *)data;
147 return shdr->sh_type == type;
148}
149
150int
151elf_get_section_type(struct ltelf *lte, GElf_Word type,
152 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
153{
154 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
155 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100156}
157
158static int
159need_data(Elf_Data *data, size_t offset, size_t size)
160{
161 assert(data != NULL);
162 if (data->d_size < size || offset > data->d_size - size) {
163 debug(1, "Not enough data to read %zd-byte value"
164 " at offset %zd.", size, offset);
165 return -1;
166 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200167 return 0;
168}
169
Petr Machatae67635d2012-03-21 03:37:39 +0100170#define DEF_READER(NAME, SIZE) \
171 int \
172 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
173 { \
174 if (!need_data(data, offset, SIZE / 8) < 0) \
175 return -1; \
176 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200177 if (data->d_buf == NULL) /* NODATA section */ { \
178 *retp = 0; \
179 return 0; \
180 } \
181 \
Petr Machatae67635d2012-03-21 03:37:39 +0100182 union { \
183 uint##SIZE##_t dst; \
184 char buf[0]; \
185 } u; \
186 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
187 *retp = u.dst; \
188 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200189 }
190
Petr Machatae67635d2012-03-21 03:37:39 +0100191DEF_READER(elf_read_u16, 16)
192DEF_READER(elf_read_u32, 32)
193DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200194
Petr Machatae67635d2012-03-21 03:37:39 +0100195#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200196
Petr Machata1974dbc2011-08-19 18:58:01 +0200197int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200198open_elf(struct ltelf *lte, const char *filename)
199{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 lte->fd = open(filename, O_RDONLY);
201 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200202 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200203
Petr Machata02bd9ec2011-09-21 17:38:59 +0200204 elf_version(EV_CURRENT);
205
Juan Cespedesd914a202004-11-10 00:15:33 +0100206#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200208#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200210#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200211
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
213 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200214
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100215 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
216 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
217 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200218
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
220 error(EXIT_FAILURE, 0,
221 "\"%s\" is not an ELF executable nor shared library",
222 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200223
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
225 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100226#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
228 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100229#endif
230#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
232 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100233#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100234 )
235 error(EXIT_FAILURE, 0,
236 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100237
Petr Machata02bd9ec2011-09-21 17:38:59 +0200238 return 0;
239}
240
Petr Machatae67635d2012-03-21 03:37:39 +0100241static int
242do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200244 int i;
245 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200247
248 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
249 debug(1, "Reading ELF from %s...", filename);
250
251 if (open_elf(lte, filename) < 0)
252 return -1;
253
Petr Machatab120fdf2012-03-21 05:05:46 +0100254 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100255 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 GElf_Phdr phdr;
257 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
258 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200259 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100260 break;
261 }
262 }
263 }
264
Petr Machatab120fdf2012-03-21 05:05:46 +0100265 if (lte->base_addr == 0) {
266 fprintf(stderr, "Couldn't determine base address of %s\n",
267 filename);
268 return -1;
269 }
270
271 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100272 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100273
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100274 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
275 Elf_Scn *scn;
276 GElf_Shdr shdr;
277 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100278
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 scn = elf_getscn(lte->elf, i);
280 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
281 error(EXIT_FAILURE, 0,
282 "Couldn't get section header from \"%s\"",
283 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100284
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100285 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
286 if (name == NULL)
287 error(EXIT_FAILURE, 0,
288 "Couldn't get section header from \"%s\"",
289 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100290
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100291 if (shdr.sh_type == SHT_SYMTAB) {
292 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100293
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 lte->symtab = elf_getdata(scn, NULL);
295 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
296 if ((lte->symtab == NULL
297 || elf_getdata(scn, lte->symtab) != NULL)
Petr Machatada3edbf2012-04-04 02:20:21 +0200298 && options.static_filter != NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 error(EXIT_FAILURE, 0,
300 "Couldn't get .symtab data from \"%s\"",
301 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100302
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100303 scn = elf_getscn(lte->elf, shdr.sh_link);
304 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
305 error(EXIT_FAILURE, 0,
306 "Couldn't get section header from \"%s\"",
307 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100308
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 data = elf_getdata(scn, NULL);
310 if (data == NULL || elf_getdata(scn, data) != NULL
311 || shdr.sh_size != data->d_size || data->d_off)
312 error(EXIT_FAILURE, 0,
313 "Couldn't get .strtab data from \"%s\"",
314 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100315
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 lte->strtab = data->d_buf;
317 } else if (shdr.sh_type == SHT_DYNSYM) {
318 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100319
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100320 lte->dynsym = elf_getdata(scn, NULL);
321 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
322 if (lte->dynsym == NULL
323 || elf_getdata(scn, lte->dynsym) != NULL)
324 error(EXIT_FAILURE, 0,
325 "Couldn't get .dynsym data from \"%s\"",
326 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100327
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 scn = elf_getscn(lte->elf, shdr.sh_link);
329 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
330 error(EXIT_FAILURE, 0,
331 "Couldn't get section header from \"%s\"",
332 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100333
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100334 data = elf_getdata(scn, NULL);
335 if (data == NULL || elf_getdata(scn, data) != NULL
336 || shdr.sh_size != data->d_size || data->d_off)
337 error(EXIT_FAILURE, 0,
338 "Couldn't get .dynstr data from \"%s\"",
339 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100340
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 lte->dynstr = data->d_buf;
342 } else if (shdr.sh_type == SHT_DYNAMIC) {
343 Elf_Data *data;
344 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100345
Joe Damato87f4f582010-11-08 15:47:36 -0800346 lte->dyn_addr = shdr.sh_addr;
347 lte->dyn_sz = shdr.sh_size;
348
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 data = elf_getdata(scn, NULL);
350 if (data == NULL || elf_getdata(scn, data) != NULL)
351 error(EXIT_FAILURE, 0,
352 "Couldn't get .dynamic data from \"%s\"",
353 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100354
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
356 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100357
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 if (gelf_getdyn(data, j, &dyn) == NULL)
359 error(EXIT_FAILURE, 0,
360 "Couldn't get .dynamic data from \"%s\"",
361 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 if (dyn.d_tag == DT_JMPREL)
363 relplt_addr = dyn.d_un.d_ptr;
364 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100365 lte->relplt_size = dyn.d_un.d_val;
366 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100367 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 } else if (shdr.sh_type == SHT_PROGBITS
370 || shdr.sh_type == SHT_NOBITS) {
371 if (strcmp(name, ".plt") == 0) {
372 lte->plt_addr = shdr.sh_addr;
373 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100374 lte->plt_data = elf_loaddata(scn, &shdr);
375 if (lte->plt_data == NULL)
376 fprintf(stderr,
377 "Can't load .plt data\n");
378 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200379 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100380 }
381#ifdef ARCH_SUPPORTS_OPD
382 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200383 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 lte->opd_size = shdr.sh_size;
385 lte->opd = elf_rawdata(scn, NULL);
386 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100387#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100389 }
390
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 if (lte->dynsym == NULL || lte->dynstr == NULL)
392 error(EXIT_FAILURE, 0,
393 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100394
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395 if (!relplt_addr || !lte->plt_addr) {
396 debug(1, "%s has no PLT relocations", filename);
397 lte->relplt = NULL;
398 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100399 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200400 debug(1, "%s has unknown PLT size", filename);
401 lte->relplt = NULL;
402 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100403 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200404
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
406 Elf_Scn *scn;
407 GElf_Shdr shdr;
408
409 scn = elf_getscn(lte->elf, i);
410 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
411 error(EXIT_FAILURE, 0,
412 "Couldn't get section header from \"%s\"",
413 filename);
414 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100415 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 lte->relplt = elf_getdata(scn, NULL);
417 lte->relplt_count =
418 shdr.sh_size / shdr.sh_entsize;
419 if (lte->relplt == NULL
420 || elf_getdata(scn, lte->relplt) != NULL)
421 error(EXIT_FAILURE, 0,
422 "Couldn't get .rel*.plt data from \"%s\"",
423 filename);
424 break;
425 }
426 }
427
428 if (i == lte->ehdr.e_shnum)
429 error(EXIT_FAILURE, 0,
430 "Couldn't find .rel*.plt section in \"%s\"",
431 filename);
432
433 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
434 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100435
436 if (soname_offset != 0)
437 lte->soname = lte->dynstr + soname_offset;
438
Petr Machata644d6692012-03-24 02:06:48 +0100439 if (arch_elf_init(lte) < 0) {
440 fprintf(stderr, "Backend initialization failed.\n");
441 return -1;
442 }
443
Petr Machata1974dbc2011-08-19 18:58:01 +0200444 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100445}
446
Petr Machata2b46cfc2012-02-18 11:17:29 +0100447/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800448void
Juan Cespedesf1350522008-12-16 18:19:58 +0100449do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200450 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100451 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100452 elf_end(lte->elf);
453 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200454}
455
Petr Machatab5f80ac2012-04-04 01:46:18 +0200456static int
457populate_plt(struct Process *proc, const char *filename,
458 struct ltelf *lte, struct library *lib)
459{
460 size_t i;
461 for (i = 0; i < lte->relplt_count; ++i) {
462 GElf_Rel rel;
463 GElf_Rela rela;
464 GElf_Sym sym;
465 void *ret;
466
467 if (lte->relplt->d_type == ELF_T_REL) {
468 ret = gelf_getrel(lte->relplt, i, &rel);
469 rela.r_offset = rel.r_offset;
470 rela.r_info = rel.r_info;
471 rela.r_addend = 0;
472 } else {
473 ret = gelf_getrela(lte->relplt, i, &rela);
474 }
475
476 if (ret == NULL
477 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
478 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
479 &sym) == NULL)
480 error(EXIT_FAILURE, 0,
481 "Couldn't get relocation from \"%s\"",
482 filename);
483
484 char const *name = lte->dynstr + sym.st_name;
485
486 if (!filter_matches_symbol(options.plt_filter, name, lib))
487 continue;
488
Petr Machatab5f80ac2012-04-04 01:46:18 +0200489 struct library_symbol *libsym;
490 switch (arch_elf_add_plt_entry(proc, lte, name,
491 &rela, i, &libsym)) {
492 case plt_default:
493 if (default_elf_add_plt_entry(proc, lte, name,
494 &rela, i, &libsym) < 0)
495 case plt_fail:
496 return -1;
497 case plt_ok:
498 if (libsym != NULL)
499 library_add_symbol(lib, libsym);
500 }
501 }
502 return 0;
503}
504
Petr Machata157cc4d2012-04-04 19:00:34 +0200505/* When -x rules result in request to trace several aliases, we only
506 * want to add such symbol once. The only way that those symbols
507 * differ in is their name, e.g. in glibc you have __GI___libc_free,
508 * __cfree, __free, __libc_free, cfree and free all defined on the
509 * same address. So instead we keep this unique symbol struct for
510 * each address, and replace name in libsym with a shorter variant if
511 * we find it. */
512struct unique_symbol {
513 target_address_t addr;
514 struct library_symbol *libsym;
515};
516
517static int
518unique_symbol_cmp(const void *key, const void *val)
519{
520 const struct unique_symbol *sym_key = key;
521 const struct unique_symbol *sym_val = val;
522 return sym_key->addr != sym_val->addr;
523}
524
Petr Machatada3edbf2012-04-04 02:20:21 +0200525static int
526populate_this_symtab(struct Process *proc, const char *filename,
527 struct ltelf *lte, struct library *lib,
528 Elf_Data *symtab, const char *strtab, size_t size)
529{
Petr Machata157cc4d2012-04-04 19:00:34 +0200530 /* Using sorted array would be arguably better, but this
531 * should be well enough for the number of symbols that we
532 * typically deal with. */
533 size_t num_symbols = 0;
534 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
535 if (symbols == NULL) {
536 error(0, errno, "couldn't insert symbols for -x");
537 return -1;
538 }
539
Petr Machatada3edbf2012-04-04 02:20:21 +0200540 size_t lib_len = strlen(lib->soname);
541 size_t i;
542 for (i = 0; i < size; ++i) {
543 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200544 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200545 fail:
546 error(0, errno, "couldn't get symbol #%zd from %s: %s",
547 i, filename, elf_errmsg(-1));
548 continue;
549 }
550
Petr Machata4de6b6b2012-04-04 14:06:09 +0200551 /* XXX support IFUNC as well. */
552 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
553 || sym.st_value == 0)
554 continue;
555
Petr Machatada3edbf2012-04-04 02:20:21 +0200556 const char *name = strtab + sym.st_name;
557 if (!filter_matches_symbol(options.static_filter, name, lib))
558 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200559
560 target_address_t addr
561 = (target_address_t)(sym.st_value + lte->bias);
562 target_address_t naddr;
563 if (arch_translate_address(proc, addr, &naddr) < 0) {
564 error(0, errno, "couldn't translate address of %s@%s",
565 name, lib->soname);
566 continue;
567 }
568 if (addr != naddr)
569 naddr += lte->bias;
570
Petr Machata3840f682012-04-06 16:05:41 +0200571 char *full_name;
572 if (lib->type != LT_LIBTYPE_MAIN) {
573 full_name = malloc(strlen(name) + 1 + lib_len + 1);
574 if (full_name == NULL)
575 goto fail;
576 sprintf(full_name, "%s@%s", name, lib->soname);
577 } else {
578 full_name = strdup(name);
579 if (full_name == NULL)
580 goto fail;
581 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200582
Petr Machata157cc4d2012-04-04 19:00:34 +0200583 /* Look whether we already have a symbol for this
584 * address. If not, add this one. */
585 struct unique_symbol key = { naddr, NULL };
586 struct unique_symbol *unique
587 = lsearch(&key, symbols, &num_symbols,
588 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200589
Petr Machata157cc4d2012-04-04 19:00:34 +0200590 if (unique->libsym == NULL) {
591 struct library_symbol *libsym = malloc(sizeof(*libsym));
592 if (libsym == NULL) {
593 --num_symbols;
594 goto fail;
595 }
596 library_symbol_init(libsym, naddr, full_name,
597 1, LS_TOPLT_NONE);
598 unique->libsym = libsym;
599 unique->addr = naddr;
600
601 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
602 library_symbol_set_name(unique->libsym, full_name, 1);
603
604 } else {
605 free(full_name);
606 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200607 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200608
609 for (i = 0; i < num_symbols; ++i) {
610 assert(symbols[i].libsym != NULL);
611 library_add_symbol(lib, symbols[i].libsym);
612 }
613
614 free(symbols);
615
Petr Machatada3edbf2012-04-04 02:20:21 +0200616 return 0;
617}
618
619static int
620populate_symtab(struct Process *proc, const char *filename,
621 struct ltelf *lte, struct library *lib)
622{
623 if (lte->symtab != NULL && lte->strtab != NULL)
624 return populate_this_symtab(proc, filename, lte, lib,
625 lte->symtab, lte->strtab,
626 lte->symtab_count);
627 else
628 return populate_this_symtab(proc, filename, lte, lib,
629 lte->dynsym, lte->dynstr,
630 lte->dynsym_count);
631}
632
Petr Machatab5f80ac2012-04-04 01:46:18 +0200633int
634ltelf_read_library(struct library *lib, struct Process *proc,
635 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100636{
Petr Machata29add4f2012-02-18 16:38:05 +0100637 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100638 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200639 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100640 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800641
Petr Machatab5f80ac2012-04-04 01:46:18 +0200642 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200643 if (lib == NULL)
644 goto fail;
645
646 /* Note that we set soname and pathname as soon as they are
647 * allocated, so in case of further errors, this get released
648 * when LIB is release, which should happen in the caller when
649 * we return error. */
650
651 if (lib->pathname == NULL) {
652 char *pathname = strdup(filename);
653 if (pathname == NULL)
654 goto fail;
655 library_set_pathname(lib, filename, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800656 }
657
Petr Machata0b55b582012-04-02 00:38:46 +0200658 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200659 char *soname = strdup(lte.soname);
660 if (soname == NULL)
661 goto fail;
662 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200663 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200664 const char *soname = rindex(lib->pathname, '/') + 1;
665 if (soname == NULL)
666 soname = lib->pathname;
667 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200668 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700669
Petr Machatab120fdf2012-03-21 05:05:46 +0100670 target_address_t entry = (target_address_t)lte.entry_addr;
Petr Machata49275b02012-04-03 12:38:51 +0200671 if (arch_translate_address(proc, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100672 goto fail;
673
Petr Machata2b46cfc2012-02-18 11:17:29 +0100674 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100675 lib->entry = entry;
Petr Machata52dbfb12012-03-29 16:38:26 +0200676 lib->dyn_addr = (target_address_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100677
Petr Machatab5f80ac2012-04-04 01:46:18 +0200678 if (filter_matches_library(options.plt_filter, lib)
679 && populate_plt(proc, filename, &lte, lib) < 0)
680 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800681
Petr Machatada3edbf2012-04-04 02:20:21 +0200682 if (filter_matches_library(options.static_filter, lib)
683 && populate_symtab(proc, filename, &lte, lib) < 0)
684 goto fail;
685
Petr Machata2b46cfc2012-02-18 11:17:29 +0100686done:
687 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200688 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200689
690fail:
691 status = -1;
692 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100693}
Petr Machatae84fa002012-02-07 13:43:03 +0100694
Petr Machata2b46cfc2012-02-18 11:17:29 +0100695struct library *
696ltelf_read_main_binary(struct Process *proc, const char *path)
697{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200698 struct library *lib = malloc(sizeof(*lib));
699 if (lib == NULL)
700 return NULL;
701 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200702 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200703
Petr Machatafc6ff182012-04-04 13:11:50 +0200704 /* There is a race between running the process and reading its
705 * binary for internal consumption. So open the binary from
706 * the /proc filesystem. XXX Note that there is similar race
707 * for libraries, but there we don't have a nice answer like
708 * that. Presumably we could read the DSOs from the process
709 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100710 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200711 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
712 library_destroy(lib);
713 free(lib);
714 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200715 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200716
Petr Machata2b46cfc2012-02-18 11:17:29 +0100717 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200718}