blob: 9285fee75192c5c4cae3a59597f1f03e484f1ed9 [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 Machata29add4f2012-02-18 16:38:05 +0100260 fprintf(stderr,
Petr Machatab120fdf2012-03-21 05:05:46 +0100261 " + vaddr=%#lx, bias=%#lx, base=%#lx\n",
262 phdr.p_vaddr, bias, lte->base_addr);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263 break;
264 }
265 }
266 }
267
Petr Machatab120fdf2012-03-21 05:05:46 +0100268 if (lte->base_addr == 0) {
269 fprintf(stderr, "Couldn't determine base address of %s\n",
270 filename);
271 return -1;
272 }
273
274 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100275 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100277 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
278 Elf_Scn *scn;
279 GElf_Shdr shdr;
280 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100281
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 scn = elf_getscn(lte->elf, i);
283 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
284 error(EXIT_FAILURE, 0,
285 "Couldn't get section header from \"%s\"",
286 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100287
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100288 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
289 if (name == NULL)
290 error(EXIT_FAILURE, 0,
291 "Couldn't get section header from \"%s\"",
292 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100293
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100294 if (shdr.sh_type == SHT_SYMTAB) {
295 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100296
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100297 lte->symtab = elf_getdata(scn, NULL);
298 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
299 if ((lte->symtab == NULL
300 || elf_getdata(scn, lte->symtab) != NULL)
Petr Machatada3edbf2012-04-04 02:20:21 +0200301 && options.static_filter != NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100302 error(EXIT_FAILURE, 0,
303 "Couldn't get .symtab data from \"%s\"",
304 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100305
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 scn = elf_getscn(lte->elf, shdr.sh_link);
307 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
308 error(EXIT_FAILURE, 0,
309 "Couldn't get section header from \"%s\"",
310 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100311
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100312 data = elf_getdata(scn, NULL);
313 if (data == NULL || elf_getdata(scn, data) != NULL
314 || shdr.sh_size != data->d_size || data->d_off)
315 error(EXIT_FAILURE, 0,
316 "Couldn't get .strtab data from \"%s\"",
317 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100318
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 lte->strtab = data->d_buf;
320 } else if (shdr.sh_type == SHT_DYNSYM) {
321 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100322
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100323 lte->dynsym = elf_getdata(scn, NULL);
324 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
325 if (lte->dynsym == NULL
326 || elf_getdata(scn, lte->dynsym) != NULL)
327 error(EXIT_FAILURE, 0,
328 "Couldn't get .dynsym data from \"%s\"",
329 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100330
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 scn = elf_getscn(lte->elf, shdr.sh_link);
332 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
333 error(EXIT_FAILURE, 0,
334 "Couldn't get section header from \"%s\"",
335 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100336
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100337 data = elf_getdata(scn, NULL);
338 if (data == NULL || elf_getdata(scn, data) != NULL
339 || shdr.sh_size != data->d_size || data->d_off)
340 error(EXIT_FAILURE, 0,
341 "Couldn't get .dynstr data from \"%s\"",
342 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100343
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 lte->dynstr = data->d_buf;
345 } else if (shdr.sh_type == SHT_DYNAMIC) {
346 Elf_Data *data;
347 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100348
Joe Damato87f4f582010-11-08 15:47:36 -0800349 lte->dyn_addr = shdr.sh_addr;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100350 fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
Joe Damato87f4f582010-11-08 15:47:36 -0800351 lte->dyn_sz = shdr.sh_size;
352
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100353 data = elf_getdata(scn, NULL);
354 if (data == NULL || elf_getdata(scn, data) != NULL)
355 error(EXIT_FAILURE, 0,
356 "Couldn't get .dynamic data from \"%s\"",
357 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100358
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100359 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
360 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100361
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 if (gelf_getdyn(data, j, &dyn) == NULL)
363 error(EXIT_FAILURE, 0,
364 "Couldn't get .dynamic data from \"%s\"",
365 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100366 if (dyn.d_tag == DT_JMPREL)
367 relplt_addr = dyn.d_un.d_ptr;
368 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100369 lte->relplt_size = dyn.d_un.d_val;
370 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100372 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 } else if (shdr.sh_type == SHT_PROGBITS
374 || shdr.sh_type == SHT_NOBITS) {
375 if (strcmp(name, ".plt") == 0) {
376 lte->plt_addr = shdr.sh_addr;
377 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100378 lte->plt_data = elf_loaddata(scn, &shdr);
379 if (lte->plt_data == NULL)
380 fprintf(stderr,
381 "Can't load .plt data\n");
382 if (shdr.sh_flags & SHF_EXECINSTR)
Paul Gilliam76c61f12006-06-14 06:55:21 +0200383 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100384 }
385#ifdef ARCH_SUPPORTS_OPD
386 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200387 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 lte->opd_size = shdr.sh_size;
389 lte->opd = elf_rawdata(scn, NULL);
390 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100391#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100393 }
394
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395 if (lte->dynsym == NULL || lte->dynstr == NULL)
396 error(EXIT_FAILURE, 0,
397 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100398
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100399 if (!relplt_addr || !lte->plt_addr) {
400 debug(1, "%s has no PLT relocations", filename);
401 lte->relplt = NULL;
402 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100403 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200404 debug(1, "%s has unknown PLT size", filename);
405 lte->relplt = NULL;
406 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100407 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200408
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100409 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
410 Elf_Scn *scn;
411 GElf_Shdr shdr;
412
413 scn = elf_getscn(lte->elf, i);
414 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
415 error(EXIT_FAILURE, 0,
416 "Couldn't get section header from \"%s\"",
417 filename);
418 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100419 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100420 lte->relplt = elf_getdata(scn, NULL);
421 lte->relplt_count =
422 shdr.sh_size / shdr.sh_entsize;
423 if (lte->relplt == NULL
424 || elf_getdata(scn, lte->relplt) != NULL)
425 error(EXIT_FAILURE, 0,
426 "Couldn't get .rel*.plt data from \"%s\"",
427 filename);
428 break;
429 }
430 }
431
432 if (i == lte->ehdr.e_shnum)
433 error(EXIT_FAILURE, 0,
434 "Couldn't find .rel*.plt section in \"%s\"",
435 filename);
436
437 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
438 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100439
440 if (soname_offset != 0)
441 lte->soname = lte->dynstr + soname_offset;
442
Petr Machata644d6692012-03-24 02:06:48 +0100443 if (arch_elf_init(lte) < 0) {
444 fprintf(stderr, "Backend initialization failed.\n");
445 return -1;
446 }
447
Petr Machata1974dbc2011-08-19 18:58:01 +0200448 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100449}
450
Petr Machata2b46cfc2012-02-18 11:17:29 +0100451/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800452void
Juan Cespedesf1350522008-12-16 18:19:58 +0100453do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200454 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100455 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100456 elf_end(lte->elf);
457 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200458}
459
Petr Machatab5f80ac2012-04-04 01:46:18 +0200460static int
461populate_plt(struct Process *proc, const char *filename,
462 struct ltelf *lte, struct library *lib)
463{
464 size_t i;
465 for (i = 0; i < lte->relplt_count; ++i) {
466 GElf_Rel rel;
467 GElf_Rela rela;
468 GElf_Sym sym;
469 void *ret;
470
471 if (lte->relplt->d_type == ELF_T_REL) {
472 ret = gelf_getrel(lte->relplt, i, &rel);
473 rela.r_offset = rel.r_offset;
474 rela.r_info = rel.r_info;
475 rela.r_addend = 0;
476 } else {
477 ret = gelf_getrela(lte->relplt, i, &rela);
478 }
479
480 if (ret == NULL
481 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
482 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
483 &sym) == NULL)
484 error(EXIT_FAILURE, 0,
485 "Couldn't get relocation from \"%s\"",
486 filename);
487
488 char const *name = lte->dynstr + sym.st_name;
489
490 if (!filter_matches_symbol(options.plt_filter, name, lib))
491 continue;
492
493 fprintf(stderr, "%s@%s matches\n", name, lib->soname);
494
495 struct library_symbol *libsym;
496 switch (arch_elf_add_plt_entry(proc, lte, name,
497 &rela, i, &libsym)) {
498 case plt_default:
499 if (default_elf_add_plt_entry(proc, lte, name,
500 &rela, i, &libsym) < 0)
501 case plt_fail:
502 return -1;
503 case plt_ok:
504 if (libsym != NULL)
505 library_add_symbol(lib, libsym);
506 }
507 }
508 return 0;
509}
510
Petr Machata157cc4d2012-04-04 19:00:34 +0200511/* When -x rules result in request to trace several aliases, we only
512 * want to add such symbol once. The only way that those symbols
513 * differ in is their name, e.g. in glibc you have __GI___libc_free,
514 * __cfree, __free, __libc_free, cfree and free all defined on the
515 * same address. So instead we keep this unique symbol struct for
516 * each address, and replace name in libsym with a shorter variant if
517 * we find it. */
518struct unique_symbol {
519 target_address_t addr;
520 struct library_symbol *libsym;
521};
522
523static int
524unique_symbol_cmp(const void *key, const void *val)
525{
526 const struct unique_symbol *sym_key = key;
527 const struct unique_symbol *sym_val = val;
528 return sym_key->addr != sym_val->addr;
529}
530
Petr Machatada3edbf2012-04-04 02:20:21 +0200531static int
532populate_this_symtab(struct Process *proc, const char *filename,
533 struct ltelf *lte, struct library *lib,
534 Elf_Data *symtab, const char *strtab, size_t size)
535{
Petr Machata157cc4d2012-04-04 19:00:34 +0200536 /* Using sorted array would be arguably better, but this
537 * should be well enough for the number of symbols that we
538 * typically deal with. */
539 size_t num_symbols = 0;
540 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
541 if (symbols == NULL) {
542 error(0, errno, "couldn't insert symbols for -x");
543 return -1;
544 }
545
Petr Machatada3edbf2012-04-04 02:20:21 +0200546 size_t lib_len = strlen(lib->soname);
547 size_t i;
548 for (i = 0; i < size; ++i) {
549 GElf_Sym sym;
550 if (gelf_getsym(lte->symtab, i, &sym) == NULL) {
551 fail:
552 error(0, errno, "couldn't get symbol #%zd from %s: %s",
553 i, filename, elf_errmsg(-1));
554 continue;
555 }
556
Petr Machata4de6b6b2012-04-04 14:06:09 +0200557 /* XXX support IFUNC as well. */
558 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
559 || sym.st_value == 0)
560 continue;
561
562 /* Currently we need to trace entry point in any case,
563 * and we don't support more than one breakpoint per
564 * address. So skip _start if it was in symbol
565 * table. */
566 if (sym.st_value == lte->entry_addr)
Petr Machatada3edbf2012-04-04 02:20:21 +0200567 continue;
568
569 const char *name = strtab + sym.st_name;
570 if (!filter_matches_symbol(options.static_filter, name, lib))
571 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200572
573 target_address_t addr
574 = (target_address_t)(sym.st_value + lte->bias);
575 target_address_t naddr;
576 if (arch_translate_address(proc, addr, &naddr) < 0) {
577 error(0, errno, "couldn't translate address of %s@%s",
578 name, lib->soname);
579 continue;
580 }
581 if (addr != naddr)
582 naddr += lte->bias;
583
Petr Machata4de6b6b2012-04-04 14:06:09 +0200584 char *full_name = malloc(strlen(name) + 1 + lib_len + 1);
585 if (full_name == NULL)
586 goto fail;
587 sprintf(full_name, "%s@%s", name, lib->soname);
588
Petr Machata157cc4d2012-04-04 19:00:34 +0200589 /* Look whether we already have a symbol for this
590 * address. If not, add this one. */
591 struct unique_symbol key = { naddr, NULL };
592 struct unique_symbol *unique
593 = lsearch(&key, symbols, &num_symbols,
594 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200595
Petr Machata157cc4d2012-04-04 19:00:34 +0200596 if (unique->libsym == NULL) {
597 struct library_symbol *libsym = malloc(sizeof(*libsym));
598 if (libsym == NULL) {
599 --num_symbols;
600 goto fail;
601 }
602 library_symbol_init(libsym, naddr, full_name,
603 1, LS_TOPLT_NONE);
604 unique->libsym = libsym;
605 unique->addr = naddr;
606
607 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
608 library_symbol_set_name(unique->libsym, full_name, 1);
609
610 } else {
611 free(full_name);
612 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200613 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200614
615 for (i = 0; i < num_symbols; ++i) {
616 assert(symbols[i].libsym != NULL);
617 library_add_symbol(lib, symbols[i].libsym);
618 }
619
620 free(symbols);
621
Petr Machatada3edbf2012-04-04 02:20:21 +0200622 return 0;
623}
624
625static int
626populate_symtab(struct Process *proc, const char *filename,
627 struct ltelf *lte, struct library *lib)
628{
629 if (lte->symtab != NULL && lte->strtab != NULL)
630 return populate_this_symtab(proc, filename, lte, lib,
631 lte->symtab, lte->strtab,
632 lte->symtab_count);
633 else
634 return populate_this_symtab(proc, filename, lte, lib,
635 lte->dynsym, lte->dynstr,
636 lte->dynsym_count);
637}
638
Petr Machatab5f80ac2012-04-04 01:46:18 +0200639int
640ltelf_read_library(struct library *lib, struct Process *proc,
641 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100642{
Petr Machata29add4f2012-02-18 16:38:05 +0100643 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100644 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200645 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100646 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800647
Petr Machatab5f80ac2012-04-04 01:46:18 +0200648 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200649 if (lib == NULL)
650 goto fail;
651
652 /* Note that we set soname and pathname as soon as they are
653 * allocated, so in case of further errors, this get released
654 * when LIB is release, which should happen in the caller when
655 * we return error. */
656
657 if (lib->pathname == NULL) {
658 char *pathname = strdup(filename);
659 if (pathname == NULL)
660 goto fail;
661 library_set_pathname(lib, filename, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800662 }
663
Petr Machata0b55b582012-04-02 00:38:46 +0200664 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200665 char *soname = strdup(lte.soname);
666 if (soname == NULL)
667 goto fail;
668 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200669 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200670 const char *soname = rindex(lib->pathname, '/') + 1;
671 if (soname == NULL)
672 soname = lib->pathname;
673 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200674 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700675
Petr Machatab120fdf2012-03-21 05:05:46 +0100676 target_address_t entry = (target_address_t)lte.entry_addr;
Petr Machata49275b02012-04-03 12:38:51 +0200677 if (arch_translate_address(proc, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100678 goto fail;
679
Petr Machata2b46cfc2012-02-18 11:17:29 +0100680 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100681 lib->entry = entry;
Petr Machata52dbfb12012-03-29 16:38:26 +0200682 lib->dyn_addr = (target_address_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100683
Petr Machatab5f80ac2012-04-04 01:46:18 +0200684 if (filter_matches_library(options.plt_filter, lib)
685 && populate_plt(proc, filename, &lte, lib) < 0)
686 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800687
Petr Machatada3edbf2012-04-04 02:20:21 +0200688 if (filter_matches_library(options.static_filter, lib)
689 && populate_symtab(proc, filename, &lte, lib) < 0)
690 goto fail;
691
Petr Machata2b46cfc2012-02-18 11:17:29 +0100692done:
693 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200694 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200695
696fail:
697 status = -1;
698 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100699}
Petr Machatae84fa002012-02-07 13:43:03 +0100700
Petr Machata2b46cfc2012-02-18 11:17:29 +0100701struct library *
702ltelf_read_main_binary(struct Process *proc, const char *path)
703{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200704 struct library *lib = malloc(sizeof(*lib));
705 if (lib == NULL)
706 return NULL;
707 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200708 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200709
Petr Machata2b46cfc2012-02-18 11:17:29 +0100710 fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
Petr Machatafc6ff182012-04-04 13:11:50 +0200711
712 /* There is a race between running the process and reading its
713 * binary for internal consumption. So open the binary from
714 * the /proc filesystem. XXX Note that there is similar race
715 * for libraries, but there we don't have a nice answer like
716 * that. Presumably we could read the DSOs from the process
717 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100718 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200719 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
720 library_destroy(lib);
721 free(lib);
722 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200723 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200724
Petr Machata2b46cfc2012-02-18 11:17:29 +0100725 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200726}