Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
Petr Machata | 99b19eb | 2013-02-05 01:48:21 +0100 | [diff] [blame] | 3 | * Copyright (C) 2006,2010,2012,2013 Petr Machata, Red Hat Inc. |
Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 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 Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 24 | #ifndef LTRACE_ELF_H |
| 25 | #define LTRACE_ELF_H |
| 26 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 27 | #include <gelf.h> |
| 28 | #include <stdlib.h> |
Petr Machata | 7a29f9e | 2013-10-14 20:04:09 +0200 | [diff] [blame^] | 29 | #include <callback.h> |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 30 | |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 31 | #include "forward.h" |
| 32 | #include "sysdep.h" |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 33 | |
| 34 | /* XXX Ok, the original idea was to separate the low-level ELF data |
| 35 | * from the abstract "struct library" object, but we use some of the |
| 36 | * following extensively in the back end. Not all though. So what we |
| 37 | * use should be move to struct library, and the rest of this |
| 38 | * structure maybe could be safely hidden in .c. How to integrate the |
| 39 | * arch-specific bits into struct library is unclear as of now. */ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 40 | struct ltelf { |
| 41 | int fd; |
| 42 | Elf *elf; |
| 43 | GElf_Ehdr ehdr; |
| 44 | Elf_Data *dynsym; |
| 45 | size_t dynsym_count; |
| 46 | const char *dynstr; |
| 47 | GElf_Addr plt_addr; |
Petr Machata | 18c801c | 2012-04-07 01:24:08 +0200 | [diff] [blame] | 48 | GElf_Word plt_flags; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 49 | size_t plt_size; |
| 50 | Elf_Data *relplt; |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 51 | Elf_Data *plt_data; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 52 | size_t relplt_count; |
| 53 | Elf_Data *symtab; |
| 54 | const char *strtab; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 55 | const char *soname; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | size_t symtab_count; |
| 57 | Elf_Data *opd; |
| 58 | GElf_Addr *opd_addr; |
| 59 | size_t opd_size; |
Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 60 | GElf_Addr dyn_addr; |
| 61 | size_t dyn_sz; |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 62 | size_t relplt_size; |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 63 | GElf_Addr bias; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 64 | GElf_Addr entry_addr; |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 65 | GElf_Addr base_addr; |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 66 | struct arch_ltelf_data arch; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 69 | int open_elf(struct ltelf *lte, const char *filename); |
Petr Machata | 588850f | 2012-10-26 22:26:54 +0200 | [diff] [blame] | 70 | void do_close_elf(struct ltelf *lte); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 71 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 72 | /* 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 Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 75 | int ltelf_read_library(struct library *lib, struct process *proc, |
Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 76 | const char *filename, GElf_Addr bias); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 77 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 78 | /* Create a library object representing the main binary. The entry |
| 79 | * point address is stored to *ENTRYP. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 80 | struct library *ltelf_read_main_binary(struct process *proc, const char *path); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 81 | |
Petr Machata | 6db61f5 | 2012-10-31 03:27:05 +0100 | [diff] [blame] | 82 | /* Create a default PLT entry. This can be used instead (or in |
Petr Machata | ade3b97 | 2012-12-17 03:45:30 +0100 | [diff] [blame] | 83 | * addition to) returning PLT_DEFAULT from arch_elf_add_plt_entry. |
Petr Machata | 6db61f5 | 2012-10-31 03:27:05 +0100 | [diff] [blame] | 84 | * RET shall be initialized, the created symbol will be added to the |
| 85 | * beginning of the linked list at *RET. This function doesn't add |
| 86 | * the symbol to LTE. arch_elf_add_plt_entry has the chance to adjust |
Petr Machata | ade3b97 | 2012-12-17 03:45:30 +0100 | [diff] [blame] | 87 | * symbol internals to its liking, and then return either PLT_DEFAULT |
| 88 | * or PLT_OK. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 89 | int default_elf_add_plt_entry(struct process *proc, struct ltelf *lte, |
Petr Machata | 6db61f5 | 2012-10-31 03:27:05 +0100 | [diff] [blame] | 90 | const char *a_name, GElf_Rela *rela, size_t ndx, |
| 91 | struct library_symbol **ret); |
| 92 | |
Edgar E. Iglesias | 35b5be7 | 2012-10-09 14:22:31 +0200 | [diff] [blame] | 93 | /* The base implementation of backend.h (arch_get_sym_info). |
| 94 | * See backend.h for details. */ |
Petr Machata | 552d75e | 2013-10-14 17:04:58 +0200 | [diff] [blame] | 95 | int elf_get_sym_info(struct ltelf *lte, const char *filename, size_t sym_index, |
| 96 | GElf_Rela *rela, GElf_Sym *sym); |
Edgar E. Iglesias | 35b5be7 | 2012-10-09 14:22:31 +0200 | [diff] [blame] | 97 | |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 98 | Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr); |
Petr Machata | 99b19eb | 2013-02-05 01:48:21 +0100 | [diff] [blame] | 99 | |
| 100 | /* The following three look for sections based on various criteria. |
| 101 | * They return 0 if there was no error, or a negative value if there |
| 102 | * was. If the section was found, it is returned in *TGT_SEC, and the |
Petr Machata | a9a12bd | 2013-10-14 20:09:31 +0200 | [diff] [blame] | 103 | * header is stored to TGT_SHDR. If it wasn't found, *TGT_SEC is set |
Petr Machata | 99b19eb | 2013-02-05 01:48:21 +0100 | [diff] [blame] | 104 | * to NULL. */ |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 105 | int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, |
| 106 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); |
Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 107 | int elf_get_section_type(struct ltelf *lte, GElf_Word type, |
| 108 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); |
Petr Machata | 5b3e26a | 2012-04-30 20:53:22 +0200 | [diff] [blame] | 109 | int elf_get_section_named(struct ltelf *lte, const char *name, |
| 110 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 111 | |
Petr Machata | 7a29f9e | 2013-10-14 20:04:09 +0200 | [diff] [blame^] | 112 | /* Iterate through all symbols in LTE. See callback.h for notes on |
| 113 | * iteration interfaces. START_AFTER is 0 in initial call. */ |
| 114 | struct elf_each_symbol_t { |
| 115 | unsigned restart; |
| 116 | int status; |
| 117 | } elf_each_symbol(struct ltelf *lte, unsigned start_after, |
| 118 | enum callback_status (*cb)(GElf_Sym *symbol, |
| 119 | const char *name, |
| 120 | void *data), |
| 121 | void *data); |
| 122 | |
Petr Machata | 3c636fb | 2013-02-05 01:48:54 +0100 | [diff] [blame] | 123 | /* Read, respectively, 1, 2, 4, or 8 bytes from Elf data at given |
| 124 | * OFFSET, and store it in *RETP. Returns 0 on success or a negative |
| 125 | * value if there's not enough data. */ |
| 126 | int elf_read_u8(Elf_Data *data, GElf_Xword offset, uint8_t *retp); |
Petr Machata | 3a01cd7 | 2012-04-30 20:50:20 +0200 | [diff] [blame] | 127 | int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp); |
| 128 | int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp); |
| 129 | int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp); |
Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 130 | |
Petr Machata | 184779e | 2013-02-05 01:52:05 +0100 | [diff] [blame] | 131 | /* Read at most 64-bit quantity recorded in an ULEB128 variable-length |
| 132 | * encoding. */ |
| 133 | int elf_read_uleb128(Elf_Data *data, GElf_Xword offset, uint64_t *retp); |
| 134 | |
Petr Machata | 439ab5b | 2013-02-05 01:50:28 +0100 | [diff] [blame] | 135 | /* These are same as above, but update *OFFSET with the width |
| 136 | * of read datum. */ |
| 137 | int elf_read_next_u8(Elf_Data *data, GElf_Xword *offset, uint8_t *retp); |
| 138 | int elf_read_next_u16(Elf_Data *data, GElf_Xword *offset, uint16_t *retp); |
| 139 | int elf_read_next_u32(Elf_Data *data, GElf_Xword *offset, uint32_t *retp); |
| 140 | int elf_read_next_u64(Elf_Data *data, GElf_Xword *offset, uint64_t *retp); |
Petr Machata | 184779e | 2013-02-05 01:52:05 +0100 | [diff] [blame] | 141 | int elf_read_next_uleb128(Elf_Data *data, GElf_Xword *offset, uint64_t *retp); |
Petr Machata | 439ab5b | 2013-02-05 01:50:28 +0100 | [diff] [blame] | 142 | |
Petr Machata | 5c37171 | 2013-02-05 01:52:37 +0100 | [diff] [blame] | 143 | /* Return whether there's AMOUNT more bytes after OFFSET in DATA. */ |
| 144 | int elf_can_read_next(Elf_Data *data, GElf_Xword offset, GElf_Xword amount); |
| 145 | |
Zachary T Welch | 3ba522f | 2010-12-14 15:12:47 -0800 | [diff] [blame] | 146 | #if __WORDSIZE == 32 |
| 147 | #define PRI_ELF_ADDR PRIx32 |
| 148 | #define GELF_ADDR_CAST(x) (void *)(uint32_t)(x) |
| 149 | #else |
| 150 | #define PRI_ELF_ADDR PRIx64 |
| 151 | #define GELF_ADDR_CAST(x) (void *)(x) |
| 152 | #endif |
| 153 | |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 154 | #endif |