bad_alloc exceptions on Linux (Was: Re: install)

H.J. Lu hjl@lucon.org
Wed Apr 22 21:10:00 GMT 1998


> 
> > But I get the following incorrect result:
> > =B4Virtual memory exceeded in `new'=B4
> >=20
> > System is:
> > LIBC:    =20
> > /lib/libc.so.4 -> libc.so.4.7.5*
> > /lib/libc.so.5 -> libc.so.5.4.44*
> 
> I see. This must be a well-known problem, at least to the authors of
> libc 5. If you do nm(1) on libc 5, you'll find it implements
> __builtin_new. It looks like this copy of __builtin_new is being used,
> instead of the copy in egcs (which produces an exception instead of a
> message).
> 
> This raise some questions:
> 1. Is this a known problem? Why was __builtin_new included into libc
>    in the first place?

The stdio in the Linux C library is based on libio, which was written
in C++ when it was first used in Linux.

> 2. Why isn't the copy of __builtin_new of cp/new1.cc being used?
>    Ah, I see: __builtin_new is not weak. Is this the problem?
> 
> So, in short: you won't get bad_alloc exceptions with libc 5, unless
> somebody renames __builtin_new (and friends).
> 
> I personally use glibc 2 (aka libc 6), and it works just fine.
> 

Here is a patch for libc 5.4.44. I don't know if it works or not.


-- 
H.J. Lu (hjl@gnu.org)
---
Index: gcc/libgcc2.c
===================================================================
RCS file: /home/work/cvs/linux/libc/gcc/libgcc2.c,v
retrieving revision 1.3
diff -u -r1.3 libgcc2.c
--- libgcc2.c	1995/09/11 02:26:27	1.3
+++ libgcc2.c	1998/04/23 03:57:09
@@ -402,6 +402,8 @@
   8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
 };
 
+#pragma weak __udivmoddi4
+
 #if (defined (L_udivdi3) || defined (L_divdi3) || \
      defined (L_umoddi3) || defined (L_moddi3))
 static inline
@@ -683,6 +685,8 @@
 #endif
 
 #ifdef L_umoddi3
+#pragma weak __umoddi3
+
 UDItype __udivmoddi4 ();
 UDItype
 __umoddi3 (u, v)
@@ -697,6 +701,8 @@
 #endif
 
 #ifdef L_udivdi3
+#pragma weak __udivdi3
+
 UDItype __udivmoddi4 ();
 UDItype
 __udivdi3 (n, d)
@@ -1141,6 +1147,8 @@
 
 #ifdef L__gcc_bcmp
 
+#pragma weak __gcc_bcmp
+
 /* Like bcmp except the sign is meaningful.
    Reult is negative if S1 is less than S2,
    positive if S1 is greater, 0 if S1 and S2 are equal.  */
@@ -1163,6 +1171,9 @@
 #endif
 
 #ifdef L_varargs
+
+#pragma weak __builtin_saveregs
+
 #ifdef __i860__
 #if defined(__svr4__) || defined(__alliant__)
 	asm ("	.text");
@@ -1616,6 +1627,7 @@
 typedef void (*vfp)(void);
 extern vfp __new_handler;
 extern void __default_new_handler (void);
+#pragma weak __builtin_new
 
 void *
 __builtin_new (size_t sz)
@@ -1642,6 +1654,7 @@
    is used by C++ programs to allocate a block of memory for an array.  */
 
 extern void * __builtin_new (size_t);
+#pragma weak __builtin_vec_new
 
 void *
 __builtin_vec_new (size_t sz)
@@ -1670,8 +1683,12 @@
 typedef void (*vfp)(void);
 void __default_new_handler (void);
 
+#pragma weak __new_handler
+
 vfp __new_handler = (vfp)0;
 
+#pragma weak set_new_handler
+
 vfp
 set_new_handler (vfp handler)
 {
@@ -1685,6 +1702,8 @@
 
 #define MESSAGE "Virtual memory exceeded in `new'\n"
 
+#pragma weak __default_new_handler
+
 void
 __default_new_handler ()
 {
@@ -1705,6 +1724,8 @@
    by C++ programs to return to the free store a block of memory allocated
    as a single object. */
 
+#pragma weak __builtin_delete
+
 void
 __builtin_delete (void *ptr)
 {
@@ -1719,6 +1740,7 @@
    allocated as an array. */
 
 extern void __builtin_delete (void *);
+#pragma weak __builtin_vec_delete
 
 void
 __builtin_vec_delete (void *ptr)
@@ -1730,6 +1752,8 @@
 /* End of C++ free-store management functions */
 
 #ifdef L_shtab
+#pragma weak __shtab
+
 unsigned int __shtab[] = {
     0x00000001, 0x00000002, 0x00000004, 0x00000008,
     0x00000010, 0x00000020, 0x00000040, 0x00000080,
@@ -1747,6 +1771,8 @@
 
 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
 
+#pragma weak __clear_cache
+
 void
 __clear_cache (beg, end)
      char *beg, *end;
@@ -2178,6 +2204,8 @@
  */
 
 
+#pragma weak __find_first_exception_table_match
+
 void *
 __find_first_exception_table_match(pc)
 void *pc;
@@ -2235,6 +2263,8 @@
   return (void*)0;
 }
 
+#pragma weak __throw_type_match
+
 void *
 __throw_type_match (void *catch_type, void *throw_type, void* obj)
 {
@@ -2247,6 +2277,8 @@
  return 0;
 }
 
+#pragma weak __register_exceptions
+
 void
 __register_exceptions (exception_table *table)
 {
@@ -2276,7 +2308,10 @@
   exception_table_list = node;
 }
 
+#pragma weak __unwind_function
+
 #if #machine(i386)
+
 void
 __unwind_function(void *ptr)
 {
@@ -2353,6 +2388,8 @@
 #endif /* inhibit_libc */
 
 #define MESSAGE "pure virtual method called\n"
+
+#pragma weak __pure_virtual
 
 void
 __pure_virtual ()



More information about the Gcc mailing list