Skip to content

Commit 723df10

Browse files
Set common-page-size for the linker invocation to ensure proper object file alignment after linking.
1 parent 4fd24a6 commit 723df10

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/CCLinkerInvocation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ private static class BinutilsCCLinkerInvocation extends CCLinkerInvocation {
268268
additionalPreOptions.add("-z");
269269
additionalPreOptions.add(SpawnIsolates.getValue() ? "text" : "notext");
270270

271+
/*
272+
* Make the linker aware of the page size used for aligning the native image object file
273+
* sections. This makes sure that the resulting object file is always properly aligned,
274+
* otherwise it would cause an error in ld-linux (glibc older than 2.35) if a specific
275+
* linker version is involved (e.g. GNU binutils ld 2.38).
276+
*
277+
* In older glibc versions this is caused by a stricter page alignment check. Page
278+
* alignment is checked against the alignment that comes from the linker instead of the
279+
* system page size. This allows technically incorrect ELF object files to run on newer
280+
* versions.
281+
*/
282+
additionalPreOptions.add("-z");
283+
additionalPreOptions.add("common-page-size=" + SubstrateOptions.getPageSize());
284+
271285
if (SubstrateOptions.RemoveUnusedSymbols.getValue()) {
272286
/* Perform garbage collection of unused input sections. */
273287
additionalPreOptions.add("-Wl,--gc-sections");

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoStripFeature.java

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ public class NativeImageDebugInfoStripFeature implements InternalFeature {
5353

5454
@Override
5555
public boolean isInConfiguration(IsInConfigurationAccess access) {
56-
/*
57-
* Make sure this feature always runs for ELF object files to fix the object file's
58-
* alignment with objcopy. This is a temporary workaround; a proper fix will be provided
59-
* with GR-68594.
60-
*/
61-
return SubstrateOptions.StripDebugInfo.getValue() || ObjectFile.getNativeFormat() == ObjectFile.Format.ELF;
56+
return SubstrateOptions.StripDebugInfo.getValue();
6257
}
6358

6459
@SuppressWarnings("try")
@@ -117,35 +112,20 @@ private static boolean stripLinux(AfterImageWriteAccessImpl accessImpl) {
117112
try {
118113
Path outputDirectory = imagePath.getParent();
119114
String imageFilePath = outputDirectory.resolve(imageName).toString();
120-
if (SubstrateOptions.StripDebugInfo.getValue()) {
121-
if (SubstrateOptions.useDebugInfoGeneration()) {
122-
/* Generate a separate debug file before stripping the executable. */
123-
String debugInfoName = imageName + debugExtension;
124-
Path debugInfoFilePath = outputDirectory.resolve(debugInfoName);
125-
FileUtils.executeCommand(objcopyExe, "--only-keep-debug", imageFilePath, debugInfoFilePath.toString());
126-
BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, debugInfoFilePath);
127-
FileUtils.executeCommand(objcopyExe, "--add-gnu-debuglink=" + debugInfoFilePath, imageFilePath);
128-
}
129-
if (SubstrateOptions.DeleteLocalSymbols.getValue()) {
130-
/* Strip debug info and local symbols. */
131-
FileUtils.executeCommand(objcopyExe, "--strip-all", imageFilePath);
132-
} else {
133-
/* Strip debug info only. */
134-
FileUtils.executeCommand(objcopyExe, "--strip-debug", imageFilePath);
135-
}
115+
if (SubstrateOptions.useDebugInfoGeneration()) {
116+
/* Generate a separate debug file before stripping the executable. */
117+
String debugInfoName = imageName + debugExtension;
118+
Path debugInfoFilePath = outputDirectory.resolve(debugInfoName);
119+
FileUtils.executeCommand(objcopyExe, "--only-keep-debug", imageFilePath, debugInfoFilePath.toString());
120+
BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, debugInfoFilePath);
121+
FileUtils.executeCommand(objcopyExe, "--add-gnu-debuglink=" + debugInfoFilePath, imageFilePath);
122+
}
123+
if (SubstrateOptions.DeleteLocalSymbols.getValue()) {
124+
/* Strip debug info and local symbols. */
125+
FileUtils.executeCommand(objcopyExe, "--strip-all", imageFilePath);
136126
} else {
137-
/*
138-
* Make sure the object file is properly aligned. This step creates a temporary
139-
* file and then destructively renames it to the original image file name. In
140-
* effect, the original native image object file is copied and replaced with the
141-
* output of objcopy.
142-
*
143-
* This is a temporary workaround; a proper fix will be provided with GR-68594.
144-
*/
145-
FileUtils.executeCommand(objcopyExe, imageFilePath);
146-
147-
/* Nothing was actually stripped here. */
148-
return false;
127+
/* Strip debug info only. */
128+
FileUtils.executeCommand(objcopyExe, "--strip-debug", imageFilePath);
149129
}
150130
return true;
151131
} catch (IOException e) {

0 commit comments

Comments
 (0)