aboutsummaryrefslogtreecommitdiff
path: root/bfd/nlmcode.h
blob: 127eb9437a1a6062a8b405df16ca8f7050970e83 (plain)
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
/* NLM (NetWare Loadable Module) executable support for BFD.
   Copyright (C) 1993 Free Software Foundation, Inc.

   Written by Fred Fish @ Cygnus Support, using ELF support as the
   template.

This file is part of BFD, the Binary File Descriptor library.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <string.h>		/* For strrchr and friends */
#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
#include "libnlm.h"

#define Nlm_External_Fixed_Header	NAME(Nlm,External_Fixed_Header)
#define Nlm_External_Version_Header	NAME(Nlm,External_Version_Header)
#define Nlm_External_Copyright_Header	NAME(Nlm,External_Copyright_Header)
#define Nlm_External_Extended_Header	NAME(Nlm,External_Extended_Header)
#define Nlm_External_Custom_Header	NAME(Nlm,External_Custom_Header)

#define nlm_symbol_type			NAME(nlm,symbol_type)

#define nlm_get_symtab_upper_bound	NAME(bfd_nlm,get_symtab_upper_bound)
#define nlm_get_symtab			NAME(bfd_nlm,get_symtab)
#define nlm_make_empty_symbol		NAME(bfd_nlm,make_empty_symbol)
#define nlm_print_symbol		NAME(bfd_nlm,print_symbol)
#define nlm_get_symbol_info		NAME(bfd_nlm,get_symbol_info)
#define nlm_set_arch_mach		NAME(bfd_nlm,set_arch_mach)
#define nlm_object_p			NAME(bfd_nlm,object_p)

#define MSB (~(~(unsigned long)0 >> 1))

/* Forward declarations of static functions */

static boolean add_bfd_section
  PARAMS ((bfd *, char *, file_ptr, bfd_size_type, flagword));

static void nlm_swap_fixed_header_in
  PARAMS ((bfd *, Nlm_External_Fixed_Header *, Nlm_Internal_Fixed_Header *));

static boolean nlm_swap_variable_header_in PARAMS ((bfd *));

static boolean nlm_swap_auxiliary_headers_in PARAMS ((bfd *));

static struct sec *section_from_nlm_index PARAMS ((bfd *, int));

static int nlm_section_from_bfd_section PARAMS ((bfd *, struct sec *));

static boolean nlm_slurp_symbol_table PARAMS ((bfd *, asymbol **));

static int nlm_symbol_from_bfd_symbol
  PARAMS ((bfd *, struct symbol_cache_entry **));

static void nlm_map_symbols PARAMS ((bfd *));

/* Should perhaps use put_offset, put_word, etc.  For now, the two versions
   can be handled by explicitly specifying 32 bits or "the long type".  */
#if ARCH_SIZE == 64
#define put_word	bfd_h_put_64
#define get_word	bfd_h_get_64
#endif
#if ARCH_SIZE == 32
#define put_word	bfd_h_put_32
#define get_word	bfd_h_get_32
#endif

bfd_target *
DEFUN (nlm_object_p, (abfd), bfd * abfd)
{
  Nlm_External_Fixed_Header x_fxdhdr;	/* Nlm file header, external form */
  Nlm_Internal_Fixed_Header *i_fxdhdrp;	/* Nlm file header, internal form */
  int shindex;

  /* Read in the fixed length portion of the NLM header in external format.  */

  if (bfd_read ((PTR) &x_fxdhdr, sizeof (x_fxdhdr), 1, abfd) !=
      sizeof (x_fxdhdr))
    {
      bfd_error = system_call_error;
      return (NULL);
    }

  /* Check to see if we have an NLM file by matching the NLM signature. */

  if (strncmp (&x_fxdhdr.signature, NLM_SIGNATURE, NLM_SIGNATURE_SIZE) != 0)
    {
    wrong:
      bfd_error = wrong_format;
      return (NULL);
    }

  /* There's no supported way to discover the endianess of an NLM, so test for
     a sane version number after doing byte swapping appropriate for this
     XVEC.  (Hack alert!) */

  if (get_word (abfd, (bfd_byte *) &x_fxdhdr.version) > 0xFFFF)
    {
      goto wrong;
    }

  /* There's no supported way to check for 32 bit versus 64 bit addresses,
     so ignore this distinction for now.  (FIXME) */

  /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
     the tdata pointer in the bfd. */

  nlm_tdata (abfd) = (struct nlm_obj_tdata *)
    bfd_zalloc (abfd, sizeof (struct nlm_obj_tdata));
  if (nlm_tdata (abfd) == NULL)
    {
      bfd_error = no_memory;
      return (NULL);
    }

  /* FIXME:  Any `wrong' exits below here will leak memory (tdata).  */

  /* Swap in the rest of the fixed length header. */

  i_fxdhdrp = nlm_fixed_header (abfd);
  nlm_swap_fixed_header_in (abfd, &x_fxdhdr, i_fxdhdrp);

  if (!nlm_swap_variable_header_in (abfd)
      || !nlm_swap_auxiliary_headers_in (abfd)
      || !add_bfd_section (abfd, NLM_CODE_NAME,
			   i_fxdhdrp -> codeImageOffset,
			   i_fxdhdrp -> codeImageSize,
			   SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
      || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
			   i_fxdhdrp -> dataImageOffset,
			   i_fxdhdrp -> dataImageSize,
			   SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
      || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
			   (file_ptr) 0,
			   i_fxdhdrp -> uninitializedDataSize,
			   SEC_DATA | SEC_ALLOC))
    {
      return (NULL);
    }

  return (abfd -> xvec);
}

