blob: 7cd1369993c175c586c64b084aa5f84c18e2a530 [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2006,2010,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch
5 * Copyright (C) 2001,2004,2007,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
Juan Cespedes1cd999a2001-07-03 00:46:04 +020024#ifndef LTRACE_ELF_H
25#define LTRACE_ELF_H
26
Juan Cespedesd914a202004-11-10 00:15:33 +010027#include <gelf.h>
28#include <stdlib.h>
Petr Machata50b96032012-03-24 04:56:52 +010029#include "sysdep.h"
Petr Machatae67635d2012-03-21 03:37:39 +010030
Petr Machata2b46cfc2012-02-18 11:17:29 +010031struct Process;
32struct library;
Petr Machata16fabd02012-04-20 02:37:40 +020033struct library_symbol;
Petr Machata2b46cfc2012-02-18 11:17:29 +010034
35/* XXX Ok, the original idea was to separate the low-level ELF data
36 * from the abstract "struct library" object, but we use some of the
37 * following extensively in the back end. Not all though. So what we
38 * use should be move to struct library, and the rest of this
39 * structure maybe could be safely hidden in .c. How to integrate the
40 * arch-specific bits into struct library is unclear as of now. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010041struct ltelf {
42 int fd;
43 Elf *elf;
44 GElf_Ehdr ehdr;
45 Elf_Data *dynsym;
46 size_t dynsym_count;
47 const char *dynstr;
48 GElf_Addr plt_addr;
Petr Machata18c801c2012-04-07 01:24:08 +020049 GElf_Word plt_flags;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010050 size_t plt_size;
51 Elf_Data *relplt;
Petr Machatae67635d2012-03-21 03:37:39 +010052 Elf_Data *plt_data;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010053 size_t relplt_count;
54 Elf_Data *symtab;
55 const char *strtab;
Petr Machata2b46cfc2012-02-18 11:17:29 +010056 const char *soname;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010057 size_t symtab_count;
58 Elf_Data *opd;
59 GElf_Addr *opd_addr;
60 size_t opd_size;
Joe Damato87f4f582010-11-08 15:47:36 -080061 GElf_Addr dyn_addr;
62 size_t dyn_sz;
Petr Machatae67635d2012-03-21 03:37:39 +010063 size_t relplt_size;
Petr Machata29add4f2012-02-18 16:38:05 +010064 GElf_Addr bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +010065 GElf_Addr entry_addr;
Petr Machata29add4f2012-02-18 16:38:05 +010066 GElf_Addr base_addr;
Petr Machatae67635d2012-03-21 03:37:39 +010067 struct arch_ltelf_data arch;
Juan Cespedes1cd999a2001-07-03 00:46:04 +020068};
69
Petr Machata2b46cfc2012-02-18 11:17:29 +010070int open_elf(struct ltelf *lte, const char *filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020071
Petr Machata2b46cfc2012-02-18 11:17:29 +010072/* XXX is it possible to put breakpoints in VDSO and VSYSCALL
73 * pseudo-libraries? For now we assume that all libraries can be
74 * opened via a filesystem. BASE is ignored for ET_EXEC files. */
Petr Machatab5f80ac2012-04-04 01:46:18 +020075int ltelf_read_library(struct library *lib, struct Process *proc,
76 const char *filename, GElf_Addr bias);
Juan Cespedesd914a202004-11-10 00:15:33 +010077
Petr Machata2b46cfc2012-02-18 11:17:29 +010078/* Create a library object representing the main binary. The entry
79 * point address is stored to *ENTRYP. */
80struct library *ltelf_read_main_binary(struct Process *proc, const char *path);
Juan Cespedes96935a91997-08-09 23:45:39 +020081
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +020082/* The base implementation of backend.h (arch_get_sym_info).
83 * See backend.h for details. */
84int elf_get_sym_info(struct ltelf *lte, const char *filename,
85 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym);
86
Petr Machatae67635d2012-03-21 03:37:39 +010087Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr);
88int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
89 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machataffd5aab2012-03-24 02:03:33 +010090int elf_get_section_type(struct ltelf *lte, GElf_Word type,
91 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machata5b3e26a2012-04-30 20:53:22 +020092int elf_get_section_named(struct ltelf *lte, const char *name,
93 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machatae67635d2012-03-21 03:37:39 +010094
95/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET,
96 * and store it in *RETP. Returns 0 on success or a negative value if
97 * there's not enough data. */
Petr Machata3a01cd72012-04-30 20:50:20 +020098int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp);
99int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp);
100int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
Petr Machatae67635d2012-03-21 03:37:39 +0100101
Zachary T Welch3ba522f2010-12-14 15:12:47 -0800102#if __WORDSIZE == 32
103#define PRI_ELF_ADDR PRIx32
104#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x)
105#else
106#define PRI_ELF_ADDR PRIx64
107#define GELF_ADDR_CAST(x) (void *)(x)
108#endif
109
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200110#endif