DWARF: add DW_AT_location for global decls with DECL_VALUE_EXPR
[gcc.git] / libbacktrace / mmap.c
index e07810d4575e64cf9cf43d35e9e730cbd44d14fd..cfa50e27d8c66aec3fee381e9426a74a17aa70c7 100644 (file)
@@ -1,5 +1,5 @@
 /* mmap.c -- Memory allocation with mmap.
-   Copyright (C) 2012 Free Software Foundation, Inc.
+   Copyright (C) 2012-2017 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Google.
 
 Redistribution and use in source and binary forms, with or without
@@ -7,13 +7,13 @@ modification, are permitted provided that the following conditions are
 met:
 
     (1) Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer. 
+    notice, this list of conditions and the following disclaimer.
 
     (2) Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
-    distribution.  
-    
+    distribution.
+
     (3) The name of the author may not be used to
     endorse or promote products derived from this software without
     specific prior written permission.
@@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/types.h>
 #include <sys/mman.h>
 
 #include "backtrace.h"
@@ -49,6 +50,10 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #define MAP_ANONYMOUS MAP_ANON
 #endif
 
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
 /* A list of free memory blocks.  */
 
 struct backtrace_freelist_struct
@@ -76,7 +81,8 @@ backtrace_free_locked (struct backtrace_state *state, void *addr, size_t size)
     }
 }
 
-/* Allocate memory like malloc.  */
+/* Allocate memory like malloc.  If ERROR_CALLBACK is NULL, don't
+   report an error.  */
 
 void *
 backtrace_alloc (struct backtrace_state *state,
@@ -84,6 +90,7 @@ backtrace_alloc (struct backtrace_state *state,
                 void *data)
 {
   void *ret;
+  int locked;
   struct backtrace_freelist_struct **pp;
   size_t pagesize;
   size_t asksize;
@@ -96,7 +103,12 @@ backtrace_alloc (struct backtrace_state *state,
      using mmap.  __sync_lock_test_and_set returns the old state of
      the lock, so we have acquired it if it returns 0.  */
 
-  if (!__sync_lock_test_and_set (&state->lock_alloc, 1))
+  if (!state->threaded)
+    locked = 1;
+  else
+    locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
+
+  if (locked)
     {
       for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
        {
@@ -120,7 +132,8 @@ backtrace_alloc (struct backtrace_state *state,
            }
        }
 
-      __sync_lock_release (&state->lock_alloc);
+      if (state->threaded)
+       __sync_lock_release (&state->lock_alloc);
     }
 
   if (ret == NULL)
@@ -131,8 +144,11 @@ backtrace_alloc (struct backtrace_state *state,
       asksize = (size + pagesize - 1) & ~ (pagesize - 1);
       page = mmap (NULL, asksize, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-      if (page == NULL)
-       error_callback (data, "mmap", errno);
+      if (page == MAP_FAILED)
+       {
+         if (error_callback)
+           error_callback (data, "mmap", errno);
+       }
       else
        {
          size = (size + 7) & ~ (size_t) 7;
@@ -154,15 +170,44 @@ backtrace_free (struct backtrace_state *state, void *addr, size_t size,
                backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
                void *data ATTRIBUTE_UNUSED)
 {
+  int locked;
+
+  /* If we are freeing a large aligned block, just release it back to
+     the system.  This case arises when growing a vector for a large
+     binary with lots of debug info.  Calling munmap here may cause us
+     to call mmap again if there is also a large shared library; we
+     just live with that.  */
+  if (size >= 16 * 4096)
+    {
+      size_t pagesize;
+
+      pagesize = getpagesize ();
+      if (((uintptr_t) addr & (pagesize - 1)) == 0
+         && (size & (pagesize - 1)) == 0)
+       {
+         /* If munmap fails for some reason, just add the block to
+            the freelist.  */
+         if (munmap (addr, size) == 0)
+           return;
+       }
+    }
+
   /* If we can acquire the lock, add the new space to the free list.
      If we can't acquire the lock, just leak the memory.
      __sync_lock_test_and_set returns the old state of the lock, so we
      have acquired it if it returns 0.  */
-  if (!__sync_lock_test_and_set (&state->lock_alloc, 1))
+
+  if (!state->threaded)
+    locked = 1;
+  else
+    locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
+
+  if (locked)
     {
       backtrace_free_locked (state, addr, size);
 
-      __sync_lock_release (&state->lock_alloc);
+      if (state->threaded)
+       __sync_lock_release (&state->lock_alloc);
     }
 }
 
@@ -192,14 +237,18 @@ backtrace_vector_grow (struct backtrace_state *state,size_t size,
            alc = pagesize;
        }
       else
-       alc = (alc + pagesize - 1) & ~ (pagesize - 1);
+       {
+         alc *= 2;
+         alc = (alc + pagesize - 1) & ~ (pagesize - 1);
+       }
       base = backtrace_alloc (state, alc, error_callback, data);
       if (base == NULL)
        return NULL;
       if (vec->base != NULL)
        {
          memcpy (base, vec->base, vec->size);
-         backtrace_free (state, vec->base, vec->alc, error_callback, data);
+         backtrace_free (state, vec->base, vec->size + vec->alc,
+                         error_callback, data);
        }
       vec->base = base;
       vec->alc = alc - vec->size;
@@ -213,12 +262,19 @@ backtrace_vector_grow (struct backtrace_state *state,size_t size,
 
 /* Finish the current allocation on VEC.  */
 
-void
-backtrace_vector_finish (struct backtrace_state *state ATTRIBUTE_UNUSED,
-                        struct backtrace_vector *vec)
+void *
+backtrace_vector_finish (
+  struct backtrace_state *state ATTRIBUTE_UNUSED,
+  struct backtrace_vector *vec,
+  backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
+  void *data ATTRIBUTE_UNUSED)
 {
+  void *ret;
+
+  ret = vec->base;
   vec->base = (char *) vec->base + vec->size;
   vec->size = 0;
+  return ret;
 }
 
 /* Release any extra space allocated for VEC.  */
@@ -229,7 +285,18 @@ backtrace_vector_release (struct backtrace_state *state,
                          backtrace_error_callback error_callback,
                          void *data)
 {
-  backtrace_free (state, (char *) vec->base + vec->size, vec->alc,
+  size_t size;
+  size_t alc;
+  size_t aligned;
+
+  /* Make sure that the block that we free is aligned on an 8-byte
+     boundary.  */
+  size = vec->size;
+  alc = vec->alc;
+  aligned = (size + 7) & ~ (size_t) 7;
+  alc -= aligned - size;
+
+  backtrace_free (state, (char *) vec->base + aligned, alc,
                  error_callback, data);
   vec->alc = 0;
   return 1;