File: evax-solib.c

package info (click to toggle)
gnat-gdb 5.3.gnat.0.0.20030225-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 75,144 kB
  • ctags: 101,348
  • sloc: ansic: 873,511; exp: 46,950; sh: 16,123; makefile: 11,757; yacc: 6,092; asm: 5,027; cpp: 4,044; perl: 2,624; lex: 877; sed: 550; lisp: 394; awk: 170; pascal: 57; java: 7; fortran: 5
file content (988 lines) | stat: -rw-r--r-- 24,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#include "defs.h"

#include <fcntl.h>

#include <imcbdef.h>
#include <kferesdef.h>
#include "bfd.h"
#include "inferior.h"
#include "environ.h"
#include "command.h"
#include "symfile.h"
#include "gdbcmd.h"
#include "objfiles.h"
#include "evax-solib.h"

#define MAX_PATH_SIZE 512

#define IMCB_ADDR(so) ((so) -> imcb.imcb$l_starting_address)
#define IMCB_NEXT(so) ((so) -> imcb.imcb$l_flink)
#define IMCB_NAME(so) ((so) -> imcb.imcb$t_log_image_name)
/* Test for first imcb entry; first entry is the exec-file. */
#define IGNORE_FIRST_IMCB_ENTRY(x) ((x).imcb$l_blink == imglstptr)

static IMCB first_imcb;

struct so_list {
  struct so_list *next;	              /* next structure in linked list */
  IMCB imcb;                          /* copy of imcb from inferior */
  IMCB *imcb_ptr;                     /* addr in inferior imcb was read from */
  CORE_ADDR imcbend;                  /* upper addr bound of mapped object */
  KFERES *kferes;
  KFERES_SECTION **kferes_sections;
  char so_name[MAX_PATH_SIZE];        /* shared object lib name (FIXME) */
  char symbols_loaded;                /* flag: symbols read in yet? */
  char from_tty;                      /* flag: print msgs? */
  struct objfile *objfile;            /* objfile for loaded lib */
  struct section_table *sections;
  struct section_table *sections_end;
  struct section_table *textsection;
  bfd *abfd;
};

static IMCB *imglstptr = 0;

static struct so_list *so_list_head = 0; /* List of known shared objects */

static struct so_list *find_solib PARAMS ((struct so_list *));

static IMCB *first_imcb_member PARAMS ((void));

static void info_sharedimage_command PARAMS ((char *, int));

static void sharedimage_command PARAMS ((char *, int));

static void solib_map_sections PARAMS ((struct so_list *));

static void special_symbol_handling PARAMS ((struct so_list *));

static int symbol_add_stub PARAMS ((char *));


/*

LOCAL FUNCTION

	find_solib -- step through list of shared objects

SYNOPSIS

	struct so_list *find_solib (struct so_list *so_list_ptr)

DESCRIPTION

	This module contains the routine which finds the names of any
	loaded "images" in the current process. The argument in must be
	NULL on the first call, and then the returned value must be passed
	in on subsequent calls. This provides the capability to "step" down
	the list of loaded objects. On the last object, a NULL value is
	returned.

 */

static struct so_list *
find_solib (so_list_ptr)
     struct so_list *so_list_ptr;	/* Last imcb or NULL for first one */
{
  struct so_list *so_list_next = NULL;
  IMCB *imcbptr = NULL;
  IMCB imcb;
  struct so_list *new_so;

