Create a new module: ASpaceMgr, the address space manager.  This
contains what was previously vg_memory.c and also vg_procselfmaps.c,
which is really just a helper for the address space manager.

This just moves code around and modularises it a bit.  It doesn't yet
resolve the circular dependencies between ASpaceMgr and various other
chunks of functionality (vg_malloc2, vg_symtab2).



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3564 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index aab971b..07839aa 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -6,11 +6,11 @@
 ## That's what DIST_SUBDIRS specifies.
 SUBDIRS = \
 	$(VG_ARCH)     $(VG_OS)     $(VG_PLATFORM)     \
-	m_sigframe demangle .
+	m_sigframe m_aspacemgr demangle .
 
 DIST_SUBDIRS = \
 	$(VG_ARCH_ALL) $(VG_OS_ALL) $(VG_PLATFORM_ALL) \
-	m_sigframe demangle .
+	m_sigframe m_aspacemgr demangle .
 
 AM_CPPFLAGS += -DVG_LIBDIR="\"$(valdir)"\" -I$(srcdir)/demangle \
 		-DKICKSTART_BASE=@KICKSTART_BASE@ \
@@ -37,6 +37,7 @@
 	pub_core_stacktrace.h	\
 	pub_core_sigframe.h	\
 	pub_core_debuglog.h	\
+	pub_core_aspacemgr.h	\
 	ume.h			\
 	vg_symtab2.h		\
 	vg_symtypes.h		\
@@ -73,11 +74,9 @@
 	vg_replace_malloc.c \
 	vg_main.c \
 	vg_malloc2.c \
-	vg_memory.c \
 	vg_messages.c \
 	vg_mylibc.c \
 	vg_needs.c \
-	vg_procselfmaps.c \
 	vg_dummy_profile.c \
 	vg_signals.c \
 	vg_symtab2.c \
@@ -97,6 +96,7 @@
 stage2_extra= \
 	demangle/libdemangle.a \
 	m_sigframe/libsigframe.a \
+	m_aspacemgr/libaspacemgr.a \
 	${VG_PLATFORM}/libplatform.a \
 	${VG_ARCH}/libarch.a \
 	${VG_OS}/libos.a \
diff --git a/coregrind/core.h b/coregrind/core.h
index 9b825f6..84ee3cb 100644
--- a/coregrind/core.h
+++ b/coregrind/core.h
@@ -862,18 +862,6 @@
                       Int      debugging_verbosity );
 
 /* ---------------------------------------------------------------------
-   Exports of vg_procselfmaps.c
-   ------------------------------------------------------------------ */
-
-/* Parses /proc/self/maps, calling `record_mapping' for each entry. */
-extern 
-void VG_(parse_procselfmaps) (
-   void (*record_mapping)( Addr addr, SizeT len, UInt prot,
-			   UInt dev, UInt ino, ULong foff,
-                           const UChar *filename ) );
-
-
-/* ---------------------------------------------------------------------
    Exports of vg_symtab2.c
    ------------------------------------------------------------------ */
 
