1 /*
2 * Copyright (c) 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 */
52 return sb.toString();
53 }
54
55 Linker(Main main) throws Exception {
56 this.options = main.options;
57 String name = options.outputName;
58 objectFileName = name;
59 libraryFileName = name;
60
61 if (options.linkerpath != null && !(new File(options.linkerpath).exists())) {
62 throw new InternalError("Invalid linker path: " + options.linkerpath);
63 }
64 String linkerPath;
65 String linkerCheck;
66
67 switch (options.osName) {
68 case "Linux":
69 if (name.endsWith(".so")) {
70 objectFileName = name.substring(0, name.length() - ".so".length());
71 }
72 linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
73 linkerCmd = linkerPath + " -shared -z noexecstack -o " + libraryFileName + " " + objectFileName;
74 linkerCheck = linkerPath + " -v";
75 break;
76 case "SunOS":
77 if (name.endsWith(".so")) {
78 objectFileName = name.substring(0, name.length() - ".so".length());
79 }
80 objectFileName = objectFileName + ".o";
81 linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
82 linkerCmd = linkerPath + " -shared -o " + libraryFileName + " " + objectFileName;
83 linkerCheck = linkerPath + " -V";
84 break;
85 case "Mac OS X":
86 if (name.endsWith(".dylib")) {
87 objectFileName = name.substring(0, name.length() - ".dylib".length());
88 }
89 objectFileName = objectFileName + ".o";
90 linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
91 linkerCmd = linkerPath + " -dylib -o " + libraryFileName + " " + objectFileName;
113 if (linkerCheck != null) {
114 Process p = Runtime.getRuntime().exec(linkerCheck);
115 final int exitCode = p.waitFor();
116 if (exitCode != 0) {
117 throw new InternalError(getString(p.getErrorStream()));
118 }
119 }
120 }
121
122 void link() throws Exception {
123 Process p = Runtime.getRuntime().exec(linkerCmd);
124 final int exitCode = p.waitFor();
125 if (exitCode != 0) {
126 String errorMessage = getString(p.getErrorStream());
127 if (errorMessage.isEmpty()) {
128 errorMessage = getString(p.getInputStream());
129 }
130 throw new InternalError(errorMessage);
131 }
132 File objFile = new File(objectFileName);
133 if (objFile.exists()) {
134 if (!objFile.delete()) {
135 throw new InternalError("Failed to delete " + objectFileName + " file");
136 }
137 }
138 // Make non-executable for all.
139 File libFile = new File(libraryFileName);
140 if (libFile.exists() && !options.osName.startsWith("Windows")) {
141 if (!libFile.setExecutable(false, false)) {
142 throw new InternalError("Failed to change attribute for " + libraryFileName + " file");
143 }
144 }
145
146 }
147
148 /**
149 * Visual Studio supported versions Search Order is: VS2013, VS2015, VS2012
150 */
151 public enum VSVERSIONS {
152 VS2013("VS120COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64\\link.exe"),
153 VS2015("VS140COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64\\link.exe"),
|
1 /*
2 * Copyright (c) 2018, 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 */
52 return sb.toString();
53 }
54
55 Linker(Main main) throws Exception {
56 this.options = main.options;
57 String name = options.outputName;
58 objectFileName = name;
59 libraryFileName = name;
60
61 if (options.linkerpath != null && !(new File(options.linkerpath).exists())) {
62 throw new InternalError("Invalid linker path: " + options.linkerpath);
63 }
64 String linkerPath;
65 String linkerCheck;
66
67 switch (options.osName) {
68 case "Linux":
69 if (name.endsWith(".so")) {
70 objectFileName = name.substring(0, name.length() - ".so".length());
71 }
72 objectFileName = objectFileName + ".o";
73 linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
74 linkerCmd = linkerPath + " -shared -z noexecstack -o " + libraryFileName + " " + objectFileName;
75 linkerCheck = linkerPath + " -v";
76 break;
77 case "SunOS":
78 if (name.endsWith(".so")) {
79 objectFileName = name.substring(0, name.length() - ".so".length());
80 }
81 objectFileName = objectFileName + ".o";
82 linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
83 linkerCmd = linkerPath + " -shared -o " + libraryFileName + " " + objectFileName;
84 linkerCheck = linkerPath + " -V";
85 break;
86 case "Mac OS X":
87 if (name.endsWith(".dylib")) {
88 objectFileName = name.substring(0, name.length() - ".dylib".length());
89 }
90 objectFileName = objectFileName + ".o";
91 linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
92 linkerCmd = linkerPath + " -dylib -o " + libraryFileName + " " + objectFileName;
114 if (linkerCheck != null) {
115 Process p = Runtime.getRuntime().exec(linkerCheck);
116 final int exitCode = p.waitFor();
117 if (exitCode != 0) {
118 throw new InternalError(getString(p.getErrorStream()));
119 }
120 }
121 }
122
123 void link() throws Exception {
124 Process p = Runtime.getRuntime().exec(linkerCmd);
125 final int exitCode = p.waitFor();
126 if (exitCode != 0) {
127 String errorMessage = getString(p.getErrorStream());
128 if (errorMessage.isEmpty()) {
129 errorMessage = getString(p.getInputStream());
130 }
131 throw new InternalError(errorMessage);
132 }
133 File objFile = new File(objectFileName);
134 boolean keepObjFile = Boolean.parseBoolean(System.getProperty("aot.keep.objFile", "false"));
135 if (objFile.exists() && !keepObjFile) {
136 if (!objFile.delete()) {
137 throw new InternalError("Failed to delete " + objectFileName + " file");
138 }
139 }
140 // Make non-executable for all.
141 File libFile = new File(libraryFileName);
142 if (libFile.exists() && !options.osName.startsWith("Windows")) {
143 if (!libFile.setExecutable(false, false)) {
144 throw new InternalError("Failed to change attribute for " + libraryFileName + " file");
145 }
146 }
147
148 }
149
150 /**
151 * Visual Studio supported versions Search Order is: VS2013, VS2015, VS2012
152 */
153 public enum VSVERSIONS {
154 VS2013("VS120COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64\\link.exe"),
155 VS2015("VS140COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64\\link.exe"),
|