2 * Copyright (c) 2010 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: libdwarf_reloc.c 3741 2019-06-07 06:32:01Z jkoshy $");
32 _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64)
37 switch (dbg->dbgp_isa) {
39 return (is64 ? R_AARCH64_ABS64 : R_AARCH64_ABS32);
43 return (is64 ? R_X86_64_64 : R_X86_64_32);
45 return (is64 ? R_SPARC_UA64 : R_SPARC_UA32);
47 return (is64 ? R_PPC64_ADDR64 : R_PPC_ADDR32);
51 return (is64 ? R_MIPS_64 : R_MIPS_32);
53 return (is64 ? R_RISCV_64 : R_RISCV_32);
55 return (is64 ? R_IA_64_DIR64LSB : R_IA_64_DIR32LSB);
59 return (0); /* NOT REACHED */
63 _dwarf_get_reloc_size(Dwarf_Debug dbg, Dwarf_Unsigned rel_type)
66 switch (dbg->dbg_machine) {
70 if (rel_type == R_AARCH64_ABS32)
72 else if (rel_type == R_AARCH64_ABS64)
76 if (rel_type == R_ARM_ABS32)
81 if (rel_type == R_386_32)
85 if (rel_type == R_X86_64_32)
87 else if (rel_type == R_X86_64_64)
91 if (rel_type == R_SPARC_UA32)
93 else if (rel_type == R_SPARC_UA64)
97 if (rel_type == R_PPC_ADDR32)
101 if (rel_type == R_PPC_ADDR32)
103 else if (rel_type == R_PPC64_ADDR64)
107 if (rel_type == R_MIPS_32)
109 else if (rel_type == R_MIPS_64)
113 if (rel_type == R_RISCV_32)
115 else if (rel_type == R_RISCV_64)
119 if (rel_type == R_IA_64_SECREL32LSB)
121 else if (rel_type == R_IA_64_DIR64LSB)
128 /* unknown relocation. */
133 _dwarf_reloc_section_init(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp,
134 Dwarf_P_Section ref, Dwarf_Error *error)
136 Dwarf_Rel_Section drs;
140 assert(dbg != NULL && drsp != NULL && ref != NULL);
142 if ((drs = calloc(1, sizeof(struct _Dwarf_Rel_Section))) == NULL) {
143 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
144 return (DW_DLE_MEMORY);
150 * FIXME The logic here is most likely wrong. It should
151 * be the ISA that determines relocation type.
153 if (dbg->dbgp_flags & DW_DLC_SIZE_64)
158 if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS)
163 snprintf(name, sizeof(name), "%s%s",
164 drs->drs_addend ? ".rela" : ".rel", ref->ds_name);
165 if (_dwarf_section_init(dbg, &drs->drs_ds, name, pseudo, error) !=
168 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
169 return (DW_DLE_MEMORY);
172 STAILQ_INIT(&drs->drs_dre);
173 STAILQ_INSERT_TAIL(&dbg->dbgp_drslist, drs, drs_next);
177 return (DW_DLE_NONE);
181 _dwarf_reloc_section_free(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp)
183 Dwarf_Rel_Section drs, tdrs;
184 Dwarf_Rel_Entry dre, tdre;
186 assert(dbg != NULL && drsp != NULL);
191 STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
194 STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
196 STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
197 STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
201 if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0)
202 _dwarf_section_free(dbg, &drs->drs_ds);
204 if (drs->drs_ds->ds_name)
205 free(drs->drs_ds->ds_name);
216 _dwarf_reloc_entry_add(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
217 Dwarf_P_Section ds, unsigned char type, unsigned char length,
218 Dwarf_Unsigned offset, Dwarf_Unsigned symndx, Dwarf_Unsigned addend,
219 const char *secname, Dwarf_Error *error)
222 Dwarf_Unsigned reloff;
226 assert(offset <= ds->ds_size);
230 * If the DW_DLC_SYMBOLIC_RELOCATIONS flag is set or ElfXX_Rel
231 * is used instead of ELfXX_Rela, we need to write the addend
232 * in the storage unit to be relocated. Otherwise write 0 in the
233 * storage unit and the addend will be written into relocation
236 if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) ||
237 drs->drs_addend == 0)
238 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
239 addend, length, error);
241 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
243 if (ret != DW_DLE_NONE)
245 if (offset > ds->ds_size)
246 ds->ds_size = offset;
248 if ((dre = calloc(1, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
249 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
250 return (DW_DLE_MEMORY);
252 STAILQ_INSERT_TAIL(&drs->drs_dre, dre, dre_next);
253 dre->dre_type = type;
254 dre->dre_length = length;
255 dre->dre_offset = reloff;
256 dre->dre_symndx = symndx;
257 dre->dre_addend = addend;
258 dre->dre_secname = secname;
261 return (DW_DLE_NONE);
265 _dwarf_reloc_entry_add_pair(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
266 Dwarf_P_Section ds, unsigned char length, Dwarf_Unsigned offset,
267 Dwarf_Unsigned symndx, Dwarf_Unsigned esymndx, Dwarf_Unsigned symoff,
268 Dwarf_Unsigned esymoff, Dwarf_Error *error)
271 Dwarf_Unsigned reloff;
275 assert(offset <= ds->ds_size);
276 assert(dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS);
279 /* Write net offset into section stream. */
280 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
281 esymoff - symoff, length, error);
282 if (ret != DW_DLE_NONE)
284 if (offset > ds->ds_size)
285 ds->ds_size = offset;
287 if ((dre = calloc(2, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
288 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
289 return (DW_DLE_MEMORY);
291 STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[0], dre_next);
292 STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[1], dre_next);
293 dre[0].dre_type = dwarf_drt_first_of_length_pair;
294 dre[0].dre_length = length;
295 dre[0].dre_offset = reloff;
296 dre[0].dre_symndx = symndx;
297 dre[0].dre_addend = 0;
298 dre[0].dre_secname = NULL;
299 dre[1].dre_type = dwarf_drt_second_of_length_pair;
300 dre[1].dre_length = length;
301 dre[1].dre_offset = reloff;
302 dre[1].dre_symndx = esymndx;
303 dre[1].dre_addend = 0;
304 dre[1].dre_secname = NULL;
305 drs->drs_drecnt += 2;
307 return (DW_DLE_NONE);
311 _dwarf_reloc_section_finalize(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
318 assert(dbg != NULL && drs != NULL && drs->drs_ds != NULL &&
319 drs->drs_ref != NULL);
324 * Calculate the size (in bytes) of the relocation section.
326 if (dbg->dbgp_flags & DW_DLC_SIZE_64)
327 unit = drs->drs_addend ? sizeof(Elf64_Rela) : sizeof(Elf64_Rel);
329 unit = drs->drs_addend ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel);
330 assert(ds->ds_size == 0);
331 size = drs->drs_drecnt * unit;
334 * Discard this relocation section if there is no entry in it.
337 _dwarf_reloc_section_free(dbg, &drs);
338 return (DW_DLE_NONE);
342 * If we are under stream mode, realloc the section data block to
345 if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
347 if ((ds->ds_data = realloc(ds->ds_data, (size_t) ds->ds_cap)) ==
349 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
350 return (DW_DLE_MEMORY);
355 * Notify the application the creation of this relocation section.
356 * Note that the section link here should point to the .symtab
357 * section, we set it to 0 since we have no way to know .symtab
360 ret = _dwarf_pro_callback(dbg, ds->ds_name, size,
361 drs->drs_addend ? SHT_RELA : SHT_REL, 0, 0, drs->drs_ref->ds_ndx,
362 &ds->ds_symndx, NULL);
364 DWARF_SET_ERROR(dbg, error, DW_DLE_ELF_SECT_ERR);
365 return (DW_DLE_ELF_SECT_ERR);
369 return (DW_DLE_NONE);
373 _dwarf_reloc_section_gen(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
381 assert((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0);
382 assert(drs->drs_ds != NULL && drs->drs_ds->ds_size == 0);
383 assert(!STAILQ_EMPTY(&drs->drs_dre));
386 STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
387 assert(dre->dre_length == 4 || dre->dre_length == 8);
388 type = _dwarf_get_reloc_type(dbg, dre->dre_length == 8);
389 if (dbg->dbgp_flags & DW_DLC_SIZE_64) {
390 /* Write r_offset (8 bytes) */
391 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
392 &ds->ds_size, dre->dre_offset, 8, error);
393 if (ret != DW_DLE_NONE)
395 /* Write r_info (8 bytes) */
396 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
397 &ds->ds_size, ELF64_R_INFO(dre->dre_symndx, type),
399 if (ret != DW_DLE_NONE)
401 /* Write r_addend (8 bytes) */
402 if (drs->drs_addend) {
403 ret = dbg->write_alloc(&ds->ds_data,
404 &ds->ds_cap, &ds->ds_size, dre->dre_addend,
406 if (ret != DW_DLE_NONE)
410 /* Write r_offset (4 bytes) */
411 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
412 &ds->ds_size, dre->dre_offset, 4, error);
413 if (ret != DW_DLE_NONE)
415 /* Write r_info (4 bytes) */
416 ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
417 &ds->ds_size, ELF32_R_INFO(dre->dre_symndx, type),
419 if (ret != DW_DLE_NONE)
421 /* Write r_addend (4 bytes) */
422 if (drs->drs_addend) {
423 ret = dbg->write_alloc(&ds->ds_data,
424 &ds->ds_cap, &ds->ds_size, dre->dre_addend,
426 if (ret != DW_DLE_NONE)
431 assert(ds->ds_size == ds->ds_cap);
433 return (DW_DLE_NONE);
437 _dwarf_reloc_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
439 Dwarf_Rel_Section drs;
444 STAILQ_FOREACH(drs, &dbg->dbgp_drslist, drs_next) {
446 * Update relocation entries: translate any section name
447 * reference to section symbol index.
449 STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
450 if (dre->dre_secname == NULL)
452 ds = _dwarf_pro_find_section(dbg, dre->dre_secname);
453 assert(ds != NULL && ds->ds_symndx != 0);
454 dre->dre_symndx = ds->ds_symndx;
458 * Generate ELF relocation section if we are under stream
461 if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
462 ret = _dwarf_reloc_section_gen(dbg, drs, error);
463 if (ret != DW_DLE_NONE)
468 return (DW_DLE_NONE);
472 _dwarf_reloc_cleanup(Dwarf_P_Debug dbg)
474 Dwarf_Rel_Section drs, tdrs;
475 Dwarf_Rel_Entry dre, tdre;
477 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
479 STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
480 STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
483 STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
484 STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
488 if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
490 if (drs->drs_ds->ds_name)
491 free(drs->drs_ds->ds_name);
497 dbg->dbgp_drscnt = 0;
498 dbg->dbgp_drspos = NULL;