You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #define _GNU_SOURCE
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <getopt.h>
  28. #include <bfd.h>
  29. #include <gpxe/efi/efi.h>
  30. #include <gpxe/efi/IndustryStandard/PeImage.h>
  31. #include <libgen.h>
  32. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  33. #define EFI_FILE_ALIGN 0x20
  34. struct pe_section {
  35. struct pe_section *next;
  36. EFI_IMAGE_SECTION_HEADER hdr;
  37. uint8_t contents[0];
  38. };
  39. struct pe_relocs {
  40. struct pe_relocs *next;
  41. unsigned long start_rva;
  42. unsigned int used_relocs;
  43. unsigned int total_relocs;
  44. uint16_t *relocs;
  45. };
  46. struct pe_header {
  47. EFI_IMAGE_DOS_HEADER dos;
  48. uint8_t padding[128];
  49. #if defined(MDE_CPU_IA32)
  50. EFI_IMAGE_NT_HEADERS32 nt;
  51. #elif defined(MDE_CPU_X64)
  52. EFI_IMAGE_NT_HEADERS64 nt;
  53. #endif
  54. };
  55. static struct pe_header efi_pe_header = {
  56. .dos = {
  57. .e_magic = EFI_IMAGE_DOS_SIGNATURE,
  58. .e_lfanew = offsetof ( typeof ( efi_pe_header ), nt ),
  59. },
  60. .nt = {
  61. .Signature = EFI_IMAGE_NT_SIGNATURE,
  62. .FileHeader = {
  63. #if defined(MDE_CPU_IA32)
  64. .Machine = EFI_IMAGE_MACHINE_IA32,
  65. #elif defined(MDE_CPU_X64)
  66. .Machine = EFI_IMAGE_MACHINE_X64,
  67. #endif
  68. .TimeDateStamp = 0x10d1a884,
  69. .SizeOfOptionalHeader =
  70. sizeof ( efi_pe_header.nt.OptionalHeader ),
  71. .Characteristics = ( EFI_IMAGE_FILE_DLL |
  72. #if defined(MDE_CPU_IA32)
  73. EFI_IMAGE_FILE_32BIT_MACHINE |
  74. #endif
  75. EFI_IMAGE_FILE_EXECUTABLE_IMAGE ),
  76. },
  77. .OptionalHeader = {
  78. #if defined(MDE_CPU_IA32)
  79. .Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC,
  80. #elif defined(MDE_CPU_X64)
  81. .Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC,
  82. #endif
  83. .SectionAlignment = EFI_FILE_ALIGN,
  84. .FileAlignment = EFI_FILE_ALIGN,
  85. .SizeOfImage = sizeof ( efi_pe_header ),
  86. .SizeOfHeaders = sizeof ( efi_pe_header ),
  87. .NumberOfRvaAndSizes =
  88. EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES,
  89. },
  90. },
  91. };
  92. /** Command-line options */
  93. struct options {
  94. unsigned int subsystem;
  95. };
  96. /**
  97. * Allocate memory
  98. *
  99. * @v len Length of memory to allocate
  100. * @ret ptr Pointer to allocated memory
  101. */
  102. static void * xmalloc ( size_t len ) {
  103. void *ptr;
  104. ptr = malloc ( len );
  105. if ( ! ptr ) {
  106. eprintf ( "Could not allocate %zd bytes\n", len );
  107. exit ( 1 );
  108. }
  109. return ptr;
  110. }
  111. /**
  112. * Align section within PE file
  113. *
  114. * @v offset Unaligned offset
  115. * @ret aligned_offset Aligned offset
  116. */
  117. static unsigned long efi_file_align ( unsigned long offset ) {
  118. return ( ( offset + EFI_FILE_ALIGN - 1 ) & ~( EFI_FILE_ALIGN - 1 ) );
  119. }
  120. /**
  121. * Generate entry in PE relocation table
  122. *
  123. * @v pe_reltab PE relocation table
  124. * @v rva RVA
  125. * @v size Size of relocation entry
  126. */
  127. static void generate_pe_reloc ( struct pe_relocs **pe_reltab,
  128. unsigned long rva, size_t size ) {
  129. unsigned long start_rva;
  130. uint16_t reloc;
  131. struct pe_relocs *pe_rel;
  132. uint16_t *relocs;
  133. /* Construct */
  134. start_rva = ( rva & ~0xfff );
  135. reloc = ( rva & 0xfff );
  136. switch ( size ) {
  137. case 8:
  138. reloc |= 0xa000;
  139. break;
  140. case 4:
  141. reloc |= 0x3000;
  142. break;
  143. case 2:
  144. reloc |= 0x2000;
  145. break;
  146. default:
  147. eprintf ( "Unsupported relocation size %zd\n", size );
  148. exit ( 1 );
  149. }
  150. /* Locate or create PE relocation table */
  151. for ( pe_rel = *pe_reltab ; pe_rel ; pe_rel = pe_rel->next ) {
  152. if ( pe_rel->start_rva == start_rva )
  153. break;
  154. }
  155. if ( ! pe_rel ) {
  156. pe_rel = xmalloc ( sizeof ( *pe_rel ) );
  157. memset ( pe_rel, 0, sizeof ( *pe_rel ) );
  158. pe_rel->next = *pe_reltab;
  159. *pe_reltab = pe_rel;
  160. pe_rel->start_rva = start_rva;
  161. }
  162. /* Expand relocation list if necessary */
  163. if ( pe_rel->used_relocs < pe_rel->total_relocs ) {
  164. relocs = pe_rel->relocs;
  165. } else {
  166. pe_rel->total_relocs = ( pe_rel->total_relocs ?
  167. ( pe_rel->total_relocs * 2 ) : 256 );
  168. relocs = xmalloc ( pe_rel->total_relocs *
  169. sizeof ( pe_rel->relocs[0] ) );
  170. memset ( relocs, 0,
  171. pe_rel->total_relocs * sizeof ( pe_rel->relocs[0] ) );
  172. memcpy ( relocs, pe_rel->relocs,
  173. pe_rel->used_relocs * sizeof ( pe_rel->relocs[0] ) );
  174. free ( pe_rel->relocs );
  175. pe_rel->relocs = relocs;
  176. }
  177. /* Store relocation */
  178. pe_rel->relocs[ pe_rel->used_relocs++ ] = reloc;
  179. }
  180. /**
  181. * Calculate size of binary PE relocation table
  182. *
  183. * @v pe_reltab PE relocation table
  184. * @v buffer Buffer to contain binary table, or NULL
  185. * @ret size Size of binary table
  186. */
  187. static size_t output_pe_reltab ( struct pe_relocs *pe_reltab,
  188. void *buffer ) {
  189. struct pe_relocs *pe_rel;
  190. unsigned int num_relocs;
  191. size_t size;
  192. size_t total_size = 0;
  193. for ( pe_rel = pe_reltab ; pe_rel ; pe_rel = pe_rel->next ) {
  194. num_relocs = ( ( pe_rel->used_relocs + 1 ) & ~1 );
  195. size = ( sizeof ( uint32_t ) /* VirtualAddress */ +
  196. sizeof ( uint32_t ) /* SizeOfBlock */ +
  197. ( num_relocs * sizeof ( uint16_t ) ) );
  198. if ( buffer ) {
  199. *( (uint32_t *) ( buffer + total_size + 0 ) )
  200. = pe_rel->start_rva;
  201. *( (uint32_t *) ( buffer + total_size + 4 ) ) = size;
  202. memcpy ( ( buffer + total_size + 8 ), pe_rel->relocs,
  203. ( num_relocs * sizeof ( uint16_t ) ) );
  204. }
  205. total_size += size;
  206. }
  207. return total_size;
  208. }
  209. /**
  210. * Open input BFD file
  211. *
  212. * @v filename File name
  213. * @ret ibfd BFD file
  214. */
  215. static bfd * open_input_bfd ( const char *filename ) {
  216. bfd *bfd;
  217. /* Open the file */
  218. bfd = bfd_openr ( filename, NULL );
  219. if ( ! bfd ) {
  220. eprintf ( "Cannot open %s: ", filename );
  221. bfd_perror ( NULL );
  222. exit ( 1 );
  223. }
  224. /* The call to bfd_check_format() must be present, otherwise
  225. * we get a segfault from later BFD calls.
  226. */
  227. if ( bfd_check_format ( bfd, bfd_object ) < 0 ) {
  228. eprintf ( "%s is not an object file\n", filename );
  229. exit ( 1 );
  230. }
  231. return bfd;
  232. }
  233. /**
  234. * Read symbol table
  235. *
  236. * @v bfd BFD file
  237. */
  238. static asymbol ** read_symtab ( bfd *bfd ) {
  239. long symtab_size;
  240. asymbol **symtab;
  241. long symcount;
  242. /* Get symbol table size */
  243. symtab_size = bfd_get_symtab_upper_bound ( bfd );
  244. if ( symtab_size < 0 ) {
  245. bfd_perror ( "Could not get symbol table upper bound" );
  246. exit ( 1 );
  247. }
  248. /* Allocate and read symbol table */
  249. symtab = xmalloc ( symtab_size );
  250. symcount = bfd_canonicalize_symtab ( bfd, symtab );
  251. if ( symcount < 0 ) {
  252. bfd_perror ( "Cannot read symbol table" );
  253. exit ( 1 );
  254. }
  255. return symtab;
  256. }
  257. /**
  258. * Read relocation table
  259. *
  260. * @v bfd BFD file
  261. * @v symtab Symbol table
  262. * @v section Section
  263. * @v symtab Symbol table
  264. * @ret reltab Relocation table
  265. */
  266. static arelent ** read_reltab ( bfd *bfd, asymbol **symtab,
  267. asection *section ) {
  268. long reltab_size;
  269. arelent **reltab;
  270. long numrels;
  271. /* Get relocation table size */
  272. reltab_size = bfd_get_reloc_upper_bound ( bfd, section );
  273. if ( reltab_size < 0 ) {
  274. bfd_perror ( "Could not get relocation table upper bound" );
  275. exit ( 1 );
  276. }
  277. /* Allocate and read relocation table */
  278. reltab = xmalloc ( reltab_size );
  279. numrels = bfd_canonicalize_reloc ( bfd, section, reltab, symtab );
  280. if ( numrels < 0 ) {
  281. bfd_perror ( "Cannot read relocation table" );
  282. exit ( 1 );
  283. }
  284. return reltab;
  285. }
  286. /**
  287. * Process section
  288. *
  289. * @v bfd BFD file
  290. * @v pe_header PE file header
  291. * @v section Section
  292. * @ret new New PE section
  293. */
  294. static struct pe_section * process_section ( bfd *bfd,
  295. struct pe_header *pe_header,
  296. asection *section ) {
  297. struct pe_section *new;
  298. size_t section_memsz;
  299. size_t section_filesz;
  300. unsigned long flags = bfd_get_section_flags ( bfd, section );
  301. unsigned long code_start;
  302. unsigned long code_end;
  303. unsigned long data_start;
  304. unsigned long data_mid;
  305. unsigned long data_end;
  306. unsigned long start;
  307. unsigned long end;
  308. unsigned long *applicable_start;
  309. unsigned long *applicable_end;
  310. /* Extract current RVA limits from file header */
  311. code_start = pe_header->nt.OptionalHeader.BaseOfCode;
  312. code_end = ( code_start + pe_header->nt.OptionalHeader.SizeOfCode );
  313. #if defined(MDE_CPU_IA32)
  314. data_start = pe_header->nt.OptionalHeader.BaseOfData;
  315. #elif defined(MDE_CPU_X64)
  316. data_start = code_end;
  317. #endif
  318. data_mid = ( data_start +
  319. pe_header->nt.OptionalHeader.SizeOfInitializedData );
  320. data_end = ( data_mid +
  321. pe_header->nt.OptionalHeader.SizeOfUninitializedData );
  322. /* Allocate PE section */
  323. section_memsz = bfd_section_size ( bfd, section );
  324. section_filesz = ( ( flags & SEC_LOAD ) ?
  325. efi_file_align ( section_memsz ) : 0 );
  326. new = xmalloc ( sizeof ( *new ) + section_filesz );
  327. memset ( new, 0, sizeof ( *new ) + section_filesz );
  328. /* Fill in section header details */
  329. strncpy ( ( char * ) new->hdr.Name, section->name,
  330. sizeof ( new->hdr.Name ) );
  331. new->hdr.Misc.VirtualSize = section_memsz;
  332. new->hdr.VirtualAddress = bfd_get_section_vma ( bfd, section );
  333. new->hdr.SizeOfRawData = section_filesz;
  334. /* Fill in section characteristics and update RVA limits */
  335. if ( flags & SEC_CODE ) {
  336. /* .text-type section */
  337. new->hdr.Characteristics =
  338. ( EFI_IMAGE_SCN_CNT_CODE |
  339. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  340. EFI_IMAGE_SCN_MEM_EXECUTE |
  341. EFI_IMAGE_SCN_MEM_READ );
  342. applicable_start = &code_start;
  343. applicable_end = &code_end;
  344. } else if ( flags & SEC_DATA ) {
  345. /* .data-type section */
  346. new->hdr.Characteristics =
  347. ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  348. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  349. EFI_IMAGE_SCN_MEM_READ |
  350. EFI_IMAGE_SCN_MEM_WRITE );
  351. applicable_start = &data_start;
  352. applicable_end = &data_mid;
  353. } else if ( flags & SEC_READONLY ) {
  354. /* .rodata-type section */
  355. new->hdr.Characteristics =
  356. ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  357. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  358. EFI_IMAGE_SCN_MEM_READ );
  359. applicable_start = &data_start;
  360. applicable_end = &data_mid;
  361. } else if ( ! ( flags & SEC_LOAD ) ) {
  362. /* .bss-type section */
  363. new->hdr.Characteristics =
  364. ( EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA |
  365. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  366. EFI_IMAGE_SCN_MEM_READ |
  367. EFI_IMAGE_SCN_MEM_WRITE );
  368. applicable_start = &data_mid;
  369. applicable_end = &data_end;
  370. }
  371. /* Copy in section contents */
  372. if ( flags & SEC_LOAD ) {
  373. if ( ! bfd_get_section_contents ( bfd, section, new->contents,
  374. 0, section_memsz ) ) {
  375. eprintf ( "Cannot read section %s: ", section->name );
  376. bfd_perror ( NULL );
  377. exit ( 1 );
  378. }
  379. }
  380. /* Update RVA limits */
  381. start = new->hdr.VirtualAddress;
  382. end = ( start + new->hdr.Misc.VirtualSize );
  383. if ( ( ! *applicable_start ) || ( *applicable_start >= start ) )
  384. *applicable_start = start;
  385. if ( *applicable_end < end )
  386. *applicable_end = end;
  387. if ( data_start < code_end )
  388. data_start = code_end;
  389. if ( data_mid < data_start )
  390. data_mid = data_start;
  391. if ( data_end < data_mid )
  392. data_end = data_mid;
  393. /* Write RVA limits back to file header */
  394. pe_header->nt.OptionalHeader.BaseOfCode = code_start;
  395. pe_header->nt.OptionalHeader.SizeOfCode = ( code_end - code_start );
  396. #if defined(MDE_CPU_IA32)
  397. pe_header->nt.OptionalHeader.BaseOfData = data_start;
  398. #endif
  399. pe_header->nt.OptionalHeader.SizeOfInitializedData =
  400. ( data_mid - data_start );
  401. pe_header->nt.OptionalHeader.SizeOfUninitializedData =
  402. ( data_end - data_mid );
  403. /* Update remaining file header fields */
  404. pe_header->nt.FileHeader.NumberOfSections++;
  405. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( new->hdr );
  406. pe_header->nt.OptionalHeader.SizeOfImage =
  407. efi_file_align ( data_end );
  408. return new;
  409. }
  410. /**
  411. * Process relocation record
  412. *
  413. * @v bfd BFD file
  414. * @v section Section
  415. * @v rel Relocation entry
  416. * @v pe_reltab PE relocation table to fill in
  417. */
  418. static void process_reloc ( bfd *bfd, asection *section, arelent *rel,
  419. struct pe_relocs **pe_reltab ) {
  420. reloc_howto_type *howto = rel->howto;
  421. asymbol *sym = *(rel->sym_ptr_ptr);
  422. unsigned long offset = ( bfd_get_section_vma ( bfd, section ) +
  423. rel->address );
  424. if ( bfd_is_abs_section ( sym->section ) ) {
  425. /* Skip absolute symbols; the symbol value won't
  426. * change when the object is loaded.
  427. */
  428. } else if ( strcmp ( howto->name, "R_X86_64_64" ) == 0 ) {
  429. /* Generate an 8-byte PE relocation */
  430. generate_pe_reloc ( pe_reltab, offset, 8 );
  431. } else if ( ( strcmp ( howto->name, "R_386_32" ) == 0 ) ||
  432. ( strcmp ( howto->name, "R_X86_64_32" ) == 0 ) ) {
  433. /* Generate a 4-byte PE relocation */
  434. generate_pe_reloc ( pe_reltab, offset, 4 );
  435. } else if ( strcmp ( howto->name, "R_386_16" ) == 0 ) {
  436. /* Generate a 2-byte PE relocation */
  437. generate_pe_reloc ( pe_reltab, offset, 2 );
  438. } else if ( ( strcmp ( howto->name, "R_386_PC32" ) == 0 ) ||
  439. ( strcmp ( howto->name, "R_X86_64_PC32" ) == 0 ) ) {
  440. /* Skip PC-relative relocations; all relative offsets
  441. * remain unaltered when the object is loaded.
  442. */
  443. } else {
  444. eprintf ( "Unrecognised relocation type %s\n", howto->name );
  445. exit ( 1 );
  446. }
  447. }
  448. /**
  449. * Create relocations section
  450. *
  451. * @v pe_header PE file header
  452. * @v pe_reltab PE relocation table
  453. * @ret section Relocation section
  454. */
  455. static struct pe_section *
  456. create_reloc_section ( struct pe_header *pe_header,
  457. struct pe_relocs *pe_reltab ) {
  458. struct pe_section *reloc;
  459. size_t section_memsz;
  460. size_t section_filesz;
  461. EFI_IMAGE_DATA_DIRECTORY *relocdir;
  462. /* Allocate PE section */
  463. section_memsz = output_pe_reltab ( pe_reltab, NULL );
  464. section_filesz = efi_file_align ( section_memsz );
  465. reloc = xmalloc ( sizeof ( *reloc ) + section_filesz );
  466. memset ( reloc, 0, sizeof ( *reloc ) + section_filesz );
  467. /* Fill in section header details */
  468. strncpy ( ( char * ) reloc->hdr.Name, ".reloc",
  469. sizeof ( reloc->hdr.Name ) );
  470. reloc->hdr.Misc.VirtualSize = section_memsz;
  471. reloc->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  472. reloc->hdr.SizeOfRawData = section_filesz;
  473. reloc->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  474. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  475. EFI_IMAGE_SCN_MEM_READ );
  476. /* Copy in section contents */
  477. output_pe_reltab ( pe_reltab, reloc->contents );
  478. /* Update file header details */
  479. pe_header->nt.FileHeader.NumberOfSections++;
  480. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( reloc->hdr );
  481. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  482. relocdir = &(pe_header->nt.OptionalHeader.DataDirectory
  483. [EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  484. relocdir->VirtualAddress = reloc->hdr.VirtualAddress;
  485. relocdir->Size = reloc->hdr.Misc.VirtualSize;
  486. return reloc;
  487. }
  488. /**
  489. * Create debug section
  490. *
  491. * @v pe_header PE file header
  492. * @ret section Debug section
  493. */
  494. static struct pe_section *
  495. create_debug_section ( struct pe_header *pe_header, const char *filename ) {
  496. struct pe_section *debug;
  497. size_t section_memsz;
  498. size_t section_filesz;
  499. EFI_IMAGE_DATA_DIRECTORY *debugdir;
  500. struct {
  501. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY debug;
  502. EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY rsds;
  503. char name[ strlen ( filename ) + 1 ];
  504. } *contents;
  505. /* Allocate PE section */
  506. section_memsz = sizeof ( *contents );
  507. section_filesz = efi_file_align ( section_memsz );
  508. debug = xmalloc ( sizeof ( *debug ) + section_filesz );
  509. memset ( debug, 0, sizeof ( *debug ) + section_filesz );
  510. contents = ( void * ) debug->contents;
  511. /* Fill in section header details */
  512. strncpy ( ( char * ) debug->hdr.Name, ".debug",
  513. sizeof ( debug->hdr.Name ) );
  514. debug->hdr.Misc.VirtualSize = section_memsz;
  515. debug->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  516. debug->hdr.SizeOfRawData = section_filesz;
  517. debug->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  518. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  519. EFI_IMAGE_SCN_MEM_READ );
  520. /* Create section contents */
  521. contents->debug.TimeDateStamp = 0x10d1a884;
  522. contents->debug.Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
  523. contents->debug.SizeOfData =
  524. ( sizeof ( *contents ) - sizeof ( contents->debug ) );
  525. contents->debug.RVA = ( debug->hdr.VirtualAddress +
  526. offsetof ( typeof ( *contents ), rsds ) );
  527. contents->rsds.Signature = CODEVIEW_SIGNATURE_RSDS;
  528. snprintf ( contents->name, sizeof ( contents->name ), "%s",
  529. filename );
  530. /* Update file header details */
  531. pe_header->nt.FileHeader.NumberOfSections++;
  532. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( debug->hdr );
  533. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  534. debugdir = &(pe_header->nt.OptionalHeader.DataDirectory
  535. [EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
  536. debugdir->VirtualAddress = debug->hdr.VirtualAddress;
  537. debugdir->Size = debug->hdr.Misc.VirtualSize;
  538. return debug;
  539. }
  540. /**
  541. * Write out PE file
  542. *
  543. * @v pe_header PE file header
  544. * @v pe_sections List of PE sections
  545. * @v pe Output file
  546. */
  547. static void write_pe_file ( struct pe_header *pe_header,
  548. struct pe_section *pe_sections,
  549. FILE *pe ) {
  550. struct pe_section *section;
  551. unsigned long fpos = 0;
  552. /* Assign raw data pointers */
  553. fpos = efi_file_align ( pe_header->nt.OptionalHeader.SizeOfHeaders );
  554. for ( section = pe_sections ; section ; section = section->next ) {
  555. if ( section->hdr.SizeOfRawData ) {
  556. section->hdr.PointerToRawData = fpos;
  557. fpos += section->hdr.SizeOfRawData;
  558. fpos = efi_file_align ( fpos );
  559. }
  560. }
  561. /* Write file header */
  562. if ( fwrite ( pe_header, sizeof ( *pe_header ), 1, pe ) != 1 ) {
  563. perror ( "Could not write PE header" );
  564. exit ( 1 );
  565. }
  566. /* Write section headers */
  567. for ( section = pe_sections ; section ; section = section->next ) {
  568. if ( fwrite ( &section->hdr, sizeof ( section->hdr ),
  569. 1, pe ) != 1 ) {
  570. perror ( "Could not write section header" );
  571. exit ( 1 );
  572. }
  573. }
  574. /* Write sections */
  575. for ( section = pe_sections ; section ; section = section->next ) {
  576. if ( fseek ( pe, section->hdr.PointerToRawData,
  577. SEEK_SET ) != 0 ) {
  578. eprintf ( "Could not seek to %lx: %s\n",
  579. section->hdr.PointerToRawData,
  580. strerror ( errno ) );
  581. exit ( 1 );
  582. }
  583. if ( section->hdr.SizeOfRawData &&
  584. ( fwrite ( section->contents, section->hdr.SizeOfRawData,
  585. 1, pe ) != 1 ) ) {
  586. eprintf ( "Could not write section %.8s: %s\n",
  587. section->hdr.Name, strerror ( errno ) );
  588. exit ( 1 );
  589. }
  590. }
  591. }
  592. /**
  593. * Convert ELF to PE
  594. *
  595. * @v elf_name ELF file name
  596. * @v pe_name PE file name
  597. */
  598. static void elf2pe ( const char *elf_name, const char *pe_name,
  599. struct options *opts ) {
  600. char pe_name_tmp[ strlen ( pe_name ) + 1 ];
  601. bfd *bfd;
  602. asymbol **symtab;
  603. asection *section;
  604. arelent **reltab;
  605. arelent **rel;
  606. struct pe_relocs *pe_reltab = NULL;
  607. struct pe_section *pe_sections = NULL;
  608. struct pe_section **next_pe_section = &pe_sections;
  609. struct pe_header pe_header;
  610. FILE *pe;
  611. /* Create a modifiable copy of the PE name */
  612. memcpy ( pe_name_tmp, pe_name, sizeof ( pe_name_tmp ) );
  613. /* Open the file */
  614. bfd = open_input_bfd ( elf_name );
  615. symtab = read_symtab ( bfd );
  616. /* Initialise the PE header */
  617. memcpy ( &pe_header, &efi_pe_header, sizeof ( pe_header ) );
  618. pe_header.nt.OptionalHeader.AddressOfEntryPoint =
  619. bfd_get_start_address ( bfd );
  620. pe_header.nt.OptionalHeader.Subsystem = opts->subsystem;
  621. /* For each input section, build an output section and create
  622. * the appropriate relocation records
  623. */
  624. for ( section = bfd->sections ; section ; section = section->next ) {
  625. /* Discard non-allocatable sections */
  626. if ( ! ( bfd_get_section_flags ( bfd, section ) & SEC_ALLOC ) )
  627. continue;
  628. /* Create output section */
  629. *(next_pe_section) = process_section ( bfd, &pe_header,
  630. section );
  631. next_pe_section = &(*next_pe_section)->next;
  632. /* Add relocations from this section */
  633. reltab = read_reltab ( bfd, symtab, section );
  634. for ( rel = reltab ; *rel ; rel++ )
  635. process_reloc ( bfd, section, *rel, &pe_reltab );
  636. free ( reltab );
  637. }
  638. /* Create the .reloc section */
  639. *(next_pe_section) = create_reloc_section ( &pe_header, pe_reltab );
  640. next_pe_section = &(*next_pe_section)->next;
  641. /* Create the .reloc section */
  642. *(next_pe_section) = create_debug_section ( &pe_header,
  643. basename ( pe_name_tmp ) );
  644. next_pe_section = &(*next_pe_section)->next;
  645. /* Write out PE file */
  646. pe = fopen ( pe_name, "w" );
  647. if ( ! pe ) {
  648. eprintf ( "Could not open %s for writing: %s\n",
  649. pe_name, strerror ( errno ) );
  650. exit ( 1 );
  651. }
  652. write_pe_file ( &pe_header, pe_sections, pe );
  653. fclose ( pe );
  654. /* Close BFD file */
  655. bfd_close ( bfd );
  656. }
  657. /**
  658. * Print help
  659. *
  660. * @v program_name Program name
  661. */
  662. static void print_help ( const char *program_name ) {
  663. eprintf ( "Syntax: %s [--subsystem=<number>] infile outfile\n",
  664. program_name );
  665. }
  666. /**
  667. * Parse command-line options
  668. *
  669. * @v argc Argument count
  670. * @v argv Argument list
  671. * @v opts Options structure to populate
  672. */
  673. static int parse_options ( const int argc, char **argv,
  674. struct options *opts ) {
  675. char *end;
  676. int c;
  677. while (1) {
  678. int option_index = 0;
  679. static struct option long_options[] = {
  680. { "subsystem", required_argument, NULL, 's' },
  681. { "help", 0, NULL, 'h' },
  682. { 0, 0, 0, 0 }
  683. };
  684. if ( ( c = getopt_long ( argc, argv, "s:h",
  685. long_options,
  686. &option_index ) ) == -1 ) {
  687. break;
  688. }
  689. switch ( c ) {
  690. case 's':
  691. opts->subsystem = strtoul ( optarg, &end, 0 );
  692. if ( *end ) {
  693. eprintf ( "Invalid subsytem \"%s\"\n",
  694. optarg );
  695. exit ( 2 );
  696. }
  697. break;
  698. case 'h':
  699. print_help ( argv[0] );
  700. exit ( 0 );
  701. case '?':
  702. default:
  703. exit ( 2 );
  704. }
  705. }
  706. return optind;
  707. }
  708. int main ( int argc, char **argv ) {
  709. struct options opts = {
  710. .subsystem = EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,
  711. };
  712. unsigned int infile_index;
  713. const char *infile;
  714. const char *outfile;
  715. /* Initialise libbfd */
  716. bfd_init();
  717. /* Parse command-line arguments */
  718. infile_index = parse_options ( argc, argv, &opts );
  719. if ( argc != ( infile_index + 2 ) ) {
  720. print_help ( argv[0] );
  721. exit ( 2 );
  722. }
  723. infile = argv[infile_index];
  724. outfile = argv[infile_index + 1];
  725. /* Convert file */
  726. elf2pe ( infile, outfile, &opts );
  727. return 0;
  728. }