2 * Copyright (c) 2009 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_elf_init.c 2972 2013-12-23 06:46:04Z kaiwang27 $");
31 static const char *debug_name[] = {
53 _dwarf_elf_apply_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data,
54 Elf_Data *symtab_data, int endian)
64 while (gelf_getrela(rel_data, j++, &rela) != NULL) {
65 symndx = GELF_R_SYM(rela.r_info);
66 type = GELF_R_TYPE(rela.r_info);
68 if (gelf_getsym(symtab_data, symndx, &sym) == NULL)
71 offset = rela.r_offset;
72 size = _dwarf_get_reloc_size(dbg, type);
74 if (endian == ELFDATA2MSB)
75 _dwarf_write_msb(buf, &offset, rela.r_addend, size);
77 _dwarf_write_lsb(buf, &offset, rela.r_addend, size);
82 _dwarf_elf_relocate(Dwarf_Debug dbg, Elf *elf, Dwarf_Elf_Data *ed, size_t shndx,
83 size_t symtab, Elf_Data *symtab_data, Dwarf_Error *error)
91 if (symtab == 0 || symtab_data == NULL)
94 if (gelf_getehdr(elf, &eh) == NULL) {
95 DWARF_SET_ELF_ERROR(dbg, error);
101 while ((scn = elf_nextscn(elf, scn)) != NULL) {
102 if (gelf_getshdr(scn, &sh) == NULL) {
103 DWARF_SET_ELF_ERROR(dbg, error);
107 if (sh.sh_type != SHT_RELA || sh.sh_size == 0)
110 if (sh.sh_info == shndx && sh.sh_link == symtab) {
111 if ((rel = elf_getdata(scn, NULL)) == NULL) {
112 elferr = elf_errno();
114 _DWARF_SET_ERROR(NULL, error,
118 return (DW_DLE_NONE);
121 ed->ed_alloc = malloc(ed->ed_data->d_size);
122 if (ed->ed_alloc == NULL) {
123 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
124 return (DW_DLE_MEMORY);
126 memcpy(ed->ed_alloc, ed->ed_data->d_buf,
127 ed->ed_data->d_size);
128 _dwarf_elf_apply_reloc(dbg, ed->ed_alloc, rel,
129 symtab_data, eh.e_ident[EI_DATA]);
131 return (DW_DLE_NONE);
134 elferr = elf_errno();
136 DWARF_SET_ELF_ERROR(dbg, error);
140 return (DW_DLE_NONE);
144 _dwarf_elf_init(Dwarf_Debug dbg, Elf *elf, Dwarf_Error *error)
146 Dwarf_Obj_Access_Interface *iface;
151 Elf_Data *symtab_data;
153 int elferr, i, j, n, ret;
157 if ((iface = calloc(1, sizeof(*iface))) == NULL) {
158 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
159 return (DW_DLE_MEMORY);
162 if ((e = calloc(1, sizeof(*e))) == NULL) {
164 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
165 return (DW_DLE_MEMORY);
169 e->eo_methods.get_section_info = _dwarf_elf_get_section_info;
170 e->eo_methods.get_byte_order = _dwarf_elf_get_byte_order;
171 e->eo_methods.get_length_size = _dwarf_elf_get_length_size;
172 e->eo_methods.get_pointer_size = _dwarf_elf_get_pointer_size;
173 e->eo_methods.get_section_count = _dwarf_elf_get_section_count;
174 e->eo_methods.load_section = _dwarf_elf_load_section;
177 iface->methods = &e->eo_methods;
179 dbg->dbg_iface = iface;
181 if (gelf_getehdr(elf, &e->eo_ehdr) == NULL) {
182 DWARF_SET_ELF_ERROR(dbg, error);
187 dbg->dbg_machine = e->eo_ehdr.e_machine;
189 if (!elf_getshstrndx(elf, &e->eo_strndx)) {
190 DWARF_SET_ELF_ERROR(dbg, error);
200 while ((scn = elf_nextscn(elf, scn)) != NULL) {
201 if (gelf_getshdr(scn, &sh) == NULL) {
202 DWARF_SET_ELF_ERROR(dbg, error);
207 if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
209 DWARF_SET_ELF_ERROR(dbg, error);
214 if (!strcmp(name, ".symtab")) {
215 symtab_ndx = elf_ndxscn(scn);
216 if ((symtab_data = elf_getdata(scn, NULL)) == NULL) {
217 elferr = elf_errno();
219 _DWARF_SET_ERROR(NULL, error,
228 for (i = 0; debug_name[i] != NULL; i++) {
229 if (!strcmp(name, debug_name[i]))
233 elferr = elf_errno();
235 DWARF_SET_ELF_ERROR(dbg, error);
242 return (DW_DLE_NONE);
244 if ((e->eo_data = calloc(n, sizeof(Dwarf_Elf_Data))) == NULL ||
245 (e->eo_shdr = calloc(n, sizeof(GElf_Shdr))) == NULL) {
246 DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
253 while ((scn = elf_nextscn(elf, scn)) != NULL && j < n) {
254 if (gelf_getshdr(scn, &sh) == NULL) {
255 DWARF_SET_ELF_ERROR(dbg, error);
260 memcpy(&e->eo_shdr[j], &sh, sizeof(sh));
262 if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
264 DWARF_SET_ELF_ERROR(dbg, error);
269 for (i = 0; debug_name[i] != NULL; i++) {
270 if (strcmp(name, debug_name[i]))
274 if ((e->eo_data[j].ed_data = elf_getdata(scn, NULL)) ==
276 elferr = elf_errno();
278 _DWARF_SET_ERROR(dbg, error,
285 if (_libdwarf.applyrela) {
286 if (_dwarf_elf_relocate(dbg, elf,
287 &e->eo_data[j], elf_ndxscn(scn), symtab_ndx,
288 symtab_data, error) != DW_DLE_NONE)
298 return (DW_DLE_NONE);
302 _dwarf_elf_deinit(dbg);
308 _dwarf_elf_deinit(Dwarf_Debug dbg)
310 Dwarf_Obj_Access_Interface *iface;
314 iface = dbg->dbg_iface;
315 assert(iface != NULL);
321 for (i = 0; (Dwarf_Unsigned) i < e->eo_seccnt; i++) {
322 if (e->eo_data[i].ed_alloc)
323 free(e->eo_data[i].ed_alloc);
333 dbg->dbg_iface = NULL;