1 /* outmacho64.c output routines for the Netwide Assembler to produce
2 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
10 /* Most of this file is, like Mach-O itself, based on a.out. For more
11 * guidelines see outaout.c. */
30 /* Mach-O in-file header structure sizes */
31 #define MACHO_HEADER64_SIZE (32)
32 #define MACHO_SEGCMD64_SIZE (72)
33 #define MACHO_SECTCMD64_SIZE (80)
34 #define MACHO_SYMCMD_SIZE (24)
35 #define MACHO_NLIST64_SIZE (16)
36 #define MACHO_RELINFO64_SIZE (8)
38 /* Mach-O file header values */
39 #define MH_MAGIC_64 (0xfeedfacf)
40 #define CPU_TYPE_X86_64 (0x01000007) /* x86-64 platform */
41 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
42 #define MH_OBJECT (0x1) /* object file */
44 #define LC_SEGMENT_64 (0x19) /* segment load command */
45 #define LC_SYMTAB (0x2) /* symbol table load command */
47 #define VM_PROT_NONE (0x00)
48 #define VM_PROT_READ (0x01)
49 #define VM_PROT_WRITE (0x02)
50 #define VM_PROT_EXECUTE (0x04)
52 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
53 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
56 /* nasm internal data */
63 /* data that goes into the file */
64 char sectname
[16]; /* what this section is called */
65 char segname
[16]; /* segment this section will be in */
66 uint64_t size
; /* in-memory and -file size */
67 uint32_t nreloc
; /* relocation entry count */
68 uint32_t flags
; /* type and attributes (masked) */
69 uint32_t extreloc
; /* external relocations */
72 #define SECTION_TYPE 0x000000ff /* section type mask */
74 #define S_REGULAR (0x0) /* standard section */
75 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
77 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
78 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
79 machine instructions */
80 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
82 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
84 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure
85 machine instructions */
87 static struct sectmap
{
93 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
|S_ATTR_PURE_INSTRUCTIONS
},
94 {".data", "__DATA", "__data", S_REGULAR
},
95 {".rodata", "__DATA", "__const", S_REGULAR
},
96 {".bss", "__DATA", "__bss", S_ZEROFILL
},
101 /* nasm internal data */
104 /* data that goes into the file */
105 int32_t addr
; /* op's offset in section */
106 uint32_t snum
:24, /* contains symbol index if
107 ** ext otherwise in-file
109 pcrel
:1, /* relative relocation */
110 length
:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
111 ext
:1, /* external symbol referenced */
112 type
:4; /* reloc type */
115 #define R_ABS 0 /* absolute relocation */
116 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
117 ** highest bit == 1 */
120 /* nasm internal data */
121 struct symbol
*next
; /* next symbol in the list */
122 char *name
; /* name of this symbol */
123 int32_t initial_snum
; /* symbol number used above in
125 int32_t snum
; /* true snum for reloc */
127 /* data that goes into the file */
128 uint32_t strx
; /* string table index */
129 uint8_t type
; /* symbol type */
130 uint8_t sect
; /* NO_SECT or section number */
131 int16_t desc
; /* for stab debugging, 0 for us */
132 uint64_t value
; /* offset of symbol in section */
135 /* symbol type bits */
136 #define N_EXT 0x01 /* global or external symbol */
138 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
139 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
140 #define N_SECT 0xe /* defined symbol, n_sect holds
143 #define N_TYPE 0x0e /* type bit mask */
145 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
147 /* special section number values */
148 #define NO_SECT 0 /* no section, invalid */
149 #define MAX_SECT 255 /* maximum number of sections */
151 static struct section
*sects
, **sectstail
;
152 static struct symbol
*syms
, **symstail
;
153 static uint32_t nsyms
;
155 /* These variables are set by macho_layout_symbols() to organize
156 the symbol table and string table in order the dynamic linker
157 expects. They are then used in macho_write() to put out the
158 symbols and strings in that order.
160 The order of the symbol table is:
162 defined external symbols (sorted by name)
163 undefined external symbols (sorted by name)
165 The order of the string table is:
166 strings for external symbols
167 strings for local symbols
169 static uint32_t ilocalsym
= 0;
170 static uint32_t iextdefsym
= 0;
171 static uint32_t iundefsym
= 0;
172 static uint32_t nlocalsym
;
173 static uint32_t nextdefsym
;
174 static uint32_t nundefsym
;
175 static struct symbol
**extdefsyms
= NULL
;
176 static struct symbol
**undefsyms
= NULL
;
178 static struct RAA
*extsyms
;
179 static struct SAA
*strs
;
180 static uint32_t strslen
;
182 static FILE *machofp
;
184 static evalfunc evaluate
;
186 extern struct ofmt of_macho64
;
188 /* Global file information. This should be cleaned up into either
189 a structure or as function arguments. */
190 uint32_t head_ncmds64
= 0;
191 uint32_t head_sizeofcmds64
= 0;
192 uint64_t seg_filesize64
= 0;
193 uint64_t seg_vmsize64
= 0;
194 uint32_t seg_nsects64
= 0;
195 uint64_t rel_padcnt64
= 0;
198 #define xstrncpy(xdst, xsrc) \
199 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
200 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
201 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
203 #define align(x, y) \
204 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
206 #define alignint32_t(x) \
207 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
209 #define alignint64_t(x) \
210 align(x, sizeof(int64_t)) /* align x to int64_t boundary */
212 static void debug_reloc (struct reloc
*);
213 static void debug_section_relocs (struct section
*) _unused
;
215 static int exact_log2 (uint32_t align
)
219 } else if (align
& (align
-1)) {
220 return -1; /* Not a power of 2 */
223 return __builtin_ctzl (align
);
227 /* We know exactly one bit is set at this point. */
228 if (align
& 0xffff0000)
230 if (align
& 0xff00ff00)
232 if (align
& 0xf0f0f0f0)
234 if (align
& 0xcccccccc)
236 if (align
& 0xaaaaaaaa)
244 static struct section
*get_section_by_name(const char *segname
,
245 const char *sectname
)
249 for (s
= sects
; s
!= NULL
; s
= s
->next
)
250 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
256 static struct section
*get_section_by_index(const int32_t index
)
260 for (s
= sects
; s
!= NULL
; s
= s
->next
)
261 if (index
== s
->index
)
267 static int32_t get_section_index_by_name(const char *segname
,
268 const char *sectname
)
272 for (s
= sects
; s
!= NULL
; s
= s
->next
)
273 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
279 static char *get_section_name_by_index(const int32_t index
)
283 for (s
= sects
; s
!= NULL
; s
= s
->next
)
284 if (index
== s
->index
)
290 static uint8_t get_section_fileindex_by_index(const int32_t index
)
295 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
296 if (index
== s
->index
)
301 "too many sections (>255) - clipped by fileindex");
307 * Special section numbers which are used to define Mach-O special
308 * symbols, which can be used with WRT to provide PIC relocation
311 static int32_t macho_gotpcrel_sect
;
313 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
323 (void)ldef
; /* placate optimizers */
335 extsyms
= raa_init();
338 /* string table starts with a zero byte - don't ask why */
339 saa_wbytes(strs
, &zero
, sizeof(char));
342 /* add special symbol for ..gotpcrel */
343 macho_gotpcrel_sect
= seg_alloc();
344 macho_gotpcrel_sect
++;
345 ldef("..gotpcrel", macho_gotpcrel_sect
, 0L, NULL
, false, false, &of_macho64
, error
);
348 static int macho_setinfo(enum geninfo type
, char **val
)
355 static void sect_write(struct section
*sect
,
356 const uint8_t *data
, uint32_t len
)
358 saa_wbytes(sect
->data
, data
, len
);
362 static void add_reloc(struct section
*sect
, int32_t section
,
363 int pcrel
, int bytes
)
368 /* NeXT as puts relocs in reversed order (address-wise) into the
369 ** files, so we do the same, doesn't seem to make much of a
370 ** difference either way */
371 r
= nasm_malloc(sizeof(struct reloc
));
372 r
->next
= sect
->relocs
;
375 /* the current end of the section will be the symbol's address for
376 ** now, might have to be fixed by macho_fixup_relocs() later on. make
377 ** sure we don't make the symbol scattered by setting the highest
378 ** bit by accident */
379 r
->addr
= sect
->size
& ~R_SCATTERED
;
381 r
->pcrel
= (pcrel
? 1 : 0);
383 /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
401 /* absolute relocation */
402 r
->type
= 0; // X86_64_RELOC_UNSIGNED
403 r
->snum
= R_ABS
; // Absolute Symbol (indicates no relocation)
405 /* relative relocation */
408 // r->type = 2; // X86_64_RELOC_BRANCH
411 if (section
== NO_SEG
) {
412 r
->type
= 1; // X86_64_RELOC_SIGNED
416 fi
= get_section_fileindex_by_index(section
);
422 r
->pcrel
= 0; // presumed X86_64_RELOC_UNSIGNED ???
423 // r->snum = raa_read(extsyms, section);
428 r
->type
= 2; // X86_64_RELOC_BRANCH
434 } else if (pcrel
== 2) {
436 r
->type
= 5; // X86_64_RELOC_SUBTRACTOR
437 // r->snum = macho_gotpcrel_sect;
440 } else if (pcrel
== 3) {
441 r
->type
= 4; // X86_64_RELOC_GOT
446 static void macho_output(int32_t secto
, const void *data
,
447 enum out_type type
, uint64_t size
,
448 int32_t section
, int32_t wrt
)
450 struct section
*s
, *sbss
;
452 uint8_t mydata
[16], *p
;
454 if (secto
== NO_SEG
) {
455 if (type
!= OUT_RESERVE
)
456 error(ERR_NONFATAL
, "attempt to assemble code in "
462 s
= get_section_by_index(secto
);
465 error(ERR_WARNING
, "attempt to assemble code in"
466 " section %d: defaulting to `.text'", secto
);
467 s
= get_section_by_name("__TEXT", "__text");
469 /* should never happen */
471 error(ERR_PANIC
, "text section not found");
474 sbss
= get_section_by_name("__DATA", "__bss");
476 if (s
== sbss
&& type
!= OUT_RESERVE
) {
477 error(ERR_WARNING
, "attempt to initialize memory in the"
478 " BSS section: ignored");
500 error(ERR_WARNING
, "uninitialized space declared in"
501 " %s section: zeroing",
502 get_section_name_by_index(secto
));
504 sect_write(s
, NULL
, size
);
511 if (section
!= NO_SEG
)
512 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
514 sect_write(s
, data
, size
);
518 addr
= *(int64_t *)data
;
520 if (section
!= NO_SEG
) {
522 error(ERR_NONFATAL
, "Mach-O format does not support"
523 " section base references");
527 add_reloc(s
, section
, 2, size
);
529 add_reloc(s
, section
, 0, size
);
530 } else if (wrt
== macho_gotpcrel_sect
) {
532 add_reloc(s
, section
, 3, size
);
534 error(ERR_NONFATAL
, "Mach-O format does not support"
541 WRITEADDR(p
, addr
, size
);
542 sect_write(s
, mydata
, size
);
546 if (section
== secto
)
547 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
549 if (section
!= NO_SEG
&& section
% 2) {
550 error(ERR_NONFATAL
, "Mach-O format does not support"
551 " section base references");
554 add_reloc(s
, section
, 2, 2);
555 add_reloc(s
, section
, 0, 2);
557 error(ERR_NONFATAL
, "Unsupported non-32-bit"
558 " Macho-O relocation [2]");
563 WRITESHORT(p
, *(int64_t *)data
- size
);
564 sect_write(s
, mydata
, 2L);
568 if (section
== secto
)
569 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
571 if (section
!= NO_SEG
&& section
% 2) {
572 error(ERR_NONFATAL
, "Mach-O format does not support"
573 " section base references");
577 //add_reloc(s, section, 2, 4);
578 add_reloc(s
, section
, 1, 4);
579 } else if (wrt
== macho_gotpcrel_sect
) {
580 error(ERR_NONFATAL
, "Mach-O format cannot produce PC-"
581 "relative GOT references");
583 error(ERR_NONFATAL
, "Mach-O format does not support"
585 wrt
= NO_SEG
; /* we can at least _try_ to continue */
590 WRITELONG(p
, *(int64_t *)data
- size
);
591 sect_write(s
, mydata
, 4L);
595 error(ERR_PANIC
, "unknown output type?");
600 static int32_t macho_section(char *name
, int pass
, int *bits
)
602 int32_t index
, originalIndex
;
603 char *sectionAttributes
;
609 /* Default to 64 bits. */
613 sectionAttributes
= NULL
;
615 sectionAttributes
= name
;
616 name
= nasm_strsep(§ionAttributes
, " \t");
619 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
620 /* make lookup into section name translation table */
621 if (!strcmp(name
, sm
->nasmsect
)) {
622 char *currentAttribute
;
624 /* try to find section with that name */
625 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
628 /* create it if it doesn't exist yet */
630 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
632 sectstail
= &s
->next
;
634 s
->data
= saa_init(1L);
635 s
->index
= seg_alloc();
639 xstrncpy(s
->segname
, sm
->segname
);
640 xstrncpy(s
->sectname
, sm
->sectname
);
643 s
->flags
= sm
->flags
;
647 s
= get_section_by_index(index
);
650 while ((NULL
!= sectionAttributes
)
651 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
652 if (0 != *currentAttribute
) {
653 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
655 int newAlignment
, value
;
657 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
658 newAlignment
= exact_log2(value
);
662 "unknown or missing alignment value \"%s\" "
663 "specified for section \"%s\"",
664 currentAttribute
+ 6,
667 } else if (0 > newAlignment
) {
669 "alignment of %d (for section \"%s\") is not "
676 if ((-1 != originalIndex
)
677 && (s
->align
!= newAlignment
)
678 && (s
->align
!= -1)) {
680 "section \"%s\" has already been specified "
681 "with alignment %d, conflicts with new "
689 s
->align
= newAlignment
;
690 } else if (!nasm_stricmp("data", currentAttribute
)) {
691 /* Do nothing; 'data' is implicit */
694 "unknown section attribute %s for section %s",
706 error(ERR_PANIC
, "invalid section name %s", name
);
710 static void macho_symdef(char *name
, int32_t section
, int64_t offset
,
711 int is_global
, char *special
)
716 error(ERR_NONFATAL
, "The Mach-O output format does "
717 "not support any special symbol types");
721 if (is_global
== 3) {
722 error(ERR_NONFATAL
, "The Mach-O format does not "
723 "(yet) support forward reference fixups.");
727 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
729 symstail
= &sym
->next
;
736 sym
->initial_snum
= -1;
738 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
740 * This is a NASM special symbol. We never allow it into
741 * the Macho-O symbol table, even if it's a valid one. If it
742 * _isn't_ a valid one, we should barf immediately.
744 if (strcmp(name
, "..gotpcrel"))
745 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
749 /* external and common symbols get N_EXT */
750 if (is_global
!= 0) {
754 if (section
== NO_SEG
) {
755 /* symbols in no section get absolute */
761 /* get the in-file index of the section the symbol was defined in */
762 sym
->sect
= get_section_fileindex_by_index(section
);
764 if (sym
->sect
== NO_SECT
) {
765 /* remember symbol number of references to external
766 ** symbols, this works because every external symbol gets
767 ** its own section number allocated internally by nasm and
768 ** can so be used as a key */
769 extsyms
= raa_write(extsyms
, section
, nsyms
);
770 sym
->initial_snum
= nsyms
;
775 /* there isn't actually a difference between global
776 ** and common symbols, both even have their size in
782 /* give an error on unfound section if it's not an
783 ** external or common symbol (assemble_file() does a
784 ** seg_alloc() on every call for them) */
785 error(ERR_PANIC
, "in-file index for section %d not found",
793 static int32_t macho_segbase(int32_t section
)
798 static int macho_directive(char *directive
, char *value
, int pass
)
806 static void macho_filename(char *inname
, char *outname
, efunc error
)
808 standard_extension(inname
, outname
, ".o", error
);
811 extern macros_t generic_stdmac
[];
813 /* Comparison function for qsort symbol layout. */
814 static int layout_compare (const struct symbol
**s1
,
815 const struct symbol
**s2
)
817 return (strcmp ((*s1
)->name
, (*s2
)->name
));
820 /* The native assembler does a few things in a similar function
822 * Remove temporary labels
823 * Sort symbols according to local, external, undefined (by name)
824 * Order the string table
826 We do not remove temporary labels right now.
828 numsyms is the total number of symbols we have. strtabsize is the
829 number entries in the string table. */
831 static void macho_layout_symbols (uint32_t *numsyms
,
832 uint32_t *strtabsize
)
834 struct symbol
*sym
, **symp
;
838 *strtabsize
= sizeof (char);
842 while ((sym
= *symp
)) {
843 /* Undefined symbols are now external. */
844 if (sym
->type
== N_UNDF
)
847 if ((sym
->type
& N_EXT
) == 0) {
848 sym
->snum
= *numsyms
;
849 *numsyms
= *numsyms
+ 1;
853 if ((sym
->type
& N_TYPE
) != N_UNDF
)
858 /* If we handle debug info we'll want
859 to check for it here instead of just
860 adding the symbol to the string table. */
861 sym
->strx
= *strtabsize
;
862 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
863 *strtabsize
+= strlen(sym
->name
) + 1;
868 /* Next, sort the symbols. Most of this code is a direct translation from
869 the Apple cctools symbol layout. We need to keep compatibility with that. */
870 /* Set the indexes for symbol groups into the symbol table */
872 iextdefsym
= nlocalsym
;
873 iundefsym
= nlocalsym
+ nextdefsym
;
875 /* allocate arrays for sorting externals by name */
876 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
877 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
884 while ((sym
= *symp
)) {
886 if((sym
->type
& N_EXT
) == 0) {
887 sym
->strx
= *strtabsize
;
888 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
889 *strtabsize
+= strlen(sym
->name
) + 1;
892 if((sym
->type
& N_TYPE
) != N_UNDF
)
893 extdefsyms
[i
++] = sym
;
895 undefsyms
[j
++] = sym
;
900 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
901 (int (*)(const void *, const void *))layout_compare
);
902 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
903 (int (*)(const void *, const void *))layout_compare
);
905 for(i
= 0; i
< nextdefsym
; i
++) {
906 extdefsyms
[i
]->snum
= *numsyms
;
909 for(j
= 0; j
< nundefsym
; j
++) {
910 undefsyms
[j
]->snum
= *numsyms
;
915 /* Calculate some values we'll need for writing later. */
917 static void macho_calculate_sizes (void)
921 /* count sections and calculate in-memory and in-file offsets */
922 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
923 /* zerofill sections aren't actually written to the file */
924 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
925 seg_filesize64
+= s
->size
;
927 seg_vmsize64
+= s
->size
;
931 /* calculate size of all headers, load commands and sections to
932 ** get a pointer to the start of all the raw data */
933 if (seg_nsects64
> 0) {
936 MACHO_SEGCMD64_SIZE
+ seg_nsects64
* MACHO_SECTCMD64_SIZE
;
941 head_sizeofcmds64
+= MACHO_SYMCMD_SIZE
;
945 /* Write out the header information for the file. */
947 static void macho_write_header (void)
949 fwriteint32_t(MH_MAGIC_64
, machofp
); /* magic */
950 fwriteint32_t(CPU_TYPE_X86_64
, machofp
); /* CPU type */
951 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
952 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
953 fwriteint32_t(head_ncmds64
, machofp
); /* number of load commands */
954 fwriteint32_t(head_sizeofcmds64
, machofp
); /* size of load commands */
955 fwriteint32_t(0, machofp
); /* no flags */
956 fwriteint32_t(0, machofp
); /* reserved for future use */
959 /* Write out the segment load command at offset. */
961 static uint32_t macho_write_segment (uint64_t offset
)
964 uint64_t rel_base
= alignint64_t (offset
+ seg_filesize64
);
965 uint32_t s_reloff
= 0;
968 fwriteint32_t(LC_SEGMENT_64
, machofp
); /* cmd == LC_SEGMENT_64 */
970 /* size of load command including section load commands */
971 fwriteint32_t(MACHO_SEGCMD64_SIZE
+ seg_nsects64
*
972 MACHO_SECTCMD64_SIZE
, machofp
);
974 /* in an MH_OBJECT file all sections are in one unnamed (name
975 ** all zeros) segment */
976 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp
);
977 fwriteint64_t(0, machofp
); /* in-memory offset */
978 fwriteint64_t(seg_vmsize64
, machofp
); /* in-memory size */
979 fwriteint64_t(offset
, machofp
); /* in-file offset to data */
980 fwriteint64_t(seg_filesize64
, machofp
); /* in-file size */
981 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
982 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
983 fwriteint32_t(seg_nsects64
, machofp
); /* number of sections */
984 fwriteint32_t(0, machofp
); /* no flags */
986 /* emit section headers */
987 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
988 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
989 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
990 fwriteint64_t(s_addr
, machofp
);
991 fwriteint64_t(s
->size
, machofp
);
993 /* dummy data for zerofill sections or proper values */
994 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
995 fwriteint32_t(offset
, machofp
);
996 /* Write out section alignment, as a power of two.
997 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
999 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
1000 fwriteint32_t(s
->align
, machofp
);
1001 /* To be compatible with cctools as we emit
1002 a zero reloff if we have no relocations. */
1003 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
1004 fwriteint32_t(s
->nreloc
, machofp
);
1007 s_reloff
+= s
->nreloc
* MACHO_RELINFO64_SIZE
;
1009 fwriteint32_t(0, machofp
);
1010 fwriteint32_t(0, machofp
);
1011 fwriteint32_t(0, machofp
);
1012 fwriteint32_t(0, machofp
);
1016 s
->flags
|= S_ATTR_LOC_RELOC
;
1018 s
->flags
|= S_ATTR_EXT_RELOC
;
1020 fwriteint32_t(s
->flags
, machofp
); /* flags */
1021 fwriteint32_t(0, machofp
); /* reserved */
1022 fwriteint32_t(0, machofp
); /* reserved */
1024 fwriteint32_t(0, machofp
); /* align */
1028 rel_padcnt64
= rel_base
- offset
;
1029 offset
= rel_base
+ s_reloff
;
1034 /* For a given chain of relocs r, write out the entire relocation
1035 chain to the object file. */
1037 static void macho_write_relocs (struct reloc
*r
)
1042 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
1045 word2
|= r
->pcrel
<< 24;
1046 word2
|= r
->length
<< 25;
1047 word2
|= r
->ext
<< 27;
1048 word2
|= r
->type
<< 28;
1049 fwriteint32_t(word2
, machofp
); /* reloc data */
1054 /* Write out the section data. */
1055 static void macho_write_section (void)
1057 struct section
*s
, *s2
;
1059 char *rel_paddata
= "\0\0\0\0\0\0\0";
1060 uint8_t fi
, *p
, *q
, blk
[8];
1064 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1065 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
1068 /* no padding needs to be done to the sections */
1070 /* Like a.out Mach-O references things in the data or bss
1071 * sections by addresses which are actually relative to the
1072 * start of the _text_ section, in the _file_. See outaout.c
1073 * for more information. */
1074 saa_rewind(s
->data
);
1075 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
1076 len
= (int32_t)r
->length
<< 1;
1077 if(len
> 4) len
= 8;
1078 saa_fread(s
->data
, r
->addr
, blk
, len
);
1082 /* get offset based on relocation type */
1083 if (r
->length
> 0) {
1084 l
+= ((int64_t)*p
++) << 8;
1086 if (r
->length
> 1) {
1087 l
+= ((int64_t)*p
++) << 16;
1088 l
+= ((int64_t)*p
++) << 24;
1091 if (r
->length
> 2) {
1092 l
+= ((int64_t)*p
++) << 32;
1093 l
+= ((int64_t)*p
++) << 40;
1094 l
+= ((int64_t)*p
++) << 48;
1095 l
+= ((int64_t)*p
++) << 56;
1101 /* If the relocation is internal add to the current section
1102 offset. Otherwise the only value we need is the symbol
1103 offset which we already have. The linker takes care
1104 of the rest of the address. */
1106 /* add sizes of previous sections to current offset */
1107 for (s2
= sects
, fi
= 1;
1108 s2
!= NULL
&& fi
< r
->snum
; s2
= s2
->next
, fi
++)
1112 /* write new offset back */
1115 else if (r
->length
== 2)
1117 else if (r
->length
== 1)
1122 saa_fwrite(s
->data
, r
->addr
, blk
, len
);
1125 /* dump the section data to file */
1126 saa_fpwrite(s
->data
, machofp
);
1129 /* pad last section up to reloc entries on int64_t boundary */
1130 fwrite(rel_paddata
, rel_padcnt64
, 1, machofp
);
1132 /* emit relocation entries */
1133 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1134 macho_write_relocs (s
->relocs
);
1137 /* Write out the symbol table. We should already have sorted this
1139 static void macho_write_symtab (void)
1146 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1148 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1149 if ((sym
->type
& N_EXT
) == 0) {
1150 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1151 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1152 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1153 fwriteint16_t(sym
->desc
, machofp
); /* description */
1155 /* Fix up the symbol value now that we know the final section
1157 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1158 for (s
= sects
, fi
= 1;
1159 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1160 sym
->value
+= s
->size
;
1163 fwriteint64_t(sym
->value
, machofp
); /* value (i.e. offset) */
1167 for (i
= 0; i
< nextdefsym
; i
++) {
1168 sym
= extdefsyms
[i
];
1169 fwriteint32_t(sym
->strx
, machofp
);
1170 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1171 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1172 fwriteint16_t(sym
->desc
, machofp
); /* description */
1174 /* Fix up the symbol value now that we know the final section
1176 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1177 for (s
= sects
, fi
= 1;
1178 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1179 sym
->value
+= s
->size
;
1182 fwriteint64_t(sym
->value
, machofp
); /* value (i.e. offset) */
1185 for (i
= 0; i
< nundefsym
; i
++) {
1187 fwriteint32_t(sym
->strx
, machofp
);
1188 fwrite(&sym
->type
, 1, 1, machofp
); // symbol type
1189 fwrite(&sym
->sect
, 1, 1, machofp
); // section
1190 fwriteint16_t(sym
->desc
, machofp
); // description
1192 // Fix up the symbol value now that we know the final section sizes.
1193 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1194 for (s
= sects
, fi
= 1;
1195 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1196 sym
->value
+= s
->size
;
1199 fwriteint64_t(sym
->value
, machofp
); // value (i.e. offset)
1204 /* Fixup the snum in the relocation entries, we should be
1205 doing this only for externally undefined symbols. */
1206 static void macho_fixup_relocs (struct reloc
*r
)
1213 for (i
= 0; i
< nundefsym
; i
++) {
1215 if (sym
->initial_snum
== r
->snum
) {
1216 r
->snum
= sym
->snum
;
1225 /* Write out the object file. */
1227 static void macho_write (void)
1229 uint64_t offset
= 0;
1231 /* mach-o object file structure:
1237 ** uint32_t mach file type
1238 ** uint32_t number of load commands
1239 ** uint32_t size of all load commands
1240 ** (includes section struct size of segment command)
1244 ** uint32_t command type == LC_SEGMENT_64
1245 ** uint32_t size of load command
1246 ** (including section load commands)
1247 ** char[16] segment name
1248 ** uint64_t in-memory offset
1249 ** uint64_t in-memory size
1250 ** uint64_t in-file offset to data area
1251 ** uint64_t in-file size
1252 ** (in-memory size excluding zerofill sections)
1253 ** int maximum vm protection
1254 ** int initial vm protection
1255 ** uint32_t number of sections
1259 ** char[16] section name
1260 ** char[16] segment name
1261 ** uint64_t in-memory offset
1262 ** uint64_t in-memory size
1263 ** uint32_t in-file offset
1264 ** uint32_t alignment
1265 ** (irrelevant in MH_OBJECT)
1266 ** uint32_t in-file offset of relocation entires
1267 ** uint32_t number of relocations
1269 ** uint32_t reserved
1270 ** uint32_t reserved
1272 ** symbol table command
1273 ** uint32_t command type == LC_SYMTAB
1274 ** uint32_t size of load command
1275 ** uint32_t symbol table offset
1276 ** uint32_t number of symbol table entries
1277 ** uint32_t string table offset
1278 ** uint32_t string table size
1282 ** padding to int64_t boundary
1284 ** relocation data (struct reloc)
1286 ** uint data (symbolnum, pcrel, length, extern, type)
1288 ** symbol table data (struct nlist)
1289 ** int32_t string table entry number
1291 ** (extern, absolute, defined in section)
1293 ** (0 for global symbols, section number of definition (>= 1, <=
1294 ** 254) for local symbols, size of variable for common symbols
1295 ** [type == extern])
1296 ** int16_t description
1297 ** (for stab debugging format)
1298 ** uint64_t value (i.e. file offset) of symbol or stab offset
1300 ** string table data
1301 ** list of null-terminated strings
1304 /* Emit the Mach-O header. */
1305 macho_write_header();
1307 offset
= MACHO_HEADER64_SIZE
+ head_sizeofcmds64
;
1309 /* emit the segment load command */
1310 if (seg_nsects64
> 0)
1311 offset
= macho_write_segment (offset
);
1313 error(ERR_WARNING
, "no sections?");
1316 /* write out symbol command */
1317 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1318 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1319 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1320 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1323 offset
+= nsyms
* MACHO_NLIST64_SIZE
;
1324 fwriteint32_t(offset
, machofp
); /* string table offset */
1325 fwriteint32_t(strslen
, machofp
); /* string table size */
1328 /* emit section data */
1329 if (seg_nsects64
> 0)
1330 macho_write_section ();
1332 /* emit symbol table if we have symbols */
1334 macho_write_symtab ();
1336 /* we don't need to pad here since MACHO_NLIST64_SIZE == 16 */
1338 /* emit string table */
1339 saa_fpwrite(strs
, machofp
);
1341 /* We do quite a bit here, starting with finalizing all of the data
1342 for the object file, writing, and then freeing all of the data from
1345 static void macho_cleanup(int debuginfo
)
1353 /* Sort all symbols. */
1354 macho_layout_symbols (&nsyms
, &strslen
);
1356 /* Fixup relocation entries */
1357 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1358 macho_fixup_relocs (s
->relocs
);
1361 /* First calculate and finalize needed values. */
1362 macho_calculate_sizes();
1368 /* free up everything */
1369 while (sects
->next
) {
1371 sects
= sects
->next
;
1374 while (s
->relocs
!= NULL
) {
1376 s
->relocs
= s
->relocs
->next
;
1387 while (syms
->next
) {
1396 /* Debugging routines. */
1397 static void debug_reloc (struct reloc
*r
)
1399 fprintf (stdout
, "reloc:\n");
1400 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1401 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1402 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1403 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1404 fprintf (stdout
, "\text: %d\n", r
->ext
);
1405 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1408 static void debug_section_relocs (struct section
*s
)
1410 struct reloc
*r
= s
->relocs
;
1412 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1420 struct ofmt of_macho64
= {
1421 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",