aboutsummaryrefslogtreecommitdiff
path: root/bfd/libpei.h
blob: 8fcae7bea58215301a0f400bd36aa66ff764ea9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* Support for the generic parts of PE/PEI; common header information.
   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005,
   2006, 2007, 2008   Free Software Foundation, Inc.
   Written by Cygnus Solutions.

   This file is part of BFD, the Binary File Descriptor library.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */


/* Most of this hacked by  Steve Chamberlain,
			sac@cygnus.com

   PE/PEI rearrangement (and code added): Donn Terry
				       Softway Systems, Inc.  */

/* Hey look, some documentation [and in a place you expect to find it]!

   The main reference for the pei format is "Microsoft Portable Executable
   and Common Object File Format Specification 4.1".  Get it if you need to
   do some serious hacking on this code.

   Another reference:
   "Peering Inside the PE: A Tour of the Win32 Portable Executable
   File Format", MSJ 1994, Volume 9.

   The *sole* difference between the pe format and the pei format is that the
   latter has an MSDOS 2.0 .exe header on the front that prints the message
   "This app must be run under Windows." (or some such).
   (FIXME: Whether that statement is *really* true or not is unknown.
   Are there more subtle differences between pe and pei formats?
   For now assume there aren't.  If you find one, then for God sakes
   document it here!)

   The Microsoft docs use the word "image" instead of "executable" because
   the former can also refer to a DLL (shared library).  Confusion can arise
   because the `i' in `pei' also refers to "image".  The `pe' format can
   also create images (i.e. executables), it's just that to run on a win32
   system you need to use the pei format.

   FIXME: Please add more docs here so the next poor fool that has to hack
   on this code has a chance of getting something accomplished without
   wasting too much time.  */

#ifndef GET_FCN_LNNOPTR
#define GET_FCN_LNNOPTR(abfd, ext) \
  H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
#endif

#ifndef GET_FCN_ENDNDX
#define GET_FCN_ENDNDX(abfd, ext) \
  H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx)
#endif

#ifndef PUT_FCN_LNNOPTR
#define PUT_FCN_LNNOPTR(abfd, in, ext) \
  H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
#endif
#ifndef PUT_FCN_ENDNDX
#define PUT_FCN_ENDNDX(abfd, in, ext) \
  H_PUT_32(abfd, in, ext->x_sym.x_fcnary.x_fcn.x_endndx)
#endif
#ifndef GET_LNSZ_LNNO
#define GET_LNSZ_LNNO(abfd, ext) \
  H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno)
#endif
#ifndef GET_LNSZ_SIZE
#define GET_LNSZ_SIZE(abfd, ext) \
  H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size)
#endif
#ifndef PUT_LNSZ_LNNO
#define PUT_LNSZ_LNNO(abfd, in, ext) \
  H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_lnno)
#endif
#ifndef PUT_LNSZ_SIZE
#define PUT_LNSZ_SIZE(abfd, in, ext) \
  H_PUT_16(abfd, in, ext->x_sym.x_misc.x_lnsz.x_size)
#endif
#ifndef GET_SCN_SCNLEN
#define GET_SCN_SCNLEN(abfd, ext) \
  H_GET_32 (abfd, ext->x_scn.x_scnlen)
#endif
#ifndef GET_SCN_NRELOC
#define GET_SCN_NRELOC(abfd, ext) \
  H_GET_16 (abfd, ext->x_scn.x_nreloc)
#endif
#ifndef GET_SCN_NLINNO
#define GET_SCN_NLINNO(abfd, ext) \
  H_GET_16 (abfd, ext->x_scn.x_nlinno)
#endif
#ifndef PUT_SCN_SCNLEN
#define PUT_SCN_SCNLEN(abfd, in, ext) \
  H_PUT_32(abfd, in, ext->x_scn.x_scnlen)
#endif
#ifndef PUT_SCN_NRELOC
#define PUT_SCN_NRELOC(abfd, in, ext) \
  H_PUT_16(abfd, in, ext->x_scn.x_nreloc)
