Loading...
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/list.h>
12#include <linux/hashtable.h>
13
14#ifdef LIBELF_USE_DEPRECATED
15# define elf_getshdrnum elf_getshnum
16# define elf_getshdrstrndx elf_getshstrndx
17#endif
18
19/*
20 * Fallback for systems without this "read, mmaping if possible" cmd.
21 */
22#ifndef ELF_C_READ_MMAP
23#define ELF_C_READ_MMAP ELF_C_READ
24#endif
25
26struct section {
27 struct list_head list;
28 GElf_Shdr sh;
29 struct list_head symbol_list;
30 DECLARE_HASHTABLE(symbol_hash, 8);
31 struct list_head rela_list;
32 DECLARE_HASHTABLE(rela_hash, 16);
33 struct section *base, *rela;
34 struct symbol *sym;
35 Elf_Data *data;
36 char *name;
37 int idx;
38 unsigned int len;
39 bool changed, text, rodata;
40};
41
42struct symbol {
43 struct list_head list;
44 struct hlist_node hash;
45 GElf_Sym sym;
46 struct section *sec;
47 char *name;
48 unsigned int idx;
49 unsigned char bind, type;
50 unsigned long offset;
51 unsigned int len;
52 struct symbol *pfunc, *cfunc, *alias;
53 bool uaccess_safe;
54};
55
56struct rela {
57 struct list_head list;
58 struct hlist_node hash;
59 GElf_Rela rela;
60 struct section *sec;
61 struct symbol *sym;
62 unsigned int type;
63 unsigned long offset;
64 int addend;
65 bool jump_table_start;
66};
67
68struct elf {
69 Elf *elf;
70 GElf_Ehdr ehdr;
71 int fd;
72 char *name;
73 struct list_head sections;
74 DECLARE_HASHTABLE(rela_hash, 16);
75};
76
77
78struct elf *elf_read(const char *name, int flags);
79struct section *find_section_by_name(struct elf *elf, const char *name);
80struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
81struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
82struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
83struct rela *find_rela_by_dest(struct section *sec, unsigned long offset);
84struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
85 unsigned int len);
86struct symbol *find_containing_func(struct section *sec, unsigned long offset);
87struct section *elf_create_section(struct elf *elf, const char *name, size_t
88 entsize, int nr);
89struct section *elf_create_rela_section(struct elf *elf, struct section *base);
90int elf_rebuild_rela_section(struct section *sec);
91int elf_write(struct elf *elf);
92void elf_close(struct elf *elf);
93
94#define for_each_sec(file, sec) \
95 list_for_each_entry(sec, &file->elf->sections, list)
96
97#endif /* _OBJTOOL_ELF_H */
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/list.h>
12#include <linux/hashtable.h>
13#include <linux/rbtree.h>
14#include <linux/jhash.h>
15
16#ifdef LIBELF_USE_DEPRECATED
17# define elf_getshdrnum elf_getshnum
18# define elf_getshdrstrndx elf_getshstrndx
19#endif
20
21/*
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
28struct section {
29 struct list_head list;
30 struct hlist_node hash;
31 struct hlist_node name_hash;
32 GElf_Shdr sh;
33 struct rb_root symbol_tree;
34 struct list_head symbol_list;
35 struct list_head reloc_list;
36 struct section *base, *reloc;
37 struct symbol *sym;
38 Elf_Data *data;
39 char *name;
40 int idx;
41 unsigned int len;
42 bool changed, text, rodata, noinstr;
43};
44
45struct symbol {
46 struct list_head list;
47 struct rb_node node;
48 struct hlist_node hash;
49 struct hlist_node name_hash;
50 GElf_Sym sym;
51 struct section *sec;
52 char *name;
53 unsigned int idx;
54 unsigned char bind, type;
55 unsigned long offset;
56 unsigned int len;
57 struct symbol *pfunc, *cfunc, *alias;
58 bool uaccess_safe;
59};
60
61struct reloc {
62 struct list_head list;
63 struct hlist_node hash;
64 union {
65 GElf_Rela rela;
66 GElf_Rel rel;
67 };
68 struct section *sec;
69 struct symbol *sym;
70 unsigned long offset;
71 unsigned int type;
72 int addend;
73 int idx;
74 bool jump_table_start;
75};
76
77#define ELF_HASH_BITS 20
78
79struct elf {
80 Elf *elf;
81 GElf_Ehdr ehdr;
82 int fd;
83 bool changed;
84 char *name;
85 struct list_head sections;
86 DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
87 DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
88 DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
89 DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
90 DECLARE_HASHTABLE(reloc_hash, ELF_HASH_BITS);
91};
92
93#define OFFSET_STRIDE_BITS 4
94#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
95#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
96
97#define for_offset_range(_offset, _start, _end) \
98 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
99 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
100 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
101 _offset += OFFSET_STRIDE)
102
103static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
104{
105 u32 ol, oh, idx = sec->idx;
106
107 offset &= OFFSET_STRIDE_MASK;
108
109 ol = offset;
110 oh = (offset >> 16) >> 16;
111
112 __jhash_mix(ol, oh, idx);
113
114 return ol;
115}
116
117static inline u32 reloc_hash(struct reloc *reloc)
118{
119 return sec_offset_hash(reloc->sec, reloc->offset);
120}
121
122struct elf *elf_open_read(const char *name, int flags);
123struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
124struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype);
125void elf_add_reloc(struct elf *elf, struct reloc *reloc);
126int elf_write_insn(struct elf *elf, struct section *sec,
127 unsigned long offset, unsigned int len,
128 const char *insn);
129int elf_write_reloc(struct elf *elf, struct reloc *reloc);
130int elf_write(struct elf *elf);
131void elf_close(struct elf *elf);
132
133struct section *find_section_by_name(const struct elf *elf, const char *name);
134struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
135struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
136struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
137struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
138struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
139struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
140 unsigned long offset, unsigned int len);
141struct symbol *find_func_containing(struct section *sec, unsigned long offset);
142int elf_rebuild_reloc_section(struct elf *elf, struct section *sec);
143
144#define for_each_sec(file, sec) \
145 list_for_each_entry(sec, &file->elf->sections, list)
146
147#endif /* _OBJTOOL_ELF_H */