elf.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _OBJTOOL_ELF_H
  18. #define _OBJTOOL_ELF_H
  19. #include <stdio.h>
  20. #include <gelf.h>
  21. #include <linux/list.h>
  22. #include <linux/hashtable.h>
  23. #ifdef LIBELF_USE_DEPRECATED
  24. # define elf_getshdrnum elf_getshnum
  25. # define elf_getshdrstrndx elf_getshstrndx
  26. #endif
  27. struct section {
  28. struct list_head list;
  29. GElf_Shdr sh;
  30. struct list_head symbol_list;
  31. DECLARE_HASHTABLE(symbol_hash, 8);
  32. struct list_head rela_list;
  33. DECLARE_HASHTABLE(rela_hash, 16);
  34. struct section *base, *rela;
  35. struct symbol *sym;
  36. Elf_Data *data;
  37. char *name;
  38. int idx;
  39. unsigned int len;
  40. };
  41. struct symbol {
  42. struct list_head list;
  43. struct hlist_node hash;
  44. GElf_Sym sym;
  45. struct section *sec;
  46. char *name;
  47. unsigned int idx;
  48. unsigned char bind, type;
  49. unsigned long offset;
  50. unsigned int len;
  51. };
  52. struct rela {
  53. struct list_head list;
  54. struct hlist_node hash;
  55. GElf_Rela rela;
  56. struct symbol *sym;
  57. unsigned int type;
  58. unsigned long offset;
  59. int addend;
  60. };
  61. struct elf {
  62. Elf *elf;
  63. GElf_Ehdr ehdr;
  64. int fd;
  65. char *name;
  66. struct list_head sections;
  67. DECLARE_HASHTABLE(rela_hash, 16);
  68. };
  69. struct elf *elf_open(const char *name);
  70. struct section *find_section_by_name(struct elf *elf, const char *name);
  71. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
  72. struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
  73. struct rela *find_rela_by_dest(struct section *sec, unsigned long offset);
  74. struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
  75. unsigned int len);
  76. struct symbol *find_containing_func(struct section *sec, unsigned long offset);
  77. void elf_close(struct elf *elf);
  78. #define for_each_sec(file, sec) \
  79. list_for_each_entry(sec, &file->elf->sections, list)
  80. #endif /* _OBJTOOL_ELF_H */