+static char buf[BUFSIZE];
+static char address[BUFSIZE];
+static char name[BUFSIZE];
+
+/* Return number of symbols in a symbol-table file. */
+
+static unsigned int
+num_of_syms_in (FILE * f)
+{
+ char type;
+ unsigned int num = 0;
+
+ while (!feof (f) && fgets (buf, BUFSIZE - 1, f))
+ {
+ if (sscanf (buf, "%" STR_BUFSIZE "s %c %" STR_BUFSIZE "s", address, &type, name) == 3)
+ if (type == 't' || type == 'T')
+ {
+ /* PR 20499 - prevent integer overflow computing argument to xmalloc. */
+ if (++num >= UINT_MAX / sizeof (Sym))
+ return -1U;
+ }
+ }
+
+ return num;
+}
+
+/* Read symbol table from a file. */
+
+void
+core_create_syms_from (const char * sym_table_file)
+{
+ char type;
+ bfd_vma min_vma = ~(bfd_vma) 0;
+ bfd_vma max_vma = 0;
+ FILE * f;
+
+ f = fopen (sym_table_file, "r");
+ if (!f)
+ {
+ fprintf (stderr, _("%s: could not open %s.\n"), whoami, sym_table_file);
+ done (1);
+ }
+
+ /* Pass 1 - determine upper bound on number of function names. */
+ symtab.len = num_of_syms_in (f);
+
+ if (symtab.len == 0)
+ {
+ fprintf (stderr, _("%s: file `%s' has no symbols\n"), whoami, sym_table_file);
+ done (1);
+ }
+ else if (symtab.len == -1U)
+ {
+ fprintf (stderr, _("%s: file `%s' has too many symbols\n"),
+ whoami, sym_table_file);
+ done (1);
+ }
+
+ symtab.base = (Sym *) xmalloc (symtab.len * sizeof (Sym));
+
+ /* Pass 2 - create symbols. */
+ symtab.limit = symtab.base;
+
+ if (fseek (f, 0, SEEK_SET) != 0)
+ {
+ perror (sym_table_file);
+ done (1);
+ }
+
+ while (!feof (f) && fgets (buf, BUFSIZE - 1, f))
+ {
+ if (sscanf (buf, "%" STR_BUFSIZE "s %c %" STR_BUFSIZE "s", address, &type, name) != 3)
+ continue;
+ if (type != 't' && type != 'T')
+ continue;
+
+ sym_init (symtab.limit);
+
+ sscanf (address, "%" BFD_VMA_FMT "x", &(symtab.limit->addr) );
+
+ symtab.limit->name = (char *) xmalloc (strlen (name) + 1);
+ strcpy ((char *) symtab.limit->name, name);
+ symtab.limit->mapped = 0;
+ symtab.limit->is_func = TRUE;
+ symtab.limit->is_bb_head = TRUE;
+ symtab.limit->is_static = (type == 't');
+ min_vma = MIN (symtab.limit->addr, min_vma);
+ max_vma = MAX (symtab.limit->addr, max_vma);
+
+ ++symtab.limit;
+ }
+ fclose (f);
+
+ symtab.len = symtab.limit - symtab.base;
+ symtab_finalize (&symtab);
+}
+
+static int
+search_mapped_symbol (const void * l, const void * r)
+{
+ return strcmp ((const char *) l, ((const struct function_map *) r)->function_name);
+}
+