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