📄 ctf_open.c
字号:
/* * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only. * See the file usr/src/LICENSING.NOTICE in this distribution or * http://www.opensolaris.org/license/ for details. */#pragma ident "@(#)ctf_open.c 1.7 04/03/20 SMI"#include <ctf_impl.h>#include <sys/mman.h>#include <sys/zmod.h>static const ctf_dmodel_t _libctf_models[] = { { "ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4 }, { "LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8 }, { NULL, 0, 0, 0, 0, 0, 0 }};const char _CTF_SECTION[] = ".SUNW_ctf";const char _CTF_NULLSTR[] = "";int _libctf_version = CTF_VERSION; /* library client version */int _libctf_debug = 0; /* debugging messages enabled */static ushort_tget_kind_v1(ushort_t info){ return (CTF_INFO_KIND_V1(info));}static ushort_tget_kind_v2(ushort_t info){ return (CTF_INFO_KIND(info));}static ushort_tget_root_v1(ushort_t info){ return (CTF_INFO_ISROOT_V1(info));}static ushort_tget_root_v2(ushort_t info){ return (CTF_INFO_ISROOT(info));}static ushort_tget_vlen_v1(ushort_t info){ return (CTF_INFO_VLEN_V1(info));}static ushort_tget_vlen_v2(ushort_t info){ return (CTF_INFO_VLEN(info));}static const ctf_fileops_t ctf_fileops[] = { { NULL, NULL }, { get_kind_v1, get_root_v1, get_vlen_v1 }, { get_kind_v2, get_root_v2, get_vlen_v2 },};/* * Convert a 32-bit ELF symbol into GElf (Elf64) and return a pointer to it. */static Elf64_Sym *sym_to_gelf(const Elf32_Sym *src, Elf64_Sym *dst){ dst->st_name = src->st_name; dst->st_value = src->st_value; dst->st_size = src->st_size; dst->st_info = src->st_info; dst->st_other = src->st_other; dst->st_shndx = src->st_shndx; return (dst);}/* * Initialize the symtab translation table by filling each entry with the * offset of the CTF type or function data corresponding to each STT_FUNC or * STT_OBJECT entry in the symbol table. */static intinit_symtab(ctf_file_t *fp, const ctf_header_t *hp, const ctf_sect_t *sp, const ctf_sect_t *strp){ const uchar_t *symp = sp->cts_data; uint_t *xp = fp->ctf_sxlate; uint_t *xend = xp + fp->ctf_nsyms; uint_t objtoff = hp->cth_objtoff; uint_t funcoff = hp->cth_funcoff; ushort_t info, vlen; Elf64_Sym sym, *gsp; const char *name; /* * The CTF data object and function type sections are ordered to match * the relative order of the respective symbol types in the symtab. * If no type information is available for a symbol table entry, a * pad is inserted in the CTF section. As a further optimization, * anonymous or undefined symbols are omitted from the CTF data. */ for (; xp < xend; xp++, symp += sp->cts_entsize) { if (sp->cts_entsize == sizeof (Elf32_Sym)) gsp = sym_to_gelf((Elf32_Sym *)(uintptr_t)symp, &sym); else gsp = (Elf64_Sym *)(uintptr_t)symp; if (gsp->st_name < strp->cts_size) name = (const char *)strp->cts_data + gsp->st_name; else name = _CTF_NULLSTR; if (gsp->st_name == 0 || gsp->st_shndx == SHN_UNDEF || strcmp(name, "_START_") == 0 || strcmp(name, "_END_") == 0) { *xp = -1u; continue; } switch (ELF64_ST_TYPE(gsp->st_info)) { case STT_OBJECT: if (objtoff >= hp->cth_funcoff || (gsp->st_shndx == SHN_ABS && gsp->st_value == 0)) { *xp = -1u; break; } *xp = objtoff; objtoff += sizeof (ushort_t); break; case STT_FUNC: if (funcoff >= hp->cth_typeoff) { *xp = -1u; break; } *xp = funcoff; info = *(ushort_t *)((uintptr_t)fp->ctf_buf + funcoff); vlen = LCTF_INFO_VLEN(fp, info); /* * If we encounter a zero pad at the end, just skip it. * Otherwise skip over the function and its return type * (+2) and the argument list (vlen). */ if (LCTF_INFO_KIND(fp, info) == CTF_K_UNKNOWN && vlen == 0) funcoff += sizeof (ushort_t); /* skip pad */ else funcoff += sizeof (ushort_t) * (vlen + 2); break; default: *xp = -1u; break; } } ctf_dprintf("loaded %lu symtab entries\n", fp->ctf_nsyms); return (0);}/* * Initialize the type ID translation table with the byte offset of each type, * and initialize the hash tables of each named type. */static intinit_types(ctf_file_t *fp, const ctf_header_t *hp){ /* LINTED - pointer alignment */ const ctf_type_t *tbuf = (ctf_type_t *)(fp->ctf_buf + hp->cth_typeoff); /* LINTED - pointer alignment */ const ctf_type_t *tend = (ctf_type_t *)(fp->ctf_buf + hp->cth_stroff); ulong_t pop[CTF_K_MAX + 1] = { 0 }; const ctf_type_t *tp; ushort_t id, dst; uint_t *xp; /* * We initially determine whether the container is a child or a parent * based on the value of cth_parname. To support containers that pre- * date cth_parname, we also scan the types themselves for references * to values in the range reserved for child types in our first pass. */ int child = hp->cth_parname != 0; int nlstructs = 0, nlunions = 0; int err; /* * We make two passes through the entire type section. In this first * pass, we count the number of each type and the total number of types. */ for (tp = tbuf; tp < tend; fp->ctf_typemax++) { ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info); ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info); ssize_t size, increment; size_t vbytes; uint_t n; (void) ctf_get_ctt_size(fp, tp, &size, &increment); switch (kind) { case CTF_K_INTEGER: case CTF_K_FLOAT: vbytes = sizeof (uint_t); break; case CTF_K_ARRAY: vbytes = sizeof (ctf_array_t); break; case CTF_K_FUNCTION: vbytes = sizeof (ushort_t) * (vlen + (vlen & 1)); break; case CTF_K_STRUCT: case CTF_K_UNION: if (fp->ctf_version == CTF_VERSION_1 || size < CTF_LSTRUCT_THRESH) { ctf_member_t *mp = (ctf_member_t *) ((uintptr_t)tp + increment); vbytes = sizeof (ctf_member_t) * vlen; for (n = vlen; n != 0; n--, mp++) child |= CTF_TYPE_ISCHILD(mp->ctm_type); } else { ctf_lmember_t *lmp = (ctf_lmember_t *) ((uintptr_t)tp + increment); vbytes = sizeof (ctf_lmember_t) * vlen; for (n = vlen; n != 0; n--, lmp++) child |= CTF_TYPE_ISCHILD(lmp->ctlm_type); } break; case CTF_K_ENUM: vbytes = sizeof (ctf_enum_t) * vlen; break; case CTF_K_FORWARD: case CTF_K_UNKNOWN: vbytes = 0; break; case CTF_K_POINTER: case CTF_K_TYPEDEF: case CTF_K_VOLATILE: case CTF_K_CONST: case CTF_K_RESTRICT: child |= CTF_TYPE_ISCHILD(tp->ctt_type); vbytes = 0; break; default: ctf_dprintf("detected invalid CTF kind -- %u\n", kind); return (ECTF_CORRUPT); } tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes); pop[kind]++; } /* * If we detected a reference to a child type ID, then we know this * container is a child and may have a parent's types imported later. */ if (child) { ctf_dprintf("CTF container %p is a child\n", (void *)fp); fp->ctf_flags |= LCTF_CHILD; } else ctf_dprintf("CTF container %p is a parent\n", (void *)fp); /* * Now that we've counted up the number of each type, we can allocate * the hash tables, type translation table, and pointer table. */ if ((err = ctf_hash_create(&fp->ctf_structs, pop[CTF_K_STRUCT] + pop[CTF_K_FORWARD])) != 0) return (err); if ((err = ctf_hash_create(&fp->ctf_unions, pop[CTF_K_UNION])) != 0) return (err); if ((err = ctf_hash_create(&fp->ctf_enums, pop[CTF_K_ENUM])) != 0) return (err); if ((err = ctf_hash_create(&fp->ctf_names, pop[CTF_K_INTEGER] + pop[CTF_K_FLOAT] + pop[CTF_K_FUNCTION] + pop[CTF_K_TYPEDEF] + pop[CTF_K_POINTER] + pop[CTF_K_VOLATILE] + pop[CTF_K_CONST] + pop[CTF_K_RESTRICT])) != 0) return (err); fp->ctf_txlate = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1)); fp->ctf_ptrtab = ctf_alloc(sizeof (ushort_t) * (fp->ctf_typemax + 1)); if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL) return (EAGAIN); /* memory allocation failed */ xp = fp->ctf_txlate; *xp++ = 0; /* type id 0 is used as a sentinel value */ bzero(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1)); bzero(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1)); /* * In the second pass through the types, we fill in each entry of the * type and pointer tables and add names to the appropriate hashes. */ for (id = 1, tp = tbuf; tp < tend; xp++, id++) { ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info); ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info); ssize_t size, increment; const char *name; size_t vbytes; ctf_helem_t *hep; ctf_encoding_t cte; (void) ctf_get_ctt_size(fp, tp, &size, &increment); name = ctf_strptr(fp, tp->ctt_name); switch (kind) { case CTF_K_INTEGER: case CTF_K_FLOAT: /* * Only insert a new integer base type definition if * this type name has not been defined yet. We re-use * the names with different encodings for bit-fields. */ if ((hep = ctf_hash_lookup(&fp->ctf_names, fp, name, strlen(name))) == NULL) { err = ctf_hash_insert(&fp->ctf_names, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); } else if (ctf_type_encoding(fp, hep->h_type, &cte) == 0 && cte.cte_bits == 0) { /* * Work-around SOS8 stabs bug: replace existing * intrinsic w/ same name if it was zero bits. */ hep->h_type = CTF_INDEX_TO_TYPE(id, child); } vbytes = sizeof (uint_t); break; case CTF_K_ARRAY: vbytes = sizeof (ctf_array_t); break; case CTF_K_FUNCTION: err = ctf_hash_insert(&fp->ctf_names, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); vbytes = sizeof (ushort_t) * (vlen + (vlen & 1)); break; case CTF_K_STRUCT: /* * If a struct's name is already present as a forward * tag, then replace the tag with the struct definition. */ if ((hep = ctf_hash_lookup(&fp->ctf_structs, fp, name, strlen(name))) == NULL) { err = ctf_hash_insert(&fp->ctf_structs, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); } else hep->h_type = CTF_INDEX_TO_TYPE(id, child); if (fp->ctf_version == CTF_VERSION_1 || size < CTF_LSTRUCT_THRESH) vbytes = sizeof (ctf_member_t) * vlen; else { vbytes = sizeof (ctf_lmember_t) * vlen; nlstructs++; } break; case CTF_K_UNION: err = ctf_hash_insert(&fp->ctf_unions, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); if (fp->ctf_version == CTF_VERSION_1 || size < CTF_LSTRUCT_THRESH) vbytes = sizeof (ctf_member_t) * vlen; else { vbytes = sizeof (ctf_lmember_t) * vlen; nlunions++; } break; case CTF_K_ENUM: err = ctf_hash_insert(&fp->ctf_enums, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); vbytes = sizeof (ctf_enum_t) * vlen; break; case CTF_K_TYPEDEF: err = ctf_hash_insert(&fp->ctf_names, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); vbytes = 0; break; case CTF_K_FORWARD: /* * Only insert forward tags into the struct hash if the * struct or tag name is not already present. */ if (ctf_hash_lookup(&fp->ctf_structs, fp, name, strlen(name)) == NULL) { err = ctf_hash_insert(&fp->ctf_structs, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); } vbytes = 0; break; case CTF_K_POINTER: /* * If the type referenced by the pointer is in this CTF * container, then store the index of the pointer type * in fp->ctf_ptrtab[ index of referenced type ]. */ if (CTF_TYPE_ISCHILD(tp->ctt_type) == child && CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax) fp->ctf_ptrtab[ CTF_TYPE_TO_INDEX(tp->ctt_type)] = id; /*FALLTHRU*/ case CTF_K_VOLATILE: case CTF_K_CONST: case CTF_K_RESTRICT: err = ctf_hash_insert(&fp->ctf_names, fp, CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); if (err != 0 && err != ECTF_STRTAB) return (err); /*FALLTHRU*/ default: vbytes = 0; break; } *xp = (uint_t)((uintptr_t)tp - (uintptr_t)fp->ctf_buf); tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -