Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_macinfo.c

raw
1 /*
2 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2011-2020 David Anderson. All Rights Reserved.
4
5 This program is free software; you can redistribute it
6 and/or modify it under the terms of version 2.1 of the
7 GNU Lesser General Public License as published by the Free
8 Software Foundation.
9
10 This program is distributed in the hope that it would be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14
15 Further, this software is distributed without any warranty
16 that it is free of the rightful claim of any third person
17 regarding infringement or the like. Any license provided
18 herein, whether implied or otherwise, applies only to this
19 software file. Patent licenses, if any, provided herein
20 do not apply to combinations of this program with other
21 software, or any other product whatsoever.
22
23 You should have received a copy of the GNU Lesser General
24 Public License along with this program; if not, write the
25 Free Software Foundation, Inc., 51 Franklin Street - Fifth
26 Floor, Boston MA 02110-1301, USA.
27
28 */
29
30 #include <config.h>
31
32 #include <stddef.h> /* NULL size_t */
33 #include <string.h> /* memcpy() strlen() */
34
35 #include "dwarf.h"
36 #include "libdwarf.h"
37 #include "dwarf_base_types.h"
38 #include "libdwarfp.h"
39 #include "dwarf_pro_incl.h"
40 #include "dwarf_pro_opaque.h"
41 #include "dwarf_pro_error.h"
42 #include "dwarf_pro_alloc.h"
43 #include "dwarf_pro_section.h"
44 #include "dwarf_pro_macinfo.h"
45
46 /* I don't much like the error strings this generates, since
47 like the rest of libdwarf they are simple strings with
48 no useful numbers in them. But that's not something I can
49 fix without more work than I have time for
50 right now. davea Nov 94.
51 */
52
53 /* these are gross overestimates of the number of
54 ** bytes needed to store a number in LEB form.
55 ** Just estimates, and since blocks are reasonable size,
56 ** the end-block waste is small.
57 ** Of course the waste is NOT present on disk.
58 */
59
60 #define COMMAND_LEN ENCODE_SPACE_NEEDED
61 #define LINE_LEN ENCODE_SPACE_NEEDED
62 #define BASE_MACINFO_MALLOC_LEN 2048
63
64 static int
65 libdwarf_compose_begin(Dwarf_P_Debug dbg, int code,
66 size_t maxlen, int *compose_error_type)
67 {
68 unsigned char *nextchar;
69 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
70
71 if (curblk == 0) {
72 struct dw_macinfo_block_s *newb;
73 size_t len;
74
75 /* initial allocation */
76 size_t blen = BASE_MACINFO_MALLOC_LEN;
77
78 if (blen < maxlen) {
79 blen = 2 * maxlen;
80 }
81 len = sizeof(struct dw_macinfo_block_s) + blen;
82 newb = (struct dw_macinfo_block_s *)
83 _dwarf_p_get_alloc(dbg, len);
84 if (!newb) {
85 *compose_error_type = DW_DLE_MACINFO_MALLOC_FAIL;
86 return DW_DLV_ERROR;
87 }
88 newb->mb_data =
89 (char *) newb + sizeof(struct dw_macinfo_block_s);
90 newb->mb_avail_len = blen;
91 newb->mb_used_len = 0;
92 newb->mb_macinfo_data_space_len = blen;
93 dbg->de_first_macinfo = newb;
94 dbg->de_current_macinfo = newb;
95 curblk = newb;
96 } else if (curblk->mb_avail_len < maxlen) {
97 struct dw_macinfo_block_s *newb;
98 size_t len;
99
100 /* no space left in block: allocate a new block */
101 size_t blen =
102 dbg->de_current_macinfo->mb_macinfo_data_space_len * 2;
103 if (blen < maxlen) {
104 blen = 2 * maxlen;
105 }
106 len = sizeof(struct dw_macinfo_block_s) + blen;
107 newb = (struct dw_macinfo_block_s *)
108 _dwarf_p_get_alloc(dbg, len);
109 if (!newb) {
110 *compose_error_type = DW_DLE_MACINFO_MALLOC_FAIL;
111 return DW_DLV_ERROR;
112 }
113 newb->mb_data =
114 (char *) newb + sizeof(struct dw_macinfo_block_s);
115 newb->mb_avail_len = blen;
116 newb->mb_used_len = 0;
117 newb->mb_macinfo_data_space_len = blen;
118 dbg->de_first_macinfo->mb_next = newb;
119 dbg->de_current_macinfo = newb;
120 curblk = newb;
121 }
122 /* now curblk has enough room */
123 dbg->de_compose_avail = curblk->mb_avail_len;
124 dbg->de_compose_used_len = curblk->mb_used_len;
125 nextchar = (unsigned char *)
126 (curblk->mb_data + dbg->de_compose_used_len);
127 *nextchar = code;
128 dbg->de_compose_avail--;
129 ++dbg->de_compose_used_len;
130 return DW_DLV_OK;
131 }
132
133 static void
134 libdwarf_compose_add_string(Dwarf_P_Debug dbg,
135 const char *string,
136 size_t len)
137 {
138 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
139 unsigned char *nextchar;
140
141 nextchar = (unsigned char *)
142 (curblk->mb_data + dbg->de_compose_used_len);
143 len += 1; /* count the null terminator */
144 memcpy(nextchar, string, len);
145 dbg->de_compose_avail -= len;
146 dbg->de_compose_used_len += len;
147 return;
148 }
149 static int
150 libdwarf_compose_add_line(Dwarf_P_Debug dbg,
151 Dwarf_Unsigned line, int *compose_error_type)
152 {
153 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
154 unsigned char *nextchar;
155 int res;
156 int nbytes;
157
158 nextchar = (unsigned char *)
159 (curblk->mb_data + dbg->de_compose_used_len);
160
161 /* Put the created leb number directly into the macro buffer If
162 dbg->de_compose_avail is > INT_MAX this will not work as the
163 'int' will look negative to dwarf_encode_leb128! */
164
165 res = dwarf_encode_leb128(line, &nbytes,
166 (char *) nextchar,
167 (int) dbg->de_compose_avail);
168 if (res != DW_DLV_OK) {
169 *compose_error_type = DW_DLE_MACINFO_INTERNAL_ERROR_SPACE;
170 return DW_DLV_ERROR;
171 }
172
173 dbg->de_compose_avail -= nbytes;
174 dbg->de_compose_used_len += nbytes;
175 return DW_DLV_OK;
176 }
177
178 /* This function actually 'commits' the space used by the
179 preceeding calls. */
180 static int
181 libdwarf_compose_complete(Dwarf_P_Debug dbg, int *compose_error_type)
182 {
183 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
184
185 if (dbg->de_compose_used_len >
186 curblk->mb_macinfo_data_space_len) {
187 *compose_error_type = DW_DLE_MACINFO_INTERNAL_ERROR_SPACE;
188 return DW_DLV_ERROR;
189 }
190 curblk->mb_avail_len = dbg->de_compose_avail;
191 curblk->mb_used_len = dbg->de_compose_used_len;
192 return DW_DLV_OK;
193 }
194
195 int
196 dwarf_def_macro(Dwarf_P_Debug dbg,
197 Dwarf_Unsigned line,
198 char *macname, char *macvalue, Dwarf_Error * error)
199 {
200 size_t len;
201 size_t len2;
202 size_t length_est;
203 int res;
204 int compose_error_type;
205
206 if (dbg == NULL) {
207 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
208 return DW_DLV_ERROR;
209 }
210 if (macname == 0) {
211 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_NULL);
212 return DW_DLV_ERROR;
213 }
214 len = strlen(macname) + 1;
215 if (len == 0) {
216 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_EMPTY);
217 return DW_DLV_ERROR;
218 }
219 if (macvalue) {
220 len2 = strlen(macvalue) + 1;
221 } else {
222 len2 = 0;
223 }
224
225 /* 1 for space character we add */
226 length_est = COMMAND_LEN + LINE_LEN + len + len2 + 1;
227
228 res = libdwarf_compose_begin(dbg, DW_MACINFO_define, length_est,
229 &compose_error_type);
230 if (res != DW_DLV_OK) {
231 _dwarf_p_error(NULL, error, compose_error_type);
232 return DW_DLV_ERROR;
233 }
234 res = libdwarf_compose_add_line(dbg, line, &compose_error_type);
235 if (res != DW_DLV_OK) {
236 _dwarf_p_error(NULL, error, compose_error_type);
237 return DW_DLV_ERROR;
238 }
239 libdwarf_compose_add_string(dbg, macname, len);
240 libdwarf_compose_add_string(dbg, " ", 1);
241 if (macvalue) {
242 libdwarf_compose_add_string(dbg, " ", 1);
243 libdwarf_compose_add_string(dbg, macvalue, len2);
244 }
245 res = libdwarf_compose_complete(dbg, &compose_error_type);
246 if (res != DW_DLV_OK) {
247 _dwarf_p_error(NULL, error, compose_error_type);
248 return DW_DLV_ERROR;
249 }
250 return DW_DLV_OK;
251 }
252
253 int
254 dwarf_undef_macro(Dwarf_P_Debug dbg,
255 Dwarf_Unsigned line,
256 char *macname, Dwarf_Error * error)
257 {
258
259 size_t len;
260 size_t length_est;
261 int res;
262 int compose_error_type;
263
264 if (dbg == NULL) {
265 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
266 return DW_DLV_ERROR;
267 }
268 if (macname == 0) {
269 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_NULL);
270 return DW_DLV_ERROR;
271 }
272 len = strlen(macname) + 1;
273 if (len == 0) {
274 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_EMPTY);
275 return DW_DLV_ERROR;
276 }
277 length_est = COMMAND_LEN + LINE_LEN + len;
278 res = libdwarf_compose_begin(dbg, DW_MACINFO_undef, length_est,
279 &compose_error_type);
280 if (res != DW_DLV_OK) {
281 _dwarf_p_error(NULL, error, compose_error_type);
282 return DW_DLV_ERROR;
283 }
284 res = libdwarf_compose_add_line(dbg, line, &compose_error_type);
285 if (res != DW_DLV_OK) {
286 _dwarf_p_error(NULL, error, compose_error_type);
287 return DW_DLV_ERROR;
288 }
289 libdwarf_compose_add_string(dbg, macname, len);
290 res = libdwarf_compose_complete(dbg, &compose_error_type);
291 if (res != DW_DLV_OK) {
292 _dwarf_p_error(NULL, error, compose_error_type);
293 return DW_DLV_ERROR;
294 }
295 return DW_DLV_OK;
296 }
297
298 int
299 dwarf_start_macro_file(Dwarf_P_Debug dbg,
300 Dwarf_Unsigned fileindex,
301 Dwarf_Unsigned linenumber, Dwarf_Error * error)
302 {
303 size_t length_est;
304 int res;
305 int compose_error_type;
306
307 if (dbg == NULL) {
308 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
309 return DW_DLV_ERROR;
310 }
311 length_est = COMMAND_LEN + LINE_LEN + LINE_LEN;
312 res = libdwarf_compose_begin(dbg, DW_MACINFO_start_file,
313 length_est,
314 &compose_error_type);
315 if (res != DW_DLV_OK) {
316 _dwarf_p_error(NULL, error, compose_error_type);
317 return DW_DLV_ERROR;
318 }
319 res = libdwarf_compose_add_line(dbg, fileindex,
320 &compose_error_type);
321 if (res != DW_DLV_OK) {
322 _dwarf_p_error(NULL, error, compose_error_type);
323 return DW_DLV_ERROR;
324 }
325 res = libdwarf_compose_add_line(dbg, linenumber,
326 &compose_error_type);
327 if (res != DW_DLV_OK) {
328 _dwarf_p_error(NULL, error, compose_error_type);
329 return DW_DLV_ERROR;
330 }
331 return DW_DLV_OK;
332 }
333
334 int
335 dwarf_end_macro_file(Dwarf_P_Debug dbg, Dwarf_Error * error)
336 {
337 size_t length_est;
338 int res;
339 int compose_error_type;
340
341 if (dbg == NULL) {
342 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
343 return DW_DLV_ERROR;
344 }
345 length_est = COMMAND_LEN;
346 res = libdwarf_compose_begin(dbg, DW_MACINFO_end_file, length_est,
347 &compose_error_type);
348 if (res == DW_DLV_ERROR) {
349 _dwarf_p_error(dbg, error, compose_error_type);
350 return DW_DLV_ERROR;
351 }
352 res = libdwarf_compose_complete(dbg, &compose_error_type);
353 if (res == DW_DLV_ERROR) {
354 _dwarf_p_error(dbg, error, compose_error_type);
355 return res;
356 }
357 return res;
358 }
359
360 int
361 dwarf_vendor_ext(Dwarf_P_Debug dbg,
362 Dwarf_Unsigned constant,
363 char *string, Dwarf_Error * error)
364 {
365 size_t len;
366 size_t length_est;
367 int res;
368 int compose_error_type;
369
370 if (dbg == NULL) {
371 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
372 return DW_DLV_ERROR;
373 }
374 if (string == 0) {
375 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_NULL);
376 return DW_DLV_ERROR;
377 }
378 len = strlen(string) + 1;
379 if (len == 0) {
380 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_EMPTY);
381 return DW_DLV_ERROR;
382 }
383 length_est = COMMAND_LEN + LINE_LEN + len;
384 res = libdwarf_compose_begin(dbg, DW_MACINFO_vendor_ext,
385 length_est,
386 &compose_error_type);
387 if (res != DW_DLV_OK) {
388 _dwarf_p_error(NULL, error, compose_error_type);
389 return DW_DLV_ERROR;
390 }
391 res = libdwarf_compose_add_line(dbg, constant,
392 &compose_error_type);
393 if (res != DW_DLV_OK) {
394 _dwarf_p_error(NULL, error, compose_error_type);
395 return DW_DLV_ERROR;
396 }
397 libdwarf_compose_add_string(dbg, string, len);
398 libdwarf_compose_complete(dbg, &compose_error_type);
399 return DW_DLV_OK;
400 }
401
402 int
403 _dwarf_pro_transform_macro_info_to_disk(Dwarf_P_Debug dbg,
404 Dwarf_Unsigned *nbufs,
405 Dwarf_Error * error)
406 {
407 /* Total num of bytes in .debug_macinfo section. */
408 Dwarf_Unsigned mac_num_bytes;
409
410 /* Points to first byte of .debug_macinfo buffer. */
411 Dwarf_Small *macinfo;
412
413 /* Fills in the .debug_macinfo buffer. */
414 Dwarf_Small *macinfo_ptr;
415
416 /* Used to scan the section data buffers. */
417 struct dw_macinfo_block_s *m_prev;
418 struct dw_macinfo_block_s *m_sect;
419
420 /* Get the size of the debug_macinfo data */
421 mac_num_bytes = 0;
422 for (m_sect = dbg->de_first_macinfo; m_sect != NULL;
423 m_sect = m_sect->mb_next) {
424 mac_num_bytes += m_sect->mb_used_len;
425 }
426 /* The final entry has a type code of 0 to indicate It is final
427 for this CU Takes just 1 byte. */
428 mac_num_bytes += 1;
429
430 GET_CHUNK(dbg, dbg->de_elf_sects[DEBUG_MACINFO],
431 macinfo, (unsigned long) mac_num_bytes, error);
432
433 macinfo_ptr = macinfo;
434 m_prev = 0;
435 for (m_sect = dbg->de_first_macinfo; m_sect != NULL;
436 m_sect = m_sect->mb_next) {
437 memcpy(macinfo_ptr, m_sect->mb_data, m_sect->mb_used_len);
438 macinfo_ptr += m_sect->mb_used_len;
439 if (m_prev) {
440 _dwarf_p_dealloc((Dwarf_Small *) m_prev);
441 }
442 m_prev = m_sect;
443 }
444 *macinfo_ptr = 0; /* the type code of 0 as last entry */
445 if (m_prev) {
446 _dwarf_p_dealloc((Dwarf_Small *) m_prev);
447 m_prev = 0;
448 }
449
450 dbg->de_first_macinfo = NULL;
451 dbg->de_current_macinfo = NULL;
452
453 *nbufs = dbg->de_n_debug_sect;
454 return DW_DLV_OK;
455 }