nasm.spec.in: add fontconfig as a built requirement
[nasm.git] / output / outmacho.c
blob76de154a5b7e7fe034d25d4e396c1f9c08974bb5
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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 * outmacho.c output routines for the Netwide Assembler to produce
36 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "labels.h"
49 #include "error.h"
50 #include "saa.h"
51 #include "raa.h"
52 #include "rbtree.h"
53 #include "outform.h"
54 #include "outlib.h"
56 #if defined(OF_MACHO) || defined(OF_MACHO64)
58 /* Mach-O in-file header structure sizes */
59 #define MACHO_HEADER_SIZE 28
60 #define MACHO_SEGCMD_SIZE 56
61 #define MACHO_SECTCMD_SIZE 68
62 #define MACHO_SYMCMD_SIZE 24
63 #define MACHO_NLIST_SIZE 12
64 #define MACHO_RELINFO_SIZE 8
66 #define MACHO_HEADER64_SIZE 32
67 #define MACHO_SEGCMD64_SIZE 72
68 #define MACHO_SECTCMD64_SIZE 80
69 #define MACHO_NLIST64_SIZE 16
71 /* Mach-O file header values */
72 #define MH_MAGIC 0xfeedface
73 #define MH_MAGIC_64 0xfeedfacf
74 #define CPU_TYPE_I386 7 /* x86 platform */
75 #define CPU_TYPE_X86_64 0x01000007 /* x86-64 platform */
76 #define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */
77 #define MH_OBJECT 0x1 /* object file */
79 /* Mach-O header flags */
80 #define MH_SUBSECTIONS_VIA_SYMBOLS 0x2000
82 /* Mach-O load commands */
83 #define LC_SEGMENT 0x1 /* 32-bit segment load cmd */
84 #define LC_SEGMENT_64 0x19 /* 64-bit segment load cmd */
85 #define LC_SYMTAB 0x2 /* symbol table load command */
87 /* Mach-O relocations numbers */
89 /* Generic relocs, used by i386 Mach-O */
90 #define GENERIC_RELOC_VANILLA 0 /* Generic relocation */
91 #define GENERIC_RELOC_TLV 5 /* Thread local */
93 #define X86_64_RELOC_UNSIGNED 0 /* Absolute address */
94 #define X86_64_RELOC_SIGNED 1 /* Signed 32-bit disp */
95 #define X86_64_RELOC_BRANCH 2 /* CALL/JMP with 32-bit disp */
96 #define X86_64_RELOC_GOT_LOAD 3 /* MOVQ of GOT entry */
97 #define X86_64_RELOC_GOT 4 /* Different GOT entry */
98 #define X86_64_RELOC_SUBTRACTOR 5 /* Subtracting two symbols */
99 #define X86_64_RELOC_SIGNED_1 6 /* SIGNED with -1 addend */
100 #define X86_64_RELOC_SIGNED_2 7 /* SIGNED with -2 addend */
101 #define X86_64_RELOC_SIGNED_4 8 /* SIGNED with -4 addend */
102 #define X86_64_RELOC_TLV 9 /* Thread local */
104 /* Mach-O VM permission constants */
105 #define VM_PROT_NONE (0x00)
106 #define VM_PROT_READ (0x01)
107 #define VM_PROT_WRITE (0x02)
108 #define VM_PROT_EXECUTE (0x04)
110 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
111 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
113 /* Our internal relocation types */
114 enum reltype {
115 RL_ABS, /* Absolute relocation */
116 RL_REL, /* Relative relocation */
117 RL_TLV, /* Thread local */
118 RL_BRANCH, /* Relative direct branch */
119 RL_SUB, /* X86_64_RELOC_SUBTRACT */
120 RL_GOT, /* X86_64_RELOC_GOT */
121 RL_GOTLOAD /* X86_64_RELOC_GOT_LOAD */
123 #define RL_MAX_32 RL_TLV
124 #define RL_MAX_64 RL_GOTLOAD
126 struct macho_fmt {
127 uint32_t ptrsize; /* Pointer size in bytes */
128 uint32_t mh_magic; /* Which magic number to use */
129 uint32_t cpu_type; /* Which CPU type */
130 uint32_t lc_segment; /* Which segment load command */
131 uint32_t header_size; /* Header size */
132 uint32_t segcmd_size; /* Segment command size */
133 uint32_t sectcmd_size; /* Section command size */
134 uint32_t nlist_size; /* Nlist (symbol) size */
135 enum reltype maxreltype; /* Maximum entry in enum reltype permitted */
136 uint32_t reloc_abs; /* Absolute relocation type */
137 uint32_t reloc_rel; /* Relative relocation type */
138 uint32_t reloc_tlv; /* Thread local relocation type */
141 static struct macho_fmt fmt;
143 static void fwriteptr(uint64_t data, FILE * fp)
145 fwriteaddr(data, fmt.ptrsize, fp);
148 struct section {
149 /* nasm internal data */
150 struct section *next;
151 struct SAA *data;
152 int32_t index;
153 int32_t fileindex;
154 struct reloc *relocs;
155 struct rbtree *gsyms; /* Global symbols in section */
156 int align;
157 bool by_name; /* This section was specified by full MachO name */
159 /* data that goes into the file */
160 char sectname[16]; /* what this section is called */
161 char segname[16]; /* segment this section will be in */
162 uint64_t addr; /* in-memory address (subject to alignment) */
163 uint64_t size; /* in-memory and -file size */
164 uint64_t offset; /* in-file offset */
165 uint32_t pad; /* padding bytes before section */
166 uint32_t nreloc; /* relocation entry count */
167 uint32_t flags; /* type and attributes (masked) */
168 uint32_t extreloc; /* external relocations */
171 #define SECTION_TYPE 0x000000ff /* section type mask */
173 #define S_REGULAR (0x0) /* standard section */
174 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
176 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
177 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
178 machine instructions */
179 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external relocation entries */
180 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local relocation entries */
181 #define S_ATTR_DEBUG 0x02000000
182 #define S_ATTR_SELF_MODIFYING_CODE 0x04000000
183 #define S_ATTR_LIVE_SUPPORT 0x08000000
184 #define S_ATTR_NO_DEAD_STRIP 0x10000000 /* no dead stripping */
185 #define S_ATTR_STRIP_STATIC_SYMS 0x20000000
186 #define S_ATTR_NO_TOC 0x40000000
187 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure machine instructions */
189 #define S_NASM_TYPE_MASK 0x800004ff /* we consider these bits "section type" */
191 /* fake section for absolute symbols, *not* part of the section linked list */
192 static struct section absolute_sect;
194 struct reloc {
195 /* nasm internal data */
196 struct reloc *next;
198 /* data that goes into the file */
199 int32_t addr; /* op's offset in section */
200 uint32_t snum:24, /* contains symbol index if
201 ** ext otherwise in-file
202 ** section number */
203 pcrel:1, /* relative relocation */
204 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
205 ext:1, /* external symbol referenced */
206 type:4; /* reloc type */
209 #define R_ABS 0 /* absolute relocation */
210 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
211 ** highest bit == 1 */
213 struct symbol {
214 /* nasm internal data */
215 struct rbtree symv; /* Global symbol rbtree; "key" contains the
216 symbol offset. */
217 struct symbol *next; /* next symbol in the list */
218 char *name; /* name of this symbol */
219 int32_t initial_snum; /* symbol number used above in reloc */
220 int32_t snum; /* true snum for reloc */
222 /* data that goes into the file */
223 uint32_t strx; /* string table index */
224 uint8_t type; /* symbol type */
225 uint8_t sect; /* NO_SECT or section number */
226 uint16_t desc; /* for stab debugging, 0 for us */
229 /* symbol type bits */
230 #define N_EXT 0x01 /* global or external symbol */
232 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
233 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
234 #define N_SECT 0xe /* defined symbol, n_sect holds
235 ** section number */
237 #define N_TYPE 0x0e /* type bit mask */
239 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
241 /* special section number values */
242 #define NO_SECT 0 /* no section, invalid */
243 #define MAX_SECT 255 /* maximum number of sections */
245 static struct section *sects, **sectstail, **sectstab;
246 static struct symbol *syms, **symstail;
247 static uint32_t nsyms;
249 /* These variables are set by macho_layout_symbols() to organize
250 the symbol table and string table in order the dynamic linker
251 expects. They are then used in macho_write() to put out the
252 symbols and strings in that order.
254 The order of the symbol table is:
255 local symbols
256 defined external symbols (sorted by name)
257 undefined external symbols (sorted by name)
259 The order of the string table is:
260 strings for external symbols
261 strings for local symbols
263 static uint32_t ilocalsym = 0;
264 static uint32_t iextdefsym = 0;
265 static uint32_t iundefsym = 0;
266 static uint32_t nlocalsym;
267 static uint32_t nextdefsym;
268 static uint32_t nundefsym;
269 static struct symbol **extdefsyms = NULL;
270 static struct symbol **undefsyms = NULL;
272 static struct RAA *extsyms;
273 static struct SAA *strs;
274 static uint32_t strslen;
276 /* Global file information. This should be cleaned up into either
277 a structure or as function arguments. */
278 static uint32_t head_ncmds = 0;
279 static uint32_t head_sizeofcmds = 0;
280 static uint32_t head_flags = 0;
281 static uint64_t seg_filesize = 0;
282 static uint64_t seg_vmsize = 0;
283 static uint32_t seg_nsects = 0;
284 static uint64_t rel_padcnt = 0;
286 #define xstrncpy(xdst, xsrc) \
287 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
288 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
289 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
291 #define alignint32_t(x) \
292 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
294 #define alignint64_t(x) \
295 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
297 #define alignptr(x) \
298 ALIGN(x, fmt.ptrsize) /* align x to output format width */
300 static struct section *get_section_by_name(const char *segname,
301 const char *sectname)
303 struct section *s;
305 for (s = sects; s != NULL; s = s->next)
306 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
307 break;
309 return s;
312 static struct section *get_section_by_index(const int32_t index)
314 struct section *s;
316 for (s = sects; s != NULL; s = s->next)
317 if (index == s->index)
318 break;
320 return s;
324 * Special section numbers which are used to define Mach-O special
325 * symbols, which can be used with WRT to provide PIC relocation
326 * types.
328 static int32_t macho_tlvp_sect;
329 static int32_t macho_gotpcrel_sect;
331 static void macho_init(void)
333 sects = NULL;
334 sectstail = &sects;
336 /* Fake section for absolute symbols */
337 absolute_sect.index = NO_SEG;
339 syms = NULL;
340 symstail = &syms;
341 nsyms = 0;
342 nlocalsym = 0;
343 nextdefsym = 0;
344 nundefsym = 0;
346 extsyms = raa_init();
347 strs = saa_init(1L);
349 /* string table starts with a zero byte so index 0 is an empty string */
350 saa_wbytes(strs, zero_buffer, 1);
351 strslen = 1;
353 /* add special symbol for TLVP */
354 macho_tlvp_sect = seg_alloc() + 1;
355 define_label("..tlvp", macho_tlvp_sect, 0L, NULL, false, false);
359 static void sect_write(struct section *sect,
360 const uint8_t *data, uint32_t len)
362 saa_wbytes(sect->data, data, len);
363 sect->size += len;
367 * Find a suitable global symbol for a ..gotpcrel or ..tlvp reference
369 static struct symbol *macho_find_gsym(struct section *s,
370 uint64_t offset, bool exact)
372 struct rbtree *srb;
374 srb = rb_search(s->gsyms, offset);
376 if (!srb || (exact && srb->key != offset)) {
377 nasm_error(ERR_NONFATAL, "unable to find a suitable %s symbol"
378 " for this reference",
379 s == &absolute_sect ? "absolute" : "global");
380 return NULL;
383 return container_of(srb, struct symbol, symv);
386 static int64_t add_reloc(struct section *sect, int32_t section,
387 int64_t offset,
388 enum reltype reltype, int bytes)
390 struct reloc *r;
391 struct section *s;
392 int32_t fi;
393 int64_t adjust;
395 /* Double check this is a valid relocation type for this platform */
396 nasm_assert(reltype <= fmt.maxreltype);
398 /* the current end of the section will be the symbol's address for
399 ** now, might have to be fixed by macho_fixup_relocs() later on. make
400 ** sure we don't make the symbol scattered by setting the highest
401 ** bit by accident */
402 r = nasm_malloc(sizeof(struct reloc));
403 r->addr = sect->size & ~R_SCATTERED;
404 r->ext = 1;
405 adjust = bytes;
407 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
408 r->length = ilog2_32(bytes);
410 /* set default relocation values */
411 r->type = fmt.reloc_abs;
412 r->pcrel = 0;
413 r->snum = R_ABS;
415 s = NULL;
416 if (section != NO_SEG)
417 s = get_section_by_index(section);
418 fi = s ? s->fileindex : NO_SECT;
420 /* absolute relocation */
421 switch (reltype) {
422 case RL_ABS:
423 if (section == NO_SEG) {
424 /* absolute (can this even happen?) */
425 r->ext = 0;
426 r->snum = R_ABS;
427 } else if (fi == NO_SECT) {
428 /* external */
429 r->snum = raa_read(extsyms, section);
430 } else {
431 /* local */
432 r->ext = 0;
433 r->snum = fi;
434 adjust = -sect->size;
436 break;
438 case RL_REL:
439 case RL_BRANCH:
440 r->type = fmt.reloc_rel;
441 r->pcrel = 1;
442 if (section == NO_SEG) {
443 /* absolute - seems to produce garbage no matter what */
444 nasm_error(ERR_NONFATAL, "Mach-O does not support relative "
445 "references to absolute addresses");
446 goto bail;
447 #if 0
448 /* This "seems" to be how it ought to work... */
450 struct symbol *sym = macho_find_gsym(&absolute_sect,
451 offset, false);
452 if (!sym)
453 goto bail;
455 sect->extreloc = 1;
456 r->snum = NO_SECT;
457 adjust = -sect->size;
458 #endif
459 } else if (fi == NO_SECT) {
460 /* external */
461 sect->extreloc = 1;
462 r->snum = raa_read(extsyms, section);
463 if (reltype == RL_BRANCH)
464 r->type = X86_64_RELOC_BRANCH;
465 else if (r->type == GENERIC_RELOC_VANILLA)
466 adjust = -sect->size;
467 } else {
468 /* local */
469 r->ext = 0;
470 r->snum = fi;
471 adjust = -sect->size;
473 break;
475 case RL_SUB:
476 r->pcrel = 0;
477 r->type = X86_64_RELOC_SUBTRACTOR;
478 break;
480 case RL_GOT:
481 r->type = X86_64_RELOC_GOT;
482 goto needsym;
484 case RL_GOTLOAD:
485 r->type = X86_64_RELOC_GOT_LOAD;
486 goto needsym;
488 case RL_TLV:
489 r->type = fmt.reloc_tlv;
490 goto needsym;
492 needsym:
493 r->pcrel = 1;
494 if (section == NO_SEG) {
495 nasm_error(ERR_NONFATAL, "Unsupported use of use of WRT");
496 } else if (fi == NO_SECT) {
497 /* external */
498 r->snum = raa_read(extsyms, section);
499 } else {
500 /* internal */
501 struct symbol *sym = macho_find_gsym(s, offset, reltype != RL_TLV);
502 if (!sym)
503 goto bail;
504 r->snum = sym->initial_snum;
506 break;
509 /* NeXT as puts relocs in reversed order (address-wise) into the
510 ** files, so we do the same, doesn't seem to make much of a
511 ** difference either way */
512 r->next = sect->relocs;
513 sect->relocs = r;
514 if (r->ext)
515 sect->extreloc = 1;
516 ++sect->nreloc;
518 return adjust;
520 bail:
521 nasm_free(r);
522 return 0;
525 static void macho_output(int32_t secto, const void *data,
526 enum out_type type, uint64_t size,
527 int32_t section, int32_t wrt)
529 struct section *s;
530 int64_t addr, offset;
531 uint8_t mydata[16], *p;
532 bool is_bss;
533 enum reltype reltype;
535 if (secto == NO_SEG) {
536 if (type != OUT_RESERVE)
537 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
538 "[ABSOLUTE] space");
539 return;
542 s = get_section_by_index(secto);
544 if (s == NULL) {
545 nasm_error(ERR_WARNING, "attempt to assemble code in"
546 " section %d: defaulting to `.text'", secto);
547 s = get_section_by_name("__TEXT", "__text");
549 /* should never happen */
550 if (s == NULL)
551 nasm_panic(0, "text section not found");
554 is_bss = (s->flags & SECTION_TYPE) == S_ZEROFILL;
556 if (is_bss && type != OUT_RESERVE) {
557 nasm_error(ERR_WARNING, "attempt to initialize memory in "
558 "BSS section: ignored");
559 s->size += realsize(type, size);
560 return;
563 memset(mydata, 0, sizeof(mydata));
565 switch (type) {
566 case OUT_RESERVE:
567 if (!is_bss) {
568 nasm_error(ERR_WARNING, "uninitialized space declared in"
569 " %s,%s section: zeroing", s->segname, s->sectname);
571 sect_write(s, NULL, size);
572 } else
573 s->size += size;
575 break;
577 case OUT_RAWDATA:
578 if (section != NO_SEG)
579 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
581 sect_write(s, data, size);
582 break;
584 case OUT_ADDRESS:
586 int asize = abs((int)size);
588 addr = *(int64_t *)data;
589 if (section != NO_SEG) {
590 if (section % 2) {
591 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
592 " section base references");
593 } else if (wrt == NO_SEG) {
594 if (fmt.ptrsize == 8 && asize != 8) {
595 nasm_error(ERR_NONFATAL,
596 "Mach-O 64-bit format does not support"
597 " 32-bit absolute addresses");
598 } else {
599 add_reloc(s, section, addr, RL_ABS, asize);
601 } else {
602 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
603 " this use of WRT");
607 p = mydata;
608 WRITEADDR(p, addr, asize);
609 sect_write(s, mydata, asize);
610 break;
613 case OUT_REL2ADR:
614 nasm_assert(section != secto);
616 p = mydata;
617 offset = *(int64_t *)data;
618 addr = offset - size;
620 if (section != NO_SEG && section % 2) {
621 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
622 " section base references");
623 } else if (fmt.ptrsize == 8) {
624 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
625 " Macho-O relocation [2]");
626 } else if (wrt != NO_SEG) {
627 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
628 " this use of WRT");
629 wrt = NO_SEG; /* we can at least _try_ to continue */
630 } else {
631 addr += add_reloc(s, section, addr+size, RL_REL, 2);
634 WRITESHORT(p, addr);
635 sect_write(s, mydata, 2);
636 break;
638 case OUT_REL4ADR:
639 nasm_assert(section != secto);
641 p = mydata;
642 offset = *(int64_t *)data;
643 addr = offset - size;
644 reltype = RL_REL;
646 if (section != NO_SEG && section % 2) {
647 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
648 " section base references");
649 } else if (wrt == NO_SEG) {
650 if (fmt.ptrsize == 8 &&
651 (s->flags & S_ATTR_SOME_INSTRUCTIONS)) {
652 uint8_t opcode[2];
654 opcode[0] = opcode[1] = 0;
656 /* HACK: Retrieve instruction opcode */
657 if (likely(s->data->datalen >= 2)) {
658 saa_fread(s->data, s->data->datalen-2, opcode, 2);
659 } else if (s->data->datalen == 1) {
660 saa_fread(s->data, 0, opcode+1, 1);
663 if ((opcode[0] != 0x0f && (opcode[1] & 0xfe) == 0xe8) ||
664 (opcode[0] == 0x0f && (opcode[1] & 0xf0) == 0x80)) {
665 /* Direct call, jmp, or jcc */
666 reltype = RL_BRANCH;
669 } else if (wrt == macho_gotpcrel_sect) {
670 reltype = RL_GOT;
672 if ((s->flags & S_ATTR_SOME_INSTRUCTIONS) &&
673 s->data->datalen >= 3) {
674 uint8_t gotload[3];
676 /* HACK: Retrieve instruction opcode */
677 saa_fread(s->data, s->data->datalen-3, gotload, 3);
678 if ((gotload[0] & 0xf8) == 0x48 &&
679 gotload[1] == 0x8b &&
680 (gotload[2] & 0307) == 0005) {
681 /* movq <reg>,[rel sym wrt ..gotpcrel] */
682 reltype = RL_GOTLOAD;
685 } else if (wrt == macho_tlvp_sect) {
686 reltype = RL_TLV;
687 } else {
688 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
689 " this use of WRT");
690 /* continue with RL_REL */
693 addr += add_reloc(s, section, offset, reltype, 4);
694 WRITELONG(p, addr);
695 sect_write(s, mydata, 4);
696 break;
698 default:
699 nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
700 break;
704 /* Translation table from traditional Unix section names to Mach-O */
705 static const struct sectmap {
706 const char *nasmsect;
707 const char *segname;
708 const char *sectname;
709 const uint32_t flags;
710 } sectmap[] = {
711 {".text", "__TEXT", "__text",
712 S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
713 {".data", "__DATA", "__data", S_REGULAR},
714 {".rodata", "__DATA", "__const", S_REGULAR},
715 {".bss", "__DATA", "__bss", S_ZEROFILL},
716 {NULL, NULL, NULL, 0}
719 #define NO_TYPE S_NASM_TYPE_MASK
721 /* Section type or attribute directives */
722 static const struct sect_attribs {
723 const char *name;
724 uint32_t flags;
725 } sect_attribs[] = {
726 { "data", S_REGULAR },
727 { "code", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS },
728 { "mixed", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS },
729 { "bss", S_ZEROFILL },
730 { "zerofill", S_ZEROFILL },
731 { "no_dead_strip", NO_TYPE|S_ATTR_NO_DEAD_STRIP },
732 { "live_support", NO_TYPE|S_ATTR_LIVE_SUPPORT },
733 { "strip_static_syms", NO_TYPE|S_ATTR_STRIP_STATIC_SYMS },
734 { NULL, 0 }
737 static int32_t macho_section(char *name, int pass, int *bits)
739 char *sectionAttributes;
740 const struct sectmap *sm;
741 struct section *s;
742 const char *section, *segment;
743 uint32_t flags;
744 const struct sect_attribs *sa;
745 char *currentAttribute;
746 char *comma;
748 bool new_seg;
750 (void)pass;
752 /* Default to the appropriate number of bits. */
753 if (!name) {
754 *bits = fmt.ptrsize << 3;
755 name = ".text";
756 sectionAttributes = NULL;
757 } else {
758 sectionAttributes = name;
759 name = nasm_strsep(&sectionAttributes, " \t");
762 section = segment = NULL;
763 flags = 0;
765 comma = strchr(name, ',');
766 if (comma) {
767 int len;
769 *comma = '\0';
770 segment = name;
771 section = comma+1;
773 len = strlen(segment);
774 if (len == 0) {
775 nasm_error(ERR_NONFATAL, "empty segment name\n");
776 } else if (len >= 16) {
777 nasm_error(ERR_NONFATAL, "segment name %s too long\n", segment);
780 len = strlen(section);
781 if (len == 0) {
782 nasm_error(ERR_NONFATAL, "empty section name\n");
783 } else if (len >= 16) {
784 nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
787 if (!strcmp(section, "__text")) {
788 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
789 S_ATTR_PURE_INSTRUCTIONS;
790 } else if (!strcmp(section, "__bss")) {
791 flags = S_ZEROFILL;
792 } else {
793 flags = S_REGULAR;
795 } else {
796 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
797 /* make lookup into section name translation table */
798 if (!strcmp(name, sm->nasmsect)) {
799 segment = sm->segname;
800 section = sm->sectname;
801 flags = sm->flags;
802 goto found;
805 nasm_error(ERR_NONFATAL, "unknown section name\n");
806 return NO_SEG;
809 found:
810 /* try to find section with that name */
811 s = get_section_by_name(segment, section);
813 /* create it if it doesn't exist yet */
814 if (!s) {
815 new_seg = true;
817 s = *sectstail = nasm_zalloc(sizeof(struct section));
818 sectstail = &s->next;
820 s->data = saa_init(1L);
821 s->index = seg_alloc();
822 s->fileindex = ++seg_nsects;
823 s->align = -1;
824 s->pad = -1;
825 s->offset = -1;
826 s->by_name = false;
828 xstrncpy(s->segname, segment);
829 xstrncpy(s->sectname, section);
830 s->size = 0;
831 s->nreloc = 0;
832 s->flags = flags;
833 } else {
834 new_seg = false;
837 if (comma)
838 *comma = ','; /* Restore comma */
840 s->by_name = s->by_name || comma; /* Was specified by name */
842 flags = NO_TYPE;
844 while (sectionAttributes &&
845 (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
846 if (!*currentAttribute)
847 continue;
849 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
850 char *end;
851 int newAlignment, value;
853 value = strtoul(currentAttribute + 6, (char**)&end, 0);
854 newAlignment = alignlog2_32(value);
856 if (0 != *end) {
857 nasm_error(ERR_NONFATAL,
858 "unknown or missing alignment value \"%s\" "
859 "specified for section \"%s\"",
860 currentAttribute + 6,
861 name);
862 } else if (0 > newAlignment) {
863 nasm_error(ERR_NONFATAL,
864 "alignment of %d (for section \"%s\") is not "
865 "a power of two",
866 value,
867 name);
870 if (s->align < newAlignment)
871 s->align = newAlignment;
872 } else {
873 for (sa = sect_attribs; sa->name; sa++) {
874 if (!nasm_stricmp(sa->name, currentAttribute)) {
875 if ((sa->flags & S_NASM_TYPE_MASK) != NO_TYPE) {
876 flags = (flags & ~S_NASM_TYPE_MASK)
877 | (sa->flags & S_NASM_TYPE_MASK);
879 flags |= sa->flags & ~S_NASM_TYPE_MASK;
880 break;
884 if (!sa->name) {
885 nasm_error(ERR_NONFATAL,
886 "unknown section attribute %s for section %s",
887 currentAttribute, name);
892 if ((flags & S_NASM_TYPE_MASK) != NO_TYPE) {
893 if (!new_seg && ((s->flags ^ flags) & S_NASM_TYPE_MASK)) {
894 nasm_error(ERR_NONFATAL,
895 "inconsistent section attributes for section %s\n",
896 name);
897 } else {
898 s->flags = (s->flags & ~S_NASM_TYPE_MASK) | flags;
900 } else {
901 s->flags |= flags & ~S_NASM_TYPE_MASK;
904 return s->index;
907 static void macho_symdef(char *name, int32_t section, int64_t offset,
908 int is_global, char *special)
910 struct symbol *sym;
912 if (special) {
913 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
914 "not support any special symbol types");
915 return;
918 if (is_global == 3) {
919 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
920 "(yet) support forward reference fixups.");
921 return;
924 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
926 * This is a NASM special symbol. We never allow it into
927 * the Macho-O symbol table, even if it's a valid one. If it
928 * _isn't_ a valid one, we should barf immediately.
930 if (strcmp(name, "..gotpcrel") && strcmp(name, "..tlvp"))
931 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
932 return;
935 sym = *symstail = nasm_zalloc(sizeof(struct symbol));
936 sym->next = NULL;
937 symstail = &sym->next;
939 sym->name = name;
940 sym->strx = strslen;
941 sym->type = 0;
942 sym->desc = 0;
943 sym->symv.key = offset;
944 sym->initial_snum = -1;
946 /* external and common symbols get N_EXT */
947 if (is_global != 0) {
948 sym->type |= N_EXT;
951 if (section == NO_SEG) {
952 /* symbols in no section get absolute */
953 sym->type |= N_ABS;
954 sym->sect = NO_SECT;
956 /* all absolute symbols are available to use as references */
957 absolute_sect.gsyms = rb_insert(absolute_sect.gsyms, &sym->symv);
958 } else {
959 struct section *s = get_section_by_index(section);
961 sym->type |= N_SECT;
963 /* get the in-file index of the section the symbol was defined in */
964 sym->sect = s ? s->fileindex : NO_SECT;
966 /* track the initially allocated symbol number for use in future fix-ups */
967 sym->initial_snum = nsyms;
969 if (!s) {
970 /* remember symbol number of references to external
971 ** symbols, this works because every external symbol gets
972 ** its own section number allocated internally by nasm and
973 ** can so be used as a key */
974 extsyms = raa_write(extsyms, section, nsyms);
976 switch (is_global) {
977 case 1:
978 case 2:
979 /* there isn't actually a difference between global
980 ** and common symbols, both even have their size in
981 ** sym->symv.key */
982 sym->type = N_EXT;
983 break;
985 default:
986 /* give an error on unfound section if it's not an
987 ** external or common symbol (assemble_file() does a
988 ** seg_alloc() on every call for them) */
989 nasm_panic(0, "in-file index for section %d not found, is_global = %d", section, is_global);
990 break;
992 } else if (is_global) {
993 s->gsyms = rb_insert(s->gsyms, &sym->symv);
996 ++nsyms;
999 static void macho_sectalign(int32_t seg, unsigned int value)
1001 struct section *s;
1002 int align;
1004 nasm_assert(!(seg & 1));
1006 s = get_section_by_index(seg);
1008 if (!s || !is_power2(value))
1009 return;
1011 align = alignlog2_32(value);
1012 if (s->align < align)
1013 s->align = align;
1016 static int32_t macho_segbase(int32_t section)
1018 return section;
1021 static void macho_filename(char *inname, char *outname)
1023 standard_extension(inname, outname, ".o");
1026 extern macros_t macho_stdmac[];
1028 /* Comparison function for qsort symbol layout. */
1029 static int layout_compare (const struct symbol **s1,
1030 const struct symbol **s2)
1032 return (strcmp ((*s1)->name, (*s2)->name));
1035 /* The native assembler does a few things in a similar function
1037 * Remove temporary labels
1038 * Sort symbols according to local, external, undefined (by name)
1039 * Order the string table
1041 We do not remove temporary labels right now.
1043 numsyms is the total number of symbols we have. strtabsize is the
1044 number entries in the string table. */
1046 static void macho_layout_symbols (uint32_t *numsyms,
1047 uint32_t *strtabsize)
1049 struct symbol *sym, **symp;
1050 uint32_t i,j;
1052 *numsyms = 0;
1053 *strtabsize = sizeof (char);
1055 symp = &syms;
1057 while ((sym = *symp)) {
1058 /* Undefined symbols are now external. */
1059 if (sym->type == N_UNDF)
1060 sym->type |= N_EXT;
1062 if ((sym->type & N_EXT) == 0) {
1063 sym->snum = *numsyms;
1064 *numsyms = *numsyms + 1;
1065 nlocalsym++;
1067 else {
1068 if ((sym->type & N_TYPE) != N_UNDF) {
1069 nextdefsym++;
1070 } else {
1071 nundefsym++;
1074 /* If we handle debug info we'll want
1075 to check for it here instead of just
1076 adding the symbol to the string table. */
1077 sym->strx = *strtabsize;
1078 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
1079 *strtabsize += strlen(sym->name) + 1;
1081 symp = &(sym->next);
1084 /* Next, sort the symbols. Most of this code is a direct translation from
1085 the Apple cctools symbol layout. We need to keep compatibility with that. */
1086 /* Set the indexes for symbol groups into the symbol table */
1087 ilocalsym = 0;
1088 iextdefsym = nlocalsym;
1089 iundefsym = nlocalsym + nextdefsym;
1091 /* allocate arrays for sorting externals by name */
1092 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
1093 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
1095 i = 0;
1096 j = 0;
1098 symp = &syms;
1100 while ((sym = *symp)) {
1102 if((sym->type & N_EXT) == 0) {
1103 sym->strx = *strtabsize;
1104 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
1105 *strtabsize += strlen(sym->name) + 1;
1107 else {
1108 if ((sym->type & N_TYPE) != N_UNDF) {
1109 extdefsyms[i++] = sym;
1110 } else {
1111 undefsyms[j++] = sym;
1114 symp = &(sym->next);
1117 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
1118 (int (*)(const void *, const void *))layout_compare);
1119 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
1120 (int (*)(const void *, const void *))layout_compare);
1122 for(i = 0; i < nextdefsym; i++) {
1123 extdefsyms[i]->snum = *numsyms;
1124 *numsyms += 1;
1126 for(j = 0; j < nundefsym; j++) {
1127 undefsyms[j]->snum = *numsyms;
1128 *numsyms += 1;
1132 /* Calculate some values we'll need for writing later. */
1134 static void macho_calculate_sizes (void)
1136 struct section *s;
1137 int fi;
1139 /* count sections and calculate in-memory and in-file offsets */
1140 for (s = sects; s != NULL; s = s->next) {
1141 uint64_t newaddr;
1143 /* recalculate segment address based on alignment and vm size */
1144 s->addr = seg_vmsize;
1146 /* we need section alignment to calculate final section address */
1147 if (s->align == -1)
1148 s->align = DEFAULT_SECTION_ALIGNMENT;
1150 newaddr = ALIGN(s->addr, UINT64_C(1) << s->align);
1151 s->addr = newaddr;
1153 seg_vmsize = newaddr + s->size;
1155 /* zerofill sections aren't actually written to the file */
1156 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1158 * LLVM/Xcode as always aligns the section data to 4
1159 * bytes; there is a comment in the LLVM source code that
1160 * perhaps aligning to pointer size would be better.
1162 s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
1163 s->offset = seg_filesize + s->pad;
1164 seg_filesize += s->size + s->pad;
1168 /* calculate size of all headers, load commands and sections to
1169 ** get a pointer to the start of all the raw data */
1170 if (seg_nsects > 0) {
1171 ++head_ncmds;
1172 head_sizeofcmds += fmt.segcmd_size + seg_nsects * fmt.sectcmd_size;
1175 if (nsyms > 0) {
1176 ++head_ncmds;
1177 head_sizeofcmds += MACHO_SYMCMD_SIZE;
1180 if (seg_nsects > MAX_SECT) {
1181 nasm_fatal(0, "MachO output is limited to %d sections\n",
1182 MAX_SECT);
1185 /* Create a table of sections by file index to avoid linear search */
1186 sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
1187 sectstab[NO_SECT] = &absolute_sect;
1188 for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
1189 sectstab[fi] = s;
1192 /* Write out the header information for the file. */
1194 static void macho_write_header (void)
1196 fwriteint32_t(fmt.mh_magic, ofile); /* magic */
1197 fwriteint32_t(fmt.cpu_type, ofile); /* CPU type */
1198 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1199 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1200 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
1201 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
1202 fwriteint32_t(head_flags, ofile); /* flags, if any */
1203 fwritezero(fmt.header_size - 7*4, ofile); /* reserved fields */
1206 /* Write out the segment load command at offset. */
1208 static uint32_t macho_write_segment (uint64_t offset)
1210 uint64_t rel_base = alignptr(offset + seg_filesize);
1211 uint32_t s_reloff = 0;
1212 struct section *s;
1214 fwriteint32_t(fmt.lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
1216 /* size of load command including section load commands */
1217 fwriteint32_t(fmt.segcmd_size + seg_nsects * fmt.sectcmd_size,
1218 ofile);
1220 /* in an MH_OBJECT file all sections are in one unnamed (name
1221 ** all zeros) segment */
1222 fwritezero(16, ofile);
1223 fwriteptr(0, ofile); /* in-memory offset */
1224 fwriteptr(seg_vmsize, ofile); /* in-memory size */
1225 fwriteptr(offset, ofile); /* in-file offset to data */
1226 fwriteptr(seg_filesize, ofile); /* in-file size */
1227 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1228 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1229 fwriteint32_t(seg_nsects, ofile); /* number of sections */
1230 fwriteint32_t(0, ofile); /* no flags */
1232 /* emit section headers */
1233 for (s = sects; s != NULL; s = s->next) {
1234 if (s->nreloc) {
1235 nasm_assert((s->flags & SECTION_TYPE) != S_ZEROFILL);
1236 s->flags |= S_ATTR_LOC_RELOC;
1237 if (s->extreloc)
1238 s->flags |= S_ATTR_EXT_RELOC;
1239 } else if (!strcmp(s->segname, "__DATA") &&
1240 !strcmp(s->sectname, "__const") &&
1241 !s->by_name &&
1242 !get_section_by_name("__TEXT", "__const")) {
1244 * The MachO equivalent to .rodata can be either
1245 * __DATA,__const or __TEXT,__const; the latter only if
1246 * there are no relocations. However, when mixed it is
1247 * better to specify the segments explicitly.
1249 xstrncpy(s->segname, "__TEXT");
1252 nasm_write(s->sectname, sizeof(s->sectname), ofile);
1253 nasm_write(s->segname, sizeof(s->segname), ofile);
1254 fwriteptr(s->addr, ofile);
1255 fwriteptr(s->size, ofile);
1257 /* dummy data for zerofill sections or proper values */
1258 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1259 nasm_assert(s->pad != (uint32_t)-1);
1260 offset += s->pad;
1261 fwriteint32_t(offset, ofile);
1262 offset += s->size;
1263 /* Write out section alignment, as a power of two.
1264 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1265 fwriteint32_t(s->align, ofile);
1266 /* To be compatible with cctools as we emit
1267 a zero reloff if we have no relocations. */
1268 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1269 fwriteint32_t(s->nreloc, ofile);
1271 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
1272 } else {
1273 fwriteint32_t(0, ofile);
1274 fwriteint32_t(s->align, ofile);
1275 fwriteint32_t(0, ofile);
1276 fwriteint32_t(0, ofile);
1279 fwriteint32_t(s->flags, ofile); /* flags */
1280 fwriteint32_t(0, ofile); /* reserved */
1281 fwriteptr(0, ofile); /* reserved */
1284 rel_padcnt = rel_base - offset;
1285 offset = rel_base + s_reloff;
1287 return offset;
1290 /* For a given chain of relocs r, write out the entire relocation
1291 chain to the object file. */
1293 static void macho_write_relocs (struct reloc *r)
1295 while (r) {
1296 uint32_t word2;
1298 fwriteint32_t(r->addr, ofile); /* reloc offset */
1300 word2 = r->snum;
1301 word2 |= r->pcrel << 24;
1302 word2 |= r->length << 25;
1303 word2 |= r->ext << 27;
1304 word2 |= r->type << 28;
1305 fwriteint32_t(word2, ofile); /* reloc data */
1306 r = r->next;
1310 /* Write out the section data. */
1311 static void macho_write_section (void)
1313 struct section *s;
1314 struct reloc *r;
1315 uint8_t *p;
1316 int32_t len;
1317 int64_t l;
1318 union offset {
1319 uint64_t val;
1320 uint8_t buf[8];
1321 } blk;
1323 for (s = sects; s != NULL; s = s->next) {
1324 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1325 continue;
1327 /* Like a.out Mach-O references things in the data or bss
1328 * sections by addresses which are actually relative to the
1329 * start of the _text_ section, in the _file_. See outaout.c
1330 * for more information. */
1331 saa_rewind(s->data);
1332 for (r = s->relocs; r != NULL; r = r->next) {
1333 len = (uint32_t)1 << r->length;
1334 if (len > 4) /* Can this ever be an issue?! */
1335 len = 8;
1336 blk.val = 0;
1337 saa_fread(s->data, r->addr, blk.buf, len);
1339 /* get offset based on relocation type */
1340 #ifdef WORDS_LITTLEENDIAN
1341 l = blk.val;
1342 #else
1343 l = blk.buf[0];
1344 l += ((int64_t)blk.buf[1]) << 8;
1345 l += ((int64_t)blk.buf[2]) << 16;
1346 l += ((int64_t)blk.buf[3]) << 24;
1347 l += ((int64_t)blk.buf[4]) << 32;
1348 l += ((int64_t)blk.buf[5]) << 40;
1349 l += ((int64_t)blk.buf[6]) << 48;
1350 l += ((int64_t)blk.buf[7]) << 56;
1351 #endif
1353 /* If the relocation is internal add to the current section
1354 offset. Otherwise the only value we need is the symbol
1355 offset which we already have. The linker takes care
1356 of the rest of the address. */
1357 if (!r->ext) {
1358 /* generate final address by section address and offset */
1359 nasm_assert(r->snum <= seg_nsects);
1360 l += sectstab[r->snum]->addr;
1361 if (r->pcrel)
1362 l -= s->addr;
1363 } else if (r->pcrel && r->type == GENERIC_RELOC_VANILLA) {
1364 l -= s->addr;
1367 /* write new offset back */
1368 p = blk.buf;
1369 WRITEDLONG(p, l);
1370 saa_fwrite(s->data, r->addr, blk.buf, len);
1373 /* dump the section data to file */
1374 fwritezero(s->pad, ofile);
1375 saa_fpwrite(s->data, ofile);
1378 /* pad last section up to reloc entries on pointer boundary */
1379 fwritezero(rel_padcnt, ofile);
1381 /* emit relocation entries */
1382 for (s = sects; s != NULL; s = s->next)
1383 macho_write_relocs (s->relocs);
1386 /* Write out the symbol table. We should already have sorted this
1387 before now. */
1388 static void macho_write_symtab (void)
1390 struct symbol *sym;
1391 uint64_t i;
1393 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1395 for (sym = syms; sym != NULL; sym = sym->next) {
1396 if ((sym->type & N_EXT) == 0) {
1397 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1398 nasm_write(&sym->type, 1, ofile); /* symbol type */
1399 nasm_write(&sym->sect, 1, ofile); /* section */
1400 fwriteint16_t(sym->desc, ofile); /* description */
1402 /* Fix up the symbol value now that we know the final section
1403 sizes. */
1404 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1405 nasm_assert(sym->sect <= seg_nsects);
1406 sym->symv.key += sectstab[sym->sect]->addr;
1409 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1413 for (i = 0; i < nextdefsym; i++) {
1414 sym = extdefsyms[i];
1415 fwriteint32_t(sym->strx, ofile);
1416 nasm_write(&sym->type, 1, ofile); /* symbol type */
1417 nasm_write(&sym->sect, 1, ofile); /* section */
1418 fwriteint16_t(sym->desc, ofile); /* description */
1420 /* Fix up the symbol value now that we know the final section
1421 sizes. */
1422 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1423 nasm_assert(sym->sect <= seg_nsects);
1424 sym->symv.key += sectstab[sym->sect]->addr;
1427 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1430 for (i = 0; i < nundefsym; i++) {
1431 sym = undefsyms[i];
1432 fwriteint32_t(sym->strx, ofile);
1433 nasm_write(&sym->type, 1, ofile); /* symbol type */
1434 nasm_write(&sym->sect, 1, ofile); /* section */
1435 fwriteint16_t(sym->desc, ofile); /* description */
1437 /* Fix up the symbol value now that we know the final section
1438 sizes. */
1439 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1440 nasm_assert(sym->sect <= seg_nsects);
1441 sym->symv.key += sectstab[sym->sect]->addr;
1444 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1449 /* Fixup the snum in the relocation entries, we should be
1450 doing this only for externally referenced symbols. */
1451 static void macho_fixup_relocs (struct reloc *r)
1453 struct symbol *sym;
1455 while (r != NULL) {
1456 if (r->ext) {
1457 for (sym = syms; sym != NULL; sym = sym->next) {
1458 if (sym->initial_snum == r->snum) {
1459 r->snum = sym->snum;
1460 break;
1464 r = r->next;
1468 /* Write out the object file. */
1470 static void macho_write (void)
1472 uint64_t offset = 0;
1474 /* mach-o object file structure:
1476 ** mach header
1477 ** uint32_t magic
1478 ** int cpu type
1479 ** int cpu subtype
1480 ** uint32_t mach file type
1481 ** uint32_t number of load commands
1482 ** uint32_t size of all load commands
1483 ** (includes section struct size of segment command)
1484 ** uint32_t flags
1486 ** segment command
1487 ** uint32_t command type == LC_SEGMENT[_64]
1488 ** uint32_t size of load command
1489 ** (including section load commands)
1490 ** char[16] segment name
1491 ** pointer in-memory offset
1492 ** pointer in-memory size
1493 ** pointer in-file offset to data area
1494 ** pointer in-file size
1495 ** (in-memory size excluding zerofill sections)
1496 ** int maximum vm protection
1497 ** int initial vm protection
1498 ** uint32_t number of sections
1499 ** uint32_t flags
1501 ** section commands
1502 ** char[16] section name
1503 ** char[16] segment name
1504 ** pointer in-memory offset
1505 ** pointer in-memory size
1506 ** uint32_t in-file offset
1507 ** uint32_t alignment
1508 ** (irrelevant in MH_OBJECT)
1509 ** uint32_t in-file offset of relocation entires
1510 ** uint32_t number of relocations
1511 ** uint32_t flags
1512 ** uint32_t reserved
1513 ** uint32_t reserved
1515 ** symbol table command
1516 ** uint32_t command type == LC_SYMTAB
1517 ** uint32_t size of load command
1518 ** uint32_t symbol table offset
1519 ** uint32_t number of symbol table entries
1520 ** uint32_t string table offset
1521 ** uint32_t string table size
1523 ** raw section data
1525 ** padding to pointer boundary
1527 ** relocation data (struct reloc)
1528 ** int32_t offset
1529 ** uint data (symbolnum, pcrel, length, extern, type)
1531 ** symbol table data (struct nlist)
1532 ** int32_t string table entry number
1533 ** uint8_t type
1534 ** (extern, absolute, defined in section)
1535 ** uint8_t section
1536 ** (0 for global symbols, section number of definition (>= 1, <=
1537 ** 254) for local symbols, size of variable for common symbols
1538 ** [type == extern])
1539 ** int16_t description
1540 ** (for stab debugging format)
1541 ** pointer value (i.e. file offset) of symbol or stab offset
1543 ** string table data
1544 ** list of null-terminated strings
1547 /* Emit the Mach-O header. */
1548 macho_write_header();
1550 offset = fmt.header_size + head_sizeofcmds;
1552 /* emit the segment load command */
1553 if (seg_nsects > 0)
1554 offset = macho_write_segment (offset);
1555 else
1556 nasm_error(ERR_WARNING, "no sections?");
1558 if (nsyms > 0) {
1559 /* write out symbol command */
1560 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1561 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1562 fwriteint32_t(offset, ofile); /* symbol table offset */
1563 fwriteint32_t(nsyms, ofile); /* number of symbol
1564 ** table entries */
1565 offset += nsyms * fmt.nlist_size;
1566 fwriteint32_t(offset, ofile); /* string table offset */
1567 fwriteint32_t(strslen, ofile); /* string table size */
1570 /* emit section data */
1571 if (seg_nsects > 0)
1572 macho_write_section ();
1574 /* emit symbol table if we have symbols */
1575 if (nsyms > 0)
1576 macho_write_symtab ();
1578 /* we don't need to pad here, we are already aligned */
1580 /* emit string table */
1581 saa_fpwrite(strs, ofile);
1583 /* We do quite a bit here, starting with finalizing all of the data
1584 for the object file, writing, and then freeing all of the data from
1585 the file. */
1587 static void macho_cleanup(void)
1589 struct section *s;
1590 struct reloc *r;
1591 struct symbol *sym;
1593 /* Sort all symbols. */
1594 macho_layout_symbols (&nsyms, &strslen);
1596 /* Fixup relocation entries */
1597 for (s = sects; s != NULL; s = s->next) {
1598 macho_fixup_relocs (s->relocs);
1601 /* First calculate and finalize needed values. */
1602 macho_calculate_sizes();
1603 macho_write();
1605 /* free up everything */
1606 while (sects->next) {
1607 s = sects;
1608 sects = sects->next;
1610 saa_free(s->data);
1611 while (s->relocs != NULL) {
1612 r = s->relocs;
1613 s->relocs = s->relocs->next;
1614 nasm_free(r);
1617 nasm_free(s);
1620 saa_free(strs);
1621 raa_free(extsyms);
1623 while (syms) {
1624 sym = syms;
1625 syms = syms->next;
1626 nasm_free (sym);
1629 nasm_free(extdefsyms);
1630 nasm_free(undefsyms);
1631 nasm_free(sectstab);
1634 static bool macho_set_section_attribute_by_symbol(const char *label, uint32_t flags)
1636 struct section *s;
1637 int32_t nasm_seg;
1638 int64_t offset;
1640 if (!lookup_label(label, &nasm_seg, &offset)) {
1641 nasm_error(ERR_NONFATAL, "unknown symbol `%s' in no_dead_strip", label);
1642 return false;
1645 s = get_section_by_index(nasm_seg);
1646 if (!s) {
1647 nasm_error(ERR_NONFATAL, "symbol `%s' is external or absolute", label);
1648 return false;
1651 s->flags |= flags;
1652 return true;
1656 * Mark a symbol for no dead stripping
1658 static enum directive_result macho_no_dead_strip(const char *labels)
1660 char *s, *p, *ep;
1661 char ec;
1662 enum directive_result rv = DIRR_ERROR;
1663 bool real = passn > 1;
1665 p = s = nasm_strdup(labels);
1666 while (*p) {
1667 ep = nasm_skip_identifier(p);
1668 if (!ep) {
1669 nasm_error(ERR_NONFATAL, "invalid symbol in NO_DEAD_STRIP");
1670 goto err;
1672 ec = *ep;
1673 if (ec && ec != ',' && !nasm_isspace(ec)) {
1674 nasm_error(ERR_NONFATAL, "cannot parse contents after symbol");
1675 goto err;
1677 *ep = '\0';
1678 if (real) {
1679 if (!macho_set_section_attribute_by_symbol(p, S_ATTR_NO_DEAD_STRIP))
1680 rv = DIRR_ERROR;
1682 *ep = ec;
1683 p = nasm_skip_spaces(ep);
1684 if (*p == ',')
1685 p = nasm_skip_spaces(++p);
1688 rv = DIRR_OK;
1690 err:
1691 nasm_free(s);
1692 return rv;
1696 * Mach-O pragmas
1698 static enum directive_result
1699 macho_pragma(const struct pragma *pragma)
1701 bool real = passn > 1;
1703 switch (pragma->opcode) {
1704 case D_SUBSECTIONS_VIA_SYMBOLS:
1705 if (*pragma->tail)
1706 return DIRR_BADPARAM;
1708 if (real)
1709 head_flags |= MH_SUBSECTIONS_VIA_SYMBOLS;
1711 return DIRR_OK;
1713 case D_NO_DEAD_STRIP:
1714 return macho_no_dead_strip(pragma->tail);
1716 default:
1717 return DIRR_UNKNOWN; /* Not a Mach-O directive */
1721 static const struct pragma_facility macho_pragma_list[] = {
1722 { "macho", macho_pragma },
1723 { NULL, macho_pragma } /* Implements macho32/macho64 namespaces */
1726 #ifdef OF_MACHO32
1727 static const struct macho_fmt macho32_fmt = {
1729 MH_MAGIC,
1730 CPU_TYPE_I386,
1731 LC_SEGMENT,
1732 MACHO_HEADER_SIZE,
1733 MACHO_SEGCMD_SIZE,
1734 MACHO_SECTCMD_SIZE,
1735 MACHO_NLIST_SIZE,
1736 RL_MAX_32,
1737 GENERIC_RELOC_VANILLA,
1738 GENERIC_RELOC_VANILLA,
1739 GENERIC_RELOC_TLV
1742 static void macho32_init(void)
1744 fmt = macho32_fmt;
1745 macho_init();
1747 macho_gotpcrel_sect = NO_SEG;
1750 const struct ofmt of_macho32 = {
1751 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1752 "macho32",
1755 null_debug_arr,
1756 &null_debug_form,
1757 macho_stdmac,
1758 macho32_init,
1759 nasm_do_legacy_output,
1760 macho_output,
1761 macho_symdef,
1762 macho_section,
1763 macho_sectalign,
1764 macho_segbase,
1765 null_directive,
1766 macho_filename,
1767 macho_cleanup,
1768 macho_pragma_list,
1770 #endif
1772 #ifdef OF_MACHO64
1773 static const struct macho_fmt macho64_fmt = {
1775 MH_MAGIC_64,
1776 CPU_TYPE_X86_64,
1777 LC_SEGMENT_64,
1778 MACHO_HEADER64_SIZE,
1779 MACHO_SEGCMD64_SIZE,
1780 MACHO_SECTCMD64_SIZE,
1781 MACHO_NLIST64_SIZE,
1782 RL_MAX_64,
1783 X86_64_RELOC_UNSIGNED,
1784 X86_64_RELOC_SIGNED,
1785 X86_64_RELOC_TLV
1788 static void macho64_init(void)
1790 fmt = macho64_fmt;
1791 macho_init();
1793 /* add special symbol for ..gotpcrel */
1794 macho_gotpcrel_sect = seg_alloc() + 1;
1795 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false);
1798 const struct ofmt of_macho64 = {
1799 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1800 "macho64",
1803 null_debug_arr,
1804 &null_debug_form,
1805 macho_stdmac,
1806 macho64_init,
1807 nasm_do_legacy_output,
1808 macho_output,
1809 macho_symdef,
1810 macho_section,
1811 macho_sectalign,
1812 macho_segbase,
1813 null_directive,
1814 macho_filename,
1815 macho_cleanup,
1816 macho_pragma_list,
1818 #endif
1820 #endif
1823 * Local Variables:
1824 * mode:c
1825 * c-basic-offset:4
1826 * End:
1828 * end of file */