Replace find_nameval(*, *, 1) with a new function create_nameval.
[ksplice.git] / inspect.c
blob32f5b4347d69c2cfae3eb3d6e4daaffa2795cc2d
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 #define _GNU_SOURCE
20 #include "objcommon.h"
21 #include "kmodsrc/ksplice.h"
22 #include <stdio.h>
24 char *str_ulong_vec(struct supersect *ss, const unsigned long *const *datap,
25 const unsigned long *sizep)
27 struct supersect *data_ss;
28 const unsigned long *data =
29 read_pointer(ss, (void *const *)datap, &data_ss);
30 unsigned long size = read_num(ss, sizep);
32 char *buf = NULL;
33 size_t bufsize = 0;
34 FILE *fp = open_memstream(&buf, &bufsize);
35 fprintf(fp, "[ ");
36 size_t i;
37 for (i = 0; i < size; ++i)
38 fprintf(fp, "%lx ", read_num(data_ss, &data[i]));
39 fprintf(fp, "]");
40 fclose(fp);
41 return buf;
44 void show_ksplice_reloc(struct supersect *ss,
45 const struct ksplice_reloc *kreloc)
47 printf("blank_addr: %s blank_offset: %lx\n"
48 "sym_name: %s\n"
49 "addend: %lx\n"
50 "pcrel: %x size: %x dst_mask: %lx rightshift: %x\n"
51 "sym_addrs: %s\n"
52 "\n",
53 str_pointer(ss, (void *const *)&kreloc->blank_addr),
54 read_num(ss, &kreloc->blank_offset),
55 read_string(ss, &kreloc->sym_name),
56 read_num(ss, &kreloc->addend),
57 read_num(ss, &kreloc->pcrel),
58 read_num(ss, &kreloc->size),
59 read_num(ss, &kreloc->dst_mask),
60 read_num(ss, &kreloc->rightshift),
61 str_ulong_vec(ss, &kreloc->sym_addrs, &kreloc->num_sym_addrs));
64 void show_ksplice_relocs(struct supersect *kreloc_ss)
66 printf("KSPLICE RELOCATIONS IN SECTION %s:\n\n", kreloc_ss->name);
67 const struct ksplice_reloc *kreloc;
68 for (kreloc = kreloc_ss->contents.data; (void *)kreloc <
69 kreloc_ss->contents.data + kreloc_ss->contents.size; kreloc++)
70 show_ksplice_reloc(kreloc_ss, kreloc);
71 printf("\n");
74 void show_ksplice_size_flags(const struct ksplice_size *ksize)
76 printf("flags:");
77 if (ksize->flags & KSPLICE_SIZE_DELETED)
78 printf(" deleted");
79 if (ksize->flags & KSPLICE_SIZE_RODATA)
80 printf(" rodata");
81 printf("\n");
84 void show_ksplice_size(struct supersect *ss, const struct ksplice_size *ksize)
86 printf("name: %s\n"
87 "thismod_addr: %s size: %lx\n"
88 "sym_addrs: %s\n",
89 read_string(ss, &ksize->name),
90 str_pointer(ss, (void *const *)&ksize->thismod_addr),
91 read_num(ss, &ksize->size),
92 str_ulong_vec(ss, &ksize->sym_addrs, &ksize->num_sym_addrs));
93 show_ksplice_size_flags(ksize);
94 printf("\n");
97 void show_ksplice_sizes(struct supersect *ksize_ss)
99 printf("KSPLICE SIZES:\n\n");
100 struct ksplice_size *ksize;
101 for (ksize = ksize_ss->contents.data; (void *)ksize <
102 ksize_ss->contents.data + ksize_ss->contents.size; ksize++)
103 show_ksplice_size(ksize_ss, ksize);
104 printf("\n");
107 void show_ksplice_patch(struct supersect *ss,
108 const struct ksplice_patch *kpatch)
110 printf("oldstr: %s\n"
111 "repladdr: %s\n"
112 "\n",
113 read_string(ss, &kpatch->oldstr),
114 str_pointer(ss, (void *const *)&kpatch->repladdr));
117 void show_ksplice_patches(struct supersect *kpatch_ss)
119 printf("KSPLICE PATCHES:\n\n");
120 const struct ksplice_patch *kpatch;
121 for (kpatch = kpatch_ss->contents.data; (void *)kpatch <
122 kpatch_ss->contents.data + kpatch_ss->contents.size; kpatch++)
123 show_ksplice_patch(kpatch_ss, kpatch);
124 printf("\n");
127 void show_ksplice_export(struct supersect *ss,
128 const struct ksplice_export *export)
130 printf("name: %s\n"
131 "newname: %s\n"
132 "type: %s\n"
133 "\n",
134 read_string(ss, &export->name),
135 read_string(ss, &export->new_name),
136 read_string(ss, &export->type));
139 void show_ksplice_exports(struct supersect *export_ss)
141 printf("KSPLICE EXPORTS:\n\n");
142 const struct ksplice_export *export;
143 for (export = export_ss->contents.data; (void *)export <
144 export_ss->contents.data + export_ss->contents.size; export++)
145 show_ksplice_export(export_ss, export);
146 printf("\n");
149 int main(int argc, char *argv[])
151 bfd *ibfd;
153 assert(argc >= 1);
154 bfd_init();
155 ibfd = bfd_openr(argv[1], NULL);
156 assert(ibfd);
158 char **matching;
159 assert(bfd_check_format_matches(ibfd, bfd_object, &matching));
161 struct superbfd *sbfd = fetch_superbfd(ibfd);
163 asection *kreloc_init_sect =
164 bfd_get_section_by_name(ibfd, ".ksplice_init_relocs");
165 if (kreloc_init_sect != NULL) {
166 struct supersect *kreloc_init_ss =
167 fetch_supersect(sbfd, kreloc_init_sect);
168 show_ksplice_relocs(kreloc_init_ss);
169 } else {
170 printf("No ksplice init relocations.\n\n");
173 asection *kreloc_sect = bfd_get_section_by_name(ibfd,
174 ".ksplice_relocs");
175 if (kreloc_sect != NULL) {
176 struct supersect *kreloc_ss =
177 fetch_supersect(sbfd, kreloc_sect);
178 show_ksplice_relocs(kreloc_ss);
179 } else {
180 printf("No ksplice relocations.\n\n");
183 asection *ksize_sect = bfd_get_section_by_name(ibfd, ".ksplice_sizes");
184 if (ksize_sect != NULL) {
185 struct supersect *ksize_ss = fetch_supersect(sbfd, ksize_sect);
186 show_ksplice_sizes(ksize_ss);
187 } else {
188 printf("No ksplice sizes.\n\n");
191 asection *kpatch_sect = bfd_get_section_by_name(ibfd,
192 ".ksplice_patches");
193 if (kpatch_sect != NULL) {
194 struct supersect *kpatch_ss =
195 fetch_supersect(sbfd, kpatch_sect);
196 show_ksplice_patches(kpatch_ss);
197 } else {
198 printf("No ksplice patches.\n\n");
201 asection *export_sect = bfd_get_section_by_name(ibfd,
202 ".ksplice_exports");
203 if (export_sect != NULL) {
204 struct supersect *export_ss =
205 fetch_supersect(sbfd, export_sect);
206 show_ksplice_exports(export_ss);
207 } else {
208 printf("No ksplice exports.\n\n");
211 return 0;