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