2008年4月9日 星期三

昨天上課

發表了 HW2 & 3
真夠勁,打算用禮拜六日來寫

講到了Class檔的format
如何把java所有資訊塞到class檔.const data, code segement這些東西
Java在執行過程中擁有完整的資訊,所以可以實現Reflection機制
及一些安全性的東西(validation)
JVM在class還沒開始執行前會檢查檔案是否被修改過
由於register是一個stack,它會檢查這個stack在每個instruction的狀態

這節課讓我對JVM的design有初步認識

把一個class dump出來看看,長這樣
這是一種二進位檔的format, 有點像ELF/PE這類的東西
有標頭檔, Data, code segment, 給JVM吃的
其中Code Segment就沒有解碼了



u4 magic; cafebabe
u2 minor_version; 0000
u2 major_version; 0032
u2 constant_pool_count; 0039 (57)

constant_pool:
01: 10: Methodref(class: 15, 29)
02: 9: Field(class: 11, 30)
03: 9: Field(class: 31, 32)
04: 7: Class(33)
05: 10: Methodref(class: 4, 29)
06: 8: String(34)
07: 10: Methodref(class: 4, 35)
08: 10: Methodref(class: 4, 36)
09: 10: Methodref(class: 4, 37)
10: 10: Methodref(class: 38, 39)
11: 7: Class(40)
12: 10: Methodref(class: 11, 41)
13: 10: Methodref(class: 11, 42)
14: 8: String(43)
15: 7: Class(44)
16: 7: Class(45)
17: 1: UTF8(4,"data")
18: 1: UTF8(1,"I")
19: 1: UTF8(6,"")
20: 1: UTF8(4,"(I)V")
21: 1: UTF8(4,"Code")
22: 1: UTF8(15,"LineNumberTable")
23: 1: UTF8(3,"run")
24: 1: UTF8(3,"()V")
25: 1: UTF8(4,"main")
26: 1: UTF8(22,"([Ljava/lang/String;)V")
27: 1: UTF8(10,"SourceFile")
28: 1: UTF8(13,"MyThread.java")
29: 12: NameAndType(19,24)
30: 12: NameAndType(17,18)
31: 7: Class(46)
32: 12: NameAndType(47,48)
33: 1: UTF8(23,"java/lang/String/Builder")
34: 1: UTF8(4,"Run ")
35: 12: NameAndType(49,50)
36: 12: NameAndType(49,51)
37: 12: NameAndType(52,53)
38: 7: Class(54)
39: 12: NameAndType(55,56)
40: 1: UTF8(8,"MyThread")
41: 12: NameAndType(19,20)
42: 12: NameAndType(23,24)
43: 1: UTF8(9,"JVM test!")
44: 1: UTF8(16,"java/lang/Object")
45: 1: UTF8(18,"java/lang/Runnable")
46: 1: UTF8(16,"java/lang/System")
47: 1: UTF8(3,"out")
48: 1: UTF8(21,"Ljava/io/PrintStream;")
49: 1: UTF8(6,"append")
50: 1: UTF8(45,"(Ljava/lang/String;)Ljava/lang/StringBuilder;")
51: 1: UTF8(28,"(I)Ljava/lang/StringBuilder;")
52: 1: UTF8(8,"toString")
53: 1: UTF8(20,"()Ljava/lang/String;")
54: 1: UTF8(19,"java/io/PrintStream")
55: 1: UTF8(7,"println")
56: 1: UTF8(21,"(Ljava/lang/String;)V")

u2 access_flags; 0020
u2 this_class; 000b
u2 super_class; 000f
u2 interfaces_count; 0001
u2 interfaces[interfaces_count]; 0010
u2 fields_count; 0001
field_info fields[fields_count];

filelds[1] {
u2 access_flages; 0000
u2 name_index; 0011
u2 descriptor_index; 0012
u2 attributes_count; 0000
attribute_info attributes;
}

u2 methods_count; 0003

Method[1] {
u2 access_flags; 0000
u2 name_index; 0013
u2 descriptor_index; 0014
u2 attributes_count; 0001
u2 attribute_name_index; 0015
u4 attribute_length; 0000 002a (42)
u1 info[attribute_length];
00 02 00 02 00 00 00 0a 2a b7 00 01 2a 1b b5 00 02 b1 00 00 00 01 00 16 00 00 00 0e 00 03 00 00 00 07 00 04 00 08 00 09 00 09
}



Method[2] {
u2 access_flags; 0001
u2 name_index; 0017
u2 descriptor_index; 0018
u2 attributes_count; 0001
u2 attribute_name_index; 0015
u4 attribute_length; 0000 0039 (57)
u1 info[attribute_length];
00 03 00 01 00 00 00 1d b2 00 03 bb 00 04 59 b7 00 05 12 06 b6 00 07 2a b4 00 02 b6 00 08 b6 00 09 b6 00 0a b1 00 00 00 01 00 16 00 00 00 0a 00 02 00 00 00 0b 00 1c 00 0c
}

Methid[3] {
u2 access_flags; 0009
u2 name_index; 0019
u2 descriptor_index; 001a
u2 attributes_count; 0001
u2 attribute_name_index; 0015
u4 attribute_length; 0000 003b (59)
u1 info[attribute_length];
00 03 00 02 00 00 00 17 bb 00 0b 59 10 0a b7 00 0c 4c 2b b6 00 0d b2 00 03 12 0e b6 00 0a b1 00 00 00 01 00 16 00 00 00 12 00 04 00 00 00 0e 00 0a 00 0f 00 0e 00 10 00 16 00 11
}

Attributes count: 0001
Attributes:

SourceFile_attribute {
u2 attribute_name_index; 001b
u4 attribute_length; 0000 0002
u2 sourcefile_index; 001c
}

沒有留言: