dl-debug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* vi: set sw=4 ts=4: */
  2. /* common debug code for ELF shared library loader
  3. *
  4. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  5. * David Engel, Hongjiu Lu and Mitch D'Souza
  6. * Copyright (C) 2001-2004 Erik Andersen
  7. * Copyright (C) 2002-2004, Axis Communications AB
  8. * Copyright (C) 2003, 2004 Red Hat, Inc.
  9. * Copyright (C) 2002, Steven J. Hill (sjhill@realitydiluted.com)
  10. * Copyright (C) 2001-2002 David A. Schleef
  11. * Copyright (C) 2004 Joakim Tjernlund
  12. * Copyright (C) 2002, Stefan Allius <allius@atecom.com> and
  13. * Eddie C. Dost <ecd@atecom.com>
  14. * Copyright (C) 2003, 2004, 2005 Paul Mundt <lethal@linux-sh.org>
  15. *
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. The name of the above contributors may not be
  24. * used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. */
  39. #include "ldso.h"
  40. #if defined (__SUPPORT_LD_DEBUG__)
  41. /* include the arch-specific _dl_reltypes_tab */
  42. #include "dl-debug.h"
  43. static const char *_dl_reltypes(int type)
  44. {
  45. static char buf[50];
  46. const char *str;
  47. int tabsize;
  48. tabsize = (int)(sizeof(_dl_reltypes_tab) / sizeof(_dl_reltypes_tab[0]));
  49. if (type >= tabsize || (str = _dl_reltypes_tab[type]) == NULL)
  50. str = _dl_simple_ltoa(buf, (unsigned long)type);
  51. return str;
  52. }
  53. static void debug_sym(ElfW(Sym) *symtab, char *strtab, int symtab_index)
  54. {
  55. if (!_dl_debug_symbols || !symtab_index)
  56. return;
  57. _dl_dprintf(_dl_debug_file,
  58. "\n%s\n\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
  59. strtab + symtab[symtab_index].st_name,
  60. symtab[symtab_index].st_value,
  61. symtab[symtab_index].st_size,
  62. symtab[symtab_index].st_info,
  63. symtab[symtab_index].st_other,
  64. symtab[symtab_index].st_shndx);
  65. }
  66. static void debug_reloc(ElfW(Sym) *symtab, char *strtab, ELF_RELOC *rpnt)
  67. {
  68. if (!_dl_debug_reloc)
  69. return;
  70. if (_dl_debug_symbols) {
  71. _dl_dprintf(_dl_debug_file, "\n\t");
  72. } else {
  73. int symtab_index;
  74. const char *sym;
  75. symtab_index = ELF_R_SYM(rpnt->r_info);
  76. sym = symtab_index ? strtab + symtab[symtab_index].st_name : "sym=0x0";
  77. _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
  78. }
  79. _dl_dprintf(_dl_debug_file, "%s\toffset=%x",
  80. _dl_reltypes(ELF_R_TYPE(rpnt->r_info)),
  81. rpnt->r_offset);
  82. #ifdef ELF_USES_RELOCA
  83. _dl_dprintf(_dl_debug_file, "\taddend=%x", rpnt->r_addend);
  84. #endif
  85. _dl_dprintf(_dl_debug_file, "\n");
  86. }
  87. #else
  88. #define debug_sym(symtab, strtab, symtab_index)
  89. #define debug_reloc(symtab, strtab, rpnt)
  90. #endif /* __SUPPORT_LD_DEBUG__ */
  91. #ifdef __LDSO_PRELINK_SUPPORT__
  92. static void
  93. internal_function
  94. _dl_debug_lookup (const char *undef_name, struct elf_resolve *undef_map,
  95. const ElfW(Sym) *ref, struct symbol_ref *value, int type_class)
  96. {
  97. #ifdef SHARED
  98. if (_dl_trace_prelink)
  99. {
  100. int conflict = 0;
  101. struct symbol_ref val = { ref, NULL };
  102. if ((_dl_trace_prelink_map == NULL
  103. || _dl_trace_prelink_map == _dl_loaded_modules)
  104. && undef_map != _dl_loaded_modules)
  105. {
  106. _dl_find_hash(undef_name, &undef_map->symbol_scope,
  107. undef_map, type_class, &val);
  108. if (val.sym != value->sym || val.tpnt != value->tpnt)
  109. conflict = 1;
  110. }
  111. if (unlikely(value->sym && ELF_ST_TYPE(value->sym->st_info) == STT_TLS))
  112. type_class = 4;
  113. if (conflict
  114. || _dl_trace_prelink_map == undef_map
  115. || _dl_trace_prelink_map == NULL
  116. || type_class == 4)
  117. {
  118. _dl_dprintf (1, "%s %x %x -> %x %x ",
  119. conflict ? "conflict" : "lookup",
  120. (size_t) undef_map->mapaddr,
  121. (size_t) (((ElfW(Addr)) ref) - undef_map->mapaddr),
  122. (size_t) (value->tpnt ? value->tpnt->mapaddr : 0),
  123. (size_t) (value->sym ? value->sym->st_value : 0));
  124. if (conflict)
  125. _dl_dprintf (1, "x %x %x ",
  126. (size_t) (val.tpnt ? val.tpnt->mapaddr : 0),
  127. (size_t) (val.sym ? val.sym->st_value : 0));
  128. _dl_dprintf (1, "/%x %s\n", type_class, undef_name);
  129. }
  130. }
  131. #endif
  132. }
  133. #else
  134. #define _dl_debug_lookup(undef_name, undef_map, ref, value, type_class)
  135. #endif