1 /* 2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 26 package jdk.tools.jaotc.binformat; 27 28 import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT; 29 30 import java.io.ByteArrayOutputStream; 31 import java.io.DataOutputStream; 32 import java.io.IOException; 33 import java.util.ArrayList; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 38 import jdk.tools.jaotc.binformat.Symbol.Binding; 39 import jdk.tools.jaotc.binformat.Symbol.Kind; 40 import jdk.tools.jaotc.binformat.elf.JELFRelocObject; 41 import jdk.tools.jaotc.binformat.macho.JMachORelocObject; 42 import jdk.tools.jaotc.binformat.pecoff.JPECoffRelocObject; 43 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 44 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; 45 import org.graalvm.compiler.options.OptionValues; 46 47 /** 48 * A format-agnostic container class that holds various components of a binary. 49 * 50 * <p> 51 * This class holds information necessary to create platform-specific binary containers such as 52 * ELFContainer for Linux and Solaris operating systems or MachOContainer for Mac OS or PEContainer 53 * for MS Windows operating systems. 54 * 55 * <p> 56 * Method APIs provided by this class are used to construct and populate platform-independent 57 * contents of a binary as the first step to create a binary representation of code generated by a 58 * compiler backend such as Graal. 59 * 60 * <p> 61 * Methods to record and access code section contents, symbols and relocations are provided. 62 */ 63 public final class BinaryContainer implements SymbolTable { 64 private final OptionValues graalOptions; 65 66 private final int codeSegmentSize; 67 68 private final int codeEntryAlignment; 69 70 private final boolean threadLocalHandshakes; 71 72 /** 73 * Container holding code bits and any other related information. 74 */ 75 private final CodeContainer codeContainer; 76 77 /** 78 * Container holding global offset data for hotspot linkage. 79 */ 80 private final ByteContainer extLinkageGOTContainer; 81 82 /** 83 * Patched by HotSpot, contains Klass pointers. 84 */ 85 private final ByteContainer klassesGotContainer; 86 87 /** 88 * Patched by HotSpot, contains MethodCounters pointers. 89 */ 90 private final ByteContainer countersGotContainer; 91 92 /** 93 * Patched lazily by hotspot, contains klass/method pointers. 94 */ 95 private final ByteContainer metadataGotContainer; 96 97 /** 98 * BSS container, contains method state array. 99 */ 100 private final ByteContainer methodStateContainer; 101 102 /** 103 * Patched by hotspot, contains java object pointers. 104 */ 105 private final ByteContainer oopGotContainer; 106 107 // Containers holding read-only data 108 private final ReadOnlyDataContainer configContainer; 109 private final ReadOnlyDataContainer metaspaceNamesContainer; 110 private final ReadOnlyDataContainer methodsOffsetsContainer; 111 private final ReadOnlyDataContainer klassesOffsetsContainer; 112 private final ReadOnlyDataContainer klassesDependenciesContainer; 113 private final HeaderContainer headerContainer; 114 private final ReadOnlyDataContainer stubsOffsetsContainer; 115 private final ReadOnlyDataContainer codeSegmentsContainer; 116 117 // This cannot be read only since we need to patch the metadata at runtime.. 118 private final ReadOnlyDataContainer methodMetadataContainer; 119 120 /** 121 * Container containing constant data used by code. 122 */ 123 private final ReadOnlyDataContainer constantDataContainer; 124 125 /** 126 * Map holding the Strings table. 127 */ 128 private final Map<String, Integer> offsetStringTable = new HashMap<>(); 129 130 private final Map<String, Integer> metaspaceNames = new HashMap<>(); 131 132 // List of relocation table entries - (symbolName, relocationInfo) 133 private final Map<String, Symbol> symbolTable = new HashMap<>(); 134 private final Map<Symbol, List<Relocation>> relocationTable = new HashMap<>(); 135 private final Map<Symbol, Relocation> uniqueRelocationTable = new HashMap<>(); 136 137 /** 138 * Mapping of local VM function names to known global symbols generated in the output binary. 139 */ 140 private static final HashMap<String, String> functionNamesToAOTSymbols = new HashMap<>(); 141 142 //@formatter:off 143 private static final String[][] map = { 144 {"CompilerToVM::Data::SharedRuntime_deopt_blob_unpack", "_aot_deopt_blob_unpack"}, 145 {"CompilerToVM::Data::SharedRuntime_deopt_blob_uncommon_trap", "_aot_deopt_blob_uncommon_trap"}, 146 {"CompilerToVM::Data::SharedRuntime_ic_miss_stub", "_aot_ic_miss_stub"}, 147 {"CompilerToVM::Data::SharedRuntime_handle_wrong_method_stub", "_aot_handle_wrong_method_stub"}, 148 {"SharedRuntime::exception_handler_for_return_address", "_aot_exception_handler_for_return_address"}, 149 {"SharedRuntime::register_finalizer", "_aot_register_finalizer"}, 150 {"SharedRuntime::OSR_migration_end", "_aot_OSR_migration_end"}, 151 {"SharedRuntime::enable_stack_reserved_zone", "_aot_enable_stack_reserved_zone"}, 152 {"CompilerRuntime::resolve_dynamic_invoke", "_aot_resolve_dynamic_invoke"}, 153 {"CompilerRuntime::resolve_string_by_symbol", "_aot_resolve_string_by_symbol"}, 154 {"CompilerRuntime::resolve_klass_by_symbol", "_aot_resolve_klass_by_symbol"}, 155 {"CompilerRuntime::resolve_method_by_symbol_and_load_counters", "_aot_resolve_method_by_symbol_and_load_counters"}, 156 {"CompilerRuntime::initialize_klass_by_symbol", "_aot_initialize_klass_by_symbol"}, 157 {"CompilerRuntime::invocation_event", "_aot_invocation_event"}, 158 {"CompilerRuntime::backedge_event", "_aot_backedge_event"}, 159 160 {"CompilerToVM::Data::dpow", "_aot_shared_runtime_dpow"}, 161 {"CompilerToVM::Data::dexp", "_aot_shared_runtime_dexp"}, 162 {"CompilerToVM::Data::dcos", "_aot_shared_runtime_dcos"}, 163 {"CompilerToVM::Data::dsin", "_aot_shared_runtime_dsin"}, 164 {"CompilerToVM::Data::dtan", "_aot_shared_runtime_dtan"}, 165 {"CompilerToVM::Data::dlog", "_aot_shared_runtime_dlog"}, 166 {"CompilerToVM::Data::dlog10", "_aot_shared_runtime_dlog10"}, 167 168 {"StubRoutines::_jbyte_arraycopy", "_aot_stub_routines_jbyte_arraycopy"}, 169 {"StubRoutines::_jshort_arraycopy", "_aot_stub_routines_jshort_arraycopy"}, 170 {"StubRoutines::_jint_arraycopy", "_aot_stub_routines_jint_arraycopy"}, 171 {"StubRoutines::_jlong_arraycopy", "_aot_stub_routines_jlong_arraycopy"}, 172 {"StubRoutines::_oop_arraycopy", "_aot_stub_routines_oop_arraycopy"}, 173 {"StubRoutines::_oop_arraycopy_uninit", "_aot_stub_routines_oop_arraycopy_uninit"}, 174 175 {"StubRoutines::_jbyte_disjoint_arraycopy", "_aot_stub_routines_jbyte_disjoint_arraycopy"}, 176 {"StubRoutines::_jshort_disjoint_arraycopy", "_aot_stub_routines_jshort_disjoint_arraycopy"}, 177 {"StubRoutines::_jint_disjoint_arraycopy", "_aot_stub_routines_jint_disjoint_arraycopy"}, 178 {"StubRoutines::_jlong_disjoint_arraycopy", "_aot_stub_routines_jlong_disjoint_arraycopy"}, 179 {"StubRoutines::_oop_disjoint_arraycopy", "_aot_stub_routines_oop_disjoint_arraycopy"}, 180 {"StubRoutines::_oop_disjoint_arraycopy_uninit", "_aot_stub_routines_oop_disjoint_arraycopy_uninit"}, 181 182 {"StubRoutines::_arrayof_jbyte_arraycopy", "_aot_stub_routines_arrayof_jbyte_arraycopy"}, 183 {"StubRoutines::_arrayof_jshort_arraycopy", "_aot_stub_routines_arrayof_jshort_arraycopy"}, 184 {"StubRoutines::_arrayof_jint_arraycopy", "_aot_stub_routines_arrayof_jint_arraycopy"}, 185 {"StubRoutines::_arrayof_jlong_arraycopy", "_aot_stub_routines_arrayof_jlong_arraycopy"}, 186 {"StubRoutines::_arrayof_oop_arraycopy", "_aot_stub_routines_arrayof_oop_arraycopy"}, 187 {"StubRoutines::_arrayof_oop_arraycopy_uninit", "_aot_stub_routines_arrayof_oop_arraycopy_uninit"}, 188 189 {"StubRoutines::_arrayof_jbyte_disjoint_arraycopy", "_aot_stub_routines_arrayof_jbyte_disjoint_arraycopy"}, 190 {"StubRoutines::_arrayof_jshort_disjoint_arraycopy", "_aot_stub_routines_arrayof_jshort_disjoint_arraycopy"}, 191 {"StubRoutines::_arrayof_jint_disjoint_arraycopy", "_aot_stub_routines_arrayof_jint_disjoint_arraycopy"}, 192 {"StubRoutines::_arrayof_jlong_disjoint_arraycopy", "_aot_stub_routines_arrayof_jlong_disjoint_arraycopy"}, 193 {"StubRoutines::_arrayof_oop_disjoint_arraycopy", "_aot_stub_routines_arrayof_oop_disjoint_arraycopy"}, 194 {"StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit", "_aot_stub_routines_arrayof_oop_disjoint_arraycopy_uninit"}, 195 196 {"StubRoutines::_unsafe_arraycopy", "_aot_stub_routines_unsafe_arraycopy"}, 197 198 {"StubRoutines::_checkcast_arraycopy", "_aot_stub_routines_checkcast_arraycopy"}, 199 200 {"StubRoutines::_generic_arraycopy", "_aot_stub_routines_generic_arraycopy"}, 201 202 {"StubRoutines::_aescrypt_encryptBlock", "_aot_stub_routines_aescrypt_encryptBlock"}, 203 {"StubRoutines::_aescrypt_decryptBlock", "_aot_stub_routines_aescrypt_decryptBlock"}, 204 {"StubRoutines::_cipherBlockChaining_encryptAESCrypt", "_aot_stub_routines_cipherBlockChaining_encryptAESCrypt"}, 205 {"StubRoutines::_cipherBlockChaining_decryptAESCrypt", "_aot_stub_routines_cipherBlockChaining_decryptAESCrypt"}, 206 {"StubRoutines::_electronicCodeBook_encryptAESCrypt", "_aot_stub_routines_electronicCodeBook_encryptAESCrypt"}, 207 {"StubRoutines::_electronicCodeBook_decryptAESCrypt", "_aot_stub_routines_electronicCodeBook_decryptAESCrypt"}, 208 {"StubRoutines::_updateBytesCRC32", "_aot_stub_routines_update_bytes_crc32"}, 209 {"StubRoutines::_crc_table_adr", "_aot_stub_routines_crc_table_adr"}, 210 211 {"StubRoutines::_sha1_implCompress", "_aot_stub_routines_sha1_implCompress" }, 212 {"StubRoutines::_sha1_implCompressMB", "_aot_stub_routines_sha1_implCompressMB" }, 213 {"StubRoutines::_sha256_implCompress", "_aot_stub_routines_sha256_implCompress" }, 214 {"StubRoutines::_sha256_implCompressMB", "_aot_stub_routines_sha256_implCompressMB" }, 215 {"StubRoutines::_sha512_implCompress", "_aot_stub_routines_sha512_implCompress" }, 216 {"StubRoutines::_sha512_implCompressMB", "_aot_stub_routines_sha512_implCompressMB" }, 217 {"StubRoutines::_multiplyToLen", "_aot_stub_routines_multiplyToLen" }, 218 219 {"StubRoutines::_counterMode_AESCrypt", "_aot_stub_routines_counterMode_AESCrypt" }, 220 {"StubRoutines::_ghash_processBlocks", "_aot_stub_routines_ghash_processBlocks" }, 221 {"StubRoutines::_base64_encodeBlock", "_aot_stub_routines_base64_encodeBlock" }, 222 {"StubRoutines::_crc32c_table_addr", "_aot_stub_routines_crc32c_table_addr" }, 223 {"StubRoutines::_updateBytesCRC32C", "_aot_stub_routines_updateBytesCRC32C" }, 224 {"StubRoutines::_updateBytesAdler32", "_aot_stub_routines_updateBytesAdler32" }, 225 {"StubRoutines::_squareToLen", "_aot_stub_routines_squareToLen" }, 226 {"StubRoutines::_mulAdd", "_aot_stub_routines_mulAdd" }, 227 {"StubRoutines::_montgomeryMultiply", "_aot_stub_routines_montgomeryMultiply" }, 228 {"StubRoutines::_montgomerySquare", "_aot_stub_routines_montgomerySquare" }, 229 {"StubRoutines::_vectorizedMismatch", "_aot_stub_routines_vectorizedMismatch" }, 230 231 {"StubRoutines::_throw_delayed_StackOverflowError_entry", "_aot_stub_routines_throw_delayed_StackOverflowError_entry" }, 232 233 234 {"os::javaTimeMillis", "_aot_os_javaTimeMillis"}, 235 {"os::javaTimeNanos", "_aot_os_javaTimeNanos"}, 236 237 {"JVMCIRuntime::monitorenter", "_aot_jvmci_runtime_monitorenter"}, 238 {"JVMCIRuntime::monitorexit", "_aot_jvmci_runtime_monitorexit"}, 239 {"JVMCIRuntime::object_notify", "_aot_object_notify"}, 240 {"JVMCIRuntime::object_notifyAll", "_aot_object_notifyAll"}, 241 {"JVMCIRuntime::log_object", "_aot_jvmci_runtime_log_object"}, 242 {"JVMCIRuntime::log_printf", "_aot_jvmci_runtime_log_printf"}, 243 {"JVMCIRuntime::vm_message", "_aot_jvmci_runtime_vm_message"}, 244 {"JVMCIRuntime::new_instance", "_aot_jvmci_runtime_new_instance"}, 245 {"JVMCIRuntime::new_array", "_aot_jvmci_runtime_new_array"}, 246 {"JVMCIRuntime::new_multi_array", "_aot_jvmci_runtime_new_multi_array"}, 247 {"JVMCIRuntime::dynamic_new_instance", "_aot_jvmci_runtime_dynamic_new_instance"}, 248 {"JVMCIRuntime::dynamic_new_array", "_aot_jvmci_runtime_dynamic_new_array"}, 249 {"JVMCIRuntime::new_instance_or_null", "_aot_jvmci_runtime_new_instance_or_null"}, 250 {"JVMCIRuntime::new_array_or_null", "_aot_jvmci_runtime_new_array_or_null"}, 251 {"JVMCIRuntime::new_multi_array_or_null", "_aot_jvmci_runtime_new_multi_array_or_null"}, 252 {"JVMCIRuntime::dynamic_new_instance_or_null", "_aot_jvmci_runtime_dynamic_new_instance_or_null"}, 253 {"JVMCIRuntime::dynamic_new_array_or_null", "_aot_jvmci_runtime_dynamic_new_array_or_null"}, 254 {"JVMCIRuntime::log_primitive", "_aot_jvmci_runtime_log_primitive"}, 255 {"JVMCIRuntime::validate_object", "_aot_jvmci_runtime_validate_object"}, 256 {"JVMCIRuntime::write_barrier_pre", "_aot_jvmci_runtime_write_barrier_pre"}, 257 {"JVMCIRuntime::identity_hash_code", "_aot_jvmci_runtime_identity_hash_code"}, 258 {"JVMCIRuntime::write_barrier_post", "_aot_jvmci_runtime_write_barrier_post"}, 259 {"JVMCIRuntime::thread_is_interrupted", "_aot_jvmci_runtime_thread_is_interrupted"}, 260 {"JVMCIRuntime::exception_handler_for_pc", "_aot_jvmci_runtime_exception_handler_for_pc"}, 261 {"JVMCIRuntime::test_deoptimize_call_int", "_aot_jvmci_runtime_test_deoptimize_call_int"}, 262 263 {"JVMCIRuntime::throw_and_post_jvmti_exception", "_aot_jvmci_runtime_throw_and_post_jvmti_exception"}, 264 {"JVMCIRuntime::throw_klass_external_name_exception", "_aot_jvmci_runtime_throw_klass_external_name_exception"}, 265 {"JVMCIRuntime::throw_class_cast_exception", "_aot_jvmci_runtime_throw_class_cast_exception"}, 266 267 {"JVMCIRuntime::vm_error", "_aot_jvmci_runtime_vm_error"} 268 }; 269 //@formatter:on 270 271 static { 272 for (String[] entry : map) { 273 functionNamesToAOTSymbols.put(entry[0], entry[1]); 274 } 275 } 276 277 /** 278 * Allocates a {@code BinaryContainer} object whose content will be generated in a file with the 279 * prefix {@code prefix}. It also initializes internal code container, symbol table and 280 * relocation tables. 281 * 282 * @param graalOptions 283 */ 284 public BinaryContainer(OptionValues graalOptions, GraalHotSpotVMConfig graalHotSpotVMConfig, GraphBuilderConfiguration graphBuilderConfig, int gc, String jvmVersion) { 285 this.graalOptions = graalOptions; 286 287 this.codeSegmentSize = graalHotSpotVMConfig.codeSegmentSize; 288 if (codeSegmentSize < 1 || codeSegmentSize > 1024) { 289 throw new InternalError("codeSegmentSize is not in range [1, 1024] bytes: (" + codeSegmentSize + "), update JPECoffRelocObject"); 290 } 291 if ((codeSegmentSize & (codeSegmentSize - 1)) != 0) { 292 throw new InternalError("codeSegmentSize is not power of 2: (" + codeSegmentSize + "), update JPECoffRelocObject"); 293 } 294 295 this.codeEntryAlignment = graalHotSpotVMConfig.codeEntryAlignment; 296 297 this.threadLocalHandshakes = graalHotSpotVMConfig.threadLocalHandshakes; 298 299 // Section unique name is limited to 8 characters due to limitation on Windows. 300 // Name could be longer but only first 8 characters are stored on Windows. 301 302 // read only, code 303 codeContainer = new CodeContainer(".text", this); 304 305 // read only, info 306 headerContainer = new HeaderContainer(jvmVersion, new ReadOnlyDataContainer(".header", this)); 307 configContainer = new ReadOnlyDataContainer(".config", this); 308 metaspaceNamesContainer = new ReadOnlyDataContainer(".meta.names", this); 309 methodsOffsetsContainer = new ReadOnlyDataContainer(".meth.offsets", this); 310 klassesOffsetsContainer = new ReadOnlyDataContainer(".kls.offsets", this); 311 klassesDependenciesContainer = new ReadOnlyDataContainer(".kls.dependencies", this); 312 313 stubsOffsetsContainer = new ReadOnlyDataContainer(".stubs.offsets", this); 314 codeSegmentsContainer = new ReadOnlyDataContainer(".code.segments", this); 315 constantDataContainer = new ReadOnlyDataContainer(".meth.constdata", this); 316 methodMetadataContainer = new ReadOnlyDataContainer(".meth.metadata", this); 317 318 // writable sections 319 oopGotContainer = new ByteContainer(".oop.got", this); 320 klassesGotContainer = new ByteContainer(".kls.got", this); 321 countersGotContainer = new ByteContainer(".cnt.got", this); 322 metadataGotContainer = new ByteContainer(".meta.got", this); 323 methodStateContainer = new ByteContainer(".meth.state", this); 324 extLinkageGOTContainer = new ByteContainer(".got.linkage", this); 325 326 addGlobalSymbols(); 327 328 recordConfiguration(graalHotSpotVMConfig, graphBuilderConfig, gc); 329 } 330 331 private void recordConfiguration(GraalHotSpotVMConfig graalHotSpotVMConfig, GraphBuilderConfiguration graphBuilderConfig, int gc) { 332 // @Checkstyle: stop 333 // @formatter:off 334 boolean[] booleanFlags = { graalHotSpotVMConfig.cAssertions, // Debug VM 335 graalHotSpotVMConfig.useCompressedOops, 336 graalHotSpotVMConfig.useCompressedClassPointers, 337 graalHotSpotVMConfig.compactFields, 338 graalHotSpotVMConfig.useTLAB, 339 graalHotSpotVMConfig.useBiasedLocking, 340 TieredAOT.getValue(graalOptions), 341 graalHotSpotVMConfig.enableContended, 342 graalHotSpotVMConfig.restrictContended, 343 graphBuilderConfig.omitAssertions(), 344 graalHotSpotVMConfig.threadLocalHandshakes 345 }; 346 347 int[] intFlags = { graalHotSpotVMConfig.getOopEncoding().getShift(), 348 graalHotSpotVMConfig.getKlassEncoding().getShift(), 349 graalHotSpotVMConfig.contendedPaddingWidth, 350 graalHotSpotVMConfig.fieldsAllocationStyle, 351 1 << graalHotSpotVMConfig.logMinObjAlignment(), 352 graalHotSpotVMConfig.codeSegmentSize, 353 gc 354 }; 355 // @formatter:on 356 // @Checkstyle: resume 357 358 byte[] booleanFlagsAsBytes = flagsToByteArray(booleanFlags); 359 int size0 = configContainer.getByteStreamSize(); 360 361 // @formatter:off 362 int computedSize = booleanFlagsAsBytes.length * Byte.BYTES + // size of boolean flags 363 intFlags.length * Integer.BYTES + // size of int flags 364 Integer.BYTES; // size of the "computedSize" 365 366 configContainer.appendInt(computedSize). 367 appendInts(intFlags). 368 appendBytes(booleanFlagsAsBytes); 369 // @formatter:on 370 371 int size = configContainer.getByteStreamSize() - size0; 372 assert size == computedSize; 373 } 374 375 private static byte[] flagsToByteArray(boolean[] flags) { 376 byte[] byteArray = new byte[flags.length]; 377 for (int i = 0; i < flags.length; ++i) { 378 byteArray[i] = boolToByte(flags[i]); 379 } 380 return byteArray; 381 } 382 383 private static byte boolToByte(boolean flag) { 384 return (byte) (flag ? 1 : 0); 385 } 386 387 /** 388 * Free some memory. 389 */ 390 public void freeMemory() { 391 offsetStringTable.clear(); 392 metaspaceNames.clear(); 393 } 394 395 /* 396 * Global symbol names in generated DSO corresponding to VM's symbols. VM needs to look up this 397 * symbol in DSO and link it with VM's corresponding symbol: store VM's symbol address or value 398 * in the named GOT cell. 399 */ 400 401 public static String getCardTableAddressSymbolName() { 402 return "_aot_card_table_address"; 403 } 404 405 public static String getHeapTopAddressSymbolName() { 406 return "_aot_heap_top_address"; 407 } 408 409 public static String getHeapEndAddressSymbolName() { 410 return "_aot_heap_end_address"; 411 } 412 413 public static String getCrcTableAddressSymbolName() { 414 return "_aot_stub_routines_crc_table_adr"; 415 } 416 417 public static String getPollingPageSymbolName() { 418 return "_aot_polling_page"; 419 } 420 421 public static String getResolveStaticEntrySymbolName() { 422 return "_resolve_static_entry"; 423 } 424 425 public static String getResolveVirtualEntrySymbolName() { 426 return "_resolve_virtual_entry"; 427 } 428 429 public static String getResolveOptVirtualEntrySymbolName() { 430 return "_resolve_opt_virtual_entry"; 431 } 432 433 public static String getNarrowKlassBaseAddressSymbolName() { 434 return "_aot_narrow_klass_base_address"; 435 } 436 437 public static String getNarrowOopBaseAddressSymbolName() { 438 return "_aot_narrow_oop_base_address"; 439 } 440 441 public static String getLogOfHeapRegionGrainBytesSymbolName() { 442 return "_aot_log_of_heap_region_grain_bytes"; 443 } 444 445 public static String getInlineContiguousAllocationSupportedSymbolName() { 446 return "_aot_inline_contiguous_allocation_supported"; 447 } 448 449 public int getCodeSegmentSize() { 450 return codeSegmentSize; 451 } 452 453 public int getCodeEntryAlignment() { 454 return codeEntryAlignment; 455 } 456 457 public boolean getThreadLocalHandshakes() { 458 return threadLocalHandshakes; 459 } 460 461 /** 462 * Gets the global AOT symbol associated with the function name. 463 * 464 * @param functionName function name 465 * @return AOT symbol for the given function name, or null if there is no mapping. 466 */ 467 public static String getAOTSymbolForVMFunctionName(String functionName) { 468 return functionNamesToAOTSymbols.get(functionName); 469 } 470 471 private void addGlobalSymbols() { 472 // Create global symbols for all containers. 473 createContainerSymbol(codeContainer); 474 createContainerSymbol(configContainer); 475 createContainerSymbol(methodsOffsetsContainer); 476 createContainerSymbol(klassesOffsetsContainer); 477 createContainerSymbol(klassesDependenciesContainer); 478 createContainerSymbol(klassesGotContainer); 479 createContainerSymbol(countersGotContainer); 480 createContainerSymbol(metadataGotContainer); 481 createContainerSymbol(methodStateContainer); 482 createContainerSymbol(oopGotContainer); 483 createContainerSymbol(metaspaceNamesContainer); 484 createContainerSymbol(methodMetadataContainer); 485 createContainerSymbol(stubsOffsetsContainer); 486 createContainerSymbol(headerContainer.getContainer()); 487 createContainerSymbol(codeSegmentsContainer); 488 489 createGotSymbol(getResolveStaticEntrySymbolName()); 490 createGotSymbol(getResolveVirtualEntrySymbolName()); 491 createGotSymbol(getResolveOptVirtualEntrySymbolName()); 492 createGotSymbol(getCardTableAddressSymbolName()); 493 createGotSymbol(getHeapTopAddressSymbolName()); 494 createGotSymbol(getHeapEndAddressSymbolName()); 495 createGotSymbol(getNarrowKlassBaseAddressSymbolName()); 496 createGotSymbol(getNarrowOopBaseAddressSymbolName()); 497 createGotSymbol(getPollingPageSymbolName()); 498 createGotSymbol(getLogOfHeapRegionGrainBytesSymbolName()); 499 createGotSymbol(getInlineContiguousAllocationSupportedSymbolName()); 500 501 for (HashMap.Entry<String, String> entry : functionNamesToAOTSymbols.entrySet()) { 502 createGotSymbol(entry.getValue()); 503 } 504 } 505 506 /** 507 * Creates a global symbol of the form {@code "A" + container name}. Note, linker on Windows 508 * does not allow names which start with '.' 509 * 510 * @param container container to create a symbol for 511 */ 512 private static void createContainerSymbol(ByteContainer container) { 513 container.createSymbol(0, Kind.OBJECT, Binding.GLOBAL, 0, "A" + container.getContainerName()); 514 } 515 516 /** 517 * Creates a global GOT symbol of the form {@code "got." + name}. 518 * 519 * @param name name for the GOT symbol 520 */ 521 private void createGotSymbol(String name) { 522 String s = "got." + name; 523 Symbol gotSymbol = extLinkageGOTContainer.createGotSymbol(s); 524 extLinkageGOTContainer.createSymbol(gotSymbol.getOffset(), Kind.OBJECT, Binding.GLOBAL, 8, name); 525 } 526 527 /** 528 * Create a platform-specific binary file representing the content of the 529 * {@code BinaryContainer} object. 530 * 531 * This method is called after creating and performing any necessary changes to the contents of 532 * code stream, symbol tables and relocation tables is completely finalized 533 * 534 * @param outputFileName name of output file 535 * 536 * @throws IOException in case of file creation failure 537 */ 538 public void createBinary(String outputFileName) throws IOException { 539 String osName = System.getProperty("os.name"); 540 switch (osName) { 541 case "Linux": 542 case "SunOS": 543 JELFRelocObject elfobj = JELFRelocObject.newInstance(this, outputFileName); 544 elfobj.createELFRelocObject(relocationTable, symbolTable.values()); 545 break; 546 case "Mac OS X": 547 JMachORelocObject machobj = new JMachORelocObject(this, outputFileName); 548 machobj.createMachORelocObject(relocationTable, symbolTable.values()); 549 break; 550 default: 551 if (osName.startsWith("Windows")) { 552 JPECoffRelocObject pecoffobj = new JPECoffRelocObject(this, outputFileName); 553 pecoffobj.createPECoffRelocObject(relocationTable, symbolTable.values()); 554 break; 555 } else { 556 throw new InternalError("Unsupported platform: " + osName); 557 } 558 } 559 } 560 561 /** 562 * Add symbol to the symbol table. If the existing symbol is undefined and the specified symbol 563 * is not undefined, replace the existing symbol information with that specified. 564 * 565 * @param symInfo symbol information to be added 566 */ 567 @Override 568 public void addSymbol(Symbol symInfo) { 569 if (symInfo.getName().startsWith("got.") && !(symInfo instanceof GotSymbol)) { 570 throw new InternalError("adding got. without being GotSymbol"); 571 } 572 if (symbolTable.containsKey(symInfo.getName())) { 573 throw new InternalError("Symbol: " + symInfo.getName() + " already exists in SymbolTable"); 574 } else { 575 // System.out.println("# Symbol [" + name + "] [" + symInfo.getValue() + "] [" + 576 // symInfo.getSection().getContainerName() + "] [" + symInfo.getSize() + "]"); 577 symbolTable.put(symInfo.getName(), symInfo); 578 } 579 } 580 581 public boolean addStringOffset(String name, Integer offset) { 582 offsetStringTable.put(name, offset); 583 return true; 584 } 585 586 /** 587 * Add relocation entry for {@code symName}. Multiple relocation entries for a given symbol may 588 * exist. 589 * 590 * @param info relocation information to be added 591 */ 592 public void addRelocation(Relocation info) { 593 // System.out.println("# Relocation [" + info.getSymbol() + "] [" + info.getOffset() + "] [" 594 // + 595 // info.getSection().getContainerName() + "] [" + info.getSymbol().getName() + "] [" + 596 // info.getSymbol().getOffset() + " @ " + info.getSymbol().getSection().getContainerName() + 597 // "]"); 598 if (relocationTable.containsKey(info.getSymbol())) { 599 relocationTable.get(info.getSymbol()).add(info); 600 } else if (uniqueRelocationTable.containsKey(info.getSymbol())) { 601 // promote 602 ArrayList<Relocation> list = new ArrayList<>(2); 603 list.add(uniqueRelocationTable.get(info.getSymbol())); 604 list.add(info); 605 relocationTable.put(info.getSymbol(), list); 606 uniqueRelocationTable.remove(info.getSymbol()); 607 } else { 608 uniqueRelocationTable.put(info.getSymbol(), info); 609 } 610 } 611 612 /** 613 * Get symbol with name {@code symName}. 614 * 615 * @param symName name of symbol for which symbol table information is being queried 616 * @return success or failure of insertion operation 617 */ 618 @Override 619 public Symbol getSymbol(String symName) { 620 return symbolTable.get(symName); 621 } 622 623 @Override 624 public Symbol createSymbol(int offset, Kind kind, Binding binding, int size, String name) { 625 if (kind != Kind.NATIVE_FUNCTION) { 626 throw new UnsupportedOperationException("Must be external functions: " + name); 627 } 628 Symbol symbol = new Symbol(offset, kind, binding, null, size, name); 629 addSymbol(symbol); 630 return symbol; 631 } 632 633 /** 634 * Get offset in got section with name {@code symName}. 635 * 636 * @param name for which String table information is being queried 637 * @return success or failure of insertion operation 638 */ 639 public Integer getStringOffset(String name) { 640 return offsetStringTable.get(name); 641 } 642 643 /** 644 * Insert {@code targetCode} to code stream with {@code size} at {@code offset}. 645 * 646 * @param targetCode byte array of native code 647 * @param offset offset at which {@code targetCode} is to be inserted 648 * @param size size of {@code targetCode} 649 */ 650 private static void appendBytes(ByteContainer byteContainer, byte[] targetCode, int offset, int size) { 651 byteContainer.appendBytes(targetCode, offset, size); 652 } 653 654 public void appendCodeBytes(byte[] targetCode, int offset, int size) { 655 appendBytes(codeContainer, targetCode, offset, size); 656 } 657 658 public void appendIntToCode(int value) { 659 codeContainer.appendInt(value); 660 } 661 662 public int appendExtLinkageGotBytes(byte[] bytes, int offset, int size) { 663 int startOffset = extLinkageGOTContainer.getByteStreamSize(); 664 appendBytes(extLinkageGOTContainer, bytes, offset, size); 665 return startOffset; 666 } 667 668 public void addMetadataGotEntry(int offset) { 669 metadataGotContainer.appendLong(offset); 670 } 671 672 public int addMetaspaceName(String name) { 673 Integer value = metaspaceNames.get(name); 674 if (value != null) { 675 return value.intValue(); 676 } 677 // Get the current length of the stubsNameContainer 678 // align on 8-byte boundary 679 int nameOffset = alignUp(metaspaceNamesContainer, 8); 680 681 try { 682 // Add the name of the symbol to the .stubs.names section 683 // Modify them to sequence of utf8 strings with length: 684 // "<u2_size>Ljava/lang/ThreadGroup;<u2_size>addUnstarted<u2_size>()V" 685 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 686 DataOutputStream out = new DataOutputStream(bout); 687 int len = name.length(); 688 if (name.startsWith("Stub")) { // Stub 689 out.writeUTF(name); 690 } else { // Method or Klass 691 int parenthesesIndex = name.lastIndexOf('(', len - 1); 692 if (parenthesesIndex > 0) { // Method name 693 int dotIndex = name.lastIndexOf('.', parenthesesIndex - 1); 694 assert dotIndex > 0 : "method's full name should have '.' : " + name; 695 String klassName = name.substring(0, dotIndex); 696 out.writeUTF(klassName); 697 String methodName = name.substring(dotIndex + 1, parenthesesIndex); 698 out.writeUTF(methodName); 699 String signature = name.substring(parenthesesIndex, len); 700 out.writeUTF(signature); 701 } else { 702 out.writeUTF(name); // Klass 703 } 704 } 705 out.writeShort(0); // Terminate by 0. 706 byte[] b = bout.toByteArray(); 707 metaspaceNamesContainer.appendBytes(b, 0, b.length); 708 709 metaspaceNames.put(name, nameOffset); 710 return nameOffset; 711 } catch (IOException e) { 712 throw new InternalError("Failed to append bytes to stubs sections", e); 713 } 714 } 715 716 /** 717 * Add oop symbol by as follows. Extend the oop.got section with another slot for the VM to 718 * patch. 719 * 720 * @param oopName name of the oop symbol 721 */ 722 public Integer addOopSymbol(String oopName) { 723 Integer oopGotOffset = getStringOffset(oopName); 724 if (oopGotOffset != null) { 725 return oopGotOffset; 726 } 727 return newOopSymbol(oopName); 728 } 729 730 private Integer newOopSymbol(String oopName) { 731 // Reference to String resolution (ldc). 732 int offset = oopGotContainer.getByteStreamSize(); 733 String gotName = "got.ldc." + offset; 734 Symbol relocationSymbol = oopGotContainer.createGotSymbol(gotName); 735 736 if (offset != relocationSymbol.getOffset()) { 737 throw new InternalError("offset must equal! (" + offset + " vs " + relocationSymbol.getOffset()); 738 } 739 740 addStringOffset(oopName, relocationSymbol.getOffset()); 741 return relocationSymbol.getOffset(); 742 } 743 744 public int addCountersSymbol(String metaspaceName) { 745 String gotName = "got." + metaspaceName; 746 Symbol relocationSymbol = getGotSymbol(gotName); 747 int metaspaceOffset = -1; 748 if (relocationSymbol == null) { 749 // Add slots when asked in the .metaspace.got section: 750 countersGotContainer.createGotSymbol(gotName); 751 } 752 return metaspaceOffset; 753 } 754 755 public Symbol getGotSymbol(String name) { 756 assert name.startsWith("got."); 757 return symbolTable.get(name); 758 } 759 760 /** 761 * Add klass symbol by as follows. - Adding the symbol name to the metaspace.names section - Add 762 * the offset of the name in metaspace.names to metaspace.offsets - Extend the klasses.got 763 * section with another slot for the VM to patch 764 * 765 * @param klassName name of the metaspace symbol 766 * @return the got offset in the klasses.got of the metaspace symbol 767 */ 768 public int addTwoSlotKlassSymbol(String klassName) { 769 String gotName = "got." + klassName; 770 Symbol previous = getGotSymbol(gotName); 771 assert previous == null : "should be called only once for: " + klassName; 772 // Add slots when asked in the .metaspace.got section: 773 // First slot 774 String gotInitName = "got.init." + klassName; 775 GotSymbol slot1Symbol = klassesGotContainer.createGotSymbol(gotInitName); 776 GotSymbol slot2Symbol = klassesGotContainer.createGotSymbol(gotName); 777 778 slot1Symbol.getIndex(); // check alignment and ignore result 779 // Get the index (offset/8) to the got in the .metaspace.got section 780 return slot2Symbol.getIndex(); 781 } 782 783 public static int addMethodsCount(int count, ReadOnlyDataContainer container) { 784 return appendInt(count, container); 785 } 786 787 private static int appendInt(int count, ReadOnlyDataContainer container) { 788 int offset = container.getByteStreamSize(); 789 container.appendInt(count); 790 return offset; 791 } 792 793 /** 794 * Add constant data as follows. - Adding the data to the meth.constdata section 795 * 796 * @param data 797 * @param alignment 798 * @return the offset in the meth.constdata of the data 799 */ 800 public int addConstantData(byte[] data, int alignment) { 801 // Get the current length of the metaspaceNameContainer 802 int constantDataOffset = alignUp(constantDataContainer, alignment); 803 constantDataContainer.appendBytes(data, 0, data.length); 804 alignUp(constantDataContainer, alignment); // Post alignment 805 return constantDataOffset; 806 } 807 808 public static int alignUp(ByteContainer container, int alignment) { 809 if (Integer.bitCount(alignment) != 1) { 810 throw new IllegalArgumentException("Must be a power of 2"); 811 } 812 int offset = container.getByteStreamSize(); 813 int aligned = (offset + (alignment - 1)) & -alignment; 814 if (aligned < offset || (aligned & (alignment - 1)) != 0) { 815 throw new RuntimeException("Error aligning: " + offset + " -> " + aligned); 816 } 817 if (aligned != offset) { 818 int nullArraySz = aligned - offset; 819 byte[] nullArray = new byte[nullArraySz]; 820 container.appendBytes(nullArray, 0, nullArraySz); 821 offset = aligned; 822 } 823 return offset; 824 } 825 826 public void addCodeSegments(int start, int end) { 827 assert (start % codeSegmentSize) == 0 : "not aligned code"; 828 int currentOffset = codeSegmentsContainer.getByteStreamSize(); 829 int offset = start / codeSegmentSize; 830 int emptySize = offset - currentOffset; 831 // add empty segments if needed 832 if (emptySize > 0) { 833 byte[] emptyArray = new byte[emptySize]; 834 for (int i = 0; i < emptySize; i++) { 835 emptyArray[i] = (byte) 0xff; 836 } 837 appendBytes(codeSegmentsContainer, emptyArray, 0, emptySize); 838 } 839 int alignedEnd = (end + (codeSegmentSize - 1)) & -codeSegmentSize; 840 int segmentsCount = (alignedEnd / codeSegmentSize) - offset; 841 byte[] segments = new byte[segmentsCount]; 842 int idx = 0; 843 for (int i = 0; i < segmentsCount; i++) { 844 segments[i] = (byte) idx; 845 idx = (idx == 0xfe) ? 1 : (idx + 1); 846 } 847 appendBytes(codeSegmentsContainer, segments, 0, segmentsCount); 848 } 849 850 public ByteContainer getExtLinkageGOTContainer() { 851 return extLinkageGOTContainer; 852 } 853 854 public ReadOnlyDataContainer getMethodMetadataContainer() { 855 return methodMetadataContainer; 856 } 857 858 public ReadOnlyDataContainer getMetaspaceNamesContainer() { 859 return metaspaceNamesContainer; 860 } 861 862 public ReadOnlyDataContainer getMethodsOffsetsContainer() { 863 return methodsOffsetsContainer; 864 } 865 866 public ReadOnlyDataContainer getKlassesOffsetsContainer() { 867 return klassesOffsetsContainer; 868 } 869 870 public ReadOnlyDataContainer getKlassesDependenciesContainer() { 871 return klassesDependenciesContainer; 872 } 873 874 public ReadOnlyDataContainer getStubsOffsetsContainer() { 875 return stubsOffsetsContainer; 876 } 877 878 public ReadOnlyDataContainer getCodeSegmentsContainer() { 879 return codeSegmentsContainer; 880 } 881 882 public ReadOnlyDataContainer getConstantDataContainer() { 883 return constantDataContainer; 884 } 885 886 public ByteContainer getKlassesGotContainer() { 887 return klassesGotContainer; 888 } 889 890 public ByteContainer getCountersGotContainer() { 891 return countersGotContainer; 892 } 893 894 public ByteContainer getMetadataGotContainer() { 895 return metadataGotContainer; 896 } 897 898 public ByteContainer getMethodStateContainer() { 899 return methodStateContainer; 900 } 901 902 public ByteContainer getOopGotContainer() { 903 return oopGotContainer; 904 } 905 906 public CodeContainer getCodeContainer() { 907 return codeContainer; 908 } 909 910 public ReadOnlyDataContainer getConfigContainer() { 911 return configContainer; 912 } 913 914 public Map<Symbol, Relocation> getUniqueRelocationTable() { 915 return uniqueRelocationTable; 916 } 917 918 public HeaderContainer getHeaderContainer() { 919 return headerContainer; 920 } 921 922 }