3 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of version 2.1 of the GNU Lesser General Public License
7 as published by the Free Software Foundation.
9 This program is distributed in the hope that it would be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 Further, this software is distributed without any warranty that it is
14 free of the rightful claim of any third person regarding infringement
15 or the like. Any license provided herein, whether implied or
16 otherwise, applies only to this software file. Patent licenses, if
17 any, provided herein do not apply to combinations of this program with
18 other software, or any other product whatsoever.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this program; if not, write the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
25 Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
26 Mountain View, CA 94043, or:
30 For further information regarding this notice, see:
32 http://oss.sgi.com/projects/GenInfo/NoticeExplan
39 #include "libdwarfdefs.h"
43 #include "pro_section.h"
44 #include "pro_macinfo.h"
47 I don't much like the error strings this generates, since
48 like the rest of libdwarf they are simple strings with
49 no useful numbers in them. But that's not something I can
50 fix without more work than I have time for
51 right now. davea Nov 94.
54 /* these are gross overestimates of the number of
55 ** bytes needed to store a number in LEB form.
56 ** Just estimates, and since blocks are reasonable size,
57 ** the end-block waste is small.
58 ** Of course the waste is NOT present on disk.
61 #define COMMAND_LEN ENCODE_SPACE_NEEDED
62 #define LINE_LEN ENCODE_SPACE_NEEDED
63 #define BASE_MACINFO_MALLOC_LEN 2048
66 libdwarf_compose_begin(Dwarf_P_Debug dbg
, int code
,
67 size_t maxlen
, int *compose_error_type
)
69 unsigned char *nextchar
;
70 struct dw_macinfo_block_s
*curblk
= dbg
->de_current_macinfo
;
73 struct dw_macinfo_block_s
*newb
;
76 /* initial allocation */
77 size_t blen
= BASE_MACINFO_MALLOC_LEN
;
82 len
= sizeof(struct dw_macinfo_block_s
) + blen
;
84 (struct dw_macinfo_block_s
*) _dwarf_p_get_alloc(dbg
, len
);
86 *compose_error_type
= DW_DLE_MACINFO_MALLOC_FAIL
;
90 (char *) newb
+ sizeof(struct dw_macinfo_block_s
);
91 newb
->mb_avail_len
= blen
;
92 newb
->mb_used_len
= 0;
93 newb
->mb_macinfo_data_space_len
= blen
;
94 dbg
->de_first_macinfo
= newb
;
95 dbg
->de_current_macinfo
= newb
;
97 } else if (curblk
->mb_avail_len
< maxlen
) {
98 struct dw_macinfo_block_s
*newb
;
101 /* no space left in block: allocate a new block */
103 dbg
->de_current_macinfo
->mb_macinfo_data_space_len
* 2;
107 len
= sizeof(struct dw_macinfo_block_s
) + blen
;
109 (struct dw_macinfo_block_s
*) _dwarf_p_get_alloc(dbg
, len
);
111 *compose_error_type
= DW_DLE_MACINFO_MALLOC_FAIL
;
115 (char *) newb
+ sizeof(struct dw_macinfo_block_s
);
116 newb
->mb_avail_len
= blen
;
117 newb
->mb_used_len
= 0;
118 newb
->mb_macinfo_data_space_len
= blen
;
119 dbg
->de_first_macinfo
->mb_next
= newb
;
120 dbg
->de_current_macinfo
= newb
;
123 /* now curblk has enough room */
124 dbg
->de_compose_avail
= curblk
->mb_avail_len
;
125 dbg
->de_compose_used_len
= curblk
->mb_used_len
;
127 (unsigned char *) (curblk
->mb_data
+ dbg
->de_compose_used_len
);
129 dbg
->de_compose_avail
--;
130 ++dbg
->de_compose_used_len
;
137 libdwarf_compose_add_string(Dwarf_P_Debug dbg
, char *string
, size_t len
)
139 struct dw_macinfo_block_s
*curblk
= dbg
->de_current_macinfo
;
140 unsigned char *nextchar
;
143 (unsigned char *) (curblk
->mb_data
+ dbg
->de_compose_used_len
);
145 len
+= 1; /* count the null terminator */
147 memcpy(nextchar
, string
, len
);
148 dbg
->de_compose_avail
-= len
;
149 dbg
->de_compose_used_len
+= len
;
154 libdwarf_compose_add_line(Dwarf_P_Debug dbg
,
155 Dwarf_Unsigned line
, int *compose_error_type
)
157 struct dw_macinfo_block_s
*curblk
= dbg
->de_current_macinfo
;
158 unsigned char *nextchar
;
163 (unsigned char *) (curblk
->mb_data
+ dbg
->de_compose_used_len
);
165 /* Put the created leb number directly into the macro buffer If
166 dbg->de_compose_avail is > INT_MAX this will not work as the
167 'int' will look negative to _dwarf_pro_encode_leb128_nm! */
169 res
= _dwarf_pro_encode_leb128_nm(line
, &nbytes
,
171 (int) dbg
->de_compose_avail
);
172 if (res
!= DW_DLV_OK
) {
173 *compose_error_type
= DW_DLE_MACINFO_INTERNAL_ERROR_SPACE
;
177 dbg
->de_compose_avail
-= nbytes
;
178 dbg
->de_compose_used_len
+= nbytes
;
183 This function actually 'commits' the space used by the
187 libdwarf_compose_complete(Dwarf_P_Debug dbg
, int *compose_error_type
)
189 struct dw_macinfo_block_s
*curblk
= dbg
->de_current_macinfo
;
191 if (dbg
->de_compose_used_len
> curblk
->mb_macinfo_data_space_len
) {
192 *compose_error_type
= DW_DLE_MACINFO_INTERNAL_ERROR_SPACE
;
195 curblk
->mb_avail_len
= dbg
->de_compose_avail
;
196 curblk
->mb_used_len
= dbg
->de_compose_used_len
;
203 dwarf_def_macro(Dwarf_P_Debug dbg
,
205 char *macname
, char *macvalue
, Dwarf_Error
* error
)
211 int compose_error_type
;
214 _dwarf_p_error(NULL
, error
, DW_DLE_DBG_NULL
);
215 return (DW_DLV_ERROR
);
218 _dwarf_p_error(NULL
, error
, DW_DLE_MACINFO_STRING_NULL
);
219 return (DW_DLV_ERROR
);
221 len
= strlen(macname
) + 1;
223 _dwarf_p_error(NULL
, error
, DW_DLE_MACINFO_STRING_EMPTY
);
224 return (DW_DLV_ERROR
);
227 len2
= strlen(macvalue
) + 1;
231 length_est
= COMMAND_LEN
+ LINE_LEN
+ len
+ len2
+ 1; /* 1
237 res
= libdwarf_compose_begin(dbg
, DW_MACINFO_define
, length_est
,
238 &compose_error_type
);
239 if (res
!= DW_DLV_OK
) {
240 _dwarf_p_error(NULL
, error
, compose_error_type
);
241 return (DW_DLV_ERROR
);
243 res
= libdwarf_compose_add_line(dbg
, line
, &compose_error_type
);
244 if (res
!= DW_DLV_OK
) {
245 _dwarf_p_error(NULL
, error
, compose_error_type
);
246 return (DW_DLV_ERROR
);
248 libdwarf_compose_add_string(dbg
, macname
, len
);
249 libdwarf_compose_add_string(dbg
, " ", 1);
251 libdwarf_compose_add_string(dbg
, " ", 1);
252 libdwarf_compose_add_string(dbg
, macvalue
, len2
);
254 res
= libdwarf_compose_complete(dbg
, &compose_error_type
);
255 if (res
!= DW_DLV_OK
) {
256 _dwarf_p_error(NULL
, error
, compose_error_type
);
257 return (DW_DLV_ERROR
);
263 dwarf_undef_macro(Dwarf_P_Debug dbg
,
265 char *macname
, Dwarf_Error
* error
)
271 int compose_error_type
;
274 _dwarf_p_error(NULL
, error
, DW_DLE_DBG_NULL
);
275 return (DW_DLV_ERROR
);
278 _dwarf_p_error(NULL
, error
, DW_DLE_MACINFO_STRING_NULL
);
279 return (DW_DLV_ERROR
);
281 len
= strlen(macname
) + 1;
283 _dwarf_p_error(NULL
, error
, DW_DLE_MACINFO_STRING_EMPTY
);
284 return (DW_DLV_ERROR
);
286 length_est
= COMMAND_LEN
+ LINE_LEN
+ len
;
287 res
= libdwarf_compose_begin(dbg
, DW_MACINFO_undef
, length_est
,
288 &compose_error_type
);
289 if (res
!= DW_DLV_OK
) {
290 _dwarf_p_error(NULL
, error
, compose_error_type
);
291 return (DW_DLV_ERROR
);
293 res
= libdwarf_compose_add_line(dbg
, line
, &compose_error_type
);
294 if (res
!= DW_DLV_OK
) {
295 _dwarf_p_error(NULL
, error
, compose_error_type
);
296 return (DW_DLV_ERROR
);
298 libdwarf_compose_add_string(dbg
, macname
, len
);
299 res
= libdwarf_compose_complete(dbg
, &compose_error_type
);
300 if (res
!= DW_DLV_OK
) {
301 _dwarf_p_error(NULL
, error
, compose_error_type
);
302 return (DW_DLV_ERROR
);
308 dwarf_start_macro_file(Dwarf_P_Debug dbg
,
309 Dwarf_Unsigned fileindex
,
310 Dwarf_Unsigned linenumber
, Dwarf_Error
* error
)
314 int compose_error_type
;
317 _dwarf_p_error(NULL
, error
, DW_DLE_DBG_NULL
);
318 return (DW_DLV_ERROR
);
320 length_est
= COMMAND_LEN
+ LINE_LEN
+ LINE_LEN
;
321 res
= libdwarf_compose_begin(dbg
, DW_MACINFO_start_file
, length_est
,
322 &compose_error_type
);
323 if (res
!= DW_DLV_OK
) {
324 _dwarf_p_error(NULL
, error
, compose_error_type
);
325 return (DW_DLV_ERROR
);
327 res
= libdwarf_compose_add_line(dbg
, fileindex
,
328 &compose_error_type
);
329 if (res
!= DW_DLV_OK
) {
330 _dwarf_p_error(NULL
, error
, compose_error_type
);
331 return (DW_DLV_ERROR
);
333 res
= libdwarf_compose_add_line(dbg
, linenumber
,
334 &compose_error_type
);
335 if (res
!= DW_DLV_OK
) {
336 _dwarf_p_error(NULL
, error
, compose_error_type
);
337 return (DW_DLV_ERROR
);
343 dwarf_end_macro_file(Dwarf_P_Debug dbg
, Dwarf_Error
* error
)
347 int compose_error_type
;
350 _dwarf_p_error(NULL
, error
, DW_DLE_DBG_NULL
);
351 return (DW_DLV_ERROR
);
353 length_est
= COMMAND_LEN
;
354 res
= libdwarf_compose_begin(dbg
, DW_MACINFO_end_file
, length_est
,
355 &compose_error_type
);
356 if (res
!= DW_DLV_OK
) {
357 _dwarf_p_error(NULL
, error
, compose_error_type
);
358 return (DW_DLV_ERROR
);
360 res
= libdwarf_compose_complete(dbg
, &compose_error_type
);
361 if (res
!= DW_DLV_OK
) {
362 _dwarf_p_error(NULL
, error
, compose_error_type
);
363 return (DW_DLV_ERROR
);
369 dwarf_vendor_ext(Dwarf_P_Debug dbg
,
370 Dwarf_Unsigned constant
,
371 char *string
, Dwarf_Error
* error
)
376 int compose_error_type
;
379 _dwarf_p_error(NULL
, error
, DW_DLE_DBG_NULL
);
380 return (DW_DLV_ERROR
);
383 _dwarf_p_error(NULL
, error
, DW_DLE_MACINFO_STRING_NULL
);
384 return (DW_DLV_ERROR
);
386 len
= strlen(string
) + 1;
388 _dwarf_p_error(NULL
, error
, DW_DLE_MACINFO_STRING_EMPTY
);
389 return (DW_DLV_ERROR
);
391 length_est
= COMMAND_LEN
+ LINE_LEN
+ len
;
392 res
= libdwarf_compose_begin(dbg
, DW_MACINFO_vendor_ext
, length_est
,
393 &compose_error_type
);
394 if (res
!= DW_DLV_OK
) {
395 _dwarf_p_error(NULL
, error
, compose_error_type
);
396 return (DW_DLV_ERROR
);
398 res
= libdwarf_compose_add_line(dbg
, constant
, &compose_error_type
);
399 if (res
!= DW_DLV_OK
) {
400 _dwarf_p_error(NULL
, error
, compose_error_type
);
401 return (DW_DLV_ERROR
);
403 libdwarf_compose_add_string(dbg
, string
, len
);
404 libdwarf_compose_complete(dbg
, &compose_error_type
);
405 if (res
!= DW_DLV_OK
) {
406 _dwarf_p_error(NULL
, error
, compose_error_type
);
407 return (DW_DLV_ERROR
);
415 _dwarf_pro_transform_macro_info_to_disk(Dwarf_P_Debug dbg
,
418 /* Total num of bytes in .debug_macinfo section. */
419 Dwarf_Unsigned mac_num_bytes
;
421 /* Points to first byte of .debug_macinfo buffer. */
422 Dwarf_Small
*macinfo
;
424 /* Fills in the .debug_macinfo buffer. */
425 Dwarf_Small
*macinfo_ptr
;
428 /* Used to scan the section data buffers. */
429 struct dw_macinfo_block_s
*m_prev
;
430 struct dw_macinfo_block_s
*m_sect
;
433 /* Get the size of the debug_macinfo data */
435 for (m_sect
= dbg
->de_first_macinfo
; m_sect
!= NULL
;
436 m_sect
= m_sect
->mb_next
) {
437 mac_num_bytes
+= m_sect
->mb_used_len
;
439 /* Tthe final entry has a type code of 0 to indicate It is final
440 for this CU Takes just 1 byte. */
443 GET_CHUNK(dbg
, dbg
->de_elf_sects
[DEBUG_MACINFO
],
444 macinfo
, (unsigned long) mac_num_bytes
, error
);
445 if (macinfo
== NULL
) {
446 _dwarf_p_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
450 macinfo_ptr
= macinfo
;
452 for (m_sect
= dbg
->de_first_macinfo
; m_sect
!= NULL
;
453 m_sect
= m_sect
->mb_next
) {
454 memcpy(macinfo_ptr
, m_sect
->mb_data
, m_sect
->mb_used_len
);
455 macinfo_ptr
+= m_sect
->mb_used_len
;
457 _dwarf_p_dealloc(dbg
, (Dwarf_Small
*) m_prev
);
462 *macinfo_ptr
= 0; /* the type code of 0 as last entry */
464 _dwarf_p_dealloc(dbg
, (Dwarf_Small
*) m_prev
);
468 dbg
->de_first_macinfo
= NULL
;
469 dbg
->de_current_macinfo
= NULL
;
471 return (int) dbg
->de_n_debug_sect
;