Debian Codegraph @

src/bin/buildopstab/buildopscounttab.c

raw
1 /*
2 Copyright (c) 2020, David Anderson
3 All rights reserved.
4
5 This software file is hereby placed in the public domain.
6 For use by anyone for any purpose.
7 */
8
9 /* This uses this condensed table to make
10 a simple fast-access C table.
11 Reads dwarf.h to be sure the fast-access table
12 has all the named DW_OP in dwarf.h present.
13 Build and run with
14 make rebuild
15 */
16
17 #include <config.h>
18
19 #include <stdio.h> /* FILE fclose() fopen() fprintf() printf() */
20 #include <stdlib.h> /* exit() getenv() */
21 #include <string.h> /* strcmp() strcpy() strncmp() strlen() */
22
23 #include "dwarf.h"
24 #include "libdwarf.h"
25 #include "libdwarf_private.h"
26 #include "dd_opscounttab.h"
27
28 #define MAXDEFINELINE 1000
29 static char *input_name = 0;
30 static char pathbuf[BUFSIZ];
31 static char buffer[BUFSIZ];
32
33 struct ops_table_s {
34 unsigned char ot_first;
35 unsigned char ot_last;
36 signed char ot_opcount;
37 signed char ot_stackchange;
38 };
39
40 /* the ops are 8 bits max */
41 #define OPS_USED_SIZE 256
42 #define OPS_USED_DUPS 20
43 static int ops_used_table[OPS_USED_SIZE];
44 struct dups_tab_s {
45 int dt_val;
46 char dt_name1[100];
47 } dups[OPS_USED_DUPS];
48 int dups_used = 0;
49
50 static struct ops_table_s optabsource[]= {
51 {DW_OP_addr , 0 , 1, 1 },
52 {DW_OP_deref , 0 , 0, 0 },
53 {DW_OP_const1u, DW_OP_consts , 1, 1},
54 {DW_OP_dup, DW_OP_over , 0, 1},
55 {DW_OP_pick, 0 , 1, 1},
56 {DW_OP_swap, 0 , 0, 0},
57 {DW_OP_rot, 0 , 0, 0},
58 {DW_OP_xderef, 0 , 0, -1},
59 {DW_OP_abs, 0 , 0, 0},
60 {DW_OP_and, DW_OP_mul , 0, -1},
61 {DW_OP_neg, DW_OP_not , 0, 0},
62 {DW_OP_or, DW_OP_plus , 0, -1},
63 {DW_OP_plus_uconst, 0 , 1, 0},
64
65 {DW_OP_shl, DW_OP_xor , 0, -1},
66 {DW_OP_bra , 0 , 1, -1},
67 {DW_OP_eq, DW_OP_ne , 0, -1},
68 {DW_OP_skip, 0 , 1, 0},
69 {DW_OP_lit0 , DW_OP_lit31 , 0, 1},
70 {DW_OP_reg0 , DW_OP_reg31 , 0, 0},
71 {DW_OP_breg0 , DW_OP_breg31 , 1, 1},
72 {DW_OP_regx , DW_OP_fbreg , 1, 0},
73 {DW_OP_bregx, 0 , 2, 1},
74 {DW_OP_piece, 0 , 1, 0},
75 {DW_OP_deref_size, 0 , 1, 0},
76 {DW_OP_xderef_size, 0 , 1, -1},
77 {DW_OP_nop, 0 , 0, 0},
78 {DW_OP_push_object_address ,0 , 0, 1},
79 /* The called expr may change the stack,
80 but not the call itself*/
81 {DW_OP_call2, DW_OP_call_ref , 1, 0},
82
83 {DW_OP_form_tls_address,0 , 0, 0},
84 {DW_OP_call_frame_cfa ,0 , 0, 1},
85 {DW_OP_bit_piece, 0 , 2, 0},
86 {DW_OP_implicit_value, 0 , 2, 0},
87 {DW_OP_stack_value, 0 , 0, 0},
88 {DW_OP_implicit_pointer,0 , 2, 0},
89 {DW_OP_addrx, DW_OP_constx , 1, 1},
90 {DW_OP_entry_value, 0, 2, 1},
91 {DW_OP_const_type, 0, 3, 1},
92 {DW_OP_regval_type, DW_OP_deref_type , 2, 1},
93 {DW_OP_xderef_type, 0, 0, -1},
94 {DW_OP_convert /*0xa8*/, 0, 1, 0},
95 {DW_OP_reinterpret /* 0xa9*/, 0, 1, 0},
96 {DW_OP_GNU_push_tls_address /*0xe0*/, 0, 0, 1},
97 {DW_OP_HP_unknown /* 0xe0*/, 0, 0, 0},
98 {DW_OP_LLVM_form_aspace_address /* 0xe1*/, 0, 0, 1},
99 {DW_OP_HP_is_value /* 0xe1*/, 0, 1, 1},
100
101 {DW_OP_LLVM_push_lane /* 0xe2*/, 0, 0, 1},
102 {DW_OP_HP_fltconst4 /* 0xe2*/ , 0, 1, 1},
103
104 {DW_OP_LLVM_offset /* 0xe3*/ , 0, 0, 0},
105 {DW_OP_HP_fltconst8 /* 0xe3*/ , 0, 1, 0},
106
107 {DW_OP_LLVM_offset_uconst /* 0xe4*/, 0, 1, 0},
108 {DW_OP_HP_mod_range /* 0xe4*/, 0, 2, 0},
109
110 {DW_OP_LLVM_bit_offset /* 0xe5*/, 0, 0, 0},
111 {DW_OP_HP_unmod_range /* 0xe5*/, 0, 2, 0},
112
113 {DW_OP_LLVM_call_frame_entry_reg /* 0xe6*/, 0, 1, 0},
114 {DW_OP_HP_tls /* 0xe6*/, 0, 0, 0},
115
116 {DW_OP_LLVM_undefined /* 0xe7*/, 0, 0, 0},
117 {DW_OP_LLVM_aspace_bregx /* 0xe8*/, 0, 2, 0},
118 {DW_OP_INTEL_bit_piece /* 0xe8*/, 0, 2, 0},
119 {DW_OP_LLVM_aspace_implicit_pointer /* 0xe9*/, 0, 2, 0},
120 {DW_OP_LLVM_piece_end /* 0xea*/, 0, 0, 0},
121 {DW_OP_LLVM_extend /* 0xeb*/, 0, 2, 0},
122
123 {DW_OP_LLVM_select_bit_piece /* 0xec*/, 0, 2, 0},
124 {DW_OP_WASM_location /* 0xed*/, 0, 1, 1},
125 {DW_OP_WASM_location_int /* 0xee*/, 0, 1, 1},
126
127 {DW_OP_GNU_uninit /* 0xf0*/, 0, 0, 0},/*unknown opcount*/
128 {DW_OP_APPLE_uninit /* 0xf0*/, 0, 1, 0},
129 {DW_OP_GNU_encoded_addr /*0xf1*/, 0, 1, 1},/*1 op is correct*/
130 {DW_OP_GNU_implicit_pointer /*0xf2*/, 0, 1, 1},/*1 op is correct*/
131 {DW_OP_GNU_entry_value /*0xf3*/, 0, 2, 1},/*2 op is correct*/
132 {DW_OP_GNU_const_type /*0xf4 */, 0, 3,1},/*3 is correct*/
133 {DW_OP_GNU_regval_type /* 0xf5*/, 0, 2,1},/*2 is correct*/
134 {DW_OP_GNU_deref_type /*0xf6*/, 0, 2,1},/*2 is correct*/
135 {DW_OP_GNU_convert /*0xf7*/, 0, 1,0},/*1 is correct*/
136 {DW_OP_PGI_omp_thread_num /*0xf8*/, 0, 0,1},/*just pushes*/
137 {DW_OP_GNU_reinterpret/* 0xf9*/, 0, 1,0},/*1 is correct*/
138 {DW_OP_GNU_parameter_ref/* 0xfa*/, 0, 1,1},/*1 is correct*/
139 {DW_OP_GNU_addr_index/* 0xfb*/, 0, 1,1},/*1 is correct.Fission*/
140 {DW_OP_GNU_const_index/* 0xfc*/, 0, 1,1},/*1 is correct.Fission*/
141 {DW_OP_GNU_variable_value/* 0xfd*/, 0, 1,1},/* GNU 2017*/
142 {0,0,0,0}
143 };
144
145 static void
146 safe_strcpy(char *targ,char *src,unsigned targlen, unsigned srclen)
147 {
148 if (srclen > targlen) {
149 printf("Target name does not fit in buffer.\n"
150 "In buildopstabcount.c increase buffer size "
151 " from %u \n",(unsigned int)sizeof(buffer));
152 exit(EXIT_FAILURE);
153 }
154 strcpy(targ,src);
155 }
156
157 /* Issue error message and exit there is a mismatch,
158 this should never fail. */
159 static void
160 validate_name(char *name,unsigned long v,unsigned int linenum)
161 {
162 int res = 0;
163 const char *s = 0;
164 int count = 0;
165 if (v >= OPS_USED_SIZE) {
166 printf("FAIL! Opcode 0x%lx too large. Impossible\n", v);
167 exit(EXIT_FAILURE);
168
169 }
170 ops_used_table[v]++;
171 count = ops_used_table[v];
172 res = dwarf_get_OP_name(v,&s);
173 if (res != DW_DLV_OK) {
174 printf("Fail dwarf.h line %u value %lu as no OP name! %s\n",
175 linenum,v,name);
176 exit(EXIT_FAILURE);
177 }
178 if ( count > 1) {
179 fprintf(stderr,"Op 0x%lx used %d times. %s and now %s\n",
180 v,count,s, name);
181 if (dups_used >= OPS_USED_DUPS) {
182 printf("Too many dups, increase table size\n");
183 exit(EXIT_FAILURE);
184 }
185 dups[dups_used].dt_val = v;
186 safe_strcpy(dups[dups_used].dt_name1,name,
187 sizeof(dups[dups_used].dt_name1),strlen(name));
188 dups_used++;
189 return;
190 }
191 if (strcmp(name,s)) {
192 printf("Fail dwarf.h line %u value %lu as OP "
193 "name mismatch! %s\n",
194 linenum,v,name);
195 exit(EXIT_FAILURE);
196 }
197 }
198
199 /* This is N*M overall but the numbers are small, so it's
200 unimportant. */
201 static void
202 validate_op_listed(char *curdefname,unsigned long v,
203 unsigned int linenum)
204 {
205 struct ops_table_s * ops = optabsource ;
206
207 for ( ; ; ++ops) {
208 unsigned int j = 0;
209 if (!ops->ot_first &&
210 !ops->ot_last &&
211 !ops->ot_opcount) {
212 break;
213 }
214 if (v == ops->ot_first) {
215 validate_name(curdefname,v,linenum);
216 return;
217 }
218 if (ops->ot_last && v == ops->ot_last) {
219 validate_name(curdefname,v,linenum);
220 return;
221 }
222 for ( j = ops->ot_first; j < ops->ot_last; ++j) {
223 if (v == j) {
224 validate_name(curdefname,v,linenum);
225 return;
226 }
227 }
228 }
229 printf("Failed to find %s val 0x%lu at dwarf.h "
230 "line %u in optabsource\n",
231 curdefname,v, linenum);
232 exit(EXIT_FAILURE);
233 }
234
235 static void
236 check_if_optabsource_complete(char *path)
237 {
238 FILE *fin = 0;
239 unsigned int linenum = 0;
240 int loop_done = FALSE;
241 const char *oldop = "#define DW_OP_";
242 int oldoplen = strlen(oldop);
243
244 fin = fopen(path,"r");
245 if (!fin) {
246 printf("Unable to open dwarf.h to read %s\n",path);
247 exit(EXIT_FAILURE);
248 }
249 for ( ;!loop_done;++linenum) {
250 char *line = 0;
251 char * pastname = 0;
252 unsigned int linelen = 0;
253 char *curdefname = 0;
254 unsigned int curdefnamelen = 0;
255 char *endptr = 0;
256 char *numstart = 0;
257 unsigned long v = 0;
258
259 line = fgets(buffer,MAXDEFINELINE,fin);
260 if (!line) {
261 break;
262 }
263 linelen = strlen(line);
264 line[linelen-1] = 0;
265 --linelen;
266 if (linelen >= (unsigned)(MAXDEFINELINE-1)) {
267 printf("define line %u is too long!\n",linenum);
268 exit(EXIT_FAILURE);
269 }
270 if (strncmp(line,oldop,oldoplen)) {
271 /* Not ours. */
272 continue;
273 }
274 /* ASSERT: line ends with NUL byte. */
275 curdefname = line+8;
276 for ( ; ; curdefnamelen++) {
277 pastname = curdefname +curdefnamelen;
278 if (!*pastname) {
279 /* At end of line. Missing value. */
280 printf("define line %u of %s: has no number value!\n",
281 linenum,path);
282 exit(EXIT_FAILURE);
283 }
284 if (*pastname == ' ') {
285 /* Ok. Now look for value. */
286 numstart = pastname + 1;
287 break;
288 }
289 }
290 *pastname = 0;
291 if (!strcmp(curdefname,"DW_OP_lo_user")) {
292 /* This is special, we ignore it. */
293 continue;
294 }
295 if (!strcmp(curdefname,"DW_OP_hi_user")) {
296 /* This is special, we ignore it. */
297 continue;
298 }
299 /* Skip spaces. */
300 for ( ; *numstart == ' '; ++numstart) { }
301 endptr = 0;
302 v = strtoul(numstart,&endptr,0);
303 if (v > 0xff) {
304 printf("define line %u: DW_OP number value "
305 "unreasonable. %lu\n",
306 linenum,v);
307 exit(EXIT_FAILURE);
308 }
309 if (v == 0 && endptr == numstart) {
310 printf("define line %u of %s: number value missing.\n",
311 linenum,path);
312 printf("Leaving a space as in #define A B 3"
313 " in dwarf.h.in will cause this.\n");
314 exit(EXIT_FAILURE);
315 }
316 if (*endptr != ' ' && *endptr != 0) {
317 unsigned char e = *endptr;
318 printf("define line %u: number value terminates oddly "
319 "char: %u 0x%x line %s\n",
320 linenum,e,e,line);
321 exit(EXIT_FAILURE);
322 }
323 if (!v) {
324 printf("define line %u: DW_OP number value "
325 "zero unreasonable.\n",
326 linenum);
327 exit(EXIT_FAILURE);
328 }
329 validate_op_listed(curdefname,v,linenum);
330 }
331 fclose(fin);
332 }
333
334 static int
335 havedup(int j, char **out)
336 {
337 int i = 0;
338 for (; i < OPS_USED_DUPS; ++i) {
339 if (dups[i].dt_val == j) {
340 *out = dups[i].dt_name1;
341 return 1;
342 }
343 }
344 return 0;
345 }
346
347 int main(int argc, char**argv)
348 {
349 struct ops_table_s *op = 0;
350 const char *tailpath = "/src/lib/libdwarf/dwarf.h";
351 char *path = 0;
352 int inindex = 0;
353 int outindex = 0;
354 int f = 0;
355 int l = 0;
356 int c = 0;
357 int res = 0;
358 int lastop = 0;
359 unsigned len = 0;
360
361 if (argc > 1) {
362 if (argc != 3) {
363 printf("Expected -f <filename> of base code path\n");
364 exit(EXIT_FAILURE);
365 }
366 if (strcmp(argv[1],"-f")) {
367 printf("Expected -f\n");
368 exit(EXIT_FAILURE);
369 }
370 path=argv[2];
371 } else {
372 /* env var should be set with base path of code */
373 path = getenv("DWTOPSRCDIR");
374 if (!path) {
375 printf("Expected environment variable "
376 "DWTOPSRCDIR with path of "
377 "base directory (usually called 'code')\n");
378 exit(EXIT_FAILURE);
379 }
380 }
381 len = strlen(path);
382 if (len >= sizeof(pathbuf)) {
383 printf(" buildopstab Input path greater length "
384 "than makes any sense:"
385 " Giving up\n");
386 exit(EXIT_FAILURE);
387
388 }
389 safe_strcpy(pathbuf,path,sizeof(pathbuf),len);
390 {
391 size_t remaining = sizeof(pathbuf) -len -1;
392 size_t tailpathlen = strlen(tailpath);
393 if (tailpathlen >= remaining) {
394 printf(" buildopstab Input tailpath greater "
395 "length fits in buf: "
396 "Giving up\n");
397 exit(EXIT_FAILURE);
398 }
399 /* Notice tailpath has a leading / */
400 safe_strcpy(pathbuf+len,(char *)tailpath,
401 remaining,tailpathlen);
402 }
403 input_name = pathbuf;
404
405 check_if_optabsource_complete(input_name);
406
407 printf("/* Generated expression ops table, "
408 "do not edit. */\n");
409 printf("#include \"dd_opscounttab.h\"\n");
410 printf("\n");
411 printf("struct dwarf_opscounttab_s _dwarf_opscounttab[] = {\n");
412 for ( ; ; ++inindex) {
413 const char *opn = 0;
414 int sc = 0;
415
416 op = &optabsource[inindex];
417 f = op->ot_first;
418 if (!f) {
419 break;
420 }
421 if (lastop && f < lastop) {
422 printf("FAILED buildopscounttab on OP,out of sequence"
423 " f=0x%x lastop=0x%x\n",
424 (unsigned)f,(unsigned)lastop);
425 exit(EXIT_FAILURE);
426 }
427 if (f == lastop) {
428 /* A duplicate, ignore here. */
429 continue;
430 }
431 l = op->ot_last;
432 c = op->ot_opcount;
433 sc = op->ot_stackchange;
434
435 while (f > outindex) {
436 printf("{/* %-26s 0x%02x*/ %d, 0},\n","unused",
437 outindex,-1);
438 ++outindex;
439 }
440 if (!l) {
441 res = dwarf_get_OP_name(f,&opn);
442 if (res != DW_DLV_OK) {
443 printf("FAILED buildopscounttab on OP 0x%x\n",
444 f);
445 exit(EXIT_FAILURE);
446 }
447 lastop = f;
448
449 printf("{/* %-26s 0x%02x*/ %d, %d},\n",opn,f,c,sc);
450 {
451 char *dup = 0;
452 if (havedup(f,&dup)) {
453 printf(" /* above has alt spelling %s */",
454 dup);
455 printf("\n");
456 }
457 }
458 ++outindex;
459 } else {
460 int j = f;
461 for ( ; j <= l; ++j) {
462 res = dwarf_get_OP_name(j,&opn);
463 if (res != DW_DLV_OK) {
464 printf("FAILED buildopscounttab on OP 0x%x\n",
465 f);
466 exit(EXIT_FAILURE);
467 }
468 printf("{/* %-26s 0x%2x*/ %d, %d}",opn,j,c,sc);
469 printf(",\n");
470 {
471 /* Should NOT happen, dups should
472 be singleton entries */
473 char *dup = 0;
474 if (havedup(j,&dup)) {
475 printf(" /* above has alt spelling %s */",
476 dup);
477 printf("\n");
478 fprintf(stderr," FAIL an entry "
479 " in a list has dup. Fix table");
480 exit(EXIT_FAILURE);
481 }
482 }
483 ++outindex;
484 lastop = j;
485 }
486 }
487 }
488 while (outindex < DWOPS_ARRAY_SIZE) {
489 printf("{/* %-26s 0x%02x*/ %d, 0},\n","unused",outindex,-1);
490 ++outindex;
491 }
492 printf("};\n");
493 return 0;
494 }