Home | History | Annotate | Line # | Download | only in dist
README.gnuefi revision 1.1.1.1.6.2
      1  1.1.1.1.6.2  yamt 	-------------------------------------------------
      2  1.1.1.1.6.2  yamt 	Building EFI Applications Using the GNU Toolchain
      3  1.1.1.1.6.2  yamt 	-------------------------------------------------
      4  1.1.1.1.6.2  yamt 
      5  1.1.1.1.6.2  yamt 		David Mosberger <davidm (a] hpl.hp.com>
      6  1.1.1.1.6.2  yamt 
      7  1.1.1.1.6.2  yamt 			23 September 1999
      8  1.1.1.1.6.2  yamt 
      9  1.1.1.1.6.2  yamt 
     10  1.1.1.1.6.2  yamt 		Copyright (c) 1999-2007 Hewlett-Packard Co.
     11  1.1.1.1.6.2  yamt 		Copyright (c) 2006-2010 Intel Co.
     12  1.1.1.1.6.2  yamt 
     13  1.1.1.1.6.2  yamt Last update: 04/09/2007
     14  1.1.1.1.6.2  yamt 
     15  1.1.1.1.6.2  yamt * Introduction
     16  1.1.1.1.6.2  yamt 
     17  1.1.1.1.6.2  yamt This document has two parts: the first part describes how to develop
     18  1.1.1.1.6.2  yamt EFI applications for IA-64,x86 and x86_64 using the GNU toolchain and the EFI
     19  1.1.1.1.6.2  yamt development environment contained in this directory.  The second part
     20  1.1.1.1.6.2  yamt describes some of the more subtle aspects of how this development
     21  1.1.1.1.6.2  yamt environment works.
     22  1.1.1.1.6.2  yamt 
     23  1.1.1.1.6.2  yamt 
     24  1.1.1.1.6.2  yamt 
     25  1.1.1.1.6.2  yamt * Part 1: Developing EFI Applications
     26  1.1.1.1.6.2  yamt 
     27  1.1.1.1.6.2  yamt 
     28  1.1.1.1.6.2  yamt ** Prerequisites:
     29  1.1.1.1.6.2  yamt 
     30  1.1.1.1.6.2  yamt  To develop x86 and x86_64 EFI applications, the following tools are needed:
     31  1.1.1.1.6.2  yamt 
     32  1.1.1.1.6.2  yamt 	- gcc-3.0 or newer (gcc 2.7.2 is NOT sufficient!)
     33  1.1.1.1.6.2  yamt 	  As of gnu-efi-3.0b, the Redhat 8.0 toolchain is known to work,
     34  1.1.1.1.6.2  yamt 	  but the Redhat 9.0 toolchain is not currently supported.
     35  1.1.1.1.6.2  yamt 
     36  1.1.1.1.6.2  yamt 	- A version of "objcopy" that supports EFI applications.  To
     37  1.1.1.1.6.2  yamt 	  check if your version includes EFI support, issue the
     38  1.1.1.1.6.2  yamt 	  command:
     39  1.1.1.1.6.2  yamt 
     40  1.1.1.1.6.2  yamt 		objcopy --help
     41  1.1.1.1.6.2  yamt 
     42  1.1.1.1.6.2  yamt 	  and verify that the line "supported targets" contains the
     43  1.1.1.1.6.2  yamt 	  string "efi-app-ia32" and "efi-app-x86_64". The binutils release
     44  1.1.1.1.6.2  yamt 	  binutils-2.17.50.0.14 supports Intel64 EFI.
     45  1.1.1.1.6.2  yamt 
     46  1.1.1.1.6.2  yamt 	- For debugging purposes, it's useful to have a version of
     47  1.1.1.1.6.2  yamt 	  "objdump" that supports EFI applications as well.  This
     48  1.1.1.1.6.2  yamt 	  allows inspect and disassemble EFI binaries.
     49  1.1.1.1.6.2  yamt 
     50  1.1.1.1.6.2  yamt  To develop IA-64 EFI applications, the following tools are needed:
     51  1.1.1.1.6.2  yamt 
     52  1.1.1.1.6.2  yamt 	- A version of gcc newer than July 30th 1999 (older versions
     53  1.1.1.1.6.2  yamt 	  had problems with generating position independent code).
     54  1.1.1.1.6.2  yamt 	  As of gnu-efi-3.0b, gcc-3.1 is known to work well.
     55  1.1.1.1.6.2  yamt 
     56  1.1.1.1.6.2  yamt 	- A version of "objcopy" that supports EFI applications.  To
     57  1.1.1.1.6.2  yamt 	  check if your version includes EFI support, issue the
     58  1.1.1.1.6.2  yamt 	  command:
     59  1.1.1.1.6.2  yamt 
     60  1.1.1.1.6.2  yamt 		objcopy --help
     61  1.1.1.1.6.2  yamt 
     62  1.1.1.1.6.2  yamt 	  and verify that the line "supported targets" contains the
     63  1.1.1.1.6.2  yamt 	  string "efi-app-ia64".
     64  1.1.1.1.6.2  yamt 
     65  1.1.1.1.6.2  yamt 	- For debugging purposes, it's useful to have a version of
     66  1.1.1.1.6.2  yamt 	  "objdump" that supports EFI applications as well.  This
     67  1.1.1.1.6.2  yamt 	  allows inspect and disassemble EFI binaries.
     68  1.1.1.1.6.2  yamt 
     69  1.1.1.1.6.2  yamt 
     70  1.1.1.1.6.2  yamt ** Directory Structure
     71  1.1.1.1.6.2  yamt 
     72  1.1.1.1.6.2  yamt This EFI development environment contains the following
     73  1.1.1.1.6.2  yamt subdirectories:
     74  1.1.1.1.6.2  yamt 
     75  1.1.1.1.6.2  yamt  inc:   This directory contains the EFI-related include files.  The
     76  1.1.1.1.6.2  yamt 	files are taken from Intel's EFI source distribution, except
     77  1.1.1.1.6.2  yamt 	that various fixes were applied to make it compile with the
     78  1.1.1.1.6.2  yamt 	GNU toolchain.
     79  1.1.1.1.6.2  yamt 
     80  1.1.1.1.6.2  yamt  lib:   This directory contains the source code for Intel's EFI library.
     81  1.1.1.1.6.2  yamt 	Again, the files are taken from Intel's EFI source
     82  1.1.1.1.6.2  yamt 	distribution, with changes to make them compile with the GNU
     83  1.1.1.1.6.2  yamt 	toolchain.
     84  1.1.1.1.6.2  yamt 
     85  1.1.1.1.6.2  yamt  gnuefi: This directory contains the glue necessary to convert ELF64
     86  1.1.1.1.6.2  yamt 	binaries to EFI binaries.  Various runtime code bits, such as
     87  1.1.1.1.6.2  yamt 	a self-relocator are included as well.  This code has been
     88  1.1.1.1.6.2  yamt 	contributed by the Hewlett-Packard Company and is distributed
     89  1.1.1.1.6.2  yamt 	under the GNU GPL.
     90  1.1.1.1.6.2  yamt 
     91  1.1.1.1.6.2  yamt  apps:	This directory contains a few simple EFI test apps.
     92  1.1.1.1.6.2  yamt 
     93  1.1.1.1.6.2  yamt ** Setup
     94  1.1.1.1.6.2  yamt 
     95  1.1.1.1.6.2  yamt It is necessary to edit the Makefile in the directory containing this
     96  1.1.1.1.6.2  yamt README file before EFI applications can be built.  Specifically, you
     97  1.1.1.1.6.2  yamt should verify that macros CC, AS, LD, AR, RANLIB, and OBJCOPY point to
     98  1.1.1.1.6.2  yamt the appropriate compiler, assembler, linker, ar, and ranlib binaries,
     99  1.1.1.1.6.2  yamt respectively.
    100  1.1.1.1.6.2  yamt 
    101  1.1.1.1.6.2  yamt If you're working in a cross-development environment, be sure to set
    102  1.1.1.1.6.2  yamt macro ARCH to the desired target architecture ("ia32" for x86, "x86_64" for
    103  1.1.1.1.6.2  yamt x86_64 and "ia64" for IA-64).  For convenience, this can also be done from
    104  1.1.1.1.6.2  yamt the make command line (e.g., "make ARCH=ia64").
    105  1.1.1.1.6.2  yamt 
    106  1.1.1.1.6.2  yamt 
    107  1.1.1.1.6.2  yamt ** Building
    108  1.1.1.1.6.2  yamt 
    109  1.1.1.1.6.2  yamt To build the sample EFI applications provided in subdirectory "apps",
    110  1.1.1.1.6.2  yamt simply invoke "make" in the toplevel directory (the directory
    111  1.1.1.1.6.2  yamt containing this README file).  This should build lib/libefi.a and
    112  1.1.1.1.6.2  yamt gnuefi/libgnuefi.a first and then all the EFI applications such as a
    113  1.1.1.1.6.2  yamt apps/t6.efi.
    114  1.1.1.1.6.2  yamt 
    115  1.1.1.1.6.2  yamt 
    116  1.1.1.1.6.2  yamt ** Running
    117  1.1.1.1.6.2  yamt 
    118  1.1.1.1.6.2  yamt Just copy the EFI application (e.g., apps/t6.efi) to the EFI
    119  1.1.1.1.6.2  yamt filesystem, boot EFI, and then select "Invoke EFI application" to run
    120  1.1.1.1.6.2  yamt the application you want to test.  Alternatively, you can invoke the
    121  1.1.1.1.6.2  yamt Intel-provided "nshell" application and then invoke your test binary
    122  1.1.1.1.6.2  yamt via the command line interface that "nshell" provides.
    123  1.1.1.1.6.2  yamt 
    124  1.1.1.1.6.2  yamt 
    125  1.1.1.1.6.2  yamt ** Writing Your Own EFI Application
    126  1.1.1.1.6.2  yamt 
    127  1.1.1.1.6.2  yamt Suppose you have your own EFI application in a file called
    128  1.1.1.1.6.2  yamt "apps/myefiapp.c".  To get this application built by the GNU EFI build
    129  1.1.1.1.6.2  yamt environment, simply add "myefiapp.efi" to macro TARGETS in
    130  1.1.1.1.6.2  yamt apps/Makefile.  Once this is done, invoke "make" in the top level
    131  1.1.1.1.6.2  yamt directory.  This should result in EFI application apps/myefiapp.efi,
    132  1.1.1.1.6.2  yamt ready for execution.
    133  1.1.1.1.6.2  yamt 
    134  1.1.1.1.6.2  yamt The GNU EFI build environment allows to write EFI applications as
    135  1.1.1.1.6.2  yamt described in Intel's EFI documentation, except for two differences:
    136  1.1.1.1.6.2  yamt 
    137  1.1.1.1.6.2  yamt  - The EFI application's entry point is always called "efi_main".  The
    138  1.1.1.1.6.2  yamt    declaration of this routine is:
    139  1.1.1.1.6.2  yamt 
    140  1.1.1.1.6.2  yamt     EFI_STATUS efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab);
    141  1.1.1.1.6.2  yamt 
    142  1.1.1.1.6.2  yamt  - UNICODE string literals must be written as W2U(L"Sample String")
    143  1.1.1.1.6.2  yamt    instead of just L"Sample String".  The W2U() macro is defined in
    144  1.1.1.1.6.2  yamt    <efilib.h>.  This header file also declares the function W2UCpy()
    145  1.1.1.1.6.2  yamt    which allows to convert a wide string into a UNICODE string and
    146  1.1.1.1.6.2  yamt    store the result in a programmer-supplied buffer.
    147  1.1.1.1.6.2  yamt 
    148  1.1.1.1.6.2  yamt  - Calls to EFI services should be made via uefi_call_wrapper(). This
    149  1.1.1.1.6.2  yamt    ensures appropriate parameter passing for the architecture.
    150  1.1.1.1.6.2  yamt 
    151  1.1.1.1.6.2  yamt 
    152  1.1.1.1.6.2  yamt * Part 2: Inner Workings
    153  1.1.1.1.6.2  yamt 
    154  1.1.1.1.6.2  yamt WARNING: This part contains all the gory detail of how the GNU EFI
    155  1.1.1.1.6.2  yamt toolchain works.  Normal users do not have to worry about such
    156  1.1.1.1.6.2  yamt details.  Reading this part incurs a definite risk of inducing severe
    157  1.1.1.1.6.2  yamt headaches or other maladies.
    158  1.1.1.1.6.2  yamt 
    159  1.1.1.1.6.2  yamt The basic idea behind the GNU EFI build environment is to use the GNU
    160  1.1.1.1.6.2  yamt toolchain to build a normal ELF binary that, at the end, is converted
    161  1.1.1.1.6.2  yamt to an EFI binary.  EFI binaries are really just PE32+ binaries.  PE
    162  1.1.1.1.6.2  yamt stands for "Portable Executable" and is the object file format
    163  1.1.1.1.6.2  yamt Microsoft is using on its Windows platforms.  PE is basically the COFF
    164  1.1.1.1.6.2  yamt object file format with an MS-DOS2.0 compatible header slapped on in
    165  1.1.1.1.6.2  yamt front of it.  The "32" in PE32+ stands for 32 bits, meaning that PE32
    166  1.1.1.1.6.2  yamt is a 32-bit object file format.  The plus in "PE32+" indicates that
    167  1.1.1.1.6.2  yamt this format has been hacked to allow loading a 4GB binary anywhere in
    168  1.1.1.1.6.2  yamt a 64-bit address space (unlike ELF64, however, this is not a full
    169  1.1.1.1.6.2  yamt 64-bit object file format because the entire binary cannot span more
    170  1.1.1.1.6.2  yamt than 4GB of address space).  EFI binaries are plain PE32+ binaries
    171  1.1.1.1.6.2  yamt except that the "subsystem id" differs from normal Windows binaries.
    172  1.1.1.1.6.2  yamt There are two flavors of EFI binaries: "applications" and "drivers"
    173  1.1.1.1.6.2  yamt and each has there own subsystem id and are identical otherwise.  At
    174  1.1.1.1.6.2  yamt present, the GNU EFI build environment supports the building of EFI
    175  1.1.1.1.6.2  yamt applications only, though it would be trivial to generate drivers, as
    176  1.1.1.1.6.2  yamt the only difference is the subsystem id.  For more details on PE32+,
    177  1.1.1.1.6.2  yamt see the spec at
    178  1.1.1.1.6.2  yamt 
    179  1.1.1.1.6.2  yamt 	http://msdn.microsoft.com/library/specs/msdn_pecoff.htm.
    180  1.1.1.1.6.2  yamt 
    181  1.1.1.1.6.2  yamt In theory, converting a suitable ELF64 binary to PE32+ is easy and
    182  1.1.1.1.6.2  yamt could be accomplished with the "objcopy" utility by specifying option
    183  1.1.1.1.6.2  yamt --target=efi-app-ia32 (x86) or --target=efi-app-ia64 (IA-64).  But
    184  1.1.1.1.6.2  yamt life never is that easy, so here some complicating factors:
    185  1.1.1.1.6.2  yamt 
    186  1.1.1.1.6.2  yamt  (1) COFF sections are very different from ELF sections.
    187  1.1.1.1.6.2  yamt 
    188  1.1.1.1.6.2  yamt 	ELF binaries distinguish between program headers and sections.
    189  1.1.1.1.6.2  yamt 	The program headers describe the memory segments that need to
    190  1.1.1.1.6.2  yamt 	be loaded/initialized, whereas the sections describe what
    191  1.1.1.1.6.2  yamt 	constitutes those segments.  In COFF (and therefore PE32+) no
    192  1.1.1.1.6.2  yamt 	such distinction is made.  Thus, COFF sections need to be page
    193  1.1.1.1.6.2  yamt 	aligned and have a size that is a multiple of the page size
    194  1.1.1.1.6.2  yamt 	(4KB for EFI), whereas ELF allows sections at arbitrary
    195  1.1.1.1.6.2  yamt 	addresses and with arbitrary sizes.
    196  1.1.1.1.6.2  yamt 
    197  1.1.1.1.6.2  yamt  (2) EFI binaries should be relocatable.
    198  1.1.1.1.6.2  yamt 
    199  1.1.1.1.6.2  yamt 	Since EFI binaries are executed in physical mode, EFI cannot
    200  1.1.1.1.6.2  yamt 	guarantee that a given binary can be loaded at its preferred
    201  1.1.1.1.6.2  yamt 	address.  EFI does _try_ to load a binary at it's preferred
    202  1.1.1.1.6.2  yamt 	address, but if it can't do so, it will load it at another
    203  1.1.1.1.6.2  yamt 	address and then relocate the binary using the contents of the
    204  1.1.1.1.6.2  yamt 	.reloc section.
    205  1.1.1.1.6.2  yamt 
    206  1.1.1.1.6.2  yamt  (3) On IA-64, the EFI entry point needs to point to a function
    207  1.1.1.1.6.2  yamt      descriptor, not to the code address of the entry point.
    208  1.1.1.1.6.2  yamt 
    209  1.1.1.1.6.2  yamt  (4) The EFI specification assumes that wide characters use UNICODE
    210  1.1.1.1.6.2  yamt      encoding.
    211  1.1.1.1.6.2  yamt 
    212  1.1.1.1.6.2  yamt 	ANSI C does not specify the size or encoding that a wide
    213  1.1.1.1.6.2  yamt 	character uses.  These choices are "implementation defined".
    214  1.1.1.1.6.2  yamt 	On most UNIX systems, the GNU toolchain uses a wchar_t that is
    215  1.1.1.1.6.2  yamt 	4 bytes in size.  The encoding used for such characters is
    216  1.1.1.1.6.2  yamt 	(mostly) UCS4.
    217  1.1.1.1.6.2  yamt 
    218  1.1.1.1.6.2  yamt In the following sections, we address how the GNU EFI build
    219  1.1.1.1.6.2  yamt environment addresses each of these issues.
    220  1.1.1.1.6.2  yamt 
    221  1.1.1.1.6.2  yamt 
    222  1.1.1.1.6.2  yamt ** (1) Accommodating COFF Sections
    223  1.1.1.1.6.2  yamt 
    224  1.1.1.1.6.2  yamt In order to satisfy the COFF constraint of page-sized and page-aligned
    225  1.1.1.1.6.2  yamt sections, the GNU EFI build environment uses the special linker script
    226  1.1.1.1.6.2  yamt in gnuefi/elf_$(ARCH)_efi.lds where $(ARCH) is the target architecture
    227  1.1.1.1.6.2  yamt ("ia32" for x86, "x86_64" for x86_64 and "ia64" for IA-64).
    228  1.1.1.1.6.2  yamt This script is set up to create only eight COFF section, each page aligned
    229  1.1.1.1.6.2  yamt and page sized.These eight sections are used to group together the much
    230  1.1.1.1.6.2  yamt greater number of sections that are typically present in ELF object files.
    231  1.1.1.1.6.2  yamt Specifically:
    232  1.1.1.1.6.2  yamt 
    233  1.1.1.1.6.2  yamt  .hash
    234  1.1.1.1.6.2  yamt 	Collects the ELF .hash info (this section _must_ be the first
    235  1.1.1.1.6.2  yamt 	section in order to build a shared object file; the section is
    236  1.1.1.1.6.2  yamt 	not actually loaded or used at runtime).
    237  1.1.1.1.6.2  yamt 
    238  1.1.1.1.6.2  yamt  .text
    239  1.1.1.1.6.2  yamt 	Collects all sections containing executable code.
    240  1.1.1.1.6.2  yamt 
    241  1.1.1.1.6.2  yamt  .data
    242  1.1.1.1.6.2  yamt 	Collects read-only and read-write data, literal string data,
    243  1.1.1.1.6.2  yamt 	global offset tables, the uninitialized data segment (bss) and
    244  1.1.1.1.6.2  yamt 	various other sections containing data.
    245  1.1.1.1.6.2  yamt 
    246  1.1.1.1.6.2  yamt 	The reason read-only data is placed here instead of the in
    247  1.1.1.1.6.2  yamt 	.text is to make it possible to disassemble the .text section
    248  1.1.1.1.6.2  yamt 	without getting garbage due to read-only data.  Besides, since
    249  1.1.1.1.6.2  yamt 	EFI binaries execute in physical mode, differences in page
    250  1.1.1.1.6.2  yamt 	protection do not matter.
    251  1.1.1.1.6.2  yamt 
    252  1.1.1.1.6.2  yamt 	The reason the uninitialized data is placed in this section is
    253  1.1.1.1.6.2  yamt 	that the EFI loader appears to be unable to handle sections
    254  1.1.1.1.6.2  yamt 	that are allocated but not loaded from the binary.
    255  1.1.1.1.6.2  yamt 
    256  1.1.1.1.6.2  yamt  .dynamic, .dynsym, .rela, .rel, .reloc
    257  1.1.1.1.6.2  yamt 	These sections contains the dynamic information necessary to
    258  1.1.1.1.6.2  yamt 	self-relocate the binary (see below).
    259  1.1.1.1.6.2  yamt 
    260  1.1.1.1.6.2  yamt A couple of more points worth noting about the linker script:
    261  1.1.1.1.6.2  yamt 
    262  1.1.1.1.6.2  yamt  o On IA-64, the global pointer symbol (__gp) needs to be placed such
    263  1.1.1.1.6.2  yamt    that the _entire_ EFI binary can be addressed using the signed
    264  1.1.1.1.6.2  yamt    22-bit offset that the "addl" instruction affords.  Specifically,
    265  1.1.1.1.6.2  yamt    this means that __gp should be placed at ImageBase + 0x200000.
    266  1.1.1.1.6.2  yamt    Strictly speaking, only a couple of symbols need to be addressable
    267  1.1.1.1.6.2  yamt    in this fashion, so with some care it should be possible to build
    268  1.1.1.1.6.2  yamt    binaries much larger than 4MB.  To get a list of symbols that need
    269  1.1.1.1.6.2  yamt    to be addressable in this fashion, grep the assembly files in
    270  1.1.1.1.6.2  yamt    directory gnuefi for the string "@gprel".
    271  1.1.1.1.6.2  yamt 
    272  1.1.1.1.6.2  yamt  o The link address (ImageBase) of the binary is (arbitrarily) set to
    273  1.1.1.1.6.2  yamt    zero.  This could be set to something larger to increase the chance
    274  1.1.1.1.6.2  yamt    of EFI being able to load the binary without requiring relocation.
    275  1.1.1.1.6.2  yamt    However, a start address of 0 makes debugging a wee bit easier
    276  1.1.1.1.6.2  yamt    (great for those of us who can add, but not subtract... ;-).
    277  1.1.1.1.6.2  yamt 
    278  1.1.1.1.6.2  yamt  o The relocation related sections (.dynamic, .rel, .rela, .reloc)
    279  1.1.1.1.6.2  yamt    cannot be placed inside .data because some tools in the GNU
    280  1.1.1.1.6.2  yamt    toolchain rely on the existence of these sections.
    281  1.1.1.1.6.2  yamt 
    282  1.1.1.1.6.2  yamt  o Some sections in the ELF binary intentionally get dropped when
    283  1.1.1.1.6.2  yamt    building the EFI binary.  Particularly noteworthy are the dynamic
    284  1.1.1.1.6.2  yamt    relocation sections for the .plabel and .reloc sections.  It would
    285  1.1.1.1.6.2  yamt    be _wrong_ to include these sections in the EFI binary because it
    286  1.1.1.1.6.2  yamt    would result in .reloc and .plabel being relocated twice (once by
    287  1.1.1.1.6.2  yamt    the EFI loader and once by the self-relocator; see below for a
    288  1.1.1.1.6.2  yamt    description of the latter).  Specifically, only the sections
    289  1.1.1.1.6.2  yamt    mentioned with the -j option in the final "objcopy" command are
    290  1.1.1.1.6.2  yamt    retained in the EFI binary (see apps/Makefile).
    291  1.1.1.1.6.2  yamt 
    292  1.1.1.1.6.2  yamt 
    293  1.1.1.1.6.2  yamt ** (2) Building Relocatable Binaries
    294  1.1.1.1.6.2  yamt 
    295  1.1.1.1.6.2  yamt ELF binaries are normally linked for a fixed load address and are thus
    296  1.1.1.1.6.2  yamt not relocatable.  The only kind of ELF object that is relocatable are
    297  1.1.1.1.6.2  yamt shared objects ("shared libraries").  However, even those objects are
    298  1.1.1.1.6.2  yamt usually not completely position independent and therefore require
    299  1.1.1.1.6.2  yamt runtime relocation by the dynamic loader.  For example, IA-64 binaries
    300  1.1.1.1.6.2  yamt normally require relocation of the global offset table.
    301  1.1.1.1.6.2  yamt 
    302  1.1.1.1.6.2  yamt The approach to building relocatable binaries in the GNU EFI build
    303  1.1.1.1.6.2  yamt environment is to:
    304  1.1.1.1.6.2  yamt 
    305  1.1.1.1.6.2  yamt  (a) build an ELF shared object
    306  1.1.1.1.6.2  yamt 
    307  1.1.1.1.6.2  yamt  (b) link it together with a self-relocator that takes care of
    308  1.1.1.1.6.2  yamt      applying the dynamic relocations that may be present in the
    309  1.1.1.1.6.2  yamt      ELF shared object
    310  1.1.1.1.6.2  yamt 
    311  1.1.1.1.6.2  yamt  (c) convert the resulting image to an EFI binary
    312  1.1.1.1.6.2  yamt 
    313  1.1.1.1.6.2  yamt The self-relocator is of course architecture dependent.  The x86
    314  1.1.1.1.6.2  yamt version can be found in gnuefi/reloc_ia32.c, the x86_64 version
    315  1.1.1.1.6.2  yamt can be found in gnuefi/reloc_x86_64.c and the IA-64 version can be
    316  1.1.1.1.6.2  yamt found in gnuefi/reloc_ia64.S.
    317  1.1.1.1.6.2  yamt 
    318  1.1.1.1.6.2  yamt The self-relocator operates as follows: the startup code invokes it
    319  1.1.1.1.6.2  yamt right after EFI has handed off control to the EFI binary at symbol
    320  1.1.1.1.6.2  yamt "_start".  Upon activation, the self-relocator searches the .dynamic
    321  1.1.1.1.6.2  yamt section (whose starting address is given by symbol _DYNAMIC) for the
    322  1.1.1.1.6.2  yamt dynamic relocation information, which can be found in the DT_REL,
    323  1.1.1.1.6.2  yamt DT_RELSZ, and DT_RELENT entries of the dynamic table (DT_RELA,
    324  1.1.1.1.6.2  yamt DT_RELASZ, and DT_RELAENT in the case of rela relocations, as is the
    325  1.1.1.1.6.2  yamt case for IA-64).  The dynamic relocation information points to the ELF
    326  1.1.1.1.6.2  yamt relocation table.  Once this table is found, the self-relocator walks
    327  1.1.1.1.6.2  yamt through it, applying each relocation one by one.  Since the EFI
    328  1.1.1.1.6.2  yamt binaries are fully resolved shared objects, only a subset of all
    329  1.1.1.1.6.2  yamt possible relocations need to be supported.  Specifically, on x86 only
    330  1.1.1.1.6.2  yamt the R_386_RELATIVE relocation is needed.  On IA-64, the relocations
    331  1.1.1.1.6.2  yamt R_IA64_DIR64LSB, R_IA64_REL64LSB, and R_IA64_FPTR64LSB are needed.
    332  1.1.1.1.6.2  yamt Note that the R_IA64_FPTR64LSB relocation requires access to the
    333  1.1.1.1.6.2  yamt dynamic symbol table.  This is why the .dynsym section is included in
    334  1.1.1.1.6.2  yamt the EFI binary.  Another complication is that this relocation requires
    335  1.1.1.1.6.2  yamt memory to hold the function descriptors (aka "procedure labels" or
    336  1.1.1.1.6.2  yamt "plabels").  Each function descriptor uses 16 bytes of memory.  The
    337  1.1.1.1.6.2  yamt IA-64 self-relocator currently reserves a static memory area that can
    338  1.1.1.1.6.2  yamt hold 100 of these descriptors.  If the self-relocator runs out of
    339  1.1.1.1.6.2  yamt space, it causes the EFI binary to fail with error code 5
    340  1.1.1.1.6.2  yamt (EFI_BUFFER_TOO_SMALL).  When this happens, the manifest constant
    341  1.1.1.1.6.2  yamt MAX_FUNCTION_DESCRIPTORS in gnuefi/reloc_ia64.S should be increased
    342  1.1.1.1.6.2  yamt and the application recompiled.  An easy way to count the number of
    343  1.1.1.1.6.2  yamt function descriptors required by an EFI application is to run the
    344  1.1.1.1.6.2  yamt command:
    345  1.1.1.1.6.2  yamt 
    346  1.1.1.1.6.2  yamt   objdump --dynamic-reloc example.so | fgrep FPTR64 | wc -l
    347  1.1.1.1.6.2  yamt 
    348  1.1.1.1.6.2  yamt assuming "example" is the name of the desired EFI application.
    349  1.1.1.1.6.2  yamt 
    350  1.1.1.1.6.2  yamt 
    351  1.1.1.1.6.2  yamt ** (3) Creating the Function Descriptor for the IA-64 EFI Binaries
    352  1.1.1.1.6.2  yamt 
    353  1.1.1.1.6.2  yamt As mentioned above, the IA-64 PE32+ format assumes that the entry
    354  1.1.1.1.6.2  yamt point of the binary is a function descriptor.  A function descriptors
    355  1.1.1.1.6.2  yamt consists of two double words: the first one is the code entry point
    356  1.1.1.1.6.2  yamt and the second is the global pointer that should be loaded before
    357  1.1.1.1.6.2  yamt calling the entry point.  Since the ELF toolchain doesn't know how to
    358  1.1.1.1.6.2  yamt generate a function descriptor for the entry point, the startup code
    359  1.1.1.1.6.2  yamt in gnuefi/crt0-efi-ia64.S crafts one manually by with the code:
    360  1.1.1.1.6.2  yamt 
    361  1.1.1.1.6.2  yamt 	        .section .plabel, "a"
    362  1.1.1.1.6.2  yamt 	_start_plabel:
    363  1.1.1.1.6.2  yamt 	        data8   _start
    364  1.1.1.1.6.2  yamt 	        data8   __gp
    365  1.1.1.1.6.2  yamt 
    366  1.1.1.1.6.2  yamt this places the procedure label for entry point _start in a section
    367  1.1.1.1.6.2  yamt called ".plabel".  Now, the only problem is that _start and __gp need
    368  1.1.1.1.6.2  yamt to be relocated _before_ EFI hands control over to the EFI binary.
    369  1.1.1.1.6.2  yamt Fortunately, PE32+ defines a section called ".reloc" that can achieve
    370  1.1.1.1.6.2  yamt this.  Thus, in addition to manually crafting the function descriptor,
    371  1.1.1.1.6.2  yamt the startup code also crafts a ".reloc" section that has will cause
    372  1.1.1.1.6.2  yamt the EFI loader to relocate the function descriptor before handing over
    373  1.1.1.1.6.2  yamt control to the EFI binary (again, see the PECOFF spec mentioned above
    374  1.1.1.1.6.2  yamt for details).
    375  1.1.1.1.6.2  yamt 
    376  1.1.1.1.6.2  yamt A final question may be why .plabel and .reloc need to go in their own
    377  1.1.1.1.6.2  yamt COFF sections.  The answer is simply: we need to be able to discard
    378  1.1.1.1.6.2  yamt the relocation entries that are generated for these sections.  By
    379  1.1.1.1.6.2  yamt placing them in these sections, the relocations end up in sections
    380  1.1.1.1.6.2  yamt ".rela.plabel" and ".rela.reloc" which makes it easy to filter them
    381  1.1.1.1.6.2  yamt out in the filter script.  Also, the ".reloc" section needs to be in
    382  1.1.1.1.6.2  yamt its own section so that the objcopy program can recognize it and can
    383  1.1.1.1.6.2  yamt create the correct directory entries in the PE32+ binary.
    384  1.1.1.1.6.2  yamt 
    385  1.1.1.1.6.2  yamt 
    386  1.1.1.1.6.2  yamt ** (4) Convenient and Portable Generation of UNICODE String Literals
    387  1.1.1.1.6.2  yamt 
    388  1.1.1.1.6.2  yamt As of gnu-efi-3.0, we make use (and somewhat abuse) the gcc option
    389  1.1.1.1.6.2  yamt that forces wide characters (WCHAR_T) to use short integers (2 bytes) 
    390  1.1.1.1.6.2  yamt instead of integers (4 bytes). This way we match the Unicode character
    391  1.1.1.1.6.2  yamt size. By abuse, we mean that we rely on the fact that the regular ASCII
    392  1.1.1.1.6.2  yamt characters are encoded the same way between (short) wide characters 
    393  1.1.1.1.6.2  yamt and Unicode and basically only use the first byte. This allows us
    394  1.1.1.1.6.2  yamt to just use them interchangeably.
    395  1.1.1.1.6.2  yamt 
    396  1.1.1.1.6.2  yamt The gcc option to force short wide characters is : -fshort-wchar
    397  1.1.1.1.6.2  yamt 
    398  1.1.1.1.6.2  yamt 			* * * The End * * *
    399