changeset 152:fcf1ef773a57

tiobjd disasm: implemented -l option to show line markers
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Thu, 01 May 2014 01:01:58 +0000
parents 6fe08feee018
children 4d7f36110f1c
files leo-obj/tool/disasm.c leo-obj/tool/ln.c
diffstat 2 files changed, 61 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/leo-obj/tool/disasm.c	Thu May 01 00:23:18 2014 +0000
+++ b/leo-obj/tool/disasm.c	Thu May 01 01:01:58 2014 +0000
@@ -15,7 +15,7 @@
 extern char *storage_class_to_string();
 
 int auto_xlat_section_relocs = 1;
-int disasm_richsym;
+int disasm_richsym, disasm_lineno;
 extern int richsym_print_bitsize;
 
 static void
@@ -181,10 +181,19 @@
 	struct hint *hint = sec->hints;
 	u_char *asciz_end;
 	unsigned asciz_len;
+	unsigned *lineno_arr;
+	unsigned lineno_cur, lineno_total;
 
 	if (sec->nreloc)
 		get_relocs_of_sec(sec);
+	if (disasm_lineno && sec->nlineent)
+		get_lineno_addrs(sec, &lineno_arr, &lineno_total);
+	else {
+		lineno_arr = 0;
+		lineno_total = 0;
+	}
 	symnum = relnum = 0;
+	lineno_cur = 0;
 	for (pos = 0; pos < sec->size; pos += incr) {
 		headroom = sec->size - pos;
 		while (symnum < sec->nsymbols) {
@@ -221,6 +230,15 @@
 			gothint = 0;
 		if (gothint && pos == hint->pos && hint->linebrk)
 			putchar('\n');
+		if (lineno_cur < lineno_total) {
+			if (pos >= lineno_arr[lineno_cur]) {
+				puts("; line");
+				lineno_cur++;
+			} else {
+				if (lineno_arr[lineno_cur] - pos < headroom)
+					headroom = lineno_arr[lineno_cur] - pos;
+			}
+		}
 		printf("%8x:\t", pos);
 		if (gothint && hint->type) {
 			if (rel) {
@@ -351,6 +369,8 @@
 	}
 	if (linebrk)
 		printf("%8x:\t<end of section>\n", sec->size);
+	if (lineno_arr)
+		free(lineno_arr);
 }
 
 void
@@ -414,7 +434,7 @@
 	unsigned secnum;
 	int c;
 
-	while ((c = getopt(argc, argv, "bgh:s")) != EOF)
+	while ((c = getopt(argc, argv, "bgh:ls")) != EOF)
 		switch (c) {
 		case 'b':
 			richsym_print_bitsize++;
@@ -425,6 +445,9 @@
 		case 'h':
 			hintsfile = optarg;
 			continue;
+		case 'l':
+			disasm_lineno++;
+			continue;
 		case 's':
 			auto_xlat_section_relocs = 0;
 			continue;
--- a/leo-obj/tool/ln.c	Thu May 01 00:23:18 2014 +0000
+++ b/leo-obj/tool/ln.c	Thu May 01 01:01:58 2014 +0000
@@ -31,3 +31,39 @@
 	}
 	exit(0);
 }
+
+get_lineno_addrs(sec, arr_rtn, count_rtn)
+	struct internal_scnhdr *sec;
+	unsigned **arr_rtn, *count_rtn;
+{
+	unsigned *array, cur, last;
+	unsigned n, m;
+	u_char *rec;
+
+	array = malloc(sizeof(unsigned) * sec->nlineent);
+	if (!array) {
+		perror("malloc");
+		exit(1);
+	}
+	m = 0;
+	rec = filemap + sec->line_offset;
+	for (n = 0; n < sec->nlineent; n++, rec += 6) {
+		if (!get_u16(rec + 4))
+			continue;
+		cur = get_u32(rec);
+		if (m) {
+			if (cur < last) {
+				fprintf(stderr,
+		"error: line # addresses in section %s going backward\n",
+					sec->name);
+				exit(1);
+			}
+			if (cur == last)
+				continue;
+		}
+		array[m++] = cur;
+		last = cur;
+	}
+	*arr_rtn = array;
+	*count_rtn = m;
+}