[udis86] Add support for 3-byte opcodes.
[ksplice.git] / inspect.c
blob09fb330222c61545311a70ef8be120cb6c91ef2a
1 /* Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
2 * Tim Abbott <tabbott@mit.edu>,
3 * Jeffrey Brian Arnold <jbarnold@mit.edu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
16 * 02110-1301, USA.
19 /* Always define KSPLICE_STANDALONE, even if you're using integrated Ksplice.
20 inspect won't compile without it. */
21 #define KSPLICE_STANDALONE
23 #define _GNU_SOURCE
24 #include "objcommon.h"
25 #include "kmodsrc/ksplice.h"
26 #include <stdio.h>
28 char *str_ulong_vec(struct supersect *ss, const unsigned long *const *datap,
29 const unsigned long *sizep)
31 struct supersect *data_ss;
32 const unsigned long *data =
33 read_pointer(ss, (void *const *)datap, &data_ss);
34 unsigned long size = read_num(ss, sizep);
36 char *buf = NULL;
37 size_t bufsize = 0;
38 FILE *fp = open_memstream(&buf, &bufsize);
39 fprintf(fp, "[ ");
40 size_t i;
41 for (i = 0; i < size; ++i)
42 fprintf(fp, "%lx ", read_num(data_ss, &data[i]));
43 fprintf(fp, "]");
44 fclose(fp);
45 return buf;
48 char *str_ksplice_symbol(struct supersect *ss,
49 const struct ksplice_symbol *ksymbol)
51 char *str;
52 assert(asprintf(&str, "%s (%s)",
53 read_string(ss, &ksymbol->label),
54 read_string(ss, &ksymbol->name)));
55 return str;
58 char *str_ksplice_symbolp(struct supersect *ptr_ss,
59 const struct ksplice_symbol *const *ksymbolp)
61 struct supersect *ss;
62 const struct ksplice_symbol *ksymbol =
63 read_pointer(ptr_ss, (void *const *)ksymbolp, &ss);
64 return ksymbol == NULL ? "(null)" : str_ksplice_symbol(ss, ksymbol);
67 void show_ksplice_reloc(struct supersect *ss,
68 const struct ksplice_reloc *kreloc)
70 printf("blank_addr: %s blank_offset: %lx\n"
71 "symbol: %s\n"
72 "addend: %lx\n"
73 "pcrel: %x size: %x dst_mask: %lx rightshift: %x\n"
74 "\n",
75 str_pointer(ss, (void *const *)&kreloc->blank_addr),
76 read_num(ss, &kreloc->blank_offset),
77 str_ksplice_symbolp(ss, &kreloc->symbol),
78 read_num(ss, &kreloc->addend),
79 read_num(ss, &kreloc->pcrel),
80 read_num(ss, &kreloc->size),
81 read_num(ss, &kreloc->dst_mask),
82 read_num(ss, &kreloc->rightshift));
85 void show_ksplice_relocs(struct supersect *kreloc_ss)
87 printf("KSPLICE RELOCATIONS IN SECTION %s:\n\n", kreloc_ss->name);
88 const struct ksplice_reloc *kreloc;
89 for (kreloc = kreloc_ss->contents.data; (void *)kreloc <
90 kreloc_ss->contents.data + kreloc_ss->contents.size; kreloc++)
91 show_ksplice_reloc(kreloc_ss, kreloc);
92 printf("\n");
95 void show_ksplice_section_flags(const struct ksplice_section *ksect)
97 printf("flags:");
98 if (ksect->flags & KSPLICE_SECTION_RODATA)
99 printf(" rodata");
100 if (ksect->flags & KSPLICE_SECTION_TEXT)
101 printf(" text");
102 if (ksect->flags & KSPLICE_SECTION_DATA)
103 printf(" data");
104 printf("\n");
107 void show_ksplice_section(struct supersect *ss,
108 const struct ksplice_section *ksect)
110 printf("symbol: %s\n"
111 "address: %s size: %lx\n",
112 str_ksplice_symbolp(ss, &ksect->symbol),
113 str_pointer(ss, (void *const *)&ksect->address),
114 read_num(ss, &ksect->size));
115 show_ksplice_section_flags(ksect);
116 printf("\n");
119 void show_ksplice_sections(struct supersect *ksect_ss)
121 printf("KSPLICE SECTIONS:\n\n");
122 struct ksplice_section *ksect;
123 for (ksect = ksect_ss->contents.data; (void *)ksect <
124 ksect_ss->contents.data + ksect_ss->contents.size; ksect++)
125 show_ksplice_section(ksect_ss, ksect);
126 printf("\n");
129 void show_ksplice_patch(struct supersect *ss,
130 const struct ksplice_patch *kpatch)
132 printf("label: %s\n"
133 "repladdr: %s\n"
134 "\n",
135 read_string(ss, &kpatch->label),
136 str_pointer(ss, (void *const *)&kpatch->repladdr));
139 void show_ksplice_patches(struct supersect *kpatch_ss)
141 printf("KSPLICE PATCHES:\n\n");
142 const struct ksplice_patch *kpatch;
143 for (kpatch = kpatch_ss->contents.data; (void *)kpatch <
144 kpatch_ss->contents.data + kpatch_ss->contents.size; kpatch++)
145 show_ksplice_patch(kpatch_ss, kpatch);
146 printf("\n");
149 void show_ksplice_export(struct supersect *ss, const struct ksplice_export *exp)
151 printf("name: %s\n"
152 "newname: %s\n"
153 "\n",
154 read_string(ss, &exp->name), read_string(ss, &exp->new_name));
157 void show_ksplice_exports(struct supersect *export_ss)
159 printf("KSPLICE EXPORTS:\n\n");
160 const struct ksplice_export *exp;
161 for (exp = export_ss->contents.data; (void *)exp <
162 export_ss->contents.data + export_ss->contents.size; exp++)
163 show_ksplice_export(export_ss, exp);
164 printf("\n");
167 void show_ksplice_system_map(struct supersect *ss,
168 const struct ksplice_system_map *smap)
170 printf("%s %s\n",
171 read_string(ss, &smap->label),
172 str_ulong_vec(ss, &smap->candidates, &smap->nr_candidates));
175 void show_ksplice_system_maps(struct supersect *smap_ss)
177 printf("KSPLICE SYSTEM.MAP:\n\n");
178 const struct ksplice_system_map *smap;
179 for (smap = smap_ss->contents.data;
180 (void *)smap < smap_ss->contents.data + smap_ss->contents.size;
181 smap++)
182 show_ksplice_system_map(smap_ss, smap);
183 printf("\n");
186 int main(int argc, char *argv[])
188 bfd *ibfd;
190 assert(argc >= 1);
191 bfd_init();
192 ibfd = bfd_openr(argv[1], NULL);
193 assert(ibfd);
195 char **matching;
196 assert(bfd_check_format_matches(ibfd, bfd_object, &matching));
198 struct superbfd *sbfd = fetch_superbfd(ibfd);
200 asection *kreloc_init_sect =
201 bfd_get_section_by_name(ibfd, ".ksplice_init_relocs");
202 if (kreloc_init_sect != NULL) {
203 struct supersect *kreloc_init_ss =
204 fetch_supersect(sbfd, kreloc_init_sect);
205 show_ksplice_relocs(kreloc_init_ss);
206 } else {
207 printf("No ksplice init relocations.\n\n");
210 asection *kreloc_sect = bfd_get_section_by_name(ibfd,
211 ".ksplice_relocs");
212 if (kreloc_sect != NULL) {
213 struct supersect *kreloc_ss =
214 fetch_supersect(sbfd, kreloc_sect);
215 show_ksplice_relocs(kreloc_ss);
216 } else {
217 printf("No ksplice relocations.\n\n");
220 asection *ksect_sect = bfd_get_section_by_name(ibfd,
221 ".ksplice_sections");
222 if (ksect_sect != NULL) {
223 struct supersect *ksect_ss = fetch_supersect(sbfd, ksect_sect);
224 show_ksplice_sections(ksect_ss);
225 } else {
226 printf("No ksplice sections.\n\n");
229 asection *kpatch_sect = bfd_get_section_by_name(ibfd,
230 ".ksplice_patches");
231 if (kpatch_sect != NULL) {
232 struct supersect *kpatch_ss =
233 fetch_supersect(sbfd, kpatch_sect);
234 show_ksplice_patches(kpatch_ss);
235 } else {
236 printf("No ksplice patches.\n\n");
239 asection *export_sect = bfd_get_section_by_name(ibfd,
240 ".ksplice_exports");
241 if (export_sect != NULL) {
242 struct supersect *export_ss =
243 fetch_supersect(sbfd, export_sect);
244 show_ksplice_exports(export_ss);
245 } else {
246 printf("No ksplice exports.\n\n");
249 asection *smap_sect = bfd_get_section_by_name(ibfd,
250 ".ksplice_system_map");
251 if (smap_sect != NULL) {
252 struct supersect *smap_ss = fetch_supersect(sbfd, smap_sect);
253 show_ksplice_system_maps(smap_ss);
254 } else {
255 printf("No ksplice System.map.\n\n");
258 return 0;