blob: 7c621856b3f5518afe441b469b20ab47c7545094 [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 Machata366c2f42012-02-09 19:34:36 +010045#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010046#include "library.h"
Petr Machatab5f80ac2012-04-04 01:46:18 +020047#include "filter.h"
Petr Machata64262602012-01-07 03:41:36 +010048#include "backend.h"
Joe Damatof0bd98b2010-11-08 15:47:42 -080049
Paul Gilliambe320772006-04-24 22:06:23 +020050#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020052#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010053
Petr Machatae67635d2012-03-21 03:37:39 +010054#ifndef ARCH_HAVE_LTELF_DATA
55int
Petr Machatae0615ab2012-04-17 05:17:48 +020056arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010057{
58 return 0;
59}
Petr Machatac67a6e62012-03-28 02:39:49 +020060
61void
62arch_elf_destroy(struct ltelf *lte)
63{
64}
Petr Machatae67635d2012-03-21 03:37:39 +010065#endif
66
Petr Machatae6523e62012-03-24 04:54:06 +010067int
68default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020069 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010070 struct library_symbol **ret)
71{
72 char *name = strdup(a_name);
73 if (name == NULL) {
74 fail:
75 free(name);
76 return -1;
77 }
78
Petr Machata1be22912012-03-27 03:11:33 +020079 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010080
81 struct library_symbol *libsym = malloc(sizeof(*libsym));
82 if (libsym == NULL)
83 goto fail;
84
Petr Machataea8eb9a2012-04-17 01:32:07 +020085 /* XXX The double cast should be removed when
86 * target_address_t becomes integral type. */
87 target_address_t taddr = (target_address_t)
88 (uintptr_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010089
Petr Machatae8d90762012-04-15 04:28:31 +020090 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
91 free(libsym);
92 goto fail;
93 }
94
Petr Machatae6523e62012-03-24 04:54:06 +010095 *ret = libsym;
96 return 0;
97}
98
99#ifndef ARCH_HAVE_ADD_PLT_ENTRY
100enum plt_status
101arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +0200102 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +0100103 struct library_symbol **ret)
104{
105 return plt_default;
106}
107#endif
108
Petr Machatae67635d2012-03-21 03:37:39 +0100109Elf_Data *
110elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200111{
112 Elf_Data *data = elf_getdata(scn, NULL);
113 if (data == NULL || elf_getdata(scn, data) != NULL
114 || data->d_off || data->d_size != shdr->sh_size)
115 return NULL;
116 return data;
117}
118
Petr Machatae67635d2012-03-21 03:37:39 +0100119static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100120elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
121 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
122 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200123{
124 int i;
125 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
126 Elf_Scn *scn;
127 GElf_Shdr shdr;
128
129 scn = elf_getscn(lte->elf, i);
130 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
131 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100132 return -1;
133 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100134 if (predicate(scn, &shdr, data)) {
135 *tgt_sec = scn;
136 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200137 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100138 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200139 }
Petr Machatae67635d2012-03-21 03:37:39 +0100140 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100141
142}
143
144static int
145inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
146{
147 GElf_Addr addr = *(GElf_Addr *)data;
148 return addr >= shdr->sh_addr
149 && addr < shdr->sh_addr + shdr->sh_size;
150}
151
152int
153elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
154 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
155{
156 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
157 &inside_p, &addr);
158}
159
160static int
161type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
162{
163 GElf_Word type = *(GElf_Word *)data;
164 return shdr->sh_type == type;
165}
166
167int
168elf_get_section_type(struct ltelf *lte, GElf_Word type,
169 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
170{
171 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
172 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100173}
174
Petr Machata5b3e26a2012-04-30 20:53:22 +0200175struct section_named_data {
176 struct ltelf *lte;
177 const char *name;
178};
179
180static int
181name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
182{
183 struct section_named_data *data = d;
184 const char *name = elf_strptr(data->lte->elf,
185 data->lte->ehdr.e_shstrndx,
186 shdr->sh_name);
187 return strcmp(name, data->name) == 0;
188}
189
190int
191elf_get_section_named(struct ltelf *lte, const char *name,
192 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
193{
194 struct section_named_data data = {
195 .lte = lte,
196 .name = name,
197 };
198 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
199 &name_p, &data);
200}
201
Petr Machatae67635d2012-03-21 03:37:39 +0100202static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200203need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100204{
205 assert(data != NULL);
206 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200207 debug(1, "Not enough data to read %"PRId64"-byte value"
208 " at offset %"PRId64".", size, offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100209 return -1;
210 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200211 return 0;
212}
213
Petr Machatae67635d2012-03-21 03:37:39 +0100214#define DEF_READER(NAME, SIZE) \
215 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200216 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100217 { \
218 if (!need_data(data, offset, SIZE / 8) < 0) \
219 return -1; \
220 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200221 if (data->d_buf == NULL) /* NODATA section */ { \
222 *retp = 0; \
223 return 0; \
224 } \
225 \
Petr Machatae67635d2012-03-21 03:37:39 +0100226 union { \
227 uint##SIZE##_t dst; \
228 char buf[0]; \
229 } u; \
230 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
231 *retp = u.dst; \
232 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200233 }
234
Petr Machatae67635d2012-03-21 03:37:39 +0100235DEF_READER(elf_read_u16, 16)
236DEF_READER(elf_read_u32, 32)
237DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200238
Petr Machatae67635d2012-03-21 03:37:39 +0100239#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200240
Petr Machata1974dbc2011-08-19 18:58:01 +0200241int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200242open_elf(struct ltelf *lte, const char *filename)
243{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100244 lte->fd = open(filename, O_RDONLY);
245 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200246 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200247
Petr Machata02bd9ec2011-09-21 17:38:59 +0200248 elf_version(EV_CURRENT);
249
Juan Cespedesd914a202004-11-10 00:15:33 +0100250#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200252#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100253 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200254#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200255
Petr Machatacc0e1e42012-04-25 13:42:07 +0200256 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
257 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
258 exit(EXIT_FAILURE);
259 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200260
Petr Machatacc0e1e42012-04-25 13:42:07 +0200261 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
262 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
263 filename, elf_errmsg(-1));
264 exit(EXIT_FAILURE);
265 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200266
Petr Machatacc0e1e42012-04-25 13:42:07 +0200267 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
268 fprintf(stderr, "\"%s\" is neither an ELF executable"
269 " nor a shared library\n", filename);
270 exit(EXIT_FAILURE);
271 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200272
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100273 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
274 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +0100275#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
277 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100278#endif
279#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100280 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
281 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100282#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200283 ) {
284 fprintf(stderr,
285 "\"%s\" is ELF from incompatible architecture\n",
286 filename);
287 exit(EXIT_FAILURE);
288 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100289
Petr Machata02bd9ec2011-09-21 17:38:59 +0200290 return 0;
291}
292
Petr Machatacc0e1e42012-04-25 13:42:07 +0200293static void
294read_symbol_table(struct ltelf *lte, const char *filename,
295 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
296 Elf_Data **datap, size_t *countp, const char **strsp)
297{
298 *datap = elf_getdata(scn, NULL);
299 *countp = shdr->sh_size / shdr->sh_entsize;
300 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
301 && options.static_filter != NULL) {
302 fprintf(stderr, "Couldn't get data of section"
303 " %s from \"%s\": %s\n",
304 name, filename, elf_errmsg(-1));
305 exit(EXIT_FAILURE);
306 }
307
308 scn = elf_getscn(lte->elf, shdr->sh_link);
309 GElf_Shdr shdr2;
310 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
311 fprintf(stderr, "Couldn't get header of section"
312 " #%d from \"%s\": %s\n",
313 shdr2.sh_link, filename, elf_errmsg(-1));
314 exit(EXIT_FAILURE);
315 }
316
317 Elf_Data *data = elf_getdata(scn, NULL);
318 if (data == NULL || elf_getdata(scn, data) != NULL
319 || shdr2.sh_size != data->d_size || data->d_off) {
320 fprintf(stderr, "Couldn't get data of section"
321 " #%d from \"%s\": %s\n",
322 shdr2.sh_link, filename, elf_errmsg(-1));
323 exit(EXIT_FAILURE);
324 }
325
326 *strsp = data->d_buf;
327}
328
Petr Machatae67635d2012-03-21 03:37:39 +0100329static int
330do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100331{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200332 int i;
333 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200335
336 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
337 debug(1, "Reading ELF from %s...", filename);
338
339 if (open_elf(lte, filename) < 0)
340 return -1;
341
Petr Machatab120fdf2012-03-21 05:05:46 +0100342 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100343 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344 GElf_Phdr phdr;
345 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
346 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200347 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100348 break;
349 }
350 }
351 }
352
Petr Machatab120fdf2012-03-21 05:05:46 +0100353 if (lte->base_addr == 0) {
354 fprintf(stderr, "Couldn't determine base address of %s\n",
355 filename);
356 return -1;
357 }
358
359 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100360 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100361
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
363 Elf_Scn *scn;
364 GElf_Shdr shdr;
365 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100366
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200368 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
369 fprintf(stderr, "Couldn't get section #%d from"
370 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
371 exit(EXIT_FAILURE);
372 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100373
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100374 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200375 if (name == NULL) {
376 fprintf(stderr, "Couldn't get name of section #%d from"
377 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
378 exit(EXIT_FAILURE);
379 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100380
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100381 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200382 read_symbol_table(lte, filename,
383 scn, &shdr, name, &lte->symtab,
384 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100385
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100386 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200387 read_symbol_table(lte, filename,
388 scn, &shdr, name, &lte->dynsym,
389 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100390
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 } else if (shdr.sh_type == SHT_DYNAMIC) {
392 Elf_Data *data;
393 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100394
Joe Damato87f4f582010-11-08 15:47:36 -0800395 lte->dyn_addr = shdr.sh_addr;
396 lte->dyn_sz = shdr.sh_size;
397
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100398 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200399 if (data == NULL || elf_getdata(scn, data) != NULL) {
400 fprintf(stderr, "Couldn't get .dynamic data"
401 " from \"%s\": %s\n",
402 filename, strerror(errno));
403 exit(EXIT_FAILURE);
404 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100405
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
407 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100408
Petr Machatacc0e1e42012-04-25 13:42:07 +0200409 if (gelf_getdyn(data, j, &dyn) == NULL) {
410 fprintf(stderr, "Couldn't get .dynamic"
411 " data from \"%s\": %s\n",
412 filename, strerror(errno));
413 exit(EXIT_FAILURE);
414 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100415 if (dyn.d_tag == DT_JMPREL)
416 relplt_addr = dyn.d_un.d_ptr;
417 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100418 lte->relplt_size = dyn.d_un.d_val;
419 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100421 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100422 } else if (shdr.sh_type == SHT_PROGBITS
423 || shdr.sh_type == SHT_NOBITS) {
424 if (strcmp(name, ".plt") == 0) {
425 lte->plt_addr = shdr.sh_addr;
426 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100427 lte->plt_data = elf_loaddata(scn, &shdr);
428 if (lte->plt_data == NULL)
429 fprintf(stderr,
430 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200431 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100432 }
433#ifdef ARCH_SUPPORTS_OPD
434 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200435 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436 lte->opd_size = shdr.sh_size;
437 lte->opd = elf_rawdata(scn, NULL);
438 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100439#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100440 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100441 }
442
Petr Machatacc0e1e42012-04-25 13:42:07 +0200443 if (lte->dynsym == NULL || lte->dynstr == NULL) {
444 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
445 filename);
446 exit(EXIT_FAILURE);
447 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100448
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100449 if (!relplt_addr || !lte->plt_addr) {
450 debug(1, "%s has no PLT relocations", filename);
451 lte->relplt = NULL;
452 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100453 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200454 debug(1, "%s has unknown PLT size", filename);
455 lte->relplt = NULL;
456 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100457 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200458
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
460 Elf_Scn *scn;
461 GElf_Shdr shdr;
462
463 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200464 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
465 fprintf(stderr, "Couldn't get section header"
466 " from \"%s\": %s\n",
467 filename, elf_errmsg(-1));
468 exit(EXIT_FAILURE);
469 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100470 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100471 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100472 lte->relplt = elf_getdata(scn, NULL);
473 lte->relplt_count =
474 shdr.sh_size / shdr.sh_entsize;
475 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200476 || elf_getdata(scn, lte->relplt) != NULL) {
477 fprintf(stderr, "Couldn't get .rel*.plt"
478 " data from \"%s\": %s\n",
479 filename, elf_errmsg(-1));
480 exit(EXIT_FAILURE);
481 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100482 break;
483 }
484 }
485
Petr Machatacc0e1e42012-04-25 13:42:07 +0200486 if (i == lte->ehdr.e_shnum) {
487 fprintf(stderr,
488 "Couldn't find .rel*.plt section in \"%s\"\n",
489 filename);
490 exit(EXIT_FAILURE);
491 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100492
493 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
494 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100495
496 if (soname_offset != 0)
497 lte->soname = lte->dynstr + soname_offset;
498
Petr Machata1974dbc2011-08-19 18:58:01 +0200499 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100500}
501
Petr Machata2b46cfc2012-02-18 11:17:29 +0100502/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800503void
Juan Cespedesf1350522008-12-16 18:19:58 +0100504do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200505 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100506 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100507 elf_end(lte->elf);
508 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200509}
510
Petr Machatab5f80ac2012-04-04 01:46:18 +0200511static int
512populate_plt(struct Process *proc, const char *filename,
513 struct ltelf *lte, struct library *lib)
514{
515 size_t i;
516 for (i = 0; i < lte->relplt_count; ++i) {
517 GElf_Rel rel;
518 GElf_Rela rela;
519 GElf_Sym sym;
520 void *ret;
521
522 if (lte->relplt->d_type == ELF_T_REL) {
523 ret = gelf_getrel(lte->relplt, i, &rel);
524 rela.r_offset = rel.r_offset;
525 rela.r_info = rel.r_info;
526 rela.r_addend = 0;
527 } else {
528 ret = gelf_getrela(lte->relplt, i, &rela);
529 }
530
531 if (ret == NULL
532 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
533 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
Petr Machatacc0e1e42012-04-25 13:42:07 +0200534 &sym) == NULL) {
535 fprintf(stderr,
536 "Couldn't get relocation from \"%s\": %s\n",
537 filename, elf_errmsg(-1));
538 exit(EXIT_FAILURE);
539 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200540
541 char const *name = lte->dynstr + sym.st_name;
542
543 if (!filter_matches_symbol(options.plt_filter, name, lib))
544 continue;
545
Petr Machata218c5ff2012-04-15 04:22:39 +0200546 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200547 switch (arch_elf_add_plt_entry(proc, lte, name,
548 &rela, i, &libsym)) {
549 case plt_default:
550 if (default_elf_add_plt_entry(proc, lte, name,
551 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200552 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200553 case plt_fail:
554 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200555 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200556 case plt_ok:
557 if (libsym != NULL)
558 library_add_symbol(lib, libsym);
559 }
560 }
561 return 0;
562}
563
Petr Machata157cc4d2012-04-04 19:00:34 +0200564/* When -x rules result in request to trace several aliases, we only
565 * want to add such symbol once. The only way that those symbols
566 * differ in is their name, e.g. in glibc you have __GI___libc_free,
567 * __cfree, __free, __libc_free, cfree and free all defined on the
568 * same address. So instead we keep this unique symbol struct for
569 * each address, and replace name in libsym with a shorter variant if
570 * we find it. */
571struct unique_symbol {
572 target_address_t addr;
573 struct library_symbol *libsym;
574};
575
576static int
577unique_symbol_cmp(const void *key, const void *val)
578{
579 const struct unique_symbol *sym_key = key;
580 const struct unique_symbol *sym_val = val;
581 return sym_key->addr != sym_val->addr;
582}
583
Petr Machatada3edbf2012-04-04 02:20:21 +0200584static int
585populate_this_symtab(struct Process *proc, const char *filename,
586 struct ltelf *lte, struct library *lib,
587 Elf_Data *symtab, const char *strtab, size_t size)
588{
Petr Machata157cc4d2012-04-04 19:00:34 +0200589 /* Using sorted array would be arguably better, but this
590 * should be well enough for the number of symbols that we
591 * typically deal with. */
592 size_t num_symbols = 0;
593 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
594 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200595 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
596 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200597 return -1;
598 }
599
Petr Machata40cc53b2012-04-07 01:25:38 +0200600 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200601 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200602 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
603 Elf_Scn *scn = elf_getscn(lte->elf, i);
604 if (scn == NULL)
605 continue;
606 GElf_Shdr shdr;
607 if (gelf_getshdr(scn, &shdr) == NULL)
608 continue;
609 secflags[i] = shdr.sh_flags;
610 }
611
612 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200613 for (i = 0; i < size; ++i) {
614 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200615 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200616 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200617 fprintf(stderr,
618 "couldn't get symbol #%zd from %s: %s\n",
619 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200620 continue;
621 }
622
Petr Machata4de6b6b2012-04-04 14:06:09 +0200623 /* XXX support IFUNC as well. */
624 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
Petr Machata6c1c0bb2012-05-18 16:49:34 +0200625 || sym.st_value == 0
626 || sym.st_shndx == STN_UNDEF)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200627 continue;
628
Petr Machata2bbeac42012-04-30 20:48:34 +0200629 const char *orig_name = strtab + sym.st_name;
630 const char *version = strchr(orig_name, '@');
631 size_t len = version != NULL ? (assert(version > orig_name),
632 (size_t)(version - orig_name))
633 : strlen(orig_name);
634 char name[len + 1];
635 memcpy(name, orig_name, len);
636 name[len] = 0;
637
Petr Machatada3edbf2012-04-04 02:20:21 +0200638 if (!filter_matches_symbol(options.static_filter, name, lib))
639 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200640
Petr Machataea8eb9a2012-04-17 01:32:07 +0200641 target_address_t addr = (target_address_t)
642 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatada3edbf2012-04-04 02:20:21 +0200643 target_address_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200644
645 /* On arches that support OPD, the value of typical
646 * function symbol will be a pointer to .opd, but some
647 * will point directly to .text. We don't want to
648 * translate those. */
649 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
650 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200651 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200652 fprintf(stderr,
653 "couldn't translate address of %s@%s: %s\n",
654 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200655 continue;
656 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200657
Petr Machata3840f682012-04-06 16:05:41 +0200658 char *full_name;
659 if (lib->type != LT_LIBTYPE_MAIN) {
660 full_name = malloc(strlen(name) + 1 + lib_len + 1);
661 if (full_name == NULL)
662 goto fail;
663 sprintf(full_name, "%s@%s", name, lib->soname);
664 } else {
665 full_name = strdup(name);
666 if (full_name == NULL)
667 goto fail;
668 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200669
Petr Machata157cc4d2012-04-04 19:00:34 +0200670 /* Look whether we already have a symbol for this
671 * address. If not, add this one. */
672 struct unique_symbol key = { naddr, NULL };
673 struct unique_symbol *unique
674 = lsearch(&key, symbols, &num_symbols,
675 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200676
Petr Machata157cc4d2012-04-04 19:00:34 +0200677 if (unique->libsym == NULL) {
678 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200679 if (libsym == NULL
680 || library_symbol_init(libsym, naddr, full_name,
681 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200682 --num_symbols;
683 goto fail;
684 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200685 unique->libsym = libsym;
686 unique->addr = naddr;
687
688 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
689 library_symbol_set_name(unique->libsym, full_name, 1);
690
691 } else {
692 free(full_name);
693 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200694 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200695
696 for (i = 0; i < num_symbols; ++i) {
697 assert(symbols[i].libsym != NULL);
698 library_add_symbol(lib, symbols[i].libsym);
699 }
700
701 free(symbols);
702
Petr Machatada3edbf2012-04-04 02:20:21 +0200703 return 0;
704}
705
706static int
707populate_symtab(struct Process *proc, const char *filename,
708 struct ltelf *lte, struct library *lib)
709{
710 if (lte->symtab != NULL && lte->strtab != NULL)
711 return populate_this_symtab(proc, filename, lte, lib,
712 lte->symtab, lte->strtab,
713 lte->symtab_count);
714 else
715 return populate_this_symtab(proc, filename, lte, lib,
716 lte->dynsym, lte->dynstr,
717 lte->dynsym_count);
718}
719
Petr Machatab5f80ac2012-04-04 01:46:18 +0200720int
721ltelf_read_library(struct library *lib, struct Process *proc,
722 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100723{
Petr Machata29add4f2012-02-18 16:38:05 +0100724 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100725 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200726 return -1;
Petr Machatae0615ab2012-04-17 05:17:48 +0200727 if (arch_elf_init(&lte, lib) < 0) {
728 fprintf(stderr, "Backend initialization failed.\n");
729 return -1;
730 }
731
Petr Machatae67635d2012-03-21 03:37:39 +0100732 proc->e_machine = lte.ehdr.e_machine;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800733
Petr Machatab5f80ac2012-04-04 01:46:18 +0200734 int status = 0;
Petr Machatafc6ff182012-04-04 13:11:50 +0200735 if (lib == NULL)
736 goto fail;
737
738 /* Note that we set soname and pathname as soon as they are
739 * allocated, so in case of further errors, this get released
740 * when LIB is release, which should happen in the caller when
741 * we return error. */
742
743 if (lib->pathname == NULL) {
744 char *pathname = strdup(filename);
745 if (pathname == NULL)
746 goto fail;
Petr Machataf13afd52012-04-14 02:30:31 +0200747 library_set_pathname(lib, pathname, 1);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800748 }
749
Petr Machata0b55b582012-04-02 00:38:46 +0200750 if (lte.soname != NULL) {
Petr Machatafc6ff182012-04-04 13:11:50 +0200751 char *soname = strdup(lte.soname);
752 if (soname == NULL)
753 goto fail;
754 library_set_soname(lib, soname, 1);
Petr Machata0b55b582012-04-02 00:38:46 +0200755 } else {
Petr Machatafc6ff182012-04-04 13:11:50 +0200756 const char *soname = rindex(lib->pathname, '/') + 1;
757 if (soname == NULL)
758 soname = lib->pathname;
759 library_set_soname(lib, soname, 0);
Petr Machata0b55b582012-04-02 00:38:46 +0200760 }
Joe Damatofa2aefc2010-10-30 19:56:50 -0700761
Petr Machataea8eb9a2012-04-17 01:32:07 +0200762 /* XXX The double cast should be removed when
763 * target_address_t becomes integral type. */
764 target_address_t entry = (target_address_t)(uintptr_t)lte.entry_addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200765 if (arch_translate_address(&lte, entry, &entry) < 0)
Petr Machatab120fdf2012-03-21 05:05:46 +0100766 goto fail;
767
Petr Machataea8eb9a2012-04-17 01:32:07 +0200768 /* XXX The double cast should be removed when
769 * target_address_t becomes integral type. */
770 lib->base = (target_address_t)(uintptr_t)lte.base_addr;
Petr Machatab120fdf2012-03-21 05:05:46 +0100771 lib->entry = entry;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200772 /* XXX The double cast should be removed when
773 * target_address_t becomes integral type. */
774 lib->dyn_addr = (target_address_t)(uintptr_t)lte.dyn_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100775
Petr Machatab5f80ac2012-04-04 01:46:18 +0200776 if (filter_matches_library(options.plt_filter, lib)
777 && populate_plt(proc, filename, &lte, lib) < 0)
778 goto fail;
Joe Damatoe2a8f572010-11-08 15:47:40 -0800779
Petr Machatada3edbf2012-04-04 02:20:21 +0200780 if (filter_matches_library(options.static_filter, lib)
781 && populate_symtab(proc, filename, &lte, lib) < 0)
782 goto fail;
783
Petr Machata2b46cfc2012-02-18 11:17:29 +0100784done:
785 do_close_elf(&lte);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200786 return status;
Petr Machatafc6ff182012-04-04 13:11:50 +0200787
788fail:
789 status = -1;
790 goto done;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100791}
Petr Machatae84fa002012-02-07 13:43:03 +0100792
Petr Machata2b46cfc2012-02-18 11:17:29 +0100793struct library *
794ltelf_read_main_binary(struct Process *proc, const char *path)
795{
Petr Machatab5f80ac2012-04-04 01:46:18 +0200796 struct library *lib = malloc(sizeof(*lib));
797 if (lib == NULL)
798 return NULL;
799 library_init(lib, LT_LIBTYPE_MAIN);
Petr Machatafc6ff182012-04-04 13:11:50 +0200800 library_set_pathname(lib, path, 0);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200801
Petr Machatafc6ff182012-04-04 13:11:50 +0200802 /* There is a race between running the process and reading its
803 * binary for internal consumption. So open the binary from
804 * the /proc filesystem. XXX Note that there is similar race
805 * for libraries, but there we don't have a nice answer like
806 * that. Presumably we could read the DSOs from the process
807 * memory image, but that's not currently done. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100808 char *fname = pid2name(proc->pid);
Petr Machatab5f80ac2012-04-04 01:46:18 +0200809 if (ltelf_read_library(lib, proc, fname, 0) < 0) {
810 library_destroy(lib);
811 free(lib);
812 return NULL;
Petr Machata0b55b582012-04-02 00:38:46 +0200813 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200814
Petr Machata2b46cfc2012-02-18 11:17:29 +0100815 return lib;
Juan Cespedes96935a91997-08-09 23:45:39 +0200816}