Home arrow static arrow Java Programming [Archive] - Bytecode info
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Bytecode info
This topic has 23 replies on 2 pages.    1 | 2 | Next »

Posts:29,016
Registered: 23.07.02
Bytecode info  
Aug 3, 2004 1:47 AM



 
I just wondered about the package information in the bytecode... does it always begin at byte 13 and is it always terminated with x07 x00? I know I could look at the specs, but I somehow seem to stupid to find them on this site... :p
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 2:02 AM (reply 1 of 23)



 
The spec is here
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html

But I don't see what you are talking about at byte 13

This might be a good page to bookmark
http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html
 

Posts:29,016
Registered: 23.07.02
Re: Bytecode info  
Aug 3, 2004 2:08 AM (reply 2 of 23)



 
Thanks a lot!
 

Posts:29,016
Registered: 23.07.02
Re: Bytecode info  
Aug 3, 2004 3:10 AM (reply 3 of 23)



 
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html states:

A class file consists of a single ClassFile structure:
    ClassFile {    	u4 magic;    	u2 minor_version;    	u2 major_version;    	u2 constant_pool_count;    	cp_info constant_pool[constant_pool_count-1];    	u2 access_flags;    	u2 this_class;            <-- I need this position    	u2 super_class;    	u2 interfaces_count;    	u2 interfaces[interfaces_count];    	u2 fields_count;    	field_info fields[fields_count];    	u2 methods_count;    	method_info methods[methods_count];    	u2 attributes_count;    	attribute_info attributes[attributes_count];    }[code] and:  [i]constant_pool_count The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one. A constant_pool index is considered valid if it is greater than zero and less than constant_pool_count, with the exception for constants of type long and double noted in �4.4.5. constant_pool[] The constant_pool is a table of structures (�4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte.The constant_pool table is indexed from 1 to constant_pool_count-1. [/i] and later: [i]All constant_pool table entries have the following general format:[/i] [code]    cp_info {    	u1 tag;    	u1 info[];    }


Each item in the constant_pool table must begin with a 1-byte tag indicating the kind of cp_info entry. The contents of the info array vary with the value of tag. The valid tags and their values are listed in Table 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.


which definitely confuses me. I only checked about 10 or so class files, but none had a constant_pool entry longer than one single byte. I don't understand what the spec is trying to tell me. How can I find out the length of the constant pool table? I need that info to determine the position of the package/classname (this_class) identifier. It has to be in there somewhere...
 

Posts:29,016
Registered: 23.07.02
Re: Bytecode info  
Aug 3, 2004 3:12 AM (reply 4 of 23)



 
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html states:

A class file consists of a single ClassFile structure:
    ClassFile {    	u4 magic;    	u2 minor_version;    	u2 major_version;    	u2 constant_pool_count;    	cp_info constant_pool[constant_pool_count-1];    	u2 access_flags;    	u2 this_class;            <-- I need this position    	u2 super_class;    	u2 interfaces_count;    	u2 interfaces[interfaces_count];    	u2 fields_count;    	field_info fields[fields_count];    	u2 methods_count;    	method_info methods[methods_count];    	u2 attributes_count;    	attribute_info attributes[attributes_count];    }


and:

constant_pool_count
The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one. A constant_pool index is considered valid if it is greater than zero and less than constant_pool_count, with the exception for constants of type long and double noted in �4.4.5.

constant_pool[]
The constant_pool is a table of structures (�4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte.
The constant_pool table is indexed from 1 to constant_pool_count-1.


and later:

All constant_pool table entries have the following general format:

    cp_info {    	u1 tag;    	u1 info[];    }


Each item in the constant_pool table must begin with a 1-byte tag indicating the kind of cp_info entry. The contents of the info array vary with the value of tag. The valid tags and their values are listed in Table 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.


which definitely confuses me. I only checked about 10 or so class files, but none had a constant_pool entry longer than one single byte. I don't understand what the spec is trying to tell me. How can I find out the length of the constant pool table? I need that info to determine the position of the package/classname (this_class) identifier. It has to be in there somewhere...
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:23 AM (reply 5 of 23)



 
If you go to section 4.4
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#20080

you will see the format of the cp_info structure. It's just a byte and word, so 3 bytes. If you take the constant_pool_count and multiply it by 3, this should give you the offset to skip the constant_pool. I haven't checked this, but that's how it ussually works.
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:23 AM (reply 6 of 23)



 
If you go to section 4.4
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#20080

you will see the format of the cp_info structure. It's just a byte and word, so 3 bytes. If you take the
constant_pool_count and multiply it by 3, this should give you the offset to skip the constant_pool. I
haven't checked this, but that's how it ussually works.
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:26 AM (reply 7 of 23)



 
Ignore previous post - Must.. get... coffee...
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:31 AM (reply 8 of 23)



 
You are going to have to iterate through them and figure out the length of each one depending on the tag.

It looks like they are constant sizes depending on tag except for CONSTANT_Utf8_info, so you'll have to
look at the length element in that structure.
 

Posts:29,016
Registered: 23.07.02
Re: Bytecode info  
Aug 3, 2004 3:32 AM (reply 9 of 23)



 
If you go to section 4.4
http://java.sun.com/docs/books/vmspec/2nd-edition/html/
lassFile.doc.html#20080

you will see the format of the cp_info structure.
It's just a byte and word, so 3 bytes. If you take
the
constant_pool_count and multiply it by 3, this should
give you the offset to skip the constant_pool. I
haven't checked this, but that's how it ussually
works.

That's what I thought. But the classes I checked looked like this (real example):

CA FE BA BE - magic
00 00 - minor version
00 2E - major version
00 7D - constant pool count
01 - one byte single byte where the constant pool is supposed to be
00 2D - access modifiers
And then, beginning with byte 13, the this_class entry.

7D * 3 might be anything, but it's definitely not 1. if it were that easy, I probably had it figured out by now. :|
 

Posts:2,830
Registered: 9/1/03
Re: Bytecode info  
Aug 3, 2004 3:36 AM (reply 10 of 23)



 
you do know about BCEL from apache right ? just wondering,
maybe you could use it to help with whatever you are doing ...
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:38 AM (reply 11 of 23)



 
CA FE BA BE - magic
00 00 - minor version
00 2E - major version
00 7D - constant pool count
01 00 - TWO byte single byte where the constant pool is supposed to be
00 2D - access modifiers
And then, beginning with byte 13, the this_class entry.

u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;

Since the constant_pool_count is 1, constant_pool_count-1 is zero and you have a zero constant pool length
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:43 AM (reply 12 of 23)



 
Dammit... Iv'e done it again.
http://www.kohlsaat.com/swill/footinmouth.htm
 

Posts:29,016
Registered: 23.07.02
Re: Bytecode info  
Aug 3, 2004 3:47 AM (reply 13 of 23)



 
CA FE BA BE - magic
00 00 - minor version
00 2E - major version
00 7D - constant pool count
01 00 - TWO byte single byte where the constant
pool is supposed to be
00 2D - access modifiers
And then, beginning with byte 13, the this_class
entry.

u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;

Since the constant_pool_count is 1,
constant_pool_count-1 is zero and you have a zero
constant pool length

But Constant Pool Count is 007D, and since the beginning of this_file is at byte 13 (starting with 0), cp_info can only be one single byte in my cases. The entire line is:

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

CA FE BA BE 00 00 00 2E 00 7D 01 00 2D 63 6F 6D
 

Posts:8,813
Registered: 10/4/00
Re: Bytecode info  
Aug 3, 2004 3:53 AM (reply 14 of 23)



 
Here's a class of mine. You didn't have enough info

CA FE BA BE - u4 magic;
00 00 - u2 minor_version;
00 2E - u2 major_version;
00 6C - u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
0A - u1 tag - CONSTANT_Methodref 10
00 21 - u2 class_index;
00 37 - u2 name_and_type_index;

07 - u1 - CONSTANT_Class 7
00 38 - u2 name_index;

etc for 6C -3 more entries
 
This topic has 23 replies on 2 pages.    1 | 2 | Next »