File: simple-object-coff.c

package info (click to toggle)
binutils 2.46-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 417,664 kB
  • sloc: ansic: 1,487,508; asm: 829,455; cpp: 216,692; exp: 80,524; makefile: 73,157; sh: 24,213; yacc: 15,060; lisp: 13,632; perl: 13,404; lex: 1,714; ada: 1,681; pascal: 1,446; cs: 879; python: 637; java: 478; sed: 191; xml: 95; awk: 25
file content (1106 lines) | stat: -rw-r--r-- 33,224 bytes parent folder | download | duplicates (4)
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
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
/* simple-object-coff.c -- routines to manipulate COFF object files.
   Copyright (C) 2010-2026 Free Software Foundation, Inc.
   Written by Ian Lance Taylor, Google.

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, 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, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */

#include "config.h"
#include "libiberty.h"
#include "simple-object.h"

#include <errno.h>
#include <stddef.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#include "simple-object-common.h"

/* COFF structures and constants.  */

/* COFF file header.  */

struct external_filehdr
{
  unsigned char f_magic[2];	/* magic number			*/
  unsigned char f_nscns[2];	/* number of sections		*/
  unsigned char f_timdat[4];	/* time & date stamp		*/
  unsigned char f_symptr[4];	/* file pointer to symtab	*/
  unsigned char f_nsyms[4];	/* number of symtab entries	*/
  unsigned char f_opthdr[2];	/* sizeof(optional hdr)		*/
  unsigned char f_flags[2];	/* flags			*/
};

/* BigObj COFF file header.  */

struct external_filehdr_bigobj
{
  unsigned char sig1[2];	/* Must be 0x0000 */
  unsigned char sig2[2];	/* Must be 0xFFFF */
  unsigned char version[2];	/* Version, currently 2 */
  unsigned char machine[2];	/* Machine type */
  unsigned char timdat[4];	/* time & date stamp */
  unsigned char classid[16];	/* Magic GUID that identifies BigObj format */
  unsigned char sizeofdata[4];	/* Size of data (unused, set to 0) */
  unsigned char flags[4];	/* Flags (unused, set to 0) */
  unsigned char metadatasize[4];	/* Metadata size (unused, set to 0) */
  unsigned char metadataoffset[4];	/* Metadata offset (unused, set to 0) */
  unsigned char nscns[4];	/* number of sections (32-bit!) */
  unsigned char symptr[4];	/* file pointer to symtab */
  unsigned char nsyms[4];	/* number of symtab entries */
};

/* The BigObj magic GUID (ClassID).  */
static const unsigned char bigobj_magic[16] =
{
  0xC7, 0xA1, 0xBA, 0xD1, 0xEE, 0xBA, 0xA9, 0x4B,
  0xAF, 0x20, 0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8
};

/* Bits for filehdr f_flags field.  */

#define F_EXEC			(0x0002)
#define IMAGE_FILE_SYSTEM	(0x1000)
#define IMAGE_FILE_DLL		(0x2000)

/* COFF section header.  */

struct external_scnhdr
{
  unsigned char s_name[8];	/* section name				*/
  unsigned char s_paddr[4];	/* physical address, aliased s_nlib 	*/
  unsigned char s_vaddr[4];	/* virtual address			*/
  unsigned char s_size[4];	/* section size				*/
  unsigned char s_scnptr[4];	/* file ptr to raw data for section 	*/
  unsigned char s_relptr[4];	/* file ptr to relocation		*/
  unsigned char s_lnnoptr[4];	/* file ptr to line numbers		*/
  unsigned char s_nreloc[2];	/* number of relocation entries		*/
  unsigned char s_nlnno[2];	/* number of line number entries	*/
  unsigned char s_flags[4];	/* flags				*/
};

/* The length of the s_name field in struct external_scnhdr.  */

#define SCNNMLEN (8)

/* Bits for scnhdr s_flags field.  This includes some bits defined
   only for PE.  This may need to be moved into coff_magic.  */

#define STYP_DATA			(1 << 6)
#define IMAGE_SCN_MEM_DISCARDABLE	(1 << 25)
#define IMAGE_SCN_MEM_SHARED		(1 << 28)
#define IMAGE_SCN_MEM_READ		(1 << 30)

#define IMAGE_SCN_ALIGN_POWER_BIT_POS	     20
#define IMAGE_SCN_ALIGN_POWER_CONST(val)     \
  (((val) + 1) << IMAGE_SCN_ALIGN_POWER_BIT_POS)

/* COFF symbol table entry.  */

#define E_SYMNMLEN	8	/* # characters in a symbol name	*/

struct external_syment
{
  union
  {
    unsigned char e_name[E_SYMNMLEN];