#endif
#ifndef PUT_SCN_NLINNO
#define PUT_SCN_NLINNO(abfd, in, ext) \
  H_PUT_16(abfd,in, ext->x_scn.x_nlinno)
#endif
#ifndef GET_LINENO_LNNO
#define GET_LINENO_LNNO(abfd, ext) \
  H_GET_16 (abfd, ext->l_lnno);
#endif
#ifndef PUT_LINENO_LNNO
#define PUT_LINENO_LNNO(abfd, val, ext) \
  H_PUT_16(abfd,val, ext->l_lnno);
#endif

/* The f_symptr field in the filehdr is sometimes 64 bits.  */
#ifndef GET_FILEHDR_SYMPTR
#define GET_FILEHDR_SYMPTR H_GET_32
#endif
#ifndef PUT_FILEHDR_SYMPTR
#define PUT_FILEHDR_SYMPTR H_PUT_32
#endif

/* Some fields in the aouthdr are sometimes 64 bits.  */
#ifndef GET_AOUTHDR_TSIZE
#define GET_AOUTHDR_TSIZE H_GET_32
#endif
#ifndef PUT_AOUTHDR_TSIZE
#define PUT_AOUTHDR_TSIZE H_PUT_32
#endif
#ifndef GET_AOUTHDR_DSIZE
#define GET_AOUTHDR_DSIZE H_GET_32
#endif
#ifndef PUT_AOUTHDR_DSIZE
#define PUT_AOUTHDR_DSIZE H_PUT_32
#endif
#ifndef GET_AOUTHDR_BSIZE
#define GET_AOUTHDR_BSIZE H_GET_32
#endif
#ifndef PUT_AOUTHDR_BSIZE
#define PUT_AOUTHDR_BSIZE H_PUT_32
#endif
#ifndef GET_AOUTHDR_ENTRY
#define GET_AOUTHDR_ENTRY H_GET_32
#endif
#ifndef PUT_AOUTHDR_ENTRY
#define PUT_AOUTHDR_ENTRY H_PUT_32
#endif
#ifndef GET_AOUTHDR_TEXT_START
#define GET_AOUTHDR_TEXT_START H_GET_32
#endif
#ifndef PUT_AOUTHDR_TEXT_START
#define PUT_AOUTHDR_TEXT_START H_PUT_32
#endif
#ifndef GET_AOUTHDR_DATA_START
#define GET_AOUTHDR_DATA_START H_GET_32
#endif
#ifndef PUT_AOUTHDR_DATA_START
#define PUT_AOUTHDR_DATA_START H_PUT_32
#endif

/* Some fields in the scnhdr are sometimes 64 bits.  */
#ifndef GET_SCNHDR_PADDR
#define GET_SCNHDR_PADDR H_GET_32
#endif
#ifndef PUT_SCNHDR_PADDR
#define PUT_SCNHDR_PADDR H_PUT_32
#endif
#ifndef GET_SCNHDR_VADDR
#define GET_SCNHDR_VADDR H_GET_32
#endif
#ifndef PUT_SCNHDR_VADDR
#define PUT_SCNHDR_VADDR H_PUT_32
#endif
#ifndef GET_SCNHDR_SIZE
#define GET_SCNHDR_SIZE H_GET_32
#endif
#ifndef PUT_SCNHDR_SIZE
#define PUT_SCNHDR_SIZE H_PUT_32
#endif
#ifndef GET_SCNHDR_SCNPTR
#define GET_SCNHDR_SCNPTR H_GET_32
#endif
#ifndef PUT_SCNHDR_SCNPTR
#define PUT_SCNHDR_SCNPTR H_PUT_32
#endif
#ifndef GET_SCNHDR_RELPTR
#define GET_SCNHDR_RELPTR H_GET_32
#endif
#ifndef PUT_SCNHDR_RELPTR
#define PUT_SCNHDR_RELPTR H_PUT_32
#endif
#ifndef GET_SCNHDR_LNNOPTR
#define GET_SCNHDR_LNNOPTR H_GET_32
#endif
#ifndef PUT_SCNHDR_LNNOPTR
#define PUT_SCNHDR_LNNOPTR H_PUT_32
#endif

