Add a generic pragma-handling infrastructure
[nasm.git] / output / outmacho.c
blob5ce3e45bc66e02d21d73be8cd4e353e08a411ec2
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * 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 "error.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "rbtree.h"
52 #include "outform.h"
53 #include "outlib.h"
55 #if defined(OF_MACHO) || defined(OF_MACHO64)
57 /* Mach-O in-file header structure sizes */
58 #define MACHO_HEADER_SIZE 28
59 #define MACHO_SEGCMD_SIZE 56
60 #define MACHO_SECTCMD_SIZE 68
61 #define MACHO_SYMCMD_SIZE 24
62 #define MACHO_NLIST_SIZE 12
63 #define MACHO_RELINFO_SIZE 8
65 #define MACHO_HEADER64_SIZE 32
66 #define MACHO_SEGCMD64_SIZE 72
67 #define MACHO_SECTCMD64_SIZE 80
68 #define MACHO_NLIST64_SIZE 16
70 /* Mach-O file header values */
71 #define MH_MAGIC 0xfeedface
72 #define MH_MAGIC_64 0xfeedfacf
73 #define CPU_TYPE_I386 7 /* x86 platform */
74 #define CPU_TYPE_X86_64 0x01000007 /* x86-64 platform */
75 #define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */
76 #define MH_OBJECT 0x1 /* object file */
78 /* Mach-O load commands */
79 #define LC_SEGMENT 0x1 /* 32-bit segment load cmd */
80 #define LC_SEGMENT_64 0x19 /* 64-bit segment load cmd */
81 #define LC_SYMTAB 0x2 /* symbol table load command */
83 /* Mach-O relocations numbers */
85 /* Generic relocs, used by i386 Mach-O */
86 #define GENERIC_RELOC_VANILLA 0 /* Generic relocation */
87 #define GENERIC_RELOC_TLV 5 /* Thread local */
89 #define X86_64_RELOC_UNSIGNED 0 /* Absolute address */
90 #define X86_64_RELOC_SIGNED 1 /* Signed 32-bit disp */
91 #define X86_64_RELOC_BRANCH 2 /* CALL/JMP with 32-bit disp */
92 #define X86_64_RELOC_GOT_LOAD 3 /* MOVQ of GOT entry */
93 #define X86_64_RELOC_GOT 4 /* Different GOT entry */
94 #define X86_64_RELOC_SUBTRACTOR 5 /* Subtracting two symbols */
95 #define X86_64_RELOC_SIGNED_1 6 /* SIGNED with -1 addend */
96 #define X86_64_RELOC_SIGNED_2 7 /* SIGNED with -2 addend */
97 #define X86_64_RELOC_SIGNED_4 8 /* SIGNED with -4 addend */
98 #define X86_64_RELOC_TLV 9 /* Thread local */
100 /* Mach-O VM permission constants */
101 #define VM_PROT_NONE (0x00)
102 #define VM_PROT_READ (0x01)
103 #define VM_PROT_WRITE (0x02)
104 #define VM_PROT_EXECUTE (0x04)
106 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
107 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
109 /* Our internal relocation types */
110 enum reltype {
111 RL_ABS, /* Absolute relocation */
112 RL_REL, /* Relative relocation */
113 RL_TLV, /* Thread local */
114 RL_BRANCH, /* Relative direct branch */
115 RL_SUB, /* X86_64_RELOC_SUBTRACT */
116 RL_GOT, /* X86_64_RELOC_GOT */
117 RL_GOTLOAD /* X86_64_RELOC_GOT_LOAD */
119 #define RL_MAX_32 RL_TLV
120 #define RL_MAX_64 RL_GOTLOAD
122 struct macho_fmt {
123 uint32_t ptrsize; /* Pointer size in bytes */
124 uint32_t mh_magic; /* Which magic number to use */
125 uint32_t cpu_type; /* Which CPU type */
126 uint32_t lc_segment; /* Which segment load command */
127 uint32_t header_size; /* Header size */
128 uint32_t segcmd_size; /* Segment command size */
129 uint32_t sectcmd_size; /* Section command size */
130 uint32_t nlist_size; /* Nlist (symbol) size */
131 enum reltype maxreltype; /* Maximum entry in enum reltype permitted */
132 uint32_t reloc_abs; /* Absolute relocation type */
133 uint32_t reloc_rel; /* Relative relocation type */
134 uint32_t reloc_tlv; /* Thread local relocation type */
137 static struct macho_fmt fmt;
139 static void fwriteptr(uint64_t data, FILE * fp)
141 fwriteaddr(data, fmt.ptrsize, fp);
144 struct section {
145 /* nasm internal data */
146 struct section *next;
147 struct SAA *data;
148 int32_t index;
149 int32_t fileindex;
150 struct reloc *relocs;
151 struct rbtree *gsyms; /* Global symbols in section */
152 int align;
153 bool by_name; /* This section was specified by full MachO name */
155 /* data that goes into the file */
156 char sectname[16]; /* what this section is called */
157 char segname[16]; /* segment this section will be in */
158 uint64_t addr; /* in-memory address (subject to alignment) */
159 uint64_t size; /* in-memory and -file size */
160 uint64_t offset; /* in-file offset */
161 uint32_t pad; /* padding bytes before section */
162 uint32_t nreloc; /* relocation entry count */
163 uint32_t flags; /* type and attributes (masked) */
164 uint32_t extreloc; /* external relocations */
167 #define SECTION_TYPE 0x000000ff /* section type mask */
169 #define S_REGULAR (0x0) /* standard section */
170 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
172 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
173 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
174 machine instructions */
175 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
176 relocation entries */
177 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
178 relocation entries */
179 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
180 machine instructions */
182 /* Fake section for absolute symbols, *not* part of the section linked list */
183 static struct section absolute_sect;
185 static const struct sectmap {
186 const char *nasmsect;
187 const char *segname;
188 const char *sectname;
189 const int32_t flags;
190 } sectmap[] = {
191 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
192 {".data", "__DATA", "__data", S_REGULAR},
193 {".rodata", "__DATA", "__const", S_REGULAR},
194 {".bss", "__DATA", "__bss", S_ZEROFILL},
195 {NULL, NULL, NULL, 0}
198 struct reloc {
199 /* nasm internal data */
200 struct reloc *next;
202 /* data that goes into the file */
203 int32_t addr; /* op's offset in section */
204 uint32_t snum:24, /* contains symbol index if
205 ** ext otherwise in-file
206 ** section number */
207 pcrel:1, /* relative relocation */
208 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
209 ext:1, /* external symbol referenced */
210 type:4; /* reloc type */
213 #define R_ABS 0 /* absolute relocation */
214 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
215 ** highest bit == 1 */
217 struct symbol {
218 /* nasm internal data */
219 struct rbtree symv; /* Global symbol rbtree; "key" contains the
220 symbol offset. */
221 struct symbol *next; /* next symbol in the list */
222 char *name; /* name of this symbol */
223 int32_t initial_snum; /* symbol number used above in reloc */
224 int32_t snum; /* true snum for reloc */
226 /* data that goes into the file */
227 uint32_t strx; /* string table index */
228 uint8_t type; /* symbol type */
229 uint8_t sect; /* NO_SECT or section number */
230 uint16_t desc; /* for stab debugging, 0 for us */
233 /* symbol type bits */
234 #define N_EXT 0x01 /* global or external symbol */
236 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
237 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
238 #define N_SECT 0xe /* defined symbol, n_sect holds
239 ** section number */
241 #define N_TYPE 0x0e /* type bit mask */
243 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
245 /* special section number values */
246 #define NO_SECT 0 /* no section, invalid */
247 #define MAX_SECT 255 /* maximum number of sections */
249 static struct section *sects, **sectstail, **sectstab;
250 static struct symbol *syms, **symstail;
251 static uint32_t nsyms;
253 /* These variables are set by macho_layout_symbols() to organize
254 the symbol table and string table in order the dynamic linker
255 expects. They are then used in macho_write() to put out the
256 symbols and strings in that order.
258 The order of the symbol table is:
259 local symbols
260 defined external symbols (sorted by name)
261 undefined external symbols (sorted by name)
263 The order of the string table is:
264 strings for external symbols
265 strings for local symbols
267 static uint32_t ilocalsym = 0;
268 static uint32_t iextdefsym = 0;
269 static uint32_t iundefsym = 0;
270 static uint32_t nlocalsym;
271 static uint32_t nextdefsym;
272 static uint32_t nundefsym;
273 static struct symbol **extdefsyms = NULL;
274 static struct symbol **undefsyms = NULL;
276 static struct RAA *extsyms;
277 static struct SAA *strs;
278 static uint32_t strslen;
280 /* Global file information. This should be cleaned up into either
281 a structure or as function arguments. */
282 static uint32_t head_ncmds = 0;
283 static uint32_t head_sizeofcmds = 0;
284 static uint64_t seg_filesize = 0;
285 static uint64_t seg_vmsize = 0;
286 static uint32_t seg_nsects = 0;
287 static uint64_t rel_padcnt = 0;
289 #define xstrncpy(xdst, xsrc) \
290 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
291 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
292 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
294 #define alignint32_t(x) \
295 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
297 #define alignint64_t(x) \
298 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
300 #define alignptr(x) \
301 ALIGN(x, fmt.ptrsize) /* align x to output format width */
303 static struct section *get_section_by_name(const char *segname,
304 const char *sectname)
306 struct section *s;
308 for (s = sects; s != NULL; s = s->next)
309 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
310 break;
312 return s;
315 static struct section *get_section_by_index(const int32_t index)
317 struct section *s;
319 for (s = sects; s != NULL; s = s->next)
320 if (index == s->index)
321 break;
323 return s;
327 * Special section numbers which are used to define Mach-O special
328 * symbols, which can be used with WRT to provide PIC relocation
329 * types.
331 static int32_t macho_tlvp_sect;
332 static int32_t macho_gotpcrel_sect;
334 static void macho_init(void)
336 sects = NULL;
337 sectstail = &sects;
339 /* Fake section for absolute symbols */
340 absolute_sect.index = NO_SEG;
342 syms = NULL;
343 symstail = &syms;
344 nsyms = 0;
345 nlocalsym = 0;
346 nextdefsym = 0;
347 nundefsym = 0;
349 extsyms = raa_init();
350 strs = saa_init(1L);
352 /* string table starts with a zero byte so index 0 is an empty string */
353 saa_wbytes(strs, zero_buffer, 1);
354 strslen = 1;
356 /* add special symbol for TLVP */
357 macho_tlvp_sect = seg_alloc() + 1;
358 define_label("..tlvp", macho_tlvp_sect, 0L, NULL, false, false);
362 static void sect_write(struct section *sect,
363 const uint8_t *data, uint32_t len)
365 saa_wbytes(sect->data, data, len);
366 sect->size += len;
370 * Find a suitable global symbol for a ..gotpcrel or ..tlvp reference
372 static struct symbol *macho_find_gsym(struct section *s,
373 uint64_t offset, bool exact)
375 struct rbtree *srb;
377 srb = rb_search(s->gsyms, offset);
379 if (!srb || (exact && srb->key != offset)) {
380 nasm_error(ERR_NONFATAL, "unable to find a suitable %s symbol"
381 " for this reference",
382 s == &absolute_sect ? "absolute" : "global");
383 return NULL;
386 return container_of(srb, struct symbol, symv);
389 static int64_t add_reloc(struct section *sect, int32_t section,
390 int64_t offset,
391 enum reltype reltype, int bytes)
393 struct reloc *r;
394 struct section *s;
395 int32_t fi;
396 int64_t adjust;
398 /* Double check this is a valid relocation type for this platform */
399 nasm_assert(reltype <= fmt.maxreltype);
401 /* the current end of the section will be the symbol's address for
402 ** now, might have to be fixed by macho_fixup_relocs() later on. make
403 ** sure we don't make the symbol scattered by setting the highest
404 ** bit by accident */
405 r = nasm_malloc(sizeof(struct reloc));
406 r->addr = sect->size & ~R_SCATTERED;
407 r->ext = 1;
408 adjust = bytes;
410 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
411 r->length = ilog2_32(bytes);
413 /* set default relocation values */
414 r->type = fmt.reloc_abs;
415 r->pcrel = 0;
416 r->snum = R_ABS;
418 s = NULL;
419 if (section != NO_SEG)
420 s = get_section_by_index(section);
421 fi = s ? s->fileindex : NO_SECT;
423 /* absolute relocation */
424 switch (reltype) {
425 case RL_ABS:
426 if (section == NO_SEG) {
427 /* absolute (can this even happen?) */
428 r->ext = 0;
429 r->snum = R_ABS;
430 } else if (fi == NO_SECT) {
431 /* external */
432 r->snum = raa_read(extsyms, section);
433 } else {
434 /* local */
435 r->ext = 0;
436 r->snum = fi;
437 adjust = -sect->size;
439 break;
441 case RL_REL:
442 case RL_BRANCH:
443 r->type = fmt.reloc_rel;
444 r->pcrel = 1;
445 if (section == NO_SEG) {
446 /* absolute - seems to produce garbage no matter what */
447 nasm_error(ERR_NONFATAL, "Mach-O does not support relative "
448 "references to absolute addresses");
449 goto bail;
450 #if 0
451 /* This "seems" to be how it ought to work... */
453 struct symbol *sym = macho_find_gsym(&absolute_sect,
454 offset, false);
455 if (!sym)
456 goto bail;
458 sect->extreloc = 1;
459 r->snum = NO_SECT;
460 adjust = -sect->size;
461 #endif
462 } else if (fi == NO_SECT) {
463 /* external */
464 sect->extreloc = 1;
465 r->snum = raa_read(extsyms, section);
466 if (reltype == RL_BRANCH)
467 r->type = X86_64_RELOC_BRANCH;
468 else if (r->type == GENERIC_RELOC_VANILLA)
469 adjust = -sect->size;
470 } else {
471 /* local */
472 r->ext = 0;
473 r->snum = fi;
474 adjust = -sect->size;
476 break;
478 case RL_SUB:
479 r->pcrel = 0;
480 r->type = X86_64_RELOC_SUBTRACTOR;
481 break;
483 case RL_GOT:
484 r->type = X86_64_RELOC_GOT;
485 goto needsym;
487 case RL_GOTLOAD:
488 r->type = X86_64_RELOC_GOT_LOAD;
489 goto needsym;
491 case RL_TLV:
492 r->type = fmt.reloc_tlv;
493 goto needsym;
495 needsym:
496 r->pcrel = 1;
497 if (section == NO_SEG) {
498 nasm_error(ERR_NONFATAL, "Unsupported use of use of WRT");
499 } else if (fi == NO_SECT) {
500 /* external */
501 r->snum = raa_read(extsyms, section);
502 } else {
503 /* internal */
504 struct symbol *sym = macho_find_gsym(s, offset, reltype != RL_TLV);
505 if (!sym)
506 goto bail;
507 r->snum = sym->initial_snum;
509 break;
512 /* NeXT as puts relocs in reversed order (address-wise) into the
513 ** files, so we do the same, doesn't seem to make much of a
514 ** difference either way */
515 r->next = sect->relocs;
516 sect->relocs = r;
517 if (r->ext)
518 sect->extreloc = 1;
519 ++sect->nreloc;
521 return adjust;
523 bail:
524 nasm_free(r);
525 return 0;
528 static void macho_output(int32_t secto, const void *data,
529 enum out_type type, uint64_t size,
530 int32_t section, int32_t wrt)
532 struct section *s;
533 int64_t addr, offset;
534 uint8_t mydata[16], *p;
535 bool is_bss;
536 enum reltype reltype;
538 if (secto == NO_SEG) {
539 if (type != OUT_RESERVE)
540 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
541 "[ABSOLUTE] space");
542 return;
545 s = get_section_by_index(secto);
547 if (s == NULL) {
548 nasm_error(ERR_WARNING, "attempt to assemble code in"
549 " section %d: defaulting to `.text'", secto);
550 s = get_section_by_name("__TEXT", "__text");
552 /* should never happen */
553 if (s == NULL)
554 nasm_panic(0, "text section not found");
557 is_bss = (s->flags & SECTION_TYPE) == S_ZEROFILL;
559 if (is_bss && type != OUT_RESERVE) {
560 nasm_error(ERR_WARNING, "attempt to initialize memory in "
561 "BSS section: ignored");
562 s->size += realsize(type, size);
563 return;
566 memset(mydata, 0, sizeof(mydata));
568 switch (type) {
569 case OUT_RESERVE:
570 if (!is_bss) {
571 nasm_error(ERR_WARNING, "uninitialized space declared in"
572 " %s,%s section: zeroing", s->segname, s->sectname);
574 sect_write(s, NULL, size);
575 } else
576 s->size += size;
578 break;
580 case OUT_RAWDATA:
581 if (section != NO_SEG)
582 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
584 sect_write(s, data, size);
585 break;
587 case OUT_ADDRESS:
589 int asize = abs((int)size);
591 addr = *(int64_t *)data;
592 if (section != NO_SEG) {
593 if (section % 2) {
594 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
595 " section base references");
596 } else if (wrt == NO_SEG) {
597 if (fmt.ptrsize == 8 && asize != 8) {
598 nasm_error(ERR_NONFATAL,
599 "Mach-O 64-bit format does not support"
600 " 32-bit absolute addresses");
601 } else {
602 add_reloc(s, section, addr, RL_ABS, asize);
604 } else {
605 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
606 " this use of WRT");
610 p = mydata;
611 WRITEADDR(p, addr, asize);
612 sect_write(s, mydata, asize);
613 break;
616 case OUT_REL2ADR:
617 nasm_assert(section != secto);
619 p = mydata;
620 offset = *(int64_t *)data;
621 addr = offset - size;
623 if (section != NO_SEG && section % 2) {
624 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
625 " section base references");
626 } else if (fmt.ptrsize == 8) {
627 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
628 " Macho-O relocation [2]");
629 } else if (wrt != NO_SEG) {
630 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
631 " this use of WRT");
632 wrt = NO_SEG; /* we can at least _try_ to continue */
633 } else {
634 addr += add_reloc(s, section, addr+size, RL_REL, 2);
637 WRITESHORT(p, addr);
638 sect_write(s, mydata, 2);
639 break;
641 case OUT_REL4ADR:
642 nasm_assert(section != secto);
644 p = mydata;
645 offset = *(int64_t *)data;
646 addr = offset - size;
647 reltype = RL_REL;
649 if (section != NO_SEG && section % 2) {
650 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
651 " section base references");
652 } else if (wrt == NO_SEG) {
653 if (fmt.ptrsize == 8 &&
654 (s->flags & S_ATTR_SOME_INSTRUCTIONS)) {
655 uint8_t opcode[2];
657 opcode[0] = opcode[1] = 0;
659 /* HACK: Retrieve instruction opcode */
660 if (likely(s->data->datalen >= 2)) {
661 saa_fread(s->data, s->data->datalen-2, opcode, 2);
662 } else if (s->data->datalen == 1) {
663 saa_fread(s->data, 0, opcode+1, 1);
666 if ((opcode[0] != 0x0f && (opcode[1] & 0xfe) == 0xe8) ||
667 (opcode[0] == 0x0f && (opcode[1] & 0xf0) == 0x80)) {
668 /* Direct call, jmp, or jcc */
669 reltype = RL_BRANCH;
672 } else if (wrt == macho_gotpcrel_sect) {
673 reltype = RL_GOT;
675 if ((s->flags & S_ATTR_SOME_INSTRUCTIONS) &&
676 s->data->datalen >= 3) {
677 uint8_t gotload[3];
679 /* HACK: Retrieve instruction opcode */
680 saa_fread(s->data, s->data->datalen-3, gotload, 3);
681 if ((gotload[0] & 0xf8) == 0x48 &&
682 gotload[1] == 0x8b &&
683 (gotload[2] & 0307) == 0005) {
684 /* movq <reg>,[rel sym wrt ..gotpcrel] */
685 reltype = RL_GOTLOAD;
688 } else if (wrt == macho_tlvp_sect) {
689 reltype = RL_TLV;
690 } else {
691 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
692 " this use of WRT");
693 /* continue with RL_REL */
696 addr += add_reloc(s, section, offset, reltype, 4);
697 WRITELONG(p, addr);
698 sect_write(s, mydata, 4);
699 break;
701 default:
702 nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
703 break;
707 static int32_t macho_section(char *name, int pass, int *bits)
709 char *sectionAttributes;
710 const struct sectmap *sm;
711 struct section *s;
712 const char *section, *segment;
713 uint32_t flags;
714 char *currentAttribute;
715 char *comma;
716 bool new_seg;
718 (void)pass;
720 /* Default to the appropriate number of bits. */
721 if (!name) {
722 *bits = fmt.ptrsize << 3;
723 name = ".text";
724 sectionAttributes = NULL;
725 } else {
726 sectionAttributes = name;
727 name = nasm_strsep(&sectionAttributes, " \t");
730 section = segment = NULL;
731 flags = 0;
733 comma = strchr(name, ',');
734 if (comma) {
735 int len;
737 *comma = '\0';
738 segment = name;
739 section = comma+1;
741 len = strlen(segment);
742 if (len == 0) {
743 nasm_error(ERR_NONFATAL, "empty segment name\n");
744 } else if (len >= 16) {
745 nasm_error(ERR_NONFATAL, "segment name %s too long\n", segment);
748 len = strlen(section);
749 if (len == 0) {
750 nasm_error(ERR_NONFATAL, "empty section name\n");
751 } else if (len >= 16) {
752 nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
755 if (!strcmp(section, "__text")) {
756 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
757 S_ATTR_PURE_INSTRUCTIONS;
758 } else if (!strcmp(section, "__bss")) {
759 flags = S_ZEROFILL;
760 } else {
761 flags = S_REGULAR;
763 } else {
764 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
765 /* make lookup into section name translation table */
766 if (!strcmp(name, sm->nasmsect)) {
767 segment = sm->segname;
768 section = sm->sectname;
769 flags = sm->flags;
770 goto found;
773 nasm_error(ERR_NONFATAL, "unknown section name\n");
774 return NO_SEG;
777 found:
778 /* try to find section with that name */
779 s = get_section_by_name(segment, section);
781 /* create it if it doesn't exist yet */
782 if (!s) {
783 new_seg = true;
785 s = *sectstail = nasm_zalloc(sizeof(struct section));
786 sectstail = &s->next;
788 s->data = saa_init(1L);
789 s->index = seg_alloc();
790 s->fileindex = ++seg_nsects;
791 s->align = -1;
792 s->pad = -1;
793 s->offset = -1;
794 s->by_name = false;
796 xstrncpy(s->segname, segment);
797 xstrncpy(s->sectname, section);
798 s->size = 0;
799 s->nreloc = 0;
800 s->flags = flags;
801 } else {
802 new_seg = false;
805 if (comma)
806 *comma = ','; /* Restore comma */
808 s->by_name = s->by_name || comma; /* Was specified by name */
810 flags = (uint32_t)-1;
812 while ((NULL != sectionAttributes)
813 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
814 if (0 != *currentAttribute) {
815 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
816 char *end;
817 int newAlignment, value;
819 value = strtoul(currentAttribute + 6, (char**)&end, 0);
820 newAlignment = alignlog2_32(value);
822 if (0 != *end) {
823 nasm_error(ERR_NONFATAL,
824 "unknown or missing alignment value \"%s\" "
825 "specified for section \"%s\"",
826 currentAttribute + 6,
827 name);
828 } else if (0 > newAlignment) {
829 nasm_error(ERR_NONFATAL,
830 "alignment of %d (for section \"%s\") is not "
831 "a power of two",
832 value,
833 name);
836 if (s->align < newAlignment)
837 s->align = newAlignment;
838 } else if (!nasm_stricmp("data", currentAttribute)) {
839 flags = S_REGULAR;
840 } else if (!nasm_stricmp("code", currentAttribute) ||
841 !nasm_stricmp("text", currentAttribute)) {
842 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
843 S_ATTR_PURE_INSTRUCTIONS;
844 } else if (!nasm_stricmp("mixed", currentAttribute)) {
845 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS;
846 } else if (!nasm_stricmp("bss", currentAttribute)) {
847 flags = S_ZEROFILL;
848 } else {
849 nasm_error(ERR_NONFATAL,
850 "unknown section attribute %s for section %s",
851 currentAttribute,
852 name);
856 if (flags != (uint32_t)-1) {
857 if (!new_seg && s->flags != flags) {
858 nasm_error(ERR_NONFATAL,
859 "inconsistent section attributes for section %s\n",
860 name);
861 } else {
862 s->flags = flags;
867 return s->index;
870 static void macho_symdef(char *name, int32_t section, int64_t offset,
871 int is_global, char *special)
873 struct symbol *sym;
875 if (special) {
876 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
877 "not support any special symbol types");
878 return;
881 if (is_global == 3) {
882 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
883 "(yet) support forward reference fixups.");
884 return;
887 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
889 * This is a NASM special symbol. We never allow it into
890 * the Macho-O symbol table, even if it's a valid one. If it
891 * _isn't_ a valid one, we should barf immediately.
893 if (strcmp(name, "..gotpcrel") && strcmp(name, "..tlvp"))
894 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
895 return;
898 sym = *symstail = nasm_zalloc(sizeof(struct symbol));
899 sym->next = NULL;
900 symstail = &sym->next;
902 sym->name = name;
903 sym->strx = strslen;
904 sym->type = 0;
905 sym->desc = 0;
906 sym->symv.key = offset;
907 sym->initial_snum = -1;
909 /* external and common symbols get N_EXT */
910 if (is_global != 0) {
911 sym->type |= N_EXT;
914 if (section == NO_SEG) {
915 /* symbols in no section get absolute */
916 sym->type |= N_ABS;
917 sym->sect = NO_SECT;
919 /* all absolute symbols are available to use as references */
920 absolute_sect.gsyms = rb_insert(absolute_sect.gsyms, &sym->symv);
921 } else {
922 struct section *s = get_section_by_index(section);
924 sym->type |= N_SECT;
926 /* get the in-file index of the section the symbol was defined in */
927 sym->sect = s ? s->fileindex : NO_SECT;
929 /* track the initially allocated symbol number for use in future fix-ups */
930 sym->initial_snum = nsyms;
932 if (!s) {
933 /* remember symbol number of references to external
934 ** symbols, this works because every external symbol gets
935 ** its own section number allocated internally by nasm and
936 ** can so be used as a key */
937 extsyms = raa_write(extsyms, section, nsyms);
939 switch (is_global) {
940 case 1:
941 case 2:
942 /* there isn't actually a difference between global
943 ** and common symbols, both even have their size in
944 ** sym->symv.key */
945 sym->type = N_EXT;
946 break;
948 default:
949 /* give an error on unfound section if it's not an
950 ** external or common symbol (assemble_file() does a
951 ** seg_alloc() on every call for them) */
952 nasm_panic(0, "in-file index for section %d not found, is_global = %d", section, is_global);
953 break;
955 } else if (is_global) {
956 s->gsyms = rb_insert(s->gsyms, &sym->symv);
959 ++nsyms;
962 static void macho_sectalign(int32_t seg, unsigned int value)
964 struct section *s;
965 int align;
967 nasm_assert(!(seg & 1));
969 s = get_section_by_index(seg);
971 if (!s || !is_power2(value))
972 return;
974 align = alignlog2_32(value);
975 if (s->align < align)
976 s->align = align;
979 static int32_t macho_segbase(int32_t section)
981 return section;
984 static void macho_filename(char *inname, char *outname)
986 standard_extension(inname, outname, ".o");
989 extern macros_t macho_stdmac[];
991 /* Comparison function for qsort symbol layout. */
992 static int layout_compare (const struct symbol **s1,
993 const struct symbol **s2)
995 return (strcmp ((*s1)->name, (*s2)->name));
998 /* The native assembler does a few things in a similar function
1000 * Remove temporary labels
1001 * Sort symbols according to local, external, undefined (by name)
1002 * Order the string table
1004 We do not remove temporary labels right now.
1006 numsyms is the total number of symbols we have. strtabsize is the
1007 number entries in the string table. */
1009 static void macho_layout_symbols (uint32_t *numsyms,
1010 uint32_t *strtabsize)
1012 struct symbol *sym, **symp;
1013 uint32_t i,j;
1015 *numsyms = 0;
1016 *strtabsize = sizeof (char);
1018 symp = &syms;
1020 while ((sym = *symp)) {
1021 /* Undefined symbols are now external. */
1022 if (sym->type == N_UNDF)
1023 sym->type |= N_EXT;
1025 if ((sym->type & N_EXT) == 0) {
1026 sym->snum = *numsyms;
1027 *numsyms = *numsyms + 1;
1028 nlocalsym++;
1030 else {
1031 if ((sym->type & N_TYPE) != N_UNDF) {
1032 nextdefsym++;
1033 } else {
1034 nundefsym++;
1037 /* If we handle debug info we'll want
1038 to check for it here instead of just
1039 adding the symbol to the string table. */
1040 sym->strx = *strtabsize;
1041 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
1042 *strtabsize += strlen(sym->name) + 1;
1044 symp = &(sym->next);
1047 /* Next, sort the symbols. Most of this code is a direct translation from
1048 the Apple cctools symbol layout. We need to keep compatibility with that. */
1049 /* Set the indexes for symbol groups into the symbol table */
1050 ilocalsym = 0;
1051 iextdefsym = nlocalsym;
1052 iundefsym = nlocalsym + nextdefsym;
1054 /* allocate arrays for sorting externals by name */
1055 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
1056 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
1058 i = 0;
1059 j = 0;
1061 symp = &syms;
1063 while ((sym = *symp)) {
1065 if((sym->type & N_EXT) == 0) {
1066 sym->strx = *strtabsize;
1067 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
1068 *strtabsize += strlen(sym->name) + 1;
1070 else {
1071 if((sym->type & N_TYPE) != N_UNDF) {
1072 extdefsyms[i++] = sym;
1073 } else {
1074 undefsyms[j++] = sym;
1077 symp = &(sym->next);
1080 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
1081 (int (*)(const void *, const void *))layout_compare);
1082 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
1083 (int (*)(const void *, const void *))layout_compare);
1085 for(i = 0; i < nextdefsym; i++) {
1086 extdefsyms[i]->snum = *numsyms;
1087 *numsyms += 1;
1089 for(j = 0; j < nundefsym; j++) {
1090 undefsyms[j]->snum = *numsyms;
1091 *numsyms += 1;
1095 /* Calculate some values we'll need for writing later. */
1097 static void macho_calculate_sizes (void)
1099 struct section *s;
1100 int fi;
1102 /* count sections and calculate in-memory and in-file offsets */
1103 for (s = sects; s != NULL; s = s->next) {
1104 uint64_t newaddr;
1106 /* recalculate segment address based on alignment and vm size */
1107 s->addr = seg_vmsize;
1109 /* we need section alignment to calculate final section address */
1110 if (s->align == -1)
1111 s->align = DEFAULT_SECTION_ALIGNMENT;
1113 newaddr = ALIGN(s->addr, UINT64_C(1) << s->align);
1114 s->addr = newaddr;
1116 seg_vmsize = newaddr + s->size;
1118 /* zerofill sections aren't actually written to the file */
1119 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1121 * LLVM/Xcode as always aligns the section data to 4
1122 * bytes; there is a comment in the LLVM source code that
1123 * perhaps aligning to pointer size would be better.
1125 s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
1126 s->offset = seg_filesize + s->pad;
1127 seg_filesize += s->size + s->pad;
1131 /* calculate size of all headers, load commands and sections to
1132 ** get a pointer to the start of all the raw data */
1133 if (seg_nsects > 0) {
1134 ++head_ncmds;
1135 head_sizeofcmds += fmt.segcmd_size + seg_nsects * fmt.sectcmd_size;
1138 if (nsyms > 0) {
1139 ++head_ncmds;
1140 head_sizeofcmds += MACHO_SYMCMD_SIZE;
1143 if (seg_nsects > MAX_SECT) {
1144 nasm_fatal(0, "MachO output is limited to %d sections\n",
1145 MAX_SECT);
1148 /* Create a table of sections by file index to avoid linear search */
1149 sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
1150 sectstab[NO_SECT] = &absolute_sect;
1151 for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
1152 sectstab[fi] = s;
1155 /* Write out the header information for the file. */
1157 static void macho_write_header (void)
1159 fwriteint32_t(fmt.mh_magic, ofile); /* magic */
1160 fwriteint32_t(fmt.cpu_type, ofile); /* CPU type */
1161 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1162 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1163 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
1164 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
1165 fwriteint32_t(0, ofile); /* no flags */
1166 fwritezero(fmt.header_size - 7*4, ofile); /* reserved fields */
1169 /* Write out the segment load command at offset. */
1171 static uint32_t macho_write_segment (uint64_t offset)
1173 uint64_t rel_base = alignptr(offset + seg_filesize);
1174 uint32_t s_reloff = 0;
1175 struct section *s;
1177 fwriteint32_t(fmt.lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
1179 /* size of load command including section load commands */
1180 fwriteint32_t(fmt.segcmd_size + seg_nsects * fmt.sectcmd_size,
1181 ofile);
1183 /* in an MH_OBJECT file all sections are in one unnamed (name
1184 ** all zeros) segment */
1185 fwritezero(16, ofile);
1186 fwriteptr(0, ofile); /* in-memory offset */
1187 fwriteptr(seg_vmsize, ofile); /* in-memory size */
1188 fwriteptr(offset, ofile); /* in-file offset to data */
1189 fwriteptr(seg_filesize, ofile); /* in-file size */
1190 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1191 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1192 fwriteint32_t(seg_nsects, ofile); /* number of sections */
1193 fwriteint32_t(0, ofile); /* no flags */
1195 /* emit section headers */
1196 for (s = sects; s != NULL; s = s->next) {
1197 if (s->nreloc) {
1198 nasm_assert((s->flags & SECTION_TYPE) != S_ZEROFILL);
1199 s->flags |= S_ATTR_LOC_RELOC;
1200 if (s->extreloc)
1201 s->flags |= S_ATTR_EXT_RELOC;
1202 } else if (!strcmp(s->segname, "__DATA") &&
1203 !strcmp(s->sectname, "__const") &&
1204 !s->by_name &&
1205 !get_section_by_name("__TEXT", "__const")) {
1207 * The MachO equivalent to .rodata can be either
1208 * __DATA,__const or __TEXT,__const; the latter only if
1209 * there are no relocations. However, when mixed it is
1210 * better to specify the segments explicitly.
1212 xstrncpy(s->segname, "__TEXT");
1215 nasm_write(s->sectname, sizeof(s->sectname), ofile);
1216 nasm_write(s->segname, sizeof(s->segname), ofile);
1217 fwriteptr(s->addr, ofile);
1218 fwriteptr(s->size, ofile);
1220 /* dummy data for zerofill sections or proper values */
1221 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1222 nasm_assert(s->pad != (uint32_t)-1);
1223 offset += s->pad;
1224 fwriteint32_t(offset, ofile);
1225 offset += s->size;
1226 /* Write out section alignment, as a power of two.
1227 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1228 fwriteint32_t(s->align, ofile);
1229 /* To be compatible with cctools as we emit
1230 a zero reloff if we have no relocations. */
1231 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1232 fwriteint32_t(s->nreloc, ofile);
1234 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
1235 } else {
1236 fwriteint32_t(0, ofile);
1237 fwriteint32_t(s->align, ofile);
1238 fwriteint32_t(0, ofile);
1239 fwriteint32_t(0, ofile);
1242 fwriteint32_t(s->flags, ofile); /* flags */
1243 fwriteint32_t(0, ofile); /* reserved */
1244 fwriteptr(0, ofile); /* reserved */
1247 rel_padcnt = rel_base - offset;
1248 offset = rel_base + s_reloff;
1250 return offset;
1253 /* For a given chain of relocs r, write out the entire relocation
1254 chain to the object file. */
1256 static void macho_write_relocs (struct reloc *r)
1258 while (r) {
1259 uint32_t word2;
1261 fwriteint32_t(r->addr, ofile); /* reloc offset */
1263 word2 = r->snum;
1264 word2 |= r->pcrel << 24;
1265 word2 |= r->length << 25;
1266 word2 |= r->ext << 27;
1267 word2 |= r->type << 28;
1268 fwriteint32_t(word2, ofile); /* reloc data */
1269 r = r->next;
1273 /* Write out the section data. */
1274 static void macho_write_section (void)
1276 struct section *s;
1277 struct reloc *r;
1278 uint8_t *p;
1279 int32_t len;
1280 int64_t l;
1281 union offset {
1282 uint64_t val;
1283 uint8_t buf[8];
1284 } blk;
1286 for (s = sects; s != NULL; s = s->next) {
1287 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1288 continue;
1290 /* Like a.out Mach-O references things in the data or bss
1291 * sections by addresses which are actually relative to the
1292 * start of the _text_ section, in the _file_. See outaout.c
1293 * for more information. */
1294 saa_rewind(s->data);
1295 for (r = s->relocs; r != NULL; r = r->next) {
1296 len = (uint32_t)1 << r->length;
1297 if (len > 4) /* Can this ever be an issue?! */
1298 len = 8;
1299 blk.val = 0;
1300 saa_fread(s->data, r->addr, blk.buf, len);
1302 /* get offset based on relocation type */
1303 #ifdef WORDS_LITTLEENDIAN
1304 l = blk.val;
1305 #else
1306 l = blk.buf[0];
1307 l += ((int64_t)blk.buf[1]) << 8;
1308 l += ((int64_t)blk.buf[2]) << 16;
1309 l += ((int64_t)blk.buf[3]) << 24;
1310 l += ((int64_t)blk.buf[4]) << 32;
1311 l += ((int64_t)blk.buf[5]) << 40;
1312 l += ((int64_t)blk.buf[6]) << 48;
1313 l += ((int64_t)blk.buf[7]) << 56;
1314 #endif
1316 /* If the relocation is internal add to the current section
1317 offset. Otherwise the only value we need is the symbol
1318 offset which we already have. The linker takes care
1319 of the rest of the address. */
1320 if (!r->ext) {
1321 /* generate final address by section address and offset */
1322 nasm_assert(r->snum <= seg_nsects);
1323 l += sectstab[r->snum]->addr;
1324 if (r->pcrel)
1325 l -= s->addr;
1326 } else if (r->pcrel && r->type == GENERIC_RELOC_VANILLA) {
1327 l -= s->addr;
1330 /* write new offset back */
1331 p = blk.buf;
1332 WRITEDLONG(p, l);
1333 saa_fwrite(s->data, r->addr, blk.buf, len);
1336 /* dump the section data to file */
1337 fwritezero(s->pad, ofile);
1338 saa_fpwrite(s->data, ofile);
1341 /* pad last section up to reloc entries on pointer boundary */
1342 fwritezero(rel_padcnt, ofile);
1344 /* emit relocation entries */
1345 for (s = sects; s != NULL; s = s->next)
1346 macho_write_relocs (s->relocs);
1349 /* Write out the symbol table. We should already have sorted this
1350 before now. */
1351 static void macho_write_symtab (void)
1353 struct symbol *sym;
1354 uint64_t i;
1356 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1358 for (sym = syms; sym != NULL; sym = sym->next) {
1359 if ((sym->type & N_EXT) == 0) {
1360 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1361 nasm_write(&sym->type, 1, ofile); /* symbol type */
1362 nasm_write(&sym->sect, 1, ofile); /* section */
1363 fwriteint16_t(sym->desc, ofile); /* description */
1365 /* Fix up the symbol value now that we know the final section
1366 sizes. */
1367 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1368 nasm_assert(sym->sect <= seg_nsects);
1369 sym->symv.key += sectstab[sym->sect]->addr;
1372 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1376 for (i = 0; i < nextdefsym; i++) {
1377 sym = extdefsyms[i];
1378 fwriteint32_t(sym->strx, ofile);
1379 nasm_write(&sym->type, 1, ofile); /* symbol type */
1380 nasm_write(&sym->sect, 1, ofile); /* section */
1381 fwriteint16_t(sym->desc, ofile); /* description */
1383 /* Fix up the symbol value now that we know the final section
1384 sizes. */
1385 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1386 nasm_assert(sym->sect <= seg_nsects);
1387 sym->symv.key += sectstab[sym->sect]->addr;
1390 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1393 for (i = 0; i < nundefsym; i++) {
1394 sym = undefsyms[i];
1395 fwriteint32_t(sym->strx, ofile);
1396 nasm_write(&sym->type, 1, ofile); /* symbol type */
1397 nasm_write(&sym->sect, 1, ofile); /* section */
1398 fwriteint16_t(sym->desc, ofile); /* description */
1400 /* Fix up the symbol value now that we know the final section
1401 sizes. */
1402 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1403 nasm_assert(sym->sect <= seg_nsects);
1404 sym->symv.key += sectstab[sym->sect]->addr;
1407 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1412 /* Fixup the snum in the relocation entries, we should be
1413 doing this only for externally referenced symbols. */
1414 static void macho_fixup_relocs (struct reloc *r)
1416 struct symbol *sym;
1418 while (r != NULL) {
1419 if (r->ext) {
1420 for (sym = syms; sym != NULL; sym = sym->next) {
1421 if (sym->initial_snum == r->snum) {
1422 r->snum = sym->snum;
1423 break;
1427 r = r->next;
1431 /* Write out the object file. */
1433 static void macho_write (void)
1435 uint64_t offset = 0;
1437 /* mach-o object file structure:
1439 ** mach header
1440 ** uint32_t magic
1441 ** int cpu type
1442 ** int cpu subtype
1443 ** uint32_t mach file type
1444 ** uint32_t number of load commands
1445 ** uint32_t size of all load commands
1446 ** (includes section struct size of segment command)
1447 ** uint32_t flags
1449 ** segment command
1450 ** uint32_t command type == LC_SEGMENT[_64]
1451 ** uint32_t size of load command
1452 ** (including section load commands)
1453 ** char[16] segment name
1454 ** pointer in-memory offset
1455 ** pointer in-memory size
1456 ** pointer in-file offset to data area
1457 ** pointer in-file size
1458 ** (in-memory size excluding zerofill sections)
1459 ** int maximum vm protection
1460 ** int initial vm protection
1461 ** uint32_t number of sections
1462 ** uint32_t flags
1464 ** section commands
1465 ** char[16] section name
1466 ** char[16] segment name
1467 ** pointer in-memory offset
1468 ** pointer in-memory size
1469 ** uint32_t in-file offset
1470 ** uint32_t alignment
1471 ** (irrelevant in MH_OBJECT)
1472 ** uint32_t in-file offset of relocation entires
1473 ** uint32_t number of relocations
1474 ** uint32_t flags
1475 ** uint32_t reserved
1476 ** uint32_t reserved
1478 ** symbol table command
1479 ** uint32_t command type == LC_SYMTAB
1480 ** uint32_t size of load command
1481 ** uint32_t symbol table offset
1482 ** uint32_t number of symbol table entries
1483 ** uint32_t string table offset
1484 ** uint32_t string table size
1486 ** raw section data
1488 ** padding to pointer boundary
1490 ** relocation data (struct reloc)
1491 ** int32_t offset
1492 ** uint data (symbolnum, pcrel, length, extern, type)
1494 ** symbol table data (struct nlist)
1495 ** int32_t string table entry number
1496 ** uint8_t type
1497 ** (extern, absolute, defined in section)
1498 ** uint8_t section
1499 ** (0 for global symbols, section number of definition (>= 1, <=
1500 ** 254) for local symbols, size of variable for common symbols
1501 ** [type == extern])
1502 ** int16_t description
1503 ** (for stab debugging format)
1504 ** pointer value (i.e. file offset) of symbol or stab offset
1506 ** string table data
1507 ** list of null-terminated strings
1510 /* Emit the Mach-O header. */
1511 macho_write_header();
1513 offset = fmt.header_size + head_sizeofcmds;
1515 /* emit the segment load command */
1516 if (seg_nsects > 0)
1517 offset = macho_write_segment (offset);
1518 else
1519 nasm_error(ERR_WARNING, "no sections?");
1521 if (nsyms > 0) {
1522 /* write out symbol command */
1523 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1524 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1525 fwriteint32_t(offset, ofile); /* symbol table offset */
1526 fwriteint32_t(nsyms, ofile); /* number of symbol
1527 ** table entries */
1528 offset += nsyms * fmt.nlist_size;
1529 fwriteint32_t(offset, ofile); /* string table offset */
1530 fwriteint32_t(strslen, ofile); /* string table size */
1533 /* emit section data */
1534 if (seg_nsects > 0)
1535 macho_write_section ();
1537 /* emit symbol table if we have symbols */
1538 if (nsyms > 0)
1539 macho_write_symtab ();
1541 /* we don't need to pad here, we are already aligned */
1543 /* emit string table */
1544 saa_fpwrite(strs, ofile);
1546 /* We do quite a bit here, starting with finalizing all of the data
1547 for the object file, writing, and then freeing all of the data from
1548 the file. */
1550 static void macho_cleanup(void)
1552 struct section *s;
1553 struct reloc *r;
1554 struct symbol *sym;
1556 /* Sort all symbols. */
1557 macho_layout_symbols (&nsyms, &strslen);
1559 /* Fixup relocation entries */
1560 for (s = sects; s != NULL; s = s->next) {
1561 macho_fixup_relocs (s->relocs);
1564 /* First calculate and finalize needed values. */
1565 macho_calculate_sizes();
1566 macho_write();
1568 /* free up everything */
1569 while (sects->next) {
1570 s = sects;
1571 sects = sects->next;
1573 saa_free(s->data);
1574 while (s->relocs != NULL) {
1575 r = s->relocs;
1576 s->relocs = s->relocs->next;
1577 nasm_free(r);
1580 nasm_free(s);
1583 saa_free(strs);
1584 raa_free(extsyms);
1586 while (syms) {
1587 sym = syms;
1588 syms = syms->next;
1589 nasm_free (sym);
1592 nasm_free(extdefsyms);
1593 nasm_free(undefsyms);
1594 nasm_free(sectstab);
1597 #ifdef OF_MACHO32
1598 static const struct macho_fmt macho32_fmt = {
1600 MH_MAGIC,
1601 CPU_TYPE_I386,
1602 LC_SEGMENT,
1603 MACHO_HEADER_SIZE,
1604 MACHO_SEGCMD_SIZE,
1605 MACHO_SECTCMD_SIZE,
1606 MACHO_NLIST_SIZE,
1607 RL_MAX_32,
1608 GENERIC_RELOC_VANILLA,
1609 GENERIC_RELOC_VANILLA,
1610 GENERIC_RELOC_TLV
1613 static void macho32_init(void)
1615 fmt = macho32_fmt;
1616 macho_init();
1618 macho_gotpcrel_sect = NO_SEG;
1621 const struct ofmt of_macho32 = {
1622 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1623 "macho32",
1626 null_debug_arr,
1627 &null_debug_form,
1628 macho_stdmac,
1629 macho32_init,
1630 null_setinfo,
1631 nasm_do_legacy_output,
1632 macho_output,
1633 macho_symdef,
1634 macho_section,
1635 macho_sectalign,
1636 macho_segbase,
1637 null_directive,
1638 macho_filename,
1639 macho_cleanup,
1640 NULL /* pragma list */
1642 #endif
1644 #ifdef OF_MACHO64
1645 static const struct macho_fmt macho64_fmt = {
1647 MH_MAGIC_64,
1648 CPU_TYPE_X86_64,
1649 LC_SEGMENT_64,
1650 MACHO_HEADER64_SIZE,
1651 MACHO_SEGCMD64_SIZE,
1652 MACHO_SECTCMD64_SIZE,
1653 MACHO_NLIST64_SIZE,
1654 RL_MAX_64,
1655 X86_64_RELOC_UNSIGNED,
1656 X86_64_RELOC_SIGNED,
1657 X86_64_RELOC_TLV
1660 static void macho64_init(void)
1662 fmt = macho64_fmt;
1663 macho_init();
1665 /* add special symbol for ..gotpcrel */
1666 macho_gotpcrel_sect = seg_alloc() + 1;
1667 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false);
1670 const struct ofmt of_macho64 = {
1671 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1672 "macho64",
1675 null_debug_arr,
1676 &null_debug_form,
1677 macho_stdmac,
1678 macho64_init,
1679 null_setinfo,
1680 nasm_do_legacy_output,
1681 macho_output,
1682 macho_symdef,
1683 macho_section,
1684 macho_sectalign,
1685 macho_segbase,
1686 null_directive,
1687 macho_filename,
1688 macho_cleanup,
1689 NULL /* pragma list */
1691 #endif
1693 #endif
1696 * Local Variables:
1697 * mode:c
1698 * c-basic-offset:4
1699 * End:
1701 * end of file */