    struct
    {
      unsigned char e_zeroes[4];
      unsigned char e_offset[4];
    } e;
  } e;

  unsigned char e_value[4];
  unsigned char e_scnum[2];
  unsigned char e_type[2];
  unsigned char e_sclass[1];
  unsigned char e_numaux[1];
};

/* BigObj COFF symbol table entry (20 bytes instead of 18).  */

struct external_syment_bigobj
{
  union
  {
    unsigned char e_name[E_SYMNMLEN];

    struct
    {
      unsigned char e_zeroes[4];
      unsigned char e_offset[4];
    } e;
  } e;

  unsigned char e_value[4];
  unsigned char e_scnum[4];	/* 32-bit section number! */
  unsigned char e_type[2];
  unsigned char e_sclass[1];
  unsigned char e_numaux[1];
};

/* Length allowed for filename in aux sym format 4.  */

#define E_FILNMLEN	18

/* Omits x_sym and other unused variants.  */

union external_auxent
{
  /* Aux sym format 4: file.  */
  union
  {
    char x_fname[E_FILNMLEN];
    struct
    {
      unsigned char x_zeroes[4];
      unsigned char x_offset[4];
    } x_n;
  } x_file;
  /* Aux sym format 5: section.  */
  struct
  {
    unsigned char x_scnlen[4];		/* section length		*/
    unsigned char x_nreloc[2];		/* # relocation entries		*/
    unsigned char x_nlinno[2];		/* # line numbers		*/
    unsigned char x_checksum[4];	/* section COMDAT checksum	*/
    unsigned char x_associated[2];	/* COMDAT assoc section index	*/
    unsigned char x_comdat[1];		/* COMDAT selection number	*/
  } x_scn;
};

/* BigObj auxiliary symbol (20 bytes to match symbol size).  */

union external_auxent_bigobj
{
  /* Aux sym format 4: file.  */
  union
  {
    char x_fname[E_FILNMLEN];
    struct
    {
      unsigned char x_zeroes[4];
      unsigned char x_offset[4];
    } x_n;
  } x_file;
  /* Aux sym format 5: section.  */
  struct
  {
    unsigned char x_scnlen[4];		/* section length		*/
    unsigned char x_nreloc[2];		/* # relocation entries		*/
    unsigned char x_nlinno[2];		/* # line numbers		*/
    unsigned char x_checksum[4];	/* section COMDAT checksum	*/
    unsigned char x_associated[2];	/* COMDAT assoc section index	*/
    unsigned char x_comdat[1];		/* COMDAT selection number	*/
    unsigned char x_pad[3];		/* Padding to 20 bytes		*/
  } x_scn;
};

/* Symbol-related constants.  */

#define IMAGE_SYM_DEBUG		(-2)
#define IMAGE_SYM_TYPE_NULL	(0)
#define IMAGE_SYM_DTYPE_NULL	(0)
#define IMAGE_SYM_CLASS_STATIC	(3)
#define IMAGE_SYM_CLASS_FILE	(103)

#define IMAGE_SYM_TYPE \
  ((IMAGE_SYM_DTYPE_NULL << 4) | IMAGE_SYM_TYPE_NULL)

/* Private data for an simple_object_read.  */

struct simple_object_coff_read
{
  /* Magic number.  */
  unsigned short magic;
  /* Whether the file is big-endian.  */
  unsigned char is_big_endian;
  /* Whether this is BigObj format.  */
  unsigned char is_bigobj;
  /* Number of sections.  */
  unsigned int nscns;
  /* File offset of symbol table.  */
  off_t symptr;
  /* Number of symbol table entries.  */
  unsigned int nsyms;
  /* Flags.  */
  unsigned short flags;
  /* Offset of section headers in file.  */
  off_t scnhdr_offset;
};

/* Private data for an simple_object_attributes.  */

struct simple_object_coff_attributes
{
  /* Magic number.  */
  unsigned short magic;
  /* Whether the file is big-endian.  */
  unsigned char is_big_endian;
  /* Whether this is BigObj format.  */
  unsigned char is_bigobj;
  /* Flags.  */
  unsigned short flags;
};

/* There is no magic number which indicates a COFF file as opposed to
   any other sort of file.  Instead, each COFF file starts with a
   two-byte magic number which also indicates the type of the target.
   This struct holds a magic number as well as characteristics of that
   COFF format.  */

struct coff_magic_struct
{
  /* Magic number.  */
  unsigned short magic;
  /* Whether this magic number is for a big-endian file.  */
  unsigned char is_big_endian;
  /* Flag bits, in the f_flags fields, which indicates that this file
     is not a relocatable object file.  There is no flag which
     specifically indicates a relocatable object file, it is only
     implied by the absence of these flags.  */
  unsigned short non_object_flags;
};

