1 /* outmacho.c output routines for the Netwide Assembler to produce
2 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X 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. */
29 /* Mach-O in-file header structure sizes */
30 #define MACHO_HEADER_SIZE (28)
31 #define MACHO_SEGCMD_SIZE (56)
32 #define MACHO_SECTCMD_SIZE (68)
33 #define MACHO_SYMCMD_SIZE (24)
34 #define MACHO_NLIST_SIZE (12)
35 #define MACHO_RELINFO_SIZE (8)
37 /* Mach-O file header values */
38 #define MH_MAGIC (0xfeedface)
39 #define CPU_TYPE_I386 (7) /* x86 platform */
40 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
41 #define MH_OBJECT (0x1) /* object file */
43 #define LC_SEGMENT (0x1) /* segment load command */
44 #define LC_SYMTAB (0x2) /* symbol table load command */
46 #define VM_PROT_NONE (0x00)
47 #define VM_PROT_READ (0x01)
48 #define VM_PROT_WRITE (0x02)
49 #define VM_PROT_EXECUTE (0x04)
51 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
52 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
55 /* nasm internal data */
62 /* data that goes into the file */
63 char sectname
[16]; /* what this section is called */
64 char segname
[16]; /* segment this section will be in */
65 uint32_t size
; /* in-memory and -file size */
66 uint32_t nreloc
; /* relocation entry count */
67 uint32_t flags
; /* type and attributes (masked) */
70 #define SECTION_TYPE 0x000000ff /* section type mask */
72 #define S_REGULAR (0x0) /* standard section */
73 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
75 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
76 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
77 machine instructions */
78 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
80 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
84 static struct sectmap
{
90 {".text", "__TEXT", "__text", S_REGULAR
|S_ATTR_SOME_INSTRUCTIONS
},
91 {".data", "__DATA", "__data", S_REGULAR
},
92 {".rodata", "__DATA", "__const", S_REGULAR
},
93 {".bss", "__DATA", "__bss", S_ZEROFILL
},
98 /* nasm internal data */
101 /* data that goes into the file */
102 int32_t addr
; /* op's offset in section */
103 unsigned int snum
:24, /* contains symbol index if
104 ** ext otherwise in-file
106 pcrel
:1, /* relative relocation */
107 length
:2, /* 0=byte, 1=word, 2=int32_t */
108 ext
:1, /* external symbol referenced */
109 type
:4; /* reloc type, 0 for us */
112 #define R_ABS 0 /* absolute relocation */
113 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
114 ** highest bit == 1 */
117 /* nasm internal data */
118 struct symbol
*next
; /* next symbol in the list */
119 char *name
; /* name of this symbol */
120 int32_t initial_snum
; /* symbol number used above in
122 int32_t snum
; /* true snum for reloc */
124 /* data that goes into the file */
125 int32_t strx
; /* string table index */
126 uint8_t type
; /* symbol type */
127 uint8_t sect
; /* NO_SECT or section number */
128 int16_t desc
; /* for stab debugging, 0 for us */
129 uint32_t value
; /* offset of symbol in section */
132 /* symbol type bits */
133 #define N_EXT 0x01 /* global or external symbol */
135 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
136 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
137 #define N_SECT 0xe /* defined symbol, n_sect holds
140 #define N_TYPE 0x0e /* type bit mask */
142 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
144 /* special section number values */
145 #define NO_SECT 0 /* no section, invalid */
146 #define MAX_SECT 255 /* maximum number of sections */
148 static struct section
*sects
, **sectstail
;
149 static struct symbol
*syms
, **symstail
;
150 static uint32_t nsyms
;
152 /* These variables are set by macho_layout_symbols() to organize
153 the symbol table and string table in order the dynamic linker
154 expects. They are then used in macho_write() to put out the
155 symbols and strings in that order.
157 The order of the symbol table is:
159 defined external symbols (sorted by name)
160 undefined external symbols (sorted by name)
162 The order of the string table is:
163 strings for external symbols
164 strings for local symbols
166 static uint32_t ilocalsym
= 0;
167 static uint32_t iextdefsym
= 0;
168 static uint32_t iundefsym
= 0;
169 static uint32_t nlocalsym
;
170 static uint32_t nextdefsym
;
171 static uint32_t nundefsym
;
172 static struct symbol
**extdefsyms
= NULL
;
173 static struct symbol
**undefsyms
= NULL
;
175 static struct RAA
*extsyms
;
176 static struct SAA
*strs
;
177 static uint32_t strslen
;
179 static FILE *machofp
;
181 static evalfunc evaluate
;
183 extern struct ofmt of_macho
;
185 /* Global file information. This should be cleaned up into either
186 a structure or as function arguments. */
187 uint32_t head_ncmds
= 0;
188 uint32_t head_sizeofcmds
= 0;
189 uint32_t seg_filesize
= 0;
190 uint32_t seg_vmsize
= 0;
191 uint32_t seg_nsects
= 0;
192 uint32_t rel_padcnt
= 0;
195 #define xstrncpy(xdst, xsrc) \
196 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
197 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
198 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
200 #define align(x, y) \
201 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
203 #define alignint32_t(x) \
204 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
206 static void debug_reloc (struct reloc
*);
207 static void debug_section_relocs (struct section
*) _unused
;
209 static int exact_log2 (uint32_t align
)
213 } else if (align
& (align
-1)) {
214 return -1; /* Not a power of 2 */
217 return __builtin_ctzl (align
);
221 /* We know exactly one bit is set at this point. */
222 if (align
& 0xffff0000)
224 if (align
& 0xff00ff00)
226 if (align
& 0xf0f0f0f0)
228 if (align
& 0xcccccccc)
230 if (align
& 0xaaaaaaaa)
238 static struct section
*get_section_by_name(const char *segname
,
239 const char *sectname
)
243 for (s
= sects
; s
!= NULL
; s
= s
->next
)
244 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
250 static struct section
*get_section_by_index(const int32_t index
)
254 for (s
= sects
; s
!= NULL
; s
= s
->next
)
255 if (index
== s
->index
)
261 static int32_t get_section_index_by_name(const char *segname
,
262 const char *sectname
)
266 for (s
= sects
; s
!= NULL
; s
= s
->next
)
267 if (!strcmp(s
->segname
, segname
) && !strcmp(s
->sectname
, sectname
))
273 static char *get_section_name_by_index(const int32_t index
)
277 for (s
= sects
; s
!= NULL
; s
= s
->next
)
278 if (index
== s
->index
)
284 static uint8_t get_section_fileindex_by_index(const int32_t index
)
289 for (s
= sects
; s
!= NULL
&& i
< MAX_SECT
; s
= s
->next
, ++i
)
290 if (index
== s
->index
)
295 "too many sections (>255) - clipped by fileindex");
300 static void macho_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
309 (void)ldef
; /* placate optimisers */
321 extsyms
= raa_init();
324 /* string table starts with a zero byte - don't ask why */
325 saa_wbytes(strs
, &zero
, sizeof(char));
329 static int macho_setinfo(enum geninfo type
, char **val
)
336 static void sect_write(struct section
*sect
,
337 const uint8_t *data
, uint32_t len
)
339 saa_wbytes(sect
->data
, data
, len
);
343 static void add_reloc(struct section
*sect
, int32_t section
,
344 int pcrel
, int bytes
)
349 /* NeXT as puts relocs in reversed order (address-wise) into the
350 ** files, so we do the same, doesn't seem to make much of a
351 ** difference either way */
352 r
= nasm_malloc(sizeof(struct reloc
));
353 r
->next
= sect
->relocs
;
356 /* the current end of the section will be the symbol's address for
357 ** now, might have to be fixed by macho_fixup_relocs() later on. make
358 ** sure we don't make the symbol scattered by setting the highest
359 ** bit by accident */
360 r
->addr
= sect
->size
& ~R_SCATTERED
;
364 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
365 r
->length
= bytes
>> 1;
367 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
370 if (section
== NO_SEG
) {
371 /* absolute local symbol if no section index given */
374 fi
= get_section_fileindex_by_index(section
);
377 /* external symbol if no section with that index known,
378 ** symbol number was saved in macho_symdef() */
379 r
->snum
= raa_read(extsyms
, section
);
382 /* local symbol in section fi */
390 static void macho_output(int32_t secto
, const void *data
,
391 enum out_type type
, uint64_t size
,
392 int32_t section
, int32_t wrt
)
394 struct section
*s
, *sbss
;
396 uint8_t mydata
[4], *p
;
400 error(ERR_NONFATAL
, "WRT not supported by Mach-O output format");
401 /* continue to do _something_ */
404 if (secto
== NO_SEG
) {
405 if (type
!= OUT_RESERVE
)
406 error(ERR_NONFATAL
, "attempt to assemble code in "
412 s
= get_section_by_index(secto
);
415 error(ERR_WARNING
, "attempt to assemble code in"
416 " section %d: defaulting to `.text'", secto
);
417 s
= get_section_by_name("__TEXT", "__text");
419 /* should never happen */
421 error(ERR_PANIC
, "text section not found");
424 sbss
= get_section_by_name("__DATA", "__bss");
426 if (s
== sbss
&& type
!= OUT_RESERVE
) {
427 error(ERR_WARNING
, "attempt to initialize memory in the"
428 " BSS section: ignored");
450 error(ERR_WARNING
, "uninitialized space declared in"
451 " %s section: zeroing",
452 get_section_name_by_index(secto
));
454 sect_write(s
, NULL
, size
);
461 if (section
!= NO_SEG
)
462 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
464 sect_write(s
, data
, size
);
468 addr
= *(int64_t *)data
;
470 if (section
!= NO_SEG
) {
472 error(ERR_NONFATAL
, "Mach-O format does not support"
473 " section base references");
475 add_reloc(s
, section
, 0, size
);
479 WRITEADDR(p
, addr
, size
);
480 sect_write(s
, mydata
, size
);
484 if (section
== secto
)
485 error(ERR_PANIC
, "intra-section OUT_REL2ADR");
487 if (section
!= NO_SEG
&& section
% 2) {
488 error(ERR_NONFATAL
, "Mach-O format does not support"
489 " section base references");
491 add_reloc(s
, section
, 1, 2);
494 WRITESHORT(p
, *(int32_t *)data
- (size
+ s
->size
));
495 sect_write(s
, mydata
, 2L);
499 if (section
== secto
)
500 error(ERR_PANIC
, "intra-section OUT_REL4ADR");
502 if (section
!= NO_SEG
&& section
% 2) {
503 error(ERR_NONFATAL
, "Mach-O format does not support"
504 " section base references");
506 add_reloc(s
, section
, 1, 4);
509 WRITELONG(p
, *(int32_t *)data
- (size
+ s
->size
));
510 sect_write(s
, mydata
, 4L);
514 error(ERR_PANIC
, "unknown output type?");
519 static int32_t macho_section(char *name
, int pass
, int *bits
)
521 int32_t index
, originalIndex
;
522 char *sectionAttributes
;
528 /* Default to 32 bits. */
532 sectionAttributes
= NULL
;
534 sectionAttributes
= name
;
535 name
= nasm_strsep(§ionAttributes
, " \t");
538 for (sm
= sectmap
; sm
->nasmsect
!= NULL
; ++sm
) {
539 /* make lookup into section name translation table */
540 if (!strcmp(name
, sm
->nasmsect
)) {
541 char *currentAttribute
;
543 /* try to find section with that name */
544 originalIndex
= index
= get_section_index_by_name(sm
->segname
,
547 /* create it if it doesn't exist yet */
549 s
= *sectstail
= nasm_malloc(sizeof(struct section
));
551 sectstail
= &s
->next
;
553 s
->data
= saa_init(1L);
554 s
->index
= seg_alloc();
558 xstrncpy(s
->segname
, sm
->segname
);
559 xstrncpy(s
->sectname
, sm
->sectname
);
562 s
->flags
= sm
->flags
;
566 s
= get_section_by_index(index
);
569 while ((NULL
!= sectionAttributes
)
570 && (currentAttribute
= nasm_strsep(§ionAttributes
, " \t"))) {
571 if (0 != *currentAttribute
) {
572 if (!nasm_strnicmp("align=", currentAttribute
, 6)) {
574 int newAlignment
, value
;
576 value
= strtoul(currentAttribute
+ 6, (char**)&end
, 0);
577 newAlignment
= exact_log2(value
);
581 "unknown or missing alignment value \"%s\" "
582 "specified for section \"%s\"",
583 currentAttribute
+ 6,
586 } else if (0 > newAlignment
) {
588 "alignment of %d (for section \"%s\") is not "
595 if ((-1 != originalIndex
)
596 && (s
->align
!= newAlignment
)
597 && (s
->align
!= -1)) {
599 "section \"%s\" has already been specified "
600 "with alignment %d, conflicts with new "
608 s
->align
= newAlignment
;
609 } else if (!nasm_stricmp("data", currentAttribute
)) {
610 /* Do nothing; 'data' is implicit */
613 "unknown section attribute %s for section %s",
625 error(ERR_PANIC
, "invalid section name %s", name
);
629 static void macho_symdef(char *name
, int32_t section
, int64_t offset
,
630 int is_global
, char *special
)
635 error(ERR_NONFATAL
, "The Mach-O output format does "
636 "not support any special symbol types");
640 if (is_global
== 3) {
641 error(ERR_NONFATAL
, "The Mach-O format does not "
642 "(yet) support forward reference fixups.");
646 sym
= *symstail
= nasm_malloc(sizeof(struct symbol
));
648 symstail
= &sym
->next
;
655 sym
->initial_snum
= -1;
657 /* external and common symbols get N_EXT */
661 if (section
== NO_SEG
) {
662 /* symbols in no section get absolute */
668 /* get the in-file index of the section the symbol was defined in */
669 sym
->sect
= get_section_fileindex_by_index(section
);
671 if (sym
->sect
== NO_SECT
) {
672 /* remember symbol number of references to external
673 ** symbols, this works because every external symbol gets
674 ** its own section number allocated internally by nasm and
675 ** can so be used as a key */
676 extsyms
= raa_write(extsyms
, section
, nsyms
);
677 sym
->initial_snum
= nsyms
;
682 /* there isn't actually a difference between global
683 ** and common symbols, both even have their size in
689 /* give an error on unfound section if it's not an
690 ** external or common symbol (assemble_file() does a
691 ** seg_alloc() on every call for them) */
692 error(ERR_PANIC
, "in-file index for section %d not found",
701 static int32_t macho_segbase(int32_t section
)
706 static int macho_directive(char *directive
, char *value
, int pass
)
714 static void macho_filename(char *inname
, char *outname
, efunc error
)
716 standard_extension(inname
, outname
, ".o", error
);
719 static const char *macho_stdmac
[] = {
720 "%define __SECT__ [section .text]",
721 "%macro __NASM_CDecl__ 1",
726 /* Comparison function for qsort symbol layout. */
727 static int layout_compare (const struct symbol
**s1
,
728 const struct symbol
**s2
)
730 return (strcmp ((*s1
)->name
, (*s2
)->name
));
733 /* The native assembler does a few things in a similar function
735 * Remove temporary labels
736 * Sort symbols according to local, external, undefined (by name)
737 * Order the string table
739 We do not remove temporary labels right now.
741 numsyms is the total number of symbols we have. strtabsize is the
742 number entries in the string table. */
744 static void macho_layout_symbols (uint32_t *numsyms
,
745 uint32_t *strtabsize
)
747 struct symbol
*sym
, **symp
;
751 *strtabsize
= sizeof (char);
755 while ((sym
= *symp
)) {
756 /* Undefined symbols are now external. */
757 if (sym
->type
== N_UNDF
)
760 if ((sym
->type
& N_EXT
) == 0) {
761 sym
->snum
= *numsyms
;
762 *numsyms
= *numsyms
+ 1;
766 if ((sym
->type
& N_TYPE
) != N_UNDF
)
771 /* If we handle debug info we'll want
772 to check for it here instead of just
773 adding the symbol to the string table. */
774 sym
->strx
= *strtabsize
;
775 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen(sym
->name
) + 1));
776 *strtabsize
+= strlen(sym
->name
) + 1;
781 /* Next, sort the symbols. Most of this code is a direct translation from
782 the Apple cctools symbol layout. We need to keep compatibility with that. */
783 /* Set the indexes for symbol groups into the symbol table */
785 iextdefsym
= nlocalsym
;
786 iundefsym
= nlocalsym
+ nextdefsym
;
788 /* allocate arrays for sorting externals by name */
789 extdefsyms
= nasm_malloc(nextdefsym
* sizeof(struct symbol
*));
790 undefsyms
= nasm_malloc(nundefsym
* sizeof(struct symbol
*));
797 while ((sym
= *symp
)) {
799 if((sym
->type
& N_EXT
) == 0) {
800 sym
->strx
= *strtabsize
;
801 saa_wbytes (strs
, sym
->name
, (int32_t)(strlen (sym
->name
) + 1));
802 *strtabsize
+= strlen(sym
->name
) + 1;
805 if((sym
->type
& N_TYPE
) != N_UNDF
)
806 extdefsyms
[i
++] = sym
;
808 undefsyms
[j
++] = sym
;
813 qsort(extdefsyms
, nextdefsym
, sizeof(struct symbol
*),
814 (int (*)(const void *, const void *))layout_compare
);
815 qsort(undefsyms
, nundefsym
, sizeof(struct symbol
*),
816 (int (*)(const void *, const void *))layout_compare
);
818 for(i
= 0; i
< nextdefsym
; i
++) {
819 extdefsyms
[i
]->snum
= *numsyms
;
822 for(j
= 0; j
< nundefsym
; j
++) {
823 undefsyms
[j
]->snum
= *numsyms
;
828 /* Calculate some values we'll need for writing later. */
830 static void macho_calculate_sizes (void)
834 /* count sections and calculate in-memory and in-file offsets */
835 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
836 /* zerofill sections aren't actually written to the file */
837 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
)
838 seg_filesize
+= s
->size
;
840 seg_vmsize
+= s
->size
;
844 /* calculate size of all headers, load commands and sections to
845 ** get a pointer to the start of all the raw data */
846 if (seg_nsects
> 0) {
849 MACHO_SEGCMD_SIZE
+ seg_nsects
* MACHO_SECTCMD_SIZE
;
854 head_sizeofcmds
+= MACHO_SYMCMD_SIZE
;
858 /* Write out the header information for the file. */
860 static void macho_write_header (void)
862 fwriteint32_t(MH_MAGIC
, machofp
); /* magic */
863 fwriteint32_t(CPU_TYPE_I386
, machofp
); /* CPU type */
864 fwriteint32_t(CPU_SUBTYPE_I386_ALL
, machofp
); /* CPU subtype */
865 fwriteint32_t(MH_OBJECT
, machofp
); /* Mach-O file type */
866 fwriteint32_t(head_ncmds
, machofp
); /* number of load commands */
867 fwriteint32_t(head_sizeofcmds
, machofp
); /* size of load commands */
868 fwriteint32_t(0, machofp
); /* no flags */
871 /* Write out the segment load command at offset. */
873 static uint32_t macho_write_segment (uint32_t offset
)
876 uint32_t rel_base
= alignint32_t (offset
+ seg_filesize
);
877 uint32_t s_reloff
= 0;
880 fwriteint32_t(LC_SEGMENT
, machofp
); /* cmd == LC_SEGMENT */
882 /* size of load command including section load commands */
883 fwriteint32_t(MACHO_SEGCMD_SIZE
+ seg_nsects
*
884 MACHO_SECTCMD_SIZE
, machofp
);
886 /* in an MH_OBJECT file all sections are in one unnamed (name
887 ** all zeros) segment */
888 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp
);
889 fwriteint32_t(0, machofp
); /* in-memory offset */
890 fwriteint32_t(seg_vmsize
, machofp
); /* in-memory size */
891 fwriteint32_t(offset
, machofp
); /* in-file offset to data */
892 fwriteint32_t(seg_filesize
, machofp
); /* in-file size */
893 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* maximum vm protection */
894 fwriteint32_t(VM_PROT_DEFAULT
, machofp
); /* initial vm protection */
895 fwriteint32_t(seg_nsects
, machofp
); /* number of sections */
896 fwriteint32_t(0, machofp
); /* no flags */
898 /* emit section headers */
899 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
900 fwrite(s
->sectname
, sizeof(s
->sectname
), 1, machofp
);
901 fwrite(s
->segname
, sizeof(s
->segname
), 1, machofp
);
902 fwriteint32_t(s_addr
, machofp
);
903 fwriteint32_t(s
->size
, machofp
);
905 /* dummy data for zerofill sections or proper values */
906 if ((s
->flags
& SECTION_TYPE
) != S_ZEROFILL
) {
907 fwriteint32_t(offset
, machofp
);
908 /* Write out section alignment, as a power of two.
909 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
911 s
->align
= DEFAULT_SECTION_ALIGNMENT
;
912 fwriteint32_t(s
->align
, machofp
);
913 /* To be compatible with cctools as we emit
914 a zero reloff if we have no relocations. */
915 fwriteint32_t(s
->nreloc
? rel_base
+ s_reloff
: 0, machofp
);
916 fwriteint32_t(s
->nreloc
, machofp
);
919 s_reloff
+= s
->nreloc
* MACHO_RELINFO_SIZE
;
921 fwriteint32_t(0, machofp
);
922 fwriteint32_t(0, machofp
);
923 fwriteint32_t(0, machofp
);
924 fwriteint32_t(0, machofp
);
927 fwriteint32_t(s
->flags
, machofp
); /* flags */
928 fwriteint32_t(0, machofp
); /* reserved */
929 fwriteint32_t(0, machofp
); /* reserved */
934 rel_padcnt
= rel_base
- offset
;
935 offset
= rel_base
+ s_reloff
;
940 /* For a given chain of relocs r, write out the entire relocation
941 chain to the object file. */
943 static void macho_write_relocs (struct reloc
*r
)
948 fwriteint32_t(r
->addr
, machofp
); /* reloc offset */
951 word2
|= r
->pcrel
<< 24;
952 word2
|= r
->length
<< 25;
953 word2
|= r
->ext
<< 27;
954 word2
|= r
->type
<< 28;
955 fwriteint32_t(word2
, machofp
); /* reloc data */
961 /* Write out the section data. */
962 static void macho_write_section (void)
964 struct section
*s
, *s2
;
966 char *rel_paddata
= "\0\0\0";
967 uint8_t fi
, *p
, *q
, blk
[4];
970 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
971 if ((s
->flags
& SECTION_TYPE
) == S_ZEROFILL
)
974 /* no padding needs to be done to the sections */
976 /* Like a.out Mach-O references things in the data or bss
977 * sections by addresses which are actually relative to the
978 * start of the _text_ section, in the _file_. See outaout.c
979 * for more information. */
981 for (r
= s
->relocs
; r
!= NULL
; r
= r
->next
) {
982 saa_fread(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
986 /* get offset based on relocation type */
988 l
+= ((int32_t)*p
++) << 8;
990 if (r
->length
== 2) {
991 l
+= ((int32_t)*p
++) << 16;
992 l
+= ((int32_t)*p
++) << 24;
996 /* If the relocation is internal add to the current section
997 offset. Otherwise the only value we need is the symbol
998 offset which we already have. The linker takes care
999 of the rest of the address. */
1001 /* add sizes of previous sections to current offset */
1002 for (s2
= sects
, fi
= 1;
1003 s2
!= NULL
&& fi
< r
->snum
; s2
= s2
->next
, fi
++)
1007 /* write new offset back */
1010 else if (r
->length
== 1)
1015 saa_fwrite(s
->data
, r
->addr
, blk
, (int32_t)r
->length
<< 1);
1018 /* dump the section data to file */
1019 saa_fpwrite(s
->data
, machofp
);
1022 /* pad last section up to reloc entries on int32_t boundary */
1023 fwrite(rel_paddata
, rel_padcnt
, 1, machofp
);
1025 /* emit relocation entries */
1026 for (s
= sects
; s
!= NULL
; s
= s
->next
)
1027 macho_write_relocs (s
->relocs
);
1030 /* Write out the symbol table. We should already have sorted this
1032 static void macho_write_symtab (void)
1039 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1041 for (sym
= syms
; sym
!= NULL
; sym
= sym
->next
) {
1042 if ((sym
->type
& N_EXT
) == 0) {
1043 fwriteint32_t(sym
->strx
, machofp
); /* string table entry number */
1044 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1045 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1046 fwriteint16_t(sym
->desc
, machofp
); /* description */
1048 /* Fix up the symbol value now that we know the final section
1050 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1051 for (s
= sects
, fi
= 1;
1052 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1053 sym
->value
+= s
->size
;
1056 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1060 for (i
= 0; i
< nextdefsym
; i
++) {
1061 sym
= extdefsyms
[i
];
1062 fwriteint32_t(sym
->strx
, machofp
);
1063 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1064 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1065 fwriteint16_t(sym
->desc
, machofp
); /* description */
1067 /* Fix up the symbol value now that we know the final section
1069 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1070 for (s
= sects
, fi
= 1;
1071 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1072 sym
->value
+= s
->size
;
1075 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1078 for (i
= 0; i
< nundefsym
; i
++) {
1080 fwriteint32_t(sym
->strx
, machofp
);
1081 fwrite(&sym
->type
, 1, 1, machofp
); /* symbol type */
1082 fwrite(&sym
->sect
, 1, 1, machofp
); /* section */
1083 fwriteint16_t(sym
->desc
, machofp
); /* description */
1085 /* Fix up the symbol value now that we know the final section
1087 if (((sym
->type
& N_TYPE
) == N_SECT
) && (sym
->sect
!= NO_SECT
)) {
1088 for (s
= sects
, fi
= 1;
1089 s
!= NULL
&& fi
< sym
->sect
; s
= s
->next
, ++fi
)
1090 sym
->value
+= s
->size
;
1093 fwriteint32_t(sym
->value
, machofp
); /* value (i.e. offset) */
1097 /* Fixup the snum in the relocation entries, we should be
1098 doing this only for externally undefined symbols. */
1099 static void macho_fixup_relocs (struct reloc
*r
)
1106 for (i
= 0; i
< nundefsym
; i
++) {
1108 if (sym
->initial_snum
== r
->snum
) {
1109 r
->snum
= sym
->snum
;
1118 /* Write out the object file. */
1120 static void macho_write (void)
1122 uint32_t offset
= 0;
1124 /* mach-o object file structure:
1130 ** uint32_t mach file type
1131 ** uint32_t number of load commands
1132 ** uint32_t size of all load commands
1133 ** (includes section struct size of segment command)
1137 ** uint32_t command type == LC_SEGMENT
1138 ** uint32_t size of load command
1139 ** (including section load commands)
1140 ** char[16] segment name
1141 ** uint32_t in-memory offset
1142 ** uint32_t in-memory size
1143 ** uint32_t in-file offset to data area
1144 ** uint32_t in-file size
1145 ** (in-memory size excluding zerofill sections)
1146 ** int maximum vm protection
1147 ** int initial vm protection
1148 ** uint32_t number of sections
1152 ** char[16] section name
1153 ** char[16] segment name
1154 ** uint32_t in-memory offset
1155 ** uint32_t in-memory size
1156 ** uint32_t in-file offset
1157 ** uint32_t alignment
1158 ** (irrelevant in MH_OBJECT)
1159 ** uint32_t in-file offset of relocation entires
1160 ** uint32_t number of relocations
1162 ** uint32_t reserved
1163 ** uint32_t reserved
1165 ** symbol table command
1166 ** uint32_t command type == LC_SYMTAB
1167 ** uint32_t size of load command
1168 ** uint32_t symbol table offset
1169 ** uint32_t number of symbol table entries
1170 ** uint32_t string table offset
1171 ** uint32_t string table size
1175 ** padding to int32_t boundary
1177 ** relocation data (struct reloc)
1179 ** uint data (symbolnum, pcrel, length, extern, type)
1181 ** symbol table data (struct nlist)
1182 ** int32_t string table entry number
1184 ** (extern, absolute, defined in section)
1186 ** (0 for global symbols, section number of definition (>= 1, <=
1187 ** 254) for local symbols, size of variable for common symbols
1188 ** [type == extern])
1189 ** int16_t description
1190 ** (for stab debugging format)
1191 ** uint32_t value (i.e. file offset) of symbol or stab offset
1193 ** string table data
1194 ** list of null-terminated strings
1197 /* Emit the Mach-O header. */
1198 macho_write_header();
1200 offset
= MACHO_HEADER_SIZE
+ head_sizeofcmds
;
1202 /* emit the segment load command */
1204 offset
= macho_write_segment (offset
);
1206 error(ERR_WARNING
, "no sections?");
1209 /* write out symbol command */
1210 fwriteint32_t(LC_SYMTAB
, machofp
); /* cmd == LC_SYMTAB */
1211 fwriteint32_t(MACHO_SYMCMD_SIZE
, machofp
); /* size of load command */
1212 fwriteint32_t(offset
, machofp
); /* symbol table offset */
1213 fwriteint32_t(nsyms
, machofp
); /* number of symbol
1216 offset
+= nsyms
* MACHO_NLIST_SIZE
;
1217 fwriteint32_t(offset
, machofp
); /* string table offset */
1218 fwriteint32_t(strslen
, machofp
); /* string table size */
1221 /* emit section data */
1223 macho_write_section ();
1225 /* emit symbol table if we have symbols */
1227 macho_write_symtab ();
1229 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1231 /* emit string table */
1232 saa_fpwrite(strs
, machofp
);
1234 /* We do quite a bit here, starting with finalizing all of the data
1235 for the object file, writing, and then freeing all of the data from
1238 static void macho_cleanup(int debuginfo
)
1246 /* Sort all symbols. */
1247 macho_layout_symbols (&nsyms
, &strslen
);
1249 /* Fixup relocation entries */
1250 for (s
= sects
; s
!= NULL
; s
= s
->next
) {
1251 macho_fixup_relocs (s
->relocs
);
1254 /* First calculate and finalize needed values. */
1255 macho_calculate_sizes();
1261 /* free up everything */
1262 while (sects
->next
) {
1264 sects
= sects
->next
;
1267 while (s
->relocs
!= NULL
) {
1269 s
->relocs
= s
->relocs
->next
;
1280 while (syms
->next
) {
1289 /* Debugging routines. */
1290 static void debug_reloc (struct reloc
*r
)
1292 fprintf (stdout
, "reloc:\n");
1293 fprintf (stdout
, "\taddr: %"PRId32
"\n", r
->addr
);
1294 fprintf (stdout
, "\tsnum: %d\n", r
->snum
);
1295 fprintf (stdout
, "\tpcrel: %d\n", r
->pcrel
);
1296 fprintf (stdout
, "\tlength: %d\n", r
->length
);
1297 fprintf (stdout
, "\text: %d\n", r
->ext
);
1298 fprintf (stdout
, "\ttype: %d\n", r
->type
);
1301 static void debug_section_relocs (struct section
*s
)
1303 struct reloc
*r
= s
->relocs
;
1305 fprintf (stdout
, "relocs for section %s:\n\n", s
->sectname
);
1313 struct ofmt of_macho
= {
1314 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",