peicode.h revision 1.1.1.8 1 1.1 christos /* Support for the generic parts of PE/PEI, for BFD.
2 1.1.1.8 christos Copyright (C) 1995-2022 Free Software Foundation, Inc.
3 1.1 christos Written by Cygnus Solutions.
4 1.1 christos
5 1.1 christos This file is part of BFD, the Binary File Descriptor library.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software
19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 1.1 christos MA 02110-1301, USA. */
21 1.1 christos
22 1.1 christos
23 1.1 christos /* Most of this hacked by Steve Chamberlain,
24 1.1 christos sac (at) cygnus.com
25 1.1 christos
26 1.1 christos PE/PEI rearrangement (and code added): Donn Terry
27 1.1.1.6 christos Softway Systems, Inc. */
28 1.1 christos
29 1.1 christos /* Hey look, some documentation [and in a place you expect to find it]!
30 1.1 christos
31 1.1 christos The main reference for the pei format is "Microsoft Portable Executable
32 1.1 christos and Common Object File Format Specification 4.1". Get it if you need to
33 1.1 christos do some serious hacking on this code.
34 1.1 christos
35 1.1 christos Another reference:
36 1.1 christos "Peering Inside the PE: A Tour of the Win32 Portable Executable
37 1.1 christos File Format", MSJ 1994, Volume 9.
38 1.1 christos
39 1.1 christos The *sole* difference between the pe format and the pei format is that the
40 1.1 christos latter has an MSDOS 2.0 .exe header on the front that prints the message
41 1.1 christos "This app must be run under Windows." (or some such).
42 1.1 christos (FIXME: Whether that statement is *really* true or not is unknown.
43 1.1 christos Are there more subtle differences between pe and pei formats?
44 1.1 christos For now assume there aren't. If you find one, then for God sakes
45 1.1 christos document it here!)
46 1.1 christos
47 1.1 christos The Microsoft docs use the word "image" instead of "executable" because
48 1.1 christos the former can also refer to a DLL (shared library). Confusion can arise
49 1.1 christos because the `i' in `pei' also refers to "image". The `pe' format can
50 1.1 christos also create images (i.e. executables), it's just that to run on a win32
51 1.1 christos system you need to use the pei format.
52 1.1 christos
53 1.1 christos FIXME: Please add more docs here so the next poor fool that has to hack
54 1.1 christos on this code has a chance of getting something accomplished without
55 1.1 christos wasting too much time. */
56 1.1 christos
57 1.1 christos #include "libpei.h"
58 1.1 christos
59 1.1.1.8 christos static bool (*pe_saved_coff_bfd_print_private_bfd_data) (bfd *, void *) =
60 1.1 christos #ifndef coff_bfd_print_private_bfd_data
61 1.1 christos NULL;
62 1.1 christos #else
63 1.1 christos coff_bfd_print_private_bfd_data;
64 1.1 christos #undef coff_bfd_print_private_bfd_data
65 1.1 christos #endif
66 1.1 christos
67 1.1.1.8 christos static bool pe_print_private_bfd_data (bfd *, void *);
68 1.1 christos #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
69 1.1 christos
70 1.1.1.8 christos static bool (*pe_saved_coff_bfd_copy_private_bfd_data) (bfd *, bfd *) =
71 1.1 christos #ifndef coff_bfd_copy_private_bfd_data
72 1.1 christos NULL;
73 1.1 christos #else
74 1.1 christos coff_bfd_copy_private_bfd_data;
75 1.1 christos #undef coff_bfd_copy_private_bfd_data
76 1.1 christos #endif
77 1.1 christos
78 1.1.1.8 christos static bool pe_bfd_copy_private_bfd_data (bfd *, bfd *);
79 1.1 christos #define coff_bfd_copy_private_bfd_data pe_bfd_copy_private_bfd_data
80 1.1 christos
81 1.1.1.6 christos #define coff_mkobject pe_mkobject
82 1.1 christos #define coff_mkobject_hook pe_mkobject_hook
83 1.1 christos
84 1.1 christos #ifdef COFF_IMAGE_WITH_PE
85 1.1 christos /* This structure contains static variables used by the ILF code. */
86 1.1 christos typedef asection * asection_ptr;
87 1.1 christos
88 1.1 christos typedef struct
89 1.1 christos {
90 1.1 christos bfd * abfd;
91 1.1 christos bfd_byte * data;
92 1.1 christos struct bfd_in_memory * bim;
93 1.1.1.6 christos unsigned short magic;
94 1.1 christos
95 1.1 christos arelent * reltab;
96 1.1.1.6 christos unsigned int relcount;
97 1.1 christos
98 1.1.1.6 christos coff_symbol_type * sym_cache;
99 1.1.1.6 christos coff_symbol_type * sym_ptr;
100 1.1.1.6 christos unsigned int sym_index;
101 1.1 christos
102 1.1.1.6 christos unsigned int * sym_table;
103 1.1.1.6 christos unsigned int * table_ptr;
104 1.1 christos
105 1.1 christos combined_entry_type * native_syms;
106 1.1 christos combined_entry_type * native_ptr;
107 1.1 christos
108 1.1 christos coff_symbol_type ** sym_ptr_table;
109 1.1 christos coff_symbol_type ** sym_ptr_ptr;
110 1.1 christos
111 1.1 christos unsigned int sec_index;
112 1.1 christos
113 1.1.1.6 christos char * string_table;
114 1.1.1.6 christos char * string_ptr;
115 1.1 christos char * end_string_ptr;
116 1.1 christos
117 1.1.1.6 christos SYMENT * esym_table;
118 1.1.1.6 christos SYMENT * esym_ptr;
119 1.1 christos
120 1.1 christos struct internal_reloc * int_reltab;
121 1.1 christos }
122 1.1 christos pe_ILF_vars;
123 1.1 christos #endif /* COFF_IMAGE_WITH_PE */
124 1.1 christos
125 1.1.1.7 christos bfd_cleanup coff_real_object_p
126 1.1 christos (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
127 1.1 christos
128 1.1 christos #ifndef NO_COFF_RELOCS
130 1.1 christos static void
131 1.1 christos coff_swap_reloc_in (bfd * abfd, void * src, void * dst)
132 1.1 christos {
133 1.1 christos RELOC *reloc_src = (RELOC *) src;
134 1.1 christos struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
135 1.1 christos
136 1.1 christos reloc_dst->r_vaddr = H_GET_32 (abfd, reloc_src->r_vaddr);
137 1.1 christos reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
138 1.1 christos reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type);
139 1.1 christos #ifdef SWAP_IN_RELOC_OFFSET
140 1.1 christos reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET (abfd, reloc_src->r_offset);
141 1.1 christos #endif
142 1.1 christos }
143 1.1 christos
144 1.1 christos static unsigned int
145 1.1 christos coff_swap_reloc_out (bfd * abfd, void * src, void * dst)
146 1.1 christos {
147 1.1 christos struct internal_reloc *reloc_src = (struct internal_reloc *) src;
148 1.1 christos struct external_reloc *reloc_dst = (struct external_reloc *) dst;
149 1.1 christos
150 1.1 christos H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
151 1.1 christos H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
152 1.1 christos H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
153 1.1 christos
154 1.1 christos #ifdef SWAP_OUT_RELOC_OFFSET
155 1.1 christos SWAP_OUT_RELOC_OFFSET (abfd, reloc_src->r_offset, reloc_dst->r_offset);
156 1.1 christos #endif
157 1.1 christos #ifdef SWAP_OUT_RELOC_EXTRA
158 1.1 christos SWAP_OUT_RELOC_EXTRA (abfd, reloc_src, reloc_dst);
159 1.1 christos #endif
160 1.1 christos return RELSZ;
161 1.1 christos }
162 1.1 christos #endif /* not NO_COFF_RELOCS */
163 1.1 christos
164 1.1 christos #ifdef COFF_IMAGE_WITH_PE
165 1.1 christos #undef FILHDR
166 1.1 christos #define FILHDR struct external_PEI_IMAGE_hdr
167 1.1 christos #endif
168 1.1 christos
169 1.1 christos static void
170 1.1 christos coff_swap_filehdr_in (bfd * abfd, void * src, void * dst)
171 1.1 christos {
172 1.1 christos FILHDR *filehdr_src = (FILHDR *) src;
173 1.1 christos struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
174 1.1 christos
175 1.1 christos filehdr_dst->f_magic = H_GET_16 (abfd, filehdr_src->f_magic);
176 1.1 christos filehdr_dst->f_nscns = H_GET_16 (abfd, filehdr_src->f_nscns);
177 1.1 christos filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->f_timdat);
178 1.1 christos filehdr_dst->f_nsyms = H_GET_32 (abfd, filehdr_src->f_nsyms);
179 1.1 christos filehdr_dst->f_flags = H_GET_16 (abfd, filehdr_src->f_flags);
180 1.1 christos filehdr_dst->f_symptr = H_GET_32 (abfd, filehdr_src->f_symptr);
181 1.1 christos
182 1.1 christos /* Other people's tools sometimes generate headers with an nsyms but
183 1.1 christos a zero symptr. */
184 1.1 christos if (filehdr_dst->f_nsyms != 0 && filehdr_dst->f_symptr == 0)
185 1.1 christos {
186 1.1 christos filehdr_dst->f_nsyms = 0;
187 1.1 christos filehdr_dst->f_flags |= F_LSYMS;
188 1.1 christos }
189 1.1 christos
190 1.1 christos filehdr_dst->f_opthdr = H_GET_16 (abfd, filehdr_src-> f_opthdr);
191 1.1 christos }
192 1.1 christos
193 1.1 christos #ifdef COFF_IMAGE_WITH_PE
194 1.1.1.8 christos # define coff_swap_filehdr_out _bfd_XXi_only_swap_filehdr_out
195 1.1.1.8 christos #elif defined COFF_WITH_peAArch64
196 1.1 christos # define coff_swap_filehdr_out _bfd_XX_only_swap_filehdr_out
197 1.1 christos #elif defined COFF_WITH_pex64
198 1.1 christos # define coff_swap_filehdr_out _bfd_pex64_only_swap_filehdr_out
199 1.1 christos #elif defined COFF_WITH_pep
200 1.1 christos # define coff_swap_filehdr_out _bfd_pep_only_swap_filehdr_out
201 1.1 christos #else
202 1.1 christos # define coff_swap_filehdr_out _bfd_pe_only_swap_filehdr_out
203 1.1 christos #endif
204 1.1 christos
205 1.1 christos static void
206 1.1 christos coff_swap_scnhdr_in (bfd * abfd, void * ext, void * in)
207 1.1 christos {
208 1.1 christos SCNHDR *scnhdr_ext = (SCNHDR *) ext;
209 1.1 christos struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
210 1.1 christos
211 1.1 christos memcpy (scnhdr_int->s_name, scnhdr_ext->s_name, sizeof (scnhdr_int->s_name));
212 1.1 christos
213 1.1 christos scnhdr_int->s_vaddr = GET_SCNHDR_VADDR (abfd, scnhdr_ext->s_vaddr);
214 1.1 christos scnhdr_int->s_paddr = GET_SCNHDR_PADDR (abfd, scnhdr_ext->s_paddr);
215 1.1 christos scnhdr_int->s_size = GET_SCNHDR_SIZE (abfd, scnhdr_ext->s_size);
216 1.1 christos scnhdr_int->s_scnptr = GET_SCNHDR_SCNPTR (abfd, scnhdr_ext->s_scnptr);
217 1.1 christos scnhdr_int->s_relptr = GET_SCNHDR_RELPTR (abfd, scnhdr_ext->s_relptr);
218 1.1 christos scnhdr_int->s_lnnoptr = GET_SCNHDR_LNNOPTR (abfd, scnhdr_ext->s_lnnoptr);
219 1.1 christos scnhdr_int->s_flags = H_GET_32 (abfd, scnhdr_ext->s_flags);
220 1.1 christos
221 1.1 christos /* MS handles overflow of line numbers by carrying into the reloc
222 1.1 christos field (it appears). Since it's supposed to be zero for PE
223 1.1 christos *IMAGE* format, that's safe. This is still a bit iffy. */
224 1.1 christos #ifdef COFF_IMAGE_WITH_PE
225 1.1 christos scnhdr_int->s_nlnno = (H_GET_16 (abfd, scnhdr_ext->s_nlnno)
226 1.1 christos + (H_GET_16 (abfd, scnhdr_ext->s_nreloc) << 16));
227 1.1 christos scnhdr_int->s_nreloc = 0;
228 1.1 christos #else
229 1.1 christos scnhdr_int->s_nreloc = H_GET_16 (abfd, scnhdr_ext->s_nreloc);
230 1.1 christos scnhdr_int->s_nlnno = H_GET_16 (abfd, scnhdr_ext->s_nlnno);
231 1.1 christos #endif
232 1.1 christos
233 1.1 christos if (scnhdr_int->s_vaddr != 0)
234 1.1 christos {
235 1.1 christos scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase;
236 1.1.1.8 christos /* Do not cut upper 32-bits for 64-bit vma. */
237 1.1 christos #if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
238 1.1 christos scnhdr_int->s_vaddr &= 0xffffffff;
239 1.1 christos #endif
240 1.1 christos }
241 1.1 christos
242 1.1 christos #ifndef COFF_NO_HACK_SCNHDR_SIZE
243 1.1 christos /* If this section holds uninitialized data and is from an object file
244 1.1 christos or from an executable image that has not initialized the field,
245 1.1 christos or if the image is an executable file and the physical size is padded,
246 1.1 christos use the virtual size (stored in s_paddr) instead. */
247 1.1 christos if (scnhdr_int->s_paddr > 0
248 1.1 christos && (((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0
249 1.1.1.6 christos && (! bfd_pei_p (abfd) || scnhdr_int->s_size == 0))
250 1.1 christos || (bfd_pei_p (abfd) && (scnhdr_int->s_size > scnhdr_int->s_paddr))))
251 1.1 christos /* This code used to set scnhdr_int->s_paddr to 0. However,
252 1.1 christos coff_set_alignment_hook stores s_paddr in virt_size, which
253 1.1 christos only works if it correctly holds the virtual size of the
254 1.1 christos section. */
255 1.1 christos scnhdr_int->s_size = scnhdr_int->s_paddr;
256 1.1 christos #endif
257 1.1 christos }
258 1.1.1.8 christos
259 1.1 christos static bool
260 1.1 christos pe_mkobject (bfd * abfd)
261 1.1 christos {
262 1.1.1.7 christos pe_data_type *pe;
263 1.1 christos size_t amt = sizeof (pe_data_type);
264 1.1 christos
265 1.1 christos abfd->tdata.pe_obj_data = (struct pe_tdata *) bfd_zalloc (abfd, amt);
266 1.1 christos
267 1.1.1.8 christos if (abfd->tdata.pe_obj_data == 0)
268 1.1 christos return false;
269 1.1 christos
270 1.1 christos pe = pe_data (abfd);
271 1.1 christos
272 1.1 christos pe->coff.pe = 1;
273 1.1 christos
274 1.1 christos /* in_reloc_p is architecture dependent. */
275 1.1 christos pe->in_reloc_p = in_reloc_p;
276 1.1.1.7 christos
277 1.1.1.7 christos /* Default DOS message string. */
278 1.1.1.7 christos pe->dos_message[0] = 0x0eba1f0e;
279 1.1.1.7 christos pe->dos_message[1] = 0xcd09b400;
280 1.1.1.7 christos pe->dos_message[2] = 0x4c01b821;
281 1.1.1.7 christos pe->dos_message[3] = 0x685421cd;
282 1.1.1.7 christos pe->dos_message[4] = 0x70207369;
283 1.1.1.7 christos pe->dos_message[5] = 0x72676f72;
284 1.1.1.7 christos pe->dos_message[6] = 0x63206d61;
285 1.1.1.7 christos pe->dos_message[7] = 0x6f6e6e61;
286 1.1.1.7 christos pe->dos_message[8] = 0x65622074;
287 1.1.1.7 christos pe->dos_message[9] = 0x6e757220;
288 1.1.1.7 christos pe->dos_message[10] = 0x206e6920;
289 1.1.1.7 christos pe->dos_message[11] = 0x20534f44;
290 1.1.1.7 christos pe->dos_message[12] = 0x65646f6d;
291 1.1.1.7 christos pe->dos_message[13] = 0x0a0d0d2e;
292 1.1.1.7 christos pe->dos_message[14] = 0x24;
293 1.1.1.7 christos pe->dos_message[15] = 0x0;
294 1.1.1.2 christos
295 1.1.1.8 christos memset (& pe->pe_opthdr, 0, sizeof pe->pe_opthdr);
296 1.1 christos return true;
297 1.1 christos }
298 1.1 christos
299 1.1 christos /* Create the COFF backend specific information. */
300 1.1 christos
301 1.1 christos static void *
302 1.1 christos pe_mkobject_hook (bfd * abfd,
303 1.1 christos void * filehdr,
304 1.1 christos void * aouthdr ATTRIBUTE_UNUSED)
305 1.1 christos {
306 1.1 christos struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
307 1.1 christos pe_data_type *pe;
308 1.1 christos
309 1.1 christos if (! pe_mkobject (abfd))
310 1.1 christos return NULL;
311 1.1 christos
312 1.1 christos pe = pe_data (abfd);
313 1.1 christos pe->coff.sym_filepos = internal_f->f_symptr;
314 1.1 christos /* These members communicate important constants about the symbol
315 1.1 christos table to GDB's symbol-reading code. These `constants'
316 1.1 christos unfortunately vary among coff implementations... */
317 1.1 christos pe->coff.local_n_btmask = N_BTMASK;
318 1.1 christos pe->coff.local_n_btshft = N_BTSHFT;
319 1.1 christos pe->coff.local_n_tmask = N_TMASK;
320 1.1 christos pe->coff.local_n_tshift = N_TSHIFT;
321 1.1 christos pe->coff.local_symesz = SYMESZ;
322 1.1 christos pe->coff.local_auxesz = AUXESZ;
323 1.1 christos pe->coff.local_linesz = LINESZ;
324 1.1 christos
325 1.1 christos pe->coff.timestamp = internal_f->f_timdat;
326 1.1 christos
327 1.1 christos obj_raw_syment_count (abfd) =
328 1.1 christos obj_conv_table_size (abfd) =
329 1.1 christos internal_f->f_nsyms;
330 1.1 christos
331 1.1 christos pe->real_flags = internal_f->f_flags;
332 1.1 christos
333 1.1 christos if ((internal_f->f_flags & F_DLL) != 0)
334 1.1 christos pe->dll = 1;
335 1.1 christos
336 1.1 christos if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
337 1.1 christos abfd->flags |= HAS_DEBUG;
338 1.1 christos
339 1.1 christos #ifdef COFF_IMAGE_WITH_PE
340 1.1 christos if (aouthdr)
341 1.1 christos pe->pe_opthdr = ((struct internal_aouthdr *) aouthdr)->pe;
342 1.1 christos #endif
343 1.1 christos
344 1.1 christos #ifdef ARM
345 1.1 christos if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
346 1.1 christos coff_data (abfd) ->flags = 0;
347 1.1 christos #endif
348 1.1.1.7 christos
349 1.1.1.7 christos memcpy (pe->dos_message, internal_f->pe.dos_message,
350 1.1.1.7 christos sizeof (pe->dos_message));
351 1.1 christos
352 1.1 christos return (void *) pe;
353 1.1 christos }
354 1.1.1.8 christos
355 1.1 christos static bool
356 1.1 christos pe_print_private_bfd_data (bfd *abfd, void * vfile)
357 1.1 christos {
358 1.1 christos FILE *file = (FILE *) vfile;
359 1.1 christos
360 1.1.1.8 christos if (!_bfd_XX_print_private_bfd_data_common (abfd, vfile))
361 1.1 christos return false;
362 1.1 christos
363 1.1.1.8 christos if (pe_saved_coff_bfd_print_private_bfd_data == NULL)
364 1.1 christos return true;
365 1.1 christos
366 1.1 christos fputc ('\n', file);
367 1.1 christos
368 1.1 christos return pe_saved_coff_bfd_print_private_bfd_data (abfd, vfile);
369 1.1 christos }
370 1.1 christos
371 1.1 christos /* Copy any private info we understand from the input bfd
372 1.1 christos to the output bfd. */
373 1.1.1.8 christos
374 1.1 christos static bool
375 1.1 christos pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
376 1.1 christos {
377 1.1 christos /* PR binutils/716: Copy the large address aware flag.
378 1.1 christos XXX: Should we be copying other flags or other fields in the pe_data()
379 1.1 christos structure ? */
380 1.1 christos if (pe_data (obfd) != NULL
381 1.1 christos && pe_data (ibfd) != NULL
382 1.1 christos && pe_data (ibfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
383 1.1 christos pe_data (obfd)->real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
384 1.1 christos
385 1.1.1.8 christos if (!_bfd_XX_bfd_copy_private_bfd_data_common (ibfd, obfd))
386 1.1 christos return false;
387 1.1 christos
388 1.1 christos if (pe_saved_coff_bfd_copy_private_bfd_data)
389 1.1 christos return pe_saved_coff_bfd_copy_private_bfd_data (ibfd, obfd);
390 1.1.1.8 christos
391 1.1 christos return true;
392 1.1 christos }
393 1.1 christos
394 1.1 christos #define coff_bfd_copy_private_section_data \
395 1.1 christos _bfd_XX_bfd_copy_private_section_data
396 1.1 christos
397 1.1 christos #define coff_get_symbol_info _bfd_XX_get_symbol_info
398 1.1 christos
399 1.1 christos #ifdef COFF_IMAGE_WITH_PE
400 1.1 christos
401 1.1 christos /* Code to handle Microsoft's Image Library Format.
403 1.1 christos Also known as LINK6 format.
404 1.1 christos Documentation about this format can be found at:
405 1.1 christos
406 1.1 christos http://msdn.microsoft.com/library/specs/pecoff_section8.htm */
407 1.1 christos
408 1.1 christos /* The following constants specify the sizes of the various data
409 1.1 christos structures that we have to create in order to build a bfd describing
410 1.1 christos an ILF object file. The final "+ 1" in the definitions of SIZEOF_IDATA6
411 1.1 christos and SIZEOF_IDATA7 below is to allow for the possibility that we might
412 1.1 christos need a padding byte in order to ensure 16 bit alignment for the section's
413 1.1 christos contents.
414 1.1 christos
415 1.1 christos The value for SIZEOF_ILF_STRINGS is computed as follows:
416 1.1 christos
417 1.1 christos There will be NUM_ILF_SECTIONS section symbols. Allow 9 characters
418 1.1 christos per symbol for their names (longest section name is .idata$x).
419 1.1 christos
420 1.1 christos There will be two symbols for the imported value, one the symbol name
421 1.1 christos and one with _imp__ prefixed. Allowing for the terminating nul's this
422 1.1 christos is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll).
423 1.1 christos
424 1.1 christos The strings in the string table must start STRING__SIZE_SIZE bytes into
425 1.1 christos the table in order to for the string lookup code in coffgen/coffcode to
426 1.1.1.6 christos work. */
427 1.1.1.6 christos #define NUM_ILF_RELOCS 8
428 1.1 christos #define NUM_ILF_SECTIONS 6
429 1.1 christos #define NUM_ILF_SYMS (2 + NUM_ILF_SECTIONS)
430 1.1 christos
431 1.1 christos #define SIZEOF_ILF_SYMS (NUM_ILF_SYMS * sizeof (* vars.sym_cache))
432 1.1 christos #define SIZEOF_ILF_SYM_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_table))
433 1.1 christos #define SIZEOF_ILF_NATIVE_SYMS (NUM_ILF_SYMS * sizeof (* vars.native_syms))
434 1.1 christos #define SIZEOF_ILF_SYM_PTR_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_ptr_table))
435 1.1 christos #define SIZEOF_ILF_EXT_SYMS (NUM_ILF_SYMS * sizeof (* vars.esym_table))
436 1.1 christos #define SIZEOF_ILF_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.reltab))
437 1.1 christos #define SIZEOF_ILF_INT_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.int_reltab))
438 1.1 christos #define SIZEOF_ILF_STRINGS (strlen (symbol_name) * 2 + 8 \
439 1.1 christos + 21 + strlen (source_dll) \
440 1.1 christos + NUM_ILF_SECTIONS * 9 \
441 1.1 christos + STRING_SIZE_SIZE)
442 1.1 christos #define SIZEOF_IDATA2 (5 * 4)
443 1.1 christos
444 1.1 christos /* For PEx64 idata4 & 5 have thumb size of 8 bytes. */
445 1.1 christos #ifdef COFF_WITH_pex64
446 1.1 christos #define SIZEOF_IDATA4 (2 * 4)
447 1.1 christos #define SIZEOF_IDATA5 (2 * 4)
448 1.1 christos #else
449 1.1 christos #define SIZEOF_IDATA4 (1 * 4)
450 1.1 christos #define SIZEOF_IDATA5 (1 * 4)
451 1.1 christos #endif
452 1.1 christos
453 1.1.1.6 christos #define SIZEOF_IDATA6 (2 + strlen (symbol_name) + 1 + 1)
454 1.1 christos #define SIZEOF_IDATA7 (strlen (source_dll) + 1 + 1)
455 1.1 christos #define SIZEOF_ILF_SECTIONS (NUM_ILF_SECTIONS * sizeof (struct coff_section_tdata))
456 1.1 christos
457 1.1 christos #define ILF_DATA_SIZE \
458 1.1 christos + SIZEOF_ILF_SYMS \
459 1.1 christos + SIZEOF_ILF_SYM_TABLE \
460 1.1 christos + SIZEOF_ILF_NATIVE_SYMS \
461 1.1 christos + SIZEOF_ILF_SYM_PTR_TABLE \
462 1.1 christos + SIZEOF_ILF_EXT_SYMS \
463 1.1 christos + SIZEOF_ILF_RELOCS \
464 1.1 christos + SIZEOF_ILF_INT_RELOCS \
465 1.1 christos + SIZEOF_ILF_STRINGS \
466 1.1 christos + SIZEOF_IDATA2 \
467 1.1 christos + SIZEOF_IDATA4 \
468 1.1 christos + SIZEOF_IDATA5 \
469 1.1 christos + SIZEOF_IDATA6 \
470 1.1 christos + SIZEOF_IDATA7 \
471 1.1 christos + SIZEOF_ILF_SECTIONS \
472 1.1 christos + MAX_TEXT_SECTION_SIZE
473 1.1 christos
474 1.1 christos /* Create an empty relocation against the given symbol. */
475 1.1.1.6 christos
476 1.1.1.6 christos static void
477 1.1.1.6 christos pe_ILF_make_a_symbol_reloc (pe_ILF_vars * vars,
478 1.1.1.6 christos bfd_vma address,
479 1.1.1.6 christos bfd_reloc_code_real_type reloc,
480 1.1 christos struct bfd_symbol ** sym,
481 1.1 christos unsigned int sym_index)
482 1.1 christos {
483 1.1 christos arelent * entry;
484 1.1 christos struct internal_reloc * internal;
485 1.1 christos
486 1.1 christos entry = vars->reltab + vars->relcount;
487 1.1 christos internal = vars->int_reltab + vars->relcount;
488 1.1 christos
489 1.1 christos entry->address = address;
490 1.1 christos entry->addend = 0;
491 1.1 christos entry->howto = bfd_reloc_type_lookup (vars->abfd, reloc);
492 1.1 christos entry->sym_ptr_ptr = sym;
493 1.1 christos
494 1.1.1.8 christos internal->r_vaddr = address;
495 1.1 christos internal->r_symndx = sym_index;
496 1.1 christos internal->r_type = entry->howto ? entry->howto->type : 0;
497 1.1 christos
498 1.1 christos vars->relcount ++;
499 1.1 christos
500 1.1 christos BFD_ASSERT (vars->relcount <= NUM_ILF_RELOCS);
501 1.1 christos }
502 1.1 christos
503 1.1 christos /* Create an empty relocation against the given section. */
504 1.1.1.6 christos
505 1.1.1.6 christos static void
506 1.1 christos pe_ILF_make_a_reloc (pe_ILF_vars * vars,
507 1.1.1.6 christos bfd_vma address,
508 1.1 christos bfd_reloc_code_real_type reloc,
509 1.1 christos asection_ptr sec)
510 1.1 christos {
511 1.1 christos pe_ILF_make_a_symbol_reloc (vars, address, reloc, sec->symbol_ptr_ptr,
512 1.1 christos coff_section_data (vars->abfd, sec)->i);
513 1.1 christos }
514 1.1 christos
515 1.1 christos /* Move the queued relocs into the given section. */
516 1.1 christos
517 1.1 christos static void
518 1.1 christos pe_ILF_save_relocs (pe_ILF_vars * vars,
519 1.1 christos asection_ptr sec)
520 1.1 christos {
521 1.1 christos /* Make sure that there is somewhere to store the internal relocs. */
522 1.1 christos if (coff_section_data (vars->abfd, sec) == NULL)
523 1.1 christos /* We should probably return an error indication here. */
524 1.1 christos abort ();
525 1.1.1.8 christos
526 1.1 christos coff_section_data (vars->abfd, sec)->relocs = vars->int_reltab;
527 1.1 christos coff_section_data (vars->abfd, sec)->keep_relocs = true;
528 1.1 christos
529 1.1 christos sec->relocation = vars->reltab;
530 1.1 christos sec->reloc_count = vars->relcount;
531 1.1 christos sec->flags |= SEC_RELOC;
532 1.1 christos
533 1.1 christos vars->reltab += vars->relcount;
534 1.1 christos vars->int_reltab += vars->relcount;
535 1.1 christos vars->relcount = 0;
536 1.1 christos
537 1.1 christos BFD_ASSERT ((bfd_byte *) vars->int_reltab < (bfd_byte *) vars->string_table);
538 1.1 christos }
539 1.1 christos
540 1.1 christos /* Create a global symbol and add it to the relevant tables. */
541 1.1 christos
542 1.1 christos static void
543 1.1 christos pe_ILF_make_a_symbol (pe_ILF_vars * vars,
544 1.1 christos const char * prefix,
545 1.1 christos const char * symbol_name,
546 1.1 christos asection_ptr section,
547 1.1 christos flagword extra_flags)
548 1.1 christos {
549 1.1 christos coff_symbol_type * sym;
550 1.1 christos combined_entry_type * ent;
551 1.1 christos SYMENT * esym;
552 1.1 christos unsigned short sclass;
553 1.1 christos
554 1.1 christos if (extra_flags & BSF_LOCAL)
555 1.1 christos sclass = C_STAT;
556 1.1 christos else
557 1.1 christos sclass = C_EXT;
558 1.1 christos
559 1.1 christos #ifdef THUMBPEMAGIC
560 1.1 christos if (vars->magic == THUMBPEMAGIC)
561 1.1 christos {
562 1.1 christos if (extra_flags & BSF_FUNCTION)
563 1.1 christos sclass = C_THUMBEXTFUNC;
564 1.1 christos else if (extra_flags & BSF_LOCAL)
565 1.1 christos sclass = C_THUMBSTAT;
566 1.1 christos else
567 1.1 christos sclass = C_THUMBEXT;
568 1.1 christos }
569 1.1 christos #endif
570 1.1 christos
571 1.1 christos BFD_ASSERT (vars->sym_index < NUM_ILF_SYMS);
572 1.1 christos
573 1.1 christos sym = vars->sym_ptr;
574 1.1 christos ent = vars->native_ptr;
575 1.1 christos esym = vars->esym_ptr;
576 1.1 christos
577 1.1 christos /* Copy the symbol's name into the string table. */
578 1.1 christos sprintf (vars->string_ptr, "%s%s", prefix, symbol_name);
579 1.1 christos
580 1.1 christos if (section == NULL)
581 1.1 christos section = bfd_und_section_ptr;
582 1.1 christos
583 1.1 christos /* Initialise the external symbol. */
584 1.1 christos H_PUT_32 (vars->abfd, vars->string_ptr - vars->string_table,
585 1.1 christos esym->e.e.e_offset);
586 1.1 christos H_PUT_16 (vars->abfd, section->target_index, esym->e_scnum);
587 1.1 christos esym->e_sclass[0] = sclass;
588 1.1 christos
589 1.1 christos /* The following initialisations are unnecessary - the memory is
590 1.1 christos zero initialised. They are just kept here as reminders. */
591 1.1.1.6 christos
592 1.1.1.6 christos /* Initialise the internal symbol structure. */
593 1.1.1.8 christos ent->u.syment.n_sclass = sclass;
594 1.1.1.8 christos ent->u.syment.n_scnum = section->target_index;
595 1.1 christos ent->u.syment._n._n_n._n_offset = (uintptr_t) sym;
596 1.1 christos ent->is_sym = true;
597 1.1 christos
598 1.1 christos sym->symbol.the_bfd = vars->abfd;
599 1.1 christos sym->symbol.name = vars->string_ptr;
600 1.1.1.6 christos sym->symbol.flags = BSF_EXPORT | BSF_GLOBAL | extra_flags;
601 1.1 christos sym->symbol.section = section;
602 1.1 christos sym->native = ent;
603 1.1 christos
604 1.1 christos * vars->table_ptr = vars->sym_index;
605 1.1 christos * vars->sym_ptr_ptr = sym;
606 1.1 christos
607 1.1 christos /* Adjust pointers for the next symbol. */
608 1.1 christos vars->sym_index ++;
609 1.1 christos vars->sym_ptr ++;
610 1.1 christos vars->sym_ptr_ptr ++;
611 1.1 christos vars->table_ptr ++;
612 1.1 christos vars->native_ptr ++;
613 1.1 christos vars->esym_ptr ++;
614 1.1 christos vars->string_ptr += strlen (symbol_name) + strlen (prefix) + 1;
615 1.1 christos
616 1.1 christos BFD_ASSERT (vars->string_ptr < vars->end_string_ptr);
617 1.1 christos }
618 1.1 christos
619 1.1 christos /* Create a section. */
620 1.1 christos
621 1.1 christos static asection_ptr
622 1.1 christos pe_ILF_make_a_section (pe_ILF_vars * vars,
623 1.1 christos const char * name,
624 1.1 christos unsigned int size,
625 1.1 christos flagword extra_flags)
626 1.1 christos {
627 1.1.1.7 christos asection_ptr sec;
628 1.1 christos flagword flags;
629 1.1 christos intptr_t alignment;
630 1.1 christos
631 1.1 christos sec = bfd_make_section_old_way (vars->abfd, name);
632 1.1 christos if (sec == NULL)
633 1.1 christos return NULL;
634 1.1 christos
635 1.1.1.7 christos flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_IN_MEMORY;
636 1.1 christos
637 1.1.1.7 christos bfd_set_section_flags (sec, flags | extra_flags);
638 1.1 christos
639 1.1 christos bfd_set_section_alignment (sec, 2);
640 1.1 christos
641 1.1 christos /* Check that we will not run out of space. */
642 1.1 christos BFD_ASSERT (vars->data + size < vars->bim->buffer + vars->bim->size);
643 1.1 christos
644 1.1.1.7 christos /* Set the section size and contents. The actual
645 1.1 christos contents are filled in by our parent. */
646 1.1 christos bfd_set_section_size (sec, (bfd_size_type) size);
647 1.1 christos sec->contents = vars->data;
648 1.1 christos sec->target_index = vars->sec_index ++;
649 1.1 christos
650 1.1 christos /* Advance data pointer in the vars structure. */
651 1.1 christos vars->data += size;
652 1.1 christos
653 1.1 christos /* Skip the padding byte if it was not needed.
654 1.1 christos The logic here is that if the string length is odd,
655 1.1 christos then the entire string length, including the null byte,
656 1.1 christos is even and so the extra, padding byte, is not needed. */
657 1.1 christos if (size & 1)
658 1.1.1.4 christos vars->data --;
659 1.1.1.7 christos
660 1.1.1.7 christos /* PR 18758: See note in pe_ILF_buid_a_bfd. We must make sure that we
661 1.1.1.7 christos preserve host alignment requirements. The BFD_ASSERTs in this
662 1.1.1.7 christos functions will warn us if we run out of room, but we should
663 1.1.1.7 christos already have enough padding built in to ILF_DATA_SIZE. */
664 1.1.1.7 christos #if GCC_VERSION >= 3000
665 1.1.1.7 christos alignment = __alignof__ (struct coff_section_tdata);
666 1.1.1.4 christos #else
667 1.1.1.7 christos alignment = 8;
668 1.1.1.7 christos #endif
669 1.1.1.7 christos vars->data
670 1.1 christos = (bfd_byte *) (((intptr_t) vars->data + alignment - 1) & -alignment);
671 1.1 christos
672 1.1 christos /* Create a coff_section_tdata structure for our use. */
673 1.1 christos sec->used_by_bfd = (struct coff_section_tdata *) vars->data;
674 1.1 christos vars->data += sizeof (struct coff_section_tdata);
675 1.1 christos
676 1.1 christos BFD_ASSERT (vars->data <= vars->bim->buffer + vars->bim->size);
677 1.1 christos
678 1.1 christos /* Create a symbol to refer to this section. */
679 1.1 christos pe_ILF_make_a_symbol (vars, "", name, sec, BSF_LOCAL);
680 1.1 christos
681 1.1 christos /* Cache the index to the symbol in the coff_section_data structure. */
682 1.1 christos coff_section_data (vars->abfd, sec)->i = vars->sym_index - 1;
683 1.1 christos
684 1.1 christos return sec;
685 1.1 christos }
686 1.1 christos
687 1.1 christos /* This structure contains the code that goes into the .text section
688 1.1 christos in order to perform a jump into the DLL lookup table. The entries
689 1.1 christos in the table are index by the magic number used to represent the
690 1.1 christos machine type in the PE file. The contents of the data[] arrays in
691 1.1 christos these entries are stolen from the jtab[] arrays in ld/pe-dll.c.
692 1.1 christos The SIZE field says how many bytes in the DATA array are actually
693 1.1 christos used. The OFFSET field says where in the data array the address
694 1.1 christos of the .idata$5 section should be placed. */
695 1.1 christos #define MAX_TEXT_SECTION_SIZE 32
696 1.1 christos
697 1.1 christos typedef struct
698 1.1 christos {
699 1.1 christos unsigned short magic;
700 1.1 christos unsigned char data[MAX_TEXT_SECTION_SIZE];
701 1.1 christos unsigned int size;
702 1.1 christos unsigned int offset;
703 1.1 christos }
704 1.1.1.8 christos jump_table;
705 1.1 christos
706 1.1 christos static const jump_table jtab[] =
707 1.1 christos {
708 1.1 christos #ifdef I386MAGIC
709 1.1 christos { I386MAGIC,
710 1.1 christos { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
711 1.1 christos 8, 2
712 1.1 christos },
713 1.1 christos #endif
714 1.1 christos
715 1.1 christos #ifdef AMD64MAGIC
716 1.1 christos { AMD64MAGIC,
717 1.1 christos { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
718 1.1 christos 8, 2
719 1.1 christos },
720 1.1 christos #endif
721 1.1 christos
722 1.1 christos #ifdef MC68MAGIC
723 1.1 christos { MC68MAGIC,
724 1.1 christos { /* XXX fill me in */ },
725 1.1 christos 0, 0
726 1.1 christos },
727 1.1 christos #endif
728 1.1 christos
729 1.1 christos #ifdef MIPS_ARCH_MAGIC_WINCE
730 1.1 christos { MIPS_ARCH_MAGIC_WINCE,
731 1.1 christos { 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
732 1.1 christos 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },
733 1.1 christos 16, 0
734 1.1 christos },
735 1.1 christos #endif
736 1.1 christos
737 1.1 christos #ifdef SH_ARCH_MAGIC_WINCE
738 1.1 christos { SH_ARCH_MAGIC_WINCE,
739 1.1 christos { 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40,
740 1.1 christos 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 },
741 1.1 christos 12, 8
742 1.1 christos },
743 1.1.1.8 christos #endif
744 1.1.1.8 christos
745 1.1.1.8 christos #ifdef AARCH64MAGIC
746 1.1.1.8 christos /* We don't currently support jumping to DLLs, so if
747 1.1.1.8 christos someone does try emit a runtime trap. Through UDF #0. */
748 1.1.1.8 christos { AARCH64MAGIC,
749 1.1.1.8 christos { 0x00, 0x00, 0x00, 0x00 },
750 1.1.1.8 christos 4, 0
751 1.1.1.8 christos },
752 1.1.1.8 christos
753 1.1 christos #endif
754 1.1 christos
755 1.1 christos #ifdef ARMPEMAGIC
756 1.1 christos { ARMPEMAGIC,
757 1.1 christos { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
758 1.1 christos 0x9c, 0xe5, 0x00, 0x00, 0x00, 0x00},
759 1.1 christos 12, 8
760 1.1 christos },
761 1.1 christos #endif
762 1.1 christos
763 1.1 christos #ifdef THUMBPEMAGIC
764 1.1 christos { THUMBPEMAGIC,
765 1.1 christos { 0x40, 0xb4, 0x02, 0x4e, 0x36, 0x68, 0xb4, 0x46,
766 1.1 christos 0x40, 0xbc, 0x60, 0x47, 0x00, 0x00, 0x00, 0x00 },
767 1.1 christos 16, 12
768 1.1.1.8 christos },
769 1.1.1.8 christos #endif
770 1.1.1.8 christos
771 1.1.1.8 christos #ifdef LOONGARCH64MAGIC
772 1.1.1.8 christos /* We don't currently support jumping to DLLs, so if
773 1.1.1.8 christos someone does try emit a runtime trap. Through BREAK 0. */
774 1.1.1.8 christos { LOONGARCH64MAGIC,
775 1.1.1.8 christos { 0x00, 0x00, 0x2a, 0x00 },
776 1.1.1.8 christos 4, 0
777 1.1.1.8 christos },
778 1.1.1.8 christos
779 1.1 christos #endif
780 1.1 christos
781 1.1 christos { 0, { 0 }, 0, 0 }
782 1.1 christos };
783 1.1 christos
784 1.1 christos #ifndef NUM_ENTRIES
785 1.1 christos #define NUM_ENTRIES(a) (sizeof (a) / sizeof (a)[0])
786 1.1 christos #endif
787 1.1 christos
788 1.1.1.8 christos /* Build a full BFD from the information supplied in a ILF object. */
789 1.1.1.6 christos
790 1.1 christos static bool
791 1.1.1.6 christos pe_ILF_build_a_bfd (bfd * abfd,
792 1.1.1.6 christos unsigned int magic,
793 1.1 christos char * symbol_name,
794 1.1 christos char * source_dll,
795 1.1 christos unsigned int ordinal,
796 1.1.1.6 christos unsigned int types)
797 1.1.1.6 christos {
798 1.1 christos bfd_byte * ptr;
799 1.1.1.6 christos pe_ILF_vars vars;
800 1.1.1.6 christos struct internal_filehdr internal_f;
801 1.1.1.6 christos unsigned int import_type;
802 1.1.1.6 christos unsigned int import_name_type;
803 1.1.1.6 christos asection_ptr id4, id5, id6 = NULL, text = NULL;
804 1.1.1.7 christos coff_symbol_type ** imp_sym;
805 1.1 christos unsigned int imp_index;
806 1.1 christos intptr_t alignment;
807 1.1 christos
808 1.1 christos /* Decode and verify the types field of the ILF structure. */
809 1.1 christos import_type = types & 0x3;
810 1.1 christos import_name_type = (types & 0x1c) >> 2;
811 1.1 christos
812 1.1 christos switch (import_type)
813 1.1 christos {
814 1.1 christos case IMPORT_CODE:
815 1.1 christos case IMPORT_DATA:
816 1.1 christos break;
817 1.1 christos
818 1.1.1.5 christos case IMPORT_CONST:
819 1.1.1.6 christos /* XXX code yet to be written. */
820 1.1 christos /* xgettext:c-format */
821 1.1.1.8 christos _bfd_error_handler (_("%pB: unhandled import type; %x"),
822 1.1 christos abfd, import_type);
823 1.1 christos return false;
824 1.1.1.5 christos
825 1.1.1.6 christos default:
826 1.1 christos /* xgettext:c-format */
827 1.1.1.8 christos _bfd_error_handler (_("%pB: unrecognized import type; %x"),
828 1.1 christos abfd, import_type);
829 1.1 christos return false;
830 1.1 christos }
831 1.1 christos
832 1.1 christos switch (import_name_type)
833 1.1 christos {
834 1.1 christos case IMPORT_ORDINAL:
835 1.1 christos case IMPORT_NAME:
836 1.1 christos case IMPORT_NAME_NOPREFIX:
837 1.1 christos case IMPORT_NAME_UNDECORATE:
838 1.1 christos break;
839 1.1.1.5 christos
840 1.1.1.6 christos default:
841 1.1 christos /* xgettext:c-format */
842 1.1.1.8 christos _bfd_error_handler (_("%pB: unrecognized import name type; %x"),
843 1.1 christos abfd, import_name_type);
844 1.1 christos return false;
845 1.1 christos }
846 1.1 christos
847 1.1 christos /* Initialise local variables.
848 1.1 christos
849 1.1 christos Note these are kept in a structure rather than being
850 1.1 christos declared as statics since bfd frowns on global variables.
851 1.1 christos
852 1.1 christos We are going to construct the contents of the BFD in memory,
853 1.1 christos so allocate all the space that we will need right now. */
854 1.1 christos vars.bim
855 1.1.1.8 christos = (struct bfd_in_memory *) bfd_malloc ((bfd_size_type) sizeof (*vars.bim));
856 1.1 christos if (vars.bim == NULL)
857 1.1 christos return false;
858 1.1 christos
859 1.1 christos ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
860 1.1 christos vars.bim->buffer = ptr;
861 1.1 christos vars.bim->size = ILF_DATA_SIZE;
862 1.1 christos if (ptr == NULL)
863 1.1 christos goto error_return;
864 1.1 christos
865 1.1 christos /* Initialise the pointers to regions of the memory and the
866 1.1 christos other contents of the pe_ILF_vars structure as well. */
867 1.1 christos vars.sym_cache = (coff_symbol_type *) ptr;
868 1.1 christos vars.sym_ptr = (coff_symbol_type *) ptr;
869 1.1 christos vars.sym_index = 0;
870 1.1 christos ptr += SIZEOF_ILF_SYMS;
871 1.1 christos
872 1.1 christos vars.sym_table = (unsigned int *) ptr;
873 1.1 christos vars.table_ptr = (unsigned int *) ptr;
874 1.1 christos ptr += SIZEOF_ILF_SYM_TABLE;
875 1.1 christos
876 1.1 christos vars.native_syms = (combined_entry_type *) ptr;
877 1.1 christos vars.native_ptr = (combined_entry_type *) ptr;
878 1.1 christos ptr += SIZEOF_ILF_NATIVE_SYMS;
879 1.1 christos
880 1.1 christos vars.sym_ptr_table = (coff_symbol_type **) ptr;
881 1.1 christos vars.sym_ptr_ptr = (coff_symbol_type **) ptr;
882 1.1 christos ptr += SIZEOF_ILF_SYM_PTR_TABLE;
883 1.1 christos
884 1.1 christos vars.esym_table = (SYMENT *) ptr;
885 1.1 christos vars.esym_ptr = (SYMENT *) ptr;
886 1.1 christos ptr += SIZEOF_ILF_EXT_SYMS;
887 1.1 christos
888 1.1 christos vars.reltab = (arelent *) ptr;
889 1.1 christos vars.relcount = 0;
890 1.1 christos ptr += SIZEOF_ILF_RELOCS;
891 1.1 christos
892 1.1 christos vars.int_reltab = (struct internal_reloc *) ptr;
893 1.1 christos ptr += SIZEOF_ILF_INT_RELOCS;
894 1.1 christos
895 1.1 christos vars.string_table = (char *) ptr;
896 1.1 christos vars.string_ptr = (char *) ptr + STRING_SIZE_SIZE;
897 1.1 christos ptr += SIZEOF_ILF_STRINGS;
898 1.1 christos vars.end_string_ptr = (char *) ptr;
899 1.1 christos
900 1.1.1.7 christos /* The remaining space in bim->buffer is used
901 1.1.1.4 christos by the pe_ILF_make_a_section() function. */
902 1.1.1.7 christos
903 1.1.1.7 christos /* PR 18758: Make sure that the data area is sufficiently aligned for
904 1.1.1.7 christos struct coff_section_tdata. __alignof__ is a gcc extension, hence
905 1.1.1.7 christos the test of GCC_VERSION. For other compilers we assume 8 byte
906 1.1.1.7 christos alignment. */
907 1.1.1.7 christos #if GCC_VERSION >= 3000
908 1.1.1.7 christos alignment = __alignof__ (struct coff_section_tdata);
909 1.1.1.4 christos #else
910 1.1.1.7 christos alignment = 8;
911 1.1.1.4 christos #endif
912 1.1 christos ptr = (bfd_byte *) (((intptr_t) ptr + alignment - 1) & -alignment);
913 1.1 christos
914 1.1 christos vars.data = ptr;
915 1.1 christos vars.abfd = abfd;
916 1.1 christos vars.sec_index = 0;
917 1.1 christos vars.magic = magic;
918 1.1 christos
919 1.1 christos /* Create the initial .idata$<n> sections:
920 1.1 christos [.idata$2: Import Directory Table -- not needed]
921 1.1 christos .idata$4: Import Lookup Table
922 1.1 christos .idata$5: Import Address Table
923 1.1 christos
924 1.1 christos Note we do not create a .idata$3 section as this is
925 1.1 christos created for us by the linker script. */
926 1.1 christos id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0);
927 1.1 christos id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0);
928 1.1 christos if (id4 == NULL || id5 == NULL)
929 1.1 christos goto error_return;
930 1.1 christos
931 1.1 christos /* Fill in the contents of these sections. */
932 1.1 christos if (import_name_type == IMPORT_ORDINAL)
933 1.1.1.5 christos {
934 1.1.1.5 christos if (ordinal == 0)
935 1.1 christos /* See PR 20907 for a reproducer. */
936 1.1.1.8 christos goto error_return;
937 1.1 christos
938 1.1 christos #if defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64)
939 1.1 christos ((unsigned int *) id4->contents)[0] = ordinal;
940 1.1 christos ((unsigned int *) id4->contents)[1] = 0x80000000;
941 1.1 christos ((unsigned int *) id5->contents)[0] = ordinal;
942 1.1 christos ((unsigned int *) id5->contents)[1] = 0x80000000;
943 1.1 christos #else
944 1.1 christos * (unsigned int *) id4->contents = ordinal | 0x80000000;
945 1.1 christos * (unsigned int *) id5->contents = ordinal | 0x80000000;
946 1.1 christos #endif
947 1.1 christos }
948 1.1 christos else
949 1.1 christos {
950 1.1 christos char * symbol;
951 1.1 christos unsigned int len;
952 1.1 christos
953 1.1 christos /* Create .idata$6 - the Hint Name Table. */
954 1.1 christos id6 = pe_ILF_make_a_section (& vars, ".idata$6", SIZEOF_IDATA6, 0);
955 1.1 christos if (id6 == NULL)
956 1.1 christos goto error_return;
957 1.1 christos
958 1.1 christos /* If necessary, trim the import symbol name. */
959 1.1 christos symbol = symbol_name;
960 1.1 christos
961 1.1 christos /* As used by MS compiler, '_', '@', and '?' are alternative
962 1.1 christos forms of USER_LABEL_PREFIX, with '?' for c++ mangled names,
963 1.1 christos '@' used for fastcall (in C), '_' everywhere else. Only one
964 1.1 christos of these is used for a symbol. We strip this leading char for
965 1.1 christos IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the
966 1.1 christos PE COFF 6.0 spec (section 8.3, Import Name Type). */
967 1.1 christos
968 1.1 christos if (import_name_type != IMPORT_NAME)
969 1.1 christos {
970 1.1 christos char c = symbol[0];
971 1.1 christos
972 1.1 christos /* Check that we don't remove for targets with empty
973 1.1 christos USER_LABEL_PREFIX the leading underscore. */
974 1.1 christos if ((c == '_' && abfd->xvec->symbol_leading_char != 0)
975 1.1 christos || c == '@' || c == '?')
976 1.1 christos symbol++;
977 1.1 christos }
978 1.1 christos
979 1.1 christos len = strlen (symbol);
980 1.1 christos if (import_name_type == IMPORT_NAME_UNDECORATE)
981 1.1 christos {
982 1.1 christos /* Truncate at the first '@'. */
983 1.1 christos char *at = strchr (symbol, '@');
984 1.1 christos
985 1.1 christos if (at != NULL)
986 1.1 christos len = at - symbol;
987 1.1 christos }
988 1.1 christos
989 1.1 christos id6->contents[0] = ordinal & 0xff;
990 1.1 christos id6->contents[1] = ordinal >> 8;
991 1.1 christos
992 1.1 christos memcpy ((char *) id6->contents + 2, symbol, len);
993 1.1 christos id6->contents[len + 2] = '\0';
994 1.1 christos }
995 1.1 christos
996 1.1 christos if (import_name_type != IMPORT_ORDINAL)
997 1.1 christos {
998 1.1 christos pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
999 1.1 christos pe_ILF_save_relocs (&vars, id4);
1000 1.1 christos
1001 1.1 christos pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
1002 1.1 christos pe_ILF_save_relocs (&vars, id5);
1003 1.1.1.4 christos }
1004 1.1.1.4 christos
1005 1.1.1.4 christos /* Create an import symbol. */
1006 1.1.1.4 christos pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0);
1007 1.1.1.4 christos imp_sym = vars.sym_ptr_ptr - 1;
1008 1.1 christos imp_index = vars.sym_index - 1;
1009 1.1 christos
1010 1.1 christos /* Create extra sections depending upon the type of import we are dealing with. */
1011 1.1 christos switch (import_type)
1012 1.1 christos {
1013 1.1 christos int i;
1014 1.1.1.4 christos
1015 1.1.1.6 christos case IMPORT_CODE:
1016 1.1 christos /* CODE functions are special, in that they get a trampoline that
1017 1.1 christos jumps to the main import symbol. Create a .text section to hold it.
1018 1.1 christos First we need to look up its contents in the jump table. */
1019 1.1 christos for (i = NUM_ENTRIES (jtab); i--;)
1020 1.1 christos {
1021 1.1 christos if (jtab[i].size == 0)
1022 1.1 christos continue;
1023 1.1 christos if (jtab[i].magic == magic)
1024 1.1 christos break;
1025 1.1 christos }
1026 1.1 christos /* If we did not find a matching entry something is wrong. */
1027 1.1 christos if (i < 0)
1028 1.1 christos abort ();
1029 1.1 christos
1030 1.1 christos /* Create the .text section. */
1031 1.1 christos text = pe_ILF_make_a_section (& vars, ".text", jtab[i].size, SEC_CODE);
1032 1.1 christos if (text == NULL)
1033 1.1 christos goto error_return;
1034 1.1 christos
1035 1.1 christos /* Copy in the jump code. */
1036 1.1 christos memcpy (text->contents, jtab[i].data, jtab[i].size);
1037 1.1 christos
1038 1.1 christos /* Create a reloc for the data in the text section. */
1039 1.1 christos #ifdef MIPS_ARCH_MAGIC_WINCE
1040 1.1 christos if (magic == MIPS_ARCH_MAGIC_WINCE)
1041 1.1 christos {
1042 1.1 christos pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 0, BFD_RELOC_HI16_S,
1043 1.1 christos (struct bfd_symbol **) imp_sym,
1044 1.1 christos imp_index);
1045 1.1 christos pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_LO16, text);
1046 1.1 christos pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 4, BFD_RELOC_LO16,
1047 1.1 christos (struct bfd_symbol **) imp_sym,
1048 1.1 christos imp_index);
1049 1.1 christos }
1050 1.1.1.3 christos else
1051 1.1.1.3 christos #endif
1052 1.1.1.3 christos #ifdef AMD64MAGIC
1053 1.1.1.3 christos if (magic == AMD64MAGIC)
1054 1.1.1.3 christos {
1055 1.1.1.3 christos pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
1056 1.1.1.3 christos BFD_RELOC_32_PCREL, (asymbol **) imp_sym,
1057 1.1.1.3 christos imp_index);
1058 1.1.1.3 christos }
1059 1.1 christos else
1060 1.1 christos #endif
1061 1.1 christos pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
1062 1.1 christos BFD_RELOC_32, (asymbol **) imp_sym,
1063 1.1 christos imp_index);
1064 1.1 christos
1065 1.1 christos pe_ILF_save_relocs (& vars, text);
1066 1.1 christos break;
1067 1.1 christos
1068 1.1 christos case IMPORT_DATA:
1069 1.1 christos break;
1070 1.1 christos
1071 1.1 christos default:
1072 1.1 christos /* XXX code not yet written. */
1073 1.1 christos abort ();
1074 1.1 christos }
1075 1.1 christos
1076 1.1 christos /* Initialise the bfd. */
1077 1.1 christos memset (& internal_f, 0, sizeof (internal_f));
1078 1.1 christos
1079 1.1 christos internal_f.f_magic = magic;
1080 1.1 christos internal_f.f_symptr = 0;
1081 1.1 christos internal_f.f_nsyms = 0;
1082 1.1 christos internal_f.f_flags = F_AR32WR | F_LNNO; /* XXX is this correct ? */
1083 1.1 christos
1084 1.1 christos if ( ! bfd_set_start_address (abfd, (bfd_vma) 0)
1085 1.1 christos || ! bfd_coff_set_arch_mach_hook (abfd, & internal_f))
1086 1.1 christos goto error_return;
1087 1.1 christos
1088 1.1 christos if (bfd_coff_mkobject_hook (abfd, (void *) & internal_f, NULL) == NULL)
1089 1.1 christos goto error_return;
1090 1.1 christos
1091 1.1 christos coff_data (abfd)->pe = 1;
1092 1.1 christos #ifdef THUMBPEMAGIC
1093 1.1 christos if (vars.magic == THUMBPEMAGIC)
1094 1.1 christos /* Stop some linker warnings about thumb code not supporting interworking. */
1095 1.1 christos coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;
1096 1.1 christos #endif
1097 1.1 christos
1098 1.1 christos /* Switch from file contents to memory contents. */
1099 1.1 christos bfd_cache_close (abfd);
1100 1.1 christos
1101 1.1 christos abfd->iostream = (void *) vars.bim;
1102 1.1 christos abfd->flags |= BFD_IN_MEMORY /* | HAS_LOCALS */;
1103 1.1 christos abfd->iovec = &_bfd_memory_iovec;
1104 1.1 christos abfd->where = 0;
1105 1.1 christos abfd->origin = 0;
1106 1.1 christos obj_sym_filepos (abfd) = 0;
1107 1.1 christos
1108 1.1 christos /* Now create a symbol describing the imported value. */
1109 1.1 christos switch (import_type)
1110 1.1 christos {
1111 1.1 christos case IMPORT_CODE:
1112 1.1 christos pe_ILF_make_a_symbol (& vars, "", symbol_name, text,
1113 1.1 christos BSF_NOT_AT_END | BSF_FUNCTION);
1114 1.1 christos
1115 1.1 christos break;
1116 1.1 christos
1117 1.1 christos case IMPORT_DATA:
1118 1.1 christos /* Nothing to do here. */
1119 1.1 christos break;
1120 1.1 christos
1121 1.1 christos default:
1122 1.1 christos /* XXX code not yet written. */
1123 1.1 christos abort ();
1124 1.1.1.4 christos }
1125 1.1.1.4 christos
1126 1.1.1.4 christos /* Create an import symbol for the DLL, without the .dll suffix. */
1127 1.1.1.4 christos ptr = (bfd_byte *) strrchr (source_dll, '.');
1128 1.1.1.4 christos if (ptr)
1129 1.1.1.4 christos * ptr = 0;
1130 1.1.1.4 christos pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0);
1131 1.1.1.4 christos if (ptr)
1132 1.1 christos * ptr = '.';
1133 1.1 christos
1134 1.1.1.7 christos /* Point the bfd at the symbol table. */
1135 1.1 christos obj_symbols (abfd) = vars.sym_cache;
1136 1.1 christos abfd->symcount = vars.sym_index;
1137 1.1 christos
1138 1.1 christos obj_raw_syments (abfd) = vars.native_syms;
1139 1.1 christos obj_raw_syment_count (abfd) = vars.sym_index;
1140 1.1.1.8 christos
1141 1.1 christos obj_coff_external_syms (abfd) = (void *) vars.esym_table;
1142 1.1 christos obj_coff_keep_syms (abfd) = true;
1143 1.1 christos
1144 1.1 christos obj_convert (abfd) = vars.sym_table;
1145 1.1 christos obj_conv_table_size (abfd) = vars.sym_index;
1146 1.1.1.8 christos
1147 1.1 christos obj_coff_strings (abfd) = vars.string_table;
1148 1.1 christos obj_coff_keep_strings (abfd) = true;
1149 1.1 christos
1150 1.1.1.8 christos abfd->flags |= HAS_SYMS;
1151 1.1 christos
1152 1.1 christos return true;
1153 1.1.1.7 christos
1154 1.1 christos error_return:
1155 1.1.1.8 christos free (vars.bim->buffer);
1156 1.1 christos free (vars.bim);
1157 1.1 christos return false;
1158 1.1 christos }
1159 1.1 christos
1160 1.1 christos /* We have detected a Image Library Format archive element.
1161 1.1.1.7 christos Decode the element and return the appropriate target. */
1162 1.1 christos
1163 1.1 christos static bfd_cleanup
1164 1.1.1.6 christos pe_ILF_object_p (bfd * abfd)
1165 1.1.1.6 christos {
1166 1.1.1.6 christos bfd_byte buffer[14];
1167 1.1.1.6 christos bfd_byte * ptr;
1168 1.1.1.6 christos char * symbol_name;
1169 1.1.1.6 christos char * source_dll;
1170 1.1.1.6 christos unsigned int machine;
1171 1.1.1.6 christos bfd_size_type size;
1172 1.1.1.6 christos unsigned int ordinal;
1173 1.1 christos unsigned int types;
1174 1.1.1.2 christos unsigned int magic;
1175 1.1 christos
1176 1.1.1.2 christos /* Upon entry the first six bytes of the ILF header have
1177 1.1 christos already been read. Now read the rest of the header. */
1178 1.1 christos if (bfd_bread (buffer, (bfd_size_type) 14, abfd) != 14)
1179 1.1 christos return NULL;
1180 1.1 christos
1181 1.1 christos ptr = buffer;
1182 1.1 christos
1183 1.1 christos machine = H_GET_16 (abfd, ptr);
1184 1.1 christos ptr += 2;
1185 1.1 christos
1186 1.1 christos /* Check that the machine type is recognised. */
1187 1.1 christos magic = 0;
1188 1.1 christos
1189 1.1 christos switch (machine)
1190 1.1 christos {
1191 1.1 christos case IMAGE_FILE_MACHINE_UNKNOWN:
1192 1.1 christos case IMAGE_FILE_MACHINE_ALPHA:
1193 1.1 christos case IMAGE_FILE_MACHINE_ALPHA64:
1194 1.1 christos case IMAGE_FILE_MACHINE_IA64:
1195 1.1 christos break;
1196 1.1 christos
1197 1.1 christos case IMAGE_FILE_MACHINE_I386:
1198 1.1 christos #ifdef I386MAGIC
1199 1.1 christos magic = I386MAGIC;
1200 1.1 christos #endif
1201 1.1 christos break;
1202 1.1 christos
1203 1.1 christos case IMAGE_FILE_MACHINE_AMD64:
1204 1.1 christos #ifdef AMD64MAGIC
1205 1.1 christos magic = AMD64MAGIC;
1206 1.1 christos #endif
1207 1.1 christos break;
1208 1.1 christos
1209 1.1 christos case IMAGE_FILE_MACHINE_R3000:
1210 1.1 christos case IMAGE_FILE_MACHINE_R4000:
1211 1.1 christos case IMAGE_FILE_MACHINE_R10000:
1212 1.1 christos
1213 1.1 christos case IMAGE_FILE_MACHINE_MIPS16:
1214 1.1 christos case IMAGE_FILE_MACHINE_MIPSFPU:
1215 1.1 christos case IMAGE_FILE_MACHINE_MIPSFPU16:
1216 1.1 christos #ifdef MIPS_ARCH_MAGIC_WINCE
1217 1.1 christos magic = MIPS_ARCH_MAGIC_WINCE;
1218 1.1 christos #endif
1219 1.1 christos break;
1220 1.1 christos
1221 1.1 christos case IMAGE_FILE_MACHINE_SH3:
1222 1.1 christos case IMAGE_FILE_MACHINE_SH4:
1223 1.1 christos #ifdef SH_ARCH_MAGIC_WINCE
1224 1.1 christos magic = SH_ARCH_MAGIC_WINCE;
1225 1.1 christos #endif
1226 1.1 christos break;
1227 1.1 christos
1228 1.1 christos case IMAGE_FILE_MACHINE_ARM:
1229 1.1 christos #ifdef ARMPEMAGIC
1230 1.1 christos magic = ARMPEMAGIC;
1231 1.1 christos #endif
1232 1.1.1.8 christos break;
1233 1.1.1.8 christos
1234 1.1.1.8 christos case IMAGE_FILE_MACHINE_ARM64:
1235 1.1.1.8 christos #ifdef AARCH64MAGIC
1236 1.1.1.8 christos magic = AARCH64MAGIC;
1237 1.1.1.8 christos #endif
1238 1.1.1.8 christos break;
1239 1.1.1.8 christos
1240 1.1.1.8 christos case IMAGE_FILE_MACHINE_LOONGARCH64:
1241 1.1.1.8 christos #ifdef LOONGARCH64MAGIC
1242 1.1.1.8 christos magic = LOONGARCH64MAGIC;
1243 1.1.1.8 christos #endif
1244 1.1 christos break;
1245 1.1 christos
1246 1.1 christos case IMAGE_FILE_MACHINE_THUMB:
1247 1.1 christos #ifdef THUMBPEMAGIC
1248 1.1 christos {
1249 1.1 christos extern const bfd_target TARGET_LITTLE_SYM;
1250 1.1 christos
1251 1.1 christos if (abfd->xvec == & TARGET_LITTLE_SYM)
1252 1.1 christos magic = THUMBPEMAGIC;
1253 1.1 christos }
1254 1.1 christos #endif
1255 1.1 christos break;
1256 1.1 christos
1257 1.1 christos case IMAGE_FILE_MACHINE_POWERPC:
1258 1.1 christos /* We no longer support PowerPC. */
1259 1.1.1.5 christos default:
1260 1.1.1.6 christos _bfd_error_handler
1261 1.1 christos /* xgettext:c-format */
1262 1.1 christos (_("%pB: unrecognised machine type (0x%x)"
1263 1.1 christos " in Import Library Format archive"),
1264 1.1 christos abfd, machine);
1265 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1266 1.1 christos
1267 1.1 christos return NULL;
1268 1.1 christos break;
1269 1.1 christos }
1270 1.1 christos
1271 1.1 christos if (magic == 0)
1272 1.1.1.5 christos {
1273 1.1.1.6 christos _bfd_error_handler
1274 1.1 christos /* xgettext:c-format */
1275 1.1 christos (_("%pB: recognised but unhandled machine type (0x%x)"
1276 1.1 christos " in Import Library Format archive"),
1277 1.1 christos abfd, machine);
1278 1.1 christos bfd_set_error (bfd_error_wrong_format);
1279 1.1 christos
1280 1.1 christos return NULL;
1281 1.1 christos }
1282 1.1 christos
1283 1.1 christos /* We do not bother to check the date.
1284 1.1 christos date = H_GET_32 (abfd, ptr); */
1285 1.1 christos ptr += 4;
1286 1.1 christos
1287 1.1 christos size = H_GET_32 (abfd, ptr);
1288 1.1 christos ptr += 4;
1289 1.1 christos
1290 1.1 christos if (size == 0)
1291 1.1.1.6 christos {
1292 1.1 christos _bfd_error_handler
1293 1.1 christos (_("%pB: size field is zero in Import Library Format header"), abfd);
1294 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1295 1.1 christos
1296 1.1 christos return NULL;
1297 1.1 christos }
1298 1.1 christos
1299 1.1 christos ordinal = H_GET_16 (abfd, ptr);
1300 1.1 christos ptr += 2;
1301 1.1 christos
1302 1.1 christos types = H_GET_16 (abfd, ptr);
1303 1.1 christos /* ptr += 2; */
1304 1.1.1.7 christos
1305 1.1 christos /* Now read in the two strings that follow. */
1306 1.1 christos ptr = (bfd_byte *) _bfd_alloc_and_read (abfd, size, size);
1307 1.1 christos if (ptr == NULL)
1308 1.1 christos return NULL;
1309 1.1.1.5 christos
1310 1.1.1.5 christos symbol_name = (char *) ptr;
1311 1.1 christos /* See PR 20905 for an example of where the strnlen is necessary. */
1312 1.1 christos source_dll = symbol_name + strnlen (symbol_name, size - 1) + 1;
1313 1.1 christos
1314 1.1 christos /* Verify that the strings are null terminated. */
1315 1.1 christos if (ptr[size - 1] != 0
1316 1.1 christos || (bfd_size_type) ((bfd_byte *) source_dll - ptr) >= size)
1317 1.1.1.6 christos {
1318 1.1 christos _bfd_error_handler
1319 1.1 christos (_("%pB: string not null terminated in ILF object file"), abfd);
1320 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1321 1.1 christos bfd_release (abfd, ptr);
1322 1.1 christos return NULL;
1323 1.1 christos }
1324 1.1 christos
1325 1.1 christos /* Now construct the bfd. */
1326 1.1 christos if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
1327 1.1 christos source_dll, ordinal, types))
1328 1.1 christos {
1329 1.1 christos bfd_release (abfd, ptr);
1330 1.1 christos return NULL;
1331 1.1.1.7 christos }
1332 1.1 christos
1333 1.1 christos return _bfd_no_cleanup;
1334 1.1.1.3 christos }
1335 1.1.1.5 christos
1336 1.1.1.3 christos static void
1337 1.1.1.3 christos pe_bfd_read_buildid (bfd *abfd)
1338 1.1.1.3 christos {
1339 1.1.1.3 christos pe_data_type *pe = pe_data (abfd);
1340 1.1.1.3 christos struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1341 1.1.1.3 christos asection *section;
1342 1.1.1.3 christos bfd_byte *data = 0;
1343 1.1.1.3 christos bfd_size_type dataoff;
1344 1.1.1.3 christos unsigned int i;
1345 1.1.1.3 christos bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
1346 1.1.1.3 christos bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
1347 1.1.1.3 christos
1348 1.1.1.3 christos if (size == 0)
1349 1.1.1.3 christos return;
1350 1.1.1.3 christos
1351 1.1.1.5 christos addr += extra->ImageBase;
1352 1.1.1.3 christos
1353 1.1.1.3 christos /* Search for the section containing the DebugDirectory. */
1354 1.1.1.3 christos for (section = abfd->sections; section != NULL; section = section->next)
1355 1.1.1.6 christos {
1356 1.1.1.3 christos if ((addr >= section->vma) && (addr < (section->vma + section->size)))
1357 1.1.1.3 christos break;
1358 1.1.1.3 christos }
1359 1.1.1.5 christos
1360 1.1.1.5 christos if (section == NULL)
1361 1.1.1.5 christos return;
1362 1.1.1.5 christos
1363 1.1.1.3 christos if (!(section->flags & SEC_HAS_CONTENTS))
1364 1.1.1.3 christos return;
1365 1.1.1.3 christos
1366 1.1.1.6 christos dataoff = addr - section->vma;
1367 1.1.1.6 christos
1368 1.1.1.6 christos /* PR 20605 and 22373: Make sure that the data is really there.
1369 1.1.1.6 christos Note - since we are dealing with unsigned quantities we have
1370 1.1.1.6 christos to be careful to check for potential overflows. */
1371 1.1.1.5 christos if (dataoff >= section->size
1372 1.1.1.6 christos || size > section->size - dataoff)
1373 1.1.1.6 christos {
1374 1.1.1.6 christos _bfd_error_handler
1375 1.1.1.5 christos (_("%pB: error: debug data ends beyond end of debug directory"),
1376 1.1.1.5 christos abfd);
1377 1.1.1.6 christos return;
1378 1.1.1.3 christos }
1379 1.1.1.3 christos
1380 1.1.1.3 christos /* Read the whole section. */
1381 1.1.1.7 christos if (!bfd_malloc_and_get_section (abfd, section, &data))
1382 1.1.1.3 christos {
1383 1.1.1.3 christos free (data);
1384 1.1.1.3 christos return;
1385 1.1.1.3 christos }
1386 1.1.1.3 christos
1387 1.1.1.3 christos /* Search for a CodeView entry in the DebugDirectory */
1388 1.1.1.3 christos for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
1389 1.1.1.3 christos {
1390 1.1.1.3 christos struct external_IMAGE_DEBUG_DIRECTORY *ext
1391 1.1.1.3 christos = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
1392 1.1.1.3 christos struct internal_IMAGE_DEBUG_DIRECTORY idd;
1393 1.1.1.3 christos
1394 1.1.1.3 christos _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
1395 1.1.1.6 christos
1396 1.1.1.6 christos if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
1397 1.1.1.6 christos {
1398 1.1.1.6 christos char buffer[256 + 1];
1399 1.1.1.6 christos CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
1400 1.1.1.6 christos
1401 1.1.1.6 christos /*
1402 1.1.1.6 christos The debug entry doesn't have to have to be in a section, in which
1403 1.1.1.6 christos case AddressOfRawData is 0, so always use PointerToRawData.
1404 1.1.1.6 christos */
1405 1.1.1.8 christos if (_bfd_XXi_slurp_codeview_record (abfd,
1406 1.1.1.6 christos (file_ptr) idd.PointerToRawData,
1407 1.1.1.6 christos idd.SizeOfData, cvinfo, NULL))
1408 1.1.1.6 christos {
1409 1.1.1.6 christos struct bfd_build_id* build_id = bfd_alloc (abfd,
1410 1.1.1.6 christos sizeof (struct bfd_build_id) + cvinfo->SignatureLength);
1411 1.1.1.6 christos if (build_id)
1412 1.1.1.6 christos {
1413 1.1.1.6 christos build_id->size = cvinfo->SignatureLength;
1414 1.1.1.6 christos memcpy(build_id->data, cvinfo->Signature,
1415 1.1.1.6 christos cvinfo->SignatureLength);
1416 1.1.1.6 christos abfd->build_id = build_id;
1417 1.1.1.6 christos }
1418 1.1.1.6 christos }
1419 1.1.1.3 christos break;
1420 1.1.1.7 christos }
1421 1.1.1.7 christos }
1422 1.1.1.3 christos
1423 1.1.1.3 christos free (data);
1424 1.1.1.7 christos }
1425 1.1 christos
1426 1.1 christos static bfd_cleanup
1427 1.1.1.2 christos pe_bfd_object_p (bfd * abfd)
1428 1.1.1.6 christos {
1429 1.1 christos bfd_byte buffer[6];
1430 1.1 christos struct external_DOS_hdr dos_hdr;
1431 1.1 christos struct external_PEI_IMAGE_hdr image_hdr;
1432 1.1.1.7 christos struct internal_filehdr internal_f;
1433 1.1 christos struct internal_aouthdr internal_a;
1434 1.1.1.7 christos bfd_size_type opt_hdr_size;
1435 1.1 christos file_ptr offset;
1436 1.1 christos bfd_cleanup result;
1437 1.1.1.2 christos
1438 1.1 christos /* Detect if this a Microsoft Import Library Format element. */
1439 1.1.1.2 christos /* First read the beginning of the header. */
1440 1.1 christos if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1441 1.1 christos || bfd_bread (buffer, (bfd_size_type) 6, abfd) != 6)
1442 1.1 christos {
1443 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1444 1.1 christos bfd_set_error (bfd_error_wrong_format);
1445 1.1 christos return NULL;
1446 1.1.1.2 christos }
1447 1.1.1.2 christos
1448 1.1.1.2 christos /* Then check the magic and the version (only 0 is supported). */
1449 1.1 christos if (H_GET_32 (abfd, buffer) == 0xffff0000
1450 1.1 christos && H_GET_16 (abfd, buffer + 4) == 0)
1451 1.1 christos return pe_ILF_object_p (abfd);
1452 1.1 christos
1453 1.1 christos if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1454 1.1 christos || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
1455 1.1 christos != sizeof (dos_hdr))
1456 1.1 christos {
1457 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1458 1.1 christos bfd_set_error (bfd_error_wrong_format);
1459 1.1 christos return NULL;
1460 1.1 christos }
1461 1.1 christos
1462 1.1.1.6 christos /* There are really two magic numbers involved; the magic number
1463 1.1 christos that says this is a NT executable (PEI) and the magic number that
1464 1.1 christos determines the architecture. The former is IMAGE_DOS_SIGNATURE, stored in
1465 1.1 christos the e_magic field. The latter is stored in the f_magic field.
1466 1.1 christos If the NT magic number isn't valid, the architecture magic number
1467 1.1 christos could be mimicked by some other field (specifically, the number
1468 1.1 christos of relocs in section 3). Since this routine can only be called
1469 1.1 christos correctly for a PEI file, check the e_magic number here, and, if
1470 1.1.1.6 christos it doesn't match, clobber the f_magic number so that we don't get
1471 1.1 christos a false match. */
1472 1.1 christos if (H_GET_16 (abfd, dos_hdr.e_magic) != IMAGE_DOS_SIGNATURE)
1473 1.1 christos {
1474 1.1 christos bfd_set_error (bfd_error_wrong_format);
1475 1.1 christos return NULL;
1476 1.1 christos }
1477 1.1 christos
1478 1.1 christos offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
1479 1.1 christos if (bfd_seek (abfd, offset, SEEK_SET) != 0
1480 1.1 christos || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
1481 1.1 christos != sizeof (image_hdr)))
1482 1.1 christos {
1483 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1484 1.1 christos bfd_set_error (bfd_error_wrong_format);
1485 1.1 christos return NULL;
1486 1.1 christos }
1487 1.1 christos
1488 1.1 christos if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
1489 1.1 christos {
1490 1.1 christos bfd_set_error (bfd_error_wrong_format);
1491 1.1 christos return NULL;
1492 1.1 christos }
1493 1.1 christos
1494 1.1.1.2 christos /* Swap file header, so that we get the location for calling
1495 1.1 christos real_object_p. */
1496 1.1 christos bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
1497 1.1 christos
1498 1.1 christos if (! bfd_coff_bad_format_hook (abfd, &internal_f)
1499 1.1 christos || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
1500 1.1 christos {
1501 1.1 christos bfd_set_error (bfd_error_wrong_format);
1502 1.1 christos return NULL;
1503 1.1.1.7 christos }
1504 1.1.1.7 christos
1505 1.1.1.7 christos memcpy (internal_f.pe.dos_message, dos_hdr.dos_message,
1506 1.1 christos sizeof (internal_f.pe.dos_message));
1507 1.1 christos
1508 1.1 christos /* Read the optional header, which has variable size. */
1509 1.1 christos opt_hdr_size = internal_f.f_opthdr;
1510 1.1 christos
1511 1.1.1.2 christos if (opt_hdr_size != 0)
1512 1.1.1.8 christos {
1513 1.1.1.2 christos bfd_size_type amt = opt_hdr_size;
1514 1.1.1.2 christos bfd_byte * opthdr;
1515 1.1.1.2 christos
1516 1.1.1.2 christos /* PR 17521 file: 230-131433-0.004. */
1517 1.1 christos if (amt < sizeof (PEAOUTHDR))
1518 1.1.1.7 christos amt = sizeof (PEAOUTHDR);
1519 1.1 christos
1520 1.1 christos opthdr = _bfd_alloc_and_read (abfd, amt, opt_hdr_size);
1521 1.1.1.7 christos if (opthdr == NULL)
1522 1.1.1.7 christos return NULL;
1523 1.1 christos if (amt > opt_hdr_size)
1524 1.1.1.8 christos memset (opthdr + opt_hdr_size, 0, amt - opt_hdr_size);
1525 1.1.1.3 christos
1526 1.1.1.8 christos bfd_coff_swap_aouthdr_in (abfd, opthdr, &internal_a);
1527 1.1.1.8 christos
1528 1.1.1.8 christos struct internal_extra_pe_aouthdr *a = &internal_a.pe;
1529 1.1.1.8 christos if ((a->SectionAlignment & -a->SectionAlignment) != a->SectionAlignment
1530 1.1.1.8 christos || a->SectionAlignment >= 0x80000000)
1531 1.1.1.8 christos {
1532 1.1.1.8 christos _bfd_error_handler (_("%pB: adjusting invalid SectionAlignment"),
1533 1.1.1.8 christos abfd);
1534 1.1.1.8 christos a->SectionAlignment &= -a->SectionAlignment;
1535 1.1.1.8 christos if (a->SectionAlignment >= 0x80000000)
1536 1.1.1.8 christos a->SectionAlignment = 0x40000000;
1537 1.1.1.8 christos }
1538 1.1.1.8 christos
1539 1.1.1.8 christos if ((a->FileAlignment & -a->FileAlignment) != a->FileAlignment
1540 1.1.1.8 christos || a->FileAlignment > a->SectionAlignment)
1541 1.1.1.8 christos {
1542 1.1.1.8 christos _bfd_error_handler (_("%pB: adjusting invalid FileAlignment"),
1543 1.1.1.8 christos abfd);
1544 1.1.1.8 christos a->FileAlignment &= -a->FileAlignment;
1545 1.1.1.8 christos if (a->FileAlignment > a->SectionAlignment)
1546 1.1.1.8 christos a->FileAlignment = a->SectionAlignment;
1547 1.1.1.8 christos }
1548 1.1.1.8 christos
1549 1.1.1.8 christos if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
1550 1.1.1.3 christos _bfd_error_handler (_("%pB: invalid NumberOfRvaAndSizes"), abfd);
1551 1.1.1.3 christos }
1552 1.1.1.6 christos
1553 1.1.1.6 christos result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
1554 1.1.1.6 christos (opt_hdr_size != 0
1555 1.1.1.3 christos ? &internal_a
1556 1.1.1.3 christos : (struct internal_aouthdr *) NULL));
1557 1.1.1.3 christos
1558 1.1.1.3 christos if (result)
1559 1.1.1.3 christos {
1560 1.1 christos /* Now the whole header has been processed, see if there is a build-id */
1561 1.1 christos pe_bfd_read_buildid(abfd);
1562 1.1.1.3 christos }
1563 1.1 christos
1564 1.1 christos return result;
1565 1.1 christos }
1566 1.1 christos
1567 #define coff_object_p pe_bfd_object_p
1568 #endif /* COFF_IMAGE_WITH_PE */
1569