home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / loader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  70.3 KB  |  2,279 lines  |  [TEXT/KAHL]

  1. #pragma options (!check_ptrs)
  2. #if 0
  3. #include "crtlocal.h"
  4. #include <Resources.h>
  5. #endif
  6.  
  7. /* Linker `ld' for GNU
  8.    Copyright (C) 1988 Free Software Foundation, Inc.
  9.  
  10.    This program is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 1, or (at your option)
  13.    any later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.    You should have received a copy of the GNU General Public License
  21.    along with this program; if not, write to the Free Software
  22.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. /* Written by Richard Stallman with some help from Eric Albert.
  25.    Set, indirect, and warning ld_symbol features added by Randy Smith.  */
  26.    
  27. /* Define how to initialize system-dependent header fields.  */
  28. #ifdef sun
  29. /* Use Sun's TARGET convention.  */
  30. #ifndef TARGET
  31. #define SUN2 2
  32. #define SUN3 3
  33. #define SUN4 4
  34. #if defined(sparc)
  35. #define TARGET SUN4
  36. #else
  37. #if defined(mc68020) || defined(m68020)
  38. #define TARGET SUN3
  39. #else
  40. #define TARGET SUN2
  41. #endif
  42. #endif
  43. #else
  44. #define _CROSS_TARGET_ARCH TARGET  /* locate the correct a.out.h file */
  45. #endif
  46. #endif
  47.  
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50. #include <sys/file.h>
  51. #ifndef sony_news
  52. #include <sys/fcntl.h>
  53. #endif
  54.  
  55. #ifdef COFF_ENCAPSULATE
  56. #include "a.out.encap.h"
  57. #else
  58. #include <a.out.h>
  59. #endif
  60.  
  61. #ifndef N_SET_MAGIC
  62. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  63. #endif
  64.  
  65. /* Always use the GNU version of debugging ld_symbol type codes, if possible.  */
  66.  
  67. #include "stab.h"
  68. #define CORE_ADDR unsigned long    /* For symseg.h */
  69. /* GDB symbol table format definitions.
  70.    Copyright (C) 1986, 1989 Free Software Foundation, Inc.
  71.    Hacked by Michael Tiemann (tiemann@mcc.com)
  72.  
  73. This file is part of GDB.
  74.  
  75. GDB is free software; you can redistribute it and/or modify
  76. it under the terms of the GNU General Public License as published by
  77. the Free Software Foundation; either version 1, or (at your option)
  78. any later version.
  79.  
  80. GDB is distributed in the hope that it will be useful,
  81. but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  83. GNU General Public License for more details.
  84.  
  85. You should have received a copy of the GNU General Public License
  86. along with GDB; see the file COPYING.  If not, write to
  87. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  88.  
  89. /* Format of GDB symbol table data.
  90.    There is one symbol segment for each source file or
  91.    independant compilation.  These segments are simply concatenated
  92.    to form the GDB symbol table.  A zero word where the beginning
  93.    of a segment is expected indicates there are no more segments.
  94.  
  95. Format of a symbol segment:
  96.  
  97.    The symbol segment begins with a word containing 1
  98.    if it is in the format described here.  Other formats may
  99.    be designed, with other code numbers.
  100.  
  101.    The segment contains many objects which point at each other.
  102.    The pointers are offsets in bytes from the beginning of the segment.
  103.    Thus, each segment can be loaded into core and its pointers relocated
  104.    to make valid in-core pointers.
  105.  
  106.    All the data objects in the segment can be found indirectly from
  107.    one of them, the root object, of type `struct symbol_root'.
  108.    It appears at the beginning of the segment.
  109.  
  110.    The total size of the segment, in bytes, appears as the `length'
  111.    field of this object.  This size includes the size of the
  112.    root object.
  113.  
  114.    All the object data types are defined here to contain pointer types
  115.    appropriate for in-core use on a relocated symbol segment.
  116.    Casts to and from type int are required for working with
  117.    unrelocated symbol segments such as are found in the file.
  118.  
  119.    The ldsymaddr word is filled in by the loader to contain
  120.    the offset (in bytes) within the ld symbol table
  121.    of the first nonglobal symbol from this compilation.
  122.    This makes it possible to match those symbols
  123.    (which contain line number information) reliably with
  124.    the segment they go with.
  125.  
  126.    Core addresses within the program that appear in the symbol segment
  127.    are not relocated by the loader.  They are inserted by the assembler
  128.    and apply to addresses as output by the assembler, so GDB must
  129.    relocate them when it loads the symbol segment.  It gets the information
  130.    on how to relocate from the textrel, datarel, bssrel, databeg and bssbeg
  131.    words of the root object.
  132.  
  133.    The words textrel, datarel and bssrel
  134.    are filled in by ld with the amounts to relocate within-the-file
  135.    text, data and bss addresses by; databeg and bssbeg can be
  136.    used to tell which kind of relocation an address needs.  */
  137.  
  138. enum language {language_c};
  139.  
  140. struct symbol_root
  141. {
  142.   int format;            /* Data format version */
  143.   int length;            /* # bytes in this symbol segment */
  144.   int ldsymoff;            /* Offset in ld symtab of this file's syms */
  145.   int textrel;            /* Relocation for text addresses */
  146.   int datarel;            /* Relocation for data addresses */
  147.   int bssrel;            /* Relocation for bss addresses */
  148.   char *filename;        /* Name of main source file compiled */
  149.   char *filedir;        /* Name of directory it was reached from */
  150.   struct blockvector *blockvector; /* Vector of all symbol-naming blocks */
  151.   struct typevector *typevector; /* Vector of all data types */
  152.   enum language language;    /* Code identifying the language used */
  153.   char *version;        /* Version info.  Not fully specified */
  154.   char *compilation;        /* Compilation info.  Not fully specified */
  155.   int databeg;            /* Address within the file of data start */
  156.   int bssbeg;            /* Address within the file of bss start */
  157.   struct sourcevector *sourcevector; /* Vector of line-number info */
  158. };
  159.  
  160. /* All data types of symbols in the compiled program
  161.    are represented by `struct type' objects.
  162.    All of these objects are pointed to by the typevector.
  163.    The type vector may have empty slots that contain zero.  */
  164.  
  165. struct typevector
  166. {
  167.   int length;            /* Number of types described */
  168.   struct type *type[1];
  169. };
  170.  
  171. /* Different kinds of data types are distinguished by the `code' field.  */
  172.  
  173. enum type_code
  174. {
  175.   TYPE_CODE_UNDEF,        /* Not used; catches errors */
  176.   TYPE_CODE_PTR,        /* Pointer type */
  177.   TYPE_CODE_ARRAY,        /* Array type, lower bound zero */
  178.   TYPE_CODE_STRUCT,        /* C struct or Pascal record */
  179.   TYPE_CODE_UNION,        /* C union or Pascal variant part */
  180.   TYPE_CODE_ENUM,        /* Enumeration type */
  181.   TYPE_CODE_FUNC,        /* Function type */
  182.   TYPE_CODE_INT,        /* Integer type */
  183.   TYPE_CODE_FLT,        /* Floating type */
  184.   TYPE_CODE_VOID,        /* Void type (values zero length) */
  185.   TYPE_CODE_SET,        /* Pascal sets */
  186.   TYPE_CODE_RANGE,        /* Range (integers within spec'd bounds) */
  187.   TYPE_CODE_PASCAL_ARRAY,    /* Array with explicit type of index */
  188.  
  189.   /* C++ */
  190.   TYPE_CODE_MEMBER,        /* Member type */
  191.   TYPE_CODE_METHOD,        /* Method type */
  192.   TYPE_CODE_REF        /* C++ Reference types */
  193. };
  194.  
  195. /* This appears in a type's flags word for an unsigned integer type.  */
  196. #define TYPE_FLAG_UNSIGNED 1
  197. /* This appears in a type's flags word
  198.    if it is a (pointer to a|function returning a)* built in scalar type.
  199.    These types are never freed.  */
  200. #define TYPE_FLAG_PERM 4
  201. /* This appears in a type's flags word if it is a stub type (eg. if
  202.    someone referenced a type that wasn't definined in a source file
  203.    via (struct sir_not_appearing_in_this_film *)).  */
  204. #define TYPE_FLAG_STUB 8
  205. /* Set when a classic has a constructor defined */
  206. #define    TYPE_FLAG_HAS_CONSTRUCTOR    256
  207. /* Set when a classic has a destructor defined */
  208. #define    TYPE_FLAG_HAS_DESTRUCTOR    512
  209. /* Indicates that this type is a public baseclass of another classic,
  210.    i.e. that all its public methods are available in the derived
  211.    classic. */
  212. #define    TYPE_FLAG_VIA_PUBLIC        1024
  213. /* Indicates that this type is a virtual baseclass of another classic,
  214.    i.e. that if this classic is inherited more than once by another
  215.    classic, only one set of member variables will be included. */ 
  216. #define    TYPE_FLAG_VIA_VIRTUAL        2048
  217.  
  218. struct type
  219. {
  220.   /* Code for kind of type */
  221.   enum type_code code;
  222.   /* Name of this type, or zero if none.
  223.      This is used for printing only.
  224.      Type names specified as input are defined by symbols.  */
  225.   char *name;
  226.   /* Length in bytes of storage for a value of this type */
  227.   int length;
  228.   /* For a pointer type, describes the type of object pointed to.
  229.      For an array type, describes the type of the elements.
  230.      For a function or method type, describes the type of the value.
  231.      For a range type, describes the type of the full range.
  232.      Unused otherwise.  */
  233.   struct type *target_type;
  234.   /* Type that is a pointer to this type.
  235.      Zero if no such pointer-to type is known yet.
  236.      The debugger may add the address of such a type
  237.      if it has to construct one later.  */ 
  238.   struct type *pointer_type;
  239.   /* C++: also need a reference type.  */
  240.   struct type *reference_type;
  241.   struct type **arg_types;
  242.   
  243.   /* Type that is a function returning this type.
  244.      Zero if no such function type is known here.
  245.      The debugger may add the address of such a type
  246.      if it has to construct one later.  */
  247.   struct type *function_type;
  248.  
  249. /* Handling of pointers to members:
  250.    TYPE_MAIN_VARIANT is used for pointer and pointer
  251.    to member types.  Normally it the value of the address of its
  252.    containing type.  However, for pointers to members, we must be
  253.    able to allocate pointer to member types and look them up
  254.    from some place of reference.
  255.    NEXT_VARIANT is the next element in the chain. */
  256.   struct type *main_variant, *next_variant;
  257.  
  258.   /* Flags about this type.  */
  259.   short flags;
  260.   /* Number of fields described for this type */
  261.   short nfields;
  262.   /* For structure and union types, a description of each field.
  263.      For set and pascal array types, there is one "field",
  264.      whose type is the domain type of the set or array.
  265.      For range types, there are two "fields",
  266.      the minimum and maximum values (both inclusive).
  267.      For enum types, each possible value is described by one "field".
  268.  
  269.      Using a pointer to a separate array of fields
  270.      allows all types to have the same size, which is useful
  271.      because we can allocate the space for a type before
  272.      we know what to put in it.  */
  273.   struct field
  274.     {
  275.       /* Position of this field, counting in bits from start of
  276.      containing structure.  For a function type, this is the
  277.      position in the argument list of this argument.
  278.      For a range bound or enum value, this is the value itself.  */
  279.       int bitpos;
  280.       /* Size of this field, in bits, or zero if not packed.
  281.      For an unpacked field, the field's type's length
  282.      says how many bytes the field occupies.  */
  283.       int bitsize;
  284.       /* In a struct or enum type, type of this field.
  285.      In a function type, type of this argument.
  286.      In an array type, the domain-type of the array.  */
  287.       struct type *type;
  288.       /* Name of field, value or argument.
  289.      Zero for range bounds and array domains.  */
  290.       char *name;
  291.     } *fields;
  292.  
  293.   /* C++ */
  294.   int *private_field_bits;
  295.   int *protected_field_bits;
  296.  
  297.   /* Number of methods described for this type */
  298.   short nfn_fields;
  299.   /* Number of base classes this type derives from. */
  300.   short n_baseclasses;
  301.  
  302.   /* Number of methods described for this type plus all the
  303.      methods that it derives from.  */
  304.   int nfn_fields_total;
  305.  
  306.   /* For classes, structures, and unions, a description of each field,
  307.      which consists of an overloaded name, followed by the types of
  308.      arguments that the method expects, and then the name after it
  309.      has been renamed to make it distinct.  */
  310.   struct fn_fieldlist
  311.     {
  312.       /* The overloaded name.  */
  313.       char *name;
  314.       /* The number of methods with this name.  */
  315.       int length;
  316.       /* The list of methods.  */
  317.       struct fn_field
  318.     {
  319. #if 0
  320.       /* The overloaded name */
  321.       char *name;
  322. #endif
  323.       /* The return value of the method */
  324.       struct type *type;
  325.       /* The argument list */
  326.       struct type **args;
  327.       /* The name after it has been processed */
  328.       char *physname;
  329.       /* If this is a virtual function, the offset into the vtbl-1,
  330.          else 0.  */
  331.       int voffset;
  332.     } *fn_fields;
  333.  
  334.       int *private_fn_field_bits;
  335.       int *protected_fn_field_bits;
  336.  
  337.     } *fn_fieldlists;
  338.  
  339.   unsigned char via_protected;
  340.   unsigned char via_public;
  341.  
  342.   /* For types with virtual functions, VPTR_BASETYPE is the base classic which
  343.      defined the virtual function table pointer.  VPTR_FIELDNO is
  344.      the field number of that pointer in the structure.
  345.  
  346.      For types that are pointer to member types, VPTR_BASETYPE
  347.      ifs the type that this pointer is a member of.
  348.  
  349.      Unused otherwise.  */
  350.   struct type *vptr_basetype;
  351.  
  352.   int vptr_fieldno;
  353.  
  354.   /* If this type has a base classic, put it here.
  355.      If this type is a pointer type, the chain of member pointer
  356.      types goes here.
  357.      Unused otherwise.
  358.      
  359.      Contrary to all maxims of C style and common sense, the baseclasses
  360.      are indexed from 1 to N_BASECLASSES rather than 0 to N_BASECLASSES-1
  361.      (i.e. BASECLASSES points to one *before* the first element of
  362.      the array).  */
  363.   struct type **baseclasses;
  364. };
  365.  
  366. /* All of the name-scope contours of the program
  367.    are represented by `struct block' objects.
  368.    All of these objects are pointed to by the blockvector.
  369.  
  370.    Each block represents one name scope.
  371.    Each lexical context has its own block.
  372.  
  373.    The first two blocks in the blockvector are special.
  374.    The first one contains all the symbols defined in this compilation
  375.    whose scope is the entire program linked together.
  376.    The second one contains all the symbols whose scope is the
  377.    entire compilation excluding other separate compilations.
  378.    In C, these correspond to global symbols and static symbols.
  379.  
  380.    Each block records a range of core addresses for the code that
  381.    is in the scope of the block.  The first two special blocks
  382.    give, for the range of code, the entire range of code produced
  383.    by the compilation that the symbol segment belongs to.
  384.  
  385.    The blocks appear in the blockvector
  386.    in order of increasing starting-address,
  387.    and, within that, in order of decreasing ending-address.
  388.  
  389.    This implies that within the body of one function
  390.    the blocks appear in the order of a depth-first tree walk.  */
  391.  
  392. struct blockvector
  393. {
  394.   /* Number of blocks in the list.  */
  395.   int nblocks;
  396.   /* The blocks themselves.  */
  397.   struct block *block[1];
  398. };
  399.  
  400. struct block
  401. {
  402.   /* Addresses in the executable code that are in this block.
  403.      Note: in an unrelocated symbol segment in a file,
  404.      these are always zero.  They can be filled in from the
  405.      N_LBRAC and N_RBRAC symbols in the loader symbol table.  */
  406.   int startaddr, endaddr;
  407.   /* The symbol that names this block,
  408.      if the block is the body of a function;
  409.      otherwise, zero.
  410.      Note: In an unrelocated symbol segment in an object file,
  411.      this field may be zero even when the block has a name.
  412.      That is because the block is output before the name
  413.      (since the name resides in a higher block).
  414.      Since the symbol does point to the block (as its value),
  415.      it is possible to find the block and set its name properly.  */
  416.   struct symbol *function;
  417.   /* The `struct block' for the containing block, or 0 if none.  */
  418.   /* Note that in an unrelocated symbol segment in an object file
  419.      this pointer may be zero when the correct value should be
  420.      the second special block (for symbols whose scope is one compilation).
  421.      This is because the compiler ouptuts the special blocks at the
  422.      very end, after the other blocks.   */
  423.   struct block *superblock;
  424.   /* A flag indicating whether or not the fucntion corresponding
  425.      to this block was compiled with gcc or not.  If there is no
  426.      function corresponding to this block, this meaning of this flag
  427.      is undefined.  (In practice it will be 1 if the block was created
  428.      while processing a file compiled with gcc and 0 when not). */
  429.   unsigned char gcc_compile_flag;
  430.   /* Number of local symbols.  */
  431.   int nsyms;
  432.   /* The symbols.  */
  433.   struct symbol *sym[1];
  434. };
  435.  
  436. /* Represent one symbol name; a variable, constant, function or typedef.  */
  437.  
  438. /* Different name spaces for symbols.  Looking up a symbol specifies
  439.    a namespace and ignores symbol definitions in other name spaces.
  440.  
  441.    VAR_NAMESPACE is the usual namespace.
  442.    In C, this contains variables, function names, typedef names
  443.    and enum type values.
  444.  
  445.    STRUCT_NAMESPACE is used in C to hold struct, union and enum type names.
  446.    Thus, if `struct foo' is used in a C program,
  447.    it produces a symbol named `foo' in the STRUCT_NAMESPACE.
  448.  
  449.    LABEL_NAMESPACE may be used for names of labels (for gotos);
  450.    currently it is not used and labels are not recorded at all.  */
  451.  
  452. /* For a non-global symbol allocated statically,
  453.    the correct core address cannot be determined by the compiler.
  454.    The compiler puts an index number into the symbol's value field.
  455.    This index number can be matched with the "desc" field of
  456.    an entry in the loader symbol table.  */
  457.  
  458. enum namespace
  459. {
  460.   UNDEF_NAMESPACE, VAR_NAMESPACE, STRUCT_NAMESPACE, LABEL_NAMESPACE
  461. };
  462.  
  463. /* An address-classic says where to find the value of the symbol in core.  */
  464.  
  465. enum address_class
  466. {
  467.   LOC_UNDEF,        /* Not used; catches errors */
  468.   LOC_CONST,        /* Value is constant int */
  469.   LOC_STATIC,        /* Value is at fixed address */
  470.   LOC_REGISTER,        /* Value is in register */
  471.   LOC_ARG,        /* Value is at spec'd position in arglist */
  472.   LOC_REF_ARG,        /* Value address is at spec'd position in */
  473.             /* arglist.  */
  474.   LOC_REGPARM,        /* Value is at spec'd position in  register window */
  475.   LOC_LOCAL,        /* Value is at spec'd pos in stack frame */
  476.   LOC_TYPEDEF,        /* Value not used; definition in SYMBOL_TYPE
  477.                Symbols in the namespace STRUCT_NAMESPACE
  478.                all have this classic.  */
  479.   LOC_LABEL,        /* Value is address in the code */
  480.   LOC_BLOCK,        /* Value is address of a `struct block'.
  481.                Function names have this classic.  */
  482.   LOC_EXTERNAL,        /* Value is at address not in this compilation.
  483.                This is used for .comm symbols
  484.                and for extern symbols within functions.
  485.                Inside GDB, this is changed to LOC_STATIC once the
  486.                real address is obtained from a loader symbol.  */
  487.   LOC_CONST_BYTES    /* Value is a constant byte-sequence.   */
  488. };
  489.  
  490. struct symbol
  491. {
  492.   /* Symbol name */
  493.   char *name;
  494.   /* Name space code.  */
  495.   enum namespace namespace;
  496.   /* Address classic */
  497.   enum address_class classic;
  498.   /* Data type of value */
  499.   struct type *type;
  500.   /* constant value, or address if static, or register number,
  501.      or offset in arguments, or offset in stack frame.  */
  502.   union
  503.     {
  504.       long value;
  505.       struct block *block;      /* for LOC_BLOCK */
  506.       char *bytes;        /* for LOC_CONST_BYTES */
  507.     }
  508.   value;
  509. };
  510.  
  511. struct partial_symbol
  512. {
  513.   /* Symbol name */
  514.   char *name;
  515.   /* Name space code.  */
  516.   enum namespace namespace;
  517.   /* Address classic (for info_symbols) */
  518.   enum address_class classic;
  519.   /* Value (only used for static functions currently).  Done this
  520.      way so that we can use the struct symbol macros.
  521.      Note that the address of a function is SYMBOL_VALUE (pst)
  522.      in a partial symbol table, but BLOCK_START (SYMBOL_BLOCK_VALUE (st))
  523.      in a symbol table.  */
  524.   union
  525.     {
  526.       long value;
  527.     }
  528.   value;
  529. };
  530.  
  531. /* 
  532.  * Vectors of all partial symbols read in from file; actually declared
  533.  * and used in dbxread.c.
  534.  */
  535. extern struct psymbol_allocation_list {
  536.   struct partial_symbol *list, *next;
  537.   int size;
  538. } global_psymbols, static_psymbols;
  539.  
  540.  
  541. /* Source-file information.
  542.    This describes the relation between source files and line numbers
  543.    and addresses in the program text.  */
  544.  
  545. struct sourcevector
  546. {
  547.   int length;            /* Number of source files described */
  548.   struct source *source[1];    /* Descriptions of the files */
  549. };
  550.  
  551. /* Each item represents a line-->pc (or the reverse) mapping.  This is
  552.    somewhat more wasteful of space than one might wish, but since only
  553.    the files which are actually debugged are read in to core, we don't
  554.    waste much space.
  555.  
  556.    Each item used to be an int; either minus a line number, or a
  557.    program counter.  If it represents a line number, that is the line
  558.    described by the next program counter value.  If it is positive, it
  559.    is the program counter at which the code for the next line starts.  */
  560.  
  561. struct linetable_entry
  562. {
  563.   int line;
  564.   CORE_ADDR pc;
  565. };
  566.  
  567. struct linetable
  568. {
  569.   int nitems;
  570.   struct linetable_entry item[1];
  571. };
  572.  
  573. /* All the information on one source file.  */
  574.  
  575. struct source
  576. {
  577.   char *name;            /* Name of file */
  578.   struct linetable contents;
  579. };
  580.  
  581. /*
  582. #ifdef USG
  583. #include <string.h>
  584. #else
  585. #include <strings.h>
  586. #endif
  587. */
  588.  
  589. #define min(a,b) ((a) < (b) ? (a) : (b))
  590.  
  591. /* Macro to control the number of undefined references printed */
  592. #define MAX_UREFS_PRINTED    10
  593.  
  594. /*
  595.  * Ok.  Following are the relocation information macros.  If your
  596.  * system should not be able to use the default set (below), you must
  597.  * define the following:
  598.  
  599.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  600.  * of structure that is stored in the relocation info section of your
  601.  * a.out files.  Often this is defined in the a.out.h for your system.
  602.  *
  603.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  604.  * <whatever> to be relocated.  *Must be an lvalue*.
  605.  *
  606.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  607.  * external ld_symbol (1), or was it fully resolved upon entering the
  608.  * loader (0) in which case some combination of the value in memory
  609.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  610.  * what the value of the relocation actually was.  *Must be an lvalue*.
  611.  *
  612.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  613.  * entering the loader, what type should it be relocated as?
  614.  *
  615.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  616.  * entering the loader, what is the index of it's ld_symbol in the ld_symbol
  617.  * table?  *Must be a lvalue*.
  618.  *
  619.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  620.  * relocation value output here should be added to memory, or if the
  621.  * section of memory described should simply be set to the relocation
  622.  * value.
  623.  *
  624.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  625.  * an extra value to be added to the relocation value based on the
  626.  * individual relocation entry.  *Must be an lvalue if defined*.
  627.  *
  628.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  629.  * pc relative.
  630.  *
  631.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  632.  * final relocation value before putting it where it belongs.
  633.  *
  634.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  635.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  636.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  637.  * do everything in terms of the bit operators below), but having this
  638.  * macro could end up producing better code on machines without fancy
  639.  * bit twiddling.  Also, it's easier to understand/code big/little
  640.  * endian distinctions with this macro.
  641.  *
  642.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  643.  * object described in RELOC_TARGET_SIZE in which the relocation value
  644.  * will go.
  645.  *
  646.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  647.  * with the bits of the relocation value.  It may be assumed by the
  648.  * code that the relocation value will fit into this many bits.  This
  649.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  650.  *
  651.  *
  652.  *        Things I haven't implemented
  653.  *        ----------------------------
  654.  *
  655.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  656.  *
  657.  *    Pc relative relocation for External references.
  658.  *
  659.  *
  660.  */
  661.  
  662. /* The following #if has been modifed for cross compilation */
  663. /* It originally ld_read:  #if defined(sun) && defined(sparc)  */
  664. /* Marc Ullman, Stanford University    Nov. 1 1989  */
  665. #if defined(sun) && (TARGET == SUN4)
  666. /* Sparc (Sun 4) macros */
  667. #undef relocation_info
  668. #define relocation_info                    reloc_info_sparc
  669. #define RELOC_ADDRESS(r)        ((r)->r_address)
  670. #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  671. #define RELOC_TYPE(r)                   ((r)->r_index)
  672. #define RELOC_SYMBOL(r)                 ((r)->r_index)
  673. #define RELOC_MEMORY_SUB_P(r)        0
  674. #define RELOC_MEMORY_ADD_P(r)           0
  675. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  676. #define RELOC_PCREL_P(r)             \
  677.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  678. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  679. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  680. #define RELOC_TARGET_BITPOS(r)          0
  681. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  682.  
  683. /* Note that these are very dependent on the order of the enums in
  684.    enum reloc_type (in a.out.h); if they change the following must be
  685.    changed */
  686. /* Also note that the last few may be incorrect; I have no information */
  687. static const int const reloc_target_rightshift[] = {
  688.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  689. };
  690. static const int const reloc_target_size[] = {
  691.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  692. };
  693. static const int const reloc_target_bitsize[] = {
  694.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  695. };
  696.  
  697. #define    MAX_ALIGNMENT    (sizeof (double))
  698. #endif
  699. #ifdef sequent
  700. #define RELOC_ADDRESS(r)        ((r)->r_address)
  701. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  702. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  703. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  704. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  705. #define RELOC_MEMORY_ADD_P(r)    1
  706. #undef RELOC_ADD_EXTRA
  707. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  708. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  709. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  710. #define RELOC_TARGET_BITPOS(r)    0
  711. #define RELOC_TARGET_BITSIZE(r)    32
  712. #endif
  713.  
  714. /* Default macros */
  715. #ifndef RELOC_ADDRESS
  716. #define RELOC_ADDRESS(r)        ((r)->r_address)
  717. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  718. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  719. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  720. #define RELOC_MEMORY_SUB_P(r)    0
  721. #define RELOC_MEMORY_ADD_P(r)    1
  722. #undef RELOC_ADD_EXTRA
  723. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  724. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  725. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  726. #define RELOC_TARGET_BITPOS(r)    0
  727. #define RELOC_TARGET_BITSIZE(r)    32
  728. #endif
  729.  
  730. #ifndef MAX_ALIGNMENT
  731. #define    MAX_ALIGNMENT    (sizeof (int))
  732. #endif
  733.  
  734. #ifdef nounderscore
  735. #define LPREFIX '.'
  736. #else
  737. #define LPREFIX 'L'
  738. #endif
  739.  
  740. #ifndef TEXT_START
  741. #define TEXT_START(x) N_TXTADDR(x)
  742. #endif
  743.  
  744. /* Special global ld_symbol types understood by GNU LD.  */
  745.  
  746. /* The following type indicates the definition of a ld_symbol as being
  747.    an indirect reference to another ld_symbol.  The other ld_symbol
  748.    appears as an undefined reference, immediately following this ld_symbol.
  749.  
  750.    Indirection is asymmetrical.  The other ld_symbol's value will be used
  751.    to satisfy requests for the indirect ld_symbol, but not vice versa.
  752.    If the other ld_symbol does not have a definition, libraries will
  753.    be searched to find a definition.
  754.  
  755.    So, for example, the following two lines placed in an assembler
  756.    input file would result in an object file which would direct gnu ld
  757.    to resolve all references to ld_symbol "foo" as references to ld_symbol
  758.    "bar".
  759.  
  760.     .stabs "_foo",11,0,0,0
  761.     .stabs "_bar",1,0,0,0
  762.  
  763.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  764.  
  765. #ifndef N_INDR
  766. #define N_INDR 0xa
  767. #endif
  768.  
  769. /* The following symbols refer to set elements.  These are expected
  770.    only in input to the loader; they should not appear in loader
  771.    output (unless relocatable output is requested).  To be recognized
  772.    by the loader, the input symbols must have their N_EXT bit set.
  773.    All the N_SET[ATDB] symbols with the same name form one set.  The
  774.    loader collects all of these elements at load time and outputs a
  775.    vector for each name.
  776.    Space (an array of 32 bit words) is allocated for the set in the
  777.    data section, and the n_value field of each set element value is
  778.    stored into one word of the array.
  779.    The first word of the array is the length of the set (number of
  780.    elements).  The last word of the vector is set to zero for possible
  781.    use by incremental loaders.  The array is ordered by the linkage
  782.    order; the first symbols which the linker encounters will be first
  783.    in the array.
  784.  
  785.    In C syntax this looks like:
  786.  
  787.     struct set_vector {
  788.       unsigned int length;
  789.       unsigned int vector[length];
  790.       unsigned int always_zero;
  791.     };
  792.  
  793.    Before being placed into the array, each element is relocated
  794.    according to its type.  This allows the loader to create an array
  795.    of pointers to objects automatically.  N_SETA type symbols will not
  796.    be relocated.
  797.  
  798.    The address of the set is made into an N_SETV ld_symbol
  799.    whose name is the same as the name of the set.
  800.    This ld_symbol acts like a N_DATA global ld_symbol
  801.    in that it can satisfy undefined external references.
  802.  
  803.    For the purposes of determining whether or not to load in a library
  804.    file, set element definitions are not considered "real
  805.    definitions"; they will not cause the loading of a library
  806.    member.
  807.  
  808.    If relocatable output is requested, none of this processing is
  809.    done.  The symbols are simply relocated and passed through to the
  810.    output file.
  811.  
  812.    So, for example, the following three lines of assembler code
  813.    (whether in one file or scattered between several different ones)
  814.    will produce a three element vector (total length is five words;
  815.    see above), referenced by the ld_symbol "_xyzzy", which will have the
  816.    addresses of the routines _init1, _init2, and _init3.
  817.  
  818.    *NOTE*: If symbolic addresses are used in the n_value field of the
  819.    defining .stabs, those symbols must be defined in the same file as
  820.    that containing the .stabs.
  821.  
  822.     .stabs "_xyzzy",23,0,0,_init1
  823.     .stabs "_xyzzy",23,0,0,_init2
  824.     .stabs "_xyzzy",23,0,0,_init3
  825.  
  826.    Note that (23 == (N_SETT | N_EXT)).  */
  827.  
  828. #ifndef N_SETA
  829. #define    N_SETA    0x14        /* Absolute set element ld_symbol */
  830. #endif                /* This is input to LD, in a .o file.  */
  831.  
  832. #ifndef N_SETT
  833. #define    N_SETT    0x16        /* Text set element ld_symbol */
  834. #endif                /* This is input to LD, in a .o file.  */
  835.  
  836. #ifndef N_SETD
  837. #define    N_SETD    0x18        /* Data set element ld_symbol */
  838. #endif                /* This is input to LD, in a .o file.  */
  839.  
  840. #ifndef N_SETB
  841. #define    N_SETB    0x1A        /* Bss set element ld_symbol */
  842. #endif                /* This is input to LD, in a .o file.  */
  843.  
  844. /* Macros dealing with the set element symbols defined in a.out.h */
  845. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  846. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  847.  
  848. #ifndef N_SETV
  849. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  850. #endif                /* This is output from LD.  */
  851.  
  852. /* If a this type of ld_symbol is encountered, its name is a warning
  853.    message to print each time the ld_symbol referenced by the next ld_symbol
  854.    table entry is referenced.
  855.  
  856.    This feature may be used to allow backwards compatibility with
  857.    certain functions (eg. gets) but to discourage programmers from
  858.    their use.
  859.  
  860.    So if, for example, you wanted to have ld print a warning whenever
  861.    the function "gets" was used in their C program, you would add the
  862.    following to the assembler file in which gets is defined:
  863.  
  864.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  865.     .stabs "_gets",1,0,0,0
  866.  
  867.    These .stabs do not necessarily have to be in the same file as the
  868.    gets function, they simply must exist somewhere in the compilation.  */
  869.  
  870. #ifndef N_WARNING
  871. #define N_WARNING 0x1E        /* Warning message to print if ld_symbol
  872.                    included */
  873. #endif                /* This is input to ld */
  874.  
  875. #ifndef __GNU_STAB__
  876.  
  877. /* Line number for the data section.  This is to be used to describe
  878.    the source location of a variable declaration.  */
  879. #ifndef N_DSLINE
  880. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  881. #endif
  882.  
  883. /* Line number for the bss section.  This is to be used to describe
  884.    the source location of a variable declaration.  */
  885. #ifndef N_BSLINE
  886. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  887. #endif
  888.  
  889. #endif /* not __GNU_STAB__ */
  890.  
  891. /* Symbol table */
  892.  
  893. /* Global ld_symbol data is recorded in these structures,
  894.    one for each global ld_symbol.
  895.    They are found via hashing in 'symtab', which points to a vector of buckets.
  896.    Each bucket is a chain of these structures through the link field.  */
  897.  
  898. typedef
  899.   struct glosym
  900.     {
  901.       /* Pointer to next ld_symbol in this ld_symbol's hash bucket.  */
  902.       struct glosym *link;
  903.       /* Name of this ld_symbol.  */
  904.       char *name;
  905.       /* Value of this ld_symbol as a global ld_symbol.  */
  906.       long value;
  907.       /* Chain of external 'nlist's in files for this ld_symbol, both defs
  908.      and refs.  */
  909.       struct nlist *refs;
  910.       /* Any warning message that might be associated with this ld_symbol
  911.          from an N_WARNING ld_symbol encountered. */
  912.       char *warning;
  913.       /* Nonzero means definitions of this ld_symbol as common have been seen,
  914.      and the value here is the largest size specified by any of them.  */
  915.       int max_common_size;
  916.       /* For relocatable_output, records the index of this global sym in the
  917.      ld_symbol table to be written, with the first global sym given index 0.*/
  918.       int def_count;
  919.       /* Nonzero means a definition of this global ld_symbol is known to exist.
  920.      Library members should not be loaded on its account.  */
  921.       char defined;
  922.       /* Nonzero means a reference to this global ld_symbol has been seen
  923.      in a file that is surely being loaded.
  924.      A value higher than 1 is the n_type code for the ld_symbol's
  925.      definition.  */
  926.       char referenced;
  927.       /* A count of the number of undefined references printed for a
  928.      specific ld_symbol.  If a ld_symbol is unresolved at the end of
  929.      digest_symbols (and the loading run is supposed to produce
  930.      relocatable output) do_file_warnings keeps track of how many
  931.      unresolved reference error messages have been printed for
  932.      each ld_symbol here.  When the number hits MAX_UREFS_PRINTED,
  933.      messages stop. */
  934.       unsigned char undef_refs;
  935.       /* 1 means that this ld_symbol has multiple definitions.  2 means
  936.          that it has multiple definitions, and some of them are set
  937.      elements, one of which has been printed out already.  */
  938.       unsigned char multiply_defined;
  939.       /* Nonzero means print a message at all refs or defs of this ld_symbol */
  940.       char trace;
  941.     }
  942.   ld_symbol;
  943.  
  944. /* Demangler for C++. */
  945. extern char *cplus_demangle ();
  946.  
  947. /* Demangler function to use. */
  948. char *(*demangler)() = NULL;
  949.  
  950. /* Number of buckets in ld_symbol hash table */
  951. #define    TABSIZE    1009
  952.  
  953. /* The ld_symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  954. ld_symbol *symtab[TABSIZE];
  955.  
  956. /* Number of symbols in ld_symbol hash table. */
  957. int num_hash_tab_syms = 0;
  958.  
  959. /* Count the number of nlist entries that are for local symbols.
  960.    This count and the three following counts
  961.    are incremented as as symbols are entered in the ld_symbol table.  */
  962. int local_sym_count;
  963.  
  964. /* Count number of nlist entries that are for local symbols
  965.    whose names don't start with L. */
  966. int non_L_local_sym_count;
  967.  
  968. /* Count the number of nlist entries for debugger info.  */
  969. int debugger_sym_count;
  970.  
  971. /* Count the number of global symbols referenced and not defined.  */
  972. int undefined_global_sym_count;
  973.  
  974. /* Count the number of global symbols multiply defined.  */
  975. int multiple_def_count;
  976.  
  977. /* Count the number of defined global symbols.
  978.    Each ld_symbol is counted only once
  979.    regardless of how many different nlist entries refer to it,
  980.    since the output file will need only one nlist entry for it.
  981.    This count is computed by `digest_symbols';
  982.    it is undefined while symbols are being loaded. */
  983. int defined_global_sym_count;
  984.  
  985. /* Count the number of symbols defined through common declarations.
  986.    This count is kept in symdef_library, linear_library, and
  987.    enter_global_ref.  It is incremented when the defined flag is set
  988.    in a ld_symbol because of a common definition, and decremented when
  989.    the ld_symbol is defined "for real" (ie. by something besides a common
  990.    definition).  */
  991. int common_defined_global_count;
  992.  
  993. /* Count the number of set element type symbols and the number of
  994.    separate vectors which these symbols will fit into.  See the
  995.    GNU a.out.h for more info.
  996.    This count is computed by 'enter_file_symbols' */
  997. int set_symbol_count;
  998. int set_vector_count;
  999.  
  1000. /* Define a linked list of strings which define symbols which should
  1001.    be treated as set elements even though they aren't.  Any ld_symbol
  1002.    with a prefix matching one of these should be treated as a set
  1003.    element.
  1004.  
  1005.    This is to make up for deficiencies in many assemblers which aren't
  1006.    willing to pass any stabs through to the loader which they don't
  1007.    understand.  */
  1008. struct string_list_element {
  1009.   char *str;
  1010.   struct string_list_element *next;
  1011. };
  1012.  
  1013. struct string_list_element *set_element_prefixes;
  1014.  
  1015. /* Count the number of definitions done indirectly (ie. done relative
  1016.    to the value of some other ld_symbol. */
  1017. int global_indirect_count;
  1018.  
  1019. /* Count the number of warning symbols encountered. */
  1020. int warning_count;
  1021.  
  1022. /* Total number of symbols to be written in the output file.
  1023.    Computed by digest_symbols from the variables above.  */
  1024. int nsyms;
  1025.  
  1026.  
  1027. /* Nonzero means ptr to ld_symbol entry for ld_symbol to use as start addr.
  1028.    -e sets this.  */
  1029. ld_symbol *edata_symbol;   /* the ld_symbol _edata */
  1030. ld_symbol *etext_symbol;   /* the ld_symbol _etext */
  1031. ld_symbol *end_symbol;    /* the ld_symbol _end */
  1032. ld_symbol *big_stack;    /* the ld_symbol _end */
  1033.  
  1034. /* Each input file, and each library member ("subfile") being loaded,
  1035.    has a `file_entry' structure for it.
  1036.  
  1037.    For files specified by command args, these are contained in the vector
  1038.    which `file_table' points to.
  1039.  
  1040.    For library members, they are dynamically allocated,
  1041.    and chained through the `chain' field.
  1042.    The chain is found in the `subfiles' field of the `file_entry'.
  1043.    The `file_entry' objects for the members have `superfile' fields pointing
  1044.    to the one for the library.  */
  1045.  
  1046. struct file_entry {
  1047.   /* Name of this file.  */
  1048.   char *filename;
  1049.   /* Name to use for the ld_symbol giving address of text start */
  1050.   /* Usually the same as filename, but for a file spec'd with -l
  1051.      this is the -l switch itself rather than the filename.  */
  1052.   char *local_sym_name;
  1053.  
  1054.   /* Describe the layout of the contents of the file */
  1055.  
  1056.   /* The file's a.out header.  */
  1057.   struct exec header;
  1058.   /* Offset in file of GDB ld_symbol segment, or 0 if there is none.  */
  1059.   int symseg_offset;
  1060.  
  1061.   /* Describe data from the file loaded into core */
  1062.  
  1063.   /* Symbol table of the file.  */
  1064.   struct nlist *symbols;
  1065.   /* Size in bytes of string table.  */
  1066.   int string_size;
  1067.   /* Pointer to the string table.
  1068.      The string table is not kept in core all the time,
  1069.      but when it is in core, its address is here.  */
  1070.   char *strings;
  1071.  
  1072.   /* Next two used only if `relocatable_output' or if needed for */
  1073.   /* output of undefined reference line numbers. */
  1074.  
  1075.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  1076.   struct relocation_info *textrel;
  1077.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  1078.   struct relocation_info *datarel;
  1079.  
  1080.   /* Relation of this file's segments to the output file */
  1081.  
  1082.   /* Start of this file's text seg in the output file core image.  */
  1083.   int text_start_address;
  1084.   /* Start of this file's data seg in the output file core image.  */
  1085.   int data_start_address;
  1086.   /* Start of this file's bss seg in the output file core image.  */
  1087.   int bss_start_address;
  1088.   /* Offset in bytes in the output file ld_symbol table
  1089.      of the first local ld_symbol for this file.  Set by `write_file_symbols'.  */
  1090.   int local_syms_offset;
  1091.  
  1092.   /* For library members only */
  1093.  
  1094.   /* For a library, points to chain of entries for the library members.  */
  1095.   struct file_entry *subfiles;
  1096.   /* For a library member, offset of the member within the archive.
  1097.      Zero for files that are not library members.  */
  1098.   int starting_offset;
  1099.   /* Size of contents of this file, if library member.  */
  1100.   int total_size;
  1101.   /* For library member, points to the library's own entry.  */
  1102.   struct file_entry *superfile;
  1103.   /* For library member, points to next entry for next member.  */
  1104.   struct file_entry *chain;
  1105.  
  1106.   /* 1 if file is a library. */
  1107.   char library_flag;
  1108.  
  1109.   /* 1 if file's header has been ld_read into this structure.  */
  1110.   char header_read_flag;
  1111.  
  1112.   /* 1 means search a set of directories for this file.  */
  1113.   char search_dirs_flag;
  1114.  
  1115.   /* 1 means this is base file of incremental load.
  1116.      Do not load this file's text or data.
  1117.      Also default text_start to after this file's bss. */
  1118.   char just_syms_flag;
  1119. };
  1120.  
  1121. /* Vector of entries for input files specified by arguments.
  1122.    These are all the input files except for members of specified libraries.  */
  1123. struct file_entry *file_table;
  1124.  
  1125. /* When loading the text and data, we can avoid doing a close
  1126.    and another open between members of the same library.
  1127.  
  1128.    These two variables remember the file that is currently open.
  1129.    Both are zero if no file is open.
  1130.  
  1131.    See `each_file' and `file_close'.  */
  1132.  
  1133. struct file_entry *input_file;
  1134. int input_desc;
  1135.  
  1136. /* The following are computed by `digest_symbols'.  */
  1137.  
  1138. int text_size;        /* total size of text of all input files.  */
  1139. int data_size;        /* total size of data of all input files.  */
  1140. int bss_size;        /* total size of bss of all input files.  */
  1141. int text_reloc_size;    /* total size of text relocation of all input files.  */
  1142. int data_reloc_size;    /* total size of data relocation of all input */
  1143.             /* files.  */
  1144.  
  1145. /* Specifications of start and length of the area reserved at the end
  1146.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  1147. int set_sect_start;
  1148. int set_sect_size;
  1149.  
  1150. /* Amount of cleared space to leave between the text and data segments.  */
  1151.  
  1152. int text_pad;
  1153.  
  1154. /* Amount of bss segment to include as part of the data segment.  */
  1155.  
  1156. int data_pad;
  1157.  
  1158. /* Format of __.SYMDEF:
  1159.    First, a longword containing the size of the 'symdef' data that follows.
  1160.    Second, zero or more 'symdef' structures.
  1161.    Third, a longword containing the length of ld_symbol name strings.
  1162.    Fourth, zero or more ld_symbol name strings (each followed by a null).  */
  1163.  
  1164. struct symdef {
  1165.   int symbol_name_string_index;
  1166.   int library_member_offset;
  1167. };
  1168.  
  1169. /* Record most of the command options.  */
  1170.  
  1171. /* Address we assume the text section will be loaded at.
  1172.    We relocate symbols and text and data for this, but we do not
  1173.    write any padding in the output file for it.  */
  1174. int text_start;
  1175.  
  1176. /* Offset of default entry-pc within the text section.  */
  1177. int entry_offset;
  1178.  
  1179. /* Address we decide the data section will be loaded at.  */
  1180. int data_start;
  1181.  
  1182. /* Size to pad data section up to.
  1183.    We simply increase the size of the data section, padding with zeros,
  1184.    and reduce the size of the bss section to match.  */
  1185. int specified_data_size;
  1186.  
  1187. /* Magic number to use for the output file, set by switch.  */
  1188. int magic;
  1189.  
  1190. /* Which symbols should be stripped (omitted from the output):
  1191.    none, all, or debugger symbols.  */
  1192. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  1193.  
  1194. /* Which local symbols should be omitted:
  1195.    none, all, or those starting with L.
  1196.    This is irrelevant if STRIP_NONE.  */
  1197. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  1198.  
  1199. /* Non zero means to create the output executable. */
  1200. /* Cleared by nonfatal errors.  */
  1201. int make_executable;
  1202.  
  1203. /* Force the executable to be output, even if there are non-fatal
  1204.    errors */
  1205. int force_executable;
  1206.  
  1207. void bcopy (), bzero ();
  1208.  
  1209. int fatal_with_file ();
  1210. void error ();
  1211.  
  1212. int digest_symbols ();
  1213. void print_symbols ();
  1214. void load_symbols ();
  1215. void decode_command ();
  1216. void list_undefined_symbols ();
  1217. void list_unresolved_references ();
  1218. void write_output ();
  1219. void write_header ();
  1220. void write_text ();
  1221. void write_data ();
  1222. void write_rel ();
  1223. void write_syms ();
  1224. void write_symsegs ();
  1225. void mywrite ();
  1226. void symtab_init ();
  1227. void padfile ();
  1228. char *get_file_name ();
  1229. ld_symbol *getsym (), *getsym_soft ();
  1230.  
  1231. char *resource_text;
  1232.  
  1233. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1234. void enter_file_symbols (), enter_global_ref (), search_library ();
  1235. void modify_location (), copy_text (), copy_data ();
  1236. int perform_relocation (
  1237.      char *data,
  1238.      int pc_relocation,
  1239.      int data_size,
  1240.      struct relocation_info *reloc_info,
  1241.      int reloc_size,
  1242.      struct file_entry *entry);
  1243.  
  1244. /* Offsets and current lengths of ld_symbol and string tables in output file. */
  1245.  
  1246. int symbol_table_offset;
  1247. int symbol_table_len;
  1248.  
  1249. /* Address in output file where string table starts.  */
  1250. int string_table_offset;
  1251.  
  1252. /* Offset within string table
  1253.    where the strings in `strtab_vector' should be written.  */
  1254. int string_table_len;
  1255.  
  1256. /* Total size of string table strings allocated so far,
  1257.    including strings in `strtab_vector'.  */
  1258. int strtab_size;
  1259.  
  1260. /* Vector whose elements are strings to be added to the string table.  */
  1261. char **strtab_vector;
  1262.  
  1263. /* Vector whose elements are the lengths of those strings.  */
  1264. int *strtab_lens;
  1265.  
  1266. /* Index in `strtab_vector' at which the next string will be stored.  */
  1267. int strtab_index;
  1268.  
  1269. char *xmalloc();
  1270.  
  1271. int loader_main (int desc, struct exec *hdr)
  1272. {
  1273.   register char *bytes;
  1274.   struct file_entry fe;
  1275.   register int len,coresize,resource_size;
  1276.   register int i;
  1277.  
  1278. #if 1
  1279. bss_size = 0;
  1280. common_defined_global_count = 0;
  1281. data_pad = 0;
  1282. data_reloc_size = 0;
  1283. data_size = 0;
  1284. data_start = 0;
  1285. debugger_sym_count = 0;
  1286. defined_global_sym_count = 0;
  1287. demangler = 0;
  1288. discard_locals = 0;
  1289. edata_symbol = 0;
  1290. end_symbol = 0;
  1291. entry_offset = 0;
  1292. etext_symbol = 0;
  1293. file_table = 0;
  1294. force_executable = 0;
  1295. global_indirect_count = 0;
  1296. input_desc = 0;
  1297. input_file = 0;
  1298. local_sym_count = 0;
  1299. magic = 0;
  1300. make_executable = 0;
  1301. multiple_def_count = 0;
  1302. non_L_local_sym_count = 0;
  1303. nsyms = 0;
  1304. num_hash_tab_syms = 0;
  1305. resource_text = 0;
  1306. set_element_prefixes = 0;
  1307. set_sect_size = 0;
  1308. set_sect_start = 0;
  1309. set_symbol_count = 0;
  1310. set_vector_count = 0;
  1311. specified_data_size = 0;
  1312. string_table_len = 0;
  1313. string_table_offset = 0;
  1314. strip_symbols = 0;
  1315. strtab_index = 0;
  1316. strtab_lens = 0;
  1317. strtab_size = 0;
  1318. strtab_vector = 0;
  1319. symbol_table_len = 0;
  1320. symbol_table_offset = 0;
  1321. bzero(symtab, sizeof(symtab));
  1322. text_pad = 0;
  1323. text_reloc_size = 0;
  1324. text_size = 0;
  1325. text_start = 0;
  1326. undefined_global_sym_count = 0;
  1327. warning_count = 0;
  1328. #endif
  1329.   
  1330.   /* Clear the cumulative info on the output file.  */
  1331.  
  1332.   text_size = 0;
  1333.   data_size = 0;
  1334.   bss_size = 0;
  1335.   text_reloc_size = 0;
  1336.   data_reloc_size = 0;
  1337.  
  1338.   data_pad = 0;
  1339.   text_pad = 0;
  1340.  
  1341.   /* Initialize the data about options.  */
  1342.  
  1343.   specified_data_size = 0;
  1344.   strip_symbols = STRIP_NONE;
  1345.   discard_locals = DISCARD_NONE;
  1346.   magic = OMAGIC;
  1347.   make_executable = 1;
  1348.   force_executable = 0;
  1349.   set_element_prefixes = 0;
  1350.  
  1351.   /* Initialize the cumulative counts of symbols.  */
  1352.  
  1353.   local_sym_count = 0;
  1354.   non_L_local_sym_count = 0;
  1355.   debugger_sym_count = 0;
  1356.   undefined_global_sym_count = 0;
  1357.   set_symbol_count = 0;
  1358.   set_vector_count = 0;
  1359.   global_indirect_count = 0;
  1360.   warning_count = 0;
  1361.   multiple_def_count = 0;
  1362.   common_defined_global_count = 0;
  1363.  
  1364.   file_table = &fe;
  1365.   bzero (file_table, sizeof (struct file_entry));
  1366.  
  1367.   file_table->filename = "a.out";
  1368.   file_table->local_sym_name = "a.out";
  1369.  
  1370.     {
  1371.     struct stat statbuf;
  1372.     if (fstat(desc, &statbuf)) return -1;
  1373.     resource_size = statbuf.st_size;
  1374.     }
  1375.  
  1376.     memcpy(&file_table->header, hdr, sizeof (struct exec));
  1377.  
  1378.     if (N_BADMAG(file_table->header))
  1379.         return fatal_with_file ("bad magic number in ", file_table);
  1380.  
  1381.     coresize = N_DATOFF(file_table->header) + 
  1382.             (file_table->header).a_data + 
  1383.             (file_table->header).a_bss;
  1384.  
  1385.     if (coresize > resource_size) resource_size = coresize;
  1386. /*
  1387.     kprintf("data,bss size = %d,%d\n", (file_table->header).a_data,(file_table->header).a_bss);
  1388.     kprintf("core,resource size = %d,%d\n", coresize,resource_size);
  1389. */
  1390.     resource_text = (char *)emptyblock(resource_size);
  1391.  
  1392.     read(desc, ((char *) (resource_text)) + sizeof(struct exec), resource_size);
  1393.         
  1394.   /* Create the symbols `etext', `edata' and `end'.  */
  1395.  
  1396.     symtab_init ();
  1397.             
  1398.     text_start = sizeof(struct exec) + ((long) (resource_text));
  1399.     data_start = N_DATOFF(file_table->header) + ((long) (resource_text));
  1400.  
  1401.   file_table->header_read_flag = 1;
  1402.   file_table->symbols = N_SYMOFF (file_table->header)  + (char *)(resource_text);
  1403.   file_table->string_size = *(int *)( N_STROFF (file_table->header) + (char *)(resource_text));
  1404.   file_table->strings = N_STROFF (file_table->header) + (char *)(resource_text);
  1405.   file_table->textrel = text_offset (file_table) + file_table->header.a_text + file_table->header.a_data + (char *)(resource_text);
  1406.   file_table->datarel = text_offset (file_table) + file_table->header.a_text + file_table->header.a_data + file_table->header.a_trsize + (char *)(resource_text);
  1407.   enter_file_symbols (file_table);
  1408.   file_table->text_start_address = text_size;
  1409.   text_size += file_table->header.a_text;
  1410.   file_table->data_start_address = data_size;
  1411.   data_size += file_table->header.a_data;
  1412.   file_table->bss_start_address = bss_size;
  1413.   bss_size += file_table->header.a_bss;
  1414.   text_reloc_size += file_table->header.a_trsize;
  1415.   data_reloc_size += file_table->header.a_drsize;
  1416.  
  1417.   if (digest_symbols ())
  1418.       {
  1419.       kprintf("relocatable module not linked with -d\n");
  1420.       return -1;
  1421.       }
  1422.  
  1423.   big_stack = getsym_soft("_setrlimit");
  1424.  
  1425.   /* Print error messages for any missing symbols, for any warning
  1426.      symbols, and possibly multiple definitions */
  1427.  
  1428.   bytes = text_offset (file_table) + (char *)resource_text;
  1429.  
  1430.   if (perform_relocation (bytes, file_table->text_start_address, file_table->header.a_text,
  1431.               file_table->textrel, file_table->header.a_trsize, file_table))
  1432.         return -1;
  1433.  
  1434.   bytes = text_offset (file_table) + file_table->header.a_text + (char *)resource_text;
  1435.  
  1436.   if (perform_relocation (bytes, file_table->data_start_address - file_table->header.a_text,
  1437.               file_table->header.a_data, file_table->datarel, file_table->header.a_drsize, file_table))
  1438.         return -1;
  1439.               
  1440.   bzero((void *)file_table->bss_start_address, (file_table->header).a_bss);
  1441.  
  1442.           {
  1443.           int hashval;
  1444.           long type;
  1445.           for (hashval = TABSIZE; hashval--; )
  1446.               {
  1447.               ld_symbol *bp;
  1448.               for (bp = symtab[hashval]; bp; )
  1449.                 {
  1450.                 ld_symbol *nxt = bp->link;
  1451.                 free(bp->name);
  1452.                 free(bp);
  1453.                 bp = nxt;
  1454.                 }
  1455.             }
  1456.         }
  1457.   if (make_executable)
  1458.           {
  1459.         hdr->a_entry = text_start;
  1460.         
  1461.           return resource_size;
  1462.           }
  1463.   
  1464.  return -1;
  1465. }
  1466.  
  1467.  
  1468. int
  1469. set_element_prefixed_p (name)
  1470.      char *name;
  1471. {
  1472.   struct string_list_element *p;
  1473.   int i;
  1474.  
  1475.   for (p = set_element_prefixes; p; p = p->next)
  1476.     {
  1477.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1478.     ;
  1479.  
  1480.       if (p->str[i] == '\0')
  1481.     return 1;
  1482.     }
  1483.   return 0;
  1484. }
  1485.  
  1486. /* Record an option and arrange to act on it later.
  1487.    ARG should be the following command argument,
  1488.    which may or may not be used by this option.
  1489.  
  1490.    The `l' and `A' options are ignored here since they actually
  1491.    specify input files.  */
  1492.  
  1493.  
  1494. /** Convenient functions for operating on one or all files being */
  1495.  /** loaded.  */
  1496. void print_file_name ();
  1497.  
  1498.  
  1499. /* Close the input file that is now open.  */
  1500.  
  1501. void
  1502. file_close ()
  1503. {
  1504.   input_file = 0;
  1505. }
  1506.  
  1507. /* Open the input file specified by 'entry', and return a descriptor.
  1508.    The open file is remembered; if the same file is opened twice in a row,
  1509.    a new open is not actually done.  */
  1510.  
  1511. int
  1512. file_open (entry)
  1513.      register struct file_entry *entry;
  1514. {
  1515.     return input_desc;
  1516. }
  1517.  
  1518.  
  1519. /* Medium-level input routines for rel files.  */
  1520.  
  1521. /* Read a file's header into the proper place in the file_entry.
  1522.    DESC is the descriptor on which the file is open.
  1523.    ENTRY is the file's entry.  */
  1524. /* Read the string table of file ENTRY into core.
  1525.    Assume it is already open, on descriptor DESC.
  1526.    Also record whether a GDB ld_symbol segment follows the string table.  */
  1527.  
  1528.  
  1529. /* Enter the external ld_symbol defs and refs of ENTRY in the hash table.  */
  1530.  
  1531. void
  1532. enter_file_symbols (entry)
  1533.      struct file_entry *entry;
  1534. {
  1535.   register struct nlist
  1536.     *p,
  1537.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1538.  
  1539.   for (p = entry->symbols; p < end; p++)
  1540.     {
  1541.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1542.       if (set_element_prefixes
  1543.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1544.     p->n_type += (N_SETA - N_ABS);
  1545.  
  1546.       if (SET_ELEMENT_P (p->n_type))
  1547.     {
  1548.       set_symbol_count++;
  1549.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1550.     }
  1551.       else if (p->n_type == N_WARNING)
  1552.     {
  1553.       char *name = p->n_un.n_strx + entry->strings;
  1554.  
  1555.       /* Grab the next entry.  */
  1556.       p++;
  1557.       if (p->n_type != (N_UNDF | N_EXT))
  1558.         {
  1559.           make_executable = 0;
  1560.           p--;        /* Process normally.  */
  1561.         }
  1562.       else
  1563.         {
  1564.           ld_symbol *sp;
  1565.           char *sname = p->n_un.n_strx + entry->strings;
  1566.           /* Deal with the warning ld_symbol.  */
  1567.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1568.           sp = getsym (sname);
  1569.           sp->warning = name;
  1570.           warning_count++;
  1571.         }
  1572.     }
  1573.       else if (p->n_type & N_EXT)
  1574.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1575.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1576.     {
  1577.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1578.         non_L_local_sym_count++;
  1579.       local_sym_count++;
  1580.     }
  1581.       else debugger_sym_count++;
  1582.     }
  1583.  
  1584.    /* Count one for the local ld_symbol that we generate,
  1585.       whose name is the file's name (usually) and whose address
  1586.       is the start of the file's text.  */
  1587.  
  1588.   local_sym_count++;
  1589.   non_L_local_sym_count++;
  1590. }
  1591.  
  1592. /* Enter one global ld_symbol in the hash table.
  1593.    NLIST_P points to the `struct nlist' ld_read from the file
  1594.    that describes the global ld_symbol.  NAME is the ld_symbol's name.
  1595.    ENTRY is the file entry for the file the ld_symbol comes from.
  1596.  
  1597.    The `struct nlist' is modified by placing it on a chain of
  1598.    all such structs that refer to the same global ld_symbol.
  1599.    This chain starts in the `refs' field of the ld_symbol table entry
  1600.    and is chained through the `n_name'.  */
  1601.  
  1602. void
  1603. enter_global_ref (nlist_p, name, entry)
  1604.      register struct nlist *nlist_p;
  1605.      char *name;
  1606.      struct file_entry *entry;
  1607. {
  1608.   register ld_symbol *sp = getsym (name);
  1609.   register int type = nlist_p->n_type;
  1610.   int oldref = sp->referenced;
  1611.   int olddef = sp->defined;
  1612.  
  1613.   nlist_p->n_un.n_name = (char *) sp->refs;
  1614.   sp->refs = nlist_p;
  1615.  
  1616.   sp->referenced = 1;
  1617.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1618.     {
  1619.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1620.     sp->defined = type;
  1621.  
  1622.       if (oldref && !olddef)
  1623.     /* It used to be undefined and we're defining it.  */
  1624.     undefined_global_sym_count--;
  1625.  
  1626.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1627.     {
  1628.       /* First definition and it's common.  */
  1629.       common_defined_global_count++;
  1630.       sp->max_common_size = nlist_p->n_value;
  1631.     }
  1632.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1633.     {
  1634.       /* It used to be common and we're defining it as
  1635.          something else.  */
  1636.       common_defined_global_count--;
  1637.       sp->max_common_size = 0;
  1638.     }
  1639.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1640.       && sp->max_common_size < nlist_p->n_value)
  1641.     /* It used to be common and this is a new common entry to
  1642.        which we need to pay attention.  */
  1643.     sp->max_common_size = nlist_p->n_value;
  1644.  
  1645.       /* Are we defining it as a set element?  */
  1646.       if (SET_ELEMENT_P (type)
  1647.       && (!olddef || (olddef && sp->max_common_size)))
  1648.     set_vector_count++;
  1649.       /* As an indirection?  */
  1650.       else if (type == (N_INDR | N_EXT))
  1651.     {
  1652.       /* Indirect symbols value should be modified to point
  1653.          a ld_symbol being equivalenced to. */
  1654.       nlist_p->n_value
  1655.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1656.                      + entry->strings);
  1657.       if ((ld_symbol *) nlist_p->n_value == sp)
  1658.         {
  1659.           /* Rewrite this ld_symbol as being a global text ld_symbol
  1660.          with value 0.  */
  1661.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  1662.           nlist_p->n_value = 0;
  1663.           /* Don't make the output executable.  */
  1664.           make_executable = 0;
  1665.         }
  1666.       else
  1667.         global_indirect_count++;
  1668.     }
  1669.     }
  1670.   else
  1671.     if (!oldref)
  1672.       undefined_global_sym_count++;
  1673.  
  1674.   if (sp->trace)
  1675.     {
  1676.       register char *reftype;
  1677.       switch (type & N_TYPE)
  1678.     {
  1679.     case N_UNDF:
  1680.       if (nlist_p->n_value)
  1681.         reftype = "defined as common";
  1682.       else reftype = "referenced";
  1683.       break;
  1684.  
  1685.     case N_ABS:
  1686.       reftype = "defined as absolute";
  1687.       break;
  1688.  
  1689.     case N_TEXT:
  1690.       reftype = "defined in text section";
  1691.       break;
  1692.  
  1693.     case N_DATA:
  1694.       reftype = "defined in data section";
  1695.       break;
  1696.  
  1697.     case N_BSS:
  1698.       reftype = "defined in BSS section";
  1699.       break;
  1700.  
  1701.     case N_SETT:
  1702.       reftype = "is a text set element";
  1703.       break;
  1704.  
  1705.     case N_SETD:
  1706.       reftype = "is a data set element";
  1707.       break;
  1708.  
  1709.     case N_SETB:
  1710.       reftype = "is a BSS set element";
  1711.       break;
  1712.  
  1713.     case N_SETA:
  1714.       reftype = "is an absolute set element";
  1715.       break;
  1716.  
  1717.     case N_SETV:
  1718.       reftype = "defined in data section as vector";
  1719.       break;
  1720.  
  1721.     case N_INDR:
  1722.       reftype = "defined equivalent";
  1723.       break;
  1724.  
  1725.     }
  1726.  
  1727.     }
  1728. }
  1729.  
  1730. /* This return 0 if the given file entry's ld_symbol table does *not*
  1731.    contain the nlist point entry, and it returns the files entry
  1732.    pointer (cast to unsigned long) if it does. */
  1733.  
  1734. unsigned long
  1735. contains_symbol (entry, n_ptr)
  1736.      struct file_entry *entry;
  1737.      register struct nlist *n_ptr;
  1738. {
  1739.   if (n_ptr >= entry->symbols &&
  1740.       n_ptr < (entry->symbols
  1741.            + (entry->header.a_syms / sizeof (struct nlist))))
  1742.     return (unsigned long) entry;
  1743.   return 0;
  1744. }
  1745.  
  1746.  
  1747.  
  1748.  
  1749. void consider_file_section_lengths ();
  1750. void relocate_file_addresses (register struct file_entry *entry);
  1751.  
  1752. /* Having entered all the global symbols and found the sizes of sections
  1753.    of all files to be linked, make all appropriate deductions from this data.
  1754.  
  1755.    We propagate global ld_symbol values from definitions to references.
  1756.    We compute the layout of the output file and where each input file's
  1757.    contents fit into it.  */
  1758.  
  1759. int
  1760. digest_symbols ()
  1761. {
  1762.   register int i;
  1763.   int setv_fill_count;
  1764.  
  1765.   /* Set up the set element vector */
  1766.  
  1767.     {
  1768.       /* The set sector size is the number of set elements + a word
  1769.          for each ld_symbol for the length word at the beginning of the
  1770.      vector, plus a word for each ld_symbol for a zero at the end of
  1771.      the vector (for incremental linking).  */
  1772.       set_sect_size
  1773.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  1774.       set_sect_start = data_start + data_size;
  1775.       data_size += set_sect_size;
  1776.       setv_fill_count = 0;
  1777.     }
  1778.  
  1779.   /* Compute start addresses of each file's sections and symbols.  */
  1780.  
  1781.   relocate_file_addresses ( file_table);
  1782.  
  1783.   /* Now, for each ld_symbol, verify that it is defined globally at most once.
  1784.      Put the global value into the ld_symbol entry.
  1785.      Common symbols are allocated here, in the BSS section.
  1786.      Each defined ld_symbol is given a '->defined' field
  1787.       which is the correct N_ code for its definition,
  1788.       except in the case of common symbols with -r.
  1789.      Then make all the references point at the ld_symbol entry
  1790.      instead of being chained together. */
  1791.  
  1792.   defined_global_sym_count = 0;
  1793.  
  1794.   for (i = 0; i < TABSIZE; i++)
  1795.     {
  1796.       register ld_symbol *sp;
  1797.       for (sp = symtab[i]; sp; sp = sp->link)
  1798.     {
  1799.       /* For each ld_symbol */
  1800.       register struct nlist *p, *next;
  1801.       int defs = 0, com = sp->max_common_size;
  1802.       struct nlist *first_definition;
  1803.       for (p = sp->refs; p; p = next)
  1804.         {
  1805.           register int type = p->n_type;
  1806.  
  1807.           if (SET_ELEMENT_P (type))
  1808.         {
  1809.           if (!defs++)
  1810.             {
  1811.               sp->value = set_sect_start
  1812.             + setv_fill_count++ * sizeof (unsigned long);
  1813.               sp->defined = N_SETV | N_EXT;
  1814.               first_definition = p;
  1815.             }
  1816.           else if ((sp->defined & ~N_EXT) != N_SETV)
  1817.             {
  1818.               sp->multiply_defined = 1;
  1819.               multiple_def_count++;
  1820.             }
  1821.           setv_fill_count++;
  1822.         }
  1823.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  1824.         {
  1825.           /* non-common definition */
  1826.           if (defs++ && sp->value != p->n_value)
  1827.             {
  1828.               sp->multiply_defined = 1;
  1829.               multiple_def_count++;
  1830.             }
  1831.           sp->value = p->n_value;
  1832.           sp->defined = type;
  1833.           first_definition = p;
  1834.         }
  1835.           next = (struct nlist *) p->n_un.n_name;
  1836.           p->n_un.n_name = (char *) sp;
  1837.         }
  1838.       /* Allocate as common if defined as common and not defined for real */
  1839.       if (com && !defs)
  1840.         {
  1841.         return -1;    /* this is not allowed when loading for execution ! */
  1842.         {
  1843.           int align = sizeof (int);
  1844.  
  1845.           /* Round up to nearest sizeof (int).  I don't know
  1846.              whether this is necessary or not (given that
  1847.              alignment is taken care of later), but it's
  1848.              traditional, so I'll leave it in.  Note that if
  1849.              this size alignment is ever removed, ALIGN above
  1850.              will have to be initialized to 1 instead of
  1851.              sizeof (int).  */
  1852.  
  1853.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  1854.  
  1855.           while (!(com & align))
  1856.             align <<= 1;
  1857.  
  1858.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  1859.  
  1860.           bss_size = ((((bss_size + data_size + data_start)
  1861.                   + (align - 1)) & (- align))
  1862.                   - data_size - data_start);
  1863.  
  1864.           sp->value = data_start + data_size + bss_size;
  1865.           sp->defined = N_BSS | N_EXT;
  1866.           bss_size += com;
  1867.         }
  1868.         }
  1869.       /* Set length word at front of vector and zero byte at end.
  1870.          Reverse the vector itself to put it in file order.  */
  1871.       if ((sp->defined & ~N_EXT) == N_SETV)
  1872.         {
  1873.           unsigned long length_word_index
  1874.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  1875.           unsigned long i, tmp;
  1876.  
  1877.           setv_fill_count++;
  1878.         }
  1879.       if (sp->defined)
  1880.         defined_global_sym_count++;
  1881.     }
  1882.     }
  1883.  
  1884.   if (end_symbol)        /* These are null if -r.  */
  1885.     {
  1886.       etext_symbol->value = text_size + text_start;
  1887.       edata_symbol->value = data_start + data_size;
  1888.       end_symbol->value = data_start + data_size + bss_size;
  1889.     }
  1890. return 0;
  1891. }
  1892.  
  1893. /* Determine where the sections of ENTRY go into the output file,
  1894.    whose total section sizes are already known.
  1895.    Also relocate the addresses of the file's local and debugger symbols.  */
  1896.  
  1897. void
  1898. relocate_file_addresses (entry)
  1899.      register struct file_entry *entry;
  1900. {
  1901.   entry->text_start_address += text_start;
  1902.   /* Note that `data_start' and `data_size' have not yet been
  1903.      adjusted for `data_pad'.  If they had been, we would get the wrong
  1904.      results here.  */
  1905.   entry->data_start_address += data_start;
  1906.   entry->bss_start_address += data_start + data_size;
  1907.  
  1908.   {
  1909.     register struct nlist *p;
  1910.     register struct nlist *end
  1911.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1912.  
  1913.     for (p = entry->symbols; p < end; p++)
  1914.       {
  1915.     /* If this belongs to a section, update it by the section's start address */
  1916.     register int type = p->n_type & N_TYPE;
  1917.  
  1918.     switch (type)
  1919.       {
  1920.       case N_TEXT:
  1921.       case N_SETT:
  1922.         p->n_value += entry->text_start_address;
  1923.         break;
  1924.       case N_DATA:
  1925.       case N_SETV:
  1926.       case N_SETD:
  1927.         /* A ld_symbol whose value is in the data section
  1928.            is present in the input file as if the data section
  1929.            started at an address equal to the length of the file's text.  */
  1930.         p->n_value += entry->data_start_address - entry->header.a_text;
  1931.         break;
  1932.       case N_BSS:
  1933.       case N_SETB:
  1934.         /* likewise for symbols with value in BSS.  */
  1935.         p->n_value += entry->bss_start_address
  1936.           - entry->header.a_text - entry->header.a_data;
  1937.         break;
  1938.       }
  1939.       }
  1940.   }
  1941. }
  1942.  
  1943.  
  1944.  
  1945.  
  1946. int
  1947. text_offset (entry)
  1948.      struct file_entry *entry;
  1949. {
  1950.   return entry->starting_offset + N_TXTOFF (entry->header);
  1951. }
  1952.  
  1953. /* Relocate ENTRY's text or data section contents.
  1954.    DATA is the address of the contents, in core.
  1955.    DATA_SIZE is the length of the contents.
  1956.    PC_RELOCATION is the difference between the address of the contents
  1957.      in the output file and its address in the input file.
  1958.    RELOC_INFO is the address of the relocation info, in core.
  1959.    RELOC_SIZE is its length in bytes.  */
  1960. /* This version is about to be severly hacked by Randy.  Hope it
  1961.    works afterwards. */
  1962. int
  1963. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  1964.      char *data;
  1965.      struct relocation_info *reloc_info;
  1966.      struct file_entry *entry;
  1967.      int pc_relocation;
  1968.      int data_size;
  1969.      int reloc_size;
  1970. {
  1971.   register struct relocation_info *p = reloc_info;
  1972.   struct relocation_info *end
  1973.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  1974.   int text_relocation = entry->text_start_address;
  1975.   int data_relocation = entry->data_start_address - entry->header.a_text;
  1976.   int bss_relocation
  1977.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  1978.  
  1979.   for (; p < end; p++)
  1980.     {
  1981.       register int relocation = 0;
  1982.       register int addr = RELOC_ADDRESS(p);
  1983.       register unsigned int mask = 0;
  1984.  
  1985.       if (addr >= data_size)
  1986.     return fatal_with_file ("relocation address out of range in ", entry);
  1987.  
  1988.       if (RELOC_EXTERN_P(p))
  1989.     {
  1990.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  1991.       ld_symbol *sp = ((ld_symbol *)
  1992.             (((struct nlist *)
  1993.               (((char *)entry->symbols) + symindex))
  1994.              ->n_un.n_name));
  1995.  
  1996. #ifdef N_INDR
  1997.       /* Resolve indirection */
  1998.       if ((sp->defined & ~N_EXT) == N_INDR)
  1999.         sp = (ld_symbol *) sp->value;
  2000. #endif
  2001.  
  2002.       if (symindex >= entry->header.a_syms)
  2003.         return fatal_with_file ("relocation symbolnum out of range in ", entry);
  2004.  
  2005.       /* If the ld_symbol is undefined, leave it at zero.  */
  2006.       if (! sp->defined)
  2007.         relocation = 0;
  2008.       else
  2009.         relocation = sp->value;
  2010.     }
  2011.       else switch (RELOC_TYPE(p))
  2012.     {
  2013.     case N_TEXT:
  2014.     case N_TEXT | N_EXT:
  2015.       relocation = text_relocation;
  2016.       break;
  2017.  
  2018.     case N_DATA:
  2019.     case N_DATA | N_EXT:
  2020.       /* A word that points to beginning of the the data section
  2021.          initially contains not 0 but rather the "address" of that section
  2022.          in the input file, which is the length of the file's text.  */
  2023.       relocation = data_relocation;
  2024.       break;
  2025.  
  2026.     case N_BSS:
  2027.     case N_BSS | N_EXT:
  2028.       /* Similarly, an input word pointing to the beginning of the bss
  2029.          initially contains the length of text plus data of the file.  */
  2030.       relocation = bss_relocation;
  2031.       break;
  2032.  
  2033.     case N_ABS:
  2034.     case N_ABS | N_EXT:
  2035.       /* Don't know why this code would occur, but apparently it does.  */
  2036.       break;
  2037.  
  2038.     case 0:
  2039.     /* bodge */
  2040.     break;
  2041.  
  2042.     default:
  2043.       return fatal_with_file ("nonexternal relocation code invalid in ", entry);
  2044.     }
  2045.  
  2046. #ifdef RELOC_ADD_EXTRA
  2047.       relocation += RELOC_ADD_EXTRA(p);
  2048.       if (relocatable_output)
  2049.     {
  2050.       /* Non-PC relative relocations which are absolute
  2051.          or which have become non-external now have fixed
  2052.          relocations.  Set the ADD_EXTRA of this relocation
  2053.          to be the relocation we have now determined.  */
  2054.       if (! RELOC_PCREL_P (p))
  2055.         {
  2056.           if ((int)p->r_type <= RELOC_32
  2057.           || RELOC_EXTERN_P (p) == 0)
  2058.         RELOC_ADD_EXTRA (p) = relocation;
  2059.         }
  2060.       /* External PC-relative relocations continue to move around;
  2061.          update their relocations by the amount they have moved
  2062.          so far.  */
  2063.       else if (RELOC_EXTERN_P (p))
  2064.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  2065.       continue;
  2066.     }
  2067. #endif
  2068.  
  2069.       if (RELOC_PCREL_P(p))
  2070.     relocation -= pc_relocation;
  2071.  
  2072.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  2073.  
  2074.       /* Unshifted mask for relocation */
  2075.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  2076.       mask |= mask - 1;
  2077.       relocation &= mask;
  2078.  
  2079.       /* Shift everything up to where it's going to be used */
  2080.       relocation <<= RELOC_TARGET_BITPOS(p);
  2081.       mask <<= RELOC_TARGET_BITPOS(p);
  2082.  
  2083.       switch (RELOC_TARGET_SIZE(p))
  2084.     {
  2085.     case 0:
  2086.       if (RELOC_MEMORY_SUB_P(p))
  2087.         relocation -= mask & *(char *) (data + addr);
  2088.       else if (RELOC_MEMORY_ADD_P(p))
  2089.         relocation += mask & *(char *) (data + addr);
  2090.       *(char *) (data + addr) &= ~mask;
  2091.       *(char *) (data + addr) |= relocation;
  2092.       break;
  2093.  
  2094.     case 1:
  2095.       if (RELOC_MEMORY_SUB_P(p))
  2096.         relocation -= mask & *(short *) (data + addr);
  2097.       else if (RELOC_MEMORY_ADD_P(p))
  2098.         relocation += mask & *(short *) (data + addr);
  2099.       *(short *) (data + addr) &= ~mask;
  2100.       *(short *) (data + addr) |= relocation;
  2101.       break;
  2102.  
  2103.     case 2:
  2104. #ifndef _CROSS_TARGET_ARCH
  2105.       if (RELOC_MEMORY_SUB_P(p))
  2106.         relocation -= mask & *(long *) (data + addr);
  2107.       else if (RELOC_MEMORY_ADD_P(p))
  2108.         relocation += mask & *(long *) (data + addr);
  2109.       *(long *) (data + addr) &= ~mask;
  2110.       *(long *) (data + addr) |= relocation;
  2111. #else
  2112.     /* Handle long word alignment requirements of SPARC architecture */
  2113.     /* WARNING:  This fix makes an assumption on byte ordering */
  2114.     /* Marc Ullman, Stanford University    Nov. 1 1989  */
  2115.       if (RELOC_MEMORY_SUB_P(p)) {
  2116.         relocation -= mask & 
  2117.           ((*(unsigned short *) (data + addr) << 16) |
  2118.         *(unsigned short *) (data + addr + 2));
  2119.       } else if (RELOC_MEMORY_ADD_P(p)) {
  2120.         relocation += mask &
  2121.           ((*(unsigned short *) (data + addr) << 16) |
  2122.         *(unsigned short *) (data + addr + 2));
  2123.       }
  2124.       *(unsigned short *) (data + addr)     &= (~mask >> 16);
  2125.       *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff);
  2126.       *(unsigned short *) (data + addr)     |= (relocation >> 16);
  2127.       *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff);
  2128. #endif
  2129.       break;
  2130.  
  2131.     default:
  2132.       return fatal_with_file ("Unimplemented relocation field length in ", entry);
  2133.     }
  2134.     }
  2135. return 0;
  2136. }
  2137.  
  2138.  
  2139. void write_file_syms ();
  2140. void write_string_table ();
  2141.  
  2142. /* Add the string NAME to the output file string table.
  2143.    Record it in `strtab_vector' to be output later.
  2144.    Return the index within the string table that this string will have.  */
  2145.  
  2146. int
  2147. assign_string_table_index (name)
  2148.      char *name;
  2149. {
  2150.   register int index = strtab_size;
  2151.   register int len = strlen (name) + 1;
  2152.  
  2153.   strtab_size += len;
  2154.   strtab_vector[strtab_index] = name;
  2155.   strtab_lens[strtab_index++] = len;
  2156.  
  2157.   return index;
  2158. }
  2159.  
  2160. /* Create the ld_symbol table entries for `etext', `edata' and `end'.  */
  2161.  
  2162. void
  2163. symtab_init ()
  2164.     {
  2165. #ifndef nounderscore
  2166.   edata_symbol = getsym ("_edata");
  2167.   etext_symbol = getsym ("_etext");
  2168.   end_symbol = getsym ("_end");
  2169. #else
  2170.   edata_symbol = getsym ("edata");
  2171.   etext_symbol = getsym ("etext");
  2172.   end_symbol = getsym ("end");
  2173. #endif
  2174.  
  2175.   edata_symbol->defined = N_DATA | N_EXT;
  2176.   etext_symbol->defined = N_TEXT | N_EXT;
  2177.   end_symbol->defined = N_BSS | N_EXT;
  2178.  
  2179.   edata_symbol->referenced = 1;
  2180.   etext_symbol->referenced = 1;
  2181.   end_symbol->referenced = 1;
  2182. }
  2183.  
  2184. /* Compute the hash code for ld_symbol name KEY.  */
  2185.  
  2186. int
  2187. hash_string (key)
  2188.      char *key;
  2189. {
  2190.   register char *cp;
  2191.   register int k;
  2192.  
  2193.   cp = key;
  2194.   k = 0;
  2195.   while (*cp)
  2196.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  2197.  
  2198.   return k;
  2199. }
  2200.  
  2201. /* Get the ld_symbol table entry for the global ld_symbol named KEY.
  2202.    Create one if there is none.  */
  2203.  
  2204. ld_symbol *
  2205. getsym (key)
  2206.      char *key;
  2207. {
  2208.   register int hashval;
  2209.   register ld_symbol *bp;
  2210.  
  2211.   /* Determine the proper bucket.  */
  2212.  
  2213.   hashval = hash_string (key) % TABSIZE;
  2214.  
  2215.   /* Search the bucket.  */
  2216.  
  2217.   for (bp = symtab[hashval]; bp; bp = bp->link)
  2218.     if (! strcmp (key, bp->name))
  2219.       return bp;
  2220.  
  2221.   /* Nothing was found; create a new ld_symbol table entry.  */
  2222.  
  2223.   bp = (ld_symbol *) xmalloc (sizeof (ld_symbol));
  2224.   bp->refs = 0;
  2225.   bp->name = (char *) xmalloc (strlen (key) + 1);
  2226.   strcpy (bp->name, key);
  2227.   bp->defined = 0;
  2228.   bp->referenced = 0;
  2229.   bp->trace = 0;
  2230.   bp->value = 0;
  2231.   bp->max_common_size = 0;
  2232.   bp->warning = 0;
  2233.   bp->undef_refs = 0;
  2234.   bp->multiply_defined = 0;
  2235.  
  2236.   /* Add the entry to the bucket.  */
  2237.  
  2238.   bp->link = symtab[hashval];
  2239.   symtab[hashval] = bp;
  2240.  
  2241.   ++num_hash_tab_syms;
  2242.  
  2243.   return bp;
  2244. }
  2245.  
  2246. /* Like `getsym' but return 0 if the ld_symbol is not already known.  */
  2247.  
  2248. ld_symbol *
  2249. getsym_soft (key)
  2250.      char *key;
  2251. {
  2252.   register int hashval;
  2253.   register ld_symbol *bp;
  2254.  
  2255.   /* Determine which bucket.  */
  2256.  
  2257.   hashval = hash_string (key) % TABSIZE;
  2258.  
  2259.   /* Search the bucket.  */
  2260.  
  2261.   for (bp = symtab[hashval]; bp; bp = bp->link)
  2262.     if (! strcmp (key, bp->name))
  2263.       return bp;
  2264.  
  2265.   return 0;
  2266. }
  2267.  
  2268. /* Report a fatal error.  The error message is STRING
  2269.    followed by the filename of ENTRY.  */
  2270.  
  2271. int
  2272. fatal_with_file (string, entry)
  2273.      char *string;
  2274.      struct file_entry *entry;
  2275. {
  2276.   error(string);
  2277.   return -1;
  2278. }
  2279.