Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-05-18 08:29:48

0001 /* Interface between the opcode library and its callers.
0002 
0003    Copyright (C) 1999-2022 Free Software Foundation, Inc.
0004 
0005    This program is free software; you can redistribute it and/or modify
0006    it under the terms of the GNU General Public License as published by
0007    the Free Software Foundation; either version 3, or (at your option)
0008    any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013    GNU General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; if not, write to the Free Software
0017    Foundation, Inc., 51 Franklin Street - Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 
0020    Written by Cygnus Support, 1993.
0021 
0022    The opcode library (libopcodes.a) provides instruction decoders for
0023    a large variety of instruction sets, callable with an identical
0024    interface, for making instruction-processing programs more independent
0025    of the instruction set being processed.  */
0026 
0027 #ifndef DIS_ASM_H
0028 #define DIS_ASM_H
0029 
0030 #ifdef __cplusplus
0031 extern "C" {
0032 #endif
0033 
0034 #include <stdio.h>
0035 #include <string.h>
0036 #include "bfd.h"
0037 
0038   typedef int (*fprintf_ftype) (void *, const char*, ...) ATTRIBUTE_FPTR_PRINTF_2;
0039 
0040 enum dis_insn_type
0041 {
0042   dis_noninsn,          /* Not a valid instruction.  */
0043   dis_nonbranch,        /* Not a branch instruction.  */
0044   dis_branch,           /* Unconditional branch.  */
0045   dis_condbranch,       /* Conditional branch.  */
0046   dis_jsr,          /* Jump to subroutine.  */
0047   dis_condjsr,          /* Conditional jump to subroutine.  */
0048   dis_dref,         /* Data reference instruction.  */
0049   dis_dref2         /* Two data references in instruction.  */
0050 };
0051 
0052 /* This struct is passed into the instruction decoding routine,
0053    and is passed back out into each callback.  The various fields are used
0054    for conveying information from your main routine into your callbacks,
0055    for passing information into the instruction decoders (such as the
0056    addresses of the callback functions), or for passing information
0057    back from the instruction decoders to their callers.
0058 
0059    It must be initialized before it is first passed; this can be done
0060    by hand, or using one of the initialization macros below.  */
0061 
0062 typedef struct disassemble_info
0063 {
0064   fprintf_ftype fprintf_func;
0065   void *stream;
0066   void *application_data;
0067 
0068   /* Target description.  We could replace this with a pointer to the bfd,
0069      but that would require one.  There currently isn't any such requirement
0070      so to avoid introducing one we record these explicitly.  */
0071   /* The bfd_flavour.  This can be bfd_target_unknown_flavour.  */
0072   enum bfd_flavour flavour;
0073   /* The bfd_arch value.  */
0074   enum bfd_architecture arch;
0075   /* The bfd_mach value.  */
0076   unsigned long mach;
0077   /* Endianness (for bi-endian cpus).  Mono-endian cpus can ignore this.  */
0078   enum bfd_endian endian;
0079   /* Endianness of code, for mixed-endian situations such as ARM BE8.  */
0080   enum bfd_endian endian_code;
0081 
0082   /* Some targets need information about the current section to accurately
0083      display insns.  If this is NULL, the target disassembler function
0084      will have to make its best guess.  */
0085   asection *section;
0086 
0087   /* An array of pointers to symbols either at the location being disassembled
0088      or at the start of the function being disassembled.  The array is sorted
0089      so that the first symbol is intended to be the one used.  The others are
0090      present for any misc. purposes.  This is not set reliably, but if it is
0091      not NULL, it is correct.  */
0092   asymbol **symbols;
0093   /* Number of symbols in array.  */
0094   int num_symbols;
0095 
0096   /* Symbol table provided for targets that want to look at it.  This is
0097      used on Arm to find mapping symbols and determine Arm/Thumb code.  */
0098   asymbol **symtab;
0099   int symtab_pos;
0100   int symtab_size;
0101 
0102   /* For use by the disassembler.
0103      The top 16 bits are reserved for public use (and are documented here).
0104      The bottom 16 bits are for the internal use of the disassembler.  */
0105   unsigned long flags;
0106   /* Set if the disassembler has determined that there are one or more
0107      relocations associated with the instruction being disassembled.  */
0108 #define INSN_HAS_RELOC   (1u << 31)
0109   /* Set if the user has requested the disassembly of data as well as code.  */
0110 #define DISASSEMBLE_DATA (1u << 30)
0111   /* Set if the user has specifically set the machine type encoded in the
0112      mach field of this structure.  */
0113 #define USER_SPECIFIED_MACHINE_TYPE (1u << 29)
0114   /* Set if the user has requested wide output.  */
0115 #define WIDE_OUTPUT (1u << 28)
0116 
0117   /* Dynamic relocations, if they have been loaded.  */
0118   arelent **dynrelbuf;
0119   long dynrelcount;
0120 
0121   /* Use internally by the target specific disassembly code.  */
0122   void *private_data;
0123 
0124   /* Function used to get bytes to disassemble.  MEMADDR is the
0125      address of the stuff to be disassembled, MYADDR is the address to
0126      put the bytes in, and LENGTH is the number of bytes to read.
0127      INFO is a pointer to this struct.
0128      Returns an errno value or 0 for success.  */
0129   int (*read_memory_func)
0130     (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
0131      struct disassemble_info *dinfo);
0132 
0133   /* Function which should be called if we get an error that we can't
0134      recover from.  STATUS is the errno value from read_memory_func and
0135      MEMADDR is the address that we were trying to read.  INFO is a
0136      pointer to this struct.  */
0137   void (*memory_error_func)
0138     (int status, bfd_vma memaddr, struct disassemble_info *dinfo);
0139 
0140   /* Function called to print ADDR.  */
0141   void (*print_address_func)
0142     (bfd_vma addr, struct disassemble_info *dinfo);
0143 
0144   /* Function called to determine if there is a symbol at the given ADDR.
0145      If there is, the function returns 1, otherwise it returns 0.
0146      This is used by ports which support an overlay manager where
0147      the overlay number is held in the top part of an address.  In
0148      some circumstances we want to include the overlay number in the
0149      address, (normally because there is a symbol associated with
0150      that address), but sometimes we want to mask out the overlay bits.  */
0151   asymbol * (*symbol_at_address_func)
0152     (bfd_vma addr, struct disassemble_info *dinfo);
0153 
0154   /* Function called to check if a SYMBOL is can be displayed to the user.
0155      This is used by some ports that want to hide special symbols when
0156      displaying debugging outout.  */
0157   bool (*symbol_is_valid)
0158     (asymbol *, struct disassemble_info *dinfo);
0159 
0160   /* These are for buffer_read_memory.  */
0161   bfd_byte *buffer;
0162   bfd_vma buffer_vma;
0163   size_t buffer_length;
0164 
0165   /* This variable may be set by the instruction decoder.  It suggests
0166       the number of bytes objdump should display on a single line.  If
0167       the instruction decoder sets this, it should always set it to
0168       the same value in order to get reasonable looking output.  */
0169   int bytes_per_line;
0170 
0171   /* The next two variables control the way objdump displays the raw data.  */
0172   /* For example, if bytes_per_line is 8 and bytes_per_chunk is 4, the */
0173   /* output will look like this:
0174      00:   00000000 00000000
0175      with the chunks displayed according to "display_endian". */
0176   int bytes_per_chunk;
0177   enum bfd_endian display_endian;
0178 
0179   /* Number of octets per incremented target address
0180      Normally one, but some DSPs have byte sizes of 16 or 32 bits.  */
0181   unsigned int octets_per_byte;
0182 
0183   /* The number of zeroes we want to see at the end of a section before we
0184      start skipping them.  */
0185   unsigned int skip_zeroes;
0186 
0187   /* The number of zeroes to skip at the end of a section.  If the number
0188      of zeroes at the end is between SKIP_ZEROES_AT_END and SKIP_ZEROES,
0189      they will be disassembled.  If there are fewer than
0190      SKIP_ZEROES_AT_END, they will be skipped.  This is a heuristic
0191      attempt to avoid disassembling zeroes inserted by section
0192      alignment.  */
0193   unsigned int skip_zeroes_at_end;
0194 
0195   /* Whether the disassembler always needs the relocations.  */
0196   bool disassembler_needs_relocs;
0197 
0198   /* Results from instruction decoders.  Not all decoders yet support
0199      this information.  This info is set each time an instruction is
0200      decoded, and is only valid for the last such instruction.
0201 
0202      To determine whether this decoder supports this information, set
0203      insn_info_valid to 0, decode an instruction, then check it.  */
0204 
0205   char insn_info_valid;     /* Branch info has been set. */
0206   char branch_delay_insns;  /* How many sequential insn's will run before
0207                    a branch takes effect.  (0 = normal) */
0208   char data_size;       /* Size of data reference in insn, in bytes */
0209   enum dis_insn_type insn_type; /* Type of instruction */
0210   bfd_vma target;       /* Target address of branch or dref, if known;
0211                    zero if unknown.  */
0212   bfd_vma target2;      /* Second target address for dref2 */
0213 
0214   /* Command line options specific to the target disassembler.  */
0215   const char *disassembler_options;
0216 
0217   /* If non-zero then try not disassemble beyond this address, even if
0218      there are values left in the buffer.  This address is the address
0219      of the nearest symbol forwards from the start of the disassembly,
0220      and it is assumed that it lies on the boundary between instructions.
0221      If an instruction spans this address then this is an error in the
0222      file being disassembled.  */
0223   bfd_vma stop_vma;
0224 
0225   /* The end range of the current range being disassembled.  This is required
0226      in order to notify the disassembler when it's currently handling a
0227      different range than it was before.  This prevent unsafe optimizations when
0228      disassembling such as the way mapping symbols are found on AArch64.  */
0229   bfd_vma stop_offset;
0230 
0231 } disassemble_info;
0232 
0233 /* This struct is used to pass information about valid disassembler
0234    option arguments from the target to the generic GDB functions
0235    that set and display them.  */
0236 
0237 typedef struct
0238 {
0239   /* Option argument name to use in descriptions.  */
0240   const char *name;
0241 
0242   /* Vector of acceptable option argument values, NULL-terminated.  */
0243   const char **values;
0244 } disasm_option_arg_t;
0245 
0246 /* This struct is used to pass information about valid disassembler
0247    options, their descriptions and arguments from the target to the
0248    generic GDB functions that set and display them.  Options are
0249    defined by tuples of vector entries at each index.  */
0250 
0251 typedef struct
0252 {
0253   /* Vector of option names, NULL-terminated.  */
0254   const char **name;
0255 
0256   /* Vector of option descriptions or NULL if none to be shown.  */
0257   const char **description;
0258 
0259   /* Vector of option argument information pointers or NULL if no
0260      option accepts an argument.  NULL entries denote individual
0261      options that accept no argument.  */
0262   const disasm_option_arg_t **arg;
0263 } disasm_options_t;
0264 
0265 /* This struct is used to pass information about valid disassembler
0266    options and arguments from the target to the generic GDB functions
0267    that set and display them.  */
0268 
0269 typedef struct
0270 {
0271   /* Valid disassembler options.  Individual options that support
0272      an argument will refer to entries in the ARGS vector.  */
0273   disasm_options_t options;
0274 
0275   /* Vector of acceptable option arguments, NULL-terminated.  This
0276      collects all possible option argument choices, some of which
0277      may be shared by different options from the OPTIONS member.  */
0278   disasm_option_arg_t *args;
0279 } disasm_options_and_args_t;
0280 
0281 /* Standard disassemblers.  Disassemble one instruction at the given
0282    target address.  Return number of octets processed.  */
0283 typedef int (*disassembler_ftype) (bfd_vma, disassemble_info *);
0284 
0285 /* Disassemblers used out side of opcodes library.  */
0286 extern int print_insn_m32c      (bfd_vma, disassemble_info *);
0287 extern int print_insn_mep       (bfd_vma, disassemble_info *);
0288 extern int print_insn_s12z      (bfd_vma, disassemble_info *);
0289 extern int print_insn_sh        (bfd_vma, disassemble_info *);
0290 extern int print_insn_sparc     (bfd_vma, disassemble_info *);
0291 extern int print_insn_rx        (bfd_vma, disassemble_info *);
0292 extern int print_insn_rl78      (bfd_vma, disassemble_info *);
0293 extern int print_insn_rl78_g10      (bfd_vma, disassemble_info *);
0294 extern int print_insn_rl78_g13      (bfd_vma, disassemble_info *);
0295 extern int print_insn_rl78_g14      (bfd_vma, disassemble_info *);
0296 
0297 extern disassembler_ftype arc_get_disassembler (bfd *);
0298 extern disassembler_ftype cris_get_disassembler (bfd *);
0299 
0300 extern void print_aarch64_disassembler_options (FILE *);
0301 extern void print_i386_disassembler_options (FILE *);
0302 extern void print_mips_disassembler_options (FILE *);
0303 extern void print_nfp_disassembler_options (FILE *);
0304 extern void print_ppc_disassembler_options (FILE *);
0305 extern void print_riscv_disassembler_options (FILE *);
0306 extern void print_arm_disassembler_options (FILE *);
0307 extern void print_arc_disassembler_options (FILE *);
0308 extern void print_s390_disassembler_options (FILE *);
0309 extern void print_wasm32_disassembler_options (FILE *);
0310 extern void print_loongarch_disassembler_options (FILE *);
0311 extern bool aarch64_symbol_is_valid (asymbol *, struct disassemble_info *);
0312 extern bool arm_symbol_is_valid (asymbol *, struct disassemble_info *);
0313 extern bool csky_symbol_is_valid (asymbol *, struct disassemble_info *);
0314 extern bool riscv_symbol_is_valid (asymbol *, struct disassemble_info *);
0315 extern void disassemble_init_powerpc (struct disassemble_info *);
0316 extern void disassemble_init_s390 (struct disassemble_info *);
0317 extern void disassemble_init_wasm32 (struct disassemble_info *);
0318 extern void disassemble_init_nds32 (struct disassemble_info *);
0319 extern const disasm_options_and_args_t *disassembler_options_arc (void);
0320 extern const disasm_options_and_args_t *disassembler_options_arm (void);
0321 extern const disasm_options_and_args_t *disassembler_options_mips (void);
0322 extern const disasm_options_and_args_t *disassembler_options_powerpc (void);
0323 extern const disasm_options_and_args_t *disassembler_options_riscv (void);
0324 extern const disasm_options_and_args_t *disassembler_options_s390 (void);
0325 
0326 /* Fetch the disassembler for a given architecture ARC, endianess (big
0327    endian if BIG is true), bfd_mach value MACH, and ABFD, if that support
0328    is available.  ABFD may be NULL.  */
0329 extern disassembler_ftype disassembler (enum bfd_architecture arc,
0330                     bool big, unsigned long mach,
0331                     bfd *abfd);
0332 
0333 /* Amend the disassemble_info structure as necessary for the target architecture.
0334    Should only be called after initialising the info->arch field.  */
0335 extern void disassemble_init_for_target (struct disassemble_info *);
0336 
0337 /* Tidy any memory allocated by targets, such as info->private_data.  */
0338 extern void disassemble_free_target (struct disassemble_info *);
0339 
0340 /* Document any target specific options available from the disassembler.  */
0341 extern void disassembler_usage (FILE *);
0342 
0343 /* Remove whitespace and consecutive commas.  */
0344 extern char *remove_whitespace_and_extra_commas (char *);
0345 
0346 /* Like STRCMP, but treat ',' the same as '\0' so that we match
0347    strings like "foobar" against "foobar,xxyyzz,...".  */
0348 extern int disassembler_options_cmp (const char *, const char *);
0349 
0350 /* A helper function for FOR_EACH_DISASSEMBLER_OPTION.  */
0351 static inline const char *
0352 next_disassembler_option (const char *options)
0353 {
0354   const char *opt = strchr (options, ',');
0355   if (opt != NULL)
0356     opt++;
0357   return opt;
0358 }
0359 
0360 /* A macro for iterating over each comma separated option in OPTIONS.  */
0361 #define FOR_EACH_DISASSEMBLER_OPTION(OPT, OPTIONS) \
0362   for ((OPT) = (OPTIONS); \
0363        (OPT) != NULL; \
0364        (OPT) = next_disassembler_option (OPT))
0365 
0366 
0367 /* This block of definitions is for particular callers who read instructions
0368    into a buffer before calling the instruction decoder.  */
0369 
0370 /* Here is a function which callers may wish to use for read_memory_func.
0371    It gets bytes from a buffer.  */
0372 extern int buffer_read_memory
0373   (bfd_vma, bfd_byte *, unsigned int, struct disassemble_info *);
0374 
0375 /* This function goes with buffer_read_memory.
0376    It prints a message using info->fprintf_func and info->stream.  */
0377 extern void perror_memory (int, bfd_vma, struct disassemble_info *);
0378 
0379 
0380 /* Just print the address in hex.  This is included for completeness even
0381    though both GDB and objdump provide their own (to print symbolic
0382    addresses).  */
0383 extern void generic_print_address
0384   (bfd_vma, struct disassemble_info *);
0385 
0386 /* Always NULL.  */
0387 extern asymbol *generic_symbol_at_address
0388   (bfd_vma, struct disassemble_info *);
0389 
0390 /* Always true.  */
0391 extern bool generic_symbol_is_valid
0392   (asymbol *, struct disassemble_info *);
0393 
0394 /* Method to initialize a disassemble_info struct.  This should be
0395    called by all applications creating such a struct.  */
0396 extern void init_disassemble_info (struct disassemble_info *dinfo, void *stream,
0397                    fprintf_ftype fprintf_func);
0398 
0399 /* For compatibility with existing code.  */
0400 #define INIT_DISASSEMBLE_INFO(INFO, STREAM, FPRINTF_FUNC) \
0401   init_disassemble_info (&(INFO), (STREAM), (fprintf_ftype) (FPRINTF_FUNC))
0402 
0403 #ifdef __cplusplus
0404 }
0405 #endif
0406 
0407 #endif /* ! defined (DIS_ASM_H) */