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 3475 2016-05-18 18:11:26Z emaste $");
31 static const char *debug_name[] = {
53 _dwarf_elf_apply_rel_reloc(Dwarf_Debug dbg, void *buf, uint64_t bufsize,
54 Elf_Data *rel_data, Elf_Data *symtab_data, int endian)
65 while (gelf_getrel(rel_data, j++, &rel) != NULL) {
66 symndx = GELF_R_SYM(rel.r_info);
67 type = GELF_R_TYPE(rel.r_info);
69 if (gelf_getsym(symtab_data, symndx, &sym) == NULL)
72 size = _dwarf_get_reloc_size(dbg, type);
74 continue; /* Unknown or non-absolute relocation. */
76 offset = rel.r_offset;
77 if (offset + size >= bufsize)
80 if (endian == ELFDATA2MSB)
81 addend = _dwarf_read_msb(buf, &offset, size);
83 addend = _dwarf_read_lsb(buf, &offset, size);
85 offset = rel.r_offset;
86 if (endian == ELFDATA2MSB)
87 _dwarf_write_msb(buf, &offset, sym.st_value + addend,
90 _dwarf_write_lsb(buf, &offset, sym.st_value + addend,
96 _dwarf_elf_apply_rela_reloc(Dwarf_Debug dbg, void *buf, uint64_t bufsize,
97 Elf_Data *rel_data, Elf_Data *symtab_data, int endian)
107 while (gelf_getrela(rel_data, j++, &rela) != NULL) {
108 symndx = GELF_R_SYM(rela.r_info);
109 type = GELF_R_TYPE(rela.r_info);
111 if (gelf_getsym(symtab_data, symndx, &sym) == NULL)
114 offset = rela.r_offset;
115 size = _dwarf_get_reloc_size(dbg, type);
117 continue; /* Unknown or non-absolute relocation. */
118 if (offset + size >= bufsize)
121 if (endian == ELFDATA2MSB)
122 _dwarf_write_msb(buf, &offset,
123 sym.st_value + rela.r_addend, size);
125 _dwarf_write_lsb(buf, &offset,
126 sym.st_value + rela.r_addend, size);
131 _dwarf_elf_relocate(Dwarf_Debug dbg, Elf *elf, Dwarf_Elf_Data *ed, size_t shndx,
132 size_t symtab, Elf_Data *symtab_data, Dwarf_Error *error)
140 if (symtab == 0 || symtab_data == NULL)
141 return (DW_DLE_NONE);
143 if (gelf_getehdr(elf, &eh) == NULL) {
144 DWARF_SET_ELF_ERROR(dbg, error);
150 while ((scn = elf_nextscn(elf, scn)) != NULL) {
151 if (gelf_getshdr(scn, &sh) == NULL) {
152 DWARF_SET_ELF_ERROR(dbg, error);
156 if ((sh.sh_type != SHT_REL && sh.sh_type != SHT_RELA) ||
160 if (sh.sh_info == shndx && sh.sh_link == symtab) {
161 if ((rel = elf_getdata(scn, NULL)) == NULL) {
162 elferr = elf_errno();
164 _DWARF_SET_ERROR(NULL, error,
168 return (DW_DLE_NONE);
171 ed->ed_alloc = malloc(ed->ed_data->d_size);
172 if (ed->ed_alloc == NULL) {
173 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
174 return (DW_DLE_MEMORY);
176 memcpy(ed->ed_alloc, ed->ed_data->d_buf,
177 ed->ed_data->d_size);
178 if (sh.sh_type == SHT_REL)
179 _dwarf_elf_apply_rel_reloc(dbg,
180 ed->ed_alloc, ed->ed_data->d_size,
181 rel, symtab_data, eh.e_ident[EI_DATA]);
183 _dwarf_elf_apply_rela_reloc(dbg,
184 ed->ed_alloc, ed->ed_data->d_size,
185 rel, symtab_data, eh.e_ident[EI_DATA]);
187 return (DW_DLE_NONE);
190 elferr = elf_errno();
192 DWARF_SET_ELF_ERROR(dbg, error);
196 return (DW_DLE_NONE);
200 _dwarf_elf_init(Dwarf_Debug dbg, Elf *elf, Dwarf_Error *error)
202 Dwarf_Obj_Access_Interface *iface;
207 Elf_Data *symtab_data;
209 int elferr, i, j, n, ret;
213 if ((iface = calloc(1, sizeof(*iface))) == NULL) {
214 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
215 return (DW_DLE_MEMORY);
218 if ((e = calloc(1, sizeof(*e))) == NULL) {
220 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
221 return (DW_DLE_MEMORY);
225 e->eo_methods.get_section_info = _dwarf_elf_get_section_info;
226 e->eo_methods.get_byte_order = _dwarf_elf_get_byte_order;
227 e->eo_methods.get_length_size = _dwarf_elf_get_length_size;
228 e->eo_methods.get_pointer_size = _dwarf_elf_get_pointer_size;
229 e->eo_methods.get_section_count = _dwarf_elf_get_section_count;
230 e->eo_methods.load_section = _dwarf_elf_load_section;
233 iface->methods = &e->eo_methods;
235 dbg->dbg_iface = iface;
237 if (gelf_getehdr(elf, &e->eo_ehdr) == NULL) {
238 DWARF_SET_ELF_ERROR(dbg, error);
243 dbg->dbg_machine = e->eo_ehdr.e_machine;
245 if (!elf_getshstrndx(elf, &e->eo_strndx)) {
246 DWARF_SET_ELF_ERROR(dbg, error);
256 while ((scn = elf_nextscn(elf, scn)) != NULL) {
257 if (gelf_getshdr(scn, &sh) == NULL) {
258 DWARF_SET_ELF_ERROR(dbg, error);
263 if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
265 DWARF_SET_ELF_ERROR(dbg, error);
270 if (!strcmp(name, ".symtab")) {
271 symtab_ndx = elf_ndxscn(scn);
272 if ((symtab_data = elf_getdata(scn, NULL)) == NULL) {
273 elferr = elf_errno();
275 _DWARF_SET_ERROR(NULL, error,
284 for (i = 0; debug_name[i] != NULL; i++) {
285 if (!strcmp(name, debug_name[i]))
289 elferr = elf_errno();
291 DWARF_SET_ELF_ERROR(dbg, error);
298 return (DW_DLE_NONE);
300 if ((e->eo_data = calloc(n, sizeof(Dwarf_Elf_Data))) == NULL ||
301 (e->eo_shdr = calloc(n, sizeof(GElf_Shdr))) == NULL) {
302 DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
309 while ((scn = elf_nextscn(elf, scn)) != NULL && j < n) {
310 if (gelf_getshdr(scn, &sh) == NULL) {
311 DWARF_SET_ELF_ERROR(dbg, error);
316 memcpy(&e->eo_shdr[j], &sh, sizeof(sh));
318 if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
320 DWARF_SET_ELF_ERROR(dbg, error);
325 for (i = 0; debug_name[i] != NULL; i++) {
326 if (strcmp(name, debug_name[i]))
330 if ((e->eo_data[j].ed_data = elf_getdata(scn, NULL)) ==
332 elferr = elf_errno();
334 _DWARF_SET_ERROR(dbg, error,
341 if (_libdwarf.applyreloc) {
342 if (_dwarf_elf_relocate(dbg, elf,
343 &e->eo_data[j], elf_ndxscn(scn), symtab_ndx,
344 symtab_data, error) != DW_DLE_NONE)
354 return (DW_DLE_NONE);
358 _dwarf_elf_deinit(dbg);
364 _dwarf_elf_deinit(Dwarf_Debug dbg)
366 Dwarf_Obj_Access_Interface *iface;
370 iface = dbg->dbg_iface;
371 assert(iface != NULL);
377 for (i = 0; (Dwarf_Unsigned) i < e->eo_seccnt; i++) {
378 if (e->eo_data[i].ed_alloc)
379 free(e->eo_data[i].ed_alloc);
389 dbg->dbg_iface = NULL;