home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cctools / as / Mach-O.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-13  |  39.4 KB  |  1,238 lines

  1. #ifdef Mach_O
  2. /*
  3.  * This file provides the means that so that GAS can put out Mach-O object files
  4.  * with more than just text, data, and bss sections.  It does this by using
  5.  * sub-segments.  The major limitation is that short references can't be used
  6.  * between sub-segments or this will break.  So do NOT create two sub-segments
  7.  * that contain instructions that branch to each other.
  8.  */
  9. #include <mach-o/loader.h>
  10. #undef SEG_TEXT
  11. #undef SEG_DATA
  12. #include "as.h"
  13. #include "subsegs.h"
  14. #include "struc-symbol.h"
  15. #include "symbols.h"
  16. #include "write.h"
  17. #include <mach-o/stab.h>
  18. #include <stuff/bytesex.h>
  19. extern int bad_error;
  20.  
  21. #ifdef M68K
  22. /*
  23.  * The last align 2 frag in the text section set in write_object_file() and
  24.  * used here to pad the text section with a nop instruction rather than two
  25.  * bytes of zero that is not a full instruction.
  26.  */
  27. struct frag *frag_nop = NULL;
  28. #endif
  29.  
  30. static void fix_to_relocation_info(
  31.     struct fix *fixP,
  32.     unsigned long segment_address_in_file,
  33.     struct relocation_info *riP);
  34. #ifdef RISC
  35. static void PAIRfix_to_relocation_info(
  36.     struct fix *fixP,
  37.     unsigned long segment_address_in_file,
  38.     struct relocation_info *riP);
  39. #endif
  40. static long round(
  41.     long v,
  42.     unsigned long r);
  43.  
  44. /* This is the nsect that the text sub-segments start at */
  45. #define TEXT_NSECT    (1)
  46. /* This is the nsect that the data sub-segments start at */
  47. #define DATA_NSECT    (TEXT_NSECT+10)
  48. /* This is the nsect for the bss which follows all the data sub-segments*/
  49. #define BSS_NSECT    (DATA_NSECT+22)
  50.  
  51. /* The array to translate nsects to the nsects in the output file.  This is
  52.  * used to remove unused sections from the output file.  It is directly indexed
  53.  * with nsect (which starts at 1).
  54.  */
  55. static long new_nsect[BSS_NSECT + 1] = { 0 };
  56.  
  57. /*
  58.  * The array to hold the sections that will actually be in the output file.
  59.  * Indexed from 0 to nsects-1.
  60.  */
  61. static struct section *sections[BSS_NSECT];
  62.  
  63. /*
  64.  * The segments and sub_segment are fixed.  The text, data and bss sections
  65.  * are assumed always to be there (as the assembler allways creates them).
  66.  * 
  67.  *    GAS segment     Gas sub_segment         Mach-O section number and name
  68.  *     SEG_ABS        n/a            0  NO_SECT
  69.  *    SEG_TEXT    0         TEXT_NSECT+0  (__TEXT,__text)
  70.  *    SEG_TEXT    1         TEXT_NSECT+1  (__TEXT,__const)
  71.  *    SEG_TEXT    2         TEXT_NSECT+2  (__TEXT,__static_const)
  72.  *    SEG_TEXT    3         TEXT_NSECT+3  (__TEXT,__cstring)
  73.  *    SEG_TEXT    4         TEXT_NSECT+4  (__TEXT,__literal4)
  74.  *    SEG_TEXT    5         TEXT_NSECT+5  (__TEXT,__literal8)
  75.  *    SEG_TEXT    6         TEXT_NSECT+6  (__TEXT,__constructor)
  76.  *    SEG_TEXT    7         TEXT_NSECT+7  (__TEXT,__destructor)
  77.  *    SEG_TEXT    8         TEXT_NSECT+8  (__TEXT,__fvmlib_init0)
  78.  *    SEG_TEXT    9         TEXT_NSECT+9  (__TEXT,__fvmlib_init1)
  79.  *    SEG_DATA    0         DATA_NSECT+0  (__DATA,__data)
  80.  *    SEG_DATA    1         DATA_NSECT+1  (__DATA,__static_data)
  81.  *    SEG_DATA    2         DATA_NSECT+2  (__OBJC,__class)
  82.  *    SEG_DATA    3         DATA_NSECT+3  (__OBJC,__meta_class)
  83.  *    SEG_DATA    4         DATA_NSECT+4  (__OBJC,__string_object)
  84.  *    SEG_DATA    5         DATA_NSECT+5  (__OBJC,__protocol)
  85.  *    SEG_DATA    6         DATA_NSECT+6  (__OBJC,__cat_cls_meth)
  86.  *    SEG_DATA    7         DATA_NSECT+7  (__OBJC,__cat_inst_meth)
  87.  *    SEG_DATA    8         DATA_NSECT+8  (__OBJC,__cls_meth)
  88.  *    SEG_DATA    9         DATA_NSECT+9  (__OBJC,__inst_meth)
  89.  *    SEG_DATA    10         DATA_NSECT+10 (__OBJC,__message_refs)
  90.  *    SEG_DATA    11         DATA_NSECT+11 (__OBJC,__cls_refs)
  91.  *    SEG_DATA    12         DATA_NSECT+12 (__OBJC,__class_names)
  92.  *    SEG_DATA    13         DATA_NSECT+13 (__OBJC,__module_info)
  93.  *    SEG_DATA    14         DATA_NSECT+14 (__OBJC,__symbols)
  94.  *    SEG_DATA    15         DATA_NSECT+15 (__OBJC,__category)
  95.  *    SEG_DATA    16         DATA_NSECT+16 (__OBJC,__meth_var_types)
  96.  *    SEG_DATA    17         DATA_NSECT+17 (__OBJC,__class_vars)
  97.  *    SEG_DATA    18         DATA_NSECT+18 (__OBJC,__instance_vars)
  98.  *    SEG_DATA    19         DATA_NSECT+19 (__OBJC,__meth_var_names)
  99.  *    SEG_DATA    20         DATA_NSECT+20 (__OBJC,__selector_strs)
  100.  *    SEG_BSS        n/a          BSS_NSECT+0  (__DATA,__bss)
  101.  */
  102.  
  103. /*
  104.  * These are static so the alignment can be set in to them by
  105.  * set_section_align() and picked up by write_Mach_O().
  106.  */
  107. static struct section    text_section;        /* __TEXT, __text */
  108. static struct section    const_section;        /* __TEXT, __const */
  109. static struct section    static_const_section;    /* __TEXT, __static_const */
  110. static struct section    cstring_section;    /* __TEXT, __cstring */
  111. static struct section    literal4_section;    /* __TEXT, __literal4 */
  112. static struct section    literal8_section;    /* __TEXT, __literal8 */
  113. static struct section    constructor_section;    /* __TEXT, __constructor */
  114. static struct section    destructor_section;    /* __TEXT, __destructor */
  115. static struct section    fvmlib_init0_section;    /* __TEXT, __fvmlib_init0 */
  116. static struct section    fvmlib_init1_section;    /* __TEXT, __fvmlib_init1 */
  117. static struct section    data_section;        /* __DATA, __data */
  118. static struct section    static_data_section;    /* __DATA, __static_data */
  119. static struct section    class_section;        /* __OBJC, __class */
  120. static struct section    meta_class_section;    /* __OBJC, __meta_class */
  121. static struct section    string_object_section;    /* __OBJC, __string_object */
  122. static struct section    protocol_section;    /* __OBJC, __protocol */
  123. static struct section    cat_cls_meth_section;    /* __OBJC, __cat_cls_meth */
  124. static struct section    cat_inst_meth_section;    /* __OBJC, __cat_inst_meth */
  125. static struct section    cls_meth_section;    /* __OBJC, __cls_meth */
  126. static struct section    inst_meth_section;    /* __OBJC, __inst_meth */
  127. static struct section    message_refs_section;    /* __OBJC, __message_refs */
  128. static struct section    cls_refs_section;    /* __OBJC, __cls_refs */
  129. static struct section    class_names_section;    /* __OBJC, __class_names */
  130. static struct section    module_info_section;    /* __OBJC, __module_info */
  131. static struct section    symbols_section;    /* __OBJC, __symbols */
  132. static struct section    category_section;    /* __OBJC, __category */
  133. static struct section    meth_var_types_section;    /* __OBJC, __meth_var_types */
  134. static struct section    class_vars_section;    /* __OBJC, __class_vars */
  135. static struct section    instance_vars_section;    /* __OBJC, __instance_vars */
  136. static struct section    meth_var_names_section;    /* __OBJC, __meth_var_names */
  137. static struct section    selector_strs_section;    /* __OBJC, __selector_strs */
  138. static struct section    bss_section;        /* __DATA, __bss */
  139.  
  140. /*
  141.  * seg_n_sect() returns the value of the n_sect field (Mach-O section number)
  142.  * for the specified segment and sub-segment.  This is used in calls to
  143.  * new_symbol to set the n_sect (n_other) field.
  144.  */
  145. unsigned char
  146. seg_n_sect(
  147.     segT seg,
  148.     int subseg)
  149. {
  150.     if(seg == SEG_ABSOLUTE)
  151.         return(0);
  152.     if(seg == SEG_TEXT){
  153.         if(subseg < 0 || subseg > DATA_NSECT)
  154.         as_fatal("Bad subseg (%d) for SEG_TEXT to seg_n_sect()\n",
  155.              subseg);
  156.         return(TEXT_NSECT + subseg);
  157.     }
  158.     if(seg == SEG_DATA){
  159.         if(subseg < 0 || subseg > BSS_NSECT)
  160.         as_fatal("Bad subseg (%d) for SEG_DATA to seg_n_sect()\n",
  161.              subseg);
  162.         return(DATA_NSECT + subseg);
  163.     }
  164.     if(seg == SEG_BSS)
  165.         return(BSS_NSECT);
  166.     as_fatal("Bad seg (%d) to seg_n_sect()\n", seg);
  167. }
  168.  
  169. /*
  170.  * n_type_n_sect() returns the value of the n_sect field (Mach-O section number)
  171.  * for the specified N_TYPE bits of the n_type field.  This is used only in
  172.  * setting the the n_sect (n_other) field for stabs from stab() in read.c.
  173.  */
  174. unsigned char
  175. n_type_n_sect(
  176.     int n_type)
  177. {
  178.     switch(n_type & N_TYPE){
  179.     case N_TEXT:
  180.         return(TEXT_NSECT);
  181.     case N_DATA:
  182.         return(DATA_NSECT);
  183.     case N_BSS:
  184.         return(BSS_NSECT);
  185.     case N_ABS:
  186.     default:
  187.         return(0);
  188.     }
  189. }
  190.  
  191. /*
  192.  * set_section_align() sets the section alignment.  Called from s_align().
  193.  * The resulting alignment is the greatest alignment seen for that section.
  194.  */
  195. set_section_align(
  196. long align)
  197. {
  198.     if(frchain_now->frch_subseg_align < align)
  199.         frchain_now->frch_subseg_align = align;
  200.  
  201.     switch(now_seg){
  202.     case SEG_TEXT:
  203.         switch(now_subseg){
  204.         case 0:
  205.         if(text_section.align < align)
  206.             text_section.align = align;
  207.         break;
  208.         case 1:
  209.         if(const_section.align < align)
  210.             const_section.align = align;
  211.         break;
  212.         case 2:
  213.         if(static_const_section.align < align)
  214.             static_const_section.align = align;
  215.         break;
  216.         case 3:
  217.         if(cstring_section.align < align)
  218.             cstring_section.align = align;
  219.         break;
  220.         case 4:
  221.         if(literal4_section.align < align)
  222.             literal4_section.align = align;
  223.         break;
  224.         case 5:
  225.         if(literal8_section.align < align)
  226.             literal8_section.align = align;
  227.         break;
  228.         case 6:
  229.         if(constructor_section.align < align)
  230.             constructor_section.align = align;
  231.         break;
  232.         case 7:
  233.         if(destructor_section.align < align)
  234.             destructor_section.align = align;
  235.         break;
  236.         case 8:
  237.         if(fvmlib_init0_section.align < align)
  238.             fvmlib_init0_section.align = align;
  239.         break;
  240.         case 9:
  241.         if(fvmlib_init1_section.align < align)
  242.             fvmlib_init1_section.align = align;
  243.         break;
  244.         default:
  245.         as_fatal("Bad now_subseg (%d) for SEG_TEXT in ",
  246.              "set_section_align()\n", now_subseg);
  247.         }
  248.         break;
  249.     case SEG_DATA:
  250.         switch(now_subseg){
  251.         case 0:
  252.         if(data_section.align < align)
  253.             data_section.align = align;
  254.         break;
  255.         case 1:
  256.         if(static_data_section.align < align)
  257.             static_data_section.align = align;
  258.         break;
  259.         case 2:
  260.         if(class_section.align < align)
  261.             class_section.align = align;
  262.         break;
  263.         case 3:
  264.         if(meta_class_section.align < align)
  265.             meta_class_section.align = align;
  266.         break;
  267.         case 4:
  268.         if(string_object_section.align < align)
  269.             string_object_section.align = align;
  270.         break;
  271.         case 5:
  272.         if(protocol_section.align < align)
  273.             protocol_section.align = align;
  274.         break;
  275.         case 6:
  276.         if(cat_cls_meth_section.align < align)
  277.             cat_cls_meth_section.align = align;
  278.         break;
  279.         case 7:
  280.         if(cat_inst_meth_section.align < align)
  281.             cat_inst_meth_section.align = align;
  282.         break;
  283.         case 8:
  284.         if(cls_meth_section.align < align)
  285.             cls_meth_section.align = align;
  286.         break;
  287.         case 9:
  288.         if(inst_meth_section.align < align)
  289.             inst_meth_section.align = align;
  290.         break;
  291.         case 10:
  292.         if(message_refs_section.align < align)
  293.             message_refs_section.align = align;
  294.         break;
  295.         case 11:
  296.         if(cls_refs_section.align < align)
  297.             cls_refs_section.align = align;
  298.         break;
  299.         case 12:
  300.         if(class_names_section.align < align)
  301.             class_names_section.align = align;
  302.         break;
  303.         case 13:
  304.         if(module_info_section.align < align)
  305.             module_info_section.align = align;
  306.         break;
  307.         case 14:
  308.         if(symbols_section.align < align)
  309.             symbols_section.align = align;
  310.         break;
  311.         case 15:
  312.         if(category_section.align < align)
  313.             category_section.align = align;
  314.         break;
  315.         case 16:
  316.         if(meth_var_types_section.align < align)
  317.             meth_var_types_section.align = align;
  318.         break;
  319.         case 17:
  320.         if(class_vars_section.align < align)
  321.             class_vars_section.align = align;
  322.         break;
  323.         case 18:
  324.         if(instance_vars_section.align < align)
  325.             instance_vars_section.align = align;
  326.         break;
  327.         case 19:
  328.         if(meth_var_names_section.align < align)
  329.             meth_var_names_section.align = align;
  330.         break;
  331.         case 20:
  332.         if(selector_strs_section.align < align)
  333.             selector_strs_section.align = align;
  334.         break;
  335.         default:
  336.         as_fatal("Bad now_subseg (%d) for SEG_DATA in ",
  337.              "set_section_align()\n", now_subseg);
  338.         }
  339.         break;
  340.     default:
  341.         as_fatal("Bad now_seg (%d) in set_section_align()\n", now_seg);
  342.     }
  343. }
  344.  
  345. /*
  346.  * write_Mach_O() writes a Mach-O object file directly from the GAS data
  347.  * structures using the segments and sub-segments as Mach-O sections.
  348.  */
  349. void
  350. write_Mach_O(
  351. long string_byte_count,
  352. long symbol_number)
  353. {
  354.     /* The structures for Mach-O relocatables */
  355.     struct mach_header        header;
  356.     struct segment_command    reloc_segment;
  357.     struct symtab_command    symbol_table;
  358.     long            i, nsects;
  359.  
  360.     /* locals to fill in section struct fields */
  361.     unsigned long addr, size, offset, address, zero;
  362.  
  363.     /* The GAS data structures */
  364.     struct frchain *frchainP;
  365.     struct symbol *symbolP;
  366.     struct frag *fragP;
  367.     struct fix *fixP;
  368.  
  369.     char *the_object_file;
  370.     unsigned long the_object_file_size;
  371.     char *next_object_file_charP;
  372.  
  373.     enum byte_sex host_byte_sex, target_byte_sex;
  374.     unsigned long reloff, nrelocs;
  375.  
  376.     nsects = 0;
  377.     /* 
  378.      * Fill in the addr and size fields of each section structure from the
  379.      * corresponding segment and sub-segment (only the first subsegment is
  380.      * always there the others may be missing).
  381.      */
  382.     for(frchainP = frchain_root;
  383.         frchainP != NULL;
  384.         frchainP = frchainP->frch_next){
  385.  
  386.         addr = frchainP->frch_root->fr_address;
  387.         size = frchainP->frch_last->fr_address -
  388.            frchainP->frch_root->fr_address;
  389.  
  390.         /*
  391.         printf("frchainP = 0x%x frch_seg = %d frch_subseg = %d ",
  392.             frchainP, frchainP->frch_seg, frchainP->frch_subseg);
  393.         printf("addr 0x%x size 0x%x\n", addr, size);
  394.         */
  395.  
  396.         switch(frchainP->frch_seg){
  397.         case SEG_TEXT:
  398.         switch(frchainP->frch_subseg){
  399.         case 0:
  400.             sections[nsects] = &text_section;
  401.             break;
  402.         case 1:
  403.             sections[nsects] = &const_section;
  404.             break;
  405.         case 2:
  406.             sections[nsects] = &static_const_section;
  407.             break;
  408.         case 3:
  409.             sections[nsects] = &cstring_section;
  410.             break;
  411.         case 4:
  412.             sections[nsects] = &literal4_section;
  413.             break;
  414.         case 5:
  415.             sections[nsects] = &literal8_section;
  416.             break;
  417.         case 6:
  418.             sections[nsects] = &constructor_section;
  419.             break;
  420.         case 7:
  421.             sections[nsects] = &destructor_section;
  422.             break;
  423.         case 8:
  424.             sections[nsects] = &fvmlib_init0_section;
  425.             break;
  426.         case 9:
  427.             sections[nsects] = &fvmlib_init1_section;
  428.             break;
  429.         default:
  430.             as_fatal("Bad subseg (%d) for SEG_TEXT in write_Mach_O()\n",
  431.                  frchainP->frch_subseg);
  432.         }
  433.         new_nsect[TEXT_NSECT + frchainP->frch_subseg] = nsects + 1;
  434.         break;
  435.         case SEG_DATA:
  436.         switch(frchainP->frch_subseg){
  437.         case 0:
  438.             sections[nsects] = &data_section;
  439.             break;
  440.         case 1:
  441.             sections[nsects] = &static_data_section;
  442.             break;
  443.         case 2:
  444.             sections[nsects] = &class_section;
  445.             break;
  446.         case 3:
  447.             sections[nsects] = &meta_class_section;
  448.             break;
  449.         case 4:
  450.             sections[nsects] = &string_object_section;
  451.             break;
  452.         case 5:
  453.             sections[nsects] = &protocol_section;
  454.             break;
  455.         case 6:
  456.             sections[nsects] = &cat_cls_meth_section;
  457.             break;
  458.         case 7:
  459.             sections[nsects] = &cat_inst_meth_section;
  460.             break;
  461.         case 8:
  462.             sections[nsects] = &cls_meth_section;
  463.             break;
  464.         case 9:
  465.             sections[nsects] = &inst_meth_section;
  466.             break;
  467.         case 10:
  468.             sections[nsects] = &message_refs_section;
  469.             break;
  470.         case 11:
  471.             sections[nsects] = &cls_refs_section;
  472.             break;
  473.         case 12:
  474.             sections[nsects] = &class_names_section;
  475.             break;
  476.         case 13:
  477.             sections[nsects] = &module_info_section;
  478.             break;
  479.         case 14:
  480.             sections[nsects] = &symbols_section;
  481.             break;
  482.         case 15:
  483.             sections[nsects] = &category_section;
  484.             break;
  485.         case 16:
  486.             sections[nsects] = &meth_var_types_section;
  487.             break;
  488.         case 17:
  489.             sections[nsects] = &class_vars_section;
  490.             break;
  491.         case 18:
  492.             sections[nsects] = &instance_vars_section;
  493.             break;
  494.         case 19:
  495.             sections[nsects] = &meth_var_names_section;
  496.             break;
  497.         case 20:
  498.             sections[nsects] = &selector_strs_section;
  499.             break;
  500.         default:
  501.             as_fatal("Bad subseg (%d) for SEG_DATA in write_Mach_O()\n",
  502.                  frchainP->frch_subseg);
  503.         }
  504.         new_nsect[DATA_NSECT + frchainP->frch_subseg] = nsects + 1;
  505.         break;
  506.         default:
  507.         as_fatal("Bad seg (%d) in write_Mach_O()\n",frchainP->frch_seg);
  508.         }
  509.         sections[nsects]->addr = addr;
  510.         sections[nsects]->size = size;
  511.         nsects++;
  512.     }
  513.     new_nsect[BSS_NSECT] = nsects + 1;
  514.     bss_section.addr = bss_address_frag.fr_address;
  515.     bss_section.size = local_bss_counter;
  516.     sections[nsects++] = &bss_section;
  517.  
  518.     /* fill in the Mach-O header */
  519.     header.magic = MH_MAGIC;
  520. #ifdef M68K
  521.     header.cputype = CPU_TYPE_MC680x0;
  522. #endif
  523. #ifdef I860
  524.     header.cputype = CPU_TYPE_I860;
  525. #endif
  526. #ifdef M88K
  527.     header.cputype = CPU_TYPE_MC88000;
  528. #endif
  529. #ifdef I386
  530.     header.cputype = CPU_TYPE_I386;
  531. #endif
  532. #ifdef M98K
  533.     header.cputype = CPU_TYPE_MC98000;
  534. #endif
  535.     if(archflag_cpusubtype != -1)
  536.         header.cpusubtype = archflag_cpusubtype;
  537.     else
  538.         header.cpusubtype = cpusubtype;
  539.  
  540.     header.filetype = MH_OBJECT;
  541.     header.ncmds = 2;
  542.     header.sizeofcmds = sizeof(struct segment_command) +
  543.                 nsects * sizeof(struct section) +
  544.                 sizeof(struct symtab_command);
  545.     header.flags = 0;
  546.  
  547.     /* fill in the segment command */
  548.     memset(&reloc_segment, '\0', sizeof(struct segment_command));
  549.     reloc_segment.cmd = LC_SEGMENT;
  550.     reloc_segment.cmdsize = sizeof(struct segment_command) +
  551.                 nsects * sizeof(struct section);
  552.     /* leave reloc_segment.segname full of zeros */
  553.     reloc_segment.vmaddr = text_section.addr;
  554.     reloc_segment.vmsize = 0;
  555.     for(i = 0; i < nsects; i++)
  556.         reloc_segment.vmsize += sections[i]->size;
  557.     offset = header.sizeofcmds + sizeof(struct mach_header);
  558.     reloc_segment.fileoff = offset;
  559.     reloc_segment.filesize = reloc_segment.vmsize - bss_section.size;
  560.     reloc_segment.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  561.     reloc_segment.initprot= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  562.     reloc_segment.nsects = nsects;
  563.     reloc_segment.flags = 0;
  564.  
  565.     /* fill in the segment and section names */
  566.     strcpy(text_section.segname,          "__TEXT");
  567.     strcpy(text_section.sectname,          "__text");
  568.     strcpy(const_section.segname,         "__TEXT");
  569.     strcpy(const_section.sectname,         "__const");
  570.     strcpy(static_const_section.segname,  "__TEXT");
  571.     strcpy(static_const_section.sectname,  "__static_const");
  572.     strcpy(cstring_section.segname,       "__TEXT");
  573.     strcpy(cstring_section.sectname,       "__cstring");
  574.     strcpy(literal4_section.segname,      "__TEXT");
  575.     strcpy(literal4_section.sectname,      "__literal4");
  576.     strcpy(literal8_section.segname,      "__TEXT");
  577.     strcpy(literal8_section.sectname,      "__literal8");
  578.     strcpy(constructor_section.segname,   "__TEXT");
  579.     strcpy(constructor_section.sectname,   "__constructor");
  580.     strcpy(destructor_section.segname,    "__TEXT");
  581.     strcpy(destructor_section.sectname,    "__destructor");
  582.     strcpy(fvmlib_init0_section.segname,  "__TEXT");
  583.     strcpy(fvmlib_init0_section.sectname,  "__fvmlib_init0");
  584.     strcpy(fvmlib_init1_section.segname,  "__TEXT");
  585.     strcpy(fvmlib_init1_section.sectname,  "__fvmlib_init1");
  586.     strcpy(data_section.segname,          "__DATA");
  587.     strcpy(data_section.sectname,          "__data");
  588.     strcpy(static_data_section.segname,   "__DATA");
  589.     strcpy(static_data_section.sectname,   "__static_data");
  590.     strcpy(class_section.segname,         "__OBJC");
  591.     strcpy(class_section.sectname,         "__class");
  592.     strcpy(meta_class_section.segname,    "__OBJC");
  593.     strcpy(meta_class_section.sectname,    "__meta_class");
  594.     strcpy(string_object_section.segname, "__OBJC");
  595.     strcpy(string_object_section.sectname, "__string_object");
  596.     strcpy(protocol_section.segname,      "__OBJC");
  597.     strcpy(protocol_section.sectname,      "__protocol");
  598.     strcpy(cat_cls_meth_section.segname,  "__OBJC");
  599.     strcpy(cat_cls_meth_section.sectname,  "__cat_cls_meth");
  600.     strcpy(cat_inst_meth_section.segname, "__OBJC");
  601.     strcpy(cat_inst_meth_section.sectname, "__cat_inst_meth");
  602.     strcpy(cls_meth_section.segname,      "__OBJC");
  603.     strcpy(cls_meth_section.sectname,      "__cls_meth");
  604.     strcpy(inst_meth_section.segname,     "__OBJC");
  605.     strcpy(inst_meth_section.sectname,     "__inst_meth");
  606.     strcpy(message_refs_section.segname,  "__OBJC");
  607.     strcpy(message_refs_section.sectname,  "__message_refs");
  608.     strcpy(cls_refs_section.segname,      "__OBJC");
  609.     strcpy(cls_refs_section.sectname,      "__cls_refs");
  610.     strcpy(class_names_section.segname,   "__OBJC");
  611.     strcpy(class_names_section.sectname,   "__class_names");
  612.     strcpy(module_info_section.segname,   "__OBJC");
  613.     strcpy(module_info_section.sectname,   "__module_info");
  614.     strcpy(symbols_section.segname,       "__OBJC");
  615.     strcpy(symbols_section.sectname,       "__symbols");
  616.     strcpy(category_section.segname,      "__OBJC");
  617.     strcpy(category_section.sectname,      "__category");
  618.     /* this copies a '\0' on segname because of the length */
  619.     strcpy(meth_var_types_section.sectname,"__meth_var_types");
  620.     strcpy(meth_var_types_section.segname,"__OBJC");
  621.     strcpy(class_vars_section.segname,    "__OBJC");
  622.     strcpy(class_vars_section.sectname,    "__class_vars");
  623.     strcpy(instance_vars_section.segname, "__OBJC");
  624.     strcpy(instance_vars_section.sectname, "__instance_vars");
  625.     /* this copies a '\0' on segname because of the length */
  626.     strcpy(meth_var_names_section.sectname,"__meth_var_names");
  627.     strcpy(meth_var_names_section.segname,"__OBJC");
  628.     strcpy(selector_strs_section.segname, "__OBJC");
  629.     strcpy(selector_strs_section.sectname, "__selector_strs");
  630.     strcpy(bss_section.segname,           "__DATA");
  631.     strcpy(bss_section.sectname,           "__bss");
  632.  
  633.     /*
  634.      * Fill in the offset to the contents of the sections and the default
  635.      * alignment.
  636.      */
  637.     for(i = 0; i < nsects; i++){
  638.         sections[i]->offset = offset;
  639.         offset += sections[i]->size;
  640.         if(sections[i] != &cstring_section &&
  641. #ifdef M68K
  642.            sections[i] != &text_section &&
  643. #endif
  644.            sections[i] != &literal4_section &&
  645.            sections[i] != &class_names_section &&
  646.            sections[i] != &meth_var_names_section &&
  647.            sections[i] != &meth_var_types_section &&
  648.            sections[i] != &selector_strs_section){
  649. #ifdef RISC
  650.         if(sections[i]->align < 3)
  651.             sections[i]->align = 3;
  652. #else
  653.         if(sections[i]->align < 2)
  654.             sections[i]->align = 2;
  655. #endif
  656.         }
  657.     }
  658.  
  659.     /* fill in the align field of the sections that are not the default */
  660. #ifdef M68K
  661.     if(text_section.align < 1)
  662.         text_section.align = 1;
  663. #endif
  664. #ifdef I860
  665.     /* 2 ^ 5 bytes, or 256 bits (needed for dirbase ops on the i860) */
  666.     if(text_section.align < 5)
  667.         text_section.align = 5;
  668.     if(data_section.align < 4)    /* 2 ^ 4 bytes, or 128 bits */
  669.         data_section.align = 4;
  670.     if(bss_section.align < 4)    /* 2 ^ 4 bytes, or 128 bits */
  671.         bss_section.align = 4;
  672. #endif
  673.     if(literal8_section.align < 3)
  674.         literal8_section.align = 3;
  675.     if(literal4_section.align < 2)
  676.         literal4_section.align = 2;
  677.  
  678.     /*
  679.      * The literal pointer sections need to be 4 byte aligned not 8 so the
  680.      * link editor does not pad them and cause an entry to exist without
  681.      * a relocation entry.
  682.      */
  683.     message_refs_section.align = 2;
  684.     cls_refs_section.align = 2;
  685.  
  686.     class_section.align = 2;
  687.     meta_class_section.align = 2;
  688.     string_object_section.align = 2;
  689.     protocol_section.align = 2;
  690.     cat_cls_meth_section.align = 2;
  691.     cat_inst_meth_section.align = 2;
  692.     cls_meth_section.align = 2;
  693.     inst_meth_section.align = 2;
  694.     module_info_section.align = 2;
  695.     symbols_section.align = 2;
  696.     category_section.align = 2;
  697.     class_vars_section.align = 2;
  698.     instance_vars_section.align = 2;
  699.  
  700.     /* count the number of relocation entries for each section */
  701.     for(fixP = text_fix_root;  fixP != NULL;  fixP = fixP->fx_next){
  702.         if(fixP->fx_addsy == NULL)
  703.         continue;
  704.         address = fixP->fx_frag->fr_address + fixP->fx_where;
  705.         for(i = 0; i < nsects; i++){
  706.         if(address >= sections[i]->addr &&
  707.            address < sections[i]->addr + sections[i]->size){
  708.             sections[i]->nreloc++;
  709. #ifdef RISC
  710.             /*
  711.              * For relocation types that encode only the half of an
  712.              * address that need both halves, are followed by a second
  713.              * relocation entry which encodes the other half.  Allow for
  714.              * this by adding one more to nreloc.
  715.              */
  716. #ifdef I860
  717.             if(fixP->fx_r_type == I860_RELOC_HIGH ||
  718.                fixP->fx_r_type == I860_RELOC_HIGHADJ)
  719. #endif
  720. #ifdef M88K
  721.             if(fixP->fx_r_type == M88K_RELOC_HI16 ||
  722.                fixP->fx_r_type == M88K_RELOC_LO16)
  723. #endif
  724. #ifdef M98K
  725.             if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  726.                fixP->fx_r_type == M98K_RELOC_LO16 ||
  727.                fixP->fx_r_type == M98K_RELOC_HA16 ||
  728.                fixP->fx_r_type == M98K_RELOC_LO14)
  729. #endif
  730.             sections[i]->nreloc++;
  731. #endif /* RISC */
  732.             break;
  733.         }
  734.         }
  735.     }
  736.     for(fixP = data_fix_root;  fixP != NULL;  fixP = fixP->fx_next){
  737.         if(fixP->fx_addsy == NULL)
  738.         continue;
  739.         address = fixP->fx_frag->fr_address + fixP->fx_where;
  740.         for(i = 0; i < nsects; i++){
  741.         if(address >= sections[i]->addr &&
  742.            address < sections[i]->addr + sections[i]->size){
  743.             sections[i]->nreloc++;
  744. #ifdef RISC
  745.             /*
  746.              * For relocation types that encode only the half of an
  747.              * address that need both halves, are followed by a second
  748.              * relocation entry which encodes the other half.  Allow for
  749.              * this by adding one more to nreloc.
  750.              */
  751. #ifdef I860
  752.             if(fixP->fx_r_type == I860_RELOC_HIGH ||
  753.                fixP->fx_r_type == I860_RELOC_HIGHADJ)
  754. #endif
  755. #ifdef M88K
  756.             if(fixP->fx_r_type == M88K_RELOC_HI16 ||
  757.                fixP->fx_r_type == M88K_RELOC_LO16)
  758. #endif
  759. #ifdef M98K
  760.             if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  761.                fixP->fx_r_type == M98K_RELOC_LO16 ||
  762.                fixP->fx_r_type == M98K_RELOC_HA16 ||
  763.                fixP->fx_r_type == M98K_RELOC_LO14)
  764. #endif
  765.             sections[i]->nreloc++;
  766. #endif /* RISC */
  767.             break;
  768.         }
  769.         }
  770.     }
  771.  
  772.     offset = round(offset, sizeof(long));
  773.     reloff = offset;
  774.     nrelocs = 0;
  775.     /* fill in the offset to the relocation entries of the sections */
  776.     for(i = 0; i < nsects; i++){
  777.         sections[i]->reloff = offset;
  778.         offset += sections[i]->nreloc * sizeof(struct relocation_info);
  779.         nrelocs += sections[i]->nreloc;
  780.     }
  781.  
  782.     /* fill in the flags field of the sections */
  783.     bss_section.flags           = S_ZEROFILL;
  784. /*
  785.  * For processors that don't have all references to these sections as unique 32
  786.  * bits wide references the literal flags are not set. This is so that the link
  787.  * editor does not merge them and get stuck not being able to fit the relocated
  788.  * address in the item to be relocated or if the high half of the reference is
  789.  * shared by two references to different symbols (which can also stick the link
  790.  * editor).
  791.  */
  792. #ifndef I860
  793.     cstring_section.flags       = S_CSTRING_LITERALS;
  794.     literal4_section.flags      = S_4BYTE_LITERALS;
  795.     literal8_section.flags      = S_8BYTE_LITERALS;
  796.     message_refs_section.flags  = S_LITERAL_POINTERS;
  797.     cls_refs_section.flags      = S_LITERAL_POINTERS;
  798.     class_names_section.flags   = S_CSTRING_LITERALS;
  799.     meth_var_names_section.flags = S_CSTRING_LITERALS;
  800.     meth_var_types_section.flags = S_CSTRING_LITERALS;
  801.     selector_strs_section.flags = S_CSTRING_LITERALS;
  802. #endif
  803.  
  804.     /* fill in the fields of the symtab_command */
  805.     symbol_table.cmd = LC_SYMTAB;
  806.     symbol_table.cmdsize = sizeof(struct symtab_command);
  807.     symbol_table.symoff = offset;
  808.     symbol_table.nsyms = symbol_number;
  809.     offset += symbol_table.nsyms * sizeof(struct nlist);
  810.     symbol_table.stroff = offset;
  811.     symbol_table.strsize = round(string_byte_count, sizeof(unsigned long));
  812.     offset += round(string_byte_count, sizeof(unsigned long));
  813.  
  814.     /* allocate the buffer for the output file */
  815.     the_object_file_size = offset;
  816.     the_object_file = xmalloc(the_object_file_size);
  817.     next_object_file_charP = the_object_file;
  818.  
  819.     /*
  820.      * First set the padded bytes at the end of the file due to the size
  821.      * of the string table being rounded to a sizeof(unsigned long).
  822.      */
  823.     the_object_file[the_object_file_size - 3] = '\0';
  824.     the_object_file[the_object_file_size - 2] = '\0';
  825.     the_object_file[the_object_file_size - 1] = '\0';
  826.  
  827.     /* put the headers in the output file's buffer */
  828.     host_byte_sex = get_host_byte_sex();
  829. #if defined(M68K) || defined(M88K) || defined(I860) || defined(M98K)
  830.     target_byte_sex = BIG_ENDIAN_BYTE_SEX;
  831. #endif
  832. #if defined(I386)
  833.     target_byte_sex = LITTLE_ENDIAN_BYTE_SEX;
  834. #endif
  835.     if(host_byte_sex != target_byte_sex){
  836.         swap_mach_header(&header, target_byte_sex);
  837.         swap_segment_command(&reloc_segment, target_byte_sex);
  838.         for(i = 0; i < nsects; i++)
  839.         swap_section(sections[i], 1, target_byte_sex);
  840.         swap_symtab_command(&symbol_table, target_byte_sex);
  841.     }
  842.  
  843.     append(&next_object_file_charP,
  844.            &header,
  845.            sizeof(struct mach_header));
  846.     append(&next_object_file_charP,
  847.            &reloc_segment,
  848.            sizeof(struct segment_command));
  849.     for(i = 0; i < nsects; i++)
  850.         append(&next_object_file_charP, sections[i],sizeof(struct section));
  851.     append(&next_object_file_charP,
  852.            &symbol_table,
  853.            sizeof(struct symtab_command));
  854.  
  855.     if(host_byte_sex != target_byte_sex){
  856.         swap_mach_header(&header, host_byte_sex);
  857.         swap_segment_command(&reloc_segment, host_byte_sex);
  858.         for(i = 0; i < nsects; i++)
  859.         swap_section(sections[i], 1, host_byte_sex);
  860.         swap_symtab_command(&symbol_table, host_byte_sex);
  861.     }
  862.  
  863. #ifdef M68K
  864.     /*
  865.      * To get only instructions in the (__TEXT,__text) section the section
  866.      * must not be padded with something that is not an instruction (or a
  867.      * full instruction as .word 0 is not a full instruction but rather half
  868.      * of an "orbi #0x??,d0".  So what was done in write_object_file() was
  869.      * to first align to a 2 byte boundary (the will not do anything if only
  870.      * text is in the text section) then align to a 4 byte boundary saving
  871.      * a handle to the frag (in frag_nop).  This is used here to put out a
  872.      * nop instruction for it in place of two zeros if it has size of 2
  873.      * (not 0).  This is a major kludge in the fact it is using 2 bytes of
  874.      * fr_literal and relying on the fact that frags are not allocated on
  875.      * one byte boundaries.
  876.      */
  877.     if(frag_nop->fr_offset == 2){
  878.         /* convert this frag from a 2 char variable fill frag to a 2 char
  879.            fixed frag */
  880.         frag_nop->fr_offset = 0;
  881.         frag_nop->fr_var = 0;
  882.         frag_nop->fr_fix = 2;
  883.         frag_nop->fr_literal[0] = 0x4e; /* 0x4e71 is a nop instruction */
  884.         frag_nop->fr_literal[1] = 0x71;
  885.     }
  886. #endif
  887.  
  888.     /* put the section contents in the output file's buffer */
  889.     for(fragP = frchain_root->frch_root;
  890.         fragP != NULL;
  891.         fragP = fragP->fr_next){
  892.  
  893.         long count;
  894.         char *fill_literal;
  895.         long fill_size;
  896.  
  897.         know(fragP->fr_type == rs_fill);
  898.         append(&next_object_file_charP, fragP->fr_literal, fragP->fr_fix);
  899.         fill_literal = fragP->fr_literal + fragP->fr_fix;
  900.         fill_size = fragP->fr_var;
  901.         know(fragP->fr_offset >= 0);
  902.         for(count = fragP->fr_offset; count != 0 ;  count--)
  903.         append(&next_object_file_charP, fill_literal, fill_size);
  904.     }
  905.  
  906.     /* put the relocation entries in the output file's buffer */
  907.     for(i = 0; i < nsects; i++)
  908.         sections[i]->nreloc = 0;
  909.     for(fixP = text_fix_root;  fixP != NULL;  fixP = fixP->fx_next){
  910.         if(fixP->fx_addsy == NULL)
  911.         continue;
  912.         address = fixP->fx_frag->fr_address + fixP->fx_where;
  913.         for(i = 0; i < nsects; i++){
  914.         if(address >= sections[i]->addr &&
  915.            address < sections[i]->addr + sections[i]->size){
  916.             fix_to_relocation_info(fixP, sections[i]->addr,
  917.                 (struct relocation_info *)(the_object_file +
  918.                 sections[i]->reloff +
  919.                 sections[i]->nreloc *
  920.                 sizeof(struct relocation_info)));
  921.             sections[i]->nreloc++;
  922. #ifdef RISC
  923.             /*
  924.              * Whenever we have a relocation item using the half of an
  925.              * address we also emit a relocation item describing the
  926.              * other half of the address if the linker needs it to
  927.              * reconstruct the address to do the relocation.
  928.              */
  929. #ifdef I860
  930.             if(fixP->fx_r_type == I860_RELOC_HIGH ||
  931.                fixP->fx_r_type == I860_RELOC_HIGHADJ){
  932. #endif
  933. #ifdef M88K
  934.             if(fixP->fx_r_type == M88K_RELOC_HI16 ||
  935.                fixP->fx_r_type == M88K_RELOC_LO16){
  936. #endif
  937. #ifdef M98K
  938.             if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  939.                fixP->fx_r_type == M98K_RELOC_LO16 ||
  940.                fixP->fx_r_type == M98K_RELOC_HA16 ||
  941.                fixP->fx_r_type == M98K_RELOC_LO14){
  942. #endif
  943.             PAIRfix_to_relocation_info(fixP, sections[i]->addr,
  944.                     (struct relocation_info *)(the_object_file +
  945.                     sections[i]->reloff +
  946.                     sections[i]->nreloc *
  947.                     sizeof(struct relocation_info)) );
  948.             sections[i]->nreloc++;
  949.             }
  950. #endif /* RISC */
  951.             break;
  952.         }
  953.         }
  954.     }
  955.     for(fixP = data_fix_root;  fixP != NULL;  fixP = fixP->fx_next){
  956.         address = fixP->fx_frag->fr_address + fixP->fx_where;
  957.         if(fixP->fx_addsy == NULL)
  958.         continue;
  959.         for(i = 0; i < nsects; i++){
  960.         if(address >= sections[i]->addr &&
  961.            address < sections[i]->addr + sections[i]->size){
  962.             fix_to_relocation_info(fixP, sections[i]->addr,
  963.                 (struct relocation_info *)(the_object_file +
  964.                 sections[i]->reloff +
  965.                 sections[i]->nreloc *
  966.                 sizeof(struct relocation_info)));
  967.             sections[i]->nreloc++;
  968. #ifdef RISC
  969.             /*
  970.              * Whenever we have a relocation item using the half of an
  971.              * address we also emit a relocation item describing the
  972.              * other half of the address if the linker needs it to
  973.              * reconstruct the address to do the relocation.
  974.              */
  975. #ifdef I860
  976.             if(fixP->fx_r_type == I860_RELOC_HIGH ||
  977.                fixP->fx_r_type == I860_RELOC_HIGHADJ){
  978. #endif
  979. #ifdef M88K
  980.             if(fixP->fx_r_type == M88K_RELOC_HI16 ||
  981.                fixP->fx_r_type == M88K_RELOC_LO16){
  982. #endif
  983. #ifdef M98K
  984.             if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  985.                fixP->fx_r_type == M98K_RELOC_LO16 ||
  986.                fixP->fx_r_type == M98K_RELOC_HA16 ||
  987.                fixP->fx_r_type == M98K_RELOC_LO14){
  988. #endif
  989.             PAIRfix_to_relocation_info(fixP, sections[i]->addr,
  990.                     (struct relocation_info *)(the_object_file +
  991.                     sections[i]->reloff +
  992.                     sections[i]->nreloc *
  993.                     sizeof(struct relocation_info)) );
  994.             sections[i]->nreloc++;
  995.             }
  996. #endif /* RISC */
  997.             break;
  998.         }
  999.         }
  1000.     }
  1001.     if(host_byte_sex != target_byte_sex)
  1002.         swap_relocation_info((struct relocation_info *)
  1003.         (the_object_file + reloff), nrelocs, target_byte_sex);
  1004.  
  1005.     /* put the symbols in the output file's buffer */
  1006.     next_object_file_charP = the_object_file + symbol_table.symoff;
  1007.     for(symbolP = symbol_rootP;
  1008.         symbolP != NULL;
  1009.         symbolP = symbolP->sy_next){
  1010.  
  1011.         register char *temp;
  1012.  
  1013.         temp = symbolP->sy_nlist.n_un.n_name;
  1014.         symbolP->sy_nlist.n_un.n_strx = symbolP->sy_name_offset;
  1015.         /* Any undefined symbols become N_EXT. */
  1016.         if(symbolP->sy_type == N_UNDF)
  1017.         symbolP->sy_type |= N_EXT;
  1018.         switch((symbolP->sy_type & N_TYPE)){
  1019.         case N_TEXT:
  1020.         case N_DATA:
  1021.         case N_BSS:
  1022.             symbolP->sy_other = new_nsect[symbolP->sy_other];
  1023.             if((symbolP->sy_type & N_STAB) == 0){
  1024.             symbolP->sy_type = N_SECT | (symbolP->sy_type & N_EXT);
  1025.             }
  1026.             break;
  1027.         }
  1028.         append(&next_object_file_charP, (char *)(&symbolP->sy_nlist),
  1029.                sizeof(struct nlist));
  1030.         symbolP->sy_nlist.n_un.n_name = temp;
  1031.     }
  1032.     if(host_byte_sex != target_byte_sex)
  1033.         swap_nlist((struct nlist *)(the_object_file + symbol_table.symoff),
  1034.                symbol_table.nsyms, target_byte_sex);
  1035.  
  1036.     /* put the strings in the output file's buffer */
  1037.     zero = 0;
  1038.     append(&next_object_file_charP,
  1039.            (char *)&zero,
  1040.            (unsigned long)sizeof(long));
  1041.     for(symbolP = symbol_rootP;
  1042.         symbolP != NULL;
  1043.         symbolP = symbolP->sy_next){
  1044.         if(symbolP->sy_name != NULL){
  1045.         /* Ordinary case: not .stabd. */
  1046.         append(&next_object_file_charP,
  1047.                symbolP->sy_name,
  1048.                (unsigned long)(strlen(symbolP->sy_name) + 1));
  1049.         }
  1050.     }
  1051.  
  1052.     /* create the output file, if no bad errors */
  1053.     if(bad_error == 0){
  1054.         output_file_create(out_file_name);
  1055.         output_write(the_object_file, the_object_file_size);
  1056.         output_file_close(out_file_name);
  1057.     }
  1058.     free(the_object_file);
  1059. }
  1060.  
  1061. /*
  1062.  * From a fix structure create a relocation entry.
  1063.  */
  1064. static
  1065. void
  1066. fix_to_relocation_info(
  1067. struct fix *fixP,
  1068. unsigned long segment_address_in_file,
  1069. struct relocation_info *riP)
  1070. {
  1071.     struct scattered_relocation_info sri;
  1072.     struct symbol *symbolP;
  1073.  
  1074.     memset(riP, '\0', sizeof(struct relocation_info));
  1075.     symbolP = fixP->fx_addsy;
  1076.     if(symbolP != NULL){
  1077.         switch(fixP->fx_size){
  1078.         case 1:
  1079.             riP->r_length = 0;
  1080.             break;
  1081.         case 2:
  1082.             riP->r_length = 1;
  1083.             break;
  1084.         case 4:
  1085.             riP->r_length = 2;
  1086.             break;
  1087.         default:
  1088.             as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
  1089.                  fixP->fx_size);
  1090.         }
  1091.         riP->r_pcrel = fixP->fx_pcrel;
  1092.         riP->r_address = fixP->fx_frag->fr_address + fixP->fx_where -
  1093.                segment_address_in_file;
  1094. #ifdef RISC
  1095.         riP->r_type = fixP->fx_r_type;
  1096. #endif
  1097.         if((symbolP->sy_type & N_TYPE) == N_UNDF){
  1098.         riP->r_extern = 1;
  1099.         riP->r_symbolnum = symbolP->sy_number;
  1100.         }
  1101.         else{
  1102.         riP->r_extern = 0;
  1103.         riP->r_symbolnum = new_nsect[symbolP->sy_other];
  1104. /*
  1105.  * For processors that don't have all references to these as unique 32 bits wide
  1106.  * references scattered relocation entries are not generated. This is so that
  1107.  * the link editor does not get stuck not being able to do the relocation if the
  1108.  * high half of the reference is shared by two references to different symbols.
  1109.  */
  1110. #ifndef I860
  1111.         /*
  1112.          * To allow the link editor to scatter the contents of a
  1113.          * section a local relocation can't be used when an offset
  1114.          * is added to the symbol's value.  For the link editor to
  1115.          * get the relocation right when it divides up a section
  1116.          * that has an item to be relocated to a symbol plus an
  1117.          * offset the link editor needs to know the value of the
  1118.          * symbol (before the offset is added) as what to base the
  1119.          * relocation on.  If this wasn't done and the value of the
  1120.          * symbol plus offset expression did not happen to be in the
  1121.          * same block as the value of symbol the link editor would
  1122.          * get the relocation wrong.  Since it would be difficult
  1123.          * to determine the lengths of the blocks in here the
  1124.          * assumption is that any non-zero offset would fall outside
  1125.          * the block that contains the symbol.  This could be done
  1126.          * for all local relocation entries but since there is no
  1127.          * room in a scattered relocation entry to place the section
  1128.          * ordinal and the symbol value they are more expensive in
  1129.          * the link editor to process since the section ordinal
  1130.          * needs to be "looked up" from the value.  So these are
  1131.          * used only when necessary.  (Also see the comments in
  1132.          * <reloc.h>).
  1133.          */
  1134.         if(
  1135. #ifdef M68K
  1136.            (fixP->fx_offset != 0 &&
  1137.             (fixP->fx_offset != 4 || !(fixP->fx_pcrel & 2)) ) &&
  1138. #else
  1139.            fixP->fx_offset != 0 &&
  1140. #endif
  1141.  
  1142.            ((symbolP->sy_type & N_TYPE) & ~N_EXT) != N_ABS &&
  1143.             (symbolP->sy_other != DATA_NSECT+20)){ /* selector_strs */
  1144.             memset(&sri, '\0',sizeof(struct scattered_relocation_info));
  1145.             sri.r_scattered = 1;
  1146.             sri.r_length    = riP->r_length;
  1147.             sri.r_pcrel     = riP->r_pcrel;
  1148.             sri.r_address   = riP->r_address;
  1149.             sri.r_type      = riP->r_type;
  1150.             sri.r_value     = symbolP->sy_value;
  1151.             *riP = *((struct relocation_info *)&sri);
  1152.         }
  1153. #endif /* !defined(I860) */
  1154.         }
  1155.     }
  1156. }
  1157.  
  1158. #ifdef RISC
  1159. /*
  1160.  * From a fix structure for a relocation type that needs two relocation entries
  1161.  * to represent it create the RELOC_PAIR relocation entry for it.
  1162.  */
  1163. static
  1164. void
  1165. PAIRfix_to_relocation_info(
  1166. struct fix *fixP,
  1167. unsigned long segment_address_in_file,
  1168. struct relocation_info *riP)
  1169. {
  1170.     memset(riP, '\0', sizeof(struct relocation_info));
  1171.     /*
  1172.      * We set r_extern to 0, so other apps won't try to use r_symbolnum
  1173.      * as a symbol table indice.  We OR in some bits in bits 16-23 of
  1174.      * r_symbolnum so it is guaranteed to be outside the range we use
  1175.      * for non-external types to denote what section the relocation is in.
  1176.      */
  1177.     switch(fixP->fx_size){
  1178.         case 1:
  1179.         riP->r_length = 0;
  1180.         break;
  1181.         case 2:
  1182.         riP->r_length = 1;
  1183.         break;
  1184.         case 4:
  1185.         riP->r_length = 2;
  1186.         break;
  1187.         default:
  1188.         as_fatal("Bad fx_size (0x%x) in PAIRfix_to_relocation_info()\n",
  1189.              fixP->fx_size);
  1190.     }
  1191.     riP->r_pcrel     = fixP->fx_pcrel;
  1192.     riP->r_symbolnum = 0x00ffffff;
  1193.     riP->r_extern     = 0;
  1194. #ifdef I860
  1195.     riP->r_type     = I860_RELOC_PAIR;
  1196.     riP->r_address   = 0xffff & fixP->fx_addnumber;
  1197. #endif
  1198. #ifdef M88K
  1199.     riP->r_type     = M88K_RELOC_PAIR;
  1200.     if(fixP->fx_r_type == M88K_RELOC_HI16)
  1201.         riP->r_address = 0xffff & fixP->fx_addnumber;
  1202.     else if(fixP->fx_r_type == M88K_RELOC_LO16)
  1203.         riP->r_address = 0xffff & (fixP->fx_addnumber >> 16);
  1204.     else
  1205.         as_fatal("Bad fx_r_type (0x%x) in PAIRfix_to_relocation_info()\n",
  1206.              fixP->fx_r_type);
  1207. #endif
  1208. #ifdef M98K
  1209.     riP->r_type     = M98K_RELOC_PAIR;
  1210.     if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  1211.        fixP->fx_r_type == M98K_RELOC_HA16)
  1212.         riP->r_address = 0xffff & fixP->fx_addnumber;
  1213.     else if(fixP->fx_r_type == M98K_RELOC_LO16 ||
  1214.         fixP->fx_r_type == M98K_RELOC_LO14)
  1215.         riP->r_address = 0xffff & (fixP->fx_addnumber >> 16);
  1216.     else
  1217.         as_fatal("Bad fx_r_type (0x%x) in PAIRfix_to_relocation_info()\n",
  1218.              fixP->fx_r_type);
  1219. #endif
  1220. }
  1221. #endif /* RISC */
  1222.  
  1223. /*
  1224.  * round() rounds v to a multiple of r.
  1225.  */
  1226. static
  1227. long
  1228. round(
  1229. long v,
  1230. unsigned long r)
  1231. {
  1232.     r--;
  1233.     v += r;
  1234.     v &= ~(long)r;
  1235.     return(v);
  1236. }
  1237. #endif /* Mach_O */
  1238.