1 /* $NetBSD: libelf.h,v 1.3 2008/05/04 19:56:28 cegger Exp $ */
2 /******************************************************************************
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #define __XC_LIBELF__ 1
27 #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
28 #define XEN_ELF_LITTLE_ENDIAN
30 #error define architectural endianness
35 #include "elfstructs.h"
38 /* ------------------------------------------------------------------------ */
83 const char *sec_strtab
;
84 const elf_shdr
*sym_tab
;
85 const char *sym_strtab
;
91 uint64_t reloc_offset
;
93 uint64_t bsd_symtab_pstart
;
94 uint64_t bsd_symtab_pend
;
103 /* ------------------------------------------------------------------------ */
104 /* accessing elf header fields */
106 #ifdef XEN_ELF_BIG_ENDIAN
107 # define NATIVE_ELFDATA ELFDATA2MSB
109 # define NATIVE_ELFDATA ELFDATA2LSB
112 #define elf_32bit(elf) (ELFCLASS32 == (elf)->class)
113 #define elf_64bit(elf) (ELFCLASS64 == (elf)->class)
114 #define elf_msb(elf) (ELFDATA2MSB == (elf)->data)
115 #define elf_lsb(elf) (ELFDATA2LSB == (elf)->data)
116 #define elf_swap(elf) (NATIVE_ELFDATA != (elf)->data)
118 #define elf_uval(elf, str, elem) \
119 ((ELFCLASS64 == (elf)->class) \
120 ? elf_access_unsigned((elf), (str), \
121 offsetof(typeof(*(str)),e64.elem), \
122 sizeof((str)->e64.elem)) \
123 : elf_access_unsigned((elf), (str), \
124 offsetof(typeof(*(str)),e32.elem), \
125 sizeof((str)->e32.elem)))
127 #define elf_sval(elf, str, elem) \
128 ((ELFCLASS64 == (elf)->class) \
129 ? elf_access_signed((elf), (str), \
130 offsetof(typeof(*(str)),e64.elem), \
131 sizeof((str)->e64.elem)) \
132 : elf_access_signed((elf), (str), \
133 offsetof(typeof(*(str)),e32.elem), \
134 sizeof((str)->e32.elem)))
136 #define elf_size(elf, str) \
137 ((ELFCLASS64 == (elf)->class) \
138 ? sizeof((str)->e64) : sizeof((str)->e32))
140 uint64_t elf_access_unsigned(struct elf_binary
*elf
, const void *ptr
,
141 uint64_t offset
, size_t size
);
142 int64_t elf_access_signed(struct elf_binary
*elf
, const void *ptr
,
143 uint64_t offset
, size_t size
);
145 uint64_t elf_round_up(struct elf_binary
*elf
, uint64_t addr
);
147 /* ------------------------------------------------------------------------ */
148 /* xc_libelf_tools.c */
150 int elf_shdr_count(struct elf_binary
*elf
);
151 int elf_phdr_count(struct elf_binary
*elf
);
153 const elf_shdr
*elf_shdr_by_name(struct elf_binary
*elf
, const char *name
);
154 const elf_shdr
*elf_shdr_by_index(struct elf_binary
*elf
, int index
);
155 const elf_phdr
*elf_phdr_by_index(struct elf_binary
*elf
, int index
);
157 const char *elf_section_name(struct elf_binary
*elf
, const elf_shdr
* shdr
);
158 const void *elf_section_start(struct elf_binary
*elf
, const elf_shdr
* shdr
);
159 const void *elf_section_end(struct elf_binary
*elf
, const elf_shdr
* shdr
);
161 const void *elf_segment_start(struct elf_binary
*elf
, const elf_phdr
* phdr
);
162 const void *elf_segment_end(struct elf_binary
*elf
, const elf_phdr
* phdr
);
164 const elf_sym
*elf_sym_by_name(struct elf_binary
*elf
, const char *symbol
);
165 const elf_sym
*elf_sym_by_index(struct elf_binary
*elf
, int index
);
167 const char *elf_note_name(struct elf_binary
*elf
, const elf_note
* note
);
168 const void *elf_note_desc(struct elf_binary
*elf
, const elf_note
* note
);
169 uint64_t elf_note_numeric(struct elf_binary
*elf
, const elf_note
* note
);
170 const elf_note
*elf_note_next(struct elf_binary
*elf
, const elf_note
* note
);
172 int elf_is_elfbinary(const void *image
);
173 int elf_phdr_is_loadable(struct elf_binary
*elf
, const elf_phdr
* phdr
);
175 /* ------------------------------------------------------------------------ */
176 /* xc_libelf_loader.c */
178 int elf_init(struct elf_binary
*elf
, const char *image
, size_t size
);
180 void elf_set_verbose(struct elf_binary
*elf
);
182 void elf_set_logfile(struct elf_binary
*elf
, FILE * log
, int verbose
);
185 void elf_parse_binary(struct elf_binary
*elf
);
186 void elf_load_binary(struct elf_binary
*elf
);
188 void *elf_get_ptr(struct elf_binary
*elf
, unsigned long addr
);
189 uint64_t elf_lookup_addr(struct elf_binary
*elf
, const char *symbol
);
191 void elf_parse_bsdsyms(struct elf_binary
*elf
, uint64_t pstart
); /* private */
193 /* ------------------------------------------------------------------------ */
194 /* xc_libelf_relocate.c */
196 int elf_reloc(struct elf_binary
*elf
);
198 /* ------------------------------------------------------------------------ */
199 /* xc_libelf_dominfo.c */
201 #define UNSET_ADDR ((uint64_t)-1)
203 enum xen_elfnote_type
{
210 enum xen_elfnote_type type
;
218 struct elf_dom_parms
{
220 const char *guest_info
;
221 const void *elf_note_start
;
222 const void *elf_note_end
;
223 struct xen_elfnote elf_notes
[XEN_ELFNOTE_MAX
+ 1];
234 uint64_t virt_hypercall
;
235 uint64_t virt_hv_start_low
;
236 uint64_t elf_paddr_offset
;
237 uint32_t f_supported
[XENFEAT_NR_SUBMAPS
];
238 uint32_t f_required
[XENFEAT_NR_SUBMAPS
];
241 uint64_t virt_offset
;
242 uint64_t virt_kstart
;
246 static inline void elf_xen_feature_set(int nr
, uint32_t * addr
)
248 addr
[nr
>> 5] |= 1 << (nr
& 31);
250 static inline int elf_xen_feature_get(int nr
, uint32_t * addr
)
252 return !!(addr
[nr
>> 5] & (1 << (nr
& 31)));
255 int elf_xen_parse_features(const char *features
,
258 int elf_xen_parse_note(struct elf_binary
*elf
,
259 struct elf_dom_parms
*parms
,
260 const elf_note
*note
);
261 int elf_xen_parse_guest_info(struct elf_binary
*elf
,
262 struct elf_dom_parms
*parms
);
263 int elf_xen_parse(struct elf_binary
*elf
,
264 struct elf_dom_parms
*parms
);
266 #endif /* __XC_LIBELF__ */