blob: 76ba3aec26c98205f4e0ca96e3551a74041e3b92 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2006,2010,2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch, CodeSourcery
5 * Copyright (C) 2010 Joe Damato
6 * Copyright (C) 1997,1998,2001,2004,2007,2008,2009 Juan Cespedes
7 * Copyright (C) 2006 Olaf Hering, SUSE Linux GmbH
8 * Copyright (C) 2006 Eric Vaitl, Cisco Systems, Inc.
9 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
10 * Copyright (C) 2006 Ian Wienand
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 */
27
Joe Damatof0bd98b2010-11-08 15:47:42 -080028#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020029
Petr Machata157cc4d2012-04-04 19:00:34 +020030#include <assert.h>
Andrey Zonov9d878c92012-08-05 00:19:51 +040031#ifdef __linux__
Juan Cespedesd914a202004-11-10 00:15:33 +010032#include <endian.h>
Andrey Zonov9d878c92012-08-05 00:19:51 +040033#endif
Juan Cespedes96935a91997-08-09 23:45:39 +020034#include <errno.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020035#include <fcntl.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010036#include <gelf.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -080037#include <inttypes.h>
Petr Machata157cc4d2012-04-04 19:00:34 +020038#include <search.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010039#include <stdint.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020040#include <stdio.h>
Juan Cespedesd914a202004-11-10 00:15:33 +010041#include <stdlib.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020042#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010043#include <unistd.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020044
Petr Machata64262602012-01-07 03:41:36 +010045#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020046#include "filter.h"
47#include "library.h"
48#include "ltrace-elf.h"
49#include "proc.h"
50#include "debug.h"
51#include "options.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080052
Paul Gilliambe320772006-04-24 22:06:23 +020053#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010054extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020055#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010056
Petr Machatae67635d2012-03-21 03:37:39 +010057#ifndef ARCH_HAVE_LTELF_DATA
58int
Petr Machatae0615ab2012-04-17 05:17:48 +020059arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010060{
61 return 0;
62}
Petr Machatac67a6e62012-03-28 02:39:49 +020063
64void
65arch_elf_destroy(struct ltelf *lte)
66{
67}
Petr Machatae67635d2012-03-21 03:37:39 +010068#endif
69
Petr Machatae6523e62012-03-24 04:54:06 +010070int
71default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020072 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010073 struct library_symbol **ret)
74{
75 char *name = strdup(a_name);
76 if (name == NULL) {
77 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)
86 goto fail;
87
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 Machatae6523e62012-03-24 04:54:06 +010098 *ret = libsym;
99 return 0;
100}
101
102#ifndef ARCH_HAVE_ADD_PLT_ENTRY
103enum plt_status
104arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +0200105 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +0100106 struct library_symbol **ret)
107{
108 return plt_default;
109}
110#endif
111
Petr Machatae67635d2012-03-21 03:37:39 +0100112Elf_Data *
113elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200114{
115 Elf_Data *data = elf_getdata(scn, NULL);
116 if (data == NULL || elf_getdata(scn, data) != NULL
117 || data->d_off || data->d_size != shdr->sh_size)
118 return NULL;
119 return data;
120}
121
Petr Machatae67635d2012-03-21 03:37:39 +0100122static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100123elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
124 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
125 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200126{
127 int i;
128 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
129 Elf_Scn *scn;
130 GElf_Shdr shdr;
131
132 scn = elf_getscn(lte->elf, i);
133 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
134 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100135 return -1;
136 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100137 if (predicate(scn, &shdr, data)) {
138 *tgt_sec = scn;
139 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200140 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100141 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200142 }
Petr Machatae67635d2012-03-21 03:37:39 +0100143 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100144
145}
146
147static int
148inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
149{
150 GElf_Addr addr = *(GElf_Addr *)data;
151 return addr >= shdr->sh_addr
152 && addr < shdr->sh_addr + shdr->sh_size;
153}
154
155int
156elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
157 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
158{
159 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
160 &inside_p, &addr);
161}
162
163static int
164type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
165{
166 GElf_Word type = *(GElf_Word *)data;
167 return shdr->sh_type == type;
168}
169
170int
171elf_get_section_type(struct ltelf *lte, GElf_Word type,
172 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
173{
174 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
175 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100176}
177
Petr Machata5b3e26a2012-04-30 20:53:22 +0200178struct section_named_data {
179 struct ltelf *lte;
180 const char *name;
181};
182
183static int
184name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
185{
186 struct section_named_data *data = d;
187 const char *name = elf_strptr(data->lte->elf,
188 data->lte->ehdr.e_shstrndx,
189 shdr->sh_name);
190 return strcmp(name, data->name) == 0;
191}
192
193int
194elf_get_section_named(struct ltelf *lte, const char *name,
195 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
196{
197 struct section_named_data data = {
198 .lte = lte,
199 .name = name,
200 };
201 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
202 &name_p, &data);
203}
204
Petr Machatae67635d2012-03-21 03:37:39 +0100205static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200206need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100207{
208 assert(data != NULL);
209 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200210 debug(1, "Not enough data to read %"PRId64"-byte value"
211 " at offset %"PRId64".", size, offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100212 return -1;
213 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200214 return 0;
215}
216
Petr Machatae67635d2012-03-21 03:37:39 +0100217#define DEF_READER(NAME, SIZE) \
218 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200219 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100220 { \
221 if (!need_data(data, offset, SIZE / 8) < 0) \
222 return -1; \
223 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200224 if (data->d_buf == NULL) /* NODATA section */ { \
225 *retp = 0; \
226 return 0; \
227 } \
228 \
Petr Machatae67635d2012-03-21 03:37:39 +0100229 union { \
230 uint##SIZE##_t dst; \
231 char buf[0]; \
232 } u; \
233 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
234 *retp = u.dst; \
235 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200236 }
237
Petr Machatae67635d2012-03-21 03:37:39 +0100238DEF_READER(elf_read_u16, 16)
239DEF_READER(elf_read_u32, 32)
240DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200241
Petr Machatae67635d2012-03-21 03:37:39 +0100242#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200243
Petr Machata1974dbc2011-08-19 18:58:01 +0200244int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200245open_elf(struct ltelf *lte, const char *filename)
246{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247 lte->fd = open(filename, O_RDONLY);
248 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200249 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200250
Petr Machata02bd9ec2011-09-21 17:38:59 +0200251 elf_version(EV_CURRENT);
252
Juan Cespedesd914a202004-11-10 00:15:33 +0100253#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200255#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100256 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200257#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200258
Petr Machatacc0e1e42012-04-25 13:42:07 +0200259 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
260 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
261 exit(EXIT_FAILURE);
262 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200263
Petr Machatacc0e1e42012-04-25 13:42:07 +0200264 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
265 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
266 filename, elf_errmsg(-1));
267 exit(EXIT_FAILURE);
268 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200269
Petr Machatacc0e1e42012-04-25 13:42:07 +0200270 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
271 fprintf(stderr, "\"%s\" is neither an ELF executable"
272 " nor a shared library\n", filename);
273 exit(EXIT_FAILURE);
274 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200275
Petr Machataddd96a32012-05-17 23:35:26 +0200276 if (1
277#ifdef LT_ELF_MACHINE
278 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
279 || lte->ehdr.e_machine != LT_ELF_MACHINE)
280#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100281#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
283 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100284#endif
285#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
287 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100288#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200289 ) {
290 fprintf(stderr,
291 "\"%s\" is ELF from incompatible architecture\n",
292 filename);
293 exit(EXIT_FAILURE);
294 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100295
Petr Machata02bd9ec2011-09-21 17:38:59 +0200296 return 0;
297}
298
Petr Machatacc0e1e42012-04-25 13:42:07 +0200299static void
300read_symbol_table(struct ltelf *lte, const char *filename,
301 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
302 Elf_Data **datap, size_t *countp, const char **strsp)
303{
304 *datap = elf_getdata(scn, NULL);
305 *countp = shdr->sh_size / shdr->sh_entsize;
306 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
307 && options.static_filter != NULL) {
308 fprintf(stderr, "Couldn't get data of section"
309 " %s from \"%s\": %s\n",
310 name, filename, elf_errmsg(-1));
311 exit(EXIT_FAILURE);
312 }
313
314 scn = elf_getscn(lte->elf, shdr->sh_link);
315 GElf_Shdr shdr2;
316 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
317 fprintf(stderr, "Couldn't get header of section"
318 " #%d from \"%s\": %s\n",
319 shdr2.sh_link, filename, elf_errmsg(-1));
320 exit(EXIT_FAILURE);
321 }
322
323 Elf_Data *data = elf_getdata(scn, NULL);
324 if (data == NULL || elf_getdata(scn, data) != NULL
325 || shdr2.sh_size != data->d_size || data->d_off) {
326 fprintf(stderr, "Couldn't get data of section"
327 " #%d from \"%s\": %s\n",
328 shdr2.sh_link, filename, elf_errmsg(-1));
329 exit(EXIT_FAILURE);
330 }
331
332 *strsp = data->d_buf;
333}
334
Petr Machatae67635d2012-03-21 03:37:39 +0100335static int
336do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200338 int i;
339 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100340 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200341
342 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
343 debug(1, "Reading ELF from %s...", filename);
344
345 if (open_elf(lte, filename) < 0)
346 return -1;
347
Petr Machatab120fdf2012-03-21 05:05:46 +0100348 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100349 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100350 GElf_Phdr phdr;
351 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
352 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200353 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100354 break;
355 }
356 }
357 }
358
Petr Machatab120fdf2012-03-21 05:05:46 +0100359 if (lte->base_addr == 0) {
360 fprintf(stderr, "Couldn't determine base address of %s\n",
361 filename);
362 return -1;
363 }
364
365 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100366 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100367
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
369 Elf_Scn *scn;
370 GElf_Shdr shdr;
371 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100372
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100373 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200374 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
375 fprintf(stderr, "Couldn't get section #%d from"
376 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
377 exit(EXIT_FAILURE);
378 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100379
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100380 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200381 if (name == NULL) {
382 fprintf(stderr, "Couldn't get name of section #%d from"
383 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
384 exit(EXIT_FAILURE);
385 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100386
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100387 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200388 read_symbol_table(lte, filename,
389 scn, &shdr, name, &lte->symtab,
390 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100391
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100392 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200393 read_symbol_table(lte, filename,
394 scn, &shdr, name, &lte->dynsym,
395 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100396
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100397 } else if (shdr.sh_type == SHT_DYNAMIC) {
398 Elf_Data *data;
399 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100400
Joe Damato87f4f582010-11-08 15:47:36 -0800401 lte->dyn_addr = shdr.sh_addr;
402 lte->dyn_sz = shdr.sh_size;
403
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100404 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200405 if (data == NULL || elf_getdata(scn, data) != NULL) {
406 fprintf(stderr, "Couldn't get .dynamic data"
407 " from \"%s\": %s\n",
408 filename, strerror(errno));
409 exit(EXIT_FAILURE);
410 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100411
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100412 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
413 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100414
Petr Machatacc0e1e42012-04-25 13:42:07 +0200415 if (gelf_getdyn(data, j, &dyn) == NULL) {
416 fprintf(stderr, "Couldn't get .dynamic"
417 " data from \"%s\": %s\n",
418 filename, strerror(errno));
419 exit(EXIT_FAILURE);
420 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100421 if (dyn.d_tag == DT_JMPREL)
422 relplt_addr = dyn.d_un.d_ptr;
423 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100424 lte->relplt_size = dyn.d_un.d_val;
425 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100427 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100428 } else if (shdr.sh_type == SHT_PROGBITS
429 || shdr.sh_type == SHT_NOBITS) {
430 if (strcmp(name, ".plt") == 0) {
431 lte->plt_addr = shdr.sh_addr;
432 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100433 lte->plt_data = elf_loaddata(scn, &shdr);
434 if (lte->plt_data == NULL)
435 fprintf(stderr,
436 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200437 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100438 }
439#ifdef ARCH_SUPPORTS_OPD
440 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200441 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100442 lte->opd_size = shdr.sh_size;
443 lte->opd = elf_rawdata(scn, NULL);
444 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100445#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100446 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100447 }
448
Petr Machatacc0e1e42012-04-25 13:42:07 +0200449 if (lte->dynsym == NULL || lte->dynstr == NULL) {
450 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
451 filename);
452 exit(EXIT_FAILURE);
453 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100454
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100455 if (!relplt_addr || !lte->plt_addr) {
456 debug(1, "%s has no PLT relocations", filename);
457 lte->relplt = NULL;
458 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100459 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200460 debug(1, "%s has unknown PLT size", filename);
461 lte->relplt = NULL;
462 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100463 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200464
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100465 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
466 Elf_Scn *scn;
467 GElf_Shdr shdr;
468
469 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200470 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
471 fprintf(stderr, "Couldn't get section header"
472 " from \"%s\": %s\n",
473 filename, elf_errmsg(-1));
474 exit(EXIT_FAILURE);
475 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100476 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100477 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100478 lte->relplt = elf_getdata(scn, NULL);
479 lte->relplt_count =
480 shdr.sh_size / shdr.sh_entsize;
481 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200482 || elf_getdata(scn, lte->relplt) != NULL) {
483 fprintf(stderr, "Couldn't get .rel*.plt"
484 " data from \"%s\": %s\n",
485 filename, elf_errmsg(-1));
486 exit(EXIT_FAILURE);
487 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100488 break;
489 }
490 }
491
Petr Machatacc0e1e42012-04-25 13:42:07 +0200492 if (i == lte->ehdr.e_shnum) {
493 fprintf(stderr,
494 "Couldn't find .rel*.plt section in \"%s\"\n",
495 filename);
496 exit(EXIT_FAILURE);
497 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100498
499 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
500 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100501
502 if (soname_offset != 0)
503 lte->soname = lte->dynstr + soname_offset;
504
Petr Machata1974dbc2011-08-19 18:58:01 +0200505 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100506}
507
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800509void
Juan Cespedesf1350522008-12-16 18:19:58 +0100510do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200511 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100512 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 elf_end(lte->elf);
514 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200515}
516
Petr Machatab5f80ac2012-04-04 01:46:18 +0200517static int
518populate_plt(struct Process *proc, const char *filename,
519 struct ltelf *lte, struct library *lib)
520{
521 size_t i;
522 for (i = 0; i < lte->relplt_count; ++i) {
523 GElf_Rel rel;
524 GElf_Rela rela;
525 GElf_Sym sym;
526 void *ret;
527
528 if (lte->relplt->d_type == ELF_T_REL) {
529 ret = gelf_getrel(lte->relplt, i, &rel);
530 rela.r_offset = rel.r_offset;
531 rela.r_info = rel.r_info;
532 rela.r_addend = 0;
533 } else {
534 ret = gelf_getrela(lte->relplt, i, &rela);
535 }
536
537 if (ret == NULL
538 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
539 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
Petr Machatacc0e1e42012-04-25 13:42:07 +0200540 &sym) == NULL) {
541 fprintf(stderr,
542 "Couldn't get relocation from \"%s\": %s\n",
543 filename, elf_errmsg(-1));
544 exit(EXIT_FAILURE);
545 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200546
547 char const *name = lte->dynstr + sym.st_name;
548
549 if (!filter_matches_symbol(options.plt_filter, name, lib))
550 continue;
551
Petr Machata218c5ff2012-04-15 04:22:39 +0200552 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200553 switch (arch_elf_add_plt_entry(proc, lte, name,
554 &rela, i, &libsym)) {
555 case plt_default:
556 if (default_elf_add_plt_entry(proc, lte, name,
557 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200558 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200559 case plt_fail:
560 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200561 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200562 case plt_ok:
563 if (libsym != NULL)
564 library_add_symbol(lib, libsym);
565 }
566 }
567 return 0;
568}
569
Petr Machata157cc4d2012-04-04 19:00:34 +0200570/* When -x rules result in request to trace several aliases, we only
571 * want to add such symbol once. The only way that those symbols
572 * differ in is their name, e.g. in glibc you have __GI___libc_free,
573 * __cfree, __free, __libc_free, cfree and free all defined on the
574 * same address. So instead we keep this unique symbol struct for
575 * each address, and replace name in libsym with a shorter variant if
576 * we find it. */
577struct unique_symbol {
Petr Machatabac2da52012-05-29 00:42:59 +0200578 arch_addr_t addr;
Petr Machata157cc4d2012-04-04 19:00:34 +0200579 struct library_symbol *libsym;
580};
581
582static int
583unique_symbol_cmp(const void *key, const void *val)
584{
585 const struct unique_symbol *sym_key = key;
586 const struct unique_symbol *sym_val = val;
587 return sym_key->addr != sym_val->addr;
588}
589
Petr Machatada3edbf2012-04-04 02:20:21 +0200590static int
591populate_this_symtab(struct Process *proc, const char *filename,
592 struct ltelf *lte, struct library *lib,
593 Elf_Data *symtab, const char *strtab, size_t size)
594{
Petr Machata157cc4d2012-04-04 19:00:34 +0200595 /* Using sorted array would be arguably better, but this
596 * should be well enough for the number of symbols that we
597 * typically deal with. */
598 size_t num_symbols = 0;
599 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
600 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200601 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
602 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200603 return -1;
604 }
605
Petr Machata40cc53b2012-04-07 01:25:38 +0200606 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200607 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200608 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
609 Elf_Scn *scn = elf_getscn(lte->elf, i);
610 if (scn == NULL)
611 continue;
612 GElf_Shdr shdr;
613 if (gelf_getshdr(scn, &shdr) == NULL)
614 continue;
615 secflags[i] = shdr.sh_flags;
616 }
617
618 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200619 for (i = 0; i < size; ++i) {
620 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200621 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200622 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200623 fprintf(stderr,
624 "couldn't get symbol #%zd from %s: %s\n",
625 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200626 continue;
627 }
628
Petr Machata4de6b6b2012-04-04 14:06:09 +0200629 /* XXX support IFUNC as well. */
630 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
Petr Machata6c1c0bb2012-05-18 16:49:34 +0200631 || sym.st_value == 0
632 || sym.st_shndx == STN_UNDEF)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200633 continue;
634
Petr Machata2bbeac42012-04-30 20:48:34 +0200635 const char *orig_name = strtab + sym.st_name;
636 const char *version = strchr(orig_name, '@');
637 size_t len = version != NULL ? (assert(version > orig_name),
638 (size_t)(version - orig_name))
639 : strlen(orig_name);
640 char name[len + 1];
641 memcpy(name, orig_name, len);
642 name[len] = 0;
643
Petr Machatada3edbf2012-04-04 02:20:21 +0200644 if (!filter_matches_symbol(options.static_filter, name, lib))
645 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200646
Petr Machatabac2da52012-05-29 00:42:59 +0200647 arch_addr_t addr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +0200648 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatabac2da52012-05-29 00:42:59 +0200649 arch_addr_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200650
651 /* On arches that support OPD, the value of typical
652 * function symbol will be a pointer to .opd, but some
653 * will point directly to .text. We don't want to
654 * translate those. */
655 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
656 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200657 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200658 fprintf(stderr,
659 "couldn't translate address of %s@%s: %s\n",
660 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200661 continue;
662 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200663
Petr Machata3840f682012-04-06 16:05:41 +0200664 char *full_name;
665 if (lib->type != LT_LIBTYPE_MAIN) {
666 full_name = malloc(strlen(name) + 1 + lib_len + 1);
667 if (full_name == NULL)
668 goto fail;
669 sprintf(full_name, "%s@%s", name, lib->soname);
670 } else {
671 full_name = strdup(name);
672 if (full_name == NULL)
673 goto fail;
674 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200675
Petr Machata157cc4d2012-04-04 19:00:34 +0200676 /* Look whether we already have a symbol for this
677 * address. If not, add this one. */
678 struct unique_symbol key = { naddr, NULL };
679 struct unique_symbol *unique
680 = lsearch(&key, symbols, &num_symbols,
681 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200682
Petr Machata157cc4d2012-04-04 19:00:34 +0200683 if (unique->libsym == NULL) {
684 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200685 if (libsym == NULL
686 || library_symbol_init(libsym, naddr, full_name,
687 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200688 --num_symbols;
689 goto fail;
690 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200691 unique->libsym = libsym;
692 unique->addr = naddr;
693
694 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
695 library_symbol_set_name(unique->libsym, full_name, 1);
696
697 } else {
698 free(full_name);
699 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200700 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200701
702 for (i = 0; i < num_symbols; ++i) {
703 assert(symbols[i].libsym != NULL);
704 library_add_symbol(lib, symbols[i].libsym);
705 }
706
707 free(symbols);
708
Petr Machatada3edbf2012-04-04 02:20:21 +0200709 return 0;
710}
711
712static int
713populate_symtab(struct Process *proc, const char *filename,
714 struct ltelf *lte, struct library *lib)
715{
716 if (lte->symtab != NULL && lte->strtab != NULL)
717 return populate_this_symtab(proc, filename, lte, lib,
718 lte->symtab, lte->strtab,
719 lte->symtab_count);
720 else
721 return populate_this_symtab(proc, filename, lte, lib,
722 lte->dynsym, lte->dynstr,
723 lte->dynsym_count);
724}
725
Petr Machatab5f80ac2012-04-04 01:46:18 +0200726int
727ltelf_read_library(struct library *lib, struct Process *proc,
728 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100729{
Petr Machata29add4f2012-02-18 16:38:05 +0100730 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100731 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200732 return -1;
Petr Machatae0615ab2012-04-17 05:17:48 +0200733 if (arch_elf_init(&lte, lib) < 0) {
734 fprintf(stderr, "Backend initialization failed.\n");
735 return -1;
736 }
737
Petr Machatae67635d2012-03-21 03:37:39 +0100738 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800739
Petr Machatab5f80ac2012-04-04 01:46:18 +0200740 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200741 if (lib == NULL)
742 goto fail;
743
744 /* Note that we set soname and pathname as soon as they are
745 * allocated, so in case of further errors, this get released
746 * when LIB is release, which should happen in the caller when
747 * we return error. */
748
749 if (lib->pathname == NULL) {
750 char *pathname = strdup(filename);
751 if (pathname == NULL)
752 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200753 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800754 }
755
Petr Machata0b55b582012-04-02 00:38:46 +0200756 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200757 char *soname = strdup(lte.soname);
758 if (soname == NULL)
759 goto fail;
760 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200761 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200762 const char *soname = rindex(lib->pathname, '/') + 1;
763 if (soname == NULL)
764 soname = lib->pathname;
765 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200766 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700767
Petr Machataea8eb9a2012-04-17 01:32:07 +0200768 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200769 * arch_addr_t becomes integral type. */
770 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200771 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100772 goto fail;
773
Petr Machataea8eb9a2012-04-17 01:32:07 +0200774 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200775 * arch_addr_t becomes integral type. */
776 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100777 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200778 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200779 * arch_addr_t becomes integral type. */
780 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100781
Petr Machatab5f80ac2012-04-04 01:46:18 +0200782 if (filter_matches_library(options.plt_filter, lib)
783 && populate_plt(proc, filename, &lte, lib) < 0)
784 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800785
Petr Machatada3edbf2012-04-04 02:20:21 +0200786 if (filter_matches_library(options.static_filter, lib)
787 && populate_symtab(proc, filename, &lte, lib) < 0)
788 goto fail;
789
Petr Machata2b46cfc2012-02-18 11:17:29 +0100790done:
791 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200792 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200793
794fail:
795 status = -1;
796 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100797}
Petr Machatae84fa002012-02-07 13:43:03 +0100798
Petr Machata2b46cfc2012-02-18 11:17:29 +0100799struct library *
800ltelf_read_main_binary(struct Process *proc, const char *path)
801{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200802 struct library *lib = malloc(sizeof(*lib));
803 if (lib == NULL)
804 return NULL;
805 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200806 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200807
Petr Machatafc6ff182012-04-04 13:11:50 +0200808 /* There is a race between running the process and reading its
809 * binary for internal consumption. So open the binary from
810 * the /proc filesystem. XXX Note that there is similar race
811 * for libraries, but there we don't have a nice answer like
812 * that. Presumably we could read the DSOs from the process
813 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100814 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200815 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
816 library_destroy(lib);
817 free(lib);
818 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200819 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200820
Petr Machata2b46cfc2012-02-18 11:17:29 +0100821 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200822}