  if (so_list_ptr == NULL)
    {
      /* We are setting up for a new scan through the loaded images. */
      if ((so_list_next = so_list_head) == NULL)
	{
	  /* Imglstptr may be null if we just attached to a running program. */
	  if (!imglstptr)
	    evax_solib_create_inferior_hook ();
	  imcbptr = first_imcb_member ();
	}
    }
  else
    {
      /* We have been called before, and are in the process of walking
	 the shared library list.  Advance to the next shared object. */
      if ((imcbptr = IMCB_NEXT (so_list_ptr)) == imglstptr)
	{
	  /* We have hit the end of the list, so check to see if any were
	     added. */
	  read_memory ((CORE_ADDR) so_list_ptr->imcb_ptr,
		       (char *) &imcb,
		       sizeof (IMCB));
	  imcbptr = imcb.imcb$l_flink;

	  IMCB_NEXT (so_list_ptr) = imcbptr;
	  if (imcbptr == imglstptr)
	    imcbptr = NULL;
	}
      so_list_next = so_list_ptr -> next;
    }
  if ((so_list_next == NULL) && (imcbptr != NULL))
    {
      /* Get next link map structure from inferior image and build a local
	 abbreviated load_map structure */
      new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
      memset ((char *) new_so, 0, sizeof (struct so_list));
      new_so -> imcb_ptr = imcbptr;
      /* Add the new node as the next node in the list, or as the root
	 node if this is the first one. */
      if (so_list_ptr != NULL)
	{
	  so_list_ptr -> next = new_so;
	}
      else
	{
	  so_list_head = new_so;
	}      
      so_list_next = new_so;
      read_memory ((CORE_ADDR) imcbptr, (char *) &(new_so->imcb),
		   sizeof (IMCB));

      if (!IGNORE_FIRST_IMCB_ENTRY (new_so->imcb)
	  && new_so->imcb.imcb$v_discontiguous
	  && new_so->imcb.imcb$l_kferes_ptr)
	{
	  int k;
	  int count;
	  KFERES *new_k;
	  KFERES_SECTION *new_ks;
	  KFERES *kferes_ptr = new_so ->imcb.imcb$l_kferes_ptr;
	  KFERES_SECTION *kferes_section_ptr;

	  new_k = (KFERES *) xmalloc (sizeof (KFERES));
	  memset ((char *) new_k, 0, sizeof (KFERES));
	  read_memory ((CORE_ADDR) kferes_ptr, (char *)new_k, sizeof (KFERES));

	  new_so->kferes = new_k;
	  count = new_so->kferes->kferes$l_count
	    + new_so->kferes->kferes$l_data_count;

	  new_so->kferes_sections
	    = (KFERES_SECTION **) xmalloc (count * sizeof (KFERES_SECTION *));
	  
	  for (k = 0, kferes_section_ptr = (KFERES_SECTION *)(kferes_ptr + 1);
	       k < count;
	       k++, kferes_section_ptr++)
	    {
	      new_ks = (KFERES_SECTION *) xmalloc (sizeof (KFERES_SECTION));
	      memset ((char *) new_ks, 0, sizeof (KFERES_SECTION));
	      read_memory ((CORE_ADDR) kferes_section_ptr,
			   (char *)new_ks,
			   sizeof (KFERES_SECTION));

	      new_so->kferes_sections[k] = new_ks;
	      
	    }
	}
      /* The first entry in the imcb is for the
	 inferior executable, so we must ignore it.  For some versions
	 it has no name. */
      if (!IGNORE_FIRST_IMCB_ENTRY (new_so->imcb))
	{
	  if (new_so->imcb.imcb$t_log_image_name [0])
	    {
	      strncpy (new_so->so_name,
		       &new_so->imcb.imcb$t_log_image_name [1],
		       new_so->imcb.imcb$t_log_image_name [0]);
	      new_so -> so_name[new_so->imcb.imcb$t_log_image_name [0]] = '\0';
	    }
	  else	  if (new_so->so_name[0] == 0)
	    return 0;

	    {
	      strncpy (new_so->so_name,
		       &new_so->imcb.imcb$t_image_name [1],
		       new_so->imcb.imcb$t_image_name [0]);
	      new_so -> so_name[new_so->imcb.imcb$t_image_name [0]] = '\0';
	    }

#if 0
	  printf ("%s symbol vector: %llx - %llx\n",
		  new_so->so_name,
		  new_so->imcb.imcb$ps_symbol_vector_address,
		  new_so->imcb.imcb$r_fill_12.imcb$q_symbol_vector_address 
		  + new_so->imcb.imcb$l_symbol_vector_size - 1);
#endif

	  solib_map_sections (new_so);
	}      
    }
  return (so_list_next);
}

/*

LOCAL FUNCTION

	first_imcb_member -- locate first member in dynamic linker's map

SYNOPSIS

	static IMCB *first_imcb_member (void)

DESCRIPTION

	Read in a copy of the first member in the inferior's dynamic
	imcb from the inferior's dynamic linker structures, and return
	a pointer to the copy in our address space.
*/

