- bfd *current;
- file_ptr elt_no = 0;
- struct orl *map;
- int orl_max = 15000; /* fine initial default */
- int orl_count = 0;
- int stridx = 0; /* string index */
-
- /* Dunno if this is the best place for this info... */
- if (elength != 0) elength += sizeof (struct ar_hdr);
- elength += elength %2 ;
-
- map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
- if (map == NULL) {
- bfd_error = no_memory;
- return false;
- }
-
- /* Drop all the files called __.SYMDEF, we're going to make our
- own */
- while (arch->archive_head &&
- strcmp(arch->archive_head->filename,"__.SYMDEF") == 0)
- {
- arch->archive_head = arch->archive_head->next;
- }
- /* Map over each element */
- for (current = arch->archive_head;
- current != (bfd *)NULL;
- current = current->next, elt_no++)
- {
- if ((bfd_check_format (current, bfd_object) == true)
- && ((bfd_get_file_flags (current) & HAS_SYMS))) {
- asymbol **syms;
- unsigned int storage;
- unsigned int symcount;
- unsigned int src_count;
-
- storage = get_symtab_upper_bound (current);
- if (storage != 0) {
-
- syms = (asymbol **) bfd_zalloc (arch,storage);
- if (syms == NULL) {
- bfd_error = no_memory; /* FIXME -- memory leak */
- return false;
- }
- symcount = bfd_canonicalize_symtab (current, syms);
-
-
- /* Now map over all the symbols, picking out the ones we want */
- for (src_count = 0; src_count <symcount; src_count++) {
- flagword flags =
- (syms[src_count])->flags;
- asection *sec =
- syms[src_count]->section;
-
- if ((flags & BSF_GLOBAL ||
- flags & BSF_WEAK ||
- flags & BSF_INDIRECT ||
- bfd_is_com_section (sec))
- && (sec != &bfd_und_section)) {
-
- /* This symbol will go into the archive header */
- if (orl_count == orl_max)
- {
- orl_max *= 2;
- map = (struct orl *) bfd_realloc (arch, (char *) map,
- orl_max * sizeof (struct orl));
- }
-
- (map[orl_count]).name = (char **) &((syms[src_count])->name);
- (map[orl_count]).pos = (file_ptr) current;
- (map[orl_count]).namidx = stridx;
-
- stridx += strlen ((syms[src_count])->name) + 1;
- ++orl_count;
- }
- }
+ char *first_name = NULL;
+ bfd *current;
+ file_ptr elt_no = 0;
+ struct orl *map = NULL;
+ unsigned int orl_max = 1024; /* Fine initial default. */
+ unsigned int orl_count = 0;
+ int stridx = 0;
+ asymbol **syms = NULL;
+ long syms_max = 0;
+ bfd_boolean ret;
+ bfd_size_type amt;
+
+ /* Dunno if this is the best place for this info... */
+ if (elength != 0)
+ elength += sizeof (struct ar_hdr);
+ elength += elength % 2;
+
+ amt = orl_max * sizeof (struct orl);
+ map = bfd_malloc (amt);
+ if (map == NULL)
+ goto error_return;
+
+ /* We put the symbol names on the arch objalloc, and then discard
+ them when done. */
+ first_name = bfd_alloc (arch, 1);
+ if (first_name == NULL)
+ goto error_return;
+
+ /* Drop all the files called __.SYMDEF, we're going to make our own. */
+ while (arch->archive_head
+ && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
+ arch->archive_head = arch->archive_head->archive_next;
+
+ /* Map over each element. */
+ for (current = arch->archive_head;
+ current != NULL;
+ current = current->archive_next, elt_no++)
+ {
+ if (bfd_check_format (current, bfd_object)
+ && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
+ {
+ long storage;
+ long symcount;
+ long src_count;
+
+ storage = bfd_get_symtab_upper_bound (current);
+ if (storage < 0)
+ goto error_return;
+
+ if (storage != 0)
+ {
+ if (storage > syms_max)
+ {
+ if (syms_max > 0)
+ free (syms);
+ syms_max = storage;
+ syms = bfd_malloc (syms_max);
+ if (syms == NULL)
+ goto error_return;
+ }
+ symcount = bfd_canonicalize_symtab (current, syms);
+ if (symcount < 0)
+ goto error_return;
+
+ /* Now map over all the symbols, picking out the ones we
+ want. */
+ for (src_count = 0; src_count < symcount; src_count++)
+ {
+ flagword flags = (syms[src_count])->flags;
+ asection *sec = syms[src_count]->section;
+
+ if ((flags & BSF_GLOBAL
+ || flags & BSF_WEAK
+ || flags & BSF_INDIRECT
+ || bfd_is_com_section (sec))
+ && ! bfd_is_und_section (sec))
+ {
+ bfd_size_type namelen;
+ struct orl *new_map;
+
+ /* This symbol will go into the archive header. */
+ if (orl_count == orl_max)
+ {
+ orl_max *= 2;
+ amt = orl_max * sizeof (struct orl);
+ new_map = bfd_realloc (map, amt);
+ if (new_map == NULL)
+ goto error_return;
+
+ map = new_map;
+ }
+
+ namelen = strlen (syms[src_count]->name);
+ amt = sizeof (char *);
+ map[orl_count].name = bfd_alloc (arch, amt);
+ if (map[orl_count].name == NULL)
+ goto error_return;
+ *(map[orl_count].name) = bfd_alloc (arch, namelen + 1);
+ if (*(map[orl_count].name) == NULL)
+ goto error_return;
+ strcpy (*(map[orl_count].name), syms[src_count]->name);
+ map[orl_count].u.abfd = current;
+ map[orl_count].namidx = stridx;
+
+ stridx += namelen + 1;
+ ++orl_count;