Commit | Line | Data |
---|---|---|
277d1b5e | 1 | /* Support for the generic parts of PE/PEI; common header information. |
ff0c9faf | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 |
7898deda | 3 | Free Software Foundation, Inc. |
277d1b5e ILT |
4 | Written by Cygnus Solutions. |
5 | ||
ff0c9faf | 6 | This file is part of BFD, the Binary File Descriptor library. |
277d1b5e | 7 | |
ff0c9faf NC |
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 2 of the License, or | |
11 | (at your option) any later version. | |
277d1b5e | 12 | |
ff0c9faf NC |
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
277d1b5e | 21 | |
ff0c9faf | 22 | /* Most of this hacked by Steve Chamberlain, |
277d1b5e ILT |
23 | sac@cygnus.com |
24 | ||
ff0c9faf NC |
25 | PE/PEI rearrangement (and code added): Donn Terry |
26 | Softway Systems, Inc. */ | |
277d1b5e ILT |
27 | |
28 | /* Hey look, some documentation [and in a place you expect to find it]! | |
29 | ||
30 | The main reference for the pei format is "Microsoft Portable Executable | |
31 | and Common Object File Format Specification 4.1". Get it if you need to | |
32 | do some serious hacking on this code. | |
33 | ||
34 | Another reference: | |
35 | "Peering Inside the PE: A Tour of the Win32 Portable Executable | |
36 | File Format", MSJ 1994, Volume 9. | |
37 | ||
38 | The *sole* difference between the pe format and the pei format is that the | |
39 | latter has an MSDOS 2.0 .exe header on the front that prints the message | |
40 | "This app must be run under Windows." (or some such). | |
41 | (FIXME: Whether that statement is *really* true or not is unknown. | |
42 | Are there more subtle differences between pe and pei formats? | |
43 | For now assume there aren't. If you find one, then for God sakes | |
44 | document it here!) | |
45 | ||
46 | The Microsoft docs use the word "image" instead of "executable" because | |
47 | the former can also refer to a DLL (shared library). Confusion can arise | |
48 | because the `i' in `pei' also refers to "image". The `pe' format can | |
49 | also create images (i.e. executables), it's just that to run on a win32 | |
50 | system you need to use the pei format. | |
51 | ||
52 | FIXME: Please add more docs here so the next poor fool that has to hack | |
53 | on this code has a chance of getting something accomplished without | |
ff0c9faf | 54 | wasting too much time. */ |
277d1b5e ILT |
55 | |
56 | #ifndef GET_FCN_LNNOPTR | |
57 | #define GET_FCN_LNNOPTR(abfd, ext) \ | |
dc810e39 | 58 | H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr) |
277d1b5e ILT |
59 | #endif |
60 | ||
61 | #ifndef GET_FCN_ENDNDX | |
dc810e39 AM |
62 | #define GET_FCN_ENDNDX(abfd, ext) \ |
63 | H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx) | |
277d1b5e ILT |
64 | #endif |
65 | ||
66 | #ifndef PUT_FCN_LNNOPTR | |
dc810e39 AM |
67 | #define PUT_FCN_LNNOPTR(abfd, in, ext) \ |
68 | H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr) | |
277d1b5e ILT |
69 | #endif |
70 | #ifndef PUT_FCN_ENDNDX | |
dc810e39 AM |
71 | #define PUT_FCN_ENDNDX(abfd, in, ext) \ |
72 | H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_endndx) | |
277d1b5e ILT |
73 | #endif |
74 | #ifndef GET_LNSZ_LNNO | |
dc810e39 AM |
75 | #define GET_LNSZ_LNNO(abfd, ext) \ |
76 | H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno) | |
277d1b5e ILT |
77 | #endif |
78 | #ifndef GET_LNSZ_SIZE | |
dc810e39 AM |
79 | #define GET_LNSZ_SIZE(abfd, ext) \ |
80 | H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size) | |
277d1b5e ILT |
81 | #endif |
82 | #ifndef PUT_LNSZ_LNNO | |
dc810e39 AM |
83 | #define PUT_LNSZ_LNNO(abfd, in, ext) \ |
84 | H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_lnno) | |
277d1b5e ILT |
85 | #endif |
86 | #ifndef PUT_LNSZ_SIZE | |
dc810e39 AM |
87 | #define PUT_LNSZ_SIZE(abfd, in, ext) \ |
88 | H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_size) | |
277d1b5e ILT |
89 | #endif |
90 | #ifndef GET_SCN_SCNLEN | |
dc810e39 AM |
91 | #define GET_SCN_SCNLEN(abfd, ext) \ |
92 | H_GET_32 (abfd, ext->x_scn.x_scnlen) | |
277d1b5e ILT |
93 | #endif |
94 | #ifndef GET_SCN_NRELOC | |
dc810e39 AM |
95 | #define GET_SCN_NRELOC(abfd, ext) \ |
96 | H_GET_16 (abfd, ext->x_scn.x_nreloc) | |
277d1b5e ILT |
97 | #endif |
98 | #ifndef GET_SCN_NLINNO | |
dc810e39 AM |
99 | #define GET_SCN_NLINNO(abfd, ext) \ |
100 | H_GET_16 (abfd, ext->x_scn.x_nlinno) | |
277d1b5e ILT |
101 | #endif |
102 | #ifndef PUT_SCN_SCNLEN | |
dc810e39 AM |
103 | #define PUT_SCN_SCNLEN(abfd, in, ext) \ |
104 | H_PUT_32(abfd, in, ext->x_scn.x_scnlen) | |
277d1b5e ILT |
105 | #endif |
106 | #ifndef PUT_SCN_NRELOC | |
dc810e39 AM |
107 | #define PUT_SCN_NRELOC(abfd, in, ext) \ |
108 | H_PUT_16(abfd, in, ext->x_scn.x_nreloc) | |
277d1b5e ILT |
109 | #endif |
110 | #ifndef PUT_SCN_NLINNO | |
dc810e39 AM |
111 | #define PUT_SCN_NLINNO(abfd, in, ext) \ |
112 | H_PUT_16(abfd,in, ext->x_scn.x_nlinno) | |
277d1b5e ILT |
113 | #endif |
114 | #ifndef GET_LINENO_LNNO | |
dc810e39 AM |
115 | #define GET_LINENO_LNNO(abfd, ext) \ |
116 | H_GET_16 (abfd, ext->l_lnno); | |
277d1b5e ILT |
117 | #endif |
118 | #ifndef PUT_LINENO_LNNO | |
dc810e39 AM |
119 | #define PUT_LINENO_LNNO(abfd, val, ext) \ |
120 | H_PUT_16(abfd,val, ext->l_lnno); | |
277d1b5e ILT |
121 | #endif |
122 | ||
123 | /* The f_symptr field in the filehdr is sometimes 64 bits. */ | |
124 | #ifndef GET_FILEHDR_SYMPTR | |
dc810e39 | 125 | #define GET_FILEHDR_SYMPTR H_GET_32 |
277d1b5e ILT |
126 | #endif |
127 | #ifndef PUT_FILEHDR_SYMPTR | |
dc810e39 | 128 | #define PUT_FILEHDR_SYMPTR H_PUT_32 |
277d1b5e ILT |
129 | #endif |
130 | ||
131 | /* Some fields in the aouthdr are sometimes 64 bits. */ | |
132 | #ifndef GET_AOUTHDR_TSIZE | |
dc810e39 | 133 | #define GET_AOUTHDR_TSIZE H_GET_32 |
277d1b5e ILT |
134 | #endif |
135 | #ifndef PUT_AOUTHDR_TSIZE | |
dc810e39 | 136 | #define PUT_AOUTHDR_TSIZE H_PUT_32 |
277d1b5e ILT |
137 | #endif |
138 | #ifndef GET_AOUTHDR_DSIZE | |
dc810e39 | 139 | #define GET_AOUTHDR_DSIZE H_GET_32 |
277d1b5e ILT |
140 | #endif |
141 | #ifndef PUT_AOUTHDR_DSIZE | |
dc810e39 | 142 | #define PUT_AOUTHDR_DSIZE H_PUT_32 |
277d1b5e ILT |
143 | #endif |
144 | #ifndef GET_AOUTHDR_BSIZE | |
dc810e39 | 145 | #define GET_AOUTHDR_BSIZE H_GET_32 |
277d1b5e ILT |
146 | #endif |
147 | #ifndef PUT_AOUTHDR_BSIZE | |
dc810e39 | 148 | #define PUT_AOUTHDR_BSIZE H_PUT_32 |
277d1b5e ILT |
149 | #endif |
150 | #ifndef GET_AOUTHDR_ENTRY | |
dc810e39 | 151 | #define GET_AOUTHDR_ENTRY H_GET_32 |
277d1b5e ILT |
152 | #endif |
153 | #ifndef PUT_AOUTHDR_ENTRY | |
dc810e39 | 154 | #define PUT_AOUTHDR_ENTRY H_PUT_32 |
277d1b5e ILT |
155 | #endif |
156 | #ifndef GET_AOUTHDR_TEXT_START | |
dc810e39 | 157 | #define GET_AOUTHDR_TEXT_START H_GET_32 |
277d1b5e ILT |
158 | #endif |
159 | #ifndef PUT_AOUTHDR_TEXT_START | |
dc810e39 | 160 | #define PUT_AOUTHDR_TEXT_START H_PUT_32 |
277d1b5e ILT |
161 | #endif |
162 | #ifndef GET_AOUTHDR_DATA_START | |
dc810e39 | 163 | #define GET_AOUTHDR_DATA_START H_GET_32 |
277d1b5e ILT |
164 | #endif |
165 | #ifndef PUT_AOUTHDR_DATA_START | |
dc810e39 | 166 | #define PUT_AOUTHDR_DATA_START H_PUT_32 |
277d1b5e ILT |
167 | #endif |
168 | ||
169 | /* Some fields in the scnhdr are sometimes 64 bits. */ | |
170 | #ifndef GET_SCNHDR_PADDR | |
dc810e39 | 171 | #define GET_SCNHDR_PADDR H_GET_32 |
277d1b5e ILT |
172 | #endif |
173 | #ifndef PUT_SCNHDR_PADDR | |
dc810e39 | 174 | #define PUT_SCNHDR_PADDR H_PUT_32 |
277d1b5e ILT |
175 | #endif |
176 | #ifndef GET_SCNHDR_VADDR | |
dc810e39 | 177 | #define GET_SCNHDR_VADDR H_GET_32 |
277d1b5e ILT |
178 | #endif |
179 | #ifndef PUT_SCNHDR_VADDR | |
dc810e39 | 180 | #define PUT_SCNHDR_VADDR H_PUT_32 |
277d1b5e ILT |
181 | #endif |
182 | #ifndef GET_SCNHDR_SIZE | |
dc810e39 | 183 | #define GET_SCNHDR_SIZE H_GET_32 |
277d1b5e ILT |
184 | #endif |
185 | #ifndef PUT_SCNHDR_SIZE | |
dc810e39 | 186 | #define PUT_SCNHDR_SIZE H_PUT_32 |
277d1b5e ILT |
187 | #endif |
188 | #ifndef GET_SCNHDR_SCNPTR | |
dc810e39 | 189 | #define GET_SCNHDR_SCNPTR H_GET_32 |
277d1b5e ILT |
190 | #endif |
191 | #ifndef PUT_SCNHDR_SCNPTR | |
dc810e39 | 192 | #define PUT_SCNHDR_SCNPTR H_PUT_32 |
277d1b5e ILT |
193 | #endif |
194 | #ifndef GET_SCNHDR_RELPTR | |
dc810e39 | 195 | #define GET_SCNHDR_RELPTR H_GET_32 |
277d1b5e ILT |
196 | #endif |
197 | #ifndef PUT_SCNHDR_RELPTR | |
dc810e39 | 198 | #define PUT_SCNHDR_RELPTR H_PUT_32 |
277d1b5e ILT |
199 | #endif |
200 | #ifndef GET_SCNHDR_LNNOPTR | |
dc810e39 | 201 | #define GET_SCNHDR_LNNOPTR H_GET_32 |
277d1b5e ILT |
202 | #endif |
203 | #ifndef PUT_SCNHDR_LNNOPTR | |
dc810e39 | 204 | #define PUT_SCNHDR_LNNOPTR H_PUT_32 |
277d1b5e ILT |
205 | #endif |
206 | ||
cbff5e0d | 207 | #ifdef COFF_WITH_pep |
fac41780 | 208 | |
dc810e39 AM |
209 | #define GET_OPTHDR_IMAGE_BASE H_GET_64 |
210 | #define PUT_OPTHDR_IMAGE_BASE H_PUT_64 | |
211 | #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64 | |
212 | #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64 | |
213 | #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_64 | |
214 | #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_64 | |
215 | #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_64 | |
216 | #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_64 | |
217 | #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_64 | |
218 | #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_64 | |
fac41780 JW |
219 | #define GET_PDATA_ENTRY bfd_get_64 |
220 | ||
cbff5e0d DD |
221 | #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pep_bfd_copy_private_bfd_data_common |
222 | #define _bfd_XX_bfd_copy_private_section_data _bfd_pep_bfd_copy_private_section_data | |
223 | #define _bfd_XX_get_symbol_info _bfd_pep_get_symbol_info | |
224 | #define _bfd_XX_only_swap_filehdr_out _bfd_pep_only_swap_filehdr_out | |
225 | #define _bfd_XX_print_private_bfd_data_common _bfd_pep_print_private_bfd_data_common | |
226 | #define _bfd_XXi_final_link_postscript _bfd_pepi_final_link_postscript | |
227 | #define _bfd_XXi_final_link_postscript _bfd_pepi_final_link_postscript | |
228 | #define _bfd_XXi_only_swap_filehdr_out _bfd_pepi_only_swap_filehdr_out | |
229 | #define _bfd_XXi_swap_aouthdr_in _bfd_pepi_swap_aouthdr_in | |
230 | #define _bfd_XXi_swap_aouthdr_out _bfd_pepi_swap_aouthdr_out | |
231 | #define _bfd_XXi_swap_aux_in _bfd_pepi_swap_aux_in | |
232 | #define _bfd_XXi_swap_aux_out _bfd_pepi_swap_aux_out | |
233 | #define _bfd_XXi_swap_lineno_in _bfd_pepi_swap_lineno_in | |
234 | #define _bfd_XXi_swap_lineno_out _bfd_pepi_swap_lineno_out | |
235 | #define _bfd_XXi_swap_scnhdr_out _bfd_pepi_swap_scnhdr_out | |
236 | #define _bfd_XXi_swap_sym_in _bfd_pepi_swap_sym_in | |
237 | #define _bfd_XXi_swap_sym_out _bfd_pepi_swap_sym_out | |
238 | ||
239 | #else /* !COFF_WITH_pep */ | |
fac41780 | 240 | |
dc810e39 AM |
241 | #define GET_OPTHDR_IMAGE_BASE H_GET_32 |
242 | #define PUT_OPTHDR_IMAGE_BASE H_PUT_32 | |
243 | #define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_32 | |
244 | #define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_32 | |
245 | #define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_32 | |
246 | #define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_32 | |
247 | #define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_32 | |
248 | #define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_32 | |
249 | #define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_32 | |
250 | #define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_32 | |
fac41780 JW |
251 | #define GET_PDATA_ENTRY bfd_get_32 |
252 | ||
cbff5e0d DD |
253 | #define _bfd_XX_bfd_copy_private_bfd_data_common _bfd_pe_bfd_copy_private_bfd_data_common |
254 | #define _bfd_XX_bfd_copy_private_section_data _bfd_pe_bfd_copy_private_section_data | |
255 | #define _bfd_XX_get_symbol_info _bfd_pe_get_symbol_info | |
256 | #define _bfd_XX_only_swap_filehdr_out _bfd_pe_only_swap_filehdr_out | |
257 | #define _bfd_XX_print_private_bfd_data_common _bfd_pe_print_private_bfd_data_common | |
258 | #define _bfd_XXi_final_link_postscript _bfd_pei_final_link_postscript | |
259 | #define _bfd_XXi_final_link_postscript _bfd_pei_final_link_postscript | |
260 | #define _bfd_XXi_only_swap_filehdr_out _bfd_pei_only_swap_filehdr_out | |
261 | #define _bfd_XXi_swap_aouthdr_in _bfd_pei_swap_aouthdr_in | |
262 | #define _bfd_XXi_swap_aouthdr_out _bfd_pei_swap_aouthdr_out | |
263 | #define _bfd_XXi_swap_aux_in _bfd_pei_swap_aux_in | |
264 | #define _bfd_XXi_swap_aux_out _bfd_pei_swap_aux_out | |
265 | #define _bfd_XXi_swap_lineno_in _bfd_pei_swap_lineno_in | |
266 | #define _bfd_XXi_swap_lineno_out _bfd_pei_swap_lineno_out | |
267 | #define _bfd_XXi_swap_scnhdr_out _bfd_pei_swap_scnhdr_out | |
268 | #define _bfd_XXi_swap_sym_in _bfd_pei_swap_sym_in | |
269 | #define _bfd_XXi_swap_sym_out _bfd_pei_swap_sym_out | |
270 | ||
271 | #endif /* !COFF_WITH_pep */ | |
fac41780 | 272 | |
277d1b5e ILT |
273 | /* These functions are architecture dependent, and are in peicode.h: |
274 | coff_swap_reloc_in | |
275 | int coff_swap_reloc_out | |
276 | coff_swap_filehdr_in | |
277 | coff_swap_scnhdr_in | |
278 | pe_mkobject | |
279 | pe_mkobject_hook */ | |
280 | ||
281 | /* The functions described below are common across all PE/PEI | |
282 | implementations architecture types, and actually appear in | |
283 | peigen.c. */ | |
284 | ||
cbff5e0d DD |
285 | void _bfd_XXi_swap_sym_in PARAMS ((bfd*, PTR, PTR)); |
286 | #define coff_swap_sym_in _bfd_XXi_swap_sym_in | |
277d1b5e | 287 | |
cbff5e0d DD |
288 | unsigned int _bfd_XXi_swap_sym_out PARAMS ((bfd*, PTR, PTR)); |
289 | #define coff_swap_sym_out _bfd_XXi_swap_sym_out | |
277d1b5e | 290 | |
cbff5e0d DD |
291 | void _bfd_XXi_swap_aux_in PARAMS ((bfd *, PTR, int, int, int, int, PTR)); |
292 | #define coff_swap_aux_in _bfd_XXi_swap_aux_in | |
277d1b5e | 293 | |
cbff5e0d | 294 | unsigned int _bfd_XXi_swap_aux_out \ |
277d1b5e | 295 | PARAMS ((bfd *, PTR, int, int, int, int, PTR)); |
cbff5e0d | 296 | #define coff_swap_aux_out _bfd_XXi_swap_aux_out |
277d1b5e | 297 | |
cbff5e0d DD |
298 | void _bfd_XXi_swap_lineno_in PARAMS ((bfd*, PTR, PTR)); |
299 | #define coff_swap_lineno_in _bfd_XXi_swap_lineno_in | |
277d1b5e | 300 | |
cbff5e0d DD |
301 | unsigned int _bfd_XXi_swap_lineno_out PARAMS ((bfd*, PTR, PTR)); |
302 | #define coff_swap_lineno_out _bfd_XXi_swap_lineno_out | |
277d1b5e | 303 | |
cbff5e0d DD |
304 | void _bfd_XXi_swap_aouthdr_in PARAMS ((bfd*, PTR, PTR)); |
305 | #define coff_swap_aouthdr_in _bfd_XXi_swap_aouthdr_in | |
277d1b5e | 306 | |
cbff5e0d DD |
307 | unsigned int _bfd_XXi_swap_aouthdr_out PARAMS ((bfd *, PTR, PTR)); |
308 | #define coff_swap_aouthdr_out _bfd_XXi_swap_aouthdr_out | |
277d1b5e | 309 | |
cbff5e0d DD |
310 | unsigned int _bfd_XXi_swap_scnhdr_out PARAMS ((bfd *, PTR, PTR)); |
311 | #define coff_swap_scnhdr_out _bfd_XXi_swap_scnhdr_out | |
277d1b5e | 312 | |
b34976b6 | 313 | bfd_boolean _bfd_XX_print_private_bfd_data_common PARAMS ((bfd *, PTR)); |
277d1b5e | 314 | |
b34976b6 | 315 | bfd_boolean _bfd_XX_bfd_copy_private_bfd_data_common PARAMS ((bfd *, bfd *)); |
7d2b58d6 | 316 | |
cbff5e0d | 317 | void _bfd_XX_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *)); |
277d1b5e | 318 | |
b34976b6 | 319 | bfd_boolean _bfd_XXi_final_link_postscript |
2fbadf2c | 320 | PARAMS ((bfd *, struct coff_final_link_info *)); |
2fbadf2c | 321 | |
17505c5c | 322 | #ifndef coff_final_link_postscript |
cbff5e0d | 323 | #define coff_final_link_postscript _bfd_XXi_final_link_postscript |
17505c5c | 324 | #endif |
277d1b5e ILT |
325 | /* The following are needed only for ONE of pe or pei, but don't |
326 | otherwise vary; peicode.h fixes up ifdefs but we provide the | |
327 | prototype. */ | |
328 | ||
cbff5e0d DD |
329 | unsigned int _bfd_XX_only_swap_filehdr_out PARAMS ((bfd*, PTR, PTR)); |
330 | unsigned int _bfd_XXi_only_swap_filehdr_out PARAMS ((bfd*, PTR, PTR)); | |
b34976b6 | 331 | bfd_boolean _bfd_XX_bfd_copy_private_section_data |
277d1b5e | 332 | PARAMS ((bfd *, asection *, bfd *, asection *)); |
ff0c9faf NC |
333 | |
334 | /* Macro: Returns true if the bfd is a PE executable as opposed to a PE object file. */ | |
4091ea4e L |
335 | #define bfd_pe_executable_p(abfd) \ |
336 | (strncmp ((abfd)->xvec->name, "pei-", 4) == 0 \ | |
337 | || strncmp ((abfd)->xvec->name, "efi-app-", 8) == 0) |