static IMCB *
first_imcb_member ()
{
  read_memory
    ((CORE_ADDR) imglstptr, (char *)&first_imcb, sizeof (IMCB));

  return (first_imcb.imcb$l_flink);
}

/*

LOCAL FUNCTION

	info_sharedimage_command -- code for "info sharedimage"

SYNOPSIS

	static void info_sharedimage_command ()

DESCRIPTION

	Walk through the shareable image list and print information
	about each attached image.
*/

static void
info_sharedimage_command (ignore, from_tty)
     char *ignore;
     int from_tty;
{
  register struct so_list *so = NULL;
  int header_done = 0;
  
  if (!target_has_execution)
    {
      printf_unfiltered ("No execution.\n");
      return;
    }

  while ((so = find_solib (so)) != NULL)
    {
      if (so -> so_name[0])
	{
	  if (!header_done)
	    {
	      printf_unfiltered
		("   %-20s%-20s%-12s\n", "From", "To", "Symbols");
	      header_done++;
	    }
	  printf_unfiltered ("%s\n",  TO_HOST_FILE_SPEC (so -> so_name));
	  printf_unfiltered ("   %-20s",
		  local_hex_string_custom ((unsigned __int64) IMCB_ADDR (so),
					   "016ll"));
	  printf_unfiltered ("%-20s",
		  local_hex_string_custom ((unsigned __int64) so -> imcbend,
					   "016ll"));
	  printf_unfiltered
	    ("%-12s\n", so -> symbols_loaded ? "Read" : "Not read");
	}

      if (so->imcb.imcb$v_discontiguous)
	{
	  int k;
	  int count
	    = so->kferes->kferes$l_count + so->kferes->kferes$l_data_count;
	  
	  for (k = 0; k < count; k++)
	    {
	      if (k < so->kferes->kferes$l_count)
		printf_unfiltered ("   CODE: ");
	      else
		printf_unfiltered ("   DATA: ");
	      
	      printf_unfiltered
		("%-20s",
		 local_hex_string_custom
		 ((unsigned __int64) so->kferes_sections[k]->kferes$l_va,
		  "016ll"));
	      printf_unfiltered
		("%-20s\n",
		 local_hex_string_custom
		 ((unsigned __int64) so->kferes_sections[k]->kferes$l_va
		  + so->kferes_sections[k]->kferes$l_length - 1,
		  "016ll"));
	    }
	}
      printf_unfiltered ("\n");
    }
  if (so_list_head == NULL)
    {
      printf_unfiltered ("No shareable images loaded at this time.\n");	
    }
}

struct section_offsets *
evax_solib_offsets (objfile, addr, num_offsets)
     struct objfile *objfile;
     CORE_ADDR addr;
     int num_offsets;
{
  register struct so_list *so = NULL;
  int i;

  struct section_offsets *section_offsets = (struct section_offsets *)
    obstack_alloc (&objfile -> psymbol_obstack,
		   sizeof (struct section_offsets) +
		   sizeof (section_offsets->offsets) * (num_offsets-1));

  for (i = 0; i < num_offsets; i++)
    {
      ANOFFSET (section_offsets, i) = 0;
    }

  while ((so = find_solib (so)) != NULL)
    {
      if (so -> so_name[0]
	  && ((unsigned __int64) IMCB_ADDR (so) == (unsigned __int64) addr))
	{
	  if (so->imcb.imcb$v_discontiguous)
	    {
	      int k;
	      int count
		= so->kferes->kferes$l_count
		  + so->kferes->kferes$l_data_count;
	  
	      for (k = 0; k < count; k++)
		{
		  ANOFFSET (section_offsets, k)
		    = (unsigned __int64) so->kferes_sections[k]->kferes$l_va;
		}

	      if (count == 1)
		{
		  ANOFFSET (section_offsets, 1)
		    = (unsigned __int64) IMCB_ADDR (so);
		}
	    }
	  else
	    {
	      for (i = 0; i < num_offsets; i++)
		{
		  ANOFFSET (section_offsets, i)
		    = (unsigned __int64) IMCB_ADDR (so);
		}
	    }
	  return (section_offsets);
	}
    }
}

