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 | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame^] | 15 | #include "common.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); |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 26 | static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr 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 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 32 | static void |
| 33 | do_init_elf(struct ltelf *lte, const char *filename) { |
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 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 38 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 39 | debug(1, "Reading ELF from %s...", filename); |
Juan Cespedes | 1afec69 | 1997-08-23 21:31:46 +0200 | [diff] [blame] | 40 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 41 | memset(lte, 0, sizeof(*lte)); |
| 42 | lte->fd = open(filename, O_RDONLY); |
| 43 | if (lte->fd == -1) |
| 44 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 45 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 46 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 47 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 48 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 49 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 50 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 51 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 52 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 53 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 54 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 55 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 56 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 57 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 58 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 59 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 60 | error(EXIT_FAILURE, 0, |
| 61 | "\"%s\" is not an ELF executable nor shared library", |
| 62 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 63 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 64 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 65 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 66 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 67 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 68 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 69 | #endif |
| 70 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 71 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 72 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 73 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 74 | ) |
| 75 | error(EXIT_FAILURE, 0, |
| 76 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 77 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 78 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 79 | Elf_Scn *scn; |
| 80 | GElf_Shdr shdr; |
| 81 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 82 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 83 | scn = elf_getscn(lte->elf, i); |
| 84 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 85 | error(EXIT_FAILURE, 0, |
| 86 | "Couldn't get section header from \"%s\"", |
| 87 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 88 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 89 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 90 | if (name == NULL) |
| 91 | error(EXIT_FAILURE, 0, |
| 92 | "Couldn't get section header from \"%s\"", |
| 93 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 94 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 95 | if (shdr.sh_type == SHT_SYMTAB) { |
| 96 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 97 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 98 | lte->symtab = elf_getdata(scn, NULL); |
| 99 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 100 | if ((lte->symtab == NULL |
| 101 | || elf_getdata(scn, lte->symtab) != NULL) |
| 102 | && opt_x != NULL) |
| 103 | error(EXIT_FAILURE, 0, |
| 104 | "Couldn't get .symtab data from \"%s\"", |
| 105 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 106 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 107 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 108 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 109 | error(EXIT_FAILURE, 0, |
| 110 | "Couldn't get section header from \"%s\"", |
| 111 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 112 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 113 | data = elf_getdata(scn, NULL); |
| 114 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 115 | || shdr.sh_size != data->d_size || data->d_off) |
| 116 | error(EXIT_FAILURE, 0, |
| 117 | "Couldn't get .strtab data from \"%s\"", |
| 118 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 119 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 120 | lte->strtab = data->d_buf; |
| 121 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 122 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 123 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 124 | lte->dynsym = elf_getdata(scn, NULL); |
| 125 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 126 | if (lte->dynsym == NULL |
| 127 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 128 | error(EXIT_FAILURE, 0, |
| 129 | "Couldn't get .dynsym data from \"%s\"", |
| 130 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 131 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 132 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 133 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 134 | error(EXIT_FAILURE, 0, |
| 135 | "Couldn't get section header from \"%s\"", |
| 136 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 137 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 138 | data = elf_getdata(scn, NULL); |
| 139 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 140 | || shdr.sh_size != data->d_size || data->d_off) |
| 141 | error(EXIT_FAILURE, 0, |
| 142 | "Couldn't get .dynstr data from \"%s\"", |
| 143 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 144 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 145 | lte->dynstr = data->d_buf; |
| 146 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 147 | Elf_Data *data; |
| 148 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 149 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 150 | data = elf_getdata(scn, NULL); |
| 151 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 152 | error(EXIT_FAILURE, 0, |
| 153 | "Couldn't get .dynamic data from \"%s\"", |
| 154 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 155 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 156 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 157 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 158 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 159 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 160 | error(EXIT_FAILURE, 0, |
| 161 | "Couldn't get .dynamic data from \"%s\"", |
| 162 | filename); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 163 | #ifdef __mips__ |
| 164 | /** |
| 165 | MIPS ABI Supplement: |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 166 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 167 | DT_PLTGOT This member holds the address of the .got section. |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 168 | |
| 169 | DT_MIPS_SYMTABNO This member holds the number of entries in the |
| 170 | .dynsym section. |
| 171 | |
| 172 | DT_MIPS_LOCAL_GOTNO This member holds the number of local global |
| 173 | offset table entries. |
| 174 | |
| 175 | DT_MIPS_GOTSYM This member holds the index of the first dyamic |
| 176 | symbol table entry that corresponds to an entry in the gobal offset |
| 177 | table. |
| 178 | |
| 179 | */ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 180 | if(dyn.d_tag==DT_PLTGOT){ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 181 | lte->pltgot_addr=dyn.d_un.d_ptr; |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 182 | } |
| 183 | if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){ |
| 184 | lte->mips_local_gotno=dyn.d_un.d_val; |
| 185 | } |
| 186 | if(dyn.d_tag==DT_MIPS_GOTSYM){ |
| 187 | lte->mips_gotsym=dyn.d_un.d_val; |
| 188 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 189 | #endif // __mips__ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 190 | if (dyn.d_tag == DT_JMPREL) |
| 191 | relplt_addr = dyn.d_un.d_ptr; |
| 192 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 193 | relplt_size = dyn.d_un.d_val; |
| 194 | } |
| 195 | } else if (shdr.sh_type == SHT_HASH) { |
| 196 | Elf_Data *data; |
| 197 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 198 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 199 | lte->hash_type = SHT_HASH; |
| 200 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 201 | data = elf_getdata(scn, NULL); |
| 202 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 203 | || data->d_off || data->d_size != shdr.sh_size) |
| 204 | error(EXIT_FAILURE, 0, |
| 205 | "Couldn't get .hash data from \"%s\"", |
| 206 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 207 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 208 | if (shdr.sh_entsize == 4) { |
| 209 | /* Standard conforming ELF. */ |
| 210 | if (data->d_type != ELF_T_WORD) |
| 211 | error(EXIT_FAILURE, 0, |
| 212 | "Couldn't get .hash data from \"%s\"", |
| 213 | filename); |
| 214 | lte->hash = (Elf32_Word *) data->d_buf; |
| 215 | } else if (shdr.sh_entsize == 8) { |
| 216 | /* Alpha or s390x. */ |
| 217 | Elf32_Word *dst, *src; |
| 218 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 219 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 220 | lte->hash = (Elf32_Word *) |
| 221 | malloc(hash_count * sizeof(Elf32_Word)); |
| 222 | if (lte->hash == NULL) |
| 223 | error(EXIT_FAILURE, 0, |
| 224 | "Couldn't convert .hash section from \"%s\"", |
| 225 | filename); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 226 | lte->lte_flags |= LTE_HASH_MALLOCED; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 227 | dst = lte->hash; |
| 228 | src = (Elf32_Word *) data->d_buf; |
| 229 | if ((data->d_type == ELF_T_WORD |
| 230 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 231 | || (data->d_type == ELF_T_XWORD |
| 232 | && lte->ehdr.e_ident[EI_DATA] == |
| 233 | ELFDATA2MSB)) |
| 234 | ++src; |
| 235 | for (j = 0; j < hash_count; ++j, src += 2) |
| 236 | *dst++ = *src; |
| 237 | } else |
| 238 | error(EXIT_FAILURE, 0, |
| 239 | "Unknown .hash sh_entsize in \"%s\"", |
| 240 | filename); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 241 | } else if (shdr.sh_type == SHT_GNU_HASH |
| 242 | && lte->hash == NULL) { |
| 243 | Elf_Data *data; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 244 | |
| 245 | lte->hash_type = SHT_GNU_HASH; |
| 246 | |
| 247 | if (shdr.sh_entsize != 0 |
| 248 | && shdr.sh_entsize != 4) { |
| 249 | error(EXIT_FAILURE, 0, |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 250 | ".gnu.hash sh_entsize in \"%s\" should be 4, but is %llu", |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 251 | filename, shdr.sh_entsize); |
| 252 | } |
| 253 | |
| 254 | data = elf_getdata(scn, NULL); |
| 255 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 256 | || data->d_off || data->d_size != shdr.sh_size) |
| 257 | error(EXIT_FAILURE, 0, |
| 258 | "Couldn't get .gnu.hash data from \"%s\"", |
| 259 | filename); |
| 260 | |
| 261 | lte->hash = (Elf32_Word *) data->d_buf; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 262 | } else if (shdr.sh_type == SHT_PROGBITS |
| 263 | || shdr.sh_type == SHT_NOBITS) { |
| 264 | if (strcmp(name, ".plt") == 0) { |
| 265 | lte->plt_addr = shdr.sh_addr; |
| 266 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 267 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 268 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 269 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 270 | } |
| 271 | #ifdef ARCH_SUPPORTS_OPD |
| 272 | else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 273 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 274 | lte->opd_size = shdr.sh_size; |
| 275 | lte->opd = elf_rawdata(scn, NULL); |
| 276 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 277 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 278 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 281 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 282 | error(EXIT_FAILURE, 0, |
| 283 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 284 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 285 | if (!relplt_addr || !lte->plt_addr) { |
| 286 | debug(1, "%s has no PLT relocations", filename); |
| 287 | lte->relplt = NULL; |
| 288 | lte->relplt_count = 0; |
| 289 | } else { |
| 290 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 291 | Elf_Scn *scn; |
| 292 | GElf_Shdr shdr; |
| 293 | |
| 294 | scn = elf_getscn(lte->elf, i); |
| 295 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 296 | error(EXIT_FAILURE, 0, |
| 297 | "Couldn't get section header from \"%s\"", |
| 298 | filename); |
| 299 | if (shdr.sh_addr == relplt_addr |
| 300 | && shdr.sh_size == relplt_size) { |
| 301 | lte->relplt = elf_getdata(scn, NULL); |
| 302 | lte->relplt_count = |
| 303 | shdr.sh_size / shdr.sh_entsize; |
| 304 | if (lte->relplt == NULL |
| 305 | || elf_getdata(scn, lte->relplt) != NULL) |
| 306 | error(EXIT_FAILURE, 0, |
| 307 | "Couldn't get .rel*.plt data from \"%s\"", |
| 308 | filename); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (i == lte->ehdr.e_shnum) |
| 314 | error(EXIT_FAILURE, 0, |
| 315 | "Couldn't find .rel*.plt section in \"%s\"", |
| 316 | filename); |
| 317 | |
| 318 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 319 | } |
| 320 | } |
| 321 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 322 | static void |
| 323 | do_close_elf(struct ltelf *lte) { |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 324 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 325 | if (lte->lte_flags & LTE_HASH_MALLOCED) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 326 | free((char *)lte->hash); |
| 327 | elf_end(lte->elf); |
| 328 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 331 | static void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 332 | add_library_symbol(GElf_Addr addr, const char *name, |
| 333 | struct library_symbol **library_symbolspp, |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 334 | enum toplt type_of_plt, int is_weak) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 335 | struct library_symbol *s; |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 336 | |
| 337 | debug(DEBUG_FUNCTION, "add_library_symbol()"); |
| 338 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 339 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 340 | if (s == NULL) |
| 341 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 342 | |
| 343 | s->needs_init = 1; |
| 344 | s->is_weak = is_weak; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 345 | s->plt_type = type_of_plt; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 346 | s->next = *library_symbolspp; |
| 347 | s->enter_addr = (void *)(uintptr_t) addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 348 | s->name = (char *)(s + 1); |
| 349 | strcpy(s->name, name); |
| 350 | *library_symbolspp = s; |
| 351 | |
| 352 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 353 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 354 | |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 355 | /* stolen from elfutils-0.123 */ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 356 | static unsigned long |
| 357 | private_elf_gnu_hash(const char *name) { |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 358 | unsigned long h = 5381; |
| 359 | const unsigned char *string = (const unsigned char *)name; |
| 360 | unsigned char c; |
| 361 | for (c = *string; c; c = *++string) |
| 362 | h = h * 33 + c; |
| 363 | return h & 0xffffffff; |
| 364 | } |
| 365 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 366 | static int |
| 367 | in_load_libraries(const char *name, struct ltelf *lte) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 368 | size_t i; |
| 369 | unsigned long hash; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 370 | unsigned long gnu_hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 371 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 372 | if (!library_num) |
| 373 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 374 | |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 375 | hash = elf_hash((const unsigned char *)name); |
Petr Machata | 07bc4d1 | 2006-11-30 14:47:40 +0100 | [diff] [blame] | 376 | gnu_hash = private_elf_gnu_hash(name); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 377 | for (i = 1; i <= library_num; ++i) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 378 | if (lte[i].hash == NULL) |
| 379 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 380 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 381 | if (lte[i].hash_type == SHT_GNU_HASH) { |
| 382 | Elf32_Word * hashbase = lte[i].hash; |
| 383 | Elf32_Word nbuckets = *hashbase++; |
| 384 | Elf32_Word symbias = *hashbase++; |
| 385 | Elf32_Word bitmask_nwords = *hashbase++; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 386 | Elf32_Word * buckets; |
| 387 | Elf32_Word * chain_zero; |
| 388 | Elf32_Word bucket; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 389 | |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 390 | // +1 for skipped `shift' |
| 391 | hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 392 | buckets = hashbase; |
| 393 | hashbase += nbuckets; |
| 394 | chain_zero = hashbase - symbias; |
| 395 | bucket = buckets[gnu_hash % nbuckets]; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 396 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 397 | if (bucket != 0) { |
| 398 | const Elf32_Word *hasharr = &chain_zero[bucket]; |
| 399 | do |
| 400 | if ((*hasharr & ~1u) == (gnu_hash & ~1u)) { |
| 401 | int symidx = hasharr - chain_zero; |
| 402 | GElf_Sym sym; |
| 403 | |
| 404 | if (gelf_getsym(lte[i].dynsym, symidx, &sym) == NULL) |
| 405 | error(EXIT_FAILURE, 0, |
| 406 | "Couldn't get symbol from .dynsym"); |
| 407 | |
| 408 | if (sym.st_value != 0 |
| 409 | && sym.st_shndx != SHN_UNDEF |
| 410 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 411 | return 1; |
| 412 | } |
| 413 | while ((*hasharr++ & 1u) == 0); |
| 414 | } |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 415 | } else { |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 416 | Elf32_Word nbuckets, symndx; |
| 417 | Elf32_Word *buckets, *chain; |
| 418 | nbuckets = lte[i].hash[0]; |
| 419 | buckets = <e[i].hash[2]; |
| 420 | chain = <e[i].hash[2 + nbuckets]; |
| 421 | |
| 422 | for (symndx = buckets[hash % nbuckets]; |
| 423 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 424 | GElf_Sym sym; |
| 425 | |
| 426 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 427 | error(EXIT_FAILURE, 0, |
| 428 | "Couldn't get symbol from .dynsym"); |
| 429 | |
| 430 | if (sym.st_value != 0 |
| 431 | && sym.st_shndx != SHN_UNDEF |
| 432 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 433 | return 1; |
| 434 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 435 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 436 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 437 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 438 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 439 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 440 | static GElf_Addr |
| 441 | opd2addr(struct ltelf *lte, GElf_Addr addr) { |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 442 | #ifdef ARCH_SUPPORTS_OPD |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 443 | unsigned long base, offset; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 444 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 445 | if (!lte->opd) |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 446 | return addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 447 | |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 448 | base = (unsigned long)lte->opd->d_buf; |
| 449 | offset = (unsigned long)addr - (unsigned long)lte->opd_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 450 | if (offset > lte->opd_size) |
| 451 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 452 | |
Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 453 | return *(GElf_Addr*)(base + offset); |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 454 | #else //!ARCH_SUPPORTS_OPD |
| 455 | return addr; |
| 456 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 459 | struct library_symbol * |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 460 | read_elf(Process *proc) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 461 | struct library_symbol *library_symbols = NULL; |
| 462 | struct ltelf lte[MAX_LIBRARY + 1]; |
| 463 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 464 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 465 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 466 | int exit_out = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 467 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 468 | debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename); |
| 469 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 470 | elf_version(EV_CURRENT); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 471 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 472 | do_init_elf(lte, proc->filename); |
| 473 | proc->e_machine = lte->ehdr.e_machine; |
| 474 | for (i = 0; i < library_num; ++i) |
| 475 | do_init_elf(<e[i + 1], library[i]); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 476 | #ifdef __mips__ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 477 | // MIPS doesn't use the PLT and the GOT entries get changed |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 478 | // on startup. |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 479 | proc->need_to_reinitialize_breakpoints = 1; |
| 480 | for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){ |
| 481 | GElf_Sym sym; |
| 482 | const char *name; |
| 483 | GElf_Addr addr = arch_plt_sym_val(lte, i, 0); |
| 484 | if (gelf_getsym(lte->dynsym, i, &sym) == NULL){ |
| 485 | error(EXIT_FAILURE, 0, |
| 486 | "Couldn't get relocation from \"%s\"", |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 487 | proc->filename); |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 488 | } |
| 489 | name=lte->dynstr+sym.st_name; |
| 490 | if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){ |
| 491 | debug(2,"sym %s not a function",name); |
| 492 | continue; |
| 493 | } |
| 494 | add_library_symbol(addr, name, &library_symbols, 0, |
| 495 | ELF64_ST_BIND(sym.st_info) != 0); |
| 496 | if (!lib_tail) |
| 497 | lib_tail = &(library_symbols->next); |
| 498 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 499 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 500 | for (i = 0; i < lte->relplt_count; ++i) { |
| 501 | GElf_Rel rel; |
| 502 | GElf_Rela rela; |
| 503 | GElf_Sym sym; |
| 504 | GElf_Addr addr; |
| 505 | void *ret; |
| 506 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 507 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 508 | if (lte->relplt->d_type == ELF_T_REL) { |
| 509 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 510 | rela.r_offset = rel.r_offset; |
| 511 | rela.r_info = rel.r_info; |
| 512 | rela.r_addend = 0; |
| 513 | } else |
| 514 | ret = gelf_getrela(lte->relplt, i, &rela); |
| 515 | |
| 516 | if (ret == NULL |
| 517 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 518 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 519 | &sym) == NULL) |
| 520 | error(EXIT_FAILURE, 0, |
| 521 | "Couldn't get relocation from \"%s\"", |
| 522 | proc->filename); |
| 523 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 524 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 525 | if (!sym.st_value && PLTs_initialized_by_here) |
| 526 | proc->need_to_reinitialize_breakpoints = 1; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 527 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 528 | |
| 529 | name = lte->dynstr + sym.st_name; |
| 530 | if (in_load_libraries(name, lte)) { |
| 531 | addr = arch_plt_sym_val(lte, i, &rela); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 532 | add_library_symbol(addr, name, &library_symbols, |
| 533 | (PLTS_ARE_EXECUTABLE(lte) |
| 534 | ? LS_TOPLT_EXEC : LS_TOPLT_POINT), |
Petr Machata | 7003fee | 2006-07-18 13:06:26 +0200 | [diff] [blame] | 535 | ELF64_ST_BIND(sym.st_info) == STB_WEAK); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 536 | if (!lib_tail) |
| 537 | lib_tail = &(library_symbols->next); |
| 538 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 539 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 540 | #endif // !__mips__ |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 541 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 542 | struct opt_x_t *main_cheat; |
| 543 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 544 | if (proc->need_to_reinitialize_breakpoints) { |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 545 | /* Add "PLTs_initialized_by_here" to opt_x list, if not |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 546 | already there. */ |
Paul Gilliam | 2b2507a | 2006-04-07 18:32:07 +0200 | [diff] [blame] | 547 | main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t)); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 548 | if (main_cheat == NULL) |
Petr Machata | 7003fee | 2006-07-18 13:06:26 +0200 | [diff] [blame] | 549 | error(EXIT_FAILURE, 0, "Couldn't allocate memory"); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 550 | main_cheat->next = opt_x; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 551 | main_cheat->found = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 552 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 553 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 554 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 555 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0 |
| 556 | && main_cheat) { |
| 557 | free(main_cheat); |
| 558 | main_cheat = NULL; |
| 559 | break; |
| 560 | } |
| 561 | if (main_cheat) |
| 562 | opt_x = main_cheat; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 563 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 564 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 565 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 566 | for (i = 0; i < lte->symtab_count; ++i) { |
| 567 | GElf_Sym sym; |
| 568 | GElf_Addr addr; |
| 569 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 570 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 571 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 572 | error(EXIT_FAILURE, 0, |
| 573 | "Couldn't get symbol from \"%s\"", |
| 574 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 575 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 576 | name = lte->strtab + sym.st_name; |
| 577 | addr = sym.st_value; |
| 578 | if (!addr) |
| 579 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 580 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 581 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 582 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 583 | /* FIXME: Should be able to use &library_symbols as above. But |
| 584 | when you do, none of the real library symbols cause breaks. */ |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 585 | add_library_symbol(opd2addr(lte, addr), |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 586 | name, lib_tail, LS_TOPLT_NONE, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 587 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 592 | if ( ! xptr->found) { |
| 593 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 594 | #ifdef PLT_REINITALISATION_BP |
| 595 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 596 | if (lte->ehdr.e_entry) { |
| 597 | add_library_symbol ( |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 598 | opd2addr (lte, lte->ehdr.e_entry), |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 599 | PLTs_initialized_by_here, |
| 600 | lib_tail, 1, 0); |
| 601 | fprintf (stderr, "WARNING: Using e_ent" |
| 602 | "ry from elf header (%p) for " |
| 603 | "address of \"%s\"\n", (void*) |
| 604 | (long) lte->ehdr.e_entry, |
| 605 | PLTs_initialized_by_here); |
| 606 | continue; |
| 607 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 608 | badthing = "ERROR"; |
| 609 | exit_out = 1; |
| 610 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 611 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 612 | fprintf (stderr, |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 613 | "%s: Couldn't find symbol \"%s\" in file \"%s" |
| 614 | "\"\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 615 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 616 | if (exit_out) { |
| 617 | exit (1); |
| 618 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 619 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 620 | for (i = 0; i < library_num + 1; ++i) |
| 621 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 622 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 623 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 624 | } |