Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_reloc_symbolic.c

raw
1 /*
2 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2016-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 <string.h> /* memcpy() */
33
34 #include "dwarf.h"
35 #include "libdwarf.h"
36 #include "dwarf_base_types.h"
37 #include "libdwarfp.h"
38 #include "dwarf_pro_incl.h"
39 #include "dwarf_pro_opaque.h"
40 #include "dwarf_pro_error.h"
41 #include "dwarf_pro_alloc.h"
42 #include "dwarf_pro_section.h"
43 #include "dwarf_pro_reloc.h"
44 #include "dwarf_pro_reloc_symbolic.h"
45
46 /* Return DW_DLV_ERROR on malloc error.
47 Return DW_DLV_OK otherwise */
48
49 int
50 _dwarf_pro_reloc_name_symbolic(Dwarf_P_Debug dbg,
51 int base_sec_index,
52 Dwarf_Unsigned offset, /* r_offset of reloc */
53 Dwarf_Unsigned symidx,
54 enum Dwarf_Rel_Type type,
55 int reltarget_length)
56 {
57 /* get a slot, fill in the slot entry */
58 void *relrec_to_fill = 0;
59 int res = 0;
60 struct Dwarf_Relocation_Data_s *slotp;
61
62 res = _dwarf_pro_reloc_get_a_slot(dbg, base_sec_index,
63 &relrec_to_fill);
64 if (res != DW_DLV_OK)
65 return res;
66 slotp = (struct Dwarf_Relocation_Data_s *) relrec_to_fill;
67 slotp->drd_type = type;
68 slotp->drd_length = reltarget_length;
69 slotp->drd_offset = offset;
70 slotp->drd_symbol_index = symidx;
71 return DW_DLV_OK;
72 }
73
74 /* Return DW_DLV_ERROR on malloc error.
75 Return DW_DLV_OK otherwise */
76 int
77 _dwarf_pro_reloc_length_symbolic(Dwarf_P_Debug dbg,
78 int base_sec_index,
79 Dwarf_Unsigned offset, /* r_offset of reloc */
80 Dwarf_Unsigned start_symidx,
81 Dwarf_Unsigned end_symidx,
82 enum Dwarf_Rel_Type type,
83 int reltarget_length)
84 {
85 /* get a slot, fill in the slot entry */
86 void *relrec_to_fill = 0;
87 int res = 0;
88 struct Dwarf_Relocation_Data_s *slotp1 = 0;
89 struct Dwarf_Relocation_Data_s *slotp2 = 0;
90
91 res = _dwarf_pro_reloc_get_a_slot(dbg, base_sec_index,
92 &relrec_to_fill);
93 if (res != DW_DLV_OK)
94 return res;
95 slotp1 = (struct Dwarf_Relocation_Data_s *) relrec_to_fill;
96 res = _dwarf_pro_reloc_get_a_slot(dbg, base_sec_index,
97 &relrec_to_fill);
98 if (res != DW_DLV_OK)
99 return res;
100 slotp2 = (struct Dwarf_Relocation_Data_s *) relrec_to_fill;
101
102 /* ASSERT: type == dwarf_drt_first_of_length_type_pair */
103 slotp1->drd_type = type;
104 slotp1->drd_length = reltarget_length;
105 slotp1->drd_offset = offset;
106 slotp1->drd_symbol_index = start_symidx;
107
108 slotp2->drd_type = dwarf_drt_second_of_length_pair;
109 slotp2->drd_length = reltarget_length;
110 slotp2->drd_offset = offset;
111 slotp2->drd_symbol_index = end_symidx;
112 return DW_DLV_OK;
113 }
114
115 /* Ensure each stream is a single buffer and
116 add that single buffer to the set of stream buffers.
117
118 By creating a new buffer and copying if necessary.
119 (If > 1 block, reduce to 1 block)
120
121 Free the input set of buffers if we consolidate.
122
123 We pass back *new_sec_count as zero because we
124 are not creating normal sections for a .o, but
125 symbolic relocations, separately counted.
126
127 Return -1 on error (malloc failure)
128
129 Return DW_DLV_OK on success. Any other return indicates
130 malloc failed. */
131 int
132 _dwarf_symbolic_relocs_to_disk(Dwarf_P_Debug dbg,
133 Dwarf_Signed * new_sec_count)
134 {
135 int i = 0;
136 Dwarf_Error error = 0;
137
138 for (i = 0; i < NUM_DEBUG_SECTIONS; ++i) {
139 int sec_index = 0;
140 Dwarf_P_Per_Reloc_Sect p_reloc = dbg->de_reloc_sect + i;
141 unsigned long ct = p_reloc->pr_reloc_total_count;
142 int err = 0;
143 if (ct == 0) {
144 /* No relocations in here. Nothing to do. */
145 continue;
146 }
147
148 /* total_size = ct *len; */
149 sec_index = p_reloc->pr_sect_num_of_reloc_sect;
150 if (sec_index == 0) {
151 /* sec_index zero means we have not processed this
152 section of relocations yet. */
153 /* Call de_callback_func
154 getting section number of reloc section. */
155 int rel_section_index = 0;
156 Dwarf_Unsigned name_idx = 0;
157
158 /* This is a bit of a fake, as we do not really have true
159 elf sections at all. Just the data such might contain.
160 But this lets the caller eventually link things
161 together: without this call we would not know what rel
162 data goes with what section when we are asked for the
163 real arrays. */
164
165 if (dbg->de_callback_func) {
166 /* For symbolic relocations de_callback_func
167 may well return 0. */
168 rel_section_index =
169 dbg->de_callback_func(_dwarf_rel_section_names[i],
170 dbg->de_relocation_record_size,
171 /* type */ SHT_REL,
172 /* flags */ 0,
173 /* link to symtab, which we cannot
174 know */ SHN_UNDEF,
175 /* sec rels apply to */
176 dbg->de_elf_sects[i],
177 &name_idx,
178 dbg->de_user_data,&err);
179 }
180 if (rel_section_index == -1) {
181 {
182 _dwarf_p_error(dbg, &error, DW_DLE_ELF_SECT_ERR);
183 return DW_DLV_ERROR;
184 }
185 }
186 p_reloc->pr_sect_num_of_reloc_sect = rel_section_index;
187 }
188
189 /* If pr_block_count 0 or 1 then the blocks are
190 an array (with 0 or 1 entries) so we'll just
191 return to the for loop. No more work to do here. */
192 if (p_reloc->pr_block_count < 2) {
193 continue;
194 }
195 {
196 /* Since more than one relocation on the section
197 we now convert the list of relocation blocks
198 into a proper array of blocks. */
199 struct Dwarf_P_Relocation_Block_s *new_blk = 0;
200 struct Dwarf_P_Relocation_Block_s *p_blk = 0;
201 Dwarf_Small *data = 0;
202 int res = 0;
203
204 p_blk = p_reloc->pr_first_block;
205 /* Do not zero pr_sect_num_of_reloc_sect */
206 p_reloc->pr_reloc_total_count = 0;
207 p_reloc->pr_first_block = 0;
208 p_reloc->pr_last_block = 0;
209 p_reloc->pr_block_count = 0;
210 /* Now we know a number making a single
211 array. Replaces DEFAULT_SLOTS_PER_BLOCK */
212 p_reloc->pr_slots_per_block_to_alloc = ct;
213
214 /* Creating new single block for all 'ct' entries.
215 Assigns a pointer value to pr_first_block
216 (which means our p_reloc).
217 It updates p_reloc->pr_first_block */
218 res = _dwarf_pro_pre_alloc_specific_reloc_slots(dbg,
219 p_reloc,ct);
220 if (res != DW_DLV_OK) {
221 return res;
222 }
223
224 new_blk = p_reloc->pr_first_block;
225 data = (Dwarf_Small *) new_blk->rb_data;
226
227 /* The following loop does the consolidation to a single
228 block and frees the input block(s).
229 p_blk points to the old singly-linked-list
230 and is the
231 only access to that list.
232 data is a pointer to the new array of ct entries
233 which is our target(destination) of the copies.
234 */
235 do {
236 struct Dwarf_P_Relocation_Block_s *p_blk_last = 0;
237 /* len identifies the data in all the slots
238 in use in this block. */
239 unsigned long len =
240 p_blk->rb_where_to_add_next - p_blk->rb_data;
241 memcpy(data, p_blk->rb_data, len);
242 data += len;
243 p_blk_last = p_blk;
244 p_blk = p_blk->rb_next;
245 _dwarf_p_dealloc((Dwarf_Small *) p_blk_last);
246 } while (p_blk);
247 /* ASSERT: the dangling p_blk list all dealloc'd
248 which is really a no-op, all deallocations
249 take place at producer_finish(). */
250 /* ASSERT: sum of len copied == total_size */
251 new_blk->rb_next_slot_to_use = ct;
252 new_blk->rb_where_to_add_next = (char *) data;
253 p_reloc->pr_reloc_total_count = ct;
254
255 /* Have now created a single block, but no
256 change in slots used (pr_reloc_total_count) */
257 }
258 }
259 /* There is no section data with symbolic,
260 so there is no count. */
261 *new_sec_count = 0;
262 return DW_DLV_OK;
263 }