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 98 extern void mdb_gelf_ehdr_to_gehdr(Ehdr *, GElf_Ehdr *); 99 100 typedef enum { GIO_READ, GIO_WRITE } mdb_gelf_rw_t; 101 102 extern ssize_t mdb_gelf_rw(mdb_gelf_file_t *, void *, size_t, uintptr_t, 103 ssize_t (*)(mdb_io_t *, void *, size_t), mdb_gelf_rw_t); 104 105 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_file(mdb_gelf_file_t *, 106 GElf_Word, uint_t); 107 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_file_by_name(mdb_gelf_file_t *, 108 const char *, const char *, uint_t); 109 110 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_raw(const GElf_Ehdr *, 111 const void *, void *, const void *, void *, uint_t); 112 113 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_dynamic(mdb_gelf_file_t *, 114 uint_t); 115 extern mdb_gelf_symtab_t *mdb_gelf_symtab_create_mutable(void); 116 117 extern void mdb_gelf_symtab_destroy(mdb_gelf_symtab_t *); 118 extern size_t mdb_gelf_symtab_size(mdb_gelf_symtab_t *); 119 120 extern const char *mdb_gelf_sym_name(mdb_gelf_symtab_t *, const GElf_Sym *); 121 extern int mdb_gelf_sym_closer(const GElf_Sym *, const GElf_Sym *, uintptr_t); 122 123 extern int mdb_gelf_symtab_lookup_by_addr(mdb_gelf_symtab_t *, 124 uintptr_t, uint_t, char *, size_t, GElf_Sym *, uint_t *); 125 126 extern int mdb_gelf_symtab_lookup_by_name(mdb_gelf_symtab_t *, 127 const char *, GElf_Sym *, uint_t *); 128 129 extern int mdb_gelf_symtab_lookup_by_file(mdb_gelf_symtab_t *, 130 const char *, const char *, GElf_Sym *, uint_t *); 131 132 extern void mdb_gelf_symtab_iter(mdb_gelf_symtab_t *, int (*)(void *, 133 const GElf_Sym *, const char *, uint_t), void *); 134 135 extern void mdb_gelf_symtab_insert(mdb_gelf_symtab_t *, 136 const char *, const GElf_Sym *); 137 138 extern void mdb_gelf_symtab_delete(mdb_gelf_symtab_t *, 139 const char *, GElf_Sym *); 140 141 extern mdb_gelf_sect_t *mdb_gelf_sect_by_name(mdb_gelf_file_t *, 142 const char *); 143 144 extern void *mdb_gelf_sect_load(mdb_gelf_file_t *, mdb_gelf_sect_t *); 145 146 #endif /* _MDB */ 147 148 #ifdef __cplusplus 149 } 150 #endif 151 152 #endif /* _MDB_GELF_H */ 153