@@ -995,98 +983,6 @@
 
 
 /* ---------------------------------------------------------------------
-   Exports of vg_memory.c
-   ------------------------------------------------------------------ */
-
-/* A Segment is mapped piece of client memory.  This covers all kinds
-   of mapped memory (exe, brk, mmap, .so, shm, stack, etc)
-
-   We try to encode everything we know about a particular segment here.
-*/
-#define SF_FIXED    (1 <<  0) // client asked for MAP_FIXED
-#define SF_SHARED   (1 <<  1) // shared
-#define SF_SHM      (1 <<  2) // SYSV SHM (also SF_SHARED)
-#define SF_MMAP     (1 <<  3) // mmap memory
-#define SF_FILE     (1 <<  4) // mapping is backed by a file
-#define SF_STACK    (1 <<  5) // is a stack
-#define SF_GROWDOWN (1 <<  6) // segment grows down
-#define SF_GROWUP   (1 <<  7) // segment grows up
-#define SF_EXEC     (1 <<  8) // segment created by exec
-#define SF_DYNLIB   (1 <<  9) // mapped from dynamic library
-#define SF_NOSYMS   (1 << 10) // don't load syms, even if present
-#define SF_BRK      (1 << 11) // brk segment
-#define SF_CORE     (1 << 12) // allocated by core on behalf of the client
-#define SF_VALGRIND (1 << 13) // a valgrind-internal mapping - not in client
-#define SF_CODE     (1 << 14) // segment contains cached code
-#define SF_DEVICE   (1 << 15) // device mapping; avoid careless touching
-
-struct _Segment {
-   UInt         prot;         // VKI_PROT_*
-   UInt         flags;        // SF_*
-
-   Addr         addr;         // mapped addr (page aligned)
-   SizeT        len;          // size of mapping (page aligned)
-
-   // These are valid if (flags & SF_FILE)
-   OffT        offset;        // file offset
-   const Char* filename;      // filename (NULL if unknown)
-   Int         fnIdx;         // filename table index (-1 if unknown)
-   UInt        dev;           // device
-   UInt        ino;           // inode
-
-   SegInfo*    symtab;        // symbol table
-};
-
-/* segment mapped from a file descriptor */
-extern void VG_(map_fd_segment)  (Addr addr, SizeT len, UInt prot, UInt flags, 
-				  Int fd, ULong off, const Char *filename);
-
-/* segment mapped from a file */
-extern void VG_(map_file_segment)(Addr addr, SizeT len, UInt prot, UInt flags, 
-				  UInt dev, UInt ino, ULong off, const Char *filename);
-
-/* simple segment */
-extern void VG_(map_segment)     (Addr addr, SizeT len, UInt prot, UInt flags);
-
-extern void VG_(unmap_range)   (Addr addr, SizeT len);
-extern void VG_(mprotect_range)(Addr addr, SizeT len, UInt prot);
-extern Addr VG_(find_map_space)(Addr base, SizeT len, Bool for_client);
-
-/* Find the segment containing a, or NULL if none. */
-extern Segment *VG_(find_segment)(Addr a);
-
-/* a is an unmapped address (is checked).  Find the next segment 
-   along in the address space, or NULL if none. */
-extern Segment *VG_(find_segment_above_unmapped)(Addr a);
-
-/* a is a mapped address (in a segment, is checked).  Find the
-   next segment along. */
-extern Segment *VG_(find_segment_above_mapped)(Addr a);
-
-extern Bool VG_(seg_contains)(const Segment *s, Addr ptr, SizeT size);
-extern Bool VG_(seg_overlaps)(const Segment *s, Addr ptr, SizeT size);
-
-extern Segment *VG_(split_segment)(Addr a);
-
-extern void VG_(pad_address_space)  (Addr start);
-extern void VG_(unpad_address_space)(Addr start);
-
-extern VGA_REGPARM(2)
-       void VG_(unknown_SP_update) ( Addr old_SP, Addr new_SP );
-
-///* Search /proc/self/maps for changes which aren't reflected in the
-//   segment list */
-//extern void VG_(sync_segments)(UInt flags);
-
-/* Return string for prot */
-extern const HChar *VG_(prot_str)(UInt prot);
-
-extern Addr VG_(get_memory_from_mmap_for_client)
-               (Addr base, SizeT len, UInt prot, UInt flags);
-
-//extern void VG_(print_shadow_stats)();
-
-/* ---------------------------------------------------------------------
    Exports of vg_syscalls.c
    ------------------------------------------------------------------ */
 
diff --git a/coregrind/linux/syscalls.c b/coregrind/linux/syscalls.c
index 98f63a0..0490967 100644
--- a/coregrind/linux/syscalls.c
+++ b/coregrind/linux/syscalls.c
@@ -29,6 +29,7 @@
 */
 
 #include "core.h"
+#include "pub_core_aspacemgr.h"
 
 /* ---------------------------------------------------------------------
    PRE/POST wrappers for arch-generic, Linux-specific syscalls
diff --git a/coregrind/m_aspacemgr/Makefile.am b/coregrind/m_aspacemgr/Makefile.am
new file mode 100644
index 0000000..d8a6626
--- /dev/null
+++ b/coregrind/m_aspacemgr/Makefile.am
@@ -0,0 +1,19 @@
+include $(top_srcdir)/Makefile.all.am
+include $(top_srcdir)/Makefile.core-AM_CPPFLAGS.am
+
+AM_CFLAGS = $(WERROR) -Wmissing-prototypes -Winline -Wall -Wshadow -O -g
+
+EXTRA_DIST = \
+	README_ASPACEMGR.txt
+
+noinst_LIBRARIES = libaspacemgr.a
+
+libaspacemgr_a_SOURCES = \
+	read_procselfmaps.c \
+	aspacemgr.c
+
+if USE_PIE
+libaspacemgr_a_CFLAGS = $(AM_CFLAGS) -fpie
+else
+libaspacemgr_a_CFLAGS = $(AM_CFLAGS)
+endif
diff --git a/coregrind/vg_memory.c b/coregrind/m_aspacemgr/aspacemgr.c
similarity index 98%
rename from coregrind/vg_memory.c
rename to coregrind/m_aspacemgr/aspacemgr.c
index eaaffb0..347fa77 100644
--- a/coregrind/vg_memory.c
+++ b/coregrind/m_aspacemgr/aspacemgr.c
@@ -1,8 +1,8 @@
 
 /*--------------------------------------------------------------------*/
