blob: 8997518412645b5fd2b8f622f32e535c674fde13 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machataaafb00b2013-01-08 18:12:07 +01003 * Copyright (C) 2006,2010,2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata64262602012-01-07 03:41:36 +01004 * 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>
Petr Machata97d13662013-11-05 01:46:30 +010039#include <stdbool.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010040#include <stdint.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020041#include <stdio.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010042#include <stdlib.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020043#include <string.h>
Petr Machata513258c2012-12-08 03:47:02 +010044#include <strings.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010045#include <unistd.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020046
Petr Machata64262602012-01-07 03:41:36 +010047#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020048#include "filter.h"
49#include "library.h"
50#include "ltrace-elf.h"
51#include "proc.h"
52#include "debug.h"
53#include "options.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080054
Petr Machatae67635d2012-03-21 03:37:39 +010055#ifndef ARCH_HAVE_LTELF_DATA
56int
Petr Machatae0615ab2012-04-17 05:17:48 +020057arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010058{
59 return 0;
60}
Petr Machatac67a6e62012-03-28 02:39:49 +020061
62void
63arch_elf_destroy(struct ltelf *lte)
64{
65}
Petr Machatae67635d2012-03-21 03:37:39 +010066#endif
67
Petr Machata97d13662013-11-05 01:46:30 +010068#ifndef OS_HAVE_ADD_PLT_ENTRY
69enum plt_status
70os_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
71 const char *a_name, GElf_Rela *rela, size_t ndx,
72 struct library_symbol **ret)
Petr Machatae6523e62012-03-24 04:54:06 +010073{
Petr Machata97d13662013-11-05 01:46:30 +010074 return PLT_DEFAULT;
Petr Machatae6523e62012-03-24 04:54:06 +010075}
Petr Machata97d13662013-11-05 01:46:30 +010076#endif
Petr Machatae6523e62012-03-24 04:54:06 +010077
78#ifndef ARCH_HAVE_ADD_PLT_ENTRY
79enum plt_status
Petr Machata929bd572012-12-17 03:20:34 +010080arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020081 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010082 struct library_symbol **ret)
83{
Petr Machataade3b972012-12-17 03:45:30 +010084 return PLT_DEFAULT;
Petr Machatae6523e62012-03-24 04:54:06 +010085}
86#endif
87
Petr Machataa186b0e2013-10-15 23:32:18 +020088#ifndef OS_HAVE_ADD_FUNC_ENTRY
89enum plt_status
90os_elf_add_func_entry(struct process *proc, struct ltelf *lte,
91 const GElf_Sym *sym,
92 arch_addr_t addr, const char *name,
93 struct library_symbol **ret)
94{
95 if (GELF_ST_TYPE(sym->st_info) != STT_FUNC) {
96 *ret = NULL;
97 return PLT_OK;
98 } else {
99 return PLT_DEFAULT;
100 }
101}
102#endif
103
Petr Machata97d13662013-11-05 01:46:30 +0100104#ifndef ARCH_HAVE_ADD_FUNC_ENTRY
105enum plt_status
106arch_elf_add_func_entry(struct process *proc, struct ltelf *lte,
107 const GElf_Sym *sym,
108 arch_addr_t addr, const char *name,
109 struct library_symbol **ret)
110{
111 return PLT_DEFAULT;
112}
113#endif
114
Petr Machatae67635d2012-03-21 03:37:39 +0100115Elf_Data *
116elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200117{
118 Elf_Data *data = elf_getdata(scn, NULL);
119 if (data == NULL || elf_getdata(scn, data) != NULL
120 || data->d_off || data->d_size != shdr->sh_size)
121 return NULL;
122 return data;
123}
124
Petr Machatae67635d2012-03-21 03:37:39 +0100125static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100126elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
127 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
128 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200129{
130 int i;
131 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
132 Elf_Scn *scn;
133 GElf_Shdr shdr;
134
135 scn = elf_getscn(lte->elf, i);
136 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
137 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100138 return -1;
139 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100140 if (predicate(scn, &shdr, data)) {
141 *tgt_sec = scn;
142 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200143 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100144 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200145 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100146
Petr Machata99b19eb2013-02-05 01:48:21 +0100147 *tgt_sec = NULL;
148 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100149}
150
151static int
152inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
153{
154 GElf_Addr addr = *(GElf_Addr *)data;
155 return addr >= shdr->sh_addr
156 && addr < shdr->sh_addr + shdr->sh_size;
157}
158
159int
160elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
161 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
162{
163 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
164 &inside_p, &addr);
165}
166
167static int
168type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
169{
170 GElf_Word type = *(GElf_Word *)data;
171 return shdr->sh_type == type;
172}
173
174int
175elf_get_section_type(struct ltelf *lte, GElf_Word type,
176 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
177{
178 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
179 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100180}
181
Petr Machata5b3e26a2012-04-30 20:53:22 +0200182struct section_named_data {
183 struct ltelf *lte;
184 const char *name;
185};
186
187static int
188name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
189{
190 struct section_named_data *data = d;
191 const char *name = elf_strptr(data->lte->elf,
192 data->lte->ehdr.e_shstrndx,
193 shdr->sh_name);
194 return strcmp(name, data->name) == 0;
195}
196
197int
198elf_get_section_named(struct ltelf *lte, const char *name,
199 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
200{
201 struct section_named_data data = {
202 .lte = lte,
203 .name = name,
204 };
205 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
206 &name_p, &data);
207}
208
Petr Machata7a29f9e2013-10-14 20:04:09 +0200209static struct elf_each_symbol_t
210each_symbol_in(Elf_Data *symtab, const char *strtab, size_t count,
211 unsigned i,
212 enum callback_status (*cb)(GElf_Sym *symbol,
213 const char *name, void *data),
214 void *data)
215{
216 for (; i < count; ++i) {
217 GElf_Sym sym;
218 if (gelf_getsym(symtab, i, &sym) == NULL)
219 return (struct elf_each_symbol_t){ i, -2 };
220
221 switch (cb(&sym, strtab + sym.st_name, data)) {
222 case CBS_FAIL:
223 return (struct elf_each_symbol_t){ i, -1 };
224 case CBS_STOP:
225 return (struct elf_each_symbol_t){ i + 1, 0 };
226 case CBS_CONT:
227 break;
228 }
229 }
230
231 return (struct elf_each_symbol_t){ 0, 0 };
232}
233
234/* N.B.: gelf_getsym takes integer argument. Since negative values
235 * are invalid as indices, we can use the extra bit to encode which
236 * symbol table we are looking into. ltrace currently doesn't handle
237 * more than two symbol tables anyway, nor does it handle the xindex
238 * stuff. */
239struct elf_each_symbol_t
240elf_each_symbol(struct ltelf *lte, unsigned start_after,
241 enum callback_status (*cb)(GElf_Sym *symbol,
242 const char *name, void *data),
243 void *data)
244{
245 unsigned index = start_after == 0 ? 0 : start_after >> 1;
246
247 /* Go through static symbol table first. */
248 if ((start_after & 0x1) == 0) {
249 struct elf_each_symbol_t st
250 = each_symbol_in(lte->symtab, lte->strtab,
251 lte->symtab_count, index, cb, data);
252
253 /* If the iteration stopped prematurely, bail out. */
254 if (st.restart != 0)
255 return ((struct elf_each_symbol_t)
256 { st.restart << 1, st.status });
257 }
258
259 struct elf_each_symbol_t st
260 = each_symbol_in(lte->dynsym, lte->dynstr, lte->dynsym_count,
261 index, cb, data);
262 if (st.restart != 0)
263 return ((struct elf_each_symbol_t)
264 { st.restart << 1 | 0x1, st.status });
265
266 return (struct elf_each_symbol_t){ 0, 0 };
267}
268
Petr Machata5c371712013-02-05 01:52:37 +0100269int
270elf_can_read_next(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100271{
272 assert(data != NULL);
273 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200274 debug(1, "Not enough data to read %"PRId64"-byte value"
275 " at offset %"PRId64".", size, offset);
Petr Machata5c371712013-02-05 01:52:37 +0100276 return 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100277 }
Petr Machata5c371712013-02-05 01:52:37 +0100278 return 1;
Petr Machatafe1c1712010-10-27 16:57:34 +0200279}
280
Petr Machatae67635d2012-03-21 03:37:39 +0100281#define DEF_READER(NAME, SIZE) \
282 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200283 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100284 { \
Petr Machata5c371712013-02-05 01:52:37 +0100285 if (!elf_can_read_next(data, offset, SIZE / 8)) \
Petr Machatae67635d2012-03-21 03:37:39 +0100286 return -1; \
287 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200288 if (data->d_buf == NULL) /* NODATA section */ { \
289 *retp = 0; \
290 return 0; \
291 } \
292 \
Petr Machatae67635d2012-03-21 03:37:39 +0100293 union { \
294 uint##SIZE##_t dst; \
295 char buf[0]; \
296 } u; \
297 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
298 *retp = u.dst; \
299 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200300 }
301
Petr Machata3c636fb2013-02-05 01:48:54 +0100302DEF_READER(elf_read_u8, 8)
Petr Machatae67635d2012-03-21 03:37:39 +0100303DEF_READER(elf_read_u16, 16)
304DEF_READER(elf_read_u32, 32)
305DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200306
Petr Machatae67635d2012-03-21 03:37:39 +0100307#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200308
Petr Machata439ab5b2013-02-05 01:50:28 +0100309#define DEF_READER(NAME, SIZE) \
310 int \
311 NAME(Elf_Data *data, GElf_Xword *offset, uint##SIZE##_t *retp) \
312 { \
313 int rc = elf_read_u##SIZE(data, *offset, retp); \
314 if (rc < 0) \
315 return rc; \
316 *offset += SIZE / 8; \
317 return 0; \
318 }
319
320DEF_READER(elf_read_next_u8, 8)
321DEF_READER(elf_read_next_u16, 16)
322DEF_READER(elf_read_next_u32, 32)
323DEF_READER(elf_read_next_u64, 64)
324
325#undef DEF_READER
326
Petr Machata1974dbc2011-08-19 18:58:01 +0200327int
Petr Machata184779e2013-02-05 01:52:05 +0100328elf_read_next_uleb128(Elf_Data *data, GElf_Xword *offset, uint64_t *retp)
329{
330 uint64_t result = 0;
331 int shift = 0;
332 int size = 8 * sizeof result;
333
334 while (1) {
335 uint8_t byte;
336 if (elf_read_next_u8(data, offset, &byte) < 0)
337 return -1;
338
339 uint8_t payload = byte & 0x7f;
340 result |= (uint64_t)payload << shift;
341 shift += 7;
342 if (shift > size && byte != 0x1)
343 return -1;
344 if ((byte & 0x80) == 0)
345 break;
346 }
347
348 if (retp != NULL)
349 *retp = result;
350 return 0;
351}
352
353int
354elf_read_uleb128(Elf_Data *data, GElf_Xword offset, uint64_t *retp)
355{
356 return elf_read_next_uleb128(data, &offset, retp);
357}
358
359int
Petr Machata673ff512013-10-25 23:45:39 +0200360ltelf_init(struct ltelf *lte, const char *filename)
Petr Machata02bd9ec2011-09-21 17:38:59 +0200361{
Petr Machata673ff512013-10-25 23:45:39 +0200362 memset(lte, 0, sizeof *lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100363 lte->fd = open(filename, O_RDONLY);
Petr Machata2bfea352013-11-11 02:27:33 +0100364 if (lte->fd == -1) {
365 fprintf(stderr, "Can't open %s: %s\n", filename,
366 strerror(errno));
Petr Machata1974dbc2011-08-19 18:58:01 +0200367 return 1;
Petr Machata2bfea352013-11-11 02:27:33 +0100368 }
Juan Cespedes96935a91997-08-09 23:45:39 +0200369
Petr Machata02bd9ec2011-09-21 17:38:59 +0200370 elf_version(EV_CURRENT);
371
Juan Cespedesd914a202004-11-10 00:15:33 +0100372#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200374#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100375 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200376#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200377
Petr Machatacc0e1e42012-04-25 13:42:07 +0200378 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
379 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
380 exit(EXIT_FAILURE);
381 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200382
Petr Machatacc0e1e42012-04-25 13:42:07 +0200383 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
384 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
385 filename, elf_errmsg(-1));
386 exit(EXIT_FAILURE);
387 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200388
Petr Machatacc0e1e42012-04-25 13:42:07 +0200389 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
390 fprintf(stderr, "\"%s\" is neither an ELF executable"
391 " nor a shared library\n", filename);
392 exit(EXIT_FAILURE);
393 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200394
Petr Machataddd96a32012-05-17 23:35:26 +0200395 if (1
396#ifdef LT_ELF_MACHINE
397 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
398 || lte->ehdr.e_machine != LT_ELF_MACHINE)
399#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100400#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
402 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100403#endif
404#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100405 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
406 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100407#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200408 ) {
409 fprintf(stderr,
410 "\"%s\" is ELF from incompatible architecture\n",
411 filename);
412 exit(EXIT_FAILURE);
413 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100414
Petr Machata673ff512013-10-25 23:45:39 +0200415 VECT_INIT(&lte->plt_relocs, GElf_Rela);
416
Petr Machata02bd9ec2011-09-21 17:38:59 +0200417 return 0;
418}
419
Petr Machata673ff512013-10-25 23:45:39 +0200420void
421ltelf_destroy(struct ltelf *lte)
422{
423 debug(DEBUG_FUNCTION, "close_elf()");
424 elf_end(lte->elf);
425 close(lte->fd);
426 VECT_DESTROY(&lte->plt_relocs, GElf_Rela, NULL, NULL);
427}
428
Petr Machatacc0e1e42012-04-25 13:42:07 +0200429static void
430read_symbol_table(struct ltelf *lte, const char *filename,
431 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
432 Elf_Data **datap, size_t *countp, const char **strsp)
433{
434 *datap = elf_getdata(scn, NULL);
435 *countp = shdr->sh_size / shdr->sh_entsize;
436 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
437 && options.static_filter != NULL) {
438 fprintf(stderr, "Couldn't get data of section"
439 " %s from \"%s\": %s\n",
440 name, filename, elf_errmsg(-1));
441 exit(EXIT_FAILURE);
442 }
443
444 scn = elf_getscn(lte->elf, shdr->sh_link);
445 GElf_Shdr shdr2;
446 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
447 fprintf(stderr, "Couldn't get header of section"
448 " #%d from \"%s\": %s\n",
Petr Machata02ecb492013-01-09 22:05:24 +0100449 shdr->sh_link, filename, elf_errmsg(-1));
Petr Machatacc0e1e42012-04-25 13:42:07 +0200450 exit(EXIT_FAILURE);
451 }
452
453 Elf_Data *data = elf_getdata(scn, NULL);
454 if (data == NULL || elf_getdata(scn, data) != NULL
455 || shdr2.sh_size != data->d_size || data->d_off) {
456 fprintf(stderr, "Couldn't get data of section"
457 " #%d from \"%s\": %s\n",
458 shdr2.sh_link, filename, elf_errmsg(-1));
459 exit(EXIT_FAILURE);
460 }
461
462 *strsp = data->d_buf;
463}
464
Petr Machatae67635d2012-03-21 03:37:39 +0100465static int
Petr Machata673ff512013-10-25 23:45:39 +0200466rel_to_rela(struct ltelf *lte, const GElf_Rel *rel, GElf_Rela *rela)
467{
468 rela->r_offset = rel->r_offset;
469 rela->r_info = rel->r_info;
470
471 Elf_Scn *sec;
472 GElf_Shdr shdr;
473 if (elf_get_section_covering(lte, rel->r_offset, &sec, &shdr) < 0
474 || sec == NULL)
475 return -1;
476
477 Elf_Data *data = elf_loaddata(sec, &shdr);
478 if (data == NULL)
479 return -1;
480
481 GElf_Xword offset = rel->r_offset - shdr.sh_addr - data->d_off;
482 uint64_t value;
483 if (lte->ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
484 uint32_t tmp;
485 if (elf_read_u32(data, offset, &tmp) < 0)
486 return -1;
487 value = tmp;
488 } else if (elf_read_u64(data, offset, &value) < 0) {
489 return -1;
490 }
491
492 rela->r_addend = value;
493 return 0;
494}
495
496int
Petr Machata0ed53992013-11-04 17:01:43 +0100497elf_read_relocs(struct ltelf *lte, Elf_Scn *scn, GElf_Shdr *shdr,
498 struct vect *rela_vec)
Petr Machata673ff512013-10-25 23:45:39 +0200499{
500 if (vect_reserve_additional(rela_vec, lte->ehdr.e_shnum) < 0)
501 return -1;
502
503 Elf_Data *relplt = elf_loaddata(scn, shdr);
504 if (relplt == NULL) {
505 fprintf(stderr, "Couldn't load .rel*.plt data.\n");
506 return -1;
507 }
508
509 if ((shdr->sh_size % shdr->sh_entsize) != 0) {
510 fprintf(stderr, ".rel*.plt size (%" PRIx64 "d) not a multiple "
511 "of its sh_entsize (%" PRIx64 "d).\n",
512 shdr->sh_size, shdr->sh_entsize);
513 return -1;
514 }
515
516 GElf_Xword relplt_count = shdr->sh_size / shdr->sh_entsize;
517 GElf_Xword i;
518 for (i = 0; i < relplt_count; ++i) {
519 GElf_Rela rela;
520 if (relplt->d_type == ELF_T_REL) {
521 GElf_Rel rel;
522 if (gelf_getrel(relplt, i, &rel) == NULL
523 || rel_to_rela(lte, &rel, &rela) < 0)
524 return -1;
525
526 } else if (gelf_getrela(relplt, i, &rela) == NULL) {
527 return -1;
528 }
529
530 if (VECT_PUSHBACK(rela_vec, &rela) < 0)
531 return -1;
532 }
533
534 return 0;
535}
536
Petr Machata4f2f66e2013-11-04 22:45:34 -0500537int
538elf_load_dynamic_entry(struct ltelf *lte, int tag, GElf_Addr *valuep)
539{
540 Elf_Scn *scn;
541 GElf_Shdr shdr;
542 if (elf_get_section_type(lte, SHT_DYNAMIC, &scn, &shdr) < 0
543 || scn == NULL) {
544 fail:
545 fprintf(stderr, "Couldn't get SHT_DYNAMIC: %s\n",
546 elf_errmsg(-1));
547 return -1;
548 }
549
550 Elf_Data *data = elf_loaddata(scn, &shdr);
551 if (data == NULL)
552 goto fail;
553
554 size_t j;
555 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
556 GElf_Dyn dyn;
557 if (gelf_getdyn(data, j, &dyn) == NULL)
558 goto fail;
559
560 if(dyn.d_tag == tag) {
561 *valuep = dyn.d_un.d_ptr;
562 return 0;
563 }
564 }
565
566 return -1;
567}
568
Petr Machata673ff512013-10-25 23:45:39 +0200569static int
570ltelf_read_elf(struct ltelf *lte, const char *filename)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100571{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200572 int i;
573 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100574 GElf_Addr soname_offset = 0;
Petr Machata673ff512013-10-25 23:45:39 +0200575 GElf_Xword relplt_size = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200576
Petr Machata673ff512013-10-25 23:45:39 +0200577 debug(DEBUG_FUNCTION, "ltelf_read_elf(filename=%s)", filename);
Petr Machata02bd9ec2011-09-21 17:38:59 +0200578 debug(1, "Reading ELF from %s...", filename);
579
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100580 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
581 Elf_Scn *scn;
582 GElf_Shdr shdr;
583 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100584
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100585 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200586 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
587 fprintf(stderr, "Couldn't get section #%d from"
588 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
589 exit(EXIT_FAILURE);
590 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100591
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100592 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200593 if (name == NULL) {
594 fprintf(stderr, "Couldn't get name of section #%d from"
595 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
596 exit(EXIT_FAILURE);
597 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100598
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200600 read_symbol_table(lte, filename,
601 scn, &shdr, name, &lte->symtab,
602 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100603
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100604 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200605 read_symbol_table(lte, filename,
606 scn, &shdr, name, &lte->dynsym,
607 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100608
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100609 } else if (shdr.sh_type == SHT_DYNAMIC) {
610 Elf_Data *data;
611 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100612
Petr Machatada69ed02012-10-18 17:27:48 +0200613 lte->dyn_addr = shdr.sh_addr + lte->bias;
Joe Damato87f4f582010-11-08 15:47:36 -0800614 lte->dyn_sz = shdr.sh_size;
615
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100616 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200617 if (data == NULL || elf_getdata(scn, data) != NULL) {
618 fprintf(stderr, "Couldn't get .dynamic data"
619 " from \"%s\": %s\n",
620 filename, strerror(errno));
621 exit(EXIT_FAILURE);
622 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100623
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100624 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
625 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100626
Petr Machatacc0e1e42012-04-25 13:42:07 +0200627 if (gelf_getdyn(data, j, &dyn) == NULL) {
628 fprintf(stderr, "Couldn't get .dynamic"
629 " data from \"%s\": %s\n",
630 filename, strerror(errno));
631 exit(EXIT_FAILURE);
632 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100633 if (dyn.d_tag == DT_JMPREL)
634 relplt_addr = dyn.d_un.d_ptr;
635 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machata673ff512013-10-25 23:45:39 +0200636 relplt_size = dyn.d_un.d_val;
Petr Machatae67635d2012-03-21 03:37:39 +0100637 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100638 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100639 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100640 } else if (shdr.sh_type == SHT_PROGBITS
641 || shdr.sh_type == SHT_NOBITS) {
642 if (strcmp(name, ".plt") == 0) {
643 lte->plt_addr = shdr.sh_addr;
644 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100645 lte->plt_data = elf_loaddata(scn, &shdr);
646 if (lte->plt_data == NULL)
647 fprintf(stderr,
648 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200649 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100650 }
651#ifdef ARCH_SUPPORTS_OPD
652 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200653 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100654 lte->opd_size = shdr.sh_size;
655 lte->opd = elf_rawdata(scn, NULL);
656 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100657#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100658 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100659 }
660
Petr Machatacc0e1e42012-04-25 13:42:07 +0200661 if (lte->dynsym == NULL || lte->dynstr == NULL) {
662 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
663 filename);
664 exit(EXIT_FAILURE);
665 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100666
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100667 if (!relplt_addr || !lte->plt_addr) {
668 debug(1, "%s has no PLT relocations", filename);
Petr Machata673ff512013-10-25 23:45:39 +0200669 } else if (relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200670 debug(1, "%s has unknown PLT size", filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100671 } else {
672 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
673 Elf_Scn *scn;
674 GElf_Shdr shdr;
675
676 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200677 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
678 fprintf(stderr, "Couldn't get section header"
679 " from \"%s\": %s\n",
680 filename, elf_errmsg(-1));
681 exit(EXIT_FAILURE);
682 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100683 if (shdr.sh_addr == relplt_addr
Petr Machata673ff512013-10-25 23:45:39 +0200684 && shdr.sh_size == relplt_size) {
Petr Machata0ed53992013-11-04 17:01:43 +0100685 if (elf_read_relocs(lte, scn, &shdr,
686 &lte->plt_relocs) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200687 fprintf(stderr, "Couldn't get .rel*.plt"
688 " data from \"%s\": %s\n",
689 filename, elf_errmsg(-1));
690 exit(EXIT_FAILURE);
691 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100692 break;
693 }
694 }
695
Petr Machatacc0e1e42012-04-25 13:42:07 +0200696 if (i == lte->ehdr.e_shnum) {
697 fprintf(stderr,
698 "Couldn't find .rel*.plt section in \"%s\"\n",
699 filename);
700 exit(EXIT_FAILURE);
701 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100702 }
Petr Machata673ff512013-10-25 23:45:39 +0200703 debug(1, "%s %zd PLT relocations", filename,
704 vect_size(&lte->plt_relocs));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100705
706 if (soname_offset != 0)
707 lte->soname = lte->dynstr + soname_offset;
708
Petr Machata1974dbc2011-08-19 18:58:01 +0200709 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100710}
711
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +0200712#ifndef ARCH_HAVE_GET_SYMINFO
713int
714arch_get_sym_info(struct ltelf *lte, const char *filename,
715 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
716{
Petr Machata673ff512013-10-25 23:45:39 +0200717 return gelf_getsym(lte->dynsym,
718 ELF64_R_SYM(rela->r_info), sym) != NULL ? 0 : -1;
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +0200719}
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200720#endif
721
Petr Machata97d13662013-11-05 01:46:30 +0100722int
723default_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
724 const char *a_name, GElf_Rela *rela, size_t ndx,
725 struct library_symbol **ret)
726{
727 char *name = strdup(a_name);
728 if (name == NULL) {
729 fail_message:
730 fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n",
731 strerror(errno));
732 fail:
733 free(name);
734 return -1;
735 }
736
737 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
738
739 struct library_symbol *libsym = malloc(sizeof(*libsym));
740 if (libsym == NULL)
741 goto fail_message;
742
743 /* XXX The double cast should be removed when
744 * arch_addr_t becomes integral type. */
745 arch_addr_t taddr = (arch_addr_t)
746 (uintptr_t)(addr + lte->bias);
747
748 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
749 free(libsym);
750 goto fail;
751 }
752
753 libsym->next = *ret;
754 *ret = libsym;
755 return 0;
756}
757
758int
759elf_add_plt_entry(struct process *proc, struct ltelf *lte,
760 const char *name, GElf_Rela *rela, size_t idx,
761 struct library_symbol **ret)
762{
763 enum plt_status plts
764 = arch_elf_add_plt_entry(proc, lte, name, rela, idx, ret);
765
766 if (plts == PLT_DEFAULT)
767 plts = os_elf_add_plt_entry(proc, lte, name, rela, idx, ret);
768
769 switch (plts) {
770 case PLT_DEFAULT:
771 return default_elf_add_plt_entry(proc, lte, name,
772 rela, idx, ret);
773 case PLT_FAIL:
774 return -1;
775 case PLT_OK:
776 return 0;
777 }
778
779 assert(! "Invalid return from X_elf_add_plt_entry!");
780 abort();
781}
782
Petr Machatae80cace2012-09-28 00:05:08 +0200783static void
784mark_chain_latent(struct library_symbol *libsym)
785{
786 for (; libsym != NULL; libsym = libsym->next) {
787 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
788 libsym->latent = 1;
789 }
790}
791
Petr Machatab420a222013-10-15 10:46:28 +0200792static void
793filter_symbol_chain(struct filter *filter,
794 struct library_symbol **libsymp, struct library *lib)
795{
796 assert(libsymp != NULL);
797 struct library_symbol **ptr = libsymp;
798 while (*ptr != NULL) {
799 if (filter_matches_symbol(filter, (*ptr)->name, lib)) {
800 ptr = &(*ptr)->next;
801 } else {
802 struct library_symbol *sym = *ptr;
803 *ptr = (*ptr)->next;
804 library_symbol_destroy(sym);
805 free(sym);
806 }
807 }
808}
809
Petr Machatab5f80ac2012-04-04 01:46:18 +0200810static int
Petr Machata929bd572012-12-17 03:20:34 +0100811populate_plt(struct process *proc, const char *filename,
Petr Machata97d13662013-11-05 01:46:30 +0100812 struct ltelf *lte, struct library *lib)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200813{
Petr Machata97d13662013-11-05 01:46:30 +0100814 const bool latent_plts = options.export_filter != NULL;
815 const size_t count = vect_size(&lte->plt_relocs);
816
Petr Machatab5f80ac2012-04-04 01:46:18 +0200817 size_t i;
Petr Machata673ff512013-10-25 23:45:39 +0200818 for (i = 0; i < count; ++i) {
819 GElf_Rela *rela = VECT_ELEMENT(&lte->plt_relocs, GElf_Rela, i);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200820 GElf_Sym sym;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200821
Petr Machata673ff512013-10-25 23:45:39 +0200822 switch (arch_get_sym_info(lte, filename, i, rela, &sym)) {
Petr Machata552d75e2013-10-14 17:04:58 +0200823 default:
824 fprintf(stderr,
825 "Couldn't get relocation for symbol #%zd"
826 " from \"%s\": %s\n",
827 i, filename, elf_errmsg(-1));
828 /* Fall through. */
829 case 1:
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200830 continue; /* Skip this entry. */
Petr Machata552d75e2013-10-14 17:04:58 +0200831 case 0:
832 break;
833 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200834
835 char const *name = lte->dynstr + sym.st_name;
Petr Machatae80cace2012-09-28 00:05:08 +0200836 int matched = filter_matches_symbol(options.plt_filter,
837 name, lib);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200838
Petr Machata218c5ff2012-04-15 04:22:39 +0200839 struct library_symbol *libsym = NULL;
Petr Machata97d13662013-11-05 01:46:30 +0100840 if (elf_add_plt_entry(proc, lte, name, rela, i, &libsym) < 0)
Petr Machataa186b0e2013-10-15 23:32:18 +0200841 return -1;
Petr Machatab420a222013-10-15 10:46:28 +0200842
Petr Machata97d13662013-11-05 01:46:30 +0100843 /* If we didn't match the PLT entry, filter the chain
844 * to only include the matching symbols (but include
845 * all if we are adding latent symbols) to allow
846 * backends to override the PLT symbol's name. */
847
848 if (! matched && ! latent_plts)
849 filter_symbol_chain(options.plt_filter, &libsym, lib);
850
851 if (libsym != NULL) {
852 /* If we are adding those symbols just for
853 * tracing exports, mark them all latent. */
854 if (! matched && latent_plts)
855 mark_chain_latent(libsym);
856 library_add_symbol(lib, libsym);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200857 }
858 }
859 return 0;
860}
861
Petr Machata97d13662013-11-05 01:46:30 +0100862static void
863delete_symbol_chain(struct library_symbol *libsym)
864{
865 while (libsym != NULL) {
866 struct library_symbol *tmp = libsym->next;
867 library_symbol_destroy(libsym);
868 free(libsym);
869 libsym = tmp;
870 }
871}
872
Petr Machata157cc4d2012-04-04 19:00:34 +0200873/* When -x rules result in request to trace several aliases, we only
874 * want to add such symbol once. The only way that those symbols
875 * differ in is their name, e.g. in glibc you have __GI___libc_free,
876 * __cfree, __free, __libc_free, cfree and free all defined on the
877 * same address. So instead we keep this unique symbol struct for
878 * each address, and replace name in libsym with a shorter variant if
879 * we find it. */
880struct unique_symbol {
Petr Machatabac2da52012-05-29 00:42:59 +0200881 arch_addr_t addr;
Petr Machata157cc4d2012-04-04 19:00:34 +0200882 struct library_symbol *libsym;
883};
884
885static int
886unique_symbol_cmp(const void *key, const void *val)
887{
888 const struct unique_symbol *sym_key = key;
889 const struct unique_symbol *sym_val = val;
890 return sym_key->addr != sym_val->addr;
891}
892
Petr Machata10bd7a92012-09-28 00:06:56 +0200893static enum callback_status
894symbol_with_address(struct library_symbol *sym, void *addrptr)
895{
896 return sym->enter_addr == *(arch_addr_t *)addrptr
897 ? CBS_STOP : CBS_CONT;
898}
899
Petr Machatada3edbf2012-04-04 02:20:21 +0200900static int
Petr Machata929bd572012-12-17 03:20:34 +0100901populate_this_symtab(struct process *proc, const char *filename,
Petr Machatada3edbf2012-04-04 02:20:21 +0200902 struct ltelf *lte, struct library *lib,
Petr Machata1187af02013-10-14 20:08:20 +0200903 Elf_Data *symtab, const char *strtab, size_t count,
Petr Machata013ef5b2012-09-28 00:00:55 +0200904 struct library_exported_name **names)
Petr Machatada3edbf2012-04-04 02:20:21 +0200905{
Petr Machata013ef5b2012-09-28 00:00:55 +0200906 /* If a valid NAMES is passed, we pass in *NAMES a list of
907 * symbol names that this library exports. */
908 if (names != NULL)
909 *names = NULL;
910
Petr Machata157cc4d2012-04-04 19:00:34 +0200911 /* Using sorted array would be arguably better, but this
912 * should be well enough for the number of symbols that we
913 * typically deal with. */
914 size_t num_symbols = 0;
Petr Machata1187af02013-10-14 20:08:20 +0200915 struct unique_symbol *symbols = malloc(sizeof(*symbols) * count);
Petr Machata157cc4d2012-04-04 19:00:34 +0200916 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200917 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
918 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200919 return -1;
920 }
921
Petr Machata40cc53b2012-04-07 01:25:38 +0200922 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200923 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200924 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
925 Elf_Scn *scn = elf_getscn(lte->elf, i);
Petr Machata40cc53b2012-04-07 01:25:38 +0200926 GElf_Shdr shdr;
Petr Machataaeb54902013-01-09 22:10:30 +0100927 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
928 secflags[i] = 0;
929 else
930 secflags[i] = shdr.sh_flags;
Petr Machata40cc53b2012-04-07 01:25:38 +0200931 }
932
Petr Machata1187af02013-10-14 20:08:20 +0200933 for (i = 0; i < count; ++i) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200934 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200935 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200936 fprintf(stderr,
937 "couldn't get symbol #%zd from %s: %s\n",
938 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200939 continue;
940 }
941
Petr Machataa186b0e2013-10-15 23:32:18 +0200942 if (sym.st_value == 0 || sym.st_shndx == STN_UNDEF
943 /* Also ignore any special values besides direct
944 * section references. */
945 || sym.st_shndx >= lte->ehdr.e_shnum)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200946 continue;
947
Petr Machata013ef5b2012-09-28 00:00:55 +0200948 /* Find symbol name and snip version. */
Petr Machata2bbeac42012-04-30 20:48:34 +0200949 const char *orig_name = strtab + sym.st_name;
950 const char *version = strchr(orig_name, '@');
951 size_t len = version != NULL ? (assert(version > orig_name),
952 (size_t)(version - orig_name))
953 : strlen(orig_name);
954 char name[len + 1];
955 memcpy(name, orig_name, len);
956 name[len] = 0;
957
Petr Machata013ef5b2012-09-28 00:00:55 +0200958 /* If we are interested in exports, store this name. */
Petr Machata013ef5b2012-09-28 00:00:55 +0200959 if (names != NULL) {
Petr Machataa186b0e2013-10-15 23:32:18 +0200960 struct library_exported_name *export
961 = malloc(sizeof *export);
962 char *name_copy = strdup(name);
Petr Machata013ef5b2012-09-28 00:00:55 +0200963
Petr Machataa186b0e2013-10-15 23:32:18 +0200964 if (name_copy == NULL || export == NULL) {
Petr Machata013ef5b2012-09-28 00:00:55 +0200965 free(name_copy);
Petr Machataa186b0e2013-10-15 23:32:18 +0200966 free(export);
Petr Machata013ef5b2012-09-28 00:00:55 +0200967 fprintf(stderr, "Couldn't store symbol %s. "
968 "Tracing may be incomplete.\n", name);
969 } else {
970 export->name = name_copy;
971 export->own_name = 1;
972 export->next = *names;
973 *names = export;
974 }
975 }
976
977 /* If the symbol is not matched, skip it. We already
978 * stored it to export list above. */
Petr Machatada3edbf2012-04-04 02:20:21 +0200979 if (!filter_matches_symbol(options.static_filter, name, lib))
980 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200981
Petr Machatabac2da52012-05-29 00:42:59 +0200982 arch_addr_t addr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +0200983 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatabac2da52012-05-29 00:42:59 +0200984 arch_addr_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200985
986 /* On arches that support OPD, the value of typical
987 * function symbol will be a pointer to .opd, but some
988 * will point directly to .text. We don't want to
989 * translate those. */
990 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
991 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200992 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200993 fprintf(stderr,
994 "couldn't translate address of %s@%s: %s\n",
995 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200996 continue;
997 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200998
Petr Machataa186b0e2013-10-15 23:32:18 +0200999 char *full_name = strdup(name);
1000 if (full_name == NULL) {
1001 fprintf(stderr, "couldn't copy name of %s@%s: %s\n",
1002 name, lib->soname, strerror(errno));
1003 continue;
Petr Machata3840f682012-04-06 16:05:41 +02001004 }
Petr Machata4de6b6b2012-04-04 14:06:09 +02001005
Petr Machataa186b0e2013-10-15 23:32:18 +02001006 struct library_symbol *libsym = NULL;
Petr Machata97d13662013-11-05 01:46:30 +01001007 enum plt_status plts
1008 = arch_elf_add_func_entry(proc, lte, &sym,
1009 naddr, full_name, &libsym);
1010 if (plts == PLT_DEFAULT)
1011 plts = os_elf_add_func_entry(proc, lte, &sym,
1012 naddr, full_name, &libsym);
1013
1014 switch (plts) {
Petr Machataa186b0e2013-10-15 23:32:18 +02001015 case PLT_DEFAULT:;
1016 /* Put the default symbol to the chain. */
1017 struct library_symbol *tmp = malloc(sizeof *tmp);
1018 if (tmp == NULL
1019 || library_symbol_init(tmp, naddr, full_name, 1,
Petr Machata013ef5b2012-09-28 00:00:55 +02001020 LS_TOPLT_NONE) < 0) {
Petr Machataa186b0e2013-10-15 23:32:18 +02001021 free(tmp);
1022
1023 /* Either add the whole bunch, or none
1024 * of it. Note that for PLT_FAIL we
1025 * don't do this--it's the callee's
1026 * job to clean up after itself before
1027 * it bails out. */
1028 delete_symbol_chain(libsym);
1029 libsym = NULL;
1030
1031 case PLT_FAIL:
1032 fprintf(stderr, "Couldn't add symbol %s@%s "
1033 "for tracing.\n", name, lib->soname);
1034
1035 break;
Petr Machata157cc4d2012-04-04 19:00:34 +02001036 }
Petr Machata157cc4d2012-04-04 19:00:34 +02001037
Petr Machataa186b0e2013-10-15 23:32:18 +02001038 full_name = NULL;
1039 tmp->next = libsym;
1040 libsym = tmp;
1041 break;
Petr Machata157cc4d2012-04-04 19:00:34 +02001042
Petr Machataa186b0e2013-10-15 23:32:18 +02001043 case PLT_OK:
1044 break;
1045 }
1046
1047 free(full_name);
1048
1049 struct library_symbol *tmp;
1050 for (tmp = libsym; tmp != NULL; ) {
1051 /* Look whether we already have a symbol for
1052 * this address. If not, add this one. If
1053 * yes, look if we should pick the new symbol
1054 * name. */
1055
1056 struct unique_symbol key = { tmp->enter_addr, NULL };
1057 struct unique_symbol *unique
1058 = lsearch(&key, symbols, &num_symbols,
1059 sizeof *symbols, &unique_symbol_cmp);
1060
1061 if (unique->libsym == NULL) {
1062 unique->libsym = tmp;
1063 unique->addr = tmp->enter_addr;
1064 tmp = tmp->next;
Petr Machata97d13662013-11-05 01:46:30 +01001065 unique->libsym->next = NULL;
Petr Machataa186b0e2013-10-15 23:32:18 +02001066 } else {
1067 if (strlen(tmp->name)
1068 < strlen(unique->libsym->name)) {
1069 library_symbol_set_name
1070 (unique->libsym, tmp->name, 1);
1071 tmp->name = NULL;
1072 }
1073 struct library_symbol *next = tmp->next;
1074 library_symbol_destroy(tmp);
1075 free(tmp);
1076 tmp = next;
1077 }
Petr Machata157cc4d2012-04-04 19:00:34 +02001078 }
Petr Machatada3edbf2012-04-04 02:20:21 +02001079 }
Petr Machata157cc4d2012-04-04 19:00:34 +02001080
Petr Machata10bd7a92012-09-28 00:06:56 +02001081 /* Now we do the union of this set of unique symbols with
1082 * what's already in the library. */
Petr Machata157cc4d2012-04-04 19:00:34 +02001083 for (i = 0; i < num_symbols; ++i) {
Petr Machata10bd7a92012-09-28 00:06:56 +02001084 struct library_symbol *this_sym = symbols[i].libsym;
1085 assert(this_sym != NULL);
1086 struct library_symbol *other
1087 = library_each_symbol(lib, NULL, symbol_with_address,
1088 &this_sym->enter_addr);
1089 if (other != NULL) {
1090 library_symbol_destroy(this_sym);
1091 free(this_sym);
1092 symbols[i].libsym = NULL;
1093 }
Petr Machata157cc4d2012-04-04 19:00:34 +02001094 }
1095
Petr Machata10bd7a92012-09-28 00:06:56 +02001096 for (i = 0; i < num_symbols; ++i)
1097 if (symbols[i].libsym != NULL)
1098 library_add_symbol(lib, symbols[i].libsym);
Petr Machata157cc4d2012-04-04 19:00:34 +02001099
Petr Machata10bd7a92012-09-28 00:06:56 +02001100 free(symbols);
Petr Machatada3edbf2012-04-04 02:20:21 +02001101 return 0;
1102}
1103
1104static int
Petr Machata929bd572012-12-17 03:20:34 +01001105populate_symtab(struct process *proc, const char *filename,
Petr Machata013ef5b2012-09-28 00:00:55 +02001106 struct ltelf *lte, struct library *lib,
1107 int symtabs, int exports)
Petr Machatada3edbf2012-04-04 02:20:21 +02001108{
Petr Machata013ef5b2012-09-28 00:00:55 +02001109 int status;
1110 if (symtabs && lte->symtab != NULL && lte->strtab != NULL
1111 && (status = populate_this_symtab(proc, filename, lte, lib,
1112 lte->symtab, lte->strtab,
1113 lte->symtab_count, NULL)) < 0)
1114 return status;
1115
1116 /* Check whether we want to trace symbols implemented by this
1117 * library (-l). */
1118 struct library_exported_name **names = NULL;
1119 if (exports) {
1120 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
1121 names = &lib->exported_names;
1122 }
1123
1124 return populate_this_symtab(proc, filename, lte, lib,
1125 lte->dynsym, lte->dynstr,
1126 lte->dynsym_count, names);
Petr Machatada3edbf2012-04-04 02:20:21 +02001127}
1128
Petr Machatada69ed02012-10-18 17:27:48 +02001129static int
Petr Machata929bd572012-12-17 03:20:34 +01001130read_module(struct library *lib, struct process *proc,
Petr Machatada69ed02012-10-18 17:27:48 +02001131 const char *filename, GElf_Addr bias, int main)
Petr Machatae84fa002012-02-07 13:43:03 +01001132{
Petr Machata673ff512013-10-25 23:45:39 +02001133 struct ltelf lte;
1134 if (ltelf_init(&lte, filename) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +02001135 return -1;
Petr Machatada69ed02012-10-18 17:27:48 +02001136
Petr Machata1c790252012-10-30 23:29:27 +01001137 /* XXX When we abstract ABI into a module, this should instead
1138 * become something like
1139 *
1140 * proc->abi = arch_get_abi(lte.ehdr);
1141 *
Petr Machata673ff512013-10-25 23:45:39 +02001142 * The code in ltelf_init needs to be replaced by this logic.
1143 * Be warned that libltrace.c calls ltelf_init as well to
Petr Machata1c790252012-10-30 23:29:27 +01001144 * determine whether ABI is supported. This is to get
1145 * reasonable error messages when trying to run 64-bit binary
1146 * with 32-bit ltrace. It is desirable to preserve this. */
1147 proc->e_machine = lte.ehdr.e_machine;
1148 proc->e_class = lte.ehdr.e_ident[EI_CLASS];
1149 get_arch_dep(proc);
1150
Petr Machatada69ed02012-10-18 17:27:48 +02001151 /* Find out the base address. For PIE main binaries we look
1152 * into auxv, otherwise we scan phdrs. */
1153 if (main && lte.ehdr.e_type == ET_DYN) {
1154 arch_addr_t entry;
1155 if (process_get_entry(proc, &entry, NULL) < 0) {
1156 fprintf(stderr, "Couldn't find entry of PIE %s\n",
1157 filename);
Petr Machata673ff512013-10-25 23:45:39 +02001158 fail:
1159 ltelf_destroy(&lte);
Petr Machatada69ed02012-10-18 17:27:48 +02001160 return -1;
1161 }
Petr Machatab024ca32012-10-18 19:57:49 +02001162 /* XXX The double cast should be removed when
1163 * arch_addr_t becomes integral type. */
1164 lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
1165 lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
Petr Machatada69ed02012-10-18 17:27:48 +02001166
1167 } else {
1168 GElf_Phdr phdr;
1169 size_t i;
1170 for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
1171 if (phdr.p_type == PT_LOAD) {
1172 lte.base_addr = phdr.p_vaddr + bias;
1173 break;
1174 }
1175 }
1176
1177 lte.bias = bias;
1178 lte.entry_addr = lte.ehdr.e_entry + lte.bias;
1179
1180 if (lte.base_addr == 0) {
1181 fprintf(stderr,
1182 "Couldn't determine base address of %s\n",
1183 filename);
Petr Machata673ff512013-10-25 23:45:39 +02001184 goto fail;
Petr Machatada69ed02012-10-18 17:27:48 +02001185 }
1186 }
1187
Petr Machata673ff512013-10-25 23:45:39 +02001188 if (ltelf_read_elf(&lte, filename) < 0)
1189 goto fail;
Petr Machatada69ed02012-10-18 17:27:48 +02001190
Petr Machatae0615ab2012-04-17 05:17:48 +02001191 if (arch_elf_init(&lte, lib) < 0) {
1192 fprintf(stderr, "Backend initialization failed.\n");
Petr Machata673ff512013-10-25 23:45:39 +02001193 goto fail;
Petr Machatae0615ab2012-04-17 05:17:48 +02001194 }
1195
Petr Machatafc6ff182012-04-04 13:11:50 +02001196 if (lib == NULL)
1197 goto fail;
1198
1199 /* Note that we set soname and pathname as soon as they are
1200 * allocated, so in case of further errors, this get released
Petr Machataf0d248a2013-01-09 17:40:18 +01001201 * when LIB is released, which should happen in the caller
1202 * when we return error. */
Petr Machatafc6ff182012-04-04 13:11:50 +02001203
1204 if (lib->pathname == NULL) {
1205 char *pathname = strdup(filename);
1206 if (pathname == NULL)
1207 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +02001208 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -08001209 }
1210
Petr Machata0b55b582012-04-02 00:38:46 +02001211 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +02001212 char *soname = strdup(lte.soname);
1213 if (soname == NULL)
1214 goto fail;
1215 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +02001216 } else {
Petr Machataaafb00b2013-01-08 18:12:07 +01001217 const char *soname = rindex(lib->pathname, '/');
1218 if (soname != NULL)
1219 soname += 1;
1220 else
Petr Machatafc6ff182012-04-04 13:11:50 +02001221 soname = lib->pathname;
1222 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +02001223 }
Joe Damatofa2aefc2010-10-30 19:56:50 -07001224
Petr Machataea8eb9a2012-04-17 01:32:07 +02001225 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +02001226 * arch_addr_t becomes integral type. */
1227 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +02001228 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +01001229 goto fail;
1230
Petr Machataea8eb9a2012-04-17 01:32:07 +02001231 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +02001232 * arch_addr_t becomes integral type. */
1233 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +01001234 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +02001235 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +02001236 * arch_addr_t becomes integral type. */
1237 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +01001238
Petr Machata013ef5b2012-09-28 00:00:55 +02001239 /* There are two reasons that we need to inspect symbol tables
1240 * or populate PLT entries. Either the user requested
1241 * corresponding tracing features (respectively -x and -e), or
1242 * they requested tracing exported symbols (-l).
1243 *
1244 * In the latter case we need to keep even those PLT slots
1245 * that are not requested by -e (but we keep them latent). We
1246 * also need to inspect .dynsym to find what exports this
1247 * library provide, to turn on existing latent PLT
1248 * entries. */
1249
1250 int plts = filter_matches_library(options.plt_filter, lib);
1251 if ((plts || options.export_filter != NULL)
Petr Machata97d13662013-11-05 01:46:30 +01001252 && populate_plt(proc, filename, &lte, lib) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +02001253 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -08001254
Petr Machata013ef5b2012-09-28 00:00:55 +02001255 int exports = filter_matches_library(options.export_filter, lib);
1256 int symtabs = filter_matches_library(options.static_filter, lib);
1257 if ((symtabs || exports)
1258 && populate_symtab(proc, filename, &lte, lib,
1259 symtabs, exports) < 0)
Petr Machatada3edbf2012-04-04 02:20:21 +02001260 goto fail;
1261
Petr Machata673ff512013-10-25 23:45:39 +02001262 arch_elf_destroy(&lte);
1263 ltelf_destroy(&lte);
1264 return 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +01001265}
Petr Machatae84fa002012-02-07 13:43:03 +01001266
Petr Machatada69ed02012-10-18 17:27:48 +02001267int
Petr Machata929bd572012-12-17 03:20:34 +01001268ltelf_read_library(struct library *lib, struct process *proc,
Petr Machatada69ed02012-10-18 17:27:48 +02001269 const char *filename, GElf_Addr bias)
1270{
1271 return read_module(lib, proc, filename, bias, 0);
1272}
1273
1274
Petr Machata2b46cfc2012-02-18 11:17:29 +01001275struct library *
Petr Machata929bd572012-12-17 03:20:34 +01001276ltelf_read_main_binary(struct process *proc, const char *path)
Petr Machata2b46cfc2012-02-18 11:17:29 +01001277{
Petr Machatab5f80ac2012-04-04 01:46:18 +02001278 struct library *lib = malloc(sizeof(*lib));
Petr Machata72871662013-10-11 14:29:09 +02001279 if (lib == NULL || library_init(lib, LT_LIBTYPE_MAIN) < 0) {
1280 free(lib);
Petr Machatab5f80ac2012-04-04 01:46:18 +02001281 return NULL;
Petr Machata72871662013-10-11 14:29:09 +02001282 }
Petr Machatafc6ff182012-04-04 13:11:50 +02001283 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +02001284
Petr Machatafc6ff182012-04-04 13:11:50 +02001285 /* There is a race between running the process and reading its
1286 * binary for internal consumption. So open the binary from
1287 * the /proc filesystem. XXX Note that there is similar race
1288 * for libraries, but there we don't have a nice answer like
1289 * that. Presumably we could read the DSOs from the process
1290 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +01001291 char *fname = pid2name(proc->pid);
Petr Machataf0d248a2013-01-09 17:40:18 +01001292 if (fname == NULL
1293 || read_module(lib, proc, fname, 0, 1) < 0) {
Petr Machatab5f80ac2012-04-04 01:46:18 +02001294 library_destroy(lib);
1295 free(lib);
Petr Machataf0d248a2013-01-09 17:40:18 +01001296 lib = NULL;
Petr Machata0b55b582012-04-02 00:38:46 +02001297 }
Petr Machatab5f80ac2012-04-04 01:46:18 +02001298
Petr Machataf0d248a2013-01-09 17:40:18 +01001299 free(fname);
Petr Machata2b46cfc2012-02-18 11:17:29 +01001300 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +02001301}