00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "config.h"
00043 #include "libdwarfdefs.h"
00044 #include <stdio.h>
00045 #include <string.h>
00046 #include "pro_incl.h"
00047 #include "pro_section.h"
00048 #include "pro_macinfo.h"
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #define COMMAND_LEN ENCODE_SPACE_NEEDED
00066 #define LINE_LEN ENCODE_SPACE_NEEDED
00067 #define BASE_MACINFO_MALLOC_LEN 2048
00068
00069 static int
00070 libdwarf_compose_begin(Dwarf_P_Debug dbg, int code,
00071 size_t maxlen, int *compose_error_type)
00072 {
00073 unsigned char *nextchar;
00074 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
00075
00076 if (curblk == 0) {
00077 struct dw_macinfo_block_s *newb;
00078 size_t len;
00079
00080
00081 size_t blen = BASE_MACINFO_MALLOC_LEN;
00082
00083 if (blen < maxlen) {
00084 blen = 2 * maxlen;
00085 }
00086 len = sizeof(struct dw_macinfo_block_s) + blen;
00087 newb =
00088 (struct dw_macinfo_block_s *) _dwarf_p_get_alloc(dbg, len);
00089 if (!newb) {
00090 *compose_error_type = DW_DLE_MACINFO_MALLOC_FAIL;
00091 return DW_DLV_ERROR;
00092 }
00093 newb->mb_data =
00094 (char *) newb + sizeof(struct dw_macinfo_block_s);
00095 newb->mb_avail_len = blen;
00096 newb->mb_used_len = 0;
00097 newb->mb_macinfo_data_space_len = blen;
00098 dbg->de_first_macinfo = newb;
00099 dbg->de_current_macinfo = newb;
00100 curblk = newb;
00101 } else if (curblk->mb_avail_len < maxlen) {
00102 struct dw_macinfo_block_s *newb;
00103 size_t len;
00104
00105
00106 size_t blen =
00107 dbg->de_current_macinfo->mb_macinfo_data_space_len * 2;
00108 if (blen < maxlen) {
00109 blen = 2 * maxlen;
00110 }
00111 len = sizeof(struct dw_macinfo_block_s) + blen;
00112 newb =
00113 (struct dw_macinfo_block_s *) _dwarf_p_get_alloc(dbg, len);
00114 if (!newb) {
00115 *compose_error_type = DW_DLE_MACINFO_MALLOC_FAIL;
00116 return DW_DLV_ERROR;
00117 }
00118 newb->mb_data =
00119 (char *) newb + sizeof(struct dw_macinfo_block_s);
00120 newb->mb_avail_len = blen;
00121 newb->mb_used_len = 0;
00122 newb->mb_macinfo_data_space_len = blen;
00123 dbg->de_first_macinfo->mb_next = newb;
00124 dbg->de_current_macinfo = newb;
00125 curblk = newb;
00126 }
00127
00128 dbg->de_compose_avail = curblk->mb_avail_len;
00129 dbg->de_compose_used_len = curblk->mb_used_len;
00130 nextchar =
00131 (unsigned char *) (curblk->mb_data + dbg->de_compose_used_len);
00132 *nextchar = code;
00133 dbg->de_compose_avail--;
00134 ++dbg->de_compose_used_len;
00135 return DW_DLV_OK;
00136 }
00137
00138 static void
00139 libdwarf_compose_add_string(Dwarf_P_Debug dbg, char *string, size_t len)
00140 {
00141 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
00142 unsigned char *nextchar;
00143
00144 nextchar =
00145 (unsigned char *) (curblk->mb_data + dbg->de_compose_used_len);
00146
00147 len += 1;
00148
00149 memcpy(nextchar, string, len);
00150 dbg->de_compose_avail -= len;
00151 dbg->de_compose_used_len += len;
00152 return;
00153
00154 }
00155
00156 #ifdef KEY
00157
00158
00159
00160
00161 static void
00162 libdwarf_compose_add_bytes(Dwarf_P_Debug dbg, char *string, size_t len)
00163 {
00164 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
00165 unsigned char *nextchar;
00166
00167 nextchar =
00168 (unsigned char *) (curblk->mb_data + dbg->de_compose_used_len);
00169
00170 memcpy(nextchar, string, len);
00171 dbg->de_compose_avail -= len;
00172 dbg->de_compose_used_len += len;
00173 return;
00174
00175 }
00176 #endif
00177
00178 static int
00179 libdwarf_compose_add_line(Dwarf_P_Debug dbg,
00180 Dwarf_Unsigned line, int *compose_error_type)
00181 {
00182 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
00183 unsigned char *nextchar;
00184 int res;
00185 int nbytes;
00186
00187 nextchar =
00188 (unsigned char *) (curblk->mb_data + dbg->de_compose_used_len);
00189
00190
00191
00192
00193
00194 res = _dwarf_pro_encode_leb128_nm(line, &nbytes,
00195 (char *) nextchar,
00196 (int) dbg->de_compose_avail);
00197 if (res != DW_DLV_OK) {
00198 *compose_error_type = DW_DLE_MACINFO_INTERNAL_ERROR_SPACE;
00199 return DW_DLV_ERROR;
00200 }
00201
00202 dbg->de_compose_avail -= nbytes;
00203 dbg->de_compose_used_len += nbytes;
00204 return DW_DLV_OK;
00205 }
00206
00207
00208
00209
00210
00211 static int
00212 libdwarf_compose_complete(Dwarf_P_Debug dbg, int *compose_error_type)
00213 {
00214 struct dw_macinfo_block_s *curblk = dbg->de_current_macinfo;
00215
00216 if (dbg->de_compose_used_len > curblk->mb_macinfo_data_space_len) {
00217 *compose_error_type = DW_DLE_MACINFO_INTERNAL_ERROR_SPACE;
00218 return DW_DLV_ERROR;
00219 }
00220 curblk->mb_avail_len = dbg->de_compose_avail;
00221 curblk->mb_used_len = dbg->de_compose_used_len;
00222 return DW_DLV_OK;
00223 }
00224
00225
00226
00227 int
00228 dwarf_def_macro(Dwarf_P_Debug dbg,
00229 Dwarf_Unsigned line,
00230 char *macname, char *macvalue, Dwarf_Error * error)
00231 {
00232 size_t len;
00233 size_t len2;
00234 size_t length_est;
00235 int res;
00236 int compose_error_type;
00237
00238 if (dbg == NULL) {
00239 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
00240 return (DW_DLV_ERROR);
00241 }
00242 if (macname == 0) {
00243 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_NULL);
00244 return (DW_DLV_ERROR);
00245 }
00246 #ifdef TARG_IA64
00247 len = strlen(macname) + 1;
00248 #else
00249 len = strlen(macname);
00250 #endif
00251 if (len == 0) {
00252 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_EMPTY);
00253 return (DW_DLV_ERROR);
00254 }
00255 #ifdef TARG_IA64
00256 if (macvalue) {
00257 len2 = strlen(macvalue) + 1;
00258 } else {
00259 len2 = 0;
00260 }
00261 length_est = COMMAND_LEN + LINE_LEN + len + len2 + 1;
00262
00263
00264
00265
00266
00267
00268 #endif
00269 res = libdwarf_compose_begin(dbg, DW_MACINFO_define, length_est,
00270 &compose_error_type);
00271 if (res != DW_DLV_OK) {
00272 _dwarf_p_error(NULL, error, compose_error_type);
00273 return (DW_DLV_ERROR);
00274 }
00275 res = libdwarf_compose_add_line(dbg, line, &compose_error_type);
00276 if (res != DW_DLV_OK) {
00277 _dwarf_p_error(NULL, error, compose_error_type);
00278 return (DW_DLV_ERROR);
00279 }
00280 #ifdef TARG_IA64
00281 libdwarf_compose_add_string(dbg, macname, len);
00282 libdwarf_compose_add_string(dbg, " ", 1);
00283 if (macvalue) {
00284 libdwarf_compose_add_string(dbg, " ", 1);
00285 libdwarf_compose_add_string(dbg, macvalue, len2);
00286 }
00287 #else
00288 if (macvalue) {
00289 libdwarf_compose_add_bytes(dbg, macname, len);
00290 libdwarf_compose_add_bytes(dbg, " ", 1);
00291 libdwarf_compose_add_string(dbg, macvalue, len2);
00292 }
00293 else {
00294 libdwarf_compose_add_string(dbg, macname, len);
00295 }
00296 #endif
00297
00298 res = libdwarf_compose_complete(dbg, &compose_error_type);
00299 if (res != DW_DLV_OK) {
00300 _dwarf_p_error(NULL, error, compose_error_type);
00301 return (DW_DLV_ERROR);
00302 }
00303 return DW_DLV_OK;
00304 }
00305
00306 int
00307 dwarf_undef_macro(Dwarf_P_Debug dbg,
00308 Dwarf_Unsigned line,
00309 char *macname, Dwarf_Error * error)
00310 {
00311
00312 size_t len;
00313 size_t length_est;
00314 int res;
00315 int compose_error_type;
00316
00317 if (dbg == NULL) {
00318 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
00319 return (DW_DLV_ERROR);
00320 }
00321 if (macname == 0) {
00322 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_NULL);
00323 return (DW_DLV_ERROR);
00324 }
00325 #ifdef TARG_IA64
00326 len = strlen(macname) + 1;
00327 #else
00328 len = strlen(macname);
00329 #endif
00330 if (len == 0) {
00331 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_EMPTY);
00332 return (DW_DLV_ERROR);
00333 }
00334 #ifdef TARG_IA64
00335 length_est = COMMAND_LEN + LINE_LEN + len;
00336 #else
00337 length_est = COMMAND_LEN + LINE_LEN + len + 1;
00338 #endif
00339 res = libdwarf_compose_begin(dbg, DW_MACINFO_undef, length_est,
00340 &compose_error_type);
00341 if (res != DW_DLV_OK) {
00342 _dwarf_p_error(NULL, error, compose_error_type);
00343 return (DW_DLV_ERROR);
00344 }
00345 res = libdwarf_compose_add_line(dbg, line, &compose_error_type);
00346 if (res != DW_DLV_OK) {
00347 _dwarf_p_error(NULL, error, compose_error_type);
00348 return (DW_DLV_ERROR);
00349 }
00350 libdwarf_compose_add_string(dbg, macname, len);
00351 res = libdwarf_compose_complete(dbg, &compose_error_type);
00352 if (res != DW_DLV_OK) {
00353 _dwarf_p_error(NULL, error, compose_error_type);
00354 return (DW_DLV_ERROR);
00355 }
00356 return DW_DLV_OK;
00357 }
00358
00359
00360 int
00361 dwarf_start_macro_file(Dwarf_P_Debug dbg,
00362 Dwarf_Unsigned fileindex,
00363 Dwarf_Unsigned linenumber, Dwarf_Error * error)
00364 {
00365 size_t length_est;
00366 int res;
00367 int compose_error_type;
00368
00369 if (dbg == NULL) {
00370 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
00371 return (DW_DLV_ERROR);
00372 }
00373 length_est = COMMAND_LEN + LINE_LEN + LINE_LEN;
00374 res = libdwarf_compose_begin(dbg, DW_MACINFO_start_file, length_est,
00375 &compose_error_type);
00376 if (res != DW_DLV_OK) {
00377 _dwarf_p_error(NULL, error, compose_error_type);
00378 return (DW_DLV_ERROR);
00379 }
00380 res = libdwarf_compose_add_line(dbg, linenumber,
00381 &compose_error_type);
00382 if (res != DW_DLV_OK) {
00383 _dwarf_p_error(NULL, error, compose_error_type);
00384 return (DW_DLV_ERROR);
00385 }
00386 res = libdwarf_compose_add_line(dbg, fileindex,
00387 &compose_error_type);
00388 if (res != DW_DLV_OK) {
00389 _dwarf_p_error(NULL, error, compose_error_type);
00390 return (DW_DLV_ERROR);
00391 }
00392 res = libdwarf_compose_complete(dbg,
00393 &compose_error_type);
00394 if (res != DW_DLV_OK) {
00395 _dwarf_p_error(NULL, error, compose_error_type);
00396 return (DW_DLV_ERROR);
00397 }
00398 return DW_DLV_OK;
00399 }
00400
00401 int
00402 dwarf_end_macro_file(Dwarf_P_Debug dbg, Dwarf_Error * error)
00403 {
00404 size_t length_est;
00405 int res;
00406 int compose_error_type;
00407
00408 if (dbg == NULL) {
00409 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
00410 return (DW_DLV_ERROR);
00411 }
00412 length_est = COMMAND_LEN;
00413 res = libdwarf_compose_begin(dbg, DW_MACINFO_end_file, length_est,
00414 &compose_error_type);
00415 if (res != DW_DLV_OK) {
00416 _dwarf_p_error(NULL, error, compose_error_type);
00417 return (DW_DLV_ERROR);
00418 }
00419 res = libdwarf_compose_complete(dbg, &compose_error_type);
00420 if (res != DW_DLV_OK) {
00421 _dwarf_p_error(NULL, error, compose_error_type);
00422 return (DW_DLV_ERROR);
00423 }
00424 res = libdwarf_compose_complete(dbg,
00425 &compose_error_type);
00426 if (res != DW_DLV_OK) {
00427 _dwarf_p_error(NULL, error, compose_error_type);
00428 return(DW_DLV_ERROR);
00429 }
00430 return DW_DLV_OK;
00431 }
00432
00433 int
00434 dwarf_vendor_ext(Dwarf_P_Debug dbg,
00435 Dwarf_Unsigned constant,
00436 char *string, Dwarf_Error * error)
00437 {
00438 size_t len;
00439 size_t length_est;
00440 int res;
00441 int compose_error_type;
00442
00443 if (dbg == NULL) {
00444 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
00445 return (DW_DLV_ERROR);
00446 }
00447 if (string == 0) {
00448 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_NULL);
00449 return (DW_DLV_ERROR);
00450 }
00451 #ifdef TARG_IA64
00452 len = strlen(string) + 1;
00453 #else
00454 len = strlen(string);
00455 #endif
00456 if (len == 0) {
00457 _dwarf_p_error(NULL, error, DW_DLE_MACINFO_STRING_EMPTY);
00458 return (DW_DLV_ERROR);
00459 }
00460 #ifdef TARG_IA64
00461 length_est = COMMAND_LEN + LINE_LEN + len;
00462 #else
00463 length_est = COMMAND_LEN + LINE_LEN + len + 1;
00464 #endif
00465 res = libdwarf_compose_begin(dbg, DW_MACINFO_vendor_ext, length_est,
00466 &compose_error_type);
00467 if (res != DW_DLV_OK) {
00468 _dwarf_p_error(NULL, error, compose_error_type);
00469 return (DW_DLV_ERROR);
00470 }
00471 res = libdwarf_compose_add_line(dbg, constant, &compose_error_type);
00472 if (res != DW_DLV_OK) {
00473 _dwarf_p_error(NULL, error, compose_error_type);
00474 return (DW_DLV_ERROR);
00475 }
00476 libdwarf_compose_add_string(dbg, string, len);
00477 libdwarf_compose_complete(dbg, &compose_error_type);
00478 if (res != DW_DLV_OK) {
00479 _dwarf_p_error(NULL, error, compose_error_type);
00480 return (DW_DLV_ERROR);
00481 }
00482 return DW_DLV_OK;
00483 }
00484
00485
00486
00487 int
00488 _dwarf_pro_transform_macro_info_to_disk(Dwarf_P_Debug dbg,
00489 Dwarf_Error * error)
00490 {
00491
00492 Dwarf_Unsigned mac_num_bytes;
00493
00494
00495 Dwarf_Small *macinfo;
00496
00497
00498 Dwarf_Small *macinfo_ptr;
00499
00500
00501
00502 struct dw_macinfo_block_s *m_prev;
00503 struct dw_macinfo_block_s *m_sect;
00504
00505
00506
00507 mac_num_bytes = 0;
00508 for (m_sect = dbg->de_first_macinfo; m_sect != NULL;
00509 m_sect = m_sect->mb_next) {
00510 mac_num_bytes += m_sect->mb_used_len;
00511 }
00512
00513
00514 mac_num_bytes += 1;
00515
00516 GET_CHUNK(dbg, dbg->de_elf_sects[DEBUG_MACINFO],
00517 macinfo, (unsigned long) mac_num_bytes, error);
00518 if (macinfo == NULL) {
00519 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
00520 return (0);
00521 }
00522
00523 macinfo_ptr = macinfo;
00524 m_prev = 0;
00525 for (m_sect = dbg->de_first_macinfo; m_sect != NULL;
00526 m_sect = m_sect->mb_next) {
00527 memcpy(macinfo_ptr, m_sect->mb_data, m_sect->mb_used_len);
00528 macinfo_ptr += m_sect->mb_used_len;
00529 if (m_prev) {
00530 _dwarf_p_dealloc(dbg, (Dwarf_Small *) m_prev);
00531 m_prev = 0;
00532 }
00533 m_prev = m_sect;
00534 }
00535 *macinfo_ptr = 0;
00536 if (m_prev) {
00537 _dwarf_p_dealloc(dbg, (Dwarf_Small *) m_prev);
00538 m_prev = 0;
00539 }
00540
00541 return (int) dbg->de_n_debug_sect;
00542 }