-/*--- Memory-related stuff: segment initialisation and tracking,   ---*/
-/*--- stack operations                                             ---*/
-/*---                                                  vg_memory.c ---*/
+/*--- The address space manager: segment initialisation and        ---*/
+/*--- tracking, stack operations                                   ---*/
+/*---                                                  aspacemgr.c ---*/
 /*--------------------------------------------------------------------*/
 
 /*
@@ -31,6 +31,8 @@
 */
 
 #include "core.h"
+#include "pub_core_aspacemgr.h"
+
 
 /* Define to debug the memory-leak-detector. */
 /* #define VG_DEBUG_LEAKCHECK */
@@ -1300,5 +1302,5 @@
 }
 
 /*--------------------------------------------------------------------*/
-/*--- end                                              vg_memory.c ---*/
+/*--- end                                              aspacemgr.c ---*/
 /*--------------------------------------------------------------------*/
diff --git a/coregrind/vg_procselfmaps.c b/coregrind/m_aspacemgr/read_procselfmaps.c
similarity index 96%
rename from coregrind/vg_procselfmaps.c
rename to coregrind/m_aspacemgr/read_procselfmaps.c
index e3ec68e..4ab8421 100644
--- a/coregrind/vg_procselfmaps.c
+++ b/coregrind/m_aspacemgr/read_procselfmaps.c
@@ -1,7 +1,7 @@
 
 /*--------------------------------------------------------------------*/
-/*--- A simple parser for /proc/self/maps on Linux 2.4.X           ---*/
-/*---                                            vg_procselfmaps.c ---*/
+/*--- A simple parser for /proc/self/maps on Linux 2.4.X/2.6.X     ---*/
+/*---                                          read_procselfmaps.c ---*/
 /*--------------------------------------------------------------------*/
 
 /*
@@ -31,6 +31,8 @@
 
 
 #include "core.h"
+#include "pub_core_aspacemgr.h"
+
 
 /* Size of a smallish table used to read /proc/self/map entries. */
 #define M_PROCMAP_BUF 50000
@@ -261,5 +263,5 @@
 }
 
 /*--------------------------------------------------------------------*/
-/*--- end                                        vg_procselfmaps.c ---*/
+/*--- end                                      read_procselfmaps.c ---*/
 /*--------------------------------------------------------------------*/
diff --git a/coregrind/m_sigframe/sigframe-x86-linux.c b/coregrind/m_sigframe/sigframe-x86-linux.c
index 07e0091..b0c60a9 100644
--- a/coregrind/m_sigframe/sigframe-x86-linux.c
+++ b/coregrind/m_sigframe/sigframe-x86-linux.c
@@ -31,6 +31,8 @@
 
 #include "core.h"
 #include "pub_core_sigframe.h"
+#include "pub_core_aspacemgr.h" /* find_segment */
+
 
 #include "libvex_guest_x86.h"
 
