Blame libasm/libasmP.h

Packit 032894
/* Internal definitions for libasm.
Packit 032894
   Copyright (C) 2002, 2004, 2005, 2016 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifndef _LIBASMP_H
Packit 032894
#define _LIBASMP_H 1
Packit 032894
Packit 032894
#include <stdio.h>
Packit 032894
Packit 032894
#include <libasm.h>
Packit 032894
Packit 032894
#include "libdwelf.h"
Packit 032894
Packit 032894
/* gettext helper macros.  */
Packit 032894
#define _(Str) dgettext ("elfutils", Str)
Packit 032894
Packit 032894
Packit 032894
/* Known error codes.  */
Packit 032894
enum
Packit 032894
  {
Packit 032894
    ASM_E_NOERROR,
Packit 032894
    ASM_E_NOMEM,		/* No more memory.  */
Packit 032894
    ASM_E_CANNOT_CREATE,	/* Output file cannot be created.  */
Packit 032894
    ASM_E_INVALID,		/* Invalid parameters.  */
Packit 032894
    ASM_E_CANNOT_CHMOD,		/* Cannot change mode of output file.  */
Packit 032894
    ASM_E_CANNOT_RENAME,	/* Cannot rename output file.  */
Packit 032894
    ASM_E_DUPLSYM,		/* Duplicate symbol definition.  */
Packit 032894
    ASM_E_LIBELF,		/* Refer to error in libelf.  */
Packit 032894
    ASM_E_TYPE,			/* Invalid section type for operation.  */
Packit 032894
    ASM_E_IOERROR,		/* Error during output of data.  */
Packit 032894
    ASM_E_ENOSUP,		/* No backend support.  */
Packit 032894
    ASM_E_NUM			/* Keep this entry as the last.  */
Packit 032894
  };
