OSDN Git Service

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