blob: f4a58a57114bb683840ba1544e6c429cb6c9b8e1 [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 Machata6db61f52012-10-31 03:27:05 +010066int
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) {
Petr Machata6db61f52012-10-31 03:27:05 +010073 fail_message:
74 fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n",
75 strerror(errno));
Petr Machatae6523e62012-03-24 04:54:06 +010076 fail:
77 free(name);
78 return -1;
79 }
80
Petr Machata1be22912012-03-27 03:11:33 +020081 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010082
83 struct library_symbol *libsym = malloc(sizeof(*libsym));
84 if (libsym == NULL)
Petr Machata6db61f52012-10-31 03:27:05 +010085 goto fail_message;
Petr Machatae6523e62012-03-24 04:54:06 +010086
Petr Machataea8eb9a2012-04-17 01:32:07 +020087 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +020088 * arch_addr_t becomes integral type. */
89 arch_addr_t taddr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +020090 (uintptr_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010091
Petr Machatae8d90762012-04-15 04:28:31 +020092 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
93 free(libsym);
94 goto fail;
95 }
96
Petr Machata0fa79042012-09-27 23:33:44 +020097 libsym->next = *ret;
Petr Machatae6523e62012-03-24 04:54:06 +010098 *ret = libsym;
99 return 0;
100}
101
102#ifndef ARCH_HAVE_ADD_PLT_ENTRY
103enum plt_status
104arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +0200105 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +0100106 struct library_symbol **ret)
107{
108 return plt_default;
109}
110#endif
111
Petr Machatae67635d2012-03-21 03:37:39 +0100112Elf_Data *
113elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200114{
115 Elf_Data *data = elf_getdata(scn, NULL);
116 if (data == NULL || elf_getdata(scn, data) != NULL
117 || data->d_off || data->d_size != shdr->sh_size)
118 return NULL;
119 return data;
120}
121
Petr Machatae67635d2012-03-21 03:37:39 +0100122static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100123elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
124 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
125 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200126{
127 int i;
128 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
129 Elf_Scn *scn;
130 GElf_Shdr shdr;
131
132 scn = elf_getscn(lte->elf, i);
133 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
134 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100135 return -1;
136 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100137 if (predicate(scn, &shdr, data)) {
138 *tgt_sec = scn;
139 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200140 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100141 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200142 }
Petr Machatae67635d2012-03-21 03:37:39 +0100143 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100144
145}
146
147static int
148inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
149{
150 GElf_Addr addr = *(GElf_Addr *)data;
151 return addr >= shdr->sh_addr
152 && addr < shdr->sh_addr + shdr->sh_size;
153}
154
155int
156elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
157 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
158{
159 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
160 &inside_p, &addr);
161}
162
163static int
164type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
165{
166 GElf_Word type = *(GElf_Word *)data;
167 return shdr->sh_type == type;
168}
169
170int
171elf_get_section_type(struct ltelf *lte, GElf_Word type,
172 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
173{
174 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
175 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100176}
177
Petr Machata5b3e26a2012-04-30 20:53:22 +0200178struct section_named_data {
179 struct ltelf *lte;
180 const char *name;
181};
182
183static int
184name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
185{
186 struct section_named_data *data = d;
187 const char *name = elf_strptr(data->lte->elf,
188 data->lte->ehdr.e_shstrndx,
189 shdr->sh_name);
190 return strcmp(name, data->name) == 0;
191}
192
193int
194elf_get_section_named(struct ltelf *lte, const char *name,
195 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
196{
197 struct section_named_data data = {
198 .lte = lte,
199 .name = name,
200 };
201 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
202 &name_p, &data);
203}
204
Petr Machatae67635d2012-03-21 03:37:39 +0100205static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200206need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100207{
208 assert(data != NULL);
209 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200210 debug(1, "Not enough data to read %"PRId64"-byte value"
211 " at offset %"PRId64".", size, offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100212 return -1;
213 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200214 return 0;
215}
216
Petr Machatae67635d2012-03-21 03:37:39 +0100217#define DEF_READER(NAME, SIZE) \
218 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200219 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100220 { \
221 if (!need_data(data, offset, SIZE / 8) < 0) \
222 return -1; \
223 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200224 if (data->d_buf == NULL) /* NODATA section */ { \
225 *retp = 0; \
226 return 0; \
227 } \
228 \
Petr Machatae67635d2012-03-21 03:37:39 +0100229 union { \
230 uint##SIZE##_t dst; \
231 char buf[0]; \
232 } u; \
233 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
234 *retp = u.dst; \
235 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200236 }
237
Petr Machatae67635d2012-03-21 03:37:39 +0100238DEF_READER(elf_read_u16, 16)
239DEF_READER(elf_read_u32, 32)
240DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200241
Petr Machatae67635d2012-03-21 03:37:39 +0100242#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200243
Petr Machata1974dbc2011-08-19 18:58:01 +0200244int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200245open_elf(struct ltelf *lte, const char *filename)
246{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247 lte->fd = open(filename, O_RDONLY);
248 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200249 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200250
Petr Machata02bd9ec2011-09-21 17:38:59 +0200251 elf_version(EV_CURRENT);
252
Juan Cespedesd914a202004-11-10 00:15:33 +0100253#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200255#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100256 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200257#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200258
Petr Machatacc0e1e42012-04-25 13:42:07 +0200259 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
260 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
261 exit(EXIT_FAILURE);
262 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200263
Petr Machatacc0e1e42012-04-25 13:42:07 +0200264 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
265 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
266 filename, elf_errmsg(-1));
267 exit(EXIT_FAILURE);
268 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200269
Petr Machatacc0e1e42012-04-25 13:42:07 +0200270 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
271 fprintf(stderr, "\"%s\" is neither an ELF executable"
272 " nor a shared library\n", filename);
273 exit(EXIT_FAILURE);
274 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200275
Petr Machataddd96a32012-05-17 23:35:26 +0200276 if (1
277#ifdef LT_ELF_MACHINE
278 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
279 || lte->ehdr.e_machine != LT_ELF_MACHINE)
280#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100281#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
283 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100284#endif
285#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
287 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100288#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200289 ) {
290 fprintf(stderr,
291 "\"%s\" is ELF from incompatible architecture\n",
292 filename);
293 exit(EXIT_FAILURE);
294 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100295
Petr Machata02bd9ec2011-09-21 17:38:59 +0200296 return 0;
297}
298
Petr Machatacc0e1e42012-04-25 13:42:07 +0200299static void
300read_symbol_table(struct ltelf *lte, const char *filename,
301 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
302 Elf_Data **datap, size_t *countp, const char **strsp)
303{
304 *datap = elf_getdata(scn, NULL);
305 *countp = shdr->sh_size / shdr->sh_entsize;
306 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
307 && options.static_filter != NULL) {
308 fprintf(stderr, "Couldn't get data of section"
309 " %s from \"%s\": %s\n",
310 name, filename, elf_errmsg(-1));
311 exit(EXIT_FAILURE);
312 }
313
314 scn = elf_getscn(lte->elf, shdr->sh_link);
315 GElf_Shdr shdr2;
316 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
317 fprintf(stderr, "Couldn't get header of section"
318 " #%d from \"%s\": %s\n",
319 shdr2.sh_link, filename, elf_errmsg(-1));
320 exit(EXIT_FAILURE);
321 }
322
323 Elf_Data *data = elf_getdata(scn, NULL);
324 if (data == NULL || elf_getdata(scn, data) != NULL
325 || shdr2.sh_size != data->d_size || data->d_off) {
326 fprintf(stderr, "Couldn't get data of section"
327 " #%d from \"%s\": %s\n",
328 shdr2.sh_link, filename, elf_errmsg(-1));
329 exit(EXIT_FAILURE);
330 }
331
332 *strsp = data->d_buf;
333}
334
Petr Machatae67635d2012-03-21 03:37:39 +0100335static int
Petr Machatada69ed02012-10-18 17:27:48 +0200336do_init_elf(struct ltelf *lte, const char *filename)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200338 int i;
339 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100340 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200341
342 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
343 debug(1, "Reading ELF from %s...", filename);
344
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
346 Elf_Scn *scn;
347 GElf_Shdr shdr;
348 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100349
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100350 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200351 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
352 fprintf(stderr, "Couldn't get section #%d from"
353 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
354 exit(EXIT_FAILURE);
355 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100356
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200358 if (name == NULL) {
359 fprintf(stderr, "Couldn't get name of section #%d from"
360 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
361 exit(EXIT_FAILURE);
362 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100363
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200365 read_symbol_table(lte, filename,
366 scn, &shdr, name, &lte->symtab,
367 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100368
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200370 read_symbol_table(lte, filename,
371 scn, &shdr, name, &lte->dynsym,
372 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100373
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 } else if (shdr.sh_type == SHT_DYNAMIC) {
375 Elf_Data *data;
376 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100377
Petr Machatada69ed02012-10-18 17:27:48 +0200378 lte->dyn_addr = shdr.sh_addr + lte->bias;
Joe Damato87f4f582010-11-08 15:47:36 -0800379 lte->dyn_sz = shdr.sh_size;
380
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200382 if (data == NULL || elf_getdata(scn, data) != NULL) {
383 fprintf(stderr, "Couldn't get .dynamic data"
384 " from \"%s\": %s\n",
385 filename, strerror(errno));
386 exit(EXIT_FAILURE);
387 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100388
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100389 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
390 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100391
Petr Machatacc0e1e42012-04-25 13:42:07 +0200392 if (gelf_getdyn(data, j, &dyn) == NULL) {
393 fprintf(stderr, "Couldn't get .dynamic"
394 " data from \"%s\": %s\n",
395 filename, strerror(errno));
396 exit(EXIT_FAILURE);
397 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100398 if (dyn.d_tag == DT_JMPREL)
399 relplt_addr = dyn.d_un.d_ptr;
400 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100401 lte->relplt_size = dyn.d_un.d_val;
402 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100403 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100404 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 } else if (shdr.sh_type == SHT_PROGBITS
406 || shdr.sh_type == SHT_NOBITS) {
407 if (strcmp(name, ".plt") == 0) {
408 lte->plt_addr = shdr.sh_addr;
409 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100410 lte->plt_data = elf_loaddata(scn, &shdr);
411 if (lte->plt_data == NULL)
412 fprintf(stderr,
413 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200414 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100415 }
416#ifdef ARCH_SUPPORTS_OPD
417 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200418 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100419 lte->opd_size = shdr.sh_size;
420 lte->opd = elf_rawdata(scn, NULL);
421 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100422#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100424 }
425
Petr Machatacc0e1e42012-04-25 13:42:07 +0200426 if (lte->dynsym == NULL || lte->dynstr == NULL) {
427 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
428 filename);
429 exit(EXIT_FAILURE);
430 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100431
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432 if (!relplt_addr || !lte->plt_addr) {
433 debug(1, "%s has no PLT relocations", filename);
434 lte->relplt = NULL;
435 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100436 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200437 debug(1, "%s has unknown PLT size", filename);
438 lte->relplt = NULL;
439 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100440 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200441
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100442 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
443 Elf_Scn *scn;
444 GElf_Shdr shdr;
445
446 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200447 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
448 fprintf(stderr, "Couldn't get section header"
449 " from \"%s\": %s\n",
450 filename, elf_errmsg(-1));
451 exit(EXIT_FAILURE);
452 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100453 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100454 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100455 lte->relplt = elf_getdata(scn, NULL);
456 lte->relplt_count =
457 shdr.sh_size / shdr.sh_entsize;
458 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200459 || elf_getdata(scn, lte->relplt) != NULL) {
460 fprintf(stderr, "Couldn't get .rel*.plt"
461 " data from \"%s\": %s\n",
462 filename, elf_errmsg(-1));
463 exit(EXIT_FAILURE);
464 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100465 break;
466 }
467 }
468
Petr Machatacc0e1e42012-04-25 13:42:07 +0200469 if (i == lte->ehdr.e_shnum) {
470 fprintf(stderr,
471 "Couldn't find .rel*.plt section in \"%s\"\n",
472 filename);
473 exit(EXIT_FAILURE);
474 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100475
476 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
477 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100478
479 if (soname_offset != 0)
480 lte->soname = lte->dynstr + soname_offset;
481
Petr Machata1974dbc2011-08-19 18:58:01 +0200482 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483}
484
Joe Damato7a2bdf82010-11-08 15:47:41 -0800485void
Petr Machata588850f2012-10-26 22:26:54 +0200486do_close_elf(struct ltelf *lte)
487{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200488 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100489 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100490 elf_end(lte->elf);
491 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200492}
493
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200494int
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +0200495elf_get_sym_info(struct ltelf *lte, const char *filename,
496 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200497{
498 int i = sym_index;
499 GElf_Rel rel;
500 void *ret;
501
502 if (lte->relplt->d_type == ELF_T_REL) {
503 ret = gelf_getrel(lte->relplt, i, &rel);
504 rela->r_offset = rel.r_offset;
505 rela->r_info = rel.r_info;
506 rela->r_addend = 0;
507 } else {
508 ret = gelf_getrela(lte->relplt, i, rela);
509 }
510
511 if (ret == NULL
512 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
513 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info),
514 sym) == NULL) {
515 fprintf(stderr,
516 "Couldn't get relocation from \"%s\": %s\n",
517 filename, elf_errmsg(-1));
518 exit(EXIT_FAILURE);
519 }
520
521 return 0;
522}
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +0200523
524#ifndef ARCH_HAVE_GET_SYMINFO
525int
526arch_get_sym_info(struct ltelf *lte, const char *filename,
527 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
528{
529 return elf_get_sym_info(lte, filename, sym_index, rela, sym);
530}
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200531#endif
532
Petr Machatae80cace2012-09-28 00:05:08 +0200533static void
534mark_chain_latent(struct library_symbol *libsym)
535{
536 for (; libsym != NULL; libsym = libsym->next) {
537 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
538 libsym->latent = 1;
539 }
540}
541
Petr Machatab5f80ac2012-04-04 01:46:18 +0200542static int
543populate_plt(struct Process *proc, const char *filename,
Petr Machatae80cace2012-09-28 00:05:08 +0200544 struct ltelf *lte, struct library *lib,
545 int latent_plts)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200546{
547 size_t i;
548 for (i = 0; i < lte->relplt_count; ++i) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200549 GElf_Rela rela;
550 GElf_Sym sym;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200551
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200552 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0)
553 continue; /* Skip this entry. */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200554
555 char const *name = lte->dynstr + sym.st_name;
556
Petr Machatae80cace2012-09-28 00:05:08 +0200557 /* If the symbol wasn't matched, reject it, unless we
558 * need to keep latent PLT breakpoints for tracing
559 * exports. */
560 int matched = filter_matches_symbol(options.plt_filter,
561 name, lib);
562 if (!matched && !latent_plts)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200563 continue;
564
Petr Machata218c5ff2012-04-15 04:22:39 +0200565 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200566 switch (arch_elf_add_plt_entry(proc, lte, name,
567 &rela, i, &libsym)) {
568 case plt_default:
569 if (default_elf_add_plt_entry(proc, lte, name,
570 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200571 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200572 case plt_fail:
573 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200574 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200575 case plt_ok:
Petr Machatae80cace2012-09-28 00:05:08 +0200576 if (libsym != NULL) {
577 /* If we are adding those symbols just
578 * for tracing exports, mark them all
579 * latent. */
580 if (!matched)
581 mark_chain_latent(libsym);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200582 library_add_symbol(lib, libsym);
Petr Machatae80cace2012-09-28 00:05:08 +0200583 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200584 }
585 }
586 return 0;
587}
588
Petr Machata157cc4d2012-04-04 19:00:34 +0200589/* When -x rules result in request to trace several aliases, we only
590 * want to add such symbol once. The only way that those symbols
591 * differ in is their name, e.g. in glibc you have __GI___libc_free,
592 * __cfree, __free, __libc_free, cfree and free all defined on the
593 * same address. So instead we keep this unique symbol struct for
594 * each address, and replace name in libsym with a shorter variant if
595 * we find it. */
596struct unique_symbol {
Petr Machatabac2da52012-05-29 00:42:59 +0200597 arch_addr_t addr;
Petr Machata157cc4d2012-04-04 19:00:34 +0200598 struct library_symbol *libsym;
599};
600
601static int
602unique_symbol_cmp(const void *key, const void *val)
603{
604 const struct unique_symbol *sym_key = key;
605 const struct unique_symbol *sym_val = val;
606 return sym_key->addr != sym_val->addr;
607}
608
Petr Machata10bd7a92012-09-28 00:06:56 +0200609static enum callback_status
610symbol_with_address(struct library_symbol *sym, void *addrptr)
611{
612 return sym->enter_addr == *(arch_addr_t *)addrptr
613 ? CBS_STOP : CBS_CONT;
614}
615
Petr Machatada3edbf2012-04-04 02:20:21 +0200616static int
617populate_this_symtab(struct Process *proc, const char *filename,
618 struct ltelf *lte, struct library *lib,
Petr Machata013ef5b2012-09-28 00:00:55 +0200619 Elf_Data *symtab, const char *strtab, size_t size,
620 struct library_exported_name **names)
Petr Machatada3edbf2012-04-04 02:20:21 +0200621{
Petr Machata013ef5b2012-09-28 00:00:55 +0200622 /* If a valid NAMES is passed, we pass in *NAMES a list of
623 * symbol names that this library exports. */
624 if (names != NULL)
625 *names = NULL;
626
Petr Machata157cc4d2012-04-04 19:00:34 +0200627 /* Using sorted array would be arguably better, but this
628 * should be well enough for the number of symbols that we
629 * typically deal with. */
630 size_t num_symbols = 0;
631 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
632 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200633 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
634 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200635 return -1;
636 }
637
Petr Machata40cc53b2012-04-07 01:25:38 +0200638 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200639 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200640 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
641 Elf_Scn *scn = elf_getscn(lte->elf, i);
642 if (scn == NULL)
643 continue;
644 GElf_Shdr shdr;
645 if (gelf_getshdr(scn, &shdr) == NULL)
646 continue;
647 secflags[i] = shdr.sh_flags;
648 }
649
650 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200651 for (i = 0; i < size; ++i) {
652 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200653 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200654 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200655 fprintf(stderr,
656 "couldn't get symbol #%zd from %s: %s\n",
657 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200658 continue;
659 }
660
Petr Machata4de6b6b2012-04-04 14:06:09 +0200661 /* XXX support IFUNC as well. */
662 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
Petr Machata6c1c0bb2012-05-18 16:49:34 +0200663 || sym.st_value == 0
664 || sym.st_shndx == STN_UNDEF)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200665 continue;
666
Petr Machata013ef5b2012-09-28 00:00:55 +0200667 /* Find symbol name and snip version. */
Petr Machata2bbeac42012-04-30 20:48:34 +0200668 const char *orig_name = strtab + sym.st_name;
669 const char *version = strchr(orig_name, '@');
670 size_t len = version != NULL ? (assert(version > orig_name),
671 (size_t)(version - orig_name))
672 : strlen(orig_name);
673 char name[len + 1];
674 memcpy(name, orig_name, len);
675 name[len] = 0;
676
Petr Machata013ef5b2012-09-28 00:00:55 +0200677 /* If we are interested in exports, store this name. */
678 char *name_copy = NULL;
679 if (names != NULL) {
680 struct library_exported_name *export = NULL;
681 name_copy = strdup(name);
682
683 if (name_copy == NULL
684 || (export = malloc(sizeof(*export))) == NULL) {
685 free(name_copy);
686 fprintf(stderr, "Couldn't store symbol %s. "
687 "Tracing may be incomplete.\n", name);
688 } else {
689 export->name = name_copy;
690 export->own_name = 1;
691 export->next = *names;
692 *names = export;
693 }
694 }
695
696 /* If the symbol is not matched, skip it. We already
697 * stored it to export list above. */
Petr Machatada3edbf2012-04-04 02:20:21 +0200698 if (!filter_matches_symbol(options.static_filter, name, lib))
699 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200700
Petr Machatabac2da52012-05-29 00:42:59 +0200701 arch_addr_t addr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +0200702 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatabac2da52012-05-29 00:42:59 +0200703 arch_addr_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200704
705 /* On arches that support OPD, the value of typical
706 * function symbol will be a pointer to .opd, but some
707 * will point directly to .text. We don't want to
708 * translate those. */
709 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
710 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200711 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200712 fprintf(stderr,
713 "couldn't translate address of %s@%s: %s\n",
714 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200715 continue;
716 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200717
Petr Machata3840f682012-04-06 16:05:41 +0200718 char *full_name;
Petr Machata013ef5b2012-09-28 00:00:55 +0200719 int own_full_name = 1;
Petr Machata3840f682012-04-06 16:05:41 +0200720 if (lib->type != LT_LIBTYPE_MAIN) {
721 full_name = malloc(strlen(name) + 1 + lib_len + 1);
722 if (full_name == NULL)
723 goto fail;
724 sprintf(full_name, "%s@%s", name, lib->soname);
725 } else {
Petr Machata013ef5b2012-09-28 00:00:55 +0200726 if (name_copy == NULL) {
727 full_name = strdup(name);
728 if (full_name == NULL)
729 goto fail;
730 } else {
731 full_name = name_copy;
732 own_full_name = 0;
733 }
Petr Machata3840f682012-04-06 16:05:41 +0200734 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200735
Petr Machata157cc4d2012-04-04 19:00:34 +0200736 /* Look whether we already have a symbol for this
737 * address. If not, add this one. */
738 struct unique_symbol key = { naddr, NULL };
739 struct unique_symbol *unique
740 = lsearch(&key, symbols, &num_symbols,
741 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200742
Petr Machata157cc4d2012-04-04 19:00:34 +0200743 if (unique->libsym == NULL) {
744 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200745 if (libsym == NULL
Petr Machata013ef5b2012-09-28 00:00:55 +0200746 || library_symbol_init(libsym, naddr,
747 full_name, own_full_name,
748 LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200749 --num_symbols;
750 goto fail;
751 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200752 unique->libsym = libsym;
753 unique->addr = naddr;
754
755 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
Petr Machata013ef5b2012-09-28 00:00:55 +0200756 library_symbol_set_name(unique->libsym,
757 full_name, own_full_name);
Petr Machata157cc4d2012-04-04 19:00:34 +0200758
Petr Machata013ef5b2012-09-28 00:00:55 +0200759 } else if (own_full_name) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200760 free(full_name);
761 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200762 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200763
Petr Machata10bd7a92012-09-28 00:06:56 +0200764 /* Now we do the union of this set of unique symbols with
765 * what's already in the library. */
Petr Machata157cc4d2012-04-04 19:00:34 +0200766 for (i = 0; i < num_symbols; ++i) {
Petr Machata10bd7a92012-09-28 00:06:56 +0200767 struct library_symbol *this_sym = symbols[i].libsym;
768 assert(this_sym != NULL);
769 struct library_symbol *other
770 = library_each_symbol(lib, NULL, symbol_with_address,
771 &this_sym->enter_addr);
772 if (other != NULL) {
773 library_symbol_destroy(this_sym);
774 free(this_sym);
775 symbols[i].libsym = NULL;
776 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200777 }
778
Petr Machata10bd7a92012-09-28 00:06:56 +0200779 for (i = 0; i < num_symbols; ++i)
780 if (symbols[i].libsym != NULL)
781 library_add_symbol(lib, symbols[i].libsym);
Petr Machata157cc4d2012-04-04 19:00:34 +0200782
Petr Machata10bd7a92012-09-28 00:06:56 +0200783 free(symbols);
Petr Machatada3edbf2012-04-04 02:20:21 +0200784 return 0;
785}
786
787static int
788populate_symtab(struct Process *proc, const char *filename,
Petr Machata013ef5b2012-09-28 00:00:55 +0200789 struct ltelf *lte, struct library *lib,
790 int symtabs, int exports)
Petr Machatada3edbf2012-04-04 02:20:21 +0200791{
Petr Machata013ef5b2012-09-28 00:00:55 +0200792 int status;
793 if (symtabs && lte->symtab != NULL && lte->strtab != NULL
794 && (status = populate_this_symtab(proc, filename, lte, lib,
795 lte->symtab, lte->strtab,
796 lte->symtab_count, NULL)) < 0)
797 return status;
798
799 /* Check whether we want to trace symbols implemented by this
800 * library (-l). */
801 struct library_exported_name **names = NULL;
802 if (exports) {
803 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
804 names = &lib->exported_names;
805 }
806
807 return populate_this_symtab(proc, filename, lte, lib,
808 lte->dynsym, lte->dynstr,
809 lte->dynsym_count, names);
Petr Machatada3edbf2012-04-04 02:20:21 +0200810}
811
Petr Machatada69ed02012-10-18 17:27:48 +0200812static int
813read_module(struct library *lib, struct Process *proc,
814 const char *filename, GElf_Addr bias, int main)
Petr Machatae84fa002012-02-07 13:43:03 +0100815{
Petr Machata29add4f2012-02-18 16:38:05 +0100816 struct ltelf lte = {};
Petr Machatada69ed02012-10-18 17:27:48 +0200817 if (open_elf(&lte, filename) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200818 return -1;
Petr Machatada69ed02012-10-18 17:27:48 +0200819
Petr Machata1c790252012-10-30 23:29:27 +0100820 /* XXX When we abstract ABI into a module, this should instead
821 * become something like
822 *
823 * proc->abi = arch_get_abi(lte.ehdr);
824 *
825 * The code in open_elf needs to be replaced by this logic.
826 * Be warned that libltrace.c calls open_elf as well to
827 * determine whether ABI is supported. This is to get
828 * reasonable error messages when trying to run 64-bit binary
829 * with 32-bit ltrace. It is desirable to preserve this. */
830 proc->e_machine = lte.ehdr.e_machine;
831 proc->e_class = lte.ehdr.e_ident[EI_CLASS];
832 get_arch_dep(proc);
833
Petr Machatada69ed02012-10-18 17:27:48 +0200834 /* Find out the base address. For PIE main binaries we look
835 * into auxv, otherwise we scan phdrs. */
836 if (main && lte.ehdr.e_type == ET_DYN) {
837 arch_addr_t entry;
838 if (process_get_entry(proc, &entry, NULL) < 0) {
839 fprintf(stderr, "Couldn't find entry of PIE %s\n",
840 filename);
841 return -1;
842 }
Petr Machatab024ca32012-10-18 19:57:49 +0200843 /* XXX The double cast should be removed when
844 * arch_addr_t becomes integral type. */
845 lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
846 lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
Petr Machatada69ed02012-10-18 17:27:48 +0200847
848 } else {
849 GElf_Phdr phdr;
850 size_t i;
851 for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
852 if (phdr.p_type == PT_LOAD) {
853 lte.base_addr = phdr.p_vaddr + bias;
854 break;
855 }
856 }
857
858 lte.bias = bias;
859 lte.entry_addr = lte.ehdr.e_entry + lte.bias;
860
861 if (lte.base_addr == 0) {
862 fprintf(stderr,
863 "Couldn't determine base address of %s\n",
864 filename);
865 return -1;
866 }
867 }
868
869 if (do_init_elf(&lte, filename) < 0)
870 return -1;
871
Petr Machatae0615ab2012-04-17 05:17:48 +0200872 if (arch_elf_init(&lte, lib) < 0) {
873 fprintf(stderr, "Backend initialization failed.\n");
874 return -1;
875 }
876
Petr Machatab5f80ac2012-04-04 01:46:18 +0200877 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200878 if (lib == NULL)
879 goto fail;
880
881 /* Note that we set soname and pathname as soon as they are
882 * allocated, so in case of further errors, this get released
883 * when LIB is release, which should happen in the caller when
884 * we return error. */
885
886 if (lib->pathname == NULL) {
887 char *pathname = strdup(filename);
888 if (pathname == NULL)
889 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200890 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800891 }
892
Petr Machata0b55b582012-04-02 00:38:46 +0200893 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200894 char *soname = strdup(lte.soname);
895 if (soname == NULL)
896 goto fail;
897 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200898 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200899 const char *soname = rindex(lib->pathname, '/') + 1;
900 if (soname == NULL)
901 soname = lib->pathname;
902 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200903 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700904
Petr Machataea8eb9a2012-04-17 01:32:07 +0200905 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200906 * arch_addr_t becomes integral type. */
907 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200908 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100909 goto fail;
910
Petr Machataea8eb9a2012-04-17 01:32:07 +0200911 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200912 * arch_addr_t becomes integral type. */
913 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100914 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200915 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200916 * arch_addr_t becomes integral type. */
917 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100918
Petr Machata013ef5b2012-09-28 00:00:55 +0200919 /* There are two reasons that we need to inspect symbol tables
920 * or populate PLT entries. Either the user requested
921 * corresponding tracing features (respectively -x and -e), or
922 * they requested tracing exported symbols (-l).
923 *
924 * In the latter case we need to keep even those PLT slots
925 * that are not requested by -e (but we keep them latent). We
926 * also need to inspect .dynsym to find what exports this
927 * library provide, to turn on existing latent PLT
928 * entries. */
929
930 int plts = filter_matches_library(options.plt_filter, lib);
931 if ((plts || options.export_filter != NULL)
Petr Machatae80cace2012-09-28 00:05:08 +0200932 && populate_plt(proc, filename, &lte, lib,
933 options.export_filter != NULL) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200934 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800935
Petr Machata013ef5b2012-09-28 00:00:55 +0200936 int exports = filter_matches_library(options.export_filter, lib);
937 int symtabs = filter_matches_library(options.static_filter, lib);
938 if ((symtabs || exports)
939 && populate_symtab(proc, filename, &lte, lib,
940 symtabs, exports) < 0)
Petr Machatada3edbf2012-04-04 02:20:21 +0200941 goto fail;
942
Petr Machata2b46cfc2012-02-18 11:17:29 +0100943done:
944 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200945 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200946
947fail:
948 status = -1;
949 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100950}
Petr Machatae84fa002012-02-07 13:43:03 +0100951
Petr Machatada69ed02012-10-18 17:27:48 +0200952int
953ltelf_read_library(struct library *lib, struct Process *proc,
954 const char *filename, GElf_Addr bias)
955{
956 return read_module(lib, proc, filename, bias, 0);
957}
958
959
Petr Machata2b46cfc2012-02-18 11:17:29 +0100960struct library *
961ltelf_read_main_binary(struct Process *proc, const char *path)
962{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200963 struct library *lib = malloc(sizeof(*lib));
964 if (lib == NULL)
965 return NULL;
966 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200967 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200968
Petr Machatafc6ff182012-04-04 13:11:50 +0200969 /* There is a race between running the process and reading its
970 * binary for internal consumption. So open the binary from
971 * the /proc filesystem. XXX Note that there is similar race
972 * for libraries, but there we don't have a nice answer like
973 * that. Presumably we could read the DSOs from the process
974 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100975 char *fname = pid2name(proc->pid);
Petr Machataef0c74d2012-10-27 00:30:57 +0200976 if (fname == NULL)
977 return NULL;
Petr Machatada69ed02012-10-18 17:27:48 +0200978 if (read_module(lib, proc, fname, 0, 1) < 0) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200979 library_destroy(lib);
980 free(lib);
981 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200982 }
Petr Machataef0c74d2012-10-27 00:30:57 +0200983 free(fname);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200984
Petr Machata2b46cfc2012-02-18 11:17:29 +0100985 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200986}