1 /******************************************************************************
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #define __XC_LIBELF__ 1
26 #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
27 #define XEN_ELF_LITTLE_ENDIAN
29 #error define architectural endianness
34 #include "elfstructs.h"
37 /* ------------------------------------------------------------------------ */
82 const char *sec_strtab;
83 const elf_shdr *sym_tab;
84 const char *sym_strtab;
90 uint64_t reloc_offset;
92 uint64_t bsd_symtab_pstart;
93 uint64_t bsd_symtab_pend;
102 /* ------------------------------------------------------------------------ */
103 /* accessing elf header fields */
105 #ifdef XEN_ELF_BIG_ENDIAN
106 # define NATIVE_ELFDATA ELFDATA2MSB
108 # define NATIVE_ELFDATA ELFDATA2LSB
111 #define elf_32bit(elf) (ELFCLASS32 == (elf)->class)
112 #define elf_64bit(elf) (ELFCLASS64 == (elf)->class)
113 #define elf_msb(elf) (ELFDATA2MSB == (elf)->data)
114 #define elf_lsb(elf) (ELFDATA2LSB == (elf)->data)
115 #define elf_swap(elf) (NATIVE_ELFDATA != (elf)->data)
117 #define elf_uval(elf, str, elem) \
118 ((ELFCLASS64 == (elf)->class) \
119 ? elf_access_unsigned((elf), (str), \
120 offsetof(typeof(*(str)),e64.elem), \
121 sizeof((str)->e64.elem)) \
122 : elf_access_unsigned((elf), (str), \
123 offsetof(typeof(*(str)),e32.elem), \
124 sizeof((str)->e32.elem)))
126 #define elf_sval(elf, str, elem) \
127 ((ELFCLASS64 == (elf)->class) \
128 ? elf_access_signed((elf), (str), \
129 offsetof(typeof(*(str)),e64.elem), \
130 sizeof((str)->e64.elem)) \
131 : elf_access_signed((elf), (str), \
132 offsetof(typeof(*(str)),e32.elem), \
133 sizeof((str)->e32.elem)))
135 #define elf_size(elf, str) \
136 ((ELFCLASS64 == (elf)->class) \
137 ? sizeof((str)->e64) : sizeof((str)->e32))
139 uint64_t elf_access_unsigned(struct elf_binary *elf, const void *ptr,
140 uint64_t offset, size_t size);
141 int64_t elf_access_signed(struct elf_binary *elf, const void *ptr,
142 uint64_t offset, size_t size);
144 uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr);
146 /* ------------------------------------------------------------------------ */
147 /* xc_libelf_tools.c */
149 int elf_shdr_count(struct elf_binary *elf);
150 int elf_phdr_count(struct elf_binary *elf);
152 const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name);
153 const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index);
154 const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index);
156 const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr);
157 const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr);
158 const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr);
160 const void *elf_segment_start(struct elf_binary *elf, const elf_phdr * phdr);
161 const void *elf_segment_end(struct elf_binary *elf, const elf_phdr * phdr);
163 const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol);
164 const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index);
166 const char *elf_note_name(struct elf_binary *elf, const elf_note * note);
167 const void *elf_note_desc(struct elf_binary *elf, const elf_note * note);
168 uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note);
169 const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note);
171 int elf_is_elfbinary(const void *image);
172 int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr);
174 /* ------------------------------------------------------------------------ */
175 /* xc_libelf_loader.c */
177 int elf_init(struct elf_binary *elf, const char *image, size_t size);
179 void elf_set_verbose(struct elf_binary *elf);
181 void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose);
184 void elf_parse_binary(struct elf_binary *elf);
185 void elf_load_binary(struct elf_binary *elf);
187 void *elf_get_ptr(struct elf_binary *elf, unsigned long addr);
188 uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol);
190 void elf_parse_bsdsyms(struct elf_binary *elf, uint64_t pstart); /* private */
192 /* ------------------------------------------------------------------------ */
193 /* xc_libelf_relocate.c */
195 int elf_reloc(struct elf_binary *elf);
197 /* ------------------------------------------------------------------------ */
198 /* xc_libelf_dominfo.c */
200 #define UNSET_ADDR ((uint64_t)-1)
202 enum xen_elfnote_type {
209 enum xen_elfnote_type type;
217 struct elf_dom_parms {
219 const char *guest_info;
220 const void *elf_note_start;
221 const void *elf_note_end;
222 struct xen_elfnote elf_notes[XEN_ELFNOTE_MAX + 1];
233 uint64_t virt_hypercall;
234 uint64_t virt_hv_start_low;
235 uint64_t elf_paddr_offset;
236 uint32_t f_supported[XENFEAT_NR_SUBMAPS];
237 uint32_t f_required[XENFEAT_NR_SUBMAPS];
240 uint64_t virt_offset;
241 uint64_t virt_kstart;
245 static inline void elf_xen_feature_set(int nr, uint32_t * addr)
247 addr[nr >> 5] |= 1 << (nr & 31);
249 static inline int elf_xen_feature_get(int nr, uint32_t * addr)
251 return !!(addr[nr >> 5] & (1 << (nr & 31)));
254 int elf_xen_parse_features(const char *features,
257 int elf_xen_parse_note(struct elf_binary *elf,
258 struct elf_dom_parms *parms,
259 const elf_note *note);
260 int elf_xen_parse_guest_info(struct elf_binary *elf,
261 struct elf_dom_parms *parms);
262 int elf_xen_parse(struct elf_binary *elf,
263 struct elf_dom_parms *parms);
265 #endif /* __XC_LIBELF__ */