Hash even backend-specific directives, unify null functions
[platform/upstream/nasm.git] / output / outmacho32.c
1 /* ----------------------------------------------------------------------- *
2  *   
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.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following
9  *   conditions are met:
10  *
11  *   * Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above
14  *     copyright notice, this list of conditions and the following
15  *     disclaimer in the documentation and/or other materials provided
16  *     with the distribution.
17  *     
18  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * ----------------------------------------------------------------------- */
33
34 /*
35  * outmacho.c   output routines for the Netwide Assembler to produce
36  *              NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
37  */
38
39 /* Most of this file is, like Mach-O itself, based on a.out. For more
40  * guidelines see outaout.c.  */
41
42 #include "compiler.h"
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <inttypes.h>
49
50 #include "nasm.h"
51 #include "nasmlib.h"
52 #include "saa.h"
53 #include "raa.h"
54 #include "output/outform.h"
55 #include "output/outlib.h"
56
57 #if defined(OF_MACHO32)
58
59 /* Mach-O in-file header structure sizes */
60 #define MACHO_HEADER_SIZE       (28)
61 #define MACHO_SEGCMD_SIZE       (56)
62 #define MACHO_SECTCMD_SIZE      (68)
63 #define MACHO_SYMCMD_SIZE       (24)
64 #define MACHO_NLIST_SIZE        (12)
65 #define MACHO_RELINFO_SIZE      (8)
66
67 /* Mach-O file header values */
68 #define MH_MAGIC                (0xfeedface)
69 #define CPU_TYPE_I386           (7)     /* x86 platform */
70 #define CPU_SUBTYPE_I386_ALL    (3)     /* all-x86 compatible */
71 #define MH_OBJECT               (0x1)   /* object file */
72
73 #define LC_SEGMENT              (0x1)   /* segment load command */
74 #define LC_SYMTAB               (0x2)   /* symbol table load command */
75
76 #define VM_PROT_NONE    (0x00)
77 #define VM_PROT_READ    (0x01)
78 #define VM_PROT_WRITE   (0x02)
79 #define VM_PROT_EXECUTE (0x04)
80
81 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
82 #define VM_PROT_ALL     (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
83
84 struct section {
85     /* nasm internal data */
86     struct section *next;
87     struct SAA *data;
88     int32_t index;
89     struct reloc *relocs;
90     int align;
91
92     /* data that goes into the file */
93     char sectname[16];          /* what this section is called */
94     char segname[16];           /* segment this section will be in */
95     uint32_t addr;         /* in-memory address (subject to alignment) */
96     uint32_t size;         /* in-memory and -file size  */
97     uint32_t nreloc;       /* relocation entry count */
98     uint32_t flags;        /* type and attributes (masked) */
99 };
100
101 #define SECTION_TYPE    0x000000ff      /* section type mask */
102
103 #define S_REGULAR       (0x0)   /* standard section */
104 #define S_ZEROFILL      (0x1)   /* zerofill, in-memory only */
105
106 #define SECTION_ATTRIBUTES_SYS   0x00ffff00     /* system setable attributes */
107 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400     /* section contains some
108                                                    machine instructions */
109 #define S_ATTR_EXT_RELOC         0x00000200     /* section has external
110                                                    relocation entries */
111 #define S_ATTR_LOC_RELOC         0x00000100     /* section has local
112                                                    relocation entries */
113
114
115 static struct sectmap {
116     const char *nasmsect;
117     const char *segname;
118     const char *sectname;
119     const int32_t flags;
120 } sectmap[] = {
121     {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
122     {".data", "__DATA", "__data", S_REGULAR},
123     {".rodata", "__DATA", "__const", S_REGULAR},
124     {".bss", "__DATA", "__bss", S_ZEROFILL},
125     {NULL, NULL, NULL, 0}
126 };
127
128 struct reloc {
129     /* nasm internal data */
130     struct reloc *next;
131
132     /* data that goes into the file */
133     int32_t addr;                  /* op's offset in section */
134     unsigned int snum:24,       /* contains symbol index if
135                                 ** ext otherwise in-file
136                                 ** section number */
137         pcrel:1,                /* relative relocation */
138         length:2,               /* 0=byte, 1=word, 2=int32_t */
139         ext:1,                  /* external symbol referenced */
140         type:4;                 /* reloc type, 0 for us */
141 };
142
143 #define R_ABS           0       /* absolute relocation */
144 #define R_SCATTERED     0x80000000      /* reloc entry is scattered if
145                                         ** highest bit == 1 */
146
147 struct symbol {
148     /* nasm internal data */
149     struct symbol *next;        /* next symbol in the list */
150     char *name;                 /* name of this symbol */
151     int32_t initial_snum;               /* symbol number used above in
152                                    reloc */
153     int32_t snum;                       /* true snum for reloc */
154
155     /* data that goes into the file */
156     int32_t strx;                  /* string table index */
157     uint8_t type;         /* symbol type */
158     uint8_t sect;         /* NO_SECT or section number */
159     int16_t desc;                 /* for stab debugging, 0 for us */
160     uint32_t value;        /* offset of symbol in section */
161 };
162
163 /* symbol type bits */
164 #define N_EXT   0x01            /* global or external symbol */
165
166 #define N_UNDF  0x0             /* undefined symbol | n_sect == */
167 #define N_ABS   0x2             /* absolute symbol  |  NO_SECT */
168 #define N_SECT  0xe             /* defined symbol, n_sect holds
169                                 ** section number */
170
171 #define N_TYPE  0x0e            /* type bit mask */
172
173 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
174
175 /* special section number values */
176 #define NO_SECT         0       /* no section, invalid */
177 #define MAX_SECT        255     /* maximum number of sections */
178
179 static struct section *sects, **sectstail;
180 static struct symbol *syms, **symstail;
181 static uint32_t nsyms;
182
183 /* These variables are set by macho_layout_symbols() to organize
184    the symbol table and string table in order the dynamic linker
185    expects.  They are then used in macho_write() to put out the
186    symbols and strings in that order.
187
188    The order of the symbol table is:
189      local symbols
190      defined external symbols (sorted by name)
191      undefined external symbols (sorted by name)
192
193    The order of the string table is:
194      strings for external symbols
195      strings for local symbols
196  */
197 static uint32_t ilocalsym = 0;
198 static uint32_t iextdefsym = 0;
199 static uint32_t iundefsym = 0;
200 static uint32_t nlocalsym;
201 static uint32_t nextdefsym;
202 static uint32_t nundefsym;
203 static struct symbol **extdefsyms = NULL;
204 static struct symbol **undefsyms = NULL;
205
206 static struct RAA *extsyms;
207 static struct SAA *strs;
208 static uint32_t strslen;
209
210 static FILE *machofp;
211 static efunc error;
212 static evalfunc evaluate;
213
214 extern struct ofmt of_macho;
215
216 /* Global file information. This should be cleaned up into either
217    a structure or as function arguments.  */
218 uint32_t head_ncmds = 0;
219 uint32_t head_sizeofcmds = 0;
220 uint32_t seg_filesize = 0;
221 uint32_t seg_vmsize = 0;
222 uint32_t seg_nsects = 0;
223 uint32_t rel_padcnt = 0;
224
225
226 #define xstrncpy(xdst, xsrc)                                            \
227     memset(xdst, '\0', sizeof(xdst));   /* zero out whole buffer */     \
228     strncpy(xdst, xsrc, sizeof(xdst));  /* copy over string */          \
229     xdst[sizeof(xdst) - 1] = '\0';      /* proper null-termination */
230
231 #define align(x, y)                                                     \
232     (((x) + (y) - 1) & ~((y) - 1))      /* align x to multiple of y */
233
234 #define alignint32_t(x)                                                 \
235     align(x, sizeof(int32_t))      /* align x to int32_t boundary */
236
237 static void debug_reloc (struct reloc *);
238 static void debug_section_relocs (struct section *) _unused;
239
240 static int exact_log2 (uint32_t align)
241 {
242     if (align == 0) {
243         return 0;
244     } else if (align & (align-1)) {
245         return -1;              /* Not a power of 2 */
246     } else {
247 #ifdef HAVE_GNUC_4
248         return __builtin_ctzl (align);
249 #else
250         uint32_t result = 0;
251
252         /* We know exactly one bit is set at this point. */
253         if (align & 0xffff0000)
254             result |= 16;
255         if (align & 0xff00ff00)
256             result |= 8;
257         if (align & 0xf0f0f0f0)
258             result |= 4;
259         if (align & 0xcccccccc)
260             result |= 2;
261         if (align & 0xaaaaaaaa)
262             result |= 1;
263
264         return result;
265 #endif
266     }
267 }
268
269 static struct section *get_section_by_name(const char *segname,
270                                            const char *sectname)
271 {
272     struct section *s;
273
274     for (s = sects; s != NULL; s = s->next)
275         if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
276             break;
277
278     return s;
279 }
280
281 static struct section *get_section_by_index(const int32_t index)
282 {
283     struct section *s;
284
285     for (s = sects; s != NULL; s = s->next)
286         if (index == s->index)
287             break;
288
289     return s;
290 }
291
292 static int32_t get_section_index_by_name(const char *segname,
293                                       const char *sectname)
294 {
295     struct section *s;
296
297     for (s = sects; s != NULL; s = s->next)
298         if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
299             return s->index;
300
301     return -1;
302 }
303
304 static char *get_section_name_by_index(const int32_t index)
305 {
306     struct section *s;
307
308     for (s = sects; s != NULL; s = s->next)
309         if (index == s->index)
310             return s->sectname;
311
312     return NULL;
313 }
314
315 static uint8_t get_section_fileindex_by_index(const int32_t index)
316 {
317     struct section *s;
318     uint8_t i = 1;
319
320     for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
321         if (index == s->index)
322             return i;
323
324     if (i == MAX_SECT)
325         error(ERR_WARNING,
326               "too many sections (>255) - clipped by fileindex");
327
328     return NO_SECT;
329 }
330
331 static void macho_init(FILE * fp, efunc errfunc, ldfunc ldef,
332                        evalfunc eval)
333 {
334     char zero = 0;
335
336     machofp = fp;
337     error = errfunc;
338     evaluate = eval;
339
340     (void)ldef;                 /* placate optimisers */
341
342     sects = NULL;
343     sectstail = &sects;
344
345     syms = NULL;
346     symstail = &syms;
347     nsyms = 0;
348     nlocalsym = 0;
349     nextdefsym = 0;
350     nundefsym = 0;
351
352     extsyms = raa_init();
353     strs = saa_init(1L);
354
355     /* string table starts with a zero byte - don't ask why */
356     saa_wbytes(strs, &zero, sizeof(char));
357     strslen = 1;
358 }
359
360 static void sect_write(struct section *sect,
361                        const uint8_t *data, uint32_t len)
362 {
363     saa_wbytes(sect->data, data, len);
364     sect->size += len;
365 }
366
367 static void add_reloc(struct section *sect, int32_t section,
368                       int pcrel, int bytes)
369 {
370     struct reloc *r;
371     int32_t fi;
372
373     /* NeXT as puts relocs in reversed order (address-wise) into the
374      ** files, so we do the same, doesn't seem to make much of a
375      ** difference either way */
376     r = nasm_malloc(sizeof(struct reloc));
377     r->next = sect->relocs;
378     sect->relocs = r;
379
380     /* the current end of the section will be the symbol's address for
381      ** now, might have to be fixed by macho_fixup_relocs() later on. make
382      ** sure we don't make the symbol scattered by setting the highest
383      ** bit by accident */
384     r->addr = sect->size & ~R_SCATTERED;
385     r->ext = 0;
386     r->pcrel = pcrel;
387
388     /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
389     r->length = bytes >> 1;
390
391     /* vanilla relocation (GENERIC_RELOC_VANILLA) */
392     r->type = 0;
393
394     if (section == NO_SEG) {
395         /* absolute local symbol if no section index given */
396         r->snum = R_ABS;
397     } else {
398         fi = get_section_fileindex_by_index(section);
399
400         if (fi == NO_SECT) {
401             /* external symbol if no section with that index known,
402              ** symbol number was saved in macho_symdef() */
403             r->snum = raa_read(extsyms, section);
404             r->ext = 1;
405         } else {
406             /* local symbol in section fi */
407             r->snum = fi;
408         }
409     }
410
411     ++sect->nreloc;
412 }
413
414 static void macho_output(int32_t secto, const void *data,
415                          enum out_type type, uint64_t size,
416                          int32_t section, int32_t wrt)
417 {
418     struct section *s, *sbss;
419     int32_t addr;
420     uint8_t mydata[4], *p;
421
422     if (wrt != NO_SEG) {
423         wrt = NO_SEG;
424         error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
425         /* continue to do _something_ */
426     }
427
428     if (secto == NO_SEG) {
429         if (type != OUT_RESERVE)
430             error(ERR_NONFATAL, "attempt to assemble code in "
431                   "[ABSOLUTE] space");
432
433         return;
434     }
435
436     s = get_section_by_index(secto);
437
438     if (s == NULL) {
439         error(ERR_WARNING, "attempt to assemble code in"
440               " section %d: defaulting to `.text'", secto);
441         s = get_section_by_name("__TEXT", "__text");
442
443         /* should never happen */
444         if (s == NULL)
445             error(ERR_PANIC, "text section not found");
446     }
447
448     sbss = get_section_by_name("__DATA", "__bss");
449
450     if (s == sbss && type != OUT_RESERVE) {
451         error(ERR_WARNING, "attempt to initialize memory in the"
452               " BSS section: ignored");
453         s->size += realsize(type, size);
454         return;
455     }
456
457     switch (type) {
458     case OUT_RESERVE:
459         if (s != sbss) {
460             error(ERR_WARNING, "uninitialized space declared in"
461                   " %s section: zeroing",
462                   get_section_name_by_index(secto));
463
464             sect_write(s, NULL, size);
465         } else
466             s->size += size;
467
468         break;
469
470     case OUT_RAWDATA:
471         if (section != NO_SEG)
472             error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
473
474         sect_write(s, data, size);
475         break;
476
477     case OUT_ADDRESS:
478         addr = *(int64_t *)data;
479
480         if (section != NO_SEG) {
481             if (section % 2) {
482                 error(ERR_NONFATAL, "Mach-O format does not support"
483                       " section base references");
484             } else
485                 add_reloc(s, section, 0, size);
486         }
487
488         p = mydata;
489         WRITEADDR(p, addr, size);
490         sect_write(s, mydata, size);
491         break;
492
493     case OUT_REL2ADR:
494         if (section == secto)
495             error(ERR_PANIC, "intra-section OUT_REL2ADR");
496
497         if (section != NO_SEG && section % 2) {
498             error(ERR_NONFATAL, "Mach-O format does not support"
499                   " section base references");
500         } else
501             add_reloc(s, section, 1, 2);
502
503         p = mydata;
504         WRITESHORT(p, *(int32_t *)data - (size + s->size));
505         sect_write(s, mydata, 2L);
506         break;
507
508     case OUT_REL4ADR:
509         if (section == secto)
510             error(ERR_PANIC, "intra-section OUT_REL4ADR");
511
512         if (section != NO_SEG && section % 2) {
513             error(ERR_NONFATAL, "Mach-O format does not support"
514                   " section base references");
515         } else
516             add_reloc(s, section, 1, 4);
517
518         p = mydata;
519         WRITELONG(p, *(int32_t *)data - (size + s->size));
520         sect_write(s, mydata, 4L);
521         break;
522
523     default:
524         error(ERR_PANIC, "unknown output type?");
525         break;
526     }
527 }
528
529 static int32_t macho_section(char *name, int pass, int *bits)
530 {
531     int32_t index, originalIndex;
532     char *sectionAttributes;
533     struct sectmap *sm;
534     struct section *s;
535
536     (void)pass;
537
538     /* Default to 32 bits. */
539     if (!name) {
540         *bits = 32;
541         name = ".text";
542         sectionAttributes = NULL;
543     } else {
544         sectionAttributes = name;
545         name = nasm_strsep(&sectionAttributes, " \t");
546     }
547
548     for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
549         /* make lookup into section name translation table */
550         if (!strcmp(name, sm->nasmsect)) {
551             char *currentAttribute;
552
553             /* try to find section with that name */
554             originalIndex = index = get_section_index_by_name(sm->segname,
555                                                               sm->sectname);
556
557             /* create it if it doesn't exist yet */
558             if (index == -1) {
559                 s = *sectstail = nasm_malloc(sizeof(struct section));
560                 s->next = NULL;
561                 sectstail = &s->next;
562
563                 s->data = saa_init(1L);
564                 s->index = seg_alloc();
565                 s->relocs = NULL;
566                 s->align = -1;
567
568                 xstrncpy(s->segname, sm->segname);
569                 xstrncpy(s->sectname, sm->sectname);
570                 s->size = 0;
571                 s->nreloc = 0;
572                 s->flags = sm->flags;
573
574                 index = s->index;
575             } else {
576                 s = get_section_by_index(index);
577             }
578
579             while ((NULL != sectionAttributes)
580                    && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
581                 if (0 != *currentAttribute) {
582                     if (!nasm_strnicmp("align=", currentAttribute, 6)) {
583                         char *end;
584                         int newAlignment, value;
585
586                         value = strtoul(currentAttribute + 6, (char**)&end, 0);
587                         newAlignment = exact_log2(value);
588
589                         if (0 != *end) {
590                             error(ERR_PANIC,
591                                   "unknown or missing alignment value \"%s\" "
592                                       "specified for section \"%s\"",
593                                   currentAttribute + 6,
594                                   name);
595                             return NO_SEG;
596                         } else if (0 > newAlignment) {
597                             error(ERR_PANIC,
598                                   "alignment of %d (for section \"%s\") is not "
599                                       "a power of two",
600                                   value,
601                                   name);
602                             return NO_SEG;
603                         }
604
605                         if ((-1 != originalIndex)
606                             && (s->align != newAlignment)
607                            && (s->align != -1)) {
608                             error(ERR_PANIC,
609                                   "section \"%s\" has already been specified "
610                                       "with alignment %d, conflicts with new "
611                                       "alignment of %d",
612                             name,
613                             (1 << s->align),
614                             value);
615                             return NO_SEG;
616                         }
617
618                         s->align = newAlignment;
619                     } else if (!nasm_stricmp("data", currentAttribute)) {
620                         /* Do nothing; 'data' is implicit */
621                     } else {
622                         error(ERR_PANIC,
623                               "unknown section attribute %s for section %s",
624                               currentAttribute,
625                               name);
626                         return NO_SEG;
627                     }
628                 }
629             }
630
631             return index;
632         }
633     }
634
635     error(ERR_PANIC, "invalid section name %s", name);
636     return NO_SEG;
637 }
638
639 static void macho_symdef(char *name, int32_t section, int64_t offset,
640                          int is_global, char *special)
641 {
642     struct symbol *sym;
643
644     if (special) {
645         error(ERR_NONFATAL, "The Mach-O output format does "
646               "not support any special symbol types");
647         return;
648     }
649
650     if (is_global == 3) {
651         error(ERR_NONFATAL, "The Mach-O format does not "
652               "(yet) support forward reference fixups.");
653         return;
654     }
655
656     sym = *symstail = nasm_malloc(sizeof(struct symbol));
657     sym->next = NULL;
658     symstail = &sym->next;
659
660     sym->name = name;
661     sym->strx = strslen;
662     sym->type = 0;
663     sym->desc = 0;
664     sym->value = offset;
665     sym->initial_snum = -1;
666
667     /* external and common symbols get N_EXT */
668     if (is_global != 0)
669         sym->type |= N_EXT;
670
671     if (section == NO_SEG) {
672         /* symbols in no section get absolute */
673         sym->type |= N_ABS;
674         sym->sect = NO_SECT;
675     } else {
676         sym->type |= N_SECT;
677
678         /* get the in-file index of the section the symbol was defined in */
679         sym->sect = get_section_fileindex_by_index(section);
680
681         if (sym->sect == NO_SECT) {
682             /* remember symbol number of references to external
683              ** symbols, this works because every external symbol gets
684              ** its own section number allocated internally by nasm and
685              ** can so be used as a key */
686             extsyms = raa_write(extsyms, section, nsyms);
687             sym->initial_snum = nsyms;
688
689             switch (is_global) {
690             case 1:
691             case 2:
692                 /* there isn't actually a difference between global
693                  ** and common symbols, both even have their size in
694                  ** sym->value */
695                 sym->type = N_EXT;
696                 break;
697
698             default:
699                 /* give an error on unfound section if it's not an
700                  ** external or common symbol (assemble_file() does a
701                  ** seg_alloc() on every call for them) */
702                 error(ERR_PANIC, "in-file index for section %d not found",
703                       section);
704             }
705         }
706     }
707
708     ++nsyms;
709 }
710
711 static int32_t macho_segbase(int32_t section)
712 {
713     return section;
714 }
715
716 static void macho_filename(char *inname, char *outname, efunc error)
717 {
718     standard_extension(inname, outname, ".o", error);
719 }
720
721 extern macros_t macho_stdmac[];
722
723 /* Comparison function for qsort symbol layout.  */
724 static int layout_compare (const struct symbol **s1,
725                            const struct symbol **s2)
726 {
727     return (strcmp ((*s1)->name, (*s2)->name));
728 }
729
730 /* The native assembler does a few things in a similar function
731
732         * Remove temporary labels
733         * Sort symbols according to local, external, undefined (by name)
734         * Order the string table
735
736    We do not remove temporary labels right now.
737
738    numsyms is the total number of symbols we have. strtabsize is the
739    number entries in the string table.  */
740
741 static void macho_layout_symbols (uint32_t *numsyms,
742                                   uint32_t *strtabsize)
743 {
744     struct symbol *sym, **symp;
745     uint32_t i,j;
746
747     *numsyms = 0;
748     *strtabsize = sizeof (char);
749
750     symp = &syms;
751
752     while ((sym = *symp)) {
753         /* Undefined symbols are now external.  */
754         if (sym->type == N_UNDF)
755             sym->type |= N_EXT;
756
757         if ((sym->type & N_EXT) == 0) {
758             sym->snum = *numsyms;
759             *numsyms = *numsyms + 1;
760             nlocalsym++;
761         }
762         else {
763             if ((sym->type & N_TYPE) != N_UNDF)
764                 nextdefsym++;
765             else
766                 nundefsym++;
767
768             /* If we handle debug info we'll want
769                to check for it here instead of just
770                adding the symbol to the string table.  */
771             sym->strx = *strtabsize;
772             saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
773             *strtabsize += strlen(sym->name) + 1;
774         }
775         symp = &(sym->next);
776     }
777
778     /* Next, sort the symbols.  Most of this code is a direct translation from
779        the Apple cctools symbol layout. We need to keep compatibility with that.  */
780     /* Set the indexes for symbol groups into the symbol table */
781     ilocalsym = 0;
782     iextdefsym = nlocalsym;
783     iundefsym = nlocalsym + nextdefsym;
784
785     /* allocate arrays for sorting externals by name */
786     extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
787     undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
788
789     i = 0;
790     j = 0;
791
792     symp = &syms;
793
794     while ((sym = *symp)) {
795
796         if((sym->type & N_EXT) == 0) {
797             sym->strx = *strtabsize;
798             saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
799             *strtabsize += strlen(sym->name) + 1;
800         }
801         else {
802             if((sym->type & N_TYPE) != N_UNDF)
803                 extdefsyms[i++] = sym;
804             else
805                 undefsyms[j++] = sym;
806         }
807         symp = &(sym->next);
808     }
809
810     qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
811           (int (*)(const void *, const void *))layout_compare);
812     qsort(undefsyms, nundefsym, sizeof(struct symbol *),
813           (int (*)(const void *, const void *))layout_compare);
814
815     for(i = 0; i < nextdefsym; i++) {
816         extdefsyms[i]->snum = *numsyms;
817         *numsyms += 1;
818     }
819     for(j = 0; j < nundefsym; j++) {
820         undefsyms[j]->snum = *numsyms;
821         *numsyms += 1;
822     }
823 }
824
825 /* Calculate some values we'll need for writing later.  */
826
827 static void macho_calculate_sizes (void)
828 {
829     struct section *s;
830
831     /* count sections and calculate in-memory and in-file offsets */
832     for (s = sects; s != NULL; s = s->next) {
833         uint32_t pad = 0;
834
835         /* zerofill sections aren't actually written to the file */
836         if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
837             seg_filesize += s->size;
838
839         /* recalculate segment address based on alignment and vm size */
840         s->addr = seg_vmsize;
841         /* we need section alignment to calculate final section address */
842         if (s->align == -1)
843             s->align = DEFAULT_SECTION_ALIGNMENT;
844         if(s->align) {
845             uint32_t newaddr = align(s->addr, 1 << s->align);
846             pad = newaddr - s->addr;
847             s->addr = newaddr;
848         }
849
850         seg_vmsize += s->size + pad;
851         ++seg_nsects;
852     }
853
854     /* calculate size of all headers, load commands and sections to
855     ** get a pointer to the start of all the raw data */
856     if (seg_nsects > 0) {
857         ++head_ncmds;
858         head_sizeofcmds +=
859             MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
860     }
861
862     if (nsyms > 0) {
863         ++head_ncmds;
864         head_sizeofcmds += MACHO_SYMCMD_SIZE;
865     }
866 }
867
868 /* Write out the header information for the file.  */
869
870 static void macho_write_header (void)
871 {
872     fwriteint32_t(MH_MAGIC, machofp);   /* magic */
873     fwriteint32_t(CPU_TYPE_I386, machofp);      /* CPU type */
874     fwriteint32_t(CPU_SUBTYPE_I386_ALL, machofp);       /* CPU subtype */
875     fwriteint32_t(MH_OBJECT, machofp);  /* Mach-O file type */
876     fwriteint32_t(head_ncmds, machofp); /* number of load commands */
877     fwriteint32_t(head_sizeofcmds, machofp);    /* size of load commands */
878     fwriteint32_t(0, machofp);  /* no flags */
879 }
880
881 /* Write out the segment load command at offset.  */
882
883 static uint32_t macho_write_segment (uint32_t offset)
884 {
885     uint32_t rel_base = alignint32_t (offset + seg_filesize);
886     uint32_t s_reloff = 0;
887     struct section *s;
888
889     fwriteint32_t(LC_SEGMENT, machofp);        /* cmd == LC_SEGMENT */
890
891     /* size of load command including section load commands */
892     fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
893                MACHO_SECTCMD_SIZE, machofp);
894
895     /* in an MH_OBJECT file all sections are in one unnamed (name
896     ** all zeros) segment */
897     fwritezero(16, machofp);
898     fwriteint32_t(0, machofp); /* in-memory offset */
899     fwriteint32_t(seg_vmsize, machofp);        /* in-memory size */
900     fwriteint32_t(offset, machofp);    /* in-file offset to data */
901     fwriteint32_t(seg_filesize, machofp);      /* in-file size */
902     fwriteint32_t(VM_PROT_DEFAULT, machofp);   /* maximum vm protection */
903     fwriteint32_t(VM_PROT_DEFAULT, machofp);   /* initial vm protection */
904     fwriteint32_t(seg_nsects, machofp);        /* number of sections */
905     fwriteint32_t(0, machofp); /* no flags */
906
907     /* emit section headers */
908     for (s = sects; s != NULL; s = s->next) {
909         fwrite(s->sectname, sizeof(s->sectname), 1, machofp);
910         fwrite(s->segname, sizeof(s->segname), 1, machofp);
911         fwriteint32_t(s->addr, machofp);
912         fwriteint32_t(s->size, machofp);
913
914         /* dummy data for zerofill sections or proper values */
915         if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
916             fwriteint32_t(offset, machofp);
917             /* Write out section alignment, as a power of two.
918                e.g. 32-bit word alignment would be 2 (2^^2 = 4).  */
919             if (s->align == -1)
920                 s->align = DEFAULT_SECTION_ALIGNMENT;
921             fwriteint32_t(s->align, machofp);
922             /* To be compatible with cctools as we emit
923                a zero reloff if we have no relocations.  */
924             fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, machofp);
925             fwriteint32_t(s->nreloc, machofp);
926
927             offset += s->size;
928             s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
929         } else {
930             fwriteint32_t(0, machofp);
931             fwriteint32_t(0, machofp);
932             fwriteint32_t(0, machofp);
933             fwriteint32_t(0, machofp);
934         }
935
936         fwriteint32_t(s->flags, machofp);      /* flags */
937         fwriteint32_t(0, machofp);     /* reserved */
938         fwriteint32_t(0, machofp);     /* reserved */
939     }
940
941     rel_padcnt = rel_base - offset;
942     offset = rel_base + s_reloff;
943
944     return offset;
945 }
946
947 /* For a given chain of relocs r, write out the entire relocation
948    chain to the object file.  */
949
950 static void macho_write_relocs (struct reloc *r)
951 {
952     while (r) {
953         uint32_t word2;
954
955         fwriteint32_t(r->addr, machofp); /* reloc offset */
956
957         word2 = r->snum;
958         word2 |= r->pcrel << 24;
959         word2 |= r->length << 25;
960         word2 |= r->ext << 27;
961         word2 |= r->type << 28;
962         fwriteint32_t(word2, machofp); /* reloc data */
963
964         r = r->next;
965     }
966 }
967
968 /* Write out the section data.  */
969 static void macho_write_section (void)
970 {
971     struct section *s, *s2;
972     struct reloc *r;
973     uint8_t fi, *p, *q, blk[4];
974     int32_t l;
975
976     for (s = sects; s != NULL; s = s->next) {
977         if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
978             continue;
979
980         /* no padding needs to be done to the sections */
981
982         /* Like a.out Mach-O references things in the data or bss
983          * sections by addresses which are actually relative to the
984          * start of the _text_ section, in the _file_. See outaout.c
985          * for more information. */
986         saa_rewind(s->data);
987         for (r = s->relocs; r != NULL; r = r->next) {
988             saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
989             p = q = blk;
990             l = *p++;
991
992             /* get offset based on relocation type */
993             if (r->length > 0) {
994                 l += ((int32_t)*p++) << 8;
995
996                 if (r->length == 2) {
997                     l += ((int32_t)*p++) << 16;
998                     l += ((int32_t)*p++) << 24;
999                 }
1000             }
1001
1002             /* If the relocation is internal add to the current section
1003                offset. Otherwise the only value we need is the symbol
1004                offset which we already have. The linker takes care
1005                of the rest of the address.  */
1006             if (!r->ext) {
1007             /* generate final address by section address and offset */
1008                     for (s2 = sects, fi = 1;
1009                         s2 != NULL; s2 = s2->next, fi++) {
1010                         if (fi == r->snum) {
1011                             l += s2->addr;
1012                             break;
1013                         }
1014                     }
1015             }
1016
1017             /* write new offset back */
1018             if (r->length == 2)
1019                 WRITELONG(q, l);
1020             else if (r->length == 1)
1021                 WRITESHORT(q, l);
1022             else
1023                 *q++ = l & 0xFF;
1024
1025             saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1026         }
1027
1028         /* dump the section data to file */
1029         saa_fpwrite(s->data, machofp);
1030     }
1031
1032     /* pad last section up to reloc entries on int32_t boundary */
1033     fwritezero(rel_padcnt, machofp);
1034
1035     /* emit relocation entries */
1036     for (s = sects; s != NULL; s = s->next)
1037         macho_write_relocs (s->relocs);
1038 }
1039
1040 /* Write out the symbol table. We should already have sorted this
1041    before now.  */
1042 static void macho_write_symtab (void)
1043 {
1044     struct symbol *sym;
1045     struct section *s;
1046     int32_t fi;
1047     uint32_t i;
1048
1049     /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1050
1051     for (sym = syms; sym != NULL; sym = sym->next) {
1052         if ((sym->type & N_EXT) == 0) {
1053             fwriteint32_t(sym->strx, machofp);          /* string table entry number */
1054             fwrite(&sym->type, 1, 1, machofp);  /* symbol type */
1055             fwrite(&sym->sect, 1, 1, machofp);  /* section */
1056             fwriteint16_t(sym->desc, machofp);  /* description */
1057
1058             /* Fix up the symbol value now that we know the final section
1059                sizes.  */
1060             if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1061                 for (s = sects, fi = 1;
1062                      s != NULL && fi < sym->sect; s = s->next, ++fi)
1063                     sym->value += s->size;
1064             }
1065
1066             fwriteint32_t(sym->value, machofp); /* value (i.e. offset) */
1067         }
1068     }
1069
1070     for (i = 0; i < nextdefsym; i++) {
1071         sym = extdefsyms[i];
1072         fwriteint32_t(sym->strx, machofp);
1073         fwrite(&sym->type, 1, 1, machofp);      /* symbol type */
1074         fwrite(&sym->sect, 1, 1, machofp);      /* section */
1075         fwriteint16_t(sym->desc, machofp);      /* description */
1076
1077         /* Fix up the symbol value now that we know the final section
1078            sizes.  */
1079         if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1080             for (s = sects, fi = 1;
1081                  s != NULL && fi < sym->sect; s = s->next, ++fi)
1082                 sym->value += s->size;
1083         }
1084
1085         fwriteint32_t(sym->value, machofp);     /* value (i.e. offset) */
1086     }
1087
1088      for (i = 0; i < nundefsym; i++) {
1089          sym = undefsyms[i];
1090          fwriteint32_t(sym->strx, machofp);
1091          fwrite(&sym->type, 1, 1, machofp);     /* symbol type */
1092          fwrite(&sym->sect, 1, 1, machofp);     /* section */
1093          fwriteint16_t(sym->desc, machofp);     /* description */
1094
1095          /* Fix up the symbol value now that we know the final section
1096             sizes.  */
1097          if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1098              for (s = sects, fi = 1;
1099                   s != NULL && fi < sym->sect; s = s->next, ++fi)
1100                  sym->value += s->size;
1101          }
1102
1103          fwriteint32_t(sym->value, machofp);    /* value (i.e. offset) */
1104      }
1105 }
1106
1107 /* Fixup the snum in the relocation entries, we should be
1108    doing this only for externally undefined symbols. */
1109 static void macho_fixup_relocs (struct reloc *r)
1110 {
1111     struct symbol *sym;
1112     uint32_t i;
1113
1114     while (r != NULL) {
1115         if (r->ext) {
1116             for (i = 0; i < nundefsym; i++) {
1117                 sym = undefsyms[i];
1118                 if (sym->initial_snum == r->snum) {
1119                     r->snum = sym->snum;
1120                    break;
1121                 }
1122             }
1123         }
1124         r = r->next;
1125     }
1126 }
1127
1128 /* Write out the object file.  */
1129
1130 static void macho_write (void)
1131 {
1132     uint32_t offset = 0;
1133
1134     /* mach-o object file structure:
1135     **
1136     ** mach header
1137     **  uint32_t magic
1138     **  int   cpu type
1139     **  int   cpu subtype
1140     **  uint32_t mach file type
1141     **  uint32_t number of load commands
1142     **  uint32_t size of all load commands
1143     **   (includes section struct size of segment command)
1144     **  uint32_t flags
1145     **
1146     ** segment command
1147     **  uint32_t command type == LC_SEGMENT
1148     **  uint32_t size of load command
1149     **   (including section load commands)
1150     **  char[16] segment name
1151     **  uint32_t in-memory offset
1152     **  uint32_t in-memory size
1153     **  uint32_t in-file offset to data area
1154     **  uint32_t in-file size
1155     **   (in-memory size excluding zerofill sections)
1156     **  int   maximum vm protection
1157     **  int   initial vm protection
1158     **  uint32_t number of sections
1159     **  uint32_t flags
1160     **
1161     ** section commands
1162     **   char[16] section name
1163     **   char[16] segment name
1164     **   uint32_t in-memory offset
1165     **   uint32_t in-memory size
1166     **   uint32_t in-file offset
1167     **   uint32_t alignment
1168     **    (irrelevant in MH_OBJECT)
1169     **   uint32_t in-file offset of relocation entires
1170     **   uint32_t number of relocations
1171     **   uint32_t flags
1172     **   uint32_t reserved
1173     **   uint32_t reserved
1174     **
1175     ** symbol table command
1176     **  uint32_t command type == LC_SYMTAB
1177     **  uint32_t size of load command
1178     **  uint32_t symbol table offset
1179     **  uint32_t number of symbol table entries
1180     **  uint32_t string table offset
1181     **  uint32_t string table size
1182     **
1183     ** raw section data
1184     **
1185     ** padding to int32_t boundary
1186     **
1187     ** relocation data (struct reloc)
1188     ** int32_t offset
1189     **  uint data (symbolnum, pcrel, length, extern, type)
1190     **
1191     ** symbol table data (struct nlist)
1192     **  int32_t  string table entry number
1193     **  uint8_t type
1194     **   (extern, absolute, defined in section)
1195     **  uint8_t section
1196     **   (0 for global symbols, section number of definition (>= 1, <=
1197     **   254) for local symbols, size of variable for common symbols
1198     **   [type == extern])
1199     **  int16_t description
1200     **   (for stab debugging format)
1201     **  uint32_t value (i.e. file offset) of symbol or stab offset
1202     **
1203     ** string table data
1204     **  list of null-terminated strings
1205     */
1206
1207     /* Emit the Mach-O header.  */
1208     macho_write_header();
1209
1210     offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1211
1212     /* emit the segment load command */
1213     if (seg_nsects > 0)
1214         offset = macho_write_segment (offset);
1215     else
1216         error(ERR_WARNING, "no sections?");
1217
1218     if (nsyms > 0) {
1219         /* write out symbol command */
1220         fwriteint32_t(LC_SYMTAB, machofp); /* cmd == LC_SYMTAB */
1221         fwriteint32_t(MACHO_SYMCMD_SIZE, machofp); /* size of load command */
1222         fwriteint32_t(offset, machofp);    /* symbol table offset */
1223         fwriteint32_t(nsyms, machofp);     /* number of symbol
1224                                          ** table entries */
1225
1226         offset += nsyms * MACHO_NLIST_SIZE;
1227         fwriteint32_t(offset, machofp);    /* string table offset */
1228         fwriteint32_t(strslen, machofp);   /* string table size */
1229     }
1230
1231     /* emit section data */
1232     if (seg_nsects > 0)
1233         macho_write_section ();
1234
1235     /* emit symbol table if we have symbols */
1236     if (nsyms > 0)
1237         macho_write_symtab ();
1238
1239     /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1240
1241     /* emit string table */
1242     saa_fpwrite(strs, machofp);
1243 }
1244 /* We do quite a bit here, starting with finalizing all of the data
1245    for the object file, writing, and then freeing all of the data from
1246    the file.  */
1247
1248 static void macho_cleanup(int debuginfo)
1249 {
1250     struct section *s;
1251     struct reloc *r;
1252     struct symbol *sym;
1253
1254     (void)debuginfo;
1255
1256     /* Sort all symbols.  */
1257     macho_layout_symbols (&nsyms, &strslen);
1258
1259     /* Fixup relocation entries */
1260     for (s = sects; s != NULL; s = s->next) {
1261         macho_fixup_relocs (s->relocs);
1262     }
1263
1264     /* First calculate and finalize needed values.  */
1265     macho_calculate_sizes();
1266     macho_write();
1267
1268     /* free up everything */
1269     while (sects->next) {
1270         s = sects;
1271         sects = sects->next;
1272
1273         saa_free(s->data);
1274         while (s->relocs != NULL) {
1275             r = s->relocs;
1276             s->relocs = s->relocs->next;
1277             nasm_free(r);
1278         }
1279
1280         nasm_free(s);
1281     }
1282
1283     saa_free(strs);
1284     raa_free(extsyms);
1285
1286     if (syms) {
1287     while (syms->next) {
1288        sym = syms;
1289        syms = syms->next;
1290
1291        nasm_free (sym);
1292     }
1293 }
1294 }
1295
1296 /* Debugging routines.  */
1297 static void debug_reloc (struct reloc *r)
1298 {
1299     fprintf (stdout, "reloc:\n");
1300     fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1301     fprintf (stdout, "\tsnum: %d\n", r->snum);
1302     fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1303     fprintf (stdout, "\tlength: %d\n", r->length);
1304     fprintf (stdout, "\text: %d\n", r->ext);
1305     fprintf (stdout, "\ttype: %d\n", r->type);
1306 }
1307
1308 static void debug_section_relocs (struct section *s)
1309 {
1310     struct reloc *r = s->relocs;
1311
1312     fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1313
1314     while (r != NULL) {
1315         debug_reloc (r);
1316         r = r->next;
1317     }
1318 }
1319
1320 struct ofmt of_macho32 = {
1321     "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1322     "macho32",
1323     0,
1324     null_debug_arr,
1325     &null_debug_form,
1326     macho_stdmac,
1327     macho_init,
1328     null_setinfo,
1329     macho_output,
1330     macho_symdef,
1331     macho_section,
1332     macho_segbase,
1333     null_directive,
1334     macho_filename,
1335     macho_cleanup
1336 };
1337
1338 struct ofmt of_macho = {
1339     "MACHO (short name for MACHO32)",
1340     "macho",
1341     0,
1342     null_debug_arr,
1343     &null_debug_form,
1344     macho_stdmac,
1345     macho_init,
1346     null_setinfo,
1347     macho_output,
1348     macho_symdef,
1349     macho_section,
1350     macho_segbase,
1351     null_directive,
1352     macho_filename,
1353     macho_cleanup
1354 };
1355
1356 #endif
1357
1358 /*
1359  * Local Variables:
1360  * mode:c
1361  * c-basic-offset:4
1362  * End:
1363  *
1364  * end of file */