* configure: Regenerated.
[official-gcc.git] / gcc / java / jcf-reader.c
blob8978de2c196680cc9d6d05d66bf4d4631bd250a4
1 /* 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.
7 This file is part of GCC.
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.
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.
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/>.
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. */
27 #include "ggc.h"
28 #include "jcf.h"
29 #include "zipfile.h"
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 static int jcf_parse_bootstrap_methods (JCF *, int) ATTRIBUTE_UNUSED;
40 #ifdef NEED_PEEK_ATTRIBUTE
41 static int peek_attribute (JCF *, int, const char *, int);
42 #endif
43 #ifdef NEED_SKIP_ATTRIBUTE
44 static void skip_attribute (JCF *, int);
45 #endif
47 /* Go through all available attribute (ATTRIBUTE_NUMER) and try to
48 identify PEEKED_NAME. Return 1 if PEEKED_NAME was found, 0
49 otherwise. JCF is restored to its initial position before
50 returning. */
52 #ifdef NEED_PEEK_ATTRIBUTE /* Not everyone uses this function */
53 static int
54 peek_attribute (JCF *jcf, int attribute_number, const char *peeked_name,
55 int peeked_name_length)
57 int to_return = 0;
58 long absolute_offset = (long)JCF_TELL (jcf);
59 int i;
61 for (i = 0; !to_return && i < attribute_number; i++)
63 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
64 uint32 attribute_length = JCF_readu4 (jcf);
65 int name_length;
66 const unsigned char *name_data;
68 JCF_FILL (jcf, (long) attribute_length);
69 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf)
70 || JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
71 continue;
73 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
74 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
76 if (name_length == peeked_name_length
77 && ! memcmp (name_data, peeked_name, peeked_name_length))
79 to_return = 1;
80 break;
83 JCF_SKIP (jcf, attribute_length);
86 JCF_SEEK (jcf, absolute_offset);
87 return to_return;
89 #endif
91 #ifdef NEED_SKIP_ATTRIBUTE /* Not everyone uses this function */
92 static void
93 skip_attribute (JCF *jcf, int number_of_attribute)
95 while (number_of_attribute--)
97 JCF_u4 N;
98 JCF_FILL (jcf, 6);
99 (void) JCF_readu2 (jcf);
100 N = JCF_readu4 (jcf);
101 JCF_SKIP (jcf, N);
104 #endif
106 static int
107 get_attribute (JCF *jcf, int index,
108 jv_attr_type attr_type ATTRIBUTE_UNUSED)
110 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
111 uint32 attribute_length = JCF_readu4 (jcf);
112 uint32 start_pos = JCF_TELL(jcf);
113 int name_length;
114 const unsigned char *name_data;
115 JCF_FILL (jcf, (long) attribute_length);
116 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
117 return -2;
118 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
119 return -2;
120 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
121 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
123 #define MATCH_ATTRIBUTE(S) \
124 (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
126 #ifdef IGNORE_ATTRIBUTE
127 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
129 JCF_SKIP (jcf, attribute_length);
131 else
132 #endif
133 #ifdef HANDLE_SOURCEFILE
134 if (MATCH_ATTRIBUTE ("SourceFile"))
136 uint16 sourcefile_index = JCF_readu2 (jcf);
137 HANDLE_SOURCEFILE(sourcefile_index);
139 else
140 #endif
141 #ifdef HANDLE_CONSTANTVALUE
142 if (MATCH_ATTRIBUTE ("ConstantValue"))
144 uint16 constantvalue_index = JCF_readu2 (jcf);
145 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
146 return -2;
147 HANDLE_CONSTANTVALUE(constantvalue_index);
149 else
150 #endif
151 #ifdef HANDLE_CODE_ATTRIBUTE
152 if (MATCH_ATTRIBUTE ("Code"))
154 uint16 j;
155 uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
156 uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
157 uint32 code_length = JCF_readu4 (jcf);
158 uint16 exception_table_length, attributes_count;
159 if (code_length + 12 > attribute_length)
160 return -1;
161 HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
162 JCF_SKIP (jcf, code_length);
163 exception_table_length = JCF_readu2 (jcf);
164 if (code_length + 8 * exception_table_length + 12 > attribute_length)
165 return -1;
166 #ifdef HANDLE_EXCEPTION_TABLE
167 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
168 #endif
169 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
170 attributes_count = JCF_readu2 (jcf);
171 for (j = 0; j < attributes_count; j++)
173 int code = get_attribute (jcf, index, JV_METHOD_ATTR);
174 if (code != 0)
175 return code;
178 else
179 #endif /* HANDLE_CODE_ATTRIBUTE */
180 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
181 if (MATCH_ATTRIBUTE ("Exceptions"))
183 uint16 count = JCF_readu2 (jcf);
184 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
186 else
187 #endif
188 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
189 if (MATCH_ATTRIBUTE ("LineNumberTable"))
191 uint16 count = JCF_readu2 (jcf);
192 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
194 else
195 #endif
196 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
197 if (MATCH_ATTRIBUTE ("LocalVariableTable"))
199 uint16 count = JCF_readu2 (jcf);
200 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
202 else
203 #endif
204 #ifdef HANDLE_LOCALVARIABLETYPETABLE_ATTRIBUTE
205 if (MATCH_ATTRIBUTE ("LocalVariableTypeTable"))
207 uint16 count = JCF_readu2 (jcf);
208 HANDLE_LOCALVARIABLETYPETABLE_ATTRIBUTE (count);
210 else
211 #endif
212 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
213 if (MATCH_ATTRIBUTE ("InnerClasses"))
215 uint16 count = JCF_readu2 (jcf);
216 HANDLE_INNERCLASSES_ATTRIBUTE (count);
218 else
219 #endif
220 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
221 if (MATCH_ATTRIBUTE ("Synthetic"))
223 HANDLE_SYNTHETIC_ATTRIBUTE ();
225 else
226 #endif
227 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
228 if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
230 HANDLE_GCJCOMPILED_ATTRIBUTE ();
232 else
233 #endif
234 #ifdef HANDLE_DEPRECATED_ATTRIBUTE
235 if (MATCH_ATTRIBUTE ("Deprecated"))
237 HANDLE_DEPRECATED_ATTRIBUTE ();
239 else
240 #endif
241 #ifdef HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE
242 if (MATCH_ATTRIBUTE ("SourceDebugExtension")) /* JSR 45 */
244 HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE (attribute_length);
246 else
247 #endif
248 #ifdef HANDLE_ENCLOSINGMETHOD_ATTRIBUTE
249 if (MATCH_ATTRIBUTE ("EnclosingMethod"))
251 HANDLE_ENCLOSINGMETHOD_ATTRIBUTE ();
253 else
254 #endif
255 #ifdef HANDLE_SIGNATURE_ATTRIBUTE
256 if (MATCH_ATTRIBUTE ("Signature"))
258 HANDLE_SIGNATURE_ATTRIBUTE ();
260 else
261 #endif
262 #ifdef HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE
263 if (MATCH_ATTRIBUTE ("RuntimeVisibleAnnotations"))
265 HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE ();
267 else
268 #endif
269 #ifdef HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE
270 if (MATCH_ATTRIBUTE ("RuntimeInvisibleAnnotations"))
272 HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE ();
274 else
275 #endif
276 #ifdef HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE
277 if (MATCH_ATTRIBUTE ("RuntimeVisibleParameterAnnotations"))
279 HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE ();
281 else
282 #endif
283 #ifdef HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE
284 if (MATCH_ATTRIBUTE ("RuntimeInvisibleParameterAnnotations"))
286 HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE ();
288 else
289 #endif
290 #ifdef HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE
291 if (MATCH_ATTRIBUTE ("AnnotationDefault"))
293 HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE ();
295 else
296 #endif
297 if (MATCH_ATTRIBUTE ("BootstrapMethods"))
299 #ifdef HANDLE_BOOTSTRAP_METHODS_ATTRIBUTE
300 HANDLE_BOOTSTRAP_METHODS_ATTRIBUTE();
301 #else
302 JCF_SKIP (jcf, attribute_length);
303 #endif
305 else
307 #ifdef PROCESS_OTHER_ATTRIBUTE
308 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
309 #else
310 JCF_SKIP (jcf, attribute_length);
311 #endif
313 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
314 return -1;
315 return 0;
318 /* Read and handle the pre-amble. */
319 static int
320 jcf_parse_preamble (JCF* jcf)
322 uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
323 uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
324 uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
325 #ifdef HANDLE_MAGIC
326 HANDLE_MAGIC (magic, minor_version, major_version);
327 #endif
328 if (magic != 0xcafebabe)
329 return -1;
330 else
331 return 0;
334 /* Read and handle the constant pool.
336 Return 0 if OK.
337 Return -2 if a bad cross-reference (index of other constant) was seen.
339 static int
340 jcf_parse_constant_pool (JCF* jcf)
342 int i, n;
343 JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
344 jcf->cpool.tags = (uint8 *) ggc_alloc_atomic (JPOOL_SIZE (jcf));
345 jcf->cpool.data = ggc_alloc_cpool_entry (sizeof (jword) * JPOOL_SIZE (jcf));
346 jcf->cpool.tags[0] = 0;
347 #ifdef HANDLE_START_CONSTANT_POOL
348 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
349 #endif
350 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
352 int constant_kind;
354 /* Make sure at least 9 bytes are available. This is enough
355 for all fixed-sized constant pool entries (so we don't need many
356 more JCF_FILL calls below), but is is small enough that
357 we are guaranteed to not hit EOF (in a valid .class file). */
358 JCF_FILL (jcf, 9);
359 constant_kind = JCF_readu (jcf);
360 jcf->cpool.tags[i] = constant_kind;
361 switch (constant_kind)
363 case CONSTANT_String:
364 case CONSTANT_Class:
365 jcf->cpool.data[i].w = JCF_readu2 (jcf);
366 break;
367 case CONSTANT_Fieldref:
368 case CONSTANT_Methodref:
369 case CONSTANT_InterfaceMethodref:
370 case CONSTANT_NameAndType:
371 jcf->cpool.data[i].w = JCF_readu2 (jcf);
372 jcf->cpool.data[i].w |= JCF_readu2 (jcf) << 16;
373 break;
374 case CONSTANT_Integer:
375 case CONSTANT_Float:
376 jcf->cpool.data[i].w = JCF_readu4 (jcf);
377 break;
378 case CONSTANT_Long:
379 case CONSTANT_Double:
380 jcf->cpool.data[i].w = JCF_readu4 (jcf);
381 i++; /* These take up two spots in the constant pool */
382 jcf->cpool.tags[i] = 0;
383 jcf->cpool.data[i].w = JCF_readu4 (jcf);
384 break;
385 case CONSTANT_Utf8:
386 n = JCF_readu2 (jcf);
387 JCF_FILL (jcf, n);
388 #ifdef HANDLE_CONSTANT_Utf8
389 HANDLE_CONSTANT_Utf8(jcf, i, n);
390 #else
391 jcf->cpool.data[i].w = JCF_TELL(jcf) - 2;
392 JCF_SKIP (jcf, n);
393 #endif
394 break;
395 case CONSTANT_MethodHandle:
396 jcf->cpool.data[i].w = JCF_readu (jcf);
397 jcf->cpool.data[i].w |= JCF_readu2 (jcf) << 16;
398 break;
399 case CONSTANT_MethodType:
400 jcf->cpool.data[i].w = JCF_readu2 (jcf);
401 break;
402 case CONSTANT_InvokeDynamic:
403 jcf->cpool.data[i].w = JCF_readu2 (jcf);
404 jcf->cpool.data[i].w |= JCF_readu2 (jcf) << 16;
405 break;
406 default:
407 return i;
410 return 0;
413 /* Read various class flags and numbers. */
415 static void
416 jcf_parse_class (JCF* jcf)
418 int i;
419 uint16 interfaces_count;
420 JCF_FILL (jcf, 8);
421 jcf->access_flags = JCF_readu2 (jcf);
422 jcf->this_class = JCF_readu2 (jcf);
423 jcf->super_class = JCF_readu2 (jcf);
424 interfaces_count = JCF_readu2 (jcf);
426 #ifdef HANDLE_CLASS_INFO
427 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
428 #endif
430 JCF_FILL (jcf, 2 * interfaces_count);
432 /* Read interfaces. */
433 for (i = 0; i < interfaces_count; i++)
435 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
436 #ifdef HANDLE_CLASS_INTERFACE
437 HANDLE_CLASS_INTERFACE (index);
438 #endif
442 /* Read fields. */
443 static int
444 jcf_parse_fields (JCF* jcf)
446 int i, j;
447 uint16 fields_count;
448 JCF_FILL (jcf, 2);
449 fields_count = JCF_readu2 (jcf);
451 #ifdef HANDLE_START_FIELDS
452 HANDLE_START_FIELDS (fields_count);
453 #endif
454 for (i = 0; i < fields_count; i++)
456 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
457 uint16 name_index = JCF_readu2 (jcf);
458 uint16 signature_index = JCF_readu2 (jcf);
459 uint16 attribute_count = JCF_readu2 (jcf);
460 #ifdef HANDLE_START_FIELD
461 HANDLE_START_FIELD (access_flags, name_index, signature_index,
462 attribute_count);
463 #endif
464 for (j = 0; j < attribute_count; j++)
466 int code = get_attribute (jcf, i, JV_FIELD_ATTR);
467 if (code != 0)
468 return code;
470 #ifdef HANDLE_END_FIELD
471 HANDLE_END_FIELD ();
472 #endif
474 #ifdef HANDLE_END_FIELDS
475 HANDLE_END_FIELDS ();
476 #endif
477 return 0;
480 /* Read methods. */
482 static int
483 jcf_parse_one_method (JCF* jcf, int index)
485 int i;
486 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
487 uint16 name_index = JCF_readu2 (jcf);
488 uint16 signature_index = JCF_readu2 (jcf);
489 uint16 attribute_count = JCF_readu2 (jcf);
490 #ifdef HANDLE_METHOD
491 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
492 #endif
493 for (i = 0; i < attribute_count; i++)
495 int code = get_attribute (jcf, index, JV_METHOD_ATTR);
496 if (code != 0)
497 return code;
499 #ifdef HANDLE_END_METHOD
500 HANDLE_END_METHOD ();
501 #endif
502 return 0;
505 static int
506 jcf_parse_methods (JCF* jcf)
508 int i;
509 uint16 methods_count;
510 JCF_FILL (jcf, 2);
511 methods_count = JCF_readu2 (jcf);
512 #ifdef HANDLE_START_METHODS
513 HANDLE_START_METHODS (methods_count);
514 #endif
515 for (i = 0; i < methods_count; i++)
517 int code = jcf_parse_one_method (jcf, i);
518 if (code != 0)
519 return code;
521 #ifdef HANDLE_END_METHODS
522 HANDLE_END_METHODS ();
523 #endif
524 return 0;
527 /* Read attributes. */
528 static int
529 jcf_parse_final_attributes (JCF *jcf)
531 int i;
532 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
533 #ifdef START_FINAL_ATTRIBUTES
534 START_FINAL_ATTRIBUTES (attributes_count)
535 #endif
536 for (i = 0; i < attributes_count; i++)
538 int code = get_attribute (jcf, i, JV_CLASS_ATTR);
539 if (code != 0)
540 return code;
542 return 0;
545 /* Read and handle the "BootstrapMethods" attribute.
547 Return 0 if OK.
549 static int
550 jcf_parse_bootstrap_methods (JCF* jcf, int attribute_length ATTRIBUTE_UNUSED)
552 int i;
553 uint16 num_methods = JCF_readu2 (jcf);
554 jcf->bootstrap_methods.count = num_methods;
555 jcf->bootstrap_methods.methods
556 = (bootstrap_method *) ggc_alloc_atomic (num_methods
557 * sizeof (bootstrap_method));
558 #ifdef HANDLE_START_BOOTSTRAP_METHODS
559 HANDLE_START_BOOTSTRAP_METHODS (jcf, num_methods);
560 #endif
562 for (i = 0; i < num_methods; i++)
564 unsigned j;
565 bootstrap_method *m = &jcf->bootstrap_methods.methods[i];
566 m->method_ref = JCF_readu2 (jcf);
567 m->num_arguments = JCF_readu2 (jcf);
568 m->bootstrap_arguments
569 = (unsigned *) ggc_alloc_atomic (m->num_arguments
570 * sizeof (unsigned));
571 for (j = 0; j < m->num_arguments; j++)
572 m->bootstrap_arguments[j] = JCF_readu2 (jcf);
575 #ifdef HANDLE_END_BOOTSTRAP_METHODS
576 HANDLE_END_BOOTSTRAP_METHODS (num_methods);
577 #endif
579 return 0;