blob: e6890cc70a25bae7b0689e7254c7879a875498ae [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
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>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <linux/rbtree.h>
14#include <linux/jhash.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015
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;
Olivier Deprez157378f2022-04-04 15:47:50 +020030 struct hlist_node hash;
31 struct hlist_node name_hash;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032 GElf_Shdr sh;
Olivier Deprez157378f2022-04-04 15:47:50 +020033 struct rb_root symbol_tree;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034 struct list_head symbol_list;
Olivier Deprez157378f2022-04-04 15:47:50 +020035 struct list_head reloc_list;
36 struct section *base, *reloc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 struct symbol *sym;
38 Elf_Data *data;
39 char *name;
40 int idx;
41 unsigned int len;
Olivier Deprez157378f2022-04-04 15:47:50 +020042 bool changed, text, rodata, noinstr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043};
44
45struct symbol {
46 struct list_head list;
Olivier Deprez157378f2022-04-04 15:47:50 +020047 struct rb_node node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048 struct hlist_node hash;
Olivier Deprez157378f2022-04-04 15:47:50 +020049 struct hlist_node name_hash;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 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;
David Brazdil0f672f62019-12-10 10:32:29 +000057 struct symbol *pfunc, *cfunc, *alias;
58 bool uaccess_safe;
Olivier Deprez157378f2022-04-04 15:47:50 +020059 bool static_call_tramp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060};
61
Olivier Deprez157378f2022-04-04 15:47:50 +020062struct reloc {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 struct list_head list;
64 struct hlist_node hash;
Olivier Deprez157378f2022-04-04 15:47:50 +020065 union {
66 GElf_Rela rela;
67 GElf_Rel rel;
68 };
David Brazdil0f672f62019-12-10 10:32:29 +000069 struct section *sec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 struct symbol *sym;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 unsigned long offset;
Olivier Deprez157378f2022-04-04 15:47:50 +020072 unsigned int type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 int addend;
Olivier Deprez157378f2022-04-04 15:47:50 +020074 int idx;
David Brazdil0f672f62019-12-10 10:32:29 +000075 bool jump_table_start;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076};
77
Olivier Deprez157378f2022-04-04 15:47:50 +020078#define ELF_HASH_BITS 20
79
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080struct elf {
81 Elf *elf;
82 GElf_Ehdr ehdr;
83 int fd;
Olivier Deprez157378f2022-04-04 15:47:50 +020084 bool changed;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085 char *name;
86 struct list_head sections;
Olivier Deprez157378f2022-04-04 15:47:50 +020087 DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
88 DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
89 DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
90 DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
91 DECLARE_HASHTABLE(reloc_hash, ELF_HASH_BITS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092};
93
Olivier Deprez157378f2022-04-04 15:47:50 +020094#define OFFSET_STRIDE_BITS 4
95#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
96#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097
Olivier Deprez157378f2022-04-04 15:47:50 +020098#define for_offset_range(_offset, _start, _end) \
99 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
100 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
101 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
102 _offset += OFFSET_STRIDE)
103
104static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
105{
106 u32 ol, oh, idx = sec->idx;
107
108 offset &= OFFSET_STRIDE_MASK;
109
110 ol = offset;
111 oh = (offset >> 16) >> 16;
112
113 __jhash_mix(ol, oh, idx);
114
115 return ol;
116}
117
118static inline u32 reloc_hash(struct reloc *reloc)
119{
120 return sec_offset_hash(reloc->sec, reloc->offset);
121}
122
123struct elf *elf_open_read(const char *name, int flags);
124struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr);
125struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype);
126void elf_add_reloc(struct elf *elf, struct reloc *reloc);
127int elf_write_insn(struct elf *elf, struct section *sec,
128 unsigned long offset, unsigned int len,
129 const char *insn);
130int elf_write_reloc(struct elf *elf, struct reloc *reloc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131int elf_write(struct elf *elf);
132void elf_close(struct elf *elf);
133
Olivier Deprez157378f2022-04-04 15:47:50 +0200134struct section *find_section_by_name(const struct elf *elf, const char *name);
135struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
136struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
137struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
138struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
139struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
140struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
141 unsigned long offset, unsigned int len);
142struct symbol *find_func_containing(struct section *sec, unsigned long offset);
143void insn_to_reloc_sym_addend(struct section *sec, unsigned long offset,
144 struct reloc *reloc);
145int elf_rebuild_reloc_section(struct elf *elf, struct section *sec);
146
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147#define for_each_sec(file, sec) \
148 list_for_each_entry(sec, &file->elf->sections, list)
149
150#endif /* _OBJTOOL_ELF_H */