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