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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
/* xtensa-dis.c. Disassembly functions for Xtensa.
Copyright (C) 2003-2018 Free Software Foundation, Inc.
Contributed by Bob Wilson at Tensilica, Inc. (bwilson@tensilica.com)
This file is part of the GNU opcodes library.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this file; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include "xtensa-isa.h"
#include "ansidecl.h"
#include "libiberty.h"
#include "bfd.h"
#include "elf/xtensa.h"
#include "disassemble.h"
#include <setjmp.h>
extern xtensa_isa xtensa_default_isa;
#ifndef MAX
#define MAX(a,b) (a > b ? a : b)
#endif
int show_raw_fields;
struct dis_private
{
bfd_byte *byte_buf;
OPCODES_SIGJMP_BUF bailout;
/* Persistent fields, valid for last_section only. */
asection *last_section;
property_table_entry *insn_table_entries;
int insn_table_entry_count;
/* Cached property table search position. */
bfd_vma insn_table_cur_addr;
int insn_table_cur_idx;
};
static void
xtensa_coalesce_insn_tables (struct dis_private *priv)
{
const int mask = ~(XTENSA_PROP_DATA | XTENSA_PROP_NO_TRANSFORM);
int count = priv->insn_table_entry_count;
int i, j;
/* Loop over all entries, combining adjacent ones that differ only in
the flag bits XTENSA_PROP_DATA and XTENSA_PROP_NO_TRANSFORM. */
for (i = j = 0; j < count; ++i)
{
property_table_entry *entry = priv->insn_table_entries + i;
*entry = priv->insn_table_entries[j];
for (++j; j < count; ++j)
{
property_table_entry *next = priv->insn_table_entries + j;
int fill = xtensa_compute_fill_extra_space (entry);
int size = entry->size + fill;
if (entry->address + size == next->address)
{
int entry_flags = entry->flags & mask;
int next_flags = next->flags & mask;
if (next_flags == entry_flags)
entry->size = next->address - entry->address + next->size;
else
break;
}
else
{
break;
}
}
}
priv->insn_table_entry_count = i;
}
static property_table_entry *
xtensa_find_table_entry (bfd_vma memaddr, struct disassemble_info *info)
{
struct dis_private *priv = (struct dis_private *) info->private_data;
int i;
if (priv->insn_table_entries == NULL
|| priv->insn_table_entry_count < 0)
return NULL;
if (memaddr < priv->insn_table_cur_addr)
priv->insn_table_cur_idx = 0;
for (i = priv->insn_table_cur_idx; i < priv->insn_table_entry_count; ++i)
{
property_table_entry *block = priv->insn_table_entries + i;
if (block->size != 0)
{
if ((memaddr >= block->address
&& memaddr < block->address + block->size)
|| memaddr < block->address)
{
priv->insn_table_cur_addr = memaddr;
priv->insn_table_cur_idx = i;
return block;
}
}
}
return NULL;
}
/* Check whether an instruction crosses an instruction block boundary
(according to property tables).
If it does, return 0 (doesn't fit), else return 1. */
static int
xtensa_instruction_fits (bfd_vma memaddr, int size,
property_table_entry *insn_block)
{
unsigned max_size;
/* If no property table info, assume it fits. */
if (insn_block == NULL || size <= 0)
return 1;
/* If too high, limit nextstop by the next insn address. */
if (insn_block->address > memaddr)
{
/* memaddr is not in an instruction block, but is followed by one. */
max_size = insn_block->address - memaddr;
}
else
{
/* memaddr is in an instruction block, go no further than the end. */
max_size = insn_block->address + insn_block->size - memaddr;
}
/* Crossing a boundary, doesn't "fit". */
if ((unsigned)size > max_size)
return 0;
return 1;
}
static int
fetch_data (struct disassemble_info *info, bfd_vma memaddr)
{
int length, status = 0;
struct dis_private *priv = (struct dis_private *) info->private_data;
int insn_size = xtensa_isa_maxlength (xtensa_default_isa);
insn_size = MAX (insn_size, 4);
/* Read the maximum instruction size, padding with zeros if we go past
the end of the text section. This code will automatically adjust
length when we hit the end of the buffer. */
memset (priv->byte_buf, 0, insn_size);
for (length = insn_size; length > 0; length--)
{
status = (*info->read_memory_func) (memaddr, priv->byte_buf, length,
info);
if (status == 0)
return length;
}
(*info->memory_error_func) (status, memaddr, info);
OPCODES_SIGLONGJMP (priv->bailout, 1);
/*NOTREACHED*/
}
static void
print_xtensa_operand (bfd_vma memaddr,
struct disassemble_info *info,
xtensa_opcode opc,
int opnd,
unsigned operand_val)
{
xtensa_isa isa = xtensa_default_isa;
int signed_operand_val;
if (show_raw_fields)
{
if (operand_val < 0xa)
(*info->fprintf_func) (info->stream, "%u", operand_val);
else
(*info->fprintf_func) (info->stream, "0x%x", operand_val);
return;
}
(void) xtensa_operand_decode (isa, opc, opnd, &operand_val);
signed_operand_val = (int) operand_val;
if (xtensa_operand_is_register (isa, opc, opnd) == 0)
{
if (xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
{
(void) xtensa_operand_undo_reloc (isa, opc, opnd,
&operand_val, memaddr);
info->target = operand_val;
(*info->print_address_func) (info->target, info);
}
else
{
if ((signed_operand_val > -256) && (signed_operand_val < 256))
(*info->fprintf_func) (info->stream, "%d", signed_operand_val);
else
(*info->fprintf_func) (info->stream, "0x%x", signed_operand_val);
}
}
else
{
int i = 1;
xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
(*info->fprintf_func) (info->stream, "%s%u",
xtensa_regfile_shortname (isa, opnd_rf),
operand_val);
while (i < xtensa_operand_num_regs (isa, opc, opnd))
{
operand_val++;
(*info->fprintf_func) (info->stream, ":%s%u",
xtensa_regfile_shortname (isa, opnd_rf),
operand_val);
i++;
}
}
}
/* Print the Xtensa instruction at address MEMADDR on info->stream.
Returns length of the instruction in bytes. */
int
print_insn_xtensa (bfd_vma memaddr, struct disassemble_info *info)
{
unsigned operand_val;
int bytes_fetched, size, maxsize, i, n, noperands, nslots;
xtensa_isa isa;
xtensa_opcode opc;
xtensa_format fmt;
static struct dis_private priv;
static bfd_byte *byte_buf = NULL;
static xtensa_insnbuf insn_buffer = NULL;
static xtensa_insnbuf slot_buffer = NULL;
int first, first_slot, valid_insn;
property_table_entry *insn_block;
if (!xtensa_default_isa)
xtensa_default_isa = xtensa_isa_init (0, 0);
info->target = 0;
maxsize = xtensa_isa_maxlength (xtensa_default_isa);
/* Set bytes_per_line to control the amount of whitespace between the hex
values and the opcode. For Xtensa, we always print one "chunk" and we
vary bytes_per_chunk to determine how many bytes to print. (objdump
would apparently prefer that we set bytes_per_chunk to 1 and vary
bytes_per_line but that makes it hard to fit 64-bit instructions on
an 80-column screen.) The value of bytes_per_line here is not exactly
right, because objdump adds an extra space for each chunk so that the
amount of whitespace depends on the chunk size. Oh well, it's good
enough.... Note that we set the minimum size to 4 to accomodate
literal pools. */
info->bytes_per_line = MAX (maxsize, 4);
/* Allocate buffers the first time through. */
if (!insn_buffer)
{
insn_buffer = xtensa_insnbuf_alloc (xtensa_default_isa);
slot_buffer = xtensa_insnbuf_alloc (xtensa_default_isa);
byte_buf = (bfd_byte *) xmalloc (MAX (maxsize, 4));
}
priv.byte_buf = byte_buf;
info->private_data = (void *) &priv;
/* Prepare instruction tables. */
if (info->section != NULL)
{
asection *section = info->section;
if (priv.last_section != section)
{
bfd *abfd = section->owner;
if (priv.last_section != NULL)
{
/* Reset insn_table_entries. */
priv.insn_table_entry_count = 0;
if (priv.insn_table_entries)
free (priv.insn_table_entries);
priv.insn_table_entries = NULL;
}
priv.last_section = section;
/* Read insn_table_entries. */
priv.insn_table_entry_count =
xtensa_read_table_entries (abfd, section,
&priv.insn_table_entries,
XTENSA_PROP_SEC_NAME, FALSE);
if (priv.insn_table_entry_count == 0)
{
if (priv.insn_table_entries)
free (priv.insn_table_entries);
priv.insn_table_entries = NULL;
/* Backwards compatibility support. */
priv.insn_table_entry_count =
xtensa_read_table_entries (abfd, section,
&priv.insn_table_entries,
XTENSA_INSN_SEC_NAME, FALSE);
}
priv.insn_table_cur_idx = 0;
xtensa_coalesce_insn_tables (&priv);
}
/* Else nothing to do, same section as last time. */
}
if (OPCODES_SIGSETJMP (priv.bailout) != 0)
/* Error return. */
return -1;
/* Fetch the maximum size instruction. */
bytes_fetched = fetch_data (info, memaddr);
insn_block = xtensa_find_table_entry (memaddr, info);
/* Don't set "isa" before the setjmp to keep the compiler from griping. */
isa = xtensa_default_isa;
size = 0;
nslots = 0;
valid_insn = 0;
fmt = 0;
if (!insn_block || (insn_block->flags & XTENSA_PROP_INSN))
{
/* Copy the bytes into the decode buffer. */
memset (insn_buffer, 0, (xtensa_insnbuf_size (isa) *
sizeof (xtensa_insnbuf_word)));
xtensa_insnbuf_from_chars (isa, insn_buffer, priv.byte_buf,
bytes_fetched);
fmt = xtensa_format_decode (isa, insn_buffer);
if (fmt != XTENSA_UNDEFINED
&& ((size = xtensa_format_length (isa, fmt)) <= bytes_fetched)
&& xtensa_instruction_fits (memaddr, size, insn_block))
{
/* Make sure all the opcodes are valid. */
valid_insn = 1;
nslots = xtensa_format_num_slots (isa, fmt);
for (n = 0; n < nslots; n++)
{
xtensa_format_get_slot (isa, fmt, n, insn_buffer, slot_buffer);
if (xtensa_opcode_decode (isa, fmt, n, slot_buffer)
== XTENSA_UNDEFINED)
{
valid_insn = 0;
break;
}
}
}
}
if (!valid_insn)
{
if (insn_block && (insn_block->flags & XTENSA_PROP_LITERAL)
&& (memaddr & 3) == 0 && bytes_fetched >= 4)
{
return 4;
}
else
{
(*info->fprintf_func) (info->stream, ".byte %#02x", priv.byte_buf[0]);
return 1;
}
}
if (nslots > 1)
(*info->fprintf_func) (info->stream, "{ ");
first_slot = 1;
for (n = 0; n < nslots; n++)
{
if (first_slot)
first_slot = 0;
else
(*info->fprintf_func) (info->stream, "; ");
xtensa_format_get_slot (isa, fmt, n, insn_buffer, slot_buffer);
opc = xtensa_opcode_decode (isa, fmt, n, slot_buffer);
(*info->fprintf_func) (info->stream, "%s",
xtensa_opcode_name (isa, opc));
/* Print the operands (if any). */
noperands = xtensa_opcode_num_operands (isa, opc);
first = 1;
for (i = 0; i < noperands; i++)
{
if (xtensa_operand_is_visible (isa, opc, i) == 0)
continue;
if (first)
{
(*info->fprintf_func) (info->stream, "\t");
first = 0;
}
else
(*info->fprintf_func) (info->stream, ", ");
(void) xtensa_operand_get_field (isa, opc, i, fmt, n,
slot_buffer, &operand_val);
print_xtensa_operand (memaddr, info, opc, i, operand_val);
}
}
if (nslots > 1)
(*info->fprintf_func) (info->stream, " }");
info->bytes_per_chunk = size;
info->display_endian = info->endian;
return size;
}
|