xref: /titanic_51/usr/src/cmd/mdb/common/mdb/mdb_gelf.h (revision 7014882c6a3672fd0e5d60200af8643ae53c5928)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <mdb/mdb_nv.h>
32 #include <mdb/mdb_io.h>
33 
34 #include <sys/types.h>
35 #include <gelf.h>
36 #include <sys/machelf.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 #ifdef _MDB
43 
44 #define	GST_FUZZY	0		/* lookup_by_addr matches closest sym */
45 #define	GST_EXACT	1		/* lookup_by_addr must be exact */
46 
47 #define	GF_FILE		0		/* Open as ELF file image */
48 #define	GF_PROGRAM	1		/* Open as ELF program image */
49 
50 typedef struct mdb_gelf_sect {
51 	GElf_Shdr gs_shdr;		/* ELF section header */
52 	const char *gs_name;		/* Section name */
53 	void *gs_data;			/* Section data */
54 } mdb_gelf_sect_t;
55 
56 typedef struct mdb_gelf_file {
57 	GElf_Ehdr gf_ehdr;		/* ELF file header */
58 	GElf_Phdr *gf_phdrs;		/* Array of program headers */
59 	size_t gf_npload;		/* Number of sorted PT_LOAD phdrs */
60 	GElf_Phdr *gf_dynp;		/* Pointer to PT_DYNAMIC phdr */
61 	GElf_Dyn *gf_dyns;		/* Array of dynamic entries */
62 	size_t gf_ndyns;		/* Number of dynamic entries */
63 	size_t gf_shnum;		/* Number of section headers */
64 	size_t gf_shstrndx;		/* Index of section string table */
65 	size_t gf_phnum;		/* Number of program headers */
66 	mdb_gelf_sect_t *gf_sects;	/* Array of section structs */
67 	mdb_io_t *gf_io;		/* I/o backend for ELF file */
68 	int gf_mode;			/* Mode flag (see above) */
69 } mdb_gelf_file_t;
70 
71 typedef struct mdb_gelf_symtab {
72 	mdb_nv_t gst_nv;		/* Name/value hash for name lookups */
73 	void *gst_asmap;		/* Sorted array of symbol pointers */
74 	size_t gst_aslen;		/* Number of entries in gst_asmap */
75 	size_t gst_asrsv;		/* Actual reserved size of gst_asmap */
76 	const GElf_Ehdr *gst_ehdr;	/* Associated ELF file ehdr */
77 	mdb_gelf_file_t *gst_file;	/* Associated ELF file */
78 	mdb_gelf_sect_t *gst_dsect;	/* Associated ELF data section */
79 	mdb_gelf_sect_t *gst_ssect;	/* Associated ELF string section */
80 	uint_t gst_id;			/* Next symbol ID to use if mutable */
81 	uint_t gst_tabid;		/* ID for symbol table */
82 } mdb_gelf_symtab_t;
83 
84 typedef struct mdb_gelf_dsym {
85 	union {
86 		Elf32_Sym ds_s32;	/* 32-bit native symbol data */
87 		Elf64_Sym ds_s64;	/* 64-bit native symbol data */
88 	} ds_u;
89 	GElf_Sym ds_sym;		/* Generic ELF symbol data */
90 	mdb_var_t *ds_var;		/* Backpointer to nv element */
91 	uint_t ds_id;			/* Symbol id number */
92 } mdb_gelf_dsym_t;
93 
94 extern int mdb_gelf_check(mdb_io_t *, Elf32_Ehdr *, GElf_Half);
95 extern mdb_gelf_file_t *mdb_gelf_create(mdb_io_t *, GElf_Half, int);
96 extern void mdb_gelf_destroy(mdb_gelf_file_t *);
97 extern void mdb_gelf_dyns_set(mdb_gelf_file_t *, void *, size_t);
98 
99 extern void mdb_gelf_ehdr_to_gehdr(Ehdr *, GElf_Ehdr *);
100 
101 typedef enum { GIO_READ, GIO_WRITE } mdb_gelf_rw_t;
102 
103 extern ssize_t mdb_gelf_rw(mdb_gelf_file_t *, void *, size_t, uintptr_t,
104     ssize_t (*)(mdb_io_t *, void *, size_t), mdb_gelf_rw_t);
105 
106 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_file(mdb_gelf_file_t *,
107     GElf_Word, uint_t);
108 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_file_by_name(mdb_gelf_file_t *,
109     const char *, const char *, uint_t);
110 
111 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_raw(const GElf_Ehdr *,
112     const void *, void *, const void *, void *, uint_t);
113 
114 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_dynamic(mdb_gelf_file_t *,
115     uint_t);
116 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_mutable(void);
117 
118 extern void mdb_gelf_symtab_destroy(mdb_gelf_symtab_t *);
119 extern size_t mdb_gelf_symtab_size(mdb_gelf_symtab_t *);
120 
121 extern const char *mdb_gelf_sym_name(mdb_gelf_symtab_t *, const GElf_Sym *);
122 extern int mdb_gelf_sym_closer(const GElf_Sym *, const GElf_Sym *, uintptr_t);
123 
124 extern int mdb_gelf_symtab_lookup_by_addr(mdb_gelf_symtab_t *,
125     uintptr_t, uint_t, char *, size_t, GElf_Sym *, uint_t *);
126 
127 extern int mdb_gelf_symtab_lookup_by_name(mdb_gelf_symtab_t *,
128     const char *, GElf_Sym *, uint_t *);
129 
130 extern int mdb_gelf_symtab_lookup_by_file(mdb_gelf_symtab_t *,
131     const char *, const char *, GElf_Sym *, uint_t *);
132 
133 extern void mdb_gelf_symtab_iter(mdb_gelf_symtab_t *, int (*)(void *,
134     const GElf_Sym *, const char *, uint_t), void *);
135 
136 extern void mdb_gelf_symtab_insert(mdb_gelf_symtab_t *,
137     const char *, const GElf_Sym *);
138 
139 extern void mdb_gelf_symtab_delete(mdb_gelf_symtab_t *,
140     const char *, GElf_Sym *);
141 
142 extern mdb_gelf_sect_t *mdb_gelf_sect_by_name(mdb_gelf_file_t *,
143     const char *);
144 
145 extern void *mdb_gelf_sect_load(mdb_gelf_file_t *, mdb_gelf_sect_t *);
146 
147 #endif /* _MDB */
148 
149 #ifdef	__cplusplus
150 }
151 #endif
152 
153 #endif	/* _MDB_GELF_H */
154