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.

elf2efi.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 <ipxe/efi/efi.h>
  30. #include <ipxe/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 ) ) {
  228. eprintf ( "%s is not an object file: ", filename );
  229. bfd_perror ( NULL );
  230. exit ( 1 );
  231. }
  232. return bfd;
  233. }
  234. /**
  235. * Read symbol table
  236. *
  237. * @v bfd BFD file
  238. */
  239. static asymbol ** read_symtab ( bfd *bfd ) {
  240. long symtab_size;
  241. asymbol **symtab;
  242. long symcount;
  243. /* Get symbol table size */
  244. symtab_size = bfd_get_symtab_upper_bound ( bfd );
  245. if ( symtab_size < 0 ) {
  246. bfd_perror ( "Could not get symbol table upper bound" );
  247. exit ( 1 );
  248. }
  249. /* Allocate and read symbol table */
  250. symtab = xmalloc ( symtab_size );
  251. symcount = bfd_canonicalize_symtab ( bfd, symtab );
  252. if ( symcount < 0 ) {
  253. bfd_perror ( "Cannot read symbol table" );
  254. exit ( 1 );
  255. }
  256. return symtab;
  257. }
  258. /**
  259. * Read relocation table
  260. *
  261. * @v bfd BFD file
  262. * @v symtab Symbol table
  263. * @v section Section
  264. * @v symtab Symbol table
  265. * @ret reltab Relocation table
  266. */
  267. static arelent ** read_reltab ( bfd *bfd, asymbol **symtab,
  268. asection *section ) {
  269. long reltab_size;
  270. arelent **reltab;
  271. long numrels;
  272. /* Get relocation table size */
  273. reltab_size = bfd_get_reloc_upper_bound ( bfd, section );
  274. if ( reltab_size < 0 ) {
  275. bfd_perror ( "Could not get relocation table upper bound" );
  276. exit ( 1 );
  277. }
  278. /* Allocate and read relocation table */
  279. reltab = xmalloc ( reltab_size );
  280. numrels = bfd_canonicalize_reloc ( bfd, section, reltab, symtab );
  281. if ( numrels < 0 ) {
  282. bfd_perror ( "Cannot read relocation table" );
  283. exit ( 1 );
  284. }
  285. return reltab;
  286. }
  287. /**
  288. * Process section
  289. *
  290. * @v bfd BFD file
  291. * @v pe_header PE file header
  292. * @v section Section
  293. * @ret new New PE section
  294. */
  295. static struct pe_section * process_section ( bfd *bfd,
  296. struct pe_header *pe_header,
  297. asection *section ) {
  298. struct pe_section *new;
  299. size_t section_memsz;
  300. size_t section_filesz;
  301. unsigned long flags = bfd_get_section_flags ( bfd, section );
  302. unsigned long code_start;
  303. unsigned long code_end;
  304. unsigned long data_start;
  305. unsigned long data_mid;
  306. unsigned long data_end;
  307. unsigned long start;
  308. unsigned long end;
  309. unsigned long *applicable_start;
  310. unsigned long *applicable_end;
  311. /* Extract current RVA limits from file header */
  312. code_start = pe_header->nt.OptionalHeader.BaseOfCode;
  313. code_end = ( code_start + pe_header->nt.OptionalHeader.SizeOfCode );
  314. #if defined(MDE_CPU_IA32)
  315. data_start = pe_header->nt.OptionalHeader.BaseOfData;
  316. #elif defined(MDE_CPU_X64)
  317. data_start = code_end;
  318. #endif
  319. data_mid = ( data_start +
  320. pe_header->nt.OptionalHeader.SizeOfInitializedData );
  321. data_end = ( data_mid +
  322. pe_header->nt.OptionalHeader.SizeOfUninitializedData );
  323. /* Allocate PE section */
  324. section_memsz = bfd_section_size ( bfd, section );
  325. section_filesz = ( ( flags & SEC_LOAD ) ?
  326. efi_file_align ( section_memsz ) : 0 );
  327. new = xmalloc ( sizeof ( *new ) + section_filesz );
  328. memset ( new, 0, sizeof ( *new ) + section_filesz );
  329. /* Fill in section header details */
  330. strncpy ( ( char * ) new->hdr.Name, section->name,
  331. sizeof ( new->hdr.Name ) );
  332. new->hdr.Misc.VirtualSize = section_memsz;
  333. new->hdr.VirtualAddress = bfd_get_section_vma ( bfd, section );
  334. new->hdr.SizeOfRawData = section_filesz;
  335. /* Fill in section characteristics and update RVA limits */
  336. if ( flags & SEC_CODE ) {
  337. /* .text-type section */
  338. new->hdr.Characteristics =
  339. ( EFI_IMAGE_SCN_CNT_CODE |
  340. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  341. EFI_IMAGE_SCN_MEM_EXECUTE |
  342. EFI_IMAGE_SCN_MEM_READ );
  343. applicable_start = &code_start;
  344. applicable_end = &code_end;
  345. } else if ( flags & SEC_DATA ) {
  346. /* .data-type section */
  347. new->hdr.Characteristics =
  348. ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  349. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  350. EFI_IMAGE_SCN_MEM_READ |
  351. EFI_IMAGE_SCN_MEM_WRITE );
  352. applicable_start = &data_start;
  353. applicable_end = &data_mid;
  354. } else if ( flags & SEC_READONLY ) {
  355. /* .rodata-type section */
  356. new->hdr.Characteristics =
  357. ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  358. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  359. EFI_IMAGE_SCN_MEM_READ );
  360. applicable_start = &data_start;
  361. applicable_end = &data_mid;
  362. } else if ( ! ( flags & SEC_LOAD ) ) {
  363. /* .bss-type section */
  364. new->hdr.Characteristics =
  365. ( EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA |
  366. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  367. EFI_IMAGE_SCN_MEM_READ |
  368. EFI_IMAGE_SCN_MEM_WRITE );
  369. applicable_start = &data_mid;
  370. applicable_end = &data_end;
  371. }
  372. /* Copy in section contents */
  373. if ( flags & SEC_LOAD ) {
  374. if ( ! bfd_get_section_contents ( bfd, section, new->contents,
  375. 0, section_memsz ) ) {
  376. eprintf ( "Cannot read section %s: ", section->name );
  377. bfd_perror ( NULL );
  378. exit ( 1 );
  379. }
  380. }
  381. /* Update RVA limits */
  382. start = new->hdr.VirtualAddress;
  383. end = ( start + new->hdr.Misc.VirtualSize );
  384. if ( ( ! *applicable_start ) || ( *applicable_start >= start ) )
  385. *applicable_start = start;
  386. if ( *applicable_end < end )
  387. *applicable_end = end;
  388. if ( data_start < code_end )
  389. data_start = code_end;
  390. if ( data_mid < data_start )
  391. data_mid = data_start;
  392. if ( data_end < data_mid )
  393. data_end = data_mid;
  394. /* Write RVA limits back to file header */
  395. pe_header->nt.OptionalHeader.BaseOfCode = code_start;
  396. pe_header->nt.OptionalHeader.SizeOfCode = ( code_end - code_start );
  397. #if defined(MDE_CPU_IA32)
  398. pe_header->nt.OptionalHeader.BaseOfData = data_start;
  399. #endif
  400. pe_header->nt.OptionalHeader.SizeOfInitializedData =
  401. ( data_mid - data_start );
  402. pe_header->nt.OptionalHeader.SizeOfUninitializedData =
  403. ( data_end - data_mid );
  404. /* Update remaining file header fields */
  405. pe_header->nt.FileHeader.NumberOfSections++;
  406. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( new->hdr );
  407. pe_header->nt.OptionalHeader.SizeOfImage =
  408. efi_file_align ( data_end );
  409. return new;
  410. }
  411. /**
  412. * Process relocation record
  413. *
  414. * @v bfd BFD file
  415. * @v section Section
  416. * @v rel Relocation entry
  417. * @v pe_reltab PE relocation table to fill in
  418. */
  419. static void process_reloc ( bfd *bfd, asection *section, arelent *rel,
  420. struct pe_relocs **pe_reltab ) {
  421. reloc_howto_type *howto = rel->howto;
  422. asymbol *sym = *(rel->sym_ptr_ptr);
  423. unsigned long offset = ( bfd_get_section_vma ( bfd, section ) +
  424. rel->address );
  425. if ( bfd_is_abs_section ( sym->section ) ) {
  426. /* Skip absolute symbols; the symbol value won't
  427. * change when the object is loaded.
  428. */
  429. } else if ( strcmp ( howto->name, "R_X86_64_64" ) == 0 ) {
  430. /* Generate an 8-byte PE relocation */
  431. generate_pe_reloc ( pe_reltab, offset, 8 );
  432. } else if ( ( strcmp ( howto->name, "R_386_32" ) == 0 ) ||
  433. ( strcmp ( howto->name, "R_X86_64_32" ) == 0 ) ) {
  434. /* Generate a 4-byte PE relocation */
  435. generate_pe_reloc ( pe_reltab, offset, 4 );
  436. } else if ( strcmp ( howto->name, "R_386_16" ) == 0 ) {
  437. /* Generate a 2-byte PE relocation */
  438. generate_pe_reloc ( pe_reltab, offset, 2 );
  439. } else if ( ( strcmp ( howto->name, "R_386_PC32" ) == 0 ) ||
  440. ( strcmp ( howto->name, "R_X86_64_PC32" ) == 0 ) ) {
  441. /* Skip PC-relative relocations; all relative offsets
  442. * remain unaltered when the object is loaded.
  443. */
  444. } else {
  445. eprintf ( "Unrecognised relocation type %s\n", howto->name );
  446. exit ( 1 );
  447. }
  448. }
  449. /**
  450. * Create relocations section
  451. *
  452. * @v pe_header PE file header
  453. * @v pe_reltab PE relocation table
  454. * @ret section Relocation section
  455. */
  456. static struct pe_section *
  457. create_reloc_section ( struct pe_header *pe_header,
  458. struct pe_relocs *pe_reltab ) {
  459. struct pe_section *reloc;
  460. size_t section_memsz;
  461. size_t section_filesz;
  462. EFI_IMAGE_DATA_DIRECTORY *relocdir;
  463. /* Allocate PE section */
  464. section_memsz = output_pe_reltab ( pe_reltab, NULL );
  465. section_filesz = efi_file_align ( section_memsz );
  466. reloc = xmalloc ( sizeof ( *reloc ) + section_filesz );
  467. memset ( reloc, 0, sizeof ( *reloc ) + section_filesz );
  468. /* Fill in section header details */
  469. strncpy ( ( char * ) reloc->hdr.Name, ".reloc",
  470. sizeof ( reloc->hdr.Name ) );
  471. reloc->hdr.Misc.VirtualSize = section_memsz;
  472. reloc->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  473. reloc->hdr.SizeOfRawData = section_filesz;
  474. reloc->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  475. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  476. EFI_IMAGE_SCN_MEM_READ );
  477. /* Copy in section contents */
  478. output_pe_reltab ( pe_reltab, reloc->contents );
  479. /* Update file header details */
  480. pe_header->nt.FileHeader.NumberOfSections++;
  481. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( reloc->hdr );
  482. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  483. relocdir = &(pe_header->nt.OptionalHeader.DataDirectory
  484. [EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  485. relocdir->VirtualAddress = reloc->hdr.VirtualAddress;
  486. relocdir->Size = reloc->hdr.Misc.VirtualSize;
  487. return reloc;
  488. }
  489. /**
  490. * Create debug section
  491. *
  492. * @v pe_header PE file header
  493. * @ret section Debug section
  494. */
  495. static struct pe_section *
  496. create_debug_section ( struct pe_header *pe_header, const char *filename ) {
  497. struct pe_section *debug;
  498. size_t section_memsz;
  499. size_t section_filesz;
  500. EFI_IMAGE_DATA_DIRECTORY *debugdir;
  501. struct {
  502. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY debug;
  503. EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY rsds;
  504. char name[ strlen ( filename ) + 1 ];
  505. } *contents;
  506. /* Allocate PE section */
  507. section_memsz = sizeof ( *contents );
  508. section_filesz = efi_file_align ( section_memsz );
  509. debug = xmalloc ( sizeof ( *debug ) + section_filesz );
  510. memset ( debug, 0, sizeof ( *debug ) + section_filesz );
  511. contents = ( void * ) debug->contents;
  512. /* Fill in section header details */
  513. strncpy ( ( char * ) debug->hdr.Name, ".debug",
  514. sizeof ( debug->hdr.Name ) );
  515. debug->hdr.Misc.VirtualSize = section_memsz;
  516. debug->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  517. debug->hdr.SizeOfRawData = section_filesz;
  518. debug->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  519. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  520. EFI_IMAGE_SCN_MEM_READ );
  521. /* Create section contents */
  522. contents->debug.TimeDateStamp = 0x10d1a884;
  523. contents->debug.Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
  524. contents->debug.SizeOfData =
  525. ( sizeof ( *contents ) - sizeof ( contents->debug ) );
  526. contents->debug.RVA = ( debug->hdr.VirtualAddress +
  527. offsetof ( typeof ( *contents ), rsds ) );
  528. contents->rsds.Signature = CODEVIEW_SIGNATURE_RSDS;
  529. snprintf ( contents->name, sizeof ( contents->name ), "%s",
  530. filename );
  531. /* Update file header details */
  532. pe_header->nt.FileHeader.NumberOfSections++;
  533. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( debug->hdr );
  534. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  535. debugdir = &(pe_header->nt.OptionalHeader.DataDirectory
  536. [EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
  537. debugdir->VirtualAddress = debug->hdr.VirtualAddress;
  538. debugdir->Size = debug->hdr.Misc.VirtualSize;
  539. return debug;
  540. }
  541. /**
  542. * Write out PE file
  543. *
  544. * @v pe_header PE file header
  545. * @v pe_sections List of PE sections
  546. * @v pe Output file
  547. */
  548. static void write_pe_file ( struct pe_header *pe_header,
  549. struct pe_section *pe_sections,
  550. FILE *pe ) {
  551. struct pe_section *section;
  552. unsigned long fpos = 0;
  553. /* Assign raw data pointers */
  554. fpos = efi_file_align ( pe_header->nt.OptionalHeader.SizeOfHeaders );
  555. for ( section = pe_sections ; section ; section = section->next ) {
  556. if ( section->hdr.SizeOfRawData ) {
  557. section->hdr.PointerToRawData = fpos;
  558. fpos += section->hdr.SizeOfRawData;
  559. fpos = efi_file_align ( fpos );
  560. }
  561. }
  562. /* Write file header */
  563. if ( fwrite ( pe_header, sizeof ( *pe_header ), 1, pe ) != 1 ) {
  564. perror ( "Could not write PE header" );
  565. exit ( 1 );
  566. }
  567. /* Write section headers */
  568. for ( section = pe_sections ; section ; section = section->next ) {
  569. if ( fwrite ( &section->hdr, sizeof ( section->hdr ),
  570. 1, pe ) != 1 ) {
  571. perror ( "Could not write section header" );
  572. exit ( 1 );
  573. }
  574. }
  575. /* Write sections */
  576. for ( section = pe_sections ; section ; section = section->next ) {
  577. if ( fseek ( pe, section->hdr.PointerToRawData,
  578. SEEK_SET ) != 0 ) {
  579. eprintf ( "Could not seek to %lx: %s\n",
  580. section->hdr.PointerToRawData,
  581. strerror ( errno ) );
  582. exit ( 1 );
  583. }
  584. if ( section->hdr.SizeOfRawData &&
  585. ( fwrite ( section->contents, section->hdr.SizeOfRawData,
  586. 1, pe ) != 1 ) ) {
  587. eprintf ( "Could not write section %.8s: %s\n",
  588. section->hdr.Name, strerror ( errno ) );
  589. exit ( 1 );
  590. }
  591. }
  592. }
  593. /**
  594. * Convert ELF to PE
  595. *
  596. * @v elf_name ELF file name
  597. * @v pe_name PE file name
  598. */
  599. static void elf2pe ( const char *elf_name, const char *pe_name,
  600. struct options *opts ) {
  601. char pe_name_tmp[ strlen ( pe_name ) + 1 ];
  602. bfd *bfd;
  603. asymbol **symtab;
  604. asection *section;
  605. arelent **reltab;
  606. arelent **rel;
  607. struct pe_relocs *pe_reltab = NULL;
  608. struct pe_section *pe_sections = NULL;
  609. struct pe_section **next_pe_section = &pe_sections;
  610. struct pe_header pe_header;
  611. FILE *pe;
  612. /* Create a modifiable copy of the PE name */
  613. memcpy ( pe_name_tmp, pe_name, sizeof ( pe_name_tmp ) );
  614. /* Open the file */
  615. bfd = open_input_bfd ( elf_name );
  616. symtab = read_symtab ( bfd );
  617. /* Initialise the PE header */
  618. memcpy ( &pe_header, &efi_pe_header, sizeof ( pe_header ) );
  619. pe_header.nt.OptionalHeader.AddressOfEntryPoint =
  620. bfd_get_start_address ( bfd );
  621. pe_header.nt.OptionalHeader.Subsystem = opts->subsystem;
  622. /* For each input section, build an output section and create
  623. * the appropriate relocation records
  624. */
  625. for ( section = bfd->sections ; section ; section = section->next ) {
  626. /* Discard non-allocatable sections */
  627. if ( ! ( bfd_get_section_flags ( bfd, section ) & SEC_ALLOC ) )
  628. continue;
  629. /* Create output section */
  630. *(next_pe_section) = process_section ( bfd, &pe_header,
  631. section );
  632. next_pe_section = &(*next_pe_section)->next;
  633. /* Add relocations from this section */
  634. reltab = read_reltab ( bfd, symtab, section );
  635. for ( rel = reltab ; *rel ; rel++ )
  636. process_reloc ( bfd, section, *rel, &pe_reltab );
  637. free ( reltab );
  638. }
  639. /* Create the .reloc section */
  640. *(next_pe_section) = create_reloc_section ( &pe_header, pe_reltab );
  641. next_pe_section = &(*next_pe_section)->next;
  642. /* Create the .reloc section */
  643. *(next_pe_section) = create_debug_section ( &pe_header,
  644. basename ( pe_name_tmp ) );
  645. next_pe_section = &(*next_pe_section)->next;
  646. /* Write out PE file */
  647. pe = fopen ( pe_name, "w" );
  648. if ( ! pe ) {
  649. eprintf ( "Could not open %s for writing: %s\n",
  650. pe_name, strerror ( errno ) );
  651. exit ( 1 );
  652. }
  653. write_pe_file ( &pe_header, pe_sections, pe );
  654. fclose ( pe );
  655. /* Close BFD file */
  656. bfd_close ( bfd );
  657. }
  658. /**
  659. * Print help
  660. *
  661. * @v program_name Program name
  662. */
  663. static void print_help ( const char *program_name ) {
  664. eprintf ( "Syntax: %s [--subsystem=<number>] infile outfile\n",
  665. program_name );
  666. }
  667. /**
  668. * Parse command-line options
  669. *
  670. * @v argc Argument count
  671. * @v argv Argument list
  672. * @v opts Options structure to populate
  673. */
  674. static int parse_options ( const int argc, char **argv,
  675. struct options *opts ) {
  676. char *end;
  677. int c;
  678. while (1) {
  679. int option_index = 0;
  680. static struct option long_options[] = {
  681. { "subsystem", required_argument, NULL, 's' },
  682. { "help", 0, NULL, 'h' },
  683. { 0, 0, 0, 0 }
  684. };
  685. if ( ( c = getopt_long ( argc, argv, "s:h",
  686. long_options,
  687. &option_index ) ) == -1 ) {
  688. break;
  689. }
  690. switch ( c ) {
  691. case 's':
  692. opts->subsystem = strtoul ( optarg, &end, 0 );
  693. if ( *end ) {
  694. eprintf ( "Invalid subsytem \"%s\"\n",
  695. optarg );
  696. exit ( 2 );
  697. }
  698. break;
  699. case 'h':
  700. print_help ( argv[0] );
  701. exit ( 0 );
  702. case '?':
  703. default:
  704. exit ( 2 );
  705. }
  706. }
  707. return optind;
  708. }
  709. int main ( int argc, char **argv ) {
  710. struct options opts = {
  711. .subsystem = EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,
  712. };
  713. unsigned int infile_index;
  714. const char *infile;
  715. const char *outfile;
  716. /* Initialise libbfd */
  717. bfd_init();
  718. /* Parse command-line arguments */
  719. infile_index = parse_options ( argc, argv, &opts );
  720. if ( argc != ( infile_index + 2 ) ) {
  721. print_help ( argv[0] );
  722. exit ( 2 );
  723. }
  724. infile = argv[infile_index];
  725. outfile = argv[infile_index + 1];
  726. /* Convert file */
  727. elf2pe ( infile, outfile, &opts );
  728. return 0;
  729. }