gcc/java/
[official-gcc.git] / gcc / java / jcf-reader.c
blob219cf657be161a032e2b427d78f7ad581a299189
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
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)
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 COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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. */
28 #include "jcf.h"
29 #include "zipfile.h"
31 static int get_attribute (JCF *);
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 *);
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
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. */
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)
56 int to_return = 0;
57 long absolute_offset = (long)JCF_TELL (jcf);
58 int i;
60 for (i = 0; !to_return && i < attribute_number; i++)
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;
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;
72 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
73 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
75 if (name_length == peeked_name_length
76 && ! memcmp (name_data, peeked_name, peeked_name_length))
78 to_return = 1;
79 break;
82 JCF_SKIP (jcf, attribute_length);
85 JCF_SEEK (jcf, absolute_offset);
86 return to_return;
88 #endif
90 #ifdef NEED_SKIP_ATTRIBUTE /* Not everyone uses this function */
91 static void
92 skip_attribute (JCF *jcf, int number_of_attribute)
94 while (number_of_attribute--)
96 JCF_u4 N;
97 JCF_FILL (jcf, 6);
98 (void) JCF_readu2 (jcf);
99 N = JCF_readu4 (jcf);
100 JCF_SKIP (jcf, N);
103 #endif
105 static int
106 get_attribute (JCF *jcf)
108 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
109 uint32 attribute_length = JCF_readu4 (jcf);
110 uint32 start_pos = JCF_TELL(jcf);
111 int name_length;
112 const unsigned char *name_data;
113 JCF_FILL (jcf, (long) attribute_length);
114 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
115 return -2;
116 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
117 return -2;
118 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
119 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
121 #define MATCH_ATTRIBUTE(S) \
122 (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
124 #ifdef IGNORE_ATTRIBUTE
125 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
127 JCF_SKIP (jcf, attribute_length);
129 else
130 #endif
131 #ifdef HANDLE_SOURCEFILE
132 if (MATCH_ATTRIBUTE ("SourceFile"))
134 uint16 sourcefile_index = JCF_readu2 (jcf);
135 HANDLE_SOURCEFILE(sourcefile_index);
137 else
138 #endif
139 #ifdef HANDLE_CONSTANTVALUE
140 if (MATCH_ATTRIBUTE ("ConstantValue"))
142 uint16 constantvalue_index = JCF_readu2 (jcf);
143 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
144 return -2;
145 HANDLE_CONSTANTVALUE(constantvalue_index);
147 else
148 #endif
149 #ifdef HANDLE_CODE_ATTRIBUTE
150 if (MATCH_ATTRIBUTE ("Code"))
152 uint16 j;
153 uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
154 uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
155 uint32 code_length = JCF_readu4 (jcf);
156 uint16 exception_table_length, attributes_count;
157 if (code_length + 12 > attribute_length)
158 return -1;
159 HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
160 JCF_SKIP (jcf, code_length);
161 exception_table_length = JCF_readu2 (jcf);
162 if (code_length + 8 * exception_table_length + 12 > attribute_length)
163 return -1;
164 #ifdef HANDLE_EXCEPTION_TABLE
165 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
166 #endif
167 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
168 attributes_count = JCF_readu2 (jcf);
169 for (j = 0; j < attributes_count; j++)
171 int code = get_attribute (jcf);
172 if (code != 0)
173 return code;
176 else
177 #endif /* HANDLE_CODE_ATTRIBUTE */
178 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
179 if (MATCH_ATTRIBUTE ("Exceptions"))
181 uint16 count = JCF_readu2 (jcf);
182 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
184 else
185 #endif
186 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
187 if (MATCH_ATTRIBUTE ("LineNumberTable"))
189 uint16 count = JCF_readu2 (jcf);
190 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
192 else
193 #endif
194 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
195 if (MATCH_ATTRIBUTE ("LocalVariableTable"))
197 uint16 count = JCF_readu2 (jcf);
198 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
200 else
201 #endif
202 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
203 if (MATCH_ATTRIBUTE ("InnerClasses"))
205 uint16 count = JCF_readu2 (jcf);
206 HANDLE_INNERCLASSES_ATTRIBUTE (count);
208 else
209 #endif
210 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
211 if (MATCH_ATTRIBUTE ("Synthetic"))
213 HANDLE_SYNTHETIC_ATTRIBUTE ();
215 else
216 #endif
217 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
218 if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
220 HANDLE_GCJCOMPILED_ATTRIBUTE ();
222 else
223 #endif
224 #ifdef HANDLE_DEPRECATED_ATTRIBUTE
225 if (MATCH_ATTRIBUTE ("Deprecated"))
227 HANDLE_DEPRECATED_ATTRIBUTE ();
229 else
230 #endif
231 #ifdef HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE
232 if (MATCH_ATTRIBUTE ("SourceDebugExtension")) /* JSR 45 */
234 HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE (attribute_length);
236 else
237 #endif
239 #ifdef PROCESS_OTHER_ATTRIBUTE
240 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
241 #else
242 JCF_SKIP (jcf, attribute_length);
243 #endif
245 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
246 return -1;
247 return 0;
250 /* Read and handle the pre-amble. */
251 static int
252 jcf_parse_preamble (JCF* jcf)
254 uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
255 uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
256 uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
257 #ifdef HANDLE_MAGIC
258 HANDLE_MAGIC (magic, minor_version, major_version);
259 #endif
260 if (magic != 0xcafebabe)
261 return -1;
262 else
263 return 0;
266 /* Read and handle the constant pool.
268 Return 0 if OK.
269 Return -2 if a bad cross-reference (index of other constant) was seen.
271 static int
272 jcf_parse_constant_pool (JCF* jcf)
274 int i, n;
275 JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
276 jcf->cpool.tags = ggc_alloc (JPOOL_SIZE (jcf));
277 jcf->cpool.data = ggc_alloc (sizeof (jword) * JPOOL_SIZE (jcf));
278 jcf->cpool.tags[0] = 0;
279 #ifdef HANDLE_START_CONSTANT_POOL
280 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
281 #endif
282 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
284 int constant_kind;
286 /* Make sure at least 9 bytes are available. This is enough
287 for all fixed-sized constant pool entries (so we don't need many
288 more JCF_FILL calls below), but is is small enough that
289 we are guaranteed to not hit EOF (in a valid .class file). */
290 JCF_FILL (jcf, 9);
291 constant_kind = JCF_readu (jcf);
292 jcf->cpool.tags[i] = constant_kind;
293 switch (constant_kind)
295 case CONSTANT_String:
296 case CONSTANT_Class:
297 jcf->cpool.data[i].w = JCF_readu2 (jcf);
298 break;
299 case CONSTANT_Fieldref:
300 case CONSTANT_Methodref:
301 case CONSTANT_InterfaceMethodref:
302 case CONSTANT_NameAndType:
303 jcf->cpool.data[i].w = JCF_readu2 (jcf);
304 jcf->cpool.data[i].w |= JCF_readu2 (jcf) << 16;
305 break;
306 case CONSTANT_Integer:
307 case CONSTANT_Float:
308 jcf->cpool.data[i].w = JCF_readu4 (jcf);
309 break;
310 case CONSTANT_Long:
311 case CONSTANT_Double:
312 jcf->cpool.data[i].w = JCF_readu4 (jcf);
313 i++; /* These take up two spots in the constant pool */
314 jcf->cpool.tags[i] = 0;
315 jcf->cpool.data[i].w = JCF_readu4 (jcf);
316 break;
317 case CONSTANT_Utf8:
318 n = JCF_readu2 (jcf);
319 JCF_FILL (jcf, n);
320 #ifdef HANDLE_CONSTANT_Utf8
321 HANDLE_CONSTANT_Utf8(jcf, i, n);
322 #else
323 jcf->cpool.data[i].w = JCF_TELL(jcf) - 2;
324 JCF_SKIP (jcf, n);
325 #endif
326 break;
327 default:
328 return i;
331 return 0;
334 /* Read various class flags and numbers. */
336 static void
337 jcf_parse_class (JCF* jcf)
339 int i;
340 uint16 interfaces_count;
341 JCF_FILL (jcf, 8);
342 jcf->access_flags = JCF_readu2 (jcf);
343 jcf->this_class = JCF_readu2 (jcf);
344 jcf->super_class = JCF_readu2 (jcf);
345 interfaces_count = JCF_readu2 (jcf);
347 #ifdef HANDLE_CLASS_INFO
348 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
349 #endif
351 JCF_FILL (jcf, 2 * interfaces_count);
353 /* Read interfaces. */
354 for (i = 0; i < interfaces_count; i++)
356 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
357 #ifdef HANDLE_CLASS_INTERFACE
358 HANDLE_CLASS_INTERFACE (index);
359 #endif
363 /* Read fields. */
364 static int
365 jcf_parse_fields (JCF* jcf)
367 int i, j;
368 uint16 fields_count;
369 JCF_FILL (jcf, 2);
370 fields_count = JCF_readu2 (jcf);
372 #ifdef HANDLE_START_FIELDS
373 HANDLE_START_FIELDS (fields_count);
374 #endif
375 for (i = 0; i < fields_count; i++)
377 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
378 uint16 name_index = JCF_readu2 (jcf);
379 uint16 signature_index = JCF_readu2 (jcf);
380 uint16 attribute_count = JCF_readu2 (jcf);
381 #ifdef HANDLE_START_FIELD
382 HANDLE_START_FIELD (access_flags, name_index, signature_index,
383 attribute_count);
384 #endif
385 for (j = 0; j < attribute_count; j++)
387 int code = get_attribute (jcf);
388 if (code != 0)
389 return code;
391 #ifdef HANDLE_END_FIELD
392 HANDLE_END_FIELD ();
393 #endif
395 #ifdef HANDLE_END_FIELDS
396 HANDLE_END_FIELDS ();
397 #endif
398 return 0;
401 /* Read methods. */
403 static int
404 jcf_parse_one_method (JCF* jcf)
406 int i;
407 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
408 uint16 name_index = JCF_readu2 (jcf);
409 uint16 signature_index = JCF_readu2 (jcf);
410 uint16 attribute_count = JCF_readu2 (jcf);
411 #ifdef HANDLE_METHOD
412 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
413 #endif
414 for (i = 0; i < attribute_count; i++)
416 int code = get_attribute (jcf);
417 if (code != 0)
418 return code;
420 #ifdef HANDLE_END_METHOD
421 HANDLE_END_METHOD ();
422 #endif
423 return 0;
426 static int
427 jcf_parse_methods (JCF* jcf)
429 int i;
430 uint16 methods_count;
431 JCF_FILL (jcf, 2);
432 methods_count = JCF_readu2 (jcf);
433 #ifdef HANDLE_START_METHODS
434 HANDLE_START_METHODS (methods_count);
435 #endif
436 for (i = 0; i < methods_count; i++)
438 int code = jcf_parse_one_method (jcf);
439 if (code != 0)
440 return code;
442 #ifdef HANDLE_END_METHODS
443 HANDLE_END_METHODS ();
444 #endif
445 return 0;
448 /* Read attributes. */
449 static int
450 jcf_parse_final_attributes (JCF *jcf)
452 int i;
453 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
454 #ifdef START_FINAL_ATTRIBUTES
455 START_FINAL_ATTRIBUTES (attributes_count)
456 #endif
457 for (i = 0; i < attributes_count; i++)
459 int code = get_attribute (jcf);
460 if (code != 0)
461 return code;
463 return 0;