1 /* Interface for libasm.
2    Copyright (C) 2002, 2005, 2008 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 _LIBASM_H
30 #define _LIBASM_H 1
31 
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <gelf.h>
35 
36 typedef struct ebl Ebl;
37 
38 
39 /* Opaque type for the assembler context descriptor.  */
40 typedef struct AsmCtx AsmCtx_t;
41 
42 /* Opaque type for a section.  */
43 typedef struct AsmScn AsmScn_t;
44 
45 /* Opaque type for a section group.  */
46 typedef struct AsmScnGrp AsmScnGrp_t;
47 
48 /* Opaque type for a symbol.  */
49 typedef struct AsmSym AsmSym_t;
50 
51 
52 /* Opaque type for the disassembler context descriptor.  */
53 typedef struct DisasmCtx DisasmCtx_t;
54 
55 /* Type used for callback functions to retrieve symbol name.  The
56    symbol reference is in the section designated by the second parameter
57    at an offset described by the first parameter.  The value is the
58    third parameter.  */
59 typedef int (*DisasmGetSymCB_t) (GElf_Addr, Elf32_Word, GElf_Addr, char **,
60 				 size_t *, void *);
61 
62 /* Output function callback.  */
63 typedef int (*DisasmOutputCB_t) (char *, size_t, void *);
64 
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /* Create output file and return descriptor for assembler context.  If
71    TEXTP is true the output is an assembler format text file.
72    Otherwise an object file is created.  The MACHINE parameter
73    corresponds to an EM_ constant from <elf.h>, KLASS specifies the
74    class (32- or 64-bit), and DATA specifies the byte order (little or
75    big endian).  */
76 extern AsmCtx_t *asm_begin (const char *fname, Ebl *ebl, bool textp);
77 
78 /* Abort the operation on the assembler context and free all resources.  */
79 extern int asm_abort (AsmCtx_t *ctx);
80 
81 /* Finalize output file and free all resources.  */
82 extern int asm_end (AsmCtx_t *ctx);
83 
84 
85 /* Return handle for the named section.  If it was not used before
86    create it.  */
87 extern AsmScn_t *asm_newscn (AsmCtx_t *ctx, const char *scnname,
88 			     GElf_Word type, GElf_Xword flags);
89 
90 
91 /* Similar to 'asm_newscn', but make it part of section group GRP.  */
92 extern AsmScn_t *asm_newscn_ingrp (AsmCtx_t *ctx, const char *scnname,
93 				   GElf_Word type, GElf_Xword flags,
94 				   AsmScnGrp_t *grp);
95 
96 /* Create new subsection NR in the given section.  */
97 extern AsmScn_t *asm_newsubscn (AsmScn_t *asmscn, unsigned int nr);
98 
99 
100 /* Return handle for new section group.  The signature symbol can be
101    set later.  */
102 extern AsmScnGrp_t *asm_newscngrp (AsmCtx_t *ctx, const char *grpname,
103 				   AsmSym_t *signature, Elf32_Word flags);
104 
105 /* Set or overwrite signature symbol for group.  */
106 extern int asm_scngrp_newsignature (AsmScnGrp_t *grp, AsmSym_t *signature);
107 
108 
109 /* Add zero terminated string STR of size LEN to (sub)section ASMSCN.  */
110 extern int asm_addstrz (AsmScn_t *asmscn, const char *str, size_t len);
111 
112 /* Add 8-bit signed integer NUM to (sub)section ASMSCN.  */
113 extern int asm_addint8 (AsmScn_t *asmscn, int8_t num);
114 
115 /* Add 8-bit unsigned integer NUM to (sub)section ASMSCN.  */
116 extern int asm_adduint8 (AsmScn_t *asmscn, uint8_t num);
117 
118 /* Add 16-bit signed integer NUM to (sub)section ASMSCN.  */
119 extern int asm_addint16 (AsmScn_t *asmscn, int16_t num);
120 
121 /* Add 16-bit unsigned integer NUM to (sub)section ASMSCN.  */
122 extern int asm_adduint16 (AsmScn_t *asmscn, uint16_t num);
123 
124 /* Add 32-bit signed integer NUM to (sub)section ASMSCN.  */
125 extern int asm_addint32 (AsmScn_t *asmscn, int32_t num);
126 
127 /* Add 32-bit unsigned integer NUM to (sub)section ASMSCN.  */
128 extern int asm_adduint32 (AsmScn_t *asmscn, uint32_t num);
129 
130 /* Add 64-bit signed integer NUM to (sub)section ASMSCN.  */
131 extern int asm_addint64 (AsmScn_t *asmscn, int64_t num);
132 
133 /* Add 64-bit unsigned integer NUM to (sub)section ASMSCN.  */
134 extern int asm_adduint64 (AsmScn_t *asmscn, uint64_t num);
135 
136 
137 /* Add signed little endian base 128 integer NUM to (sub)section ASMSCN.  */
138 extern int asm_addsleb128 (AsmScn_t *asmscn, int32_t num);
139 
140 /* Add unsigned little endian base 128 integer NUM to (sub)section ASMSCN.  */
141 extern int asm_adduleb128 (AsmScn_t *asmscn, uint32_t num);
142 
143 
144 /* Define new symbol NAME for current position in given section ASMSCN.  */
145 extern AsmSym_t *asm_newsym (AsmScn_t *asmscn, const char *name,
146 			     GElf_Xword size, int type, int binding);
147 
148 
149 /* Define new common symbol NAME with given SIZE and alignment.  */
150 extern AsmSym_t *asm_newcomsym (AsmCtx_t *ctx, const char *name,
151 				GElf_Xword size, GElf_Addr align);
152 
153 /* Define new common symbol NAME with given SIZE, VALUE, TYPE, and BINDING.  */
154 extern AsmSym_t *asm_newabssym (AsmCtx_t *ctx, const char *name,
155 				GElf_Xword size, GElf_Addr value,
156 				int type, int binding);
157 
158 
159 /* Align (sub)section offset according to VALUE.  */
160 extern int asm_align (AsmScn_t *asmscn, GElf_Word value);
161 
162 /* Set the byte pattern used to fill gaps created by alignment.  */
163 extern int asm_fill (AsmScn_t *asmscn, void *bytes, size_t len);
164 
165 
166 /* Return ELF descriptor created for the output file of the given context.  */
167 extern Elf *asm_getelf (AsmCtx_t *ctx);
168 
169 
170 /* Return error code of last failing function call.  This value is kept
171    separately for each thread.  */
172 extern int asm_errno (void);
173 
174 /* Return error string for ERROR.  If ERROR is zero, return error string
175    for most recent error or NULL is none occurred.  If ERROR is -1 the
176    behaviour is similar to the last case except that not NULL but a legal
177    string is returned.  */
178 extern const char *asm_errmsg (int __error);
179 
180 
181 /* Create context descriptor for disassembler.  */
182 extern DisasmCtx_t *disasm_begin (Ebl *ebl, Elf *elf, DisasmGetSymCB_t symcb);
183 
184 /* Release descriptor for disassembler.  */
185 extern int disasm_end (DisasmCtx_t *ctx);
186 
187 /* Produce of disassembly output for given memory, store text in
188    provided buffer.  */
189 extern int disasm_str (DisasmCtx_t *ctx, const uint8_t **startp,
190 		       const uint8_t *end, GElf_Addr addr, const char *fmt,
191 		       char **bufp, size_t len, void *symcbarg);
192 
193 /* Produce disassembly output for given memory and output it using the
194    given callback functions.  */
195 extern int disasm_cb (DisasmCtx_t *ctx, const uint8_t **startp,
196 		      const uint8_t *end, GElf_Addr addr, const char *fmt,
197 		      DisasmOutputCB_t outcb, void *outcbarg, void *symcbarg);
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif	/* libasm.h */
204