tools/elf4rom/libs/dwarf-20071209/dwarfdump/tag_attr.c
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /* 
       
     2   Copyright (C) 2000,2004,2005 Silicon Graphics, Inc.  All Rights Reserved.
       
     3 
       
     4   This program is free software; you can redistribute it and/or modify it
       
     5   under the terms of version 2 of the GNU General Public License as
       
     6   published by the Free Software Foundation.
       
     7 
       
     8   This program is distributed in the hope that it would be useful, but
       
     9   WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
    11 
       
    12   Further, this software is distributed without any warranty that it is
       
    13   free of the rightful claim of any third person regarding infringement
       
    14   or the like.  Any license provided herein, whether implied or
       
    15   otherwise, applies only to this software file.  Patent licenses, if
       
    16   any, provided herein do not apply to combinations of this program with
       
    17   other software, or any other product whatsoever.
       
    18 
       
    19   You should have received a copy of the GNU General Public License along
       
    20   with this program; if not, write the Free Software Foundation, Inc., 51
       
    21   Franklin Street - Fifth Floor, Boston MA 02110-1301, USA.
       
    22 
       
    23   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
       
    24   Mountain View, CA 94043, or:
       
    25 
       
    26   http://www.sgi.com
       
    27 
       
    28   For further information regarding this notice, see:
       
    29 
       
    30   http://oss.sgi.com/projects/GenInfo/NoticeExplan
       
    31 
       
    32 
       
    33 
       
    34 $Header: /plroot/cmplrs.src/v7.4.5m/.RCS/PL/dwarfdump/RCS/tag_attr.c,v 1.8 2005/12/01 17:34:59 davea Exp $ */
       
    35 #include <dwarf.h>
       
    36 #include <stdio.h>
       
    37 #include <stdlib.h>		/* For exit() declaration etc. */
       
    38 #include <errno.h>		/* For errno declaration. */
       
    39 
       
    40 
       
    41 /*  The following is the magic token used to 
       
    42     distinguish real tags/attrs from group-delimiters. 
       
    43     Blank lines have been eliminated by an awk script.
       
    44 */
       
    45 #define MAGIC_TOKEN_VALUE 0xffffffff
       
    46 
       
    47 /* Expected input format 
       
    48 
       
    49 0xffffffff
       
    50 value of a tag
       
    51 value of a standard attribute that follows that tag
       
    52 ...
       
    53 0xffffffff
       
    54 value of a tag
       
    55 value of a standard attribute that follows that tag
       
    56 ...
       
    57 0xffffffff
       
    58 ...
       
    59 
       
    60 No blank lines or commentary allowed, no symbols, just numbers.
       
    61 
       
    62 
       
    63 */
       
    64 
       
    65 
       
    66 /* We don't need really long lines: the input file is simple. */
       
    67 #define MAX_LINE_SIZE 1000
       
    68 
       
    69 /* 1 more than the higest number in the DW_TAG defines. */
       
    70 #define TABLE_SIZE 0x41
       
    71 
       
    72 /* Enough entries to have a bit for each standard legal attr. */
       
    73 #define COLUMN_COUNT 4
       
    74 
       
    75 /* Bits per 'int' to mark legal attrs. */
       
    76 #define BITS_PER_WORD 32
       
    77 
       
    78 static unsigned int
       
    79     tag_attr_combination_table[TABLE_SIZE][COLUMN_COUNT];
       
    80 static char *tag_name[] = {
       
    81     "0x00",
       
    82     "0x01 DW_TAG_array_type",
       
    83     "0x02 DW_TAG_class_type",
       
    84     "0x03 DW_TAG_entry_point",
       
    85     "0x04 DW_TAG_enumeration_type",
       
    86     "0x05 DW_TAG_formal_parameter",
       
    87     "0x06",
       
    88     "0x07",
       
    89     "0x08 DW_TAG_imported_declaration",
       
    90     "0x09",
       
    91     "0x0a DW_TAG_label",
       
    92     "0x0b DW_TAG_lexical_block",
       
    93     "0x0c",
       
    94     "0x0d DW_TAG_member",
       
    95     "0x0e",
       
    96     "0x0f DW_TAG_pointer_type",
       
    97     "0x10 DW_TAG_reference_type",
       
    98     "0x11 DW_TAG_compile_unit",
       
    99     "0x12 DW_TAG_string_type",
       
   100     "0x13 DW_TAG_structure_type",
       
   101     "0x14",
       
   102     "0x15 DW_TAG_subroutine_type",
       
   103     "0x16 DW_TAG_typedef",
       
   104     "0x17 DW_TAG_union_type",
       
   105     "0x18 DW_TAG_unspecified_parameters",
       
   106     "0x19 DW_TAG_variant",
       
   107     "0x1a DW_TAG_common_block",
       
   108     "0x1b DW_TAG_common_inclusion",
       
   109     "0x1c DW_TAG_inheritance",
       
   110     "0x1d DW_TAG_inlined_subroutine",
       
   111     "0x1e DW_TAG_module",
       
   112     "0x1f DW_TAG_ptr_to_member_type",
       
   113     "0x20 DW_TAG_set_type",
       
   114     "0x21 DW_TAG_subrange_type",
       
   115     "0x22 DW_TAG_with_stmt",
       
   116     "0x23 DW_TAG_access_declaration",
       
   117     "0x24 DW_TAG_base_type",
       
   118     "0x25 DW_TAG_catch_block",
       
   119     "0x26 DW_TAG_const_type",
       
   120     "0x27 DW_TAG_constant",
       
   121     "0x28 DW_TAG_enumerator",
       
   122     "0x29 DW_TAG_file_type",
       
   123     "0x2a DW_TAG_friend",
       
   124     "0x2b DW_TAG_namelist",
       
   125     "0x2c DW_TAG_namelist_item",
       
   126     "0x2d DW_TAG_packed_type",
       
   127     "0x2e DW_TAG_subprogram",
       
   128     "0x2f DW_TAG_template_type_parameter",
       
   129     "0x30 DW_TAG_template_value_parameter",
       
   130     "0x31 DW_TAG_thrown_type",
       
   131     "0x32 DW_TAG_try_block",
       
   132     "0x33 DW_TAG_variant_part",
       
   133     "0x34 DW_TAG_variable",
       
   134     "0x35 DW_TAG_volatile_type",
       
   135     "0x36 DW_TAG_dwarf_procedure",
       
   136     "0x37 DW_TAG_restrict_type",
       
   137     "0x38 DW_TAG_interface_type",
       
   138     "0x39 DW_TAG_namespace",
       
   139     "0x3a DW_TAG_imported_module",
       
   140     "0x3b DW_TAG_unspecified_type",
       
   141     "0x3c DW_TAG_partial_unit",
       
   142     "0x3d DW_TAG_imported_unit",
       
   143     "0x3e",			/* was DW_TAG_mutable_type, removed
       
   144 				   from DWARF3f. */
       
   145     "0x3f DW_TAG_condition",
       
   146     "0x40 DW_TAG_shared_type",
       
   147 };
       
   148 
       
   149 static int linecount = 0;
       
   150 static char line_in[MAX_LINE_SIZE];
       
   151 
       
   152 #define IS_EOF 1
       
   153 #define NOT_EOF 0
       
   154 
       
   155 #define MAX_LINE_SIZE 1000
       
   156 
       
   157 
       
   158 static void
       
   159 bad_line_input(char *msg)
       
   160 {
       
   161     fprintf(stderr,
       
   162 	    "tag_attr table build failed %s, line %d: \"%s\"  \n",
       
   163 	    msg, linecount, line_in);
       
   164     exit(1);
       
   165 
       
   166 }
       
   167 static void
       
   168 trim_newline(char *line, int max)
       
   169 {
       
   170     char *end = line + max - 1;
       
   171 
       
   172     for (; *line && (line < end); ++line) {
       
   173 	if (*line == '\n') {
       
   174 	    /* Found newline, drop it */
       
   175 	    *line = 0;
       
   176 	    return;
       
   177 	}
       
   178     }
       
   179 
       
   180     return;
       
   181 }
       
   182 
       
   183 
       
   184 /* Reads a value from the text table. 
       
   185    Exits  with non-zero status 
       
   186    if the table is erroneous in some way. 
       
   187 */
       
   188 static int
       
   189 read_value(unsigned int *outval)
       
   190 {
       
   191     char *res = 0;
       
   192     FILE *file = stdin;
       
   193     unsigned long lval;
       
   194     char *strout = 0;
       
   195 
       
   196     ++linecount;
       
   197     *outval = 0;
       
   198     res = fgets(line_in, sizeof(line_in), file);
       
   199     if (res == 0) {
       
   200 	if (ferror(file)) {
       
   201 	    fprintf(stderr,
       
   202 		    "tag_attr: Error reading table, %d lines read\n",
       
   203 		    linecount);
       
   204 	    exit(1);
       
   205 	}
       
   206 	if (feof(file)) {
       
   207 	    return IS_EOF;
       
   208 	}
       
   209 	/* impossible */
       
   210 	fprintf(stderr, "tag_attr: Impossible error reading table, "
       
   211 		"%d lines read\n", linecount);
       
   212 	exit(1);
       
   213     }
       
   214     trim_newline(line_in, sizeof(line_in));
       
   215     errno = 0;
       
   216     lval = strtoul(line_in, &strout, 0);
       
   217     if (strout == line_in) {
       
   218 	bad_line_input("bad number input!");
       
   219     }
       
   220     if (errno != 0) {
       
   221 	int myerr = errno;
       
   222 
       
   223 	fprintf(stderr, "tag_attr errno %d\n", myerr);
       
   224 	bad_line_input("invalid number on line");
       
   225     }
       
   226     *outval = (int) lval;
       
   227     return NOT_EOF;
       
   228 }
       
   229 
       
   230 int
       
   231 main()
       
   232 {
       
   233     int i;
       
   234     unsigned int num;
       
   235     int input_eof;
       
   236 
       
   237 
       
   238     input_eof = read_value(&num);	/* 0xffffffff */
       
   239     if (IS_EOF == input_eof) {
       
   240 	bad_line_input("Empty input file");
       
   241     }
       
   242     if (num != MAGIC_TOKEN_VALUE) {
       
   243 	bad_line_input("Expected 0xffffffff");
       
   244     }
       
   245     while (!feof(stdin)) {
       
   246 	unsigned int tag;
       
   247 
       
   248 	input_eof = read_value(&tag);
       
   249 	if (IS_EOF == input_eof) {
       
   250 	    /* Reached normal eof */
       
   251 	    break;
       
   252 	}
       
   253 	if (tag >= TABLE_SIZE) {
       
   254 	    bad_line_input("tag value exceeds table size");
       
   255 	}
       
   256 	input_eof = read_value(&num);
       
   257 	if (IS_EOF == input_eof) {
       
   258 	    bad_line_input("Not terminated correctly..");
       
   259 	}
       
   260 	while (num != MAGIC_TOKEN_VALUE) {
       
   261 	    unsigned idx = num / BITS_PER_WORD;
       
   262 	    unsigned bit = num % BITS_PER_WORD;
       
   263 
       
   264 	    if (idx >= COLUMN_COUNT) {
       
   265 		bad_line_input
       
   266 		    ("too many attributes: table incomplete.");
       
   267 	    }
       
   268 	    tag_attr_combination_table[tag][idx] |= (1 << bit);
       
   269 	    input_eof = read_value(&num);
       
   270 	    if (IS_EOF == input_eof) {
       
   271 		bad_line_input("Not terminated correctly.");
       
   272 	    }
       
   273 	}
       
   274     }
       
   275     printf("static unsigned int tag_attr_combination_table [ ][%d]"
       
   276 	   " = {\n", COLUMN_COUNT);
       
   277     for (i = 0; i < TABLE_SIZE; i++) {
       
   278 	printf("/* %-37s*/\n", tag_name[i]);
       
   279 	printf("    { %#.8x, %#.8x, %#.8x, %#.8x,},\n",
       
   280 	       tag_attr_combination_table[i][0],
       
   281 	       tag_attr_combination_table[i][1],
       
   282 	       tag_attr_combination_table[i][2],
       
   283 	       tag_attr_combination_table[i][3]
       
   284 	    );
       
   285     }
       
   286     printf("};\n");
       
   287     return (0);
       
   288 }