Index: trunk/src/emx/ChangeLog.LIBC
===================================================================
--- trunk/src/emx/ChangeLog.LIBC	(revision 2274)
+++ trunk/src/emx/ChangeLog.LIBC	(revision 2275)
@@ -10,4 +10,5 @@
     - ld:
         o Fixed a case of memory access after free.
+        o Switched back to alloca and adding 23 MB of stack.
 
 2005-07-21: knut st. osmundsen <bird-gccos2-spam@anduin.net>
Index: trunk/src/emx/src/ld/ld.c
===================================================================
--- trunk/src/emx/src/ld/ld.c	(revision 2274)
+++ trunk/src/emx/src/ld/ld.c	(revision 2275)
@@ -63,5 +63,12 @@
 
 /* If compiled with GNU C, use the built-in alloca */
+#ifdef USE_ALLOCA
 #include "alloca.h"
+#define ALLOCA(size)    alloca (size)
+#define FREEA(ptr)      do { (ptr) = NULL; } while (0)
+#else
+#define ALLOCA(size)    malloc (size)
+#define FREEA(ptr)      do { free(ptr); (ptr) = NULL; } while (0)
+#endif
 #include "getopt.h"
 
@@ -1953,9 +1960,8 @@
     {
       read_entry_symbols (desc, entry);
-      entry->strings = (char *) malloc (entry->strs_size);
+      entry->strings = (char *) ALLOCA (entry->strs_size);
       read_entry_strings (desc, entry);
       enter_file_symbols (entry);
-      free (entry->strings);
-      entry->strings = NULL;
+      FREEA (entry->strings);
     }
 
@@ -2172,4 +2178,5 @@
     {
       register char *reftype;
+      int free_reftype = 0;
       switch (realtype & ~N_EXT)
 	{
@@ -2217,7 +2224,8 @@
 
 	case N_INDR:
-	  reftype = (char *) alloca (23 + strlen (((symbol *) nlist_p->n_value)->name));
+	  reftype = (char *) ALLOCA (23 + strlen (((symbol *) nlist_p->n_value)->name));
 	  sprintf (reftype, "defined equivalent to %s",
 		   ((symbol *) nlist_p->n_value)->name);
+          free_reftype = 1;
 	  break;
 
@@ -2248,4 +2256,6 @@
       fprintf (stderr, ": %3d %s %s\n",
                nlist_p - entry->symbols, sp->name, reftype);
+      if (free_reftype)
+        FREEA (reftype);
     }
 }
@@ -2530,11 +2540,10 @@
 {
   read_entry_symbols (desc, subentry);
-  subentry->strings = (char *) malloc (subentry->strs_size);
+  subentry->strings = (char *) ALLOCA (subentry->strs_size);
   read_entry_strings (desc, subentry);
 
   if (!subfile_wanted_p (subentry))
     {
-      free (subentry->strings);
-      subentry->strings = NULL;
+      FREEA (subentry->strings);
       free (subentry->symbols);
       free (subentry);
@@ -2549,6 +2558,5 @@
 	entry->subfiles = subentry;
       *prev_addr = subentry;
-      free (subentry->strings);
-      subentry->strings = NULL;
+      FREEA (subentry->strings);
     }
 }
@@ -3062,5 +3070,5 @@
     *end = entry->symbols + entry->syms_size / sizeof (struct nlist);
 
-  entry->strings = (char *) malloc (entry->strs_size);
+  entry->strings = (char *) ALLOCA (entry->strs_size);
   read_entry_strings (file_open (entry), entry);
 
@@ -3080,6 +3088,5 @@
 	       ((symbol *)p->n_un.n_name)->name, p->n_value);
 
-  free (entry->strings);
-  entry->strings = NULL;		/* All done with them.  */
+  FREEA (entry->strings);
 }
 
@@ -3452,5 +3459,5 @@
   int number_of_syms = entry->syms_size / sizeof (struct nlist);
   unsigned char *nlist_bitvector
-    = (unsigned char *) malloc ((number_of_syms >> 3) + 1);
+    = (unsigned char *) ALLOCA ((number_of_syms >> 3) + 1);
   struct line_debug_entry *text_scan, *data_scan;
   int i;
@@ -3466,5 +3473,5 @@
       int desc;
 
-      entry->strings = (char *) malloc (entry->strs_size);
+      entry->strings = (char *) ALLOCA (entry->strs_size);
       desc = file_open (entry);
       read_entry_strings (desc, entry);
