~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/xcutils/readnotes.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
8
 
 
9
#include <sys/types.h>
 
10
#include <sys/stat.h>
 
11
#include <sys/mman.h>
 
12
 
 
13
#include <xg_private.h>
 
14
#include <xc_dom.h> /* gunzip bits */
 
15
 
 
16
#include <xen/libelf/libelf.h>
 
17
 
 
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
}
 
23
 
 
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);
 
29
 
 
30
        printf("%s: %#*" PRIx64 " (%d bytes)\n",
 
31
               prefix, 2+2*descsz, value, descsz);
 
32
}
 
33
 
 
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);
 
40
 
 
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
        }
 
53
 
 
54
}
 
55
 
 
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;
 
60
 
 
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;
 
65
 
 
66
                notes_found++;
 
67
 
 
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_SUSPEND_CANCEL:
 
107
                        print_numeric_note("SUSPEND_CANCEL", elf, note);
 
108
                        break;
 
109
                case XEN_ELFNOTE_L1_MFN_VALID:
 
110
                        print_l1_mfn_valid_note("L1_MFN_VALID", elf , note);
 
111
                        break;
 
112
                default:
 
113
                        printf("unknown note type %#x\n",
 
114
                               (int)elf_uval(elf, note, type));
 
115
                        break;
 
116
                }
 
117
        }
 
118
        return notes_found;
 
119
}
 
120
 
 
121
int main(int argc, char **argv)
 
122
{
 
123
        const char *f;
 
124
        int fd,h,size,usize,count;
 
125
        void *image,*tmp;
 
126
        struct stat st;
 
127
        struct elf_binary elf;
 
128
        const elf_shdr *shdr;
 
129
        int notes_found = 0;
 
130
 
 
131
        if (argc != 2)
 
132
        {
 
133
                fprintf(stderr, "Usage: readnotes <elfimage>\n");
 
134
                return 1;
 
135
        }
 
136
        f = argv[1];
 
137
 
 
138
        fd = open(f, O_RDONLY);
 
139
        if (fd == -1)
 
140
        {
 
141
                fprintf(stderr, "Unable to open %s: %s\n", f, strerror(errno));
 
142
                return 1;
 
143
        }
 
144
        if (fstat(fd, &st) == -1)
 
145
        {
 
146
                fprintf(stderr, "Unable to determine size of %s: %s\n",
 
147
                        f, strerror(errno));
 
148
                return 1;
 
149
        }
 
150
 
 
151
        image = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
 
152
        if (image == MAP_FAILED)
 
153
        {
 
154
                fprintf(stderr, "Unable to map %s: %s\n", f, strerror(errno));
 
155
                return 1;
 
156
        }
 
157
        size = st.st_size;
 
158
 
 
159
        usize = xc_dom_check_gzip(image, st.st_size);
 
160
        if (usize)
 
161
        {
 
162
                tmp = malloc(usize);
 
163
                xc_dom_do_gunzip(image, st.st_size, tmp, usize);
 
164
                image = tmp;
 
165
                size = usize;
 
166
        }
 
167
 
 
168
        if (0 != elf_init(&elf, image, size))
 
169
        {
 
170
                fprintf(stderr, "File %s is not an ELF image\n", f);
 
171
                return 1;
 
172
        }
 
173
        elf_set_logfile(&elf, stderr, 0);
 
174
 
 
175
        count = elf_phdr_count(&elf);
 
176
        for ( h=0; h < count; h++)
 
177
        {
 
178
                const elf_phdr *phdr;
 
179
                phdr = elf_phdr_by_index(&elf, h);
 
180
                if (elf_uval(&elf, phdr, p_type) != PT_NOTE)
 
181
                        continue;
 
182
 
 
183
                /* Some versions of binutils do not correctly set
 
184
                 * p_offset for note segments.
 
185
                 */
 
186
                if (elf_uval(&elf, phdr, p_offset) == 0)
 
187
                        continue;
 
188
 
 
189
                notes_found = print_notes(&elf,
 
190
                                          elf_segment_start(&elf, phdr),
 
191
                                          elf_segment_end(&elf, phdr));
 
192
        }
 
193
 
 
194
        if ( notes_found == 0 )
 
195
        {
 
196
                count = elf_shdr_count(&elf);
 
197
                for ( h=0; h < count; h++)
 
198
                {
 
199
                        const elf_shdr *shdr;
 
200
                        shdr = elf_shdr_by_index(&elf, h);
 
201
                        if (elf_uval(&elf, shdr, sh_type) != SHT_NOTE)
 
202
                                continue;
 
203
                        notes_found = print_notes(&elf,
 
204
                                                  elf_section_start(&elf, shdr),
 
205
                                                  elf_section_end(&elf, shdr));
 
206
                        if ( notes_found )
 
207
                                fprintf(stderr, "using notes from SHT_NOTE section\n");
 
208
 
 
209
                }
 
210
        }
 
211
 
 
212
        shdr = elf_shdr_by_name(&elf, "__xen_guest");
 
213
        if (shdr)
 
214
                printf("__xen_guest: %s\n", (char*)elf_section_start(&elf, shdr));
 
215
 
 
216
        return 0;
 
217
}
 
218
 
 
219