nasm: rename nasm_zap_spaces() to nasm_zap_spaces_fwd()
[nasm/avx512.git] / output / outmacho32.c
blob234cf2859690a5c5547316fcc8161c4c6233afcd
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 static efunc error;
213 extern struct ofmt of_macho;
215 /* Global file information. This should be cleaned up into either
216 a structure or as function arguments. */
217 uint32_t head_ncmds = 0;
218 uint32_t head_sizeofcmds = 0;
219 uint32_t seg_filesize = 0;
220 uint32_t seg_vmsize = 0;
221 uint32_t seg_nsects = 0;
222 uint32_t rel_padcnt = 0;
225 #define xstrncpy(xdst, xsrc) \
226 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
227 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
228 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
230 #define align(x, y) \
231 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
233 #define alignint32_t(x) \
234 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
236 static void debug_reloc (struct reloc *);
237 static void debug_section_relocs (struct section *) _unused;
239 static int exact_log2 (uint32_t align)
241 if (align == 0) {
242 return 0;
243 } else if (align & (align-1)) {
244 return -1; /* Not a power of 2 */
245 } else {
246 #ifdef HAVE_GNUC_4
247 return __builtin_ctzl (align);
248 #else
249 uint32_t result = 0;
251 /* We know exactly one bit is set at this point. */
252 if (align & 0xffff0000)
253 result |= 16;
254 if (align & 0xff00ff00)
255 result |= 8;
256 if (align & 0xf0f0f0f0)
257 result |= 4;
258 if (align & 0xcccccccc)
259 result |= 2;
260 if (align & 0xaaaaaaaa)
261 result |= 1;
263 return result;
264 #endif
268 static struct section *get_section_by_name(const char *segname,
269 const char *sectname)
271 struct section *s;
273 for (s = sects; s != NULL; s = s->next)
274 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
275 break;
277 return s;
280 static struct section *get_section_by_index(const int32_t index)
282 struct section *s;
284 for (s = sects; s != NULL; s = s->next)
285 if (index == s->index)
286 break;
288 return s;
291 static int32_t get_section_index_by_name(const char *segname,
292 const char *sectname)
294 struct section *s;
296 for (s = sects; s != NULL; s = s->next)
297 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
298 return s->index;
300 return -1;
303 static char *get_section_name_by_index(const int32_t index)
305 struct section *s;
307 for (s = sects; s != NULL; s = s->next)
308 if (index == s->index)
309 return s->sectname;
311 return NULL;
314 static uint8_t get_section_fileindex_by_index(const int32_t index)
316 struct section *s;
317 uint8_t i = 1;
319 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
320 if (index == s->index)
321 return i;
323 if (i == MAX_SECT)
324 error(ERR_WARNING,
325 "too many sections (>255) - clipped by fileindex");
327 return NO_SECT;
330 static void macho_init(void)
332 char zero = 0;
334 sects = NULL;
335 sectstail = &sects;
337 syms = NULL;
338 symstail = &syms;
339 nsyms = 0;
340 nlocalsym = 0;
341 nextdefsym = 0;
342 nundefsym = 0;
344 extsyms = raa_init();
345 strs = saa_init(1L);
347 /* string table starts with a zero byte - don't ask why */
348 saa_wbytes(strs, &zero, sizeof(char));
349 strslen = 1;
352 static void sect_write(struct section *sect,
353 const uint8_t *data, uint32_t len)
355 saa_wbytes(sect->data, data, len);
356 sect->size += len;
359 static void add_reloc(struct section *sect, int32_t section,
360 int pcrel, int bytes)
362 struct reloc *r;
363 int32_t fi;
365 /* NeXT as puts relocs in reversed order (address-wise) into the
366 ** files, so we do the same, doesn't seem to make much of a
367 ** difference either way */
368 r = nasm_malloc(sizeof(struct reloc));
369 r->next = sect->relocs;
370 sect->relocs = r;
372 /* the current end of the section will be the symbol's address for
373 ** now, might have to be fixed by macho_fixup_relocs() later on. make
374 ** sure we don't make the symbol scattered by setting the highest
375 ** bit by accident */
376 r->addr = sect->size & ~R_SCATTERED;
377 r->ext = 0;
378 r->pcrel = pcrel;
380 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
381 r->length = bytes >> 1;
383 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
384 r->type = 0;
386 if (section == NO_SEG) {
387 /* absolute local symbol if no section index given */
388 r->snum = R_ABS;
389 } else {
390 fi = get_section_fileindex_by_index(section);
392 if (fi == NO_SECT) {
393 /* external symbol if no section with that index known,
394 ** symbol number was saved in macho_symdef() */
395 r->snum = raa_read(extsyms, section);
396 r->ext = 1;
397 } else {
398 /* local symbol in section fi */
399 r->snum = fi;
403 ++sect->nreloc;
406 static void macho_output(int32_t secto, const void *data,
407 enum out_type type, uint64_t size,
408 int32_t section, int32_t wrt)
410 struct section *s, *sbss;
411 int32_t addr;
412 uint8_t mydata[4], *p;
414 if (wrt != NO_SEG) {
415 wrt = NO_SEG;
416 error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
417 /* continue to do _something_ */
420 if (secto == NO_SEG) {
421 if (type != OUT_RESERVE)
422 error(ERR_NONFATAL, "attempt to assemble code in "
423 "[ABSOLUTE] space");
425 return;
428 s = get_section_by_index(secto);
430 if (s == NULL) {
431 error(ERR_WARNING, "attempt to assemble code in"
432 " section %d: defaulting to `.text'", secto);
433 s = get_section_by_name("__TEXT", "__text");
435 /* should never happen */
436 if (s == NULL)
437 error(ERR_PANIC, "text section not found");
440 sbss = get_section_by_name("__DATA", "__bss");
442 if (s == sbss && type != OUT_RESERVE) {
443 error(ERR_WARNING, "attempt to initialize memory in the"
444 " BSS section: ignored");
445 s->size += realsize(type, size);
446 return;
449 switch (type) {
450 case OUT_RESERVE:
451 if (s != sbss) {
452 error(ERR_WARNING, "uninitialized space declared in"
453 " %s section: zeroing",
454 get_section_name_by_index(secto));
456 sect_write(s, NULL, size);
457 } else
458 s->size += size;
460 break;
462 case OUT_RAWDATA:
463 if (section != NO_SEG)
464 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
466 sect_write(s, data, size);
467 break;
469 case OUT_ADDRESS:
470 addr = *(int64_t *)data;
472 if (section != NO_SEG) {
473 if (section % 2) {
474 error(ERR_NONFATAL, "Mach-O format does not support"
475 " section base references");
476 } else
477 add_reloc(s, section, 0, size);
480 p = mydata;
481 WRITEADDR(p, addr, size);
482 sect_write(s, mydata, size);
483 break;
485 case OUT_REL2ADR:
486 if (section == secto)
487 error(ERR_PANIC, "intra-section OUT_REL2ADR");
489 if (section != NO_SEG && section % 2) {
490 error(ERR_NONFATAL, "Mach-O format does not support"
491 " section base references");
492 } else
493 add_reloc(s, section, 1, 2);
495 p = mydata;
496 WRITESHORT(p, *(int32_t *)data - (size + s->size));
497 sect_write(s, mydata, 2L);
498 break;
500 case OUT_REL4ADR:
501 if (section == secto)
502 error(ERR_PANIC, "intra-section OUT_REL4ADR");
504 if (section != NO_SEG && section % 2) {
505 error(ERR_NONFATAL, "Mach-O format does not support"
506 " section base references");
507 } else
508 add_reloc(s, section, 1, 4);
510 p = mydata;
511 WRITELONG(p, *(int32_t *)data - (size + s->size));
512 sect_write(s, mydata, 4L);
513 break;
515 default:
516 error(ERR_PANIC, "unknown output type?");
517 break;
521 static int32_t macho_section(char *name, int pass, int *bits)
523 int32_t index, originalIndex;
524 char *sectionAttributes;
525 struct sectmap *sm;
526 struct section *s;
528 (void)pass;
530 /* Default to 32 bits. */
531 if (!name) {
532 *bits = 32;
533 name = ".text";
534 sectionAttributes = NULL;
535 } else {
536 sectionAttributes = name;
537 name = nasm_strsep(&sectionAttributes, " \t");
540 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
541 /* make lookup into section name translation table */
542 if (!strcmp(name, sm->nasmsect)) {
543 char *currentAttribute;
545 /* try to find section with that name */
546 originalIndex = index = get_section_index_by_name(sm->segname,
547 sm->sectname);
549 /* create it if it doesn't exist yet */
550 if (index == -1) {
551 s = *sectstail = nasm_malloc(sizeof(struct section));
552 s->next = NULL;
553 sectstail = &s->next;
555 s->data = saa_init(1L);
556 s->index = seg_alloc();
557 s->relocs = NULL;
558 s->align = -1;
560 xstrncpy(s->segname, sm->segname);
561 xstrncpy(s->sectname, sm->sectname);
562 s->size = 0;
563 s->nreloc = 0;
564 s->flags = sm->flags;
566 index = s->index;
567 } else {
568 s = get_section_by_index(index);
571 while ((NULL != sectionAttributes)
572 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
573 if (0 != *currentAttribute) {
574 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
575 char *end;
576 int newAlignment, value;
578 value = strtoul(currentAttribute + 6, (char**)&end, 0);
579 newAlignment = exact_log2(value);
581 if (0 != *end) {
582 error(ERR_PANIC,
583 "unknown or missing alignment value \"%s\" "
584 "specified for section \"%s\"",
585 currentAttribute + 6,
586 name);
587 return NO_SEG;
588 } else if (0 > newAlignment) {
589 error(ERR_PANIC,
590 "alignment of %d (for section \"%s\") is not "
591 "a power of two",
592 value,
593 name);
594 return NO_SEG;
597 if ((-1 != originalIndex)
598 && (s->align != newAlignment)
599 && (s->align != -1)) {
600 error(ERR_PANIC,
601 "section \"%s\" has already been specified "
602 "with alignment %d, conflicts with new "
603 "alignment of %d",
604 name,
605 (1 << s->align),
606 value);
607 return NO_SEG;
610 s->align = newAlignment;
611 } else if (!nasm_stricmp("data", currentAttribute)) {
612 /* Do nothing; 'data' is implicit */
613 } else {
614 error(ERR_PANIC,
615 "unknown section attribute %s for section %s",
616 currentAttribute,
617 name);
618 return NO_SEG;
623 return index;
627 error(ERR_PANIC, "invalid section name %s", name);
628 return NO_SEG;
631 static void macho_symdef(char *name, int32_t section, int64_t offset,
632 int is_global, char *special)
634 struct symbol *sym;
636 if (special) {
637 error(ERR_NONFATAL, "The Mach-O output format does "
638 "not support any special symbol types");
639 return;
642 if (is_global == 3) {
643 error(ERR_NONFATAL, "The Mach-O format does not "
644 "(yet) support forward reference fixups.");
645 return;
648 sym = *symstail = nasm_malloc(sizeof(struct symbol));
649 sym->next = NULL;
650 symstail = &sym->next;
652 sym->name = name;
653 sym->strx = strslen;
654 sym->type = 0;
655 sym->desc = 0;
656 sym->value = offset;
657 sym->initial_snum = -1;
659 /* external and common symbols get N_EXT */
660 if (is_global != 0)
661 sym->type |= N_EXT;
663 if (section == NO_SEG) {
664 /* symbols in no section get absolute */
665 sym->type |= N_ABS;
666 sym->sect = NO_SECT;
667 } else {
668 sym->type |= N_SECT;
670 /* get the in-file index of the section the symbol was defined in */
671 sym->sect = get_section_fileindex_by_index(section);
673 if (sym->sect == NO_SECT) {
674 /* remember symbol number of references to external
675 ** symbols, this works because every external symbol gets
676 ** its own section number allocated internally by nasm and
677 ** can so be used as a key */
678 extsyms = raa_write(extsyms, section, nsyms);
679 sym->initial_snum = nsyms;
681 switch (is_global) {
682 case 1:
683 case 2:
684 /* there isn't actually a difference between global
685 ** and common symbols, both even have their size in
686 ** sym->value */
687 sym->type = N_EXT;
688 break;
690 default:
691 /* give an error on unfound section if it's not an
692 ** external or common symbol (assemble_file() does a
693 ** seg_alloc() on every call for them) */
694 error(ERR_PANIC, "in-file index for section %d not found",
695 section);
700 ++nsyms;
703 static int32_t macho_segbase(int32_t section)
705 return section;
708 static void macho_filename(char *inname, char *outname)
710 standard_extension(inname, outname, ".o");
713 extern macros_t macho_stdmac[];
715 /* Comparison function for qsort symbol layout. */
716 static int layout_compare (const struct symbol **s1,
717 const struct symbol **s2)
719 return (strcmp ((*s1)->name, (*s2)->name));
722 /* The native assembler does a few things in a similar function
724 * Remove temporary labels
725 * Sort symbols according to local, external, undefined (by name)
726 * Order the string table
728 We do not remove temporary labels right now.
730 numsyms is the total number of symbols we have. strtabsize is the
731 number entries in the string table. */
733 static void macho_layout_symbols (uint32_t *numsyms,
734 uint32_t *strtabsize)
736 struct symbol *sym, **symp;
737 uint32_t i,j;
739 *numsyms = 0;
740 *strtabsize = sizeof (char);
742 symp = &syms;
744 while ((sym = *symp)) {
745 /* Undefined symbols are now external. */
746 if (sym->type == N_UNDF)
747 sym->type |= N_EXT;
749 if ((sym->type & N_EXT) == 0) {
750 sym->snum = *numsyms;
751 *numsyms = *numsyms + 1;
752 nlocalsym++;
754 else {
755 if ((sym->type & N_TYPE) != N_UNDF)
756 nextdefsym++;
757 else
758 nundefsym++;
760 /* If we handle debug info we'll want
761 to check for it here instead of just
762 adding the symbol to the string table. */
763 sym->strx = *strtabsize;
764 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
765 *strtabsize += strlen(sym->name) + 1;
767 symp = &(sym->next);
770 /* Next, sort the symbols. Most of this code is a direct translation from
771 the Apple cctools symbol layout. We need to keep compatibility with that. */
772 /* Set the indexes for symbol groups into the symbol table */
773 ilocalsym = 0;
774 iextdefsym = nlocalsym;
775 iundefsym = nlocalsym + nextdefsym;
777 /* allocate arrays for sorting externals by name */
778 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
779 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
781 i = 0;
782 j = 0;
784 symp = &syms;
786 while ((sym = *symp)) {
788 if((sym->type & N_EXT) == 0) {
789 sym->strx = *strtabsize;
790 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
791 *strtabsize += strlen(sym->name) + 1;
793 else {
794 if((sym->type & N_TYPE) != N_UNDF)
795 extdefsyms[i++] = sym;
796 else
797 undefsyms[j++] = sym;
799 symp = &(sym->next);
802 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
803 (int (*)(const void *, const void *))layout_compare);
804 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
805 (int (*)(const void *, const void *))layout_compare);
807 for(i = 0; i < nextdefsym; i++) {
808 extdefsyms[i]->snum = *numsyms;
809 *numsyms += 1;
811 for(j = 0; j < nundefsym; j++) {
812 undefsyms[j]->snum = *numsyms;
813 *numsyms += 1;
817 /* Calculate some values we'll need for writing later. */
819 static void macho_calculate_sizes (void)
821 struct section *s;
823 /* count sections and calculate in-memory and in-file offsets */
824 for (s = sects; s != NULL; s = s->next) {
825 uint32_t pad = 0;
827 /* zerofill sections aren't actually written to the file */
828 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
829 seg_filesize += s->size;
831 /* recalculate segment address based on alignment and vm size */
832 s->addr = seg_vmsize;
833 /* we need section alignment to calculate final section address */
834 if (s->align == -1)
835 s->align = DEFAULT_SECTION_ALIGNMENT;
836 if(s->align) {
837 uint32_t newaddr = align(s->addr, 1 << s->align);
838 pad = newaddr - s->addr;
839 s->addr = newaddr;
842 seg_vmsize += s->size + pad;
843 ++seg_nsects;
846 /* calculate size of all headers, load commands and sections to
847 ** get a pointer to the start of all the raw data */
848 if (seg_nsects > 0) {
849 ++head_ncmds;
850 head_sizeofcmds +=
851 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
854 if (nsyms > 0) {
855 ++head_ncmds;
856 head_sizeofcmds += MACHO_SYMCMD_SIZE;
860 /* Write out the header information for the file. */
862 static void macho_write_header (void)
864 fwriteint32_t(MH_MAGIC, ofile); /* magic */
865 fwriteint32_t(CPU_TYPE_I386, ofile); /* CPU type */
866 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
867 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
868 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
869 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
870 fwriteint32_t(0, ofile); /* no flags */
873 /* Write out the segment load command at offset. */
875 static uint32_t macho_write_segment (uint32_t offset)
877 uint32_t rel_base = alignint32_t (offset + seg_filesize);
878 uint32_t s_reloff = 0;
879 struct section *s;
881 fwriteint32_t(LC_SEGMENT, ofile); /* cmd == LC_SEGMENT */
883 /* size of load command including section load commands */
884 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
885 MACHO_SECTCMD_SIZE, ofile);
887 /* in an MH_OBJECT file all sections are in one unnamed (name
888 ** all zeros) segment */
889 fwritezero(16, ofile);
890 fwriteint32_t(0, ofile); /* in-memory offset */
891 fwriteint32_t(seg_vmsize, ofile); /* in-memory size */
892 fwriteint32_t(offset, ofile); /* in-file offset to data */
893 fwriteint32_t(seg_filesize, ofile); /* in-file size */
894 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
895 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
896 fwriteint32_t(seg_nsects, ofile); /* number of sections */
897 fwriteint32_t(0, ofile); /* no flags */
899 /* emit section headers */
900 for (s = sects; s != NULL; s = s->next) {
901 fwrite(s->sectname, sizeof(s->sectname), 1, ofile);
902 fwrite(s->segname, sizeof(s->segname), 1, ofile);
903 fwriteint32_t(s->addr, ofile);
904 fwriteint32_t(s->size, ofile);
906 /* dummy data for zerofill sections or proper values */
907 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
908 fwriteint32_t(offset, ofile);
909 /* Write out section alignment, as a power of two.
910 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
911 if (s->align == -1)
912 s->align = DEFAULT_SECTION_ALIGNMENT;
913 fwriteint32_t(s->align, ofile);
914 /* To be compatible with cctools as we emit
915 a zero reloff if we have no relocations. */
916 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
917 fwriteint32_t(s->nreloc, ofile);
919 offset += s->size;
920 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
921 } else {
922 fwriteint32_t(0, ofile);
923 fwriteint32_t(0, ofile);
924 fwriteint32_t(0, ofile);
925 fwriteint32_t(0, ofile);
928 fwriteint32_t(s->flags, ofile); /* flags */
929 fwriteint32_t(0, ofile); /* reserved */
930 fwriteint32_t(0, ofile); /* reserved */
933 rel_padcnt = rel_base - offset;
934 offset = rel_base + s_reloff;
936 return offset;
939 /* For a given chain of relocs r, write out the entire relocation
940 chain to the object file. */
942 static void macho_write_relocs (struct reloc *r)
944 while (r) {
945 uint32_t word2;
947 fwriteint32_t(r->addr, ofile); /* reloc offset */
949 word2 = r->snum;
950 word2 |= r->pcrel << 24;
951 word2 |= r->length << 25;
952 word2 |= r->ext << 27;
953 word2 |= r->type << 28;
954 fwriteint32_t(word2, ofile); /* reloc data */
956 r = r->next;
960 /* Write out the section data. */
961 static void macho_write_section (void)
963 struct section *s, *s2;
964 struct reloc *r;
965 uint8_t fi, *p, *q, blk[4];
966 int32_t l;
968 for (s = sects; s != NULL; s = s->next) {
969 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
970 continue;
972 /* no padding needs to be done to the sections */
974 /* Like a.out Mach-O references things in the data or bss
975 * sections by addresses which are actually relative to the
976 * start of the _text_ section, in the _file_. See outaout.c
977 * for more information. */
978 saa_rewind(s->data);
979 for (r = s->relocs; r != NULL; r = r->next) {
980 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
981 p = q = blk;
982 l = *p++;
984 /* get offset based on relocation type */
985 if (r->length > 0) {
986 l += ((int32_t)*p++) << 8;
988 if (r->length == 2) {
989 l += ((int32_t)*p++) << 16;
990 l += ((int32_t)*p++) << 24;
994 /* If the relocation is internal add to the current section
995 offset. Otherwise the only value we need is the symbol
996 offset which we already have. The linker takes care
997 of the rest of the address. */
998 if (!r->ext) {
999 /* generate final address by section address and offset */
1000 for (s2 = sects, fi = 1;
1001 s2 != NULL; s2 = s2->next, fi++) {
1002 if (fi == r->snum) {
1003 l += s2->addr;
1004 break;
1009 /* write new offset back */
1010 if (r->length == 2)
1011 WRITELONG(q, l);
1012 else if (r->length == 1)
1013 WRITESHORT(q, l);
1014 else
1015 *q++ = l & 0xFF;
1017 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1020 /* dump the section data to file */
1021 saa_fpwrite(s->data, ofile);
1024 /* pad last section up to reloc entries on int32_t boundary */
1025 fwritezero(rel_padcnt, ofile);
1027 /* emit relocation entries */
1028 for (s = sects; s != NULL; s = s->next)
1029 macho_write_relocs (s->relocs);
1032 /* Write out the symbol table. We should already have sorted this
1033 before now. */
1034 static void macho_write_symtab (void)
1036 struct symbol *sym;
1037 struct section *s;
1038 int32_t fi;
1039 uint32_t i;
1041 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1043 for (sym = syms; sym != NULL; sym = sym->next) {
1044 if ((sym->type & N_EXT) == 0) {
1045 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1046 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1047 fwrite(&sym->sect, 1, 1, ofile); /* section */
1048 fwriteint16_t(sym->desc, ofile); /* description */
1050 /* Fix up the symbol value now that we know the final section
1051 sizes. */
1052 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1053 for (s = sects, fi = 1;
1054 s != NULL && fi < sym->sect; s = s->next, ++fi)
1055 sym->value += s->size;
1058 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1062 for (i = 0; i < nextdefsym; i++) {
1063 sym = extdefsyms[i];
1064 fwriteint32_t(sym->strx, ofile);
1065 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1066 fwrite(&sym->sect, 1, 1, ofile); /* section */
1067 fwriteint16_t(sym->desc, ofile); /* description */
1069 /* Fix up the symbol value now that we know the final section
1070 sizes. */
1071 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1072 for (s = sects, fi = 1;
1073 s != NULL && fi < sym->sect; s = s->next, ++fi)
1074 sym->value += s->size;
1077 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1080 for (i = 0; i < nundefsym; i++) {
1081 sym = undefsyms[i];
1082 fwriteint32_t(sym->strx, ofile);
1083 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1084 fwrite(&sym->sect, 1, 1, ofile); /* section */
1085 fwriteint16_t(sym->desc, ofile); /* description */
1087 /* Fix up the symbol value now that we know the final section
1088 sizes. */
1089 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1090 for (s = sects, fi = 1;
1091 s != NULL && fi < sym->sect; s = s->next, ++fi)
1092 sym->value += s->size;
1095 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1099 /* Fixup the snum in the relocation entries, we should be
1100 doing this only for externally undefined symbols. */
1101 static void macho_fixup_relocs (struct reloc *r)
1103 struct symbol *sym;
1104 uint32_t i;
1106 while (r != NULL) {
1107 if (r->ext) {
1108 for (i = 0; i < nundefsym; i++) {
1109 sym = undefsyms[i];
1110 if (sym->initial_snum == r->snum) {
1111 r->snum = sym->snum;
1112 break;
1116 r = r->next;
1120 /* Write out the object file. */
1122 static void macho_write (void)
1124 uint32_t offset = 0;
1126 /* mach-o object file structure:
1128 ** mach header
1129 ** uint32_t magic
1130 ** int cpu type
1131 ** int cpu subtype
1132 ** uint32_t mach file type
1133 ** uint32_t number of load commands
1134 ** uint32_t size of all load commands
1135 ** (includes section struct size of segment command)
1136 ** uint32_t flags
1138 ** segment command
1139 ** uint32_t command type == LC_SEGMENT
1140 ** uint32_t size of load command
1141 ** (including section load commands)
1142 ** char[16] segment name
1143 ** uint32_t in-memory offset
1144 ** uint32_t in-memory size
1145 ** uint32_t in-file offset to data area
1146 ** uint32_t in-file size
1147 ** (in-memory size excluding zerofill sections)
1148 ** int maximum vm protection
1149 ** int initial vm protection
1150 ** uint32_t number of sections
1151 ** uint32_t flags
1153 ** section commands
1154 ** char[16] section name
1155 ** char[16] segment name
1156 ** uint32_t in-memory offset
1157 ** uint32_t in-memory size
1158 ** uint32_t in-file offset
1159 ** uint32_t alignment
1160 ** (irrelevant in MH_OBJECT)
1161 ** uint32_t in-file offset of relocation entires
1162 ** uint32_t number of relocations
1163 ** uint32_t flags
1164 ** uint32_t reserved
1165 ** uint32_t reserved
1167 ** symbol table command
1168 ** uint32_t command type == LC_SYMTAB
1169 ** uint32_t size of load command
1170 ** uint32_t symbol table offset
1171 ** uint32_t number of symbol table entries
1172 ** uint32_t string table offset
1173 ** uint32_t string table size
1175 ** raw section data
1177 ** padding to int32_t boundary
1179 ** relocation data (struct reloc)
1180 ** int32_t offset
1181 ** uint data (symbolnum, pcrel, length, extern, type)
1183 ** symbol table data (struct nlist)
1184 ** int32_t string table entry number
1185 ** uint8_t type
1186 ** (extern, absolute, defined in section)
1187 ** uint8_t section
1188 ** (0 for global symbols, section number of definition (>= 1, <=
1189 ** 254) for local symbols, size of variable for common symbols
1190 ** [type == extern])
1191 ** int16_t description
1192 ** (for stab debugging format)
1193 ** uint32_t value (i.e. file offset) of symbol or stab offset
1195 ** string table data
1196 ** list of null-terminated strings
1199 /* Emit the Mach-O header. */
1200 macho_write_header();
1202 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1204 /* emit the segment load command */
1205 if (seg_nsects > 0)
1206 offset = macho_write_segment (offset);
1207 else
1208 error(ERR_WARNING, "no sections?");
1210 if (nsyms > 0) {
1211 /* write out symbol command */
1212 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1213 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1214 fwriteint32_t(offset, ofile); /* symbol table offset */
1215 fwriteint32_t(nsyms, ofile); /* number of symbol
1216 ** table entries */
1218 offset += nsyms * MACHO_NLIST_SIZE;
1219 fwriteint32_t(offset, ofile); /* string table offset */
1220 fwriteint32_t(strslen, ofile); /* string table size */
1223 /* emit section data */
1224 if (seg_nsects > 0)
1225 macho_write_section ();
1227 /* emit symbol table if we have symbols */
1228 if (nsyms > 0)
1229 macho_write_symtab ();
1231 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1233 /* emit string table */
1234 saa_fpwrite(strs, ofile);
1236 /* We do quite a bit here, starting with finalizing all of the data
1237 for the object file, writing, and then freeing all of the data from
1238 the file. */
1240 static void macho_cleanup(int debuginfo)
1242 struct section *s;
1243 struct reloc *r;
1244 struct symbol *sym;
1246 (void)debuginfo;
1248 /* Sort all symbols. */
1249 macho_layout_symbols (&nsyms, &strslen);
1251 /* Fixup relocation entries */
1252 for (s = sects; s != NULL; s = s->next) {
1253 macho_fixup_relocs (s->relocs);
1256 /* First calculate and finalize needed values. */
1257 macho_calculate_sizes();
1258 macho_write();
1260 /* free up everything */
1261 while (sects->next) {
1262 s = sects;
1263 sects = sects->next;
1265 saa_free(s->data);
1266 while (s->relocs != NULL) {
1267 r = s->relocs;
1268 s->relocs = s->relocs->next;
1269 nasm_free(r);
1272 nasm_free(s);
1275 saa_free(strs);
1276 raa_free(extsyms);
1278 if (syms) {
1279 while (syms->next) {
1280 sym = syms;
1281 syms = syms->next;
1283 nasm_free (sym);
1288 /* Debugging routines. */
1289 static void debug_reloc (struct reloc *r)
1291 fprintf (stdout, "reloc:\n");
1292 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1293 fprintf (stdout, "\tsnum: %d\n", r->snum);
1294 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1295 fprintf (stdout, "\tlength: %d\n", r->length);
1296 fprintf (stdout, "\text: %d\n", r->ext);
1297 fprintf (stdout, "\ttype: %d\n", r->type);
1300 static void debug_section_relocs (struct section *s)
1302 struct reloc *r = s->relocs;
1304 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1306 while (r != NULL) {
1307 debug_reloc (r);
1308 r = r->next;
1312 struct ofmt of_macho32 = {
1313 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1314 "macho32",
1316 null_debug_arr,
1317 &null_debug_form,
1318 macho_stdmac,
1319 macho_init,
1320 null_setinfo,
1321 macho_output,
1322 macho_symdef,
1323 macho_section,
1324 macho_segbase,
1325 null_directive,
1326 macho_filename,
1327 macho_cleanup
1330 struct ofmt of_macho = {
1331 "MACHO (short name for MACHO32)",
1332 "macho",
1334 null_debug_arr,
1335 &null_debug_form,
1336 macho_stdmac,
1337 macho_init,
1338 null_setinfo,
1339 macho_output,
1340 macho_symdef,
1341 macho_section,
1342 macho_segbase,
1343 null_directive,
1344 macho_filename,
1345 macho_cleanup
1348 #endif
1351 * Local Variables:
1352 * mode:c
1353 * c-basic-offset:4
1354 * End:
1356 * end of file */