lynx-core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* BFD back end for Lynx core files
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Written by Stu Grossman of Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #ifdef LYNX_CORE
  21. #include <sys/conf.h>
  22. #include <sys/kernel.h>
  23. /* sys/kernel.h should define this, but doesn't always, sigh. */
  24. #ifndef __LYNXOS
  25. #define __LYNXOS
  26. #endif
  27. #include <sys/mem.h>
  28. #include <sys/signal.h>
  29. #include <sys/time.h>
  30. #include <sys/resource.h>
  31. #include <sys/itimer.h>
  32. #include <sys/file.h>
  33. #include <sys/proc.h>
  34. /* These are stored in the bfd's tdata */
  35. struct lynx_core_struct
  36. {
  37. int sig;
  38. char cmd[PNMLEN + 1];
  39. };
  40. #define core_hdr(bfd) ((bfd)->tdata.lynx_core_data)
  41. #define core_signal(bfd) (core_hdr(bfd)->sig)
  42. #define core_command(bfd) (core_hdr(bfd)->cmd)
  43. #define lynx_core_file_matches_executable_p generic_core_file_matches_executable_p
  44. #define lynx_core_file_pid _bfd_nocore_core_file_pid
  45. /* Handle Lynx core dump file. */
  46. static asection *
  47. make_bfd_asection (bfd *abfd,
  48. const char *name,
  49. flagword flags,
  50. bfd_size_type size,
  51. bfd_vma vma,
  52. file_ptr filepos)
  53. {
  54. asection *asect;
  55. char *newname;
  56. newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1);
  57. if (!newname)
  58. return NULL;
  59. strcpy (newname, name);
  60. asect = bfd_make_section_with_flags (abfd, newname, flags);
  61. if (!asect)
  62. return NULL;
  63. asect->size = size;
  64. asect->vma = vma;
  65. asect->filepos = filepos;
  66. asect->alignment_power = 2;
  67. return asect;
  68. }
  69. const bfd_target *
  70. lynx_core_file_p (bfd *abfd)
  71. {
  72. int secnum;
  73. struct pssentry pss;
  74. bfd_size_type tcontext_size;
  75. core_st_t *threadp;
  76. int pagesize;
  77. asection *newsect;
  78. bfd_size_type amt;
  79. pagesize = getpagesize (); /* Serious cross-target issue here... This
  80. really needs to come from a system-specific
  81. header file. */
  82. /* Get the pss entry from the core file */
  83. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  84. return NULL;
  85. amt = sizeof pss;
  86. if (bfd_bread ((void *) &pss, amt, abfd) != amt)
  87. {
  88. /* Too small to be a core file */
  89. if (bfd_get_error () != bfd_error_system_call)
  90. bfd_set_error (bfd_error_wrong_format);
  91. return NULL;
  92. }
  93. amt = sizeof (struct lynx_core_struct);
  94. core_hdr (abfd) = (struct lynx_core_struct *) bfd_zalloc (abfd, amt);
  95. if (!core_hdr (abfd))
  96. return NULL;
  97. strncpy (core_command (abfd), pss.pname, PNMLEN + 1);
  98. /* Compute the size of the thread contexts */
  99. tcontext_size = pss.threadcnt * sizeof (core_st_t);
  100. /* Allocate space for the thread contexts */
  101. threadp = (core_st_t *) bfd_alloc (abfd, tcontext_size);
  102. if (!threadp)
  103. goto fail;
  104. /* Save thread contexts */
  105. if (bfd_seek (abfd, (file_ptr) pagesize, SEEK_SET) != 0)
  106. goto fail;
  107. if (bfd_bread ((void *) threadp, tcontext_size, abfd) != tcontext_size)
  108. {
  109. /* Probably too small to be a core file */
  110. if (bfd_get_error () != bfd_error_system_call)
  111. bfd_set_error (bfd_error_wrong_format);
  112. goto fail;
  113. }
  114. core_signal (abfd) = threadp->currsig;
  115. newsect = make_bfd_asection (abfd, ".stack",
  116. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  117. pss.ssize,
  118. pss.slimit,
  119. pagesize + tcontext_size);
  120. if (!newsect)
  121. goto fail;
  122. newsect = make_bfd_asection (abfd, ".data",
  123. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  124. pss.data_len + pss.bss_len,
  125. pss.data_start,
  126. pagesize + tcontext_size + pss.ssize
  127. #if defined (SPARC) || defined (__SPARC__)
  128. /* SPARC Lynx seems to start dumping
  129. the .data section at a page
  130. boundary. It's OK to check a
  131. #define like SPARC here because this
  132. file can only be compiled on a Lynx
  133. host. */
  134. + pss.data_start % pagesize
  135. #endif
  136. );
  137. if (!newsect)
  138. goto fail;
  139. /* And, now for the .reg/XXX pseudo sections. Each thread has it's own
  140. .reg/XXX section, where XXX is the thread id (without leading zeros). The
  141. currently running thread (at the time of the core dump) also has an alias
  142. called `.reg' (just to keep GDB happy). Note that we use `.reg/XXX' as
  143. opposed to `.regXXX' because GDB expects that .reg2 will be the floating-
  144. point registers. */
  145. newsect = make_bfd_asection (abfd, ".reg",
  146. SEC_HAS_CONTENTS,
  147. sizeof (core_st_t),
  148. 0,
  149. pagesize);
  150. if (!newsect)
  151. goto fail;
  152. for (secnum = 0; secnum < pss.threadcnt; secnum++)
  153. {
  154. char secname[100];
  155. sprintf (secname, ".reg/%d", BUILDPID (0, threadp[secnum].tid));
  156. newsect = make_bfd_asection (abfd, secname,
  157. SEC_HAS_CONTENTS,
  158. sizeof (core_st_t),
  159. 0,
  160. pagesize + secnum * sizeof (core_st_t));
  161. if (!newsect)
  162. goto fail;
  163. }
  164. return abfd->xvec;
  165. fail:
  166. bfd_release (abfd, core_hdr (abfd));
  167. core_hdr (abfd) = NULL;
  168. bfd_section_list_clear (abfd);
  169. return NULL;
  170. }
  171. char *
  172. lynx_core_file_failing_command (bfd *abfd)
  173. {
  174. return core_command (abfd);
  175. }
  176. int
  177. lynx_core_file_failing_signal (bfd *abfd)
  178. {
  179. return core_signal (abfd);
  180. }
  181. #endif /* LYNX_CORE */