blob: 3f0c636c05c504710fb80d3cd06fe89bdc8361aa [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 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>
Petr Machata157cc4d2012-04-04 19:00:34 +02009#include <search.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010010#include <stdint.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020011#include <stdio.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010012#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 Machatae67635d2012-03-21 03:37:39 +010025#ifndef ARCH_HAVE_LTELF_DATA
26int
Petr Machatae0615ab2012-04-17 05:17:48 +020027arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010028{
29 return 0;
30}
Petr Machatac67a6e62012-03-28 02:39:49 +020031
32void
33arch_elf_destroy(struct ltelf *lte)
34{
35}
Petr Machatae67635d2012-03-21 03:37:39 +010036#endif
37
Petr Machatae6523e62012-03-24 04:54:06 +010038int
39default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020040 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010041 struct library_symbol **ret)
42{
43 char *name = strdup(a_name);
44 if (name == NULL) {
45 fail:
46 free(name);
47 return -1;
48 }
49
Petr Machata1be22912012-03-27 03:11:33 +020050 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010051
52 struct library_symbol *libsym = malloc(sizeof(*libsym));
53 if (libsym == NULL)
54 goto fail;
55
Petr Machataea8eb9a2012-04-17 01:32:07 +020056 /* XXX The double cast should be removed when
57 * target_address_t becomes integral type. */
58 target_address_t taddr = (target_address_t)
59 (uintptr_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010060
Petr Machatae8d90762012-04-15 04:28:31 +020061 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
62 free(libsym);
63 goto fail;
64 }
65
Petr Machatae6523e62012-03-24 04:54:06 +010066 *ret = libsym;
67 return 0;
68}
69
70#ifndef ARCH_HAVE_ADD_PLT_ENTRY
71enum plt_status
72arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020073 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010074 struct library_symbol **ret)
75{
76 return plt_default;
77}
78#endif
79
Petr Machatae67635d2012-03-21 03:37:39 +010080Elf_Data *
81elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020082{
83 Elf_Data *data = elf_getdata(scn, NULL);
84 if (data == NULL || elf_getdata(scn, data) != NULL
85 || data->d_off || data->d_size != shdr->sh_size)
86 return NULL;
87 return data;
88}
89
Petr Machatae67635d2012-03-21 03:37:39 +010090static int
Petr Machataffd5aab2012-03-24 02:03:33 +010091elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
92 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
93 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020094{
95 int i;
96 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
97 Elf_Scn *scn;
98 GElf_Shdr shdr;
99
100 scn = elf_getscn(lte->elf, i);
101 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
102 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100103 return -1;
104 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100105 if (predicate(scn, &shdr, data)) {
106 *tgt_sec = scn;
107 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200108 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100109 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200110 }
Petr Machatae67635d2012-03-21 03:37:39 +0100111 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100112
113}
114
115static int
116inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
117{
118 GElf_Addr addr = *(GElf_Addr *)data;
119 return addr >= shdr->sh_addr
120 && addr < shdr->sh_addr + shdr->sh_size;
121}
122
123int
124elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
125 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
126{
127 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
128 &inside_p, &addr);
129}
130
131static int
132type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
133{
134 GElf_Word type = *(GElf_Word *)data;
135 return shdr->sh_type == type;
136}
137
138int
139elf_get_section_type(struct ltelf *lte, GElf_Word type,
140 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
141{
142 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
143 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100144}
145
146static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200147need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100148{
149 assert(data != NULL);
150 if (data->d_size < size || offset > data->d_size - size) {
151 debug(1, "Not enough data to read %zd-byte value"
152 " at offset %zd.", size, offset);
153 return -1;
154 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200155 return 0;
156}
157
Petr Machatae67635d2012-03-21 03:37:39 +0100158#define DEF_READER(NAME, SIZE) \
159 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200160 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100161 { \
162 if (!need_data(data, offset, SIZE / 8) < 0) \
163 return -1; \
164 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200165 if (data->d_buf == NULL) /* NODATA section */ { \
166 *retp = 0; \
167 return 0; \
168 } \
169 \
Petr Machatae67635d2012-03-21 03:37:39 +0100170 union { \
171 uint##SIZE##_t dst; \
172 char buf[0]; \
173 } u; \
174 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
175 *retp = u.dst; \
176 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200177 }
178
Petr Machatae67635d2012-03-21 03:37:39 +0100179DEF_READER(elf_read_u16, 16)
180DEF_READER(elf_read_u32, 32)
181DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200182
Petr Machatae67635d2012-03-21 03:37:39 +0100183#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200184
Petr Machata1974dbc2011-08-19 18:58:01 +0200185int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200186open_elf(struct ltelf *lte, const char *filename)
187{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100188 lte->fd = open(filename, O_RDONLY);
189 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200190 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200191
Petr Machata02bd9ec2011-09-21 17:38:59 +0200192 elf_version(EV_CURRENT);
193
Juan Cespedesd914a202004-11-10 00:15:33 +0100194#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200196#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200198#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200199
Petr Machatacc0e1e42012-04-25 13:42:07 +0200200 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
201 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
202 exit(EXIT_FAILURE);
203 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200204
Petr Machatacc0e1e42012-04-25 13:42:07 +0200205 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
206 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
207 filename, elf_errmsg(-1));
208 exit(EXIT_FAILURE);
209 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200210
Petr Machatacc0e1e42012-04-25 13:42:07 +0200211 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
212 fprintf(stderr, "\"%s\" is neither an ELF executable"
213 " nor a shared library\n", filename);
214 exit(EXIT_FAILURE);
215 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200216
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
218 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100219#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
221 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100222#endif
223#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
225 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100226#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200227 ) {
228 fprintf(stderr,
229 "\"%s\" is ELF from incompatible architecture\n",
230 filename);
231 exit(EXIT_FAILURE);
232 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100233
Petr Machata02bd9ec2011-09-21 17:38:59 +0200234 return 0;
235}
236
Petr Machatacc0e1e42012-04-25 13:42:07 +0200237static void
238read_symbol_table(struct ltelf *lte, const char *filename,
239 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
240 Elf_Data **datap, size_t *countp, const char **strsp)
241{
242 *datap = elf_getdata(scn, NULL);
243 *countp = shdr->sh_size / shdr->sh_entsize;
244 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
245 && options.static_filter != NULL) {
246 fprintf(stderr, "Couldn't get data of section"
247 " %s from \"%s\": %s\n",
248 name, filename, elf_errmsg(-1));
249 exit(EXIT_FAILURE);
250 }
251
252 scn = elf_getscn(lte->elf, shdr->sh_link);
253 GElf_Shdr shdr2;
254 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
255 fprintf(stderr, "Couldn't get header of section"
256 " #%d from \"%s\": %s\n",
257 shdr2.sh_link, filename, elf_errmsg(-1));
258 exit(EXIT_FAILURE);
259 }
260
261 Elf_Data *data = elf_getdata(scn, NULL);
262 if (data == NULL || elf_getdata(scn, data) != NULL
263 || shdr2.sh_size != data->d_size || data->d_off) {
264 fprintf(stderr, "Couldn't get data of section"
265 " #%d from \"%s\": %s\n",
266 shdr2.sh_link, filename, elf_errmsg(-1));
267 exit(EXIT_FAILURE);
268 }
269
270 *strsp = data->d_buf;
271}
272
Petr Machatae67635d2012-03-21 03:37:39 +0100273static int
274do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200276 int i;
277 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100278 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200279
280 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
281 debug(1, "Reading ELF from %s...", filename);
282
283 if (open_elf(lte, filename) < 0)
284 return -1;
285
Petr Machatab120fdf2012-03-21 05:05:46 +0100286 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100287 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100288 GElf_Phdr phdr;
289 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
290 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200291 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100292 break;
293 }
294 }
295 }
296
Petr Machatab120fdf2012-03-21 05:05:46 +0100297 if (lte->base_addr == 0) {
298 fprintf(stderr, "Couldn't determine base address of %s\n",
299 filename);
300 return -1;
301 }
302
303 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100304 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
307 Elf_Scn *scn;
308 GElf_Shdr shdr;
309 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100310
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100311 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200312 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
313 fprintf(stderr, "Couldn't get section #%d from"
314 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
315 exit(EXIT_FAILURE);
316 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100317
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100318 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200319 if (name == NULL) {
320 fprintf(stderr, "Couldn't get name of section #%d from"
321 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
322 exit(EXIT_FAILURE);
323 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100324
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100325 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200326 read_symbol_table(lte, filename,
327 scn, &shdr, name, &lte->symtab,
328 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100329
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200331 read_symbol_table(lte, filename,
332 scn, &shdr, name, &lte->dynsym,
333 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100334
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 } else if (shdr.sh_type == SHT_DYNAMIC) {
336 Elf_Data *data;
337 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100338
Joe Damato87f4f582010-11-08 15:47:36 -0800339 lte->dyn_addr = shdr.sh_addr;
340 lte->dyn_sz = shdr.sh_size;
341
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100342 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200343 if (data == NULL || elf_getdata(scn, data) != NULL) {
344 fprintf(stderr, "Couldn't get .dynamic data"
345 " from \"%s\": %s\n",
346 filename, strerror(errno));
347 exit(EXIT_FAILURE);
348 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100349
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
351 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100352
Petr Machatacc0e1e42012-04-25 13:42:07 +0200353 if (gelf_getdyn(data, j, &dyn) == NULL) {
354 fprintf(stderr, "Couldn't get .dynamic"
355 " data from \"%s\": %s\n",
356 filename, strerror(errno));
357 exit(EXIT_FAILURE);
358 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100359 if (dyn.d_tag == DT_JMPREL)
360 relplt_addr = dyn.d_un.d_ptr;
361 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100362 lte->relplt_size = dyn.d_un.d_val;
363 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100364 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100366 } else if (shdr.sh_type == SHT_PROGBITS
367 || shdr.sh_type == SHT_NOBITS) {
368 if (strcmp(name, ".plt") == 0) {
369 lte->plt_addr = shdr.sh_addr;
370 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100371 lte->plt_data = elf_loaddata(scn, &shdr);
372 if (lte->plt_data == NULL)
373 fprintf(stderr,
374 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200375 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100376 }
377#ifdef ARCH_SUPPORTS_OPD
378 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200379 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 lte->opd_size = shdr.sh_size;
381 lte->opd = elf_rawdata(scn, NULL);
382 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100383#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100385 }
386
Petr Machatacc0e1e42012-04-25 13:42:07 +0200387 if (lte->dynsym == NULL || lte->dynstr == NULL) {
388 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
389 filename);
390 exit(EXIT_FAILURE);
391 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100392
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100393 if (!relplt_addr || !lte->plt_addr) {
394 debug(1, "%s has no PLT relocations", filename);
395 lte->relplt = NULL;
396 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100397 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200398 debug(1, "%s has unknown PLT size", filename);
399 lte->relplt = NULL;
400 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200402
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100403 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
404 Elf_Scn *scn;
405 GElf_Shdr shdr;
406
407 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200408 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
409 fprintf(stderr, "Couldn't get section header"
410 " from \"%s\": %s\n",
411 filename, elf_errmsg(-1));
412 exit(EXIT_FAILURE);
413 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100414 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
Petr Machatacc0e1e42012-04-25 13:42:07 +0200420 || elf_getdata(scn, lte->relplt) != NULL) {
421 fprintf(stderr, "Couldn't get .rel*.plt"
422 " data from \"%s\": %s\n",
423 filename, elf_errmsg(-1));
424 exit(EXIT_FAILURE);
425 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 break;
427 }
428 }
429
Petr Machatacc0e1e42012-04-25 13:42:07 +0200430 if (i == lte->ehdr.e_shnum) {
431 fprintf(stderr,
432 "Couldn't find .rel*.plt section in \"%s\"\n",
433 filename);
434 exit(EXIT_FAILURE);
435 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436
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 Machata1974dbc2011-08-19 18:58:01 +0200443 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100444}
445
Petr Machata2b46cfc2012-02-18 11:17:29 +0100446/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800447void
Juan Cespedesf1350522008-12-16 18:19:58 +0100448do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200449 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100450 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100451 elf_end(lte->elf);
452 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200453}
454
Petr Machatab5f80ac2012-04-04 01:46:18 +0200455static int
456populate_plt(struct Process *proc, const char *filename,
457 struct ltelf *lte, struct library *lib)
458{
459 size_t i;
460 for (i = 0; i < lte->relplt_count; ++i) {
461 GElf_Rel rel;
462 GElf_Rela rela;
463 GElf_Sym sym;
464 void *ret;
465
466 if (lte->relplt->d_type == ELF_T_REL) {
467 ret = gelf_getrel(lte->relplt, i, &rel);
468 rela.r_offset = rel.r_offset;
469 rela.r_info = rel.r_info;
470 rela.r_addend = 0;
471 } else {
472 ret = gelf_getrela(lte->relplt, i, &rela);
473 }
474
475 if (ret == NULL
476 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
477 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
Petr Machatacc0e1e42012-04-25 13:42:07 +0200478 &sym) == NULL) {
479 fprintf(stderr,
480 "Couldn't get relocation from \"%s\": %s\n",
481 filename, elf_errmsg(-1));
482 exit(EXIT_FAILURE);
483 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200484
485 char const *name = lte->dynstr + sym.st_name;
486
487 if (!filter_matches_symbol(options.plt_filter, name, lib))
488 continue;
489
Petr Machata218c5ff2012-04-15 04:22:39 +0200490 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200491 switch (arch_elf_add_plt_entry(proc, lte, name,
492 &rela, i, &libsym)) {
493 case plt_default:
494 if (default_elf_add_plt_entry(proc, lte, name,
495 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200496 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200497 case plt_fail:
498 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200499 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200500 case plt_ok:
501 if (libsym != NULL)
502 library_add_symbol(lib, libsym);
503 }
504 }
505 return 0;
506}
507
Petr Machata157cc4d2012-04-04 19:00:34 +0200508/* When -x rules result in request to trace several aliases, we only
509 * want to add such symbol once. The only way that those symbols
510 * differ in is their name, e.g. in glibc you have __GI___libc_free,
511 * __cfree, __free, __libc_free, cfree and free all defined on the
512 * same address. So instead we keep this unique symbol struct for
513 * each address, and replace name in libsym with a shorter variant if
514 * we find it. */
515struct unique_symbol {
516 target_address_t addr;
517 struct library_symbol *libsym;
518};
519
520static int
521unique_symbol_cmp(const void *key, const void *val)
522{
523 const struct unique_symbol *sym_key = key;
524 const struct unique_symbol *sym_val = val;
525 return sym_key->addr != sym_val->addr;
526}
527
Petr Machatada3edbf2012-04-04 02:20:21 +0200528static int
529populate_this_symtab(struct Process *proc, const char *filename,
530 struct ltelf *lte, struct library *lib,
531 Elf_Data *symtab, const char *strtab, size_t size)
532{
Petr Machata157cc4d2012-04-04 19:00:34 +0200533 /* Using sorted array would be arguably better, but this
534 * should be well enough for the number of symbols that we
535 * typically deal with. */
536 size_t num_symbols = 0;
537 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
538 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200539 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
540 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200541 return -1;
542 }
543
Petr Machata40cc53b2012-04-07 01:25:38 +0200544 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200545 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200546 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
547 Elf_Scn *scn = elf_getscn(lte->elf, i);
548 if (scn == NULL)
549 continue;
550 GElf_Shdr shdr;
551 if (gelf_getshdr(scn, &shdr) == NULL)
552 continue;
553 secflags[i] = shdr.sh_flags;
554 }
555
556 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200557 for (i = 0; i < size; ++i) {
558 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200559 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200560 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200561 fprintf(stderr,
562 "couldn't get symbol #%zd from %s: %s\n",
563 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200564 continue;
565 }
566
Petr Machata4de6b6b2012-04-04 14:06:09 +0200567 /* XXX support IFUNC as well. */
568 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
569 || sym.st_value == 0)
570 continue;
571
Petr Machata2bbeac42012-04-30 20:48:34 +0200572 const char *orig_name = strtab + sym.st_name;
573 const char *version = strchr(orig_name, '@');
574 size_t len = version != NULL ? (assert(version > orig_name),
575 (size_t)(version - orig_name))
576 : strlen(orig_name);
577 char name[len + 1];
578 memcpy(name, orig_name, len);
579 name[len] = 0;
580
Petr Machatada3edbf2012-04-04 02:20:21 +0200581 if (!filter_matches_symbol(options.static_filter, name, lib))
582 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200583
Petr Machataea8eb9a2012-04-17 01:32:07 +0200584 target_address_t addr = (target_address_t)
585 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatada3edbf2012-04-04 02:20:21 +0200586 target_address_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200587
588 /* On arches that support OPD, the value of typical
589 * function symbol will be a pointer to .opd, but some
590 * will point directly to .text. We don't want to
591 * translate those. */
592 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
593 naddr = addr;
594 } else if (arch_translate_address(proc, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200595 fprintf(stderr,
596 "couldn't translate address of %s@%s: %s\n",
597 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200598 continue;
599 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200600
601 /* If the translation actually took place, and wasn't
602 * a no-op, then bias again. XXX We shouldn't apply
603 * second bias for libraries that were open at the
604 * time that we attached. In fact what we should do
605 * is look at each translated address, whether it
606 * falls into a SHF_EXECINSTR section. If it does,
607 * it's most likely already translated. */
Petr Machatada3edbf2012-04-04 02:20:21 +0200608 if (addr != naddr)
609 naddr += lte->bias;
610
Petr Machata3840f682012-04-06 16:05:41 +0200611 char *full_name;
612 if (lib->type != LT_LIBTYPE_MAIN) {
613 full_name = malloc(strlen(name) + 1 + lib_len + 1);
614 if (full_name == NULL)
615 goto fail;
616 sprintf(full_name, "%s@%s", name, lib->soname);
617 } else {
618 full_name = strdup(name);
619 if (full_name == NULL)
620 goto fail;
621 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200622
Petr Machata157cc4d2012-04-04 19:00:34 +0200623 /* Look whether we already have a symbol for this
624 * address. If not, add this one. */
625 struct unique_symbol key = { naddr, NULL };
626 struct unique_symbol *unique
627 = lsearch(&key, symbols, &num_symbols,
628 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200629
Petr Machata157cc4d2012-04-04 19:00:34 +0200630 if (unique->libsym == NULL) {
631 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200632 if (libsym == NULL
633 || library_symbol_init(libsym, naddr, full_name,
634 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200635 --num_symbols;
636 goto fail;
637 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200638 unique->libsym = libsym;
639 unique->addr = naddr;
640
641 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
642 library_symbol_set_name(unique->libsym, full_name, 1);
643
644 } else {
645 free(full_name);
646 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200647 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200648
649 for (i = 0; i < num_symbols; ++i) {
650 assert(symbols[i].libsym != NULL);
651 library_add_symbol(lib, symbols[i].libsym);
652 }
653
654 free(symbols);
655
Petr Machatada3edbf2012-04-04 02:20:21 +0200656 return 0;
657}
658
659static int
660populate_symtab(struct Process *proc, const char *filename,
661 struct ltelf *lte, struct library *lib)
662{
663 if (lte->symtab != NULL && lte->strtab != NULL)
664 return populate_this_symtab(proc, filename, lte, lib,
665 lte->symtab, lte->strtab,
666 lte->symtab_count);
667 else
668 return populate_this_symtab(proc, filename, lte, lib,
669 lte->dynsym, lte->dynstr,
670 lte->dynsym_count);
671}
672
Petr Machatab5f80ac2012-04-04 01:46:18 +0200673int
674ltelf_read_library(struct library *lib, struct Process *proc,
675 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100676{
Petr Machata29add4f2012-02-18 16:38:05 +0100677 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100678 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200679 return -1;
Petr Machatae0615ab2012-04-17 05:17:48 +0200680 if (arch_elf_init(&lte, lib) < 0) {
681 fprintf(stderr, "Backend initialization failed.\n");
682 return -1;
683 }
684
Petr Machatae67635d2012-03-21 03:37:39 +0100685 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800686
Petr Machatab5f80ac2012-04-04 01:46:18 +0200687 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200688 if (lib == NULL)
689 goto fail;
690
691 /* Note that we set soname and pathname as soon as they are
692 * allocated, so in case of further errors, this get released
693 * when LIB is release, which should happen in the caller when
694 * we return error. */
695
696 if (lib->pathname == NULL) {
697 char *pathname = strdup(filename);
698 if (pathname == NULL)
699 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200700 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800701 }
702
Petr Machata0b55b582012-04-02 00:38:46 +0200703 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200704 char *soname = strdup(lte.soname);
705 if (soname == NULL)
706 goto fail;
707 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200708 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200709 const char *soname = rindex(lib->pathname, '/') + 1;
710 if (soname == NULL)
711 soname = lib->pathname;
712 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200713 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700714
Petr Machataea8eb9a2012-04-17 01:32:07 +0200715 /* XXX The double cast should be removed when
716 * target_address_t becomes integral type. */
717 target_address_t entry = (target_address_t)(uintptr_t)lte.entry_addr;
Petr Machata49275b02012-04-03 12:38:51 +0200718 if (arch_translate_address(proc, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100719 goto fail;
720
Petr Machataea8eb9a2012-04-17 01:32:07 +0200721 /* XXX The double cast should be removed when
722 * target_address_t becomes integral type. */
723 lib->base = (target_address_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100724 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200725 /* XXX The double cast should be removed when
726 * target_address_t becomes integral type. */
727 lib->dyn_addr = (target_address_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100728
Petr Machatab5f80ac2012-04-04 01:46:18 +0200729 if (filter_matches_library(options.plt_filter, lib)
730 && populate_plt(proc, filename, &lte, lib) < 0)
731 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800732
Petr Machatada3edbf2012-04-04 02:20:21 +0200733 if (filter_matches_library(options.static_filter, lib)
734 && populate_symtab(proc, filename, &lte, lib) < 0)
735 goto fail;
736
Petr Machata2b46cfc2012-02-18 11:17:29 +0100737done:
738 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200739 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200740
741fail:
742 status = -1;
743 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100744}
Petr Machatae84fa002012-02-07 13:43:03 +0100745
Petr Machata2b46cfc2012-02-18 11:17:29 +0100746struct library *
747ltelf_read_main_binary(struct Process *proc, const char *path)
748{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200749 struct library *lib = malloc(sizeof(*lib));
750 if (lib == NULL)
751 return NULL;
752 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200753 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200754
Petr Machatafc6ff182012-04-04 13:11:50 +0200755 /* There is a race between running the process and reading its
756 * binary for internal consumption. So open the binary from
757 * the /proc filesystem. XXX Note that there is similar race
758 * for libraries, but there we don't have a nice answer like
759 * that. Presumably we could read the DSOs from the process
760 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100761 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200762 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
763 library_destroy(lib);
764 free(lib);
765 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200766 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200767
Petr Machata2b46cfc2012-02-18 11:17:29 +0100768 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200769}