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.
5 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
29 static int get_attribute
PARAMS ((JCF
*));
30 static int jcf_parse_preamble
PARAMS ((JCF
*));
31 static int jcf_parse_constant_pool
PARAMS ((JCF
*));
32 static void jcf_parse_class
PARAMS ((JCF
*));
33 static int jcf_parse_fields
PARAMS ((JCF
*));
34 static int jcf_parse_one_method
PARAMS ((JCF
*));
35 static int jcf_parse_methods
PARAMS ((JCF
*));
36 static int jcf_parse_final_attributes
PARAMS ((JCF
*));
37 #ifdef NEED_PEEK_ATTRIBUTE
38 static int peek_attribute
PARAMS ((JCF
*, int, const char *, int));
40 #ifdef NEED_SKIP_ATTRIBUTE
41 static void skip_attribute
PARAMS ((JCF
*, int));
44 /* Go through all available attribute (ATTRIBUTE_NUMER) and try to
45 identify PEEKED_NAME. Return 1 if PEEKED_NAME was found, 0
46 otherwise. JCF is restored to its initial position before
49 #ifdef NEED_PEEK_ATTRIBUTE /* Not everyone uses this function */
51 peek_attribute (jcf
, attribute_number
, peeked_name
, peeked_name_length
)
54 const char *peeked_name
;
55 int peeked_name_length
;
58 long absolute_offset
= (long)JCF_TELL (jcf
);
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
);
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
)
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
))
83 JCF_SKIP (jcf
, attribute_length
);
86 JCF_SEEK (jcf
, absolute_offset
);
91 #ifdef NEED_SKIP_ATTRIBUTE /* Not everyone uses this function */
93 skip_attribute (jcf
, number_of_attribute
)
95 int number_of_attribute
;
97 while (number_of_attribute
--)
100 (void) JCF_readu2 (jcf
);
101 JCF_SKIP (jcf
, JCF_readu4 (jcf
));
107 DEFUN(get_attribute
, (jcf
),
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
);
114 const unsigned char *name_data
;
115 JCF_FILL (jcf
, (long) attribute_length
);
116 if (attribute_name
<= 0 || attribute_name
>= JPOOL_SIZE(jcf
))
118 if (JPOOL_TAG (jcf
, attribute_name
) != CONSTANT_Utf8
)
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
);
133 #ifdef HANDLE_SOURCEFILE
134 if (MATCH_ATTRIBUTE ("SourceFile"))
136 uint16 sourcefile_index
= JCF_readu2 (jcf
);
137 HANDLE_SOURCEFILE(sourcefile_index
);
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
))
147 HANDLE_CONSTANTVALUE(constantvalue_index
);
151 #ifdef HANDLE_CODE_ATTRIBUTE
152 if (MATCH_ATTRIBUTE ("Code"))
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
)
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
)
166 #ifdef HANDLE_EXCEPTION_TABLE
167 HANDLE_EXCEPTION_TABLE (jcf
->read_ptr
, exception_table_length
);
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
);
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
);
188 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
189 if (MATCH_ATTRIBUTE ("LineNumberTable"))
191 uint16 count
= JCF_readu2 (jcf
);
192 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count
);
196 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
197 if (MATCH_ATTRIBUTE ("LocalVariableTable"))
199 uint16 count
= JCF_readu2 (jcf
);
200 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count
);
204 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
205 if (MATCH_ATTRIBUTE ("InnerClasses"))
207 uint16 count
= JCF_readu2 (jcf
);
208 HANDLE_INNERCLASSES_ATTRIBUTE (count
);
212 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
213 if (MATCH_ATTRIBUTE ("Synthetic"))
215 HANDLE_SYNTHETIC_ATTRIBUTE ();
219 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
220 if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
222 HANDLE_GCJCOMPILED_ATTRIBUTE ();
227 #ifdef PROCESS_OTHER_ATTRIBUTE
228 PROCESS_OTHER_ATTRIBUTE(jcf
, attribute_name
, attribute_length
);
230 JCF_SKIP (jcf
, attribute_length
);
233 if ((long) (start_pos
+ attribute_length
) != JCF_TELL(jcf
))
238 /* Read and handle the pre-amble. */
240 DEFUN(jcf_parse_preamble
, (jcf
),
243 uint32 magic
= (JCF_FILL (jcf
, 8), JCF_readu4 (jcf
));
244 uint16 minor_version ATTRIBUTE_UNUSED
= JCF_readu2 (jcf
);
245 uint16 major_version ATTRIBUTE_UNUSED
= JCF_readu2 (jcf
);
247 HANDLE_MAGIC (magic
, minor_version
, major_version
);
249 if (magic
!= 0xcafebabe)
255 /* Read and handle the constant pool.
258 Return -2 if a bad cross-reference (index of other constant) was seen.
261 DEFUN(jcf_parse_constant_pool
, (jcf
),
265 JPOOL_SIZE (jcf
) = (JCF_FILL (jcf
, 2), JCF_readu2 (jcf
));
266 jcf
->cpool
.tags
= ALLOC (JPOOL_SIZE (jcf
));
267 jcf
->cpool
.data
= ALLOC (sizeof (jword
) * JPOOL_SIZE (jcf
));
268 jcf
->cpool
.tags
[0] = 0;
269 #ifdef HANDLE_START_CONSTANT_POOL
270 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf
));
272 for (i
= 1; i
< (int) JPOOL_SIZE (jcf
); i
++)
276 /* Make sure at least 9 bytes are available. This is enough
277 for all fixed-sized constant pool entries (so we don't need many
278 more JCF_FILL calls below), but is is small enough that
279 we are guaranteed to not hit EOF (in a valid .class file). */
281 constant_kind
= JCF_readu (jcf
);
282 jcf
->cpool
.tags
[i
] = constant_kind
;
283 switch (constant_kind
)
285 case CONSTANT_String
:
287 jcf
->cpool
.data
[i
] = JCF_readu2 (jcf
);
289 case CONSTANT_Fieldref
:
290 case CONSTANT_Methodref
:
291 case CONSTANT_InterfaceMethodref
:
292 case CONSTANT_NameAndType
:
293 jcf
->cpool
.data
[i
] = JCF_readu2 (jcf
);
294 jcf
->cpool
.data
[i
] |= JCF_readu2 (jcf
) << 16;
296 case CONSTANT_Integer
:
298 jcf
->cpool
.data
[i
] = JCF_readu4 (jcf
);
301 case CONSTANT_Double
:
302 jcf
->cpool
.data
[i
] = JCF_readu4 (jcf
);
303 i
++; /* These take up two spots in the constant pool */
304 jcf
->cpool
.tags
[i
] = 0;
305 jcf
->cpool
.data
[i
] = JCF_readu4 (jcf
);
308 n
= JCF_readu2 (jcf
);
310 #ifdef HANDLE_CONSTANT_Utf8
311 HANDLE_CONSTANT_Utf8(jcf
, i
, n
);
313 jcf
->cpool
.data
[i
] = JCF_TELL(jcf
) - 2;
324 /* Read various class flags and numbers. */
327 DEFUN(jcf_parse_class
, (jcf
),
331 uint16 interfaces_count
;
333 jcf
->access_flags
= JCF_readu2 (jcf
);
334 jcf
->this_class
= JCF_readu2 (jcf
);
335 jcf
->super_class
= JCF_readu2 (jcf
);
336 interfaces_count
= JCF_readu2 (jcf
);
338 #ifdef HANDLE_CLASS_INFO
339 HANDLE_CLASS_INFO(jcf
->access_flags
, jcf
->this_class
, jcf
->super_class
, interfaces_count
);
342 JCF_FILL (jcf
, 2 * interfaces_count
);
344 /* Read interfaces. */
345 for (i
= 0; i
< interfaces_count
; i
++)
347 uint16 index ATTRIBUTE_UNUSED
= JCF_readu2 (jcf
);
348 #ifdef HANDLE_CLASS_INTERFACE
349 HANDLE_CLASS_INTERFACE (index
);
356 DEFUN(jcf_parse_fields
, (jcf
),
362 fields_count
= JCF_readu2 (jcf
);
364 #ifdef HANDLE_START_FIELDS
365 HANDLE_START_FIELDS (fields_count
);
367 for (i
= 0; i
< fields_count
; i
++)
369 uint16 access_flags
= (JCF_FILL (jcf
, 8), JCF_readu2 (jcf
));
370 uint16 name_index
= JCF_readu2 (jcf
);
371 uint16 signature_index
= JCF_readu2 (jcf
);
372 uint16 attribute_count
= JCF_readu2 (jcf
);
373 #ifdef HANDLE_START_FIELD
374 HANDLE_START_FIELD (access_flags
, name_index
, signature_index
,
377 for (j
= 0; j
< attribute_count
; j
++)
379 int code
= get_attribute (jcf
);
383 #ifdef HANDLE_END_FIELD
387 #ifdef HANDLE_END_FIELDS
388 HANDLE_END_FIELDS ();
396 DEFUN(jcf_parse_one_method
, (jcf
),
400 uint16 access_flags
= (JCF_FILL (jcf
, 8), JCF_readu2 (jcf
));
401 uint16 name_index
= JCF_readu2 (jcf
);
402 uint16 signature_index
= JCF_readu2 (jcf
);
403 uint16 attribute_count
= JCF_readu2 (jcf
);
405 HANDLE_METHOD(access_flags
, name_index
, signature_index
, attribute_count
);
407 for (i
= 0; i
< attribute_count
; i
++)
409 int code
= get_attribute (jcf
);
413 #ifdef HANDLE_END_METHOD
414 HANDLE_END_METHOD ();
420 DEFUN(jcf_parse_methods
, (jcf
),
424 uint16 methods_count
;
426 methods_count
= JCF_readu2 (jcf
);
427 #ifdef HANDLE_START_METHODS
428 HANDLE_START_METHODS (methods_count
);
430 for (i
= 0; i
< methods_count
; i
++)
432 int code
= jcf_parse_one_method (jcf
);
436 #ifdef HANDLE_END_METHODS
437 HANDLE_END_METHODS ();
442 /* Read attributes. */
444 DEFUN(jcf_parse_final_attributes
, (jcf
),
448 uint16 attributes_count
= (JCF_FILL (jcf
, 2), JCF_readu2 (jcf
));
449 #ifdef START_FINAL_ATTRIBUTES
450 START_FINAL_ATTRIBUTES (attributes_count
)
452 for (i
= 0; i
< attributes_count
; i
++)
454 int code
= get_attribute (jcf
);