| 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> /* 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_section.h" |
| 44 |
#include "dwarf_pro_types.h" |
| 45 |
|
| 46 |
#define SIZEOFT32 4 |
| 47 |
|
| 48 |
/* |
| 49 |
This function adds another type name to the |
| 50 |
list of type names for the given Dwarf_P_Debug. |
| 51 |
*/ |
| 52 |
int |
| 53 |
dwarf_add_typename_a(Dwarf_P_Debug dbg, |
| 54 |
Dwarf_P_Die die, |
| 55 |
char *type_name, |
| 56 |
Dwarf_Error * error) |
| 57 |
{ |
| 58 |
int res = 0; |
| 59 |
|
| 60 |
res = _dwarf_add_simple_name_entry(dbg, die, type_name, |
| 61 |
dwarf_snk_typename, error); |
| 62 |
return res; |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
The following is the generic 'add a simple name entry' |
| 67 |
for any of the simple name sections. |
| 68 |
|
| 69 |
See enum dwarf_sn_kind in pro_opaque.h |
| 70 |
|
| 71 |
*/ |
| 72 |
int |
| 73 |
_dwarf_add_simple_name_entry(Dwarf_P_Debug dbg, |
| 74 |
Dwarf_P_Die die, |
| 75 |
char *entry_name, |
| 76 |
enum dwarf_sn_kind entrykind, |
| 77 |
Dwarf_Error * error) |
| 78 |
{ |
| 79 |
Dwarf_P_Simple_nameentry nameentry; |
| 80 |
Dwarf_P_Simple_name_header hdr; |
| 81 |
char *name; |
| 82 |
int uword_size; |
| 83 |
|
| 84 |
if (dbg == NULL) { |
| 85 |
_dwarf_p_error(NULL, error, DW_DLE_DBG_NULL); |
| 86 |
return DW_DLV_ERROR; |
| 87 |
} |
| 88 |
|
| 89 |
if (die == NULL) { |
| 90 |
_dwarf_p_error(NULL, error, DW_DLE_DIE_NULL); |
| 91 |
return DW_DLV_ERROR; |
| 92 |
} |
| 93 |
|
| 94 |
nameentry = (Dwarf_P_Simple_nameentry) |
| 95 |
_dwarf_p_get_alloc(dbg, |
| 96 |
sizeof(struct Dwarf_P_Simple_nameentry_s)); |
| 97 |
if (nameentry == NULL) { |
| 98 |
_dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 99 |
return DW_DLV_ERROR; |
| 100 |
} |
| 101 |
|
| 102 |
name = _dwarf_p_get_alloc(dbg, strlen(entry_name) + 1); |
| 103 |
if (name == NULL) { |
| 104 |
_dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 105 |
return DW_DLV_ERROR; |
| 106 |
} |
| 107 |
strcpy(name, entry_name); |
| 108 |
|
| 109 |
nameentry->sne_die = die; |
| 110 |
nameentry->sne_name = name; |
| 111 |
nameentry->sne_name_len = strlen(name); |
| 112 |
uword_size = dbg->de_dwarf_offset_size; |
| 113 |
|
| 114 |
hdr = &dbg->de_simple_name_headers[entrykind]; |
| 115 |
if (hdr->sn_head == NULL) |
| 116 |
hdr->sn_head = hdr->sn_tail = nameentry; |
| 117 |
else { |
| 118 |
hdr->sn_tail->sne_next = nameentry; |
| 119 |
hdr->sn_tail = nameentry; |
| 120 |
} |
| 121 |
hdr->sn_count++; |
| 122 |
hdr->sn_net_len += uword_size + nameentry->sne_name_len + 1; |
| 123 |
|
| 124 |
return DW_DLV_OK; |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
_dwarf_transform_simplename_to_disk writes |
| 129 |
".rel.debug_pubnames", |
| 130 |
".rel.debug_funcnames", sgi extension |
| 131 |
".rel.debug_typenames", sgi extension |
| 132 |
".rel.debug_varnames", sgi extension |
| 133 |
".rel.debug_weaknames", sgi extension |
| 134 |
to disk. |
| 135 |
section_index indexes one of those sections. |
| 136 |
entrykind is one of those 'kind's. */ |
| 137 |
int |
| 138 |
_dwarf_transform_simplename_to_disk(Dwarf_P_Debug dbg, |
| 139 |
enum dwarf_sn_kind entrykind, |
| 140 |
int section_index, /* in de_elf_sects etc */ |
| 141 |
Dwarf_Unsigned *nbufs, |
| 142 |
Dwarf_Error * error) |
| 143 |
{ |
| 144 |
|
| 145 |
/* Used to fill in 0. */ |
| 146 |
const Dwarf_Signed big_zero = 0; |
| 147 |
|
| 148 |
/* Used to scan the section data buffers. */ |
| 149 |
Dwarf_P_Section_Data debug_sect; |
| 150 |
|
| 151 |
Dwarf_Signed debug_info_size; |
| 152 |
|
| 153 |
Dwarf_P_Simple_nameentry nameentry_original; |
| 154 |
Dwarf_P_Simple_nameentry nameentry; |
| 155 |
Dwarf_Small *stream_bytes; |
| 156 |
Dwarf_Small *cur_stream_bytes_ptr; |
| 157 |
Dwarf_Unsigned stream_bytes_count; |
| 158 |
Dwarf_Unsigned adjusted_length; /* count excluding length field */ |
| 159 |
|
| 160 |
int uword_size = dbg->de_dwarf_offset_size; |
| 161 |
int extension_size = dbg->de_64bit_extension ? 4 : 0; |
| 162 |
|
| 163 |
Dwarf_P_Simple_name_header hdr; |
| 164 |
|
| 165 |
/* ***** BEGIN CODE ***** */ |
| 166 |
|
| 167 |
debug_info_size = 0; |
| 168 |
for (debug_sect = dbg->de_debug_sects; debug_sect != NULL; |
| 169 |
debug_sect = debug_sect->ds_next) { |
| 170 |
/* We want the size of the .debug_info section for this CU |
| 171 |
because the dwarf spec requires us |
| 172 |
to output it below so we |
| 173 |
look for it specifically. */ |
| 174 |
if (debug_sect->ds_elf_sect_no == |
| 175 |
dbg->de_elf_sects[DEBUG_INFO]) { |
| 176 |
debug_info_size += debug_sect->ds_nbytes; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
hdr = &dbg->de_simple_name_headers[entrykind]; |
| 181 |
/* Size of the .debug_typenames (or similar) section header. */ |
| 182 |
stream_bytes_count = extension_size + uword_size + /* Size of |
| 183 |
length field. */ |
| 184 |
DWARF_HALF_SIZE + /* Size of version field. */ |
| 185 |
uword_size + /* Size of .debug_info offset. */ |
| 186 |
uword_size; /* Size of .debug_names. */ |
| 187 |
|
| 188 |
nameentry_original = hdr->sn_head; |
| 189 |
/* add in the content size */ |
| 190 |
stream_bytes_count += hdr->sn_net_len; |
| 191 |
|
| 192 |
/* Size of the last 0 offset. */ |
| 193 |
stream_bytes_count += uword_size; |
| 194 |
|
| 195 |
/* Now we know how long the entire section is */ |
| 196 |
GET_CHUNK(dbg, dbg->de_elf_sects[section_index], |
| 197 |
stream_bytes, (unsigned long) stream_bytes_count, error); |
| 198 |
cur_stream_bytes_ptr = stream_bytes; |
| 199 |
|
| 200 |
if (extension_size) { |
| 201 |
DISTINGUISHED_VALUE_ARRAY(v4); |
| 202 |
|
| 203 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 204 |
(const void *)&v4[0],SIZEOFT32 , extension_size); |
| 205 |
cur_stream_bytes_ptr += extension_size; |
| 206 |
|
| 207 |
} |
| 208 |
/* Write the adjusted length of .debug_*names section. */ |
| 209 |
adjusted_length = stream_bytes_count - uword_size - |
| 210 |
extension_size; |
| 211 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 212 |
(const void *) &adjusted_length, |
| 213 |
sizeof(adjusted_length), uword_size); |
| 214 |
cur_stream_bytes_ptr += uword_size; |
| 215 |
|
| 216 |
/* Write the version as 2 bytes. */ |
| 217 |
{ |
| 218 |
Dwarf_Half verstamp = VERSION_STAMP2; |
| 219 |
|
| 220 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 221 |
(const void *) &verstamp, |
| 222 |
sizeof(verstamp), DWARF_HALF_SIZE); |
| 223 |
cur_stream_bytes_ptr += DWARF_HALF_SIZE; |
| 224 |
} |
| 225 |
|
| 226 |
/* Write the offset of the compile-unit. */ |
| 227 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 228 |
(const void *) &big_zero, |
| 229 |
sizeof(big_zero), uword_size); |
| 230 |
cur_stream_bytes_ptr += uword_size; |
| 231 |
|
| 232 |
/* now create the relocation for the compile_unit offset */ |
| 233 |
{ |
| 234 |
int res = dbg->de_relocate_by_name_symbol(dbg, |
| 235 |
section_index, |
| 236 |
extension_size + uword_size + |
| 237 |
DWARF_HALF_SIZE /* r_offset */ , |
| 238 |
/* debug_info section name symbol */ |
| 239 |
dbg->de_sect_name_idx[DEBUG_INFO], |
| 240 |
dwarf_drt_data_reloc, |
| 241 |
uword_size); |
| 242 |
|
| 243 |
if (res != DW_DLV_OK) { |
| 244 |
_dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 245 |
return DW_DLV_ERROR; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/* Write the size of .debug_info section. */ |
| 250 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 251 |
(const void *) &debug_info_size, |
| 252 |
sizeof(debug_info_size), uword_size); |
| 253 |
cur_stream_bytes_ptr += uword_size; |
| 254 |
|
| 255 |
for (nameentry = nameentry_original; |
| 256 |
nameentry != NULL; nameentry = nameentry->sne_next) { |
| 257 |
|
| 258 |
/* Copy offset of die from start of compile-unit. */ |
| 259 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 260 |
(const void *) &nameentry->sne_die->di_offset, |
| 261 |
sizeof(nameentry->sne_die->di_offset), |
| 262 |
uword_size); |
| 263 |
cur_stream_bytes_ptr += uword_size; |
| 264 |
|
| 265 |
/* Copy the type name. */ |
| 266 |
strcpy((char *) cur_stream_bytes_ptr, nameentry->sne_name); |
| 267 |
cur_stream_bytes_ptr += nameentry->sne_name_len + 1; |
| 268 |
} |
| 269 |
|
| 270 |
WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr, |
| 271 |
(const void *) &big_zero, |
| 272 |
sizeof(big_zero), uword_size); |
| 273 |
*nbufs = dbg->de_n_debug_sect; |
| 274 |
return DW_DLV_OK; |
| 275 |
} |