aboutsummaryrefslogtreecommitdiff
path: root/libiberty/simple-object-mach-o.c
blob: bbbbd09de5809257344157e40cb64cfd7c8f3dba (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
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
/* simple-object-mach-o.c -- routines to manipulate Mach-O object files.
   Copyright 2010 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 <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"

/* Mach-O structures and constants.  */

/* Mach-O header (32-bit version).  */

struct mach_o_header_32
{
  unsigned char magic[4];	/* Magic number.  */
  unsigned char cputype[4];	/* CPU that this object is for.  */
  unsigned char cpusubtype[4];	/* CPU subtype.  */
  unsigned char filetype[4];	/* Type of file.  */
  unsigned char ncmds[4];	/* Number of load commands.  */
  unsigned char sizeofcmds[4];	/* Total size of load commands.  */
  unsigned char flags[4];	/* Flags for special featues.  */
};

/* Mach-O header (64-bit version).  */

struct mach_o_header_64
{
  unsigned char magic[4];	/* Magic number.  */
  unsigned char cputype[4];	/* CPU that this object is for.  */
  unsigned char cpusubtype[4];	/* CPU subtype.  */
  unsigned char filetype[4];	/* Type of file.  */
  unsigned char ncmds[4];	/* Number of load commands.  */
  unsigned char sizeofcmds[4];	/* Total size of load commands.  */
  unsigned char flags[4];	/* Flags for special featues.  */
  unsigned char reserved[4];	/* Reserved.  Duh.  */
};

/* For magic field in header.  */

#define MACH_O_MH_MAGIC			0xfeedface
#define MACH_O_MH_MAGIC_64		0xfeedfacf

/* For filetype field in header.  */

#define MACH_O_MH_OBJECT		0x01

/* A Mach-O file is a list of load commands.  This is the header of a
   load command.  */

struct mach_o_load_command
{
  unsigned char cmd[4];		/* The type of load command.  */
  unsigned char cmdsize[4];	/* Size in bytes of entire command.  */
};

/* For cmd field in load command.   */

#define MACH_O_LC_SEGMENT		0x01
#define MACH_O_LC_SEGMENT_64		0x19

/* LC_SEGMENT load command.  */

struct mach_o_segment_command_32
{
  unsigned char cmd[4];		/* The type of load command (LC_SEGMENT).  */
  unsigned char cmdsize[4];	/* Size in bytes of entire command.  */
  unsigned char segname[16];	/* Name of this segment.  */
  unsigned char vmaddr[4];	/* Virtual memory address of this segment.  */
  unsigned char vmsize[4];	/* Size there, in bytes.  */
  unsigned char fileoff[4];	/* Offset in bytes of the data to be mapped.  */
  unsigned char filesize[4];	/* Size in bytes on disk.  */
  unsigned char maxprot[4];	/* Maximum permitted vmem protection.  */
  unsigned char initprot[4];	/* Initial vmem protection.  */
  unsigned char nsects[4];	/* Number of sections in this segment.  */
  unsigned char flags[4];	/* Flags that affect the loading.  */
};

/* LC_SEGMENT_64 load command.  */

struct mach_o_segment_command_64
{
  unsigned char cmd[4];		/* The type of load command (LC_SEGMENT_64).  */
  unsigned char cmdsize[4];	/* Size in bytes of entire command.  */
  unsigned char segname[16];	/* Name of this segment.  */
  unsigned char vmaddr[8];	/* Virtual memory address of this segment.  */
  unsigned char vmsize[8];	/* Size there, in bytes.  */
  unsigned char fileoff[8];	/* Offset in bytes of the data to be mapped.  */
  unsigned char filesize[8];	/* Size in bytes on disk.  */
  unsigned char maxprot[4];	/* Maximum permitted vmem protection.  */
  unsigned char initprot[4];	/* Initial vmem protection.  */
  unsigned char nsects[4];	/* Number of sections in this segment.  */
  unsigned char flags[4];	/* Flags that affect the loading.  */
};

/* 32-bit section header.  */

struct mach_o_section_32
{
  unsigned char sectname[16];	/* Section name.  */
  unsigned char segname[16];	/* Segment that the section belongs to.  */
  unsigned char addr[4];	/* Address of this section in memory.  */
  unsigned char size[4];	/* Size in bytes of this section.  */
  unsigned char offset[4];	/* File offset of this section.  */
  unsigned char align[4];	/* log2 of this section's alignment.  */
  unsigned char reloff[4];	/* File offset of this section's relocs.  */
  unsigned char nreloc[4];	/* Number of relocs for this section.  */
  unsigned char flags[4];	/* Section flags/attributes.  */
  unsigned char reserved1[4];
  unsigned char reserved2[4];
};

/* 64-bit section header.  */

struct mach_o_section_64
{
  unsigned char sectname[16];	/* Section name.  */
  unsigned char segname[16];	/* Segment that the section belongs to.  */
  unsigned char addr[8];	/* Address of this section in memory.  */
  unsigned char size[8];	/* Size in bytes of this section.  */
  unsigned char offset[4];	/* File offset of this section.  */
  unsigned char align[4];	/* log2 of this section's alignment.  */
  unsigned char reloff[4];	/* File offset of this section's relocs.  */
  unsigned char nreloc[4];	/* Number of relocs for this section.  */
  unsigned char flags[4];	/* Section flags/attributes.  */
  unsigned char reserved1[4];
  unsigned char reserved2[4];
  unsigned char reserved3[4];
};

/* Flags for Mach-O sections.  */

#define MACH_O_S_ATTR_DEBUG			0x02000000

/* The length of a segment or section name.  */

#define MACH_O_NAME_LEN (16)

/* A GNU specific extension for long section names.  */

#define GNU_SECTION_NAMES "__section_names"

/* Private data for an simple_object_read.  */

struct simple_object_mach_o_read
{
  /* User specified segment name.  */
  char *segment_name;
  /* Magic number.  */
  unsigned int magic;
  /* Whether this file is big-endian.  */
  int is_big_endian;
  /* CPU type from header.  */
  unsigned int cputype;
  /* CPU subtype from header.  */
  unsigned int cpusubtype;
  /* Number of commands, from header.  */
  unsigned int ncmds;
  /* Flags from header.  */
  unsigned int flags;
  /* Reserved field from header, only used on 64-bit.  */
  unsigned int reserved;
};

/* Private data for an simple_object_attributes.  */

struct simple_object_mach_o_attributes
{
  /* Magic number.  */
  unsigned int magic;
  /* Whether this file is big-endian.  */
  int is_big_endian;
  /* CPU type from header.  */
  unsigned int cputype;
  /* CPU subtype from header.  */
  unsigned int cpusubtype;
  /* Flags from header.  */
  unsigned int flags;
  /* Reserved field from header, only used on 64-bit.  */
  unsigned int reserved;
};

/* See if we have a Mach-O file.  */

static void *
simple_object_mach_o_match (
    unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
    int descriptor,
    off_t offset,
    const char *segment_name,
    const char **errmsg,
    int *err)
{
  unsigned int magic;
  int is_big_endian;
  unsigned int (*fetch_32) (const unsigned char *);
  unsigned int filetype;
  struct simple_object_mach_o_read *omr;
  unsigned char buf[sizeof (struct mach_o_header_64)];
  unsigned char *b;

  magic = simple_object_fetch_big_32 (header);
  if (magic == MACH_O_MH_MAGIC || magic == MACH_O_MH_MAGIC_64)
    is_big_endian = 1;
  else
    {
      magic = simple_object_fetch_little_32 (header);
      if (magic == MACH_O_MH_MAGIC || magic == MACH_O_MH_MAGIC_64)
	is_big_endian = 0;
      else
	{
	  *errmsg = NULL;
	  *err = 0;
	  return NULL;
	}
    }

#ifndef UNSIGNED_64BIT_TYPE
  if (magic == MACH_O_MH_MAGIC_64)
    {
      *errmsg = "64-bit Mach-O objects not supported";
      *err = 0;
      return NULL;
    }
#endif

  /* We require the user to provide a segment name.  This is
     unfortunate but I don't see any good choices here.  */

  if (segment_name == NULL)
    {
      *errmsg = "Mach-O file found but no segment name specified";
      *err = 0;
      return NULL;
    }

  if (strlen (segment_name) > MACH_O_NAME_LEN)
    {
      *errmsg = "Mach-O segment name too long";
      *err = 0;
      return NULL;
    }

  /* The 32-bit and 64-bit headers are similar enough that we can use
     the same code.  */

  fetch_32 = (is_big_endian
	      ? simple_object_fetch_big_32
	      : simple_object_fetch_little_32);

  if (!simple_object_internal_read (descriptor, offset, buf,
				    (magic == MACH_O_MH_MAGIC
				     ? sizeof (struct mach_o_header_32)
				     : sizeof (struct mach_o_header_64)),
				    errmsg, err))
    return NULL;

  b = &buf[0];

  filetype = (*fetch_32) (b + offsetof (struct mach_o_header_32, filetype));
  if (filetype != MACH_O_MH_OBJECT)
    {
      *errmsg = "Mach-O file is not object file";
      *err = 0;
      return NULL;
    }

  omr = XNEW (struct simple_object_mach_o_read);
  omr->segment_name = xstrdup (segment_name);
  omr->magic = magic;
  omr->is_big_endian = is_big_endian;
  omr->cputype = (*fetch_32) (b + offsetof (struct mach_o_header_32, cputype));
  omr->cpusubtype = (*fetch_32) (b
				 + offsetof (struct mach_o_header_32,
					     cpusubtype));
  omr->ncmds = (*fetch_32) (b + offsetof (struct mach_o_header_32, ncmds));
  omr->flags = (*fetch_32) (b + offsetof (struct mach_o_header_32, flags));
  if (magic == MACH_O_MH_MAGIC)
    omr->reserved = 0;
  else
    omr->reserved = (*fetch_32) (b
				 + offsetof (struct mach_o_header_64,
					     reserved));

  return (void *) omr;
}

/* Get the file offset and size from a section header.  */

static void
simple_object_mach_o_section_info (int is_big_endian, int is_32,
				   const unsigned char *sechdr, off_t *offset,
				   size_t *size)
{
  unsigned int (*fetch_32) (const unsigned char *);
  ulong_type (*fetch_64) (const unsigned char *);

  fetch_32 = (is_big_endian
	      ? simple_object_fetch_big_32
	      : simple_object_fetch_little_32);

  fetch_64 = NULL;
#ifdef UNSIGNED_64BIT_TYPE
  fetch_64 = (is_big_endian
	      ? simple_object_fetch_big_64
	      : simple_object_fetch_little_64);
#endif

  if (is_32)
    {
      *offset = fetch_32 (sechdr
			  + offsetof (struct mach_o_section_32, offset));
      *size = fetch_32 (sechdr
			+ offsetof (struct mach_o_section_32, size));
    }
  else
    {
      *offset = fetch_32 (sechdr
			  + offsetof (struct mach_o_section_64, offset));
      *size = fetch_64 (sechdr
			+ offsetof (struct mach_o_section_64, size));
    }
}

/* Handle a segment in a Mach-O file.  Return 1 if we should continue,
   0 if the caller should return.  */

static int
simple_object_mach_o_segment (simple_object_read *sobj, off_t offset,
			      const unsigned char *segbuf,
			      int (*pfn) (void *, const char *, off_t offset,
					  off_t length),
			      void *data,
			      const char **errmsg, int *err)
{
  struct simple_object_mach_o_read *omr =
    (struct simple_object_mach_o_read *) sobj->data;
  unsigned int (*fetch_32) (const unsigned char *);
  int is_32;
  size_t seghdrsize;
  size_t sechdrsize;
  size_t segname_offset;
  size_t sectname_offset;
  unsigned int nsects;
  unsigned char *secdata;
  unsigned int i;
  unsigned int strtab_index;
  char *strtab;
  size_t strtab_size;

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

  is_32 = omr->magic == MACH_O_MH_MAGIC;

  if (is_32)
    {
      seghdrsize = sizeof (struct mach_o_segment_command_32);
      sechdrsize = sizeof (struct mach_o_section_32);
      segname_offset = offsetof (struct mach_o_section_32, segname);
      sectname_offset = offsetof (struct mach_o_section_32, sectname);
      nsects = (*fetch_32) (segbuf
			    + offsetof (struct mach_o_segment_command_32,
					nsects));
    }
  else
    {
      seghdrsize = sizeof (struct mach_o_segment_command_64);
      sechdrsize = sizeof (struct mach_o_section_64);
      segname_offset = offsetof (struct mach_o_section_64, segname);
      sectname_offset = offsetof (struct mach_o_section_64, sectname);
      nsects = (*fetch_32) (segbuf
			    + offsetof (struct mach_o_segment_command_64,
					nsects));
    }

  secdata = XNEWVEC (unsigned char, nsects * sechdrsize);
  if (!simple_object_internal_read (sobj->descriptor, offset + seghdrsize,
				    secdata, nsects * sechdrsize, errmsg, err))
    {
      XDELETEVEC (secdata);
      return 0;
    }

  /* Scan for a __section_names section.  This is in effect a GNU
     extension that permits section names longer than 16 chars.  */

  for (i = 0; i < nsects; ++i)
    {
      size_t nameoff;

      nameoff = i * sechdrsize + segname_offset;
      if (strcmp ((char *) secdata + nameoff, omr->segment_name) != 0)
	continue;
      nameoff = i * sechdrsize + sectname_offset;
      if (strcmp ((char *) secdata + nameoff, GNU_SECTION_NAMES) == 0)
	break;
    }

  strtab_index = i;
  if (strtab_index >= nsects)
    {
      strtab = NULL;
      strtab_size = 0;
    }
  else
    {
      off_t strtab_offset;

      simple_object_mach_o_section_info (omr->is_big_endian, is_32,
					 secdata + strtab_index * sechdrsize,
					 &strtab_offset, &strtab_size);
      strtab = XNEWVEC (char, strtab_size);
      if (!simple_object_internal_read (sobj->descriptor,
					sobj->offset + strtab_offset,
					(unsigned char *) strtab, strtab_size,
					errmsg, err))
	{
	  XDELETEVEC (strtab);
	  XDELETEVEC (secdata);
	  return 0;
	}
    }

  /* Process the sections.  */

  for (i = 0; i < nsects; ++i)
    {
      const unsigned char *sechdr;
      char namebuf[MACH_O_NAME_LEN + 1];
      char *name;
      off_t secoffset;
      size_t secsize;

      if (i == strtab_index)
	continue;

      sechdr = secdata + i * sechdrsize;

      if (strcmp ((char *) sechdr + segname_offset, omr->segment_name) != 0)
	continue;

      memcpy (namebuf, sechdr + sectname_offset, MACH_O_NAME_LEN);
      namebuf[MACH_O_NAME_LEN] = '\0';

      name = &namebuf[0];
      if (strtab != NULL && name[0] == '_' && name[1] == '_')
	{
	  unsigned long stringoffset;

	  if (sscanf (name + 2, "%08lX", &stringoffset) == 1)
	    {
	      if (stringoffset >= strtab_size)
		{
		  *errmsg = "section name offset out of range";
		  *err = 0;
		  XDELETEVEC (strtab);
		  XDELETEVEC (secdata);
		  return 0;
		}

	      name = strtab + stringoffset;
	    }
	}

      simple_object_mach_o_section_info (omr->is_big_endian, is_32, sechdr,
					 &secoffset, &secsize);

      if (!(*pfn) (data, name, secoffset, secsize))
	{
	  *errmsg = NULL;
	  *err = 0;
	  XDELETEVEC (strtab);
	  XDELETEVEC (secdata);
	  return 0;
	}
    }

  XDELETEVEC (strtab);
  XDELETEVEC (secdata);

  return 1;
}

/* Find all sections in a Mach-O file.  */

static const char *
simple_object_mach_o_find_sections (simple_object_read *sobj,
				    int (*pfn) (void *, const char *,
						off_t offset, off_t length),
				    void *data,
				    int *err)
{
  struct simple_object_mach_o_read *omr =
    (struct simple_object_mach_o_read *) sobj->data;
  off_t offset;
  size_t seghdrsize;
  unsigned int (*fetch_32) (const unsigned char *);
  const char *errmsg;
  unsigned int i;

  if (omr->magic == MACH_O_MH_MAGIC)
    {
      offset = sizeof (struct mach_o_header_32);
      seghdrsize = sizeof (struct mach_o_segment_command_32);
    }
  else
    {
      offset = sizeof (struct mach_o_header_64);
      seghdrsize = sizeof (struct mach_o_segment_command_64);
    }

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

  for (i = 0; i < omr->ncmds; ++i)
    {
      unsigned char loadbuf[sizeof (struct mach_o_load_command)];
      unsigned int cmd;
      unsigned int cmdsize;

      if (!simple_object_internal_read (sobj->descriptor,
					sobj->offset + offset,
					loadbuf,
					sizeof (struct mach_o_load_command),
					&errmsg, err))
	return errmsg;

      cmd = (*fetch_32) (loadbuf + offsetof (struct mach_o_load_command, cmd));
      cmdsize = (*fetch_32) (loadbuf
			     + offsetof (struct mach_o_load_command, cmdsize));

      if (cmd == MACH_O_LC_SEGMENT || cmd == MACH_O_LC_SEGMENT_64)
	{
	  unsigned char segbuf[sizeof (struct mach_o_segment_command_64)];
	  int r;

	  if (!simple_object_internal_read (sobj->descriptor,
					    sobj->offset + offset,
					    segbuf, seghdrsize, &errmsg, err))
	    return errmsg;

	  r = simple_object_mach_o_segment (sobj, offset, segbuf, pfn,
					    data, &errmsg, err);
	  if (!r)
	    return errmsg;
	}

      offset += cmdsize;
    }

  return NULL;
}

/* Fetch the attributes for an simple_object_read.  */

static void *
simple_object_mach_o_fetch_attributes (simple_object_read *sobj,
				       const char **errmsg ATTRIBUTE_UNUSED,
				       int *err ATTRIBUTE_UNUSED)
{
  struct simple_object_mach_o_read *omr =
    (struct simple_object_mach_o_read *) sobj->data;
  struct simple_object_mach_o_attributes *ret;

  ret = XNEW (struct simple_object_mach_o_attributes);
  ret->magic = omr->magic;
  ret->is_big_endian = omr->is_big_endian;
  ret->cputype = omr->cputype;
  ret->cpusubtype = omr->cpusubtype;
  ret->flags = omr->flags;
  ret->reserved = omr->reserved;
  return ret;
}

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

static void
simple_object_mach_o_release_read (void *data)
{
  struct simple_object_mach_o_read *omr =
    (struct simple_object_mach_o_read *) data;

  free (omr->segment_name);
  XDELETE (omr);
}

/* Compare two attributes structures.  */

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

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

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

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

/* Prepare to write out a file.  */

static void *
simple_object_mach_o_start_write (void *attributes_data,
				  const char **errmsg ATTRIBUTE_UNUSED,
				  int *err ATTRIBUTE_UNUSED)
{
  struct simple_object_mach_o_attributes *attrs =
    (struct simple_object_mach_o_attributes *) attributes_data;
  struct simple_object_mach_o_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_mach_o_attributes);
  *ret = *attrs;
  return ret;
}

