Add new copyright headers to the output modules
[nasm/avx512.git] / output / outmacho.c
blobbf9151d9ea9a6e70b104d8fb9909bdc804fd8b79
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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
12 * reference.
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
21 * conditions are met:
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * outmacho.c output routines for the Netwide Assembler to produce
48 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
51 /* Most of this file is, like Mach-O itself, based on a.out. For more
52 * guidelines see outaout.c. */
54 #include "compiler.h"
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <ctype.h>
60 #include <inttypes.h>
62 #include "nasm.h"
63 #include "nasmlib.h"
64 #include "saa.h"
65 #include "raa.h"
66 #include "output/outform.h"
67 #include "output/outlib.h"
69 #if defined(OF_MACHO)
71 /* Mach-O in-file header structure sizes */
72 #define MACHO_HEADER_SIZE (28)
73 #define MACHO_SEGCMD_SIZE (56)
74 #define MACHO_SECTCMD_SIZE (68)
75 #define MACHO_SYMCMD_SIZE (24)
76 #define MACHO_NLIST_SIZE (12)
77 #define MACHO_RELINFO_SIZE (8)
79 /* Mach-O file header values */
80 #define MH_MAGIC (0xfeedface)
81 #define CPU_TYPE_I386 (7) /* x86 platform */
82 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
83 #define MH_OBJECT (0x1) /* object file */
85 #define LC_SEGMENT (0x1) /* segment load command */
86 #define LC_SYMTAB (0x2) /* symbol table load command */
88 #define VM_PROT_NONE (0x00)
89 #define VM_PROT_READ (0x01)
90 #define VM_PROT_WRITE (0x02)
91 #define VM_PROT_EXECUTE (0x04)
93 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
94 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
96 struct section {
97 /* nasm internal data */
98 struct section *next;
99 struct SAA *data;
100 int32_t index;
101 struct reloc *relocs;
102 int align;
104 /* data that goes into the file */
105 char sectname[16]; /* what this section is called */
106 char segname[16]; /* segment this section will be in */
107 uint32_t addr; /* in-memory address (subject to alignment) */
108 uint32_t size; /* in-memory and -file size */
109 uint32_t nreloc; /* relocation entry count */
110 uint32_t flags; /* type and attributes (masked) */
113 #define SECTION_TYPE 0x000000ff /* section type mask */
115 #define S_REGULAR (0x0) /* standard section */
116 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
118 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
119 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
120 machine instructions */
121 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
122 relocation entries */
123 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
124 relocation entries */
127 static struct sectmap {
128 const char *nasmsect;
129 const char *segname;
130 const char *sectname;
131 const int32_t flags;
132 } sectmap[] = {
133 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
134 {".data", "__DATA", "__data", S_REGULAR},
135 {".rodata", "__DATA", "__const", S_REGULAR},
136 {".bss", "__DATA", "__bss", S_ZEROFILL},
137 {NULL, NULL, NULL, 0}
140 struct reloc {
141 /* nasm internal data */
142 struct reloc *next;
144 /* data that goes into the file */
145 int32_t addr; /* op's offset in section */
146 unsigned int snum:24, /* contains symbol index if
147 ** ext otherwise in-file
148 ** section number */
149 pcrel:1, /* relative relocation */
150 length:2, /* 0=byte, 1=word, 2=int32_t */
151 ext:1, /* external symbol referenced */
152 type:4; /* reloc type, 0 for us */
155 #define R_ABS 0 /* absolute relocation */
156 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
157 ** highest bit == 1 */
159 struct symbol {
160 /* nasm internal data */
161 struct symbol *next; /* next symbol in the list */
162 char *name; /* name of this symbol */
163 int32_t initial_snum; /* symbol number used above in
164 reloc */
165 int32_t snum; /* true snum for reloc */
167 /* data that goes into the file */
168 int32_t strx; /* string table index */
169 uint8_t type; /* symbol type */
170 uint8_t sect; /* NO_SECT or section number */
171 int16_t desc; /* for stab debugging, 0 for us */
172 uint32_t value; /* offset of symbol in section */
175 /* symbol type bits */
176 #define N_EXT 0x01 /* global or external symbol */
178 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
179 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
180 #define N_SECT 0xe /* defined symbol, n_sect holds
181 ** section number */
183 #define N_TYPE 0x0e /* type bit mask */
185 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
187 /* special section number values */
188 #define NO_SECT 0 /* no section, invalid */
189 #define MAX_SECT 255 /* maximum number of sections */
191 static struct section *sects, **sectstail;
192 static struct symbol *syms, **symstail;
193 static uint32_t nsyms;
195 /* These variables are set by macho_layout_symbols() to organize
196 the symbol table and string table in order the dynamic linker
197 expects. They are then used in macho_write() to put out the
198 symbols and strings in that order.
200 The order of the symbol table is:
201 local symbols
202 defined external symbols (sorted by name)
203 undefined external symbols (sorted by name)
205 The order of the string table is:
206 strings for external symbols
207 strings for local symbols
209 static uint32_t ilocalsym = 0;
210 static uint32_t iextdefsym = 0;
211 static uint32_t iundefsym = 0;
212 static uint32_t nlocalsym;
213 static uint32_t nextdefsym;
214 static uint32_t nundefsym;
215 static struct symbol **extdefsyms = NULL;
216 static struct symbol **undefsyms = NULL;
218 static struct RAA *extsyms;
219 static struct SAA *strs;
220 static uint32_t strslen;
222 static FILE *machofp;
223 static efunc error;
224 static evalfunc evaluate;
226 extern struct ofmt of_macho;
228 /* Global file information. This should be cleaned up into either
229 a structure or as function arguments. */
230 uint32_t head_ncmds = 0;
231 uint32_t head_sizeofcmds = 0;
232 uint32_t seg_filesize = 0;
233 uint32_t seg_vmsize = 0;
234 uint32_t seg_nsects = 0;
235 uint32_t rel_padcnt = 0;
238 #define xstrncpy(xdst, xsrc) \
239 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
240 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
241 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
243 #define align(x, y) \
244 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
246 #define alignint32_t(x) \
247 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
249 static void debug_reloc (struct reloc *);
250 static void debug_section_relocs (struct section *) _unused;
252 static int exact_log2 (uint32_t align)
254 if (align == 0) {
255 return 0;
256 } else if (align & (align-1)) {
257 return -1; /* Not a power of 2 */
258 } else {
259 #ifdef HAVE_GNUC_4
260 return __builtin_ctzl (align);
261 #else
262 uint32_t result = 0;
264 /* We know exactly one bit is set at this point. */
265 if (align & 0xffff0000)
266 result |= 16;
267 if (align & 0xff00ff00)
268 result |= 8;
269 if (align & 0xf0f0f0f0)
270 result |= 4;
271 if (align & 0xcccccccc)
272 result |= 2;
273 if (align & 0xaaaaaaaa)
274 result |= 1;
276 return result;
277 #endif
281 static struct section *get_section_by_name(const char *segname,
282 const char *sectname)
284 struct section *s;
286 for (s = sects; s != NULL; s = s->next)
287 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
288 break;
290 return s;
293 static struct section *get_section_by_index(const int32_t index)
295 struct section *s;
297 for (s = sects; s != NULL; s = s->next)
298 if (index == s->index)
299 break;
301 return s;
304 static int32_t get_section_index_by_name(const char *segname,
305 const char *sectname)
307 struct section *s;
309 for (s = sects; s != NULL; s = s->next)
310 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
311 return s->index;
313 return -1;
316 static char *get_section_name_by_index(const int32_t index)
318 struct section *s;
320 for (s = sects; s != NULL; s = s->next)
321 if (index == s->index)
322 return s->sectname;
324 return NULL;
327 static uint8_t get_section_fileindex_by_index(const int32_t index)
329 struct section *s;
330 uint8_t i = 1;
332 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
333 if (index == s->index)
334 return i;
336 if (i == MAX_SECT)
337 error(ERR_WARNING,
338 "too many sections (>255) - clipped by fileindex");
340 return NO_SECT;
343 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
344 evalfunc eval)
346 char zero = 0;
348 machofp = fp;
349 error = errfunc;
350 evaluate = eval;
352 (void)ldef; /* placate optimisers */
354 sects = NULL;
355 sectstail = &sects;
357 syms = NULL;
358 symstail = &syms;
359 nsyms = 0;
360 nlocalsym = 0;
361 nextdefsym = 0;
362 nundefsym = 0;
364 extsyms = raa_init();
365 strs = saa_init(1L);
367 /* string table starts with a zero byte - don't ask why */
368 saa_wbytes(strs, &zero, sizeof(char));
369 strslen = 1;
372 static int macho_setinfo(enum geninfo type, char **val)
374 (void)type;
375 (void)val;
376 return 0;
379 static void sect_write(struct section *sect,
380 const uint8_t *data, uint32_t len)
382 saa_wbytes(sect->data, data, len);
383 sect->size += len;
386 static void add_reloc(struct section *sect, int32_t section,
387 int pcrel, int bytes)
389 struct reloc *r;
390 int32_t fi;
392 /* NeXT as puts relocs in reversed order (address-wise) into the
393 ** files, so we do the same, doesn't seem to make much of a
394 ** difference either way */
395 r = nasm_malloc(sizeof(struct reloc));
396 r->next = sect->relocs;
397 sect->relocs = r;
399 /* the current end of the section will be the symbol's address for
400 ** now, might have to be fixed by macho_fixup_relocs() later on. make
401 ** sure we don't make the symbol scattered by setting the highest
402 ** bit by accident */
403 r->addr = sect->size & ~R_SCATTERED;
404 r->ext = 0;
405 r->pcrel = pcrel;
407 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
408 r->length = bytes >> 1;
410 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
411 r->type = 0;
413 if (section == NO_SEG) {
414 /* absolute local symbol if no section index given */
415 r->snum = R_ABS;
416 } else {
417 fi = get_section_fileindex_by_index(section);
419 if (fi == NO_SECT) {
420 /* external symbol if no section with that index known,
421 ** symbol number was saved in macho_symdef() */
422 r->snum = raa_read(extsyms, section);
423 r->ext = 1;
424 } else {
425 /* local symbol in section fi */
426 r->snum = fi;
430 ++sect->nreloc;
433 static void macho_output(int32_t secto, const void *data,
434 enum out_type type, uint64_t size,
435 int32_t section, int32_t wrt)
437 struct section *s, *sbss;
438 int32_t addr;
439 uint8_t mydata[4], *p;
441 if (wrt != NO_SEG) {
442 wrt = NO_SEG;
443 error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
444 /* continue to do _something_ */
447 if (secto == NO_SEG) {
448 if (type != OUT_RESERVE)
449 error(ERR_NONFATAL, "attempt to assemble code in "
450 "[ABSOLUTE] space");
452 return;
455 s = get_section_by_index(secto);
457 if (s == NULL) {
458 error(ERR_WARNING, "attempt to assemble code in"
459 " section %d: defaulting to `.text'", secto);
460 s = get_section_by_name("__TEXT", "__text");
462 /* should never happen */
463 if (s == NULL)
464 error(ERR_PANIC, "text section not found");
467 sbss = get_section_by_name("__DATA", "__bss");
469 if (s == sbss && type != OUT_RESERVE) {
470 error(ERR_WARNING, "attempt to initialize memory in the"
471 " BSS section: ignored");
472 s->size += realsize(type, size);
473 return;
476 switch (type) {
477 case OUT_RESERVE:
478 if (s != sbss) {
479 error(ERR_WARNING, "uninitialized space declared in"
480 " %s section: zeroing",
481 get_section_name_by_index(secto));
483 sect_write(s, NULL, size);
484 } else
485 s->size += size;
487 break;
489 case OUT_RAWDATA:
490 if (section != NO_SEG)
491 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
493 sect_write(s, data, size);
494 break;
496 case OUT_ADDRESS:
497 addr = *(int64_t *)data;
499 if (section != NO_SEG) {
500 if (section % 2) {
501 error(ERR_NONFATAL, "Mach-O format does not support"
502 " section base references");
503 } else
504 add_reloc(s, section, 0, size);
507 p = mydata;
508 WRITEADDR(p, addr, size);
509 sect_write(s, mydata, size);
510 break;
512 case OUT_REL2ADR:
513 if (section == secto)
514 error(ERR_PANIC, "intra-section OUT_REL2ADR");
516 if (section != NO_SEG && section % 2) {
517 error(ERR_NONFATAL, "Mach-O format does not support"
518 " section base references");
519 } else
520 add_reloc(s, section, 1, 2);
522 p = mydata;
523 WRITESHORT(p, *(int32_t *)data - (size + s->size));
524 sect_write(s, mydata, 2L);
525 break;
527 case OUT_REL4ADR:
528 if (section == secto)
529 error(ERR_PANIC, "intra-section OUT_REL4ADR");
531 if (section != NO_SEG && section % 2) {
532 error(ERR_NONFATAL, "Mach-O format does not support"
533 " section base references");
534 } else
535 add_reloc(s, section, 1, 4);
537 p = mydata;
538 WRITELONG(p, *(int32_t *)data - (size + s->size));
539 sect_write(s, mydata, 4L);
540 break;
542 default:
543 error(ERR_PANIC, "unknown output type?");
544 break;
548 static int32_t macho_section(char *name, int pass, int *bits)
550 int32_t index, originalIndex;
551 char *sectionAttributes;
552 struct sectmap *sm;
553 struct section *s;
555 (void)pass;
557 /* Default to 32 bits. */
558 if (!name) {
559 *bits = 32;
560 name = ".text";
561 sectionAttributes = NULL;
562 } else {
563 sectionAttributes = name;
564 name = nasm_strsep(&sectionAttributes, " \t");
567 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
568 /* make lookup into section name translation table */
569 if (!strcmp(name, sm->nasmsect)) {
570 char *currentAttribute;
572 /* try to find section with that name */
573 originalIndex = index = get_section_index_by_name(sm->segname,
574 sm->sectname);
576 /* create it if it doesn't exist yet */
577 if (index == -1) {
578 s = *sectstail = nasm_malloc(sizeof(struct section));
579 s->next = NULL;
580 sectstail = &s->next;
582 s->data = saa_init(1L);
583 s->index = seg_alloc();
584 s->relocs = NULL;
585 s->align = -1;
587 xstrncpy(s->segname, sm->segname);
588 xstrncpy(s->sectname, sm->sectname);
589 s->size = 0;
590 s->nreloc = 0;
591 s->flags = sm->flags;
593 index = s->index;
594 } else {
595 s = get_section_by_index(index);
598 while ((NULL != sectionAttributes)
599 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
600 if (0 != *currentAttribute) {
601 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
602 char *end;
603 int newAlignment, value;
605 value = strtoul(currentAttribute + 6, (char**)&end, 0);
606 newAlignment = exact_log2(value);
608 if (0 != *end) {
609 error(ERR_PANIC,
610 "unknown or missing alignment value \"%s\" "
611 "specified for section \"%s\"",
612 currentAttribute + 6,
613 name);
614 return NO_SEG;
615 } else if (0 > newAlignment) {
616 error(ERR_PANIC,
617 "alignment of %d (for section \"%s\") is not "
618 "a power of two",
619 value,
620 name);
621 return NO_SEG;
624 if ((-1 != originalIndex)
625 && (s->align != newAlignment)
626 && (s->align != -1)) {
627 error(ERR_PANIC,
628 "section \"%s\" has already been specified "
629 "with alignment %d, conflicts with new "
630 "alignment of %d",
631 name,
632 (1 << s->align),
633 value);
634 return NO_SEG;
637 s->align = newAlignment;
638 } else if (!nasm_stricmp("data", currentAttribute)) {
639 /* Do nothing; 'data' is implicit */
640 } else {
641 error(ERR_PANIC,
642 "unknown section attribute %s for section %s",
643 currentAttribute,
644 name);
645 return NO_SEG;
650 return index;
654 error(ERR_PANIC, "invalid section name %s", name);
655 return NO_SEG;
658 static void macho_symdef(char *name, int32_t section, int64_t offset,
659 int is_global, char *special)
661 struct symbol *sym;
663 if (special) {
664 error(ERR_NONFATAL, "The Mach-O output format does "
665 "not support any special symbol types");
666 return;
669 if (is_global == 3) {
670 error(ERR_NONFATAL, "The Mach-O format does not "
671 "(yet) support forward reference fixups.");
672 return;
675 sym = *symstail = nasm_malloc(sizeof(struct symbol));
676 sym->next = NULL;
677 symstail = &sym->next;
679 sym->name = name;
680 sym->strx = strslen;
681 sym->type = 0;
682 sym->desc = 0;
683 sym->value = offset;
684 sym->initial_snum = -1;
686 /* external and common symbols get N_EXT */
687 if (is_global != 0)
688 sym->type |= N_EXT;
690 if (section == NO_SEG) {
691 /* symbols in no section get absolute */
692 sym->type |= N_ABS;
693 sym->sect = NO_SECT;
694 } else {
695 sym->type |= N_SECT;
697 /* get the in-file index of the section the symbol was defined in */
698 sym->sect = get_section_fileindex_by_index(section);
700 if (sym->sect == NO_SECT) {
701 /* remember symbol number of references to external
702 ** symbols, this works because every external symbol gets
703 ** its own section number allocated internally by nasm and
704 ** can so be used as a key */
705 extsyms = raa_write(extsyms, section, nsyms);
706 sym->initial_snum = nsyms;
708 switch (is_global) {
709 case 1:
710 case 2:
711 /* there isn't actually a difference between global
712 ** and common symbols, both even have their size in
713 ** sym->value */
714 sym->type = N_EXT;
715 break;
717 default:
718 /* give an error on unfound section if it's not an
719 ** external or common symbol (assemble_file() does a
720 ** seg_alloc() on every call for them) */
721 error(ERR_PANIC, "in-file index for section %d not found",
722 section);
727 ++nsyms;
730 static int32_t macho_segbase(int32_t section)
732 return section;
735 static int macho_directive(char *directive, char *value, int pass)
737 (void)directive;
738 (void)value;
739 (void)pass;
740 return 0;
743 static void macho_filename(char *inname, char *outname, efunc error)
745 standard_extension(inname, outname, ".o", error);
748 extern macros_t macho_stdmac[];
750 /* Comparison function for qsort symbol layout. */
751 static int layout_compare (const struct symbol **s1,
752 const struct symbol **s2)
754 return (strcmp ((*s1)->name, (*s2)->name));
757 /* The native assembler does a few things in a similar function
759 * Remove temporary labels
760 * Sort symbols according to local, external, undefined (by name)
761 * Order the string table
763 We do not remove temporary labels right now.
765 numsyms is the total number of symbols we have. strtabsize is the
766 number entries in the string table. */
768 static void macho_layout_symbols (uint32_t *numsyms,
769 uint32_t *strtabsize)
771 struct symbol *sym, **symp;
772 uint32_t i,j;
774 *numsyms = 0;
775 *strtabsize = sizeof (char);
777 symp = &syms;
779 while ((sym = *symp)) {
780 /* Undefined symbols are now external. */
781 if (sym->type == N_UNDF)
782 sym->type |= N_EXT;
784 if ((sym->type & N_EXT) == 0) {
785 sym->snum = *numsyms;
786 *numsyms = *numsyms + 1;
787 nlocalsym++;
789 else {
790 if ((sym->type & N_TYPE) != N_UNDF)
791 nextdefsym++;
792 else
793 nundefsym++;
795 /* If we handle debug info we'll want
796 to check for it here instead of just
797 adding the symbol to the string table. */
798 sym->strx = *strtabsize;
799 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
800 *strtabsize += strlen(sym->name) + 1;
802 symp = &(sym->next);
805 /* Next, sort the symbols. Most of this code is a direct translation from
806 the Apple cctools symbol layout. We need to keep compatibility with that. */
807 /* Set the indexes for symbol groups into the symbol table */
808 ilocalsym = 0;
809 iextdefsym = nlocalsym;
810 iundefsym = nlocalsym + nextdefsym;
812 /* allocate arrays for sorting externals by name */
813 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
814 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
816 i = 0;
817 j = 0;
819 symp = &syms;
821 while ((sym = *symp)) {
823 if((sym->type & N_EXT) == 0) {
824 sym->strx = *strtabsize;
825 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
826 *strtabsize += strlen(sym->name) + 1;
828 else {
829 if((sym->type & N_TYPE) != N_UNDF)
830 extdefsyms[i++] = sym;
831 else
832 undefsyms[j++] = sym;
834 symp = &(sym->next);
837 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
838 (int (*)(const void *, const void *))layout_compare);
839 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
840 (int (*)(const void *, const void *))layout_compare);
842 for(i = 0; i < nextdefsym; i++) {
843 extdefsyms[i]->snum = *numsyms;
844 *numsyms += 1;
846 for(j = 0; j < nundefsym; j++) {
847 undefsyms[j]->snum = *numsyms;
848 *numsyms += 1;
852 /* Calculate some values we'll need for writing later. */
854 static void macho_calculate_sizes (void)
856 struct section *s;
858 /* count sections and calculate in-memory and in-file offsets */
859 for (s = sects; s != NULL; s = s->next) {
860 uint32_t pad = 0;
862 /* zerofill sections aren't actually written to the file */
863 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
864 seg_filesize += s->size;
866 /* recalculate segment address based on alignment and vm size */
867 s->addr = seg_vmsize;
868 /* we need section alignment to calculate final section address */
869 if (s->align == -1)
870 s->align = DEFAULT_SECTION_ALIGNMENT;
871 if(s->align) {
872 uint32_t newaddr = align(s->addr, 1 << s->align);
873 pad = newaddr - s->addr;
874 s->addr = newaddr;
877 seg_vmsize += s->size + pad;
878 ++seg_nsects;
881 /* calculate size of all headers, load commands and sections to
882 ** get a pointer to the start of all the raw data */
883 if (seg_nsects > 0) {
884 ++head_ncmds;
885 head_sizeofcmds +=
886 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
889 if (nsyms > 0) {
890 ++head_ncmds;
891 head_sizeofcmds += MACHO_SYMCMD_SIZE;
895 /* Write out the header information for the file. */
897 static void macho_write_header (void)
899 fwriteint32_t(MH_MAGIC, machofp); /* magic */
900 fwriteint32_t(CPU_TYPE_I386, machofp); /* CPU type */
901 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
902 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
903 fwriteint32_t(head_ncmds, machofp); /* number of load commands */
904 fwriteint32_t(head_sizeofcmds, machofp); /* size of load commands */
905 fwriteint32_t(0, machofp); /* no flags */
908 /* Write out the segment load command at offset. */
910 static uint32_t macho_write_segment (uint32_t offset)
912 uint32_t rel_base = alignint32_t (offset + seg_filesize);
913 uint32_t s_reloff = 0;
914 struct section *s;
916 fwriteint32_t(LC_SEGMENT, machofp); /* cmd == LC_SEGMENT */
918 /* size of load command including section load commands */
919 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
920 MACHO_SECTCMD_SIZE, machofp);
922 /* in an MH_OBJECT file all sections are in one unnamed (name
923 ** all zeros) segment */
924 fwritezero(16, machofp);
925 fwriteint32_t(0, machofp); /* in-memory offset */
926 fwriteint32_t(seg_vmsize, machofp); /* in-memory size */
927 fwriteint32_t(offset, machofp); /* in-file offset to data */
928 fwriteint32_t(seg_filesize, machofp); /* in-file size */
929 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
930 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
931 fwriteint32_t(seg_nsects, machofp); /* number of sections */
932 fwriteint32_t(0, machofp); /* no flags */
934 /* emit section headers */
935 for (s = sects; s != NULL; s = s->next) {
936 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
937 fwrite(s->segname, sizeof(s->segname), 1, machofp);
938 fwriteint32_t(s->addr, machofp);
939 fwriteint32_t(s->size, machofp);
941 /* dummy data for zerofill sections or proper values */
942 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
943 fwriteint32_t(offset, machofp);
944 /* Write out section alignment, as a power of two.
945 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
946 if (s->align == -1)
947 s->align = DEFAULT_SECTION_ALIGNMENT;
948 fwriteint32_t(s->align, machofp);
949 /* To be compatible with cctools as we emit
950 a zero reloff if we have no relocations. */
951 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
952 fwriteint32_t(s->nreloc, machofp);
954 offset += s->size;
955 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
956 } else {
957 fwriteint32_t(0, machofp);
958 fwriteint32_t(0, machofp);
959 fwriteint32_t(0, machofp);
960 fwriteint32_t(0, machofp);
963 fwriteint32_t(s->flags, machofp); /* flags */
964 fwriteint32_t(0, machofp); /* reserved */
965 fwriteint32_t(0, machofp); /* reserved */
968 rel_padcnt = rel_base - offset;
969 offset = rel_base + s_reloff;
971 return offset;
974 /* For a given chain of relocs r, write out the entire relocation
975 chain to the object file. */
977 static void macho_write_relocs (struct reloc *r)
979 while (r) {
980 uint32_t word2;
982 fwriteint32_t(r->addr, machofp); /* reloc offset */
984 word2 = r->snum;
985 word2 |= r->pcrel << 24;
986 word2 |= r->length << 25;
987 word2 |= r->ext << 27;
988 word2 |= r->type << 28;
989 fwriteint32_t(word2, machofp); /* reloc data */
991 r = r->next;
995 /* Write out the section data. */
996 static void macho_write_section (void)
998 struct section *s, *s2;
999 struct reloc *r;
1000 uint8_t *p, *q, blk[4];
1001 int32_t l;
1003 for (s = sects; s != NULL; s = s->next) {
1004 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1005 continue;
1007 /* no padding needs to be done to the sections */
1009 /* Like a.out Mach-O references things in the data or bss
1010 * sections by addresses which are actually relative to the
1011 * start of the _text_ section, in the _file_. See outaout.c
1012 * for more information. */
1013 saa_rewind(s->data);
1014 for (r = s->relocs; r != NULL; r = r->next) {
1015 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
1016 p = q = blk;
1017 l = *p++;
1019 /* get offset based on relocation type */
1020 if (r->length > 0) {
1021 l += ((int32_t)*p++) << 8;
1023 if (r->length == 2) {
1024 l += ((int32_t)*p++) << 16;
1025 l += ((int32_t)*p++) << 24;
1029 /* If the relocation is internal add to the current section
1030 offset. Otherwise the only value we need is the symbol
1031 offset which we already have. The linker takes care
1032 of the rest of the address. */
1033 if (!r->ext) {
1034 /* generate final address by section address and offset */
1035 s2 = get_section_by_index(r->snum);
1036 if(s2)
1037 l += s2->addr; // else what?!?
1040 /* write new offset back */
1041 if (r->length == 2)
1042 WRITELONG(q, l);
1043 else if (r->length == 1)
1044 WRITESHORT(q, l);
1045 else
1046 *q++ = l & 0xFF;
1048 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1051 /* dump the section data to file */
1052 saa_fpwrite(s->data, machofp);
1055 /* pad last section up to reloc entries on int32_t boundary */
1056 fwritezero(rel_padcnt, machofp);
1058 /* emit relocation entries */
1059 for (s = sects; s != NULL; s = s->next)
1060 macho_write_relocs (s->relocs);
1063 /* Write out the symbol table. We should already have sorted this
1064 before now. */
1065 static void macho_write_symtab (void)
1067 struct symbol *sym;
1068 struct section *s;
1069 int32_t fi;
1070 uint32_t i;
1072 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1074 for (sym = syms; sym != NULL; sym = sym->next) {
1075 if ((sym->type & N_EXT) == 0) {
1076 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1077 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1078 fwrite(&sym->sect, 1, 1, machofp); /* section */
1079 fwriteint16_t(sym->desc, machofp); /* description */
1081 /* Fix up the symbol value now that we know the final section
1082 sizes. */
1083 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1084 for (s = sects, fi = 1;
1085 s != NULL && fi < sym->sect; s = s->next, ++fi)
1086 sym->value += s->size;
1089 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1093 for (i = 0; i < nextdefsym; i++) {
1094 sym = extdefsyms[i];
1095 fwriteint32_t(sym->strx, machofp);
1096 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1097 fwrite(&sym->sect, 1, 1, machofp); /* section */
1098 fwriteint16_t(sym->desc, machofp); /* description */
1100 /* Fix up the symbol value now that we know the final section
1101 sizes. */
1102 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1103 for (s = sects, fi = 1;
1104 s != NULL && fi < sym->sect; s = s->next, ++fi)
1105 sym->value += s->size;
1108 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1111 for (i = 0; i < nundefsym; i++) {
1112 sym = undefsyms[i];
1113 fwriteint32_t(sym->strx, machofp);
1114 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1115 fwrite(&sym->sect, 1, 1, machofp); /* section */
1116 fwriteint16_t(sym->desc, machofp); /* description */
1118 /* Fix up the symbol value now that we know the final section
1119 sizes. */
1120 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1121 for (s = sects, fi = 1;
1122 s != NULL && fi < sym->sect; s = s->next, ++fi)
1123 sym->value += s->size;
1126 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1130 /* Fixup the snum in the relocation entries, we should be
1131 doing this only for externally undefined symbols. */
1132 static void macho_fixup_relocs (struct reloc *r)
1134 struct symbol *sym;
1135 uint32_t i;
1137 while (r != NULL) {
1138 if (r->ext) {
1139 for (i = 0; i < nundefsym; i++) {
1140 sym = undefsyms[i];
1141 if (sym->initial_snum == r->snum) {
1142 r->snum = sym->snum;
1143 break;
1147 r = r->next;
1151 /* Write out the object file. */
1153 static void macho_write (void)
1155 uint32_t offset = 0;
1157 /* mach-o object file structure:
1159 ** mach header
1160 ** uint32_t magic
1161 ** int cpu type
1162 ** int cpu subtype
1163 ** uint32_t mach file type
1164 ** uint32_t number of load commands
1165 ** uint32_t size of all load commands
1166 ** (includes section struct size of segment command)
1167 ** uint32_t flags
1169 ** segment command
1170 ** uint32_t command type == LC_SEGMENT
1171 ** uint32_t size of load command
1172 ** (including section load commands)
1173 ** char[16] segment name
1174 ** uint32_t in-memory offset
1175 ** uint32_t in-memory size
1176 ** uint32_t in-file offset to data area
1177 ** uint32_t in-file size
1178 ** (in-memory size excluding zerofill sections)
1179 ** int maximum vm protection
1180 ** int initial vm protection
1181 ** uint32_t number of sections
1182 ** uint32_t flags
1184 ** section commands
1185 ** char[16] section name
1186 ** char[16] segment name
1187 ** uint32_t in-memory offset
1188 ** uint32_t in-memory size
1189 ** uint32_t in-file offset
1190 ** uint32_t alignment
1191 ** (irrelevant in MH_OBJECT)
1192 ** uint32_t in-file offset of relocation entires
1193 ** uint32_t number of relocations
1194 ** uint32_t flags
1195 ** uint32_t reserved
1196 ** uint32_t reserved
1198 ** symbol table command
1199 ** uint32_t command type == LC_SYMTAB
1200 ** uint32_t size of load command
1201 ** uint32_t symbol table offset
1202 ** uint32_t number of symbol table entries
1203 ** uint32_t string table offset
1204 ** uint32_t string table size
1206 ** raw section data
1208 ** padding to int32_t boundary
1210 ** relocation data (struct reloc)
1211 ** int32_t offset
1212 ** uint data (symbolnum, pcrel, length, extern, type)
1214 ** symbol table data (struct nlist)
1215 ** int32_t string table entry number
1216 ** uint8_t type
1217 ** (extern, absolute, defined in section)
1218 ** uint8_t section
1219 ** (0 for global symbols, section number of definition (>= 1, <=
1220 ** 254) for local symbols, size of variable for common symbols
1221 ** [type == extern])
1222 ** int16_t description
1223 ** (for stab debugging format)
1224 ** uint32_t value (i.e. file offset) of symbol or stab offset
1226 ** string table data
1227 ** list of null-terminated strings
1230 /* Emit the Mach-O header. */
1231 macho_write_header();
1233 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1235 /* emit the segment load command */
1236 if (seg_nsects > 0)
1237 offset = macho_write_segment (offset);
1238 else
1239 error(ERR_WARNING, "no sections?");
1241 if (nsyms > 0) {
1242 /* write out symbol command */
1243 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1244 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1245 fwriteint32_t(offset, machofp); /* symbol table offset */
1246 fwriteint32_t(nsyms, machofp); /* number of symbol
1247 ** table entries */
1249 offset += nsyms * MACHO_NLIST_SIZE;
1250 fwriteint32_t(offset, machofp); /* string table offset */
1251 fwriteint32_t(strslen, machofp); /* string table size */
1254 /* emit section data */
1255 if (seg_nsects > 0)
1256 macho_write_section ();
1258 /* emit symbol table if we have symbols */
1259 if (nsyms > 0)
1260 macho_write_symtab ();
1262 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1264 /* emit string table */
1265 saa_fpwrite(strs, machofp);
1267 /* We do quite a bit here, starting with finalizing all of the data
1268 for the object file, writing, and then freeing all of the data from
1269 the file. */
1271 static void macho_cleanup(int debuginfo)
1273 struct section *s;
1274 struct reloc *r;
1275 struct symbol *sym;
1277 (void)debuginfo;
1279 /* Sort all symbols. */
1280 macho_layout_symbols (&nsyms, &strslen);
1282 /* Fixup relocation entries */
1283 for (s = sects; s != NULL; s = s->next) {
1284 macho_fixup_relocs (s->relocs);
1287 /* First calculate and finalize needed values. */
1288 macho_calculate_sizes();
1289 macho_write();
1291 /* done - yay! */
1292 fclose(machofp);
1294 /* free up everything */
1295 while (sects->next) {
1296 s = sects;
1297 sects = sects->next;
1299 saa_free(s->data);
1300 while (s->relocs != NULL) {
1301 r = s->relocs;
1302 s->relocs = s->relocs->next;
1303 nasm_free(r);
1306 nasm_free(s);
1309 saa_free(strs);
1310 raa_free(extsyms);
1312 if (syms) {
1313 while (syms->next) {
1314 sym = syms;
1315 syms = syms->next;
1317 nasm_free (sym);
1322 /* Debugging routines. */
1323 static void debug_reloc (struct reloc *r)
1325 fprintf (stdout, "reloc:\n");
1326 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1327 fprintf (stdout, "\tsnum: %d\n", r->snum);
1328 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1329 fprintf (stdout, "\tlength: %d\n", r->length);
1330 fprintf (stdout, "\text: %d\n", r->ext);
1331 fprintf (stdout, "\ttype: %d\n", r->type);
1334 static void debug_section_relocs (struct section *s)
1336 struct reloc *r = s->relocs;
1338 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1340 while (r != NULL) {
1341 debug_reloc (r);
1342 r = r->next;
1346 struct ofmt of_macho = {
1347 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",
1348 "macho",
1349 NULL,
1350 null_debug_arr,
1351 &null_debug_form,
1352 macho_stdmac,
1353 macho_init,
1354 macho_setinfo,
1355 macho_output,
1356 macho_symdef,
1357 macho_section,
1358 macho_segbase,
1359 macho_directive,
1360 macho_filename,
1361 macho_cleanup
1364 #endif
1367 * Local Variables:
1368 * mode:c
1369 * c-basic-offset:4
1370 * End:
1372 * end of file */