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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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_386_NONE" ) == 0 ) ||
  438. ( strcmp ( howto->name, "R_X86_64_NONE" ) == 0 ) ) {
  439. /* Ignore dummy relocations used by REQUIRE_SYMBOL() */
  440. } else if ( strcmp ( howto->name, "R_X86_64_64" ) == 0 ) {
  441. /* Generate an 8-byte PE relocation */
  442. generate_pe_reloc ( pe_reltab, offset, 8 );
  443. } else if ( strcmp ( howto->name, "R_386_32" ) == 0 ) {
  444. /* Generate a 4-byte PE relocation */
  445. generate_pe_reloc ( pe_reltab, offset, 4 );
  446. } else if ( strcmp ( howto->name, "R_386_16" ) == 0 ) {
  447. /* Generate a 2-byte PE relocation */
  448. generate_pe_reloc ( pe_reltab, offset, 2 );
  449. } else if ( ( strcmp ( howto->name, "R_386_PC32" ) == 0 ) ||
  450. ( strcmp ( howto->name, "R_X86_64_PC32" ) == 0 ) ) {
  451. /* Skip PC-relative relocations; all relative offsets
  452. * remain unaltered when the object is loaded.
  453. */
  454. } else {
  455. eprintf ( "Unrecognised relocation type %s\n", howto->name );
  456. exit ( 1 );
  457. }
  458. }
  459. /**
  460. * Create relocations section
  461. *
  462. * @v pe_header PE file header
  463. * @v pe_reltab PE relocation table
  464. * @ret section Relocation section
  465. */
  466. static struct pe_section *
  467. create_reloc_section ( struct pe_header *pe_header,
  468. struct pe_relocs *pe_reltab ) {
  469. struct pe_section *reloc;
  470. size_t section_memsz;
  471. size_t section_filesz;
  472. EFI_IMAGE_DATA_DIRECTORY *relocdir;
  473. /* Allocate PE section */
  474. section_memsz = output_pe_reltab ( pe_reltab, NULL );
  475. section_filesz = efi_file_align ( section_memsz );
  476. reloc = xmalloc ( sizeof ( *reloc ) + section_filesz );
  477. memset ( reloc, 0, sizeof ( *reloc ) + section_filesz );
  478. /* Fill in section header details */
  479. strncpy ( ( char * ) reloc->hdr.Name, ".reloc",
  480. sizeof ( reloc->hdr.Name ) );
  481. reloc->hdr.Misc.VirtualSize = section_memsz;
  482. reloc->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  483. reloc->hdr.SizeOfRawData = section_filesz;
  484. reloc->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  485. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  486. EFI_IMAGE_SCN_MEM_READ );
  487. /* Copy in section contents */
  488. output_pe_reltab ( pe_reltab, reloc->contents );
  489. /* Update file header details */
  490. pe_header->nt.FileHeader.NumberOfSections++;
  491. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( reloc->hdr );
  492. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  493. relocdir = &(pe_header->nt.OptionalHeader.DataDirectory
  494. [EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  495. relocdir->VirtualAddress = reloc->hdr.VirtualAddress;
  496. relocdir->Size = reloc->hdr.Misc.VirtualSize;
  497. return reloc;
  498. }
  499. /**
  500. * Create debug section
  501. *
  502. * @v pe_header PE file header
  503. * @ret section Debug section
  504. */
  505. static struct pe_section *
  506. create_debug_section ( struct pe_header *pe_header, const char *filename ) {
  507. struct pe_section *debug;
  508. size_t section_memsz;
  509. size_t section_filesz;
  510. EFI_IMAGE_DATA_DIRECTORY *debugdir;
  511. struct {
  512. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY debug;
  513. EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY rsds;
  514. char name[ strlen ( filename ) + 1 ];
  515. } *contents;
  516. /* Allocate PE section */
  517. section_memsz = sizeof ( *contents );
  518. section_filesz = efi_file_align ( section_memsz );
  519. debug = xmalloc ( sizeof ( *debug ) + section_filesz );
  520. memset ( debug, 0, sizeof ( *debug ) + section_filesz );
  521. contents = ( void * ) debug->contents;
  522. /* Fill in section header details */
  523. strncpy ( ( char * ) debug->hdr.Name, ".debug",
  524. sizeof ( debug->hdr.Name ) );
  525. debug->hdr.Misc.VirtualSize = section_memsz;
  526. debug->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
  527. debug->hdr.SizeOfRawData = section_filesz;
  528. debug->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  529. EFI_IMAGE_SCN_MEM_NOT_PAGED |
  530. EFI_IMAGE_SCN_MEM_READ );
  531. /* Create section contents */
  532. contents->debug.TimeDateStamp = 0x10d1a884;
  533. contents->debug.Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
  534. contents->debug.SizeOfData =
  535. ( sizeof ( *contents ) - sizeof ( contents->debug ) );
  536. contents->debug.RVA = ( debug->hdr.VirtualAddress +
  537. offsetof ( typeof ( *contents ), rsds ) );
  538. contents->rsds.Signature = CODEVIEW_SIGNATURE_RSDS;
  539. snprintf ( contents->name, sizeof ( contents->name ), "%s",
  540. filename );
  541. /* Update file header details */
  542. pe_header->nt.FileHeader.NumberOfSections++;
  543. pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( debug->hdr );
  544. pe_header->nt.OptionalHeader.SizeOfImage += section_filesz;
  545. debugdir = &(pe_header->nt.OptionalHeader.DataDirectory
  546. [EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
  547. debugdir->VirtualAddress = debug->hdr.VirtualAddress;
  548. debugdir->Size = debug->hdr.Misc.VirtualSize;
  549. return debug;
  550. }
  551. /**
  552. * Write out PE file
  553. *
  554. * @v pe_header PE file header
  555. * @v pe_sections List of PE sections
  556. * @v pe Output file
  557. */
  558. static void write_pe_file ( struct pe_header *pe_header,
  559. struct pe_section *pe_sections,
  560. FILE *pe ) {
  561. struct pe_section *section;
  562. unsigned long fpos = 0;
  563. /* Align length of headers */
  564. fpos = pe_header->nt.OptionalHeader.SizeOfHeaders =
  565. efi_file_align ( pe_header->nt.OptionalHeader.SizeOfHeaders );
  566. /* Assign raw data pointers */
  567. for ( section = pe_sections ; section ; section = section->next ) {
  568. if ( section->hdr.SizeOfRawData ) {
  569. section->hdr.PointerToRawData = fpos;
  570. fpos += section->hdr.SizeOfRawData;
  571. fpos = efi_file_align ( fpos );
  572. }
  573. }
  574. /* Write file header */
  575. if ( fwrite ( pe_header, sizeof ( *pe_header ), 1, pe ) != 1 ) {
  576. perror ( "Could not write PE header" );
  577. exit ( 1 );
  578. }
  579. /* Write section headers */
  580. for ( section = pe_sections ; section ; section = section->next ) {
  581. if ( fwrite ( &section->hdr, sizeof ( section->hdr ),
  582. 1, pe ) != 1 ) {
  583. perror ( "Could not write section header" );
  584. exit ( 1 );
  585. }
  586. }
  587. /* Write sections */
  588. for ( section = pe_sections ; section ; section = section->next ) {
  589. if ( fseek ( pe, section->hdr.PointerToRawData,
  590. SEEK_SET ) != 0 ) {
  591. eprintf ( "Could not seek to %x: %s\n",
  592. section->hdr.PointerToRawData,
  593. strerror ( errno ) );
  594. exit ( 1 );
  595. }
  596. if ( section->hdr.SizeOfRawData &&
  597. ( fwrite ( section->contents, section->hdr.SizeOfRawData,
  598. 1, pe ) != 1 ) ) {
  599. eprintf ( "Could not write section %.8s: %s\n",
  600. section->hdr.Name, strerror ( errno ) );
  601. exit ( 1 );
  602. }
  603. }
  604. }
  605. /**
  606. * Convert ELF to PE
  607. *
  608. * @v elf_name ELF file name
  609. * @v pe_name PE file name
  610. */
  611. static void elf2pe ( const char *elf_name, const char *pe_name,
  612. struct options *opts ) {
  613. char pe_name_tmp[ strlen ( pe_name ) + 1 ];
  614. bfd *bfd;
  615. asymbol **symtab;
  616. asection *section;
  617. arelent **reltab;
  618. arelent **rel;
  619. struct pe_relocs *pe_reltab = NULL;
  620. struct pe_section *pe_sections = NULL;
  621. struct pe_section **next_pe_section = &pe_sections;
  622. struct pe_header pe_header;
  623. FILE *pe;
  624. /* Create a modifiable copy of the PE name */
  625. memcpy ( pe_name_tmp, pe_name, sizeof ( pe_name_tmp ) );
  626. /* Open the file */
  627. bfd = open_input_bfd ( elf_name );
  628. symtab = read_symtab ( bfd );
  629. /* Initialise the PE header */
  630. memcpy ( &pe_header, &efi_pe_header, sizeof ( pe_header ) );
  631. pe_header.nt.OptionalHeader.AddressOfEntryPoint =
  632. bfd_get_start_address ( bfd );
  633. pe_header.nt.OptionalHeader.Subsystem = opts->subsystem;
  634. /* For each input section, build an output section and create
  635. * the appropriate relocation records
  636. */
  637. for ( section = bfd->sections ; section ; section = section->next ) {
  638. /* Discard non-allocatable sections */
  639. if ( ! ( bfd_get_section_flags ( bfd, section ) & SEC_ALLOC ) )
  640. continue;
  641. /* Create output section */
  642. *(next_pe_section) = process_section ( bfd, &pe_header,
  643. section );
  644. next_pe_section = &(*next_pe_section)->next;
  645. /* Add relocations from this section */
  646. reltab = read_reltab ( bfd, symtab, section );
  647. for ( rel = reltab ; *rel ; rel++ )
  648. process_reloc ( bfd, section, *rel, &pe_reltab );
  649. free ( reltab );
  650. }
  651. /* Create the .reloc section */
  652. *(next_pe_section) = create_reloc_section ( &pe_header, pe_reltab );
  653. next_pe_section = &(*next_pe_section)->next;
  654. /* Create the .reloc section */
  655. *(next_pe_section) = create_debug_section ( &pe_header,
  656. basename ( pe_name_tmp ) );
  657. next_pe_section = &(*next_pe_section)->next;
  658. /* Write out PE file */
  659. pe = fopen ( pe_name, "w" );
  660. if ( ! pe ) {
  661. eprintf ( "Could not open %s for writing: %s\n",
  662. pe_name, strerror ( errno ) );
  663. exit ( 1 );
  664. }
  665. write_pe_file ( &pe_header, pe_sections, pe );
  666. fclose ( pe );
  667. /* Close BFD file */
  668. bfd_close ( bfd );
  669. }
  670. /**
  671. * Print help
  672. *
  673. * @v program_name Program name
  674. */
  675. static void print_help ( const char *program_name ) {
  676. eprintf ( "Syntax: %s [--subsystem=<number>] infile outfile\n",
  677. program_name );
  678. }
  679. /**
  680. * Parse command-line options
  681. *
  682. * @v argc Argument count
  683. * @v argv Argument list
  684. * @v opts Options structure to populate
  685. */
  686. static int parse_options ( const int argc, char **argv,
  687. struct options *opts ) {
  688. char *end;
  689. int c;
  690. while (1) {
  691. int option_index = 0;
  692. static struct option long_options[] = {
  693. { "subsystem", required_argument, NULL, 's' },
  694. { "help", 0, NULL, 'h' },
  695. { 0, 0, 0, 0 }
  696. };
  697. if ( ( c = getopt_long ( argc, argv, "s:h",
  698. long_options,
  699. &option_index ) ) == -1 ) {
  700. break;
  701. }
  702. switch ( c ) {
  703. case 's':
  704. opts->subsystem = strtoul ( optarg, &end, 0 );
  705. if ( *end ) {
  706. eprintf ( "Invalid subsytem \"%s\"\n",
  707. optarg );
  708. exit ( 2 );
  709. }
  710. break;
  711. case 'h':
  712. print_help ( argv[0] );
  713. exit ( 0 );
  714. case '?':
  715. default:
  716. exit ( 2 );
  717. }
  718. }
  719. return optind;
  720. }
  721. int main ( int argc, char **argv ) {
  722. struct options opts = {
  723. .subsystem = EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,
  724. };
  725. int infile_index;
  726. const char *infile;
  727. const char *outfile;
  728. /* Initialise libbfd */
  729. bfd_init();
  730. /* Parse command-line arguments */
  731. infile_index = parse_options ( argc, argv, &opts );
  732. if ( argc != ( infile_index + 2 ) ) {
  733. print_help ( argv[0] );
  734. exit ( 2 );
  735. }
  736. infile = argv[infile_index];
  737. outfile = argv[infile_index + 1];
  738. /* Convert file */
  739. elf2pe ( infile, outfile, &opts );
  740. return 0;
  741. }