2 * Copyright (c) 2010 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: libdwarf_sections.c 3041 2014-05-18 15:11:03Z kaiwang27 $");
31 #define _SECTION_INIT_SIZE 128
34 _dwarf_section_init(Dwarf_P_Debug dbg, Dwarf_P_Section *dsp, const char *name,
35 int pseudo, Dwarf_Error *error)
39 assert(dbg != NULL && dsp != NULL && name != NULL);
41 if ((ds = calloc(1, sizeof(struct _Dwarf_P_Section))) == NULL) {
42 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
43 return (DW_DLE_MEMORY);
46 if ((ds->ds_name = strdup(name)) == NULL) {
48 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
49 return (DW_DLE_MEMORY);
53 ds->ds_cap = _SECTION_INIT_SIZE;
54 if ((ds->ds_data = malloc((size_t) ds->ds_cap)) == NULL) {
57 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
58 return (DW_DLE_MEMORY);
60 STAILQ_INSERT_TAIL(&dbg->dbgp_seclist, ds, ds_next);
70 _dwarf_section_free(Dwarf_P_Debug dbg, Dwarf_P_Section *dsp)
72 Dwarf_P_Section ds, tds;
74 assert(dbg != NULL && dsp != NULL);
79 STAILQ_FOREACH_SAFE(ds, &dbg->dbgp_seclist, ds_next, tds) {
81 STAILQ_REMOVE(&dbg->dbgp_seclist, ds, _Dwarf_P_Section,
97 _dwarf_pro_callback(Dwarf_P_Debug dbg, char *name, int size,
98 Dwarf_Unsigned type, Dwarf_Unsigned flags, Dwarf_Unsigned link,
99 Dwarf_Unsigned info, Dwarf_Unsigned *symndx, int *error)
103 assert(dbg != NULL && name != NULL && symndx != NULL);
105 if (dbg->dbgp_func_b)
106 ret = dbg->dbgp_func_b(name, size, type, flags, link, info,
109 ret = dbg->dbgp_func(name, size, type, flags, link, info,
122 _dwarf_section_callback(Dwarf_P_Debug dbg, Dwarf_P_Section ds,
123 Dwarf_Unsigned type, Dwarf_Unsigned flags, Dwarf_Unsigned link,
124 Dwarf_Unsigned info, Dwarf_Error *error)
128 ndx = _dwarf_pro_callback(dbg, ds->ds_name, (int) ds->ds_size,
129 type, flags, link, info, &ds->ds_symndx, NULL);
131 ret = DW_DLE_ELF_SECT_ERR;
132 DWARF_SET_ERROR(dbg, error, ret);
137 return (DW_DLE_NONE);
141 _dwarf_generate_sections(Dwarf_P_Debug dbg, Dwarf_Error *error)
145 /* Produce .debug_info section. */
146 if ((ret = _dwarf_info_gen(dbg, error)) != DW_DLE_NONE)
149 /* Produce .debug_abbrev section. */
150 if ((ret = _dwarf_abbrev_gen(dbg, error)) != DW_DLE_NONE)
153 /* Produce .debug_line section. */
154 if ((ret = _dwarf_lineno_gen(dbg, error)) != DW_DLE_NONE)
157 /* Produce .debug_frame section. */
158 if ((ret = _dwarf_frame_gen(dbg, error)) != DW_DLE_NONE)
161 /* Produce .debug_aranges section. */
162 if ((ret = _dwarf_arange_gen(dbg, error)) != DW_DLE_NONE)
165 /* Produce .debug_macinfo section. */
166 if ((ret = _dwarf_macinfo_gen(dbg, error)) != DW_DLE_NONE)
169 /* Produce .debug_pubnames section. */
170 if ((ret = _dwarf_nametbl_gen(dbg, ".debug_pubnames", dbg->dbgp_pubs,
171 error)) != DW_DLE_NONE)
174 /* Produce .debug_weaknames section. */
175 if ((ret = _dwarf_nametbl_gen(dbg, ".debug_weaknames", dbg->dbgp_weaks,
176 error)) != DW_DLE_NONE)
179 /* Produce .debug_funcnames section. */
180 if ((ret = _dwarf_nametbl_gen(dbg, ".debug_funcnames", dbg->dbgp_funcs,
181 error)) != DW_DLE_NONE)
184 /* Produce .debug_typenames section. */
185 if ((ret = _dwarf_nametbl_gen(dbg, ".debug_typenames", dbg->dbgp_types,
186 error)) != DW_DLE_NONE)
189 /* Produce .debug_varnames section. */
190 if ((ret = _dwarf_nametbl_gen(dbg, ".debug_varnames", dbg->dbgp_vars,
191 error)) != DW_DLE_NONE)
194 /* Produce .debug_str section. */
195 if ((ret = _dwarf_strtab_gen(dbg, error)) != DW_DLE_NONE)
198 /* Finally, update and generate all relocation sections. */
199 if ((ret = _dwarf_reloc_gen(dbg, error)) != DW_DLE_NONE)
202 /* Set section/relocation iterator to the first element. */
203 dbg->dbgp_secpos = STAILQ_FIRST(&dbg->dbgp_seclist);
204 dbg->dbgp_drspos = STAILQ_FIRST(&dbg->dbgp_drslist);
206 return (DW_DLE_NONE);
210 _dwarf_find_section(Dwarf_Debug dbg, const char *name)
215 assert(dbg != NULL && name != NULL);
217 for (i = 0; i < dbg->dbg_seccnt; i++) {
218 ds = &dbg->dbg_section[i];
219 if (ds->ds_name != NULL && !strcmp(ds->ds_name, name))
227 _dwarf_find_next_types_section(Dwarf_Debug dbg, Dwarf_Section *ds)
233 return (_dwarf_find_section(dbg, ".debug_types"));
235 assert(ds->ds_name != NULL);
239 if (ds->ds_name != NULL &&
240 !strcmp(ds->ds_name, ".debug_types"))
242 } while (ds->ds_name != NULL);
248 _dwarf_pro_find_section(Dwarf_P_Debug dbg, const char *name)
252 assert(dbg != NULL && name != NULL);
254 STAILQ_FOREACH(ds, &dbg->dbgp_seclist, ds_next) {
255 if (ds->ds_name != NULL && !strcmp(ds->ds_name ,name))
263 _dwarf_section_cleanup(Dwarf_P_Debug dbg)
265 Dwarf_P_Section ds, tds;
267 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
269 STAILQ_FOREACH_SAFE(ds, &dbg->dbgp_seclist, ds_next, tds) {
270 STAILQ_REMOVE(&dbg->dbgp_seclist, ds, _Dwarf_P_Section,
278 dbg->dbgp_seccnt = 0;
279 dbg->dbgp_secpos = 0;