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 macho32_fmt
= {
114 static const struct macho_fmt macho64_fmt
= {
121 MACHO_SECTCMD64_SIZE
,
125 static const struct macho_fmt
*fmt
;
127 static void fwriteptr(uint64_t data
, FILE * fp
)
129 fwriteaddr(data
, fmt
->ptrsize
, fp
);
133 /* nasm internal data */
134 struct section
*next
;
137 struct reloc
*relocs
;
140 /* data that goes into the file */
141 char sectname
[16]; /* what this section is called */
142 char segname
[16]; /* segment this section will be in */
143 uint64_t addr
; /* in-memory address (subject to alignment) */
144 uint64_t size
; /* in-memory and -file size */
145 uint64_t offset
; /* in-file offset */
146 uint32_t pad
; /* padding bytes before section */
147 uint32_t nreloc
; /* relocation entry count */
148 uint32_t flags
; /* type and attributes (masked) */
149 uint32_t extreloc
; /* external relocations */
152 #define SECTION_TYPE 0x000000ff /* section type mask */
154 #define S_REGULAR (0x0) /* standard section */
155 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
157 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
158 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
159 machine instructions */
160 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
161 relocation entries */
162 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
163 relocation entries */
164 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
165 machine instructions */
167 static struct sectmap
{
168 const char *nasmsect
;
170 const char *sectname
;
173 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
|S_ATTR_PURE_INSTRUCTIONS
},
174 {".data", "__DATA", "__data", S_REGULAR
},
175 {".rodata", "__DATA", "__const", S_REGULAR
},
176 {".bss", "__DATA", "__bss", S_ZEROFILL
},
177 {NULL
, NULL
, NULL
, 0}
181 /* nasm internal data */
184 /* data that goes into the file */
185 int32_t addr
; /* op's offset in section */
186 uint32_t snum
:24, /* contains symbol index if
187 ** ext otherwise in-file
189 pcrel
:1, /* relative relocation */
190 length
:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
191 ext
:1, /* external symbol referenced */
192 type
:4; /* reloc type */
195 #define R_ABS 0 /* absolute relocation */
196 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
197 ** highest bit == 1 */
200 /* nasm internal data */
201 struct symbol
*next
; /* next symbol in the list */
202 char *name
; /* name of this symbol */
203 int32_t initial_snum
; /* symbol number used above in
205 int32_t snum
; /* true snum for reloc */
207 /* data that goes into the file */
208 uint32_t strx
; /* string table index */
209 uint8_t type
; /* symbol type */
210 uint8_t sect
; /* NO_SECT or section number */
211 uint16_t desc
; /* for stab debugging, 0 for us */
212 uint64_t value
; /* offset of symbol in section */
215 /* symbol type bits */
216 #define N_EXT 0x01 /* global or external symbol */
218 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
219 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
220 #define N_SECT 0xe /* defined symbol, n_sect holds
223 #define N_TYPE 0x0e /* type bit mask */
225 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
227 /* special section number values */
228 #define NO_SECT 0 /* no section, invalid */
229 #define MAX_SECT 255 /* maximum number of sections */
231 static struct section
*sects
, **sectstail
, **sectstab
;
232 static struct symbol
*syms
, **symstail
;
233 static uint32_t nsyms
;
235 /* These variables are set by macho_layout_symbols() to organize
236 the symbol table and string table in order the dynamic linker
237 expects. They are then used in macho_write() to put out the
238 symbols and strings in that order.
240 The order of the symbol table is:
242 defined external symbols (sorted by name)
243 undefined external symbols (sorted by name)
245 The order of the string table is:
246 strings for external symbols
247 strings for local symbols
249 static uint32_t ilocalsym
= 0;
250 static uint32_t iextdefsym
= 0;
251 static uint32_t iundefsym
= 0;
252 static uint32_t nlocalsym
;
253 static uint32_t nextdefsym
;
254 static uint32_t nundefsym
;
255 static struct symbol
**extdefsyms
= NULL
;
256 static struct symbol
**undefsyms
= NULL
;
258 static struct RAA
*extsyms
;
259 static struct SAA
*strs
;
260 static uint32_t strslen
;
262 extern struct ofmt of_macho64
;
264 /* Global file information. This should be cleaned up into either
265 a structure or as function arguments. */
266 static uint32_t head_ncmds
= 0;
267 static uint32_t head_sizeofcmds
= 0;
268 static uint64_t seg_filesize
= 0;
269 static uint64_t seg_vmsize
= 0;
270 static uint32_t seg_nsects
= 0;
271 static uint64_t rel_padcnt
= 0;
274 #define xstrncpy(xdst, xsrc) \
275 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
276 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
277 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
279 #define alignint32_t(x) \
280 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
282 #define alignint64_t(x) \
283 ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
285 #define alignptr(x) \
286 ALIGN(x, fmt->ptrsize) /* align x to output format width */
288 static void debug_reloc (struct reloc
*);
289 static void debug_section_relocs (struct section
*) _unused
;
291 static struct section
*get_section_by_name(const char *segname
,
292 const char *sectname
)
296 for (s
= sects
; s
!= NULL
; s
= s
->next
)
297 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
303 static struct section
*get_section_by_index(const int32_t index
)
307 for (s
= sects
; s
!= NULL
; s
= s
->next
)
308 if (index
== s
->index
)
314 static int32_t get_section_index_by_name(const char *segname
,
315 const char *sectname
)
319 for (s
= sects
; s
!= NULL
; s
= s
->next
)
320 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
326 static char *get_section_name_by_index(const int32_t index
)
330 for (s
= sects
; s
!= NULL
; s
= s
->next
)
331 if (index
== s
->index
)
337 static uint8_t get_section_fileindex_by_index(const int32_t index
)
342 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
343 if (index
== s
->index
)
347 nasm_error(ERR_WARNING
,
348 "too many sections (>255) - clipped by fileindex");
353 static struct symbol
*get_closest_section_symbol_by_offset(uint8_t fileindex
, int64_t offset
)
355 struct symbol
*nearest
= NULL
;
358 for (sym
= syms
; sym
; sym
= sym
->next
) {
359 if ((sym
->sect
!= NO_SECT
) && (sym
->sect
== fileindex
)) {
360 if ((int64_t)sym
->value
> offset
)
367 nasm_error(ERR_FATAL
, "No section for index %x offset %llx found\n",
368 fileindex
, (long long)offset
);
374 * Special section numbers which are used to define Mach-O special
375 * symbols, which can be used with WRT to provide PIC relocation
378 static int32_t macho_gotpcrel_sect
;
380 static void macho_init(void)
392 extsyms
= raa_init();
395 /* string table starts with a zero byte so index 0 is an empty string */
396 saa_wbytes(strs
, zero_buffer
, 1);
400 static void sect_write(struct section
*sect
,
401 const uint8_t *data
, uint32_t len
)
403 saa_wbytes(sect
->data
, data
, len
);
408 RL_ABS
, /* Absolute relocation */
409 RL_REL
, /* Relative relocation */
410 RL_SUB
, /* X86_64_RELOC_SUBTRACT */
411 RL_GOT
, /* X86_64_RELOC_GOT */
412 RL_GOTLOAD
, /* X86_64_RELOC_GOT_LOAD */
415 static int32_t add_reloc(struct section
*sect
, int32_t section
,
416 enum reltype reltype
, int bytes
, int64_t reloff
)
421 int32_t adjustment
= 0;
423 if (section
== NO_SEG
)
426 /* NeXT as puts relocs in reversed order (address-wise) into the
427 ** files, so we do the same, doesn't seem to make much of a
428 ** difference either way */
429 r
= nasm_malloc(sizeof(struct reloc
));
430 r
->next
= sect
->relocs
;
433 /* the current end of the section will be the symbol's address for
434 ** now, might have to be fixed by macho_fixup_relocs() later on. make
435 ** sure we don't make the symbol scattered by setting the highest
436 ** bit by accident */
437 r
->addr
= sect
->size
& ~R_SCATTERED
;
440 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
441 r
->length
= ilog2_32(bytes
);
443 /* set default relocation values */
448 /* absolute relocation */
451 if (section
== NO_SEG
) {
456 fi
= get_section_fileindex_by_index(section
);
460 r
->snum
= raa_read(extsyms
, section
);
463 sym
= get_closest_section_symbol_by_offset(fi
, reloff
);
464 r
->snum
= sym
->initial_snum
;
465 adjustment
= sym
->value
;
472 if (section
== NO_SEG
) {
474 r
->type
= 1; // X86_64_RELOC_SIGNED
477 r
->type
= 1; // X86_64_RELOC_SIGNED
478 fi
= get_section_fileindex_by_index(section
);
483 r
->snum
= raa_read(extsyms
, section
);
486 sym
= get_closest_section_symbol_by_offset(fi
, reloff
);
487 r
->snum
= sym
->initial_snum
;
488 adjustment
= sym
->value
;
495 r
->type
= 5; // X86_64_RELOC_SUBTRACTOR
500 r
->type
= 4; // X86_64_RELOC_GOT
501 r
->snum
= macho_gotpcrel_sect
;
506 r
->type
= 3; // X86_64_RELOC_GOT_LOAD
507 r
->snum
= macho_gotpcrel_sect
;
516 static void macho_output(int32_t secto
, const void *data
,
517 enum out_type type
, uint64_t size
,
518 int32_t section
, int32_t wrt
)
520 struct section
*s
, *sbss
;
522 uint8_t mydata
[16], *p
, gotload
;
524 if (secto
== NO_SEG
) {
525 if (type
!= OUT_RESERVE
)
526 nasm_error(ERR_NONFATAL
, "attempt to assemble code in "
532 s
= get_section_by_index(secto
);
535 nasm_error(ERR_WARNING
, "attempt to assemble code in"
536 " section %d: defaulting to `.text'", secto
);
537 s
= get_section_by_name("__TEXT", "__text");
539 /* should never happen */
541 nasm_error(ERR_PANIC
, "text section not found");
544 sbss
= get_section_by_name("__DATA", "__bss");
546 if (s
== sbss
&& type
!= OUT_RESERVE
) {
547 nasm_error(ERR_WARNING
, "attempt to initialize memory in the"
548 " BSS section: ignored");
549 s
->size
+= realsize(type
, size
);
553 memset(mydata
, 0, sizeof(mydata
));
558 nasm_error(ERR_WARNING
, "uninitialized space declared in"
559 " %s section: zeroing",
560 get_section_name_by_index(secto
));
562 sect_write(s
, NULL
, size
);
569 if (section
!= NO_SEG
)
570 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
572 sect_write(s
, data
, size
);
577 int asize
= abs((int)size
);
579 addr
= *(int64_t *)data
;
580 if (section
!= NO_SEG
) {
582 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
583 " section base references");
584 } else if (wrt
== NO_SEG
) {
585 if (fmt
->ptrsize
== 8 && asize
!= 8) {
586 nasm_error(ERR_NONFATAL
, "Mach-O 64-bit format does not support"
587 " 32-bit absolute addresses");
589 addr
-= add_reloc(s
, section
, RL_ABS
, asize
, addr
);
592 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
598 WRITEADDR(p
, addr
, asize
);
599 sect_write(s
, mydata
, asize
);
604 nasm_assert(section
!= secto
);
607 addr
= *(int64_t *)data
+ 2 - size
;
609 if (section
!= NO_SEG
&& section
% 2) {
610 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
611 " section base references");
612 } else if (fmt
->ptrsize
== 8) {
613 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit"
614 " Macho-O relocation [2]");
615 } else if (wrt
!= NO_SEG
) {
616 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
618 wrt
= NO_SEG
; /* we can at least _try_ to continue */
620 addr
-= add_reloc(s
, section
, RL_REL
, 2, addr
);
624 sect_write(s
, mydata
, 2);
628 nasm_assert(section
!= secto
);
631 addr
= *(int64_t *)data
+ 4 - size
;
633 if (section
!= NO_SEG
&& section
% 2) {
634 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
635 " section base references");
636 } else if (wrt
== NO_SEG
) {
637 /* Plain relative relocation */
638 addr
-= add_reloc(s
, section
, RL_REL
, 4, addr
);
639 } else if (wrt
== macho_gotpcrel_sect
) {
640 if (s
->data
->datalen
> 1) {
641 /* Retrieve instruction opcode */
642 saa_fread(s
->data
, s
->data
->datalen
-2, &gotload
, 1);
646 if (gotload
== 0x8B) {
647 /* Check for MOVQ Opcode -> X86_64_RELOC_GOT_LOAD */
648 addr
-= add_reloc(s
, section
, RL_GOTLOAD
, 4, addr
);
650 /* X86_64_RELOC_GOT */
651 addr
-= add_reloc(s
, section
, RL_GOT
, 4, addr
);
654 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
656 wrt
= NO_SEG
; /* we can at least _try_ to continue */
660 sect_write(s
, mydata
, 4);
664 nasm_error(ERR_NONFATAL
, "Unrepresentable relocation in Mach-O");
669 static int32_t macho_section(char *name
, int pass
, int *bits
)
671 int32_t index
, originalIndex
;
672 char *sectionAttributes
;
678 /* Default to 64 bits. */
682 sectionAttributes
= NULL
;
684 sectionAttributes
= name
;
685 name
= nasm_strsep(§ionAttributes
, " \t");
688 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
689 /* make lookup into section name translation table */
690 if (!strcmp(name
, sm
->nasmsect
)) {
691 char *currentAttribute
;
693 /* try to find section with that name */
694 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
697 /* create it if it doesn't exist yet */
699 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
701 sectstail
= &s
->next
;
703 s
->data
= saa_init(1L);
704 s
->index
= seg_alloc();
710 xstrncpy(s
->segname
, sm
->segname
);
711 xstrncpy(s
->sectname
, sm
->sectname
);
714 s
->flags
= sm
->flags
;
718 s
= get_section_by_index(index
);
721 while ((NULL
!= sectionAttributes
)
722 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
723 if (0 != *currentAttribute
) {
724 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
726 int newAlignment
, value
;
728 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
729 newAlignment
= alignlog2_32(value
);
732 nasm_error(ERR_PANIC
,
733 "unknown or missing alignment value \"%s\" "
734 "specified for section \"%s\"",
735 currentAttribute
+ 6,
738 } else if (0 > newAlignment
) {
739 nasm_error(ERR_PANIC
,
740 "alignment of %d (for section \"%s\") is not "
747 if ((-1 != originalIndex
)
748 && (s
->align
!= newAlignment
)
749 && (s
->align
!= -1)) {
750 nasm_error(ERR_PANIC
,
751 "section \"%s\" has already been specified "
752 "with alignment %d, conflicts with new "
760 s
->align
= newAlignment
;
761 } else if (!nasm_stricmp("data", currentAttribute
)) {
762 /* Do nothing; 'data' is implicit */
764 nasm_error(ERR_PANIC
,
765 "unknown section attribute %s for section %s",
777 nasm_error(ERR_PANIC
, "invalid section name %s", name
);
781 static void macho_symdef(char *name
, int32_t section
, int64_t offset
,
782 int is_global
, char *special
)
787 nasm_error(ERR_NONFATAL
, "The Mach-O output format does "
788 "not support any special symbol types");
792 if (is_global
== 3) {
793 nasm_error(ERR_NONFATAL
, "The Mach-O format does not "
794 "(yet) support forward reference fixups.");
798 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
800 * This is a NASM special symbol. We never allow it into
801 * the Macho-O symbol table, even if it's a valid one. If it
802 * _isn't_ a valid one, we should barf immediately.
804 if (strcmp(name
, "..gotpcrel"))
805 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
809 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
811 symstail
= &sym
->next
;
818 sym
->initial_snum
= -1;
820 /* external and common symbols get N_EXT */
821 if (is_global
!= 0) {
825 if (section
== NO_SEG
) {
826 /* symbols in no section get absolute */
832 /* get the in-file index of the section the symbol was defined in */
833 sym
->sect
= get_section_fileindex_by_index(section
);
835 /* track the initially allocated symbol number for use in future fix-ups */
836 sym
->initial_snum
= nsyms
;
838 if (sym
->sect
== NO_SECT
) {
840 /* remember symbol number of references to external
841 ** symbols, this works because every external symbol gets
842 ** its own section number allocated internally by nasm and
843 ** can so be used as a key */
844 extsyms
= raa_write(extsyms
, section
, nsyms
);
849 /* there isn't actually a difference between global
850 ** and common symbols, both even have their size in
856 /* give an error on unfound section if it's not an
857 ** external or common symbol (assemble_file() does a
858 ** seg_alloc() on every call for them) */
859 nasm_error(ERR_PANIC
, "in-file index for section %d not found",
867 static void macho_sectalign(int32_t seg
, unsigned int value
)
871 list_for_each(s
, sects
) {
876 if (!s
|| !is_power2(value
))
879 value
= alignlog2_32(value
);
880 if (s
->align
< (int)value
)
884 static int32_t macho_segbase(int32_t section
)
889 static void macho_filename(char *inname
, char *outname
)
891 standard_extension(inname
, outname
, ".o");
894 extern macros_t macho_stdmac
[];
896 /* Comparison function for qsort symbol layout. */
897 static int layout_compare (const struct symbol
**s1
,
898 const struct symbol
**s2
)
900 return (strcmp ((*s1
)->name
, (*s2
)->name
));
903 /* The native assembler does a few things in a similar function
905 * Remove temporary labels
906 * Sort symbols according to local, external, undefined (by name)
907 * Order the string table
909 We do not remove temporary labels right now.
911 numsyms is the total number of symbols we have. strtabsize is the
912 number entries in the string table. */
914 static void macho_layout_symbols (uint32_t *numsyms
,
915 uint32_t *strtabsize
)
917 struct symbol
*sym
, **symp
;
921 *strtabsize
= sizeof (char);
925 while ((sym
= *symp
)) {
926 /* Undefined symbols are now external. */
927 if (sym
->type
== N_UNDF
)
930 if ((sym
->type
& N_EXT
) == 0) {
931 sym
->snum
= *numsyms
;
932 *numsyms
= *numsyms
+ 1;
936 if ((sym
->type
& N_TYPE
) != N_UNDF
) {
942 /* If we handle debug info we'll want
943 to check for it here instead of just
944 adding the symbol to the string table. */
945 sym
->strx
= *strtabsize
;
946 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
947 *strtabsize
+= strlen(sym
->name
) + 1;
952 /* Next, sort the symbols. Most of this code is a direct translation from
953 the Apple cctools symbol layout. We need to keep compatibility with that. */
954 /* Set the indexes for symbol groups into the symbol table */
956 iextdefsym
= nlocalsym
;
957 iundefsym
= nlocalsym
+ nextdefsym
;
959 /* allocate arrays for sorting externals by name */
960 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
961 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
968 while ((sym
= *symp
)) {
970 if((sym
->type
& N_EXT
) == 0) {
971 sym
->strx
= *strtabsize
;
972 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
973 *strtabsize
+= strlen(sym
->name
) + 1;
976 if((sym
->type
& N_TYPE
) != N_UNDF
) {
977 extdefsyms
[i
++] = sym
;
979 undefsyms
[j
++] = sym
;
985 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
986 (int (*)(const void *, const void *))layout_compare
);
987 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
988 (int (*)(const void *, const void *))layout_compare
);
990 for(i
= 0; i
< nextdefsym
; i
++) {
991 extdefsyms
[i
]->snum
= *numsyms
;
994 for(j
= 0; j
< nundefsym
; j
++) {
995 undefsyms
[j
]->snum
= *numsyms
;
1000 /* Calculate some values we'll need for writing later. */
1002 static void macho_calculate_sizes (void)
1007 /* count sections and calculate in-memory and in-file offsets */
1008 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1011 /* recalculate segment address based on alignment and vm size */
1012 s
->addr
= seg_vmsize
;
1014 /* we need section alignment to calculate final section address */
1016 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
1018 newaddr
= ALIGN(s
->addr
, 1 << s
->align
);
1021 seg_vmsize
= newaddr
+ s
->size
;
1023 /* zerofill sections aren't actually written to the file */
1024 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
1026 * LLVM/Xcode as always aligns the section data to 4
1027 * bytes; there is a comment in the LLVM source code that
1028 * perhaps aligning to pointer size would be better.
1030 s
->pad
= ALIGN(seg_filesize
, 4) - seg_filesize
;
1031 s
->offset
= seg_filesize
+ s
->pad
;
1032 seg_filesize
+= s
->size
+ s
->pad
;
1038 /* calculate size of all headers, load commands and sections to
1039 ** get a pointer to the start of all the raw data */
1040 if (seg_nsects
> 0) {
1042 head_sizeofcmds
+= fmt
->segcmd_size
+ seg_nsects
* fmt
->sectcmd_size
;
1047 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
1050 /* Create a table of sections by file index to avoid linear search */
1051 sectstab
= nasm_malloc((seg_nsects
+ 1) * sizeof(*sectstab
));
1053 for (s
= sects
, fi
= 1; s
!= NULL
; s
= s
->next
, fi
++)
1057 /* Write out the header information for the file. */
1059 static void macho_write_header (void)
1061 fwriteint32_t(fmt
->mh_magic
, ofile
); /* magic */
1062 fwriteint32_t(fmt
->cpu_type
, ofile
); /* CPU type */
1063 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, ofile
); /* CPU subtype */
1064 fwriteint32_t(MH_OBJECT
, ofile
); /* Mach-O file type */
1065 fwriteint32_t(head_ncmds
, ofile
); /* number of load commands */
1066 fwriteint32_t(head_sizeofcmds
, ofile
); /* size of load commands */
1067 fwriteint32_t(0, ofile
); /* no flags */
1068 fwritezero(fmt
->header_size
- 7*4, ofile
); /* reserved fields */
1071 /* Write out the segment load command at offset. */
1073 static uint32_t macho_write_segment (uint64_t offset
)
1075 uint64_t rel_base
= alignptr(offset
+ seg_filesize
);
1076 uint32_t s_reloff
= 0;
1079 fwriteint32_t(fmt
->lc_segment
, ofile
); /* cmd == LC_SEGMENT_64 */
1081 /* size of load command including section load commands */
1082 fwriteint32_t(fmt
->segcmd_size
+ seg_nsects
* fmt
->sectcmd_size
,
1085 /* in an MH_OBJECT file all sections are in one unnamed (name
1086 ** all zeros) segment */
1087 fwritezero(16, ofile
);
1088 fwriteptr(0, ofile
); /* in-memory offset */
1089 fwriteptr(seg_vmsize
, ofile
); /* in-memory size */
1090 fwriteptr(offset
, ofile
); /* in-file offset to data */
1091 fwriteptr(seg_filesize
, ofile
); /* in-file size */
1092 fwriteint32_t(VM_PROT_DEFAULT
, ofile
); /* maximum vm protection */
1093 fwriteint32_t(VM_PROT_DEFAULT
, ofile
); /* initial vm protection */
1094 fwriteint32_t(seg_nsects
, ofile
); /* number of sections */
1095 fwriteint32_t(0, ofile
); /* no flags */
1097 /* emit section headers */
1098 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1099 nasm_write(s
->sectname
, sizeof(s
->sectname
), ofile
);
1100 nasm_write(s
->segname
, sizeof(s
->segname
), ofile
);
1101 fwriteptr(s
->addr
, ofile
);
1102 fwriteptr(s
->size
, ofile
);
1104 /* dummy data for zerofill sections or proper values */
1105 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
1106 nasm_assert(s
->pad
!= (uint32_t)-1);
1108 fwriteint32_t(offset
, ofile
);
1110 /* Write out section alignment, as a power of two.
1111 e.g. 32-bit word alignment would be 2 (2^2 = 4). */
1112 fwriteint32_t(s
->align
, ofile
);
1113 /* To be compatible with cctools as we emit
1114 a zero reloff if we have no relocations. */
1115 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, ofile
);
1116 fwriteint32_t(s
->nreloc
, ofile
);
1118 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
1120 fwriteint32_t(0, ofile
);
1121 fwriteint32_t(s
->align
, ofile
);
1122 fwriteint32_t(0, ofile
);
1123 fwriteint32_t(0, ofile
);
1127 s
->flags
|= S_ATTR_LOC_RELOC
;
1129 s
->flags
|= S_ATTR_EXT_RELOC
;
1132 fwriteint32_t(s
->flags
, ofile
); /* flags */
1133 fwriteint32_t(0, ofile
); /* reserved */
1134 fwriteptr(0, ofile
); /* reserved */
1137 rel_padcnt
= rel_base
- offset
;
1138 offset
= rel_base
+ s_reloff
;
1143 /* For a given chain of relocs r, write out the entire relocation
1144 chain to the object file. */
1146 static void macho_write_relocs (struct reloc
*r
)
1151 fwriteint32_t(r
->addr
, ofile
); /* reloc offset */
1154 word2
|= r
->pcrel
<< 24;
1155 word2
|= r
->length
<< 25;
1156 word2
|= r
->ext
<< 27;
1157 word2
|= r
->type
<< 28;
1158 fwriteint32_t(word2
, ofile
); /* reloc data */
1163 /* Write out the section data. */
1164 static void macho_write_section (void)
1166 struct section
*s
, *s2
;
1176 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1177 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
1180 /* Like a.out Mach-O references things in the data or bss
1181 * sections by addresses which are actually relative to the
1182 * start of the _text_ section, in the _file_. See outaout.c
1183 * for more information. */
1184 saa_rewind(s
->data
);
1185 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
1186 len
= (uint32_t)1 << r
->length
;
1187 if (len
> 4) /* Can this ever be an issue?! */
1190 saa_fread(s
->data
, r
->addr
, blk
.buf
, len
);
1192 /* get offset based on relocation type */
1193 #ifdef WORDS_LITTLEENDIAN
1197 l
+= ((int64_t)blk
.buf
[1]) << 8;
1198 l
+= ((int64_t)blk
.buf
[2]) << 16;
1199 l
+= ((int64_t)blk
.buf
[3]) << 24;
1200 l
+= ((int64_t)blk
.buf
[4]) << 32;
1201 l
+= ((int64_t)blk
.buf
[5]) << 40;
1202 l
+= ((int64_t)blk
.buf
[6]) << 48;
1203 l
+= ((int64_t)blk
.buf
[7]) << 56;
1206 /* If the relocation is internal add to the current section
1207 offset. Otherwise the only value we need is the symbol
1208 offset which we already have. The linker takes care
1209 of the rest of the address. */
1211 /* generate final address by section address and offset */
1212 for (s2
= sects
, fi
= 1;
1213 s2
!= NULL
; s2
= s2
->next
, fi
++) {
1214 if (fi
== r
->snum
) {
1221 /* write new offset back */
1224 saa_fwrite(s
->data
, r
->addr
, blk
.buf
, len
);
1227 /* dump the section data to file */
1228 fwritezero(s
->pad
, ofile
);
1229 saa_fpwrite(s
->data
, ofile
);
1232 /* pad last section up to reloc entries on pointer boundary */
1233 fwritezero(rel_padcnt
, ofile
);
1235 /* emit relocation entries */
1236 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1237 macho_write_relocs (s
->relocs
);
1240 /* Write out the symbol table. We should already have sorted this
1242 static void macho_write_symtab (void)
1247 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1249 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1250 if ((sym
->type
& N_EXT
) == 0) {
1251 fwriteint32_t(sym
->strx
, ofile
); /* string table entry number */
1252 nasm_write(&sym
->type
, 1, ofile
); /* symbol type */
1253 nasm_write(&sym
->sect
, 1, ofile
); /* section */
1254 fwriteint16_t(sym
->desc
, ofile
); /* description */
1256 /* Fix up the symbol value now that we know the final section
1258 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1259 nasm_assert(sym
->sect
<= seg_nsects
);
1260 sym
->value
+= sectstab
[sym
->sect
]->addr
;
1263 fwriteptr(sym
->value
, ofile
); /* value (i.e. offset) */
1267 for (i
= 0; i
< nextdefsym
; i
++) {
1268 sym
= extdefsyms
[i
];
1269 fwriteint32_t(sym
->strx
, ofile
);
1270 nasm_write(&sym
->type
, 1, ofile
); /* symbol type */
1271 nasm_write(&sym
->sect
, 1, ofile
); /* section */
1272 fwriteint16_t(sym
->desc
, ofile
); /* description */
1274 /* Fix up the symbol value now that we know the final section
1276 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1277 nasm_assert(sym
->sect
<= seg_nsects
);
1278 sym
->value
+= sectstab
[sym
->sect
]->addr
;
1281 fwriteptr(sym
->value
, ofile
); /* value (i.e. offset) */
1284 for (i
= 0; i
< nundefsym
; i
++) {
1286 fwriteint32_t(sym
->strx
, ofile
);
1287 nasm_write(&sym
->type
, 1, ofile
); /* symbol type */
1288 nasm_write(&sym
->sect
, 1, ofile
); /* section */
1289 fwriteint16_t(sym
->desc
, ofile
); /* description */
1291 /* Fix up the symbol value now that we know the final section
1293 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1294 nasm_assert(sym
->sect
<= seg_nsects
);
1295 sym
->value
+= sectstab
[sym
->sect
]->addr
;
1298 fwriteptr(sym
->value
, ofile
); /* value (i.e. offset) */
1303 /* Fixup the snum in the relocation entries, we should be
1304 doing this only for externally referenced symbols. */
1305 static void macho_fixup_relocs (struct reloc
*r
)
1311 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1312 if (sym
->initial_snum
== r
->snum
) {
1313 r
->snum
= sym
->snum
;
1322 /* Write out the object file. */
1324 static void macho_write (void)
1326 uint64_t offset
= 0;
1328 /* mach-o object file structure:
1334 ** uint32_t mach file type
1335 ** uint32_t number of load commands
1336 ** uint32_t size of all load commands
1337 ** (includes section struct size of segment command)
1341 ** uint32_t command type == LC_SEGMENT[_64]
1342 ** uint32_t size of load command
1343 ** (including section load commands)
1344 ** char[16] segment name
1345 ** pointer in-memory offset
1346 ** pointer in-memory size
1347 ** pointer in-file offset to data area
1348 ** pointer in-file size
1349 ** (in-memory size excluding zerofill sections)
1350 ** int maximum vm protection
1351 ** int initial vm protection
1352 ** uint32_t number of sections
1356 ** char[16] section name
1357 ** char[16] segment name
1358 ** pointer in-memory offset
1359 ** pointer in-memory size
1360 ** uint32_t in-file offset
1361 ** uint32_t alignment
1362 ** (irrelevant in MH_OBJECT)
1363 ** uint32_t in-file offset of relocation entires
1364 ** uint32_t number of relocations
1366 ** uint32_t reserved
1367 ** uint32_t reserved
1369 ** symbol table command
1370 ** uint32_t command type == LC_SYMTAB
1371 ** uint32_t size of load command
1372 ** uint32_t symbol table offset
1373 ** uint32_t number of symbol table entries
1374 ** uint32_t string table offset
1375 ** uint32_t string table size
1379 ** padding to pointer boundary
1381 ** relocation data (struct reloc)
1383 ** uint data (symbolnum, pcrel, length, extern, type)
1385 ** symbol table data (struct nlist)
1386 ** int32_t string table entry number
1388 ** (extern, absolute, defined in section)
1390 ** (0 for global symbols, section number of definition (>= 1, <=
1391 ** 254) for local symbols, size of variable for common symbols
1392 ** [type == extern])
1393 ** int16_t description
1394 ** (for stab debugging format)
1395 ** pointer value (i.e. file offset) of symbol or stab offset
1397 ** string table data
1398 ** list of null-terminated strings
1401 /* Emit the Mach-O header. */
1402 macho_write_header();
1404 offset
= fmt
->header_size
+ head_sizeofcmds
;
1406 /* emit the segment load command */
1408 offset
= macho_write_segment (offset
);
1410 nasm_error(ERR_WARNING
, "no sections?");
1413 /* write out symbol command */
1414 fwriteint32_t(LC_SYMTAB
, ofile
); /* cmd == LC_SYMTAB */
1415 fwriteint32_t(MACHO_SYMCMD_SIZE
, ofile
); /* size of load command */
1416 fwriteint32_t(offset
, ofile
); /* symbol table offset */
1417 fwriteint32_t(nsyms
, ofile
); /* number of symbol
1419 offset
+= nsyms
* fmt
->nlist_size
;
1420 fwriteint32_t(offset
, ofile
); /* string table offset */
1421 fwriteint32_t(strslen
, ofile
); /* string table size */
1424 /* emit section data */
1426 macho_write_section ();
1428 /* emit symbol table if we have symbols */
1430 macho_write_symtab ();
1432 /* we don't need to pad here, we are already aligned */
1434 /* emit string table */
1435 saa_fpwrite(strs
, ofile
);
1437 /* We do quite a bit here, starting with finalizing all of the data
1438 for the object file, writing, and then freeing all of the data from
1441 static void macho_cleanup(int debuginfo
)
1449 /* Sort all symbols. */
1450 macho_layout_symbols (&nsyms
, &strslen
);
1452 /* Fixup relocation entries */
1453 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1454 macho_fixup_relocs (s
->relocs
);
1457 /* First calculate and finalize needed values. */
1458 macho_calculate_sizes();
1461 /* free up everything */
1462 while (sects
->next
) {
1464 sects
= sects
->next
;
1467 while (s
->relocs
!= NULL
) {
1469 s
->relocs
= s
->relocs
->next
;
1485 nasm_free(extdefsyms
);
1486 nasm_free(undefsyms
);
1487 nasm_free(sectstab
);
1490 /* Debugging routines. */
1491 static void debug_reloc (struct reloc
*r
)
1493 fprintf (stdout
, "reloc:\n");
1494 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1495 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1496 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1497 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1498 fprintf (stdout
, "\text: %d\n", r
->ext
);
1499 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1502 static void debug_section_relocs (struct section
*s
)
1504 struct reloc
*r
= s
->relocs
;
1506 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1515 static void macho32_init(void)
1520 macho_gotpcrel_sect
= NO_SEG
;
1523 struct ofmt of_macho32
= {
1524 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1545 static void macho64_init(void)
1550 /* add special symbol for ..gotpcrel */
1551 macho_gotpcrel_sect
= seg_alloc();
1552 macho_gotpcrel_sect
++;
1553 define_label("..gotpcrel", macho_gotpcrel_sect
, 0L, NULL
, false, false);
1556 struct ofmt of_macho64
= {
1557 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",