Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / ltrace-elf.c
1 /*
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
28 #include "config.h"
29
30 #include <assert.h>
31 #ifdef  __linux__
32 #include <endian.h>
33 #endif
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <gelf.h>
37 #include <inttypes.h>
38 #include <search.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "backend.h"
46 #include "filter.h"
47 #include "library.h"
48 #include "ltrace-elf.h"
49 #include "proc.h"
50 #include "debug.h"
51 #include "options.h"
52
53 #ifndef ARCH_HAVE_LTELF_DATA
54 int
55 arch_elf_init(struct ltelf *lte, struct library *lib)
56 {
57         return 0;
58 }
59
60 void
61 arch_elf_destroy(struct ltelf *lte)
62 {
63 }
64 #endif
65
66 int
67 default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
68                           const char *a_name, GElf_Rela *rela, size_t ndx,
69                           struct library_symbol **ret)
70 {
71         char *name = strdup(a_name);
72         if (name == NULL) {
73         fail_message:
74                 fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n",
75                         strerror(errno));
76         fail:
77                 free(name);
78                 return -1;
79         }
80
81         GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
82
83         struct library_symbol *libsym = malloc(sizeof(*libsym));
84         if (libsym == NULL)
85                 goto fail_message;
86
87         /* XXX The double cast should be removed when
88          * arch_addr_t becomes integral type.  */
89         arch_addr_t taddr = (arch_addr_t)
90                 (uintptr_t)(addr + lte->bias);
91
92         if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
93                 free(libsym);
94                 goto fail;
95         }
96
97         libsym->next = *ret;
98         *ret = libsym;
99         return 0;
100 }
101
102 #ifndef ARCH_HAVE_ADD_PLT_ENTRY
103 enum plt_status
104 arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
105                        const char *a_name, GElf_Rela *rela, size_t ndx,
106                        struct library_symbol **ret)
107 {
108         return plt_default;
109 }
110 #endif
111
112 Elf_Data *
113 elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
114 {
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
122 static int
123 elf_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)
126 {
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.");
135                         return -1;
136                 }
137                 if (predicate(scn, &shdr, data)) {
138                         *tgt_sec = scn;
139                         *tgt_shdr = shdr;
140                         return 0;
141                 }
142         }
143         return -1;
144
145 }
146
147 static int
148 inside_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
155 int
156 elf_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
163 static int
164 type_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
170 int
171 elf_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);
176 }
177
178 struct section_named_data {
179         struct ltelf *lte;
180         const char *name;
181 };
182
183 static int
184 name_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
193 int
194 elf_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
205 static int
206 need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
207 {
208         assert(data != NULL);
209         if (data->d_size < size || offset > data->d_size - size) {
210                 debug(1, "Not enough data to read %"PRId64"-byte value"
211                       " at offset %"PRId64".", size, offset);
212                 return -1;
213         }
214         return 0;
215 }
216
217 #define DEF_READER(NAME, SIZE)                                          \
218         int                                                             \
219         NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp)   \
220         {                                                               \
221                 if (!need_data(data, offset, SIZE / 8) < 0)             \
222                         return -1;                                      \
223                                                                         \
224                 if (data->d_buf == NULL) /* NODATA section */ {         \
225                         *retp = 0;                                      \
226                         return 0;                                       \
227                 }                                                       \
228                                                                         \
229                 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;                                               \
236         }
237
238 DEF_READER(elf_read_u16, 16)
239 DEF_READER(elf_read_u32, 32)
240 DEF_READER(elf_read_u64, 64)
241
242 #undef DEF_READER
243
244 int
245 open_elf(struct ltelf *lte, const char *filename)
246 {
247         lte->fd = open(filename, O_RDONLY);
248         if (lte->fd == -1)
249                 return 1;
250
251         elf_version(EV_CURRENT);
252
253 #ifdef HAVE_ELF_C_READ_MMAP
254         lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
255 #else
256         lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
257 #endif
258
259         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         }
263
264         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         }
269
270         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         }
275
276         if (1
277 #ifdef LT_ELF_MACHINE
278             && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
279                 || lte->ehdr.e_machine != LT_ELF_MACHINE)
280 #endif
281 #ifdef LT_ELF_MACHINE2
282             && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
283                 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
284 #endif
285 #ifdef LT_ELF_MACHINE3
286             && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
287                 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
288 #endif
289                 ) {
290                 fprintf(stderr,
291                         "\"%s\" is ELF from incompatible architecture\n",
292                         filename);
293                 exit(EXIT_FAILURE);
294         }
295
296         return 0;
297 }
298
299 static void
300 read_symbol_table(struct ltelf *lte, const char *filename,
301                   Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
302                   Elf_Data **datap, size_t *countp, const char **strsp)
303 {
304         *datap = elf_getdata(scn, NULL);
305         *countp = shdr->sh_size / shdr->sh_entsize;
306         if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
307             && options.static_filter != NULL) {
308                 fprintf(stderr, "Couldn't get data of section"
309                         " %s from \"%s\": %s\n",
310                         name, filename, elf_errmsg(-1));
311                 exit(EXIT_FAILURE);
312         }
313
314         scn = elf_getscn(lte->elf, shdr->sh_link);
315         GElf_Shdr shdr2;
316         if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
317                 fprintf(stderr, "Couldn't get header of section"
318                         " #%d from \"%s\": %s\n",
319                         shdr2.sh_link, filename, elf_errmsg(-1));
320                 exit(EXIT_FAILURE);
321         }
322
323         Elf_Data *data = elf_getdata(scn, NULL);
324         if (data == NULL || elf_getdata(scn, data) != NULL
325             || shdr2.sh_size != data->d_size || data->d_off) {
326                 fprintf(stderr, "Couldn't get data of section"
327                         " #%d from \"%s\": %s\n",
328                         shdr2.sh_link, filename, elf_errmsg(-1));
329                 exit(EXIT_FAILURE);
330         }
331
332         *strsp = data->d_buf;
333 }
334
335 static int
336 do_init_elf(struct ltelf *lte, const char *filename)
337 {
338         int i;
339         GElf_Addr relplt_addr = 0;
340         GElf_Addr soname_offset = 0;
341
342         debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
343         debug(1, "Reading ELF from %s...", filename);
344
345         for (i = 1; i < lte->ehdr.e_shnum; ++i) {
346                 Elf_Scn *scn;
347                 GElf_Shdr shdr;
348                 const char *name;
349
350                 scn = elf_getscn(lte->elf, i);
351                 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
352                         fprintf(stderr, "Couldn't get section #%d from"
353                                 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
354                         exit(EXIT_FAILURE);
355                 }
356
357                 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
358                 if (name == NULL) {
359                         fprintf(stderr, "Couldn't get name of section #%d from"
360                                 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
361                         exit(EXIT_FAILURE);
362                 }
363
364                 if (shdr.sh_type == SHT_SYMTAB) {
365                         read_symbol_table(lte, filename,
366                                           scn, &shdr, name, &lte->symtab,
367                                           &lte->symtab_count, &lte->strtab);
368
369                 } else if (shdr.sh_type == SHT_DYNSYM) {
370                         read_symbol_table(lte, filename,
371                                           scn, &shdr, name, &lte->dynsym,
372                                           &lte->dynsym_count, &lte->dynstr);
373
374                 } else if (shdr.sh_type == SHT_DYNAMIC) {
375                         Elf_Data *data;
376                         size_t j;
377
378                         lte->dyn_addr = shdr.sh_addr + lte->bias;
379                         lte->dyn_sz = shdr.sh_size;
380
381                         data = elf_getdata(scn, NULL);
382                         if (data == NULL || elf_getdata(scn, data) != NULL) {
383                                 fprintf(stderr, "Couldn't get .dynamic data"
384                                         " from \"%s\": %s\n",
385                                         filename, strerror(errno));
386                                 exit(EXIT_FAILURE);
387                         }
388
389                         for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
390                                 GElf_Dyn dyn;
391
392                                 if (gelf_getdyn(data, j, &dyn) == NULL) {
393                                         fprintf(stderr, "Couldn't get .dynamic"
394                                                 " data from \"%s\": %s\n",
395                                                 filename, strerror(errno));
396                                         exit(EXIT_FAILURE);
397                                 }
398                                 if (dyn.d_tag == DT_JMPREL)
399                                         relplt_addr = dyn.d_un.d_ptr;
400                                 else if (dyn.d_tag == DT_PLTRELSZ)
401                                         lte->relplt_size = dyn.d_un.d_val;
402                                 else if (dyn.d_tag == DT_SONAME)
403                                         soname_offset = dyn.d_un.d_val;
404                         }
405                 } else if (shdr.sh_type == SHT_PROGBITS
406                            || shdr.sh_type == SHT_NOBITS) {
407                         if (strcmp(name, ".plt") == 0) {
408                                 lte->plt_addr = shdr.sh_addr;
409                                 lte->plt_size = shdr.sh_size;
410                                 lte->plt_data = elf_loaddata(scn, &shdr);
411                                 if (lte->plt_data == NULL)
412                                         fprintf(stderr,
413                                                 "Can't load .plt data\n");
414                                 lte->plt_flags = shdr.sh_flags;
415                         }
416 #ifdef ARCH_SUPPORTS_OPD
417                         else if (strcmp(name, ".opd") == 0) {
418                                 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
419                                 lte->opd_size = shdr.sh_size;
420                                 lte->opd = elf_rawdata(scn, NULL);
421                         }
422 #endif
423                 }
424         }
425
426         if (lte->dynsym == NULL || lte->dynstr == NULL) {
427                 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
428                         filename);
429                 exit(EXIT_FAILURE);
430         }
431
432         if (!relplt_addr || !lte->plt_addr) {
433                 debug(1, "%s has no PLT relocations", filename);
434                 lte->relplt = NULL;
435                 lte->relplt_count = 0;
436         } else if (lte->relplt_size == 0) {
437                 debug(1, "%s has unknown PLT size", filename);
438                 lte->relplt = NULL;
439                 lte->relplt_count = 0;
440         } else {
441
442                 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
443                         Elf_Scn *scn;
444                         GElf_Shdr shdr;
445
446                         scn = elf_getscn(lte->elf, i);
447                         if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
448                                 fprintf(stderr, "Couldn't get section header"
449                                         " from \"%s\": %s\n",
450                                         filename, elf_errmsg(-1));
451                                 exit(EXIT_FAILURE);
452                         }
453                         if (shdr.sh_addr == relplt_addr
454                             && shdr.sh_size == lte->relplt_size) {
455                                 lte->relplt = elf_getdata(scn, NULL);
456                                 lte->relplt_count =
457                                     shdr.sh_size / shdr.sh_entsize;
458                                 if (lte->relplt == NULL
459                                     || elf_getdata(scn, lte->relplt) != NULL) {
460                                         fprintf(stderr, "Couldn't get .rel*.plt"
461                                                 " data from \"%s\": %s\n",
462                                                 filename, elf_errmsg(-1));
463                                         exit(EXIT_FAILURE);
464                                 }
465                                 break;
466                         }
467                 }
468
469                 if (i == lte->ehdr.e_shnum) {
470                         fprintf(stderr,
471                                 "Couldn't find .rel*.plt section in \"%s\"\n",
472                                 filename);
473                         exit(EXIT_FAILURE);
474                 }
475
476                 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
477         }
478
479         if (soname_offset != 0)
480                 lte->soname = lte->dynstr + soname_offset;
481
482         return 0;
483 }
484
485 void
486 do_close_elf(struct ltelf *lte)
487 {
488         debug(DEBUG_FUNCTION, "do_close_elf()");
489         arch_elf_destroy(lte);
490         elf_end(lte->elf);
491         close(lte->fd);
492 }
493
494 int
495 elf_get_sym_info(struct ltelf *lte, const char *filename,
496                  size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
497 {
498         int i = sym_index;
499         GElf_Rel rel;
500         void *ret;
501
502         if (lte->relplt->d_type == ELF_T_REL) {
503                 ret = gelf_getrel(lte->relplt, i, &rel);
504                 rela->r_offset = rel.r_offset;
505                 rela->r_info = rel.r_info;
506                 rela->r_addend = 0;
507         } else {
508                 ret = gelf_getrela(lte->relplt, i, rela);
509         }
510
511         if (ret == NULL
512             || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
513             || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info),
514                            sym) == NULL) {
515                 fprintf(stderr,
516                         "Couldn't get relocation from \"%s\": %s\n",
517                         filename, elf_errmsg(-1));
518                 exit(EXIT_FAILURE);
519         }
520
521         return 0;
522 }
523
524 #ifndef ARCH_HAVE_GET_SYMINFO
525 int
526 arch_get_sym_info(struct ltelf *lte, const char *filename,
527                   size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
528 {
529         return elf_get_sym_info(lte, filename, sym_index, rela, sym);
530 }
531 #endif
532
533 static void
534 mark_chain_latent(struct library_symbol *libsym)
535 {
536         for (; libsym != NULL; libsym = libsym->next) {
537                 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
538                 libsym->latent = 1;
539         }
540 }
541
542 static int
543 populate_plt(struct Process *proc, const char *filename,
544              struct ltelf *lte, struct library *lib,
545              int latent_plts)
546 {
547         size_t i;
548         for (i = 0; i < lte->relplt_count; ++i) {
549                 GElf_Rela rela;
550                 GElf_Sym sym;
551
552                 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0)
553                         continue; /* Skip this entry.  */
554
555                 char const *name = lte->dynstr + sym.st_name;
556
557                 /* If the symbol wasn't matched, reject it, unless we
558                  * need to keep latent PLT breakpoints for tracing
559                  * exports.  */
560                 int matched = filter_matches_symbol(options.plt_filter,
561                                                     name, lib);
562                 if (!matched && !latent_plts)
563                         continue;
564
565                 struct library_symbol *libsym = NULL;
566                 switch (arch_elf_add_plt_entry(proc, lte, name,
567                                                &rela, i, &libsym)) {
568                 case plt_default:
569                         if (default_elf_add_plt_entry(proc, lte, name,
570                                                       &rela, i, &libsym) < 0)
571                         /* fall-through */
572                 case plt_fail:
573                                 return -1;
574                         /* fall-through */
575                 case plt_ok:
576                         if (libsym != NULL) {
577                                 /* If we are adding those symbols just
578                                  * for tracing exports, mark them all
579                                  * latent.  */
580                                 if (!matched)
581                                         mark_chain_latent(libsym);
582                                 library_add_symbol(lib, libsym);
583                         }
584                 }
585         }
586         return 0;
587 }
588
589 /* When -x rules result in request to trace several aliases, we only
590  * want to add such symbol once.  The only way that those symbols
591  * differ in is their name, e.g. in glibc you have __GI___libc_free,
592  * __cfree, __free, __libc_free, cfree and free all defined on the
593  * same address.  So instead we keep this unique symbol struct for
594  * each address, and replace name in libsym with a shorter variant if
595  * we find it.  */
596 struct unique_symbol {
597         arch_addr_t addr;
598         struct library_symbol *libsym;
599 };
600
601 static int
602 unique_symbol_cmp(const void *key, const void *val)
603 {
604         const struct unique_symbol *sym_key = key;
605         const struct unique_symbol *sym_val = val;
606         return sym_key->addr != sym_val->addr;
607 }
608
609 static enum callback_status
610 symbol_with_address(struct library_symbol *sym, void *addrptr)
611 {
612         return sym->enter_addr == *(arch_addr_t *)addrptr
613                 ? CBS_STOP : CBS_CONT;
614 }
615
616 static int
617 populate_this_symtab(struct Process *proc, const char *filename,
618                      struct ltelf *lte, struct library *lib,
619                      Elf_Data *symtab, const char *strtab, size_t size,
620                      struct library_exported_name **names)
621 {
622         /* If a valid NAMES is passed, we pass in *NAMES a list of
623          * symbol names that this library exports.  */
624         if (names != NULL)
625                 *names = NULL;
626
627         /* Using sorted array would be arguably better, but this
628          * should be well enough for the number of symbols that we
629          * typically deal with.  */
630         size_t num_symbols = 0;
631         struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
632         if (symbols == NULL) {
633                 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
634                         strerror(errno));
635                 return -1;
636         }
637
638         GElf_Word secflags[lte->ehdr.e_shnum];
639         size_t i;
640         for (i = 1; i < lte->ehdr.e_shnum; ++i) {
641                 Elf_Scn *scn = elf_getscn(lte->elf, i);
642                 if (scn == NULL)
643                         continue;
644                 GElf_Shdr shdr;
645                 if (gelf_getshdr(scn, &shdr) == NULL)
646                         continue;
647                 secflags[i] = shdr.sh_flags;
648         }
649
650         for (i = 0; i < size; ++i) {
651                 GElf_Sym sym;
652                 if (gelf_getsym(symtab, i, &sym) == NULL) {
653                 fail:
654                         fprintf(stderr,
655                                 "couldn't get symbol #%zd from %s: %s\n",
656                                 i, filename, elf_errmsg(-1));
657                         continue;
658                 }
659
660                 /* XXX support IFUNC as well.  */
661                 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
662                     || sym.st_value == 0
663                     || sym.st_shndx == STN_UNDEF)
664                         continue;
665
666                 /* Find symbol name and snip version.  */
667                 const char *orig_name = strtab + sym.st_name;
668                 const char *version = strchr(orig_name, '@');
669                 size_t len = version != NULL ? (assert(version > orig_name),
670                                                 (size_t)(version - orig_name))
671                         : strlen(orig_name);
672                 char name[len + 1];
673                 memcpy(name, orig_name, len);
674                 name[len] = 0;
675
676                 /* If we are interested in exports, store this name.  */
677                 char *name_copy = NULL;
678                 if (names != NULL) {
679                         struct library_exported_name *export = NULL;
680                         name_copy = strdup(name);
681
682                         if (name_copy == NULL
683                             || (export = malloc(sizeof(*export))) == NULL) {
684                                 free(name_copy);
685                                 fprintf(stderr, "Couldn't store symbol %s.  "
686                                         "Tracing may be incomplete.\n", name);
687                         } else {
688                                 export->name = name_copy;
689                                 export->own_name = 1;
690                                 export->next = *names;
691                                 *names = export;
692                         }
693                 }
694
695                 /* If the symbol is not matched, skip it.  We already
696                  * stored it to export list above.  */
697                 if (!filter_matches_symbol(options.static_filter, name, lib))
698                         continue;
699
700                 arch_addr_t addr = (arch_addr_t)
701                         (uintptr_t)(sym.st_value + lte->bias);
702                 arch_addr_t naddr;
703
704                 /* On arches that support OPD, the value of typical
705                  * function symbol will be a pointer to .opd, but some
706                  * will point directly to .text.  We don't want to
707                  * translate those.  */
708                 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
709                         naddr = addr;
710                 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
711                         fprintf(stderr,
712                                 "couldn't translate address of %s@%s: %s\n",
713                                 name, lib->soname, strerror(errno));
714                         continue;
715                 }
716
717                 char *full_name;
718                 int own_full_name = 1;
719                 if (name_copy == NULL) {
720                         full_name = strdup(name);
721                         if (full_name == NULL)
722                                 goto fail;
723                 } else {
724                         full_name = name_copy;
725                         own_full_name = 0;
726                 }
727
728                 /* Look whether we already have a symbol for this
729                  * address.  If not, add this one.  */
730                 struct unique_symbol key = { naddr, NULL };
731                 struct unique_symbol *unique
732                         = lsearch(&key, symbols, &num_symbols,
733                                   sizeof(*symbols), &unique_symbol_cmp);
734
735                 if (unique->libsym == NULL) {
736                         struct library_symbol *libsym = malloc(sizeof(*libsym));
737                         if (libsym == NULL
738                             || library_symbol_init(libsym, naddr,
739                                                    full_name, own_full_name,
740                                                    LS_TOPLT_NONE) < 0) {
741                                 --num_symbols;
742                                 goto fail;
743                         }
744                         unique->libsym = libsym;
745                         unique->addr = naddr;
746
747                 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
748                         library_symbol_set_name(unique->libsym,
749                                                 full_name, own_full_name);
750
751                 } else if (own_full_name) {
752                         free(full_name);
753                 }
754         }
755
756         /* Now we do the union of this set of unique symbols with
757          * what's already in the library.  */
758         for (i = 0; i < num_symbols; ++i) {
759                 struct library_symbol *this_sym = symbols[i].libsym;
760                 assert(this_sym != NULL);
761                 struct library_symbol *other
762                         = library_each_symbol(lib, NULL, symbol_with_address,
763                                               &this_sym->enter_addr);
764                 if (other != NULL) {
765                         library_symbol_destroy(this_sym);
766                         free(this_sym);
767                         symbols[i].libsym = NULL;
768                 }
769         }
770
771         for (i = 0; i < num_symbols; ++i)
772                 if (symbols[i].libsym != NULL)
773                         library_add_symbol(lib, symbols[i].libsym);
774
775         free(symbols);
776         return 0;
777 }
778
779 static int
780 populate_symtab(struct Process *proc, const char *filename,
781                 struct ltelf *lte, struct library *lib,
782                 int symtabs, int exports)
783 {
784         int status;
785         if (symtabs && lte->symtab != NULL && lte->strtab != NULL
786             && (status = populate_this_symtab(proc, filename, lte, lib,
787                                               lte->symtab, lte->strtab,
788                                               lte->symtab_count, NULL)) < 0)
789                 return status;
790
791         /* Check whether we want to trace symbols implemented by this
792          * library (-l).  */
793         struct library_exported_name **names = NULL;
794         if (exports) {
795                 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
796                 names = &lib->exported_names;
797         }
798
799         return populate_this_symtab(proc, filename, lte, lib,
800                                     lte->dynsym, lte->dynstr,
801                                     lte->dynsym_count, names);
802 }
803
804 static int
805 read_module(struct library *lib, struct Process *proc,
806             const char *filename, GElf_Addr bias, int main)
807 {
808         struct ltelf lte = {};
809         if (open_elf(&lte, filename) < 0)
810                 return -1;
811
812         /* XXX When we abstract ABI into a module, this should instead
813          * become something like
814          *
815          *    proc->abi = arch_get_abi(lte.ehdr);
816          *
817          * The code in open_elf needs to be replaced by this logic.
818          * Be warned that libltrace.c calls open_elf as well to
819          * determine whether ABI is supported.  This is to get
820          * reasonable error messages when trying to run 64-bit binary
821          * with 32-bit ltrace.  It is desirable to preserve this.  */
822         proc->e_machine = lte.ehdr.e_machine;
823         proc->e_class = lte.ehdr.e_ident[EI_CLASS];
824         get_arch_dep(proc);
825
826         /* Find out the base address.  For PIE main binaries we look
827          * into auxv, otherwise we scan phdrs.  */
828         if (main && lte.ehdr.e_type == ET_DYN) {
829                 arch_addr_t entry;
830                 if (process_get_entry(proc, &entry, NULL) < 0) {
831                         fprintf(stderr, "Couldn't find entry of PIE %s\n",
832                                 filename);
833                         return -1;
834                 }
835                 /* XXX The double cast should be removed when
836                  * arch_addr_t becomes integral type.  */
837                 lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
838                 lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
839
840         } else {
841                 GElf_Phdr phdr;
842                 size_t i;
843                 for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
844                         if (phdr.p_type == PT_LOAD) {
845                                 lte.base_addr = phdr.p_vaddr + bias;
846                                 break;
847                         }
848                 }
849
850                 lte.bias = bias;
851                 lte.entry_addr = lte.ehdr.e_entry + lte.bias;
852
853                 if (lte.base_addr == 0) {
854                         fprintf(stderr,
855                                 "Couldn't determine base address of %s\n",
856                                 filename);
857                         return -1;
858                 }
859         }
860
861         if (do_init_elf(&lte, filename) < 0)
862                 return -1;
863
864         if (arch_elf_init(&lte, lib) < 0) {
865                 fprintf(stderr, "Backend initialization failed.\n");
866                 return -1;
867         }
868
869         int status = 0;
870         if (lib == NULL)
871                 goto fail;
872
873         /* Note that we set soname and pathname as soon as they are
874          * allocated, so in case of further errors, this get released
875          * when LIB is release, which should happen in the caller when
876          * we return error.  */
877
878         if (lib->pathname == NULL) {
879                 char *pathname = strdup(filename);
880                 if (pathname == NULL)
881                         goto fail;
882                 library_set_pathname(lib, pathname, 1);
883         }
884
885         if (lte.soname != NULL) {
886                 char *soname = strdup(lte.soname);
887                 if (soname == NULL)
888                         goto fail;
889                 library_set_soname(lib, soname, 1);
890         } else {
891                 const char *soname = rindex(lib->pathname, '/') + 1;
892                 if (soname == NULL)
893                         soname = lib->pathname;
894                 library_set_soname(lib, soname, 0);
895         }
896
897         /* XXX The double cast should be removed when
898          * arch_addr_t becomes integral type.  */
899         arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
900         if (arch_translate_address(&lte, entry, &entry) < 0)
901                 goto fail;
902
903         /* XXX The double cast should be removed when
904          * arch_addr_t becomes integral type.  */
905         lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
906         lib->entry = entry;
907         /* XXX The double cast should be removed when
908          * arch_addr_t becomes integral type.  */
909         lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
910
911         /* There are two reasons that we need to inspect symbol tables
912          * or populate PLT entries.  Either the user requested
913          * corresponding tracing features (respectively -x and -e), or
914          * they requested tracing exported symbols (-l).
915          *
916          * In the latter case we need to keep even those PLT slots
917          * that are not requested by -e (but we keep them latent).  We
918          * also need to inspect .dynsym to find what exports this
919          * library provide, to turn on existing latent PLT
920          * entries.  */
921
922         int plts = filter_matches_library(options.plt_filter, lib);
923         if ((plts || options.export_filter != NULL)
924             && populate_plt(proc, filename, &lte, lib,
925                             options.export_filter != NULL) < 0)
926                 goto fail;
927
928         int exports = filter_matches_library(options.export_filter, lib);
929         int symtabs = filter_matches_library(options.static_filter, lib);
930         if ((symtabs || exports)
931             && populate_symtab(proc, filename, &lte, lib,
932                                symtabs, exports) < 0)
933                 goto fail;
934
935 done:
936         do_close_elf(&lte);
937         return status;
938
939 fail:
940         status = -1;
941         goto done;
942 }
943
944 int
945 ltelf_read_library(struct library *lib, struct Process *proc,
946                    const char *filename, GElf_Addr bias)
947 {
948         return read_module(lib, proc, filename, bias, 0);
949 }
950
951
952 struct library *
953 ltelf_read_main_binary(struct Process *proc, const char *path)
954 {
955         struct library *lib = malloc(sizeof(*lib));
956         if (lib == NULL)
957                 return NULL;
958         library_init(lib, LT_LIBTYPE_MAIN);
959         library_set_pathname(lib, path, 0);
960
961         /* There is a race between running the process and reading its
962          * binary for internal consumption.  So open the binary from
963          * the /proc filesystem.  XXX Note that there is similar race
964          * for libraries, but there we don't have a nice answer like
965          * that.  Presumably we could read the DSOs from the process
966          * memory image, but that's not currently done.  */
967         char *fname = pid2name(proc->pid);
968         if (fname == NULL)
969                 return NULL;
970         if (read_module(lib, proc, fname, 0, 1) < 0) {
971                 library_destroy(lib);
972                 free(lib);
973                 return NULL;
974         }
975         free(fname);
976
977         return lib;
978 }