/* Add a section to the bfd. */

static boolean
DEFUN (add_bfd_section, (abfd, name, offset, size, flags),
       bfd *abfd AND
       char *name AND
       file_ptr offset AND
       bfd_size_type size AND
       flagword flags)
{
  asection *newsect;

  newsect = bfd_make_section (abfd, name);
  if (newsect == NULL)
    {
      return (false);
    }
  newsect -> vma = 0;				/* NLM's are relocatable. */
  newsect -> _raw_size = size;
  newsect -> filepos = offset;
  newsect -> flags = flags;
  newsect -> alignment_power = bfd_log2 (0);	/* FIXME */
  return (true);
}


/* Translate an NLM fixed length file header in external format into an NLM
   file header in internal format. */

static void
DEFUN (nlm_swap_fixed_header_in, (abfd, src, dst),
       bfd * abfd AND
       Nlm_External_Fixed_Header * src AND
       Nlm_Internal_Fixed_Header * dst)
{
  memcpy (dst -> signature, src -> signature, NLM_SIGNATURE_SIZE);
  memcpy (dst -> moduleName, src -> moduleName, NLM_MODULE_NAME_SIZE);
  dst -> version =
    get_word (abfd, (bfd_byte *) src -> version);
  dst -> codeImageOffset =
    get_word (abfd, (bfd_byte *) src -> codeImageOffset);
  dst -> codeImageSize =
    get_word (abfd, (bfd_byte *) src -> codeImageSize);
  dst -> dataImageOffset =
    get_word (abfd, (bfd_byte *) src -> dataImageOffset);
  dst -> dataImageSize =
    get_word (abfd, (bfd_byte *) src -> dataImageSize);
  dst -> uninitializedDataSize =
    get_word (abfd, (bfd_byte *) src -> uninitializedDataSize);
  dst -> customDataOffset =
    get_word (abfd, (bfd_byte *) src -> customDataOffset);
  dst -> customDataSize =
    get_word (abfd, (bfd_byte *) src -> customDataSize);
  dst -> moduleDependencyOffset =
    get_word (abfd, (bfd_byte *) src -> moduleDependencyOffset);
  dst -> numberOfModuleDependencies =
    get_word (abfd, (bfd_byte *) src -> numberOfModuleDependencies);
  dst -> relocationFixupOffset =
    get_word (abfd, (bfd_byte *) src -> relocationFixupOffset);
  dst -> numberOfRelocationFixups =
    get_word (abfd, (bfd_byte *) src -> numberOfRelocationFixups);
  dst -> externalReferencesOffset =
    get_word (abfd, (bfd_byte *) src -> externalReferencesOffset);
  dst -> numberOfExternalReferences =
    get_word (abfd, (bfd_byte *) src -> numberOfExternalReferences);
  dst -> publicsOffset =
    get_word (abfd, (bfd_byte *) src -> publicsOffset);
  dst -> numberOfPublics =
    get_word (abfd, (bfd_byte *) src -> numberOfPublics);
  dst -> debugInfoOffset =
    get_word (abfd, (bfd_byte *) src -> debugInfoOffset);
  dst -> numberOfDebugRecords =
    get_word (abfd, (bfd_byte *) src -> numberOfDebugRecords);
  dst -> codeStartOffset =
    get_word (abfd, (bfd_byte *) src -> codeStartOffset);
  dst -> exitProcedureOffset =
    get_word (abfd, (bfd_byte *) src -> exitProcedureOffset);
  dst -> checkUnloadProcedureOffset =
    get_word (abfd, (bfd_byte *) src -> checkUnloadProcedureOffset);
  dst -> moduleType =
    get_word (abfd, (bfd_byte *) src -> moduleType);
  dst -> flags = 
    get_word (abfd, (bfd_byte *) src -> flags);
}

