2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
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
31 #include "_libdwarf.h"
33 static const char *debug_snames[DWARF_DEBUG_SNAMES] = {
54 static uint64_t (*dwarf_read) (Elf_Data **, uint64_t *, int);
55 static void (*dwarf_write) (Elf_Data **, uint64_t *, uint64_t, int);
58 dwarf_read_lsb(Elf_Data **dp, uint64_t *offsetp, int bytes_to_read)
62 uint8_t *src = (uint8_t *) (*dp)->d_buf + *offsetp;
64 switch (bytes_to_read) {
66 ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
67 ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
69 ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
71 ret |= ((uint64_t) src[1]) << 8;
80 *offsetp += bytes_to_read;
86 dwarf_read_msb(Elf_Data **dp, uint64_t *offsetp, int bytes_to_read)
90 uint8_t *src = (uint8_t *) (*dp)->d_buf + *offsetp;
92 switch (bytes_to_read) {
97 ret = src[1] | ((uint64_t) src[0]) << 8;
100 ret = src[3] | ((uint64_t) src[2]) << 8;
101 ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
104 ret = src[7] | ((uint64_t) src[6]) << 8;
105 ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
106 ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
107 ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
114 *offsetp += bytes_to_read;
120 dwarf_write_lsb(Elf_Data **dp, uint64_t *offsetp, uint64_t value, int bytes_to_write)
122 uint8_t *dst = (uint8_t *) (*dp)->d_buf + *offsetp;
124 switch (bytes_to_write) {
126 dst[7] = (value >> 56) & 0xff;
127 dst[6] = (value >> 48) & 0xff;
128 dst[5] = (value >> 40) & 0xff;
129 dst[4] = (value >> 32) & 0xff;
131 dst[3] = (value >> 24) & 0xff;
132 dst[2] = (value >> 16) & 0xff;
134 dst[1] = (value >> 8) & 0xff;
136 dst[0] = value & 0xff;
143 *offsetp += bytes_to_write;
147 dwarf_write_msb(Elf_Data **dp, uint64_t *offsetp, uint64_t value, int bytes_to_write)
149 uint8_t *dst = (uint8_t *) (*dp)->d_buf + *offsetp;
151 switch (bytes_to_write) {
153 dst[7] = value & 0xff;
154 dst[6] = (value >> 8) & 0xff;
155 dst[5] = (value >> 16) & 0xff;
156 dst[4] = (value >> 24) & 0xff;
159 dst[3] = value & 0xff;
160 dst[2] = (value >> 8) & 0xff;
163 dst[1] = value & 0xff;
166 dst[0] = value & 0xff;
173 *offsetp += bytes_to_write;
177 dwarf_read_sleb128(Elf_Data **dp, uint64_t *offsetp)
183 uint8_t *src = (uint8_t *) (*dp)->d_buf + *offsetp;
188 ret |= ((b & 0x7f) << shift);
193 } while ((b & 0x80) != 0);
195 if (shift < 32 && (b & 0x40) != 0)
196 ret |= (-1 << shift);
202 dwarf_read_uleb128(Elf_Data **dp, uint64_t *offsetp)
208 uint8_t *src = (uint8_t *) (*dp)->d_buf + *offsetp;
213 ret |= ((b & 0x7f) << shift);
218 } while ((b & 0x80) != 0);
224 dwarf_read_string(Elf_Data **dp, uint64_t *offsetp)
228 char *src = (char *) (*dp)->d_buf + *offsetp;
232 while (*src != '\0' && *offsetp < (*dp)->d_size) {
237 if (*src == '\0' && *offsetp < (*dp)->d_size)
244 dwarf_read_block(Elf_Data **dp, uint64_t *offsetp, uint64_t length)
248 uint8_t *src = (char *) (*dp)->d_buf + *offsetp;
252 (*offsetp) += length;
258 dwarf_apply_relocations(Dwarf_Debug dbg, Elf_Data *reld, int secindx)
263 int ret = DWARF_E_NONE;
266 /* Point to the data to be relocated: */
267 d = dbg->dbg_s[secindx].s_data;
269 /* Enter a loop to process each relocation addend: */
270 while (gelf_getrela(reld, indx++, &rela) != NULL) {
272 Elf64_Xword symindx = ELF64_R_SYM(rela.r_info);
274 if (gelf_getsym(dbg->dbg_s[DWARF_symtab].s_data, symindx, &sym) == NULL) {
275 printf("Couldn't find symbol index %lu for relocation\n",(u_long) symindx);
279 offset = rela.r_offset;
281 dwarf_write(&d, &offset, rela.r_addend, dbg->dbg_offsize);
288 dwarf_relocate(Dwarf_Debug dbg, Dwarf_Error *error)
293 int ret = DWARF_E_NONE;
295 /* Look for sections which relocate the debug sections. */
296 while ((scn = elf_nextscn(dbg->dbg_elf, scn)) != NULL) {
297 if (gelf_getshdr(scn, &shdr) == NULL) {
298 DWARF_SET_ELF_ERROR(error, elf_errno());
302 if (shdr.sh_type != SHT_RELA || shdr.sh_size == 0)
305 for (i = 0; i < DWARF_DEBUG_SNAMES; i++) {
306 if (dbg->dbg_s[i].s_shnum == shdr.sh_info &&
307 dbg->dbg_s[DWARF_symtab].s_shnum == shdr.sh_link) {
310 /* Get the relocation data. */
311 if ((rd = elf_getdata(scn, NULL)) == NULL) {
312 DWARF_SET_ELF_ERROR(error, elf_errno());
316 /* Apply the relocations. */
317 dwarf_apply_relocations(dbg, rd, i);
327 dwarf_init_attr(Dwarf_Debug dbg, Elf_Data **dp, uint64_t *offsetp,
328 Dwarf_CU cu, Dwarf_Die die, Dwarf_Attribute at, uint64_t form,
331 int ret = DWARF_E_NONE;
332 struct _Dwarf_AttrValue avref;
334 memset(&avref, 0, sizeof(avref));
335 avref.av_attrib = at->at_attrib;
336 avref.av_form = at->at_form;
340 avref.u[0].u64 = dwarf_read(dp, offsetp, cu->cu_pointer_size);
343 avref.u[0].u64 = dwarf_read_uleb128(dp, offsetp);
344 avref.u[1].u8p = dwarf_read_block(dp, offsetp, avref.u[0].u64);
347 avref.u[0].u64 = dwarf_read(dp, offsetp, 1);
348 avref.u[1].u8p = dwarf_read_block(dp, offsetp, avref.u[0].u64);
351 avref.u[0].u64 = dwarf_read(dp, offsetp, 2);
352 avref.u[1].u8p = dwarf_read_block(dp, offsetp, avref.u[0].u64);
355 avref.u[0].u64 = dwarf_read(dp, offsetp, 4);
356 avref.u[1].u8p = dwarf_read_block(dp, offsetp, avref.u[0].u64);
361 avref.u[0].u64 = dwarf_read(dp, offsetp, 1);
365 avref.u[0].u64 = dwarf_read(dp, offsetp, 2);
369 avref.u[0].u64 = dwarf_read(dp, offsetp, 4);
373 avref.u[0].u64 = dwarf_read(dp, offsetp, 8);
375 case DW_FORM_indirect:
376 form = dwarf_read_uleb128(dp, offsetp);
377 return dwarf_init_attr(dbg, dp, offsetp, cu, die, at, form, error);
378 case DW_FORM_ref_addr:
379 if (cu->cu_version == 2)
380 avref.u[0].u64 = dwarf_read(dp, offsetp, cu->cu_pointer_size);
381 else if (cu->cu_version == 3)
382 avref.u[0].u64 = dwarf_read(dp, offsetp, dbg->dbg_offsize);
384 case DW_FORM_ref_udata:
386 avref.u[0].u64 = dwarf_read_uleb128(dp, offsetp);
389 avref.u[0].s64 = dwarf_read_sleb128(dp, offsetp);
392 avref.u[0].s = dwarf_read_string(dp, offsetp);
395 avref.u[0].u64 = dwarf_read(dp, offsetp, dbg->dbg_offsize);
396 avref.u[1].s = elf_strptr(dbg->dbg_elf,
397 dbg->dbg_s[DWARF_debug_str].s_shnum, avref.u[0].u64);
400 DWARF_SET_ERROR(error, DWARF_E_NOT_IMPLEMENTED);
401 ret = DWARF_E_NOT_IMPLEMENTED;
405 if (ret == DWARF_E_NONE)
406 ret = dwarf_attrval_add(die, &avref, NULL, error);
412 dwarf_init_abbrev(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Error *error)
416 int ret = DWARF_E_NONE;
424 d = dbg->dbg_s[DWARF_debug_abbrev].s_data;
426 offset = cu->cu_abbrev_offset;
428 while (offset < d->d_size) {
430 entry = dwarf_read_uleb128(&d, &offset);
432 /* Check if this is the end of the data: */
436 tag = dwarf_read_uleb128(&d, &offset);
438 children = dwarf_read(&d, &offset, 1);
440 if ((ret = dwarf_abbrev_add(cu, entry, tag, children, &a, error)) != DWARF_E_NONE)
444 attr = dwarf_read_uleb128(&d, &offset);
445 form = dwarf_read_uleb128(&d, &offset);
448 if ((ret = dwarf_attr_add(a, attr, form, NULL, error)) != DWARF_E_NONE)
457 dwarf_init_info(Dwarf_Debug dbg, Dwarf_Error *error)
465 int ret = DWARF_E_NONE;
467 uint64_t next_offset;
470 scn = dbg->dbg_s[DWARF_debug_info].s_scn;
472 d = dbg->dbg_s[DWARF_debug_info].s_data;
474 while (offset < d->d_size) {
475 /* Allocate memory for the first compilation unit. */
476 if ((cu = calloc(sizeof(struct _Dwarf_CU), 1)) == NULL) {
477 DWARF_SET_ERROR(error, DWARF_E_MEMORY);
478 return DWARF_E_MEMORY;
481 /* Save the offet to this compilation unit: */
482 cu->cu_offset = offset;
484 length = dwarf_read(&d, &offset, 4);
485 if (length == 0xffffffff) {
486 length = dwarf_read(&d, &offset, 8);
487 dbg->dbg_offsize = 8;
489 dbg->dbg_offsize = 4;
492 * Check if there is enough ELF data for this CU.
493 * This assumes that libelf gives us the entire
494 * section in one Elf_Data object.
496 if (length > d->d_size - offset) {
498 DWARF_SET_ERROR(error, DWARF_E_INVALID_CU);
499 return DWARF_E_INVALID_CU;
502 /* Relocate the DWARF sections if necessary: */
504 if ((ret = dwarf_relocate(dbg, error)) != DWARF_E_NONE)
509 /* Compute the offset to the next compilation unit: */
510 next_offset = offset + length;
512 /* Initialise the compilation unit. */
513 cu->cu_length = length;
514 cu->cu_header_length = (dbg->dbg_offsize == 4) ? 4 : 12;
515 cu->cu_version = dwarf_read(&d, &offset, 2);
516 cu->cu_abbrev_offset = dwarf_read(&d, &offset, dbg->dbg_offsize);
517 cu->cu_pointer_size = dwarf_read(&d, &offset, 1);
518 cu->cu_next_offset = next_offset;
520 /* Initialise the list of abbrevs. */
521 STAILQ_INIT(&cu->cu_abbrev);
523 /* Initialise the list of dies. */
524 STAILQ_INIT(&cu->cu_die);
526 /* Initialise the hash table of dies. */
527 for (i = 0; i < DWARF_DIE_HASH_SIZE; i++)
528 STAILQ_INIT(&cu->cu_die_hash[i]);
530 /* Add the compilation unit to the list. */
531 STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next);
533 if (cu->cu_version != 2 && cu->cu_version != 3) {
534 DWARF_SET_ERROR(error, DWARF_E_CU_VERSION);
535 ret = DWARF_E_CU_VERSION;
539 /* Parse the .debug_abbrev info for this CU: */
540 if ((ret = dwarf_init_abbrev(dbg, cu, error)) != DWARF_E_NONE)
545 while (offset < next_offset && offset < d->d_size) {
550 uint64_t die_offset = offset;;
552 abnum = dwarf_read_uleb128(&d, &offset);
559 if ((a = dwarf_abbrev_find(cu, abnum)) == NULL) {
560 DWARF_SET_ERROR(error, DWARF_E_MISSING_ABBREV);
561 return DWARF_E_MISSING_ABBREV;
564 if ((ret = dwarf_die_add(cu, level, die_offset,
565 abnum, a, &die, error)) != DWARF_E_NONE)
568 STAILQ_FOREACH(at, &a->a_attrib, at_next) {
569 if ((ret = dwarf_init_attr(dbg, &d, &offset,
570 cu, die, at, at->at_form, error)) != DWARF_E_NONE)
574 if (a->a_children == DW_CHILDREN_yes)
578 offset = next_offset;
585 dwarf_elf_read(Dwarf_Debug dbg, Dwarf_Error *error)
591 int ret = DWARF_E_NONE;
593 /* Get a copy of the ELF header. */
594 if (gelf_getehdr(dbg->dbg_elf, &dbg->dbg_ehdr) == NULL) {
595 DWARF_SET_ELF_ERROR(error, elf_errno());
599 /* Check the ELF data format: */
600 switch (dbg->dbg_ehdr.e_ident[EI_DATA]) {
602 dwarf_read = dwarf_read_msb;
603 dwarf_write = dwarf_write_msb;
609 dwarf_read = dwarf_read_lsb;
610 dwarf_write = dwarf_write_lsb;
614 /* Get the section index to the string table. */
615 if (elf_getshstrndx(dbg->dbg_elf, &dbg->dbg_stnum) == 0) {
616 DWARF_SET_ELF_ERROR(error, elf_errno());
620 /* Look for the debug sections. */
621 while ((scn = elf_nextscn(dbg->dbg_elf, scn)) != NULL) {
622 /* Get a copy of the section header: */
623 if (gelf_getshdr(scn, &shdr) == NULL) {
624 DWARF_SET_ELF_ERROR(error, elf_errno());
628 /* Get a pointer to the section name: */
629 if ((sname = elf_strptr(dbg->dbg_elf, dbg->dbg_stnum, shdr.sh_name)) == NULL) {
630 DWARF_SET_ELF_ERROR(error, elf_errno());
635 * Look up the section name to check if it's
636 * one we need for DWARF.
638 for (i = 0; i < DWARF_DEBUG_SNAMES; i++) {
639 if (strcmp(sname, debug_snames[i]) == 0) {
640 dbg->dbg_s[i].s_sname = sname;
641 dbg->dbg_s[i].s_shnum = elf_ndxscn(scn);
642 dbg->dbg_s[i].s_scn = scn;
643 memcpy(&dbg->dbg_s[i].s_shdr, &shdr, sizeof(shdr));
644 if ((dbg->dbg_s[i].s_data = elf_getdata(scn, NULL)) == NULL) {
645 DWARF_SET_ELF_ERROR(error, elf_errno());
653 /* Check if any of the required sections are missing: */
654 if (dbg->dbg_s[DWARF_debug_abbrev].s_scn == NULL ||
655 dbg->dbg_s[DWARF_debug_info].s_scn == NULL) {
656 /* Missing debug information. */
657 DWARF_SET_ERROR(error, DWARF_E_DEBUG_INFO);
658 return DWARF_E_DEBUG_INFO;
661 /* Initialise the compilation-units: */
662 ret = dwarf_init_info(dbg, error);
668 dwarf_elf_init(Elf *elf, int mode, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
671 int ret = DWARF_E_NONE;
674 /* Can only return a generic error. */
675 return DWARF_E_ERROR;
677 if (elf == NULL || ret_dbg == NULL) {
678 DWARF_SET_ERROR(error, DWARF_E_ARGUMENT);
679 ret = DWARF_E_ARGUMENT;
680 } else if ((dbg = calloc(sizeof(struct _Dwarf_Debug), 1)) == NULL) {
681 DWARF_SET_ERROR(error, DWARF_E_MEMORY);
682 ret = DWARF_E_MEMORY;
685 dbg->dbg_elf_close = 0;
686 dbg->dbg_mode = mode;
688 STAILQ_INIT(&dbg->dbg_cu);
692 /* Read the ELF sections. */
693 ret = dwarf_elf_read(dbg, error);
700 dwarf_init(int fd, int mode, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
708 /* Can only return a generic error. */
709 return DWARF_E_ERROR;
711 if (fd < 0 || ret_dbg == NULL) {
712 DWARF_SET_ERROR(error, DWARF_E_ARGUMENT);
713 return DWARF_E_ERROR;
716 /* Translate the DWARF mode to ELF mode. */
724 if (elf_version(EV_CURRENT) == EV_NONE) {
725 DWARF_SET_ELF_ERROR(error, elf_errno());
726 return DWARF_E_ERROR;
729 if ((elf = elf_begin(fd, c, NULL)) == NULL) {
730 DWARF_SET_ELF_ERROR(error, elf_errno());
731 return DWARF_E_ERROR;
734 ret = dwarf_elf_init(elf, mode, ret_dbg, error);
736 if (*ret_dbg != NULL)
737 /* Remember to close the ELF file. */
738 (*ret_dbg)->dbg_elf_close = 1;
740 if (ret != DWARF_E_NONE) {
741 if (*ret_dbg != NULL) {
742 dwarf_finish(ret_dbg, &lerror);