annotate objgrep/dumpmatch.c @ 172:452baa748747

objgrep: -r implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 04 Jul 2014 03:22:41 +0000
parents ddbfc1a1a811
children 77cd647375e5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * objgrep: output on matches
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <stdio.h>
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdint.h>
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <stdlib.h>
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <string.h>
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <strings.h>
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include "intstruct.h"
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include "coffconst.h"
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include "globals.h"
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 extern unsigned get_u16(), get_u32();
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 dump_symtab_on_match()
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 {
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 unsigned n;
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 struct internal_syment *sym;
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 printf("\nThis source section's symbols:\n\n");
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 for (n = 0; n < nsymtab; n++) {
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 sym = symtab[n];
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 if (!sym)
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 continue;
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 if (sym->section != grep_section)
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 continue;
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 switch (sym->class) {
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 case C_EXT:
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 printf("%s = %08X\n", sym->name,
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 sym->value + match_offset);
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 continue;
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 case C_STAT:
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 printf("%s:%s = %08X\n", objfilename, sym->name,
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 sym->value + match_offset);
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 continue;
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 }
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 }
ddbfc1a1a811 objgrep: -s implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 }
172
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
41
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
42 static unsigned
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
43 arm_branch_reloc(location)
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
44 unsigned location;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
45 {
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
46 unsigned word, dest;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
47
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
48 word = get_u32(binfilemap + location);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
49 dest = (word & 0x00FFFFFF) << 2;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
50 if (dest & 0x02000000)
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
51 dest |= 0xFC000000;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
52 dest += location + 8;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
53 return(dest);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
54 }
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
55
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
56 static unsigned
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
57 thumb_bl_reloc(location)
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
58 unsigned location;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
59 {
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
60 unsigned ins1, ins2;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
61 unsigned dest;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
62
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
63 ins1 = get_u16(binfilemap + location);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
64 ins2 = get_u16(binfilemap + location + 2);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
65 dest = ((ins1 & 0x7FF) << 12) | ((ins2 & 0x7FF) << 1);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
66 if (dest & 0x00400000)
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
67 dest |= 0xFF800000;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
68 dest += location + 4;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
69 return(dest);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
70 }
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
71
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
72 dump_relocs_on_match()
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
73 {
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
74 unsigned n, value;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
75 struct internal_reloc *rel;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
76
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
77 printf("\nDeduced from relocs:\n\n");
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
78 for (n = 0, rel = relocs; n < grep_section->nreloc; n++, rel++) {
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
79 switch (rel->type) {
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
80 case RTYPE_LONG:
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
81 value = get_u32(binfilemap + match_offset +
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
82 rel->location);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
83 break;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
84 case RTYPE_ARM_B:
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
85 value = arm_branch_reloc(match_offset + rel->location);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
86 break;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
87 case RTYPE_THUMB_BL:
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
88 value = thumb_bl_reloc(match_offset + rel->location);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
89 break;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
90 default:
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
91 fprintf(stderr,
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
92 "BUG in dump_relocs_on_match(): bad reloc type\n");
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
93 abort();
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
94 }
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
95 value -= rel->addend;
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
96 if (rel->secbase)
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
97 printf("%s:%s = %08X\n", objfilename,
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
98 rel->secbase->name, value);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
99 else if (rel->extsym)
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
100 printf("%s = %08X\n", rel->extsym->name, value);
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
101 else {
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
102 fprintf(stderr,
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
103 "BUG in dump_relocs_on_match(): no base for reloc\n");
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
104 abort();
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
105 }
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
106 }
452baa748747 objgrep: -r implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 171
diff changeset
107 }