#ifdef COFF_WITH_pex64

#define GET_OPTHDR_IMAGE_BASE            H_GET_64
#define PUT_OPTHDR_IMAGE_BASE            H_PUT_64
#define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64
#define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64
#define GET_OPTHDR_SIZE_OF_STACK_COMMIT  H_GET_64
#define PUT_OPTHDR_SIZE_OF_STACK_COMMIT  H_PUT_64
#define GET_OPTHDR_SIZE_OF_HEAP_RESERVE  H_GET_64
#define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE  H_PUT_64
#define GET_OPTHDR_SIZE_OF_HEAP_COMMIT   H_GET_64
#define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT   H_PUT_64
#define GET_PDATA_ENTRY                  bfd_get_32

#define _bfd_XX_bfd_copy_private_bfd_data_common	_bfd_pex64_bfd_copy_private_bfd_data_common
#define _bfd_XX_bfd_copy_private_section_data		_bfd_pex64_bfd_copy_private_section_data
#define _bfd_XX_get_symbol_info				_bfd_pex64_get_symbol_info
#define _bfd_XX_only_swap_filehdr_out			_bfd_pex64_only_swap_filehdr_out
#define _bfd_XX_print_private_bfd_data_common		_bfd_pex64_print_private_bfd_data_common
#define _bfd_XXi_final_link_postscript			_bfd_pex64i_final_link_postscript
#define _bfd_XXi_only_swap_filehdr_out			_bfd_pex64i_only_swap_filehdr_out
#define _bfd_XXi_swap_aouthdr_in			_bfd_pex64i_swap_aouthdr_in
#define _bfd_XXi_swap_aouthdr_out			_bfd_pex64i_swap_aouthdr_out
#define _bfd_XXi_swap_aux_in				_bfd_pex64i_swap_aux_in
#define _bfd_XXi_swap_aux_out				_bfd_pex64i_swap_aux_out
#define _bfd_XXi_swap_lineno_in				_bfd_pex64i_swap_lineno_in
#define _bfd_XXi_swap_lineno_out			_bfd_pex64i_swap_lineno_out
#define _bfd_XXi_swap_scnhdr_out			_bfd_pex64i_swap_scnhdr_out
#define _bfd_XXi_swap_sym_in				_bfd_pex64i_swap_sym_in
#define _bfd_XXi_swap_sym_out				_bfd_pex64i_swap_sym_out

#elif defined COFF_WITH_pep

#define GET_OPTHDR_IMAGE_BASE H_GET_64
#define PUT_OPTHDR_IMAGE_BASE H_PUT_64
#define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64
#define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64
#define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_64
#define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_64
#define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_64
#define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_64
#define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_64
#define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_64
#define GET_PDATA_ENTRY bfd_get_64

#define _bfd_XX_bfd_copy_private_bfd_data_common	_bfd_pep_bfd_copy_private_bfd_data_common
#define _bfd_XX_bfd_copy_private_section_data		_bfd_pep_bfd_copy_private_section_data
#define _bfd_XX_get_symbol_info				_bfd_pep_get_symbol_info
#define _bfd_XX_only_swap_filehdr_out			_bfd_pep_only_swap_filehdr_out
#define _bfd_XX_print_private_bfd_data_common		_bfd_pep_print_private_bfd_data_common
#define _bfd_XXi_final_link_postscript			_bfd_pepi_final_link_postscript
#define _bfd_XXi_only_swap_filehdr_out			_bfd_pepi_only_swap_filehdr_out
#define _bfd_XXi_swap_aouthdr_in			_bfd_pepi_swap_aouthdr_in
#define _bfd_XXi_swap_aouthdr_out			_bfd_pepi_swap_aouthdr_out
#define _bfd_XXi_swap_aux_in				_bfd_pepi_swap_aux_in
#define _bfd_XXi_swap_aux_out				_bfd_pepi_swap_aux_out
#define _bfd_XXi_swap_lineno_in				_bfd_pepi_swap_lineno_in
#define _bfd_XXi_swap_lineno_out			_bfd_pepi_swap_lineno_out
#define _bfd_XXi_swap_scnhdr_out			_bfd_pepi_swap_scnhdr_out
#define _bfd_XXi_swap_sym_in				_bfd_pepi_swap_sym_in
#define _bfd_XXi_swap_sym_out				_bfd_pepi_swap_sym_out

