1 /* Interface between the opcode library and its callers.
2 
3    Copyright (C) 1999-2020 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street - Fifth Floor,
18    Boston, MA 02110-1301, USA.
19 
20    Written by Cygnus Support, 1993.
21 
22    The opcode library (libopcodes.a) provides instruction decoders for
23    a large variety of instruction sets, callable with an identical
24    interface, for making instruction-processing programs more independent
25    of the instruction set being processed.  */
26 
27 #ifndef DIS_ASM_H
28 #define DIS_ASM_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include "bfd.h"
37 
38   typedef int (*fprintf_ftype) (void *, const char*, ...) ATTRIBUTE_FPTR_PRINTF_2;
39 
40 enum dis_insn_type
41 {
42   dis_noninsn,			/* Not a valid instruction.  */
43   dis_nonbranch,		/* Not a branch instruction.  */
44   dis_branch,			/* Unconditional branch.  */
45   dis_condbranch,		/* Conditional branch.  */
46   dis_jsr,			/* Jump to subroutine.  */
47   dis_condjsr,			/* Conditional jump to subroutine.  */
48   dis_dref,			/* Data reference instruction.  */
49   dis_dref2			/* Two data references in instruction.  */
50 };
51 
52 /* This struct is passed into the instruction decoding routine,
53    and is passed back out into each callback.  The various fields are used
54    for conveying information from your main routine into your callbacks,
55    for passing information into the instruction decoders (such as the
56    addresses of the callback functions), or for passing information
57    back from the instruction decoders to their callers.
58 
59    It must be initialized before it is first passed; this can be done
60    by hand, or using one of the initialization macros below.  */
61 
62 typedef struct disassemble_info
63 {
64   fprintf_ftype fprintf_func;
65   void *stream;
66   void *application_data;
67 
68   /* Target description.  We could replace this with a pointer to the bfd,
69      but that would require one.  There currently isn't any such requirement
70      so to avoid introducing one we record these explicitly.  */
71   /* The bfd_flavour.  This can be bfd_target_unknown_flavour.  */
72   enum bfd_flavour flavour;
73   /* The bfd_arch value.  */
74   enum bfd_architecture arch;
75   /* The bfd_mach value.  */
76   unsigned long mach;
77   /* Endianness (for bi-endian cpus).  Mono-endian cpus can ignore this.  */
78   enum bfd_endian endian;
79   /* Endianness of code, for mixed-endian situations such as ARM BE8.  */
80   enum bfd_endian endian_code;
81 
82   /* Some targets need information about the current section to accurately
83      display insns.  If this is NULL, the target disassembler function
84      will have to make its best guess.  */
85   asection *section;
86 
87   /* An array of pointers to symbols either at the location being disassembled
88      or at the start of the function being disassembled.  The array is sorted
89      so that the first symbol is intended to be the one used.  The others are
90      present for any misc. purposes.  This is not set reliably, but if it is
91      not NULL, it is correct.  */
92   asymbol **symbols;
93   /* Number of symbols in array.  */
94   int num_symbols;
95 
96   /* Symbol table provided for targets that want to look at it.  This is
97      used on Arm to find mapping symbols and determine Arm/Thumb code.  */
98   asymbol **symtab;
99   int symtab_pos;
100   int symtab_size;
101 
102   /* For use by the disassembler.
103      The top 16 bits are reserved for public use (and are documented here).
104      The bottom 16 bits are for the internal use of the disassembler.  */
105   unsigned long flags;
106   /* Set if the disassembler has determined that there are one or more
107      relocations associated with the instruction being disassembled.  */
108 #define INSN_HAS_RELOC	 (1u << 31)
109   /* Set if the user has requested the disassembly of data as well as code.  */
110 #define DISASSEMBLE_DATA (1u << 30)
111   /* Set if the user has specifically set the machine type encoded in the
112      mach field of this structure.  */
113 #define USER_SPECIFIED_MACHINE_TYPE (1u << 29)
114   /* Set if the user has requested wide output.  */
115 #define WIDE_OUTPUT (1u << 28)
116 
117   /* Use internally by the target specific disassembly code.  */
118   void *private_data;
119 
120   /* Function used to get bytes to disassemble.  MEMADDR is the
121      address of the stuff to be disassembled, MYADDR is the address to
122      put the bytes in, and LENGTH is the number of bytes to read.
123      INFO is a pointer to this struct.
124      Returns an errno value or 0 for success.  */
125   int (*read_memory_func)
126     (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
127      struct disassemble_info *dinfo);
128 
129   /* Function which should be called if we get an error that we can't
130      recover from.  STATUS is the errno value from read_memory_func and
131      MEMADDR is the address that we were trying to read.  INFO is a
132      pointer to this struct.  */
133   void (*memory_error_func)
134     (int status, bfd_vma memaddr, struct disassemble_info *dinfo);
135 
136   /* Function called to print ADDR.  */
137   void (*print_address_func)
138     (bfd_vma addr, struct disassemble_info *dinfo);
139 
140   /* Function called to determine if there is a symbol at the given ADDR.
141      If there is, the function returns 1, otherwise it returns 0.
142      This is used by ports which support an overlay manager where
143      the overlay number is held in the top part of an address.  In
144      some circumstances we want to include the overlay number in the
145      address, (normally because there is a symbol associated with
146      that address), but sometimes we want to mask out the overlay bits.  */
147   int (* symbol_at_address_func)
148     (bfd_vma addr, struct disassemble_info *dinfo);
149 
150   /* Function called to check if a SYMBOL is can be displayed to the user.
151      This is used by some ports that want to hide special symbols when
152      displaying debugging outout.  */
153   bfd_boolean (* symbol_is_valid)
154     (asymbol *, struct disassemble_info *dinfo);
155 
156   /* These are for buffer_read_memory.  */
157   bfd_byte *buffer;
158   bfd_vma buffer_vma;
159   size_t buffer_length;
160 
161   /* This variable may be set by the instruction decoder.  It suggests
162       the number of bytes objdump should display on a single line.  If
163       the instruction decoder sets this, it should always set it to
164       the same value in order to get reasonable looking output.  */
165   int bytes_per_line;
166 
167   /* The next two variables control the way objdump displays the raw data.  */
168   /* For example, if bytes_per_line is 8 and bytes_per_chunk is 4, the */
169   /* output will look like this:
170      00:   00000000 00000000
171      with the chunks displayed according to "display_endian". */
172   int bytes_per_chunk;
173   enum bfd_endian display_endian;
174 
175   /* Number of octets per incremented target address
176      Normally one, but some DSPs have byte sizes of 16 or 32 bits.  */
177   unsigned int octets_per_byte;
178 
179   /* The number of zeroes we want to see at the end of a section before we
180      start skipping them.  */
181   unsigned int skip_zeroes;
182 
183   /* The number of zeroes to skip at the end of a section.  If the number
184      of zeroes at the end is between SKIP_ZEROES_AT_END and SKIP_ZEROES,
185      they will be disassembled.  If there are fewer than
186      SKIP_ZEROES_AT_END, they will be skipped.  This is a heuristic
187      attempt to avoid disassembling zeroes inserted by section
188      alignment.  */
189   unsigned int skip_zeroes_at_end;
190 
191   /* Whether the disassembler always needs the relocations.  */
192   bfd_boolean disassembler_needs_relocs;
193 
194   /* Results from instruction decoders.  Not all decoders yet support
195      this information.  This info is set each time an instruction is
196      decoded, and is only valid for the last such instruction.
197 
198      To determine whether this decoder supports this information, set
199      insn_info_valid to 0, decode an instruction, then check it.  */
200 
201   char insn_info_valid;		/* Branch info has been set. */
202   char branch_delay_insns;	/* How many sequential insn's will run before
203 				   a branch takes effect.  (0 = normal) */
204   char data_size;		/* Size of data reference in insn, in bytes */
205   enum dis_insn_type insn_type;	/* Type of instruction */
206   bfd_vma target;		/* Target address of branch or dref, if known;
207 				   zero if unknown.  */
208   bfd_vma target2;		/* Second target address for dref2 */
209 
210   /* Command line options specific to the target disassembler.  */
211   const char *disassembler_options;
212 
213   /* If non-zero then try not disassemble beyond this address, even if
214      there are values left in the buffer.  This address is the address
215      of the nearest symbol forwards from the start of the disassembly,
216      and it is assumed that it lies on the boundary between instructions.
217      If an instruction spans this address then this is an error in the
218      file being disassembled.  */
219   bfd_vma stop_vma;
220 
221   /* The end range of the current range being disassembled.  This is required
222      in order to notify the disassembler when it's currently handling a
223      different range than it was before.  This prevent unsafe optimizations when
224      disassembling such as the way mapping symbols are found on AArch64.  */
225   bfd_vma stop_offset;
226 
227 } disassemble_info;
228 
229 /* This struct is used to pass information about valid disassembler
230    option arguments from the target to the generic GDB functions
231    that set and display them.  */
232 
233 typedef struct
234 {
235   /* Option argument name to use in descriptions.  */
236   const char *name;
237 
238   /* Vector of acceptable option argument values, NULL-terminated.  */
239   const char **values;
240 } disasm_option_arg_t;
241 
242 /* This struct is used to pass information about valid disassembler
243    options, their descriptions and arguments from the target to the
244    generic GDB functions that set and display them.  Options are
245    defined by tuples of vector entries at each index.  */
246 
247 typedef struct
248 {
249   /* Vector of option names, NULL-terminated.  */
250   const char **name;
251 
252   /* Vector of option descriptions or NULL if none to be shown.  */
253   const char **description;
254 
255   /* Vector of option argument information pointers or NULL if no
256      option accepts an argument.  NULL entries denote individual
257      options that accept no argument.  */
258   const disasm_option_arg_t **arg;
259 } disasm_options_t;
260 
261 /* This struct is used to pass information about valid disassembler
262    options and arguments from the target to the generic GDB functions
263    that set and display them.  */
264 
265 typedef struct
266 {
267   /* Valid disassembler options.  Individual options that support
268      an argument will refer to entries in the ARGS vector.  */
269   disasm_options_t options;
270 
271   /* Vector of acceptable option arguments, NULL-terminated.  This
272      collects all possible option argument choices, some of which
273      may be shared by different options from the OPTIONS member.  */
274   disasm_option_arg_t *args;
275 } disasm_options_and_args_t;
276 
277 /* Standard disassemblers.  Disassemble one instruction at the given
278    target address.  Return number of octets processed.  */
279 typedef int (*disassembler_ftype) (bfd_vma, disassemble_info *);
280 
281 /* Disassemblers used out side of opcodes library.  */
282 extern int print_insn_m32c		(bfd_vma, disassemble_info *);
283 extern int print_insn_mep		(bfd_vma, disassemble_info *);
284 extern int print_insn_s12z		(bfd_vma, disassemble_info *);
285 extern int print_insn_sh		(bfd_vma, disassemble_info *);
286 extern int print_insn_sparc		(bfd_vma, disassemble_info *);
287 extern int print_insn_rx		(bfd_vma, disassemble_info *);
288 extern int print_insn_rl78		(bfd_vma, disassemble_info *);
289 extern int print_insn_rl78_g10		(bfd_vma, disassemble_info *);
290 extern int print_insn_rl78_g13		(bfd_vma, disassemble_info *);
291 extern int print_insn_rl78_g14		(bfd_vma, disassemble_info *);
292 
293 extern disassembler_ftype arc_get_disassembler (bfd *);
294 extern disassembler_ftype cris_get_disassembler (bfd *);
295 
296 extern void print_aarch64_disassembler_options (FILE *);
297 extern void print_i386_disassembler_options (FILE *);
298 extern void print_mips_disassembler_options (FILE *);
299 extern void print_nfp_disassembler_options (FILE *);
300 extern void print_ppc_disassembler_options (FILE *);
301 extern void print_riscv_disassembler_options (FILE *);
302 extern void print_arm_disassembler_options (FILE *);
303 extern void print_arc_disassembler_options (FILE *);
304 extern void print_s390_disassembler_options (FILE *);
305 extern void print_wasm32_disassembler_options (FILE *);
306 extern bfd_boolean aarch64_symbol_is_valid (asymbol *, struct disassemble_info *);
307 extern bfd_boolean arm_symbol_is_valid (asymbol *, struct disassemble_info *);
308 extern bfd_boolean csky_symbol_is_valid (asymbol *, struct disassemble_info *);
309 extern bfd_boolean riscv_symbol_is_valid (asymbol *, struct disassemble_info *);
310 extern void disassemble_init_powerpc (struct disassemble_info *);
311 extern void disassemble_init_s390 (struct disassemble_info *);
312 extern void disassemble_init_wasm32 (struct disassemble_info *);
313 extern void disassemble_init_nds32 (struct disassemble_info *);
314 extern const disasm_options_and_args_t *disassembler_options_arm (void);
315 extern const disasm_options_and_args_t *disassembler_options_mips (void);
316 extern const disasm_options_and_args_t *disassembler_options_powerpc (void);
317 extern const disasm_options_and_args_t *disassembler_options_s390 (void);
318 
319 /* Fetch the disassembler for a given architecture ARC, endianess (big
320    endian if BIG is true), bfd_mach value MACH, and ABFD, if that support
321    is available.  ABFD may be NULL.  */
322 extern disassembler_ftype disassembler (enum bfd_architecture arc,
323 					bfd_boolean big, unsigned long mach,
324 					bfd *abfd);
325 
326 /* Amend the disassemble_info structure as necessary for the target architecture.
327    Should only be called after initialising the info->arch field.  */
328 extern void disassemble_init_for_target (struct disassemble_info *);
329 
330 /* Tidy any memory allocated by targets, such as info->private_data.  */
331 extern void disassemble_free_target (struct disassemble_info *);
332 
333 /* Document any target specific options available from the disassembler.  */
334 extern void disassembler_usage (FILE *);
335 
336 /* Remove whitespace and consecutive commas.  */
337 extern char *remove_whitespace_and_extra_commas (char *);
338 
339 /* Like STRCMP, but treat ',' the same as '\0' so that we match
340    strings like "foobar" against "foobar,xxyyzz,...".  */
341 extern int disassembler_options_cmp (const char *, const char *);
342 
343 /* A helper function for FOR_EACH_DISASSEMBLER_OPTION.  */
344 static inline const char *
345 next_disassembler_option (const char *options)
346 {
347   const char *opt = strchr (options, ',');
348   if (opt != NULL)
349     opt++;
350   return opt;
351 }
352 
353 /* A macro for iterating over each comma separated option in OPTIONS.  */
354 #define FOR_EACH_DISASSEMBLER_OPTION(OPT, OPTIONS) \
355   for ((OPT) = (OPTIONS); \
356        (OPT) != NULL; \
357        (OPT) = next_disassembler_option (OPT))
358 
359 
360 /* This block of definitions is for particular callers who read instructions
361    into a buffer before calling the instruction decoder.  */
362 
363 /* Here is a function which callers may wish to use for read_memory_func.
364    It gets bytes from a buffer.  */
365 extern int buffer_read_memory
366   (bfd_vma, bfd_byte *, unsigned int, struct disassemble_info *);
367 
368 /* This function goes with buffer_read_memory.
369    It prints a message using info->fprintf_func and info->stream.  */
370 extern void perror_memory (int, bfd_vma, struct disassemble_info *);
371 
372 
373 /* Just print the address in hex.  This is included for completeness even
374    though both GDB and objdump provide their own (to print symbolic
375    addresses).  */
376 extern void generic_print_address
377   (bfd_vma, struct disassemble_info *);
378 
379 /* Always true.  */
380 extern int generic_symbol_at_address
381   (bfd_vma, struct disassemble_info *);
382 
383 /* Also always true.  */
384 extern bfd_boolean generic_symbol_is_valid
385   (asymbol *, struct disassemble_info *);
386 
387 /* Method to initialize a disassemble_info struct.  This should be
388    called by all applications creating such a struct.  */
389 extern void init_disassemble_info (struct disassemble_info *dinfo, void *stream,
390 				   fprintf_ftype fprintf_func);
391 
392 /* For compatibility with existing code.  */
393 #define INIT_DISASSEMBLE_INFO(INFO, STREAM, FPRINTF_FUNC) \
394   init_disassemble_info (&(INFO), (STREAM), (fprintf_ftype) (FPRINTF_FUNC))
395 
396 #ifdef __cplusplus
397 }
398 #endif
399 
400 #endif /* ! defined (DIS_ASM_H) */
401