/*

LOCAL FUNCTION

	sharedimage_command -- handle command to explicitly add library

SYNOPSIS

	static void sharedimage_command (char *args, int from_tty)

DESCRIPTION

*/

static void
sharedimage_command (args, from_tty)
char *args;
int from_tty;
{
  dont_repeat ();
  evax_solib_add (args, from_tty, (struct target_ops *) 0);
}

/*

LOCAL FUNCTION

	solib_map_sections -- open bfd and build sections for shared lib

SYNOPSIS

	static void solib_map_sections (struct so_list *so)

DESCRIPTION

	Given a pointer to one of the shared objects in our list
	of mapped objects, use the recorded name to open a bfd
	descriptor for the object, build a section table, and then
	relocate all the section addresses by the base address at
	which the shared object was mapped.

FIXMES

	In most (all?) cases the shared object file name recorded in the
	dynamic linkage tables will be a fully qualified pathname.  For
	cases where it isn't, do we really mimic the systems search
	mechanism correctly in the below code (particularly the tilde
	expansion stuff?).
 */

static void
solib_map_sections (so)
     struct so_list *so;
{
  char *filename, *full_filename;
  char *scratch_pathname;
  int scratch_chan;
  struct cleanup *old_chain;
  bfd *abfd;
  
  filename = get_in_environ (inferior_environ, so -> so_name);

  if (filename == 0)
    filename = so -> so_name;
  
  full_filename = (char *)xmalloc (strlen (filename) + strlen (".EXE") + 1);
  strcpy (full_filename, filename);
  strcat (full_filename, ".EXE");

  old_chain = make_cleanup (free, full_filename);


  scratch_chan = openp ("/SYS$SHARE", 0, full_filename,
			O_RDONLY, 0, &scratch_pathname);

  if (scratch_chan < 0)
    {
      scratch_chan = openp ("/SYS$LOADABLE_IMAGES", 0, full_filename,
			    O_RDONLY, 0, &scratch_pathname);
    }

  if (scratch_chan < 0)
    {
      scratch_chan = openp ("/SYS$MESSAGE", 0, full_filename,
			    O_RDONLY, 0, &scratch_pathname);
    }

  if (scratch_chan < 0)
    {
      scratch_chan = openp ("/GNU/LIB", 0, full_filename,
			    O_RDONLY, 0, &scratch_pathname);
    }

  if (scratch_chan < 0 && getenv (filename))
    {
      scratch_chan = openp (0, 1, TO_NORMAL_FILE_SPEC (getenv (filename)),
			    O_RDONLY, 0, &scratch_pathname);
    }

  if (scratch_chan < 0)
    {
      scratch_chan = openp
	(TO_NORMAL_DIR_SPEC (get_in_environ (inferior_environ, "PATH")), 
	 1, full_filename, O_RDONLY, 0, &scratch_pathname);
    }

  if (scratch_chan < 0)
    {
      perror_with_name (full_filename);
    }

  abfd = bfd_fdopenr (scratch_pathname, 0, scratch_chan);
  if (!abfd)
    {
      close (scratch_chan);
      error ("Could not open `%s' as a shareable image file: %s",
	     TO_HOST_FILE_SPEC (scratch_pathname),
	     bfd_errmsg (bfd_get_error ()));
    }
/*  close (scratch_chan); */
  /* Leave bfd open, core_xfer_memory and "info files" need it.  */
  so -> abfd = abfd;
  abfd -> cacheable = true;

  /* copy full path name into so_name, so that later symbol_file_add can find
     it */
  if (strlen (scratch_pathname) >= MAX_PATH_SIZE)
    error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
  strcpy (so->so_name, scratch_pathname);

#if 0
  printf ("Mapping: %s\n", so->so_name);
#endif
  if (!bfd_check_format (abfd, bfd_object))
    {
      error ("\"%s\": not in executable format: %s.",
	     TO_HOST_FILE_SPEC (scratch_pathname),
	     bfd_errmsg (bfd_get_error ()));
    }

