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
5 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 2, or (at your option)
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 COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
31 static int get_attribute PARAMS ((JCF *));
32 static int jcf_parse_preamble PARAMS ((JCF *));
33 static int jcf_parse_constant_pool PARAMS ((JCF *));
34 static void jcf_parse_class PARAMS ((JCF *));
35 static int jcf_parse_fields PARAMS ((JCF *));
36 static int jcf_parse_one_method PARAMS ((JCF *));
37 static int jcf_parse_methods PARAMS ((JCF *));
38 static int jcf_parse_final_attributes PARAMS ((JCF *));
39 #ifdef NEED_PEEK_ATTRIBUTE
40 static int peek_attribute PARAMS ((JCF *, int, const char *, int));
42 #ifdef NEED_SKIP_ATTRIBUTE
43 static void skip_attribute PARAMS ((JCF *, int));
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
51 #ifdef NEED_PEEK_ATTRIBUTE /* Not everyone uses this function */
53 peek_attribute (jcf, attribute_number, peeked_name, peeked_name_length)
56 const char *peeked_name;
57 int peeked_name_length;
60 long absolute_offset = (long)JCF_TELL (jcf);
63 for (i = 0; !to_return && i < attribute_number; i++)
65 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
66 uint32 attribute_length = JCF_readu4 (jcf);
68 const unsigned char *name_data;
70 JCF_FILL (jcf, (long) attribute_length);
71 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf)
72 || JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
75 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
76 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
78 if (name_length == peeked_name_length
79 && ! memcmp (name_data, peeked_name, peeked_name_length))
85 JCF_SKIP (jcf, attribute_length);
88 JCF_SEEK (jcf, absolute_offset);
93 #ifdef NEED_SKIP_ATTRIBUTE /* Not everyone uses this function */
95 skip_attribute (jcf, number_of_attribute)
97 int number_of_attribute;
99 while (number_of_attribute--)
103 (void) JCF_readu2 (jcf);
104 N = JCF_readu4 (jcf);
111 DEFUN(get_attribute, (jcf),
114 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
115 uint32 attribute_length = JCF_readu4 (jcf);
116 uint32 start_pos = JCF_TELL(jcf);
118 const unsigned char *name_data;
119 JCF_FILL (jcf, (long) attribute_length);
120 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
122 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
124 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
125 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
127 #define MATCH_ATTRIBUTE(S) \
128 (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
130 #ifdef IGNORE_ATTRIBUTE
131 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
133 JCF_SKIP (jcf, attribute_length);
137 #ifdef HANDLE_SOURCEFILE
138 if (MATCH_ATTRIBUTE ("SourceFile"))
140 uint16 sourcefile_index = JCF_readu2 (jcf);
141 HANDLE_SOURCEFILE(sourcefile_index);
145 #ifdef HANDLE_CONSTANTVALUE
146 if (MATCH_ATTRIBUTE ("ConstantValue"))
148 uint16 constantvalue_index = JCF_readu2 (jcf);
149 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
151 HANDLE_CONSTANTVALUE(constantvalue_index);
155 #ifdef HANDLE_CODE_ATTRIBUTE
156 if (MATCH_ATTRIBUTE ("Code"))
159 uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
160 uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
161 uint32 code_length = JCF_readu4 (jcf);
162 uint16 exception_table_length, attributes_count;
163 if (code_length + 12 > attribute_length)
165 HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
166 JCF_SKIP (jcf, code_length);
167 exception_table_length = JCF_readu2 (jcf);
168 if (code_length + 8 * exception_table_length + 12 > attribute_length)
170 #ifdef HANDLE_EXCEPTION_TABLE
171 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
173 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
174 attributes_count = JCF_readu2 (jcf);
175 for (j = 0; j < attributes_count; j++)
177 int code = get_attribute (jcf);
183 #endif /* HANDLE_CODE_ATTRIBUTE */
184 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
185 if (MATCH_ATTRIBUTE ("Exceptions"))
187 uint16 count = JCF_readu2 (jcf);
188 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
192 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
193 if (MATCH_ATTRIBUTE ("LineNumberTable"))
195 uint16 count = JCF_readu2 (jcf);
196 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
200 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
201 if (MATCH_ATTRIBUTE ("LocalVariableTable"))
203 uint16 count = JCF_readu2 (jcf);
204 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
208 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
209 if (MATCH_ATTRIBUTE ("InnerClasses"))
211 uint16 count = JCF_readu2 (jcf);
212 HANDLE_INNERCLASSES_ATTRIBUTE (count);
216 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
217 if (MATCH_ATTRIBUTE ("Synthetic"))
219 HANDLE_SYNTHETIC_ATTRIBUTE ();
223 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
224 if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
226 HANDLE_GCJCOMPILED_ATTRIBUTE ();
231 #ifdef PROCESS_OTHER_ATTRIBUTE
232 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
234 JCF_SKIP (jcf, attribute_length);
237 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
242 /* Read and handle the pre-amble. */
244 DEFUN(jcf_parse_preamble, (jcf),
247 uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
248 uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
249 uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
251 HANDLE_MAGIC (magic, minor_version, major_version);
253 if (magic != 0xcafebabe)
259 /* Read and handle the constant pool.
262 Return -2 if a bad cross-reference (index of other constant) was seen.
265 DEFUN(jcf_parse_constant_pool, (jcf),
269 JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
270 jcf->cpool.tags = ALLOC (JPOOL_SIZE (jcf));
271 jcf->cpool.data = ALLOC (sizeof (jword) * JPOOL_SIZE (jcf));
272 jcf->cpool.tags[0] = 0;
273 #ifdef HANDLE_START_CONSTANT_POOL
274 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
276 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
280 /* Make sure at least 9 bytes are available. This is enough
281 for all fixed-sized constant pool entries (so we don't need many
282 more JCF_FILL calls below), but is is small enough that
283 we are guaranteed to not hit EOF (in a valid .class file). */
285 constant_kind = JCF_readu (jcf);
286 jcf->cpool.tags[i] = constant_kind;
287 switch (constant_kind)
289 case CONSTANT_String:
291 jcf->cpool.data[i] = JCF_readu2 (jcf);
293 case CONSTANT_Fieldref:
294 case CONSTANT_Methodref:
295 case CONSTANT_InterfaceMethodref:
296 case CONSTANT_NameAndType:
297 jcf->cpool.data[i] = JCF_readu2 (jcf);
298 jcf->cpool.data[i] |= JCF_readu2 (jcf) << 16;
300 case CONSTANT_Integer:
302 jcf->cpool.data[i] = JCF_readu4 (jcf);
305 case CONSTANT_Double:
306 jcf->cpool.data[i] = JCF_readu4 (jcf);
307 i++; /* These take up two spots in the constant pool */
308 jcf->cpool.tags[i] = 0;
309 jcf->cpool.data[i] = JCF_readu4 (jcf);
312 n = JCF_readu2 (jcf);
314 #ifdef HANDLE_CONSTANT_Utf8
315 HANDLE_CONSTANT_Utf8(jcf, i, n);
317 jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
328 /* Read various class flags and numbers. */
331 DEFUN(jcf_parse_class, (jcf),
335 uint16 interfaces_count;
337 jcf->access_flags = JCF_readu2 (jcf);
338 jcf->this_class = JCF_readu2 (jcf);
339 jcf->super_class = JCF_readu2 (jcf);
340 interfaces_count = JCF_readu2 (jcf);
342 #ifdef HANDLE_CLASS_INFO
343 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
346 JCF_FILL (jcf, 2 * interfaces_count);
348 /* Read interfaces. */
349 for (i = 0; i < interfaces_count; i++)
351 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
352 #ifdef HANDLE_CLASS_INTERFACE
353 HANDLE_CLASS_INTERFACE (index);
360 DEFUN(jcf_parse_fields, (jcf),
366 fields_count = JCF_readu2 (jcf);
368 #ifdef HANDLE_START_FIELDS
369 HANDLE_START_FIELDS (fields_count);
371 for (i = 0; i < fields_count; i++)
373 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
374 uint16 name_index = JCF_readu2 (jcf);
375 uint16 signature_index = JCF_readu2 (jcf);
376 uint16 attribute_count = JCF_readu2 (jcf);
377 #ifdef HANDLE_START_FIELD
378 HANDLE_START_FIELD (access_flags, name_index, signature_index,
381 for (j = 0; j < attribute_count; j++)
383 int code = get_attribute (jcf);
387 #ifdef HANDLE_END_FIELD
391 #ifdef HANDLE_END_FIELDS
392 HANDLE_END_FIELDS ();
400 DEFUN(jcf_parse_one_method, (jcf),
404 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
405 uint16 name_index = JCF_readu2 (jcf);
406 uint16 signature_index = JCF_readu2 (jcf);
407 uint16 attribute_count = JCF_readu2 (jcf);
409 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
411 for (i = 0; i < attribute_count; i++)
413 int code = get_attribute (jcf);
417 #ifdef HANDLE_END_METHOD
418 HANDLE_END_METHOD ();
424 DEFUN(jcf_parse_methods, (jcf),
428 uint16 methods_count;
430 methods_count = JCF_readu2 (jcf);
431 #ifdef HANDLE_START_METHODS
432 HANDLE_START_METHODS (methods_count);
434 for (i = 0; i < methods_count; i++)
436 int code = jcf_parse_one_method (jcf);
440 #ifdef HANDLE_END_METHODS
441 HANDLE_END_METHODS ();
446 /* Read attributes. */
448 DEFUN(jcf_parse_final_attributes, (jcf),
452 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
453 #ifdef START_FINAL_ATTRIBUTES
454 START_FINAL_ATTRIBUTES (attributes_count)
456 for (i = 0; i < attributes_count; i++)
458 int code = get_attribute (jcf);