macho: Improve macho_calculate_sizes
[nasm.git] / output / outmacho.c
bloba6a16de65286367122c06b8dc3d5ad0ce16338d8
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 "saa.h"
49 #include "raa.h"
50 #include "rbtree.h"
51 #include "outform.h"
52 #include "outlib.h"
54 #if defined(OF_MACHO) || defined(OF_MACHO64)
56 /* Mach-O in-file header structure sizes */
57 #define MACHO_HEADER_SIZE 28
58 #define MACHO_SEGCMD_SIZE 56
59 #define MACHO_SECTCMD_SIZE 68
60 #define MACHO_SYMCMD_SIZE 24
61 #define MACHO_NLIST_SIZE 12
62 #define MACHO_RELINFO_SIZE 8
64 #define MACHO_HEADER64_SIZE 32
65 #define MACHO_SEGCMD64_SIZE 72
66 #define MACHO_SECTCMD64_SIZE 80
67 #define MACHO_NLIST64_SIZE 16
69 /* Mach-O file header values */
70 #define MH_MAGIC 0xfeedface
71 #define MH_MAGIC_64 0xfeedfacf
72 #define CPU_TYPE_I386 7 /* x86 platform */
73 #define CPU_TYPE_X86_64 0x01000007 /* x86-64 platform */
74 #define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */
75 #define MH_OBJECT 0x1 /* object file */
77 /* Mach-O load commands */
78 #define LC_SEGMENT 0x1 /* 32-bit segment load cmd */
79 #define LC_SEGMENT_64 0x19 /* 64-bit segment load cmd */
80 #define LC_SYMTAB 0x2 /* symbol table load command */
82 /* Mach-O relocations numbers */
84 /* Generic relocs, used by i386 Mach-O */
85 #define GENERIC_RELOC_VANILLA 0 /* Generic relocation */
86 #define GENERIC_RELOC_TLV 5 /* Thread local */
88 #define X86_64_RELOC_UNSIGNED 0 /* Absolute address */
89 #define X86_64_RELOC_SIGNED 1 /* Signed 32-bit disp */
90 #define X86_64_RELOC_BRANCH 2 /* CALL/JMP with 32-bit disp */
91 #define X86_64_RELOC_GOT_LOAD 3 /* MOVQ of GOT entry */
92 #define X86_64_RELOC_GOT 4 /* Different GOT entry */
93 #define X86_64_RELOC_SUBTRACTOR 5 /* Subtracting two symbols */
94 #define X86_64_RELOC_SIGNED_1 6 /* SIGNED with -1 addend */
95 #define X86_64_RELOC_SIGNED_2 7 /* SIGNED with -2 addend */
96 #define X86_64_RELOC_SIGNED_4 8 /* SIGNED with -4 addend */
97 #define X86_64_RELOC_TLV 9 /* Thread local */
99 /* Mach-O VM permission constants */
100 #define VM_PROT_NONE (0x00)
101 #define VM_PROT_READ (0x01)
102 #define VM_PROT_WRITE (0x02)
103 #define VM_PROT_EXECUTE (0x04)
105 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
106 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
108 /* Our internal relocation types */
109 enum reltype {
110 RL_ABS, /* Absolute relocation */
111 RL_REL, /* Relative relocation */
112 RL_TLV, /* Thread local */
113 RL_BRANCH, /* Relative direct branch */
114 RL_SUB, /* X86_64_RELOC_SUBTRACT */
115 RL_GOT, /* X86_64_RELOC_GOT */
116 RL_GOTLOAD /* X86_64_RELOC_GOT_LOAD */
118 #define RL_MAX_32 RL_TLV
119 #define RL_MAX_64 RL_GOTLOAD
121 struct macho_fmt {
122 uint32_t ptrsize; /* Pointer size in bytes */
123 uint32_t mh_magic; /* Which magic number to use */
124 uint32_t cpu_type; /* Which CPU type */
125 uint32_t lc_segment; /* Which segment load command */
126 uint32_t header_size; /* Header size */
127 uint32_t segcmd_size; /* Segment command size */
128 uint32_t sectcmd_size; /* Section command size */
129 uint32_t nlist_size; /* Nlist (symbol) size */
130 enum reltype maxreltype; /* Maximum entry in enum reltype permitted */
131 uint32_t reloc_abs; /* Absolute relocation type */
132 uint32_t reloc_rel; /* Relative relocation type */
133 uint32_t reloc_tlv; /* Thread local relocation type */
136 static struct macho_fmt fmt;
138 static void fwriteptr(uint64_t data, FILE * fp)
140 fwriteaddr(data, fmt.ptrsize, fp);
143 struct section {
144 /* nasm internal data */
145 struct section *next;
146 struct SAA *data;
147 int32_t index;
148 int32_t fileindex;
149 struct reloc *relocs;
150 struct rbtree *gsyms; /* Global symbols in section */
151 int align;
152 bool by_name; /* This section was specified by full MachO name */
154 /* data that goes into the file */
155 char sectname[16]; /* what this section is called */
156 char segname[16]; /* segment this section will be in */
157 uint64_t addr; /* in-memory address (subject to alignment) */
158 uint64_t size; /* in-memory and -file size */
159 uint64_t offset; /* in-file offset */
160 uint32_t pad; /* padding bytes before section */
161 uint32_t nreloc; /* relocation entry count */
162 uint32_t flags; /* type and attributes (masked) */
163 uint32_t extreloc; /* external relocations */
166 #define SECTION_TYPE 0x000000ff /* section type mask */
168 #define S_REGULAR (0x0) /* standard section */
169 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
171 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
172 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
173 machine instructions */
174 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
175 relocation entries */
176 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
177 relocation entries */
178 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
179 machine instructions */
181 /* Fake section for absolute symbols, *not* part of the section linked list */
182 static struct section absolute_sect;
184 static const struct sectmap {
185 const char *nasmsect;
186 const char *segname;
187 const char *sectname;
188 const int32_t flags;
189 } sectmap[] = {
190 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
191 {".data", "__DATA", "__data", S_REGULAR},
192 {".rodata", "__DATA", "__const", S_REGULAR},
193 {".bss", "__DATA", "__bss", S_ZEROFILL},
194 {NULL, NULL, NULL, 0}
197 struct reloc {
198 /* nasm internal data */
199 struct reloc *next;
201 /* data that goes into the file */
202 int32_t addr; /* op's offset in section */
203 uint32_t snum:24, /* contains symbol index if
204 ** ext otherwise in-file
205 ** section number */
206 pcrel:1, /* relative relocation */
207 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
208 ext:1, /* external symbol referenced */
209 type:4; /* reloc type */
212 #define R_ABS 0 /* absolute relocation */
213 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
214 ** highest bit == 1 */
216 struct symbol {
217 /* nasm internal data */
218 struct rbtree symv; /* Global symbol rbtree; "key" contains the
219 symbol offset. */
220 struct symbol *next; /* next symbol in the list */
221 char *name; /* name of this symbol */
222 int32_t initial_snum; /* symbol number used above in reloc */
223 int32_t snum; /* true snum for reloc */
225 /* data that goes into the file */
226 uint32_t strx; /* string table index */
227 uint8_t type; /* symbol type */
228 uint8_t sect; /* NO_SECT or section number */
229 uint16_t desc; /* for stab debugging, 0 for us */
232 /* symbol type bits */
233 #define N_EXT 0x01 /* global or external symbol */
235 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
236 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
237 #define N_SECT 0xe /* defined symbol, n_sect holds
238 ** section number */
240 #define N_TYPE 0x0e /* type bit mask */
242 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
244 /* special section number values */
245 #define NO_SECT 0 /* no section, invalid */
246 #define MAX_SECT 255 /* maximum number of sections */
248 static struct section *sects, **sectstail, **sectstab;
249 static struct symbol *syms, **symstail;
250 static uint32_t nsyms;
252 /* These variables are set by macho_layout_symbols() to organize
253 the symbol table and string table in order the dynamic linker
254 expects. They are then used in macho_write() to put out the
255 symbols and strings in that order.
257 The order of the symbol table is:
258 local symbols
259 defined external symbols (sorted by name)
260 undefined external symbols (sorted by name)
262 The order of the string table is:
263 strings for external symbols
264 strings for local symbols
266 static uint32_t ilocalsym = 0;
267 static uint32_t iextdefsym = 0;
268 static uint32_t iundefsym = 0;
269 static uint32_t nlocalsym;
270 static uint32_t nextdefsym;
271 static uint32_t nundefsym;
272 static struct symbol **extdefsyms = NULL;
273 static struct symbol **undefsyms = NULL;
275 static struct RAA *extsyms;
276 static struct SAA *strs;
277 static uint32_t strslen;
279 /* Global file information. This should be cleaned up into either
280 a structure or as function arguments. */
281 static uint32_t head_ncmds = 0;
282 static uint32_t head_sizeofcmds = 0;
283 static uint64_t seg_filesize = 0;
284 static uint64_t seg_vmsize = 0;
285 static uint32_t seg_nsects = 0;
286 static uint64_t rel_padcnt = 0;
288 #define xstrncpy(xdst, xsrc) \
289 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
290 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
291 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
293 #define alignint32_t(x) \
294 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
296 #define alignint64_t(x) \
297 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
299 #define alignptr(x) \
300 ALIGN(x, fmt.ptrsize) /* align x to output format width */
302 static struct section *get_section_by_name(const char *segname,
303 const char *sectname)
305 struct section *s;
307 for (s = sects; s != NULL; s = s->next)
308 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
309 break;
311 return s;
314 static struct section *get_section_by_index(const int32_t index)
316 struct section *s;
318 for (s = sects; s != NULL; s = s->next)
319 if (index == s->index)
320 break;
322 return s;
326 * Special section numbers which are used to define Mach-O special
327 * symbols, which can be used with WRT to provide PIC relocation
328 * types.
330 static int32_t macho_tlvp_sect;
331 static int32_t macho_gotpcrel_sect;
333 static void macho_init(void)
335 sects = NULL;
336 sectstail = &sects;
338 /* Fake section for absolute symbols */
339 absolute_sect.index = NO_SEG;
341 syms = NULL;
342 symstail = &syms;
343 nsyms = 0;
344 nlocalsym = 0;
345 nextdefsym = 0;
346 nundefsym = 0;
348 extsyms = raa_init();
349 strs = saa_init(1L);
351 /* string table starts with a zero byte so index 0 is an empty string */
352 saa_wbytes(strs, zero_buffer, 1);
353 strslen = 1;
355 /* add special symbol for TLVP */
356 macho_tlvp_sect = seg_alloc() + 1;
357 define_label("..tlvp", macho_tlvp_sect, 0L, NULL, false, false);
361 static void sect_write(struct section *sect,
362 const uint8_t *data, uint32_t len)
364 saa_wbytes(sect->data, data, len);
365 sect->size += len;
369 * Find a suitable global symbol for a ..gotpcrel or ..tlvp reference
371 static struct symbol *macho_find_gsym(struct section *s,
372 uint64_t offset, bool exact)
374 struct rbtree *srb;
376 srb = rb_search(s->gsyms, offset);
378 if (!srb || (exact && srb->key != offset)) {
379 nasm_error(ERR_NONFATAL, "unable to find a suitable %s symbol"
380 " for this reference",
381 s == &absolute_sect ? "absolute" : "global");
382 return NULL;
385 return container_of(srb, struct symbol, symv);
388 static int64_t add_reloc(struct section *sect, int32_t section,
389 int64_t offset,
390 enum reltype reltype, int bytes)
392 struct reloc *r;
393 struct section *s;
394 int32_t fi;
395 int64_t adjust;
397 /* Double check this is a valid relocation type for this platform */
398 nasm_assert(reltype <= fmt.maxreltype);
400 /* the current end of the section will be the symbol's address for
401 ** now, might have to be fixed by macho_fixup_relocs() later on. make
402 ** sure we don't make the symbol scattered by setting the highest
403 ** bit by accident */
404 r = nasm_malloc(sizeof(struct reloc));
405 r->addr = sect->size & ~R_SCATTERED;
406 r->ext = 1;
407 adjust = bytes;
409 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
410 r->length = ilog2_32(bytes);
412 /* set default relocation values */
413 r->type = fmt.reloc_abs;
414 r->pcrel = 0;
415 r->snum = R_ABS;
417 s = NULL;
418 if (section != NO_SEG)
419 s = get_section_by_index(section);
420 fi = s ? s->fileindex : NO_SECT;
422 /* absolute relocation */
423 switch (reltype) {
424 case RL_ABS:
425 if (section == NO_SEG) {
426 /* absolute (can this even happen?) */
427 r->ext = 0;
428 r->snum = R_ABS;
429 } else if (fi == NO_SECT) {
430 /* external */
431 r->snum = raa_read(extsyms, section);
432 } else {
433 /* local */
434 r->ext = 0;
435 r->snum = fi;
436 adjust = -sect->size;
438 break;
440 case RL_REL:
441 case RL_BRANCH:
442 r->type = fmt.reloc_rel;
443 r->pcrel = 1;
444 if (section == NO_SEG) {
445 /* absolute - seems to produce garbage no matter what */
446 nasm_error(ERR_NONFATAL, "Mach-O does not support relative "
447 "references to absolute addresses");
448 goto bail;
449 #if 0
450 /* This "seems" to be how it ought to work... */
452 struct symbol *sym = macho_find_gsym(&absolute_sect,
453 offset, false);
454 if (!sym)
455 goto bail;
457 sect->extreloc = 1;
458 r->snum = NO_SECT;
459 adjust = -sect->size;
460 #endif
461 } else if (fi == NO_SECT) {
462 /* external */
463 sect->extreloc = 1;
464 r->snum = raa_read(extsyms, section);
465 if (reltype == RL_BRANCH)
466 r->type = X86_64_RELOC_BRANCH;
467 else if (r->type == GENERIC_RELOC_VANILLA)
468 adjust = -sect->size;
469 } else {
470 /* local */
471 r->ext = 0;
472 r->snum = fi;
473 adjust = -sect->size;
475 break;
477 case RL_SUB:
478 r->pcrel = 0;
479 r->type = X86_64_RELOC_SUBTRACTOR;
480 break;
482 case RL_GOT:
483 r->type = X86_64_RELOC_GOT;
484 goto needsym;
486 case RL_GOTLOAD:
487 r->type = X86_64_RELOC_GOT_LOAD;
488 goto needsym;
490 case RL_TLV:
491 r->type = fmt.reloc_tlv;
492 goto needsym;
494 needsym:
495 r->pcrel = 1;
496 if (section == NO_SEG) {
497 nasm_error(ERR_NONFATAL, "Unsupported use of use of WRT");
498 } else if (fi == NO_SECT) {
499 /* external */
500 r->snum = raa_read(extsyms, section);
501 } else {
502 /* internal */
503 struct symbol *sym = macho_find_gsym(s, offset, reltype != RL_TLV);
504 if (!sym)
505 goto bail;
506 r->snum = sym->initial_snum;
508 break;
511 /* NeXT as puts relocs in reversed order (address-wise) into the
512 ** files, so we do the same, doesn't seem to make much of a
513 ** difference either way */
514 r->next = sect->relocs;
515 sect->relocs = r;
516 if (r->ext)
517 sect->extreloc = 1;
518 ++sect->nreloc;
520 return adjust;
522 bail:
523 nasm_free(r);
524 return 0;
527 static void macho_output(int32_t secto, const void *data,
528 enum out_type type, uint64_t size,
529 int32_t section, int32_t wrt)
531 struct section *s;
532 int64_t addr, offset;
533 uint8_t mydata[16], *p;
534 bool is_bss;
535 enum reltype reltype;
537 if (secto == NO_SEG) {
538 if (type != OUT_RESERVE)
539 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
540 "[ABSOLUTE] space");
541 return;
544 s = get_section_by_index(secto);
546 if (s == NULL) {
547 nasm_error(ERR_WARNING, "attempt to assemble code in"
548 " section %d: defaulting to `.text'", secto);
549 s = get_section_by_name("__TEXT", "__text");
551 /* should never happen */
552 if (s == NULL)
553 nasm_panic(0, "text section not found");
556 is_bss = (s->flags & SECTION_TYPE) == S_ZEROFILL;
558 if (is_bss && type != OUT_RESERVE) {
559 nasm_error(ERR_WARNING, "attempt to initialize memory in "
560 "BSS section: ignored");
561 s->size += realsize(type, size);
562 return;
565 memset(mydata, 0, sizeof(mydata));
567 switch (type) {
568 case OUT_RESERVE:
569 if (!is_bss) {
570 nasm_error(ERR_WARNING, "uninitialized space declared in"
571 " %s,%s section: zeroing", s->segname, s->sectname);
573 sect_write(s, NULL, size);
574 } else
575 s->size += size;
577 break;
579 case OUT_RAWDATA:
580 if (section != NO_SEG)
581 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
583 sect_write(s, data, size);
584 break;
586 case OUT_ADDRESS:
588 int asize = abs((int)size);
590 addr = *(int64_t *)data;
591 if (section != NO_SEG) {
592 if (section % 2) {
593 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
594 " section base references");
595 } else if (wrt == NO_SEG) {
596 if (fmt.ptrsize == 8 && asize != 8) {
597 nasm_error(ERR_NONFATAL,
598 "Mach-O 64-bit format does not support"
599 " 32-bit absolute addresses");
600 } else {
601 add_reloc(s, section, addr, RL_ABS, asize);
603 } else {
604 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
605 " this use of WRT");
609 p = mydata;
610 WRITEADDR(p, addr, asize);
611 sect_write(s, mydata, asize);
612 break;
615 case OUT_REL2ADR:
616 nasm_assert(section != secto);
618 p = mydata;
619 offset = *(int64_t *)data;
620 addr = offset - size;
622 if (section != NO_SEG && section % 2) {
623 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
624 " section base references");
625 } else if (fmt.ptrsize == 8) {
626 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
627 " Macho-O relocation [2]");
628 } else if (wrt != NO_SEG) {
629 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
630 " this use of WRT");
631 wrt = NO_SEG; /* we can at least _try_ to continue */
632 } else {
633 addr += add_reloc(s, section, addr+size, RL_REL, 2);
636 WRITESHORT(p, addr);
637 sect_write(s, mydata, 2);
638 break;
640 case OUT_REL4ADR:
641 nasm_assert(section != secto);
643 p = mydata;
644 offset = *(int64_t *)data;
645 addr = offset - size;
646 reltype = RL_REL;
648 if (section != NO_SEG && section % 2) {
649 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
650 " section base references");
651 } else if (wrt == NO_SEG) {
652 if (fmt.ptrsize == 8 &&
653 (s->flags & S_ATTR_SOME_INSTRUCTIONS)) {
654 uint8_t opcode[2];
656 opcode[0] = opcode[1] = 0;
658 /* HACK: Retrieve instruction opcode */
659 if (likely(s->data->datalen >= 2)) {
660 saa_fread(s->data, s->data->datalen-2, opcode, 2);
661 } else if (s->data->datalen == 1) {
662 saa_fread(s->data, 0, opcode+1, 1);
665 if ((opcode[0] != 0x0f && (opcode[1] & 0xfe) == 0xe8) ||
666 (opcode[0] == 0x0f && (opcode[1] & 0xf0) == 0x80)) {
667 /* Direct call, jmp, or jcc */
668 reltype = RL_BRANCH;
671 } else if (wrt == macho_gotpcrel_sect) {
672 reltype = RL_GOT;
674 if ((s->flags & S_ATTR_SOME_INSTRUCTIONS) &&
675 s->data->datalen >= 3) {
676 uint8_t gotload[3];
678 /* HACK: Retrieve instruction opcode */
679 saa_fread(s->data, s->data->datalen-3, gotload, 3);
680 if ((gotload[0] & 0xf8) == 0x48 &&
681 gotload[1] == 0x8b &&
682 (gotload[2] & 0307) == 0005) {
683 /* movq <reg>,[rel sym wrt ..gotpcrel] */
684 reltype = RL_GOTLOAD;
687 } else if (wrt == macho_tlvp_sect) {
688 reltype = RL_TLV;
689 } else {
690 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
691 " this use of WRT");
692 /* continue with RL_REL */
695 addr += add_reloc(s, section, offset, reltype, 4);
696 WRITELONG(p, addr);
697 sect_write(s, mydata, 4);
698 break;
700 default:
701 nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
702 break;
706 static int32_t macho_section(char *name, int pass, int *bits)
708 char *sectionAttributes;
709 const struct sectmap *sm;
710 struct section *s;
711 const char *section, *segment;
712 uint32_t flags;
713 char *currentAttribute;
714 char *comma;
715 bool new_seg;
717 (void)pass;
719 /* Default to the appropriate number of bits. */
720 if (!name) {
721 *bits = fmt.ptrsize << 3;
722 name = ".text";
723 sectionAttributes = NULL;
724 } else {
725 sectionAttributes = name;
726 name = nasm_strsep(&sectionAttributes, " \t");
729 section = segment = NULL;
730 flags = 0;
732 comma = strchr(name, ',');
733 if (comma) {
734 int len;
736 *comma = '\0';
737 segment = name;
738 section = comma+1;
740 len = strlen(segment);
741 if (len == 0) {
742 nasm_error(ERR_NONFATAL, "empty segment name\n");
743 } else if (len >= 16) {
744 nasm_error(ERR_NONFATAL, "segment name %s too long\n", segment);
747 len = strlen(section);
748 if (len == 0) {
749 nasm_error(ERR_NONFATAL, "empty section name\n");
750 } else if (len >= 16) {
751 nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
754 if (!strcmp(section, "__text")) {
755 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
756 S_ATTR_PURE_INSTRUCTIONS;
757 } else if (!strcmp(section, "__bss")) {
758 flags = S_ZEROFILL;
759 } else {
760 flags = S_REGULAR;
762 } else {
763 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
764 /* make lookup into section name translation table */
765 if (!strcmp(name, sm->nasmsect)) {
766 segment = sm->segname;
767 section = sm->sectname;
768 flags = sm->flags;
769 goto found;
772 nasm_error(ERR_NONFATAL, "unknown section name\n");
773 return NO_SEG;
776 found:
777 /* try to find section with that name */
778 s = get_section_by_name(segment, section);
780 /* create it if it doesn't exist yet */
781 if (!s) {
782 new_seg = true;
784 s = *sectstail = nasm_zalloc(sizeof(struct section));
785 sectstail = &s->next;
787 s->data = saa_init(1L);
788 s->index = seg_alloc();
789 s->fileindex = ++seg_nsects;
790 s->align = -1;
791 s->pad = -1;
792 s->offset = -1;
793 s->by_name = false;
795 xstrncpy(s->segname, segment);
796 xstrncpy(s->sectname, section);
797 s->size = 0;
798 s->nreloc = 0;
799 s->flags = flags;
800 } else {
801 new_seg = false;
804 if (comma)
805 *comma = ','; /* Restore comma */
807 s->by_name = s->by_name || comma; /* Was specified by name */
809 flags = (uint32_t)-1;
811 while ((NULL != sectionAttributes)
812 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
813 if (0 != *currentAttribute) {
814 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
815 char *end;
816 int newAlignment, value;
818 value = strtoul(currentAttribute + 6, (char**)&end, 0);
819 newAlignment = alignlog2_32(value);
821 if (0 != *end) {
822 nasm_error(ERR_NONFATAL,
823 "unknown or missing alignment value \"%s\" "
824 "specified for section \"%s\"",
825 currentAttribute + 6,
826 name);
827 } else if (0 > newAlignment) {
828 nasm_error(ERR_NONFATAL,
829 "alignment of %d (for section \"%s\") is not "
830 "a power of two",
831 value,
832 name);
835 if (s->align < newAlignment)
836 s->align = newAlignment;
837 } else if (!nasm_stricmp("data", currentAttribute)) {
838 flags = S_REGULAR;
839 } else if (!nasm_stricmp("code", currentAttribute) ||
840 !nasm_stricmp("text", currentAttribute)) {
841 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
842 S_ATTR_PURE_INSTRUCTIONS;
843 } else if (!nasm_stricmp("mixed", currentAttribute)) {
844 flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS;
845 } else if (!nasm_stricmp("bss", currentAttribute)) {
846 flags = S_ZEROFILL;
847 } else {
848 nasm_error(ERR_NONFATAL,
849 "unknown section attribute %s for section %s",
850 currentAttribute,
851 name);
855 if (flags != (uint32_t)-1) {
856 if (!new_seg && s->flags != flags) {
857 nasm_error(ERR_NONFATAL,
858 "inconsistent section attributes for section %s\n",
859 name);
860 } else {
861 s->flags = flags;
866 return s->index;
869 static void macho_symdef(char *name, int32_t section, int64_t offset,
870 int is_global, char *special)
872 struct symbol *sym;
874 if (special) {
875 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
876 "not support any special symbol types");
877 return;
880 if (is_global == 3) {
881 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
882 "(yet) support forward reference fixups.");
883 return;
886 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
888 * This is a NASM special symbol. We never allow it into
889 * the Macho-O symbol table, even if it's a valid one. If it
890 * _isn't_ a valid one, we should barf immediately.
892 if (strcmp(name, "..gotpcrel") && strcmp(name, "..tlvp"))
893 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
894 return;
897 sym = *symstail = nasm_zalloc(sizeof(struct symbol));
898 sym->next = NULL;
899 symstail = &sym->next;
901 sym->name = name;
902 sym->strx = strslen;
903 sym->type = 0;
904 sym->desc = 0;
905 sym->symv.key = offset;
906 sym->initial_snum = -1;
908 /* external and common symbols get N_EXT */
909 if (is_global != 0) {
910 sym->type |= N_EXT;
913 if (section == NO_SEG) {
914 /* symbols in no section get absolute */
915 sym->type |= N_ABS;
916 sym->sect = NO_SECT;
918 /* all absolute symbols are available to use as references */
919 absolute_sect.gsyms = rb_insert(absolute_sect.gsyms, &sym->symv);
920 } else {
921 struct section *s = get_section_by_index(section);
923 sym->type |= N_SECT;
925 /* get the in-file index of the section the symbol was defined in */
926 sym->sect = s ? s->fileindex : NO_SECT;
928 /* track the initially allocated symbol number for use in future fix-ups */
929 sym->initial_snum = nsyms;
931 if (!s) {
932 /* remember symbol number of references to external
933 ** symbols, this works because every external symbol gets
934 ** its own section number allocated internally by nasm and
935 ** can so be used as a key */
936 extsyms = raa_write(extsyms, section, nsyms);
938 switch (is_global) {
939 case 1:
940 case 2:
941 /* there isn't actually a difference between global
942 ** and common symbols, both even have their size in
943 ** sym->symv.key */
944 sym->type = N_EXT;
945 break;
947 default:
948 /* give an error on unfound section if it's not an
949 ** external or common symbol (assemble_file() does a
950 ** seg_alloc() on every call for them) */
951 nasm_panic(0, "in-file index for section %d not found, is_global = %d", section, is_global);
952 break;
954 } else if (is_global) {
955 s->gsyms = rb_insert(s->gsyms, &sym->symv);
958 ++nsyms;
961 static void macho_sectalign(int32_t seg, unsigned int value)
963 struct section *s;
964 int align;
966 nasm_assert(!(seg & 1));
968 s = get_section_by_index(seg);
970 if (!s || !is_power2(value))
971 return;
973 align = alignlog2_32(value);
974 if (s->align < align)
975 s->align = align;
978 static int32_t macho_segbase(int32_t section)
980 return section;
983 static void macho_filename(char *inname, char *outname)
985 standard_extension(inname, outname, ".o");
988 extern macros_t macho_stdmac[];
990 /* Comparison function for qsort symbol layout. */
991 static int layout_compare (const struct symbol **s1,
992 const struct symbol **s2)
994 return (strcmp ((*s1)->name, (*s2)->name));
997 /* The native assembler does a few things in a similar function
999 * Remove temporary labels
1000 * Sort symbols according to local, external, undefined (by name)
1001 * Order the string table
1003 We do not remove temporary labels right now.
1005 numsyms is the total number of symbols we have. strtabsize is the
1006 number entries in the string table. */
1008 static void macho_layout_symbols (uint32_t *numsyms,
1009 uint32_t *strtabsize)
1011 struct symbol *sym, **symp;
1012 uint32_t i,j;
1014 *numsyms = 0;
1015 *strtabsize = sizeof (char);
1017 symp = &syms;
1019 while ((sym = *symp)) {
1020 /* Undefined symbols are now external. */
1021 if (sym->type == N_UNDF)
1022 sym->type |= N_EXT;
1024 if ((sym->type & N_EXT) == 0) {
1025 sym->snum = *numsyms;
1026 *numsyms = *numsyms + 1;
1027 nlocalsym++;
1029 else {
1030 if ((sym->type & N_TYPE) != N_UNDF) {
1031 nextdefsym++;
1032 } else {
1033 nundefsym++;
1036 /* If we handle debug info we'll want
1037 to check for it here instead of just
1038 adding the symbol to the string table. */
1039 sym->strx = *strtabsize;
1040 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
1041 *strtabsize += strlen(sym->name) + 1;
1043 symp = &(sym->next);
1046 /* Next, sort the symbols. Most of this code is a direct translation from
1047 the Apple cctools symbol layout. We need to keep compatibility with that. */
1048 /* Set the indexes for symbol groups into the symbol table */
1049 ilocalsym = 0;
1050 iextdefsym = nlocalsym;
1051 iundefsym = nlocalsym + nextdefsym;
1053 /* allocate arrays for sorting externals by name */
1054 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
1055 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
1057 i = 0;
1058 j = 0;
1060 symp = &syms;
1062 while ((sym = *symp)) {
1064 if((sym->type & N_EXT) == 0) {
1065 sym->strx = *strtabsize;
1066 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
1067 *strtabsize += strlen(sym->name) + 1;
1069 else {
1070 if((sym->type & N_TYPE) != N_UNDF) {
1071 extdefsyms[i++] = sym;
1072 } else {
1073 undefsyms[j++] = sym;
1076 symp = &(sym->next);
1079 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
1080 (int (*)(const void *, const void *))layout_compare);
1081 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
1082 (int (*)(const void *, const void *))layout_compare);
1084 for(i = 0; i < nextdefsym; i++) {
1085 extdefsyms[i]->snum = *numsyms;
1086 *numsyms += 1;
1088 for(j = 0; j < nundefsym; j++) {
1089 undefsyms[j]->snum = *numsyms;
1090 *numsyms += 1;
1094 /* Calculate some values we'll need for writing later. */
1096 static void macho_calculate_sizes (void)
1098 struct section *s;
1099 int fi;
1101 /* count sections and calculate in-memory and in-file offsets */
1102 for (s = sects; s != NULL; s = s->next) {
1103 uint64_t newaddr;
1105 /* recalculate segment address based on alignment and vm size */
1106 s->addr = seg_vmsize;
1108 /* we need section alignment to calculate final section address */
1109 if (s->align == -1)
1110 s->align = DEFAULT_SECTION_ALIGNMENT;
1112 newaddr = ALIGN(s->addr, 1L << s->align);
1113 s->addr = newaddr;
1115 seg_vmsize = newaddr + s->size;
1117 /* zerofill sections aren't actually written to the file */
1118 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1120 * LLVM/Xcode as always aligns the section data to 4
1121 * bytes; there is a comment in the LLVM source code that
1122 * perhaps aligning to pointer size would be better.
1124 s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
1125 s->offset = seg_filesize + s->pad;
1126 seg_filesize += s->size + s->pad;
1130 /* calculate size of all headers, load commands and sections to
1131 ** get a pointer to the start of all the raw data */
1132 if (seg_nsects > 0) {
1133 ++head_ncmds;
1134 head_sizeofcmds += fmt.segcmd_size + seg_nsects * fmt.sectcmd_size;
1137 if (nsyms > 0) {
1138 ++head_ncmds;
1139 head_sizeofcmds += MACHO_SYMCMD_SIZE;
1142 if (seg_nsects > MAX_SECT) {
1143 nasm_fatal(0, "MachO output is limited to %d sections\n",
1144 MAX_SECT);
1147 /* Create a table of sections by file index to avoid linear search */
1148 sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
1149 sectstab[NO_SECT] = &absolute_sect;
1150 for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
1151 sectstab[fi] = s;
1154 /* Write out the header information for the file. */
1156 static void macho_write_header (void)
1158 fwriteint32_t(fmt.mh_magic, ofile); /* magic */
1159 fwriteint32_t(fmt.cpu_type, ofile); /* CPU type */
1160 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
1161 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
1162 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
1163 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
1164 fwriteint32_t(0, ofile); /* no flags */
1165 fwritezero(fmt.header_size - 7*4, ofile); /* reserved fields */
1168 /* Write out the segment load command at offset. */
1170 static uint32_t macho_write_segment (uint64_t offset)
1172 uint64_t rel_base = alignptr(offset + seg_filesize);
1173 uint32_t s_reloff = 0;
1174 struct section *s;
1176 fwriteint32_t(fmt.lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
1178 /* size of load command including section load commands */
1179 fwriteint32_t(fmt.segcmd_size + seg_nsects * fmt.sectcmd_size,
1180 ofile);
1182 /* in an MH_OBJECT file all sections are in one unnamed (name
1183 ** all zeros) segment */
1184 fwritezero(16, ofile);
1185 fwriteptr(0, ofile); /* in-memory offset */
1186 fwriteptr(seg_vmsize, ofile); /* in-memory size */
1187 fwriteptr(offset, ofile); /* in-file offset to data */
1188 fwriteptr(seg_filesize, ofile); /* in-file size */
1189 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
1190 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
1191 fwriteint32_t(seg_nsects, ofile); /* number of sections */
1192 fwriteint32_t(0, ofile); /* no flags */
1194 /* emit section headers */
1195 for (s = sects; s != NULL; s = s->next) {
1196 if (s->nreloc) {
1197 nasm_assert((s->flags & SECTION_TYPE) != S_ZEROFILL);
1198 s->flags |= S_ATTR_LOC_RELOC;
1199 if (s->extreloc)
1200 s->flags |= S_ATTR_EXT_RELOC;
1201 } else if (!strcmp(s->segname, "__DATA") &&
1202 !strcmp(s->sectname, "__const") &&
1203 !s->by_name &&
1204 !get_section_by_name("__TEXT", "__const")) {
1206 * The MachO equivalent to .rodata can be either
1207 * __DATA,__const or __TEXT,__const; the latter only if
1208 * there are no relocations. However, when mixed it is
1209 * better to specify the segments explicitly.
1211 xstrncpy(s->segname, "__TEXT");
1214 nasm_write(s->sectname, sizeof(s->sectname), ofile);
1215 nasm_write(s->segname, sizeof(s->segname), ofile);
1216 fwriteptr(s->addr, ofile);
1217 fwriteptr(s->size, ofile);
1219 /* dummy data for zerofill sections or proper values */
1220 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1221 nasm_assert(s->pad != (uint32_t)-1);
1222 offset += s->pad;
1223 fwriteint32_t(offset, ofile);
1224 offset += s->size;
1225 /* Write out section alignment, as a power of two.
1226 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1227 fwriteint32_t(s->align, ofile);
1228 /* To be compatible with cctools as we emit
1229 a zero reloff if we have no relocations. */
1230 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
1231 fwriteint32_t(s->nreloc, ofile);
1233 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
1234 } else {
1235 fwriteint32_t(0, ofile);
1236 fwriteint32_t(s->align, ofile);
1237 fwriteint32_t(0, ofile);
1238 fwriteint32_t(0, ofile);
1241 fwriteint32_t(s->flags, ofile); /* flags */
1242 fwriteint32_t(0, ofile); /* reserved */
1243 fwriteptr(0, ofile); /* reserved */
1246 rel_padcnt = rel_base - offset;
1247 offset = rel_base + s_reloff;
1249 return offset;
1252 /* For a given chain of relocs r, write out the entire relocation
1253 chain to the object file. */
1255 static void macho_write_relocs (struct reloc *r)
1257 while (r) {
1258 uint32_t word2;
1260 fwriteint32_t(r->addr, ofile); /* reloc offset */
1262 word2 = r->snum;
1263 word2 |= r->pcrel << 24;
1264 word2 |= r->length << 25;
1265 word2 |= r->ext << 27;
1266 word2 |= r->type << 28;
1267 fwriteint32_t(word2, ofile); /* reloc data */
1268 r = r->next;
1272 /* Write out the section data. */
1273 static void macho_write_section (void)
1275 struct section *s;
1276 struct reloc *r;
1277 uint8_t *p;
1278 int32_t len;
1279 int64_t l;
1280 union offset {
1281 uint64_t val;
1282 uint8_t buf[8];
1283 } blk;
1285 for (s = sects; s != NULL; s = s->next) {
1286 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1287 continue;
1289 /* Like a.out Mach-O references things in the data or bss
1290 * sections by addresses which are actually relative to the
1291 * start of the _text_ section, in the _file_. See outaout.c
1292 * for more information. */
1293 saa_rewind(s->data);
1294 for (r = s->relocs; r != NULL; r = r->next) {
1295 len = (uint32_t)1 << r->length;
1296 if (len > 4) /* Can this ever be an issue?! */
1297 len = 8;
1298 blk.val = 0;
1299 saa_fread(s->data, r->addr, blk.buf, len);
1301 /* get offset based on relocation type */
1302 #ifdef WORDS_LITTLEENDIAN
1303 l = blk.val;
1304 #else
1305 l = blk.buf[0];
1306 l += ((int64_t)blk.buf[1]) << 8;
1307 l += ((int64_t)blk.buf[2]) << 16;
1308 l += ((int64_t)blk.buf[3]) << 24;
1309 l += ((int64_t)blk.buf[4]) << 32;
1310 l += ((int64_t)blk.buf[5]) << 40;
1311 l += ((int64_t)blk.buf[6]) << 48;
1312 l += ((int64_t)blk.buf[7]) << 56;
1313 #endif
1315 /* If the relocation is internal add to the current section
1316 offset. Otherwise the only value we need is the symbol
1317 offset which we already have. The linker takes care
1318 of the rest of the address. */
1319 if (!r->ext) {
1320 /* generate final address by section address and offset */
1321 nasm_assert(r->snum <= seg_nsects);
1322 l += sectstab[r->snum]->addr;
1323 if (r->pcrel)
1324 l -= s->addr;
1325 } else if (r->pcrel && r->type == GENERIC_RELOC_VANILLA) {
1326 l -= s->addr;
1329 /* write new offset back */
1330 p = blk.buf;
1331 WRITEDLONG(p, l);
1332 saa_fwrite(s->data, r->addr, blk.buf, len);
1335 /* dump the section data to file */
1336 fwritezero(s->pad, ofile);
1337 saa_fpwrite(s->data, ofile);
1340 /* pad last section up to reloc entries on pointer boundary */
1341 fwritezero(rel_padcnt, ofile);
1343 /* emit relocation entries */
1344 for (s = sects; s != NULL; s = s->next)
1345 macho_write_relocs (s->relocs);
1348 /* Write out the symbol table. We should already have sorted this
1349 before now. */
1350 static void macho_write_symtab (void)
1352 struct symbol *sym;
1353 uint64_t i;
1355 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1357 for (sym = syms; sym != NULL; sym = sym->next) {
1358 if ((sym->type & N_EXT) == 0) {
1359 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1360 nasm_write(&sym->type, 1, ofile); /* symbol type */
1361 nasm_write(&sym->sect, 1, ofile); /* section */
1362 fwriteint16_t(sym->desc, ofile); /* description */
1364 /* Fix up the symbol value now that we know the final section
1365 sizes. */
1366 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1367 nasm_assert(sym->sect <= seg_nsects);
1368 sym->symv.key += sectstab[sym->sect]->addr;
1371 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1375 for (i = 0; i < nextdefsym; i++) {
1376 sym = extdefsyms[i];
1377 fwriteint32_t(sym->strx, ofile);
1378 nasm_write(&sym->type, 1, ofile); /* symbol type */
1379 nasm_write(&sym->sect, 1, ofile); /* section */
1380 fwriteint16_t(sym->desc, ofile); /* description */
1382 /* Fix up the symbol value now that we know the final section
1383 sizes. */
1384 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1385 nasm_assert(sym->sect <= seg_nsects);
1386 sym->symv.key += sectstab[sym->sect]->addr;
1389 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1392 for (i = 0; i < nundefsym; i++) {
1393 sym = undefsyms[i];
1394 fwriteint32_t(sym->strx, ofile);
1395 nasm_write(&sym->type, 1, ofile); /* symbol type */
1396 nasm_write(&sym->sect, 1, ofile); /* section */
1397 fwriteint16_t(sym->desc, ofile); /* description */
1399 /* Fix up the symbol value now that we know the final section
1400 sizes. */
1401 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1402 nasm_assert(sym->sect <= seg_nsects);
1403 sym->symv.key += sectstab[sym->sect]->addr;
1406 fwriteptr(sym->symv.key, ofile); /* value (i.e. offset) */
1411 /* Fixup the snum in the relocation entries, we should be
1412 doing this only for externally referenced symbols. */
1413 static void macho_fixup_relocs (struct reloc *r)
1415 struct symbol *sym;
1417 while (r != NULL) {
1418 if (r->ext) {
1419 for (sym = syms; sym != NULL; sym = sym->next) {
1420 if (sym->initial_snum == r->snum) {
1421 r->snum = sym->snum;
1422 break;
1426 r = r->next;
1430 /* Write out the object file. */
1432 static void macho_write (void)
1434 uint64_t offset = 0;
1436 /* mach-o object file structure:
1438 ** mach header
1439 ** uint32_t magic
1440 ** int cpu type
1441 ** int cpu subtype
1442 ** uint32_t mach file type
1443 ** uint32_t number of load commands
1444 ** uint32_t size of all load commands
1445 ** (includes section struct size of segment command)
1446 ** uint32_t flags
1448 ** segment command
1449 ** uint32_t command type == LC_SEGMENT[_64]
1450 ** uint32_t size of load command
1451 ** (including section load commands)
1452 ** char[16] segment name
1453 ** pointer in-memory offset
1454 ** pointer in-memory size
1455 ** pointer in-file offset to data area
1456 ** pointer in-file size
1457 ** (in-memory size excluding zerofill sections)
1458 ** int maximum vm protection
1459 ** int initial vm protection
1460 ** uint32_t number of sections
1461 ** uint32_t flags
1463 ** section commands
1464 ** char[16] section name
1465 ** char[16] segment name
1466 ** pointer in-memory offset
1467 ** pointer in-memory size
1468 ** uint32_t in-file offset
1469 ** uint32_t alignment
1470 ** (irrelevant in MH_OBJECT)
1471 ** uint32_t in-file offset of relocation entires
1472 ** uint32_t number of relocations
1473 ** uint32_t flags
1474 ** uint32_t reserved
1475 ** uint32_t reserved
1477 ** symbol table command
1478 ** uint32_t command type == LC_SYMTAB
1479 ** uint32_t size of load command
1480 ** uint32_t symbol table offset
1481 ** uint32_t number of symbol table entries
1482 ** uint32_t string table offset
1483 ** uint32_t string table size
1485 ** raw section data
1487 ** padding to pointer boundary
1489 ** relocation data (struct reloc)
1490 ** int32_t offset
1491 ** uint data (symbolnum, pcrel, length, extern, type)
1493 ** symbol table data (struct nlist)
1494 ** int32_t string table entry number
1495 ** uint8_t type
1496 ** (extern, absolute, defined in section)
1497 ** uint8_t section
1498 ** (0 for global symbols, section number of definition (>= 1, <=
1499 ** 254) for local symbols, size of variable for common symbols
1500 ** [type == extern])
1501 ** int16_t description
1502 ** (for stab debugging format)
1503 ** pointer value (i.e. file offset) of symbol or stab offset
1505 ** string table data
1506 ** list of null-terminated strings
1509 /* Emit the Mach-O header. */
1510 macho_write_header();
1512 offset = fmt.header_size + head_sizeofcmds;
1514 /* emit the segment load command */
1515 if (seg_nsects > 0)
1516 offset = macho_write_segment (offset);
1517 else
1518 nasm_error(ERR_WARNING, "no sections?");
1520 if (nsyms > 0) {
1521 /* write out symbol command */
1522 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1523 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1524 fwriteint32_t(offset, ofile); /* symbol table offset */
1525 fwriteint32_t(nsyms, ofile); /* number of symbol
1526 ** table entries */
1527 offset += nsyms * fmt.nlist_size;
1528 fwriteint32_t(offset, ofile); /* string table offset */
1529 fwriteint32_t(strslen, ofile); /* string table size */
1532 /* emit section data */
1533 if (seg_nsects > 0)
1534 macho_write_section ();
1536 /* emit symbol table if we have symbols */
1537 if (nsyms > 0)
1538 macho_write_symtab ();
1540 /* we don't need to pad here, we are already aligned */
1542 /* emit string table */
1543 saa_fpwrite(strs, ofile);
1545 /* We do quite a bit here, starting with finalizing all of the data
1546 for the object file, writing, and then freeing all of the data from
1547 the file. */
1549 static void macho_cleanup(void)
1551 struct section *s;
1552 struct reloc *r;
1553 struct symbol *sym;
1555 /* Sort all symbols. */
1556 macho_layout_symbols (&nsyms, &strslen);
1558 /* Fixup relocation entries */
1559 for (s = sects; s != NULL; s = s->next) {
1560 macho_fixup_relocs (s->relocs);
1563 /* First calculate and finalize needed values. */
1564 macho_calculate_sizes();
1565 macho_write();
1567 /* free up everything */
1568 while (sects->next) {
1569 s = sects;
1570 sects = sects->next;
1572 saa_free(s->data);
1573 while (s->relocs != NULL) {
1574 r = s->relocs;
1575 s->relocs = s->relocs->next;
1576 nasm_free(r);
1579 nasm_free(s);
1582 saa_free(strs);
1583 raa_free(extsyms);
1585 while (syms) {
1586 sym = syms;
1587 syms = syms->next;
1588 nasm_free (sym);
1591 nasm_free(extdefsyms);
1592 nasm_free(undefsyms);
1593 nasm_free(sectstab);
1596 #ifdef OF_MACHO32
1597 static const struct macho_fmt macho32_fmt = {
1599 MH_MAGIC,
1600 CPU_TYPE_I386,
1601 LC_SEGMENT,
1602 MACHO_HEADER_SIZE,
1603 MACHO_SEGCMD_SIZE,
1604 MACHO_SECTCMD_SIZE,
1605 MACHO_NLIST_SIZE,
1606 RL_MAX_32,
1607 GENERIC_RELOC_VANILLA,
1608 GENERIC_RELOC_VANILLA,
1609 GENERIC_RELOC_TLV
1612 static void macho32_init(void)
1614 fmt = macho32_fmt;
1615 macho_init();
1617 macho_gotpcrel_sect = NO_SEG;
1620 const struct ofmt of_macho32 = {
1621 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1622 "macho32",
1625 null_debug_arr,
1626 &null_debug_form,
1627 macho_stdmac,
1628 macho32_init,
1629 null_setinfo,
1630 nasm_do_legacy_output,
1631 macho_output,
1632 macho_symdef,
1633 macho_section,
1634 macho_sectalign,
1635 macho_segbase,
1636 null_directive,
1637 macho_filename,
1638 macho_cleanup
1640 #endif
1642 #ifdef OF_MACHO64
1643 static const struct macho_fmt macho64_fmt = {
1645 MH_MAGIC_64,
1646 CPU_TYPE_X86_64,
1647 LC_SEGMENT_64,
1648 MACHO_HEADER64_SIZE,
1649 MACHO_SEGCMD64_SIZE,
1650 MACHO_SECTCMD64_SIZE,
1651 MACHO_NLIST64_SIZE,
1652 RL_MAX_64,
1653 X86_64_RELOC_UNSIGNED,
1654 X86_64_RELOC_SIGNED,
1655 X86_64_RELOC_TLV
1658 static void macho64_init(void)
1660 fmt = macho64_fmt;
1661 macho_init();
1663 /* add special symbol for ..gotpcrel */
1664 macho_gotpcrel_sect = seg_alloc() + 1;
1665 define_label("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false);
1668 const struct ofmt of_macho64 = {
1669 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
1670 "macho64",
1673 null_debug_arr,
1674 &null_debug_form,
1675 macho_stdmac,
1676 macho64_init,
1677 null_setinfo,
1678 nasm_do_legacy_output,
1679 macho_output,
1680 macho_symdef,
1681 macho_section,
1682 macho_sectalign,
1683 macho_segbase,
1684 null_directive,
1685 macho_filename,
1686 macho_cleanup
1688 #endif
1690 #endif
1693 * Local Variables:
1694 * mode:c
1695 * c-basic-offset:4
1696 * End:
1698 * end of file */