blob: 9b6cf6b287e853cbe05c8cf4f11d634b1f7adc57 [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
89 * target_address_t becomes integral type. */
90 target_address_t taddr = (target_address_t)
91 (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
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
277 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100278#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100279 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
280 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100281#endif
282#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100283 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
284 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100285#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200286 ) {
287 fprintf(stderr,
288 "\"%s\" is ELF from incompatible architecture\n",
289 filename);
290 exit(EXIT_FAILURE);
291 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100292
Petr Machata02bd9ec2011-09-21 17:38:59 +0200293 return 0;
294}
295
Petr Machatacc0e1e42012-04-25 13:42:07 +0200296static void
297read_symbol_table(struct ltelf *lte, const char *filename,
298 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
299 Elf_Data **datap, size_t *countp, const char **strsp)
300{
301 *datap = elf_getdata(scn, NULL);
302 *countp = shdr->sh_size / shdr->sh_entsize;
303 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
304 && options.static_filter != NULL) {
305 fprintf(stderr, "Couldn't get data of section"
306 " %s from \"%s\": %s\n",
307 name, filename, elf_errmsg(-1));
308 exit(EXIT_FAILURE);
309 }
310
311 scn = elf_getscn(lte->elf, shdr->sh_link);
312 GElf_Shdr shdr2;
313 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
314 fprintf(stderr, "Couldn't get header of section"
315 " #%d from \"%s\": %s\n",
316 shdr2.sh_link, filename, elf_errmsg(-1));
317 exit(EXIT_FAILURE);
318 }
319
320 Elf_Data *data = elf_getdata(scn, NULL);
321 if (data == NULL || elf_getdata(scn, data) != NULL
322 || shdr2.sh_size != data->d_size || data->d_off) {
323 fprintf(stderr, "Couldn't get data of section"
324 " #%d from \"%s\": %s\n",
325 shdr2.sh_link, filename, elf_errmsg(-1));
326 exit(EXIT_FAILURE);
327 }
328
329 *strsp = data->d_buf;
330}
331
Petr Machatae67635d2012-03-21 03:37:39 +0100332static int
333do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200335 int i;
336 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200338
339 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
340 debug(1, "Reading ELF from %s...", filename);
341
342 if (open_elf(lte, filename) < 0)
343 return -1;
344
Petr Machatab120fdf2012-03-21 05:05:46 +0100345 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100346 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100347 GElf_Phdr phdr;
348 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
349 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200350 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100351 break;
352 }
353 }
354 }
355
Petr Machatab120fdf2012-03-21 05:05:46 +0100356 if (lte->base_addr == 0) {
357 fprintf(stderr, "Couldn't determine base address of %s\n",
358 filename);
359 return -1;
360 }
361
362 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100363 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100364
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100365 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
366 Elf_Scn *scn;
367 GElf_Shdr shdr;
368 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100369
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200371 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
372 fprintf(stderr, "Couldn't get section #%d from"
373 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
374 exit(EXIT_FAILURE);
375 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100376
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100377 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200378 if (name == NULL) {
379 fprintf(stderr, "Couldn't get name of section #%d from"
380 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
381 exit(EXIT_FAILURE);
382 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100383
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200385 read_symbol_table(lte, filename,
386 scn, &shdr, name, &lte->symtab,
387 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100388
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100389 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200390 read_symbol_table(lte, filename,
391 scn, &shdr, name, &lte->dynsym,
392 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100393
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100394 } else if (shdr.sh_type == SHT_DYNAMIC) {
395 Elf_Data *data;
396 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100397
Joe Damato87f4f582010-11-08 15:47:36 -0800398 lte->dyn_addr = shdr.sh_addr;
399 lte->dyn_sz = shdr.sh_size;
400
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200402 if (data == NULL || elf_getdata(scn, data) != NULL) {
403 fprintf(stderr, "Couldn't get .dynamic data"
404 " from \"%s\": %s\n",
405 filename, strerror(errno));
406 exit(EXIT_FAILURE);
407 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100408
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100409 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
410 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100411
Petr Machatacc0e1e42012-04-25 13:42:07 +0200412 if (gelf_getdyn(data, j, &dyn) == NULL) {
413 fprintf(stderr, "Couldn't get .dynamic"
414 " data from \"%s\": %s\n",
415 filename, strerror(errno));
416 exit(EXIT_FAILURE);
417 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100418 if (dyn.d_tag == DT_JMPREL)
419 relplt_addr = dyn.d_un.d_ptr;
420 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100421 lte->relplt_size = dyn.d_un.d_val;
422 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100423 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100424 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100425 } else if (shdr.sh_type == SHT_PROGBITS
426 || shdr.sh_type == SHT_NOBITS) {
427 if (strcmp(name, ".plt") == 0) {
428 lte->plt_addr = shdr.sh_addr;
429 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100430 lte->plt_data = elf_loaddata(scn, &shdr);
431 if (lte->plt_data == NULL)
432 fprintf(stderr,
433 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200434 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100435 }
436#ifdef ARCH_SUPPORTS_OPD
437 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200438 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 lte->opd_size = shdr.sh_size;
440 lte->opd = elf_rawdata(scn, NULL);
441 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100442#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100444 }
445
Petr Machatacc0e1e42012-04-25 13:42:07 +0200446 if (lte->dynsym == NULL || lte->dynstr == NULL) {
447 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
448 filename);
449 exit(EXIT_FAILURE);
450 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100451
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100452 if (!relplt_addr || !lte->plt_addr) {
453 debug(1, "%s has no PLT relocations", filename);
454 lte->relplt = NULL;
455 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100456 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200457 debug(1, "%s has unknown PLT size", filename);
458 lte->relplt = NULL;
459 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100460 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200461
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100462 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
463 Elf_Scn *scn;
464 GElf_Shdr shdr;
465
466 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200467 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
468 fprintf(stderr, "Couldn't get section header"
469 " from \"%s\": %s\n",
470 filename, elf_errmsg(-1));
471 exit(EXIT_FAILURE);
472 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100473 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100474 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100475 lte->relplt = elf_getdata(scn, NULL);
476 lte->relplt_count =
477 shdr.sh_size / shdr.sh_entsize;
478 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200479 || elf_getdata(scn, lte->relplt) != NULL) {
480 fprintf(stderr, "Couldn't get .rel*.plt"
481 " data from \"%s\": %s\n",
482 filename, elf_errmsg(-1));
483 exit(EXIT_FAILURE);
484 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100485 break;
486 }
487 }
488
Petr Machatacc0e1e42012-04-25 13:42:07 +0200489 if (i == lte->ehdr.e_shnum) {
490 fprintf(stderr,
491 "Couldn't find .rel*.plt section in \"%s\"\n",
492 filename);
493 exit(EXIT_FAILURE);
494 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100495
496 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
497 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100498
499 if (soname_offset != 0)
500 lte->soname = lte->dynstr + soname_offset;
501
Petr Machata1974dbc2011-08-19 18:58:01 +0200502 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100503}
504
Petr Machata2b46cfc2012-02-18 11:17:29 +0100505/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800506void
Juan Cespedesf1350522008-12-16 18:19:58 +0100507do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200508 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100509 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 elf_end(lte->elf);
511 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200512}
513
Petr Machatab5f80ac2012-04-04 01:46:18 +0200514static int
515populate_plt(struct Process *proc, const char *filename,
516 struct ltelf *lte, struct library *lib)
517{
518 size_t i;
519 for (i = 0; i < lte->relplt_count; ++i) {
520 GElf_Rel rel;
521 GElf_Rela rela;
522 GElf_Sym sym;
523 void *ret;
524
525 if (lte->relplt->d_type == ELF_T_REL) {
526 ret = gelf_getrel(lte->relplt, i, &rel);
527 rela.r_offset = rel.r_offset;
528 rela.r_info = rel.r_info;
529 rela.r_addend = 0;
530 } else {
531 ret = gelf_getrela(lte->relplt, i, &rela);
532 }
533
534 if (ret == NULL
535 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
536 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
Petr Machatacc0e1e42012-04-25 13:42:07 +0200537 &sym) == NULL) {
538 fprintf(stderr,
539 "Couldn't get relocation from \"%s\": %s\n",
540 filename, elf_errmsg(-1));
541 exit(EXIT_FAILURE);
542 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200543
544 char const *name = lte->dynstr + sym.st_name;
545
546 if (!filter_matches_symbol(options.plt_filter, name, lib))
547 continue;
548
Petr Machata218c5ff2012-04-15 04:22:39 +0200549 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200550 switch (arch_elf_add_plt_entry(proc, lte, name,
551 &rela, i, &libsym)) {
552 case plt_default:
553 if (default_elf_add_plt_entry(proc, lte, name,
554 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200555 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200556 case plt_fail:
557 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200558 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200559 case plt_ok:
560 if (libsym != NULL)
561 library_add_symbol(lib, libsym);
562 }
563 }
564 return 0;
565}
566
Petr Machata157cc4d2012-04-04 19:00:34 +0200567/* When -x rules result in request to trace several aliases, we only
568 * want to add such symbol once. The only way that those symbols
569 * differ in is their name, e.g. in glibc you have __GI___libc_free,
570 * __cfree, __free, __libc_free, cfree and free all defined on the
571 * same address. So instead we keep this unique symbol struct for
572 * each address, and replace name in libsym with a shorter variant if
573 * we find it. */
574struct unique_symbol {
575 target_address_t addr;
576 struct library_symbol *libsym;
577};
578
579static int
580unique_symbol_cmp(const void *key, const void *val)
581{
582 const struct unique_symbol *sym_key = key;
583 const struct unique_symbol *sym_val = val;
584 return sym_key->addr != sym_val->addr;
585}
586
Petr Machatada3edbf2012-04-04 02:20:21 +0200587static int
588populate_this_symtab(struct Process *proc, const char *filename,
589 struct ltelf *lte, struct library *lib,
590 Elf_Data *symtab, const char *strtab, size_t size)
591{
Petr Machata157cc4d2012-04-04 19:00:34 +0200592 /* Using sorted array would be arguably better, but this
593 * should be well enough for the number of symbols that we
594 * typically deal with. */
595 size_t num_symbols = 0;
596 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
597 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200598 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
599 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200600 return -1;
601 }
602
Petr Machata40cc53b2012-04-07 01:25:38 +0200603 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200604 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200605 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
606 Elf_Scn *scn = elf_getscn(lte->elf, i);
607 if (scn == NULL)
608 continue;
609 GElf_Shdr shdr;
610 if (gelf_getshdr(scn, &shdr) == NULL)
611 continue;
612 secflags[i] = shdr.sh_flags;
613 }
614
615 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200616 for (i = 0; i < size; ++i) {
617 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200618 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200619 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200620 fprintf(stderr,
621 "couldn't get symbol #%zd from %s: %s\n",
622 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200623 continue;
624 }
625
Petr Machata4de6b6b2012-04-04 14:06:09 +0200626 /* XXX support IFUNC as well. */
627 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
Petr Machata6c1c0bb2012-05-18 16:49:34 +0200628 || sym.st_value == 0
629 || sym.st_shndx == STN_UNDEF)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200630 continue;
631
Petr Machata2bbeac42012-04-30 20:48:34 +0200632 const char *orig_name = strtab + sym.st_name;
633 const char *version = strchr(orig_name, '@');
634 size_t len = version != NULL ? (assert(version > orig_name),
635 (size_t)(version - orig_name))
636 : strlen(orig_name);
637 char name[len + 1];
638 memcpy(name, orig_name, len);
639 name[len] = 0;
640
Petr Machatada3edbf2012-04-04 02:20:21 +0200641 if (!filter_matches_symbol(options.static_filter, name, lib))
642 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200643
Petr Machataea8eb9a2012-04-17 01:32:07 +0200644 target_address_t addr = (target_address_t)
645 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatada3edbf2012-04-04 02:20:21 +0200646 target_address_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200647
648 /* On arches that support OPD, the value of typical
649 * function symbol will be a pointer to .opd, but some
650 * will point directly to .text. We don't want to
651 * translate those. */
652 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
653 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200654 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200655 fprintf(stderr,
656 "couldn't translate address of %s@%s: %s\n",
657 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200658 continue;
659 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200660
Petr Machata3840f682012-04-06 16:05:41 +0200661 char *full_name;
662 if (lib->type != LT_LIBTYPE_MAIN) {
663 full_name = malloc(strlen(name) + 1 + lib_len + 1);
664 if (full_name == NULL)
665 goto fail;
666 sprintf(full_name, "%s@%s", name, lib->soname);
667 } else {
668 full_name = strdup(name);
669 if (full_name == NULL)
670 goto fail;
671 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200672
Petr Machata157cc4d2012-04-04 19:00:34 +0200673 /* Look whether we already have a symbol for this
674 * address. If not, add this one. */
675 struct unique_symbol key = { naddr, NULL };
676 struct unique_symbol *unique
677 = lsearch(&key, symbols, &num_symbols,
678 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200679
Petr Machata157cc4d2012-04-04 19:00:34 +0200680 if (unique->libsym == NULL) {
681 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200682 if (libsym == NULL
683 || library_symbol_init(libsym, naddr, full_name,
684 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200685 --num_symbols;
686 goto fail;
687 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200688 unique->libsym = libsym;
689 unique->addr = naddr;
690
691 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
692 library_symbol_set_name(unique->libsym, full_name, 1);
693
694 } else {
695 free(full_name);
696 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200697 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200698
699 for (i = 0; i < num_symbols; ++i) {
700 assert(symbols[i].libsym != NULL);
701 library_add_symbol(lib, symbols[i].libsym);
702 }
703
704 free(symbols);
705
Petr Machatada3edbf2012-04-04 02:20:21 +0200706 return 0;
707}
708
709static int
710populate_symtab(struct Process *proc, const char *filename,
711 struct ltelf *lte, struct library *lib)
712{
713 if (lte->symtab != NULL && lte->strtab != NULL)
714 return populate_this_symtab(proc, filename, lte, lib,
715 lte->symtab, lte->strtab,
716 lte->symtab_count);
717 else
718 return populate_this_symtab(proc, filename, lte, lib,
719 lte->dynsym, lte->dynstr,
720 lte->dynsym_count);
721}
722
Petr Machatab5f80ac2012-04-04 01:46:18 +0200723int
724ltelf_read_library(struct library *lib, struct Process *proc,
725 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100726{
Petr Machata29add4f2012-02-18 16:38:05 +0100727 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100728 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200729 return -1;
Petr Machatae0615ab2012-04-17 05:17:48 +0200730 if (arch_elf_init(&lte, lib) < 0) {
731 fprintf(stderr, "Backend initialization failed.\n");
732 return -1;
733 }
734
Petr Machatae67635d2012-03-21 03:37:39 +0100735 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800736
Petr Machatab5f80ac2012-04-04 01:46:18 +0200737 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200738 if (lib == NULL)
739 goto fail;
740
741 /* Note that we set soname and pathname as soon as they are
742 * allocated, so in case of further errors, this get released
743 * when LIB is release, which should happen in the caller when
744 * we return error. */
745
746 if (lib->pathname == NULL) {
747 char *pathname = strdup(filename);
748 if (pathname == NULL)
749 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200750 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800751 }
752
Petr Machata0b55b582012-04-02 00:38:46 +0200753 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200754 char *soname = strdup(lte.soname);
755 if (soname == NULL)
756 goto fail;
757 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200758 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200759 const char *soname = rindex(lib->pathname, '/') + 1;
760 if (soname == NULL)
761 soname = lib->pathname;
762 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200763 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700764
Petr Machataea8eb9a2012-04-17 01:32:07 +0200765 /* XXX The double cast should be removed when
766 * target_address_t becomes integral type. */
767 target_address_t entry = (target_address_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200768 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100769 goto fail;
770
Petr Machataea8eb9a2012-04-17 01:32:07 +0200771 /* XXX The double cast should be removed when
772 * target_address_t becomes integral type. */
773 lib->base = (target_address_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100774 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200775 /* XXX The double cast should be removed when
776 * target_address_t becomes integral type. */
777 lib->dyn_addr = (target_address_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100778
Petr Machatab5f80ac2012-04-04 01:46:18 +0200779 if (filter_matches_library(options.plt_filter, lib)
780 && populate_plt(proc, filename, &lte, lib) < 0)
781 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800782
Petr Machatada3edbf2012-04-04 02:20:21 +0200783 if (filter_matches_library(options.static_filter, lib)
784 && populate_symtab(proc, filename, &lte, lib) < 0)
785 goto fail;
786
Petr Machata2b46cfc2012-02-18 11:17:29 +0100787done:
788 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200789 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200790
791fail:
792 status = -1;
793 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100794}
Petr Machatae84fa002012-02-07 13:43:03 +0100795
Petr Machata2b46cfc2012-02-18 11:17:29 +0100796struct library *
797ltelf_read_main_binary(struct Process *proc, const char *path)
798{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200799 struct library *lib = malloc(sizeof(*lib));
800 if (lib == NULL)
801 return NULL;
802 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200803 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200804
Petr Machatafc6ff182012-04-04 13:11:50 +0200805 /* There is a race between running the process and reading its
806 * binary for internal consumption. So open the binary from
807 * the /proc filesystem. XXX Note that there is similar race
808 * for libraries, but there we don't have a nice answer like
809 * that. Presumably we could read the DSOs from the process
810 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100811 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200812 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
813 library_destroy(lib);
814 free(lib);
815 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200816 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200817
Petr Machata2b46cfc2012-02-18 11:17:29 +0100818 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200819}