Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 1 | # include "config.h" |
Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 2 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 3 | #include <endian.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 4 | #include <errno.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 5 | #include <error.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 6 | #include <fcntl.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 7 | #include <gelf.h> |
| 8 | #include <stdint.h> |
| 9 | #include <stdlib.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 10 | #include <string.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 11 | #include <unistd.h> |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 12 | #include <assert.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 13 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 14 | #include "common.h" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 15 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 16 | void do_init_elf(struct ltelf *lte, const char *filename); |
| 17 | void do_close_elf(struct ltelf *lte); |
| 18 | void add_library_symbol(GElf_Addr addr, const char *name, |
| 19 | struct library_symbol **library_symbolspp, |
| 20 | enum toplt type_of_plt, int is_weak); |
| 21 | int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym); |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 22 | static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr addr); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 23 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 24 | struct library_symbol *library_symbols = NULL; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 25 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 26 | extern char *PLTs_initialized_by_here; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 27 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 28 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 29 | #ifndef DT_PPC_GOT |
| 30 | # define DT_PPC_GOT (DT_LOPROC + 0) |
| 31 | #endif |
| 32 | |
| 33 | #define PPC_PLT_STUB_SIZE 16 |
| 34 | |
| 35 | static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr) |
| 36 | { |
| 37 | Elf_Data *data = elf_getdata(scn, NULL); |
| 38 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 39 | || data->d_off || data->d_size != shdr->sh_size) |
| 40 | return NULL; |
| 41 | return data; |
| 42 | } |
| 43 | |
| 44 | static int inside(GElf_Addr addr, GElf_Shdr *shdr) |
| 45 | { |
| 46 | return addr >= shdr->sh_addr |
| 47 | && addr < shdr->sh_addr + shdr->sh_size; |
| 48 | } |
| 49 | |
| 50 | static int maybe_pick_section(GElf_Addr addr, |
| 51 | Elf_Scn *in_sec, GElf_Shdr *in_shdr, |
| 52 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 53 | { |
| 54 | if (inside (addr, in_shdr)) { |
| 55 | *tgt_sec = in_sec; |
| 56 | *tgt_shdr = *in_shdr; |
| 57 | return 1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int get_section_covering(struct ltelf *lte, GElf_Addr addr, |
| 63 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 64 | { |
| 65 | int i; |
| 66 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 67 | Elf_Scn *scn; |
| 68 | GElf_Shdr shdr; |
| 69 | |
| 70 | scn = elf_getscn(lte->elf, i); |
| 71 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { |
| 72 | debug(1, "Couldn't read section or header."); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr)) |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static GElf_Addr read32be(Elf_Data *data, size_t offset) |
| 84 | { |
| 85 | if (data->d_size < offset + 4) { |
| 86 | debug(1, "Not enough data to read 32bit value at offset %zd.", |
| 87 | offset); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | unsigned char const *buf = data->d_buf + offset; |
| 92 | return ((Elf32_Word)buf[0] << 24) |
| 93 | | ((Elf32_Word)buf[1] << 16) |
| 94 | | ((Elf32_Word)buf[2] << 8) |
| 95 | | ((Elf32_Word)buf[3]); |
| 96 | } |
| 97 | |
| 98 | static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, |
| 99 | Elf_Data *plt_data) |
| 100 | { |
| 101 | Elf_Scn *ppcgot_sec = NULL; |
| 102 | GElf_Shdr ppcgot_shdr; |
| 103 | if (ppcgot != 0 |
| 104 | && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr)) |
| 105 | // xxx should be the log out |
| 106 | fprintf(stderr, |
| 107 | "DT_PPC_GOT=%#lx, but no such section found.\n", |
| 108 | ppcgot); |
| 109 | |
| 110 | if (ppcgot_sec != NULL) { |
| 111 | Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr); |
| 112 | if (data == NULL |
| 113 | || data->d_size < 8 ) |
| 114 | debug(1, "Couldn't read GOT data."); |
| 115 | else { |
| 116 | // where PPCGOT begins in .got |
| 117 | size_t offset = ppcgot - ppcgot_shdr.sh_addr; |
| 118 | GElf_Addr glink_vma = read32be(data, offset + 4); |
| 119 | if (glink_vma != 0) { |
| 120 | debug(1, "PPC GOT glink_vma address: %#lx", |
| 121 | glink_vma); |
| 122 | return glink_vma; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (plt_data != NULL) { |
| 128 | GElf_Addr glink_vma = read32be(plt_data, 0); |
| 129 | debug(1, ".plt glink_vma address: %#lx", glink_vma); |
| 130 | return glink_vma; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 136 | void |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 137 | do_init_elf(struct ltelf *lte, const char *filename) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 138 | int i; |
| 139 | GElf_Addr relplt_addr = 0; |
| 140 | size_t relplt_size = 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 141 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 142 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 143 | debug(1, "Reading ELF from %s...", filename); |
Juan Cespedes | 1afec69 | 1997-08-23 21:31:46 +0200 | [diff] [blame] | 144 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 145 | memset(lte, 0, sizeof(*lte)); |
| 146 | lte->fd = open(filename, O_RDONLY); |
| 147 | if (lte->fd == -1) |
| 148 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 149 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 150 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 151 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 152 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 153 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 154 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 155 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 156 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 157 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 158 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 159 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 160 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 161 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 162 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 163 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 164 | error(EXIT_FAILURE, 0, |
| 165 | "\"%s\" is not an ELF executable nor shared library", |
| 166 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 167 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 168 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 169 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 170 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 171 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 172 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 173 | #endif |
| 174 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 175 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 176 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 177 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 178 | ) |
| 179 | error(EXIT_FAILURE, 0, |
| 180 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 181 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 182 | Elf_Data *plt_data = NULL; |
| 183 | GElf_Addr ppcgot = 0; |
| 184 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 185 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 186 | Elf_Scn *scn; |
| 187 | GElf_Shdr shdr; |
| 188 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 189 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 190 | scn = elf_getscn(lte->elf, i); |
| 191 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 192 | error(EXIT_FAILURE, 0, |
| 193 | "Couldn't get section header from \"%s\"", |
| 194 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 195 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 196 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 197 | if (name == NULL) |
| 198 | error(EXIT_FAILURE, 0, |
| 199 | "Couldn't get section header from \"%s\"", |
| 200 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 201 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 202 | if (shdr.sh_type == SHT_SYMTAB) { |
| 203 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 204 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 205 | lte->symtab = elf_getdata(scn, NULL); |
| 206 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 207 | if ((lte->symtab == NULL |
| 208 | || elf_getdata(scn, lte->symtab) != NULL) |
| 209 | && opt_x != NULL) |
| 210 | error(EXIT_FAILURE, 0, |
| 211 | "Couldn't get .symtab data from \"%s\"", |
| 212 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 213 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 214 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 215 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 216 | error(EXIT_FAILURE, 0, |
| 217 | "Couldn't get section header from \"%s\"", |
| 218 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 219 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 220 | data = elf_getdata(scn, NULL); |
| 221 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 222 | || shdr.sh_size != data->d_size || data->d_off) |
| 223 | error(EXIT_FAILURE, 0, |
| 224 | "Couldn't get .strtab data from \"%s\"", |
| 225 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 226 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 227 | lte->strtab = data->d_buf; |
| 228 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 229 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 230 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 231 | lte->dynsym = elf_getdata(scn, NULL); |
| 232 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 233 | if (lte->dynsym == NULL |
| 234 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 235 | error(EXIT_FAILURE, 0, |
| 236 | "Couldn't get .dynsym data from \"%s\"", |
| 237 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 238 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 239 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 240 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 241 | error(EXIT_FAILURE, 0, |
| 242 | "Couldn't get section header from \"%s\"", |
| 243 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 244 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 245 | data = elf_getdata(scn, NULL); |
| 246 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 247 | || shdr.sh_size != data->d_size || data->d_off) |
| 248 | error(EXIT_FAILURE, 0, |
| 249 | "Couldn't get .dynstr data from \"%s\"", |
| 250 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 251 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 252 | lte->dynstr = data->d_buf; |
| 253 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 254 | Elf_Data *data; |
| 255 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 256 | |
Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 257 | lte->dyn_addr = shdr.sh_addr; |
| 258 | lte->dyn_sz = shdr.sh_size; |
| 259 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 260 | data = elf_getdata(scn, NULL); |
| 261 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 262 | error(EXIT_FAILURE, 0, |
| 263 | "Couldn't get .dynamic data from \"%s\"", |
| 264 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 265 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 266 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 267 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 268 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 269 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 270 | error(EXIT_FAILURE, 0, |
| 271 | "Couldn't get .dynamic data from \"%s\"", |
| 272 | filename); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 273 | #ifdef __mips__ |
| 274 | /** |
| 275 | MIPS ABI Supplement: |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 276 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 277 | DT_PLTGOT This member holds the address of the .got section. |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 278 | |
| 279 | DT_MIPS_SYMTABNO This member holds the number of entries in the |
| 280 | .dynsym section. |
| 281 | |
| 282 | DT_MIPS_LOCAL_GOTNO This member holds the number of local global |
| 283 | offset table entries. |
| 284 | |
| 285 | DT_MIPS_GOTSYM This member holds the index of the first dyamic |
| 286 | symbol table entry that corresponds to an entry in the gobal offset |
| 287 | table. |
| 288 | |
| 289 | */ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 290 | if(dyn.d_tag==DT_PLTGOT){ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 291 | lte->pltgot_addr=dyn.d_un.d_ptr; |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 292 | } |
| 293 | if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){ |
| 294 | lte->mips_local_gotno=dyn.d_un.d_val; |
| 295 | } |
| 296 | if(dyn.d_tag==DT_MIPS_GOTSYM){ |
| 297 | lte->mips_gotsym=dyn.d_un.d_val; |
| 298 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 299 | #endif // __mips__ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 300 | if (dyn.d_tag == DT_JMPREL) |
| 301 | relplt_addr = dyn.d_un.d_ptr; |
| 302 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 303 | relplt_size = dyn.d_un.d_val; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 304 | else if (dyn.d_tag == DT_PPC_GOT) { |
| 305 | ppcgot = dyn.d_un.d_val; |
| 306 | debug(1, "ppcgot %#lx", ppcgot); |
| 307 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 308 | } |
| 309 | } else if (shdr.sh_type == SHT_HASH) { |
| 310 | Elf_Data *data; |
| 311 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 312 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 313 | lte->hash_type = SHT_HASH; |
| 314 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 315 | data = elf_getdata(scn, NULL); |
| 316 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 317 | || data->d_off || data->d_size != shdr.sh_size) |
| 318 | error(EXIT_FAILURE, 0, |
| 319 | "Couldn't get .hash data from \"%s\"", |
| 320 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 321 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 322 | if (shdr.sh_entsize == 4) { |
| 323 | /* Standard conforming ELF. */ |
| 324 | if (data->d_type != ELF_T_WORD) |
| 325 | error(EXIT_FAILURE, 0, |
| 326 | "Couldn't get .hash data from \"%s\"", |
| 327 | filename); |
| 328 | lte->hash = (Elf32_Word *) data->d_buf; |
| 329 | } else if (shdr.sh_entsize == 8) { |
| 330 | /* Alpha or s390x. */ |
| 331 | Elf32_Word *dst, *src; |
| 332 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 333 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 334 | lte->hash = (Elf32_Word *) |
| 335 | malloc(hash_count * sizeof(Elf32_Word)); |
| 336 | if (lte->hash == NULL) |
| 337 | error(EXIT_FAILURE, 0, |
| 338 | "Couldn't convert .hash section from \"%s\"", |
| 339 | filename); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 340 | lte->lte_flags |= LTE_HASH_MALLOCED; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 341 | dst = lte->hash; |
| 342 | src = (Elf32_Word *) data->d_buf; |
| 343 | if ((data->d_type == ELF_T_WORD |
| 344 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 345 | || (data->d_type == ELF_T_XWORD |
| 346 | && lte->ehdr.e_ident[EI_DATA] == |
| 347 | ELFDATA2MSB)) |
| 348 | ++src; |
| 349 | for (j = 0; j < hash_count; ++j, src += 2) |
| 350 | *dst++ = *src; |
| 351 | } else |
| 352 | error(EXIT_FAILURE, 0, |
| 353 | "Unknown .hash sh_entsize in \"%s\"", |
| 354 | filename); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 355 | } else if (shdr.sh_type == SHT_GNU_HASH |
| 356 | && lte->hash == NULL) { |
| 357 | Elf_Data *data; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 358 | |
| 359 | lte->hash_type = SHT_GNU_HASH; |
| 360 | |
| 361 | if (shdr.sh_entsize != 0 |
| 362 | && shdr.sh_entsize != 4) { |
| 363 | error(EXIT_FAILURE, 0, |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 364 | ".gnu.hash sh_entsize in \"%s\" should be 4, but is %llu", |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 365 | filename, shdr.sh_entsize); |
| 366 | } |
| 367 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 368 | data = loaddata(scn, &shdr); |
| 369 | if (data == NULL) |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 370 | error(EXIT_FAILURE, 0, |
| 371 | "Couldn't get .gnu.hash data from \"%s\"", |
| 372 | filename); |
| 373 | |
| 374 | lte->hash = (Elf32_Word *) data->d_buf; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 375 | } else if (shdr.sh_type == SHT_PROGBITS |
| 376 | || shdr.sh_type == SHT_NOBITS) { |
| 377 | if (strcmp(name, ".plt") == 0) { |
| 378 | lte->plt_addr = shdr.sh_addr; |
| 379 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 380 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 381 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 382 | } |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 383 | if (lte->ehdr.e_machine == EM_PPC) { |
| 384 | plt_data = loaddata(scn, &shdr); |
| 385 | if (plt_data == NULL) |
| 386 | fprintf(stderr, |
| 387 | "Can't load .plt data\n"); |
| 388 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 389 | } |
| 390 | #ifdef ARCH_SUPPORTS_OPD |
| 391 | else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 392 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 393 | lte->opd_size = shdr.sh_size; |
| 394 | lte->opd = elf_rawdata(scn, NULL); |
| 395 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 396 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 397 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 400 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 401 | error(EXIT_FAILURE, 0, |
| 402 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 403 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 404 | if (!relplt_addr || !lte->plt_addr) { |
| 405 | debug(1, "%s has no PLT relocations", filename); |
| 406 | lte->relplt = NULL; |
| 407 | lte->relplt_count = 0; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 408 | } else if (relplt_size == 0) { |
| 409 | debug(1, "%s has unknown PLT size", filename); |
| 410 | lte->relplt = NULL; |
| 411 | lte->relplt_count = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 412 | } else { |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 413 | if (lte->ehdr.e_machine == EM_PPC) { |
| 414 | GElf_Addr glink_vma |
| 415 | = get_glink_vma(lte, ppcgot, plt_data); |
| 416 | |
| 417 | assert (relplt_size % 12 == 0); |
| 418 | size_t count = relplt_size / 12; // size of RELA entry |
| 419 | lte->plt_stub_vma = glink_vma |
| 420 | - (GElf_Addr)count * PPC_PLT_STUB_SIZE; |
| 421 | debug(1, "stub_vma is %#lx", lte->plt_stub_vma); |
| 422 | } |
| 423 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 424 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 425 | Elf_Scn *scn; |
| 426 | GElf_Shdr shdr; |
| 427 | |
| 428 | scn = elf_getscn(lte->elf, i); |
| 429 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 430 | error(EXIT_FAILURE, 0, |
| 431 | "Couldn't get section header from \"%s\"", |
| 432 | filename); |
| 433 | if (shdr.sh_addr == relplt_addr |
| 434 | && shdr.sh_size == relplt_size) { |
| 435 | lte->relplt = elf_getdata(scn, NULL); |
| 436 | lte->relplt_count = |
| 437 | shdr.sh_size / shdr.sh_entsize; |
| 438 | if (lte->relplt == NULL |
| 439 | || elf_getdata(scn, lte->relplt) != NULL) |
| 440 | error(EXIT_FAILURE, 0, |
| 441 | "Couldn't get .rel*.plt data from \"%s\"", |
| 442 | filename); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (i == lte->ehdr.e_shnum) |
| 448 | error(EXIT_FAILURE, 0, |
| 449 | "Couldn't find .rel*.plt section in \"%s\"", |
| 450 | filename); |
| 451 | |
| 452 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 453 | } |
| 454 | } |
| 455 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 456 | void |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 457 | do_close_elf(struct ltelf *lte) { |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 458 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 459 | if (lte->lte_flags & LTE_HASH_MALLOCED) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 460 | free((char *)lte->hash); |
| 461 | elf_end(lte->elf); |
| 462 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 465 | void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 466 | add_library_symbol(GElf_Addr addr, const char *name, |
| 467 | struct library_symbol **library_symbolspp, |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 468 | enum toplt type_of_plt, int is_weak) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 469 | struct library_symbol *s; |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 470 | |
| 471 | debug(DEBUG_FUNCTION, "add_library_symbol()"); |
| 472 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 473 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 474 | if (s == NULL) |
| 475 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 476 | |
| 477 | s->needs_init = 1; |
| 478 | s->is_weak = is_weak; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 479 | s->plt_type = type_of_plt; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 480 | s->next = *library_symbolspp; |
| 481 | s->enter_addr = (void *)(uintptr_t) addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 482 | s->name = (char *)(s + 1); |
| 483 | strcpy(s->name, name); |
| 484 | *library_symbolspp = s; |
| 485 | |
| 486 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 487 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 488 | |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 489 | /* stolen from elfutils-0.123 */ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 490 | static unsigned long |
| 491 | private_elf_gnu_hash(const char *name) { |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 492 | unsigned long h = 5381; |
| 493 | const unsigned char *string = (const unsigned char *)name; |
| 494 | unsigned char c; |
| 495 | for (c = *string; c; c = *++string) |
| 496 | h = h * 33 + c; |
| 497 | return h & 0xffffffff; |
| 498 | } |
| 499 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 500 | static int |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 501 | symbol_matches(struct ltelf *lte, size_t lte_i, GElf_Sym *sym, |
| 502 | size_t symidx, const char *name) |
| 503 | { |
| 504 | GElf_Sym tmp_sym; |
| 505 | GElf_Sym *tmp; |
| 506 | |
| 507 | tmp = (sym) ? (sym) : (&tmp_sym); |
| 508 | |
| 509 | if (gelf_getsym(lte[lte_i].dynsym, symidx, tmp) == NULL) |
| 510 | error(EXIT_FAILURE, 0, "Couldn't get symbol from .dynsym"); |
| 511 | else { |
| 512 | tmp->st_value += lte[lte_i].base_addr; |
| 513 | debug(2, "symbol found: %s, %zd, %lx", |
| 514 | name, lte_i, tmp->st_value); |
| 515 | } |
| 516 | return tmp->st_value != 0 |
| 517 | && tmp->st_shndx != SHN_UNDEF |
| 518 | && strcmp(name, lte[lte_i].dynstr + tmp->st_name) == 0; |
| 519 | } |
| 520 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 521 | int |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 522 | in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 523 | size_t i; |
| 524 | unsigned long hash; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 525 | unsigned long gnu_hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 526 | |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 527 | if (!count) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 528 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 529 | |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 530 | hash = elf_hash((const unsigned char *)name); |
Petr Machata | 07bc4d1 | 2006-11-30 14:47:40 +0100 | [diff] [blame] | 531 | gnu_hash = private_elf_gnu_hash(name); |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 532 | |
| 533 | for (i = 0; i < count; ++i) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 534 | if (lte[i].hash == NULL) |
| 535 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 536 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 537 | if (lte[i].hash_type == SHT_GNU_HASH) { |
| 538 | Elf32_Word * hashbase = lte[i].hash; |
| 539 | Elf32_Word nbuckets = *hashbase++; |
| 540 | Elf32_Word symbias = *hashbase++; |
| 541 | Elf32_Word bitmask_nwords = *hashbase++; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 542 | Elf32_Word * buckets; |
| 543 | Elf32_Word * chain_zero; |
| 544 | Elf32_Word bucket; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 545 | |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 546 | // +1 for skipped `shift' |
| 547 | hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 548 | buckets = hashbase; |
| 549 | hashbase += nbuckets; |
| 550 | chain_zero = hashbase - symbias; |
| 551 | bucket = buckets[gnu_hash % nbuckets]; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 552 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 553 | if (bucket != 0) { |
| 554 | const Elf32_Word *hasharr = &chain_zero[bucket]; |
| 555 | do |
| 556 | if ((*hasharr & ~1u) == (gnu_hash & ~1u)) { |
| 557 | int symidx = hasharr - chain_zero; |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 558 | if (symbol_matches(lte, i, |
| 559 | sym, symidx, |
| 560 | name)) |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 561 | return 1; |
| 562 | } |
| 563 | while ((*hasharr++ & 1u) == 0); |
| 564 | } |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 565 | } else { |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 566 | Elf32_Word nbuckets, symndx; |
| 567 | Elf32_Word *buckets, *chain; |
| 568 | nbuckets = lte[i].hash[0]; |
| 569 | buckets = <e[i].hash[2]; |
| 570 | chain = <e[i].hash[2 + nbuckets]; |
| 571 | |
| 572 | for (symndx = buckets[hash % nbuckets]; |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 573 | symndx != STN_UNDEF; symndx = chain[symndx]) |
| 574 | if (symbol_matches(lte, i, sym, symndx, name)) |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 575 | return 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 576 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 577 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 578 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 579 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 580 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 581 | static GElf_Addr |
| 582 | opd2addr(struct ltelf *lte, GElf_Addr addr) { |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 583 | #ifdef ARCH_SUPPORTS_OPD |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 584 | unsigned long base, offset; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 585 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 586 | if (!lte->opd) |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 587 | return addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 588 | |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 589 | base = (unsigned long)lte->opd->d_buf; |
| 590 | offset = (unsigned long)addr - (unsigned long)lte->opd_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 591 | if (offset > lte->opd_size) |
| 592 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 593 | |
Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 594 | return *(GElf_Addr*)(base + offset); |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 595 | #else //!ARCH_SUPPORTS_OPD |
| 596 | return addr; |
| 597 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 600 | struct library_symbol * |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 601 | read_elf(Process *proc) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 602 | struct ltelf lte[MAX_LIBRARIES + 1]; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 603 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 604 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 605 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 606 | int exit_out = 0; |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 607 | int count = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 608 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 609 | debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename); |
| 610 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame^] | 611 | library_symbols = NULL; |
| 612 | library_num = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 613 | elf_version(EV_CURRENT); |
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 | do_init_elf(lte, proc->filename); |
| 616 | proc->e_machine = lte->ehdr.e_machine; |
| 617 | for (i = 0; i < library_num; ++i) |
| 618 | do_init_elf(<e[i + 1], library[i]); |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 619 | |
| 620 | if (!options.no_plt) { |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 621 | #ifdef __mips__ |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 622 | // MIPS doesn't use the PLT and the GOT entries get changed |
| 623 | // on startup. |
| 624 | proc->need_to_reinitialize_breakpoints = 1; |
| 625 | for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){ |
| 626 | GElf_Sym sym; |
| 627 | const char *name; |
| 628 | GElf_Addr addr = arch_plt_sym_val(lte, i, 0); |
| 629 | if (gelf_getsym(lte->dynsym, i, &sym) == NULL){ |
| 630 | error(EXIT_FAILURE, 0, |
| 631 | "Couldn't get relocation from \"%s\"", |
| 632 | proc->filename); |
| 633 | } |
| 634 | name=lte->dynstr+sym.st_name; |
Arnaud Patard | 95623b2 | 2010-01-08 08:40:02 -0500 | [diff] [blame] | 635 | if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){ |
| 636 | debug(2,"sym %s not a function",name); |
| 637 | continue; |
| 638 | } |
| 639 | add_library_symbol(addr, name, &library_symbols, 0, |
| 640 | ELF64_ST_BIND(sym.st_info) != 0); |
| 641 | if (!lib_tail) |
| 642 | lib_tail = &(library_symbols->next); |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 643 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 644 | #else |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 645 | for (i = 0; i < lte->relplt_count; ++i) { |
| 646 | GElf_Rel rel; |
| 647 | GElf_Rela rela; |
| 648 | GElf_Sym sym; |
| 649 | GElf_Addr addr; |
| 650 | void *ret; |
| 651 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 652 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 653 | if (lte->relplt->d_type == ELF_T_REL) { |
| 654 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 655 | rela.r_offset = rel.r_offset; |
| 656 | rela.r_info = rel.r_info; |
| 657 | rela.r_addend = 0; |
| 658 | } else |
| 659 | ret = gelf_getrela(lte->relplt, i, &rela); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 660 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 661 | if (ret == NULL |
| 662 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 663 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 664 | &sym) == NULL) |
| 665 | error(EXIT_FAILURE, 0, |
| 666 | "Couldn't get relocation from \"%s\"", |
| 667 | proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 668 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 669 | #ifdef PLT_REINITALISATION_BP |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 670 | if (!sym.st_value && PLTs_initialized_by_here) |
| 671 | proc->need_to_reinitialize_breakpoints = 1; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 672 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 673 | |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 674 | name = lte->dynstr + sym.st_name; |
| 675 | count = library_num ? library_num+1 : 0; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 676 | |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 677 | if (in_load_libraries(name, lte, count, NULL)) { |
| 678 | enum toplt pltt; |
| 679 | if (sym.st_value == 0 && lte->plt_stub_vma != 0) { |
| 680 | pltt = LS_TOPLT_EXEC; |
| 681 | addr = lte->plt_stub_vma + PPC_PLT_STUB_SIZE * i; |
| 682 | } |
| 683 | else { |
| 684 | pltt = PLTS_ARE_EXECUTABLE(lte) |
| 685 | ? LS_TOPLT_EXEC : LS_TOPLT_POINT; |
| 686 | addr = arch_plt_sym_val(lte, i, &rela); |
| 687 | } |
| 688 | |
| 689 | add_library_symbol(addr, name, &library_symbols, pltt, |
| 690 | ELF64_ST_BIND(sym.st_info) == STB_WEAK); |
| 691 | if (!lib_tail) |
| 692 | lib_tail = &(library_symbols->next); |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 693 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 694 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 695 | #endif // !__mips__ |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 696 | #ifdef PLT_REINITALISATION_BP |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 697 | struct opt_x_t *main_cheat; |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 698 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 699 | if (proc->need_to_reinitialize_breakpoints) { |
| 700 | /* Add "PLTs_initialized_by_here" to opt_x list, if not |
| 701 | already there. */ |
| 702 | main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t)); |
| 703 | if (main_cheat == NULL) |
| 704 | error(EXIT_FAILURE, 0, "Couldn't allocate memory"); |
| 705 | main_cheat->next = opt_x; |
| 706 | main_cheat->found = 0; |
| 707 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 708 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 709 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 710 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0 |
| 711 | && main_cheat) { |
| 712 | free(main_cheat); |
| 713 | main_cheat = NULL; |
| 714 | break; |
| 715 | } |
| 716 | if (main_cheat) |
| 717 | opt_x = main_cheat; |
| 718 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 719 | #endif |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 720 | } else { |
| 721 | lib_tail = &library_symbols; |
| 722 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 723 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 724 | for (i = 0; i < lte->symtab_count; ++i) { |
| 725 | GElf_Sym sym; |
| 726 | GElf_Addr addr; |
| 727 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 728 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 729 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 730 | error(EXIT_FAILURE, 0, |
| 731 | "Couldn't get symbol from \"%s\"", |
| 732 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 733 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 734 | name = lte->strtab + sym.st_name; |
| 735 | addr = sym.st_value; |
| 736 | if (!addr) |
| 737 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 738 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 739 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 740 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 741 | /* FIXME: Should be able to use &library_symbols as above. But |
| 742 | when you do, none of the real library symbols cause breaks. */ |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 743 | add_library_symbol(opd2addr(lte, addr), |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 744 | name, lib_tail, LS_TOPLT_NONE, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 745 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | } |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 749 | |
| 750 | int found_count = 0; |
| 751 | |
| 752 | for (xptr = opt_x; xptr; xptr = xptr->next) { |
| 753 | if (xptr->found) |
| 754 | continue; |
| 755 | |
| 756 | GElf_Sym sym; |
| 757 | GElf_Addr addr; |
| 758 | if (in_load_libraries(xptr->name, lte, library_num+1, &sym)) { |
| 759 | debug(2, "found symbol %s @ %lx, adding it.", xptr->name, sym.st_value); |
| 760 | addr = sym.st_value; |
| 761 | if (ELF32_ST_TYPE (sym.st_info) == STT_FUNC) { |
| 762 | add_library_symbol(addr, xptr->name, lib_tail, LS_TOPLT_NONE, 0); |
| 763 | xptr->found = 1; |
| 764 | found_count++; |
| 765 | } |
| 766 | } |
| 767 | if (found_count == opt_x_cnt){ |
| 768 | debug(2, "done, found everything: %d\n", found_count); |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 773 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 774 | if ( ! xptr->found) { |
| 775 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 776 | #ifdef PLT_REINITALISATION_BP |
| 777 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 778 | if (lte->ehdr.e_entry) { |
| 779 | add_library_symbol ( |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 780 | opd2addr (lte, lte->ehdr.e_entry), |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 781 | PLTs_initialized_by_here, |
| 782 | lib_tail, 1, 0); |
| 783 | fprintf (stderr, "WARNING: Using e_ent" |
| 784 | "ry from elf header (%p) for " |
| 785 | "address of \"%s\"\n", (void*) |
| 786 | (long) lte->ehdr.e_entry, |
| 787 | PLTs_initialized_by_here); |
| 788 | continue; |
| 789 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 790 | badthing = "ERROR"; |
| 791 | exit_out = 1; |
| 792 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 793 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 794 | fprintf (stderr, |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 795 | "%s: Couldn't find symbol \"%s\" in file \"%s" |
| 796 | "\"\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 797 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 798 | if (exit_out) { |
| 799 | exit (1); |
| 800 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 801 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 802 | for (i = 0; i < library_num + 1; ++i) |
| 803 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 804 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 805 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 806 | } |