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. */
25 /* Mach-O in-file header structure sizes */
26 #define MACHO_HEADER_SIZE (28)
27 #define MACHO_SEGCMD_SIZE (56)
28 #define MACHO_SECTCMD_SIZE (68)
29 #define MACHO_SYMCMD_SIZE (24)
30 #define MACHO_NLIST_SIZE (12)
31 #define MACHO_RELINFO_SIZE (8)
33 /* Mach-O file header values */
34 #define MH_MAGIC (0xfeedface)
35 #define CPU_TYPE_I386 (7) /* x86 platform */
36 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
37 #define MH_OBJECT (0x1) /* object file */
39 #define LC_SEGMENT (0x1) /* segment load command */
40 #define LC_SYMTAB (0x2) /* symbol table load command */
42 #define VM_PROT_NONE (0x00)
43 #define VM_PROT_READ (0x01)
44 #define VM_PROT_WRITE (0x02)
45 #define VM_PROT_EXECUTE (0x04)
47 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
48 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
51 /* nasm internal data */
58 /* data that goes into the file */
59 int8_t sectname
[16]; /* what this section is called */
60 int8_t segname
[16]; /* segment this section will be in */
61 uint32_t size
; /* in-memory and -file size */
62 uint32_t nreloc
; /* relocation entry count */
63 uint32_t flags
; /* type and attributes (masked) */
66 #define SECTION_TYPE 0x000000ff /* section type mask */
68 #define S_REGULAR (0x0) /* standard section */
69 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
71 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
72 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
73 machine instructions */
74 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
76 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
80 static struct sectmap
{
81 const int8_t *nasmsect
;
82 const int8_t *segname
;
83 const int8_t *sectname
;
86 ".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
}, {
87 ".data", "__DATA", "__data", S_REGULAR
}, {
88 ".rodata", "__DATA", "__const", S_REGULAR
}, {
89 ".bss", "__DATA", "__bss", S_ZEROFILL
}, {
93 /* nasm internal data */
96 /* data that goes into the file */
97 int32_t addr
; /* op's offset in section */
98 unsigned int snum
:24, /* contains symbol index if
99 ** ext otherwise in-file
101 pcrel
:1, /* relative relocation */
102 length
:2, /* 0=byte, 1=word, 2=int32_t */
103 ext
:1, /* external symbol referenced */
104 type
:4; /* reloc type, 0 for us */
107 #define R_ABS 0 /* absolute relocation */
108 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
109 ** highest bit == 1 */
112 /* nasm internal data */
113 struct symbol
*next
; /* next symbol in the list */
114 int8_t *name
; /* name of this symbol */
115 int32_t initial_snum
; /* symbol number used above in
117 int32_t snum
; /* true snum for reloc */
119 /* data that goes into the file */
120 int32_t strx
; /* string table index */
121 uint8_t type
; /* symbol type */
122 uint8_t sect
; /* NO_SECT or section number */
123 int16_t desc
; /* for stab debugging, 0 for us */
124 uint32_t value
; /* offset of symbol in section */
127 /* symbol type bits */
128 #define N_EXT 0x01 /* global or external symbol */
130 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
131 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
132 #define N_SECT 0xe /* defined symbol, n_sect holds
135 #define N_TYPE 0x0e /* type bit mask */
137 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
139 /* special section number values */
140 #define NO_SECT 0 /* no section, invalid */
141 #define MAX_SECT 255 /* maximum number of sections */
143 static struct section
*sects
, **sectstail
;
144 static struct symbol
*syms
, **symstail
;
145 static uint32_t nsyms
;
147 /* These variables are set by macho_layout_symbols() to organize
148 the symbol table and string table in order the dynamic linker
149 expects. They are then used in macho_write() to put out the
150 symbols and strings in that order.
152 The order of the symbol table is:
154 defined external symbols (sorted by name)
155 undefined external symbols (sorted by name)
157 The order of the string table is:
158 strings for external symbols
159 strings for local symbols
161 static uint32_t ilocalsym
= 0;
162 static uint32_t iextdefsym
= 0;
163 static uint32_t iundefsym
= 0;
164 static uint32_t nlocalsym
;
165 static uint32_t nextdefsym
;
166 static uint32_t nundefsym
;
167 static struct symbol
**extdefsyms
= NULL
;
168 static struct symbol
**undefsyms
= NULL
;
170 static struct RAA
*extsyms
;
171 static struct SAA
*strs
;
172 static uint32_t strslen
;
174 static FILE *machofp
;
176 static evalfunc evaluate
;
178 extern struct ofmt of_macho
;
180 /* Global file information. This should be cleaned up into either
181 a structure or as function arguments. */
182 uint32_t head_ncmds
= 0;
183 uint32_t head_sizeofcmds
= 0;
184 uint32_t seg_filesize
= 0;
185 uint32_t seg_vmsize
= 0;
186 uint32_t seg_nsects
= 0;
187 uint32_t rel_padcnt
= 0;
190 #define xstrncpy(xdst, xsrc) \
191 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
192 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
193 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
195 #define align(x, y) \
196 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
198 #define alignint32_t(x) \
199 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
201 static void debug_reloc (struct reloc
*);
202 static void debug_section_relocs (struct section
*) __attribute__ ((unused
));
204 static int exact_log2 (uint32_t align
) {
205 if (align
!= (align
& -align
)) {
209 return (align
? __builtin_ctzl (align
) : 0);
213 while (align
>>= 1) {
222 static struct section
*get_section_by_name(const int8_t *segname
,
223 const int8_t *sectname
)
227 for (s
= sects
; s
!= NULL
; s
= s
->next
)
228 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
234 static struct section
*get_section_by_index(const int32_t index
)
238 for (s
= sects
; s
!= NULL
; s
= s
->next
)
239 if (index
== s
->index
)
245 static int32_t get_section_index_by_name(const int8_t *segname
,
246 const int8_t *sectname
)
250 for (s
= sects
; s
!= NULL
; s
= s
->next
)
251 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
257 static int8_t *get_section_name_by_index(const int32_t index
)
261 for (s
= sects
; s
!= NULL
; s
= s
->next
)
262 if (index
== s
->index
)
268 static uint8_t get_section_fileindex_by_index(const int32_t index
)
273 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
274 if (index
== s
->index
)
279 "too many sections (>255) - clipped by fileindex");
284 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
293 (void)ldef
; /* placate optimisers */
305 extsyms
= raa_init();
308 /* string table starts with a zero byte - don't ask why */
309 saa_wbytes(strs
, &zero
, sizeof(int8_t));
313 static int macho_setinfo(enum geninfo type
, int8_t **val
)
318 static void sect_write(struct section
*sect
,
319 const uint8_t *data
, uint32_t len
)
321 saa_wbytes(sect
->data
, data
, len
);
325 static void add_reloc(struct section
*sect
, int32_t section
,
326 int pcrel
, int bytes
)
331 /* NeXT as puts relocs in reversed order (address-wise) into the
332 ** files, so we do the same, doesn't seem to make much of a
333 ** difference either way */
334 r
= nasm_malloc(sizeof(struct reloc
));
335 r
->next
= sect
->relocs
;
338 /* the current end of the section will be the symbol's address for
339 ** now, might have to be fixed by macho_fixup_relocs() later on. make
340 ** sure we don't make the symbol scattered by setting the highest
341 ** bit by accident */
342 r
->addr
= sect
->size
& ~R_SCATTERED
;
346 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
347 r
->length
= bytes
>> 1;
349 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
352 if (section
== NO_SEG
) {
353 /* absolute local symbol if no section index given */
356 fi
= get_section_fileindex_by_index(section
);
359 /* external symbol if no section with that index known,
360 ** symbol number was saved in macho_symdef() */
361 r
->snum
= raa_read(extsyms
, section
);
364 /* local symbol in section fi */
372 static void macho_output(int32_t secto
, const void *data
, uint32_t type
,
373 int32_t section
, int32_t wrt
)
375 struct section
*s
, *sbss
;
376 int32_t realbytes
= type
& OUT_SIZMASK
;
378 uint8_t mydata
[4], *p
;
384 error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
385 /* continue to do _something_ */
388 if (secto
== NO_SEG
) {
389 if (type
!= OUT_RESERVE
)
390 error(ERR_NONFATAL
, "attempt to assemble code in "
396 s
= get_section_by_index(secto
);
399 error(ERR_WARNING
, "attempt to assemble code in"
400 " section %d: defaulting to `.text'", secto
);
401 s
= get_section_by_name("__TEXT", "__text");
403 /* should never happen */
405 error(ERR_PANIC
, "text section not found");
408 sbss
= get_section_by_name("__DATA", "__bss");
410 if (s
== sbss
&& type
!= OUT_RESERVE
) {
411 error(ERR_WARNING
, "attempt to initialize memory in the"
412 " BSS section: ignored");
427 s
->size
+= realbytes
;
434 error(ERR_WARNING
, "uninitialized space declared in"
435 " %s section: zeroing",
436 get_section_name_by_index(secto
));
438 sect_write(s
, NULL
, realbytes
);
440 s
->size
+= realbytes
;
445 if (section
!= NO_SEG
)
446 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
448 sect_write(s
, data
, realbytes
);
452 addr
= *(int32_t *)data
;
454 if (section
!= NO_SEG
) {
456 error(ERR_NONFATAL
, "Mach-O format does not support"
457 " section base references");
459 add_reloc(s
, section
, 0, realbytes
);
469 sect_write(s
, mydata
, realbytes
);
473 if (section
== secto
)
474 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
476 if (section
!= NO_SEG
&& section
% 2) {
477 error(ERR_NONFATAL
, "Mach-O format does not support"
478 " section base references");
480 add_reloc(s
, section
, 1, 2);
483 WRITESHORT(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
484 sect_write(s
, mydata
, 2L);
488 if (section
== secto
)
489 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
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, 4);
498 WRITELONG(p
, *(int32_t *)data
- (realbytes
+ s
->size
));
499 sect_write(s
, mydata
, 4L);
503 error(ERR_PANIC
, "unknown output type?");
508 static int32_t macho_section(int8_t *name
, int pass
, int *bits
)
510 int32_t index
, originalIndex
;
511 int8_t *sectionAttributes
;
515 /* Default to 32 bits. */
519 sectionAttributes
= NULL
;
521 sectionAttributes
= name
;
522 name
= strtok((char*)§ionAttributes
, " \t");
525 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
526 /* make lookup into section name translation table */
527 if (!strcmp(name
, sm
->nasmsect
)) {
528 int8_t *currentAttribute
;
530 /* try to find section with that name */
531 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
534 /* create it if it doesn't exist yet */
536 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
538 sectstail
= &s
->next
;
540 s
->data
= saa_init(1L);
541 s
->index
= seg_alloc();
543 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
545 xstrncpy(s
->segname
, sm
->segname
);
546 xstrncpy(s
->sectname
, sm
->sectname
);
549 s
->flags
= sm
->flags
;
553 s
= get_section_by_index(index
);
556 while ((NULL
!= sectionAttributes
)
557 && (currentAttribute
= strtok((char*)§ionAttributes
, " \t"))) {
558 if (0 != *currentAttribute
) {
559 if (0 == strncasecmp("align=", currentAttribute
, 6)) {
561 int newAlignment
, value
;
563 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
564 newAlignment
= exact_log2(value
);
568 "unknown or missing alignment value \"%s\" "
569 "specified for section \"%s\"",
570 currentAttribute
+ 6,
573 } else if (0 > newAlignment
) {
575 "alignment of %d (for section \"%s\") is not "
582 if ((-1 != originalIndex
)
583 && (s
->align
!= newAlignment
)) {
585 "section \"%s\" has already been specified "
586 "with alignment %d, conflicts with new "
594 s
->align
= newAlignment
;
595 } else if (0 == strcasecmp("data", currentAttribute
)) {
596 /* Do nothing; 'data' is implicit */
599 "unknown section attribute %s for section %s",
611 error(ERR_PANIC
, "invalid section name %s", name
);
615 static void macho_symdef(int8_t *name
, int32_t section
, int32_t offset
,
616 int is_global
, int8_t *special
)
621 error(ERR_NONFATAL
, "The Mach-O output format does "
622 "not support any special symbol types");
626 if (is_global
== 3) {
627 error(ERR_NONFATAL
, "The Mach-O format does not "
628 "(yet) support forward reference fixups.");
632 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
634 symstail
= &sym
->next
;
641 sym
->initial_snum
= -1;
643 /* external and common symbols get N_EXT */
647 if (section
== NO_SEG
) {
648 /* symbols in no section get absolute */
654 /* get the in-file index of the section the symbol was defined in */
655 sym
->sect
= get_section_fileindex_by_index(section
);
657 if (sym
->sect
== NO_SECT
) {
658 /* remember symbol number of references to external
659 ** symbols, this works because every external symbol gets
660 ** its own section number allocated internally by nasm and
661 ** can so be used as a key */
662 extsyms
= raa_write(extsyms
, section
, nsyms
);
663 sym
->initial_snum
= nsyms
;
668 /* there isn't actually a difference between global
669 ** and common symbols, both even have their size in
675 /* give an error on unfound section if it's not an
676 ** external or common symbol (assemble_file() does a
677 ** seg_alloc() on every call for them) */
678 error(ERR_PANIC
, "in-file index for section %d not found",
687 static int32_t macho_segbase(int32_t section
)
692 static int macho_directive(int8_t *directive
, int8_t *value
, int pass
)
697 static void macho_filename(int8_t *inname
, int8_t *outname
, efunc error
)
699 standard_extension(inname
, outname
, ".o", error
);
702 static const int8_t *macho_stdmac
[] = {
703 "%define __SECT__ [section .text]",
704 "%macro __NASM_CDecl__ 1",
709 /* Comparison function for qsort symbol layout. */
710 static int layout_compare (const struct symbol
**s1
,
711 const struct symbol
**s2
)
713 return (strcmp ((*s1
)->name
, (*s2
)->name
));
716 /* The native assembler does a few things in a similar function
718 * Remove temporary labels
719 * Sort symbols according to local, external, undefined (by name)
720 * Order the string table
722 We do not remove temporary labels right now.
724 numsyms is the total number of symbols we have. strtabsize is the
725 number entries in the string table. */
727 static void macho_layout_symbols (uint32_t *numsyms
,
728 uint32_t *strtabsize
)
730 struct symbol
*sym
, **symp
;
734 *strtabsize
= sizeof (int8_t);
738 while ((sym
= *symp
)) {
739 /* Undefined symbols are now external. */
740 if (sym
->type
== N_UNDF
)
743 if ((sym
->type
& N_EXT
) == 0) {
744 sym
->snum
= *numsyms
;
745 *numsyms
= *numsyms
+ 1;
749 if ((sym
->type
& N_TYPE
) != N_UNDF
)
754 /* If we handle debug info we'll want
755 to check for it here instead of just
756 adding the symbol to the string table. */
757 sym
->strx
= *strtabsize
;
758 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
759 *strtabsize
+= strlen(sym
->name
) + 1;
764 /* Next, sort the symbols. Most of this code is a direct translation from
765 the Apple cctools symbol layout. We need to keep compatibility with that. */
766 /* Set the indexes for symbol groups into the symbol table */
768 iextdefsym
= nlocalsym
;
769 iundefsym
= nlocalsym
+ nextdefsym
;
771 /* allocate arrays for sorting externals by name */
772 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
773 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
780 while ((sym
= *symp
)) {
782 if((sym
->type
& N_EXT
) == 0) {
783 sym
->strx
= *strtabsize
;
784 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
785 *strtabsize
+= strlen(sym
->name
) + 1;
788 if((sym
->type
& N_TYPE
) != N_UNDF
)
789 extdefsyms
[i
++] = sym
;
791 undefsyms
[j
++] = sym
;
796 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
797 (int (*)(const void *, const void *))layout_compare
);
798 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
799 (int (*)(const void *, const void *))layout_compare
);
801 for(i
= 0; i
< nextdefsym
; i
++) {
802 extdefsyms
[i
]->snum
= *numsyms
;
805 for(j
= 0; j
< nundefsym
; j
++) {
806 undefsyms
[j
]->snum
= *numsyms
;
811 /* Calculate some values we'll need for writing later. */
813 static void macho_calculate_sizes (void)
817 /* count sections and calculate in-memory and in-file offsets */
818 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
819 /* zerofill sections aren't actually written to the file */
820 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
821 seg_filesize
+= s
->size
;
823 seg_vmsize
+= s
->size
;
827 /* calculate size of all headers, load commands and sections to
828 ** get a pointer to the start of all the raw data */
829 if (seg_nsects
> 0) {
832 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
837 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
841 /* Write out the header information for the file. */
843 static void macho_write_header (void)
845 fwriteint32_t(MH_MAGIC
, machofp
); /* magic */
846 fwriteint32_t(CPU_TYPE_I386
, machofp
); /* CPU type */
847 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
848 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
849 fwriteint32_t(head_ncmds
, machofp
); /* number of load commands */
850 fwriteint32_t(head_sizeofcmds
, machofp
); /* size of load commands */
851 fwriteint32_t(0, machofp
); /* no flags */
854 /* Write out the segment load command at offset. */
856 static uint32_t macho_write_segment (uint32_t offset
)
859 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
860 uint32_t s_reloff
= 0;
863 fwriteint32_t(LC_SEGMENT
, machofp
); /* cmd == LC_SEGMENT */
865 /* size of load command including section load commands */
866 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
867 MACHO_SECTCMD_SIZE
, machofp
);
869 /* in an MH_OBJECT file all sections are in one unnamed (name
870 ** all zeros) segment */
871 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp
);
872 fwriteint32_t(0, machofp
); /* in-memory offset */
873 fwriteint32_t(seg_vmsize
, machofp
); /* in-memory size */
874 fwriteint32_t(offset
, machofp
); /* in-file offset to data */
875 fwriteint32_t(seg_filesize
, machofp
); /* in-file size */
876 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
877 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
878 fwriteint32_t(seg_nsects
, machofp
); /* number of sections */
879 fwriteint32_t(0, machofp
); /* no flags */
881 /* emit section headers */
882 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
883 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
884 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
885 fwriteint32_t(s_addr
, machofp
);
886 fwriteint32_t(s
->size
, machofp
);
888 /* dummy data for zerofill sections or proper values */
889 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
890 fwriteint32_t(offset
, machofp
);
891 /* Write out section alignment, as a power of two.
892 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
893 fwriteint32_t(s
->align
, machofp
);
894 /* To be compatible with cctools as we emit
895 a zero reloff if we have no relocations. */
896 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
897 fwriteint32_t(s
->nreloc
, machofp
);
900 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
902 fwriteint32_t(0, machofp
);
903 fwriteint32_t(0, machofp
);
904 fwriteint32_t(0, machofp
);
905 fwriteint32_t(0, machofp
);
908 fwriteint32_t(s
->flags
, machofp
); /* flags */
909 fwriteint32_t(0, machofp
); /* reserved */
910 fwriteint32_t(0, machofp
); /* reserved */
915 rel_padcnt
= rel_base
- offset
;
916 offset
= rel_base
+ s_reloff
;
921 /* For a given chain of relocs r, write out the entire relocation
922 chain to the object file. */
924 static void macho_write_relocs (struct reloc
*r
)
929 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
932 word2
|= r
->pcrel
<< 24;
933 word2
|= r
->length
<< 25;
934 word2
|= r
->ext
<< 27;
935 word2
|= r
->type
<< 28;
936 fwriteint32_t(word2
, machofp
); /* reloc data */
942 /* Write out the section data. */
943 static void macho_write_section (void)
945 struct section
*s
, *s2
;
947 int8_t *rel_paddata
= "\0\0\0";
948 uint8_t fi
, *p
, *q
, blk
[4];
951 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
952 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
955 /* no padding needs to be done to the sections */
957 /* Like a.out Mach-O references things in the data or bss
958 * sections by addresses which are actually relative to the
959 * start of the _text_ section, in the _file_. See outaout.c
960 * for more information. */
962 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
963 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
967 /* get offset based on relocation type */
969 l
+= ((int32_t)*p
++) << 8;
971 if (r
->length
== 2) {
972 l
+= ((int32_t)*p
++) << 16;
973 l
+= ((int32_t)*p
++) << 24;
977 /* If the relocation is internal add to the current section
978 offset. Otherwise the only value we need is the symbol
979 offset which we already have. The linker takes care
980 of the rest of the address. */
982 /* add sizes of previous sections to current offset */
983 for (s2
= sects
, fi
= 1;
984 s2
!= NULL
&& fi
< r
->snum
; s2
= s2
->next
, fi
++)
985 if ((s2
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
989 /* write new offset back */
992 else if (r
->length
== 1)
997 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1000 /* dump the section data to file */
1001 saa_fpwrite(s
->data
, machofp
);
1004 /* pad last section up to reloc entries on int32_t boundary */
1005 fwrite(rel_paddata
, rel_padcnt
, 1, machofp
);
1007 /* emit relocation entries */
1008 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1009 macho_write_relocs (s
->relocs
);
1012 /* Write out the symbol table. We should already have sorted this
1014 static void macho_write_symtab (void)
1021 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1023 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1024 if ((sym
->type
& N_EXT
) == 0) {
1025 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1026 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1027 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1028 fwriteint16_t(sym
->desc
, machofp
); /* description */
1030 /* Fix up the symbol value now that we know the final section
1032 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1033 for (s
= sects
, fi
= 1;
1034 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1035 sym
->value
+= s
->size
;
1038 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1042 for (i
= 0; i
< nextdefsym
; i
++) {
1043 sym
= extdefsyms
[i
];
1044 fwriteint32_t(sym
->strx
, machofp
);
1045 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1046 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1047 fwriteint16_t(sym
->desc
, machofp
); /* description */
1049 /* Fix up the symbol value now that we know the final section
1051 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1052 for (s
= sects
, fi
= 1;
1053 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1054 sym
->value
+= s
->size
;
1057 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1060 for (i
= 0; i
< nundefsym
; i
++) {
1062 fwriteint32_t(sym
->strx
, machofp
);
1063 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1064 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1065 fwriteint16_t(sym
->desc
, machofp
); /* description */
1067 /* Fix up the symbol value now that we know the final section
1069 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1070 for (s
= sects
, fi
= 1;
1071 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1072 sym
->value
+= s
->size
;
1075 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1079 /* Fixup the snum in the relocation entries, we should be
1080 doing this only for externally undefined symbols. */
1081 static void macho_fixup_relocs (struct reloc
*r
)
1088 for (i
= 0; i
< nundefsym
; i
++) {
1090 if (sym
->initial_snum
== r
->snum
) {
1091 r
->snum
= sym
->snum
;
1099 /* Write out the object file. */
1101 static void macho_write (void)
1103 uint32_t offset
= 0;
1105 /* mach-o object file structure:
1111 ** uint32_t mach file type
1112 ** uint32_t number of load commands
1113 ** uint32_t size of all load commands
1114 ** (includes section struct size of segment command)
1118 ** uint32_t command type == LC_SEGMENT
1119 ** uint32_t size of load command
1120 ** (including section load commands)
1121 ** int8_t[16] segment name
1122 ** uint32_t in-memory offset
1123 ** uint32_t in-memory size
1124 ** uint32_t in-file offset to data area
1125 ** uint32_t in-file size
1126 ** (in-memory size excluding zerofill sections)
1127 ** int maximum vm protection
1128 ** int initial vm protection
1129 ** uint32_t number of sections
1133 ** int8_t[16] section name
1134 ** int8_t[16] segment name
1135 ** uint32_t in-memory offset
1136 ** uint32_t in-memory size
1137 ** uint32_t in-file offset
1138 ** uint32_t alignment
1139 ** (irrelevant in MH_OBJECT)
1140 ** uint32_t in-file offset of relocation entires
1141 ** uint32_t number of relocations
1143 ** uint32_t reserved
1144 ** uint32_t reserved
1146 ** symbol table command
1147 ** uint32_t command type == LC_SYMTAB
1148 ** uint32_t size of load command
1149 ** uint32_t symbol table offset
1150 ** uint32_t number of symbol table entries
1151 ** uint32_t string table offset
1152 ** uint32_t string table size
1156 ** padding to int32_t boundary
1158 ** relocation data (struct reloc)
1160 ** uint data (symbolnum, pcrel, length, extern, type)
1162 ** symbol table data (struct nlist)
1163 ** int32_t string table entry number
1165 ** (extern, absolute, defined in section)
1167 ** (0 for global symbols, section number of definition (>= 1, <=
1168 ** 254) for local symbols, size of variable for common symbols
1169 ** [type == extern])
1170 ** int16_t description
1171 ** (for stab debugging format)
1172 ** uint32_t value (i.e. file offset) of symbol or stab offset
1174 ** string table data
1175 ** list of null-terminated strings
1178 /* Emit the Mach-O header. */
1179 macho_write_header();
1181 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1183 /* emit the segment load command */
1185 offset
= macho_write_segment (offset
);
1187 error(ERR_WARNING
, "no sections?");
1190 /* write out symbol command */
1191 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1192 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1193 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1194 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1197 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1198 fwriteint32_t(offset
, machofp
); /* string table offset */
1199 fwriteint32_t(strslen
, machofp
); /* string table size */
1202 /* emit section data */
1204 macho_write_section ();
1206 /* emit symbol table if we have symbols */
1208 macho_write_symtab ();
1210 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1212 /* emit string table */
1213 saa_fpwrite(strs
, machofp
);
1215 /* We do quite a bit here, starting with finalizing all of the data
1216 for the object file, writing, and then freeing all of the data from
1219 static void macho_cleanup(int debuginfo
)
1227 /* Sort all symbols. */
1228 macho_layout_symbols (&nsyms
, &strslen
);
1230 /* Fixup relocation entries */
1231 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1232 macho_fixup_relocs (s
->relocs
);
1235 /* First calculate and finalize needed values. */
1236 macho_calculate_sizes();
1242 /* free up everything */
1243 while (sects
->next
) {
1245 sects
= sects
->next
;
1248 while (s
->relocs
!= NULL
) {
1250 s
->relocs
= s
->relocs
->next
;
1261 while (syms
->next
) {
1270 /* Debugging routines. */
1271 static void debug_reloc (struct reloc
*r
)
1273 fprintf (stdout
, "reloc:\n");
1274 fprintf (stdout
, "\taddr: %ld\n", r
->addr
);
1275 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1276 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1277 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1278 fprintf (stdout
, "\text: %d\n", r
->ext
);
1279 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1282 static void debug_section_relocs (struct section
*s
)
1284 struct reloc
*r
= s
->relocs
;
1286 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1294 struct ofmt of_macho
= {
1295 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",