当前位置:网站首页>JVM (IV) Class file structure (complete parsing of bytecode attached)
JVM (IV) Class file structure (complete parsing of bytecode attached)
2022-06-12 10:02:00 【A god given dream never wakes up】
JVM( Four ).Class File structure ( Complete analysis of bytecode attached )
1. summary
The program that the computer executes is 01 The instruction structure ; The code needs to be translated into 01 The binary format can be executed ; But when , The development of virtual machine language in the last decade , More and more programming languages have chosen to work with operating system and Machine instructions Irrelevant format as compiled format ; The compiler compiles the source code into bytecode , Then the virtual machine of the specific system translates the bytecode into executable instructions ;
2. Irrelevance
There are many kinds of instruction sets for computers X86,ARM , Operating system platform Windows ,Linux; therefore Write Once,Run Anywhere Is implemented on the operating system platform ; Virtual machines on different platforms load unified bytecode to realize ;
The basis of independence is the virtual machine and bytecode ,Java Virtual machine load only Class file ,Class There are Java Instruction set of virtual machine , The symbol table , And other supporting information ; Ensure that all platforms are consistent , therefore Class Documents must have many mandatory syntax and institutional constraints ; No matter what source code you write , As long as the compiled language conforms to Class Specification of documents , Can be executed by the virtual machine ( Language independence );

