| Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 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 | |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 28 | #include "config.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 29 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 30 | #include <assert.h> |
| Andrey Zonov | 9d878c9 | 2012-08-05 00:19:51 +0400 | [diff] [blame] | 31 | #ifdef __linux__ |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 32 | #include <endian.h> |
| Andrey Zonov | 9d878c9 | 2012-08-05 00:19:51 +0400 | [diff] [blame] | 33 | #endif |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 34 | #include <errno.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 35 | #include <fcntl.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 36 | #include <gelf.h> |
| Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 37 | #include <inttypes.h> |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 38 | #include <search.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 39 | #include <stdint.h> |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 40 | #include <stdio.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 41 | #include <stdlib.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 42 | #include <string.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 43 | #include <unistd.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 44 | |
| Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 45 | #include "backend.h" |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 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" |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 52 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 53 | #ifndef ARCH_HAVE_LTELF_DATA |
| 54 | int |
| Petr Machata | e0615ab | 2012-04-17 05:17:48 +0200 | [diff] [blame] | 55 | arch_elf_init(struct ltelf *lte, struct library *lib) |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 56 | { |
| 57 | return 0; |
| 58 | } |
| Petr Machata | c67a6e6 | 2012-03-28 02:39:49 +0200 | [diff] [blame] | 59 | |
| 60 | void |
| 61 | arch_elf_destroy(struct ltelf *lte) |
| 62 | { |
| 63 | } |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 64 | #endif |
| 65 | |
| Petr Machata | 6db61f5 | 2012-10-31 03:27:05 +0100 | [diff] [blame] | 66 | int |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 67 | default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte, |
| Petr Machata | 1be2291 | 2012-03-27 03:11:33 +0200 | [diff] [blame] | 68 | const char *a_name, GElf_Rela *rela, size_t ndx, |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 69 | struct library_symbol **ret) |
| 70 | { |
| 71 | char *name = strdup(a_name); |
| 72 | if (name == NULL) { |
| Petr Machata | 6db61f5 | 2012-10-31 03:27:05 +0100 | [diff] [blame] | 73 | fail_message: |
| 74 | fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n", |
| 75 | strerror(errno)); |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 76 | fail: |
| 77 | free(name); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| Petr Machata | 1be2291 | 2012-03-27 03:11:33 +0200 | [diff] [blame] | 81 | GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela); |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 82 | |
| 83 | struct library_symbol *libsym = malloc(sizeof(*libsym)); |
| 84 | if (libsym == NULL) |
| Petr Machata | 6db61f5 | 2012-10-31 03:27:05 +0100 | [diff] [blame] | 85 | goto fail_message; |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 86 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 87 | /* XXX The double cast should be removed when |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 88 | * arch_addr_t becomes integral type. */ |
| 89 | arch_addr_t taddr = (arch_addr_t) |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 90 | (uintptr_t)(addr + lte->bias); |
| Petr Machata | bb79060 | 2012-03-25 01:41:59 +0100 | [diff] [blame] | 91 | |
| Petr Machata | e8d9076 | 2012-04-15 04:28:31 +0200 | [diff] [blame] | 92 | if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) { |
| 93 | free(libsym); |
| 94 | goto fail; |
| 95 | } |
| 96 | |
| Petr Machata | 0fa7904 | 2012-09-27 23:33:44 +0200 | [diff] [blame] | 97 | libsym->next = *ret; |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 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, |
| Petr Machata | 1be2291 | 2012-03-27 03:11:33 +0200 | [diff] [blame] | 105 | const char *a_name, GElf_Rela *rela, size_t ndx, |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 106 | struct library_symbol **ret) |
| 107 | { |
| 108 | return plt_default; |
| 109 | } |
| 110 | #endif |
| 111 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 112 | Elf_Data * |
| 113 | elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr) |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 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 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 122 | static int |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 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) |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 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."); |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 137 | if (predicate(scn, &shdr, data)) { |
| 138 | *tgt_sec = scn; |
| 139 | *tgt_shdr = shdr; |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 140 | return 0; |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 141 | } |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 142 | } |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 143 | return -1; |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 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); |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| Petr Machata | 5b3e26a | 2012-04-30 20:53:22 +0200 | [diff] [blame] | 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 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 205 | static int |
| Petr Machata | 3a01cd7 | 2012-04-30 20:50:20 +0200 | [diff] [blame] | 206 | need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size) |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 207 | { |
| 208 | assert(data != NULL); |
| 209 | if (data->d_size < size || offset > data->d_size - size) { |
| Petr Machata | a82d322 | 2012-05-01 01:04:27 +0200 | [diff] [blame] | 210 | debug(1, "Not enough data to read %"PRId64"-byte value" |
| 211 | " at offset %"PRId64".", size, offset); |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 217 | #define DEF_READER(NAME, SIZE) \ |
| 218 | int \ |
| Petr Machata | 3a01cd7 | 2012-04-30 20:50:20 +0200 | [diff] [blame] | 219 | NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \ |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 220 | { \ |
| 221 | if (!need_data(data, offset, SIZE / 8) < 0) \ |
| 222 | return -1; \ |
| 223 | \ |
| Petr Machata | 6d8ccb2 | 2012-03-27 03:11:57 +0200 | [diff] [blame] | 224 | if (data->d_buf == NULL) /* NODATA section */ { \ |
| 225 | *retp = 0; \ |
| 226 | return 0; \ |
| 227 | } \ |
| 228 | \ |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 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; \ |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 238 | DEF_READER(elf_read_u16, 16) |
| 239 | DEF_READER(elf_read_u32, 32) |
| 240 | DEF_READER(elf_read_u64, 64) |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 241 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 242 | #undef DEF_READER |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 243 | |
| Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 244 | int |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 245 | open_elf(struct ltelf *lte, const char *filename) |
| 246 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 247 | lte->fd = open(filename, O_RDONLY); |
| 248 | if (lte->fd == -1) |
| Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 249 | return 1; |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 250 | |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 251 | elf_version(EV_CURRENT); |
| 252 | |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 253 | #ifdef HAVE_ELF_C_READ_MMAP |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 254 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 255 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 256 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 257 | #endif |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 258 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 263 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 264 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) { |
| 265 | fprintf(stderr, "can't read ELF header of \"%s\": %s\n", |
| 266 | filename, elf_errmsg(-1)); |
| 267 | exit(EXIT_FAILURE); |
| 268 | } |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 269 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 275 | |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 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 |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 281 | #ifdef LT_ELF_MACHINE2 |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 282 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 283 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 284 | #endif |
| 285 | #ifdef LT_ELF_MACHINE3 |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 286 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 287 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 288 | #endif |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 289 | ) { |
| 290 | fprintf(stderr, |
| 291 | "\"%s\" is ELF from incompatible architecture\n", |
| 292 | filename); |
| 293 | exit(EXIT_FAILURE); |
| 294 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 295 | |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 335 | static int |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 336 | do_init_elf(struct ltelf *lte, const char *filename) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 337 | { |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 338 | int i; |
| 339 | GElf_Addr relplt_addr = 0; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 340 | GElf_Addr soname_offset = 0; |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 341 | |
| 342 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
| 343 | debug(1, "Reading ELF from %s...", filename); |
| 344 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 345 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 346 | Elf_Scn *scn; |
| 347 | GElf_Shdr shdr; |
| 348 | const char *name; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 349 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 350 | scn = elf_getscn(lte->elf, i); |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 356 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 357 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 363 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 364 | if (shdr.sh_type == SHT_SYMTAB) { |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 365 | read_symbol_table(lte, filename, |
| 366 | scn, &shdr, name, <e->symtab, |
| 367 | <e->symtab_count, <e->strtab); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 368 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 369 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 370 | read_symbol_table(lte, filename, |
| 371 | scn, &shdr, name, <e->dynsym, |
| 372 | <e->dynsym_count, <e->dynstr); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 373 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 374 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 375 | Elf_Data *data; |
| 376 | size_t j; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 377 | |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 378 | lte->dyn_addr = shdr.sh_addr + lte->bias; |
| Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 379 | lte->dyn_sz = shdr.sh_size; |
| 380 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 381 | data = elf_getdata(scn, NULL); |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 388 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 389 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 390 | GElf_Dyn dyn; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 391 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 398 | if (dyn.d_tag == DT_JMPREL) |
| 399 | relplt_addr = dyn.d_un.d_ptr; |
| 400 | else if (dyn.d_tag == DT_PLTRELSZ) |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 401 | lte->relplt_size = dyn.d_un.d_val; |
| 402 | else if (dyn.d_tag == DT_SONAME) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 403 | soname_offset = dyn.d_un.d_val; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 404 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 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; |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 410 | lte->plt_data = elf_loaddata(scn, &shdr); |
| 411 | if (lte->plt_data == NULL) |
| 412 | fprintf(stderr, |
| 413 | "Can't load .plt data\n"); |
| Petr Machata | 18c801c | 2012-04-07 01:24:08 +0200 | [diff] [blame] | 414 | lte->plt_flags = shdr.sh_flags; |
| Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 415 | } |
| 416 | #ifdef ARCH_SUPPORTS_OPD |
| 417 | else if (strcmp(name, ".opd") == 0) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 418 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 419 | lte->opd_size = shdr.sh_size; |
| 420 | lte->opd = elf_rawdata(scn, NULL); |
| 421 | } |
| Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 422 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 423 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 431 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 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; |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 436 | } else if (lte->relplt_size == 0) { |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 437 | debug(1, "%s has unknown PLT size", filename); |
| 438 | lte->relplt = NULL; |
| 439 | lte->relplt_count = 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 440 | } else { |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 441 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 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); |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 453 | if (shdr.sh_addr == relplt_addr |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 454 | && shdr.sh_size == lte->relplt_size) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 455 | lte->relplt = elf_getdata(scn, NULL); |
| 456 | lte->relplt_count = |
| 457 | shdr.sh_size / shdr.sh_entsize; |
| 458 | if (lte->relplt == NULL |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 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 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 475 | |
| 476 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 477 | } |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 478 | |
| 479 | if (soname_offset != 0) |
| 480 | lte->soname = lte->dynstr + soname_offset; |
| 481 | |
| Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 482 | return 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 485 | void |
| Petr Machata | 588850f | 2012-10-26 22:26:54 +0200 | [diff] [blame] | 486 | do_close_elf(struct ltelf *lte) |
| 487 | { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 488 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
| Petr Machata | 4d9a91c | 2012-03-24 04:55:03 +0100 | [diff] [blame] | 489 | arch_elf_destroy(lte); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 490 | elf_end(lte->elf); |
| 491 | close(lte->fd); |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| Edgar E. Iglesias | b5920d1 | 2012-09-27 12:07:35 +0200 | [diff] [blame] | 494 | int |
| Edgar E. Iglesias | 35b5be7 | 2012-10-09 14:22:31 +0200 | [diff] [blame] | 495 | elf_get_sym_info(struct ltelf *lte, const char *filename, |
| 496 | size_t sym_index, GElf_Rela *rela, GElf_Sym *sym) |
| Edgar E. Iglesias | b5920d1 | 2012-09-27 12:07:35 +0200 | [diff] [blame] | 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 | } |
| Edgar E. Iglesias | 35b5be7 | 2012-10-09 14:22:31 +0200 | [diff] [blame] | 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 | } |
| Edgar E. Iglesias | b5920d1 | 2012-09-27 12:07:35 +0200 | [diff] [blame] | 531 | #endif |
| 532 | |
| Petr Machata | e80cace | 2012-09-28 00:05:08 +0200 | [diff] [blame] | 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 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 542 | static int |
| 543 | populate_plt(struct Process *proc, const char *filename, |
| Petr Machata | e80cace | 2012-09-28 00:05:08 +0200 | [diff] [blame] | 544 | struct ltelf *lte, struct library *lib, |
| 545 | int latent_plts) |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 546 | { |
| 547 | size_t i; |
| 548 | for (i = 0; i < lte->relplt_count; ++i) { |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 549 | GElf_Rela rela; |
| 550 | GElf_Sym sym; |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 551 | |
| Edgar E. Iglesias | b5920d1 | 2012-09-27 12:07:35 +0200 | [diff] [blame] | 552 | if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0) |
| 553 | continue; /* Skip this entry. */ |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 554 | |
| 555 | char const *name = lte->dynstr + sym.st_name; |
| 556 | |
| Petr Machata | e80cace | 2012-09-28 00:05:08 +0200 | [diff] [blame] | 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) |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 563 | continue; |
| 564 | |
| Petr Machata | 218c5ff | 2012-04-15 04:22:39 +0200 | [diff] [blame] | 565 | struct library_symbol *libsym = NULL; |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 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) |
| Petr Machata | 8eb0d93 | 2012-04-17 05:18:18 +0200 | [diff] [blame] | 571 | /* fall-through */ |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 572 | case plt_fail: |
| 573 | return -1; |
| Petr Machata | 8eb0d93 | 2012-04-17 05:18:18 +0200 | [diff] [blame] | 574 | /* fall-through */ |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 575 | case plt_ok: |
| Petr Machata | e80cace | 2012-09-28 00:05:08 +0200 | [diff] [blame] | 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); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 582 | library_add_symbol(lib, libsym); |
| Petr Machata | e80cace | 2012-09-28 00:05:08 +0200 | [diff] [blame] | 583 | } |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 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 { |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 597 | arch_addr_t addr; |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 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 | |
| Petr Machata | 10bd7a9 | 2012-09-28 00:06:56 +0200 | [diff] [blame] | 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 | |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 616 | static int |
| 617 | populate_this_symtab(struct Process *proc, const char *filename, |
| 618 | struct ltelf *lte, struct library *lib, |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 619 | Elf_Data *symtab, const char *strtab, size_t size, |
| 620 | struct library_exported_name **names) |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 621 | { |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 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 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 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) { |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 633 | fprintf(stderr, "couldn't insert symbols for -x: %s\n", |
| 634 | strerror(errno)); |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 638 | GElf_Word secflags[lte->ehdr.e_shnum]; |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 639 | size_t i; |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 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 | |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 650 | for (i = 0; i < size; ++i) { |
| 651 | GElf_Sym sym; |
| Petr Machata | 074f68f | 2012-04-07 01:01:02 +0200 | [diff] [blame] | 652 | if (gelf_getsym(symtab, i, &sym) == NULL) { |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 653 | fail: |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 654 | fprintf(stderr, |
| 655 | "couldn't get symbol #%zd from %s: %s\n", |
| 656 | i, filename, elf_errmsg(-1)); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 657 | continue; |
| 658 | } |
| 659 | |
| Petr Machata | 4de6b6b | 2012-04-04 14:06:09 +0200 | [diff] [blame] | 660 | /* XXX support IFUNC as well. */ |
| 661 | if (GELF_ST_TYPE(sym.st_info) != STT_FUNC |
| Petr Machata | 6c1c0bb | 2012-05-18 16:49:34 +0200 | [diff] [blame] | 662 | || sym.st_value == 0 |
| 663 | || sym.st_shndx == STN_UNDEF) |
| Petr Machata | 4de6b6b | 2012-04-04 14:06:09 +0200 | [diff] [blame] | 664 | continue; |
| 665 | |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 666 | /* Find symbol name and snip version. */ |
| Petr Machata | 2bbeac4 | 2012-04-30 20:48:34 +0200 | [diff] [blame] | 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 | |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 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. */ |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 697 | if (!filter_matches_symbol(options.static_filter, name, lib)) |
| 698 | continue; |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 699 | |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 700 | arch_addr_t addr = (arch_addr_t) |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 701 | (uintptr_t)(sym.st_value + lte->bias); |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 702 | arch_addr_t naddr; |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 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; |
| Petr Machata | b1492df | 2012-04-30 21:01:40 +0200 | [diff] [blame] | 710 | } else if (arch_translate_address(lte, addr, &naddr) < 0) { |
| Petr Machata | cc0e1e4 | 2012-04-25 13:42:07 +0200 | [diff] [blame] | 711 | fprintf(stderr, |
| 712 | "couldn't translate address of %s@%s: %s\n", |
| 713 | name, lib->soname, strerror(errno)); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 714 | continue; |
| 715 | } |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 716 | |
| Petr Machata | 3840f68 | 2012-04-06 16:05:41 +0200 | [diff] [blame] | 717 | char *full_name; |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 718 | int own_full_name = 1; |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 719 | if (name_copy == NULL) { |
| 720 | full_name = strdup(name); |
| Petr Machata | 3840f68 | 2012-04-06 16:05:41 +0200 | [diff] [blame] | 721 | if (full_name == NULL) |
| 722 | goto fail; |
| Petr Machata | 3840f68 | 2012-04-06 16:05:41 +0200 | [diff] [blame] | 723 | } else { |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 724 | full_name = name_copy; |
| 725 | own_full_name = 0; |
| Petr Machata | 3840f68 | 2012-04-06 16:05:41 +0200 | [diff] [blame] | 726 | } |
| Petr Machata | 4de6b6b | 2012-04-04 14:06:09 +0200 | [diff] [blame] | 727 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 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); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 734 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 735 | if (unique->libsym == NULL) { |
| 736 | struct library_symbol *libsym = malloc(sizeof(*libsym)); |
| Petr Machata | e8d9076 | 2012-04-15 04:28:31 +0200 | [diff] [blame] | 737 | if (libsym == NULL |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 738 | || library_symbol_init(libsym, naddr, |
| 739 | full_name, own_full_name, |
| 740 | LS_TOPLT_NONE) < 0) { |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 741 | --num_symbols; |
| 742 | goto fail; |
| 743 | } |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 744 | unique->libsym = libsym; |
| 745 | unique->addr = naddr; |
| 746 | |
| 747 | } else if (strlen(full_name) < strlen(unique->libsym->name)) { |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 748 | library_symbol_set_name(unique->libsym, |
| 749 | full_name, own_full_name); |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 750 | |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 751 | } else if (own_full_name) { |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 752 | free(full_name); |
| 753 | } |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 754 | } |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 755 | |
| Petr Machata | 10bd7a9 | 2012-09-28 00:06:56 +0200 | [diff] [blame] | 756 | /* Now we do the union of this set of unique symbols with |
| 757 | * what's already in the library. */ |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 758 | for (i = 0; i < num_symbols; ++i) { |
| Petr Machata | 10bd7a9 | 2012-09-28 00:06:56 +0200 | [diff] [blame] | 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 | } |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| Petr Machata | 10bd7a9 | 2012-09-28 00:06:56 +0200 | [diff] [blame] | 771 | for (i = 0; i < num_symbols; ++i) |
| 772 | if (symbols[i].libsym != NULL) |
| 773 | library_add_symbol(lib, symbols[i].libsym); |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 774 | |
| Petr Machata | 10bd7a9 | 2012-09-28 00:06:56 +0200 | [diff] [blame] | 775 | free(symbols); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static int |
| 780 | populate_symtab(struct Process *proc, const char *filename, |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 781 | struct ltelf *lte, struct library *lib, |
| 782 | int symtabs, int exports) |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 783 | { |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 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); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 802 | } |
| 803 | |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 804 | static int |
| 805 | read_module(struct library *lib, struct Process *proc, |
| 806 | const char *filename, GElf_Addr bias, int main) |
| Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 807 | { |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 808 | struct ltelf lte = {}; |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 809 | if (open_elf(<e, filename) < 0) |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 810 | return -1; |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 811 | |
| Petr Machata | 1c79025 | 2012-10-30 23:29:27 +0100 | [diff] [blame] | 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 | |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 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 | } |
| Petr Machata | b024ca3 | 2012-10-18 19:57:49 +0200 | [diff] [blame] | 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; |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 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(<e, filename) < 0) |
| 862 | return -1; |
| 863 | |
| Petr Machata | e0615ab | 2012-04-17 05:17:48 +0200 | [diff] [blame] | 864 | if (arch_elf_init(<e, lib) < 0) { |
| 865 | fprintf(stderr, "Backend initialization failed.\n"); |
| 866 | return -1; |
| 867 | } |
| 868 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 869 | int status = 0; |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 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; |
| Petr Machata | f13afd5 | 2012-04-14 02:30:31 +0200 | [diff] [blame] | 882 | library_set_pathname(lib, pathname, 1); |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 885 | if (lte.soname != NULL) { |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 886 | char *soname = strdup(lte.soname); |
| 887 | if (soname == NULL) |
| 888 | goto fail; |
| 889 | library_set_soname(lib, soname, 1); |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 890 | } else { |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 891 | const char *soname = rindex(lib->pathname, '/') + 1; |
| 892 | if (soname == NULL) |
| 893 | soname = lib->pathname; |
| 894 | library_set_soname(lib, soname, 0); |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 895 | } |
| Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 896 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 897 | /* XXX The double cast should be removed when |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 898 | * arch_addr_t becomes integral type. */ |
| 899 | arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr; |
| Petr Machata | b1492df | 2012-04-30 21:01:40 +0200 | [diff] [blame] | 900 | if (arch_translate_address(<e, entry, &entry) < 0) |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 901 | goto fail; |
| 902 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 903 | /* XXX The double cast should be removed when |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 904 | * arch_addr_t becomes integral type. */ |
| 905 | lib->base = (arch_addr_t)(uintptr_t)lte.base_addr; |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 906 | lib->entry = entry; |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 907 | /* XXX The double cast should be removed when |
| Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 908 | * arch_addr_t becomes integral type. */ |
| 909 | lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 910 | |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 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) |
| Petr Machata | e80cace | 2012-09-28 00:05:08 +0200 | [diff] [blame] | 924 | && populate_plt(proc, filename, <e, lib, |
| 925 | options.export_filter != NULL) < 0) |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 926 | goto fail; |
| Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 927 | |
| Petr Machata | 013ef5b | 2012-09-28 00:00:55 +0200 | [diff] [blame] | 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, <e, lib, |
| 932 | symtabs, exports) < 0) |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 933 | goto fail; |
| 934 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 935 | done: |
| 936 | do_close_elf(<e); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 937 | return status; |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 938 | |
| 939 | fail: |
| 940 | status = -1; |
| 941 | goto done; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 942 | } |
| Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 943 | |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 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 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 952 | struct library * |
| 953 | ltelf_read_main_binary(struct Process *proc, const char *path) |
| 954 | { |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 955 | struct library *lib = malloc(sizeof(*lib)); |
| 956 | if (lib == NULL) |
| 957 | return NULL; |
| 958 | library_init(lib, LT_LIBTYPE_MAIN); |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 959 | library_set_pathname(lib, path, 0); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 960 | |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 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. */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 967 | char *fname = pid2name(proc->pid); |
| Petr Machata | ef0c74d | 2012-10-27 00:30:57 +0200 | [diff] [blame] | 968 | if (fname == NULL) |
| 969 | return NULL; |
| Petr Machata | da69ed0 | 2012-10-18 17:27:48 +0200 | [diff] [blame] | 970 | if (read_module(lib, proc, fname, 0, 1) < 0) { |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 971 | library_destroy(lib); |
| 972 | free(lib); |
| 973 | return NULL; |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 974 | } |
| Petr Machata | ef0c74d | 2012-10-27 00:30:57 +0200 | [diff] [blame] | 975 | free(fname); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 976 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 977 | return lib; |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 978 | } |