1 /* Utility routines for finding and reading Java(TM) .class files.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC 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 3, or (at your option)
12 GCC 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 GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
29 #include "coretypes.h"
35 #include "java-tree.h"
41 /* DOS brain-damage */
43 #define O_BINARY 0 /* MS-DOS brain-damage */
47 jcf_unexpected_eof (JCF
*jcf
, int count ATTRIBUTE_UNUSED
)
50 fprintf (stderr
, "Premature end of .class file %s.\n", jcf
->filename
);
52 fprintf (stderr
, "Premature end of .class file <stdin>.\n");
57 jcf_trim_old_input (JCF
*jcf
)
59 int count
= jcf
->read_ptr
- jcf
->buffer
;
62 memmove (jcf
->buffer
, jcf
->read_ptr
, jcf
->read_end
- jcf
->read_ptr
);
63 jcf
->read_ptr
-= count
;
64 jcf
->read_end
-= count
;
69 jcf_filbuf_from_stdio (JCF
*jcf
, int count
)
71 FILE *file
= (FILE*) (jcf
->read_state
);
72 if (count
> jcf
->buffer_end
- jcf
->read_ptr
)
74 JCF_u4 old_read_ptr
= jcf
->read_ptr
- jcf
->buffer
;
75 JCF_u4 old_read_end
= jcf
->read_end
- jcf
->buffer
;
76 JCF_u4 old_size
= jcf
->buffer_end
- jcf
->buffer
;
77 JCF_u4 new_size
= (old_size
== 0 ? 2000 : 2 * old_size
) + count
;
78 unsigned char *new_buffer
79 = jcf
->buffer
== NULL
? XNEWVAR (unsigned char, new_size
)
80 : XRESIZEVAR (unsigned char, jcf
->buffer
, new_size
);
81 jcf
->buffer
= new_buffer
;
82 jcf
->buffer_end
= new_buffer
+ new_size
;
83 jcf
->read_ptr
= new_buffer
+ old_read_ptr
;
84 jcf
->read_end
= new_buffer
+ old_read_end
;
86 count
-= jcf
->read_end
- jcf
->read_ptr
;
89 if ((int) fread (jcf
->read_end
, 1, count
, file
) != count
)
90 jcf_unexpected_eof (jcf
, count
);
91 jcf
->read_end
+= count
;
97 struct ZipFile
*SeenZipFiles
= NULL
;
99 /* Open a zip file with the given name, and cache directory and file
100 descriptor. If the file is missing, treat it as an empty archive.
101 Return NULL if the .zip file is malformed.
105 opendir_in_zip (const char *zipfile
, int is_system
)
107 struct ZipFile
* zipf
;
110 for (zipf
= SeenZipFiles
; zipf
!= NULL
; zipf
= zipf
->next
)
112 if (strcmp (zipf
->name
, zipfile
) == 0)
116 zipf
= XNEWVAR (struct ZipFile
, sizeof (struct ZipFile
) + strlen (zipfile
) + 1);
117 zipf
->next
= SeenZipFiles
;
118 zipf
->name
= (char*)(zipf
+1);
119 strcpy (zipf
->name
, zipfile
);
120 fd
= open (zipfile
, O_RDONLY
| O_BINARY
);
124 /* A missing zip file is not considered an error.
125 We may want to re-consider that. FIXME. */
128 zipf
->central_directory
= NULL
;
132 jcf_dependency_add_file (zipfile
, is_system
);
133 if (read (fd
, magic
, 4) != 4 || GET_u4 (magic
) != (JCF_u4
)ZIPMAGIC
)
139 lseek (fd
, 0L, SEEK_SET
);
140 if (read_zip_archive (zipf
) != 0)
153 0: OK - zipmember found.
155 -2: Malformed archive.
159 open_in_zip (JCF
*jcf
, const char *zipfile
, const char *zipmember
,
164 ZipFile
*zipf
= opendir_in_zip (zipfile
, is_system
);
172 len
= strlen (zipmember
);
174 zipd
= (struct ZipDirectory
*) zipf
->central_directory
;
175 for (i
= 0; i
< zipf
->count
; i
++, zipd
= ZIPDIR_NEXT (zipd
))
177 if (len
== zipd
->filename_length
&&
178 strncmp (ZIPDIR_FILENAME (zipd
), zipmember
, len
) == 0)
182 jcf
->filename
= xstrdup (zipfile
);
183 jcf
->classname
= xstrdup (zipmember
);
184 return read_zip_member(jcf
, zipd
, zipf
);
190 /* Read data from zip archive member. */
193 read_zip_member (JCF
*jcf
, ZipDirectory
*zipd
, ZipFile
*zipf
)
195 jcf
->filbuf
= jcf_unexpected_eof
;
198 if (zipd
->compression_method
== Z_NO_COMPRESSION
)
200 jcf
->buffer
= XNEWVEC (unsigned char, zipd
->size
);
201 jcf
->buffer_end
= jcf
->buffer
+ zipd
->size
;
202 jcf
->read_ptr
= jcf
->buffer
;
203 jcf
->read_end
= jcf
->buffer_end
;
204 if (lseek (zipf
->fd
, zipd
->filestart
, 0) < 0
205 || read (zipf
->fd
, jcf
->buffer
, zipd
->size
) != (long) zipd
->size
)
211 z_stream d_stream
; /* decompression stream */
212 d_stream
.zalloc
= (alloc_func
) 0;
213 d_stream
.zfree
= (free_func
) 0;
214 d_stream
.opaque
= (voidpf
) 0;
216 jcf
->buffer
= XNEWVEC (unsigned char, zipd
->uncompressed_size
);
217 d_stream
.next_out
= jcf
->buffer
;
218 d_stream
.avail_out
= zipd
->uncompressed_size
;
219 jcf
->buffer_end
= jcf
->buffer
+ zipd
->uncompressed_size
;
220 jcf
->read_ptr
= jcf
->buffer
;
221 jcf
->read_end
= jcf
->buffer_end
;
222 buffer
= XNEWVEC (char, zipd
->size
);
223 d_stream
.next_in
= (unsigned char *) buffer
;
224 d_stream
.avail_in
= zipd
->size
;
225 if (lseek (zipf
->fd
, zipd
->filestart
, 0) < 0
226 || read (zipf
->fd
, buffer
, zipd
->size
) != (long) zipd
->size
)
228 /* Handle NO_HEADER using undocumented zlib feature.
229 This is a very common hack. */
230 inflateInit2 (&d_stream
, -MAX_WBITS
);
231 inflate (&d_stream
, Z_NO_FLUSH
);
232 inflateEnd (&d_stream
);
240 open_class (const char *filename
, JCF
*jcf
, int fd
, const char *dep_name
)
244 struct stat stat_buf
;
245 if (fstat (fd
, &stat_buf
) != 0
246 || ! S_ISREG (stat_buf
.st_mode
))
248 perror ("Could not figure length of .class file");
251 if (dep_name
!= NULL
)
252 jcf_dependency_add_file (dep_name
, 0);
254 jcf
->buffer
= XNEWVEC (unsigned char, stat_buf
.st_size
);
255 jcf
->buffer_end
= jcf
->buffer
+ stat_buf
.st_size
;
256 jcf
->read_ptr
= jcf
->buffer
;
257 jcf
->read_end
= jcf
->buffer_end
;
258 jcf
->read_state
= NULL
;
259 jcf
->filename
= xstrdup (filename
);
260 if (read (fd
, jcf
->buffer
, stat_buf
.st_size
) != stat_buf
.st_size
)
262 perror ("Failed to read .class file");
266 jcf
->filbuf
= jcf_unexpected_eof
;
275 find_classfile (char *filename
, JCF
*jcf
, const char *dep_name
)
277 int fd
= open (filename
, O_RDONLY
| O_BINARY
);
280 return open_class (filename
, jcf
, fd
, dep_name
);
283 /* Returns 1 if the CLASSNAME (really a char *) matches the name
284 stored in TABLE_ENTRY (also a char *). */
287 memoized_class_lookup_eq (const void *table_entry
, const void *classname
)
289 return strcmp ((const char *)classname
, (const char *)table_entry
) == 0;
292 /* A hash table keeping track of class names that were not found
293 during class lookup. (There is no need to cache the values
294 associated with names that were found; they are saved in
295 IDENTIFIER_CLASS_VALUE.) */
296 static htab_t memoized_class_lookups
;
298 /* Returns a freshly malloc'd string with the fully qualified pathname
299 of the .class file for the class CLASSNAME. CLASSNAME must be
300 allocated in permanent storage; this function may retain a pointer
301 to it. Returns NULL on failure. If JCF != NULL, it is suitably
302 initialized. SOURCE_OK is true if we should also look for .java
306 find_class (const char *classname
, int classname_length
, JCF
*jcf
)
309 int i
, k
, klass
= -1;
310 struct stat class_buf
;
317 /* Create the hash table, if it does not already exist. */
318 if (!memoized_class_lookups
)
319 memoized_class_lookups
= htab_create (37,
321 memoized_class_lookup_eq
,
324 /* Loop for this class in the hashtable. If it is present, we've
325 already looked for this class and failed to find it. */
326 hash
= htab_hash_string (classname
);
327 if (htab_find_with_hash (memoized_class_lookups
, classname
, hash
))
330 /* Allocate and zero out the buffer, since we don't explicitly put a
331 null pointer when we're copying it below. */
332 buflen
= jcf_path_max_len () + classname_length
+ 10;
333 buffer
= XNEWVAR (char, buflen
);
334 memset (buffer
, 0, buflen
);
336 for (entry
= jcf_path_start (); entry
!= NULL
; entry
= jcf_path_next (entry
))
338 const char *path_name
= jcf_path_name (entry
);
343 strcpy (buffer
, path_name
);
346 /* This is right because we know that `.zip' entries will have a
347 trailing slash. See jcf-path.c. */
350 for (k
= 0; k
< classname_length
; k
++, i
++)
352 char ch
= classname
[k
];
353 buffer
[i
] = ch
== '.' ? '/' : ch
;
355 strcpy (buffer
+i
, ".class");
357 if (jcf_path_is_zipfile (entry
))
361 buffer
[dir_len
] = '\0';
362 SOURCE_FRONTEND_DEBUG
363 (("Trying [...%s]:%s",
364 &buffer
[dir_len
-(dir_len
> 15 ? 15 : dir_len
)],
368 err_code
= open_in_zip (jcf
, buffer
, buffer
+dir_len
+1,
369 jcf_path_is_system (entry
));
372 /* Should we check if .zip is out-of-date wrt .java? */
373 buffer
[dir_len
] = '(';
374 strcpy (buffer
+i
, ".class)");
382 klass
= stat (buffer
, &class_buf
);
389 SOURCE_FRONTEND_DEBUG ((stderr
, "[Class selected: %s]\n",
390 classname
+classname_length
-
391 (classname_length
<= 30 ?
392 classname_length
: 30)));
393 fd
= JCF_OPEN_EXACT_CASE (buffer
, O_RDONLY
| O_BINARY
);
400 /* Remember that this class could not be found so that we do not
401 have to look again. */
402 *htab_find_slot_with_hash (memoized_class_lookups
, classname
, hash
, INSERT
)
403 = (void *) CONST_CAST (char *, classname
);
408 const char *const tmp
= open_class (buffer
, jcf
, fd
, dep_file
);
409 jcf
->classname
= xstrdup (classname
);
415 jcf_print_char (FILE *stream
, int ch
)
422 fprintf (stream
, "\\%c", ch
);
425 fprintf (stream
, "\\n");
428 fprintf (stream
, "\\t");
431 fprintf (stream
, "\\r");
434 if (ch
>= ' ' && ch
< 127)
437 fprintf (stream
, "\\%03x", ch
);
439 fprintf (stream
, "\\u%04x", ch
);
443 /* Print UTF8 string at STR of length LENGTH bytes to STREAM. */
446 jcf_print_utf8 (FILE *stream
, const unsigned char *str
, int length
)
448 const unsigned char * limit
= str
+ length
;
451 int ch
= UTF8_GET (str
, limit
);
454 fprintf (stream
, "\\<invalid>");
457 jcf_print_char (stream
, ch
);
461 /* Same as jcf_print_utf8, but print IN_CHAR as OUT_CHAR. */
464 jcf_print_utf8_replace (FILE *stream
, const unsigned char *str
, int length
,
465 int in_char
, int out_char
)
467 const unsigned char *limit
= str
+ length
;
470 int ch
= UTF8_GET (str
, limit
);
473 fprintf (stream
, "\\<invalid>");
476 jcf_print_char (stream
, ch
== in_char
? out_char
: ch
);
480 /* Check that all the cross-references in the constant pool are
481 valid. Returns 0 on success.
482 Otherwise, returns the index of the (first) invalid entry.
483 Only checks internal consistency, but does not check that
484 any classes, fields, or methods are valid.*/
487 verify_constant_pool (JCF
*jcf
)
490 for (i
= 1; i
< JPOOL_SIZE (jcf
); i
++)
492 switch (JPOOL_TAG (jcf
, i
))
494 case CONSTANT_NameAndType
:
495 n
= JPOOL_USHORT2 (jcf
, i
);
496 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
497 || JPOOL_TAG (jcf
, n
) != CONSTANT_Utf8
)
499 /* ... fall through ... */
501 case CONSTANT_String
:
502 n
= JPOOL_USHORT1 (jcf
, i
);
503 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
504 || JPOOL_TAG (jcf
, n
) != CONSTANT_Utf8
)
507 case CONSTANT_Fieldref
:
508 case CONSTANT_Methodref
:
509 case CONSTANT_InterfaceMethodref
:
510 n
= JPOOL_USHORT1 (jcf
, i
);
511 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
512 || JPOOL_TAG (jcf
, n
) != CONSTANT_Class
)
514 n
= JPOOL_USHORT2 (jcf
, i
);
515 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
516 || JPOOL_TAG (jcf
, n
) != CONSTANT_NameAndType
)
520 case CONSTANT_Double
:
524 case CONSTANT_Integer
:
526 case CONSTANT_Unicode
:
536 format_uint (char *buffer
, uint64 value
, int base
)
538 #define WRITE_BUF_SIZE (4 + sizeof(uint64) * 8)
539 char buf
[WRITE_BUF_SIZE
];
540 char *buf_ptr
= buf
+WRITE_BUF_SIZE
; /* End of buf. */
544 /* Now do the actual conversion, placing the result at the *end* of buf. */
545 /* Note this code does not pretend to be optimized. */
547 int digit
= value
% base
;
548 static const char digit_chars
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
549 *--buf_ptr
= digit_chars
[digit
];
551 } while (value
!= 0);
553 chars_written
= buf
+WRITE_BUF_SIZE
- buf_ptr
;
554 for (i
= 0; i
< chars_written
; i
++)
555 buffer
[i
] = *buf_ptr
++;
560 format_int (char *buffer
, jlong value
, int base
)
565 abs_value
= -(uint64
)value
;
569 abs_value
= (uint64
) value
;
570 format_uint (buffer
, abs_value
, base
);