3. Class File structure
Java Technology has always maintained good compatibility .Class The stability of the file structure is indispensable ,Java In the release of more than a dozen large versions , After countless small updates ,Class The details of the document have hardly changed ; Although the virtual machine specification is for Class The file format has been updated , It is also a new extension of the original structure , The original definition is not modified ;
// example class file 16bit View
CAFEBABE 00000034 00150A00 04001109 num:1 16B
00030012 07001307 00140100 01610100 num:2 32B
01490100 063C696E 69743E01 00032829 num:3 48B
56010004 436F6465 01000F4C 696E654E num:4 64B
756D6265 72546162 6C650100 04676574 num:5 80B
41010003 28294901 00047365 74410100 num:6 96B
04284929 5601000A 536F7572 63654669 num:7 112B
6C650100 0748572E 6A617661 0C000700 num:8 128B
080C0005 00060100 0B636F6D 2F696A76 num:9 144B
6D2F4857 0100106A 6176612F 6C616E67 num:10 160B
2F4F626A 65637400 21000300 04000000 num:11 176B
01000200 05000600 00000300 01000700 num:12 192B
08000100 09000000 1D000100 01000000 num:13 208B
052AB700 01B10000 0001000A 00000006 num:14 224B
00010000 00030001 000B000C 00010009 num:15 240B
0000001D 00010001 00000005 2AB40002 num:16 256B
AC000000 01000A00 00000600 01000000 num:17 272B
08000100 0D000E00 01000900 00002200 num:18 288B
02000200 0000062A 1BB50002 B1000000 num:19 304B
01000A00 00000A00 02000000 0C000500 num:20 320B
0D000100 0F000000 020010 num:21 331B
// Related to the source code
package com.ijvm;
public class HW {
private int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
Class The files are grouped in 8 A binary stream in bytes ; Data items are arranged in order , There is no separator in the middle ;
Class The format of the file is : notes u1,u2,u4,u8 respectively 1,2,4,8 An unsigned number of bytes
| type | name | Number |
|---|---|---|
| u4 | magic | 1 |
| u2 | minor_version | 1 |
| u2 | major_version | 1 |
| u2 | constant_pool_count | 1 |
| cp_info | constant_pool | constant_pool_count-1 |
| u2 | access_flags | 1 |
| u2 | this_class | 1 |
| u2 | super_class | 1 |
| u2 | interfaces_count | 1 |
| u2 | interfaces | 1 |
| u2 | field_count | 1 |
| field_info | field | field_count |
| u2 | method_count | 1 |
| method_info | method | method_count |
| u2 | attribute_count | 1 |
| attribute_info | attribute | attribute_count |
3.1 Magic and Class Version of a file
CAFEBABE 00000034
Class In front of the file 4 Bytes oxCAFEBABE , Make sure the file is acceptable to the virtual machine Class file , Because the file extension can be modified at will ;
The four bytes following the magic number are the version number :5~6 Sub version number 0000 7~8 The major version number 0034 The corresponding decimal system is 52;
JDK Version number | Version number |
|---|---|
| 1.1 | 45 |
| 1.2 | 46 |
| 1.3 | 47 |
| 1.4 | 48 |
JDK5 | 49 |
JDK6 | 50 |
JDK7 | 51 |
JDK8 | 52 |
JDK9 | 53 |
You can see 52 The corresponding version is JDK8 That is the JDK8 Or the above version of virtual machine Class file
notes : High version virtual machines support adding parameters to compile code into low version virtual machine class files
javac -target 7 -source 7 HW.java
3.2 Constant pool
The major and minor version numbers are followed by the constant pool ; It is Class The most data associated with other projects in the file structure ;
CAFEBABE 00000034
// Constant pool u2 constant_pool_count 0015->21
0015
0A00 04001109 num:1 16B
00030012 07001307 00140100 01610100 num:2 32B
01490100 063C696E 69743E01 00032829 num:3 48B
Constant pool capacity ox0015 convert to 10 Base number 21; Express 20(constant_pool_count) A constant string ; The index of the string is 1 ~ 21; common 21 individual ;
The constant pool mainly stores two types of information Literal Literal And symbols Symbolic Reference
- Module export or open package Package
- Class and interface fully qualified names Fully Qualified Name
- Field and name descriptors Descriptor
- Method and name descriptors
- Method handle and method type Method Handle Method Type Invoke Dynamic
- Dynamic call points and dynamic constants Dynamically-Computed Call Site , Dynamically-Computed Constant
The item types of the constant pool are : Every constant in the constant pool is a table : The starting position is u1 type The mark of a tag ;
| type | tag | describe | project | digit byte | meaning |
|---|---|---|---|---|---|
CONSTANT_Utf8_info | 1 | Utf8 code String | tag length bytes | 1 2 length | tag Number of string characters utf8 code String u1 Array |
CONSTANT_Integer_info | 3 | integer Literal | tag bytes | 1 4 | tag Store in high order int value |
CONSTANT_Float_info | 4 | floating-point Literal | tag bytes | 1 4 | tag Store in high order float value |
CONSTANT_Long_info | 5 | Long integer Literal | tag bytes | 1 8 | tag Store in high order long value |
CONSTANT_Double_info | 6 | Double precision Literal | tag bytes | 1 8 | tag Store in high order double value |
CONSTANT_Class_info | 7 | Symbolic references to classes and interfaces | tag index | 1 2 | tag Index to a fully qualified constant item |
CONSTANT_String_info | 8 | character string Literal | tag index | 1 2 | tag Index to literal string |
CONSTANT_Fieldref_info | 9 | Field symbol reference | tag index index | 1 2 2 | tagCONSTANT_Class_info The index of CONSTANT_NameAndType_info The index of |
CONSTANT_Methodref_info | 10 | Symbolic references to methods in class | tag index index | 1 2 2 | tagCONSTANT_Class_info The index of CONSTANT_NameAndType_info The index of |
CONSTANT_InterfaceMethodref_info | 11 | Symbolic references to methods in interfaces | tag index index | 1 2 2 | tagCONSTANT_Class_info The index of CONSTANT_NameAndType_info The index of |
CONSTANT_NameAndType_info | 12 | Symbolic references to fields or methods | tag index index | 1 2 2 | tag Index of field or method name Field or method descriptor index |
CONSTANT_MethodHandle_info | 15 | Method handle | tag reference_kind reference_kind | 1 1 2 | tag 1~9 Handle type Constant pool index |
CONSTANT_MethodType_info | 16 | Method type | tag descriptor_kind | 1 2 | tagCONSTANT_Utf8_info The reference index of |
CONSTANT_Dynamic_info | 17 | Calculate constants dynamically | tag index index | 1 2 2 | tag Method index CONSTANT_NameAndType_info The index of |
CONSTANT_InvokeDynamic_info | 18 | Dynamic method call points | tag index index | 1 2 2 | tag Method index CONSTANT_NameAndType_info The index of |
CONSTANT_Module_info | 19 | Represents a module | tag index | 1 2 | tagCONSTANT_Utf8_info The reference index of |
CONSTANT_Package_info | 20 | Open or exported packages in modules | tag index | 1 2 | tagCONSTANT_Utf8_info The reference index of |
It's on it 17 Types of constants ; Each constant type has its own completely independent data structure Example ;
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
analysis :
#1 // Constant pool u2 constant_pool_count 0015->21
0015
//tag = 0A -> 10 CONSTANT_Methodref_info
// 0004-> #4 0011->#17
0A 0004 0011
#2 //tag = 09 -> 9 CONSTANT_Fieldref_info
// 0003-> #3 0012->#18
09 0003 0012
#3 //tag = 07 -> 7 CONSTANT_Class_info
// 0013-> #19
07 0013
#4 //tag = 07 -> 7 CONSTANT_Class_info
// 0014-> #20
07 0014
#5 ~ # 16 CONSTANT_Utf8_info
// a
01 0001 61
01 0001 49
// <init>
01 0006 3C696E69743E
01 0003 282956
01 0004 436F6465
01 000F 4C696E654E756D6265725461626C65
01 0004 67657441
01 0003 282949
01 0004 73657441
01 0004 28492956
01 000A 536F7572636546696C65
// HW.java
01 0007 48572E6A617661
#17 //tag = 0c -> 12 CONSTANT_NameAndType_info
// 0007-> #7 0008->#8
0C 0007 0008
#18 //tag = 0c -> 12 CONSTANT_NameAndType_info
// 0007-> #7 0008->#8
0C 0005 0006
#19 // com/ijvm/HW
01 000B 636F6D2F696A766D2F4857
#20 // java/lang/Object
01 0010 6A6176612F6C616E672F4F626A656374
Verify that the resolution was successful Use Built in command parsing
javap -v HW.class
Classfile /D:/Resource/study/src/main/java/com/ijvm/HW.class
Last modified 2022-2-6; size 331 bytes
MD5 checksum f794f59e60ead2b659a9a4b1f8079a7f
Compiled from "HW.java"
public class com.ijvm.HW
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
// contrast The above analysis is consistent
Constant pool:
#1 = Methodref #4.#17 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#18 // com/ijvm/HW.a:I
#3 = Class #19 // com/ijvm/HW
#4 = Class #20 // java/lang/Object
#5 = Utf8 a
#6 = Utf8 I
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 getA
#12 = Utf8 ()I
#13 = Utf8 setA
#14 = Utf8 (I)V
#15 = Utf8 SourceFile
#16 = Utf8 HW.java
#17 = NameAndType #7:#8 // "<init>":()V
#18 = NameAndType #5:#6 // a:I
#19 = Utf8 com/ijvm/HW
#20 = Utf8 java/lang/Object
a I <init> V LineNumberTable And other strings are automatically generated by the compiler , Describe something that is not convenient to express in fixed bytes
3.3 Access signs
After the end of the measuring cell , And then one u2 Represents the access flag access_flags: Used to represent access information at the class or interface level , Identify whether it is a class or an interface , Whether it is public,abstract ,final
| Logo name | Flag value tag | meaning |
|---|---|---|
ACC_PUBLIC | 0x0001 | Whether it is public |
ACC_FINAL | 0x0010 | Whether it is final |
ACC_SUPER | 0x0020 | 1.0.2 After that, it's all true |
ACC_INTERFACE | 0x0200 | Whether it is Interface |
ACC_ABSTRACT | 0x0400 | Whether it is abstract class |
ACC_SYNTHETIC | 0x1000 | Non user code generation |
ACC_ANNOTATION | 0x2000 | Whether it is annotation |
ACC_ENUM | 0x4000 | Whether it is enumeration |
ACC_MODULE | 0x8000 | Whether it is A module |
Instance class ACC_PUBLIC、ACC_SUPER The sign should be true , Other signs should be false ;access_flags The value should be :0x0001|0x0020=0x0021
// Constant pool end position
2F4F626A 656374
// Access signs 0x0021
00 21
000300 04000000
01000200 05000600 00000300 01000700
08000100 09000000 1D000100 01000000
052AB700 01B10000 0001000A 00000006
00010000 00030001 000B000C 00010009
3.4 Class index 、 Parent index and interface index set
One Class There are the following three to determine the inheritance relationship of a class
- Class index this_class
u2 typeThe data of : The fully qualified name of the class ; Point to a type ofCONSTANT_Class_infoClass descriptor constant for - The parent class is called super_class
u2 typeThe data of : The fully qualified name of the parent class ;Java It's single inheritance , exceptjava.lang.Objectoutside , be-all Java Class has a parent class ; Point to a type ofCONSTANT_Class_infoClass descriptor constant for - Interface index set ( A class may implement multiple interfaces ) It's a
u2 typeSet ; The data structure of a collection is generallyu2 Number, Then there is the data ;
// Access signs 0x0021
00 21
//this_class super_class Look in the constant pool #3 #4
// #3 = Class #19
// #4 = Class #20
// #19 = Utf8 com/ijvm/HW this_class
// #20 = Utf8 java/lang/Object
0003 0004
// Interface index set The length is 0 That is to say, the interfaces that are not implemented subsequently
0000
00 01000200 05000600 00000300 01000700
08000100 09000000 1D000100 01000000
052AB700 01B10000 0001000A 00000006
.......
3.5 Field table aggregate
Field table Field_info Describe variables declared in interfaces or classes ; There are several directions ;
- Scope public private protected Modifier
- Class level (static) And instance level variables
- variability final
- Concurrent visibility volatile
- Can be serialized transient
- Type of field Basic types object Array
Data structure of field table
| type | name | Number | meaning |
|---|---|---|---|
| u2 | access_flags | 1 | Access signs |
| u2 | name_index | 1 | Field name |
| u2 | descriptor_index | 1 | Field descriptors |
| u2 | attributes_count | 1 | Number of attributes |
| attribute_info | attributes | attributes_count |
// A field
0001
//#### 3.5.1 access_flags 0002 -> public
0002
// 3.5.2 name_index Application of constant pool a
0005
// 3.5.3 descriptor_index Application of constant pool I -> int type
0006
//attributes_count 0000
0000
000300 01000700
08000100 09000000 1D000100 01000000
052AB700 01B10000 0001000A 00000006
.......
3.5.1 access_flags
access_flags And the above class access_flags similar
| Logo name | Flag value tag | meaning |
|---|---|---|
ACC_PUBLIC | 0x0001 | Whether it is public |
ACC_PRIVATE | 0x0002 | Whether it is private |
ACC_PROTESTED | 0x0004 | Whether it is protected |
ACC_STATIC | 0x0008 | Whether it is static |
ACC_FINAL | 0x0010 | Whether it is final |
ACC_VOLATILE | 0x0040 | Whether it is volatile |
ACC_TRANSIENT | 0x0080 | Whether it is transient |
ACC_SYNTHETIC | 0x1000 | Whether it is The compiler generates |
ACC_ENUM | 0x4000 | Whether it is enumeration |
3.5.2 name_index
This is a reference to the constant pool ;
3.5.3 descriptor_index
This is a reference to the constant pool ; The resulting data is of the following types ; For example, the example code arrives at I
| Identification characters | meaning | Identification characters | meaning |
|---|---|---|---|
| B | byte | J | long |
| C | char | S | short |
| D | double | Z | boolean |
| F | float | V | void |
| I | int | L | object type |
3.6 Method table aggregate
Class File store The description of the method in the format is almost exactly the same as that of the field , The structure of a method table is like a field table , In turn, it includes the access flag access_flags、 Index of names name_index、 Descriptor index descriptor_index、 Property sheet aggregate attributes Several items ;
// Number of methods 0x0003
0003
// Access signs public Method name #7 <init> Method Descriptor #8 ()V
0001 0007 0008
// The number of arguments Parameter name #9 ->Code
0001 0009
000000 1D000100 01000000
052AB700 01B10000 0001000A 00000006
.......
Data structure of method table
| type | name | Number | meaning |
|---|---|---|---|
| u2 | access_flags | 1 | Access signs |
| u2 | name_index | 1 | Method name |
| u2 | descriptor_index | 1 | Method Descriptor |
| u2 | attributes_count | 1 | Number of attributes |
| attribute_info | attributes | attributes_count |
3.6.1 access_flags
access_flags It is a little different from the field , such as volatile Just not on the method
| Logo name | Flag value tag | meaning |
|---|---|---|
ACC_PUBLIC | 0x0001 | Whether it is public |
ACC_PRIVATE | 0x0002 | Whether it is private |
ACC_PROTESTED | 0x0004 | Whether it is protected |
ACC_STATIC | 0x0008 | Whether it is static |
ACC_FINAL | 0x0010 | Whether it is final |
ACC_SYNCHRONIZED | 0x0020 | Whether it is synchronized |
ACC_BRIDEG | 0x0040 | Whether it is Compiler bridging method |
ACC_VARARGS | 0x0080 | Whether it is Variable parameters |
ACC_NATIVE | 0x0100 | Whether it is native |
ACC_ABSTRACT | 0x0400 | Whether it is abstract |
ACC_STRICT | 0x0800 | Whether it is strictfp |
ACC_SYNTHETIC | 0x1000 | Whether it is The compiler generates |
3.6.2 name_index descriptor_index attributes_count
name_index descriptor_index attributes_count Same field
3.6.3 attribute_info
Below
3.7 Property sheet aggregate
Property sheet (attribute_info) It has appeared several times ,Class file 、 Field table 、 Methods and tables are OK Carry your own property sheet collection , To describe some scenario specific information ; The set of property sheets is slightly more restrictive some , It is no longer required that each property sheet has a strict order
| The attribute name | Use location | meaning |
|---|---|---|
Code | Method table | Java Bytecode instructions for code compilation |
ConstantValue | Field table | from final Constant value defined by keyword |
Deprecated | Class method table | Statement Deprecated |
Exceptions | Method table | Method throws a list of exceptions |
LineNumberTable | Code attribute | Java Source code line number and byte code correspondence |
SourceFile | Class file | Record source file name |
LocalVariableTable | Code attribute | Local variable description |
InnerClass | Class file | Inner class list |
StackMapTable | Code attribute | JDK6 Add a new attribute after , Verify that the types match |
synthetic | class , Method table , Field table | Identity is automatically generated by the compiler |
3.7.1 Code
Java The code in the method body of the program goes through Javac After the compiler processes , Finally, the bytecode instructions are stored in Code Within the properties
Code The data structure of the table
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | Fix Code |
| u4 | attribute_length | 1 | Property sheet length -u2-u4; Back length |
| u2 | max_stack | 1 | Method Descriptor |
| u2 | max_locals | 1 | Local variable table length , Variable slot 32bit A variable slot ; That's all 2 Variable slot |
| u4 | code_length | 1 | Bytecode length ; The theory is 2^32, The actual use 65535 Maximum (u2) |
| u1 | code | code_length | Code u1 Set u1=0x00~0xFF The decimal system is 0~255 Can express 256 Orders |
| u2 | exception_table_length | 1 | Number of exception tables |
| exception_info | exception_table | exception_table_length | Abnormal information |
| u2 | attributes_count | 1 | Number of attributes |
| attribute_info | attributes | attributes_count | Property sheet |
Exception table data structure ( Interview test return value )
| type | name | Number | meaning |
|---|---|---|---|
| u2 | start_pc | 1 | try Start |
| u2 | end_pc | 1 | try end |
| u2 | handle_pc | 1 | The exception is to jump to handle_pc |
| u2 | catch_type | 1 | Exception types by 0 when Any exception to handle_pc |
// Example 1 inc() normal 1 abnormal 2 ;
public int inc() {
int x;
try {
x = 1;
// int i = 2/0;
return x;
} catch (Exception e) {
x = 2;
// int i = 2/0;
return x;
} finally {
x = 3;
}
}
** notes : When the return value is a basic type return Instructions will be copied x To Variable slot of local variable table Inside , hinder finally The modification in is x Not at all Variable slot Data in , So the return is 2 No 3, If it's a reference type There is no process of copying **
// Number of methods 0x0003
0003
// Access signs public Method name #7 <init> Method Descriptor #8 ()V
0001 0007 0008
// The number of arguments Parameter name #9 ->Code
0001 0009
//code length = 0x0000001D = 29
0000001D
//max_stack max_locals
0001 0001
//code_length
00000005
2A B7 00 01 B1
// A byte An order Five instructions
0 2A: The first reference type is a local variable
1 B7: Call superclass constructor , Instance initialization method , Private method
2 00: Don't do anything?
3 01: take null Push to top of stack
4 B1: Return from current method void
//
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
//exception_table_length exception_info because exception_table_length = 0 No,
0000
// 3.7.3 LineNumberTable attribute
//attributes_count One
0001
// Code attribute 000A->LineNumberTable length 00000006
000A 00000006
// The number of line numbers corresponds to 0001 Contrast relationship 0000:0003
0001 0000 0003
Be careful : stack=1, locals=1, args_size=1 Why? args_size=1 It's because of putting this It also becomes a parameter , If it is static The method of decoration Will be for 0
3.7.2 Exceptions
Code An attribute of the same rank ; List the checked exceptions that may be thrown in the method (Checked Exceptions); The method is described in throws Exception listed after keyword ;
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | Exceptions |
| u4 | attribute_length | 1 | The length added up later u2+ u2*number_of_exceptions |
| u2 | number_of_exceptions | 1 | exception The number of |
| u2 | exception_index_table | number_of_exceptions | CONSTANT_Class_info The index of the type constant |
3.7.3 LineNumberTable
LineNumberTable Property is used to describe Java Source code line number and bytecode line number ( The offset of the bytecode ) The correspondence between , By default, it will be generated to Class In the document . Optional , Javac Use in -g:none or -g:lines Option to cancel or request the generation of this information .
If you choose not to generate LineNumberTable attribute , The most important impact on program operation Ring is when an exception is thrown , The wrong line number will not be displayed on the stack , And when debugging the program , You can't set breakpoints according to the source line .
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | LineNumberTable |
| u4 | attribute_length | 1 | The length added up later u2+(u2+u2)* line_number_table_length |
| u2 | line_number_table_length | 1 | line_number_table The number of |
| line_number_info(u2+u2) | line_number_table | line_number_table_length | every last line_number_table; Bytecode line number , The latter is Java Source |
3.7.4 LocalVariableTable And LocalVariableTypeTable attribute
LocalVariableTable: Describes the variables and values of the local variable table in the stack frame Java The relationship between variables defined in the source code ; Optional , stay Javac Use in -g:none or -g:vars Option to cancel or request the generation of this information . If this property is not generated , All parameter names will be lost ,IDE Will use things like arg0、arg1 Such placeholders replace the original parameter names , The program operation has no effect , But it will bring great inconvenience to code writing , And during debugging, the parameter value cannot be obtained from the context according to the parameter name ;
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | LocalVariableTable |
| u4 | attribute_length | 1 | The length added up later |
| u2 | local_variable_table_length | 1 | local_variable_table The number of |
| local_variable_info | local_variable_table | local_variable_table_length | every last local_variable_table |
local_variable_info structure
| type | name | Number | meaning |
|---|---|---|---|
| u2 | start_pc | 1 | Variable scope start |
| u2 | length | 1 | Offset after the start of variable scope |
| u2 | name_index | 1 | CONSTANT_Utf8_info The index of the type constant |
| u2 | descriptor | 1 | CONSTANT_Utf8_info The index of the type constant |
| u2 | index | 1 | Position of the variable slot in the local variable table ; yes 64 Bit type ; The variable slot is index and index+1 Two |
3.7.5 SourceFile And SourceDebugExtension attribute
SourceFile Property is used to record the generation of this Class File source file name ; Optional ; Do not generate this property , When an exception is thrown , Stack The file name to which the error code belongs... Will not be displayed
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | SourceFile |
| u4 | attribute_length | 1 | Attribute length |
| u2 | source_index | 1 | Indexes |
SourceDebugExtension Property is used to store additional code debugging information . A typical scenario is when JSP file debugging
3.7.6 ConstantValue attribute
ConstantValue Property is used to inform the virtual machine to automatically assign values to static variables . Only by static Keyword modifies the variable ( Class variables ) To use this property . Right static Variable of type ( That is, instance variables ) The assignment of is in the instance constructor () Method ; And for class variables , There are two ways to choose : stay class constructor () Method or use ConstantValue attribute
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | ConstantValue |
| u4 | attribute_length | 1 | Attribute length |
| u2 | constant_value_index | 1 | Indexes |
3.7.6 InnerClasses attribute
InnerClasses Property is used to record the association between the internal class and the host class . If an inner class is defined in a class , Then the compiler will generate... For it and the inner classes it contains InnerClasses attribute
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | InnerClasses |
| u4 | attribute_length | 1 | Attribute length |
| u2 | number_of_class | 1 | Indexes |
| inner_class_info | inner_class | number_of_class |
inner_class_info structure
| type | name | Number | meaning |
|---|---|---|---|
| u2 | inner_class_info_index | 1 | CONSTANT_Class_info The index of the type constant |
| u2 | outer_class_info_index | 1 | CONSTANT_Class_info The index of the type constant |
| u2 | inner_class_index | 1 | CONSTANT_Utf8_info The index of the type constant |
| u2 | inner_class_access_flags | number_of_class | Class like access_flags |
3.7.8 Deprecated and Synthetic
Deprecated and Synthetic: Boolean properties , There is only a difference between with and without , No property values ;
Deprecated Property is used to represent a class 、 Fields or methods , It has been determined by the program author that it is no longer recommended to use , It can be used in code “@deprecated” Comments to set
Synthetic Property represents this field or method, not by Java The source code is generated directly , It's added by the compiler
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | Deprecated/Synthetic |
| u4 | attribute_length | 1 | Value must be 0x00000000, Because there is no attribute value to set |
3.7.9 .Signature attribute
Signature Attribute in JDK 5 Add to Class In the document specification ; class 、 Field In the property table of the table and method table structure ; Greatly enhanced Java Grammar of language , After that , Any class 、 Interface 、 Initializes the generic signature of a method or member if it contains a type variable (Type Variable) Or parameterized type Parameterized Type, be Signature Property will be for it Record generic signature information . Bytecode (Code attribute ) Compile all generic information in ( Type variable 、 Parameterized types ) After compilation, it is all Erase ; For example, you need to use this signature information area to verify the type of added data ;
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | Signature |
| u4 | attribute_length | 1 | Value must be 0x00000000, Because there is no attribute value to set |
| u2 | signature_index | 1 | CONSTANT_Utf8_info The index of |
3.7.10 . Runtime annotation related properties
Runtime**Annotations * There are many kinds of signs Runtime annotations
| type | name | Number | meaning |
|---|---|---|---|
| u2 | attribute_name_index | 1 | InnerClasses |
| u4 | attribute_length | 1 | Attribute length |
| u2 | number_of_annotations | 1 | Indexes |
| annotation | annotations | number_of_annotations | annotations Each element in represents A note visible at runtime |
annotation structure
| type | name | Number | meaning |
|---|---|---|---|
| u2 | type_index | 1 | pool CONSTANT_Utf8_info Index value of constant |
| u2 | num_element_value_pairs | 1 | The number of key value pairs in the annotation |
| element_value_pair | element_value_pairs | 1 | Key value pair |
4. Bytecode instruction
Java Virtual machine instructions : opcode + Parameters ( Operands )
opcode :Opcode A byte 0x00 ~ 0xFF 0~255 ; Each of these numbers corresponds to an operation / Instruction code ; So in theory, the biggest one is 256 Orders ;
Parameters ( Operands ): Most instructions do not contain operands , There is only one opcode , Instructions are stored in the operand stack ;Class The file has no length adjustment on the storage operand
Example 1
// Source code
this.a = 1111;
//class file 2A:aload_0
2A
//11:sipush Put a short integer constant value (-32768~32767) Push to top of stack This is followed by two digit parameters 0457 Stored
// But when reading, it comes one by one 0457 0x04 << 8 | 0x57 = Get a short integer 0x0457 = Decimal system 1111
11 0457
// Bytecode instruction
ALOAD 0
SIPUSH 1111
Example 2
// Source code
this.a = 10;
//class file 2A:aload_0
2A
//10:bipush The constant value of a single byte (-128~127) Push to top of stack And then 1 Bit parameters 0A Stored
10 0A
// Bytecode instruction
ALOAD 0
BIPUSH 10
Pseudo code execution instructions
do{
pc register +1;
pc The register value removes the opcode from the byte code stream ;
if( opcode Yes Operands ) Fetch operand from byte code stream ;
Perform the operation ;
}while( Byte stream length > 0 )
4.1 Bytecode & data type
stay Java Instruction set of virtual machine , Most instructions contain data type information corresponding to their operations ; stay Java Instruction set of virtual machine , Most instructions contain data type information corresponding to their operations ; The sub instruction does not support integer type byte、char and short, There's not even any instruction support boolean type , Most of them are for boolean、byte、short and char Operation of type data , In fact, they all use corresponding pairs int Type as operation type Computational Type To carry out
4.2 Load and store instructions
Load a local variable into the operation stack Tload [ _<n> ]; Example :iload ,iload ,lload ,lload_<n> n : 0 1 2 3
Store a value from the operand stack to the local variable table Tstore [ _<n> ];
Load a constant onto the operand stack Tconst [ _<T> ]bipush ,sipush,ldc;
4.3 Operation instruction
Not directly supported byte、short、char and boolean Type of arithmetic instruction , Use operation int Type of instruction instead of
- Add instruction :
iadd、ladd、fadd、dadd - Subtraction instructions :
isub、lsub、fsub、dsub - Multiplication instructions :
imul、lmul、fmul、dmul - Division instructions :
idiv、ldiv、fdiv、ddiv - Order for redundancy :
irem、lrem、frem、drem - Reverse instruction :
ineg、lneg、fneg、dneg - Displacement command :
ishl、ishr、iushr、lshl、lshr、lushr - Press bit or command :
ior、lor - Press bit and command :
iand、land - Bitwise XOR command :
ixor、lxor - Local variable auto increment instruction :
iinc - Comparison instruction :
dcmpg、dcmpl、fcmpg、fcmpl、lcmp
4.4 Type conversion instructions
Java Virtual machine directly supports ( That is to say, there is no explicit conversion instruction ) The following generalized type conversions for numeric types (Widening Numeric Conversion, That is, the safe conversion from small scope type to large scope type ; such as int -> long 、double
Narrowing type conversion (Narrowing Numeric Conversion) when , You must explicitly use conversion instructions to accomplish , These conversion instructions include i2b、i2c、i2s、l2i、f2i、f2l、d2i、d2l and d2f
4.5 Object creation and access instructions
Instances and arrays are created using different bytecode instructions
- Instructions for creating class instances :new
- Instructions for creating arrays :
newarray、anewarray、multianewarrayAccess class fields (static Field , Or called a class variable ) And instance fields ( Not static Field , Or called instance variable ) Of Instructions :getfield、putfield、getstatic、putstatic - An instruction to load an array element into the operand stack :
baload、caload、saload、iaload、laload、faload、 daload、aaload - An instruction that stores the value of an operand stack in an array element :
bastore、castore、sastore、iastore、fastore、aastore - The instruction to get the length of the array :
arraylength - Check class instance type instructions :
instanceof、checkcast
4.6 Operand stack management instructions
One or two elements at the top of the stack are out of the stack :
pop、pop2Swap the two values at the top of the stack :swap
Copy one or two values at the top of the stack and push the copied values or double copied values back into the top of the stack :
dup、dup2、dup_x1、 dup2_x1、dup_x2、dup2_x2
4.7 Control transfer instructions
Give Way Java A virtual machine commands conditionally or unconditionally from a specified location ( Instead of controlling transfer instructions ) Next to An instruction continues to execute the program , Understand... From a conceptual model , It can be considered that the control instruction is modified conditionally or unconditionally PC Register value ;
- Conditional branch :
ifeq、iflt、ifle、ifne、ifgt、ifge、ifnull、ifnonnull、if_icmpeq、if_icmpne、if_icmplt、 if_icmpgt、if_icmple、if_icmpge、if_acmpeq and if_acmpne - Compound conditional branch :
tableswitch、lookupswitch - An unconditional branch :
goto、goto_w、jsr、jsr_w、ret
boolean type 、byte type 、char The type and short Type of conditional branch comparison operation , All use int Type of comparison instruction ;long type 、float The type and double Type of conditional branch comparison operation , The corresponding type of comparison operation instruction will be executed first dcmpg、dcmpl、fcmpg、fcmpl、lcmp,
4.8 Method calls and return instructions
Method calls and Data type independent
invokevirtualInstructions : Used to invoke The instance method of the object , Dispatch based on the actual type of object ( Virtual method dispatch ).invokeinterfaceInstructions : Used to invoke Interface method , It searches at run time for an object that implements this interface method , Find the right method to call .invokespecialInstructions : Used to call some instance methods that need special handling , Include Instance initialization method 、 Private methods and Parent class method .invokestaticInstructions : Used to call class Static methods (static Method ).invokedynamicInstructions : Used to dynamically resolve the method referenced by the call point qualifier at run time . And implement the method .
Method return and Data types are related to
boolean、byte、char、short int The return value of the ireturn 、lreturn、freturn、dreturn and areturn, return void, Instance initialization method 、 Class initialization of classes and interfaces , Use return Instructions ;
4.9 Exception handling instructions
Operation that throws an exception athrow Instructions
Handling exceptions (catch sentence ) Not by bytecode instructions , The exception table is used to complete ;
4.10 Synchronization instructions
Java Virtual machines can support Method level synchronization and Synchronization of a sequence of instructions within a method ;
The synchronous instruction set sequence consists of Java In language synchronized Statement block to represent ,Java Instruction set of virtual machine Yes monitorenter and monitorexit Two instructions to support synchronized The semantics of keywords , Correct implementation synchronized Keywords need Javac compiler ( The compiler will automatically generate an exception handler to execute monitorexit) And Java The two virtual machines work together to support
// Source code
void fun (HW3 hw3){
synchronized (hw3){
fun2();
}
}
// Bytecode
fun(Lcom/ijvm/HW3;)V
TRYCATCHBLOCK L0 L1 L2 null //L0 ~ L1 error Jump L2
TRYCATCHBLOCK L2 L3 L2 null //L2 ~ L3 error Jump L2 Guarantee monitorexit It will be carried out
L4
LINENUMBER 11 L4
ALOAD 1
DUP
ASTORE 2
MONITORENTER // Lock Instructions To (ASTORE 2 ) On the object
L0
LINENUMBER 12 L0
ALOAD 0
INVOKEVIRTUAL com/ijvm/HW3.fun2 ()V
L5
LINENUMBER 13 L5
ALOAD 2
MONITOREXIT // Normal execution : Unlock Instructions To (ASTORE 2 ) On the object
L1
GOTO L6
// Method exception flow
L2
FRAME FULL [com/ijvm/HW3 com/ijvm/HW3 java/lang/Object] [java/lang/Throwable]
ASTORE 3 // Storage exception
ALOAD 2
MONITOREXIT // Exception execution : Unlock Instructions To (ASTORE 2 ) On the object Guarantee monitorexit It will be carried out
L3
ALOAD 3 // It is very difficult to get out Throw out
ATHROW
L6
LINENUMBER 14 L6
FRAME CHOP 1
RETURN
notes : Whether the method is abnormal or not monitorenter Instructions are executed accordingly monitorexit Instructions .
5. Public design , Private implementation
《Java Virtual machine specification 》 It depicts Java What virtual machines should be common Program storage format :Class File format and bytecode instruction set ; There are many ways to implement virtual machines , however Class The file and its corresponding instructions are unique ;
6. Appendix bytecode
// Related to the source code
package com.ijvm;
public class HW {
private int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
Complete data
// example class file 16bit View
CAFEBABE 00000034 00150A00 04001109 num:1 16B
00030012 07001307 00140100 01610100 num:2 32B
01490100 063C696E 69743E01 00032829 num:3 48B
56010004 436F6465 01000F4C 696E654E num:4 64B
756D6265 72546162 6C650100 04676574 num:5 80B
41010003 28294901 00047365 74410100 num:6 96B
04284929 5601000A 536F7572 63654669 num:7 112B
6C650100 0748572E 6A617661 0C000700 num:8 128B
080C0005 00060100 0B636F6D 2F696A76 num:9 144B
6D2F4857 0100106A 6176612F 6C616E67 num:10 160B
2F4F626A 65637400 21000300 04000000 num:11 176B
01000200 05000600 00000300 01000700 num:12 192B
08000100 09000000 1D000100 01000000 num:13 208B
052AB700 01B10000 0001000A 00000006 num:14 224B
00010000 00030001 000B000C 00010009 num:15 240B
0000001D 00010001 00000005 2AB40002 num:16 256B
AC000000 01000A00 00000600 01000000 num:17 272B
08000100 0D000E00 01000900 00002200 num:18 288B
02000200 0000062A 1BB50002 B1000000 num:19 304B
01000A00 00000A00 02000000 0C000500 num:20 320B
0D000100 0F000000 020010 num:21 331B
Parsing data
magic number :
CAFEBABE
Small version minor version:
0000
The big version JDK edition major version :
00034 // ->52 JDK8
Constant pool Number :0015 -> 21 On behalf of 20 individual 1~20
#1 0A->Methodref (1 2 2 )
0A 0004 0011 //Methodref #4 #17
#2 09 Fieldref (1 2 2 )
09 0003 0012 //Fieldref #3 #18
#3 07 Class (1 2 )
07 0013 //Class #19
#4 07 Class (1 2 )
07 0014 //Class #20
#5 ~ #16 --> 01 CONSTANT_Utf8_info( Back Utf8 Ellipsis )(1 2 1 )
01 0001 61 //Utf8 1 a
01 0001 49 //Utf8 1 I
01 0006 3C696E69743E //Utf8 6 <init>
01 0003 282956 //Utf8 3 ()V
01 0004 436F6465 //Utf8 4 Code
01 000F 4C696E654E756D6265725461626C65 //Utf8 15 LineNumberTable
01 0004 67657441 //Utf8 4 getA
01 0003 282949 //Utf8 3 ()I
01 0004 73657441 //Utf8 4 setA
01 0004 28492956 //Utf8 4 (I)V
01 000A 536F7572636546696C65 //Utf8 10 SourceFile
01 0007 48572E 6A617661 //Utf8 7 HW.java
#17 0C -> NameAndType (1 2 2 )
0C 0007 0008 // NameAndType #7:#8
#18 0C -> NameAndType (1 2 2 )
0C 0005 0006 // NameAndType #5:#6
#19 --> 01 CONSTANT_Utf8_info
01 000B 636F6D2F696A766D2F4857 //Utf8 11 com/ijvm/HW
#20 --> 01 CONSTANT_Utf8_info
01 0010 6A6176612F6C616E672F4F626A656374 //Utf8 16 java/lang/Object
// Class description 0x0001|0x0020=0x0021
0021 // The access flag of the class is ACC_PUBLIC
0003 0004 // Class index ->(#3)-> (#19)-> com/ijvm/HW 、 Parent index ->(#4)->(#20)->java/lang/Object
0000 // The interface index has no
// Field description
0001 // There is always one field
# First field
0002 //private
0005 // The attribute name a
0006 // Attribute types I Express int
0000 // There are no other properties There can be annotation information here What? Can exist here
// Methods described
0003 // There are three methods With the default constructor
// The first method
0001 //public
0007 // Method name <init>
0008 // Return value ()V
0001 // An attribute
// Property start 0009->code (2 4 2 2 4 1*n 2 2*m 2 2*j)
0009
0000001D // Subsequent length 29
0001 0001 // Stack depth Local variable table
00000005 2AB70001B1 // Bytecode instruction length Bytecode instruction
0000 // The number of exceptions 0 therefore n = 0
0001 // Number of attributes Code Nested properties in
//#10 ->LineNumberTable Row correspondence (2 4 2 (2+2)*n)
000A
00000006 0001 0000 0003 // Subsequent length 6 0001 Two corresponding relations The first pair 0:3
// Second method
0001 //public
000B //getA
000C //()I
0001 // An attribute
// Property start 0009->code (2 4 2 2 4 1*n 2 2*m 2 2*j)
0009
0000001D // Subsequent length 29
0001 0001 // Stack depth Local variable table
00000005 2AB40002AC // Bytecode instruction length Bytecode instruction
0000 // The number of exceptions 0 therefore n = 0
0001 // Number of attributes Code Nested properties in
//#10 ->LineNumberTable Row correspondence (2 4 2 (2+2)*n)
000A
00000006 0001 0000 0008 // Subsequent length 6 0001 Two corresponding relations The first pair 0:3
// Third way
0001 //public
000D //setA
000E //(I)V
0001 // An attribute
// Property start 0009->code (2 4 2 2 4 1*n 2 2*m 2 2*j)
0009
00000022 // Subsequent length 29
0002 0002 // Stack depth Local variable table
00000006 2A1BB50002B1 // Bytecode instruction length Bytecode instruction
0000 // The number of exceptions 0 therefore n = 0
0001 // Number of attributes Code Nested properties in
//#10 ->LineNumberTable Row correspondence (2 4 2 (2+2)*n)
000A
0000000A 0002 0000 000C 0005 000D // Subsequent length 10 0002 Two corresponding relations The first pair 0:12 Second pairs 5:13
Additional information
0001 // Number 0001
//#15 000F->SourceFile (2 4 2)
000F 00000002 0010 // The additional information is the source file length 2 0010->#16 HW.java
边栏推荐
- Create simple windowing programs using Visual Studio 2017
- Research on autojs wechat: the control IP in wechat on different versions of wechat or simulators is different.
- string类对象的访问及遍历操作
- 枚举所有USB设备代码
- [cloud native] establishment of Eureka service registration
- MySQL优化之慢日志查询
- UEFI EDKII 编程学习
- UE4_以现成资源探索创建背景场景的方法
- Briefly introduce the difference between threads and processes
- 哈希表的理论讲解
猜你喜欢
随机推荐
《要领》读书笔记
How to do industry analysis
Jetpack architecture component learning (3) -- activity results API usage
Li Yang, a scientific and technological innovator and CIO of the world's top 500 group: the success of digital transformation depends on people. Decision makers should always focus on "firewood"
002:数据湖有哪些特征
C language recursive folder code
UE4_以现成资源探索创建背景场景的方法
Abstract classes and interfaces
[cloud native] establishment of Eureka service registration
Create simple windowing programs using Visual Studio 2017
III Regular expression to finite state automata: NFA to DFA
Access and traversal of string class objects
一文读懂Dfinity生态中的首个NFT平台:IMPOSSIBLE THINGS
枚举所有USB设备代码
Storage R & D Engineer Recruitment
Auto.js调试:使用雷电模拟器的网络模式进行调试
Auto. JS learning notes 7: JS file calls functions and variables in another JS file to solve various problems of call failure
MySQL index FAQs
5种最常见的CEPH失败方案
Web3.0与数字时尚,该如何落地?









