1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> 4 */ 5 6 #ifndef _OBJTOOL_ELF_H 7 #define _OBJTOOL_ELF_H 8 9 #include <stdio.h> 10 #include <gelf.h> 11 #include <linux/string.h> 12 #include <linux/list.h> 13 #include <linux/hashtable.h> 14 #include <linux/rbtree.h> 15 #include <linux/jhash.h> 16 17 #include <objtool/endianness.h> 18 #include <objtool/checksum_types.h> 19 #include <arch/elf.h> 20 21 #define SEC_NAME_LEN 1024 22 #define SYM_NAME_LEN 512 23 24 static inline u32 str_hash(const char *str) 25 { 26 return jhash(str, strlen(str), 0); 27 } 28 29 u32 str_hash_demangled(const char *str); 30 31 #define bswap_if_needed(elf, val) __bswap_if_needed(&elf->ehdr, val) 32 33 #ifdef LIBELF_USE_DEPRECATED 34 # define elf_getshdrnum elf_getshnum 35 # define elf_getshdrstrndx elf_getshstrndx 36 #endif 37 38 /* 39 * Fallback for systems without this "read, mmaping if possible" cmd. 40 */ 41 #ifndef ELF_C_READ_MMAP 42 #define ELF_C_READ_MMAP ELF_C_READ 43 #endif 44 45 struct elf_hash_node { 46 struct elf_hash_node *next; 47 }; 48 49 struct section { 50 struct list_head list; 51 struct elf_hash_node hash; 52 struct elf_hash_node name_hash; 53 GElf_Shdr sh; 54 struct rb_root_cached symbol_tree; 55 struct list_head symbol_list; 56 struct section *base, *rsec; 57 struct symbol *sym; 58 Elf_Data *data; 59 const char *name; 60 int idx; 61 bool _changed, text, rodata, noinstr, init, truncate; 62 struct reloc *relocs; 63 unsigned long nr_alloc_relocs; 64 struct section *twin; 65 }; 66 67 struct symbol { 68 struct list_head list; 69 struct list_head global_list; 70 struct rb_node node; 71 struct elf_hash_node hash; 72 struct elf_hash_node name_hash; 73 GElf_Sym sym; 74 struct section *sec; 75 const char *name, *demangled_name; 76 unsigned int idx, len; 77 unsigned long offset; 78 unsigned long __subtree_last; 79 struct symbol *pfunc, *cfunc, *alias, *file; 80 unsigned char bind, type; 81 u8 uaccess_safe : 1; 82 u8 static_call_tramp : 1; 83 u8 retpoline_thunk : 1; 84 u8 return_thunk : 1; 85 u8 fentry : 1; 86 u8 profiling_func : 1; 87 u8 warned : 1; 88 u8 embedded_insn : 1; 89 u8 local_label : 1; 90 u8 frame_pointer : 1; 91 u8 ignore : 1; 92 u8 nocfi : 1; 93 u8 cold : 1; 94 u8 prefix : 1; 95 u8 debug_checksum : 1; 96 u8 changed : 1; 97 u8 included : 1; 98 u8 klp : 1; 99 u8 dont_correlate : 1; 100 struct list_head pv_target; 101 struct reloc *relocs; 102 struct section *group_sec; 103 struct checksum csum; 104 struct symbol *twin, *clone; 105 }; 106 107 struct reloc { 108 struct elf_hash_node hash; 109 struct section *sec; 110 struct symbol *sym; 111 unsigned long _sym_next_reloc; 112 }; 113 114 struct elf { 115 Elf *elf; 116 GElf_Ehdr ehdr; 117 int fd; 118 bool changed; 119 const char *name, *tmp_name; 120 unsigned int num_files; 121 struct list_head sections; 122 struct list_head symbols; 123 unsigned long num_relocs; 124 125 int symbol_bits; 126 int symbol_name_bits; 127 int section_bits; 128 int section_name_bits; 129 int reloc_bits; 130 131 struct elf_hash_node **symbol_hash; 132 struct elf_hash_node **symbol_name_hash; 133 struct elf_hash_node **section_hash; 134 struct elf_hash_node **section_name_hash; 135 struct elf_hash_node **reloc_hash; 136 137 struct section *section_data; 138 struct symbol *symbol_data; 139 }; 140 141 #define __elf_table(elf, name) ((elf)->name##_hash) 142 #define __elf_bits(elf, name) ((elf)->name##_bits) 143 144 #define __elf_table_entry(elf, name, key) \ 145 __elf_table(elf, name)[hash_min(key, __elf_bits(elf, name))] 146 147 #define elf_list_entry(ptr, type, member) \ 148 ({ \ 149 typeof(ptr) __ptr = (ptr); \ 150 __ptr ? container_of(__ptr, type, member) : NULL; \ 151 }) 152 153 #define elf_hash_for_each_possible(elf, name, obj, member, key) \ 154 for (obj = elf_list_entry(__elf_table_entry(elf, name, key), typeof(*obj), member); \ 155 obj; \ 156 obj = elf_list_entry(obj->member.next, typeof(*(obj)), member)) 157 158 struct elf *elf_open_read(const char *name, int flags); 159 struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name); 160 161 struct section *elf_create_section(struct elf *elf, const char *name, 162 size_t size, size_t entsize, 163 unsigned int type, unsigned int align, 164 unsigned int flags); 165 struct section *elf_create_section_pair(struct elf *elf, const char *name, 166 size_t entsize, unsigned int nr, 167 unsigned int reloc_nr); 168 169 struct section *elf_create_rela_section(struct elf *elf, struct section *sec, 170 unsigned int reloc_nr); 171 172 struct symbol *elf_create_symbol(struct elf *elf, const char *name, 173 struct section *sec, unsigned int bind, 174 unsigned int type, unsigned long offset, 175 size_t size); 176 struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec); 177 178 void *elf_add_data(struct elf *elf, struct section *sec, const void *data, 179 size_t size); 180 181 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str); 182 183 struct reloc *elf_create_reloc(struct elf *elf, struct section *sec, 184 unsigned long offset, struct symbol *sym, 185 s64 addend, unsigned int type); 186 187 struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, 188 unsigned int reloc_idx, unsigned long offset, 189 struct symbol *sym, s64 addend, unsigned int type); 190 191 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, 192 unsigned long offset, 193 unsigned int reloc_idx, 194 struct section *insn_sec, 195 unsigned long insn_off); 196 197 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, 198 unsigned long offset, 199 unsigned int reloc_idx, 200 struct symbol *sym, 201 s64 addend); 202 203 int elf_write_symbol(struct elf *elf, struct symbol *sym); 204 int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, 205 unsigned int len, const char *insn); 206 207 int elf_write(struct elf *elf); 208 int elf_close(struct elf *elf); 209 210 struct section *find_section_by_name(const struct elf *elf, const char *name); 211 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset); 212 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset); 213 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name); 214 struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name); 215 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset); 216 struct symbol *find_symbol_containing_inclusive(const struct section *sec, unsigned long offset); 217 int find_symbol_hole_containing(const struct section *sec, unsigned long offset); 218 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset); 219 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, 220 unsigned long offset, unsigned int len); 221 struct symbol *find_func_containing(struct section *sec, unsigned long offset); 222 223 /* 224 * Try to see if it's a whole archive (vmlinux.o or module). 225 * 226 * Note this will miss the case where a module only has one source file. 227 */ 228 static inline bool has_multiple_files(struct elf *elf) 229 { 230 return elf->num_files > 1; 231 } 232 233 static inline size_t elf_addr_size(struct elf *elf) 234 { 235 return elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; 236 } 237 238 static inline size_t elf_rela_size(struct elf *elf) 239 { 240 return elf_addr_size(elf) == 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Rela); 241 } 242 243 static inline unsigned int elf_data_rela_type(struct elf *elf) 244 { 245 return elf_addr_size(elf) == 4 ? R_DATA32 : R_DATA64; 246 } 247 248 static inline unsigned int elf_text_rela_type(struct elf *elf) 249 { 250 return elf_addr_size(elf) == 4 ? R_TEXT32 : R_TEXT64; 251 } 252 253 static inline bool is_undef_sym(struct symbol *sym) 254 { 255 return !sym->sec->idx; 256 } 257 258 static inline bool is_null_sym(struct symbol *sym) 259 { 260 return !sym->idx; 261 } 262 263 static inline bool is_sec_sym(struct symbol *sym) 264 { 265 return sym->type == STT_SECTION; 266 } 267 268 static inline bool is_object_sym(struct symbol *sym) 269 { 270 return sym->type == STT_OBJECT; 271 } 272 273 static inline bool is_func_sym(struct symbol *sym) 274 { 275 return sym->type == STT_FUNC; 276 } 277 278 static inline bool is_file_sym(struct symbol *sym) 279 { 280 return sym->type == STT_FILE; 281 } 282 283 static inline bool is_notype_sym(struct symbol *sym) 284 { 285 return sym->type == STT_NOTYPE; 286 } 287 288 static inline bool is_global_sym(struct symbol *sym) 289 { 290 return sym->bind == STB_GLOBAL; 291 } 292 293 static inline bool is_weak_sym(struct symbol *sym) 294 { 295 return sym->bind == STB_WEAK; 296 } 297 298 static inline bool is_local_sym(struct symbol *sym) 299 { 300 return sym->bind == STB_LOCAL; 301 } 302 303 static inline bool is_alias_sym(struct symbol *sym) 304 { 305 return sym->alias != sym; 306 } 307 308 static inline bool is_prefix_func(struct symbol *sym) 309 { 310 return sym->prefix; 311 } 312 313 static inline bool is_cold_func(struct symbol *sym) 314 { 315 return sym->cold; 316 } 317 318 static inline bool is_reloc_sec(struct section *sec) 319 { 320 return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == SHT_REL; 321 } 322 323 static inline bool is_string_sec(struct section *sec) 324 { 325 return sec->sh.sh_flags & SHF_STRINGS; 326 } 327 328 static inline bool is_text_sec(struct section *sec) 329 { 330 return sec->sh.sh_flags & SHF_EXECINSTR; 331 } 332 333 static inline bool is_rodata_sec(struct section *sec) 334 { 335 return sec->rodata; 336 } 337 338 static inline bool sec_changed(struct section *sec) 339 { 340 return sec->_changed; 341 } 342 343 static inline void mark_sec_changed(struct elf *elf, struct section *sec, 344 bool changed) 345 { 346 sec->_changed = changed; 347 elf->changed |= changed; 348 } 349 350 static inline unsigned int sec_num_entries(struct section *sec) 351 { 352 return sec->sh.sh_size / sec->sh.sh_entsize; 353 } 354 355 static inline unsigned int reloc_idx(struct reloc *reloc) 356 { 357 return reloc - reloc->sec->relocs; 358 } 359 360 static inline void *reloc_rel(struct reloc *reloc) 361 { 362 struct section *rsec = reloc->sec; 363 364 return rsec->data->d_buf + (reloc_idx(reloc) * rsec->sh.sh_entsize); 365 } 366 367 static inline bool is_32bit_reloc(struct reloc *reloc) 368 { 369 /* 370 * Elf32_Rel: 8 bytes 371 * Elf32_Rela: 12 bytes 372 * Elf64_Rel: 16 bytes 373 * Elf64_Rela: 24 bytes 374 */ 375 return reloc->sec->sh.sh_entsize < 16; 376 } 377 378 static inline unsigned long sec_size(struct section *sec) 379 { 380 return sec->sh.sh_size; 381 } 382 383 #define __get_reloc_field(reloc, field) \ 384 ({ \ 385 is_32bit_reloc(reloc) ? \ 386 ((Elf32_Rela *)reloc_rel(reloc))->field : \ 387 ((Elf64_Rela *)reloc_rel(reloc))->field; \ 388 }) 389 390 #define __set_reloc_field(reloc, field, val) \ 391 ({ \ 392 if (is_32bit_reloc(reloc)) \ 393 ((Elf32_Rela *)reloc_rel(reloc))->field = val; \ 394 else \ 395 ((Elf64_Rela *)reloc_rel(reloc))->field = val; \ 396 }) 397 398 static inline u64 reloc_offset(struct reloc *reloc) 399 { 400 return __get_reloc_field(reloc, r_offset); 401 } 402 403 static inline void set_reloc_offset(struct elf *elf, struct reloc *reloc, u64 offset) 404 { 405 __set_reloc_field(reloc, r_offset, offset); 406 mark_sec_changed(elf, reloc->sec, true); 407 } 408 409 static inline s64 reloc_addend(struct reloc *reloc) 410 { 411 return __get_reloc_field(reloc, r_addend); 412 } 413 414 static inline void set_reloc_addend(struct elf *elf, struct reloc *reloc, s64 addend) 415 { 416 __set_reloc_field(reloc, r_addend, addend); 417 mark_sec_changed(elf, reloc->sec, true); 418 } 419 420 421 static inline unsigned int reloc_sym(struct reloc *reloc) 422 { 423 u64 info = __get_reloc_field(reloc, r_info); 424 425 return is_32bit_reloc(reloc) ? 426 ELF32_R_SYM(info) : 427 ELF64_R_SYM(info); 428 } 429 430 static inline unsigned int reloc_type(struct reloc *reloc) 431 { 432 u64 info = __get_reloc_field(reloc, r_info); 433 434 return is_32bit_reloc(reloc) ? 435 ELF32_R_TYPE(info) : 436 ELF64_R_TYPE(info); 437 } 438 439 static inline void set_reloc_sym(struct elf *elf, struct reloc *reloc, unsigned int sym) 440 { 441 u64 info = is_32bit_reloc(reloc) ? 442 ELF32_R_INFO(sym, reloc_type(reloc)) : 443 ELF64_R_INFO(sym, reloc_type(reloc)); 444 445 __set_reloc_field(reloc, r_info, info); 446 447 mark_sec_changed(elf, reloc->sec, true); 448 } 449 static inline void set_reloc_type(struct elf *elf, struct reloc *reloc, unsigned int type) 450 { 451 u64 info = is_32bit_reloc(reloc) ? 452 ELF32_R_INFO(reloc_sym(reloc), type) : 453 ELF64_R_INFO(reloc_sym(reloc), type); 454 455 __set_reloc_field(reloc, r_info, info); 456 457 mark_sec_changed(elf, reloc->sec, true); 458 } 459 460 static inline unsigned int annotype(struct elf *elf, struct section *sec, 461 struct reloc *reloc) 462 { 463 unsigned int type; 464 465 type = *(u32 *)(sec->data->d_buf + (reloc_idx(reloc) * 8) + 4); 466 return bswap_if_needed(elf, type); 467 } 468 469 #define RELOC_JUMP_TABLE_BIT 1UL 470 471 /* Does reloc mark the beginning of a jump table? */ 472 static inline bool is_jump_table(struct reloc *reloc) 473 { 474 return reloc->_sym_next_reloc & RELOC_JUMP_TABLE_BIT; 475 } 476 477 static inline void set_jump_table(struct reloc *reloc) 478 { 479 reloc->_sym_next_reloc |= RELOC_JUMP_TABLE_BIT; 480 } 481 482 static inline struct reloc *sym_next_reloc(struct reloc *reloc) 483 { 484 return (struct reloc *)(reloc->_sym_next_reloc & ~RELOC_JUMP_TABLE_BIT); 485 } 486 487 static inline void set_sym_next_reloc(struct reloc *reloc, struct reloc *next) 488 { 489 unsigned long bit = reloc->_sym_next_reloc & RELOC_JUMP_TABLE_BIT; 490 491 reloc->_sym_next_reloc = (unsigned long)next | bit; 492 } 493 494 #define for_each_sec(elf, sec) \ 495 list_for_each_entry(sec, &elf->sections, list) 496 497 #define sec_for_each_sym(sec, sym) \ 498 list_for_each_entry(sym, &sec->symbol_list, list) 499 500 #define sec_prev_sym(sym) \ 501 sym->sec && sym->list.prev != &sym->sec->symbol_list ? \ 502 list_prev_entry(sym, list) : NULL 503 504 #define for_each_sym(elf, sym) \ 505 list_for_each_entry(sym, &elf->symbols, global_list) 506 507 #define for_each_sym_continue(elf, sym) \ 508 list_for_each_entry_continue(sym, &elf->symbols, global_list) 509 510 #define for_each_sym_by_name(elf, _name, sym) \ 511 elf_hash_for_each_possible(elf, symbol_name, sym, name_hash, \ 512 str_hash_demangled(_name)) \ 513 if (strcmp(sym->name, _name)) {} else 514 515 #define for_each_sym_by_demangled_name(elf, name, sym) \ 516 elf_hash_for_each_possible(elf, symbol_name, sym, name_hash, \ 517 str_hash(name)) \ 518 if (strcmp(sym->demangled_name, name)) {} else 519 520 #define rsec_next_reloc(rsec, reloc) \ 521 reloc_idx(reloc) < sec_num_entries(rsec) - 1 ? reloc + 1 : NULL 522 523 #define for_each_reloc(rsec, reloc) \ 524 for (reloc = rsec->relocs; reloc; reloc = rsec_next_reloc(rsec, reloc)) 525 526 #define for_each_reloc_from(rsec, reloc) \ 527 for (; reloc; reloc = rsec_next_reloc(rsec, reloc)) 528 529 #define for_each_reloc_continue(rsec, reloc) \ 530 for (reloc = rsec_next_reloc(rsec, reloc); reloc; \ 531 reloc = rsec_next_reloc(rsec, reloc)) 532 533 #define sym_for_each_reloc(elf, sym, reloc) \ 534 for (reloc = find_reloc_by_dest_range(elf, sym->sec, \ 535 sym->offset, sym->len); \ 536 reloc && reloc_offset(reloc) < sym->offset + sym->len; \ 537 reloc = rsec_next_reloc(sym->sec->rsec, reloc)) 538 539 static inline struct symbol *get_func_prefix(struct symbol *func) 540 { 541 struct symbol *prev; 542 543 if (!is_func_sym(func) || !func->offset) 544 return NULL; 545 546 prev = find_func_containing(func->sec, func->offset - 1); 547 if (prev && is_prefix_func(prev)) 548 return prev; 549 550 return NULL; 551 } 552 553 #define OFFSET_STRIDE_BITS 4 554 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) 555 #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) 556 557 #define for_offset_range(_offset, _start, _end) \ 558 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \ 559 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \ 560 _offset <= ((_end) & OFFSET_STRIDE_MASK); \ 561 _offset += OFFSET_STRIDE) 562 563 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) 564 { 565 u32 ol, oh, idx = sec->idx; 566 567 offset &= OFFSET_STRIDE_MASK; 568 569 ol = offset; 570 oh = (offset >> 16) >> 16; 571 572 __jhash_mix(ol, oh, idx); 573 574 return ol; 575 } 576 577 static inline u32 reloc_hash(struct reloc *reloc) 578 { 579 return sec_offset_hash(reloc->sec, reloc_offset(reloc)); 580 } 581 582 #endif /* _OBJTOOL_ELF_H */ 583