[PATCH] Sample TLS debugging patch.
sjhill@realitydiluted.com
sjhill@realitydiluted.com
Sat Oct 29 18:20:00 GMT 2005
Greetings.
I wrote this patch while working on my new dynamic loader for MIPS and
thought I would share it. I would also like any ideas for its improvement
if anyone wants to critique it. Cheers.
-Steve
diff -ur libc-head/elf/dl-load.c libc-head-tls-debug/elf/dl-load.c
--- libc-head/elf/dl-load.c 2005-10-22 17:23:50.683068750 -0500
+++ libc-head-tls-debug/elf/dl-load.c 2005-10-29 13:15:45.506862500 -0500
@@ -1063,6 +1063,9 @@
/* Nothing to do for an empty segment. */
break;
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("Found TLS header for %s\n", name);
+
l->l_tls_blocksize = ph->p_memsz;
l->l_tls_align = ph->p_align;
if (ph->p_align == 0)
diff -ur libc-head/elf/dl-open.c libc-head-tls-debug/elf/dl-open.c
--- libc-head/elf/dl-open.c 2005-06-13 21:36:32.000000000 -0500
+++ libc-head-tls-debug/elf/dl-open.c 2005-10-29 13:15:45.506862500 -0500
@@ -444,6 +444,8 @@
/* Now that we know the object is loaded successfully add
modules containing TLS data to the slot info table. We
might have to increase its size. */
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("\nAdding to slotinfo for %s\n", imap->l_name);
_dl_add_to_slotinfo (imap);
if (imap->l_need_tls_init)
@@ -455,6 +457,8 @@
_dl_update_slotinfo (imap->l_tls_modid);
# endif
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("\nCalling _dl_init_static_tls()!\n");
GL(dl_init_static_tls) (imap);
assert (imap->l_need_tls_init == 0);
}
diff -ur libc-head/elf/dl-reloc.c libc-head-tls-debug/elf/dl-reloc.c
--- libc-head/elf/dl-reloc.c 2005-07-09 09:58:25.000000000 -0500
+++ libc-head-tls-debug/elf/dl-reloc.c 2005-10-29 13:15:45.506862500 -0500
@@ -73,6 +73,9 @@
- map->l_tls_firstbyte_offset);
map->l_tls_offset = GL(dl_tls_static_used) = offset;
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("_dl_allocate_static_tls(): map->l_tls_offset = 0x%x\n",
+ map->l_tls_offset);
# elif TLS_DTV_AT_TP
size_t used;
size_t check;
@@ -87,6 +90,9 @@
map->l_tls_offset = offset;
GL(dl_tls_static_used) = used;
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("_dl_allocate_static_tls(): map->l_tls_offset = 0x%x\n",
+ map->l_tls_offset);
# else
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
# endif
diff -ur libc-head/elf/rtld.c libc-head-tls-debug/elf/rtld.c
--- libc-head/elf/rtld.c 2005-10-01 09:42:51.000000000 -0500
+++ libc-head-tls-debug/elf/rtld.c 2005-10-29 13:15:45.506862500 -0500
@@ -1165,6 +1165,8 @@
/* This image gets the ID one. */
GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
}
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("Found TLS header for application program\n");
#else
_dl_fatal_printf ("\
ld.so does not support TLS, but program uses it!\n");
@@ -1812,7 +1814,11 @@
multiple threads (from a non-TLS-using libpthread). */
bool was_tls_init_tp_called = tls_init_tp_called;
if (tcbp == NULL)
- tcbp = init_tls ();
+ {
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("Calling init_tls()!\n");
+ tcbp = init_tls ();
+ }
#endif
/* Set up the stack checker's canary. */
@@ -2146,7 +2152,11 @@
#ifdef USE_TLS
/* Add object to slot information data if necessasy. */
if (l->l_tls_blocksize != 0 && tls_init_tp_called)
- _dl_add_to_slotinfo (l);
+ {
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("1:Adding to slotinfo for %s\n", l->l_name);
+ _dl_add_to_slotinfo (l);
+ }
#endif
}
@@ -2177,6 +2187,8 @@
while (l->l_next)
l = l->l_next;
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("Beginning relocation fixups\n");
HP_TIMING_NOW (start);
do
{
@@ -2198,7 +2210,11 @@
#ifdef USE_TLS
/* Add object to slot information data if necessasy. */
if (l->l_tls_blocksize != 0 && tls_init_tp_called)
- _dl_add_to_slotinfo (l);
+ {
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("2:Adding to slotinfo for %s\n", l->l_name);
+ _dl_add_to_slotinfo (l);
+ }
#endif
l = l->l_prev;
@@ -2248,6 +2264,8 @@
/* Now that we have completed relocation, the initializer data
for the TLS blocks has its final values and we can copy them
into the main thread's TLS area, which we allocated above. */
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("Calling _dl_allocate_tls_init()!\n");
_dl_allocate_tls_init (tcbp);
/* And finally install it for the main thread. If ld.so itself uses
@@ -2357,6 +2375,8 @@
DL_DEBUG_STATISTICS },
{ LEN_AND_STR ("unused"), "determined unused DSOs",
DL_DEBUG_UNUSED },
+ { LEN_AND_STR ("tls"), "display tls operations",
+ DL_DEBUG_TLS },
{ LEN_AND_STR ("help"), "display this help message and exit",
DL_DEBUG_HELP },
};
diff -ur libc-head/sysdeps/generic/ldsodefs.h libc-head-tls-debug/sysdeps/generic/ldsodefs.h
--- libc-head/sysdeps/generic/ldsodefs.h 2005-10-08 09:45:28.614301250 -0500
+++ libc-head-tls-debug/sysdeps/generic/ldsodefs.h 2005-10-29 13:15:45.510862750 -0500
@@ -583,9 +583,10 @@
#define DL_DEBUG_FILES (1 << 6)
#define DL_DEBUG_STATISTICS (1 << 7)
#define DL_DEBUG_UNUSED (1 << 8)
+#define DL_DEBUG_TLS (1 << 9)
/* These two are used only internally. */
-#define DL_DEBUG_HELP (1 << 9)
-#define DL_DEBUG_PRELINK (1 << 10)
+#define DL_DEBUG_HELP (1 << 10)
+#define DL_DEBUG_PRELINK (1 << 11)
/* Cached value of `getpagesize ()'. */
EXTERN size_t _dl_pagesize;
diff -ur libc-head/sysdeps/mips/dl-machine.h libc-head-tls-debug/sysdeps/mips/dl-machine.h
--- libc-head/sysdeps/mips/dl-machine.h 2005-03-28 03:32:04.000000000 -0600
+++ libc-head-tls-debug/sysdeps/mips/dl-machine.h 2005-10-29 13:15:45.510862750 -0500
@@ -337,27 +337,47 @@
{
struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
Elf32_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
+ const char *strtab = (const char *) D_PTR (sym_map, l_info[DT_STRTAB]);
+ unsigned int tmp = 0;
if (sym)
value += sym->st_value;
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ tmp = *((unsigned int *)reloc_addr);
+
switch (r_type)
{
case R_MIPS_TLS_DTPMOD64:
case R_MIPS_TLS_DTPMOD32:
if (sym_map)
*(ElfW(Word) *)reloc_addr = sym_map->l_tls_modid;
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0) &&
+ sym_map)
+ _dl_debug_printf ("TLS_DTPMOD : %s, %x, %x\n",
+ (strtab + sym->st_name), tmp,
+ (*((unsigned int *)reloc_addr)));
break;
case R_MIPS_TLS_DTPREL64:
case R_MIPS_TLS_DTPREL32:
*(ElfW(Word) *)reloc_addr += TLS_DTPREL_VALUE (sym);
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ _dl_debug_printf ("TLS_DTPREL : %s, %x, %x\n",
+ (strtab + sym->st_name), tmp,
+ (*((unsigned int *)reloc_addr)));
break;
case R_MIPS_TLS_TPREL32:
case R_MIPS_TLS_TPREL64:
CHECK_STATIC_TLS (map, sym_map);
*(ElfW(Word) *)reloc_addr += TLS_TPREL_VALUE (sym_map, sym);
+ if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_TLS, 0))
+ {
+ _dl_debug_printf ("TLS_TPREL : %s, %x, %x\n",
+ (strtab + sym->st_name), tmp,
+ (*((unsigned int *)reloc_addr)));
+ }
break;
}
More information about the Libc-alpha
mailing list