]>
Commit | Line | Data |
---|---|---|
0ad10c0f CF |
1 | /* parse_pe.cc |
2 | ||
0ad10c0f CF |
3 | Written by Egor Duda <deo@logos-m.ru> |
4 | ||
eedc36cb | 5 | This file is part of Cygwin. |
0ad10c0f | 6 | |
dd67e696 CV |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
6e623e93 | 9 | the Free Software Foundation; either version 3 of the License, or |
dd67e696 CV |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License (file COPYING.dumper) for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ | |
0ad10c0f | 20 | |
4c36016b | 21 | #define PACKAGE |
0ad10c0f CF |
22 | #include <bfd.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | #include "dumper.h" | |
27 | ||
ba2f251d JT |
28 | #ifndef bfd_get_section_size |
29 | #define bfd_get_section_size(sect) bfd_section_size(sect) | |
30 | #endif | |
31 | ||
0ad10c0f | 32 | int |
61522196 | 33 | exclusion::add (LPBYTE mem_base, SIZE_T mem_size) |
0ad10c0f | 34 | { |
eedc36cb CF |
35 | while (last >= size) |
36 | size += step; | |
37 | region = (process_mem_region *) realloc (region, size * sizeof (process_mem_region)); | |
38 | if (region == NULL) | |
39 | return 0; | |
40 | region[last].base = mem_base; | |
41 | region[last].size = mem_size; | |
0ad10c0f CF |
42 | last++; |
43 | return 1; | |
44 | }; | |
45 | ||
4bfc614b | 46 | int |
eedc36cb | 47 | cmp_regions (const void *r1, const void *r2) |
0ad10c0f | 48 | { |
eedc36cb | 49 | if (((process_mem_region *) r1)->base < ((process_mem_region *) r2)->base) |
0ad10c0f | 50 | return -1; |
eedc36cb | 51 | if (((process_mem_region *) r1)->base > ((process_mem_region *) r2)->base) |
0ad10c0f CF |
52 | return 1; |
53 | return 0; | |
54 | } | |
55 | ||
56 | int | |
57 | exclusion::sort_and_check () | |
58 | { | |
eedc36cb CF |
59 | qsort (region, last, sizeof (process_mem_region), &cmp_regions); |
60 | for (process_mem_region * p = region; p < region + last - 1; p++) | |
0ad10c0f | 61 | { |
eedc36cb | 62 | process_mem_region *q = p + 1; |
b1e00863 CF |
63 | if (q == p + 1) |
64 | continue; | |
eedc36cb CF |
65 | if (p->base + size > q->base) |
66 | { | |
61522196 | 67 | fprintf (stderr, "region error @ (%p + %zd) > %p\n", p->base, size, q->base); |
eedc36cb CF |
68 | return 0; |
69 | } | |
0ad10c0f CF |
70 | } |
71 | return 1; | |
72 | } | |
73 | ||
74 | static void | |
eedc36cb | 75 | select_data_section (bfd * abfd, asection * sect, PTR obj) |
0ad10c0f | 76 | { |
eedc36cb | 77 | exclusion *excl_list = (exclusion *) obj; |
0ad10c0f | 78 | |
eedc36cb | 79 | if ((sect->flags & (SEC_CODE | SEC_DEBUGGING)) && |
64b49cce | 80 | sect->vma && bfd_get_section_size (sect)) |
0ad10c0f | 81 | { |
61522196 | 82 | excl_list->add ((LPBYTE) sect->vma, (SIZE_T) bfd_get_section_size (sect)); |
64b49cce AM |
83 | deb_printf ("excluding section: %20s %08lx\n", sect->name, |
84 | bfd_get_section_size (sect)); | |
0ad10c0f CF |
85 | } |
86 | } | |
87 | ||
88 | int | |
eedc36cb | 89 | parse_pe (const char *file_name, exclusion * excl_list) |
0ad10c0f | 90 | { |
eedc36cb CF |
91 | if (file_name == NULL || excl_list == NULL) |
92 | return 0; | |
0ad10c0f | 93 | |
eedc36cb CF |
94 | bfd *abfd = bfd_openr (file_name, "pei-i386"); |
95 | if (abfd == NULL) | |
0ad10c0f | 96 | { |
eedc36cb | 97 | bfd_perror ("failed to open file"); |
0ad10c0f CF |
98 | return 0; |
99 | } | |
100 | ||
eedc36cb CF |
101 | bfd_check_format (abfd, bfd_object); |
102 | bfd_map_over_sections (abfd, &select_data_section, (PTR) excl_list); | |
0ad10c0f CF |
103 | excl_list->sort_and_check (); |
104 | ||
eedc36cb | 105 | bfd_close (abfd); |
0ad10c0f CF |
106 | return 1; |
107 | } |