0N/A/*
2362N/A * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/A
0N/A// Global Structures
0N/Astruct jar;
0N/Astruct gunzip;
0N/Astruct band;
0N/Astruct cpool;
0N/Astruct entry;
0N/Astruct cpindex;
0N/Astruct inner_class;
0N/Astruct value_stream;
0N/A
0N/Astruct cpindex {
0N/A uint len;
0N/A entry* base1; // base of primary index
0N/A entry** base2; // base of secondary index
0N/A byte ixTag; // type of entries (!= CONSTANT_None), plus 64 if sub-index
0N/A enum { SUB_TAG = 64 };
0N/A
0N/A entry* get(uint i);
0N/A
0N/A void init(int len_, entry* base1_, int ixTag_) {
0N/A len = len_;
0N/A base1 = base1_;
0N/A base2 = null;
0N/A ixTag = ixTag_;
0N/A }
0N/A void init(int len_, entry** base2_, int ixTag_) {
0N/A len = len_;
0N/A base1 = null;
0N/A base2 = base2_;
0N/A ixTag = ixTag_;
0N/A }
0N/A};
0N/A
0N/Astruct cpool {
0N/A uint nentries;
0N/A entry* entries;
0N/A entry* first_extra_entry;
0N/A uint maxentries; // total allocated size of entries
0N/A
0N/A // Position and size of each homogeneous subrange:
0N/A int tag_count[CONSTANT_Limit];
0N/A int tag_base[CONSTANT_Limit];
0N/A cpindex tag_index[CONSTANT_Limit];
0N/A ptrlist tag_extras[CONSTANT_Limit];
0N/A
0N/A cpindex* member_indexes; // indexed by 2*CONSTANT_Class.inord
0N/A cpindex* getFieldIndex(entry* classRef);
0N/A cpindex* getMethodIndex(entry* classRef);
0N/A
0N/A inner_class** ic_index;
0N/A inner_class** ic_child_index;
0N/A inner_class* getIC(entry* inner);
0N/A inner_class* getFirstChildIC(entry* outer);
0N/A inner_class* getNextChildIC(inner_class* child);
0N/A
0N/A int outputIndexLimit; // index limit after renumbering
0N/A ptrlist outputEntries; // list of entry* needing output idx assigned
0N/A
0N/A entry** hashTab;
0N/A uint hashTabLength;
0N/A entry*& hashTabRef(byte tag, bytes& b);
0N/A entry* ensureUtf8(bytes& b);
0N/A entry* ensureClass(bytes& b);
0N/A
0N/A // Well-known Utf8 symbols.
0N/A enum {
0N/A #define SNAME(n,s) s_##s,
0N/A ALL_ATTR_DO(SNAME)
0N/A #undef SNAME
0N/A s_lt_init_gt, // <init>
0N/A s_LIMIT
0N/A };
0N/A entry* sym[s_LIMIT];
0N/A
0N/A // read counts from hdr, allocate main arrays
0N/A enum { NUM_COUNTS = 12 };
0N/A void init(unpacker* u, int counts[NUM_COUNTS]);
0N/A
0N/A // pointer to outer unpacker, for error checks etc.
0N/A unpacker* u;
0N/A
0N/A int getCount(byte tag) {
0N/A assert((uint)tag < CONSTANT_Limit);
0N/A return tag_count[tag];
0N/A }
0N/A cpindex* getIndex(byte tag) {
0N/A assert((uint)tag < CONSTANT_Limit);
0N/A return &tag_index[tag];
0N/A }
0N/A cpindex* getKQIndex(); // uses cur_descr
0N/A
0N/A void expandSignatures();
0N/A void initMemberIndexes();
0N/A
0N/A void computeOutputOrder();
0N/A void computeOutputIndexes();
0N/A void resetOutputIndexes();
0N/A
0N/A // error handling
0N/A inline void abort(const char* msg);
0N/A inline bool aborting();
0N/A};
0N/A
0N/A/*
0N/A * The unpacker provides the entry points to the unpack engine,
0N/A * as well as maintains the state of the engine.
0N/A */
0N/Astruct unpacker {
0N/A // One element of the resulting JAR.
0N/A struct file {
0N/A const char* name;
0N/A julong size;
0N/A int modtime;
0N/A int options;
0N/A bytes data[2];
0N/A // Note: If Sum(data[*].len) < size,
0N/A // remaining bytes must be read directly from the input stream.
0N/A bool deflate_hint() { return ((options & FO_DEFLATE_HINT) != 0); }
0N/A };
0N/A
0N/A // back pointer to NativeUnpacker obj and Java environment
0N/A void* jniobj;
0N/A void* jnienv;
0N/A
0N/A // global pointer to self, if not running under JNI (not multi-thread safe)
0N/A static unpacker* non_mt_current;
0N/A
0N/A // if running Unix-style, here are the inputs and outputs
0N/A FILE* infileptr; // buffered
0N/A int infileno; // unbuffered
0N/A bytes inbytes; // direct
0N/A gunzip* gzin; // gunzip filter, if any
0N/A jar* jarout; // output JAR file
0N/A
0N/A#ifndef PRODUCT
0N/A int nowrite;
0N/A int skipfiles;
0N/A int verbose_bands;
0N/A#endif
0N/A
0N/A // pointer to self, for U_NEW macro
0N/A unpacker* u;
0N/A
0N/A // private abort message string, allocated to PATH_MAX*2
0N/A const char* abort_message;
0N/A ptrlist mallocs; // list of guys to free when we are all done
0N/A ptrlist tmallocs; // list of guys to free on next client request
0N/A fillbytes smallbuf; // supplies small alloc requests
0N/A fillbytes tsmallbuf; // supplies temporary small alloc requests
0N/A
0N/A // option management members
0N/A int verbose; // verbose level, 0 means no output
0N/A bool strip_compile;
0N/A bool strip_debug;
0N/A bool strip_jcov;
0N/A bool remove_packfile;
0N/A int deflate_hint_or_zero; // ==0 means not set, otherwise -1 or 1
0N/A int modification_time_or_zero;
0N/A
0N/A FILE* errstrm;
0N/A const char* errstrm_name;
0N/A
0N/A const char* log_file;
0N/A
0N/A // input stream
0N/A fillbytes input; // the whole block (size is predicted, has slop too)
0N/A bool live_input; // is the data in this block live?
0N/A bool free_input; // must the input buffer be freed?
0N/A byte* rp; // read pointer (< rplimit <= input.limit())
0N/A byte* rplimit; // how much of the input block has been read?
0N/A julong bytes_read;
0N/A int unsized_bytes_read;
0N/A
0N/A // callback to read at least one byte, up to available input
0N/A typedef jlong (*read_input_fn_t)(unpacker* self, void* buf, jlong minlen, jlong maxlen);
0N/A read_input_fn_t read_input_fn;
0N/A
0N/A // archive header fields
0N/A int magic, minver, majver;
1117N/A size_t archive_size;
0N/A int archive_next_count, archive_options, archive_modtime;
0N/A int band_headers_size;
0N/A int file_count, attr_definition_count, ic_count, class_count;
0N/A int default_class_minver, default_class_majver;
0N/A int default_file_options, suppress_file_options; // not header fields
0N/A int default_archive_modtime, default_file_modtime; // not header fields
0N/A int code_count; // not a header field
0N/A int files_remaining; // not a header field
0N/A
0N/A // engine state
0N/A band* all_bands; // indexed by band_number
0N/A byte* meta_rp; // read-pointer into (copy of) band_headers
0N/A cpool cp; // all constant pool information
0N/A inner_class* ics; // InnerClasses
0N/A
0N/A // output stream
0N/A bytes output; // output block (either classfile head or tail)
0N/A byte* wp; // write pointer (< wplimit == output.limit())
0N/A byte* wpbase; // write pointer starting address (<= wp)
0N/A byte* wplimit; // how much of the output block has been written?
0N/A
0N/A // output state
0N/A file cur_file;
0N/A entry* cur_class; // CONSTANT_Class entry
0N/A entry* cur_super; // CONSTANT_Class entry or null
0N/A entry* cur_descr; // CONSTANT_NameandType entry
0N/A int cur_descr_flags; // flags corresponding to cur_descr
0N/A int cur_class_minver, cur_class_majver;
0N/A bool cur_class_has_local_ics;
0N/A fillbytes cur_classfile_head;
0N/A fillbytes cur_classfile_tail;
0N/A int files_written; // also tells which file we're working on
0N/A int classes_written; // also tells which class we're working on
0N/A julong bytes_written;
0N/A intlist bcimap;
0N/A fillbytes class_fixup_type;
0N/A intlist class_fixup_offset;
0N/A ptrlist class_fixup_ref;
0N/A fillbytes code_fixup_type; // which format of branch operand?
0N/A intlist code_fixup_offset; // location of operand needing fixup
0N/A intlist code_fixup_source; // encoded ID of branch insn
0N/A ptrlist requested_ics; // which ics need output?
0N/A
0N/A // stats pertaining to multiple segments (updated on reset)
0N/A julong bytes_read_before_reset;
0N/A julong bytes_written_before_reset;
0N/A int files_written_before_reset;
0N/A int classes_written_before_reset;
0N/A int segments_read_before_reset;
0N/A
0N/A // attribute state
0N/A struct layout_definition {
0N/A uint idx; // index (0..31...) which identifies this layout
0N/A const char* name; // name of layout
0N/A entry* nameEntry;
0N/A const char* layout; // string of layout (not yet parsed)
0N/A band** elems; // array of top-level layout elems (or callables)
0N/A
0N/A bool hasCallables() { return layout[0] == '['; }
0N/A band** bands() { assert(elems != null); return elems; }
0N/A };
0N/A struct attr_definitions {
0N/A unpacker* u; // pointer to self, for U_NEW macro
0N/A int xxx_flags_hi_bn;// locator for flags, count, indexes, calls bands
0N/A int attrc; // ATTR_CONTEXT_CLASS, etc.
0N/A uint flag_limit; // 32 or 63, depending on archive_options bit
0N/A julong predef; // mask of built-in definitions
0N/A julong redef; // mask of local flag definitions or redefinitions
0N/A ptrlist layouts; // local (compressor-defined) defs, in index order
0N/A int flag_count[X_ATTR_LIMIT_FLAGS_HI];
0N/A intlist overflow_count;
0N/A ptrlist strip_names; // what attribute names are being stripped?
0N/A ptrlist band_stack; // Temp., used during layout parsing.
0N/A ptrlist calls_to_link; // (ditto)
0N/A int bands_made; // (ditto)
0N/A
0N/A void free() {
0N/A layouts.free();
0N/A overflow_count.free();
0N/A strip_names.free();
0N/A band_stack.free();
0N/A calls_to_link.free();
0N/A }
0N/A
0N/A // Locate the five fixed bands.
0N/A band& xxx_flags_hi();
0N/A band& xxx_flags_lo();
0N/A band& xxx_attr_count();
0N/A band& xxx_attr_indexes();
0N/A band& xxx_attr_calls();
0N/A band& fixed_band(int e_class_xxx);
0N/A
0N/A // Register a new layout, and make bands for it.
0N/A layout_definition* defineLayout(int idx, const char* name, const char* layout);
0N/A layout_definition* defineLayout(int idx, entry* nameEntry, const char* layout);
0N/A band** buildBands(layout_definition* lo);
0N/A
0N/A // Parse a layout string or part of one, recursively if necessary.
0N/A const char* parseLayout(const char* lp, band** &res, int curCble);
0N/A const char* parseNumeral(const char* lp, int &res);
0N/A const char* parseIntLayout(const char* lp, band* &res, byte le_kind,
0N/A bool can_be_signed = false);
0N/A band** popBody(int band_stack_base); // pops a body off band_stack
0N/A
0N/A // Read data into the bands of the idx-th layout.
0N/A void readBandData(int idx); // parse layout, make bands, read data
0N/A void readBandData(band** body, uint count); // recursive helper
0N/A
0N/A layout_definition* getLayout(uint idx) {
494N/A if (idx >= (uint)layouts.length()) return null;
0N/A return (layout_definition*) layouts.get(idx);
0N/A }
0N/A
0N/A void setHaveLongFlags(bool z) {
0N/A assert(flag_limit == 0); // not set up yet
0N/A flag_limit = (z? X_ATTR_LIMIT_FLAGS_HI: X_ATTR_LIMIT_NO_FLAGS_HI);
0N/A }
0N/A bool haveLongFlags() {
0N/A assert(flag_limit == X_ATTR_LIMIT_NO_FLAGS_HI ||
0N/A flag_limit == X_ATTR_LIMIT_FLAGS_HI);
0N/A return flag_limit == X_ATTR_LIMIT_FLAGS_HI;
0N/A }
0N/A
0N/A // Return flag_count if idx is predef and not redef, else zero.
0N/A int predefCount(uint idx);
0N/A
0N/A bool isRedefined(uint idx) {
494N/A if (idx >= flag_limit) return false;
494N/A return (bool)((redef >> idx) & 1);
0N/A }
0N/A bool isPredefined(uint idx) {
494N/A if (idx >= flag_limit) return false;
494N/A return (bool)(((predef & ~redef) >> idx) & 1);
0N/A }
0N/A julong flagIndexMask() {
0N/A return (predef | redef);
0N/A }
0N/A bool isIndex(uint idx) {
0N/A assert(flag_limit != 0); // must be set up already
0N/A if (idx < flag_limit)
494N/A return (bool)(((predef | redef) >> idx) & 1);
0N/A else
494N/A return (idx - flag_limit < (uint)overflow_count.length());
0N/A }
0N/A int& getCount(uint idx) {
0N/A assert(isIndex(idx));
0N/A if (idx < flag_limit)
0N/A return flag_count[idx];
0N/A else
0N/A return overflow_count.get(idx - flag_limit);
0N/A }
0N/A bool aborting() { return u->aborting(); }
0N/A void abort(const char* msg) { u->abort(msg); }
0N/A };
0N/A
0N/A attr_definitions attr_defs[ATTR_CONTEXT_LIMIT];
0N/A
0N/A // Initialization
0N/A void init(read_input_fn_t input_fn = null);
0N/A // Resets to a known sane state
0N/A void reset();
0N/A // Deallocates all storage.
0N/A void free();
0N/A // Deallocates temporary storage (volatile after next client call).
0N/A void free_temps() { tsmallbuf.init(); tmallocs.freeAll(); }
0N/A
0N/A // Option management methods
0N/A bool set_option(const char* option, const char* value);
0N/A const char* get_option(const char* option);
0N/A
0N/A void dump_options();
0N/A
0N/A // Fetching input.
0N/A bool ensure_input(jlong more);
0N/A byte* input_scan() { return rp; }
0N/A size_t input_remaining() { return rplimit - rp; }
0N/A size_t input_consumed() { return rp - input.base(); }
0N/A
0N/A // Entry points to the unpack engine
0N/A static int run(int argc, char **argv); // Unix-style entry point.
0N/A void check_options();
0N/A void start(void* packptr = null, size_t len = 0);
0N/A void redirect_stdio();
0N/A void write_file_to_jar(file* f);
0N/A void finish();
0N/A
0N/A // Public post unpack methods
0N/A int get_files_remaining() { return files_remaining; }
0N/A int get_segments_remaining() { return archive_next_count; }
0N/A file* get_next_file(); // returns null on last file
0N/A
0N/A // General purpose methods
0N/A void* alloc(size_t size) { return alloc_heap(size, true); }
0N/A void* temp_alloc(size_t size) { return alloc_heap(size, true, true); }
0N/A void* alloc_heap(size_t size, bool smallOK = false, bool temp = false);
0N/A void saveTo(bytes& b, const char* str) { saveTo(b, (byte*)str, strlen(str)); }
0N/A void saveTo(bytes& b, bytes& data) { saveTo(b, data.ptr, data.len); }
0N/A void saveTo(bytes& b, byte* ptr, size_t len); //{ b.ptr = U_NEW...}
0N/A const char* saveStr(const char* str) { bytes buf; saveTo(buf, str); return buf.strval(); }
0N/A const char* saveIntStr(int num) { char buf[30]; sprintf(buf, "%d", num); return saveStr(buf); }
0N/A#ifndef PRODUCT
0N/A int printcr_if_verbose(int level, const char* fmt,...);
0N/A#endif
0N/A const char* get_abort_message();
0N/A void abort(const char* s = null);
0N/A bool aborting() { return abort_message != null; }
0N/A static unpacker* current(); // find current instance
0N/A
0N/A // Output management
0N/A void set_output(fillbytes* which) {
0N/A assert(wp == null);
0N/A which->ensureSize(1 << 12); // covers the average classfile
0N/A wpbase = which->base();
0N/A wp = which->limit();
0N/A wplimit = which->end();
0N/A }
0N/A fillbytes* close_output(fillbytes* which = null); // inverse of set_output
0N/A
0N/A // These take an implicit parameter of wp/wplimit, and resize as necessary:
0N/A byte* put_space(size_t len); // allocates space at wp, returns pointer
0N/A size_t put_empty(size_t s) { byte* p = put_space(s); return p - wpbase; }
0N/A void ensure_put_space(size_t len);
0N/A void put_bytes(bytes& b) { b.writeTo(put_space(b.len)); }
0N/A void putu1(int n) { putu1_at(put_space(1), n); }
0N/A void putu1_fast(int n) { putu1_at(wp++, n); }
0N/A void putu2(int n); // { putu2_at(put_space(2), n); }
0N/A void putu4(int n); // { putu4_at(put_space(4), n); }
0N/A void putu8(jlong n); // { putu8_at(put_space(8), n); }
0N/A void putref(entry* e); // { putu2_at(put_space(2), putref_index(e, 2)); }
0N/A void putu1ref(entry* e); // { putu1_at(put_space(1), putref_index(e, 1)); }
0N/A int putref_index(entry* e, int size); // size in [1..2]
0N/A void put_label(int curIP, int size); // size in {2,4}
0N/A void putlayout(band** body);
0N/A void put_stackmap_type();
0N/A
0N/A size_t wpoffset() { return (size_t)(wp - wpbase); } // (unvariant across overflow)
0N/A byte* wp_at(size_t offset) { return wpbase + offset; }
0N/A uint to_bci(uint bii);
0N/A void get_code_header(int& max_stack,
0N/A int& max_na_locals,
0N/A int& handler_count,
0N/A int& cflags);
0N/A band* ref_band_for_self_op(int bc, bool& isAloadVar, int& origBCVar);
0N/A band* ref_band_for_op(int bc);
0N/A
0N/A // Definitions of standard classfile int formats:
0N/A static void putu1_at(byte* wp, int n) { assert(n == (n & 0xFF)); wp[0] = n; }
0N/A static void putu2_at(byte* wp, int n);
0N/A static void putu4_at(byte* wp, int n);
0N/A static void putu8_at(byte* wp, jlong n);
0N/A
0N/A // Private stuff
0N/A void reset_cur_classfile();
0N/A void write_classfile_tail();
0N/A void write_classfile_head();
0N/A void write_code();
0N/A void write_bc_ops();
0N/A void write_members(int num, int attrc); // attrc=ATTR_CONTEXT_FIELD/METHOD
0N/A int write_attrs(int attrc, julong indexBits);
0N/A
0N/A // The readers
0N/A void read_bands();
0N/A void read_file_header();
0N/A void read_cp();
0N/A void read_cp_counts(value_stream& hdr);
0N/A void read_attr_defs();
0N/A void read_ics();
0N/A void read_attrs(int attrc, int obj_count);
0N/A void read_classes();
0N/A void read_code_headers();
0N/A void read_bcs();
0N/A void read_bc_ops();
0N/A void read_files();
0N/A void read_Utf8_values(entry* cpMap, int len);
0N/A void read_single_words(band& cp_band, entry* cpMap, int len);
0N/A void read_double_words(band& cp_bands, entry* cpMap, int len);
0N/A void read_single_refs(band& cp_band, byte refTag, entry* cpMap, int len);
0N/A void read_double_refs(band& cp_band, byte ref1Tag, byte ref2Tag, entry* cpMap, int len);
0N/A void read_signature_values(entry* cpMap, int len);
0N/A};
0N/A
0N/Ainline void cpool::abort(const char* msg) { u->abort(msg); }
0N/Ainline bool cpool::aborting() { return u->aborting(); }