1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/* data.c
* This file is part of Decomp - a decompiler. In this file are
* routines for disassembling data sections from an object file.
*
* Copyright (C) 2001, Jonathan duSaint <dusaint@earthlink.net>
*
* Started around 1 December 2001.
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
#include "decomp.h"
/* Whether or not to align the datum size. */
int unalign;
/* find_size
* Guess the size of the datum at OFFSET.
*/
unsigned long
find_size (hash_t symtab, char *data, unsigned long base,
unsigned long offset, unsigned long limit)
{
unsigned long seek;
/* check for a zero byte or for another symbol */
for (seek = offset + 1; data[seek]; seek++) {
if (hash_get (symtab, base + seek) != NULL) { unalign = 1; break; }
}
return seek - offset;
}
/* check_is_string
* Determine if STR, which is LEN bytes long, is a string or not, using
* a heuristic.
*/
int
check_is_string (char *str, unsigned long len)
{
unsigned long k = -1;
if (len < 2) return 0;
if (str[len - 1] != '\0' && str[len] != '\0') return 0;
while (++k < len - 1) if (!isgraph (str[k]) && !isspace (str[k])) return 0;
return 1;
}
/* escape_string
* Convert chars that need escaping into an escape. The returned string
* must be freed.
*/
char *
escape_string (char *str)
{
unsigned long k, o;
char *esc;
/* allocate a lot */
esc = xmalloc (strlen (str) * 2 + 1);
for (k = 0, o = 0; k < strlen (str); k++)
switch (str[k])
{
case '\f':
esc[o++] = '\\';
esc[o++] = 'f';
break;
case '\n':
esc[o++] = '\\';
esc[o++] = 'n';
break;
case '\r':
esc[o++] = '\\';
esc[o++] = 'r';
break;
case '\t':
esc[o++] = '\\';
esc[o++] = 't';
break;
case '\v':
esc[o++] = '\\';
esc[o++] = 'v';
break;
case '"':
esc[o++] = '\\';
esc[o++] = '"';
break;
case '\\':
esc[o++] = '\\';
esc[o++] = '\\';
break;
default:
esc[o++] = str[k];
break;
}
return esc;
}
/* print_symbol
* Print the named object at VADDR.
*/
void
print_symbol (hash_t symtab, struct symtab *sym, unsigned long load_addr,
unsigned long *offset, unsigned long limit,
unsigned long align, char *data, FILE *ofp)
{
unsigned long k, size;
char *buff;
unalign = 0;
if (sym->size == 0)
size = find_size (symtab, data, load_addr, *offset, limit);
else
size = sym->size;
/* double check the size */
if (size == 0) /* rare but possible */
size = align;
buff = xmalloc (size);
memcpy (buff, data + *offset, size);
/* fprintf (ofp, "\t.size\t%ld\n", size); */
if (check_is_string (buff, size))
{
char *esc = escape_string (buff);
fprintf (ofp, "\t.string\t\"%s\"", esc);
xfree (esc);
}
else
{
/* if we guessed the size, it's not a string and there isn't a symbol
immediately after, round up to align */
if (sym->size == 0 && !unalign) while (size % align) size++;
switch (size)
{
case 1:
fprintf (ofp, "\t.byte\t%#x", *buff);
break;
case 2:
fprintf (ofp, "\t.hword\t%#hx", *(uint16_t *)buff);
break;
case 4:
fprintf (ofp, "\t.int\t%#x", *(uint32_t *)buff);
break;
case 8:
fprintf (ofp, "\t.quad\t%#llx", *(uint64_t *)buff);
break;
default:
for (k = 0; k < size; k++)
{
if (!(k % 8)) fprintf (ofp, "\n\t.byte\t0x%02hhx", buff[k]);
else fprintf (ofp, ", 0x%02hhx", buff[k]);
}
}
}
fputc ('\n', ofp);
xfree (buff);
*offset += size;
}
/* decode_data_section
* Disassemble a data section into named and unnamed chunks of data.
*/
void
decode_data_section (struct file_info *fi, struct section_info *s,
hash_t symtab, FILE *ofp)
{
int need_to_print_header = 1;
unsigned long offset = 0;
struct symtab *sym;
char *data;
data = xmalloc (s->section_size);
fseek (fi->fp, s->section_offset, SEEK_SET);
if (fread (data, sizeof (char), s->section_size, fi->fp) < s->section_size)
error_out (ERR_READ_SECTION);
fprintf (ofp, "\n\n\t.section\t%s%s\n\t.align\t%d\n", s->section_name,
s->flags, s->align);
for (offset = 0; offset < s->section_size;)
{
sym = hash_get (symtab, s->load_address + offset);
if (sym != NULL)
{
fputc ('\n', ofp);
/* if this sym is global, mark it */
if (sym->type == GLOBAL_DATA || sym->type == GLOBAL_UNKNOWN)
fprintf (ofp, "\n\t.global\t%s", sym->name);
/* if it's data, say so */
if (sym->type == GLOBAL_DATA || sym->type == LOCAL_DATA
|| sym->type == UNKNOWN_DATA)
fprintf (ofp, "\n\t.type\t%s,@object", sym->name);
if (sym->size != 0) fprintf (ofp, "\n\t.size\t%s, %ld", sym->name,
sym->size);
fprintf (ofp, "\n%s:", sym->name);
print_symbol (symtab, sym, s->load_address, &offset, s->section_size,
s->align, data, ofp);
/* skip to next aligned address */
/* this doesn't seem to be a good idea - not sure why */
/* maybe it's because data isn't always aligned in the object file */
/* while ((s->load_address + offset) % s->align) offset++; */
need_to_print_header = 1;
}
else /* no data - just print out the section contents */
{
char byte = data[offset++];
if (!((s->load_address + offset) % 8) || need_to_print_header)
{
fprintf (ofp, "\n\t.byte\t0x%02hhx", byte);
need_to_print_header = 0;
}
else
fprintf (ofp, ", 0x%02hhx", byte);
}
}
}
|