Version 0.99.03
[nasm/avx512.git] / output / outmacho.c
blob86700f8e5d91c9d75c76e507b6df3dbb335bf04c
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.
8 */
10 /* Most of this file is, like Mach-O itself, based on a.out. For more
11 * guidelines see outaout.c. */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <inttypes.h>
19 #include "nasm.h"
20 #include "nasmlib.h"
21 #include "outform.h"
22 #include "compiler.h"
24 #if defined(OF_MACHO)
26 /* Mach-O in-file header structure sizes */
27 #define MACHO_HEADER_SIZE (28)
28 #define MACHO_SEGCMD_SIZE (56)
29 #define MACHO_SECTCMD_SIZE (68)
30 #define MACHO_SYMCMD_SIZE (24)
31 #define MACHO_NLIST_SIZE (12)
32 #define MACHO_RELINFO_SIZE (8)
34 /* Mach-O file header values */
35 #define MH_MAGIC (0xfeedface)
36 #define CPU_TYPE_I386 (7) /* x86 platform */
37 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
38 #define MH_OBJECT (0x1) /* object file */
40 #define LC_SEGMENT (0x1) /* segment load command */
41 #define LC_SYMTAB (0x2) /* symbol table load command */
43 #define VM_PROT_NONE (0x00)
44 #define VM_PROT_READ (0x01)
45 #define VM_PROT_WRITE (0x02)
46 #define VM_PROT_EXECUTE (0x04)
48 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
49 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
51 struct section {
52 /* nasm internal data */
53 struct section *next;
54 struct SAA *data;
55 int32_t index;
56 struct reloc *relocs;
57 int align;
59 /* data that goes into the file */
60 char sectname[16]; /* what this section is called */
61 char segname[16]; /* segment this section will be in */
62 uint32_t size; /* in-memory and -file size */
63 uint32_t nreloc; /* relocation entry count */
64 uint32_t flags; /* type and attributes (masked) */
67 #define SECTION_TYPE 0x000000ff /* section type mask */
69 #define S_REGULAR (0x0) /* standard section */
70 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
72 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
73 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
74 machine instructions */
75 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
76 relocation entries */
77 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
78 relocation entries */
81 static struct sectmap {
82 const char *nasmsect;
83 const char *segname;
84 const char *sectname;
85 const int32_t flags;
86 } sectmap[] = {
87 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
88 {".data", "__DATA", "__data", S_REGULAR},
89 {".rodata", "__DATA", "__const", S_REGULAR},
90 {".bss", "__DATA", "__bss", S_ZEROFILL},
91 {NULL, NULL, NULL, 0}
94 struct reloc {
95 /* nasm internal data */
96 struct reloc *next;
98 /* data that goes into the file */
99 int32_t addr; /* op's offset in section */
100 unsigned int snum:24, /* contains symbol index if
101 ** ext otherwise in-file
102 ** section number */
103 pcrel:1, /* relative relocation */
104 length:2, /* 0=byte, 1=word, 2=int32_t */
105 ext:1, /* external symbol referenced */
106 type:4; /* reloc type, 0 for us */
109 #define R_ABS 0 /* absolute relocation */
110 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
111 ** highest bit == 1 */
113 struct symbol {
114 /* nasm internal data */
115 struct symbol *next; /* next symbol in the list */
116 char *name; /* name of this symbol */
117 int32_t initial_snum; /* symbol number used above in
118 reloc */
119 int32_t snum; /* true snum for reloc */
121 /* data that goes into the file */
122 int32_t strx; /* string table index */
123 uint8_t type; /* symbol type */
124 uint8_t sect; /* NO_SECT or section number */
125 int16_t desc; /* for stab debugging, 0 for us */
126 uint32_t value; /* offset of symbol in section */
129 /* symbol type bits */
130 #define N_EXT 0x01 /* global or external symbol */
132 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
133 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
134 #define N_SECT 0xe /* defined symbol, n_sect holds
135 ** section number */
137 #define N_TYPE 0x0e /* type bit mask */
139 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
141 /* special section number values */
142 #define NO_SECT 0 /* no section, invalid */
143 #define MAX_SECT 255 /* maximum number of sections */
145 static struct section *sects, **sectstail;
146 static struct symbol *syms, **symstail;
147 static uint32_t nsyms;
149 /* These variables are set by macho_layout_symbols() to organize
150 the symbol table and string table in order the dynamic linker
151 expects. They are then used in macho_write() to put out the
152 symbols and strings in that order.
154 The order of the symbol table is:
155 local symbols
156 defined external symbols (sorted by name)
157 undefined external symbols (sorted by name)
159 The order of the string table is:
160 strings for external symbols
161 strings for local symbols
163 static uint32_t ilocalsym = 0;
164 static uint32_t iextdefsym = 0;
165 static uint32_t iundefsym = 0;
166 static uint32_t nlocalsym;
167 static uint32_t nextdefsym;
168 static uint32_t nundefsym;
169 static struct symbol **extdefsyms = NULL;
170 static struct symbol **undefsyms = NULL;
172 static struct RAA *extsyms;
173 static struct SAA *strs;
174 static uint32_t strslen;
176 static FILE *machofp;
177 static efunc error;
178 static evalfunc evaluate;
180 extern struct ofmt of_macho;
182 /* Global file information. This should be cleaned up into either
183 a structure or as function arguments. */
184 uint32_t head_ncmds = 0;
185 uint32_t head_sizeofcmds = 0;
186 uint32_t seg_filesize = 0;
187 uint32_t seg_vmsize = 0;
188 uint32_t seg_nsects = 0;
189 uint32_t rel_padcnt = 0;
192 #define xstrncpy(xdst, xsrc) \
193 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
194 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
195 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
197 #define align(x, y) \
198 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
200 #define alignint32_t(x) \
201 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
203 static void debug_reloc (struct reloc *);
204 static void debug_section_relocs (struct section *) _unused;
206 static int exact_log2 (uint32_t align)
208 if (align == 0) {
209 return 0;
210 } else if (align & (align-1)) {
211 return -1; /* Not a power of 2 */
212 } else {
213 #ifdef HAVE_GNUC_4
214 return __builtin_ctzl (align);
215 #else
216 uint32_t result = 0;
218 /* We know exactly one bit is set at this point. */
219 if (align & 0xffff0000)
220 result |= 16;
221 if (align & 0xff00ff00)
222 result |= 8;
223 if (align & 0xf0f0f0f0)
224 result |= 4;
225 if (align & 0xcccccccc)
226 result |= 2;
227 if (align & 0xaaaaaaaa)
228 result |= 1;
230 return result;
231 #endif
235 static struct section *get_section_by_name(const char *segname,
236 const char *sectname)
238 struct section *s;
240 for (s = sects; s != NULL; s = s->next)
241 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
242 break;
244 return s;
247 static struct section *get_section_by_index(const int32_t index)
249 struct section *s;
251 for (s = sects; s != NULL; s = s->next)
252 if (index == s->index)
253 break;
255 return s;
258 static int32_t get_section_index_by_name(const char *segname,
259 const char *sectname)
261 struct section *s;
263 for (s = sects; s != NULL; s = s->next)
264 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
265 return s->index;
267 return -1;
270 static char *get_section_name_by_index(const int32_t index)
272 struct section *s;
274 for (s = sects; s != NULL; s = s->next)
275 if (index == s->index)
276 return s->sectname;
278 return NULL;
281 static uint8_t get_section_fileindex_by_index(const int32_t index)
283 struct section *s;
284 uint8_t i = 1;
286 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
287 if (index == s->index)
288 return i;
290 if (i == MAX_SECT)
291 error(ERR_WARNING,
292 "too many sections (>255) - clipped by fileindex");
294 return NO_SECT;
297 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
298 evalfunc eval)
300 char zero = 0;
302 machofp = fp;
303 error = errfunc;
304 evaluate = eval;
306 (void)ldef; /* placate optimisers */
308 sects = NULL;
309 sectstail = &sects;
311 syms = NULL;
312 symstail = &syms;
313 nsyms = 0;
314 nlocalsym = 0;
315 nextdefsym = 0;
316 nundefsym = 0;
318 extsyms = raa_init();
319 strs = saa_init(1L);
321 /* string table starts with a zero byte - don't ask why */
322 saa_wbytes(strs, &zero, sizeof(char));
323 strslen = 1;
326 static int macho_setinfo(enum geninfo type, char **val)
328 (void)type;
329 (void)val;
330 return 0;
333 static void sect_write(struct section *sect,
334 const uint8_t *data, uint32_t len)
336 saa_wbytes(sect->data, data, len);
337 sect->size += len;
340 static void add_reloc(struct section *sect, int32_t section,
341 int pcrel, int bytes)
343 struct reloc *r;
344 int32_t fi;
346 /* NeXT as puts relocs in reversed order (address-wise) into the
347 ** files, so we do the same, doesn't seem to make much of a
348 ** difference either way */
349 r = nasm_malloc(sizeof(struct reloc));
350 r->next = sect->relocs;
351 sect->relocs = r;
353 /* the current end of the section will be the symbol's address for
354 ** now, might have to be fixed by macho_fixup_relocs() later on. make
355 ** sure we don't make the symbol scattered by setting the highest
356 ** bit by accident */
357 r->addr = sect->size & ~R_SCATTERED;
358 r->ext = 0;
359 r->pcrel = pcrel;
361 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
362 r->length = bytes >> 1;
364 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
365 r->type = 0;
367 if (section == NO_SEG) {
368 /* absolute local symbol if no section index given */
369 r->snum = R_ABS;
370 } else {
371 fi = get_section_fileindex_by_index(section);
373 if (fi == NO_SECT) {
374 /* external symbol if no section with that index known,
375 ** symbol number was saved in macho_symdef() */
376 r->snum = raa_read(extsyms, section);
377 r->ext = 1;
378 } else {
379 /* local symbol in section fi */
380 r->snum = fi;
384 ++sect->nreloc;
387 static void macho_output(int32_t secto, const void *data, uint32_t type,
388 int32_t section, int32_t wrt)
390 struct section *s, *sbss;
391 int32_t realbytes = type & OUT_SIZMASK;
392 int32_t addr;
393 uint8_t mydata[4], *p;
395 type &= OUT_TYPMASK;
397 if (wrt != NO_SEG) {
398 wrt = NO_SEG;
399 error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
400 /* continue to do _something_ */
403 if (secto == NO_SEG) {
404 if (type != OUT_RESERVE)
405 error(ERR_NONFATAL, "attempt to assemble code in "
406 "[ABSOLUTE] space");
408 return;
411 s = get_section_by_index(secto);
413 if (s == NULL) {
414 error(ERR_WARNING, "attempt to assemble code in"
415 " section %d: defaulting to `.text'", secto);
416 s = get_section_by_name("__TEXT", "__text");
418 /* should never happen */
419 if (s == NULL)
420 error(ERR_PANIC, "text section not found");
423 sbss = get_section_by_name("__DATA", "__bss");
425 if (s == sbss && type != OUT_RESERVE) {
426 error(ERR_WARNING, "attempt to initialize memory in the"
427 " BSS section: ignored");
429 switch (type) {
430 case OUT_REL2ADR:
431 realbytes = 2;
432 break;
434 case OUT_REL4ADR:
435 realbytes = 4;
436 break;
438 default:
439 break;
442 s->size += realbytes;
443 return;
446 switch (type) {
447 case OUT_RESERVE:
448 if (s != sbss) {
449 error(ERR_WARNING, "uninitialized space declared in"
450 " %s section: zeroing",
451 get_section_name_by_index(secto));
453 sect_write(s, NULL, realbytes);
454 } else
455 s->size += realbytes;
457 break;
459 case OUT_RAWDATA:
460 if (section != NO_SEG)
461 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
463 sect_write(s, data, realbytes);
464 break;
466 case OUT_ADDRESS:
467 addr = *(int32_t *)data;
469 if (section != NO_SEG) {
470 if (section % 2) {
471 error(ERR_NONFATAL, "Mach-O format does not support"
472 " section base references");
473 } else
474 add_reloc(s, section, 0, realbytes);
477 p = mydata;
479 if (realbytes == 2)
480 WRITESHORT(p, addr);
481 else
482 WRITELONG(p, addr);
484 sect_write(s, mydata, realbytes);
485 break;
487 case OUT_REL2ADR:
488 if (section == secto)
489 error(ERR_PANIC, "intra-section OUT_REL2ADR");
491 if (section != NO_SEG && section % 2) {
492 error(ERR_NONFATAL, "Mach-O format does not support"
493 " section base references");
494 } else
495 add_reloc(s, section, 1, 2);
497 p = mydata;
498 WRITESHORT(p, *(int32_t *)data - (realbytes + s->size));
499 sect_write(s, mydata, 2L);
500 break;
502 case OUT_REL4ADR:
503 if (section == secto)
504 error(ERR_PANIC, "intra-section OUT_REL4ADR");
506 if (section != NO_SEG && section % 2) {
507 error(ERR_NONFATAL, "Mach-O format does not support"
508 " section base references");
509 } else
510 add_reloc(s, section, 1, 4);
512 p = mydata;
513 WRITELONG(p, *(int32_t *)data - (realbytes + s->size));
514 sect_write(s, mydata, 4L);
515 break;
517 default:
518 error(ERR_PANIC, "unknown output type?");
519 break;
523 static int32_t macho_section(char *name, int pass, int *bits)
525 int32_t index, originalIndex;
526 char *sectionAttributes;
527 struct sectmap *sm;
528 struct section *s;
530 (void)pass;
532 /* Default to 32 bits. */
533 if (!name) {
534 *bits = 32;
535 name = ".text";
536 sectionAttributes = NULL;
537 } else {
538 sectionAttributes = name;
539 name = nasm_strsep(&sectionAttributes, " \t");
542 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
543 /* make lookup into section name translation table */
544 if (!strcmp(name, sm->nasmsect)) {
545 char *currentAttribute;
547 /* try to find section with that name */
548 originalIndex = index = get_section_index_by_name(sm->segname,
549 sm->sectname);
551 /* create it if it doesn't exist yet */
552 if (index == -1) {
553 s = *sectstail = nasm_malloc(sizeof(struct section));
554 s->next = NULL;
555 sectstail = &s->next;
557 s->data = saa_init(1L);
558 s->index = seg_alloc();
559 s->relocs = NULL;
560 s->align = DEFAULT_SECTION_ALIGNMENT;
562 xstrncpy(s->segname, sm->segname);
563 xstrncpy(s->sectname, sm->sectname);
564 s->size = 0;
565 s->nreloc = 0;
566 s->flags = sm->flags;
568 index = s->index;
569 } else {
570 s = get_section_by_index(index);
573 while ((NULL != sectionAttributes)
574 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
575 if (0 != *currentAttribute) {
576 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
577 char *end;
578 int newAlignment, value;
580 value = strtoul(currentAttribute + 6, (char**)&end, 0);
581 newAlignment = exact_log2(value);
583 if (0 != *end) {
584 error(ERR_PANIC,
585 "unknown or missing alignment value \"%s\" "
586 "specified for section \"%s\"",
587 currentAttribute + 6,
588 name);
589 return NO_SEG;
590 } else if (0 > newAlignment) {
591 error(ERR_PANIC,
592 "alignment of %d (for section \"%s\") is not "
593 "a power of two",
594 value,
595 name);
596 return NO_SEG;
599 if ((-1 != originalIndex)
600 && (s->align != newAlignment)) {
601 error(ERR_PANIC,
602 "section \"%s\" has already been specified "
603 "with alignment %d, conflicts with new "
604 "alignment of %d",
605 name,
606 (1 << s->align),
607 value);
608 return NO_SEG;
611 s->align = newAlignment;
612 } else if (!nasm_stricmp("data", currentAttribute)) {
613 /* Do nothing; 'data' is implicit */
614 } else {
615 error(ERR_PANIC,
616 "unknown section attribute %s for section %s",
617 currentAttribute,
618 name);
619 return NO_SEG;
624 return index;
628 error(ERR_PANIC, "invalid section name %s", name);
629 return NO_SEG;
632 static void macho_symdef(char *name, int32_t section, int32_t offset,
633 int is_global, char *special)
635 struct symbol *sym;
637 if (special) {
638 error(ERR_NONFATAL, "The Mach-O output format does "
639 "not support any special symbol types");
640 return;
643 if (is_global == 3) {
644 error(ERR_NONFATAL, "The Mach-O format does not "
645 "(yet) support forward reference fixups.");
646 return;
649 sym = *symstail = nasm_malloc(sizeof(struct symbol));
650 sym->next = NULL;
651 symstail = &sym->next;
653 sym->name = name;
654 sym->strx = strslen;
655 sym->type = 0;
656 sym->desc = 0;
657 sym->value = offset;
658 sym->initial_snum = -1;
660 /* external and common symbols get N_EXT */
661 if (is_global != 0)
662 sym->type |= N_EXT;
664 if (section == NO_SEG) {
665 /* symbols in no section get absolute */
666 sym->type |= N_ABS;
667 sym->sect = NO_SECT;
668 } else {
669 sym->type |= N_SECT;
671 /* get the in-file index of the section the symbol was defined in */
672 sym->sect = get_section_fileindex_by_index(section);
674 if (sym->sect == NO_SECT) {
675 /* remember symbol number of references to external
676 ** symbols, this works because every external symbol gets
677 ** its own section number allocated internally by nasm and
678 ** can so be used as a key */
679 extsyms = raa_write(extsyms, section, nsyms);
680 sym->initial_snum = nsyms;
682 switch (is_global) {
683 case 1:
684 case 2:
685 /* there isn't actually a difference between global
686 ** and common symbols, both even have their size in
687 ** sym->value */
688 sym->type = N_EXT;
689 break;
691 default:
692 /* give an error on unfound section if it's not an
693 ** external or common symbol (assemble_file() does a
694 ** seg_alloc() on every call for them) */
695 error(ERR_PANIC, "in-file index for section %d not found",
696 section);
701 ++nsyms;
704 static int32_t macho_segbase(int32_t section)
706 return section;
709 static int macho_directive(char *directive, char *value, int pass)
711 (void)directive;
712 (void)value;
713 (void)pass;
714 return 0;
717 static void macho_filename(char *inname, char *outname, efunc error)
719 standard_extension(inname, outname, ".o", error);
722 static const char *macho_stdmac[] = {
723 "%define __SECT__ [section .text]",
724 "%macro __NASM_CDecl__ 1",
725 "%endmacro",
726 NULL
729 /* Comparison function for qsort symbol layout. */
730 static int layout_compare (const struct symbol **s1,
731 const struct symbol **s2)
733 return (strcmp ((*s1)->name, (*s2)->name));
736 /* The native assembler does a few things in a similar function
738 * Remove temporary labels
739 * Sort symbols according to local, external, undefined (by name)
740 * Order the string table
742 We do not remove temporary labels right now.
744 numsyms is the total number of symbols we have. strtabsize is the
745 number entries in the string table. */
747 static void macho_layout_symbols (uint32_t *numsyms,
748 uint32_t *strtabsize)
750 struct symbol *sym, **symp;
751 uint32_t i,j;
753 *numsyms = 0;
754 *strtabsize = sizeof (char);
756 symp = &syms;
758 while ((sym = *symp)) {
759 /* Undefined symbols are now external. */
760 if (sym->type == N_UNDF)
761 sym->type |= N_EXT;
763 if ((sym->type & N_EXT) == 0) {
764 sym->snum = *numsyms;
765 *numsyms = *numsyms + 1;
766 nlocalsym++;
768 else {
769 if ((sym->type & N_TYPE) != N_UNDF)
770 nextdefsym++;
771 else
772 nundefsym++;
774 /* If we handle debug info we'll want
775 to check for it here instead of just
776 adding the symbol to the string table. */
777 sym->strx = *strtabsize;
778 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
779 *strtabsize += strlen(sym->name) + 1;
781 symp = &(sym->next);
784 /* Next, sort the symbols. Most of this code is a direct translation from
785 the Apple cctools symbol layout. We need to keep compatibility with that. */
786 /* Set the indexes for symbol groups into the symbol table */
787 ilocalsym = 0;
788 iextdefsym = nlocalsym;
789 iundefsym = nlocalsym + nextdefsym;
791 /* allocate arrays for sorting externals by name */
792 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
793 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
795 i = 0;
796 j = 0;
798 symp = &syms;
800 while ((sym = *symp)) {
802 if((sym->type & N_EXT) == 0) {
803 sym->strx = *strtabsize;
804 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
805 *strtabsize += strlen(sym->name) + 1;
807 else {
808 if((sym->type & N_TYPE) != N_UNDF)
809 extdefsyms[i++] = sym;
810 else
811 undefsyms[j++] = sym;
813 symp = &(sym->next);
816 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
817 (int (*)(const void *, const void *))layout_compare);
818 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
819 (int (*)(const void *, const void *))layout_compare);
821 for(i = 0; i < nextdefsym; i++) {
822 extdefsyms[i]->snum = *numsyms;
823 *numsyms += 1;
825 for(j = 0; j < nundefsym; j++) {
826 undefsyms[j]->snum = *numsyms;
827 *numsyms += 1;
831 /* Calculate some values we'll need for writing later. */
833 static void macho_calculate_sizes (void)
835 struct section *s;
837 /* count sections and calculate in-memory and in-file offsets */
838 for (s = sects; s != NULL; s = s->next) {
839 /* zerofill sections aren't actually written to the file */
840 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
841 seg_filesize += s->size;
843 seg_vmsize += s->size;
844 ++seg_nsects;
847 /* calculate size of all headers, load commands and sections to
848 ** get a pointer to the start of all the raw data */
849 if (seg_nsects > 0) {
850 ++head_ncmds;
851 head_sizeofcmds +=
852 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
855 if (nsyms > 0) {
856 ++head_ncmds;
857 head_sizeofcmds += MACHO_SYMCMD_SIZE;
861 /* Write out the header information for the file. */
863 static void macho_write_header (void)
865 fwriteint32_t(MH_MAGIC, machofp); /* magic */
866 fwriteint32_t(CPU_TYPE_I386, machofp); /* CPU type */
867 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
868 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
869 fwriteint32_t(head_ncmds, machofp); /* number of load commands */
870 fwriteint32_t(head_sizeofcmds, machofp); /* size of load commands */
871 fwriteint32_t(0, machofp); /* no flags */
874 /* Write out the segment load command at offset. */
876 static uint32_t macho_write_segment (uint32_t offset)
878 uint32_t s_addr = 0;
879 uint32_t rel_base = alignint32_t (offset + seg_filesize);
880 uint32_t s_reloff = 0;
881 struct section *s;
883 fwriteint32_t(LC_SEGMENT, machofp); /* cmd == LC_SEGMENT */
885 /* size of load command including section load commands */
886 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
887 MACHO_SECTCMD_SIZE, machofp);
889 /* in an MH_OBJECT file all sections are in one unnamed (name
890 ** all zeros) segment */
891 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp);
892 fwriteint32_t(0, machofp); /* in-memory offset */
893 fwriteint32_t(seg_vmsize, machofp); /* in-memory size */
894 fwriteint32_t(offset, machofp); /* in-file offset to data */
895 fwriteint32_t(seg_filesize, machofp); /* in-file size */
896 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
897 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
898 fwriteint32_t(seg_nsects, machofp); /* number of sections */
899 fwriteint32_t(0, machofp); /* no flags */
901 /* emit section headers */
902 for (s = sects; s != NULL; s = s->next) {
903 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
904 fwrite(s->segname, sizeof(s->segname), 1, machofp);
905 fwriteint32_t(s_addr, machofp);
906 fwriteint32_t(s->size, machofp);
908 /* dummy data for zerofill sections or proper values */
909 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
910 fwriteint32_t(offset, machofp);
911 /* Write out section alignment, as a power of two.
912 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
913 fwriteint32_t(s->align, machofp);
914 /* To be compatible with cctools as we emit
915 a zero reloff if we have no relocations. */
916 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
917 fwriteint32_t(s->nreloc, machofp);
919 offset += s->size;
920 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
921 } else {
922 fwriteint32_t(0, machofp);
923 fwriteint32_t(0, machofp);
924 fwriteint32_t(0, machofp);
925 fwriteint32_t(0, machofp);
928 fwriteint32_t(s->flags, machofp); /* flags */
929 fwriteint32_t(0, machofp); /* reserved */
930 fwriteint32_t(0, machofp); /* reserved */
932 s_addr += s->size;
935 rel_padcnt = rel_base - offset;
936 offset = rel_base + s_reloff;
938 return offset;
941 /* For a given chain of relocs r, write out the entire relocation
942 chain to the object file. */
944 static void macho_write_relocs (struct reloc *r)
946 while (r) {
947 uint32_t word2;
949 fwriteint32_t(r->addr, machofp); /* reloc offset */
951 word2 = r->snum;
952 word2 |= r->pcrel << 24;
953 word2 |= r->length << 25;
954 word2 |= r->ext << 27;
955 word2 |= r->type << 28;
956 fwriteint32_t(word2, machofp); /* reloc data */
958 r = r->next;
962 /* Write out the section data. */
963 static void macho_write_section (void)
965 struct section *s, *s2;
966 struct reloc *r;
967 char *rel_paddata = "\0\0\0";
968 uint8_t fi, *p, *q, blk[4];
969 int32_t l;
971 for (s = sects; s != NULL; s = s->next) {
972 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
973 continue;
975 /* no padding needs to be done to the sections */
977 /* Like a.out Mach-O references things in the data or bss
978 * sections by addresses which are actually relative to the
979 * start of the _text_ section, in the _file_. See outaout.c
980 * for more information. */
981 saa_rewind(s->data);
982 for (r = s->relocs; r != NULL; r = r->next) {
983 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
984 p = q = blk;
985 l = *p++;
987 /* get offset based on relocation type */
988 if (r->length > 0) {
989 l += ((int32_t)*p++) << 8;
991 if (r->length == 2) {
992 l += ((int32_t)*p++) << 16;
993 l += ((int32_t)*p++) << 24;
997 /* If the relocation is internal add to the current section
998 offset. Otherwise the only value we need is the symbol
999 offset which we already have. The linker takes care
1000 of the rest of the address. */
1001 if (!r->ext) {
1002 /* add sizes of previous sections to current offset */
1003 for (s2 = sects, fi = 1;
1004 s2 != NULL && fi < r->snum; s2 = s2->next, fi++)
1005 if ((s2->flags & SECTION_TYPE) != S_ZEROFILL)
1006 l += s2->size;
1009 /* write new offset back */
1010 if (r->length == 2)
1011 WRITELONG(q, l);
1012 else if (r->length == 1)
1013 WRITESHORT(q, l);
1014 else
1015 *q++ = l & 0xFF;
1017 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1020 /* dump the section data to file */
1021 saa_fpwrite(s->data, machofp);
1024 /* pad last section up to reloc entries on int32_t boundary */
1025 fwrite(rel_paddata, rel_padcnt, 1, machofp);
1027 /* emit relocation entries */
1028 for (s = sects; s != NULL; s = s->next)
1029 macho_write_relocs (s->relocs);
1032 /* Write out the symbol table. We should already have sorted this
1033 before now. */
1034 static void macho_write_symtab (void)
1036 struct symbol *sym;
1037 struct section *s;
1038 int32_t fi;
1039 uint32_t i;
1041 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1043 for (sym = syms; sym != NULL; sym = sym->next) {
1044 if ((sym->type & N_EXT) == 0) {
1045 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1046 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1047 fwrite(&sym->sect, 1, 1, machofp); /* section */
1048 fwriteint16_t(sym->desc, machofp); /* description */
1050 /* Fix up the symbol value now that we know the final section
1051 sizes. */
1052 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1053 for (s = sects, fi = 1;
1054 s != NULL && fi < sym->sect; s = s->next, ++fi)
1055 sym->value += s->size;
1058 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1062 for (i = 0; i < nextdefsym; i++) {
1063 sym = extdefsyms[i];
1064 fwriteint32_t(sym->strx, machofp);
1065 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1066 fwrite(&sym->sect, 1, 1, machofp); /* section */
1067 fwriteint16_t(sym->desc, machofp); /* description */
1069 /* Fix up the symbol value now that we know the final section
1070 sizes. */
1071 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1072 for (s = sects, fi = 1;
1073 s != NULL && fi < sym->sect; s = s->next, ++fi)
1074 sym->value += s->size;
1077 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1080 for (i = 0; i < nundefsym; i++) {
1081 sym = undefsyms[i];
1082 fwriteint32_t(sym->strx, machofp);
1083 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1084 fwrite(&sym->sect, 1, 1, machofp); /* section */
1085 fwriteint16_t(sym->desc, machofp); /* description */
1087 /* Fix up the symbol value now that we know the final section
1088 sizes. */
1089 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1090 for (s = sects, fi = 1;
1091 s != NULL && fi < sym->sect; s = s->next, ++fi)
1092 sym->value += s->size;
1095 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1099 /* Fixup the snum in the relocation entries, we should be
1100 doing this only for externally undefined symbols. */
1101 static void macho_fixup_relocs (struct reloc *r)
1103 struct symbol *sym;
1104 uint32_t i;
1106 while (r != NULL) {
1107 if (r->ext) {
1108 for (i = 0; i < nundefsym; i++) {
1109 sym = undefsyms[i];
1110 if (sym->initial_snum == r->snum) {
1111 r->snum = sym->snum;
1115 r = r->next;
1119 /* Write out the object file. */
1121 static void macho_write (void)
1123 uint32_t offset = 0;
1125 /* mach-o object file structure:
1127 ** mach header
1128 ** uint32_t magic
1129 ** int cpu type
1130 ** int cpu subtype
1131 ** uint32_t mach file type
1132 ** uint32_t number of load commands
1133 ** uint32_t size of all load commands
1134 ** (includes section struct size of segment command)
1135 ** uint32_t flags
1137 ** segment command
1138 ** uint32_t command type == LC_SEGMENT
1139 ** uint32_t size of load command
1140 ** (including section load commands)
1141 ** char[16] segment name
1142 ** uint32_t in-memory offset
1143 ** uint32_t in-memory size
1144 ** uint32_t in-file offset to data area
1145 ** uint32_t in-file size
1146 ** (in-memory size excluding zerofill sections)
1147 ** int maximum vm protection
1148 ** int initial vm protection
1149 ** uint32_t number of sections
1150 ** uint32_t flags
1152 ** section commands
1153 ** char[16] section name
1154 ** char[16] segment name
1155 ** uint32_t in-memory offset
1156 ** uint32_t in-memory size
1157 ** uint32_t in-file offset
1158 ** uint32_t alignment
1159 ** (irrelevant in MH_OBJECT)
1160 ** uint32_t in-file offset of relocation entires
1161 ** uint32_t number of relocations
1162 ** uint32_t flags
1163 ** uint32_t reserved
1164 ** uint32_t reserved
1166 ** symbol table command
1167 ** uint32_t command type == LC_SYMTAB
1168 ** uint32_t size of load command
1169 ** uint32_t symbol table offset
1170 ** uint32_t number of symbol table entries
1171 ** uint32_t string table offset
1172 ** uint32_t string table size
1174 ** raw section data
1176 ** padding to int32_t boundary
1178 ** relocation data (struct reloc)
1179 ** int32_t offset
1180 ** uint data (symbolnum, pcrel, length, extern, type)
1182 ** symbol table data (struct nlist)
1183 ** int32_t string table entry number
1184 ** uint8_t type
1185 ** (extern, absolute, defined in section)
1186 ** uint8_t section
1187 ** (0 for global symbols, section number of definition (>= 1, <=
1188 ** 254) for local symbols, size of variable for common symbols
1189 ** [type == extern])
1190 ** int16_t description
1191 ** (for stab debugging format)
1192 ** uint32_t value (i.e. file offset) of symbol or stab offset
1194 ** string table data
1195 ** list of null-terminated strings
1198 /* Emit the Mach-O header. */
1199 macho_write_header();
1201 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1203 /* emit the segment load command */
1204 if (seg_nsects > 0)
1205 offset = macho_write_segment (offset);
1206 else
1207 error(ERR_WARNING, "no sections?");
1209 if (nsyms > 0) {
1210 /* write out symbol command */
1211 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1212 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1213 fwriteint32_t(offset, machofp); /* symbol table offset */
1214 fwriteint32_t(nsyms, machofp); /* number of symbol
1215 ** table entries */
1217 offset += nsyms * MACHO_NLIST_SIZE;
1218 fwriteint32_t(offset, machofp); /* string table offset */
1219 fwriteint32_t(strslen, machofp); /* string table size */
1222 /* emit section data */
1223 if (seg_nsects > 0)
1224 macho_write_section ();
1226 /* emit symbol table if we have symbols */
1227 if (nsyms > 0)
1228 macho_write_symtab ();
1230 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1232 /* emit string table */
1233 saa_fpwrite(strs, machofp);
1235 /* We do quite a bit here, starting with finalizing all of the data
1236 for the object file, writing, and then freeing all of the data from
1237 the file. */
1239 static void macho_cleanup(int debuginfo)
1241 struct section *s;
1242 struct reloc *r;
1243 struct symbol *sym;
1245 (void)debuginfo;
1247 /* Sort all symbols. */
1248 macho_layout_symbols (&nsyms, &strslen);
1250 /* Fixup relocation entries */
1251 for (s = sects; s != NULL; s = s->next) {
1252 macho_fixup_relocs (s->relocs);
1255 /* First calculate and finalize needed values. */
1256 macho_calculate_sizes();
1257 macho_write();
1259 /* done - yay! */
1260 fclose(machofp);
1262 /* free up everything */
1263 while (sects->next) {
1264 s = sects;
1265 sects = sects->next;
1267 saa_free(s->data);
1268 while (s->relocs != NULL) {
1269 r = s->relocs;
1270 s->relocs = s->relocs->next;
1271 nasm_free(r);
1274 nasm_free(s);
1277 saa_free(strs);
1278 raa_free(extsyms);
1280 if (syms) {
1281 while (syms->next) {
1282 sym = syms;
1283 syms = syms->next;
1285 nasm_free (sym);
1290 /* Debugging routines. */
1291 static void debug_reloc (struct reloc *r)
1293 fprintf (stdout, "reloc:\n");
1294 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1295 fprintf (stdout, "\tsnum: %d\n", r->snum);
1296 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1297 fprintf (stdout, "\tlength: %d\n", r->length);
1298 fprintf (stdout, "\text: %d\n", r->ext);
1299 fprintf (stdout, "\ttype: %d\n", r->type);
1302 static void debug_section_relocs (struct section *s)
1304 struct reloc *r = s->relocs;
1306 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1308 while (r != NULL) {
1309 debug_reloc (r);
1310 r = r->next;
1314 struct ofmt of_macho = {
1315 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",
1316 "macho",
1317 NULL,
1318 null_debug_arr,
1319 &null_debug_form,
1320 macho_stdmac,
1321 macho_init,
1322 macho_setinfo,
1323 macho_output,
1324 macho_symdef,
1325 macho_section,
1326 macho_segbase,
1327 macho_directive,
1328 macho_filename,
1329 macho_cleanup
1332 #endif
1335 * Local Variables:
1336 * mode:c
1337 * c-basic-offset:4
1338 * End:
1340 * end of file */