1*5ac3bc71Schristos /* $NetBSD: dwarf_cu.c,v 1.5 2024/03/03 17:37:30 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5*5ac3bc71Schristos * Copyright (c) 2014,2023 Kai Wang
69dd9d0cfSchristos * All rights reserved.
79dd9d0cfSchristos *
89dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
99dd9d0cfSchristos * modification, are permitted provided that the following conditions
109dd9d0cfSchristos * are met:
119dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
129dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
139dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
149dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
159dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
169dd9d0cfSchristos *
179dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
189dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
219dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279dd9d0cfSchristos * SUCH DAMAGE.
289dd9d0cfSchristos */
299dd9d0cfSchristos
309dd9d0cfSchristos #include "_libdwarf.h"
319dd9d0cfSchristos
32*5ac3bc71Schristos __RCSID("$NetBSD: dwarf_cu.c,v 1.5 2024/03/03 17:37:30 christos Exp $");
33*5ac3bc71Schristos ELFTC_VCSID("Id: dwarf_cu.c 4013 2023-10-14 22:40:50Z kaiwang27");
349dd9d0cfSchristos
359dd9d0cfSchristos int
dwarf_next_cu_header_d(Dwarf_Debug dbg,Dwarf_Bool is_info,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Half * cu_offset_size,Dwarf_Half * cu_extension_size,Dwarf_Sig8 * type_signature,Dwarf_Unsigned * type_offset,Dwarf_Unsigned * cu_next_offset,Dwarf_Half * cu_type,Dwarf_Error * error)36*5ac3bc71Schristos dwarf_next_cu_header_d(Dwarf_Debug dbg, Dwarf_Bool is_info,
3742bd3019Schristos Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version,
3842bd3019Schristos Dwarf_Off *cu_abbrev_offset, Dwarf_Half *cu_pointer_size,
3942bd3019Schristos Dwarf_Half *cu_offset_size, Dwarf_Half *cu_extension_size,
4042bd3019Schristos Dwarf_Sig8 *type_signature, Dwarf_Unsigned *type_offset,
41*5ac3bc71Schristos Dwarf_Unsigned *cu_next_offset, Dwarf_Half *cu_type,
42*5ac3bc71Schristos Dwarf_Error *error)
439dd9d0cfSchristos {
449dd9d0cfSchristos Dwarf_CU cu;
459dd9d0cfSchristos int ret;
469dd9d0cfSchristos
479dd9d0cfSchristos if (dbg == NULL) {
489dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
499dd9d0cfSchristos return (DW_DLV_ERROR);
509dd9d0cfSchristos }
519dd9d0cfSchristos
5242bd3019Schristos if (is_info) {
539dd9d0cfSchristos if (dbg->dbg_cu_current == NULL)
549dd9d0cfSchristos ret = _dwarf_info_first_cu(dbg, error);
559dd9d0cfSchristos else
569dd9d0cfSchristos ret = _dwarf_info_next_cu(dbg, error);
5742bd3019Schristos } else {
5842bd3019Schristos if (dbg->dbg_tu_current == NULL)
5942bd3019Schristos ret = _dwarf_info_first_tu(dbg, error);
6042bd3019Schristos else
6142bd3019Schristos ret = _dwarf_info_next_tu(dbg, error);
6242bd3019Schristos }
639dd9d0cfSchristos
649dd9d0cfSchristos if (ret == DW_DLE_NO_ENTRY) {
659dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
669dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
679dd9d0cfSchristos } else if (ret != DW_DLE_NONE)
689dd9d0cfSchristos return (DW_DLV_ERROR);
699dd9d0cfSchristos
7042bd3019Schristos if (is_info) {
719dd9d0cfSchristos if (dbg->dbg_cu_current == NULL) {
729dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
739dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
749dd9d0cfSchristos }
759dd9d0cfSchristos cu = dbg->dbg_cu_current;
7642bd3019Schristos } else {
7742bd3019Schristos if (dbg->dbg_tu_current == NULL) {
7842bd3019Schristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
7942bd3019Schristos return (DW_DLV_NO_ENTRY);
8042bd3019Schristos }
8142bd3019Schristos cu = dbg->dbg_tu_current;
8242bd3019Schristos }
839dd9d0cfSchristos
849dd9d0cfSchristos if (cu_length)
859dd9d0cfSchristos *cu_length = cu->cu_length;
869dd9d0cfSchristos if (cu_version)
879dd9d0cfSchristos *cu_version = cu->cu_version;
889dd9d0cfSchristos if (cu_abbrev_offset)
899dd9d0cfSchristos *cu_abbrev_offset = (Dwarf_Off) cu->cu_abbrev_offset;
909dd9d0cfSchristos if (cu_pointer_size)
919dd9d0cfSchristos *cu_pointer_size = cu->cu_pointer_size;
929dd9d0cfSchristos if (cu_offset_size) {
939dd9d0cfSchristos if (cu->cu_length_size == 4)
949dd9d0cfSchristos *cu_offset_size = 4;
959dd9d0cfSchristos else
969dd9d0cfSchristos *cu_offset_size = 8;
979dd9d0cfSchristos }
989dd9d0cfSchristos if (cu_extension_size) {
999dd9d0cfSchristos if (cu->cu_length_size == 4)
1009dd9d0cfSchristos *cu_extension_size = 0;
1019dd9d0cfSchristos else
1029dd9d0cfSchristos *cu_extension_size = 4;
1039dd9d0cfSchristos }
1049dd9d0cfSchristos if (cu_next_offset)
10542bd3019Schristos *cu_next_offset = cu->cu_next_offset;
10642bd3019Schristos
10742bd3019Schristos if (!is_info) {
10842bd3019Schristos if (type_signature)
10942bd3019Schristos *type_signature = cu->cu_type_sig;
11042bd3019Schristos if (type_offset)
11142bd3019Schristos *type_offset = cu->cu_type_offset;
11242bd3019Schristos }
1139dd9d0cfSchristos
114*5ac3bc71Schristos if (cu_type)
115*5ac3bc71Schristos *cu_type = cu->cu_unit_type;
116*5ac3bc71Schristos
1179dd9d0cfSchristos return (DW_DLV_OK);
1189dd9d0cfSchristos }
1199dd9d0cfSchristos
120*5ac3bc71Schristos int
dwarf_next_cu_header_c(Dwarf_Debug dbg,Dwarf_Bool is_info,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Half * cu_offset_size,Dwarf_Half * cu_extension_size,Dwarf_Sig8 * type_signature,Dwarf_Unsigned * type_offset,Dwarf_Unsigned * cu_next_offset,Dwarf_Error * error)121*5ac3bc71Schristos dwarf_next_cu_header_c(Dwarf_Debug dbg, Dwarf_Bool is_info,
122*5ac3bc71Schristos Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version,
123*5ac3bc71Schristos Dwarf_Off *cu_abbrev_offset, Dwarf_Half *cu_pointer_size,
124*5ac3bc71Schristos Dwarf_Half *cu_offset_size, Dwarf_Half *cu_extension_size,
125*5ac3bc71Schristos Dwarf_Sig8 *type_signature, Dwarf_Unsigned *type_offset,
126*5ac3bc71Schristos Dwarf_Unsigned *cu_next_offset, Dwarf_Error *error)
127*5ac3bc71Schristos {
128*5ac3bc71Schristos
129*5ac3bc71Schristos return (dwarf_next_cu_header_d(dbg, 1, cu_length, cu_version,
130*5ac3bc71Schristos cu_abbrev_offset, cu_pointer_size, cu_offset_size,
131*5ac3bc71Schristos cu_extension_size, NULL, NULL, cu_next_offset, NULL, error));
132*5ac3bc71Schristos }
133*5ac3bc71Schristos
13442bd3019Schristos
13542bd3019Schristos int
dwarf_next_cu_header_b(Dwarf_Debug dbg,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Half * cu_offset_size,Dwarf_Half * cu_extension_size,Dwarf_Unsigned * cu_next_offset,Dwarf_Error * error)13642bd3019Schristos dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length,
13742bd3019Schristos Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset,
13842bd3019Schristos Dwarf_Half *cu_pointer_size, Dwarf_Half *cu_offset_size,
13942bd3019Schristos Dwarf_Half *cu_extension_size, Dwarf_Unsigned *cu_next_offset,
14042bd3019Schristos Dwarf_Error *error)
14142bd3019Schristos {
14242bd3019Schristos
14342bd3019Schristos return (dwarf_next_cu_header_c(dbg, 1, cu_length, cu_version,
14442bd3019Schristos cu_abbrev_offset, cu_pointer_size, cu_offset_size,
14542bd3019Schristos cu_extension_size, NULL, NULL, cu_next_offset, error));
14642bd3019Schristos }
14742bd3019Schristos
1489dd9d0cfSchristos int
dwarf_next_cu_header(Dwarf_Debug dbg,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Unsigned * cu_next_offset,Dwarf_Error * error)1499dd9d0cfSchristos dwarf_next_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length,
1509dd9d0cfSchristos Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset,
1519dd9d0cfSchristos Dwarf_Half *cu_pointer_size, Dwarf_Unsigned *cu_next_offset,
1529dd9d0cfSchristos Dwarf_Error *error)
1539dd9d0cfSchristos {
1549dd9d0cfSchristos
1559dd9d0cfSchristos return (dwarf_next_cu_header_b(dbg, cu_length, cu_version,
1569dd9d0cfSchristos cu_abbrev_offset, cu_pointer_size, NULL, NULL, cu_next_offset,
1579dd9d0cfSchristos error));
1589dd9d0cfSchristos }
15942bd3019Schristos
16042bd3019Schristos int
dwarf_next_types_section(Dwarf_Debug dbg,Dwarf_Error * error)16142bd3019Schristos dwarf_next_types_section(Dwarf_Debug dbg, Dwarf_Error *error)
16242bd3019Schristos {
16342bd3019Schristos
16442bd3019Schristos /* Free resource allocated for current .debug_types section. */
16542bd3019Schristos _dwarf_type_unit_cleanup(dbg);
16642bd3019Schristos dbg->dbg_types_loaded = 0;
16742bd3019Schristos dbg->dbg_types_off = 0;
16842bd3019Schristos
16942bd3019Schristos /* Reset type unit pointer. */
17042bd3019Schristos dbg->dbg_tu_current = NULL;
17142bd3019Schristos
17242bd3019Schristos /* Search for the next .debug_types section. */
17342bd3019Schristos dbg->dbg_types_sec = _dwarf_find_next_types_section(dbg,
17442bd3019Schristos dbg->dbg_types_sec);
17542bd3019Schristos
17642bd3019Schristos if (dbg->dbg_types_sec == NULL) {
17742bd3019Schristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
17842bd3019Schristos return (DW_DLV_NO_ENTRY);
17942bd3019Schristos }
18042bd3019Schristos
18142bd3019Schristos return (DW_DLV_OK);
18242bd3019Schristos }
183