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
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 * outmac64.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_MACHO) || defined(OF_MACHO64)
59 /* Mach-O in-file header structure sizes */
60 #define MACHO_HEADER_SIZE 28
61 #define MACHO_SEGCMD_SIZE 56
62 #define MACHO_SECTCMD_SIZE 68
63 #define MACHO_SYMCMD_SIZE 24
64 #define MACHO_NLIST_SIZE 12
65 #define MACHO_RELINFO_SIZE 8
67 #define MACHO_HEADER64_SIZE 32
68 #define MACHO_SEGCMD64_SIZE 72
69 #define MACHO_SECTCMD64_SIZE 80
70 #define MACHO_NLIST64_SIZE 16
72 /* Mach-O file header values */
73 #define MH_MAGIC 0xfeedface
74 #define MH_MAGIC_64 0xfeedfacf
75 #define CPU_TYPE_I386 7 /* x86 platform */
76 #define CPU_TYPE_X86_64 0x01000007 /* x86-64 platform */
77 #define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */
78 #define MH_OBJECT 0x1 /* object file */
80 #define LC_SEGMENT 0x1 /* 32-bit segment load cmd */
81 #define LC_SEGMENT_64 0x19 /* 64-bit segment load cmd */
82 #define LC_SYMTAB 0x2 /* symbol table load command */
84 #define VM_PROT_NONE (0x00)
85 #define VM_PROT_READ (0x01)
86 #define VM_PROT_WRITE (0x02)
87 #define VM_PROT_EXECUTE (0x04)
89 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
90 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
93 uint32_t ptrsize
; /* Pointer size in bytes */
94 uint32_t mh_magic
; /* Which magic number to use */
95 uint32_t cpu_type
; /* Which CPU type */
96 uint32_t lc_segment
; /* Which segment load command */
97 uint32_t header_size
; /* Header size */
98 uint32_t segcmd_size
; /* Segment command size */
99 uint32_t sectcmd_size
; /* Section command size */
100 uint32_t nlist_size
; /* Nlist (symbol) size */
103 static const struct macho_fmt
*fmt
;
105 static void fwriteptr(uint64_t data
, FILE * fp
)
107 fwriteaddr(data
, fmt
->ptrsize
, fp
);
111 /* nasm internal data */
112 struct section
*next
;
115 struct reloc
*relocs
;
118 /* data that goes into the file */
119 char sectname
[16]; /* what this section is called */
120 char segname
[16]; /* segment this section will be in */
121 uint64_t addr
; /* in-memory address (subject to alignment) */
122 uint64_t size
; /* in-memory and -file size */
123 uint64_t offset
; /* in-file offset */
124 uint32_t pad
; /* padding bytes before section */
125 uint32_t nreloc
; /* relocation entry count */
126 uint32_t flags
; /* type and attributes (masked) */
127 uint32_t extreloc
; /* external relocations */
130 #define SECTION_TYPE 0x000000ff /* section type mask */
132 #define S_REGULAR (0x0) /* standard section */
133 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
135 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
136 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
137 machine instructions */
138 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
139 relocation entries */
140 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
141 relocation entries */
142 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
143 machine instructions */
145 static struct sectmap
{
146 const char *nasmsect
;
148 const char *sectname
;
151 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
|S_ATTR_PURE_INSTRUCTIONS
},
152 {".data", "__DATA", "__data", S_REGULAR
},
153 {".rodata", "__DATA", "__const", S_REGULAR
},
154 {".bss", "__DATA", "__bss", S_ZEROFILL
},
155 {NULL
, NULL
, NULL
, 0}
159 /* nasm internal data */
162 /* data that goes into the file */
163 int32_t addr
; /* op's offset in section */
164 uint32_t snum
:24, /* contains symbol index if
165 ** ext otherwise in-file
167 pcrel
:1, /* relative relocation */
168 length
:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
169 ext
:1, /* external symbol referenced */
170 type
:4; /* reloc type */
173 #define R_ABS 0 /* absolute relocation */
174 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
175 ** highest bit == 1 */
178 /* nasm internal data */
179 struct symbol
*next
; /* next symbol in the list */
180 char *name
; /* name of this symbol */
181 int32_t initial_snum
; /* symbol number used above in
183 int32_t snum
; /* true snum for reloc */
185 /* data that goes into the file */
186 uint32_t strx
; /* string table index */
187 uint8_t type
; /* symbol type */
188 uint8_t sect
; /* NO_SECT or section number */
189 uint16_t desc
; /* for stab debugging, 0 for us */
190 uint64_t value
; /* offset of symbol in section */
193 /* symbol type bits */
194 #define N_EXT 0x01 /* global or external symbol */
196 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
197 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
198 #define N_SECT 0xe /* defined symbol, n_sect holds
201 #define N_TYPE 0x0e /* type bit mask */
203 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
205 /* special section number values */
206 #define NO_SECT 0 /* no section, invalid */
207 #define MAX_SECT 255 /* maximum number of sections */
209 static struct section
*sects
, **sectstail
, **sectstab
;
210 static struct symbol
*syms
, **symstail
;
211 static uint32_t nsyms
;
213 /* These variables are set by macho_layout_symbols() to organize
214 the symbol table and string table in order the dynamic linker
215 expects. They are then used in macho_write() to put out the
216 symbols and strings in that order.
218 The order of the symbol table is:
220 defined external symbols (sorted by name)
221 undefined external symbols (sorted by name)
223 The order of the string table is:
224 strings for external symbols
225 strings for local symbols
227 static uint32_t ilocalsym
= 0;
228 static uint32_t iextdefsym
= 0;
229 static uint32_t iundefsym
= 0;
230 static uint32_t nlocalsym
;
231 static uint32_t nextdefsym
;
232 static uint32_t nundefsym
;
233 static struct symbol
**extdefsyms
= NULL
;
234 static struct symbol
**undefsyms
= NULL
;
236 static struct RAA
*extsyms
;
237 static struct SAA
*strs
;
238 static uint32_t strslen
;
240 extern struct ofmt of_macho64
;
242 /* Global file information. This should be cleaned up into either
243 a structure or as function arguments. */
244 static uint32_t head_ncmds
= 0;
245 static uint32_t head_sizeofcmds
= 0;
246 static uint64_t seg_filesize
= 0;
247 static uint64_t seg_vmsize
= 0;
248 static uint32_t seg_nsects
= 0;
249 static uint64_t rel_padcnt
= 0;
252 #define xstrncpy(xdst, xsrc) \
253 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
254 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
255 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
257 #define alignint32_t(x) \
258 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
260 #define alignint64_t(x) \
261 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
263 #define alignptr(x) \
264 ALIGN(x, fmt->ptrsize) /* align x to output format width */
266 static void debug_reloc (struct reloc
*);
267 static void debug_section_relocs (struct section
*) _unused
;
269 static struct section
*get_section_by_name(const char *segname
,
270 const char *sectname
)
274 for (s
= sects
; s
!= NULL
; s
= s
->next
)
275 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
281 static struct section
*get_section_by_index(const int32_t index
)
285 for (s
= sects
; s
!= NULL
; s
= s
->next
)
286 if (index
== s
->index
)
292 static int32_t get_section_index_by_name(const char *segname
,
293 const char *sectname
)
297 for (s
= sects
; s
!= NULL
; s
= s
->next
)
298 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
304 static char *get_section_name_by_index(const int32_t index
)
308 for (s
= sects
; s
!= NULL
; s
= s
->next
)
309 if (index
== s
->index
)
315 static uint8_t get_section_fileindex_by_index(const int32_t index
)
320 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
321 if (index
== s
->index
)
325 nasm_error(ERR_WARNING
,
326 "too many sections (>255) - clipped by fileindex");
331 static struct symbol
*get_closest_section_symbol_by_offset(uint8_t fileindex
, int64_t offset
)
333 struct symbol
*nearest
= NULL
;
336 for (sym
= syms
; sym
; sym
= sym
->next
) {
337 if ((sym
->sect
!= NO_SECT
) && (sym
->sect
== fileindex
)) {
338 if ((int64_t)sym
->value
> offset
)
345 nasm_error(ERR_FATAL
, "No section for index %x offset %llx found\n",
346 fileindex
, (long long)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(void)
370 extsyms
= raa_init();
373 /* string table starts with a zero byte so index 0 is an empty string */
374 saa_wbytes(strs
, zero_buffer
, 1);
378 static void sect_write(struct section
*sect
,
379 const uint8_t *data
, uint32_t len
)
381 saa_wbytes(sect
->data
, data
, len
);
386 RL_ABS
, /* Absolute relocation */
387 RL_REL
, /* Relative relocation */
388 RL_SUB
, /* X86_64_RELOC_SUBTRACT */
389 RL_GOT
, /* X86_64_RELOC_GOT */
390 RL_GOTLOAD
, /* X86_64_RELOC_GOT_LOAD */
393 static int32_t add_reloc(struct section
*sect
, int32_t section
,
394 enum reltype reltype
, int bytes
, int64_t reloff
)
399 int32_t adjustment
= 0;
401 if (section
== NO_SEG
)
404 /* NeXT as puts relocs in reversed order (address-wise) into the
405 ** files, so we do the same, doesn't seem to make much of a
406 ** difference either way */
407 r
= nasm_malloc(sizeof(struct reloc
));
408 r
->next
= sect
->relocs
;
411 /* the current end of the section will be the symbol's address for
412 ** now, might have to be fixed by macho_fixup_relocs() later on. make
413 ** sure we don't make the symbol scattered by setting the highest
414 ** bit by accident */
415 r
->addr
= sect
->size
& ~R_SCATTERED
;
418 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
419 r
->length
= ilog2_32(bytes
);
421 /* set default relocation values */
426 /* absolute relocation */
429 if (section
== NO_SEG
) {
434 fi
= get_section_fileindex_by_index(section
);
438 r
->snum
= raa_read(extsyms
, section
);
441 sym
= get_closest_section_symbol_by_offset(fi
, reloff
);
442 r
->snum
= sym
->initial_snum
;
443 adjustment
= sym
->value
;
450 if (section
== NO_SEG
) {
452 r
->type
= 1; // X86_64_RELOC_SIGNED
455 r
->type
= 1; // X86_64_RELOC_SIGNED
456 fi
= get_section_fileindex_by_index(section
);
461 r
->snum
= raa_read(extsyms
, section
);
464 sym
= get_closest_section_symbol_by_offset(fi
, reloff
);
465 r
->snum
= sym
->initial_snum
;
466 adjustment
= sym
->value
;
473 r
->type
= 5; // X86_64_RELOC_SUBTRACTOR
478 r
->type
= 4; // X86_64_RELOC_GOT
479 r
->snum
= macho_gotpcrel_sect
;
484 r
->type
= 3; // X86_64_RELOC_GOT_LOAD
485 r
->snum
= macho_gotpcrel_sect
;
494 static void macho_output(int32_t secto
, const void *data
,
495 enum out_type type
, uint64_t size
,
496 int32_t section
, int32_t wrt
)
498 struct section
*s
, *sbss
;
500 uint8_t mydata
[16], *p
, gotload
;
502 if (secto
== NO_SEG
) {
503 if (type
!= OUT_RESERVE
)
504 nasm_error(ERR_NONFATAL
, "attempt to assemble code in "
510 s
= get_section_by_index(secto
);
513 nasm_error(ERR_WARNING
, "attempt to assemble code in"
514 " section %d: defaulting to `.text'", secto
);
515 s
= get_section_by_name("__TEXT", "__text");
517 /* should never happen */
519 nasm_error(ERR_PANIC
, "text section not found");
522 sbss
= get_section_by_name("__DATA", "__bss");
524 if (s
== sbss
&& type
!= OUT_RESERVE
) {
525 nasm_error(ERR_WARNING
, "attempt to initialize memory in the"
526 " BSS section: ignored");
527 s
->size
+= realsize(type
, size
);
531 memset(mydata
, 0, sizeof(mydata
));
536 nasm_error(ERR_WARNING
, "uninitialized space declared in"
537 " %s section: zeroing",
538 get_section_name_by_index(secto
));
540 sect_write(s
, NULL
, size
);
547 if (section
!= NO_SEG
)
548 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
550 sect_write(s
, data
, size
);
555 int asize
= abs((int)size
);
557 addr
= *(int64_t *)data
;
558 if (section
!= NO_SEG
) {
560 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
561 " section base references");
562 } else if (wrt
== NO_SEG
) {
563 if (fmt
->ptrsize
== 8 && asize
!= 8) {
564 nasm_error(ERR_NONFATAL
, "Mach-O 64-bit format does not support"
565 " 32-bit absolute addresses");
567 addr
-= add_reloc(s
, section
, RL_ABS
, asize
, addr
);
570 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
576 WRITEADDR(p
, addr
, asize
);
577 sect_write(s
, mydata
, asize
);
582 nasm_assert(section
!= secto
);
585 addr
= *(int64_t *)data
+ 2 - size
;
587 if (section
!= NO_SEG
&& section
% 2) {
588 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
589 " section base references");
590 } else if (fmt
->ptrsize
== 8) {
591 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit"
592 " Macho-O relocation [2]");
593 } else if (wrt
!= NO_SEG
) {
594 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
596 wrt
= NO_SEG
; /* we can at least _try_ to continue */
598 addr
-= add_reloc(s
, section
, RL_REL
, 2, addr
);
602 sect_write(s
, mydata
, 2);
606 nasm_assert(section
!= secto
);
609 addr
= *(int64_t *)data
+ 4 - size
;
611 if (section
!= NO_SEG
&& section
% 2) {
612 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
613 " section base references");
614 } else if (wrt
== NO_SEG
) {
615 /* Plain relative relocation */
616 addr
-= add_reloc(s
, section
, RL_REL
, 4, addr
);
617 } else if (wrt
== macho_gotpcrel_sect
) {
618 if (s
->data
->datalen
> 1) {
619 /* Retrieve instruction opcode */
620 saa_fread(s
->data
, s
->data
->datalen
-2, &gotload
, 1);
624 if (gotload
== 0x8B) {
625 /* Check for MOVQ Opcode -> X86_64_RELOC_GOT_LOAD */
626 addr
-= add_reloc(s
, section
, RL_GOTLOAD
, 4, addr
);
628 /* X86_64_RELOC_GOT */
629 addr
-= add_reloc(s
, section
, RL_GOT
, 4, addr
);
632 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
634 wrt
= NO_SEG
; /* we can at least _try_ to continue */
638 sect_write(s
, mydata
, 4);
642 nasm_error(ERR_NONFATAL
, "Unrepresentable relocation in Mach-O");
647 static int32_t macho_section(char *name
, int pass
, int *bits
)
650 char *sectionAttributes
;
656 /* Default to the appropriate number of bits. */
658 *bits
= fmt
->ptrsize
<< 3;
660 sectionAttributes
= NULL
;
662 sectionAttributes
= name
;
663 name
= nasm_strsep(§ionAttributes
, " \t");
666 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
667 /* make lookup into section name translation table */
668 if (!strcmp(name
, sm
->nasmsect
)) {
669 char *currentAttribute
;
671 /* try to find section with that name */
672 index
= get_section_index_by_name(sm
->segname
, sm
->sectname
);
674 /* create it if it doesn't exist yet */
676 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
678 sectstail
= &s
->next
;
680 s
->data
= saa_init(1L);
681 s
->index
= seg_alloc();
687 xstrncpy(s
->segname
, sm
->segname
);
688 xstrncpy(s
->sectname
, sm
->sectname
);
691 s
->flags
= sm
->flags
;
695 s
= get_section_by_index(index
);
698 while ((NULL
!= sectionAttributes
)
699 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
700 if (0 != *currentAttribute
) {
701 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
703 int newAlignment
, value
;
705 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
706 newAlignment
= alignlog2_32(value
);
709 nasm_error(ERR_FATAL
,
710 "unknown or missing alignment value \"%s\" "
711 "specified for section \"%s\"",
712 currentAttribute
+ 6,
715 } else if (0 > newAlignment
) {
716 nasm_error(ERR_FATAL
,
717 "alignment of %d (for section \"%s\") is not "
724 if (s
->align
< newAlignment
)
725 s
->align
= newAlignment
;
726 } else if (!nasm_stricmp("data", currentAttribute
)) {
727 /* Do nothing; 'data' is implicit */
729 nasm_error(ERR_FATAL
,
730 "unknown section attribute %s for section %s",
742 nasm_error(ERR_FATAL
, "invalid section name %s", name
);
746 static void macho_symdef(char *name
, int32_t section
, int64_t offset
,
747 int is_global
, char *special
)
752 nasm_error(ERR_NONFATAL
, "The Mach-O output format does "
753 "not support any special symbol types");
757 if (is_global
== 3) {
758 nasm_error(ERR_NONFATAL
, "The Mach-O format does not "
759 "(yet) support forward reference fixups.");
763 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
765 * This is a NASM special symbol. We never allow it into
766 * the Macho-O symbol table, even if it's a valid one. If it
767 * _isn't_ a valid one, we should barf immediately.
769 if (strcmp(name
, "..gotpcrel"))
770 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
774 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
776 symstail
= &sym
->next
;
783 sym
->initial_snum
= -1;
785 /* external and common symbols get N_EXT */
786 if (is_global
!= 0) {
790 if (section
== NO_SEG
) {
791 /* symbols in no section get absolute */
797 /* get the in-file index of the section the symbol was defined in */
798 sym
->sect
= get_section_fileindex_by_index(section
);
800 /* track the initially allocated symbol number for use in future fix-ups */
801 sym
->initial_snum
= nsyms
;
803 if (sym
->sect
== NO_SECT
) {
805 /* remember symbol number of references to external
806 ** symbols, this works because every external symbol gets
807 ** its own section number allocated internally by nasm and
808 ** can so be used as a key */
809 extsyms
= raa_write(extsyms
, section
, nsyms
);
814 /* there isn't actually a difference between global
815 ** and common symbols, both even have their size in
821 /* give an error on unfound section if it's not an
822 ** external or common symbol (assemble_file() does a
823 ** seg_alloc() on every call for them) */
824 nasm_error(ERR_PANIC
, "in-file index for section %d not found",
832 static void macho_sectalign(int32_t seg
, unsigned int value
)
837 list_for_each(s
, sects
) {
842 if (!s
|| !is_power2(value
))
845 align
= alignlog2_32(value
);
846 if (s
->align
< align
)
850 static int32_t macho_segbase(int32_t section
)
855 static void macho_filename(char *inname
, char *outname
)
857 standard_extension(inname
, outname
, ".o");
860 extern macros_t macho_stdmac
[];
862 /* Comparison function for qsort symbol layout. */
863 static int layout_compare (const struct symbol
**s1
,
864 const struct symbol
**s2
)
866 return (strcmp ((*s1
)->name
, (*s2
)->name
));
869 /* The native assembler does a few things in a similar function
871 * Remove temporary labels
872 * Sort symbols according to local, external, undefined (by name)
873 * Order the string table
875 We do not remove temporary labels right now.
877 numsyms is the total number of symbols we have. strtabsize is the
878 number entries in the string table. */
880 static void macho_layout_symbols (uint32_t *numsyms
,
881 uint32_t *strtabsize
)
883 struct symbol
*sym
, **symp
;
887 *strtabsize
= sizeof (char);
891 while ((sym
= *symp
)) {
892 /* Undefined symbols are now external. */
893 if (sym
->type
== N_UNDF
)
896 if ((sym
->type
& N_EXT
) == 0) {
897 sym
->snum
= *numsyms
;
898 *numsyms
= *numsyms
+ 1;
902 if ((sym
->type
& N_TYPE
) != N_UNDF
) {
908 /* If we handle debug info we'll want
909 to check for it here instead of just
910 adding the symbol to the string table. */
911 sym
->strx
= *strtabsize
;
912 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
913 *strtabsize
+= strlen(sym
->name
) + 1;
918 /* Next, sort the symbols. Most of this code is a direct translation from
919 the Apple cctools symbol layout. We need to keep compatibility with that. */
920 /* Set the indexes for symbol groups into the symbol table */
922 iextdefsym
= nlocalsym
;
923 iundefsym
= nlocalsym
+ nextdefsym
;
925 /* allocate arrays for sorting externals by name */
926 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
927 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
934 while ((sym
= *symp
)) {
936 if((sym
->type
& N_EXT
) == 0) {
937 sym
->strx
= *strtabsize
;
938 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
939 *strtabsize
+= strlen(sym
->name
) + 1;
942 if((sym
->type
& N_TYPE
) != N_UNDF
) {
943 extdefsyms
[i
++] = sym
;
945 undefsyms
[j
++] = sym
;
951 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
952 (int (*)(const void *, const void *))layout_compare
);
953 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
954 (int (*)(const void *, const void *))layout_compare
);
956 for(i
= 0; i
< nextdefsym
; i
++) {
957 extdefsyms
[i
]->snum
= *numsyms
;
960 for(j
= 0; j
< nundefsym
; j
++) {
961 undefsyms
[j
]->snum
= *numsyms
;
966 /* Calculate some values we'll need for writing later. */
968 static void macho_calculate_sizes (void)
973 /* count sections and calculate in-memory and in-file offsets */
974 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
977 /* recalculate segment address based on alignment and vm size */
978 s
->addr
= seg_vmsize
;
980 /* we need section alignment to calculate final section address */
982 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
984 newaddr
= ALIGN(s
->addr
, 1 << s
->align
);
987 seg_vmsize
= newaddr
+ s
->size
;
989 /* zerofill sections aren't actually written to the file */
990 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
992 * LLVM/Xcode as always aligns the section data to 4
993 * bytes; there is a comment in the LLVM source code that
994 * perhaps aligning to pointer size would be better.
996 s
->pad
= ALIGN(seg_filesize
, 4) - seg_filesize
;
997 s
->offset
= seg_filesize
+ s
->pad
;
998 seg_filesize
+= s
->size
+ s
->pad
;
1004 /* calculate size of all headers, load commands and sections to
1005 ** get a pointer to the start of all the raw data */
1006 if (seg_nsects
> 0) {
1008 head_sizeofcmds
+= fmt
->segcmd_size
+ seg_nsects
* fmt
->sectcmd_size
;
1013 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
1016 /* Create a table of sections by file index to avoid linear search */
1017 sectstab
= nasm_malloc((seg_nsects
+ 1) * sizeof(*sectstab
));
1019 for (s
= sects
, fi
= 1; s
!= NULL
; s
= s
->next
, fi
++)
1023 /* Write out the header information for the file. */
1025 static void macho_write_header (void)
1027 fwriteint32_t(fmt
->mh_magic
, ofile
); /* magic */
1028 fwriteint32_t(fmt
->cpu_type
, ofile
); /* CPU type */
1029 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, ofile
); /* CPU subtype */
1030 fwriteint32_t(MH_OBJECT
, ofile
); /* Mach-O file type */
1031 fwriteint32_t(head_ncmds
, ofile
); /* number of load commands */
1032 fwriteint32_t(head_sizeofcmds
, ofile
); /* size of load commands */
1033 fwriteint32_t(0, ofile
); /* no flags */
1034 fwritezero(fmt
->header_size
- 7*4, ofile
); /* reserved fields */
1037 /* Write out the segment load command at offset. */
1039 static uint32_t macho_write_segment (uint64_t offset
)
1041 uint64_t rel_base
= alignptr(offset
+ seg_filesize
);
1042 uint32_t s_reloff
= 0;
1045 fwriteint32_t(fmt
->lc_segment
, ofile
); /* cmd == LC_SEGMENT_64 */
1047 /* size of load command including section load commands */
1048 fwriteint32_t(fmt
->segcmd_size
+ seg_nsects
* fmt
->sectcmd_size
,
1051 /* in an MH_OBJECT file all sections are in one unnamed (name
1052 ** all zeros) segment */
1053 fwritezero(16, ofile
);
1054 fwriteptr(0, ofile
); /* in-memory offset */
1055 fwriteptr(seg_vmsize
, ofile
); /* in-memory size */
1056 fwriteptr(offset
, ofile
); /* in-file offset to data */
1057 fwriteptr(seg_filesize
, ofile
); /* in-file size */
1058 fwriteint32_t(VM_PROT_DEFAULT
, ofile
); /* maximum vm protection */
1059 fwriteint32_t(VM_PROT_DEFAULT
, ofile
); /* initial vm protection */
1060 fwriteint32_t(seg_nsects
, ofile
); /* number of sections */
1061 fwriteint32_t(0, ofile
); /* no flags */
1063 /* emit section headers */
1064 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1065 nasm_write(s
->sectname
, sizeof(s
->sectname
), ofile
);
1066 nasm_write(s
->segname
, sizeof(s
->segname
), ofile
);
1067 fwriteptr(s
->addr
, ofile
);
1068 fwriteptr(s
->size
, ofile
);
1070 /* dummy data for zerofill sections or proper values */
1071 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
1072 nasm_assert(s
->pad
!= (uint32_t)-1);
1074 fwriteint32_t(offset
, ofile
);
1076 /* Write out section alignment, as a power of two.
1077 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1078 fwriteint32_t(s
->align
, ofile
);
1079 /* To be compatible with cctools as we emit
1080 a zero reloff if we have no relocations. */
1081 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, ofile
);
1082 fwriteint32_t(s
->nreloc
, ofile
);
1084 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
1086 fwriteint32_t(0, ofile
);
1087 fwriteint32_t(s
->align
, ofile
);
1088 fwriteint32_t(0, ofile
);
1089 fwriteint32_t(0, ofile
);
1093 s
->flags
|= S_ATTR_LOC_RELOC
;
1095 s
->flags
|= S_ATTR_EXT_RELOC
;
1098 fwriteint32_t(s
->flags
, ofile
); /* flags */
1099 fwriteint32_t(0, ofile
); /* reserved */
1100 fwriteptr(0, ofile
); /* reserved */
1103 rel_padcnt
= rel_base
- offset
;
1104 offset
= rel_base
+ s_reloff
;
1109 /* For a given chain of relocs r, write out the entire relocation
1110 chain to the object file. */
1112 static void macho_write_relocs (struct reloc
*r
)
1117 fwriteint32_t(r
->addr
, ofile
); /* reloc offset */
1120 word2
|= r
->pcrel
<< 24;
1121 word2
|= r
->length
<< 25;
1122 word2
|= r
->ext
<< 27;
1123 word2
|= r
->type
<< 28;
1124 fwriteint32_t(word2
, ofile
); /* reloc data */
1129 /* Write out the section data. */
1130 static void macho_write_section (void)
1132 struct section
*s
, *s2
;
1142 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1143 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
1146 /* Like a.out Mach-O references things in the data or bss
1147 * sections by addresses which are actually relative to the
1148 * start of the _text_ section, in the _file_. See outaout.c
1149 * for more information. */
1150 saa_rewind(s
->data
);
1151 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
1152 len
= (uint32_t)1 << r
->length
;
1153 if (len
> 4) /* Can this ever be an issue?! */
1156 saa_fread(s
->data
, r
->addr
, blk
.buf
, len
);
1158 /* get offset based on relocation type */
1159 #ifdef WORDS_LITTLEENDIAN
1163 l
+= ((int64_t)blk
.buf
[1]) << 8;
1164 l
+= ((int64_t)blk
.buf
[2]) << 16;
1165 l
+= ((int64_t)blk
.buf
[3]) << 24;
1166 l
+= ((int64_t)blk
.buf
[4]) << 32;
1167 l
+= ((int64_t)blk
.buf
[5]) << 40;
1168 l
+= ((int64_t)blk
.buf
[6]) << 48;
1169 l
+= ((int64_t)blk
.buf
[7]) << 56;
1172 /* If the relocation is internal add to the current section
1173 offset. Otherwise the only value we need is the symbol
1174 offset which we already have. The linker takes care
1175 of the rest of the address. */
1177 /* generate final address by section address and offset */
1178 for (s2
= sects
, fi
= 1;
1179 s2
!= NULL
; s2
= s2
->next
, fi
++) {
1180 if (fi
== r
->snum
) {
1187 /* write new offset back */
1190 saa_fwrite(s
->data
, r
->addr
, blk
.buf
, len
);
1193 /* dump the section data to file */
1194 fwritezero(s
->pad
, ofile
);
1195 saa_fpwrite(s
->data
, ofile
);
1198 /* pad last section up to reloc entries on pointer boundary */
1199 fwritezero(rel_padcnt
, ofile
);
1201 /* emit relocation entries */
1202 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1203 macho_write_relocs (s
->relocs
);
1206 /* Write out the symbol table. We should already have sorted this
1208 static void macho_write_symtab (void)
1213 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1215 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1216 if ((sym
->type
& N_EXT
) == 0) {
1217 fwriteint32_t(sym
->strx
, ofile
); /* string table entry number */
1218 nasm_write(&sym
->type
, 1, ofile
); /* symbol type */
1219 nasm_write(&sym
->sect
, 1, ofile
); /* section */
1220 fwriteint16_t(sym
->desc
, ofile
); /* description */
1222 /* Fix up the symbol value now that we know the final section
1224 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1225 nasm_assert(sym
->sect
<= seg_nsects
);
1226 sym
->value
+= sectstab
[sym
->sect
]->addr
;
1229 fwriteptr(sym
->value
, ofile
); /* value (i.e. offset) */
1233 for (i
= 0; i
< nextdefsym
; i
++) {
1234 sym
= extdefsyms
[i
];
1235 fwriteint32_t(sym
->strx
, ofile
);
1236 nasm_write(&sym
->type
, 1, ofile
); /* symbol type */
1237 nasm_write(&sym
->sect
, 1, ofile
); /* section */
1238 fwriteint16_t(sym
->desc
, ofile
); /* description */
1240 /* Fix up the symbol value now that we know the final section
1242 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1243 nasm_assert(sym
->sect
<= seg_nsects
);
1244 sym
->value
+= sectstab
[sym
->sect
]->addr
;
1247 fwriteptr(sym
->value
, ofile
); /* value (i.e. offset) */
1250 for (i
= 0; i
< nundefsym
; i
++) {
1252 fwriteint32_t(sym
->strx
, ofile
);
1253 nasm_write(&sym
->type
, 1, ofile
); /* symbol type */
1254 nasm_write(&sym
->sect
, 1, ofile
); /* section */
1255 fwriteint16_t(sym
->desc
, ofile
); /* description */
1257 /* Fix up the symbol value now that we know the final section
1259 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1260 nasm_assert(sym
->sect
<= seg_nsects
);
1261 sym
->value
+= sectstab
[sym
->sect
]->addr
;
1264 fwriteptr(sym
->value
, ofile
); /* value (i.e. offset) */
1269 /* Fixup the snum in the relocation entries, we should be
1270 doing this only for externally referenced symbols. */
1271 static void macho_fixup_relocs (struct reloc
*r
)
1277 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1278 if (sym
->initial_snum
== r
->snum
) {
1279 r
->snum
= sym
->snum
;
1288 /* Write out the object file. */
1290 static void macho_write (void)
1292 uint64_t offset
= 0;
1294 /* mach-o object file structure:
1300 ** uint32_t mach file type
1301 ** uint32_t number of load commands
1302 ** uint32_t size of all load commands
1303 ** (includes section struct size of segment command)
1307 ** uint32_t command type == LC_SEGMENT[_64]
1308 ** uint32_t size of load command
1309 ** (including section load commands)
1310 ** char[16] segment name
1311 ** pointer in-memory offset
1312 ** pointer in-memory size
1313 ** pointer in-file offset to data area
1314 ** pointer in-file size
1315 ** (in-memory size excluding zerofill sections)
1316 ** int maximum vm protection
1317 ** int initial vm protection
1318 ** uint32_t number of sections
1322 ** char[16] section name
1323 ** char[16] segment name
1324 ** pointer in-memory offset
1325 ** pointer in-memory size
1326 ** uint32_t in-file offset
1327 ** uint32_t alignment
1328 ** (irrelevant in MH_OBJECT)
1329 ** uint32_t in-file offset of relocation entires
1330 ** uint32_t number of relocations
1332 ** uint32_t reserved
1333 ** uint32_t reserved
1335 ** symbol table command
1336 ** uint32_t command type == LC_SYMTAB
1337 ** uint32_t size of load command
1338 ** uint32_t symbol table offset
1339 ** uint32_t number of symbol table entries
1340 ** uint32_t string table offset
1341 ** uint32_t string table size
1345 ** padding to pointer boundary
1347 ** relocation data (struct reloc)
1349 ** uint data (symbolnum, pcrel, length, extern, type)
1351 ** symbol table data (struct nlist)
1352 ** int32_t string table entry number
1354 ** (extern, absolute, defined in section)
1356 ** (0 for global symbols, section number of definition (>= 1, <=
1357 ** 254) for local symbols, size of variable for common symbols
1358 ** [type == extern])
1359 ** int16_t description
1360 ** (for stab debugging format)
1361 ** pointer value (i.e. file offset) of symbol or stab offset
1363 ** string table data
1364 ** list of null-terminated strings
1367 /* Emit the Mach-O header. */
1368 macho_write_header();
1370 offset
= fmt
->header_size
+ head_sizeofcmds
;
1372 /* emit the segment load command */
1374 offset
= macho_write_segment (offset
);
1376 nasm_error(ERR_WARNING
, "no sections?");
1379 /* write out symbol command */
1380 fwriteint32_t(LC_SYMTAB
, ofile
); /* cmd == LC_SYMTAB */
1381 fwriteint32_t(MACHO_SYMCMD_SIZE
, ofile
); /* size of load command */
1382 fwriteint32_t(offset
, ofile
); /* symbol table offset */
1383 fwriteint32_t(nsyms
, ofile
); /* number of symbol
1385 offset
+= nsyms
* fmt
->nlist_size
;
1386 fwriteint32_t(offset
, ofile
); /* string table offset */
1387 fwriteint32_t(strslen
, ofile
); /* string table size */
1390 /* emit section data */
1392 macho_write_section ();
1394 /* emit symbol table if we have symbols */
1396 macho_write_symtab ();
1398 /* we don't need to pad here, we are already aligned */
1400 /* emit string table */
1401 saa_fpwrite(strs
, ofile
);
1403 /* We do quite a bit here, starting with finalizing all of the data
1404 for the object file, writing, and then freeing all of the data from
1407 static void macho_cleanup(int debuginfo
)
1415 /* Sort all symbols. */
1416 macho_layout_symbols (&nsyms
, &strslen
);
1418 /* Fixup relocation entries */
1419 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1420 macho_fixup_relocs (s
->relocs
);
1423 /* First calculate and finalize needed values. */
1424 macho_calculate_sizes();
1427 /* free up everything */
1428 while (sects
->next
) {
1430 sects
= sects
->next
;
1433 while (s
->relocs
!= NULL
) {
1435 s
->relocs
= s
->relocs
->next
;
1451 nasm_free(extdefsyms
);
1452 nasm_free(undefsyms
);
1453 nasm_free(sectstab
);
1456 /* Debugging routines. */
1457 static void debug_reloc (struct reloc
*r
)
1459 fprintf (stdout
, "reloc:\n");
1460 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1461 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1462 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1463 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1464 fprintf (stdout
, "\text: %d\n", r
->ext
);
1465 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1468 static void debug_section_relocs (struct section
*s
)
1470 struct reloc
*r
= s
->relocs
;
1472 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1481 static const struct macho_fmt macho32_fmt
= {
1492 static void macho32_init(void)
1497 macho_gotpcrel_sect
= NO_SEG
;
1500 struct ofmt of_macho32
= {
1501 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1522 static const struct macho_fmt macho64_fmt
= {
1527 MACHO_HEADER64_SIZE
,
1528 MACHO_SEGCMD64_SIZE
,
1529 MACHO_SECTCMD64_SIZE
,
1533 static void macho64_init(void)
1538 /* add special symbol for ..gotpcrel */
1539 macho_gotpcrel_sect
= seg_alloc();
1540 macho_gotpcrel_sect
++;
1541 define_label("..gotpcrel", macho_gotpcrel_sect
, 0L, NULL
, false, false);
1544 struct ofmt of_macho64
= {
1545 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",