pahole: Describe expected use of 'default' in the man page
[dwarves.git] / elf_symtab.h
blob5d4cb5281dfd7c876f762dcccf97b02658eba2aa
1 #ifndef _ELF_SYMTAB_H_
2 #define _ELF_SYMTAB_H_ 1
3 /*
4 SPDX-License-Identifier: GPL-2.0-only
6 Copyright (C) 2009 Red Hat Inc.
7 Copyright (C) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
8 */
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <gelf.h>
13 #include <elf.h>
15 struct elf_symtab {
16 uint32_t nr_syms;
17 Elf_Data *syms;
18 Elf_Data *symstrs;
19 /* Data of SHT_SYMTAB_SHNDX section. */
20 Elf_Data *syms_sec_idx_table;
21 char *name;
24 struct elf_symtab *elf_symtab__new(const char *name, Elf *elf);
25 void elf_symtab__delete(struct elf_symtab *symtab);
27 static inline uint32_t elf_symtab__nr_symbols(const struct elf_symtab *symtab)
29 return symtab->nr_syms;
32 static inline const char *elf_sym__name(const GElf_Sym *sym,
33 const struct elf_symtab *symtab)
35 return symtab->symstrs->d_buf + sym->st_name;
38 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
40 return GELF_ST_TYPE(sym->st_info);
43 static inline uint16_t elf_sym__section(const GElf_Sym *sym)
45 return sym->st_shndx;
48 static inline uint8_t elf_sym__bind(const GElf_Sym *sym)
50 return GELF_ST_BIND(sym->st_info);
53 static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
55 return GELF_ST_VISIBILITY(sym->st_other);
58 static inline uint32_t elf_sym__size(const GElf_Sym *sym)
60 return sym->st_size;
63 static inline uint64_t elf_sym__value(const GElf_Sym *sym)
65 return sym->st_value;
68 static inline bool elf_sym__is_local_function(const GElf_Sym *sym)
70 return elf_sym__type(sym) == STT_FUNC &&
71 sym->st_name != 0 &&
72 sym->st_shndx != SHN_UNDEF;
75 static inline bool elf_sym__is_local_object(const GElf_Sym *sym)
77 return elf_sym__type(sym) == STT_OBJECT &&
78 sym->st_name != 0 &&
79 sym->st_shndx != SHN_UNDEF;
82 static inline bool
83 elf_sym__get(Elf_Data *syms, Elf_Data *syms_sec_idx_table,
84 int id, GElf_Sym *sym, Elf32_Word *sym_sec_idx)
86 if (!gelf_getsymshndx(syms, syms_sec_idx_table, id, sym, sym_sec_idx))
87 return false;
89 if (sym->st_shndx != SHN_XINDEX)
90 *sym_sec_idx = sym->st_shndx;
92 return true;
95 /**
96 * elf_symtab__for_each_symbol - iterate thru all the symbols
98 * @symtab: struct elf_symtab instance to iterate
99 * @index: uint32_t index
100 * @sym: GElf_Sym iterator
102 #define elf_symtab__for_each_symbol(symtab, index, sym) \
103 for (index = 0, gelf_getsym(symtab->syms, index, &sym);\
104 index < symtab->nr_syms; \
105 index++, gelf_getsym(symtab->syms, index, &sym))
108 * elf_symtab__for_each_symbol_index - iterate through all the symbols,
109 * that takes extended symbols indexes into account
111 * @symtab: struct elf_symtab instance to iterate
112 * @index: uint32_t index
113 * @sym: GElf_Sym iterator
114 * @sym_sec_idx: symbol's index
116 #define elf_symtab__for_each_symbol_index(symtab, id, sym, sym_sec_idx) \
117 for (id = 0; id < symtab->nr_syms; id++) \
118 if (elf_sym__get(symtab->syms, symtab->syms_sec_idx_table, \
119 id, &sym, &sym_sec_idx))
121 #endif /* _ELF_SYMTAB_H_ */