dwarf_pro_reloc.c revision 260684
1169689Skan/*-
2169689Skan * Copyright (c) 2010 Kai Wang
3171825Skan * All rights reserved.
4169689Skan *
5169689Skan * Redistribution and use in source and binary forms, with or without
6169689Skan * modification, are permitted provided that the following conditions
7169689Skan * are met:
8169689Skan * 1. Redistributions of source code must retain the above copyright
9169689Skan *    notice, this list of conditions and the following disclaimer.
10169689Skan * 2. Redistributions in binary form must reproduce the above copyright
11169689Skan *    notice, this list of conditions and the following disclaimer in the
12169689Skan *    documentation and/or other materials provided with the distribution.
13169689Skan *
14169689Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24169689Skan * SUCH DAMAGE.
25169689Skan */
26169689Skan
27169689Skan#include "_libdwarf.h"
28169689Skan
29169689SkanELFTC_VCSID("$Id: dwarf_pro_reloc.c 2074 2011-10-27 03:34:33Z jkoshy $");
30169689Skan
31169689Skanint
32169689Skandwarf_get_relocation_info_count(Dwarf_P_Debug dbg, Dwarf_Unsigned *reloc_cnt,
33169689Skan    int *drd_buffer_version, Dwarf_Error *error)
34169689Skan{
35171825Skan
36169689Skan	if (dbg == NULL || reloc_cnt == NULL || drd_buffer_version == NULL) {
37169689Skan		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
38169689Skan		return (DW_DLV_ERROR);
39169689Skan	}
40169689Skan
41169689Skan	if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
42169689Skan		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
43169689Skan		return (DW_DLV_NO_ENTRY);
44169689Skan	}
45169689Skan
46169689Skan	*reloc_cnt = dbg->dbgp_drscnt;
47	*drd_buffer_version = DWARF_DRD_BUFFER_VERSION;
48
49	return (DW_DLV_OK);
50}
51
52int
53dwarf_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