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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * outmacho.c output routines for the Netwide Assembler to produce
48 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
51 /* Most of this file is, like Mach-O itself, based on a.out. For more
52 * guidelines see outaout.c. */
66 #include "output/outform.h"
67 #include "output/outlib.h"
71 /* Mach-O in-file header structure sizes */
72 #define MACHO_HEADER_SIZE (28)
73 #define MACHO_SEGCMD_SIZE (56)
74 #define MACHO_SECTCMD_SIZE (68)
75 #define MACHO_SYMCMD_SIZE (24)
76 #define MACHO_NLIST_SIZE (12)
77 #define MACHO_RELINFO_SIZE (8)
79 /* Mach-O file header values */
80 #define MH_MAGIC (0xfeedface)
81 #define CPU_TYPE_I386 (7) /* x86 platform */
82 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
83 #define MH_OBJECT (0x1) /* object file */
85 #define LC_SEGMENT (0x1) /* segment load command */
86 #define LC_SYMTAB (0x2) /* symbol table load command */
88 #define VM_PROT_NONE (0x00)
89 #define VM_PROT_READ (0x01)
90 #define VM_PROT_WRITE (0x02)
91 #define VM_PROT_EXECUTE (0x04)
93 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
94 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
97 /* nasm internal data */
101 struct reloc
*relocs
;
104 /* data that goes into the file */
105 char sectname
[16]; /* what this section is called */
106 char segname
[16]; /* segment this section will be in */
107 uint32_t addr
; /* in-memory address (subject to alignment) */
108 uint32_t size
; /* in-memory and -file size */
109 uint32_t nreloc
; /* relocation entry count */
110 uint32_t flags
; /* type and attributes (masked) */
113 #define SECTION_TYPE 0x000000ff /* section type mask */
115 #define S_REGULAR (0x0) /* standard section */
116 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
118 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
119 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
120 machine instructions */
121 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
122 relocation entries */
123 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
124 relocation entries */
127 static struct sectmap
{
128 const char *nasmsect
;
130 const char *sectname
;
133 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
},
134 {".data", "__DATA", "__data", S_REGULAR
},
135 {".rodata", "__DATA", "__const", S_REGULAR
},
136 {".bss", "__DATA", "__bss", S_ZEROFILL
},
137 {NULL
, NULL
, NULL
, 0}
141 /* nasm internal data */
144 /* data that goes into the file */
145 int32_t addr
; /* op's offset in section */
146 unsigned int snum
:24, /* contains symbol index if
147 ** ext otherwise in-file
149 pcrel
:1, /* relative relocation */
150 length
:2, /* 0=byte, 1=word, 2=int32_t */
151 ext
:1, /* external symbol referenced */
152 type
:4; /* reloc type, 0 for us */
155 #define R_ABS 0 /* absolute relocation */
156 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
157 ** highest bit == 1 */
160 /* nasm internal data */
161 struct symbol
*next
; /* next symbol in the list */
162 char *name
; /* name of this symbol */
163 int32_t initial_snum
; /* symbol number used above in
165 int32_t snum
; /* true snum for reloc */
167 /* data that goes into the file */
168 int32_t strx
; /* string table index */
169 uint8_t type
; /* symbol type */
170 uint8_t sect
; /* NO_SECT or section number */
171 int16_t desc
; /* for stab debugging, 0 for us */
172 uint32_t value
; /* offset of symbol in section */
175 /* symbol type bits */
176 #define N_EXT 0x01 /* global or external symbol */
178 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
179 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
180 #define N_SECT 0xe /* defined symbol, n_sect holds
183 #define N_TYPE 0x0e /* type bit mask */
185 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
187 /* special section number values */
188 #define NO_SECT 0 /* no section, invalid */
189 #define MAX_SECT 255 /* maximum number of sections */
191 static struct section
*sects
, **sectstail
;
192 static struct symbol
*syms
, **symstail
;
193 static uint32_t nsyms
;
195 /* These variables are set by macho_layout_symbols() to organize
196 the symbol table and string table in order the dynamic linker
197 expects. They are then used in macho_write() to put out the
198 symbols and strings in that order.
200 The order of the symbol table is:
202 defined external symbols (sorted by name)
203 undefined external symbols (sorted by name)
205 The order of the string table is:
206 strings for external symbols
207 strings for local symbols
209 static uint32_t ilocalsym
= 0;
210 static uint32_t iextdefsym
= 0;
211 static uint32_t iundefsym
= 0;
212 static uint32_t nlocalsym
;
213 static uint32_t nextdefsym
;
214 static uint32_t nundefsym
;
215 static struct symbol
**extdefsyms
= NULL
;
216 static struct symbol
**undefsyms
= NULL
;
218 static struct RAA
*extsyms
;
219 static struct SAA
*strs
;
220 static uint32_t strslen
;
222 static FILE *machofp
;
224 static evalfunc evaluate
;
226 extern struct ofmt of_macho
;
228 /* Global file information. This should be cleaned up into either
229 a structure or as function arguments. */
230 uint32_t head_ncmds
= 0;
231 uint32_t head_sizeofcmds
= 0;
232 uint32_t seg_filesize
= 0;
233 uint32_t seg_vmsize
= 0;
234 uint32_t seg_nsects
= 0;
235 uint32_t rel_padcnt
= 0;
238 #define xstrncpy(xdst, xsrc) \
239 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
240 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
241 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
243 #define align(x, y) \
244 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
246 #define alignint32_t(x) \
247 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
249 static void debug_reloc (struct reloc
*);
250 static void debug_section_relocs (struct section
*) _unused
;
252 static int exact_log2 (uint32_t align
)
256 } else if (align
& (align
-1)) {
257 return -1; /* Not a power of 2 */
260 return __builtin_ctzl (align
);
264 /* We know exactly one bit is set at this point. */
265 if (align
& 0xffff0000)
267 if (align
& 0xff00ff00)
269 if (align
& 0xf0f0f0f0)
271 if (align
& 0xcccccccc)
273 if (align
& 0xaaaaaaaa)
281 static struct section
*get_section_by_name(const char *segname
,
282 const char *sectname
)
286 for (s
= sects
; s
!= NULL
; s
= s
->next
)
287 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
293 static struct section
*get_section_by_index(const int32_t index
)
297 for (s
= sects
; s
!= NULL
; s
= s
->next
)
298 if (index
== s
->index
)
304 static int32_t get_section_index_by_name(const char *segname
,
305 const char *sectname
)
309 for (s
= sects
; s
!= NULL
; s
= s
->next
)
310 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
316 static char *get_section_name_by_index(const int32_t index
)
320 for (s
= sects
; s
!= NULL
; s
= s
->next
)
321 if (index
== s
->index
)
327 static uint8_t get_section_fileindex_by_index(const int32_t index
)
332 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
333 if (index
== s
->index
)
338 "too many sections (>255) - clipped by fileindex");
343 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
352 (void)ldef
; /* placate optimisers */
364 extsyms
= raa_init();
367 /* string table starts with a zero byte - don't ask why */
368 saa_wbytes(strs
, &zero
, sizeof(char));
372 static int macho_setinfo(enum geninfo type
, char **val
)
379 static void sect_write(struct section
*sect
,
380 const uint8_t *data
, uint32_t len
)
382 saa_wbytes(sect
->data
, data
, len
);
386 static void add_reloc(struct section
*sect
, int32_t section
,
387 int pcrel
, int bytes
)
392 /* NeXT as puts relocs in reversed order (address-wise) into the
393 ** files, so we do the same, doesn't seem to make much of a
394 ** difference either way */
395 r
= nasm_malloc(sizeof(struct reloc
));
396 r
->next
= sect
->relocs
;
399 /* the current end of the section will be the symbol's address for
400 ** now, might have to be fixed by macho_fixup_relocs() later on. make
401 ** sure we don't make the symbol scattered by setting the highest
402 ** bit by accident */
403 r
->addr
= sect
->size
& ~R_SCATTERED
;
407 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
408 r
->length
= bytes
>> 1;
410 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
413 if (section
== NO_SEG
) {
414 /* absolute local symbol if no section index given */
417 fi
= get_section_fileindex_by_index(section
);
420 /* external symbol if no section with that index known,
421 ** symbol number was saved in macho_symdef() */
422 r
->snum
= raa_read(extsyms
, section
);
425 /* local symbol in section fi */
433 static void macho_output(int32_t secto
, const void *data
,
434 enum out_type type
, uint64_t size
,
435 int32_t section
, int32_t wrt
)
437 struct section
*s
, *sbss
;
439 uint8_t mydata
[4], *p
;
443 error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
444 /* continue to do _something_ */
447 if (secto
== NO_SEG
) {
448 if (type
!= OUT_RESERVE
)
449 error(ERR_NONFATAL
, "attempt to assemble code in "
455 s
= get_section_by_index(secto
);
458 error(ERR_WARNING
, "attempt to assemble code in"
459 " section %d: defaulting to `.text'", secto
);
460 s
= get_section_by_name("__TEXT", "__text");
462 /* should never happen */
464 error(ERR_PANIC
, "text section not found");
467 sbss
= get_section_by_name("__DATA", "__bss");
469 if (s
== sbss
&& type
!= OUT_RESERVE
) {
470 error(ERR_WARNING
, "attempt to initialize memory in the"
471 " BSS section: ignored");
472 s
->size
+= realsize(type
, size
);
479 error(ERR_WARNING
, "uninitialized space declared in"
480 " %s section: zeroing",
481 get_section_name_by_index(secto
));
483 sect_write(s
, NULL
, size
);
490 if (section
!= NO_SEG
)
491 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
493 sect_write(s
, data
, size
);
497 addr
= *(int64_t *)data
;
499 if (section
!= NO_SEG
) {
501 error(ERR_NONFATAL
, "Mach-O format does not support"
502 " section base references");
504 add_reloc(s
, section
, 0, size
);
508 WRITEADDR(p
, addr
, size
);
509 sect_write(s
, mydata
, size
);
513 if (section
== secto
)
514 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
516 if (section
!= NO_SEG
&& section
% 2) {
517 error(ERR_NONFATAL
, "Mach-O format does not support"
518 " section base references");
520 add_reloc(s
, section
, 1, 2);
523 WRITESHORT(p
, *(int32_t *)data
- (size
+ s
->size
));
524 sect_write(s
, mydata
, 2L);
528 if (section
== secto
)
529 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
531 if (section
!= NO_SEG
&& section
% 2) {
532 error(ERR_NONFATAL
, "Mach-O format does not support"
533 " section base references");
535 add_reloc(s
, section
, 1, 4);
538 WRITELONG(p
, *(int32_t *)data
- (size
+ s
->size
));
539 sect_write(s
, mydata
, 4L);
543 error(ERR_PANIC
, "unknown output type?");
548 static int32_t macho_section(char *name
, int pass
, int *bits
)
550 int32_t index
, originalIndex
;
551 char *sectionAttributes
;
557 /* Default to 32 bits. */
561 sectionAttributes
= NULL
;
563 sectionAttributes
= name
;
564 name
= nasm_strsep(§ionAttributes
, " \t");
567 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
568 /* make lookup into section name translation table */
569 if (!strcmp(name
, sm
->nasmsect
)) {
570 char *currentAttribute
;
572 /* try to find section with that name */
573 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
576 /* create it if it doesn't exist yet */
578 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
580 sectstail
= &s
->next
;
582 s
->data
= saa_init(1L);
583 s
->index
= seg_alloc();
587 xstrncpy(s
->segname
, sm
->segname
);
588 xstrncpy(s
->sectname
, sm
->sectname
);
591 s
->flags
= sm
->flags
;
595 s
= get_section_by_index(index
);
598 while ((NULL
!= sectionAttributes
)
599 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
600 if (0 != *currentAttribute
) {
601 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
603 int newAlignment
, value
;
605 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
606 newAlignment
= exact_log2(value
);
610 "unknown or missing alignment value \"%s\" "
611 "specified for section \"%s\"",
612 currentAttribute
+ 6,
615 } else if (0 > newAlignment
) {
617 "alignment of %d (for section \"%s\") is not "
624 if ((-1 != originalIndex
)
625 && (s
->align
!= newAlignment
)
626 && (s
->align
!= -1)) {
628 "section \"%s\" has already been specified "
629 "with alignment %d, conflicts with new "
637 s
->align
= newAlignment
;
638 } else if (!nasm_stricmp("data", currentAttribute
)) {
639 /* Do nothing; 'data' is implicit */
642 "unknown section attribute %s for section %s",
654 error(ERR_PANIC
, "invalid section name %s", name
);
658 static void macho_symdef(char *name
, int32_t section
, int64_t offset
,
659 int is_global
, char *special
)
664 error(ERR_NONFATAL
, "The Mach-O output format does "
665 "not support any special symbol types");
669 if (is_global
== 3) {
670 error(ERR_NONFATAL
, "The Mach-O format does not "
671 "(yet) support forward reference fixups.");
675 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
677 symstail
= &sym
->next
;
684 sym
->initial_snum
= -1;
686 /* external and common symbols get N_EXT */
690 if (section
== NO_SEG
) {
691 /* symbols in no section get absolute */
697 /* get the in-file index of the section the symbol was defined in */
698 sym
->sect
= get_section_fileindex_by_index(section
);
700 if (sym
->sect
== NO_SECT
) {
701 /* remember symbol number of references to external
702 ** symbols, this works because every external symbol gets
703 ** its own section number allocated internally by nasm and
704 ** can so be used as a key */
705 extsyms
= raa_write(extsyms
, section
, nsyms
);
706 sym
->initial_snum
= nsyms
;
711 /* there isn't actually a difference between global
712 ** and common symbols, both even have their size in
718 /* give an error on unfound section if it's not an
719 ** external or common symbol (assemble_file() does a
720 ** seg_alloc() on every call for them) */
721 error(ERR_PANIC
, "in-file index for section %d not found",
730 static int32_t macho_segbase(int32_t section
)
735 static int macho_directive(char *directive
, char *value
, int pass
)
743 static void macho_filename(char *inname
, char *outname
, efunc error
)
745 standard_extension(inname
, outname
, ".o", error
);
748 extern macros_t macho_stdmac
[];
750 /* Comparison function for qsort symbol layout. */
751 static int layout_compare (const struct symbol
**s1
,
752 const struct symbol
**s2
)
754 return (strcmp ((*s1
)->name
, (*s2
)->name
));
757 /* The native assembler does a few things in a similar function
759 * Remove temporary labels
760 * Sort symbols according to local, external, undefined (by name)
761 * Order the string table
763 We do not remove temporary labels right now.
765 numsyms is the total number of symbols we have. strtabsize is the
766 number entries in the string table. */
768 static void macho_layout_symbols (uint32_t *numsyms
,
769 uint32_t *strtabsize
)
771 struct symbol
*sym
, **symp
;
775 *strtabsize
= sizeof (char);
779 while ((sym
= *symp
)) {
780 /* Undefined symbols are now external. */
781 if (sym
->type
== N_UNDF
)
784 if ((sym
->type
& N_EXT
) == 0) {
785 sym
->snum
= *numsyms
;
786 *numsyms
= *numsyms
+ 1;
790 if ((sym
->type
& N_TYPE
) != N_UNDF
)
795 /* If we handle debug info we'll want
796 to check for it here instead of just
797 adding the symbol to the string table. */
798 sym
->strx
= *strtabsize
;
799 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
800 *strtabsize
+= strlen(sym
->name
) + 1;
805 /* Next, sort the symbols. Most of this code is a direct translation from
806 the Apple cctools symbol layout. We need to keep compatibility with that. */
807 /* Set the indexes for symbol groups into the symbol table */
809 iextdefsym
= nlocalsym
;
810 iundefsym
= nlocalsym
+ nextdefsym
;
812 /* allocate arrays for sorting externals by name */
813 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
814 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
821 while ((sym
= *symp
)) {
823 if((sym
->type
& N_EXT
) == 0) {
824 sym
->strx
= *strtabsize
;
825 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
826 *strtabsize
+= strlen(sym
->name
) + 1;
829 if((sym
->type
& N_TYPE
) != N_UNDF
)
830 extdefsyms
[i
++] = sym
;
832 undefsyms
[j
++] = sym
;
837 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
838 (int (*)(const void *, const void *))layout_compare
);
839 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
840 (int (*)(const void *, const void *))layout_compare
);
842 for(i
= 0; i
< nextdefsym
; i
++) {
843 extdefsyms
[i
]->snum
= *numsyms
;
846 for(j
= 0; j
< nundefsym
; j
++) {
847 undefsyms
[j
]->snum
= *numsyms
;
852 /* Calculate some values we'll need for writing later. */
854 static void macho_calculate_sizes (void)
858 /* count sections and calculate in-memory and in-file offsets */
859 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
862 /* zerofill sections aren't actually written to the file */
863 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
864 seg_filesize
+= s
->size
;
866 /* recalculate segment address based on alignment and vm size */
867 s
->addr
= seg_vmsize
;
868 /* we need section alignment to calculate final section address */
870 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
872 uint32_t newaddr
= align(s
->addr
, 1 << s
->align
);
873 pad
= newaddr
- s
->addr
;
877 seg_vmsize
+= s
->size
+ pad
;
881 /* calculate size of all headers, load commands and sections to
882 ** get a pointer to the start of all the raw data */
883 if (seg_nsects
> 0) {
886 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
891 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
895 /* Write out the header information for the file. */
897 static void macho_write_header (void)
899 fwriteint32_t(MH_MAGIC
, machofp
); /* magic */
900 fwriteint32_t(CPU_TYPE_I386
, machofp
); /* CPU type */
901 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
902 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
903 fwriteint32_t(head_ncmds
, machofp
); /* number of load commands */
904 fwriteint32_t(head_sizeofcmds
, machofp
); /* size of load commands */
905 fwriteint32_t(0, machofp
); /* no flags */
908 /* Write out the segment load command at offset. */
910 static uint32_t macho_write_segment (uint32_t offset
)
912 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
913 uint32_t s_reloff
= 0;
916 fwriteint32_t(LC_SEGMENT
, machofp
); /* cmd == LC_SEGMENT */
918 /* size of load command including section load commands */
919 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
920 MACHO_SECTCMD_SIZE
, machofp
);
922 /* in an MH_OBJECT file all sections are in one unnamed (name
923 ** all zeros) segment */
924 fwritezero(16, machofp
);
925 fwriteint32_t(0, machofp
); /* in-memory offset */
926 fwriteint32_t(seg_vmsize
, machofp
); /* in-memory size */
927 fwriteint32_t(offset
, machofp
); /* in-file offset to data */
928 fwriteint32_t(seg_filesize
, machofp
); /* in-file size */
929 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
930 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
931 fwriteint32_t(seg_nsects
, machofp
); /* number of sections */
932 fwriteint32_t(0, machofp
); /* no flags */
934 /* emit section headers */
935 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
936 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
937 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
938 fwriteint32_t(s
->addr
, machofp
);
939 fwriteint32_t(s
->size
, machofp
);
941 /* dummy data for zerofill sections or proper values */
942 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
943 fwriteint32_t(offset
, machofp
);
944 /* Write out section alignment, as a power of two.
945 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
947 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
948 fwriteint32_t(s
->align
, machofp
);
949 /* To be compatible with cctools as we emit
950 a zero reloff if we have no relocations. */
951 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
952 fwriteint32_t(s
->nreloc
, machofp
);
955 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
957 fwriteint32_t(0, machofp
);
958 fwriteint32_t(0, machofp
);
959 fwriteint32_t(0, machofp
);
960 fwriteint32_t(0, machofp
);
963 fwriteint32_t(s
->flags
, machofp
); /* flags */
964 fwriteint32_t(0, machofp
); /* reserved */
965 fwriteint32_t(0, machofp
); /* reserved */
968 rel_padcnt
= rel_base
- offset
;
969 offset
= rel_base
+ s_reloff
;
974 /* For a given chain of relocs r, write out the entire relocation
975 chain to the object file. */
977 static void macho_write_relocs (struct reloc
*r
)
982 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
985 word2
|= r
->pcrel
<< 24;
986 word2
|= r
->length
<< 25;
987 word2
|= r
->ext
<< 27;
988 word2
|= r
->type
<< 28;
989 fwriteint32_t(word2
, machofp
); /* reloc data */
995 /* Write out the section data. */
996 static void macho_write_section (void)
998 struct section
*s
, *s2
;
1000 uint8_t *p
, *q
, blk
[4];
1003 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1004 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
1007 /* no padding needs to be done to the sections */
1009 /* Like a.out Mach-O references things in the data or bss
1010 * sections by addresses which are actually relative to the
1011 * start of the _text_ section, in the _file_. See outaout.c
1012 * for more information. */
1013 saa_rewind(s
->data
);
1014 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
1015 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1019 /* get offset based on relocation type */
1020 if (r
->length
> 0) {
1021 l
+= ((int32_t)*p
++) << 8;
1023 if (r
->length
== 2) {
1024 l
+= ((int32_t)*p
++) << 16;
1025 l
+= ((int32_t)*p
++) << 24;
1029 /* If the relocation is internal add to the current section
1030 offset. Otherwise the only value we need is the symbol
1031 offset which we already have. The linker takes care
1032 of the rest of the address. */
1034 /* generate final address by section address and offset */
1035 s2
= get_section_by_index(r
->snum
);
1037 l
+= s2
->addr
; // else what?!?
1040 /* write new offset back */
1043 else if (r
->length
== 1)
1048 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1051 /* dump the section data to file */
1052 saa_fpwrite(s
->data
, machofp
);
1055 /* pad last section up to reloc entries on int32_t boundary */
1056 fwritezero(rel_padcnt
, machofp
);
1058 /* emit relocation entries */
1059 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1060 macho_write_relocs (s
->relocs
);
1063 /* Write out the symbol table. We should already have sorted this
1065 static void macho_write_symtab (void)
1072 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1074 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1075 if ((sym
->type
& N_EXT
) == 0) {
1076 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1077 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1078 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1079 fwriteint16_t(sym
->desc
, machofp
); /* description */
1081 /* Fix up the symbol value now that we know the final section
1083 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1084 for (s
= sects
, fi
= 1;
1085 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1086 sym
->value
+= s
->size
;
1089 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1093 for (i
= 0; i
< nextdefsym
; i
++) {
1094 sym
= extdefsyms
[i
];
1095 fwriteint32_t(sym
->strx
, machofp
);
1096 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1097 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1098 fwriteint16_t(sym
->desc
, machofp
); /* description */
1100 /* Fix up the symbol value now that we know the final section
1102 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1103 for (s
= sects
, fi
= 1;
1104 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1105 sym
->value
+= s
->size
;
1108 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1111 for (i
= 0; i
< nundefsym
; i
++) {
1113 fwriteint32_t(sym
->strx
, machofp
);
1114 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1115 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1116 fwriteint16_t(sym
->desc
, machofp
); /* description */
1118 /* Fix up the symbol value now that we know the final section
1120 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1121 for (s
= sects
, fi
= 1;
1122 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1123 sym
->value
+= s
->size
;
1126 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1130 /* Fixup the snum in the relocation entries, we should be
1131 doing this only for externally undefined symbols. */
1132 static void macho_fixup_relocs (struct reloc
*r
)
1139 for (i
= 0; i
< nundefsym
; i
++) {
1141 if (sym
->initial_snum
== r
->snum
) {
1142 r
->snum
= sym
->snum
;
1151 /* Write out the object file. */
1153 static void macho_write (void)
1155 uint32_t offset
= 0;
1157 /* mach-o object file structure:
1163 ** uint32_t mach file type
1164 ** uint32_t number of load commands
1165 ** uint32_t size of all load commands
1166 ** (includes section struct size of segment command)
1170 ** uint32_t command type == LC_SEGMENT
1171 ** uint32_t size of load command
1172 ** (including section load commands)
1173 ** char[16] segment name
1174 ** uint32_t in-memory offset
1175 ** uint32_t in-memory size
1176 ** uint32_t in-file offset to data area
1177 ** uint32_t in-file size
1178 ** (in-memory size excluding zerofill sections)
1179 ** int maximum vm protection
1180 ** int initial vm protection
1181 ** uint32_t number of sections
1185 ** char[16] section name
1186 ** char[16] segment name
1187 ** uint32_t in-memory offset
1188 ** uint32_t in-memory size
1189 ** uint32_t in-file offset
1190 ** uint32_t alignment
1191 ** (irrelevant in MH_OBJECT)
1192 ** uint32_t in-file offset of relocation entires
1193 ** uint32_t number of relocations
1195 ** uint32_t reserved
1196 ** uint32_t reserved
1198 ** symbol table command
1199 ** uint32_t command type == LC_SYMTAB
1200 ** uint32_t size of load command
1201 ** uint32_t symbol table offset
1202 ** uint32_t number of symbol table entries
1203 ** uint32_t string table offset
1204 ** uint32_t string table size
1208 ** padding to int32_t boundary
1210 ** relocation data (struct reloc)
1212 ** uint data (symbolnum, pcrel, length, extern, type)
1214 ** symbol table data (struct nlist)
1215 ** int32_t string table entry number
1217 ** (extern, absolute, defined in section)
1219 ** (0 for global symbols, section number of definition (>= 1, <=
1220 ** 254) for local symbols, size of variable for common symbols
1221 ** [type == extern])
1222 ** int16_t description
1223 ** (for stab debugging format)
1224 ** uint32_t value (i.e. file offset) of symbol or stab offset
1226 ** string table data
1227 ** list of null-terminated strings
1230 /* Emit the Mach-O header. */
1231 macho_write_header();
1233 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1235 /* emit the segment load command */
1237 offset
= macho_write_segment (offset
);
1239 error(ERR_WARNING
, "no sections?");
1242 /* write out symbol command */
1243 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1244 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1245 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1246 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1249 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1250 fwriteint32_t(offset
, machofp
); /* string table offset */
1251 fwriteint32_t(strslen
, machofp
); /* string table size */
1254 /* emit section data */
1256 macho_write_section ();
1258 /* emit symbol table if we have symbols */
1260 macho_write_symtab ();
1262 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1264 /* emit string table */
1265 saa_fpwrite(strs
, machofp
);
1267 /* We do quite a bit here, starting with finalizing all of the data
1268 for the object file, writing, and then freeing all of the data from
1271 static void macho_cleanup(int debuginfo
)
1279 /* Sort all symbols. */
1280 macho_layout_symbols (&nsyms
, &strslen
);
1282 /* Fixup relocation entries */
1283 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1284 macho_fixup_relocs (s
->relocs
);
1287 /* First calculate and finalize needed values. */
1288 macho_calculate_sizes();
1294 /* free up everything */
1295 while (sects
->next
) {
1297 sects
= sects
->next
;
1300 while (s
->relocs
!= NULL
) {
1302 s
->relocs
= s
->relocs
->next
;
1313 while (syms
->next
) {
1322 /* Debugging routines. */
1323 static void debug_reloc (struct reloc
*r
)
1325 fprintf (stdout
, "reloc:\n");
1326 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1327 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1328 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1329 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1330 fprintf (stdout
, "\text: %d\n", r
->ext
);
1331 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1334 static void debug_section_relocs (struct section
*s
)
1336 struct reloc
*r
= s
->relocs
;
1338 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1346 struct ofmt of_macho
= {
1347 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",