/* Write out the header of a Mach-O file.  */

static int
simple_object_mach_o_write_header (simple_object_write *sobj, int descriptor,
				   size_t nsects, const char **errmsg,
				   int *err)
{
  struct simple_object_mach_o_attributes *attrs =
    (struct simple_object_mach_o_attributes *) sobj->data;
  void (*set_32) (unsigned char *, unsigned int);
  unsigned char hdrbuf[sizeof (struct mach_o_header_64)];
  unsigned char *hdr;
  size_t wrsize;

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

  memset (hdrbuf, 0, sizeof hdrbuf);

  /* The 32-bit and 64-bit headers start out the same.  */

  hdr = &hdrbuf[0];
  set_32 (hdr + offsetof (struct mach_o_header_32, magic), attrs->magic);
  set_32 (hdr + offsetof (struct mach_o_header_32, cputype), attrs->cputype);
  set_32 (hdr + offsetof (struct mach_o_header_32, cpusubtype),
	  attrs->cpusubtype);
  set_32 (hdr + offsetof (struct mach_o_header_32, filetype), MACH_O_MH_OBJECT);
  set_32 (hdr + offsetof (struct mach_o_header_32, ncmds), 1);
  set_32 (hdr + offsetof (struct mach_o_header_32, flags), attrs->flags);
  if (attrs->magic == MACH_O_MH_MAGIC)
    {
      wrsize = sizeof (struct mach_o_header_32);
      set_32 (hdr + offsetof (struct mach_o_header_32, sizeofcmds),
	      (sizeof (struct mach_o_segment_command_32)
	       + nsects * sizeof (struct mach_o_section_32)));
    }
  else
    {
      set_32 (hdr + offsetof (struct mach_o_header_64, sizeofcmds),
	      (sizeof (struct mach_o_segment_command_64)
	       + nsects * sizeof (struct mach_o_section_64)));
      set_32 (hdr + offsetof (struct mach_o_header_64, reserved),
	      attrs->reserved);
      wrsize = sizeof (struct mach_o_header_64);
    }

  return simple_object_internal_write (descriptor, 0, hdrbuf, wrsize,
				       errmsg, err);
}