/* This is a list of the COFF magic numbers which we recognize, namely
   the ones used on Windows.  More can be added as needed.  */

static const struct coff_magic_struct coff_magic[] =
{
  /* i386.  */
  { 0x14c, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL },
  /* x86_64.  */
  { 0x8664, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL },
  /* AArch64.  */
  { 0xaa64, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL }
};

/* See if we have a COFF file.  */

static void *
simple_object_coff_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
			  int descriptor, off_t offset,
			  const char *segment_name ATTRIBUTE_UNUSED,
			  const char **errmsg, int *err)
{
  size_t c;
  unsigned short magic_big;
  unsigned short magic_little;
  unsigned short magic;
  size_t i;
  int is_big_endian;
  unsigned short (*fetch_16) (const unsigned char *);
  unsigned int (*fetch_32) (const unsigned char *);
  unsigned char hdrbuf[sizeof (struct external_filehdr_bigobj)];
  unsigned short flags;
  struct simple_object_coff_read *ocr;
  unsigned short sig1, sig2;

  /* Try regular COFF first.  */
  c = sizeof (coff_magic) / sizeof (coff_magic[0]);
  magic_big = simple_object_fetch_big_16 (header);
  magic_little = simple_object_fetch_little_16 (header);
  for (i = 0; i < c; ++i)
    {
      if (coff_magic[i].is_big_endian
	  ? coff_magic[i].magic == magic_big
	  : coff_magic[i].magic == magic_little)
	break;
    }

  /* Check for BigObj if regular COFF didn't match.  */
  sig1 = simple_object_fetch_little_16 (header);
  sig2 = simple_object_fetch_little_16 (header + 2);

  if (i >= c && (sig1 != 0 || sig2 != 0xFFFF))
    {
      /* Not regular COFF and not BigObj.  */
      *errmsg = NULL;
      *err = 0;
      return NULL;
    }

  if (sig1 == 0 && sig2 == 0xFFFF)
    {
      /* This looks like BigObj.  Verify the ClassID.  */
      unsigned char bigobj_hdrbuf[sizeof (struct external_filehdr_bigobj)];

      if (!simple_object_internal_read (descriptor, offset, bigobj_hdrbuf,
					sizeof bigobj_hdrbuf, errmsg, err))
	return NULL;

      if (memcmp (bigobj_hdrbuf + offsetof (struct external_filehdr_bigobj,
					    classid),
		  bigobj_magic, 16) != 0)
	{
	  *errmsg = NULL;
	  *err = 0;
	  return NULL;
	}

      /* BigObj is always little-endian.  */
      is_big_endian = 0;

      ocr = XNEW (struct simple_object_coff_read);
      ocr->magic = simple_object_fetch_little_16
		     (bigobj_hdrbuf
		      + offsetof (struct external_filehdr_bigobj, machine));
      ocr->is_big_endian = 0;
      ocr->is_bigobj = 1;
      ocr->nscns = simple_object_fetch_little_32
		     (bigobj_hdrbuf
		      + offsetof (struct external_filehdr_bigobj, nscns));
      ocr->symptr = simple_object_fetch_little_32
		      (bigobj_hdrbuf
		       + offsetof (struct external_filehdr_bigobj, symptr));
      ocr->nsyms = simple_object_fetch_little_32
		     (bigobj_hdrbuf
		      + offsetof (struct external_filehdr_bigobj, nsyms));
      ocr->flags = simple_object_fetch_little_32
		     (bigobj_hdrbuf
		      + offsetof (struct external_filehdr_bigobj, flags));
      ocr->scnhdr_offset = sizeof (struct external_filehdr_bigobj);

      return (void *) ocr;
    }

  /* Regular COFF.  */
  is_big_endian = coff_magic[i].is_big_endian;

  magic = is_big_endian ? magic_big : magic_little;
  fetch_16 = (is_big_endian
	      ? simple_object_fetch_big_16
	      : simple_object_fetch_little_16);
  fetch_32 = (is_big_endian
	      ? simple_object_fetch_big_32
	      : simple_object_fetch_little_32);

  if (!simple_object_internal_read (descriptor, offset, hdrbuf, sizeof (struct external_filehdr),
				    errmsg, err))
    return NULL;

  flags = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_flags));
  if ((flags & coff_magic[i].non_object_flags) != 0)
    {
      *errmsg = "not relocatable object file";
      *err = 0;
      return NULL;
    }

  ocr = XNEW (struct simple_object_coff_read);
  ocr->magic = magic;
  ocr->is_big_endian = is_big_endian;
  ocr->is_bigobj = 0;
  ocr->nscns = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_nscns));
  ocr->symptr = fetch_32 (hdrbuf
			  + offsetof (struct external_filehdr, f_symptr));
  ocr->nsyms = fetch_32 (hdrbuf + offsetof (struct external_filehdr, f_nsyms));
  ocr->flags = flags;
  ocr->scnhdr_offset = (sizeof (struct external_filehdr)
			+ fetch_16 (hdrbuf + offsetof (struct external_filehdr,
						       f_opthdr)));

  return (void *) ocr;
}

