Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [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> |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 8 | #include <inttypes.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include <stdlib.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 11 | #include <string.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 12 | #include <unistd.h> |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 13 | #include <assert.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" |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 16 | #include "proc.h" |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 17 | #include "library.h" |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 18 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 19 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | extern char *PLTs_initialized_by_here; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 21 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 22 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 23 | #ifndef DT_PPC_GOT |
| 24 | # define DT_PPC_GOT (DT_LOPROC + 0) |
| 25 | #endif |
| 26 | |
| 27 | #define PPC_PLT_STUB_SIZE 16 |
| 28 | |
| 29 | static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr) |
| 30 | { |
| 31 | Elf_Data *data = elf_getdata(scn, NULL); |
| 32 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 33 | || data->d_off || data->d_size != shdr->sh_size) |
| 34 | return NULL; |
| 35 | return data; |
| 36 | } |
| 37 | |
| 38 | static int inside(GElf_Addr addr, GElf_Shdr *shdr) |
| 39 | { |
| 40 | return addr >= shdr->sh_addr |
| 41 | && addr < shdr->sh_addr + shdr->sh_size; |
| 42 | } |
| 43 | |
| 44 | static int maybe_pick_section(GElf_Addr addr, |
| 45 | Elf_Scn *in_sec, GElf_Shdr *in_shdr, |
| 46 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 47 | { |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 48 | if (inside(addr, in_shdr)) { |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 49 | *tgt_sec = in_sec; |
| 50 | *tgt_shdr = *in_shdr; |
| 51 | return 1; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int get_section_covering(struct ltelf *lte, GElf_Addr addr, |
| 57 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 58 | { |
| 59 | int i; |
| 60 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 61 | Elf_Scn *scn; |
| 62 | GElf_Shdr shdr; |
| 63 | |
| 64 | scn = elf_getscn(lte->elf, i); |
| 65 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { |
| 66 | debug(1, "Couldn't read section or header."); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr)) |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static GElf_Addr read32be(Elf_Data *data, size_t offset) |
| 78 | { |
| 79 | if (data->d_size < offset + 4) { |
| 80 | debug(1, "Not enough data to read 32bit value at offset %zd.", |
| 81 | offset); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | unsigned char const *buf = data->d_buf + offset; |
| 86 | return ((Elf32_Word)buf[0] << 24) |
| 87 | | ((Elf32_Word)buf[1] << 16) |
| 88 | | ((Elf32_Word)buf[2] << 8) |
| 89 | | ((Elf32_Word)buf[3]); |
| 90 | } |
| 91 | |
| 92 | static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, |
| 93 | Elf_Data *plt_data) |
| 94 | { |
| 95 | Elf_Scn *ppcgot_sec = NULL; |
| 96 | GElf_Shdr ppcgot_shdr; |
| 97 | if (ppcgot != 0 |
| 98 | && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr)) |
| 99 | // xxx should be the log out |
| 100 | fprintf(stderr, |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 101 | "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n", |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 102 | ppcgot); |
| 103 | |
| 104 | if (ppcgot_sec != NULL) { |
| 105 | Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr); |
| 106 | if (data == NULL |
| 107 | || data->d_size < 8 ) |
| 108 | debug(1, "Couldn't read GOT data."); |
| 109 | else { |
| 110 | // where PPCGOT begins in .got |
| 111 | size_t offset = ppcgot - ppcgot_shdr.sh_addr; |
| 112 | GElf_Addr glink_vma = read32be(data, offset + 4); |
| 113 | if (glink_vma != 0) { |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 114 | debug(1, "PPC GOT glink_vma address: %#" PRIx64, |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 115 | glink_vma); |
| 116 | return glink_vma; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (plt_data != NULL) { |
| 122 | GElf_Addr glink_vma = read32be(plt_data, 0); |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 123 | debug(1, ".plt glink_vma address: %#" PRIx64, glink_vma); |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 124 | return glink_vma; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 130 | int |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 131 | open_elf(struct ltelf *lte, const char *filename) |
| 132 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 133 | lte->fd = open(filename, O_RDONLY); |
| 134 | if (lte->fd == -1) |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 135 | return 1; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 136 | |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 137 | elf_version(EV_CURRENT); |
| 138 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 139 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 140 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 141 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 142 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 143 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 144 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 145 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 146 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 147 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 148 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 149 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 150 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 151 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 152 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 153 | error(EXIT_FAILURE, 0, |
| 154 | "\"%s\" is not an ELF executable nor shared library", |
| 155 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 156 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 157 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 158 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 159 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 160 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 161 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 162 | #endif |
| 163 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 164 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 165 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 166 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 167 | ) |
| 168 | error(EXIT_FAILURE, 0, |
| 169 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 170 | |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 174 | /* XXX temporarily non-static */ |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 175 | int |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame^] | 176 | do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr base) |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 177 | { |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 178 | int i; |
| 179 | GElf_Addr relplt_addr = 0; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 180 | GElf_Addr soname_offset = 0; |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 181 | size_t relplt_size = 0; |
| 182 | |
| 183 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
| 184 | debug(1, "Reading ELF from %s...", filename); |
| 185 | |
| 186 | if (open_elf(lte, filename) < 0) |
| 187 | return -1; |
| 188 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 189 | Elf_Data *plt_data = NULL; |
| 190 | GElf_Addr ppcgot = 0; |
| 191 | |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame^] | 192 | /* Find out the bias. For DSOs, this will be just BASE, |
| 193 | * unless the DSO is pre-linked. For ET_EXEC files, this will |
| 194 | * turn out to be 0. */ |
| 195 | { |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 196 | GElf_Phdr phdr; |
| 197 | for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) { |
| 198 | if (phdr.p_type == PT_LOAD) { |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame^] | 199 | if (lte->ehdr.e_type == ET_EXEC) |
| 200 | lte->base_addr = phdr.p_vaddr; |
| 201 | else |
| 202 | lte->base_addr = base; |
| 203 | lte->bias = lte->base_addr - phdr.p_vaddr; |
| 204 | fprintf(stderr, |
| 205 | " + vaddr=%#lx, base=%#lx, bias=%#lx\n", |
| 206 | lte->base_addr, base, lte->bias); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame^] | 212 | lte->entry_addr = lte->ehdr.e_entry + lte->bias; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 213 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 214 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 215 | Elf_Scn *scn; |
| 216 | GElf_Shdr shdr; |
| 217 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 218 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 219 | scn = elf_getscn(lte->elf, i); |
| 220 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 221 | error(EXIT_FAILURE, 0, |
| 222 | "Couldn't get section header from \"%s\"", |
| 223 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 224 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 225 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 226 | if (name == NULL) |
| 227 | error(EXIT_FAILURE, 0, |
| 228 | "Couldn't get section header from \"%s\"", |
| 229 | filename); |
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 | if (shdr.sh_type == SHT_SYMTAB) { |
| 232 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 233 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 234 | lte->symtab = elf_getdata(scn, NULL); |
| 235 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 236 | if ((lte->symtab == NULL |
| 237 | || elf_getdata(scn, lte->symtab) != NULL) |
| 238 | && opt_x != NULL) |
| 239 | error(EXIT_FAILURE, 0, |
| 240 | "Couldn't get .symtab data from \"%s\"", |
| 241 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 242 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 243 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 244 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 245 | error(EXIT_FAILURE, 0, |
| 246 | "Couldn't get section header from \"%s\"", |
| 247 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 248 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 249 | data = elf_getdata(scn, NULL); |
| 250 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 251 | || shdr.sh_size != data->d_size || data->d_off) |
| 252 | error(EXIT_FAILURE, 0, |
| 253 | "Couldn't get .strtab data from \"%s\"", |
| 254 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 255 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 256 | lte->strtab = data->d_buf; |
| 257 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 258 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 259 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 260 | lte->dynsym = elf_getdata(scn, NULL); |
| 261 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 262 | if (lte->dynsym == NULL |
| 263 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 264 | error(EXIT_FAILURE, 0, |
| 265 | "Couldn't get .dynsym data from \"%s\"", |
| 266 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 267 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 268 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 269 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 270 | error(EXIT_FAILURE, 0, |
| 271 | "Couldn't get section header from \"%s\"", |
| 272 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 273 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 274 | data = elf_getdata(scn, NULL); |
| 275 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 276 | || shdr.sh_size != data->d_size || data->d_off) |
| 277 | error(EXIT_FAILURE, 0, |
| 278 | "Couldn't get .dynstr data from \"%s\"", |
| 279 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 280 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 281 | lte->dynstr = data->d_buf; |
| 282 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 283 | Elf_Data *data; |
| 284 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 285 | |
Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 286 | lte->dyn_addr = shdr.sh_addr; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 287 | fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr); |
| 288 | extern void *dyn_addr; |
| 289 | dyn_addr = (void *)lte->dyn_addr; |
Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 290 | lte->dyn_sz = shdr.sh_size; |
| 291 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 292 | data = elf_getdata(scn, NULL); |
| 293 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 294 | error(EXIT_FAILURE, 0, |
| 295 | "Couldn't get .dynamic data from \"%s\"", |
| 296 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 297 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 298 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 299 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 300 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 301 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 302 | error(EXIT_FAILURE, 0, |
| 303 | "Couldn't get .dynamic data from \"%s\"", |
| 304 | filename); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 305 | #ifdef __mips__ |
| 306 | /** |
| 307 | MIPS ABI Supplement: |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 308 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 309 | DT_PLTGOT This member holds the address of the .got section. |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 310 | |
| 311 | DT_MIPS_SYMTABNO This member holds the number of entries in the |
| 312 | .dynsym section. |
| 313 | |
| 314 | DT_MIPS_LOCAL_GOTNO This member holds the number of local global |
| 315 | offset table entries. |
| 316 | |
| 317 | DT_MIPS_GOTSYM This member holds the index of the first dyamic |
| 318 | symbol table entry that corresponds to an entry in the gobal offset |
| 319 | table. |
| 320 | |
| 321 | */ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 322 | if(dyn.d_tag==DT_PLTGOT){ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 323 | lte->pltgot_addr=dyn.d_un.d_ptr; |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 324 | } |
| 325 | if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){ |
| 326 | lte->mips_local_gotno=dyn.d_un.d_val; |
| 327 | } |
| 328 | if(dyn.d_tag==DT_MIPS_GOTSYM){ |
| 329 | lte->mips_gotsym=dyn.d_un.d_val; |
| 330 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 331 | #endif // __mips__ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 332 | if (dyn.d_tag == DT_JMPREL) |
| 333 | relplt_addr = dyn.d_un.d_ptr; |
| 334 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 335 | relplt_size = dyn.d_un.d_val; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 336 | else if (dyn.d_tag == DT_PPC_GOT) { |
| 337 | ppcgot = dyn.d_un.d_val; |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 338 | debug(1, "ppcgot %#" PRIx64, ppcgot); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 339 | } else if (dyn.d_tag == DT_SONAME) { |
| 340 | soname_offset = dyn.d_un.d_val; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 341 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 342 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 343 | } else if (shdr.sh_type == SHT_PROGBITS |
| 344 | || shdr.sh_type == SHT_NOBITS) { |
| 345 | if (strcmp(name, ".plt") == 0) { |
| 346 | lte->plt_addr = shdr.sh_addr; |
| 347 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 348 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 349 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 350 | } |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 351 | if (lte->ehdr.e_machine == EM_PPC) { |
| 352 | plt_data = loaddata(scn, &shdr); |
| 353 | if (plt_data == NULL) |
| 354 | fprintf(stderr, |
| 355 | "Can't load .plt data\n"); |
| 356 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 357 | } |
| 358 | #ifdef ARCH_SUPPORTS_OPD |
| 359 | else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 360 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 361 | lte->opd_size = shdr.sh_size; |
| 362 | lte->opd = elf_rawdata(scn, NULL); |
| 363 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 364 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 365 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 368 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 369 | error(EXIT_FAILURE, 0, |
| 370 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
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 (!relplt_addr || !lte->plt_addr) { |
| 373 | debug(1, "%s has no PLT relocations", filename); |
| 374 | lte->relplt = NULL; |
| 375 | lte->relplt_count = 0; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 376 | } else if (relplt_size == 0) { |
| 377 | debug(1, "%s has unknown PLT size", filename); |
| 378 | lte->relplt = NULL; |
| 379 | lte->relplt_count = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 380 | } else { |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 381 | if (lte->ehdr.e_machine == EM_PPC) { |
| 382 | GElf_Addr glink_vma |
| 383 | = get_glink_vma(lte, ppcgot, plt_data); |
| 384 | |
| 385 | assert (relplt_size % 12 == 0); |
| 386 | size_t count = relplt_size / 12; // size of RELA entry |
| 387 | lte->plt_stub_vma = glink_vma |
| 388 | - (GElf_Addr)count * PPC_PLT_STUB_SIZE; |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 389 | debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma); |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 392 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 393 | Elf_Scn *scn; |
| 394 | GElf_Shdr shdr; |
| 395 | |
| 396 | scn = elf_getscn(lte->elf, i); |
| 397 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 398 | error(EXIT_FAILURE, 0, |
| 399 | "Couldn't get section header from \"%s\"", |
| 400 | filename); |
| 401 | if (shdr.sh_addr == relplt_addr |
| 402 | && shdr.sh_size == relplt_size) { |
| 403 | lte->relplt = elf_getdata(scn, NULL); |
| 404 | lte->relplt_count = |
| 405 | shdr.sh_size / shdr.sh_entsize; |
| 406 | if (lte->relplt == NULL |
| 407 | || elf_getdata(scn, lte->relplt) != NULL) |
| 408 | error(EXIT_FAILURE, 0, |
| 409 | "Couldn't get .rel*.plt data from \"%s\"", |
| 410 | filename); |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (i == lte->ehdr.e_shnum) |
| 416 | error(EXIT_FAILURE, 0, |
| 417 | "Couldn't find .rel*.plt section in \"%s\"", |
| 418 | filename); |
| 419 | |
| 420 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 421 | } |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 422 | |
| 423 | if (soname_offset != 0) |
| 424 | lte->soname = lte->dynstr + soname_offset; |
| 425 | |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 426 | return 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 429 | /* XXX temporarily non-static */ |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 430 | void |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 431 | do_close_elf(struct ltelf *lte) { |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 432 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 433 | elf_end(lte->elf); |
| 434 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 437 | /* XXX non-static for now, as it's not called anywhere. But we want |
| 438 | * this code around. */ |
| 439 | GElf_Addr |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 440 | opd2addr(struct ltelf *lte, GElf_Addr addr) { |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 441 | #ifdef ARCH_SUPPORTS_OPD |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 442 | unsigned long base, offset; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 443 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 444 | if (!lte->opd) |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 445 | return addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 446 | |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 447 | base = (unsigned long)lte->opd->d_buf; |
| 448 | offset = (unsigned long)addr - (unsigned long)lte->opd_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 449 | if (offset > lte->opd_size) |
| 450 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 451 | |
Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 452 | return *(GElf_Addr*)(base + offset); |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 453 | #else //!ARCH_SUPPORTS_OPD |
| 454 | return addr; |
| 455 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 458 | struct library * |
| 459 | ltelf_read_library(const char *filename, GElf_Addr base) |
Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 460 | { |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 461 | // XXX we leak LTE contents |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame^] | 462 | struct ltelf lte = {}; |
| 463 | if (do_init_elf(<e, filename, base) < 0) |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 464 | return NULL; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 465 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 466 | struct library *lib = malloc(sizeof(*lib)); |
| 467 | char *soname = NULL; |
| 468 | if (lib == NULL) { |
| 469 | fail: |
| 470 | free(soname); |
| 471 | library_destroy(lib); |
| 472 | free(lib); |
| 473 | lib = NULL; |
| 474 | goto done; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 477 | if (lte.soname != NULL) { |
| 478 | soname = strdup(lte.soname); |
| 479 | if (soname == NULL) |
| 480 | goto fail; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 481 | } |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 482 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 483 | library_init(lib, soname, soname != NULL); |
| 484 | lib->entry = (target_address_t)lte.entry_addr; |
| 485 | lib->base = (target_address_t)lte.base_addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 486 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 487 | size_t i; |
| 488 | for (i = 0; i < lte.relplt_count; ++i) { |
| 489 | GElf_Rel rel; |
| 490 | GElf_Rela rela; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 491 | GElf_Sym sym; |
| 492 | GElf_Addr addr; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 493 | void *ret; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 494 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 495 | if (lte.relplt->d_type == ELF_T_REL) { |
| 496 | ret = gelf_getrel(lte.relplt, i, &rel); |
| 497 | rela.r_offset = rel.r_offset; |
| 498 | rela.r_info = rel.r_info; |
| 499 | rela.r_addend = 0; |
| 500 | } else { |
| 501 | ret = gelf_getrela(lte.relplt, i, &rela); |
| 502 | } |
| 503 | |
| 504 | if (ret == NULL |
| 505 | || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count |
| 506 | || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info), |
| 507 | &sym) == NULL) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 508 | error(EXIT_FAILURE, 0, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 509 | "Couldn't get relocation from \"%s\"", |
| 510 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 511 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 512 | /* We will destroy the ELF object at the end of the |
| 513 | * scope. We need to copy the name for our purposes. |
| 514 | * XXX consider just keeping the ELF around. */ |
| 515 | char *name = strdup(lte.dynstr + sym.st_name); |
| 516 | if (name == NULL) { |
| 517 | fail2: |
| 518 | free(name); |
| 519 | goto fail; |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 520 | } |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 521 | |
| 522 | enum toplt pltt; |
| 523 | if (sym.st_value == 0 && lte.plt_stub_vma != 0) { |
| 524 | pltt = LS_TOPLT_EXEC; |
| 525 | addr = lte.plt_stub_vma + PPC_PLT_STUB_SIZE * i; |
| 526 | } else { |
| 527 | pltt = PLTS_ARE_EXECUTABLE(<e) |
| 528 | ? LS_TOPLT_EXEC : LS_TOPLT_POINT; |
| 529 | addr = arch_plt_sym_val(<e, i, &rela); |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 530 | } |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 531 | |
| 532 | struct library_symbol *libsym = malloc(sizeof(*libsym)); |
| 533 | if (libsym == NULL) |
| 534 | goto fail2; |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame^] | 535 | library_symbol_init(libsym, lib, addr + lte.bias, name, 1, pltt, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 536 | ELF64_ST_BIND(sym.st_info) == STB_WEAK); |
| 537 | library_add_symbol(lib, libsym); |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 540 | done: |
| 541 | do_close_elf(<e); |
| 542 | return lib; |
| 543 | } |
Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 544 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 545 | struct library * |
| 546 | ltelf_read_main_binary(struct Process *proc, const char *path) |
| 547 | { |
| 548 | fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path); |
| 549 | char *fname = pid2name(proc->pid); |
| 550 | struct library *lib = ltelf_read_library(fname, 0); |
| 551 | library_set_name(lib, path, 0); |
| 552 | return lib; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 553 | } |