diff --git a/coregrind/pub_core_aspacemgr.h b/coregrind/pub_core_aspacemgr.h
new file mode 100644
index 0000000..0debc0d
--- /dev/null
+++ b/coregrind/pub_core_aspacemgr.h
@@ -0,0 +1,144 @@
+
+/*--------------------------------------------------------------------*/
+/*--- The address space manager.                                   ---*/
+/*---                                         pub_core_aspacemgr.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   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 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., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_CORE_ASPACEMGR_H
+#define __PUB_CORE_ASPACEMGR_H
+
+//--------------------------------------------------------------------
+// PURPOSE: This module deals with management of the entire process
+// address space.  Almost everything depends upon it, including dynamic
+// memory management.  Hence this module is almost completely
+// standalone; the only module it uses is m_debuglog.  DO NOT CHANGE
+// THIS.
+//--------------------------------------------------------------------
+
+/* #include "pub_tool_aspacemgr.h" */
+
+/* A Segment is mapped piece of client memory.  This covers all kinds
+   of mapped memory (exe, brk, mmap, .so, shm, stack, etc)
+
+   We try to encode everything we know about a particular segment here.
+*/
+#define SF_FIXED    (1 <<  0) // client asked for MAP_FIXED
+#define SF_SHARED   (1 <<  1) // shared
+#define SF_SHM      (1 <<  2) // SYSV SHM (also SF_SHARED)
+#define SF_MMAP     (1 <<  3) // mmap memory
+#define SF_FILE     (1 <<  4) // mapping is backed by a file
+#define SF_STACK    (1 <<  5) // is a stack
+#define SF_GROWDOWN (1 <<  6) // segment grows down
+#define SF_GROWUP   (1 <<  7) // segment grows up
+#define SF_EXEC     (1 <<  8) // segment created by exec
+#define SF_DYNLIB   (1 <<  9) // mapped from dynamic library
+#define SF_NOSYMS   (1 << 10) // don't load syms, even if present
+#define SF_BRK      (1 << 11) // brk segment
+#define SF_CORE     (1 << 12) // allocated by core on behalf of the client
+#define SF_VALGRIND (1 << 13) // a valgrind-internal mapping - not in client
+#define SF_CODE     (1 << 14) // segment contains cached code
+#define SF_DEVICE   (1 << 15) // device mapping; avoid careless touching
+
+struct _Segment {
+   UInt         prot;         // VKI_PROT_*
+   UInt         flags;        // SF_*
+
+   Addr         addr;         // mapped addr (page aligned)
+   SizeT        len;          // size of mapping (page aligned)
+
+   // These are valid if (flags & SF_FILE)
+   OffT        offset;        // file offset
+   const Char* filename;      // filename (NULL if unknown)
+   Int         fnIdx;         // filename table index (-1 if unknown)
+   UInt        dev;           // device
+   UInt        ino;           // inode
+
+   SegInfo*    symtab;        // symbol table
+};
+
+/* segment mapped from a file descriptor */
+extern void VG_(map_fd_segment)  (Addr addr, SizeT len, UInt prot, UInt flags, 
+				  Int fd, ULong off, const Char *filename);
+
+/* segment mapped from a file */
+extern void VG_(map_file_segment)(Addr addr, SizeT len, UInt prot, UInt flags, 
+				  UInt dev, UInt ino, ULong off, const Char *filename);
+
+/* simple segment */
+extern void VG_(map_segment)     (Addr addr, SizeT len, UInt prot, UInt flags);
+
+extern void VG_(unmap_range)   (Addr addr, SizeT len);
+extern void VG_(mprotect_range)(Addr addr, SizeT len, UInt prot);
+extern Addr VG_(find_map_space)(Addr base, SizeT len, Bool for_client);
+
+/* Find the segment containing a, or NULL if none. */
+extern Segment *VG_(find_segment)(Addr a);
+
+/* a is an unmapped address (is checked).  Find the next segment 
+   along in the address space, or NULL if none. */
+extern Segment *VG_(find_segment_above_unmapped)(Addr a);
+
+/* a is a mapped address (in a segment, is checked).  Find the
+   next segment along. */
+extern Segment *VG_(find_segment_above_mapped)(Addr a);
+
+extern Bool VG_(seg_contains)(const Segment *s, Addr ptr, SizeT size);
+extern Bool VG_(seg_overlaps)(const Segment *s, Addr ptr, SizeT size);
+
+extern Segment *VG_(split_segment)(Addr a);
+
+extern void VG_(pad_address_space)  (Addr start);
+extern void VG_(unpad_address_space)(Addr start);
+
+extern VGA_REGPARM(2)
+       void VG_(unknown_SP_update) ( Addr old_SP, Addr new_SP );
+
+///* Search /proc/self/maps for changes which aren't reflected in the
+//   segment list */
+//extern void VG_(sync_segments)(UInt flags);
+
+/* Return string for prot */
+extern const HChar *VG_(prot_str)(UInt prot);
+
+extern Addr VG_(get_memory_from_mmap_for_client)
+               (Addr base, SizeT len, UInt prot, UInt flags);
+
+//extern void VG_(print_shadow_stats)();
+
+/* Parses /proc/self/maps, calling `record_mapping' for each entry. */
+extern 
+void VG_(parse_procselfmaps) (
+   void (*record_mapping)( Addr addr, SizeT len, UInt prot,
+			   UInt dev, UInt ino, ULong foff,
+                           const UChar *filename ) );
+
+#endif   // __PUB_CORE_ASPACEMGR_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                     pub_core_aspacemgr.h ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index 9a2e2ed..eaa5f69 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -35,6 +35,7 @@
 #include "pub_core_execontext.h"
 #include "pub_core_errormgr.h"
 #include "pub_core_debuglog.h"
