elfxx.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2005 Hewlett-Packard Co
  3. Copyright (C) 2007 David Mosberger-Tang
  4. Contributed by David Mosberger-Tang <dmosberger@gmail.com>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #include <stdio.h>
  23. #include <sys/param.h>
  24. #include "libunwind_i.h"
  25. static int
  26. elf_w (lookup_symbol) (unw_addr_space_t as,
  27. unw_word_t ip, struct elf_image *ei,
  28. Elf_W (Addr) load_offset,
  29. char *buf, size_t buf_len, unw_word_t *offp)
  30. {
  31. size_t syment_size;
  32. Elf_W (Ehdr) *ehdr = ei->image;
  33. Elf_W (Sym) *sym, *symtab, *symtab_end;
  34. Elf_W (Off) soff, str_soff;
  35. Elf_W (Shdr) *shdr, *str_shdr;
  36. Elf_W (Addr) val, min_dist = ~(Elf_W (Addr))0;
  37. int i, ret = 0;
  38. char *strtab;
  39. if (!elf_w (valid_object) (ei))
  40. return -UNW_ENOINFO;
  41. soff = ehdr->e_shoff;
  42. if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size)
  43. {
  44. Debug (1, "section table outside of image? (%lu > %lu)\n",
  45. (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
  46. (unsigned long) ei->size);
  47. return -UNW_ENOINFO;
  48. }
  49. shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff);
  50. for (i = 0; i < ehdr->e_shnum; ++i)
  51. {
  52. switch (shdr->sh_type)
  53. {
  54. case SHT_SYMTAB:
  55. case SHT_DYNSYM:
  56. symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset);
  57. symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size);
  58. syment_size = shdr->sh_entsize;
  59. str_soff = soff + (shdr->sh_link * ehdr->e_shentsize);
  60. if (str_soff + ehdr->e_shentsize >= ei->size)
  61. {
  62. Debug (1, "string table outside of image? (%lu >= %lu)\n",
  63. (unsigned long) (str_soff + ehdr->e_shentsize),
  64. (unsigned long) ei->size);
  65. break;
  66. }
  67. str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff);
  68. strtab = (char *) ei->image + str_shdr->sh_offset;
  69. Debug (16, "symtab=0x%lx[%d], strtab=0x%lx\n",
  70. (long) shdr->sh_offset, shdr->sh_type,
  71. (long) str_shdr->sh_offset);
  72. for (sym = symtab;
  73. sym < symtab_end;
  74. sym = (Elf_W (Sym) *) ((char *) sym + syment_size))
  75. {
  76. if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC
  77. && sym->st_shndx != SHN_UNDEF)
  78. {
  79. if (tdep_get_func_addr (as, sym->st_value, &val) < 0)
  80. continue;
  81. if (sym->st_shndx != SHN_ABS)
  82. val += load_offset;
  83. Debug (16, "0x%016lx info=0x%02x %s\n",
  84. (long) val, sym->st_info, strtab + sym->st_name);
  85. if ((Elf_W (Addr)) (ip - val) < min_dist)
  86. {
  87. min_dist = (Elf_W (Addr)) (ip - val);
  88. strncpy (buf, strtab + sym->st_name, buf_len);
  89. buf[buf_len - 1] = '\0';
  90. if (strlen (strtab + sym->st_name) >= buf_len)
  91. ret = -UNW_ENOMEM;
  92. }
  93. }
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
  100. }
  101. if (min_dist >= ei->size)
  102. return -UNW_ENOINFO; /* not found */
  103. if (offp)
  104. *offp = min_dist;
  105. return ret;
  106. }
  107. /* Find the ELF image that contains IP and return the "closest"
  108. procedure name, if there is one. With some caching, this could be
  109. sped up greatly, but until an application materializes that's
  110. sensitive to the performance of this routine, why bother... */
  111. HIDDEN int
  112. elf_w (get_proc_name_in_image) (unw_addr_space_t as, struct elf_image *ei,
  113. unsigned long segbase,
  114. unsigned long mapoff,
  115. unw_word_t ip,
  116. char *buf, size_t buf_len, unw_word_t *offp)
  117. {
  118. Elf_W (Addr) load_offset = 0;
  119. Elf_W (Ehdr) *ehdr;
  120. Elf_W (Phdr) *phdr;
  121. int i, ret;
  122. ehdr = ei->image;
  123. phdr = (Elf_W (Phdr) *) ((char *) ei->image + ehdr->e_phoff);
  124. for (i = 0; i < ehdr->e_phnum; ++i)
  125. if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
  126. {
  127. load_offset = segbase - phdr[i].p_vaddr;
  128. break;
  129. }
  130. ret = elf_w (lookup_symbol) (as, ip, ei, load_offset, buf, buf_len, offp);
  131. return ret;
  132. }
  133. HIDDEN int
  134. elf_w (get_proc_name) (unw_addr_space_t as, pid_t pid, unw_word_t ip,
  135. char *buf, size_t buf_len, unw_word_t *offp)
  136. {
  137. unsigned long segbase, mapoff;
  138. struct elf_image ei;
  139. int ret;
  140. ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff, NULL, 0);
  141. if (ret < 0)
  142. return ret;
  143. ret = elf_w (get_proc_name_in_image) (as, &ei, segbase, mapoff, ip, buf, buf_len, offp);
  144. munmap (ei.image, ei.size);
  145. ei.image = NULL;
  146. return ret;
  147. }