2000-08-25 03:03:12 +08:00
|
|
|
/* parse_pe.cc
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2012, 2013 Red Hat, Inc.
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
Written by Egor Duda <deo@logos-m.ru>
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
This file is part of Cygwin.
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2007-07-25 03:08:23 +08:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License (file COPYING.dumper) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2012-11-23 21:22:47 +08:00
|
|
|
#define PACKAGE
|
2000-08-25 03:03:12 +08:00
|
|
|
#include <bfd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "dumper.h"
|
|
|
|
|
|
|
|
int
|
2013-04-23 17:44:36 +08:00
|
|
|
exclusion::add (LPBYTE mem_base, SIZE_T mem_size)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
while (last >= size)
|
|
|
|
size += step;
|
|
|
|
region = (process_mem_region *) realloc (region, size * sizeof (process_mem_region));
|
|
|
|
if (region == NULL)
|
|
|
|
return 0;
|
|
|
|
region[last].base = mem_base;
|
|
|
|
region[last].size = mem_size;
|
2000-08-25 03:03:12 +08:00
|
|
|
last++;
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
2004-10-25 23:49:36 +08:00
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
cmp_regions (const void *r1, const void *r2)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (((process_mem_region *) r1)->base < ((process_mem_region *) r2)->base)
|
2000-08-25 03:03:12 +08:00
|
|
|
return -1;
|
2000-10-28 13:00:00 +08:00
|
|
|
if (((process_mem_region *) r1)->base > ((process_mem_region *) r2)->base)
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
exclusion::sort_and_check ()
|
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
qsort (region, last, sizeof (process_mem_region), &cmp_regions);
|
|
|
|
for (process_mem_region * p = region; p < region + last - 1; p++)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
process_mem_region *q = p + 1;
|
2003-09-18 09:46:18 +08:00
|
|
|
if (q == p + 1)
|
|
|
|
continue;
|
2000-10-28 13:00:00 +08:00
|
|
|
if (p->base + size > q->base)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "region error @ (%p + %zd) > %p\n", p->base, size, q->base);
|
2000-10-28 13:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-28 13:00:00 +08:00
|
|
|
select_data_section (bfd * abfd, asection * sect, PTR obj)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
exclusion *excl_list = (exclusion *) obj;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if ((sect->flags & (SEC_CODE | SEC_DEBUGGING)) &&
|
2004-06-15 10:18:51 +08:00
|
|
|
sect->vma && bfd_get_section_size (sect))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
excl_list->add ((LPBYTE) sect->vma, (SIZE_T) bfd_get_section_size (sect));
|
2004-06-15 10:18:51 +08:00
|
|
|
deb_printf ("excluding section: %20s %08lx\n", sect->name,
|
|
|
|
bfd_get_section_size (sect));
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
parse_pe (const char *file_name, exclusion * excl_list)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (file_name == NULL || excl_list == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd *abfd = bfd_openr (file_name, "pei-i386");
|
|
|
|
if (abfd == NULL)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_perror ("failed to open file");
|
2000-08-25 03:03:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_check_format (abfd, bfd_object);
|
|
|
|
bfd_map_over_sections (abfd, &select_data_section, (PTR) excl_list);
|
2000-08-25 03:03:12 +08:00
|
|
|
excl_list->sort_and_check ();
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_close (abfd);
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|