1 /*- 2 * Copyright (c) 2010 Kai Wang 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include "_libdwarf.h" 28 29 ELFTC_VCSID("$Id: dwarf_pro_reloc.c 2074 2011-10-27 03:34:33Z jkoshy $"); 30 31 int 32 dwarf_get_relocation_info_count(Dwarf_P_Debug dbg, Dwarf_Unsigned *reloc_cnt, 33 int *drd_buffer_version, Dwarf_Error *error) 34 { 35 36 if (dbg == NULL || reloc_cnt == NULL || drd_buffer_version == NULL) { 37 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 38 return (DW_DLV_ERROR); 39 } 40 41 if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) { 42 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 43 return (DW_DLV_NO_ENTRY); 44 } 45 46 *reloc_cnt = dbg->dbgp_drscnt; 47 *drd_buffer_version = DWARF_DRD_BUFFER_VERSION; 48 49 return (DW_DLV_OK); 50 } 51 52 int 53 dwarf_get_relocation_info(Dwarf_P_Debug dbg, Dwarf_Signed *elf_section_index, 54 Dwarf_Signed *elf_section_link, Dwarf_Unsigned *reloc_entry_count, 55 Dwarf_Relocation_Data *reloc_buffer, Dwarf_Error *error) 56 { 57 Dwarf_Rel_Section drs; 58 Dwarf_Rel_Entry dre; 59 int i; 60 61 if (dbg == NULL || elf_section_index == NULL || 62 elf_section_link == NULL || reloc_entry_count == NULL || 63 reloc_buffer == NULL) { 64 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 65 return (DW_DLV_ERROR); 66 } 67 68 if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) { 69 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 70 return (DW_DLV_NO_ENTRY); 71 } 72 73 if (dbg->dbgp_drscnt == 0) { 74 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 75 return (DW_DLV_NO_ENTRY); 76 } 77 78 if (dbg->dbgp_drspos == NULL) { 79 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 80 return (DW_DLV_NO_ENTRY); 81 } 82 83 drs = dbg->dbgp_drspos; 84 assert(drs->drs_ds != NULL && drs->drs_ref != NULL); 85 assert(drs->drs_drecnt > 0); 86 87 *elf_section_index = drs->drs_ds->ds_ndx; 88 *elf_section_link = drs->drs_ref->ds_ndx; 89 *reloc_entry_count = drs->drs_drecnt; 90 91 if (drs->drs_drd == NULL) { 92 drs->drs_drd = calloc(*reloc_entry_count, 93 sizeof(struct Dwarf_Relocation_Data_s)); 94 if (drs->drs_drd == NULL) { 95 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 96 return (DW_DLV_ERROR); 97 } 98 for (i = 0, dre = STAILQ_FIRST(&drs->drs_dre); 99 (Dwarf_Unsigned) i < *reloc_entry_count && dre != NULL; 100 i++, dre = STAILQ_NEXT(dre, dre_next)) { 101 drs->drs_drd[i].drd_type = dre->dre_type; 102 drs->drs_drd[i].drd_length = dre->dre_length; 103 drs->drs_drd[i].drd_offset = dre->dre_offset; 104 drs->drs_drd[i].drd_symbol_index = dre->dre_symndx; 105 } 106 assert((Dwarf_Unsigned) i == *reloc_entry_count && dre == NULL); 107 } 108 109 *reloc_buffer = drs->drs_drd; 110 111 dbg->dbgp_drspos = STAILQ_NEXT(dbg->dbgp_drspos, drs_next); 112 113 return (DW_DLV_OK); 114 } 115