xref: /titanic_52/usr/src/cmd/mdb/common/mdb/mdb_gelf.h (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_MDB_GELF_H
28 #define	_MDB_GELF_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <mdb/mdb_nv.h>
33 #include <mdb/mdb_io.h>
34 
35 #include <sys/types.h>
36 #include <gelf.h>
37 #include <sys/machelf.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 #ifdef _MDB
44 
45 #define	GST_FUZZY	0		/* lookup_by_addr matches closest sym */
46 #define	GST_EXACT	1		/* lookup_by_addr must be exact */
47 
48 #define	GF_FILE		0		/* Open as ELF file image */
49 #define	GF_PROGRAM	1		/* Open as ELF program image */
50 
51 typedef struct mdb_gelf_sect {
52 	GElf_Shdr gs_shdr;		/* ELF section header */
53 	const char *gs_name;		/* Section name */
54 	void *gs_data;			/* Section data */
55 } mdb_gelf_sect_t;
56 
57 typedef struct mdb_gelf_file {
58 	GElf_Ehdr gf_ehdr;		/* ELF file header */
59 	GElf_Phdr *gf_phdrs;		/* Array of program headers */
60 	size_t gf_npload;		/* Number of sorted PT_LOAD phdrs */
61 	GElf_Phdr *gf_dynp;		/* Pointer to PT_DYNAMIC phdr */
62 	GElf_Dyn *gf_dyns;		/* Array of dynamic entries */
63 	size_t gf_ndyns;		/* Number of dynamic entries */
64 	size_t gf_shnum;		/* Number of section headers */
65 	size_t gf_shstrndx;		/* Index of section string table */
66 	size_t gf_phnum;		/* Number of program headers */
67 	mdb_gelf_sect_t *gf_sects;	/* Array of section structs */
68 	mdb_io_t *gf_io;		/* I/o backend for ELF file */
69 	int gf_mode;			/* Mode flag (see above) */
70 } mdb_gelf_file_t;
71 
72 typedef struct mdb_gelf_symtab {
73 	mdb_nv_t gst_nv;		/* Name/value hash for name lookups */
74 	void *gst_asmap;		/* Sorted array of symbol pointers */
75 	size_t gst_aslen;		/* Number of entries in gst_asmap */
76 	size_t gst_asrsv;		/* Actual reserved size of gst_asmap */
77 	const GElf_Ehdr *gst_ehdr;	/* Associated ELF file ehdr */
78 	mdb_gelf_file_t *gst_file;	/* Associated ELF file */
79 	mdb_gelf_sect_t *gst_dsect;	/* Associated ELF data section */
80 	mdb_gelf_sect_t *gst_ssect;	/* Associated ELF string section */
81 	uint_t gst_id;			/* Next symbol ID to use if mutable */
82 	uint_t gst_tabid;		/* ID for symbol table */
83 } mdb_gelf_symtab_t;
84 
85 typedef struct mdb_gelf_dsym {
86 	union {
87 		Elf32_Sym ds_s32;	/* 32-bit native symbol data */
88 		Elf64_Sym ds_s64;	/* 64-bit native symbol data */
89 	} ds_u;
90 	GElf_Sym ds_sym;		/* Generic ELF symbol data */
91 	mdb_var_t *ds_var;		/* Backpointer to nv element */
92 	uint_t ds_id;			/* Symbol id number */
93 } mdb_gelf_dsym_t;
94 
95 extern int mdb_gelf_check(mdb_io_t *, Elf32_Ehdr *, GElf_Half);
96 extern mdb_gelf_file_t *mdb_gelf_create(mdb_io_t *, GElf_Half, int);
97 extern void mdb_gelf_destroy(mdb_gelf_file_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 #endif /* _MDB */
143 
144 #ifdef	__cplusplus
145 }
146 #endif
147 
148 #endif	/* _MDB_GELF_H */
149