/* Write a Mach-O section header.  */

static int
simple_object_mach_o_write_section_header (simple_object_write *sobj,
					   int descriptor,
					   size_t sechdr_offset,
					   const char *name, size_t secaddr,
					   size_t secsize, size_t offset,
					   unsigned int align,
					   const char **errmsg, int *err)
{
  struct simple_object_mach_o_attributes *attrs =
    (struct simple_object_mach_o_attributes *) sobj->data;
  void (*set_32) (unsigned char *, unsigned int);
  unsigned char hdrbuf[sizeof (struct mach_o_section_64)];
  unsigned char *hdr;
  size_t sechdrsize;

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

  memset (hdrbuf, 0, sizeof hdrbuf);

  hdr = &hdrbuf[0];
  if (attrs->magic == MACH_O_MH_MAGIC)
    {
      strncpy ((char *) hdr + offsetof (struct mach_o_section_32, sectname),
	       name, MACH_O_NAME_LEN);
      strncpy ((char *) hdr + offsetof (struct mach_o_section_32, segname),
	       sobj->segment_name, MACH_O_NAME_LEN);
      set_32 (hdr + offsetof (struct mach_o_section_32, addr), secaddr);
      set_32 (hdr + offsetof (struct mach_o_section_32, size), secsize);
      set_32 (hdr + offsetof (struct mach_o_section_32, offset), offset);
      set_32 (hdr + offsetof (struct mach_o_section_32, align), align);
      /* reloff left as zero.  */
      /* nreloc left as zero.  */
      set_32 (hdr + offsetof (struct mach_o_section_32, flags),
	      MACH_O_S_ATTR_DEBUG);
      /* reserved1 left as zero.  */
      /* reserved2 left as zero.  */
      sechdrsize = sizeof (struct mach_o_section_32);
    }
  else
    {
#ifdef UNSIGNED_64BIT_TYPE
      void (*set_64) (unsigned char *, ulong_type);

      set_64 = (attrs->is_big_endian
		? simple_object_set_big_64
		: simple_object_set_little_64);

      strncpy ((char *) hdr + offsetof (struct mach_o_section_64, sectname),
	       name, MACH_O_NAME_LEN);
      strncpy ((char *) hdr + offsetof (struct mach_o_section_64, segname),
	       sobj->segment_name, MACH_O_NAME_LEN);
      set_64 (hdr + offsetof (struct mach_o_section_64, addr), secaddr);
      set_64 (hdr + offsetof (struct mach_o_section_64, size), secsize);
      set_32 (hdr + offsetof (struct mach_o_section_64, offset), offset);
      set_32 (hdr + offsetof (struct mach_o_section_64, align), align);
      /* reloff left as zero.  */
      /* nreloc left as zero.  */
      set_32 (hdr + offsetof (struct mach_o_section_64, flags),
	      MACH_O_S_ATTR_DEBUG);
      /* reserved1 left as zero.  */
      /* reserved2 left as zero.  */
      /* reserved3 left as zero.  */
#endif
      sechdrsize = sizeof (struct mach_o_section_64);
    }

  return simple_object_internal_write (descriptor, sechdr_offset, hdr,
				       sechdrsize, errmsg, err);
}

