GDB (API)
/home/stan/gdb/src/gdb/hppa-linux-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for GNU/Linux running on PA-RISC, for GDB.
00002 
00003    Copyright (C) 2004-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 "gdbcore.h"
00022 #include "osabi.h"
00023 #include "target.h"
00024 #include "objfiles.h"
00025 #include "solib-svr4.h"
00026 #include "glibc-tdep.h"
00027 #include "frame-unwind.h"
00028 #include "trad-frame.h"
00029 #include "dwarf2-frame.h"
00030 #include "value.h"
00031 #include "regset.h"
00032 #include "regcache.h"
00033 #include "hppa-tdep.h"
00034 #include "linux-tdep.h"
00035 #include "elf/common.h"
00036 
00037 /* Map DWARF DBX register numbers to GDB register numbers.  */
00038 static int
00039 hppa_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
00040 {
00041   /* The general registers and the sar are the same in both sets.  */
00042   if (reg <= 32)
00043     return reg;
00044 
00045   /* fr4-fr31 (left and right halves) are mapped from 72.  */
00046   if (reg >= 72 && reg <= 72 + 28 * 2)
00047     return HPPA_FP4_REGNUM + (reg - 72);
00048 
00049   warning (_("Unmapped DWARF DBX Register #%d encountered."), reg);
00050   return -1;
00051 }
00052 
00053 static void
00054 hppa_linux_target_write_pc (struct regcache *regcache, CORE_ADDR v)
00055 {
00056   /* Probably this should be done by the kernel, but it isn't.  */
00057   regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, v | 0x3);
00058   regcache_cooked_write_unsigned (regcache,
00059                                   HPPA_PCOQ_TAIL_REGNUM, (v + 4) | 0x3);
00060 }
00061 
00062 /* An instruction to match.  */
00063 struct insn_pattern
00064 {
00065   unsigned int data;            /* See if it matches this....  */
00066   unsigned int mask;            /* ... with this mask.  */
00067 };
00068 
00069 static struct insn_pattern hppa_sigtramp[] = {
00070   /* ldi 0, %r25 or ldi 1, %r25 */
00071   { 0x34190000, 0xfffffffd },
00072   /* ldi __NR_rt_sigreturn, %r20 */
00073   { 0x3414015a, 0xffffffff },
00074   /* be,l 0x100(%sr2, %r0), %sr0, %r31 */
00075   { 0xe4008200, 0xffffffff },
00076   /* nop */
00077   { 0x08000240, 0xffffffff },
00078   { 0, 0 }
00079 };
00080 
00081 #define HPPA_MAX_INSN_PATTERN_LEN (4)
00082 
00083 /* Return non-zero if the instructions at PC match the series
00084    described in PATTERN, or zero otherwise.  PATTERN is an array of
00085    'struct insn_pattern' objects, terminated by an entry whose mask is
00086    zero.
00087 
00088    When the match is successful, fill INSN[i] with what PATTERN[i]
00089    matched.  */
00090 static int
00091 insns_match_pattern (struct gdbarch *gdbarch, CORE_ADDR pc,
00092                      struct insn_pattern *pattern,
00093                      unsigned int *insn)
00094 {
00095   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00096   int i;
00097   CORE_ADDR npc = pc;
00098 
00099   for (i = 0; pattern[i].mask; i++)
00100     {
00101       gdb_byte buf[4];
00102 
00103       target_read_memory (npc, buf, 4);
00104       insn[i] = extract_unsigned_integer (buf, 4, byte_order);
00105       if ((insn[i] & pattern[i].mask) == pattern[i].data)
00106         npc += 4;
00107       else
00108         return 0;
00109     }
00110   return 1;
00111 }
00112 
00113 /* Signal frames.  */
00114 
00115 /* (This is derived from MD_FALLBACK_FRAME_STATE_FOR in gcc.)
00116  
00117    Unfortunately, because of various bugs and changes to the kernel,
00118    we have several cases to deal with.
00119 
00120    In 2.4, the signal trampoline is 4 bytes, and pc should point directly at 
00121    the beginning of the trampoline and struct rt_sigframe.
00122 
00123    In <= 2.6.5-rc2-pa3, the signal trampoline is 9 bytes, and pc points at
00124    the 4th word in the trampoline structure.  This is wrong, it should point 
00125    at the 5th word.  This is fixed in 2.6.5-rc2-pa4.
00126 
00127    To detect these cases, we first take pc, align it to 64-bytes
00128    to get the beginning of the signal frame, and then check offsets 0, 4
00129    and 5 to see if we found the beginning of the trampoline.  This will
00130    tell us how to locate the sigcontext structure.
00131 
00132    Note that with a 2.4 64-bit kernel, the signal context is not properly
00133    passed back to userspace so the unwind will not work correctly.  */
00134 static CORE_ADDR
00135 hppa_linux_sigtramp_find_sigcontext (struct gdbarch *gdbarch, CORE_ADDR pc)
00136 {
00137   unsigned int dummy[HPPA_MAX_INSN_PATTERN_LEN];
00138   int offs = 0;
00139   int try;
00140   /* offsets to try to find the trampoline */
00141   static int pcoffs[] = { 0, 4*4, 5*4 };
00142   /* offsets to the rt_sigframe structure */
00143   static int sfoffs[] = { 4*4, 10*4, 10*4 };
00144   CORE_ADDR sp;
00145 
00146   /* Most of the time, this will be correct.  The one case when this will
00147      fail is if the user defined an alternate stack, in which case the
00148      beginning of the stack will not be align_down (pc, 64).  */
00149   sp = align_down (pc, 64);
00150 
00151   /* rt_sigreturn trampoline:
00152      3419000x ldi 0, %r25 or ldi 1, %r25   (x = 0 or 2)
00153      3414015a ldi __NR_rt_sigreturn, %r20 
00154      e4008200 be,l 0x100(%sr2, %r0), %sr0, %r31
00155      08000240 nop  */
00156 
00157   for (try = 0; try < ARRAY_SIZE (pcoffs); try++)
00158     {
00159       if (insns_match_pattern (gdbarch, sp + pcoffs[try],
00160                                hppa_sigtramp, dummy))
00161         {
00162           offs = sfoffs[try];
00163           break;
00164         }
00165     }
00166 
00167   if (offs == 0)
00168     {
00169       if (insns_match_pattern (gdbarch, pc, hppa_sigtramp, dummy))
00170         {
00171           /* sigaltstack case: we have no way of knowing which offset to 
00172              use in this case; default to new kernel handling.  If this is
00173              wrong the unwinding will fail.  */
00174           try = 2;
00175           sp = pc - pcoffs[try];
00176         }
00177       else
00178       {
00179         return 0;
00180       }
00181     }
00182 
00183   /* sp + sfoffs[try] points to a struct rt_sigframe, which contains
00184      a struct siginfo and a struct ucontext.  struct ucontext contains
00185      a struct sigcontext.  Return an offset to this sigcontext here.  Too 
00186      bad we cannot include system specific headers :-(.
00187      sizeof(struct siginfo) == 128
00188      offsetof(struct ucontext, uc_mcontext) == 24.  */
00189   return sp + sfoffs[try] + 128 + 24;
00190 }
00191 
00192 struct hppa_linux_sigtramp_unwind_cache
00193 {
00194   CORE_ADDR base;
00195   struct trad_frame_saved_reg *saved_regs;
00196 };
00197 
00198 static struct hppa_linux_sigtramp_unwind_cache *
00199 hppa_linux_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
00200                                         void **this_cache)
00201 {
00202   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00203   struct hppa_linux_sigtramp_unwind_cache *info;
00204   CORE_ADDR pc, scptr;
00205   int i;
00206 
00207   if (*this_cache)
00208     return *this_cache;
00209 
00210   info = FRAME_OBSTACK_ZALLOC (struct hppa_linux_sigtramp_unwind_cache);
00211   *this_cache = info;
00212   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00213 
00214   pc = get_frame_pc (this_frame);
00215   scptr = hppa_linux_sigtramp_find_sigcontext (gdbarch, pc);
00216 
00217   /* structure of struct sigcontext:
00218    
00219      struct sigcontext {
00220         unsigned long sc_flags;
00221         unsigned long sc_gr[32]; 
00222         unsigned long long sc_fr[32];
00223         unsigned long sc_iasq[2];
00224         unsigned long sc_iaoq[2];
00225         unsigned long sc_sar;           */
00226 
00227   /* Skip sc_flags.  */
00228   scptr += 4;
00229 
00230   /* GR[0] is the psw.  */
00231   info->saved_regs[HPPA_IPSW_REGNUM].addr = scptr;
00232   scptr += 4;
00233 
00234   /* General registers.  */
00235   for (i = 1; i < 32; i++)
00236     {
00237       info->saved_regs[HPPA_R0_REGNUM + i].addr = scptr;
00238       scptr += 4;
00239     }
00240 
00241   /* Pad to long long boundary.  */
00242   scptr += 4;
00243 
00244   /* FP regs; FP0-3 are not restored.  */
00245   scptr += (8 * 4);
00246 
00247   for (i = 4; i < 32; i++)
00248     {
00249       info->saved_regs[HPPA_FP0_REGNUM + (i * 2)].addr = scptr;
00250       scptr += 4;
00251       info->saved_regs[HPPA_FP0_REGNUM + (i * 2) + 1].addr = scptr;
00252       scptr += 4;
00253     }
00254 
00255   /* IASQ/IAOQ.  */
00256   info->saved_regs[HPPA_PCSQ_HEAD_REGNUM].addr = scptr;
00257   scptr += 4;
00258   info->saved_regs[HPPA_PCSQ_TAIL_REGNUM].addr = scptr;
00259   scptr += 4;
00260 
00261   info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].addr = scptr;
00262   scptr += 4;
00263   info->saved_regs[HPPA_PCOQ_TAIL_REGNUM].addr = scptr;
00264   scptr += 4;
00265 
00266   info->saved_regs[HPPA_SAR_REGNUM].addr = scptr;
00267 
00268   info->base = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);
00269 
00270   return info;
00271 }
00272 
00273 static void
00274 hppa_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
00275                                    void **this_prologue_cache,
00276                                    struct frame_id *this_id)
00277 {
00278   struct hppa_linux_sigtramp_unwind_cache *info
00279     = hppa_linux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
00280   *this_id = frame_id_build (info->base, get_frame_pc (this_frame));
00281 }
00282 
00283 static struct value *
00284 hppa_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
00285                                          void **this_prologue_cache,
00286                                          int regnum)
00287 {
00288   struct hppa_linux_sigtramp_unwind_cache *info
00289     = hppa_linux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
00290   return hppa_frame_prev_register_helper (this_frame,
00291                                           info->saved_regs, regnum);
00292 }
00293 
00294 /* hppa-linux always uses "new-style" rt-signals.  The signal handler's return
00295    address should point to a signal trampoline on the stack.  The signal
00296    trampoline is embedded in a rt_sigframe structure that is aligned on
00297    the stack.  We take advantage of the fact that sp must be 64-byte aligned,
00298    and the trampoline is small, so by rounding down the trampoline address
00299    we can find the beginning of the struct rt_sigframe.  */
00300 static int
00301 hppa_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
00302                                    struct frame_info *this_frame,
00303                                    void **this_prologue_cache)
00304 {
00305   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00306   CORE_ADDR pc = get_frame_pc (this_frame);
00307 
00308   if (hppa_linux_sigtramp_find_sigcontext (gdbarch, pc))
00309     return 1;
00310 
00311   return 0;
00312 }
00313 
00314 static const struct frame_unwind hppa_linux_sigtramp_frame_unwind = {
00315   SIGTRAMP_FRAME,
00316   default_frame_unwind_stop_reason,
00317   hppa_linux_sigtramp_frame_this_id,
00318   hppa_linux_sigtramp_frame_prev_register,
00319   NULL,
00320   hppa_linux_sigtramp_frame_sniffer
00321 };
00322 
00323 /* Attempt to find (and return) the global pointer for the given
00324    function.
00325 
00326    This is a rather nasty bit of code searchs for the .dynamic section
00327    in the objfile corresponding to the pc of the function we're trying
00328    to call.  Once it finds the addresses at which the .dynamic section
00329    lives in the child process, it scans the Elf32_Dyn entries for a
00330    DT_PLTGOT tag.  If it finds one of these, the corresponding
00331    d_un.d_ptr value is the global pointer.  */
00332 
00333 static CORE_ADDR
00334 hppa_linux_find_global_pointer (struct gdbarch *gdbarch,
00335                                 struct value *function)
00336 {
00337   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00338   struct obj_section *faddr_sect;
00339   CORE_ADDR faddr;
00340   
00341   faddr = value_as_address (function);
00342 
00343   /* Is this a plabel? If so, dereference it to get the gp value.  */
00344   if (faddr & 2)
00345     {
00346       int status;
00347       gdb_byte buf[4];
00348 
00349       faddr &= ~3;
00350 
00351       status = target_read_memory (faddr + 4, buf, sizeof (buf));
00352       if (status == 0)
00353         return extract_unsigned_integer (buf, sizeof (buf), byte_order);
00354     }
00355 
00356   /* If the address is in the plt section, then the real function hasn't 
00357      yet been fixed up by the linker so we cannot determine the gp of 
00358      that function.  */
00359   if (in_plt_section (faddr))
00360     return 0;
00361 
00362   faddr_sect = find_pc_section (faddr);
00363   if (faddr_sect != NULL)
00364     {
00365       struct obj_section *osect;
00366 
00367       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
00368         {
00369           if (strcmp (osect->the_bfd_section->name, ".dynamic") == 0)
00370             break;
00371         }
00372 
00373       if (osect < faddr_sect->objfile->sections_end)
00374         {
00375           CORE_ADDR addr, endaddr;
00376 
00377           addr = obj_section_addr (osect);
00378           endaddr = obj_section_endaddr (osect);
00379 
00380           while (addr < endaddr)
00381             {
00382               int status;
00383               LONGEST tag;
00384               gdb_byte buf[4];
00385 
00386               status = target_read_memory (addr, buf, sizeof (buf));
00387               if (status != 0)
00388                 break;
00389               tag = extract_signed_integer (buf, sizeof (buf), byte_order);
00390 
00391               if (tag == DT_PLTGOT)
00392                 {
00393                   CORE_ADDR global_pointer;
00394 
00395                   status = target_read_memory (addr + 4, buf, sizeof (buf));
00396                   if (status != 0)
00397                     break;
00398                   global_pointer = extract_unsigned_integer (buf, sizeof (buf),
00399                                                              byte_order);
00400                   /* The payoff...  */
00401                   return global_pointer;
00402                 }
00403 
00404               if (tag == DT_NULL)
00405                 break;
00406 
00407               addr += 8;
00408             }
00409         }
00410     }
00411   return 0;
00412 }
00413 
00414 /*
00415  * Registers saved in a coredump:
00416  * gr0..gr31
00417  * sr0..sr7
00418  * iaoq0..iaoq1
00419  * iasq0..iasq1
00420  * sar, iir, isr, ior, ipsw
00421  * cr0, cr24..cr31
00422  * cr8,9,12,13
00423  * cr10, cr15
00424  */
00425 
00426 #define GR_REGNUM(_n)   (HPPA_R0_REGNUM+_n)
00427 #define TR_REGNUM(_n)   (HPPA_TR0_REGNUM+_n)
00428 static const int greg_map[] =
00429   {
00430     GR_REGNUM(0), GR_REGNUM(1), GR_REGNUM(2), GR_REGNUM(3),
00431     GR_REGNUM(4), GR_REGNUM(5), GR_REGNUM(6), GR_REGNUM(7),
00432     GR_REGNUM(8), GR_REGNUM(9), GR_REGNUM(10), GR_REGNUM(11),
00433     GR_REGNUM(12), GR_REGNUM(13), GR_REGNUM(14), GR_REGNUM(15),
00434     GR_REGNUM(16), GR_REGNUM(17), GR_REGNUM(18), GR_REGNUM(19),
00435     GR_REGNUM(20), GR_REGNUM(21), GR_REGNUM(22), GR_REGNUM(23),
00436     GR_REGNUM(24), GR_REGNUM(25), GR_REGNUM(26), GR_REGNUM(27),
00437     GR_REGNUM(28), GR_REGNUM(29), GR_REGNUM(30), GR_REGNUM(31),
00438 
00439     HPPA_SR4_REGNUM+1, HPPA_SR4_REGNUM+2, HPPA_SR4_REGNUM+3, HPPA_SR4_REGNUM+4,
00440     HPPA_SR4_REGNUM, HPPA_SR4_REGNUM+5, HPPA_SR4_REGNUM+6, HPPA_SR4_REGNUM+7,
00441 
00442     HPPA_PCOQ_HEAD_REGNUM, HPPA_PCOQ_TAIL_REGNUM,
00443     HPPA_PCSQ_HEAD_REGNUM, HPPA_PCSQ_TAIL_REGNUM,
00444 
00445     HPPA_SAR_REGNUM, HPPA_IIR_REGNUM, HPPA_ISR_REGNUM, HPPA_IOR_REGNUM,
00446     HPPA_IPSW_REGNUM, HPPA_RCR_REGNUM,
00447 
00448     TR_REGNUM(0), TR_REGNUM(1), TR_REGNUM(2), TR_REGNUM(3),
00449     TR_REGNUM(4), TR_REGNUM(5), TR_REGNUM(6), TR_REGNUM(7),
00450 
00451     HPPA_PID0_REGNUM, HPPA_PID1_REGNUM, HPPA_PID2_REGNUM, HPPA_PID3_REGNUM,
00452     HPPA_CCR_REGNUM, HPPA_EIEM_REGNUM,
00453   };
00454 
00455 static void
00456 hppa_linux_supply_regset (const struct regset *regset,
00457                           struct regcache *regcache,
00458                           int regnum, const void *regs, size_t len)
00459 {
00460   struct gdbarch *arch = get_regcache_arch (regcache);
00461   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
00462   const char *buf = regs;
00463   int i, offset;
00464 
00465   offset = 0;
00466   for (i = 0; i < ARRAY_SIZE (greg_map); i++)
00467     {
00468       if (regnum == greg_map[i] || regnum == -1)
00469         regcache_raw_supply (regcache, greg_map[i], buf + offset);
00470 
00471       offset += tdep->bytes_per_address;
00472     }
00473 }
00474 
00475 static void
00476 hppa_linux_supply_fpregset (const struct regset *regset,
00477                             struct regcache *regcache,
00478                             int regnum, const void *regs, size_t len)
00479 {
00480   const char *buf = regs;
00481   int i, offset;
00482 
00483   offset = 0;
00484   for (i = 0; i < 64; i++)
00485     {
00486       if (regnum == HPPA_FP0_REGNUM + i || regnum == -1)
00487         regcache_raw_supply (regcache, HPPA_FP0_REGNUM + i, 
00488                              buf + offset);
00489       offset += 4;
00490     }
00491 }
00492 
00493 /* HPPA Linux kernel register set.  */
00494 static struct regset hppa_linux_regset =
00495 {
00496   NULL,
00497   hppa_linux_supply_regset
00498 };
00499 
00500 static struct regset hppa_linux_fpregset =
00501 {
00502   NULL,
00503   hppa_linux_supply_fpregset
00504 };
00505 
00506 static const struct regset *
00507 hppa_linux_regset_from_core_section (struct gdbarch *gdbarch,
00508                                      const char *sect_name,
00509                                      size_t sect_size)
00510 {
00511   if (strcmp (sect_name, ".reg") == 0)
00512     return &hppa_linux_regset;
00513   else if (strcmp (sect_name, ".reg2") == 0)
00514     return &hppa_linux_fpregset;
00515 
00516   return NULL;
00517 }
00518 
00519 
00520 /* Forward declarations.  */
00521 extern initialize_file_ftype _initialize_hppa_linux_tdep;
00522 
00523 static void
00524 hppa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
00525 {
00526   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00527 
00528   linux_init_abi (info, gdbarch);
00529 
00530   /* GNU/Linux is always ELF.  */
00531   tdep->is_elf = 1;
00532 
00533   tdep->find_global_pointer = hppa_linux_find_global_pointer;
00534 
00535   set_gdbarch_write_pc (gdbarch, hppa_linux_target_write_pc);
00536 
00537   frame_unwind_append_unwinder (gdbarch, &hppa_linux_sigtramp_frame_unwind);
00538 
00539   /* GNU/Linux uses SVR4-style shared libraries.  */
00540   set_solib_svr4_fetch_link_map_offsets
00541     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
00542 
00543   tdep->in_solib_call_trampoline = hppa_in_solib_call_trampoline;
00544   set_gdbarch_skip_trampoline_code (gdbarch, hppa_skip_trampoline_code);
00545 
00546   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
00547   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
00548 
00549   /* On hppa-linux, currently, sizeof(long double) == 8.  There has been
00550      some discussions to support 128-bit long double, but it requires some
00551      more work in gcc and glibc first.  */
00552   set_gdbarch_long_double_bit (gdbarch, 64);
00553 
00554   set_gdbarch_regset_from_core_section
00555     (gdbarch, hppa_linux_regset_from_core_section);
00556 
00557   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, hppa_dwarf_reg_to_regnum);
00558 
00559   /* Enable TLS support.  */
00560   set_gdbarch_fetch_tls_load_module_address (gdbarch,
00561                                              svr4_fetch_objfile_link_map);
00562 }
00563 
00564 void
00565 _initialize_hppa_linux_tdep (void)
00566 {
00567   gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_LINUX,
00568                           hppa_linux_init_abi);
00569   gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w,
00570                           GDB_OSABI_LINUX, hppa_linux_init_abi);
00571 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines