diff ticoff/symtab.c @ 81:192da19c7506

tiobjd: symbol sorting implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Wed, 26 Mar 2014 03:23:20 +0000
parents da103b9377e3
children 5f4141ee175b
line wrap: on
line diff
--- a/ticoff/symtab.c	Wed Mar 26 02:44:19 2014 +0000
+++ b/ticoff/symtab.c	Wed Mar 26 03:23:20 2014 +0000
@@ -141,3 +141,81 @@
 	extern_profile_report(objfilename);
 	exit(0);
 }
+
+static void
+initial_fill_for_sort(sec)
+	struct internal_scnhdr *sec;
+{
+	unsigned n, m;
+	struct internal_syment *sym;
+
+	m = 0;
+	for (n = 0; n < nsymtab; n++) {
+		sym = symtab[n];
+		if (!sym)
+			continue;
+		if (sym->section != sec)
+			continue;
+		sec->sorted_symbols[m++] = sym;
+	}
+}
+
+static int
+compare_for_sort(p1, p2)
+	struct internal_syment **p1, **p2;
+{
+	if ((*p1)->value < (*p2)->value)
+		return(-1);
+	if ((*p1)->value > (*p2)->value)
+		return(1);
+	else
+		return(0);
+}
+
+void
+sort_symbols_of_sec(sec)
+	struct internal_scnhdr *sec;
+{
+	if (sec->sorted_symbols)
+		return;
+	if (!sec->nsymbols) {
+		fprintf(stderr,
+	"BUG: sort_symbols_of_sec() called for section \"%s\" w/o symbols\n",
+			sec->name);
+		exit(1);
+	}
+	sec->sorted_symbols = malloc(sizeof(void *) * sec->nsymbols);
+	if (!sec->sorted_symbols) {
+		perror("malloc");
+		exit(1);
+	}
+	initial_fill_for_sort(sec);
+	qsort(sec->sorted_symbols, sec->nsymbols, sizeof(void *),
+		compare_for_sort);
+}
+
+cmd_nm()
+{
+	unsigned n, m;
+	struct internal_scnhdr *sec;
+	struct internal_syment *sym;
+	char classbuf[8];
+
+	get_int_section_table();
+	get_int_symbol_table();
+	for (n = 0; n < nsections; n++) {
+		sec = sections + n;
+		if (!sec->nsymbols)
+			continue;
+		printf("%s:\n\n", sec->name);
+		sort_symbols_of_sec(sec);
+		for (m = 0; m < sec->nsymbols; m++) {
+			sym = sec->sorted_symbols[m];
+			printf("%08X %-7s %s\n", sym->value,
+				storage_class_to_string(sym->class, classbuf),
+				sym->name);
+		}
+		putchar('\n');
+	}
+	exit(0);
+}