1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outmacho.c output routines for the Netwide Assembler to produce
36 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
39 /* Most of this file is, like Mach-O itself, based on a.out. For more
40 * guidelines see outaout.c. */
55 #include "output/outform.h"
56 #include "output/outlib.h"
58 #if defined(OF_MACHO32)
60 /* Mach-O in-file header structure sizes */
61 #define MACHO_HEADER_SIZE (28)
62 #define MACHO_SEGCMD_SIZE (56)
63 #define MACHO_SECTCMD_SIZE (68)
64 #define MACHO_SYMCMD_SIZE (24)
65 #define MACHO_NLIST_SIZE (12)
66 #define MACHO_RELINFO_SIZE (8)
68 /* Mach-O file header values */
69 #define MH_MAGIC (0xfeedface)
70 #define CPU_TYPE_I386 (7) /* x86 platform */
71 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
72 #define MH_OBJECT (0x1) /* object file */
74 #define LC_SEGMENT (0x1) /* segment load command */
75 #define LC_SYMTAB (0x2) /* symbol table load command */
77 #define VM_PROT_NONE (0x00)
78 #define VM_PROT_READ (0x01)
79 #define VM_PROT_WRITE (0x02)
80 #define VM_PROT_EXECUTE (0x04)
82 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
83 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
86 /* nasm internal data */
93 /* data that goes into the file */
94 char sectname
[16]; /* what this section is called */
95 char segname
[16]; /* segment this section will be in */
96 uint32_t addr
; /* in-memory address (subject to alignment) */
97 uint32_t size
; /* in-memory and -file size */
98 uint32_t nreloc
; /* relocation entry count */
99 uint32_t flags
; /* type and attributes (masked) */
102 #define SECTION_TYPE 0x000000ff /* section type mask */
104 #define S_REGULAR (0x0) /* standard section */
105 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
107 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
108 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
109 machine instructions */
110 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
111 relocation entries */
112 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
113 relocation entries */
116 static struct sectmap
{
117 const char *nasmsect
;
119 const char *sectname
;
122 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
},
123 {".data", "__DATA", "__data", S_REGULAR
},
124 {".rodata", "__DATA", "__const", S_REGULAR
},
125 {".bss", "__DATA", "__bss", S_ZEROFILL
},
126 {NULL
, NULL
, NULL
, 0}
130 /* nasm internal data */
133 /* data that goes into the file */
134 int32_t addr
; /* op's offset in section */
135 unsigned int snum
:24, /* contains symbol index if
136 ** ext otherwise in-file
138 pcrel
:1, /* relative relocation */
139 length
:2, /* 0=byte, 1=word, 2=int32_t */
140 ext
:1, /* external symbol referenced */
141 type
:4; /* reloc type, 0 for us */
144 #define R_ABS 0 /* absolute relocation */
145 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
146 ** highest bit == 1 */
149 /* nasm internal data */
150 struct symbol
*next
; /* next symbol in the list */
151 char *name
; /* name of this symbol */
152 int32_t initial_snum
; /* symbol number used above in
154 int32_t snum
; /* true snum for reloc */
156 /* data that goes into the file */
157 int32_t strx
; /* string table index */
158 uint8_t type
; /* symbol type */
159 uint8_t sect
; /* NO_SECT or section number */
160 int16_t desc
; /* for stab debugging, 0 for us */
161 uint32_t value
; /* offset of symbol in section */
164 /* symbol type bits */
165 #define N_EXT 0x01 /* global or external symbol */
167 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
168 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
169 #define N_SECT 0xe /* defined symbol, n_sect holds
172 #define N_TYPE 0x0e /* type bit mask */
174 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
176 /* special section number values */
177 #define NO_SECT 0 /* no section, invalid */
178 #define MAX_SECT 255 /* maximum number of sections */
180 static struct section
*sects
, **sectstail
;
181 static struct symbol
*syms
, **symstail
;
182 static uint32_t nsyms
;
184 /* These variables are set by macho_layout_symbols() to organize
185 the symbol table and string table in order the dynamic linker
186 expects. They are then used in macho_write() to put out the
187 symbols and strings in that order.
189 The order of the symbol table is:
191 defined external symbols (sorted by name)
192 undefined external symbols (sorted by name)
194 The order of the string table is:
195 strings for external symbols
196 strings for local symbols
198 static uint32_t ilocalsym
= 0;
199 static uint32_t iextdefsym
= 0;
200 static uint32_t iundefsym
= 0;
201 static uint32_t nlocalsym
;
202 static uint32_t nextdefsym
;
203 static uint32_t nundefsym
;
204 static struct symbol
**extdefsyms
= NULL
;
205 static struct symbol
**undefsyms
= NULL
;
207 static struct RAA
*extsyms
;
208 static struct SAA
*strs
;
209 static uint32_t strslen
;
211 /* Global file information. This should be cleaned up into either
212 a structure or as function arguments. */
213 uint32_t head_ncmds
= 0;
214 uint32_t head_sizeofcmds
= 0;
215 uint32_t seg_filesize
= 0;
216 uint32_t seg_vmsize
= 0;
217 uint32_t seg_nsects
= 0;
218 uint32_t rel_padcnt
= 0;
221 #define xstrncpy(xdst, xsrc) \
222 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
223 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
224 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
226 #define alignint32_t(x) \
227 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
229 static void debug_reloc (struct reloc
*);
230 static void debug_section_relocs (struct section
*) _unused
;
232 static struct section
*get_section_by_name(const char *segname
,
233 const char *sectname
)
237 for (s
= sects
; s
!= NULL
; s
= s
->next
)
238 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
244 static struct section
*get_section_by_index(const int32_t index
)
248 for (s
= sects
; s
!= NULL
; s
= s
->next
)
249 if (index
== s
->index
)
255 static int32_t get_section_index_by_name(const char *segname
,
256 const char *sectname
)
260 for (s
= sects
; s
!= NULL
; s
= s
->next
)
261 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
267 static char *get_section_name_by_index(const int32_t index
)
271 for (s
= sects
; s
!= NULL
; s
= s
->next
)
272 if (index
== s
->index
)
278 static uint8_t get_section_fileindex_by_index(const int32_t index
)
283 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
284 if (index
== s
->index
)
288 nasm_error(ERR_WARNING
,
289 "too many sections (>255) - clipped by fileindex");
294 static void macho_init(void)
308 extsyms
= raa_init();
311 /* string table starts with a zero byte - don't ask why */
312 saa_wbytes(strs
, &zero
, sizeof(char));
316 static void sect_write(struct section
*sect
,
317 const uint8_t *data
, uint32_t len
)
319 saa_wbytes(sect
->data
, data
, len
);
323 static void add_reloc(struct section
*sect
, int32_t section
,
324 int pcrel
, int bytes
)
329 /* NeXT as puts relocs in reversed order (address-wise) into the
330 ** files, so we do the same, doesn't seem to make much of a
331 ** difference either way */
332 r
= nasm_malloc(sizeof(struct reloc
));
333 r
->next
= sect
->relocs
;
336 /* the current end of the section will be the symbol's address for
337 ** now, might have to be fixed by macho_fixup_relocs() later on. make
338 ** sure we don't make the symbol scattered by setting the highest
339 ** bit by accident */
340 r
->addr
= sect
->size
& ~R_SCATTERED
;
344 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
345 r
->length
= bytes
>> 1;
347 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
350 if (section
== NO_SEG
) {
351 /* absolute local symbol if no section index given */
354 fi
= get_section_fileindex_by_index(section
);
357 /* external symbol if no section with that index known,
358 ** symbol number was saved in macho_symdef() */
359 r
->snum
= raa_read(extsyms
, section
);
362 /* local symbol in section fi */
370 static void macho_output(int32_t secto
, const void *data
,
371 enum out_type type
, uint64_t size
,
372 int32_t section
, int32_t wrt
)
374 struct section
*s
, *sbss
;
376 uint8_t mydata
[4], *p
;
380 nasm_error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
381 /* continue to do _something_ */
384 if (secto
== NO_SEG
) {
385 if (type
!= OUT_RESERVE
)
386 nasm_error(ERR_NONFATAL
, "attempt to assemble code in "
392 s
= get_section_by_index(secto
);
395 nasm_error(ERR_WARNING
, "attempt to assemble code in"
396 " section %d: defaulting to `.text'", secto
);
397 s
= get_section_by_name("__TEXT", "__text");
399 /* should never happen */
401 nasm_error(ERR_PANIC
, "text section not found");
404 sbss
= get_section_by_name("__DATA", "__bss");
406 if (s
== sbss
&& type
!= OUT_RESERVE
) {
407 nasm_error(ERR_WARNING
, "attempt to initialize memory in the"
408 " BSS section: ignored");
409 s
->size
+= realsize(type
, size
);
416 nasm_error(ERR_WARNING
, "uninitialized space declared in"
417 " %s section: zeroing",
418 get_section_name_by_index(secto
));
420 sect_write(s
, NULL
, size
);
427 if (section
!= NO_SEG
)
428 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
430 sect_write(s
, data
, size
);
434 addr
= *(int64_t *)data
;
436 if (section
!= NO_SEG
) {
438 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
439 " section base references");
441 add_reloc(s
, section
, 0, size
);
445 WRITEADDR(p
, addr
, size
);
446 sect_write(s
, mydata
, size
);
450 if (section
== secto
)
451 nasm_error(ERR_PANIC
, "intra-section OUT_REL2ADR");
453 if (section
!= NO_SEG
&& section
% 2) {
454 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
455 " section base references");
457 add_reloc(s
, section
, 1, 2);
460 WRITESHORT(p
, *(int32_t *)data
- (size
+ s
->size
));
461 sect_write(s
, mydata
, 2L);
465 if (section
== secto
)
466 nasm_error(ERR_PANIC
, "intra-section OUT_REL4ADR");
468 if (section
!= NO_SEG
&& section
% 2) {
469 nasm_error(ERR_NONFATAL
, "Mach-O format does not support"
470 " section base references");
472 add_reloc(s
, section
, 1, 4);
475 WRITELONG(p
, *(int32_t *)data
- (size
+ s
->size
));
476 sect_write(s
, mydata
, 4L);
480 nasm_error(ERR_PANIC
, "unknown output type?");
485 static int32_t macho_section(char *name
, int pass
, int *bits
)
487 int32_t index
, originalIndex
;
488 char *sectionAttributes
;
494 /* Default to 32 bits. */
498 sectionAttributes
= NULL
;
500 sectionAttributes
= name
;
501 name
= nasm_strsep(§ionAttributes
, " \t");
504 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
505 /* make lookup into section name translation table */
506 if (!strcmp(name
, sm
->nasmsect
)) {
507 char *currentAttribute
;
509 /* try to find section with that name */
510 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
513 /* create it if it doesn't exist yet */
515 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
517 sectstail
= &s
->next
;
519 s
->data
= saa_init(1L);
520 s
->index
= seg_alloc();
524 xstrncpy(s
->segname
, sm
->segname
);
525 xstrncpy(s
->sectname
, sm
->sectname
);
528 s
->flags
= sm
->flags
;
532 s
= get_section_by_index(index
);
535 while ((NULL
!= sectionAttributes
)
536 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
537 if (0 != *currentAttribute
) {
538 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
540 int newAlignment
, value
;
542 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
543 newAlignment
= alignlog2_32(value
);
546 nasm_error(ERR_PANIC
,
547 "unknown or missing alignment value \"%s\" "
548 "specified for section \"%s\"",
549 currentAttribute
+ 6,
552 } else if (0 > newAlignment
) {
553 nasm_error(ERR_PANIC
,
554 "alignment of %d (for section \"%s\") is not "
561 if ((-1 != originalIndex
)
562 && (s
->align
!= newAlignment
)
563 && (s
->align
!= -1)) {
564 nasm_error(ERR_PANIC
,
565 "section \"%s\" has already been specified "
566 "with alignment %d, conflicts with new "
574 s
->align
= newAlignment
;
575 } else if (!nasm_stricmp("data", currentAttribute
)) {
576 /* Do nothing; 'data' is implicit */
578 nasm_error(ERR_PANIC
,
579 "unknown section attribute %s for section %s",
591 nasm_error(ERR_PANIC
, "invalid section name %s", name
);
595 static void macho_symdef(char *name
, int32_t section
, int64_t offset
,
596 int is_global
, char *special
)
601 nasm_error(ERR_NONFATAL
, "The Mach-O output format does "
602 "not support any special symbol types");
606 if (is_global
== 3) {
607 nasm_error(ERR_NONFATAL
, "The Mach-O format does not "
608 "(yet) support forward reference fixups.");
612 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
614 symstail
= &sym
->next
;
621 sym
->initial_snum
= -1;
623 /* external and common symbols get N_EXT */
627 if (section
== NO_SEG
) {
628 /* symbols in no section get absolute */
634 /* get the in-file index of the section the symbol was defined in */
635 sym
->sect
= get_section_fileindex_by_index(section
);
637 if (sym
->sect
== NO_SECT
) {
638 /* remember symbol number of references to external
639 ** symbols, this works because every external symbol gets
640 ** its own section number allocated internally by nasm and
641 ** can so be used as a key */
642 extsyms
= raa_write(extsyms
, section
, nsyms
);
643 sym
->initial_snum
= nsyms
;
648 /* there isn't actually a difference between global
649 ** and common symbols, both even have their size in
655 /* give an error on unfound section if it's not an
656 ** external or common symbol (assemble_file() does a
657 ** seg_alloc() on every call for them) */
658 nasm_error(ERR_PANIC
, "in-file index for section %d not found",
667 static int32_t macho_segbase(int32_t section
)
672 static void macho_filename(char *inname
, char *outname
)
674 standard_extension(inname
, outname
, ".o");
677 extern macros_t macho_stdmac
[];
679 /* Comparison function for qsort symbol layout. */
680 static int layout_compare (const struct symbol
**s1
,
681 const struct symbol
**s2
)
683 return (strcmp ((*s1
)->name
, (*s2
)->name
));
686 /* The native assembler does a few things in a similar function
688 * Remove temporary labels
689 * Sort symbols according to local, external, undefined (by name)
690 * Order the string table
692 We do not remove temporary labels right now.
694 numsyms is the total number of symbols we have. strtabsize is the
695 number entries in the string table. */
697 static void macho_layout_symbols (uint32_t *numsyms
,
698 uint32_t *strtabsize
)
700 struct symbol
*sym
, **symp
;
704 *strtabsize
= sizeof (char);
708 while ((sym
= *symp
)) {
709 /* Undefined symbols are now external. */
710 if (sym
->type
== N_UNDF
)
713 if ((sym
->type
& N_EXT
) == 0) {
714 sym
->snum
= *numsyms
;
715 *numsyms
= *numsyms
+ 1;
719 if ((sym
->type
& N_TYPE
) != N_UNDF
)
724 /* If we handle debug info we'll want
725 to check for it here instead of just
726 adding the symbol to the string table. */
727 sym
->strx
= *strtabsize
;
728 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
729 *strtabsize
+= strlen(sym
->name
) + 1;
734 /* Next, sort the symbols. Most of this code is a direct translation from
735 the Apple cctools symbol layout. We need to keep compatibility with that. */
736 /* Set the indexes for symbol groups into the symbol table */
738 iextdefsym
= nlocalsym
;
739 iundefsym
= nlocalsym
+ nextdefsym
;
741 /* allocate arrays for sorting externals by name */
742 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
743 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
750 while ((sym
= *symp
)) {
752 if((sym
->type
& N_EXT
) == 0) {
753 sym
->strx
= *strtabsize
;
754 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
755 *strtabsize
+= strlen(sym
->name
) + 1;
758 if((sym
->type
& N_TYPE
) != N_UNDF
)
759 extdefsyms
[i
++] = sym
;
761 undefsyms
[j
++] = sym
;
766 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
767 (int (*)(const void *, const void *))layout_compare
);
768 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
769 (int (*)(const void *, const void *))layout_compare
);
771 for(i
= 0; i
< nextdefsym
; i
++) {
772 extdefsyms
[i
]->snum
= *numsyms
;
775 for(j
= 0; j
< nundefsym
; j
++) {
776 undefsyms
[j
]->snum
= *numsyms
;
781 /* Calculate some values we'll need for writing later. */
783 static void macho_calculate_sizes (void)
787 /* count sections and calculate in-memory and in-file offsets */
788 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
791 /* zerofill sections aren't actually written to the file */
792 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
793 seg_filesize
+= s
->size
;
795 /* recalculate segment address based on alignment and vm size */
796 s
->addr
= seg_vmsize
;
797 /* we need section alignment to calculate final section address */
799 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
801 uint32_t newaddr
= ALIGN(s
->addr
, 1 << s
->align
);
802 pad
= newaddr
- s
->addr
;
806 seg_vmsize
+= s
->size
+ pad
;
810 /* calculate size of all headers, load commands and sections to
811 ** get a pointer to the start of all the raw data */
812 if (seg_nsects
> 0) {
815 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
820 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
824 /* Write out the header information for the file. */
826 static void macho_write_header (void)
828 fwriteint32_t(MH_MAGIC
, ofile
); /* magic */
829 fwriteint32_t(CPU_TYPE_I386
, ofile
); /* CPU type */
830 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, ofile
); /* CPU subtype */
831 fwriteint32_t(MH_OBJECT
, ofile
); /* Mach-O file type */
832 fwriteint32_t(head_ncmds
, ofile
); /* number of load commands */
833 fwriteint32_t(head_sizeofcmds
, ofile
); /* size of load commands */
834 fwriteint32_t(0, ofile
); /* no flags */
837 /* Write out the segment load command at offset. */
839 static uint32_t macho_write_segment (uint32_t offset
)
841 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
842 uint32_t s_reloff
= 0;
845 fwriteint32_t(LC_SEGMENT
, ofile
); /* cmd == LC_SEGMENT */
847 /* size of load command including section load commands */
848 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
849 MACHO_SECTCMD_SIZE
, ofile
);
851 /* in an MH_OBJECT file all sections are in one unnamed (name
852 ** all zeros) segment */
853 fwritezero(16, ofile
);
854 fwriteint32_t(0, ofile
); /* in-memory offset */
855 fwriteint32_t(seg_vmsize
, ofile
); /* in-memory size */
856 fwriteint32_t(offset
, ofile
); /* in-file offset to data */
857 fwriteint32_t(seg_filesize
, ofile
); /* in-file size */
858 fwriteint32_t(VM_PROT_DEFAULT
, ofile
); /* maximum vm protection */
859 fwriteint32_t(VM_PROT_DEFAULT
, ofile
); /* initial vm protection */
860 fwriteint32_t(seg_nsects
, ofile
); /* number of sections */
861 fwriteint32_t(0, ofile
); /* no flags */
863 /* emit section headers */
864 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
865 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, ofile
);
866 fwrite(s
->segname
, sizeof(s
->segname
), 1, ofile
);
867 fwriteint32_t(s
->addr
, ofile
);
868 fwriteint32_t(s
->size
, ofile
);
870 /* dummy data for zerofill sections or proper values */
871 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
872 fwriteint32_t(offset
, ofile
);
873 /* Write out section alignment, as a power of two.
874 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
876 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
877 fwriteint32_t(s
->align
, ofile
);
878 /* To be compatible with cctools as we emit
879 a zero reloff if we have no relocations. */
880 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, ofile
);
881 fwriteint32_t(s
->nreloc
, ofile
);
884 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
886 fwriteint32_t(0, ofile
);
887 fwriteint32_t(0, ofile
);
888 fwriteint32_t(0, ofile
);
889 fwriteint32_t(0, ofile
);
892 fwriteint32_t(s
->flags
, ofile
); /* flags */
893 fwriteint32_t(0, ofile
); /* reserved */
894 fwriteint32_t(0, ofile
); /* reserved */
897 rel_padcnt
= rel_base
- offset
;
898 offset
= rel_base
+ s_reloff
;
903 /* For a given chain of relocs r, write out the entire relocation
904 chain to the object file. */
906 static void macho_write_relocs (struct reloc
*r
)
911 fwriteint32_t(r
->addr
, ofile
); /* reloc offset */
914 word2
|= r
->pcrel
<< 24;
915 word2
|= r
->length
<< 25;
916 word2
|= r
->ext
<< 27;
917 word2
|= r
->type
<< 28;
918 fwriteint32_t(word2
, ofile
); /* reloc data */
924 /* Write out the section data. */
925 static void macho_write_section (void)
927 struct section
*s
, *s2
;
929 uint8_t fi
, *p
, *q
, blk
[4];
932 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
933 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
936 /* no padding needs to be done to the sections */
938 /* Like a.out Mach-O references things in the data or bss
939 * sections by addresses which are actually relative to the
940 * start of the _text_ section, in the _file_. See outaout.c
941 * for more information. */
943 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
944 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
948 /* get offset based on relocation type */
950 l
+= ((int32_t)*p
++) << 8;
952 if (r
->length
== 2) {
953 l
+= ((int32_t)*p
++) << 16;
954 l
+= ((int32_t)*p
++) << 24;
958 /* If the relocation is internal add to the current section
959 offset. Otherwise the only value we need is the symbol
960 offset which we already have. The linker takes care
961 of the rest of the address. */
963 /* generate final address by section address and offset */
964 for (s2
= sects
, fi
= 1;
965 s2
!= NULL
; s2
= s2
->next
, fi
++) {
973 /* write new offset back */
976 else if (r
->length
== 1)
981 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
984 /* dump the section data to file */
985 saa_fpwrite(s
->data
, ofile
);
988 /* pad last section up to reloc entries on int32_t boundary */
989 fwritezero(rel_padcnt
, ofile
);
991 /* emit relocation entries */
992 for (s
= sects
; s
!= NULL
; s
= s
->next
)
993 macho_write_relocs (s
->relocs
);
996 /* Write out the symbol table. We should already have sorted this
998 static void macho_write_symtab (void)
1005 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1007 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1008 if ((sym
->type
& N_EXT
) == 0) {
1009 fwriteint32_t(sym
->strx
, ofile
); /* string table entry number */
1010 fwrite(&sym
->type
, 1, 1, ofile
); /* symbol type */
1011 fwrite(&sym
->sect
, 1, 1, ofile
); /* section */
1012 fwriteint16_t(sym
->desc
, ofile
); /* description */
1014 /* Fix up the symbol value now that we know the final section
1016 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1017 for (s
= sects
, fi
= 1; s
!= NULL
; s
= s
->next
, fi
++) {
1018 if (fi
== sym
->sect
) {
1019 sym
->value
+= s
->addr
;
1025 fwriteint32_t(sym
->value
, ofile
); /* value (i.e. offset) */
1029 for (i
= 0; i
< nextdefsym
; i
++) {
1030 sym
= extdefsyms
[i
];
1031 fwriteint32_t(sym
->strx
, ofile
);
1032 fwrite(&sym
->type
, 1, 1, ofile
); /* symbol type */
1033 fwrite(&sym
->sect
, 1, 1, ofile
); /* section */
1034 fwriteint16_t(sym
->desc
, ofile
); /* description */
1036 /* Fix up the symbol value now that we know the final section
1038 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1039 for (s
= sects
, fi
= 1;
1040 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1041 sym
->value
+= s
->size
;
1044 fwriteint32_t(sym
->value
, ofile
); /* value (i.e. offset) */
1047 for (i
= 0; i
< nundefsym
; i
++) {
1049 fwriteint32_t(sym
->strx
, ofile
);
1050 fwrite(&sym
->type
, 1, 1, ofile
); /* symbol type */
1051 fwrite(&sym
->sect
, 1, 1, ofile
); /* section */
1052 fwriteint16_t(sym
->desc
, ofile
); /* description */
1054 /* Fix up the symbol value now that we know the final section
1056 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1057 for (s
= sects
, fi
= 1;
1058 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1059 sym
->value
+= s
->size
;
1062 fwriteint32_t(sym
->value
, ofile
); /* value (i.e. offset) */
1066 /* Fixup the snum in the relocation entries, we should be
1067 doing this only for externally undefined symbols. */
1068 static void macho_fixup_relocs (struct reloc
*r
)
1075 for (i
= 0; i
< nundefsym
; i
++) {
1077 if (sym
->initial_snum
== r
->snum
) {
1078 r
->snum
= sym
->snum
;
1087 /* Write out the object file. */
1089 static void macho_write (void)
1091 uint32_t offset
= 0;
1093 /* mach-o object file structure:
1099 ** uint32_t mach file type
1100 ** uint32_t number of load commands
1101 ** uint32_t size of all load commands
1102 ** (includes section struct size of segment command)
1106 ** uint32_t command type == LC_SEGMENT
1107 ** uint32_t size of load command
1108 ** (including section load commands)
1109 ** char[16] segment name
1110 ** uint32_t in-memory offset
1111 ** uint32_t in-memory size
1112 ** uint32_t in-file offset to data area
1113 ** uint32_t in-file size
1114 ** (in-memory size excluding zerofill sections)
1115 ** int maximum vm protection
1116 ** int initial vm protection
1117 ** uint32_t number of sections
1121 ** char[16] section name
1122 ** char[16] segment name
1123 ** uint32_t in-memory offset
1124 ** uint32_t in-memory size
1125 ** uint32_t in-file offset
1126 ** uint32_t alignment
1127 ** (irrelevant in MH_OBJECT)
1128 ** uint32_t in-file offset of relocation entires
1129 ** uint32_t number of relocations
1131 ** uint32_t reserved
1132 ** uint32_t reserved
1134 ** symbol table command
1135 ** uint32_t command type == LC_SYMTAB
1136 ** uint32_t size of load command
1137 ** uint32_t symbol table offset
1138 ** uint32_t number of symbol table entries
1139 ** uint32_t string table offset
1140 ** uint32_t string table size
1144 ** padding to int32_t boundary
1146 ** relocation data (struct reloc)
1148 ** uint data (symbolnum, pcrel, length, extern, type)
1150 ** symbol table data (struct nlist)
1151 ** int32_t string table entry number
1153 ** (extern, absolute, defined in section)
1155 ** (0 for global symbols, section number of definition (>= 1, <=
1156 ** 254) for local symbols, size of variable for common symbols
1157 ** [type == extern])
1158 ** int16_t description
1159 ** (for stab debugging format)
1160 ** uint32_t value (i.e. file offset) of symbol or stab offset
1162 ** string table data
1163 ** list of null-terminated strings
1166 /* Emit the Mach-O header. */
1167 macho_write_header();
1169 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1171 /* emit the segment load command */
1173 offset
= macho_write_segment (offset
);
1175 nasm_error(ERR_WARNING
, "no sections?");
1178 /* write out symbol command */
1179 fwriteint32_t(LC_SYMTAB
, ofile
); /* cmd == LC_SYMTAB */
1180 fwriteint32_t(MACHO_SYMCMD_SIZE
, ofile
); /* size of load command */
1181 fwriteint32_t(offset
, ofile
); /* symbol table offset */
1182 fwriteint32_t(nsyms
, ofile
); /* number of symbol
1185 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1186 fwriteint32_t(offset
, ofile
); /* string table offset */
1187 fwriteint32_t(strslen
, ofile
); /* string table size */
1190 /* emit section data */
1192 macho_write_section ();
1194 /* emit symbol table if we have symbols */
1196 macho_write_symtab ();
1198 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1200 /* emit string table */
1201 saa_fpwrite(strs
, ofile
);
1203 /* We do quite a bit here, starting with finalizing all of the data
1204 for the object file, writing, and then freeing all of the data from
1207 static void macho_cleanup(int debuginfo
)
1215 /* Sort all symbols. */
1216 macho_layout_symbols (&nsyms
, &strslen
);
1218 /* Fixup relocation entries */
1219 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1220 macho_fixup_relocs (s
->relocs
);
1223 /* First calculate and finalize needed values. */
1224 macho_calculate_sizes();
1227 /* free up everything */
1228 while (sects
->next
) {
1230 sects
= sects
->next
;
1233 while (s
->relocs
!= NULL
) {
1235 s
->relocs
= s
->relocs
->next
;
1246 while (syms
->next
) {
1255 /* Debugging routines. */
1256 static void debug_reloc (struct reloc
*r
)
1258 fprintf (stdout
, "reloc:\n");
1259 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1260 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1261 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1262 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1263 fprintf (stdout
, "\text: %d\n", r
->ext
);
1264 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1267 static void debug_section_relocs (struct section
*s
)
1269 struct reloc
*r
= s
->relocs
;
1271 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1279 struct ofmt of_macho32
= {
1280 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",