+#include "pub_core_aspacemgr.h"
 
 #include <dirent.h>
 #include <dlfcn.h>
diff --git a/coregrind/vg_malloc2.c b/coregrind/vg_malloc2.c
index 1af123d..00e0ee4 100644
--- a/coregrind/vg_malloc2.c
+++ b/coregrind/vg_malloc2.c
@@ -31,6 +31,8 @@
 
 
 #include "core.h"
+#include "pub_core_aspacemgr.h"
+
 //zz#include "memcheck/memcheck.h"
 
 //#define DEBUG_MALLOC      // turn on heavyweight debugging machinery
diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c
index 4465f6d..c5bfc7b 100644
--- a/coregrind/vg_mylibc.c
+++ b/coregrind/vg_mylibc.c
@@ -33,6 +33,7 @@
 #include "core.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_debuglog.h"    /* VG_(debugLog_vprintf) */
+#include "pub_core_aspacemgr.h"
 
 
 /* ---------------------------------------------------------------------
diff --git a/coregrind/vg_redir.c b/coregrind/vg_redir.c
index 0e7f1b3..b1487d9 100644
--- a/coregrind/vg_redir.c
+++ b/coregrind/vg_redir.c
@@ -32,6 +32,8 @@
 #include "core.h"
 #include "vg_symtab2.h"
 
+#include "pub_core_aspacemgr.h"
+
 /*------------------------------------------------------------*/
 /*--- General purpose redirection.                         ---*/
 /*------------------------------------------------------------*/
diff --git a/coregrind/vg_signals.c b/coregrind/vg_signals.c
index 1f0c640..8650b1f 100644
--- a/coregrind/vg_signals.c
+++ b/coregrind/vg_signals.c
@@ -83,6 +83,7 @@
 
 #include "pub_core_errormgr.h"
 #include "pub_core_sigframe.h"
+#include "pub_core_aspacemgr.h"
 
 
 /* Define to give more sanity checking for signals. */
diff --git a/coregrind/vg_symtab2.c b/coregrind/vg_symtab2.c
index 391688e..d6aa328 100644
--- a/coregrind/vg_symtab2.c
+++ b/coregrind/vg_symtab2.c
@@ -33,6 +33,8 @@
 #include "vg_symtypes.h"
 #include "vg_symtab2.h"
 
+#include "pub_core_aspacemgr.h"
+
 #include <elf.h>          /* ELF defns                      */
 
 static SegInfo* segInfo = NULL;
diff --git a/coregrind/vg_syscalls.c b/coregrind/vg_syscalls.c
index 344ee6d..de05e10 100644
--- a/coregrind/vg_syscalls.c
+++ b/coregrind/vg_syscalls.c
@@ -30,6 +30,8 @@
 
 #include "core.h"
 #include "pub_core_stacktrace.h"
+#include "pub_core_aspacemgr.h"
+
 
 /* All system calls are channelled through here, doing two things:
 
diff --git a/coregrind/vg_translate.c b/coregrind/vg_translate.c
index c236c30..b6c31ac 100644
--- a/coregrind/vg_translate.c
+++ b/coregrind/vg_translate.c
@@ -30,6 +30,7 @@
 */
 
 #include "core.h"
+#include "pub_core_aspacemgr.h"
 
 
 /*------------------------------------------------------------*/
diff --git a/coregrind/x86-linux/syscalls.c b/coregrind/x86-linux/syscalls.c
index b2ee4aa..093a45b 100644
--- a/coregrind/x86-linux/syscalls.c
+++ b/coregrind/x86-linux/syscalls.c
@@ -36,6 +36,8 @@
 #include "core.h"
 #include "ume.h"                /* for jmp_with_stack */
 #include "pub_core_sigframe.h"
+#include "pub_core_aspacemgr.h"
+
 
 /* ---------------------------------------------------------------------
    Stacks, thread wrappers, clone