blob: f4fe1d6ea3924c5641346dc433edfe7451676f5d [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06002/*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06004 */
5
6#ifndef _OBJTOOL_ELF_H
7#define _OBJTOOL_ELF_H
8
9#include <stdio.h>
10#include <gelf.h>
11#include <linux/list.h>
Josh Poimboeuf042ba732016-03-09 00:07:00 -060012#include <linux/hashtable.h>
Peter Zijlstra2a362ec2020-03-12 09:34:42 +010013#include <linux/rbtree.h>
Peter Zijlstraae358192020-03-12 09:32:10 +010014#include <linux/jhash.h>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060015
Jan Beulich2e51f262016-05-16 15:31:07 -050016#ifdef LIBELF_USE_DEPRECATED
17# define elf_getshdrnum elf_getshnum
18# define elf_getshdrstrndx elf_getshstrndx
19#endif
20
Josh Poimboeuf627fce12017-07-11 10:33:42 -050021/*
22 * Fallback for systems without this "read, mmaping if possible" cmd.
23 */
24#ifndef ELF_C_READ_MMAP
25#define ELF_C_READ_MMAP ELF_C_READ
26#endif
27
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060028struct section {
29 struct list_head list;
Peter Zijlstra530389962020-03-10 18:43:35 +010030 struct hlist_node hash;
Peter Zijlstraae358192020-03-12 09:32:10 +010031 struct hlist_node name_hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060032 GElf_Shdr sh;
Peter Zijlstra2a362ec2020-03-12 09:34:42 +010033 struct rb_root symbol_tree;
Josh Poimboeufa196e172016-03-09 00:06:57 -060034 struct list_head symbol_list;
35 struct list_head rela_list;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060036 struct section *base, *rela;
37 struct symbol *sym;
Josh Poimboeufbaa41462017-06-28 10:11:07 -050038 Elf_Data *data;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060039 char *name;
40 int idx;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060041 unsigned int len;
Peter Zijlstrac4a33932020-03-10 18:57:41 +010042 bool changed, text, rodata, noinstr;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060043};
44
45struct symbol {
46 struct list_head list;
Peter Zijlstra2a362ec2020-03-12 09:34:42 +010047 struct rb_node node;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060048 struct hlist_node hash;
Peter Zijlstracdb3d052020-03-12 10:17:38 +010049 struct hlist_node name_hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060050 GElf_Sym sym;
51 struct section *sec;
52 char *name;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060053 unsigned int idx;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060054 unsigned char bind, type;
55 unsigned long offset;
56 unsigned int len;
Peter Zijlstra09f30d82019-02-28 14:17:50 +010057 struct symbol *pfunc, *cfunc, *alias;
Peter Zijlstraea242132019-02-25 12:50:09 +010058 bool uaccess_safe;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060059};
60
61struct rela {
62 struct list_head list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060063 struct hlist_node hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060064 GElf_Rela rela;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -050065 struct section *sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060066 struct symbol *sym;
67 unsigned int type;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060068 unsigned long offset;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060069 int addend;
Jann Hornbd98c812019-07-17 20:36:54 -050070 bool jump_table_start;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060071};
72
Peter Zijlstra34f7c962020-03-12 14:29:38 +010073#define ELF_HASH_BITS 20
74
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060075struct elf {
76 Elf *elf;
77 GElf_Ehdr ehdr;
78 int fd;
79 char *name;
80 struct list_head sections;
Peter Zijlstra34f7c962020-03-12 14:29:38 +010081 DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
82 DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
83 DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
84 DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
85 DECLARE_HASHTABLE(rela_hash, ELF_HASH_BITS);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060086};
87
Peter Zijlstra74b873e2020-03-12 11:30:50 +010088#define OFFSET_STRIDE_BITS 4
89#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
90#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
91
Josh Poimboeuf53fb6e92020-04-25 14:19:01 -050092#define for_offset_range(_offset, _start, _end) \
93 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
94 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
95 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
Peter Zijlstra74b873e2020-03-12 11:30:50 +010096 _offset += OFFSET_STRIDE)
97
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +010098static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
99{
Peter Zijlstra74b873e2020-03-12 11:30:50 +0100100 u32 ol, oh, idx = sec->idx;
101
102 offset &= OFFSET_STRIDE_MASK;
103
104 ol = offset;
Peter Zijlstra963d5662020-04-20 10:33:32 +0200105 oh = (offset >> 16) >> 16;
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100106
107 __jhash_mix(ol, oh, idx);
108
109 return ol;
110}
111
112static inline u32 rela_hash(struct rela *rela)
113{
114 return sec_offset_hash(rela->sec, rela->offset);
115}
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600116
Ingo Molnarbc359ff2020-04-22 12:32:04 +0200117struct elf *elf_open_read(const char *name, int flags);
Ingo Molnar894e48c2020-04-22 12:32:03 +0200118struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
119struct section *elf_create_rela_section(struct elf *elf, struct section *base);
120void elf_add_rela(struct elf *elf, struct rela *rela);
121int elf_write(const struct elf *elf);
122void elf_close(struct elf *elf);
123
124struct section *find_section_by_name(const struct elf *elf, const char *name);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600125struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600126struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
Ingo Molnar894e48c2020-04-22 12:32:03 +0200127struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
Miroslav Benesb490f452020-04-24 16:30:42 +0200128struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
Ingo Molnar894e48c2020-04-22 12:32:03 +0200129struct rela *find_rela_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
130struct rela *find_rela_by_dest_range(const struct elf *elf, struct section *sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100131 unsigned long offset, unsigned int len);
Peter Zijlstra53d20722020-03-16 10:36:53 +0100132struct symbol *find_func_containing(struct section *sec, unsigned long offset);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500133int elf_rebuild_rela_section(struct section *sec);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600134
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500135#define for_each_sec(file, sec) \
136 list_for_each_entry(sec, &file->elf->sections, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600137
138#endif /* _OBJTOOL_ELF_H */