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, |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 24 | enum toplt type_of_plt, int is_weak); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 25 | static int in_load_libraries(const char *name, struct ltelf *lte); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 26 | static GElf_Addr opd2addr(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 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 172 | lte->hash_type = SHT_HASH; |
| 173 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 174 | data = elf_getdata(scn, NULL); |
| 175 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 176 | || data->d_off || data->d_size != shdr.sh_size) |
| 177 | error(EXIT_FAILURE, 0, |
| 178 | "Couldn't get .hash data from \"%s\"", |
| 179 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 180 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 181 | if (shdr.sh_entsize == 4) { |
| 182 | /* Standard conforming ELF. */ |
| 183 | if (data->d_type != ELF_T_WORD) |
| 184 | error(EXIT_FAILURE, 0, |
| 185 | "Couldn't get .hash data from \"%s\"", |
| 186 | filename); |
| 187 | lte->hash = (Elf32_Word *) data->d_buf; |
| 188 | } else if (shdr.sh_entsize == 8) { |
| 189 | /* Alpha or s390x. */ |
| 190 | Elf32_Word *dst, *src; |
| 191 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 192 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 193 | lte->hash = (Elf32_Word *) |
| 194 | malloc(hash_count * sizeof(Elf32_Word)); |
| 195 | if (lte->hash == NULL) |
| 196 | error(EXIT_FAILURE, 0, |
| 197 | "Couldn't convert .hash section from \"%s\"", |
| 198 | filename); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 199 | lte->lte_flags |= LTE_HASH_MALLOCED; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 200 | dst = lte->hash; |
| 201 | src = (Elf32_Word *) data->d_buf; |
| 202 | if ((data->d_type == ELF_T_WORD |
| 203 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 204 | || (data->d_type == ELF_T_XWORD |
| 205 | && lte->ehdr.e_ident[EI_DATA] == |
| 206 | ELFDATA2MSB)) |
| 207 | ++src; |
| 208 | for (j = 0; j < hash_count; ++j, src += 2) |
| 209 | *dst++ = *src; |
| 210 | } else |
| 211 | error(EXIT_FAILURE, 0, |
| 212 | "Unknown .hash sh_entsize in \"%s\"", |
| 213 | filename); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 214 | #ifdef SHT_GNU_HASH |
| 215 | } else if (shdr.sh_type == SHT_GNU_HASH |
| 216 | && lte->hash == NULL) { |
| 217 | Elf_Data *data; |
| 218 | size_t j; |
| 219 | |
| 220 | lte->hash_type = SHT_GNU_HASH; |
| 221 | |
| 222 | if (shdr.sh_entsize != 0 |
| 223 | && shdr.sh_entsize != 4) { |
| 224 | error(EXIT_FAILURE, 0, |
| 225 | ".gnu.hash sh_entsize in \"%s\" should be 4, but is %d", |
| 226 | filename, shdr.sh_entsize); |
| 227 | } |
| 228 | |
| 229 | data = elf_getdata(scn, NULL); |
| 230 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 231 | || data->d_off || data->d_size != shdr.sh_size) |
| 232 | error(EXIT_FAILURE, 0, |
| 233 | "Couldn't get .gnu.hash data from \"%s\"", |
| 234 | filename); |
| 235 | |
| 236 | lte->hash = (Elf32_Word *) data->d_buf; |
| 237 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 238 | } else if (shdr.sh_type == SHT_PROGBITS |
| 239 | || shdr.sh_type == SHT_NOBITS) { |
| 240 | if (strcmp(name, ".plt") == 0) { |
| 241 | lte->plt_addr = shdr.sh_addr; |
| 242 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 243 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 244 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 245 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 246 | } else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 247 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 248 | lte->opd_size = shdr.sh_size; |
| 249 | lte->opd = elf_rawdata(scn, NULL); |
| 250 | } |
| 251 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 254 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 255 | error(EXIT_FAILURE, 0, |
| 256 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 257 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 258 | if (!relplt_addr || !lte->plt_addr) { |
| 259 | debug(1, "%s has no PLT relocations", filename); |
| 260 | lte->relplt = NULL; |
| 261 | lte->relplt_count = 0; |
| 262 | } else { |
| 263 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 264 | Elf_Scn *scn; |
| 265 | GElf_Shdr shdr; |
| 266 | |
| 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); |
| 272 | if (shdr.sh_addr == relplt_addr |
| 273 | && shdr.sh_size == relplt_size) { |
| 274 | lte->relplt = elf_getdata(scn, NULL); |
| 275 | lte->relplt_count = |
| 276 | shdr.sh_size / shdr.sh_entsize; |
| 277 | if (lte->relplt == NULL |
| 278 | || elf_getdata(scn, lte->relplt) != NULL) |
| 279 | error(EXIT_FAILURE, 0, |
| 280 | "Couldn't get .rel*.plt data from \"%s\"", |
| 281 | filename); |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if (i == lte->ehdr.e_shnum) |
| 287 | error(EXIT_FAILURE, 0, |
| 288 | "Couldn't find .rel*.plt section in \"%s\"", |
| 289 | filename); |
| 290 | |
| 291 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static void do_close_elf(struct ltelf *lte) |
| 296 | { |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 297 | if (lte->lte_flags & LTE_HASH_MALLOCED) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 298 | free((char *)lte->hash); |
| 299 | elf_end(lte->elf); |
| 300 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 303 | static void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 304 | add_library_symbol(GElf_Addr addr, const char *name, |
| 305 | struct library_symbol **library_symbolspp, |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 306 | enum toplt type_of_plt, int is_weak) |
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 | struct library_symbol *s; |
| 309 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 310 | if (s == NULL) |
| 311 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 312 | |
| 313 | s->needs_init = 1; |
| 314 | s->is_weak = is_weak; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 315 | s->plt_type = type_of_plt; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 316 | s->next = *library_symbolspp; |
| 317 | s->enter_addr = (void *)(uintptr_t) addr; |
| 318 | s->brkpnt = NULL; |
| 319 | s->name = (char *)(s + 1); |
| 320 | strcpy(s->name, name); |
| 321 | *library_symbolspp = s; |
| 322 | |
| 323 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 324 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 325 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 326 | static int in_load_libraries(const char *name, struct ltelf *lte) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 327 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 328 | size_t i; |
| 329 | unsigned long hash; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 330 | #ifdef SHT_GNU_HASH |
| 331 | unsigned long gnu_hash; |
| 332 | #endif |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 333 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 334 | if (!library_num) |
| 335 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 336 | |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 337 | hash = elf_hash((const unsigned char *)name); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 338 | #ifdef SHT_GNU_HASH |
| 339 | gnu_hash = elf_gnu_hash((const unsigned char *)name); |
| 340 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 341 | for (i = 1; i <= library_num; ++i) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 342 | if (lte[i].hash == NULL) |
| 343 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 344 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 345 | #ifdef SHT_GNU_HASH |
| 346 | if (lte[i].hash_type == SHT_GNU_HASH) { |
| 347 | Elf32_Word * hashbase = lte[i].hash; |
| 348 | Elf32_Word nbuckets = *hashbase++; |
| 349 | Elf32_Word symbias = *hashbase++; |
| 350 | Elf32_Word bitmask_nwords = *hashbase++; |
| 351 | Elf32_Word bitmask_idxbits = bitmask_nwords - 1; |
| 352 | Elf32_Word shift = *hashbase++; |
| 353 | Elf32_Word * buckets; |
| 354 | Elf32_Word * chain_zero; |
| 355 | Elf32_Word bucket; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 356 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 357 | hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords; |
| 358 | buckets = hashbase; |
| 359 | hashbase += nbuckets; |
| 360 | chain_zero = hashbase - symbias; |
| 361 | bucket = buckets[gnu_hash % nbuckets]; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 362 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame^] | 363 | if (bucket != 0) { |
| 364 | const Elf32_Word *hasharr = &chain_zero[bucket]; |
| 365 | do |
| 366 | if ((*hasharr & ~1u) == (gnu_hash & ~1u)) { |
| 367 | int symidx = hasharr - chain_zero; |
| 368 | GElf_Sym sym; |
| 369 | |
| 370 | if (gelf_getsym(lte[i].dynsym, symidx, &sym) == NULL) |
| 371 | error(EXIT_FAILURE, 0, |
| 372 | "Couldn't get symbol from .dynsym"); |
| 373 | |
| 374 | if (sym.st_value != 0 |
| 375 | && sym.st_shndx != SHN_UNDEF |
| 376 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 377 | return 1; |
| 378 | } |
| 379 | while ((*hasharr++ & 1u) == 0); |
| 380 | } |
| 381 | } else |
| 382 | #endif |
| 383 | { |
| 384 | Elf32_Word nbuckets, symndx; |
| 385 | Elf32_Word *buckets, *chain; |
| 386 | nbuckets = lte[i].hash[0]; |
| 387 | buckets = <e[i].hash[2]; |
| 388 | chain = <e[i].hash[2 + nbuckets]; |
| 389 | |
| 390 | for (symndx = buckets[hash % nbuckets]; |
| 391 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 392 | GElf_Sym sym; |
| 393 | |
| 394 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 395 | error(EXIT_FAILURE, 0, |
| 396 | "Couldn't get symbol from .dynsym"); |
| 397 | |
| 398 | if (sym.st_value != 0 |
| 399 | && sym.st_shndx != SHN_UNDEF |
| 400 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 401 | return 1; |
| 402 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 403 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 404 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 405 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 406 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 407 | |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 408 | static GElf_Addr opd2addr(struct ltelf *lte, void *addr) |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 409 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 410 | long base; |
| 411 | long offset; |
| 412 | GElf_Addr ret_val; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 413 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 414 | if (!lte->opd) |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 415 | return (GElf_Addr) (long) addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 416 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 417 | base = (long)lte->opd->d_buf; |
| 418 | offset = (long)addr - (long)lte->opd_addr; |
| 419 | if (offset > lte->opd_size) |
| 420 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 421 | |
| 422 | ret_val = (GElf_Addr) * (long *)(base + offset); |
| 423 | return ret_val; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 426 | struct library_symbol *read_elf(struct process *proc) |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 427 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 428 | struct library_symbol *library_symbols = NULL; |
| 429 | struct ltelf lte[MAX_LIBRARY + 1]; |
| 430 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 431 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 432 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 433 | struct opt_x_t *main_cheat; |
| 434 | int exit_out = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 435 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 436 | elf_version(EV_CURRENT); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 437 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 438 | do_init_elf(lte, proc->filename); |
| 439 | proc->e_machine = lte->ehdr.e_machine; |
| 440 | for (i = 0; i < library_num; ++i) |
| 441 | do_init_elf(<e[i + 1], library[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 442 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 443 | for (i = 0; i < lte->relplt_count; ++i) { |
| 444 | GElf_Rel rel; |
| 445 | GElf_Rela rela; |
| 446 | GElf_Sym sym; |
| 447 | GElf_Addr addr; |
| 448 | void *ret; |
| 449 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 450 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 451 | if (lte->relplt->d_type == ELF_T_REL) { |
| 452 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 453 | rela.r_offset = rel.r_offset; |
| 454 | rela.r_info = rel.r_info; |
| 455 | rela.r_addend = 0; |
| 456 | } else |
| 457 | ret = gelf_getrela(lte->relplt, i, &rela); |
| 458 | |
| 459 | if (ret == NULL |
| 460 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 461 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 462 | &sym) == NULL) |
| 463 | error(EXIT_FAILURE, 0, |
| 464 | "Couldn't get relocation from \"%s\"", |
| 465 | proc->filename); |
| 466 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 467 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 468 | if (!sym.st_value && PLTs_initialized_by_here) |
| 469 | proc->need_to_reinitialize_breakpoints = 1; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 470 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 471 | |
| 472 | name = lte->dynstr + sym.st_name; |
| 473 | if (in_load_libraries(name, lte)) { |
| 474 | addr = arch_plt_sym_val(lte, i, &rela); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 475 | add_library_symbol(addr, name, &library_symbols, |
| 476 | (PLTS_ARE_EXECUTABLE(lte) |
| 477 | ? LS_TOPLT_EXEC : LS_TOPLT_POINT), |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 478 | ELF64_ST_BIND(sym.st_info) != 0); |
| 479 | if (!lib_tail) |
| 480 | lib_tail = &(library_symbols->next); |
| 481 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 482 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 483 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 484 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 485 | if (proc->need_to_reinitialize_breakpoints) { |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 486 | /* Add "PLTs_initialized_by_here" to opt_x list, if not |
| 487 | already there. */ |
Paul Gilliam | 2b2507a | 2006-04-07 18:32:07 +0200 | [diff] [blame] | 488 | main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t)); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 489 | if (main_cheat == NULL) |
| 490 | error(EXIT_FAILURE, 0, "Couldn allocate memory"); |
| 491 | main_cheat->next = opt_x; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 492 | main_cheat->found = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 493 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 494 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 495 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 496 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0 |
| 497 | && main_cheat) { |
| 498 | free(main_cheat); |
| 499 | main_cheat = NULL; |
| 500 | break; |
| 501 | } |
| 502 | if (main_cheat) |
| 503 | opt_x = main_cheat; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 504 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 505 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 506 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 507 | for (i = 0; i < lte->symtab_count; ++i) { |
| 508 | GElf_Sym sym; |
| 509 | GElf_Addr addr; |
| 510 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 511 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 512 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 513 | error(EXIT_FAILURE, 0, |
| 514 | "Couldn't get symbol from \"%s\"", |
| 515 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 516 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 517 | name = lte->strtab + sym.st_name; |
| 518 | addr = sym.st_value; |
| 519 | if (!addr) |
| 520 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 521 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 522 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 523 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 524 | /* FIXME: Should be able to use &library_symbols as above. But |
| 525 | when you do, none of the real library symbols cause breaks. */ |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 526 | add_library_symbol(opd2addr(lte, (void*)addr), |
| 527 | name, lib_tail, LS_TOPLT_NONE, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 528 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | } |
| 532 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 533 | if ( ! xptr->found) { |
| 534 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 535 | #ifdef PLT_REINITALISATION_BP |
| 536 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 537 | if (lte->ehdr.e_entry) { |
| 538 | add_library_symbol ( |
| 539 | elf_plt2addr (lte, (void*)(long) |
| 540 | lte->ehdr.e_entry), |
| 541 | PLTs_initialized_by_here, |
| 542 | lib_tail, 1, 0); |
| 543 | fprintf (stderr, "WARNING: Using e_ent" |
| 544 | "ry from elf header (%p) for " |
| 545 | "address of \"%s\"\n", (void*) |
| 546 | (long) lte->ehdr.e_entry, |
| 547 | PLTs_initialized_by_here); |
| 548 | continue; |
| 549 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 550 | badthing = "ERROR"; |
| 551 | exit_out = 1; |
| 552 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 553 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 554 | fprintf (stderr, |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 555 | "%s: Couldn't find symbol \"%s\" in file \"%s" |
| 556 | "\"\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 557 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 558 | if (exit_out) { |
| 559 | exit (1); |
| 560 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 561 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 562 | for (i = 0; i < library_num + 1; ++i) |
| 563 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 564 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 565 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 566 | } |