diff ticoff/reloc.c @ 110:e650fdc743fe

tiobjd: higher-level reloc handling
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Thu, 03 Apr 2014 03:03:41 +0000
parents c20dc315a9d4
children d97fbe98600b
line wrap: on
line diff
--- a/ticoff/reloc.c	Mon Mar 31 19:06:33 2014 +0000
+++ b/ticoff/reloc.c	Thu Apr 03 03:03:41 2014 +0000
@@ -7,6 +7,7 @@
 #include <stdlib.h>
 #include "filestruct.h"
 #include "intstruct.h"
+#include "coffconst.h"
 #include "globals.h"
 
 extern unsigned get_u16(), get_u32();
@@ -33,3 +34,92 @@
 	}
 	exit(0);
 }
+
+void
+get_relocs_of_sec(sec)
+	struct internal_scnhdr *sec;
+{
+	unsigned n;
+	struct external_reloc *extrel;
+	struct internal_reloc *intrel;
+	unsigned lastloc, symidx;
+
+	if (sec->int_relocs)
+		return;
+	if (!sec->nreloc) {
+		fprintf(stderr,
+	"BUG: get_relocs_of_sec() called for section \"%s\" w/o relocs\n",
+			sec->name);
+		exit(1);
+	}
+	intrel = malloc(sizeof(struct internal_reloc) * sec->nreloc);
+	if (!intrel) {
+		perror("malloc");
+		exit(1);
+	}
+	sec->int_relocs = intrel;
+	extrel = (struct external_reloc *)(filemap + sec->reloc_offset);
+	for (n = 0; n < sec->nreloc; n++, extrel++, intrel++) {
+		intrel->location = get_u32(extrel->r_vaddr);
+		if (n && intrel->location <= lastloc) {
+			fprintf(stderr,
+			"error: non-increasing reloc order in section \"%s\"\n",
+				sec->name);
+			exit(1);
+		}
+		lastloc = intrel->location;
+		symidx = get_u32(extrel->r_symndx);
+		if (symidx >= nsymtab || !symtab[symidx]) {
+			fprintf(stderr,
+	"error: reloc references invalid symbol #%u in section \"%s\"\n",
+				symidx, sec->name);
+			exit(1);
+		}
+		intrel->sym = symtab[symidx];
+		intrel->type = get_u16(extrel->r_type);
+		switch (intrel->type) {
+		case RTYPE_LONG:
+			intrel->typestr = "Word32";
+			break;
+		case RTYPE_THUMB_BL:
+			intrel->typestr = "Thumb_BL";
+			break;
+		case RTYPE_ARM_B:
+			intrel->typestr = "ARM_B";
+			break;
+		default:
+			fprintf(stderr,
+		"error: reloc in section \"%s\" of unexpected type 0x%x\n",
+				sec->name, intrel->type);
+			exit(1);
+		}
+		if (get_u16(extrel->r_reserved))
+			fprintf(stderr,
+	"warning: reloc in section \"%s\" has non-zero reserved field\n",
+				sec->name);
+	}
+}
+
+cmd_reloc()
+{
+	unsigned n, m;
+	struct internal_scnhdr *sec;
+	struct internal_reloc *rel;
+
+	get_int_section_table();
+	get_int_symbol_table();
+	for (n = 0; n < nsections; n++) {
+		sec = sections + n;
+		if (!sec->nreloc)
+			continue;
+		printf("%s:\n\n", sec->name);
+		get_relocs_of_sec(sec);
+		rel = sec->int_relocs;
+		printf("Location  Type      Symbol relative to\n");
+		for (m = 0; m < sec->nreloc; m++, rel++)
+			printf("%08X  %-8s  %s\n", rel->location, rel->typestr,
+				rel->sym->name);
+		putchar('\n');
+	}
+	exit(0);
+}