Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / xen / include / xen3-public / libelf.h
blob727d37c2a153160045e91ac1d62aee0e998ad402
1 /* $NetBSD: libelf.h,v 1.3 2008/05/04 19:56:28 cegger Exp $ */
2 /******************************************************************************
3 * libelf.h
4 *
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.
24 #ifndef __XC_LIBELF__
25 #define __XC_LIBELF__ 1
27 #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
28 #define XEN_ELF_LITTLE_ENDIAN
29 #else
30 #error define architectural endianness
31 #endif
33 #undef ELFSIZE
34 #include "elfnote.h"
35 #include "elfstructs.h"
36 #include "features.h"
38 /* ------------------------------------------------------------------------ */
40 typedef union {
41 Elf32_Ehdr e32;
42 Elf64_Ehdr e64;
43 } elf_ehdr;
45 typedef union {
46 Elf32_Phdr e32;
47 Elf64_Phdr e64;
48 } elf_phdr;
50 typedef union {
51 Elf32_Shdr e32;
52 Elf64_Shdr e64;
53 } elf_shdr;
55 typedef union {
56 Elf32_Sym e32;
57 Elf64_Sym e64;
58 } elf_sym;
60 typedef union {
61 Elf32_Rel e32;
62 Elf64_Rel e64;
63 } elf_rel;
65 typedef union {
66 Elf32_Rela e32;
67 Elf64_Rela e64;
68 } elf_rela;
70 typedef union {
71 Elf32_Note e32;
72 Elf64_Note e64;
73 } elf_note;
75 struct elf_binary {
76 /* elf binary */
77 const char *image;
78 size_t size;
79 char class;
80 char data;
82 const elf_ehdr *ehdr;
83 const char *sec_strtab;
84 const elf_shdr *sym_tab;
85 const char *sym_strtab;
87 /* loaded to */
88 char *dest;
89 uint64_t pstart;
90 uint64_t pend;
91 uint64_t reloc_offset;
93 uint64_t bsd_symtab_pstart;
94 uint64_t bsd_symtab_pend;
96 #ifndef __XEN__
97 /* misc */
98 FILE *log;
99 #endif
100 int verbose;
103 /* ------------------------------------------------------------------------ */
104 /* accessing elf header fields */
106 #ifdef XEN_ELF_BIG_ENDIAN
107 # define NATIVE_ELFDATA ELFDATA2MSB
108 #else
109 # define NATIVE_ELFDATA ELFDATA2LSB
110 #endif
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);
179 #ifdef __XEN__
180 void elf_set_verbose(struct elf_binary *elf);
181 #else
182 void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose);
183 #endif
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 {
204 XEN_ENT_NONE = 0,
205 XEN_ENT_LONG = 1,
206 XEN_ENT_STR = 2
209 struct xen_elfnote {
210 enum xen_elfnote_type type;
211 const char *name;
212 union {
213 const char *str;
214 uint64_t num;
215 } data;
218 struct elf_dom_parms {
219 /* raw */
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];
225 /* parsed */
226 char guest_os[16];
227 char guest_ver[16];
228 char xen_ver[16];
229 char loader[16];
230 int pae;
231 int bsd_symtab;
232 uint64_t virt_base;
233 uint64_t virt_entry;
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];
240 /* calculated */
241 uint64_t virt_offset;
242 uint64_t virt_kstart;
243 uint64_t virt_kend;
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,
256 uint32_t *supported,
257 uint32_t *required);
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__ */