1*5ac3bc71Schristos /* $NetBSD: dwarf_pro_reloc.c,v 1.5 2024/03/03 17:37:32 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2010 Kai Wang
59dd9d0cfSchristos * All rights reserved.
69dd9d0cfSchristos *
79dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos * modification, are permitted provided that the following conditions
99dd9d0cfSchristos * are met:
109dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos *
169dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos * SUCH DAMAGE.
279dd9d0cfSchristos */
289dd9d0cfSchristos
299dd9d0cfSchristos #include "_libdwarf.h"
309dd9d0cfSchristos
31*5ac3bc71Schristos __RCSID("$NetBSD: dwarf_pro_reloc.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
329dd9d0cfSchristos ELFTC_VCSID("Id: dwarf_pro_reloc.c 2074 2011-10-27 03:34:33Z jkoshy");
339dd9d0cfSchristos
349dd9d0cfSchristos int
dwarf_get_relocation_info_count(Dwarf_P_Debug dbg,Dwarf_Unsigned * reloc_cnt,int * drd_buffer_version,Dwarf_Error * error)359dd9d0cfSchristos dwarf_get_relocation_info_count(Dwarf_P_Debug dbg, Dwarf_Unsigned *reloc_cnt,
369dd9d0cfSchristos int *drd_buffer_version, Dwarf_Error *error)
379dd9d0cfSchristos {
389dd9d0cfSchristos
399dd9d0cfSchristos if (dbg == NULL || reloc_cnt == NULL || drd_buffer_version == NULL) {
409dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
419dd9d0cfSchristos return (DW_DLV_ERROR);
429dd9d0cfSchristos }
439dd9d0cfSchristos
449dd9d0cfSchristos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
459dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
469dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
479dd9d0cfSchristos }
489dd9d0cfSchristos
499dd9d0cfSchristos *reloc_cnt = dbg->dbgp_drscnt;
509dd9d0cfSchristos *drd_buffer_version = DWARF_DRD_BUFFER_VERSION;
519dd9d0cfSchristos
529dd9d0cfSchristos return (DW_DLV_OK);
539dd9d0cfSchristos }
549dd9d0cfSchristos
559dd9d0cfSchristos int
dwarf_get_relocation_info(Dwarf_P_Debug dbg,Dwarf_Signed * elf_section_index,Dwarf_Signed * elf_section_link,Dwarf_Unsigned * reloc_entry_count,Dwarf_Relocation_Data * reloc_buffer,Dwarf_Error * error)569dd9d0cfSchristos dwarf_get_relocation_info(Dwarf_P_Debug dbg, Dwarf_Signed *elf_section_index,
579dd9d0cfSchristos Dwarf_Signed *elf_section_link, Dwarf_Unsigned *reloc_entry_count,
589dd9d0cfSchristos Dwarf_Relocation_Data *reloc_buffer, Dwarf_Error *error)
599dd9d0cfSchristos {
609dd9d0cfSchristos Dwarf_Rel_Section drs;
619dd9d0cfSchristos Dwarf_Rel_Entry dre;
629dd9d0cfSchristos int i;
639dd9d0cfSchristos
649dd9d0cfSchristos if (dbg == NULL || elf_section_index == NULL ||
659dd9d0cfSchristos elf_section_link == NULL || reloc_entry_count == NULL ||
669dd9d0cfSchristos reloc_buffer == NULL) {
679dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
689dd9d0cfSchristos return (DW_DLV_ERROR);
699dd9d0cfSchristos }
709dd9d0cfSchristos
719dd9d0cfSchristos if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
729dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
739dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
749dd9d0cfSchristos }
759dd9d0cfSchristos
769dd9d0cfSchristos if (dbg->dbgp_drscnt == 0) {
779dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
789dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
799dd9d0cfSchristos }
809dd9d0cfSchristos
819dd9d0cfSchristos if (dbg->dbgp_drspos == NULL) {
829dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
839dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
849dd9d0cfSchristos }
859dd9d0cfSchristos
869dd9d0cfSchristos drs = dbg->dbgp_drspos;
879dd9d0cfSchristos assert(drs->drs_ds != NULL && drs->drs_ref != NULL);
889dd9d0cfSchristos assert(drs->drs_drecnt > 0);
899dd9d0cfSchristos
909dd9d0cfSchristos *elf_section_index = drs->drs_ds->ds_ndx;
919dd9d0cfSchristos *elf_section_link = drs->drs_ref->ds_ndx;
929dd9d0cfSchristos *reloc_entry_count = drs->drs_drecnt;
939dd9d0cfSchristos
949dd9d0cfSchristos if (drs->drs_drd == NULL) {
959dd9d0cfSchristos drs->drs_drd = calloc(*reloc_entry_count,
969dd9d0cfSchristos sizeof(struct Dwarf_Relocation_Data_s));
979dd9d0cfSchristos if (drs->drs_drd == NULL) {
989dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
999dd9d0cfSchristos return (DW_DLV_ERROR);
1009dd9d0cfSchristos }
1019dd9d0cfSchristos for (i = 0, dre = STAILQ_FIRST(&drs->drs_dre);
1029dd9d0cfSchristos (Dwarf_Unsigned) i < *reloc_entry_count && dre != NULL;
1039dd9d0cfSchristos i++, dre = STAILQ_NEXT(dre, dre_next)) {
1049dd9d0cfSchristos drs->drs_drd[i].drd_type = dre->dre_type;
1059dd9d0cfSchristos drs->drs_drd[i].drd_length = dre->dre_length;
1069dd9d0cfSchristos drs->drs_drd[i].drd_offset = dre->dre_offset;
1079dd9d0cfSchristos drs->drs_drd[i].drd_symbol_index = dre->dre_symndx;
1089dd9d0cfSchristos }
1099dd9d0cfSchristos assert((Dwarf_Unsigned) i == *reloc_entry_count && dre == NULL);
1109dd9d0cfSchristos }
1119dd9d0cfSchristos
1129dd9d0cfSchristos *reloc_buffer = drs->drs_drd;
1139dd9d0cfSchristos
1149dd9d0cfSchristos dbg->dbgp_drspos = STAILQ_NEXT(dbg->dbgp_drspos, drs_next);
1159dd9d0cfSchristos
1169dd9d0cfSchristos return (DW_DLV_OK);
1179dd9d0cfSchristos }
118