blob: 882bdb99d414c310cef595ee8c16ddf3d7080e82 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Interface for libebl.
Mark Wielaard55cb7df2017-07-20 22:34:29 +02002 Copyright (C) 2000-2010, 2013, 2014, 2015, 2016, 2017 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00007
Mark Wielaardde2ed972012-06-05 17:15:16 +02008 * 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
Ulrich Drepper361df7d2006-04-04 21:38:57 +000021 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
Mark Wielaardde2ed972012-06-05 17:15:16 +020025 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000028
Mark Wielaard858dc652015-01-27 13:07:17 +010029
30/* This is the interface for the Elfutils Backend Library.
31 It is a completely UNSUPPORTED interface. Don't use any libebl
32 function directly. These are only for internal elfutils backends
33 and tools. There is NO source or binary compatible guarantee.
34
Mark Wielaardc8067362015-01-27 15:55:41 +010035 The ABI of the backend modules is not guaranteed. Really, no guarantee
Mark Wielaard858dc652015-01-27 13:07:17 +010036 whatsoever. We are enforcing this in the code. The modules and their
37 users must match. No third-party EBL module are supported or allowed.
38 The only reason there are separate modules is to not have the code for
39 all architectures in all the binaries. */
40
41
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000042#ifndef _LIBEBL_H
43#define _LIBEBL_H 1
44
45#include <gelf.h>
Roland McGrathe47ab762005-11-17 03:16:00 +000046#include "libdw.h"
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000047#include <stdbool.h>
48#include <stddef.h>
49#include <stdint.h>
50
Roland McGrath215c6322005-11-16 22:46:04 +000051#include "elf-knowledge.h"
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000052
53
54/* Opaque type for the handle. */
55typedef struct ebl Ebl;
56
57
Ulrich Drepper41cbd762006-05-27 18:19:23 +000058#ifdef __cplusplus
59extern "C" {
60#endif
61
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000062/* Get backend handle for object associated with ELF handle. */
63extern Ebl *ebl_openbackend (Elf *elf);
64/* Similar but without underlying ELF file. */
65extern Ebl *ebl_openbackend_machine (GElf_Half machine);
66/* Similar but with emulation name given. */
67extern Ebl *ebl_openbackend_emulation (const char *emulation);
68
69/* Free resources allocated for backend handle. */
70extern void ebl_closebackend (Ebl *bh);
71
72
Ulrich Dreppera38998e2005-08-03 02:05:39 +000073/* Information about the descriptor. */
74
75/* Get ELF machine. */
Ulf Hermannc73a00d2017-04-20 15:55:47 +020076extern int ebl_get_elfmachine (Ebl *ebl) __pure_attribute__;
Ulrich Dreppera38998e2005-08-03 02:05:39 +000077
78/* Get ELF class. */
Ulf Hermannc73a00d2017-04-20 15:55:47 +020079extern int ebl_get_elfclass (Ebl *ebl) __pure_attribute__;
Ulrich Dreppera38998e2005-08-03 02:05:39 +000080
81/* Get ELF data encoding. */
Ulf Hermannc73a00d2017-04-20 15:55:47 +020082extern int ebl_get_elfdata (Ebl *ebl) __pure_attribute__;
Ulrich Dreppera38998e2005-08-03 02:05:39 +000083
84
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000085/* Function to call the callback functions including default ELF
86 handling. */
87
88/* Return backend name. */
89extern const char *ebl_backend_name (Ebl *ebl);
90
91/* Return relocation type name. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000092extern const char *ebl_reloc_type_name (Ebl *ebl, int reloc,
93 char *buf, size_t len);
94
95/* Check relocation type. */
96extern bool ebl_reloc_type_check (Ebl *ebl, int reloc);
97
98/* Check relocation type use. */
99extern bool ebl_reloc_valid_use (Ebl *ebl, int reloc);
100
101/* Check if relocation type is for simple absolute relocations.
102 Return ELF_T_{BYTE,HALF,SWORD,SXWORD} for a simple type, else ELF_T_NUM. */
103extern Elf_Type ebl_reloc_simple_type (Ebl *ebl, int reloc);
104
105/* Return true if the symbol type is that referencing the GOT. E.g.,
106 R_386_GOTPC. */
107extern bool ebl_gotpc_reloc_check (Ebl *ebl, int reloc);
108
109/* Return segment type name. */
110extern const char *ebl_segment_type_name (Ebl *ebl, int segment,
111 char *buf, size_t len);
112
113/* Return section type name. */
114extern const char *ebl_section_type_name (Ebl *ebl, int section,
115 char *buf, size_t len);
116
117/* Return section name. */
118extern const char *ebl_section_name (Ebl *ebl, int section, int xsection,
119 char *buf, size_t len,
120 const char *scnnames[], size_t shnum);
121
122/* Return machine flag names. */
123extern const char *ebl_machine_flag_name (Ebl *ebl, GElf_Word flags,
124 char *buf, size_t len);
125
126/* Check whether machine flag is valid. */
127extern bool ebl_machine_flag_check (Ebl *ebl, GElf_Word flags);
128
Ulrich Drepperaa915fd2007-02-05 07:25:33 +0000129/* Check whether SHF_MASKPROC flags are valid. */
130extern bool ebl_machine_section_flag_check (Ebl *ebl, GElf_Xword flags);
131
Roland McGrath13b69602008-04-01 02:30:05 +0000132/* Check whether the section with the given index, header, and name
133 is a special machine section that is valid despite a combination
134 of flags or other details that are not generically valid. */
135extern bool ebl_check_special_section (Ebl *ebl, int ndx,
136 const GElf_Shdr *shdr, const char *name);
137
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000138/* Return symbol type name. */
139extern const char *ebl_symbol_type_name (Ebl *ebl, int symbol,
140 char *buf, size_t len);
141
142/* Return symbol binding name. */
143extern const char *ebl_symbol_binding_name (Ebl *ebl, int binding,
144 char *buf, size_t len);
145
146/* Return dynamic tag name. */
147extern const char *ebl_dynamic_tag_name (Ebl *ebl, int64_t tag,
148 char *buf, size_t len);
149
150/* Check dynamic tag. */
151extern bool ebl_dynamic_tag_check (Ebl *ebl, int64_t tag);
152
Roland McGrath653d3762005-08-13 01:59:10 +0000153/* Check whether given symbol's st_value and st_size are OK despite failing
154 normal checks. */
Ulrich Drepperc5c33a62005-08-13 17:50:47 +0000155extern bool ebl_check_special_symbol (Ebl *ebl, GElf_Ehdr *ehdr,
Roland McGrath653d3762005-08-13 01:59:10 +0000156 const GElf_Sym *sym, const char *name,
157 const GElf_Shdr *destshdr);
158
Mark Wielaard55cb7df2017-07-20 22:34:29 +0200159/* Check if this is a data marker symbol. e.g. '$d' symbols for ARM. */
160extern bool ebl_data_marker_symbol (Ebl *ebl, const GElf_Sym *sym,
161 const char *sname);
162
Mark Wielaarda062b6b2011-03-10 13:02:32 +0100163/* Check whether only valid bits are set on the st_other symbol flag. */
164extern bool ebl_check_st_other_bits (Ebl *ebl, unsigned char st_other);
165
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000166/* Return symbolic representation of OS ABI. */
167extern const char *ebl_osabi_name (Ebl *ebl, int osabi, char *buf, size_t len);
168
169
170/* Return name of the note section type for a core file. */
171extern const char *ebl_core_note_type_name (Ebl *ebl, uint32_t type, char *buf,
172 size_t len);
173
174/* Return name of the note section type for an object file. */
Mark Wielaardbb9d1b42011-04-24 17:53:38 +0200175extern const char *ebl_object_note_type_name (Ebl *ebl, const char *name,
176 uint32_t type, char *buf,
177 size_t len);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000178
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000179/* Print information about object note if available. */
180extern void ebl_object_note (Ebl *ebl, const char *name, uint32_t type,
181 uint32_t descsz, const char *desc);
182
Roland McGrath059c83e2008-02-21 06:19:39 +0000183/* Check whether an attribute in a .gnu_attributes section is recognized.
184 Fills in *TAG_NAME with the name for this tag.
185 If VALUE is a known value for that tag, also fills in *VALUE_NAME. */
186extern bool ebl_check_object_attribute (Ebl *ebl, const char *vendor,
187 int tag, uint64_t value,
188 const char **tag_name,
189 const char **value_name);
190
Mark Wielaard028d0ab2014-05-19 16:52:56 +0200191/* Check whether a section type is a valid reloc target. */
192extern bool ebl_check_reloc_target_type (Ebl *ebl, Elf64_Word sh_type);
193
Roland McGrath059c83e2008-02-21 06:19:39 +0000194
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000195/* Check section name for being that of a debug informatino section. */
196extern bool ebl_debugscn_p (Ebl *ebl, const char *name);
197
198/* Check whether given relocation is a copy relocation. */
199extern bool ebl_copy_reloc_p (Ebl *ebl, int reloc);
200
Ulrich Drepper6ca46002006-06-12 22:40:23 +0000201/* Check whether given relocation is a no-op relocation. */
202extern bool ebl_none_reloc_p (Ebl *ebl, int reloc);
203
Ulrich Drepper28ed8952006-07-07 03:43:47 +0000204/* Check whether given relocation is a relative relocation. */
205extern bool ebl_relative_reloc_p (Ebl *ebl, int reloc);
206
Roland McGrath653d3762005-08-13 01:59:10 +0000207/* Check whether section should be stripped. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000208extern bool ebl_section_strip_p (Ebl *ebl, const GElf_Ehdr *ehdr,
209 const GElf_Shdr *shdr, const char *name,
210 bool remove_comment, bool only_remove_debug);
211
Roland McGrath653d3762005-08-13 01:59:10 +0000212/* Check if backend uses a bss PLT in this file. */
Mark Wielaard712c8fa2014-11-22 23:08:48 +0100213extern bool ebl_bss_plt_p (Ebl *ebl);
Roland McGrath653d3762005-08-13 01:59:10 +0000214
Ulrich Drepper28ed8952006-07-07 03:43:47 +0000215/* Return size of entry in SysV-style hash table. */
216extern int ebl_sysvhash_entrysize (Ebl *ebl);
217
Roland McGrathe47ab762005-11-17 03:16:00 +0000218/* Return location expression to find return value given a
219 DW_TAG_subprogram, DW_TAG_subroutine_type, or similar DIE describing
220 function itself (whose DW_AT_type attribute describes its return type).
221 Returns -1 for a libdw error (see dwarf_errno).
222 Returns -2 for an unrecognized type formation.
223 Returns zero if the function has no return value (e.g. "void" in C).
224 Otherwise, *LOCOPS gets a location expression to find the return value,
225 and returns the number of operations in the expression. The pointer is
226 permanently allocated at least as long as the Ebl handle is open. */
227extern int ebl_return_value_location (Ebl *ebl,
228 Dwarf_Die *functypedie,
229 const Dwarf_Op **locops);
230
Roland McGrathc373d852006-10-10 00:25:21 +0000231/* Fill in register information given DWARF register numbers.
Roland McGrath994b4892005-12-05 22:46:21 +0000232 If NAME is null, return the maximum REGNO + 1 that has a name.
233 Otherwise, store in NAME the name for DWARF register number REGNO
234 and return the number of bytes written (including '\0' terminator).
235 Return -1 if NAMELEN is too short or REGNO is negative or too large.
236 Return 0 if REGNO is unused (a gap in the DWARF number assignment).
237 On success, set *SETNAME to a description like "integer" or "FPU"
238 fit for "%s registers" title display, and *PREFIX to the string
239 that precedes NAME in canonical assembler syntax (e.g. "%" or "$").
240 The NAME string contains identifier characters only (maybe just digits). */
Roland McGrathc373d852006-10-10 00:25:21 +0000241extern ssize_t ebl_register_info (Ebl *ebl,
Roland McGrath994b4892005-12-05 22:46:21 +0000242 int regno, char *name, size_t namelen,
Roland McGrathc373d852006-10-10 00:25:21 +0000243 const char **prefix, const char **setname,
244 int *bits, int *type);
Roland McGrath994b4892005-12-05 22:46:21 +0000245
Roland McGrath1d8bb252008-08-07 08:39:41 +0000246/* Fill in the DWARF register numbers for the registers used in system calls.
247 The SP and PC are what kernel reports call the user stack pointer and PC.
248 The CALLNO and ARGS are the system call number and incoming arguments.
249 Each of these is filled with the DWARF register number corresponding,
250 or -1 if there is none. Returns zero when the information is available. */
251extern int ebl_syscall_abi (Ebl *ebl, int *sp, int *pc,
252 int *callno, int args[6]);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000253
Roland McGrath3c84db32009-06-24 17:41:40 -0700254/* Supply the ABI-specified state of DWARF CFI before CIE initial programs.
255
256 The DWARF 3.0 spec says that the default initial states of all registers
257 are "undefined", unless otherwise specified by the machine/compiler ABI.
258
259 This default is wrong for every machine with the CFI generated by GCC.
260 The EH unwinder does not really distinguish "same_value" and "undefined",
261 since it doesn't matter for unwinding (in either case there is no change
262 to make for that register). GCC generates CFI that says nothing at all
263 about registers it hasn't spilled somewhere. For our unwinder to give
264 the true story, the backend must supply an initial state that uses
265 "same_value" rules for all the callee-saves registers.
266
267 This can fill in the initial_instructions, initial_instructions_end
268 members of *ABI_INFO to point at a CFI instruction stream to process
269 before each CIE's initial instructions. It should set the
270 data_alignment_factor member if it affects the initial instructions.
271
Mark Wielaard3dec3e12013-10-06 17:04:07 +0200272 The callback should not use the register rules DW_CFA_expression or
273 DW_CFA_val_expression. Defining the CFA using DW_CFA_def_cfa_expression
274 is allowed. This is an implementation detail since register rules
275 store expressions as offsets from the .eh_frame or .debug_frame data.
276
Roland McGrath3c84db32009-06-24 17:41:40 -0700277 As a shorthand for some common cases, for this instruction stream
278 we overload some CFI instructions that cannot be used in a CIE:
279
280 DW_CFA_restore -- Change default rule for all unmentioned
281 registers from undefined to same_value.
282
283 This function can also fill in ABI_INFO->return_address_register with the
284 DWARF register number that identifies the actual PC in machine state.
285 If there is no canonical DWARF register number with that meaning, it's
286 left unchanged (callers usually initialize with (Dwarf_Word) -1).
Jan Kratochvil0631d1f2012-10-12 20:43:21 +0200287 This value is not used by CFI per se.
288
289 Function returns 0 on success and -1 for error or unsupported by the
290 backend. */
Roland McGrath3c84db32009-06-24 17:41:40 -0700291extern int ebl_abi_cfi (Ebl *ebl, Dwarf_CIE *abi_info)
292 __nonnull_attribute__ (2);
293
Roland McGrathcb6d8652007-08-23 08:10:54 +0000294/* Register map info. */
295typedef struct
296{
297 Dwarf_Half offset; /* Byte offset in register data block. */
298 Dwarf_Half regno; /* DWARF register number. */
299 uint8_t bits; /* Bits of data for one register. */
300 uint8_t pad; /* Bytes of padding after register's data. */
301 Dwarf_Half count; /* Consecutive register numbers here. */
Jan Kratochvilc6a41482013-12-17 18:49:54 +0100302 bool pc_register;
Roland McGrathcb6d8652007-08-23 08:10:54 +0000303} Ebl_Register_Location;
304
305/* Non-register data items in core notes. */
306typedef struct
307{
308 const char *name; /* Printable identifier. */
309 const char *group; /* Identifier for category of related items. */
310 Dwarf_Half offset; /* Byte offset in note data. */
311 Dwarf_Half count;
312 Elf_Type type;
313 char format;
314 bool thread_identifier;
Jan Kratochvil5cbf42a2013-12-15 18:56:17 +0100315 bool pc_register;
Roland McGrathcb6d8652007-08-23 08:10:54 +0000316} Ebl_Core_Item;
317
Roland McGrath0ccbbcd2010-01-04 21:59:07 -0800318/* Describe the format of a core file note with the given header and NAME.
319 NAME is not guaranteed terminated, it's NHDR->n_namesz raw bytes. */
320extern int ebl_core_note (Ebl *ebl, const GElf_Nhdr *nhdr, const char *name,
Roland McGrathcb6d8652007-08-23 08:10:54 +0000321 GElf_Word *regs_offset, size_t *nregloc,
322 const Ebl_Register_Location **reglocs,
323 size_t *nitems, const Ebl_Core_Item **items)
Roland McGrath0ccbbcd2010-01-04 21:59:07 -0800324 __nonnull_attribute__ (1, 2, 3, 4, 5, 6, 7, 8);
Roland McGrathcb6d8652007-08-23 08:10:54 +0000325
326/* Describe the auxv type number. */
327extern int ebl_auxv_info (Ebl *ebl, GElf_Xword a_type,
328 const char **name, const char **format)
329 __nonnull_attribute__ (1, 3, 4);
330
Jan Kratochvil5cbf42a2013-12-15 18:56:17 +0100331/* Callback type for ebl_set_initial_registers_tid.
332 Register -1 is mapped to PC (if arch PC has no DWARF number).
333 If FIRSTREG is -1 then NREGS has to be 1. */
Jan Kratochvil1c1a53b2013-11-14 20:55:41 +0100334typedef bool (ebl_tid_registers_t) (int firstreg, unsigned nregs,
335 const Dwarf_Word *regs, void *arg)
Jan Kratochvil0b867462013-05-30 14:37:38 +0200336 __nonnull_attribute__ (3);
337
338/* Callback to fetch process data from live TID.
339 EBL architecture has to have EBL_FRAME_NREGS > 0, otherwise the
340 backend doesn't support unwinding and this function call may crash. */
341extern bool ebl_set_initial_registers_tid (Ebl *ebl,
342 pid_t tid,
343 ebl_tid_registers_t *setfunc,
344 void *arg)
345 __nonnull_attribute__ (1, 3);
346
347/* Number of registers to allocate for ebl_set_initial_registers_tid.
348 EBL architecture can unwind iff EBL_FRAME_NREGS > 0. */
349extern size_t ebl_frame_nregs (Ebl *ebl)
350 __nonnull_attribute__ (1);
Roland McGrathcb6d8652007-08-23 08:10:54 +0000351
Jose E. Marchesi42f0df12015-12-08 19:10:40 +0100352/* Offset to apply to the value of the return_address_register, as
353 fetched from a Dwarf CFI. This is used by some backends, where the
354 return_address_register actually contains the call address. */
355extern int ebl_ra_offset (Ebl *ebl)
356 __nonnull_attribute__ (1);
357
Mark Wielaardc1c1c062014-06-14 17:15:37 +0200358/* Mask to use for function symbol or unwind return addresses in case
359 the architecture adds some extra non-address bits to it. This is
360 different from ebl_resolve_sym_value which only works for actual
361 symbol addresses (in non-ET_REL files) that might resolve to an
362 address in a different section. ebl_func_addr_mask is called to
363 turn a given function value into the a real address or offset (the
364 original value might not be a real address). This works for all
365 cases where an actual function address (or offset in ET_REL symbol
366 tables) is needed. */
367extern GElf_Addr ebl_func_addr_mask (Ebl *ebl);
368
Jan Kratochvil5cbf42a2013-12-15 18:56:17 +0100369/* Convert *REGNO as is in DWARF to a lower range suitable for
370 Dwarf_Frame->REGS indexing. */
371extern bool ebl_dwarf_to_regno (Ebl *ebl, unsigned *regno)
372 __nonnull_attribute__ (1, 2);
373
Jan Kratochvilc6a41482013-12-17 18:49:54 +0100374/* Modify PC as fetched from inferior data into valid PC. */
375extern void ebl_normalize_pc (Ebl *ebl, Dwarf_Addr *pc)
376 __nonnull_attribute__ (1, 2);
377
378/* Callback type for ebl_unwind's parameter getfunc. */
379typedef bool (ebl_tid_registers_get_t) (int firstreg, unsigned nregs,
380 Dwarf_Word *regs, void *arg)
381 __nonnull_attribute__ (3);
382
383/* Callback type for ebl_unwind's parameter readfunc. */
384typedef bool (ebl_pid_memory_read_t) (Dwarf_Addr addr, Dwarf_Word *data,
385 void *arg)
386 __nonnull_attribute__ (3);
387
388/* Get previous frame state for an existing frame state. Method is called only
389 if unwinder could not find CFI for current PC. PC is for the
390 existing frame. SETFUNC sets register in the previous frame. GETFUNC gets
391 register from the existing frame. Note that GETFUNC vs. SETFUNC act on
392 a disjunct set of registers. READFUNC reads memory. ARG has to be passed
393 for SETFUNC, GETFUNC and READFUNC. *SIGNAL_FRAMEP is initialized to false,
394 it can be set to true if existing frame is a signal frame. SIGNAL_FRAMEP is
395 never NULL. */
396extern bool ebl_unwind (Ebl *ebl, Dwarf_Addr pc, ebl_tid_registers_t *setfunc,
397 ebl_tid_registers_get_t *getfunc,
398 ebl_pid_memory_read_t *readfunc, void *arg,
399 bool *signal_framep)
400 __nonnull_attribute__ (1, 3, 4, 5, 7);
401
Mark Wielaard159ac522013-12-18 11:05:54 +0100402/* Returns true if the value can be resolved to an address in an
403 allocated section, which will be returned in *ADDR
404 (e.g. function descriptor resolving) */
405extern bool ebl_resolve_sym_value (Ebl *ebl, GElf_Addr *addr)
406 __nonnull_attribute__ (2);
407
Ulrich Drepper41cbd762006-05-27 18:19:23 +0000408#ifdef __cplusplus
409}
410#endif
411
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000412#endif /* libebl.h */