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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* NASM-style list format
*
* Copyright (C) 2004-2007 Peter Johnson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <util.h>
#include <libyasm.h>
/* NOTE: For this code to generate relocation information, the relocations
* have to be added by the object format to each section in program source
* order.
*
* This should not be an issue, as program source order == section bytecode
* order, so unless the object formats are very obtuse with their bytecode
* iteration, this should just happen.
*/
#define REGULAR_BUF_SIZE 1024
yasm_listfmt_module yasm_nasm_LTX_listfmt;
typedef struct sectreloc {
/*@reldef@*/ SLIST_ENTRY(sectreloc) link;
yasm_section *sect;
/*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */
unsigned long next_reloc_addr;
} sectreloc;
typedef struct bcreloc {
/*@reldef@*/ STAILQ_ENTRY(bcreloc) link;
unsigned long offset; /* start of reloc from start of bytecode */
size_t size; /* size of reloc in bytes */
int rel; /* PC/IP-relative or "absolute" */
} bcreloc;
typedef struct nasm_listfmt_output_info {
yasm_arch *arch;
/*@reldef@*/ STAILQ_HEAD(bcrelochead, bcreloc) bcrelocs;
/*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */
unsigned long next_reloc_addr;
} nasm_listfmt_output_info;
static /*@null@*/ /*@only@*/ yasm_listfmt *
nasm_listfmt_create(const char *in_filename, const char *obj_filename)
{
yasm_listfmt_base *listfmt = yasm_xmalloc(sizeof(yasm_listfmt_base));
listfmt->module = &yasm_nasm_LTX_listfmt;
return (yasm_listfmt *)listfmt;
}
static void
nasm_listfmt_destroy(/*@only@*/ yasm_listfmt *listfmt)
{
yasm_xfree(listfmt);
}
static int
nasm_listfmt_output_value(yasm_value *value, unsigned char *buf,
unsigned int destsize, unsigned long offset,
yasm_bytecode *bc, int warn, /*@null@*/ void *d)
{
/*@null@*/ nasm_listfmt_output_info *info = (nasm_listfmt_output_info *)d;
/*@dependent@*/ /*@null@*/ yasm_intnum *intn;
unsigned int valsize = value->size;
assert(info != NULL);
/* Output */
switch (yasm_value_output_basic(value, buf, destsize, bc, warn,
info->arch)) {
case -1:
return 1;
case 0:
break;
default:
return 0;
}
/* Generate reloc if needed */
if (info->next_reloc && info->next_reloc_addr == bc->offset+offset) {
bcreloc *reloc = yasm_xmalloc(sizeof(bcreloc));
reloc->offset = offset;
reloc->size = destsize;
reloc->rel = value->curpos_rel;
STAILQ_INSERT_TAIL(&info->bcrelocs, reloc, link);
/* Get next reloc's info */
info->next_reloc = yasm_section_reloc_next(info->next_reloc);
if (info->next_reloc) {
yasm_intnum *addr;
yasm_symrec *sym;
yasm_reloc_get(info->next_reloc, &addr, &sym);
info->next_reloc_addr = yasm_intnum_get_uint(addr);
}
}
if (value->abs) {
intn = yasm_expr_get_intnum(&value->abs, 0);
if (intn)
return yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize,
valsize, 0, bc, 0);
else {
yasm_error_set(YASM_ERROR_TOO_COMPLEX,
N_("relocation too complex"));
return 1;
}
} else {
int retval;
intn = yasm_intnum_create_uint(0);
retval = yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize,
valsize, 0, bc, 0);
yasm_intnum_destroy(intn);
return retval;
}
return 0;
}
static void
nasm_listfmt_output(yasm_listfmt *listfmt, FILE *f, yasm_linemap *linemap,
yasm_arch *arch)
{
yasm_bytecode *bc;
const char *source;
unsigned long line = 1;
unsigned long listline = 1;
/*@only@*/ unsigned char *buf;
nasm_listfmt_output_info info;
/*@reldef@*/ SLIST_HEAD(sectrelochead, sectreloc) reloc_hist;
/*@null@*/ sectreloc *last_hist = NULL;
/*@null@*/ bcreloc *reloc = NULL;
yasm_section *sect;
SLIST_INIT(&reloc_hist);
info.arch = arch;
buf = yasm_xmalloc(REGULAR_BUF_SIZE);
while (!yasm_linemap_get_source(linemap, line, &bc, &source)) {
if (!bc) {
fprintf(f, "%6lu %*s%s\n", listline++, 32, "", source);
} else {
/* get the next relocation for the bytecode's section */
sect = yasm_bc_get_section(bc);
if (!last_hist || last_hist->sect != sect) {
int found = 0;
/* look through reloc_hist for matching section */
SLIST_FOREACH(last_hist, &reloc_hist, link) {
if (last_hist->sect == sect) {
found = 1;
break;
}
}
if (!found) {
/* not found, add to list*/
last_hist = yasm_xmalloc(sizeof(sectreloc));
last_hist->sect = sect;
last_hist->next_reloc = yasm_section_relocs_first(sect);
if (last_hist->next_reloc) {
yasm_intnum *addr;
yasm_symrec *sym;
yasm_reloc_get(last_hist->next_reloc, &addr, &sym);
last_hist->next_reloc_addr =
yasm_intnum_get_uint(addr);
}
SLIST_INSERT_HEAD(&reloc_hist, last_hist, link);
}
}
info.next_reloc = last_hist->next_reloc;
info.next_reloc_addr = last_hist->next_reloc_addr;
STAILQ_INIT(&info.bcrelocs);
/* loop over bytecodes on this line (usually only one) */
while (bc && bc->line == line) {
/*@null@*/ /*@only@*/ unsigned char *bigbuf;
unsigned long size = REGULAR_BUF_SIZE;
long multiple;
unsigned long offset = bc->offset;
unsigned char *origp, *p;
int gap;
/* convert bytecode into bytes, recording relocs along the
* way
*/
bigbuf = yasm_bc_tobytes(bc, buf, &size, &gap, &info,
nasm_listfmt_output_value, NULL);
yasm_bc_get_multiple(bc, &multiple, 1);
if (multiple <= 0)
size = 0;
else
size /= multiple;
/* output bytes with reloc information */
origp = bigbuf ? bigbuf : buf;
p = origp;
reloc = STAILQ_FIRST(&info.bcrelocs);
if (gap) {
fprintf(f, "%6lu %08lX <gap>%*s%s\n", listline++, offset,
18, "", source ? source : "");
} else while (size > 0) {
int i;
fprintf(f, "%6lu %08lX ", listline++, offset);
for (i=0; i<18 && size > 0; size--) {
if (reloc && (unsigned long)(p-origp) ==
reloc->offset) {
fprintf(f, "%c", reloc->rel ? '(' : '[');
i++;
}
fprintf(f, "%02X", *(p++));
i+=2;
if (reloc && (unsigned long)(p-origp) ==
reloc->offset+reloc->size) {
fprintf(f, "%c", reloc->rel ? ')' : ']');
i++;
reloc = STAILQ_NEXT(reloc, link);
}
}
if (size > 0)
fprintf(f, "-");
else {
if (multiple > 1) {
fprintf(f, "<rept>");
i += 6;
}
fprintf(f, "%*s", 18-i+1, "");
}
if (source) {
fprintf(f, " %s", source);
source = NULL;
}
fprintf(f, "\n");
}
if (bigbuf)
yasm_xfree(bigbuf);
bc = STAILQ_NEXT(bc, link);
}
/* delete bcrelocs (newly generated next bytecode if any) */
reloc = STAILQ_FIRST(&info.bcrelocs);
while (reloc) {
bcreloc *reloc2 = STAILQ_NEXT(reloc, link);
yasm_xfree(reloc);
reloc = reloc2;
}
/* save reloc context */
last_hist->next_reloc = info.next_reloc;
last_hist->next_reloc_addr = info.next_reloc_addr;
}
line++;
}
/* delete reloc history */
while (!SLIST_EMPTY(&reloc_hist)) {
last_hist = SLIST_FIRST(&reloc_hist);
SLIST_REMOVE_HEAD(&reloc_hist, link);
yasm_xfree(last_hist);
}
yasm_xfree(buf);
}
/* Define listfmt structure -- see listfmt.h for details */
yasm_listfmt_module yasm_nasm_LTX_listfmt = {
"NASM-style list format",
"nasm",
nasm_listfmt_create,
nasm_listfmt_destroy,
nasm_listfmt_output
};
|