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