blob: c147ad8741a9d7019f0747084930a0df19ef01a7 [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>
Petr Machata513258c2012-12-08 03:47:02 +010043#include <strings.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010044#include <unistd.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020045
Petr Machata64262602012-01-07 03:41:36 +010046#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020047#include "filter.h"
48#include "library.h"
49#include "ltrace-elf.h"
50#include "proc.h"
51#include "debug.h"
52#include "options.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080053
Petr Machatae67635d2012-03-21 03:37:39 +010054#ifndef ARCH_HAVE_LTELF_DATA
55int
Petr Machatae0615ab2012-04-17 05:17:48 +020056arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010057{
58 return 0;
59}
Petr Machatac67a6e62012-03-28 02:39:49 +020060
61void
62arch_elf_destroy(struct ltelf *lte)
63{
64}
Petr Machatae67635d2012-03-21 03:37:39 +010065#endif
66
Petr Machata6db61f52012-10-31 03:27:05 +010067int
Petr Machata929bd572012-12-17 03:20:34 +010068default_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020069 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010070 struct library_symbol **ret)
71{
72 char *name = strdup(a_name);
73 if (name == NULL) {
Petr Machata6db61f52012-10-31 03:27:05 +010074 fail_message:
75 fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n",
76 strerror(errno));
Petr Machatae6523e62012-03-24 04:54:06 +010077 fail:
78 free(name);
79 return -1;
80 }
81
Petr Machata1be22912012-03-27 03:11:33 +020082 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010083
84 struct library_symbol *libsym = malloc(sizeof(*libsym));
85 if (libsym == NULL)
Petr Machata6db61f52012-10-31 03:27:05 +010086 goto fail_message;
Petr Machatae6523e62012-03-24 04:54:06 +010087
Petr Machataea8eb9a2012-04-17 01:32:07 +020088 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +020089 * arch_addr_t becomes integral type. */
90 arch_addr_t taddr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +020091 (uintptr_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010092
Petr Machatae8d90762012-04-15 04:28:31 +020093 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
94 free(libsym);
95 goto fail;
96 }
97
Petr Machata0fa79042012-09-27 23:33:44 +020098 libsym->next = *ret;
Petr Machatae6523e62012-03-24 04:54:06 +010099 *ret = libsym;
100 return 0;
101}
102
103#ifndef ARCH_HAVE_ADD_PLT_ENTRY
104enum plt_status
Petr Machata929bd572012-12-17 03:20:34 +0100105arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +0200106 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +0100107 struct library_symbol **ret)
108{
109 return plt_default;
110}
111#endif
112
Petr Machatae67635d2012-03-21 03:37:39 +0100113Elf_Data *
114elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200115{
116 Elf_Data *data = elf_getdata(scn, NULL);
117 if (data == NULL || elf_getdata(scn, data) != NULL
118 || data->d_off || data->d_size != shdr->sh_size)
119 return NULL;
120 return data;
121}
122
Petr Machatae67635d2012-03-21 03:37:39 +0100123static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100124elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
125 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
126 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200127{
128 int i;
129 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
130 Elf_Scn *scn;
131 GElf_Shdr shdr;
132
133 scn = elf_getscn(lte->elf, i);
134 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
135 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100136 return -1;
137 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100138 if (predicate(scn, &shdr, data)) {
139 *tgt_sec = scn;
140 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200141 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100142 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200143 }
Petr Machatae67635d2012-03-21 03:37:39 +0100144 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100145
146}
147
148static int
149inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
150{
151 GElf_Addr addr = *(GElf_Addr *)data;
152 return addr >= shdr->sh_addr
153 && addr < shdr->sh_addr + shdr->sh_size;
154}
155
156int
157elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
158 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
159{
160 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
161 &inside_p, &addr);
162}
163
164static int
165type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
166{
167 GElf_Word type = *(GElf_Word *)data;
168 return shdr->sh_type == type;
169}
170
171int
172elf_get_section_type(struct ltelf *lte, GElf_Word type,
173 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
174{
175 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
176 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100177}
178
Petr Machata5b3e26a2012-04-30 20:53:22 +0200179struct section_named_data {
180 struct ltelf *lte;
181 const char *name;
182};
183
184static int
185name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
186{
187 struct section_named_data *data = d;
188 const char *name = elf_strptr(data->lte->elf,
189 data->lte->ehdr.e_shstrndx,
190 shdr->sh_name);
191 return strcmp(name, data->name) == 0;
192}
193
194int
195elf_get_section_named(struct ltelf *lte, const char *name,
196 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
197{
198 struct section_named_data data = {
199 .lte = lte,
200 .name = name,
201 };
202 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
203 &name_p, &data);
204}
205
Petr Machatae67635d2012-03-21 03:37:39 +0100206static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200207need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100208{
209 assert(data != NULL);
210 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200211 debug(1, "Not enough data to read %"PRId64"-byte value"
212 " at offset %"PRId64".", size, offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100213 return -1;
214 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200215 return 0;
216}
217
Petr Machatae67635d2012-03-21 03:37:39 +0100218#define DEF_READER(NAME, SIZE) \
219 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200220 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100221 { \
222 if (!need_data(data, offset, SIZE / 8) < 0) \
223 return -1; \
224 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200225 if (data->d_buf == NULL) /* NODATA section */ { \
226 *retp = 0; \
227 return 0; \
228 } \
229 \
Petr Machatae67635d2012-03-21 03:37:39 +0100230 union { \
231 uint##SIZE##_t dst; \
232 char buf[0]; \
233 } u; \
234 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
235 *retp = u.dst; \
236 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200237 }
238
Petr Machatae67635d2012-03-21 03:37:39 +0100239DEF_READER(elf_read_u16, 16)
240DEF_READER(elf_read_u32, 32)
241DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200242
Petr Machatae67635d2012-03-21 03:37:39 +0100243#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200244
Petr Machata1974dbc2011-08-19 18:58:01 +0200245int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200246open_elf(struct ltelf *lte, const char *filename)
247{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100248 lte->fd = open(filename, O_RDONLY);
249 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200250 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200251
Petr Machata02bd9ec2011-09-21 17:38:59 +0200252 elf_version(EV_CURRENT);
253
Juan Cespedesd914a202004-11-10 00:15:33 +0100254#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200256#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100257 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200258#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200259
Petr Machatacc0e1e42012-04-25 13:42:07 +0200260 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
261 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
262 exit(EXIT_FAILURE);
263 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200264
Petr Machatacc0e1e42012-04-25 13:42:07 +0200265 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
266 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
267 filename, elf_errmsg(-1));
268 exit(EXIT_FAILURE);
269 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200270
Petr Machatacc0e1e42012-04-25 13:42:07 +0200271 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
272 fprintf(stderr, "\"%s\" is neither an ELF executable"
273 " nor a shared library\n", filename);
274 exit(EXIT_FAILURE);
275 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200276
Petr Machataddd96a32012-05-17 23:35:26 +0200277 if (1
278#ifdef LT_ELF_MACHINE
279 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
280 || lte->ehdr.e_machine != LT_ELF_MACHINE)
281#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100282#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
284 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100285#endif
286#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100287 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
288 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100289#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200290 ) {
291 fprintf(stderr,
292 "\"%s\" is ELF from incompatible architecture\n",
293 filename);
294 exit(EXIT_FAILURE);
295 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100296
Petr Machata02bd9ec2011-09-21 17:38:59 +0200297 return 0;
298}
299
Petr Machatacc0e1e42012-04-25 13:42:07 +0200300static void
301read_symbol_table(struct ltelf *lte, const char *filename,
302 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
303 Elf_Data **datap, size_t *countp, const char **strsp)
304{
305 *datap = elf_getdata(scn, NULL);
306 *countp = shdr->sh_size / shdr->sh_entsize;
307 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
308 && options.static_filter != NULL) {
309 fprintf(stderr, "Couldn't get data of section"
310 " %s from \"%s\": %s\n",
311 name, filename, elf_errmsg(-1));
312 exit(EXIT_FAILURE);
313 }
314
315 scn = elf_getscn(lte->elf, shdr->sh_link);
316 GElf_Shdr shdr2;
317 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
318 fprintf(stderr, "Couldn't get header of section"
319 " #%d from \"%s\": %s\n",
320 shdr2.sh_link, filename, elf_errmsg(-1));
321 exit(EXIT_FAILURE);
322 }
323
324 Elf_Data *data = elf_getdata(scn, NULL);
325 if (data == NULL || elf_getdata(scn, data) != NULL
326 || shdr2.sh_size != data->d_size || data->d_off) {
327 fprintf(stderr, "Couldn't get data of section"
328 " #%d from \"%s\": %s\n",
329 shdr2.sh_link, filename, elf_errmsg(-1));
330 exit(EXIT_FAILURE);
331 }
332
333 *strsp = data->d_buf;
334}
335
Petr Machatae67635d2012-03-21 03:37:39 +0100336static int
Petr Machatada69ed02012-10-18 17:27:48 +0200337do_init_elf(struct ltelf *lte, const char *filename)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100338{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200339 int i;
340 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100341 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200342
343 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
344 debug(1, "Reading ELF from %s...", filename);
345
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100346 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
347 Elf_Scn *scn;
348 GElf_Shdr shdr;
349 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100350
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200352 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
353 fprintf(stderr, "Couldn't get section #%d from"
354 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
355 exit(EXIT_FAILURE);
356 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100357
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100358 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200359 if (name == NULL) {
360 fprintf(stderr, "Couldn't get name of section #%d from"
361 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
362 exit(EXIT_FAILURE);
363 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100364
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200366 read_symbol_table(lte, filename,
367 scn, &shdr, name, &lte->symtab,
368 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100369
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200371 read_symbol_table(lte, filename,
372 scn, &shdr, name, &lte->dynsym,
373 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100374
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 } else if (shdr.sh_type == SHT_DYNAMIC) {
376 Elf_Data *data;
377 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100378
Petr Machatada69ed02012-10-18 17:27:48 +0200379 lte->dyn_addr = shdr.sh_addr + lte->bias;
Joe Damato87f4f582010-11-08 15:47:36 -0800380 lte->dyn_sz = shdr.sh_size;
381
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100382 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200383 if (data == NULL || elf_getdata(scn, data) != NULL) {
384 fprintf(stderr, "Couldn't get .dynamic data"
385 " from \"%s\": %s\n",
386 filename, strerror(errno));
387 exit(EXIT_FAILURE);
388 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100389
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100390 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
391 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100392
Petr Machatacc0e1e42012-04-25 13:42:07 +0200393 if (gelf_getdyn(data, j, &dyn) == NULL) {
394 fprintf(stderr, "Couldn't get .dynamic"
395 " data from \"%s\": %s\n",
396 filename, strerror(errno));
397 exit(EXIT_FAILURE);
398 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100399 if (dyn.d_tag == DT_JMPREL)
400 relplt_addr = dyn.d_un.d_ptr;
401 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100402 lte->relplt_size = dyn.d_un.d_val;
403 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100404 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 } else if (shdr.sh_type == SHT_PROGBITS
407 || shdr.sh_type == SHT_NOBITS) {
408 if (strcmp(name, ".plt") == 0) {
409 lte->plt_addr = shdr.sh_addr;
410 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100411 lte->plt_data = elf_loaddata(scn, &shdr);
412 if (lte->plt_data == NULL)
413 fprintf(stderr,
414 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200415 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100416 }
417#ifdef ARCH_SUPPORTS_OPD
418 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200419 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100420 lte->opd_size = shdr.sh_size;
421 lte->opd = elf_rawdata(scn, NULL);
422 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100423#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100424 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100425 }
426
Petr Machatacc0e1e42012-04-25 13:42:07 +0200427 if (lte->dynsym == NULL || lte->dynstr == NULL) {
428 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
429 filename);
430 exit(EXIT_FAILURE);
431 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100432
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100433 if (!relplt_addr || !lte->plt_addr) {
434 debug(1, "%s has no PLT relocations", filename);
435 lte->relplt = NULL;
436 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100437 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200438 debug(1, "%s has unknown PLT size", filename);
439 lte->relplt = NULL;
440 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100441 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200442
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
444 Elf_Scn *scn;
445 GElf_Shdr shdr;
446
447 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200448 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
449 fprintf(stderr, "Couldn't get section header"
450 " from \"%s\": %s\n",
451 filename, elf_errmsg(-1));
452 exit(EXIT_FAILURE);
453 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100454 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100455 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100456 lte->relplt = elf_getdata(scn, NULL);
457 lte->relplt_count =
458 shdr.sh_size / shdr.sh_entsize;
459 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200460 || elf_getdata(scn, lte->relplt) != NULL) {
461 fprintf(stderr, "Couldn't get .rel*.plt"
462 " data from \"%s\": %s\n",
463 filename, elf_errmsg(-1));
464 exit(EXIT_FAILURE);
465 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100466 break;
467 }
468 }
469
Petr Machatacc0e1e42012-04-25 13:42:07 +0200470 if (i == lte->ehdr.e_shnum) {
471 fprintf(stderr,
472 "Couldn't find .rel*.plt section in \"%s\"\n",
473 filename);
474 exit(EXIT_FAILURE);
475 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100476
477 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
478 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100479
480 if (soname_offset != 0)
481 lte->soname = lte->dynstr + soname_offset;
482
Petr Machata1974dbc2011-08-19 18:58:01 +0200483 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100484}
485
Joe Damato7a2bdf82010-11-08 15:47:41 -0800486void
Petr Machata588850f2012-10-26 22:26:54 +0200487do_close_elf(struct ltelf *lte)
488{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200489 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100490 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100491 elf_end(lte->elf);
492 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200493}
494
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200495int
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +0200496elf_get_sym_info(struct ltelf *lte, const char *filename,
497 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200498{
499 int i = sym_index;
500 GElf_Rel rel;
501 void *ret;
502
503 if (lte->relplt->d_type == ELF_T_REL) {
504 ret = gelf_getrel(lte->relplt, i, &rel);
505 rela->r_offset = rel.r_offset;
506 rela->r_info = rel.r_info;
507 rela->r_addend = 0;
508 } else {
509 ret = gelf_getrela(lte->relplt, i, rela);
510 }
511
512 if (ret == NULL
513 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
514 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info),
515 sym) == NULL) {
516 fprintf(stderr,
517 "Couldn't get relocation from \"%s\": %s\n",
518 filename, elf_errmsg(-1));
519 exit(EXIT_FAILURE);
520 }
521
522 return 0;
523}
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +0200524
525#ifndef ARCH_HAVE_GET_SYMINFO
526int
527arch_get_sym_info(struct ltelf *lte, const char *filename,
528 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
529{
530 return elf_get_sym_info(lte, filename, sym_index, rela, sym);
531}
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200532#endif
533
Petr Machatae80cace2012-09-28 00:05:08 +0200534static void
535mark_chain_latent(struct library_symbol *libsym)
536{
537 for (; libsym != NULL; libsym = libsym->next) {
538 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
539 libsym->latent = 1;
540 }
541}
542
Petr Machatab5f80ac2012-04-04 01:46:18 +0200543static int
Petr Machata929bd572012-12-17 03:20:34 +0100544populate_plt(struct process *proc, const char *filename,
Petr Machatae80cace2012-09-28 00:05:08 +0200545 struct ltelf *lte, struct library *lib,
546 int latent_plts)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200547{
548 size_t i;
549 for (i = 0; i < lte->relplt_count; ++i) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200550 GElf_Rela rela;
551 GElf_Sym sym;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200552
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200553 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0)
554 continue; /* Skip this entry. */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200555
556 char const *name = lte->dynstr + sym.st_name;
557
Petr Machatae80cace2012-09-28 00:05:08 +0200558 /* If the symbol wasn't matched, reject it, unless we
559 * need to keep latent PLT breakpoints for tracing
560 * exports. */
561 int matched = filter_matches_symbol(options.plt_filter,
562 name, lib);
563 if (!matched && !latent_plts)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200564 continue;
565
Petr Machata218c5ff2012-04-15 04:22:39 +0200566 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200567 switch (arch_elf_add_plt_entry(proc, lte, name,
568 &rela, i, &libsym)) {
569 case plt_default:
570 if (default_elf_add_plt_entry(proc, lte, name,
571 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200572 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200573 case plt_fail:
574 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200575 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200576 case plt_ok:
Petr Machatae80cace2012-09-28 00:05:08 +0200577 if (libsym != NULL) {
578 /* If we are adding those symbols just
579 * for tracing exports, mark them all
580 * latent. */
581 if (!matched)
582 mark_chain_latent(libsym);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200583 library_add_symbol(lib, libsym);
Petr Machatae80cace2012-09-28 00:05:08 +0200584 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200585 }
586 }
587 return 0;
588}
589
Petr Machata157cc4d2012-04-04 19:00:34 +0200590/* When -x rules result in request to trace several aliases, we only
591 * want to add such symbol once. The only way that those symbols
592 * differ in is their name, e.g. in glibc you have __GI___libc_free,
593 * __cfree, __free, __libc_free, cfree and free all defined on the
594 * same address. So instead we keep this unique symbol struct for
595 * each address, and replace name in libsym with a shorter variant if
596 * we find it. */
597struct unique_symbol {
Petr Machatabac2da52012-05-29 00:42:59 +0200598 arch_addr_t addr;
Petr Machata157cc4d2012-04-04 19:00:34 +0200599 struct library_symbol *libsym;
600};
601
602static int
603unique_symbol_cmp(const void *key, const void *val)
604{
605 const struct unique_symbol *sym_key = key;
606 const struct unique_symbol *sym_val = val;
607 return sym_key->addr != sym_val->addr;
608}
609
Petr Machata10bd7a92012-09-28 00:06:56 +0200610static enum callback_status
611symbol_with_address(struct library_symbol *sym, void *addrptr)
612{
613 return sym->enter_addr == *(arch_addr_t *)addrptr
614 ? CBS_STOP : CBS_CONT;
615}
616
Petr Machatada3edbf2012-04-04 02:20:21 +0200617static int
Petr Machata929bd572012-12-17 03:20:34 +0100618populate_this_symtab(struct process *proc, const char *filename,
Petr Machatada3edbf2012-04-04 02:20:21 +0200619 struct ltelf *lte, struct library *lib,
Petr Machata013ef5b2012-09-28 00:00:55 +0200620 Elf_Data *symtab, const char *strtab, size_t size,
621 struct library_exported_name **names)
Petr Machatada3edbf2012-04-04 02:20:21 +0200622{
Petr Machata013ef5b2012-09-28 00:00:55 +0200623 /* If a valid NAMES is passed, we pass in *NAMES a list of
624 * symbol names that this library exports. */
625 if (names != NULL)
626 *names = NULL;
627
Petr Machata157cc4d2012-04-04 19:00:34 +0200628 /* Using sorted array would be arguably better, but this
629 * should be well enough for the number of symbols that we
630 * typically deal with. */
631 size_t num_symbols = 0;
632 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
633 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200634 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
635 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200636 return -1;
637 }
638
Petr Machata40cc53b2012-04-07 01:25:38 +0200639 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200640 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200641 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
642 Elf_Scn *scn = elf_getscn(lte->elf, i);
643 if (scn == NULL)
644 continue;
645 GElf_Shdr shdr;
646 if (gelf_getshdr(scn, &shdr) == NULL)
647 continue;
648 secflags[i] = shdr.sh_flags;
649 }
650
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 Machata09654202012-12-05 17:07:21 +0100720 if (name_copy == NULL) {
721 full_name = strdup(name);
Petr Machata3840f682012-04-06 16:05:41 +0200722 if (full_name == NULL)
723 goto fail;
Petr Machata3840f682012-04-06 16:05:41 +0200724 } else {
Petr Machata09654202012-12-05 17:07:21 +0100725 full_name = name_copy;
726 own_full_name = 0;
Petr Machata3840f682012-04-06 16:05:41 +0200727 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200728
Petr Machata157cc4d2012-04-04 19:00:34 +0200729 /* Look whether we already have a symbol for this
730 * address. If not, add this one. */
731 struct unique_symbol key = { naddr, NULL };
732 struct unique_symbol *unique
733 = lsearch(&key, symbols, &num_symbols,
734 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200735
Petr Machata157cc4d2012-04-04 19:00:34 +0200736 if (unique->libsym == NULL) {
737 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200738 if (libsym == NULL
Petr Machata013ef5b2012-09-28 00:00:55 +0200739 || library_symbol_init(libsym, naddr,
740 full_name, own_full_name,
741 LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200742 --num_symbols;
743 goto fail;
744 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200745 unique->libsym = libsym;
746 unique->addr = naddr;
747
748 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
Petr Machata013ef5b2012-09-28 00:00:55 +0200749 library_symbol_set_name(unique->libsym,
750 full_name, own_full_name);
Petr Machata157cc4d2012-04-04 19:00:34 +0200751
Petr Machata013ef5b2012-09-28 00:00:55 +0200752 } else if (own_full_name) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200753 free(full_name);
754 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200755 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200756
Petr Machata10bd7a92012-09-28 00:06:56 +0200757 /* Now we do the union of this set of unique symbols with
758 * what's already in the library. */
Petr Machata157cc4d2012-04-04 19:00:34 +0200759 for (i = 0; i < num_symbols; ++i) {
Petr Machata10bd7a92012-09-28 00:06:56 +0200760 struct library_symbol *this_sym = symbols[i].libsym;
761 assert(this_sym != NULL);
762 struct library_symbol *other
763 = library_each_symbol(lib, NULL, symbol_with_address,
764 &this_sym->enter_addr);
765 if (other != NULL) {
766 library_symbol_destroy(this_sym);
767 free(this_sym);
768 symbols[i].libsym = NULL;
769 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200770 }
771
Petr Machata10bd7a92012-09-28 00:06:56 +0200772 for (i = 0; i < num_symbols; ++i)
773 if (symbols[i].libsym != NULL)
774 library_add_symbol(lib, symbols[i].libsym);
Petr Machata157cc4d2012-04-04 19:00:34 +0200775
Petr Machata10bd7a92012-09-28 00:06:56 +0200776 free(symbols);
Petr Machatada3edbf2012-04-04 02:20:21 +0200777 return 0;
778}
779
780static int
Petr Machata929bd572012-12-17 03:20:34 +0100781populate_symtab(struct process *proc, const char *filename,
Petr Machata013ef5b2012-09-28 00:00:55 +0200782 struct ltelf *lte, struct library *lib,
783 int symtabs, int exports)
Petr Machatada3edbf2012-04-04 02:20:21 +0200784{
Petr Machata013ef5b2012-09-28 00:00:55 +0200785 int status;
786 if (symtabs && lte->symtab != NULL && lte->strtab != NULL
787 && (status = populate_this_symtab(proc, filename, lte, lib,
788 lte->symtab, lte->strtab,
789 lte->symtab_count, NULL)) < 0)
790 return status;
791
792 /* Check whether we want to trace symbols implemented by this
793 * library (-l). */
794 struct library_exported_name **names = NULL;
795 if (exports) {
796 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
797 names = &lib->exported_names;
798 }
799
800 return populate_this_symtab(proc, filename, lte, lib,
801 lte->dynsym, lte->dynstr,
802 lte->dynsym_count, names);
Petr Machatada3edbf2012-04-04 02:20:21 +0200803}
804
Petr Machatada69ed02012-10-18 17:27:48 +0200805static int
Petr Machata929bd572012-12-17 03:20:34 +0100806read_module(struct library *lib, struct process *proc,
Petr Machatada69ed02012-10-18 17:27:48 +0200807 const char *filename, GElf_Addr bias, int main)
Petr Machatae84fa002012-02-07 13:43:03 +0100808{
Petr Machata29add4f2012-02-18 16:38:05 +0100809 struct ltelf lte = {};
Petr Machatada69ed02012-10-18 17:27:48 +0200810 if (open_elf(&lte, filename) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200811 return -1;
Petr Machatada69ed02012-10-18 17:27:48 +0200812
Petr Machata1c790252012-10-30 23:29:27 +0100813 /* XXX When we abstract ABI into a module, this should instead
814 * become something like
815 *
816 * proc->abi = arch_get_abi(lte.ehdr);
817 *
818 * The code in open_elf needs to be replaced by this logic.
819 * Be warned that libltrace.c calls open_elf as well to
820 * determine whether ABI is supported. This is to get
821 * reasonable error messages when trying to run 64-bit binary
822 * with 32-bit ltrace. It is desirable to preserve this. */
823 proc->e_machine = lte.ehdr.e_machine;
824 proc->e_class = lte.ehdr.e_ident[EI_CLASS];
825 get_arch_dep(proc);
826
Petr Machatada69ed02012-10-18 17:27:48 +0200827 /* Find out the base address. For PIE main binaries we look
828 * into auxv, otherwise we scan phdrs. */
829 if (main && lte.ehdr.e_type == ET_DYN) {
830 arch_addr_t entry;
831 if (process_get_entry(proc, &entry, NULL) < 0) {
832 fprintf(stderr, "Couldn't find entry of PIE %s\n",
833 filename);
834 return -1;
835 }
Petr Machatab024ca32012-10-18 19:57:49 +0200836 /* XXX The double cast should be removed when
837 * arch_addr_t becomes integral type. */
838 lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
839 lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
Petr Machatada69ed02012-10-18 17:27:48 +0200840
841 } else {
842 GElf_Phdr phdr;
843 size_t i;
844 for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
845 if (phdr.p_type == PT_LOAD) {
846 lte.base_addr = phdr.p_vaddr + bias;
847 break;
848 }
849 }
850
851 lte.bias = bias;
852 lte.entry_addr = lte.ehdr.e_entry + lte.bias;
853
854 if (lte.base_addr == 0) {
855 fprintf(stderr,
856 "Couldn't determine base address of %s\n",
857 filename);
858 return -1;
859 }
860 }
861
862 if (do_init_elf(&lte, filename) < 0)
863 return -1;
864
Petr Machatae0615ab2012-04-17 05:17:48 +0200865 if (arch_elf_init(&lte, lib) < 0) {
866 fprintf(stderr, "Backend initialization failed.\n");
867 return -1;
868 }
869
Petr Machatab5f80ac2012-04-04 01:46:18 +0200870 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200871 if (lib == NULL)
872 goto fail;
873
874 /* Note that we set soname and pathname as soon as they are
875 * allocated, so in case of further errors, this get released
876 * when LIB is release, which should happen in the caller when
877 * we return error. */
878
879 if (lib->pathname == NULL) {
880 char *pathname = strdup(filename);
881 if (pathname == NULL)
882 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200883 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800884 }
885
Petr Machata0b55b582012-04-02 00:38:46 +0200886 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200887 char *soname = strdup(lte.soname);
888 if (soname == NULL)
889 goto fail;
890 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200891 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200892 const char *soname = rindex(lib->pathname, '/') + 1;
893 if (soname == NULL)
894 soname = lib->pathname;
895 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200896 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700897
Petr Machataea8eb9a2012-04-17 01:32:07 +0200898 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200899 * arch_addr_t becomes integral type. */
900 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200901 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100902 goto fail;
903
Petr Machataea8eb9a2012-04-17 01:32:07 +0200904 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200905 * arch_addr_t becomes integral type. */
906 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100907 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200908 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200909 * arch_addr_t becomes integral type. */
910 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100911
Petr Machata013ef5b2012-09-28 00:00:55 +0200912 /* There are two reasons that we need to inspect symbol tables
913 * or populate PLT entries. Either the user requested
914 * corresponding tracing features (respectively -x and -e), or
915 * they requested tracing exported symbols (-l).
916 *
917 * In the latter case we need to keep even those PLT slots
918 * that are not requested by -e (but we keep them latent). We
919 * also need to inspect .dynsym to find what exports this
920 * library provide, to turn on existing latent PLT
921 * entries. */
922
923 int plts = filter_matches_library(options.plt_filter, lib);
924 if ((plts || options.export_filter != NULL)
Petr Machatae80cace2012-09-28 00:05:08 +0200925 && populate_plt(proc, filename, &lte, lib,
926 options.export_filter != NULL) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200927 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800928
Petr Machata013ef5b2012-09-28 00:00:55 +0200929 int exports = filter_matches_library(options.export_filter, lib);
930 int symtabs = filter_matches_library(options.static_filter, lib);
931 if ((symtabs || exports)
932 && populate_symtab(proc, filename, &lte, lib,
933 symtabs, exports) < 0)
Petr Machatada3edbf2012-04-04 02:20:21 +0200934 goto fail;
935
Petr Machata2b46cfc2012-02-18 11:17:29 +0100936done:
937 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200938 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200939
940fail:
941 status = -1;
942 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100943}
Petr Machatae84fa002012-02-07 13:43:03 +0100944
Petr Machatada69ed02012-10-18 17:27:48 +0200945int
Petr Machata929bd572012-12-17 03:20:34 +0100946ltelf_read_library(struct library *lib, struct process *proc,
Petr Machatada69ed02012-10-18 17:27:48 +0200947 const char *filename, GElf_Addr bias)
948{
949 return read_module(lib, proc, filename, bias, 0);
950}
951
952
Petr Machata2b46cfc2012-02-18 11:17:29 +0100953struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100954ltelf_read_main_binary(struct process *proc, const char *path)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100955{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200956 struct library *lib = malloc(sizeof(*lib));
957 if (lib == NULL)
958 return NULL;
959 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200960 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200961
Petr Machatafc6ff182012-04-04 13:11:50 +0200962 /* There is a race between running the process and reading its
963 * binary for internal consumption. So open the binary from
964 * the /proc filesystem. XXX Note that there is similar race
965 * for libraries, but there we don't have a nice answer like
966 * that. Presumably we could read the DSOs from the process
967 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100968 char *fname = pid2name(proc->pid);
Petr Machataef0c74d2012-10-27 00:30:57 +0200969 if (fname == NULL)
970 return NULL;
Petr Machatada69ed02012-10-18 17:27:48 +0200971 if (read_module(lib, proc, fname, 0, 1) < 0) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200972 library_destroy(lib);
973 free(lib);
974 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200975 }
Petr Machataef0c74d2012-10-27 00:30:57 +0200976 free(fname);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200977
Petr Machata2b46cfc2012-02-18 11:17:29 +0100978 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200979}