Remove function pointers in output, simplify error handling
[nasm/sigaren-mirror.git] / output / outmacho64.c
blob3c38ddb691b2b0074ecb3c0c9d4a1c988f607e3f
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 * outmacho64.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_MACHO64)
59 /* Mach-O in-file header structure sizes */
60 #define MACHO_HEADER64_SIZE (32)
61 #define MACHO_SEGCMD64_SIZE (72)
62 #define MACHO_SECTCMD64_SIZE (80)
63 #define MACHO_SYMCMD_SIZE (24)
64 #define MACHO_NLIST64_SIZE (16)
65 #define MACHO_RELINFO64_SIZE (8)
67 /* Mach-O file header values */
68 #define MH_MAGIC_64 (0xfeedfacf)
69 #define CPU_TYPE_X86_64 (0x01000007) /* x86-64 platform */
70 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
71 #define MH_OBJECT (0x1) /* object file */
73 #define LC_SEGMENT_64 (0x19) /* segment load command */
74 #define LC_SYMTAB (0x2) /* symbol table load command */
76 #define VM_PROT_NONE (0x00)
77 #define VM_PROT_READ (0x01)
78 #define VM_PROT_WRITE (0x02)
79 #define VM_PROT_EXECUTE (0x04)
81 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
82 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
84 struct section {
85 /* nasm internal data */
86 struct section *next;
87 struct SAA *data;
88 int32_t index;
89 struct reloc *relocs;
90 int align;
92 /* data that goes into the file */
93 char sectname[16]; /* what this section is called */
94 char segname[16]; /* segment this section will be in */
95 uint64_t addr; /* in-memory address (subject to alignment) */
96 uint64_t size; /* in-memory and -file size */
97 uint32_t nreloc; /* relocation entry count */
98 uint32_t flags; /* type and attributes (masked) */
99 uint32_t extreloc; /* external relocations */
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 */
114 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
115 machine instructions */
117 static struct sectmap {
118 const char *nasmsect;
119 const char *segname;
120 const char *sectname;
121 const int32_t flags;
122 } sectmap[] = {
123 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
124 {".data", "__DATA", "__data", S_REGULAR},
125 {".rodata", "__DATA", "__const", S_REGULAR},
126 {".bss", "__DATA", "__bss", S_ZEROFILL},
127 {NULL, NULL, NULL, 0}
130 struct reloc {
131 /* nasm internal data */
132 struct reloc *next;
134 /* data that goes into the file */
135 int32_t addr; /* op's offset in section */
136 uint32_t snum:24, /* contains symbol index if
137 ** ext otherwise in-file
138 ** section number */
139 pcrel:1, /* relative relocation */
140 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
141 ext:1, /* external symbol referenced */
142 type:4; /* reloc type */
145 #define R_ABS 0 /* absolute relocation */
146 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
147 ** highest bit == 1 */
149 struct symbol {
150 /* nasm internal data */
151 struct symbol *next; /* next symbol in the list */
152 char *name; /* name of this symbol */
153 int32_t initial_snum; /* symbol number used above in
154 reloc */
155 int32_t snum; /* true snum for reloc */
157 /* data that goes into the file */
158 uint32_t strx; /* string table index */
159 uint8_t type; /* symbol type */
160 uint8_t sect; /* NO_SECT or section number */
161 uint16_t desc; /* for stab debugging, 0 for us */
162 uint64_t value; /* offset of symbol in section */
165 /* symbol type bits */
166 #define N_EXT 0x01 /* global or external symbol */
168 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
169 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
170 #define N_SECT 0xe /* defined symbol, n_sect holds
171 ** section number */
173 #define N_TYPE 0x0e /* type bit mask */
175 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
177 /* special section number values */
178 #define NO_SECT 0 /* no section, invalid */
179 #define MAX_SECT 255 /* maximum number of sections */
181 static struct section *sects, **sectstail;
182 static struct symbol *syms, **symstail;
183 static uint32_t nsyms;
185 /* These variables are set by macho_layout_symbols() to organize
186 the symbol table and string table in order the dynamic linker
187 expects. They are then used in macho_write() to put out the
188 symbols and strings in that order.
190 The order of the symbol table is:
191 local symbols
192 defined external symbols (sorted by name)
193 undefined external symbols (sorted by name)
195 The order of the string table is:
196 strings for external symbols
197 strings for local symbols
199 static uint32_t ilocalsym = 0;
200 static uint32_t iextdefsym = 0;
201 static uint32_t iundefsym = 0;
202 static uint32_t nlocalsym;
203 static uint32_t nextdefsym;
204 static uint32_t nundefsym;
205 static struct symbol **extdefsyms = NULL;
206 static struct symbol **undefsyms = NULL;
208 static struct RAA *extsyms;
209 static struct SAA *strs;
210 static uint32_t strslen;
212 extern struct ofmt of_macho64;
214 /* Global file information. This should be cleaned up into either
215 a structure or as function arguments. */
216 uint32_t head_ncmds64 = 0;
217 uint32_t head_sizeofcmds64 = 0;
218 uint64_t seg_filesize64 = 0;
219 uint64_t seg_vmsize64 = 0;
220 uint32_t seg_nsects64 = 0;
221 uint64_t rel_padcnt64 = 0;
224 #define xstrncpy(xdst, xsrc) \
225 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
226 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
227 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
229 #define align(x, y) \
230 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
232 #define alignint32_t(x) \
233 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
235 #define alignint64_t(x) \
236 align(x, sizeof(int64_t)) /* align x to int64_t boundary */
238 static void debug_reloc (struct reloc *);
239 static void debug_section_relocs (struct section *) _unused;
241 static int exact_log2 (uint32_t align)
243 if (align == 0) {
244 return 0;
245 } else if (align & (align-1)) {
246 return -1; /* Not a power of 2 */
247 } else {
248 #ifdef HAVE_GNUC_4
249 return __builtin_ctzl (align);
250 #else
251 uint32_t result = 0;
253 /* We know exactly one bit is set at this point. */
254 if (align & 0xffff0000)
255 result |= 16;
256 if (align & 0xff00ff00)
257 result |= 8;
258 if (align & 0xf0f0f0f0)
259 result |= 4;
260 if (align & 0xcccccccc)
261 result |= 2;
262 if (align & 0xaaaaaaaa)
263 result |= 1;
265 return result;
266 #endif
270 static struct section *get_section_by_name(const char *segname,
271 const char *sectname)
273 struct section *s;
275 for (s = sects; s != NULL; s = s->next)
276 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
277 break;
279 return s;
282 static struct section *get_section_by_index(const int32_t index)
284 struct section *s;
286 for (s = sects; s != NULL; s = s->next)
287 if (index == s->index)
288 break;
290 return s;
293 static int32_t get_section_index_by_name(const char *segname,
294 const char *sectname)
296 struct section *s;
298 for (s = sects; s != NULL; s = s->next)
299 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
300 return s->index;
302 return -1;
305 static char *get_section_name_by_index(const int32_t index)
307 struct section *s;
309 for (s = sects; s != NULL; s = s->next)
310 if (index == s->index)
311 return s->sectname;
313 return NULL;
316 static uint8_t get_section_fileindex_by_index(const int32_t index)
318 struct section *s;
319 uint8_t i = 1;
321 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
322 if (index == s->index)
323 return i;
325 if (i == MAX_SECT)
326 nasm_error(ERR_WARNING,
327 "too many sections (>255) - clipped by fileindex");
329 return NO_SECT;
332 static struct symbol *get_closest_section_symbol_by_offset(uint8_t fileindex, int64_t offset)
334 struct symbol *sym;
336 for (sym = syms; sym != NULL; sym = sym->next) {
337 if ((sym->sect != NO_SECT) &&
338 (sym->sect == fileindex) &&
339 ((int64_t)sym->value >= offset))
340 return sym;
343 return NULL;
348 * Special section numbers which are used to define Mach-O special
349 * symbols, which can be used with WRT to provide PIC relocation
350 * types.
352 static int32_t macho_gotpcrel_sect;
354 static void macho_init(void)
356 char zero = 0;
358 maxbits = 64;
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 - don't ask why */
374 saa_wbytes(strs, &zero, sizeof(char));
375 strslen = 1;
377 /* add special symbol for ..gotpcrel */
378 macho_gotpcrel_sect = seg_alloc();
379 macho_gotpcrel_sect ++;
380 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false, &of_macho64, nasm_error);
383 static void sect_write(struct section *sect,
384 const uint8_t *data, uint32_t len)
386 saa_wbytes(sect->data, data, len);
387 sect->size += len;
390 static int32_t add_reloc(struct section *sect, int32_t section,
391 int pcrel, int bytes, int64_t reloff)
393 struct reloc *r;
394 struct symbol *sym;
395 int32_t fi;
396 int32_t adjustment = 0;
398 /* NeXT as puts relocs in reversed order (address-wise) into the
399 ** files, so we do the same, doesn't seem to make much of a
400 ** difference either way */
401 r = nasm_malloc(sizeof(struct reloc));
402 r->next = sect->relocs;
403 sect->relocs = r;
405 /* the current end of the section will be the symbol's address for
406 ** now, might have to be fixed by macho_fixup_relocs() later on. make
407 ** sure we don't make the symbol scattered by setting the highest
408 ** bit by accident */
409 r->addr = sect->size & ~R_SCATTERED;
410 r->ext = 1;
411 r->pcrel = (pcrel ? 1 : 0);
413 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
414 switch(bytes){
415 case 1:
416 r->length = 0;
417 break;
418 case 2:
419 r->length = 1;
420 break;
421 case 4:
422 r->length = 2;
423 break;
424 case 8:
425 r->length = 3;
426 break;
427 default:
428 break;
431 /* set default relocation values */
432 r->type = 0; // X86_64_RELOC_UNSIGNED
433 r->snum = R_ABS; // Absolute Symbol (indicates no relocation)
435 /* absolute relocation */
436 if (pcrel == 0) {
438 /* intra-section */
439 if (section == NO_SEG) {
440 // r->snum = R_ABS; // Set above
442 /* inter-section */
443 } else {
444 fi = get_section_fileindex_by_index(section);
446 /* external */
447 if (fi == NO_SECT) {
448 r->snum = raa_read(extsyms, section);
450 /* local */
451 } else {
452 sym = get_closest_section_symbol_by_offset(fi, reloff);
453 r->snum = sym->initial_snum;
454 adjustment = sym->value;
458 /* relative relocation */
459 } else if (pcrel == 1) {
461 /* intra-section */
462 if (section == NO_SEG) {
463 r->type = 1; // X86_64_RELOC_SIGNED
465 /* inter-section */
466 } else {
467 r->type = 2; // X86_64_RELOC_BRANCH
468 fi = get_section_fileindex_by_index(section);
470 /* external */
471 if (fi == NO_SECT) {
472 sect->extreloc = 1;
473 r->snum = raa_read(extsyms, section);
475 /* local */
476 } else {
477 sym = get_closest_section_symbol_by_offset(fi, reloff);
478 r->snum = sym->initial_snum;
479 adjustment = sym->value;
483 /* subtractor */
484 } else if (pcrel == 2) {
485 r->pcrel = 0;
486 r->type = 5; // X86_64_RELOC_SUBTRACTOR
488 /* gotpcrel */
489 } else if (pcrel == 3) {
490 r->type = 4; // X86_64_RELOC_GOT
491 r->snum = macho_gotpcrel_sect;
493 /* gotpcrel MOVQ load */
494 } else if (pcrel == 4) {
495 r->type = 3; // X86_64_RELOC_GOT_LOAD
496 r->snum = macho_gotpcrel_sect;
499 ++sect->nreloc;
501 return adjustment;
504 static void macho_output(int32_t secto, const void *data,
505 enum out_type type, uint64_t size,
506 int32_t section, int32_t wrt)
508 struct section *s, *sbss;
509 int64_t addr;
510 uint8_t mydata[16], *p, gotload;
512 if (secto == NO_SEG) {
513 if (type != OUT_RESERVE)
514 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
515 "[ABSOLUTE] space");
517 return;
520 s = get_section_by_index(secto);
522 if (s == NULL) {
523 nasm_error(ERR_WARNING, "attempt to assemble code in"
524 " section %d: defaulting to `.text'", secto);
525 s = get_section_by_name("__TEXT", "__text");
527 /* should never happen */
528 if (s == NULL)
529 nasm_error(ERR_PANIC, "text section not found");
532 sbss = get_section_by_name("__DATA", "__bss");
534 if (s == sbss && type != OUT_RESERVE) {
535 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
536 " BSS section: ignored");
537 s->size += realsize(type, size);
538 return;
541 switch (type) {
542 case OUT_RESERVE:
543 if (s != sbss) {
544 nasm_error(ERR_WARNING, "uninitialized space declared in"
545 " %s section: zeroing",
546 get_section_name_by_index(secto));
548 sect_write(s, NULL, size);
549 } else
550 s->size += size;
552 break;
554 case OUT_RAWDATA:
555 if (section != NO_SEG)
556 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
558 sect_write(s, data, size);
559 break;
561 case OUT_ADDRESS:
562 addr = *(int64_t *)data;
563 if (section != NO_SEG) {
564 if (section % 2) {
565 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
566 " section base references");
567 } else {
568 if (wrt == NO_SEG) {
569 if (size < 8) {
570 nasm_error(ERR_NONFATAL, "Mach-O 64-bit format does not support"
571 " 32-bit absolute addresses");
573 Seemingly, Mach-O's X86_64_RELOC_SUBTRACTOR would require
574 pre-determined knowledge of where the image base would be,
575 making it impractical for use in intermediate object files
577 } else {
578 addr -= add_reloc(s, section, 0, size, addr); // X86_64_RELOC_UNSIGNED
580 } else {
581 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
582 " this use of WRT");
587 p = mydata;
588 WRITEADDR(p, addr, size);
589 sect_write(s, mydata, size);
590 break;
592 case OUT_REL2ADR:
593 p = mydata;
594 WRITESHORT(p, *(int64_t *)data);
596 if (section == secto)
597 nasm_error(ERR_PANIC, "intra-section OUT_REL2ADR");
599 if (section == NO_SEG) {
600 /* Do nothing */
601 } else if (section % 2) {
602 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
603 " section base references");
604 } else {
605 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
606 " Macho-O relocation [2]");
609 sect_write(s, mydata, 2L);
610 break;
612 case OUT_REL4ADR:
613 p = mydata;
614 WRITELONG(p, *(int64_t *)data);
616 if (section == secto)
617 nasm_error(ERR_PANIC, "intra-section OUT_REL4ADR");
619 if (section != NO_SEG && section % 2) {
620 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
621 " section base references");
622 } else {
623 if (wrt == NO_SEG) {
624 *mydata -= add_reloc(s, section, 1, 4, (int64_t)*mydata); // X86_64_RELOC_SIGNED/BRANCH
625 } else if (wrt == macho_gotpcrel_sect) {
626 if (s->data->datalen > 1) {
627 saa_fread(s->data, s->data->datalen-2, &gotload, 1); // Retrieve Instruction Opcode
628 } else {
629 gotload = 0;
631 if (gotload == 0x8B) { // Check for MOVQ Opcode
632 *mydata -= add_reloc(s, section, 4, 4, (int64_t)*mydata); // X86_64_GOT_LOAD (MOVQ load)
633 } else {
634 *mydata -= add_reloc(s, section, 3, 4, (int64_t)*mydata); // X86_64_GOT
636 } else {
637 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
638 " this use of WRT");
639 wrt = NO_SEG; /* we can at least _try_ to continue */
643 sect_write(s, mydata, 4L);
644 break;
646 default:
647 nasm_error(ERR_PANIC, "unknown output type?");
648 break;
652 static int32_t macho_section(char *name, int pass, int *bits)
654 int32_t index, originalIndex;
655 char *sectionAttributes;
656 struct sectmap *sm;
657 struct section *s;
659 (void)pass;
661 /* Default to 64 bits. */
662 if (!name) {
663 *bits = 64;
664 name = ".text";
665 sectionAttributes = NULL;
666 } else {
667 sectionAttributes = name;
668 name = nasm_strsep(&sectionAttributes, " \t");
671 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
672 /* make lookup into section name translation table */
673 if (!strcmp(name, sm->nasmsect)) {
674 char *currentAttribute;
676 /* try to find section with that name */
677 originalIndex = index = get_section_index_by_name(sm->segname,
678 sm->sectname);
680 /* create it if it doesn't exist yet */
681 if (index == -1) {
682 s = *sectstail = nasm_malloc(sizeof(struct section));
683 s->next = NULL;
684 sectstail = &s->next;
686 s->data = saa_init(1L);
687 s->index = seg_alloc();
688 s->relocs = NULL;
689 s->align = -1;
691 xstrncpy(s->segname, sm->segname);
692 xstrncpy(s->sectname, sm->sectname);
693 s->size = 0;
694 s->nreloc = 0;
695 s->flags = sm->flags;
697 index = s->index;
698 } else {
699 s = get_section_by_index(index);
702 while ((NULL != sectionAttributes)
703 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
704 if (0 != *currentAttribute) {
705 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
706 char *end;
707 int newAlignment, value;
709 value = strtoul(currentAttribute + 6, (char**)&end, 0);
710 newAlignment = exact_log2(value);
712 if (0 != *end) {
713 nasm_error(ERR_PANIC,
714 "unknown or missing alignment value \"%s\" "
715 "specified for section \"%s\"",
716 currentAttribute + 6,
717 name);
718 return NO_SEG;
719 } else if (0 > newAlignment) {
720 nasm_error(ERR_PANIC,
721 "alignment of %d (for section \"%s\") is not "
722 "a power of two",
723 value,
724 name);
725 return NO_SEG;
728 if ((-1 != originalIndex)
729 && (s->align != newAlignment)
730 && (s->align != -1)) {
731 nasm_error(ERR_PANIC,
732 "section \"%s\" has already been specified "
733 "with alignment %d, conflicts with new "
734 "alignment of %d",
735 name,
736 (1 << s->align),
737 value);
738 return NO_SEG;
741 s->align = newAlignment;
742 } else if (!nasm_stricmp("data", currentAttribute)) {
743 /* Do nothing; 'data' is implicit */
744 } else {
745 nasm_error(ERR_PANIC,
746 "unknown section attribute %s for section %s",
747 currentAttribute,
748 name);
749 return NO_SEG;
754 return index;
758 nasm_error(ERR_PANIC, "invalid section name %s", name);
759 return NO_SEG;
762 static void macho_symdef(char *name, int32_t section, int64_t offset,
763 int is_global, char *special)
765 struct symbol *sym;
767 if (special) {
768 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
769 "not support any special symbol types");
770 return;
773 if (is_global == 3) {
774 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
775 "(yet) support forward reference fixups.");
776 return;
779 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
781 * This is a NASM special symbol. We never allow it into
782 * the Macho-O symbol table, even if it's a valid one. If it
783 * _isn't_ a valid one, we should barf immediately.
785 if (strcmp(name, "..gotpcrel"))
786 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
787 return;
790 sym = *symstail = nasm_malloc(sizeof(struct symbol));
791 sym->next = NULL;
792 symstail = &sym->next;
794 sym->name = name;
795 sym->strx = strslen;
796 sym->type = 0;
797 sym->desc = 0;
798 sym->value = offset;
799 sym->initial_snum = -1;
801 /* external and common symbols get N_EXT */
802 if (is_global != 0) {
803 sym->type |= N_EXT;
806 if (section == NO_SEG) {
807 /* symbols in no section get absolute */
808 sym->type |= N_ABS;
809 sym->sect = NO_SECT;
810 } else {
811 sym->type |= N_SECT;
813 /* get the in-file index of the section the symbol was defined in */
814 sym->sect = get_section_fileindex_by_index(section);
816 /* track the initially allocated symbol number for use in future fix-ups */
817 sym->initial_snum = nsyms;
819 if (sym->sect == NO_SECT) {
821 /* remember symbol number of references to external
822 ** symbols, this works because every external symbol gets
823 ** its own section number allocated internally by nasm and
824 ** can so be used as a key */
825 extsyms = raa_write(extsyms, section, nsyms);
827 switch (is_global) {
828 case 1:
829 case 2:
830 /* there isn't actually a difference between global
831 ** and common symbols, both even have their size in
832 ** sym->value */
833 sym->type = N_EXT;
834 break;
836 default:
837 /* give an error on unfound section if it's not an
838 ** external or common symbol (assemble_file() does a
839 ** seg_alloc() on every call for them) */
840 nasm_error(ERR_PANIC, "in-file index for section %d not found",
841 section);
845 ++nsyms;
848 static int32_t macho_segbase(int32_t section)
850 return section;
853 static void macho_filename(char *inname, char *outname)
855 standard_extension(inname, outname, ".o");
858 extern macros_t macho_stdmac[];
860 /* Comparison function for qsort symbol layout. */
861 static int layout_compare (const struct symbol **s1,
862 const struct symbol **s2)
864 return (strcmp ((*s1)->name, (*s2)->name));
867 /* The native assembler does a few things in a similar function
869 * Remove temporary labels
870 * Sort symbols according to local, external, undefined (by name)
871 * Order the string table
873 We do not remove temporary labels right now.
875 numsyms is the total number of symbols we have. strtabsize is the
876 number entries in the string table. */
878 static void macho_layout_symbols (uint32_t *numsyms,
879 uint32_t *strtabsize)
881 struct symbol *sym, **symp;
882 uint32_t i,j;
884 *numsyms = 0;
885 *strtabsize = sizeof (char);
887 symp = &syms;
889 while ((sym = *symp)) {
890 /* Undefined symbols are now external. */
891 if (sym->type == N_UNDF)
892 sym->type |= N_EXT;
894 if ((sym->type & N_EXT) == 0) {
895 sym->snum = *numsyms;
896 *numsyms = *numsyms + 1;
897 nlocalsym++;
899 else {
900 if ((sym->type & N_TYPE) != N_UNDF) {
901 nextdefsym++;
902 } else {
903 nundefsym++;
906 /* If we handle debug info we'll want
907 to check for it here instead of just
908 adding the symbol to the string table. */
909 sym->strx = *strtabsize;
910 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
911 *strtabsize += strlen(sym->name) + 1;
913 symp = &(sym->next);
916 /* Next, sort the symbols. Most of this code is a direct translation from
917 the Apple cctools symbol layout. We need to keep compatibility with that. */
918 /* Set the indexes for symbol groups into the symbol table */
919 ilocalsym = 0;
920 iextdefsym = nlocalsym;
921 iundefsym = nlocalsym + nextdefsym;
923 /* allocate arrays for sorting externals by name */
924 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
925 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
927 i = 0;
928 j = 0;
930 symp = &syms;
932 while ((sym = *symp)) {
934 if((sym->type & N_EXT) == 0) {
935 sym->strx = *strtabsize;
936 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
937 *strtabsize += strlen(sym->name) + 1;
939 else {
940 if((sym->type & N_TYPE) != N_UNDF) {
941 extdefsyms[i++] = sym;
942 } else {
943 undefsyms[j++] = sym;
946 symp = &(sym->next);
949 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
950 (int (*)(const void *, const void *))layout_compare);
951 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
952 (int (*)(const void *, const void *))layout_compare);
954 for(i = 0; i < nextdefsym; i++) {
955 extdefsyms[i]->snum = *numsyms;
956 *numsyms += 1;
958 for(j = 0; j < nundefsym; j++) {
959 undefsyms[j]->snum = *numsyms;
960 *numsyms += 1;
964 /* Calculate some values we'll need for writing later. */
966 static void macho_calculate_sizes (void)
968 struct section *s;
970 /* count sections and calculate in-memory and in-file offsets */
971 for (s = sects; s != NULL; s = s->next) {
972 uint64_t pad = 0;
974 /* zerofill sections aren't actually written to the file */
975 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
976 seg_filesize64 += s->size;
978 /* recalculate segment address based on alignment and vm size */
979 s->addr = seg_vmsize64;
980 /* we need section alignment to calculate final section address */
981 if (s->align == -1)
982 s->align = DEFAULT_SECTION_ALIGNMENT;
983 if(s->align) {
984 uint64_t newaddr = align(s->addr, 1 << s->align);
985 pad = newaddr - s->addr;
986 s->addr = newaddr;
989 seg_vmsize64 += s->size + pad;
990 ++seg_nsects64;
993 /* calculate size of all headers, load commands and sections to
994 ** get a pointer to the start of all the raw data */
995 if (seg_nsects64 > 0) {
996 ++head_ncmds64;
997 head_sizeofcmds64 +=
998 MACHO_SEGCMD64_SIZE + seg_nsects64 * MACHO_SECTCMD64_SIZE;
1001 if (nsyms > 0) {
1002 ++head_ncmds64;
1003 head_sizeofcmds64 += MACHO_SYMCMD_SIZE;
1007 /* Write out the header information for the file. */
1009 static void macho_write_header (void)
1011 fwriteint32_t(MH_MAGIC_64, ofile); /* magic */
1012 fwriteint32_t(CPU_TYPE_X86_64, ofile); /* CPU type */
1013 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1014 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1015 fwriteint32_t(head_ncmds64, ofile); /* number of load commands */
1016 fwriteint32_t(head_sizeofcmds64, ofile); /* size of load commands */
1017 fwriteint32_t(0, ofile); /* no flags */
1018 fwriteint32_t(0, ofile); /* reserved for future use */
1021 /* Write out the segment load command at offset. */
1023 static uint32_t macho_write_segment (uint64_t offset)
1025 uint64_t rel_base = alignint64_t (offset + seg_filesize64);
1026 uint32_t s_reloff = 0;
1027 struct section *s;
1029 fwriteint32_t(LC_SEGMENT_64, ofile); /* cmd == LC_SEGMENT_64 */
1031 /* size of load command including section load commands */
1032 fwriteint32_t(MACHO_SEGCMD64_SIZE + seg_nsects64 *
1033 MACHO_SECTCMD64_SIZE, ofile);
1035 /* in an MH_OBJECT file all sections are in one unnamed (name
1036 ** all zeros) segment */
1037 fwritezero(16, ofile);
1038 fwriteint64_t(0, ofile); /* in-memory offset */
1039 fwriteint64_t(seg_vmsize64, ofile); /* in-memory size */
1040 fwriteint64_t(offset, ofile); /* in-file offset to data */
1041 fwriteint64_t(seg_filesize64, ofile); /* in-file size */
1042 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1043 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1044 fwriteint32_t(seg_nsects64, ofile); /* number of sections */
1045 fwriteint32_t(0, ofile); /* no flags */
1047 /* emit section headers */
1048 for (s = sects; s != NULL; s = s->next) {
1049 fwrite(s->sectname, sizeof(s->sectname), 1, ofile);
1050 fwrite(s->segname, sizeof(s->segname), 1, ofile);
1051 fwriteint64_t(s->addr, ofile);
1052 fwriteint64_t(s->size, ofile);
1054 /* dummy data for zerofill sections or proper values */
1055 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1056 fwriteint32_t(offset, ofile);
1057 /* Write out section alignment, as a power of two.
1058 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1059 if (s->align == -1)
1060 s->align = DEFAULT_SECTION_ALIGNMENT;
1061 fwriteint32_t(s->align, ofile);
1062 /* To be compatible with cctools as we emit
1063 a zero reloff if we have no relocations. */
1064 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1065 fwriteint32_t(s->nreloc, ofile);
1067 offset += s->size;
1068 s_reloff += s->nreloc * MACHO_RELINFO64_SIZE;
1069 } else {
1070 fwriteint32_t(0, ofile);
1071 fwriteint32_t(0, ofile);
1072 fwriteint32_t(0, ofile);
1073 fwriteint32_t(0, ofile);
1076 if (s->nreloc) {
1077 s->flags |= S_ATTR_LOC_RELOC;
1078 if (s->extreloc)
1079 s->flags |= S_ATTR_EXT_RELOC;
1082 fwriteint32_t(s->flags, ofile); /* flags */
1083 fwriteint32_t(0, ofile); /* reserved */
1084 fwriteint32_t(0, ofile); /* reserved */
1086 fwriteint32_t(0, ofile); /* align */
1089 rel_padcnt64 = rel_base - offset;
1090 offset = rel_base + s_reloff;
1092 return offset;
1095 /* For a given chain of relocs r, write out the entire relocation
1096 chain to the object file. */
1098 static void macho_write_relocs (struct reloc *r)
1100 while (r) {
1101 uint32_t word2;
1103 fwriteint32_t(r->addr, ofile); /* reloc offset */
1105 word2 = r->snum;
1106 word2 |= r->pcrel << 24;
1107 word2 |= r->length << 25;
1108 word2 |= r->ext << 27;
1109 word2 |= r->type << 28;
1110 fwriteint32_t(word2, ofile); /* reloc data */
1111 r = r->next;
1115 /* Write out the section data. */
1116 static void macho_write_section (void)
1118 struct section *s, *s2;
1119 struct reloc *r;
1120 uint8_t fi, *p, *q, blk[8];
1121 int32_t len;
1122 int64_t l;
1124 for (s = sects; s != NULL; s = s->next) {
1125 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1126 continue;
1128 /* no padding needs to be done to the sections */
1130 /* Like a.out Mach-O references things in the data or bss
1131 * sections by addresses which are actually relative to the
1132 * start of the _text_ section, in the _file_. See outaout.c
1133 * for more information. */
1134 saa_rewind(s->data);
1135 for (r = s->relocs; r != NULL; r = r->next) {
1136 len = (int32_t)r->length << 1;
1137 if(len > 4) len = 8;
1138 saa_fread(s->data, r->addr, blk, len);
1139 p = q = blk;
1140 l = *p++;
1142 /* get offset based on relocation type */
1143 if (r->length > 0) {
1144 l += ((int64_t)*p++) << 8;
1146 if (r->length > 1) {
1147 l += ((int64_t)*p++) << 16;
1148 l += ((int64_t)*p++) << 24;
1151 if (r->length > 2) {
1152 l += ((int64_t)*p++) << 32;
1153 l += ((int64_t)*p++) << 40;
1154 l += ((int64_t)*p++) << 48;
1155 l += ((int64_t)*p++) << 56;
1161 /* If the relocation is internal add to the current section
1162 offset. Otherwise the only value we need is the symbol
1163 offset which we already have. The linker takes care
1164 of the rest of the address. */
1165 if (!r->ext) {
1166 /* generate final address by section address and offset */
1167 for (s2 = sects, fi = 1;
1168 s2 != NULL; s2 = s2->next, fi++) {
1169 if (fi == r->snum) {
1170 l += s2->addr;
1171 break;
1176 /* write new offset back */
1177 if (r->length == 3)
1178 WRITEDLONG(q, l);
1179 else if (r->length == 2)
1180 WRITELONG(q, l);
1181 else if (r->length == 1)
1182 WRITESHORT(q, l);
1183 else
1184 *q++ = l & 0xFF;
1186 saa_fwrite(s->data, r->addr, blk, len);
1189 /* dump the section data to file */
1190 saa_fpwrite(s->data, ofile);
1193 /* pad last section up to reloc entries on int64_t boundary */
1194 fwritezero(rel_padcnt64, ofile);
1196 /* emit relocation entries */
1197 for (s = sects; s != NULL; s = s->next)
1198 macho_write_relocs (s->relocs);
1201 /* Write out the symbol table. We should already have sorted this
1202 before now. */
1203 static void macho_write_symtab (void)
1205 struct symbol *sym;
1206 struct section *s;
1207 int64_t fi;
1208 uint64_t i;
1210 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1212 for (sym = syms; sym != NULL; sym = sym->next) {
1213 if ((sym->type & N_EXT) == 0) {
1214 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1215 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1216 fwrite(&sym->sect, 1, 1, ofile); /* section */
1217 fwriteint16_t(sym->desc, ofile); /* description */
1219 /* Fix up the symbol value now that we know the final section
1220 sizes. */
1221 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1222 for (s = sects, fi = 1;
1223 s != NULL && fi < sym->sect; s = s->next, ++fi)
1224 sym->value += s->size;
1227 fwriteint64_t(sym->value, ofile); /* value (i.e. offset) */
1231 for (i = 0; i < nextdefsym; i++) {
1232 sym = extdefsyms[i];
1233 fwriteint32_t(sym->strx, ofile);
1234 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1235 fwrite(&sym->sect, 1, 1, ofile); /* section */
1236 fwriteint16_t(sym->desc, ofile); /* description */
1238 /* Fix up the symbol value now that we know the final section
1239 sizes. */
1240 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1241 for (s = sects, fi = 1;
1242 s != NULL && fi < sym->sect; s = s->next, ++fi)
1243 sym->value += s->size;
1246 fwriteint64_t(sym->value, ofile); /* value (i.e. offset) */
1249 for (i = 0; i < nundefsym; i++) {
1250 sym = undefsyms[i];
1251 fwriteint32_t(sym->strx, ofile);
1252 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1253 fwrite(&sym->sect, 1, 1, ofile); /* section */
1254 fwriteint16_t(sym->desc, ofile); /* description */
1256 // Fix up the symbol value now that we know the final section sizes.
1257 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1258 for (s = sects, fi = 1;
1259 s != NULL && fi < sym->sect; s = s->next, ++fi)
1260 sym->value += s->size;
1263 fwriteint64_t(sym->value, ofile); // value (i.e. offset)
1268 /* Fixup the snum in the relocation entries, we should be
1269 doing this only for externally referenced symbols. */
1270 static void macho_fixup_relocs (struct reloc *r)
1272 struct symbol *sym;
1274 while (r != NULL) {
1275 if (r->ext) {
1276 for (sym = syms; sym != NULL; sym = sym->next) {
1277 if (sym->initial_snum == r->snum) {
1278 r->snum = sym->snum;
1279 break;
1283 r = r->next;
1287 /* Write out the object file. */
1289 static void macho_write (void)
1291 uint64_t offset = 0;
1293 /* mach-o object file structure:
1295 ** mach header
1296 ** uint32_t magic
1297 ** int cpu type
1298 ** int cpu subtype
1299 ** uint32_t mach file type
1300 ** uint32_t number of load commands
1301 ** uint32_t size of all load commands
1302 ** (includes section struct size of segment command)
1303 ** uint32_t flags
1305 ** segment command
1306 ** uint32_t command type == LC_SEGMENT_64
1307 ** uint32_t size of load command
1308 ** (including section load commands)
1309 ** char[16] segment name
1310 ** uint64_t in-memory offset
1311 ** uint64_t in-memory size
1312 ** uint64_t in-file offset to data area
1313 ** uint64_t in-file size
1314 ** (in-memory size excluding zerofill sections)
1315 ** int maximum vm protection
1316 ** int initial vm protection
1317 ** uint32_t number of sections
1318 ** uint32_t flags
1320 ** section commands
1321 ** char[16] section name
1322 ** char[16] segment name
1323 ** uint64_t in-memory offset
1324 ** uint64_t in-memory size
1325 ** uint32_t in-file offset
1326 ** uint32_t alignment
1327 ** (irrelevant in MH_OBJECT)
1328 ** uint32_t in-file offset of relocation entires
1329 ** uint32_t number of relocations
1330 ** uint32_t flags
1331 ** uint32_t reserved
1332 ** uint32_t reserved
1334 ** symbol table command
1335 ** uint32_t command type == LC_SYMTAB
1336 ** uint32_t size of load command
1337 ** uint32_t symbol table offset
1338 ** uint32_t number of symbol table entries
1339 ** uint32_t string table offset
1340 ** uint32_t string table size
1342 ** raw section data
1344 ** padding to int64_t boundary
1346 ** relocation data (struct reloc)
1347 ** int32_t offset
1348 ** uint data (symbolnum, pcrel, length, extern, type)
1350 ** symbol table data (struct nlist)
1351 ** int32_t string table entry number
1352 ** uint8_t type
1353 ** (extern, absolute, defined in section)
1354 ** uint8_t section
1355 ** (0 for global symbols, section number of definition (>= 1, <=
1356 ** 254) for local symbols, size of variable for common symbols
1357 ** [type == extern])
1358 ** int16_t description
1359 ** (for stab debugging format)
1360 ** uint64_t value (i.e. file offset) of symbol or stab offset
1362 ** string table data
1363 ** list of null-terminated strings
1366 /* Emit the Mach-O header. */
1367 macho_write_header();
1369 offset = MACHO_HEADER64_SIZE + head_sizeofcmds64;
1371 /* emit the segment load command */
1372 if (seg_nsects64 > 0)
1373 offset = macho_write_segment (offset);
1374 else
1375 nasm_error(ERR_WARNING, "no sections?");
1377 if (nsyms > 0) {
1378 /* write out symbol command */
1379 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1380 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1381 fwriteint32_t(offset, ofile); /* symbol table offset */
1382 fwriteint32_t(nsyms, ofile); /* number of symbol
1383 ** table entries */
1385 offset += nsyms * MACHO_NLIST64_SIZE;
1386 fwriteint32_t(offset, ofile); /* string table offset */
1387 fwriteint32_t(strslen, ofile); /* string table size */
1390 /* emit section data */
1391 if (seg_nsects64 > 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 since MACHO_NLIST64_SIZE == 16 */
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 if (syms) {
1446 while (syms->next) {
1447 sym = syms;
1448 syms = syms->next;
1450 nasm_free (sym);
1455 /* Debugging routines. */
1456 static void debug_reloc (struct reloc *r)
1458 fprintf (stdout, "reloc:\n");
1459 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1460 fprintf (stdout, "\tsnum: %d\n", r->snum);
1461 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1462 fprintf (stdout, "\tlength: %d\n", r->length);
1463 fprintf (stdout, "\text: %d\n", r->ext);
1464 fprintf (stdout, "\ttype: %d\n", r->type);
1467 static void debug_section_relocs (struct section *s)
1469 struct reloc *r = s->relocs;
1471 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1473 while (r != NULL) {
1474 debug_reloc (r);
1475 r = r->next;
1479 struct ofmt of_macho64 = {
1480 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1481 "macho64",
1483 null_debug_arr,
1484 &null_debug_form,
1485 macho_stdmac,
1486 macho_init,
1487 null_setinfo,
1488 macho_output,
1489 macho_symdef,
1490 macho_section,
1491 macho_segbase,
1492 null_directive,
1493 macho_filename,
1494 macho_cleanup
1497 #endif
1500 * Local Variables:
1501 * mode:c
1502 * c-basic-offset:4
1503 * End:
1505 * end of file */