| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 1 | #include "config.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 2 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 3 | #include <assert.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 4 | #include <endian.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 5 | #include <errno.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 6 | #include <error.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 7 | #include <fcntl.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 8 | #include <gelf.h> |
| Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 9 | #include <inttypes.h> |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 10 | #include <search.h> |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <stdlib.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 13 | #include <string.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 14 | #include <unistd.h> |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 15 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 16 | #include "common.h" |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 17 | #include "proc.h" |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 18 | #include "library.h" |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 19 | #include "filter.h" |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 20 | |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 21 | #ifdef PLT_REINITALISATION_BP |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 22 | extern char *PLTs_initialized_by_here; |
| Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 23 | #endif |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 24 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 25 | #ifndef ARCH_HAVE_LTELF_DATA |
| 26 | int |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 27 | arch_elf_init(struct ltelf *lte) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| Petr Machata | c67a6e6 | 2012-03-28 02:39:49 +0200 | [diff] [blame] | 31 | |
| 32 | void |
| 33 | arch_elf_destroy(struct ltelf *lte) |
| 34 | { |
| 35 | } |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 38 | int |
| 39 | default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte, |
| Petr Machata | 1be2291 | 2012-03-27 03:11:33 +0200 | [diff] [blame] | 40 | const char *a_name, GElf_Rela *rela, size_t ndx, |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 41 | struct library_symbol **ret) |
| 42 | { |
| 43 | char *name = strdup(a_name); |
| 44 | if (name == NULL) { |
| 45 | fail: |
| 46 | free(name); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| Petr Machata | 1be2291 | 2012-03-27 03:11:33 +0200 | [diff] [blame] | 50 | GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela); |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 51 | |
| 52 | struct library_symbol *libsym = malloc(sizeof(*libsym)); |
| 53 | if (libsym == NULL) |
| 54 | goto fail; |
| 55 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 56 | /* XXX The double cast should be removed when |
| 57 | * target_address_t becomes integral type. */ |
| 58 | target_address_t taddr = (target_address_t) |
| 59 | (uintptr_t)(addr + lte->bias); |
| Petr Machata | bb79060 | 2012-03-25 01:41:59 +0100 | [diff] [blame] | 60 | |
| Petr Machata | e8d9076 | 2012-04-15 04:28:31 +0200 | [diff] [blame] | 61 | if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) { |
| 62 | free(libsym); |
| 63 | goto fail; |
| 64 | } |
| 65 | |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 66 | *ret = libsym; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | #ifndef ARCH_HAVE_ADD_PLT_ENTRY |
| 71 | enum plt_status |
| 72 | arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte, |
| Petr Machata | 1be2291 | 2012-03-27 03:11:33 +0200 | [diff] [blame] | 73 | const char *a_name, GElf_Rela *rela, size_t ndx, |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 74 | struct library_symbol **ret) |
| 75 | { |
| 76 | return plt_default; |
| 77 | } |
| 78 | #endif |
| 79 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 80 | Elf_Data * |
| 81 | elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr) |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 82 | { |
| 83 | Elf_Data *data = elf_getdata(scn, NULL); |
| 84 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 85 | || data->d_off || data->d_size != shdr->sh_size) |
| 86 | return NULL; |
| 87 | return data; |
| 88 | } |
| 89 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 90 | static int |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 91 | elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr, |
| 92 | int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data), |
| 93 | void *data) |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 94 | { |
| 95 | int i; |
| 96 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 97 | Elf_Scn *scn; |
| 98 | GElf_Shdr shdr; |
| 99 | |
| 100 | scn = elf_getscn(lte->elf, i); |
| 101 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { |
| 102 | debug(1, "Couldn't read section or header."); |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 105 | if (predicate(scn, &shdr, data)) { |
| 106 | *tgt_sec = scn; |
| 107 | *tgt_shdr = shdr; |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 108 | return 0; |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 109 | } |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 110 | } |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 111 | return -1; |
| Petr Machata | ffd5aab | 2012-03-24 02:03:33 +0100 | [diff] [blame] | 112 | |
| 113 | } |
| 114 | |
| 115 | static int |
| 116 | inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) |
| 117 | { |
| 118 | GElf_Addr addr = *(GElf_Addr *)data; |
| 119 | return addr >= shdr->sh_addr |
| 120 | && addr < shdr->sh_addr + shdr->sh_size; |
| 121 | } |
| 122 | |
| 123 | int |
| 124 | elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, |
| 125 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 126 | { |
| 127 | return elf_get_section_if(lte, tgt_sec, tgt_shdr, |
| 128 | &inside_p, &addr); |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) |
| 133 | { |
| 134 | GElf_Word type = *(GElf_Word *)data; |
| 135 | return shdr->sh_type == type; |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | elf_get_section_type(struct ltelf *lte, GElf_Word type, |
| 140 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 141 | { |
| 142 | return elf_get_section_if(lte, tgt_sec, tgt_shdr, |
| 143 | &type_p, &type); |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int |
| 147 | need_data(Elf_Data *data, size_t offset, size_t size) |
| 148 | { |
| 149 | assert(data != NULL); |
| 150 | if (data->d_size < size || offset > data->d_size - size) { |
| 151 | debug(1, "Not enough data to read %zd-byte value" |
| 152 | " at offset %zd.", size, offset); |
| 153 | return -1; |
| 154 | } |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 158 | #define DEF_READER(NAME, SIZE) \ |
| 159 | int \ |
| 160 | NAME(Elf_Data *data, size_t offset, uint##SIZE##_t *retp) \ |
| 161 | { \ |
| 162 | if (!need_data(data, offset, SIZE / 8) < 0) \ |
| 163 | return -1; \ |
| 164 | \ |
| Petr Machata | 6d8ccb2 | 2012-03-27 03:11:57 +0200 | [diff] [blame] | 165 | if (data->d_buf == NULL) /* NODATA section */ { \ |
| 166 | *retp = 0; \ |
| 167 | return 0; \ |
| 168 | } \ |
| 169 | \ |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 170 | union { \ |
| 171 | uint##SIZE##_t dst; \ |
| 172 | char buf[0]; \ |
| 173 | } u; \ |
| 174 | memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \ |
| 175 | *retp = u.dst; \ |
| 176 | return 0; \ |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 179 | DEF_READER(elf_read_u16, 16) |
| 180 | DEF_READER(elf_read_u32, 32) |
| 181 | DEF_READER(elf_read_u64, 64) |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 182 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 183 | #undef DEF_READER |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 184 | |
| Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 185 | int |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 186 | open_elf(struct ltelf *lte, const char *filename) |
| 187 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 188 | lte->fd = open(filename, O_RDONLY); |
| 189 | if (lte->fd == -1) |
| Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 190 | return 1; |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 191 | |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 192 | elf_version(EV_CURRENT); |
| 193 | |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 194 | #ifdef HAVE_ELF_C_READ_MMAP |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 195 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 196 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 197 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 198 | #endif |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 199 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 200 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 201 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 202 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 203 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 204 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 205 | filename); |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 206 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 207 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 208 | error(EXIT_FAILURE, 0, |
| 209 | "\"%s\" is not an ELF executable nor shared library", |
| 210 | filename); |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 211 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 212 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 213 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 214 | #ifdef LT_ELF_MACHINE2 |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 215 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 216 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 217 | #endif |
| 218 | #ifdef LT_ELF_MACHINE3 |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 219 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 220 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 221 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 222 | ) |
| 223 | error(EXIT_FAILURE, 0, |
| 224 | "\"%s\" is ELF from incompatible architecture", filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 225 | |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 229 | static int |
| 230 | do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 231 | { |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 232 | int i; |
| 233 | GElf_Addr relplt_addr = 0; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 234 | GElf_Addr soname_offset = 0; |
| Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 235 | |
| 236 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
| 237 | debug(1, "Reading ELF from %s...", filename); |
| 238 | |
| 239 | if (open_elf(lte, filename) < 0) |
| 240 | return -1; |
| 241 | |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 242 | /* Find out the base address. */ |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 243 | { |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 244 | GElf_Phdr phdr; |
| 245 | for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) { |
| 246 | if (phdr.p_type == PT_LOAD) { |
| Petr Machata | 49275b0 | 2012-04-03 12:38:51 +0200 | [diff] [blame] | 247 | lte->base_addr = phdr.p_vaddr + bias; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 253 | if (lte->base_addr == 0) { |
| 254 | fprintf(stderr, "Couldn't determine base address of %s\n", |
| 255 | filename); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | lte->bias = bias; |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 260 | lte->entry_addr = lte->ehdr.e_entry + lte->bias; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 261 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 262 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 263 | Elf_Scn *scn; |
| 264 | GElf_Shdr shdr; |
| 265 | const char *name; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 266 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 267 | scn = elf_getscn(lte->elf, i); |
| 268 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 269 | error(EXIT_FAILURE, 0, |
| 270 | "Couldn't get section header from \"%s\"", |
| 271 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 272 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 273 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 274 | if (name == NULL) |
| 275 | error(EXIT_FAILURE, 0, |
| 276 | "Couldn't get section header from \"%s\"", |
| 277 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 278 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 279 | if (shdr.sh_type == SHT_SYMTAB) { |
| 280 | Elf_Data *data; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 281 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 282 | lte->symtab = elf_getdata(scn, NULL); |
| 283 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 284 | if ((lte->symtab == NULL |
| 285 | || elf_getdata(scn, lte->symtab) != NULL) |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 286 | && options.static_filter != NULL) |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 287 | error(EXIT_FAILURE, 0, |
| 288 | "Couldn't get .symtab data from \"%s\"", |
| 289 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 290 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 291 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 292 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 293 | error(EXIT_FAILURE, 0, |
| 294 | "Couldn't get section header from \"%s\"", |
| 295 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 296 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 297 | data = elf_getdata(scn, NULL); |
| 298 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 299 | || shdr.sh_size != data->d_size || data->d_off) |
| 300 | error(EXIT_FAILURE, 0, |
| 301 | "Couldn't get .strtab data from \"%s\"", |
| 302 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 303 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 304 | lte->strtab = data->d_buf; |
| 305 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 306 | Elf_Data *data; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 307 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 308 | lte->dynsym = elf_getdata(scn, NULL); |
| 309 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 310 | if (lte->dynsym == NULL |
| 311 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 312 | error(EXIT_FAILURE, 0, |
| 313 | "Couldn't get .dynsym data from \"%s\"", |
| 314 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 315 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 316 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 317 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 318 | error(EXIT_FAILURE, 0, |
| 319 | "Couldn't get section header from \"%s\"", |
| 320 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 321 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 322 | data = elf_getdata(scn, NULL); |
| 323 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 324 | || shdr.sh_size != data->d_size || data->d_off) |
| 325 | error(EXIT_FAILURE, 0, |
| 326 | "Couldn't get .dynstr data from \"%s\"", |
| 327 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 328 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 329 | lte->dynstr = data->d_buf; |
| 330 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 331 | Elf_Data *data; |
| 332 | size_t j; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 333 | |
| Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 334 | lte->dyn_addr = shdr.sh_addr; |
| 335 | lte->dyn_sz = shdr.sh_size; |
| 336 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 337 | data = elf_getdata(scn, NULL); |
| 338 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 339 | error(EXIT_FAILURE, 0, |
| 340 | "Couldn't get .dynamic data from \"%s\"", |
| 341 | filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 342 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 343 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 344 | GElf_Dyn dyn; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 345 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 346 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 347 | error(EXIT_FAILURE, 0, |
| 348 | "Couldn't get .dynamic data from \"%s\"", |
| 349 | filename); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 350 | if (dyn.d_tag == DT_JMPREL) |
| 351 | relplt_addr = dyn.d_un.d_ptr; |
| 352 | else if (dyn.d_tag == DT_PLTRELSZ) |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 353 | lte->relplt_size = dyn.d_un.d_val; |
| 354 | else if (dyn.d_tag == DT_SONAME) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 355 | soname_offset = dyn.d_un.d_val; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 356 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 357 | } else if (shdr.sh_type == SHT_PROGBITS |
| 358 | || shdr.sh_type == SHT_NOBITS) { |
| 359 | if (strcmp(name, ".plt") == 0) { |
| 360 | lte->plt_addr = shdr.sh_addr; |
| 361 | lte->plt_size = shdr.sh_size; |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 362 | lte->plt_data = elf_loaddata(scn, &shdr); |
| 363 | if (lte->plt_data == NULL) |
| 364 | fprintf(stderr, |
| 365 | "Can't load .plt data\n"); |
| Petr Machata | 18c801c | 2012-04-07 01:24:08 +0200 | [diff] [blame] | 366 | lte->plt_flags = shdr.sh_flags; |
| Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 367 | } |
| 368 | #ifdef ARCH_SUPPORTS_OPD |
| 369 | else if (strcmp(name, ".opd") == 0) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 370 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 371 | lte->opd_size = shdr.sh_size; |
| 372 | lte->opd = elf_rawdata(scn, NULL); |
| 373 | } |
| Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 374 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 375 | } |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 378 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 379 | error(EXIT_FAILURE, 0, |
| 380 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 381 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 382 | if (!relplt_addr || !lte->plt_addr) { |
| 383 | debug(1, "%s has no PLT relocations", filename); |
| 384 | lte->relplt = NULL; |
| 385 | lte->relplt_count = 0; |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 386 | } else if (lte->relplt_size == 0) { |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 387 | debug(1, "%s has unknown PLT size", filename); |
| 388 | lte->relplt = NULL; |
| 389 | lte->relplt_count = 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 390 | } else { |
| Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 391 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 392 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 393 | Elf_Scn *scn; |
| 394 | GElf_Shdr shdr; |
| 395 | |
| 396 | scn = elf_getscn(lte->elf, i); |
| 397 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 398 | error(EXIT_FAILURE, 0, |
| 399 | "Couldn't get section header from \"%s\"", |
| 400 | filename); |
| 401 | if (shdr.sh_addr == relplt_addr |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 402 | && shdr.sh_size == lte->relplt_size) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 403 | lte->relplt = elf_getdata(scn, NULL); |
| 404 | lte->relplt_count = |
| 405 | shdr.sh_size / shdr.sh_entsize; |
| 406 | if (lte->relplt == NULL |
| 407 | || elf_getdata(scn, lte->relplt) != NULL) |
| 408 | error(EXIT_FAILURE, 0, |
| 409 | "Couldn't get .rel*.plt data from \"%s\"", |
| 410 | filename); |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (i == lte->ehdr.e_shnum) |
| 416 | error(EXIT_FAILURE, 0, |
| 417 | "Couldn't find .rel*.plt section in \"%s\"", |
| 418 | filename); |
| 419 | |
| 420 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 421 | } |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 422 | |
| 423 | if (soname_offset != 0) |
| 424 | lte->soname = lte->dynstr + soname_offset; |
| 425 | |
| Petr Machata | 644d669 | 2012-03-24 02:06:48 +0100 | [diff] [blame] | 426 | if (arch_elf_init(lte) < 0) { |
| 427 | fprintf(stderr, "Backend initialization failed.\n"); |
| 428 | return -1; |
| 429 | } |
| 430 | |
| Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 431 | return 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 434 | /* XXX temporarily non-static */ |
| Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 435 | void |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 436 | do_close_elf(struct ltelf *lte) { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 437 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
| Petr Machata | 4d9a91c | 2012-03-24 04:55:03 +0100 | [diff] [blame] | 438 | arch_elf_destroy(lte); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 439 | elf_end(lte->elf); |
| 440 | close(lte->fd); |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 443 | static int |
| 444 | populate_plt(struct Process *proc, const char *filename, |
| 445 | struct ltelf *lte, struct library *lib) |
| 446 | { |
| 447 | size_t i; |
| 448 | for (i = 0; i < lte->relplt_count; ++i) { |
| 449 | GElf_Rel rel; |
| 450 | GElf_Rela rela; |
| 451 | GElf_Sym sym; |
| 452 | void *ret; |
| 453 | |
| 454 | if (lte->relplt->d_type == ELF_T_REL) { |
| 455 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 456 | rela.r_offset = rel.r_offset; |
| 457 | rela.r_info = rel.r_info; |
| 458 | rela.r_addend = 0; |
| 459 | } else { |
| 460 | ret = gelf_getrela(lte->relplt, i, &rela); |
| 461 | } |
| 462 | |
| 463 | if (ret == NULL |
| 464 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 465 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 466 | &sym) == NULL) |
| 467 | error(EXIT_FAILURE, 0, |
| 468 | "Couldn't get relocation from \"%s\"", |
| 469 | filename); |
| 470 | |
| 471 | char const *name = lte->dynstr + sym.st_name; |
| 472 | |
| 473 | if (!filter_matches_symbol(options.plt_filter, name, lib)) |
| 474 | continue; |
| 475 | |
| Petr Machata | 218c5ff | 2012-04-15 04:22:39 +0200 | [diff] [blame] | 476 | struct library_symbol *libsym = NULL; |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 477 | switch (arch_elf_add_plt_entry(proc, lte, name, |
| 478 | &rela, i, &libsym)) { |
| 479 | case plt_default: |
| 480 | if (default_elf_add_plt_entry(proc, lte, name, |
| 481 | &rela, i, &libsym) < 0) |
| 482 | case plt_fail: |
| 483 | return -1; |
| 484 | case plt_ok: |
| 485 | if (libsym != NULL) |
| 486 | library_add_symbol(lib, libsym); |
| 487 | } |
| 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 492 | /* When -x rules result in request to trace several aliases, we only |
| 493 | * want to add such symbol once. The only way that those symbols |
| 494 | * differ in is their name, e.g. in glibc you have __GI___libc_free, |
| 495 | * __cfree, __free, __libc_free, cfree and free all defined on the |
| 496 | * same address. So instead we keep this unique symbol struct for |
| 497 | * each address, and replace name in libsym with a shorter variant if |
| 498 | * we find it. */ |
| 499 | struct unique_symbol { |
| 500 | target_address_t addr; |
| 501 | struct library_symbol *libsym; |
| 502 | }; |
| 503 | |
| 504 | static int |
| 505 | unique_symbol_cmp(const void *key, const void *val) |
| 506 | { |
| 507 | const struct unique_symbol *sym_key = key; |
| 508 | const struct unique_symbol *sym_val = val; |
| 509 | return sym_key->addr != sym_val->addr; |
| 510 | } |
| 511 | |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 512 | static int |
| 513 | populate_this_symtab(struct Process *proc, const char *filename, |
| 514 | struct ltelf *lte, struct library *lib, |
| 515 | Elf_Data *symtab, const char *strtab, size_t size) |
| 516 | { |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 517 | /* Using sorted array would be arguably better, but this |
| 518 | * should be well enough for the number of symbols that we |
| 519 | * typically deal with. */ |
| 520 | size_t num_symbols = 0; |
| 521 | struct unique_symbol *symbols = malloc(sizeof(*symbols) * size); |
| 522 | if (symbols == NULL) { |
| 523 | error(0, errno, "couldn't insert symbols for -x"); |
| 524 | return -1; |
| 525 | } |
| 526 | |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 527 | GElf_Word secflags[lte->ehdr.e_shnum]; |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 528 | size_t i; |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 529 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 530 | Elf_Scn *scn = elf_getscn(lte->elf, i); |
| 531 | if (scn == NULL) |
| 532 | continue; |
| 533 | GElf_Shdr shdr; |
| 534 | if (gelf_getshdr(scn, &shdr) == NULL) |
| 535 | continue; |
| 536 | secflags[i] = shdr.sh_flags; |
| 537 | } |
| 538 | |
| 539 | size_t lib_len = strlen(lib->soname); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 540 | for (i = 0; i < size; ++i) { |
| 541 | GElf_Sym sym; |
| Petr Machata | 074f68f | 2012-04-07 01:01:02 +0200 | [diff] [blame] | 542 | if (gelf_getsym(symtab, i, &sym) == NULL) { |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 543 | fail: |
| 544 | error(0, errno, "couldn't get symbol #%zd from %s: %s", |
| 545 | i, filename, elf_errmsg(-1)); |
| 546 | continue; |
| 547 | } |
| 548 | |
| Petr Machata | 4de6b6b | 2012-04-04 14:06:09 +0200 | [diff] [blame] | 549 | /* XXX support IFUNC as well. */ |
| 550 | if (GELF_ST_TYPE(sym.st_info) != STT_FUNC |
| 551 | || sym.st_value == 0) |
| 552 | continue; |
| 553 | |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 554 | const char *name = strtab + sym.st_name; |
| 555 | if (!filter_matches_symbol(options.static_filter, name, lib)) |
| 556 | continue; |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 557 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 558 | target_address_t addr = (target_address_t) |
| 559 | (uintptr_t)(sym.st_value + lte->bias); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 560 | target_address_t naddr; |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 561 | |
| 562 | /* On arches that support OPD, the value of typical |
| 563 | * function symbol will be a pointer to .opd, but some |
| 564 | * will point directly to .text. We don't want to |
| 565 | * translate those. */ |
| 566 | if (secflags[sym.st_shndx] & SHF_EXECINSTR) { |
| 567 | naddr = addr; |
| 568 | } else if (arch_translate_address(proc, addr, &naddr) < 0) { |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 569 | error(0, errno, "couldn't translate address of %s@%s", |
| 570 | name, lib->soname); |
| 571 | continue; |
| 572 | } |
| Petr Machata | 40cc53b | 2012-04-07 01:25:38 +0200 | [diff] [blame] | 573 | |
| 574 | /* If the translation actually took place, and wasn't |
| 575 | * a no-op, then bias again. XXX We shouldn't apply |
| 576 | * second bias for libraries that were open at the |
| 577 | * time that we attached. In fact what we should do |
| 578 | * is look at each translated address, whether it |
| 579 | * falls into a SHF_EXECINSTR section. If it does, |
| 580 | * it's most likely already translated. */ |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 581 | if (addr != naddr) |
| 582 | naddr += lte->bias; |
| 583 | |
| Petr Machata | 3840f68 | 2012-04-06 16:05:41 +0200 | [diff] [blame] | 584 | char *full_name; |
| 585 | if (lib->type != LT_LIBTYPE_MAIN) { |
| 586 | full_name = malloc(strlen(name) + 1 + lib_len + 1); |
| 587 | if (full_name == NULL) |
| 588 | goto fail; |
| 589 | sprintf(full_name, "%s@%s", name, lib->soname); |
| 590 | } else { |
| 591 | full_name = strdup(name); |
| 592 | if (full_name == NULL) |
| 593 | goto fail; |
| 594 | } |
| Petr Machata | 4de6b6b | 2012-04-04 14:06:09 +0200 | [diff] [blame] | 595 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 596 | /* Look whether we already have a symbol for this |
| 597 | * address. If not, add this one. */ |
| 598 | struct unique_symbol key = { naddr, NULL }; |
| 599 | struct unique_symbol *unique |
| 600 | = lsearch(&key, symbols, &num_symbols, |
| 601 | sizeof(*symbols), &unique_symbol_cmp); |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 602 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 603 | if (unique->libsym == NULL) { |
| 604 | struct library_symbol *libsym = malloc(sizeof(*libsym)); |
| Petr Machata | e8d9076 | 2012-04-15 04:28:31 +0200 | [diff] [blame] | 605 | if (libsym == NULL |
| 606 | || library_symbol_init(libsym, naddr, full_name, |
| 607 | 1, LS_TOPLT_NONE) < 0) { |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 608 | --num_symbols; |
| 609 | goto fail; |
| 610 | } |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 611 | unique->libsym = libsym; |
| 612 | unique->addr = naddr; |
| 613 | |
| 614 | } else if (strlen(full_name) < strlen(unique->libsym->name)) { |
| 615 | library_symbol_set_name(unique->libsym, full_name, 1); |
| 616 | |
| 617 | } else { |
| 618 | free(full_name); |
| 619 | } |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 620 | } |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 621 | |
| 622 | for (i = 0; i < num_symbols; ++i) { |
| 623 | assert(symbols[i].libsym != NULL); |
| 624 | library_add_symbol(lib, symbols[i].libsym); |
| 625 | } |
| 626 | |
| 627 | free(symbols); |
| 628 | |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int |
| 633 | populate_symtab(struct Process *proc, const char *filename, |
| 634 | struct ltelf *lte, struct library *lib) |
| 635 | { |
| 636 | if (lte->symtab != NULL && lte->strtab != NULL) |
| 637 | return populate_this_symtab(proc, filename, lte, lib, |
| 638 | lte->symtab, lte->strtab, |
| 639 | lte->symtab_count); |
| 640 | else |
| 641 | return populate_this_symtab(proc, filename, lte, lib, |
| 642 | lte->dynsym, lte->dynstr, |
| 643 | lte->dynsym_count); |
| 644 | } |
| 645 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 646 | int |
| 647 | ltelf_read_library(struct library *lib, struct Process *proc, |
| 648 | const char *filename, GElf_Addr bias) |
| Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 649 | { |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 650 | struct ltelf lte = {}; |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 651 | if (do_init_elf(<e, filename, bias) < 0) |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 652 | return -1; |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 653 | proc->e_machine = lte.ehdr.e_machine; |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 654 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 655 | int status = 0; |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 656 | if (lib == NULL) |
| 657 | goto fail; |
| 658 | |
| 659 | /* Note that we set soname and pathname as soon as they are |
| 660 | * allocated, so in case of further errors, this get released |
| 661 | * when LIB is release, which should happen in the caller when |
| 662 | * we return error. */ |
| 663 | |
| 664 | if (lib->pathname == NULL) { |
| 665 | char *pathname = strdup(filename); |
| 666 | if (pathname == NULL) |
| 667 | goto fail; |
| Petr Machata | f13afd5 | 2012-04-14 02:30:31 +0200 | [diff] [blame] | 668 | library_set_pathname(lib, pathname, 1); |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 669 | } |
| 670 | |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 671 | if (lte.soname != NULL) { |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 672 | char *soname = strdup(lte.soname); |
| 673 | if (soname == NULL) |
| 674 | goto fail; |
| 675 | library_set_soname(lib, soname, 1); |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 676 | } else { |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 677 | const char *soname = rindex(lib->pathname, '/') + 1; |
| 678 | if (soname == NULL) |
| 679 | soname = lib->pathname; |
| 680 | library_set_soname(lib, soname, 0); |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 681 | } |
| Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 682 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 683 | /* XXX The double cast should be removed when |
| 684 | * target_address_t becomes integral type. */ |
| 685 | target_address_t entry = (target_address_t)(uintptr_t)lte.entry_addr; |
| Petr Machata | 49275b0 | 2012-04-03 12:38:51 +0200 | [diff] [blame] | 686 | if (arch_translate_address(proc, entry, &entry) < 0) |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 687 | goto fail; |
| 688 | |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 689 | /* XXX The double cast should be removed when |
| 690 | * target_address_t becomes integral type. */ |
| 691 | lib->base = (target_address_t)(uintptr_t)lte.base_addr; |
| Petr Machata | b120fdf | 2012-03-21 05:05:46 +0100 | [diff] [blame] | 692 | lib->entry = entry; |
| Petr Machata | ea8eb9a | 2012-04-17 01:32:07 +0200 | [diff] [blame] | 693 | /* XXX The double cast should be removed when |
| 694 | * target_address_t becomes integral type. */ |
| 695 | lib->dyn_addr = (target_address_t)(uintptr_t)lte.dyn_addr; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 696 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 697 | if (filter_matches_library(options.plt_filter, lib) |
| 698 | && populate_plt(proc, filename, <e, lib) < 0) |
| 699 | goto fail; |
| Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 700 | |
| Petr Machata | da3edbf | 2012-04-04 02:20:21 +0200 | [diff] [blame] | 701 | if (filter_matches_library(options.static_filter, lib) |
| 702 | && populate_symtab(proc, filename, <e, lib) < 0) |
| 703 | goto fail; |
| 704 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 705 | done: |
| 706 | do_close_elf(<e); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 707 | return status; |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 708 | |
| 709 | fail: |
| 710 | status = -1; |
| 711 | goto done; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 712 | } |
| Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 713 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 714 | struct library * |
| 715 | ltelf_read_main_binary(struct Process *proc, const char *path) |
| 716 | { |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 717 | struct library *lib = malloc(sizeof(*lib)); |
| 718 | if (lib == NULL) |
| 719 | return NULL; |
| 720 | library_init(lib, LT_LIBTYPE_MAIN); |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 721 | library_set_pathname(lib, path, 0); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 722 | |
| Petr Machata | fc6ff18 | 2012-04-04 13:11:50 +0200 | [diff] [blame] | 723 | /* There is a race between running the process and reading its |
| 724 | * binary for internal consumption. So open the binary from |
| 725 | * the /proc filesystem. XXX Note that there is similar race |
| 726 | * for libraries, but there we don't have a nice answer like |
| 727 | * that. Presumably we could read the DSOs from the process |
| 728 | * memory image, but that's not currently done. */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 729 | char *fname = pid2name(proc->pid); |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 730 | if (ltelf_read_library(lib, proc, fname, 0) < 0) { |
| 731 | library_destroy(lib); |
| 732 | free(lib); |
| 733 | return NULL; |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 734 | } |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 735 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 736 | return lib; |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 737 | } |