Back to index

glibc  2.9
unwind-forcedunwind.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2005, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License as
00007    published by the Free Software Foundation; either version 2.1 of the
00008    License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <dlfcn.h>
00021 #include <stdio.h>
00022 #include <unwind.h>
00023 #include <pthreadP.h>
00024 
00025 static void (*libgcc_s_resume) (struct _Unwind_Exception *exc);
00026 static _Unwind_Reason_Code (*libgcc_s_personality)
00027   (_Unwind_State, struct _Unwind_Exception *, struct _Unwind_Context *);
00028 static _Unwind_Reason_Code (*libgcc_s_forcedunwind)
00029   (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
00030 static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *);
00031 
00032 void
00033 __attribute_noinline__
00034 pthread_cancel_init (void)
00035 {
00036   void *resume, *personality, *forcedunwind, *getcfa;
00037   void *handle;
00038 
00039   if (__builtin_expect (libgcc_s_getcfa != NULL, 1))
00040     {
00041       /* Force gcc to reload all values.  */
00042       asm volatile ("" ::: "memory");
00043       return;
00044     }
00045 
00046   handle = __libc_dlopen ("libgcc_s.so.1");
00047 
00048   if (handle == NULL
00049       || (resume = __libc_dlsym (handle, "_Unwind_Resume")) == NULL
00050       || (personality = __libc_dlsym (handle, "__gcc_personality_v0")) == NULL
00051       || (forcedunwind = __libc_dlsym (handle, "_Unwind_ForcedUnwind"))
00052         == NULL
00053       || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
00054 #ifdef ARCH_CANCEL_INIT
00055       || ARCH_CANCEL_INIT (handle)
00056 #endif
00057       )
00058     __libc_fatal ("libgcc_s.so.1 must be installed for pthread_cancel to work\n");
00059 
00060   libgcc_s_resume = resume;
00061   libgcc_s_personality = personality;
00062   libgcc_s_forcedunwind = forcedunwind;
00063   /* Make sure libgcc_s_getcfa is written last.  Otherwise,
00064      pthread_cancel_init might return early even when the pointer the
00065      caller is interested in is not initialized yet.  */
00066   atomic_write_barrier ();
00067   libgcc_s_getcfa = getcfa;
00068 }
00069 
00070 /* It's vitally important that _Unwind_Resume not have a stack frame; the
00071    ARM unwinder relies on register state at entrance.  So we write this in
00072    assembly.  */
00073 
00074 asm (
00075 "      .globl _Unwind_Resume\n"
00076 "      .type  _Unwind_Resume, %function\n"
00077 "_Unwind_Resume:\n"
00078 "      stmfd  sp!, {r4, r5, r6, lr}\n"
00079 "      ldr    r4, 1f\n"
00080 "      ldr    r5, 2f\n"
00081 "3:    add    r4, pc, r4\n"
00082 "      ldr    r3, [r4, r5]\n"
00083 "      mov    r6, r0\n"
00084 "      cmp    r3, #0\n"
00085 "      beq    4f\n"
00086 "5:    mov    r0, r6\n"
00087 "      ldmfd  sp!, {r4, r5, r6, lr}\n"
00088 "      bx     r3\n"
00089 "4:    bl     pthread_cancel_init\n"
00090 "      ldr    r3, [r4, r5]\n"
00091 "      b      5b\n"
00092 "1:    .word  _GLOBAL_OFFSET_TABLE_ - 3b - 8\n"
00093 "2:    .word  libgcc_s_resume(GOTOFF)\n"
00094 "      .size  _Unwind_Resume, .-_Unwind_Resume\n"
00095 );
00096 
00097 _Unwind_Reason_Code
00098 __gcc_personality_v0 (_Unwind_State state,
00099                     struct _Unwind_Exception *ue_header,
00100                     struct _Unwind_Context *context)
00101 {
00102   if (__builtin_expect (libgcc_s_personality == NULL, 0))
00103     pthread_cancel_init ();
00104 
00105   return libgcc_s_personality (state, ue_header, context);
00106 }
00107 
00108 _Unwind_Reason_Code
00109 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
00110                     void *stop_argument)
00111 {
00112   if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
00113     pthread_cancel_init ();
00114 
00115   return libgcc_s_forcedunwind (exc, stop, stop_argument);
00116 }
00117 
00118 _Unwind_Word
00119 _Unwind_GetCFA (struct _Unwind_Context *context)
00120 {
00121   if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
00122     pthread_cancel_init ();
00123 
00124   return libgcc_s_getcfa (context);
00125 }