1*0a6a1f1dSLionel Sambuc /* $NetBSD: libdwarf_sections.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2010 Kai Wang
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libdwarf_sections.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_sections.c 2379 2012-01-05 02:08:20Z jkoshy ");
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc #define _SECTION_INIT_SIZE 128
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc int
_dwarf_section_init(Dwarf_P_Debug dbg,Dwarf_P_Section * dsp,const char * name,int pseudo,Dwarf_Error * error)37*0a6a1f1dSLionel Sambuc _dwarf_section_init(Dwarf_P_Debug dbg, Dwarf_P_Section *dsp, const char *name,
38*0a6a1f1dSLionel Sambuc int pseudo, Dwarf_Error *error)
39*0a6a1f1dSLionel Sambuc {
40*0a6a1f1dSLionel Sambuc Dwarf_P_Section ds;
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc assert(dbg != NULL && dsp != NULL && name != NULL);
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc if ((ds = calloc(1, sizeof(struct _Dwarf_P_Section))) == NULL) {
45*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
46*0a6a1f1dSLionel Sambuc return (DW_DLE_MEMORY);
47*0a6a1f1dSLionel Sambuc }
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc if ((ds->ds_name = strdup(name)) == NULL) {
50*0a6a1f1dSLionel Sambuc free(ds);
51*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
52*0a6a1f1dSLionel Sambuc return (DW_DLE_MEMORY);
53*0a6a1f1dSLionel Sambuc }
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc if (!pseudo) {
56*0a6a1f1dSLionel Sambuc ds->ds_cap = _SECTION_INIT_SIZE;
57*0a6a1f1dSLionel Sambuc if ((ds->ds_data = malloc((size_t) ds->ds_cap)) == NULL) {
58*0a6a1f1dSLionel Sambuc free(ds->ds_name);
59*0a6a1f1dSLionel Sambuc free(ds);
60*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
61*0a6a1f1dSLionel Sambuc return (DW_DLE_MEMORY);
62*0a6a1f1dSLionel Sambuc }
63*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&dbg->dbgp_seclist, ds, ds_next);
64*0a6a1f1dSLionel Sambuc dbg->dbgp_seccnt++;
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc *dsp = ds;
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc void
_dwarf_section_free(Dwarf_P_Debug dbg,Dwarf_P_Section * dsp)73*0a6a1f1dSLionel Sambuc _dwarf_section_free(Dwarf_P_Debug dbg, Dwarf_P_Section *dsp)
74*0a6a1f1dSLionel Sambuc {
75*0a6a1f1dSLionel Sambuc Dwarf_P_Section ds, tds;
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambuc assert(dbg != NULL && dsp != NULL);
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc if (*dsp == NULL)
80*0a6a1f1dSLionel Sambuc return;
81*0a6a1f1dSLionel Sambuc
82*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(ds, &dbg->dbgp_seclist, ds_next, tds) {
83*0a6a1f1dSLionel Sambuc if (ds == *dsp) {
84*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&dbg->dbgp_seclist, ds, _Dwarf_P_Section,
85*0a6a1f1dSLionel Sambuc ds_next);
86*0a6a1f1dSLionel Sambuc dbg->dbgp_seccnt--;
87*0a6a1f1dSLionel Sambuc break;
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc ds = *dsp;
91*0a6a1f1dSLionel Sambuc if (ds->ds_name)
92*0a6a1f1dSLionel Sambuc free(ds->ds_name);
93*0a6a1f1dSLionel Sambuc if (ds->ds_data)
94*0a6a1f1dSLionel Sambuc free(ds->ds_data);
95*0a6a1f1dSLionel Sambuc free(ds);
96*0a6a1f1dSLionel Sambuc *dsp = NULL;
97*0a6a1f1dSLionel Sambuc }
98*0a6a1f1dSLionel Sambuc
99*0a6a1f1dSLionel Sambuc int
_dwarf_pro_callback(Dwarf_P_Debug dbg,char * name,int size,Dwarf_Unsigned type,Dwarf_Unsigned flags,Dwarf_Unsigned link,Dwarf_Unsigned info,Dwarf_Unsigned * symndx,int * error)100*0a6a1f1dSLionel Sambuc _dwarf_pro_callback(Dwarf_P_Debug dbg, char *name, int size,
101*0a6a1f1dSLionel Sambuc Dwarf_Unsigned type, Dwarf_Unsigned flags, Dwarf_Unsigned link,
102*0a6a1f1dSLionel Sambuc Dwarf_Unsigned info, Dwarf_Unsigned *symndx, int *error)
103*0a6a1f1dSLionel Sambuc {
104*0a6a1f1dSLionel Sambuc int e, ret, isymndx;
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc assert(dbg != NULL && name != NULL && symndx != NULL);
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc if (dbg->dbgp_func_b)
109*0a6a1f1dSLionel Sambuc ret = dbg->dbgp_func_b(name, size, type, flags, link, info,
110*0a6a1f1dSLionel Sambuc symndx, &e);
111*0a6a1f1dSLionel Sambuc else {
112*0a6a1f1dSLionel Sambuc ret = dbg->dbgp_func(name, size, type, flags, link, info,
113*0a6a1f1dSLionel Sambuc &isymndx, &e);
114*0a6a1f1dSLionel Sambuc *symndx = isymndx;
115*0a6a1f1dSLionel Sambuc }
116*0a6a1f1dSLionel Sambuc if (ret < 0) {
117*0a6a1f1dSLionel Sambuc if (error)
118*0a6a1f1dSLionel Sambuc *error = e;
119*0a6a1f1dSLionel Sambuc }
120*0a6a1f1dSLionel Sambuc
121*0a6a1f1dSLionel Sambuc return (ret);
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc
124*0a6a1f1dSLionel Sambuc int
_dwarf_section_callback(Dwarf_P_Debug dbg,Dwarf_P_Section ds,Dwarf_Unsigned type,Dwarf_Unsigned flags,Dwarf_Unsigned link,Dwarf_Unsigned info,Dwarf_Error * error)125*0a6a1f1dSLionel Sambuc _dwarf_section_callback(Dwarf_P_Debug dbg, Dwarf_P_Section ds,
126*0a6a1f1dSLionel Sambuc Dwarf_Unsigned type, Dwarf_Unsigned flags, Dwarf_Unsigned link,
127*0a6a1f1dSLionel Sambuc Dwarf_Unsigned info, Dwarf_Error *error)
128*0a6a1f1dSLionel Sambuc {
129*0a6a1f1dSLionel Sambuc int ret, ndx;
130*0a6a1f1dSLionel Sambuc
131*0a6a1f1dSLionel Sambuc ndx = _dwarf_pro_callback(dbg, ds->ds_name, (int) ds->ds_size,
132*0a6a1f1dSLionel Sambuc type, flags, link, info, &ds->ds_symndx, NULL);
133*0a6a1f1dSLionel Sambuc if (ndx < 0) {
134*0a6a1f1dSLionel Sambuc ret = DW_DLE_ELF_SECT_ERR;
135*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, ret);
136*0a6a1f1dSLionel Sambuc return (ret);
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc ds->ds_ndx = ndx;
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc
143*0a6a1f1dSLionel Sambuc int
_dwarf_generate_sections(Dwarf_P_Debug dbg,Dwarf_Error * error)144*0a6a1f1dSLionel Sambuc _dwarf_generate_sections(Dwarf_P_Debug dbg, Dwarf_Error *error)
145*0a6a1f1dSLionel Sambuc {
146*0a6a1f1dSLionel Sambuc int ret;
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc /* Produce .debug_info section. */
149*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_info_gen(dbg, error)) != DW_DLE_NONE)
150*0a6a1f1dSLionel Sambuc return (ret);
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambuc /* Produce .debug_abbrev section. */
153*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_abbrev_gen(dbg, error)) != DW_DLE_NONE)
154*0a6a1f1dSLionel Sambuc return (ret);
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc /* Produce .debug_line section. */
157*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_lineno_gen(dbg, error)) != DW_DLE_NONE)
158*0a6a1f1dSLionel Sambuc return (ret);
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc /* Produce .debug_frame section. */
161*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_frame_gen(dbg, error)) != DW_DLE_NONE)
162*0a6a1f1dSLionel Sambuc return (ret);
163*0a6a1f1dSLionel Sambuc
164*0a6a1f1dSLionel Sambuc /* Produce .debug_aranges section. */
165*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_arange_gen(dbg, error)) != DW_DLE_NONE)
166*0a6a1f1dSLionel Sambuc return (ret);
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc /* Produce .debug_macinfo section. */
169*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_macinfo_gen(dbg, error)) != DW_DLE_NONE)
170*0a6a1f1dSLionel Sambuc return (ret);
171*0a6a1f1dSLionel Sambuc
172*0a6a1f1dSLionel Sambuc /* Produce .debug_pubnames section. */
173*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_nametbl_gen(dbg, ".debug_pubnames", dbg->dbgp_pubs,
174*0a6a1f1dSLionel Sambuc error)) != DW_DLE_NONE)
175*0a6a1f1dSLionel Sambuc return (ret);
176*0a6a1f1dSLionel Sambuc
177*0a6a1f1dSLionel Sambuc /* Produce .debug_weaknames section. */
178*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_nametbl_gen(dbg, ".debug_weaknames", dbg->dbgp_weaks,
179*0a6a1f1dSLionel Sambuc error)) != DW_DLE_NONE)
180*0a6a1f1dSLionel Sambuc return (ret);
181*0a6a1f1dSLionel Sambuc
182*0a6a1f1dSLionel Sambuc /* Produce .debug_funcnames section. */
183*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_nametbl_gen(dbg, ".debug_funcnames", dbg->dbgp_funcs,
184*0a6a1f1dSLionel Sambuc error)) != DW_DLE_NONE)
185*0a6a1f1dSLionel Sambuc return (ret);
186*0a6a1f1dSLionel Sambuc
187*0a6a1f1dSLionel Sambuc /* Produce .debug_typenames section. */
188*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_nametbl_gen(dbg, ".debug_typenames", dbg->dbgp_types,
189*0a6a1f1dSLionel Sambuc error)) != DW_DLE_NONE)
190*0a6a1f1dSLionel Sambuc return (ret);
191*0a6a1f1dSLionel Sambuc
192*0a6a1f1dSLionel Sambuc /* Produce .debug_varnames section. */
193*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_nametbl_gen(dbg, ".debug_varnames", dbg->dbgp_vars,
194*0a6a1f1dSLionel Sambuc error)) != DW_DLE_NONE)
195*0a6a1f1dSLionel Sambuc return (ret);
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel Sambuc /* Produce .debug_str section. */
198*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_strtab_gen(dbg, error)) != DW_DLE_NONE)
199*0a6a1f1dSLionel Sambuc return (ret);
200*0a6a1f1dSLionel Sambuc
201*0a6a1f1dSLionel Sambuc /* Finally, update and generate all relocation sections. */
202*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_reloc_gen(dbg, error)) != DW_DLE_NONE)
203*0a6a1f1dSLionel Sambuc return (ret);
204*0a6a1f1dSLionel Sambuc
205*0a6a1f1dSLionel Sambuc /* Set section/relocation iterator to the first element. */
206*0a6a1f1dSLionel Sambuc dbg->dbgp_secpos = STAILQ_FIRST(&dbg->dbgp_seclist);
207*0a6a1f1dSLionel Sambuc dbg->dbgp_drspos = STAILQ_FIRST(&dbg->dbgp_drslist);
208*0a6a1f1dSLionel Sambuc
209*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
210*0a6a1f1dSLionel Sambuc }
211*0a6a1f1dSLionel Sambuc
212*0a6a1f1dSLionel Sambuc Dwarf_Section *
_dwarf_find_section(Dwarf_Debug dbg,const char * name)213*0a6a1f1dSLionel Sambuc _dwarf_find_section(Dwarf_Debug dbg, const char *name)
214*0a6a1f1dSLionel Sambuc {
215*0a6a1f1dSLionel Sambuc Dwarf_Section *ds;
216*0a6a1f1dSLionel Sambuc Dwarf_Half i;
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc assert(name != NULL);
219*0a6a1f1dSLionel Sambuc
220*0a6a1f1dSLionel Sambuc for (i = 0; i < dbg->dbg_seccnt; i++) {
221*0a6a1f1dSLionel Sambuc ds = &dbg->dbg_section[i];
222*0a6a1f1dSLionel Sambuc if (ds->ds_name != NULL && !strcmp(ds->ds_name, name))
223*0a6a1f1dSLionel Sambuc return (ds);
224*0a6a1f1dSLionel Sambuc }
225*0a6a1f1dSLionel Sambuc
226*0a6a1f1dSLionel Sambuc return (NULL);
227*0a6a1f1dSLionel Sambuc }
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc Dwarf_P_Section
_dwarf_pro_find_section(Dwarf_P_Debug dbg,const char * name)230*0a6a1f1dSLionel Sambuc _dwarf_pro_find_section(Dwarf_P_Debug dbg, const char *name)
231*0a6a1f1dSLionel Sambuc {
232*0a6a1f1dSLionel Sambuc Dwarf_P_Section ds;
233*0a6a1f1dSLionel Sambuc
234*0a6a1f1dSLionel Sambuc assert(dbg != NULL && name != NULL);
235*0a6a1f1dSLionel Sambuc
236*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(ds, &dbg->dbgp_seclist, ds_next) {
237*0a6a1f1dSLionel Sambuc if (ds->ds_name != NULL && !strcmp(ds->ds_name ,name))
238*0a6a1f1dSLionel Sambuc return (ds);
239*0a6a1f1dSLionel Sambuc }
240*0a6a1f1dSLionel Sambuc
241*0a6a1f1dSLionel Sambuc return (NULL);
242*0a6a1f1dSLionel Sambuc }
243*0a6a1f1dSLionel Sambuc
244*0a6a1f1dSLionel Sambuc void
_dwarf_section_cleanup(Dwarf_P_Debug dbg)245*0a6a1f1dSLionel Sambuc _dwarf_section_cleanup(Dwarf_P_Debug dbg)
246*0a6a1f1dSLionel Sambuc {
247*0a6a1f1dSLionel Sambuc Dwarf_P_Section ds, tds;
248*0a6a1f1dSLionel Sambuc
249*0a6a1f1dSLionel Sambuc assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
250*0a6a1f1dSLionel Sambuc
251*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(ds, &dbg->dbgp_seclist, ds_next, tds) {
252*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&dbg->dbgp_seclist, ds, _Dwarf_P_Section,
253*0a6a1f1dSLionel Sambuc ds_next);
254*0a6a1f1dSLionel Sambuc if (ds->ds_name)
255*0a6a1f1dSLionel Sambuc free(ds->ds_name);
256*0a6a1f1dSLionel Sambuc if (ds->ds_data)
257*0a6a1f1dSLionel Sambuc free(ds->ds_data);
258*0a6a1f1dSLionel Sambuc free(ds);
259*0a6a1f1dSLionel Sambuc }
260*0a6a1f1dSLionel Sambuc dbg->dbgp_seccnt = 0;
261*0a6a1f1dSLionel Sambuc dbg->dbgp_secpos = 0;
262*0a6a1f1dSLionel Sambuc }
263