blob: bc99c6a643fbf86d0ffb6b04449c24a4aa1a3897 [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
Petr Machatae67635d2012-03-21 03:37:39 +010053#ifndef ARCH_HAVE_LTELF_DATA
54int
Petr Machatae0615ab2012-04-17 05:17:48 +020055arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +010056{
57 return 0;
58}
Petr Machatac67a6e62012-03-28 02:39:49 +020059
60void
61arch_elf_destroy(struct ltelf *lte)
62{
63}
Petr Machatae67635d2012-03-21 03:37:39 +010064#endif
65
Petr Machatae6523e62012-03-24 04:54:06 +010066int
67default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +020068 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +010069 struct library_symbol **ret)
70{
71 char *name = strdup(a_name);
72 if (name == NULL) {
73 fail:
74 free(name);
75 return -1;
76 }
77
Petr Machata1be22912012-03-27 03:11:33 +020078 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
Petr Machatae6523e62012-03-24 04:54:06 +010079
80 struct library_symbol *libsym = malloc(sizeof(*libsym));
81 if (libsym == NULL)
82 goto fail;
83
Petr Machataea8eb9a2012-04-17 01:32:07 +020084 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +020085 * arch_addr_t becomes integral type. */
86 arch_addr_t taddr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +020087 (uintptr_t)(addr + lte->bias);
Petr Machatabb790602012-03-25 01:41:59 +010088
Petr Machatae8d90762012-04-15 04:28:31 +020089 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
90 free(libsym);
91 goto fail;
92 }
93
Petr Machatae6523e62012-03-24 04:54:06 +010094 *ret = libsym;
95 return 0;
96}
97
98#ifndef ARCH_HAVE_ADD_PLT_ENTRY
99enum plt_status
100arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machata1be22912012-03-27 03:11:33 +0200101 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machatae6523e62012-03-24 04:54:06 +0100102 struct library_symbol **ret)
103{
104 return plt_default;
105}
106#endif
107
Petr Machatae67635d2012-03-21 03:37:39 +0100108Elf_Data *
109elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
Petr Machatafe1c1712010-10-27 16:57:34 +0200110{
111 Elf_Data *data = elf_getdata(scn, NULL);
112 if (data == NULL || elf_getdata(scn, data) != NULL
113 || data->d_off || data->d_size != shdr->sh_size)
114 return NULL;
115 return data;
116}
117
Petr Machatae67635d2012-03-21 03:37:39 +0100118static int
Petr Machataffd5aab2012-03-24 02:03:33 +0100119elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
120 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
121 void *data)
Petr Machatafe1c1712010-10-27 16:57:34 +0200122{
123 int i;
124 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
125 Elf_Scn *scn;
126 GElf_Shdr shdr;
127
128 scn = elf_getscn(lte->elf, i);
129 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
130 debug(1, "Couldn't read section or header.");
Petr Machatae67635d2012-03-21 03:37:39 +0100131 return -1;
132 }
Petr Machataffd5aab2012-03-24 02:03:33 +0100133 if (predicate(scn, &shdr, data)) {
134 *tgt_sec = scn;
135 *tgt_shdr = shdr;
Petr Machatafe1c1712010-10-27 16:57:34 +0200136 return 0;
Petr Machataffd5aab2012-03-24 02:03:33 +0100137 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200138 }
Petr Machatae67635d2012-03-21 03:37:39 +0100139 return -1;
Petr Machataffd5aab2012-03-24 02:03:33 +0100140
141}
142
143static int
144inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
145{
146 GElf_Addr addr = *(GElf_Addr *)data;
147 return addr >= shdr->sh_addr
148 && addr < shdr->sh_addr + shdr->sh_size;
149}
150
151int
152elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
153 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
154{
155 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
156 &inside_p, &addr);
157}
158
159static int
160type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
161{
162 GElf_Word type = *(GElf_Word *)data;
163 return shdr->sh_type == type;
164}
165
166int
167elf_get_section_type(struct ltelf *lte, GElf_Word type,
168 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
169{
170 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
171 &type_p, &type);
Petr Machatae67635d2012-03-21 03:37:39 +0100172}
173
Petr Machata5b3e26a2012-04-30 20:53:22 +0200174struct section_named_data {
175 struct ltelf *lte;
176 const char *name;
177};
178
179static int
180name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
181{
182 struct section_named_data *data = d;
183 const char *name = elf_strptr(data->lte->elf,
184 data->lte->ehdr.e_shstrndx,
185 shdr->sh_name);
186 return strcmp(name, data->name) == 0;
187}
188
189int
190elf_get_section_named(struct ltelf *lte, const char *name,
191 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
192{
193 struct section_named_data data = {
194 .lte = lte,
195 .name = name,
196 };
197 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
198 &name_p, &data);
199}
200
Petr Machatae67635d2012-03-21 03:37:39 +0100201static int
Petr Machata3a01cd72012-04-30 20:50:20 +0200202need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
Petr Machatae67635d2012-03-21 03:37:39 +0100203{
204 assert(data != NULL);
205 if (data->d_size < size || offset > data->d_size - size) {
Petr Machataa82d3222012-05-01 01:04:27 +0200206 debug(1, "Not enough data to read %"PRId64"-byte value"
207 " at offset %"PRId64".", size, offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100208 return -1;
209 }
Petr Machatafe1c1712010-10-27 16:57:34 +0200210 return 0;
211}
212
Petr Machatae67635d2012-03-21 03:37:39 +0100213#define DEF_READER(NAME, SIZE) \
214 int \
Petr Machata3a01cd72012-04-30 20:50:20 +0200215 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
Petr Machatae67635d2012-03-21 03:37:39 +0100216 { \
217 if (!need_data(data, offset, SIZE / 8) < 0) \
218 return -1; \
219 \
Petr Machata6d8ccb22012-03-27 03:11:57 +0200220 if (data->d_buf == NULL) /* NODATA section */ { \
221 *retp = 0; \
222 return 0; \
223 } \
224 \
Petr Machatae67635d2012-03-21 03:37:39 +0100225 union { \
226 uint##SIZE##_t dst; \
227 char buf[0]; \
228 } u; \
229 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
230 *retp = u.dst; \
231 return 0; \
Petr Machatafe1c1712010-10-27 16:57:34 +0200232 }
233
Petr Machatae67635d2012-03-21 03:37:39 +0100234DEF_READER(elf_read_u16, 16)
235DEF_READER(elf_read_u32, 32)
236DEF_READER(elf_read_u64, 64)
Petr Machatafe1c1712010-10-27 16:57:34 +0200237
Petr Machatae67635d2012-03-21 03:37:39 +0100238#undef DEF_READER
Petr Machatafe1c1712010-10-27 16:57:34 +0200239
Petr Machata1974dbc2011-08-19 18:58:01 +0200240int
Petr Machata02bd9ec2011-09-21 17:38:59 +0200241open_elf(struct ltelf *lte, const char *filename)
242{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100243 lte->fd = open(filename, O_RDONLY);
244 if (lte->fd == -1)
Petr Machata1974dbc2011-08-19 18:58:01 +0200245 return 1;
Juan Cespedes96935a91997-08-09 23:45:39 +0200246
Petr Machata02bd9ec2011-09-21 17:38:59 +0200247 elf_version(EV_CURRENT);
248
Juan Cespedesd914a202004-11-10 00:15:33 +0100249#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200251#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100252 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200253#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200254
Petr Machatacc0e1e42012-04-25 13:42:07 +0200255 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
256 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
257 exit(EXIT_FAILURE);
258 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200259
Petr Machatacc0e1e42012-04-25 13:42:07 +0200260 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
261 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
262 filename, elf_errmsg(-1));
263 exit(EXIT_FAILURE);
264 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200265
Petr Machatacc0e1e42012-04-25 13:42:07 +0200266 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
267 fprintf(stderr, "\"%s\" is neither an ELF executable"
268 " nor a shared library\n", filename);
269 exit(EXIT_FAILURE);
270 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200271
Petr Machataddd96a32012-05-17 23:35:26 +0200272 if (1
273#ifdef LT_ELF_MACHINE
274 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
275 || lte->ehdr.e_machine != LT_ELF_MACHINE)
276#endif
Juan Cespedesd914a202004-11-10 00:15:33 +0100277#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
279 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +0100280#endif
281#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
283 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +0100284#endif
Petr Machatacc0e1e42012-04-25 13:42:07 +0200285 ) {
286 fprintf(stderr,
287 "\"%s\" is ELF from incompatible architecture\n",
288 filename);
289 exit(EXIT_FAILURE);
290 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100291
Petr Machata02bd9ec2011-09-21 17:38:59 +0200292 return 0;
293}
294
Petr Machatacc0e1e42012-04-25 13:42:07 +0200295static void
296read_symbol_table(struct ltelf *lte, const char *filename,
297 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
298 Elf_Data **datap, size_t *countp, const char **strsp)
299{
300 *datap = elf_getdata(scn, NULL);
301 *countp = shdr->sh_size / shdr->sh_entsize;
302 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
303 && options.static_filter != NULL) {
304 fprintf(stderr, "Couldn't get data of section"
305 " %s from \"%s\": %s\n",
306 name, filename, elf_errmsg(-1));
307 exit(EXIT_FAILURE);
308 }
309
310 scn = elf_getscn(lte->elf, shdr->sh_link);
311 GElf_Shdr shdr2;
312 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
313 fprintf(stderr, "Couldn't get header of section"
314 " #%d from \"%s\": %s\n",
315 shdr2.sh_link, filename, elf_errmsg(-1));
316 exit(EXIT_FAILURE);
317 }
318
319 Elf_Data *data = elf_getdata(scn, NULL);
320 if (data == NULL || elf_getdata(scn, data) != NULL
321 || shdr2.sh_size != data->d_size || data->d_off) {
322 fprintf(stderr, "Couldn't get data of section"
323 " #%d from \"%s\": %s\n",
324 shdr2.sh_link, filename, elf_errmsg(-1));
325 exit(EXIT_FAILURE);
326 }
327
328 *strsp = data->d_buf;
329}
330
Petr Machatae67635d2012-03-21 03:37:39 +0100331static int
332do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333{
Petr Machata02bd9ec2011-09-21 17:38:59 +0200334 int i;
335 GElf_Addr relplt_addr = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100336 GElf_Addr soname_offset = 0;
Petr Machata02bd9ec2011-09-21 17:38:59 +0200337
338 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
339 debug(1, "Reading ELF from %s...", filename);
340
341 if (open_elf(lte, filename) < 0)
342 return -1;
343
Petr Machatab120fdf2012-03-21 05:05:46 +0100344 /* Find out the base address. */
Petr Machata29add4f2012-02-18 16:38:05 +0100345 {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100346 GElf_Phdr phdr;
347 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
348 if (phdr.p_type == PT_LOAD) {
Petr Machata49275b02012-04-03 12:38:51 +0200349 lte->base_addr = phdr.p_vaddr + bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100350 break;
351 }
352 }
353 }
354
Petr Machatab120fdf2012-03-21 05:05:46 +0100355 if (lte->base_addr == 0) {
356 fprintf(stderr, "Couldn't determine base address of %s\n",
357 filename);
358 return -1;
359 }
360
361 lte->bias = bias;
Petr Machata29add4f2012-02-18 16:38:05 +0100362 lte->entry_addr = lte->ehdr.e_entry + lte->bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100363
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
365 Elf_Scn *scn;
366 GElf_Shdr shdr;
367 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100368
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200370 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
371 fprintf(stderr, "Couldn't get section #%d from"
372 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
373 exit(EXIT_FAILURE);
374 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100375
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200377 if (name == NULL) {
378 fprintf(stderr, "Couldn't get name of section #%d from"
379 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
380 exit(EXIT_FAILURE);
381 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100382
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100383 if (shdr.sh_type == SHT_SYMTAB) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200384 read_symbol_table(lte, filename,
385 scn, &shdr, name, &lte->symtab,
386 &lte->symtab_count, &lte->strtab);
Juan Cespedesd914a202004-11-10 00:15:33 +0100387
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100388 } else if (shdr.sh_type == SHT_DYNSYM) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200389 read_symbol_table(lte, filename,
390 scn, &shdr, name, &lte->dynsym,
391 &lte->dynsym_count, &lte->dynstr);
Juan Cespedesd914a202004-11-10 00:15:33 +0100392
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100393 } else if (shdr.sh_type == SHT_DYNAMIC) {
394 Elf_Data *data;
395 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100396
Joe Damato87f4f582010-11-08 15:47:36 -0800397 lte->dyn_addr = shdr.sh_addr;
398 lte->dyn_sz = shdr.sh_size;
399
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 data = elf_getdata(scn, NULL);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200401 if (data == NULL || elf_getdata(scn, data) != NULL) {
402 fprintf(stderr, "Couldn't get .dynamic data"
403 " from \"%s\": %s\n",
404 filename, strerror(errno));
405 exit(EXIT_FAILURE);
406 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100407
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
409 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100410
Petr Machatacc0e1e42012-04-25 13:42:07 +0200411 if (gelf_getdyn(data, j, &dyn) == NULL) {
412 fprintf(stderr, "Couldn't get .dynamic"
413 " data from \"%s\": %s\n",
414 filename, strerror(errno));
415 exit(EXIT_FAILURE);
416 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100417 if (dyn.d_tag == DT_JMPREL)
418 relplt_addr = dyn.d_un.d_ptr;
419 else if (dyn.d_tag == DT_PLTRELSZ)
Petr Machatae67635d2012-03-21 03:37:39 +0100420 lte->relplt_size = dyn.d_un.d_val;
421 else if (dyn.d_tag == DT_SONAME)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100422 soname_offset = dyn.d_un.d_val;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100423 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100424 } else if (shdr.sh_type == SHT_PROGBITS
425 || shdr.sh_type == SHT_NOBITS) {
426 if (strcmp(name, ".plt") == 0) {
427 lte->plt_addr = shdr.sh_addr;
428 lte->plt_size = shdr.sh_size;
Petr Machatae67635d2012-03-21 03:37:39 +0100429 lte->plt_data = elf_loaddata(scn, &shdr);
430 if (lte->plt_data == NULL)
431 fprintf(stderr,
432 "Can't load .plt data\n");
Petr Machata18c801c2012-04-07 01:24:08 +0200433 lte->plt_flags = shdr.sh_flags;
Petr Machatab3f8fef2006-11-30 14:45:07 +0100434 }
435#ifdef ARCH_SUPPORTS_OPD
436 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200437 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 lte->opd_size = shdr.sh_size;
439 lte->opd = elf_rawdata(scn, NULL);
440 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100441#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100442 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100443 }
444
Petr Machatacc0e1e42012-04-25 13:42:07 +0200445 if (lte->dynsym == NULL || lte->dynstr == NULL) {
446 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
447 filename);
448 exit(EXIT_FAILURE);
449 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100450
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100451 if (!relplt_addr || !lte->plt_addr) {
452 debug(1, "%s has no PLT relocations", filename);
453 lte->relplt = NULL;
454 lte->relplt_count = 0;
Petr Machatae67635d2012-03-21 03:37:39 +0100455 } else if (lte->relplt_size == 0) {
Petr Machatafe1c1712010-10-27 16:57:34 +0200456 debug(1, "%s has unknown PLT size", filename);
457 lte->relplt = NULL;
458 lte->relplt_count = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100459 } else {
Petr Machatafe1c1712010-10-27 16:57:34 +0200460
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100461 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
462 Elf_Scn *scn;
463 GElf_Shdr shdr;
464
465 scn = elf_getscn(lte->elf, i);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200466 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
467 fprintf(stderr, "Couldn't get section header"
468 " from \"%s\": %s\n",
469 filename, elf_errmsg(-1));
470 exit(EXIT_FAILURE);
471 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100472 if (shdr.sh_addr == relplt_addr
Petr Machatae67635d2012-03-21 03:37:39 +0100473 && shdr.sh_size == lte->relplt_size) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100474 lte->relplt = elf_getdata(scn, NULL);
475 lte->relplt_count =
476 shdr.sh_size / shdr.sh_entsize;
477 if (lte->relplt == NULL
Petr Machatacc0e1e42012-04-25 13:42:07 +0200478 || elf_getdata(scn, lte->relplt) != NULL) {
479 fprintf(stderr, "Couldn't get .rel*.plt"
480 " data from \"%s\": %s\n",
481 filename, elf_errmsg(-1));
482 exit(EXIT_FAILURE);
483 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100484 break;
485 }
486 }
487
Petr Machatacc0e1e42012-04-25 13:42:07 +0200488 if (i == lte->ehdr.e_shnum) {
489 fprintf(stderr,
490 "Couldn't find .rel*.plt section in \"%s\"\n",
491 filename);
492 exit(EXIT_FAILURE);
493 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100494
495 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
496 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100497
498 if (soname_offset != 0)
499 lte->soname = lte->dynstr + soname_offset;
500
Petr Machata1974dbc2011-08-19 18:58:01 +0200501 return 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100502}
503
Petr Machata2b46cfc2012-02-18 11:17:29 +0100504/* XXX temporarily non-static */
Joe Damato7a2bdf82010-11-08 15:47:41 -0800505void
Juan Cespedesf1350522008-12-16 18:19:58 +0100506do_close_elf(struct ltelf *lte) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200507 debug(DEBUG_FUNCTION, "do_close_elf()");
Petr Machata4d9a91c2012-03-24 04:55:03 +0100508 arch_elf_destroy(lte);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100509 elf_end(lte->elf);
510 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200511}
512
Petr Machatab5f80ac2012-04-04 01:46:18 +0200513static int
514populate_plt(struct Process *proc, const char *filename,
515 struct ltelf *lte, struct library *lib)
516{
517 size_t i;
518 for (i = 0; i < lte->relplt_count; ++i) {
519 GElf_Rel rel;
520 GElf_Rela rela;
521 GElf_Sym sym;
522 void *ret;
523
524 if (lte->relplt->d_type == ELF_T_REL) {
525 ret = gelf_getrel(lte->relplt, i, &rel);
526 rela.r_offset = rel.r_offset;
527 rela.r_info = rel.r_info;
528 rela.r_addend = 0;
529 } else {
530 ret = gelf_getrela(lte->relplt, i, &rela);
531 }
532
533 if (ret == NULL
534 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
535 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
Petr Machatacc0e1e42012-04-25 13:42:07 +0200536 &sym) == NULL) {
537 fprintf(stderr,
538 "Couldn't get relocation from \"%s\": %s\n",
539 filename, elf_errmsg(-1));
540 exit(EXIT_FAILURE);
541 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200542
543 char const *name = lte->dynstr + sym.st_name;
544
545 if (!filter_matches_symbol(options.plt_filter, name, lib))
546 continue;
547
Petr Machata218c5ff2012-04-15 04:22:39 +0200548 struct library_symbol *libsym = NULL;
Petr Machatab5f80ac2012-04-04 01:46:18 +0200549 switch (arch_elf_add_plt_entry(proc, lte, name,
550 &rela, i, &libsym)) {
551 case plt_default:
552 if (default_elf_add_plt_entry(proc, lte, name,
553 &rela, i, &libsym) < 0)
Petr Machata8eb0d932012-04-17 05:18:18 +0200554 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200555 case plt_fail:
556 return -1;
Petr Machata8eb0d932012-04-17 05:18:18 +0200557 /* fall-through */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200558 case plt_ok:
559 if (libsym != NULL)
560 library_add_symbol(lib, libsym);
561 }
562 }
563 return 0;
564}
565
Petr Machata157cc4d2012-04-04 19:00:34 +0200566/* When -x rules result in request to trace several aliases, we only
567 * want to add such symbol once. The only way that those symbols
568 * differ in is their name, e.g. in glibc you have __GI___libc_free,
569 * __cfree, __free, __libc_free, cfree and free all defined on the
570 * same address. So instead we keep this unique symbol struct for
571 * each address, and replace name in libsym with a shorter variant if
572 * we find it. */
573struct unique_symbol {
Petr Machatabac2da52012-05-29 00:42:59 +0200574 arch_addr_t addr;
Petr Machata157cc4d2012-04-04 19:00:34 +0200575 struct library_symbol *libsym;
576};
577
578static int
579unique_symbol_cmp(const void *key, const void *val)
580{
581 const struct unique_symbol *sym_key = key;
582 const struct unique_symbol *sym_val = val;
583 return sym_key->addr != sym_val->addr;
584}
585
Petr Machatada3edbf2012-04-04 02:20:21 +0200586static int
587populate_this_symtab(struct Process *proc, const char *filename,
588 struct ltelf *lte, struct library *lib,
589 Elf_Data *symtab, const char *strtab, size_t size)
590{
Petr Machata157cc4d2012-04-04 19:00:34 +0200591 /* Using sorted array would be arguably better, but this
592 * should be well enough for the number of symbols that we
593 * typically deal with. */
594 size_t num_symbols = 0;
595 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
596 if (symbols == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200597 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
598 strerror(errno));
Petr Machata157cc4d2012-04-04 19:00:34 +0200599 return -1;
600 }
601
Petr Machata40cc53b2012-04-07 01:25:38 +0200602 GElf_Word secflags[lte->ehdr.e_shnum];
Petr Machatada3edbf2012-04-04 02:20:21 +0200603 size_t i;
Petr Machata40cc53b2012-04-07 01:25:38 +0200604 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
605 Elf_Scn *scn = elf_getscn(lte->elf, i);
606 if (scn == NULL)
607 continue;
608 GElf_Shdr shdr;
609 if (gelf_getshdr(scn, &shdr) == NULL)
610 continue;
611 secflags[i] = shdr.sh_flags;
612 }
613
614 size_t lib_len = strlen(lib->soname);
Petr Machatada3edbf2012-04-04 02:20:21 +0200615 for (i = 0; i < size; ++i) {
616 GElf_Sym sym;
Petr Machata074f68f2012-04-07 01:01:02 +0200617 if (gelf_getsym(symtab, i, &sym) == NULL) {
Petr Machatada3edbf2012-04-04 02:20:21 +0200618 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200619 fprintf(stderr,
620 "couldn't get symbol #%zd from %s: %s\n",
621 i, filename, elf_errmsg(-1));
Petr Machatada3edbf2012-04-04 02:20:21 +0200622 continue;
623 }
624
Petr Machata4de6b6b2012-04-04 14:06:09 +0200625 /* XXX support IFUNC as well. */
626 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
Petr Machata6c1c0bb2012-05-18 16:49:34 +0200627 || sym.st_value == 0
628 || sym.st_shndx == STN_UNDEF)
Petr Machata4de6b6b2012-04-04 14:06:09 +0200629 continue;
630
Petr Machata2bbeac42012-04-30 20:48:34 +0200631 const char *orig_name = strtab + sym.st_name;
632 const char *version = strchr(orig_name, '@');
633 size_t len = version != NULL ? (assert(version > orig_name),
634 (size_t)(version - orig_name))
635 : strlen(orig_name);
636 char name[len + 1];
637 memcpy(name, orig_name, len);
638 name[len] = 0;
639
Petr Machatada3edbf2012-04-04 02:20:21 +0200640 if (!filter_matches_symbol(options.static_filter, name, lib))
641 continue;
Petr Machatada3edbf2012-04-04 02:20:21 +0200642
Petr Machatabac2da52012-05-29 00:42:59 +0200643 arch_addr_t addr = (arch_addr_t)
Petr Machataea8eb9a2012-04-17 01:32:07 +0200644 (uintptr_t)(sym.st_value + lte->bias);
Petr Machatabac2da52012-05-29 00:42:59 +0200645 arch_addr_t naddr;
Petr Machata40cc53b2012-04-07 01:25:38 +0200646
647 /* On arches that support OPD, the value of typical
648 * function symbol will be a pointer to .opd, but some
649 * will point directly to .text. We don't want to
650 * translate those. */
651 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
652 naddr = addr;
Petr Machatab1492df2012-04-30 21:01:40 +0200653 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200654 fprintf(stderr,
655 "couldn't translate address of %s@%s: %s\n",
656 name, lib->soname, strerror(errno));
Petr Machatada3edbf2012-04-04 02:20:21 +0200657 continue;
658 }
Petr Machata40cc53b2012-04-07 01:25:38 +0200659
Petr Machata3840f682012-04-06 16:05:41 +0200660 char *full_name;
661 if (lib->type != LT_LIBTYPE_MAIN) {
662 full_name = malloc(strlen(name) + 1 + lib_len + 1);
663 if (full_name == NULL)
664 goto fail;
665 sprintf(full_name, "%s@%s", name, lib->soname);
666 } else {
667 full_name = strdup(name);
668 if (full_name == NULL)
669 goto fail;
670 }
Petr Machata4de6b6b2012-04-04 14:06:09 +0200671
Petr Machata157cc4d2012-04-04 19:00:34 +0200672 /* Look whether we already have a symbol for this
673 * address. If not, add this one. */
674 struct unique_symbol key = { naddr, NULL };
675 struct unique_symbol *unique
676 = lsearch(&key, symbols, &num_symbols,
677 sizeof(*symbols), &unique_symbol_cmp);
Petr Machatada3edbf2012-04-04 02:20:21 +0200678
Petr Machata157cc4d2012-04-04 19:00:34 +0200679 if (unique->libsym == NULL) {
680 struct library_symbol *libsym = malloc(sizeof(*libsym));
Petr Machatae8d90762012-04-15 04:28:31 +0200681 if (libsym == NULL
682 || library_symbol_init(libsym, naddr, full_name,
683 1, LS_TOPLT_NONE) < 0) {
Petr Machata157cc4d2012-04-04 19:00:34 +0200684 --num_symbols;
685 goto fail;
686 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200687 unique->libsym = libsym;
688 unique->addr = naddr;
689
690 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
691 library_symbol_set_name(unique->libsym, full_name, 1);
692
693 } else {
694 free(full_name);
695 }
Petr Machatada3edbf2012-04-04 02:20:21 +0200696 }
Petr Machata157cc4d2012-04-04 19:00:34 +0200697
698 for (i = 0; i < num_symbols; ++i) {
699 assert(symbols[i].libsym != NULL);
700 library_add_symbol(lib, symbols[i].libsym);
701 }
702
703 free(symbols);
704
Petr Machatada3edbf2012-04-04 02:20:21 +0200705 return 0;
706}
707
708static int
709populate_symtab(struct Process *proc, const char *filename,
710 struct ltelf *lte, struct library *lib)
711{
712 if (lte->symtab != NULL && lte->strtab != NULL)
713 return populate_this_symtab(proc, filename, lte, lib,
714 lte->symtab, lte->strtab,
715 lte->symtab_count);
716 else
717 return populate_this_symtab(proc, filename, lte, lib,
718 lte->dynsym, lte->dynstr,
719 lte->dynsym_count);
720}
721
Petr Machatab5f80ac2012-04-04 01:46:18 +0200722int
723ltelf_read_library(struct library *lib, struct Process *proc,
724 const char *filename, GElf_Addr bias)
Petr Machatae84fa002012-02-07 13:43:03 +0100725{
Petr Machata29add4f2012-02-18 16:38:05 +0100726 struct ltelf lte = {};
Petr Machatab120fdf2012-03-21 05:05:46 +0100727 if (do_init_elf(&lte, filename, bias) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200728 return -1;
Petr Machatae0615ab2012-04-17 05:17:48 +0200729 if (arch_elf_init(&lte, lib) < 0) {
730 fprintf(stderr, "Backend initialization failed.\n");
731 return -1;
732 }
733
Petr Machatae67635d2012-03-21 03:37:39 +0100734 proc->e_machine = lte.ehdr.e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400735 proc->e_class = lte.ehdr.e_ident[EI_CLASS];
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
Petr Machatabac2da52012-05-29 00:42:59 +0200766 * arch_addr_t becomes integral type. */
767 arch_addr_t entry = (arch_addr_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
Petr Machatabac2da52012-05-29 00:42:59 +0200772 * arch_addr_t becomes integral type. */
773 lib->base = (arch_addr_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
Petr Machatabac2da52012-05-29 00:42:59 +0200776 * arch_addr_t becomes integral type. */
777 lib->dyn_addr = (arch_addr_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}