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