comparison leo-obj/tool/richsym.c @ 145:25d3ead621f8

tiobjd: ctypes command implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 29 Apr 2014 04:49:17 +0000
parents fd772de226cb
children 70631c246df0
comparison
equal deleted inserted replaced
144:fd772de226cb 145:25d3ead621f8
12 #include "coffconst.h" 12 #include "coffconst.h"
13 #include "globals.h" 13 #include "globals.h"
14 14
15 extern unsigned get_u16(), get_u32(); 15 extern unsigned get_u16(), get_u32();
16 extern char *storage_class_to_string(); 16 extern char *storage_class_to_string();
17
18 int richsym_print_bitsize;
17 19
18 static int 20 static int
19 try_typedef_hack(struct_sym) 21 try_typedef_hack(struct_sym)
20 struct internal_syment *struct_sym; 22 struct internal_syment *struct_sym;
21 { 23 {
180 return("u_long"); 182 return("u_long");
181 default: 183 default:
182 return("__unknown_base_type"); 184 return("__unknown_base_type");
183 } 185 }
184 } 186 }
187
188 richsym_print_in_c(prefix, sym, is_typedef)
189 char *prefix;
190 struct internal_syment *sym;
191 {
192 char *base_type, *s1, *s2;
193 int dertype, last_ptr, narray;
194
195 base_type = get_base_type_of_syment(sym, is_typedef);
196 dertype = sym->type >> 4;
197 s1 = malloc(strlen(sym->name));
198 if (!s1) {
199 perror("malloc");
200 exit(1);
201 }
202 strcpy(s1, sym->name + 1);
203 last_ptr = 0;
204 narray = 0;
205 for (; dertype; dertype >>= 2) {
206 switch (dertype & 3) {
207 case DT_NON:
208 fprintf(stderr,
209 "error: symbol #%u: DT_NON followed by more derived types\n",
210 sym->number);
211 exit(1);
212 case DT_PTR:
213 s2 = malloc(strlen(s1) + 2);
214 if (!s2) {
215 perror("malloc");
216 exit(1);
217 }
218 sprintf(s2, "*%s", s1);
219 free(s1);
220 s1 = s2;
221 last_ptr = 1;
222 continue;
223 }
224 if (last_ptr) {
225 s2 = malloc(strlen(s1) + 3);
226 if (!s2) {
227 perror("malloc");
228 exit(1);
229 }
230 sprintf(s2, "(%s)", s1);
231 free(s1);
232 s1 = s2;
233 }
234 switch (dertype & 3) {
235 case DT_FCN:
236 s2 = malloc(strlen(s1) + 3);
237 if (!s2) {
238 perror("malloc");
239 exit(1);
240 }
241 sprintf(s2, "%s()", s1);
242 free(s1);
243 s1 = s2;
244 break;
245 case DT_ARY:
246 if (narray >= 4) {
247 fprintf(stderr,
248 "error: symbol #%u: too many [] types\n",
249 sym->number);
250 exit(1);
251 }
252 s2 = malloc(strlen(s1) + 8);
253 if (!s2) {
254 perror("malloc");
255 exit(1);
256 }
257 sprintf(s2, "%s[%u]", s1,
258 get_u16(sym->aux + 8 + narray * 2));
259 free(s1);
260 s1 = s2;
261 narray++;
262 break;
263 default:
264 fprintf(stderr,
265 "BUG in richsym_print_in_c(): bad derived type\n");
266 exit(1);
267 }
268 last_ptr = 0;
269 }
270 printf("%s%s %s;", prefix, base_type, s1);
271 free(s1);
272 if (richsym_print_bitsize)
273 printf("\t/* %u bits */", get_u32(sym->aux + 4));
274 putchar('\n');
275 }
276
277 cmd_ctypes(argc, argv)
278 char **argv;
279 {
280 int c;
281 unsigned n;
282 struct internal_syment *sym;
283
284 while ((c = getopt(argc, argv, "b")) != EOF)
285 switch (c) {
286 case 'b':
287 richsym_print_bitsize++;
288 continue;
289 default:
290 /* error msg already printed */
291 exit(1);
292 }
293
294 get_int_section_table();
295 get_int_symbol_table();
296 richsym_initial_preen();
297 for (n = 0; n < nsymtab; n++) {
298 sym = symtab[n];
299 if (!sym)
300 continue;
301 switch (sym->class) {
302 case C_FILE:
303 printf("/* from %s */\n", sym->name);
304 continue;
305 case C_STRTAG:
306 case C_UNTAG:
307 case C_ENTAG:
308 printf("%s {", sym->struct_name_raw);
309 if (richsym_print_bitsize && sym->aux)
310 printf("\t/* %u bits */", get_u32(sym->aux+4));
311 putchar('\n');
312 continue;
313 case C_EOS:
314 fputs("};", stdout);
315 if (richsym_print_bitsize && sym->aux)
316 printf("\t/* %u bits */", get_u32(sym->aux+4));
317 putchar('\n');
318 continue;
319 case C_MOS:
320 case C_MOU:
321 case C_MOE:
322 case C_TPDEF:
323 break;
324 default:
325 continue;
326 }
327 if (sym->name[0] != '_') {
328 printf(
329 "/* symbol #%u of class %s has no leading underscore */\n",
330 sym->number,
331 storage_class_to_string(sym->class, 0));
332 continue;
333 }
334 if (!sym->aux && sym->class != C_MOE) {
335 printf(
336 "/* symbol #%u of class %s has no aux record */\n",
337 sym->number,
338 storage_class_to_string(sym->class, 0));
339 continue;
340 }
341 switch (sym->class) {
342 case C_MOS:
343 case C_MOU:
344 if (sym->scnum != -1)
345 printf("\t/* MOS/MOU section != ABS! */\n");
346 else if (richsym_print_bitsize >= 2)
347 printf("\t/* offset: %u bits\n", sym->value);
348 richsym_print_in_c("\t", sym, 0);
349 continue;
350 case C_MOE:
351 if (sym->scnum != -1) {
352 printf("\t/* MOE section != ABS! */\n");
353 continue;
354 }
355 printf("\t%s = %u;", sym->name + 1, sym->value);
356 if (sym->value >= 10)
357 printf("\t/* 0x%x */", sym->value);
358 putchar('\n');
359 continue;
360 case C_TPDEF:
361 richsym_print_in_c("typedef ", sym,
362 !(sym->type & 0xFFF0));
363 continue;
364 }
365 }
366 exit(0);
367 }