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