lynx-core.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* BFD back end for Lynx core files
  2. Copyright (C) 1993-2022 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. bfd_cleanup
  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. size_t 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. /* Save thread contexts */
  101. if (bfd_seek (abfd, pagesize, SEEK_SET) != 0)
  102. goto fail;
  103. threadp = (core_st_t *) _bfd_alloc_and_read (abfd, tcontext_size,
  104. tcontext_size);
  105. if (!threadp)
  106. goto fail;
  107. core_signal (abfd) = threadp->currsig;
  108. newsect = make_bfd_asection (abfd, ".stack",
  109. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  110. pss.ssize,
  111. pss.slimit,
  112. pagesize + tcontext_size);
  113. if (!newsect)
  114. goto fail;
  115. newsect = make_bfd_asection (abfd, ".data",
  116. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  117. pss.data_len + pss.bss_len,
  118. pss.data_start,
  119. pagesize + tcontext_size + pss.ssize
  120. #if defined (SPARC) || defined (__SPARC__)
  121. /* SPARC Lynx seems to start dumping
  122. the .data section at a page
  123. boundary. It's OK to check a
  124. #define like SPARC here because this
  125. file can only be compiled on a Lynx
  126. host. */
  127. + pss.data_start % pagesize
  128. #endif
  129. );
  130. if (!newsect)
  131. goto fail;
  132. /* And, now for the .reg/XXX pseudo sections. Each thread has it's own
  133. .reg/XXX section, where XXX is the thread id (without leading zeros). The
  134. currently running thread (at the time of the core dump) also has an alias
  135. called `.reg' (just to keep GDB happy). Note that we use `.reg/XXX' as
  136. opposed to `.regXXX' because GDB expects that .reg2 will be the floating-
  137. point registers. */
  138. newsect = make_bfd_asection (abfd, ".reg",
  139. SEC_HAS_CONTENTS,
  140. sizeof (core_st_t),
  141. 0,
  142. pagesize);
  143. if (!newsect)
  144. goto fail;
  145. for (secnum = 0; secnum < pss.threadcnt; secnum++)
  146. {
  147. char secname[100];
  148. sprintf (secname, ".reg/%d", BUILDPID (0, threadp[secnum].tid));
  149. newsect = make_bfd_asection (abfd, secname,
  150. SEC_HAS_CONTENTS,
  151. sizeof (core_st_t),
  152. 0,
  153. pagesize + secnum * sizeof (core_st_t));
  154. if (!newsect)
  155. goto fail;
  156. }
  157. return _bfd_no_cleanup;
  158. fail:
  159. bfd_release (abfd, core_hdr (abfd));
  160. core_hdr (abfd) = NULL;
  161. bfd_section_list_clear (abfd);
  162. return NULL;
  163. }
  164. char *
  165. lynx_core_file_failing_command (bfd *abfd)
  166. {
  167. return core_command (abfd);
  168. }
  169. int
  170. lynx_core_file_failing_signal (bfd *abfd)
  171. {
  172. return core_signal (abfd);
  173. }
  174. #endif /* LYNX_CORE */