blob: e6cf253e40655448e09d5344bff66af1ebbe3879 [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 Machatae8d90762012-04-15 04:28:31 +020063 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
64 free(libsym);
65 goto fail;
66 }
67
Petr Machatae6523e62012-03-24 04:54:06 +010068 *ret = libsym;
69 return 0;
70}
71
72#ifndef ARCH_HAVE_ADD_PLT_ENTRY
73enum plt_status
74arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020075 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010076 struct library_symbol **ret)
77{
78 return plt_default;
79}
80#endif
81
Petr Machatae67635d2012-03-21 03:37:39 +010082Elf_Data *
83elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +020084{
85 Elf_Data *data = elf_getdata(scn, NULL);
86 if (data == NULL || elf_getdata(scn, data) != NULL
87 || data->d_off || data->d_size != shdr->sh_size)
88 return NULL;
89 return data;
90}
91
Petr Machatae67635d2012-03-21 03:37:39 +010092static int
Petr Machataffd5aab2012-03-24 02:03:33 +010093elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
94 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
95 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +020096{
97 int i;
98 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
99 Elf_Scn *scn;
100 GElf_Shdr shdr;
101
102 scn = elf_getscn(lte->elf, i);
103 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
104 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100105 return -1;
106 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100107 if (predicate(scn, &shdr, data)) {
108 *tgt_sec = scn;
109 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200110 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100111 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200112 }
Petr Machatae67635d2012-03-21 03:37:39 +0100113 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100114
115}
116
117static int
118inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
119{
120 GElf_Addr addr = *(GElf_Addr *)data;
121 return addr >= shdr->sh_addr
122 && addr < shdr->sh_addr + shdr->sh_size;
123}
124
125int
126elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
127 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
128{
129 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
130 &inside_p, &addr);
131}
132
133static int
134type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
135{
136 GElf_Word type = *(GElf_Word *)data;
137 return shdr->sh_type == type;
138}
139
140int
141elf_get_section_type(struct ltelf *lte, GElf_Word type,
142 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
143{
144 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
145 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100146}
147
148static int
149need_data(Elf_Data *data, size_t offset, size_t size)
150{
151 assert(data != NULL);
152 if (data->d_size < size || offset > data->d_size - size) {
153 debug(1, "Not enough data to read %zd-byte value"
154 " at offset %zd.", size, offset);
155 return -1;
156 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200157 return 0;
158}
159
Petr Machatae67635d2012-03-21 03:37:39 +0100160#define DEF_READER(NAME, SIZE) \
161 int \
162 NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \
163 { \
164 if (!need_data(data, offset, SIZE / 8) < 0) \
165 return -1; \
166 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200167 if (data->d_buf == NULL) /* NODATA section */ { \
168 *retp = 0; \
169 return 0; \
170 } \
171 \
Petr Machatae67635d2012-03-21 03:37:39 +0100172 union { \
173 uint##SIZE##_t dst; \
174 char buf[0]; \
175 } u; \
176 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
177 *retp = u.dst; \
178 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200179 }
180
Petr Machatae67635d2012-03-21 03:37:39 +0100181DEF_READER(elf_read_u16, 16)
182DEF_READER(elf_read_u32, 32)
183DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200184
Petr Machatae67635d2012-03-21 03:37:39 +0100185#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200186
Petr Machata1974dbc2011-08-19 18:58:01 +0200187int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200188open_elf(struct ltelf *lte, const char *filename)
189{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100190 lte->fd = open(filename, O_RDONLY);
191 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200192 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200193
Petr Machata02bd9ec2011-09-21 17:38:59 +0200194 elf_version(EV_CURRENT);
195
Juan Cespedesd914a202004-11-10 00:15:33 +0100196#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200198#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100199 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200200#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200201
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100202 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
203 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200204
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
206 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
207 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200208
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
210 error(EXIT_FAILURE, 0,
211 "\"%s\" is not an ELF executable nor shared library",
212 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200213
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
215 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100216#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
218 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100219#endif
220#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
222 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100223#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 )
225 error(EXIT_FAILURE, 0,
226 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100227
Petr Machata02bd9ec2011-09-21 17:38:59 +0200228 return 0;
229}
230
Petr Machatae67635d2012-03-21 03:37:39 +0100231static int
232do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100233{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200234 int i;
235 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200237
238 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
239 debug(1, "Reading ELF from %s...", filename);
240
241 if (open_elf(lte, filename) < 0)
242 return -1;
243
Petr Machatab120fdf2012-03-21 05:05:46 +0100244 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100245 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 GElf_Phdr phdr;
247 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
248 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200249 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100250 break;
251 }
252 }
253 }
254
Petr Machatab120fdf2012-03-21 05:05:46 +0100255 if (lte->base_addr == 0) {
256 fprintf(stderr, "Couldn't determine base address of %s\n",
257 filename);
258 return -1;
259 }
260
261 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100262 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100264 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
265 Elf_Scn *scn;
266 GElf_Shdr shdr;
267 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100268
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 scn = elf_getscn(lte->elf, i);
270 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
271 error(EXIT_FAILURE, 0,
272 "Couldn't get section header from \"%s\"",
273 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100274
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
276 if (name == NULL)
277 error(EXIT_FAILURE, 0,
278 "Couldn't get section header from \"%s\"",
279 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100280
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100281 if (shdr.sh_type == SHT_SYMTAB) {
282 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100283
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 lte->symtab = elf_getdata(scn, NULL);
285 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
286 if ((lte->symtab == NULL
287 || elf_getdata(scn, lte->symtab) != NULL)
Petr Machatada3edbf2012-04-04 02:20:21 +0200288 && options.static_filter != NULL)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100289 error(EXIT_FAILURE, 0,
290 "Couldn't get .symtab data from \"%s\"",
291 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100292
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100293 scn = elf_getscn(lte->elf, shdr.sh_link);
294 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
295 error(EXIT_FAILURE, 0,
296 "Couldn't get section header from \"%s\"",
297 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100298
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 data = elf_getdata(scn, NULL);
300 if (data == NULL || elf_getdata(scn, data) != NULL
301 || shdr.sh_size != data->d_size || data->d_off)
302 error(EXIT_FAILURE, 0,
303 "Couldn't get .strtab data from \"%s\"",
304 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100305
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 lte->strtab = data->d_buf;
307 } else if (shdr.sh_type == SHT_DYNSYM) {
308 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100309
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100310 lte->dynsym = elf_getdata(scn, NULL);
311 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
312 if (lte->dynsym == NULL
313 || elf_getdata(scn, lte->dynsym) != NULL)
314 error(EXIT_FAILURE, 0,
315 "Couldn't get .dynsym data from \"%s\"",
316 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100317
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100318 scn = elf_getscn(lte->elf, shdr.sh_link);
319 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
320 error(EXIT_FAILURE, 0,
321 "Couldn't get section header from \"%s\"",
322 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100323
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 data = elf_getdata(scn, NULL);
325 if (data == NULL || elf_getdata(scn, data) != NULL
326 || shdr.sh_size != data->d_size || data->d_off)
327 error(EXIT_FAILURE, 0,
328 "Couldn't get .dynstr data from \"%s\"",
329 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100330
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100331 lte->dynstr = data->d_buf;
332 } else if (shdr.sh_type == SHT_DYNAMIC) {
333 Elf_Data *data;
334 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100335
Joe Damato87f4f582010-11-08 15:47:36 -0800336 lte->dyn_addr = shdr.sh_addr;
337 lte->dyn_sz = shdr.sh_size;
338
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100339 data = elf_getdata(scn, NULL);
340 if (data == NULL || elf_getdata(scn, data) != NULL)
341 error(EXIT_FAILURE, 0,
342 "Couldn't get .dynamic data from \"%s\"",
343 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100344
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
346 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100347
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 if (gelf_getdyn(data, j, &dyn) == NULL)
349 error(EXIT_FAILURE, 0,
350 "Couldn't get .dynamic data from \"%s\"",
351 filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100352 if (dyn.d_tag == DT_JMPREL)
353 relplt_addr = dyn.d_un.d_ptr;
354 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100355 lte->relplt_size = dyn.d_un.d_val;
356 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100357 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100359 } else if (shdr.sh_type == SHT_PROGBITS
360 || shdr.sh_type == SHT_NOBITS) {
361 if (strcmp(name, ".plt") == 0) {
362 lte->plt_addr = shdr.sh_addr;
363 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100364 lte->plt_data = elf_loaddata(scn, &shdr);
365 if (lte->plt_data == NULL)
366 fprintf(stderr,
367 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200368 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100369 }
370#ifdef ARCH_SUPPORTS_OPD
371 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200372 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 lte->opd_size = shdr.sh_size;
374 lte->opd = elf_rawdata(scn, NULL);
375 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100376#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100377 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100378 }
379
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 if (lte->dynsym == NULL || lte->dynstr == NULL)
381 error(EXIT_FAILURE, 0,
382 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100383
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 if (!relplt_addr || !lte->plt_addr) {
385 debug(1, "%s has no PLT relocations", filename);
386 lte->relplt = NULL;
387 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100388 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200389 debug(1, "%s has unknown PLT size", filename);
390 lte->relplt = NULL;
391 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200393
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100394 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
395 Elf_Scn *scn;
396 GElf_Shdr shdr;
397
398 scn = elf_getscn(lte->elf, i);
399 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
400 error(EXIT_FAILURE, 0,
401 "Couldn't get section header from \"%s\"",
402 filename);
403 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100404 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 lte->relplt = elf_getdata(scn, NULL);
406 lte->relplt_count =
407 shdr.sh_size / shdr.sh_entsize;
408 if (lte->relplt == NULL
409 || elf_getdata(scn, lte->relplt) != NULL)
410 error(EXIT_FAILURE, 0,
411 "Couldn't get .rel*.plt data from \"%s\"",
412 filename);
413 break;
414 }
415 }
416
417 if (i == lte->ehdr.e_shnum)
418 error(EXIT_FAILURE, 0,
419 "Couldn't find .rel*.plt section in \"%s\"",
420 filename);
421
422 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
423 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100424
425 if (soname_offset != 0)
426 lte->soname = lte->dynstr + soname_offset;
427
Petr Machata644d6692012-03-24 02:06:48 +0100428 if (arch_elf_init(lte) < 0) {
429 fprintf(stderr, "Backend initialization failed.\n");
430 return -1;
431 }
432
Petr Machata1974dbc2011-08-19 18:58:01 +0200433 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100434}
435
Petr Machata2b46cfc2012-02-18 11:17:29 +0100436/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800437void
Juan Cespedesf1350522008-12-16 18:19:58 +0100438do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200439 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100440 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100441 elf_end(lte->elf);
442 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200443}
444
Petr Machatab5f80ac2012-04-04 01:46:18 +0200445static int
446populate_plt(struct Process *proc, const char *filename,
447 struct ltelf *lte, struct library *lib)
448{
449 size_t i;
450 for (i = 0; i < lte->relplt_count; ++i) {
451 GElf_Rel rel;
452 GElf_Rela rela;
453 GElf_Sym sym;
454 void *ret;
455
456 if (lte->relplt->d_type == ELF_T_REL) {
457 ret = gelf_getrel(lte->relplt, i, &rel);
458 rela.r_offset = rel.r_offset;
459 rela.r_info = rel.r_info;
460 rela.r_addend = 0;
461 } else {
462 ret = gelf_getrela(lte->relplt, i, &rela);
463 }
464
465 if (ret == NULL
466 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
467 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
468 &sym) == NULL)
469 error(EXIT_FAILURE, 0,
470 "Couldn't get relocation from \"%s\"",
471 filename);
472
473 char const *name = lte->dynstr + sym.st_name;
474
475 if (!filter_matches_symbol(options.plt_filter, name, lib))
476 continue;
477
Petr Machata218c5ff2012-04-15 04:22:39 +0200478 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200479 switch (arch_elf_add_plt_entry(proc, lte, name,
480 &rela, i, &libsym)) {
481 case plt_default:
482 if (default_elf_add_plt_entry(proc, lte, name,
483 &rela, i, &libsym) < 0)
484 case plt_fail:
485 return -1;
486 case plt_ok:
487 if (libsym != NULL)
488 library_add_symbol(lib, libsym);
489 }
490 }
491 return 0;
492}
493
Petr Machata157cc4d2012-04-04 19:00:34 +0200494/* When -x rules result in request to trace several aliases, we only
495 * want to add such symbol once. The only way that those symbols
496 * differ in is their name, e.g. in glibc you have __GI___libc_free,
497 * __cfree, __free, __libc_free, cfree and free all defined on the
498 * same address. So instead we keep this unique symbol struct for
499 * each address, and replace name in libsym with a shorter variant if
500 * we find it. */
501struct unique_symbol {
502 target_address_t addr;
503 struct library_symbol *libsym;
504};
505
506static int
507unique_symbol_cmp(const void *key, const void *val)
508{
509 const struct unique_symbol *sym_key = key;
510 const struct unique_symbol *sym_val = val;
511 return sym_key->addr != sym_val->addr;
512}
513
Petr Machatada3edbf2012-04-04 02:20:21 +0200514static int
515populate_this_symtab(struct Process *proc, const char *filename,
516 struct ltelf *lte, struct library *lib,
517 Elf_Data *symtab, const char *strtab, size_t size)
518{
Petr Machata157cc4d2012-04-04 19:00:34 +0200519 /* Using sorted array would be arguably better, but this
520 * should be well enough for the number of symbols that we
521 * typically deal with. */
522 size_t num_symbols = 0;
523 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
524 if (symbols == NULL) {
525 error(0, errno, "couldn't insert symbols for -x");
526 return -1;
527 }
528
Petr Machata40cc53b2012-04-07 01:25:38 +0200529 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200530 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200531 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
532 Elf_Scn *scn = elf_getscn(lte->elf, i);
533 if (scn == NULL)
534 continue;
535 GElf_Shdr shdr;
536 if (gelf_getshdr(scn, &shdr) == NULL)
537 continue;
538 secflags[i] = shdr.sh_flags;
539 }
540
541 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200542 for (i = 0; i < size; ++i) {
543 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200544 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200545 fail:
546 error(0, errno, "couldn't get symbol #%zd from %s: %s",
547 i, filename, elf_errmsg(-1));
548 continue;
549 }
550
Petr Machata4de6b6b2012-04-04 14:06:09 +0200551 /* XXX support IFUNC as well. */
552 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
553 || sym.st_value == 0)
554 continue;
555
Petr Machatada3edbf2012-04-04 02:20:21 +0200556 const char *name = strtab + sym.st_name;
557 if (!filter_matches_symbol(options.static_filter, name, lib))
558 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200559
560 target_address_t addr
561 = (target_address_t)(sym.st_value + lte->bias);
562 target_address_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200563
564 /* On arches that support OPD, the value of typical
565 * function symbol will be a pointer to .opd, but some
566 * will point directly to .text. We don't want to
567 * translate those. */
568 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
569 naddr = addr;
570 } else if (arch_translate_address(proc, addr, &naddr) < 0) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200571 error(0, errno, "couldn't translate address of %s@%s",
572 name, lib->soname);
573 continue;
574 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200575
576 /* If the translation actually took place, and wasn't
577 * a no-op, then bias again. XXX We shouldn't apply
578 * second bias for libraries that were open at the
579 * time that we attached. In fact what we should do
580 * is look at each translated address, whether it
581 * falls into a SHF_EXECINSTR section. If it does,
582 * it's most likely already translated. */
Petr Machatada3edbf2012-04-04 02:20:21 +0200583 if (addr != naddr)
584 naddr += lte->bias;
585
Petr Machata3840f682012-04-06 16:05:41 +0200586 char *full_name;
587 if (lib->type != LT_LIBTYPE_MAIN) {
588 full_name = malloc(strlen(name) + 1 + lib_len + 1);
589 if (full_name == NULL)
590 goto fail;
591 sprintf(full_name, "%s@%s", name, lib->soname);
592 } else {
593 full_name = strdup(name);
594 if (full_name == NULL)
595 goto fail;
596 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200597
Petr Machata157cc4d2012-04-04 19:00:34 +0200598 /* Look whether we already have a symbol for this
599 * address. If not, add this one. */
600 struct unique_symbol key = { naddr, NULL };
601 struct unique_symbol *unique
602 = lsearch(&key, symbols, &num_symbols,
603 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200604
Petr Machata157cc4d2012-04-04 19:00:34 +0200605 if (unique->libsym == NULL) {
606 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200607 if (libsym == NULL
608 || library_symbol_init(libsym, naddr, full_name,
609 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200610 --num_symbols;
611 goto fail;
612 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200613 unique->libsym = libsym;
614 unique->addr = naddr;
615
616 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
617 library_symbol_set_name(unique->libsym, full_name, 1);
618
619 } else {
620 free(full_name);
621 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200622 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200623
624 for (i = 0; i < num_symbols; ++i) {
625 assert(symbols[i].libsym != NULL);
626 library_add_symbol(lib, symbols[i].libsym);
627 }
628
629 free(symbols);
630
Petr Machatada3edbf2012-04-04 02:20:21 +0200631 return 0;
632}
633
634static int
635populate_symtab(struct Process *proc, const char *filename,
636 struct ltelf *lte, struct library *lib)
637{
638 if (lte->symtab != NULL && lte->strtab != NULL)
639 return populate_this_symtab(proc, filename, lte, lib,
640 lte->symtab, lte->strtab,
641 lte->symtab_count);
642 else
643 return populate_this_symtab(proc, filename, lte, lib,
644 lte->dynsym, lte->dynstr,
645 lte->dynsym_count);
646}
647
Petr Machatab5f80ac2012-04-04 01:46:18 +0200648int
649ltelf_read_library(struct library *lib, struct Process *proc,
650 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100651{
Petr Machata29add4f2012-02-18 16:38:05 +0100652 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100653 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200654 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100655 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800656
Petr Machatab5f80ac2012-04-04 01:46:18 +0200657 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200658 if (lib == NULL)
659 goto fail;
660
661 /* Note that we set soname and pathname as soon as they are
662 * allocated, so in case of further errors, this get released
663 * when LIB is release, which should happen in the caller when
664 * we return error. */
665
666 if (lib->pathname == NULL) {
667 char *pathname = strdup(filename);
668 if (pathname == NULL)
669 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200670 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800671 }
672
Petr Machata0b55b582012-04-02 00:38:46 +0200673 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200674 char *soname = strdup(lte.soname);
675 if (soname == NULL)
676 goto fail;
677 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200678 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200679 const char *soname = rindex(lib->pathname, '/') + 1;
680 if (soname == NULL)
681 soname = lib->pathname;
682 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200683 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700684
Petr Machatab120fdf2012-03-21 05:05:46 +0100685 target_address_t entry = (target_address_t)lte.entry_addr;
Petr Machata49275b02012-04-03 12:38:51 +0200686 if (arch_translate_address(proc, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100687 goto fail;
688
Petr Machata2b46cfc2012-02-18 11:17:29 +0100689 lib->base = (target_address_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100690 lib->entry = entry;
Petr Machata52dbfb12012-03-29 16:38:26 +0200691 lib->dyn_addr = (target_address_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100692
Petr Machatab5f80ac2012-04-04 01:46:18 +0200693 if (filter_matches_library(options.plt_filter, lib)
694 && populate_plt(proc, filename, &lte, lib) < 0)
695 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800696
Petr Machatada3edbf2012-04-04 02:20:21 +0200697 if (filter_matches_library(options.static_filter, lib)
698 && populate_symtab(proc, filename, &lte, lib) < 0)
699 goto fail;
700
Petr Machata2b46cfc2012-02-18 11:17:29 +0100701done:
702 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200703 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200704
705fail:
706 status = -1;
707 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100708}
Petr Machatae84fa002012-02-07 13:43:03 +0100709
Petr Machata2b46cfc2012-02-18 11:17:29 +0100710struct library *
711ltelf_read_main_binary(struct Process *proc, const char *path)
712{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200713 struct library *lib = malloc(sizeof(*lib));
714 if (lib == NULL)
715 return NULL;
716 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200717 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200718
Petr Machatafc6ff182012-04-04 13:11:50 +0200719 /* There is a race between running the process and reading its
720 * binary for internal consumption. So open the binary from
721 * the /proc filesystem. XXX Note that there is similar race
722 * for libraries, but there we don't have a nice answer like
723 * that. Presumably we could read the DSOs from the process
724 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100725 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200726 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
727 library_destroy(lib);
728 free(lib);
729 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200730 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200731
Petr Machata2b46cfc2012-02-18 11:17:29 +0100732 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200733}