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