GDB (API)
/home/stan/gdb/src/gdb/hppa-hpux-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for HP-UX on PA-RISC.
00002 
00003    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "arch-utils.h"
00022 #include "gdbcore.h"
00023 #include "osabi.h"
00024 #include "frame.h"
00025 #include "frame-unwind.h"
00026 #include "trad-frame.h"
00027 #include "symtab.h"
00028 #include "objfiles.h"
00029 #include "inferior.h"
00030 #include "infcall.h"
00031 #include "observer.h"
00032 #include "hppa-tdep.h"
00033 #include "solib-som.h"
00034 #include "solib-pa64.h"
00035 #include "regset.h"
00036 #include "regcache.h"
00037 #include "exceptions.h"
00038 
00039 #include "gdb_string.h"
00040 
00041 #define IS_32BIT_TARGET(_gdbarch) \
00042         ((gdbarch_tdep (_gdbarch))->bytes_per_address == 4)
00043 
00044 /* Bit in the `ss_flag' member of `struct save_state' that indicates
00045    that the 64-bit register values are live.  From
00046    <machine/save_state.h>.  */
00047 #define HPPA_HPUX_SS_WIDEREGS           0x40
00048 
00049 /* Offsets of various parts of `struct save_state'.  From
00050    <machine/save_state.h>.  */
00051 #define HPPA_HPUX_SS_FLAGS_OFFSET       0
00052 #define HPPA_HPUX_SS_NARROW_OFFSET      4
00053 #define HPPA_HPUX_SS_FPBLOCK_OFFSET     256
00054 #define HPPA_HPUX_SS_WIDE_OFFSET        640
00055 
00056 /* The size of `struct save_state.  */
00057 #define HPPA_HPUX_SAVE_STATE_SIZE       1152
00058 
00059 /* The size of `struct pa89_save_state', which corresponds to PA-RISC
00060    1.1, the lowest common denominator that we support.  */
00061 #define HPPA_HPUX_PA89_SAVE_STATE_SIZE  512
00062 
00063 
00064 /* Forward declarations.  */
00065 extern void _initialize_hppa_hpux_tdep (void);
00066 extern initialize_file_ftype _initialize_hppa_hpux_tdep;
00067 
00068 /* Return one if PC is in the call path of a trampoline, else return zero.
00069 
00070    Note we return one for *any* call trampoline (long-call, arg-reloc), not
00071    just shared library trampolines (import, export).  */
00072 
00073 static int
00074 hppa32_hpux_in_solib_call_trampoline (struct gdbarch *gdbarch, CORE_ADDR pc)
00075 {
00076   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00077   struct bound_minimal_symbol minsym;
00078   struct unwind_table_entry *u;
00079 
00080   /* First see if PC is in one of the two C-library trampolines.  */
00081   if (pc == hppa_symbol_address("$$dyncall") 
00082       || pc == hppa_symbol_address("_sr4export"))
00083     return 1;
00084 
00085   minsym = lookup_minimal_symbol_by_pc (pc);
00086   if (minsym.minsym
00087       && strcmp (SYMBOL_LINKAGE_NAME (minsym.minsym), ".stub") == 0)
00088     return 1;
00089 
00090   /* Get the unwind descriptor corresponding to PC, return zero
00091      if no unwind was found.  */
00092   u = find_unwind_entry (pc);
00093   if (!u)
00094     return 0;
00095 
00096   /* If this isn't a linker stub, then return now.  */
00097   if (u->stub_unwind.stub_type == 0)
00098     return 0;
00099 
00100   /* By definition a long-branch stub is a call stub.  */
00101   if (u->stub_unwind.stub_type == LONG_BRANCH)
00102     return 1;
00103 
00104   /* The call and return path execute the same instructions within
00105      an IMPORT stub!  So an IMPORT stub is both a call and return
00106      trampoline.  */
00107   if (u->stub_unwind.stub_type == IMPORT)
00108     return 1;
00109 
00110   /* Parameter relocation stubs always have a call path and may have a
00111      return path.  */
00112   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
00113       || u->stub_unwind.stub_type == EXPORT)
00114     {
00115       CORE_ADDR addr;
00116 
00117       /* Search forward from the current PC until we hit a branch
00118          or the end of the stub.  */
00119       for (addr = pc; addr <= u->region_end; addr += 4)
00120         {
00121           unsigned long insn;
00122 
00123           insn = read_memory_integer (addr, 4, byte_order);
00124 
00125           /* Does it look like a bl?  If so then it's the call path, if
00126              we find a bv or be first, then we're on the return path.  */
00127           if ((insn & 0xfc00e000) == 0xe8000000)
00128             return 1;
00129           else if ((insn & 0xfc00e001) == 0xe800c000
00130                    || (insn & 0xfc000000) == 0xe0000000)
00131             return 0;
00132         }
00133 
00134       /* Should never happen.  */
00135       warning (_("Unable to find branch in parameter relocation stub."));
00136       return 0;
00137     }
00138 
00139   /* Unknown stub type.  For now, just return zero.  */
00140   return 0;
00141 }
00142 
00143 static int
00144 hppa64_hpux_in_solib_call_trampoline (struct gdbarch *gdbarch, CORE_ADDR pc)
00145 {
00146   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00147 
00148   /* PA64 has a completely different stub/trampoline scheme.  Is it
00149      better?  Maybe.  It's certainly harder to determine with any
00150      certainty that we are in a stub because we can not refer to the
00151      unwinders to help.
00152 
00153      The heuristic is simple.  Try to lookup the current PC value in th
00154      minimal symbol table.  If that fails, then assume we are not in a
00155      stub and return.
00156 
00157      Then see if the PC value falls within the section bounds for the
00158      section containing the minimal symbol we found in the first
00159      step.  If it does, then assume we are not in a stub and return.
00160 
00161      Finally peek at the instructions to see if they look like a stub.  */
00162   struct bound_minimal_symbol minsym;
00163   asection *sec;
00164   CORE_ADDR addr;
00165   int insn;
00166 
00167   minsym = lookup_minimal_symbol_by_pc (pc);
00168   if (! minsym.minsym)
00169     return 0;
00170 
00171   sec = SYMBOL_OBJ_SECTION (minsym.objfile, minsym.minsym)->the_bfd_section;
00172 
00173   if (bfd_get_section_vma (sec->owner, sec) <= pc
00174       && pc < (bfd_get_section_vma (sec->owner, sec)
00175                  + bfd_section_size (sec->owner, sec)))
00176       return 0;
00177 
00178   /* We might be in a stub.  Peek at the instructions.  Stubs are 3
00179      instructions long.  */
00180   insn = read_memory_integer (pc, 4, byte_order);
00181 
00182   /* Find out where we think we are within the stub.  */
00183   if ((insn & 0xffffc00e) == 0x53610000)
00184     addr = pc;
00185   else if ((insn & 0xffffffff) == 0xe820d000)
00186     addr = pc - 4;
00187   else if ((insn & 0xffffc00e) == 0x537b0000)
00188     addr = pc - 8;
00189   else
00190     return 0;
00191 
00192   /* Now verify each insn in the range looks like a stub instruction.  */
00193   insn = read_memory_integer (addr, 4, byte_order);
00194   if ((insn & 0xffffc00e) != 0x53610000)
00195     return 0;
00196         
00197   /* Now verify each insn in the range looks like a stub instruction.  */
00198   insn = read_memory_integer (addr + 4, 4, byte_order);
00199   if ((insn & 0xffffffff) != 0xe820d000)
00200     return 0;
00201     
00202   /* Now verify each insn in the range looks like a stub instruction.  */
00203   insn = read_memory_integer (addr + 8, 4, byte_order);
00204   if ((insn & 0xffffc00e) != 0x537b0000)
00205     return 0;
00206 
00207   /* Looks like a stub.  */
00208   return 1;
00209 }
00210 
00211 /* Return one if PC is in the return path of a trampoline, else return zero.
00212 
00213    Note we return one for *any* call trampoline (long-call, arg-reloc), not
00214    just shared library trampolines (import, export).  */
00215 
00216 static int
00217 hppa_hpux_in_solib_return_trampoline (struct gdbarch *gdbarch,
00218                                       CORE_ADDR pc, const char *name)
00219 {
00220   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00221   struct unwind_table_entry *u;
00222 
00223   /* Get the unwind descriptor corresponding to PC, return zero
00224      if no unwind was found.  */
00225   u = find_unwind_entry (pc);
00226   if (!u)
00227     return 0;
00228 
00229   /* If this isn't a linker stub or it's just a long branch stub, then
00230      return zero.  */
00231   if (u->stub_unwind.stub_type == 0 || u->stub_unwind.stub_type == LONG_BRANCH)
00232     return 0;
00233 
00234   /* The call and return path execute the same instructions within
00235      an IMPORT stub!  So an IMPORT stub is both a call and return
00236      trampoline.  */
00237   if (u->stub_unwind.stub_type == IMPORT)
00238     return 1;
00239 
00240   /* Parameter relocation stubs always have a call path and may have a
00241      return path.  */
00242   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
00243       || u->stub_unwind.stub_type == EXPORT)
00244     {
00245       CORE_ADDR addr;
00246 
00247       /* Search forward from the current PC until we hit a branch
00248          or the end of the stub.  */
00249       for (addr = pc; addr <= u->region_end; addr += 4)
00250         {
00251           unsigned long insn;
00252 
00253           insn = read_memory_integer (addr, 4, byte_order);
00254 
00255           /* Does it look like a bl?  If so then it's the call path, if
00256              we find a bv or be first, then we're on the return path.  */
00257           if ((insn & 0xfc00e000) == 0xe8000000)
00258             return 0;
00259           else if ((insn & 0xfc00e001) == 0xe800c000
00260                    || (insn & 0xfc000000) == 0xe0000000)
00261             return 1;
00262         }
00263 
00264       /* Should never happen.  */
00265       warning (_("Unable to find branch in parameter relocation stub."));
00266       return 0;
00267     }
00268 
00269   /* Unknown stub type.  For now, just return zero.  */
00270   return 0;
00271 
00272 }
00273 
00274 /* Figure out if PC is in a trampoline, and if so find out where
00275    the trampoline will jump to.  If not in a trampoline, return zero.
00276 
00277    Simple code examination probably is not a good idea since the code
00278    sequences in trampolines can also appear in user code.
00279 
00280    We use unwinds and information from the minimal symbol table to
00281    determine when we're in a trampoline.  This won't work for ELF
00282    (yet) since it doesn't create stub unwind entries.  Whether or
00283    not ELF will create stub unwinds or normal unwinds for linker
00284    stubs is still being debated.
00285 
00286    This should handle simple calls through dyncall or sr4export,
00287    long calls, argument relocation stubs, and dyncall/sr4export
00288    calling an argument relocation stub.  It even handles some stubs
00289    used in dynamic executables.  */
00290 
00291 static CORE_ADDR
00292 hppa_hpux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
00293 {
00294   struct gdbarch *gdbarch = get_frame_arch (frame);
00295   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00296   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
00297   long orig_pc = pc;
00298   long prev_inst, curr_inst, loc;
00299   struct bound_minimal_symbol msym;
00300   struct unwind_table_entry *u;
00301 
00302   /* Addresses passed to dyncall may *NOT* be the actual address
00303      of the function.  So we may have to do something special.  */
00304   if (pc == hppa_symbol_address("$$dyncall"))
00305     {
00306       pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
00307 
00308       /* If bit 30 (counting from the left) is on, then pc is the address of
00309          the PLT entry for this function, not the address of the function
00310          itself.  Bit 31 has meaning too, but only for MPE.  */
00311       if (pc & 0x2)
00312         pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, word_size,
00313                                               byte_order);
00314     }
00315   if (pc == hppa_symbol_address("$$dyncall_external"))
00316     {
00317       pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
00318       pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, word_size, byte_order);
00319     }
00320   else if (pc == hppa_symbol_address("_sr4export"))
00321     pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
00322 
00323   /* Get the unwind descriptor corresponding to PC, return zero
00324      if no unwind was found.  */
00325   u = find_unwind_entry (pc);
00326   if (!u)
00327     return 0;
00328 
00329   /* If this isn't a linker stub, then return now.  */
00330   /* elz: attention here! (FIXME) because of a compiler/linker 
00331      error, some stubs which should have a non zero stub_unwind.stub_type 
00332      have unfortunately a value of zero.  So this function would return here
00333      as if we were not in a trampoline.  To fix this, we go look at the partial
00334      symbol information, which reports this guy as a stub.
00335      (FIXME): Unfortunately, we are not that lucky: it turns out that the 
00336      partial symbol information is also wrong sometimes.  This is because 
00337      when it is entered (somread.c::som_symtab_read()) it can happen that
00338      if the type of the symbol (from the som) is Entry, and the symbol is
00339      in a shared library, then it can also be a trampoline.  This would be OK,
00340      except that I believe the way they decide if we are ina shared library
00341      does not work.  SOOOO..., even if we have a regular function w/o
00342      trampolines its minimal symbol can be assigned type mst_solib_trampoline.
00343      Also, if we find that the symbol is a real stub, then we fix the unwind
00344      descriptor, and define the stub type to be EXPORT.
00345      Hopefully this is correct most of the times.  */
00346   if (u->stub_unwind.stub_type == 0)
00347     {
00348 
00349 /* elz: NOTE (FIXME!) once the problem with the unwind information is fixed
00350    we can delete all the code which appears between the lines.  */
00351 /*--------------------------------------------------------------------------*/
00352       msym = lookup_minimal_symbol_by_pc (pc);
00353 
00354       if (msym.minsym == NULL
00355           || MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
00356         return orig_pc == pc ? 0 : pc & ~0x3;
00357 
00358       else if (msym.minsym != NULL
00359                && MSYMBOL_TYPE (msym.minsym) == mst_solib_trampoline)
00360         {
00361           struct objfile *objfile;
00362           struct minimal_symbol *msymbol;
00363           int function_found = 0;
00364 
00365           /* Go look if there is another minimal symbol with the same name as 
00366              this one, but with type mst_text.  This would happen if the msym
00367              is an actual trampoline, in which case there would be another
00368              symbol with the same name corresponding to the real function.  */
00369 
00370           ALL_MSYMBOLS (objfile, msymbol)
00371           {
00372             if (MSYMBOL_TYPE (msymbol) == mst_text
00373                 && strcmp (SYMBOL_LINKAGE_NAME (msymbol),
00374                             SYMBOL_LINKAGE_NAME (msym.minsym)) == 0)
00375               {
00376                 function_found = 1;
00377                 break;
00378               }
00379           }
00380 
00381           if (function_found)
00382             /* The type of msym is correct (mst_solib_trampoline), but
00383                the unwind info is wrong, so set it to the correct value.  */
00384             u->stub_unwind.stub_type = EXPORT;
00385           else
00386             /* The stub type info in the unwind is correct (this is not a
00387                trampoline), but the msym type information is wrong, it
00388                should be mst_text.  So we need to fix the msym, and also
00389                get out of this function.  */
00390             {
00391               MSYMBOL_TYPE (msym.minsym) = mst_text;
00392               return orig_pc == pc ? 0 : pc & ~0x3;
00393             }
00394         }
00395 
00396 /*--------------------------------------------------------------------------*/
00397     }
00398 
00399   /* It's a stub.  Search for a branch and figure out where it goes.
00400      Note we have to handle multi insn branch sequences like ldil;ble.
00401      Most (all?) other branches can be determined by examining the contents
00402      of certain registers and the stack.  */
00403 
00404   loc = pc;
00405   curr_inst = 0;
00406   prev_inst = 0;
00407   while (1)
00408     {
00409       /* Make sure we haven't walked outside the range of this stub.  */
00410       if (u != find_unwind_entry (loc))
00411         {
00412           warning (_("Unable to find branch in linker stub"));
00413           return orig_pc == pc ? 0 : pc & ~0x3;
00414         }
00415 
00416       prev_inst = curr_inst;
00417       curr_inst = read_memory_integer (loc, 4, byte_order);
00418 
00419       /* Does it look like a branch external using %r1?  Then it's the
00420          branch from the stub to the actual function.  */
00421       if ((curr_inst & 0xffe0e000) == 0xe0202000)
00422         {
00423           /* Yup.  See if the previous instruction loaded
00424              a value into %r1.  If so compute and return the jump address.  */
00425           if ((prev_inst & 0xffe00000) == 0x20200000)
00426             return (hppa_extract_21 (prev_inst) 
00427                     + hppa_extract_17 (curr_inst)) & ~0x3;
00428           else
00429             {
00430               warning (_("Unable to find ldil X,%%r1 "
00431                          "before ble Y(%%sr4,%%r1)."));
00432               return orig_pc == pc ? 0 : pc & ~0x3;
00433             }
00434         }
00435 
00436       /* Does it look like a be 0(sr0,%r21)? OR 
00437          Does it look like a be, n 0(sr0,%r21)? OR 
00438          Does it look like a bve (r21)? (this is on PA2.0)
00439          Does it look like a bve, n(r21)? (this is also on PA2.0)
00440          That's the branch from an
00441          import stub to an export stub.
00442 
00443          It is impossible to determine the target of the branch via
00444          simple examination of instructions and/or data (consider
00445          that the address in the plabel may be the address of the
00446          bind-on-reference routine in the dynamic loader).
00447 
00448          So we have try an alternative approach.
00449 
00450          Get the name of the symbol at our current location; it should
00451          be a stub symbol with the same name as the symbol in the
00452          shared library.
00453 
00454          Then lookup a minimal symbol with the same name; we should
00455          get the minimal symbol for the target routine in the shared
00456          library as those take precedence of import/export stubs.  */
00457       if ((curr_inst == 0xe2a00000) ||
00458           (curr_inst == 0xe2a00002) ||
00459           (curr_inst == 0xeaa0d000) ||
00460           (curr_inst == 0xeaa0d002))
00461         {
00462           struct bound_minimal_symbol stubsym;
00463           struct minimal_symbol *libsym;
00464 
00465           stubsym = lookup_minimal_symbol_by_pc (loc);
00466           if (stubsym.minsym == NULL)
00467             {
00468               warning (_("Unable to find symbol for 0x%lx"), loc);
00469               return orig_pc == pc ? 0 : pc & ~0x3;
00470             }
00471 
00472           libsym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (stubsym.minsym),
00473                                           NULL, NULL);
00474           if (libsym == NULL)
00475             {
00476               warning (_("Unable to find library symbol for %s."),
00477                        SYMBOL_PRINT_NAME (stubsym.minsym));
00478               return orig_pc == pc ? 0 : pc & ~0x3;
00479             }
00480 
00481           return SYMBOL_VALUE (libsym);
00482         }
00483 
00484       /* Does it look like bl X,%rp or bl X,%r0?  Another way to do a
00485          branch from the stub to the actual function.  */
00486       /*elz */
00487       else if ((curr_inst & 0xffe0e000) == 0xe8400000
00488                || (curr_inst & 0xffe0e000) == 0xe8000000
00489                || (curr_inst & 0xffe0e000) == 0xe800A000)
00490         return (loc + hppa_extract_17 (curr_inst) + 8) & ~0x3;
00491 
00492       /* Does it look like bv (rp)?   Note this depends on the
00493          current stack pointer being the same as the stack
00494          pointer in the stub itself!  This is a branch on from the
00495          stub back to the original caller.  */
00496       /*else if ((curr_inst & 0xffe0e000) == 0xe840c000) */
00497       else if ((curr_inst & 0xffe0f000) == 0xe840c000)
00498         {
00499           /* Yup.  See if the previous instruction loaded
00500              rp from sp - 8.  */
00501           if (prev_inst == 0x4bc23ff1)
00502             {
00503               CORE_ADDR sp;
00504               sp = get_frame_register_unsigned (frame, HPPA_SP_REGNUM);
00505               return read_memory_integer (sp - 8, 4, byte_order) & ~0x3;
00506             }
00507           else
00508             {
00509               warning (_("Unable to find restore of %%rp before bv (%%rp)."));
00510               return orig_pc == pc ? 0 : pc & ~0x3;
00511             }
00512         }
00513 
00514       /* elz: added this case to capture the new instruction
00515          at the end of the return part of an export stub used by
00516          the PA2.0: BVE, n (rp) */
00517       else if ((curr_inst & 0xffe0f000) == 0xe840d000)
00518         {
00519           return (read_memory_integer
00520                   (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
00521                    word_size, byte_order)) & ~0x3;
00522         }
00523 
00524       /* What about be,n 0(sr0,%rp)?  It's just another way we return to
00525          the original caller from the stub.  Used in dynamic executables.  */
00526       else if (curr_inst == 0xe0400002)
00527         {
00528           /* The value we jump to is sitting in sp - 24.  But that's
00529              loaded several instructions before the be instruction.
00530              I guess we could check for the previous instruction being
00531              mtsp %r1,%sr0 if we want to do sanity checking.  */
00532           return (read_memory_integer
00533                   (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
00534                    word_size, byte_order)) & ~0x3;
00535         }
00536 
00537       /* Haven't found the branch yet, but we're still in the stub.
00538          Keep looking.  */
00539       loc += 4;
00540     }
00541 }
00542 
00543 static void
00544 hppa_skip_permanent_breakpoint (struct regcache *regcache)
00545 {
00546   /* To step over a breakpoint instruction on the PA takes some
00547      fiddling with the instruction address queue.
00548 
00549      When we stop at a breakpoint, the IA queue front (the instruction
00550      we're executing now) points at the breakpoint instruction, and
00551      the IA queue back (the next instruction to execute) points to
00552      whatever instruction we would execute after the breakpoint, if it
00553      were an ordinary instruction.  This is the case even if the
00554      breakpoint is in the delay slot of a branch instruction.
00555 
00556      Clearly, to step past the breakpoint, we need to set the queue
00557      front to the back.  But what do we put in the back?  What
00558      instruction comes after that one?  Because of the branch delay
00559      slot, the next insn is always at the back + 4.  */
00560 
00561   ULONGEST pcoq_tail, pcsq_tail;
00562   regcache_cooked_read_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, &pcoq_tail);
00563   regcache_cooked_read_unsigned (regcache, HPPA_PCSQ_TAIL_REGNUM, &pcsq_tail);
00564 
00565   regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, pcoq_tail);
00566   regcache_cooked_write_unsigned (regcache, HPPA_PCSQ_HEAD_REGNUM, pcsq_tail);
00567 
00568   regcache_cooked_write_unsigned (regcache,
00569                                   HPPA_PCOQ_TAIL_REGNUM, pcoq_tail + 4);
00570   /* We can leave the tail's space the same, since there's no jump.  */
00571 }
00572 
00573 
00574 /* Signal frames.  */
00575 struct hppa_hpux_sigtramp_unwind_cache
00576 {
00577   CORE_ADDR base;
00578   struct trad_frame_saved_reg *saved_regs;
00579 };
00580 
00581 static int hppa_hpux_tramp_reg[] = {
00582   HPPA_SAR_REGNUM,
00583   HPPA_PCOQ_HEAD_REGNUM,
00584   HPPA_PCSQ_HEAD_REGNUM,
00585   HPPA_PCOQ_TAIL_REGNUM,
00586   HPPA_PCSQ_TAIL_REGNUM,
00587   HPPA_EIEM_REGNUM,
00588   HPPA_IIR_REGNUM,
00589   HPPA_ISR_REGNUM,
00590   HPPA_IOR_REGNUM,
00591   HPPA_IPSW_REGNUM,
00592   -1,
00593   HPPA_SR4_REGNUM,
00594   HPPA_SR4_REGNUM + 1,
00595   HPPA_SR4_REGNUM + 2,
00596   HPPA_SR4_REGNUM + 3,
00597   HPPA_SR4_REGNUM + 4,
00598   HPPA_SR4_REGNUM + 5,
00599   HPPA_SR4_REGNUM + 6,
00600   HPPA_SR4_REGNUM + 7,
00601   HPPA_RCR_REGNUM,
00602   HPPA_PID0_REGNUM,
00603   HPPA_PID1_REGNUM,
00604   HPPA_CCR_REGNUM,
00605   HPPA_PID2_REGNUM,
00606   HPPA_PID3_REGNUM,
00607   HPPA_TR0_REGNUM,
00608   HPPA_TR0_REGNUM + 1,
00609   HPPA_TR0_REGNUM + 2,
00610   HPPA_CR27_REGNUM
00611 };
00612 
00613 static struct hppa_hpux_sigtramp_unwind_cache *
00614 hppa_hpux_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
00615                                        void **this_cache)
00616 
00617 {
00618   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00619   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00620   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00621   struct hppa_hpux_sigtramp_unwind_cache *info;
00622   unsigned int flag;
00623   CORE_ADDR sp, scptr, off;
00624   int i, incr, szoff;
00625 
00626   if (*this_cache)
00627     return *this_cache;
00628 
00629   info = FRAME_OBSTACK_ZALLOC (struct hppa_hpux_sigtramp_unwind_cache);
00630   *this_cache = info;
00631   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00632 
00633   sp = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);
00634 
00635   if (IS_32BIT_TARGET (gdbarch))
00636     scptr = sp - 1352;
00637   else
00638     scptr = sp - 1520;
00639 
00640   off = scptr;
00641 
00642   /* See /usr/include/machine/save_state.h for the structure of the
00643      save_state_t structure.  */
00644   
00645   flag = read_memory_unsigned_integer (scptr + HPPA_HPUX_SS_FLAGS_OFFSET,
00646                                        4, byte_order);
00647 
00648   if (!(flag & HPPA_HPUX_SS_WIDEREGS))
00649     {
00650       /* Narrow registers.  */
00651       off = scptr + HPPA_HPUX_SS_NARROW_OFFSET;
00652       incr = 4;
00653       szoff = 0;
00654     }
00655   else
00656     {
00657       /* Wide registers.  */
00658       off = scptr + HPPA_HPUX_SS_WIDE_OFFSET + 8;
00659       incr = 8;
00660       szoff = (tdep->bytes_per_address == 4 ? 4 : 0);
00661     }
00662 
00663   for (i = 1; i < 32; i++)
00664     {
00665       info->saved_regs[HPPA_R0_REGNUM + i].addr = off + szoff;
00666       off += incr;
00667     }
00668 
00669   for (i = 0; i < ARRAY_SIZE (hppa_hpux_tramp_reg); i++)
00670     {
00671       if (hppa_hpux_tramp_reg[i] > 0)
00672         info->saved_regs[hppa_hpux_tramp_reg[i]].addr = off + szoff;
00673 
00674       off += incr;
00675     }
00676 
00677   /* TODO: fp regs */
00678 
00679   info->base = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);
00680 
00681   return info;
00682 }
00683 
00684 static void
00685 hppa_hpux_sigtramp_frame_this_id (struct frame_info *this_frame,
00686                                    void **this_prologue_cache,
00687                                    struct frame_id *this_id)
00688 {
00689   struct hppa_hpux_sigtramp_unwind_cache *info
00690     = hppa_hpux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
00691 
00692   *this_id = frame_id_build (info->base, get_frame_pc (this_frame));
00693 }
00694 
00695 static struct value *
00696 hppa_hpux_sigtramp_frame_prev_register (struct frame_info *this_frame,
00697                                         void **this_prologue_cache,
00698                                         int regnum)
00699 {
00700   struct hppa_hpux_sigtramp_unwind_cache *info
00701     = hppa_hpux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
00702 
00703   return hppa_frame_prev_register_helper (this_frame,
00704                                           info->saved_regs, regnum);
00705 }
00706 
00707 static int
00708 hppa_hpux_sigtramp_unwind_sniffer (const struct frame_unwind *self,
00709                                    struct frame_info *this_frame,
00710                                    void **this_cache)
00711 {
00712   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00713   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00714   struct unwind_table_entry *u;
00715   CORE_ADDR pc = get_frame_pc (this_frame);
00716 
00717   u = find_unwind_entry (pc);
00718 
00719   /* If this is an export stub, try to get the unwind descriptor for
00720      the actual function itself.  */
00721   if (u && u->stub_unwind.stub_type == EXPORT)
00722     {
00723       gdb_byte buf[HPPA_INSN_SIZE];
00724       unsigned long insn;
00725 
00726       if (!safe_frame_unwind_memory (this_frame, u->region_start,
00727                                      buf, sizeof buf))
00728         return 0;
00729 
00730       insn = extract_unsigned_integer (buf, sizeof buf, byte_order);
00731       if ((insn & 0xffe0e000) == 0xe8400000)
00732         u = find_unwind_entry(u->region_start + hppa_extract_17 (insn) + 8);
00733     }
00734 
00735   if (u && u->HP_UX_interrupt_marker)
00736     return 1;
00737 
00738   return 0;
00739 }
00740 
00741 static const struct frame_unwind hppa_hpux_sigtramp_frame_unwind = {
00742   SIGTRAMP_FRAME,
00743   default_frame_unwind_stop_reason,
00744   hppa_hpux_sigtramp_frame_this_id,
00745   hppa_hpux_sigtramp_frame_prev_register,
00746   NULL,
00747   hppa_hpux_sigtramp_unwind_sniffer
00748 };
00749 
00750 static CORE_ADDR
00751 hppa32_hpux_find_global_pointer (struct gdbarch *gdbarch,
00752                                  struct value *function)
00753 {
00754   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00755   CORE_ADDR faddr;
00756   
00757   faddr = value_as_address (function);
00758 
00759   /* Is this a plabel? If so, dereference it to get the gp value.  */
00760   if (faddr & 2)
00761     {
00762       int status;
00763       gdb_byte buf[4];
00764 
00765       faddr &= ~3;
00766 
00767       status = target_read_memory (faddr + 4, buf, sizeof (buf));
00768       if (status == 0)
00769         return extract_unsigned_integer (buf, sizeof (buf), byte_order);
00770     }
00771 
00772   return gdbarch_tdep (gdbarch)->solib_get_got_by_pc (faddr);
00773 }
00774 
00775 static CORE_ADDR
00776 hppa64_hpux_find_global_pointer (struct gdbarch *gdbarch,
00777                                  struct value *function)
00778 {
00779   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00780   CORE_ADDR faddr;
00781   gdb_byte buf[32];
00782 
00783   faddr = value_as_address (function);
00784 
00785   if (pc_in_section (faddr, ".opd"))
00786     {
00787       target_read_memory (faddr, buf, sizeof (buf));
00788       return extract_unsigned_integer (&buf[24], 8, byte_order);
00789     }
00790   else
00791     {
00792       return gdbarch_tdep (gdbarch)->solib_get_got_by_pc (faddr);
00793     }
00794 }
00795 
00796 static unsigned int ldsid_pattern[] = {
00797   0x000010a0, /* ldsid (rX),rY */
00798   0x00001820, /* mtsp rY,sr0 */
00799   0xe0000000  /* be,n (sr0,rX) */
00800 };
00801 
00802 static CORE_ADDR
00803 hppa_hpux_search_pattern (struct gdbarch *gdbarch,
00804                           CORE_ADDR start, CORE_ADDR end,
00805                           unsigned int *patterns, int count)
00806 {
00807   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00808   int num_insns = (end - start + HPPA_INSN_SIZE) / HPPA_INSN_SIZE;
00809   unsigned int *insns;
00810   gdb_byte *buf;
00811   int offset, i;
00812 
00813   buf = alloca (num_insns * HPPA_INSN_SIZE);
00814   insns = alloca (num_insns * sizeof (unsigned int));
00815 
00816   read_memory (start, buf, num_insns * HPPA_INSN_SIZE);
00817   for (i = 0; i < num_insns; i++, buf += HPPA_INSN_SIZE)
00818     insns[i] = extract_unsigned_integer (buf, HPPA_INSN_SIZE, byte_order);
00819 
00820   for (offset = 0; offset <= num_insns - count; offset++)
00821     {
00822       for (i = 0; i < count; i++)
00823         {
00824           if ((insns[offset + i] & patterns[i]) != patterns[i])
00825             break;
00826         }
00827       if (i == count)
00828         break;
00829     }
00830 
00831   if (offset <= num_insns - count)
00832     return start + offset * HPPA_INSN_SIZE;
00833   else
00834     return 0;
00835 }
00836 
00837 static CORE_ADDR
00838 hppa32_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
00839                                         int *argreg)
00840 {
00841   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00842   struct objfile *obj;
00843   struct obj_section *sec;
00844   struct hppa_objfile_private *priv;
00845   struct frame_info *frame;
00846   struct unwind_table_entry *u;
00847   CORE_ADDR addr, rp;
00848   gdb_byte buf[4];
00849   unsigned int insn;
00850 
00851   sec = find_pc_section (pc);
00852   obj = sec->objfile;
00853   priv = objfile_data (obj, hppa_objfile_priv_data);
00854 
00855   if (!priv)
00856     priv = hppa_init_objfile_priv_data (obj);
00857   if (!priv)
00858     error (_("Internal error creating objfile private data."));
00859 
00860   /* Use the cached value if we have one.  */
00861   if (priv->dummy_call_sequence_addr != 0)
00862     {
00863       *argreg = priv->dummy_call_sequence_reg;
00864       return priv->dummy_call_sequence_addr;
00865     }
00866 
00867   /* First try a heuristic; if we are in a shared library call, our return
00868      pointer is likely to point at an export stub.  */
00869   frame = get_current_frame ();
00870   rp = frame_unwind_register_unsigned (frame, 2);
00871   u = find_unwind_entry (rp);
00872   if (u && u->stub_unwind.stub_type == EXPORT)
00873     {
00874       addr = hppa_hpux_search_pattern (gdbarch,
00875                                        u->region_start, u->region_end,
00876                                        ldsid_pattern, 
00877                                        ARRAY_SIZE (ldsid_pattern));
00878       if (addr)
00879         goto found_pattern;
00880     }
00881 
00882   /* Next thing to try is to look for an export stub.  */
00883   if (priv->unwind_info)
00884     {
00885       int i;
00886 
00887       for (i = 0; i < priv->unwind_info->last; i++)
00888         {
00889           struct unwind_table_entry *u;
00890           u = &priv->unwind_info->table[i];
00891           if (u->stub_unwind.stub_type == EXPORT)
00892             {
00893               addr = hppa_hpux_search_pattern (gdbarch,
00894                                                u->region_start, u->region_end,
00895                                                ldsid_pattern, 
00896                                                ARRAY_SIZE (ldsid_pattern));
00897               if (addr)
00898                 {
00899                   goto found_pattern;
00900                 }
00901             }
00902         }
00903     }
00904 
00905   /* Finally, if this is the main executable, try to locate a sequence 
00906      from noshlibs */
00907   addr = hppa_symbol_address ("noshlibs");
00908   sec = find_pc_section (addr);
00909 
00910   if (sec && sec->objfile == obj)
00911     {
00912       CORE_ADDR start, end;
00913 
00914       find_pc_partial_function (addr, NULL, &start, &end);
00915       if (start != 0 && end != 0)
00916         {
00917           addr = hppa_hpux_search_pattern (gdbarch, start, end, ldsid_pattern,
00918                                            ARRAY_SIZE (ldsid_pattern));
00919           if (addr)
00920             goto found_pattern;
00921         }
00922     }
00923 
00924   /* Can't find a suitable sequence.  */
00925   return 0;
00926 
00927 found_pattern:
00928   target_read_memory (addr, buf, sizeof (buf));
00929   insn = extract_unsigned_integer (buf, sizeof (buf), byte_order);
00930   priv->dummy_call_sequence_addr = addr;
00931   priv->dummy_call_sequence_reg = (insn >> 21) & 0x1f;
00932 
00933   *argreg = priv->dummy_call_sequence_reg;
00934   return priv->dummy_call_sequence_addr;
00935 }
00936 
00937 static CORE_ADDR
00938 hppa64_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
00939                                         int *argreg)
00940 {
00941   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00942   struct objfile *obj;
00943   struct obj_section *sec;
00944   struct hppa_objfile_private *priv;
00945   CORE_ADDR addr;
00946   struct minimal_symbol *msym;
00947 
00948   sec = find_pc_section (pc);
00949   obj = sec->objfile;
00950   priv = objfile_data (obj, hppa_objfile_priv_data);
00951 
00952   if (!priv)
00953     priv = hppa_init_objfile_priv_data (obj);
00954   if (!priv)
00955     error (_("Internal error creating objfile private data."));
00956 
00957   /* Use the cached value if we have one.  */
00958   if (priv->dummy_call_sequence_addr != 0)
00959     {
00960       *argreg = priv->dummy_call_sequence_reg;
00961       return priv->dummy_call_sequence_addr;
00962     }
00963 
00964   /* FIXME: Without stub unwind information, locating a suitable sequence is
00965      fairly difficult.  For now, we implement a very naive and inefficient
00966      scheme; try to read in blocks of code, and look for a "bve,n (rp)" 
00967      instruction.  These are likely to occur at the end of functions, so
00968      we only look at the last two instructions of each function.  */
00969   ALL_OBJFILE_MSYMBOLS (obj, msym)
00970     {
00971       CORE_ADDR begin, end;
00972       const char *name;
00973       gdb_byte buf[2 * HPPA_INSN_SIZE];
00974       int offset;
00975 
00976       find_pc_partial_function (SYMBOL_VALUE_ADDRESS (msym), &name,
00977                                 &begin, &end);
00978 
00979       if (name == NULL || begin == 0 || end == 0)
00980         continue;
00981 
00982       if (target_read_memory (end - sizeof (buf), buf, sizeof (buf)) == 0)
00983         {
00984           for (offset = 0; offset < sizeof (buf); offset++)
00985             {
00986               unsigned int insn;
00987 
00988               insn = extract_unsigned_integer (buf + offset,
00989                                                HPPA_INSN_SIZE, byte_order);
00990               if (insn == 0xe840d002) /* bve,n (rp) */
00991                 {
00992                   addr = (end - sizeof (buf)) + offset;
00993                   goto found_pattern;
00994                 }
00995             }
00996         }
00997     }
00998 
00999   /* Can't find a suitable sequence.  */
01000   return 0;
01001 
01002 found_pattern:
01003   priv->dummy_call_sequence_addr = addr;
01004   /* Right now we only look for a "bve,l (rp)" sequence, so the register is 
01005      always HPPA_RP_REGNUM.  */
01006   priv->dummy_call_sequence_reg = HPPA_RP_REGNUM;
01007 
01008   *argreg = priv->dummy_call_sequence_reg;
01009   return priv->dummy_call_sequence_addr;
01010 }
01011 
01012 static CORE_ADDR
01013 hppa_hpux_find_import_stub_for_addr (CORE_ADDR funcaddr)
01014 {
01015   struct objfile *objfile;
01016   struct bound_minimal_symbol funsym;
01017   struct minimal_symbol *stubsym;
01018   CORE_ADDR stubaddr;
01019 
01020   funsym = lookup_minimal_symbol_by_pc (funcaddr);
01021   stubaddr = 0;
01022 
01023   ALL_OBJFILES (objfile)
01024     {
01025       stubsym = lookup_minimal_symbol_solib_trampoline
01026         (SYMBOL_LINKAGE_NAME (funsym.minsym), objfile);
01027 
01028       if (stubsym)
01029         {
01030           struct unwind_table_entry *u;
01031 
01032           u = find_unwind_entry (SYMBOL_VALUE (stubsym));
01033           if (u == NULL 
01034               || (u->stub_unwind.stub_type != IMPORT
01035                   && u->stub_unwind.stub_type != IMPORT_SHLIB))
01036             continue;
01037 
01038           stubaddr = SYMBOL_VALUE (stubsym);
01039 
01040           /* If we found an IMPORT stub, then we can stop searching;
01041              if we found an IMPORT_SHLIB, we want to continue the search
01042              in the hopes that we will find an IMPORT stub.  */
01043           if (u->stub_unwind.stub_type == IMPORT)
01044             break;
01045         }
01046     }
01047 
01048   return stubaddr;
01049 }
01050 
01051 static int
01052 hppa_hpux_sr_for_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
01053 {
01054   int sr;
01055   /* The space register to use is encoded in the top 2 bits of the address.  */
01056   sr = addr >> (gdbarch_tdep (gdbarch)->bytes_per_address * 8 - 2);
01057   return sr + 4;
01058 }
01059 
01060 static CORE_ADDR
01061 hppa_hpux_find_dummy_bpaddr (CORE_ADDR addr)
01062 {
01063   /* In order for us to restore the space register to its starting state, 
01064      we need the dummy trampoline to return to an instruction address in 
01065      the same space as where we started the call.  We used to place the 
01066      breakpoint near the current pc, however, this breaks nested dummy calls 
01067      as the nested call will hit the breakpoint address and terminate 
01068      prematurely.  Instead, we try to look for an address in the same space to 
01069      put the breakpoint.  
01070      
01071      This is similar in spirit to putting the breakpoint at the "entry point"
01072      of an executable.  */
01073 
01074   struct obj_section *sec;
01075   struct unwind_table_entry *u;
01076   struct minimal_symbol *msym;
01077   CORE_ADDR func;
01078 
01079   sec = find_pc_section (addr);
01080   if (sec)
01081     {
01082       /* First try the lowest address in the section; we can use it as long
01083          as it is "regular" code (i.e. not a stub).  */
01084       u = find_unwind_entry (obj_section_addr (sec));
01085       if (!u || u->stub_unwind.stub_type == 0)
01086         return obj_section_addr (sec);
01087 
01088       /* Otherwise, we need to find a symbol for a regular function.  We
01089          do this by walking the list of msymbols in the objfile.  The symbol
01090          we find should not be the same as the function that was passed in.  */
01091 
01092       /* FIXME: this is broken, because we can find a function that will be
01093          called by the dummy call target function, which will still not 
01094          work.  */
01095 
01096       find_pc_partial_function (addr, NULL, &func, NULL);
01097       ALL_OBJFILE_MSYMBOLS (sec->objfile, msym)
01098         {
01099           u = find_unwind_entry (SYMBOL_VALUE_ADDRESS (msym));
01100           if (func != SYMBOL_VALUE_ADDRESS (msym) 
01101               && (!u || u->stub_unwind.stub_type == 0))
01102             return SYMBOL_VALUE_ADDRESS (msym);
01103         }
01104     }
01105 
01106   warning (_("Cannot find suitable address to place dummy breakpoint; nested "
01107              "calls may fail."));
01108   return addr - 4;
01109 }
01110 
01111 static CORE_ADDR
01112 hppa_hpux_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
01113                            CORE_ADDR funcaddr,
01114                            struct value **args, int nargs,
01115                            struct type *value_type,
01116                            CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
01117                            struct regcache *regcache)
01118 {
01119   CORE_ADDR pc, stubaddr;
01120   int argreg = 0;
01121 
01122   pc = regcache_read_pc (regcache);
01123 
01124   /* Note: we don't want to pass a function descriptor here; push_dummy_call
01125      fills in the PIC register for us.  */
01126   funcaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funcaddr, NULL);
01127 
01128   /* The simple case is where we call a function in the same space that we are
01129      currently in; in that case we don't really need to do anything.  */
01130   if (hppa_hpux_sr_for_addr (gdbarch, pc)
01131       == hppa_hpux_sr_for_addr (gdbarch, funcaddr))
01132     {
01133       /* Intraspace call.  */
01134       *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
01135       *real_pc = funcaddr;
01136       regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, *bp_addr);
01137 
01138       return sp;
01139     }
01140 
01141   /* In order to make an interspace call, we need to go through a stub.
01142      gcc supplies an appropriate stub called "__gcc_plt_call", however, if
01143      an application is compiled with HP compilers then this stub is not
01144      available.  We used to fallback to "__d_plt_call", however that stub
01145      is not entirely useful for us because it doesn't do an interspace
01146      return back to the caller.  Also, on hppa64-hpux, there is no 
01147      __gcc_plt_call available.  In order to keep the code uniform, we
01148      instead don't use either of these stubs, but instead write our own
01149      onto the stack.
01150 
01151      A problem arises since the stack is located in a different space than
01152      code, so in order to branch to a stack stub, we will need to do an
01153      interspace branch.  Previous versions of gdb did this by modifying code
01154      at the current pc and doing single-stepping to set the pcsq.  Since this
01155      is highly undesirable, we use a different scheme:
01156 
01157      All we really need to do the branch to the stub is a short instruction
01158      sequence like this:
01159       
01160      PA1.1:
01161                 ldsid (rX),r1
01162                 mtsp r1,sr0
01163                 be,n (sr0,rX)
01164 
01165      PA2.0:
01166                 bve,n (sr0,rX)
01167 
01168      Instead of writing these sequences ourselves, we can find it in
01169      the instruction stream that belongs to the current space.  While this
01170      seems difficult at first, we are actually guaranteed to find the sequences
01171      in several places:
01172 
01173      For 32-bit code:
01174      - in export stubs for shared libraries
01175      - in the "noshlibs" routine in the main module
01176 
01177      For 64-bit code:
01178      - at the end of each "regular" function
01179 
01180      We cache the address of these sequences in the objfile's private data
01181      since these operations can potentially be quite expensive.
01182 
01183      So, what we do is:
01184      - write a stack trampoline
01185      - look for a suitable instruction sequence in the current space
01186      - point the sequence at the trampoline
01187      - set the return address of the trampoline to the current space 
01188        (see hppa_hpux_find_dummy_call_bpaddr)
01189      - set the continuing address of the "dummy code" as the sequence.  */
01190 
01191   if (IS_32BIT_TARGET (gdbarch))
01192     {
01193 #define INSN(I1, I2, I3, I4) 0x ## I1, 0x ## I2, 0x ## I3, 0x ## I4
01194      static const gdb_byte hppa32_tramp[] = {
01195         INSN(0f,df,12,91), /* stw r31,-8(,sp) */
01196         INSN(02,c0,10,a1), /* ldsid (,r22),r1 */
01197         INSN(00,01,18,20), /* mtsp r1,sr0 */
01198         INSN(e6,c0,00,00), /* be,l 0(sr0,r22),%sr0,%r31 */
01199         INSN(08,1f,02,42), /* copy r31,rp */
01200         INSN(0f,d1,10,82), /* ldw -8(,sp),rp */
01201         INSN(00,40,10,a1), /* ldsid (,rp),r1 */
01202         INSN(00,01,18,20), /* mtsp r1,sr0 */
01203         INSN(e0,40,00,00), /* be 0(sr0,rp) */
01204         INSN(08,00,02,40)  /* nop */
01205       };
01206 
01207       /* for hppa32, we must call the function through a stub so that on
01208          return it can return to the space of our trampoline.  */
01209       stubaddr = hppa_hpux_find_import_stub_for_addr (funcaddr);
01210       if (stubaddr == 0)
01211         error (_("Cannot call external function not referenced by application "
01212                "(no import stub).\n"));
01213       regcache_cooked_write_unsigned (regcache, 22, stubaddr);
01214 
01215       write_memory (sp, hppa32_tramp, sizeof (hppa32_tramp));
01216 
01217       *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
01218       regcache_cooked_write_unsigned (regcache, 31, *bp_addr);
01219 
01220       *real_pc = hppa32_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
01221       if (*real_pc == 0)
01222         error (_("Cannot make interspace call from here."));
01223 
01224       regcache_cooked_write_unsigned (regcache, argreg, sp);
01225 
01226       sp += sizeof (hppa32_tramp);
01227     }
01228   else
01229     {
01230       static const gdb_byte hppa64_tramp[] = {
01231         INSN(ea,c0,f0,00), /* bve,l (r22),%r2 */
01232         INSN(0f,df,12,d1), /* std r31,-8(,sp) */
01233         INSN(0f,d1,10,c2), /* ldd -8(,sp),rp */
01234         INSN(e8,40,d0,02), /* bve,n (rp) */
01235         INSN(08,00,02,40)  /* nop */
01236       };
01237 #undef INSN
01238 
01239       /* for hppa64, we don't need to call through a stub; all functions
01240          return via a bve.  */
01241       regcache_cooked_write_unsigned (regcache, 22, funcaddr);
01242       write_memory (sp, hppa64_tramp, sizeof (hppa64_tramp));
01243 
01244       *bp_addr = pc - 4;
01245       regcache_cooked_write_unsigned (regcache, 31, *bp_addr);
01246 
01247       *real_pc = hppa64_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
01248       if (*real_pc == 0)
01249         error (_("Cannot make interspace call from here."));
01250 
01251       regcache_cooked_write_unsigned (regcache, argreg, sp);
01252 
01253       sp += sizeof (hppa64_tramp);
01254     }
01255 
01256   sp = gdbarch_frame_align (gdbarch, sp);
01257 
01258   return sp;
01259 }
01260 
01261 
01262 
01263 static void
01264 hppa_hpux_supply_ss_narrow (struct regcache *regcache,
01265                             int regnum, const gdb_byte *save_state)
01266 {
01267   const gdb_byte *ss_narrow = save_state + HPPA_HPUX_SS_NARROW_OFFSET;
01268   int i, offset = 0;
01269 
01270   for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
01271     {
01272       if (regnum == i || regnum == -1)
01273         regcache_raw_supply (regcache, i, ss_narrow + offset);
01274 
01275       offset += 4;
01276     }
01277 }
01278 
01279 static void
01280 hppa_hpux_supply_ss_fpblock (struct regcache *regcache,
01281                              int regnum, const gdb_byte *save_state)
01282 {
01283   const gdb_byte *ss_fpblock = save_state + HPPA_HPUX_SS_FPBLOCK_OFFSET;
01284   int i, offset = 0;
01285 
01286   /* FIXME: We view the floating-point state as 64 single-precision
01287      registers for 32-bit code, and 32 double-precision register for
01288      64-bit code.  This distinction is artificial and should be
01289      eliminated.  If that ever happens, we should remove the if-clause
01290      below.  */
01291 
01292   if (register_size (get_regcache_arch (regcache), HPPA_FP0_REGNUM) == 4)
01293     {
01294       for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 64; i++)
01295         {
01296           if (regnum == i || regnum == -1)
01297             regcache_raw_supply (regcache, i, ss_fpblock + offset);
01298 
01299           offset += 4;
01300         }
01301     }
01302   else
01303     {
01304       for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32; i++)
01305         {
01306           if (regnum == i || regnum == -1)
01307             regcache_raw_supply (regcache, i, ss_fpblock + offset);
01308 
01309           offset += 8;
01310         }
01311     }
01312 }
01313 
01314 static void
01315 hppa_hpux_supply_ss_wide (struct regcache *regcache,
01316                           int regnum, const gdb_byte *save_state)
01317 {
01318   const gdb_byte *ss_wide = save_state + HPPA_HPUX_SS_WIDE_OFFSET;
01319   int i, offset = 8;
01320 
01321   if (register_size (get_regcache_arch (regcache), HPPA_R1_REGNUM) == 4)
01322     offset += 4;
01323 
01324   for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
01325     {
01326       if (regnum == i || regnum == -1)
01327         regcache_raw_supply (regcache, i, ss_wide + offset);
01328 
01329       offset += 8;
01330     }
01331 }
01332 
01333 static void
01334 hppa_hpux_supply_save_state (const struct regset *regset,
01335                              struct regcache *regcache,
01336                              int regnum, const void *regs, size_t len)
01337 {
01338   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01339   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01340   const gdb_byte *proc_info = regs;
01341   const gdb_byte *save_state = proc_info + 8;
01342   ULONGEST flags;
01343 
01344   flags = extract_unsigned_integer (save_state + HPPA_HPUX_SS_FLAGS_OFFSET,
01345                                     4, byte_order);
01346   if (regnum == -1 || regnum == HPPA_FLAGS_REGNUM)
01347     {
01348       size_t size = register_size (gdbarch, HPPA_FLAGS_REGNUM);
01349       gdb_byte buf[8];
01350 
01351       store_unsigned_integer (buf, size, byte_order, flags);
01352       regcache_raw_supply (regcache, HPPA_FLAGS_REGNUM, buf);
01353     }
01354 
01355   /* If the SS_WIDEREGS flag is set, we really do need the full
01356      `struct save_state'.  */
01357   if (flags & HPPA_HPUX_SS_WIDEREGS && len < HPPA_HPUX_SAVE_STATE_SIZE)
01358     error (_("Register set contents too small"));
01359 
01360   if (flags & HPPA_HPUX_SS_WIDEREGS)
01361     hppa_hpux_supply_ss_wide (regcache, regnum, save_state);
01362   else
01363     hppa_hpux_supply_ss_narrow (regcache, regnum, save_state);
01364 
01365   hppa_hpux_supply_ss_fpblock (regcache, regnum, save_state);
01366 }
01367 
01368 /* HP-UX register set.  */
01369 
01370 static struct regset hppa_hpux_regset =
01371 {
01372   NULL,
01373   hppa_hpux_supply_save_state
01374 };
01375 
01376 static const struct regset *
01377 hppa_hpux_regset_from_core_section (struct gdbarch *gdbarch,
01378                                     const char *sect_name, size_t sect_size)
01379 {
01380   if (strcmp (sect_name, ".reg") == 0
01381       && sect_size >= HPPA_HPUX_PA89_SAVE_STATE_SIZE + 8)
01382     return &hppa_hpux_regset;
01383 
01384   return NULL;
01385 }
01386 
01387 
01388 /* Bit in the `ss_flag' member of `struct save_state' that indicates
01389    the state was saved from a system call.  From
01390    <machine/save_state.h>.  */
01391 #define HPPA_HPUX_SS_INSYSCALL  0x02
01392 
01393 static CORE_ADDR
01394 hppa_hpux_read_pc (struct regcache *regcache)
01395 {
01396   ULONGEST flags;
01397 
01398   /* If we're currently in a system call return the contents of %r31.  */
01399   regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
01400   if (flags & HPPA_HPUX_SS_INSYSCALL)
01401     {
01402       ULONGEST pc;
01403       regcache_cooked_read_unsigned (regcache, HPPA_R31_REGNUM, &pc);
01404       return pc & ~0x3;
01405     }
01406 
01407   return hppa_read_pc (regcache);
01408 }
01409 
01410 static void
01411 hppa_hpux_write_pc (struct regcache *regcache, CORE_ADDR pc)
01412 {
01413   ULONGEST flags;
01414 
01415   /* If we're currently in a system call also write PC into %r31.  */
01416   regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
01417   if (flags & HPPA_HPUX_SS_INSYSCALL)
01418     regcache_cooked_write_unsigned (regcache, HPPA_R31_REGNUM, pc | 0x3);
01419 
01420   hppa_write_pc (regcache, pc);
01421 }
01422 
01423 static CORE_ADDR
01424 hppa_hpux_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01425 {
01426   ULONGEST flags;
01427 
01428   /* If we're currently in a system call return the contents of %r31.  */
01429   flags = frame_unwind_register_unsigned (next_frame, HPPA_FLAGS_REGNUM);
01430   if (flags & HPPA_HPUX_SS_INSYSCALL)
01431     return frame_unwind_register_unsigned (next_frame, HPPA_R31_REGNUM) & ~0x3;
01432 
01433   return hppa_unwind_pc (gdbarch, next_frame);
01434 }
01435 
01436 
01437 /* Given the current value of the pc, check to see if it is inside a stub, and
01438    if so, change the value of the pc to point to the caller of the stub.
01439    THIS_FRAME is the current frame in the current list of frames.
01440    BASE contains to stack frame base of the current frame.
01441    SAVE_REGS is the register file stored in the frame cache.  */
01442 static void
01443 hppa_hpux_unwind_adjust_stub (struct frame_info *this_frame, CORE_ADDR base,
01444                               struct trad_frame_saved_reg *saved_regs)
01445 {
01446   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01447   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01448   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
01449   struct value *pcoq_head_val;
01450   ULONGEST pcoq_head;
01451   CORE_ADDR stubpc;
01452   struct unwind_table_entry *u;
01453 
01454   pcoq_head_val = trad_frame_get_prev_register (this_frame, saved_regs, 
01455                                                 HPPA_PCOQ_HEAD_REGNUM);
01456   pcoq_head =
01457     extract_unsigned_integer (value_contents_all (pcoq_head_val),
01458                               register_size (gdbarch, HPPA_PCOQ_HEAD_REGNUM),
01459                               byte_order);
01460 
01461   u = find_unwind_entry (pcoq_head);
01462   if (u && u->stub_unwind.stub_type == EXPORT)
01463     {
01464       stubpc = read_memory_integer (base - 24, word_size, byte_order);
01465       trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
01466     }
01467   else if (hppa_symbol_address ("__gcc_plt_call") 
01468            == get_pc_function_start (pcoq_head))
01469     {
01470       stubpc = read_memory_integer (base - 8, word_size, byte_order);
01471       trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
01472     }
01473 }
01474 
01475 static void
01476 hppa_hpux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
01477 {
01478   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01479 
01480   if (IS_32BIT_TARGET (gdbarch))
01481     tdep->in_solib_call_trampoline = hppa32_hpux_in_solib_call_trampoline;
01482   else
01483     tdep->in_solib_call_trampoline = hppa64_hpux_in_solib_call_trampoline;
01484 
01485   tdep->unwind_adjust_stub = hppa_hpux_unwind_adjust_stub;
01486 
01487   set_gdbarch_in_solib_return_trampoline
01488     (gdbarch, hppa_hpux_in_solib_return_trampoline);
01489   set_gdbarch_skip_trampoline_code (gdbarch, hppa_hpux_skip_trampoline_code);
01490 
01491   set_gdbarch_push_dummy_code (gdbarch, hppa_hpux_push_dummy_code);
01492   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
01493 
01494   set_gdbarch_read_pc (gdbarch, hppa_hpux_read_pc);
01495   set_gdbarch_write_pc (gdbarch, hppa_hpux_write_pc);
01496   set_gdbarch_unwind_pc (gdbarch, hppa_hpux_unwind_pc);
01497   set_gdbarch_skip_permanent_breakpoint
01498     (gdbarch, hppa_skip_permanent_breakpoint);
01499 
01500   set_gdbarch_regset_from_core_section
01501     (gdbarch, hppa_hpux_regset_from_core_section);
01502 
01503   frame_unwind_append_unwinder (gdbarch, &hppa_hpux_sigtramp_frame_unwind);
01504 }
01505 
01506 static void
01507 hppa_hpux_som_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
01508 {
01509   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01510 
01511   tdep->is_elf = 0;
01512 
01513   tdep->find_global_pointer = hppa32_hpux_find_global_pointer;
01514 
01515   hppa_hpux_init_abi (info, gdbarch);
01516   som_solib_select (gdbarch);
01517 }
01518 
01519 static void
01520 hppa_hpux_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
01521 {
01522   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01523 
01524   tdep->is_elf = 1;
01525   tdep->find_global_pointer = hppa64_hpux_find_global_pointer;
01526 
01527   hppa_hpux_init_abi (info, gdbarch);
01528   pa64_solib_select (gdbarch);
01529 }
01530 
01531 static enum gdb_osabi
01532 hppa_hpux_core_osabi_sniffer (bfd *abfd)
01533 {
01534   if (strcmp (bfd_get_target (abfd), "hpux-core") == 0)
01535     return GDB_OSABI_HPUX_SOM;
01536   else if (strcmp (bfd_get_target (abfd), "elf64-hppa") == 0)
01537     {
01538       asection *section;
01539       
01540       section = bfd_get_section_by_name (abfd, ".kernel");
01541       if (section)
01542         {
01543           bfd_size_type size;
01544           char *contents;
01545 
01546           size = bfd_section_size (abfd, section);
01547           contents = alloca (size);
01548           if (bfd_get_section_contents (abfd, section, contents, 
01549                                         (file_ptr) 0, size)
01550               && strcmp (contents, "HP-UX") == 0)
01551             return GDB_OSABI_HPUX_ELF;
01552         }
01553     }
01554 
01555   return GDB_OSABI_UNKNOWN;
01556 }
01557 
01558 void
01559 _initialize_hppa_hpux_tdep (void)
01560 {
01561   /* BFD doesn't set a flavour for HP-UX style core files.  It doesn't
01562      set the architecture either.  */
01563   gdbarch_register_osabi_sniffer (bfd_arch_unknown,
01564                                   bfd_target_unknown_flavour,
01565                                   hppa_hpux_core_osabi_sniffer);
01566   gdbarch_register_osabi_sniffer (bfd_arch_hppa,
01567                                   bfd_target_elf_flavour,
01568                                   hppa_hpux_core_osabi_sniffer);
01569 
01570   gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_HPUX_SOM,
01571                           hppa_hpux_som_init_abi);
01572   gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w, GDB_OSABI_HPUX_ELF,
01573                           hppa_hpux_elf_init_abi);
01574 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines