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
},
95 /* nasm internal data */
98 /* data that goes into the file */
99 int32_t addr
; /* op's offset in section */
100 unsigned int snum
:24, /* contains symbol index if
101 ** ext otherwise in-file
103 pcrel
:1, /* relative relocation */
104 length
:2, /* 0=byte, 1=word, 2=int32_t */
105 ext
:1, /* external symbol referenced */
106 type
:4; /* reloc type, 0 for us */
109 #define R_ABS 0 /* absolute relocation */
110 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
111 ** highest bit == 1 */
114 /* nasm internal data */
115 struct symbol
*next
; /* next symbol in the list */
116 char *name
; /* name of this symbol */
117 int32_t initial_snum
; /* symbol number used above in
119 int32_t snum
; /* true snum for reloc */
121 /* data that goes into the file */
122 int32_t strx
; /* string table index */
123 uint8_t type
; /* symbol type */
124 uint8_t sect
; /* NO_SECT or section number */
125 int16_t desc
; /* for stab debugging, 0 for us */
126 uint32_t value
; /* offset of symbol in section */
129 /* symbol type bits */
130 #define N_EXT 0x01 /* global or external symbol */
132 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
133 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
134 #define N_SECT 0xe /* defined symbol, n_sect holds
137 #define N_TYPE 0x0e /* type bit mask */
139 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
141 /* special section number values */
142 #define NO_SECT 0 /* no section, invalid */
143 #define MAX_SECT 255 /* maximum number of sections */
145 static struct section
*sects
, **sectstail
;
146 static struct symbol
*syms
, **symstail
;
147 static uint32_t nsyms
;
149 /* These variables are set by macho_layout_symbols() to organize
150 the symbol table and string table in order the dynamic linker
151 expects. They are then used in macho_write() to put out the
152 symbols and strings in that order.
154 The order of the symbol table is:
156 defined external symbols (sorted by name)
157 undefined external symbols (sorted by name)
159 The order of the string table is:
160 strings for external symbols
161 strings for local symbols
163 static uint32_t ilocalsym
= 0;
164 static uint32_t iextdefsym
= 0;
165 static uint32_t iundefsym
= 0;
166 static uint32_t nlocalsym
;
167 static uint32_t nextdefsym
;
168 static uint32_t nundefsym
;
169 static struct symbol
**extdefsyms
= NULL
;
170 static struct symbol
**undefsyms
= NULL
;
172 static struct RAA
*extsyms
;
173 static struct SAA
*strs
;
174 static uint32_t strslen
;
176 static FILE *machofp
;
178 static evalfunc evaluate
;
180 extern struct ofmt of_macho
;
182 /* Global file information. This should be cleaned up into either
183 a structure or as function arguments. */
184 uint32_t head_ncmds
= 0;
185 uint32_t head_sizeofcmds
= 0;
186 uint32_t seg_filesize
= 0;
187 uint32_t seg_vmsize
= 0;
188 uint32_t seg_nsects
= 0;
189 uint32_t rel_padcnt
= 0;
192 #define xstrncpy(xdst, xsrc) \
193 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
194 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
195 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
197 #define align(x, y) \
198 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
200 #define alignint32_t(x) \
201 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
203 static void debug_reloc (struct reloc
*);
204 static void debug_section_relocs (struct section
*) _unused
;
206 static int exact_log2 (uint32_t align
)
210 } else if (align
& (align
-1)) {
211 return -1; /* Not a power of 2 */
214 return __builtin_ctzl (align
);
218 /* We know exactly one bit is set at this point. */
219 if (align
& 0xffff0000)
221 if (align
& 0xff00ff00)
223 if (align
& 0xf0f0f0f0)
225 if (align
& 0xcccccccc)
227 if (align
& 0xaaaaaaaa)
235 static struct section
*get_section_by_name(const char *segname
,
236 const char *sectname
)
240 for (s
= sects
; s
!= NULL
; s
= s
->next
)
241 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
247 static struct section
*get_section_by_index(const int32_t index
)
251 for (s
= sects
; s
!= NULL
; s
= s
->next
)
252 if (index
== s
->index
)
258 static int32_t get_section_index_by_name(const char *segname
,
259 const char *sectname
)
263 for (s
= sects
; s
!= NULL
; s
= s
->next
)
264 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
270 static char *get_section_name_by_index(const int32_t index
)
274 for (s
= sects
; s
!= NULL
; s
= s
->next
)
275 if (index
== s
->index
)
281 static uint8_t get_section_fileindex_by_index(const int32_t index
)
286 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
287 if (index
== s
->index
)
292 "too many sections (>255) - clipped by fileindex");
297 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
306 (void)ldef
; /* placate optimisers */
318 extsyms
= raa_init();
321 /* string table starts with a zero byte - don't ask why */
322 saa_wbytes(strs
, &zero
, sizeof(char));
326 static int macho_setinfo(enum geninfo type
, char **val
)
333 static void sect_write(struct section
*sect
,
334 const uint8_t *data
, uint32_t len
)
336 saa_wbytes(sect
->data
, data
, len
);
340 static void add_reloc(struct section
*sect
, int32_t section
,
341 int pcrel
, int bytes
)
346 /* NeXT as puts relocs in reversed order (address-wise) into the
347 ** files, so we do the same, doesn't seem to make much of a
348 ** difference either way */
349 r
= nasm_malloc(sizeof(struct reloc
));
350 r
->next
= sect
->relocs
;
353 /* the current end of the section will be the symbol's address for
354 ** now, might have to be fixed by macho_fixup_relocs() later on. make
355 ** sure we don't make the symbol scattered by setting the highest
356 ** bit by accident */
357 r
->addr
= sect
->size
& ~R_SCATTERED
;
361 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
362 r
->length
= bytes
>> 1;
364 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
367 if (section
== NO_SEG
) {
368 /* absolute local symbol if no section index given */
371 fi
= get_section_fileindex_by_index(section
);
374 /* external symbol if no section with that index known,
375 ** symbol number was saved in macho_symdef() */
376 r
->snum
= raa_read(extsyms
, section
);
379 /* local symbol in section fi */
387 static void macho_output(int32_t secto
, const void *data
, uint32_t type
,
388 int32_t section
, int32_t wrt
)
390 struct section
*s
, *sbss
;
391 int32_t realbytes
= type
& OUT_SIZMASK
;
393 uint8_t mydata
[4], *p
;
399 error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
400 /* continue to do _something_ */
403 if (secto
== NO_SEG
) {
404 if (type
!= OUT_RESERVE
)
405 error(ERR_NONFATAL
, "attempt to assemble code in "
411 s
= get_section_by_index(secto
);
414 error(ERR_WARNING
, "attempt to assemble code in"
415 " section %d: defaulting to `.text'", secto
);
416 s
= get_section_by_name("__TEXT", "__text");
418 /* should never happen */
420 error(ERR_PANIC
, "text section not found");
423 sbss
= get_section_by_name("__DATA", "__bss");
425 if (s
== sbss
&& type
!= OUT_RESERVE
) {
426 error(ERR_WARNING
, "attempt to initialize memory in the"
427 " BSS section: ignored");
442 s
->size
+= realbytes
;
449 error(ERR_WARNING
, "uninitialized space declared in"
450 " %s section: zeroing",
451 get_section_name_by_index(secto
));
453 sect_write(s
, NULL
, realbytes
);
455 s
->size
+= realbytes
;
460 if (section
!= NO_SEG
)
461 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
463 sect_write(s
, data
, realbytes
);
467 addr
= *(int32_t *)data
;
469 if (section
!= NO_SEG
) {
471 error(ERR_NONFATAL
, "Mach-O format does not support"
472 " section base references");
474 add_reloc(s
, section
, 0, realbytes
);
484 sect_write(s
, mydata
, realbytes
);
488 if (section
== secto
)
489 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
491 if (section
!= NO_SEG
&& section
% 2) {
492 error(ERR_NONFATAL
, "Mach-O format does not support"
493 " section base references");
495 add_reloc(s
, section
, 1, 2);
498 WRITESHORT(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
499 sect_write(s
, mydata
, 2L);
503 if (section
== secto
)
504 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
506 if (section
!= NO_SEG
&& section
% 2) {
507 error(ERR_NONFATAL
, "Mach-O format does not support"
508 " section base references");
510 add_reloc(s
, section
, 1, 4);
513 WRITELONG(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
514 sect_write(s
, mydata
, 4L);
518 error(ERR_PANIC
, "unknown output type?");
523 static int32_t macho_section(char *name
, int pass
, int *bits
)
528 int32_t index
, originalIndex
;
529 char *sectionAttributes
;
533 /* Default to 32 bits. */
537 sectionAttributes
= NULL
;
539 sectionAttributes
= name
;
540 name
= nasm_strsep(§ionAttributes
, " \t");
543 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
544 /* make lookup into section name translation table */
545 if (!strcmp(name
, sm
->nasmsect
)) {
546 char *currentAttribute
;
548 /* try to find section with that name */
549 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
552 /* create it if it doesn't exist yet */
554 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
556 sectstail
= &s
->next
;
558 s
->data
= saa_init(1L);
559 s
->index
= seg_alloc();
561 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
563 xstrncpy(s
->segname
, sm
->segname
);
564 xstrncpy(s
->sectname
, sm
->sectname
);
567 s
->flags
= sm
->flags
;
571 s
= get_section_by_index(index
);
574 while ((NULL
!= sectionAttributes
)
575 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
576 if (0 != *currentAttribute
) {
577 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
579 int newAlignment
, value
;
581 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
582 newAlignment
= exact_log2(value
);
586 "unknown or missing alignment value \"%s\" "
587 "specified for section \"%s\"",
588 currentAttribute
+ 6,
591 } else if (0 > newAlignment
) {
593 "alignment of %d (for section \"%s\") is not "
600 if ((-1 != originalIndex
)
601 && (s
->align
!= newAlignment
)) {
603 "section \"%s\" has already been specified "
604 "with alignment %d, conflicts with new "
612 s
->align
= newAlignment
;
613 } else if (!nasm_stricmp("data", currentAttribute
)) {
614 /* Do nothing; 'data' is implicit */
617 "unknown section attribute %s for section %s",
629 error(ERR_PANIC
, "invalid section name %s", name
);
633 static void macho_symdef(char *name
, int32_t section
, int32_t offset
,
634 int is_global
, char *special
)
639 error(ERR_NONFATAL
, "The Mach-O output format does "
640 "not support any special symbol types");
644 if (is_global
== 3) {
645 error(ERR_NONFATAL
, "The Mach-O format does not "
646 "(yet) support forward reference fixups.");
650 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
652 symstail
= &sym
->next
;
659 sym
->initial_snum
= -1;
661 /* external and common symbols get N_EXT */
665 if (section
== NO_SEG
) {
666 /* symbols in no section get absolute */
672 /* get the in-file index of the section the symbol was defined in */
673 sym
->sect
= get_section_fileindex_by_index(section
);
675 if (sym
->sect
== NO_SECT
) {
676 /* remember symbol number of references to external
677 ** symbols, this works because every external symbol gets
678 ** its own section number allocated internally by nasm and
679 ** can so be used as a key */
680 extsyms
= raa_write(extsyms
, section
, nsyms
);
681 sym
->initial_snum
= nsyms
;
686 /* there isn't actually a difference between global
687 ** and common symbols, both even have their size in
693 /* give an error on unfound section if it's not an
694 ** external or common symbol (assemble_file() does a
695 ** seg_alloc() on every call for them) */
696 error(ERR_PANIC
, "in-file index for section %d not found",
705 static int32_t macho_segbase(int32_t section
)
710 static int macho_directive(char *directive
, char *value
, int pass
)
718 static void macho_filename(char *inname
, char *outname
, efunc error
)
720 standard_extension(inname
, outname
, ".o", error
);
723 static const char *macho_stdmac
[] = {
724 "%define __SECT__ [section .text]",
725 "%macro __NASM_CDecl__ 1",
730 /* Comparison function for qsort symbol layout. */
731 static int layout_compare (const struct symbol
**s1
,
732 const struct symbol
**s2
)
734 return (strcmp ((*s1
)->name
, (*s2
)->name
));
737 /* The native assembler does a few things in a similar function
739 * Remove temporary labels
740 * Sort symbols according to local, external, undefined (by name)
741 * Order the string table
743 We do not remove temporary labels right now.
745 numsyms is the total number of symbols we have. strtabsize is the
746 number entries in the string table. */
748 static void macho_layout_symbols (uint32_t *numsyms
,
749 uint32_t *strtabsize
)
751 struct symbol
*sym
, **symp
;
755 *strtabsize
= sizeof (char);
759 while ((sym
= *symp
)) {
760 /* Undefined symbols are now external. */
761 if (sym
->type
== N_UNDF
)
764 if ((sym
->type
& N_EXT
) == 0) {
765 sym
->snum
= *numsyms
;
766 *numsyms
= *numsyms
+ 1;
770 if ((sym
->type
& N_TYPE
) != N_UNDF
)
775 /* If we handle debug info we'll want
776 to check for it here instead of just
777 adding the symbol to the string table. */
778 sym
->strx
= *strtabsize
;
779 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
780 *strtabsize
+= strlen(sym
->name
) + 1;
785 /* Next, sort the symbols. Most of this code is a direct translation from
786 the Apple cctools symbol layout. We need to keep compatibility with that. */
787 /* Set the indexes for symbol groups into the symbol table */
789 iextdefsym
= nlocalsym
;
790 iundefsym
= nlocalsym
+ nextdefsym
;
792 /* allocate arrays for sorting externals by name */
793 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
794 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
801 while ((sym
= *symp
)) {
803 if((sym
->type
& N_EXT
) == 0) {
804 sym
->strx
= *strtabsize
;
805 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
806 *strtabsize
+= strlen(sym
->name
) + 1;
809 if((sym
->type
& N_TYPE
) != N_UNDF
)
810 extdefsyms
[i
++] = sym
;
812 undefsyms
[j
++] = sym
;
817 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
818 (int (*)(const void *, const void *))layout_compare
);
819 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
820 (int (*)(const void *, const void *))layout_compare
);
822 for(i
= 0; i
< nextdefsym
; i
++) {
823 extdefsyms
[i
]->snum
= *numsyms
;
826 for(j
= 0; j
< nundefsym
; j
++) {
827 undefsyms
[j
]->snum
= *numsyms
;
832 /* Calculate some values we'll need for writing later. */
834 static void macho_calculate_sizes (void)
838 /* count sections and calculate in-memory and in-file offsets */
839 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
840 /* zerofill sections aren't actually written to the file */
841 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
842 seg_filesize
+= s
->size
;
844 seg_vmsize
+= s
->size
;
848 /* calculate size of all headers, load commands and sections to
849 ** get a pointer to the start of all the raw data */
850 if (seg_nsects
> 0) {
853 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
858 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
862 /* Write out the header information for the file. */
864 static void macho_write_header (void)
866 fwriteint32_t(MH_MAGIC
, machofp
); /* magic */
867 fwriteint32_t(CPU_TYPE_I386
, machofp
); /* CPU type */
868 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
869 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
870 fwriteint32_t(head_ncmds
, machofp
); /* number of load commands */
871 fwriteint32_t(head_sizeofcmds
, machofp
); /* size of load commands */
872 fwriteint32_t(0, machofp
); /* no flags */
875 /* Write out the segment load command at offset. */
877 static uint32_t macho_write_segment (uint32_t offset
)
880 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
881 uint32_t s_reloff
= 0;
884 fwriteint32_t(LC_SEGMENT
, machofp
); /* cmd == LC_SEGMENT */
886 /* size of load command including section load commands */
887 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
888 MACHO_SECTCMD_SIZE
, machofp
);
890 /* in an MH_OBJECT file all sections are in one unnamed (name
891 ** all zeros) segment */
892 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp
);
893 fwriteint32_t(0, machofp
); /* in-memory offset */
894 fwriteint32_t(seg_vmsize
, machofp
); /* in-memory size */
895 fwriteint32_t(offset
, machofp
); /* in-file offset to data */
896 fwriteint32_t(seg_filesize
, machofp
); /* in-file size */
897 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
898 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
899 fwriteint32_t(seg_nsects
, machofp
); /* number of sections */
900 fwriteint32_t(0, machofp
); /* no flags */
902 /* emit section headers */
903 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
904 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
905 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
906 fwriteint32_t(s_addr
, machofp
);
907 fwriteint32_t(s
->size
, machofp
);
909 /* dummy data for zerofill sections or proper values */
910 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
911 fwriteint32_t(offset
, machofp
);
912 /* Write out section alignment, as a power of two.
913 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
914 fwriteint32_t(s
->align
, machofp
);
915 /* To be compatible with cctools as we emit
916 a zero reloff if we have no relocations. */
917 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
918 fwriteint32_t(s
->nreloc
, machofp
);
921 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
923 fwriteint32_t(0, machofp
);
924 fwriteint32_t(0, machofp
);
925 fwriteint32_t(0, machofp
);
926 fwriteint32_t(0, machofp
);
929 fwriteint32_t(s
->flags
, machofp
); /* flags */
930 fwriteint32_t(0, machofp
); /* reserved */
931 fwriteint32_t(0, machofp
); /* reserved */
936 rel_padcnt
= rel_base
- offset
;
937 offset
= rel_base
+ s_reloff
;
942 /* For a given chain of relocs r, write out the entire relocation
943 chain to the object file. */
945 static void macho_write_relocs (struct reloc
*r
)
950 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
953 word2
|= r
->pcrel
<< 24;
954 word2
|= r
->length
<< 25;
955 word2
|= r
->ext
<< 27;
956 word2
|= r
->type
<< 28;
957 fwriteint32_t(word2
, machofp
); /* reloc data */
963 /* Write out the section data. */
964 static void macho_write_section (void)
966 struct section
*s
, *s2
;
968 char *rel_paddata
= "\0\0\0";
969 uint8_t fi
, *p
, *q
, blk
[4];
972 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
973 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
976 /* no padding needs to be done to the sections */
978 /* Like a.out Mach-O references things in the data or bss
979 * sections by addresses which are actually relative to the
980 * start of the _text_ section, in the _file_. See outaout.c
981 * for more information. */
983 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
984 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
988 /* get offset based on relocation type */
990 l
+= ((int32_t)*p
++) << 8;
992 if (r
->length
== 2) {
993 l
+= ((int32_t)*p
++) << 16;
994 l
+= ((int32_t)*p
++) << 24;
998 /* If the relocation is internal add to the current section
999 offset. Otherwise the only value we need is the symbol
1000 offset which we already have. The linker takes care
1001 of the rest of the address. */
1003 /* add sizes of previous sections to current offset */
1004 for (s2
= sects
, fi
= 1;
1005 s2
!= NULL
&& fi
< r
->snum
; s2
= s2
->next
, fi
++)
1006 if ((s2
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
1010 /* write new offset back */
1013 else if (r
->length
== 1)
1018 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1021 /* dump the section data to file */
1022 saa_fpwrite(s
->data
, machofp
);
1025 /* pad last section up to reloc entries on int32_t boundary */
1026 fwrite(rel_paddata
, rel_padcnt
, 1, machofp
);
1028 /* emit relocation entries */
1029 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1030 macho_write_relocs (s
->relocs
);
1033 /* Write out the symbol table. We should already have sorted this
1035 static void macho_write_symtab (void)
1042 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1044 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1045 if ((sym
->type
& N_EXT
) == 0) {
1046 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1047 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1048 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1049 fwriteint16_t(sym
->desc
, machofp
); /* description */
1051 /* Fix up the symbol value now that we know the final section
1053 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1054 for (s
= sects
, fi
= 1;
1055 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1056 sym
->value
+= s
->size
;
1059 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1063 for (i
= 0; i
< nextdefsym
; i
++) {
1064 sym
= extdefsyms
[i
];
1065 fwriteint32_t(sym
->strx
, machofp
);
1066 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1067 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1068 fwriteint16_t(sym
->desc
, machofp
); /* description */
1070 /* Fix up the symbol value now that we know the final section
1072 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1073 for (s
= sects
, fi
= 1;
1074 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1075 sym
->value
+= s
->size
;
1078 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1081 for (i
= 0; i
< nundefsym
; i
++) {
1083 fwriteint32_t(sym
->strx
, machofp
);
1084 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1085 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1086 fwriteint16_t(sym
->desc
, machofp
); /* description */
1088 /* Fix up the symbol value now that we know the final section
1090 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1091 for (s
= sects
, fi
= 1;
1092 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1093 sym
->value
+= s
->size
;
1096 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1100 /* Fixup the snum in the relocation entries, we should be
1101 doing this only for externally undefined symbols. */
1102 static void macho_fixup_relocs (struct reloc
*r
)
1109 for (i
= 0; i
< nundefsym
; i
++) {
1111 if (sym
->initial_snum
== r
->snum
) {
1112 r
->snum
= sym
->snum
;
1120 /* Write out the object file. */
1122 static void macho_write (void)
1124 uint32_t offset
= 0;
1126 /* mach-o object file structure:
1132 ** uint32_t mach file type
1133 ** uint32_t number of load commands
1134 ** uint32_t size of all load commands
1135 ** (includes section struct size of segment command)
1139 ** uint32_t command type == LC_SEGMENT
1140 ** uint32_t size of load command
1141 ** (including section load commands)
1142 ** char[16] segment name
1143 ** uint32_t in-memory offset
1144 ** uint32_t in-memory size
1145 ** uint32_t in-file offset to data area
1146 ** uint32_t in-file size
1147 ** (in-memory size excluding zerofill sections)
1148 ** int maximum vm protection
1149 ** int initial vm protection
1150 ** uint32_t number of sections
1154 ** char[16] section name
1155 ** char[16] segment name
1156 ** uint32_t in-memory offset
1157 ** uint32_t in-memory size
1158 ** uint32_t in-file offset
1159 ** uint32_t alignment
1160 ** (irrelevant in MH_OBJECT)
1161 ** uint32_t in-file offset of relocation entires
1162 ** uint32_t number of relocations
1164 ** uint32_t reserved
1165 ** uint32_t reserved
1167 ** symbol table command
1168 ** uint32_t command type == LC_SYMTAB
1169 ** uint32_t size of load command
1170 ** uint32_t symbol table offset
1171 ** uint32_t number of symbol table entries
1172 ** uint32_t string table offset
1173 ** uint32_t string table size
1177 ** padding to int32_t boundary
1179 ** relocation data (struct reloc)
1181 ** uint data (symbolnum, pcrel, length, extern, type)
1183 ** symbol table data (struct nlist)
1184 ** int32_t string table entry number
1186 ** (extern, absolute, defined in section)
1188 ** (0 for global symbols, section number of definition (>= 1, <=
1189 ** 254) for local symbols, size of variable for common symbols
1190 ** [type == extern])
1191 ** int16_t description
1192 ** (for stab debugging format)
1193 ** uint32_t value (i.e. file offset) of symbol or stab offset
1195 ** string table data
1196 ** list of null-terminated strings
1199 /* Emit the Mach-O header. */
1200 macho_write_header();
1202 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1204 /* emit the segment load command */
1206 offset
= macho_write_segment (offset
);
1208 error(ERR_WARNING
, "no sections?");
1211 /* write out symbol command */
1212 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1213 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1214 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1215 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1218 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1219 fwriteint32_t(offset
, machofp
); /* string table offset */
1220 fwriteint32_t(strslen
, machofp
); /* string table size */
1223 /* emit section data */
1225 macho_write_section ();
1227 /* emit symbol table if we have symbols */
1229 macho_write_symtab ();
1231 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1233 /* emit string table */
1234 saa_fpwrite(strs
, machofp
);
1236 /* We do quite a bit here, starting with finalizing all of the data
1237 for the object file, writing, and then freeing all of the data from
1240 static void macho_cleanup(int debuginfo
)
1248 /* Sort all symbols. */
1249 macho_layout_symbols (&nsyms
, &strslen
);
1251 /* Fixup relocation entries */
1252 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1253 macho_fixup_relocs (s
->relocs
);
1256 /* First calculate and finalize needed values. */
1257 macho_calculate_sizes();
1263 /* free up everything */
1264 while (sects
->next
) {
1266 sects
= sects
->next
;
1269 while (s
->relocs
!= NULL
) {
1271 s
->relocs
= s
->relocs
->next
;
1282 while (syms
->next
) {
1291 /* Debugging routines. */
1292 static void debug_reloc (struct reloc
*r
)
1294 fprintf (stdout
, "reloc:\n");
1295 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1296 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1297 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1298 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1299 fprintf (stdout
, "\text: %d\n", r
->ext
);
1300 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1303 static void debug_section_relocs (struct section
*s
)
1305 struct reloc
*r
= s
->relocs
;
1307 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1315 struct ofmt of_macho
= {
1316 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",