@@ -3586,7 +3593,6 @@
   free (text_scan);
   free (data_scan);
-  free (entry->strings);
-  entry->strings = NULL;
-  free (nlist_bitvector);
+  FREEA (entry->strings);
+  FREEA (nlist_bitvector);
 }
 
@@ -3975,7 +3981,8 @@
     {
       char *nargv[11];
-      int i, saved_errno;
-
-      i = 0;
+      char *freeav[11];
+      int i, j, saved_errno;
+
+      i = j = 0;
       nargv[i++] = "emxbind";
       nargv[i++] = "-bq";
@@ -3986,5 +3993,5 @@
       if (def_filename != NULL)
 	{
-	  nargv[i] = alloca (strlen (def_filename) + 3);
+	  freeav[j++] = nargv[i] = ALLOCA (strlen (def_filename) + 3);
 	  strcpy (nargv[i], "-d");
 	  strcat (nargv[i], def_filename);
@@ -3995,5 +4002,5 @@
       if (stack_size != 0)
 	{
-	  nargv[i] = alloca (20);
+	  freeav[j++] = nargv[i] = ALLOCA (20);
           sprintf (nargv[i], "-k0x%x", stack_size);
 	  i++;
@@ -4003,10 +4010,10 @@
 	  if (map_filename == NULL)
 	    {
-	      map_filename = alloca (strlen (output_filename) + 5);
+	      freeav[j++] = map_filename = ALLOCA (strlen (output_filename) + 5);
               strcpy (map_filename, exe_filename);
 	      _remext (map_filename);
 	      strcat (map_filename, ".map");
 	    }
-	  nargv[i] = alloca (strlen (map_filename) + 3);
+	  freeav[j++] = nargv[i] = ALLOCA (strlen (map_filename) + 3);
           strcpy (nargv[i], "-m");
           strcat (nargv[i], map_filename);
@@ -4015,5 +4022,5 @@
       if (res_filename != NULL)
 	{
-	  nargv[i] = alloca (strlen (res_filename) + 3);
+	  freeav[j++] = nargv[i] = ALLOCA (strlen (res_filename) + 3);
 	  strcpy (nargv[i], "-r");
 	  strcat (nargv[i], res_filename);
@@ -4054,8 +4061,11 @@
             perror_name (touch_filename);
         }
+      while (j-- > 0)
+        FREEA (freeav[j]);
     }
   else if (exe_filename != NULL) /* RSXNT */
     {
       char *nargv[10];
+      char *freea = NULL;
       int i, saved_errno;
 
@@ -4077,5 +4087,5 @@
 	{
           nargv[i++] = "-d";
-	  nargv[i] = alloca (strlen (def_filename) + 3);
+	  freea = nargv[i] = ALLOCA (strlen (def_filename) + 3);
           strcpy(nargv[i], def_filename);
           i++;
@@ -4099,4 +4109,5 @@
           close (i);
         }
+      FREEA (freea);
     }
 }
@@ -4190,4 +4201,5 @@
   register int desc;
   register struct relocation_info *reloc;
+  int free_reloc = 0;
 
   if (trace_files)
@@ -4198,5 +4210,5 @@
   /* Allocate space for the file's text section */
 
-  bytes = (char *) malloc (entry->text_size);
+  bytes = (char *) ALLOCA (entry->text_size);
 
   /* Deal with relocation information however is appropriate */
@@ -4210,5 +4222,6 @@
   else
     {
-      reloc = (struct relocation_info *) alloca (entry->text_reloc_size);
+      free_reloc = 1;
+      reloc = (struct relocation_info *) ALLOCA (entry->text_reloc_size);
       lseek (desc, entry->starting_offset + entry->text_reloc_offset, L_SET);
       if (entry->text_reloc_size != read (desc, reloc, entry->text_reloc_size))
@@ -4231,5 +4244,7 @@
   mywrite (bytes, 1, entry->text_size, outdesc);
 
-  free (bytes);
+  FREEA (bytes);
+  if (free_reloc)
+    FREEA (reloc);
 }
 
@@ -4322,4 +4337,5 @@
   register char *bytes;
   register int desc;
+  int free_reloc = 0;
 
   if (trace_files)
@@ -4328,5 +4344,5 @@
   desc = file_open (entry);
 
-  bytes = (char *) malloc (entry->data_size);
+  bytes = (char *) ALLOCA (entry->data_size);
 
   if (entry->datarel) reloc = entry->datarel;
@@ -4338,5 +4354,6 @@
   else
     {
-      reloc = (struct relocation_info *) alloca (entry->data_reloc_size);
+      free_reloc = 1;
+      reloc = (struct relocation_info *) ALLOCA (entry->data_reloc_size);
       lseek (desc, entry->starting_offset + entry->data_reloc_offset, L_SET);
       if (entry->data_reloc_size != read (desc, reloc, entry->data_reloc_size))
@@ -4354,5 +4371,7 @@
   padfile ((SECTION_ALIGN - entry->data_size) & SECTION_ALIGN_MASK, outdesc);
 
-  free (bytes);
+  FREEA (bytes);
+  if (free_reloc)
+    FREEA (reloc);
 }
 
@@ -4839,5 +4858,5 @@
      extra struct for each indirect symbol to hold the extra reference
      following. */
-  struct nlist *buf = (struct nlist *) malloc ((defined_global_sym_count
+  struct nlist *buf = (struct nlist *) ALLOCA ((defined_global_sym_count
 				+ undefined_global_sym_count
 				+ global_indirect_count)
@@ -4866,7 +4885,7 @@
      extra space for the references following indirect outputs. */
 
-  strtab_vector = (char **) malloc ((num_hash_tab_syms
+  strtab_vector = (char **) ALLOCA ((num_hash_tab_syms
 				     + global_indirect_count) * sizeof (char *));
-  strtab_lens = (int *) malloc ((num_hash_tab_syms
+  strtab_lens = (int *) ALLOCA ((num_hash_tab_syms
 				 + global_indirect_count) * sizeof (int));
   strtab_index = 0;
@@ -4980,9 +4999,7 @@
 
   write_string_table ();
-  free (strtab_vector);
-  strtab_vector = NULL;
-  free (strtab_lens);
-  strtab_lens = NULL;
-  free (buf);
+  FREEA (strtab_vector);
+  FREEA (strtab_lens);
+  FREEA (buf);
 }
 
@@ -5005,5 +5022,5 @@
      It has one extra slot for the local symbol we generate here.  */
   struct nlist *buf
-    = (struct nlist *) malloc (entry->syms_size + sizeof (struct nlist));
+    = (struct nlist *) ALLOCA (entry->syms_size + sizeof (struct nlist));
   register struct nlist *bufp = buf;
 
@@ -5014,6 +5031,6 @@
      The elements are filled in by `assign_string_table_index'.  */
 
-  strtab_vector = (char **) malloc (max_syms * sizeof (char *));
-  strtab_lens = (int *) malloc (max_syms * sizeof (int));
+  strtab_vector = (char **) ALLOCA (max_syms * sizeof (char *));
+  strtab_lens = (int *) ALLOCA (max_syms * sizeof (int));
   strtab_index = 0;
 
@@ -5054,5 +5071,5 @@
   /* Read the file's string table.  */
 
-  entry->strings = (char *) malloc (entry->strs_size);
+  entry->strings = (char *) ALLOCA (entry->strs_size);
   read_entry_strings (file_open (entry), entry);
 
@@ -5107,11 +5124,8 @@
 
   write_string_table ();
-  free (entry->strings);
-  entry->strings = NULL;
-  free (strtab_vector);
-  strtab_vector = NULL;
-  free (strtab_lens);
-  strtab_lens = NULL;
-  free (buf);
+  FREEA (entry->strings);
+  FREEA (strtab_vector);
+  FREEA (strtab_lens);
+  FREEA (buf);
 }
 
@@ -5454,7 +5468,8 @@
     return;
 
-  buf = (char *) alloca (padding);
+  buf = (char *) ALLOCA (padding);
   bzero (buf, padding);
   mywrite (buf, padding, 1, outdesc);
+  FREEA (buf);
 }
 
Index: trunk/src/emx/src/ld/ld.smak
===================================================================
--- trunk/src/emx/src/ld/ld.smak	(revision 2274)
+++ trunk/src/emx/src/ld/ld.smak	(revision 2275)
@@ -8,6 +8,6 @@
 .TKIND	:= $(TOOLFMT)
 .TSRC   := $(wildcard src/ld/*.c)
-.TCF	:= -DEMX
-.TLDF   := -liberty
+.TCF	:= -DEMX -DUSE_ALLOCA
+.TLDF   := -liberty -Zstack 24575
 .TKEEP  := 1
 include mkexe.smak