/* Read the string table in a COFF file.  */

static char *
simple_object_coff_read_strtab (simple_object_read *sobj, size_t *strtab_size,
				const char **errmsg, int *err)
{
  struct simple_object_coff_read *ocr =
    (struct simple_object_coff_read *) sobj->data;
  off_t strtab_offset;
  unsigned char strsizebuf[4];
  size_t strsize;
  char *strtab;
  size_t sym_size;

  /* Symbol size depends on format.  */
  sym_size = ocr->is_bigobj ? sizeof (struct external_syment_bigobj)
			    : sizeof (struct external_syment);

  strtab_offset = sobj->offset + ocr->symptr + ocr->nsyms * sym_size;
  if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
				    strsizebuf, 4, errmsg, err))
    return NULL;
  strsize = (ocr->is_big_endian
	     ? simple_object_fetch_big_32 (strsizebuf)
	     : simple_object_fetch_little_32 (strsizebuf));
  strtab = XNEWVEC (char, strsize);
  if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
				    (unsigned char *) strtab, strsize, errmsg,
				    err))
    {
      XDELETEVEC (strtab);
      return NULL;
    }
  *strtab_size = strsize;
  return strtab;
}

/* Find all sections in a COFF file.  */

static const char *
simple_object_coff_find_sections (simple_object_read *sobj,
				  int (*pfn) (void *, const char *,
					      off_t offset, off_t length),
				  void *data,
				  int *err)
{
  struct simple_object_coff_read *ocr =
    (struct simple_object_coff_read *) sobj->data;
  size_t scnhdr_size;
  unsigned char *scnbuf;
  const char *errmsg;
  unsigned int (*fetch_32) (const unsigned char *);
  unsigned int nscns;
  char *strtab;
  size_t strtab_size;
  unsigned int i;

  scnhdr_size = sizeof (struct external_scnhdr);
  scnbuf = XNEWVEC (unsigned char, scnhdr_size * ocr->nscns);
  if (!simple_object_internal_read (sobj->descriptor,
				    sobj->offset + ocr->scnhdr_offset,
				    scnbuf, scnhdr_size * ocr->nscns, &errmsg,
				    err))
    {
      XDELETEVEC (scnbuf);
      return errmsg;
    }

  fetch_32 = (ocr->is_big_endian
	      ? simple_object_fetch_big_32
	      : simple_object_fetch_little_32);

  nscns = ocr->nscns;
  strtab = NULL;
  strtab_size = 0;
  for (i = 0; i < nscns; ++i)
    {
      unsigned char *scnhdr;
      unsigned char *scnname;
      char namebuf[SCNNMLEN + 1];
      char *name;
      off_t scnptr;
      unsigned int size;

      scnhdr = scnbuf + i * scnhdr_size;
      scnname = scnhdr + offsetof (struct external_scnhdr, s_name);
      memcpy (namebuf, scnname, SCNNMLEN);
      namebuf[SCNNMLEN] = '\0';
      name = &namebuf[0];
      if (namebuf[0] == '/')
	{
	  size_t strindex;
	  char *end;

	  strindex = strtol (namebuf + 1, &end, 10);
	  if (*end == '\0')
	    {
	      /* The real section name is found in the string
		 table.  */
	      if (strtab == NULL)
		{
		  strtab = simple_object_coff_read_strtab (sobj,
							   &strtab_size,
							   &errmsg, err);
		  if (strtab == NULL)
		    {
		      XDELETEVEC (scnbuf);
		      return errmsg;
		    }
		}

	      if (strindex < 4 || strindex >= strtab_size)
		{
		  XDELETEVEC (strtab);
		  XDELETEVEC (scnbuf);
		  *err = 0;
		  return "section string index out of range";
		}

	      name = strtab + strindex;
	    }
	}

      scnptr = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_scnptr));
      size = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_size));

      if (!(*pfn) (data, name, scnptr, size))
	break;
    }

  if (strtab != NULL)
    XDELETEVEC (strtab);
  XDELETEVEC (scnbuf);

  return NULL;
}

/* Fetch the attributes for an simple_object_read.  */

