Ubuntu14.04下64位Java虚拟机编译过程

引言

Java程序员应该都听过HotSpot JVM,它是Sun JDK和Open JDK中所带的虚拟机,也是目前使用范围最广的虚拟机. 不过很多人不知道的是,这个目前“血统纯正”的虚拟机最初并非由Sun公司开发,而是由一家名为”Longview Technologies”的小公司设计的;甚至这个虚拟机最初并非是为Java语言开发的,它来源于Strongtalk VM,而这款虚拟机中相当多的技术又是来源于一款支持Self语言实现”达到C语言50%以上的执行效率”的目标而设计的虚拟机,Sun公司注意到了这款虚拟机在JIM编译上有许多优秀的理念和实际效果,在1997年收购了Longview Technologies公司,从而获得了HotSpot VM.

HotSpot VM继承了Sun之前的两款商用VM的优点,同时也有直接的一些优势,如它名称中的HotSpot指的是它的热点代码探测技术.

为了更好地分析这款目前使用最广的Java虚拟机,首先需要将虚拟机源码进行编译。这里选择jdk7进行分析是基于以下原因:

  • hotspot源代码包含在openjdk7中

  • openjdk7包含了jdk的Java代码,所以要用jdk6对这部分代码进行编译。(有点类似于gcc高版本代码要用gcc低版本编译一样)

1.安装依赖项

安装如下依赖即可:

1
2
$ sudo apt-get install build-essential gawk m4 openjdk-6-jdk libasound2-dev libcups2-dev libxrender-dev xorg-dev xutils-dev X11proto-print-dev binutils libmotifs3 libmotif-dev ant

2.openjdk6安装

由于jdk7的很多feature使用java完成,所以它需要一个老版本的jdk来编译它自己,很多的书或者博客会说安装openjdk6就可以,但实际上由于openjdk的一个bug,导致使用openjdk6会出现以下错误:

1
2
./gamma: relocation error: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.36.x86_64/jre/lib/amd64/libjava.so: symbol JVM_FindClassFromCaller, version SUNWprivate_1.1 not defined in file libjvm.so with link time reference

真正要安装的是Oracel JDK6,我自己安装的是Oracel JDK 1.6.0_45-b06这个版本.至于如何安装Oracle JDK这样简单的问题就不说了,不知道的童鞋可以看看这个:
安装JDK步骤

3.下载openjdk7源码

最开始我是下载Oracle官网上最新的Openjdk源码,但是发现始终编译不过,从出错信息也查不到原因,所以后面使用了另外一个版本的源码(openjdk-7u6-fcs-src-b24-28_aug_2012),可以在这个链接下载:
openjdk7u6

4.编译脚本

在编译前,需要设置一些条件,由于条件非常多,最好将其写成脚本,如下是我的编译脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
export LANG=C
export ALT_BOOTDIR=/usr/lib/jvm/jdk1.6.0_45
export HOST_BUILD_JOBS=6
export ALLOW_DOWNLOADS=true
export USE_PRECOMPILED_HEADER=true
export BUILD_LANGTOOLS=true
export BUILD_JAXP=true
export BUILD_JAXWS=true
export BUILD_CORBA=true
export BUILD_HOTSPOT=true
export BUILD_JDK=true
export SKIP_DEBUG_BUILD=false
export SKIP_FASTDEBUG_BUILD=true
export DEBUG_NAME=debug
BUILD_DEPLOY=false
BUILD_INSTALL=false
export ALT_OUTPUTDIR=/home/allen/Study/CS/Java/jvm/openjdk/build
unset CLASSPATH
unset JAVA_HOME

编写好之后运行一下,然后输入以下命令检查:

1
$ make sanity

如果检查无误的话会出现如下画面:

之后可以简单的输入make,也可以使用以下命令以保存log:

1
2
$ make 2>&1 | tee $ALT_OUTPUTDIR/build.log

5.编译中遇到的问题

一定会遇到的error如下:

1
2
3
4
5
6
7
/opt/jdk1.6.0_31/bin/java -Xmx896m -Xms128m -XX:PermSize=32m -XX:MaxPermSize=160m -jar /root/openjdk6/build/btjars/generatecurrencydata.jar -o /root/openjdk6/build/lib/currency.data.temp \
< ../../../src/share/classes/java/util/CurrencyData.properties
Error: time is more than 10 years from present: 1120165200000
java.lang.RuntimeException: time is more than 10 years from present: 1120165200000
at build.tools.generatecurrencydata.GenerateCurrencyData.makeSpecialCaseEntry(GenerateCurrencyData.java:285)
at build.tools.generatecurrencydata.GenerateCurrencyData.buildMainAndSpecialCaseTables(GenerateCurrencyData.java:225)
at build.tools.generatecurrencydata.GenerateCurrencyData.main(GenerateCurrencyData.java:154)

从出错消息也能判断,这个是由于时间超过10年导致的,这个问题很好解决:通过修改openjdk/jdk/src/share/classes/java/util/CurrencyData.properties文件, 把10年之前的时间修改为10年之内即可(把所有的时间格式的时间改为2015以前的十年以内的年数)。下面是我修改后的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#
# Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
formatVersion=1
# Version of the currency code information in this class.
# It is a serial number that accompanies with each amendment, such as
# 'MAxxx.doc'
dataVersion=151
# List of all valid ISO 4217 currency codes.
# To ensure compatibility, do not remove codes.
all=ADP020-AED784-AFA004-AFN971-ALL008-AMD051-ANG532-AOA973-ARS032-ATS040-AUD036-\
AWG533-AYM945-AZM031-AZN944-BAM977-BBD052-BDT050-BEF056-BGL100-BGN975-BHD048-BIF108-\
BMD060-BND096-BOB068-BOV984-BRL986-BSD044-BTN064-BWP072-BYB112-BYR974-\
BZD084-CAD124-CDF976-CHF756-CLF990-CLP152-CNY156-COP170-CRC188-CSD891-CUP192-CUC931-\
CVE132-CYP196-CZK203-DEM276-DJF262-DKK208-DOP214-DZD012-EEK233-EGP818-\
ERN232-ESP724-ETB230-EUR978-FIM246-FJD242-FKP238-FRF250-GBP826-GEL981-\
GHC288-GHS936-GIP292-GMD270-GNF324-GRD300-GTQ320-GWP624-GYD328-HKD344-HNL340-\
HRK191-HTG332-HUF348-IDR360-IEP372-ILS376-INR356-IQD368-IRR364-ISK352-\
ITL380-JMD388-JOD400-JPY392-KES404-KGS417-KHR116-KMF174-KPW408-KRW410-\
KWD414-KYD136-KZT398-LAK418-LBP422-LKR144-LRD430-LSL426-LTL440-LUF442-\
LVL428-LYD434-MAD504-MDL498-MGA969-MGF450-MKD807-MMK104-MNT496-MOP446-MRO478-\
MTL470-MUR480-MVR462-MWK454-MXN484-MXV979-MYR458-MZM508-MZN943-NAD516-NGN566-\
NIO558-NLG528-NOK578-NPR524-NZD554-OMR512-PAB590-PEN604-PGK598-PHP608-\
PKR586-PLN985-PTE620-PYG600-QAR634-ROL946-RON946-RSD941-RUB643-RUR810-RWF646-SAR682-\
SBD090-SCR690-SDD736-SDG938-SEK752-SGD702-SHP654-SIT705-SKK703-SLL694-SOS706-\
SRD968-SRG740-STD678-SVC222-SYP760-SZL748-THB764-TJS972-TMM795-TMT934-TND788-TOP776-\
TPE626-TRL792-TRY949-TTD780-TWD901-TZS834-UAH980-UGX800-USD840-USN997-USS998-\
UYU858-UZS860-VEB862-VEF937-VND704-VUV548-WST882-XAF950-XAG961-XAU959-XBA955-\
XBB956-XBC957-XBD958-XCD951-XDR960-XFO000-XFU000-XOF952-XPD964-XPF953-\
XPT962-XSU994-XTS963-XUA965-XXX999-YER886-YUM891-ZAR710-ZMK894-ZWD716-ZWL932-ZWN942-ZWR935
# Mappings from ISO 3166 country codes to ISO 4217 currency codes.
#
# Three forms are used:
# Form 1: <country code>=<currency code>
# Form 2: <country code>=<currency code 1>;<time stamp>;<currency code 2>
# Form 3: <country code>=
# Form 1 is used if no future change in currency is known.
# Form 2 indicates that before the specified time currency 1 is used, from
# the specified time currency 2. The time is given in SimpleDateFormat's
# yyyy-MM-dd-HH-mm-ss format in the GMT time zone.
# Form 3 indicates the country doesn't have a currency (the entry is still
# needed to verify that the country code is valid).
#
# The table is based on the following web sites:
# http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/db_en.html
# http://www.bsi-global.com/iso4217currency
# http://www.cia.gov/cia/publications/factbook/indexgeo.html
# AFGHANISTAN
AF=AFN
# \u00c5LAND ISLANDS
AX=EUR
# ALBANIA
AL=ALL
# ALGERIA
DZ=DZD
# AMERICAN SAMOA
AS=USD
# ANDORRA
AD=EUR
# ANGOLA
AO=AOA
# ANGUILLA
AI=XCD
# ANTARCTICA
AQ=
# ANTIGUA AND BARBUDA
AG=XCD
# ARGENTINA
AR=ARS
# ARMENIA
AM=AMD
# ARUBA
AW=AWG
# AUSTRALIA
AU=AUD
# AUSTRIA
AT=EUR
# AZERBAIJAN
AZ=AZM;2008-12-31-20-00-00;AZN
# BAHAMAS
BS=BSD
# BAHRAIN
BH=BHD
# BANGLADESH
BD=BDT
# BARBADOS
BB=BBD
# BELARUS
BY=BYR
# BELGIUM
BE=EUR
# BELIZE
BZ=BZD
# BENIN
BJ=XOF
# BERMUDA
BM=BMD
# Bonaire, Sint Eustatius and Saba
BQ=USD
# BHUTAN
BT=BTN
# BOLIVIA
BO=BOB
# BOSNIA AND HERZEGOVINA
BA=BAM
# BOTSWANA
BW=BWP
# BOUVET ISLAND
BV=NOK
# BRAZIL
BR=BRL
# BRITISH INDIAN OCEAN TERRITORY
IO=USD
# BRUNEI DARUSSALAM
BN=BND
# BULGARIA
BG=BGN
# BURKINA FASO
BF=XOF
# BURUNDI
BI=BIF
# CAMBODIA
KH=KHR
# CAMEROON
CM=XAF
# CANADA
CA=CAD
# CAPE VERDE
CV=CVE
# CAYMAN ISLANDS
KY=KYD
# CENTRAL AFRICAN REPUBLIC
CF=XAF
# CHAD
TD=XAF
# CHILE
CL=CLP
# CHINA
CN=CNY
# CHRISTMAS ISLAND
CX=AUD
# COCOS (KEELING) ISLANDS
CC=AUD
# COLOMBIA
CO=COP
# COMOROS
KM=KMF
# CONGO
CG=XAF
# CONGO, THE DEMOCRATIC REPUBLIC OF THE
CD=CDF
# COOK ISLANDS
CK=NZD
# COSTA RICA
CR=CRC
# COTE D'IVOIRE
CI=XOF
# CROATIA
HR=HRK
# CUBA
CU=CUP
# Cura\u00e7ao
CW=ANG
# CYPRUS
CY=EUR
# CZECH REPUBLIC
CZ=CZK
# DENMARK
DK=DKK
# DJIBOUTI
DJ=DJF
# DOMINICA
DM=XCD
# DOMINICAN REPUBLIC
DO=DOP
# ECUADOR
EC=USD
# EGYPT
EG=EGP
# EL SALVADOR
# USD is also legal currency as of 2001/01/01
SV=SVC
# EQUATORIAL GUINEA
GQ=XAF
# ERITREA
ER=ERN
# ESTONIA
EE=EUR
# ETHIOPIA
ET=ETB
# FALKLAND ISLANDS (MALVINAS)
FK=FKP
# FAROE ISLANDS
FO=DKK
# FIJI
FJ=FJD
# FINLAND
FI=EUR
# FRANCE
FR=EUR
# FRENCH GUIANA
GF=EUR
# FRENCH POLYNESIA
PF=XPF
# FRENCH SOUTHERN TERRITORIES
TF=EUR
# GABON
GA=XAF
# GAMBIA
GM=GMD
# GEORGIA
GE=GEL
# GERMANY
DE=EUR
# GHANA
GH=GHS
# GIBRALTAR
GI=GIP
# GREECE
GR=EUR
# GREENLAND
GL=DKK
# GRENADA
GD=XCD
# GUADELOUPE
GP=EUR
# GUAM
GU=USD
# GUATEMALA
GT=GTQ
# GUERNSEY
GG=GBP
# GUINEA
GN=GNF
# GUINEA-BISSAU
GW=XOF
# GUYANA
GY=GYD
# HAITI
HT=HTG
# HEARD ISLAND AND MCDONALD ISLANDS
HM=AUD
# HOLY SEE (VATICAN CITY STATE)
VA=EUR
# HONDURAS
HN=HNL
# HONG KONG
HK=HKD
# HUNGARY
HU=HUF
# ICELAND
IS=ISK
# INDIA
IN=INR
# INDONESIA
ID=IDR
# IRAN, ISLAMIC REPUBLIC OF
IR=IRR
# IRAQ
IQ=IQD
# IRELAND
IE=EUR
# ISLE OF MAN
IM=GBP
# ISRAEL
IL=ILS
# ITALY
IT=EUR
# JAMAICA
JM=JMD
# JAPAN
JP=JPY
# JERSEY
JE=GBP
# JORDAN
JO=JOD
# KAZAKSTAN
KZ=KZT
# KENYA
KE=KES
# KIRIBATI
KI=AUD
# KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF
KP=KPW
# KOREA, REPUBLIC OF
KR=KRW
# KUWAIT
KW=KWD
# KYRGYZSTAN
KG=KGS
# LAO PEOPLE'S DEMOCRATIC REPUBLIC
LA=LAK
# LATVIA
LV=LVL
# LEBANON
LB=LBP
# LESOTHO
LS=LSL
# LIBERIA
LR=LRD
# LIBYAN ARAB JAMAHIRIYA
LY=LYD
# LIECHTENSTEIN
LI=CHF
# LITHUANIA
LT=LTL
# LUXEMBOURG
LU=EUR
# MACAU
MO=MOP
# MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF
MK=MKD
# MADAGASCAR
MG=MGA
# MALAWI
MW=MWK
# MALAYSIA
MY=MYR
# MALDIVES
MV=MVR
# MALI
ML=XOF
# MALTA
MT=EUR
# MARSHALL ISLANDS
MH=USD
# MARTINIQUE
MQ=EUR
# MAURITANIA
MR=MRO
# MAURITIUS
MU=MUR
# MAYOTTE
YT=EUR
# MEXICO
MX=MXN
# MICRONESIA, FEDERATED STATES OF
FM=USD
# MOLDOVA, REPUBLIC OF
MD=MDL
# MONACO
MC=EUR
# MONGOLIA
MN=MNT
# MONTENEGRO
ME=EUR
# MONTSERRAT
MS=XCD
# MOROCCO
MA=MAD
# MOZAMBIQUE
MZ=MZM;2008-06-30-22-00-00;MZN
# MYANMAR
MM=MMK
# NAMIBIA
NA=NAD
# NAURU
NR=AUD
# NEPAL
NP=NPR
# NETHERLANDS
NL=EUR
# NETHERLANDS ANTILLES
AN=ANG
# NEW CALEDONIA
NC=XPF
# NEW ZEALAND
NZ=NZD
# NICARAGUA
NI=NIO
# NIGER
NE=XOF
# NIGERIA
NG=NGN
# NIUE
NU=NZD
# NORFOLK ISLAND
NF=AUD
# NORTHERN MARIANA ISLANDS
MP=USD
# NORWAY
NO=NOK
# OMAN
OM=OMR
# PAKISTAN
PK=PKR
# PALAU
PW=USD
# PALESTINIAN TERRITORY, OCCUPIED
PS=ILS
# PANAMA
PA=PAB
# PAPUA NEW GUINEA
PG=PGK
# PARAGUAY
PY=PYG
# PERU
PE=PEN
# PHILIPPINES
PH=PHP
# PITCAIRN
PN=NZD
# POLAND
PL=PLN
# PORTUGAL
PT=EUR
# PUERTO RICO
PR=USD
# QATAR
QA=QAR
# REUNION
RE=EUR
# ROMANIA
RO=ROL;2008-06-30-21-00-00;RON
# RUSSIAN FEDERATION
RU=RUB
# RWANDA
RW=RWF
# SAINT BARTHELEMY
BL=EUR
# SAINT HELENA
SH=SHP
# SAINT KITTS AND NEVIS
KN=XCD
# SAINT LUCIA
LC=XCD
# SAINT MARTIN
MF=EUR
# SAINT PIERRE AND MIQUELON
PM=EUR
# SAINT VINCENT AND THE GRENADINES
VC=XCD
# SAMOA
WS=WST
# SAN MARINO
SM=EUR
# SAO TOME AND PRINCIPE
ST=STD
# SAUDI ARABIA
SA=SAR
# SENEGAL
SN=XOF
# SERBIA
RS=RSD
# SERBIA AND MONTENEGRO
CS=CSD
# SEYCHELLES
SC=SCR
# SIERRA LEONE
SL=SLL
# SINGAPORE
SG=SGD
# SLOVAKIA
SK=EUR
# SLOVENIA
SI=EUR
# SOLOMON ISLANDS
SB=SBD
# SOMALIA
SO=SOS
# SOUTH AFRICA
ZA=ZAR
# SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS
GS=GBP
# SPAIN
ES=EUR
# SRI LANKA
LK=LKR
# SUDAN
SD=SDG
# SURINAME
SR=SRD
# SVALBARD AND JAN MAYEN
SJ=NOK
# Sint Maarten (Dutch part)
SX=ANG
# SWAZILAND
SZ=SZL
# SWEDEN
SE=SEK
# SWITZERLAND
CH=CHF
# SYRIAN ARAB REPUBLIC
SY=SYP
# TAIWAN
TW=TWD
# TAJIKISTAN
TJ=TJS
# TANZANIA, UNITED REPUBLIC OF
TZ=TZS
# THAILAND
TH=THB
# TIMOR-LESTE
TL=USD
# TOGO
TG=XOF
# TOKELAU
TK=NZD
# TONGA
TO=TOP
# TRINIDAD AND TOBAGO
TT=TTD
# TUNISIA
TN=TND
# TURKEY
TR=TRL;2009-12-31-22-00-00;TRY
# TURKMENISTAN
TM=TMT
# TURKS AND CAICOS ISLANDS
TC=USD
# TUVALU
TV=AUD
# UGANDA
UG=UGX
# UKRAINE
UA=UAH
# UNITED ARAB EMIRATES
AE=AED
# UNITED KINGDOM
GB=GBP
# UNITED STATES
US=USD
# UNITED STATES MINOR OUTLYING ISLANDS
UM=USD
# URUGUAY
UY=UYU
# UZBEKISTAN
UZ=UZS
# VANUATU
VU=VUV
# VENEZUELA
VE=VEB;2008-01-01-04-00-00;VEF
# VIET NAM
VN=VND
# VIRGIN ISLANDS, BRITISH
VG=USD
# VIRGIN ISLANDS, U.S.
VI=USD
# WALLIS AND FUTUNA
WF=XPF
# WESTERN SAHARA
EH=MAD
# YEMEN
YE=YER
# ZAMBIA
ZM=ZMK
# ZIMBABWE
ZW=ZWL
# List of currencies with 0, 1, OR 3 decimals for minor units, or where there
# are no minor units defined. All others use 2 decimals.
minor0=\
ADP-BEF-BIF-BYB-BYR-CLF-CLP-DJF-ESP-GNF-\
GRD-ISK-ITL-JPY-KMF-KRW-LUF-MGF-PYG-PTE-RWF-\
TPE-TRL-VUV-XAF-XOF-XPF
minor1=
minor3=\
BHD-IQD-JOD-KWD-LYD-OMR-TND
minorUndefined=\
XAG-XAU-XBA-XBB-XBC-XBD-XDR-XFO-XFU-XPD-\
XPT-XSU-XTS-XUA-XXX

5.编译结果

编译成功后会看到以下消息:

至于虚拟机的特性和源码分析等,会在后面的博客陆续进行讲解,敬请期待!