blob: 44150204db4d1e1e3ed73218ee2bd37906379621 [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>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060013
Jan Beulich2e51f262016-05-16 15:31:07 -050014#ifdef LIBELF_USE_DEPRECATED
15# define elf_getshdrnum elf_getshnum
16# define elf_getshdrstrndx elf_getshstrndx
17#endif
18
Josh Poimboeuf627fce12017-07-11 10:33:42 -050019/*
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
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060026struct section {
27 struct list_head list;
28 GElf_Shdr sh;
Josh Poimboeufa196e172016-03-09 00:06:57 -060029 struct list_head symbol_list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060030 DECLARE_HASHTABLE(symbol_hash, 8);
Josh Poimboeufa196e172016-03-09 00:06:57 -060031 struct list_head rela_list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060032 DECLARE_HASHTABLE(rela_hash, 16);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060033 struct section *base, *rela;
34 struct symbol *sym;
Josh Poimboeufbaa41462017-06-28 10:11:07 -050035 Elf_Data *data;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060036 char *name;
37 int idx;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060038 unsigned int len;
Allan Xavier4a60aa02018-09-07 08:12:01 -050039 bool changed, text, rodata;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060040};
41
42struct symbol {
43 struct list_head list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060044 struct hlist_node hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060045 GElf_Sym sym;
46 struct section *sec;
47 char *name;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060048 unsigned int idx;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060049 unsigned char bind, type;
50 unsigned long offset;
51 unsigned int len;
Peter Zijlstra09f30d82019-02-28 14:17:50 +010052 struct symbol *pfunc, *cfunc, *alias;
Peter Zijlstraea242132019-02-25 12:50:09 +010053 bool uaccess_safe;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060054};
55
56struct rela {
57 struct list_head list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060058 struct hlist_node hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060059 GElf_Rela rela;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -050060 struct section *sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060061 struct symbol *sym;
62 unsigned int type;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060063 unsigned long offset;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060064 int addend;
Jann Hornbd98c812019-07-17 20:36:54 -050065 bool jump_table_start;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060066};
67
68struct elf {
69 Elf *elf;
70 GElf_Ehdr ehdr;
71 int fd;
72 char *name;
73 struct list_head sections;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060074 DECLARE_HASHTABLE(rela_hash, 16);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060075};
76
77
Michael Forney8e144792019-07-10 16:20:11 -050078struct elf *elf_read(const char *name, int flags);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060079struct section *find_section_by_name(struct elf *elf, const char *name);
80struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
Josh Poimboeuf13810432018-05-09 22:39:15 -050081struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -060082struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060083struct 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);
Josh Poimboeuf627fce12017-07-11 10:33:42 -050087struct 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);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060092void elf_close(struct elf *elf);
93
Josh Poimboeufbaa41462017-06-28 10:11:07 -050094#define for_each_sec(file, sec) \
95 list_for_each_entry(sec, &file->elf->sections, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060096
97#endif /* _OBJTOOL_ELF_H */