output: outmac -- Fix few nits during merge
[nasm.git] / output / outmac.c
blob18bbf498830de32cdf9af2a5affe094bed59a17b
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 *fmt;
105 static void fwriteptr(uint64_t data, FILE * fp)
107 fwriteaddr(data, fmt->ptrsize, fp);
110 struct section {
111 /* nasm internal data */
112 struct section *next;
113 struct SAA *data;
114 int32_t index;
115 struct reloc *relocs;
116 int align;
118 /* data that goes into the file */
119 char sectname[16]; /* what this section is called */
120 char segname[16]; /* segment this section will be in */
121 uint64_t addr; /* in-memory address (subject to alignment) */
122 uint64_t size; /* in-memory and -file size */
123 uint64_t offset; /* in-file offset */
124 uint32_t pad; /* padding bytes before section */
125 uint32_t nreloc; /* relocation entry count */
126 uint32_t flags; /* type and attributes (masked) */
127 uint32_t extreloc; /* external relocations */
130 #define SECTION_TYPE 0x000000ff /* section type mask */
132 #define S_REGULAR (0x0) /* standard section */
133 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
135 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
136 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
137 machine instructions */
138 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
139 relocation entries */
140 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
141 relocation entries */
142 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
143 machine instructions */
145 static struct sectmap {
146 const char *nasmsect;
147 const char *segname;
148 const char *sectname;
149 const int32_t flags;
150 } sectmap[] = {
151 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
152 {".data", "__DATA", "__data", S_REGULAR},
153 {".rodata", "__DATA", "__const", S_REGULAR},
154 {".bss", "__DATA", "__bss", S_ZEROFILL},
155 {NULL, NULL, NULL, 0}
158 struct reloc {
159 /* nasm internal data */
160 struct reloc *next;
162 /* data that goes into the file */
163 int32_t addr; /* op's offset in section */
164 uint32_t snum:24, /* contains symbol index if
165 ** ext otherwise in-file
166 ** section number */
167 pcrel:1, /* relative relocation */
168 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
169 ext:1, /* external symbol referenced */
170 type:4; /* reloc type */
173 #define R_ABS 0 /* absolute relocation */
174 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
175 ** highest bit == 1 */
177 struct symbol {
178 /* nasm internal data */
179 struct symbol *next; /* next symbol in the list */
180 char *name; /* name of this symbol */
181 int32_t initial_snum; /* symbol number used above in
182 reloc */
183 int32_t snum; /* true snum for reloc */
185 /* data that goes into the file */
186 uint32_t strx; /* string table index */
187 uint8_t type; /* symbol type */
188 uint8_t sect; /* NO_SECT or section number */
189 uint16_t desc; /* for stab debugging, 0 for us */
190 uint64_t value; /* offset of symbol in section */
193 /* symbol type bits */
194 #define N_EXT 0x01 /* global or external symbol */
196 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
197 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
198 #define N_SECT 0xe /* defined symbol, n_sect holds
199 ** section number */
201 #define N_TYPE 0x0e /* type bit mask */
203 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
205 /* special section number values */
206 #define NO_SECT 0 /* no section, invalid */
207 #define MAX_SECT 255 /* maximum number of sections */
209 static struct section *sects, **sectstail, **sectstab;
210 static struct symbol *syms, **symstail;
211 static uint32_t nsyms;
213 /* These variables are set by macho_layout_symbols() to organize
214 the symbol table and string table in order the dynamic linker
215 expects. They are then used in macho_write() to put out the
216 symbols and strings in that order.
218 The order of the symbol table is:
219 local symbols
220 defined external symbols (sorted by name)
221 undefined external symbols (sorted by name)
223 The order of the string table is:
224 strings for external symbols
225 strings for local symbols
227 static uint32_t ilocalsym = 0;
228 static uint32_t iextdefsym = 0;
229 static uint32_t iundefsym = 0;
230 static uint32_t nlocalsym;
231 static uint32_t nextdefsym;
232 static uint32_t nundefsym;
233 static struct symbol **extdefsyms = NULL;
234 static struct symbol **undefsyms = NULL;
236 static struct RAA *extsyms;
237 static struct SAA *strs;
238 static uint32_t strslen;
240 extern struct ofmt of_macho64;
242 /* Global file information. This should be cleaned up into either
243 a structure or as function arguments. */
244 static uint32_t head_ncmds = 0;
245 static uint32_t head_sizeofcmds = 0;
246 static uint64_t seg_filesize = 0;
247 static uint64_t seg_vmsize = 0;
248 static uint32_t seg_nsects = 0;
249 static uint64_t rel_padcnt = 0;
252 #define xstrncpy(xdst, xsrc) \
253 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
254 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
255 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
257 #define alignint32_t(x) \
258 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
260 #define alignint64_t(x) \
261 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
263 #define alignptr(x) \
264 ALIGN(x, fmt->ptrsize) /* align x to output format width */
266 static void debug_reloc (struct reloc *);
267 static void debug_section_relocs (struct section *) _unused;
269 static struct section *get_section_by_name(const char *segname,
270 const char *sectname)
272 struct section *s;
274 for (s = sects; s != NULL; s = s->next)
275 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
276 break;
278 return s;
281 static struct section *get_section_by_index(const int32_t index)
283 struct section *s;
285 for (s = sects; s != NULL; s = s->next)
286 if (index == s->index)
287 break;
289 return s;
292 static int32_t get_section_index_by_name(const char *segname,
293 const char *sectname)
295 struct section *s;
297 for (s = sects; s != NULL; s = s->next)
298 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
299 return s->index;
301 return -1;
304 static char *get_section_name_by_index(const int32_t index)
306 struct section *s;
308 for (s = sects; s != NULL; s = s->next)
309 if (index == s->index)
310 return s->sectname;
312 return NULL;
315 static uint8_t get_section_fileindex_by_index(const int32_t index)
317 struct section *s;
318 uint8_t i = 1;
320 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
321 if (index == s->index)
322 return i;
324 if (i == MAX_SECT)
325 nasm_error(ERR_WARNING,
326 "too many sections (>255) - clipped by fileindex");
328 return NO_SECT;
331 static struct symbol *get_closest_section_symbol_by_offset(uint8_t fileindex, int64_t offset)
333 struct symbol *nearest = NULL;
334 struct symbol *sym;
336 for (sym = syms; sym; sym = sym->next) {
337 if ((sym->sect != NO_SECT) && (sym->sect == fileindex)) {
338 if ((int64_t)sym->value > offset)
339 break;
340 nearest = sym;
344 if (!nearest)
345 nasm_error(ERR_FATAL, "No section for index %x offset %llx found\n",
346 fileindex, (long long)offset);
348 return nearest;
352 * Special section numbers which are used to define Mach-O special
353 * symbols, which can be used with WRT to provide PIC relocation
354 * types.
356 static int32_t macho_gotpcrel_sect;
358 static void macho_init(void)
360 sects = NULL;
361 sectstail = &sects;
363 syms = NULL;
364 symstail = &syms;
365 nsyms = 0;
366 nlocalsym = 0;
367 nextdefsym = 0;
368 nundefsym = 0;
370 extsyms = raa_init();
371 strs = saa_init(1L);
373 /* string table starts with a zero byte so index 0 is an empty string */
374 saa_wbytes(strs, zero_buffer, 1);
375 strslen = 1;
378 static void sect_write(struct section *sect,
379 const uint8_t *data, uint32_t len)
381 saa_wbytes(sect->data, data, len);
382 sect->size += len;
385 enum reltype {
386 RL_ABS, /* Absolute relocation */
387 RL_REL, /* Relative relocation */
388 RL_SUB, /* X86_64_RELOC_SUBTRACT */
389 RL_GOT, /* X86_64_RELOC_GOT */
390 RL_GOTLOAD, /* X86_64_RELOC_GOT_LOAD */
393 static int32_t add_reloc(struct section *sect, int32_t section,
394 enum reltype reltype, int bytes, int64_t reloff)
396 struct reloc *r;
397 struct symbol *sym;
398 int32_t fi;
399 int32_t adjustment = 0;
401 if (section == NO_SEG)
402 return 0;
404 /* NeXT as puts relocs in reversed order (address-wise) into the
405 ** files, so we do the same, doesn't seem to make much of a
406 ** difference either way */
407 r = nasm_malloc(sizeof(struct reloc));
408 r->next = sect->relocs;
409 sect->relocs = r;
411 /* the current end of the section will be the symbol's address for
412 ** now, might have to be fixed by macho_fixup_relocs() later on. make
413 ** sure we don't make the symbol scattered by setting the highest
414 ** bit by accident */
415 r->addr = sect->size & ~R_SCATTERED;
416 r->ext = 1;
418 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
419 r->length = ilog2_32(bytes);
421 /* set default relocation values */
422 r->type = 0;
423 r->pcrel = 0;
424 r->snum = R_ABS;
426 /* absolute relocation */
427 switch (reltype) {
428 case RL_ABS:
429 if (section == NO_SEG) {
430 /* intra-section */
431 r->snum = R_ABS;
432 } else {
433 /* inter-section */
434 fi = get_section_fileindex_by_index(section);
436 if (fi == NO_SECT) {
437 /* external */
438 r->snum = raa_read(extsyms, section);
439 } else {
440 /* local */
441 sym = get_closest_section_symbol_by_offset(fi, reloff);
442 r->snum = sym->initial_snum;
443 adjustment = sym->value;
446 break;
448 case RL_REL:
449 r->pcrel = 1;
450 if (section == NO_SEG) {
451 /* intra-section */
452 r->type = 1; // X86_64_RELOC_SIGNED
453 } else {
454 /* inter-section */
455 r->type = 1; // X86_64_RELOC_SIGNED
456 fi = get_section_fileindex_by_index(section);
458 if (fi == NO_SECT) {
459 /* external */
460 sect->extreloc = 1;
461 r->snum = raa_read(extsyms, section);
462 } else {
463 /* local */
464 sym = get_closest_section_symbol_by_offset(fi, reloff);
465 r->snum = sym->initial_snum;
466 adjustment = sym->value;
469 break;
471 case RL_SUB:
472 r->pcrel = 0;
473 r->type = 5; // X86_64_RELOC_SUBTRACTOR
474 break;
476 case RL_GOT:
477 r->pcrel = 1;
478 r->type = 4; // X86_64_RELOC_GOT
479 r->snum = macho_gotpcrel_sect;
480 break;
482 case RL_GOTLOAD:
483 r->pcrel = 1;
484 r->type = 3; // X86_64_RELOC_GOT_LOAD
485 r->snum = macho_gotpcrel_sect;
486 break;
489 ++sect->nreloc;
491 return adjustment;
494 static void macho_output(int32_t secto, const void *data,
495 enum out_type type, uint64_t size,
496 int32_t section, int32_t wrt)
498 struct section *s, *sbss;
499 int64_t addr;
500 uint8_t mydata[16], *p, gotload;
502 if (secto == NO_SEG) {
503 if (type != OUT_RESERVE)
504 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
505 "[ABSOLUTE] space");
507 return;
510 s = get_section_by_index(secto);
512 if (s == NULL) {
513 nasm_error(ERR_WARNING, "attempt to assemble code in"
514 " section %d: defaulting to `.text'", secto);
515 s = get_section_by_name("__TEXT", "__text");
517 /* should never happen */
518 if (s == NULL)
519 nasm_error(ERR_PANIC, "text section not found");
522 sbss = get_section_by_name("__DATA", "__bss");
524 if (s == sbss && type != OUT_RESERVE) {
525 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
526 " BSS section: ignored");
527 s->size += realsize(type, size);
528 return;
531 memset(mydata, 0, sizeof(mydata));
533 switch (type) {
534 case OUT_RESERVE:
535 if (s != sbss) {
536 nasm_error(ERR_WARNING, "uninitialized space declared in"
537 " %s section: zeroing",
538 get_section_name_by_index(secto));
540 sect_write(s, NULL, size);
541 } else
542 s->size += size;
544 break;
546 case OUT_RAWDATA:
547 if (section != NO_SEG)
548 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
550 sect_write(s, data, size);
551 break;
553 case OUT_ADDRESS:
555 int asize = abs((int)size);
557 addr = *(int64_t *)data;
558 if (section != NO_SEG) {
559 if (section % 2) {
560 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
561 " section base references");
562 } else if (wrt == NO_SEG) {
563 if (fmt->ptrsize == 8 && asize != 8) {
564 nasm_error(ERR_NONFATAL, "Mach-O 64-bit format does not support"
565 " 32-bit absolute addresses");
566 } else {
567 addr -= add_reloc(s, section, RL_ABS, asize, addr);
569 } else {
570 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
571 " this use of WRT");
575 p = mydata;
576 WRITEADDR(p, addr, asize);
577 sect_write(s, mydata, asize);
578 break;
581 case OUT_REL2ADR:
582 nasm_assert(section != secto);
584 p = mydata;
585 addr = *(int64_t *)data + 2 - size;
587 if (section != NO_SEG && section % 2) {
588 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
589 " section base references");
590 } else if (fmt->ptrsize == 8) {
591 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
592 " Macho-O relocation [2]");
593 } else if (wrt != NO_SEG) {
594 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
595 " this use of WRT");
596 wrt = NO_SEG; /* we can at least _try_ to continue */
597 } else {
598 addr -= add_reloc(s, section, RL_REL, 2, addr);
601 WRITESHORT(p, addr);
602 sect_write(s, mydata, 2);
603 break;
605 case OUT_REL4ADR:
606 nasm_assert(section != secto);
608 p = mydata;
609 addr = *(int64_t *)data + 4 - size;
611 if (section != NO_SEG && section % 2) {
612 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
613 " section base references");
614 } else if (wrt == NO_SEG) {
615 /* Plain relative relocation */
616 addr -= add_reloc(s, section, RL_REL, 4, addr);
617 } else if (wrt == macho_gotpcrel_sect) {
618 if (s->data->datalen > 1) {
619 /* Retrieve instruction opcode */
620 saa_fread(s->data, s->data->datalen-2, &gotload, 1);
621 } else {
622 gotload = 0;
624 if (gotload == 0x8B) {
625 /* Check for MOVQ Opcode -> X86_64_RELOC_GOT_LOAD */
626 addr -= add_reloc(s, section, RL_GOTLOAD, 4, addr);
627 } else {
628 /* X86_64_RELOC_GOT */
629 addr -= add_reloc(s, section, RL_GOT, 4, addr);
631 } else {
632 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
633 " this use of WRT");
634 wrt = NO_SEG; /* we can at least _try_ to continue */
637 WRITELONG(p, addr);
638 sect_write(s, mydata, 4);
639 break;
641 default:
642 nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
643 break;
647 static int32_t macho_section(char *name, int pass, int *bits)
649 int32_t index;
650 char *sectionAttributes;
651 struct sectmap *sm;
652 struct section *s;
654 (void)pass;
656 /* Default to the appropriate number of bits. */
657 if (!name) {
658 *bits = fmt->ptrsize << 3;
659 name = ".text";
660 sectionAttributes = NULL;
661 } else {
662 sectionAttributes = name;
663 name = nasm_strsep(&sectionAttributes, " \t");
666 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
667 /* make lookup into section name translation table */
668 if (!strcmp(name, sm->nasmsect)) {
669 char *currentAttribute;
671 /* try to find section with that name */
672 index = get_section_index_by_name(sm->segname, sm->sectname);
674 /* create it if it doesn't exist yet */
675 if (index == -1) {
676 s = *sectstail = nasm_malloc(sizeof(struct section));
677 s->next = NULL;
678 sectstail = &s->next;
680 s->data = saa_init(1L);
681 s->index = seg_alloc();
682 s->relocs = NULL;
683 s->align = -1;
684 s->pad = -1;
685 s->offset = -1;
687 xstrncpy(s->segname, sm->segname);
688 xstrncpy(s->sectname, sm->sectname);
689 s->size = 0;
690 s->nreloc = 0;
691 s->flags = sm->flags;
693 index = s->index;
694 } else {
695 s = get_section_by_index(index);
698 while ((NULL != sectionAttributes)
699 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
700 if (0 != *currentAttribute) {
701 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
702 char *end;
703 int newAlignment, value;
705 value = strtoul(currentAttribute + 6, (char**)&end, 0);
706 newAlignment = alignlog2_32(value);
708 if (0 != *end) {
709 nasm_error(ERR_FATAL,
710 "unknown or missing alignment value \"%s\" "
711 "specified for section \"%s\"",
712 currentAttribute + 6,
713 name);
714 return NO_SEG;
715 } else if (0 > newAlignment) {
716 nasm_error(ERR_FATAL,
717 "alignment of %d (for section \"%s\") is not "
718 "a power of two",
719 value,
720 name);
721 return NO_SEG;
724 if (s->align < newAlignment)
725 s->align = newAlignment;
726 } else if (!nasm_stricmp("data", currentAttribute)) {
727 /* Do nothing; 'data' is implicit */
728 } else {
729 nasm_error(ERR_FATAL,
730 "unknown section attribute %s for section %s",
731 currentAttribute,
732 name);
733 return NO_SEG;
738 return index;
742 nasm_error(ERR_FATAL, "invalid section name %s", name);
743 return NO_SEG;
746 static void macho_symdef(char *name, int32_t section, int64_t offset,
747 int is_global, char *special)
749 struct symbol *sym;
751 if (special) {
752 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
753 "not support any special symbol types");
754 return;
757 if (is_global == 3) {
758 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
759 "(yet) support forward reference fixups.");
760 return;
763 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
765 * This is a NASM special symbol. We never allow it into
766 * the Macho-O symbol table, even if it's a valid one. If it
767 * _isn't_ a valid one, we should barf immediately.
769 if (strcmp(name, "..gotpcrel"))
770 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
771 return;
774 sym = *symstail = nasm_malloc(sizeof(struct symbol));
775 sym->next = NULL;
776 symstail = &sym->next;
778 sym->name = name;
779 sym->strx = strslen;
780 sym->type = 0;
781 sym->desc = 0;
782 sym->value = offset;
783 sym->initial_snum = -1;
785 /* external and common symbols get N_EXT */
786 if (is_global != 0) {
787 sym->type |= N_EXT;
790 if (section == NO_SEG) {
791 /* symbols in no section get absolute */
792 sym->type |= N_ABS;
793 sym->sect = NO_SECT;
794 } else {
795 sym->type |= N_SECT;
797 /* get the in-file index of the section the symbol was defined in */
798 sym->sect = get_section_fileindex_by_index(section);
800 /* track the initially allocated symbol number for use in future fix-ups */
801 sym->initial_snum = nsyms;
803 if (sym->sect == NO_SECT) {
805 /* remember symbol number of references to external
806 ** symbols, this works because every external symbol gets
807 ** its own section number allocated internally by nasm and
808 ** can so be used as a key */
809 extsyms = raa_write(extsyms, section, nsyms);
811 switch (is_global) {
812 case 1:
813 case 2:
814 /* there isn't actually a difference between global
815 ** and common symbols, both even have their size in
816 ** sym->value */
817 sym->type = N_EXT;
818 break;
820 default:
821 /* give an error on unfound section if it's not an
822 ** external or common symbol (assemble_file() does a
823 ** seg_alloc() on every call for them) */
824 nasm_error(ERR_PANIC, "in-file index for section %d not found",
825 section);
829 ++nsyms;
832 static void macho_sectalign(int32_t seg, unsigned int value)
834 struct section *s;
835 int align;
837 list_for_each(s, sects) {
838 if (s->index == seg)
839 break;
842 if (!s || !is_power2(value))
843 return;
845 align = alignlog2_32(value);
846 if (s->align < align)
847 s->align = align;
850 static int32_t macho_segbase(int32_t section)
852 return section;
855 static void macho_filename(char *inname, char *outname)
857 standard_extension(inname, outname, ".o");
860 extern macros_t macho_stdmac[];
862 /* Comparison function for qsort symbol layout. */
863 static int layout_compare (const struct symbol **s1,
864 const struct symbol **s2)
866 return (strcmp ((*s1)->name, (*s2)->name));
869 /* The native assembler does a few things in a similar function
871 * Remove temporary labels
872 * Sort symbols according to local, external, undefined (by name)
873 * Order the string table
875 We do not remove temporary labels right now.
877 numsyms is the total number of symbols we have. strtabsize is the
878 number entries in the string table. */
880 static void macho_layout_symbols (uint32_t *numsyms,
881 uint32_t *strtabsize)
883 struct symbol *sym, **symp;
884 uint32_t i,j;
886 *numsyms = 0;
887 *strtabsize = sizeof (char);
889 symp = &syms;
891 while ((sym = *symp)) {
892 /* Undefined symbols are now external. */
893 if (sym->type == N_UNDF)
894 sym->type |= N_EXT;
896 if ((sym->type & N_EXT) == 0) {
897 sym->snum = *numsyms;
898 *numsyms = *numsyms + 1;
899 nlocalsym++;
901 else {
902 if ((sym->type & N_TYPE) != N_UNDF) {
903 nextdefsym++;
904 } else {
905 nundefsym++;
908 /* If we handle debug info we'll want
909 to check for it here instead of just
910 adding the symbol to the string table. */
911 sym->strx = *strtabsize;
912 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
913 *strtabsize += strlen(sym->name) + 1;
915 symp = &(sym->next);
918 /* Next, sort the symbols. Most of this code is a direct translation from
919 the Apple cctools symbol layout. We need to keep compatibility with that. */
920 /* Set the indexes for symbol groups into the symbol table */
921 ilocalsym = 0;
922 iextdefsym = nlocalsym;
923 iundefsym = nlocalsym + nextdefsym;
925 /* allocate arrays for sorting externals by name */
926 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
927 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
929 i = 0;
930 j = 0;
932 symp = &syms;
934 while ((sym = *symp)) {
936 if((sym->type & N_EXT) == 0) {
937 sym->strx = *strtabsize;
938 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
939 *strtabsize += strlen(sym->name) + 1;
941 else {
942 if((sym->type & N_TYPE) != N_UNDF) {
943 extdefsyms[i++] = sym;
944 } else {
945 undefsyms[j++] = sym;
948 symp = &(sym->next);
951 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
952 (int (*)(const void *, const void *))layout_compare);
953 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
954 (int (*)(const void *, const void *))layout_compare);
956 for(i = 0; i < nextdefsym; i++) {
957 extdefsyms[i]->snum = *numsyms;
958 *numsyms += 1;
960 for(j = 0; j < nundefsym; j++) {
961 undefsyms[j]->snum = *numsyms;
962 *numsyms += 1;
966 /* Calculate some values we'll need for writing later. */
968 static void macho_calculate_sizes (void)
970 struct section *s;
971 int fi;
973 /* count sections and calculate in-memory and in-file offsets */
974 for (s = sects; s != NULL; s = s->next) {
975 uint64_t newaddr;
977 /* recalculate segment address based on alignment and vm size */
978 s->addr = seg_vmsize;
980 /* we need section alignment to calculate final section address */
981 if (s->align == -1)
982 s->align = DEFAULT_SECTION_ALIGNMENT;
984 newaddr = ALIGN(s->addr, 1 << s->align);
985 s->addr = newaddr;
987 seg_vmsize = newaddr + s->size;
989 /* zerofill sections aren't actually written to the file */
990 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
992 * LLVM/Xcode as always aligns the section data to 4
993 * bytes; there is a comment in the LLVM source code that
994 * perhaps aligning to pointer size would be better.
996 s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
997 s->offset = seg_filesize + s->pad;
998 seg_filesize += s->size + s->pad;
1001 ++seg_nsects;
1004 /* calculate size of all headers, load commands and sections to
1005 ** get a pointer to the start of all the raw data */
1006 if (seg_nsects > 0) {
1007 ++head_ncmds;
1008 head_sizeofcmds += fmt->segcmd_size + seg_nsects * fmt->sectcmd_size;
1011 if (nsyms > 0) {
1012 ++head_ncmds;
1013 head_sizeofcmds += MACHO_SYMCMD_SIZE;
1016 /* Create a table of sections by file index to avoid linear search */
1017 sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
1018 sectstab[0] = NULL;
1019 for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
1020 sectstab[fi] = s;
1023 /* Write out the header information for the file. */
1025 static void macho_write_header (void)
1027 fwriteint32_t(fmt->mh_magic, ofile); /* magic */
1028 fwriteint32_t(fmt->cpu_type, ofile); /* CPU type */
1029 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1030 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1031 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
1032 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
1033 fwriteint32_t(0, ofile); /* no flags */
1034 fwritezero(fmt->header_size - 7*4, ofile); /* reserved fields */
1037 /* Write out the segment load command at offset. */
1039 static uint32_t macho_write_segment (uint64_t offset)
1041 uint64_t rel_base = alignptr(offset + seg_filesize);
1042 uint32_t s_reloff = 0;
1043 struct section *s;
1045 fwriteint32_t(fmt->lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
1047 /* size of load command including section load commands */
1048 fwriteint32_t(fmt->segcmd_size + seg_nsects * fmt->sectcmd_size,
1049 ofile);
1051 /* in an MH_OBJECT file all sections are in one unnamed (name
1052 ** all zeros) segment */
1053 fwritezero(16, ofile);
1054 fwriteptr(0, ofile); /* in-memory offset */
1055 fwriteptr(seg_vmsize, ofile); /* in-memory size */
1056 fwriteptr(offset, ofile); /* in-file offset to data */
1057 fwriteptr(seg_filesize, ofile); /* in-file size */
1058 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1059 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1060 fwriteint32_t(seg_nsects, ofile); /* number of sections */
1061 fwriteint32_t(0, ofile); /* no flags */
1063 /* emit section headers */
1064 for (s = sects; s != NULL; s = s->next) {
1065 nasm_write(s->sectname, sizeof(s->sectname), ofile);
1066 nasm_write(s->segname, sizeof(s->segname), ofile);
1067 fwriteptr(s->addr, ofile);
1068 fwriteptr(s->size, ofile);
1070 /* dummy data for zerofill sections or proper values */
1071 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1072 nasm_assert(s->pad != (uint32_t)-1);
1073 offset += s->pad;
1074 fwriteint32_t(offset, ofile);
1075 offset += s->size;
1076 /* Write out section alignment, as a power of two.
1077 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1078 fwriteint32_t(s->align, ofile);
1079 /* To be compatible with cctools as we emit
1080 a zero reloff if we have no relocations. */
1081 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1082 fwriteint32_t(s->nreloc, ofile);
1084 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
1085 } else {
1086 fwriteint32_t(0, ofile);
1087 fwriteint32_t(s->align, ofile);
1088 fwriteint32_t(0, ofile);
1089 fwriteint32_t(0, ofile);
1092 if (s->nreloc) {
1093 s->flags |= S_ATTR_LOC_RELOC;
1094 if (s->extreloc)
1095 s->flags |= S_ATTR_EXT_RELOC;
1098 fwriteint32_t(s->flags, ofile); /* flags */
1099 fwriteint32_t(0, ofile); /* reserved */
1100 fwriteptr(0, ofile); /* reserved */
1103 rel_padcnt = rel_base - offset;
1104 offset = rel_base + s_reloff;
1106 return offset;
1109 /* For a given chain of relocs r, write out the entire relocation
1110 chain to the object file. */
1112 static void macho_write_relocs (struct reloc *r)
1114 while (r) {
1115 uint32_t word2;
1117 fwriteint32_t(r->addr, ofile); /* reloc offset */
1119 word2 = r->snum;
1120 word2 |= r->pcrel << 24;
1121 word2 |= r->length << 25;
1122 word2 |= r->ext << 27;
1123 word2 |= r->type << 28;
1124 fwriteint32_t(word2, ofile); /* reloc data */
1125 r = r->next;
1129 /* Write out the section data. */
1130 static void macho_write_section (void)
1132 struct section *s, *s2;
1133 struct reloc *r;
1134 uint8_t fi, *p;
1135 int32_t len;
1136 int64_t l;
1137 union offset {
1138 uint64_t val;
1139 uint8_t buf[8];
1140 } blk;
1142 for (s = sects; s != NULL; s = s->next) {
1143 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1144 continue;
1146 /* Like a.out Mach-O references things in the data or bss
1147 * sections by addresses which are actually relative to the
1148 * start of the _text_ section, in the _file_. See outaout.c
1149 * for more information. */
1150 saa_rewind(s->data);
1151 for (r = s->relocs; r != NULL; r = r->next) {
1152 len = (uint32_t)1 << r->length;
1153 if (len > 4) /* Can this ever be an issue?! */
1154 len = 8;
1155 blk.val = 0;
1156 saa_fread(s->data, r->addr, blk.buf, len);
1158 /* get offset based on relocation type */
1159 #ifdef WORDS_LITTLEENDIAN
1160 l = blk.val;
1161 #else
1162 l = blk.buf[0];
1163 l += ((int64_t)blk.buf[1]) << 8;
1164 l += ((int64_t)blk.buf[2]) << 16;
1165 l += ((int64_t)blk.buf[3]) << 24;
1166 l += ((int64_t)blk.buf[4]) << 32;
1167 l += ((int64_t)blk.buf[5]) << 40;
1168 l += ((int64_t)blk.buf[6]) << 48;
1169 l += ((int64_t)blk.buf[7]) << 56;
1170 #endif
1172 /* If the relocation is internal add to the current section
1173 offset. Otherwise the only value we need is the symbol
1174 offset which we already have. The linker takes care
1175 of the rest of the address. */
1176 if (!r->ext) {
1177 /* generate final address by section address and offset */
1178 for (s2 = sects, fi = 1;
1179 s2 != NULL; s2 = s2->next, fi++) {
1180 if (fi == r->snum) {
1181 l += s2->addr;
1182 break;
1187 /* write new offset back */
1188 p = blk.buf;
1189 WRITEDLONG(p, l);
1190 saa_fwrite(s->data, r->addr, blk.buf, len);
1193 /* dump the section data to file */
1194 fwritezero(s->pad, ofile);
1195 saa_fpwrite(s->data, ofile);
1198 /* pad last section up to reloc entries on pointer boundary */
1199 fwritezero(rel_padcnt, ofile);
1201 /* emit relocation entries */
1202 for (s = sects; s != NULL; s = s->next)
1203 macho_write_relocs (s->relocs);
1206 /* Write out the symbol table. We should already have sorted this
1207 before now. */
1208 static void macho_write_symtab (void)
1210 struct symbol *sym;
1211 uint64_t i;
1213 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1215 for (sym = syms; sym != NULL; sym = sym->next) {
1216 if ((sym->type & N_EXT) == 0) {
1217 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1218 nasm_write(&sym->type, 1, ofile); /* symbol type */
1219 nasm_write(&sym->sect, 1, ofile); /* section */
1220 fwriteint16_t(sym->desc, ofile); /* description */
1222 /* Fix up the symbol value now that we know the final section
1223 sizes. */
1224 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1225 nasm_assert(sym->sect <= seg_nsects);
1226 sym->value += sectstab[sym->sect]->addr;
1229 fwriteptr(sym->value, ofile); /* value (i.e. offset) */
1233 for (i = 0; i < nextdefsym; i++) {
1234 sym = extdefsyms[i];
1235 fwriteint32_t(sym->strx, ofile);
1236 nasm_write(&sym->type, 1, ofile); /* symbol type */
1237 nasm_write(&sym->sect, 1, ofile); /* section */
1238 fwriteint16_t(sym->desc, ofile); /* description */
1240 /* Fix up the symbol value now that we know the final section
1241 sizes. */
1242 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1243 nasm_assert(sym->sect <= seg_nsects);
1244 sym->value += sectstab[sym->sect]->addr;
1247 fwriteptr(sym->value, ofile); /* value (i.e. offset) */
1250 for (i = 0; i < nundefsym; i++) {
1251 sym = undefsyms[i];
1252 fwriteint32_t(sym->strx, ofile);
1253 nasm_write(&sym->type, 1, ofile); /* symbol type */
1254 nasm_write(&sym->sect, 1, ofile); /* section */
1255 fwriteint16_t(sym->desc, ofile); /* description */
1257 /* Fix up the symbol value now that we know the final section
1258 sizes. */
1259 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1260 nasm_assert(sym->sect <= seg_nsects);
1261 sym->value += sectstab[sym->sect]->addr;
1264 fwriteptr(sym->value, ofile); /* value (i.e. offset) */
1269 /* Fixup the snum in the relocation entries, we should be
1270 doing this only for externally referenced symbols. */
1271 static void macho_fixup_relocs (struct reloc *r)
1273 struct symbol *sym;
1275 while (r != NULL) {
1276 if (r->ext) {
1277 for (sym = syms; sym != NULL; sym = sym->next) {
1278 if (sym->initial_snum == r->snum) {
1279 r->snum = sym->snum;
1280 break;
1284 r = r->next;
1288 /* Write out the object file. */
1290 static void macho_write (void)
1292 uint64_t offset = 0;
1294 /* mach-o object file structure:
1296 ** mach header
1297 ** uint32_t magic
1298 ** int cpu type
1299 ** int cpu subtype
1300 ** uint32_t mach file type
1301 ** uint32_t number of load commands
1302 ** uint32_t size of all load commands
1303 ** (includes section struct size of segment command)
1304 ** uint32_t flags
1306 ** segment command
1307 ** uint32_t command type == LC_SEGMENT[_64]
1308 ** uint32_t size of load command
1309 ** (including section load commands)
1310 ** char[16] segment name
1311 ** pointer in-memory offset
1312 ** pointer in-memory size
1313 ** pointer in-file offset to data area
1314 ** pointer in-file size
1315 ** (in-memory size excluding zerofill sections)
1316 ** int maximum vm protection
1317 ** int initial vm protection
1318 ** uint32_t number of sections
1319 ** uint32_t flags
1321 ** section commands
1322 ** char[16] section name
1323 ** char[16] segment name
1324 ** pointer in-memory offset
1325 ** pointer in-memory size
1326 ** uint32_t in-file offset
1327 ** uint32_t alignment
1328 ** (irrelevant in MH_OBJECT)
1329 ** uint32_t in-file offset of relocation entires
1330 ** uint32_t number of relocations
1331 ** uint32_t flags
1332 ** uint32_t reserved
1333 ** uint32_t reserved
1335 ** symbol table command
1336 ** uint32_t command type == LC_SYMTAB
1337 ** uint32_t size of load command
1338 ** uint32_t symbol table offset
1339 ** uint32_t number of symbol table entries
1340 ** uint32_t string table offset
1341 ** uint32_t string table size
1343 ** raw section data
1345 ** padding to pointer boundary
1347 ** relocation data (struct reloc)
1348 ** int32_t offset
1349 ** uint data (symbolnum, pcrel, length, extern, type)
1351 ** symbol table data (struct nlist)
1352 ** int32_t string table entry number
1353 ** uint8_t type
1354 ** (extern, absolute, defined in section)
1355 ** uint8_t section
1356 ** (0 for global symbols, section number of definition (>= 1, <=
1357 ** 254) for local symbols, size of variable for common symbols
1358 ** [type == extern])
1359 ** int16_t description
1360 ** (for stab debugging format)
1361 ** pointer value (i.e. file offset) of symbol or stab offset
1363 ** string table data
1364 ** list of null-terminated strings
1367 /* Emit the Mach-O header. */
1368 macho_write_header();
1370 offset = fmt->header_size + head_sizeofcmds;
1372 /* emit the segment load command */
1373 if (seg_nsects > 0)
1374 offset = macho_write_segment (offset);
1375 else
1376 nasm_error(ERR_WARNING, "no sections?");
1378 if (nsyms > 0) {
1379 /* write out symbol command */
1380 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1381 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1382 fwriteint32_t(offset, ofile); /* symbol table offset */
1383 fwriteint32_t(nsyms, ofile); /* number of symbol
1384 ** table entries */
1385 offset += nsyms * fmt->nlist_size;
1386 fwriteint32_t(offset, ofile); /* string table offset */
1387 fwriteint32_t(strslen, ofile); /* string table size */
1390 /* emit section data */
1391 if (seg_nsects > 0)
1392 macho_write_section ();
1394 /* emit symbol table if we have symbols */
1395 if (nsyms > 0)
1396 macho_write_symtab ();
1398 /* we don't need to pad here, we are already aligned */
1400 /* emit string table */
1401 saa_fpwrite(strs, ofile);
1403 /* We do quite a bit here, starting with finalizing all of the data
1404 for the object file, writing, and then freeing all of the data from
1405 the file. */
1407 static void macho_cleanup(int debuginfo)
1409 struct section *s;
1410 struct reloc *r;
1411 struct symbol *sym;
1413 (void)debuginfo;
1415 /* Sort all symbols. */
1416 macho_layout_symbols (&nsyms, &strslen);
1418 /* Fixup relocation entries */
1419 for (s = sects; s != NULL; s = s->next) {
1420 macho_fixup_relocs (s->relocs);
1423 /* First calculate and finalize needed values. */
1424 macho_calculate_sizes();
1425 macho_write();
1427 /* free up everything */
1428 while (sects->next) {
1429 s = sects;
1430 sects = sects->next;
1432 saa_free(s->data);
1433 while (s->relocs != NULL) {
1434 r = s->relocs;
1435 s->relocs = s->relocs->next;
1436 nasm_free(r);
1439 nasm_free(s);
1442 saa_free(strs);
1443 raa_free(extsyms);
1445 while (syms) {
1446 sym = syms;
1447 syms = syms->next;
1448 nasm_free (sym);
1451 nasm_free(extdefsyms);
1452 nasm_free(undefsyms);
1453 nasm_free(sectstab);
1456 /* Debugging routines. */
1457 static void debug_reloc (struct reloc *r)
1459 fprintf (stdout, "reloc:\n");
1460 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1461 fprintf (stdout, "\tsnum: %d\n", r->snum);
1462 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1463 fprintf (stdout, "\tlength: %d\n", r->length);
1464 fprintf (stdout, "\text: %d\n", r->ext);
1465 fprintf (stdout, "\ttype: %d\n", r->type);
1468 static void debug_section_relocs (struct section *s)
1470 struct reloc *r = s->relocs;
1472 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1474 while (r != NULL) {
1475 debug_reloc (r);
1476 r = r->next;
1480 #ifdef OF_MACHO32
1481 static const struct macho_fmt macho32_fmt = {
1483 MH_MAGIC,
1484 CPU_TYPE_I386,
1485 LC_SEGMENT,
1486 MACHO_HEADER_SIZE,
1487 MACHO_SEGCMD_SIZE,
1488 MACHO_SECTCMD_SIZE,
1489 MACHO_NLIST_SIZE
1492 static void macho32_init(void)
1494 fmt = &macho32_fmt;
1495 macho_init();
1497 macho_gotpcrel_sect = NO_SEG;
1500 struct ofmt of_macho32 = {
1501 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1502 "macho64",
1505 null_debug_arr,
1506 &null_debug_form,
1507 macho_stdmac,
1508 macho32_init,
1509 null_setinfo,
1510 macho_output,
1511 macho_symdef,
1512 macho_section,
1513 macho_sectalign,
1514 macho_segbase,
1515 null_directive,
1516 macho_filename,
1517 macho_cleanup
1519 #endif
1521 #ifdef OF_MACHO64
1522 static const struct macho_fmt macho64_fmt = {
1524 MH_MAGIC_64,
1525 CPU_TYPE_X86_64,
1526 LC_SEGMENT_64,
1527 MACHO_HEADER64_SIZE,
1528 MACHO_SEGCMD64_SIZE,
1529 MACHO_SECTCMD64_SIZE,
1530 MACHO_NLIST64_SIZE
1533 static void macho64_init(void)
1535 fmt = &macho64_fmt;
1536 macho_init();
1538 /* add special symbol for ..gotpcrel */
1539 macho_gotpcrel_sect = seg_alloc();
1540 macho_gotpcrel_sect++;
1541 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false);
1544 struct ofmt of_macho64 = {
1545 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1546 "macho64",
1549 null_debug_arr,
1550 &null_debug_form,
1551 macho_stdmac,
1552 macho64_init,
1553 null_setinfo,
1554 macho_output,
1555 macho_symdef,
1556 macho_section,
1557 macho_sectalign,
1558 macho_segbase,
1559 null_directive,
1560 macho_filename,
1561 macho_cleanup
1563 #endif
1565 #endif
1568 * Local Variables:
1569 * mode:c
1570 * c-basic-offset:4
1571 * End:
1573 * end of file */