/* Write out the single segment and the sections of a Mach-O file.  */

static int
simple_object_mach_o_write_segment (simple_object_write *sobj, int descriptor,
				    size_t nsects, const char **errmsg,
				    int *err)
{
  struct simple_object_mach_o_attributes *attrs =
    (struct simple_object_mach_o_attributes *) sobj->data;
  void (*set_32) (unsigned char *, unsigned int);
  size_t hdrsize;
  size_t seghdrsize;
  size_t sechdrsize;
  size_t cmdsize;
  size_t offset;
  size_t sechdr_offset;
  size_t secaddr;
  unsigned int name_offset;
  simple_object_write_section *section;
  unsigned char hdrbuf[sizeof (struct mach_o_segment_command_64)];
  unsigned char *hdr;

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

  /* Write out the sections first.  */

  if (attrs->magic == MACH_O_MH_MAGIC)
    {
      hdrsize = sizeof (struct mach_o_header_32);
      seghdrsize = sizeof (struct mach_o_segment_command_32);
      sechdrsize = sizeof (struct mach_o_section_32);
    }
  else
    {
      hdrsize = sizeof (struct mach_o_header_64);
      seghdrsize = sizeof (struct mach_o_segment_command_64);
      sechdrsize = sizeof (struct mach_o_section_64);
    }

  sechdr_offset = hdrsize + seghdrsize;
  cmdsize = seghdrsize + nsects * sechdrsize;
  offset = hdrsize + cmdsize;
  name_offset = 0;
  secaddr = 0;

  for (section = sobj->sections; section != NULL; section = section->next)
    {
      size_t mask;
      size_t new_offset;
      size_t secsize;
      struct simple_object_write_section_buffer *buffer;
      char namebuf[MACH_O_NAME_LEN + 1];

      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 0;
	  offset += write;
	}

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

      snprintf (namebuf, sizeof namebuf, "__%08X", name_offset);
      if (!simple_object_mach_o_write_section_header (sobj, descriptor,
						      sechdr_offset, namebuf,
						      secaddr, secsize, offset,
						      section->align,
						      errmsg, err))
	return 0;

      sechdr_offset += sechdrsize;
      offset += secsize;
      name_offset += strlen (section->name) + 1;
      secaddr += secsize;
    }

  /* Write out the section names.  */

  if (!simple_object_mach_o_write_section_header (sobj, descriptor,
						  sechdr_offset,
						  GNU_SECTION_NAMES, secaddr,
						  name_offset, offset, 0,
						  errmsg, err))
    return 0;

  for (section = sobj->sections; section != NULL; section = section->next)
    {
      size_t namelen;

      namelen = strlen (section->name) + 1;
      if (!simple_object_internal_write (descriptor, offset,
					 (const unsigned char *) section->name,
					 namelen, errmsg, err))
	return 0;
      offset += namelen;
    }

  /* Write out the segment header.  */

  memset (hdrbuf, 0, sizeof hdrbuf);

  hdr = &hdrbuf[0];
  if (attrs->magic == MACH_O_MH_MAGIC)
    {
      set_32 (hdr + offsetof (struct mach_o_segment_command_32, cmd),
	      MACH_O_LC_SEGMENT);
      set_32 (hdr + offsetof (struct mach_o_segment_command_32, cmdsize),
	      cmdsize);
      strncpy (((char *) hdr
		+ offsetof (struct mach_o_segment_command_32, segname)),
	       sobj->segment_name, MACH_O_NAME_LEN);
      /* vmaddr left as zero.  */
      /* vmsize left as zero.  */
      set_32 (hdr + offsetof (struct mach_o_segment_command_32, fileoff),
	      hdrsize + cmdsize);
      set_32 (hdr + offsetof (struct mach_o_segment_command_32, filesize),
	      offset - (hdrsize + cmdsize));
      /* maxprot left as zero.  */
      /* initprot left as zero.  */
      set_32 (hdr + offsetof (struct mach_o_segment_command_32, nsects),
	      nsects);
      /* flags left as zero.  */
    }
  else
    {
#ifdef UNSIGNED_64BIT_TYPE
      void (*set_64) (unsigned char *, ulong_type);

      set_64 = (attrs->is_big_endian
		? simple_object_set_big_64
		: simple_object_set_little_64);

      set_32 (hdr + offsetof (struct mach_o_segment_command_64, cmd),
	      MACH_O_LC_SEGMENT);
      set_32 (hdr + offsetof (struct mach_o_segment_command_64, cmdsize),
	      cmdsize);
      strncpy (((char *) hdr
		+ offsetof (struct mach_o_segment_command_64, segname)),
	       sobj->segment_name, MACH_O_NAME_LEN);
      /* vmaddr left as zero.  */
      /* vmsize left as zero.  */
      set_64 (hdr + offsetof (struct mach_o_segment_command_64, fileoff),
	      hdrsize + cmdsize);
      set_64 (hdr + offsetof (struct mach_o_segment_command_64, filesize),
	      offset - (hdrsize + cmdsize));
      /* maxprot left as zero.  */
      /* initprot left as zero.  */
      set_32 (hdr + offsetof (struct mach_o_segment_command_64, nsects),
	      nsects);
      /* flags left as zero.  */
#endif
    }

  return simple_object_internal_write (descriptor, hdrsize, hdr, seghdrsize,
				       errmsg, err);
}

/* Write out a complete Mach-O file.  */

static const char *
simple_object_mach_o_write_to_file (simple_object_write *sobj, int descriptor,
				    int *err)
{
  size_t nsects;
  simple_object_write_section *section;
  const char *errmsg;

  /* Start at 1 for symbol_names section.  */
  nsects = 1;
  for (section = sobj->sections; section != NULL; section = section->next)
    ++nsects;

  if (!simple_object_mach_o_write_header (sobj, descriptor, nsects,
					  &errmsg, err))
    return errmsg;

  if (!simple_object_mach_o_write_segment (sobj, descriptor, nsects,
					   &errmsg, err))
    return errmsg;

  return NULL;
}

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

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

/* The Mach-O functions.  */

const struct simple_object_functions simple_object_mach_o_functions =
{
  simple_object_mach_o_match,
  simple_object_mach_o_find_sections,
  simple_object_mach_o_fetch_attributes,
  simple_object_mach_o_release_read,
  simple_object_mach_o_attributes_merge,
  simple_object_mach_o_release_attributes,
  simple_object_mach_o_start_write,
  simple_object_mach_o_write_to_file,
  simple_object_mach_o_release_write
};