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