ia64/xen-unstable

view tools/xcutils/readnotes.c @ 14164:2cc8c117b7b8

[TOOLS] Add support for XEN_ELFNOTE_L1_MFN_VALID to readnotes.

Signed-off-by: Ian Campbell <ian.campbell@xensource.com>
author Ian Campbell <ian.campbell@xensource.com>
date Tue Feb 27 15:39:08 2007 +0000 (2007-02-27)
parents 5a07ea77a61d
children 9149ef0e61ce
line source
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <inttypes.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/mman.h>
13 #include <xg_private.h>
14 #include <xc_dom.h> /* gunzip bits */
16 #include <xen/libelf.h>
18 static void print_string_note(const char *prefix, struct elf_binary *elf,
19 const elf_note *note)
20 {
21 printf("%s: %s\n", prefix, (char*)elf_note_desc(elf, note));
22 }
24 static void print_numeric_note(const char *prefix, struct elf_binary *elf,
25 const elf_note *note)
26 {
27 uint64_t value = elf_note_numeric(elf, note);
28 int descsz = elf_uval(elf, note, descsz);
30 printf("%s: %#*" PRIx64 " (%d bytes)\n",
31 prefix, 2+2*descsz, value, descsz);
32 }
34 static void print_l1_mfn_valid_note(const char *prefix, struct elf_binary *elf,
35 const elf_note *note)
36 {
37 int descsz = elf_uval(elf, note, descsz);
38 const uint32_t *desc32 = elf_note_desc(elf, note);
39 const uint64_t *desc64 = elf_note_desc(elf, note);
41 /* XXX should be able to cope with a list of values. */
42 switch ( descsz / 2 )
43 {
44 case 8:
45 printf("%s: mask=%#"PRIx64" value=%#"PRIx64"\n", prefix,
46 desc64[0], desc64[1]);
47 break;
48 case 4:
49 printf("%s: mask=%#"PRIx32" value=%#"PRIx32"\n", prefix,
50 desc32[0],desc32[1]);
51 break;
52 }
54 }
56 static int print_notes(struct elf_binary *elf, const elf_note *start, const elf_note *end)
57 {
58 const elf_note *note;
59 int notes_found = 0;
61 for ( note = start; note < end; note = elf_note_next(elf, note) )
62 {
63 if (0 != strcmp(elf_note_name(elf, note), "Xen"))
64 continue;
66 notes_found++;
68 switch(elf_uval(elf, note, type))
69 {
70 case XEN_ELFNOTE_INFO:
71 print_string_note("INFO", elf , note);
72 break;
73 case XEN_ELFNOTE_ENTRY:
74 print_numeric_note("ENTRY", elf , note);
75 break;
76 case XEN_ELFNOTE_HYPERCALL_PAGE:
77 print_numeric_note("HYPERCALL_PAGE", elf , note);
78 break;
79 case XEN_ELFNOTE_VIRT_BASE:
80 print_numeric_note("VIRT_BASE", elf , note);
81 break;
82 case XEN_ELFNOTE_PADDR_OFFSET:
83 print_numeric_note("PADDR_OFFSET", elf , note);
84 break;
85 case XEN_ELFNOTE_XEN_VERSION:
86 print_string_note("XEN_VERSION", elf , note);
87 break;
88 case XEN_ELFNOTE_GUEST_OS:
89 print_string_note("GUEST_OS", elf , note);
90 break;
91 case XEN_ELFNOTE_GUEST_VERSION:
92 print_string_note("GUEST_VERSION", elf , note);
93 break;
94 case XEN_ELFNOTE_LOADER:
95 print_string_note("LOADER", elf , note);
96 break;
97 case XEN_ELFNOTE_PAE_MODE:
98 print_string_note("PAE_MODE", elf , note);
99 break;
100 case XEN_ELFNOTE_FEATURES:
101 print_string_note("FEATURES", elf , note);
102 break;
103 case XEN_ELFNOTE_HV_START_LOW:
104 print_numeric_note("HV_START_LOW", elf , note);
105 break;
106 case XEN_ELFNOTE_L1_MFN_VALID:
107 print_l1_mfn_valid_note("L1_MFN_VALID", elf , note);
108 break;
109 default:
110 printf("unknown note type %#x\n",
111 (int)elf_uval(elf, note, type));
112 break;
113 }
114 }
115 return notes_found;
116 }
118 int main(int argc, char **argv)
119 {
120 const char *f;
121 int fd,h,size,usize,count;
122 void *image,*tmp;
123 struct stat st;
124 struct elf_binary elf;
125 const elf_shdr *shdr;
126 int notes_found = 0;
128 if (argc != 2)
129 {
130 fprintf(stderr, "Usage: readnotes <elfimage>\n");
131 return 1;
132 }
133 f = argv[1];
135 fd = open(f, O_RDONLY);
136 if (fd == -1)
137 {
138 fprintf(stderr, "Unable to open %s: %s\n", f, strerror(errno));
139 return 1;
140 }
141 if (fstat(fd, &st) == -1)
142 {
143 fprintf(stderr, "Unable to determine size of %s: %s\n",
144 f, strerror(errno));
145 return 1;
146 }
148 image = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
149 if (image == MAP_FAILED)
150 {
151 fprintf(stderr, "Unable to map %s: %s\n", f, strerror(errno));
152 return 1;
153 }
154 size = st.st_size;
156 usize = xc_dom_check_gzip(image, st.st_size);
157 if (usize)
158 {
159 tmp = malloc(usize);
160 xc_dom_do_gunzip(image, st.st_size, tmp, usize);
161 image = tmp;
162 size = usize;
163 }
165 if (0 != elf_init(&elf, image, size))
166 {
167 fprintf(stderr, "File %s is not an ELF image\n", f);
168 return 1;
169 }
170 elf_set_logfile(&elf, stderr, 0);
172 count = elf_phdr_count(&elf);
173 for ( h=0; h < count; h++)
174 {
175 const elf_phdr *phdr;
176 phdr = elf_phdr_by_index(&elf, h);
177 if (elf_uval(&elf, phdr, p_type) != PT_NOTE)
178 continue;
180 /* Some versions of binutils do not correctly set
181 * p_offset for note segments.
182 */
183 if (elf_uval(&elf, phdr, p_offset) == 0)
184 continue;
186 notes_found = print_notes(&elf,
187 elf_segment_start(&elf, phdr),
188 elf_segment_end(&elf, phdr));
189 }
191 if ( notes_found == 0 )
192 {
193 count = elf_shdr_count(&elf);
194 for ( h=0; h < count; h++)
195 {
196 const elf_shdr *shdr;
197 shdr = elf_shdr_by_index(&elf, h);
198 if (elf_uval(&elf, shdr, sh_type) != SHT_NOTE)
199 continue;
200 notes_found = print_notes(&elf,
201 elf_section_start(&elf, shdr),
202 elf_section_end(&elf, shdr));
203 if ( notes_found )
204 fprintf(stderr, "using notes from SHT_NOTE section\n");
206 }
207 }
209 shdr = elf_shdr_by_name(&elf, "__xen_guest");
210 if (shdr)
211 printf("__xen_guest: %s\n", (char*)elf_section_start(&elf, shdr));
213 return 0;
214 }