Makefiles: run "make alldeps"
[nasm.git] / output / outmacho.c
blob26a62ea6f63ac0da435ec85bdf8e8198bd4ef144
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>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "rbtree.h"
52 #include "output/outform.h"
53 #include "output/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 {
469 /* local */
470 r->ext = 0;
471 r->snum = fi;
472 adjust = -sect->size;
474 break;
476 case RL_SUB:
477 r->pcrel = 0;
478 r->type = X86_64_RELOC_SUBTRACTOR;
479 break;
481 case RL_GOT:
482 r->type = X86_64_RELOC_GOT;
483 goto needsym;
485 case RL_GOTLOAD:
486 r->type = X86_64_RELOC_GOT_LOAD;
487 goto needsym;
489 case RL_TLV:
490 r->type = fmt.reloc_tlv;
491 goto needsym;
493 needsym:
494 r->pcrel = 1;
495 if (section == NO_SEG) {
496 nasm_error(ERR_NONFATAL, "Unsupported use of use of WRT");
497 } else if (fi == NO_SECT) {
498 /* external */
499 r->snum = raa_read(extsyms, section);
500 } else {
501 /* internal */
502 struct symbol *sym = macho_find_gsym(s, offset, reltype != RL_TLV);
503 if (!sym)
504 goto bail;
505 r->snum = sym->initial_snum;
507 break;
510 /* NeXT as puts relocs in reversed order (address-wise) into the
511 ** files, so we do the same, doesn't seem to make much of a
512 ** difference either way */
513 r->next = sect->relocs;
514 sect->relocs = r;
515 if (r->ext)
516 sect->extreloc = 1;
517 ++sect->nreloc;
519 return adjust;
521 bail:
522 nasm_free(r);
523 return 0;
526 static void macho_output(int32_t secto, const void *data,
527 enum out_type type, uint64_t size,
528 int32_t section, int32_t wrt)
530 struct section *s;
531 int64_t addr, offset;
532 uint8_t mydata[16], *p;
533 bool is_bss;
534 enum reltype reltype;
536 if (secto == NO_SEG) {
537 if (type != OUT_RESERVE)
538 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
539 "[ABSOLUTE] space");
540 return;
543 s = get_section_by_index(secto);
545 if (s == NULL) {
546 nasm_error(ERR_WARNING, "attempt to assemble code in"
547 " section %d: defaulting to `.text'", secto);
548 s = get_section_by_name("__TEXT", "__text");
550 /* should never happen */
551 if (s == NULL)
552 nasm_panic(0, "text section not found");
555 is_bss = (s->flags & SECTION_TYPE) == S_ZEROFILL;
557 if (is_bss && type != OUT_RESERVE) {
558 nasm_error(ERR_WARNING, "attempt to initialize memory in "
559 "BSS section: ignored");
560 s->size += realsize(type, size);
561 return;
564 memset(mydata, 0, sizeof(mydata));
566 switch (type) {
567 case OUT_RESERVE:
568 if (!is_bss) {
569 nasm_error(ERR_WARNING, "uninitialized space declared in"
570 " %s,%s section: zeroing", s->segname, s->sectname);
572 sect_write(s, NULL, size);
573 } else
574 s->size += size;
576 break;
578 case OUT_RAWDATA:
579 if (section != NO_SEG)
580 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
582 sect_write(s, data, size);
583 break;
585 case OUT_ADDRESS:
587 int asize = abs((int)size);
589 addr = *(int64_t *)data;
590 if (section != NO_SEG) {
591 if (section % 2) {
592 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
593 " section base references");
594 } else if (wrt == NO_SEG) {
595 if (fmt.ptrsize == 8 && asize != 8) {
596 nasm_error(ERR_NONFATAL,
597 "Mach-O 64-bit format does not support"
598 " 32-bit absolute addresses");
599 } else {
600 add_reloc(s, section, addr, RL_ABS, asize);
602 } else {
603 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
604 " this use of WRT");
608 p = mydata;
609 WRITEADDR(p, addr, asize);
610 sect_write(s, mydata, asize);
611 break;
614 case OUT_REL2ADR:
615 nasm_assert(section != secto);
617 p = mydata;
618 offset = *(int64_t *)data;
619 addr = offset - size;
621 if (section != NO_SEG && section % 2) {
622 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
623 " section base references");
624 } else if (fmt.ptrsize == 8) {
625 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
626 " Macho-O relocation [2]");
627 } else if (wrt != NO_SEG) {
628 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
629 " this use of WRT");
630 wrt = NO_SEG; /* we can at least _try_ to continue */
631 } else {
632 addr += add_reloc(s, section, addr+size, RL_REL, 2);
635 WRITESHORT(p, addr);
636 sect_write(s, mydata, 2);
637 break;
639 case OUT_REL4ADR:
640 nasm_assert(section != secto);
642 p = mydata;
643 offset = *(int64_t *)data;
644 addr = offset - size;
645 reltype = RL_REL;
647 if (section != NO_SEG && section % 2) {
648 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
649 " section base references");
650 } else if (wrt == NO_SEG) {
651 if (fmt.ptrsize == 8 &&
652 (s->flags & S_ATTR_SOME_INSTRUCTIONS)) {
653 uint8_t opcode[2];
655 opcode[0] = opcode[1] = 0;
657 /* HACK: Retrieve instruction opcode */
658 if (likely(s->data->datalen >= 2)) {
659 saa_fread(s->data, s->data->datalen-2, opcode, 2);
660 } else if (s->data->datalen == 1) {
661 saa_fread(s->data, 0, opcode+1, 1);
664 if ((opcode[0] != 0x0f && (opcode[1] & 0xfe) == 0xe8) ||
665 (opcode[0] == 0x0f && (opcode[1] & 0xf0) == 0x80)) {
666 /* Direct call, jmp, or jcc */
667 reltype = RL_BRANCH;
670 } else if (wrt == macho_gotpcrel_sect) {
671 reltype = RL_GOT;
673 if ((s->flags & S_ATTR_SOME_INSTRUCTIONS) &&
674 s->data->datalen >= 3) {
675 uint8_t gotload[3];
677 /* HACK: Retrieve instruction opcode */
678 saa_fread(s->data, s->data->datalen-3, gotload, 3);
679 if ((gotload[0] & 0xf8) == 0x48 &&
680 gotload[1] == 0x8b &&
681 (gotload[2] & 0307) == 0005) {
682 /* movq <reg>,[rel sym wrt ..gotpcrel] */
683 reltype = RL_GOTLOAD;
686 } else if (wrt == macho_tlvp_sect) {
687 reltype = RL_TLV;
688 } else {
689 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
690 " this use of WRT");
691 /* continue with RL_REL */
694 addr += add_reloc(s, section, offset, reltype, 4);
695 WRITELONG(p, addr);
696 sect_write(s, mydata, 4);
697 break;
699 default:
700 nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
701 break;
705 static int32_t macho_section(char *name, int pass, int *bits)
707 char *sectionAttributes;
708 const struct sectmap *sm;
709 struct section *s;
710 const char *section, *segment;
711 uint32_t flags;
712 char *currentAttribute;
713 char *comma;
714 bool new_seg;
716 (void)pass;
718 /* Default to the appropriate number of bits. */
719 if (!name) {
720 *bits = fmt.ptrsize << 3;
721 name = ".text";
722 sectionAttributes = NULL;
723 } else {
724 sectionAttributes = name;
725 name = nasm_strsep(&sectionAttributes, " \t");
728 section = segment = NULL;
729 flags = 0;
731 comma = strchr(name, ',');
732 if (comma) {
733 int len;
735 *comma = '\0';
736 segment = name;
737 section = comma+1;
739 len = strlen(segment);
740 if (len == 0) {
741 nasm_error(ERR_NONFATAL, "empty segment name\n");
742 } else if (len >= 16) {
743 nasm_error(ERR_NONFATAL, "segment name %s too long\n", segment);
746 len = strlen(section);
747 if (len == 0) {
748 nasm_error(ERR_NONFATAL, "empty section name\n");
749 } else if (len >= 16) {
750 nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
753 if (!strcmp(section, "__text")) {
754 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
755 S_ATTR_PURE_INSTRUCTIONS;
756 } else if (!strcmp(section, "__bss")) {
757 flags = S_ZEROFILL;
758 } else {
759 flags = S_REGULAR;
761 } else {
762 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
763 /* make lookup into section name translation table */
764 if (!strcmp(name, sm->nasmsect)) {
765 segment = sm->segname;
766 section = sm->sectname;
767 flags = sm->flags;
768 goto found;
771 nasm_error(ERR_NONFATAL, "unknown section name\n");
772 return NO_SEG;
775 found:
776 /* try to find section with that name */
777 s = get_section_by_name(segment, section);
779 /* create it if it doesn't exist yet */
780 if (!s) {
781 new_seg = true;
783 s = *sectstail = nasm_zalloc(sizeof(struct section));
784 sectstail = &s->next;
786 s->data = saa_init(1L);
787 s->index = seg_alloc();
788 s->fileindex = ++seg_nsects;
789 s->align = -1;
790 s->pad = -1;
791 s->offset = -1;
792 s->by_name = false;
794 xstrncpy(s->segname, segment);
795 xstrncpy(s->sectname, section);
796 s->size = 0;
797 s->nreloc = 0;
798 s->flags = flags;
799 } else {
800 new_seg = false;
803 if (comma)
804 *comma = ','; /* Restore comma */
806 s->by_name = s->by_name || comma; /* Was specified by name */
808 flags = (uint32_t)-1;
810 while ((NULL != sectionAttributes)
811 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
812 if (0 != *currentAttribute) {
813 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
814 char *end;
815 int newAlignment, value;
817 value = strtoul(currentAttribute + 6, (char**)&end, 0);
818 newAlignment = alignlog2_32(value);
820 if (0 != *end) {
821 nasm_error(ERR_NONFATAL,
822 "unknown or missing alignment value \"%s\" "
823 "specified for section \"%s\"",
824 currentAttribute + 6,
825 name);
826 } else if (0 > newAlignment) {
827 nasm_error(ERR_NONFATAL,
828 "alignment of %d (for section \"%s\") is not "
829 "a power of two",
830 value,
831 name);
834 if (s->align < newAlignment)
835 s->align = newAlignment;
836 } else if (!nasm_stricmp("data", currentAttribute)) {
837 flags = S_REGULAR;
838 } else if (!nasm_stricmp("code", currentAttribute) ||
839 !nasm_stricmp("text", currentAttribute)) {
840 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
841 S_ATTR_PURE_INSTRUCTIONS;
842 } else if (!nasm_stricmp("mixed", currentAttribute)) {
843 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS;
844 } else if (!nasm_stricmp("bss", currentAttribute)) {
845 flags = S_ZEROFILL;
846 } else {
847 nasm_error(ERR_NONFATAL,
848 "unknown section attribute %s for section %s",
849 currentAttribute,
850 name);
854 if (flags != (uint32_t)-1) {
855 if (!new_seg && s->flags != flags) {
856 nasm_error(ERR_NONFATAL,
857 "inconsistent section attributes for section %s\n",
858 name);
859 } else {
860 s->flags = flags;
865 return s->index;
868 static void macho_symdef(char *name, int32_t section, int64_t offset,
869 int is_global, char *special)
871 struct symbol *sym;
873 if (special) {
874 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
875 "not support any special symbol types");
876 return;
879 if (is_global == 3) {
880 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
881 "(yet) support forward reference fixups.");
882 return;
885 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
887 * This is a NASM special symbol. We never allow it into
888 * the Macho-O symbol table, even if it's a valid one. If it
889 * _isn't_ a valid one, we should barf immediately.
891 if (strcmp(name, "..gotpcrel") && strcmp(name, "..tlvp"))
892 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
893 return;
896 sym = *symstail = nasm_zalloc(sizeof(struct symbol));
897 sym->next = NULL;
898 symstail = &sym->next;
900 sym->name = name;
901 sym->strx = strslen;
902 sym->type = 0;
903 sym->desc = 0;
904 sym->symv.key = offset;
905 sym->initial_snum = -1;
907 /* external and common symbols get N_EXT */
908 if (is_global != 0) {
909 sym->type |= N_EXT;
912 if (section == NO_SEG) {
913 /* symbols in no section get absolute */
914 sym->type |= N_ABS;
915 sym->sect = NO_SECT;
917 /* all absolute symbols are available to use as references */
918 absolute_sect.gsyms = rb_insert(absolute_sect.gsyms, &sym->symv);
919 } else {
920 struct section *s = get_section_by_index(section);
922 sym->type |= N_SECT;
924 /* get the in-file index of the section the symbol was defined in */
925 sym->sect = s ? s->fileindex : NO_SECT;
927 /* track the initially allocated symbol number for use in future fix-ups */
928 sym->initial_snum = nsyms;
930 if (!s) {
931 /* remember symbol number of references to external
932 ** symbols, this works because every external symbol gets
933 ** its own section number allocated internally by nasm and
934 ** can so be used as a key */
935 extsyms = raa_write(extsyms, section, nsyms);
937 switch (is_global) {
938 case 1:
939 case 2:
940 /* there isn't actually a difference between global
941 ** and common symbols, both even have their size in
942 ** sym->symv.key */
943 sym->type = N_EXT;
944 break;
946 default:
947 /* give an error on unfound section if it's not an
948 ** external or common symbol (assemble_file() does a
949 ** seg_alloc() on every call for them) */
950 nasm_panic(0, "in-file index for section %d not found, is_global = %d", section, is_global);
951 break;
953 } else if (is_global) {
954 s->gsyms = rb_insert(s->gsyms, &sym->symv);
957 ++nsyms;
960 static void macho_sectalign(int32_t seg, unsigned int value)
962 struct section *s;
963 int align;
965 nasm_assert(!(seg & 1));
967 s = get_section_by_index(seg);
969 if (!s || !is_power2(value))
970 return;
972 align = alignlog2_32(value);
973 if (s->align < align)
974 s->align = align;
977 static int32_t macho_segbase(int32_t section)
979 return section;
982 static void macho_filename(char *inname, char *outname)
984 standard_extension(inname, outname, ".o");
987 extern macros_t macho_stdmac[];
989 /* Comparison function for qsort symbol layout. */
990 static int layout_compare (const struct symbol **s1,
991 const struct symbol **s2)
993 return (strcmp ((*s1)->name, (*s2)->name));
996 /* The native assembler does a few things in a similar function
998 * Remove temporary labels
999 * Sort symbols according to local, external, undefined (by name)
1000 * Order the string table
1002 We do not remove temporary labels right now.
1004 numsyms is the total number of symbols we have. strtabsize is the
1005 number entries in the string table. */
1007 static void macho_layout_symbols (uint32_t *numsyms,
1008 uint32_t *strtabsize)
1010 struct symbol *sym, **symp;
1011 uint32_t i,j;
1013 *numsyms = 0;
1014 *strtabsize = sizeof (char);
1016 symp = &syms;
1018 while ((sym = *symp)) {
1019 /* Undefined symbols are now external. */
1020 if (sym->type == N_UNDF)
1021 sym->type |= N_EXT;
1023 if ((sym->type & N_EXT) == 0) {
1024 sym->snum = *numsyms;
1025 *numsyms = *numsyms + 1;
1026 nlocalsym++;
1028 else {
1029 if ((sym->type & N_TYPE) != N_UNDF) {
1030 nextdefsym++;
1031 } else {
1032 nundefsym++;
1035 /* If we handle debug info we'll want
1036 to check for it here instead of just
1037 adding the symbol to the string table. */
1038 sym->strx = *strtabsize;
1039 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
1040 *strtabsize += strlen(sym->name) + 1;
1042 symp = &(sym->next);
1045 /* Next, sort the symbols. Most of this code is a direct translation from
1046 the Apple cctools symbol layout. We need to keep compatibility with that. */
1047 /* Set the indexes for symbol groups into the symbol table */
1048 ilocalsym = 0;
1049 iextdefsym = nlocalsym;
1050 iundefsym = nlocalsym + nextdefsym;
1052 /* allocate arrays for sorting externals by name */
1053 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
1054 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
1056 i = 0;
1057 j = 0;
1059 symp = &syms;
1061 while ((sym = *symp)) {
1063 if((sym->type & N_EXT) == 0) {
1064 sym->strx = *strtabsize;
1065 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
1066 *strtabsize += strlen(sym->name) + 1;
1068 else {
1069 if((sym->type & N_TYPE) != N_UNDF) {
1070 extdefsyms[i++] = sym;
1071 } else {
1072 undefsyms[j++] = sym;
1075 symp = &(sym->next);
1078 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
1079 (int (*)(const void *, const void *))layout_compare);
1080 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
1081 (int (*)(const void *, const void *))layout_compare);
1083 for(i = 0; i < nextdefsym; i++) {
1084 extdefsyms[i]->snum = *numsyms;
1085 *numsyms += 1;
1087 for(j = 0; j < nundefsym; j++) {
1088 undefsyms[j]->snum = *numsyms;
1089 *numsyms += 1;
1093 /* Calculate some values we'll need for writing later. */
1095 static void macho_calculate_sizes (void)
1097 struct section *s;
1098 int fi;
1100 /* count sections and calculate in-memory and in-file offsets */
1101 for (s = sects; s != NULL; s = s->next) {
1102 uint64_t newaddr;
1104 /* recalculate segment address based on alignment and vm size */
1105 s->addr = seg_vmsize;
1107 /* we need section alignment to calculate final section address */
1108 if (s->align == -1)
1109 s->align = DEFAULT_SECTION_ALIGNMENT;
1111 newaddr = ALIGN(s->addr, 1 << s->align);
1112 s->addr = newaddr;
1114 seg_vmsize = newaddr + s->size;
1116 /* zerofill sections aren't actually written to the file */
1117 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1119 * LLVM/Xcode as always aligns the section data to 4
1120 * bytes; there is a comment in the LLVM source code that
1121 * perhaps aligning to pointer size would be better.
1123 s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
1124 s->offset = seg_filesize + s->pad;
1125 seg_filesize += s->size + s->pad;
1129 /* calculate size of all headers, load commands and sections to
1130 ** get a pointer to the start of all the raw data */
1131 if (seg_nsects > 0) {
1132 ++head_ncmds;
1133 head_sizeofcmds += fmt.segcmd_size + seg_nsects * fmt.sectcmd_size;
1136 if (nsyms > 0) {
1137 ++head_ncmds;
1138 head_sizeofcmds += MACHO_SYMCMD_SIZE;
1141 if (seg_nsects > MAX_SECT) {
1142 nasm_fatal(0, "MachO output is limited to %d sections\n",
1143 MAX_SECT);
1146 /* Create a table of sections by file index to avoid linear search */
1147 sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
1148 sectstab[NO_SECT] = &absolute_sect;
1149 for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
1150 sectstab[fi] = s;
1153 /* Write out the header information for the file. */
1155 static void macho_write_header (void)
1157 fwriteint32_t(fmt.mh_magic, ofile); /* magic */
1158 fwriteint32_t(fmt.cpu_type, ofile); /* CPU type */
1159 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1160 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1161 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
1162 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
1163 fwriteint32_t(0, ofile); /* no flags */
1164 fwritezero(fmt.header_size - 7*4, ofile); /* reserved fields */
1167 /* Write out the segment load command at offset. */
1169 static uint32_t macho_write_segment (uint64_t offset)
1171 uint64_t rel_base = alignptr(offset + seg_filesize);
1172 uint32_t s_reloff = 0;
1173 struct section *s;
1175 fwriteint32_t(fmt.lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
1177 /* size of load command including section load commands */
1178 fwriteint32_t(fmt.segcmd_size + seg_nsects * fmt.sectcmd_size,
1179 ofile);
1181 /* in an MH_OBJECT file all sections are in one unnamed (name
1182 ** all zeros) segment */
1183 fwritezero(16, ofile);
1184 fwriteptr(0, ofile); /* in-memory offset */
1185 fwriteptr(seg_vmsize, ofile); /* in-memory size */
1186 fwriteptr(offset, ofile); /* in-file offset to data */
1187 fwriteptr(seg_filesize, ofile); /* in-file size */
1188 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1189 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1190 fwriteint32_t(seg_nsects, ofile); /* number of sections */
1191 fwriteint32_t(0, ofile); /* no flags */
1193 /* emit section headers */
1194 for (s = sects; s != NULL; s = s->next) {
1195 if (s->nreloc) {
1196 nasm_assert((s->flags & SECTION_TYPE) != S_ZEROFILL);
1197 s->flags |= S_ATTR_LOC_RELOC;
1198 if (s->extreloc)
1199 s->flags |= S_ATTR_EXT_RELOC;
1200 } else if (!strcmp(s->segname, "__DATA") &&
1201 !strcmp(s->sectname, "__const") &&
1202 !s->by_name &&
1203 !get_section_by_name("__TEXT", "__const")) {
1205 * The MachO equivalent to .rodata can be either
1206 * __DATA,__const or __TEXT,__const; the latter only if
1207 * there are no relocations. However, when mixed it is
1208 * better to specify the segments explicitly.
1210 xstrncpy(s->segname, "__TEXT");
1213 nasm_write(s->sectname, sizeof(s->sectname), ofile);
1214 nasm_write(s->segname, sizeof(s->segname), ofile);
1215 fwriteptr(s->addr, ofile);
1216 fwriteptr(s->size, ofile);
1218 /* dummy data for zerofill sections or proper values */
1219 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1220 nasm_assert(s->pad != (uint32_t)-1);
1221 offset += s->pad;
1222 fwriteint32_t(offset, ofile);
1223 offset += s->size;
1224 /* Write out section alignment, as a power of two.
1225 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1226 fwriteint32_t(s->align, ofile);
1227 /* To be compatible with cctools as we emit
1228 a zero reloff if we have no relocations. */
1229 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1230 fwriteint32_t(s->nreloc, ofile);
1232 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
1233 } else {
1234 fwriteint32_t(0, ofile);
1235 fwriteint32_t(s->align, ofile);
1236 fwriteint32_t(0, ofile);
1237 fwriteint32_t(0, ofile);
1240 fwriteint32_t(s->flags, ofile); /* flags */
1241 fwriteint32_t(0, ofile); /* reserved */
1242 fwriteptr(0, ofile); /* reserved */
1245 rel_padcnt = rel_base - offset;
1246 offset = rel_base + s_reloff;
1248 return offset;
1251 /* For a given chain of relocs r, write out the entire relocation
1252 chain to the object file. */
1254 static void macho_write_relocs (struct reloc *r)
1256 while (r) {
1257 uint32_t word2;
1259 fwriteint32_t(r->addr, ofile); /* reloc offset */
1261 word2 = r->snum;
1262 word2 |= r->pcrel << 24;
1263 word2 |= r->length << 25;
1264 word2 |= r->ext << 27;
1265 word2 |= r->type << 28;
1266 fwriteint32_t(word2, ofile); /* reloc data */
1267 r = r->next;
1271 /* Write out the section data. */
1272 static void macho_write_section (void)
1274 struct section *s;
1275 struct reloc *r;
1276 uint8_t *p;
1277 int32_t len;
1278 int64_t l;
1279 union offset {
1280 uint64_t val;
1281 uint8_t buf[8];
1282 } blk;
1284 for (s = sects; s != NULL; s = s->next) {
1285 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1286 continue;
1288 /* Like a.out Mach-O references things in the data or bss
1289 * sections by addresses which are actually relative to the
1290 * start of the _text_ section, in the _file_. See outaout.c
1291 * for more information. */
1292 saa_rewind(s->data);
1293 for (r = s->relocs; r != NULL; r = r->next) {
1294 len = (uint32_t)1 << r->length;
1295 if (len > 4) /* Can this ever be an issue?! */
1296 len = 8;
1297 blk.val = 0;
1298 saa_fread(s->data, r->addr, blk.buf, len);
1300 /* get offset based on relocation type */
1301 #ifdef WORDS_LITTLEENDIAN
1302 l = blk.val;
1303 #else
1304 l = blk.buf[0];
1305 l += ((int64_t)blk.buf[1]) << 8;
1306 l += ((int64_t)blk.buf[2]) << 16;
1307 l += ((int64_t)blk.buf[3]) << 24;
1308 l += ((int64_t)blk.buf[4]) << 32;
1309 l += ((int64_t)blk.buf[5]) << 40;
1310 l += ((int64_t)blk.buf[6]) << 48;
1311 l += ((int64_t)blk.buf[7]) << 56;
1312 #endif
1314 /* If the relocation is internal add to the current section
1315 offset. Otherwise the only value we need is the symbol
1316 offset which we already have. The linker takes care
1317 of the rest of the address. */
1318 if (!r->ext) {
1319 /* generate final address by section address and offset */
1320 nasm_assert(r->snum <= seg_nsects);
1321 l += sectstab[r->snum]->addr;
1322 if (r->pcrel)
1323 l -= s->addr;
1326 /* write new offset back */
1327 p = blk.buf;
1328 WRITEDLONG(p, l);
1329 saa_fwrite(s->data, r->addr, blk.buf, len);
1332 /* dump the section data to file */
1333 fwritezero(s->pad, ofile);
1334 saa_fpwrite(s->data, ofile);
1337 /* pad last section up to reloc entries on pointer boundary */
1338 fwritezero(rel_padcnt, ofile);
1340 /* emit relocation entries */
1341 for (s = sects; s != NULL; s = s->next)
1342 macho_write_relocs (s->relocs);
1345 /* Write out the symbol table. We should already have sorted this
1346 before now. */
1347 static void macho_write_symtab (void)
1349 struct symbol *sym;
1350 uint64_t i;
1352 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1354 for (sym = syms; sym != NULL; sym = sym->next) {
1355 if ((sym->type & N_EXT) == 0) {
1356 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1357 nasm_write(&sym->type, 1, ofile); /* symbol type */
1358 nasm_write(&sym->sect, 1, ofile); /* section */
1359 fwriteint16_t(sym->desc, ofile); /* description */
1361 /* Fix up the symbol value now that we know the final section
1362 sizes. */
1363 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1364 nasm_assert(sym->sect <= seg_nsects);
1365 sym->symv.key += sectstab[sym->sect]->addr;
1368 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1372 for (i = 0; i < nextdefsym; i++) {
1373 sym = extdefsyms[i];
1374 fwriteint32_t(sym->strx, ofile);
1375 nasm_write(&sym->type, 1, ofile); /* symbol type */
1376 nasm_write(&sym->sect, 1, ofile); /* section */
1377 fwriteint16_t(sym->desc, ofile); /* description */
1379 /* Fix up the symbol value now that we know the final section
1380 sizes. */
1381 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1382 nasm_assert(sym->sect <= seg_nsects);
1383 sym->symv.key += sectstab[sym->sect]->addr;
1386 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1389 for (i = 0; i < nundefsym; i++) {
1390 sym = undefsyms[i];
1391 fwriteint32_t(sym->strx, ofile);
1392 nasm_write(&sym->type, 1, ofile); /* symbol type */
1393 nasm_write(&sym->sect, 1, ofile); /* section */
1394 fwriteint16_t(sym->desc, ofile); /* description */
1396 /* Fix up the symbol value now that we know the final section
1397 sizes. */
1398 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1399 nasm_assert(sym->sect <= seg_nsects);
1400 sym->symv.key += sectstab[sym->sect]->addr;
1403 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1408 /* Fixup the snum in the relocation entries, we should be
1409 doing this only for externally referenced symbols. */
1410 static void macho_fixup_relocs (struct reloc *r)
1412 struct symbol *sym;
1414 while (r != NULL) {
1415 if (r->ext) {
1416 for (sym = syms; sym != NULL; sym = sym->next) {
1417 if (sym->initial_snum == r->snum) {
1418 r->snum = sym->snum;
1419 break;
1423 r = r->next;
1427 /* Write out the object file. */
1429 static void macho_write (void)
1431 uint64_t offset = 0;
1433 /* mach-o object file structure:
1435 ** mach header
1436 ** uint32_t magic
1437 ** int cpu type
1438 ** int cpu subtype
1439 ** uint32_t mach file type
1440 ** uint32_t number of load commands
1441 ** uint32_t size of all load commands
1442 ** (includes section struct size of segment command)
1443 ** uint32_t flags
1445 ** segment command
1446 ** uint32_t command type == LC_SEGMENT[_64]
1447 ** uint32_t size of load command
1448 ** (including section load commands)
1449 ** char[16] segment name
1450 ** pointer in-memory offset
1451 ** pointer in-memory size
1452 ** pointer in-file offset to data area
1453 ** pointer in-file size
1454 ** (in-memory size excluding zerofill sections)
1455 ** int maximum vm protection
1456 ** int initial vm protection
1457 ** uint32_t number of sections
1458 ** uint32_t flags
1460 ** section commands
1461 ** char[16] section name
1462 ** char[16] segment name
1463 ** pointer in-memory offset
1464 ** pointer in-memory size
1465 ** uint32_t in-file offset
1466 ** uint32_t alignment
1467 ** (irrelevant in MH_OBJECT)
1468 ** uint32_t in-file offset of relocation entires
1469 ** uint32_t number of relocations
1470 ** uint32_t flags
1471 ** uint32_t reserved
1472 ** uint32_t reserved
1474 ** symbol table command
1475 ** uint32_t command type == LC_SYMTAB
1476 ** uint32_t size of load command
1477 ** uint32_t symbol table offset
1478 ** uint32_t number of symbol table entries
1479 ** uint32_t string table offset
1480 ** uint32_t string table size
1482 ** raw section data
1484 ** padding to pointer boundary
1486 ** relocation data (struct reloc)
1487 ** int32_t offset
1488 ** uint data (symbolnum, pcrel, length, extern, type)
1490 ** symbol table data (struct nlist)
1491 ** int32_t string table entry number
1492 ** uint8_t type
1493 ** (extern, absolute, defined in section)
1494 ** uint8_t section
1495 ** (0 for global symbols, section number of definition (>= 1, <=
1496 ** 254) for local symbols, size of variable for common symbols
1497 ** [type == extern])
1498 ** int16_t description
1499 ** (for stab debugging format)
1500 ** pointer value (i.e. file offset) of symbol or stab offset
1502 ** string table data
1503 ** list of null-terminated strings
1506 /* Emit the Mach-O header. */
1507 macho_write_header();
1509 offset = fmt.header_size + head_sizeofcmds;
1511 /* emit the segment load command */
1512 if (seg_nsects > 0)
1513 offset = macho_write_segment (offset);
1514 else
1515 nasm_error(ERR_WARNING, "no sections?");
1517 if (nsyms > 0) {
1518 /* write out symbol command */
1519 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1520 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1521 fwriteint32_t(offset, ofile); /* symbol table offset */
1522 fwriteint32_t(nsyms, ofile); /* number of symbol
1523 ** table entries */
1524 offset += nsyms * fmt.nlist_size;
1525 fwriteint32_t(offset, ofile); /* string table offset */
1526 fwriteint32_t(strslen, ofile); /* string table size */
1529 /* emit section data */
1530 if (seg_nsects > 0)
1531 macho_write_section ();
1533 /* emit symbol table if we have symbols */
1534 if (nsyms > 0)
1535 macho_write_symtab ();
1537 /* we don't need to pad here, we are already aligned */
1539 /* emit string table */
1540 saa_fpwrite(strs, ofile);
1542 /* We do quite a bit here, starting with finalizing all of the data
1543 for the object file, writing, and then freeing all of the data from
1544 the file. */
1546 static void macho_cleanup(void)
1548 struct section *s;
1549 struct reloc *r;
1550 struct symbol *sym;
1552 /* Sort all symbols. */
1553 macho_layout_symbols (&nsyms, &strslen);
1555 /* Fixup relocation entries */
1556 for (s = sects; s != NULL; s = s->next) {
1557 macho_fixup_relocs (s->relocs);
1560 /* First calculate and finalize needed values. */
1561 macho_calculate_sizes();
1562 macho_write();
1564 /* free up everything */
1565 while (sects->next) {
1566 s = sects;
1567 sects = sects->next;
1569 saa_free(s->data);
1570 while (s->relocs != NULL) {
1571 r = s->relocs;
1572 s->relocs = s->relocs->next;
1573 nasm_free(r);
1576 nasm_free(s);
1579 saa_free(strs);
1580 raa_free(extsyms);
1582 while (syms) {
1583 sym = syms;
1584 syms = syms->next;
1585 nasm_free (sym);
1588 nasm_free(extdefsyms);
1589 nasm_free(undefsyms);
1590 nasm_free(sectstab);
1593 #ifdef OF_MACHO32
1594 static const struct macho_fmt macho32_fmt = {
1596 MH_MAGIC,
1597 CPU_TYPE_I386,
1598 LC_SEGMENT,
1599 MACHO_HEADER_SIZE,
1600 MACHO_SEGCMD_SIZE,
1601 MACHO_SECTCMD_SIZE,
1602 MACHO_NLIST_SIZE,
1603 RL_MAX_32,
1604 GENERIC_RELOC_VANILLA,
1605 GENERIC_RELOC_VANILLA,
1606 GENERIC_RELOC_TLV
1609 static void macho32_init(void)
1611 fmt = macho32_fmt;
1612 macho_init();
1614 macho_gotpcrel_sect = NO_SEG;
1617 struct ofmt of_macho32 = {
1618 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1619 "macho32",
1622 null_debug_arr,
1623 &null_debug_form,
1624 macho_stdmac,
1625 macho32_init,
1626 null_setinfo,
1627 macho_output,
1628 macho_symdef,
1629 macho_section,
1630 macho_sectalign,
1631 macho_segbase,
1632 null_directive,
1633 macho_filename,
1634 macho_cleanup
1636 #endif
1638 #ifdef OF_MACHO64
1639 static const struct macho_fmt macho64_fmt = {
1641 MH_MAGIC_64,
1642 CPU_TYPE_X86_64,
1643 LC_SEGMENT_64,
1644 MACHO_HEADER64_SIZE,
1645 MACHO_SEGCMD64_SIZE,
1646 MACHO_SECTCMD64_SIZE,
1647 MACHO_NLIST64_SIZE,
1648 RL_MAX_64,
1649 X86_64_RELOC_UNSIGNED,
1650 X86_64_RELOC_SIGNED,
1651 X86_64_RELOC_TLV
1654 static void macho64_init(void)
1656 fmt = macho64_fmt;
1657 macho_init();
1659 /* add special symbol for ..gotpcrel */
1660 macho_gotpcrel_sect = seg_alloc() + 1;
1661 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false);
1664 struct ofmt of_macho64 = {
1665 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1666 "macho64",
1669 null_debug_arr,
1670 &null_debug_form,
1671 macho_stdmac,
1672 macho64_init,
1673 null_setinfo,
1674 macho_output,
1675 macho_symdef,
1676 macho_section,
1677 macho_sectalign,
1678 macho_segbase,
1679 null_directive,
1680 macho_filename,
1681 macho_cleanup
1683 #endif
1685 #endif
1688 * Local Variables:
1689 * mode:c
1690 * c-basic-offset:4
1691 * End:
1693 * end of file */