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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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(EFI_TARGET_IA32)
  50. EFI_IMAGE_NT_HEADERS32 nt;
  51. #elif defined(EFI_TARGET_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(EFI_TARGET_IA32)
  64. .Machine = EFI_IMAGE_MACHINE_IA32,
  65. #elif defined(EFI_TARGET_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(EFI_TARGET_IA32)
  73. EFI_IMAGE_FILE_32BIT_MACHINE |
  74. #endif
  75. EFI_IMAGE_FILE_EXECUTABLE_IMAGE ),
  76. },
  77. .OptionalHeader = {
  78. #if defined(EFI_TARGET_IA32)
  79. .Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC,
  80. #elif defined(EFI_TARGET_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(EFI_TARGET_IA32)
  315. data_start = pe_header->nt.OptionalHeader.BaseOfData;
  316. #elif defined(EFI_TARGET_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. } else {
  372. eprintf ( "Unrecognised characteristics %#lx for section %s\n",
  373. flags, section->name );
  374. exit ( 1 );
  375. }
  376. /* Copy in section contents */
  377. if ( flags & SEC_LOAD ) {
  378. if ( ! bfd_get_section_contents ( bfd, section, new->contents,
  379. 0, section_memsz ) ) {
  380. eprintf ( "Cannot read section %s: ", section->name );
  381. bfd_perror ( NULL );
  382. exit ( 1 );
  383. }
  384. }
  385. /* Update RVA limits */
  386. start = new->hdr.VirtualAddress;
  387. end = ( start + new->hdr.Misc.VirtualSize );
  388. if ( ( ! *applicable_start ) || ( *applicable_start >= start ) )
  389. *applicable_start = start;
  390. if ( *applicable_end < end )
  391. *applicable_end = end;
  392. if ( data_start < code_end )
  393. data_start = code_end;
  394. if ( data_mid < data_start )
  395. data_mid = data_start;
  396. if ( data_end < data_mid )
  397. data_end = data_mid;
  398. /* Write RVA limits back to file header */
  399. pe_header->nt.OptionalHeader.BaseOfCode = code_start;
  400. pe_header->nt.OptionalHeader.SizeOfCode = ( code_end - code_start );
  401. #if defined(EFI_TARGET_IA32)
  402. pe_header->nt.OptionalHeader.BaseOfData = data_start;
  403. #endif
  404. pe_header->nt.OptionalHeader.SizeOfInitializedData =
  405. ( data_mid - data_start );
  406. pe_header->nt.OptionalHeader.SizeOfUninitializedData =
  407. ( data_end - data_mid );
  408. /* Update remaining file header fields */
  409. pe_header->nt.FileHeader.NumberOfSections++;
  410. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( new->hdr );
  411. pe_header->nt.OptionalHeader.SizeOfImage =
  412. efi_file_align ( data_end );
  413. return new;
  414. }
  415. /**
  416. * Process relocation record
  417. *
  418. * @v bfd BFD file
  419. * @v section Section
  420. * @v rel Relocation entry
  421. * @v pe_reltab PE relocation table to fill in
  422. */
  423. static void process_reloc ( bfd *bfd __attribute__ (( unused )),
  424. asection *section, arelent *rel,
  425. struct pe_relocs **pe_reltab ) {
  426. reloc_howto_type *howto = rel->howto;
  427. asymbol *sym = *(rel->sym_ptr_ptr);
  428. unsigned long offset = ( bfd_get_section_vma ( bfd, section ) +
  429. rel->address );
  430. if ( bfd_is_abs_section ( sym->section ) ) {
  431. /* Skip absolute symbols; the symbol value won't
  432. * change when the object is loaded.
  433. */
  434. } else if ( strcmp ( howto->name, "R_X86_64_64" ) == 0 ) {
  435. /* Generate an 8-byte PE relocation */
  436. generate_pe_reloc ( pe_reltab, offset, 8 );
  437. } else if ( ( strcmp ( howto->name, "R_386_32" ) == 0 ) ||
  438. ( strcmp ( howto->name, "R_X86_64_32" ) == 0 ) ) {
  439. /* Generate a 4-byte PE relocation */
  440. generate_pe_reloc ( pe_reltab, offset, 4 );
  441. } else if ( strcmp ( howto->name, "R_386_16" ) == 0 ) {
  442. /* Generate a 2-byte PE relocation */
  443. generate_pe_reloc ( pe_reltab, offset, 2 );
  444. } else if ( ( strcmp ( howto->name, "R_386_PC32" ) == 0 ) ||
  445. ( strcmp ( howto->name, "R_X86_64_PC32" ) == 0 ) ) {
  446. /* Skip PC-relative relocations; all relative offsets
  447. * remain unaltered when the object is loaded.
  448. */
  449. } else {
  450. eprintf ( "Unrecognised relocation type %s\n", howto->name );
  451. exit ( 1 );
  452. }
  453. }
  454. /**
  455. * Create relocations section
  456. *
  457. * @v pe_header PE file header
  458. * @v pe_reltab PE relocation table
  459. * @ret section Relocation section
  460. */
  461. static struct pe_section *
  462. create_reloc_section ( struct pe_header *pe_header,
  463. struct pe_relocs *pe_reltab ) {
  464. struct pe_section *reloc;
  465. size_t section_memsz;
  466. size_t section_filesz;
  467. EFI_IMAGE_DATA_DIRECTORY *relocdir;
  468. /* Allocate PE section */
  469. section_memsz = output_pe_reltab ( pe_reltab, NULL );
  470. section_filesz = efi_file_align ( section_memsz );
  471. reloc = xmalloc ( sizeof ( *reloc ) + section_filesz );
  472. memset ( reloc, 0, sizeof ( *reloc ) + section_filesz );
  473. /* Fill in section header details */
  474. strncpy ( ( char * ) reloc->hdr.Name, ".reloc",
  475. sizeof ( reloc->hdr.Name ) );
  476. reloc->hdr.Misc.VirtualSize = section_memsz;
  477. reloc->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  478. reloc->hdr.SizeOfRawData = section_filesz;
  479. reloc->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  480. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  481. EFI_IMAGE_SCN_MEM_READ );
  482. /* Copy in section contents */
  483. output_pe_reltab ( pe_reltab, reloc->contents );
  484. /* Update file header details */
  485. pe_header->nt.FileHeader.NumberOfSections++;
  486. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( reloc->hdr );
  487. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  488. relocdir = &(pe_header->nt.OptionalHeader.DataDirectory
  489. [EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  490. relocdir->VirtualAddress = reloc->hdr.VirtualAddress;
  491. relocdir->Size = reloc->hdr.Misc.VirtualSize;
  492. return reloc;
  493. }
  494. /**
  495. * Create debug section
  496. *
  497. * @v pe_header PE file header
  498. * @ret section Debug section
  499. */
  500. static struct pe_section *
  501. create_debug_section ( struct pe_header *pe_header, const char *filename ) {
  502. struct pe_section *debug;
  503. size_t section_memsz;
  504. size_t section_filesz;
  505. EFI_IMAGE_DATA_DIRECTORY *debugdir;
  506. struct {
  507. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY debug;
  508. EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY rsds;
  509. char name[ strlen ( filename ) + 1 ];
  510. } *contents;
  511. /* Allocate PE section */
  512. section_memsz = sizeof ( *contents );
  513. section_filesz = efi_file_align ( section_memsz );
  514. debug = xmalloc ( sizeof ( *debug ) + section_filesz );
  515. memset ( debug, 0, sizeof ( *debug ) + section_filesz );
  516. contents = ( void * ) debug->contents;
  517. /* Fill in section header details */
  518. strncpy ( ( char * ) debug->hdr.Name, ".debug",
  519. sizeof ( debug->hdr.Name ) );
  520. debug->hdr.Misc.VirtualSize = section_memsz;
  521. debug->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  522. debug->hdr.SizeOfRawData = section_filesz;
  523. debug->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  524. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  525. EFI_IMAGE_SCN_MEM_READ );
  526. /* Create section contents */
  527. contents->debug.TimeDateStamp = 0x10d1a884;
  528. contents->debug.Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
  529. contents->debug.SizeOfData =
  530. ( sizeof ( *contents ) - sizeof ( contents->debug ) );
  531. contents->debug.RVA = ( debug->hdr.VirtualAddress +
  532. offsetof ( typeof ( *contents ), rsds ) );
  533. contents->rsds.Signature = CODEVIEW_SIGNATURE_RSDS;
  534. snprintf ( contents->name, sizeof ( contents->name ), "%s",
  535. filename );
  536. /* Update file header details */
  537. pe_header->nt.FileHeader.NumberOfSections++;
  538. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( debug->hdr );
  539. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  540. debugdir = &(pe_header->nt.OptionalHeader.DataDirectory
  541. [EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
  542. debugdir->VirtualAddress = debug->hdr.VirtualAddress;
  543. debugdir->Size = debug->hdr.Misc.VirtualSize;
  544. return debug;
  545. }
  546. /**
  547. * Write out PE file
  548. *
  549. * @v pe_header PE file header
  550. * @v pe_sections List of PE sections
  551. * @v pe Output file
  552. */
  553. static void write_pe_file ( struct pe_header *pe_header,
  554. struct pe_section *pe_sections,
  555. FILE *pe ) {
  556. struct pe_section *section;
  557. unsigned long fpos = 0;
  558. /* Assign raw data pointers */
  559. fpos = efi_file_align ( pe_header->nt.OptionalHeader.SizeOfHeaders );
  560. for ( section = pe_sections ; section ; section = section->next ) {
  561. if ( section->hdr.SizeOfRawData ) {
  562. section->hdr.PointerToRawData = fpos;
  563. fpos += section->hdr.SizeOfRawData;
  564. fpos = efi_file_align ( fpos );
  565. }
  566. }
  567. /* Write file header */
  568. if ( fwrite ( pe_header, sizeof ( *pe_header ), 1, pe ) != 1 ) {
  569. perror ( "Could not write PE header" );
  570. exit ( 1 );
  571. }
  572. /* Write section headers */
  573. for ( section = pe_sections ; section ; section = section->next ) {
  574. if ( fwrite ( &section->hdr, sizeof ( section->hdr ),
  575. 1, pe ) != 1 ) {
  576. perror ( "Could not write section header" );
  577. exit ( 1 );
  578. }
  579. }
  580. /* Write sections */
  581. for ( section = pe_sections ; section ; section = section->next ) {
  582. if ( fseek ( pe, section->hdr.PointerToRawData,
  583. SEEK_SET ) != 0 ) {
  584. eprintf ( "Could not seek to %x: %s\n",
  585. section->hdr.PointerToRawData,
  586. strerror ( errno ) );
  587. exit ( 1 );
  588. }
  589. if ( section->hdr.SizeOfRawData &&
  590. ( fwrite ( section->contents, section->hdr.SizeOfRawData,
  591. 1, pe ) != 1 ) ) {
  592. eprintf ( "Could not write section %.8s: %s\n",
  593. section->hdr.Name, strerror ( errno ) );
  594. exit ( 1 );
  595. }
  596. }
  597. }
  598. /**
  599. * Convert ELF to PE
  600. *
  601. * @v elf_name ELF file name
  602. * @v pe_name PE file name
  603. */
  604. static void elf2pe ( const char *elf_name, const char *pe_name,
  605. struct options *opts ) {
  606. char pe_name_tmp[ strlen ( pe_name ) + 1 ];
  607. bfd *bfd;
  608. asymbol **symtab;
  609. asection *section;
  610. arelent **reltab;
  611. arelent **rel;
  612. struct pe_relocs *pe_reltab = NULL;
  613. struct pe_section *pe_sections = NULL;
  614. struct pe_section **next_pe_section = &pe_sections;
  615. struct pe_header pe_header;
  616. FILE *pe;
  617. /* Create a modifiable copy of the PE name */
  618. memcpy ( pe_name_tmp, pe_name, sizeof ( pe_name_tmp ) );
  619. /* Open the file */
  620. bfd = open_input_bfd ( elf_name );
  621. symtab = read_symtab ( bfd );
  622. /* Initialise the PE header */
  623. memcpy ( &pe_header, &efi_pe_header, sizeof ( pe_header ) );
  624. pe_header.nt.OptionalHeader.AddressOfEntryPoint =
  625. bfd_get_start_address ( bfd );
  626. pe_header.nt.OptionalHeader.Subsystem = opts->subsystem;
  627. /* For each input section, build an output section and create
  628. * the appropriate relocation records
  629. */
  630. for ( section = bfd->sections ; section ; section = section->next ) {
  631. /* Discard non-allocatable sections */
  632. if ( ! ( bfd_get_section_flags ( bfd, section ) & SEC_ALLOC ) )
  633. continue;
  634. /* Create output section */
  635. *(next_pe_section) = process_section ( bfd, &pe_header,
  636. section );
  637. next_pe_section = &(*next_pe_section)->next;
  638. /* Add relocations from this section */
  639. reltab = read_reltab ( bfd, symtab, section );
  640. for ( rel = reltab ; *rel ; rel++ )
  641. process_reloc ( bfd, section, *rel, &pe_reltab );
  642. free ( reltab );
  643. }
  644. /* Create the .reloc section */
  645. *(next_pe_section) = create_reloc_section ( &pe_header, pe_reltab );
  646. next_pe_section = &(*next_pe_section)->next;
  647. /* Create the .reloc section */
  648. *(next_pe_section) = create_debug_section ( &pe_header,
  649. basename ( pe_name_tmp ) );
  650. next_pe_section = &(*next_pe_section)->next;
  651. /* Write out PE file */
  652. pe = fopen ( pe_name, "w" );
  653. if ( ! pe ) {
  654. eprintf ( "Could not open %s for writing: %s\n",
  655. pe_name, strerror ( errno ) );
  656. exit ( 1 );
  657. }
  658. write_pe_file ( &pe_header, pe_sections, pe );
  659. fclose ( pe );
  660. /* Close BFD file */
  661. bfd_close ( bfd );
  662. }
  663. /**
  664. * Print help
  665. *
  666. * @v program_name Program name
  667. */
  668. static void print_help ( const char *program_name ) {
  669. eprintf ( "Syntax: %s [--subsystem=<number>] infile outfile\n",
  670. program_name );
  671. }
  672. /**
  673. * Parse command-line options
  674. *
  675. * @v argc Argument count
  676. * @v argv Argument list
  677. * @v opts Options structure to populate
  678. */
  679. static int parse_options ( const int argc, char **argv,
  680. struct options *opts ) {
  681. char *end;
  682. int c;
  683. while (1) {
  684. int option_index = 0;
  685. static struct option long_options[] = {
  686. { "subsystem", required_argument, NULL, 's' },
  687. { "help", 0, NULL, 'h' },
  688. { 0, 0, 0, 0 }
  689. };
  690. if ( ( c = getopt_long ( argc, argv, "s:h",
  691. long_options,
  692. &option_index ) ) == -1 ) {
  693. break;
  694. }
  695. switch ( c ) {
  696. case 's':
  697. opts->subsystem = strtoul ( optarg, &end, 0 );
  698. if ( *end ) {
  699. eprintf ( "Invalid subsytem \"%s\"\n",
  700. optarg );
  701. exit ( 2 );
  702. }
  703. break;
  704. case 'h':
  705. print_help ( argv[0] );
  706. exit ( 0 );
  707. case '?':
  708. default:
  709. exit ( 2 );
  710. }
  711. }
  712. return optind;
  713. }
  714. int main ( int argc, char **argv ) {
  715. struct options opts = {
  716. .subsystem = EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,
  717. };
  718. int infile_index;
  719. const char *infile;
  720. const char *outfile;
  721. /* Initialise libbfd */
  722. bfd_init();
  723. /* Parse command-line arguments */
  724. infile_index = parse_options ( argc, argv, &opts );
  725. if ( argc != ( infile_index + 2 ) ) {
  726. print_help ( argv[0] );
  727. exit ( 2 );
  728. }
  729. infile = argv[infile_index];
  730. outfile = argv[infile_index + 1];
  731. /* Convert file */
  732. elf2pe ( infile, outfile, &opts );
  733. return 0;
  734. }