1 /* outmacho.c output routines for the Netwide Assembler to produce
2 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
10 /* Most of this file is, like Mach-O itself, based on a.out. For more
11 * guidelines see outaout.c. */
26 /* Mach-O in-file header structure sizes */
27 #define MACHO_HEADER_SIZE (28)
28 #define MACHO_SEGCMD_SIZE (56)
29 #define MACHO_SECTCMD_SIZE (68)
30 #define MACHO_SYMCMD_SIZE (24)
31 #define MACHO_NLIST_SIZE (12)
32 #define MACHO_RELINFO_SIZE (8)
34 /* Mach-O file header values */
35 #define MH_MAGIC (0xfeedface)
36 #define CPU_TYPE_I386 (7) /* x86 platform */
37 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
38 #define MH_OBJECT (0x1) /* object file */
40 #define LC_SEGMENT (0x1) /* segment load command */
41 #define LC_SYMTAB (0x2) /* symbol table load command */
43 #define VM_PROT_NONE (0x00)
44 #define VM_PROT_READ (0x01)
45 #define VM_PROT_WRITE (0x02)
46 #define VM_PROT_EXECUTE (0x04)
48 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
49 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
52 /* nasm internal data */
59 /* data that goes into the file */
60 char sectname
[16]; /* what this section is called */
61 char segname
[16]; /* segment this section will be in */
62 uint32_t size
; /* in-memory and -file size */
63 uint32_t nreloc
; /* relocation entry count */
64 uint32_t flags
; /* type and attributes (masked) */
67 #define SECTION_TYPE 0x000000ff /* section type mask */
69 #define S_REGULAR (0x0) /* standard section */
70 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
72 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
73 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
74 machine instructions */
75 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
77 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
81 static struct sectmap
{
87 ".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
}, {
88 ".data", "__DATA", "__data", S_REGULAR
}, {
89 ".rodata", "__DATA", "__const", S_REGULAR
}, {
90 ".bss", "__DATA", "__bss", S_ZEROFILL
}, {
94 /* nasm internal data */
97 /* data that goes into the file */
98 int32_t addr
; /* op's offset in section */
99 unsigned int snum
:24, /* contains symbol index if
100 ** ext otherwise in-file
102 pcrel
:1, /* relative relocation */
103 length
:2, /* 0=byte, 1=word, 2=int32_t */
104 ext
:1, /* external symbol referenced */
105 type
:4; /* reloc type, 0 for us */
108 #define R_ABS 0 /* absolute relocation */
109 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
110 ** highest bit == 1 */
113 /* nasm internal data */
114 struct symbol
*next
; /* next symbol in the list */
115 char *name
; /* name of this symbol */
116 int32_t initial_snum
; /* symbol number used above in
118 int32_t snum
; /* true snum for reloc */
120 /* data that goes into the file */
121 int32_t strx
; /* string table index */
122 uint8_t type
; /* symbol type */
123 uint8_t sect
; /* NO_SECT or section number */
124 int16_t desc
; /* for stab debugging, 0 for us */
125 uint32_t value
; /* offset of symbol in section */
128 /* symbol type bits */
129 #define N_EXT 0x01 /* global or external symbol */
131 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
132 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
133 #define N_SECT 0xe /* defined symbol, n_sect holds
136 #define N_TYPE 0x0e /* type bit mask */
138 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
140 /* special section number values */
141 #define NO_SECT 0 /* no section, invalid */
142 #define MAX_SECT 255 /* maximum number of sections */
144 static struct section
*sects
, **sectstail
;
145 static struct symbol
*syms
, **symstail
;
146 static uint32_t nsyms
;
148 /* These variables are set by macho_layout_symbols() to organize
149 the symbol table and string table in order the dynamic linker
150 expects. They are then used in macho_write() to put out the
151 symbols and strings in that order.
153 The order of the symbol table is:
155 defined external symbols (sorted by name)
156 undefined external symbols (sorted by name)
158 The order of the string table is:
159 strings for external symbols
160 strings for local symbols
162 static uint32_t ilocalsym
= 0;
163 static uint32_t iextdefsym
= 0;
164 static uint32_t iundefsym
= 0;
165 static uint32_t nlocalsym
;
166 static uint32_t nextdefsym
;
167 static uint32_t nundefsym
;
168 static struct symbol
**extdefsyms
= NULL
;
169 static struct symbol
**undefsyms
= NULL
;
171 static struct RAA
*extsyms
;
172 static struct SAA
*strs
;
173 static uint32_t strslen
;
175 static FILE *machofp
;
177 static evalfunc evaluate
;
179 extern struct ofmt of_macho
;
181 /* Global file information. This should be cleaned up into either
182 a structure or as function arguments. */
183 uint32_t head_ncmds
= 0;
184 uint32_t head_sizeofcmds
= 0;
185 uint32_t seg_filesize
= 0;
186 uint32_t seg_vmsize
= 0;
187 uint32_t seg_nsects
= 0;
188 uint32_t rel_padcnt
= 0;
191 #define xstrncpy(xdst, xsrc) \
192 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
193 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
194 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
196 #define align(x, y) \
197 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
199 #define alignint32_t(x) \
200 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
202 static void debug_reloc (struct reloc
*);
203 static void debug_section_relocs (struct section
*) _unused
;
205 static int exact_log2 (uint32_t align
)
209 } else if (align
& (align
-1)) {
210 return -1; /* Not a power of 2 */
213 return __builtin_ctzl (align
);
217 /* We know exactly one bit is set at this point. */
218 if (align
& 0xffff0000)
220 if (align
& 0xff00ff00)
222 if (align
& 0xf0f0f0f0)
224 if (align
& 0xcccccccc)
226 if (align
& 0xaaaaaaaa)
234 static struct section
*get_section_by_name(const char *segname
,
235 const char *sectname
)
239 for (s
= sects
; s
!= NULL
; s
= s
->next
)
240 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
246 static struct section
*get_section_by_index(const int32_t index
)
250 for (s
= sects
; s
!= NULL
; s
= s
->next
)
251 if (index
== s
->index
)
257 static int32_t get_section_index_by_name(const char *segname
,
258 const char *sectname
)
262 for (s
= sects
; s
!= NULL
; s
= s
->next
)
263 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
269 static char *get_section_name_by_index(const int32_t index
)
273 for (s
= sects
; s
!= NULL
; s
= s
->next
)
274 if (index
== s
->index
)
280 static uint8_t get_section_fileindex_by_index(const int32_t index
)
285 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
286 if (index
== s
->index
)
291 "too many sections (>255) - clipped by fileindex");
296 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
305 (void)ldef
; /* placate optimisers */
317 extsyms
= raa_init();
320 /* string table starts with a zero byte - don't ask why */
321 saa_wbytes(strs
, &zero
, sizeof(char));
325 static int macho_setinfo(enum geninfo type
, char **val
)
330 static void sect_write(struct section
*sect
,
331 const uint8_t *data
, uint32_t len
)
333 saa_wbytes(sect
->data
, data
, len
);
337 static void add_reloc(struct section
*sect
, int32_t section
,
338 int pcrel
, int bytes
)
343 /* NeXT as puts relocs in reversed order (address-wise) into the
344 ** files, so we do the same, doesn't seem to make much of a
345 ** difference either way */
346 r
= nasm_malloc(sizeof(struct reloc
));
347 r
->next
= sect
->relocs
;
350 /* the current end of the section will be the symbol's address for
351 ** now, might have to be fixed by macho_fixup_relocs() later on. make
352 ** sure we don't make the symbol scattered by setting the highest
353 ** bit by accident */
354 r
->addr
= sect
->size
& ~R_SCATTERED
;
358 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
359 r
->length
= bytes
>> 1;
361 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
364 if (section
== NO_SEG
) {
365 /* absolute local symbol if no section index given */
368 fi
= get_section_fileindex_by_index(section
);
371 /* external symbol if no section with that index known,
372 ** symbol number was saved in macho_symdef() */
373 r
->snum
= raa_read(extsyms
, section
);
376 /* local symbol in section fi */
384 static void macho_output(int32_t secto
, const void *data
, uint32_t type
,
385 int32_t section
, int32_t wrt
)
387 struct section
*s
, *sbss
;
388 int32_t realbytes
= type
& OUT_SIZMASK
;
390 uint8_t mydata
[4], *p
;
396 error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
397 /* continue to do _something_ */
400 if (secto
== NO_SEG
) {
401 if (type
!= OUT_RESERVE
)
402 error(ERR_NONFATAL
, "attempt to assemble code in "
408 s
= get_section_by_index(secto
);
411 error(ERR_WARNING
, "attempt to assemble code in"
412 " section %d: defaulting to `.text'", secto
);
413 s
= get_section_by_name("__TEXT", "__text");
415 /* should never happen */
417 error(ERR_PANIC
, "text section not found");
420 sbss
= get_section_by_name("__DATA", "__bss");
422 if (s
== sbss
&& type
!= OUT_RESERVE
) {
423 error(ERR_WARNING
, "attempt to initialize memory in the"
424 " BSS section: ignored");
439 s
->size
+= realbytes
;
446 error(ERR_WARNING
, "uninitialized space declared in"
447 " %s section: zeroing",
448 get_section_name_by_index(secto
));
450 sect_write(s
, NULL
, realbytes
);
452 s
->size
+= realbytes
;
457 if (section
!= NO_SEG
)
458 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
460 sect_write(s
, data
, realbytes
);
464 addr
= *(int32_t *)data
;
466 if (section
!= NO_SEG
) {
468 error(ERR_NONFATAL
, "Mach-O format does not support"
469 " section base references");
471 add_reloc(s
, section
, 0, realbytes
);
481 sect_write(s
, mydata
, realbytes
);
485 if (section
== secto
)
486 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
488 if (section
!= NO_SEG
&& section
% 2) {
489 error(ERR_NONFATAL
, "Mach-O format does not support"
490 " section base references");
492 add_reloc(s
, section
, 1, 2);
495 WRITESHORT(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
496 sect_write(s
, mydata
, 2L);
500 if (section
== secto
)
501 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
503 if (section
!= NO_SEG
&& section
% 2) {
504 error(ERR_NONFATAL
, "Mach-O format does not support"
505 " section base references");
507 add_reloc(s
, section
, 1, 4);
510 WRITELONG(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
511 sect_write(s
, mydata
, 4L);
515 error(ERR_PANIC
, "unknown output type?");
520 static int32_t macho_section(char *name
, int pass
, int *bits
)
522 int32_t index
, originalIndex
;
523 char *sectionAttributes
;
527 /* Default to 32 bits. */
531 sectionAttributes
= NULL
;
533 sectionAttributes
= name
;
534 name
= strtok((char*)§ionAttributes
, " \t");
537 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
538 /* make lookup into section name translation table */
539 if (!strcmp(name
, sm
->nasmsect
)) {
540 char *currentAttribute
;
542 /* try to find section with that name */
543 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
546 /* create it if it doesn't exist yet */
548 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
550 sectstail
= &s
->next
;
552 s
->data
= saa_init(1L);
553 s
->index
= seg_alloc();
555 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
557 xstrncpy(s
->segname
, sm
->segname
);
558 xstrncpy(s
->sectname
, sm
->sectname
);
561 s
->flags
= sm
->flags
;
565 s
= get_section_by_index(index
);
568 while ((NULL
!= sectionAttributes
)
569 && (currentAttribute
= strtok((char*)§ionAttributes
, " \t"))) {
570 if (0 != *currentAttribute
) {
571 if (!(nasm_strnicmp("align=", currentAttribute
, 6))) {
573 int newAlignment
, value
;
575 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
576 newAlignment
= exact_log2(value
);
580 "unknown or missing alignment value \"%s\" "
581 "specified for section \"%s\"",
582 currentAttribute
+ 6,
585 } else if (0 > newAlignment
) {
587 "alignment of %d (for section \"%s\") is not "
594 if ((-1 != originalIndex
)
595 && (s
->align
!= newAlignment
)) {
597 "section \"%s\" has already been specified "
598 "with alignment %d, conflicts with new "
606 s
->align
= newAlignment
;
607 } else if (!(nasm_stricmp("data", currentAttribute
))) {
608 /* Do nothing; 'data' is implicit */
611 "unknown section attribute %s for section %s",
623 error(ERR_PANIC
, "invalid section name %s", name
);
627 static void macho_symdef(char *name
, int32_t section
, int32_t offset
,
628 int is_global
, char *special
)
633 error(ERR_NONFATAL
, "The Mach-O output format does "
634 "not support any special symbol types");
638 if (is_global
== 3) {
639 error(ERR_NONFATAL
, "The Mach-O format does not "
640 "(yet) support forward reference fixups.");
644 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
646 symstail
= &sym
->next
;
653 sym
->initial_snum
= -1;
655 /* external and common symbols get N_EXT */
659 if (section
== NO_SEG
) {
660 /* symbols in no section get absolute */
666 /* get the in-file index of the section the symbol was defined in */
667 sym
->sect
= get_section_fileindex_by_index(section
);
669 if (sym
->sect
== NO_SECT
) {
670 /* remember symbol number of references to external
671 ** symbols, this works because every external symbol gets
672 ** its own section number allocated internally by nasm and
673 ** can so be used as a key */
674 extsyms
= raa_write(extsyms
, section
, nsyms
);
675 sym
->initial_snum
= nsyms
;
680 /* there isn't actually a difference between global
681 ** and common symbols, both even have their size in
687 /* give an error on unfound section if it's not an
688 ** external or common symbol (assemble_file() does a
689 ** seg_alloc() on every call for them) */
690 error(ERR_PANIC
, "in-file index for section %d not found",
699 static int32_t macho_segbase(int32_t section
)
704 static int macho_directive(char *directive
, char *value
, int pass
)
709 static void macho_filename(char *inname
, char *outname
, efunc error
)
711 standard_extension(inname
, outname
, ".o", error
);
714 static const char *macho_stdmac
[] = {
715 "%define __SECT__ [section .text]",
716 "%macro __NASM_CDecl__ 1",
721 /* Comparison function for qsort symbol layout. */
722 static int layout_compare (const struct symbol
**s1
,
723 const struct symbol
**s2
)
725 return (strcmp ((*s1
)->name
, (*s2
)->name
));
728 /* The native assembler does a few things in a similar function
730 * Remove temporary labels
731 * Sort symbols according to local, external, undefined (by name)
732 * Order the string table
734 We do not remove temporary labels right now.
736 numsyms is the total number of symbols we have. strtabsize is the
737 number entries in the string table. */
739 static void macho_layout_symbols (uint32_t *numsyms
,
740 uint32_t *strtabsize
)
742 struct symbol
*sym
, **symp
;
746 *strtabsize
= sizeof (char);
750 while ((sym
= *symp
)) {
751 /* Undefined symbols are now external. */
752 if (sym
->type
== N_UNDF
)
755 if ((sym
->type
& N_EXT
) == 0) {
756 sym
->snum
= *numsyms
;
757 *numsyms
= *numsyms
+ 1;
761 if ((sym
->type
& N_TYPE
) != N_UNDF
)
766 /* If we handle debug info we'll want
767 to check for it here instead of just
768 adding the symbol to the string table. */
769 sym
->strx
= *strtabsize
;
770 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
771 *strtabsize
+= strlen(sym
->name
) + 1;
776 /* Next, sort the symbols. Most of this code is a direct translation from
777 the Apple cctools symbol layout. We need to keep compatibility with that. */
778 /* Set the indexes for symbol groups into the symbol table */
780 iextdefsym
= nlocalsym
;
781 iundefsym
= nlocalsym
+ nextdefsym
;
783 /* allocate arrays for sorting externals by name */
784 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
785 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
792 while ((sym
= *symp
)) {
794 if((sym
->type
& N_EXT
) == 0) {
795 sym
->strx
= *strtabsize
;
796 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
797 *strtabsize
+= strlen(sym
->name
) + 1;
800 if((sym
->type
& N_TYPE
) != N_UNDF
)
801 extdefsyms
[i
++] = sym
;
803 undefsyms
[j
++] = sym
;
808 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
809 (int (*)(const void *, const void *))layout_compare
);
810 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
811 (int (*)(const void *, const void *))layout_compare
);
813 for(i
= 0; i
< nextdefsym
; i
++) {
814 extdefsyms
[i
]->snum
= *numsyms
;
817 for(j
= 0; j
< nundefsym
; j
++) {
818 undefsyms
[j
]->snum
= *numsyms
;
823 /* Calculate some values we'll need for writing later. */
825 static void macho_calculate_sizes (void)
829 /* count sections and calculate in-memory and in-file offsets */
830 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
831 /* zerofill sections aren't actually written to the file */
832 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
833 seg_filesize
+= s
->size
;
835 seg_vmsize
+= s
->size
;
839 /* calculate size of all headers, load commands and sections to
840 ** get a pointer to the start of all the raw data */
841 if (seg_nsects
> 0) {
844 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
849 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
853 /* Write out the header information for the file. */
855 static void macho_write_header (void)
857 fwriteint32_t(MH_MAGIC
, machofp
); /* magic */
858 fwriteint32_t(CPU_TYPE_I386
, machofp
); /* CPU type */
859 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
860 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
861 fwriteint32_t(head_ncmds
, machofp
); /* number of load commands */
862 fwriteint32_t(head_sizeofcmds
, machofp
); /* size of load commands */
863 fwriteint32_t(0, machofp
); /* no flags */
866 /* Write out the segment load command at offset. */
868 static uint32_t macho_write_segment (uint32_t offset
)
871 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
872 uint32_t s_reloff
= 0;
875 fwriteint32_t(LC_SEGMENT
, machofp
); /* cmd == LC_SEGMENT */
877 /* size of load command including section load commands */
878 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
879 MACHO_SECTCMD_SIZE
, machofp
);
881 /* in an MH_OBJECT file all sections are in one unnamed (name
882 ** all zeros) segment */
883 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp
);
884 fwriteint32_t(0, machofp
); /* in-memory offset */
885 fwriteint32_t(seg_vmsize
, machofp
); /* in-memory size */
886 fwriteint32_t(offset
, machofp
); /* in-file offset to data */
887 fwriteint32_t(seg_filesize
, machofp
); /* in-file size */
888 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
889 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
890 fwriteint32_t(seg_nsects
, machofp
); /* number of sections */
891 fwriteint32_t(0, machofp
); /* no flags */
893 /* emit section headers */
894 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
895 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
896 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
897 fwriteint32_t(s_addr
, machofp
);
898 fwriteint32_t(s
->size
, machofp
);
900 /* dummy data for zerofill sections or proper values */
901 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
902 fwriteint32_t(offset
, machofp
);
903 /* Write out section alignment, as a power of two.
904 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
905 fwriteint32_t(s
->align
, machofp
);
906 /* To be compatible with cctools as we emit
907 a zero reloff if we have no relocations. */
908 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
909 fwriteint32_t(s
->nreloc
, machofp
);
912 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
914 fwriteint32_t(0, machofp
);
915 fwriteint32_t(0, machofp
);
916 fwriteint32_t(0, machofp
);
917 fwriteint32_t(0, machofp
);
920 fwriteint32_t(s
->flags
, machofp
); /* flags */
921 fwriteint32_t(0, machofp
); /* reserved */
922 fwriteint32_t(0, machofp
); /* reserved */
927 rel_padcnt
= rel_base
- offset
;
928 offset
= rel_base
+ s_reloff
;
933 /* For a given chain of relocs r, write out the entire relocation
934 chain to the object file. */
936 static void macho_write_relocs (struct reloc
*r
)
941 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
944 word2
|= r
->pcrel
<< 24;
945 word2
|= r
->length
<< 25;
946 word2
|= r
->ext
<< 27;
947 word2
|= r
->type
<< 28;
948 fwriteint32_t(word2
, machofp
); /* reloc data */
954 /* Write out the section data. */
955 static void macho_write_section (void)
957 struct section
*s
, *s2
;
959 char *rel_paddata
= "\0\0\0";
960 uint8_t fi
, *p
, *q
, blk
[4];
963 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
964 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
967 /* no padding needs to be done to the sections */
969 /* Like a.out Mach-O references things in the data or bss
970 * sections by addresses which are actually relative to the
971 * start of the _text_ section, in the _file_. See outaout.c
972 * for more information. */
974 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
975 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
979 /* get offset based on relocation type */
981 l
+= ((int32_t)*p
++) << 8;
983 if (r
->length
== 2) {
984 l
+= ((int32_t)*p
++) << 16;
985 l
+= ((int32_t)*p
++) << 24;
989 /* If the relocation is internal add to the current section
990 offset. Otherwise the only value we need is the symbol
991 offset which we already have. The linker takes care
992 of the rest of the address. */
994 /* add sizes of previous sections to current offset */
995 for (s2
= sects
, fi
= 1;
996 s2
!= NULL
&& fi
< r
->snum
; s2
= s2
->next
, fi
++)
997 if ((s2
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
1001 /* write new offset back */
1004 else if (r
->length
== 1)
1009 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1012 /* dump the section data to file */
1013 saa_fpwrite(s
->data
, machofp
);
1016 /* pad last section up to reloc entries on int32_t boundary */
1017 fwrite(rel_paddata
, rel_padcnt
, 1, machofp
);
1019 /* emit relocation entries */
1020 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1021 macho_write_relocs (s
->relocs
);
1024 /* Write out the symbol table. We should already have sorted this
1026 static void macho_write_symtab (void)
1033 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1035 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1036 if ((sym
->type
& N_EXT
) == 0) {
1037 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1038 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1039 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1040 fwriteint16_t(sym
->desc
, machofp
); /* description */
1042 /* Fix up the symbol value now that we know the final section
1044 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1045 for (s
= sects
, fi
= 1;
1046 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1047 sym
->value
+= s
->size
;
1050 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1054 for (i
= 0; i
< nextdefsym
; i
++) {
1055 sym
= extdefsyms
[i
];
1056 fwriteint32_t(sym
->strx
, machofp
);
1057 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1058 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1059 fwriteint16_t(sym
->desc
, machofp
); /* description */
1061 /* Fix up the symbol value now that we know the final section
1063 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1064 for (s
= sects
, fi
= 1;
1065 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1066 sym
->value
+= s
->size
;
1069 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1072 for (i
= 0; i
< nundefsym
; i
++) {
1074 fwriteint32_t(sym
->strx
, machofp
);
1075 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1076 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1077 fwriteint16_t(sym
->desc
, machofp
); /* description */
1079 /* Fix up the symbol value now that we know the final section
1081 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1082 for (s
= sects
, fi
= 1;
1083 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1084 sym
->value
+= s
->size
;
1087 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1091 /* Fixup the snum in the relocation entries, we should be
1092 doing this only for externally undefined symbols. */
1093 static void macho_fixup_relocs (struct reloc
*r
)
1100 for (i
= 0; i
< nundefsym
; i
++) {
1102 if (sym
->initial_snum
== r
->snum
) {
1103 r
->snum
= sym
->snum
;
1111 /* Write out the object file. */
1113 static void macho_write (void)
1115 uint32_t offset
= 0;
1117 /* mach-o object file structure:
1123 ** uint32_t mach file type
1124 ** uint32_t number of load commands
1125 ** uint32_t size of all load commands
1126 ** (includes section struct size of segment command)
1130 ** uint32_t command type == LC_SEGMENT
1131 ** uint32_t size of load command
1132 ** (including section load commands)
1133 ** char[16] segment name
1134 ** uint32_t in-memory offset
1135 ** uint32_t in-memory size
1136 ** uint32_t in-file offset to data area
1137 ** uint32_t in-file size
1138 ** (in-memory size excluding zerofill sections)
1139 ** int maximum vm protection
1140 ** int initial vm protection
1141 ** uint32_t number of sections
1145 ** char[16] section name
1146 ** char[16] segment name
1147 ** uint32_t in-memory offset
1148 ** uint32_t in-memory size
1149 ** uint32_t in-file offset
1150 ** uint32_t alignment
1151 ** (irrelevant in MH_OBJECT)
1152 ** uint32_t in-file offset of relocation entires
1153 ** uint32_t number of relocations
1155 ** uint32_t reserved
1156 ** uint32_t reserved
1158 ** symbol table command
1159 ** uint32_t command type == LC_SYMTAB
1160 ** uint32_t size of load command
1161 ** uint32_t symbol table offset
1162 ** uint32_t number of symbol table entries
1163 ** uint32_t string table offset
1164 ** uint32_t string table size
1168 ** padding to int32_t boundary
1170 ** relocation data (struct reloc)
1172 ** uint data (symbolnum, pcrel, length, extern, type)
1174 ** symbol table data (struct nlist)
1175 ** int32_t string table entry number
1177 ** (extern, absolute, defined in section)
1179 ** (0 for global symbols, section number of definition (>= 1, <=
1180 ** 254) for local symbols, size of variable for common symbols
1181 ** [type == extern])
1182 ** int16_t description
1183 ** (for stab debugging format)
1184 ** uint32_t value (i.e. file offset) of symbol or stab offset
1186 ** string table data
1187 ** list of null-terminated strings
1190 /* Emit the Mach-O header. */
1191 macho_write_header();
1193 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1195 /* emit the segment load command */
1197 offset
= macho_write_segment (offset
);
1199 error(ERR_WARNING
, "no sections?");
1202 /* write out symbol command */
1203 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1204 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1205 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1206 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1209 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1210 fwriteint32_t(offset
, machofp
); /* string table offset */
1211 fwriteint32_t(strslen
, machofp
); /* string table size */
1214 /* emit section data */
1216 macho_write_section ();
1218 /* emit symbol table if we have symbols */
1220 macho_write_symtab ();
1222 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1224 /* emit string table */
1225 saa_fpwrite(strs
, machofp
);
1227 /* We do quite a bit here, starting with finalizing all of the data
1228 for the object file, writing, and then freeing all of the data from
1231 static void macho_cleanup(int debuginfo
)
1239 /* Sort all symbols. */
1240 macho_layout_symbols (&nsyms
, &strslen
);
1242 /* Fixup relocation entries */
1243 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1244 macho_fixup_relocs (s
->relocs
);
1247 /* First calculate and finalize needed values. */
1248 macho_calculate_sizes();
1254 /* free up everything */
1255 while (sects
->next
) {
1257 sects
= sects
->next
;
1260 while (s
->relocs
!= NULL
) {
1262 s
->relocs
= s
->relocs
->next
;
1273 while (syms
->next
) {
1282 /* Debugging routines. */
1283 static void debug_reloc (struct reloc
*r
)
1285 fprintf (stdout
, "reloc:\n");
1286 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1287 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1288 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1289 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1290 fprintf (stdout
, "\text: %d\n", r
->ext
);
1291 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1294 static void debug_section_relocs (struct section
*s
)
1296 struct reloc
*r
= s
->relocs
;
1298 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1306 struct ofmt of_macho
= {
1307 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",