symtab.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* symtab.c
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "gprof.h"
  17. #include "search_list.h"
  18. #include "source.h"
  19. #include "symtab.h"
  20. #include "cg_arcs.h"
  21. #include "corefile.h"
  22. static int cmp_addr (const PTR, const PTR);
  23. Sym_Table symtab;
  24. /* Initialize a symbol (so it's empty). */
  25. void
  26. sym_init (Sym *sym)
  27. {
  28. memset (sym, 0, sizeof (*sym));
  29. /* It is not safe to assume that a binary zero corresponds
  30. to a floating-point 0.0, so initialize floats explicitly. */
  31. sym->hist.time = 0.0;
  32. sym->cg.child_time = 0.0;
  33. sym->cg.prop.fract = 0.0;
  34. sym->cg.prop.self = 0.0;
  35. sym->cg.prop.child = 0.0;
  36. }
  37. /* Compare the function entry-point of two symbols and return <0, =0,
  38. or >0 depending on whether the left value is smaller than, equal
  39. to, or greater than the right value. If two symbols are equal
  40. but one has is_func set and the other doesn't, we make the
  41. non-function symbol one "bigger" so that the function symbol will
  42. survive duplicate removal. Finally, if both symbols have the
  43. same is_func value, we discriminate against is_static such that
  44. the global symbol survives. */
  45. static int
  46. cmp_addr (const PTR lp, const PTR rp)
  47. {
  48. const Sym *left = (const Sym *) lp;
  49. const Sym *right = (const Sym *) rp;
  50. if (left->addr > right->addr)
  51. return 1;
  52. else if (left->addr < right->addr)
  53. return -1;
  54. if (left->is_func != right->is_func)
  55. return right->is_func - left->is_func;
  56. return left->is_static - right->is_static;
  57. }
  58. void
  59. symtab_finalize (Sym_Table *tab)
  60. {
  61. Sym *src, *dst;
  62. bfd_vma prev_addr;
  63. if (!tab->len)
  64. return;
  65. /* Sort symbol table in order of increasing function addresses. */
  66. qsort (tab->base, tab->len, sizeof (Sym), cmp_addr);
  67. /* Remove duplicate entries to speed-up later processing and
  68. set end_addr if its not set yet. */
  69. prev_addr = tab->base[0].addr + 1;
  70. for (src = dst = tab->base; src < tab->limit; ++src)
  71. {
  72. if (src->addr == prev_addr)
  73. {
  74. /* If same address, favor global symbol over static one,
  75. then function over line number. If both symbols are
  76. either static or global and either function or line, check
  77. whether one has name beginning with underscore while
  78. the other doesn't. In such cases, keep sym without
  79. underscore. This takes cares of compiler generated
  80. symbols (such as __gnu_compiled, __c89_used, etc.). */
  81. if ((!src->is_static && dst[-1].is_static)
  82. || ((src->is_static == dst[-1].is_static)
  83. && ((src->is_func && !dst[-1].is_func)
  84. || ((src->is_func == dst[-1].is_func)
  85. && ((src->name[0] != '_' && dst[-1].name[0] == '_')
  86. || (src->name[0]
  87. && src->name[1] != '_'
  88. && dst[-1].name[1] == '_'))))))
  89. {
  90. DBG (AOUTDEBUG | IDDEBUG,
  91. printf ("[symtab_finalize] favor %s@%c%c over %s@%c%c",
  92. src->name, src->is_static ? 't' : 'T',
  93. src->is_func ? 'F' : 'f',
  94. dst[-1].name, dst[-1].is_static ? 't' : 'T',
  95. dst[-1].is_func ? 'F' : 'f');
  96. printf (" (addr=%lx)\n", (unsigned long) src->addr));
  97. dst[-1] = *src;
  98. }
  99. else
  100. {
  101. DBG (AOUTDEBUG | IDDEBUG,
  102. printf ("[symtab_finalize] favor %s@%c%c over %s@%c%c",
  103. dst[-1].name, dst[-1].is_static ? 't' : 'T',
  104. dst[-1].is_func ? 'F' : 'f',
  105. src->name, src->is_static ? 't' : 'T',
  106. src->is_func ? 'F' : 'f');
  107. printf (" (addr=%lx)\n", (unsigned long) src->addr));
  108. }
  109. }
  110. else
  111. {
  112. if (dst > tab->base && dst[-1].end_addr == 0)
  113. dst[-1].end_addr = src->addr - 1;
  114. /* Retain sym only if it has a non-empty address range. */
  115. if (!src->end_addr || src->addr <= src->end_addr)
  116. {
  117. *dst = *src;
  118. dst++;
  119. prev_addr = src->addr;
  120. }
  121. }
  122. }
  123. if (tab->len > 0 && dst[-1].end_addr == 0)
  124. dst[-1].end_addr
  125. = core_text_sect->vma + bfd_get_section_size (core_text_sect) - 1;
  126. DBG (AOUTDEBUG | IDDEBUG,
  127. printf ("[symtab_finalize]: removed %d duplicate entries\n",
  128. tab->len - (int) (dst - tab->base)));
  129. tab->limit = dst;
  130. tab->len = tab->limit - tab->base;
  131. DBG (AOUTDEBUG | IDDEBUG,
  132. unsigned int j;
  133. for (j = 0; j < tab->len; ++j)
  134. {
  135. printf ("[symtab_finalize] 0x%lx-0x%lx\t%s\n",
  136. (unsigned long) tab->base[j].addr,
  137. (unsigned long) tab->base[j].end_addr,
  138. tab->base[j].name);
  139. }
  140. );
  141. }
  142. #ifdef DEBUG
  143. Sym *
  144. dbg_sym_lookup (Sym_Table *sym_tab, bfd_vma address)
  145. {
  146. unsigned long low, mid, high;
  147. Sym *sym;
  148. fprintf (stderr, "[dbg_sym_lookup] address 0x%lx\n",
  149. (unsigned long) address);
  150. sym = sym_tab->base;
  151. for (low = 0, high = sym_tab->len - 1; low != high;)
  152. {
  153. mid = (high + low) >> 1;
  154. fprintf (stderr, "[dbg_sym_lookup] low=0x%lx, mid=0x%lx, high=0x%lx\n",
  155. low, mid, high);
  156. fprintf (stderr, "[dbg_sym_lookup] sym[m]=0x%lx sym[m + 1]=0x%lx\n",
  157. (unsigned long) sym[mid].addr,
  158. (unsigned long) sym[mid + 1].addr);
  159. if (sym[mid].addr <= address && sym[mid + 1].addr > address)
  160. return &sym[mid];
  161. if (sym[mid].addr > address)
  162. high = mid;
  163. else
  164. low = mid + 1;
  165. }
  166. fprintf (stderr, "[dbg_sym_lookup] binary search fails???\n");
  167. return 0;
  168. }
  169. #endif /* DEBUG */
  170. /* Look up an address in the symbol-table that is sorted by address.
  171. If address does not hit any symbol, 0 is returned. */
  172. Sym *
  173. sym_lookup (Sym_Table *sym_tab, bfd_vma address)
  174. {
  175. long low, high;
  176. long mid = -1;
  177. Sym *sym;
  178. #ifdef DEBUG
  179. int probes = 0;
  180. #endif /* DEBUG */
  181. if (!sym_tab->len)
  182. return 0;
  183. sym = sym_tab->base;
  184. for (low = 0, high = sym_tab->len - 1; low != high;)
  185. {
  186. DBG (LOOKUPDEBUG, ++probes);
  187. mid = (high + low) / 2;
  188. if (sym[mid].addr <= address && sym[mid + 1].addr > address)
  189. {
  190. if (address > sym[mid].end_addr)
  191. {
  192. /* Address falls into gap between
  193. sym[mid] and sym[mid + 1]. */
  194. return 0;
  195. }
  196. else
  197. {
  198. DBG (LOOKUPDEBUG,
  199. printf ("[sym_lookup] %d probes (symtab->len=%u)\n",
  200. probes, sym_tab->len - 1));
  201. return &sym[mid];
  202. }
  203. }
  204. if (sym[mid].addr > address)
  205. high = mid;
  206. else
  207. low = mid + 1;
  208. }
  209. if (sym[mid + 1].addr <= address)
  210. {
  211. if (address > sym[mid + 1].end_addr)
  212. {
  213. /* Address is beyond end of sym[mid + 1]. */
  214. return 0;
  215. }
  216. else
  217. {
  218. DBG (LOOKUPDEBUG, printf ("[sym_lookup] %d (%u) probes, fall off\n",
  219. probes, sym_tab->len - 1));
  220. return &sym[mid + 1];
  221. }
  222. }
  223. return 0;
  224. }