directives.pl: trivial formatting fix
[nasm.git] / output / outmacho32.c
blob51c22318c1a8129381a7116ff44e923fa0e3df22
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outmacho.c output routines for the Netwide Assembler to produce
36 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
39 /* Most of this file is, like Mach-O itself, based on a.out. For more
40 * guidelines see outaout.c. */
42 #include "compiler.h"
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <inttypes.h>
50 #include "nasm.h"
51 #include "nasmlib.h"
52 #include "saa.h"
53 #include "raa.h"
54 #include "eval.h"
55 #include "output/outform.h"
56 #include "output/outlib.h"
58 #if defined(OF_MACHO32)
60 /* Mach-O in-file header structure sizes */
61 #define MACHO_HEADER_SIZE (28)
62 #define MACHO_SEGCMD_SIZE (56)
63 #define MACHO_SECTCMD_SIZE (68)
64 #define MACHO_SYMCMD_SIZE (24)
65 #define MACHO_NLIST_SIZE (12)
66 #define MACHO_RELINFO_SIZE (8)
68 /* Mach-O file header values */
69 #define MH_MAGIC (0xfeedface)
70 #define CPU_TYPE_I386 (7) /* x86 platform */
71 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
72 #define MH_OBJECT (0x1) /* object file */
74 #define LC_SEGMENT (0x1) /* segment load command */
75 #define LC_SYMTAB (0x2) /* symbol table load command */
77 #define VM_PROT_NONE (0x00)
78 #define VM_PROT_READ (0x01)
79 #define VM_PROT_WRITE (0x02)
80 #define VM_PROT_EXECUTE (0x04)
82 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
83 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
85 struct section {
86 /* nasm internal data */
87 struct section *next;
88 struct SAA *data;
89 int32_t index;
90 struct reloc *relocs;
91 int align;
93 /* data that goes into the file */
94 char sectname[16]; /* what this section is called */
95 char segname[16]; /* segment this section will be in */
96 uint32_t addr; /* in-memory address (subject to alignment) */
97 uint32_t size; /* in-memory and -file size */
98 uint32_t nreloc; /* relocation entry count */
99 uint32_t flags; /* type and attributes (masked) */
102 #define SECTION_TYPE 0x000000ff /* section type mask */
104 #define S_REGULAR (0x0) /* standard section */
105 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
107 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
108 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
109 machine instructions */
110 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
111 relocation entries */
112 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
113 relocation entries */
116 static struct sectmap {
117 const char *nasmsect;
118 const char *segname;
119 const char *sectname;
120 const int32_t flags;
121 } sectmap[] = {
122 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
123 {".data", "__DATA", "__data", S_REGULAR},
124 {".rodata", "__DATA", "__const", S_REGULAR},
125 {".bss", "__DATA", "__bss", S_ZEROFILL},
126 {NULL, NULL, NULL, 0}
129 struct reloc {
130 /* nasm internal data */
131 struct reloc *next;
133 /* data that goes into the file */
134 int32_t addr; /* op's offset in section */
135 unsigned int snum:24, /* contains symbol index if
136 ** ext otherwise in-file
137 ** section number */
138 pcrel:1, /* relative relocation */
139 length:2, /* 0=byte, 1=word, 2=int32_t */
140 ext:1, /* external symbol referenced */
141 type:4; /* reloc type, 0 for us */
144 #define R_ABS 0 /* absolute relocation */
145 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
146 ** highest bit == 1 */
148 struct symbol {
149 /* nasm internal data */
150 struct symbol *next; /* next symbol in the list */
151 char *name; /* name of this symbol */
152 int32_t initial_snum; /* symbol number used above in
153 reloc */
154 int32_t snum; /* true snum for reloc */
156 /* data that goes into the file */
157 int32_t strx; /* string table index */
158 uint8_t type; /* symbol type */
159 uint8_t sect; /* NO_SECT or section number */
160 int16_t desc; /* for stab debugging, 0 for us */
161 uint32_t value; /* offset of symbol in section */
164 /* symbol type bits */
165 #define N_EXT 0x01 /* global or external symbol */
167 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
168 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
169 #define N_SECT 0xe /* defined symbol, n_sect holds
170 ** section number */
172 #define N_TYPE 0x0e /* type bit mask */
174 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
176 /* special section number values */
177 #define NO_SECT 0 /* no section, invalid */
178 #define MAX_SECT 255 /* maximum number of sections */
180 static struct section *sects, **sectstail;
181 static struct symbol *syms, **symstail;
182 static uint32_t nsyms;
184 /* These variables are set by macho_layout_symbols() to organize
185 the symbol table and string table in order the dynamic linker
186 expects. They are then used in macho_write() to put out the
187 symbols and strings in that order.
189 The order of the symbol table is:
190 local symbols
191 defined external symbols (sorted by name)
192 undefined external symbols (sorted by name)
194 The order of the string table is:
195 strings for external symbols
196 strings for local symbols
198 static uint32_t ilocalsym = 0;
199 static uint32_t iextdefsym = 0;
200 static uint32_t iundefsym = 0;
201 static uint32_t nlocalsym;
202 static uint32_t nextdefsym;
203 static uint32_t nundefsym;
204 static struct symbol **extdefsyms = NULL;
205 static struct symbol **undefsyms = NULL;
207 static struct RAA *extsyms;
208 static struct SAA *strs;
209 static uint32_t strslen;
211 extern struct ofmt of_macho;
213 /* Global file information. This should be cleaned up into either
214 a structure or as function arguments. */
215 uint32_t head_ncmds = 0;
216 uint32_t head_sizeofcmds = 0;
217 uint32_t seg_filesize = 0;
218 uint32_t seg_vmsize = 0;
219 uint32_t seg_nsects = 0;
220 uint32_t rel_padcnt = 0;
223 #define xstrncpy(xdst, xsrc) \
224 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
225 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
226 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
228 #define alignint32_t(x) \
229 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
231 static void debug_reloc (struct reloc *);
232 static void debug_section_relocs (struct section *) _unused;
234 static int exact_log2 (uint32_t align)
236 if (align == 0) {
237 return 0;
238 } else if (align & (align-1)) {
239 return -1; /* Not a power of 2 */
240 } else {
241 #ifdef HAVE_GNUC_4
242 return __builtin_ctzl (align);
243 #else
244 uint32_t result = 0;
246 /* We know exactly one bit is set at this point. */
247 if (align & 0xffff0000)
248 result |= 16;
249 if (align & 0xff00ff00)
250 result |= 8;
251 if (align & 0xf0f0f0f0)
252 result |= 4;
253 if (align & 0xcccccccc)
254 result |= 2;
255 if (align & 0xaaaaaaaa)
256 result |= 1;
258 return result;
259 #endif
263 static struct section *get_section_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 break;
272 return s;
275 static struct section *get_section_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 break;
283 return s;
286 static int32_t get_section_index_by_name(const char *segname,
287 const char *sectname)
289 struct section *s;
291 for (s = sects; s != NULL; s = s->next)
292 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
293 return s->index;
295 return -1;
298 static char *get_section_name_by_index(const int32_t index)
300 struct section *s;
302 for (s = sects; s != NULL; s = s->next)
303 if (index == s->index)
304 return s->sectname;
306 return NULL;
309 static uint8_t get_section_fileindex_by_index(const int32_t index)
311 struct section *s;
312 uint8_t i = 1;
314 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
315 if (index == s->index)
316 return i;
318 if (i == MAX_SECT)
319 nasm_error(ERR_WARNING,
320 "too many sections (>255) - clipped by fileindex");
322 return NO_SECT;
325 static void macho_init(void)
327 char zero = 0;
329 sects = NULL;
330 sectstail = &sects;
332 syms = NULL;
333 symstail = &syms;
334 nsyms = 0;
335 nlocalsym = 0;
336 nextdefsym = 0;
337 nundefsym = 0;
339 extsyms = raa_init();
340 strs = saa_init(1L);
342 /* string table starts with a zero byte - don't ask why */
343 saa_wbytes(strs, &zero, sizeof(char));
344 strslen = 1;
347 static void sect_write(struct section *sect,
348 const uint8_t *data, uint32_t len)
350 saa_wbytes(sect->data, data, len);
351 sect->size += len;
354 static void add_reloc(struct section *sect, int32_t section,
355 int pcrel, int bytes)
357 struct reloc *r;
358 int32_t fi;
360 /* NeXT as puts relocs in reversed order (address-wise) into the
361 ** files, so we do the same, doesn't seem to make much of a
362 ** difference either way */
363 r = nasm_malloc(sizeof(struct reloc));
364 r->next = sect->relocs;
365 sect->relocs = r;
367 /* the current end of the section will be the symbol's address for
368 ** now, might have to be fixed by macho_fixup_relocs() later on. make
369 ** sure we don't make the symbol scattered by setting the highest
370 ** bit by accident */
371 r->addr = sect->size & ~R_SCATTERED;
372 r->ext = 0;
373 r->pcrel = pcrel;
375 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
376 r->length = bytes >> 1;
378 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
379 r->type = 0;
381 if (section == NO_SEG) {
382 /* absolute local symbol if no section index given */
383 r->snum = R_ABS;
384 } else {
385 fi = get_section_fileindex_by_index(section);
387 if (fi == NO_SECT) {
388 /* external symbol if no section with that index known,
389 ** symbol number was saved in macho_symdef() */
390 r->snum = raa_read(extsyms, section);
391 r->ext = 1;
392 } else {
393 /* local symbol in section fi */
394 r->snum = fi;
398 ++sect->nreloc;
401 static void macho_output(int32_t secto, const void *data,
402 enum out_type type, uint64_t size,
403 int32_t section, int32_t wrt)
405 struct section *s, *sbss;
406 int32_t addr;
407 uint8_t mydata[4], *p;
409 if (wrt != NO_SEG) {
410 wrt = NO_SEG;
411 nasm_error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
412 /* continue to do _something_ */
415 if (secto == NO_SEG) {
416 if (type != OUT_RESERVE)
417 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
418 "[ABSOLUTE] space");
420 return;
423 s = get_section_by_index(secto);
425 if (s == NULL) {
426 nasm_error(ERR_WARNING, "attempt to assemble code in"
427 " section %d: defaulting to `.text'", secto);
428 s = get_section_by_name("__TEXT", "__text");
430 /* should never happen */
431 if (s == NULL)
432 nasm_error(ERR_PANIC, "text section not found");
435 sbss = get_section_by_name("__DATA", "__bss");
437 if (s == sbss && type != OUT_RESERVE) {
438 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
439 " BSS section: ignored");
440 s->size += realsize(type, size);
441 return;
444 switch (type) {
445 case OUT_RESERVE:
446 if (s != sbss) {
447 nasm_error(ERR_WARNING, "uninitialized space declared in"
448 " %s section: zeroing",
449 get_section_name_by_index(secto));
451 sect_write(s, NULL, size);
452 } else
453 s->size += size;
455 break;
457 case OUT_RAWDATA:
458 if (section != NO_SEG)
459 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
461 sect_write(s, data, size);
462 break;
464 case OUT_ADDRESS:
465 addr = *(int64_t *)data;
467 if (section != NO_SEG) {
468 if (section % 2) {
469 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
470 " section base references");
471 } else
472 add_reloc(s, section, 0, size);
475 p = mydata;
476 WRITEADDR(p, addr, size);
477 sect_write(s, mydata, size);
478 break;
480 case OUT_REL2ADR:
481 if (section == secto)
482 nasm_error(ERR_PANIC, "intra-section OUT_REL2ADR");
484 if (section != NO_SEG && section % 2) {
485 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
486 " section base references");
487 } else
488 add_reloc(s, section, 1, 2);
490 p = mydata;
491 WRITESHORT(p, *(int32_t *)data - (size + s->size));
492 sect_write(s, mydata, 2L);
493 break;
495 case OUT_REL4ADR:
496 if (section == secto)
497 nasm_error(ERR_PANIC, "intra-section OUT_REL4ADR");
499 if (section != NO_SEG && section % 2) {
500 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
501 " section base references");
502 } else
503 add_reloc(s, section, 1, 4);
505 p = mydata;
506 WRITELONG(p, *(int32_t *)data - (size + s->size));
507 sect_write(s, mydata, 4L);
508 break;
510 default:
511 nasm_error(ERR_PANIC, "unknown output type?");
512 break;
516 static int32_t macho_section(char *name, int pass, int *bits)
518 int32_t index, originalIndex;
519 char *sectionAttributes;
520 struct sectmap *sm;
521 struct section *s;
523 (void)pass;
525 /* Default to 32 bits. */
526 if (!name) {
527 *bits = 32;
528 name = ".text";
529 sectionAttributes = NULL;
530 } else {
531 sectionAttributes = name;
532 name = nasm_strsep(&sectionAttributes, " \t");
535 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
536 /* make lookup into section name translation table */
537 if (!strcmp(name, sm->nasmsect)) {
538 char *currentAttribute;
540 /* try to find section with that name */
541 originalIndex = index = get_section_index_by_name(sm->segname,
542 sm->sectname);
544 /* create it if it doesn't exist yet */
545 if (index == -1) {
546 s = *sectstail = nasm_malloc(sizeof(struct section));
547 s->next = NULL;
548 sectstail = &s->next;
550 s->data = saa_init(1L);
551 s->index = seg_alloc();
552 s->relocs = NULL;
553 s->align = -1;
555 xstrncpy(s->segname, sm->segname);
556 xstrncpy(s->sectname, sm->sectname);
557 s->size = 0;
558 s->nreloc = 0;
559 s->flags = sm->flags;
561 index = s->index;
562 } else {
563 s = get_section_by_index(index);
566 while ((NULL != sectionAttributes)
567 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
568 if (0 != *currentAttribute) {
569 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
570 char *end;
571 int newAlignment, value;
573 value = strtoul(currentAttribute + 6, (char**)&end, 0);
574 newAlignment = exact_log2(value);
576 if (0 != *end) {
577 nasm_error(ERR_PANIC,
578 "unknown or missing alignment value \"%s\" "
579 "specified for section \"%s\"",
580 currentAttribute + 6,
581 name);
582 return NO_SEG;
583 } else if (0 > newAlignment) {
584 nasm_error(ERR_PANIC,
585 "alignment of %d (for section \"%s\") is not "
586 "a power of two",
587 value,
588 name);
589 return NO_SEG;
592 if ((-1 != originalIndex)
593 && (s->align != newAlignment)
594 && (s->align != -1)) {
595 nasm_error(ERR_PANIC,
596 "section \"%s\" has already been specified "
597 "with alignment %d, conflicts with new "
598 "alignment of %d",
599 name,
600 (1 << s->align),
601 value);
602 return NO_SEG;
605 s->align = newAlignment;
606 } else if (!nasm_stricmp("data", currentAttribute)) {
607 /* Do nothing; 'data' is implicit */
608 } else {
609 nasm_error(ERR_PANIC,
610 "unknown section attribute %s for section %s",
611 currentAttribute,
612 name);
613 return NO_SEG;
618 return index;
622 nasm_error(ERR_PANIC, "invalid section name %s", name);
623 return NO_SEG;
626 static void macho_symdef(char *name, int32_t section, int64_t offset,
627 int is_global, char *special)
629 struct symbol *sym;
631 if (special) {
632 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
633 "not support any special symbol types");
634 return;
637 if (is_global == 3) {
638 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
639 "(yet) support forward reference fixups.");
640 return;
643 sym = *symstail = nasm_malloc(sizeof(struct symbol));
644 sym->next = NULL;
645 symstail = &sym->next;
647 sym->name = name;
648 sym->strx = strslen;
649 sym->type = 0;
650 sym->desc = 0;
651 sym->value = offset;
652 sym->initial_snum = -1;
654 /* external and common symbols get N_EXT */
655 if (is_global != 0)
656 sym->type |= N_EXT;
658 if (section == NO_SEG) {
659 /* symbols in no section get absolute */
660 sym->type |= N_ABS;
661 sym->sect = NO_SECT;
662 } else {
663 sym->type |= N_SECT;
665 /* get the in-file index of the section the symbol was defined in */
666 sym->sect = get_section_fileindex_by_index(section);
668 if (sym->sect == NO_SECT) {
669 /* remember symbol number of references to external
670 ** symbols, this works because every external symbol gets
671 ** its own section number allocated internally by nasm and
672 ** can so be used as a key */
673 extsyms = raa_write(extsyms, section, nsyms);
674 sym->initial_snum = nsyms;
676 switch (is_global) {
677 case 1:
678 case 2:
679 /* there isn't actually a difference between global
680 ** and common symbols, both even have their size in
681 ** sym->value */
682 sym->type = N_EXT;
683 break;
685 default:
686 /* give an error on unfound section if it's not an
687 ** external or common symbol (assemble_file() does a
688 ** seg_alloc() on every call for them) */
689 nasm_error(ERR_PANIC, "in-file index for section %d not found",
690 section);
695 ++nsyms;
698 static int32_t macho_segbase(int32_t section)
700 return section;
703 static void macho_filename(char *inname, char *outname)
705 standard_extension(inname, outname, ".o");
708 extern macros_t macho_stdmac[];
710 /* Comparison function for qsort symbol layout. */
711 static int layout_compare (const struct symbol **s1,
712 const struct symbol **s2)
714 return (strcmp ((*s1)->name, (*s2)->name));
717 /* The native assembler does a few things in a similar function
719 * Remove temporary labels
720 * Sort symbols according to local, external, undefined (by name)
721 * Order the string table
723 We do not remove temporary labels right now.
725 numsyms is the total number of symbols we have. strtabsize is the
726 number entries in the string table. */
728 static void macho_layout_symbols (uint32_t *numsyms,
729 uint32_t *strtabsize)
731 struct symbol *sym, **symp;
732 uint32_t i,j;
734 *numsyms = 0;
735 *strtabsize = sizeof (char);
737 symp = &syms;
739 while ((sym = *symp)) {
740 /* Undefined symbols are now external. */
741 if (sym->type == N_UNDF)
742 sym->type |= N_EXT;
744 if ((sym->type & N_EXT) == 0) {
745 sym->snum = *numsyms;
746 *numsyms = *numsyms + 1;
747 nlocalsym++;
749 else {
750 if ((sym->type & N_TYPE) != N_UNDF)
751 nextdefsym++;
752 else
753 nundefsym++;
755 /* If we handle debug info we'll want
756 to check for it here instead of just
757 adding the symbol to the string table. */
758 sym->strx = *strtabsize;
759 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
760 *strtabsize += strlen(sym->name) + 1;
762 symp = &(sym->next);
765 /* Next, sort the symbols. Most of this code is a direct translation from
766 the Apple cctools symbol layout. We need to keep compatibility with that. */
767 /* Set the indexes for symbol groups into the symbol table */
768 ilocalsym = 0;
769 iextdefsym = nlocalsym;
770 iundefsym = nlocalsym + nextdefsym;
772 /* allocate arrays for sorting externals by name */
773 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
774 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
776 i = 0;
777 j = 0;
779 symp = &syms;
781 while ((sym = *symp)) {
783 if((sym->type & N_EXT) == 0) {
784 sym->strx = *strtabsize;
785 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
786 *strtabsize += strlen(sym->name) + 1;
788 else {
789 if((sym->type & N_TYPE) != N_UNDF)
790 extdefsyms[i++] = sym;
791 else
792 undefsyms[j++] = sym;
794 symp = &(sym->next);
797 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
798 (int (*)(const void *, const void *))layout_compare);
799 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
800 (int (*)(const void *, const void *))layout_compare);
802 for(i = 0; i < nextdefsym; i++) {
803 extdefsyms[i]->snum = *numsyms;
804 *numsyms += 1;
806 for(j = 0; j < nundefsym; j++) {
807 undefsyms[j]->snum = *numsyms;
808 *numsyms += 1;
812 /* Calculate some values we'll need for writing later. */
814 static void macho_calculate_sizes (void)
816 struct section *s;
818 /* count sections and calculate in-memory and in-file offsets */
819 for (s = sects; s != NULL; s = s->next) {
820 uint32_t pad = 0;
822 /* zerofill sections aren't actually written to the file */
823 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
824 seg_filesize += s->size;
826 /* recalculate segment address based on alignment and vm size */
827 s->addr = seg_vmsize;
828 /* we need section alignment to calculate final section address */
829 if (s->align == -1)
830 s->align = DEFAULT_SECTION_ALIGNMENT;
831 if(s->align) {
832 uint32_t newaddr = ALIGN(s->addr, 1 << s->align);
833 pad = newaddr - s->addr;
834 s->addr = newaddr;
837 seg_vmsize += s->size + pad;
838 ++seg_nsects;
841 /* calculate size of all headers, load commands and sections to
842 ** get a pointer to the start of all the raw data */
843 if (seg_nsects > 0) {
844 ++head_ncmds;
845 head_sizeofcmds +=
846 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
849 if (nsyms > 0) {
850 ++head_ncmds;
851 head_sizeofcmds += MACHO_SYMCMD_SIZE;
855 /* Write out the header information for the file. */
857 static void macho_write_header (void)
859 fwriteint32_t(MH_MAGIC, ofile); /* magic */
860 fwriteint32_t(CPU_TYPE_I386, ofile); /* CPU type */
861 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
862 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
863 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
864 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
865 fwriteint32_t(0, ofile); /* no flags */
868 /* Write out the segment load command at offset. */
870 static uint32_t macho_write_segment (uint32_t offset)
872 uint32_t rel_base = alignint32_t (offset + seg_filesize);
873 uint32_t s_reloff = 0;
874 struct section *s;
876 fwriteint32_t(LC_SEGMENT, ofile); /* cmd == LC_SEGMENT */
878 /* size of load command including section load commands */
879 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
880 MACHO_SECTCMD_SIZE, ofile);
882 /* in an MH_OBJECT file all sections are in one unnamed (name
883 ** all zeros) segment */
884 fwritezero(16, ofile);
885 fwriteint32_t(0, ofile); /* in-memory offset */
886 fwriteint32_t(seg_vmsize, ofile); /* in-memory size */
887 fwriteint32_t(offset, ofile); /* in-file offset to data */
888 fwriteint32_t(seg_filesize, ofile); /* in-file size */
889 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
890 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
891 fwriteint32_t(seg_nsects, ofile); /* number of sections */
892 fwriteint32_t(0, ofile); /* no flags */
894 /* emit section headers */
895 for (s = sects; s != NULL; s = s->next) {
896 fwrite(s->sectname, sizeof(s->sectname), 1, ofile);
897 fwrite(s->segname, sizeof(s->segname), 1, ofile);
898 fwriteint32_t(s->addr, ofile);
899 fwriteint32_t(s->size, ofile);
901 /* dummy data for zerofill sections or proper values */
902 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
903 fwriteint32_t(offset, ofile);
904 /* Write out section alignment, as a power of two.
905 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
906 if (s->align == -1)
907 s->align = DEFAULT_SECTION_ALIGNMENT;
908 fwriteint32_t(s->align, ofile);
909 /* To be compatible with cctools as we emit
910 a zero reloff if we have no relocations. */
911 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
912 fwriteint32_t(s->nreloc, ofile);
914 offset += s->size;
915 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
916 } else {
917 fwriteint32_t(0, ofile);
918 fwriteint32_t(0, ofile);
919 fwriteint32_t(0, ofile);
920 fwriteint32_t(0, ofile);
923 fwriteint32_t(s->flags, ofile); /* flags */
924 fwriteint32_t(0, ofile); /* reserved */
925 fwriteint32_t(0, ofile); /* reserved */
928 rel_padcnt = rel_base - offset;
929 offset = rel_base + s_reloff;
931 return offset;
934 /* For a given chain of relocs r, write out the entire relocation
935 chain to the object file. */
937 static void macho_write_relocs (struct reloc *r)
939 while (r) {
940 uint32_t word2;
942 fwriteint32_t(r->addr, ofile); /* reloc offset */
944 word2 = r->snum;
945 word2 |= r->pcrel << 24;
946 word2 |= r->length << 25;
947 word2 |= r->ext << 27;
948 word2 |= r->type << 28;
949 fwriteint32_t(word2, ofile); /* reloc data */
951 r = r->next;
955 /* Write out the section data. */
956 static void macho_write_section (void)
958 struct section *s, *s2;
959 struct reloc *r;
960 uint8_t fi, *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 for (s2 = sects, fi = 1;
996 s2 != NULL; s2 = s2->next, fi++) {
997 if (fi == r->snum) {
998 l += s2->addr;
999 break;
1004 /* write new offset back */
1005 if (r->length == 2)
1006 WRITELONG(q, l);
1007 else if (r->length == 1)
1008 WRITESHORT(q, l);
1009 else
1010 *q++ = l & 0xFF;
1012 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1015 /* dump the section data to file */
1016 saa_fpwrite(s->data, ofile);
1019 /* pad last section up to reloc entries on int32_t boundary */
1020 fwritezero(rel_padcnt, ofile);
1022 /* emit relocation entries */
1023 for (s = sects; s != NULL; s = s->next)
1024 macho_write_relocs (s->relocs);
1027 /* Write out the symbol table. We should already have sorted this
1028 before now. */
1029 static void macho_write_symtab (void)
1031 struct symbol *sym;
1032 struct section *s;
1033 int32_t fi;
1034 uint32_t i;
1036 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1038 for (sym = syms; sym != NULL; sym = sym->next) {
1039 if ((sym->type & N_EXT) == 0) {
1040 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1041 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1042 fwrite(&sym->sect, 1, 1, ofile); /* section */
1043 fwriteint16_t(sym->desc, ofile); /* description */
1045 /* Fix up the symbol value now that we know the final section
1046 sizes. */
1047 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1048 for (s = sects, fi = 1; s != NULL; s = s->next, fi++) {
1049 if (fi == sym->sect) {
1050 sym->value += s->addr;
1051 break;
1056 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1060 for (i = 0; i < nextdefsym; i++) {
1061 sym = extdefsyms[i];
1062 fwriteint32_t(sym->strx, ofile);
1063 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1064 fwrite(&sym->sect, 1, 1, ofile); /* section */
1065 fwriteint16_t(sym->desc, ofile); /* 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, ofile); /* value (i.e. offset) */
1078 for (i = 0; i < nundefsym; i++) {
1079 sym = undefsyms[i];
1080 fwriteint32_t(sym->strx, ofile);
1081 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1082 fwrite(&sym->sect, 1, 1, ofile); /* section */
1083 fwriteint16_t(sym->desc, ofile); /* 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, ofile); /* 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 nasm_error(ERR_WARNING, "no sections?");
1208 if (nsyms > 0) {
1209 /* write out symbol command */
1210 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1211 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1212 fwriteint32_t(offset, ofile); /* symbol table offset */
1213 fwriteint32_t(nsyms, ofile); /* number of symbol
1214 ** table entries */
1216 offset += nsyms * MACHO_NLIST_SIZE;
1217 fwriteint32_t(offset, ofile); /* string table offset */
1218 fwriteint32_t(strslen, ofile); /* 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, ofile);
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 /* free up everything */
1259 while (sects->next) {
1260 s = sects;
1261 sects = sects->next;
1263 saa_free(s->data);
1264 while (s->relocs != NULL) {
1265 r = s->relocs;
1266 s->relocs = s->relocs->next;
1267 nasm_free(r);
1270 nasm_free(s);
1273 saa_free(strs);
1274 raa_free(extsyms);
1276 if (syms) {
1277 while (syms->next) {
1278 sym = syms;
1279 syms = syms->next;
1281 nasm_free (sym);
1286 /* Debugging routines. */
1287 static void debug_reloc (struct reloc *r)
1289 fprintf (stdout, "reloc:\n");
1290 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1291 fprintf (stdout, "\tsnum: %d\n", r->snum);
1292 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1293 fprintf (stdout, "\tlength: %d\n", r->length);
1294 fprintf (stdout, "\text: %d\n", r->ext);
1295 fprintf (stdout, "\ttype: %d\n", r->type);
1298 static void debug_section_relocs (struct section *s)
1300 struct reloc *r = s->relocs;
1302 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1304 while (r != NULL) {
1305 debug_reloc (r);
1306 r = r->next;
1310 struct ofmt of_macho32 = {
1311 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1312 "macho32",
1314 null_debug_arr,
1315 &null_debug_form,
1316 macho_stdmac,
1317 macho_init,
1318 null_setinfo,
1319 macho_output,
1320 macho_symdef,
1321 macho_section,
1322 macho_segbase,
1323 null_directive,
1324 macho_filename,
1325 macho_cleanup
1328 struct ofmt of_macho = {
1329 "MACHO (short name for MACHO32)",
1330 "macho",
1332 null_debug_arr,
1333 &null_debug_form,
1334 macho_stdmac,
1335 macho_init,
1336 null_setinfo,
1337 macho_output,
1338 macho_symdef,
1339 macho_section,
1340 macho_segbase,
1341 null_directive,
1342 macho_filename,
1343 macho_cleanup
1346 #endif
1349 * Local Variables:
1350 * mode:c
1351 * c-basic-offset:4
1352 * End:
1354 * end of file */