/* Read and swap in the variable length header.  All the fields must
   exist in the NLM, and must exist in the order they are read here. */

static boolean
DEFUN (nlm_swap_variable_header_in, (abfd),
       bfd * abfd)
{
  unsigned char temp [TARGET_LONG_SIZE];

  /* Read the description length and text members. */

  if (bfd_read ((PTR) &nlm_variable_header (abfd) -> descriptionLength,
		sizeof (nlm_variable_header (abfd) -> descriptionLength),
		1, abfd) !=
      sizeof (nlm_variable_header (abfd) -> descriptionLength))
    {
      bfd_error = system_call_error;
      return (false);
    }
  if (bfd_read ((PTR) nlm_variable_header (abfd) -> descriptionText,
		nlm_variable_header (abfd) -> descriptionLength + 1,
		1, abfd) !=
      nlm_variable_header (abfd) -> descriptionLength + 1)
    {
      bfd_error = system_call_error;
      return (false);
    }

  /* Read and convert the stackSize field. */

  if (bfd_read ((PTR) &temp, sizeof (temp), 1, abfd) != sizeof (temp))
    {
      bfd_error = system_call_error;
      return (false);
    }
  nlm_variable_header (abfd) -> stackSize = get_word (abfd, (bfd_byte *) temp);

  /* Read and convert the reserved field. */

  if (bfd_read ((PTR) &temp, sizeof (temp), 1, abfd) != sizeof (temp))
    {
      bfd_error = system_call_error;
      return (false);
    }
  nlm_variable_header (abfd) -> reserved = get_word (abfd, (bfd_byte *) temp);

  /* Read the oldThreadName field.  This field is a fixed length string. */

  if (bfd_read ((PTR) nlm_variable_header (abfd) -> oldThreadName,
		sizeof (nlm_variable_header (abfd) -> oldThreadName),
		1, abfd) !=
      sizeof (nlm_variable_header (abfd) -> oldThreadName))
    {
      bfd_error = system_call_error;
      return (false);
    }

  /* Read the screen name length and text members. */

  if (bfd_read ((PTR) &nlm_variable_header (abfd) -> screenNameLength,
		sizeof (nlm_variable_header (abfd) -> screenNameLength),
		1, abfd) !=
      sizeof (nlm_variable_header (abfd) -> screenNameLength))
    {
      bfd_error = system_call_error;
      return (false);
    }
  if (bfd_read ((PTR) nlm_variable_header (abfd) -> screenName,
		nlm_variable_header (abfd) -> screenNameLength + 1,
		1, abfd) !=
      nlm_variable_header (abfd) -> screenNameLength + 1)
    {
      bfd_error = system_call_error;
      return (false);
    }

  /* Read the thread name length and text members. */

  if (bfd_read ((PTR) &nlm_variable_header (abfd) -> threadNameLength,
		sizeof (nlm_variable_header (abfd) -> threadNameLength),
		1, abfd) !=
      sizeof (nlm_variable_header (abfd) -> threadNameLength))
    {
      bfd_error = system_call_error;
      return (false);
    }
  if (bfd_read ((PTR) nlm_variable_header (abfd) -> threadName,
		nlm_variable_header (abfd) -> threadNameLength + 1,
		1, abfd) !=
      nlm_variable_header (abfd) -> threadNameLength + 1)
    {
      bfd_error = system_call_error;
      return (false);
    }
  return (true);
}

/* Read and swap in the contents of all the auxiliary headers.  Because of
   the braindead design, we have to do strcmps on strings of indeterminate
   length to figure out what each auxiliary header is.  Even worse, we have
   no way of knowing how many auxiliary headers there are or where the end
   of the auxiliary headers are, except by finding something that doesn't
   look like a known auxiliary header.  This means that the first new type
   of auxiliary header added will break all existing tools that don't
   recognize it. */

