Home | History | Annotate | Download | only in libasm
      1 /* Internal definitions for libasm.
      2    Copyright (C) 2002, 2004 Red Hat, Inc.
      3 
      4    This program is Open Source software; you can redistribute it and/or
      5    modify it under the terms of the Open Software License version 1.0 as
      6    published by the Open Source Initiative.
      7 
      8    You should have received a copy of the Open Software License along
      9    with this program; if not, you may obtain a copy of the Open Software
     10    License version 1.0 from http://www.opensource.org/licenses/osl.php or
     11    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
     12    3001 King Ranch Road, Ukiah, CA 95482.   */
     13 
     14 #ifndef _LIBASMP_H
     15 #define _LIBASMP_H 1
     16 
     17 #include <stdio.h>
     18 
     19 #include <libasm.h>
     20 #include <libebl.h>
     21 
     22 /* gettext helper macros.  */
     23 #define _(Str) dgettext ("elfutils", Str)
     24 
     25 
     26 /* Known error codes.  */
     27 enum
     28   {
     29     ASM_E_NOERROR,
     30     ASM_E_NOMEM,		/* No more memory.  */
     31     ASM_E_CANNOT_CREATE,	/* Output file cannot be created.  */
     32     ASM_E_INVALID,		/* Invalid parameters.  */
     33     ASM_E_CANNOT_CHMOD,		/* Cannot change mode of output file.  */
     34     ASM_E_CANNOT_RENAME,	/* Cannot rename output file.  */
     35     ASM_E_DUPLSYM,		/* Duplicate symbol definition.  */
     36     ASM_E_LIBELF,		/* Refer to error in libelf.  */
     37     ASM_E_TYPE,			/* Invalid section type for operation.  */
     38     ASM_E_NUM			/* Keep this entry as the last.  */
     39   };
     40 
     41 
     42 /* Special sections.  */
     43 #define ASM_ABS_SCN ((Elf_Scn *) 1)
     44 #define ASM_COM_SCN ((Elf_Scn *) 2)
     45 
     46 
     47 /* And the hash table for symbols.  */
     48 #include <symbolhash.h>
     49 
     50 
     51 /* Descriptor for a section.  */
     52 struct AsmScn
     53 {
     54   /* The underlying assembler context.  */
     55   AsmCtx_t *ctx;
     56 
     57   /* Subsection ID.  */
     58   unsigned int subsection_id;
     59 
     60   /* Section type.  */
     61   GElf_Word type;
     62 
     63   union
     64   {
     65     /* Data only stored in the record for subsection zero.  */
     66     struct
     67     {
     68       /* The ELF section.  */
     69       Elf_Scn *scn;
     70 
     71       /* Entry in the section header string table.  */
     72       struct Ebl_Strent *strent;
     73 
     74       /* Next member of group.  */
     75       struct AsmScn *next_in_group;
     76     } main;
     77 
     78     /* Pointer to the record for subsection zero.  */
     79     AsmScn_t *up;
     80   } data;
     81 
     82   /* Current offset in the (sub)section.  */
     83   GElf_Off offset;
     84   /* Maximum alignment of the section so far.  */
     85   GElf_Word max_align;
     86 
     87   /* Section content.  */
     88   struct AsmData
     89   {
     90     /* Currently used number of bytes in the block.  */
     91     size_t len;
     92 
     93     /* Number of bytes allocated.  */
     94     size_t maxlen;
     95 
     96     /* Pointer to the next block.  */
     97     struct AsmData *next;
     98 
     99     /* The actual data.  */
    100     char data[flexarr_size];
    101   } *content;
    102 
    103   /* Fill pattern.  */
    104   struct FillPattern
    105   {
    106     size_t len;
    107     char bytes[flexarr_size];
    108   } *pattern;
    109 
    110   /* Next subsection.  */
    111   AsmScn_t *subnext;
    112 
    113   /* List of all allocated sections.  */
    114   AsmScn_t *allnext;
    115 
    116   /* Name of the section.  */
    117   char name[flexarr_size];
    118 };
    119 
    120 
    121 /* Descriptor used for the assembling session.  */
    122 struct AsmCtx
    123 {
    124   /* File descriptor of the temporary file.  */
    125   int fd;
    126 
    127   /* True if text output is wanted.  */
    128   bool textp;
    129 
    130   /* Output file handle.  */
    131   union
    132   {
    133     /* ELF descriptor of the temporary file.  */
    134     Elf *elf;
    135     /* I/O stream for text output.  */
    136     FILE *file;
    137   } out;
    138 
    139 
    140   /* List with defined sections.  */
    141   AsmScn_t *section_list;
    142   /* Section header string table.  */
    143   struct Ebl_Strtab *section_strtab;
    144 
    145   /* Table with defined symbols.  */
    146   asm_symbol_tab symbol_tab;
    147   /* Number of symbols in the table.  */
    148   unsigned int nsymbol_tab;
    149   /* Symbol string table.  */
    150   struct Ebl_Strtab *symbol_strtab;
    151 
    152   /* List of section groups.  */
    153   struct AsmScnGrp *groups;
    154   /* Number of section groups.  */
    155   size_t ngroups;
    156 
    157   /* Current required alignment for common symbols.  */
    158   GElf_Word common_align;
    159 
    160   /* Lock to handle multithreaded programs.  */
    161   rwlock_define (,lock);
    162 
    163   /* Counter for temporary symbols.  */
    164   unsigned int tempsym_count;
    165 
    166   /* Name of the output file.  */
    167   char *fname;
    168   /* The name of the temporary file.  */
    169   char tmp_fname[flexarr_size];
    170 };
    171 
    172 
    173 /* Descriptor for a symbol.  */
    174 struct AsmSym
    175 {
    176   /* Reference to the section which contains the symbol.  */
    177   AsmScn_t *scn;
    178 
    179   /* Type of the symbol.  */
    180   int8_t type;
    181   /* Binding of the symbol.  */
    182   int8_t binding;
    183 
    184   /* Size of the symbol.  */
    185   GElf_Xword size;
    186 
    187   /* Offset in the section.  */
    188   GElf_Off offset;
    189 
    190   /* Symbol table index of the symbol in the symbol table.  */
    191   size_t symidx;
    192 
    193   /* Reference to name of the symbol.  */
    194   struct Ebl_Strent *strent;
    195 };
    196 
    197 
    198 /* Descriptor for section group.  */
    199 struct AsmScnGrp
    200 {
    201   /* Entry in the section header string table.  */
    202   struct Ebl_Strent *strent;
    203 
    204   /* The ELF section.  */
    205   Elf_Scn *scn;
    206 
    207   /* The signature.  */
    208   struct AsmSym *signature;
    209 
    210   /* First member.  */
    211   struct AsmScn *members;
    212   /* Number of members.  */
    213   size_t nmembers;
    214 
    215   /* Flags.  */
    216   Elf32_Word flags;
    217 
    218   /* Next group.  */
    219   struct AsmScnGrp *next;
    220 
    221   /* Name of the section group.  */
    222   char name[flexarr_size];
    223 };
    224 
    225 
    226 /* The default fill pattern: one zero byte.  */
    227 extern const struct FillPattern *__libasm_default_pattern
    228      attribute_hidden;
    229 
    230 
    231 /* Ensure there are at least LEN bytes available in the output buffer
    232    for ASMSCN.  */
    233 extern int __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
    234      internal_function;
    235 
    236 /* Free all resources associated with the assembler context.  */
    237 extern void __libasm_finictx (AsmCtx_t *ctx) internal_function;
    238 
    239 /* Set error code.  */
    240 extern void __libasm_seterrno (int err) internal_function;
    241 
    242 /* Return handle for the named section.  If it was not used before
    243    create it.  */
    244 extern AsmScn_t *__asm_newscn_internal (AsmCtx_t *ctx, const char *scnname,
    245 					GElf_Word type, GElf_Xword flags)
    246      attribute_hidden;
    247 
    248 
    249 /* Internal aliases of the asm_addintXX functions.  */
    250 extern int __asm_addint8_internal (AsmScn_t *asmscn, int8_t num)
    251      attribute_hidden;
    252 extern int __asm_addint16_internal (AsmScn_t *asmscn, int16_t num)
    253      attribute_hidden;
    254 extern int __asm_addint32_internal (AsmScn_t *asmscn, int32_t num)
    255      attribute_hidden;
    256 extern int __asm_addint64_internal (AsmScn_t *asmscn, int64_t num)
    257      attribute_hidden;
    258 
    259 
    260 
    261 /* Test whether given symbol is an internal symbol and if yes, whether
    262    we should nevertheless emit it in the symbol table.  */
    263 // XXX The second part should probably be controlled by an option which
    264 // isn't implemented yet
    265 // XXX Also, the format will change with the backend.
    266 #define asm_emit_symbol_p(name) (strncmp (name, ".L", 2) != 0)
    267 
    268 #endif	/* libasmP.h */
    269