Home | History | Annotate | Download | only in libasm
      1 /* Interface for libasm.
      2    Copyright (C) 2002, 2005, 2008 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    Red Hat elfutils is an included package of the Open Invention Network.
     19    An included package of the Open Invention Network is a package for which
     20    Open Invention Network licensees cross-license their patents.  No patent
     21    license is granted, either expressly or impliedly, by designation as an
     22    included package.  Should you wish to participate in the Open Invention
     23    Network licensing program, please visit www.openinventionnetwork.com
     24    <http://www.openinventionnetwork.com>.  */
     25 
     26 #ifndef _LIBASM_H
     27 #define _LIBASM_H 1
     28 
     29 #include <stdbool.h>
     30 #include <stdint.h>
     31 
     32 #include <libebl.h>
     33 
     34 
     35 /* Opaque type for the assembler context descriptor.  */
     36 typedef struct AsmCtx AsmCtx_t;
     37 
     38 /* Opaque type for a section.  */
     39 typedef struct AsmScn AsmScn_t;
     40 
     41 /* Opaque type for a section group.  */
     42 typedef struct AsmScnGrp AsmScnGrp_t;
     43 
     44 /* Opaque type for a symbol.  */
     45 typedef struct AsmSym AsmSym_t;
     46 
     47 
     48 /* Opaque type for the disassembler context descriptor.  */
     49 typedef struct DisasmCtx DisasmCtx_t;
     50 
     51 /* Type used for callback functions to retrieve symbol name.  The
     52    symbol reference is in the section designated by the second parameter
     53    at an offset described by the first parameter.  The value is the
     54    third parameter.  */
     55 typedef int (*DisasmGetSymCB_t) (GElf_Addr, Elf32_Word, GElf_Addr, char **,
     56 				 size_t *, void *);
     57 
     58 /* Output function callback.  */
     59 typedef int (*DisasmOutputCB_t) (char *, size_t, void *);
     60 
     61 
     62 #ifdef __cplusplus
     63 extern "C" {
     64 #endif
     65 
     66 /* Create output file and return descriptor for assembler context.  If
     67    TEXTP is true the output is an assembler format text file.
     68    Otherwise an object file is created.  The MACHINE parameter
     69    corresponds to an EM_ constant from <elf.h>, KLASS specifies the
     70    class (32- or 64-bit), and DATA specifies the byte order (little or
     71    big endian).  */
     72 extern AsmCtx_t *asm_begin (const char *fname, Ebl *ebl, bool textp);
     73 
     74 /* Abort the operation on the assembler context and free all resources.  */
     75 extern int asm_abort (AsmCtx_t *ctx);
     76 
     77 /* Finalize output file and free all resources.  */
     78 extern int asm_end (AsmCtx_t *ctx);
     79 
     80 
     81 /* Return handle for the named section.  If it was not used before
     82    create it.  */
     83 extern AsmScn_t *asm_newscn (AsmCtx_t *ctx, const char *scnname,
     84 			     GElf_Word type, GElf_Xword flags);
     85 
     86 
     87 /* Similar to 'asm_newscn', but make it part of section group GRP.  */
     88 extern AsmScn_t *asm_newscn_ingrp (AsmCtx_t *ctx, const char *scnname,
     89 				   GElf_Word type, GElf_Xword flags,
     90 				   AsmScnGrp_t *grp);
     91 
     92 /* Create new subsection NR in the given section.  */
     93 extern AsmScn_t *asm_newsubscn (AsmScn_t *asmscn, unsigned int nr);
     94 
     95 
     96 /* Return handle for new section group.  The signature symbol can be
     97    set later.  */
     98 extern AsmScnGrp_t *asm_newscngrp (AsmCtx_t *ctx, const char *grpname,
     99 				   AsmSym_t *signature, Elf32_Word flags);
    100 
    101 /* Set or overwrite signature symbol for group.  */
    102 extern int asm_scngrp_newsignature (AsmScnGrp_t *grp, AsmSym_t *signature);
    103 
    104 
    105 /* Add zero terminated string STR of size LEN to (sub)section ASMSCN.  */
    106 extern int asm_addstrz (AsmScn_t *asmscn, const char *str, size_t len);
    107 
    108 /* Add 8-bit signed integer NUM to (sub)section ASMSCN.  */
    109 extern int asm_addint8 (AsmScn_t *asmscn, int8_t num);
    110 
    111 /* Add 8-bit unsigned integer NUM to (sub)section ASMSCN.  */
    112 extern int asm_adduint8 (AsmScn_t *asmscn, uint8_t num);
    113 
    114 /* Add 16-bit signed integer NUM to (sub)section ASMSCN.  */
    115 extern int asm_addint16 (AsmScn_t *asmscn, int16_t num);
    116 
    117 /* Add 16-bit unsigned integer NUM to (sub)section ASMSCN.  */
    118 extern int asm_adduint16 (AsmScn_t *asmscn, uint16_t num);
    119 
    120 /* Add 32-bit signed integer NUM to (sub)section ASMSCN.  */
    121 extern int asm_addint32 (AsmScn_t *asmscn, int32_t num);
    122 
    123 /* Add 32-bit unsigned integer NUM to (sub)section ASMSCN.  */
    124 extern int asm_adduint32 (AsmScn_t *asmscn, uint32_t num);
    125 
    126 /* Add 64-bit signed integer NUM to (sub)section ASMSCN.  */
    127 extern int asm_addint64 (AsmScn_t *asmscn, int64_t num);
    128 
    129 /* Add 64-bit unsigned integer NUM to (sub)section ASMSCN.  */
    130 extern int asm_adduint64 (AsmScn_t *asmscn, uint64_t num);
    131 
    132 
    133 /* Add signed little endian base 128 integer NUM to (sub)section ASMSCN.  */
    134 extern int asm_addsleb128 (AsmScn_t *asmscn, int32_t num);
    135 
    136 /* Add unsigned little endian base 128 integer NUM to (sub)section ASMSCN.  */
    137 extern int asm_adduleb128 (AsmScn_t *asmscn, uint32_t num);
    138 
    139 
    140 /* Define new symbol NAME for current position in given section ASMSCN.  */
    141 extern AsmSym_t *asm_newsym (AsmScn_t *asmscn, const char *name,
    142 			     GElf_Xword size, int type, int binding);
    143 
    144 
    145 /* Define new common symbol NAME with given SIZE and alignment.  */
    146 extern AsmSym_t *asm_newcomsym (AsmCtx_t *ctx, const char *name,
    147 				GElf_Xword size, GElf_Addr align);
    148 
    149 /* Define new common symbol NAME with given SIZE, VALUE, TYPE, and BINDING.  */
    150 extern AsmSym_t *asm_newabssym (AsmCtx_t *ctx, const char *name,
    151 				GElf_Xword size, GElf_Addr value,
    152 				int type, int binding);
    153 
    154 
    155 /* Align (sub)section offset according to VALUE.  */
    156 extern int asm_align (AsmScn_t *asmscn, GElf_Word value);
    157 
    158 /* Set the byte pattern used to fill gaps created by alignment.  */
    159 extern int asm_fill (AsmScn_t *asmscn, void *bytes, size_t len);
    160 
    161 
    162 /* Return ELF descriptor created for the output file of the given context.  */
    163 extern Elf *asm_getelf (AsmCtx_t *ctx);
    164 
    165 
    166 /* Return error code of last failing function call.  This value is kept
    167    separately for each thread.  */
    168 extern int asm_errno (void);
    169 
    170 /* Return error string for ERROR.  If ERROR is zero, return error string
    171    for most recent error or NULL is none occurred.  If ERROR is -1 the
    172    behaviour is similar to the last case except that not NULL but a legal
    173    string is returned.  */
    174 extern const char *asm_errmsg (int __error);
    175 
    176 
    177 /* Create context descriptor for disassembler.  */
    178 extern DisasmCtx_t *disasm_begin (Ebl *ebl, Elf *elf, DisasmGetSymCB_t symcb);
    179 
    180 /* Release descriptor for disassembler.  */
    181 extern int disasm_end (DisasmCtx_t *ctx);
    182 
    183 /* Produce of disassembly output for given memory, store text in
    184    provided buffer.  */
    185 extern int disasm_str (DisasmCtx_t *ctx, const uint8_t **startp,
    186 		       const uint8_t *end, GElf_Addr addr, const char *fmt,
    187 		       char **bufp, size_t len, void *symcbarg);
    188 
    189 /* Produce disassembly output for given memory and output it using the
    190    given callback functions.  */
    191 extern int disasm_cb (DisasmCtx_t *ctx, const uint8_t **startp,
    192 		      const uint8_t *end, GElf_Addr addr, const char *fmt,
    193 		      DisasmOutputCB_t outcb, void *outcbarg, void *symcbarg);
    194 
    195 #ifdef __cplusplus
    196 }
    197 #endif
    198 
    199 #endif	/* libasm.h */
    200