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 3161 2015-02-15 21:43:36Z emaste $");
31 static const char *debug_name[] = {
53 _dwarf_elf_write_reloc(Dwarf_Debug dbg, Elf_Data *symtab_data, int endian,
54 void *buf, uint64_t offset, GElf_Xword r_info, GElf_Sxword r_addend)
59 if (gelf_getsym(symtab_data, GELF_R_SYM(r_info), &sym) == NULL)
61 if ((size = _dwarf_get_reloc_size(dbg, GELF_R_TYPE(r_info))) == 0)
62 return; /* Unknown or non-absolute relocation. */
63 if (endian == ELFDATA2MSB)
64 _dwarf_write_msb(buf, &offset, sym.st_value + r_addend, size);
66 _dwarf_write_lsb(buf, &offset, sym.st_value + r_addend, size);
70 _dwarf_elf_apply_rel_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data,
71 Elf_Data *symtab_data, int endian)
77 while (gelf_getrel(rel_data, j++, &rel) != NULL)
78 _dwarf_elf_write_reloc(dbg, symtab_data, endian, buf,
79 rel.r_offset, rel.r_info, 0);
83 _dwarf_elf_apply_rela_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data,
84 Elf_Data *symtab_data, int endian)
90 while (gelf_getrela(rel_data, j++, &rela) != NULL)
91 _dwarf_elf_write_reloc(dbg, symtab_data, endian, buf,
92 rela.r_offset, rela.r_info, rela.r_addend);
96 _dwarf_elf_relocate(Dwarf_Debug dbg, Elf *elf, Dwarf_Elf_Data *ed, size_t shndx,
97 size_t symtab, Elf_Data *symtab_data, Dwarf_Error *error)
105 if (symtab == 0 || symtab_data == NULL)
106 return (DW_DLE_NONE);
108 if (gelf_getehdr(elf, &eh) == NULL) {
109 DWARF_SET_ELF_ERROR(dbg, error);
115 while ((scn = elf_nextscn(elf, scn)) != NULL) {
116 if (gelf_getshdr(scn, &sh) == NULL) {
117 DWARF_SET_ELF_ERROR(dbg, error);
121 if ((sh.sh_type != SHT_REL && sh.sh_type != SHT_RELA) ||
125 if (sh.sh_info == shndx && sh.sh_link == symtab) {
126 if ((rel = elf_getdata(scn, NULL)) == NULL) {
127 elferr = elf_errno();
129 _DWARF_SET_ERROR(NULL, error,
133 return (DW_DLE_NONE);
136 ed->ed_alloc = malloc(ed->ed_data->d_size);
137 if (ed->ed_alloc == NULL) {
138 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
139 return (DW_DLE_MEMORY);
141 memcpy(ed->ed_alloc, ed->ed_data->d_buf,
142 ed->ed_data->d_size);
143 if (sh.sh_type == SHT_REL)
144 _dwarf_elf_apply_rel_reloc(dbg, ed->ed_alloc,
145 rel, symtab_data, eh.e_ident[EI_DATA]);
147 _dwarf_elf_apply_rela_reloc(dbg, ed->ed_alloc,
148 rel, symtab_data, eh.e_ident[EI_DATA]);
150 return (DW_DLE_NONE);
153 elferr = elf_errno();
155 DWARF_SET_ELF_ERROR(dbg, error);
159 return (DW_DLE_NONE);
163 _dwarf_elf_init(Dwarf_Debug dbg, Elf *elf, Dwarf_Error *error)
165 Dwarf_Obj_Access_Interface *iface;
170 Elf_Data *symtab_data;
172 int elferr, i, j, n, ret;
176 if ((iface = calloc(1, sizeof(*iface))) == NULL) {
177 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
178 return (DW_DLE_MEMORY);
181 if ((e = calloc(1, sizeof(*e))) == NULL) {
183 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
184 return (DW_DLE_MEMORY);
188 e->eo_methods.get_section_info = _dwarf_elf_get_section_info;
189 e->eo_methods.get_byte_order = _dwarf_elf_get_byte_order;
190 e->eo_methods.get_length_size = _dwarf_elf_get_length_size;
191 e->eo_methods.get_pointer_size = _dwarf_elf_get_pointer_size;
192 e->eo_methods.get_section_count = _dwarf_elf_get_section_count;
193 e->eo_methods.load_section = _dwarf_elf_load_section;
196 iface->methods = &e->eo_methods;
198 dbg->dbg_iface = iface;
200 if (gelf_getehdr(elf, &e->eo_ehdr) == NULL) {
201 DWARF_SET_ELF_ERROR(dbg, error);
206 dbg->dbg_machine = e->eo_ehdr.e_machine;
208 if (!elf_getshstrndx(elf, &e->eo_strndx)) {
209 DWARF_SET_ELF_ERROR(dbg, error);
219 while ((scn = elf_nextscn(elf, scn)) != NULL) {
220 if (gelf_getshdr(scn, &sh) == NULL) {
221 DWARF_SET_ELF_ERROR(dbg, error);
226 if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
228 DWARF_SET_ELF_ERROR(dbg, error);
233 if (!strcmp(name, ".symtab")) {
234 symtab_ndx = elf_ndxscn(scn);
235 if ((symtab_data = elf_getdata(scn, NULL)) == NULL) {
236 elferr = elf_errno();
238 _DWARF_SET_ERROR(NULL, error,
247 for (i = 0; debug_name[i] != NULL; i++) {
248 if (!strcmp(name, debug_name[i]))
252 elferr = elf_errno();
254 DWARF_SET_ELF_ERROR(dbg, error);
261 return (DW_DLE_NONE);
263 if ((e->eo_data = calloc(n, sizeof(Dwarf_Elf_Data))) == NULL ||
264 (e->eo_shdr = calloc(n, sizeof(GElf_Shdr))) == NULL) {
265 DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
272 while ((scn = elf_nextscn(elf, scn)) != NULL && j < n) {
273 if (gelf_getshdr(scn, &sh) == NULL) {
274 DWARF_SET_ELF_ERROR(dbg, error);
279 memcpy(&e->eo_shdr[j], &sh, sizeof(sh));
281 if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
283 DWARF_SET_ELF_ERROR(dbg, error);
288 for (i = 0; debug_name[i] != NULL; i++) {
289 if (strcmp(name, debug_name[i]))
293 if ((e->eo_data[j].ed_data = elf_getdata(scn, NULL)) ==
295 elferr = elf_errno();
297 _DWARF_SET_ERROR(dbg, error,
304 if (_libdwarf.applyreloc) {
305 if (_dwarf_elf_relocate(dbg, elf,
306 &e->eo_data[j], elf_ndxscn(scn), symtab_ndx,
307 symtab_data, error) != DW_DLE_NONE)
317 return (DW_DLE_NONE);
321 _dwarf_elf_deinit(dbg);
327 _dwarf_elf_deinit(Dwarf_Debug dbg)
329 Dwarf_Obj_Access_Interface *iface;
333 iface = dbg->dbg_iface;
334 assert(iface != NULL);
340 for (i = 0; (Dwarf_Unsigned) i < e->eo_seccnt; i++) {
341 if (e->eo_data[i].ed_alloc)
342 free(e->eo_data[i].ed_alloc);
352 dbg->dbg_iface = NULL;