static void *
simple_object_coff_fetch_attributes (simple_object_read *sobj,
				     const char **errmsg ATTRIBUTE_UNUSED,
				     int *err ATTRIBUTE_UNUSED)
{
  struct simple_object_coff_read *ocr =
    (struct simple_object_coff_read *) sobj->data;
  struct simple_object_coff_attributes *ret;

  ret = XNEW (struct simple_object_coff_attributes);
  ret->magic = ocr->magic;
  ret->is_big_endian = ocr->is_big_endian;
  ret->is_bigobj = ocr->is_bigobj;
  ret->flags = ocr->flags;
  return ret;
}

/* Release the private data for an simple_object_read.  */

static void
simple_object_coff_release_read (void *data)
{
  XDELETE (data);
}

/* Compare two attributes structures.  */

static const char *
simple_object_coff_attributes_merge (void *todata, void *fromdata, int *err)
{
  struct simple_object_coff_attributes *to =
    (struct simple_object_coff_attributes *) todata;
  struct simple_object_coff_attributes *from =
    (struct simple_object_coff_attributes *) fromdata;

  if (to->magic != from->magic
      || to->is_big_endian != from->is_big_endian
      || to->is_bigobj != from->is_bigobj)
    {
      *err = 0;
      return "COFF object format mismatch";
    }
  return NULL;
}

/* Release the private data for an attributes structure.  */

static void
simple_object_coff_release_attributes (void *data)
{
  XDELETE (data);
}

/* Prepare to write out a file.  */

static void *
simple_object_coff_start_write (void *attributes_data,
				const char **errmsg ATTRIBUTE_UNUSED,
				int *err ATTRIBUTE_UNUSED)
{
  struct simple_object_coff_attributes *attrs =
    (struct simple_object_coff_attributes *) attributes_data;
  struct simple_object_coff_attributes *ret;

  /* We're just going to record the attributes, but we need to make a
     copy because the user may delete them.  */
  ret = XNEW (struct simple_object_coff_attributes);
  *ret = *attrs;
  return ret;
}

/* Write out a BigObj COFF filehdr.  */

static int
simple_object_coff_write_filehdr_bigobj (simple_object_write *sobj,
					 int descriptor,
					 unsigned int nscns,
					 size_t symtab_offset,
					 unsigned int nsyms,
					 const char **errmsg, int *err)
{
  struct simple_object_coff_attributes *attrs =
    (struct simple_object_coff_attributes *) sobj->data;
  unsigned char hdrbuf[sizeof (struct external_filehdr_bigobj)];
  unsigned char *hdr;
  void (*set_16) (unsigned char *, unsigned short);
  void (*set_32) (unsigned char *, unsigned int);

  hdr = &hdrbuf[0];

  /* BigObj is always little-endian.  */
  set_16 = simple_object_set_little_16;
  set_32 = simple_object_set_little_32;

  memset (hdr, 0, sizeof (struct external_filehdr_bigobj));

  /* Set BigObj signatures.  */
  set_16 (hdr + offsetof (struct external_filehdr_bigobj, sig1), 0);
  set_16 (hdr + offsetof (struct external_filehdr_bigobj, sig2), 0xFFFF);
  set_16 (hdr + offsetof (struct external_filehdr_bigobj, version), 2);
  set_16 (hdr + offsetof (struct external_filehdr_bigobj, machine),
	  attrs->magic);
  /* timdat left as zero.  */
  /* Copy ClassID.  */
  memcpy (hdr + offsetof (struct external_filehdr_bigobj, classid),
	  bigobj_magic, 16);
  /* sizeofdata, flags, metadatasize, metadataoffset left as zero.  */
  set_32 (hdr + offsetof (struct external_filehdr_bigobj, nscns), nscns);
  set_32 (hdr + offsetof (struct external_filehdr_bigobj, symptr),
	  symtab_offset);
  set_32 (hdr + offsetof (struct external_filehdr_bigobj, nsyms), nsyms);

  return simple_object_internal_write (descriptor, 0, hdrbuf,
				       sizeof (struct external_filehdr_bigobj),
				       errmsg, err);
}

/* Write out a COFF filehdr.  */

