comparison nt/addsection.c @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 23a1cea22d13
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 /* Add an uninitialized data section to an executable. 1 /* Add an uninitialized data section to an executable.
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 3
4 This file is part of GNU Emacs. 4 This file is part of GNU Emacs.
5 5
6 GNU Emacs is free software; you can redistribute it and/or modify 6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to 17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02111-1307, USA. 19 Boston, MA 02110-1301, USA.
20 20
21 Andrew Innes <andrewi@harlequin.co.uk> 04-Jan-1999 21 Andrew Innes <andrewi@harlequin.co.uk> 04-Jan-1999
22 based on code from unexw32.c 22 based on code from unexw32.c
23 */ 23 */
24 24
281 PIMAGE_NT_HEADERS dst_nt_header; 281 PIMAGE_NT_HEADERS dst_nt_header;
282 PIMAGE_SECTION_HEADER section; 282 PIMAGE_SECTION_HEADER section;
283 PIMAGE_SECTION_HEADER dst_section; 283 PIMAGE_SECTION_HEADER dst_section;
284 DWORD offset; 284 DWORD offset;
285 int i; 285 int i;
286 286 int be_verbose = GetEnvironmentVariable ("DEBUG_DUMP", NULL, 0) > 0;
287 #define COPY_CHUNK(message, src, size) \ 287
288 #define COPY_CHUNK(message, src, size, verbose) \
288 do { \ 289 do { \
289 unsigned char *s = (void *)(src); \ 290 unsigned char *s = (void *)(src); \
290 unsigned long count = (size); \ 291 unsigned long count = (size); \
291 printf ("%s\n", (message)); \ 292 if (verbose) \
292 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \ 293 { \
293 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \ 294 printf ("%s\n", (message)); \
294 printf ("\t0x%08x Size in bytes.\n", count); \ 295 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
296 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
297 printf ("\t0x%08x Size in bytes.\n", count); \
298 } \
295 memcpy (dst, s, count); \ 299 memcpy (dst, s, count); \
296 dst += count; \ 300 dst += count; \
297 } while (0) 301 } while (0)
298 302
299 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile) 303 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
319 section = IMAGE_FIRST_SECTION (nt_header); 323 section = IMAGE_FIRST_SECTION (nt_header);
320 324
321 dst = (unsigned char *) p_outfile->file_base; 325 dst = (unsigned char *) p_outfile->file_base;
322 326
323 COPY_CHUNK ("Copying DOS header...", dos_header, 327 COPY_CHUNK ("Copying DOS header...", dos_header,
324 (DWORD) nt_header - (DWORD) dos_header); 328 (DWORD) nt_header - (DWORD) dos_header, be_verbose);
325 dst_nt_header = (PIMAGE_NT_HEADERS) dst; 329 dst_nt_header = (PIMAGE_NT_HEADERS) dst;
326 COPY_CHUNK ("Copying NT header...", nt_header, 330 COPY_CHUNK ("Copying NT header...", nt_header,
327 (DWORD) section - (DWORD) nt_header); 331 (DWORD) section - (DWORD) nt_header, be_verbose);
328 dst_section = (PIMAGE_SECTION_HEADER) dst; 332 dst_section = (PIMAGE_SECTION_HEADER) dst;
329 COPY_CHUNK ("Copying section table...", section, 333 COPY_CHUNK ("Copying section table...", section,
330 nt_header->FileHeader.NumberOfSections * sizeof (*section)); 334 nt_header->FileHeader.NumberOfSections * sizeof (*section),
335 be_verbose);
331 336
332 /* To improve the efficiency of demand loading, make the file 337 /* To improve the efficiency of demand loading, make the file
333 alignment match the section alignment (VC++ 6.0 does this by 338 alignment match the section alignment (VC++ 6.0 does this by
334 default anyway). */ 339 default anyway). */
335 dst_nt_header->OptionalHeader.FileAlignment = 340 dst_nt_header->OptionalHeader.FileAlignment =
349 dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET (); 354 dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET ();
350 355
351 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) 356 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
352 { 357 {
353 char msg[100]; 358 char msg[100];
354 sprintf (msg, "Copying raw data for %s...", section->Name); 359 /* Windows section names are fixed 8-char strings, only
360 zero-terminated if the name is shorter than 8 characters. */
361 sprintf (msg, "Copying raw data for %.8s...", section->Name);
355 362
356 /* Update the file-relative offset for this section's raw data (if 363 /* Update the file-relative offset for this section's raw data (if
357 it has any) in case things have been relocated; we will update 364 it has any) in case things have been relocated; we will update
358 the other offsets below once we know where everything is. */ 365 the other offsets below once we know where everything is. */
359 if (dst_section->PointerToRawData) 366 if (dst_section->PointerToRawData)
360 dst_section->PointerToRawData = DST_TO_OFFSET (); 367 dst_section->PointerToRawData = DST_TO_OFFSET ();
361 368
362 /* Can always copy the original raw data. */ 369 /* Can always copy the original raw data. */
363 COPY_CHUNK 370 COPY_CHUNK
364 (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile), 371 (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile),
365 section->SizeOfRawData); 372 section->SizeOfRawData, be_verbose);
366 373
367 /* Round up the raw data size to the new alignment. */ 374 /* Round up the raw data size to the new alignment. */
368 dst_section->SizeOfRawData = 375 dst_section->SizeOfRawData =
369 ROUND_UP (dst_section->SizeOfRawData, 376 ROUND_UP (dst_section->SizeOfRawData,
370 dst_nt_header->OptionalHeader.FileAlignment); 377 dst_nt_header->OptionalHeader.FileAlignment);
400 offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData, 407 offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData,
401 nt_header->OptionalHeader.FileAlignment); 408 nt_header->OptionalHeader.FileAlignment);
402 COPY_CHUNK 409 COPY_CHUNK
403 ("Copying remainder of executable...", 410 ("Copying remainder of executable...",
404 OFFSET_TO_PTR (offset, p_infile), 411 OFFSET_TO_PTR (offset, p_infile),
405 p_infile->size - offset); 412 p_infile->size - offset, be_verbose);
406 413
407 /* Final size for new image. */ 414 /* Final size for new image. */
408 p_outfile->size = DST_TO_OFFSET (); 415 p_outfile->size = DST_TO_OFFSET ();
409 416
410 /* Now patch up remaining file-relative offsets. */ 417 /* Now patch up remaining file-relative offsets. */
532 539
533 return 0; 540 return 0;
534 } 541 }
535 542
536 /* eof */ 543 /* eof */
544
545 /* arch-tag: 17e2b0aa-8c17-4bd1-b24b-1cda689245fa
546 (do not change this comment) */