1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
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 * outmacho64.c output routines for the Netwide Assembler to produce
36 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files
39 /* Most of this file is, like Mach-O itself, based on a.out. For more
40 * guidelines see outaout.c. */
54 #include "output/outform.h"
55 #include "output/outlib.h"
57 #if defined(OF_MACHO64)
59 /* Mach-O in-file header structure sizes */
60 #define MACHO_HEADER64_SIZE (32)
61 #define MACHO_SEGCMD64_SIZE (72)
62 #define MACHO_SECTCMD64_SIZE (80)
63 #define MACHO_SYMCMD_SIZE (24)
64 #define MACHO_NLIST64_SIZE (16)
65 #define MACHO_RELINFO64_SIZE (8)
67 /* Mach-O file header values */
68 #define MH_MAGIC_64 (0xfeedfacf)
69 #define CPU_TYPE_X86_64 (0x01000007) /* x86-64 platform */
70 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
71 #define MH_OBJECT (0x1) /* object file */
73 #define LC_SEGMENT_64 (0x19) /* segment load command */
74 #define LC_SYMTAB (0x2) /* symbol table load command */
76 #define VM_PROT_NONE (0x00)
77 #define VM_PROT_READ (0x01)
78 #define VM_PROT_WRITE (0x02)
79 #define VM_PROT_EXECUTE (0x04)
81 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
82 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
85 /* nasm internal data */
92 /* data that goes into the file */
93 char sectname[16]; /* what this section is called */
94 char segname[16]; /* segment this section will be in */
95 uint64_t addr; /* in-memory address (subject to alignment) */
96 uint64_t size; /* in-memory and -file size */
97 uint32_t nreloc; /* relocation entry count */
98 uint32_t flags; /* type and attributes (masked) */
99 uint32_t extreloc; /* external relocations */
102 #define SECTION_TYPE 0x000000ff /* section type mask */
104 #define S_REGULAR (0x0) /* standard section */
105 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
107 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
108 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
109 machine instructions */
110 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
111 relocation entries */
112 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
113 relocation entries */
114 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
115 machine instructions */
117 static struct sectmap {
118 const char *nasmsect;
120 const char *sectname;
123 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
124 {".data", "__DATA", "__data", S_REGULAR},
125 {".rodata", "__DATA", "__const", S_REGULAR},
126 {".bss", "__DATA", "__bss", S_ZEROFILL},
127 {NULL, NULL, NULL, 0}
131 /* nasm internal data */
134 /* data that goes into the file */
135 int32_t addr; /* op's offset in section */
136 uint32_t snum:24, /* contains symbol index if
137 ** ext otherwise in-file
139 pcrel:1, /* relative relocation */
140 length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
141 ext:1, /* external symbol referenced */
142 type:4; /* reloc type */
145 #define R_ABS 0 /* absolute relocation */
146 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
147 ** highest bit == 1 */
150 /* nasm internal data */
151 struct symbol *next; /* next symbol in the list */
152 char *name; /* name of this symbol */
153 int32_t initial_snum; /* symbol number used above in
155 int32_t snum; /* true snum for reloc */
157 /* data that goes into the file */
158 uint32_t strx; /* string table index */
159 uint8_t type; /* symbol type */
160 uint8_t sect; /* NO_SECT or section number */
161 uint16_t desc; /* for stab debugging, 0 for us */
162 uint64_t value; /* offset of symbol in section */
165 /* symbol type bits */
166 #define N_EXT 0x01 /* global or external symbol */
168 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
169 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
170 #define N_SECT 0xe /* defined symbol, n_sect holds
173 #define N_TYPE 0x0e /* type bit mask */
175 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
177 /* special section number values */
178 #define NO_SECT 0 /* no section, invalid */
179 #define MAX_SECT 255 /* maximum number of sections */
181 static struct section *sects, **sectstail;
182 static struct symbol *syms, **symstail;
183 static uint32_t nsyms;
185 /* These variables are set by macho_layout_symbols() to organize
186 the symbol table and string table in order the dynamic linker
187 expects. They are then used in macho_write() to put out the
188 symbols and strings in that order.
190 The order of the symbol table is:
192 defined external symbols (sorted by name)
193 undefined external symbols (sorted by name)
195 The order of the string table is:
196 strings for external symbols
197 strings for local symbols
199 static uint32_t ilocalsym = 0;
200 static uint32_t iextdefsym = 0;
201 static uint32_t iundefsym = 0;
202 static uint32_t nlocalsym;
203 static uint32_t nextdefsym;
204 static uint32_t nundefsym;
205 static struct symbol **extdefsyms = NULL;
206 static struct symbol **undefsyms = NULL;
208 static struct RAA *extsyms;
209 static struct SAA *strs;
210 static uint32_t strslen;
212 static FILE *machofp;
214 static evalfunc evaluate;
216 extern struct ofmt of_macho64;
218 /* Global file information. This should be cleaned up into either
219 a structure or as function arguments. */
220 uint32_t head_ncmds64 = 0;
221 uint32_t head_sizeofcmds64 = 0;
222 uint64_t seg_filesize64 = 0;
223 uint64_t seg_vmsize64 = 0;
224 uint32_t seg_nsects64 = 0;
225 uint64_t rel_padcnt64 = 0;
228 #define xstrncpy(xdst, xsrc) \
229 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
230 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
231 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
233 #define align(x, y) \
234 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
236 #define alignint32_t(x) \
237 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
239 #define alignint64_t(x) \
240 align(x, sizeof(int64_t)) /* align x to int64_t boundary */
242 static void debug_reloc (struct reloc *);
243 static void debug_section_relocs (struct section *) _unused;
245 static int exact_log2 (uint32_t align)
249 } else if (align & (align-1)) {
250 return -1; /* Not a power of 2 */
253 return __builtin_ctzl (align);
257 /* We know exactly one bit is set at this point. */
258 if (align & 0xffff0000)
260 if (align & 0xff00ff00)
262 if (align & 0xf0f0f0f0)
264 if (align & 0xcccccccc)
266 if (align & 0xaaaaaaaa)
274 static struct section *get_section_by_name(const char *segname,
275 const char *sectname)
279 for (s = sects; s != NULL; s = s->next)
280 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
286 static struct section *get_section_by_index(const int32_t index)
290 for (s = sects; s != NULL; s = s->next)
291 if (index == s->index)
297 static int32_t get_section_index_by_name(const char *segname,
298 const char *sectname)
302 for (s = sects; s != NULL; s = s->next)
303 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
309 static char *get_section_name_by_index(const int32_t index)
313 for (s = sects; s != NULL; s = s->next)
314 if (index == s->index)
320 static uint8_t get_section_fileindex_by_index(const int32_t index)
325 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
326 if (index == s->index)
331 "too many sections (>255) - clipped by fileindex");
336 static struct symbol *get_closest_section_symbol_by_offset(uint8_t fileindex, int64_t offset)
340 for (sym = syms; sym != NULL; sym = sym->next) {
341 if ((sym->sect != NO_SECT) &&
342 (sym->sect == fileindex) &&
343 ((int64_t)sym->value >= offset))
352 * Special section numbers which are used to define Mach-O special
353 * symbols, which can be used with WRT to provide PIC relocation
356 static int32_t macho_gotpcrel_sect;
358 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
368 (void)ldef; /* placate optimizers */
380 extsyms = raa_init();
383 /* string table starts with a zero byte - don't ask why */
384 saa_wbytes(strs, &zero, sizeof(char));
387 /* add special symbol for ..gotpcrel */
388 macho_gotpcrel_sect = seg_alloc();
389 macho_gotpcrel_sect ++;
390 ldef("..gotpcrel", macho_gotpcrel_sect, 0L, NULL, false, false, &of_macho64, error);
393 static void sect_write(struct section *sect,
394 const uint8_t *data, uint32_t len)
396 saa_wbytes(sect->data, data, len);
400 static int32_t add_reloc(struct section *sect, int32_t section,
401 int pcrel, int bytes, int64_t reloff)
406 int32_t adjustment = 0;
408 /* NeXT as puts relocs in reversed order (address-wise) into the
409 ** files, so we do the same, doesn't seem to make much of a
410 ** difference either way */
411 r = nasm_malloc(sizeof(struct reloc));
412 r->next = sect->relocs;
415 /* the current end of the section will be the symbol's address for
416 ** now, might have to be fixed by macho_fixup_relocs() later on. make
417 ** sure we don't make the symbol scattered by setting the highest
418 ** bit by accident */
419 r->addr = sect->size & ~R_SCATTERED;
421 r->pcrel = (pcrel ? 1 : 0);
423 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
441 /* set default relocation values */
442 r->type = 0; // X86_64_RELOC_UNSIGNED
443 r->snum = R_ABS; // Absolute Symbol (indicates no relocation)
445 /* absolute relocation */
449 if (section == NO_SEG) {
450 // r->snum = R_ABS; // Set above
454 fi = get_section_fileindex_by_index(section);
458 r->snum = raa_read(extsyms, section);
462 sym = get_closest_section_symbol_by_offset(fi, reloff);
463 r->snum = sym->initial_snum;
464 adjustment = sym->value;
468 /* relative relocation */
469 } else if (pcrel == 1) {
472 if (section == NO_SEG) {
473 r->type = 1; // X86_64_RELOC_SIGNED
477 r->type = 2; // X86_64_RELOC_BRANCH
478 fi = get_section_fileindex_by_index(section);
483 r->snum = raa_read(extsyms, section);
487 sym = get_closest_section_symbol_by_offset(fi, reloff);
488 r->snum = sym->initial_snum;
489 adjustment = sym->value;
494 } else if (pcrel == 2) {
496 r->type = 5; // X86_64_RELOC_SUBTRACTOR
499 } else if (pcrel == 3) {
500 r->type = 4; // X86_64_RELOC_GOT
501 r->snum = macho_gotpcrel_sect;
503 /* gotpcrel MOVQ load */
504 } else if (pcrel == 4) {
505 r->type = 3; // X86_64_RELOC_GOT_LOAD
506 r->snum = macho_gotpcrel_sect;
514 static void macho_output(int32_t secto, const void *data,
515 enum out_type type, uint64_t size,
516 int32_t section, int32_t wrt)
518 struct section *s, *sbss;
520 uint8_t mydata[16], *p, gotload;
522 if (secto == NO_SEG) {
523 if (type != OUT_RESERVE)
524 error(ERR_NONFATAL, "attempt to assemble code in "
530 s = get_section_by_index(secto);
533 error(ERR_WARNING, "attempt to assemble code in"
534 " section %d: defaulting to `.text'", secto);
535 s = get_section_by_name("__TEXT", "__text");
537 /* should never happen */
539 error(ERR_PANIC, "text section not found");
542 sbss = get_section_by_name("__DATA", "__bss");
544 if (s == sbss && type != OUT_RESERVE) {
545 error(ERR_WARNING, "attempt to initialize memory in the"
546 " BSS section: ignored");
547 s->size += realsize(type, size);
554 error(ERR_WARNING, "uninitialized space declared in"
555 " %s section: zeroing",
556 get_section_name_by_index(secto));
558 sect_write(s, NULL, size);
565 if (section != NO_SEG)
566 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
568 sect_write(s, data, size);
572 addr = *(int64_t *)data;
573 if (section != NO_SEG) {
575 error(ERR_NONFATAL, "Mach-O format does not support"
576 " section base references");
580 error(ERR_NONFATAL, "Mach-O 64-bit format does not support"
581 " 32-bit absolute addresses");
583 Seemingly, Mach-O's X86_64_RELOC_SUBTRACTOR would require
584 pre-determined knowledge of where the image base would be,
585 making it impractical for use in intermediate object files
588 addr -= add_reloc(s, section, 0, size, addr); // X86_64_RELOC_UNSIGNED
591 error(ERR_NONFATAL, "Mach-O format does not support"
598 WRITEADDR(p, addr, size);
599 sect_write(s, mydata, size);
604 WRITESHORT(p, *(int64_t *)data);
606 if (section == secto)
607 error(ERR_PANIC, "intra-section OUT_REL2ADR");
609 if (section == NO_SEG) {
611 } else if (section % 2) {
612 error(ERR_NONFATAL, "Mach-O format does not support"
613 " section base references");
615 error(ERR_NONFATAL, "Unsupported non-32-bit"
616 " Macho-O relocation [2]");
619 sect_write(s, mydata, 2L);
624 WRITELONG(p, *(int64_t *)data);
626 if (section == secto)
627 error(ERR_PANIC, "intra-section OUT_REL4ADR");
629 if (section != NO_SEG && section % 2) {
630 error(ERR_NONFATAL, "Mach-O format does not support"
631 " section base references");
634 *mydata -= add_reloc(s, section, 1, 4, (int64_t)*mydata); // X86_64_RELOC_SIGNED/BRANCH
635 } else if (wrt == macho_gotpcrel_sect) {
636 if (s->data->datalen > 1) {
637 saa_fread(s->data, s->data->datalen-2, &gotload, 1); // Retrieve Instruction Opcode
641 if (gotload == 0x8B) { // Check for MOVQ Opcode
642 *mydata -= add_reloc(s, section, 4, 4, (int64_t)*mydata); // X86_64_GOT_LOAD (MOVQ load)
644 *mydata -= add_reloc(s, section, 3, 4, (int64_t)*mydata); // X86_64_GOT
647 error(ERR_NONFATAL, "Mach-O format does not support"
649 wrt = NO_SEG; /* we can at least _try_ to continue */
653 sect_write(s, mydata, 4L);
657 error(ERR_PANIC, "unknown output type?");
662 static int32_t macho_section(char *name, int pass, int *bits)
664 int32_t index, originalIndex;
665 char *sectionAttributes;
671 /* Default to 64 bits. */
675 sectionAttributes = NULL;
677 sectionAttributes = name;
678 name = nasm_strsep(§ionAttributes, " \t");
681 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
682 /* make lookup into section name translation table */
683 if (!strcmp(name, sm->nasmsect)) {
684 char *currentAttribute;
686 /* try to find section with that name */
687 originalIndex = index = get_section_index_by_name(sm->segname,
690 /* create it if it doesn't exist yet */
692 s = *sectstail = nasm_malloc(sizeof(struct section));
694 sectstail = &s->next;
696 s->data = saa_init(1L);
697 s->index = seg_alloc();
701 xstrncpy(s->segname, sm->segname);
702 xstrncpy(s->sectname, sm->sectname);
705 s->flags = sm->flags;
709 s = get_section_by_index(index);
712 while ((NULL != sectionAttributes)
713 && (currentAttribute = nasm_strsep(§ionAttributes, " \t"))) {
714 if (0 != *currentAttribute) {
715 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
717 int newAlignment, value;
719 value = strtoul(currentAttribute + 6, (char**)&end, 0);
720 newAlignment = exact_log2(value);
724 "unknown or missing alignment value \"%s\" "
725 "specified for section \"%s\"",
726 currentAttribute + 6,
729 } else if (0 > newAlignment) {
731 "alignment of %d (for section \"%s\") is not "
738 if ((-1 != originalIndex)
739 && (s->align != newAlignment)
740 && (s->align != -1)) {
742 "section \"%s\" has already been specified "
743 "with alignment %d, conflicts with new "
751 s->align = newAlignment;
752 } else if (!nasm_stricmp("data", currentAttribute)) {
753 /* Do nothing; 'data' is implicit */
756 "unknown section attribute %s for section %s",
768 error(ERR_PANIC, "invalid section name %s", name);
772 static void macho_symdef(char *name, int32_t section, int64_t offset,
773 int is_global, char *special)
778 error(ERR_NONFATAL, "The Mach-O output format does "
779 "not support any special symbol types");
783 if (is_global == 3) {
784 error(ERR_NONFATAL, "The Mach-O format does not "
785 "(yet) support forward reference fixups.");
789 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
791 * This is a NASM special symbol. We never allow it into
792 * the Macho-O symbol table, even if it's a valid one. If it
793 * _isn't_ a valid one, we should barf immediately.
795 if (strcmp(name, "..gotpcrel"))
796 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
800 sym = *symstail = nasm_malloc(sizeof(struct symbol));
802 symstail = &sym->next;
809 sym->initial_snum = -1;
811 /* external and common symbols get N_EXT */
812 if (is_global != 0) {
816 if (section == NO_SEG) {
817 /* symbols in no section get absolute */
823 /* get the in-file index of the section the symbol was defined in */
824 sym->sect = get_section_fileindex_by_index(section);
826 /* track the initially allocated symbol number for use in future fix-ups */
827 sym->initial_snum = nsyms;
829 if (sym->sect == NO_SECT) {
831 /* remember symbol number of references to external
832 ** symbols, this works because every external symbol gets
833 ** its own section number allocated internally by nasm and
834 ** can so be used as a key */
835 extsyms = raa_write(extsyms, section, nsyms);
840 /* there isn't actually a difference between global
841 ** and common symbols, both even have their size in
847 /* give an error on unfound section if it's not an
848 ** external or common symbol (assemble_file() does a
849 ** seg_alloc() on every call for them) */
850 error(ERR_PANIC, "in-file index for section %d not found",
858 static int32_t macho_segbase(int32_t section)
863 static void macho_filename(char *inname, char *outname, efunc error)
865 standard_extension(inname, outname, ".o", error);
868 extern macros_t macho_stdmac[];
870 /* Comparison function for qsort symbol layout. */
871 static int layout_compare (const struct symbol **s1,
872 const struct symbol **s2)
874 return (strcmp ((*s1)->name, (*s2)->name));
877 /* The native assembler does a few things in a similar function
879 * Remove temporary labels
880 * Sort symbols according to local, external, undefined (by name)
881 * Order the string table
883 We do not remove temporary labels right now.
885 numsyms is the total number of symbols we have. strtabsize is the
886 number entries in the string table. */
888 static void macho_layout_symbols (uint32_t *numsyms,
889 uint32_t *strtabsize)
891 struct symbol *sym, **symp;
895 *strtabsize = sizeof (char);
899 while ((sym = *symp)) {
900 /* Undefined symbols are now external. */
901 if (sym->type == N_UNDF)
904 if ((sym->type & N_EXT) == 0) {
905 sym->snum = *numsyms;
906 *numsyms = *numsyms + 1;
910 if ((sym->type & N_TYPE) != N_UNDF) {
916 /* If we handle debug info we'll want
917 to check for it here instead of just
918 adding the symbol to the string table. */
919 sym->strx = *strtabsize;
920 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
921 *strtabsize += strlen(sym->name) + 1;
926 /* Next, sort the symbols. Most of this code is a direct translation from
927 the Apple cctools symbol layout. We need to keep compatibility with that. */
928 /* Set the indexes for symbol groups into the symbol table */
930 iextdefsym = nlocalsym;
931 iundefsym = nlocalsym + nextdefsym;
933 /* allocate arrays for sorting externals by name */
934 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
935 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
942 while ((sym = *symp)) {
944 if((sym->type & N_EXT) == 0) {
945 sym->strx = *strtabsize;
946 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
947 *strtabsize += strlen(sym->name) + 1;
950 if((sym->type & N_TYPE) != N_UNDF) {
951 extdefsyms[i++] = sym;
953 undefsyms[j++] = sym;
959 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
960 (int (*)(const void *, const void *))layout_compare);
961 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
962 (int (*)(const void *, const void *))layout_compare);
964 for(i = 0; i < nextdefsym; i++) {
965 extdefsyms[i]->snum = *numsyms;
968 for(j = 0; j < nundefsym; j++) {
969 undefsyms[j]->snum = *numsyms;
974 /* Calculate some values we'll need for writing later. */
976 static void macho_calculate_sizes (void)
980 /* count sections and calculate in-memory and in-file offsets */
981 for (s = sects; s != NULL; s = s->next) {
984 /* zerofill sections aren't actually written to the file */
985 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
986 seg_filesize64 += s->size;
988 /* recalculate segment address based on alignment and vm size */
989 s->addr = seg_vmsize64;
990 /* we need section alignment to calculate final section address */
992 s->align = DEFAULT_SECTION_ALIGNMENT;
994 uint64_t newaddr = align(s->addr, 1 << s->align);
995 pad = newaddr - s->addr;
999 seg_vmsize64 += s->size + pad;
1003 /* calculate size of all headers, load commands and sections to
1004 ** get a pointer to the start of all the raw data */
1005 if (seg_nsects64 > 0) {
1007 head_sizeofcmds64 +=
1008 MACHO_SEGCMD64_SIZE + seg_nsects64 * MACHO_SECTCMD64_SIZE;
1013 head_sizeofcmds64 += MACHO_SYMCMD_SIZE;
1017 /* Write out the header information for the file. */
1019 static void macho_write_header (void)
1021 fwriteint32_t(MH_MAGIC_64, machofp); /* magic */
1022 fwriteint32_t(CPU_TYPE_X86_64, machofp); /* CPU type */
1023 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
1024 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
1025 fwriteint32_t(head_ncmds64, machofp); /* number of load commands */
1026 fwriteint32_t(head_sizeofcmds64, machofp); /* size of load commands */
1027 fwriteint32_t(0, machofp); /* no flags */
1028 fwriteint32_t(0, machofp); /* reserved for future use */
1031 /* Write out the segment load command at offset. */
1033 static uint32_t macho_write_segment (uint64_t offset)
1035 uint64_t rel_base = alignint64_t (offset + seg_filesize64);
1036 uint32_t s_reloff = 0;
1039 fwriteint32_t(LC_SEGMENT_64, machofp); /* cmd == LC_SEGMENT_64 */
1041 /* size of load command including section load commands */
1042 fwriteint32_t(MACHO_SEGCMD64_SIZE + seg_nsects64 *
1043 MACHO_SECTCMD64_SIZE, machofp);
1045 /* in an MH_OBJECT file all sections are in one unnamed (name
1046 ** all zeros) segment */
1047 fwritezero(16, machofp);
1048 fwriteint64_t(0, machofp); /* in-memory offset */
1049 fwriteint64_t(seg_vmsize64, machofp); /* in-memory size */
1050 fwriteint64_t(offset, machofp); /* in-file offset to data */
1051 fwriteint64_t(seg_filesize64, machofp); /* in-file size */
1052 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
1053 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
1054 fwriteint32_t(seg_nsects64, machofp); /* number of sections */
1055 fwriteint32_t(0, machofp); /* no flags */
1057 /* emit section headers */
1058 for (s = sects; s != NULL; s = s->next) {
1059 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
1060 fwrite(s->segname, sizeof(s->segname), 1, machofp);
1061 fwriteint64_t(s->addr, machofp);
1062 fwriteint64_t(s->size, machofp);
1064 /* dummy data for zerofill sections or proper values */
1065 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
1066 fwriteint32_t(offset, machofp);
1067 /* Write out section alignment, as a power of two.
1068 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1070 s->align = DEFAULT_SECTION_ALIGNMENT;
1071 fwriteint32_t(s->align, machofp);
1072 /* To be compatible with cctools as we emit
1073 a zero reloff if we have no relocations. */
1074 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
1075 fwriteint32_t(s->nreloc, machofp);
1078 s_reloff += s->nreloc * MACHO_RELINFO64_SIZE;
1080 fwriteint32_t(0, machofp);
1081 fwriteint32_t(0, machofp);
1082 fwriteint32_t(0, machofp);
1083 fwriteint32_t(0, machofp);
1087 s->flags |= S_ATTR_LOC_RELOC;
1089 s->flags |= S_ATTR_EXT_RELOC;
1092 fwriteint32_t(s->flags, machofp); /* flags */
1093 fwriteint32_t(0, machofp); /* reserved */
1094 fwriteint32_t(0, machofp); /* reserved */
1096 fwriteint32_t(0, machofp); /* align */
1099 rel_padcnt64 = rel_base - offset;
1100 offset = rel_base + s_reloff;
1105 /* For a given chain of relocs r, write out the entire relocation
1106 chain to the object file. */
1108 static void macho_write_relocs (struct reloc *r)
1113 fwriteint32_t(r->addr, machofp); /* reloc offset */
1116 word2 |= r->pcrel << 24;
1117 word2 |= r->length << 25;
1118 word2 |= r->ext << 27;
1119 word2 |= r->type << 28;
1120 fwriteint32_t(word2, machofp); /* reloc data */
1125 /* Write out the section data. */
1126 static void macho_write_section (void)
1128 struct section *s, *s2;
1130 uint8_t fi, *p, *q, blk[8];
1134 for (s = sects; s != NULL; s = s->next) {
1135 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
1138 /* no padding needs to be done to the sections */
1140 /* Like a.out Mach-O references things in the data or bss
1141 * sections by addresses which are actually relative to the
1142 * start of the _text_ section, in the _file_. See outaout.c
1143 * for more information. */
1144 saa_rewind(s->data);
1145 for (r = s->relocs; r != NULL; r = r->next) {
1146 len = (int32_t)r->length << 1;
1147 if(len > 4) len = 8;
1148 saa_fread(s->data, r->addr, blk, len);
1152 /* get offset based on relocation type */
1153 if (r->length > 0) {
1154 l += ((int64_t)*p++) << 8;
1156 if (r->length > 1) {
1157 l += ((int64_t)*p++) << 16;
1158 l += ((int64_t)*p++) << 24;
1161 if (r->length > 2) {
1162 l += ((int64_t)*p++) << 32;
1163 l += ((int64_t)*p++) << 40;
1164 l += ((int64_t)*p++) << 48;
1165 l += ((int64_t)*p++) << 56;
1171 /* If the relocation is internal add to the current section
1172 offset. Otherwise the only value we need is the symbol
1173 offset which we already have. The linker takes care
1174 of the rest of the address. */
1176 /* generate final address by section address and offset */
1177 for (s2 = sects, fi = 1;
1178 s2 != NULL; s2 = s2->next, fi++) {
1179 if (fi == r->snum) {
1186 /* write new offset back */
1189 else if (r->length == 2)
1191 else if (r->length == 1)
1196 saa_fwrite(s->data, r->addr, blk, len);
1199 /* dump the section data to file */
1200 saa_fpwrite(s->data, machofp);
1203 /* pad last section up to reloc entries on int64_t boundary */
1204 fwritezero(rel_padcnt64, machofp);
1206 /* emit relocation entries */
1207 for (s = sects; s != NULL; s = s->next)
1208 macho_write_relocs (s->relocs);
1211 /* Write out the symbol table. We should already have sorted this
1213 static void macho_write_symtab (void)
1220 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1222 for (sym = syms; sym != NULL; sym = sym->next) {
1223 if ((sym->type & N_EXT) == 0) {
1224 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1225 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1226 fwrite(&sym->sect, 1, 1, machofp); /* section */
1227 fwriteint16_t(sym->desc, machofp); /* description */
1229 /* Fix up the symbol value now that we know the final section
1231 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1232 for (s = sects, fi = 1;
1233 s != NULL && fi < sym->sect; s = s->next, ++fi)
1234 sym->value += s->size;
1237 fwriteint64_t(sym->value, machofp); /* value (i.e. offset) */
1241 for (i = 0; i < nextdefsym; i++) {
1242 sym = extdefsyms[i];
1243 fwriteint32_t(sym->strx, machofp);
1244 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1245 fwrite(&sym->sect, 1, 1, machofp); /* section */
1246 fwriteint16_t(sym->desc, machofp); /* description */
1248 /* Fix up the symbol value now that we know the final section
1250 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1251 for (s = sects, fi = 1;
1252 s != NULL && fi < sym->sect; s = s->next, ++fi)
1253 sym->value += s->size;
1256 fwriteint64_t(sym->value, machofp); /* value (i.e. offset) */
1259 for (i = 0; i < nundefsym; i++) {
1261 fwriteint32_t(sym->strx, machofp);
1262 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1263 fwrite(&sym->sect, 1, 1, machofp); /* section */
1264 fwriteint16_t(sym->desc, machofp); /* description */
1266 // Fix up the symbol value now that we know the final section sizes.
1267 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1268 for (s = sects, fi = 1;
1269 s != NULL && fi < sym->sect; s = s->next, ++fi)
1270 sym->value += s->size;
1273 fwriteint64_t(sym->value, machofp); // value (i.e. offset)
1278 /* Fixup the snum in the relocation entries, we should be
1279 doing this only for externally referenced symbols. */
1280 static void macho_fixup_relocs (struct reloc *r)
1286 for (sym = syms; sym != NULL; sym = sym->next) {
1287 if (sym->initial_snum == r->snum) {
1288 r->snum = sym->snum;
1297 /* Write out the object file. */
1299 static void macho_write (void)
1301 uint64_t offset = 0;
1303 /* mach-o object file structure:
1309 ** uint32_t mach file type
1310 ** uint32_t number of load commands
1311 ** uint32_t size of all load commands
1312 ** (includes section struct size of segment command)
1316 ** uint32_t command type == LC_SEGMENT_64
1317 ** uint32_t size of load command
1318 ** (including section load commands)
1319 ** char[16] segment name
1320 ** uint64_t in-memory offset
1321 ** uint64_t in-memory size
1322 ** uint64_t in-file offset to data area
1323 ** uint64_t in-file size
1324 ** (in-memory size excluding zerofill sections)
1325 ** int maximum vm protection
1326 ** int initial vm protection
1327 ** uint32_t number of sections
1331 ** char[16] section name
1332 ** char[16] segment name
1333 ** uint64_t in-memory offset
1334 ** uint64_t in-memory size
1335 ** uint32_t in-file offset
1336 ** uint32_t alignment
1337 ** (irrelevant in MH_OBJECT)
1338 ** uint32_t in-file offset of relocation entires
1339 ** uint32_t number of relocations
1341 ** uint32_t reserved
1342 ** uint32_t reserved
1344 ** symbol table command
1345 ** uint32_t command type == LC_SYMTAB
1346 ** uint32_t size of load command
1347 ** uint32_t symbol table offset
1348 ** uint32_t number of symbol table entries
1349 ** uint32_t string table offset
1350 ** uint32_t string table size
1354 ** padding to int64_t boundary
1356 ** relocation data (struct reloc)
1358 ** uint data (symbolnum, pcrel, length, extern, type)
1360 ** symbol table data (struct nlist)
1361 ** int32_t string table entry number
1363 ** (extern, absolute, defined in section)
1365 ** (0 for global symbols, section number of definition (>= 1, <=
1366 ** 254) for local symbols, size of variable for common symbols
1367 ** [type == extern])
1368 ** int16_t description
1369 ** (for stab debugging format)
1370 ** uint64_t value (i.e. file offset) of symbol or stab offset
1372 ** string table data
1373 ** list of null-terminated strings
1376 /* Emit the Mach-O header. */
1377 macho_write_header();
1379 offset = MACHO_HEADER64_SIZE + head_sizeofcmds64;
1381 /* emit the segment load command */
1382 if (seg_nsects64 > 0)
1383 offset = macho_write_segment (offset);
1385 error(ERR_WARNING, "no sections?");
1388 /* write out symbol command */
1389 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1390 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1391 fwriteint32_t(offset, machofp); /* symbol table offset */
1392 fwriteint32_t(nsyms, machofp); /* number of symbol
1395 offset += nsyms * MACHO_NLIST64_SIZE;
1396 fwriteint32_t(offset, machofp); /* string table offset */
1397 fwriteint32_t(strslen, machofp); /* string table size */
1400 /* emit section data */
1401 if (seg_nsects64 > 0)
1402 macho_write_section ();
1404 /* emit symbol table if we have symbols */
1406 macho_write_symtab ();
1408 /* we don't need to pad here since MACHO_NLIST64_SIZE == 16 */
1410 /* emit string table */
1411 saa_fpwrite(strs, machofp);
1413 /* We do quite a bit here, starting with finalizing all of the data
1414 for the object file, writing, and then freeing all of the data from
1417 static void macho_cleanup(int debuginfo)
1425 /* Sort all symbols. */
1426 macho_layout_symbols (&nsyms, &strslen);
1428 /* Fixup relocation entries */
1429 for (s = sects; s != NULL; s = s->next) {
1430 macho_fixup_relocs (s->relocs);
1433 /* First calculate and finalize needed values. */
1434 macho_calculate_sizes();
1437 /* free up everything */
1438 while (sects->next) {
1440 sects = sects->next;
1443 while (s->relocs != NULL) {
1445 s->relocs = s->relocs->next;
1456 while (syms->next) {
1465 /* Debugging routines. */
1466 static void debug_reloc (struct reloc *r)
1468 fprintf (stdout, "reloc:\n");
1469 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1470 fprintf (stdout, "\tsnum: %d\n", r->snum);
1471 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1472 fprintf (stdout, "\tlength: %d\n", r->length);
1473 fprintf (stdout, "\text: %d\n", r->ext);
1474 fprintf (stdout, "\ttype: %d\n", r->type);
1477 static void debug_section_relocs (struct section *s)
1479 struct reloc *r = s->relocs;
1481 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1489 struct ofmt of_macho64 = {
1490 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",