1 /* Interface for libasm.
2 Copyright (C) 2002, 2005, 2008 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
38 /* Opaque type for the assembler context descriptor. */
39 typedef struct AsmCtx AsmCtx_t;
41 /* Opaque type for a section. */
42 typedef struct AsmScn AsmScn_t;
44 /* Opaque type for a section group. */
45 typedef struct AsmScnGrp AsmScnGrp_t;
47 /* Opaque type for a symbol. */
48 typedef struct AsmSym AsmSym_t;
51 /* Opaque type for the disassembler context descriptor. */
52 typedef struct DisasmCtx DisasmCtx_t;
54 /* Type used for callback functions to retrieve symbol name. The
55 symbol reference is in the section designated by the second parameter
56 at an offset described by the first parameter. The value is the
58 typedef int (*DisasmGetSymCB_t) (GElf_Addr, Elf32_Word, GElf_Addr, char **,
61 /* Output function callback. */
62 typedef int (*DisasmOutputCB_t) (char *, size_t, void *);
69 /* Create output file and return descriptor for assembler context. If
70 TEXTP is true the output is an assembler format text file.
71 Otherwise an object file is created. The MACHINE parameter
72 corresponds to an EM_ constant from <elf.h>, KLASS specifies the
73 class (32- or 64-bit), and DATA specifies the byte order (little or
75 extern AsmCtx_t *asm_begin (const char *fname, Ebl *ebl, bool textp);
77 /* Abort the operation on the assembler context and free all resources. */
78 extern int asm_abort (AsmCtx_t *ctx);
80 /* Finalize output file and free all resources. */
81 extern int asm_end (AsmCtx_t *ctx);
84 /* Return handle for the named section. If it was not used before
86 extern AsmScn_t *asm_newscn (AsmCtx_t *ctx, const char *scnname,
87 GElf_Word type, GElf_Xword flags);
90 /* Similar to 'asm_newscn', but make it part of section group GRP. */
91 extern AsmScn_t *asm_newscn_ingrp (AsmCtx_t *ctx, const char *scnname,
92 GElf_Word type, GElf_Xword flags,
95 /* Create new subsection NR in the given section. */
96 extern AsmScn_t *asm_newsubscn (AsmScn_t *asmscn, unsigned int nr);
99 /* Return handle for new section group. The signature symbol can be
101 extern AsmScnGrp_t *asm_newscngrp (AsmCtx_t *ctx, const char *grpname,
102 AsmSym_t *signature, Elf32_Word flags);
104 /* Set or overwrite signature symbol for group. */
105 extern int asm_scngrp_newsignature (AsmScnGrp_t *grp, AsmSym_t *signature);
108 /* Add zero terminated string STR of size LEN to (sub)section ASMSCN. */
109 extern int asm_addstrz (AsmScn_t *asmscn, const char *str, size_t len);
111 /* Add 8-bit signed integer NUM to (sub)section ASMSCN. */
112 extern int asm_addint8 (AsmScn_t *asmscn, int8_t num);
114 /* Add 8-bit unsigned integer NUM to (sub)section ASMSCN. */
115 extern int asm_adduint8 (AsmScn_t *asmscn, uint8_t num);
117 /* Add 16-bit signed integer NUM to (sub)section ASMSCN. */
118 extern int asm_addint16 (AsmScn_t *asmscn, int16_t num);
120 /* Add 16-bit unsigned integer NUM to (sub)section ASMSCN. */
121 extern int asm_adduint16 (AsmScn_t *asmscn, uint16_t num);
123 /* Add 32-bit signed integer NUM to (sub)section ASMSCN. */
124 extern int asm_addint32 (AsmScn_t *asmscn, int32_t num);
126 /* Add 32-bit unsigned integer NUM to (sub)section ASMSCN. */
127 extern int asm_adduint32 (AsmScn_t *asmscn, uint32_t num);
129 /* Add 64-bit signed integer NUM to (sub)section ASMSCN. */
130 extern int asm_addint64 (AsmScn_t *asmscn, int64_t num);
132 /* Add 64-bit unsigned integer NUM to (sub)section ASMSCN. */
133 extern int asm_adduint64 (AsmScn_t *asmscn, uint64_t num);
136 /* Add signed little endian base 128 integer NUM to (sub)section ASMSCN. */
137 extern int asm_addsleb128 (AsmScn_t *asmscn, int32_t num);
139 /* Add unsigned little endian base 128 integer NUM to (sub)section ASMSCN. */
140 extern int asm_adduleb128 (AsmScn_t *asmscn, uint32_t num);
143 /* Define new symbol NAME for current position in given section ASMSCN. */
144 extern AsmSym_t *asm_newsym (AsmScn_t *asmscn, const char *name,
145 GElf_Xword size, int type, int binding);
148 /* Define new common symbol NAME with given SIZE and alignment. */
149 extern AsmSym_t *asm_newcomsym (AsmCtx_t *ctx, const char *name,
150 GElf_Xword size, GElf_Addr align);
152 /* Define new common symbol NAME with given SIZE, VALUE, TYPE, and BINDING. */
153 extern AsmSym_t *asm_newabssym (AsmCtx_t *ctx, const char *name,
154 GElf_Xword size, GElf_Addr value,
155 int type, int binding);
158 /* Align (sub)section offset according to VALUE. */
159 extern int asm_align (AsmScn_t *asmscn, GElf_Word value);
161 /* Set the byte pattern used to fill gaps created by alignment. */
162 extern int asm_fill (AsmScn_t *asmscn, void *bytes, size_t len);
165 /* Return ELF descriptor created for the output file of the given context. */
166 extern Elf *asm_getelf (AsmCtx_t *ctx);
169 /* Return error code of last failing function call. This value is kept
170 separately for each thread. */
171 extern int asm_errno (void);
173 /* Return error string for ERROR. If ERROR is zero, return error string
174 for most recent error or NULL is none occurred. If ERROR is -1 the
175 behaviour is similar to the last case except that not NULL but a legal
176 string is returned. */
177 extern const char *asm_errmsg (int __error);
180 /* Create context descriptor for disassembler. */
181 extern DisasmCtx_t *disasm_begin (Ebl *ebl, Elf *elf, DisasmGetSymCB_t symcb);
183 /* Release descriptor for disassembler. */
184 extern int disasm_end (DisasmCtx_t *ctx);
186 /* Produce of disassembly output for given memory, store text in
188 extern int disasm_str (DisasmCtx_t *ctx, const uint8_t **startp,
189 const uint8_t *end, GElf_Addr addr, const char *fmt,
190 char **bufp, size_t len, void *symcbarg);
192 /* Produce disassembly output for given memory and output it using the
193 given callback functions. */
194 extern int disasm_cb (DisasmCtx_t *ctx, const uint8_t **startp,
195 const uint8_t *end, GElf_Addr addr, const char *fmt,
196 DisasmOutputCB_t outcb, void *outcbarg, void *symcbarg);
202 #endif /* libasm.h */