1 /* $NetBSD: dwarf_cu.c,v 1.4 2022/05/01 17:20:47 jkoshy Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 John Birrell (jb@freebsd.org) 5 * Copyright (c) 2014 Kai Wang 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include "_libdwarf.h" 31 32 __RCSID("$NetBSD: dwarf_cu.c,v 1.4 2022/05/01 17:20:47 jkoshy Exp $"); 33 ELFTC_VCSID("Id: dwarf_cu.c 3041 2014-05-18 15:11:03Z kaiwang27"); 34 35 int 36 dwarf_next_cu_header_c(Dwarf_Debug dbg, Dwarf_Bool is_info, 37 Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version, 38 Dwarf_Off *cu_abbrev_offset, Dwarf_Half *cu_pointer_size, 39 Dwarf_Half *cu_offset_size, Dwarf_Half *cu_extension_size, 40 Dwarf_Sig8 *type_signature, Dwarf_Unsigned *type_offset, 41 Dwarf_Unsigned *cu_next_offset, Dwarf_Error *error) 42 { 43 Dwarf_CU cu; 44 int ret; 45 46 if (dbg == NULL) { 47 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 48 return (DW_DLV_ERROR); 49 } 50 51 if (is_info) { 52 if (dbg->dbg_cu_current == NULL) 53 ret = _dwarf_info_first_cu(dbg, error); 54 else 55 ret = _dwarf_info_next_cu(dbg, error); 56 } else { 57 if (dbg->dbg_tu_current == NULL) 58 ret = _dwarf_info_first_tu(dbg, error); 59 else 60 ret = _dwarf_info_next_tu(dbg, error); 61 } 62 63 if (ret == DW_DLE_NO_ENTRY) { 64 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 65 return (DW_DLV_NO_ENTRY); 66 } else if (ret != DW_DLE_NONE) 67 return (DW_DLV_ERROR); 68 69 if (is_info) { 70 if (dbg->dbg_cu_current == NULL) { 71 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 72 return (DW_DLV_NO_ENTRY); 73 } 74 cu = dbg->dbg_cu_current; 75 } else { 76 if (dbg->dbg_tu_current == NULL) { 77 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 78 return (DW_DLV_NO_ENTRY); 79 } 80 cu = dbg->dbg_tu_current; 81 } 82 83 if (cu_length) 84 *cu_length = cu->cu_length; 85 if (cu_version) 86 *cu_version = cu->cu_version; 87 if (cu_abbrev_offset) 88 *cu_abbrev_offset = (Dwarf_Off) cu->cu_abbrev_offset; 89 if (cu_pointer_size) 90 *cu_pointer_size = cu->cu_pointer_size; 91 if (cu_offset_size) { 92 if (cu->cu_length_size == 4) 93 *cu_offset_size = 4; 94 else 95 *cu_offset_size = 8; 96 } 97 if (cu_extension_size) { 98 if (cu->cu_length_size == 4) 99 *cu_extension_size = 0; 100 else 101 *cu_extension_size = 4; 102 } 103 if (cu_next_offset) 104 *cu_next_offset = cu->cu_next_offset; 105 106 if (!is_info) { 107 if (type_signature) 108 *type_signature = cu->cu_type_sig; 109 if (type_offset) 110 *type_offset = cu->cu_type_offset; 111 } 112 113 return (DW_DLV_OK); 114 } 115 116 117 int 118 dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, 119 Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset, 120 Dwarf_Half *cu_pointer_size, Dwarf_Half *cu_offset_size, 121 Dwarf_Half *cu_extension_size, Dwarf_Unsigned *cu_next_offset, 122 Dwarf_Error *error) 123 { 124 125 return (dwarf_next_cu_header_c(dbg, 1, cu_length, cu_version, 126 cu_abbrev_offset, cu_pointer_size, cu_offset_size, 127 cu_extension_size, NULL, NULL, cu_next_offset, error)); 128 } 129 130 int 131 dwarf_next_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, 132 Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset, 133 Dwarf_Half *cu_pointer_size, Dwarf_Unsigned *cu_next_offset, 134 Dwarf_Error *error) 135 { 136 137 return (dwarf_next_cu_header_b(dbg, cu_length, cu_version, 138 cu_abbrev_offset, cu_pointer_size, NULL, NULL, cu_next_offset, 139 error)); 140 } 141 142 int 143 dwarf_next_types_section(Dwarf_Debug dbg, Dwarf_Error *error) 144 { 145 146 /* Free resource allocated for current .debug_types section. */ 147 _dwarf_type_unit_cleanup(dbg); 148 dbg->dbg_types_loaded = 0; 149 dbg->dbg_types_off = 0; 150 151 /* Reset type unit pointer. */ 152 dbg->dbg_tu_current = NULL; 153 154 /* Search for the next .debug_types section. */ 155 dbg->dbg_types_sec = _dwarf_find_next_types_section(dbg, 156 dbg->dbg_types_sec); 157 158 if (dbg->dbg_types_sec == NULL) { 159 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 160 return (DW_DLV_NO_ENTRY); 161 } 162 163 return (DW_DLV_OK); 164 } 165