elf.h 2.3 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 *elf_data;
  37. char *name;
  38. int idx;
  39. unsigned long data;
  40. unsigned int len;
  41. };
  42. struct 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. };
  53. struct rela {
  54. struct list_head list;
  55. struct hlist_node hash;
  56. GElf_Rela rela;
  57. struct symbol *sym;
  58. unsigned int type;
  59. unsigned long offset;
  60. int addend;
  61. };
  62. struct elf {
  63. Elf *elf;
  64. GElf_Ehdr ehdr;
  65. int fd;
  66. char *name;
  67. struct list_head sections;
  68. DECLARE_HASHTABLE(rela_hash, 16);
  69. };
  70. struct elf *elf_open(const char *name);
  71. struct section *find_section_by_name(struct elf *elf, const char *name);
  72. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
  73. struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
  74. struct rela *find_rela_by_dest(struct section *sec, unsigned long offset);
  75. struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
  76. unsigned int len);
  77. struct symbol *find_containing_func(struct section *sec, unsigned long offset);
  78. void elf_close(struct elf *elf);
  79. #endif /* _OBJTOOL_ELF_H */