Class文件結構
整體結構
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;
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];
}
從結構上看可以分爲幾大塊 文件頭、常量池、接口、字段、函數、屬性
文件頭
u4 magic; //固定值 0xCAFEBABE
u2 minor_version;
u2 major_version;
u2 access_flags; //訪問修飾
u2 this_class; //類名 常量池下標
u2 super_class; //父類名 常量池下標 如果是0 就是java/lang/Object;
</pre></code>
我把這幾個描述了類基本信息的字段稱爲文件頭
major_version.minor_version表示該class文件的版本號,由編譯器版本決定,然而不同版本的虛擬機只會支持一定版本範圍內的class文件,如果不在則會拒絕解析。
例如 openJDK中的實現
<pre><code>
// Check version numbers - we check this even with verifier off
if (!is_supported_version(major_version, minor_version)) {
if (name == NULL) {
Exceptions::fthrow(
THREAD_AND_LOCATION,
vmSymbols::java_lang_UnsupportedClassVersionError(),
"Unsupported major.minor version %u.%u",
major_version,
minor_version);
}
常量池
常量池包含了class文件中使用到的例如 函數標識/類型信息/字符串等等,運行時加載到內存中形成運行時常量池
常量項中文件中使用變長結構描述
cp_info {
u1 tag; //表示常量的類型
u1 info[]; //具體常量的內容結構 不同類型常量有不同的結構
}
/*常量類型*/
Constant Type Value
CONSTANT_Class 7
CONSTANT_Fieldref 9
CONSTANT_Methodref 10
CONSTANT_InterfaceMethodref 11
CONSTANT_String 8
CONSTANT_Integer 3
CONSTANT_Float 4
CONSTANT_Long 5
CONSTANT_Double 6
CONSTANT_NameAndType 12
CONSTANT_Utf8 1
CONSTANT_MethodHandle 15
CONSTANT_MethodType 16
CONSTANT_InvokeDynamic 18
例如:Utf8_info常量,更多的可以查看規範
CONSTANT_Utf8_info {
u1 tag;
u2 length; //字符串長度
u1 bytes[length]; //字符串數據(utf-8編碼)
}
解析常量池的時候要注意:常量池長度爲 constant_pool_count -1 但是 下標從1開始
屬性表
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
屬性項可以包含在class、method、field、code中,作爲具體信息項
在class中可以描述文件信息,在method中可以描述字節碼內容,函數棧信息,在code中可以描述行號等 所以attribute_info同樣是具備不同類型的變長結構。但是attribute_info並沒有tag這樣的專門標記去標識類型,而是使用名字attribute_name。
//一部分Attribute Name
Attribute Section class file Java SE
ConstantValue §4.7.2 45.3 1.0.2
Code §4.7.3 45.3 1.0.2
StackMapTable §4.7.4 50.0 6
Exceptions §4.7.5 45.3 1.0.2
InnerClasses §4.7.6 45.3 1.1
//.......
Code_attribute結構
Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack; //最大棧深度
u2 max_locals; //局部變量數量
u4 code_length; //字節碼內容長度
u1 code[code_length]; //字節碼內容
u2 exception_table_length; //異常處理表 由try,catch/finaly 產生
{ u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
} exception_table[exception_table_length];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
函數表/字段表
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
//一定會包含一個code屬性項
attribute_info attributes[attributes_count];
}
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
可以看到函數/字段中的內容具體有屬性來描述
字節碼解析
對於class文件解析,我們最關心的可能就兩個 常量池和字節碼
一條字節碼由操作碼,操作數組成,不同的字節碼操作數的長度/表示意義可能不一樣,對着規範翻譯就好
例如invokevirtual字節碼就是 0xb6 u2 2字節的操作數在運行時指向的是一個instance method ref