Skip to content

Commit 4352500

Browse files
committed
[GR-60631] Always copy over relocatable section for layered images.
PullRequest: graal/19654
2 parents cc48895 + b0dd39a commit 4352500

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxImageHeapProvider.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import static com.oracle.svm.core.Isolates.IMAGE_HEAP_WRITABLE_END;
3434
import static com.oracle.svm.core.Isolates.IMAGE_HEAP_WRITABLE_PATCHED_BEGIN;
3535
import static com.oracle.svm.core.Isolates.IMAGE_HEAP_WRITABLE_PATCHED_END;
36-
import static com.oracle.svm.core.imagelayer.ImageLayerSection.SectionEntries.HEAP_ANY_RELOCATABLE_POINTER;
3736
import static com.oracle.svm.core.imagelayer.ImageLayerSection.SectionEntries.HEAP_BEGIN;
3837
import static com.oracle.svm.core.imagelayer.ImageLayerSection.SectionEntries.HEAP_END;
3938
import static com.oracle.svm.core.imagelayer.ImageLayerSection.SectionEntries.HEAP_RELOCATABLE_BEGIN;
@@ -187,7 +186,8 @@ protected int initializeLayeredImage(Pointer firstHeapStart, Pointer selfReserve
187186
Word heapEnd = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_END));
188187
Word heapRelocBegin = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_RELOCATABLE_BEGIN));
189188
Word heapRelocEnd = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_RELOCATABLE_END));
190-
Word heapAnyRelocPointer = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_ANY_RELOCATABLE_POINTER));
189+
/* This value is not used for layered images. */
190+
Word heapAnyRelocPointer = Word.nullPointer();
191191
Word heapWritableBegin = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_WRITEABLE_BEGIN));
192192
Word heapWritableEnd = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_WRITEABLE_END));
193193
Word heapWritablePatchedBegin = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_WRITEABLE_PATCHED_BEGIN));
@@ -284,7 +284,7 @@ private static int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedS
284284
Word heapWritablePatchedEndSym, Word heapWritableSym, Word heapWritableEndSym) {
285285
assert heapBeginSym.belowOrEqual(heapWritableSym) && heapWritableSym.belowOrEqual(heapWritableEndSym) && heapWritableEndSym.belowOrEqual(heapEndSym);
286286
assert heapBeginSym.belowOrEqual(heapRelocsSym) && heapRelocsSym.belowOrEqual(heapRelocsEndSym) && heapRelocsEndSym.belowOrEqual(heapEndSym);
287-
assert heapRelocsSym.belowOrEqual(heapAnyRelocPointer) && heapAnyRelocPointer.belowThan(heapRelocsEndSym);
287+
assert ImageLayerBuildingSupport.buildingImageLayer() || heapRelocsSym.belowOrEqual(heapAnyRelocPointer) && heapAnyRelocPointer.belowThan(heapRelocsEndSym);
288288
assert heapRelocsSym.belowOrEqual(heapRelocsEndSym) && heapRelocsEndSym.belowOrEqual(heapWritablePatchedSym) && heapWritablePatchedSym.belowOrEqual(heapWritableEndSym);
289289

290290
SignedWord fd = cachedFd.read();
@@ -444,14 +444,26 @@ private static int copyRelocations(Pointer imageHeap, UnsignedWord pageSize, Wor
444444
Pointer sourceRelocsBoundary = cachedRelocsBoundary.isNonNull() ? cachedRelocsBoundary : linkedRelocsBoundary;
445445
Pointer linkedCopyStart = Word.nullPointer();
446446
if (heapRelocsEndSym.subtract(heapRelocsSym).isNonNull()) {
447-
/*
448-
* Use a representative pointer to determine whether it is necessary to copy over the
449-
* relocations from the original image, or if it has already been performed during
450-
* build-link time.
451-
*/
452-
ComparableWord relocatedValue = sourceRelocsBoundary.readWord(heapAnyRelocPointer.subtract(linkedRelocsBoundary));
453-
ComparableWord mappedValue = imageHeap.readWord(heapAnyRelocPointer.subtract(heapBeginSym));
454-
if (relocatedValue.notEqual(mappedValue)) {
447+
boolean copyRequired;
448+
if (ImageLayerBuildingSupport.buildingImageLayer()) {
449+
assert heapAnyRelocPointer.isNull();
450+
/*
451+
* The relocatable section with layered images will normally contain relocations
452+
* referring to both the same and different layers. Hence, it is not possible to use
453+
* a representative pointer to determine whether copying is necessary.
454+
*/
455+
copyRequired = true;
456+
} else {
457+
/*
458+
* Use a representative pointer to determine whether it is necessary to copy over
459+
* the relocations from the original image, or if it has already been performed
460+
* during build-link time.
461+
*/
462+
ComparableWord relocatedValue = sourceRelocsBoundary.readWord(heapAnyRelocPointer.subtract(linkedRelocsBoundary));
463+
ComparableWord mappedValue = imageHeap.readWord(heapAnyRelocPointer.subtract(heapBeginSym));
464+
copyRequired = relocatedValue.notEqual(mappedValue);
465+
}
466+
if (copyRequired) {
455467
linkedCopyStart = heapRelocsSym;
456468
}
457469
}
@@ -464,14 +476,17 @@ private static int copyRelocations(Pointer imageHeap, UnsignedWord pageSize, Wor
464476
}
465477
if (linkedCopyStart.isNonNull()) {
466478
/*
467-
* A portion of the image heap has relocations either resolved by the dynamic linker or
468-
* (for layered images) manually during our runtime initialization code. To preserve the
469-
* relocations, we must copy this code directly from the original image heap and not
470-
* reload it from disk.
479+
* A portion of the image heap needs to be manually copied over from the original image.
480+
* This is needed whenever:
481+
*
482+
* 1. Relocations resolved by the dynamic linker are present.
483+
*
484+
* 2. (For layered images only): Heap relative relocations are present (i.e. the
485+
* heapWritablePatched section is non-zero).
471486
*
472-
* We need to round to page boundaries, so we may copy some extra data which could be
473-
* copy-on-write. Also, we must first remap the pages to avoid loading them from disk
474-
* (only to then overwrite them).
487+
* To preserve this information, we must copy over this data from the original image
488+
* heap and not reload it from disk. Note this copying must be performed at a page
489+
* granularity, and hence may copy some extra data which could be copy-on-write.
475490
*/
476491
Pointer linkedCopyStartBoundary = roundDown(linkedCopyStart, pageSize);
477492
UnsignedWord copyAlignedSize = roundUp(heapWritablePatchedEndSym.subtract(linkedCopyStartBoundary), pageSize);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/imagelayer/ImageLayerSection.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public enum SectionEntries {
5757
HEAP_END,
5858
HEAP_RELOCATABLE_BEGIN,
5959
HEAP_RELOCATABLE_END,
60-
HEAP_ANY_RELOCATABLE_POINTER,
6160
HEAP_WRITEABLE_BEGIN,
6261
HEAP_WRITEABLE_END,
6362
HEAP_WRITEABLE_PATCHED_BEGIN,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public void build(String imageName, DebugContext debug) {
504504
objectFile.createDefinedSymbol(heapSection.getName(), heapSection, 0, 0, false, false);
505505

506506
long sectionOffsetOfARelocatablePointer = writer.writeHeap(debug, heapSectionBuffer);
507-
if (SpawnIsolates.getValue()) {
507+
if (!ImageLayerBuildingSupport.buildingImageLayer() && SpawnIsolates.getValue()) {
508508
if (heapLayout.getReadOnlyRelocatableSize() == 0) {
509509
/*
510510
* When there isn't a read only relocation section, the value of the relocatable
@@ -530,7 +530,10 @@ public void build(String imageName, DebugContext debug) {
530530
defineDataSymbol(Isolates.IMAGE_HEAP_RELOCATABLE_BEGIN_SYMBOL_NAME, heapSection, heapLayout.getReadOnlyRelocatableOffset() - heapLayout.getStartOffset());
531531
defineDataSymbol(Isolates.IMAGE_HEAP_RELOCATABLE_END_SYMBOL_NAME, heapSection,
532532
heapLayout.getReadOnlyRelocatableOffset() + heapLayout.getReadOnlyRelocatableSize() - heapLayout.getStartOffset());
533-
defineDataSymbol(Isolates.IMAGE_HEAP_A_RELOCATABLE_POINTER_SYMBOL_NAME, heapSection, sectionOffsetOfARelocatablePointer);
533+
if (!ImageLayerBuildingSupport.buildingImageLayer()) {
534+
/* Layered native-image builds do not use this symbol. */
535+
defineDataSymbol(Isolates.IMAGE_HEAP_A_RELOCATABLE_POINTER_SYMBOL_NAME, heapSection, sectionOffsetOfARelocatablePointer);
536+
}
534537
defineDataSymbol(Isolates.IMAGE_HEAP_WRITABLE_BEGIN_SYMBOL_NAME, heapSection, heapLayout.getWritableOffset() - heapLayout.getStartOffset());
535538
defineDataSymbol(Isolates.IMAGE_HEAP_WRITABLE_END_SYMBOL_NAME, heapSection, heapLayout.getWritableOffset() + heapLayout.getWritableSize() - heapLayout.getStartOffset());
536539
defineDataSymbol(Isolates.IMAGE_HEAP_WRITABLE_PATCHED_BEGIN_SYMBOL_NAME, heapSection, heapLayout.getWritablePatchedOffset() - heapLayout.getStartOffset());

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/ImageLayerSectionFeature.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@
7575
* | 8 | heap end |
7676
* | 16 | heap relocatable begin |
7777
* | 24 | heap relocatable end |
78-
* | 32 | heap any relocatable pointer |
79-
* | 40 | heap writable begin |
80-
* | 48 | heap writable end |
81-
* | 56 | heap writable patched begin |
82-
* | 64 | heap writable patched end |
83-
* | 72 | next layer section (0 if final layer) |
84-
* | 80 | cross-layer singleton table start |
78+
* | 32 | heap writable begin |
79+
* | 40 | heap writable end |
80+
* | 48 | heap writable patched begin |
81+
* | 56 | heap writable patched end |
82+
* | 64 | next layer section (0 if final layer) |
83+
* | 72 | cross-layer singleton table start |
8584
* | (after table) | heap relative patches |
8685
* ---------------------------------------------------------
8786
* </pre>
@@ -95,13 +94,12 @@ public final class ImageLayerSectionFeature implements InternalFeature, FeatureS
9594
private static final int HEAP_END_OFFSET = 8;
9695
private static final int HEAP_RELOCATABLE_BEGIN_OFFSET = 16;
9796
private static final int HEAP_RELOCATABLE_END_OFFSET = 24;
98-
private static final int HEAP_ANY_RELOCATABLE_POINTER_OFFSET = 32;
99-
private static final int HEAP_WRITABLE_BEGIN_OFFSET = 40;
100-
private static final int HEAP_WRITABLE_END_OFFSET = 48;
101-
private static final int HEAP_WRITABLE_PATCHED_BEGIN_OFFSET = 56;
102-
private static final int HEAP_WRITABLE_PATCHED_END_OFFSET = 64;
103-
private static final int NEXT_SECTION_OFFSET = 72;
104-
private static final int STATIC_SECTION_SIZE = 80;
97+
private static final int HEAP_WRITABLE_BEGIN_OFFSET = 32;
98+
private static final int HEAP_WRITABLE_END_OFFSET = 40;
99+
private static final int HEAP_WRITABLE_PATCHED_BEGIN_OFFSET = 48;
100+
private static final int HEAP_WRITABLE_PATCHED_END_OFFSET = 56;
101+
private static final int NEXT_SECTION_OFFSET = 64;
102+
private static final int STATIC_SECTION_SIZE = 72;
105103

106104
private static final String CACHED_IMAGE_FDS_NAME = "__svm_layer_cached_image_fds";
107105
private static final String CACHED_IMAGE_HEAP_OFFSETS_NAME = "__svm_layer_cached_image_heap_offsets";
@@ -241,7 +239,6 @@ public void beforeImageWrite(BeforeImageWriteAccess access) {
241239
layeredSectionData.markRelocationSite(HEAP_END_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_END_SYMBOL_NAME, 0);
242240
layeredSectionData.markRelocationSite(HEAP_RELOCATABLE_BEGIN_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_RELOCATABLE_BEGIN_SYMBOL_NAME, 0);
243241
layeredSectionData.markRelocationSite(HEAP_RELOCATABLE_END_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_RELOCATABLE_END_SYMBOL_NAME, 0);
244-
layeredSectionData.markRelocationSite(HEAP_ANY_RELOCATABLE_POINTER_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_A_RELOCATABLE_POINTER_SYMBOL_NAME, 0);
245242
layeredSectionData.markRelocationSite(HEAP_WRITABLE_BEGIN_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_WRITABLE_BEGIN_SYMBOL_NAME, 0);
246243
layeredSectionData.markRelocationSite(HEAP_WRITABLE_END_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_WRITABLE_END_SYMBOL_NAME, 0);
247244
layeredSectionData.markRelocationSite(HEAP_WRITABLE_PATCHED_BEGIN_OFFSET, ObjectFile.RelocationKind.DIRECT_8, Isolates.IMAGE_HEAP_WRITABLE_PATCHED_BEGIN_SYMBOL_NAME, 0);
@@ -305,7 +302,6 @@ public int getEntryOffsetInternal(SectionEntries entry) {
305302
case HEAP_END -> HEAP_END_OFFSET;
306303
case HEAP_RELOCATABLE_BEGIN -> HEAP_RELOCATABLE_BEGIN_OFFSET;
307304
case HEAP_RELOCATABLE_END -> HEAP_RELOCATABLE_END_OFFSET;
308-
case HEAP_ANY_RELOCATABLE_POINTER -> HEAP_ANY_RELOCATABLE_POINTER_OFFSET;
309305
case HEAP_WRITEABLE_BEGIN -> HEAP_WRITABLE_BEGIN_OFFSET;
310306
case HEAP_WRITEABLE_END -> HEAP_WRITABLE_END_OFFSET;
311307
case HEAP_WRITEABLE_PATCHED_BEGIN -> HEAP_WRITABLE_PATCHED_BEGIN_OFFSET;

0 commit comments

Comments
 (0)