NASM 2.06rc2
[nasm/avx512.git] / output / outmacho.c
blob30234a96706c13a39756711f35b5a7711291120f
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 "raa.h"
25 #include "outform.h"
26 #include "outlib.h"
28 #if defined(OF_MACHO)
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 addr; /* in-memory address (subject to alignment) */
67 uint32_t size; /* in-memory and -file size */
68 uint32_t nreloc; /* relocation entry count */
69 uint32_t flags; /* type and attributes (masked) */
72 #define SECTION_TYPE 0x000000ff /* section type mask */
74 #define S_REGULAR (0x0) /* standard section */
75 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
77 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
78 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
79 machine instructions */
80 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
81 relocation entries */
82 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
83 relocation entries */
86 static struct sectmap {
87 const char *nasmsect;
88 const char *segname;
89 const char *sectname;
90 const int32_t flags;
91 } sectmap[] = {
92 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
93 {".data", "__DATA", "__data", S_REGULAR},
94 {".rodata", "__DATA", "__const", S_REGULAR},
95 {".bss", "__DATA", "__bss", S_ZEROFILL},
96 {NULL, NULL, NULL, 0}
99 struct reloc {
100 /* nasm internal data */
101 struct reloc *next;
103 /* data that goes into the file */
104 int32_t addr; /* op's offset in section */
105 unsigned int snum:24, /* contains symbol index if
106 ** ext otherwise in-file
107 ** section number */
108 pcrel:1, /* relative relocation */
109 length:2, /* 0=byte, 1=word, 2=int32_t */
110 ext:1, /* external symbol referenced */
111 type:4; /* reloc type, 0 for us */
114 #define R_ABS 0 /* absolute relocation */
115 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
116 ** highest bit == 1 */
118 struct symbol {
119 /* nasm internal data */
120 struct symbol *next; /* next symbol in the list */
121 char *name; /* name of this symbol */
122 int32_t initial_snum; /* symbol number used above in
123 reloc */
124 int32_t snum; /* true snum for reloc */
126 /* data that goes into the file */
127 int32_t strx; /* string table index */
128 uint8_t type; /* symbol type */
129 uint8_t sect; /* NO_SECT or section number */
130 int16_t desc; /* for stab debugging, 0 for us */
131 uint32_t value; /* offset of symbol in section */
134 /* symbol type bits */
135 #define N_EXT 0x01 /* global or external symbol */
137 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
138 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
139 #define N_SECT 0xe /* defined symbol, n_sect holds
140 ** section number */
142 #define N_TYPE 0x0e /* type bit mask */
144 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
146 /* special section number values */
147 #define NO_SECT 0 /* no section, invalid */
148 #define MAX_SECT 255 /* maximum number of sections */
150 static struct section *sects, **sectstail;
151 static struct symbol *syms, **symstail;
152 static uint32_t nsyms;
154 /* These variables are set by macho_layout_symbols() to organize
155 the symbol table and string table in order the dynamic linker
156 expects. They are then used in macho_write() to put out the
157 symbols and strings in that order.
159 The order of the symbol table is:
160 local symbols
161 defined external symbols (sorted by name)
162 undefined external symbols (sorted by name)
164 The order of the string table is:
165 strings for external symbols
166 strings for local symbols
168 static uint32_t ilocalsym = 0;
169 static uint32_t iextdefsym = 0;
170 static uint32_t iundefsym = 0;
171 static uint32_t nlocalsym;
172 static uint32_t nextdefsym;
173 static uint32_t nundefsym;
174 static struct symbol **extdefsyms = NULL;
175 static struct symbol **undefsyms = NULL;
177 static struct RAA *extsyms;
178 static struct SAA *strs;
179 static uint32_t strslen;
181 static FILE *machofp;
182 static efunc error;
183 static evalfunc evaluate;
185 extern struct ofmt of_macho;
187 /* Global file information. This should be cleaned up into either
188 a structure or as function arguments. */
189 uint32_t head_ncmds = 0;
190 uint32_t head_sizeofcmds = 0;
191 uint32_t seg_filesize = 0;
192 uint32_t seg_vmsize = 0;
193 uint32_t seg_nsects = 0;
194 uint32_t rel_padcnt = 0;
197 #define xstrncpy(xdst, xsrc) \
198 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
199 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
200 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
202 #define align(x, y) \
203 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
205 #define alignint32_t(x) \
206 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
208 static void debug_reloc (struct reloc *);
209 static void debug_section_relocs (struct section *) _unused;
211 static int exact_log2 (uint32_t align)
213 if (align == 0) {
214 return 0;
215 } else if (align & (align-1)) {
216 return -1; /* Not a power of 2 */
217 } else {
218 #ifdef HAVE_GNUC_4
219 return __builtin_ctzl (align);
220 #else
221 uint32_t result = 0;
223 /* We know exactly one bit is set at this point. */
224 if (align & 0xffff0000)
225 result |= 16;
226 if (align & 0xff00ff00)
227 result |= 8;
228 if (align & 0xf0f0f0f0)
229 result |= 4;
230 if (align & 0xcccccccc)
231 result |= 2;
232 if (align & 0xaaaaaaaa)
233 result |= 1;
235 return result;
236 #endif
240 static struct section *get_section_by_name(const char *segname,
241 const char *sectname)
243 struct section *s;
245 for (s = sects; s != NULL; s = s->next)
246 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
247 break;
249 return s;
252 static struct section *get_section_by_index(const int32_t index)
254 struct section *s;
256 for (s = sects; s != NULL; s = s->next)
257 if (index == s->index)
258 break;
260 return s;
263 static int32_t get_section_index_by_name(const char *segname,
264 const char *sectname)
266 struct section *s;
268 for (s = sects; s != NULL; s = s->next)
269 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
270 return s->index;
272 return -1;
275 static char *get_section_name_by_index(const int32_t index)
277 struct section *s;
279 for (s = sects; s != NULL; s = s->next)
280 if (index == s->index)
281 return s->sectname;
283 return NULL;
286 static uint8_t get_section_fileindex_by_index(const int32_t index)
288 struct section *s;
289 uint8_t i = 1;
291 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
292 if (index == s->index)
293 return i;
295 if (i == MAX_SECT)
296 error(ERR_WARNING,
297 "too many sections (>255) - clipped by fileindex");
299 return NO_SECT;
302 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
303 evalfunc eval)
305 char zero = 0;
307 machofp = fp;
308 error = errfunc;
309 evaluate = eval;
311 (void)ldef; /* placate optimisers */
313 sects = NULL;
314 sectstail = &sects;
316 syms = NULL;
317 symstail = &syms;
318 nsyms = 0;
319 nlocalsym = 0;
320 nextdefsym = 0;
321 nundefsym = 0;
323 extsyms = raa_init();
324 strs = saa_init(1L);
326 /* string table starts with a zero byte - don't ask why */
327 saa_wbytes(strs, &zero, sizeof(char));
328 strslen = 1;
331 static int macho_setinfo(enum geninfo type, char **val)
333 (void)type;
334 (void)val;
335 return 0;
338 static void sect_write(struct section *sect,
339 const uint8_t *data, uint32_t len)
341 saa_wbytes(sect->data, data, len);
342 sect->size += len;
345 static void add_reloc(struct section *sect, int32_t section,
346 int pcrel, int bytes)
348 struct reloc *r;
349 int32_t fi;
351 /* NeXT as puts relocs in reversed order (address-wise) into the
352 ** files, so we do the same, doesn't seem to make much of a
353 ** difference either way */
354 r = nasm_malloc(sizeof(struct reloc));
355 r->next = sect->relocs;
356 sect->relocs = r;
358 /* the current end of the section will be the symbol's address for
359 ** now, might have to be fixed by macho_fixup_relocs() later on. make
360 ** sure we don't make the symbol scattered by setting the highest
361 ** bit by accident */
362 r->addr = sect->size & ~R_SCATTERED;
363 r->ext = 0;
364 r->pcrel = pcrel;
366 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
367 r->length = bytes >> 1;
369 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
370 r->type = 0;
372 if (section == NO_SEG) {
373 /* absolute local symbol if no section index given */
374 r->snum = R_ABS;
375 } else {
376 fi = get_section_fileindex_by_index(section);
378 if (fi == NO_SECT) {
379 /* external symbol if no section with that index known,
380 ** symbol number was saved in macho_symdef() */
381 r->snum = raa_read(extsyms, section);
382 r->ext = 1;
383 } else {
384 /* local symbol in section fi */
385 r->snum = fi;
389 ++sect->nreloc;
392 static void macho_output(int32_t secto, const void *data,
393 enum out_type type, uint64_t size,
394 int32_t section, int32_t wrt)
396 struct section *s, *sbss;
397 int32_t addr;
398 uint8_t mydata[4], *p;
400 if (wrt != NO_SEG) {
401 wrt = NO_SEG;
402 error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
403 /* continue to do _something_ */
406 if (secto == NO_SEG) {
407 if (type != OUT_RESERVE)
408 error(ERR_NONFATAL, "attempt to assemble code in "
409 "[ABSOLUTE] space");
411 return;
414 s = get_section_by_index(secto);
416 if (s == NULL) {
417 error(ERR_WARNING, "attempt to assemble code in"
418 " section %d: defaulting to `.text'", secto);
419 s = get_section_by_name("__TEXT", "__text");
421 /* should never happen */
422 if (s == NULL)
423 error(ERR_PANIC, "text section not found");
426 sbss = get_section_by_name("__DATA", "__bss");
428 if (s == sbss && type != OUT_RESERVE) {
429 error(ERR_WARNING, "attempt to initialize memory in the"
430 " BSS section: ignored");
431 s->size += realsize(type, size);
432 return;
435 switch (type) {
436 case OUT_RESERVE:
437 if (s != sbss) {
438 error(ERR_WARNING, "uninitialized space declared in"
439 " %s section: zeroing",
440 get_section_name_by_index(secto));
442 sect_write(s, NULL, size);
443 } else
444 s->size += size;
446 break;
448 case OUT_RAWDATA:
449 if (section != NO_SEG)
450 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
452 sect_write(s, data, size);
453 break;
455 case OUT_ADDRESS:
456 addr = *(int64_t *)data;
458 if (section != NO_SEG) {
459 if (section % 2) {
460 error(ERR_NONFATAL, "Mach-O format does not support"
461 " section base references");
462 } else
463 add_reloc(s, section, 0, size);
466 p = mydata;
467 WRITEADDR(p, addr, size);
468 sect_write(s, mydata, size);
469 break;
471 case OUT_REL2ADR:
472 if (section == secto)
473 error(ERR_PANIC, "intra-section OUT_REL2ADR");
475 if (section != NO_SEG && section % 2) {
476 error(ERR_NONFATAL, "Mach-O format does not support"
477 " section base references");
478 } else
479 add_reloc(s, section, 1, 2);
481 p = mydata;
482 WRITESHORT(p, *(int32_t *)data - (size + s->size));
483 sect_write(s, mydata, 2L);
484 break;
486 case OUT_REL4ADR:
487 if (section == secto)
488 error(ERR_PANIC, "intra-section OUT_REL4ADR");
490 if (section != NO_SEG && section % 2) {
491 error(ERR_NONFATAL, "Mach-O format does not support"
492 " section base references");
493 } else
494 add_reloc(s, section, 1, 4);
496 p = mydata;
497 WRITELONG(p, *(int32_t *)data - (size + s->size));
498 sect_write(s, mydata, 4L);
499 break;
501 default:
502 error(ERR_PANIC, "unknown output type?");
503 break;
507 static int32_t macho_section(char *name, int pass, int *bits)
509 int32_t index, originalIndex;
510 char *sectionAttributes;
511 struct sectmap *sm;
512 struct section *s;
514 (void)pass;
516 /* Default to 32 bits. */
517 if (!name) {
518 *bits = 32;
519 name = ".text";
520 sectionAttributes = NULL;
521 } else {
522 sectionAttributes = name;
523 name = nasm_strsep(&sectionAttributes, " \t");
526 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
527 /* make lookup into section name translation table */
528 if (!strcmp(name, sm->nasmsect)) {
529 char *currentAttribute;
531 /* try to find section with that name */
532 originalIndex = index = get_section_index_by_name(sm->segname,
533 sm->sectname);
535 /* create it if it doesn't exist yet */
536 if (index == -1) {
537 s = *sectstail = nasm_malloc(sizeof(struct section));
538 s->next = NULL;
539 sectstail = &s->next;
541 s->data = saa_init(1L);
542 s->index = seg_alloc();
543 s->relocs = NULL;
544 s->align = -1;
546 xstrncpy(s->segname, sm->segname);
547 xstrncpy(s->sectname, sm->sectname);
548 s->size = 0;
549 s->nreloc = 0;
550 s->flags = sm->flags;
552 index = s->index;
553 } else {
554 s = get_section_by_index(index);
557 while ((NULL != sectionAttributes)
558 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
559 if (0 != *currentAttribute) {
560 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
561 char *end;
562 int newAlignment, value;
564 value = strtoul(currentAttribute + 6, (char**)&end, 0);
565 newAlignment = exact_log2(value);
567 if (0 != *end) {
568 error(ERR_PANIC,
569 "unknown or missing alignment value \"%s\" "
570 "specified for section \"%s\"",
571 currentAttribute + 6,
572 name);
573 return NO_SEG;
574 } else if (0 > newAlignment) {
575 error(ERR_PANIC,
576 "alignment of %d (for section \"%s\") is not "
577 "a power of two",
578 value,
579 name);
580 return NO_SEG;
583 if ((-1 != originalIndex)
584 && (s->align != newAlignment)
585 && (s->align != -1)) {
586 error(ERR_PANIC,
587 "section \"%s\" has already been specified "
588 "with alignment %d, conflicts with new "
589 "alignment of %d",
590 name,
591 (1 << s->align),
592 value);
593 return NO_SEG;
596 s->align = newAlignment;
597 } else if (!nasm_stricmp("data", currentAttribute)) {
598 /* Do nothing; 'data' is implicit */
599 } else {
600 error(ERR_PANIC,
601 "unknown section attribute %s for section %s",
602 currentAttribute,
603 name);
604 return NO_SEG;
609 return index;
613 error(ERR_PANIC, "invalid section name %s", name);
614 return NO_SEG;
617 static void macho_symdef(char *name, int32_t section, int64_t offset,
618 int is_global, char *special)
620 struct symbol *sym;
622 if (special) {
623 error(ERR_NONFATAL, "The Mach-O output format does "
624 "not support any special symbol types");
625 return;
628 if (is_global == 3) {
629 error(ERR_NONFATAL, "The Mach-O format does not "
630 "(yet) support forward reference fixups.");
631 return;
634 sym = *symstail = nasm_malloc(sizeof(struct symbol));
635 sym->next = NULL;
636 symstail = &sym->next;
638 sym->name = name;
639 sym->strx = strslen;
640 sym->type = 0;
641 sym->desc = 0;
642 sym->value = offset;
643 sym->initial_snum = -1;
645 /* external and common symbols get N_EXT */
646 if (is_global != 0)
647 sym->type |= N_EXT;
649 if (section == NO_SEG) {
650 /* symbols in no section get absolute */
651 sym->type |= N_ABS;
652 sym->sect = NO_SECT;
653 } else {
654 sym->type |= N_SECT;
656 /* get the in-file index of the section the symbol was defined in */
657 sym->sect = get_section_fileindex_by_index(section);
659 if (sym->sect == NO_SECT) {
660 /* remember symbol number of references to external
661 ** symbols, this works because every external symbol gets
662 ** its own section number allocated internally by nasm and
663 ** can so be used as a key */
664 extsyms = raa_write(extsyms, section, nsyms);
665 sym->initial_snum = nsyms;
667 switch (is_global) {
668 case 1:
669 case 2:
670 /* there isn't actually a difference between global
671 ** and common symbols, both even have their size in
672 ** sym->value */
673 sym->type = N_EXT;
674 break;
676 default:
677 /* give an error on unfound section if it's not an
678 ** external or common symbol (assemble_file() does a
679 ** seg_alloc() on every call for them) */
680 error(ERR_PANIC, "in-file index for section %d not found",
681 section);
686 ++nsyms;
689 static int32_t macho_segbase(int32_t section)
691 return section;
694 static int macho_directive(char *directive, char *value, int pass)
696 (void)directive;
697 (void)value;
698 (void)pass;
699 return 0;
702 static void macho_filename(char *inname, char *outname, efunc error)
704 standard_extension(inname, outname, ".o", error);
707 extern macros_t macho_stdmac[];
709 /* Comparison function for qsort symbol layout. */
710 static int layout_compare (const struct symbol **s1,
711 const struct symbol **s2)
713 return (strcmp ((*s1)->name, (*s2)->name));
716 /* The native assembler does a few things in a similar function
718 * Remove temporary labels
719 * Sort symbols according to local, external, undefined (by name)
720 * Order the string table
722 We do not remove temporary labels right now.
724 numsyms is the total number of symbols we have. strtabsize is the
725 number entries in the string table. */
727 static void macho_layout_symbols (uint32_t *numsyms,
728 uint32_t *strtabsize)
730 struct symbol *sym, **symp;
731 uint32_t i,j;
733 *numsyms = 0;
734 *strtabsize = sizeof (char);
736 symp = &syms;
738 while ((sym = *symp)) {
739 /* Undefined symbols are now external. */
740 if (sym->type == N_UNDF)
741 sym->type |= N_EXT;
743 if ((sym->type & N_EXT) == 0) {
744 sym->snum = *numsyms;
745 *numsyms = *numsyms + 1;
746 nlocalsym++;
748 else {
749 if ((sym->type & N_TYPE) != N_UNDF)
750 nextdefsym++;
751 else
752 nundefsym++;
754 /* If we handle debug info we'll want
755 to check for it here instead of just
756 adding the symbol to the string table. */
757 sym->strx = *strtabsize;
758 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
759 *strtabsize += strlen(sym->name) + 1;
761 symp = &(sym->next);
764 /* Next, sort the symbols. Most of this code is a direct translation from
765 the Apple cctools symbol layout. We need to keep compatibility with that. */
766 /* Set the indexes for symbol groups into the symbol table */
767 ilocalsym = 0;
768 iextdefsym = nlocalsym;
769 iundefsym = nlocalsym + nextdefsym;
771 /* allocate arrays for sorting externals by name */
772 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
773 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
775 i = 0;
776 j = 0;
778 symp = &syms;
780 while ((sym = *symp)) {
782 if((sym->type & N_EXT) == 0) {
783 sym->strx = *strtabsize;
784 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
785 *strtabsize += strlen(sym->name) + 1;
787 else {
788 if((sym->type & N_TYPE) != N_UNDF)
789 extdefsyms[i++] = sym;
790 else
791 undefsyms[j++] = sym;
793 symp = &(sym->next);
796 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
797 (int (*)(const void *, const void *))layout_compare);
798 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
799 (int (*)(const void *, const void *))layout_compare);
801 for(i = 0; i < nextdefsym; i++) {
802 extdefsyms[i]->snum = *numsyms;
803 *numsyms += 1;
805 for(j = 0; j < nundefsym; j++) {
806 undefsyms[j]->snum = *numsyms;
807 *numsyms += 1;
811 /* Calculate some values we'll need for writing later. */
813 static void macho_calculate_sizes (void)
815 struct section *s;
817 /* count sections and calculate in-memory and in-file offsets */
818 for (s = sects; s != NULL; s = s->next) {
819 uint32_t pad = 0;
821 /* zerofill sections aren't actually written to the file */
822 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
823 seg_filesize += s->size;
825 /* recalculate segment address based on alignment and vm size */
826 s->addr = seg_vmsize;
827 /* we need section alignment to calculate final section address */
828 if (s->align == -1)
829 s->align = DEFAULT_SECTION_ALIGNMENT;
830 if(s->align) {
831 uint32_t newaddr = align(s->addr, 1 << s->align);
832 pad = newaddr - s->addr;
833 s->addr = newaddr;
836 seg_vmsize += s->size + pad;
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 rel_base = alignint32_t (offset + seg_filesize);
872 uint32_t s_reloff = 0;
873 struct section *s;
875 fwriteint32_t(LC_SEGMENT, machofp); /* cmd == LC_SEGMENT */
877 /* size of load command including section load commands */
878 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
879 MACHO_SECTCMD_SIZE, machofp);
881 /* in an MH_OBJECT file all sections are in one unnamed (name
882 ** all zeros) segment */
883 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp);
884 fwriteint32_t(0, machofp); /* in-memory offset */
885 fwriteint32_t(seg_vmsize, machofp); /* in-memory size */
886 fwriteint32_t(offset, machofp); /* in-file offset to data */
887 fwriteint32_t(seg_filesize, machofp); /* in-file size */
888 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
889 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
890 fwriteint32_t(seg_nsects, machofp); /* number of sections */
891 fwriteint32_t(0, machofp); /* no flags */
893 /* emit section headers */
894 for (s = sects; s != NULL; s = s->next) {
895 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
896 fwrite(s->segname, sizeof(s->segname), 1, machofp);
897 fwriteint32_t(s->addr, machofp);
898 fwriteint32_t(s->size, machofp);
900 /* dummy data for zerofill sections or proper values */
901 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
902 fwriteint32_t(offset, machofp);
903 /* Write out section alignment, as a power of two.
904 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
905 if (s->align == -1)
906 s->align = DEFAULT_SECTION_ALIGNMENT;
907 fwriteint32_t(s->align, machofp);
908 /* To be compatible with cctools as we emit
909 a zero reloff if we have no relocations. */
910 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
911 fwriteint32_t(s->nreloc, machofp);
913 offset += s->size;
914 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
915 } else {
916 fwriteint32_t(0, machofp);
917 fwriteint32_t(0, machofp);
918 fwriteint32_t(0, machofp);
919 fwriteint32_t(0, machofp);
922 fwriteint32_t(s->flags, machofp); /* flags */
923 fwriteint32_t(0, machofp); /* reserved */
924 fwriteint32_t(0, machofp); /* reserved */
927 rel_padcnt = rel_base - offset;
928 offset = rel_base + s_reloff;
930 return offset;
933 /* For a given chain of relocs r, write out the entire relocation
934 chain to the object file. */
936 static void macho_write_relocs (struct reloc *r)
938 while (r) {
939 uint32_t word2;
941 fwriteint32_t(r->addr, machofp); /* reloc offset */
943 word2 = r->snum;
944 word2 |= r->pcrel << 24;
945 word2 |= r->length << 25;
946 word2 |= r->ext << 27;
947 word2 |= r->type << 28;
948 fwriteint32_t(word2, machofp); /* reloc data */
950 r = r->next;
954 /* Write out the section data. */
955 static void macho_write_section (void)
957 struct section *s, *s2;
958 struct reloc *r;
959 char *rel_paddata = "\0\0\0";
960 uint8_t *p, *q, blk[4];
961 int32_t l;
963 for (s = sects; s != NULL; s = s->next) {
964 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
965 continue;
967 /* no padding needs to be done to the sections */
969 /* Like a.out Mach-O references things in the data or bss
970 * sections by addresses which are actually relative to the
971 * start of the _text_ section, in the _file_. See outaout.c
972 * for more information. */
973 saa_rewind(s->data);
974 for (r = s->relocs; r != NULL; r = r->next) {
975 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
976 p = q = blk;
977 l = *p++;
979 /* get offset based on relocation type */
980 if (r->length > 0) {
981 l += ((int32_t)*p++) << 8;
983 if (r->length == 2) {
984 l += ((int32_t)*p++) << 16;
985 l += ((int32_t)*p++) << 24;
989 /* If the relocation is internal add to the current section
990 offset. Otherwise the only value we need is the symbol
991 offset which we already have. The linker takes care
992 of the rest of the address. */
993 if (!r->ext) {
994 /* generate final address by section address and offset */
995 s2 = get_section_by_index(r->snum);
996 if(s2)
997 l += s2->addr; // else what?!?
1000 /* write new offset back */
1001 if (r->length == 2)
1002 WRITELONG(q, l);
1003 else if (r->length == 1)
1004 WRITESHORT(q, l);
1005 else
1006 *q++ = l & 0xFF;
1008 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1011 /* dump the section data to file */
1012 saa_fpwrite(s->data, machofp);
1015 /* pad last section up to reloc entries on int32_t boundary */
1016 fwrite(rel_paddata, rel_padcnt, 1, machofp);
1018 /* emit relocation entries */
1019 for (s = sects; s != NULL; s = s->next)
1020 macho_write_relocs (s->relocs);
1023 /* Write out the symbol table. We should already have sorted this
1024 before now. */
1025 static void macho_write_symtab (void)
1027 struct symbol *sym;
1028 struct section *s;
1029 int32_t fi;
1030 uint32_t i;
1032 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1034 for (sym = syms; sym != NULL; sym = sym->next) {
1035 if ((sym->type & N_EXT) == 0) {
1036 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1037 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1038 fwrite(&sym->sect, 1, 1, machofp); /* section */
1039 fwriteint16_t(sym->desc, machofp); /* description */
1041 /* Fix up the symbol value now that we know the final section
1042 sizes. */
1043 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1044 for (s = sects, fi = 1;
1045 s != NULL && fi < sym->sect; s = s->next, ++fi)
1046 sym->value += s->size;
1049 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1053 for (i = 0; i < nextdefsym; i++) {
1054 sym = extdefsyms[i];
1055 fwriteint32_t(sym->strx, machofp);
1056 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1057 fwrite(&sym->sect, 1, 1, machofp); /* section */
1058 fwriteint16_t(sym->desc, machofp); /* description */
1060 /* Fix up the symbol value now that we know the final section
1061 sizes. */
1062 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1063 for (s = sects, fi = 1;
1064 s != NULL && fi < sym->sect; s = s->next, ++fi)
1065 sym->value += s->size;
1068 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1071 for (i = 0; i < nundefsym; i++) {
1072 sym = undefsyms[i];
1073 fwriteint32_t(sym->strx, machofp);
1074 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1075 fwrite(&sym->sect, 1, 1, machofp); /* section */
1076 fwriteint16_t(sym->desc, machofp); /* description */
1078 /* Fix up the symbol value now that we know the final section
1079 sizes. */
1080 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1081 for (s = sects, fi = 1;
1082 s != NULL && fi < sym->sect; s = s->next, ++fi)
1083 sym->value += s->size;
1086 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1090 /* Fixup the snum in the relocation entries, we should be
1091 doing this only for externally undefined symbols. */
1092 static void macho_fixup_relocs (struct reloc *r)
1094 struct symbol *sym;
1095 uint32_t i;
1097 while (r != NULL) {
1098 if (r->ext) {
1099 for (i = 0; i < nundefsym; i++) {
1100 sym = undefsyms[i];
1101 if (sym->initial_snum == r->snum) {
1102 r->snum = sym->snum;
1103 break;
1107 r = r->next;
1111 /* Write out the object file. */
1113 static void macho_write (void)
1115 uint32_t offset = 0;
1117 /* mach-o object file structure:
1119 ** mach header
1120 ** uint32_t magic
1121 ** int cpu type
1122 ** int cpu subtype
1123 ** uint32_t mach file type
1124 ** uint32_t number of load commands
1125 ** uint32_t size of all load commands
1126 ** (includes section struct size of segment command)
1127 ** uint32_t flags
1129 ** segment command
1130 ** uint32_t command type == LC_SEGMENT
1131 ** uint32_t size of load command
1132 ** (including section load commands)
1133 ** char[16] segment name
1134 ** uint32_t in-memory offset
1135 ** uint32_t in-memory size
1136 ** uint32_t in-file offset to data area
1137 ** uint32_t in-file size
1138 ** (in-memory size excluding zerofill sections)
1139 ** int maximum vm protection
1140 ** int initial vm protection
1141 ** uint32_t number of sections
1142 ** uint32_t flags
1144 ** section commands
1145 ** char[16] section name
1146 ** char[16] segment name
1147 ** uint32_t in-memory offset
1148 ** uint32_t in-memory size
1149 ** uint32_t in-file offset
1150 ** uint32_t alignment
1151 ** (irrelevant in MH_OBJECT)
1152 ** uint32_t in-file offset of relocation entires
1153 ** uint32_t number of relocations
1154 ** uint32_t flags
1155 ** uint32_t reserved
1156 ** uint32_t reserved
1158 ** symbol table command
1159 ** uint32_t command type == LC_SYMTAB
1160 ** uint32_t size of load command
1161 ** uint32_t symbol table offset
1162 ** uint32_t number of symbol table entries
1163 ** uint32_t string table offset
1164 ** uint32_t string table size
1166 ** raw section data
1168 ** padding to int32_t boundary
1170 ** relocation data (struct reloc)
1171 ** int32_t offset
1172 ** uint data (symbolnum, pcrel, length, extern, type)
1174 ** symbol table data (struct nlist)
1175 ** int32_t string table entry number
1176 ** uint8_t type
1177 ** (extern, absolute, defined in section)
1178 ** uint8_t section
1179 ** (0 for global symbols, section number of definition (>= 1, <=
1180 ** 254) for local symbols, size of variable for common symbols
1181 ** [type == extern])
1182 ** int16_t description
1183 ** (for stab debugging format)
1184 ** uint32_t value (i.e. file offset) of symbol or stab offset
1186 ** string table data
1187 ** list of null-terminated strings
1190 /* Emit the Mach-O header. */
1191 macho_write_header();
1193 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1195 /* emit the segment load command */
1196 if (seg_nsects > 0)
1197 offset = macho_write_segment (offset);
1198 else
1199 error(ERR_WARNING, "no sections?");
1201 if (nsyms > 0) {
1202 /* write out symbol command */
1203 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1204 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1205 fwriteint32_t(offset, machofp); /* symbol table offset */
1206 fwriteint32_t(nsyms, machofp); /* number of symbol
1207 ** table entries */
1209 offset += nsyms * MACHO_NLIST_SIZE;
1210 fwriteint32_t(offset, machofp); /* string table offset */
1211 fwriteint32_t(strslen, machofp); /* string table size */
1214 /* emit section data */
1215 if (seg_nsects > 0)
1216 macho_write_section ();
1218 /* emit symbol table if we have symbols */
1219 if (nsyms > 0)
1220 macho_write_symtab ();
1222 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1224 /* emit string table */
1225 saa_fpwrite(strs, machofp);
1227 /* We do quite a bit here, starting with finalizing all of the data
1228 for the object file, writing, and then freeing all of the data from
1229 the file. */
1231 static void macho_cleanup(int debuginfo)
1233 struct section *s;
1234 struct reloc *r;
1235 struct symbol *sym;
1237 (void)debuginfo;
1239 /* Sort all symbols. */
1240 macho_layout_symbols (&nsyms, &strslen);
1242 /* Fixup relocation entries */
1243 for (s = sects; s != NULL; s = s->next) {
1244 macho_fixup_relocs (s->relocs);
1247 /* First calculate and finalize needed values. */
1248 macho_calculate_sizes();
1249 macho_write();
1251 /* done - yay! */
1252 fclose(machofp);
1254 /* free up everything */
1255 while (sects->next) {
1256 s = sects;
1257 sects = sects->next;
1259 saa_free(s->data);
1260 while (s->relocs != NULL) {
1261 r = s->relocs;
1262 s->relocs = s->relocs->next;
1263 nasm_free(r);
1266 nasm_free(s);
1269 saa_free(strs);
1270 raa_free(extsyms);
1272 if (syms) {
1273 while (syms->next) {
1274 sym = syms;
1275 syms = syms->next;
1277 nasm_free (sym);
1282 /* Debugging routines. */
1283 static void debug_reloc (struct reloc *r)
1285 fprintf (stdout, "reloc:\n");
1286 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1287 fprintf (stdout, "\tsnum: %d\n", r->snum);
1288 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1289 fprintf (stdout, "\tlength: %d\n", r->length);
1290 fprintf (stdout, "\text: %d\n", r->ext);
1291 fprintf (stdout, "\ttype: %d\n", r->type);
1294 static void debug_section_relocs (struct section *s)
1296 struct reloc *r = s->relocs;
1298 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1300 while (r != NULL) {
1301 debug_reloc (r);
1302 r = r->next;
1306 struct ofmt of_macho = {
1307 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",
1308 "macho",
1309 NULL,
1310 null_debug_arr,
1311 &null_debug_form,
1312 macho_stdmac,
1313 macho_init,
1314 macho_setinfo,
1315 macho_output,
1316 macho_symdef,
1317 macho_section,
1318 macho_segbase,
1319 macho_directive,
1320 macho_filename,
1321 macho_cleanup
1324 #endif
1327 * Local Variables:
1328 * mode:c
1329 * c-basic-offset:4
1330 * End:
1332 * end of file */