+/* These globals are used to pass information into the sorting
+ routines. */
+static bfd *sort_bfd;
+static bfd_boolean sort_dynamic;
+static asymbol *sort_x;
+static asymbol *sort_y;
+
+/* Symbol-sorting predicates */
+#define valueof(x) ((x)->section->vma + (x)->value)
+
+/* Numeric sorts. Undefined symbols are always considered "less than"
+ defined symbols with zero values. Common symbols are not treated
+ specially -- i.e., their sizes are used as their "values". */
+
+static int
+non_numeric_forward (const void *P_x, const void *P_y)
+{
+ asymbol *x, *y;
+ const char *xn, *yn;
+
+ x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
+ y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
+ if (x == NULL || y == NULL)
+ bfd_fatal (bfd_get_filename (sort_bfd));
+
+ xn = bfd_asymbol_name (x);
+ yn = bfd_asymbol_name (y);
+
+ if (yn == NULL)
+ return xn != NULL;
+ if (xn == NULL)
+ return -1;
+
+#ifdef HAVE_STRCOLL
+ /* Solaris 2.5 has a bug in strcoll.
+ strcoll returns invalid values when confronted with empty strings. */
+ if (*yn == '\0')
+ return *xn != '\0';
+ if (*xn == '\0')
+ return -1;
+
+ return strcoll (xn, yn);
+#else
+ return strcmp (xn, yn);
+#endif
+}
+
+static int
+non_numeric_reverse (const void *x, const void *y)
+{
+ return - non_numeric_forward (x, y);
+}
+
+static int
+numeric_forward (const void *P_x, const void *P_y)
+{
+ asymbol *x, *y;
+ asection *xs, *ys;
+
+ x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
+ y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
+ if (x == NULL || y == NULL)
+ bfd_fatal (bfd_get_filename (sort_bfd));
+
+ xs = bfd_get_section (x);
+ ys = bfd_get_section (y);
+
+ if (bfd_is_und_section (xs))
+ {
+ if (! bfd_is_und_section (ys))
+ return -1;
+ }
+ else if (bfd_is_und_section (ys))
+ return 1;
+ else if (valueof (x) != valueof (y))
+ return valueof (x) < valueof (y) ? -1 : 1;
+
+ return non_numeric_forward (P_x, P_y);
+}
+
+static int
+numeric_reverse (const void *x, const void *y)
+{
+ return - numeric_forward (x, y);
+}
+
+static int (*(sorters[2][2])) (const void *, const void *) =
+{
+ { non_numeric_forward, non_numeric_reverse },
+ { numeric_forward, numeric_reverse }
+};
+
+/* This sort routine is used by sort_symbols_by_size. It is similar
+ to numeric_forward, but when symbols have the same value it sorts
+ by section VMA. This simplifies the sort_symbols_by_size code
+ which handles symbols at the end of sections. Also, this routine
+ tries to sort file names before other symbols with the same value.
+ That will make the file name have a zero size, which will make
+ sort_symbols_by_size choose the non file name symbol, leading to
+ more meaningful output. For similar reasons, this code sorts
+ gnu_compiled_* and gcc2_compiled before other symbols with the same
+ value. */
+
+static int
+size_forward1 (const void *P_x, const void *P_y)
+{
+ asymbol *x, *y;
+ asection *xs, *ys;
+ const char *xn, *yn;
+ size_t xnl, ynl;
+ int xf, yf;
+
+ x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
+ y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
+ if (x == NULL || y == NULL)
+ bfd_fatal (bfd_get_filename (sort_bfd));
+
+ xs = bfd_get_section (x);
+ ys = bfd_get_section (y);
+
+ if (bfd_is_und_section (xs))
+ abort ();
+ if (bfd_is_und_section (ys))
+ abort ();
+
+ if (valueof (x) != valueof (y))
+ return valueof (x) < valueof (y) ? -1 : 1;
+
+ if (xs->vma != ys->vma)
+ return xs->vma < ys->vma ? -1 : 1;
+
+ xn = bfd_asymbol_name (x);
+ yn = bfd_asymbol_name (y);
+ xnl = strlen (xn);
+ ynl = strlen (yn);
+
+ /* The symbols gnu_compiled and gcc2_compiled convey even less
+ information than the file name, so sort them out first. */
+
+ xf = (strstr (xn, "gnu_compiled") != NULL
+ || strstr (xn, "gcc2_compiled") != NULL);
+ yf = (strstr (yn, "gnu_compiled") != NULL
+ || strstr (yn, "gcc2_compiled") != NULL);
+
+ if (xf && ! yf)
+ return -1;
+ if (! xf && yf)
+ return 1;
+
+ /* We use a heuristic for the file name. It may not work on non
+ Unix systems, but it doesn't really matter; the only difference
+ is precisely which symbol names get printed. */
+
+#define file_symbol(s, sn, snl) \
+ (((s)->flags & BSF_FILE) != 0 \
+ || ((sn)[(snl) - 2] == '.' \
+ && ((sn)[(snl) - 1] == 'o' \
+ || (sn)[(snl) - 1] == 'a')))
+
+ xf = file_symbol (x, xn, xnl);
+ yf = file_symbol (y, yn, ynl);
+
+ if (xf && ! yf)
+ return -1;
+ if (! xf && yf)
+ return 1;
+
+ return non_numeric_forward (P_x, P_y);
+}
+
+/* This sort routine is used by sort_symbols_by_size. It is sorting
+ an array of size_sym structures into size order. */
+
+static int
+size_forward2 (const void *P_x, const void *P_y)
+{
+ const struct size_sym *x = (const struct size_sym *) P_x;
+ const struct size_sym *y = (const struct size_sym *) P_y;
+
+ if (x->size < y->size)
+ return reverse_sort ? 1 : -1;
+ else if (x->size > y->size)
+ return reverse_sort ? -1 : 1;
+ else
+ return sorters[0][reverse_sort] (x->minisym, y->minisym);
+}
+
+/* Sort the symbols by size. ELF provides a size but for other formats
+ we have to make a guess by assuming that the difference between the
+ address of a symbol and the address of the next higher symbol is the
+ size. */
+
+static long
+sort_symbols_by_size (bfd *abfd, bfd_boolean is_dynamic, void *minisyms,
+ long symcount, unsigned int size,
+ struct size_sym **symsizesp)
+{
+ struct size_sym *symsizes;
+ bfd_byte *from, *fromend;
+ asymbol *sym = NULL;
+ asymbol *store_sym, *store_next;
+
+ qsort (minisyms, symcount, size, size_forward1);
+
+ /* We are going to return a special set of symbols and sizes to
+ print. */
+ symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
+ *symsizesp = symsizes;
+
+ /* Note that filter_symbols has already removed all absolute and
+ undefined symbols. Here we remove all symbols whose size winds
+ up as zero. */
+ from = (bfd_byte *) minisyms;
+ fromend = from + symcount * size;
+
+ store_sym = sort_x;
+ store_next = sort_y;
+
+ if (from < fromend)
+ {
+ sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from,
+ store_sym);
+ if (sym == NULL)
+ bfd_fatal (bfd_get_filename (abfd));
+ }
+
+ for (; from < fromend; from += size)
+ {
+ asymbol *next;
+ asection *sec;
+ bfd_vma sz;
+ asymbol *temp;
+
+ if (from + size < fromend)
+ {
+ next = bfd_minisymbol_to_symbol (abfd,
+ is_dynamic,
+ (const void *) (from + size),
+ store_next);
+ if (next == NULL)
+ bfd_fatal (bfd_get_filename (abfd));
+ }
+ else
+ next = NULL;
+
+ sec = bfd_get_section (sym);
+
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
+ else if (bfd_is_com_section (sec))
+ sz = sym->value;
+ else
+ {
+ if (from + size < fromend
+ && sec == bfd_get_section (next))
+ sz = valueof (next) - valueof (sym);
+ else
+ sz = (bfd_get_section_vma (abfd, sec)
+ + bfd_section_size (abfd, sec)
+ - valueof (sym));
+ }
+
+ if (sz != 0)
+ {
+ symsizes->minisym = (const void *) from;
+ symsizes->size = sz;
+ ++symsizes;
+ }
+
+ sym = next;
+
+ temp = store_sym;
+ store_sym = store_next;
+ store_next = temp;
+ }
+
+ symcount = symsizes - *symsizesp;
+
+ /* We must now sort again by size. */
+ qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
+
+ return symcount;
+}
+
+/* This function is used to get the relocs for a particular section.
+ It is called via bfd_map_over_sections. */