modified: output/outmacho64.c
[nasm.git] / output / outmacho64.c
blob3624a39a63ab24684fd0e14787f35f033663617b
1 /* outmacho64.c output routines for the Netwide Assembler to produce
2 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 /* Most of this file is, like Mach-O itself, based on a.out. For more
11 * guidelines see outaout.c. */
13 #include "compiler.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <inttypes.h>
21 #include "nasm.h"
22 #include "nasmlib.h"
23 #include "saa.h"
24 #include "raa.h"
25 #include "outform.h"
26 #include "compiler.h"
28 #ifdef OF_MACHO64
30 /* Mach-O in-file header structure sizes */
31 #define MACHO_HEADER64_SIZE (32)
32 #define MACHO_SEGCMD64_SIZE (72)
33 #define MACHO_SECTCMD64_SIZE (80)
34 #define MACHO_SYMCMD_SIZE (24)
35 #define MACHO_NLIST64_SIZE (16)
36 #define MACHO_RELINFO64_SIZE (8)
38 /* Mach-O file header values */
39 #define MH_MAGIC_64 (0xfeedfacf)
40 #define CPU_TYPE_X86_64 (0x01000007) /* x86-64 platform */
41 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
42 #define MH_OBJECT (0x1) /* object file */
44 #define LC_SEGMENT_64 (0x19) /* segment load command */
45 #define LC_SYMTAB (0x2) /* symbol table load command */
47 #define VM_PROT_NONE (0x00)
48 #define VM_PROT_READ (0x01)
49 #define VM_PROT_WRITE (0x02)
50 #define VM_PROT_EXECUTE (0x04)
52 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
53 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
55 struct section {
56 /* nasm internal data */
57 struct section *next;
58 struct SAA *data;
59 int32_t index;
60 struct reloc *relocs;
61 int align;
63 /* data that goes into the file */
64 char sectname[16]; /* what this section is called */
65 char segname[16]; /* segment this section will be in */
66 uint64_t size; /* in-memory and -file size */
67 uint32_t nreloc; /* relocation entry count */
68 uint32_t flags; /* type and attributes (masked) */
69 uint32_t extreloc; /* external relocations */
72 #define SECTION_TYPE 0x000000ff /* section type mask */
74 #define S_REGULAR (0x0) /* standard section */
75 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
77 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
78 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
79 machine instructions */
80 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
81 relocation entries */
82 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
83 relocation entries */
84 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
85 machine instructions */
87 static struct sectmap {
88 const char *nasmsect;
89 const char *segname;
90 const char *sectname;
91 const int32_t flags;
92 } sectmap[] = {
93 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
94 {".data", "__DATA", "__data", S_REGULAR},
95 {".rodata", "__DATA", "__const", S_REGULAR},
96 {".bss", "__DATA", "__bss", S_ZEROFILL},
97 {NULL, NULL, NULL, 0}
100 struct reloc {
101 /* nasm internal data */
102 struct reloc *next;
104 /* data that goes into the file */
105 int32_t addr; /* op's offset in section */
106 uint32_t snum:24, /* contains symbol index if
107 ** ext otherwise in-file
108 ** section number */
109 pcrel:1, /* relative relocation */
110 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
111 ext:1, /* external symbol referenced */
112 type:4; /* reloc type */
115 #define R_ABS 0 /* absolute relocation */
116 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
117 ** highest bit == 1 */
119 struct symbol {
120 /* nasm internal data */
121 struct symbol *next; /* next symbol in the list */
122 char *name; /* name of this symbol */
123 int32_t initial_snum; /* symbol number used above in
124 reloc */
125 int32_t snum; /* true snum for reloc */
127 /* data that goes into the file */
128 uint32_t strx; /* string table index */
129 uint8_t type; /* symbol type */
130 uint8_t sect; /* NO_SECT or section number */
131 int16_t desc; /* for stab debugging, 0 for us */
132 uint64_t value; /* offset of symbol in section */
135 /* symbol type bits */
136 #define N_EXT 0x01 /* global or external symbol */
138 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
139 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
140 #define N_SECT 0xe /* defined symbol, n_sect holds
141 ** section number */
143 #define N_TYPE 0x0e /* type bit mask */
145 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
147 /* special section number values */
148 #define NO_SECT 0 /* no section, invalid */
149 #define MAX_SECT 255 /* maximum number of sections */
151 static struct section *sects, **sectstail;
152 static struct symbol *syms, **symstail;
153 static uint32_t nsyms;
155 /* These variables are set by macho_layout_symbols() to organize
156 the symbol table and string table in order the dynamic linker
157 expects. They are then used in macho_write() to put out the
158 symbols and strings in that order.
160 The order of the symbol table is:
161 local symbols
162 defined external symbols (sorted by name)
163 undefined external symbols (sorted by name)
165 The order of the string table is:
166 strings for external symbols
167 strings for local symbols
169 static uint32_t ilocalsym = 0;
170 static uint32_t iextdefsym = 0;
171 static uint32_t iundefsym = 0;
172 static uint32_t nlocalsym;
173 static uint32_t nextdefsym;
174 static uint32_t nundefsym;
175 static struct symbol **extdefsyms = NULL;
176 static struct symbol **undefsyms = NULL;
178 static struct RAA *extsyms;
179 static struct SAA *strs;
180 static uint32_t strslen;
182 static FILE *machofp;
183 static efunc error;
184 static evalfunc evaluate;
186 extern struct ofmt of_macho64;
188 /* Global file information. This should be cleaned up into either
189 a structure or as function arguments. */
190 uint32_t head_ncmds64 = 0;
191 uint32_t head_sizeofcmds64 = 0;
192 uint64_t seg_filesize64 = 0;
193 uint64_t seg_vmsize64 = 0;
194 uint32_t seg_nsects64 = 0;
195 uint64_t rel_padcnt64 = 0;
198 #define xstrncpy(xdst, xsrc) \
199 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
200 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
201 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
203 #define align(x, y) \
204 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
206 #define alignint32_t(x) \
207 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
209 #define alignint64_t(x) \
210 align(x, sizeof(int64_t)) /* align x to int64_t boundary */
212 static void debug_reloc (struct reloc *);
213 static void debug_section_relocs (struct section *) _unused;
215 static int exact_log2 (uint32_t align)
217 if (align == 0) {
218 return 0;
219 } else if (align & (align-1)) {
220 return -1; /* Not a power of 2 */
221 } else {
222 #ifdef HAVE_GNUC_4
223 return __builtin_ctzl (align);
224 #else
225 uint32_t result = 0;
227 /* We know exactly one bit is set at this point. */
228 if (align & 0xffff0000)
229 result |= 16;
230 if (align & 0xff00ff00)
231 result |= 8;
232 if (align & 0xf0f0f0f0)
233 result |= 4;
234 if (align & 0xcccccccc)
235 result |= 2;
236 if (align & 0xaaaaaaaa)
237 result |= 1;
239 return result;
240 #endif
244 static struct section *get_section_by_name(const char *segname,
245 const char *sectname)
247 struct section *s;
249 for (s = sects; s != NULL; s = s->next)
250 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
251 break;
253 return s;
256 static struct section *get_section_by_index(const int32_t index)
258 struct section *s;
260 for (s = sects; s != NULL; s = s->next)
261 if (index == s->index)
262 break;
264 return s;
267 static int32_t get_section_index_by_name(const char *segname,
268 const char *sectname)
270 struct section *s;
272 for (s = sects; s != NULL; s = s->next)
273 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
274 return s->index;
276 return -1;
279 static char *get_section_name_by_index(const int32_t index)
281 struct section *s;
283 for (s = sects; s != NULL; s = s->next)
284 if (index == s->index)
285 return s->sectname;
287 return NULL;
290 static uint8_t get_section_fileindex_by_index(const int32_t index)
292 struct section *s;
293 uint8_t i = 1;
295 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
296 if (index == s->index)
297 return i;
299 if (i == MAX_SECT)
300 error(ERR_WARNING,
301 "too many sections (>255) - clipped by fileindex");
303 return NO_SECT;
307 * Special section numbers which are used to define Mach-O special
308 * symbols, which can be used with WRT to provide PIC relocation
309 * types.
311 static int32_t macho_gotpcrel_sect;
313 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
314 evalfunc eval)
316 char zero = 0;
318 maxbits = 64;
319 machofp = fp;
320 error = errfunc;
321 evaluate = eval;
323 (void)ldef; /* placate optimizers */
325 sects = NULL;
326 sectstail = &sects;
328 syms = NULL;
329 symstail = &syms;
330 nsyms = 0;
331 nlocalsym = 0;
332 nextdefsym = 0;
333 nundefsym = 0;
335 extsyms = raa_init();
336 strs = saa_init(1L);
338 /* string table starts with a zero byte - don't ask why */
339 saa_wbytes(strs, &zero, sizeof(char));
340 strslen = 1;
342 /* add special symbol for ..gotpcrel */
343 macho_gotpcrel_sect = seg_alloc();
344 macho_gotpcrel_sect ++;
345 ldef("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false, &of_macho64, error);
348 static int macho_setinfo(enum geninfo type, char **val)
350 (void)type;
351 (void)val;
352 return 0;
355 static void sect_write(struct section *sect,
356 const uint8_t *data, uint32_t len)
358 saa_wbytes(sect->data, data, len);
359 sect->size += len;
362 static void add_reloc(struct section *sect, int32_t section,
363 int pcrel, int bytes)
365 struct reloc *r;
366 int32_t fi;
368 /* NeXT as puts relocs in reversed order (address-wise) into the
369 ** files, so we do the same, doesn't seem to make much of a
370 ** difference either way */
371 r = nasm_malloc(sizeof(struct reloc));
372 r->next = sect->relocs;
373 sect->relocs = r;
375 /* the current end of the section will be the symbol's address for
376 ** now, might have to be fixed by macho_fixup_relocs() later on. make
377 ** sure we don't make the symbol scattered by setting the highest
378 ** bit by accident */
379 r->addr = sect->size & ~R_SCATTERED;
380 r->ext = 1;
381 r->pcrel = (pcrel ? 1 : 0);
383 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
384 switch(bytes){
385 case 1:
386 r->length = 0;
387 break;
388 case 2:
389 r->length = 1;
390 break;
391 case 4:
392 r->length = 2;
393 break;
394 case 8:
395 r->length = 3;
396 break;
397 default:
398 break;
401 /* absolute relocation */
402 r->type = 0; // X86_64_RELOC_UNSIGNED
403 r->snum = R_ABS; // Absolute Symbol (indicates no relocation)
405 /* relative relocation */
406 if (pcrel == 1) {
408 // r->type = 2; // X86_64_RELOC_BRANCH
410 /* intra-section */
411 if (section == NO_SEG) {
412 r->type = 1; // X86_64_RELOC_SIGNED
414 /* inter-section */
415 } else {
416 fi = get_section_fileindex_by_index(section);
418 /* external */
419 if (fi == NO_SECT) {
420 sect->extreloc = 1;
422 r->pcrel = 0; // presumed X86_64_RELOC_UNSIGNED ???
423 // r->snum = raa_read(extsyms, section);
425 /* local */
426 } else {
428 r->type = 2; // X86_64_RELOC_BRANCH
429 r->snum = fi;
433 /* subtractor */
434 } else if (pcrel == 2) {
435 r->pcrel = 0;
436 r->type = 5; // X86_64_RELOC_SUBTRACTOR
437 // r->snum = macho_gotpcrel_sect;
439 /* gotpcrel */
440 } else if (pcrel == 3) {
441 r->type = 4; // X86_64_RELOC_GOT
443 ++sect->nreloc;
446 static void macho_output(int32_t secto, const void *data,
447 enum out_type type, uint64_t size,
448 int32_t section, int32_t wrt)
450 struct section *s, *sbss;
451 int64_t addr;
452 uint8_t mydata[16], *p;
454 if (secto == NO_SEG) {
455 if (type != OUT_RESERVE)
456 error(ERR_NONFATAL, "attempt to assemble code in "
457 "[ABSOLUTE] space");
459 return;
462 s = get_section_by_index(secto);
464 if (s == NULL) {
465 error(ERR_WARNING, "attempt to assemble code in"
466 " section %d: defaulting to `.text'", secto);
467 s = get_section_by_name("__TEXT", "__text");
469 /* should never happen */
470 if (s == NULL)
471 error(ERR_PANIC, "text section not found");
474 sbss = get_section_by_name("__DATA", "__bss");
476 if (s == sbss && type != OUT_RESERVE) {
477 error(ERR_WARNING, "attempt to initialize memory in the"
478 " BSS section: ignored");
480 switch (type) {
481 case OUT_REL2ADR:
482 size = 2;
483 break;
485 case OUT_REL4ADR:
486 size = 4;
487 break;
489 default:
490 break;
493 s->size += size;
494 return;
497 switch (type) {
498 case OUT_RESERVE:
499 if (s != sbss) {
500 error(ERR_WARNING, "uninitialized space declared in"
501 " %s section: zeroing",
502 get_section_name_by_index(secto));
504 sect_write(s, NULL, size);
505 } else
506 s->size += size;
508 break;
510 case OUT_RAWDATA:
511 if (section != NO_SEG)
512 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
514 sect_write(s, data, size);
515 break;
517 case OUT_ADDRESS:
518 addr = *(int64_t *)data;
520 if (section != NO_SEG) {
521 if (section % 2) {
522 error(ERR_NONFATAL, "Mach-O format does not support"
523 " section base references");
524 } else {
525 if (wrt == NO_SEG) {
526 if (size < 8) {
527 add_reloc(s, section, 2, size);
529 add_reloc(s, section, 0, size);
530 } else if (wrt == macho_gotpcrel_sect) {
531 addr += s->size;
532 add_reloc(s, section, 3, size);
533 } else {
534 error(ERR_NONFATAL, "Mach-O format does not support"
535 " this use of WRT");
540 p = mydata;
541 WRITEADDR(p, addr, size);
542 sect_write(s, mydata, size);
543 break;
545 case OUT_REL2ADR:
546 if (section == secto)
547 error(ERR_PANIC, "intra-section OUT_REL2ADR");
549 if (section != NO_SEG && section % 2) {
550 error(ERR_NONFATAL, "Mach-O format does not support"
551 " section base references");
552 } else {
553 if (wrt == NO_SEG) {
554 add_reloc(s, section, 2, 2);
555 add_reloc(s, section, 0, 2);
556 } else {
557 error(ERR_NONFATAL, "Unsupported non-32-bit"
558 " Macho-O relocation [2]");
562 p = mydata;
563 WRITESHORT(p, *(int64_t *)data - size);
564 sect_write(s, mydata, 2L);
565 break;
567 case OUT_REL4ADR:
568 if (section == secto)
569 error(ERR_PANIC, "intra-section OUT_REL4ADR");
571 if (section != NO_SEG && section % 2) {
572 error(ERR_NONFATAL, "Mach-O format does not support"
573 " section base references");
574 } else {
575 if (wrt == NO_SEG) {
577 //add_reloc(s, section, 2, 4);
578 add_reloc(s, section, 1, 4);
579 } else if (wrt == macho_gotpcrel_sect) {
580 error(ERR_NONFATAL, "Mach-O format cannot produce PC-"
581 "relative GOT references");
582 } else {
583 error(ERR_NONFATAL, "Mach-O format does not support"
584 " this use of WRT");
585 wrt = NO_SEG; /* we can at least _try_ to continue */
589 p = mydata;
590 WRITELONG(p, *(int64_t *)data - size);
591 sect_write(s, mydata, 4L);
592 break;
594 default:
595 error(ERR_PANIC, "unknown output type?");
596 break;
600 static int32_t macho_section(char *name, int pass, int *bits)
602 int32_t index, originalIndex;
603 char *sectionAttributes;
604 struct sectmap *sm;
605 struct section *s;
607 (void)pass;
609 /* Default to 64 bits. */
610 if (!name) {
611 *bits = 64;
612 name = ".text";
613 sectionAttributes = NULL;
614 } else {
615 sectionAttributes = name;
616 name = nasm_strsep(&sectionAttributes, " \t");
619 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
620 /* make lookup into section name translation table */
621 if (!strcmp(name, sm->nasmsect)) {
622 char *currentAttribute;
624 /* try to find section with that name */
625 originalIndex = index = get_section_index_by_name(sm->segname,
626 sm->sectname);
628 /* create it if it doesn't exist yet */
629 if (index == -1) {
630 s = *sectstail = nasm_malloc(sizeof(struct section));
631 s->next = NULL;
632 sectstail = &s->next;
634 s->data = saa_init(1L);
635 s->index = seg_alloc();
636 s->relocs = NULL;
637 s->align = -1;
639 xstrncpy(s->segname, sm->segname);
640 xstrncpy(s->sectname, sm->sectname);
641 s->size = 0;
642 s->nreloc = 0;
643 s->flags = sm->flags;
645 index = s->index;
646 } else {
647 s = get_section_by_index(index);
650 while ((NULL != sectionAttributes)
651 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
652 if (0 != *currentAttribute) {
653 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
654 char *end;
655 int newAlignment, value;
657 value = strtoul(currentAttribute + 6, (char**)&end, 0);
658 newAlignment = exact_log2(value);
660 if (0 != *end) {
661 error(ERR_PANIC,
662 "unknown or missing alignment value \"%s\" "
663 "specified for section \"%s\"",
664 currentAttribute + 6,
665 name);
666 return NO_SEG;
667 } else if (0 > newAlignment) {
668 error(ERR_PANIC,
669 "alignment of %d (for section \"%s\") is not "
670 "a power of two",
671 value,
672 name);
673 return NO_SEG;
676 if ((-1 != originalIndex)
677 && (s->align != newAlignment)
678 && (s->align != -1)) {
679 error(ERR_PANIC,
680 "section \"%s\" has already been specified "
681 "with alignment %d, conflicts with new "
682 "alignment of %d",
683 name,
684 (1 << s->align),
685 value);
686 return NO_SEG;
689 s->align = newAlignment;
690 } else if (!nasm_stricmp("data", currentAttribute)) {
691 /* Do nothing; 'data' is implicit */
692 } else {
693 error(ERR_PANIC,
694 "unknown section attribute %s for section %s",
695 currentAttribute,
696 name);
697 return NO_SEG;
702 return index;
706 error(ERR_PANIC, "invalid section name %s", name);
707 return NO_SEG;
710 static void macho_symdef(char *name, int32_t section, int64_t offset,
711 int is_global, char *special)
713 struct symbol *sym;
715 if (special) {
716 error(ERR_NONFATAL, "The Mach-O output format does "
717 "not support any special symbol types");
718 return;
721 if (is_global == 3) {
722 error(ERR_NONFATAL, "The Mach-O format does not "
723 "(yet) support forward reference fixups.");
724 return;
727 sym = *symstail = nasm_malloc(sizeof(struct symbol));
728 sym->next = NULL;
729 symstail = &sym->next;
731 sym->name = name;
732 sym->strx = strslen;
733 sym->type = 0;
734 sym->desc = 0;
735 sym->value = offset;
736 sym->initial_snum = -1;
738 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
740 * This is a NASM special symbol. We never allow it into
741 * the Macho-O symbol table, even if it's a valid one. If it
742 * _isn't_ a valid one, we should barf immediately.
744 if (strcmp(name, "..gotpcrel"))
745 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
746 return;
749 /* external and common symbols get N_EXT */
750 if (is_global != 0) {
751 sym->type |= N_EXT;
754 if (section == NO_SEG) {
755 /* symbols in no section get absolute */
756 sym->type |= N_ABS;
757 sym->sect = NO_SECT;
758 } else {
759 sym->type |= N_SECT;
761 /* get the in-file index of the section the symbol was defined in */
762 sym->sect = get_section_fileindex_by_index(section);
764 if (sym->sect == NO_SECT) {
765 /* remember symbol number of references to external
766 ** symbols, this works because every external symbol gets
767 ** its own section number allocated internally by nasm and
768 ** can so be used as a key */
769 extsyms = raa_write(extsyms, section, nsyms);
770 sym->initial_snum = nsyms;
772 switch (is_global) {
773 case 1:
774 case 2:
775 /* there isn't actually a difference between global
776 ** and common symbols, both even have their size in
777 ** sym->value */
778 sym->type = N_EXT;
779 break;
781 default:
782 /* give an error on unfound section if it's not an
783 ** external or common symbol (assemble_file() does a
784 ** seg_alloc() on every call for them) */
785 error(ERR_PANIC, "in-file index for section %d not found",
786 section);
790 ++nsyms;
793 static int32_t macho_segbase(int32_t section)
795 return section;
798 static int macho_directive(char *directive, char *value, int pass)
800 (void)directive;
801 (void)value;
802 (void)pass;
803 return 0;
806 static void macho_filename(char *inname, char *outname, efunc error)
808 standard_extension(inname, outname, ".o", error);
811 extern macros_t generic_stdmac[];
813 /* Comparison function for qsort symbol layout. */
814 static int layout_compare (const struct symbol **s1,
815 const struct symbol **s2)
817 return (strcmp ((*s1)->name, (*s2)->name));
820 /* The native assembler does a few things in a similar function
822 * Remove temporary labels
823 * Sort symbols according to local, external, undefined (by name)
824 * Order the string table
826 We do not remove temporary labels right now.
828 numsyms is the total number of symbols we have. strtabsize is the
829 number entries in the string table. */
831 static void macho_layout_symbols (uint32_t *numsyms,
832 uint32_t *strtabsize)
834 struct symbol *sym, **symp;
835 uint32_t i,j;
837 *numsyms = 0;
838 *strtabsize = sizeof (char);
840 symp = &syms;
842 while ((sym = *symp)) {
843 /* Undefined symbols are now external. */
844 if (sym->type == N_UNDF)
845 sym->type |= N_EXT;
847 if ((sym->type & N_EXT) == 0) {
848 sym->snum = *numsyms;
849 *numsyms = *numsyms + 1;
850 nlocalsym++;
852 else {
853 if ((sym->type & N_TYPE) != N_UNDF)
854 nextdefsym++;
855 else
856 nundefsym++;
858 /* If we handle debug info we'll want
859 to check for it here instead of just
860 adding the symbol to the string table. */
861 sym->strx = *strtabsize;
862 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
863 *strtabsize += strlen(sym->name) + 1;
865 symp = &(sym->next);
868 /* Next, sort the symbols. Most of this code is a direct translation from
869 the Apple cctools symbol layout. We need to keep compatibility with that. */
870 /* Set the indexes for symbol groups into the symbol table */
871 ilocalsym = 0;
872 iextdefsym = nlocalsym;
873 iundefsym = nlocalsym + nextdefsym;
875 /* allocate arrays for sorting externals by name */
876 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
877 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
879 i = 0;
880 j = 0;
882 symp = &syms;
884 while ((sym = *symp)) {
886 if((sym->type & N_EXT) == 0) {
887 sym->strx = *strtabsize;
888 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
889 *strtabsize += strlen(sym->name) + 1;
891 else {
892 if((sym->type & N_TYPE) != N_UNDF)
893 extdefsyms[i++] = sym;
894 else
895 undefsyms[j++] = sym;
897 symp = &(sym->next);
900 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
901 (int (*)(const void *, const void *))layout_compare);
902 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
903 (int (*)(const void *, const void *))layout_compare);
905 for(i = 0; i < nextdefsym; i++) {
906 extdefsyms[i]->snum = *numsyms;
907 *numsyms += 1;
909 for(j = 0; j < nundefsym; j++) {
910 undefsyms[j]->snum = *numsyms;
911 *numsyms += 1;
915 /* Calculate some values we'll need for writing later. */
917 static void macho_calculate_sizes (void)
919 struct section *s;
921 /* count sections and calculate in-memory and in-file offsets */
922 for (s = sects; s != NULL; s = s->next) {
923 /* zerofill sections aren't actually written to the file */
924 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
925 seg_filesize64 += s->size;
927 seg_vmsize64 += s->size;
928 ++seg_nsects64;
931 /* calculate size of all headers, load commands and sections to
932 ** get a pointer to the start of all the raw data */
933 if (seg_nsects64 > 0) {
934 ++head_ncmds64;
935 head_sizeofcmds64 +=
936 MACHO_SEGCMD64_SIZE + seg_nsects64 * MACHO_SECTCMD64_SIZE;
939 if (nsyms > 0) {
940 ++head_ncmds64;
941 head_sizeofcmds64 += MACHO_SYMCMD_SIZE;
945 /* Write out the header information for the file. */
947 static void macho_write_header (void)
949 fwriteint32_t(MH_MAGIC_64, machofp); /* magic */
950 fwriteint32_t(CPU_TYPE_X86_64, machofp); /* CPU type */
951 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
952 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
953 fwriteint32_t(head_ncmds64, machofp); /* number of load commands */
954 fwriteint32_t(head_sizeofcmds64, machofp); /* size of load commands */
955 fwriteint32_t(0, machofp); /* no flags */
956 fwriteint32_t(0, machofp); /* reserved for future use */
959 /* Write out the segment load command at offset. */
961 static uint32_t macho_write_segment (uint64_t offset)
963 uint64_t s_addr = 0;
964 uint64_t rel_base = alignint64_t (offset + seg_filesize64);
965 uint32_t s_reloff = 0;
966 struct section *s;
968 fwriteint32_t(LC_SEGMENT_64, machofp); /* cmd == LC_SEGMENT_64 */
970 /* size of load command including section load commands */
971 fwriteint32_t(MACHO_SEGCMD64_SIZE + seg_nsects64 *
972 MACHO_SECTCMD64_SIZE, machofp);
974 /* in an MH_OBJECT file all sections are in one unnamed (name
975 ** all zeros) segment */
976 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp);
977 fwriteint64_t(0, machofp); /* in-memory offset */
978 fwriteint64_t(seg_vmsize64, machofp); /* in-memory size */
979 fwriteint64_t(offset, machofp); /* in-file offset to data */
980 fwriteint64_t(seg_filesize64, machofp); /* in-file size */
981 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
982 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
983 fwriteint32_t(seg_nsects64, machofp); /* number of sections */
984 fwriteint32_t(0, machofp); /* no flags */
986 /* emit section headers */
987 for (s = sects; s != NULL; s = s->next) {
988 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
989 fwrite(s->segname, sizeof(s->segname), 1, machofp);
990 fwriteint64_t(s_addr, machofp);
991 fwriteint64_t(s->size, machofp);
993 /* dummy data for zerofill sections or proper values */
994 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
995 fwriteint32_t(offset, machofp);
996 /* Write out section alignment, as a power of two.
997 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
998 if (s->align == -1)
999 s->align = DEFAULT_SECTION_ALIGNMENT;
1000 fwriteint32_t(s->align, machofp);
1001 /* To be compatible with cctools as we emit
1002 a zero reloff if we have no relocations. */
1003 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
1004 fwriteint32_t(s->nreloc, machofp);
1006 offset += s->size;
1007 s_reloff += s->nreloc * MACHO_RELINFO64_SIZE;
1008 } else {
1009 fwriteint32_t(0, machofp);
1010 fwriteint32_t(0, machofp);
1011 fwriteint32_t(0, machofp);
1012 fwriteint32_t(0, machofp);
1015 if (s->nreloc) {
1016 s->flags |= S_ATTR_LOC_RELOC;
1017 if (s->extreloc)
1018 s->flags |= S_ATTR_EXT_RELOC;
1020 fwriteint32_t(s->flags, machofp); /* flags */
1021 fwriteint32_t(0, machofp); /* reserved */
1022 fwriteint32_t(0, machofp); /* reserved */
1024 fwriteint32_t(0, machofp); /* align */
1025 s_addr += s->size;
1028 rel_padcnt64 = rel_base - offset;
1029 offset = rel_base + s_reloff;
1031 return offset;
1034 /* For a given chain of relocs r, write out the entire relocation
1035 chain to the object file. */
1037 static void macho_write_relocs (struct reloc *r)
1039 while (r) {
1040 uint32_t word2;
1042 fwriteint32_t(r->addr, machofp); /* reloc offset */
1044 word2 = r->snum;
1045 word2 |= r->pcrel << 24;
1046 word2 |= r->length << 25;
1047 word2 |= r->ext << 27;
1048 word2 |= r->type << 28;
1049 fwriteint32_t(word2, machofp); /* reloc data */
1050 r = r->next;
1054 /* Write out the section data. */
1055 static void macho_write_section (void)
1057 struct section *s, *s2;
1058 struct reloc *r;
1059 char *rel_paddata = "\0\0\0\0\0\0\0";
1060 uint8_t fi, *p, *q, blk[8];
1061 int32_t len;
1062 int64_t l;
1064 for (s = sects; s != NULL; s = s->next) {
1065 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1066 continue;
1068 /* no padding needs to be done to the sections */
1070 /* Like a.out Mach-O references things in the data or bss
1071 * sections by addresses which are actually relative to the
1072 * start of the _text_ section, in the _file_. See outaout.c
1073 * for more information. */
1074 saa_rewind(s->data);
1075 for (r = s->relocs; r != NULL; r = r->next) {
1076 len = (int32_t)r->length << 1;
1077 if(len > 4) len = 8;
1078 saa_fread(s->data, r->addr, blk, len);
1079 p = q = blk;
1080 l = *p++;
1082 /* get offset based on relocation type */
1083 if (r->length > 0) {
1084 l += ((int64_t)*p++) << 8;
1086 if (r->length > 1) {
1087 l += ((int64_t)*p++) << 16;
1088 l += ((int64_t)*p++) << 24;
1091 if (r->length > 2) {
1092 l += ((int64_t)*p++) << 32;
1093 l += ((int64_t)*p++) << 40;
1094 l += ((int64_t)*p++) << 48;
1095 l += ((int64_t)*p++) << 56;
1101 /* If the relocation is internal add to the current section
1102 offset. Otherwise the only value we need is the symbol
1103 offset which we already have. The linker takes care
1104 of the rest of the address. */
1105 if (!r->ext) {
1106 /* add sizes of previous sections to current offset */
1107 for (s2 = sects, fi = 1;
1108 s2 != NULL && fi < r->snum; s2 = s2->next, fi++)
1109 l += s2->size;
1112 /* write new offset back */
1113 if (r->length == 3)
1114 WRITEDLONG(q, l);
1115 else if (r->length == 2)
1116 WRITELONG(q, l);
1117 else if (r->length == 1)
1118 WRITESHORT(q, l);
1119 else
1120 *q++ = l & 0xFF;
1122 saa_fwrite(s->data, r->addr, blk, len);
1125 /* dump the section data to file */
1126 saa_fpwrite(s->data, machofp);
1129 /* pad last section up to reloc entries on int64_t boundary */
1130 fwrite(rel_paddata, rel_padcnt64, 1, machofp);
1132 /* emit relocation entries */
1133 for (s = sects; s != NULL; s = s->next)
1134 macho_write_relocs (s->relocs);
1137 /* Write out the symbol table. We should already have sorted this
1138 before now. */
1139 static void macho_write_symtab (void)
1141 struct symbol *sym;
1142 struct section *s;
1143 int64_t fi;
1144 uint64_t i;
1146 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1148 for (sym = syms; sym != NULL; sym = sym->next) {
1149 if ((sym->type & N_EXT) == 0) {
1150 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1151 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1152 fwrite(&sym->sect, 1, 1, machofp); /* section */
1153 fwriteint16_t(sym->desc, machofp); /* description */
1155 /* Fix up the symbol value now that we know the final section
1156 sizes. */
1157 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1158 for (s = sects, fi = 1;
1159 s != NULL && fi < sym->sect; s = s->next, ++fi)
1160 sym->value += s->size;
1163 fwriteint64_t(sym->value, machofp); /* value (i.e. offset) */
1167 for (i = 0; i < nextdefsym; i++) {
1168 sym = extdefsyms[i];
1169 fwriteint32_t(sym->strx, machofp);
1170 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1171 fwrite(&sym->sect, 1, 1, machofp); /* section */
1172 fwriteint16_t(sym->desc, machofp); /* description */
1174 /* Fix up the symbol value now that we know the final section
1175 sizes. */
1176 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1177 for (s = sects, fi = 1;
1178 s != NULL && fi < sym->sect; s = s->next, ++fi)
1179 sym->value += s->size;
1182 fwriteint64_t(sym->value, machofp); /* value (i.e. offset) */
1185 for (i = 0; i < nundefsym; i++) {
1186 sym = undefsyms[i];
1187 fwriteint32_t(sym->strx, machofp);
1188 fwrite(&sym->type, 1, 1, machofp); // symbol type
1189 fwrite(&sym->sect, 1, 1, machofp); // section
1190 fwriteint16_t(sym->desc, machofp); // description
1192 // Fix up the symbol value now that we know the final section sizes.
1193 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1194 for (s = sects, fi = 1;
1195 s != NULL && fi < sym->sect; s = s->next, ++fi)
1196 sym->value += s->size;
1199 fwriteint64_t(sym->value, machofp); // value (i.e. offset)
1204 /* Fixup the snum in the relocation entries, we should be
1205 doing this only for externally undefined symbols. */
1206 static void macho_fixup_relocs (struct reloc *r)
1208 struct symbol *sym;
1209 uint32_t i;
1211 while (r != NULL) {
1212 if (r->ext) {
1213 for (i = 0; i < nundefsym; i++) {
1214 sym = undefsyms[i];
1215 if (sym->initial_snum == r->snum) {
1216 r->snum = sym->snum;
1217 break;
1221 r = r->next;
1225 /* Write out the object file. */
1227 static void macho_write (void)
1229 uint64_t offset = 0;
1231 /* mach-o object file structure:
1233 ** mach header
1234 ** uint32_t magic
1235 ** int cpu type
1236 ** int cpu subtype
1237 ** uint32_t mach file type
1238 ** uint32_t number of load commands
1239 ** uint32_t size of all load commands
1240 ** (includes section struct size of segment command)
1241 ** uint32_t flags
1243 ** segment command
1244 ** uint32_t command type == LC_SEGMENT_64
1245 ** uint32_t size of load command
1246 ** (including section load commands)
1247 ** char[16] segment name
1248 ** uint64_t in-memory offset
1249 ** uint64_t in-memory size
1250 ** uint64_t in-file offset to data area
1251 ** uint64_t in-file size
1252 ** (in-memory size excluding zerofill sections)
1253 ** int maximum vm protection
1254 ** int initial vm protection
1255 ** uint32_t number of sections
1256 ** uint32_t flags
1258 ** section commands
1259 ** char[16] section name
1260 ** char[16] segment name
1261 ** uint64_t in-memory offset
1262 ** uint64_t in-memory size
1263 ** uint32_t in-file offset
1264 ** uint32_t alignment
1265 ** (irrelevant in MH_OBJECT)
1266 ** uint32_t in-file offset of relocation entires
1267 ** uint32_t number of relocations
1268 ** uint32_t flags
1269 ** uint32_t reserved
1270 ** uint32_t reserved
1272 ** symbol table command
1273 ** uint32_t command type == LC_SYMTAB
1274 ** uint32_t size of load command
1275 ** uint32_t symbol table offset
1276 ** uint32_t number of symbol table entries
1277 ** uint32_t string table offset
1278 ** uint32_t string table size
1280 ** raw section data
1282 ** padding to int64_t boundary
1284 ** relocation data (struct reloc)
1285 ** int32_t offset
1286 ** uint data (symbolnum, pcrel, length, extern, type)
1288 ** symbol table data (struct nlist)
1289 ** int32_t string table entry number
1290 ** uint8_t type
1291 ** (extern, absolute, defined in section)
1292 ** uint8_t section
1293 ** (0 for global symbols, section number of definition (>= 1, <=
1294 ** 254) for local symbols, size of variable for common symbols
1295 ** [type == extern])
1296 ** int16_t description
1297 ** (for stab debugging format)
1298 ** uint64_t value (i.e. file offset) of symbol or stab offset
1300 ** string table data
1301 ** list of null-terminated strings
1304 /* Emit the Mach-O header. */
1305 macho_write_header();
1307 offset = MACHO_HEADER64_SIZE + head_sizeofcmds64;
1309 /* emit the segment load command */
1310 if (seg_nsects64 > 0)
1311 offset = macho_write_segment (offset);
1312 else
1313 error(ERR_WARNING, "no sections?");
1315 if (nsyms > 0) {
1316 /* write out symbol command */
1317 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1318 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1319 fwriteint32_t(offset, machofp); /* symbol table offset */
1320 fwriteint32_t(nsyms, machofp); /* number of symbol
1321 ** table entries */
1323 offset += nsyms * MACHO_NLIST64_SIZE;
1324 fwriteint32_t(offset, machofp); /* string table offset */
1325 fwriteint32_t(strslen, machofp); /* string table size */
1328 /* emit section data */
1329 if (seg_nsects64 > 0)
1330 macho_write_section ();
1332 /* emit symbol table if we have symbols */
1333 if (nsyms > 0)
1334 macho_write_symtab ();
1336 /* we don't need to pad here since MACHO_NLIST64_SIZE == 16 */
1338 /* emit string table */
1339 saa_fpwrite(strs, machofp);
1341 /* We do quite a bit here, starting with finalizing all of the data
1342 for the object file, writing, and then freeing all of the data from
1343 the file. */
1345 static void macho_cleanup(int debuginfo)
1347 struct section *s;
1348 struct reloc *r;
1349 struct symbol *sym;
1351 (void)debuginfo;
1353 /* Sort all symbols. */
1354 macho_layout_symbols (&nsyms, &strslen);
1356 /* Fixup relocation entries */
1357 for (s = sects; s != NULL; s = s->next) {
1358 macho_fixup_relocs (s->relocs);
1361 /* First calculate and finalize needed values. */
1362 macho_calculate_sizes();
1363 macho_write();
1365 /* done - yay! */
1366 fclose(machofp);
1368 /* free up everything */
1369 while (sects->next) {
1370 s = sects;
1371 sects = sects->next;
1373 saa_free(s->data);
1374 while (s->relocs != NULL) {
1375 r = s->relocs;
1376 s->relocs = s->relocs->next;
1377 nasm_free(r);
1380 nasm_free(s);
1383 saa_free(strs);
1384 raa_free(extsyms);
1386 if (syms) {
1387 while (syms->next) {
1388 sym = syms;
1389 syms = syms->next;
1391 nasm_free (sym);
1396 /* Debugging routines. */
1397 static void debug_reloc (struct reloc *r)
1399 fprintf (stdout, "reloc:\n");
1400 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1401 fprintf (stdout, "\tsnum: %d\n", r->snum);
1402 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1403 fprintf (stdout, "\tlength: %d\n", r->length);
1404 fprintf (stdout, "\text: %d\n", r->ext);
1405 fprintf (stdout, "\ttype: %d\n", r->type);
1408 static void debug_section_relocs (struct section *s)
1410 struct reloc *r = s->relocs;
1412 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1414 while (r != NULL) {
1415 debug_reloc (r);
1416 r = r->next;
1420 struct ofmt of_macho64 = {
1421 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1422 "macho64",
1423 NULL,
1424 null_debug_arr,
1425 &null_debug_form,
1426 generic_stdmac,
1427 macho_init,
1428 macho_setinfo,
1429 macho_output,
1430 macho_symdef,
1431 macho_section,
1432 macho_segbase,
1433 macho_directive,
1434 macho_filename,
1435 macho_cleanup
1438 #endif
1441 * Local Variables:
1442 * mode:c
1443 * c-basic-offset:4
1444 * End:
1446 * end of file */