libelf.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /******************************************************************************
  2. * libelf.h
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #ifndef __XC_LIBELF__
  23. #define __XC_LIBELF__ 1
  24. #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
  25. #define XEN_ELF_LITTLE_ENDIAN
  26. #else
  27. #error define architectural endianness
  28. #endif
  29. #undef ELFSIZE
  30. #include "elfnote.h"
  31. #include "elfstructs.h"
  32. #include "features.h"
  33. /* ------------------------------------------------------------------------ */
  34. typedef union {
  35. Elf32_Ehdr e32;
  36. Elf64_Ehdr e64;
  37. } elf_ehdr;
  38. typedef union {
  39. Elf32_Phdr e32;
  40. Elf64_Phdr e64;
  41. } elf_phdr;
  42. typedef union {
  43. Elf32_Shdr e32;
  44. Elf64_Shdr e64;
  45. } elf_shdr;
  46. typedef union {
  47. Elf32_Sym e32;
  48. Elf64_Sym e64;
  49. } elf_sym;
  50. typedef union {
  51. Elf32_Rel e32;
  52. Elf64_Rel e64;
  53. } elf_rel;
  54. typedef union {
  55. Elf32_Rela e32;
  56. Elf64_Rela e64;
  57. } elf_rela;
  58. typedef union {
  59. Elf32_Note e32;
  60. Elf64_Note e64;
  61. } elf_note;
  62. struct elf_binary {
  63. /* elf binary */
  64. const char *image;
  65. size_t size;
  66. char class;
  67. char data;
  68. const elf_ehdr *ehdr;
  69. const char *sec_strtab;
  70. const elf_shdr *sym_tab;
  71. const char *sym_strtab;
  72. /* loaded to */
  73. char *dest;
  74. uint64_t pstart;
  75. uint64_t pend;
  76. uint64_t reloc_offset;
  77. uint64_t bsd_symtab_pstart;
  78. uint64_t bsd_symtab_pend;
  79. #ifndef __XEN__
  80. /* misc */
  81. FILE *log;
  82. #endif
  83. int verbose;
  84. };
  85. /* ------------------------------------------------------------------------ */
  86. /* accessing elf header fields */
  87. #ifdef XEN_ELF_BIG_ENDIAN
  88. # define NATIVE_ELFDATA ELFDATA2MSB
  89. #else
  90. # define NATIVE_ELFDATA ELFDATA2LSB
  91. #endif
  92. #define elf_32bit(elf) (ELFCLASS32 == (elf)->class)
  93. #define elf_64bit(elf) (ELFCLASS64 == (elf)->class)
  94. #define elf_msb(elf) (ELFDATA2MSB == (elf)->data)
  95. #define elf_lsb(elf) (ELFDATA2LSB == (elf)->data)
  96. #define elf_swap(elf) (NATIVE_ELFDATA != (elf)->data)
  97. #define elf_uval(elf, str, elem) \
  98. ((ELFCLASS64 == (elf)->class) \
  99. ? elf_access_unsigned((elf), (str), \
  100. offsetof(typeof(*(str)),e64.elem), \
  101. sizeof((str)->e64.elem)) \
  102. : elf_access_unsigned((elf), (str), \
  103. offsetof(typeof(*(str)),e32.elem), \
  104. sizeof((str)->e32.elem)))
  105. #define elf_sval(elf, str, elem) \
  106. ((ELFCLASS64 == (elf)->class) \
  107. ? elf_access_signed((elf), (str), \
  108. offsetof(typeof(*(str)),e64.elem), \
  109. sizeof((str)->e64.elem)) \
  110. : elf_access_signed((elf), (str), \
  111. offsetof(typeof(*(str)),e32.elem), \
  112. sizeof((str)->e32.elem)))
  113. #define elf_size(elf, str) \
  114. ((ELFCLASS64 == (elf)->class) \
  115. ? sizeof((str)->e64) : sizeof((str)->e32))
  116. uint64_t elf_access_unsigned(struct elf_binary *elf, const void *ptr,
  117. uint64_t offset, size_t size);
  118. int64_t elf_access_signed(struct elf_binary *elf, const void *ptr,
  119. uint64_t offset, size_t size);
  120. uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr);
  121. /* ------------------------------------------------------------------------ */
  122. /* xc_libelf_tools.c */
  123. int elf_shdr_count(struct elf_binary *elf);
  124. int elf_phdr_count(struct elf_binary *elf);
  125. const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name);
  126. const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index);
  127. const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index);
  128. const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr);
  129. const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr);
  130. const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr);
  131. const void *elf_segment_start(struct elf_binary *elf, const elf_phdr * phdr);
  132. const void *elf_segment_end(struct elf_binary *elf, const elf_phdr * phdr);
  133. const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol);
  134. const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index);
  135. const char *elf_note_name(struct elf_binary *elf, const elf_note * note);
  136. const void *elf_note_desc(struct elf_binary *elf, const elf_note * note);
  137. uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note);
  138. const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note);
  139. int elf_is_elfbinary(const void *image);
  140. int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr);
  141. /* ------------------------------------------------------------------------ */
  142. /* xc_libelf_loader.c */
  143. int elf_init(struct elf_binary *elf, const char *image, size_t size);
  144. #ifdef __XEN__
  145. void elf_set_verbose(struct elf_binary *elf);
  146. #else
  147. void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose);
  148. #endif
  149. void elf_parse_binary(struct elf_binary *elf);
  150. void elf_load_binary(struct elf_binary *elf);
  151. void *elf_get_ptr(struct elf_binary *elf, unsigned long addr);
  152. uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol);
  153. void elf_parse_bsdsyms(struct elf_binary *elf, uint64_t pstart); /* private */
  154. /* ------------------------------------------------------------------------ */
  155. /* xc_libelf_relocate.c */
  156. int elf_reloc(struct elf_binary *elf);
  157. /* ------------------------------------------------------------------------ */
  158. /* xc_libelf_dominfo.c */
  159. #define UNSET_ADDR ((uint64_t)-1)
  160. enum xen_elfnote_type {
  161. XEN_ENT_NONE = 0,
  162. XEN_ENT_LONG = 1,
  163. XEN_ENT_STR = 2
  164. };
  165. struct xen_elfnote {
  166. enum xen_elfnote_type type;
  167. const char *name;
  168. union {
  169. const char *str;
  170. uint64_t num;
  171. } data;
  172. };
  173. struct elf_dom_parms {
  174. /* raw */
  175. const char *guest_info;
  176. const void *elf_note_start;
  177. const void *elf_note_end;
  178. struct xen_elfnote elf_notes[XEN_ELFNOTE_MAX + 1];
  179. /* parsed */
  180. char guest_os[16];
  181. char guest_ver[16];
  182. char xen_ver[16];
  183. char loader[16];
  184. int pae;
  185. int bsd_symtab;
  186. uint64_t virt_base;
  187. uint64_t virt_entry;
  188. uint64_t virt_hypercall;
  189. uint64_t virt_hv_start_low;
  190. uint64_t elf_paddr_offset;
  191. uint32_t f_supported[XENFEAT_NR_SUBMAPS];
  192. uint32_t f_required[XENFEAT_NR_SUBMAPS];
  193. /* calculated */
  194. uint64_t virt_offset;
  195. uint64_t virt_kstart;
  196. uint64_t virt_kend;
  197. };
  198. static inline void elf_xen_feature_set(int nr, uint32_t * addr)
  199. {
  200. addr[nr >> 5] |= 1 << (nr & 31);
  201. }
  202. static inline int elf_xen_feature_get(int nr, uint32_t * addr)
  203. {
  204. return !!(addr[nr >> 5] & (1 << (nr & 31)));
  205. }
  206. int elf_xen_parse_features(const char *features,
  207. uint32_t *supported,
  208. uint32_t *required);
  209. int elf_xen_parse_note(struct elf_binary *elf,
  210. struct elf_dom_parms *parms,
  211. const elf_note *note);
  212. int elf_xen_parse_guest_info(struct elf_binary *elf,
  213. struct elf_dom_parms *parms);
  214. int elf_xen_parse(struct elf_binary *elf,
  215. struct elf_dom_parms *parms);
  216. #endif /* __XC_LIBELF__ */