Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 2 | # include "config.h" |
Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 3 | #endif |
| 4 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 5 | #include <endian.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 6 | #include <errno.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 7 | #include <error.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 8 | #include <fcntl.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 9 | #include <gelf.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdlib.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 12 | #include <string.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 13 | #include <unistd.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 14 | |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 15 | #include "ltrace.h" |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 16 | #include "elf.h" |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 17 | #include "debug.h" |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 18 | #include "options.h" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 19 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | static void do_init_elf(struct ltelf *lte, const char *filename); |
| 21 | static void do_close_elf(struct ltelf *lte); |
| 22 | static void add_library_symbol(GElf_Addr addr, const char *name, |
| 23 | struct library_symbol **library_symbolspp, |
| 24 | int use_elf_plt2addr, int is_weak); |
| 25 | static int in_load_libraries(const char *name, struct ltelf *lte); |
| 26 | static GElf_Addr elf_plt2addr(struct ltelf *ltc, void *addr); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 27 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 28 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | extern char *PLTs_initialized_by_here; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 30 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 31 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 32 | static void do_init_elf(struct ltelf *lte, const char *filename) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 33 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 34 | int i; |
| 35 | GElf_Addr relplt_addr = 0; |
| 36 | size_t relplt_size = 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 37 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 38 | debug(1, "Reading ELF from %s...", filename); |
Juan Cespedes | 1afec69 | 1997-08-23 21:31:46 +0200 | [diff] [blame] | 39 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 40 | memset(lte, 0, sizeof(*lte)); |
| 41 | lte->fd = open(filename, O_RDONLY); |
| 42 | if (lte->fd == -1) |
| 43 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 44 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 45 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 46 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 47 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 48 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 49 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 50 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 51 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 52 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 53 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 54 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 55 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 56 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 57 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 58 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 59 | error(EXIT_FAILURE, 0, |
| 60 | "\"%s\" is not an ELF executable nor shared library", |
| 61 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 62 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 63 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 64 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 65 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 67 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 68 | #endif |
| 69 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 70 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 71 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 72 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 73 | ) |
| 74 | error(EXIT_FAILURE, 0, |
| 75 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 76 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 77 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 78 | Elf_Scn *scn; |
| 79 | GElf_Shdr shdr; |
| 80 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 81 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 82 | scn = elf_getscn(lte->elf, i); |
| 83 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 84 | error(EXIT_FAILURE, 0, |
| 85 | "Couldn't get section header from \"%s\"", |
| 86 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 87 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 88 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 89 | if (name == NULL) |
| 90 | error(EXIT_FAILURE, 0, |
| 91 | "Couldn't get section header from \"%s\"", |
| 92 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 93 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 94 | if (shdr.sh_type == SHT_SYMTAB) { |
| 95 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 96 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 97 | lte->symtab = elf_getdata(scn, NULL); |
| 98 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 99 | if ((lte->symtab == NULL |
| 100 | || elf_getdata(scn, lte->symtab) != NULL) |
| 101 | && opt_x != NULL) |
| 102 | error(EXIT_FAILURE, 0, |
| 103 | "Couldn't get .symtab data from \"%s\"", |
| 104 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 105 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 106 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 107 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 108 | error(EXIT_FAILURE, 0, |
| 109 | "Couldn't get section header from \"%s\"", |
| 110 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 111 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 112 | data = elf_getdata(scn, NULL); |
| 113 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 114 | || shdr.sh_size != data->d_size || data->d_off) |
| 115 | error(EXIT_FAILURE, 0, |
| 116 | "Couldn't get .strtab data from \"%s\"", |
| 117 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 118 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 119 | lte->strtab = data->d_buf; |
| 120 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 121 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 122 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 123 | lte->dynsym = elf_getdata(scn, NULL); |
| 124 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 125 | if (lte->dynsym == NULL |
| 126 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 127 | error(EXIT_FAILURE, 0, |
| 128 | "Couldn't get .dynsym data from \"%s\"", |
| 129 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 130 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 131 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 132 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 133 | error(EXIT_FAILURE, 0, |
| 134 | "Couldn't get section header from \"%s\"", |
| 135 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 136 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 137 | data = elf_getdata(scn, NULL); |
| 138 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 139 | || shdr.sh_size != data->d_size || data->d_off) |
| 140 | error(EXIT_FAILURE, 0, |
| 141 | "Couldn't get .dynstr data from \"%s\"", |
| 142 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 143 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 144 | lte->dynstr = data->d_buf; |
| 145 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 146 | Elf_Data *data; |
| 147 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 148 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 149 | data = elf_getdata(scn, NULL); |
| 150 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 151 | error(EXIT_FAILURE, 0, |
| 152 | "Couldn't get .dynamic data from \"%s\"", |
| 153 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 154 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 155 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 156 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 157 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 158 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 159 | error(EXIT_FAILURE, 0, |
| 160 | "Couldn't get .dynamic data from \"%s\"", |
| 161 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 162 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 163 | if (dyn.d_tag == DT_JMPREL) |
| 164 | relplt_addr = dyn.d_un.d_ptr; |
| 165 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 166 | relplt_size = dyn.d_un.d_val; |
| 167 | } |
| 168 | } else if (shdr.sh_type == SHT_HASH) { |
| 169 | Elf_Data *data; |
| 170 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 171 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 172 | data = elf_getdata(scn, NULL); |
| 173 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 174 | || data->d_off || data->d_size != shdr.sh_size) |
| 175 | error(EXIT_FAILURE, 0, |
| 176 | "Couldn't get .hash data from \"%s\"", |
| 177 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 178 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 179 | if (shdr.sh_entsize == 4) { |
| 180 | /* Standard conforming ELF. */ |
| 181 | if (data->d_type != ELF_T_WORD) |
| 182 | error(EXIT_FAILURE, 0, |
| 183 | "Couldn't get .hash data from \"%s\"", |
| 184 | filename); |
| 185 | lte->hash = (Elf32_Word *) data->d_buf; |
| 186 | } else if (shdr.sh_entsize == 8) { |
| 187 | /* Alpha or s390x. */ |
| 188 | Elf32_Word *dst, *src; |
| 189 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 190 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 191 | lte->hash = (Elf32_Word *) |
| 192 | malloc(hash_count * sizeof(Elf32_Word)); |
| 193 | if (lte->hash == NULL) |
| 194 | error(EXIT_FAILURE, 0, |
| 195 | "Couldn't convert .hash section from \"%s\"", |
| 196 | filename); |
| 197 | lte->hash_malloced = 1; |
| 198 | dst = lte->hash; |
| 199 | src = (Elf32_Word *) data->d_buf; |
| 200 | if ((data->d_type == ELF_T_WORD |
| 201 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 202 | || (data->d_type == ELF_T_XWORD |
| 203 | && lte->ehdr.e_ident[EI_DATA] == |
| 204 | ELFDATA2MSB)) |
| 205 | ++src; |
| 206 | for (j = 0; j < hash_count; ++j, src += 2) |
| 207 | *dst++ = *src; |
| 208 | } else |
| 209 | error(EXIT_FAILURE, 0, |
| 210 | "Unknown .hash sh_entsize in \"%s\"", |
| 211 | filename); |
| 212 | } else if (shdr.sh_type == SHT_PROGBITS |
| 213 | || shdr.sh_type == SHT_NOBITS) { |
| 214 | if (strcmp(name, ".plt") == 0) { |
| 215 | lte->plt_addr = shdr.sh_addr; |
| 216 | lte->plt_size = shdr.sh_size; |
| 217 | } else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 218 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 219 | lte->opd_size = shdr.sh_size; |
| 220 | lte->opd = elf_rawdata(scn, NULL); |
| 221 | } |
| 222 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 225 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 226 | error(EXIT_FAILURE, 0, |
| 227 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 228 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 229 | if (!relplt_addr || !lte->plt_addr) { |
| 230 | debug(1, "%s has no PLT relocations", filename); |
| 231 | lte->relplt = NULL; |
| 232 | lte->relplt_count = 0; |
| 233 | } else { |
| 234 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 235 | Elf_Scn *scn; |
| 236 | GElf_Shdr shdr; |
| 237 | |
| 238 | scn = elf_getscn(lte->elf, i); |
| 239 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 240 | error(EXIT_FAILURE, 0, |
| 241 | "Couldn't get section header from \"%s\"", |
| 242 | filename); |
| 243 | if (shdr.sh_addr == relplt_addr |
| 244 | && shdr.sh_size == relplt_size) { |
| 245 | lte->relplt = elf_getdata(scn, NULL); |
| 246 | lte->relplt_count = |
| 247 | shdr.sh_size / shdr.sh_entsize; |
| 248 | if (lte->relplt == NULL |
| 249 | || elf_getdata(scn, lte->relplt) != NULL) |
| 250 | error(EXIT_FAILURE, 0, |
| 251 | "Couldn't get .rel*.plt data from \"%s\"", |
| 252 | filename); |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (i == lte->ehdr.e_shnum) |
| 258 | error(EXIT_FAILURE, 0, |
| 259 | "Couldn't find .rel*.plt section in \"%s\"", |
| 260 | filename); |
| 261 | |
| 262 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void do_close_elf(struct ltelf *lte) |
| 267 | { |
| 268 | if (lte->hash_malloced) |
| 269 | free((char *)lte->hash); |
| 270 | elf_end(lte->elf); |
| 271 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 274 | static void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 275 | add_library_symbol(GElf_Addr addr, const char *name, |
| 276 | struct library_symbol **library_symbolspp, |
| 277 | int use_elf_plt2addr, int is_weak) |
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 | struct library_symbol *s; |
| 280 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 281 | if (s == NULL) |
| 282 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 283 | |
| 284 | s->needs_init = 1; |
| 285 | s->is_weak = is_weak; |
| 286 | s->static_plt2addr = use_elf_plt2addr; |
| 287 | s->next = *library_symbolspp; |
| 288 | s->enter_addr = (void *)(uintptr_t) addr; |
| 289 | s->brkpnt = NULL; |
| 290 | s->name = (char *)(s + 1); |
| 291 | strcpy(s->name, name); |
| 292 | *library_symbolspp = s; |
| 293 | |
| 294 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 295 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 296 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 297 | static int in_load_libraries(const char *name, struct ltelf *lte) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 298 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 299 | size_t i; |
| 300 | unsigned long hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 301 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 302 | if (!library_num) |
| 303 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 304 | |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 305 | hash = elf_hash((const unsigned char *)name); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 306 | for (i = 1; i <= library_num; ++i) { |
| 307 | Elf32_Word nbuckets, symndx; |
| 308 | Elf32_Word *buckets, *chain; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 309 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 310 | if (lte[i].hash == NULL) |
| 311 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 312 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 313 | nbuckets = lte[i].hash[0]; |
| 314 | buckets = <e[i].hash[2]; |
| 315 | chain = <e[i].hash[2 + nbuckets]; |
| 316 | for (symndx = buckets[hash % nbuckets]; |
| 317 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 318 | GElf_Sym sym; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 319 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 320 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 321 | error(EXIT_FAILURE, 0, |
| 322 | "Couldn't get symbol from .dynsym"); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 323 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 324 | if (sym.st_value != 0 |
| 325 | && sym.st_shndx != SHN_UNDEF |
| 326 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 327 | return 1; |
| 328 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 329 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 330 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 331 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 332 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 333 | static GElf_Addr elf_plt2addr(struct ltelf *lte, void *addr) |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 334 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 335 | long base; |
| 336 | long offset; |
| 337 | GElf_Addr ret_val; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 338 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 339 | if (!lte->opd) |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 340 | return (GElf_Addr) (long) addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 341 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 342 | base = (long)lte->opd->d_buf; |
| 343 | offset = (long)addr - (long)lte->opd_addr; |
| 344 | if (offset > lte->opd_size) |
| 345 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 346 | |
| 347 | ret_val = (GElf_Addr) * (long *)(base + offset); |
| 348 | return ret_val; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 351 | struct library_symbol *read_elf(struct process *proc) |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 352 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 353 | struct library_symbol *library_symbols = NULL; |
| 354 | struct ltelf lte[MAX_LIBRARY + 1]; |
| 355 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 356 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 357 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 358 | struct opt_x_t *main_cheat; |
| 359 | int exit_out = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 360 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 361 | elf_version(EV_CURRENT); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 362 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 363 | do_init_elf(lte, proc->filename); |
| 364 | proc->e_machine = lte->ehdr.e_machine; |
| 365 | for (i = 0; i < library_num; ++i) |
| 366 | do_init_elf(<e[i + 1], library[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 367 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 368 | for (i = 0; i < lte->relplt_count; ++i) { |
| 369 | GElf_Rel rel; |
| 370 | GElf_Rela rela; |
| 371 | GElf_Sym sym; |
| 372 | GElf_Addr addr; |
| 373 | void *ret; |
| 374 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 375 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 376 | if (lte->relplt->d_type == ELF_T_REL) { |
| 377 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 378 | rela.r_offset = rel.r_offset; |
| 379 | rela.r_info = rel.r_info; |
| 380 | rela.r_addend = 0; |
| 381 | } else |
| 382 | ret = gelf_getrela(lte->relplt, i, &rela); |
| 383 | |
| 384 | if (ret == NULL |
| 385 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 386 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 387 | &sym) == NULL) |
| 388 | error(EXIT_FAILURE, 0, |
| 389 | "Couldn't get relocation from \"%s\"", |
| 390 | proc->filename); |
| 391 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 392 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 393 | if (!sym.st_value && PLTs_initialized_by_here) |
| 394 | proc->need_to_reinitialize_breakpoints = 1; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 395 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 396 | |
| 397 | name = lte->dynstr + sym.st_name; |
| 398 | if (in_load_libraries(name, lte)) { |
| 399 | addr = arch_plt_sym_val(lte, i, &rela); |
| 400 | add_library_symbol(addr, name, &library_symbols, 0, |
| 401 | ELF64_ST_BIND(sym.st_info) != 0); |
| 402 | if (!lib_tail) |
| 403 | lib_tail = &(library_symbols->next); |
| 404 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 405 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 406 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 407 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 408 | if (proc->need_to_reinitialize_breakpoints) { |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 409 | /* Add "PLTs_initialized_by_here" to opt_x list, if not |
| 410 | already there. */ |
Paul Gilliam | 2b2507a | 2006-04-07 18:32:07 +0200 | [diff] [blame] | 411 | main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t)); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 412 | if (main_cheat == NULL) |
| 413 | error(EXIT_FAILURE, 0, "Couldn allocate memory"); |
| 414 | main_cheat->next = opt_x; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 415 | main_cheat->found = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 416 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 417 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 418 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 419 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0 |
| 420 | && main_cheat) { |
| 421 | free(main_cheat); |
| 422 | main_cheat = NULL; |
| 423 | break; |
| 424 | } |
| 425 | if (main_cheat) |
| 426 | opt_x = main_cheat; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 427 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 428 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 429 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 430 | for (i = 0; i < lte->symtab_count; ++i) { |
| 431 | GElf_Sym sym; |
| 432 | GElf_Addr addr; |
| 433 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 434 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 435 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 436 | error(EXIT_FAILURE, 0, |
| 437 | "Couldn't get symbol from \"%s\"", |
| 438 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 439 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 440 | name = lte->strtab + sym.st_name; |
| 441 | addr = sym.st_value; |
| 442 | if (!addr) |
| 443 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 444 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 445 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 446 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 447 | /* FIXME: Should be able to use &library_symbols as above. But |
| 448 | when you do, none of the real library symbols cause breaks. */ |
| 449 | add_library_symbol(elf_plt2addr |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 450 | (lte, (void *) (long) addr), |
| 451 | name, lib_tail, 1, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 452 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | } |
| 456 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 457 | if ( ! xptr->found) { |
| 458 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 459 | #ifdef PLT_REINITALISATION_BP |
| 460 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 461 | if (lte->ehdr.e_entry) { |
| 462 | add_library_symbol ( |
| 463 | elf_plt2addr (lte, (void*)(long) |
| 464 | lte->ehdr.e_entry), |
| 465 | PLTs_initialized_by_here, |
| 466 | lib_tail, 1, 0); |
| 467 | fprintf (stderr, "WARNING: Using e_ent" |
| 468 | "ry from elf header (%p) for " |
| 469 | "address of \"%s\"\n", (void*) |
| 470 | (long) lte->ehdr.e_entry, |
| 471 | PLTs_initialized_by_here); |
| 472 | continue; |
| 473 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 474 | badthing = "ERROR"; |
| 475 | exit_out = 1; |
| 476 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 477 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 478 | fprintf (stderr, |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame^] | 479 | "%s: Couldn't find symbol \"%s\" in file \"%s" |
| 480 | "\"\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 481 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 482 | if (exit_out) { |
| 483 | exit (1); |
| 484 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 485 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 486 | for (i = 0; i < library_num + 1; ++i) |
| 487 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 488 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 489 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 490 | } |