blob: db4ffe9ee8a959cfe3c810c1be00dfc4a895eb4a [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
2 * This file is part of ltrace.
Petr Machata99b19eb2013-02-05 01:48:21 +01003 * Copyright (C) 2006,2010,2012,2013 Petr Machata, Red Hat Inc.
Petr Machatae99af272012-10-26 00:29:52 +02004 * 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 Machata7a29f9e2013-10-14 20:04:09 +020029#include <callback.h>
Petr Machatae67635d2012-03-21 03:37:39 +010030
Petr Machata929bd572012-12-17 03:20:34 +010031#include "forward.h"
32#include "sysdep.h"
Petr Machata673ff512013-10-25 23:45:39 +020033#include "vect.h"
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;
Petr Machatae67635d2012-03-21 03:37:39 +010051 Elf_Data *plt_data;
Petr Machata673ff512013-10-25 23:45:39 +020052
53 /* Vector of GElf_Rela with PLT relocations. */
54 struct vect plt_relocs;
55
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056 Elf_Data *symtab;
57 const char *strtab;
Petr Machata2b46cfc2012-02-18 11:17:29 +010058 const char *soname;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010059 size_t symtab_count;
60 Elf_Data *opd;
61 GElf_Addr *opd_addr;
62 size_t opd_size;
Joe Damato87f4f582010-11-08 15:47:36 -080063 GElf_Addr dyn_addr;
64 size_t dyn_sz;
Petr Machata29add4f2012-02-18 16:38:05 +010065 GElf_Addr bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +010066 GElf_Addr entry_addr;
Petr Machata29add4f2012-02-18 16:38:05 +010067 GElf_Addr base_addr;
Petr Machatae67635d2012-03-21 03:37:39 +010068 struct arch_ltelf_data arch;
Juan Cespedes1cd999a2001-07-03 00:46:04 +020069};
70
Petr Machata673ff512013-10-25 23:45:39 +020071int ltelf_init(struct ltelf *lte, const char *filename);
72void ltelf_destroy(struct ltelf *lte);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020073
Petr Machata2b46cfc2012-02-18 11:17:29 +010074/* XXX is it possible to put breakpoints in VDSO and VSYSCALL
75 * pseudo-libraries? For now we assume that all libraries can be
76 * opened via a filesystem. BASE is ignored for ET_EXEC files. */
Petr Machata929bd572012-12-17 03:20:34 +010077int ltelf_read_library(struct library *lib, struct process *proc,
Petr Machatab5f80ac2012-04-04 01:46:18 +020078 const char *filename, GElf_Addr bias);
Juan Cespedesd914a202004-11-10 00:15:33 +010079
Petr Machata2b46cfc2012-02-18 11:17:29 +010080/* Create a library object representing the main binary. The entry
81 * point address is stored to *ENTRYP. */
Petr Machata929bd572012-12-17 03:20:34 +010082struct library *ltelf_read_main_binary(struct process *proc, const char *path);
Juan Cespedes96935a91997-08-09 23:45:39 +020083
Petr Machata97d13662013-11-05 01:46:30 +010084/* This is called for every PLT relocation R in ELF file LTE, that
85 * ltrace is about to add to a library constructed in process PROC.
86 * The corresponding PLT entry is for symbol called NAME, and it's
87 * I-th relocation in the file. *RET shall be initialized and
88 * symbol(s) corresponding to the given PLT entry will be added to the
89 * front. Returns 0 for success, or a negative value for failures.
90 *
91 * This calls os_elf_add_plt_entry and arch_elf_add_plt_entry in the
92 * background (see backend.h for documentation). The arch callback is
93 * called first. If it returns PLT_DEFAULT, the os callback is called
94 * next. If that returns PLT_DEFAULT, default_elf_add_plt_entry is
95 * called. */
96int elf_add_plt_entry(struct process *proc, struct ltelf *lte,
97 const char *name, GElf_Rela *rela, size_t idx,
98 struct library_symbol **ret);
99
Petr Machata6db61f52012-10-31 03:27:05 +0100100/* Create a default PLT entry. This can be used instead (or in
Petr Machataade3b972012-12-17 03:45:30 +0100101 * addition to) returning PLT_DEFAULT from arch_elf_add_plt_entry.
Petr Machata6db61f52012-10-31 03:27:05 +0100102 * RET shall be initialized, the created symbol will be added to the
103 * beginning of the linked list at *RET. This function doesn't add
104 * the symbol to LTE. arch_elf_add_plt_entry has the chance to adjust
Petr Machataade3b972012-12-17 03:45:30 +0100105 * symbol internals to its liking, and then return either PLT_DEFAULT
106 * or PLT_OK. */
Petr Machata929bd572012-12-17 03:20:34 +0100107int default_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
Petr Machata6db61f52012-10-31 03:27:05 +0100108 const char *a_name, GElf_Rela *rela, size_t ndx,
109 struct library_symbol **ret);
110
Petr Machatae67635d2012-03-21 03:37:39 +0100111Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr);
Petr Machata99b19eb2013-02-05 01:48:21 +0100112
113/* The following three look for sections based on various criteria.
114 * They return 0 if there was no error, or a negative value if there
115 * was. If the section was found, it is returned in *TGT_SEC, and the
Petr Machataa9a12bd2013-10-14 20:09:31 +0200116 * header is stored to TGT_SHDR. If it wasn't found, *TGT_SEC is set
Petr Machata99b19eb2013-02-05 01:48:21 +0100117 * to NULL. */
Petr Machatae67635d2012-03-21 03:37:39 +0100118int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
119 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machataffd5aab2012-03-24 02:03:33 +0100120int elf_get_section_type(struct ltelf *lte, GElf_Word type,
121 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machata5b3e26a2012-04-30 20:53:22 +0200122int elf_get_section_named(struct ltelf *lte, const char *name,
123 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machatae67635d2012-03-21 03:37:39 +0100124
Petr Machata7a29f9e2013-10-14 20:04:09 +0200125/* Iterate through all symbols in LTE. See callback.h for notes on
126 * iteration interfaces. START_AFTER is 0 in initial call. */
127struct elf_each_symbol_t {
128 unsigned restart;
129 int status;
130} elf_each_symbol(struct ltelf *lte, unsigned start_after,
131 enum callback_status (*cb)(GElf_Sym *symbol,
132 const char *name,
133 void *data),
134 void *data);
135
Petr Machata0ed53992013-11-04 17:01:43 +0100136/* Read relocations from relocation section SCN with header SHDR and
137 * add them to RELA_VEC, which is a vector of GElf_Rela. Return 0 on
138 * success, or a negative value on failure. */
139int elf_read_relocs(struct ltelf *lte, Elf_Scn *scn, GElf_Shdr *shdr,
140 struct vect *rela_vec);
141
Petr Machata4f2f66e2013-11-04 22:45:34 -0500142/* Read a given DT_ TAG from LTE. Value is returned in *VALUEP.
143 * Returns 0 on success or a negative value on failure. */
144int elf_load_dynamic_entry(struct ltelf *lte, int tag, GElf_Addr *valuep);
145
Petr Machata3c636fb2013-02-05 01:48:54 +0100146/* Read, respectively, 1, 2, 4, or 8 bytes from Elf data at given
147 * OFFSET, and store it in *RETP. Returns 0 on success or a negative
148 * value if there's not enough data. */
149int elf_read_u8(Elf_Data *data, GElf_Xword offset, uint8_t *retp);
Petr Machata3a01cd72012-04-30 20:50:20 +0200150int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp);
151int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp);
152int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
Petr Machatae67635d2012-03-21 03:37:39 +0100153
Petr Machata184779e2013-02-05 01:52:05 +0100154/* Read at most 64-bit quantity recorded in an ULEB128 variable-length
155 * encoding. */
156int elf_read_uleb128(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
157
Petr Machata439ab5b2013-02-05 01:50:28 +0100158/* These are same as above, but update *OFFSET with the width
159 * of read datum. */
160int elf_read_next_u8(Elf_Data *data, GElf_Xword *offset, uint8_t *retp);
161int elf_read_next_u16(Elf_Data *data, GElf_Xword *offset, uint16_t *retp);
162int elf_read_next_u32(Elf_Data *data, GElf_Xword *offset, uint32_t *retp);
163int elf_read_next_u64(Elf_Data *data, GElf_Xword *offset, uint64_t *retp);
Petr Machata184779e2013-02-05 01:52:05 +0100164int elf_read_next_uleb128(Elf_Data *data, GElf_Xword *offset, uint64_t *retp);
Petr Machata439ab5b2013-02-05 01:50:28 +0100165
Petr Machata5c371712013-02-05 01:52:37 +0100166/* Return whether there's AMOUNT more bytes after OFFSET in DATA. */
167int elf_can_read_next(Elf_Data *data, GElf_Xword offset, GElf_Xword amount);
168
Zachary T Welch3ba522f2010-12-14 15:12:47 -0800169#if __WORDSIZE == 32
170#define PRI_ELF_ADDR PRIx32
171#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x)
172#else
173#define PRI_ELF_ADDR PRIx64
174#define GELF_ADDR_CAST(x) (void *)(x)
175#endif
176
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200177#endif