addsection.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* Add an uninitialized data section to an executable.
  2. Copyright (C) 1999, 2001-2012 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. Andrew Innes <andrewi@harlequin.co.uk> 04-Jan-1999
  15. based on code from unexw32.c
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <time.h>
  21. #ifdef __GNUC__
  22. #define _ANONYMOUS_UNION
  23. #define _ANONYMOUS_STRUCT
  24. #endif
  25. #include <windows.h>
  26. /* Include relevant definitions from IMAGEHLP.H, which can be found
  27. in \\win32sdk\mstools\samples\image\include\imagehlp.h. */
  28. PIMAGE_NT_HEADERS
  29. (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress,
  30. DWORD FileLength,
  31. LPDWORD HeaderSum,
  32. LPDWORD CheckSum);
  33. #undef min
  34. #undef max
  35. #define min(x, y) (((x) < (y)) ? (x) : (y))
  36. #define max(x, y) (((x) > (y)) ? (x) : (y))
  37. /* File handling. */
  38. typedef struct file_data {
  39. const char *name;
  40. unsigned long size;
  41. HANDLE file;
  42. HANDLE file_mapping;
  43. unsigned char *file_base;
  44. } file_data;
  45. int
  46. open_input_file (file_data *p_file, const char *filename)
  47. {
  48. HANDLE file;
  49. HANDLE file_mapping;
  50. void *file_base;
  51. unsigned long size, upper_size;
  52. file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
  53. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  54. if (file == INVALID_HANDLE_VALUE)
  55. return FALSE;
  56. size = GetFileSize (file, &upper_size);
  57. file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
  58. 0, size, NULL);
  59. if (!file_mapping)
  60. return FALSE;
  61. file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
  62. if (file_base == 0)
  63. return FALSE;
  64. p_file->name = filename;
  65. p_file->size = size;
  66. p_file->file = file;
  67. p_file->file_mapping = file_mapping;
  68. p_file->file_base = file_base;
  69. return TRUE;
  70. }
  71. int
  72. open_output_file (file_data *p_file, const char *filename, unsigned long size)
  73. {
  74. HANDLE file;
  75. HANDLE file_mapping;
  76. void *file_base;
  77. file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  78. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  79. if (file == INVALID_HANDLE_VALUE)
  80. return FALSE;
  81. file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
  82. 0, size, NULL);
  83. if (!file_mapping)
  84. return FALSE;
  85. file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
  86. if (file_base == 0)
  87. return FALSE;
  88. p_file->name = filename;
  89. p_file->size = size;
  90. p_file->file = file;
  91. p_file->file_mapping = file_mapping;
  92. p_file->file_base = file_base;
  93. return TRUE;
  94. }
  95. /* Close the system structures associated with the given file. */
  96. void
  97. close_file_data (file_data *p_file)
  98. {
  99. UnmapViewOfFile (p_file->file_base);
  100. CloseHandle (p_file->file_mapping);
  101. /* For the case of output files, set final size. */
  102. SetFilePointer (p_file->file, p_file->size, NULL, FILE_BEGIN);
  103. SetEndOfFile (p_file->file);
  104. CloseHandle (p_file->file);
  105. }
  106. /* Routines to manipulate NT executable file sections. */
  107. unsigned long
  108. get_unrounded_section_size (PIMAGE_SECTION_HEADER p_section)
  109. {
  110. /* The true section size, before rounding, for an initialized data or
  111. code section. (Supposedly some linkers swap the meaning of these
  112. two values.) */
  113. return min (p_section->SizeOfRawData,
  114. p_section->Misc.VirtualSize);
  115. }
  116. /* Return pointer to section header for named section. */
  117. IMAGE_SECTION_HEADER *
  118. find_section (const char *name, IMAGE_NT_HEADERS *nt_header)
  119. {
  120. PIMAGE_SECTION_HEADER section;
  121. int i;
  122. section = IMAGE_FIRST_SECTION (nt_header);
  123. for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
  124. {
  125. if (strcmp (section->Name, name) == 0)
  126. return section;
  127. section++;
  128. }
  129. return NULL;
  130. }
  131. /* Return pointer to section header for section containing the given
  132. relative virtual address. */
  133. IMAGE_SECTION_HEADER *
  134. rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
  135. {
  136. PIMAGE_SECTION_HEADER section;
  137. int i;
  138. section = IMAGE_FIRST_SECTION (nt_header);
  139. for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
  140. {
  141. /* Some linkers (eg. the NT SDK linker I believe) swapped the
  142. meaning of these two values - or rather, they ignored
  143. VirtualSize entirely and always set it to zero. This affects
  144. some very old exes (eg. gzip dated Dec 1993). Since
  145. w32_executable_type relies on this function to work reliably,
  146. we need to cope with this. */
  147. DWORD real_size = max (section->SizeOfRawData,
  148. section->Misc.VirtualSize);
  149. if (rva >= section->VirtualAddress
  150. && rva < section->VirtualAddress + real_size)
  151. return section;
  152. section++;
  153. }
  154. return NULL;
  155. }
  156. /* Return pointer to section header for section containing the given
  157. offset in its raw data area. */
  158. IMAGE_SECTION_HEADER *
  159. offset_to_section (DWORD offset, IMAGE_NT_HEADERS * nt_header)
  160. {
  161. PIMAGE_SECTION_HEADER section;
  162. int i;
  163. section = IMAGE_FIRST_SECTION (nt_header);
  164. for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
  165. {
  166. if (offset >= section->PointerToRawData
  167. && offset < section->PointerToRawData + section->SizeOfRawData)
  168. return section;
  169. section++;
  170. }
  171. return NULL;
  172. }
  173. /* Return offset to an object in dst, given offset in src. We assume
  174. there is at least one section in both src and dst images, and that
  175. the some sections may have been added to dst (after sections in src). */
  176. static DWORD
  177. relocate_offset (DWORD offset,
  178. IMAGE_NT_HEADERS * src_nt_header,
  179. IMAGE_NT_HEADERS * dst_nt_header)
  180. {
  181. PIMAGE_SECTION_HEADER src_section = IMAGE_FIRST_SECTION (src_nt_header);
  182. PIMAGE_SECTION_HEADER dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
  183. int i = 0;
  184. while (offset >= src_section->PointerToRawData)
  185. {
  186. if (offset < src_section->PointerToRawData + src_section->SizeOfRawData)
  187. break;
  188. i++;
  189. if (i == src_nt_header->FileHeader.NumberOfSections)
  190. {
  191. /* Handle offsets after the last section. */
  192. dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
  193. dst_section += dst_nt_header->FileHeader.NumberOfSections - 1;
  194. while (dst_section->PointerToRawData == 0)
  195. dst_section--;
  196. while (src_section->PointerToRawData == 0)
  197. src_section--;
  198. return offset
  199. + (dst_section->PointerToRawData + dst_section->SizeOfRawData)
  200. - (src_section->PointerToRawData + src_section->SizeOfRawData);
  201. }
  202. src_section++;
  203. dst_section++;
  204. }
  205. return offset +
  206. (dst_section->PointerToRawData - src_section->PointerToRawData);
  207. }
  208. #define OFFSET_TO_RVA(offset, section) \
  209. (section->VirtualAddress + ((DWORD)(offset) - section->PointerToRawData))
  210. #define RVA_TO_OFFSET(rva, section) \
  211. (section->PointerToRawData + ((DWORD)(rva) - section->VirtualAddress))
  212. #define RVA_TO_SECTION_OFFSET(rva, section) \
  213. ((DWORD)(rva) - section->VirtualAddress)
  214. /* Convert address in executing image to RVA. */
  215. #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
  216. #define PTR_TO_OFFSET(ptr, pfile_data) \
  217. ((unsigned const char *)(ptr) - (pfile_data)->file_base)
  218. #define OFFSET_TO_PTR(offset, pfile_data) \
  219. ((pfile_data)->file_base + (DWORD)(offset))
  220. #define ROUND_UP(p, align) (((DWORD)(p) + (align)-1) & ~((align)-1))
  221. #define ROUND_DOWN(p, align) ((DWORD)(p) & ~((align)-1))
  222. static void
  223. copy_executable_and_add_section (file_data *p_infile,
  224. file_data *p_outfile,
  225. const char *new_section_name,
  226. DWORD new_section_size)
  227. {
  228. unsigned char *dst;
  229. PIMAGE_DOS_HEADER dos_header;
  230. PIMAGE_NT_HEADERS nt_header;
  231. PIMAGE_NT_HEADERS dst_nt_header;
  232. PIMAGE_SECTION_HEADER section;
  233. PIMAGE_SECTION_HEADER dst_section;
  234. DWORD offset;
  235. int i;
  236. int be_verbose = GetEnvironmentVariable ("DEBUG_DUMP", NULL, 0) > 0;
  237. #define COPY_CHUNK(message, src, size, verbose) \
  238. do { \
  239. unsigned const char *s = (void *)(src); \
  240. unsigned long count = (size); \
  241. if (verbose) \
  242. { \
  243. printf ("%s\n", (message)); \
  244. printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
  245. printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
  246. printf ("\t0x%08x Size in bytes.\n", count); \
  247. } \
  248. memcpy (dst, s, count); \
  249. dst += count; \
  250. } while (0)
  251. #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
  252. #define ROUND_UP_DST_AND_ZERO(align) \
  253. do { \
  254. unsigned char *newdst = p_outfile->file_base \
  255. + ROUND_UP (DST_TO_OFFSET (), (align)); \
  256. /* Zero the alignment slop; it may actually initialize real data. */ \
  257. memset (dst, 0, newdst - dst); \
  258. dst = newdst; \
  259. } while (0)
  260. /* Copy the source image sequentially, ie. section by section after
  261. copying the headers and section table, to simplify the process of
  262. adding an extra section table entry (which might force the raw
  263. section data to be relocated).
  264. Note that dst is updated implicitly by each COPY_CHUNK. */
  265. dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
  266. nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
  267. dos_header->e_lfanew);
  268. section = IMAGE_FIRST_SECTION (nt_header);
  269. dst = (unsigned char *) p_outfile->file_base;
  270. COPY_CHUNK ("Copying DOS header...", dos_header,
  271. (DWORD) nt_header - (DWORD) dos_header, be_verbose);
  272. dst_nt_header = (PIMAGE_NT_HEADERS) dst;
  273. COPY_CHUNK ("Copying NT header...", nt_header,
  274. (DWORD) section - (DWORD) nt_header, be_verbose);
  275. dst_section = (PIMAGE_SECTION_HEADER) dst;
  276. COPY_CHUNK ("Copying section table...", section,
  277. nt_header->FileHeader.NumberOfSections * sizeof (*section),
  278. be_verbose);
  279. /* To improve the efficiency of demand loading, make the file
  280. alignment match the section alignment (VC++ 6.0 does this by
  281. default anyway). */
  282. dst_nt_header->OptionalHeader.FileAlignment =
  283. dst_nt_header->OptionalHeader.SectionAlignment;
  284. /* Add an uninitialized data section at the end, of the specified name
  285. and virtual size. */
  286. if (find_section (new_section_name, nt_header) == NULL)
  287. /* Leave room for extra section table entry; filled in below. */
  288. dst += sizeof (*section);
  289. else
  290. new_section_name = NULL;
  291. /* Align the first section's raw data area, and set the header size
  292. field accordingly. */
  293. ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
  294. dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET ();
  295. for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
  296. {
  297. char msg[100];
  298. /* Windows section names are fixed 8-char strings, only
  299. zero-terminated if the name is shorter than 8 characters. */
  300. sprintf (msg, "Copying raw data for %.8s...", section->Name);
  301. /* Update the file-relative offset for this section's raw data (if
  302. it has any) in case things have been relocated; we will update
  303. the other offsets below once we know where everything is. */
  304. if (dst_section->PointerToRawData)
  305. dst_section->PointerToRawData = DST_TO_OFFSET ();
  306. /* Can always copy the original raw data. */
  307. COPY_CHUNK
  308. (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile),
  309. section->SizeOfRawData, be_verbose);
  310. /* Round up the raw data size to the new alignment. */
  311. dst_section->SizeOfRawData =
  312. ROUND_UP (dst_section->SizeOfRawData,
  313. dst_nt_header->OptionalHeader.FileAlignment);
  314. /* Align the next section's raw data area. */
  315. ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
  316. section++;
  317. dst_section++;
  318. }
  319. /* Add the extra section entry (which adds no raw data). */
  320. if (new_section_name != NULL)
  321. {
  322. dst_nt_header->FileHeader.NumberOfSections++;
  323. dst_nt_header->OptionalHeader.SizeOfImage += new_section_size;
  324. strncpy (dst_section->Name, new_section_name, sizeof (dst_section->Name));
  325. dst_section->VirtualAddress =
  326. section[-1].VirtualAddress
  327. + ROUND_UP (section[-1].Misc.VirtualSize,
  328. dst_nt_header->OptionalHeader.SectionAlignment);
  329. dst_section->Misc.VirtualSize = new_section_size;
  330. dst_section->PointerToRawData = 0;
  331. dst_section->SizeOfRawData = 0;
  332. dst_section->Characteristics =
  333. IMAGE_SCN_CNT_UNINITIALIZED_DATA
  334. | IMAGE_SCN_MEM_READ
  335. | IMAGE_SCN_MEM_WRITE;
  336. }
  337. /* Copy remainder of source image. */
  338. section--;
  339. offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData,
  340. nt_header->OptionalHeader.FileAlignment);
  341. COPY_CHUNK
  342. ("Copying remainder of executable...",
  343. OFFSET_TO_PTR (offset, p_infile),
  344. p_infile->size - offset, be_verbose);
  345. /* Final size for new image. */
  346. p_outfile->size = DST_TO_OFFSET ();
  347. /* Now patch up remaining file-relative offsets. */
  348. section = IMAGE_FIRST_SECTION (nt_header);
  349. dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
  350. #define ADJUST_OFFSET(var) \
  351. do { \
  352. if ((var) != 0) \
  353. (var) = relocate_offset ((var), nt_header, dst_nt_header); \
  354. } while (0)
  355. dst_nt_header->OptionalHeader.SizeOfInitializedData = 0;
  356. dst_nt_header->OptionalHeader.SizeOfUninitializedData = 0;
  357. for (i = 0; i < dst_nt_header->FileHeader.NumberOfSections; i++)
  358. {
  359. /* Recompute data sizes for completeness. */
  360. if (dst_section[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
  361. dst_nt_header->OptionalHeader.SizeOfInitializedData +=
  362. ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
  363. else if (dst_section[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
  364. dst_nt_header->OptionalHeader.SizeOfUninitializedData +=
  365. ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
  366. ADJUST_OFFSET (dst_section[i].PointerToLinenumbers);
  367. }
  368. ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable);
  369. /* Update offsets in debug directory entries. */
  370. {
  371. IMAGE_DATA_DIRECTORY debug_dir =
  372. dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
  373. PIMAGE_DEBUG_DIRECTORY debug_entry;
  374. section = rva_to_section (debug_dir.VirtualAddress, dst_nt_header);
  375. if (section)
  376. {
  377. debug_entry = (PIMAGE_DEBUG_DIRECTORY)
  378. (RVA_TO_OFFSET (debug_dir.VirtualAddress, section) + p_outfile->file_base);
  379. debug_dir.Size /= sizeof (IMAGE_DEBUG_DIRECTORY);
  380. for (i = 0; i < debug_dir.Size; i++, debug_entry++)
  381. ADJUST_OFFSET (debug_entry->PointerToRawData);
  382. }
  383. }
  384. }
  385. int
  386. main (int argc, char **argv)
  387. {
  388. file_data in_file, out_file;
  389. char out_filename[MAX_PATH], in_filename[MAX_PATH];
  390. unsigned long size;
  391. PIMAGE_DOS_HEADER dos_header;
  392. PIMAGE_NT_HEADERS nt_header;
  393. #define OLD_NAME argv[1]
  394. #define NEW_NAME argv[2]
  395. #define SECTION_NAME argv[3]
  396. #define SECTION_SIZE argv[4]
  397. strcpy (in_filename, OLD_NAME);
  398. strcpy (out_filename, NEW_NAME);
  399. printf ("Dumping from %s\n", in_filename);
  400. printf (" to %s\n", out_filename);
  401. /* Open the undumped executable file. */
  402. if (!open_input_file (&in_file, in_filename))
  403. {
  404. printf ("Failed to open %s (%d)...bailing.\n",
  405. in_filename, GetLastError ());
  406. exit (1);
  407. }
  408. dos_header = (PIMAGE_DOS_HEADER) in_file.file_base;
  409. nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
  410. /* Allow for expansion due to increasing file align to section align.
  411. We can overestimate here, since close_file_data will update the
  412. size exactly. */
  413. size = in_file.size
  414. + nt_header->OptionalHeader.SectionAlignment
  415. * nt_header->FileHeader.NumberOfSections;
  416. if (!open_output_file (&out_file, out_filename, size))
  417. {
  418. printf ("Failed to open %s (%d)...bailing.\n",
  419. out_filename, GetLastError ());
  420. exit (1);
  421. }
  422. copy_executable_and_add_section (&in_file, &out_file,
  423. SECTION_NAME,
  424. atoi (SECTION_SIZE) * 1024 * 1024);
  425. /* Patch up header fields; profiler is picky about this. */
  426. {
  427. HANDLE hImagehelp = LoadLibrary ("imagehlp.dll");
  428. DWORD headersum;
  429. DWORD checksum;
  430. dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
  431. nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
  432. nt_header->OptionalHeader.CheckSum = 0;
  433. // nt_header->FileHeader.TimeDateStamp = time (NULL);
  434. // dos_header->e_cp = size / 512;
  435. // nt_header->OptionalHeader.SizeOfImage = size;
  436. pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
  437. if (pfnCheckSumMappedFile)
  438. {
  439. // nt_header->FileHeader.TimeDateStamp = time (NULL);
  440. pfnCheckSumMappedFile (out_file.file_base,
  441. out_file.size,
  442. &headersum,
  443. &checksum);
  444. nt_header->OptionalHeader.CheckSum = checksum;
  445. }
  446. FreeLibrary (hImagehelp);
  447. }
  448. close_file_data (&in_file);
  449. close_file_data (&out_file);
  450. return 0;
  451. }
  452. /* eof */