xref: /titanic_41/usr/src/tools/ctf/dwarf/common/pro_reloc.c (revision c037192b037119c9fd354732fc29b38ce097d356)
1 /*
2 
3   Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
4   Portions Copyright 2008-2010 David Anderson, Inc. All rights reserved.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of version 2.1 of the GNU Lesser General Public License
8   as published by the Free Software Foundation.
9 
10   This program is distributed in the hope that it would be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 
14   Further, this software is distributed without any warranty that it is
15   free of the rightful claim of any third person regarding infringement
16   or the like.  Any license provided herein, whether implied or
17   otherwise, applies only to this software file.  Patent licenses, if
18   any, provided herein do not apply to combinations of this program with
19   other software, or any other product whatsoever.
20 
21   You should have received a copy of the GNU Lesser General Public
22   License along with this program; if not, write the Free Software
23   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24   USA.
25 
26   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
27   Mountain View, CA 94043, or:
28 
29   http://www.sgi.com
30 
31   For further information regarding this notice, see:
32 
33   http://oss.sgi.com/projects/GenInfo/NoticeExplan
34 
35 */
36 
37 
38 
39 #include "config.h"
40 #include "libdwarfdefs.h"
41 #include <stdio.h>
42 #include <string.h>
43 /*#include <elfaccess.h> */
44 #include "pro_incl.h"
45 
46 
47 /*Do initial alloc of newslots slots.
48   Fails only if malloc fails.
49 
50   Supposed to be called before any relocs allocated.
51   Ignored if after any allocated.
52 
53   Part of an optimization, so that for a known 'newslots'
54   relocations count we can preallocate the right size block.
55   Called from just 2 places.
56 
57   returns DW_DLV_OK or  DW_DLV_ERROR
58 */
59 int
60 _dwarf_pro_pre_alloc_n_reloc_slots(Dwarf_P_Debug dbg,
61     int rel_sec_index,
62     Dwarf_Unsigned newslots)
63 {
64     unsigned long len = 0;
65     struct Dwarf_P_Relocation_Block_s *data = 0;
66     Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[rel_sec_index];
67     unsigned long slots_in_blk = (unsigned long) newslots;
68     unsigned long rel_rec_size = dbg->de_relocation_record_size;
69 
70     if (prel->pr_first_block)
71         return DW_DLV_OK;       /* do nothing */
72 
73     len = sizeof(struct Dwarf_P_Relocation_Block_s) +
74         slots_in_blk * rel_rec_size;
75 
76 
77     data = (struct Dwarf_P_Relocation_Block_s *)
78         _dwarf_p_get_alloc(dbg, len);
79     if (!data) {
80         return DW_DLV_ERROR;
81     }
82     data->rb_slots_in_block = slots_in_blk;     /* could use default
83                                                    here, as fallback in
84                                                    case our origininal
85                                                    estimate wrong. When
86                                                    we call this we
87                                                    presumably know what
88                                                    we are doing, so
89                                                    keep this count for
90                                                    now */
91     data->rb_next_slot_to_use = 0;
92     data->rb_where_to_add_next =
93         ((char *) data) + sizeof(struct Dwarf_P_Relocation_Block_s);
94     data->rb_data = data->rb_where_to_add_next;
95 
96     prel->pr_first_block = data;
97     prel->pr_last_block = data;
98     prel->pr_block_count = 1;
99 
100 
101     return DW_DLV_OK;
102 }
103 
104 
105 /*Do alloc of slots.
106   Fails only if malloc fails.
107 
108   Only allocator used.
109 
110   returns DW_DLV_OK or  DW_DLV_ERROR
111 */
112 int
113 _dwarf_pro_alloc_reloc_slots(Dwarf_P_Debug dbg, int rel_sec_index)
114 {
115     unsigned long len = 0;
116     struct Dwarf_P_Relocation_Block_s *data = 0;
117     Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[rel_sec_index];
118     unsigned long slots_in_blk = prel->pr_slots_per_block_to_alloc;
119     unsigned long rel_rec_size = dbg->de_relocation_record_size;
120 
121     len = sizeof(struct Dwarf_P_Relocation_Block_s) +
122         slots_in_blk * rel_rec_size;
123 
124     data = (struct Dwarf_P_Relocation_Block_s *)
125         _dwarf_p_get_alloc(dbg, len);
126     if (!data) {
127         return DW_DLV_ERROR;
128     }
129 
130     if (prel->pr_first_block) {
131         prel->pr_last_block->rb_next = data;
132         prel->pr_last_block = data;
133         prel->pr_block_count += 1;
134 
135     } else {
136 
137         prel->pr_first_block = data;
138         prel->pr_last_block = data;
139         prel->pr_block_count = 1;
140     }
141 
142     data->rb_slots_in_block = slots_in_blk;
143     data->rb_next_slot_to_use = 0;
144     data->rb_where_to_add_next =
145         ((char *) data) + sizeof(struct Dwarf_P_Relocation_Block_s);
146     data->rb_data = data->rb_where_to_add_next;
147 
148     return DW_DLV_OK;
149 
150 }
151 
152 /*
153         Reserve a slot. return DW_DLV_OK if succeeds.
154 
155         Return DW_DLV_ERROR if fails (malloc error).
156 
157         Use the relrec_to_fill to pass back a pointer to
158         a slot space to use.
159 */
160 int
161 _dwarf_pro_reloc_get_a_slot(Dwarf_P_Debug dbg,
162     int base_sec_index, void **relrec_to_fill)
163 {
164     struct Dwarf_P_Relocation_Block_s *data = 0;
165     Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[base_sec_index];
166     unsigned long rel_rec_size = dbg->de_relocation_record_size;
167 
168     char *ret_addr = 0;
169 
170     data = prel->pr_last_block;
171     if ((data == 0) ||
172         (data->rb_next_slot_to_use >= data->rb_slots_in_block)) {
173         int res;
174 
175         res = _dwarf_pro_alloc_reloc_slots(dbg, base_sec_index);
176         if (res != DW_DLV_OK) {
177             return res;
178         }
179     }
180 
181     data = prel->pr_last_block;
182     /* now we have an empty slot */
183     ret_addr = data->rb_where_to_add_next;
184 
185     data->rb_where_to_add_next += rel_rec_size;
186     data->rb_next_slot_to_use += 1;
187 
188     prel->pr_reloc_total_count += 1;
189 
190     *relrec_to_fill = (void *) ret_addr;
191 
192     return DW_DLV_OK;
193 
194 }
195 
196 /*
197    On success  returns count of
198    .rel.* sections that are symbolic
199    thru count_of_relocation_sections.
200 
201    On success, returns DW_DLV_OK.
202 
203    If this is not a 'symbolic' run, returns
204     DW_DLV_NO_ENTRY.
205 
206    No errors are possible.
207 
208 
209 
210 
211 */
212 
213  /*ARGSUSED*/ int
214 dwarf_get_relocation_info_count(Dwarf_P_Debug dbg,
215     Dwarf_Unsigned *
216     count_of_relocation_sections,
217     int *drd_buffer_version,
218     Dwarf_Error * error)
219 {
220     if (dbg->de_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
221         int i;
222         unsigned int count = 0;
223 
224         for (i = 0; i < NUM_DEBUG_SECTIONS; ++i) {
225             if (dbg->de_reloc_sect[i].pr_reloc_total_count > 0) {
226                 ++count;
227             }
228         }
229         *count_of_relocation_sections = (Dwarf_Unsigned) count;
230         *drd_buffer_version = DWARF_DRD_BUFFER_VERSION;
231         return DW_DLV_OK;
232     }
233     return DW_DLV_NO_ENTRY;
234 }
235 
236 int
237 dwarf_get_relocation_info(Dwarf_P_Debug dbg,
238     Dwarf_Signed * elf_section_index,
239     Dwarf_Signed * elf_section_index_link,
240     Dwarf_Unsigned * relocation_buffer_count,
241     Dwarf_Relocation_Data * reldata_buffer,
242     Dwarf_Error * error)
243 {
244     int next = dbg->de_reloc_next_to_return;
245 
246     if (dbg->de_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
247         int i;
248 
249         for (i = next; i < NUM_DEBUG_SECTIONS; ++i) {
250             Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[i];
251 
252             if (prel->pr_reloc_total_count > 0) {
253                 dbg->de_reloc_next_to_return = i + 1;
254 
255 
256                 /* ASSERT: prel->.pr_block_count == 1 */
257 
258                 *elf_section_index = prel->pr_sect_num_of_reloc_sect;
259                 *elf_section_index_link = dbg->de_elf_sects[i];
260                 *relocation_buffer_count = prel->pr_reloc_total_count;
261                 *reldata_buffer = (Dwarf_Relocation_Data)
262                     (prel->pr_first_block->rb_data);
263                 return DW_DLV_OK;
264             }
265         }
266         DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
267     }
268     return DW_DLV_NO_ENTRY;
269 }
270