1 /* Support for the generic parts of PE/PEI; common header information. 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 3 2006, 2007, 2008 Free Software Foundation, Inc. 4 Written by Cygnus Solutions. 5 6 This file is part of BFD, the Binary File Descriptor library. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 MA 02110-1301, USA. */ 22 23 24 /* Most of this hacked by Steve Chamberlain, 25 sac@cygnus.com 26 27 PE/PEI rearrangement (and code added): Donn Terry 28 Softway Systems, Inc. */ 29 30 /* Hey look, some documentation [and in a place you expect to find it]! 31 32 The main reference for the pei format is "Microsoft Portable Executable 33 and Common Object File Format Specification 4.1". Get it if you need to 34 do some serious hacking on this code. 35 36 Another reference: 37 "Peering Inside the PE: A Tour of the Win32 Portable Executable 38 File Format", MSJ 1994, Volume 9. 39 40 The *sole* difference between the pe format and the pei format is that the 41 latter has an MSDOS 2.0 .exe header on the front that prints the message 42 "This app must be run under Windows." (or some such). 43 (FIXME: Whether that statement is *really* true or not is unknown. 44 Are there more subtle differences between pe and pei formats? 45 For now assume there aren't. If you find one, then for God sakes 46 document it here!) 47 48 The Microsoft docs use the word "image" instead of "executable" because 49 the former can also refer to a DLL (shared library). Confusion can arise 50 because the `i' in `pei' also refers to "image". The `pe' format can 51 also create images (i.e. executables), it's just that to run on a win32 52 system you need to use the pei format. 53 54 FIXME: Please add more docs here so the next poor fool that has to hack 55 on this code has a chance of getting something accomplished without 56 wasting too much time. */ 57 58 #ifndef GET_FCN_LNNOPTR 59 #define GET_FCN_LNNOPTR(abfd, ext) \ 60 H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr) 61 #endif 62 63 #ifndef GET_FCN_ENDNDX 64 #define GET_FCN_ENDNDX(abfd, ext) \ 65 H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx) 66 #endif 67 68 #ifndef PUT_FCN_LNNOPTR 69 #define PUT_FCN_LNNOPTR(abfd, in, ext) \ 70 H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr) 71 #endif 72 #ifndef PUT_FCN_ENDNDX 73 #define PUT_FCN_ENDNDX(abfd, in, ext) \ 74 H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_endndx) 75 #endif 76 #ifndef GET_LNSZ_LNNO 77 #define GET_LNSZ_LNNO(abfd, ext) \ 78 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno) 79 #endif 80 #ifndef GET_LNSZ_SIZE 81 #define GET_LNSZ_SIZE(abfd, ext) \ 82 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size) 83 #endif 84 #ifndef PUT_LNSZ_LNNO 85 #define PUT_LNSZ_LNNO(abfd, in, ext) \ 86 H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_lnno) 87 #endif 88 #ifndef PUT_LNSZ_SIZE 89 #define PUT_LNSZ_SIZE(abfd, in, ext) \ 90 H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_size) 91 #endif 92 #ifndef GET_SCN_SCNLEN 93 #define GET_SCN_SCNLEN(abfd, ext) \ 94 H_GET_32 (abfd, ext->x_scn.x_scnlen) 95 #endif 96 #ifndef GET_SCN_NRELOC 97 #define GET_SCN_NRELOC(abfd, ext) \ 98 H_GET_16 (abfd, ext->x_scn.x_nreloc) 99 #endif 100 #ifndef GET_SCN_NLINNO 101 #define GET_SCN_NLINNO(abfd, ext) \ 102 H_GET_16 (abfd, ext->x_scn.x_nlinno) 103 #endif 104 #ifndef PUT_SCN_SCNLEN 105 #define PUT_SCN_SCNLEN(abfd, in, ext) \ 106 H_PUT_32(abfd, in, ext->x_scn.x_scnlen) 107 #endif 108 #ifndef PUT_SCN_NRELOC 109 #define PUT_SCN_NRELOC(abfd, in, ext) \ 110 H_PUT_16(abfd, in, ext->x_scn.x_nreloc) 111 #endif 112 #ifndef PUT_SCN_NLINNO 113 #define PUT_SCN_NLINNO(abfd, in, ext) \ 114 H_PUT_16(abfd,in, ext->x_scn.x_nlinno) 115 #endif 116 #ifndef GET_LINENO_LNNO 117 #define GET_LINENO_LNNO(abfd, ext) \ 118 H_GET_16 (abfd, ext->l_lnno); 119 #endif 120 #ifndef PUT_LINENO_LNNO 121 #define PUT_LINENO_LNNO(abfd, val, ext) \ 122 H_PUT_16(abfd,val, ext->l_lnno); 123 #endif 124 125 /* The f_symptr field in the filehdr is sometimes 64 bits. */ 126 #ifndef GET_FILEHDR_SYMPTR 127 #define GET_FILEHDR_SYMPTR H_GET_32 128 #endif 129 #ifndef PUT_FILEHDR_SYMPTR 130 #define PUT_FILEHDR_SYMPTR H_PUT_32 131 #endif 132 133 /* Some fields in the aouthdr are sometimes 64 bits. */ 134 #ifndef GET_AOUTHDR_TSIZE 135 #define GET_AOUTHDR_TSIZE H_GET_32 136 #endif 137 #ifndef PUT_AOUTHDR_TSIZE 138 #define PUT_AOUTHDR_TSIZE H_PUT_32 139 #endif 140 #ifndef GET_AOUTHDR_DSIZE 141 #define GET_AOUTHDR_DSIZE H_GET_32 142 #endif 143 #ifndef PUT_AOUTHDR_DSIZE 144 #define PUT_AOUTHDR_DSIZE H_PUT_32 145 #endif 146 #ifndef GET_AOUTHDR_BSIZE 147 #define GET_AOUTHDR_BSIZE H_GET_32 148 #endif 149 #ifndef PUT_AOUTHDR_BSIZE 150 #define PUT_AOUTHDR_BSIZE H_PUT_32 151 #endif 152 #ifndef GET_AOUTHDR_ENTRY 153 #define GET_AOUTHDR_ENTRY H_GET_32 154 #endif 155 #ifndef PUT_AOUTHDR_ENTRY 156 #define PUT_AOUTHDR_ENTRY H_PUT_32 157 #endif 158 #ifndef GET_AOUTHDR_TEXT_START 159 #define GET_AOUTHDR_TEXT_START H_GET_32 160 #endif 161 #ifndef PUT_AOUTHDR_TEXT_START 162 #define PUT_AOUTHDR_TEXT_START H_PUT_32 163 #endif 164 #ifndef GET_AOUTHDR_DATA_START 165 #define GET_AOUTHDR_DATA_START H_GET_32 166 #endif 167 #ifndef PUT_AOUTHDR_DATA_START 168 #define PUT_AOUTHDR_DATA_START H_PUT_32 169 #endif 170 171 /* Some fields in the scnhdr are sometimes 64 bits. */ 172 #ifndef GET_SCNHDR_PADDR 173 #define GET_SCNHDR_PADDR H_GET_32 174 #endif 175 #ifndef PUT_SCNHDR_PADDR 176 #define PUT_SCNHDR_PADDR H_PUT_32 177 #endif 178 #ifndef GET_SCNHDR_VADDR 179 #define GET_SCNHDR_VADDR H_GET_32 180 #endif 181 #ifndef PUT_SCNHDR_VADDR 182 #define PUT_SCNHDR_VADDR H_PUT_32 183 #endif 184 #ifndef GET_SCNHDR_SIZE 185 #define GET_SCNHDR_SIZE H_GET_32 186 #endif 187 #ifndef PUT_SCNHDR_SIZE 188 #define PUT_SCNHDR_SIZE H_PUT_32 189 #endif 190 #ifndef GET_SCNHDR_SCNPTR 191 #define GET_SCNHDR_SCNPTR H_GET_32 192 #endif 193 #ifndef PUT_SCNHDR_SCNPTR 194 #define PUT_SCNHDR_SCNPTR H_PUT_32 195 #endif 196 #ifndef GET_SCNHDR_RELPTR 197 #define GET_SCNHDR_RELPTR H_GET_32 198 #endif 199 #ifndef PUT_SCNHDR_RELPTR 200 #define PUT_SCNHDR_RELPTR H_PUT_32 201 #endif 202 #ifndef GET_SCNHDR_LNNOPTR 203 #define GET_SCNHDR_LNNOPTR H_GET_32 204 #endif 205 #ifndef PUT_SCNHDR_LNNOPTR 206 #define PUT_SCNHDR_LNNOPTR H_PUT_32 207 #endif 208 209 #ifdef COFF_WITH_pex64 210 211 #define GET_OPTHDR_IMAGE_BASE H_GET_64 212 #define PUT_OPTHDR_IMAGE_BASE H_PUT_64 213 #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64 214 #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64 215 #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_64 216 #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_64 217 #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_64 218 #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_64 219 #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_64 220 #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_64 221 #define GET_PDATA_ENTRY bfd_get_32 222 223 #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pex64_bfd_copy_private_bfd_data_common 224 #define _bfd_XX_bfd_copy_private_section_data _bfd_pex64_bfd_copy_private_section_data 225 #define _bfd_XX_get_symbol_info _bfd_pex64_get_symbol_info 226 #define _bfd_XX_only_swap_filehdr_out _bfd_pex64_only_swap_filehdr_out 227 #define _bfd_XX_print_private_bfd_data_common _bfd_pex64_print_private_bfd_data_common 228 #define _bfd_XXi_final_link_postscript _bfd_pex64i_final_link_postscript 229 #define _bfd_XXi_only_swap_filehdr_out _bfd_pex64i_only_swap_filehdr_out 230 #define _bfd_XXi_swap_aouthdr_in _bfd_pex64i_swap_aouthdr_in 231 #define _bfd_XXi_swap_aouthdr_out _bfd_pex64i_swap_aouthdr_out 232 #define _bfd_XXi_swap_aux_in _bfd_pex64i_swap_aux_in 233 #define _bfd_XXi_swap_aux_out _bfd_pex64i_swap_aux_out 234 #define _bfd_XXi_swap_lineno_in _bfd_pex64i_swap_lineno_in 235 #define _bfd_XXi_swap_lineno_out _bfd_pex64i_swap_lineno_out 236 #define _bfd_XXi_swap_scnhdr_out _bfd_pex64i_swap_scnhdr_out 237 #define _bfd_XXi_swap_sym_in _bfd_pex64i_swap_sym_in 238 #define _bfd_XXi_swap_sym_out _bfd_pex64i_swap_sym_out 239 240 #elif defined COFF_WITH_pep 241 242 #define GET_OPTHDR_IMAGE_BASE H_GET_64 243 #define PUT_OPTHDR_IMAGE_BASE H_PUT_64 244 #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64 245 #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64 246 #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_64 247 #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_64 248 #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_64 249 #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_64 250 #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_64 251 #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_64 252 #define GET_PDATA_ENTRY bfd_get_64 253 254 #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pep_bfd_copy_private_bfd_data_common 255 #define _bfd_XX_bfd_copy_private_section_data _bfd_pep_bfd_copy_private_section_data 256 #define _bfd_XX_get_symbol_info _bfd_pep_get_symbol_info 257 #define _bfd_XX_only_swap_filehdr_out _bfd_pep_only_swap_filehdr_out 258 #define _bfd_XX_print_private_bfd_data_common _bfd_pep_print_private_bfd_data_common 259 #define _bfd_XXi_final_link_postscript _bfd_pepi_final_link_postscript 260 #define _bfd_XXi_only_swap_filehdr_out _bfd_pepi_only_swap_filehdr_out 261 #define _bfd_XXi_swap_aouthdr_in _bfd_pepi_swap_aouthdr_in 262 #define _bfd_XXi_swap_aouthdr_out _bfd_pepi_swap_aouthdr_out 263 #define _bfd_XXi_swap_aux_in _bfd_pepi_swap_aux_in 264 #define _bfd_XXi_swap_aux_out _bfd_pepi_swap_aux_out 265 #define _bfd_XXi_swap_lineno_in _bfd_pepi_swap_lineno_in 266 #define _bfd_XXi_swap_lineno_out _bfd_pepi_swap_lineno_out 267 #define _bfd_XXi_swap_scnhdr_out _bfd_pepi_swap_scnhdr_out 268 #define _bfd_XXi_swap_sym_in _bfd_pepi_swap_sym_in 269 #define _bfd_XXi_swap_sym_out _bfd_pepi_swap_sym_out 270 271 #else /* !COFF_WITH_pep */ 272 273 #define GET_OPTHDR_IMAGE_BASE H_GET_32 274 #define PUT_OPTHDR_IMAGE_BASE H_PUT_32 275 #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_32 276 #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_32 277 #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_32 278 #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_32 279 #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_32 280 #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_32 281 #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_32 282 #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_32 283 #define GET_PDATA_ENTRY bfd_get_32 284 285 #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pe_bfd_copy_private_bfd_data_common 286 #define _bfd_XX_bfd_copy_private_section_data _bfd_pe_bfd_copy_private_section_data 287 #define _bfd_XX_get_symbol_info _bfd_pe_get_symbol_info 288 #define _bfd_XX_only_swap_filehdr_out _bfd_pe_only_swap_filehdr_out 289 #define _bfd_XX_print_private_bfd_data_common _bfd_pe_print_private_bfd_data_common 290 #define _bfd_XXi_final_link_postscript _bfd_pei_final_link_postscript 291 #define _bfd_XXi_only_swap_filehdr_out _bfd_pei_only_swap_filehdr_out 292 #define _bfd_XXi_swap_aouthdr_in _bfd_pei_swap_aouthdr_in 293 #define _bfd_XXi_swap_aouthdr_out _bfd_pei_swap_aouthdr_out 294 #define _bfd_XXi_swap_aux_in _bfd_pei_swap_aux_in 295 #define _bfd_XXi_swap_aux_out _bfd_pei_swap_aux_out 296 #define _bfd_XXi_swap_lineno_in _bfd_pei_swap_lineno_in 297 #define _bfd_XXi_swap_lineno_out _bfd_pei_swap_lineno_out 298 #define _bfd_XXi_swap_scnhdr_out _bfd_pei_swap_scnhdr_out 299 #define _bfd_XXi_swap_sym_in _bfd_pei_swap_sym_in 300 #define _bfd_XXi_swap_sym_out _bfd_pei_swap_sym_out 301 302 #endif /* !COFF_WITH_pep */ 303 304 /* Returns true if the target is a PE executable target. */ 305 #define bfd_target_pei_p(xvec) \ 306 (CONST_STRNEQ ((xvec)->name, "pei-")) 307 308 /* Return the arch string of a PE executable target. */ 309 #define bfd_target_pei_arch(xvec) \ 310 ((xvec)->name + sizeof ("pei-") - 1) 311 312 /* Returns true if the target is an EFI application target. */ 313 #define bfd_target_efi_app_p(xvec) \ 314 (CONST_STRNEQ ((xvec)->name, "efi-app-")) 315 316 /* Return the arch string of an EFI application target. */ 317 #define bfd_target_efi_app_arch(xvec) \ 318 ((xvec)->name + sizeof ("efi-app-") - 1) 319 320 /* Returns true if the target is an EFI Boot Service driver target. */ 321 #define bfd_target_efi_bsdrv_p(xvec) \ 322 (CONST_STRNEQ ((xvec)->name, "efi-bsdrv-")) 323 324 /* Return the arch string of an EFI Boot Service driver target. */ 325 #define bfd_target_efi_bsdrv_arch(xvec) \ 326 ((xvec)->name + sizeof ("efi-bsdrv-") - 1) 327 328 /* Returns true if the target is an EFI runtime driver target. */ 329 #define bfd_target_efi_rtdrv_p(xvec) \ 330 (CONST_STRNEQ ((xvec)->name, "efi-rtdrv-")) 331 332 /* Return the arch string of an EFI runtime driver target. */ 333 #define bfd_target_efi_rtdrv_arch(xvec) \ 334 ((xvec)->name + sizeof ("efi-rtdrv-") - 1) 335 336 /* Macro: Returns true if the bfd is a PE executable as opposed to a 337 PE object file. */ 338 #define bfd_pe_executable_p(abfd) \ 339 ( bfd_target_pei_p ((abfd)->xvec) \ 340 || bfd_target_efi_app_p ((abfd)->xvec) \ 341 || bfd_target_efi_bsdrv_p ((abfd)->xvec) \ 342 || bfd_target_efi_rtdrv_p ((abfd)->xvec)) 343 344 /* These functions are architecture dependent, and are in peicode.h: 345 coff_swap_reloc_in 346 int coff_swap_reloc_out 347 coff_swap_filehdr_in 348 coff_swap_scnhdr_in 349 pe_mkobject 350 pe_mkobject_hook */ 351 352 /* The functions described below are common across all PE/PEI 353 implementations architecture types, and actually appear in 354 peigen.c. */ 355 356 #define coff_swap_sym_in _bfd_XXi_swap_sym_in 357 #define coff_swap_sym_out _bfd_XXi_swap_sym_out 358 #define coff_swap_aux_in _bfd_XXi_swap_aux_in 359 #define coff_swap_aux_out _bfd_XXi_swap_aux_out 360 #define coff_swap_lineno_in _bfd_XXi_swap_lineno_in 361 #define coff_swap_lineno_out _bfd_XXi_swap_lineno_out 362 #define coff_swap_aouthdr_in _bfd_XXi_swap_aouthdr_in 363 #define coff_swap_aouthdr_out _bfd_XXi_swap_aouthdr_out 364 #define coff_swap_scnhdr_out _bfd_XXi_swap_scnhdr_out 365 366 #ifndef coff_final_link_postscript 367 #define coff_final_link_postscript _bfd_XXi_final_link_postscript 368 #endif 369 370 void _bfd_XXi_swap_sym_in (bfd *, void *, void *); 371 unsigned _bfd_XXi_swap_sym_out (bfd *, void *, void *); 372 void _bfd_XXi_swap_aux_in (bfd *, void *, int, int, int, int, void *); 373 unsigned _bfd_XXi_swap_aux_out (bfd *, void *, int, int, int, int, void *); 374 void _bfd_XXi_swap_lineno_in (bfd *, void *, void *); 375 unsigned _bfd_XXi_swap_lineno_out (bfd *, void *, void *); 376 void _bfd_XXi_swap_aouthdr_in (bfd *, void *, void *); 377 unsigned _bfd_XXi_swap_aouthdr_out (bfd *, void *, void *); 378 unsigned _bfd_XXi_swap_scnhdr_out (bfd *, void *, void *); 379 bfd_boolean _bfd_XX_print_private_bfd_data_common (bfd *, void *); 380 bfd_boolean _bfd_XX_bfd_copy_private_bfd_data_common (bfd *, bfd *); 381 void _bfd_XX_get_symbol_info (bfd *, asymbol *, symbol_info *); 382 bfd_boolean _bfd_XXi_final_link_postscript (bfd *, struct coff_final_link_info *); 383 384 /* The following are needed only for ONE of pe or pei, but don't 385 otherwise vary; peicode.h fixes up ifdefs but we provide the 386 prototype. */ 387 388 unsigned _bfd_XX_only_swap_filehdr_out (bfd *, void *, void *); 389 unsigned _bfd_XXi_only_swap_filehdr_out (bfd *, void *, void *); 390 bfd_boolean _bfd_XX_bfd_copy_private_section_data (bfd *, asection *, bfd *, asection *); 391 392 bfd_boolean _bfd_pe_print_ce_compressed_pdata (bfd *, void *); 393 bfd_boolean _bfd_pe64_print_ce_compressed_pdata (bfd *, void *); 394 bfd_boolean _bfd_pex64_print_ce_compressed_pdata (bfd *, void *); 395 bfd_boolean _bfd_pep_print_ce_compressed_pdata (bfd *, void *); 396 397