blob: 0f431add08dedcd30aae432bdc525e94274934a1 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2006,2010,2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch, CodeSourcery
5 * Copyright (C) 2010 Joe Damato
6 * Copyright (C) 1997,1998,2001,2004,2007,2008,2009 Juan Cespedes
7 * Copyright (C) 2006 Olaf Hering, SUSE Linux GmbH
8 * Copyright (C) 2006 Eric Vaitl, Cisco Systems, Inc.
9 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
10 * Copyright (C) 2006 Ian Wienand
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 */
27
Joe Damatof0bd98b2010-11-08 15:47:42 -080028#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020029
Petr Machata157cc4d2012-04-04 19:00:34 +020030#include <assert.h>
Andrey Zonov9d878c92012-08-05 00:19:51 +040031#ifdef __linux__
Juan Cespedesd914a202004-11-10 00:15:33 +010032#include <endian.h>
Andrey Zonov9d878c92012-08-05 00:19:51 +040033#endif
Juan Cespedes96935a91997-08-09 23:45:39 +020034#include <errno.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020035#include <fcntl.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010036#include <gelf.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -080037#include <inttypes.h>
Petr Machata157cc4d2012-04-04 19:00:34 +020038#include <search.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010039#include <stdint.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020040#include <stdio.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010041#include <stdlib.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020042#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010043#include <unistd.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020044
Petr Machata64262602012-01-07 03:41:36 +010045#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020046#include "filter.h"
47#include "library.h"
48#include "ltrace-elf.h"
49#include "proc.h"
50#include "debug.h"
51#include "options.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080052
Petr Machatae67635d2012-03-21 03:37:39 +010053#ifndef ARCH_HAVE_LTELF_DATA
54int
Petr Machatae0615ab2012-04-17 05:17:48 +020055arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010056{
57 return 0;
58}
Petr Machatac67a6e62012-03-28 02:39:49 +020059
60void
61arch_elf_destroy(struct ltelf *lte)
62{
63}
Petr Machatae67635d2012-03-21 03:37:39 +010064#endif
65
Petr Machata8bda1ee2012-09-26 01:52:23 +020066static int
Petr Machatae6523e62012-03-24 04:54:06 +010067default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020068 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010069 struct library_symbol **ret)
70{
71 char *name = strdup(a_name);
72 if (name == NULL) {
73 fail:
74 free(name);
75 return -1;
76 }
77
Petr Machata1be22912012-03-27 03:11:33 +020078 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010079
80 struct library_symbol *libsym = malloc(sizeof(*libsym));
81 if (libsym == NULL)
82 goto fail;
83
Petr Machataea8eb9a2012-04-17 01:32:07 +020084 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +020085 * arch_addr_t becomes integral type. */
86 arch_addr_t taddr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +020087 (uintptr_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010088
Petr Machatae8d90762012-04-15 04:28:31 +020089 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
90 free(libsym);
91 goto fail;
92 }
93
Petr Machatae6523e62012-03-24 04:54:06 +010094 *ret = libsym;
95 return 0;
96}
97
98#ifndef ARCH_HAVE_ADD_PLT_ENTRY
99enum plt_status
100arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +0200101 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +0100102 struct library_symbol **ret)
103{
104 return plt_default;
105}
106#endif
107
Petr Machatae67635d2012-03-21 03:37:39 +0100108Elf_Data *
109elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200110{
111 Elf_Data *data = elf_getdata(scn, NULL);
112 if (data == NULL || elf_getdata(scn, data) != NULL
113 || data->d_off || data->d_size != shdr->sh_size)
114 return NULL;
115 return data;
116}
117
Petr Machatae67635d2012-03-21 03:37:39 +0100118static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100119elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
120 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
121 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200122{
123 int i;
124 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
125 Elf_Scn *scn;
126 GElf_Shdr shdr;
127
128 scn = elf_getscn(lte->elf, i);
129 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
130 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100131 return -1;
132 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100133 if (predicate(scn, &shdr, data)) {
134 *tgt_sec = scn;
135 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200136 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100137 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200138 }
Petr Machatae67635d2012-03-21 03:37:39 +0100139 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100140
141}
142
143static int
144inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
145{
146 GElf_Addr addr = *(GElf_Addr *)data;
147 return addr >= shdr->sh_addr
148 && addr < shdr->sh_addr + shdr->sh_size;
149}
150
151int
152elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
153 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
154{
155 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
156 &inside_p, &addr);
157}
158
159static int
160type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
161{
162 GElf_Word type = *(GElf_Word *)data;
163 return shdr->sh_type == type;
164}
165
166int
167elf_get_section_type(struct ltelf *lte, GElf_Word type,
168 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
169{
170 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
171 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100172}
173
Petr Machata5b3e26a2012-04-30 20:53:22 +0200174struct section_named_data {
175 struct ltelf *lte;
176 const char *name;
177};
178
179static int
180name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
181{
182 struct section_named_data *data = d;
183 const char *name = elf_strptr(data->lte->elf,
184 data->lte->ehdr.e_shstrndx,
185 shdr->sh_name);
186 return strcmp(name, data->name) == 0;
187}
188
189int
190elf_get_section_named(struct ltelf *lte, const char *name,
191 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
192{
193 struct section_named_data data = {
194 .lte = lte,
195 .name = name,
196 };
197 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
198 &name_p, &data);
199}
200
Petr Machatae67635d2012-03-21 03:37:39 +0100201static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200202need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100203{
204 assert(data != NULL);
205 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200206 debug(1, "Not enough data to read %"PRId64"-byte value"
207 " at offset %"PRId64".", size, offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100208 return -1;
209 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200210 return 0;
211}
212
Petr Machatae67635d2012-03-21 03:37:39 +0100213#define DEF_READER(NAME, SIZE) \
214 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200215 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100216 { \
217 if (!need_data(data, offset, SIZE / 8) < 0) \
218 return -1; \
219 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200220 if (data->d_buf == NULL) /* NODATA section */ { \
221 *retp = 0; \
222 return 0; \
223 } \
224 \
Petr Machatae67635d2012-03-21 03:37:39 +0100225 union { \
226 uint##SIZE##_t dst; \
227 char buf[0]; \
228 } u; \
229 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
230 *retp = u.dst; \
231 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200232 }
233
Petr Machatae67635d2012-03-21 03:37:39 +0100234DEF_READER(elf_read_u16, 16)
235DEF_READER(elf_read_u32, 32)
236DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200237
Petr Machatae67635d2012-03-21 03:37:39 +0100238#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200239
Petr Machata1974dbc2011-08-19 18:58:01 +0200240int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200241open_elf(struct ltelf *lte, const char *filename)
242{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 lte->fd = open(filename, O_RDONLY);
244 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200245 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200246
Petr Machata02bd9ec2011-09-21 17:38:59 +0200247 elf_version(EV_CURRENT);
248
Juan Cespedesd914a202004-11-10 00:15:33 +0100249#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200251#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100252 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200253#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200254
Petr Machatacc0e1e42012-04-25 13:42:07 +0200255 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
256 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
257 exit(EXIT_FAILURE);
258 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200259
Petr Machatacc0e1e42012-04-25 13:42:07 +0200260 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
261 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
262 filename, elf_errmsg(-1));
263 exit(EXIT_FAILURE);
264 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200265
Petr Machatacc0e1e42012-04-25 13:42:07 +0200266 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
267 fprintf(stderr, "\"%s\" is neither an ELF executable"
268 " nor a shared library\n", filename);
269 exit(EXIT_FAILURE);
270 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200271
Petr Machataddd96a32012-05-17 23:35:26 +0200272 if (1
273#ifdef LT_ELF_MACHINE
274 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
275 || lte->ehdr.e_machine != LT_ELF_MACHINE)
276#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100277#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
279 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100280#endif
281#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
283 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100284#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200285 ) {
286 fprintf(stderr,
287 "\"%s\" is ELF from incompatible architecture\n",
288 filename);
289 exit(EXIT_FAILURE);
290 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100291
Petr Machata02bd9ec2011-09-21 17:38:59 +0200292 return 0;
293}
294
Petr Machatacc0e1e42012-04-25 13:42:07 +0200295static void
296read_symbol_table(struct ltelf *lte, const char *filename,
297 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
298 Elf_Data **datap, size_t *countp, const char **strsp)
299{
300 *datap = elf_getdata(scn, NULL);
301 *countp = shdr->sh_size / shdr->sh_entsize;
302 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
303 && options.static_filter != NULL) {
304 fprintf(stderr, "Couldn't get data of section"
305 " %s from \"%s\": %s\n",
306 name, filename, elf_errmsg(-1));
307 exit(EXIT_FAILURE);
308 }
309
310 scn = elf_getscn(lte->elf, shdr->sh_link);
311 GElf_Shdr shdr2;
312 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
313 fprintf(stderr, "Couldn't get header of section"
314 " #%d from \"%s\": %s\n",
315 shdr2.sh_link, filename, elf_errmsg(-1));
316 exit(EXIT_FAILURE);
317 }
318
319 Elf_Data *data = elf_getdata(scn, NULL);
320 if (data == NULL || elf_getdata(scn, data) != NULL
321 || shdr2.sh_size != data->d_size || data->d_off) {
322 fprintf(stderr, "Couldn't get data of section"
323 " #%d from \"%s\": %s\n",
324 shdr2.sh_link, filename, elf_errmsg(-1));
325 exit(EXIT_FAILURE);
326 }
327
328 *strsp = data->d_buf;
329}
330
Petr Machatae67635d2012-03-21 03:37:39 +0100331static int
332do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200334 int i;
335 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100336 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200337
338 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
339 debug(1, "Reading ELF from %s...", filename);
340
341 if (open_elf(lte, filename) < 0)
342 return -1;
343
Petr Machatab120fdf2012-03-21 05:05:46 +0100344 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100345 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100346 GElf_Phdr phdr;
347 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
348 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200349 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100350 break;
351 }
352 }
353 }
354
Petr Machatab120fdf2012-03-21 05:05:46 +0100355 if (lte->base_addr == 0) {
356 fprintf(stderr, "Couldn't determine base address of %s\n",
357 filename);
358 return -1;
359 }
360
361 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100362 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100363
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
365 Elf_Scn *scn;
366 GElf_Shdr shdr;
367 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100368
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200370 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
371 fprintf(stderr, "Couldn't get section #%d from"
372 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
373 exit(EXIT_FAILURE);
374 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100375
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200377 if (name == NULL) {
378 fprintf(stderr, "Couldn't get name of section #%d from"
379 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
380 exit(EXIT_FAILURE);
381 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100382
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100383 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200384 read_symbol_table(lte, filename,
385 scn, &shdr, name, &lte->symtab,
386 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100387
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200389 read_symbol_table(lte, filename,
390 scn, &shdr, name, &lte->dynsym,
391 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100392
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100393 } else if (shdr.sh_type == SHT_DYNAMIC) {
394 Elf_Data *data;
395 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100396
Joe Damato87f4f582010-11-08 15:47:36 -0800397 lte->dyn_addr = shdr.sh_addr;
398 lte->dyn_sz = shdr.sh_size;
399
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200401 if (data == NULL || elf_getdata(scn, data) != NULL) {
402 fprintf(stderr, "Couldn't get .dynamic data"
403 " from \"%s\": %s\n",
404 filename, strerror(errno));
405 exit(EXIT_FAILURE);
406 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100407
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
409 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100410
Petr Machatacc0e1e42012-04-25 13:42:07 +0200411 if (gelf_getdyn(data, j, &dyn) == NULL) {
412 fprintf(stderr, "Couldn't get .dynamic"
413 " data from \"%s\": %s\n",
414 filename, strerror(errno));
415 exit(EXIT_FAILURE);
416 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 if (dyn.d_tag == DT_JMPREL)
418 relplt_addr = dyn.d_un.d_ptr;
419 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100420 lte->relplt_size = dyn.d_un.d_val;
421 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100422 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100424 } else if (shdr.sh_type == SHT_PROGBITS
425 || shdr.sh_type == SHT_NOBITS) {
426 if (strcmp(name, ".plt") == 0) {
427 lte->plt_addr = shdr.sh_addr;
428 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100429 lte->plt_data = elf_loaddata(scn, &shdr);
430 if (lte->plt_data == NULL)
431 fprintf(stderr,
432 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200433 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100434 }
435#ifdef ARCH_SUPPORTS_OPD
436 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200437 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 lte->opd_size = shdr.sh_size;
439 lte->opd = elf_rawdata(scn, NULL);
440 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100441#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100442 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100443 }
444
Petr Machatacc0e1e42012-04-25 13:42:07 +0200445 if (lte->dynsym == NULL || lte->dynstr == NULL) {
446 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
447 filename);
448 exit(EXIT_FAILURE);
449 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100450
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100451 if (!relplt_addr || !lte->plt_addr) {
452 debug(1, "%s has no PLT relocations", filename);
453 lte->relplt = NULL;
454 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100455 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200456 debug(1, "%s has unknown PLT size", filename);
457 lte->relplt = NULL;
458 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200460
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100461 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
462 Elf_Scn *scn;
463 GElf_Shdr shdr;
464
465 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200466 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
467 fprintf(stderr, "Couldn't get section header"
468 " from \"%s\": %s\n",
469 filename, elf_errmsg(-1));
470 exit(EXIT_FAILURE);
471 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100472 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100473 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100474 lte->relplt = elf_getdata(scn, NULL);
475 lte->relplt_count =
476 shdr.sh_size / shdr.sh_entsize;
477 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200478 || elf_getdata(scn, lte->relplt) != NULL) {
479 fprintf(stderr, "Couldn't get .rel*.plt"
480 " data from \"%s\": %s\n",
481 filename, elf_errmsg(-1));
482 exit(EXIT_FAILURE);
483 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100484 break;
485 }
486 }
487
Petr Machatacc0e1e42012-04-25 13:42:07 +0200488 if (i == lte->ehdr.e_shnum) {
489 fprintf(stderr,
490 "Couldn't find .rel*.plt section in \"%s\"\n",
491 filename);
492 exit(EXIT_FAILURE);
493 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100494
495 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
496 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100497
498 if (soname_offset != 0)
499 lte->soname = lte->dynstr + soname_offset;
500
Petr Machata1974dbc2011-08-19 18:58:01 +0200501 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100502}
503
Petr Machata2b46cfc2012-02-18 11:17:29 +0100504/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800505void
Juan Cespedesf1350522008-12-16 18:19:58 +0100506do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200507 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100508 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100509 elf_end(lte->elf);
510 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200511}
512
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200513#ifndef ARCH_HAVE_GET_SYMINFO
514int
515arch_get_sym_info(struct ltelf *lte, const char *filename,
516 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
517{
518 int i = sym_index;
519 GElf_Rel rel;
520 void *ret;
521
522 if (lte->relplt->d_type == ELF_T_REL) {
523 ret = gelf_getrel(lte->relplt, i, &rel);
524 rela->r_offset = rel.r_offset;
525 rela->r_info = rel.r_info;
526 rela->r_addend = 0;
527 } else {
528 ret = gelf_getrela(lte->relplt, i, rela);
529 }
530
531 if (ret == NULL
532 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
533 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info),
534 sym) == NULL) {
535 fprintf(stderr,
536 "Couldn't get relocation from \"%s\": %s\n",
537 filename, elf_errmsg(-1));
538 exit(EXIT_FAILURE);
539 }
540
541 return 0;
542}
543#endif
544
Petr Machatab5f80ac2012-04-04 01:46:18 +0200545static int
546populate_plt(struct Process *proc, const char *filename,
547 struct ltelf *lte, struct library *lib)
548{
549 size_t i;
550 for (i = 0; i < lte->relplt_count; ++i) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200551 GElf_Rela rela;
552 GElf_Sym sym;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200553
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200554 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0)
555 continue; /* Skip this entry. */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200556
557 char const *name = lte->dynstr + sym.st_name;
558
559 if (!filter_matches_symbol(options.plt_filter, name, lib))
560 continue;
561
Petr Machata218c5ff2012-04-15 04:22:39 +0200562 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200563 switch (arch_elf_add_plt_entry(proc, lte, name,
564 &rela, i, &libsym)) {
565 case plt_default:
566 if (default_elf_add_plt_entry(proc, lte, name,
567 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200568 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200569 case plt_fail:
570 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200571 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200572 case plt_ok:
573 if (libsym != NULL)
574 library_add_symbol(lib, libsym);
575 }
576 }
577 return 0;
578}
579
Petr Machata157cc4d2012-04-04 19:00:34 +0200580/* When -x rules result in request to trace several aliases, we only
581 * want to add such symbol once. The only way that those symbols
582 * differ in is their name, e.g. in glibc you have __GI___libc_free,
583 * __cfree, __free, __libc_free, cfree and free all defined on the
584 * same address. So instead we keep this unique symbol struct for
585 * each address, and replace name in libsym with a shorter variant if
586 * we find it. */
587struct unique_symbol {
Petr Machatabac2da52012-05-29 00:42:59 +0200588 arch_addr_t addr;
Petr Machata157cc4d2012-04-04 19:00:34 +0200589 struct library_symbol *libsym;
590};
591
592static int
593unique_symbol_cmp(const void *key, const void *val)
594{
595 const struct unique_symbol *sym_key = key;
596 const struct unique_symbol *sym_val = val;
597 return sym_key->addr != sym_val->addr;
598}
599
Petr Machatada3edbf2012-04-04 02:20:21 +0200600static int
601populate_this_symtab(struct Process *proc, const char *filename,
602 struct ltelf *lte, struct library *lib,
603 Elf_Data *symtab, const char *strtab, size_t size)
604{
Petr Machata157cc4d2012-04-04 19:00:34 +0200605 /* Using sorted array would be arguably better, but this
606 * should be well enough for the number of symbols that we
607 * typically deal with. */
608 size_t num_symbols = 0;
609 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
610 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200611 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
612 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200613 return -1;
614 }
615
Petr Machata40cc53b2012-04-07 01:25:38 +0200616 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200617 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200618 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
619 Elf_Scn *scn = elf_getscn(lte->elf, i);
620 if (scn == NULL)
621 continue;
622 GElf_Shdr shdr;
623 if (gelf_getshdr(scn, &shdr) == NULL)
624 continue;
625 secflags[i] = shdr.sh_flags;
626 }
627
628 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200629 for (i = 0; i < size; ++i) {
630 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200631 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200632 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200633 fprintf(stderr,
634 "couldn't get symbol #%zd from %s: %s\n",
635 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200636 continue;
637 }
638
Petr Machata4de6b6b2012-04-04 14:06:09 +0200639 /* XXX support IFUNC as well. */
640 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
Petr Machata6c1c0bb2012-05-18 16:49:34 +0200641 || sym.st_value == 0
642 || sym.st_shndx == STN_UNDEF)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200643 continue;
644
Petr Machata2bbeac42012-04-30 20:48:34 +0200645 const char *orig_name = strtab + sym.st_name;
646 const char *version = strchr(orig_name, '@');
647 size_t len = version != NULL ? (assert(version > orig_name),
648 (size_t)(version - orig_name))
649 : strlen(orig_name);
650 char name[len + 1];
651 memcpy(name, orig_name, len);
652 name[len] = 0;
653
Petr Machatada3edbf2012-04-04 02:20:21 +0200654 if (!filter_matches_symbol(options.static_filter, name, lib))
655 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200656
Petr Machatabac2da52012-05-29 00:42:59 +0200657 arch_addr_t addr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +0200658 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatabac2da52012-05-29 00:42:59 +0200659 arch_addr_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200660
661 /* On arches that support OPD, the value of typical
662 * function symbol will be a pointer to .opd, but some
663 * will point directly to .text. We don't want to
664 * translate those. */
665 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
666 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200667 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200668 fprintf(stderr,
669 "couldn't translate address of %s@%s: %s\n",
670 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200671 continue;
672 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200673
Petr Machata3840f682012-04-06 16:05:41 +0200674 char *full_name;
675 if (lib->type != LT_LIBTYPE_MAIN) {
676 full_name = malloc(strlen(name) + 1 + lib_len + 1);
677 if (full_name == NULL)
678 goto fail;
679 sprintf(full_name, "%s@%s", name, lib->soname);
680 } else {
681 full_name = strdup(name);
682 if (full_name == NULL)
683 goto fail;
684 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200685
Petr Machata157cc4d2012-04-04 19:00:34 +0200686 /* Look whether we already have a symbol for this
687 * address. If not, add this one. */
688 struct unique_symbol key = { naddr, NULL };
689 struct unique_symbol *unique
690 = lsearch(&key, symbols, &num_symbols,
691 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200692
Petr Machata157cc4d2012-04-04 19:00:34 +0200693 if (unique->libsym == NULL) {
694 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200695 if (libsym == NULL
696 || library_symbol_init(libsym, naddr, full_name,
697 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200698 --num_symbols;
699 goto fail;
700 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200701 unique->libsym = libsym;
702 unique->addr = naddr;
703
704 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
705 library_symbol_set_name(unique->libsym, full_name, 1);
706
707 } else {
708 free(full_name);
709 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200710 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200711
712 for (i = 0; i < num_symbols; ++i) {
713 assert(symbols[i].libsym != NULL);
714 library_add_symbol(lib, symbols[i].libsym);
715 }
716
717 free(symbols);
718
Petr Machatada3edbf2012-04-04 02:20:21 +0200719 return 0;
720}
721
722static int
723populate_symtab(struct Process *proc, const char *filename,
724 struct ltelf *lte, struct library *lib)
725{
726 if (lte->symtab != NULL && lte->strtab != NULL)
727 return populate_this_symtab(proc, filename, lte, lib,
728 lte->symtab, lte->strtab,
729 lte->symtab_count);
730 else
731 return populate_this_symtab(proc, filename, lte, lib,
732 lte->dynsym, lte->dynstr,
733 lte->dynsym_count);
734}
735
Petr Machatab5f80ac2012-04-04 01:46:18 +0200736int
737ltelf_read_library(struct library *lib, struct Process *proc,
738 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100739{
Petr Machata29add4f2012-02-18 16:38:05 +0100740 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100741 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200742 return -1;
Petr Machatae0615ab2012-04-17 05:17:48 +0200743 if (arch_elf_init(&lte, lib) < 0) {
744 fprintf(stderr, "Backend initialization failed.\n");
745 return -1;
746 }
747
Petr Machatae67635d2012-03-21 03:37:39 +0100748 proc->e_machine = lte.ehdr.e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400749 proc->e_class = lte.ehdr.e_ident[EI_CLASS];
Joe Damatof0bd98b2010-11-08 15:47:42 -0800750
Petr Machatab5f80ac2012-04-04 01:46:18 +0200751 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200752 if (lib == NULL)
753 goto fail;
754
755 /* Note that we set soname and pathname as soon as they are
756 * allocated, so in case of further errors, this get released
757 * when LIB is release, which should happen in the caller when
758 * we return error. */
759
760 if (lib->pathname == NULL) {
761 char *pathname = strdup(filename);
762 if (pathname == NULL)
763 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200764 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800765 }
766
Petr Machata0b55b582012-04-02 00:38:46 +0200767 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200768 char *soname = strdup(lte.soname);
769 if (soname == NULL)
770 goto fail;
771 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200772 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200773 const char *soname = rindex(lib->pathname, '/') + 1;
774 if (soname == NULL)
775 soname = lib->pathname;
776 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200777 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700778
Petr Machataea8eb9a2012-04-17 01:32:07 +0200779 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200780 * arch_addr_t becomes integral type. */
781 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200782 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100783 goto fail;
784
Petr Machataea8eb9a2012-04-17 01:32:07 +0200785 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200786 * arch_addr_t becomes integral type. */
787 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100788 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200789 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200790 * arch_addr_t becomes integral type. */
791 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100792
Petr Machatab5f80ac2012-04-04 01:46:18 +0200793 if (filter_matches_library(options.plt_filter, lib)
794 && populate_plt(proc, filename, &lte, lib) < 0)
795 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800796
Petr Machatada3edbf2012-04-04 02:20:21 +0200797 if (filter_matches_library(options.static_filter, lib)
798 && populate_symtab(proc, filename, &lte, lib) < 0)
799 goto fail;
800
Petr Machata2b46cfc2012-02-18 11:17:29 +0100801done:
802 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200803 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200804
805fail:
806 status = -1;
807 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100808}
Petr Machatae84fa002012-02-07 13:43:03 +0100809
Petr Machata2b46cfc2012-02-18 11:17:29 +0100810struct library *
811ltelf_read_main_binary(struct Process *proc, const char *path)
812{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200813 struct library *lib = malloc(sizeof(*lib));
814 if (lib == NULL)
815 return NULL;
816 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200817 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200818
Petr Machatafc6ff182012-04-04 13:11:50 +0200819 /* There is a race between running the process and reading its
820 * binary for internal consumption. So open the binary from
821 * the /proc filesystem. XXX Note that there is similar race
822 * for libraries, but there we don't have a nice answer like
823 * that. Presumably we could read the DSOs from the process
824 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100825 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200826 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
827 library_destroy(lib);
828 free(lib);
829 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200830 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200831
Petr Machata2b46cfc2012-02-18 11:17:29 +0100832 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200833}