+
+/* 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 (P_x, P_y)
+ const PTR P_x;
+ const PTR 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 (P_x, P_y)
+ const PTR P_x;
+ const PTR 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. We guess the size by assuming that the
+ difference between the address of a symbol and the address of the
+ next higher symbol is the size. FIXME: ELF actually stores a size
+ with each symbol. We should use it. */
+
+static long
+sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
+ bfd *abfd;
+ boolean dynamic;
+ PTR minisyms;
+ long symcount;
+ unsigned int size;
+ struct size_sym **symsizesp;
+{
+ struct size_sym *symsizes;
+ bfd_byte *from, *fromend;
+ asymbol *sym;
+ 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, dynamic, (const PTR) 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,
+ dynamic,
+ (const PTR) (from + size),
+ store_next);
+ if (next == NULL)
+ bfd_fatal (bfd_get_filename (abfd));
+ }
+
+ sec = bfd_get_section (sym);
+
+ 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 PTR) 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 ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
+
+ return symcount;
+}