libdwarf_reloc.c revision 1.5 1 1.5 christos /* $NetBSD: libdwarf_reloc.c,v 1.5 2024/03/03 17:37:32 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.5 christos __RCSID("$NetBSD: libdwarf_reloc.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
32 1.5 christos ELFTC_VCSID("Id: libdwarf_reloc.c 3741 2019-06-07 06:32:01Z jkoshy");
33 1.2 christos
34 1.1 christos Dwarf_Unsigned
35 1.1 christos _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64)
36 1.1 christos {
37 1.1 christos
38 1.1 christos assert(dbg != NULL);
39 1.1 christos
40 1.1 christos switch (dbg->dbgp_isa) {
41 1.3 christos case DW_ISA_AARCH64:
42 1.3 christos return (is64 ? R_AARCH64_ABS64 : R_AARCH64_ABS32);
43 1.1 christos case DW_ISA_X86:
44 1.1 christos return (R_386_32);
45 1.1 christos case DW_ISA_X86_64:
46 1.1 christos return (is64 ? R_X86_64_64 : R_X86_64_32);
47 1.1 christos case DW_ISA_SPARC:
48 1.1 christos return (is64 ? R_SPARC_UA64 : R_SPARC_UA32);
49 1.1 christos case DW_ISA_PPC:
50 1.5 christos return (is64 ? R_PPC64_ADDR64 : R_PPC_ADDR32);
51 1.1 christos case DW_ISA_ARM:
52 1.1 christos return (R_ARM_ABS32);
53 1.1 christos case DW_ISA_MIPS:
54 1.1 christos return (is64 ? R_MIPS_64 : R_MIPS_32);
55 1.5 christos case DW_ISA_RISCV:
56 1.5 christos return (is64 ? R_RISCV_64 : R_RISCV_32);
57 1.1 christos case DW_ISA_IA64:
58 1.1 christos return (is64 ? R_IA_64_DIR64LSB : R_IA_64_DIR32LSB);
59 1.1 christos default:
60 1.1 christos break;
61 1.1 christos }
62 1.1 christos return (0); /* NOT REACHED */
63 1.1 christos }
64 1.1 christos
65 1.1 christos int
66 1.1 christos _dwarf_get_reloc_size(Dwarf_Debug dbg, Dwarf_Unsigned rel_type)
67 1.1 christos {
68 1.1 christos
69 1.1 christos switch (dbg->dbg_machine) {
70 1.1 christos case EM_NONE:
71 1.1 christos break;
72 1.3 christos case EM_AARCH64:
73 1.3 christos if (rel_type == R_AARCH64_ABS32)
74 1.3 christos return (4);
75 1.3 christos else if (rel_type == R_AARCH64_ABS64)
76 1.3 christos return (8);
77 1.3 christos break;
78 1.1 christos case EM_ARM:
79 1.1 christos if (rel_type == R_ARM_ABS32)
80 1.1 christos return (4);
81 1.1 christos break;
82 1.1 christos case EM_386:
83 1.3 christos case EM_IAMCU:
84 1.1 christos if (rel_type == R_386_32)
85 1.1 christos return (4);
86 1.1 christos break;
87 1.1 christos case EM_X86_64:
88 1.1 christos if (rel_type == R_X86_64_32)
89 1.1 christos return (4);
90 1.1 christos else if (rel_type == R_X86_64_64)
91 1.1 christos return (8);
92 1.1 christos break;
93 1.1 christos case EM_SPARC:
94 1.1 christos if (rel_type == R_SPARC_UA32)
95 1.1 christos return (4);
96 1.1 christos else if (rel_type == R_SPARC_UA64)
97 1.1 christos return (8);
98 1.1 christos break;
99 1.1 christos case EM_PPC:
100 1.1 christos if (rel_type == R_PPC_ADDR32)
101 1.1 christos return (4);
102 1.1 christos break;
103 1.5 christos case EM_PPC64:
104 1.5 christos if (rel_type == R_PPC_ADDR32)
105 1.5 christos return (4);
106 1.5 christos else if (rel_type == R_PPC64_ADDR64)
107 1.5 christos return (8);
108 1.5 christos break;
109 1.1 christos case EM_MIPS:
110 1.1 christos if (rel_type == R_MIPS_32)
111 1.1 christos return (4);
112 1.1 christos else if (rel_type == R_MIPS_64)
113 1.1 christos return (8);
114 1.1 christos break;
115 1.5 christos case EM_RISCV:
116 1.5 christos if (rel_type == R_RISCV_32)
117 1.5 christos return (4);
118 1.5 christos else if (rel_type == R_RISCV_64)
119 1.5 christos return (8);
120 1.5 christos break;
121 1.1 christos case EM_IA_64:
122 1.1 christos if (rel_type == R_IA_64_SECREL32LSB)
123 1.1 christos return (4);
124 1.1 christos else if (rel_type == R_IA_64_DIR64LSB)
125 1.1 christos return (8);
126 1.1 christos break;
127 1.1 christos default:
128 1.1 christos break;
129 1.1 christos }
130 1.1 christos
131 1.1 christos /* unknown relocation. */
132 1.1 christos return (0);
133 1.1 christos }
134 1.1 christos
135 1.1 christos int
136 1.1 christos _dwarf_reloc_section_init(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp,
137 1.1 christos Dwarf_P_Section ref, Dwarf_Error *error)
138 1.1 christos {
139 1.1 christos Dwarf_Rel_Section drs;
140 1.1 christos char name[128];
141 1.1 christos int pseudo;
142 1.1 christos
143 1.1 christos assert(dbg != NULL && drsp != NULL && ref != NULL);
144 1.1 christos
145 1.1 christos if ((drs = calloc(1, sizeof(struct _Dwarf_Rel_Section))) == NULL) {
146 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
147 1.1 christos return (DW_DLE_MEMORY);
148 1.1 christos }
149 1.1 christos
150 1.1 christos drs->drs_ref = ref;
151 1.1 christos
152 1.1 christos /*
153 1.1 christos * FIXME The logic here is most likely wrong. It should
154 1.1 christos * be the ISA that determines relocation type.
155 1.1 christos */
156 1.1 christos if (dbg->dbgp_flags & DW_DLC_SIZE_64)
157 1.1 christos drs->drs_addend = 1;
158 1.1 christos else
159 1.1 christos drs->drs_addend = 0;
160 1.1 christos
161 1.1 christos if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS)
162 1.1 christos pseudo = 1;
163 1.1 christos else
164 1.1 christos pseudo = 0;
165 1.1 christos
166 1.1 christos snprintf(name, sizeof(name), "%s%s",
167 1.1 christos drs->drs_addend ? ".rela" : ".rel", ref->ds_name);
168 1.1 christos if (_dwarf_section_init(dbg, &drs->drs_ds, name, pseudo, error) !=
169 1.1 christos DW_DLE_NONE) {
170 1.1 christos free(drs);
171 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
172 1.1 christos return (DW_DLE_MEMORY);
173 1.1 christos }
174 1.1 christos
175 1.1 christos STAILQ_INIT(&drs->drs_dre);
176 1.1 christos STAILQ_INSERT_TAIL(&dbg->dbgp_drslist, drs, drs_next);
177 1.1 christos dbg->dbgp_drscnt++;
178 1.1 christos *drsp = drs;
179 1.1 christos
180 1.1 christos return (DW_DLE_NONE);
181 1.1 christos }
182 1.1 christos
183 1.1 christos void
184 1.1 christos _dwarf_reloc_section_free(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp)
185 1.1 christos {
186 1.1 christos Dwarf_Rel_Section drs, tdrs;
187 1.1 christos Dwarf_Rel_Entry dre, tdre;
188 1.1 christos
189 1.1 christos assert(dbg != NULL && drsp != NULL);
190 1.1 christos
191 1.1 christos if (*drsp == NULL)
192 1.1 christos return;
193 1.1 christos
194 1.1 christos STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
195 1.1 christos if (drs != *drsp)
196 1.1 christos continue;
197 1.1 christos STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
198 1.1 christos drs_next);
199 1.1 christos STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
200 1.1 christos STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
201 1.1 christos dre_next);
202 1.1 christos free(dre);
203 1.1 christos }
204 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0)
205 1.1 christos _dwarf_section_free(dbg, &drs->drs_ds);
206 1.1 christos else {
207 1.1 christos if (drs->drs_ds->ds_name)
208 1.1 christos free(drs->drs_ds->ds_name);
209 1.1 christos free(drs->drs_ds);
210 1.1 christos }
211 1.1 christos free(drs);
212 1.1 christos *drsp = NULL;
213 1.1 christos dbg->dbgp_drscnt--;
214 1.1 christos break;
215 1.1 christos }
216 1.1 christos }
217 1.1 christos
218 1.1 christos int
219 1.1 christos _dwarf_reloc_entry_add(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
220 1.1 christos Dwarf_P_Section ds, unsigned char type, unsigned char length,
221 1.1 christos Dwarf_Unsigned offset, Dwarf_Unsigned symndx, Dwarf_Unsigned addend,
222 1.1 christos const char *secname, Dwarf_Error *error)
223 1.1 christos {
224 1.1 christos Dwarf_Rel_Entry dre;
225 1.1 christos Dwarf_Unsigned reloff;
226 1.1 christos int ret;
227 1.1 christos
228 1.1 christos assert(drs != NULL);
229 1.1 christos assert(offset <= ds->ds_size);
230 1.1 christos reloff = offset;
231 1.1 christos
232 1.1 christos /*
233 1.1 christos * If the DW_DLC_SYMBOLIC_RELOCATIONS flag is set or ElfXX_Rel
234 1.1 christos * is used instead of ELfXX_Rela, we need to write the addend
235 1.1 christos * in the storage unit to be relocated. Otherwise write 0 in the
236 1.1 christos * storage unit and the addend will be written into relocation
237 1.1 christos * section later.
238 1.1 christos */
239 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) ||
240 1.1 christos drs->drs_addend == 0)
241 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
242 1.1 christos addend, length, error);
243 1.1 christos else
244 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
245 1.1 christos 0, length, error);
246 1.1 christos if (ret != DW_DLE_NONE)
247 1.1 christos return (ret);
248 1.1 christos if (offset > ds->ds_size)
249 1.1 christos ds->ds_size = offset;
250 1.1 christos
251 1.1 christos if ((dre = calloc(1, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
252 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
253 1.1 christos return (DW_DLE_MEMORY);
254 1.1 christos }
255 1.1 christos STAILQ_INSERT_TAIL(&drs->drs_dre, dre, dre_next);
256 1.1 christos dre->dre_type = type;
257 1.1 christos dre->dre_length = length;
258 1.1 christos dre->dre_offset = reloff;
259 1.1 christos dre->dre_symndx = symndx;
260 1.1 christos dre->dre_addend = addend;
261 1.1 christos dre->dre_secname = secname;
262 1.1 christos drs->drs_drecnt++;
263 1.1 christos
264 1.1 christos return (DW_DLE_NONE);
265 1.1 christos }
266 1.1 christos
267 1.1 christos int
268 1.1 christos _dwarf_reloc_entry_add_pair(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
269 1.1 christos Dwarf_P_Section ds, unsigned char length, Dwarf_Unsigned offset,
270 1.1 christos Dwarf_Unsigned symndx, Dwarf_Unsigned esymndx, Dwarf_Unsigned symoff,
271 1.1 christos Dwarf_Unsigned esymoff, Dwarf_Error *error)
272 1.1 christos {
273 1.1 christos Dwarf_Rel_Entry dre;
274 1.1 christos Dwarf_Unsigned reloff;
275 1.1 christos int ret;
276 1.1 christos
277 1.1 christos assert(drs != NULL);
278 1.1 christos assert(offset <= ds->ds_size);
279 1.1 christos assert(dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS);
280 1.1 christos reloff = offset;
281 1.1 christos
282 1.1 christos /* Write net offset into section stream. */
283 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
284 1.1 christos esymoff - symoff, length, error);
285 1.1 christos if (ret != DW_DLE_NONE)
286 1.1 christos return (ret);
287 1.1 christos if (offset > ds->ds_size)
288 1.1 christos ds->ds_size = offset;
289 1.1 christos
290 1.1 christos if ((dre = calloc(2, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
291 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
292 1.1 christos return (DW_DLE_MEMORY);
293 1.1 christos }
294 1.1 christos STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[0], dre_next);
295 1.1 christos STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[1], dre_next);
296 1.1 christos dre[0].dre_type = dwarf_drt_first_of_length_pair;
297 1.1 christos dre[0].dre_length = length;
298 1.1 christos dre[0].dre_offset = reloff;
299 1.1 christos dre[0].dre_symndx = symndx;
300 1.1 christos dre[0].dre_addend = 0;
301 1.1 christos dre[0].dre_secname = NULL;
302 1.1 christos dre[1].dre_type = dwarf_drt_second_of_length_pair;
303 1.1 christos dre[1].dre_length = length;
304 1.1 christos dre[1].dre_offset = reloff;
305 1.1 christos dre[1].dre_symndx = esymndx;
306 1.1 christos dre[1].dre_addend = 0;
307 1.1 christos dre[1].dre_secname = NULL;
308 1.1 christos drs->drs_drecnt += 2;
309 1.1 christos
310 1.1 christos return (DW_DLE_NONE);
311 1.1 christos }
312 1.1 christos
313 1.1 christos int
314 1.1 christos _dwarf_reloc_section_finalize(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
315 1.1 christos Dwarf_Error *error)
316 1.1 christos {
317 1.1 christos Dwarf_P_Section ds;
318 1.1 christos Dwarf_Unsigned unit;
319 1.1 christos int ret, size;
320 1.1 christos
321 1.1 christos assert(dbg != NULL && drs != NULL && drs->drs_ds != NULL &&
322 1.1 christos drs->drs_ref != NULL);
323 1.1 christos
324 1.1 christos ds = drs->drs_ds;
325 1.1 christos
326 1.1 christos /*
327 1.1 christos * Calculate the size (in bytes) of the relocation section.
328 1.1 christos */
329 1.1 christos if (dbg->dbgp_flags & DW_DLC_SIZE_64)
330 1.1 christos unit = drs->drs_addend ? sizeof(Elf64_Rela) : sizeof(Elf64_Rel);
331 1.1 christos else
332 1.1 christos unit = drs->drs_addend ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel);
333 1.1 christos assert(ds->ds_size == 0);
334 1.1 christos size = drs->drs_drecnt * unit;
335 1.1 christos
336 1.1 christos /*
337 1.1 christos * Discard this relocation section if there is no entry in it.
338 1.1 christos */
339 1.1 christos if (size == 0) {
340 1.1 christos _dwarf_reloc_section_free(dbg, &drs);
341 1.1 christos return (DW_DLE_NONE);
342 1.1 christos }
343 1.1 christos
344 1.1 christos /*
345 1.1 christos * If we are under stream mode, realloc the section data block to
346 1.1 christos * this size.
347 1.1 christos */
348 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
349 1.1 christos ds->ds_cap = size;
350 1.1 christos if ((ds->ds_data = realloc(ds->ds_data, (size_t) ds->ds_cap)) ==
351 1.1 christos NULL) {
352 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
353 1.1 christos return (DW_DLE_MEMORY);
354 1.1 christos }
355 1.1 christos }
356 1.1 christos
357 1.1 christos /*
358 1.1 christos * Notify the application the creation of this relocation section.
359 1.1 christos * Note that the section link here should point to the .symtab
360 1.1 christos * section, we set it to 0 since we have no way to know .symtab
361 1.1 christos * section index.
362 1.1 christos */
363 1.1 christos ret = _dwarf_pro_callback(dbg, ds->ds_name, size,
364 1.1 christos drs->drs_addend ? SHT_RELA : SHT_REL, 0, 0, drs->drs_ref->ds_ndx,
365 1.1 christos &ds->ds_symndx, NULL);
366 1.1 christos if (ret < 0) {
367 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_ELF_SECT_ERR);
368 1.1 christos return (DW_DLE_ELF_SECT_ERR);
369 1.1 christos }
370 1.1 christos ds->ds_ndx = ret;
371 1.1 christos
372 1.1 christos return (DW_DLE_NONE);
373 1.1 christos }
374 1.1 christos
375 1.1 christos int
376 1.1 christos _dwarf_reloc_section_gen(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
377 1.1 christos Dwarf_Error *error)
378 1.1 christos {
379 1.1 christos Dwarf_Rel_Entry dre;
380 1.1 christos Dwarf_P_Section ds;
381 1.1 christos Dwarf_Unsigned type;
382 1.1 christos int ret;
383 1.1 christos
384 1.1 christos assert((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0);
385 1.1 christos assert(drs->drs_ds != NULL && drs->drs_ds->ds_size == 0);
386 1.1 christos assert(!STAILQ_EMPTY(&drs->drs_dre));
387 1.1 christos ds = drs->drs_ds;
388 1.1 christos
389 1.1 christos STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
390 1.1 christos assert(dre->dre_length == 4 || dre->dre_length == 8);
391 1.1 christos type = _dwarf_get_reloc_type(dbg, dre->dre_length == 8);
392 1.1 christos if (dbg->dbgp_flags & DW_DLC_SIZE_64) {
393 1.1 christos /* Write r_offset (8 bytes) */
394 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
395 1.1 christos &ds->ds_size, dre->dre_offset, 8, error);
396 1.1 christos if (ret != DW_DLE_NONE)
397 1.1 christos return (ret);
398 1.1 christos /* Write r_info (8 bytes) */
399 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
400 1.1 christos &ds->ds_size, ELF64_R_INFO(dre->dre_symndx, type),
401 1.1 christos 8, error);
402 1.1 christos if (ret != DW_DLE_NONE)
403 1.1 christos return (ret);
404 1.1 christos /* Write r_addend (8 bytes) */
405 1.1 christos if (drs->drs_addend) {
406 1.1 christos ret = dbg->write_alloc(&ds->ds_data,
407 1.1 christos &ds->ds_cap, &ds->ds_size, dre->dre_addend,
408 1.1 christos 8, error);
409 1.1 christos if (ret != DW_DLE_NONE)
410 1.1 christos return (ret);
411 1.1 christos }
412 1.1 christos } else {
413 1.1 christos /* Write r_offset (4 bytes) */
414 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
415 1.1 christos &ds->ds_size, dre->dre_offset, 4, error);
416 1.1 christos if (ret != DW_DLE_NONE)
417 1.1 christos return (ret);
418 1.1 christos /* Write r_info (4 bytes) */
419 1.1 christos ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
420 1.1 christos &ds->ds_size, ELF32_R_INFO(dre->dre_symndx, type),
421 1.1 christos 4, error);
422 1.1 christos if (ret != DW_DLE_NONE)
423 1.1 christos return (ret);
424 1.1 christos /* Write r_addend (4 bytes) */
425 1.1 christos if (drs->drs_addend) {
426 1.1 christos ret = dbg->write_alloc(&ds->ds_data,
427 1.1 christos &ds->ds_cap, &ds->ds_size, dre->dre_addend,
428 1.1 christos 4, error);
429 1.1 christos if (ret != DW_DLE_NONE)
430 1.1 christos return (ret);
431 1.1 christos }
432 1.1 christos }
433 1.1 christos }
434 1.1 christos assert(ds->ds_size == ds->ds_cap);
435 1.1 christos
436 1.1 christos return (DW_DLE_NONE);
437 1.1 christos }
438 1.1 christos
439 1.1 christos int
440 1.1 christos _dwarf_reloc_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
441 1.1 christos {
442 1.1 christos Dwarf_Rel_Section drs;
443 1.1 christos Dwarf_Rel_Entry dre;
444 1.1 christos Dwarf_P_Section ds;
445 1.1 christos int ret;
446 1.1 christos
447 1.1 christos STAILQ_FOREACH(drs, &dbg->dbgp_drslist, drs_next) {
448 1.1 christos /*
449 1.1 christos * Update relocation entries: translate any section name
450 1.1 christos * reference to section symbol index.
451 1.1 christos */
452 1.1 christos STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
453 1.1 christos if (dre->dre_secname == NULL)
454 1.1 christos continue;
455 1.1 christos ds = _dwarf_pro_find_section(dbg, dre->dre_secname);
456 1.1 christos assert(ds != NULL && ds->ds_symndx != 0);
457 1.1 christos dre->dre_symndx = ds->ds_symndx;
458 1.1 christos }
459 1.1 christos
460 1.1 christos /*
461 1.1 christos * Generate ELF relocation section if we are under stream
462 1.1 christos * mode.
463 1.1 christos */
464 1.1 christos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
465 1.1 christos ret = _dwarf_reloc_section_gen(dbg, drs, error);
466 1.1 christos if (ret != DW_DLE_NONE)
467 1.1 christos return (ret);
468 1.1 christos }
469 1.1 christos }
470 1.1 christos
471 1.1 christos return (DW_DLE_NONE);
472 1.1 christos }
473 1.1 christos
474 1.1 christos void
475 1.1 christos _dwarf_reloc_cleanup(Dwarf_P_Debug dbg)
476 1.1 christos {
477 1.1 christos Dwarf_Rel_Section drs, tdrs;
478 1.1 christos Dwarf_Rel_Entry dre, tdre;
479 1.1 christos
480 1.1 christos assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
481 1.1 christos
482 1.1 christos STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
483 1.1 christos STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
484 1.1 christos drs_next);
485 1.1 christos free(drs->drs_drd);
486 1.1 christos STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
487 1.1 christos STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
488 1.1 christos dre_next);
489 1.1 christos free(dre);
490 1.1 christos }
491 1.1 christos if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
492 1.1 christos if (drs->drs_ds) {
493 1.1 christos if (drs->drs_ds->ds_name)
494 1.1 christos free(drs->drs_ds->ds_name);
495 1.1 christos free(drs->drs_ds);
496 1.1 christos }
497 1.1 christos }
498 1.1 christos free(drs);
499 1.1 christos }
500 1.1 christos dbg->dbgp_drscnt = 0;
501 1.1 christos dbg->dbgp_drspos = NULL;
502 1.1 christos }
503