2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
30 #include <gpxe/efi/efi.h>
31 #include <gpxe/efi/IndustryStandard/PeImage.h>
34 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
36 #define EFI_FILE_ALIGN 0x20
39 struct pe_section
*next
;
40 EFI_IMAGE_SECTION_HEADER hdr
;
45 struct pe_relocs
*next
;
46 unsigned long start_rva
;
47 unsigned int used_relocs
;
48 unsigned int total_relocs
;
53 EFI_IMAGE_DOS_HEADER dos
;
55 #if defined(MDE_CPU_IA32)
56 EFI_IMAGE_NT_HEADERS32 nt
;
57 #elif defined(MDE_CPU_X64)
58 EFI_IMAGE_NT_HEADERS64 nt
;
62 static struct pe_header efi_pe_header
= {
64 .e_magic
= EFI_IMAGE_DOS_SIGNATURE
,
65 .e_lfanew
= offsetof ( typeof ( efi_pe_header
), nt
),
68 .Signature
= EFI_IMAGE_NT_SIGNATURE
,
70 #if defined(MDE_CPU_IA32)
71 .Machine
= EFI_IMAGE_MACHINE_IA32
,
72 #elif defined(MDE_CPU_X64)
73 .Machine
= EFI_IMAGE_MACHINE_X64
,
75 .TimeDateStamp
= 0x10d1a884,
76 .SizeOfOptionalHeader
=
77 sizeof ( efi_pe_header
.nt
.OptionalHeader
),
78 .Characteristics
= ( EFI_IMAGE_FILE_DLL
|
79 #if defined(MDE_CPU_IA32)
80 EFI_IMAGE_FILE_32BIT_MACHINE
|
82 EFI_IMAGE_FILE_EXECUTABLE_IMAGE
),
85 #if defined(MDE_CPU_IA32)
86 .Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
,
87 #elif defined(MDE_CPU_X64)
88 .Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
,
90 .SectionAlignment
= EFI_FILE_ALIGN
,
91 .FileAlignment
= EFI_FILE_ALIGN
,
92 .SizeOfImage
= sizeof ( efi_pe_header
),
93 .SizeOfHeaders
= sizeof ( efi_pe_header
),
94 .NumberOfRvaAndSizes
=
95 EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
,
100 /** Command-line options */
102 unsigned int subsystem
;
108 * @v len Length of memory to allocate
109 * @ret ptr Pointer to allocated memory
111 static void * xmalloc ( size_t len
) {
114 ptr
= malloc ( len
);
116 eprintf ( "Could not allocate %zd bytes\n", len
);
124 * Align section within PE file
126 * @v offset Unaligned offset
127 * @ret aligned_offset Aligned offset
129 static unsigned long efi_file_align ( unsigned long offset
) {
130 return ( ( offset
+ EFI_FILE_ALIGN
- 1 ) & ~( EFI_FILE_ALIGN
- 1 ) );
134 * Generate entry in PE relocation table
136 * @v pe_reltab PE relocation table
138 * @v size Size of relocation entry
140 static void generate_pe_reloc ( struct pe_relocs
**pe_reltab
,
141 unsigned long rva
, size_t size
) {
142 unsigned long start_rva
;
144 struct pe_relocs
*pe_rel
;
148 start_rva
= ( rva
& ~0xfff );
149 reloc
= ( rva
& 0xfff );
161 eprintf ( "Unsupported relocation size %zd\n", size
);
165 /* Locate or create PE relocation table */
166 for ( pe_rel
= *pe_reltab
; pe_rel
; pe_rel
= pe_rel
->next
) {
167 if ( pe_rel
->start_rva
== start_rva
)
171 pe_rel
= xmalloc ( sizeof ( *pe_rel
) );
172 memset ( pe_rel
, 0, sizeof ( *pe_rel
) );
173 pe_rel
->next
= *pe_reltab
;
175 pe_rel
->start_rva
= start_rva
;
178 /* Expand relocation list if necessary */
179 if ( pe_rel
->used_relocs
< pe_rel
->total_relocs
) {
180 relocs
= pe_rel
->relocs
;
182 pe_rel
->total_relocs
= ( pe_rel
->total_relocs
?
183 ( pe_rel
->total_relocs
* 2 ) : 256 );
184 relocs
= xmalloc ( pe_rel
->total_relocs
*
185 sizeof ( pe_rel
->relocs
[0] ) );
187 pe_rel
->total_relocs
* sizeof ( pe_rel
->relocs
[0] ) );
188 memcpy ( relocs
, pe_rel
->relocs
,
189 pe_rel
->used_relocs
* sizeof ( pe_rel
->relocs
[0] ) );
190 free ( pe_rel
->relocs
);
191 pe_rel
->relocs
= relocs
;
194 /* Store relocation */
195 pe_rel
->relocs
[ pe_rel
->used_relocs
++ ] = reloc
;
199 * Calculate size of binary PE relocation table
201 * @v pe_reltab PE relocation table
202 * @v buffer Buffer to contain binary table, or NULL
203 * @ret size Size of binary table
205 static size_t output_pe_reltab ( struct pe_relocs
*pe_reltab
,
207 struct pe_relocs
*pe_rel
;
208 unsigned int num_relocs
;
210 size_t total_size
= 0;
212 for ( pe_rel
= pe_reltab
; pe_rel
; pe_rel
= pe_rel
->next
) {
213 num_relocs
= ( ( pe_rel
->used_relocs
+ 1 ) & ~1 );
214 size
= ( sizeof ( uint32_t ) /* VirtualAddress */ +
215 sizeof ( uint32_t ) /* SizeOfBlock */ +
216 ( num_relocs
* sizeof ( uint16_t ) ) );
218 *( (uint32_t *) ( buffer
+ total_size
+ 0 ) )
220 *( (uint32_t *) ( buffer
+ total_size
+ 4 ) ) = size
;
221 memcpy ( ( buffer
+ total_size
+ 8 ), pe_rel
->relocs
,
222 ( num_relocs
* sizeof ( uint16_t ) ) );
231 * Open input BFD file
233 * @v filename File name
236 static bfd
* open_input_bfd ( const char *filename
) {
240 bfd
= bfd_openr ( filename
, NULL
);
242 eprintf ( "Cannot open %s: ", filename
);
247 /* The call to bfd_check_format() must be present, otherwise
248 * we get a segfault from later BFD calls.
250 if ( bfd_check_format ( bfd
, bfd_object
) < 0 ) {
251 eprintf ( "%s is not an object file\n", filename
);
263 static asymbol
** read_symtab ( bfd
*bfd
) {
268 /* Get symbol table size */
269 symtab_size
= bfd_get_symtab_upper_bound ( bfd
);
270 if ( symtab_size
< 0 ) {
271 bfd_perror ( "Could not get symbol table upper bound" );
275 /* Allocate and read symbol table */
276 symtab
= xmalloc ( symtab_size
);
277 symcount
= bfd_canonicalize_symtab ( bfd
, symtab
);
278 if ( symcount
< 0 ) {
279 bfd_perror ( "Cannot read symbol table" );
287 * Read relocation table
290 * @v symtab Symbol table
292 * @v symtab Symbol table
293 * @ret reltab Relocation table
295 static arelent
** read_reltab ( bfd
*bfd
, asymbol
**symtab
,
296 asection
*section
) {
301 /* Get relocation table size */
302 reltab_size
= bfd_get_reloc_upper_bound ( bfd
, section
);
303 if ( reltab_size
< 0 ) {
304 bfd_perror ( "Could not get relocation table upper bound" );
308 /* Allocate and read relocation table */
309 reltab
= xmalloc ( reltab_size
);
310 numrels
= bfd_canonicalize_reloc ( bfd
, section
, reltab
, symtab
);
312 bfd_perror ( "Cannot read relocation table" );
323 * @v pe_header PE file header
325 * @ret new New PE section
327 static struct pe_section
* process_section ( bfd
*bfd
,
328 struct pe_header
*pe_header
,
329 asection
*section
) {
330 struct pe_section
*new;
331 size_t section_memsz
;
332 size_t section_filesz
;
333 unsigned long flags
= bfd_get_section_flags ( bfd
, section
);
334 unsigned long code_start
;
335 unsigned long code_end
;
336 unsigned long data_start
;
337 unsigned long data_mid
;
338 unsigned long data_end
;
341 unsigned long *applicable_start
;
342 unsigned long *applicable_end
;
344 /* Extract current RVA limits from file header */
345 code_start
= pe_header
->nt
.OptionalHeader
.BaseOfCode
;
346 code_end
= ( code_start
+ pe_header
->nt
.OptionalHeader
.SizeOfCode
);
347 #if defined(MDE_CPU_IA32)
348 data_start
= pe_header
->nt
.OptionalHeader
.BaseOfData
;
349 #elif defined(MDE_CPU_X64)
350 data_start
= code_end
;
352 data_mid
= ( data_start
+
353 pe_header
->nt
.OptionalHeader
.SizeOfInitializedData
);
354 data_end
= ( data_mid
+
355 pe_header
->nt
.OptionalHeader
.SizeOfUninitializedData
);
357 /* Allocate PE section */
358 section_memsz
= bfd_section_size ( bfd
, section
);
359 section_filesz
= ( ( flags
& SEC_LOAD
) ?
360 efi_file_align ( section_memsz
) : 0 );
361 new = xmalloc ( sizeof ( *new ) + section_filesz
);
362 memset ( new, 0, sizeof ( *new ) + section_filesz
);
364 /* Fill in section header details */
365 strncpy ( ( char * ) new->hdr
.Name
, section
->name
,
366 sizeof ( new->hdr
.Name
) );
367 new->hdr
.Misc
.VirtualSize
= section_memsz
;
368 new->hdr
.VirtualAddress
= bfd_get_section_vma ( bfd
, section
);
369 new->hdr
.SizeOfRawData
= section_filesz
;
371 /* Fill in section characteristics and update RVA limits */
372 if ( flags
& SEC_CODE
) {
373 /* .text-type section */
374 new->hdr
.Characteristics
=
375 ( EFI_IMAGE_SCN_CNT_CODE
|
376 EFI_IMAGE_SCN_MEM_NOT_PAGED
|
377 EFI_IMAGE_SCN_MEM_EXECUTE
|
378 EFI_IMAGE_SCN_MEM_READ
);
379 applicable_start
= &code_start
;
380 applicable_end
= &code_end
;
381 } else if ( flags
& SEC_DATA
) {
382 /* .data-type section */
383 new->hdr
.Characteristics
=
384 ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
|
385 EFI_IMAGE_SCN_MEM_NOT_PAGED
|
386 EFI_IMAGE_SCN_MEM_READ
|
387 EFI_IMAGE_SCN_MEM_WRITE
);
388 applicable_start
= &data_start
;
389 applicable_end
= &data_mid
;
390 } else if ( flags
& SEC_READONLY
) {
391 /* .rodata-type section */
392 new->hdr
.Characteristics
=
393 ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
|
394 EFI_IMAGE_SCN_MEM_NOT_PAGED
|
395 EFI_IMAGE_SCN_MEM_READ
);
396 applicable_start
= &data_start
;
397 applicable_end
= &data_mid
;
398 } else if ( ! ( flags
& SEC_LOAD
) ) {
399 /* .bss-type section */
400 new->hdr
.Characteristics
=
401 ( EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
402 EFI_IMAGE_SCN_MEM_NOT_PAGED
|
403 EFI_IMAGE_SCN_MEM_READ
|
404 EFI_IMAGE_SCN_MEM_WRITE
);
405 applicable_start
= &data_mid
;
406 applicable_end
= &data_end
;
409 /* Copy in section contents */
410 if ( flags
& SEC_LOAD
) {
411 if ( ! bfd_get_section_contents ( bfd
, section
, new->contents
,
412 0, section_memsz
) ) {
413 eprintf ( "Cannot read section %s: ", section
->name
);
419 /* Update RVA limits */
420 start
= new->hdr
.VirtualAddress
;
421 end
= ( start
+ new->hdr
.Misc
.VirtualSize
);
422 if ( ( ! *applicable_start
) || ( *applicable_start
>= start
) )
423 *applicable_start
= start
;
424 if ( *applicable_end
< end
)
425 *applicable_end
= end
;
426 if ( data_start
< code_end
)
427 data_start
= code_end
;
428 if ( data_mid
< data_start
)
429 data_mid
= data_start
;
430 if ( data_end
< data_mid
)
433 /* Write RVA limits back to file header */
434 pe_header
->nt
.OptionalHeader
.BaseOfCode
= code_start
;
435 pe_header
->nt
.OptionalHeader
.SizeOfCode
= ( code_end
- code_start
);
436 #if defined(MDE_CPU_IA32)
437 pe_header
->nt
.OptionalHeader
.BaseOfData
= data_start
;
439 pe_header
->nt
.OptionalHeader
.SizeOfInitializedData
=
440 ( data_mid
- data_start
);
441 pe_header
->nt
.OptionalHeader
.SizeOfUninitializedData
=
442 ( data_end
- data_mid
);
444 /* Update remaining file header fields */
445 pe_header
->nt
.FileHeader
.NumberOfSections
++;
446 pe_header
->nt
.OptionalHeader
.SizeOfHeaders
+= sizeof ( new->hdr
);
447 pe_header
->nt
.OptionalHeader
.SizeOfImage
=
448 efi_file_align ( data_end
);
454 * Process relocation record
458 * @v rel Relocation entry
459 * @v pe_reltab PE relocation table to fill in
461 static void process_reloc ( bfd
*bfd
, asection
*section
, arelent
*rel
,
462 struct pe_relocs
**pe_reltab
) {
463 reloc_howto_type
*howto
= rel
->howto
;
464 asymbol
*sym
= *(rel
->sym_ptr_ptr
);
465 unsigned long offset
= ( bfd_get_section_vma ( bfd
, section
) +
468 if ( bfd_is_abs_section ( sym
->section
) ) {
469 /* Skip absolute symbols; the symbol value won't
470 * change when the object is loaded.
472 } else if ( strcmp ( howto
->name
, "R_X86_64_64" ) == 0 ) {
473 /* Generate an 8-byte PE relocation */
474 generate_pe_reloc ( pe_reltab
, offset
, 8 );
475 } else if ( ( strcmp ( howto
->name
, "R_386_32" ) == 0 ) ||
476 ( strcmp ( howto
->name
, "R_X86_64_32" ) == 0 ) ) {
477 /* Generate a 4-byte PE relocation */
478 generate_pe_reloc ( pe_reltab
, offset
, 4 );
479 } else if ( strcmp ( howto
->name
, "R_386_16" ) == 0 ) {
480 /* Generate a 2-byte PE relocation */
481 generate_pe_reloc ( pe_reltab
, offset
, 2 );
482 } else if ( ( strcmp ( howto
->name
, "R_386_PC32" ) == 0 ) ||
483 ( strcmp ( howto
->name
, "R_X86_64_PC32" ) == 0 ) ) {
484 /* Skip PC-relative relocations; all relative offsets
485 * remain unaltered when the object is loaded.
488 eprintf ( "Unrecognised relocation type %s\n", howto
->name
);
494 * Create relocations section
496 * @v pe_header PE file header
497 * @v pe_reltab PE relocation table
498 * @ret section Relocation section
500 static struct pe_section
*
501 create_reloc_section ( struct pe_header
*pe_header
,
502 struct pe_relocs
*pe_reltab
) {
503 struct pe_section
*reloc
;
504 size_t section_memsz
;
505 size_t section_filesz
;
506 EFI_IMAGE_DATA_DIRECTORY
*relocdir
;
508 /* Allocate PE section */
509 section_memsz
= output_pe_reltab ( pe_reltab
, NULL
);
510 section_filesz
= efi_file_align ( section_memsz
);
511 reloc
= xmalloc ( sizeof ( *reloc
) + section_filesz
);
512 memset ( reloc
, 0, sizeof ( *reloc
) + section_filesz
);
514 /* Fill in section header details */
515 strncpy ( ( char * ) reloc
->hdr
.Name
, ".reloc",
516 sizeof ( reloc
->hdr
.Name
) );
517 reloc
->hdr
.Misc
.VirtualSize
= section_memsz
;
518 reloc
->hdr
.VirtualAddress
= pe_header
->nt
.OptionalHeader
.SizeOfImage
;
519 reloc
->hdr
.SizeOfRawData
= section_filesz
;
520 reloc
->hdr
.Characteristics
= ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
|
521 EFI_IMAGE_SCN_MEM_NOT_PAGED
|
522 EFI_IMAGE_SCN_MEM_READ
);
524 /* Copy in section contents */
525 output_pe_reltab ( pe_reltab
, reloc
->contents
);
527 /* Update file header details */
528 pe_header
->nt
.FileHeader
.NumberOfSections
++;
529 pe_header
->nt
.OptionalHeader
.SizeOfHeaders
+= sizeof ( reloc
->hdr
);
530 pe_header
->nt
.OptionalHeader
.SizeOfImage
+= section_filesz
;
531 relocdir
= &(pe_header
->nt
.OptionalHeader
.DataDirectory
532 [EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
]);
533 relocdir
->VirtualAddress
= reloc
->hdr
.VirtualAddress
;
534 relocdir
->Size
= reloc
->hdr
.Misc
.VirtualSize
;
540 * Create debug section
542 * @v pe_header PE file header
543 * @ret section Debug section
545 static struct pe_section
*
546 create_debug_section ( struct pe_header
*pe_header
, const char *filename
) {
547 struct pe_section
*debug
;
548 size_t section_memsz
;
549 size_t section_filesz
;
550 EFI_IMAGE_DATA_DIRECTORY
*debugdir
;
552 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY debug
;
553 EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY rsds
;
554 char name
[ strlen ( filename
) + 1 ];
557 /* Allocate PE section */
558 section_memsz
= sizeof ( *contents
);
559 section_filesz
= efi_file_align ( section_memsz
);
560 debug
= xmalloc ( sizeof ( *debug
) + section_filesz
);
561 memset ( debug
, 0, sizeof ( *debug
) + section_filesz
);
562 contents
= ( void * ) debug
->contents
;
564 /* Fill in section header details */
565 strncpy ( ( char * ) debug
->hdr
.Name
, ".debug",
566 sizeof ( debug
->hdr
.Name
) );
567 debug
->hdr
.Misc
.VirtualSize
= section_memsz
;
568 debug
->hdr
.VirtualAddress
= pe_header
->nt
.OptionalHeader
.SizeOfImage
;
569 debug
->hdr
.SizeOfRawData
= section_filesz
;
570 debug
->hdr
.Characteristics
= ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
|
571 EFI_IMAGE_SCN_MEM_NOT_PAGED
|
572 EFI_IMAGE_SCN_MEM_READ
);
574 /* Create section contents */
575 contents
->debug
.TimeDateStamp
= 0x10d1a884;
576 contents
->debug
.Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
577 contents
->debug
.SizeOfData
=
578 ( sizeof ( *contents
) - sizeof ( contents
->debug
) );
579 contents
->debug
.RVA
= ( debug
->hdr
.VirtualAddress
+
580 offsetof ( typeof ( *contents
), rsds
) );
581 contents
->rsds
.Signature
= CODEVIEW_SIGNATURE_RSDS
;
582 snprintf ( contents
->name
, sizeof ( contents
->name
), "%s",
585 /* Update file header details */
586 pe_header
->nt
.FileHeader
.NumberOfSections
++;
587 pe_header
->nt
.OptionalHeader
.SizeOfHeaders
+= sizeof ( debug
->hdr
);
588 pe_header
->nt
.OptionalHeader
.SizeOfImage
+= section_filesz
;
589 debugdir
= &(pe_header
->nt
.OptionalHeader
.DataDirectory
590 [EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
591 debugdir
->VirtualAddress
= debug
->hdr
.VirtualAddress
;
592 debugdir
->Size
= debug
->hdr
.Misc
.VirtualSize
;
600 * @v pe_header PE file header
601 * @v pe_sections List of PE sections
604 static void write_pe_file ( struct pe_header
*pe_header
,
605 struct pe_section
*pe_sections
,
607 struct pe_section
*section
;
608 unsigned long fpos
= 0;
610 /* Assign raw data pointers */
611 fpos
= efi_file_align ( pe_header
->nt
.OptionalHeader
.SizeOfHeaders
);
612 for ( section
= pe_sections
; section
; section
= section
->next
) {
613 if ( section
->hdr
.SizeOfRawData
) {
614 section
->hdr
.PointerToRawData
= fpos
;
615 fpos
+= section
->hdr
.SizeOfRawData
;
616 fpos
= efi_file_align ( fpos
);
620 /* Write file header */
621 if ( fwrite ( pe_header
, sizeof ( *pe_header
), 1, pe
) != 1 ) {
622 perror ( "Could not write PE header" );
626 /* Write section headers */
627 for ( section
= pe_sections
; section
; section
= section
->next
) {
628 if ( fwrite ( §ion
->hdr
, sizeof ( section
->hdr
),
630 perror ( "Could not write section header" );
636 for ( section
= pe_sections
; section
; section
= section
->next
) {
637 if ( fseek ( pe
, section
->hdr
.PointerToRawData
,
639 eprintf ( "Could not seek to %lx: %s\n",
640 section
->hdr
.PointerToRawData
,
641 strerror ( errno
) );
644 if ( section
->hdr
.SizeOfRawData
&&
645 ( fwrite ( section
->contents
, section
->hdr
.SizeOfRawData
,
647 eprintf ( "Could not write section %.8s: %s\n",
648 section
->hdr
.Name
, strerror ( errno
) );
657 * @v elf_name ELF file name
658 * @v pe_name PE file name
660 static void elf2pe ( const char *elf_name
, const char *pe_name
,
661 struct options
*opts
) {
662 char pe_name_tmp
[ strlen ( pe_name
) + 1 ];
668 struct pe_relocs
*pe_reltab
= NULL
;
669 struct pe_section
*pe_sections
= NULL
;
670 struct pe_section
**next_pe_section
= &pe_sections
;
671 struct pe_header pe_header
;
674 /* Create a modifiable copy of the PE name */
675 memcpy ( pe_name_tmp
, pe_name
, sizeof ( pe_name_tmp
) );
678 bfd
= open_input_bfd ( elf_name
);
679 symtab
= read_symtab ( bfd
);
681 /* Initialise the PE header */
682 memcpy ( &pe_header
, &efi_pe_header
, sizeof ( pe_header
) );
683 pe_header
.nt
.OptionalHeader
.AddressOfEntryPoint
=
684 bfd_get_start_address ( bfd
);
685 pe_header
.nt
.OptionalHeader
.Subsystem
= opts
->subsystem
;
687 /* For each input section, build an output section and create
688 * the appropriate relocation records
690 for ( section
= bfd
->sections
; section
; section
= section
->next
) {
691 /* Discard non-allocatable sections */
692 if ( ! ( bfd_get_section_flags ( bfd
, section
) & SEC_ALLOC
) )
694 /* Create output section */
695 *(next_pe_section
) = process_section ( bfd
, &pe_header
,
697 next_pe_section
= &(*next_pe_section
)->next
;
698 /* Add relocations from this section */
699 reltab
= read_reltab ( bfd
, symtab
, section
);
700 for ( rel
= reltab
; *rel
; rel
++ )
701 process_reloc ( bfd
, section
, *rel
, &pe_reltab
);
705 /* Create the .reloc section */
706 *(next_pe_section
) = create_reloc_section ( &pe_header
, pe_reltab
);
707 next_pe_section
= &(*next_pe_section
)->next
;
709 /* Create the .reloc section */
710 *(next_pe_section
) = create_debug_section ( &pe_header
,
711 basename ( pe_name_tmp
) );
712 next_pe_section
= &(*next_pe_section
)->next
;
714 /* Write out PE file */
715 pe
= fopen ( pe_name
, "w" );
717 eprintf ( "Could not open %s for writing: %s\n",
718 pe_name
, strerror ( errno
) );
721 write_pe_file ( &pe_header
, pe_sections
, pe
);
731 * @v program_name Program name
733 static void print_help ( const char *program_name
) {
734 eprintf ( "Syntax: %s [--subsystem=<number>] infile outfile\n",
739 * Parse command-line options
741 * @v argc Argument count
742 * @v argv Argument list
743 * @v opts Options structure to populate
745 static int parse_options ( const int argc
, char **argv
,
746 struct options
*opts
) {
751 int option_index
= 0;
752 static struct option long_options
[] = {
753 { "subsystem", required_argument
, NULL
, 's' },
754 { "help", 0, NULL
, 'h' },
758 if ( ( c
= getopt_long ( argc
, argv
, "s:h",
760 &option_index
) ) == -1 ) {
766 opts
->subsystem
= strtoul ( optarg
, &end
, 0 );
768 eprintf ( "Invalid subsytem \"%s\"\n",
774 print_help ( argv
[0] );
784 int main ( int argc
, char **argv
) {
785 struct options opts
= {
786 .subsystem
= EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
,
788 unsigned int infile_index
;
792 /* Initialise libbfd */
795 /* Parse command-line arguments */
796 infile_index
= parse_options ( argc
, argv
, &opts
);
797 if ( argc
!= ( infile_index
+ 2 ) ) {
798 print_help ( argv
[0] );
801 infile
= argv
[infile_index
];
802 outfile
= argv
[infile_index
+ 1];
805 elf2pe ( infile
, outfile
, &opts
);