xref: /freebsd/contrib/elftoolchain/libdwarf/dwarf_sections.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * Copyright (c) 2014 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: dwarf_sections.c 3226 2015-06-23 13:00:16Z emaste $");
30 
31 #define	SET(N, V)				\
32 	do {					\
33 		if ((N) != NULL)		\
34 			*(N) = (V);		\
35 	} while (0)
36 
37 int
38 dwarf_get_section_max_offsets_b(Dwarf_Debug dbg, Dwarf_Unsigned *debug_info,
39     Dwarf_Unsigned *debug_abbrev, Dwarf_Unsigned *debug_line,
40     Dwarf_Unsigned *debug_loc, Dwarf_Unsigned *debug_aranges,
41     Dwarf_Unsigned *debug_macinfo, Dwarf_Unsigned *debug_pubnames,
42     Dwarf_Unsigned *debug_str, Dwarf_Unsigned *debug_frame,
43     Dwarf_Unsigned *debug_ranges, Dwarf_Unsigned *debug_pubtypes,
44     Dwarf_Unsigned *debug_types)
45 {
46 	const char *n;
47 	Dwarf_Unsigned sz;
48 	int i;
49 
50 	if (dbg == NULL)
51 		return (DW_DLV_ERROR);
52 
53 	SET(debug_info, 0);
54 	SET(debug_abbrev, 0);
55 	SET(debug_line, 0);
56 	SET(debug_loc, 0);
57 	SET(debug_aranges, 0);
58 	SET(debug_macinfo, 0);
59 	SET(debug_pubnames, 0);
60 	SET(debug_str, 0);
61 	SET(debug_frame, 0);
62 	SET(debug_ranges, 0);
63 	SET(debug_pubtypes, 0);
64 	SET(debug_types, 0);
65 
66 	for (i = 0; (Dwarf_Unsigned) i < dbg->dbg_seccnt; i++) {
67 		n = dbg->dbg_section[i].ds_name;
68 		sz = dbg->dbg_section[i].ds_size;
69 		if (!strcmp(n, ".debug_info"))
70 			SET(debug_info, sz);
71 		else if (!strcmp(n, ".debug_abbrev"))
72 			SET(debug_abbrev, sz);
73 		else if (!strcmp(n, ".debug_line"))
74 			SET(debug_line, sz);
75 		else if (!strcmp(n, ".debug_loc"))
76 			SET(debug_loc, sz);
77 		else if (!strcmp(n, ".debug_aranges"))
78 			SET(debug_aranges, sz);
79 		else if (!strcmp(n, ".debug_macinfo"))
80 			SET(debug_macinfo, sz);
81 		else if (!strcmp(n, ".debug_pubnames"))
82 			SET(debug_pubnames, sz);
83 		else if (!strcmp(n, ".debug_str"))
84 			SET(debug_str, sz);
85 		else if (!strcmp(n, ".debug_frame"))
86 			SET(debug_frame, sz);
87 		else if (!strcmp(n, ".debug_ranges"))
88 			SET(debug_ranges, sz);
89 		else if (!strcmp(n, ".debug_pubtypes"))
90 			SET(debug_pubtypes, sz);
91 		else if (!strcmp(n, ".debug_types"))
92 			SET(debug_types, sz);
93 	}
94 
95 	return (DW_DLV_OK);
96 }
97 
98 int
99 dwarf_get_section_max_offsets(Dwarf_Debug dbg, Dwarf_Unsigned *debug_info,
100     Dwarf_Unsigned *debug_abbrev, Dwarf_Unsigned *debug_line,
101     Dwarf_Unsigned *debug_loc, Dwarf_Unsigned *debug_aranges,
102     Dwarf_Unsigned *debug_macinfo, Dwarf_Unsigned *debug_pubnames,
103     Dwarf_Unsigned *debug_str, Dwarf_Unsigned *debug_frame,
104     Dwarf_Unsigned *debug_ranges, Dwarf_Unsigned *debug_pubtypes)
105 {
106 
107 	return (dwarf_get_section_max_offsets_b(dbg, debug_info, debug_abbrev,
108 	    debug_line, debug_loc, debug_aranges, debug_macinfo,
109 	    debug_pubnames, debug_str, debug_frame, debug_ranges,
110 	    debug_pubtypes, NULL));
111 }
112