xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_gelf.h (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_MDB_GELF_H
27 #define	_MDB_GELF_H
28 
29 #include <mdb/mdb_nv.h>
30 #include <mdb/mdb_io.h>
31 
32 #include <sys/types.h>
33 #include <gelf.h>
34 #include <sys/machelf.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef _MDB
41 
42 #define	GST_FUZZY	0		/* lookup_by_addr matches closest sym */
43 #define	GST_EXACT	1		/* lookup_by_addr must be exact */
44 
45 #define	GF_FILE		0		/* Open as ELF file image */
46 #define	GF_PROGRAM	1		/* Open as ELF program image */
47 
48 typedef struct mdb_gelf_sect {
49 	GElf_Shdr gs_shdr;		/* ELF section header */
50 	const char *gs_name;		/* Section name */
51 	void *gs_data;			/* Section data */
52 } mdb_gelf_sect_t;
53 
54 typedef struct mdb_gelf_file {
55 	GElf_Ehdr gf_ehdr;		/* ELF file header */
56 	GElf_Phdr *gf_phdrs;		/* Array of program headers */
57 	size_t gf_npload;		/* Number of sorted PT_LOAD phdrs */
58 	GElf_Phdr *gf_dynp;		/* Pointer to PT_DYNAMIC phdr */
59 	GElf_Dyn *gf_dyns;		/* Array of dynamic entries */
60 	size_t gf_ndyns;		/* Number of dynamic entries */
61 	size_t gf_shnum;		/* Number of section headers */
62 	size_t gf_shstrndx;		/* Index of section string table */
63 	size_t gf_phnum;		/* Number of program headers */
64 	mdb_gelf_sect_t *gf_sects;	/* Array of section structs */
65 	mdb_io_t *gf_io;		/* I/o backend for ELF file */
66 	int gf_mode;			/* Mode flag (see above) */
67 } mdb_gelf_file_t;
68 
69 typedef struct mdb_gelf_symtab {
70 	mdb_nv_t gst_nv;		/* Name/value hash for name lookups */
71 	void *gst_asmap;		/* Sorted array of symbol pointers */
72 	size_t gst_aslen;		/* Number of entries in gst_asmap */
73 	size_t gst_asrsv;		/* Actual reserved size of gst_asmap */
74 	const GElf_Ehdr *gst_ehdr;	/* Associated ELF file ehdr */
75 	mdb_gelf_file_t *gst_file;	/* Associated ELF file */
76 	mdb_gelf_sect_t *gst_dsect;	/* Associated ELF data section */
77 	mdb_gelf_sect_t *gst_ssect;	/* Associated ELF string section */
78 	uint_t gst_id;			/* Next symbol ID to use if mutable */
79 	uint_t gst_tabid;		/* ID for symbol table */
80 } mdb_gelf_symtab_t;
81 
82 typedef struct mdb_gelf_dsym {
83 	union {
84 		Elf32_Sym ds_s32;	/* 32-bit native symbol data */
85 		Elf64_Sym ds_s64;	/* 64-bit native symbol data */
86 	} ds_u;
87 	GElf_Sym ds_sym;		/* Generic ELF symbol data */
88 	mdb_var_t *ds_var;		/* Backpointer to nv element */
89 	uint_t ds_id;			/* Symbol id number */
90 } mdb_gelf_dsym_t;
91 
92 extern int mdb_gelf_check(mdb_io_t *, Elf32_Ehdr *, GElf_Half);
93 extern mdb_gelf_file_t *mdb_gelf_create(mdb_io_t *, GElf_Half, int);
94 extern void mdb_gelf_destroy(mdb_gelf_file_t *);
95 extern void mdb_gelf_dyns_set(mdb_gelf_file_t *, void *, size_t);
96 
97 extern void mdb_gelf_ehdr_to_gehdr(Ehdr *, GElf_Ehdr *);
98 
99 typedef enum { GIO_READ, GIO_WRITE } mdb_gelf_rw_t;
100 
101 extern ssize_t mdb_gelf_rw(mdb_gelf_file_t *, void *, size_t, uintptr_t,
102     ssize_t (*)(mdb_io_t *, void *, size_t), mdb_gelf_rw_t);
103 
104 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_file(mdb_gelf_file_t *,
105     GElf_Word, uint_t);
106 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_file_by_name(mdb_gelf_file_t *,
107     const char *, const char *, uint_t);
108 
109 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_raw(const GElf_Ehdr *,
110     const void *, void *, const void *, void *, uint_t);
111 
112 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_dynamic(mdb_gelf_file_t *,
113     uint_t);
114 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_mutable(void);
115 
116 extern void mdb_gelf_symtab_destroy(mdb_gelf_symtab_t *);
117 extern size_t mdb_gelf_symtab_size(mdb_gelf_symtab_t *);
118 
119 extern const char *mdb_gelf_sym_name(mdb_gelf_symtab_t *, const GElf_Sym *);
120 extern int mdb_gelf_sym_closer(const GElf_Sym *, const GElf_Sym *, uintptr_t);
121 
122 extern int mdb_gelf_symtab_lookup_by_addr(mdb_gelf_symtab_t *,
123     uintptr_t, uint_t, char *, size_t, GElf_Sym *, uint_t *);
124 
125 extern int mdb_gelf_symtab_lookup_by_name(mdb_gelf_symtab_t *,
126     const char *, GElf_Sym *, uint_t *);
127 
128 extern int mdb_gelf_symtab_lookup_by_file(mdb_gelf_symtab_t *,
129     const char *, const char *, GElf_Sym *, uint_t *);
130 
131 extern void mdb_gelf_symtab_iter(mdb_gelf_symtab_t *, int (*)(void *,
132     const GElf_Sym *, const char *, uint_t), void *);
133 
134 extern void mdb_gelf_symtab_insert(mdb_gelf_symtab_t *,
135     const char *, const GElf_Sym *);
136 
137 extern void mdb_gelf_symtab_delete(mdb_gelf_symtab_t *,
138     const char *, GElf_Sym *);
139 
140 extern mdb_gelf_sect_t *mdb_gelf_sect_by_name(mdb_gelf_file_t *,
141     const char *);
142 
143 extern void *mdb_gelf_sect_load(mdb_gelf_file_t *, mdb_gelf_sect_t *);
144 
145 #endif /* _MDB */
146 
147 #ifdef	__cplusplus
148 }
149 #endif
150 
151 #endif	/* _MDB_GELF_H */
152