Debian Codegraph @

src/lib/libdwarf/dwarf_debug_sup.c

raw
1 /*
2 Copyright (c) 2020, David Anderson
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8
9 Redistributions of source code must retain the above
10 copyright notice, this list of conditions and the following
11 disclaimer.
12
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials
16 provided with the distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 */
33
34 /* This provides access to the DWARF5 .debug_sup section. */
35
36 #include <config.h>
37
38 #include <string.h> /* strlen() */
39
40 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
41 #include "stdafx.h"
42 #endif /* HAVE_STDAFX_H */
43
44 #include "dwarf.h"
45 #include "libdwarf.h"
46 #include "libdwarf_private.h"
47 #include "dwarf_base_types.h"
48 #include "dwarf_opaque.h"
49 #include "dwarf_alloc.h"
50 #include "dwarf_error.h"
51 #include "dwarf_util.h"
52 #include "dwarf_global.h"
53 #include "dwarf_string.h"
54
55 static void
56 get_sup_fields(Dwarf_Debug dbg,
57 struct Dwarf_Section_s **sec_out)
58 {
59 if (IS_INVALID_DBG(dbg)) {
60 return;
61 }
62 *sec_out = &dbg->de_debug_sup;
63 }
64
65 static int
66 load_sup(Dwarf_Debug dbg,
67 Dwarf_Error *error)
68 {
69 struct Dwarf_Section_s * sec = 0;
70 int res;
71
72 get_sup_fields(dbg,&sec);
73 if (!sec) {
74 return DW_DLV_NO_ENTRY;
75 }
76 res = _dwarf_load_section(dbg,sec,error);
77 return res;
78 }
79
80 /* New for DWARF5 in July 2020. */
81 int
82 dwarf_get_debug_sup(Dwarf_Debug dbg,
83 Dwarf_Half * version_out,
84 Dwarf_Small * is_supplementary_out,
85 char ** filename_out,
86 Dwarf_Unsigned * checksum_len_out,
87 Dwarf_Small ** checksum_out,
88 Dwarf_Error * error)
89 {
90 Dwarf_Unsigned version = 0;
91 Dwarf_Small is_supp = 0;
92 char *filename = 0;
93 Dwarf_Unsigned checksum_len = 0;
94 Dwarf_Small *checksum_ptr = 0;
95 int res = 0;
96 Dwarf_Small *data = 0;
97 Dwarf_Small *enddata = 0;
98 Dwarf_Unsigned size = 0;
99
100 CHECK_DBG(dbg,error,"dwarf_get_debug_sup()");
101 res = load_sup(dbg,error);
102 if (res != DW_DLV_OK) {
103 return res;
104 }
105 data = dbg->de_debug_sup.dss_data;
106 size = dbg->de_debug_sup.dss_size;
107 if (dbg->de_filesize && size > dbg->de_filesize) {
108 dwarfstring m;
109
110 dwarfstring_constructor(&m);
111 dwarfstring_append_printf_u(&m, "DW_DLE_DEBUG_SUP_ERROR: "
112 ".debug_sup section size 0x%x bigger than file "
113 "size! Corrupt",
114 size);
115 _dwarf_error_string(dbg,error,
116 DW_DLE_DEBUG_SUP_ERROR,
117 dwarfstring_string(&m));
118 dwarfstring_destructor(&m);
119 return DW_DLV_ERROR;
120 }
121 enddata = data + size;
122 res = _dwarf_read_unaligned_ck_wrapper(dbg,
123 &version,
124 data,DWARF_HALF_SIZE,
125 enddata,error);
126 if (res != DW_DLV_OK) {
127 return res;
128 }
129 data+= DWARF_HALF_SIZE;
130 if ((data+4) > enddata) {
131 dwarfstring m;
132
133 dwarfstring_constructor(&m);
134 dwarfstring_append_printf_u(&m, "DW_DLE_DEBUG_SUP_ERROR: "
135 " .debug_sup section size 0x%x"
136 " too small to be correct! Corrupt",
137 size);
138 _dwarf_error_string(dbg,error,
139 DW_DLE_DEBUG_SUP_ERROR,
140 dwarfstring_string(&m));
141 dwarfstring_destructor(&m);
142 return DW_DLV_ERROR;
143 }
144
145 is_supp = (Dwarf_Small)*data;
146 ++data;
147 res = _dwarf_check_string_valid(dbg,data,data,enddata,
148 DW_DLE_DEBUG_SUP_STRING_ERROR,error);
149 if (res != DW_DLV_OK) {
150 return res;
151 }
152 filename = (char *)data;
153
154 data += strlen((char *)data) +1;
155
156 res = _dwarf_leb128_uword_wrapper(dbg, &data,enddata,
157 &checksum_len,error);
158 if (res != DW_DLV_OK) {
159 return res;
160 }
161
162 if (checksum_len >= size) {
163 dwarfstring m;
164
165 dwarfstring_constructor(&m);
166 dwarfstring_append_printf_u(&m, "DW_DLE_DEBUG_SUP_ERROR: "
167 " .debug_sup checksum length 0x%x"
168 " too large to be correct! Corrupt",
169 checksum_len);
170 _dwarf_error_string(dbg,error,
171 DW_DLE_DEBUG_SUP_ERROR,
172 dwarfstring_string(&m));
173 dwarfstring_destructor(&m);
174 return DW_DLV_ERROR;
175 }
176 if ((data +checksum_len) > enddata) {
177 dwarfstring m;
178
179 dwarfstring_constructor(&m);
180 dwarfstring_append_printf_u(&m, "DW_DLE_DEBUG_SUP_ERROR: "
181 " .debug_sup checksum (length 0x%x) "
182 " runs off the end of the section, Corrupt data",
183 checksum_len);
184 _dwarf_error_string(dbg,error,
185 DW_DLE_DEBUG_SUP_ERROR,
186 dwarfstring_string(&m));
187 dwarfstring_destructor(&m);
188 return DW_DLV_ERROR;
189 }
190 checksum_ptr = data;
191 if (version_out) {
192 *version_out = (Dwarf_Half)version;
193 }
194 if (is_supp) {
195 *is_supplementary_out = is_supp;
196 }
197 if (filename_out) {
198 *filename_out = filename;
199 }
200 if (checksum_len_out) {
201 *checksum_len_out = checksum_len;
202 }
203 if (checksum_out) {
204 *checksum_out = checksum_ptr;
205 }
206 return DW_DLV_OK;
207 }