src/jdk.aot/share/classes/jdk.tools.jaotc.binformat/src/jdk/tools/jaotc/binformat/macho/MachOContainer.java
author iveresov
Fri, 17 Aug 2018 13:20:53 -0700
changeset 51436 091c0d22e735
parent 47216 71c04702a3d5
child 52910 583fd71c47d6
permissions -rw-r--r--
8206992: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     1
/*
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     4
 *
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     8
 *
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    13
 * accompanied this code).
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    14
 *
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    18
 *
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    21
 * questions.
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    22
 */
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    23
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    24
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    25
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    26
package jdk.tools.jaotc.binformat.macho;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    27
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    28
import java.io.File;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    29
import java.io.FileOutputStream;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    30
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    31
final class MachOContainer {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    32
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    33
    private final File outputFile;
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    34
    private FileOutputStream outputStream;
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    35
    private long fileOffset;
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    36
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    37
    MachOContainer(String fileName) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    38
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    39
        outputFile = new File(fileName);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    40
        if (outputFile.exists()) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    41
            outputFile.delete();
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    42
        }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    43
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    44
        try {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    45
            outputStream = new FileOutputStream(outputFile);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    46
        } catch (Exception e) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    47
            System.out.println("MachOContainer: Can't create file " + fileName);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    48
        }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    49
        fileOffset = 0;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    50
    }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    51
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    52
    void close() {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    53
        try {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    54
            outputStream.close();
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    55
        } catch (Exception e) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    56
            System.out.println("MachOContainer: close failed");
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    57
        }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    58
    }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    59
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    60
    void writeBytes(byte[] bytes) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    61
        try {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    62
            outputStream.write(bytes);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    63
        } catch (Exception e) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    64
            System.out.println("MachOContainer: writeBytes failed");
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    65
        }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    66
        fileOffset += bytes.length;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    67
    }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    68
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    69
    // Write bytes to output file with up front alignment padding
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    70
    void writeBytes(byte[] bytes, int alignment) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    71
        try {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    72
            // Pad to alignment
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46260
diff changeset
    73
            while ((fileOffset & (alignment - 1)) != 0) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    74
                outputStream.write(0);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    75
                fileOffset++;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    76
            }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    77
            outputStream.write(bytes);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    78
        } catch (Exception e) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    79
            System.out.println("MachOContainer: writeBytes failed");
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    80
        }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    81
        fileOffset += bytes.length;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    82
    }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
diff changeset
    83
}