libdwarf_reloc.c revision 1.3 1 1.2 christos /* $NetBSD: libdwarf_reloc.c,v 1.3 2016/02/20 02:43:41 christos Exp $ */
2 1.2 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2010 Kai Wang
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 christos * SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos
29 1.1 christos #include "_libdwarf.h"
30 1.1 christos
31 1.2 christos __RCSID("$NetBSD: libdwarf_reloc.c,v 1.3 2016/02/20 02:43:41 christos Exp $");
32 1.3 christos ELFTC_VCSID("Id: libdwarf_reloc.c 3198 2015-05-14 18:36:19Z emaste ");
33 1.1 christos
34 1.2 christos #ifndef R_386_32
35 1.2 christos #define R_386_32 1
36 1.2 christos #endif
37 1.2 christos #ifndef R_X86_64_64
38 1.2 christos #define R_X86_64_64 1
39 1.2 christos #endif
40 1.2 christos #ifndef R_X86_64_32
41 1.2 christos #define R_X86_64_32 10
42 1.2 christos #endif
43 1.2 christos #ifndef R_SPARC_UA32
44 1.2 christos #define R_SPARC_UA32 23
45 1.2 christos #endif
46 1.2 christos #ifndef R_SPARC_UA64
47 1.2 christos #define R_SPARC_UA64 54
48 1.2 christos #endif
49 1.2 christos #ifndef R_PPC_ADDR32
50 1.2 christos #define R_PPC_ADDR32 1
51 1.2 christos #endif
52 1.2 christos #ifndef R_ARM_ABS32
53 1.2 christos #define R_ARM_ABS32 2
54 1.2 christos #endif
55 1.2 christos #ifndef R_MIPS_32
56 1.2 christos #define R_MIPS_32 2
57 1.2 christos #endif
58 1.2 christos #ifndef R_MIPS_64
59 1.2 christos #define R_MIPS_64 18
60 1.2 christos #endif
61 1.2 christos #ifndef R_IA_64_DIR32LSB
62 1.2 christos #define R_IA_64_DIR32LSB 0x25
63 1.2 christos #endif
64 1.2 christos #ifndef R_IA_64_DIR64LSB
65 1.2 christos #define R_IA_64_DIR64LSB 0x27
66 1.2 christos #endif
67 1.2 christos #ifndef R_IA_64_SECREL32LSB
68 1.2 christos #define R_IA_64_SECREL32LSB 0x65
69 1.2 christos #endif
70 1.3 christos #ifndef R_AARCH64_ABS64
71 1.3 christos #define R_AARCH64_ABS64 257
72 1.3 christos #endif
73 1.3 christos #ifndef R_AARCH64_ABS32
74 1.3 christos #define R_AARCH64_ABS32 258
75 1.3 christos #endif
76 1.2 christos
77 1.1 christos Dwarf_Unsigned
78 1.1 christos _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64)
79 1.1 christos {
80 1.1 christos
81 1.1 christos assert(dbg != NULL);
82 1.1 christos
83 1.1 christos switch (dbg->dbgp_isa) {
84 1.3 christos case DW_ISA_AARCH64:
85 1.3 christos return (is64 ? R_AARCH64_ABS64 : R_AARCH64_ABS32);
86 1.1 christos case DW_ISA_X86:
87 1.1 christos return (R_386_32);
88 1.1 christos case DW_ISA_X86_64:
89 1.1 christos return (is64 ? R_X86_64_64 : R_X86_64_32);
90 1.1 christos case DW_ISA_SPARC:
91 1.1 christos return (is64 ? R_SPARC_UA64 : R_SPARC_UA32);
92 1.1 christos case DW_ISA_PPC:
93 1.1 christos return (R_PPC_ADDR32);
94 1.1 christos case DW_ISA_ARM:
95 1.1 christos return (R_ARM_ABS32);
96 1.1 christos case DW_ISA_MIPS:
97 1.1 christos return (is64 ? R_MIPS_64 : R_MIPS_32);
98 1.1 christos case DW_ISA_IA64:
99 1.1 christos return (is64 ? R_IA_64_DIR64LSB : R_IA_64_DIR32LSB);
100 1.1 christos default:
101 1.1 christos break;
102 1.1 christos }
103 1.1 christos return (0); /* NOT REACHED */
104 1.1 christos }
105 1.1 christos
106 1.1 christos int
107 1.1 christos _dwarf_get_reloc_size(Dwarf_Debug dbg, Dwarf_Unsigned rel_type)
108 1.1 christos {
109 1.1 christos
110 1.1 christos switch (dbg->dbg_machine) {
111 1.1 christos case EM_NONE:
112 1.1 christos break;
113 1.3 christos case EM_AARCH64:
114 1.3 christos if (rel_type == R_AARCH64_ABS32)
115 1.3 christos return (4);
116 1.3 christos else if (rel_type == R_AARCH64_ABS64)
117 1.3 christos return (8);
118 1.3 christos break;
119 1.1 christos case EM_ARM:
120 1.1 christos if (rel_type == R_ARM_ABS32)
121 1.1 christos return (4);
122 1.1 christos break;
123 1.1 christos case EM_386:
124 1.3 christos case EM_IAMCU:
125 1.1 christos if (rel_type == R_386_32)
126 1.1 christos return (4);
127 1.1 christos break;
128 1.1 christos case EM_X86_64:
129 1.1 christos if (rel_type == R_X86_64_32)
130 1.1 christos return (4);
131 1.1 christos else if (rel_type == R_X86_64_64)
132 1.1 christos return (8);
133 1.1 christos break;
134 1.1 christos case EM_SPARC:
135 1.1 christos if (rel_type == R_SPARC_UA32)
136 1.1 christos return (4);
137 1.1 christos else if (rel_type == R_SPARC_UA64)
138 1.1 christos return (8);
139 1.1 christos break;
140 1.1 christos case EM_PPC:
141 1.1 christos if (rel_type == R_PPC_ADDR32)
142 1.1 christos return (4);
143 1.1 christos break;
144 1.1 christos case EM_MIPS:
145 1.1 christos if (rel_type == R_MIPS_32)
146 1.1 christos return (4);
147 1.1 christos else if (rel_type == R_MIPS_64)
148 1.1 christos return (8);
149 1.1 christos break;
150 1.1 christos case EM_IA_64:
151 1.1 christos if (rel_type == R_IA_64_SECREL32LSB)
152 1.1 christos return (4);
153 1.1 christos else if (rel_type == R_IA_64_DIR64LSB)
154 1.1 christos return (8);
155 1.1 christos break;
156 1.1 christos default:
157 1.1 christos break;
158 1.1 christos }
159 1.1 christos
160 1.1 christos /* unknown relocation. */
161 1.1 christos return (0);
162 1.1 christos }
163 1.1 christos
164 1.1 christos int
165 1.1 christos _dwarf_reloc_section_init(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp,
166 1.1 christos Dwarf_P_Section ref, Dwarf_Error *error)
167 1.1 christos {
168 1.1 christos Dwarf_Rel_Section drs;
169 1.1 christos char name[128];
170 1.1 christos int pseudo;
171 1.1 christos
172 1.1 christos assert(dbg != NULL && drsp != NULL && ref != NULL);
173 1.1 christos
174 1.1 christos if ((drs = calloc(1, sizeof(struct _Dwarf_Rel_Section))) == NULL) {
175 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
176 1.1 christos return (DW_DLE_MEMORY);
177 1.1 christos }
178 1.1 christos
179 1.1 christos drs->drs_ref = ref;
180 1.1 christos
181 1.1 christos /*
182 1.1 christos * FIXME The logic here is most likely wrong. It should
183 1.1 christos * be the ISA that determines relocation type.
184 1.1 christos */
185 1.1 christos if (dbg->dbgp_flags & DW_DLC_SIZE_64)
186 1.1 christos drs->drs_addend = 1;
187 1.1 christos else
188 1.1 christos drs->drs_addend = 0;
189 1.1 christos
190 1.1 christos if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS)
191 1.1 christos pseudo = 1;
192 1.1 christos else
193 1.1 christos pseudo = 0;
194 1.1 christos
195 1.1 christos snprintf(name, sizeof(name), "%s%s",
196 1.1 christos drs->drs_addend ? ".rela" : ".rel", ref->ds_name);
197 1.1 christos if (_dwarf_section_init(dbg, &drs->drs_ds, name, pseudo, error) !=
198 1.1 christos DW_DLE_NONE) {
199 1.1 christos free(drs);
200 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
201 1.1 christos return (DW_DLE_MEMORY);
202 1.1 christos }
203 1.1 christos
204 1.1 christos STAILQ_INIT(&drs->drs_dre);
205 1.1 christos STAILQ_INSERT_TAIL(&dbg->dbgp_drslist, drs, drs_next);
206 1.1 christos dbg->dbgp_drscnt++;
207 1.1 christos *drsp = drs;
208 1.1 christos
209 1.1 christos return (DW_DLE_NONE);
210 1.1 christos }
211 1.1 christos
212 1.1 christos void
213 1.1 christos _dwarf_reloc_section_free(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp)
214 1.1 christos {
215 1.1 christos Dwarf_Rel_Section drs, tdrs;
216 1.1 christos Dwarf_Rel_Entry dre, tdre;
217 1.1 christos
218 1.1 christos assert(dbg != NULL && drsp != NULL);
219 1.1 christos
220 1.1 christos if (*drsp == NULL)
221 1.1 christos return;
222 1.1 christos
223 1.1 christos STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
224 1.1 christos if (drs != *drsp)
225 1.1 christos continue;
226 1.1 christos STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
227 1.1 christos drs_next);
228 1.1 christos STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
229 1.1 christos STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
230 1.1 christos dre_next);
231 1.1 christos free(dre);
232 1.1 christos }
233 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0)
234 1.1 christos _dwarf_section_free(dbg, &drs->drs_ds);
235 1.1 christos else {
236 1.1 christos if (drs->drs_ds->ds_name)
237 1.1 christos free(drs->drs_ds->ds_name);
238 1.1 christos free(drs->drs_ds);
239 1.1 christos }
240 1.1 christos free(drs);
241 1.1 christos *drsp = NULL;
242 1.1 christos dbg->dbgp_drscnt--;
243 1.1 christos break;
244 1.1 christos }
245 1.1 christos }
246 1.1 christos
247 1.1 christos int
248 1.1 christos _dwarf_reloc_entry_add(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
249 1.1 christos Dwarf_P_Section ds, unsigned char type, unsigned char length,
250 1.1 christos Dwarf_Unsigned offset, Dwarf_Unsigned symndx, Dwarf_Unsigned addend,
251 1.1 christos const char *secname, Dwarf_Error *error)
252 1.1 christos {
253 1.1 christos Dwarf_Rel_Entry dre;
254 1.1 christos Dwarf_Unsigned reloff;
255 1.1 christos int ret;
256 1.1 christos
257 1.1 christos assert(drs != NULL);
258 1.1 christos assert(offset <= ds->ds_size);
259 1.1 christos reloff = offset;
260 1.1 christos
261 1.1 christos /*
262 1.1 christos * If the DW_DLC_SYMBOLIC_RELOCATIONS flag is set or ElfXX_Rel
263 1.1 christos * is used instead of ELfXX_Rela, we need to write the addend
264 1.1 christos * in the storage unit to be relocated. Otherwise write 0 in the
265 1.1 christos * storage unit and the addend will be written into relocation
266 1.1 christos * section later.
267 1.1 christos */
268 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) ||
269 1.1 christos drs->drs_addend == 0)
270 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
271 1.1 christos addend, length, error);
272 1.1 christos else
273 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
274 1.1 christos 0, length, error);
275 1.1 christos if (ret != DW_DLE_NONE)
276 1.1 christos return (ret);
277 1.1 christos if (offset > ds->ds_size)
278 1.1 christos ds->ds_size = offset;
279 1.1 christos
280 1.1 christos if ((dre = calloc(1, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
281 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
282 1.1 christos return (DW_DLE_MEMORY);
283 1.1 christos }
284 1.1 christos STAILQ_INSERT_TAIL(&drs->drs_dre, dre, dre_next);
285 1.1 christos dre->dre_type = type;
286 1.1 christos dre->dre_length = length;
287 1.1 christos dre->dre_offset = reloff;
288 1.1 christos dre->dre_symndx = symndx;
289 1.1 christos dre->dre_addend = addend;
290 1.1 christos dre->dre_secname = secname;
291 1.1 christos drs->drs_drecnt++;
292 1.1 christos
293 1.1 christos return (DW_DLE_NONE);
294 1.1 christos }
295 1.1 christos
296 1.1 christos int
297 1.1 christos _dwarf_reloc_entry_add_pair(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
298 1.1 christos Dwarf_P_Section ds, unsigned char length, Dwarf_Unsigned offset,
299 1.1 christos Dwarf_Unsigned symndx, Dwarf_Unsigned esymndx, Dwarf_Unsigned symoff,
300 1.1 christos Dwarf_Unsigned esymoff, Dwarf_Error *error)
301 1.1 christos {
302 1.1 christos Dwarf_Rel_Entry dre;
303 1.1 christos Dwarf_Unsigned reloff;
304 1.1 christos int ret;
305 1.1 christos
306 1.1 christos assert(drs != NULL);
307 1.1 christos assert(offset <= ds->ds_size);
308 1.1 christos assert(dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS);
309 1.1 christos reloff = offset;
310 1.1 christos
311 1.1 christos /* Write net offset into section stream. */
312 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
313 1.1 christos esymoff - symoff, length, error);
314 1.1 christos if (ret != DW_DLE_NONE)
315 1.1 christos return (ret);
316 1.1 christos if (offset > ds->ds_size)
317 1.1 christos ds->ds_size = offset;
318 1.1 christos
319 1.1 christos if ((dre = calloc(2, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
320 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
321 1.1 christos return (DW_DLE_MEMORY);
322 1.1 christos }
323 1.1 christos STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[0], dre_next);
324 1.1 christos STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[1], dre_next);
325 1.1 christos dre[0].dre_type = dwarf_drt_first_of_length_pair;
326 1.1 christos dre[0].dre_length = length;
327 1.1 christos dre[0].dre_offset = reloff;
328 1.1 christos dre[0].dre_symndx = symndx;
329 1.1 christos dre[0].dre_addend = 0;
330 1.1 christos dre[0].dre_secname = NULL;
331 1.1 christos dre[1].dre_type = dwarf_drt_second_of_length_pair;
332 1.1 christos dre[1].dre_length = length;
333 1.1 christos dre[1].dre_offset = reloff;
334 1.1 christos dre[1].dre_symndx = esymndx;
335 1.1 christos dre[1].dre_addend = 0;
336 1.1 christos dre[1].dre_secname = NULL;
337 1.1 christos drs->drs_drecnt += 2;
338 1.1 christos
339 1.1 christos return (DW_DLE_NONE);
340 1.1 christos }
341 1.1 christos
342 1.1 christos int
343 1.1 christos _dwarf_reloc_section_finalize(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
344 1.1 christos Dwarf_Error *error)
345 1.1 christos {
346 1.1 christos Dwarf_P_Section ds;
347 1.1 christos Dwarf_Unsigned unit;
348 1.1 christos int ret, size;
349 1.1 christos
350 1.1 christos assert(dbg != NULL && drs != NULL && drs->drs_ds != NULL &&
351 1.1 christos drs->drs_ref != NULL);
352 1.1 christos
353 1.1 christos ds = drs->drs_ds;
354 1.1 christos
355 1.1 christos /*
356 1.1 christos * Calculate the size (in bytes) of the relocation section.
357 1.1 christos */
358 1.1 christos if (dbg->dbgp_flags & DW_DLC_SIZE_64)
359 1.1 christos unit = drs->drs_addend ? sizeof(Elf64_Rela) : sizeof(Elf64_Rel);
360 1.1 christos else
361 1.1 christos unit = drs->drs_addend ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel);
362 1.1 christos assert(ds->ds_size == 0);
363 1.1 christos size = drs->drs_drecnt * unit;
364 1.1 christos
365 1.1 christos /*
366 1.1 christos * Discard this relocation section if there is no entry in it.
367 1.1 christos */
368 1.1 christos if (size == 0) {
369 1.1 christos _dwarf_reloc_section_free(dbg, &drs);
370 1.1 christos return (DW_DLE_NONE);
371 1.1 christos }
372 1.1 christos
373 1.1 christos /*
374 1.1 christos * If we are under stream mode, realloc the section data block to
375 1.1 christos * this size.
376 1.1 christos */
377 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
378 1.1 christos ds->ds_cap = size;
379 1.1 christos if ((ds->ds_data = realloc(ds->ds_data, (size_t) ds->ds_cap)) ==
380 1.1 christos NULL) {
381 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
382 1.1 christos return (DW_DLE_MEMORY);
383 1.1 christos }
384 1.1 christos }
385 1.1 christos
386 1.1 christos /*
387 1.1 christos * Notify the application the creation of this relocation section.
388 1.1 christos * Note that the section link here should point to the .symtab
389 1.1 christos * section, we set it to 0 since we have no way to know .symtab
390 1.1 christos * section index.
391 1.1 christos */
392 1.1 christos ret = _dwarf_pro_callback(dbg, ds->ds_name, size,
393 1.1 christos drs->drs_addend ? SHT_RELA : SHT_REL, 0, 0, drs->drs_ref->ds_ndx,
394 1.1 christos &ds->ds_symndx, NULL);
395 1.1 christos if (ret < 0) {
396 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_ELF_SECT_ERR);
397 1.1 christos return (DW_DLE_ELF_SECT_ERR);
398 1.1 christos }
399 1.1 christos ds->ds_ndx = ret;
400 1.1 christos
401 1.1 christos return (DW_DLE_NONE);
402 1.1 christos }
403 1.1 christos
404 1.1 christos int
405 1.1 christos _dwarf_reloc_section_gen(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
406 1.1 christos Dwarf_Error *error)
407 1.1 christos {
408 1.1 christos Dwarf_Rel_Entry dre;
409 1.1 christos Dwarf_P_Section ds;
410 1.1 christos Dwarf_Unsigned type;
411 1.1 christos int ret;
412 1.1 christos
413 1.1 christos assert((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0);
414 1.1 christos assert(drs->drs_ds != NULL && drs->drs_ds->ds_size == 0);
415 1.1 christos assert(!STAILQ_EMPTY(&drs->drs_dre));
416 1.1 christos ds = drs->drs_ds;
417 1.1 christos
418 1.1 christos STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
419 1.1 christos assert(dre->dre_length == 4 || dre->dre_length == 8);
420 1.1 christos type = _dwarf_get_reloc_type(dbg, dre->dre_length == 8);
421 1.1 christos if (dbg->dbgp_flags & DW_DLC_SIZE_64) {
422 1.1 christos /* Write r_offset (8 bytes) */
423 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
424 1.1 christos &ds->ds_size, dre->dre_offset, 8, error);
425 1.1 christos if (ret != DW_DLE_NONE)
426 1.1 christos return (ret);
427 1.1 christos /* Write r_info (8 bytes) */
428 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
429 1.1 christos &ds->ds_size, ELF64_R_INFO(dre->dre_symndx, type),
430 1.1 christos 8, error);
431 1.1 christos if (ret != DW_DLE_NONE)
432 1.1 christos return (ret);
433 1.1 christos /* Write r_addend (8 bytes) */
434 1.1 christos if (drs->drs_addend) {
435 1.1 christos ret = dbg->write_alloc(&ds->ds_data,
436 1.1 christos &ds->ds_cap, &ds->ds_size, dre->dre_addend,
437 1.1 christos 8, error);
438 1.1 christos if (ret != DW_DLE_NONE)
439 1.1 christos return (ret);
440 1.1 christos }
441 1.1 christos } else {
442 1.1 christos /* Write r_offset (4 bytes) */
443 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
444 1.1 christos &ds->ds_size, dre->dre_offset, 4, error);
445 1.1 christos if (ret != DW_DLE_NONE)
446 1.1 christos return (ret);
447 1.1 christos /* Write r_info (4 bytes) */
448 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
449 1.1 christos &ds->ds_size, ELF32_R_INFO(dre->dre_symndx, type),
450 1.1 christos 4, error);
451 1.1 christos if (ret != DW_DLE_NONE)
452 1.1 christos return (ret);
453 1.1 christos /* Write r_addend (4 bytes) */
454 1.1 christos if (drs->drs_addend) {
455 1.1 christos ret = dbg->write_alloc(&ds->ds_data,
456 1.1 christos &ds->ds_cap, &ds->ds_size, dre->dre_addend,
457 1.1 christos 4, error);
458 1.1 christos if (ret != DW_DLE_NONE)
459 1.1 christos return (ret);
460 1.1 christos }
461 1.1 christos }
462 1.1 christos }
463 1.1 christos assert(ds->ds_size == ds->ds_cap);
464 1.1 christos
465 1.1 christos return (DW_DLE_NONE);
466 1.1 christos }
467 1.1 christos
468 1.1 christos int
469 1.1 christos _dwarf_reloc_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
470 1.1 christos {
471 1.1 christos Dwarf_Rel_Section drs;
472 1.1 christos Dwarf_Rel_Entry dre;
473 1.1 christos Dwarf_P_Section ds;
474 1.1 christos int ret;
475 1.1 christos
476 1.1 christos STAILQ_FOREACH(drs, &dbg->dbgp_drslist, drs_next) {
477 1.1 christos /*
478 1.1 christos * Update relocation entries: translate any section name
479 1.1 christos * reference to section symbol index.
480 1.1 christos */
481 1.1 christos STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
482 1.1 christos if (dre->dre_secname == NULL)
483 1.1 christos continue;
484 1.1 christos ds = _dwarf_pro_find_section(dbg, dre->dre_secname);
485 1.1 christos assert(ds != NULL && ds->ds_symndx != 0);
486 1.1 christos dre->dre_symndx = ds->ds_symndx;
487 1.1 christos }
488 1.1 christos
489 1.1 christos /*
490 1.1 christos * Generate ELF relocation section if we are under stream
491 1.1 christos * mode.
492 1.1 christos */
493 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
494 1.1 christos ret = _dwarf_reloc_section_gen(dbg, drs, error);
495 1.1 christos if (ret != DW_DLE_NONE)
496 1.1 christos return (ret);
497 1.1 christos }
498 1.1 christos }
499 1.1 christos
500 1.1 christos return (DW_DLE_NONE);
501 1.1 christos }
502 1.1 christos
503 1.1 christos void
504 1.1 christos _dwarf_reloc_cleanup(Dwarf_P_Debug dbg)
505 1.1 christos {
506 1.1 christos Dwarf_Rel_Section drs, tdrs;
507 1.1 christos Dwarf_Rel_Entry dre, tdre;
508 1.1 christos
509 1.1 christos assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
510 1.1 christos
511 1.1 christos STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
512 1.1 christos STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
513 1.1 christos drs_next);
514 1.1 christos free(drs->drs_drd);
515 1.1 christos STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
516 1.1 christos STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
517 1.1 christos dre_next);
518 1.1 christos free(dre);
519 1.1 christos }
520 1.1 christos if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
521 1.1 christos if (drs->drs_ds) {
522 1.1 christos if (drs->drs_ds->ds_name)
523 1.1 christos free(drs->drs_ds->ds_name);
524 1.1 christos free(drs->drs_ds);
525 1.1 christos }
526 1.1 christos }
527 1.1 christos free(drs);
528 1.1 christos }
529 1.1 christos dbg->dbgp_drscnt = 0;
530 1.1 christos dbg->dbgp_drspos = NULL;
531 1.1 christos }
532