| 1 |
/* |
| 2 |
Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2008-2020 David Anderson, Inc. 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 "dwarf.h" |
| 33 |
#include "libdwarf.h" |
| 34 |
#include "dwarf_base_types.h" |
| 35 |
#include "libdwarfp.h" |
| 36 |
#include "dwarf_pro_incl.h" |
| 37 |
#include "dwarf_pro_opaque.h" |
| 38 |
#include "dwarf_pro_error.h" |
| 39 |
#include "dwarf_pro_alloc.h" |
| 40 |
#include "dwarf_pro_reloc.h" |
| 41 |
|
| 42 |
/* Do initial alloc of newslots slots. |
| 43 |
Fails only if malloc fails. |
| 44 |
|
| 45 |
Supposed to be called before any relocs allocated. |
| 46 |
Ignored if after any allocated. |
| 47 |
|
| 48 |
Part of an optimization, so that for a known 'newslots' |
| 49 |
relocations count we can preallocate the right size block. |
| 50 |
Called from just 2 places. |
| 51 |
|
| 52 |
We use 'slots' so we don't have to do a new |
| 53 |
allocation for every relocation, just a new |
| 54 |
allocation every n slots. slots_in_block. |
| 55 |
|
| 56 |
returns DW_DLV_OK or DW_DLV_ERROR |
| 57 |
*/ |
| 58 |
int |
| 59 |
_dwarf_pro_pre_alloc_specific_reloc_slots(Dwarf_P_Debug dbg, |
| 60 |
Dwarf_P_Per_Reloc_Sect prel, |
| 61 |
Dwarf_Unsigned newslots) |
| 62 |
{ |
| 63 |
unsigned long len = 0; |
| 64 |
struct Dwarf_P_Relocation_Block_s *data = 0; |
| 65 |
unsigned long slots_in_blk = (unsigned long) newslots; |
| 66 |
unsigned long rel_rec_size = dbg->de_relocation_record_size; |
| 67 |
|
| 68 |
if (prel->pr_first_block) { |
| 69 |
return DW_DLV_OK; /* do nothing */ |
| 70 |
} |
| 71 |
|
| 72 |
len = sizeof(struct Dwarf_P_Relocation_Block_s) + |
| 73 |
slots_in_blk * rel_rec_size; |
| 74 |
data = (struct Dwarf_P_Relocation_Block_s *) |
| 75 |
_dwarf_p_get_alloc(dbg, len); |
| 76 |
if (!data) { |
| 77 |
return DW_DLV_ERROR; |
| 78 |
} |
| 79 |
data->rb_slots_in_block = slots_in_blk; /* could use default |
| 80 |
here, as fallback in case our original |
| 81 |
estimate wrong. When we call this we |
| 82 |
presumably know what we are doing, so |
| 83 |
keep this count for now */ |
| 84 |
data->rb_next_slot_to_use = 0; |
| 85 |
data->rb_where_to_add_next = |
| 86 |
((char *) data) + sizeof(struct Dwarf_P_Relocation_Block_s); |
| 87 |
data->rb_data = data->rb_where_to_add_next; |
| 88 |
|
| 89 |
prel->pr_first_block = data; |
| 90 |
prel->pr_last_block = data; |
| 91 |
prel->pr_block_count = 1; |
| 92 |
return DW_DLV_OK; |
| 93 |
} |
| 94 |
|
| 95 |
/*Do alloc of slots. |
| 96 |
Fails only if malloc fails. |
| 97 |
|
| 98 |
Only allocator used. |
| 99 |
|
| 100 |
returns DW_DLV_OK or DW_DLV_ERROR |
| 101 |
*/ |
| 102 |
int |
| 103 |
_dwarf_pro_alloc_reloc_slots(Dwarf_P_Debug dbg, int rel_sec_index) |
| 104 |
{ |
| 105 |
unsigned long len = 0; |
| 106 |
struct Dwarf_P_Relocation_Block_s *data = 0; |
| 107 |
Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[rel_sec_index]; |
| 108 |
unsigned long slots_in_blk = prel->pr_slots_per_block_to_alloc; |
| 109 |
unsigned long rel_rec_size = dbg->de_relocation_record_size; |
| 110 |
|
| 111 |
len = sizeof(struct Dwarf_P_Relocation_Block_s) + |
| 112 |
slots_in_blk * rel_rec_size; |
| 113 |
|
| 114 |
data = (struct Dwarf_P_Relocation_Block_s *) |
| 115 |
_dwarf_p_get_alloc(dbg, len); |
| 116 |
if (!data) { |
| 117 |
return DW_DLV_ERROR; |
| 118 |
} |
| 119 |
|
| 120 |
if (prel->pr_first_block) { |
| 121 |
prel->pr_last_block->rb_next = data; |
| 122 |
prel->pr_last_block = data; |
| 123 |
prel->pr_block_count += 1; |
| 124 |
} else { |
| 125 |
prel->pr_first_block = data; |
| 126 |
prel->pr_last_block = data; |
| 127 |
prel->pr_block_count = 1; |
| 128 |
} |
| 129 |
data->rb_slots_in_block = slots_in_blk; |
| 130 |
data->rb_next_slot_to_use = 0; |
| 131 |
data->rb_where_to_add_next = |
| 132 |
((char *) data) + sizeof(struct Dwarf_P_Relocation_Block_s); |
| 133 |
data->rb_data = data->rb_where_to_add_next; |
| 134 |
return DW_DLV_OK; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
/* Reserve a slot. return DW_DLV_OK if succeeds. |
| 139 |
|
| 140 |
Return DW_DLV_ERROR if fails (malloc error). |
| 141 |
|
| 142 |
Use the relrec_to_fill to pass back a pointer to |
| 143 |
a slot space to use. */ |
| 144 |
int |
| 145 |
_dwarf_pro_reloc_get_a_slot(Dwarf_P_Debug dbg, |
| 146 |
int base_sec_index, void **relrec_to_fill) |
| 147 |
{ |
| 148 |
struct Dwarf_P_Relocation_Block_s *data = 0; |
| 149 |
Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[base_sec_index]; |
| 150 |
unsigned long rel_rec_size = dbg->de_relocation_record_size; |
| 151 |
|
| 152 |
char *ret_addr = 0; |
| 153 |
|
| 154 |
data = prel->pr_last_block; |
| 155 |
if ((data == 0) || |
| 156 |
(data->rb_next_slot_to_use >= data->rb_slots_in_block)) { |
| 157 |
int res; |
| 158 |
|
| 159 |
res = _dwarf_pro_alloc_reloc_slots(dbg, base_sec_index); |
| 160 |
if (res != DW_DLV_OK) { |
| 161 |
return res; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
data = prel->pr_last_block; |
| 166 |
/* now we have an empty slot */ |
| 167 |
ret_addr = data->rb_where_to_add_next; |
| 168 |
data->rb_where_to_add_next += rel_rec_size; |
| 169 |
data->rb_next_slot_to_use += 1; |
| 170 |
prel->pr_reloc_total_count += 1; |
| 171 |
*relrec_to_fill = (void *) ret_addr; |
| 172 |
return DW_DLV_OK; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
On success returns count of |
| 178 |
.rel.* sections that are symbolic |
| 179 |
thru count_of_relocation_sections. |
| 180 |
|
| 181 |
On success, returns DW_DLV_OK. |
| 182 |
|
| 183 |
If this is not a 'symbolic' run, returns |
| 184 |
DW_DLV_NO_ENTRY. |
| 185 |
|
| 186 |
No errors are possible. |
| 187 |
*/ |
| 188 |
/*ARGSUSED*/ |
| 189 |
int |
| 190 |
dwarf_get_relocation_info_count(Dwarf_P_Debug dbg, |
| 191 |
Dwarf_Unsigned * count_of_relocation_sections, |
| 192 |
int *drd_buffer_version, |
| 193 |
Dwarf_Error * error) |
| 194 |
{ |
| 195 |
(void)error; |
| 196 |
if (dbg->de_flags & DW_DLC_SYMBOLIC_RELOCATIONS) { |
| 197 |
int i = 0; |
| 198 |
unsigned int count = 0; |
| 199 |
|
| 200 |
for (i = 0; i < NUM_DEBUG_SECTIONS; ++i) { |
| 201 |
if (dbg->de_reloc_sect[i].pr_reloc_total_count > 0) { |
| 202 |
++count; |
| 203 |
} |
| 204 |
} |
| 205 |
*count_of_relocation_sections = (Dwarf_Unsigned) count; |
| 206 |
*drd_buffer_version = DWARF_DRD_BUFFER_VERSION; |
| 207 |
return DW_DLV_OK; |
| 208 |
} |
| 209 |
/* Reset to start at beginning of reloc groups. */ |
| 210 |
dbg->de_reloc_next_to_return = 0; |
| 211 |
return DW_DLV_NO_ENTRY; |
| 212 |
} |
| 213 |
|
| 214 |
int |
| 215 |
dwarf_get_relocation_info(Dwarf_P_Debug dbg, |
| 216 |
Dwarf_Unsigned * elf_section_index, |
| 217 |
Dwarf_Unsigned * elf_section_index_link, |
| 218 |
Dwarf_Unsigned * relocation_buffer_count, |
| 219 |
Dwarf_Relocation_Data * reldata_buffer, |
| 220 |
Dwarf_Error * error) |
| 221 |
{ |
| 222 |
int next = dbg->de_reloc_next_to_return; |
| 223 |
|
| 224 |
if (dbg->de_flags & DW_DLC_SYMBOLIC_RELOCATIONS) { |
| 225 |
int i = 0; |
| 226 |
|
| 227 |
for (i = next; i < NUM_DEBUG_SECTIONS; ++i) { |
| 228 |
/* de_reloc_sect[] and de_elf_sects[] are |
| 229 |
in direct parallel. */ |
| 230 |
Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[i]; |
| 231 |
int elf_sect_num = dbg->de_elf_sects[i]; |
| 232 |
|
| 233 |
if (prel->pr_reloc_total_count > 0) { |
| 234 |
/* Set up 'next' for subsequent call to |
| 235 |
dwarf_get_relocation_info(). */ |
| 236 |
dbg->de_reloc_next_to_return = i + 1; |
| 237 |
|
| 238 |
/* ASSERT: prel->pr_block_count == 1 */ |
| 239 |
*elf_section_index = prel->pr_sect_num_of_reloc_sect; |
| 240 |
|
| 241 |
/* Elf sec num in generated elf */ |
| 242 |
*elf_section_index_link = elf_sect_num; |
| 243 |
|
| 244 |
*relocation_buffer_count = prel->pr_reloc_total_count; |
| 245 |
*reldata_buffer = (Dwarf_Relocation_Data) |
| 246 |
(prel->pr_first_block->rb_data); |
| 247 |
return DW_DLV_OK; |
| 248 |
} |
| 249 |
} |
| 250 |
DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR); |
| 251 |
} |
| 252 |
return DW_DLV_NO_ENTRY; |
| 253 |
} |