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 6 This program is free software; you can redistribute it and/or modify it 7 under the terms of version 2.1 of the GNU Lesser General Public License 8 as published by the Free Software Foundation. 9 10 This program is distributed in the hope that it would be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 14 Further, this software is distributed without any warranty that it is 15 free of the rightful claim of any third person regarding infringement 16 or the like. Any license provided herein, whether implied or 17 otherwise, applies only to this software file. Patent licenses, if 18 any, provided herein do not apply to combinations of this program with 19 other software, or any other product whatsoever. 20 21 You should have received a copy of the GNU Lesser General Public 22 License along with this program; if not, write the Free Software 23 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301, 24 USA. 25 26 Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane, 27 Mountain View, CA 94043, or: 28 29 http://www.sgi.com 30 31 For further information regarding this notice, see: 32 33 http://oss.sgi.com/projects/GenInfo/NoticeExplan 34 35 */ 36 37 38 #include <stddef.h> 39 40 /* 41 Sgidefs included to define __uint32_t, 42 a guaranteed 4-byte quantity. 43 */ 44 #include "libdwarfdefs.h" 45 46 #define true 1 47 #define false 0 48 49 /* to identify a cie */ 50 #define DW_CIE_ID ~(0x0) 51 #define DW_CIE_VERSION 1 52 53 /*Dwarf_Word is unsigned word usable for index, count in memory */ 54 /*Dwarf_Sword is signed word usable for index, count in memory */ 55 /* The are 32 or 64 bits depending if 64 bit longs or not, which 56 ** fits the ILP32 and LP64 models 57 ** These work equally well with ILP64. 58 */ 59 60 typedef unsigned long Dwarf_Word; 61 typedef long Dwarf_Sword; 62 63 64 typedef signed char Dwarf_Sbyte; 65 typedef unsigned char Dwarf_Ubyte; 66 typedef signed short Dwarf_Shalf; 67 68 /* 69 On any change that makes libdwarf producer 70 incompatible, increment this number. 71 1->2->3 ... 72 73 */ 74 #define PRO_VERSION_MAGIC 0xdead1 75 76 77 /* these 2 are fixed sizes which must not vary with the 78 ** ILP32/LP64 model. These two stay at 32 bit. 79 */ 80 typedef __uint32_t Dwarf_ufixed; 81 typedef __int32_t Dwarf_sfixed; 82 83 /* 84 producer: 85 This struct is used to hold information about all 86 debug* sections. On creating a new section, section 87 names and indices are added to this struct 88 definition in pro_section.h 89 */ 90 typedef struct Dwarf_P_Section_Data_s *Dwarf_P_Section_Data; 91 92 /* 93 producer: 94 This struct is used to hold entries in the include directories 95 part of statement prologue. Definition in pro_line.h 96 */ 97 typedef struct Dwarf_P_Inc_Dir_s *Dwarf_P_Inc_Dir; 98 99 /* 100 producer: 101 This struct holds file entries for the statement prologue. 102 Defined in pro_line.h 103 */ 104 typedef struct Dwarf_P_F_Entry_s *Dwarf_P_F_Entry; 105 106 /* 107 producer: 108 This struct holds information for each cie. Defn in pro_frame.h 109 */ 110 typedef struct Dwarf_P_Cie_s *Dwarf_P_Cie; 111 112 /* 113 producer: 114 Struct to hold line number information, different from 115 Dwarf_Line opaque type. 116 */ 117 typedef struct Dwarf_P_Line_s *Dwarf_P_Line; 118 119 /* 120 producer: 121 Struct to hold information about address ranges. 122 */ 123 typedef struct Dwarf_P_Simple_nameentry_s *Dwarf_P_Simple_nameentry; 124 typedef struct Dwarf_P_Simple_name_header_s *Dwarf_P_Simple_name_header; 125 typedef struct Dwarf_P_Arange_s *Dwarf_P_Arange; 126 typedef struct Dwarf_P_Per_Reloc_Sect_s *Dwarf_P_Per_Reloc_Sect; 127 typedef struct Dwarf_P_Per_Sect_String_Attrs_s *Dwarf_P_Per_Sect_String_Attrs; 128 129 /* Defined to get at the elf section numbers and section name 130 indices in symtab for the dwarf sections 131 Must match .rel.* names in _dwarf_rel_section_names 132 exactly. 133 */ 134 #define DEBUG_INFO 0 135 #define DEBUG_LINE 1 136 #define DEBUG_ABBREV 2 137 #define DEBUG_FRAME 3 138 #define DEBUG_ARANGES 4 139 #define DEBUG_PUBNAMES 5 140 #define DEBUG_STR 6 141 #define DEBUG_FUNCNAMES 7 142 #define DEBUG_TYPENAMES 8 143 #define DEBUG_VARNAMES 9 144 #define DEBUG_WEAKNAMES 10 145 #define DEBUG_MACINFO 11 146 #define DEBUG_LOC 12 147 148 /* number of debug_* sections not including the relocations */ 149 #define NUM_DEBUG_SECTIONS DEBUG_LOC + 1 150 151 152 struct Dwarf_P_Die_s { 153 Dwarf_Unsigned di_offset; /* offset in debug info */ 154 char *di_abbrev; /* abbreviation */ 155 Dwarf_Word di_abbrev_nbytes; /* # of bytes in abbrev */ 156 Dwarf_Tag di_tag; 157 Dwarf_P_Die di_parent; /* parent of current die */ 158 Dwarf_P_Die di_child; /* first child */ 159 /* The last child field makes linking up children an O(1) operation, 160 See pro_die.c. */ 161 Dwarf_P_Die di_last_child; 162 Dwarf_P_Die di_left; /* left sibling */ 163 Dwarf_P_Die di_right; /* right sibling */ 164 Dwarf_P_Attribute di_attrs; /* list of attributes */ 165 Dwarf_P_Attribute di_last_attr; /* last attribute */ 166 int di_n_attr; /* number of attributes */ 167 Dwarf_P_Debug di_dbg; /* For memory management */ 168 Dwarf_Unsigned di_marker; /* used to attach symbols to dies */ 169 }; 170 171 172 /* producer fields */ 173 struct Dwarf_P_Attribute_s { 174 Dwarf_Half ar_attribute; /* Attribute Value. */ 175 Dwarf_Half ar_attribute_form; /* Attribute Form. */ 176 Dwarf_P_Die ar_ref_die; /* die pointer if form ref */ 177 char *ar_data; /* data, format given by form */ 178 Dwarf_Unsigned ar_nbytes; /* no. of bytes of data */ 179 Dwarf_Unsigned ar_rel_symidx; /* when attribute has a 180 relocatable value, holds 181 index of symbol in SYMTAB */ 182 Dwarf_Ubyte ar_rel_type; /* relocation type */ 183 Dwarf_Word ar_rel_offset; /* Offset of relocation within block */ 184 char ar_reloc_len; /* Number of bytes that relocation 185 applies to. 4 or 8. Unused and may 186 be 0 if if ar_rel_type is 187 R_MIPS_NONE */ 188 Dwarf_P_Attribute ar_next; 189 }; 190 191 /* A block of .debug_macinfo data: this forms a series of blocks. 192 ** Each macinfo input is compressed immediately and put into 193 ** the current block if room, else a newblock allocated. 194 ** The space allocation is such that the block and the macinfo 195 ** data are one malloc block: free with a pointer to this and the 196 ** mb_data is freed automatically. 197 ** Like the struct hack, but legal ANSI C. 198 */ 199 struct dw_macinfo_block_s { 200 struct dw_macinfo_block_s *mb_next; 201 unsigned long mb_avail_len; 202 unsigned long mb_used_len; 203 unsigned long mb_macinfo_data_space_len; 204 char *mb_data; /* original malloc ptr. */ 205 }; 206 207 /* dwarf_sn_kind is for the array of similarly-treated 208 name -> cu ties 209 */ 210 enum dwarf_sn_kind { dwarf_snk_pubname, dwarf_snk_funcname, 211 dwarf_snk_weakname, dwarf_snk_typename, 212 dwarf_snk_varname, 213 dwarf_snk_entrycount /* this one must be last */ 214 }; 215 216 217 218 /* The calls to add a varname etc use a list of 219 these as the list. 220 */ 221 struct Dwarf_P_Simple_nameentry_s { 222 Dwarf_P_Die sne_die; 223 char *sne_name; 224 int sne_name_len; 225 Dwarf_P_Simple_nameentry sne_next; 226 }; 227 228 /* An array of these, each of which heads a list 229 of Dwarf_P_Simple_nameentry 230 */ 231 struct Dwarf_P_Simple_name_header_s { 232 Dwarf_P_Simple_nameentry sn_head; 233 Dwarf_P_Simple_nameentry sn_tail; 234 Dwarf_Signed sn_count; 235 236 /* length that will be generated, not counting fixed header or 237 trailer */ 238 Dwarf_Signed sn_net_len; 239 }; 240 typedef int (*_dwarf_pro_reloc_name_func_ptr) (Dwarf_P_Debug dbg, 241 int sec_index, 242 Dwarf_Unsigned offset,/* r_offset */ 243 Dwarf_Unsigned symidx, 244 enum Dwarf_Rel_Type type, 245 int reltarget_length); 246 247 typedef int (*_dwarf_pro_reloc_length_func_ptr) (Dwarf_P_Debug dbg, 248 int sec_index, Dwarf_Unsigned offset,/* r_offset */ 249 Dwarf_Unsigned start_symidx, 250 Dwarf_Unsigned end_symidx, 251 enum Dwarf_Rel_Type type, 252 int reltarget_length); 253 typedef int (*_dwarf_pro_transform_relocs_func_ptr) (Dwarf_P_Debug dbg, 254 Dwarf_Signed * 255 new_sec_count); 256 257 /* 258 Each slot in a block of slots could be: 259 a binary stream relocation entry (32 or 64bit relocation data) 260 a SYMBOLIC relocation entry. 261 During creation sometimes we create multiple chained blocks, 262 but sometimes we create a single long block. 263 Before returning reloc data to caller, 264 we switch to a single, long-enough, 265 block. 266 267 We make counters here Dwarf_Unsigned so that we 268 get sufficient alignment. Since we use space after 269 the struct (at malloc time) for user data which 270 must have Dwarf_Unsigned alignment, this 271 struct must have that alignment too. 272 */ 273 struct Dwarf_P_Relocation_Block_s { 274 Dwarf_Unsigned rb_slots_in_block; /* slots in block, as created */ 275 Dwarf_Unsigned rb_next_slot_to_use; /* counter, start at 0. */ 276 struct Dwarf_P_Relocation_Block_s *rb_next; 277 char *rb_where_to_add_next; /* pointer to next slot (might be past 278 end, depending on 279 rb_next_slot_to_use) */ 280 char *rb_data; /* data area */ 281 }; 282 283 /* One of these per potential relocation section 284 So one per actual dwarf section. 285 Left zeroed when not used (some sections have 286 no relocations). 287 */ 288 struct Dwarf_P_Per_Reloc_Sect_s { 289 unsigned long pr_reloc_total_count; /* total number of entries 290 across all blocks */ 291 292 unsigned long pr_slots_per_block_to_alloc; /* at Block alloc, this 293 is the default number of slots to use */ 294 295 int pr_sect_num_of_reloc_sect; /* sect number returned by 296 de_callback_func() or de_callback_func_b() call, this is the sect 297 number of the relocation section. */ 298 299 /* singly-linked list. add at and ('last') with count of blocks */ 300 struct Dwarf_P_Relocation_Block_s *pr_first_block; 301 struct Dwarf_P_Relocation_Block_s *pr_last_block; 302 unsigned long pr_block_count; 303 }; 304 305 #define DEFAULT_SLOTS_PER_BLOCK 3 306 307 typedef struct memory_list_s { 308 struct memory_list_s *prev; 309 struct memory_list_s *next; 310 } memory_list_t; 311 312 struct Dwarf_P_Per_Sect_String_Attrs_s { 313 int sect_sa_section_number; 314 unsigned sect_sa_n_alloc; 315 unsigned sect_sa_n_used; 316 Dwarf_P_String_Attr sect_sa_list; 317 }; 318 319 /* Fields used by producer */ 320 struct Dwarf_P_Debug_s { 321 /* used to catch dso passing dbg to another DSO with incompatible 322 version of libdwarf See PRO_VERSION_MAGIC */ 323 int de_version_magic_number; 324 325 Dwarf_Handler de_errhand; 326 Dwarf_Ptr de_errarg; 327 328 /* Call back function, used to create .debug* sections. Provided 329 by user. Only of these used per dbg. */ 330 Dwarf_Callback_Func de_callback_func; 331 Dwarf_Callback_Func_b de_callback_func_b; 332 333 /* Flags from producer_init call */ 334 Dwarf_Unsigned de_flags; 335 336 /* This holds information on debug section stream output, including 337 the stream data */ 338 Dwarf_P_Section_Data de_debug_sects; 339 340 /* Pointer to the 'current active' section */ 341 Dwarf_P_Section_Data de_current_active_section; 342 343 /* Number of debug data streams globs. */ 344 Dwarf_Word de_n_debug_sect; 345 346 /* File entry information, null terminated singly-linked list */ 347 Dwarf_P_F_Entry de_file_entries; 348 Dwarf_P_F_Entry de_last_file_entry; 349 Dwarf_Unsigned de_n_file_entries; 350 351 /* Has the directories used to search for source files */ 352 Dwarf_P_Inc_Dir de_inc_dirs; 353 Dwarf_P_Inc_Dir de_last_inc_dir; 354 Dwarf_Unsigned de_n_inc_dirs; 355 356 /* Has all the line number info for the stmt program */ 357 Dwarf_P_Line de_lines; 358 Dwarf_P_Line de_last_line; 359 360 /* List of cie's for the debug unit */ 361 Dwarf_P_Cie de_frame_cies; 362 Dwarf_P_Cie de_last_cie; 363 Dwarf_Unsigned de_n_cie; 364 365 /* Singly-linked list of fde's for the debug unit */ 366 Dwarf_P_Fde de_frame_fdes; 367 Dwarf_P_Fde de_last_fde; 368 Dwarf_Unsigned de_n_fde; 369 370 /* First die, leads to all others */ 371 Dwarf_P_Die de_dies; 372 373 /* Pointer to list of strings */ 374 char *de_strings; 375 376 /* Pointer to chain of aranges */ 377 Dwarf_P_Arange de_arange; 378 Dwarf_P_Arange de_last_arange; 379 Dwarf_Sword de_arange_count; 380 381 /* macinfo controls. */ 382 /* first points to beginning of the list during creation */ 383 struct dw_macinfo_block_s *de_first_macinfo; 384 385 /* current points to the current, unfilled, block */ 386 struct dw_macinfo_block_s *de_current_macinfo; 387 388 /* Pointer to the first section, to support reset_section_bytes */ 389 Dwarf_P_Section_Data de_first_debug_sect; 390 391 /* handles pubnames, weaknames, etc. See dwarf_sn_kind in 392 pro_opaque.h */ 393 struct Dwarf_P_Simple_name_header_s 394 de_simple_name_headers[dwarf_snk_entrycount]; 395 396 /* relocation data. not all sections will actally have relocation 397 info, of course */ 398 struct Dwarf_P_Per_Reloc_Sect_s de_reloc_sect[NUM_DEBUG_SECTIONS]; 399 int de_reloc_next_to_return; /* iterator on reloc sections 400 (SYMBOLIC output) */ 401 402 /* used in remembering sections */ 403 int de_elf_sects[NUM_DEBUG_SECTIONS]; /* elf sect number of 404 the section itself, DEBUG_LINE for example */ 405 406 Dwarf_Unsigned de_sect_name_idx[NUM_DEBUG_SECTIONS]; /* section 407 name index or handle for the name of the symbol for 408 DEBUG_LINE for example */ 409 410 int de_offset_reloc; /* offset reloc type, R_MIPS_32 for 411 example. Specific to the ABI being 412 produced. Relocates offset size 413 field */ 414 int de_exc_reloc; /* reloc type specific to exception 415 table relocs. */ 416 int de_ptr_reloc; /* standard reloc type, R_MIPS_32 for 417 example. Specific to the ABI being 418 produced. relocates pointer size 419 field */ 420 421 unsigned char de_offset_size; /* section offset. Here to 422 avoid test of abi in macro 423 at run time MIPS -n32 4, 424 -64 8. */ 425 426 unsigned char de_pointer_size; /* size of pointer in target. 427 Here to avoid test of abi in 428 macro at run time MIPS -n32 429 4, -64 is 8. */ 430 431 unsigned char de_is_64bit; /* non-zero if is 64bit. Else 32 bit: 432 used for passing this info as a flag 433 */ 434 unsigned char de_relocation_record_size; /* reloc record size 435 varies by ABI and 436 relocation-output 437 method (stream or 438 symbolic) */ 439 440 unsigned char de_64bit_extension; /* non-zero if creating 64 bit 441 offsets using dwarf2-99 442 extension proposal */ 443 444 int de_ar_data_attribute_form; /* data8, data4 abi dependent */ 445 int de_ar_ref_attr_form; /* ref8 ref4 , abi dependent */ 446 447 /* simple name relocations */ 448 _dwarf_pro_reloc_name_func_ptr de_reloc_name; 449 450 /* relocations for a length, requiring a pair of symbols */ 451 _dwarf_pro_reloc_length_func_ptr de_reloc_pair; 452 453 _dwarf_pro_transform_relocs_func_ptr de_transform_relocs_to_disk; 454 455 /* following used for macro buffers */ 456 unsigned long de_compose_avail; 457 unsigned long de_compose_used_len; 458 459 unsigned char de_same_endian; 460 void *(*de_copy_word) (void *, const void *, size_t); 461 462 /* Add new fields at the END of this struct to preserve some hope 463 of sensible behavior on dbg passing between DSOs linked with 464 mismatched libdwarf producer versions. */ 465 466 Dwarf_P_Marker de_markers; /* pointer to array of markers */ 467 unsigned de_marker_n_alloc; 468 unsigned de_marker_n_used; 469 int de_sect_sa_next_to_return; /* Iterator on sring attrib sects */ 470 /* String attributes data of each section. */ 471 struct Dwarf_P_Per_Sect_String_Attrs_s de_sect_string_attr[NUM_DEBUG_SECTIONS]; 472 }; 473 474 #define CURRENT_VERSION_STAMP 2 475 476 Dwarf_Unsigned _dwarf_add_simple_name_entry(Dwarf_P_Debug dbg, 477 Dwarf_P_Die die, 478 char *entry_name, 479 enum dwarf_sn_kind 480 entrykind, 481 Dwarf_Error * error); 482 483 484 #define DISTINGUISHED_VALUE 0xffffffff /* 64bit extension flag */ 485