|
| 1 | +// Copyright 2018-2019 - Omar Sandoval |
| 2 | +// SPDX-License-Identifier: GPL-3.0+ |
| 3 | + |
| 4 | +#include <inttypes.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +#include "internal.h" |
| 8 | +#include "elf_relocator.h" |
| 9 | + |
| 10 | +DEFINE_VECTOR_FUNCTIONS(elf_vector) |
| 11 | + |
| 12 | +void drgn_elf_relocator_init(struct drgn_elf_relocator *relocator) |
| 13 | +{ |
| 14 | + elf_vector_init(&relocator->elfs); |
| 15 | +} |
| 16 | + |
| 17 | +void drgn_elf_relocator_deinit(struct drgn_elf_relocator *relocator) |
| 18 | +{ |
| 19 | + elf_vector_deinit(&relocator->elfs); |
| 20 | +} |
| 21 | + |
| 22 | +struct drgn_error * |
| 23 | +drgn_elf_relocator_add_elf(struct drgn_elf_relocator *relocator, Elf *elf) |
| 24 | +{ |
| 25 | + GElf_Ehdr ehdr_mem, *ehdr; |
| 26 | + |
| 27 | + ehdr = gelf_getehdr(elf, &ehdr_mem); |
| 28 | + if (!ehdr) |
| 29 | + return drgn_error_libelf(); |
| 30 | + |
| 31 | + if (ehdr->e_type != ET_REL || |
| 32 | + ehdr->e_machine != EM_X86_64 || |
| 33 | + ehdr->e_ident[EI_CLASS] != ELFCLASS64 || |
| 34 | + ehdr->e_ident[EI_DATA] != |
| 35 | + (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ? |
| 36 | + ELFDATA2LSB : ELFDATA2MSB)) |
| 37 | + return NULL; |
| 38 | + |
| 39 | + if (!elf_vector_append(&relocator->elfs, &elf)) |
| 40 | + return &drgn_enomem; |
| 41 | + return NULL; |
| 42 | +} |
| 43 | + |
| 44 | +static struct drgn_error *apply_relocation(Elf_Data *data, uint64_t r_offset, |
| 45 | + uint32_t r_type, int64_t r_addend, |
| 46 | + uint64_t st_value) |
| 47 | +{ |
| 48 | + char *p; |
| 49 | + |
| 50 | + p = (char *)data->d_buf + r_offset; |
| 51 | + switch (r_type) { |
| 52 | + case R_X86_64_NONE: |
| 53 | + break; |
| 54 | + case R_X86_64_32: |
| 55 | + if (r_offset > SIZE_MAX - sizeof(uint32_t) || |
| 56 | + r_offset + sizeof(uint32_t) > data->d_size) { |
| 57 | + return drgn_error_create(DRGN_ERROR_ELF_FORMAT, |
| 58 | + "invalid relocation offset"); |
| 59 | + } |
| 60 | + *(uint32_t *)p = st_value + r_addend; |
| 61 | + break; |
| 62 | + case R_X86_64_64: |
| 63 | + if (r_offset > SIZE_MAX - sizeof(uint64_t) || |
| 64 | + r_offset + sizeof(uint64_t) > data->d_size) { |
| 65 | + return drgn_error_create(DRGN_ERROR_ELF_FORMAT, |
| 66 | + "invalid relocation offset"); |
| 67 | + } |
| 68 | + *(uint64_t *)p = st_value + r_addend; |
| 69 | + break; |
| 70 | + default: |
| 71 | + return drgn_error_format(DRGN_ERROR_ELF_FORMAT, |
| 72 | + "unimplemented relocation type %" PRIu32, |
| 73 | + r_type); |
| 74 | + } |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | +static struct drgn_error *relocate_section(Elf_Scn *scn, Elf_Scn *rela_scn, |
| 79 | + Elf_Scn *symtab_scn, |
| 80 | + uint64_t *sh_addrs, size_t shdrnum) |
| 81 | +{ |
| 82 | + struct drgn_error *err; |
| 83 | + Elf_Data *data, *rela_data, *symtab_data; |
| 84 | + const Elf64_Rela *relocs; |
| 85 | + const Elf64_Sym *syms; |
| 86 | + size_t num_relocs, num_syms; |
| 87 | + size_t i; |
| 88 | + GElf_Shdr *shdr, shdr_mem; |
| 89 | + |
| 90 | + err = read_elf_section(scn, &data); |
| 91 | + if (err) |
| 92 | + return err; |
| 93 | + err = read_elf_section(rela_scn, &rela_data); |
| 94 | + if (err) |
| 95 | + return err; |
| 96 | + err = read_elf_section(symtab_scn, &symtab_data); |
| 97 | + if (err) |
| 98 | + return err; |
| 99 | + |
| 100 | + relocs = (Elf64_Rela *)rela_data->d_buf; |
| 101 | + num_relocs = rela_data->d_size / sizeof(Elf64_Rela); |
| 102 | + syms = (Elf64_Sym *)symtab_data->d_buf; |
| 103 | + num_syms = symtab_data->d_size / sizeof(Elf64_Sym); |
| 104 | + |
| 105 | + for (i = 0; i < num_relocs; i++) { |
| 106 | + const Elf64_Rela *reloc = &relocs[i]; |
| 107 | + uint32_t r_sym, r_type; |
| 108 | + uint16_t st_shndx; |
| 109 | + uint64_t sh_addr; |
| 110 | + |
| 111 | + r_sym = ELF64_R_SYM(reloc->r_info); |
| 112 | + r_type = ELF64_R_TYPE(reloc->r_info); |
| 113 | + |
| 114 | + if (r_sym >= num_syms) { |
| 115 | + return drgn_error_create(DRGN_ERROR_ELF_FORMAT, |
| 116 | + "invalid relocation symbol"); |
| 117 | + } |
| 118 | + st_shndx = syms[r_sym].st_shndx; |
| 119 | + if (st_shndx == 0) { |
| 120 | + sh_addr = 0; |
| 121 | + } else if (st_shndx < shdrnum) { |
| 122 | + sh_addr = sh_addrs[st_shndx - 1]; |
| 123 | + } else { |
| 124 | + return drgn_error_create(DRGN_ERROR_ELF_FORMAT, |
| 125 | + "invalid symbol section index"); |
| 126 | + } |
| 127 | + err = apply_relocation(data, reloc->r_offset, r_type, |
| 128 | + reloc->r_addend, |
| 129 | + sh_addr + syms[r_sym].st_value); |
| 130 | + if (err) |
| 131 | + return err; |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * Mark the relocation section as empty so that libdwfl doesn't try to |
| 136 | + * apply it again. |
| 137 | + */ |
| 138 | + shdr = gelf_getshdr(rela_scn, &shdr_mem); |
| 139 | + if (!shdr) |
| 140 | + return drgn_error_libelf(); |
| 141 | + shdr->sh_size = 0; |
| 142 | + if (!gelf_update_shdr(rela_scn, shdr)) |
| 143 | + return drgn_error_libelf(); |
| 144 | + rela_data->d_size = 0; |
| 145 | + return NULL; |
| 146 | +} |
| 147 | + |
| 148 | +static struct drgn_error *relocate_elf(Elf *elf) |
| 149 | +{ |
| 150 | + struct drgn_error *err; |
| 151 | + size_t shdrnum, shstrndx; |
| 152 | + uint64_t *sh_addrs; |
| 153 | + Elf_Scn *scn; |
| 154 | + |
| 155 | + if (elf_getshdrnum(elf, &shdrnum)) |
| 156 | + return drgn_error_libelf(); |
| 157 | + if (shdrnum > 1) { |
| 158 | + sh_addrs = calloc(shdrnum - 1, sizeof(*sh_addrs)); |
| 159 | + if (!sh_addrs) |
| 160 | + return &drgn_enomem; |
| 161 | + |
| 162 | + scn = NULL; |
| 163 | + while ((scn = elf_nextscn(elf, scn))) { |
| 164 | + size_t ndx; |
| 165 | + |
| 166 | + ndx = elf_ndxscn(scn); |
| 167 | + if (ndx > 0 && ndx < shdrnum) { |
| 168 | + GElf_Shdr *shdr, shdr_mem; |
| 169 | + |
| 170 | + shdr = gelf_getshdr(scn, &shdr_mem); |
| 171 | + if (!shdr) { |
| 172 | + err = drgn_error_libelf(); |
| 173 | + goto out; |
| 174 | + } |
| 175 | + sh_addrs[ndx - 1] = shdr->sh_addr; |
| 176 | + } |
| 177 | + } |
| 178 | + } else { |
| 179 | + sh_addrs = NULL; |
| 180 | + } |
| 181 | + |
| 182 | + if (elf_getshdrstrndx(elf, &shstrndx)) { |
| 183 | + err = drgn_error_libelf(); |
| 184 | + goto out; |
| 185 | + } |
| 186 | + |
| 187 | + scn = NULL; |
| 188 | + while ((scn = elf_nextscn(elf, scn))) { |
| 189 | + GElf_Shdr *shdr, shdr_mem; |
| 190 | + const char *scnname; |
| 191 | + |
| 192 | + shdr = gelf_getshdr(scn, &shdr_mem); |
| 193 | + if (!shdr) { |
| 194 | + err = drgn_error_libelf(); |
| 195 | + goto out; |
| 196 | + } |
| 197 | + |
| 198 | + if (shdr->sh_type != SHT_RELA) |
| 199 | + continue; |
| 200 | + |
| 201 | + scnname = elf_strptr(elf, shstrndx, shdr->sh_name); |
| 202 | + if (!scnname) |
| 203 | + continue; |
| 204 | + |
| 205 | + if (strncmp(scnname, ".rela.debug_", 12) == 0) { |
| 206 | + Elf_Scn *info_scn, *link_scn; |
| 207 | + |
| 208 | + info_scn = elf_getscn(elf, shdr->sh_info); |
| 209 | + if (!info_scn) { |
| 210 | + err = drgn_error_libelf(); |
| 211 | + goto out; |
| 212 | + } |
| 213 | + |
| 214 | + link_scn = elf_getscn(elf, shdr->sh_link); |
| 215 | + if (!link_scn) { |
| 216 | + err = drgn_error_libelf(); |
| 217 | + goto out; |
| 218 | + } |
| 219 | + |
| 220 | + err = relocate_section(info_scn, scn, link_scn, |
| 221 | + sh_addrs, shdrnum); |
| 222 | + if (err) |
| 223 | + goto out; |
| 224 | + } |
| 225 | + } |
| 226 | +out: |
| 227 | + free(sh_addrs); |
| 228 | + return NULL; |
| 229 | +} |
| 230 | + |
| 231 | +struct drgn_error * |
| 232 | +drgn_elf_relocator_apply(struct drgn_elf_relocator *relocator) |
| 233 | +{ |
| 234 | + struct drgn_error *err = NULL; |
| 235 | + Elf **elfs = relocator->elfs.data; |
| 236 | + size_t num_elfs = relocator->elfs.size; |
| 237 | + |
| 238 | + #pragma omp parallel for schedule(dynamic) |
| 239 | + for (size_t i = 0; i < num_elfs; i++) { |
| 240 | + struct drgn_error *err2; |
| 241 | + |
| 242 | + if (err) |
| 243 | + continue; |
| 244 | + |
| 245 | + err2 = relocate_elf(elfs[i]); |
| 246 | + if (err2) { |
| 247 | + #pragma omp critical(relocators_err) |
| 248 | + { |
| 249 | + if (err) |
| 250 | + drgn_error_destroy(err2); |
| 251 | + else |
| 252 | + err = err2; |
| 253 | + } |
| 254 | + continue; |
| 255 | + } |
| 256 | + } |
| 257 | + return err; |
| 258 | +} |
0 commit comments