* nm.c (value_format_64bit): Define appropriately for __MSVCRT__.
[deliverable/binutils-gdb.git] / bfd / pei-x86_64.c
1 /* BFD back-end for Intel 386 PE IMAGE COFF files.
2 Copyright 2006, 2007, 2009 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA.
20
21 Written by Kai Tietz, OneVision Software GmbH&CoKg. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25
26 #define TARGET_SYM x86_64pei_vec
27 #define TARGET_NAME "pei-x86-64"
28 #define COFF_IMAGE_WITH_PE
29 #define COFF_WITH_PE
30 #define COFF_WITH_pex64
31 #define PCRELOFFSET TRUE
32 #if defined (USE_MINGW64_LEADING_UNDERSCORES)
33 #define TARGET_UNDERSCORE '_'
34 #else
35 #define TARGET_UNDERSCORE 0
36 #endif
37 /* Long section names not allowed in executable images, only object files. */
38 #define COFF_LONG_SECTION_NAMES 0
39 #define COFF_SUPPORT_GNU_LINKONCE
40 #define COFF_LONG_FILENAMES
41 #define PDATA_ROW_SIZE (3 * 4)
42
43 #define COFF_SECTION_ALIGNMENT_ENTRIES \
44 { COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
45 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
46 { COFF_SECTION_NAME_PARTIAL_MATCH (".data"), \
47 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
48 { COFF_SECTION_NAME_PARTIAL_MATCH (".rdata"), \
49 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
50 { COFF_SECTION_NAME_PARTIAL_MATCH (".text"), \
51 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
52 { COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
53 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
54 { COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
55 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
56 { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
57 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
58 { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
59 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
60
61 /* Note we have to make sure not to include headers twice.
62 Not all headers are wrapped in #ifdef guards, so we define
63 PEI_HEADERS to prevent double including in coff-x86_64.c */
64 #define PEI_HEADERS
65 #include "sysdep.h"
66 #include "bfd.h"
67 #include "libbfd.h"
68 #include "coff/x86_64.h"
69 #include "coff/internal.h"
70 #include "coff/pe.h"
71 #include "libcoff.h"
72 #include "libpei.h"
73 #include "libiberty.h"
74
75 #undef AOUTSZ
76 #define AOUTSZ PEPAOUTSZ
77 #define PEAOUTHDR PEPAOUTHDR
78
79 static const char *pex_regs[16] = {
80 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
81 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
82 };
83
84 static void
85 pex64_get_runtime_function (bfd *abfd, struct pex64_runtime_function *rf,
86 const void *data)
87 {
88 const struct external_pex64_runtime_function *ex_rf =
89 (const struct external_pex64_runtime_function *) data;
90 rf->rva_BeginAddress = bfd_get_32 (abfd, ex_rf->rva_BeginAddress);
91 rf->rva_EndAddress = bfd_get_32 (abfd, ex_rf->rva_EndAddress);
92 rf->rva_UnwindData = bfd_get_32 (abfd, ex_rf->rva_UnwindData);
93 rf->isChained = PEX64_IS_RUNTIME_FUNCTION_CHAINED (rf);
94 rf->rva_UnwindData = PEX64_GET_UNWINDDATA_UNIFIED_RVA (rf);
95 }
96
97 static void
98 pex64_get_unwind_info (bfd *abfd, struct pex64_unwind_info *ui, void *data)
99 {
100 struct external_pex64_unwind_info *ex_ui =
101 (struct external_pex64_unwind_info *) data;
102 bfd_byte *ex_dta = (bfd_byte *) data;
103
104 memset (ui, 0, sizeof (struct pex64_unwind_info));
105 ui->Version = PEX64_UWI_VERSION (ex_ui->Version_Flags);
106 ui->Flags = PEX64_UWI_FLAGS (ex_ui->Version_Flags);
107 ui->SizeOfPrologue = (bfd_vma) ex_ui->SizeOfPrologue;
108 ui->CountOfCodes = (bfd_vma) ex_ui->CountOfCodes;
109 ui->FrameRegister = PEX64_UWI_FRAMEREG (ex_ui->FrameRegisterOffset);
110 ui->FrameOffset = PEX64_UWI_FRAMEOFF (ex_ui->FrameRegisterOffset);
111 ui->sizeofUnwindCodes = PEX64_UWI_SIZEOF_UWCODE_ARRAY (ui->CountOfCodes);
112 ui->SizeOfBlock = ui->sizeofUnwindCodes + 4;
113 ui->rawUnwindCodes = &ex_dta[4];
114 ex_dta += ui->SizeOfBlock;
115 switch (ui->Flags)
116 {
117 case UNW_FLAG_CHAININFO:
118 ui->rva_FunctionEntry = bfd_get_32 (abfd, ex_dta);
119 ui->SizeOfBlock += 4;
120 return;
121 default:
122 return;
123 }
124 }
125
126 static void
127 pex64_xdata_print_uwd_codes (FILE *file, struct pex64_unwind_info *ui,
128 bfd_vma pc_addr)
129 {
130 bfd_vma i;
131 bfd_vma tmp = 0;
132 const bfd_byte *insns[256];
133 bfd_vma insns_count = 0;
134 const bfd_byte *dta = ui->rawUnwindCodes;
135
136 if (ui->CountOfCodes == 0 || !dta)
137 return;
138
139 /* Sort array ascending. Note: it is stored in reversed order. */
140 for (i = 0; i < ui->CountOfCodes; i++)
141 {
142 const bfd_byte *t;
143
144 t = insns[insns_count++] = &dta[i * 2];
145 switch (PEX64_UNWCODE_CODE (t[1]))
146 {
147 case UWOP_PUSH_NONVOL:
148 case UWOP_ALLOC_SMALL:
149 case UWOP_SET_FPREG:
150 case UWOP_PUSH_MACHFRAME:
151 break;
152 case UWOP_ALLOC_LARGE:
153 if (PEX64_UNWCODE_INFO (t[1]) == 0)
154 {
155 i += 1;
156 break;
157 }
158 else if (PEX64_UNWCODE_INFO (t[1]) == 1)
159 {
160 i += 2;
161 break;
162 }
163 /* fall through. */
164 default:
165 fprintf (file, "\t contains unknown code (%u).\n",
166 (unsigned int) PEX64_UNWCODE_CODE (t[1]));
167 return;
168 case UWOP_SAVE_NONVOL:
169 case UWOP_SAVE_XMM:
170 case UWOP_SAVE_XMM128:
171 i++;
172 break;
173 case UWOP_SAVE_NONVOL_FAR:
174 case UWOP_SAVE_XMM_FAR:
175 case UWOP_SAVE_XMM128_FAR:
176 i += 2;
177 break;
178 }
179 }
180 fprintf (file, "\t At pc 0x");
181 fprintf_vma (file, pc_addr);
182 fprintf (file, " there are the following saves (in logical order).\n");
183 for (i = insns_count; i > 0;)
184 {
185 --i;
186 dta = insns[i];
187 fprintf (file, "\t insn ends at pc+0x%02x: ", (unsigned int) dta[0]);
188 switch (PEX64_UNWCODE_CODE (dta[1]))
189 {
190 case UWOP_PUSH_NONVOL:
191 fprintf (file, "push %s.\n", pex_regs[PEX64_UNWCODE_INFO (dta[1])]);
192 break;
193 case UWOP_ALLOC_LARGE:
194 if (PEX64_UNWCODE_INFO (dta[1]) == 0)
195 {
196 tmp = (bfd_vma) (*((unsigned short *) &dta[2]));
197 tmp *= 8;
198 }
199 else
200 tmp = (bfd_vma) (*((unsigned int *)&dta[2]));
201 fprintf (file, "save stack region of size 0x");
202 fprintf_vma (file, tmp);
203 fprintf (file,".\n");
204 break;
205 case UWOP_ALLOC_SMALL:
206 tmp = (bfd_vma) PEX64_UNWCODE_INFO (dta[1]);
207 tmp += 1;
208 tmp *= 8;
209 fprintf (file, "save stack region of size 0x");
210 fprintf_vma (file, tmp);
211 fprintf (file,".\n");
212 break;
213 case UWOP_SET_FPREG:
214 tmp = (bfd_vma) PEX64_UNWCODE_INFO (dta[1]);
215 tmp *= 16;
216 fprintf (file, "FPReg = (FrameReg) + 0x");
217 fprintf_vma (file, tmp);
218 fprintf (file, ".\n");
219 break;
220 case UWOP_SAVE_NONVOL:
221 fprintf (file, "mov %s at 0x",
222 pex_regs[PEX64_UNWCODE_INFO (dta[1])]);
223 tmp = (bfd_vma) (*((unsigned short *) &dta[2]));
224 tmp *= 8;
225 fprintf_vma (file, tmp);
226 fprintf (file, ".\n");
227 break;
228 case UWOP_SAVE_NONVOL_FAR:
229 fprintf (file, "mov %s at 0x",
230 pex_regs[PEX64_UNWCODE_INFO (dta[1])]);
231 tmp = (bfd_vma) (*((unsigned int *) &dta[2]));
232 fprintf_vma (file, tmp);
233 fprintf (file, ".\n");
234 break;
235 case UWOP_SAVE_XMM:
236 tmp = (bfd_vma) (*((unsigned short *) &dta[2]));
237 tmp *= 8;
238 fprintf (file, "mov mm%u at 0x",
239 (unsigned int) PEX64_UNWCODE_INFO (dta[1]));
240 fprintf_vma (file, tmp);
241 fprintf (file, ".\n");
242 break;
243 case UWOP_SAVE_XMM_FAR:
244 tmp = (bfd_vma) (*((unsigned int *) &dta[2]));
245 fprintf (file, "mov mm%u at 0x",
246 (unsigned int) PEX64_UNWCODE_INFO (dta[1]));
247 fprintf_vma (file, tmp);
248 fprintf (file, ".\n");
249 break;
250 case UWOP_SAVE_XMM128:
251 tmp = (bfd_vma) (*((unsigned short *) &dta[2]));
252 tmp *= 16;
253 fprintf (file, "mov xmm%u at 0x",
254 (unsigned int) PEX64_UNWCODE_INFO ( dta[1]));
255 fprintf_vma (file, tmp);
256 fprintf (file, ".\n");
257 break;
258 case UWOP_SAVE_XMM128_FAR:
259 tmp = (bfd_vma) (*((unsigned int *) &dta[2]));
260 fprintf (file, "mov xmm%u at 0x",
261 (unsigned int) PEX64_UNWCODE_INFO (dta[1]));
262 fprintf_vma (file, tmp);
263 fprintf (file, ".\n");
264 break;
265 case UWOP_PUSH_MACHFRAME:
266 fprintf (file, "interrupt entry (SS, old RSP, EFLAGS, CS, RIP");
267 if (PEX64_UNWCODE_INFO (dta[1]) == 0)
268 {
269 fprintf (file, ")");
270 }
271 else if (PEX64_UNWCODE_INFO (dta[1]) == 1)
272 {
273 fprintf (file, ",ErrorCode)");
274 }
275 else
276 fprintf (file, ", unknown(%u))",
277 (unsigned int) PEX64_UNWCODE_INFO (dta[1]));
278 fprintf (file,".\n");
279 break;
280 default:
281 fprintf (file, "unknown code %u.\n",
282 (unsigned int) PEX64_UNWCODE_INFO (dta[1]));
283 break;
284 }
285 }
286 }
287
288 static asection *
289 pex64_get_section_by_rva (bfd *abfd, bfd_vma addr, const char *sec_name)
290 {
291 asection *section = bfd_get_section_by_name (abfd, sec_name);
292 bfd_vma vsize;
293 bfd_size_type datasize = 0;
294
295 if (section == NULL
296 || coff_section_data (abfd, section) == NULL
297 || pei_section_data (abfd, section) == NULL)
298 return NULL;
299 vsize = section->vma - pe_data (abfd)->pe_opthdr.ImageBase;
300 datasize = section->size;
301 if (!datasize || vsize > addr || (vsize + datasize) < addr)
302 return NULL;
303 return section;
304 }
305
306 static bfd_vma
307 find_next_xdata_or_end (bfd *abfd, bfd_byte *pdata, bfd_size_type stop,
308 int onaline, bfd_vma cur_address, bfd_vma max_size)
309 {
310 bfd_size_type i;
311 bfd_vma ret = 0;
312
313 for (i = 0; i < stop; i += onaline)
314 {
315 struct pex64_runtime_function rf;
316
317 if (i + PDATA_ROW_SIZE > stop)
318 break;
319 pex64_get_runtime_function (abfd, &rf, &pdata[i]);
320
321 if (rf.rva_BeginAddress == 0 && rf.rva_EndAddress == 0
322 && rf.rva_UnwindData == 0)
323 /* We are probably into the padding of the section now. */
324 break;
325 if (rf.rva_UnwindData != 0 && !rf.isChained)
326 {
327 if (!ret && rf.rva_UnwindData > cur_address)
328 ret = rf.rva_UnwindData;
329 else if (rf.rva_UnwindData > cur_address && ret > rf.rva_UnwindData)
330 ret = rf.rva_UnwindData;
331 }
332 }
333 if (!ret)
334 return max_size;
335 return ret;
336 }
337
338 static void
339 pex64_dump_xdata (FILE *file, bfd *abfd, bfd_vma addr, bfd_vma pc_addr,
340 bfd_size_type stop, int onaline, bfd_byte *pdata)
341 {
342 asection *section = pex64_get_section_by_rva (abfd, addr, ".rdata");
343 bfd_vma vsize;
344 bfd_byte *data = NULL;
345 bfd_vma end_addr;
346
347 if (!section)
348 section = pex64_get_section_by_rva (abfd, addr, ".data");
349 if (!section)
350 section = pex64_get_section_by_rva (abfd, addr, ".xdata");
351 if (!section)
352 {
353 section = pex64_get_section_by_rva (abfd, addr, ".pdata");
354 if (section)
355 {
356 fprintf (file, "\t Shares information with pdata element at 0x");
357 fprintf_vma (file, addr + pe_data (abfd)->pe_opthdr.ImageBase);
358 fprintf (file, ".\n");
359 }
360 }
361 if (!section)
362 return;
363
364 vsize = section->vma - pe_data (abfd)->pe_opthdr.ImageBase;
365 addr -= vsize;
366
367 end_addr = find_next_xdata_or_end (abfd, pdata, stop, onaline, addr + vsize,
368 vsize + (section->rawsize != 0 ? section->rawsize : section->size));
369
370 end_addr -= vsize;
371 if (bfd_malloc_and_get_section (abfd, section, &data))
372 {
373 struct pex64_unwind_info ui;
374
375 if (!data)
376 return;
377
378 pex64_get_unwind_info (abfd, &ui, &data[addr]);
379
380 if (ui.Version != 1)
381 {
382 fprintf (file, "\tVersion %u (unknown).\n", (unsigned int) ui.Version);
383 return;
384 }
385
386 fprintf (file, "\tFlags: ");
387 switch (ui.Flags)
388 {
389 case UNW_FLAG_NHANDLER:
390 fprintf (file, "UNW_FLAG_NHANDLER");
391 break;
392 case UNW_FLAG_EHANDLER:
393 fprintf (file, "UNW_FLAG_EHANDLER");
394 break;
395 case UNW_FLAG_UHANDLER:
396 fprintf (file, "UNW_FLAG_UHANDLER");
397 break;
398 case UNW_FLAG_FHANDLER:
399 fprintf (file, "UNW_FLAG_FHANDLER = (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER)");
400 break;
401 case UNW_FLAG_CHAININFO:
402 fprintf (file, "UNW_FLAG_CHAININFO");
403 break;
404 default:
405 fprintf (file, "unknown flags value 0x%x", (unsigned int) ui.Flags);
406 break;
407 }
408 fprintf (file, ".\n");
409 if (ui.CountOfCodes != 0)
410 fprintf (file, "\tEntry has %u codes.", (unsigned int) ui.CountOfCodes);
411 fprintf (file, "\tPrologue size: %u, Frame offset = 0x%x.\n",
412 (unsigned int) ui.SizeOfPrologue, (unsigned int) ui.FrameOffset);
413 fprintf (file, "\tFrame register is %s.\n",
414 ui.FrameRegister == 0 ? "CFA"
415 : pex_regs[(unsigned int) ui.FrameRegister]);
416
417 pex64_xdata_print_uwd_codes (file, &ui, pc_addr);
418
419 /* Now we need end of this xdata block. */
420 addr += ui.SizeOfBlock;
421 if (addr < end_addr)
422 {
423 unsigned int i;
424 fprintf (file,"\tUser data:\n");
425 for (i = 0; addr < end_addr; addr += 1, i++)
426 {
427 if ((i & 15) == 0)
428 fprintf (file, "\t %03x:", i);
429 fprintf (file, " %02x", data[addr]);
430 if ((i & 15) == 15)
431 fprintf (file, "\n");
432 }
433 if ((i & 15) != 0)
434 fprintf (file, "\n");
435 }
436 }
437 if (data != NULL)
438 free (data);
439 }
440
441 static bfd_boolean
442 pex64_bfd_print_pdata (bfd *abfd, void *vfile)
443 {
444 FILE *file = (FILE *) vfile;
445 bfd_byte *data = NULL;
446 asection *section = bfd_get_section_by_name (abfd, ".pdata");
447 bfd_size_type datasize = 0;
448 bfd_size_type i;
449 bfd_size_type stop;
450 bfd_vma prev_beginaddress = 0;
451 int onaline = PDATA_ROW_SIZE;
452 int seen_error = 0;
453
454 if (section == NULL
455 || coff_section_data (abfd, section) == NULL
456 || pei_section_data (abfd, section) == NULL)
457 return TRUE;
458
459 stop = pei_section_data (abfd, section)->virt_size;
460 if ((stop % onaline) != 0)
461 fprintf (file,
462 _("warning: .pdata section size (%ld) is not a multiple of %d\n"),
463 (long) stop, onaline);
464
465 fprintf (file,
466 _("\nThe Function Table (interpreted .pdata section contents)\n"));
467
468 fprintf (file, _("vma:\t\t\tBeginAddress\t EndAddress\t UnwindData\n"));
469
470 datasize = section->size;
471 if (datasize == 0)
472 return TRUE;
473
474 if (!bfd_malloc_and_get_section (abfd, section, &data))
475 {
476 if (data != NULL)
477 free (data);
478 return FALSE;
479 }
480
481 /* Do sanity check of pdata. */
482 for (i = 0; i < stop; i += onaline)
483 {
484 struct pex64_runtime_function rf;
485
486 if (i + PDATA_ROW_SIZE > stop)
487 break;
488 pex64_get_runtime_function (abfd, &rf, &data[i]);
489
490 if (rf.rva_BeginAddress == 0 && rf.rva_EndAddress == 0
491 && rf.rva_UnwindData == 0)
492 /* We are probably into the padding of the section now. */
493 break;
494 fputc (' ', file);
495 fprintf_vma (file, i + section->vma);
496 fprintf (file, ":\t");
497 fprintf_vma (file, rf.rva_BeginAddress);
498 fputc (' ', file);
499 fprintf_vma (file, rf.rva_EndAddress);
500 fputc (' ', file);
501 fprintf_vma (file, rf.rva_UnwindData);
502 fprintf (file, "\n");
503 if (i != 0 && rf.rva_BeginAddress <= prev_beginaddress)
504 {
505 seen_error = 1;
506 fprintf (file, " has %s begin address as predecessor\n",
507 (rf.rva_BeginAddress < prev_beginaddress ? "smaller" : "same"));
508 }
509 prev_beginaddress = rf.rva_BeginAddress;
510 /* Now we check for negative addresses. */
511 if ((prev_beginaddress & 0x80000000) != 0)
512 {
513 seen_error = 1;
514 fprintf (file, " has negative begin address\n");
515 }
516 if ((rf.rva_EndAddress & 0x80000000) != 0)
517 {
518 seen_error = 1;
519 fprintf (file, " has negative end address\n");
520 }
521 if ((rf.rva_UnwindData & 0x80000000) != 0)
522 {
523 seen_error = 1;
524 fprintf (file, " has negative unwind address\n");
525 }
526 }
527
528 if (seen_error)
529 {
530 free (data);
531
532 return TRUE;
533 }
534 /* Do dump of pdata related xdata. */
535
536 for (i = 0; i < stop; i += onaline)
537 {
538 struct pex64_runtime_function rf;
539
540 if (i + PDATA_ROW_SIZE > stop)
541 break;
542 pex64_get_runtime_function (abfd, &rf, &data[i]);
543
544 if (rf.rva_BeginAddress == 0 && rf.rva_EndAddress == 0
545 && rf.rva_UnwindData == 0)
546 /* We are probably into the padding of the section now. */
547 break;
548 if (i == 0)
549 fprintf (file, "\nDump of .xdata\n");
550 fputc (' ', file);
551 fprintf_vma (file, rf.rva_UnwindData);
552 fprintf (file, ":\n");
553
554 rf.rva_BeginAddress += pe_data (abfd)->pe_opthdr.ImageBase;
555 rf.rva_EndAddress += pe_data (abfd)->pe_opthdr.ImageBase;
556
557 if (rf.rva_UnwindData != 0)
558 {
559 if (rf.isChained)
560 {
561 fprintf (file, "\t shares information with pdata element at 0x");
562 fprintf_vma (file, rf.rva_UnwindData);
563 fprintf (file, ".\n");
564 }
565 else
566 pex64_dump_xdata (file, abfd, rf.rva_UnwindData, rf.rva_BeginAddress,
567 stop, onaline, data);
568 }
569 }
570
571 free (data);
572
573 return TRUE;
574 }
575
576 #define bfd_pe_print_pdata pex64_bfd_print_pdata
577
578 #include "coff-x86_64.c"
This page took 0.044089 seconds and 5 git commands to generate.