1 /* Utility routines for finding and reading Java(TM) .class files.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003
3 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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. */
26 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
30 #include "coretypes.h"
36 #include "java-tree.h"
45 /* DOS brain-damage */
47 #define O_BINARY 0 /* MS-DOS brain-damage */
51 DEFUN(jcf_unexpected_eof
, (jcf
, count
),
52 JCF
*jcf AND
int count ATTRIBUTE_UNUSED
)
55 fprintf (stderr
, "Premature end of .class file %s.\n", jcf
->filename
);
57 fprintf (stderr
, "Premature end of .class file <stdin>.\n");
62 DEFUN(jcf_trim_old_input
, (jcf
),
65 int count
= jcf
->read_ptr
- jcf
->buffer
;
68 memmove (jcf
->buffer
, jcf
->read_ptr
, jcf
->read_end
- jcf
->read_ptr
);
69 jcf
->read_ptr
-= count
;
70 jcf
->read_end
-= count
;
75 DEFUN(jcf_filbuf_from_stdio
, (jcf
, count
),
76 JCF
*jcf AND
int count
)
78 FILE *file
= (FILE*) (jcf
->read_state
);
79 if (count
> jcf
->buffer_end
- jcf
->read_ptr
)
81 JCF_u4 old_read_ptr
= jcf
->read_ptr
- jcf
->buffer
;
82 JCF_u4 old_read_end
= jcf
->read_end
- jcf
->buffer
;
83 JCF_u4 old_size
= jcf
->buffer_end
- jcf
->buffer
;
84 JCF_u4 new_size
= (old_size
== 0 ? 2000 : 2 * old_size
) + count
;
85 unsigned char *new_buffer
= jcf
->buffer
== NULL
? ALLOC (new_size
)
86 : REALLOC (jcf
->buffer
, new_size
);
87 jcf
->buffer
= new_buffer
;
88 jcf
->buffer_end
= new_buffer
+ new_size
;
89 jcf
->read_ptr
= new_buffer
+ old_read_ptr
;
90 jcf
->read_end
= new_buffer
+ old_read_end
;
92 count
-= jcf
->read_end
- jcf
->read_ptr
;
95 if ((int) fread (jcf
->read_end
, 1, count
, file
) != count
)
96 jcf_unexpected_eof (jcf
, count
);
97 jcf
->read_end
+= count
;
103 struct ZipFile
*SeenZipFiles
= NULL
;
105 /* Open a zip file with the given name, and cache directory and file
106 descriptor. If the file is missing, treat it as an empty archive.
107 Return NULL if the .zip file is malformed.
111 DEFUN(opendir_in_zip
, (zipfile
, is_system
),
112 const char *zipfile AND
int is_system
)
114 struct ZipFile
* zipf
;
117 for (zipf
= SeenZipFiles
; zipf
!= NULL
; zipf
= zipf
->next
)
119 if (strcmp (zipf
->name
, zipfile
) == 0)
123 zipf
= ALLOC (sizeof (struct ZipFile
) + strlen (zipfile
) + 1);
124 zipf
->next
= SeenZipFiles
;
125 zipf
->name
= (char*)(zipf
+1);
126 strcpy (zipf
->name
, zipfile
);
128 fd
= open (zipfile
, O_RDONLY
| O_BINARY
);
132 /* A missing zip file is not considered an error.
133 We may want to re-consider that. FIXME. */
136 zipf
->central_directory
= NULL
;
140 jcf_dependency_add_file (zipfile
, is_system
);
141 if (read (fd
, magic
, 4) != 4 || GET_u4 (magic
) != (JCF_u4
)ZIPMAGIC
)
143 lseek (fd
, 0L, SEEK_SET
);
144 if (read_zip_archive (zipf
) != 0)
151 0: OK - zipmember found.
153 -2: Malformed archive.
157 DEFUN(open_in_zip
, (jcf
, zipfile
, zipmember
, is_system
),
158 JCF
*jcf AND
const char *zipfile AND
const char *zipmember
163 ZipFile
*zipf
= opendir_in_zip (zipfile
, is_system
);
171 len
= strlen (zipmember
);
173 zipd
= (struct ZipDirectory
*) zipf
->central_directory
;
174 for (i
= 0; i
< zipf
->count
; i
++, zipd
= ZIPDIR_NEXT (zipd
))
176 if (len
== zipd
->filename_length
&&
177 strncmp (ZIPDIR_FILENAME (zipd
), zipmember
, len
) == 0)
181 jcf
->filename
= xstrdup (zipfile
);
182 jcf
->classname
= xstrdup (zipmember
);
183 return read_zip_member(jcf
, zipd
, zipf
);
189 /* Read data from zip archive member. */
192 DEFUN(read_zip_member
, (jcf
, zipd
, zipf
),
193 JCF
*jcf AND ZipDirectory
*zipd AND ZipFile
*zipf
)
195 jcf
->filbuf
= jcf_unexpected_eof
;
196 jcf
->zipd
= (void *)zipd
;
198 if (zipd
->compression_method
== Z_NO_COMPRESSION
)
200 jcf
->buffer
= ALLOC (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
= ALLOC (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
= ALLOC (zipd
->size
);
223 d_stream
.next_in
= 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 DEFUN(open_class
, (filename
, jcf
, fd
, dep_name
),
241 const char *filename AND JCF
*jcf AND
int fd AND
const char *dep_name
)
245 struct stat stat_buf
;
246 if (fstat (fd
, &stat_buf
) != 0
247 || ! S_ISREG (stat_buf
.st_mode
))
249 perror ("Could not figure length of .class file");
252 if (dep_name
!= NULL
)
253 jcf_dependency_add_file (dep_name
, 0);
255 jcf
->buffer
= ALLOC (stat_buf
.st_size
);
256 jcf
->buffer_end
= jcf
->buffer
+ stat_buf
.st_size
;
257 jcf
->read_ptr
= jcf
->buffer
;
258 jcf
->read_end
= jcf
->buffer_end
;
259 jcf
->read_state
= NULL
;
260 jcf
->filename
= filename
;
261 if (read (fd
, jcf
->buffer
, stat_buf
.st_size
) != stat_buf
.st_size
)
263 perror ("Failed to read .class file");
267 jcf
->filbuf
= jcf_unexpected_eof
;
276 DEFUN(find_classfile
, (filename
, jcf
, dep_name
),
277 char *filename AND JCF
*jcf AND
const char *dep_name
)
279 int fd
= open (filename
, O_RDONLY
| O_BINARY
);
282 return open_class (filename
, jcf
, fd
, dep_name
);
287 /* A comparison function (as for qsort) that compares KEY (a char *
288 giving the basename of a file) with the name stored in ENTRY (a
292 DEFUN(compare_path
, (key
, entry
),
293 const void *key AND
const void *entry
)
295 return strcmp ((const char *) key
,
296 (*((const struct dirent
**) entry
))->d_name
);
299 /* Returns nonzero if ENTRY names a .java or .class file. */
302 DEFUN(java_or_class_file
, (entry
),
303 const struct dirent
*entry
)
305 const char *base
= basename (entry
->d_name
);
306 return (fnmatch ("*.java", base
, 0) == 0 ||
307 fnmatch ("*.class", base
, 0) == 0);
310 /* Information about the files present in a particular directory. */
311 typedef struct memoized_dirlist_entry
313 /* The name of the directory. */
315 /* The number of .java and .class files present, or -1 if we could
316 not, for some reason, obtain the list. */
318 /* The .java and .class files in the directory, in alphabetical
320 struct dirent
**files
;
321 } memoized_dirlist_entry
;
323 /* Returns true if ENTRY (a memoized_dirlist_entry *) correponds to
324 the directory given by KEY (a char *) giving the directory
328 DEFUN(memoized_dirlist_lookup_eq
, (entry
, key
),
329 const void *entry AND
const void *key
)
331 return strcmp ((const char *) key
,
332 ((const memoized_dirlist_entry
*) entry
)->dir
) == 0;
335 /* A hash table mapping directory names to the lists of .java and
336 .class files in that directory. */
338 static htab_t memoized_dirlists
;
342 /* Like stat, but avoids actually making the stat system call if we
343 know that it cannot succeed. FILENAME and BUF are as for stat. */
346 DEFUN(caching_stat
, (filename
, buf
),
347 char *filename AND
struct stat
*buf
)
352 memoized_dirlist_entry
*dent
;
355 /* If the hashtable has not already been created, create it now. */
356 if (!memoized_dirlists
)
357 memoized_dirlists
= htab_create (37,
359 memoized_dirlist_lookup_eq
,
362 /* Get the name of the directory. */
363 sep
= strrchr (filename
, DIR_SEPARATOR
);
372 /* Obtain the entry for this directory form the hash table. */
373 slot
= htab_find_slot (memoized_dirlists
, filename
, INSERT
);
376 /* We have not already scanned this directory; scan it now. */
377 dent
= ((memoized_dirlist_entry
*)
378 ALLOC (sizeof (memoized_dirlist_entry
)));
379 dent
->dir
= xstrdup (filename
);
380 /* Unfortunately, scandir is not fully standardized. In
381 particular, the type of the function pointer passed as the
382 third argument sometimes takes a "const struct dirent *"
383 parameter, and sometimes just a "struct dirent *". We rely
384 on the ability to interchange these two types of function
386 dent
->num_files
= scandir (filename
, &dent
->files
,
392 dent
= *((memoized_dirlist_entry
**) slot
);
394 /* Put the spearator back. */
396 *sep
= DIR_SEPARATOR
;
398 /* If the file is not in the list, there is no need to stat it; it
400 if (dent
->num_files
!= -1
401 && !bsearch (base
, dent
->files
, dent
->num_files
,
402 sizeof (struct dirent
*), compare_path
))
406 return stat (filename
, buf
);
409 /* Returns 1 if the CLASSNAME (really a char *) matches the name
410 stored in TABLE_ENTRY (also a char *). */
413 DEFUN(memoized_class_lookup_eq
, (table_entry
, classname
),
414 const void *table_entry AND
const void *classname
)
416 return strcmp ((const char *)classname
, (const char *)table_entry
) == 0;
419 /* A hash table keeping track of class names that were not found
420 during class lookup. (There is no need to cache the values
421 associated with names that were found; they are saved in
422 IDENTIFIER_CLASS_VALUE.) */
423 static htab_t memoized_class_lookups
;
425 /* Returns a freshly malloc'd string with the fully qualified pathname
426 of the .class file for the class CLASSNAME. CLASSNAME must be
427 allocated in permanent storage; this function may retain a pointer
428 to it. Returns NULL on failure. If JCF != NULL, it is suitably
429 initialized. SOURCE_OK is true if we should also look for .java
433 DEFUN(find_class
, (classname
, classname_length
, jcf
, source_ok
),
434 const char *classname AND
int classname_length AND JCF
*jcf AND
int source_ok
)
438 int i
, k
, java
= -1, class = -1;
439 struct stat java_buf
, class_buf
;
447 /* Create the hash table, if it does not already exist. */
448 if (!memoized_class_lookups
)
449 memoized_class_lookups
= htab_create (37,
451 memoized_class_lookup_eq
,
454 /* Loop for this class in the hashtable. If it is present, we've
455 already looked for this class and failed to find it. */
456 hash
= htab_hash_string (classname
);
457 if (htab_find_with_hash (memoized_class_lookups
, classname
, hash
))
460 /* Allocate and zero out the buffer, since we don't explicitly put a
461 null pointer when we're copying it below. */
462 buflen
= jcf_path_max_len () + classname_length
+ 10;
463 buffer
= ALLOC (buflen
);
464 memset (buffer
, 0, buflen
);
466 java_buffer
= alloca (buflen
);
468 jcf
->java_source
= 0;
470 for (entry
= jcf_path_start (); entry
!= NULL
; entry
= jcf_path_next (entry
))
472 const char *path_name
= jcf_path_name (entry
);
477 strcpy (buffer
, path_name
);
480 /* This is right because we know that `.zip' entries will have a
481 trailing slash. See jcf-path.c. */
484 for (k
= 0; k
< classname_length
; k
++, i
++)
486 char ch
= classname
[k
];
487 buffer
[i
] = ch
== '.' ? '/' : ch
;
489 strcpy (buffer
+i
, ".class");
491 if (jcf_path_is_zipfile (entry
))
495 buffer
[dir_len
] = '\0';
496 SOURCE_FRONTEND_DEBUG
497 (("Trying [...%s]:%s",
498 &buffer
[dir_len
-(dir_len
> 15 ? 15 : dir_len
)],
502 err_code
= open_in_zip (jcf
, buffer
, buffer
+dir_len
+1,
503 jcf_path_is_system (entry
));
506 /* Should we check if .zip is out-of-date wrt .java? */
507 buffer
[dir_len
] = '(';
508 strcpy (buffer
+i
, ".class)");
516 class = caching_stat(buffer
, &class_buf
);
521 /* Compute name of .java file. */
523 strcpy (java_buffer
, path_name
);
524 l
= strlen (java_buffer
);
525 for (m
= 0; m
< classname_length
; ++m
)
526 java_buffer
[m
+ l
] = (classname
[m
] == '.' ? '/' : classname
[m
]);
527 strcpy (java_buffer
+ m
+ l
, ".java");
528 java
= caching_stat (java_buffer
, &java_buf
);
534 /* We preferably pick a class file if we have a chance. If the source
535 file is newer than the class file, we issue a warning and parse the
537 There should be a flag to allow people have the class file picked
538 up no matter what. FIXME. */
539 if (! java
&& ! class && java_buf
.st_mtime
> class_buf
.st_mtime
)
542 warning ("source file for class `%s' is newer than its matching class file. Source file `%s' used instead", classname
, java_buffer
);
547 dep_file
= java_buffer
;
552 SOURCE_FRONTEND_DEBUG ((stderr
, "[Class selected: %s]\n",
553 classname
+classname_length
-
554 (classname_length
<= 30 ?
555 classname_length
: 30)));
556 fd
= open (buffer
, O_RDONLY
| O_BINARY
);
560 /* Give .java a try, if necessary */
563 strcpy (buffer
, java_buffer
);
564 SOURCE_FRONTEND_DEBUG ((stderr
, "[Source selected: %s]\n",
565 classname
+classname_length
-
566 (classname_length
<= 30 ?
567 classname_length
: 30)));
568 fd
= open (buffer
, O_RDONLY
);
571 jcf
->java_source
= 1;
578 /* Remember that this class could not be found so that we do not
579 have to look again. */
580 *htab_find_slot_with_hash (memoized_class_lookups
, classname
, hash
, INSERT
)
581 = (void *) classname
;
585 if (jcf
->java_source
)
587 JCF_ZERO (jcf
); /* JCF_FINISH relies on this */
588 jcf
->java_source
= 1;
589 jcf
->filename
= xstrdup (buffer
);
590 close (fd
); /* We use STDIO for source file */
593 buffer
= (char *) open_class (buffer
, jcf
, fd
, dep_file
);
594 jcf
->classname
= xstrdup (classname
);
599 DEFUN(jcf_print_char
, (stream
, ch
),
600 FILE *stream AND
int ch
)
607 fprintf (stream
, "\\%c", ch
);
610 fprintf (stream
, "\\n");
613 fprintf (stream
, "\\t");
616 fprintf (stream
, "\\r");
619 if (ch
>= ' ' && ch
< 127)
622 fprintf (stream
, "\\%03x", ch
);
624 fprintf (stream
, "\\u%04x", ch
);
628 /* Print UTF8 string at STR of length LENGTH bytes to STREAM. */
631 DEFUN(jcf_print_utf8
, (stream
, str
, length
),
632 FILE *stream AND
register const unsigned char *str AND
int length
)
634 const unsigned char * limit
= str
+ length
;
637 int ch
= UTF8_GET (str
, limit
);
640 fprintf (stream
, "\\<invalid>");
643 jcf_print_char (stream
, ch
);
647 /* Same as jcf_print_utf8, but print IN_CHAR as OUT_CHAR. */
650 DEFUN(jcf_print_utf8_replace
, (stream
, str
, length
, in_char
, out_char
),
651 FILE *stream AND
const unsigned char *str AND
int length
652 AND
int in_char AND
int out_char
)
654 const unsigned char *limit
= str
+ length
;
657 int ch
= UTF8_GET (str
, limit
);
660 fprintf (stream
, "\\<invalid>");
663 jcf_print_char (stream
, ch
== in_char
? out_char
: ch
);
667 /* Check that all the cross-references in the constant pool are
668 valid. Returns 0 on success.
669 Otherwise, returns the index of the (first) invalid entry.
670 Only checks internal consistency, but does not check that
671 any classes, fields, or methods are valid.*/
674 DEFUN(verify_constant_pool
, (jcf
),
678 for (i
= 1; i
< JPOOL_SIZE (jcf
); i
++)
680 switch (JPOOL_TAG (jcf
, i
))
682 case CONSTANT_NameAndType
:
683 n
= JPOOL_USHORT2 (jcf
, i
);
684 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
685 || JPOOL_TAG (jcf
, n
) != CONSTANT_Utf8
)
687 /* ... fall through ... */
689 case CONSTANT_String
:
690 n
= JPOOL_USHORT1 (jcf
, i
);
691 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
692 || JPOOL_TAG (jcf
, n
) != CONSTANT_Utf8
)
695 case CONSTANT_Fieldref
:
696 case CONSTANT_Methodref
:
697 case CONSTANT_InterfaceMethodref
:
698 n
= JPOOL_USHORT1 (jcf
, i
);
699 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
700 || JPOOL_TAG (jcf
, n
) != CONSTANT_Class
)
702 n
= JPOOL_USHORT2 (jcf
, i
);
703 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
704 || JPOOL_TAG (jcf
, n
) != CONSTANT_NameAndType
)
708 case CONSTANT_Double
:
712 case CONSTANT_Integer
:
714 case CONSTANT_Unicode
:
724 DEFUN(format_uint
, (buffer
, value
, base
),
725 char *buffer AND uint64 value AND
int base
)
727 #define WRITE_BUF_SIZE (4 + sizeof(uint64) * 8)
728 char buf
[WRITE_BUF_SIZE
];
729 register char *buf_ptr
= buf
+WRITE_BUF_SIZE
; /* End of buf. */
733 /* Now do the actual conversion, placing the result at the *end* of buf. */
734 /* Note this code does not pretend to be optimized. */
736 int digit
= value
% base
;
737 static const char digit_chars
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
738 *--buf_ptr
= digit_chars
[digit
];
740 } while (value
!= 0);
742 chars_written
= buf
+WRITE_BUF_SIZE
- buf_ptr
;
743 for (i
= 0; i
< chars_written
; i
++)
744 buffer
[i
] = *buf_ptr
++;
749 DEFUN(format_int
, (buffer
, value
, base
),
750 char *buffer AND jlong value AND
int base
)
755 abs_value
= -(uint64
)value
;
759 abs_value
= (uint64
) value
;
760 format_uint (buffer
, abs_value
, base
);