blob: 56280930f7a5e81159ea6d7507ae660484baa414 [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
Petr Machata1be22912012-03-27 03:11:33 +020055 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010056
57 struct library_symbol *libsym = malloc(sizeof(*libsym));
58 if (libsym == NULL)
59 goto fail;
60
61 target_address_t taddr = (target_address_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010062
Petr Machata18c801c2012-04-07 01:24:08 +020063 library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC);
Petr Machatae6523e62012-03-24 04:54:06 +010064 *ret = libsym;
65 return 0;
66}
67
68#ifndef ARCH_HAVE_ADD_PLT_ENTRY
69enum plt_status
70arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020071 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010072 struct library_symbol **ret)
73{
74 return plt_default;
75}
76#endif
77
Petr Machatae67635d2012-03-21 03:37:39 +010078Elf_Data *
79elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020080{
81 Elf_Data *data = elf_getdata(scn, NULL);
82 if (data == NULL || elf_getdata(scn, data) != NULL
83 || data->d_off || data->d_size != shdr->sh_size)
84 return NULL;
85 return data;
86}
87
Petr Machatae67635d2012-03-21 03:37:39 +010088static int
Petr Machataffd5aab2012-03-24 02:03:33 +010089elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
90 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
91 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020092{
93 int i;
94 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
95 Elf_Scn *scn;
96 GElf_Shdr shdr;
97
98 scn = elf_getscn(lte->elf, i);
99 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
100 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100101 return -1;
102 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100103 if (predicate(scn, &shdr, data)) {
104 *tgt_sec = scn;
105 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200106 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100107 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200108 }
Petr Machatae67635d2012-03-21 03:37:39 +0100109 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100110
111}
112
113static int
114inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
115{
116 GElf_Addr addr = *(GElf_Addr *)data;
117 return addr >= shdr->sh_addr
118 && addr < shdr->sh_addr + shdr->sh_size;
119}
120
121int
122elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
123 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
124{
125 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
126 &inside_p, &addr);
127}
128
129static int
130type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
131{
132 GElf_Word type = *(GElf_Word *)data;
133 return shdr->sh_type == type;
134}
135
136int
137elf_get_section_type(struct ltelf *lte, GElf_Word type,
138 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
139{
140 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
141 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100142}
143
144static int
145need_data(Elf_Data *data, size_t offset, size_t size)
146{
147 assert(data != NULL);
148 if (data->d_size < size || offset > data->d_size - size) {
149 debug(1, "Not enough data to read %zd-byte value"
150 " at offset %zd.", size, offset);
151 return -1;
152 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200153 return 0;
154}
155
Petr Machatae67635d2012-03-21 03:37:39 +0100156#define DEF_READER(NAME, SIZE) \
157 int \
158 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
159 { \
160 if (!need_data(data, offset, SIZE / 8) < 0) \
161 return -1; \
162 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200163 if (data->d_buf == NULL) /* NODATA section */ { \
164 *retp = 0; \
165 return 0; \
166 } \
167 \
Petr Machatae67635d2012-03-21 03:37:39 +0100168 union { \
169 uint##SIZE##_t dst; \
170 char buf[0]; \
171 } u; \
172 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
173 *retp = u.dst; \
174 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200175 }
176
Petr Machatae67635d2012-03-21 03:37:39 +0100177DEF_READER(elf_read_u16, 16)
178DEF_READER(elf_read_u32, 32)
179DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200180
Petr Machatae67635d2012-03-21 03:37:39 +0100181#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200182
Petr Machata1974dbc2011-08-19 18:58:01 +0200183int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200184open_elf(struct ltelf *lte, const char *filename)
185{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 lte->fd = open(filename, O_RDONLY);
187 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200188 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200189
Petr Machata02bd9ec2011-09-21 17:38:59 +0200190 elf_version(EV_CURRENT);
191
Juan Cespedesd914a202004-11-10 00:15:33 +0100192#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200194#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200196#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200197
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
199 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200200
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
202 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
203 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200204
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
206 error(EXIT_FAILURE, 0,
207 "\"%s\" is not an ELF executable nor shared library",
208 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200209
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100210 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
211 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100212#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100213 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
214 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100215#endif
216#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
218 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100219#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 )
221 error(EXIT_FAILURE, 0,
222 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100223
Petr Machata02bd9ec2011-09-21 17:38:59 +0200224 return 0;
225}
226
Petr Machatae67635d2012-03-21 03:37:39 +0100227static int
228do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200230 int i;
231 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100232 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200233
234 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
235 debug(1, "Reading ELF from %s...", filename);
236
237 if (open_elf(lte, filename) < 0)
238 return -1;
239
Petr Machatab120fdf2012-03-21 05:05:46 +0100240 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100241 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100242 GElf_Phdr phdr;
243 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
244 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200245 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 break;
247 }
248 }
249 }
250
Petr Machatab120fdf2012-03-21 05:05:46 +0100251 if (lte->base_addr == 0) {
252 fprintf(stderr, "Couldn't determine base address of %s\n",
253 filename);
254 return -1;
255 }
256
257 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100258 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100260 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
261 Elf_Scn *scn;
262 GElf_Shdr shdr;
263 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100264
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 scn = elf_getscn(lte->elf, i);
266 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
267 error(EXIT_FAILURE, 0,
268 "Couldn't get section header from \"%s\"",
269 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100270
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100271 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
272 if (name == NULL)
273 error(EXIT_FAILURE, 0,
274 "Couldn't get section header from \"%s\"",
275 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100276
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100277 if (shdr.sh_type == SHT_SYMTAB) {
278 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100279
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100280 lte->symtab = elf_getdata(scn, NULL);
281 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
282 if ((lte->symtab == NULL
283 || elf_getdata(scn, lte->symtab) != NULL)
Petr Machatada3edbf2012-04-04 02:20:21 +0200284 && options.static_filter != NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100285 error(EXIT_FAILURE, 0,
286 "Couldn't get .symtab data from \"%s\"",
287 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100288
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100289 scn = elf_getscn(lte->elf, shdr.sh_link);
290 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
291 error(EXIT_FAILURE, 0,
292 "Couldn't get section header from \"%s\"",
293 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100294
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100295 data = elf_getdata(scn, NULL);
296 if (data == NULL || elf_getdata(scn, data) != NULL
297 || shdr.sh_size != data->d_size || data->d_off)
298 error(EXIT_FAILURE, 0,
299 "Couldn't get .strtab data from \"%s\"",
300 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100301
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100302 lte->strtab = data->d_buf;
303 } else if (shdr.sh_type == SHT_DYNSYM) {
304 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100305
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 lte->dynsym = elf_getdata(scn, NULL);
307 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
308 if (lte->dynsym == NULL
309 || elf_getdata(scn, lte->dynsym) != NULL)
310 error(EXIT_FAILURE, 0,
311 "Couldn't get .dynsym data from \"%s\"",
312 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100313
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100314 scn = elf_getscn(lte->elf, shdr.sh_link);
315 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
316 error(EXIT_FAILURE, 0,
317 "Couldn't get section header from \"%s\"",
318 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100319
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100320 data = elf_getdata(scn, NULL);
321 if (data == NULL || elf_getdata(scn, data) != NULL
322 || shdr.sh_size != data->d_size || data->d_off)
323 error(EXIT_FAILURE, 0,
324 "Couldn't get .dynstr data from \"%s\"",
325 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100326
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100327 lte->dynstr = data->d_buf;
328 } else if (shdr.sh_type == SHT_DYNAMIC) {
329 Elf_Data *data;
330 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100331
Joe Damato87f4f582010-11-08 15:47:36 -0800332 lte->dyn_addr = shdr.sh_addr;
333 lte->dyn_sz = shdr.sh_size;
334
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100335 data = elf_getdata(scn, NULL);
336 if (data == NULL || elf_getdata(scn, data) != NULL)
337 error(EXIT_FAILURE, 0,
338 "Couldn't get .dynamic data from \"%s\"",
339 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100340
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100341 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
342 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100343
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100344 if (gelf_getdyn(data, j, &dyn) == NULL)
345 error(EXIT_FAILURE, 0,
346 "Couldn't get .dynamic data from \"%s\"",
347 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 if (dyn.d_tag == DT_JMPREL)
349 relplt_addr = dyn.d_un.d_ptr;
350 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100351 lte->relplt_size = dyn.d_un.d_val;
352 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100353 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100355 } else if (shdr.sh_type == SHT_PROGBITS
356 || shdr.sh_type == SHT_NOBITS) {
357 if (strcmp(name, ".plt") == 0) {
358 lte->plt_addr = shdr.sh_addr;
359 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100360 lte->plt_data = elf_loaddata(scn, &shdr);
361 if (lte->plt_data == NULL)
362 fprintf(stderr,
363 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200364 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100365 }
366#ifdef ARCH_SUPPORTS_OPD
367 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200368 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 lte->opd_size = shdr.sh_size;
370 lte->opd = elf_rawdata(scn, NULL);
371 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100372#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100374 }
375
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 if (lte->dynsym == NULL || lte->dynstr == NULL)
377 error(EXIT_FAILURE, 0,
378 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100379
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 if (!relplt_addr || !lte->plt_addr) {
381 debug(1, "%s has no PLT relocations", filename);
382 lte->relplt = NULL;
383 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100384 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200385 debug(1, "%s has unknown PLT size", filename);
386 lte->relplt = NULL;
387 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200389
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
391 Elf_Scn *scn;
392 GElf_Shdr shdr;
393
394 scn = elf_getscn(lte->elf, i);
395 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
396 error(EXIT_FAILURE, 0,
397 "Couldn't get section header from \"%s\"",
398 filename);
399 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100400 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 lte->relplt = elf_getdata(scn, NULL);
402 lte->relplt_count =
403 shdr.sh_size / shdr.sh_entsize;
404 if (lte->relplt == NULL
405 || elf_getdata(scn, lte->relplt) != NULL)
406 error(EXIT_FAILURE, 0,
407 "Couldn't get .rel*.plt data from \"%s\"",
408 filename);
409 break;
410 }
411 }
412
413 if (i == lte->ehdr.e_shnum)
414 error(EXIT_FAILURE, 0,
415 "Couldn't find .rel*.plt section in \"%s\"",
416 filename);
417
418 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
419 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420
421 if (soname_offset != 0)
422 lte->soname = lte->dynstr + soname_offset;
423
Petr Machata644d6692012-03-24 02:06:48 +0100424 if (arch_elf_init(lte) < 0) {
425 fprintf(stderr, "Backend initialization failed.\n");
426 return -1;
427 }
428
Petr Machata1974dbc2011-08-19 18:58:01 +0200429 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100430}
431
Petr Machata2b46cfc2012-02-18 11:17:29 +0100432/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800433void
Juan Cespedesf1350522008-12-16 18:19:58 +0100434do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200435 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100436 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 elf_end(lte->elf);
438 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200439}
440
Petr Machatab5f80ac2012-04-04 01:46:18 +0200441static int
442populate_plt(struct Process *proc, const char *filename,
443 struct ltelf *lte, struct library *lib)
444{
445 size_t i;
446 for (i = 0; i < lte->relplt_count; ++i) {
447 GElf_Rel rel;
448 GElf_Rela rela;
449 GElf_Sym sym;
450 void *ret;
451
452 if (lte->relplt->d_type == ELF_T_REL) {
453 ret = gelf_getrel(lte->relplt, i, &rel);
454 rela.r_offset = rel.r_offset;
455 rela.r_info = rel.r_info;
456 rela.r_addend = 0;
457 } else {
458 ret = gelf_getrela(lte->relplt, i, &rela);
459 }
460
461 if (ret == NULL
462 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
463 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
464 &sym) == NULL)
465 error(EXIT_FAILURE, 0,
466 "Couldn't get relocation from \"%s\"",
467 filename);
468
469 char const *name = lte->dynstr + sym.st_name;
470
471 if (!filter_matches_symbol(options.plt_filter, name, lib))
472 continue;
473
Petr Machatab5f80ac2012-04-04 01:46:18 +0200474 struct library_symbol *libsym;
475 switch (arch_elf_add_plt_entry(proc, lte, name,
476 &rela, i, &libsym)) {
477 case plt_default:
478 if (default_elf_add_plt_entry(proc, lte, name,
479 &rela, i, &libsym) < 0)
480 case plt_fail:
481 return -1;
482 case plt_ok:
483 if (libsym != NULL)
484 library_add_symbol(lib, libsym);
485 }
486 }
487 return 0;
488}
489
Petr Machata157cc4d2012-04-04 19:00:34 +0200490/* When -x rules result in request to trace several aliases, we only
491 * want to add such symbol once. The only way that those symbols
492 * differ in is their name, e.g. in glibc you have __GI___libc_free,
493 * __cfree, __free, __libc_free, cfree and free all defined on the
494 * same address. So instead we keep this unique symbol struct for
495 * each address, and replace name in libsym with a shorter variant if
496 * we find it. */
497struct unique_symbol {
498 target_address_t addr;
499 struct library_symbol *libsym;
500};
501
502static int
503unique_symbol_cmp(const void *key, const void *val)
504{
505 const struct unique_symbol *sym_key = key;
506 const struct unique_symbol *sym_val = val;
507 return sym_key->addr != sym_val->addr;
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{
Petr Machata157cc4d2012-04-04 19:00:34 +0200515 /* Using sorted array would be arguably better, but this
516 * should be well enough for the number of symbols that we
517 * typically deal with. */
518 size_t num_symbols = 0;
519 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
520 if (symbols == NULL) {
521 error(0, errno, "couldn't insert symbols for -x");
522 return -1;
523 }
524
Petr Machata40cc53b2012-04-07 01:25:38 +0200525 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200526 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200527 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
528 Elf_Scn *scn = elf_getscn(lte->elf, i);
529 if (scn == NULL)
530 continue;
531 GElf_Shdr shdr;
532 if (gelf_getshdr(scn, &shdr) == NULL)
533 continue;
534 secflags[i] = shdr.sh_flags;
535 }
536
537 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200538 for (i = 0; i < size; ++i) {
539 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200540 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200541 fail:
542 error(0, errno, "couldn't get symbol #%zd from %s: %s",
543 i, filename, elf_errmsg(-1));
544 continue;
545 }
546
Petr Machata4de6b6b2012-04-04 14:06:09 +0200547 /* XXX support IFUNC as well. */
548 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
549 || sym.st_value == 0)
550 continue;
551
Petr Machatada3edbf2012-04-04 02:20:21 +0200552 const char *name = strtab + sym.st_name;
553 if (!filter_matches_symbol(options.static_filter, name, lib))
554 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200555
556 target_address_t addr
557 = (target_address_t)(sym.st_value + lte->bias);
558 target_address_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200559
560 /* On arches that support OPD, the value of typical
561 * function symbol will be a pointer to .opd, but some
562 * will point directly to .text. We don't want to
563 * translate those. */
564 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
565 naddr = addr;
566 } else if (arch_translate_address(proc, addr, &naddr) < 0) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200567 error(0, errno, "couldn't translate address of %s@%s",
568 name, lib->soname);
569 continue;
570 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200571
572 /* If the translation actually took place, and wasn't
573 * a no-op, then bias again. XXX We shouldn't apply
574 * second bias for libraries that were open at the
575 * time that we attached. In fact what we should do
576 * is look at each translated address, whether it
577 * falls into a SHF_EXECINSTR section. If it does,
578 * it's most likely already translated. */
Petr Machatada3edbf2012-04-04 02:20:21 +0200579 if (addr != naddr)
580 naddr += lte->bias;
581
Petr Machata3840f682012-04-06 16:05:41 +0200582 char *full_name;
583 if (lib->type != LT_LIBTYPE_MAIN) {
584 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 } else {
589 full_name = strdup(name);
590 if (full_name == NULL)
591 goto fail;
592 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200593
Petr Machata157cc4d2012-04-04 19:00:34 +0200594 /* Look whether we already have a symbol for this
595 * address. If not, add this one. */
596 struct unique_symbol key = { naddr, NULL };
597 struct unique_symbol *unique
598 = lsearch(&key, symbols, &num_symbols,
599 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200600
Petr Machata157cc4d2012-04-04 19:00:34 +0200601 if (unique->libsym == NULL) {
602 struct library_symbol *libsym = malloc(sizeof(*libsym));
603 if (libsym == NULL) {
604 --num_symbols;
605 goto fail;
606 }
607 library_symbol_init(libsym, naddr, full_name,
608 1, LS_TOPLT_NONE);
609 unique->libsym = libsym;
610 unique->addr = naddr;
611
612 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
613 library_symbol_set_name(unique->libsym, full_name, 1);
614
615 } else {
616 free(full_name);
617 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200618 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200619
620 for (i = 0; i < num_symbols; ++i) {
621 assert(symbols[i].libsym != NULL);
622 library_add_symbol(lib, symbols[i].libsym);
623 }
624
625 free(symbols);
626
Petr Machatada3edbf2012-04-04 02:20:21 +0200627 return 0;
628}
629
630static int
631populate_symtab(struct Process *proc, const char *filename,
632 struct ltelf *lte, struct library *lib)
633{
634 if (lte->symtab != NULL && lte->strtab != NULL)
635 return populate_this_symtab(proc, filename, lte, lib,
636 lte->symtab, lte->strtab,
637 lte->symtab_count);
638 else
639 return populate_this_symtab(proc, filename, lte, lib,
640 lte->dynsym, lte->dynstr,
641 lte->dynsym_count);
642}
643
Petr Machatab5f80ac2012-04-04 01:46:18 +0200644int
645ltelf_read_library(struct library *lib, struct Process *proc,
646 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100647{
Petr Machata29add4f2012-02-18 16:38:05 +0100648 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100649 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200650 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100651 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800652
Petr Machatab5f80ac2012-04-04 01:46:18 +0200653 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200654 if (lib == NULL)
655 goto fail;
656
657 /* Note that we set soname and pathname as soon as they are
658 * allocated, so in case of further errors, this get released
659 * when LIB is release, which should happen in the caller when
660 * we return error. */
661
662 if (lib->pathname == NULL) {
663 char *pathname = strdup(filename);
664 if (pathname == NULL)
665 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200666 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800667 }
668
Petr Machata0b55b582012-04-02 00:38:46 +0200669 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200670 char *soname = strdup(lte.soname);
671 if (soname == NULL)
672 goto fail;
673 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200674 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200675 const char *soname = rindex(lib->pathname, '/') + 1;
676 if (soname == NULL)
677 soname = lib->pathname;
678 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200679 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700680
Petr Machatab120fdf2012-03-21 05:05:46 +0100681 target_address_t entry = (target_address_t)lte.entry_addr;
Petr Machata49275b02012-04-03 12:38:51 +0200682 if (arch_translate_address(proc, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100683 goto fail;
684
Petr Machata2b46cfc2012-02-18 11:17:29 +0100685 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100686 lib->entry = entry;
Petr Machata52dbfb12012-03-29 16:38:26 +0200687 lib->dyn_addr = (target_address_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100688
Petr Machatab5f80ac2012-04-04 01:46:18 +0200689 if (filter_matches_library(options.plt_filter, lib)
690 && populate_plt(proc, filename, &lte, lib) < 0)
691 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800692
Petr Machatada3edbf2012-04-04 02:20:21 +0200693 if (filter_matches_library(options.static_filter, lib)
694 && populate_symtab(proc, filename, &lte, lib) < 0)
695 goto fail;
696
Petr Machata2b46cfc2012-02-18 11:17:29 +0100697done:
698 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200699 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200700
701fail:
702 status = -1;
703 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100704}
Petr Machatae84fa002012-02-07 13:43:03 +0100705
Petr Machata2b46cfc2012-02-18 11:17:29 +0100706struct library *
707ltelf_read_main_binary(struct Process *proc, const char *path)
708{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200709 struct library *lib = malloc(sizeof(*lib));
710 if (lib == NULL)
711 return NULL;
712 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200713 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200714
Petr Machatafc6ff182012-04-04 13:11:50 +0200715 /* There is a race between running the process and reading its
716 * binary for internal consumption. So open the binary from
717 * the /proc filesystem. XXX Note that there is similar race
718 * for libraries, but there we don't have a nice answer like
719 * that. Presumably we could read the DSOs from the process
720 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100721 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200722 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
723 library_destroy(lib);
724 free(lib);
725 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200726 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200727
Petr Machata2b46cfc2012-02-18 11:17:29 +0100728 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200729}