OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [java/] [jcf-reader.c] - Blame information for rev 820

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 715 jeremybenn
/* This file read a Java(TM) .class file.
2
   It is not stand-alone:  It depends on tons of macros, and the
3
   intent is you #include this file after you've defined the macros.
4
   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
5
   2007, 2008, 2010 Free Software Foundation, Inc.
6
 
7
This file is part of GCC.
8
 
9
GCC is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 3, or (at your option)
12
any later version.
13
 
14
GCC is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with GCC; see the file COPYING3.  If not see
21
<http://www.gnu.org/licenses/>.
22
 
23
Java and all Java-based marks are trademarks or registered trademarks
24
of Sun Microsystems, Inc. in the United States and other countries.
25
The Free Software Foundation is independent of Sun Microsystems, Inc.  */
26
 
27
#include "ggc.h"
28
#include "jcf.h"
29
#include "zipfile.h"
30
 
31
static int get_attribute (JCF *, int, jv_attr_type);
32
static int jcf_parse_preamble (JCF *);
33
static int jcf_parse_constant_pool (JCF *);
34
static void jcf_parse_class (JCF *);
35
static int jcf_parse_fields (JCF *);
36
static int jcf_parse_one_method (JCF *, int);
37
static int jcf_parse_methods (JCF *);
38
static int jcf_parse_final_attributes (JCF *);
39
#ifdef NEED_PEEK_ATTRIBUTE
40
static int peek_attribute (JCF *, int, const char *, int);
41
#endif
42
#ifdef NEED_SKIP_ATTRIBUTE
43
static void skip_attribute (JCF *, int);
44
#endif
45
 
46
/* Go through all available attribute (ATTRIBUTE_NUMER) and try to
47
   identify PEEKED_NAME.  Return 1 if PEEKED_NAME was found, 0
48
   otherwise. JCF is restored to its initial position before
49
   returning.  */
50
 
51
#ifdef NEED_PEEK_ATTRIBUTE      /* Not everyone uses this function */
52
static int
53
peek_attribute (JCF *jcf, int attribute_number, const char *peeked_name,
54
                int peeked_name_length)
55
{
56
  int to_return = 0;
57
  long absolute_offset = (long)JCF_TELL (jcf);
58
  int i;
59
 
60
  for (i = 0; !to_return && i < attribute_number; i++)
61
    {
62
      uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
63
      uint32 attribute_length = JCF_readu4 (jcf);
64
      int name_length;
65
      const unsigned char *name_data;
66
 
67
      JCF_FILL (jcf, (long) attribute_length);
68
      if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf)
69
          || JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
70
        continue;
71
 
72
      name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
73
      name_data = JPOOL_UTF_DATA (jcf, attribute_name);
74
 
75
      if (name_length == peeked_name_length
76
          && ! memcmp (name_data, peeked_name, peeked_name_length))
77
        {
78
          to_return = 1;
79
          break;
80
        }
81
 
82
      JCF_SKIP (jcf, attribute_length);
83
    }
84
 
85
  JCF_SEEK (jcf, absolute_offset);
86
  return to_return;
87
}
88
#endif
89
 
90
#ifdef NEED_SKIP_ATTRIBUTE      /* Not everyone uses this function */
91
static void
92
skip_attribute (JCF *jcf, int number_of_attribute)
93
{
94
  while (number_of_attribute--)
95
    {
96
      JCF_u4 N;
97
      JCF_FILL (jcf, 6);
98
      (void) JCF_readu2 (jcf);
99
      N = JCF_readu4 (jcf);
100
      JCF_SKIP (jcf, N);
101
    }
102
}
103
#endif
104
 
105
static int
106
get_attribute (JCF *jcf, int index,
107
               jv_attr_type attr_type ATTRIBUTE_UNUSED)
