shithub: scc

Download patch

ref: 620dc383f44dc5d3057809993b834223720aae91
parent: 0665b2980f119a119f4c158a7d82267c640f953f
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Nov 27 15:23:06 EST 2017

[objdump] Add printsymbols()

This function prints all the symbols defined in the object file.

--- a/objdump/main.c
+++ b/objdump/main.c
@@ -20,7 +20,7 @@
 	if (off < SIZE_MAX) {
 		for (n = off; n < strsiz && strings[n]; ++n)
 			;
-		if (n != strsiz)
+		if (n < strsiz)
 			return &strings[off];
 	}
 	fprintf(stderr, "objdump: wrong string offset %lu\n", off);
@@ -95,6 +95,33 @@
 	return 0;
 }
 
+static int
+printsymbols(struct myrohdr *hdr, FILE *fp)
+{
+	size_t n, i;
+	struct myrosym sym;
+
+	puts("symbols:");
+	n = hdr->symsize / MYROSYM_SIZ;
+	for (i = 0; i < n; ++i) {
+		if (rdmyrosym(fp, &sym) < 0)
+			return -1;
+		printf("\tname: %lu (\"%s\")\n"
+		       "\ttype: %lu (\"%s\")\n"
+		       "\tsection: %u\n"
+		       "\tflags: %x\n"
+		       "\toffset: %llu\n"
+		       "\tlength: %llu\n\n",
+		       sym.name, getstring(sym.name),
+		       sym.type, getstring(sym.type),
+		       sym.section,
+		       sym.flags,
+		       sym.offset,
+		       sym.len);
+	}
+	return 0;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -112,7 +139,8 @@
 		if (rdmyrohdr(fp, &hdr) < 0)
 			goto wrong_file;
 		if (hdr.strsize > SIZE_MAX ||
-		    hdr.secsize > SIZE_MAX / MYROSECT_SIZ) {
+		    hdr.secsize > SIZE_MAX / MYROSECT_SIZ ||
+		    hdr.symsize > SIZE_MAX / MYROSYM_SIZ) {
 			goto overflow;
 		}
 		strsiz = hdr.strsize;
@@ -128,7 +156,8 @@
 		printstrings(&hdr);
 		if (printsections(&hdr, fp) < 0)
 			goto wrong_file;
-
+		if (printsymbols(&hdr, fp) < 0)
+			goto wrong_file;
 
 		goto close_file;