| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: dbsym.c,v 1.3 2012/03/19 09:14:15 wiz Exp $ */ | | 1 | /* $NetBSD: dbsym.c,v 1.4 2014/08/17 19:12:59 joerg Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2001 Simon Burge (for Wasabi Systems) | | 4 | * Copyright (c) 2001 Simon Burge (for Wasabi Systems) |
5 | * Copyright (c) 1996 Christopher G. Demetriou | | 5 | * Copyright (c) 1996 Christopher G. Demetriou |
6 | * All rights reserved. | | 6 | * All rights reserved. |
7 | * | | 7 | * |
8 | * Redistribution and use in source and binary forms, with or without | | 8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | | 9 | * modification, are permitted provided that the following conditions |
10 | * are met: | | 10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright | | 11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | | 12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright | | 13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | | 14 | * notice, this list of conditions and the following disclaimer in the |
| @@ -29,27 +29,27 @@ | | | @@ -29,27 +29,27 @@ |
29 | * | | 29 | * |
30 | * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>> | | 30 | * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>> |
31 | */ | | 31 | */ |
32 | | | 32 | |
33 | #if HAVE_NBTOOL_CONFIG_H | | 33 | #if HAVE_NBTOOL_CONFIG_H |
34 | #include "nbtool_config.h" | | 34 | #include "nbtool_config.h" |
35 | #endif | | 35 | #endif |
36 | | | 36 | |
37 | #include <sys/cdefs.h> | | 37 | #include <sys/cdefs.h> |
38 | #if !defined(lint) | | 38 | #if !defined(lint) |
39 | __COPYRIGHT("@(#) Copyright (c) 1996 Christopher G. Demetriou.\ | | 39 | __COPYRIGHT("@(#) Copyright (c) 1996 Christopher G. Demetriou.\ |
40 | Copyright 2001 Simon Burge.\ | | 40 | Copyright 2001 Simon Burge.\ |
41 | All rights reserved."); | | 41 | All rights reserved."); |
42 | __RCSID("$NetBSD: dbsym.c,v 1.3 2012/03/19 09:14:15 wiz Exp $"); | | 42 | __RCSID("$NetBSD: dbsym.c,v 1.4 2014/08/17 19:12:59 joerg Exp $"); |
43 | #endif /* not lint */ | | 43 | #endif /* not lint */ |
44 | | | 44 | |
45 | #include <sys/param.h> | | 45 | #include <sys/param.h> |
46 | #include <sys/mman.h> | | 46 | #include <sys/mman.h> |
47 | #include <sys/stat.h> | | 47 | #include <sys/stat.h> |
48 | | | 48 | |
49 | #include <bfd.h> | | 49 | #include <bfd.h> |
50 | #include <err.h> | | 50 | #include <err.h> |
51 | #include <fcntl.h> | | 51 | #include <fcntl.h> |
52 | #include <stdio.h> | | 52 | #include <stdio.h> |
53 | #include <stdlib.h> | | 53 | #include <stdlib.h> |
54 | #include <string.h> | | 54 | #include <string.h> |
55 | #include <unistd.h> | | 55 | #include <unistd.h> |
| @@ -66,52 +66,56 @@ struct symbols { | | | @@ -66,52 +66,56 @@ struct symbols { |
66 | { "_db_symtab", 0 }, | | 66 | { "_db_symtab", 0 }, |
67 | #define X_DB_SYMTABSIZE 1 | | 67 | #define X_DB_SYMTABSIZE 1 |
68 | { "_db_symtabsize", 0 }, | | 68 | { "_db_symtabsize", 0 }, |
69 | { NULL, 0 } | | 69 | { NULL, 0 } |
70 | }; | | 70 | }; |
71 | | | 71 | |
72 | int main(int, char **); | | 72 | int main(int, char **); |
73 | void usage(void) __attribute__((noreturn)); | | 73 | void usage(void) __attribute__((noreturn)); |
74 | int find_symtab(bfd *, struct symbols *); | | 74 | int find_symtab(bfd *, struct symbols *); |
75 | int load_symtab(bfd *, int fd, char **, u_int32_t *); | | 75 | int load_symtab(bfd *, int fd, char **, u_int32_t *); |
76 | | | 76 | |
77 | int verbose; | | 77 | int verbose; |
78 | int printsize; | | 78 | int printsize; |
| | | 79 | int printsize2; |
79 | | | 80 | |
80 | int | | 81 | int |
81 | main(int argc, char **argv) | | 82 | main(int argc, char **argv) |
82 | { | | 83 | { |
83 | int ch, kfd; | | 84 | int ch, kfd; |
84 | struct stat ksb; | | 85 | struct stat ksb; |
85 | size_t symtab_offset; | | 86 | size_t symtab_offset; |
86 | u_int32_t symtab_space, symtabsize; | | 87 | u_int32_t symtab_space, symtabsize; |
87 | const char *kfile; | | 88 | const char *kfile; |
88 | char *bfdname, *mappedkfile, *symtab; | | 89 | char *bfdname, *mappedkfile, *symtab; |
89 | bfd *abfd; | | 90 | bfd *abfd; |
90 | | | 91 | |
91 | setprogname(argv[0]); | | 92 | setprogname(argv[0]); |
92 | | | 93 | |
93 | bfdname = NULL; | | 94 | bfdname = NULL; |
94 | while ((ch = getopt(argc, argv, "b:pv")) != -1) | | 95 | while ((ch = getopt(argc, argv, "b:Ppv")) != -1) |
95 | switch (ch) { | | 96 | switch (ch) { |
96 | case 'b': | | 97 | case 'b': |
97 | bfdname = optarg; | | 98 | bfdname = optarg; |
98 | break; | | 99 | break; |
99 | case 'v': | | 100 | case 'v': |
100 | verbose = 1; | | 101 | verbose = 1; |
101 | break; | | 102 | break; |
102 | case 'p': | | 103 | case 'p': |
103 | printsize = 1; | | 104 | printsize = 1; |
104 | break; | | 105 | break; |
| | | 106 | case 'P': |
| | | 107 | printsize2 = 1; |
| | | 108 | break; |
105 | case '?': | | 109 | case '?': |
106 | default: | | 110 | default: |
107 | usage(); | | 111 | usage(); |
108 | } | | 112 | } |
109 | argc -= optind; | | 113 | argc -= optind; |
110 | argv += optind; | | 114 | argv += optind; |
111 | | | 115 | |
112 | if (argc != 1) | | 116 | if (argc != 1) |
113 | usage(); | | 117 | usage(); |
114 | kfile = argv[0]; | | 118 | kfile = argv[0]; |
115 | | | 119 | |
116 | if ((kfd = open(kfile, O_RDWR, 0)) == -1) | | 120 | if ((kfd = open(kfile, O_RDWR, 0)) == -1) |
117 | err(1, "open %s", kfile); | | 121 | err(1, "open %s", kfile); |
| @@ -148,26 +152,30 @@ main(int argc, char **argv) | | | @@ -148,26 +152,30 @@ main(int argc, char **argv) |
148 | MAP_FILE | MAP_SHARED, kfd, 0)) == (caddr_t)-1) | | 152 | MAP_FILE | MAP_SHARED, kfd, 0)) == (caddr_t)-1) |
149 | err(1, "mmap %s", kfile); | | 153 | err(1, "mmap %s", kfile); |
150 | if (verbose) | | 154 | if (verbose) |
151 | fprintf(stderr, "mapped %s\n", kfile); | | 155 | fprintf(stderr, "mapped %s\n", kfile); |
152 | | | 156 | |
153 | symtab_offset = db_symtab_symbols[X_DB_SYMTAB].offset; | | 157 | symtab_offset = db_symtab_symbols[X_DB_SYMTAB].offset; |
154 | symtab_space = bfd_get_32(abfd, | | 158 | symtab_space = bfd_get_32(abfd, |
155 | &mappedkfile[db_symtab_symbols[X_DB_SYMTABSIZE].offset]); | | 159 | &mappedkfile[db_symtab_symbols[X_DB_SYMTABSIZE].offset]); |
156 | | | 160 | |
157 | if (printsize) { | | 161 | if (printsize) { |
158 | printf("%d %d\n", symtabsize, symtab_space); | | 162 | printf("%d %d\n", symtabsize, symtab_space); |
159 | goto done; | | 163 | goto done; |
160 | } | | 164 | } |
| | | 165 | if (printsize2) { |
| | | 166 | printf("%d\n", symtabsize); |
| | | 167 | goto done; |
| | | 168 | } |
161 | | | 169 | |
162 | if (symtabsize > symtab_space) | | 170 | if (symtabsize > symtab_space) |
163 | errx(1, "symbol table (%u bytes) too big for buffer (%u bytes)\n" | | 171 | errx(1, "symbol table (%u bytes) too big for buffer (%u bytes)\n" |
164 | "Increase options SYMTAB_SPACE in your kernel config", | | 172 | "Increase options SYMTAB_SPACE in your kernel config", |
165 | symtabsize, symtab_space); | | 173 | symtabsize, symtab_space); |
166 | | | 174 | |
167 | if (verbose) | | 175 | if (verbose) |
168 | fprintf(stderr, "symtab size %d, space available %d\n", | | 176 | fprintf(stderr, "symtab size %d, space available %d\n", |
169 | symtabsize, symtab_space); | | 177 | symtabsize, symtab_space); |
170 | | | 178 | |
171 | memcpy(mappedkfile + symtab_offset, symtab, symtabsize); | | 179 | memcpy(mappedkfile + symtab_offset, symtab, symtabsize); |
172 | | | 180 | |
173 | if (verbose) | | 181 | if (verbose) |
| @@ -184,27 +192,27 @@ done: | | | @@ -184,27 +192,27 @@ done: |
184 | if (verbose) | | 192 | if (verbose) |
185 | fprintf(stderr, "exiting\n"); | | 193 | fprintf(stderr, "exiting\n"); |
186 | | | 194 | |
187 | bfd_close_all_done(abfd); | | 195 | bfd_close_all_done(abfd); |
188 | exit(0); | | 196 | exit(0); |
189 | } | | 197 | } |
190 | | | 198 | |
191 | void | | 199 | void |
192 | usage(void) | | 200 | usage(void) |
193 | { | | 201 | { |
194 | const char **list; | | 202 | const char **list; |
195 | | | 203 | |
196 | fprintf(stderr, | | 204 | fprintf(stderr, |
197 | "usage: %s [-pv] [-b bfdname] kernel\n", | | 205 | "usage: %s [-Ppv] [-b bfdname] kernel\n", |
198 | getprogname()); | | 206 | getprogname()); |
199 | fprintf(stderr, "supported targets:"); | | 207 | fprintf(stderr, "supported targets:"); |
200 | for (list = bfd_target_list(); *list != NULL; list++) | | 208 | for (list = bfd_target_list(); *list != NULL; list++) |
201 | fprintf(stderr, " %s", *list); | | 209 | fprintf(stderr, " %s", *list); |
202 | fprintf(stderr, "\n"); | | 210 | fprintf(stderr, "\n"); |
203 | exit(1); | | 211 | exit(1); |
204 | } | | 212 | } |
205 | | | 213 | |
206 | int | | 214 | int |
207 | find_symtab(bfd *abfd, struct symbols *symbols) | | 215 | find_symtab(bfd *abfd, struct symbols *symbols) |
208 | { | | 216 | { |
209 | long i; | | 217 | long i; |
210 | long storage_needed; | | 218 | long storage_needed; |