  if (build_section_table (abfd, &so -> sections, &so -> sections_end))
    {
      error ("Can't find the file sections in `%s': %s", 
	     bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
    }

  /* Relocate the section binding addresses as recorded in the shared
     object's file by the base address to which the object was actually
     mapped. */
  if (so->imcb.imcb$v_discontiguous)
    {
      /* Sections are discontiguous, usually this means the text section
	 is installed shareable and is in high memory and the rest of
	 the sections are elsewhere */

      int k;
      struct section_table *p;

      p = so->sections;
      /* Do the code sections (hopefully there's only one) */
      for (k=0; k<so->kferes->kferes$l_count; k++)
	{
	  KFERES_SECTION *ks;

	  ks = so->kferes_sections [k];
	  while (p < so->sections_end
		 && !(p->the_bfd_section->flags & SEC_CODE))
	    p++;

	  if (p < so->sections_end)
	    {
#if 0
	      printf ("text section %s (osize: %x, nsize: %x) moved to %llx\n",
		      p->the_bfd_section->name,
		      p->endaddr - p->addr,
		      ks->kferes$l_length,
		      ks->kferes$l_va);
#endif
	      p->addr = (CORE_ADDR) ks->kferes$l_va;
	      p->endaddr = p->addr + ks->kferes$l_length - 1;
	      so->textsection = p;

	      p++;
	    }
	  else
	    error ("Couldn't find text section in \"%s\".",
		   TO_HOST_FILE_SPEC (scratch_pathname));
	}

      /* Do the data sections */
      if (so->kferes->kferes$l_data_count > 0)
	{
	  /* Sometimes that data sections are listed in separate
	     KFERES_SECTION structures, but all the examples seen
	     have them actually at contiguous addresses. */

	  p = so->sections;
	  for (k = so->kferes->kferes$l_count;
	       k < (so->kferes->kferes$l_count
		    + so->kferes->kferes$l_data_count);
	       k++)
	    {
	      KFERES_SECTION *ks;
	      
	      ks = so->kferes_sections [k];
	      while (p < so->sections_end
		     && (p->the_bfd_section->flags & SEC_CODE))
		p++;
	      
	      if (p < so->sections_end)
		{
#if 0
		  printf ("data section %s (osize: %x, nsize: %x) moved to %llx\n",
			  p->the_bfd_section->name,
			  p->endaddr - p->addr,
			  ks->kferes$l_length,
			  ks->kferes$l_va);
#endif
		  p->addr = (CORE_ADDR) ks->kferes$l_va;
		  p->endaddr = p->addr + ks->kferes$l_length - 1;
		  so -> imcbend
		    = (CORE_ADDR) max (p -> endaddr, so -> imcbend);

		  p++;
		}
	      else
		error ("Couldn't find all data sections in \"%s\".",
		       TO_HOST_FILE_SPEC (scratch_pathname));
	    }
	}
      else
	{
	  /* And sometimes the data sections are lumped together */

	  for (p = so -> sections; p < so -> sections_end; p++)
	    {
	      if (!(p->the_bfd_section->flags & SEC_CODE))
		{
		  p -> addr += (CORE_ADDR) IMCB_ADDR (so);
		  p -> endaddr += (CORE_ADDR) IMCB_ADDR (so);

		  so -> imcbend
		    = (CORE_ADDR) max (p -> endaddr, so -> imcbend);

#if 0
		  printf ("data section %s moved to %llx\n",
			  p->the_bfd_section->name,
			  p->addr);
#endif
		}
	    }
	}
    }
  else
    {
      /* All sections are contiguous */

      struct section_table *p;

      for (p = so -> sections; p < so -> sections_end; p++)
	{
	  p -> addr += (CORE_ADDR) IMCB_ADDR (so);
	  p -> endaddr += (CORE_ADDR) IMCB_ADDR (so);
#if 0
	  printf ("section %s moved to %llx\n",
		  p->the_bfd_section->name,
		  p->addr);
#endif
	  so -> imcbend = (CORE_ADDR) max (p -> endaddr, so -> imcbend);
	  if (p->the_bfd_section->flags & SEC_CODE)
	    so -> textsection = p;
	}
    }

  do_cleanups (old_chain);
}

/*

LOCAL FUNCTION

	special_symbol_handling -- additional shared library symbol handling

SYNOPSIS

	void special_symbol_handling (struct so_list *so)

DESCRIPTION

	Once the symbols from a shared object have been loaded in the usual
	way, we are called to do any system specific symbol handling that 
	is needed.

*/

static void
special_symbol_handling (so)
     struct so_list *so;
{
}

/* A small stub to get us past the arg-passing pinhole of catch_errors.  */

static int
symbol_add_stub (arg)
     char *arg;
{
  register struct so_list *so = (struct so_list *) arg;	/* catch_errs bogon */
  
  so -> objfile =
    symbol_file_add
      (so->so_name, so->from_tty, (CORE_ADDR) IMCB_ADDR (so), 0, 0, 0);
  return (1);
}

extern void
print_section_info ();

int
evax_files_info ()
{
  struct so_list *so;
  bfd *abfd;

  for (so = so_list_head; so; so = so->next)
    {
      if (so->objfile)
	{
	  abfd = so->objfile->obfd;
	}
      else
	{
	  abfd = so->abfd;
	}
      if (abfd)
	{
	  struct target_ops t;

	  build_section_table (abfd, &t.to_sections, &t.to_sections_end);
	  printf_filtered ("Shareable image file:\n");
	  print_section_info (&t, abfd);

	  free (t.to_sections);
	}
    }

  return 0;
}

/*

GLOBAL FUNCTION

	evax_solib_add -- add a shared library file to the symtab
	                  and section list

SYNOPSIS

	void evax_solib_add (char *arg_string, int from_tty,
			struct target_ops *target)

DESCRIPTION

*/

void
evax_solib_add (arg_string, from_tty, target)
     char *arg_string;
     int from_tty;
     struct target_ops *target;
{	
  register struct so_list *so = NULL;   	/* link map state variable */

