outmacho32: replace error() -> nasm_error()
[nasm.git] / output / outmacho32.c
blob1a211a6e3f22e9a4e072c608c611ab39df6b685b
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 align(x, y) \
229 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
231 #define alignint32_t(x) \
232 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
234 static void debug_reloc (struct reloc *);
235 static void debug_section_relocs (struct section *) _unused;
237 static int exact_log2 (uint32_t align)
239 if (align == 0) {
240 return 0;
241 } else if (align & (align-1)) {
242 return -1; /* Not a power of 2 */
243 } else {
244 #ifdef HAVE_GNUC_4
245 return __builtin_ctzl (align);
246 #else
247 uint32_t result = 0;
249 /* We know exactly one bit is set at this point. */
250 if (align & 0xffff0000)
251 result |= 16;
252 if (align & 0xff00ff00)
253 result |= 8;
254 if (align & 0xf0f0f0f0)
255 result |= 4;
256 if (align & 0xcccccccc)
257 result |= 2;
258 if (align & 0xaaaaaaaa)
259 result |= 1;
261 return result;
262 #endif
266 static struct section *get_section_by_name(const char *segname,
267 const char *sectname)
269 struct section *s;
271 for (s = sects; s != NULL; s = s->next)
272 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
273 break;
275 return s;
278 static struct section *get_section_by_index(const int32_t index)
280 struct section *s;
282 for (s = sects; s != NULL; s = s->next)
283 if (index == s->index)
284 break;
286 return s;
289 static int32_t get_section_index_by_name(const char *segname,
290 const char *sectname)
292 struct section *s;
294 for (s = sects; s != NULL; s = s->next)
295 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
296 return s->index;
298 return -1;
301 static char *get_section_name_by_index(const int32_t index)
303 struct section *s;
305 for (s = sects; s != NULL; s = s->next)
306 if (index == s->index)
307 return s->sectname;
309 return NULL;
312 static uint8_t get_section_fileindex_by_index(const int32_t index)
314 struct section *s;
315 uint8_t i = 1;
317 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
318 if (index == s->index)
319 return i;
321 if (i == MAX_SECT)
322 nasm_error(ERR_WARNING,
323 "too many sections (>255) - clipped by fileindex");
325 return NO_SECT;
328 static void macho_init(void)
330 char zero = 0;
332 sects = NULL;
333 sectstail = &sects;
335 syms = NULL;
336 symstail = &syms;
337 nsyms = 0;
338 nlocalsym = 0;
339 nextdefsym = 0;
340 nundefsym = 0;
342 extsyms = raa_init();
343 strs = saa_init(1L);
345 /* string table starts with a zero byte - don't ask why */
346 saa_wbytes(strs, &zero, sizeof(char));
347 strslen = 1;
350 static void sect_write(struct section *sect,
351 const uint8_t *data, uint32_t len)
353 saa_wbytes(sect->data, data, len);
354 sect->size += len;
357 static void add_reloc(struct section *sect, int32_t section,
358 int pcrel, int bytes)
360 struct reloc *r;
361 int32_t fi;
363 /* NeXT as puts relocs in reversed order (address-wise) into the
364 ** files, so we do the same, doesn't seem to make much of a
365 ** difference either way */
366 r = nasm_malloc(sizeof(struct reloc));
367 r->next = sect->relocs;
368 sect->relocs = r;
370 /* the current end of the section will be the symbol's address for
371 ** now, might have to be fixed by macho_fixup_relocs() later on. make
372 ** sure we don't make the symbol scattered by setting the highest
373 ** bit by accident */
374 r->addr = sect->size & ~R_SCATTERED;
375 r->ext = 0;
376 r->pcrel = pcrel;
378 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
379 r->length = bytes >> 1;
381 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
382 r->type = 0;
384 if (section == NO_SEG) {
385 /* absolute local symbol if no section index given */
386 r->snum = R_ABS;
387 } else {
388 fi = get_section_fileindex_by_index(section);
390 if (fi == NO_SECT) {
391 /* external symbol if no section with that index known,
392 ** symbol number was saved in macho_symdef() */
393 r->snum = raa_read(extsyms, section);
394 r->ext = 1;
395 } else {
396 /* local symbol in section fi */
397 r->snum = fi;
401 ++sect->nreloc;
404 static void macho_output(int32_t secto, const void *data,
405 enum out_type type, uint64_t size,
406 int32_t section, int32_t wrt)
408 struct section *s, *sbss;
409 int32_t addr;
410 uint8_t mydata[4], *p;
412 if (wrt != NO_SEG) {
413 wrt = NO_SEG;
414 nasm_error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
415 /* continue to do _something_ */
418 if (secto == NO_SEG) {
419 if (type != OUT_RESERVE)
420 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
421 "[ABSOLUTE] space");
423 return;
426 s = get_section_by_index(secto);
428 if (s == NULL) {
429 nasm_error(ERR_WARNING, "attempt to assemble code in"
430 " section %d: defaulting to `.text'", secto);
431 s = get_section_by_name("__TEXT", "__text");
433 /* should never happen */
434 if (s == NULL)
435 nasm_error(ERR_PANIC, "text section not found");
438 sbss = get_section_by_name("__DATA", "__bss");
440 if (s == sbss && type != OUT_RESERVE) {
441 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
442 " BSS section: ignored");
443 s->size += realsize(type, size);
444 return;
447 switch (type) {
448 case OUT_RESERVE:
449 if (s != sbss) {
450 nasm_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 nasm_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 nasm_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 nasm_error(ERR_PANIC, "intra-section OUT_REL2ADR");
487 if (section != NO_SEG && section % 2) {
488 nasm_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 nasm_error(ERR_PANIC, "intra-section OUT_REL4ADR");
502 if (section != NO_SEG && section % 2) {
503 nasm_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 nasm_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 nasm_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 nasm_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 nasm_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 nasm_error(ERR_PANIC,
613 "unknown section attribute %s for section %s",
614 currentAttribute,
615 name);
616 return NO_SEG;
621 return index;
625 nasm_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 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
636 "not support any special symbol types");
637 return;
640 if (is_global == 3) {
641 nasm_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 nasm_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 void macho_filename(char *inname, char *outname)
708 standard_extension(inname, outname, ".o");
711 extern macros_t macho_stdmac[];
713 /* Comparison function for qsort symbol layout. */
714 static int layout_compare (const struct symbol **s1,
715 const struct symbol **s2)
717 return (strcmp ((*s1)->name, (*s2)->name));
720 /* The native assembler does a few things in a similar function
722 * Remove temporary labels
723 * Sort symbols according to local, external, undefined (by name)
724 * Order the string table
726 We do not remove temporary labels right now.
728 numsyms is the total number of symbols we have. strtabsize is the
729 number entries in the string table. */
731 static void macho_layout_symbols (uint32_t *numsyms,
732 uint32_t *strtabsize)
734 struct symbol *sym, **symp;
735 uint32_t i,j;
737 *numsyms = 0;
738 *strtabsize = sizeof (char);
740 symp = &syms;
742 while ((sym = *symp)) {
743 /* Undefined symbols are now external. */
744 if (sym->type == N_UNDF)
745 sym->type |= N_EXT;
747 if ((sym->type & N_EXT) == 0) {
748 sym->snum = *numsyms;
749 *numsyms = *numsyms + 1;
750 nlocalsym++;
752 else {
753 if ((sym->type & N_TYPE) != N_UNDF)
754 nextdefsym++;
755 else
756 nundefsym++;
758 /* If we handle debug info we'll want
759 to check for it here instead of just
760 adding the symbol to the string table. */
761 sym->strx = *strtabsize;
762 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
763 *strtabsize += strlen(sym->name) + 1;
765 symp = &(sym->next);
768 /* Next, sort the symbols. Most of this code is a direct translation from
769 the Apple cctools symbol layout. We need to keep compatibility with that. */
770 /* Set the indexes for symbol groups into the symbol table */
771 ilocalsym = 0;
772 iextdefsym = nlocalsym;
773 iundefsym = nlocalsym + nextdefsym;
775 /* allocate arrays for sorting externals by name */
776 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
777 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
779 i = 0;
780 j = 0;
782 symp = &syms;
784 while ((sym = *symp)) {
786 if((sym->type & N_EXT) == 0) {
787 sym->strx = *strtabsize;
788 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
789 *strtabsize += strlen(sym->name) + 1;
791 else {
792 if((sym->type & N_TYPE) != N_UNDF)
793 extdefsyms[i++] = sym;
794 else
795 undefsyms[j++] = sym;
797 symp = &(sym->next);
800 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
801 (int (*)(const void *, const void *))layout_compare);
802 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
803 (int (*)(const void *, const void *))layout_compare);
805 for(i = 0; i < nextdefsym; i++) {
806 extdefsyms[i]->snum = *numsyms;
807 *numsyms += 1;
809 for(j = 0; j < nundefsym; j++) {
810 undefsyms[j]->snum = *numsyms;
811 *numsyms += 1;
815 /* Calculate some values we'll need for writing later. */
817 static void macho_calculate_sizes (void)
819 struct section *s;
821 /* count sections and calculate in-memory and in-file offsets */
822 for (s = sects; s != NULL; s = s->next) {
823 uint32_t pad = 0;
825 /* zerofill sections aren't actually written to the file */
826 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
827 seg_filesize += s->size;
829 /* recalculate segment address based on alignment and vm size */
830 s->addr = seg_vmsize;
831 /* we need section alignment to calculate final section address */
832 if (s->align == -1)
833 s->align = DEFAULT_SECTION_ALIGNMENT;
834 if(s->align) {
835 uint32_t newaddr = align(s->addr, 1 << s->align);
836 pad = newaddr - s->addr;
837 s->addr = newaddr;
840 seg_vmsize += s->size + pad;
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, ofile); /* magic */
863 fwriteint32_t(CPU_TYPE_I386, ofile); /* CPU type */
864 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
865 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
866 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
867 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
868 fwriteint32_t(0, ofile); /* no flags */
871 /* Write out the segment load command at offset. */
873 static uint32_t macho_write_segment (uint32_t offset)
875 uint32_t rel_base = alignint32_t (offset + seg_filesize);
876 uint32_t s_reloff = 0;
877 struct section *s;
879 fwriteint32_t(LC_SEGMENT, ofile); /* cmd == LC_SEGMENT */
881 /* size of load command including section load commands */
882 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
883 MACHO_SECTCMD_SIZE, ofile);
885 /* in an MH_OBJECT file all sections are in one unnamed (name
886 ** all zeros) segment */
887 fwritezero(16, ofile);
888 fwriteint32_t(0, ofile); /* in-memory offset */
889 fwriteint32_t(seg_vmsize, ofile); /* in-memory size */
890 fwriteint32_t(offset, ofile); /* in-file offset to data */
891 fwriteint32_t(seg_filesize, ofile); /* in-file size */
892 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
893 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
894 fwriteint32_t(seg_nsects, ofile); /* number of sections */
895 fwriteint32_t(0, ofile); /* no flags */
897 /* emit section headers */
898 for (s = sects; s != NULL; s = s->next) {
899 fwrite(s->sectname, sizeof(s->sectname), 1, ofile);
900 fwrite(s->segname, sizeof(s->segname), 1, ofile);
901 fwriteint32_t(s->addr, ofile);
902 fwriteint32_t(s->size, ofile);
904 /* dummy data for zerofill sections or proper values */
905 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
906 fwriteint32_t(offset, ofile);
907 /* Write out section alignment, as a power of two.
908 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
909 if (s->align == -1)
910 s->align = DEFAULT_SECTION_ALIGNMENT;
911 fwriteint32_t(s->align, ofile);
912 /* To be compatible with cctools as we emit
913 a zero reloff if we have no relocations. */
914 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
915 fwriteint32_t(s->nreloc, ofile);
917 offset += s->size;
918 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
919 } else {
920 fwriteint32_t(0, ofile);
921 fwriteint32_t(0, ofile);
922 fwriteint32_t(0, ofile);
923 fwriteint32_t(0, ofile);
926 fwriteint32_t(s->flags, ofile); /* flags */
927 fwriteint32_t(0, ofile); /* reserved */
928 fwriteint32_t(0, ofile); /* reserved */
931 rel_padcnt = rel_base - offset;
932 offset = rel_base + s_reloff;
934 return offset;
937 /* For a given chain of relocs r, write out the entire relocation
938 chain to the object file. */
940 static void macho_write_relocs (struct reloc *r)
942 while (r) {
943 uint32_t word2;
945 fwriteint32_t(r->addr, ofile); /* reloc offset */
947 word2 = r->snum;
948 word2 |= r->pcrel << 24;
949 word2 |= r->length << 25;
950 word2 |= r->ext << 27;
951 word2 |= r->type << 28;
952 fwriteint32_t(word2, ofile); /* reloc data */
954 r = r->next;
958 /* Write out the section data. */
959 static void macho_write_section (void)
961 struct section *s, *s2;
962 struct reloc *r;
963 uint8_t fi, *p, *q, blk[4];
964 int32_t l;
966 for (s = sects; s != NULL; s = s->next) {
967 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
968 continue;
970 /* no padding needs to be done to the sections */
972 /* Like a.out Mach-O references things in the data or bss
973 * sections by addresses which are actually relative to the
974 * start of the _text_ section, in the _file_. See outaout.c
975 * for more information. */
976 saa_rewind(s->data);
977 for (r = s->relocs; r != NULL; r = r->next) {
978 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
979 p = q = blk;
980 l = *p++;
982 /* get offset based on relocation type */
983 if (r->length > 0) {
984 l += ((int32_t)*p++) << 8;
986 if (r->length == 2) {
987 l += ((int32_t)*p++) << 16;
988 l += ((int32_t)*p++) << 24;
992 /* If the relocation is internal add to the current section
993 offset. Otherwise the only value we need is the symbol
994 offset which we already have. The linker takes care
995 of the rest of the address. */
996 if (!r->ext) {
997 /* generate final address by section address and offset */
998 for (s2 = sects, fi = 1;
999 s2 != NULL; s2 = s2->next, fi++) {
1000 if (fi == r->snum) {
1001 l += s2->addr;
1002 break;
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, ofile);
1022 /* pad last section up to reloc entries on int32_t boundary */
1023 fwritezero(rel_padcnt, ofile);
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, ofile); /* string table entry number */
1044 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1045 fwrite(&sym->sect, 1, 1, ofile); /* section */
1046 fwriteint16_t(sym->desc, ofile); /* 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; s != NULL; s = s->next, fi++) {
1052 if (fi == sym->sect) {
1053 sym->value += s->addr;
1054 break;
1059 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1063 for (i = 0; i < nextdefsym; i++) {
1064 sym = extdefsyms[i];
1065 fwriteint32_t(sym->strx, ofile);
1066 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1067 fwrite(&sym->sect, 1, 1, ofile); /* section */
1068 fwriteint16_t(sym->desc, ofile); /* description */
1070 /* Fix up the symbol value now that we know the final section
1071 sizes. */
1072 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1073 for (s = sects, fi = 1;
1074 s != NULL && fi < sym->sect; s = s->next, ++fi)
1075 sym->value += s->size;
1078 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1081 for (i = 0; i < nundefsym; i++) {
1082 sym = undefsyms[i];
1083 fwriteint32_t(sym->strx, ofile);
1084 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1085 fwrite(&sym->sect, 1, 1, ofile); /* section */
1086 fwriteint16_t(sym->desc, ofile); /* description */
1088 /* Fix up the symbol value now that we know the final section
1089 sizes. */
1090 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1091 for (s = sects, fi = 1;
1092 s != NULL && fi < sym->sect; s = s->next, ++fi)
1093 sym->value += s->size;
1096 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1100 /* Fixup the snum in the relocation entries, we should be
1101 doing this only for externally undefined symbols. */
1102 static void macho_fixup_relocs (struct reloc *r)
1104 struct symbol *sym;
1105 uint32_t i;
1107 while (r != NULL) {
1108 if (r->ext) {
1109 for (i = 0; i < nundefsym; i++) {
1110 sym = undefsyms[i];
1111 if (sym->initial_snum == r->snum) {
1112 r->snum = sym->snum;
1113 break;
1117 r = r->next;
1121 /* Write out the object file. */
1123 static void macho_write (void)
1125 uint32_t offset = 0;
1127 /* mach-o object file structure:
1129 ** mach header
1130 ** uint32_t magic
1131 ** int cpu type
1132 ** int cpu subtype
1133 ** uint32_t mach file type
1134 ** uint32_t number of load commands
1135 ** uint32_t size of all load commands
1136 ** (includes section struct size of segment command)
1137 ** uint32_t flags
1139 ** segment command
1140 ** uint32_t command type == LC_SEGMENT
1141 ** uint32_t size of load command
1142 ** (including section load commands)
1143 ** char[16] segment name
1144 ** uint32_t in-memory offset
1145 ** uint32_t in-memory size
1146 ** uint32_t in-file offset to data area
1147 ** uint32_t in-file size
1148 ** (in-memory size excluding zerofill sections)
1149 ** int maximum vm protection
1150 ** int initial vm protection
1151 ** uint32_t number of sections
1152 ** uint32_t flags
1154 ** section commands
1155 ** char[16] section name
1156 ** char[16] segment name
1157 ** uint32_t in-memory offset
1158 ** uint32_t in-memory size
1159 ** uint32_t in-file offset
1160 ** uint32_t alignment
1161 ** (irrelevant in MH_OBJECT)
1162 ** uint32_t in-file offset of relocation entires
1163 ** uint32_t number of relocations
1164 ** uint32_t flags
1165 ** uint32_t reserved
1166 ** uint32_t reserved
1168 ** symbol table command
1169 ** uint32_t command type == LC_SYMTAB
1170 ** uint32_t size of load command
1171 ** uint32_t symbol table offset
1172 ** uint32_t number of symbol table entries
1173 ** uint32_t string table offset
1174 ** uint32_t string table size
1176 ** raw section data
1178 ** padding to int32_t boundary
1180 ** relocation data (struct reloc)
1181 ** int32_t offset
1182 ** uint data (symbolnum, pcrel, length, extern, type)
1184 ** symbol table data (struct nlist)
1185 ** int32_t string table entry number
1186 ** uint8_t type
1187 ** (extern, absolute, defined in section)
1188 ** uint8_t section
1189 ** (0 for global symbols, section number of definition (>= 1, <=
1190 ** 254) for local symbols, size of variable for common symbols
1191 ** [type == extern])
1192 ** int16_t description
1193 ** (for stab debugging format)
1194 ** uint32_t value (i.e. file offset) of symbol or stab offset
1196 ** string table data
1197 ** list of null-terminated strings
1200 /* Emit the Mach-O header. */
1201 macho_write_header();
1203 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1205 /* emit the segment load command */
1206 if (seg_nsects > 0)
1207 offset = macho_write_segment (offset);
1208 else
1209 nasm_error(ERR_WARNING, "no sections?");
1211 if (nsyms > 0) {
1212 /* write out symbol command */
1213 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1214 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1215 fwriteint32_t(offset, ofile); /* symbol table offset */
1216 fwriteint32_t(nsyms, ofile); /* number of symbol
1217 ** table entries */
1219 offset += nsyms * MACHO_NLIST_SIZE;
1220 fwriteint32_t(offset, ofile); /* string table offset */
1221 fwriteint32_t(strslen, ofile); /* string table size */
1224 /* emit section data */
1225 if (seg_nsects > 0)
1226 macho_write_section ();
1228 /* emit symbol table if we have symbols */
1229 if (nsyms > 0)
1230 macho_write_symtab ();
1232 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1234 /* emit string table */
1235 saa_fpwrite(strs, ofile);
1237 /* We do quite a bit here, starting with finalizing all of the data
1238 for the object file, writing, and then freeing all of the data from
1239 the file. */
1241 static void macho_cleanup(int debuginfo)
1243 struct section *s;
1244 struct reloc *r;
1245 struct symbol *sym;
1247 (void)debuginfo;
1249 /* Sort all symbols. */
1250 macho_layout_symbols (&nsyms, &strslen);
1252 /* Fixup relocation entries */
1253 for (s = sects; s != NULL; s = s->next) {
1254 macho_fixup_relocs (s->relocs);
1257 /* First calculate and finalize needed values. */
1258 macho_calculate_sizes();
1259 macho_write();
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_macho32 = {
1314 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1315 "macho32",
1317 null_debug_arr,
1318 &null_debug_form,
1319 macho_stdmac,
1320 macho_init,
1321 null_setinfo,
1322 macho_output,
1323 macho_symdef,
1324 macho_section,
1325 macho_segbase,
1326 null_directive,
1327 macho_filename,
1328 macho_cleanup
1331 struct ofmt of_macho = {
1332 "MACHO (short name for MACHO32)",
1333 "macho",
1335 null_debug_arr,
1336 &null_debug_form,
1337 macho_stdmac,
1338 macho_init,
1339 null_setinfo,
1340 macho_output,
1341 macho_symdef,
1342 macho_section,
1343 macho_segbase,
1344 null_directive,
1345 macho_filename,
1346 macho_cleanup
1349 #endif
1352 * Local Variables:
1353 * mode:c
1354 * c-basic-offset:4
1355 * End:
1357 * end of file */