OSDN Git Service

2003-01-02 Steven Bosscher <s.bosscher@student.tudelft.nl>
[pf3gnuchains/gcc-fork.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    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
5    Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
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.
13
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.
18
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.  
23
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.  */
27
28 #include "jcf.h"
29 #include "zipfile.h"
30
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));
41 #endif
42 #ifdef NEED_SKIP_ATTRIBUTE
43 static void skip_attribute PARAMS ((JCF *, int));
44 #endif
45
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.  */
50
51 #ifdef NEED_PEEK_ATTRIBUTE      /* Not everyone uses this function */
52 static int
53 peek_attribute (jcf, attribute_number, peeked_name, peeked_name_length)
54       JCF *jcf;
55       int attribute_number;
56       const char *peeked_name;
57       int peeked_name_length;
58 {
59   int to_return = 0;
60   long absolute_offset = (long)JCF_TELL (jcf);
61   int i;
62
63   for (i = 0; !to_return && i < attribute_number; i++)
64     {
65       uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
66       uint32 attribute_length = JCF_readu4 (jcf);
67       int name_length;
68       const unsigned char *name_data; 
69
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)
73         continue;
74
75       name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
76       name_data = JPOOL_UTF_DATA (jcf, attribute_name);
77
78       if (name_length == peeked_name_length 
79           && ! memcmp (name_data, peeked_name, peeked_name_length)) 
80         {
81           to_return = 1; 
82           break;
83         }
84       
85       JCF_SKIP (jcf, attribute_length);
86     }
87
88   JCF_SEEK (jcf, absolute_offset);
89   return to_return;
90 }
91 #endif
92
93 #ifdef NEED_SKIP_ATTRIBUTE      /* Not everyone uses this function */
94 static void
95 skip_attribute (jcf, number_of_attribute)
96      JCF *jcf;
97      int number_of_attribute;
98 {
99   while (number_of_attribute--)
100     {
101       JCF_u4 N;
102       JCF_FILL (jcf, 6);
103       (void) JCF_readu2 (jcf);
104       N = JCF_readu4 (jcf);
105       JCF_SKIP (jcf, N);
106     }
107 }
108 #endif
109
110 static int
111 DEFUN(get_attribute, (jcf),
112       JCF *jcf)
113 {
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);
117   int name_length;
118   const unsigned char *name_data;
119   JCF_FILL (jcf, (long) attribute_length);
120   if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
121     return -2;
122   if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
123     return -2;
124   name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
125   name_data = JPOOL_UTF_DATA (jcf, attribute_name);
126
127 #define MATCH_ATTRIBUTE(S) \
128   (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
129
130 #ifdef IGNORE_ATTRIBUTE
131    if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
132      {
133        JCF_SKIP (jcf, attribute_length);
134      }
135    else
136 #endif
137 #ifdef HANDLE_SOURCEFILE
138   if (MATCH_ATTRIBUTE ("SourceFile"))
139     {
140       uint16 sourcefile_index = JCF_readu2 (jcf);
141       HANDLE_SOURCEFILE(sourcefile_index);
142     }
143   else
144 #endif
145 #ifdef HANDLE_CONSTANTVALUE
146   if (MATCH_ATTRIBUTE ("ConstantValue"))
147     {
148       uint16 constantvalue_index = JCF_readu2 (jcf);
149       if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
150         return -2;
151       HANDLE_CONSTANTVALUE(constantvalue_index);
152     }
153   else
154 #endif
155 #ifdef HANDLE_CODE_ATTRIBUTE
156   if (MATCH_ATTRIBUTE ("Code"))
157     {
158       uint16 j;
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)
164         return -1;
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)
169         return -1;
170 #ifdef HANDLE_EXCEPTION_TABLE
171       HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
172 #endif
173       JCF_SKIP (jcf, 2 * 4 * exception_table_length);
174       attributes_count = JCF_readu2 (jcf);
175       for (j = 0; j < attributes_count; j++)
176         {
177           int code = get_attribute (jcf);
178           if (code != 0)
179             return code;
180         }
181     }
182   else
183 #endif /* HANDLE_CODE_ATTRIBUTE */
184 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
185   if (MATCH_ATTRIBUTE ("Exceptions"))
186     {
187       uint16 count = JCF_readu2 (jcf);
188       HANDLE_EXCEPTIONS_ATTRIBUTE (count);
189     }
190   else
191 #endif
192 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
193   if (MATCH_ATTRIBUTE ("LineNumberTable"))
194     {
195       uint16 count = JCF_readu2 (jcf);
196       HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
197     }
198   else
199 #endif
200 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
201   if (MATCH_ATTRIBUTE ("LocalVariableTable"))
202     {
203       uint16 count = JCF_readu2 (jcf);
204       HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
205     }
206   else
207 #endif
208 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
209   if (MATCH_ATTRIBUTE ("InnerClasses"))
210     {
211       uint16 count = JCF_readu2 (jcf);
212       HANDLE_INNERCLASSES_ATTRIBUTE (count);
213     }
214   else
215 #endif
216 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
217   if (MATCH_ATTRIBUTE ("Synthetic"))
218     {
219       HANDLE_SYNTHETIC_ATTRIBUTE ();
220     }
221   else
222 #endif
223 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
224   if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
225     {
226       HANDLE_GCJCOMPILED_ATTRIBUTE ();
227     }
228   else
229 #endif
230     {
231 #ifdef PROCESS_OTHER_ATTRIBUTE
232       PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
233 #else
234       JCF_SKIP (jcf, attribute_length);
235 #endif
236     }
237   if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
238     return -1;
239   return 0;
240 }
241
242 /* Read and handle the pre-amble. */
243 static int
244 DEFUN(jcf_parse_preamble, (jcf),
245       JCF* jcf)
246 {
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);
250 #ifdef HANDLE_MAGIC
251   HANDLE_MAGIC (magic, minor_version, major_version);
252 #endif
253   if (magic != 0xcafebabe)
254     return -1;
255   else
256     return 0;
257 }
258
259 /* Read and handle the constant pool.
260
261    Return 0 if OK.
262    Return -2 if a bad cross-reference (index of other constant) was seen.
263 */
264 static int
265 DEFUN(jcf_parse_constant_pool, (jcf),
266       JCF* jcf)
267 {
268   int i, n;
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));
275 #endif
276   for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
277     {
278       int constant_kind;
279        
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). */
284       JCF_FILL (jcf, 9);
285       constant_kind = JCF_readu (jcf);
286       jcf->cpool.tags[i] = constant_kind;
287       switch (constant_kind)
288         {
289         case CONSTANT_String:
290         case CONSTANT_Class:
291           jcf->cpool.data[i] = JCF_readu2 (jcf);
292           break;
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;
299           break;
300         case CONSTANT_Integer:
301         case CONSTANT_Float:
302           jcf->cpool.data[i] = JCF_readu4 (jcf);
303           break;
304         case CONSTANT_Long:
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);
310           break;
311         case CONSTANT_Utf8:
312           n = JCF_readu2 (jcf);
313           JCF_FILL (jcf, n);
314 #ifdef HANDLE_CONSTANT_Utf8
315           HANDLE_CONSTANT_Utf8(jcf, i, n);
316 #else
317           jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
318           JCF_SKIP (jcf, n);
319 #endif
320           break;
321         default:
322           return i;
323         }
324     }
325   return 0;
326 }
327
328 /* Read various class flags and numbers. */
329
330 static void
331 DEFUN(jcf_parse_class, (jcf),
332       JCF* jcf)
333 {
334   int i;
335   uint16 interfaces_count;
336   JCF_FILL (jcf, 8);
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);
341
342 #ifdef HANDLE_CLASS_INFO
343   HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
344 #endif
345
346   JCF_FILL (jcf, 2 * interfaces_count);
347
348   /* Read interfaces. */
349   for (i = 0; i < interfaces_count; i++)
350     {
351       uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
352 #ifdef HANDLE_CLASS_INTERFACE
353       HANDLE_CLASS_INTERFACE (index);
354 #endif
355     }
356 }
357
358 /* Read fields. */
359 static int
360 DEFUN(jcf_parse_fields, (jcf),
361       JCF* jcf)
362 {
363   int i, j;
364   uint16 fields_count;
365   JCF_FILL (jcf, 2);
366   fields_count = JCF_readu2 (jcf);
367
368 #ifdef HANDLE_START_FIELDS
369   HANDLE_START_FIELDS (fields_count);
370 #endif
371   for (i = 0; i < fields_count; i++)
372     {
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,
379                           attribute_count);
380 #endif
381       for (j = 0; j < attribute_count; j++)
382         {
383           int code = get_attribute (jcf);
384           if (code != 0)
385             return code;
386         }
387 #ifdef HANDLE_END_FIELD
388       HANDLE_END_FIELD ();
389 #endif
390     }
391 #ifdef HANDLE_END_FIELDS
392   HANDLE_END_FIELDS ();
393 #endif
394   return 0;
395 }
396
397 /* Read methods. */
398
399 static int
400 DEFUN(jcf_parse_one_method, (jcf),
401       JCF* jcf)
402 {
403   int i;
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);
408 #ifdef HANDLE_METHOD
409   HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
410 #endif
411   for (i = 0; i < attribute_count; i++)
412     {
413       int code = get_attribute (jcf);
414       if (code != 0)
415         return code;
416     }
417 #ifdef HANDLE_END_METHOD
418   HANDLE_END_METHOD ();
419 #endif
420   return 0;
421 }
422
423 static int
424 DEFUN(jcf_parse_methods, (jcf),
425       JCF* jcf)
426 {
427   int i;
428   uint16 methods_count;
429   JCF_FILL (jcf, 2);
430   methods_count = JCF_readu2 (jcf);
431 #ifdef HANDLE_START_METHODS
432   HANDLE_START_METHODS (methods_count);
433 #endif
434   for (i = 0; i < methods_count; i++)
435     {
436       int code = jcf_parse_one_method (jcf);
437       if (code != 0)
438         return code;
439     }
440 #ifdef HANDLE_END_METHODS
441   HANDLE_END_METHODS ();
442 #endif
443   return 0;
444 }
445
446 /* Read attributes. */
447 static int
448 DEFUN(jcf_parse_final_attributes, (jcf),
449       JCF *jcf)
450 {
451   int i;
452   uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
453 #ifdef START_FINAL_ATTRIBUTES
454   START_FINAL_ATTRIBUTES (attributes_count)
455 #endif
456   for (i = 0; i < attributes_count; i++)
457     {
458       int code = get_attribute (jcf);
459       if (code != 0)
460         return code;
461     }
462   return 0;
463 }
464