Move all the SAA code out of nasmlib
[nasm/nasm.git] / output / outmacho.c
blob50cd0b422521fd35d329218ec202a009522f6bd8
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 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 "outform.h"
25 #include "compiler.h"
27 #if defined(OF_MACHO)
29 /* Mach-O in-file header structure sizes */
30 #define MACHO_HEADER_SIZE (28)
31 #define MACHO_SEGCMD_SIZE (56)
32 #define MACHO_SECTCMD_SIZE (68)
33 #define MACHO_SYMCMD_SIZE (24)
34 #define MACHO_NLIST_SIZE (12)
35 #define MACHO_RELINFO_SIZE (8)
37 /* Mach-O file header values */
38 #define MH_MAGIC (0xfeedface)
39 #define CPU_TYPE_I386 (7) /* x86 platform */
40 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
41 #define MH_OBJECT (0x1) /* object file */
43 #define LC_SEGMENT (0x1) /* segment load command */
44 #define LC_SYMTAB (0x2) /* symbol table load command */
46 #define VM_PROT_NONE (0x00)
47 #define VM_PROT_READ (0x01)
48 #define VM_PROT_WRITE (0x02)
49 #define VM_PROT_EXECUTE (0x04)
51 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
52 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
54 struct section {
55 /* nasm internal data */
56 struct section *next;
57 struct SAA *data;
58 int32_t index;
59 struct reloc *relocs;
60 int align;
62 /* data that goes into the file */
63 char sectname[16]; /* what this section is called */
64 char segname[16]; /* segment this section will be in */
65 uint32_t size; /* in-memory and -file size */
66 uint32_t nreloc; /* relocation entry count */
67 uint32_t flags; /* type and attributes (masked) */
70 #define SECTION_TYPE 0x000000ff /* section type mask */
72 #define S_REGULAR (0x0) /* standard section */
73 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
75 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
76 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
77 machine instructions */
78 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
79 relocation entries */
80 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
81 relocation entries */
84 static struct sectmap {
85 const char *nasmsect;
86 const char *segname;
87 const char *sectname;
88 const int32_t flags;
89 } sectmap[] = {
90 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
91 {".data", "__DATA", "__data", S_REGULAR},
92 {".rodata", "__DATA", "__const", S_REGULAR},
93 {".bss", "__DATA", "__bss", S_ZEROFILL},
94 {NULL, NULL, NULL, 0}
97 struct reloc {
98 /* nasm internal data */
99 struct reloc *next;
101 /* data that goes into the file */
102 int32_t addr; /* op's offset in section */
103 unsigned int snum:24, /* contains symbol index if
104 ** ext otherwise in-file
105 ** section number */
106 pcrel:1, /* relative relocation */
107 length:2, /* 0=byte, 1=word, 2=int32_t */
108 ext:1, /* external symbol referenced */
109 type:4; /* reloc type, 0 for us */
112 #define R_ABS 0 /* absolute relocation */
113 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
114 ** highest bit == 1 */
116 struct symbol {
117 /* nasm internal data */
118 struct symbol *next; /* next symbol in the list */
119 char *name; /* name of this symbol */
120 int32_t initial_snum; /* symbol number used above in
121 reloc */
122 int32_t snum; /* true snum for reloc */
124 /* data that goes into the file */
125 int32_t strx; /* string table index */
126 uint8_t type; /* symbol type */
127 uint8_t sect; /* NO_SECT or section number */
128 int16_t desc; /* for stab debugging, 0 for us */
129 uint32_t value; /* offset of symbol in section */
132 /* symbol type bits */
133 #define N_EXT 0x01 /* global or external symbol */
135 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
136 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
137 #define N_SECT 0xe /* defined symbol, n_sect holds
138 ** section number */
140 #define N_TYPE 0x0e /* type bit mask */
142 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
144 /* special section number values */
145 #define NO_SECT 0 /* no section, invalid */
146 #define MAX_SECT 255 /* maximum number of sections */
148 static struct section *sects, **sectstail;
149 static struct symbol *syms, **symstail;
150 static uint32_t nsyms;
152 /* These variables are set by macho_layout_symbols() to organize
153 the symbol table and string table in order the dynamic linker
154 expects. They are then used in macho_write() to put out the
155 symbols and strings in that order.
157 The order of the symbol table is:
158 local symbols
159 defined external symbols (sorted by name)
160 undefined external symbols (sorted by name)
162 The order of the string table is:
163 strings for external symbols
164 strings for local symbols
166 static uint32_t ilocalsym = 0;
167 static uint32_t iextdefsym = 0;
168 static uint32_t iundefsym = 0;
169 static uint32_t nlocalsym;
170 static uint32_t nextdefsym;
171 static uint32_t nundefsym;
172 static struct symbol **extdefsyms = NULL;
173 static struct symbol **undefsyms = NULL;
175 static struct RAA *extsyms;
176 static struct SAA *strs;
177 static uint32_t strslen;
179 static FILE *machofp;
180 static efunc error;
181 static evalfunc evaluate;
183 extern struct ofmt of_macho;
185 /* Global file information. This should be cleaned up into either
186 a structure or as function arguments. */
187 uint32_t head_ncmds = 0;
188 uint32_t head_sizeofcmds = 0;
189 uint32_t seg_filesize = 0;
190 uint32_t seg_vmsize = 0;
191 uint32_t seg_nsects = 0;
192 uint32_t rel_padcnt = 0;
195 #define xstrncpy(xdst, xsrc) \
196 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
197 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
198 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
200 #define align(x, y) \
201 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
203 #define alignint32_t(x) \
204 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
206 static void debug_reloc (struct reloc *);
207 static void debug_section_relocs (struct section *) _unused;
209 static int exact_log2 (uint32_t align)
211 if (align == 0) {
212 return 0;
213 } else if (align & (align-1)) {
214 return -1; /* Not a power of 2 */
215 } else {
216 #ifdef HAVE_GNUC_4
217 return __builtin_ctzl (align);
218 #else
219 uint32_t result = 0;
221 /* We know exactly one bit is set at this point. */
222 if (align & 0xffff0000)
223 result |= 16;
224 if (align & 0xff00ff00)
225 result |= 8;
226 if (align & 0xf0f0f0f0)
227 result |= 4;
228 if (align & 0xcccccccc)
229 result |= 2;
230 if (align & 0xaaaaaaaa)
231 result |= 1;
233 return result;
234 #endif
238 static struct section *get_section_by_name(const char *segname,
239 const char *sectname)
241 struct section *s;
243 for (s = sects; s != NULL; s = s->next)
244 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
245 break;
247 return s;
250 static struct section *get_section_by_index(const int32_t index)
252 struct section *s;
254 for (s = sects; s != NULL; s = s->next)
255 if (index == s->index)
256 break;
258 return s;
261 static int32_t get_section_index_by_name(const char *segname,
262 const char *sectname)
264 struct section *s;
266 for (s = sects; s != NULL; s = s->next)
267 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
268 return s->index;
270 return -1;
273 static char *get_section_name_by_index(const int32_t index)
275 struct section *s;
277 for (s = sects; s != NULL; s = s->next)
278 if (index == s->index)
279 return s->sectname;
281 return NULL;
284 static uint8_t get_section_fileindex_by_index(const int32_t index)
286 struct section *s;
287 uint8_t i = 1;
289 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
290 if (index == s->index)
291 return i;
293 if (i == MAX_SECT)
294 error(ERR_WARNING,
295 "too many sections (>255) - clipped by fileindex");
297 return NO_SECT;
300 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
301 evalfunc eval)
303 char zero = 0;
305 machofp = fp;
306 error = errfunc;
307 evaluate = eval;
309 (void)ldef; /* placate optimisers */
311 sects = NULL;
312 sectstail = &sects;
314 syms = NULL;
315 symstail = &syms;
316 nsyms = 0;
317 nlocalsym = 0;
318 nextdefsym = 0;
319 nundefsym = 0;
321 extsyms = raa_init();
322 strs = saa_init(1L);
324 /* string table starts with a zero byte - don't ask why */
325 saa_wbytes(strs, &zero, sizeof(char));
326 strslen = 1;
329 static int macho_setinfo(enum geninfo type, char **val)
331 (void)type;
332 (void)val;
333 return 0;
336 static void sect_write(struct section *sect,
337 const uint8_t *data, uint32_t len)
339 saa_wbytes(sect->data, data, len);
340 sect->size += len;
343 static void add_reloc(struct section *sect, int32_t section,
344 int pcrel, int bytes)
346 struct reloc *r;
347 int32_t fi;
349 /* NeXT as puts relocs in reversed order (address-wise) into the
350 ** files, so we do the same, doesn't seem to make much of a
351 ** difference either way */
352 r = nasm_malloc(sizeof(struct reloc));
353 r->next = sect->relocs;
354 sect->relocs = r;
356 /* the current end of the section will be the symbol's address for
357 ** now, might have to be fixed by macho_fixup_relocs() later on. make
358 ** sure we don't make the symbol scattered by setting the highest
359 ** bit by accident */
360 r->addr = sect->size & ~R_SCATTERED;
361 r->ext = 0;
362 r->pcrel = pcrel;
364 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
365 r->length = bytes >> 1;
367 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
368 r->type = 0;
370 if (section == NO_SEG) {
371 /* absolute local symbol if no section index given */
372 r->snum = R_ABS;
373 } else {
374 fi = get_section_fileindex_by_index(section);
376 if (fi == NO_SECT) {
377 /* external symbol if no section with that index known,
378 ** symbol number was saved in macho_symdef() */
379 r->snum = raa_read(extsyms, section);
380 r->ext = 1;
381 } else {
382 /* local symbol in section fi */
383 r->snum = fi;
387 ++sect->nreloc;
390 static void macho_output(int32_t secto, const void *data,
391 enum out_type type, uint64_t size,
392 int32_t section, int32_t wrt)
394 struct section *s, *sbss;
395 int32_t addr;
396 uint8_t mydata[4], *p;
398 if (wrt != NO_SEG) {
399 wrt = NO_SEG;
400 error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
401 /* continue to do _something_ */
404 if (secto == NO_SEG) {
405 if (type != OUT_RESERVE)
406 error(ERR_NONFATAL, "attempt to assemble code in "
407 "[ABSOLUTE] space");
409 return;
412 s = get_section_by_index(secto);
414 if (s == NULL) {
415 error(ERR_WARNING, "attempt to assemble code in"
416 " section %d: defaulting to `.text'", secto);
417 s = get_section_by_name("__TEXT", "__text");
419 /* should never happen */
420 if (s == NULL)
421 error(ERR_PANIC, "text section not found");
424 sbss = get_section_by_name("__DATA", "__bss");
426 if (s == sbss && type != OUT_RESERVE) {
427 error(ERR_WARNING, "attempt to initialize memory in the"
428 " BSS section: ignored");
430 switch (type) {
431 case OUT_REL2ADR:
432 size = 2;
433 break;
435 case OUT_REL4ADR:
436 size = 4;
437 break;
439 default:
440 break;
443 s->size += size;
444 return;
447 switch (type) {
448 case OUT_RESERVE:
449 if (s != sbss) {
450 error(ERR_WARNING, "uninitialized space declared in"
451 " %s section: zeroing",
452 get_section_name_by_index(secto));
454 sect_write(s, NULL, size);
455 } else
456 s->size += size;
458 break;
460 case OUT_RAWDATA:
461 if (section != NO_SEG)
462 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
464 sect_write(s, data, size);
465 break;
467 case OUT_ADDRESS:
468 addr = *(int64_t *)data;
470 if (section != NO_SEG) {
471 if (section % 2) {
472 error(ERR_NONFATAL, "Mach-O format does not support"
473 " section base references");
474 } else
475 add_reloc(s, section, 0, size);
478 p = mydata;
479 WRITEADDR(p, addr, size);
480 sect_write(s, mydata, size);
481 break;
483 case OUT_REL2ADR:
484 if (section == secto)
485 error(ERR_PANIC, "intra-section OUT_REL2ADR");
487 if (section != NO_SEG && section % 2) {
488 error(ERR_NONFATAL, "Mach-O format does not support"
489 " section base references");
490 } else
491 add_reloc(s, section, 1, 2);
493 p = mydata;
494 WRITESHORT(p, *(int32_t *)data - (size + s->size));
495 sect_write(s, mydata, 2L);
496 break;
498 case OUT_REL4ADR:
499 if (section == secto)
500 error(ERR_PANIC, "intra-section OUT_REL4ADR");
502 if (section != NO_SEG && section % 2) {
503 error(ERR_NONFATAL, "Mach-O format does not support"
504 " section base references");
505 } else
506 add_reloc(s, section, 1, 4);
508 p = mydata;
509 WRITELONG(p, *(int32_t *)data - (size + s->size));
510 sect_write(s, mydata, 4L);
511 break;
513 default:
514 error(ERR_PANIC, "unknown output type?");
515 break;
519 static int32_t macho_section(char *name, int pass, int *bits)
521 int32_t index, originalIndex;
522 char *sectionAttributes;
523 struct sectmap *sm;
524 struct section *s;
526 (void)pass;
528 /* Default to 32 bits. */
529 if (!name) {
530 *bits = 32;
531 name = ".text";
532 sectionAttributes = NULL;
533 } else {
534 sectionAttributes = name;
535 name = nasm_strsep(&sectionAttributes, " \t");
538 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
539 /* make lookup into section name translation table */
540 if (!strcmp(name, sm->nasmsect)) {
541 char *currentAttribute;
543 /* try to find section with that name */
544 originalIndex = index = get_section_index_by_name(sm->segname,
545 sm->sectname);
547 /* create it if it doesn't exist yet */
548 if (index == -1) {
549 s = *sectstail = nasm_malloc(sizeof(struct section));
550 s->next = NULL;
551 sectstail = &s->next;
553 s->data = saa_init(1L);
554 s->index = seg_alloc();
555 s->relocs = NULL;
556 s->align = -1;
558 xstrncpy(s->segname, sm->segname);
559 xstrncpy(s->sectname, sm->sectname);
560 s->size = 0;
561 s->nreloc = 0;
562 s->flags = sm->flags;
564 index = s->index;
565 } else {
566 s = get_section_by_index(index);
569 while ((NULL != sectionAttributes)
570 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
571 if (0 != *currentAttribute) {
572 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
573 char *end;
574 int newAlignment, value;
576 value = strtoul(currentAttribute + 6, (char**)&end, 0);
577 newAlignment = exact_log2(value);
579 if (0 != *end) {
580 error(ERR_PANIC,
581 "unknown or missing alignment value \"%s\" "
582 "specified for section \"%s\"",
583 currentAttribute + 6,
584 name);
585 return NO_SEG;
586 } else if (0 > newAlignment) {
587 error(ERR_PANIC,
588 "alignment of %d (for section \"%s\") is not "
589 "a power of two",
590 value,
591 name);
592 return NO_SEG;
595 if ((-1 != originalIndex)
596 && (s->align != newAlignment)
597 && (s->align != -1)) {
598 error(ERR_PANIC,
599 "section \"%s\" has already been specified "
600 "with alignment %d, conflicts with new "
601 "alignment of %d",
602 name,
603 (1 << s->align),
604 value);
605 return NO_SEG;
608 s->align = newAlignment;
609 } else if (!nasm_stricmp("data", currentAttribute)) {
610 /* Do nothing; 'data' is implicit */
611 } else {
612 error(ERR_PANIC,
613 "unknown section attribute %s for section %s",
614 currentAttribute,
615 name);
616 return NO_SEG;
621 return index;
625 error(ERR_PANIC, "invalid section name %s", name);
626 return NO_SEG;
629 static void macho_symdef(char *name, int32_t section, int64_t offset,
630 int is_global, char *special)
632 struct symbol *sym;
634 if (special) {
635 error(ERR_NONFATAL, "The Mach-O output format does "
636 "not support any special symbol types");
637 return;
640 if (is_global == 3) {
641 error(ERR_NONFATAL, "The Mach-O format does not "
642 "(yet) support forward reference fixups.");
643 return;
646 sym = *symstail = nasm_malloc(sizeof(struct symbol));
647 sym->next = NULL;
648 symstail = &sym->next;
650 sym->name = name;
651 sym->strx = strslen;
652 sym->type = 0;
653 sym->desc = 0;
654 sym->value = offset;
655 sym->initial_snum = -1;
657 /* external and common symbols get N_EXT */
658 if (is_global != 0)
659 sym->type |= N_EXT;
661 if (section == NO_SEG) {
662 /* symbols in no section get absolute */
663 sym->type |= N_ABS;
664 sym->sect = NO_SECT;
665 } else {
666 sym->type |= N_SECT;
668 /* get the in-file index of the section the symbol was defined in */
669 sym->sect = get_section_fileindex_by_index(section);
671 if (sym->sect == NO_SECT) {
672 /* remember symbol number of references to external
673 ** symbols, this works because every external symbol gets
674 ** its own section number allocated internally by nasm and
675 ** can so be used as a key */
676 extsyms = raa_write(extsyms, section, nsyms);
677 sym->initial_snum = nsyms;
679 switch (is_global) {
680 case 1:
681 case 2:
682 /* there isn't actually a difference between global
683 ** and common symbols, both even have their size in
684 ** sym->value */
685 sym->type = N_EXT;
686 break;
688 default:
689 /* give an error on unfound section if it's not an
690 ** external or common symbol (assemble_file() does a
691 ** seg_alloc() on every call for them) */
692 error(ERR_PANIC, "in-file index for section %d not found",
693 section);
698 ++nsyms;
701 static int32_t macho_segbase(int32_t section)
703 return section;
706 static int macho_directive(char *directive, char *value, int pass)
708 (void)directive;
709 (void)value;
710 (void)pass;
711 return 0;
714 static void macho_filename(char *inname, char *outname, efunc error)
716 standard_extension(inname, outname, ".o", error);
719 static const char *macho_stdmac[] = {
720 "%define __SECT__ [section .text]",
721 "%macro __NASM_CDecl__ 1",
722 "%endmacro",
723 NULL
726 /* Comparison function for qsort symbol layout. */
727 static int layout_compare (const struct symbol **s1,
728 const struct symbol **s2)
730 return (strcmp ((*s1)->name, (*s2)->name));
733 /* The native assembler does a few things in a similar function
735 * Remove temporary labels
736 * Sort symbols according to local, external, undefined (by name)
737 * Order the string table
739 We do not remove temporary labels right now.
741 numsyms is the total number of symbols we have. strtabsize is the
742 number entries in the string table. */
744 static void macho_layout_symbols (uint32_t *numsyms,
745 uint32_t *strtabsize)
747 struct symbol *sym, **symp;
748 uint32_t i,j;
750 *numsyms = 0;
751 *strtabsize = sizeof (char);
753 symp = &syms;
755 while ((sym = *symp)) {
756 /* Undefined symbols are now external. */
757 if (sym->type == N_UNDF)
758 sym->type |= N_EXT;
760 if ((sym->type & N_EXT) == 0) {
761 sym->snum = *numsyms;
762 *numsyms = *numsyms + 1;
763 nlocalsym++;
765 else {
766 if ((sym->type & N_TYPE) != N_UNDF)
767 nextdefsym++;
768 else
769 nundefsym++;
771 /* If we handle debug info we'll want
772 to check for it here instead of just
773 adding the symbol to the string table. */
774 sym->strx = *strtabsize;
775 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
776 *strtabsize += strlen(sym->name) + 1;
778 symp = &(sym->next);
781 /* Next, sort the symbols. Most of this code is a direct translation from
782 the Apple cctools symbol layout. We need to keep compatibility with that. */
783 /* Set the indexes for symbol groups into the symbol table */
784 ilocalsym = 0;
785 iextdefsym = nlocalsym;
786 iundefsym = nlocalsym + nextdefsym;
788 /* allocate arrays for sorting externals by name */
789 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
790 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
792 i = 0;
793 j = 0;
795 symp = &syms;
797 while ((sym = *symp)) {
799 if((sym->type & N_EXT) == 0) {
800 sym->strx = *strtabsize;
801 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
802 *strtabsize += strlen(sym->name) + 1;
804 else {
805 if((sym->type & N_TYPE) != N_UNDF)
806 extdefsyms[i++] = sym;
807 else
808 undefsyms[j++] = sym;
810 symp = &(sym->next);
813 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
814 (int (*)(const void *, const void *))layout_compare);
815 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
816 (int (*)(const void *, const void *))layout_compare);
818 for(i = 0; i < nextdefsym; i++) {
819 extdefsyms[i]->snum = *numsyms;
820 *numsyms += 1;
822 for(j = 0; j < nundefsym; j++) {
823 undefsyms[j]->snum = *numsyms;
824 *numsyms += 1;
828 /* Calculate some values we'll need for writing later. */
830 static void macho_calculate_sizes (void)
832 struct section *s;
834 /* count sections and calculate in-memory and in-file offsets */
835 for (s = sects; s != NULL; s = s->next) {
836 /* zerofill sections aren't actually written to the file */
837 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
838 seg_filesize += s->size;
840 seg_vmsize += s->size;
841 ++seg_nsects;
844 /* calculate size of all headers, load commands and sections to
845 ** get a pointer to the start of all the raw data */
846 if (seg_nsects > 0) {
847 ++head_ncmds;
848 head_sizeofcmds +=
849 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
852 if (nsyms > 0) {
853 ++head_ncmds;
854 head_sizeofcmds += MACHO_SYMCMD_SIZE;
858 /* Write out the header information for the file. */
860 static void macho_write_header (void)
862 fwriteint32_t(MH_MAGIC, machofp); /* magic */
863 fwriteint32_t(CPU_TYPE_I386, machofp); /* CPU type */
864 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
865 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
866 fwriteint32_t(head_ncmds, machofp); /* number of load commands */
867 fwriteint32_t(head_sizeofcmds, machofp); /* size of load commands */
868 fwriteint32_t(0, machofp); /* no flags */
871 /* Write out the segment load command at offset. */
873 static uint32_t macho_write_segment (uint32_t offset)
875 uint32_t s_addr = 0;
876 uint32_t rel_base = alignint32_t (offset + seg_filesize);
877 uint32_t s_reloff = 0;
878 struct section *s;
880 fwriteint32_t(LC_SEGMENT, machofp); /* cmd == LC_SEGMENT */
882 /* size of load command including section load commands */
883 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
884 MACHO_SECTCMD_SIZE, machofp);
886 /* in an MH_OBJECT file all sections are in one unnamed (name
887 ** all zeros) segment */
888 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp);
889 fwriteint32_t(0, machofp); /* in-memory offset */
890 fwriteint32_t(seg_vmsize, machofp); /* in-memory size */
891 fwriteint32_t(offset, machofp); /* in-file offset to data */
892 fwriteint32_t(seg_filesize, machofp); /* in-file size */
893 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
894 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
895 fwriteint32_t(seg_nsects, machofp); /* number of sections */
896 fwriteint32_t(0, machofp); /* no flags */
898 /* emit section headers */
899 for (s = sects; s != NULL; s = s->next) {
900 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
901 fwrite(s->segname, sizeof(s->segname), 1, machofp);
902 fwriteint32_t(s_addr, machofp);
903 fwriteint32_t(s->size, machofp);
905 /* dummy data for zerofill sections or proper values */
906 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
907 fwriteint32_t(offset, machofp);
908 /* Write out section alignment, as a power of two.
909 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
910 if (s->align == -1)
911 s->align = DEFAULT_SECTION_ALIGNMENT;
912 fwriteint32_t(s->align, machofp);
913 /* To be compatible with cctools as we emit
914 a zero reloff if we have no relocations. */
915 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
916 fwriteint32_t(s->nreloc, machofp);
918 offset += s->size;
919 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
920 } else {
921 fwriteint32_t(0, machofp);
922 fwriteint32_t(0, machofp);
923 fwriteint32_t(0, machofp);
924 fwriteint32_t(0, machofp);
927 fwriteint32_t(s->flags, machofp); /* flags */
928 fwriteint32_t(0, machofp); /* reserved */
929 fwriteint32_t(0, machofp); /* reserved */
931 s_addr += s->size;
934 rel_padcnt = rel_base - offset;
935 offset = rel_base + s_reloff;
937 return offset;
940 /* For a given chain of relocs r, write out the entire relocation
941 chain to the object file. */
943 static void macho_write_relocs (struct reloc *r)
945 while (r) {
946 uint32_t word2;
948 fwriteint32_t(r->addr, machofp); /* reloc offset */
950 word2 = r->snum;
951 word2 |= r->pcrel << 24;
952 word2 |= r->length << 25;
953 word2 |= r->ext << 27;
954 word2 |= r->type << 28;
955 fwriteint32_t(word2, machofp); /* reloc data */
957 r = r->next;
961 /* Write out the section data. */
962 static void macho_write_section (void)
964 struct section *s, *s2;
965 struct reloc *r;
966 char *rel_paddata = "\0\0\0";
967 uint8_t fi, *p, *q, blk[4];
968 int32_t l;
970 for (s = sects; s != NULL; s = s->next) {
971 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
972 continue;
974 /* no padding needs to be done to the sections */
976 /* Like a.out Mach-O references things in the data or bss
977 * sections by addresses which are actually relative to the
978 * start of the _text_ section, in the _file_. See outaout.c
979 * for more information. */
980 saa_rewind(s->data);
981 for (r = s->relocs; r != NULL; r = r->next) {
982 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
983 p = q = blk;
984 l = *p++;
986 /* get offset based on relocation type */
987 if (r->length > 0) {
988 l += ((int32_t)*p++) << 8;
990 if (r->length == 2) {
991 l += ((int32_t)*p++) << 16;
992 l += ((int32_t)*p++) << 24;
996 /* If the relocation is internal add to the current section
997 offset. Otherwise the only value we need is the symbol
998 offset which we already have. The linker takes care
999 of the rest of the address. */
1000 if (!r->ext) {
1001 /* add sizes of previous sections to current offset */
1002 for (s2 = sects, fi = 1;
1003 s2 != NULL && fi < r->snum; s2 = s2->next, fi++)
1004 l += s2->size;
1007 /* write new offset back */
1008 if (r->length == 2)
1009 WRITELONG(q, l);
1010 else if (r->length == 1)
1011 WRITESHORT(q, l);
1012 else
1013 *q++ = l & 0xFF;
1015 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1018 /* dump the section data to file */
1019 saa_fpwrite(s->data, machofp);
1022 /* pad last section up to reloc entries on int32_t boundary */
1023 fwrite(rel_paddata, rel_padcnt, 1, machofp);
1025 /* emit relocation entries */
1026 for (s = sects; s != NULL; s = s->next)
1027 macho_write_relocs (s->relocs);
1030 /* Write out the symbol table. We should already have sorted this
1031 before now. */
1032 static void macho_write_symtab (void)
1034 struct symbol *sym;
1035 struct section *s;
1036 int32_t fi;
1037 uint32_t i;
1039 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1041 for (sym = syms; sym != NULL; sym = sym->next) {
1042 if ((sym->type & N_EXT) == 0) {
1043 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1044 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1045 fwrite(&sym->sect, 1, 1, machofp); /* section */
1046 fwriteint16_t(sym->desc, machofp); /* description */
1048 /* Fix up the symbol value now that we know the final section
1049 sizes. */
1050 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1051 for (s = sects, fi = 1;
1052 s != NULL && fi < sym->sect; s = s->next, ++fi)
1053 sym->value += s->size;
1056 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1060 for (i = 0; i < nextdefsym; i++) {
1061 sym = extdefsyms[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
1068 sizes. */
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) */
1078 for (i = 0; i < nundefsym; i++) {
1079 sym = undefsyms[i];
1080 fwriteint32_t(sym->strx, machofp);
1081 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1082 fwrite(&sym->sect, 1, 1, machofp); /* section */
1083 fwriteint16_t(sym->desc, machofp); /* description */
1085 /* Fix up the symbol value now that we know the final section
1086 sizes. */
1087 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1088 for (s = sects, fi = 1;
1089 s != NULL && fi < sym->sect; s = s->next, ++fi)
1090 sym->value += s->size;
1093 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1097 /* Fixup the snum in the relocation entries, we should be
1098 doing this only for externally undefined symbols. */
1099 static void macho_fixup_relocs (struct reloc *r)
1101 struct symbol *sym;
1102 uint32_t i;
1104 while (r != NULL) {
1105 if (r->ext) {
1106 for (i = 0; i < nundefsym; i++) {
1107 sym = undefsyms[i];
1108 if (sym->initial_snum == r->snum) {
1109 r->snum = sym->snum;
1110 break;
1114 r = r->next;
1118 /* Write out the object file. */
1120 static void macho_write (void)
1122 uint32_t offset = 0;
1124 /* mach-o object file structure:
1126 ** mach header
1127 ** uint32_t magic
1128 ** int cpu type
1129 ** int cpu subtype
1130 ** uint32_t mach file type
1131 ** uint32_t number of load commands
1132 ** uint32_t size of all load commands
1133 ** (includes section struct size of segment command)
1134 ** uint32_t flags
1136 ** segment command
1137 ** uint32_t command type == LC_SEGMENT
1138 ** uint32_t size of load command
1139 ** (including section load commands)
1140 ** char[16] segment name
1141 ** uint32_t in-memory offset
1142 ** uint32_t in-memory size
1143 ** uint32_t in-file offset to data area
1144 ** uint32_t in-file size
1145 ** (in-memory size excluding zerofill sections)
1146 ** int maximum vm protection
1147 ** int initial vm protection
1148 ** uint32_t number of sections
1149 ** uint32_t flags
1151 ** section commands
1152 ** char[16] section name
1153 ** char[16] segment name
1154 ** uint32_t in-memory offset
1155 ** uint32_t in-memory size
1156 ** uint32_t in-file offset
1157 ** uint32_t alignment
1158 ** (irrelevant in MH_OBJECT)
1159 ** uint32_t in-file offset of relocation entires
1160 ** uint32_t number of relocations
1161 ** uint32_t flags
1162 ** uint32_t reserved
1163 ** uint32_t reserved
1165 ** symbol table command
1166 ** uint32_t command type == LC_SYMTAB
1167 ** uint32_t size of load command
1168 ** uint32_t symbol table offset
1169 ** uint32_t number of symbol table entries
1170 ** uint32_t string table offset
1171 ** uint32_t string table size
1173 ** raw section data
1175 ** padding to int32_t boundary
1177 ** relocation data (struct reloc)
1178 ** int32_t offset
1179 ** uint data (symbolnum, pcrel, length, extern, type)
1181 ** symbol table data (struct nlist)
1182 ** int32_t string table entry number
1183 ** uint8_t type
1184 ** (extern, absolute, defined in section)
1185 ** uint8_t section
1186 ** (0 for global symbols, section number of definition (>= 1, <=
1187 ** 254) for local symbols, size of variable for common symbols
1188 ** [type == extern])
1189 ** int16_t description
1190 ** (for stab debugging format)
1191 ** uint32_t value (i.e. file offset) of symbol or stab offset
1193 ** string table data
1194 ** list of null-terminated strings
1197 /* Emit the Mach-O header. */
1198 macho_write_header();
1200 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1202 /* emit the segment load command */
1203 if (seg_nsects > 0)
1204 offset = macho_write_segment (offset);
1205 else
1206 error(ERR_WARNING, "no sections?");
1208 if (nsyms > 0) {
1209 /* write out symbol command */
1210 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1211 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1212 fwriteint32_t(offset, machofp); /* symbol table offset */
1213 fwriteint32_t(nsyms, machofp); /* number of symbol
1214 ** table entries */
1216 offset += nsyms * MACHO_NLIST_SIZE;
1217 fwriteint32_t(offset, machofp); /* string table offset */
1218 fwriteint32_t(strslen, machofp); /* string table size */
1221 /* emit section data */
1222 if (seg_nsects > 0)
1223 macho_write_section ();
1225 /* emit symbol table if we have symbols */
1226 if (nsyms > 0)
1227 macho_write_symtab ();
1229 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1231 /* emit string table */
1232 saa_fpwrite(strs, machofp);
1234 /* We do quite a bit here, starting with finalizing all of the data
1235 for the object file, writing, and then freeing all of the data from
1236 the file. */
1238 static void macho_cleanup(int debuginfo)
1240 struct section *s;
1241 struct reloc *r;
1242 struct symbol *sym;
1244 (void)debuginfo;
1246 /* Sort all symbols. */
1247 macho_layout_symbols (&nsyms, &strslen);
1249 /* Fixup relocation entries */
1250 for (s = sects; s != NULL; s = s->next) {
1251 macho_fixup_relocs (s->relocs);
1254 /* First calculate and finalize needed values. */
1255 macho_calculate_sizes();
1256 macho_write();
1258 /* done - yay! */
1259 fclose(machofp);
1261 /* free up everything */
1262 while (sects->next) {
1263 s = sects;
1264 sects = sects->next;
1266 saa_free(s->data);
1267 while (s->relocs != NULL) {
1268 r = s->relocs;
1269 s->relocs = s->relocs->next;
1270 nasm_free(r);
1273 nasm_free(s);
1276 saa_free(strs);
1277 raa_free(extsyms);
1279 if (syms) {
1280 while (syms->next) {
1281 sym = syms;
1282 syms = syms->next;
1284 nasm_free (sym);
1289 /* Debugging routines. */
1290 static void debug_reloc (struct reloc *r)
1292 fprintf (stdout, "reloc:\n");
1293 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1294 fprintf (stdout, "\tsnum: %d\n", r->snum);
1295 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1296 fprintf (stdout, "\tlength: %d\n", r->length);
1297 fprintf (stdout, "\text: %d\n", r->ext);
1298 fprintf (stdout, "\ttype: %d\n", r->type);
1301 static void debug_section_relocs (struct section *s)
1303 struct reloc *r = s->relocs;
1305 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1307 while (r != NULL) {
1308 debug_reloc (r);
1309 r = r->next;
1313 struct ofmt of_macho = {
1314 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",
1315 "macho",
1316 NULL,
1317 null_debug_arr,
1318 &null_debug_form,
1319 macho_stdmac,
1320 macho_init,
1321 macho_setinfo,
1322 macho_output,
1323 macho_symdef,
1324 macho_section,
1325 macho_segbase,
1326 macho_directive,
1327 macho_filename,
1328 macho_cleanup
1331 #endif
1334 * Local Variables:
1335 * mode:c
1336 * c-basic-offset:4
1337 * End:
1339 * end of file */