From mboxrd@z Thu Jan 1 00:00:00 1970 From: James Ingham To: gdb-patches@sourceware.cygnus.com Subject: Resizing the to_sections target vector field. Date: Wed, 22 Sep 1999 15:00:00 -0000 Message-id: <14313.20900.407612.412542@leda.cygnus.com> X-SW-Source: 1999-q3/msg00262.html Hi, all... Gdb crashes when you attach & detach a few times on Solaris native. The bug was in a bit of code that was roughly cut & pasted around in 5 places in gdb - the bug was fixed in two places, but existed in the others. So I made it a function & fixed the bug there. Look okay? Jim Index: target.h =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/target.h,v retrieving revision 1.82 diff -p -r1.82 target.h *** target.h 1999/08/31 22:23:00 1.82 --- target.h 1999/09/22 21:01:27 *************** extern struct target_ops *find_run_targe *** 1266,1271 **** --- 1266,1274 ---- extern struct target_ops * find_core_target PARAMS ((void)); + + int + target_resize_to_sections PARAMS ((struct target_ops *target, int num_added)); /* Stuff that should be shared among the various remote targets. */ Index: target.c =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/target.c,v retrieving revision 1.105 diff -p -r1.105 target.c *** target.c 1999/08/31 22:22:59 1.105 --- target.c 1999/09/22 21:01:28 *************** return_one () *** 1113,1118 **** --- 1113,1168 ---- return 1; } + /* + * Resize the to_sections pointer. Also make sure that anyone that + * was holding on to an old value of it gets updated. + * Returns the old size. + */ + + int + target_resize_to_sections (struct target_ops *target, int num_added) + { + struct target_ops **t; + struct section_table *old_value; + int old_count; + + old_value = target->to_sections; + + if (target->to_sections) + { + old_count = target->to_sections_end - target->to_sections; + target->to_sections = (struct section_table *) + xrealloc ((char *) target->to_sections, + (sizeof (struct section_table)) * (num_added + old_count)); + } + else + { + old_count = 0; + target->to_sections = (struct section_table *) + xmalloc ((sizeof (struct section_table)) * num_added); + } + target->to_sections_end = target->to_sections + (num_added + old_count); + + /* Check to see if anyone else was pointing to this structure. + If old_value was null, then no one was. */ + + if (old_value) + { + for (t = target_structs; t < target_structs + target_struct_size; + ++t) + { + if ((*t)->to_sections == old_value) + { + (*t)->to_sections = target->to_sections; + (*t)->to_sections_end = target->to_sections_end; + } + } + } + + return old_count; + + } + /* Find a single runnable target in the stack and return it. If for some reason there is more than one, return NULL. */ Index: solib.c =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/solib.c,v retrieving revision 1.129 diff -p -r1.129 solib.c *** solib.c 1999/08/30 09:04:32 1.129 --- solib.c 1999/09/22 21:01:28 *************** solib_add (arg_string, from_tty, target) *** 1174,1180 **** #endif SVR4_SHARED_LIBS ! if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL) { error ("Invalid regexp: %s", re_err); } --- 1174,1180 ---- #endif SVR4_SHARED_LIBS ! if ((re_err = re_comp (arg_string? arg_string : ".")) != NULL) { error ("Invalid regexp: %s", re_err); } *************** solib_add (arg_string, from_tty, target) *** 1196,1233 **** if (count) { ! int update_coreops; ! ! /* We must update the to_sections field in the core_ops structure ! here, otherwise we dereference a potential dangling pointer ! for each call to target_read/write_memory within this routine. */ ! update_coreops = core_ops.to_sections == target->to_sections; ! ! /* Reallocate the target's section table including the new size. */ ! if (target->to_sections) ! { ! old = target->to_sections_end - target->to_sections; ! target->to_sections = (struct section_table *) ! xrealloc ((char *) target->to_sections, ! (sizeof (struct section_table)) * (count + old)); ! } ! else ! { ! old = 0; ! target->to_sections = (struct section_table *) ! xmalloc ((sizeof (struct section_table)) * count); ! } ! target->to_sections_end = target->to_sections + (count + old); ! ! /* Update the to_sections field in the core_ops structure ! if needed. */ ! if (update_coreops) ! { ! core_ops.to_sections = target->to_sections; ! core_ops.to_sections_end = target->to_sections_end; ! } ! /* Add these section table entries to the target's table. */ while ((so = find_solib (so)) != NULL) { if (so->so_name[0]) --- 1196,1204 ---- if (count) { ! /* Add these section table entries to the target's table. */ + old = target_resize_to_sections (target, count); while ((so = find_solib (so)) != NULL) { if (so->so_name[0]) Index: somsolib.c =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/somsolib.c,v retrieving revision 2.32 diff -p -r2.32 somsolib.c *** somsolib.c 1999/07/07 23:52:05 2.32 --- somsolib.c 1999/09/22 21:01:28 *************** som_solib_load_symbols (so, name, from_t *** 375,423 **** if (status != 0) { int old, new; - int update_coreops; - int update_execops; - /* We must update the to_sections field in the core_ops structure - here, otherwise we dereference a potential dangling pointer - for each call to target_read/write_memory within this routine. */ - update_coreops = core_ops.to_sections == target->to_sections; - - /* Ditto exec_ops (this was a bug). - */ - update_execops = exec_ops.to_sections == target->to_sections; - new = so->sections_end - so->sections; ! /* Add sections from the shared library to the core target. */ ! if (target->to_sections) ! { ! old = target->to_sections_end - target->to_sections; ! target->to_sections = (struct section_table *) ! xrealloc ((char *) target->to_sections, ! ((sizeof (struct section_table)) * (old + new))); ! } ! else ! { ! old = 0; ! target->to_sections = (struct section_table *) ! xmalloc ((sizeof (struct section_table)) * new); ! } ! target->to_sections_end = (target->to_sections + old + new); ! ! /* Update the to_sections field in the core_ops structure ! if needed, ditto exec_ops. */ ! if (update_coreops) ! { ! core_ops.to_sections = target->to_sections; ! core_ops.to_sections_end = target->to_sections_end; ! } ! ! if (update_execops) ! { ! exec_ops.to_sections = target->to_sections; ! exec_ops.to_sections_end = target->to_sections_end; ! } ! /* Copy over the old data before it gets clobbered. */ memcpy ((char *) (target->to_sections + old), so->sections, --- 375,385 ---- if (status != 0) { int old, new; new = so->sections_end - so->sections; ! ! old = target_resize_to_sections (target, new); ! /* Copy over the old data before it gets clobbered. */ memcpy ((char *) (target->to_sections + old), so->sections, Index: rs6000-nat.c =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/rs6000-nat.c,v retrieving revision 2.37 diff -p -r2.37 rs6000-nat.c *** rs6000-nat.c 1999/09/01 00:16:03 2.37 --- rs6000-nat.c 1999/09/22 21:01:28 *************** xcoff_relocate_core (target) *** 755,782 **** add our sections to the section table for the core target. */ if (vp != vmap) { - int count; struct section_table *stp; - int update_coreops; ! /* We must update the to_sections field in the core_ops structure ! now to avoid dangling pointer dereferences. */ ! update_coreops = core_ops.to_sections == target->to_sections; ! ! count = target->to_sections_end - target->to_sections; ! count += 2; ! target->to_sections = (struct section_table *) ! xrealloc (target->to_sections, ! sizeof (struct section_table) * count); ! target->to_sections_end = target->to_sections + count; ! ! /* Update the to_sections field in the core_ops structure ! if needed. */ ! if (update_coreops) ! { ! core_ops.to_sections = target->to_sections; ! core_ops.to_sections_end = target->to_sections_end; ! } stp = target->to_sections_end - 2; stp->bfd = vp->bfd; --- 755,763 ---- add our sections to the section table for the core target. */ if (vp != vmap) { struct section_table *stp; ! target_resize_to_sections (target, 2); stp = target->to_sections_end - 2; stp->bfd = vp->bfd; Index: irix5-nat.c =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/irix5-nat.c,v retrieving revision 2.35 diff -p -r2.35 irix5-nat.c *** irix5-nat.c 1999/08/08 19:59:57 2.35 --- irix5-nat.c 1999/09/22 21:01:28 *************** solib_add (arg_string, from_tty, target) *** 908,944 **** if (count) { ! int update_coreops; ! ! /* We must update the to_sections field in the core_ops structure ! here, otherwise we dereference a potential dangling pointer ! for each call to target_read/write_memory within this routine. */ ! update_coreops = core_ops.to_sections == target->to_sections; ! ! /* Reallocate the target's section table including the new size. */ ! if (target->to_sections) ! { ! old = target->to_sections_end - target->to_sections; ! target->to_sections = (struct section_table *) ! xrealloc ((char *) target->to_sections, ! (sizeof (struct section_table)) * (count + old)); ! } ! else ! { ! old = 0; ! target->to_sections = (struct section_table *) ! xmalloc ((sizeof (struct section_table)) * count); ! } ! target->to_sections_end = target->to_sections + (count + old); ! ! /* Update the to_sections field in the core_ops structure ! if needed. */ ! if (update_coreops) ! { ! core_ops.to_sections = target->to_sections; ! core_ops.to_sections_end = target->to_sections_end; ! } ! /* Add these section table entries to the target's table. */ while ((so = find_solib (so)) != NULL) { --- 908,915 ---- if (count) { ! target_resize_to_sections (target, count); ! /* Add these section table entries to the target's table. */ while ((so = find_solib (so)) != NULL) { Index: pa64solib.c =================================================================== RCS file: /cvs/cvsfiles/devo/gdb/pa64solib.c,v retrieving revision 2.7 diff -p -r2.7 pa64solib.c *** pa64solib.c 1999/09/18 16:33:46 2.7 --- pa64solib.c 1999/09/22 21:01:28 *************** pa64_solib_load_symbols (so, name, from_ *** 357,405 **** status = target_read_memory (text_addr, buf, 4); if (status != 0) { ! int old, new; ! int update_coreops; ! int update_execops; ! ! /* We must update the to_sections field in the core_ops structure ! here, otherwise we dereference a potential dangling pointer ! for each call to target_read/write_memory within this routine. */ ! update_coreops = core_ops.to_sections == target->to_sections; ! ! /* Ditto exec_ops (this was a bug). */ ! update_execops = exec_ops.to_sections == target->to_sections; ! new = so->sections_end - so->sections; - /* Add sections from the shared library to the core target. */ - if (target->to_sections) - { - old = target->to_sections_end - target->to_sections; - target->to_sections = (struct section_table *) - xrealloc ((char *) target->to_sections, - ((sizeof (struct section_table)) * (old + new))); - } - else - { - old = 0; - target->to_sections = (struct section_table *) - xmalloc ((sizeof (struct section_table)) * new); - } - target->to_sections_end = (target->to_sections + old + new); - - /* Update the to_sections field in the core_ops structure - if needed, ditto exec_ops. */ - if (update_coreops) - { - core_ops.to_sections = target->to_sections; - core_ops.to_sections_end = target->to_sections_end; - } - - if (update_execops) - { - exec_ops.to_sections = target->to_sections; - exec_ops.to_sections_end = target->to_sections_end; - } /* Copy over the old data before it gets clobbered. */ memcpy ((char *) (target->to_sections + old), so->sections, --- 357,368 ---- status = target_read_memory (text_addr, buf, 4); if (status != 0) { ! int new, old; ! new = so->sections_end - so->sections; + old = target_resize_to_sections (target, new); + /* Copy over the old data before it gets clobbered. */ memcpy ((char *) (target->to_sections + old), so->sections, -- ++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++ Jim Ingham jingham@cygnus.com Cygnus Solutions Inc.