| 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 */ |
| 33 |
#include <string.h> /* memcpy() strcpy() 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_line.h" |
| 44 |
|
| 45 |
static int _dwarf_pro_add_line_entry(Dwarf_P_Debug, |
| 46 |
Dwarf_Unsigned file_index, |
| 47 |
Dwarf_Addr code_address, |
| 48 |
Dwarf_Unsigned symidx, |
| 49 |
Dwarf_Unsigned line_no, |
| 50 |
Dwarf_Signed col_no, |
| 51 |
Dwarf_Bool is_stmt_begin, |
| 52 |
Dwarf_Bool is_bb_begin, |
| 53 |
Dwarf_Ubyte opc, |
| 54 |
Dwarf_Bool isepilbeg, |
| 55 |
Dwarf_Bool isprolend, |
| 56 |
Dwarf_Unsigned isa, |
| 57 |
Dwarf_Unsigned discriminator, |
| 58 |
Dwarf_Error * error); |
| 59 |
|
| 60 |
int |
| 61 |
dwarf_add_line_entry_c(Dwarf_P_Debug dbg, |
| 62 |
Dwarf_Unsigned file_index, |
| 63 |
Dwarf_Addr code_address, |
| 64 |
Dwarf_Unsigned line_no, |
| 65 |
Dwarf_Signed col_no, |
| 66 |
Dwarf_Bool is_stmt_begin, |
| 67 |
Dwarf_Bool is_bb_begin, |
| 68 |
Dwarf_Bool isepilbeg, |
| 69 |
Dwarf_Bool isprolend, |
| 70 |
Dwarf_Unsigned isa, |
| 71 |
Dwarf_Unsigned discriminator, |
| 72 |
Dwarf_Error * error) |
| 73 |
{ |
| 74 |
int retval = 0; |
| 75 |
Dwarf_Ubyte opc = 0; |
| 76 |
Dwarf_Unsigned symidx = 0; |
| 77 |
|
| 78 |
retval = _dwarf_pro_add_line_entry(dbg, file_index, code_address, |
| 79 |
symidx, |
| 80 |
line_no, col_no, is_stmt_begin, |
| 81 |
is_bb_begin, |
| 82 |
opc, |
| 83 |
isepilbeg,isprolend,isa,discriminator, error); |
| 84 |
return retval; |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
_dwarf_init_default_line_header_vals(Dwarf_P_Debug dbg) |
| 89 |
{ |
| 90 |
dbg->de_line_inits.pi_linetable_version = dbg->de_output_version; |
| 91 |
dbg->de_line_inits.pi_default_is_stmt = |
| 92 |
/* is FALSE pro_line.h */ |
| 93 |
DEFAULT_IS_STMT; |
| 94 |
dbg->de_line_inits.pi_minimum_instruction_length = |
| 95 |
/* is 1 or 4 depending on ifdefs in pro_line.h */ |
| 96 |
MIN_INST_LENGTH; |
| 97 |
dbg->de_line_inits.pi_maximum_operations_per_instruction = |
| 98 |
/* Assuming the instruction set is not VLIW, |
| 99 |
used in the line table */ |
| 100 |
1; |
| 101 |
dbg->de_line_inits.pi_opcode_base = |
| 102 |
/* is 10 in pro_line.h but should be 13 in DWARF3 |
| 103 |
and later. */ |
| 104 |
OPCODE_BASE; |
| 105 |
dbg->de_line_inits.pi_line_base = |
| 106 |
/* is -1 in pro_line.h */ |
| 107 |
LINE_BASE; |
| 108 |
dbg->de_line_inits.pi_line_range = |
| 109 |
/* is 4 in pro_line.h */ |
| 110 |
LINE_RANGE; |
| 111 |
|
| 112 |
/* Applies to line table and everywhere else |
| 113 |
for a CU. */ |
| 114 |
dbg->de_line_inits.pi_address_size = dbg->de_pointer_size; |
| 115 |
|
| 116 |
/* Assuming no segments. */ |
| 117 |
dbg->de_line_inits.pi_segment_selector_size = 0; |
| 118 |
dbg->de_line_inits.pi_segment_size = 0; |
| 119 |
} |
| 120 |
|
| 121 |
/* Ask to emit DW_LNE_set_address opcode explicitly. Used by be |
| 122 |
to emit start of a new .text section, or to force a relocated |
| 123 |
address into debug line information entry. */ |
| 124 |
int |
| 125 |
dwarf_lne_set_address_a(Dwarf_P_Debug dbg, |
| 126 |
Dwarf_Addr offs, |
| 127 |
Dwarf_Unsigned symidx, Dwarf_Error * error) |
| 128 |
{ |
| 129 |
int retval = 0; |
| 130 |
Dwarf_Ubyte opc = 0; |
| 131 |
Dwarf_Unsigned file_index = 0; |
| 132 |
Dwarf_Unsigned line_no = 0; |
| 133 |
Dwarf_Signed col_no = 0; |
| 134 |
Dwarf_Bool is_stmt = 0; |
| 135 |
Dwarf_Bool is_bb = 0; |
| 136 |
Dwarf_Bool isepilbeg = 0; |
| 137 |
Dwarf_Bool isprolend = 0; |
| 138 |
Dwarf_Unsigned isa = 0; |
| 139 |
Dwarf_Unsigned discriminator = 0; |
| 140 |
|
| 141 |
opc = DW_LNE_set_address; |
| 142 |
retval = _dwarf_pro_add_line_entry(dbg, file_index, offs, |
| 143 |
symidx, |
| 144 |
line_no, col_no, is_stmt, |
| 145 |
is_bb, |
| 146 |
opc, |
| 147 |
isepilbeg, isprolend, isa, discriminator, |
| 148 |
error); |
| 149 |
return retval; |
| 150 |
} |
| 151 |
|
| 152 |
/* Ask to emit end_seqence opcode. Used normally at the end of a |
| 153 |
compilation unit. Can also be used in the middle if there |
| 154 |
are gaps in the region described by the code address. */ |
| 155 |
int |
| 156 |
dwarf_lne_end_sequence_a(Dwarf_P_Debug dbg, |
| 157 |
Dwarf_Addr end_address, Dwarf_Error * error) |
| 158 |
{ |
| 159 |
Dwarf_Ubyte opc = 0; |
| 160 |
int retval = 0; |
| 161 |
Dwarf_Unsigned file_index = 0; |
| 162 |
Dwarf_Unsigned symidx = 0; |
| 163 |
Dwarf_Unsigned line_no = 0; |
| 164 |
Dwarf_Bool is_stmt = 0; |
| 165 |
Dwarf_Bool is_bb = 0; |
| 166 |
Dwarf_Signed col_no = 0;/* Wrong, should be unsigned. */ |
| 167 |
Dwarf_Bool isepilbeg = 0; |
| 168 |
Dwarf_Bool isprolend = 0; |
| 169 |
Dwarf_Unsigned isa = 0; |
| 170 |
Dwarf_Unsigned discriminator = 0; |
| 171 |
|
| 172 |
opc = DW_LNE_end_sequence; |
| 173 |
retval = _dwarf_pro_add_line_entry(dbg, file_index, end_address, |
| 174 |
symidx, |
| 175 |
line_no, col_no, is_stmt, |
| 176 |
is_bb, |
| 177 |
opc, |
| 178 |
isepilbeg, isprolend, isa, discriminator, |
| 179 |
error); |
| 180 |
return retval; |
| 181 |
} |
| 182 |
|
| 183 |
/* As of December 2018 this returns DW_DLV_OK, DW_DLV_ERROR */ |
| 184 |
/* Add an entry in the internal list of lines maintained by producer. |
| 185 |
Opc indicates if an opcode needs to be generated, rather than just |
| 186 |
an entry in the matrix. During opcodes generation time, these |
| 187 |
opcodes will be used. */ |
| 188 |
static int |
| 189 |
_dwarf_pro_add_line_entry(Dwarf_P_Debug dbg, |
| 190 |
Dwarf_Unsigned file_index, |
| 191 |
Dwarf_Addr code_address, |
| 192 |
Dwarf_Unsigned symidx, |
| 193 |
Dwarf_Unsigned line_no, |
| 194 |
Dwarf_Signed col_no, |
| 195 |
Dwarf_Bool is_stmt_begin, |
| 196 |
Dwarf_Bool is_bb_begin, |
| 197 |
Dwarf_Ubyte opc, |
| 198 |
Dwarf_Bool isepilbeg, |
| 199 |
Dwarf_Bool isprolend, |
| 200 |
Dwarf_Unsigned isa, |
| 201 |
Dwarf_Unsigned discriminator, |
| 202 |
Dwarf_Error * error) |
| 203 |
{ |
| 204 |
if (dbg->de_lines == NULL) { |
| 205 |
dbg->de_lines = (Dwarf_P_Line) |
| 206 |
_dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Line_s)); |
| 207 |
if (dbg->de_lines == NULL) { |
| 208 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_LINE_ALLOC, DW_DLV_ERROR); |
| 209 |
} |
| 210 |
dbg->de_last_line = dbg->de_lines; |
| 211 |
_dwarf_pro_reg_init(dbg,dbg->de_lines); |
| 212 |
|
| 213 |
} else { |
| 214 |
dbg->de_last_line->dpl_next = (Dwarf_P_Line) |
| 215 |
_dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Line_s)); |
| 216 |
if (dbg->de_last_line->dpl_next == NULL) { |
| 217 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_LINE_ALLOC, DW_DLV_ERROR); |
| 218 |
} |
| 219 |
dbg->de_last_line = dbg->de_last_line->dpl_next; |
| 220 |
_dwarf_pro_reg_init(dbg,dbg->de_last_line); |
| 221 |
} |
| 222 |
dbg->de_last_line->dpl_address = code_address; |
| 223 |
dbg->de_last_line->dpl_file = (unsigned long) file_index; |
| 224 |
dbg->de_last_line->dpl_line = (unsigned long) line_no; |
| 225 |
dbg->de_last_line->dpl_column = (unsigned long) col_no; |
| 226 |
dbg->de_last_line->dpl_is_stmt = is_stmt_begin; |
| 227 |
dbg->de_last_line->dpl_basic_block = is_bb_begin; |
| 228 |
dbg->de_last_line->dpl_opc = opc; |
| 229 |
dbg->de_last_line->dpl_r_symidx = symidx; |
| 230 |
dbg->de_last_line->dpl_prologue_end = isprolend; |
| 231 |
dbg->de_last_line->dpl_epilogue_begin = isepilbeg; |
| 232 |
dbg->de_last_line->dpl_isa = isa; |
| 233 |
dbg->de_last_line->dpl_discriminator = discriminator; |
| 234 |
return DW_DLV_OK; |
| 235 |
} |
| 236 |
|
| 237 |
/* Add a directory declaration to the debug_line section. Stored |
| 238 |
in linked list. */ |
| 239 |
int |
| 240 |
dwarf_add_directory_decl_a(Dwarf_P_Debug dbg, |
| 241 |
char *name, |
| 242 |
Dwarf_Unsigned *index_in_directories, |
| 243 |
Dwarf_Error * error) |
| 244 |
{ |
| 245 |
if (dbg->de_inc_dirs == NULL) { |
| 246 |
dbg->de_inc_dirs = (Dwarf_P_F_Entry) |
| 247 |
_dwarf_p_get_alloc(dbg, |
| 248 |
sizeof(struct Dwarf_P_F_Entry_s)); |
| 249 |
if (dbg->de_inc_dirs == NULL) { |
| 250 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_INCDIR_ALLOC, |
| 251 |
DW_DLV_ERROR); |
| 252 |
} |
| 253 |
dbg->de_last_inc_dir = dbg->de_inc_dirs; |
| 254 |
dbg->de_n_inc_dirs = 1; |
| 255 |
} else { |
| 256 |
dbg->de_last_inc_dir->dfe_next = (Dwarf_P_F_Entry) |
| 257 |
_dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_F_Entry_s)); |
| 258 |
if (dbg->de_last_inc_dir->dfe_next == NULL) { |
| 259 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_INCDIR_ALLOC, |
| 260 |
DW_DLV_ERROR); |
| 261 |
} |
| 262 |
dbg->de_last_inc_dir = dbg->de_last_inc_dir->dfe_next; |
| 263 |
dbg->de_n_inc_dirs++; |
| 264 |
} |
| 265 |
dbg->de_last_inc_dir->dfe_name = |
| 266 |
(char *) _dwarf_p_get_alloc(dbg, strlen(name) + 1); |
| 267 |
if (dbg->de_last_inc_dir->dfe_name == NULL) { |
| 268 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_STRING_ALLOC, DW_DLV_ERROR); |
| 269 |
} |
| 270 |
strcpy(dbg->de_last_inc_dir->dfe_name, name); |
| 271 |
dbg->de_last_inc_dir->dfe_next = NULL; |
| 272 |
|
| 273 |
*index_in_directories = dbg->de_n_inc_dirs; |
| 274 |
return DW_DLV_OK; |
| 275 |
} |
| 276 |
|
| 277 |
int |
| 278 |
dwarf_add_file_decl_a(Dwarf_P_Debug dbg, |
| 279 |
char *name, |
| 280 |
Dwarf_Unsigned dir_idx, |
| 281 |
Dwarf_Unsigned time_mod, |
| 282 |
Dwarf_Unsigned length, |
| 283 |
Dwarf_Unsigned *file_entry_count_out, |
| 284 |
Dwarf_Error * error) |
| 285 |
{ |
| 286 |
Dwarf_P_F_Entry cur; |
| 287 |
char *ptr = 0; |
| 288 |
int nbytes_idx, nbytes_time, nbytes_len; |
| 289 |
char buffidx[ENCODE_SPACE_NEEDED]; |
| 290 |
char bufftime[ENCODE_SPACE_NEEDED]; |
| 291 |
char bufflen[ENCODE_SPACE_NEEDED]; |
| 292 |
int res = 0; |
| 293 |
|
| 294 |
if (dbg->de_file_entries == NULL) { |
| 295 |
dbg->de_file_entries = (Dwarf_P_F_Entry) |
| 296 |
_dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_F_Entry_s)); |
| 297 |
if (dbg->de_file_entries == NULL) { |
| 298 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_FILE_ENTRY_ALLOC, |
| 299 |
DW_DLV_ERROR); |
| 300 |
} |
| 301 |
cur = dbg->de_file_entries; |
| 302 |
dbg->de_last_file_entry = cur; |
| 303 |
dbg->de_n_file_entries = 1; |
| 304 |
} else { |
| 305 |
cur = dbg->de_last_file_entry; |
| 306 |
cur->dfe_next = (Dwarf_P_F_Entry) |
| 307 |
_dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_F_Entry_s)); |
| 308 |
if (cur->dfe_next == NULL) { |
| 309 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_FILE_ENTRY_ALLOC, |
| 310 |
DW_DLV_ERROR); |
| 311 |
} |
| 312 |
cur = cur->dfe_next; |
| 313 |
dbg->de_last_file_entry = cur; |
| 314 |
dbg->de_n_file_entries++; |
| 315 |
} |
| 316 |
cur->dfe_name = (char *) _dwarf_p_get_alloc(dbg, |
| 317 |
strlen(name) + 1); |
| 318 |
if (cur->dfe_name == NULL) { |
| 319 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_ALLOC_FAIL, DW_DLV_ERROR); |
| 320 |
} |
| 321 |
strcpy((char *) cur->dfe_name, name); |
| 322 |
res = dwarf_encode_leb128(dir_idx, &nbytes_idx, |
| 323 |
buffidx, sizeof(buffidx)); |
| 324 |
if (res != DW_DLV_OK) { |
| 325 |
/* DW_DLV_NO_ENTRY impossible */ |
| 326 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR); |
| 327 |
} |
| 328 |
res = dwarf_encode_leb128(time_mod, &nbytes_time, |
| 329 |
bufftime, sizeof(bufftime)); |
| 330 |
if (res != DW_DLV_OK) { |
| 331 |
/* DW_DLV_NO_ENTRY impossible */ |
| 332 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR); |
| 333 |
} |
| 334 |
res = dwarf_encode_leb128(length, &nbytes_len, |
| 335 |
bufflen, sizeof(bufflen)); |
| 336 |
if (res != DW_DLV_OK) { |
| 337 |
/* DW_DLV_NO_ENTRY impossible */ |
| 338 |
DWARF_P_DBG_ERROR(dbg,DW_DLE_LEB_OUT_ERROR,DW_DLV_ERROR); |
| 339 |
} |
| 340 |
cur->dfe_args = (char *) |
| 341 |
_dwarf_p_get_alloc(dbg, nbytes_idx + |
| 342 |
nbytes_time + nbytes_len); |
| 343 |
if (cur->dfe_args == NULL) { |
| 344 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_ALLOC_FAIL, DW_DLV_ERROR); |
| 345 |
} |
| 346 |
ptr = cur->dfe_args; |
| 347 |
memcpy((void *) ptr, buffidx, nbytes_idx); |
| 348 |
ptr += nbytes_idx; |
| 349 |
memcpy((void *) ptr, bufftime, nbytes_time); |
| 350 |
ptr += nbytes_time; |
| 351 |
memcpy((void *) ptr, bufflen, nbytes_len); |
| 352 |
cur->dfe_nbytes = nbytes_idx + nbytes_time + nbytes_len; |
| 353 |
cur->dfe_next = NULL; |
| 354 |
*file_entry_count_out = dbg->de_n_file_entries; |
| 355 |
return DW_DLV_OK; |
| 356 |
} |
| 357 |
|
| 358 |
/* Initialize a row of the matrix for line numbers, meaning |
| 359 |
initialize the struct corresponding to it */ |
| 360 |
void |
| 361 |
_dwarf_pro_reg_init(Dwarf_P_Debug dbg, Dwarf_P_Line cur_line) |
| 362 |
{ |
| 363 |
cur_line->dpl_address = 0; |
| 364 |
cur_line->dpl_file = 1; |
| 365 |
cur_line->dpl_line = 1; |
| 366 |
cur_line->dpl_column = 0; |
| 367 |
cur_line->dpl_is_stmt = dbg->de_line_inits.pi_default_is_stmt; |
| 368 |
cur_line->dpl_basic_block = FALSE; |
| 369 |
cur_line->dpl_next = NULL; |
| 370 |
cur_line->dpl_prologue_end = 0; |
| 371 |
cur_line->dpl_epilogue_begin = 0; |
| 372 |
cur_line->dpl_isa = 0; |
| 373 |
cur_line->dpl_discriminator = 0; |
| 374 |
cur_line->dpl_opc = 0; |
| 375 |
} |