| 1 |
/* |
| 2 |
Copyright (C) 2000,2001,2004 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved. |
| 4 |
Portions Copyright 2008-2020 David Anderson, Inc. All rights reserved. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it |
| 7 |
and/or modify it under the terms of version 2.1 of the |
| 8 |
GNU Lesser General Public License as published by the Free |
| 9 |
Software Foundation. |
| 10 |
|
| 11 |
This program is distributed in the hope that it would be |
| 12 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 13 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
PURPOSE. |
| 15 |
|
| 16 |
Further, this software is distributed without any warranty |
| 17 |
that it is free of the rightful claim of any third person |
| 18 |
regarding infringement or the like. Any license provided |
| 19 |
herein, whether implied or otherwise, applies only to this |
| 20 |
software file. Patent licenses, if any, provided herein |
| 21 |
do not apply to combinations of this program with other |
| 22 |
software, or any other product whatsoever. |
| 23 |
|
| 24 |
You should have received a copy of the GNU Lesser General |
| 25 |
Public License along with this program; if not, write the |
| 26 |
Free Software Foundation, Inc., 51 Franklin Street - Fifth |
| 27 |
Floor, Boston MA 02110-1301, USA. |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
|
| 33 |
#include <stddef.h> /* NULL */ |
| 34 |
#include <string.h> /* memcpy() */ |
| 35 |
|
| 36 |
#ifdef DWARF_WITH_LIBELF |
| 37 |
#ifdef HAVE_LIBELF_H |
| 38 |
#include <libelf.h> |
| 39 |
#endif /* HAVE_LIBELF_H */ |
| 40 |
#ifdef HAVE_LIBELF_LIBELF_H |
| 41 |
#include <libelf/libelf.h> |
| 42 |
#endif /* HAVE_LIBELF_LIBELF_H */ |
| 43 |
#ifdef HAVE_ELF_H /* does includes of elf.h libelf.h here. */ |
| 44 |
#include <elf.h> |
| 45 |
#endif /* HAVE_ELF_H */ |
| 46 |
|
| 47 |
#include "dwarf.h" |
| 48 |
#include "libdwarf.h" |
| 49 |
#include "dwarf_base_types.h" |
| 50 |
#include "libdwarfp.h" |
| 51 |
#include "dwarf_pro_incl.h" |
| 52 |
#include "dwarf_pro_opaque.h" |
| 53 |
#include "dwarf_pro_error.h" |
| 54 |
#include "dwarf_pro_alloc.h" |
| 55 |
#include "dwarf_pro_section.h" |
| 56 |
#include "dwarf_pro_reloc.h" |
| 57 |
#include "dwarf_pro_reloc_stream.h" |
| 58 |
|
| 59 |
/* Set r_info as defined by ELF generic ABI */ |
| 60 |
#define Set_REL32_info(r,s,t) ((r).r_info = ELF32_R_INFO(s,t)) |
| 61 |
#define Set_REL64_info(r,s,t) ((r).r_info = ELF64_R_INFO(s,t)) |
| 62 |
|
| 63 |
/* Return DW_DLV_ERROR on malloc error or reltarget_length error. |
| 64 |
Return DW_DLV_OK otherwise */ |
| 65 |
/*ARGSUSED*/ int |
| 66 |
_dwarf_pro_reloc_name_stream64( |
| 67 |
Dwarf_P_Debug dbg, |
| 68 |
int base_sec_index, |
| 69 |
Dwarf_Unsigned offset, /* r_offset of reloc */ |
| 70 |
Dwarf_Unsigned symidx, |
| 71 |
enum Dwarf_Rel_Type type, |
| 72 |
int reltarget_length) |
| 73 |
{ |
| 74 |
#if HAVE_ELF64_GETEHDR |
| 75 |
REL64 *elf64_reloc = 0; |
| 76 |
void *relrec_to_fill = 0; |
| 77 |
int res = 0; |
| 78 |
int rel_type = 0; |
| 79 |
|
| 80 |
res = _dwarf_pro_reloc_get_a_slot(dbg, base_sec_index, |
| 81 |
&relrec_to_fill); |
| 82 |
if (res != DW_DLV_OK) |
| 83 |
return res; |
| 84 |
|
| 85 |
if (type == dwarf_drt_data_reloc) { |
| 86 |
if (reltarget_length == dbg->de_dwarf_offset_size) { |
| 87 |
rel_type = dbg->de_offset_reloc; |
| 88 |
} else if (reltarget_length == dbg->de_pointer_size) { |
| 89 |
rel_type = dbg->de_ptr_reloc; |
| 90 |
} else { |
| 91 |
return DW_DLV_ERROR; |
| 92 |
} |
| 93 |
} else if (type == dwarf_drt_segment_rel) { |
| 94 |
rel_type = dbg->de_exc_reloc; |
| 95 |
} else { |
| 96 |
/* We are in trouble: improper use of stream relocations. |
| 97 |
Someone else will diagnose */ |
| 98 |
rel_type = 0; |
| 99 |
} |
| 100 |
|
| 101 |
elf64_reloc = (REL64 *)relrec_to_fill; |
| 102 |
elf64_reloc->r_offset = offset; |
| 103 |
Set_REL64_info(*elf64_reloc, symidx, rel_type); |
| 104 |
return DW_DLV_OK; |
| 105 |
#else /* !HAVE_ELF64_GETEHDR */ |
| 106 |
(void)dbg; |
| 107 |
(void)base_sec_index;; |
| 108 |
(void)offset; |
| 109 |
(void)end_symidx; |
| 110 |
(void)type; |
| 111 |
(void)reltarget_length; |
| 112 |
return DW_DLV_ERROR; |
| 113 |
#endif /* #if HAVE_ELF64_GETEHDR */ |
| 114 |
} |
| 115 |
|
| 116 |
/* Return DW_DLV_ERROR on malloc error or reltarget_length error. |
| 117 |
Return DW_DLV_OK otherwise |
| 118 |
a binary reloc: 32bit ABI */ int |
| 119 |
_dwarf_pro_reloc_name_stream32(Dwarf_P_Debug dbg, int base_sec_index, |
| 120 |
Dwarf_Unsigned offset, /* r_offset of reloc */ |
| 121 |
Dwarf_Unsigned symidx, |
| 122 |
enum Dwarf_Rel_Type type, |
| 123 |
int reltarget_length) |
| 124 |
{ |
| 125 |
REL32 *elf32_reloc = 0; |
| 126 |
void *relrec_to_fill = 0; |
| 127 |
int res = 0; |
| 128 |
int rel_type = 0; |
| 129 |
|
| 130 |
res = _dwarf_pro_reloc_get_a_slot(dbg, base_sec_index, |
| 131 |
&relrec_to_fill); |
| 132 |
if (res != DW_DLV_OK) |
| 133 |
return res; |
| 134 |
if (type == dwarf_drt_data_reloc) { |
| 135 |
if (reltarget_length == dbg->de_dwarf_offset_size) { |
| 136 |
rel_type = dbg->de_offset_reloc; |
| 137 |
} else if (reltarget_length == dbg->de_pointer_size) { |
| 138 |
rel_type = dbg->de_ptr_reloc; |
| 139 |
} else { |
| 140 |
return DW_DLV_ERROR; |
| 141 |
} |
| 142 |
} else if (type == dwarf_drt_segment_rel) { |
| 143 |
rel_type = dbg->de_exc_reloc; |
| 144 |
} else { |
| 145 |
/* We are in trouble: improper use of stream relocations. |
| 146 |
Someone else will diagnose */ |
| 147 |
rel_type = 0; |
| 148 |
} |
| 149 |
|
| 150 |
elf32_reloc = (REL32*)relrec_to_fill; |
| 151 |
elf32_reloc->r_offset = (Elf32_Addr) offset; |
| 152 |
Set_REL32_info(*elf32_reloc, symidx, rel_type); |
| 153 |
return DW_DLV_OK; |
| 154 |
|
| 155 |
/* get a slot, fill in the slot entry */ |
| 156 |
} |
| 157 |
|
| 158 |
/* Return DW_DLV_OK. |
| 159 |
Never can really do anything: lengths cannot |
| 160 |
be represented as end-start in a stream. */ |
| 161 |
/*ARGSUSED*/ int |
| 162 |
_dwarf_pro_reloc_length_stream(Dwarf_P_Debug dbg, |
| 163 |
int base_sec_index, |
| 164 |
Dwarf_Unsigned offset, /* r_offset of reloc */ |
| 165 |
Dwarf_Unsigned start_symidx, |
| 166 |
Dwarf_Unsigned end_symidx, |
| 167 |
enum Dwarf_Rel_Type type, |
| 168 |
int reltarget_length) |
| 169 |
{ |
| 170 |
(void)dbg; |
| 171 |
(void)base_sec_index;; |
| 172 |
(void)offset; |
| 173 |
(void)start_symidx; |
| 174 |
(void)end_symidx; |
| 175 |
(void)type; |
| 176 |
(void)reltarget_length; |
| 177 |
/* get a slot, fill in the slot entry */ |
| 178 |
return DW_DLV_OK; |
| 179 |
} |
| 180 |
|
| 181 |
/* Ensure each stream is a single buffer and |
| 182 |
add that single buffer to the set of stream buffers. |
| 183 |
|
| 184 |
By creating a new buffer and copying if necessary. |
| 185 |
|
| 186 |
Free the input set of buffers if we consolidate. |
| 187 |
Return -1 on error (malloc failure) |
| 188 |
|
| 189 |
Return DW_DLV_OK on success. Any other return indicates |
| 190 |
malloc failed. */ |
| 191 |
int |
| 192 |
_dwarf_stream_relocs_to_disk(Dwarf_P_Debug dbg, |
| 193 |
Dwarf_Signed * new_sec_count) |
| 194 |
{ |
| 195 |
unsigned long total_size = 0; |
| 196 |
int i = 0; |
| 197 |
Dwarf_Error erre = 0; |
| 198 |
Dwarf_Error *error = &erre; |
| 199 |
Dwarf_Signed sec_count = 0; |
| 200 |
|
| 201 |
for (i = 0; i < NUM_DEBUG_SECTIONS; ++i) { |
| 202 |
Dwarf_P_Per_Reloc_Sect p_reloc = dbg->de_reloc_sect +i; |
| 203 |
Dwarf_Small *data = 0; |
| 204 |
int sec_index = 0; |
| 205 |
unsigned long ct = p_reloc->pr_reloc_total_count; |
| 206 |
unsigned len = 0; |
| 207 |
struct Dwarf_P_Relocation_Block_s *p_blk = 0; |
| 208 |
struct Dwarf_P_Relocation_Block_s *p_blk_last = 0; |
| 209 |
Dwarf_P_Per_Reloc_Sect prb = 0; |
| 210 |
|
| 211 |
if (ct == 0) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
prb = &dbg->de_reloc_sect[i]; |
| 215 |
len = dbg->de_relocation_record_size; |
| 216 |
++sec_count; |
| 217 |
|
| 218 |
total_size = ct * len; |
| 219 |
sec_index = prb->pr_sect_num_of_reloc_sect; |
| 220 |
if (sec_index == 0) { |
| 221 |
/* Call de_callback_func or de_callback_func_b |
| 222 |
or _c, getting |
| 223 |
section number of reloc section. */ |
| 224 |
int rel_section_index = 0; |
| 225 |
Dwarf_Unsigned name_idx = 0; |
| 226 |
int erri = 0; |
| 227 |
|
| 228 |
if (dbg->de_callback_func) { |
| 229 |
rel_section_index = |
| 230 |
dbg->de_callback_func(_dwarf_rel_section_names[i], |
| 231 |
/* size */ dbg->de_relocation_record_size, |
| 232 |
/* type */ SHT_REL, |
| 233 |
/* flags */ 0, |
| 234 |
/* link to symtab, which we cannot |
| 235 |
know */ 0, |
| 236 |
/* info == link to sec rels apply to */ |
| 237 |
dbg->de_elf_sects[i], |
| 238 |
&name_idx, |
| 239 |
dbg->de_user_data, |
| 240 |
&erri); |
| 241 |
} |
| 242 |
if (rel_section_index == -1) { |
| 243 |
{ |
| 244 |
_dwarf_p_error(dbg, error, DW_DLE_ELF_SECT_ERR); |
| 245 |
return DW_DLV_ERROR; |
| 246 |
} |
| 247 |
} |
| 248 |
prb->pr_sect_num_of_reloc_sect = rel_section_index; |
| 249 |
sec_index = rel_section_index; |
| 250 |
} |
| 251 |
GET_CHUNK(dbg, sec_index, data, total_size, &erri); |
| 252 |
p_blk = p_reloc->pr_first_block; |
| 253 |
|
| 254 |
/* Following loop executes at least once. Effects the |
| 255 |
consolidation to a single block or, if already a single |
| 256 |
block, simply copies to the output buffer. And frees the |
| 257 |
input block. The new block is in the |
| 258 |
de_debug_sects list. */ |
| 259 |
while (p_blk) { |
| 260 |
unsigned long lenk = |
| 261 |
p_blk->rb_where_to_add_next - p_blk->rb_data; |
| 262 |
|
| 263 |
memcpy(data, p_blk->rb_data, lenk); |
| 264 |
data += lenk; |
| 265 |
p_blk_last = p_blk; |
| 266 |
p_blk = p_blk->rb_next; |
| 267 |
_dwarf_p_dealloc((Dwarf_Small *) p_blk_last); |
| 268 |
} |
| 269 |
/* ASSERT: sum of len copied == total_size */ |
| 270 |
|
| 271 |
/* We have copied the input, now drop the pointers to it. For |
| 272 |
debugging, leave the other data untouched. */ |
| 273 |
p_reloc->pr_first_block = 0; |
| 274 |
p_reloc->pr_last_block = 0; |
| 275 |
} |
| 276 |
*new_sec_count = sec_count; |
| 277 |
return DW_DLV_OK; |
| 278 |
} |
| 279 |
#else /* DWARF_WITH_LIBELF */ |
| 280 |
/* avoids empty file warning if no libelf */ |
| 281 |
int dwarf_dummy_pro_reloc_stream = 2; |
| 282 |
#endif /* DWARF_WITH_LIBELF */ |