#else /* !COFF_WITH_pep */

#define GET_OPTHDR_IMAGE_BASE H_GET_32
#define PUT_OPTHDR_IMAGE_BASE H_PUT_32
#define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_32
#define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_32
#define GET_OPTHDR_SIZE_OF_STACK_COMMIT H_GET_32
#define PUT_OPTHDR_SIZE_OF_STACK_COMMIT H_PUT_32
#define GET_OPTHDR_SIZE_OF_HEAP_RESERVE H_GET_32
#define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE H_PUT_32
#define GET_OPTHDR_SIZE_OF_HEAP_COMMIT H_GET_32
#define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT H_PUT_32
#define GET_PDATA_ENTRY bfd_get_32

#define _bfd_XX_bfd_copy_private_bfd_data_common	_bfd_pe_bfd_copy_private_bfd_data_common
#define _bfd_XX_bfd_copy_private_section_data		_bfd_pe_bfd_copy_private_section_data
#define _bfd_XX_get_symbol_info				_bfd_pe_get_symbol_info
#define _bfd_XX_only_swap_filehdr_out			_bfd_pe_only_swap_filehdr_out
#define _bfd_XX_print_private_bfd_data_common		_bfd_pe_print_private_bfd_data_common
#define _bfd_XXi_final_link_postscript			_bfd_pei_final_link_postscript
#define _bfd_XXi_only_swap_filehdr_out			_bfd_pei_only_swap_filehdr_out
#define _bfd_XXi_swap_aouthdr_in			_bfd_pei_swap_aouthdr_in
#define _bfd_XXi_swap_aouthdr_out			_bfd_pei_swap_aouthdr_out
#define _bfd_XXi_swap_aux_in				_bfd_pei_swap_aux_in
#define _bfd_XXi_swap_aux_out				_bfd_pei_swap_aux_out
#define _bfd_XXi_swap_lineno_in				_bfd_pei_swap_lineno_in
#define _bfd_XXi_swap_lineno_out			_bfd_pei_swap_lineno_out
#define _bfd_XXi_swap_scnhdr_out			_bfd_pei_swap_scnhdr_out
#define _bfd_XXi_swap_sym_in				_bfd_pei_swap_sym_in
#define _bfd_XXi_swap_sym_out				_bfd_pei_swap_sym_out

#endif /* !COFF_WITH_pep */

/* Returns true if the target is a PE executable target.  */
#define bfd_target_pei_p(xvec)					\
  (CONST_STRNEQ ((xvec)->name, "pei-"))

/* Return the arch string of a PE executable target.  */
#define bfd_target_pei_arch(xvec)				\
  ((xvec)->name + sizeof ("pei-") - 1)

/* Returns true if the target is an EFI application target.  */
#define bfd_target_efi_app_p(xvec)				\
   (CONST_STRNEQ ((xvec)->name, "efi-app-"))

/* Return the arch string of an EFI application target.  */
#define bfd_target_efi_app_arch(xvec)				\
  ((xvec)->name + sizeof ("efi-app-") - 1)

/* Returns true if the target is an EFI Boot Service driver target.  */
#define bfd_target_efi_bsdrv_p(xvec)				\
   (CONST_STRNEQ ((xvec)->name, "efi-bsdrv-"))

/* Return the arch string of an EFI Boot Service driver target.  */
#define bfd_target_efi_bsdrv_arch(xvec)				\
  ((xvec)->name + sizeof ("efi-bsdrv-") - 1)

