--- old/src/jdk.aot/share/classes/jdk.tools.jaotc.binformat/src/jdk/tools/jaotc/binformat/pecoff/JPECoffRelocObject.java 2017-08-22 11:46:36.000000000 -0700 +++ new/src/jdk.aot/share/classes/jdk.tools.jaotc.binformat/src/jdk/tools/jaotc/binformat/pecoff/JPECoffRelocObject.java 2017-08-22 11:46:36.000000000 -0700 @@ -24,13 +24,11 @@ package jdk.tools.jaotc.binformat.pecoff; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; -import jdk.tools.jaotc.binformat.Container; import jdk.tools.jaotc.binformat.BinaryContainer; import jdk.tools.jaotc.binformat.ByteContainer; import jdk.tools.jaotc.binformat.CodeContainer; @@ -38,11 +36,9 @@ import jdk.tools.jaotc.binformat.Relocation; import jdk.tools.jaotc.binformat.Relocation.RelocType; import jdk.tools.jaotc.binformat.Symbol; -import jdk.tools.jaotc.binformat.NativeSymbol; import jdk.tools.jaotc.binformat.Symbol.Binding; import jdk.tools.jaotc.binformat.Symbol.Kind; -import jdk.tools.jaotc.binformat.pecoff.PECoff; import jdk.tools.jaotc.binformat.pecoff.PECoffSymbol; import jdk.tools.jaotc.binformat.pecoff.PECoffTargetInfo; import jdk.tools.jaotc.binformat.pecoff.PECoff.IMAGE_FILE_HEADER; @@ -56,71 +52,53 @@ private final PECoffContainer pecoffContainer; - private final int segmentSize; + private final int sectionAlignment; - public JPECoffRelocObject(BinaryContainer binContainer, String outputFileName, String aotVersion) { + public JPECoffRelocObject(BinaryContainer binContainer, String outputFileName) { this.binContainer = binContainer; - this.pecoffContainer = new PECoffContainer(outputFileName, aotVersion); - this.segmentSize = binContainer.getCodeSegmentSize(); - if (segmentSize != 64) { - System.out.println("binContainer alignment size not 64 bytes, update JPECoffRelocObject"); - } + this.pecoffContainer = new PECoffContainer(outputFileName); + this.sectionAlignment = binContainer.getCodeSegmentSize(); } - private PECoffSection createByteSection(ArrayListsections, - String sectName, - byte [] scnData, - boolean hasRelocs, - int scnFlags) { - - PECoffSection sect = new PECoffSection(sectName, - scnData, - scnFlags, - hasRelocs, - sections.size()); + private static PECoffSection createByteSection(ArrayList sections, String sectName, byte[] scnData, + boolean hasRelocs, int scnFlags, int sectAlign) { + + PECoffSection sect = new PECoffSection(sectName, scnData, scnFlags, sectAlign, hasRelocs, sections.size()); // Add this section to our list sections.add(sect); return (sect); } - private void createByteSection(ArrayListsections, - ByteContainer c, int scnFlags) { + private static void createByteSection(ArrayList sections, ByteContainer c, int scnFlags, int sectAlign) { PECoffSection sect; boolean hasRelocs = c.hasRelocations(); byte[] scnData = c.getByteArray(); - sect = createByteSection(sections, c.getContainerName(), - scnData, hasRelocs, - scnFlags); + sect = createByteSection(sections, c.getContainerName(), scnData, hasRelocs, scnFlags, sectAlign); c.setSectionId(sect.getSectionId()); } - private void createCodeSection(ArrayListsections, CodeContainer c) { - createByteSection(sections, c, IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_READ | - IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_EXECUTE | - IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_64BYTES | - IMAGE_SECTION_HEADER.IMAGE_SCN_CNT_CODE); + private void createCodeSection(ArrayList sections, CodeContainer c) { + int scnFlags = IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_READ | IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_EXECUTE | IMAGE_SECTION_HEADER.IMAGE_SCN_CNT_CODE; + createByteSection(sections, c, scnFlags, sectionAlignment); } - private void createReadOnlySection(ArrayListsections, ReadOnlyDataContainer c) { - createByteSection(sections, c, IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_READ | - IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_64BYTES | - IMAGE_SECTION_HEADER.IMAGE_SCN_CNT_INITIALIZED_DATA); + private void createReadOnlySection(ArrayList sections, ReadOnlyDataContainer c) { + int scnFlags = IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_READ | IMAGE_SECTION_HEADER.IMAGE_SCN_CNT_INITIALIZED_DATA; + createByteSection(sections, c, scnFlags, sectionAlignment); } - private void createReadWriteSection(ArrayListsections, ByteContainer c) { - int scnFlags = IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_READ | - IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_WRITE | - IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_64BYTES; + private void createReadWriteSection(ArrayList sections, ByteContainer c) { + int scnFlags = IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_READ | IMAGE_SECTION_HEADER.IMAGE_SCN_MEM_WRITE; - if (c.getByteArray().length > 0) + if (c.getByteArray().length > 0) { scnFlags |= IMAGE_SECTION_HEADER.IMAGE_SCN_CNT_INITIALIZED_DATA; - else + } else { scnFlags |= IMAGE_SECTION_HEADER.IMAGE_SCN_CNT_UNINITIALIZED_DATA; - - createByteSection(sections, c, scnFlags); + } + createByteSection(sections, c, scnFlags, sectionAlignment); } /** @@ -131,7 +109,7 @@ * @throws IOException throws {@code IOException} as a result of file system access failures. */ public void createPECoffRelocObject(Map> relocationTable, Collection symbols) throws IOException { - ArrayList sections = new ArrayList(); + ArrayList sections = new ArrayList<>(); // Create text section createCodeSection(sections, binContainer.getCodeContainer()); @@ -139,51 +117,45 @@ createReadOnlySection(sections, binContainer.getKlassesOffsetsContainer()); createReadOnlySection(sections, binContainer.getMethodsOffsetsContainer()); createReadOnlySection(sections, binContainer.getKlassesDependenciesContainer()); - createReadWriteSection(sections, binContainer.getMetaspaceGotContainer()); - createReadWriteSection(sections, binContainer.getMetadataGotContainer()); - createReadWriteSection(sections, binContainer.getMethodStateContainer()); - createReadWriteSection(sections, binContainer.getOopGotContainer()); - createReadWriteSection(sections, binContainer.getMethodMetadataContainer()); + createReadOnlySection(sections, binContainer.getMethodMetadataContainer()); createReadOnlySection(sections, binContainer.getStubsOffsetsContainer()); createReadOnlySection(sections, binContainer.getHeaderContainer().getContainer()); createReadOnlySection(sections, binContainer.getCodeSegmentsContainer()); createReadOnlySection(sections, binContainer.getConstantDataContainer()); createReadOnlySection(sections, binContainer.getConfigContainer()); - - // createExternalLinkage(); - - createCodeSection(sections, binContainer.getExtLinkageContainer()); + createReadWriteSection(sections, binContainer.getKlassesGotContainer()); + createReadWriteSection(sections, binContainer.getCountersGotContainer()); + createReadWriteSection(sections, binContainer.getMetadataGotContainer()); + createReadWriteSection(sections, binContainer.getMethodStateContainer()); + createReadWriteSection(sections, binContainer.getOopGotContainer()); createReadWriteSection(sections, binContainer.getExtLinkageGOTContainer()); // Allocate PECoff Header PECoffHeader header = new PECoffHeader(); // Get PECoff symbol data from BinaryContainer object's symbol tables - PECoffSymtab symtab = createPECoffSymbolTables(sections, symbols); + PECoffSymtab symtab = createPECoffSymbolTables(symbols); // Add Linker Directives Section - createByteSection(sections, ".drectve", - symtab.getDirectiveArray(), false, - IMAGE_SECTION_HEADER.IMAGE_SCN_LNK_INFO | - IMAGE_SECTION_HEADER.IMAGE_SCN_LNK_REMOVE | - IMAGE_SECTION_HEADER.IMAGE_SCN_ALIGN_1BYTES); + int scnFlags = IMAGE_SECTION_HEADER.IMAGE_SCN_LNK_INFO | IMAGE_SECTION_HEADER.IMAGE_SCN_LNK_REMOVE; + createByteSection(sections, ".drectve", symtab.getDirectiveArray(), false, scnFlags, 1 /* 1 byte alignment */); // Create the Relocation Tables PECoffRelocTable pecoffRelocs = createPECoffRelocTable(sections, relocationTable); // File Output Order // - // HEADER (Need address of Symbol Table + symbol count) - // SECTIONS (Need pointer to Section Data, Relocation Table) - // DIRECTIVES - // SYMBOL TABLE - // SYMBOLS - // SECTION DATA - // RELOCATION TABLE + // HEADER (Need address of Symbol Table + symbol count) + // SECTIONS (Need pointer to Section Data, Relocation Table) + // DIRECTIVES + // SYMBOL TABLE + // SYMBOLS + // SECTION DATA + // RELOCATION TABLE // Calculate Offset for Symbol table int file_offset = IMAGE_FILE_HEADER.totalsize + - (IMAGE_SECTION_HEADER.totalsize*sections.size()); + (IMAGE_SECTION_HEADER.totalsize * sections.size()); // Update Header fields header.setSectionCount(sections.size()); @@ -194,14 +166,14 @@ file_offset += ((symtab.getSymtabCount() * IMAGE_SYMBOL.totalsize) + symtab.getStrtabSize()); // And round it up - file_offset = (file_offset + (sections.get(0).getDataAlign()-1)) & - ~((sections.get(0).getDataAlign()-1)); + file_offset = (file_offset + (sections.get(0).getDataAlign() - 1)) & + ~((sections.get(0).getDataAlign() - 1)); // Calc file offsets for section data for (int i = 0; i < sections.size(); i++) { PECoffSection sect = sections.get(i); - file_offset = (file_offset + (sect.getDataAlign()-1)) & - ~((sect.getDataAlign()-1)); + file_offset = (file_offset + (sect.getDataAlign() - 1)) & + ~((sect.getDataAlign() - 1)); sect.setOffset(file_offset); file_offset += sect.getSize(); } @@ -214,7 +186,9 @@ sect.setReloff(file_offset); sect.setRelcount(nreloc); // extended relocations add an addition entry - if (nreloc > 0xFFFF) nreloc++; + if (nreloc > 0xFFFF) { + nreloc++; + } file_offset += (nreloc * IMAGE_RELOCATION.totalsize); } } @@ -253,7 +227,7 @@ * * @param symbols */ - private PECoffSymtab createPECoffSymbolTables(ArrayList sections, Collection symbols) { + private static PECoffSymtab createPECoffSymbolTables(Collection symbols) { PECoffSymtab symtab = new PECoffSymtab(); // First, create the initial null symbol. This is a local symbol. @@ -263,8 +237,8 @@ for (Symbol symbol : symbols) { // Get the index of section this symbol is defined in. int secHdrIndex = symbol.getSection().getSectionId(); - PECoffSymbol pecoffSymbol = symtab.addSymbolEntry(symbol.getName(), getPECoffTypeOf(symbol), getPECoffClassOf(symbol), (byte)secHdrIndex, symbol.getOffset(), symbol.getSize()); - symbol.setNativeSymbol((NativeSymbol)pecoffSymbol); + PECoffSymbol pecoffSymbol = symtab.addSymbolEntry(symbol.getName(), getPECoffTypeOf(symbol), getPECoffClassOf(symbol), (byte) secHdrIndex, symbol.getOffset()); + symbol.setNativeSymbol(pecoffSymbol); } return (symtab); } @@ -291,13 +265,11 @@ * @param sections * @param relocationTable */ - private PECoffRelocTable createPECoffRelocTable(ArrayList sections, - Map> relocationTable) { + private PECoffRelocTable createPECoffRelocTable(ArrayList sections, Map> relocationTable) { PECoffRelocTable pecoffRelocTable = new PECoffRelocTable(sections.size()); /* - * For each of the symbols with associated relocation records, create a PECoff relocation - * entry. + * For each of the symbols with associated relocation records, create a PECoff relocation entry. */ for (Map.Entry> entry : relocationTable.entrySet()) { List relocs = entry.getValue(); @@ -315,18 +287,17 @@ return (pecoffRelocTable); } - private void createRelocation(Symbol symbol, Relocation reloc, PECoffRelocTable pecoffRelocTable) { + private static void createRelocation(Symbol symbol, Relocation reloc, PECoffRelocTable pecoffRelocTable) { RelocType relocType = reloc.getType(); int pecoffRelocType = getPECoffRelocationType(relocType); - PECoffSymbol sym = (PECoffSymbol)symbol.getNativeSymbol(); + PECoffSymbol sym = (PECoffSymbol) symbol.getNativeSymbol(); int symno = sym.getIndex(); int sectindex = reloc.getSection().getSectionId(); int offset = reloc.getOffset(); int addend = 0; switch (relocType) { - case FOREIGN_CALL_DIRECT: case JAVA_CALL_DIRECT: case STUB_CALL_DIRECT: case FOREIGN_CALL_INDIRECT_GOT: { @@ -336,47 +307,22 @@ offset = offset + reloc.getSize() + addend; break; } - case FOREIGN_CALL_DIRECT_FAR: { - // Create relocation entry - addend = -8; // Size in bytes of the patch location - // Relocation should be applied at the location after call operand - // 10 = 2 (jmp [r]) + 8 (imm64) - offset = offset + reloc.getSize() + addend - 2; - break; - } - case FOREIGN_CALL_INDIRECT: - case JAVA_CALL_INDIRECT: - case STUB_CALL_INDIRECT: { + case JAVA_CALL_INDIRECT: { // Do nothing. return; } - case EXTERNAL_DATA_REFERENCE_FAR: { - // Create relocation entry - addend = -4; // Size of 32-bit address of the GOT - /* - * Relocation should be applied before the test instruction to the move instruction. - * offset points to the test instruction after the instruction that loads - * the address of polling page. So set the offset appropriately. - */ - offset = offset + addend; - break; - } case METASPACE_GOT_REFERENCE: - case EXTERNAL_PLT_TO_GOT: - case STATIC_STUB_TO_STATIC_METHOD: - case STATIC_STUB_TO_HOTSPOT_LINKAGE_GOT: { + case EXTERNAL_PLT_TO_GOT: { addend = -4; // Size of 32-bit address of the GOT /* - * Relocation should be applied before the test instruction to - * the move instruction. reloc.getOffset() points to the - * test instruction after the instruction that loads the - * address of polling page. So set the offset appropriately. + * Relocation should be applied before the test instruction to the move instruction. + * reloc.getOffset() points to the test instruction after the instruction that loads the address of + * polling page. So set the offset appropriately. */ offset = offset + addend; break; } - case EXTERNAL_GOT_TO_PLT: - case LOADTIME_ADDRESS: { + case EXTERNAL_GOT_TO_PLT: { // this is load time relocations break; } @@ -391,27 +337,17 @@ int pecoffRelocType = 0; // R__NONE if #define'd to 0 for all values of ARCH switch (PECoffTargetInfo.getPECoffArch()) { case IMAGE_FILE_HEADER.IMAGE_FILE_MACHINE_AMD64: - if (relocType == RelocType.FOREIGN_CALL_DIRECT || - relocType == RelocType.JAVA_CALL_DIRECT || + if (relocType == RelocType.JAVA_CALL_DIRECT || relocType == RelocType.FOREIGN_CALL_INDIRECT_GOT) { pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_REL32; } else if (relocType == RelocType.STUB_CALL_DIRECT) { pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_REL32; - } else if (relocType == RelocType.FOREIGN_CALL_DIRECT_FAR) { - pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_ADDR64; - } else if (relocType == RelocType.FOREIGN_CALL_INDIRECT || - relocType == RelocType.JAVA_CALL_INDIRECT || - relocType == RelocType.STUB_CALL_INDIRECT) { + } else if (relocType == RelocType.JAVA_CALL_INDIRECT) { pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_ABSOLUTE; - } else if ((relocType == RelocType.EXTERNAL_DATA_REFERENCE_FAR)) { - pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_REL32; } else if (relocType == RelocType.METASPACE_GOT_REFERENCE || - relocType == RelocType.EXTERNAL_PLT_TO_GOT || - relocType == RelocType.STATIC_STUB_TO_STATIC_METHOD || - relocType == RelocType.STATIC_STUB_TO_HOTSPOT_LINKAGE_GOT) { + relocType == RelocType.EXTERNAL_PLT_TO_GOT) { pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_REL32; - } else if (relocType == RelocType.EXTERNAL_GOT_TO_PLT || - relocType == RelocType.LOADTIME_ADDRESS) { + } else if (relocType == RelocType.EXTERNAL_GOT_TO_PLT) { pecoffRelocType = IMAGE_RELOCATION.IMAGE_REL_AMD64_ADDR64; } else { assert false : "Unhandled relocation type: " + relocType;