1 /* 2 3 Copyright (C) 2000,2002,2004 Silicon Graphics, Inc. All Rights Reserved. 4 Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved. 5 Portions Copyright 2011-2017 David Anderson. All Rights Reserved. 6 7 This program is free software; you can redistribute it and/or modify it 8 under the terms of version 2.1 of the GNU Lesser General Public License 9 as published by the Free Software Foundation. 10 11 This program is distributed in the hope that it would be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 14 15 Further, this software is distributed without any warranty that it is 16 free of the rightful claim of any third person regarding infringement 17 or the like. Any license provided herein, whether implied or 18 otherwise, applies only to this software file. Patent licenses, if 19 any, provided herein do not apply to combinations of this program with 20 other software, or any other product whatsoever. 21 22 You should have received a copy of the GNU Lesser General Public 23 License along with this program; if not, write the Free Software 24 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301, 25 USA. 26 27 */ 28 29 #include "libdwarfdefs.h" 30 31 #define true 1 32 #define false 0 33 34 /* The DISTINGUISHED VALUE is 4 byte value defined by DWARF 35 since DWARF3. */ 36 #define DISTINGUISHED_VALUE_ARRAY(x) unsigned char x[4] = { 0xff,0xff,0xff,0xff } 37 #define DISTINGUISHED_VALUE 0xffffffff /* 64bit extension flag */ 38 39 /* to identify a cie */ 40 #define DW_CIE_ID ~(0x0) 41 #define DW_CIE_VERSION 1 42 43 typedef signed char Dwarf_Sbyte; 44 typedef unsigned char Dwarf_Ubyte; 45 typedef signed short Dwarf_Shalf; 46 47 /* 48 On any change that makes libdwarf producer 49 incompatible, increment this number. 50 1->2->3 ... */ 51 #define PRO_VERSION_MAGIC 0xdead1 52 53 54 #define DWARF_HALF_SIZE 2 55 #define DWARF_32BIT_SIZE 4 56 #define DWARF_64BIT_SIZE 8 57 58 /* 59 producer: 60 This struct is used to hold information about all 61 debug* sections. On creating a new section, section 62 names and indices are added to this struct 63 definition in pro_section.h */ 64 typedef struct Dwarf_P_Section_Data_s *Dwarf_P_Section_Data; 65 66 /* 67 producer: 68 This struct holds file entries or 69 include file entries for the statement prologue. 70 Defined in pro_line.h 71 */ 72 typedef struct Dwarf_P_F_Entry_s *Dwarf_P_F_Entry; 73 74 /* 75 producer: 76 This struct holds information for each cie. Defn in pro_frame.h 77 */ 78 typedef struct Dwarf_P_Cie_s *Dwarf_P_Cie; 79 80 /* 81 producer: 82 Struct to hold line number information, different from 83 Dwarf_Line opaque type. 84 */ 85 typedef struct Dwarf_P_Line_s *Dwarf_P_Line; 86 87 /* 88 producer: 89 Struct to hold information about address ranges. 90 */ 91 typedef struct Dwarf_P_Simple_nameentry_s *Dwarf_P_Simple_nameentry; 92 typedef struct Dwarf_P_Simple_name_header_s *Dwarf_P_Simple_name_header; 93 typedef struct Dwarf_P_Arange_s *Dwarf_P_Arange; 94 typedef struct Dwarf_P_Per_Reloc_Sect_s *Dwarf_P_Per_Reloc_Sect; 95 typedef struct Dwarf_P_Per_Sect_String_Attrs_s *Dwarf_P_Per_Sect_String_Attrs; 96 typedef struct Dwarf_P_Dnames_s *Dwarf_P_Dnames; 97 98 /* Defined to get at the elf section numbers and section name 99 indices in symtab for the dwarf sections 100 Must match .rel.* names in _dwarf_rel_section_names 101 exactly. 102 */ 103 #define DEBUG_INFO 0 104 #define DEBUG_LINE 1 105 #define DEBUG_ABBREV 2 106 #define DEBUG_FRAME 3 107 #define DEBUG_ARANGES 4 108 #define DEBUG_PUBNAMES 5 109 #define DEBUG_FUNCNAMES 6 110 #define DEBUG_TYPENAMES 7 111 #define DEBUG_VARNAMES 8 112 #define DEBUG_WEAKNAMES 9 113 #define DEBUG_MACINFO 10 /* DWARF 2,3,4 only */ 114 #define DEBUG_LOC 11 115 #define DEBUG_RANGES 12 116 #define DEBUG_TYPES 13 117 #define DEBUG_PUBTYPES 14 118 #define DEBUG_NAMES 15 /* DWARF5. aka dnames */ 119 #define DEBUG_STR 16 120 #define DEBUG_LINE_STR 17 121 #define DEBUG_MACRO 18 /* DWARF 5. */ 122 #define DEBUG_LOCLISTS 19 /* DWARF 5. */ 123 #define DEBUG_RNGLISTS 20 /* DWARF 5. */ 124 125 /* Maximum number of debug_* sections not including the relocations */ 126 #define NUM_DEBUG_SECTIONS 21 127 128 /* The FORM codes available are defined in DWARF5 129 on page 158, DW_LNCT_path */ 130 struct Dwarf_P_Line_format_s { 131 /* DW_LNCT_path etc. */ 132 unsigned def_content_type; 133 134 /* DW_FORM_string or DW_FORM_strp or DW_FORM_strp 135 or DW_FORM_strp_sup or for dwo, some others. */ 136 unsigned def_form_code; 137 }; 138 139 #define DW_LINE_FORMATS_MAX 6 140 /* Describes the data needed to generate line table header info 141 so we can vary the init at runtime. */ 142 struct Dwarf_P_Line_Inits_s { 143 unsigned pi_linetable_version; /* line table version number */ 144 unsigned pi_default_is_stmt; /* default value for is_stmt */ 145 146 /* Size of the smallest instruction, in bytes. */ 147 unsigned pi_minimum_instruction_length; 148 149 /* Normally opcode_base is determined by pi_version, but we 150 allow manual setting here so we can generate data like 151 GNU with a DWARF3 opcode base in a DWARF2 section. 152 This determines how much of the header_opcode_lengths 153 table is emitted in the line table header */ 154 unsigned pi_opcode_base; 155 156 int pi_line_base; /* For line table header. */ 157 int pi_line_range; /* For line table header. */ 158 159 /* Make this >1 for VLIW machines. DWARF4,DWARF5 */ 160 unsigned pi_maximum_operations_per_instruction; 161 162 /* DWARF 5 */ 163 unsigned pi_segment_selector_size; 164 unsigned pi_address_size; 165 unsigned pi_segment_size; 166 unsigned pi_directory_entry_format_count; 167 struct Dwarf_P_Line_format_s pi_incformats[DW_LINE_FORMATS_MAX]; 168 169 unsigned pi_file_entry_format_count; 170 struct Dwarf_P_Line_format_s pi_fileformats[DW_LINE_FORMATS_MAX]; 171 }; 172 173 174 struct Dwarf_P_Die_s { 175 Dwarf_Unsigned di_offset; /* offset in debug info */ 176 char *di_abbrev; /* abbreviation */ 177 Dwarf_Unsigned di_abbrev_nbytes; /* # of bytes in abbrev */ 178 Dwarf_Tag di_tag; 179 Dwarf_P_Die di_parent; /* parent of current die */ 180 Dwarf_P_Die di_child; /* first child */ 181 /* The last child field makes linking up children an O(1) operation, 182 See pro_die.c. */ 183 Dwarf_P_Die di_last_child; 184 Dwarf_P_Die di_left; /* left sibling */ 185 Dwarf_P_Die di_right; /* right sibling */ 186 Dwarf_P_Attribute di_attrs; /* list of attributes */ 187 Dwarf_P_Attribute di_last_attr; /* last attribute */ 188 int di_n_attr; /* number of attributes */ 189 Dwarf_P_Debug di_dbg; /* For memory management */ 190 Dwarf_Unsigned di_marker; /* used to attach symbols to dies */ 191 }; 192 193 194 /* producer fields */ 195 struct Dwarf_P_Attribute_s { 196 Dwarf_Half ar_attribute; /* Attribute Value. */ 197 Dwarf_Half ar_attribute_form; /* Attribute Form. */ 198 Dwarf_P_Die ar_ref_die; /* die pointer if form ref */ 199 char *ar_data; /* data, format given by form */ 200 Dwarf_Unsigned ar_nbytes; /* no. of bytes of data */ 201 Dwarf_Unsigned ar_rel_symidx; /* when attribute has a 202 relocatable value, holds 203 index of symbol in SYMTAB */ 204 Dwarf_Unsigned ar_debug_str_offset; /* Offset in .debug_str 205 if non-zero. Zero offset never assigned a string. */ 206 Dwarf_Ubyte ar_rel_type; /* relocation type */ 207 Dwarf_Unsigned ar_rel_offset; /* Offset of relocation within block */ 208 char ar_reloc_len; /* Number of bytes that relocation 209 applies to. 4 or 8. Unused and may 210 be 0 if if ar_rel_type is 211 R_MIPS_NONE */ 212 Dwarf_P_Attribute ar_next; 213 /* set if form = DW_FORM_implicit_const; */ 214 Dwarf_Signed ar_implicit_const; 215 }; 216 217 /* A block of .debug_macinfo data: this forms a series of blocks. 218 ** Each macinfo input is compressed immediately and put into 219 ** the current block if room, else a newblock allocated. 220 ** The space allocation is such that the block and the macinfo 221 ** data are one malloc block: free with a pointer to this and the 222 ** mb_data is freed automatically. 223 ** Like the struct hack, but legal ANSI C. 224 */ 225 struct dw_macinfo_block_s { 226 struct dw_macinfo_block_s *mb_next; 227 unsigned long mb_avail_len; 228 unsigned long mb_used_len; 229 unsigned long mb_macinfo_data_space_len; 230 char *mb_data;/* original malloc ptr. */ 231 }; 232 233 /* dwarf_sn_kind is for the array of similarly-treated 234 name -> cu ties 235 */ 236 enum dwarf_sn_kind { dwarf_snk_pubname, /* .debug_pubnames */ 237 dwarf_snk_funcname, /* SGI extension. */ 238 dwarf_snk_weakname, /* SGI extension. */ 239 dwarf_snk_typename, /* SGI extension. */ 240 dwarf_snk_varname, /* SGI extension. */ 241 dwarf_snk_pubtype, /* .debug_pubtypes */ 242 dwarf_snk_entrycount /* this one must be last */ 243 }; 244 245 246 247 /* The calls to add a varname etc use a list of 248 these as the list. 249 */ 250 struct Dwarf_P_Simple_nameentry_s { 251 Dwarf_P_Die sne_die; 252 char *sne_name; 253 int sne_name_len; 254 Dwarf_P_Simple_nameentry sne_next; 255 }; 256 257 /* An array of these, each of which heads a list 258 of Dwarf_P_Simple_nameentry 259 */ 260 struct Dwarf_P_Simple_name_header_s { 261 Dwarf_P_Simple_nameentry sn_head; 262 Dwarf_P_Simple_nameentry sn_tail; 263 Dwarf_Signed sn_count; 264 265 /* Length that will be generated, not counting fixed header or 266 trailer */ 267 Dwarf_Signed sn_net_len; 268 }; 269 typedef int (*_dwarf_pro_reloc_name_func_ptr) (Dwarf_P_Debug dbg, 270 int sec_index, 271 Dwarf_Unsigned offset,/* r_offset */ 272 Dwarf_Unsigned symidx, 273 enum Dwarf_Rel_Type type, 274 int reltarget_length); 275 276 typedef int (*_dwarf_pro_reloc_length_func_ptr) (Dwarf_P_Debug dbg, 277 int sec_index, Dwarf_Unsigned offset,/* r_offset */ 278 Dwarf_Unsigned start_symidx, 279 Dwarf_Unsigned end_symidx, 280 enum Dwarf_Rel_Type type, 281 int reltarget_length); 282 typedef int (*_dwarf_pro_transform_relocs_func_ptr) (Dwarf_P_Debug dbg, 283 Dwarf_Signed * 284 new_sec_count); 285 286 /* 287 Each slot in a block of slots could be: 288 a binary stream relocation entry (32 or 64bit relocation data) 289 a SYMBOLIC relocation entry. 290 During creation sometimes we create multiple chained blocks, 291 but sometimes we create a single long block. 292 Before returning reloc data to caller, 293 we switch to a single, long-enough, 294 block. 295 296 We make counters here Dwarf_Unsigned so that we 297 get sufficient alignment. Since we use space after 298 the struct (at malloc time) for user data which 299 must have Dwarf_Unsigned alignment, this 300 struct must have that alignment too. 301 */ 302 struct Dwarf_P_Relocation_Block_s { 303 Dwarf_Unsigned rb_slots_in_block; /* slots in block, as created */ 304 Dwarf_Unsigned rb_next_slot_to_use; /* counter, start at 0. */ 305 struct Dwarf_P_Relocation_Block_s *rb_next; 306 char *rb_where_to_add_next; /* pointer to next slot (might be past 307 end, depending on 308 rb_next_slot_to_use) */ 309 char *rb_data; /* data area */ 310 }; 311 312 /* One of these per potential relocation section 313 So one per actual dwarf section. 314 Left zeroed when not used (some sections have 315 no relocations). 316 */ 317 struct Dwarf_P_Per_Reloc_Sect_s { 318 unsigned long pr_reloc_total_count; /* total number of entries 319 across all blocks */ 320 321 unsigned long pr_slots_per_block_to_alloc; /* at Block alloc, this 322 is the default number of slots to use */ 323 324 int pr_sect_num_of_reloc_sect; /* sect number returned by 325 de_callback_func() or de_callback_func_b() or_c() 326 call, this is the sect 327 number of the relocation section. */ 328 329 /* singly-linked list. add at and ('last') with count of blocks */ 330 struct Dwarf_P_Relocation_Block_s *pr_first_block; 331 struct Dwarf_P_Relocation_Block_s *pr_last_block; 332 unsigned long pr_block_count; 333 }; 334 335 #define DEFAULT_SLOTS_PER_BLOCK 3 336 337 typedef struct memory_list_s { 338 struct memory_list_s *prev; 339 struct memory_list_s *next; 340 } memory_list_t; 341 342 struct Dwarf_P_Per_Sect_String_Attrs_s { 343 int sect_sa_section_number; 344 unsigned sect_sa_n_alloc; 345 unsigned sect_sa_n_used; 346 Dwarf_P_String_Attr sect_sa_list; 347 }; 348 349 struct Dwarf_P_debug_str_entry_s { 350 Dwarf_P_Debug dse_dbg; 351 /* Name used initially with tfind. */ 352 char *dse_name; 353 354 Dwarf_Unsigned dse_slen; /* includes space for NUL terminator */ 355 356 /* See dse_has_table_offset below. */ 357 Dwarf_Unsigned dse_table_offset; 358 359 /* For tsearch a hash table exists and we have a table offset. 360 dse_dbg->de_debug_str->ds_data + dse_table_offset 361 points to the string iff dse_has_table_offset != 0. */ 362 unsigned char dse_has_table_offset; 363 }; 364 365 struct Dwarf_P_Str_stats_s { 366 Dwarf_Unsigned ps_strp_count_debug_str; 367 Dwarf_Unsigned ps_strp_len_debug_str; 368 Dwarf_Unsigned ps_strp_len_debug_line_str; 369 Dwarf_Unsigned ps_strp_reused_count; 370 Dwarf_Unsigned ps_strp_reused_len; 371 }; 372 373 struct Dwarf_P_Stats_s { 374 Dwarf_Unsigned ps_str_count; 375 Dwarf_Unsigned ps_str_total_length; 376 struct Dwarf_P_Str_stats_s ps_strp; 377 struct Dwarf_P_Str_stats_s ps_line_strp; 378 }; 379 380 /* Fields used by producer */ 381 struct Dwarf_P_Debug_s { 382 /* Used to catch dso passing dbg to another DSO with incompatible 383 version of libdwarf See PRO_VERSION_MAGIC */ 384 int de_version_magic_number; 385 386 Dwarf_Handler de_errhand; 387 /* de_user_data is provided so users can use it to readily tie 388 a callback to anything they desire. The contents are not 389 used by libdwarf except to pass the data as a callback 390 argument. New in June 2011. Available in 391 dwarf_pro_init_c() and its callback function. */ 392 void * de_user_data; 393 Dwarf_Ptr de_errarg; 394 395 /* Call back function, used to create .debug* sections. Provided 396 By user. */ 397 Dwarf_Callback_Func de_callback_func; 398 399 /* Flags from producer_init call */ 400 Dwarf_Unsigned de_flags; 401 402 /* This holds information on debug info section 403 stream output, including the stream data */ 404 Dwarf_P_Section_Data de_debug_sects; 405 406 /* Defaults set as DW_FORM_string, 407 meaning not using .debug_str by default. 408 This intended for the .debug_info section. */ 409 int de_debug_default_str_form; 410 411 /* If form DW_FORM_strp */ 412 Dwarf_P_Section_Data de_debug_str; 413 void *de_debug_str_hashtab; /* for tsearch */ 414 415 /* .debug_line_str section data if form DW_FORM_line_strp */ 416 Dwarf_P_Section_Data de_debug_line_str; 417 void *de_debug_line_str_hashtab; /* for tsearch */ 418 419 /* Pointer to the 'current active' section */ 420 Dwarf_P_Section_Data de_current_active_section; 421 422 /* Number of debug data streams globs. */ 423 Dwarf_Unsigned de_n_debug_sect; 424 425 /* File entry information, null terminated singly-linked list */ 426 Dwarf_P_F_Entry de_file_entries; 427 Dwarf_P_F_Entry de_last_file_entry; 428 Dwarf_Unsigned de_n_file_entries; 429 430 /* Has the directories used to search for source files */ 431 Dwarf_P_F_Entry de_inc_dirs; 432 Dwarf_P_F_Entry de_last_inc_dir; 433 Dwarf_Unsigned de_n_inc_dirs; 434 435 /* Has all the line number info for the stmt program */ 436 Dwarf_P_Line de_lines; 437 Dwarf_P_Line de_last_line; 438 439 /* List of cie's for the debug unit */ 440 Dwarf_P_Cie de_frame_cies; 441 Dwarf_P_Cie de_last_cie; 442 Dwarf_Unsigned de_n_cie; 443 444 /* Singly-linked list of fde's for the debug unit */ 445 Dwarf_P_Fde de_frame_fdes; 446 Dwarf_P_Fde de_last_fde; 447 Dwarf_Unsigned de_n_fde; 448 449 /* First die, leads to all others */ 450 Dwarf_P_Die de_dies; 451 452 /* Pointer to chain of aranges */ 453 Dwarf_P_Arange de_arange; 454 Dwarf_P_Arange de_last_arange; 455 Dwarf_Signed de_arange_count; 456 457 /* debug_names de_dnames is base of dnames info 458 before disk form */ 459 Dwarf_P_Dnames de_dnames; 460 Dwarf_P_Section_Data de_dnames_sect; 461 462 /* macinfo controls. */ 463 /* first points to beginning of the list during creation */ 464 struct dw_macinfo_block_s *de_first_macinfo; 465 466 /* current points to the current, unfilled, block */ 467 struct dw_macinfo_block_s *de_current_macinfo; 468 469 /* Pointer to the first section, to support reset_section_bytes */ 470 Dwarf_P_Section_Data de_first_debug_sect; 471 472 /* Handles pubnames, weaknames, etc. See dwarf_sn_kind in 473 pro_opaque.h */ 474 struct Dwarf_P_Simple_name_header_s 475 de_simple_name_headers[dwarf_snk_entrycount]; 476 477 /* Relocation data. not all sections will actally have relocation 478 info, of course. de_reloc_sect, de_elf_sects, and de_sect_name_idx 479 arrays are exactly in parallel. Not every de_elf_sect has 480 any relocations for it, of course. */ 481 struct Dwarf_P_Per_Reloc_Sect_s de_reloc_sect[NUM_DEBUG_SECTIONS]; 482 int de_reloc_next_to_return; /* iterator on reloc sections 483 (SYMBOLIC output) */ 484 485 /* Used in remembering sections. See de_reloc_sect above. */ 486 int de_elf_sects[NUM_DEBUG_SECTIONS]; /* elf sect number of 487 the section itself, DEBUG_LINE for example */ 488 489 /* Section name index or handle for the name of the symbol for 490 DEBUG_LINE for example */ 491 Dwarf_Unsigned de_sect_name_idx[NUM_DEBUG_SECTIONS]; 492 493 int de_offset_reloc; /* offset reloc type, R_MIPS_32 for 494 example. Specific to the ABI being 495 produced. Relocates offset size 496 field */ 497 int de_exc_reloc; /* reloc type specific to exception 498 table relocs. */ 499 int de_ptr_reloc; /* standard reloc type, R_MIPS_32 for 500 example. Specific to the ABI being 501 produced. relocates pointer size 502 field */ 503 unsigned char de_irix_exc_augmentation; /* If non-zero means 504 that producing an IRIX exception-table offset in a CIE header 505 is allowed (depending on the augmentation string). */ 506 507 unsigned char de_dwarf_offset_size; /* dwarf offset size. */ 508 unsigned char de_elf_offset_size; /* object section offset size. */ 509 unsigned char de_pointer_size; /* size of address in target. */ 510 511 /* Added April 19, 2017. For DWARF5 */ 512 unsigned char de_segment_selector_size; 513 514 unsigned char de_relocation_record_size; /* reloc record size 515 varies by ABI and 516 relocation-output 517 method (stream or 518 symbolic) */ 519 520 unsigned char de_64bit_extension;/* non-zero if creating 64 bit 521 offsets using dwarf2-99 522 extension proposal */ 523 524 unsigned char de_output_version; /* 2,3,4, or 5. The version number 525 of the output. (not necessarily that of each section, 526 which depends on the base version). */ 527 528 /* Defaults will be mostly useless, but such do exist */ 529 unsigned de_big_endian; /* if 0 target is little-endian */ 530 531 int de_ar_data_attribute_form; /* data8, data4 abi &version dependent */ 532 int de_ar_ref_attr_form; /* ref8 ref4 , abi dependent */ 533 534 /* simple name relocations */ 535 _dwarf_pro_reloc_name_func_ptr de_relocate_by_name_symbol; 536 537 /* relocations for a length, requiring a pair of symbols */ 538 _dwarf_pro_reloc_length_func_ptr de_relocate_pair_by_symbol; 539 540 _dwarf_pro_transform_relocs_func_ptr de_transform_relocs_to_disk; 541 542 /* following used for macro buffers */ 543 unsigned long de_compose_avail; 544 unsigned long de_compose_used_len; 545 546 unsigned char de_same_endian; 547 void (*de_copy_word) (void *, const void *, unsigned long); 548 549 /* Add new fields at the END of this struct to preserve some hope 550 of sensible behavior on dbg passing between DSOs linked with 551 mismatched libdwarf producer versions. */ 552 553 Dwarf_P_Marker de_markers; /* pointer to array of markers */ 554 unsigned de_marker_n_alloc; 555 unsigned de_marker_n_used; 556 int de_sect_sa_next_to_return; /* Iterator on sring attrib sects */ 557 /* String attributes data of each section. */ 558 struct Dwarf_P_Per_Sect_String_Attrs_s de_sect_string_attr[NUM_DEBUG_SECTIONS]; 559 560 /* Hold data needed to init line output flexibly. */ 561 struct Dwarf_P_Line_Inits_s de_line_inits; 562 563 struct Dwarf_P_Stats_s de_stats; 564 }; 565 566 #define CURRENT_VERSION_STAMP 2 567 568 int _dwarf_add_simple_name_entry(Dwarf_P_Debug dbg, 569 Dwarf_P_Die die, 570 char *entry_name, 571 enum dwarf_sn_kind 572 entrykind, 573 Dwarf_Error * error); 574 575 enum dwarf_which_hash { 576 _dwarf_hash_debug_str, 577 _dwarf_hash_debug_line_str, 578 _dwarf_hash_debug_str_sup 579 }; 580 581 int 582 _dwarf_insert_or_find_in_debug_str(Dwarf_P_Debug dbg, 583 char *name, 584 enum dwarf_which_hash, 585 unsigned slen, /* includes space for trailing NUL */ 586 Dwarf_Unsigned *offset_in_debug_str, 587 Dwarf_Error *error); 588 589 int _dwarf_log_extra_flagstrings(Dwarf_P_Debug dbg, 590 const char *extra, 591 int *err); 592