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