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