  /* Last shared library that we read.  */
  struct so_list *so_last = NULL;

  char *re_err;
  int count;
  int old;
  
  if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
    {
      error ("Invalid regexp: %s", re_err);
    }

  /* Add the shared library sections to the section table of the
     specified target, if any.  */
  if (target)
    {
      /* Count how many new section_table entries there are.  */
      so = NULL;
      count = 0;
      while ((so = find_solib (so)) != NULL)
	{
	  if (so -> so_name[0])
	    {
	      count += so -> sections_end - so -> sections;
	    }
	}
      
      if (count)
	{
	  /* Reallocate the target's section table including the new size.  */
	  if (target -> to_sections)
	    {
	      old = target -> to_sections_end - target -> to_sections;
	      target -> to_sections = (struct section_table *)
		xrealloc ((char *)target -> to_sections,
			 (sizeof (struct section_table)) * (count + old));
	    }
	  else
	    {
	      old = 0;
	      target -> to_sections = (struct section_table *)
		xmalloc ((sizeof (struct section_table)) * count);
	    }
	  target -> to_sections_end = target -> to_sections + (count + old);
	  
	  /* Add these section table entries to the target's table.  */
	  while ((so = find_solib (so)) != NULL)
	    {
	      if (so -> so_name[0])
		{
		  count = so -> sections_end - so -> sections;
		  memcpy ((char *) (target -> to_sections + old),
			  so -> sections, 
			  (sizeof (struct section_table)) * count);
		  old += count;
		}
	    }
	}
    }
  
  /* Now add the symbol files.  */
  while ((so = find_solib (so)) != NULL)
    {
      if (so -> so_name[0] && re_exec (so -> so_name))
	{
	  so -> from_tty = from_tty;
	  if (so -> symbols_loaded)
	    {
	      if (from_tty)
		{
		  printf_unfiltered ("Symbols already loaded for %s\n",
				     TO_HOST_FILE_SPEC (so -> so_name));
		}
	    }
	  else if (catch_errors
		   (symbol_add_stub, (char *) so,
		    "Error while reading shared library symbols:\n",
		    RETURN_MASK_ALL))
	    {
	      so_last = so;
	      so -> symbols_loaded = 1;
	    }
	}
    }

  /* Getting new symbols may change our opinion about what is
     frameless.  */
  if (so_last)
    reinit_frame_cache ();

