@@ -209,7 +209,7 @@ class CoffHeaders {
209
209
int get size => optionalHeader.headersSize;
210
210
211
211
void addSnapshotSectionHeader (int length) {
212
- final oldSize = size ;
212
+ final oldHeadersSize = optionalHeader.headersSize ;
213
213
final address =
214
214
align (sectionTable.addressEnd, optionalHeader.sectionAlignment);
215
215
final offset = align (sectionTable.offsetEnd, optionalHeader.fileAlignment);
@@ -227,30 +227,35 @@ class CoffHeaders {
227
227
// Increment the number of sections in the file header.
228
228
fileHeader.sectionCount += 1 ;
229
229
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.
231
232
optionalHeader.headersSize = align (
232
233
_coffOffset + fileHeader.size + optionalHeader.size + sectionTable.size,
233
234
optionalHeader.fileAlignment);
234
- optionalHeader.imageSize += align (length, optionalHeader.fileAlignment);
235
235
236
236
// 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 ;
239
239
if (headersSizeDiff > 0 ) {
240
240
// Safety check that section virtual addresses need not be adjusted, as
241
241
// that requires rewriting much more of the fields and section contents.
242
242
// (Generally, the size of the headers is much smaller than the section
243
243
// alignment and so this is not expected to happen.)
244
244
if (size ~ / optionalHeader.sectionAlignment !=
245
- oldSize ~ / optionalHeader.sectionAlignment) {
245
+ oldHeadersSize ~ / optionalHeader.sectionAlignment) {
246
246
throw 'Adding the snapshot would require adjusting virtual addresses' ;
247
247
}
248
248
assert (headersSizeDiff % optionalHeader.fileAlignment == 0 );
249
249
for (final entry in sectionTable.entries) {
250
250
entry.fileOffset += headersSizeDiff;
251
251
}
252
- optionalHeader.imageSize += headersSizeDiff;
253
252
}
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);
254
259
}
255
260
256
261
Future <void > write (RandomAccessFile output) async {
0 commit comments