Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_arange.c

raw
1 /*
2
3 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright 2011-2020 David Anderson. 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
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_arange.h"
44 #include "dwarf_pro_section.h"
45 #include "dwarf_pro_reloc.h"
46
47 #define SIZEOFT32 4
48
49 /* This function adds another address range
50 to the list of address ranges for the
51 given Dwarf_P_Debug. It returns DW_DLV_ERROR on error,
52 and DW_DLV_OK otherwise. */
53 int
54 dwarf_add_arange_c(Dwarf_P_Debug dbg,
55 Dwarf_Addr begin_address,
56 Dwarf_Unsigned length,
57 Dwarf_Unsigned symbol_index,
58 Dwarf_Unsigned end_symbol_index,
59 Dwarf_Addr offset_from_end_sym,
60 Dwarf_Error * error)
61 {
62 Dwarf_P_Arange arange;
63
64 if (dbg == NULL) {
65 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
66 return DW_DLV_ERROR;
67 }
68
69 arange = (Dwarf_P_Arange)
70 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Arange_s));
71 if (arange == NULL) {
72 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
73 return DW_DLV_ERROR;
74 }
75
76 arange->ag_begin_address = begin_address;
77 arange->ag_length = length;
78 arange->ag_symbol_index = symbol_index;
79 arange->ag_end_symbol_index = end_symbol_index;
80 arange->ag_end_symbol_offset = offset_from_end_sym;
81
82 if (dbg->de_arange == NULL)
83 dbg->de_arange = dbg->de_last_arange = arange;
84 else {
85 dbg->de_last_arange->ag_next = arange;
86 dbg->de_last_arange = arange;
87 }
88 dbg->de_arange_count++;
89 return DW_DLV_OK;
90 }
91
92 int
93 _dwarf_transform_arange_to_disk(Dwarf_P_Debug dbg,
94 Dwarf_Unsigned *nbufs, Dwarf_Error * error)
95 {
96 /* Total num of bytes in .debug_aranges section. */
97 Dwarf_Unsigned arange_num_bytes = 0;
98
99 /* Adjustment to align the start of the actual
100 address ranges on a
101 boundary aligned with twice the address size. */
102 Dwarf_Small remainder = 0;
103
104 /* Total number of bytes excluding the length field. */
105 Dwarf_Unsigned adjusted_length = 0;
106
107 /* Points to first byte of .debug_aranges buffer. */
108 Dwarf_Small *arange = 0;
109
110 /* Fills in the .debug_aranges buffer. */
111 Dwarf_Small *arange_ptr = 0;
112
113 /* Scans the list of address ranges provided by user. */
114 Dwarf_P_Arange given_arange = 0;
115
116 /* Used to fill in 0. */
117 const Dwarf_Signed big_zero = 0;
118
119 int extension_word_size = dbg->de_64bit_extension ? 4 : 0;
120 int offset_size = dbg->de_dwarf_offset_size;
121 int upointer_size = dbg->de_pointer_size;
122
123 /* All dwarf versions so far use 2 here. */
124 Dwarf_Half version = 2;
125 int res = 0;
126
127 /* ***** BEGIN CODE ***** */
128
129 /* Size of the .debug_aranges section header. */
130 arange_num_bytes = extension_word_size +
131 offset_size + /* Size of length field. */
132 DWARF_HALF_SIZE + /* Size of version field. */
133 offset_size + /* Size of .debug_info offset. */
134 sizeof(Dwarf_Small) + /* Size of address size field. */
135 sizeof(Dwarf_Small); /* Size of segment size field. */
136
137 /* Adjust the size so that the set of aranges
138 begins on a boundary
139 that aligned with twice the address size.
140 This is a Libdwarf requirement. */
141 remainder = arange_num_bytes % (2 * upointer_size);
142 if (remainder != 0)
143 arange_num_bytes += (2 * upointer_size) - remainder;
144
145 /* Add the bytes for the actual address ranges. */
146 arange_num_bytes += upointer_size * 2 *
147 (dbg->de_arange_count + 1);
148
149 GET_CHUNK(dbg, dbg->de_elf_sects[DEBUG_ARANGES],
150 arange, (unsigned long) arange_num_bytes, error);
151 arange_ptr = arange;
152 if (extension_word_size) {
153 DISTINGUISHED_VALUE_ARRAY(v4);
154 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
155 (const void *)&v4[0] ,
156 SIZEOFT32, extension_word_size);
157 arange_ptr += extension_word_size;
158 }
159
160 /* Write the total length of .debug_aranges section. */
161 adjusted_length = arange_num_bytes - offset_size
162 - extension_word_size;
163 {
164 Dwarf_Unsigned du = adjusted_length;
165
166 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
167 (const void *) &du, sizeof(du), offset_size);
168 arange_ptr += offset_size;
169 }
170
171 /* Write the version as 2 bytes. */
172 {
173 Dwarf_Half verstamp = version;
174
175 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
176 (const void *) &verstamp,
177 sizeof(verstamp), DWARF_HALF_SIZE);
178 arange_ptr += DWARF_HALF_SIZE;
179 }
180
181 /* Write the .debug_info offset. This is always 0. */
182 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
183 (const void *) &big_zero,
184 sizeof(big_zero), offset_size);
185 arange_ptr += offset_size;
186
187 {
188 unsigned long count = dbg->de_arange_count + 1;
189 int res2 = 0;
190 Dwarf_P_Per_Reloc_Sect p_reloc =
191 &dbg->de_reloc_sect[DEBUG_ARANGES];
192
193 if (dbg->de_relocate_pair_by_symbol) {
194 count = (3 * dbg->de_arange_count) + 1;
195 }
196 /* The following is a small optimization: not needed for
197 correctness. Does nothing if
198 preloc->pr_first_block is non-null */
199 res2 = _dwarf_pro_pre_alloc_specific_reloc_slots(dbg,
200 p_reloc, count);
201 if (res2 != DW_DLV_OK) {
202 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
203 return DW_DLV_ERROR;
204 }
205 }
206
207 /* reloc for .debug_info */
208 res = dbg->de_relocate_by_name_symbol(dbg,
209 DEBUG_ARANGES,
210 extension_word_size +
211 offset_size + DWARF_HALF_SIZE,
212 dbg->de_sect_name_idx[DEBUG_INFO],
213 dwarf_drt_data_reloc, offset_size);
214 if (res == DW_DLV_NO_ENTRY) {
215 return res;
216 }
217 if (res == DW_DLV_ERROR) {
218 _dwarf_p_error(dbg, error,DW_DLE_RELOCS_ERROR);
219 return res;
220 }
221
222 /* Write the size of addresses. */
223 *arange_ptr = dbg->de_pointer_size;
224 arange_ptr++;
225
226 /* Write the size of segment addresses. This is zero for MIPS
227 architectures. */
228 *arange_ptr = 0;
229 arange_ptr++;
230
231 /* Skip over the padding to align the start of the actual address
232 ranges to twice the address size. */
233 if (remainder != 0)
234 arange_ptr += (2 * upointer_size) - remainder;
235
236 /* The arange address, length are pointer-size
237 fields of the target machine. */
238 for (given_arange = dbg->de_arange; given_arange != NULL;
239 given_arange = given_arange->ag_next) {
240
241 /* Write relocation record for beginning of address range. */
242 res = dbg->de_relocate_by_name_symbol(dbg, DEBUG_ARANGES,
243 arange_ptr - arange, /* r_offset */
244 (long) given_arange->ag_symbol_index,
245 dwarf_drt_data_reloc, upointer_size);
246 if (res != DW_DLV_OK) {
247 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
248 return DW_DLV_ERROR;
249 }
250
251 /* Copy beginning address of range. */
252 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
253 (const void *) &given_arange->ag_begin_address,
254 sizeof(given_arange->ag_begin_address),
255 upointer_size);
256 arange_ptr += upointer_size;
257
258 if (dbg->de_relocate_pair_by_symbol &&
259 given_arange->ag_end_symbol_index != 0 &&
260 given_arange->ag_length == 0) {
261 /* Symbolic reloc, need reloc for length
262 What if we really
263 know the length? If so, should use
264 the other part of 'if'. */
265 Dwarf_Unsigned val;
266
267 res = dbg->de_relocate_pair_by_symbol(dbg,
268 DEBUG_ARANGES,
269 arange_ptr - arange, /* r_offset */
270 given_arange->ag_symbol_index,
271 given_arange->ag_end_symbol_index,
272 dwarf_drt_first_of_length_pair,
273 upointer_size);
274 if (res != DW_DLV_OK) {
275 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
276 return DW_DLV_ERROR;
277 }
278
279 /* arange pre-calc so assem text can do .word end - begin
280 + val (gets val from stream) */
281 val = given_arange->ag_end_symbol_offset -
282 given_arange->ag_begin_address;
283 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
284 (const void *) &val,
285 sizeof(val), upointer_size);
286 arange_ptr += upointer_size;
287
288 } else {
289 /* plain old length to copy, no relocation at all */
290 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
291 (const void *) &given_arange->ag_length,
292 sizeof(given_arange->ag_length),
293 upointer_size);
294 arange_ptr += upointer_size;
295 }
296 }
297
298 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
299 (const void *) &big_zero,
300 sizeof(big_zero), upointer_size);
301
302 arange_ptr += upointer_size;
303 WRITE_UNALIGNED(dbg, (void *) arange_ptr,
304 (const void *) &big_zero,
305 sizeof(big_zero), upointer_size);
306 *nbufs = dbg->de_n_debug_sect;
307 return DW_DLV_OK;
308 }