comparison leo-obj/tool/reloc.c @ 130:87b82398a08b

leo-obj project subtree started, tiobjd tool moved into it
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 06 Apr 2014 22:14:39 +0000
parents ticoff/reloc.c@d88f2f40e3ae
children
comparison
equal deleted inserted replaced
129:597143ba1c37 130:87b82398a08b
1 /*
2 * Handling of relocation records
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "filestruct.h"
9 #include "intstruct.h"
10 #include "coffconst.h"
11 #include "globals.h"
12
13 extern unsigned get_u16(), get_u32();
14
15 cmd_rawrel()
16 {
17 unsigned n, m;
18 struct internal_scnhdr *sec;
19 struct external_reloc *rel;
20
21 get_int_section_table();
22 for (n = 0; n < nsections; n++) {
23 sec = sections + n;
24 if (!sec->nreloc)
25 continue;
26 printf("%s:\n\n", sec->name);
27 rel = (struct external_reloc *)(filemap + sec->reloc_offset);
28 printf("Location SymIndex Rsvd Type\n");
29 for (m = 0; m < sec->nreloc; m++, rel++)
30 printf("%08X %08X %04X %04X\n",
31 get_u32(rel->r_vaddr), get_u32(rel->r_symndx),
32 get_u16(rel->r_reserved), get_u16(rel->r_type));
33 putchar('\n');
34 }
35 exit(0);
36 }
37
38 void
39 get_relocs_of_sec(sec)
40 struct internal_scnhdr *sec;
41 {
42 unsigned n;
43 struct external_reloc *extrel;
44 struct internal_reloc *intrel;
45 unsigned lastloc, symidx;
46
47 if (sec->int_relocs)
48 return;
49 if (!sec->nreloc) {
50 fprintf(stderr,
51 "BUG: get_relocs_of_sec() called for section \"%s\" w/o relocs\n",
52 sec->name);
53 exit(1);
54 }
55 intrel = malloc(sizeof(struct internal_reloc) * sec->nreloc);
56 if (!intrel) {
57 perror("malloc");
58 exit(1);
59 }
60 sec->int_relocs = intrel;
61 extrel = (struct external_reloc *)(filemap + sec->reloc_offset);
62 for (n = 0; n < sec->nreloc; n++, extrel++, intrel++) {
63 intrel->location = get_u32(extrel->r_vaddr);
64 if (n && intrel->location <= lastloc) {
65 fprintf(stderr,
66 "error: non-increasing reloc order in section \"%s\"\n",
67 sec->name);
68 exit(1);
69 }
70 lastloc = intrel->location;
71 symidx = get_u32(extrel->r_symndx);
72 if (symidx == 0xFFFFFFFF)
73 intrel->sym = 0;
74 else if (symidx >= nsymtab || !symtab[symidx]) {
75 fprintf(stderr,
76 "error: reloc references invalid symbol #%u in section \"%s\"\n",
77 symidx, sec->name);
78 exit(1);
79 } else
80 intrel->sym = symtab[symidx];
81 intrel->type = get_u16(extrel->r_type);
82 switch (intrel->type) {
83 case RTYPE_LONG:
84 intrel->typestr = "Word32";
85 break;
86 case RTYPE_THUMB_BL:
87 intrel->typestr = "Thumb_BL";
88 break;
89 case RTYPE_ARM_B:
90 intrel->typestr = "ARM_B";
91 break;
92 default:
93 fprintf(stderr,
94 "error: reloc in section \"%s\" of unexpected type 0x%x\n",
95 sec->name, intrel->type);
96 exit(1);
97 }
98 if (get_u16(extrel->r_reserved))
99 fprintf(stderr,
100 "warning: reloc in section \"%s\" has non-zero reserved field\n",
101 sec->name);
102 }
103 }
104
105 cmd_reloc()
106 {
107 unsigned n, m;
108 struct internal_scnhdr *sec;
109 struct internal_reloc *rel;
110 char *symname;
111
112 get_int_section_table();
113 get_int_symbol_table();
114 for (n = 0; n < nsections; n++) {
115 sec = sections + n;
116 if (!sec->nreloc)
117 continue;
118 printf("%s:\n\n", sec->name);
119 get_relocs_of_sec(sec);
120 rel = sec->int_relocs;
121 printf("Location Type Symbol relative to\n");
122 for (m = 0; m < sec->nreloc; m++, rel++) {
123 if (rel->sym)
124 symname = rel->sym->name;
125 else
126 symname = "<none>";
127 printf("%08X %-8s %s\n", rel->location, rel->typestr,
128 symname);
129 }
130 putchar('\n');
131 }
132 exit(0);
133 }
134
135 struct internal_reloc *
136 find_reloc(sec, loc)
137 struct internal_scnhdr *sec;
138 unsigned loc;
139 {
140 struct internal_reloc *rel;
141 unsigned m;
142
143 rel = sec->int_relocs;
144 for (m = 0; m < sec->nreloc; m++, rel++) {
145 if (rel->location == loc)
146 return(rel);
147 if (rel->location > loc)
148 return(0);
149 }
150 return(0);
151 }
152
153 struct internal_reloc *
154 find_word32_reloc(sec, loc)
155 struct internal_scnhdr *sec;
156 unsigned loc;
157 {
158 struct internal_reloc *rel;
159
160 rel = find_reloc(sec, loc);
161 if (rel && rel->type != RTYPE_LONG)
162 rel = 0;
163 return(rel);
164 }