elf.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /*
  28. * Fallback for systems without this "read, mmaping if possible" cmd.
  29. */
  30. #ifndef ELF_C_READ_MMAP
  31. #define ELF_C_READ_MMAP ELF_C_READ
  32. #endif
  33. struct section {
  34. struct list_head list;
  35. GElf_Shdr sh;
  36. struct list_head symbol_list;
  37. DECLARE_HASHTABLE(symbol_hash, 8);
  38. struct list_head rela_list;
  39. DECLARE_HASHTABLE(rela_hash, 16);
  40. struct section *base, *rela;
  41. struct symbol *sym;
  42. Elf_Data *data;
  43. char *name;
  44. int idx;
  45. unsigned int len;
  46. bool changed, text, rodata;
  47. };
  48. struct symbol {
  49. struct list_head list;
  50. struct hlist_node hash;
  51. GElf_Sym sym;
  52. struct section *sec;
  53. char *name;
  54. unsigned int idx;
  55. unsigned char bind, type;
  56. unsigned long offset;
  57. unsigned int len;
  58. struct symbol *pfunc, *cfunc;
  59. };
  60. struct rela {
  61. struct list_head list;
  62. struct hlist_node hash;
  63. GElf_Rela rela;
  64. struct section *rela_sec;
  65. struct symbol *sym;
  66. unsigned int type;
  67. unsigned long offset;
  68. int addend;
  69. };
  70. struct elf {
  71. Elf *elf;
  72. GElf_Ehdr ehdr;
  73. int fd;
  74. char *name;
  75. struct list_head sections;
  76. DECLARE_HASHTABLE(rela_hash, 16);
  77. };
  78. struct elf *elf_open(const char *name, int flags);
  79. struct section *find_section_by_name(struct elf *elf, const char *name);
  80. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
  81. struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
  82. struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
  83. struct rela *find_rela_by_dest(struct section *sec, unsigned long offset);
  84. struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
  85. unsigned int len);
  86. struct symbol *find_containing_func(struct section *sec, unsigned long offset);
  87. struct section *elf_create_section(struct elf *elf, const char *name, size_t
  88. entsize, int nr);
  89. struct section *elf_create_rela_section(struct elf *elf, struct section *base);
  90. int elf_rebuild_rela_section(struct section *sec);
  91. int elf_write(struct elf *elf);
  92. void elf_close(struct elf *elf);
  93. #define for_each_sec(file, sec) \
  94. list_for_each_entry(sec, &file->elf->sections, list)
  95. #endif /* _OBJTOOL_ELF_H */