Home | History | Annotate | Download | only in libasm
      1 /* Internal definitions for libasm.
      2    Copyright (C) 2002, 2004, 2005 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 _LIBASMP_H
     27 #define _LIBASMP_H 1
     28 
     29 #include <stdio.h>
     30 
     31 #include <libasm.h>
     32 
     33 /* gettext helper macros.  */
     34 #define _(Str) dgettext ("elfutils", Str)
     35 
     36 
     37 /* Known error codes.  */
     38 enum
     39   {
     40     ASM_E_NOERROR,
     41     ASM_E_NOMEM,		/* No more memory.  */
     42     ASM_E_CANNOT_CREATE,	/* Output file cannot be created.  */
     43     ASM_E_INVALID,		/* Invalid parameters.  */
     44     ASM_E_CANNOT_CHMOD,		/* Cannot change mode of output file.  */
     45     ASM_E_CANNOT_RENAME,	/* Cannot rename output file.  */
     46     ASM_E_DUPLSYM,		/* Duplicate symbol definition.  */
     47     ASM_E_LIBELF,		/* Refer to error in libelf.  */
     48     ASM_E_TYPE,			/* Invalid section type for operation.  */
     49     ASM_E_IOERROR,		/* Error during output of data.  */
     50     ASM_E_ENOSUP,		/* No backend support.  */
     51     ASM_E_NUM			/* Keep this entry as the last.  */
     52   };
     53 
     54 
     55 /* Special sections.  */
     56 #define ASM_ABS_SCN ((Elf_Scn *) 1)
     57 #define ASM_COM_SCN ((Elf_Scn *) 2)
     58 
     59 
     60 /* And the hash table for symbols.  */
     61 #include <symbolhash.h>
     62 
     63 
     64 /* Descriptor for a section.  */
     65 struct AsmScn
     66 {
     67   /* The underlying assembler context.  */
     68   AsmCtx_t *ctx;
     69 
     70   /* Subsection ID.  */
     71   unsigned int subsection_id;
     72 
     73   /* Section type.  */
     74   GElf_Word type;
     75 
     76   union
     77   {
     78     /* Data only stored in the record for subsection zero.  */
     79     struct
     80     {
     81       /* The ELF section.  */
     82       Elf_Scn *scn;
     83 
     84       /* Entry in the section header string table.  */
     85       struct Ebl_Strent *strent;
     86 
     87       /* Next member of group.  */
     88       struct AsmScn *next_in_group;
     89     } main;
     90 
     91     /* Pointer to the record for subsection zero.  */
     92     AsmScn_t *up;
     93   } data;
     94 
     95   /* Current offset in the (sub)section.  */
     96   GElf_Off offset;
     97   /* Maximum alignment of the section so far.  */
     98   GElf_Word max_align;
     99 
    100   /* Section content.  */
    101   struct AsmData
    102   {
    103     /* Currently used number of bytes in the block.  */
    104     size_t len;
    105 
    106     /* Number of bytes allocated.  */
    107     size_t maxlen;
    108 
    109     /* Pointer to the next block.  */
    110     struct AsmData *next;
    111 
    112     /* The actual data.  */
    113     char data[flexarr_size];
    114   } *content;
    115 
    116   /* Fill pattern.  */
    117   struct FillPattern
    118   {
    119     size_t len;
    120     char bytes[flexarr_size];
    121   } *pattern;
    122 
    123   /* Next subsection.  */
    124   AsmScn_t *subnext;
    125 
    126   /* List of all allocated sections.  */
    127   AsmScn_t *allnext;
    128 
    129   /* Name of the section.  */
    130   char name[flexarr_size];
    131 };
    132 
    133 
    134 /* Descriptor used for the assembling session.  */
    135 struct AsmCtx
    136 {
    137   /* File descriptor of the temporary file.  */
    138   int fd;
    139 
    140   /* True if text output is wanted.  */
    141   bool textp;
    142 
    143   /* Output file handle.  */
    144   union
    145   {
    146     /* ELF descriptor of the temporary file.  */
    147     Elf *elf;
    148     /* I/O stream for text output.  */
    149     FILE *file;
    150   } out;
    151 
    152 
    153   /* List with defined sections.  */
    154   AsmScn_t *section_list;
    155   /* Section header string table.  */
    156   struct Ebl_Strtab *section_strtab;
    157 
    158   /* Table with defined symbols.  */
    159   asm_symbol_tab symbol_tab;
    160   /* Number of symbols in the table.  */
    161   unsigned int nsymbol_tab;
    162   /* Symbol string table.  */
    163   struct Ebl_Strtab *symbol_strtab;
    164 
    165   /* List of section groups.  */
    166   struct AsmScnGrp *groups;
    167   /* Number of section groups.  */
    168   size_t ngroups;
    169 
    170   /* Current required alignment for common symbols.  */
    171   GElf_Word common_align;
    172 
    173   /* Lock to handle multithreaded programs.  */
    174   rwlock_define (,lock);
    175 
    176   /* Counter for temporary symbols.  */
    177   unsigned int tempsym_count;
    178 
    179   /* Name of the output file.  */
    180   char *fname;
    181   /* The name of the temporary file.  */
    182   char tmp_fname[flexarr_size];
    183 };
    184 
    185 
    186 /* Descriptor for a symbol.  */
    187 struct AsmSym
    188 {
    189   /* Reference to the section which contains the symbol.  */
    190   AsmScn_t *scn;
    191 
    192   /* Type of the symbol.  */
    193   int8_t type;
    194   /* Binding of the symbol.  */
    195   int8_t binding;
    196 
    197   /* Size of the symbol.  */
    198   GElf_Xword size;
    199 
    200   /* Offset in the section.  */
    201   GElf_Off offset;
    202 
    203   /* Symbol table index of the symbol in the symbol table.  */
    204   size_t symidx;
    205 
    206   /* Reference to name of the symbol.  */
    207   struct Ebl_Strent *strent;
    208 };
    209 
    210 
    211 /* Descriptor for section group.  */
    212 struct AsmScnGrp
    213 {
    214   /* Entry in the section header string table.  */
    215   struct Ebl_Strent *strent;
    216 
    217   /* The ELF section.  */
    218   Elf_Scn *scn;
    219 
    220   /* The signature.  */
    221   struct AsmSym *signature;
    222 
    223   /* First member.  */
    224   struct AsmScn *members;
    225   /* Number of members.  */
    226   size_t nmembers;
    227 
    228   /* Flags.  */
    229   Elf32_Word flags;
    230 
    231   /* Next group.  */
    232   struct AsmScnGrp *next;
    233 
    234   /* Name of the section group.  */
    235   char name[flexarr_size];
    236 };
    237 
    238 
    239 /* Descriptor for disassembler.   */
    240 struct DisasmCtx
    241 {
    242   /* Handle for the backend library with the disassembler routine.  */
    243   Ebl *ebl;
    244 
    245   /* ELF file containing all the data passed to the function.  This
    246      allows to look up symbols.  */
    247   Elf *elf;
    248 
    249   /* Callback function to determine symbol names.  */
    250   DisasmGetSymCB_t symcb;
    251 };
    252 
    253 
    254 /* The default fill pattern: one zero byte.  */
    255 extern const struct FillPattern *__libasm_default_pattern
    256      attribute_hidden;
    257 
    258 
    259 /* Ensure there are at least LEN bytes available in the output buffer
    260    for ASMSCN.  */
    261 extern int __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
    262      internal_function;
    263 
    264 /* Free all resources associated with the assembler context.  */
    265 extern void __libasm_finictx (AsmCtx_t *ctx) internal_function;
    266 
    267 /* Set error code.  */
    268 extern void __libasm_seterrno (int err) internal_function;
    269 
    270 /* Return handle for the named section.  If it was not used before
    271    create it.  */
    272 extern AsmScn_t *__asm_newscn_internal (AsmCtx_t *ctx, const char *scnname,
    273 					GElf_Word type, GElf_Xword flags)
    274      attribute_hidden;
    275 
    276 
    277 /* Internal aliases of the asm_addintXX functions.  */
    278 extern int __asm_addint8_internal (AsmScn_t *asmscn, int8_t num)
    279      attribute_hidden;
    280 extern int __asm_addint16_internal (AsmScn_t *asmscn, int16_t num)
    281      attribute_hidden;
    282 extern int __asm_addint32_internal (AsmScn_t *asmscn, int32_t num)
    283      attribute_hidden;
    284 extern int __asm_addint64_internal (AsmScn_t *asmscn, int64_t num)
    285      attribute_hidden;
    286 
    287 
    288 /* Produce disassembly output for given memory and output it using the
    289    given callback functions.  */
    290 extern int __disasm_cb_internal (DisasmCtx_t *ctx, const uint8_t **startp,
    291 				 const uint8_t *end, GElf_Addr addr,
    292 				 const char *fmt, DisasmOutputCB_t outcb,
    293 				 void *outcbarp, void *symcbarg)
    294      attribute_hidden;
    295 
    296 
    297 /* Test whether given symbol is an internal symbol and if yes, whether
    298    we should nevertheless emit it in the symbol table.  */
    299 // XXX The second part should probably be controlled by an option which
    300 // isn't implemented yet
    301 // XXX Also, the format will change with the backend.
    302 #define asm_emit_symbol_p(name) (strncmp (name, ".L", 2) != 0)
    303 
    304 #endif	/* libasmP.h */
    305