Merge the macho32 and macho64 (outmac32/64) backends
[nasm.git] / output / outmac.c
blob75a7838d08b6cbb5640ded62d8c8c8357dc3afb7
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 * outmac64.c output routines for the Netwide Assembler to produce
36 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) 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 "output/outform.h"
55 #include "output/outlib.h"
57 #if defined(OF_MACHO) || defined(OF_MACHO64)
59 /* Mach-O in-file header structure sizes */
60 #define MACHO_HEADER_SIZE 28
61 #define MACHO_SEGCMD_SIZE 56
62 #define MACHO_SECTCMD_SIZE 68
63 #define MACHO_SYMCMD_SIZE 24
64 #define MACHO_NLIST_SIZE 12
65 #define MACHO_RELINFO_SIZE 8
67 #define MACHO_HEADER64_SIZE 32
68 #define MACHO_SEGCMD64_SIZE 72
69 #define MACHO_SECTCMD64_SIZE 80
70 #define MACHO_NLIST64_SIZE 16
72 /* Mach-O file header values */
73 #define MH_MAGIC 0xfeedface
74 #define MH_MAGIC_64 0xfeedfacf
75 #define CPU_TYPE_I386 7 /* x86 platform */
76 #define CPU_TYPE_X86_64 0x01000007 /* x86-64 platform */
77 #define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */
78 #define MH_OBJECT 0x1 /* object file */
80 #define LC_SEGMENT 0x1 /* 32-bit segment load cmd */
81 #define LC_SEGMENT_64 0x19 /* 64-bit segment load cmd */
82 #define LC_SYMTAB 0x2 /* symbol table load command */
84 #define VM_PROT_NONE (0x00)
85 #define VM_PROT_READ (0x01)
86 #define VM_PROT_WRITE (0x02)
87 #define VM_PROT_EXECUTE (0x04)
89 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
90 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
92 struct macho_fmt {
93 uint32_t ptrsize; /* Pointer size in bytes */
94 uint32_t mh_magic; /* Which magic number to use */
95 uint32_t cpu_type; /* Which CPU type */
96 uint32_t lc_segment; /* Which segment load command */
97 uint32_t header_size; /* Header size */
98 uint32_t segcmd_size; /* Segment command size */
99 uint32_t sectcmd_size; /* Section command size */
100 uint32_t nlist_size; /* Nlist (symbol) size */
103 static const struct macho_fmt macho32_fmt = {
105 MH_MAGIC,
106 CPU_TYPE_I386,
107 LC_SEGMENT,
108 MACHO_HEADER_SIZE,
109 MACHO_SEGCMD_SIZE,
110 MACHO_SECTCMD_SIZE,
111 MACHO_NLIST_SIZE
114 static const struct macho_fmt macho64_fmt = {
116 MH_MAGIC_64,
117 CPU_TYPE_X86_64,
118 LC_SEGMENT_64,
119 MACHO_HEADER64_SIZE,
120 MACHO_SEGCMD64_SIZE,
121 MACHO_SECTCMD64_SIZE,
122 MACHO_NLIST64_SIZE
125 static const struct macho_fmt *fmt;
127 static void fwriteptr(uint64_t data, FILE * fp)
129 fwriteaddr(data, fmt->ptrsize, fp);
132 struct section {
133 /* nasm internal data */
134 struct section *next;
135 struct SAA *data;
136 int32_t index;
137 struct reloc *relocs;
138 int align;
140 /* data that goes into the file */
141 char sectname[16]; /* what this section is called */
142 char segname[16]; /* segment this section will be in */
143 uint64_t addr; /* in-memory address (subject to alignment) */
144 uint64_t size; /* in-memory and -file size */
145 uint64_t offset; /* in-file offset */
146 uint32_t pad; /* padding bytes before section */
147 uint32_t nreloc; /* relocation entry count */
148 uint32_t flags; /* type and attributes (masked) */
149 uint32_t extreloc; /* external relocations */
152 #define SECTION_TYPE 0x000000ff /* section type mask */
154 #define S_REGULAR (0x0) /* standard section */
155 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
157 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
158 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
159 machine instructions */
160 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
161 relocation entries */
162 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
163 relocation entries */
164 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
165 machine instructions */
167 static struct sectmap {
168 const char *nasmsect;
169 const char *segname;
170 const char *sectname;
171 const int32_t flags;
172 } sectmap[] = {
173 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
174 {".data", "__DATA", "__data", S_REGULAR},
175 {".rodata", "__DATA", "__const", S_REGULAR},
176 {".bss", "__DATA", "__bss", S_ZEROFILL},
177 {NULL, NULL, NULL, 0}
180 struct reloc {
181 /* nasm internal data */
182 struct reloc *next;
184 /* data that goes into the file */
185 int32_t addr; /* op's offset in section */
186 uint32_t snum:24, /* contains symbol index if
187 ** ext otherwise in-file
188 ** section number */
189 pcrel:1, /* relative relocation */
190 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
191 ext:1, /* external symbol referenced */
192 type:4; /* reloc type */
195 #define R_ABS 0 /* absolute relocation */
196 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
197 ** highest bit == 1 */
199 struct symbol {
200 /* nasm internal data */
201 struct symbol *next; /* next symbol in the list */
202 char *name; /* name of this symbol */
203 int32_t initial_snum; /* symbol number used above in
204 reloc */
205 int32_t snum; /* true snum for reloc */
207 /* data that goes into the file */
208 uint32_t strx; /* string table index */
209 uint8_t type; /* symbol type */
210 uint8_t sect; /* NO_SECT or section number */
211 uint16_t desc; /* for stab debugging, 0 for us */
212 uint64_t value; /* offset of symbol in section */
215 /* symbol type bits */
216 #define N_EXT 0x01 /* global or external symbol */
218 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
219 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
220 #define N_SECT 0xe /* defined symbol, n_sect holds
221 ** section number */
223 #define N_TYPE 0x0e /* type bit mask */
225 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
227 /* special section number values */
228 #define NO_SECT 0 /* no section, invalid */
229 #define MAX_SECT 255 /* maximum number of sections */
231 static struct section *sects, **sectstail, **sectstab;
232 static struct symbol *syms, **symstail;
233 static uint32_t nsyms;
235 /* These variables are set by macho_layout_symbols() to organize
236 the symbol table and string table in order the dynamic linker
237 expects. They are then used in macho_write() to put out the
238 symbols and strings in that order.
240 The order of the symbol table is:
241 local symbols
242 defined external symbols (sorted by name)
243 undefined external symbols (sorted by name)
245 The order of the string table is:
246 strings for external symbols
247 strings for local symbols
249 static uint32_t ilocalsym = 0;
250 static uint32_t iextdefsym = 0;
251 static uint32_t iundefsym = 0;
252 static uint32_t nlocalsym;
253 static uint32_t nextdefsym;
254 static uint32_t nundefsym;
255 static struct symbol **extdefsyms = NULL;
256 static struct symbol **undefsyms = NULL;
258 static struct RAA *extsyms;
259 static struct SAA *strs;
260 static uint32_t strslen;
262 extern struct ofmt of_macho64;
264 /* Global file information. This should be cleaned up into either
265 a structure or as function arguments. */
266 static uint32_t head_ncmds = 0;
267 static uint32_t head_sizeofcmds = 0;
268 static uint64_t seg_filesize = 0;
269 static uint64_t seg_vmsize = 0;
270 static uint32_t seg_nsects = 0;
271 static uint64_t rel_padcnt = 0;
274 #define xstrncpy(xdst, xsrc) \
275 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
276 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
277 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
279 #define alignint32_t(x) \
280 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
282 #define alignint64_t(x) \
283 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
285 #define alignptr(x) \
286 ALIGN(x, fmt->ptrsize) /* align x to output format width */
288 static void debug_reloc (struct reloc *);
289 static void debug_section_relocs (struct section *) _unused;
291 static struct section *get_section_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 break;
300 return s;
303 static struct section *get_section_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 break;
311 return s;
314 static int32_t get_section_index_by_name(const char *segname,
315 const char *sectname)
317 struct section *s;
319 for (s = sects; s != NULL; s = s->next)
320 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
321 return s->index;
323 return -1;
326 static char *get_section_name_by_index(const int32_t index)
328 struct section *s;
330 for (s = sects; s != NULL; s = s->next)
331 if (index == s->index)
332 return s->sectname;
334 return NULL;
337 static uint8_t get_section_fileindex_by_index(const int32_t index)
339 struct section *s;
340 uint8_t i = 1;
342 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
343 if (index == s->index)
344 return i;
346 if (i == MAX_SECT)
347 nasm_error(ERR_WARNING,
348 "too many sections (>255) - clipped by fileindex");
350 return NO_SECT;
353 static struct symbol *get_closest_section_symbol_by_offset(uint8_t fileindex, int64_t offset)
355 struct symbol *nearest = NULL;
356 struct symbol *sym;
358 for (sym = syms; sym; sym = sym->next) {
359 if ((sym->sect != NO_SECT) && (sym->sect == fileindex)) {
360 if ((int64_t)sym->value > offset)
361 break;
362 nearest = sym;
366 if (!nearest)
367 nasm_error(ERR_FATAL, "No section for index %x offset %llx found\n",
368 fileindex, (long long)offset);
370 return nearest;
374 * Special section numbers which are used to define Mach-O special
375 * symbols, which can be used with WRT to provide PIC relocation
376 * types.
378 static int32_t macho_gotpcrel_sect;
380 static void macho_init(void)
382 sects = NULL;
383 sectstail = &sects;
385 syms = NULL;
386 symstail = &syms;
387 nsyms = 0;
388 nlocalsym = 0;
389 nextdefsym = 0;
390 nundefsym = 0;
392 extsyms = raa_init();
393 strs = saa_init(1L);
395 /* string table starts with a zero byte so index 0 is an empty string */
396 saa_wbytes(strs, zero_buffer, 1);
397 strslen = 1;
400 static void sect_write(struct section *sect,
401 const uint8_t *data, uint32_t len)
403 saa_wbytes(sect->data, data, len);
404 sect->size += len;
407 enum reltype {
408 RL_ABS, /* Absolute relocation */
409 RL_REL, /* Relative relocation */
410 RL_SUB, /* X86_64_RELOC_SUBTRACT */
411 RL_GOT, /* X86_64_RELOC_GOT */
412 RL_GOTLOAD, /* X86_64_RELOC_GOT_LOAD */
415 static int32_t add_reloc(struct section *sect, int32_t section,
416 enum reltype reltype, int bytes, int64_t reloff)
418 struct reloc *r;
419 struct symbol *sym;
420 int32_t fi;
421 int32_t adjustment = 0;
423 if (section == NO_SEG)
424 return 0;
426 /* NeXT as puts relocs in reversed order (address-wise) into the
427 ** files, so we do the same, doesn't seem to make much of a
428 ** difference either way */
429 r = nasm_malloc(sizeof(struct reloc));
430 r->next = sect->relocs;
431 sect->relocs = r;
433 /* the current end of the section will be the symbol's address for
434 ** now, might have to be fixed by macho_fixup_relocs() later on. make
435 ** sure we don't make the symbol scattered by setting the highest
436 ** bit by accident */
437 r->addr = sect->size & ~R_SCATTERED;
438 r->ext = 1;
440 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
441 r->length = ilog2_32(bytes);
443 /* set default relocation values */
444 r->type = 0;
445 r->pcrel = 0;
446 r->snum = R_ABS;
448 /* absolute relocation */
449 switch (reltype) {
450 case RL_ABS:
451 if (section == NO_SEG) {
452 /* intra-section */
453 r->snum = R_ABS;
454 } else {
455 /* inter-section */
456 fi = get_section_fileindex_by_index(section);
458 if (fi == NO_SECT) {
459 /* external */
460 r->snum = raa_read(extsyms, section);
461 } else {
462 /* local */
463 sym = get_closest_section_symbol_by_offset(fi, reloff);
464 r->snum = sym->initial_snum;
465 adjustment = sym->value;
468 break;
470 case RL_REL:
471 r->pcrel = 1;
472 if (section == NO_SEG) {
473 /* intra-section */
474 r->type = 1; // X86_64_RELOC_SIGNED
475 } else {
476 /* inter-section */
477 r->type = 1; // X86_64_RELOC_SIGNED
478 fi = get_section_fileindex_by_index(section);
480 if (fi == NO_SECT) {
481 /* external */
482 sect->extreloc = 1;
483 r->snum = raa_read(extsyms, section);
484 } else {
485 /* local */
486 sym = get_closest_section_symbol_by_offset(fi, reloff);
487 r->snum = sym->initial_snum;
488 adjustment = sym->value;
491 break;
493 case RL_SUB:
494 r->pcrel = 0;
495 r->type = 5; // X86_64_RELOC_SUBTRACTOR
496 break;
498 case RL_GOT:
499 r->pcrel = 1;
500 r->type = 4; // X86_64_RELOC_GOT
501 r->snum = macho_gotpcrel_sect;
502 break;
504 case RL_GOTLOAD:
505 r->pcrel = 1;
506 r->type = 3; // X86_64_RELOC_GOT_LOAD
507 r->snum = macho_gotpcrel_sect;
508 break;
511 ++sect->nreloc;
513 return adjustment;
516 static void macho_output(int32_t secto, const void *data,
517 enum out_type type, uint64_t size,
518 int32_t section, int32_t wrt)
520 struct section *s, *sbss;
521 int64_t addr;
522 uint8_t mydata[16], *p, gotload;
524 if (secto == NO_SEG) {
525 if (type != OUT_RESERVE)
526 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
527 "[ABSOLUTE] space");
529 return;
532 s = get_section_by_index(secto);
534 if (s == NULL) {
535 nasm_error(ERR_WARNING, "attempt to assemble code in"
536 " section %d: defaulting to `.text'", secto);
537 s = get_section_by_name("__TEXT", "__text");
539 /* should never happen */
540 if (s == NULL)
541 nasm_error(ERR_PANIC, "text section not found");
544 sbss = get_section_by_name("__DATA", "__bss");
546 if (s == sbss && type != OUT_RESERVE) {
547 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
548 " BSS section: ignored");
549 s->size += realsize(type, size);
550 return;
553 memset(mydata, 0, sizeof(mydata));
555 switch (type) {
556 case OUT_RESERVE:
557 if (s != sbss) {
558 nasm_error(ERR_WARNING, "uninitialized space declared in"
559 " %s section: zeroing",
560 get_section_name_by_index(secto));
562 sect_write(s, NULL, size);
563 } else
564 s->size += size;
566 break;
568 case OUT_RAWDATA:
569 if (section != NO_SEG)
570 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
572 sect_write(s, data, size);
573 break;
575 case OUT_ADDRESS:
577 int asize = abs((int)size);
579 addr = *(int64_t *)data;
580 if (section != NO_SEG) {
581 if (section % 2) {
582 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
583 " section base references");
584 } else if (wrt == NO_SEG) {
585 if (fmt->ptrsize == 8 && asize != 8) {
586 nasm_error(ERR_NONFATAL, "Mach-O 64-bit format does not support"
587 " 32-bit absolute addresses");
588 } else {
589 addr -= add_reloc(s, section, RL_ABS, asize, addr);
591 } else {
592 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
593 " this use of WRT");
597 p = mydata;
598 WRITEADDR(p, addr, asize);
599 sect_write(s, mydata, asize);
600 break;
603 case OUT_REL2ADR:
604 nasm_assert(section != secto);
606 p = mydata;
607 addr = *(int64_t *)data + 2 - size;
609 if (section != NO_SEG && section % 2) {
610 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
611 " section base references");
612 } else if (fmt->ptrsize == 8) {
613 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
614 " Macho-O relocation [2]");
615 } else if (wrt != NO_SEG) {
616 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
617 " this use of WRT");
618 wrt = NO_SEG; /* we can at least _try_ to continue */
619 } else {
620 addr -= add_reloc(s, section, RL_REL, 2, addr);
623 WRITESHORT(p, addr);
624 sect_write(s, mydata, 2);
625 break;
627 case OUT_REL4ADR:
628 nasm_assert(section != secto);
630 p = mydata;
631 addr = *(int64_t *)data + 4 - size;
633 if (section != NO_SEG && section % 2) {
634 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
635 " section base references");
636 } else if (wrt == NO_SEG) {
637 /* Plain relative relocation */
638 addr -= add_reloc(s, section, RL_REL, 4, addr);
639 } else if (wrt == macho_gotpcrel_sect) {
640 if (s->data->datalen > 1) {
641 /* Retrieve instruction opcode */
642 saa_fread(s->data, s->data->datalen-2, &gotload, 1);
643 } else {
644 gotload = 0;
646 if (gotload == 0x8B) {
647 /* Check for MOVQ Opcode -> X86_64_RELOC_GOT_LOAD */
648 addr -= add_reloc(s, section, RL_GOTLOAD, 4, addr);
649 } else {
650 /* X86_64_RELOC_GOT */
651 addr -= add_reloc(s, section, RL_GOT, 4, addr);
653 } else {
654 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
655 " this use of WRT");
656 wrt = NO_SEG; /* we can at least _try_ to continue */
659 WRITELONG(p, addr);
660 sect_write(s, mydata, 4);
661 break;
663 default:
664 nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
665 break;
669 static int32_t macho_section(char *name, int pass, int *bits)
671 int32_t index, originalIndex;
672 char *sectionAttributes;
673 struct sectmap *sm;
674 struct section *s;
676 (void)pass;
678 /* Default to 64 bits. */
679 if (!name) {
680 *bits = 64;
681 name = ".text";
682 sectionAttributes = NULL;
683 } else {
684 sectionAttributes = name;
685 name = nasm_strsep(&sectionAttributes, " \t");
688 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
689 /* make lookup into section name translation table */
690 if (!strcmp(name, sm->nasmsect)) {
691 char *currentAttribute;
693 /* try to find section with that name */
694 originalIndex = index = get_section_index_by_name(sm->segname,
695 sm->sectname);
697 /* create it if it doesn't exist yet */
698 if (index == -1) {
699 s = *sectstail = nasm_malloc(sizeof(struct section));
700 s->next = NULL;
701 sectstail = &s->next;
703 s->data = saa_init(1L);
704 s->index = seg_alloc();
705 s->relocs = NULL;
706 s->align = -1;
707 s->pad = -1;
708 s->offset = -1;
710 xstrncpy(s->segname, sm->segname);
711 xstrncpy(s->sectname, sm->sectname);
712 s->size = 0;
713 s->nreloc = 0;
714 s->flags = sm->flags;
716 index = s->index;
717 } else {
718 s = get_section_by_index(index);
721 while ((NULL != sectionAttributes)
722 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
723 if (0 != *currentAttribute) {
724 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
725 char *end;
726 int newAlignment, value;
728 value = strtoul(currentAttribute + 6, (char**)&end, 0);
729 newAlignment = alignlog2_32(value);
731 if (0 != *end) {
732 nasm_error(ERR_PANIC,
733 "unknown or missing alignment value \"%s\" "
734 "specified for section \"%s\"",
735 currentAttribute + 6,
736 name);
737 return NO_SEG;
738 } else if (0 > newAlignment) {
739 nasm_error(ERR_PANIC,
740 "alignment of %d (for section \"%s\") is not "
741 "a power of two",
742 value,
743 name);
744 return NO_SEG;
747 if ((-1 != originalIndex)
748 && (s->align != newAlignment)
749 && (s->align != -1)) {
750 nasm_error(ERR_PANIC,
751 "section \"%s\" has already been specified "
752 "with alignment %d, conflicts with new "
753 "alignment of %d",
754 name,
755 (1 << s->align),
756 value);
757 return NO_SEG;
760 s->align = newAlignment;
761 } else if (!nasm_stricmp("data", currentAttribute)) {
762 /* Do nothing; 'data' is implicit */
763 } else {
764 nasm_error(ERR_PANIC,
765 "unknown section attribute %s for section %s",
766 currentAttribute,
767 name);
768 return NO_SEG;
773 return index;
777 nasm_error(ERR_PANIC, "invalid section name %s", name);
778 return NO_SEG;
781 static void macho_symdef(char *name, int32_t section, int64_t offset,
782 int is_global, char *special)
784 struct symbol *sym;
786 if (special) {
787 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
788 "not support any special symbol types");
789 return;
792 if (is_global == 3) {
793 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
794 "(yet) support forward reference fixups.");
795 return;
798 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
800 * This is a NASM special symbol. We never allow it into
801 * the Macho-O symbol table, even if it's a valid one. If it
802 * _isn't_ a valid one, we should barf immediately.
804 if (strcmp(name, "..gotpcrel"))
805 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
806 return;
809 sym = *symstail = nasm_malloc(sizeof(struct symbol));
810 sym->next = NULL;
811 symstail = &sym->next;
813 sym->name = name;
814 sym->strx = strslen;
815 sym->type = 0;
816 sym->desc = 0;
817 sym->value = offset;
818 sym->initial_snum = -1;
820 /* external and common symbols get N_EXT */
821 if (is_global != 0) {
822 sym->type |= N_EXT;
825 if (section == NO_SEG) {
826 /* symbols in no section get absolute */
827 sym->type |= N_ABS;
828 sym->sect = NO_SECT;
829 } else {
830 sym->type |= N_SECT;
832 /* get the in-file index of the section the symbol was defined in */
833 sym->sect = get_section_fileindex_by_index(section);
835 /* track the initially allocated symbol number for use in future fix-ups */
836 sym->initial_snum = nsyms;
838 if (sym->sect == NO_SECT) {
840 /* remember symbol number of references to external
841 ** symbols, this works because every external symbol gets
842 ** its own section number allocated internally by nasm and
843 ** can so be used as a key */
844 extsyms = raa_write(extsyms, section, nsyms);
846 switch (is_global) {
847 case 1:
848 case 2:
849 /* there isn't actually a difference between global
850 ** and common symbols, both even have their size in
851 ** sym->value */
852 sym->type = N_EXT;
853 break;
855 default:
856 /* give an error on unfound section if it's not an
857 ** external or common symbol (assemble_file() does a
858 ** seg_alloc() on every call for them) */
859 nasm_error(ERR_PANIC, "in-file index for section %d not found",
860 section);
864 ++nsyms;
867 static void macho_sectalign(int32_t seg, unsigned int value)
869 struct section *s;
871 list_for_each(s, sects) {
872 if (s->index == seg)
873 break;
876 if (!s || !is_power2(value))
877 return;
879 value = alignlog2_32(value);
880 if (s->align < (int)value)
881 s->align = value;
884 static int32_t macho_segbase(int32_t section)
886 return section;
889 static void macho_filename(char *inname, char *outname)
891 standard_extension(inname, outname, ".o");
894 extern macros_t macho_stdmac[];
896 /* Comparison function for qsort symbol layout. */
897 static int layout_compare (const struct symbol **s1,
898 const struct symbol **s2)
900 return (strcmp ((*s1)->name, (*s2)->name));
903 /* The native assembler does a few things in a similar function
905 * Remove temporary labels
906 * Sort symbols according to local, external, undefined (by name)
907 * Order the string table
909 We do not remove temporary labels right now.
911 numsyms is the total number of symbols we have. strtabsize is the
912 number entries in the string table. */
914 static void macho_layout_symbols (uint32_t *numsyms,
915 uint32_t *strtabsize)
917 struct symbol *sym, **symp;
918 uint32_t i,j;
920 *numsyms = 0;
921 *strtabsize = sizeof (char);
923 symp = &syms;
925 while ((sym = *symp)) {
926 /* Undefined symbols are now external. */
927 if (sym->type == N_UNDF)
928 sym->type |= N_EXT;
930 if ((sym->type & N_EXT) == 0) {
931 sym->snum = *numsyms;
932 *numsyms = *numsyms + 1;
933 nlocalsym++;
935 else {
936 if ((sym->type & N_TYPE) != N_UNDF) {
937 nextdefsym++;
938 } else {
939 nundefsym++;
942 /* If we handle debug info we'll want
943 to check for it here instead of just
944 adding the symbol to the string table. */
945 sym->strx = *strtabsize;
946 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
947 *strtabsize += strlen(sym->name) + 1;
949 symp = &(sym->next);
952 /* Next, sort the symbols. Most of this code is a direct translation from
953 the Apple cctools symbol layout. We need to keep compatibility with that. */
954 /* Set the indexes for symbol groups into the symbol table */
955 ilocalsym = 0;
956 iextdefsym = nlocalsym;
957 iundefsym = nlocalsym + nextdefsym;
959 /* allocate arrays for sorting externals by name */
960 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
961 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
963 i = 0;
964 j = 0;
966 symp = &syms;
968 while ((sym = *symp)) {
970 if((sym->type & N_EXT) == 0) {
971 sym->strx = *strtabsize;
972 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
973 *strtabsize += strlen(sym->name) + 1;
975 else {
976 if((sym->type & N_TYPE) != N_UNDF) {
977 extdefsyms[i++] = sym;
978 } else {
979 undefsyms[j++] = sym;
982 symp = &(sym->next);
985 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
986 (int (*)(const void *, const void *))layout_compare);
987 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
988 (int (*)(const void *, const void *))layout_compare);
990 for(i = 0; i < nextdefsym; i++) {
991 extdefsyms[i]->snum = *numsyms;
992 *numsyms += 1;
994 for(j = 0; j < nundefsym; j++) {
995 undefsyms[j]->snum = *numsyms;
996 *numsyms += 1;
1000 /* Calculate some values we'll need for writing later. */
1002 static void macho_calculate_sizes (void)
1004 struct section *s;
1005 int fi;
1007 /* count sections and calculate in-memory and in-file offsets */
1008 for (s = sects; s != NULL; s = s->next) {
1009 uint64_t newaddr;
1011 /* recalculate segment address based on alignment and vm size */
1012 s->addr = seg_vmsize;
1014 /* we need section alignment to calculate final section address */
1015 if (s->align == -1)
1016 s->align = DEFAULT_SECTION_ALIGNMENT;
1018 newaddr = ALIGN(s->addr, 1 << s->align);
1019 s->addr = newaddr;
1021 seg_vmsize = newaddr + s->size;
1023 /* zerofill sections aren't actually written to the file */
1024 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1026 * LLVM/Xcode as always aligns the section data to 4
1027 * bytes; there is a comment in the LLVM source code that
1028 * perhaps aligning to pointer size would be better.
1030 s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
1031 s->offset = seg_filesize + s->pad;
1032 seg_filesize += s->size + s->pad;
1035 ++seg_nsects;
1038 /* calculate size of all headers, load commands and sections to
1039 ** get a pointer to the start of all the raw data */
1040 if (seg_nsects > 0) {
1041 ++head_ncmds;
1042 head_sizeofcmds += fmt->segcmd_size + seg_nsects * fmt->sectcmd_size;
1045 if (nsyms > 0) {
1046 ++head_ncmds;
1047 head_sizeofcmds += MACHO_SYMCMD_SIZE;
1050 /* Create a table of sections by file index to avoid linear search */
1051 sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
1052 sectstab[0] = NULL;
1053 for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
1054 sectstab[fi] = s;
1057 /* Write out the header information for the file. */
1059 static void macho_write_header (void)
1061 fwriteint32_t(fmt->mh_magic, ofile); /* magic */
1062 fwriteint32_t(fmt->cpu_type, ofile); /* CPU type */
1063 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1064 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1065 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
1066 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
1067 fwriteint32_t(0, ofile); /* no flags */
1068 fwritezero(fmt->header_size - 7*4, ofile); /* reserved fields */
1071 /* Write out the segment load command at offset. */
1073 static uint32_t macho_write_segment (uint64_t offset)
1075 uint64_t rel_base = alignptr(offset + seg_filesize);
1076 uint32_t s_reloff = 0;
1077 struct section *s;
1079 fwriteint32_t(fmt->lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
1081 /* size of load command including section load commands */
1082 fwriteint32_t(fmt->segcmd_size + seg_nsects * fmt->sectcmd_size,
1083 ofile);
1085 /* in an MH_OBJECT file all sections are in one unnamed (name
1086 ** all zeros) segment */
1087 fwritezero(16, ofile);
1088 fwriteptr(0, ofile); /* in-memory offset */
1089 fwriteptr(seg_vmsize, ofile); /* in-memory size */
1090 fwriteptr(offset, ofile); /* in-file offset to data */
1091 fwriteptr(seg_filesize, ofile); /* in-file size */
1092 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1093 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1094 fwriteint32_t(seg_nsects, ofile); /* number of sections */
1095 fwriteint32_t(0, ofile); /* no flags */
1097 /* emit section headers */
1098 for (s = sects; s != NULL; s = s->next) {
1099 nasm_write(s->sectname, sizeof(s->sectname), ofile);
1100 nasm_write(s->segname, sizeof(s->segname), ofile);
1101 fwriteptr(s->addr, ofile);
1102 fwriteptr(s->size, ofile);
1104 /* dummy data for zerofill sections or proper values */
1105 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1106 nasm_assert(s->pad != (uint32_t)-1);
1107 offset += s->pad;
1108 fwriteint32_t(offset, ofile);
1109 offset += s->size;
1110 /* Write out section alignment, as a power of two.
1111 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1112 fwriteint32_t(s->align, ofile);
1113 /* To be compatible with cctools as we emit
1114 a zero reloff if we have no relocations. */
1115 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1116 fwriteint32_t(s->nreloc, ofile);
1118 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
1119 } else {
1120 fwriteint32_t(0, ofile);
1121 fwriteint32_t(s->align, ofile);
1122 fwriteint32_t(0, ofile);
1123 fwriteint32_t(0, ofile);
1126 if (s->nreloc) {
1127 s->flags |= S_ATTR_LOC_RELOC;
1128 if (s->extreloc)
1129 s->flags |= S_ATTR_EXT_RELOC;
1132 fwriteint32_t(s->flags, ofile); /* flags */
1133 fwriteint32_t(0, ofile); /* reserved */
1134 fwriteptr(0, ofile); /* reserved */
1137 rel_padcnt = rel_base - offset;
1138 offset = rel_base + s_reloff;
1140 return offset;
1143 /* For a given chain of relocs r, write out the entire relocation
1144 chain to the object file. */
1146 static void macho_write_relocs (struct reloc *r)
1148 while (r) {
1149 uint32_t word2;
1151 fwriteint32_t(r->addr, ofile); /* reloc offset */
1153 word2 = r->snum;
1154 word2 |= r->pcrel << 24;
1155 word2 |= r->length << 25;
1156 word2 |= r->ext << 27;
1157 word2 |= r->type << 28;
1158 fwriteint32_t(word2, ofile); /* reloc data */
1159 r = r->next;
1163 /* Write out the section data. */
1164 static void macho_write_section (void)
1166 struct section *s, *s2;
1167 struct reloc *r;
1168 uint8_t fi, *p;
1169 int32_t len;
1170 int64_t l;
1171 union offset {
1172 uint64_t val;
1173 uint8_t buf[8];
1174 } blk;
1176 for (s = sects; s != NULL; s = s->next) {
1177 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1178 continue;
1180 /* Like a.out Mach-O references things in the data or bss
1181 * sections by addresses which are actually relative to the
1182 * start of the _text_ section, in the _file_. See outaout.c
1183 * for more information. */
1184 saa_rewind(s->data);
1185 for (r = s->relocs; r != NULL; r = r->next) {
1186 len = (uint32_t)1 << r->length;
1187 if (len > 4) /* Can this ever be an issue?! */
1188 len = 8;
1189 blk.val = 0;
1190 saa_fread(s->data, r->addr, blk.buf, len);
1192 /* get offset based on relocation type */
1193 #ifdef WORDS_LITTLEENDIAN
1194 l = blk.val;
1195 #else
1196 l = blk.buf[0];
1197 l += ((int64_t)blk.buf[1]) << 8;
1198 l += ((int64_t)blk.buf[2]) << 16;
1199 l += ((int64_t)blk.buf[3]) << 24;
1200 l += ((int64_t)blk.buf[4]) << 32;
1201 l += ((int64_t)blk.buf[5]) << 40;
1202 l += ((int64_t)blk.buf[6]) << 48;
1203 l += ((int64_t)blk.buf[7]) << 56;
1204 #endif
1206 /* If the relocation is internal add to the current section
1207 offset. Otherwise the only value we need is the symbol
1208 offset which we already have. The linker takes care
1209 of the rest of the address. */
1210 if (!r->ext) {
1211 /* generate final address by section address and offset */
1212 for (s2 = sects, fi = 1;
1213 s2 != NULL; s2 = s2->next, fi++) {
1214 if (fi == r->snum) {
1215 l += s2->addr;
1216 break;
1221 /* write new offset back */
1222 p = blk.buf;
1223 WRITEDLONG(p, l);
1224 saa_fwrite(s->data, r->addr, blk.buf, len);
1227 /* dump the section data to file */
1228 fwritezero(s->pad, ofile);
1229 saa_fpwrite(s->data, ofile);
1232 /* pad last section up to reloc entries on pointer boundary */
1233 fwritezero(rel_padcnt, ofile);
1235 /* emit relocation entries */
1236 for (s = sects; s != NULL; s = s->next)
1237 macho_write_relocs (s->relocs);
1240 /* Write out the symbol table. We should already have sorted this
1241 before now. */
1242 static void macho_write_symtab (void)
1244 struct symbol *sym;
1245 uint64_t i;
1247 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1249 for (sym = syms; sym != NULL; sym = sym->next) {
1250 if ((sym->type & N_EXT) == 0) {
1251 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1252 nasm_write(&sym->type, 1, ofile); /* symbol type */
1253 nasm_write(&sym->sect, 1, ofile); /* section */
1254 fwriteint16_t(sym->desc, ofile); /* description */
1256 /* Fix up the symbol value now that we know the final section
1257 sizes. */
1258 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1259 nasm_assert(sym->sect <= seg_nsects);
1260 sym->value += sectstab[sym->sect]->addr;
1263 fwriteptr(sym->value, ofile); /* value (i.e. offset) */
1267 for (i = 0; i < nextdefsym; i++) {
1268 sym = extdefsyms[i];
1269 fwriteint32_t(sym->strx, ofile);
1270 nasm_write(&sym->type, 1, ofile); /* symbol type */
1271 nasm_write(&sym->sect, 1, ofile); /* section */
1272 fwriteint16_t(sym->desc, ofile); /* description */
1274 /* Fix up the symbol value now that we know the final section
1275 sizes. */
1276 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1277 nasm_assert(sym->sect <= seg_nsects);
1278 sym->value += sectstab[sym->sect]->addr;
1281 fwriteptr(sym->value, ofile); /* value (i.e. offset) */
1284 for (i = 0; i < nundefsym; i++) {
1285 sym = undefsyms[i];
1286 fwriteint32_t(sym->strx, ofile);
1287 nasm_write(&sym->type, 1, ofile); /* symbol type */
1288 nasm_write(&sym->sect, 1, ofile); /* section */
1289 fwriteint16_t(sym->desc, ofile); /* description */
1291 /* Fix up the symbol value now that we know the final section
1292 sizes. */
1293 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1294 nasm_assert(sym->sect <= seg_nsects);
1295 sym->value += sectstab[sym->sect]->addr;
1298 fwriteptr(sym->value, ofile); /* value (i.e. offset) */
1303 /* Fixup the snum in the relocation entries, we should be
1304 doing this only for externally referenced symbols. */
1305 static void macho_fixup_relocs (struct reloc *r)
1307 struct symbol *sym;
1309 while (r != NULL) {
1310 if (r->ext) {
1311 for (sym = syms; sym != NULL; sym = sym->next) {
1312 if (sym->initial_snum == r->snum) {
1313 r->snum = sym->snum;
1314 break;
1318 r = r->next;
1322 /* Write out the object file. */
1324 static void macho_write (void)
1326 uint64_t offset = 0;
1328 /* mach-o object file structure:
1330 ** mach header
1331 ** uint32_t magic
1332 ** int cpu type
1333 ** int cpu subtype
1334 ** uint32_t mach file type
1335 ** uint32_t number of load commands
1336 ** uint32_t size of all load commands
1337 ** (includes section struct size of segment command)
1338 ** uint32_t flags
1340 ** segment command
1341 ** uint32_t command type == LC_SEGMENT[_64]
1342 ** uint32_t size of load command
1343 ** (including section load commands)
1344 ** char[16] segment name
1345 ** pointer in-memory offset
1346 ** pointer in-memory size
1347 ** pointer in-file offset to data area
1348 ** pointer in-file size
1349 ** (in-memory size excluding zerofill sections)
1350 ** int maximum vm protection
1351 ** int initial vm protection
1352 ** uint32_t number of sections
1353 ** uint32_t flags
1355 ** section commands
1356 ** char[16] section name
1357 ** char[16] segment name
1358 ** pointer in-memory offset
1359 ** pointer in-memory size
1360 ** uint32_t in-file offset
1361 ** uint32_t alignment
1362 ** (irrelevant in MH_OBJECT)
1363 ** uint32_t in-file offset of relocation entires
1364 ** uint32_t number of relocations
1365 ** uint32_t flags
1366 ** uint32_t reserved
1367 ** uint32_t reserved
1369 ** symbol table command
1370 ** uint32_t command type == LC_SYMTAB
1371 ** uint32_t size of load command
1372 ** uint32_t symbol table offset
1373 ** uint32_t number of symbol table entries
1374 ** uint32_t string table offset
1375 ** uint32_t string table size
1377 ** raw section data
1379 ** padding to pointer boundary
1381 ** relocation data (struct reloc)
1382 ** int32_t offset
1383 ** uint data (symbolnum, pcrel, length, extern, type)
1385 ** symbol table data (struct nlist)
1386 ** int32_t string table entry number
1387 ** uint8_t type
1388 ** (extern, absolute, defined in section)
1389 ** uint8_t section
1390 ** (0 for global symbols, section number of definition (>= 1, <=
1391 ** 254) for local symbols, size of variable for common symbols
1392 ** [type == extern])
1393 ** int16_t description
1394 ** (for stab debugging format)
1395 ** pointer value (i.e. file offset) of symbol or stab offset
1397 ** string table data
1398 ** list of null-terminated strings
1401 /* Emit the Mach-O header. */
1402 macho_write_header();
1404 offset = fmt->header_size + head_sizeofcmds;
1406 /* emit the segment load command */
1407 if (seg_nsects > 0)
1408 offset = macho_write_segment (offset);
1409 else
1410 nasm_error(ERR_WARNING, "no sections?");
1412 if (nsyms > 0) {
1413 /* write out symbol command */
1414 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1415 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1416 fwriteint32_t(offset, ofile); /* symbol table offset */
1417 fwriteint32_t(nsyms, ofile); /* number of symbol
1418 ** table entries */
1419 offset += nsyms * fmt->nlist_size;
1420 fwriteint32_t(offset, ofile); /* string table offset */
1421 fwriteint32_t(strslen, ofile); /* string table size */
1424 /* emit section data */
1425 if (seg_nsects > 0)
1426 macho_write_section ();
1428 /* emit symbol table if we have symbols */
1429 if (nsyms > 0)
1430 macho_write_symtab ();
1432 /* we don't need to pad here, we are already aligned */
1434 /* emit string table */
1435 saa_fpwrite(strs, ofile);
1437 /* We do quite a bit here, starting with finalizing all of the data
1438 for the object file, writing, and then freeing all of the data from
1439 the file. */
1441 static void macho_cleanup(int debuginfo)
1443 struct section *s;
1444 struct reloc *r;
1445 struct symbol *sym;
1447 (void)debuginfo;
1449 /* Sort all symbols. */
1450 macho_layout_symbols (&nsyms, &strslen);
1452 /* Fixup relocation entries */
1453 for (s = sects; s != NULL; s = s->next) {
1454 macho_fixup_relocs (s->relocs);
1457 /* First calculate and finalize needed values. */
1458 macho_calculate_sizes();
1459 macho_write();
1461 /* free up everything */
1462 while (sects->next) {
1463 s = sects;
1464 sects = sects->next;
1466 saa_free(s->data);
1467 while (s->relocs != NULL) {
1468 r = s->relocs;
1469 s->relocs = s->relocs->next;
1470 nasm_free(r);
1473 nasm_free(s);
1476 saa_free(strs);
1477 raa_free(extsyms);
1479 while (syms) {
1480 sym = syms;
1481 syms = syms->next;
1482 nasm_free (sym);
1485 nasm_free(extdefsyms);
1486 nasm_free(undefsyms);
1487 nasm_free(sectstab);
1490 /* Debugging routines. */
1491 static void debug_reloc (struct reloc *r)
1493 fprintf (stdout, "reloc:\n");
1494 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1495 fprintf (stdout, "\tsnum: %d\n", r->snum);
1496 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1497 fprintf (stdout, "\tlength: %d\n", r->length);
1498 fprintf (stdout, "\text: %d\n", r->ext);
1499 fprintf (stdout, "\ttype: %d\n", r->type);
1502 static void debug_section_relocs (struct section *s)
1504 struct reloc *r = s->relocs;
1506 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1508 while (r != NULL) {
1509 debug_reloc (r);
1510 r = r->next;
1514 #ifdef OF_MACHO32
1515 static void macho32_init(void)
1517 fmt = &macho32_fmt;
1518 macho_init();
1520 macho_gotpcrel_sect = NO_SEG;
1523 struct ofmt of_macho32 = {
1524 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1525 "macho64",
1528 null_debug_arr,
1529 &null_debug_form,
1530 macho_stdmac,
1531 macho32_init,
1532 null_setinfo,
1533 macho_output,
1534 macho_symdef,
1535 macho_section,
1536 macho_sectalign,
1537 macho_segbase,
1538 null_directive,
1539 macho_filename,
1540 macho_cleanup
1542 #endif
1544 #ifdef OF_MACHO64
1545 static void macho64_init(void)
1547 fmt = &macho64_fmt;
1548 macho_init();
1550 /* add special symbol for ..gotpcrel */
1551 macho_gotpcrel_sect = seg_alloc();
1552 macho_gotpcrel_sect++;
1553 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false);
1556 struct ofmt of_macho64 = {
1557 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1558 "macho64",
1561 null_debug_arr,
1562 &null_debug_form,
1563 macho_stdmac,
1564 macho64_init,
1565 null_setinfo,
1566 macho_output,
1567 macho_symdef,
1568 macho_section,
1569 macho_sectalign,
1570 macho_segbase,
1571 null_directive,
1572 macho_filename,
1573 macho_cleanup
1575 #endif
1577 #endif
1580 * Local Variables:
1581 * mode:c
1582 * c-basic-offset:4
1583 * End:
1585 * end of file */