static boolean
DEFUN (nlm_swap_auxiliary_headers_in, (abfd),
       bfd * abfd)
{
  unsigned char temp [TARGET_LONG_SIZE];
  unsigned char tempstr [16];
  long position;

  for (;;)
    {
      position = bfd_tell (abfd);
      if (bfd_read ((PTR) tempstr, sizeof (tempstr), 1, abfd) !=
	  sizeof (tempstr))
	{
	  bfd_error = system_call_error;
	  return (false);
	}
      if (bfd_seek (abfd, position, SEEK_SET) == -1)
	{
	  bfd_error = system_call_error;
	  return (false);
	}
      if (strncmp (tempstr, "VeRsIoN#", 8) == 0)
	{
	  Nlm_External_Version_Header thdr;
	  if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
	    {
	      bfd_error = system_call_error;
	      return (false);
	    }
	  memcpy (nlm_version_header (abfd) -> stamp, thdr.stamp,
		  sizeof (thdr.stamp));
	  nlm_version_header (abfd) -> majorVersion =
	    get_word (abfd, (bfd_byte *) thdr.majorVersion);
	  nlm_version_header (abfd) -> minorVersion =
	    get_word (abfd, (bfd_byte *) thdr.minorVersion);
	  nlm_version_header (abfd) -> revision =
	    get_word (abfd, (bfd_byte *) thdr.revision);
	  nlm_version_header (abfd) -> year =
	    get_word (abfd, (bfd_byte *) thdr.year);
	  nlm_version_header (abfd) -> month =
	    get_word (abfd, (bfd_byte *) thdr.month);
	  nlm_version_header (abfd) -> day =
	    get_word (abfd, (bfd_byte *) thdr.day);
	}
      else if (strncmp (tempstr, "MeSsAgEs", 8) == 0)
	{
	  Nlm_External_Extended_Header thdr;
	  if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
	    {
	      bfd_error = system_call_error;
	      return (false);
	    }
	  memcpy (nlm_extended_header (abfd) -> stamp, thdr.stamp,
		  sizeof (thdr.stamp));
	  nlm_extended_header (abfd) -> languageID =
	    get_word (abfd, (bfd_byte *) thdr.languageID);
	  nlm_extended_header (abfd) -> messageFileOffset =
	    get_word (abfd, (bfd_byte *) thdr.messageFileOffset);
	  nlm_extended_header (abfd) -> messageFileLength =
	    get_word (abfd, (bfd_byte *) thdr.messageFileLength);
	  nlm_extended_header (abfd) -> messageCount =
	    get_word (abfd, (bfd_byte *) thdr.messageCount);
	  nlm_extended_header (abfd) -> helpFileOffset =
	    get_word (abfd, (bfd_byte *) thdr.helpFileOffset);
	  nlm_extended_header (abfd) -> helpFileLength =
	    get_word (abfd, (bfd_byte *) thdr.helpFileLength);
	  nlm_extended_header (abfd) -> RPCDataOffset =
	    get_word (abfd, (bfd_byte *) thdr.RPCDataOffset);
	  nlm_extended_header (abfd) -> RPCDataLength =
	    get_word (abfd, (bfd_byte *) thdr.RPCDataLength);
	  nlm_extended_header (abfd) -> sharedCodeOffset =
	    get_word (abfd, (bfd_byte *) thdr.sharedCodeOffset);
	  nlm_extended_header (abfd) -> sharedCodeLength =
	    get_word (abfd, (bfd_byte *) thdr.sharedCodeLength);
	  nlm_extended_header (abfd) -> sharedDataOffset =
	    get_word (abfd, (bfd_byte *) thdr.sharedDataOffset);
	  nlm_extended_header (abfd) -> sharedDataLength =
	    get_word (abfd, (bfd_byte *) thdr.sharedDataLength);
	  nlm_extended_header (abfd) -> sharedRelocationFixupOffset =
	    get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupOffset);
	  nlm_extended_header (abfd) -> sharedRelocationFixupCount =
	    get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupCount);
	  nlm_extended_header (abfd) -> sharedExternalReferenceOffset =
	    get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceOffset);
	  nlm_extended_header (abfd) -> sharedExternalReferenceCount =
	    get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceCount);
	  nlm_extended_header (abfd) -> sharedPublicsOffset =
	    get_word (abfd, (bfd_byte *) thdr.sharedPublicsOffset);
	  nlm_extended_header (abfd) -> sharedPublicsCount =
	    get_word (abfd, (bfd_byte *) thdr.sharedPublicsCount);
	  nlm_extended_header (abfd) -> SharedInitializationOffset =
	    get_word (abfd, (bfd_byte *) thdr.sharedInitializationOffset);
	  nlm_extended_header (abfd) -> SharedExitProcedureOffset =
	    get_word (abfd, (bfd_byte *) thdr.SharedExitProcedureOffset);
	  nlm_extended_header (abfd) -> productID =
	    get_word (abfd, (bfd_byte *) thdr.productID);
	  nlm_extended_header (abfd) -> reserved0 =
	    get_word (abfd, (bfd_byte *) thdr.reserved0);
	  nlm_extended_header (abfd) -> reserved1 =
	    get_word (abfd, (bfd_byte *) thdr.reserved1);
	  nlm_extended_header (abfd) -> reserved2 =
	    get_word (abfd, (bfd_byte *) thdr.reserved2);
	  nlm_extended_header (abfd) -> reserved3 =
	    get_word (abfd, (bfd_byte *) thdr.reserved3);
	  nlm_extended_header (abfd) -> reserved4 =
	    get_word (abfd, (bfd_byte *) thdr.reserved4);
	  nlm_extended_header (abfd) -> reserved5 =
	    get_word (abfd, (bfd_byte *) thdr.reserved5);
	}
      else if (strncmp (tempstr, "CuStHeAd", 8) == 0)
	{
	  Nlm_External_Custom_Header thdr;
	  if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
	    {
	      bfd_error = system_call_error;
	      return (false);
	    }
	  memcpy (nlm_custom_header (abfd) -> stamp, thdr.stamp,
		  sizeof (thdr.stamp));
	  nlm_custom_header (abfd) -> dataLength =
	    get_word (abfd, (bfd_byte *) thdr.dataLength);
	  nlm_custom_header (abfd) -> debugRecOffset =
	    get_word (abfd, (bfd_byte *) thdr.debugRecOffset);
	  nlm_custom_header (abfd) -> debugRecLength =
	    get_word (abfd, (bfd_byte *) thdr.debugRecLength);
	}
      else if (strncmp (tempstr, "CoPyRiGhT=", 10) == 0)
	{
	  Nlm_External_Copyright_Header thdr;
	  if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
	    {
	      bfd_error = system_call_error;
	      return (false);
	    }
	  memcpy (nlm_copyright_header (abfd) -> stamp, thdr.stamp,
		  sizeof (thdr.stamp));
	  nlm_copyright_header (abfd) -> copyrightMessageLength =
	    get_word (abfd, (bfd_byte *) thdr.copyrightMessageLength);
	  /* The copyright message is a variable length string. */
	  if (bfd_read ((PTR) nlm_copyright_header (abfd) -> copyrightMessage,
			nlm_copyright_header (abfd) -> copyrightMessageLength + 1,
			1, abfd) !=
	      nlm_copyright_header (abfd) -> copyrightMessageLength + 1)
	    {
	      bfd_error = system_call_error;
	      return (false);
	    }
	}
      else
	{
	  break;
	}
    }
}

