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.

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