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