1 /* 2 * elf.c - ELF access library 3 * 4 * Adapted from kpatch (https://github.com/dynup/kpatch): 5 * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com> 6 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 #include <fcntl.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "elf.h" 31 #include "warn.h" 32 33 /* 34 * Fallback for systems without this "read, mmaping if possible" cmd. 35 */ 36 #ifndef ELF_C_READ_MMAP 37 #define ELF_C_READ_MMAP ELF_C_READ 38 #endif 39 40 struct section *find_section_by_name(struct elf *elf, const char *name) 41 { 42 struct section *sec; 43 44 list_for_each_entry(sec, &elf->sections, list) 45 if (!strcmp(sec->name, name)) 46 return sec; 47 48 return NULL; 49 } 50 51 static struct section *find_section_by_index(struct elf *elf, 52 unsigned int idx) 53 { 54 struct section *sec; 55 56 list_for_each_entry(sec, &elf->sections, list) 57 if (sec->idx == idx) 58 return sec; 59 60 return NULL; 61 } 62 63 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) 64 { 65 struct section *sec; 66 struct symbol *sym; 67 68 list_for_each_entry(sec, &elf->sections, list) 69 hash_for_each_possible(sec->symbol_hash, sym, hash, idx) 70 if (sym->idx == idx) 71 return sym; 72 73 return NULL; 74 } 75 76 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) 77 { 78 struct symbol *sym; 79 80 list_for_each_entry(sym, &sec->symbol_list, list) 81 if (sym->type != STT_SECTION && 82 sym->offset == offset) 83 return sym; 84 85 return NULL; 86 } 87 88 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset) 89 { 90 struct symbol *sym; 91 92 list_for_each_entry(sym, &sec->symbol_list, list) 93 if (sym->type != STT_SECTION && 94 offset >= sym->offset && offset < sym->offset + sym->len) 95 return sym; 96 97 return NULL; 98 } 99 100 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset, 101 unsigned int len) 102 { 103 struct rela *rela; 104 unsigned long o; 105 106 if (!sec->rela) 107 return NULL; 108 109 for (o = offset; o < offset + len; o++) 110 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o) 111 if (rela->offset == o) 112 return rela; 113 114 return NULL; 115 } 116 117 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset) 118 { 119 return find_rela_by_dest_range(sec, offset, 1); 120 } 121 122 struct symbol *find_containing_func(struct section *sec, unsigned long offset) 123 { 124 struct symbol *func; 125 126 list_for_each_entry(func, &sec->symbol_list, list) 127 if (func->type == STT_FUNC && offset >= func->offset && 128 offset < func->offset + func->len) 129 return func; 130 131 return NULL; 132 } 133 134 static int read_sections(struct elf *elf) 135 { 136 Elf_Scn *s = NULL; 137 struct section *sec; 138 size_t shstrndx, sections_nr; 139 int i; 140 141 if (elf_getshdrnum(elf->elf, §ions_nr)) { 142 perror("elf_getshdrnum"); 143 return -1; 144 } 145 146 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { 147 perror("elf_getshdrstrndx"); 148 return -1; 149 } 150 151 for (i = 0; i < sections_nr; i++) { 152 sec = malloc(sizeof(*sec)); 153 if (!sec) { 154 perror("malloc"); 155 return -1; 156 } 157 memset(sec, 0, sizeof(*sec)); 158 159 INIT_LIST_HEAD(&sec->symbol_list); 160 INIT_LIST_HEAD(&sec->rela_list); 161 hash_init(sec->rela_hash); 162 hash_init(sec->symbol_hash); 163 164 list_add_tail(&sec->list, &elf->sections); 165 166 s = elf_getscn(elf->elf, i); 167 if (!s) { 168 perror("elf_getscn"); 169 return -1; 170 } 171 172 sec->idx = elf_ndxscn(s); 173 174 if (!gelf_getshdr(s, &sec->sh)) { 175 perror("gelf_getshdr"); 176 return -1; 177 } 178 179 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); 180 if (!sec->name) { 181 perror("elf_strptr"); 182 return -1; 183 } 184 185 sec->elf_data = elf_getdata(s, NULL); 186 if (!sec->elf_data) { 187 perror("elf_getdata"); 188 return -1; 189 } 190 191 if (sec->elf_data->d_off != 0 || 192 sec->elf_data->d_size != sec->sh.sh_size) { 193 WARN("unexpected data attributes for %s", sec->name); 194 return -1; 195 } 196 197 sec->data = (unsigned long)sec->elf_data->d_buf; 198 sec->len = sec->elf_data->d_size; 199 } 200 201 /* sanity check, one more call to elf_nextscn() should return NULL */ 202 if (elf_nextscn(elf->elf, s)) { 203 WARN("section entry mismatch"); 204 return -1; 205 } 206 207 return 0; 208 } 209 210 static int read_symbols(struct elf *elf) 211 { 212 struct section *symtab; 213 struct symbol *sym; 214 struct list_head *entry, *tmp; 215 int symbols_nr, i; 216 217 symtab = find_section_by_name(elf, ".symtab"); 218 if (!symtab) { 219 WARN("missing symbol table"); 220 return -1; 221 } 222 223 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize; 224 225 for (i = 0; i < symbols_nr; i++) { 226 sym = malloc(sizeof(*sym)); 227 if (!sym) { 228 perror("malloc"); 229 return -1; 230 } 231 memset(sym, 0, sizeof(*sym)); 232 233 sym->idx = i; 234 235 if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) { 236 perror("gelf_getsym"); 237 goto err; 238 } 239 240 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, 241 sym->sym.st_name); 242 if (!sym->name) { 243 perror("elf_strptr"); 244 goto err; 245 } 246 247 sym->type = GELF_ST_TYPE(sym->sym.st_info); 248 sym->bind = GELF_ST_BIND(sym->sym.st_info); 249 250 if (sym->sym.st_shndx > SHN_UNDEF && 251 sym->sym.st_shndx < SHN_LORESERVE) { 252 sym->sec = find_section_by_index(elf, 253 sym->sym.st_shndx); 254 if (!sym->sec) { 255 WARN("couldn't find section for symbol %s", 256 sym->name); 257 goto err; 258 } 259 if (sym->type == STT_SECTION) { 260 sym->name = sym->sec->name; 261 sym->sec->sym = sym; 262 } 263 } else 264 sym->sec = find_section_by_index(elf, 0); 265 266 sym->offset = sym->sym.st_value; 267 sym->len = sym->sym.st_size; 268 269 /* sorted insert into a per-section list */ 270 entry = &sym->sec->symbol_list; 271 list_for_each_prev(tmp, &sym->sec->symbol_list) { 272 struct symbol *s; 273 274 s = list_entry(tmp, struct symbol, list); 275 276 if (sym->offset > s->offset) { 277 entry = tmp; 278 break; 279 } 280 281 if (sym->offset == s->offset && sym->len >= s->len) { 282 entry = tmp; 283 break; 284 } 285 } 286 list_add(&sym->list, entry); 287 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx); 288 } 289 290 return 0; 291 292 err: 293 free(sym); 294 return -1; 295 } 296 297 static int read_relas(struct elf *elf) 298 { 299 struct section *sec; 300 struct rela *rela; 301 int i; 302 unsigned int symndx; 303 304 list_for_each_entry(sec, &elf->sections, list) { 305 if (sec->sh.sh_type != SHT_RELA) 306 continue; 307 308 sec->base = find_section_by_name(elf, sec->name + 5); 309 if (!sec->base) { 310 WARN("can't find base section for rela section %s", 311 sec->name); 312 return -1; 313 } 314 315 sec->base->rela = sec; 316 317 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) { 318 rela = malloc(sizeof(*rela)); 319 if (!rela) { 320 perror("malloc"); 321 return -1; 322 } 323 memset(rela, 0, sizeof(*rela)); 324 325 if (!gelf_getrela(sec->elf_data, i, &rela->rela)) { 326 perror("gelf_getrela"); 327 return -1; 328 } 329 330 rela->type = GELF_R_TYPE(rela->rela.r_info); 331 rela->addend = rela->rela.r_addend; 332 rela->offset = rela->rela.r_offset; 333 symndx = GELF_R_SYM(rela->rela.r_info); 334 rela->sym = find_symbol_by_index(elf, symndx); 335 if (!rela->sym) { 336 WARN("can't find rela entry symbol %d for %s", 337 symndx, sec->name); 338 return -1; 339 } 340 341 list_add_tail(&rela->list, &sec->rela_list); 342 hash_add(sec->rela_hash, &rela->hash, rela->offset); 343 344 } 345 } 346 347 return 0; 348 } 349 350 struct elf *elf_open(const char *name) 351 { 352 struct elf *elf; 353 354 elf_version(EV_CURRENT); 355 356 elf = malloc(sizeof(*elf)); 357 if (!elf) { 358 perror("malloc"); 359 return NULL; 360 } 361 memset(elf, 0, sizeof(*elf)); 362 363 INIT_LIST_HEAD(&elf->sections); 364 365 elf->name = strdup(name); 366 if (!elf->name) { 367 perror("strdup"); 368 goto err; 369 } 370 371 elf->fd = open(name, O_RDONLY); 372 if (elf->fd == -1) { 373 perror("open"); 374 goto err; 375 } 376 377 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL); 378 if (!elf->elf) { 379 perror("elf_begin"); 380 goto err; 381 } 382 383 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { 384 perror("gelf_getehdr"); 385 goto err; 386 } 387 388 if (read_sections(elf)) 389 goto err; 390 391 if (read_symbols(elf)) 392 goto err; 393 394 if (read_relas(elf)) 395 goto err; 396 397 return elf; 398 399 err: 400 elf_close(elf); 401 return NULL; 402 } 403 404 void elf_close(struct elf *elf) 405 { 406 struct section *sec, *tmpsec; 407 struct symbol *sym, *tmpsym; 408 struct rela *rela, *tmprela; 409 410 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { 411 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) { 412 list_del(&sym->list); 413 hash_del(&sym->hash); 414 free(sym); 415 } 416 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) { 417 list_del(&rela->list); 418 hash_del(&rela->hash); 419 free(rela); 420 } 421 list_del(&sec->list); 422 free(sec); 423 } 424 if (elf->name) 425 free(elf->name); 426 if (elf->fd > 0) 427 close(elf->fd); 428 if (elf->elf) 429 elf_end(elf->elf); 430 free(elf); 431 } 432