1 /* $NetBSD: libdwarf_sections.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2010 Kai Wang
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "_libdwarf.h"
31 __RCSID("$NetBSD: libdwarf_sections.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32 ELFTC_VCSID("Id: libdwarf_sections.c 2379 2012-01-05 02:08:20Z jkoshy ");
34 #define _SECTION_INIT_SIZE 128
37 _dwarf_section_init(Dwarf_P_Debug dbg
, Dwarf_P_Section
*dsp
, const char *name
,
38 int pseudo
, Dwarf_Error
*error
)
42 assert(dbg
!= NULL
&& dsp
!= NULL
&& name
!= NULL
);
44 if ((ds
= calloc(1, sizeof(struct _Dwarf_P_Section
))) == NULL
) {
45 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
46 return (DW_DLE_MEMORY
);
49 if ((ds
->ds_name
= strdup(name
)) == NULL
) {
51 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
52 return (DW_DLE_MEMORY
);
56 ds
->ds_cap
= _SECTION_INIT_SIZE
;
57 if ((ds
->ds_data
= malloc((size_t) ds
->ds_cap
)) == NULL
) {
60 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
61 return (DW_DLE_MEMORY
);
63 STAILQ_INSERT_TAIL(&dbg
->dbgp_seclist
, ds
, ds_next
);
73 _dwarf_section_free(Dwarf_P_Debug dbg
, Dwarf_P_Section
*dsp
)
75 Dwarf_P_Section ds
, tds
;
77 assert(dbg
!= NULL
&& dsp
!= NULL
);
82 STAILQ_FOREACH_SAFE(ds
, &dbg
->dbgp_seclist
, ds_next
, tds
) {
84 STAILQ_REMOVE(&dbg
->dbgp_seclist
, ds
, _Dwarf_P_Section
,
100 _dwarf_pro_callback(Dwarf_P_Debug dbg
, char *name
, int size
,
101 Dwarf_Unsigned type
, Dwarf_Unsigned flags
, Dwarf_Unsigned link
,
102 Dwarf_Unsigned info
, Dwarf_Unsigned
*symndx
, int *error
)
106 assert(dbg
!= NULL
&& name
!= NULL
&& symndx
!= NULL
);
108 if (dbg
->dbgp_func_b
)
109 ret
= dbg
->dbgp_func_b(name
, size
, type
, flags
, link
, info
,
112 ret
= dbg
->dbgp_func(name
, size
, type
, flags
, link
, info
,
125 _dwarf_section_callback(Dwarf_P_Debug dbg
, Dwarf_P_Section ds
,
126 Dwarf_Unsigned type
, Dwarf_Unsigned flags
, Dwarf_Unsigned link
,
127 Dwarf_Unsigned info
, Dwarf_Error
*error
)
131 ndx
= _dwarf_pro_callback(dbg
, ds
->ds_name
, (int) ds
->ds_size
,
132 type
, flags
, link
, info
, &ds
->ds_symndx
, NULL
);
134 ret
= DW_DLE_ELF_SECT_ERR
;
135 DWARF_SET_ERROR(dbg
, error
, ret
);
140 return (DW_DLE_NONE
);
144 _dwarf_generate_sections(Dwarf_P_Debug dbg
, Dwarf_Error
*error
)
148 /* Produce .debug_info section. */
149 if ((ret
= _dwarf_info_gen(dbg
, error
)) != DW_DLE_NONE
)
152 /* Produce .debug_abbrev section. */
153 if ((ret
= _dwarf_abbrev_gen(dbg
, error
)) != DW_DLE_NONE
)
156 /* Produce .debug_line section. */
157 if ((ret
= _dwarf_lineno_gen(dbg
, error
)) != DW_DLE_NONE
)
160 /* Produce .debug_frame section. */
161 if ((ret
= _dwarf_frame_gen(dbg
, error
)) != DW_DLE_NONE
)
164 /* Produce .debug_aranges section. */
165 if ((ret
= _dwarf_arange_gen(dbg
, error
)) != DW_DLE_NONE
)
168 /* Produce .debug_macinfo section. */
169 if ((ret
= _dwarf_macinfo_gen(dbg
, error
)) != DW_DLE_NONE
)
172 /* Produce .debug_pubnames section. */
173 if ((ret
= _dwarf_nametbl_gen(dbg
, ".debug_pubnames", dbg
->dbgp_pubs
,
174 error
)) != DW_DLE_NONE
)
177 /* Produce .debug_weaknames section. */
178 if ((ret
= _dwarf_nametbl_gen(dbg
, ".debug_weaknames", dbg
->dbgp_weaks
,
179 error
)) != DW_DLE_NONE
)
182 /* Produce .debug_funcnames section. */
183 if ((ret
= _dwarf_nametbl_gen(dbg
, ".debug_funcnames", dbg
->dbgp_funcs
,
184 error
)) != DW_DLE_NONE
)
187 /* Produce .debug_typenames section. */
188 if ((ret
= _dwarf_nametbl_gen(dbg
, ".debug_typenames", dbg
->dbgp_types
,
189 error
)) != DW_DLE_NONE
)
192 /* Produce .debug_varnames section. */
193 if ((ret
= _dwarf_nametbl_gen(dbg
, ".debug_varnames", dbg
->dbgp_vars
,
194 error
)) != DW_DLE_NONE
)
197 /* Produce .debug_str section. */
198 if ((ret
= _dwarf_strtab_gen(dbg
, error
)) != DW_DLE_NONE
)
201 /* Finally, update and generate all relocation sections. */
202 if ((ret
= _dwarf_reloc_gen(dbg
, error
)) != DW_DLE_NONE
)
205 /* Set section/relocation iterator to the first element. */
206 dbg
->dbgp_secpos
= STAILQ_FIRST(&dbg
->dbgp_seclist
);
207 dbg
->dbgp_drspos
= STAILQ_FIRST(&dbg
->dbgp_drslist
);
209 return (DW_DLE_NONE
);
213 _dwarf_find_section(Dwarf_Debug dbg
, const char *name
)
218 assert(name
!= NULL
);
220 for (i
= 0; i
< dbg
->dbg_seccnt
; i
++) {
221 ds
= &dbg
->dbg_section
[i
];
222 if (ds
->ds_name
!= NULL
&& !strcmp(ds
->ds_name
, name
))
230 _dwarf_pro_find_section(Dwarf_P_Debug dbg
, const char *name
)
234 assert(dbg
!= NULL
&& name
!= NULL
);
236 STAILQ_FOREACH(ds
, &dbg
->dbgp_seclist
, ds_next
) {
237 if (ds
->ds_name
!= NULL
&& !strcmp(ds
->ds_name
,name
))
245 _dwarf_section_cleanup(Dwarf_P_Debug dbg
)
247 Dwarf_P_Section ds
, tds
;
249 assert(dbg
!= NULL
&& dbg
->dbg_mode
== DW_DLC_WRITE
);
251 STAILQ_FOREACH_SAFE(ds
, &dbg
->dbgp_seclist
, ds_next
, tds
) {
252 STAILQ_REMOVE(&dbg
->dbgp_seclist
, ds
, _Dwarf_P_Section
,
260 dbg
->dbgp_seccnt
= 0;
261 dbg
->dbgp_secpos
= 0;