/* We read the NLM's public symbols and use it to generate a bfd symbol
   table (hey, it's better than nothing) on a one-for-one basis.  Thus
   use the number of public symbols as the number of bfd symbols we will
   have once we actually get around to reading them in.

   Return the number of bytes required to hold the symtab vector, based on
   the count plus 1, since we will NULL terminate the vector allocated based
   on this size. */

unsigned int
DEFUN (nlm_get_symtab_upper_bound, (abfd), bfd * abfd)
{
  Nlm_Internal_Fixed_Header *i_fxdhdrp;	/* Nlm file header, internal form */
  unsigned int symcount;
  unsigned int symtab_size = 0;

  i_fxdhdrp = nlm_fixed_header (abfd);
  symcount = i_fxdhdrp -> numberOfPublics;
  symtab_size = (symcount + 1) * (sizeof (asymbol));
  return (symtab_size);
}

/* Note that bfd_get_symcount is guaranteed to be zero if slurping the
   symbol table fails. */

unsigned int
DEFUN (nlm_get_symtab, (abfd, alocation),
       bfd *abfd AND
       asymbol **alocation)
{
  nlm_slurp_symbol_table (abfd, alocation);
  return (bfd_get_symcount (abfd));
}

asymbol *
DEFUN (nlm_make_empty_symbol, (abfd),		/* FIXME! */
      bfd * abfd)
{
  asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
  new -> the_bfd = abfd;
  return new;
}