  if (so_last)
    special_symbol_handling (so_last);
}

void
evax_solib_create_inferior_hook ()
{
  IMCB imcb;
  KFERES kferes;
  KFERES_SECTION kferes_section;
  char buff1 [256];
  char buff2 [256];
  struct so_list *so;

  /* Rereading the shared images causes a memory leak wrt symbol table info
     (objfile->obfd).  This needs to be rethought, maybe only reading in
     those images perturbed by run-time addition of message shared images.
     9/15/98 */

  if (so_list_head)
    return;

  /* Reinitialize imglstptr and reread shared images on restarts, otherwise
     the pointers get messed up due to the run-time addition of
     message shared images. */

  for (so = so_list_head; so; so = so->next)
    {
      if (so->abfd)
	bfd_close (so->abfd);
    }

  imglstptr = (IMCB *)vmschild_get_imglstptr ();
  so_list_head = 0;

#if 0
  target_read_memory ((CORE_ADDR) imglstptr, (char *)&imcb, sizeof (IMCB));
  for (; imcb.imcb$l_flink != imglstptr;)
    {
      target_read_memory
	((CORE_ADDR) imcb.imcb$l_flink, (char *)&imcb, sizeof (IMCB));
      strncpy (buff1,
	       &imcb.imcb$t_log_image_name [1],
	       imcb.imcb$t_log_image_name [0]);
      buff1 [imcb.imcb$t_log_image_name[0]] = 0;
      strncpy (buff2,
	       &imcb.imcb$t_image_name [1],
	       imcb.imcb$t_image_name [0]);
      buff2 [imcb.imcb$t_image_name[0]] = 0;
      
      printf ("%s %s: %llx to %llx\n",
	      buff1,
	      buff2,
	      imcb.imcb$l_starting_address,
	      imcb.imcb$l_end_address);
      
      if (imcb.imcb$v_discontiguous)
	{
	  int k;
	  int count;
	  KFERES_SECTION *kferes_section_ptr;
	  KFERES *kferes_ptr = imcb.imcb$l_kferes_ptr;
	  
	  target_read_memory ((CORE_ADDR) kferes_ptr,
			      (char *)&kferes,
			      sizeof (KFERES));
	  
	  count = kferes.kferes$l_count + kferes.kferes$l_data_count;
	  
	  for (k = 0, kferes_section_ptr = (KFERES_SECTION *)(kferes_ptr + 1);
	       k < count;
	       k++, kferes_section_ptr++)
	    {
	      if (k < kferes.kferes$l_count)
		printf ("   CODE: ");
	      else
		printf ("   DATA: ");
	      
	      target_read_memory ((CORE_ADDR) kferes_section_ptr,
				  (char *)&kferes_section,
				  sizeof (KFERES_SECTION));
	      
	      printf ("%llx to %llx\n",
		      kferes_section.kferes$l_va,
		      (unsigned long)kferes_section.kferes$l_va
		      + kferes_section.kferes$l_length - 1);
	    }
	}
    }
#endif

  if (auto_solib_add)
    evax_solib_add ((char *) 0, 0, (struct target_ops *) 0);
}

char *
evax_solib_address (address)
     CORE_ADDR address;
{
  register struct so_list *so = 0;   	/* link map state variable */
  
  while ((so = find_solib (so)) != NULL)
    {
      /* The main image name was ignored to make this field null, since
	 we probably don't want to have those functions show up in a
	 shared library.  Can this ever really happen? */
      if (so -> so_name[0])
	{
#if 0
	  printf ("checking if %x in %s: %x %x\n",
		  address,
		  so->so_name,
		  IMCB_ADDR (so),
		  so->imcbend);
#endif
	  if ((so->textsection)
	      && ((address >= so->textsection->addr) &&
		  (address <= so->textsection->endaddr)))
	    return (so->so_name);
	}
    }
  return (0);
}

void
_initialize_solib()
{
  add_com ("sharedimage", class_files, sharedimage_command,
	   "Load shareable image symbols for files matching REGEXP.");
  add_info ("sharedimage", info_sharedimage_command, 
	    "Status of loaded shareable images.");

  add_show_from_set
    (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
		  (char *) &auto_solib_add,
		  "Set autoloading of shared library symbols.\n\
If nonzero, symbols from all shareable images will be loaded\n\
automatically when the inferior begins execution or when the loader\n\
informs gdb that a new image has been loaded.  Otherwise, symbols\n\
must be loaded manually, using `sharedimage'.",
		  &setlist),
     &showlist);

}