/* Returns true if the target is an EFI runtime driver target.  */
#define bfd_target_efi_rtdrv_p(xvec)				\
   (CONST_STRNEQ ((xvec)->name, "efi-rtdrv-"))

/* Return the arch string of an EFI runtime driver target.  */
#define bfd_target_efi_rtdrv_arch(xvec)				\
  ((xvec)->name + sizeof ("efi-rtdrv-") - 1)

/* Macro: Returns true if the bfd is a PE executable as opposed to a
   PE object file.  */
#define bfd_pe_executable_p(abfd)			\
  (   bfd_target_pei_p ((abfd)->xvec)			\
   || bfd_target_efi_app_p ((abfd)->xvec)		\
   || bfd_target_efi_bsdrv_p ((abfd)->xvec)		\
   || bfd_target_efi_rtdrv_p ((abfd)->xvec))

/* These functions are architecture dependent, and are in peicode.h:
   coff_swap_reloc_in
   int coff_swap_reloc_out
   coff_swap_filehdr_in
   coff_swap_scnhdr_in
   pe_mkobject
   pe_mkobject_hook  */

/* The functions described below are common across all PE/PEI
   implementations architecture types, and actually appear in
   peigen.c.  */

#define coff_swap_sym_in      _bfd_XXi_swap_sym_in
#define coff_swap_sym_out     _bfd_XXi_swap_sym_out
#define coff_swap_aux_in      _bfd_XXi_swap_aux_in
#define coff_swap_aux_out     _bfd_XXi_swap_aux_out
#define coff_swap_lineno_in   _bfd_XXi_swap_lineno_in
#define coff_swap_lineno_out  _bfd_XXi_swap_lineno_out
#define coff_swap_aouthdr_in  _bfd_XXi_swap_aouthdr_in
#define coff_swap_aouthdr_out _bfd_XXi_swap_aouthdr_out
#define coff_swap_scnhdr_out  _bfd_XXi_swap_scnhdr_out

#ifndef coff_final_link_postscript
#define coff_final_link_postscript _bfd_XXi_final_link_postscript
#endif

void        _bfd_XXi_swap_sym_in (bfd *, void *, void *);
unsigned    _bfd_XXi_swap_sym_out (bfd *, void *, void *);
void        _bfd_XXi_swap_aux_in (bfd *, void *, int, int, int, int, void *);
unsigned    _bfd_XXi_swap_aux_out (bfd *, void *, int, int, int, int, void *);
void        _bfd_XXi_swap_lineno_in (bfd *, void *, void *);
unsigned    _bfd_XXi_swap_lineno_out (bfd *, void *, void *);
void        _bfd_XXi_swap_aouthdr_in (bfd *, void *, void *);
unsigned    _bfd_XXi_swap_aouthdr_out (bfd *, void *, void *);
unsigned    _bfd_XXi_swap_scnhdr_out (bfd *, void *, void *);
bfd_boolean _bfd_XX_print_private_bfd_data_common (bfd *, void *);
bfd_boolean _bfd_XX_bfd_copy_private_bfd_data_common (bfd *, bfd *);
void        _bfd_XX_get_symbol_info (bfd *, asymbol *, symbol_info *);
bfd_boolean _bfd_XXi_final_link_postscript (bfd *, struct coff_final_link_info *);

/* The following are needed only for ONE of pe or pei, but don't
   otherwise vary; peicode.h fixes up ifdefs but we provide the
   prototype.  */

unsigned    _bfd_XX_only_swap_filehdr_out  (bfd *, void *, void *);
unsigned    _bfd_XXi_only_swap_filehdr_out (bfd *, void *, void *);
bfd_boolean _bfd_XX_bfd_copy_private_section_data (bfd *, asection *, bfd *, asection *);

bfd_boolean _bfd_pe_print_ce_compressed_pdata (bfd *, void *);
bfd_boolean _bfd_pe64_print_ce_compressed_pdata (bfd *, void *);
bfd_boolean _bfd_pex64_print_ce_compressed_pdata (bfd *, void *);
bfd_boolean _bfd_pep_print_ce_compressed_pdata (bfd *, void *);