static int
simple_object_coff_write_filehdr (simple_object_write *sobj, int descriptor,
				  unsigned int nscns, size_t symtab_offset,
				  unsigned int nsyms, const char **errmsg,
				  int *err)
{
  struct simple_object_coff_attributes *attrs =
    (struct simple_object_coff_attributes *) sobj->data;
  unsigned char hdrbuf[sizeof (struct external_filehdr)];
  unsigned char *hdr;
  void (*set_16) (unsigned char *, unsigned short);
  void (*set_32) (unsigned char *, unsigned int);

  hdr = &hdrbuf[0];

  set_16 = (attrs->is_big_endian
	    ? simple_object_set_big_16
	    : simple_object_set_little_16);
  set_32 = (attrs->is_big_endian
	    ? simple_object_set_big_32
	    : simple_object_set_little_32);

  memset (hdr, 0, sizeof (struct external_filehdr));

  set_16 (hdr + offsetof (struct external_filehdr, f_magic), attrs->magic);
  set_16 (hdr + offsetof (struct external_filehdr, f_nscns), nscns);
  /* f_timdat left as zero.  */
  set_32 (hdr + offsetof (struct external_filehdr, f_symptr), symtab_offset);
  set_32 (hdr + offsetof (struct external_filehdr, f_nsyms), nsyms);
  /* f_opthdr left as zero.  */
  set_16 (hdr + offsetof (struct external_filehdr, f_flags), attrs->flags);

  return simple_object_internal_write (descriptor, 0, hdrbuf,
				       sizeof (struct external_filehdr),
				       errmsg, err);
}

/* Write out a COFF section header.  */

static int
simple_object_coff_write_scnhdr (simple_object_write *sobj, int descriptor,
				 const char *name, size_t *name_offset,
				 off_t scnhdr_offset, size_t scnsize,
				 off_t offset, unsigned int align,
				 const char **errmsg, int *err)
{
  struct simple_object_coff_attributes *attrs =
    (struct simple_object_coff_attributes *) sobj->data;
  void (*set_32) (unsigned char *, unsigned int);
  unsigned char hdrbuf[sizeof (struct external_scnhdr)];
  unsigned char *hdr;
  size_t namelen;
  unsigned int flags;

  set_32 = (attrs->is_big_endian
	    ? simple_object_set_big_32
	    : simple_object_set_little_32);

  memset (hdrbuf, 0, sizeof hdrbuf);
  hdr = &hdrbuf[0];

  namelen = strlen (name);
  if (namelen <= SCNNMLEN)
    strncpy ((char *) hdr + offsetof (struct external_scnhdr, s_name), name,
	     SCNNMLEN);
  else
    {
      snprintf ((char *) hdr + offsetof (struct external_scnhdr, s_name),
		SCNNMLEN, "/%lu", (unsigned long) *name_offset);
      *name_offset += namelen + 1;
    }

  /* s_paddr left as zero.  */
  /* s_vaddr left as zero.  */
  set_32 (hdr + offsetof (struct external_scnhdr, s_size), scnsize);
  set_32 (hdr + offsetof (struct external_scnhdr, s_scnptr), offset);
  /* s_relptr left as zero.  */
  /* s_lnnoptr left as zero.  */
  /* s_nreloc left as zero.  */
  /* s_nlnno left as zero.  */
  flags = (STYP_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_SHARED
	   | IMAGE_SCN_MEM_READ);
  /* PE can represent alignment up to 13.  */
  if (align > 13)
    align = 13;
  flags |= IMAGE_SCN_ALIGN_POWER_CONST(align);
  set_32 (hdr + offsetof (struct external_scnhdr, s_flags), flags);

  return simple_object_internal_write (descriptor, scnhdr_offset, hdrbuf,
				       sizeof (struct external_scnhdr),
				       errmsg, err);
}

/* Write out a complete COFF file.  */

