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