108
{
109
  uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
110
  uint32 attribute_length = JCF_readu4 (jcf);
111
  uint32 start_pos = JCF_TELL(jcf);
112
  int name_length;
113
  const unsigned char *name_data;
114
  JCF_FILL (jcf, (long) attribute_length);
115
  if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
116
    return -2;
117
  if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
118
    return -2;
119
  name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
120
  name_data = JPOOL_UTF_DATA (jcf, attribute_name);
121
 
122
#define MATCH_ATTRIBUTE(S) \
123
  (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
124
 
125
#ifdef IGNORE_ATTRIBUTE
126
   if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
127
     {
128
       JCF_SKIP (jcf, attribute_length);
129
     }
130
   else
131
#endif
132
#ifdef HANDLE_SOURCEFILE
133
  if (MATCH_ATTRIBUTE ("SourceFile"))
134
    {
135
      uint16 sourcefile_index = JCF_readu2 (jcf);
136
      HANDLE_SOURCEFILE(sourcefile_index);
137
    }
138
  else
139
#endif
140
#ifdef HANDLE_CONSTANTVALUE
141
  if (MATCH_ATTRIBUTE ("ConstantValue"))
142
    {
143
      uint16 constantvalue_index = JCF_readu2 (jcf);
144
      if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
145
        return -2;
146
      HANDLE_CONSTANTVALUE(constantvalue_index);
147
    }
148
  else
149
#endif
150
#ifdef HANDLE_CODE_ATTRIBUTE
151
  if (MATCH_ATTRIBUTE ("Code"))
152
    {
153
      uint16 j;
154
      uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
155
      uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
156
      uint32 code_length = JCF_readu4 (jcf);
157
      uint16 exception_table_length, attributes_count;
158
      if (code_length + 12 > attribute_length)
159
        return -1;
160
      HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
161
      JCF_SKIP (jcf, code_length);
162
      exception_table_length = JCF_readu2 (jcf);
163
      if (code_length + 8 * exception_table_length + 12 > attribute_length)
164
        return -1;
165
#ifdef HANDLE_EXCEPTION_TABLE
166
      HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
167
#endif
168
      JCF_SKIP (jcf, 2 * 4 * exception_table_length);
169
      attributes_count = JCF_readu2 (jcf);
170
      for (j = 0; j < attributes_count; j++)
171
        {
172
          int code = get_attribute (jcf, index, JV_METHOD_ATTR);
173
          if (code != 0)
174
            return code;
175
        }
176
    }
177
  else
178
#endif /* HANDLE_CODE_ATTRIBUTE */
179
#ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
180
  if (MATCH_ATTRIBUTE ("Exceptions"))
181
    {
182
      uint16 count = JCF_readu2 (jcf);
183
      HANDLE_EXCEPTIONS_ATTRIBUTE (count);
184
    }
185
  else
186
#endif
187
#ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
188
  if (MATCH_ATTRIBUTE ("LineNumberTable"))
189
    {
190
      uint16 count = JCF_readu2 (jcf);
191
      HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
192
    }
193
  else
194
#endif
195
#ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
196
  if (MATCH_ATTRIBUTE ("LocalVariableTable"))
197
    {
198
      uint16 count = JCF_readu2 (jcf);
199
      HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
200
    }
201
  else
202
#endif
203
#ifdef HANDLE_LOCALVARIABLETYPETABLE_ATTRIBUTE
204
  if (MATCH_ATTRIBUTE ("LocalVariableTypeTable"))
205
    {
206
      uint16 count = JCF_readu2 (jcf);
207
      HANDLE_LOCALVARIABLETYPETABLE_ATTRIBUTE (count);
208
    }
209
  else
210
#endif
211
#ifdef HANDLE_INNERCLASSES_ATTRIBUTE
212
  if (MATCH_ATTRIBUTE ("InnerClasses"))
213
    {
214
      uint16 count = JCF_readu2 (jcf);
215
      HANDLE_INNERCLASSES_ATTRIBUTE (count);
216
    }
217
  else
218
#endif
219
#ifdef HANDLE_SYNTHETIC_ATTRIBUTE
220
  if (MATCH_ATTRIBUTE ("Synthetic"))
221
    {
222
      HANDLE_SYNTHETIC_ATTRIBUTE ();
223
    }
224
  else
225
#endif
226
#ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
227
  if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
228
    {
229
      HANDLE_GCJCOMPILED_ATTRIBUTE ();
230
    }
231
  else
232
#endif
233
#ifdef HANDLE_DEPRECATED_ATTRIBUTE
234
  if (MATCH_ATTRIBUTE ("Deprecated"))
235
    {
236
      HANDLE_DEPRECATED_ATTRIBUTE ();
237
    }
238
  else
239
#endif
240
#ifdef HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE
241
  if (MATCH_ATTRIBUTE ("SourceDebugExtension")) /* JSR 45 */
242
    {
243
      HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE (attribute_length);
244
    }
245
  else
246
#endif
247
#ifdef HANDLE_ENCLOSINGMETHOD_ATTRIBUTE
248
  if (MATCH_ATTRIBUTE ("EnclosingMethod"))
249
    {
250
      HANDLE_ENCLOSINGMETHOD_ATTRIBUTE ();
251
    }
252
  else
253
#endif
254
#ifdef HANDLE_SIGNATURE_ATTRIBUTE
255
  if (MATCH_ATTRIBUTE ("Signature"))
256
    {
257
      HANDLE_SIGNATURE_ATTRIBUTE ();
258
    }
259
  else
260
#endif
261
#ifdef HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE
262
  if (MATCH_ATTRIBUTE ("RuntimeVisibleAnnotations"))
263
    {
264
      HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE ();
265
    }
266
  else
267
#endif
268
#ifdef HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE
269
  if (MATCH_ATTRIBUTE ("RuntimeInvisibleAnnotations"))
270
    {
271
      HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE ();
272
    }
273
  else
274
#endif
275
#ifdef HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE
276
  if (MATCH_ATTRIBUTE ("RuntimeVisibleParameterAnnotations"))
277
    {
278
      HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE ();
279
    }
280
  else
281
#endif
282
#ifdef HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE
283
  if (MATCH_ATTRIBUTE ("RuntimeInvisibleParameterAnnotations"))
284
    {
285
      HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE ();
286
    }
287
  else
288
#endif
289
#ifdef HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE
290
  if (MATCH_ATTRIBUTE ("AnnotationDefault"))
291
    {
292
      HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE ();
293
    }
294
  else
295
#endif
296
    {
297
#ifdef PROCESS_OTHER_ATTRIBUTE
298
      PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
299
#else
300
      JCF_SKIP (jcf, attribute_length);
301
#endif
302
    }
303
  if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
304
    return -1;
305
  return 0;
306
}
307
 
308
/* Read and handle the pre-amble. */
309
static int
310
jcf_parse_preamble (JCF* jcf)
311
{
312
  uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
313
  uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
314
  uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
315
#ifdef HANDLE_MAGIC
316
  HANDLE_MAGIC (magic, minor_version, major_version);
317
#endif
318
  if (magic != 0xcafebabe)
319
    return -1;
320
  else
321
    return 0;
322
}
323
 
324
/* Read and handle the constant pool.
325
 
326
   Return 0 if OK.
327
   Return -2 if a bad cross-reference (index of other constant) was seen.
328
*/
329
static int
330
jcf_parse_constant_pool (JCF* jcf)
331
{
332
  int i, n;
333
  JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
334
  jcf->cpool.tags = (uint8 *) ggc_alloc_atomic (JPOOL_SIZE (jcf));
335
  jcf->cpool.data = ggc_alloc_cpool_entry (sizeof (jword) * JPOOL_SIZE (jcf));
336
  jcf->cpool.tags[0] = 0;
337
#ifdef HANDLE_START_CONSTANT_POOL
338
  HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
339
#endif
340
  for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
341
    {
342
      int constant_kind;
343
 
344
      /* Make sure at least 9 bytes are available.  This is enough
345
         for all fixed-sized constant pool entries (so we don't need many
346
         more JCF_FILL calls below), but is is small enough that
347
         we are guaranteed to not hit EOF (in a valid .class file). */
348
      JCF_FILL (jcf, 9);
349
      constant_kind = JCF_readu (jcf);
350
      jcf->cpool.tags[i] = constant_kind;
351
      switch (constant_kind)
352
        {
353
        case CONSTANT_String:
354
        case CONSTANT_Class:
355
          jcf->cpool.data[i].w = JCF_readu2 (jcf);
356
          break;
357
        case CONSTANT_Fieldref:
358
        case CONSTANT_Methodref:
359
        case CONSTANT_InterfaceMethodref:
360
        case CONSTANT_NameAndType:
361
          jcf->cpool.data[i].w = JCF_readu2 (jcf);
362
          jcf->cpool.data[i].w |= JCF_readu2 (jcf) << 16;
363
          break;
364
        case CONSTANT_Integer:
365
        case CONSTANT_Float:
366
          jcf->cpool.data[i].w = JCF_readu4 (jcf);
367
          break;
368
        case CONSTANT_Long:
369
        case CONSTANT_Double:
370
          jcf->cpool.data[i].w = JCF_readu4 (jcf);
371
          i++; /* These take up two spots in the constant pool */
372
          jcf->cpool.tags[i] = 0;
373
          jcf->cpool.data[i].w = JCF_readu4 (jcf);
374
          break;
375
        case CONSTANT_Utf8:
376
          n = JCF_readu2 (jcf);
377
          JCF_FILL (jcf, n);
378
#ifdef HANDLE_CONSTANT_Utf8
379
          HANDLE_CONSTANT_Utf8(jcf, i, n);
380
#else
381
          jcf->cpool.data[i].w = JCF_TELL(jcf) - 2;
382
          JCF_SKIP (jcf, n);
383
#endif
384
          break;
385
        default:
386
          return i;
387
        }
388
    }
389
  return 0;
390
}
391
 
392
/* Read various class flags and numbers. */
393
 
394
static void
395
jcf_parse_class (JCF* jcf)
396
{
397
  int i;
398
  uint16 interfaces_count;
399
  JCF_FILL (jcf, 8);
400
  jcf->access_flags = JCF_readu2 (jcf);
401
  jcf->this_class = JCF_readu2 (jcf);
402
  jcf->super_class = JCF_readu2 (jcf);
403
  interfaces_count = JCF_readu2 (jcf);
404
 
405
#ifdef HANDLE_CLASS_INFO
406
  HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
407
#endif
408
 
409
  JCF_FILL (jcf, 2 * interfaces_count);
410
 
411
  /* Read interfaces. */
412
  for (i = 0; i < interfaces_count; i++)
413
    {
414
      uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
415
#ifdef HANDLE_CLASS_INTERFACE
416
      HANDLE_CLASS_INTERFACE (index);
417
#endif
418
    }
419
}
420
 
421
/* Read fields. */
422
static int
423
jcf_parse_fields (JCF* jcf)
424
{
425
  int i, j;
426
  uint16 fields_count;
427
  JCF_FILL (jcf, 2);
428
  fields_count = JCF_readu2 (jcf);
429
 
430
#ifdef HANDLE_START_FIELDS
431
  HANDLE_START_FIELDS (fields_count);
432
#endif
433
  for (i = 0; i < fields_count; i++)
434
    {
435
      uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
436
      uint16 name_index = JCF_readu2 (jcf);
437
      uint16 signature_index = JCF_readu2 (jcf);
438
      uint16 attribute_count = JCF_readu2 (jcf);
439
#ifdef HANDLE_START_FIELD
440
      HANDLE_START_FIELD (access_flags, name_index, signature_index,
441
                          attribute_count);
442
#endif
443
      for (j = 0; j < attribute_count; j++)
444
        {
445
          int code = get_attribute (jcf, i, JV_FIELD_ATTR);
446
          if (code != 0)
447
            return code;
448
        }
449
#ifdef HANDLE_END_FIELD
450
      HANDLE_END_FIELD ();
451
#endif
452
    }
453
#ifdef HANDLE_END_FIELDS
454
  HANDLE_END_FIELDS ();
455
#endif
456
  return 0;
457
}
458
 
459
/* Read methods. */
460
 
461
static int
462
jcf_parse_one_method (JCF* jcf, int index)
463
{
464
  int i;
465
  uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
466
  uint16 name_index = JCF_readu2 (jcf);
467
  uint16 signature_index = JCF_readu2 (jcf);
468
  uint16 attribute_count = JCF_readu2 (jcf);
469
#ifdef HANDLE_METHOD
470
  HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
471
#endif
472
  for (i = 0; i < attribute_count; i++)
473
    {
474
      int code = get_attribute (jcf, index, JV_METHOD_ATTR);
475
      if (code != 0)
476
        return code;
477
    }
478
#ifdef HANDLE_END_METHOD
479
  HANDLE_END_METHOD ();
480
#endif
481
  return 0;
482
}
483
 
484
static int
485
jcf_parse_methods (JCF* jcf)
486
{
487
  int i;
488
  uint16 methods_count;
489
  JCF_FILL (jcf, 2);
490
  methods_count = JCF_readu2 (jcf);
491
#ifdef HANDLE_START_METHODS
492
  HANDLE_START_METHODS (methods_count);
493
#endif
494
  for (i = 0; i < methods_count; i++)
495
    {
496
      int code = jcf_parse_one_method (jcf, i);
497
      if (code != 0)
498
        return code;
499
    }
500
#ifdef HANDLE_END_METHODS
501
  HANDLE_END_METHODS ();
502
#endif
503
  return 0;
504
}
505
 
506
/* Read attributes. */
507
static int
508
jcf_parse_final_attributes (JCF *jcf)
509
{
510
  int i;
511
  uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
512
#ifdef START_FINAL_ATTRIBUTES
513
  START_FINAL_ATTRIBUTES (attributes_count)
514
#endif
515
  for (i = 0; i < attributes_count; i++)
516
    {
517
      int code = get_attribute (jcf, i, JV_CLASS_ATTR);
518
      if (code != 0)
519
        return code;
520
    }
521
  return 0;
522
}
523
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.