static const char *
simple_object_coff_write_to_file (simple_object_write *sobj, int descriptor,
				  int *err)
{
  struct simple_object_coff_attributes *attrs =
    (struct simple_object_coff_attributes *) sobj->data;
  unsigned int nscns, secnum;
  simple_object_write_section *section;
  off_t scnhdr_offset;
  size_t symtab_offset;
  off_t secsym_offset;
  unsigned int nsyms;
  size_t offset;
  size_t name_offset;
  const char *errmsg;
  unsigned char strsizebuf[4];
  /* The interface doesn't give us access to the name of the input file
     yet.  We want to use its basename for the FILE symbol.  This is
     what 'gas' uses when told to assemble from stdin.  */
  const char *source_filename = "fake";
  size_t sflen;
  size_t symsize;
  void (*set_16) (unsigned char *, unsigned short);
  void (*set_32) (unsigned char *, unsigned int);

  /* Determine symbol size based on format.  */
  if (attrs->is_bigobj)
    symsize = sizeof (struct external_syment_bigobj);
  else
    symsize = sizeof (struct external_syment);

  set_16 = (attrs->is_big_endian
	    ? simple_object_set_big_16
	    : simple_object_set_little_16);
  set_32 = (attrs->is_big_endian
	    ? simple_object_set_big_32
	    : simple_object_set_little_32);

  nscns = 0;
  for (section = sobj->sections; section != NULL; section = section->next)
    ++nscns;

  if (attrs->is_bigobj)
    scnhdr_offset = sizeof (struct external_filehdr_bigobj);
  else
    scnhdr_offset = sizeof (struct external_filehdr);
  offset = scnhdr_offset + nscns * sizeof (struct external_scnhdr);
  name_offset = 4;
  for (section = sobj->sections; section != NULL; section = section->next)
    {
      size_t mask;
      size_t new_offset;
      size_t scnsize;
      struct simple_object_write_section_buffer *buffer;

      mask = (1U << section->align) - 1;
      new_offset = offset & mask;
      new_offset &= ~ mask;
      while (new_offset > offset)
	{
	  unsigned char zeroes[16];
	  size_t write;

	  memset (zeroes, 0, sizeof zeroes);
	  write = new_offset - offset;
	  if (write > sizeof zeroes)
	    write = sizeof zeroes;
	  if (!simple_object_internal_write (descriptor, offset, zeroes, write,
					     &errmsg, err))
	    return errmsg;
	}

      scnsize = 0;
      for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
	{
	  if (!simple_object_internal_write (descriptor, offset + scnsize,
					     ((const unsigned char *)
					      buffer->buffer),
					     buffer->size, &errmsg, err))
	    return errmsg;
	  scnsize += buffer->size;
	}

      if (!simple_object_coff_write_scnhdr (sobj, descriptor, section->name,
					    &name_offset, scnhdr_offset,
					    scnsize, offset, section->align,
					    &errmsg, err))
	return errmsg;

      scnhdr_offset += sizeof (struct external_scnhdr);
      offset += scnsize;
    }

  /* Symbol table is always half-word aligned.  */
  offset += (offset & 1);
  /* There is a file symbol and a section symbol per section,
     and each of these has a single auxiliary symbol following.  */
  nsyms = 2 * (nscns + 1);
  symtab_offset = offset;
  /* Advance across space reserved for symbol table to locate
     start of string table.  */
  offset += nsyms * symsize;

  /* Write out file symbol.  */
  if (attrs->is_bigobj)
    {
      union
      {
	struct external_syment_bigobj sym;
	union external_auxent_bigobj aux;
      } syms[2];

      memset (&syms[0], 0, sizeof (syms));
      strcpy ((char *)&syms[0].sym.e.e_name[0], ".file");
      set_32 (&syms[0].sym.e_scnum[0], IMAGE_SYM_DEBUG);
      set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
      syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_FILE;
      syms[0].sym.e_numaux[0] = 1;
      /* The name need not be nul-terminated if it fits into the x_fname field
	 directly, but must be if it has to be placed into the string table.  */
      sflen = strlen (source_filename);
      if (sflen <= E_FILNMLEN)
	memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
      else
	{
	  set_32 (&syms[1].aux.x_file.x_n.x_offset[0], name_offset);
	  if (!simple_object_internal_write (descriptor, offset + name_offset,
					     ((const unsigned char *)
					      source_filename),
					     sflen + 1, &errmsg, err))
	    return errmsg;
	  name_offset += strlen (source_filename) + 1;
	}
      if (!simple_object_internal_write (descriptor, symtab_offset,
					 (const unsigned char *) &syms[0],
					 sizeof (syms), &errmsg, err))
	return errmsg;

      /* Write the string table length, followed by the strings and section
	 symbols in step with each other.  */
      set_32 (strsizebuf, name_offset);
      if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
					 &errmsg, err))
	return errmsg;

      name_offset = 4;
      secsym_offset = symtab_offset + sizeof (syms);
      memset (&syms[0], 0, sizeof (syms));
      set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
      syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_STATIC;
      syms[0].sym.e_numaux[0] = 1;
      secnum = 1;

      for (section = sobj->sections; section != NULL; section = section->next)
	{
	  size_t namelen;
	  size_t scnsize;
	  struct simple_object_write_section_buffer *buffer;

	  namelen = strlen (section->name);
	  set_32 (&syms[0].sym.e_scnum[0], secnum++);
	  scnsize = 0;
	  for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
	    scnsize += buffer->size;
	  set_32 (&syms[1].aux.x_scn.x_scnlen[0], scnsize);
	  if (namelen > SCNNMLEN)
	    {
	      set_32 (&syms[0].sym.e.e.e_zeroes[0], 0);
	      set_32 (&syms[0].sym.e.e.e_offset[0], name_offset);
	      if (!simple_object_internal_write (descriptor, offset + name_offset,
						 ((const unsigned char *)
						  section->name),
						 namelen + 1, &errmsg, err))
		return errmsg;
	      name_offset += namelen + 1;
	    }
	  else
	    {
	      memcpy (&syms[0].sym.e.e_name[0], section->name,
		      strlen (section->name));
	      memset (&syms[0].sym.e.e_name[strlen (section->name)], 0,
		      E_SYMNMLEN - strlen (section->name));
	    }

	  if (!simple_object_internal_write (descriptor, secsym_offset,
					     (const unsigned char *) &syms[0],
					     sizeof (syms), &errmsg, err))
	    return errmsg;
	  secsym_offset += sizeof (syms);
	}
    }
  else
    {
      /* Regular COFF.  */
      union
      {
	struct external_syment sym;
	union external_auxent aux;
      } syms[2];

      memset (&syms[0], 0, sizeof (syms));
      strcpy ((char *)&syms[0].sym.e.e_name[0], ".file");
      set_16 (&syms[0].sym.e_scnum[0], IMAGE_SYM_DEBUG);
      set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
      syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_FILE;
      syms[0].sym.e_numaux[0] = 1;
      /* The name need not be nul-terminated if it fits into the x_fname field
	 directly, but must be if it has to be placed into the string table.  */
      sflen = strlen (source_filename);
      if (sflen <= E_FILNMLEN)
	memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
      else
	{
	  set_32 (&syms[1].aux.x_file.x_n.x_offset[0], name_offset);
	  if (!simple_object_internal_write (descriptor, offset + name_offset,
					     ((const unsigned char *)
					      source_filename),
					     sflen + 1, &errmsg, err))
	    return errmsg;
	  name_offset += strlen (source_filename) + 1;
	}
      if (!simple_object_internal_write (descriptor, symtab_offset,
					 (const unsigned char *) &syms[0],
					 sizeof (syms), &errmsg, err))
	return errmsg;

      /* Write the string table length, followed by the strings and section
	 symbols in step with each other.  */
      set_32 (strsizebuf, name_offset);
      if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
					 &errmsg, err))
	return errmsg;

      name_offset = 4;
      secsym_offset = symtab_offset + sizeof (syms);
      memset (&syms[0], 0, sizeof (syms));
      set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
      syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_STATIC;
      syms[0].sym.e_numaux[0] = 1;
      secnum = 1;

      for (section = sobj->sections; section != NULL; section = section->next)
	{
	  size_t namelen;
	  size_t scnsize;
	  struct simple_object_write_section_buffer *buffer;

	  namelen = strlen (section->name);
	  set_16 (&syms[0].sym.e_scnum[0], secnum++);
	  scnsize = 0;
	  for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
	    scnsize += buffer->size;
	  set_32 (&syms[1].aux.x_scn.x_scnlen[0], scnsize);
	  if (namelen > SCNNMLEN)
	    {
	      set_32 (&syms[0].sym.e.e.e_zeroes[0], 0);
	      set_32 (&syms[0].sym.e.e.e_offset[0], name_offset);
	      if (!simple_object_internal_write (descriptor, offset + name_offset,
						 ((const unsigned char *)
						  section->name),
						 namelen + 1, &errmsg, err))
		return errmsg;
	      name_offset += namelen + 1;
	    }
	  else
	    {
	      memcpy (&syms[0].sym.e.e_name[0], section->name,
		      strlen (section->name));
	      memset (&syms[0].sym.e.e_name[strlen (section->name)], 0,
		      E_SYMNMLEN - strlen (section->name));
	    }

	  if (!simple_object_internal_write (descriptor, secsym_offset,
					     (const unsigned char *) &syms[0],
					     sizeof (syms), &errmsg, err))
	    return errmsg;
	  secsym_offset += sizeof (syms);
	}
    }

  if (attrs->is_bigobj)
    {
      if (!simple_object_coff_write_filehdr_bigobj (sobj, descriptor, nscns,
						     symtab_offset, nsyms,
						     &errmsg, err))
	return errmsg;
    }
  else
    {
      if (!simple_object_coff_write_filehdr (sobj, descriptor, nscns,
					     symtab_offset, nsyms, &errmsg, err))
	return errmsg;
    }

  return NULL;
}

/* Release the private data for an simple_object_write structure.  */

static void
simple_object_coff_release_write (void *data)
{
  XDELETE (data);
}

/* The COFF functions.  */

const struct simple_object_functions simple_object_coff_functions =
{
  simple_object_coff_match,
  simple_object_coff_find_sections,
  simple_object_coff_fetch_attributes,
  simple_object_coff_release_read,
  simple_object_coff_attributes_merge,
  simple_object_coff_release_attributes,
  simple_object_coff_start_write,
  simple_object_coff_write_to_file,
  simple_object_coff_release_write,
  NULL
};