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. */
28 /* Mach-O in-file header structure sizes */
29 #define MACHO_HEADER_SIZE (28)
30 #define MACHO_SEGCMD_SIZE (56)
31 #define MACHO_SECTCMD_SIZE (68)
32 #define MACHO_SYMCMD_SIZE (24)
33 #define MACHO_NLIST_SIZE (12)
34 #define MACHO_RELINFO_SIZE (8)
36 /* Mach-O file header values */
37 #define MH_MAGIC (0xfeedface)
38 #define CPU_TYPE_I386 (7) /* x86 platform */
39 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
40 #define MH_OBJECT (0x1) /* object file */
42 #define LC_SEGMENT (0x1) /* segment load command */
43 #define LC_SYMTAB (0x2) /* symbol table load command */
45 #define VM_PROT_NONE (0x00)
46 #define VM_PROT_READ (0x01)
47 #define VM_PROT_WRITE (0x02)
48 #define VM_PROT_EXECUTE (0x04)
50 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
51 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
54 /* nasm internal data */
61 /* data that goes into the file */
62 char sectname
[16]; /* what this section is called */
63 char segname
[16]; /* segment this section will be in */
64 uint32_t size
; /* in-memory and -file size */
65 uint32_t nreloc
; /* relocation entry count */
66 uint32_t flags
; /* type and attributes (masked) */
69 #define SECTION_TYPE 0x000000ff /* section type mask */
71 #define S_REGULAR (0x0) /* standard section */
72 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
74 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
75 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
76 machine instructions */
77 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
79 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
83 static struct sectmap
{
89 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
},
90 {".data", "__DATA", "__data", S_REGULAR
},
91 {".rodata", "__DATA", "__const", S_REGULAR
},
92 {".bss", "__DATA", "__bss", S_ZEROFILL
},
97 /* nasm internal data */
100 /* data that goes into the file */
101 int32_t addr
; /* op's offset in section */
102 unsigned int snum
:24, /* contains symbol index if
103 ** ext otherwise in-file
105 pcrel
:1, /* relative relocation */
106 length
:2, /* 0=byte, 1=word, 2=int32_t */
107 ext
:1, /* external symbol referenced */
108 type
:4; /* reloc type, 0 for us */
111 #define R_ABS 0 /* absolute relocation */
112 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
113 ** highest bit == 1 */
116 /* nasm internal data */
117 struct symbol
*next
; /* next symbol in the list */
118 char *name
; /* name of this symbol */
119 int32_t initial_snum
; /* symbol number used above in
121 int32_t snum
; /* true snum for reloc */
123 /* data that goes into the file */
124 int32_t strx
; /* string table index */
125 uint8_t type
; /* symbol type */
126 uint8_t sect
; /* NO_SECT or section number */
127 int16_t desc
; /* for stab debugging, 0 for us */
128 uint32_t value
; /* offset of symbol in section */
131 /* symbol type bits */
132 #define N_EXT 0x01 /* global or external symbol */
134 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
135 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
136 #define N_SECT 0xe /* defined symbol, n_sect holds
139 #define N_TYPE 0x0e /* type bit mask */
141 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
143 /* special section number values */
144 #define NO_SECT 0 /* no section, invalid */
145 #define MAX_SECT 255 /* maximum number of sections */
147 static struct section
*sects
, **sectstail
;
148 static struct symbol
*syms
, **symstail
;
149 static uint32_t nsyms
;
151 /* These variables are set by macho_layout_symbols() to organize
152 the symbol table and string table in order the dynamic linker
153 expects. They are then used in macho_write() to put out the
154 symbols and strings in that order.
156 The order of the symbol table is:
158 defined external symbols (sorted by name)
159 undefined external symbols (sorted by name)
161 The order of the string table is:
162 strings for external symbols
163 strings for local symbols
165 static uint32_t ilocalsym
= 0;
166 static uint32_t iextdefsym
= 0;
167 static uint32_t iundefsym
= 0;
168 static uint32_t nlocalsym
;
169 static uint32_t nextdefsym
;
170 static uint32_t nundefsym
;
171 static struct symbol
**extdefsyms
= NULL
;
172 static struct symbol
**undefsyms
= NULL
;
174 static struct RAA
*extsyms
;
175 static struct SAA
*strs
;
176 static uint32_t strslen
;
178 static FILE *machofp
;
180 static evalfunc evaluate
;
182 extern struct ofmt of_macho
;
184 /* Global file information. This should be cleaned up into either
185 a structure or as function arguments. */
186 uint32_t head_ncmds
= 0;
187 uint32_t head_sizeofcmds
= 0;
188 uint32_t seg_filesize
= 0;
189 uint32_t seg_vmsize
= 0;
190 uint32_t seg_nsects
= 0;
191 uint32_t rel_padcnt
= 0;
194 #define xstrncpy(xdst, xsrc) \
195 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
196 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
197 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
199 #define align(x, y) \
200 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
202 #define alignint32_t(x) \
203 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
205 static void debug_reloc (struct reloc
*);
206 static void debug_section_relocs (struct section
*) _unused
;
208 static int exact_log2 (uint32_t align
)
212 } else if (align
& (align
-1)) {
213 return -1; /* Not a power of 2 */
216 return __builtin_ctzl (align
);
220 /* We know exactly one bit is set at this point. */
221 if (align
& 0xffff0000)
223 if (align
& 0xff00ff00)
225 if (align
& 0xf0f0f0f0)
227 if (align
& 0xcccccccc)
229 if (align
& 0xaaaaaaaa)
237 static struct section
*get_section_by_name(const char *segname
,
238 const char *sectname
)
242 for (s
= sects
; s
!= NULL
; s
= s
->next
)
243 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
249 static struct section
*get_section_by_index(const int32_t index
)
253 for (s
= sects
; s
!= NULL
; s
= s
->next
)
254 if (index
== s
->index
)
260 static int32_t get_section_index_by_name(const char *segname
,
261 const char *sectname
)
265 for (s
= sects
; s
!= NULL
; s
= s
->next
)
266 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
272 static char *get_section_name_by_index(const int32_t index
)
276 for (s
= sects
; s
!= NULL
; s
= s
->next
)
277 if (index
== s
->index
)
283 static uint8_t get_section_fileindex_by_index(const int32_t index
)
288 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
289 if (index
== s
->index
)
294 "too many sections (>255) - clipped by fileindex");
299 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
308 (void)ldef
; /* placate optimisers */
320 extsyms
= raa_init();
323 /* string table starts with a zero byte - don't ask why */
324 saa_wbytes(strs
, &zero
, sizeof(char));
328 static int macho_setinfo(enum geninfo type
, char **val
)
335 static void sect_write(struct section
*sect
,
336 const uint8_t *data
, uint32_t len
)
338 saa_wbytes(sect
->data
, data
, len
);
342 static void add_reloc(struct section
*sect
, int32_t section
,
343 int pcrel
, int bytes
)
348 /* NeXT as puts relocs in reversed order (address-wise) into the
349 ** files, so we do the same, doesn't seem to make much of a
350 ** difference either way */
351 r
= nasm_malloc(sizeof(struct reloc
));
352 r
->next
= sect
->relocs
;
355 /* the current end of the section will be the symbol's address for
356 ** now, might have to be fixed by macho_fixup_relocs() later on. make
357 ** sure we don't make the symbol scattered by setting the highest
358 ** bit by accident */
359 r
->addr
= sect
->size
& ~R_SCATTERED
;
363 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
364 r
->length
= bytes
>> 1;
366 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
369 if (section
== NO_SEG
) {
370 /* absolute local symbol if no section index given */
373 fi
= get_section_fileindex_by_index(section
);
376 /* external symbol if no section with that index known,
377 ** symbol number was saved in macho_symdef() */
378 r
->snum
= raa_read(extsyms
, section
);
381 /* local symbol in section fi */
389 static void macho_output(int32_t secto
, const void *data
, uint32_t type
,
390 int32_t section
, int32_t wrt
)
392 struct section
*s
, *sbss
;
393 int32_t realbytes
= type
& OUT_SIZMASK
;
395 uint8_t mydata
[4], *p
;
401 error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
402 /* continue to do _something_ */
405 if (secto
== NO_SEG
) {
406 if (type
!= OUT_RESERVE
)
407 error(ERR_NONFATAL
, "attempt to assemble code in "
413 s
= get_section_by_index(secto
);
416 error(ERR_WARNING
, "attempt to assemble code in"
417 " section %d: defaulting to `.text'", secto
);
418 s
= get_section_by_name("__TEXT", "__text");
420 /* should never happen */
422 error(ERR_PANIC
, "text section not found");
425 sbss
= get_section_by_name("__DATA", "__bss");
427 if (s
== sbss
&& type
!= OUT_RESERVE
) {
428 error(ERR_WARNING
, "attempt to initialize memory in the"
429 " BSS section: ignored");
444 s
->size
+= realbytes
;
451 error(ERR_WARNING
, "uninitialized space declared in"
452 " %s section: zeroing",
453 get_section_name_by_index(secto
));
455 sect_write(s
, NULL
, realbytes
);
457 s
->size
+= realbytes
;
462 if (section
!= NO_SEG
)
463 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
465 sect_write(s
, data
, realbytes
);
469 addr
= *(int32_t *)data
;
471 if (section
!= NO_SEG
) {
473 error(ERR_NONFATAL
, "Mach-O format does not support"
474 " section base references");
476 add_reloc(s
, section
, 0, realbytes
);
486 sect_write(s
, mydata
, realbytes
);
490 if (section
== secto
)
491 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
493 if (section
!= NO_SEG
&& section
% 2) {
494 error(ERR_NONFATAL
, "Mach-O format does not support"
495 " section base references");
497 add_reloc(s
, section
, 1, 2);
500 WRITESHORT(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
501 sect_write(s
, mydata
, 2L);
505 if (section
== secto
)
506 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
508 if (section
!= NO_SEG
&& section
% 2) {
509 error(ERR_NONFATAL
, "Mach-O format does not support"
510 " section base references");
512 add_reloc(s
, section
, 1, 4);
515 WRITELONG(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
516 sect_write(s
, mydata
, 4L);
520 error(ERR_PANIC
, "unknown output type?");
525 static int32_t macho_section(char *name
, int pass
, int *bits
)
527 int32_t index
, originalIndex
;
528 char *sectionAttributes
;
534 /* Default to 32 bits. */
538 sectionAttributes
= NULL
;
540 sectionAttributes
= name
;
541 name
= nasm_strsep(§ionAttributes
, " \t");
544 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
545 /* make lookup into section name translation table */
546 if (!strcmp(name
, sm
->nasmsect
)) {
547 char *currentAttribute
;
549 /* try to find section with that name */
550 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
553 /* create it if it doesn't exist yet */
555 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
557 sectstail
= &s
->next
;
559 s
->data
= saa_init(1L);
560 s
->index
= seg_alloc();
562 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
564 xstrncpy(s
->segname
, sm
->segname
);
565 xstrncpy(s
->sectname
, sm
->sectname
);
568 s
->flags
= sm
->flags
;
572 s
= get_section_by_index(index
);
575 while ((NULL
!= sectionAttributes
)
576 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
577 if (0 != *currentAttribute
) {
578 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
580 int newAlignment
, value
;
582 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
583 newAlignment
= exact_log2(value
);
587 "unknown or missing alignment value \"%s\" "
588 "specified for section \"%s\"",
589 currentAttribute
+ 6,
592 } else if (0 > newAlignment
) {
594 "alignment of %d (for section \"%s\") is not "
601 if ((-1 != originalIndex
)
602 && (s
->align
!= newAlignment
)) {
604 "section \"%s\" has already been specified "
605 "with alignment %d, conflicts with new "
613 s
->align
= newAlignment
;
614 } else if (!nasm_stricmp("data", currentAttribute
)) {
615 /* Do nothing; 'data' is implicit */
618 "unknown section attribute %s for section %s",
630 error(ERR_PANIC
, "invalid section name %s", name
);
634 static void macho_symdef(char *name
, int32_t section
, int32_t offset
,
635 int is_global
, char *special
)
640 error(ERR_NONFATAL
, "The Mach-O output format does "
641 "not support any special symbol types");
645 if (is_global
== 3) {
646 error(ERR_NONFATAL
, "The Mach-O format does not "
647 "(yet) support forward reference fixups.");
651 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
653 symstail
= &sym
->next
;
660 sym
->initial_snum
= -1;
662 /* external and common symbols get N_EXT */
666 if (section
== NO_SEG
) {
667 /* symbols in no section get absolute */
673 /* get the in-file index of the section the symbol was defined in */
674 sym
->sect
= get_section_fileindex_by_index(section
);
676 if (sym
->sect
== NO_SECT
) {
677 /* remember symbol number of references to external
678 ** symbols, this works because every external symbol gets
679 ** its own section number allocated internally by nasm and
680 ** can so be used as a key */
681 extsyms
= raa_write(extsyms
, section
, nsyms
);
682 sym
->initial_snum
= nsyms
;
687 /* there isn't actually a difference between global
688 ** and common symbols, both even have their size in
694 /* give an error on unfound section if it's not an
695 ** external or common symbol (assemble_file() does a
696 ** seg_alloc() on every call for them) */
697 error(ERR_PANIC
, "in-file index for section %d not found",
706 static int32_t macho_segbase(int32_t section
)
711 static int macho_directive(char *directive
, char *value
, int pass
)
719 static void macho_filename(char *inname
, char *outname
, efunc error
)
721 standard_extension(inname
, outname
, ".o", error
);
724 static const char *macho_stdmac
[] = {
725 "%define __SECT__ [section .text]",
726 "%macro __NASM_CDecl__ 1",
731 /* Comparison function for qsort symbol layout. */
732 static int layout_compare (const struct symbol
**s1
,
733 const struct symbol
**s2
)
735 return (strcmp ((*s1
)->name
, (*s2
)->name
));
738 /* The native assembler does a few things in a similar function
740 * Remove temporary labels
741 * Sort symbols according to local, external, undefined (by name)
742 * Order the string table
744 We do not remove temporary labels right now.
746 numsyms is the total number of symbols we have. strtabsize is the
747 number entries in the string table. */
749 static void macho_layout_symbols (uint32_t *numsyms
,
750 uint32_t *strtabsize
)
752 struct symbol
*sym
, **symp
;
756 *strtabsize
= sizeof (char);
760 while ((sym
= *symp
)) {
761 /* Undefined symbols are now external. */
762 if (sym
->type
== N_UNDF
)
765 if ((sym
->type
& N_EXT
) == 0) {
766 sym
->snum
= *numsyms
;
767 *numsyms
= *numsyms
+ 1;
771 if ((sym
->type
& N_TYPE
) != N_UNDF
)
776 /* If we handle debug info we'll want
777 to check for it here instead of just
778 adding the symbol to the string table. */
779 sym
->strx
= *strtabsize
;
780 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
781 *strtabsize
+= strlen(sym
->name
) + 1;
786 /* Next, sort the symbols. Most of this code is a direct translation from
787 the Apple cctools symbol layout. We need to keep compatibility with that. */
788 /* Set the indexes for symbol groups into the symbol table */
790 iextdefsym
= nlocalsym
;
791 iundefsym
= nlocalsym
+ nextdefsym
;
793 /* allocate arrays for sorting externals by name */
794 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
795 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
802 while ((sym
= *symp
)) {
804 if((sym
->type
& N_EXT
) == 0) {
805 sym
->strx
= *strtabsize
;
806 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
807 *strtabsize
+= strlen(sym
->name
) + 1;
810 if((sym
->type
& N_TYPE
) != N_UNDF
)
811 extdefsyms
[i
++] = sym
;
813 undefsyms
[j
++] = sym
;
818 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
819 (int (*)(const void *, const void *))layout_compare
);
820 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
821 (int (*)(const void *, const void *))layout_compare
);
823 for(i
= 0; i
< nextdefsym
; i
++) {
824 extdefsyms
[i
]->snum
= *numsyms
;
827 for(j
= 0; j
< nundefsym
; j
++) {
828 undefsyms
[j
]->snum
= *numsyms
;
833 /* Calculate some values we'll need for writing later. */
835 static void macho_calculate_sizes (void)
839 /* count sections and calculate in-memory and in-file offsets */
840 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
841 /* zerofill sections aren't actually written to the file */
842 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
843 seg_filesize
+= s
->size
;
845 seg_vmsize
+= s
->size
;
849 /* calculate size of all headers, load commands and sections to
850 ** get a pointer to the start of all the raw data */
851 if (seg_nsects
> 0) {
854 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
859 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
863 /* Write out the header information for the file. */
865 static void macho_write_header (void)
867 fwriteint32_t(MH_MAGIC
, machofp
); /* magic */
868 fwriteint32_t(CPU_TYPE_I386
, machofp
); /* CPU type */
869 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
870 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
871 fwriteint32_t(head_ncmds
, machofp
); /* number of load commands */
872 fwriteint32_t(head_sizeofcmds
, machofp
); /* size of load commands */
873 fwriteint32_t(0, machofp
); /* no flags */
876 /* Write out the segment load command at offset. */
878 static uint32_t macho_write_segment (uint32_t offset
)
881 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
882 uint32_t s_reloff
= 0;
885 fwriteint32_t(LC_SEGMENT
, machofp
); /* cmd == LC_SEGMENT */
887 /* size of load command including section load commands */
888 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
889 MACHO_SECTCMD_SIZE
, machofp
);
891 /* in an MH_OBJECT file all sections are in one unnamed (name
892 ** all zeros) segment */
893 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp
);
894 fwriteint32_t(0, machofp
); /* in-memory offset */
895 fwriteint32_t(seg_vmsize
, machofp
); /* in-memory size */
896 fwriteint32_t(offset
, machofp
); /* in-file offset to data */
897 fwriteint32_t(seg_filesize
, machofp
); /* in-file size */
898 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
899 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
900 fwriteint32_t(seg_nsects
, machofp
); /* number of sections */
901 fwriteint32_t(0, machofp
); /* no flags */
903 /* emit section headers */
904 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
905 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
906 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
907 fwriteint32_t(s_addr
, machofp
);
908 fwriteint32_t(s
->size
, machofp
);
910 /* dummy data for zerofill sections or proper values */
911 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
912 fwriteint32_t(offset
, machofp
);
913 /* Write out section alignment, as a power of two.
914 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
915 fwriteint32_t(s
->align
, machofp
);
916 /* To be compatible with cctools as we emit
917 a zero reloff if we have no relocations. */
918 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
919 fwriteint32_t(s
->nreloc
, machofp
);
922 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
924 fwriteint32_t(0, machofp
);
925 fwriteint32_t(0, machofp
);
926 fwriteint32_t(0, machofp
);
927 fwriteint32_t(0, machofp
);
930 fwriteint32_t(s
->flags
, machofp
); /* flags */
931 fwriteint32_t(0, machofp
); /* reserved */
932 fwriteint32_t(0, machofp
); /* reserved */
937 rel_padcnt
= rel_base
- offset
;
938 offset
= rel_base
+ s_reloff
;
943 /* For a given chain of relocs r, write out the entire relocation
944 chain to the object file. */
946 static void macho_write_relocs (struct reloc
*r
)
951 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
954 word2
|= r
->pcrel
<< 24;
955 word2
|= r
->length
<< 25;
956 word2
|= r
->ext
<< 27;
957 word2
|= r
->type
<< 28;
958 fwriteint32_t(word2
, machofp
); /* reloc data */
964 /* Write out the section data. */
965 static void macho_write_section (void)
967 struct section
*s
, *s2
;
969 char *rel_paddata
= "\0\0\0";
970 uint8_t fi
, *p
, *q
, blk
[4];
973 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
974 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
977 /* no padding needs to be done to the sections */
979 /* Like a.out Mach-O references things in the data or bss
980 * sections by addresses which are actually relative to the
981 * start of the _text_ section, in the _file_. See outaout.c
982 * for more information. */
984 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
985 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
989 /* get offset based on relocation type */
991 l
+= ((int32_t)*p
++) << 8;
993 if (r
->length
== 2) {
994 l
+= ((int32_t)*p
++) << 16;
995 l
+= ((int32_t)*p
++) << 24;
999 /* If the relocation is internal add to the current section
1000 offset. Otherwise the only value we need is the symbol
1001 offset which we already have. The linker takes care
1002 of the rest of the address. */
1004 /* add sizes of previous sections to current offset */
1005 for (s2
= sects
, fi
= 1;
1006 s2
!= NULL
&& fi
< r
->snum
; s2
= s2
->next
, fi
++)
1007 if ((s2
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
1011 /* write new offset back */
1014 else if (r
->length
== 1)
1019 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1022 /* dump the section data to file */
1023 saa_fpwrite(s
->data
, machofp
);
1026 /* pad last section up to reloc entries on int32_t boundary */
1027 fwrite(rel_paddata
, rel_padcnt
, 1, machofp
);
1029 /* emit relocation entries */
1030 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1031 macho_write_relocs (s
->relocs
);
1034 /* Write out the symbol table. We should already have sorted this
1036 static void macho_write_symtab (void)
1043 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1045 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1046 if ((sym
->type
& N_EXT
) == 0) {
1047 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1048 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1049 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1050 fwriteint16_t(sym
->desc
, machofp
); /* description */
1052 /* Fix up the symbol value now that we know the final section
1054 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1055 for (s
= sects
, fi
= 1;
1056 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1057 sym
->value
+= s
->size
;
1060 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1064 for (i
= 0; i
< nextdefsym
; i
++) {
1065 sym
= extdefsyms
[i
];
1066 fwriteint32_t(sym
->strx
, machofp
);
1067 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1068 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1069 fwriteint16_t(sym
->desc
, machofp
); /* description */
1071 /* Fix up the symbol value now that we know the final section
1073 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1074 for (s
= sects
, fi
= 1;
1075 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1076 sym
->value
+= s
->size
;
1079 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1082 for (i
= 0; i
< nundefsym
; i
++) {
1084 fwriteint32_t(sym
->strx
, machofp
);
1085 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1086 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1087 fwriteint16_t(sym
->desc
, machofp
); /* description */
1089 /* Fix up the symbol value now that we know the final section
1091 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1092 for (s
= sects
, fi
= 1;
1093 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1094 sym
->value
+= s
->size
;
1097 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1101 /* Fixup the snum in the relocation entries, we should be
1102 doing this only for externally undefined symbols. */
1103 static void macho_fixup_relocs (struct reloc
*r
)
1110 for (i
= 0; i
< nundefsym
; i
++) {
1112 if (sym
->initial_snum
== r
->snum
) {
1113 r
->snum
= sym
->snum
;
1121 /* Write out the object file. */
1123 static void macho_write (void)
1125 uint32_t offset
= 0;
1127 /* mach-o object file structure:
1133 ** uint32_t mach file type
1134 ** uint32_t number of load commands
1135 ** uint32_t size of all load commands
1136 ** (includes section struct size of segment command)
1140 ** uint32_t command type == LC_SEGMENT
1141 ** uint32_t size of load command
1142 ** (including section load commands)
1143 ** char[16] segment name
1144 ** uint32_t in-memory offset
1145 ** uint32_t in-memory size
1146 ** uint32_t in-file offset to data area
1147 ** uint32_t in-file size
1148 ** (in-memory size excluding zerofill sections)
1149 ** int maximum vm protection
1150 ** int initial vm protection
1151 ** uint32_t number of sections
1155 ** char[16] section name
1156 ** char[16] segment name
1157 ** uint32_t in-memory offset
1158 ** uint32_t in-memory size
1159 ** uint32_t in-file offset
1160 ** uint32_t alignment
1161 ** (irrelevant in MH_OBJECT)
1162 ** uint32_t in-file offset of relocation entires
1163 ** uint32_t number of relocations
1165 ** uint32_t reserved
1166 ** uint32_t reserved
1168 ** symbol table command
1169 ** uint32_t command type == LC_SYMTAB
1170 ** uint32_t size of load command
1171 ** uint32_t symbol table offset
1172 ** uint32_t number of symbol table entries
1173 ** uint32_t string table offset
1174 ** uint32_t string table size
1178 ** padding to int32_t boundary
1180 ** relocation data (struct reloc)
1182 ** uint data (symbolnum, pcrel, length, extern, type)
1184 ** symbol table data (struct nlist)
1185 ** int32_t string table entry number
1187 ** (extern, absolute, defined in section)
1189 ** (0 for global symbols, section number of definition (>= 1, <=
1190 ** 254) for local symbols, size of variable for common symbols
1191 ** [type == extern])
1192 ** int16_t description
1193 ** (for stab debugging format)
1194 ** uint32_t value (i.e. file offset) of symbol or stab offset
1196 ** string table data
1197 ** list of null-terminated strings
1200 /* Emit the Mach-O header. */
1201 macho_write_header();
1203 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1205 /* emit the segment load command */
1207 offset
= macho_write_segment (offset
);
1209 error(ERR_WARNING
, "no sections?");
1212 /* write out symbol command */
1213 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1214 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1215 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1216 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1219 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1220 fwriteint32_t(offset
, machofp
); /* string table offset */
1221 fwriteint32_t(strslen
, machofp
); /* string table size */
1224 /* emit section data */
1226 macho_write_section ();
1228 /* emit symbol table if we have symbols */
1230 macho_write_symtab ();
1232 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1234 /* emit string table */
1235 saa_fpwrite(strs
, machofp
);
1237 /* We do quite a bit here, starting with finalizing all of the data
1238 for the object file, writing, and then freeing all of the data from
1241 static void macho_cleanup(int debuginfo
)
1249 /* Sort all symbols. */
1250 macho_layout_symbols (&nsyms
, &strslen
);
1252 /* Fixup relocation entries */
1253 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1254 macho_fixup_relocs (s
->relocs
);
1257 /* First calculate and finalize needed values. */
1258 macho_calculate_sizes();
1264 /* free up everything */
1265 while (sects
->next
) {
1267 sects
= sects
->next
;
1270 while (s
->relocs
!= NULL
) {
1272 s
->relocs
= s
->relocs
->next
;
1283 while (syms
->next
) {
1292 /* Debugging routines. */
1293 static void debug_reloc (struct reloc
*r
)
1295 fprintf (stdout
, "reloc:\n");
1296 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1297 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1298 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1299 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1300 fprintf (stdout
, "\text: %d\n", r
->ext
);
1301 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1304 static void debug_section_relocs (struct section
*s
)
1306 struct reloc
*r
= s
->relocs
;
1308 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1316 struct ofmt of_macho
= {
1317 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",