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