Skip to content

Commit da691ea

Browse files
sstricklCommit Bot
authored and
Commit Bot
committed
[pkg/dart2native] Create PE images with sizes that are section aligned.
Bug: #39106 Change-Id: If1c88b4969fa44ffc6d764d3d1e34732acdf4d64 Cq-Include-Trybots: luci.dart.try:pkg-win-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/238541 Reviewed-by: Aske Simon Christensen <[email protected]> Commit-Queue: Tess Strickland <[email protected]>
1 parent 1a3ae1e commit da691ea

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

pkg/dart2native/lib/dart2native_pe.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class CoffHeaders {
209209
int get size => optionalHeader.headersSize;
210210

211211
void addSnapshotSectionHeader(int length) {
212-
final oldSize = size;
212+
final oldHeadersSize = optionalHeader.headersSize;
213213
final address =
214214
align(sectionTable.addressEnd, optionalHeader.sectionAlignment);
215215
final offset = align(sectionTable.offsetEnd, optionalHeader.fileAlignment);
@@ -227,30 +227,35 @@ class CoffHeaders {
227227
// Increment the number of sections in the file header.
228228
fileHeader.sectionCount += 1;
229229

230-
// Adjust the sizes stored in the optional header.
230+
// Adjust the header size stored in the optional header, which must be
231+
// a multiple of fileAlignment.
231232
optionalHeader.headersSize = align(
232233
_coffOffset + fileHeader.size + optionalHeader.size + sectionTable.size,
233234
optionalHeader.fileAlignment);
234-
optionalHeader.imageSize += align(length, optionalHeader.fileAlignment);
235235

236236
// If the size of the headers changed, we'll need to adjust the section
237-
// offsets and image size accordingly.
238-
final headersSizeDiff = size - oldSize;
237+
// offsets.
238+
final headersSizeDiff = optionalHeader.headersSize - oldHeadersSize;
239239
if (headersSizeDiff > 0) {
240240
// Safety check that section virtual addresses need not be adjusted, as
241241
// that requires rewriting much more of the fields and section contents.
242242
// (Generally, the size of the headers is much smaller than the section
243243
// alignment and so this is not expected to happen.)
244244
if (size ~/ optionalHeader.sectionAlignment !=
245-
oldSize ~/ optionalHeader.sectionAlignment) {
245+
oldHeadersSize ~/ optionalHeader.sectionAlignment) {
246246
throw 'Adding the snapshot would require adjusting virtual addresses';
247247
}
248248
assert(headersSizeDiff % optionalHeader.fileAlignment == 0);
249249
for (final entry in sectionTable.entries) {
250250
entry.fileOffset += headersSizeDiff;
251251
}
252-
optionalHeader.imageSize += headersSizeDiff;
253252
}
253+
254+
// Adjust the image size stored in the optional header, which must be a
255+
// multiple of section alignment (as it is the size in memory, not on disk).
256+
optionalHeader.imageSize = align(
257+
newHeader.virtualAddress + newHeader.virtualSize,
258+
optionalHeader.sectionAlignment);
254259
}
255260

256261
Future<void> write(RandomAccessFile output) async {

0 commit comments

Comments
 (0)