Packit 032894
Packit 032894
Packit 032894
/* Special sections.  */
Packit 032894
#define ASM_ABS_SCN ((Elf_Scn *) 1)
Packit 032894
#define ASM_COM_SCN ((Elf_Scn *) 2)
Packit 032894
Packit 032894
Packit 032894
/* And the hash table for symbols.  */
Packit 032894
#include <symbolhash.h>
Packit 032894
Packit 032894
Packit 032894
/* Descriptor for a section.  */
Packit 032894
struct AsmScn
Packit 032894
{
Packit 032894
  /* The underlying assembler context.  */
Packit 032894
  AsmCtx_t *ctx;
Packit 032894
Packit 032894
  /* Subsection ID.  */
Packit 032894
  unsigned int subsection_id;
Packit 032894
Packit 032894
  /* Section type.  */
Packit 032894
  GElf_Word type;
Packit 032894
Packit 032894
  union
Packit 032894
  {
Packit 032894
    /* Data only stored in the record for subsection zero.  */
Packit 032894
    struct
Packit 032894
    {
Packit 032894
      /* The ELF section.  */
Packit 032894
      Elf_Scn *scn;
Packit 032894
Packit 032894
      /* Entry in the section header string table.  */
Packit 032894
      Dwelf_Strent *strent;
Packit 032894
Packit 032894
      /* Next member of group.  */
Packit 032894
      struct AsmScn *next_in_group;
Packit 032894
    } main;
Packit 032894
Packit 032894
    /* Pointer to the record for subsection zero.  */
Packit 032894
    AsmScn_t *up;
Packit 032894
  } data;
Packit 032894
Packit 032894
  /* Current offset in the (sub)section.  */
Packit 032894
  GElf_Off offset;
Packit 032894
  /* Maximum alignment of the section so far.  */
Packit 032894
  GElf_Word max_align;
Packit 032894
Packit 032894
  /* Section content.  */
Packit 032894
  struct AsmData
Packit 032894
  {
Packit 032894
    /* Currently used number of bytes in the block.  */
Packit 032894
    size_t len;
Packit 032894
Packit 032894
    /* Number of bytes allocated.  */
Packit 032894
    size_t maxlen;
Packit 032894
Packit 032894
    /* Pointer to the next block.  */
Packit 032894
    struct AsmData *next;
Packit 032894
Packit 032894
    /* The actual data.  */
Packit 032894
    char data[flexarr_size];
Packit 032894
  } *content;
Packit 032894
Packit 032894
  /* Fill pattern.  */
Packit 032894
  struct FillPattern
Packit 032894
  {
Packit 032894
    size_t len;
Packit 032894
    char bytes[flexarr_size];
Packit 032894
  } *pattern;
Packit 032894
Packit 032894
  /* Next subsection.  */
Packit 032894
  AsmScn_t *subnext;
Packit 032894
Packit 032894
  /* List of all allocated sections.  */
Packit 032894
  AsmScn_t *allnext;
Packit 032894
Packit 032894
  /* Name of the section.  */
Packit 032894
  char name[flexarr_size];
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
/* Descriptor used for the assembling session.  */
Packit 032894
struct AsmCtx
Packit 032894
{
Packit 032894
  /* File descriptor of the temporary file.  */
Packit 032894
  int fd;
Packit 032894
Packit 032894
  /* True if text output is wanted.  */
Packit 032894
  bool textp;
Packit 032894
Packit 032894
  /* Output file handle.  */
Packit 032894
  union
Packit 032894
  {
Packit 032894
    /* ELF descriptor of the temporary file.  */
Packit 032894
    Elf *elf;
Packit 032894
    /* I/O stream for text output.  */
Packit 032894
    FILE *file;
Packit 032894
  } out;
Packit 032894
Packit 032894
Packit 032894
  /* List with defined sections.  */
Packit 032894
  AsmScn_t *section_list;
Packit 032894
  /* Section header string table.  */
Packit 032894
  Dwelf_Strtab *section_strtab;
Packit 032894
Packit 032894
  /* Table with defined symbols.  */
Packit 032894
  asm_symbol_tab symbol_tab;
Packit 032894
  /* Number of symbols in the table.  */
Packit 032894
  unsigned int nsymbol_tab;
Packit 032894
  /* Symbol string table.  */
Packit 032894
  Dwelf_Strtab *symbol_strtab;
Packit 032894
Packit 032894
  /* List of section groups.  */
Packit 032894
  struct AsmScnGrp *groups;
Packit 032894
  /* Number of section groups.  */
Packit 032894
  size_t ngroups;
Packit 032894
Packit 032894
  /* Current required alignment for common symbols.  */
Packit 032894
  GElf_Word common_align;
Packit 032894
Packit 032894
  /* Lock to handle multithreaded programs.  */
Packit 032894
  rwlock_define (,lock);
Packit 032894
Packit 032894
  /* Counter for temporary symbols.  */
Packit 032894
  unsigned int tempsym_count;
Packit 032894
Packit 032894
  /* Name of the output file.  */
Packit 032894
  char *fname;
Packit 032894
  /* The name of the temporary file.  */
Packit 032894
  char tmp_fname[flexarr_size];
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
/* Descriptor for a symbol.  */
Packit 032894
struct AsmSym
Packit 032894
{
Packit 032894
  /* Reference to the section which contains the symbol.  */
Packit 032894
  AsmScn_t *scn;
Packit 032894
Packit 032894
  /* Type of the symbol.  */
Packit 032894
  int8_t type;
Packit 032894
  /* Binding of the symbol.  */
Packit 032894
  int8_t binding;
Packit 032894
Packit 032894
  /* Size of the symbol.  */
Packit 032894
  GElf_Xword size;
Packit 032894
Packit 032894
  /* Offset in the section.  */
Packit 032894
  GElf_Off offset;
Packit 032894
Packit 032894
  /* Symbol table index of the symbol in the symbol table.  */
Packit 032894
  size_t symidx;
Packit 032894
Packit 032894
  /* Reference to name of the symbol.  */
Packit 032894
  Dwelf_Strent *strent;
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
/* Descriptor for section group.  */
Packit 032894
struct AsmScnGrp
Packit 032894
{
Packit 032894
  /* Entry in the section header string table.  */
Packit 032894
  Dwelf_Strent *strent;
Packit 032894
Packit 032894
  /* The ELF section.  */
Packit 032894
  Elf_Scn *scn;
Packit 032894
Packit 032894
  /* The signature.  */
Packit 032894
  struct AsmSym *signature;
Packit 032894
Packit 032894
  /* First member.  */
Packit 032894
  struct AsmScn *members;
Packit 032894
  /* Number of members.  */
Packit 032894
  size_t nmembers;
Packit 032894
Packit 032894
  /* Flags.  */
Packit 032894
  Elf32_Word flags;
Packit 032894
Packit 032894
  /* Next group.  */
Packit 032894
  struct AsmScnGrp *next;
Packit 032894
Packit 032894
  /* Name of the section group.  */
Packit 032894
  char name[flexarr_size];
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
/* Descriptor for disassembler.   */
Packit 032894
struct DisasmCtx
Packit 032894
{
Packit 032894
  /* Handle for the backend library with the disassembler routine.  */
Packit 032894
  Ebl *ebl;
Packit 032894
Packit 032894
  /* ELF file containing all the data passed to the function.  This
Packit 032894
     allows to look up symbols.  */
Packit 032894
  Elf *elf;
Packit 032894
Packit 032894
  /* Callback function to determine symbol names.  */
Packit 032894
  DisasmGetSymCB_t symcb;
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
/* The default fill pattern: one zero byte.  */
Packit 032894
extern const struct FillPattern *__libasm_default_pattern
Packit 032894
     attribute_hidden;
Packit 032894
Packit 032894
Packit 032894
/* Ensure there are at least LEN bytes available in the output buffer
Packit 032894
   for ASMSCN.  */
Packit 032894
extern int __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
Packit 032894
     internal_function;
Packit 032894
Packit 032894
/* Free all resources associated with the assembler context.  */
Packit 032894
extern void __libasm_finictx (AsmCtx_t *ctx) internal_function;
Packit 032894
Packit 032894
/* Set error code.  */
Packit 032894
extern void __libasm_seterrno (int err) internal_function;
Packit 032894
Packit 032894
/* Return handle for the named section.  If it was not used before
Packit 032894
   create it.  */
Packit 032894
extern AsmScn_t *__asm_newscn_internal (AsmCtx_t *ctx, const char *scnname,
Packit 032894
					GElf_Word type, GElf_Xword flags)
Packit 032894
     attribute_hidden;
Packit 032894
Packit 032894
Packit 032894
/* Internal aliases of the asm_addintXX functions.  */
Packit 032894
extern int __asm_addint8_internal (AsmScn_t *asmscn, int8_t num)
Packit 032894
     attribute_hidden;
Packit 032894
extern int __asm_addint16_internal (AsmScn_t *asmscn, int16_t num)
Packit 032894
     attribute_hidden;
Packit 032894
extern int __asm_addint32_internal (AsmScn_t *asmscn, int32_t num)
Packit 032894
     attribute_hidden;
Packit 032894
extern int __asm_addint64_internal (AsmScn_t *asmscn, int64_t num)
Packit 032894
     attribute_hidden;
Packit 032894
Packit 032894
Packit 032894
/* Produce disassembly output for given memory and output it using the
Packit 032894
   given callback functions.  */
Packit 032894
extern int __disasm_cb_internal (DisasmCtx_t *ctx, const uint8_t **startp,
Packit 032894
				 const uint8_t *end, GElf_Addr addr,
Packit 032894
				 const char *fmt, DisasmOutputCB_t outcb,
Packit 032894
				 void *outcbarp, void *symcbarg)
Packit 032894
     attribute_hidden;
Packit 032894
Packit 032894
Packit 032894
/* Test whether given symbol is an internal symbol and if yes, whether
Packit 032894
   we should nevertheless emit it in the symbol table.  */
Packit 032894
// XXX The second part should probably be controlled by an option which
Packit 032894
// isn't implemented yet
Packit 032894
// XXX Also, the format will change with the backend.
Packit 032894
#define asm_emit_symbol_p(name) (strncmp (name, ".L", 2) != 0)
Packit 032894
Packit 032894
#endif	/* libasmP.h */