void
DEFUN (nlm_get_symbol_info, (ignore_abfd, symbol, ret),
       bfd * ignore_abfd AND
       asymbol * symbol AND
       symbol_info * ret)
{
  bfd_symbol_info (symbol, ret);
}

boolean
DEFUN (nlm_set_arch_mach, (abfd, arch, machine),
       bfd * abfd AND
       enum bfd_architecture arch AND
       unsigned long machine)
{
   return false;
}

/* Slurp in nlm symbol table.

   In the external (in-file) form, NLM export records are variable length,
   with the following form:

	1 byte		length of the symbol name (N)
	N bytes		the symbol name
	4 bytes		the symbol offset from start of it's section

   Note that we currently ignore the internal debug records.  There is
   a lot of duplication between the export records and the internal debug
   records.  We may in the future, want to merge the information from the
   debug records with the information from the export records to produce
   a more complete symbol table, treating additional information from the
   debug records as static symbols. (FIXME)

   If SYMPTRS is non-NULL, the bfd symbols are copied to where it points.

   When we return, the bfd symcount is either zero or contains the correct
   number of symbols.
*/

static boolean
DEFUN (nlm_slurp_symbol_table, (abfd, symptrs),
       bfd *abfd AND
       asymbol **symptrs)	/* Buffer for generated bfd symbols */
{
  Nlm_Internal_Fixed_Header *i_fxdhdrp;	/* Nlm file header, internal form */
  int symcount;				/* Number of external NLM symbols */
  int idx;
  nlm_symbol_type *sym;			/* Pointer to current bfd symbol */
  nlm_symbol_type *symbase;		/* Buffer for generated bfd symbols */
  char symlength;			/* Symbol length read into here */
  long symoffset;			/* Symbol offset read into here */

  if (bfd_get_outsymbols (abfd) != NULL)
    {
      return (true);
    }

  /* Read each raw NLM symbol, using the information to create a canonical bfd
     symbol table entry.

     Note that we allocate the initial bfd canonical symbol buffer based on a
     one-to-one mapping of the NLM symbols to canonical symbols.  We actually
     use all the NLM symbols, so there will be no space left over at the end.
     When we have all the symbols, we build the caller's pointer vector. */

  abfd -> symcount = 0;
  i_fxdhdrp = nlm_fixed_header (abfd);
  symcount = i_fxdhdrp -> numberOfPublics;
  if (symcount == 0)
    {
      return (true);
    }
  if (bfd_seek (abfd, i_fxdhdrp -> publicsOffset, SEEK_SET) == -1)
    {
      bfd_error = system_call_error;
      return (false);
    }
  symbase = (nlm_symbol_type *)
    bfd_zalloc (abfd, symcount * sizeof (nlm_symbol_type));
  sym = symbase;

  /* We use the bfd's symcount directly as the control count, so that early
     termination of the loop leaves the symcount correct for the symbols that
     were read. */

  while (abfd -> symcount < symcount)
    {
      if (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd)
	  != sizeof (symlength))
	{
	  bfd_error = system_call_error;
	  return (false);
	}
      sym -> symbol.the_bfd = abfd;
      sym -> symbol.name = bfd_alloc (abfd, symlength + 1);
      if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd)
	  != symlength)
	{
	  bfd_error = system_call_error;
	  return (false);
	}
      if (bfd_read ((PTR) &symoffset, sizeof (symoffset), 1, abfd)
	  != sizeof (symoffset))
	{
	  bfd_error = system_call_error;
	  return (false);
	}
      sym -> symbol.flags = BSF_GLOBAL | BSF_EXPORT;
      sym -> symbol.value = get_word (abfd, (bfd_byte *) &symoffset);
      if (sym -> symbol.value & MSB)
	{
	  sym -> symbol.value &= ~MSB;
	  sym -> symbol.flags |= BSF_FUNCTION;
	  sym -> symbol.section =
	    bfd_get_section_by_name (abfd, NLM_CODE_NAME);
	}
      else
	{
	  sym -> symbol.section =
	    bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
	}
      abfd -> symcount++;
      sym++;
    }

  /* Fill in the user's symbol pointer vector if needed.  */

  if (symptrs != NULL)
    {
      sym = symbase;
      symcount = abfd -> symcount;
      while (symcount-- > 0)
	{
	  *symptrs++ = &sym -> symbol;
	  sym++;
	}
      *symptrs = NULL;		/* Final NULL pointer */
    }

  return (true);
}