README.gnuefi 17 KB

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