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 23KB

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