c99 printf/fprintf compliance.
[nasm.git] / output / outmacho.c
blobee2f4204188196989cd05795363c27a856b82776
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 licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 /* Most of this file is, like Mach-O itself, based on a.out. For more
11 * guidelines see outaout.c. */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <inttypes.h>
19 #include "nasm.h"
20 #include "nasmlib.h"
21 #include "outform.h"
22 #include "compiler.h"
24 #if defined(OF_MACHO)
26 /* Mach-O in-file header structure sizes */
27 #define MACHO_HEADER_SIZE (28)
28 #define MACHO_SEGCMD_SIZE (56)
29 #define MACHO_SECTCMD_SIZE (68)
30 #define MACHO_SYMCMD_SIZE (24)
31 #define MACHO_NLIST_SIZE (12)
32 #define MACHO_RELINFO_SIZE (8)
34 /* Mach-O file header values */
35 #define MH_MAGIC (0xfeedface)
36 #define CPU_TYPE_I386 (7) /* x86 platform */
37 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
38 #define MH_OBJECT (0x1) /* object file */
40 #define LC_SEGMENT (0x1) /* segment load command */
41 #define LC_SYMTAB (0x2) /* symbol table load command */
43 #define VM_PROT_NONE (0x00)
44 #define VM_PROT_READ (0x01)
45 #define VM_PROT_WRITE (0x02)
46 #define VM_PROT_EXECUTE (0x04)
48 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
49 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
51 struct section {
52 /* nasm internal data */
53 struct section *next;
54 struct SAA *data;
55 int32_t index;
56 struct reloc *relocs;
57 int align;
59 /* data that goes into the file */
60 char sectname[16]; /* what this section is called */
61 char segname[16]; /* segment this section will be in */
62 uint32_t size; /* in-memory and -file size */
63 uint32_t nreloc; /* relocation entry count */
64 uint32_t flags; /* type and attributes (masked) */
67 #define SECTION_TYPE 0x000000ff /* section type mask */
69 #define S_REGULAR (0x0) /* standard section */
70 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
72 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
73 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
74 machine instructions */
75 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
76 relocation entries */
77 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
78 relocation entries */
81 static struct sectmap {
82 const char *nasmsect;
83 const char *segname;
84 const char *sectname;
85 const int32_t flags;
86 } sectmap[] = { {
87 ".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS}, {
88 ".data", "__DATA", "__data", S_REGULAR}, {
89 ".rodata", "__DATA", "__const", S_REGULAR}, {
90 ".bss", "__DATA", "__bss", S_ZEROFILL}, {
91 NULL, NULL, NULL}};
93 struct reloc {
94 /* nasm internal data */
95 struct reloc *next;
97 /* data that goes into the file */
98 int32_t addr; /* op's offset in section */
99 unsigned int snum:24, /* contains symbol index if
100 ** ext otherwise in-file
101 ** section number */
102 pcrel:1, /* relative relocation */
103 length:2, /* 0=byte, 1=word, 2=int32_t */
104 ext:1, /* external symbol referenced */
105 type:4; /* reloc type, 0 for us */
108 #define R_ABS 0 /* absolute relocation */
109 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
110 ** highest bit == 1 */
112 struct symbol {
113 /* nasm internal data */
114 struct symbol *next; /* next symbol in the list */
115 char *name; /* name of this symbol */
116 int32_t initial_snum; /* symbol number used above in
117 reloc */
118 int32_t snum; /* true snum for reloc */
120 /* data that goes into the file */
121 int32_t strx; /* string table index */
122 uint8_t type; /* symbol type */
123 uint8_t sect; /* NO_SECT or section number */
124 int16_t desc; /* for stab debugging, 0 for us */
125 uint32_t value; /* offset of symbol in section */
128 /* symbol type bits */
129 #define N_EXT 0x01 /* global or external symbol */
131 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
132 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
133 #define N_SECT 0xe /* defined symbol, n_sect holds
134 ** section number */
136 #define N_TYPE 0x0e /* type bit mask */
138 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
140 /* special section number values */
141 #define NO_SECT 0 /* no section, invalid */
142 #define MAX_SECT 255 /* maximum number of sections */
144 static struct section *sects, **sectstail;
145 static struct symbol *syms, **symstail;
146 static uint32_t nsyms;
148 /* These variables are set by macho_layout_symbols() to organize
149 the symbol table and string table in order the dynamic linker
150 expects. They are then used in macho_write() to put out the
151 symbols and strings in that order.
153 The order of the symbol table is:
154 local symbols
155 defined external symbols (sorted by name)
156 undefined external symbols (sorted by name)
158 The order of the string table is:
159 strings for external symbols
160 strings for local symbols
162 static uint32_t ilocalsym = 0;
163 static uint32_t iextdefsym = 0;
164 static uint32_t iundefsym = 0;
165 static uint32_t nlocalsym;
166 static uint32_t nextdefsym;
167 static uint32_t nundefsym;
168 static struct symbol **extdefsyms = NULL;
169 static struct symbol **undefsyms = NULL;
171 static struct RAA *extsyms;
172 static struct SAA *strs;
173 static uint32_t strslen;
175 static FILE *machofp;
176 static efunc error;
177 static evalfunc evaluate;
179 extern struct ofmt of_macho;
181 /* Global file information. This should be cleaned up into either
182 a structure or as function arguments. */
183 uint32_t head_ncmds = 0;
184 uint32_t head_sizeofcmds = 0;
185 uint32_t seg_filesize = 0;
186 uint32_t seg_vmsize = 0;
187 uint32_t seg_nsects = 0;
188 uint32_t rel_padcnt = 0;
191 #define xstrncpy(xdst, xsrc) \
192 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
193 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
194 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
196 #define align(x, y) \
197 (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */
199 #define alignint32_t(x) \
200 align(x, sizeof(int32_t)) /* align x to int32_t boundary */
202 static void debug_reloc (struct reloc *);
203 static void debug_section_relocs (struct section *) _unused;
205 static int exact_log2 (uint32_t align)
207 if (align == 0) {
208 return 0;
209 } else if (align & (align-1)) {
210 return -1; /* Not a power of 2 */
211 } else {
212 #ifdef HAVE_GNUC_4
213 return __builtin_ctzl (align);
214 #else
215 uint32_t result = 0;
217 /* We know exactly one bit is set at this point. */
218 if (align & 0xffff0000)
219 result |= 16;
220 if (align & 0xff00ff00)
221 result |= 8;
222 if (align & 0xf0f0f0f0)
223 result |= 4;
224 if (align & 0xcccccccc)
225 result |= 2;
226 if (align & 0xaaaaaaaa)
227 result |= 1;
229 return result;
230 #endif
234 static struct section *get_section_by_name(const char *segname,
235 const char *sectname)
237 struct section *s;
239 for (s = sects; s != NULL; s = s->next)
240 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
241 break;
243 return s;
246 static struct section *get_section_by_index(const int32_t index)
248 struct section *s;
250 for (s = sects; s != NULL; s = s->next)
251 if (index == s->index)
252 break;
254 return s;
257 static int32_t get_section_index_by_name(const char *segname,
258 const char *sectname)
260 struct section *s;
262 for (s = sects; s != NULL; s = s->next)
263 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
264 return s->index;
266 return -1;
269 static char *get_section_name_by_index(const int32_t index)
271 struct section *s;
273 for (s = sects; s != NULL; s = s->next)
274 if (index == s->index)
275 return s->sectname;
277 return NULL;
280 static uint8_t get_section_fileindex_by_index(const int32_t index)
282 struct section *s;
283 uint8_t i = 1;
285 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
286 if (index == s->index)
287 return i;
289 if (i == MAX_SECT)
290 error(ERR_WARNING,
291 "too many sections (>255) - clipped by fileindex");
293 return NO_SECT;
296 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
297 evalfunc eval)
299 char zero = 0;
301 machofp = fp;
302 error = errfunc;
303 evaluate = eval;
305 (void)ldef; /* placate optimisers */
307 sects = NULL;
308 sectstail = &sects;
310 syms = NULL;
311 symstail = &syms;
312 nsyms = 0;
313 nlocalsym = 0;
314 nextdefsym = 0;
315 nundefsym = 0;
317 extsyms = raa_init();
318 strs = saa_init(1L);
320 /* string table starts with a zero byte - don't ask why */
321 saa_wbytes(strs, &zero, sizeof(char));
322 strslen = 1;
325 static int macho_setinfo(enum geninfo type, char **val)
327 return 0;
330 static void sect_write(struct section *sect,
331 const uint8_t *data, uint32_t len)
333 saa_wbytes(sect->data, data, len);
334 sect->size += len;
337 static void add_reloc(struct section *sect, int32_t section,
338 int pcrel, int bytes)
340 struct reloc *r;
341 int32_t fi;
343 /* NeXT as puts relocs in reversed order (address-wise) into the
344 ** files, so we do the same, doesn't seem to make much of a
345 ** difference either way */
346 r = nasm_malloc(sizeof(struct reloc));
347 r->next = sect->relocs;
348 sect->relocs = r;
350 /* the current end of the section will be the symbol's address for
351 ** now, might have to be fixed by macho_fixup_relocs() later on. make
352 ** sure we don't make the symbol scattered by setting the highest
353 ** bit by accident */
354 r->addr = sect->size & ~R_SCATTERED;
355 r->ext = 0;
356 r->pcrel = pcrel;
358 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
359 r->length = bytes >> 1;
361 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
362 r->type = 0;
364 if (section == NO_SEG) {
365 /* absolute local symbol if no section index given */
366 r->snum = R_ABS;
367 } else {
368 fi = get_section_fileindex_by_index(section);
370 if (fi == NO_SECT) {
371 /* external symbol if no section with that index known,
372 ** symbol number was saved in macho_symdef() */
373 r->snum = raa_read(extsyms, section);
374 r->ext = 1;
375 } else {
376 /* local symbol in section fi */
377 r->snum = fi;
381 ++sect->nreloc;
384 static void macho_output(int32_t secto, const void *data, uint32_t type,
385 int32_t section, int32_t wrt)
387 struct section *s, *sbss;
388 int32_t realbytes = type & OUT_SIZMASK;
389 int32_t addr;
390 uint8_t mydata[4], *p;
392 type &= OUT_TYPMASK;
394 if (wrt != NO_SEG) {
395 wrt = NO_SEG;
396 error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
397 /* continue to do _something_ */
400 if (secto == NO_SEG) {
401 if (type != OUT_RESERVE)
402 error(ERR_NONFATAL, "attempt to assemble code in "
403 "[ABSOLUTE] space");
405 return;
408 s = get_section_by_index(secto);
410 if (s == NULL) {
411 error(ERR_WARNING, "attempt to assemble code in"
412 " section %d: defaulting to `.text'", secto);
413 s = get_section_by_name("__TEXT", "__text");
415 /* should never happen */
416 if (s == NULL)
417 error(ERR_PANIC, "text section not found");
420 sbss = get_section_by_name("__DATA", "__bss");
422 if (s == sbss && type != OUT_RESERVE) {
423 error(ERR_WARNING, "attempt to initialize memory in the"
424 " BSS section: ignored");
426 switch (type) {
427 case OUT_REL2ADR:
428 realbytes = 2;
429 break;
431 case OUT_REL4ADR:
432 realbytes = 4;
433 break;
435 default:
436 break;
439 s->size += realbytes;
440 return;
443 switch (type) {
444 case OUT_RESERVE:
445 if (s != sbss) {
446 error(ERR_WARNING, "uninitialized space declared in"
447 " %s section: zeroing",
448 get_section_name_by_index(secto));
450 sect_write(s, NULL, realbytes);
451 } else
452 s->size += realbytes;
454 break;
456 case OUT_RAWDATA:
457 if (section != NO_SEG)
458 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
460 sect_write(s, data, realbytes);
461 break;
463 case OUT_ADDRESS:
464 addr = *(int32_t *)data;
466 if (section != NO_SEG) {
467 if (section % 2) {
468 error(ERR_NONFATAL, "Mach-O format does not support"
469 " section base references");
470 } else
471 add_reloc(s, section, 0, realbytes);
474 p = mydata;
476 if (realbytes == 2)
477 WRITESHORT(p, addr);
478 else
479 WRITELONG(p, addr);
481 sect_write(s, mydata, realbytes);
482 break;
484 case OUT_REL2ADR:
485 if (section == secto)
486 error(ERR_PANIC, "intra-section OUT_REL2ADR");
488 if (section != NO_SEG && section % 2) {
489 error(ERR_NONFATAL, "Mach-O format does not support"
490 " section base references");
491 } else
492 add_reloc(s, section, 1, 2);
494 p = mydata;
495 WRITESHORT(p, *(int32_t *)data - (realbytes + s->size));
496 sect_write(s, mydata, 2L);
497 break;
499 case OUT_REL4ADR:
500 if (section == secto)
501 error(ERR_PANIC, "intra-section OUT_REL4ADR");
503 if (section != NO_SEG && section % 2) {
504 error(ERR_NONFATAL, "Mach-O format does not support"
505 " section base references");
506 } else
507 add_reloc(s, section, 1, 4);
509 p = mydata;
510 WRITELONG(p, *(int32_t *)data - (realbytes + s->size));
511 sect_write(s, mydata, 4L);
512 break;
514 default:
515 error(ERR_PANIC, "unknown output type?");
516 break;
520 static int32_t macho_section(char *name, int pass, int *bits)
522 int32_t index, originalIndex;
523 char *sectionAttributes;
524 struct sectmap *sm;
525 struct section *s;
527 /* Default to 32 bits. */
528 if (!name) {
529 *bits = 32;
530 name = ".text";
531 sectionAttributes = NULL;
532 } else {
533 sectionAttributes = name;
534 name = strtok((char*)&sectionAttributes, " \t");
537 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
538 /* make lookup into section name translation table */
539 if (!strcmp(name, sm->nasmsect)) {
540 char *currentAttribute;
542 /* try to find section with that name */
543 originalIndex = index = get_section_index_by_name(sm->segname,
544 sm->sectname);
546 /* create it if it doesn't exist yet */
547 if (index == -1) {
548 s = *sectstail = nasm_malloc(sizeof(struct section));
549 s->next = NULL;
550 sectstail = &s->next;
552 s->data = saa_init(1L);
553 s->index = seg_alloc();
554 s->relocs = NULL;
555 s->align = DEFAULT_SECTION_ALIGNMENT;
557 xstrncpy(s->segname, sm->segname);
558 xstrncpy(s->sectname, sm->sectname);
559 s->size = 0;
560 s->nreloc = 0;
561 s->flags = sm->flags;
563 index = s->index;
564 } else {
565 s = get_section_by_index(index);
568 while ((NULL != sectionAttributes)
569 && (currentAttribute = strtok((char*)&sectionAttributes, " \t"))) {
570 if (0 != *currentAttribute) {
571 if (!(nasm_strnicmp("align=", currentAttribute, 6))) {
572 char *end;
573 int newAlignment, value;
575 value = strtoul(currentAttribute + 6, (char**)&end, 0);
576 newAlignment = exact_log2(value);
578 if (0 != *end) {
579 error(ERR_PANIC,
580 "unknown or missing alignment value \"%s\" "
581 "specified for section \"%s\"",
582 currentAttribute + 6,
583 name);
584 return NO_SEG;
585 } else if (0 > newAlignment) {
586 error(ERR_PANIC,
587 "alignment of %d (for section \"%s\") is not "
588 "a power of two",
589 value,
590 name);
591 return NO_SEG;
594 if ((-1 != originalIndex)
595 && (s->align != newAlignment)) {
596 error(ERR_PANIC,
597 "section \"%s\" has already been specified "
598 "with alignment %d, conflicts with new "
599 "alignment of %d",
600 name,
601 (1 << s->align),
602 value);
603 return NO_SEG;
606 s->align = newAlignment;
607 } else if (!(nasm_stricmp("data", currentAttribute))) {
608 /* Do nothing; 'data' is implicit */
609 } else {
610 error(ERR_PANIC,
611 "unknown section attribute %s for section %s",
612 currentAttribute,
613 name);
614 return NO_SEG;
619 return index;
623 error(ERR_PANIC, "invalid section name %s", name);
624 return NO_SEG;
627 static void macho_symdef(char *name, int32_t section, int32_t offset,
628 int is_global, char *special)
630 struct symbol *sym;
632 if (special) {
633 error(ERR_NONFATAL, "The Mach-O output format does "
634 "not support any special symbol types");
635 return;
638 if (is_global == 3) {
639 error(ERR_NONFATAL, "The Mach-O format does not "
640 "(yet) support forward reference fixups.");
641 return;
644 sym = *symstail = nasm_malloc(sizeof(struct symbol));
645 sym->next = NULL;
646 symstail = &sym->next;
648 sym->name = name;
649 sym->strx = strslen;
650 sym->type = 0;
651 sym->desc = 0;
652 sym->value = offset;
653 sym->initial_snum = -1;
655 /* external and common symbols get N_EXT */
656 if (is_global != 0)
657 sym->type |= N_EXT;
659 if (section == NO_SEG) {
660 /* symbols in no section get absolute */
661 sym->type |= N_ABS;
662 sym->sect = NO_SECT;
663 } else {
664 sym->type |= N_SECT;
666 /* get the in-file index of the section the symbol was defined in */
667 sym->sect = get_section_fileindex_by_index(section);
669 if (sym->sect == NO_SECT) {
670 /* remember symbol number of references to external
671 ** symbols, this works because every external symbol gets
672 ** its own section number allocated internally by nasm and
673 ** can so be used as a key */
674 extsyms = raa_write(extsyms, section, nsyms);
675 sym->initial_snum = nsyms;
677 switch (is_global) {
678 case 1:
679 case 2:
680 /* there isn't actually a difference between global
681 ** and common symbols, both even have their size in
682 ** sym->value */
683 sym->type = N_EXT;
684 break;
686 default:
687 /* give an error on unfound section if it's not an
688 ** external or common symbol (assemble_file() does a
689 ** seg_alloc() on every call for them) */
690 error(ERR_PANIC, "in-file index for section %d not found",
691 section);
696 ++nsyms;
699 static int32_t macho_segbase(int32_t section)
701 return section;
704 static int macho_directive(char *directive, char *value, int pass)
706 return 0;
709 static void macho_filename(char *inname, char *outname, efunc error)
711 standard_extension(inname, outname, ".o", error);
714 static const char *macho_stdmac[] = {
715 "%define __SECT__ [section .text]",
716 "%macro __NASM_CDecl__ 1",
717 "%endmacro",
718 NULL
721 /* Comparison function for qsort symbol layout. */
722 static int layout_compare (const struct symbol **s1,
723 const struct symbol **s2)
725 return (strcmp ((*s1)->name, (*s2)->name));
728 /* The native assembler does a few things in a similar function
730 * Remove temporary labels
731 * Sort symbols according to local, external, undefined (by name)
732 * Order the string table
734 We do not remove temporary labels right now.
736 numsyms is the total number of symbols we have. strtabsize is the
737 number entries in the string table. */
739 static void macho_layout_symbols (uint32_t *numsyms,
740 uint32_t *strtabsize)
742 struct symbol *sym, **symp;
743 uint32_t i,j;
745 *numsyms = 0;
746 *strtabsize = sizeof (char);
748 symp = &syms;
750 while ((sym = *symp)) {
751 /* Undefined symbols are now external. */
752 if (sym->type == N_UNDF)
753 sym->type |= N_EXT;
755 if ((sym->type & N_EXT) == 0) {
756 sym->snum = *numsyms;
757 *numsyms = *numsyms + 1;
758 nlocalsym++;
760 else {
761 if ((sym->type & N_TYPE) != N_UNDF)
762 nextdefsym++;
763 else
764 nundefsym++;
766 /* If we handle debug info we'll want
767 to check for it here instead of just
768 adding the symbol to the string table. */
769 sym->strx = *strtabsize;
770 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
771 *strtabsize += strlen(sym->name) + 1;
773 symp = &(sym->next);
776 /* Next, sort the symbols. Most of this code is a direct translation from
777 the Apple cctools symbol layout. We need to keep compatibility with that. */
778 /* Set the indexes for symbol groups into the symbol table */
779 ilocalsym = 0;
780 iextdefsym = nlocalsym;
781 iundefsym = nlocalsym + nextdefsym;
783 /* allocate arrays for sorting externals by name */
784 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
785 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
787 i = 0;
788 j = 0;
790 symp = &syms;
792 while ((sym = *symp)) {
794 if((sym->type & N_EXT) == 0) {
795 sym->strx = *strtabsize;
796 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
797 *strtabsize += strlen(sym->name) + 1;
799 else {
800 if((sym->type & N_TYPE) != N_UNDF)
801 extdefsyms[i++] = sym;
802 else
803 undefsyms[j++] = sym;
805 symp = &(sym->next);
808 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
809 (int (*)(const void *, const void *))layout_compare);
810 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
811 (int (*)(const void *, const void *))layout_compare);
813 for(i = 0; i < nextdefsym; i++) {
814 extdefsyms[i]->snum = *numsyms;
815 *numsyms += 1;
817 for(j = 0; j < nundefsym; j++) {
818 undefsyms[j]->snum = *numsyms;
819 *numsyms += 1;
823 /* Calculate some values we'll need for writing later. */
825 static void macho_calculate_sizes (void)
827 struct section *s;
829 /* count sections and calculate in-memory and in-file offsets */
830 for (s = sects; s != NULL; s = s->next) {
831 /* zerofill sections aren't actually written to the file */
832 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
833 seg_filesize += s->size;
835 seg_vmsize += s->size;
836 ++seg_nsects;
839 /* calculate size of all headers, load commands and sections to
840 ** get a pointer to the start of all the raw data */
841 if (seg_nsects > 0) {
842 ++head_ncmds;
843 head_sizeofcmds +=
844 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
847 if (nsyms > 0) {
848 ++head_ncmds;
849 head_sizeofcmds += MACHO_SYMCMD_SIZE;
853 /* Write out the header information for the file. */
855 static void macho_write_header (void)
857 fwriteint32_t(MH_MAGIC, machofp); /* magic */
858 fwriteint32_t(CPU_TYPE_I386, machofp); /* CPU type */
859 fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp); /* CPU subtype */
860 fwriteint32_t(MH_OBJECT, machofp); /* Mach-O file type */
861 fwriteint32_t(head_ncmds, machofp); /* number of load commands */
862 fwriteint32_t(head_sizeofcmds, machofp); /* size of load commands */
863 fwriteint32_t(0, machofp); /* no flags */
866 /* Write out the segment load command at offset. */
868 static uint32_t macho_write_segment (uint32_t offset)
870 uint32_t s_addr = 0;
871 uint32_t rel_base = alignint32_t (offset + seg_filesize);
872 uint32_t s_reloff = 0;
873 struct section *s;
875 fwriteint32_t(LC_SEGMENT, machofp); /* cmd == LC_SEGMENT */
877 /* size of load command including section load commands */
878 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
879 MACHO_SECTCMD_SIZE, machofp);
881 /* in an MH_OBJECT file all sections are in one unnamed (name
882 ** all zeros) segment */
883 fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp);
884 fwriteint32_t(0, machofp); /* in-memory offset */
885 fwriteint32_t(seg_vmsize, machofp); /* in-memory size */
886 fwriteint32_t(offset, machofp); /* in-file offset to data */
887 fwriteint32_t(seg_filesize, machofp); /* in-file size */
888 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* maximum vm protection */
889 fwriteint32_t(VM_PROT_DEFAULT, machofp); /* initial vm protection */
890 fwriteint32_t(seg_nsects, machofp); /* number of sections */
891 fwriteint32_t(0, machofp); /* no flags */
893 /* emit section headers */
894 for (s = sects; s != NULL; s = s->next) {
895 fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
896 fwrite(s->segname, sizeof(s->segname), 1, machofp);
897 fwriteint32_t(s_addr, machofp);
898 fwriteint32_t(s->size, machofp);
900 /* dummy data for zerofill sections or proper values */
901 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
902 fwriteint32_t(offset, machofp);
903 /* Write out section alignment, as a power of two.
904 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
905 fwriteint32_t(s->align, machofp);
906 /* To be compatible with cctools as we emit
907 a zero reloff if we have no relocations. */
908 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
909 fwriteint32_t(s->nreloc, machofp);
911 offset += s->size;
912 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
913 } else {
914 fwriteint32_t(0, machofp);
915 fwriteint32_t(0, machofp);
916 fwriteint32_t(0, machofp);
917 fwriteint32_t(0, machofp);
920 fwriteint32_t(s->flags, machofp); /* flags */
921 fwriteint32_t(0, machofp); /* reserved */
922 fwriteint32_t(0, machofp); /* reserved */
924 s_addr += s->size;
927 rel_padcnt = rel_base - offset;
928 offset = rel_base + s_reloff;
930 return offset;
933 /* For a given chain of relocs r, write out the entire relocation
934 chain to the object file. */
936 static void macho_write_relocs (struct reloc *r)
938 while (r) {
939 uint32_t word2;
941 fwriteint32_t(r->addr, machofp); /* reloc offset */
943 word2 = r->snum;
944 word2 |= r->pcrel << 24;
945 word2 |= r->length << 25;
946 word2 |= r->ext << 27;
947 word2 |= r->type << 28;
948 fwriteint32_t(word2, machofp); /* reloc data */
950 r = r->next;
954 /* Write out the section data. */
955 static void macho_write_section (void)
957 struct section *s, *s2;
958 struct reloc *r;
959 char *rel_paddata = "\0\0\0";
960 uint8_t fi, *p, *q, blk[4];
961 int32_t l;
963 for (s = sects; s != NULL; s = s->next) {
964 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
965 continue;
967 /* no padding needs to be done to the sections */
969 /* Like a.out Mach-O references things in the data or bss
970 * sections by addresses which are actually relative to the
971 * start of the _text_ section, in the _file_. See outaout.c
972 * for more information. */
973 saa_rewind(s->data);
974 for (r = s->relocs; r != NULL; r = r->next) {
975 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
976 p = q = blk;
977 l = *p++;
979 /* get offset based on relocation type */
980 if (r->length > 0) {
981 l += ((int32_t)*p++) << 8;
983 if (r->length == 2) {
984 l += ((int32_t)*p++) << 16;
985 l += ((int32_t)*p++) << 24;
989 /* If the relocation is internal add to the current section
990 offset. Otherwise the only value we need is the symbol
991 offset which we already have. The linker takes care
992 of the rest of the address. */
993 if (!r->ext) {
994 /* add sizes of previous sections to current offset */
995 for (s2 = sects, fi = 1;
996 s2 != NULL && fi < r->snum; s2 = s2->next, fi++)
997 if ((s2->flags & SECTION_TYPE) != S_ZEROFILL)
998 l += s2->size;
1001 /* write new offset back */
1002 if (r->length == 2)
1003 WRITELONG(q, l);
1004 else if (r->length == 1)
1005 WRITESHORT(q, l);
1006 else
1007 *q++ = l & 0xFF;
1009 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1012 /* dump the section data to file */
1013 saa_fpwrite(s->data, machofp);
1016 /* pad last section up to reloc entries on int32_t boundary */
1017 fwrite(rel_paddata, rel_padcnt, 1, machofp);
1019 /* emit relocation entries */
1020 for (s = sects; s != NULL; s = s->next)
1021 macho_write_relocs (s->relocs);
1024 /* Write out the symbol table. We should already have sorted this
1025 before now. */
1026 static void macho_write_symtab (void)
1028 struct symbol *sym;
1029 struct section *s;
1030 int32_t fi;
1031 int32_t i;
1033 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1035 for (sym = syms; sym != NULL; sym = sym->next) {
1036 if ((sym->type & N_EXT) == 0) {
1037 fwriteint32_t(sym->strx, machofp); /* string table entry number */
1038 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1039 fwrite(&sym->sect, 1, 1, machofp); /* section */
1040 fwriteint16_t(sym->desc, machofp); /* description */
1042 /* Fix up the symbol value now that we know the final section
1043 sizes. */
1044 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1045 for (s = sects, fi = 1;
1046 s != NULL && fi < sym->sect; s = s->next, ++fi)
1047 sym->value += s->size;
1050 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1054 for (i = 0; i < nextdefsym; i++) {
1055 sym = extdefsyms[i];
1056 fwriteint32_t(sym->strx, machofp);
1057 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1058 fwrite(&sym->sect, 1, 1, machofp); /* section */
1059 fwriteint16_t(sym->desc, machofp); /* description */
1061 /* Fix up the symbol value now that we know the final section
1062 sizes. */
1063 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1064 for (s = sects, fi = 1;
1065 s != NULL && fi < sym->sect; s = s->next, ++fi)
1066 sym->value += s->size;
1069 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1072 for (i = 0; i < nundefsym; i++) {
1073 sym = undefsyms[i];
1074 fwriteint32_t(sym->strx, machofp);
1075 fwrite(&sym->type, 1, 1, machofp); /* symbol type */
1076 fwrite(&sym->sect, 1, 1, machofp); /* section */
1077 fwriteint16_t(sym->desc, machofp); /* description */
1079 /* Fix up the symbol value now that we know the final section
1080 sizes. */
1081 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1082 for (s = sects, fi = 1;
1083 s != NULL && fi < sym->sect; s = s->next, ++fi)
1084 sym->value += s->size;
1087 fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1091 /* Fixup the snum in the relocation entries, we should be
1092 doing this only for externally undefined symbols. */
1093 static void macho_fixup_relocs (struct reloc *r)
1095 struct symbol *sym;
1096 int i;
1098 while (r != NULL) {
1099 if (r->ext) {
1100 for (i = 0; i < nundefsym; i++) {
1101 sym = undefsyms[i];
1102 if (sym->initial_snum == r->snum) {
1103 r->snum = sym->snum;
1107 r = r->next;
1111 /* Write out the object file. */
1113 static void macho_write (void)
1115 uint32_t offset = 0;
1117 /* mach-o object file structure:
1119 ** mach header
1120 ** uint32_t magic
1121 ** int cpu type
1122 ** int cpu subtype
1123 ** uint32_t mach file type
1124 ** uint32_t number of load commands
1125 ** uint32_t size of all load commands
1126 ** (includes section struct size of segment command)
1127 ** uint32_t flags
1129 ** segment command
1130 ** uint32_t command type == LC_SEGMENT
1131 ** uint32_t size of load command
1132 ** (including section load commands)
1133 ** char[16] segment name
1134 ** uint32_t in-memory offset
1135 ** uint32_t in-memory size
1136 ** uint32_t in-file offset to data area
1137 ** uint32_t in-file size
1138 ** (in-memory size excluding zerofill sections)
1139 ** int maximum vm protection
1140 ** int initial vm protection
1141 ** uint32_t number of sections
1142 ** uint32_t flags
1144 ** section commands
1145 ** char[16] section name
1146 ** char[16] segment name
1147 ** uint32_t in-memory offset
1148 ** uint32_t in-memory size
1149 ** uint32_t in-file offset
1150 ** uint32_t alignment
1151 ** (irrelevant in MH_OBJECT)
1152 ** uint32_t in-file offset of relocation entires
1153 ** uint32_t number of relocations
1154 ** uint32_t flags
1155 ** uint32_t reserved
1156 ** uint32_t reserved
1158 ** symbol table command
1159 ** uint32_t command type == LC_SYMTAB
1160 ** uint32_t size of load command
1161 ** uint32_t symbol table offset
1162 ** uint32_t number of symbol table entries
1163 ** uint32_t string table offset
1164 ** uint32_t string table size
1166 ** raw section data
1168 ** padding to int32_t boundary
1170 ** relocation data (struct reloc)
1171 ** int32_t offset
1172 ** uint data (symbolnum, pcrel, length, extern, type)
1174 ** symbol table data (struct nlist)
1175 ** int32_t string table entry number
1176 ** uint8_t type
1177 ** (extern, absolute, defined in section)
1178 ** uint8_t section
1179 ** (0 for global symbols, section number of definition (>= 1, <=
1180 ** 254) for local symbols, size of variable for common symbols
1181 ** [type == extern])
1182 ** int16_t description
1183 ** (for stab debugging format)
1184 ** uint32_t value (i.e. file offset) of symbol or stab offset
1186 ** string table data
1187 ** list of null-terminated strings
1190 /* Emit the Mach-O header. */
1191 macho_write_header();
1193 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1195 /* emit the segment load command */
1196 if (seg_nsects > 0)
1197 offset = macho_write_segment (offset);
1198 else
1199 error(ERR_WARNING, "no sections?");
1201 if (nsyms > 0) {
1202 /* write out symbol command */
1203 fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1204 fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1205 fwriteint32_t(offset, machofp); /* symbol table offset */
1206 fwriteint32_t(nsyms, machofp); /* number of symbol
1207 ** table entries */
1209 offset += nsyms * MACHO_NLIST_SIZE;
1210 fwriteint32_t(offset, machofp); /* string table offset */
1211 fwriteint32_t(strslen, machofp); /* string table size */
1214 /* emit section data */
1215 if (seg_nsects > 0)
1216 macho_write_section ();
1218 /* emit symbol table if we have symbols */
1219 if (nsyms > 0)
1220 macho_write_symtab ();
1222 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1224 /* emit string table */
1225 saa_fpwrite(strs, machofp);
1227 /* We do quite a bit here, starting with finalizing all of the data
1228 for the object file, writing, and then freeing all of the data from
1229 the file. */
1231 static void macho_cleanup(int debuginfo)
1233 struct section *s;
1234 struct reloc *r;
1235 struct symbol *sym;
1237 (void)debuginfo;
1239 /* Sort all symbols. */
1240 macho_layout_symbols (&nsyms, &strslen);
1242 /* Fixup relocation entries */
1243 for (s = sects; s != NULL; s = s->next) {
1244 macho_fixup_relocs (s->relocs);
1247 /* First calculate and finalize needed values. */
1248 macho_calculate_sizes();
1249 macho_write();
1251 /* done - yay! */
1252 fclose(machofp);
1254 /* free up everything */
1255 while (sects->next) {
1256 s = sects;
1257 sects = sects->next;
1259 saa_free(s->data);
1260 while (s->relocs != NULL) {
1261 r = s->relocs;
1262 s->relocs = s->relocs->next;
1263 nasm_free(r);
1266 nasm_free(s);
1269 saa_free(strs);
1270 raa_free(extsyms);
1272 if (syms) {
1273 while (syms->next) {
1274 sym = syms;
1275 syms = syms->next;
1277 nasm_free (sym);
1282 /* Debugging routines. */
1283 static void debug_reloc (struct reloc *r)
1285 fprintf (stdout, "reloc:\n");
1286 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1287 fprintf (stdout, "\tsnum: %d\n", r->snum);
1288 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1289 fprintf (stdout, "\tlength: %d\n", r->length);
1290 fprintf (stdout, "\text: %d\n", r->ext);
1291 fprintf (stdout, "\ttype: %d\n", r->type);
1294 static void debug_section_relocs (struct section *s)
1296 struct reloc *r = s->relocs;
1298 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1300 while (r != NULL) {
1301 debug_reloc (r);
1302 r = r->next;
1306 struct ofmt of_macho = {
1307 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files",
1308 "macho",
1309 NULL,
1310 null_debug_arr,
1311 &null_debug_form,
1312 macho_stdmac,
1313 macho_init,
1314 macho_setinfo,
1315 macho_output,
1316 macho_symdef,
1317 macho_section,
1318 macho_segbase,
1319 macho_directive,
1320 macho_filename,
1321 macho_cleanup
1324 #endif
1327 * Local Variables:
1328 * mode:c
1329 * c-basic-offset:4
1330 * End:
1332 * end of file */