newlib/winsup/utils/parse_pe.cc

106 lines
2.8 KiB
C++
Raw Normal View History

/* parse_pe.cc
2013-01-21 17:28:27 +01:00
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2012 Red Hat, Inc.
Written by Egor Duda <deo@logos-m.ru>
This file is part of Cygwin.
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. */
ChangeLog: 2012-11-12 Christopher Faylor <me.cygwin2012@cgf.cx> * Makefile.common: Revamp for new configury. Add default compilation targets, include .E processing. Add magic for allowing "CFLAGS" to control optimization options in "CXXFLAGS". * configure.cygwin: New include for Cygwin configure.in's. * acinclude.m4: Delete old definitions. Implement AC_WINDOWS_HEADERS, AC_WINDOWS_LIBS, AC_CYGWIN_INCLUDES, target_builddir, winsup_srcdir. * aclocal.m4: Regenerate. * autogen.sh: New file. * ccwrap: New script. * c++wrap: New script. * config.guess: New script. * config.sub: New script. * configure: Regenerate. * configure.in: Eliminate LIB_AC_PROG_* calls in favor of standard. Delete ancient target test. * install-sh: New script. cygserver/ChangeLog: 2012-11-12 Christopher Faylor <me.cygwin2012@cgf.cx> * Makefile.in: Revamp for new configury. * configure.in: Revamp for new configury. * aclocal.m4: Regenerate. * configure: Ditto. * autogen.sh: New script. cygwin/ChangeLog: 2012-11-22 Christopher Faylor <me.cygwin2012@cgf.cx> * select.cc (select): Don't return -1 when we've timed out after looping. 2012-11-22 Christopher Faylor <me.cygwin2012@cgf.cx> * Makefile.in: Revamp for new configury. (datarootdir): Add variable setting. (winver_stamp): Accommodate changes to mkvers.sh setting. (libc.a): Fix race when libm.a might not have been built yet. * configure.in: Revamp for new configury. * aclocal.m4: Regenerate. * configure: Ditto. * autogen.sh: New script. * mkvers.sh: Find include directives via CFLAGS and friends rather than assuming that w32api lives nearby. utils/ChangeLog: 2012-11-12 Christopher Faylor <me.cygwin2012@cgf.cx> * aclocal.m4: Regenerate. * configure: Ditto. * autogen.sh: New script. * configure.in: Revamp for new configury. * Makefile.in: Revamp for new configury. Rename ALL_* to just *. Always use "VERBOSE" setting. (MINGW_CXX): Don't include CFLAGS in definition. (all): Define target first, before everything else so that it is the default. (ps.exe): Don't add useless -lcygwin. (ldh.exe): For consistency, add to existing MINGW_LDFLAGS rather than redefining them. (cygcheck.exe): Always include -lz for MINGW_LDFLAGS. Don't try to figure out where to find it. (dumper.exe): Simplify check. Assume libraries are installed rather than trying to retrieve from source tree. (install): Just use /bin/mkdir to create directories. (Makefile): Regenerate when standard dependencies change. * dump_setup.cc: Always include zlib.h. Remove accommodations for it possibly not existing. * parse_pe.cc: Add define which allows building with installed binutils package. * dumper.cc: Ditto.
2012-11-23 14:22:47 +01:00
#define PACKAGE
#include <bfd.h>
#include <stdio.h>
#include <stdlib.h>
#include "dumper.h"
int
exclusion::add (LPBYTE mem_base, DWORD mem_size)
{
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;
last++;
return 1;
};
2004-10-25 17:49:36 +02:00
int
cmp_regions (const void *r1, const void *r2)
{
if (((process_mem_region *) r1)->base < ((process_mem_region *) r2)->base)
return -1;
if (((process_mem_region *) r1)->base > ((process_mem_region *) r2)->base)
return 1;
return 0;
}
int
exclusion::sort_and_check ()
{
qsort (region, last, sizeof (process_mem_region), &cmp_regions);
for (process_mem_region * p = region; p < region + last - 1; p++)
{
process_mem_region *q = p + 1;
if (q == p + 1)
continue;
if (p->base + size > q->base)
{
fprintf (stderr, "region error @ (%8p + %d) > %8p\n", p->base, size, q->base);
return 0;
}
}
return 1;
}
static void
select_data_section (bfd * abfd, asection * sect, PTR obj)
{
exclusion *excl_list = (exclusion *) obj;
if ((sect->flags & (SEC_CODE | SEC_DEBUGGING)) &&
sect->vma && bfd_get_section_size (sect))
{
excl_list->add ((LPBYTE) sect->vma, (DWORD) bfd_get_section_size (sect));
deb_printf ("excluding section: %20s %08lx\n", sect->name,
bfd_get_section_size (sect));
}
}
int
parse_pe (const char *file_name, exclusion * excl_list)
{
if (file_name == NULL || excl_list == NULL)
return 0;
bfd *abfd = bfd_openr (file_name, "pei-i386");
if (abfd == NULL)
{
bfd_perror ("failed to open file");
return 0;
}
bfd_check_format (abfd, bfd_object);
bfd_map_over_sections (abfd, &select_data_section, (PTR) excl_list);
excl_list->sort_and_check ();
bfd_close (abfd);
return 1;
}