1 /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #define CHECK_OUT_OF_MEM(ptr, size) if (ptr == NULL) err_out_of_memory(size)
27 /* Report Out of Memory error and exit */
29 err_out_of_memory (unsigned nbytes
)
31 char *nm
= get_prog_name (1);
33 fprintf (stderr
, GTXT ("%s: Error: Memory capacity exceeded.\n"), nm
);
35 fprintf (stderr
, GTXT ("Error: Memory capacity exceeded.\n"));
36 fprintf (stderr
, GTXT (" Requested %u bytes.\n"), nbytes
);
40 #define CALL_REAL(x) (__real_##x)
41 #define NULL_PTR(x) ( __real_##x == NULL )
43 static void *(*__real_malloc
)(size_t) = NULL
;
44 static void (*__real_free
)(void *) = NULL
;
45 static void *(*__real_realloc
)(void *, size_t) = NULL
;
46 static void *(*__real_calloc
)(size_t, size_t) = NULL
;
47 static char *(*__real_strdup
)(const char*) = NULL
;
48 static volatile int in_init
= 0;
54 __real_malloc
= (void*(*)(size_t))dlsym (RTLD_NEXT
, "malloc");
55 __real_free
= (void(*)(void *))dlsym (RTLD_NEXT
, "free");
56 __real_realloc
= (void*(*)(void *, size_t))dlsym (RTLD_NEXT
, "realloc");
57 __real_calloc
= (void*(*)(size_t, size_t))dlsym (RTLD_NEXT
, "calloc");
58 __real_strdup
= (char*(*)(const char*))dlsym (RTLD_NEXT
, "strdup");
63 /* --------------------------------------------------------------------------- */
64 /* libc's memory management functions substitutions */
66 /* Allocate memory and make sure we got some */
70 if (NULL_PTR (malloc
))
72 void *ptr
= CALL_REAL (malloc
)(size
);
73 CHECK_OUT_OF_MEM (ptr
, size
);
78 /* Implement a workaround for a libdl recursion problem */
80 calloc (size_t nelem
, size_t size
)
82 if (NULL_PTR (calloc
))
84 /* If a program is linked with libpthread then the following
85 * calling sequence occurs:
86 * init_heap_intf -> dlsym -> calloc -> malloc -> init_heap_intf
87 * We break some performance improvement in libdl by returning
88 * NULL but preserve functionality.
94 return CALL_REAL (calloc
)(nelem
, size
);
97 /* Free the storage associated with data */
105 CALL_REAL (free
)(ptr
);
109 /* Reallocate buffer */
111 realloc (void *ptr
, size_t size
)
113 if (NULL_PTR (realloc
))
115 ptr
= CALL_REAL (realloc
)(ptr
, size
);
116 CHECK_OUT_OF_MEM (ptr
, size
);