Back to index

cell-binutils  2.17cvs20070401
libpei.h
Go to the documentation of this file.
00001 /* Support for the generic parts of PE/PEI; common header information.
00002    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006
00003    Free Software Foundation, Inc.
00004    Written by Cygnus Solutions.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* Most of this hacked by  Steve Chamberlain,
00023                      sac@cygnus.com
00024 
00025    PE/PEI rearrangement (and code added): Donn Terry
00026                                    Softway Systems, Inc.  */
00027 
00028 /* Hey look, some documentation [and in a place you expect to find it]!
00029 
00030    The main reference for the pei format is "Microsoft Portable Executable
00031    and Common Object File Format Specification 4.1".  Get it if you need to
00032    do some serious hacking on this code.
00033 
00034    Another reference:
00035    "Peering Inside the PE: A Tour of the Win32 Portable Executable
00036    File Format", MSJ 1994, Volume 9.
00037 
00038    The *sole* difference between the pe format and the pei format is that the
00039    latter has an MSDOS 2.0 .exe header on the front that prints the message
00040    "This app must be run under Windows." (or some such).
00041    (FIXME: Whether that statement is *really* true or not is unknown.
00042    Are there more subtle differences between pe and pei formats?
00043    For now assume there aren't.  If you find one, then for God sakes
00044    document it here!)
00045 
00046    The Microsoft docs use the word "image" instead of "executable" because
00047    the former can also refer to a DLL (shared library).  Confusion can arise
00048    because the `i' in `pei' also refers to "image".  The `pe' format can
00049    also create images (i.e. executables), it's just that to run on a win32
00050    system you need to use the pei format.
00051 
00052    FIXME: Please add more docs here so the next poor fool that has to hack
00053    on this code has a chance of getting something accomplished without
00054    wasting too much time.  */
00055 
00056 #ifndef GET_FCN_LNNOPTR
00057 #define GET_FCN_LNNOPTR(abfd, ext) \
00058   H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
00059 #endif
00060 
00061 #ifndef GET_FCN_ENDNDX
00062 #define GET_FCN_ENDNDX(abfd, ext) \
00063   H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx)
00064 #endif
00065 
00066 #ifndef PUT_FCN_LNNOPTR
00067 #define PUT_FCN_LNNOPTR(abfd, in, ext) \
00068   H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
00069 #endif
00070 #ifndef PUT_FCN_ENDNDX
00071 #define PUT_FCN_ENDNDX(abfd, in, ext) \
00072   H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_endndx)
00073 #endif
00074 #ifndef GET_LNSZ_LNNO
00075 #define GET_LNSZ_LNNO(abfd, ext) \
00076   H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno)
00077 #endif
00078 #ifndef GET_LNSZ_SIZE
00079 #define GET_LNSZ_SIZE(abfd, ext) \
00080   H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size)
00081 #endif
00082 #ifndef PUT_LNSZ_LNNO
00083 #define PUT_LNSZ_LNNO(abfd, in, ext) \
00084   H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_lnno)
00085 #endif
00086 #ifndef PUT_LNSZ_SIZE
00087 #define PUT_LNSZ_SIZE(abfd, in, ext) \
00088   H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_size)
00089 #endif
00090 #ifndef GET_SCN_SCNLEN
00091 #define GET_SCN_SCNLEN(abfd, ext) \
00092   H_GET_32 (abfd, ext->x_scn.x_scnlen)
00093 #endif
00094 #ifndef GET_SCN_NRELOC
00095 #define GET_SCN_NRELOC(abfd, ext) \
00096   H_GET_16 (abfd, ext->x_scn.x_nreloc)
00097 #endif
00098 #ifndef GET_SCN_NLINNO
00099 #define GET_SCN_NLINNO(abfd, ext) \
00100   H_GET_16 (abfd, ext->x_scn.x_nlinno)
00101 #endif
00102 #ifndef PUT_SCN_SCNLEN
00103 #define PUT_SCN_SCNLEN(abfd, in, ext) \
00104   H_PUT_32(abfd, in, ext->x_scn.x_scnlen)
00105 #endif
00106 #ifndef PUT_SCN_NRELOC
00107 #define PUT_SCN_NRELOC(abfd, in, ext) \
00108   H_PUT_16(abfd, in, ext->x_scn.x_nreloc)
00109 #endif
00110 #ifndef PUT_SCN_NLINNO
00111 #define PUT_SCN_NLINNO(abfd, in, ext) \
00112   H_PUT_16(abfd,in, ext->x_scn.x_nlinno)
00113 #endif
00114 #ifndef GET_LINENO_LNNO
00115 #define GET_LINENO_LNNO(abfd, ext) \
00116   H_GET_16 (abfd, ext->l_lnno);
00117 #endif
00118 #ifndef PUT_LINENO_LNNO
00119 #define PUT_LINENO_LNNO(abfd, val, ext) \
00120   H_PUT_16(abfd,val, ext->l_lnno);
00121 #endif
00122 
00123 /* The f_symptr field in the filehdr is sometimes 64 bits.  */
00124 #ifndef GET_FILEHDR_SYMPTR
00125 #define GET_FILEHDR_SYMPTR H_GET_32
00126 #endif
00127 #ifndef PUT_FILEHDR_SYMPTR
00128 #define PUT_FILEHDR_SYMPTR H_PUT_32
00129 #endif
00130 
00131 /* Some fields in the aouthdr are sometimes 64 bits.  */
00132 #ifndef GET_AOUTHDR_TSIZE
00133 #define GET_AOUTHDR_TSIZE H_GET_32
00134 #endif
00135 #ifndef PUT_AOUTHDR_TSIZE
00136 #define PUT_AOUTHDR_TSIZE H_PUT_32
00137 #endif
00138 #ifndef GET_AOUTHDR_DSIZE
00139 #define GET_AOUTHDR_DSIZE H_GET_32
00140 #endif
00141 #ifndef PUT_AOUTHDR_DSIZE
00142 #define PUT_AOUTHDR_DSIZE H_PUT_32
00143 #endif
00144 #ifndef GET_AOUTHDR_BSIZE
00145 #define GET_AOUTHDR_BSIZE H_GET_32
00146 #endif
00147 #ifndef PUT_AOUTHDR_BSIZE
00148 #define PUT_AOUTHDR_BSIZE H_PUT_32
00149 #endif
00150 #ifndef GET_AOUTHDR_ENTRY
00151 #define GET_AOUTHDR_ENTRY H_GET_32
00152 #endif
00153 #ifndef PUT_AOUTHDR_ENTRY
00154 #define PUT_AOUTHDR_ENTRY H_PUT_32
00155 #endif
00156 #ifndef GET_AOUTHDR_TEXT_START
00157 #define GET_AOUTHDR_TEXT_START H_GET_32
00158 #endif
00159 #ifndef PUT_AOUTHDR_TEXT_START
00160 #define PUT_AOUTHDR_TEXT_START H_PUT_32
00161 #endif
00162 #ifndef GET_AOUTHDR_DATA_START
00163 #define GET_AOUTHDR_DATA_START H_GET_32
00164 #endif
00165 #ifndef PUT_AOUTHDR_DATA_START
00166 #define PUT_AOUTHDR_DATA_START H_PUT_32
00167 #endif
00168 
00169 /* Some fields in the scnhdr are sometimes 64 bits.  */
00170 #ifndef GET_SCNHDR_PADDR
00171 #define GET_SCNHDR_PADDR H_GET_32
00172 #endif
00173 #ifndef PUT_SCNHDR_PADDR
00174 #define PUT_SCNHDR_PADDR H_PUT_32
00175 #endif
00176 #ifndef GET_SCNHDR_VADDR
00177 #define GET_SCNHDR_VADDR H_GET_32
00178 #endif
00179 #ifndef PUT_SCNHDR_VADDR
00180 #define PUT_SCNHDR_VADDR H_PUT_32
00181 #endif
00182 #ifndef GET_SCNHDR_SIZE
00183 #define GET_SCNHDR_SIZE H_GET_32
00184 #endif
00185 #ifndef PUT_SCNHDR_SIZE
00186 #define PUT_SCNHDR_SIZE H_PUT_32
00187 #endif
00188 #ifndef GET_SCNHDR_SCNPTR
00189 #define GET_SCNHDR_SCNPTR H_GET_32
00190 #endif
00191 #ifndef PUT_SCNHDR_SCNPTR
00192 #define PUT_SCNHDR_SCNPTR H_PUT_32
00193 #endif
00194 #ifndef GET_SCNHDR_RELPTR
00195 #define GET_SCNHDR_RELPTR H_GET_32
00196 #endif
00197 #ifndef PUT_SCNHDR_RELPTR
00198 #define PUT_SCNHDR_RELPTR H_PUT_32
00199 #endif
00200 #ifndef GET_SCNHDR_LNNOPTR
00201 #define GET_SCNHDR_LNNOPTR H_GET_32
00202 #endif
00203 #ifndef PUT_SCNHDR_LNNOPTR
00204 #define PUT_SCNHDR_LNNOPTR H_PUT_32
00205 #endif
00206 
00207 #ifdef COFF_WITH_pex64
00208 
00209 #define GET_OPTHDR_IMAGE_BASE            H_GET_64
00210 #define PUT_OPTHDR_IMAGE_BASE            H_PUT_64
00211 #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64
00212 #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64
00213 #define GET_OPTHDR_SIZE_OF_STACK_COMMIT  H_GET_64
00214 #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT  H_PUT_64
00215 #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE  H_GET_64
00216 #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE  H_PUT_64
00217 #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT   H_GET_64
00218 #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT   H_PUT_64
00219 #define GET_PDATA_ENTRY                  bfd_get_32
00220 
00221 #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pex64_bfd_copy_private_bfd_data_common
00222 #define _bfd_XX_bfd_copy_private_section_data           _bfd_pex64_bfd_copy_private_section_data
00223 #define _bfd_XX_get_symbol_info                         _bfd_pex64_get_symbol_info
00224 #define _bfd_XX_only_swap_filehdr_out                   _bfd_pex64_only_swap_filehdr_out
00225 #define _bfd_XX_print_private_bfd_data_common           _bfd_pex64_print_private_bfd_data_common
00226 #define _bfd_XXi_final_link_postscript                  _bfd_pex64i_final_link_postscript
00227 #define _bfd_XXi_final_link_postscript                  _bfd_pex64i_final_link_postscript
00228 #define _bfd_XXi_only_swap_filehdr_out                  _bfd_pex64i_only_swap_filehdr_out
00229 #define _bfd_XXi_swap_aouthdr_in                 _bfd_pex64i_swap_aouthdr_in
00230 #define _bfd_XXi_swap_aouthdr_out                _bfd_pex64i_swap_aouthdr_out
00231 #define _bfd_XXi_swap_aux_in                            _bfd_pex64i_swap_aux_in
00232 #define _bfd_XXi_swap_aux_out                           _bfd_pex64i_swap_aux_out
00233 #define _bfd_XXi_swap_lineno_in                         _bfd_pex64i_swap_lineno_in
00234 #define _bfd_XXi_swap_lineno_out                 _bfd_pex64i_swap_lineno_out
00235 #define _bfd_XXi_swap_scnhdr_out                 _bfd_pex64i_swap_scnhdr_out
00236 #define _bfd_XXi_swap_sym_in                            _bfd_pex64i_swap_sym_in
00237 #define _bfd_XXi_swap_sym_out                           _bfd_pex64i_swap_sym_out
00238 
00239 #elif defined COFF_WITH_pep
00240 
00241 #define GET_OPTHDR_IMAGE_BASE H_GET_64
00242 #define PUT_OPTHDR_IMAGE_BASE H_PUT_64
00243 #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64
00244 #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64
00245 #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_64
00246 #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_64
00247 #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_64
00248 #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_64
00249 #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_64
00250 #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_64
00251 #define GET_PDATA_ENTRY bfd_get_64
00252 
00253 #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pep_bfd_copy_private_bfd_data_common
00254 #define _bfd_XX_bfd_copy_private_section_data           _bfd_pep_bfd_copy_private_section_data
00255 #define _bfd_XX_get_symbol_info                         _bfd_pep_get_symbol_info
00256 #define _bfd_XX_only_swap_filehdr_out                   _bfd_pep_only_swap_filehdr_out
00257 #define _bfd_XX_print_private_bfd_data_common           _bfd_pep_print_private_bfd_data_common
00258 #define _bfd_XXi_final_link_postscript                  _bfd_pepi_final_link_postscript
00259 #define _bfd_XXi_final_link_postscript                  _bfd_pepi_final_link_postscript
00260 #define _bfd_XXi_only_swap_filehdr_out                  _bfd_pepi_only_swap_filehdr_out
00261 #define _bfd_XXi_swap_aouthdr_in                 _bfd_pepi_swap_aouthdr_in
00262 #define _bfd_XXi_swap_aouthdr_out                _bfd_pepi_swap_aouthdr_out
00263 #define _bfd_XXi_swap_aux_in                            _bfd_pepi_swap_aux_in
00264 #define _bfd_XXi_swap_aux_out                           _bfd_pepi_swap_aux_out
00265 #define _bfd_XXi_swap_lineno_in                         _bfd_pepi_swap_lineno_in
00266 #define _bfd_XXi_swap_lineno_out                 _bfd_pepi_swap_lineno_out
00267 #define _bfd_XXi_swap_scnhdr_out                 _bfd_pepi_swap_scnhdr_out
00268 #define _bfd_XXi_swap_sym_in                            _bfd_pepi_swap_sym_in
00269 #define _bfd_XXi_swap_sym_out                           _bfd_pepi_swap_sym_out
00270 
00271 #else /* !COFF_WITH_pep */
00272 
00273 #define GET_OPTHDR_IMAGE_BASE H_GET_32
00274 #define PUT_OPTHDR_IMAGE_BASE H_PUT_32
00275 #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_32
00276 #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_32
00277 #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_32
00278 #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_32
00279 #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_32
00280 #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_32
00281 #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_32
00282 #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_32
00283 #define GET_PDATA_ENTRY bfd_get_32
00284 
00285 #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pe_bfd_copy_private_bfd_data_common
00286 #define _bfd_XX_bfd_copy_private_section_data           _bfd_pe_bfd_copy_private_section_data
00287 #define _bfd_XX_get_symbol_info                         _bfd_pe_get_symbol_info
00288 #define _bfd_XX_only_swap_filehdr_out                   _bfd_pe_only_swap_filehdr_out
00289 #define _bfd_XX_print_private_bfd_data_common           _bfd_pe_print_private_bfd_data_common
00290 #define _bfd_XXi_final_link_postscript                  _bfd_pei_final_link_postscript
00291 #define _bfd_XXi_final_link_postscript                  _bfd_pei_final_link_postscript
00292 #define _bfd_XXi_only_swap_filehdr_out                  _bfd_pei_only_swap_filehdr_out
00293 #define _bfd_XXi_swap_aouthdr_in                 _bfd_pei_swap_aouthdr_in
00294 #define _bfd_XXi_swap_aouthdr_out                _bfd_pei_swap_aouthdr_out
00295 #define _bfd_XXi_swap_aux_in                            _bfd_pei_swap_aux_in
00296 #define _bfd_XXi_swap_aux_out                           _bfd_pei_swap_aux_out
00297 #define _bfd_XXi_swap_lineno_in                         _bfd_pei_swap_lineno_in
00298 #define _bfd_XXi_swap_lineno_out                 _bfd_pei_swap_lineno_out
00299 #define _bfd_XXi_swap_scnhdr_out                 _bfd_pei_swap_scnhdr_out
00300 #define _bfd_XXi_swap_sym_in                            _bfd_pei_swap_sym_in
00301 #define _bfd_XXi_swap_sym_out                           _bfd_pei_swap_sym_out
00302 
00303 #endif /* !COFF_WITH_pep */
00304 
00305 /* Macro: Returns true if the bfd is a PE executable as opposed to a PE object file.  */
00306 #define bfd_pe_executable_p(abfd)                \
00307   (CONST_STRNEQ ((abfd)->xvec->name, "pei-")            \
00308    || CONST_STRNEQ ((abfd)->xvec->name, "efi-app-"))
00309 
00310 /* These functions are architecture dependent, and are in peicode.h:
00311    coff_swap_reloc_in
00312    int coff_swap_reloc_out
00313    coff_swap_filehdr_in
00314    coff_swap_scnhdr_in
00315    pe_mkobject
00316    pe_mkobject_hook  */
00317 
00318 /* The functions described below are common across all PE/PEI
00319    implementations architecture types, and actually appear in
00320    peigen.c.  */
00321 
00322 #define coff_swap_sym_in      _bfd_XXi_swap_sym_in
00323 #define coff_swap_sym_out     _bfd_XXi_swap_sym_out
00324 #define coff_swap_aux_in      _bfd_XXi_swap_aux_in
00325 #define coff_swap_aux_out     _bfd_XXi_swap_aux_out
00326 #define coff_swap_lineno_in   _bfd_XXi_swap_lineno_in
00327 #define coff_swap_lineno_out  _bfd_XXi_swap_lineno_out
00328 #define coff_swap_aouthdr_in  _bfd_XXi_swap_aouthdr_in
00329 #define coff_swap_aouthdr_out _bfd_XXi_swap_aouthdr_out
00330 #define coff_swap_scnhdr_out  _bfd_XXi_swap_scnhdr_out
00331 
00332 #ifndef coff_final_link_postscript
00333 #define coff_final_link_postscript _bfd_XXi_final_link_postscript
00334 #endif
00335 
00336 void        _bfd_XXi_swap_sym_in (bfd *, void *, void *);
00337 unsigned    _bfd_XXi_swap_sym_out (bfd *, void *, void *);
00338 void        _bfd_XXi_swap_aux_in (bfd *, void *, int, int, int, int, void *);
00339 unsigned    _bfd_XXi_swap_aux_out (bfd *, void *, int, int, int, int, void *);
00340 void        _bfd_XXi_swap_lineno_in (bfd *, void *, void *);
00341 unsigned    _bfd_XXi_swap_lineno_out (bfd *, void *, void *);
00342 void        _bfd_XXi_swap_aouthdr_in (bfd *, void *, void *);
00343 unsigned    _bfd_XXi_swap_aouthdr_out (bfd *, void *, void *);
00344 unsigned    _bfd_XXi_swap_scnhdr_out (bfd *, void *, void *);
00345 bfd_boolean _bfd_XX_print_private_bfd_data_common (bfd *, void *);
00346 bfd_boolean _bfd_XX_bfd_copy_private_bfd_data_common (bfd *, bfd *);
00347 void        _bfd_XX_get_symbol_info (bfd *, asymbol *, symbol_info *);
00348 bfd_boolean _bfd_XXi_final_link_postscript (bfd *, struct coff_final_link_info *);
00349 
00350 /* The following are needed only for ONE of pe or pei, but don't
00351    otherwise vary; peicode.h fixes up ifdefs but we provide the
00352    prototype.  */
00353 
00354 unsigned    _bfd_XX_only_swap_filehdr_out  (bfd *, void *, void *);
00355 unsigned    _bfd_XXi_only_swap_filehdr_out (bfd *, void *, void *);
00356 bfd_boolean _bfd_XX_bfd_copy_private_section_data (bfd *, asection *, bfd *, asection *);
00357