output: combine macros for the most generic output formats
[nasm.git] / output / outmacho32.c
blob6b6cfaba95e51d2243d6e769b68324551ca356ce
1 /* outmacho32.c output routines for the Netwide Assembler to produce
2 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) 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 license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 /* Most of this file is, like Mach-O itself, based on a.out. For more
11 * guidelines see outaout.c. */
13 #include "compiler.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <inttypes.h>
21 #include "nasm.h"
22 #include "nasmlib.h"
23 #include "saa.h"
24 #include "raa.h"
25 #include "outform.h"
26 #include "compiler.h"
28 #if defined(OF_MACHO32)
30 /* Mach-O in-file header structure sizes */
31 #define MACHO_HEADER_SIZE (28)
32 #define MACHO_SEGCMD_SIZE (56)
33 #define MACHO_SECTCMD_SIZE (68)
34 #define MACHO_SYMCMD_SIZE (24)
35 #define MACHO_NLIST_SIZE (12)
36 #define MACHO_RELINFO_SIZE (8)
38 /* Mach-O file header values */
39 #define MH_MAGIC (0xfeedface)
40 #define CPU_TYPE_I386 (7) /* x86 platform */
41 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
42 #define MH_OBJECT (0x1) /* object file */
44 #define LC_SEGMENT (0x1) /* segment load command */
45 #define LC_SYMTAB (0x2) /* symbol table load command */
47 #define VM_PROT_NONE (0x00)
48 #define VM_PROT_READ (0x01)
49 #define VM_PROT_WRITE (0x02)
50 #define VM_PROT_EXECUTE (0x04)
52 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
53 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
55 struct section {
56 /* nasm internal data */
57 struct section *next;
58 struct SAA *data;
59 int32_t index;
60 struct reloc *relocs;
61 int align;
63 /* data that goes into the file */
64 char sectname[16]; /* what this section is called */
65 char segname[16]; /* segment this section will be in */
66 uint32_t size; /* in-memory and -file size */
67 uint32_t nreloc; /* relocation entry count */
68 uint32_t flags; /* type and attributes (masked) */
71 #define SECTION_TYPE 0x000000ff /* section type mask */
73 #define S_REGULAR (0x0) /* standard section */
74 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
76 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
77 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
78 machine instructions */
79 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
80 relocation entries */
81 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
82 relocation entries */
85 static struct sectmap {
86 const char *nasmsect;
87 const char *segname;
88 const char *sectname;
89 const int32_t flags;
90 } sectmap[] = {
91 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
92 {".data", "__DATA", "__data", S_REGULAR},
93 {".rodata", "__DATA", "__const", S_REGULAR},
94 {".bss", "__DATA", "__bss", S_ZEROFILL},
95 {NULL, NULL, NULL, 0}
98 struct reloc {
99 /* nasm internal data */
100 struct reloc *next;
102 /* data that goes into the file */
103 int32_t addr; /* op's offset in section */
104 unsigned int snum:24, /* contains symbol index if
105 ** ext otherwise in-file
106 ** section number */
107 pcrel:1, /* relative relocation */
108 length:2, /* 0=byte, 1=word, 2=int32_t */
109 ext:1, /* external symbol referenced */
110 type:4; /* reloc type, 0 for us */
113 #define R_ABS 0 /* absolute relocation */
114 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
115 ** highest bit == 1 */
117 struct symbol {
118 /* nasm internal data */
119 struct symbol *next; /* next symbol in the list */
120 char *name; /* name of this symbol */
121 int32_t initial_snum; /* symbol number used above in
122 reloc */
123 int32_t snum; /* true snum for reloc */
125 /* data that goes into the file */
126 int32_t strx; /* string table index */
127 uint8_t type; /* symbol type */
128 uint8_t sect; /* NO_SECT or section number */
129 int16_t desc; /* for stab debugging, 0 for us */
130 uint32_t value; /* offset of symbol in section */
133 /* symbol type bits */
134 #define N_EXT 0x01 /* global or external symbol */
136 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
137 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
138 #define N_SECT 0xe /* defined symbol, n_sect holds
139 ** section number */
141 #define N_TYPE 0x0e /* type bit mask */
143 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
145 /* special section number values */
146 #define NO_SECT 0 /* no section, invalid */
147 #define MAX_SECT 255 /* maximum number of sections */
149 static struct section *sects, **sectstail;
150 static struct symbol *syms, **symstail;
151 static uint32_t nsyms;
153 /* These variables are set by macho_layout_symbols() to organize
154 the symbol table and string table in order the dynamic linker
155 expects. They are then used in macho_write() to put out the
156 symbols and strings in that order.
158 The order of the symbol table is:
159 local symbols
160 defined external symbols (sorted by name)
161 undefined external symbols (sorted by name)
163 The order of the string table is:
164 strings for external symbols
165 strings for local symbols
167 static uint32_t ilocalsym = 0;
168 static uint32_t iextdefsym = 0;
169 static uint32_t iundefsym = 0;
170 static uint32_t nlocalsym;
171 static uint32_t nextdefsym;
172 static uint32_t nundefsym;
173 static struct symbol **extdefsyms = NULL;
174 static struct symbol **undefsyms = NULL;
176 static struct RAA *extsyms;
177 static struct SAA *strs;
178 static uint32_t strslen;
180 static FILE *machofp;
181 static efunc error;
182 static evalfunc evaluate;
184 extern struct ofmt of_macho;
186 /* Global file information. This should be cleaned up into either
187 a structure or as function arguments. */
188 uint32_t head_ncmds = 0;
189 uint32_t head_sizeofcmds = 0;
190 uint32_t seg_filesize = 0;
191 uint32_t seg_vmsize = 0;
192 uint32_t seg_nsects = 0;
193 uint32_t rel_padcnt = 0;
196 #define xstrncpy(xdst, xsrc) \
197 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
198 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
199 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
201 #define align(x, y) \
202 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
204 #define alignint32_t(x) \
205 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
207 static void debug_reloc (struct reloc *);
208 static void debug_section_relocs (struct section *) _unused;
210 static int exact_log2 (uint32_t align)
212 if (align == 0) {
213 return 0;
214 } else if (align & (align-1)) {
215 return -1; /* Not a power of 2 */
216 } else {
217 #ifdef HAVE_GNUC_4
218 return __builtin_ctzl (align);
219 #else
220 uint32_t result = 0;
222 /* We know exactly one bit is set at this point. */
223 if (align & 0xffff0000)
224 result |= 16;
225 if (align & 0xff00ff00)
226 result |= 8;
227 if (align & 0xf0f0f0f0)
228 result |= 4;
229 if (align & 0xcccccccc)
230 result |= 2;
231 if (align & 0xaaaaaaaa)
232 result |= 1;
234 return result;
235 #endif
239 static struct section *get_section_by_name(const char *segname,
240 const char *sectname)
242 struct section *s;
244 for (s = sects; s != NULL; s = s->next)
245 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
246 break;
248 return s;
251 static struct section *get_section_by_index(const int32_t index)
253 struct section *s;
255 for (s = sects; s != NULL; s = s->next)
256 if (index == s->index)
257 break;
259 return s;
262 static int32_t get_section_index_by_name(const char *segname,
263 const char *sectname)
265 struct section *s;
267 for (s = sects; s != NULL; s = s->next)
268 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
269 return s->index;
271 return -1;
274 static char *get_section_name_by_index(const int32_t index)
276 struct section *s;
278 for (s = sects; s != NULL; s = s->next)
279 if (index == s->index)
280 return s->sectname;
282 return NULL;
285 static uint8_t get_section_fileindex_by_index(const int32_t index)
287 struct section *s;
288 uint8_t i = 1;
290 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
291 if (index == s->index)
292 return i;
294 if (i == MAX_SECT)
295 error(ERR_WARNING,
296 "too many sections (>255) - clipped by fileindex");
298 return NO_SECT;
301 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
302 evalfunc eval)
304 char zero = 0;
306 machofp = fp;
307 error = errfunc;
308 evaluate = eval;
310 (void)ldef; /* placate optimisers */
312 sects = NULL;
313 sectstail = &sects;
315 syms = NULL;
316 symstail = &syms;
317 nsyms = 0;
318 nlocalsym = 0;
319 nextdefsym = 0;
320 nundefsym = 0;
322 extsyms = raa_init();
323 strs = saa_init(1L);
325 /* string table starts with a zero byte - don't ask why */
326 saa_wbytes(strs, &zero, sizeof(char));
327 strslen = 1;
330 static int macho_setinfo(enum geninfo type, char **val)
332 (void)type;
333 (void)val;
334 return 0;
337 static void sect_write(struct section *sect,
338 const uint8_t *data, uint32_t len)
340 saa_wbytes(sect->data, data, len);
341 sect->size += len;
344 static void add_reloc(struct section *sect, int32_t section,
345 int pcrel, int bytes)
347 struct reloc *r;
348 int32_t fi;
350 /* NeXT as puts relocs in reversed order (address-wise) into the
351 ** files, so we do the same, doesn't seem to make much of a
352 ** difference either way */
353 r = nasm_malloc(sizeof(struct reloc));
354 r->next = sect->relocs;
355 sect->relocs = r;
357 /* the current end of the section will be the symbol's address for
358 ** now, might have to be fixed by macho_fixup_relocs() later on. make
359 ** sure we don't make the symbol scattered by setting the highest
360 ** bit by accident */
361 r->addr = sect->size & ~R_SCATTERED;
362 r->ext = 0;
363 r->pcrel = pcrel;
365 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
366 r->length = bytes >> 1;
368 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
369 r->type = 0;
371 if (section == NO_SEG) {
372 /* absolute local symbol if no section index given */
373 r->snum = R_ABS;
374 } else {
375 fi = get_section_fileindex_by_index(section);
377 if (fi == NO_SECT) {
378 /* external symbol if no section with that index known,
379 ** symbol number was saved in macho_symdef() */
380 r->snum = raa_read(extsyms, section);
381 r->ext = 1;
382 } else {
383 /* local symbol in section fi */
384 r->snum = fi;
388 ++sect->nreloc;
391 static void macho_output(int32_t secto, const void *data,
392 enum out_type type, uint64_t size,
393 int32_t section, int32_t wrt)
395 struct section *s, *sbss;
396 int32_t addr;
397 uint8_t mydata[4], *p;
399 if (wrt != NO_SEG) {
400 wrt = NO_SEG;
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 "
408 "[ABSOLUTE] space");
410 return;
413 s = get_section_by_index(secto);
415 if (s == NULL) {
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 */
421 if (s == NULL)
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");
431 switch (type) {
432 case OUT_REL2ADR:
433 size = 2;
434 break;
436 case OUT_REL4ADR:
437 size = 4;
438 break;
440 default:
441 break;
444 s->size += size;
445 return;
448 switch (type) {
449 case OUT_RESERVE:
450 if (s != sbss) {
451 error(ERR_WARNING, "uninitialized space declared in"
452 " %s section: zeroing",
453 get_section_name_by_index(secto));
455 sect_write(s, NULL, size);
456 } else
457 s->size += size;
459 break;
461 case OUT_RAWDATA:
462 if (section != NO_SEG)
463 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
465 sect_write(s, data, size);
466 break;
468 case OUT_ADDRESS:
469 addr = *(int64_t *)data;
471 if (section != NO_SEG) {
472 if (section % 2) {
473 error(ERR_NONFATAL, "Mach-O format does not support"
474 " section base references");
475 } else
476 add_reloc(s, section, 0, size);
479 p = mydata;
480 WRITEADDR(p, addr, size);
481 sect_write(s, mydata, size);
482 break;
484 case OUT_REL2ADR:
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");
491 } else
492 add_reloc(s, section, 1, 2);
494 p = mydata;
495 WRITESHORT(p, *(int32_t *)data - (size + s->size));
496 sect_write(s, mydata, 2L);
497 break;
499 case OUT_REL4ADR:
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");
506 } else
507 add_reloc(s, section, 1, 4);
509 p = mydata;
510 WRITELONG(p, *(int32_t *)data - (size + s->size));
511 sect_write(s, mydata, 4L);
512 break;
514 default:
515 error(ERR_PANIC, "unknown output type?");
516 break;
520 static int32_t macho_section(char *name, int pass, int *bits)
522 int32_t index, originalIndex;
523 char *sectionAttributes;
524 struct sectmap *sm;
525 struct section *s;
527 (void)pass;
529 /* Default to 32 bits. */
530 if (!name) {
531 *bits = 32;
532 name = ".text";
533 sectionAttributes = NULL;
534 } else {
535 sectionAttributes = name;
536 name = nasm_strsep(&sectionAttributes, " \t");
539 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
540 /* make lookup into section name translation table */
541 if (!strcmp(name, sm->nasmsect)) {
542 char *currentAttribute;
544 /* try to find section with that name */
545 originalIndex = index = get_section_index_by_name(sm->segname,
546 sm->sectname);
548 /* create it if it doesn't exist yet */
549 if (index == -1) {
550 s = *sectstail = nasm_malloc(sizeof(struct section));
551 s->next = NULL;
552 sectstail = &s->next;
554 s->data = saa_init(1L);
555 s->index = seg_alloc();
556 s->relocs = NULL;
557 s->align = -1;
559 xstrncpy(s->segname, sm->segname);
560 xstrncpy(s->sectname, sm->sectname);
561 s->size = 0;
562 s->nreloc = 0;
563 s->flags = sm->flags;
565 index = s->index;
566 } else {
567 s = get_section_by_index(index);
570 while ((NULL != sectionAttributes)
571 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
572 if (0 != *currentAttribute) {
573 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
574 char *end;
575 int newAlignment, value;
577 value = strtoul(currentAttribute + 6, (char**)&end, 0);
578 newAlignment = exact_log2(value);
580 if (0 != *end) {
581 error(ERR_PANIC,
582 "unknown or missing alignment value \"%s\" "
583 "specified for section \"%s\"",
584 currentAttribute + 6,
585 name);
586 return NO_SEG;
587 } else if (0 > newAlignment) {
588 error(ERR_PANIC,
589 "alignment of %d (for section \"%s\") is not "
590 "a power of two",
591 value,
592 name);
593 return NO_SEG;
596 if ((-1 != originalIndex)
597 && (s->align != newAlignment)
598 && (s->align != -1)) {
599 error(ERR_PANIC,
600 "section \"%s\" has already been specified "
601 "with alignment %d, conflicts with new "
602 "alignment of %d",
603 name,
604 (1 << s->align),
605 value);
606 return NO_SEG;
609 s->align = newAlignment;
610 } else if (!nasm_stricmp("data", currentAttribute)) {
611 /* Do nothing; 'data' is implicit */
612 } else {
613 error(ERR_PANIC,
614 "unknown section attribute %s for section %s",
615 currentAttribute,
616 name);
617 return NO_SEG;
622 return index;
626 error(ERR_PANIC, "invalid section name %s", name);
627 return NO_SEG;
630 static void macho_symdef(char *name, int32_t section, int64_t offset,
631 int is_global, char *special)
633 struct symbol *sym;
635 if (special) {
636 error(ERR_NONFATAL, "The Mach-O output format does "
637 "not support any special symbol types");
638 return;
641 if (is_global == 3) {
642 error(ERR_NONFATAL, "The Mach-O format does not "
643 "(yet) support forward reference fixups.");
644 return;
647 sym = *symstail = nasm_malloc(sizeof(struct symbol));
648 sym->next = NULL;
649 symstail = &sym->next;
651 sym->name = name;
652 sym->strx = strslen;
653 sym->type = 0;
654 sym->desc = 0;
655 sym->value = offset;
656 sym->initial_snum = -1;
658 /* external and common symbols get N_EXT */
659 if (is_global != 0)
660 sym->type |= N_EXT;
662 if (section == NO_SEG) {
663 /* symbols in no section get absolute */
664 sym->type |= N_ABS;
665 sym->sect = NO_SECT;
666 } else {
667 sym->type |= N_SECT;
669 /* get the in-file index of the section the symbol was defined in */
670 sym->sect = get_section_fileindex_by_index(section);
672 if (sym->sect == NO_SECT) {
673 /* remember symbol number of references to external
674 ** symbols, this works because every external symbol gets
675 ** its own section number allocated internally by nasm and
676 ** can so be used as a key */
677 extsyms = raa_write(extsyms, section, nsyms);
678 sym->initial_snum = nsyms;
680 switch (is_global) {
681 case 1:
682 case 2:
683 /* there isn't actually a difference between global
684 ** and common symbols, both even have their size in
685 ** sym->value */
686 sym->type = N_EXT;
687 break;
689 default:
690 /* give an error on unfound section if it's not an
691 ** external or common symbol (assemble_file() does a
692 ** seg_alloc() on every call for them) */
693 error(ERR_PANIC, "in-file index for section %d not found",
694 section);
699 ++nsyms;
702 static int32_t macho_segbase(int32_t section)
704 return section;
707 static int macho_directive(char *directive, char *value, int pass)
709 (void)directive;
710 (void)value;
711 (void)pass;
712 return 0;
715 static void macho_filename(char *inname, char *outname, efunc error)
717 standard_extension(inname, outname, ".o", error);
720 extern macros_t generic_stdmac[];
722 /* Comparison function for qsort symbol layout. */
723 static int layout_compare (const struct symbol **s1,
724 const struct symbol **s2)
726 return (strcmp ((*s1)->name, (*s2)->name));
729 /* The native assembler does a few things in a similar function
731 * Remove temporary labels
732 * Sort symbols according to local, external, undefined (by name)
733 * Order the string table
735 We do not remove temporary labels right now.
737 numsyms is the total number of symbols we have. strtabsize is the
738 number entries in the string table. */
740 static void macho_layout_symbols (uint32_t *numsyms,
741 uint32_t *strtabsize)
743 struct symbol *sym, **symp;
744 uint32_t i,j;
746 *numsyms = 0;
747 *strtabsize = sizeof (char);
749 symp = &syms;
751 while ((sym = *symp)) {
752 /* Undefined symbols are now external. */
753 if (sym->type == N_UNDF)
754 sym->type |= N_EXT;
756 if ((sym->type & N_EXT) == 0) {
757 sym->snum = *numsyms;
758 *numsyms = *numsyms + 1;
759 nlocalsym++;
761 else {
762 if ((sym->type & N_TYPE) != N_UNDF)
763 nextdefsym++;
764 else
765 nundefsym++;
767 /* If we handle debug info we'll want
768 to check for it here instead of just
769 adding the symbol to the string table. */
770 sym->strx = *strtabsize;
771 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
772 *strtabsize += strlen(sym->name) + 1;
774 symp = &(sym->next);
777 /* Next, sort the symbols. Most of this code is a direct translation from
778 the Apple cctools symbol layout. We need to keep compatibility with that. */
779 /* Set the indexes for symbol groups into the symbol table */
780 ilocalsym = 0;
781 iextdefsym = nlocalsym;
782 iundefsym = nlocalsym + nextdefsym;
784 /* allocate arrays for sorting externals by name */
785 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
786 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
788 i = 0;
789 j = 0;
791 symp = &syms;
793 while ((sym = *symp)) {
795 if((sym->type & N_EXT) == 0) {
796 sym->strx = *strtabsize;
797 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
798 *strtabsize += strlen(sym->name) + 1;
800 else {
801 if((sym->type & N_TYPE) != N_UNDF)
802 extdefsyms[i++] = sym;
803 else
804 undefsyms[j++] = sym;
806 symp = &(sym->next);
809 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
810 (int (*)(const void *, const void *))layout_compare);
811 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
812 (int (*)(const void *, const void *))layout_compare);
814 for(i = 0; i < nextdefsym; i++) {
815 extdefsyms[i]->snum = *numsyms;
816 *numsyms += 1;
818 for(j = 0; j < nundefsym; j++) {
819 undefsyms[j]->snum = *numsyms;
820 *numsyms += 1;
824 /* Calculate some values we'll need for writing later. */
826 static void macho_calculate_sizes (void)
828 struct section *s;
830 /* count sections and calculate in-memory and in-file offsets */
831 for (s = sects; s != NULL; s = s->next) {
832 /* zerofill sections aren't actually written to the file */
833 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
834 seg_filesize += s->size;
836 seg_vmsize += s->size;
837 ++seg_nsects;
840 /* calculate size of all headers, load commands and sections to
841 ** get a pointer to the start of all the raw data */
842 if (seg_nsects > 0) {
843 ++head_ncmds;
844 head_sizeofcmds +=
845 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
848 if (nsyms > 0) {
849 ++head_ncmds;
850 head_sizeofcmds += MACHO_SYMCMD_SIZE;
854 /* Write out the header information for the file. */
856 static void macho_write_header (void)
858 fwriteint32_t(MH_MAGIC, machofp); /* magic */
859 fwriteint32_t(CPU_TYPE_I386, machofp); /* CPU type */
860 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
861 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
862 fwriteint32_t(head_ncmds, machofp); /* number of load commands */
863 fwriteint32_t(head_sizeofcmds, machofp); /* size of load commands */
864 fwriteint32_t(0, machofp); /* no flags */
867 /* Write out the segment load command at offset. */
869 static uint32_t macho_write_segment (uint32_t offset)
871 uint32_t s_addr = 0;
872 uint32_t rel_base = alignint32_t (offset + seg_filesize);
873 uint32_t s_reloff = 0;
874 struct section *s;
876 fwriteint32_t(LC_SEGMENT, machofp); /* cmd == LC_SEGMENT */
878 /* size of load command including section load commands */
879 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
880 MACHO_SECTCMD_SIZE, machofp);
882 /* in an MH_OBJECT file all sections are in one unnamed (name
883 ** all zeros) segment */
884 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp);
885 fwriteint32_t(0, machofp); /* in-memory offset */
886 fwriteint32_t(seg_vmsize, machofp); /* in-memory size */
887 fwriteint32_t(offset, machofp); /* in-file offset to data */
888 fwriteint32_t(seg_filesize, machofp); /* in-file size */
889 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
890 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
891 fwriteint32_t(seg_nsects, machofp); /* number of sections */
892 fwriteint32_t(0, machofp); /* no flags */
894 /* emit section headers */
895 for (s = sects; s != NULL; s = s->next) {
896 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
897 fwrite(s->segname, sizeof(s->segname), 1, machofp);
898 fwriteint32_t(s_addr, machofp);
899 fwriteint32_t(s->size, machofp);
901 /* dummy data for zerofill sections or proper values */
902 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
903 fwriteint32_t(offset, machofp);
904 /* Write out section alignment, as a power of two.
905 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
906 if (s->align == -1)
907 s->align = DEFAULT_SECTION_ALIGNMENT;
908 fwriteint32_t(s->align, machofp);
909 /* To be compatible with cctools as we emit
910 a zero reloff if we have no relocations. */
911 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
912 fwriteint32_t(s->nreloc, machofp);
914 offset += s->size;
915 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
916 } else {
917 fwriteint32_t(0, machofp);
918 fwriteint32_t(0, machofp);
919 fwriteint32_t(0, machofp);
920 fwriteint32_t(0, machofp);
923 fwriteint32_t(s->flags, machofp); /* flags */
924 fwriteint32_t(0, machofp); /* reserved */
925 fwriteint32_t(0, machofp); /* reserved */
927 s_addr += s->size;
930 rel_padcnt = rel_base - offset;
931 offset = rel_base + s_reloff;
933 return offset;
936 /* For a given chain of relocs r, write out the entire relocation
937 chain to the object file. */
939 static void macho_write_relocs (struct reloc *r)
941 while (r) {
942 uint32_t word2;
944 fwriteint32_t(r->addr, machofp); /* reloc offset */
946 word2 = r->snum;
947 word2 |= r->pcrel << 24;
948 word2 |= r->length << 25;
949 word2 |= r->ext << 27;
950 word2 |= r->type << 28;
951 fwriteint32_t(word2, machofp); /* reloc data */
953 r = r->next;
957 /* Write out the section data. */
958 static void macho_write_section (void)
960 struct section *s, *s2;
961 struct reloc *r;
962 char *rel_paddata = "\0\0\0";
963 uint8_t fi, *p, *q, blk[4];
964 int32_t l;
966 for (s = sects; s != NULL; s = s->next) {
967 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
968 continue;
970 /* no padding needs to be done to the sections */
972 /* Like a.out Mach-O references things in the data or bss
973 * sections by addresses which are actually relative to the
974 * start of the _text_ section, in the _file_. See outaout.c
975 * for more information. */
976 saa_rewind(s->data);
977 for (r = s->relocs; r != NULL; r = r->next) {
978 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
979 p = q = blk;
980 l = *p++;
982 /* get offset based on relocation type */
983 if (r->length > 0) {
984 l += ((int32_t)*p++) << 8;
986 if (r->length == 2) {
987 l += ((int32_t)*p++) << 16;
988 l += ((int32_t)*p++) << 24;
992 /* If the relocation is internal add to the current section
993 offset. Otherwise the only value we need is the symbol
994 offset which we already have. The linker takes care
995 of the rest of the address. */
996 if (!r->ext) {
997 /* add sizes of previous sections to current offset */
998 for (s2 = sects, fi = 1;
999 s2 != NULL && fi < r->snum; s2 = s2->next, fi++)
1000 l += s2->size;
1003 /* write new offset back */
1004 if (r->length == 2)
1005 WRITELONG(q, l);
1006 else if (r->length == 1)
1007 WRITESHORT(q, l);
1008 else
1009 *q++ = l & 0xFF;
1011 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1014 /* dump the section data to file */
1015 saa_fpwrite(s->data, machofp);
1018 /* pad last section up to reloc entries on int32_t boundary */
1019 fwrite(rel_paddata, rel_padcnt, 1, machofp);
1021 /* emit relocation entries */
1022 for (s = sects; s != NULL; s = s->next)
1023 macho_write_relocs (s->relocs);
1026 /* Write out the symbol table. We should already have sorted this
1027 before now. */
1028 static void macho_write_symtab (void)
1030 struct symbol *sym;
1031 struct section *s;
1032 int32_t fi;
1033 uint32_t i;
1035 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1037 for (sym = syms; sym != NULL; sym = sym->next) {
1038 if ((sym->type & N_EXT) == 0) {
1039 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1040 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1041 fwrite(&sym->sect, 1, 1, machofp); /* section */
1042 fwriteint16_t(sym->desc, machofp); /* description */
1044 /* Fix up the symbol value now that we know the final section
1045 sizes. */
1046 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1047 for (s = sects, fi = 1;
1048 s != NULL && fi < sym->sect; s = s->next, ++fi)
1049 sym->value += s->size;
1052 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1056 for (i = 0; i < nextdefsym; i++) {
1057 sym = extdefsyms[i];
1058 fwriteint32_t(sym->strx, machofp);
1059 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1060 fwrite(&sym->sect, 1, 1, machofp); /* section */
1061 fwriteint16_t(sym->desc, machofp); /* description */
1063 /* Fix up the symbol value now that we know the final section
1064 sizes. */
1065 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1066 for (s = sects, fi = 1;
1067 s != NULL && fi < sym->sect; s = s->next, ++fi)
1068 sym->value += s->size;
1071 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1074 for (i = 0; i < nundefsym; i++) {
1075 sym = undefsyms[i];
1076 fwriteint32_t(sym->strx, machofp);
1077 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1078 fwrite(&sym->sect, 1, 1, machofp); /* section */
1079 fwriteint16_t(sym->desc, machofp); /* description */
1081 /* Fix up the symbol value now that we know the final section
1082 sizes. */
1083 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1084 for (s = sects, fi = 1;
1085 s != NULL && fi < sym->sect; s = s->next, ++fi)
1086 sym->value += s->size;
1089 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1093 /* Fixup the snum in the relocation entries, we should be
1094 doing this only for externally undefined symbols. */
1095 static void macho_fixup_relocs (struct reloc *r)
1097 struct symbol *sym;
1098 uint32_t i;
1100 while (r != NULL) {
1101 if (r->ext) {
1102 for (i = 0; i < nundefsym; i++) {
1103 sym = undefsyms[i];
1104 if (sym->initial_snum == r->snum) {
1105 r->snum = sym->snum;
1106 break;
1110 r = r->next;
1114 /* Write out the object file. */
1116 static void macho_write (void)
1118 uint32_t offset = 0;
1120 /* mach-o object file structure:
1122 ** mach header
1123 ** uint32_t magic
1124 ** int cpu type
1125 ** int cpu subtype
1126 ** uint32_t mach file type
1127 ** uint32_t number of load commands
1128 ** uint32_t size of all load commands
1129 ** (includes section struct size of segment command)
1130 ** uint32_t flags
1132 ** segment command
1133 ** uint32_t command type == LC_SEGMENT
1134 ** uint32_t size of load command
1135 ** (including section load commands)
1136 ** char[16] segment name
1137 ** uint32_t in-memory offset
1138 ** uint32_t in-memory size
1139 ** uint32_t in-file offset to data area
1140 ** uint32_t in-file size
1141 ** (in-memory size excluding zerofill sections)
1142 ** int maximum vm protection
1143 ** int initial vm protection
1144 ** uint32_t number of sections
1145 ** uint32_t flags
1147 ** section commands
1148 ** char[16] section name
1149 ** char[16] segment name
1150 ** uint32_t in-memory offset
1151 ** uint32_t in-memory size
1152 ** uint32_t in-file offset
1153 ** uint32_t alignment
1154 ** (irrelevant in MH_OBJECT)
1155 ** uint32_t in-file offset of relocation entires
1156 ** uint32_t number of relocations
1157 ** uint32_t flags
1158 ** uint32_t reserved
1159 ** uint32_t reserved
1161 ** symbol table command
1162 ** uint32_t command type == LC_SYMTAB
1163 ** uint32_t size of load command
1164 ** uint32_t symbol table offset
1165 ** uint32_t number of symbol table entries
1166 ** uint32_t string table offset
1167 ** uint32_t string table size
1169 ** raw section data
1171 ** padding to int32_t boundary
1173 ** relocation data (struct reloc)
1174 ** int32_t offset
1175 ** uint data (symbolnum, pcrel, length, extern, type)
1177 ** symbol table data (struct nlist)
1178 ** int32_t string table entry number
1179 ** uint8_t type
1180 ** (extern, absolute, defined in section)
1181 ** uint8_t section
1182 ** (0 for global symbols, section number of definition (>= 1, <=
1183 ** 254) for local symbols, size of variable for common symbols
1184 ** [type == extern])
1185 ** int16_t description
1186 ** (for stab debugging format)
1187 ** uint32_t value (i.e. file offset) of symbol or stab offset
1189 ** string table data
1190 ** list of null-terminated strings
1193 /* Emit the Mach-O header. */
1194 macho_write_header();
1196 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1198 /* emit the segment load command */
1199 if (seg_nsects > 0)
1200 offset = macho_write_segment (offset);
1201 else
1202 error(ERR_WARNING, "no sections?");
1204 if (nsyms > 0) {
1205 /* write out symbol command */
1206 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1207 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1208 fwriteint32_t(offset, machofp); /* symbol table offset */
1209 fwriteint32_t(nsyms, machofp); /* number of symbol
1210 ** table entries */
1212 offset += nsyms * MACHO_NLIST_SIZE;
1213 fwriteint32_t(offset, machofp); /* string table offset */
1214 fwriteint32_t(strslen, machofp); /* string table size */
1217 /* emit section data */
1218 if (seg_nsects > 0)
1219 macho_write_section ();
1221 /* emit symbol table if we have symbols */
1222 if (nsyms > 0)
1223 macho_write_symtab ();
1225 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1227 /* emit string table */
1228 saa_fpwrite(strs, machofp);
1230 /* We do quite a bit here, starting with finalizing all of the data
1231 for the object file, writing, and then freeing all of the data from
1232 the file. */
1234 static void macho_cleanup(int debuginfo)
1236 struct section *s;
1237 struct reloc *r;
1238 struct symbol *sym;
1240 (void)debuginfo;
1242 /* Sort all symbols. */
1243 macho_layout_symbols (&nsyms, &strslen);
1245 /* Fixup relocation entries */
1246 for (s = sects; s != NULL; s = s->next) {
1247 macho_fixup_relocs (s->relocs);
1250 /* First calculate and finalize needed values. */
1251 macho_calculate_sizes();
1252 macho_write();
1254 /* done - yay! */
1255 fclose(machofp);
1257 /* free up everything */
1258 while (sects->next) {
1259 s = sects;
1260 sects = sects->next;
1262 saa_free(s->data);
1263 while (s->relocs != NULL) {
1264 r = s->relocs;
1265 s->relocs = s->relocs->next;
1266 nasm_free(r);
1269 nasm_free(s);
1272 saa_free(strs);
1273 raa_free(extsyms);
1275 if (syms) {
1276 while (syms->next) {
1277 sym = syms;
1278 syms = syms->next;
1280 nasm_free (sym);
1285 /* Debugging routines. */
1286 static void debug_reloc (struct reloc *r)
1288 fprintf (stdout, "reloc:\n");
1289 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1290 fprintf (stdout, "\tsnum: %d\n", r->snum);
1291 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1292 fprintf (stdout, "\tlength: %d\n", r->length);
1293 fprintf (stdout, "\text: %d\n", r->ext);
1294 fprintf (stdout, "\ttype: %d\n", r->type);
1297 static void debug_section_relocs (struct section *s)
1299 struct reloc *r = s->relocs;
1301 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1303 while (r != NULL) {
1304 debug_reloc (r);
1305 r = r->next;
1309 struct ofmt of_macho32 = {
1310 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1311 "macho32",
1312 NULL,
1313 null_debug_arr,
1314 &null_debug_form,
1315 generic_stdmac,
1316 macho_init,
1317 macho_setinfo,
1318 macho_output,
1319 macho_symdef,
1320 macho_section,
1321 macho_segbase,
1322 macho_directive,
1323 macho_filename,
1324 macho_cleanup
1327 struct ofmt of_macho = {
1328 "MACHO (short name for MACHO32)",
1329 "macho",
1330 NULL,
1331 null_debug_arr,
1332 &null_debug_form,
1333 generic_stdmac,
1334 macho_init,
1335 macho_setinfo,
1336 macho_output,
1337 macho_symdef,
1338 macho_section,
1339 macho_segbase,
1340 macho_directive,
1341 macho_filename,
1342 macho_cleanup
1345 #endif
1348 * Local Variables:
1349 * mode:c
1350 * c-basic-offset:4
1351 * End:
1353 * end of file */