+ for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
+ {
+ char msg[100];
+ sprintf (msg, "Copying raw data for %s...", section->Name);
+
+ dst_save = dst;
+
+ /* Update the file-relative offset for this section's raw data (if
+ it has any) in case things have been relocated; we will update
+ the other offsets below once we know where everything is. */
+ if (dst_section->PointerToRawData)
+ dst_section->PointerToRawData = DST_TO_OFFSET ();
+
+ /* Can always copy the original raw data. */
+ COPY_CHUNK
+ (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile),
+ section->SizeOfRawData);
+ /* Ensure alignment slop is zeroed. */
+ ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
+
+ /* Note that various sections below may be aliases. */
+ if (section == data_section)
+ {
+ dst = dst_save
+ + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section);
+ COPY_PROC_CHUNK ("Dumping initialized data...", data_start, data_size);
+ dst = dst_save + dst_section->SizeOfRawData;
+ }
+ if (section == bss_section)
+ {
+ /* Dump contents of bss variables, adjusting the section's raw
+ data size as necessary. */
+ dst = dst_save
+ + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start), dst_section);
+ COPY_PROC_CHUNK ("Dumping bss data...", bss_start, bss_size);
+ ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
+ dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
+ /* Determine new size of raw data area. */
+ dst = max (dst, dst_save + dst_section->SizeOfRawData);
+ dst_section->SizeOfRawData = dst - dst_save;
+ dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
+ dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
+ }
+ if (section == bss_section_static)
+ {
+ /* Dump contents of static bss variables, adjusting the
+ section's raw data size as necessary. */
+ dst = dst_save
+ + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static), dst_section);
+ COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, bss_size_static);
+ ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
+ dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
+ /* Determine new size of raw data area. */
+ dst = max (dst, dst_save + dst_section->SizeOfRawData);
+ dst_section->SizeOfRawData = dst - dst_save;
+ dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
+ dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
+ }
+ if (section == heap_section)
+ {
+ DWORD heap_start = get_heap_start ();
+ DWORD heap_size = get_committed_heap_size ();
+
+ /* Dump the used portion of the predump heap, adjusting the
+ section's size to the appropriate size. */
+ dst = dst_save
+ + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start), dst_section);
+ COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size);
+ ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
+ dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
+ /* Determine new size of raw data area. */
+ dst = max (dst, dst_save + dst_section->SizeOfRawData);
+ dst_section->SizeOfRawData = dst - dst_save;
+ /* Reduce the size of the heap section to fit (must be last
+ section). */
+ dst_nt_header->OptionalHeader.SizeOfImage -=
+ dst_section->Misc.VirtualSize
+ - ROUND_UP (dst_section->SizeOfRawData,
+ dst_nt_header->OptionalHeader.SectionAlignment);
+ dst_section->Misc.VirtualSize =
+ ROUND_UP (dst_section->SizeOfRawData,
+ dst_nt_header->OptionalHeader.SectionAlignment);
+ dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
+ dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
+ }