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