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 Free Software Foundation, Inc.
4 Written by Cygnus Solutions.
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
24 /* Most of this hacked by Steve Chamberlain,
27 PE/PEI rearrangement (and code added): Donn Terry
28 Softway Systems, Inc. */
30 /* Hey look, some documentation [and in a place you expect to find it]!
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.
37 "Peering Inside the PE: A Tour of the Win32 Portable Executable
38 File Format", MSJ 1994, Volume 9.
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
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.
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. */
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)
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)
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)
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)
77 #define GET_LNSZ_LNNO(abfd, ext) \
78 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno)
81 #define GET_LNSZ_SIZE(abfd, ext) \
82 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size)
85 #define PUT_LNSZ_LNNO(abfd, in, ext) \
86 H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_lnno)
89 #define PUT_LNSZ_SIZE(abfd, in, ext) \
90 H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_size)
92 #ifndef GET_SCN_SCNLEN
93 #define GET_SCN_SCNLEN(abfd, ext) \
94 H_GET_32 (abfd, ext->x_scn.x_scnlen)
96 #ifndef GET_SCN_NRELOC
97 #define GET_SCN_NRELOC(abfd, ext) \
98 H_GET_16 (abfd, ext->x_scn.x_nreloc)
100 #ifndef GET_SCN_NLINNO
101 #define GET_SCN_NLINNO(abfd, ext) \
102 H_GET_16 (abfd, ext->x_scn.x_nlinno)
104 #ifndef PUT_SCN_SCNLEN
105 #define PUT_SCN_SCNLEN(abfd, in, ext) \
106 H_PUT_32(abfd, in, ext->x_scn.x_scnlen)
108 #ifndef PUT_SCN_NRELOC
109 #define PUT_SCN_NRELOC(abfd, in, ext) \
110 H_PUT_16(abfd, in, ext->x_scn.x_nreloc)
112 #ifndef PUT_SCN_NLINNO
113 #define PUT_SCN_NLINNO(abfd, in, ext) \
114 H_PUT_16(abfd,in, ext->x_scn.x_nlinno)
116 #ifndef GET_LINENO_LNNO
117 #define GET_LINENO_LNNO(abfd, ext) \
118 H_GET_16 (abfd, ext->l_lnno);
120 #ifndef PUT_LINENO_LNNO
121 #define PUT_LINENO_LNNO(abfd, val, ext) \
122 H_PUT_16(abfd,val, ext->l_lnno);
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
129 #ifndef PUT_FILEHDR_SYMPTR
130 #define PUT_FILEHDR_SYMPTR H_PUT_32
133 /* Some fields in the aouthdr are sometimes 64 bits. */
134 #ifndef GET_AOUTHDR_TSIZE
135 #define GET_AOUTHDR_TSIZE H_GET_32
137 #ifndef PUT_AOUTHDR_TSIZE
138 #define PUT_AOUTHDR_TSIZE H_PUT_32
140 #ifndef GET_AOUTHDR_DSIZE
141 #define GET_AOUTHDR_DSIZE H_GET_32
143 #ifndef PUT_AOUTHDR_DSIZE
144 #define PUT_AOUTHDR_DSIZE H_PUT_32
146 #ifndef GET_AOUTHDR_BSIZE
147 #define GET_AOUTHDR_BSIZE H_GET_32
149 #ifndef PUT_AOUTHDR_BSIZE
150 #define PUT_AOUTHDR_BSIZE H_PUT_32
152 #ifndef GET_AOUTHDR_ENTRY
153 #define GET_AOUTHDR_ENTRY H_GET_32
155 #ifndef PUT_AOUTHDR_ENTRY
156 #define PUT_AOUTHDR_ENTRY H_PUT_32
158 #ifndef GET_AOUTHDR_TEXT_START
159 #define GET_AOUTHDR_TEXT_START H_GET_32
161 #ifndef PUT_AOUTHDR_TEXT_START
162 #define PUT_AOUTHDR_TEXT_START H_PUT_32
164 #ifndef GET_AOUTHDR_DATA_START
165 #define GET_AOUTHDR_DATA_START H_GET_32
167 #ifndef PUT_AOUTHDR_DATA_START
168 #define PUT_AOUTHDR_DATA_START H_PUT_32
171 /* Some fields in the scnhdr are sometimes 64 bits. */
172 #ifndef GET_SCNHDR_PADDR
173 #define GET_SCNHDR_PADDR H_GET_32
175 #ifndef PUT_SCNHDR_PADDR
176 #define PUT_SCNHDR_PADDR H_PUT_32
178 #ifndef GET_SCNHDR_VADDR
179 #define GET_SCNHDR_VADDR H_GET_32
181 #ifndef PUT_SCNHDR_VADDR
182 #define PUT_SCNHDR_VADDR H_PUT_32
184 #ifndef GET_SCNHDR_SIZE
185 #define GET_SCNHDR_SIZE H_GET_32
187 #ifndef PUT_SCNHDR_SIZE
188 #define PUT_SCNHDR_SIZE H_PUT_32
190 #ifndef GET_SCNHDR_SCNPTR
191 #define GET_SCNHDR_SCNPTR H_GET_32
193 #ifndef PUT_SCNHDR_SCNPTR
194 #define PUT_SCNHDR_SCNPTR H_PUT_32
196 #ifndef GET_SCNHDR_RELPTR
197 #define GET_SCNHDR_RELPTR H_GET_32
199 #ifndef PUT_SCNHDR_RELPTR
200 #define PUT_SCNHDR_RELPTR H_PUT_32
202 #ifndef GET_SCNHDR_LNNOPTR
203 #define GET_SCNHDR_LNNOPTR H_GET_32
205 #ifndef PUT_SCNHDR_LNNOPTR
206 #define PUT_SCNHDR_LNNOPTR H_PUT_32
209 #ifdef COFF_WITH_pex64
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
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
240 #elif defined COFF_WITH_pep
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
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
271 #else /* !COFF_WITH_pep */
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
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
302 #endif /* !COFF_WITH_pep */
304 /* Returns true if the target is a PE executable target. */
305 #define bfd_target_pei_p(xvec) \
306 (CONST_STRNEQ ((xvec)->name, "pei-"))
308 /* Return the arch string of a PE executable target. */
309 #define bfd_target_pei_arch(xvec) \
310 ((xvec)->name + sizeof ("pei-") - 1)
312 /* Returns true if the target is an EFI target. */
313 #define bfd_target_efi_p(xvec) \
314 (CONST_STRNEQ ((xvec)->name, "efi-app-"))
316 /* Return the arch string of an EFI target. */
317 #define bfd_target_efi_arch(xvec) \
318 ((xvec)->name + sizeof ("efi-app-") - 1)
320 /* Macro: Returns true if the bfd is a PE executable as opposed to a
322 #define bfd_pe_executable_p(abfd) \
323 (bfd_target_pei_p ((abfd)->xvec) \
324 || bfd_target_efi_p ((abfd)->xvec))
326 /* These functions are architecture dependent, and are in peicode.h:
328 int coff_swap_reloc_out
334 /* The functions described below are common across all PE/PEI
335 implementations architecture types, and actually appear in
338 #define coff_swap_sym_in _bfd_XXi_swap_sym_in
339 #define coff_swap_sym_out _bfd_XXi_swap_sym_out
340 #define coff_swap_aux_in _bfd_XXi_swap_aux_in
341 #define coff_swap_aux_out _bfd_XXi_swap_aux_out
342 #define coff_swap_lineno_in _bfd_XXi_swap_lineno_in
343 #define coff_swap_lineno_out _bfd_XXi_swap_lineno_out
344 #define coff_swap_aouthdr_in _bfd_XXi_swap_aouthdr_in
345 #define coff_swap_aouthdr_out _bfd_XXi_swap_aouthdr_out
346 #define coff_swap_scnhdr_out _bfd_XXi_swap_scnhdr_out
348 #ifndef coff_final_link_postscript
349 #define coff_final_link_postscript _bfd_XXi_final_link_postscript
352 void _bfd_XXi_swap_sym_in (bfd
*, void *, void *);
353 unsigned _bfd_XXi_swap_sym_out (bfd
*, void *, void *);
354 void _bfd_XXi_swap_aux_in (bfd
*, void *, int, int, int, int, void *);
355 unsigned _bfd_XXi_swap_aux_out (bfd
*, void *, int, int, int, int, void *);
356 void _bfd_XXi_swap_lineno_in (bfd
*, void *, void *);
357 unsigned _bfd_XXi_swap_lineno_out (bfd
*, void *, void *);
358 void _bfd_XXi_swap_aouthdr_in (bfd
*, void *, void *);
359 unsigned _bfd_XXi_swap_aouthdr_out (bfd
*, void *, void *);
360 unsigned _bfd_XXi_swap_scnhdr_out (bfd
*, void *, void *);
361 bfd_boolean
_bfd_XX_print_private_bfd_data_common (bfd
*, void *);
362 bfd_boolean
_bfd_XX_bfd_copy_private_bfd_data_common (bfd
*, bfd
*);
363 void _bfd_XX_get_symbol_info (bfd
*, asymbol
*, symbol_info
*);
364 bfd_boolean
_bfd_XXi_final_link_postscript (bfd
*, struct coff_final_link_info
*);
366 /* The following are needed only for ONE of pe or pei, but don't
367 otherwise vary; peicode.h fixes up ifdefs but we provide the
370 unsigned _bfd_XX_only_swap_filehdr_out (bfd
*, void *, void *);
371 unsigned _bfd_XXi_only_swap_filehdr_out (bfd
*, void *, void *);
372 bfd_boolean
_bfd_XX_bfd_copy_private_section_data (bfd
*, asection
*, bfd
*, asection
*);