OSDN Git Service

* common.opt (Wmudflap): New option.
[pf3gnuchains/gcc-fork.git] / libmudflap / mf-hooks1.c
index a99d772..acdbc44 100644 (file)
@@ -75,21 +75,24 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 
 
 #if PIC
+
+enum { BS = 4096, NB=10 };
+static char __mf_0fn_bufs[NB][BS];
+static unsigned __mf_0fn_bufs_used[NB];
+
+
 /* A special bootstrap variant. */
 void *
 __mf_0fn_malloc (size_t c)
 {
-  enum foo { BS = 4096, NB=10 };
-  static char bufs[NB][BS];
-  static unsigned bufs_used[NB];
   unsigned i;
 
   for (i=0; i<NB; i++)
     {
-      if (! bufs_used[i] && c < BS)
+      if (! __mf_0fn_bufs_used[i] && c < BS)
        {
-         bufs_used[i] = 1;
-         return & bufs[i][0];
+         __mf_0fn_bufs_used[i] = 1;
+         return & __mf_0fn_bufs[i][0];
        }
     }
   return NULL;
@@ -246,6 +249,19 @@ WRAPPER(void, free, void *buf)
   if (UNLIKELY(buf == NULL))
     return;
 
+#if PIC
+  /* Check whether the given buffer might have come from a
+     __mf_0fn_malloc/calloc call that for whatever reason was not
+     redirected back to __mf_0fn_free.  If so, we just ignore the
+     call. */
+  if (UNLIKELY((uintptr_t) buf >= (uintptr_t) __mf_0fn_bufs &&
+               (uintptr_t) buf < ((uintptr_t) __mf_0fn_bufs + sizeof(__mf_0fn_bufs))))
+  {
+    VERBOSE_TRACE ("skipping free of boot (0fn) alloc buffer %p\n", buf);
+    return;
+  }
+#endif
+
   LOCKTH ();
   if (UNLIKELY(!freeq_initialized))
     {
@@ -302,6 +318,10 @@ WRAPPER(void, free, void *buf)
 }
 
 
+/* We can only wrap mmap if the target supports it.  Likewise for munmap.
+   We assume we have both if we have mmap.  */
+#ifdef HAVE_MMAP
+
 #if PIC
 /* A special bootstrap variant. */
 void *
@@ -392,6 +412,7 @@ WRAPPER(int , munmap, void *start, size_t length)
     }
   return result;
 }
+#endif /* HAVE_MMAP */
 
 
 /* This wrapper is a little different, as it's called indirectly from