96e3463ac2537727b759a6a988a4caeac1973a61
[gcc.git] / gcc / java / jcf-reader.c
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
5 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
6
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)
10 any later version.
11
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.
16
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.
21
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. */
25
26 #include "jcf.h"
27 #include "zipfile.h"
28
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));
39 #endif
40 #ifdef NEED_SKIP_ATTRIBUTE
41 static void skip_attribute PARAMS ((JCF *, int));
42 #endif
43
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
47 returning. */
48
49 #ifdef NEED_PEEK_ATTRIBUTE /* Not everyone uses this function */
50 static int
51 peek_attribute (jcf, attribute_number, peeked_name, peeked_name_length)
52 JCF *jcf;
53 int attribute_number;
54 const char *peeked_name;
55 int peeked_name_length;
56 {
57 int to_return = 0;
58 long absolute_offset = (long)JCF_TELL (jcf);
59 int i;
60
61 for (i = 0; !to_return && i < attribute_number; i++)
62 {
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;
67
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;
72
73 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
74 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
75
76 if (name_length == peeked_name_length
77 && ! memcmp (name_data, peeked_name, peeked_name_length))
78 {
79 to_return = 1;
80 break;
81 }
82
83 JCF_SKIP (jcf, attribute_length);
84 }
85
86 JCF_SEEK (jcf, absolute_offset);
87 return to_return;
88 }
89 #endif
90
91 #ifdef NEED_SKIP_ATTRIBUTE /* Not everyone uses this function */
92 static void
93 skip_attribute (jcf, number_of_attribute)
94 JCF *jcf;
95 int number_of_attribute;
96 {
97 while (number_of_attribute--)
98 {
99 JCF_u4 N;
100 JCF_FILL (jcf, 6);
101 (void) JCF_readu2 (jcf);
102 N = JCF_readu4 (jcf);
103 JCF_SKIP (jcf, N);
104 }
105 }
106 #endif
107
108 static int
109 DEFUN(get_attribute, (jcf),
110 JCF *jcf)
111 {
112 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
113 uint32 attribute_length = JCF_readu4 (jcf);
114 uint32 start_pos = JCF_TELL(jcf);
115 int name_length;
116 const unsigned char *name_data;
117 JCF_FILL (jcf, (long) attribute_length);
118 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
119 return -2;
120 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
121 return -2;
122 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
123 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
124
125 #define MATCH_ATTRIBUTE(S) \
126 (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
127
128 #ifdef IGNORE_ATTRIBUTE
129 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
130 {
131 JCF_SKIP (jcf, attribute_length);
132 }
133 else
134 #endif
135 #ifdef HANDLE_SOURCEFILE
136 if (MATCH_ATTRIBUTE ("SourceFile"))
137 {
138 uint16 sourcefile_index = JCF_readu2 (jcf);
139 HANDLE_SOURCEFILE(sourcefile_index);
140 }
141 else
142 #endif
143 #ifdef HANDLE_CONSTANTVALUE
144 if (MATCH_ATTRIBUTE ("ConstantValue"))
145 {
146 uint16 constantvalue_index = JCF_readu2 (jcf);
147 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
148 return -2;
149 HANDLE_CONSTANTVALUE(constantvalue_index);
150 }
151 else
152 #endif
153 #ifdef HANDLE_CODE_ATTRIBUTE
154 if (MATCH_ATTRIBUTE ("Code"))
155 {
156 uint16 j;
157 uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
158 uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
159 uint32 code_length = JCF_readu4 (jcf);
160 uint16 exception_table_length, attributes_count;
161 if (code_length + 12 > attribute_length)
162 return -1;
163 HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
164 JCF_SKIP (jcf, code_length);
165 exception_table_length = JCF_readu2 (jcf);
166 if (code_length + 8 * exception_table_length + 12 > attribute_length)
167 return -1;
168 #ifdef HANDLE_EXCEPTION_TABLE
169 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
170 #endif
171 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
172 attributes_count = JCF_readu2 (jcf);
173 for (j = 0; j < attributes_count; j++)
174 {
175 int code = get_attribute (jcf);
176 if (code != 0)
177 return code;
178 }
179 }
180 else
181 #endif /* HANDLE_CODE_ATTRIBUTE */
182 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
183 if (MATCH_ATTRIBUTE ("Exceptions"))
184 {
185 uint16 count = JCF_readu2 (jcf);
186 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
187 }
188 else
189 #endif
190 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
191 if (MATCH_ATTRIBUTE ("LineNumberTable"))
192 {
193 uint16 count = JCF_readu2 (jcf);
194 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
195 }
196 else
197 #endif
198 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
199 if (MATCH_ATTRIBUTE ("LocalVariableTable"))
200 {
201 uint16 count = JCF_readu2 (jcf);
202 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
203 }
204 else
205 #endif
206 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
207 if (MATCH_ATTRIBUTE ("InnerClasses"))
208 {
209 uint16 count = JCF_readu2 (jcf);
210 HANDLE_INNERCLASSES_ATTRIBUTE (count);
211 }
212 else
213 #endif
214 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
215 if (MATCH_ATTRIBUTE ("Synthetic"))
216 {
217 HANDLE_SYNTHETIC_ATTRIBUTE ();
218 }
219 else
220 #endif
221 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
222 if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
223 {
224 HANDLE_GCJCOMPILED_ATTRIBUTE ();
225 }
226 else
227 #endif
228 {
229 #ifdef PROCESS_OTHER_ATTRIBUTE
230 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
231 #else
232 JCF_SKIP (jcf, attribute_length);
233 #endif
234 }
235 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
236 return -1;
237 return 0;
238 }
239
240 /* Read and handle the pre-amble. */
241 static int
242 DEFUN(jcf_parse_preamble, (jcf),
243 JCF* jcf)
244 {
245 uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
246 uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
247 uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
248 #ifdef HANDLE_MAGIC
249 HANDLE_MAGIC (magic, minor_version, major_version);
250 #endif
251 if (magic != 0xcafebabe)
252 return -1;
253 else
254 return 0;
255 }
256
257 /* Read and handle the constant pool.
258
259 Return 0 if OK.
260 Return -2 if a bad cross-reference (index of other constant) was seen.
261 */
262 static int
263 DEFUN(jcf_parse_constant_pool, (jcf),
264 JCF* jcf)
265 {
266 int i, n;
267 JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
268 jcf->cpool.tags = ALLOC (JPOOL_SIZE (jcf));
269 jcf->cpool.data = ALLOC (sizeof (jword) * JPOOL_SIZE (jcf));
270 jcf->cpool.tags[0] = 0;
271 #ifdef HANDLE_START_CONSTANT_POOL
272 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
273 #endif
274 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
275 {
276 int constant_kind;
277
278 /* Make sure at least 9 bytes are available. This is enough
279 for all fixed-sized constant pool entries (so we don't need many
280 more JCF_FILL calls below), but is is small enough that
281 we are guaranteed to not hit EOF (in a valid .class file). */
282 JCF_FILL (jcf, 9);
283 constant_kind = JCF_readu (jcf);
284 jcf->cpool.tags[i] = constant_kind;
285 switch (constant_kind)
286 {
287 case CONSTANT_String:
288 case CONSTANT_Class:
289 jcf->cpool.data[i] = JCF_readu2 (jcf);
290 break;
291 case CONSTANT_Fieldref:
292 case CONSTANT_Methodref:
293 case CONSTANT_InterfaceMethodref:
294 case CONSTANT_NameAndType:
295 jcf->cpool.data[i] = JCF_readu2 (jcf);
296 jcf->cpool.data[i] |= JCF_readu2 (jcf) << 16;
297 break;
298 case CONSTANT_Integer:
299 case CONSTANT_Float:
300 jcf->cpool.data[i] = JCF_readu4 (jcf);
301 break;
302 case CONSTANT_Long:
303 case CONSTANT_Double:
304 jcf->cpool.data[i] = JCF_readu4 (jcf);
305 i++; /* These take up two spots in the constant pool */
306 jcf->cpool.tags[i] = 0;
307 jcf->cpool.data[i] = JCF_readu4 (jcf);
308 break;
309 case CONSTANT_Utf8:
310 n = JCF_readu2 (jcf);
311 JCF_FILL (jcf, n);
312 #ifdef HANDLE_CONSTANT_Utf8
313 HANDLE_CONSTANT_Utf8(jcf, i, n);
314 #else
315 jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
316 JCF_SKIP (jcf, n);
317 #endif
318 break;
319 default:
320 return i;
321 }
322 }
323 return 0;
324 }
325
326 /* Read various class flags and numbers. */
327
328 static void
329 DEFUN(jcf_parse_class, (jcf),
330 JCF* jcf)
331 {
332 int i;
333 uint16 interfaces_count;
334 JCF_FILL (jcf, 8);
335 jcf->access_flags = JCF_readu2 (jcf);
336 jcf->this_class = JCF_readu2 (jcf);
337 jcf->super_class = JCF_readu2 (jcf);
338 interfaces_count = JCF_readu2 (jcf);
339
340 #ifdef HANDLE_CLASS_INFO
341 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
342 #endif
343
344 JCF_FILL (jcf, 2 * interfaces_count);
345
346 /* Read interfaces. */
347 for (i = 0; i < interfaces_count; i++)
348 {
349 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
350 #ifdef HANDLE_CLASS_INTERFACE
351 HANDLE_CLASS_INTERFACE (index);
352 #endif
353 }
354 }
355
356 /* Read fields. */
357 static int
358 DEFUN(jcf_parse_fields, (jcf),
359 JCF* jcf)
360 {
361 int i, j;
362 uint16 fields_count;
363 JCF_FILL (jcf, 2);
364 fields_count = JCF_readu2 (jcf);
365
366 #ifdef HANDLE_START_FIELDS
367 HANDLE_START_FIELDS (fields_count);
368 #endif
369 for (i = 0; i < fields_count; i++)
370 {
371 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
372 uint16 name_index = JCF_readu2 (jcf);
373 uint16 signature_index = JCF_readu2 (jcf);
374 uint16 attribute_count = JCF_readu2 (jcf);
375 #ifdef HANDLE_START_FIELD
376 HANDLE_START_FIELD (access_flags, name_index, signature_index,
377 attribute_count);
378 #endif
379 for (j = 0; j < attribute_count; j++)
380 {
381 int code = get_attribute (jcf);
382 if (code != 0)
383 return code;
384 }
385 #ifdef HANDLE_END_FIELD
386 HANDLE_END_FIELD ();
387 #endif
388 }
389 #ifdef HANDLE_END_FIELDS
390 HANDLE_END_FIELDS ();
391 #endif
392 return 0;
393 }
394
395 /* Read methods. */
396
397 static int
398 DEFUN(jcf_parse_one_method, (jcf),
399 JCF* jcf)
400 {
401 int i;
402 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
403 uint16 name_index = JCF_readu2 (jcf);
404 uint16 signature_index = JCF_readu2 (jcf);
405 uint16 attribute_count = JCF_readu2 (jcf);
406 #ifdef HANDLE_METHOD
407 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
408 #endif
409 for (i = 0; i < attribute_count; i++)
410 {
411 int code = get_attribute (jcf);
412 if (code != 0)
413 return code;
414 }
415 #ifdef HANDLE_END_METHOD
416 HANDLE_END_METHOD ();
417 #endif
418 return 0;
419 }
420
421 static int
422 DEFUN(jcf_parse_methods, (jcf),
423 JCF* jcf)
424 {
425 int i;
426 uint16 methods_count;
427 JCF_FILL (jcf, 2);
428 methods_count = JCF_readu2 (jcf);
429 #ifdef HANDLE_START_METHODS
430 HANDLE_START_METHODS (methods_count);
431 #endif
432 for (i = 0; i < methods_count; i++)
433 {
434 int code = jcf_parse_one_method (jcf);
435 if (code != 0)
436 return code;
437 }
438 #ifdef HANDLE_END_METHODS
439 HANDLE_END_METHODS ();
440 #endif
441 return 0;
442 }
443
444 /* Read attributes. */
445 static int
446 DEFUN(jcf_parse_final_attributes, (jcf),
447 JCF *jcf)
448 {
449 int i;
450 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
451 #ifdef START_FINAL_ATTRIBUTES
452 START_FINAL_ATTRIBUTES (attributes_count)
453 #endif
454 for (i = 0; i < attributes_count; i++)
455 {
456 int code = get_attribute (jcf);
457 if (code != 0)
458 return code;
459 }
460 return 0;
461 }
462