home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / dld / dld.c < prev    next >
C/C++ Source or Header  |  1994-10-16  |  61KB  |  2,208 lines

  1. /* dld -- dynamic link/unlink editor for C
  2.    Copyright (C) 1990 by W. Wilson Ho.
  3.  
  4.    Version 3.2.3
  5.    The author can be reached electronically by how@cs.ucdavis.edu or
  6.    through physical mail at:
  7.  
  8.    W. Wilson Ho
  9.    Division of Computer Science
  10.    University of California at Davis
  11.    Davis, CA 95616
  12.  */
  13.  
  14. /* This program is free software; you can redistribute it and/or modify it
  15.    under the terms of the GNU General Public License as published by the
  16.    Free Software Foundation; either version 1, or (at your option) any
  17.    later version.
  18.  
  19.    This program borrows and modifies a number of functions and data
  20.    structures from the implementation of the GNU `ld' link editor.  The
  21.    original copyleft notice from the GNU `ld' is also included. */
  22.  
  23. /* Linker `ld' for GNU
  24.    Copyright (C) 1988 Free Software Foundation, Inc.
  25.  
  26.    This program is free software; you can redistribute it and/or modify
  27.    it under the terms of the GNU General Public License as published by
  28.    the Free Software Foundation; either version 1, or (at your option)
  29.    any later version.
  30.  
  31.    This program is distributed in the hope that it will be useful,
  32.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.    GNU General Public License for more details.
  35.  
  36.    You should have received a copy of the GNU General Public License
  37.    along with this program; if not, write to the Free Software
  38.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  39.  
  40. #include "defs.h"
  41.  
  42. #if 0
  43. /*
  44.  * This part is taken out after the elimination of use of alloca().
  45.  */
  46. /* 
  47.  * Alloca include.
  48.  */
  49. /* If compiled with GNU C, use the built-in alloca */
  50. #ifdef __GNUC__
  51. #define alloca __builtin_alloca
  52. #endif
  53.  
  54. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  55. #include <alloca.h>
  56. #endif
  57. #endif
  58.  
  59. /* System dependencies */
  60.  
  61. #ifdef atarist
  62. #include <basepage.h>
  63. /* _initial_stack determines the amount of memory available as stack/heap,  */
  64. /* if it's negative this is the amount available to the system, 1L means    */
  65. /* use a ratio of 1:3 (stack:system), 2L means ratio 1:1, 3L stands for 3:1 */
  66. static char __patch_str[] = "{PatchVar}stack = %ld bytes";
  67. extern long _initial_stack = - 100 * 1024L;
  68. #endif /* atarist */
  69.  
  70. /* Ordinary 4.3 bsd lacks these macros in a.out.h */
  71.     
  72. #ifndef N_TXTADDR
  73. #ifdef vax
  74. #define N_TXTADDR(x) 0
  75. #endif
  76. #ifdef sequent
  77. #define N_TXTADDR(x) (N_ADDRADJ(x))
  78. #endif
  79. #endif
  80.  
  81. #ifndef N_DATADDR
  82. #ifdef vax
  83. #define N_DATADDR(x) \
  84.     (((x).a_magic == OMAGIC) ? (N_TXTADDR(x)+(x).a_text) \
  85.      : (page_size + ((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  86. #endif
  87. #ifdef sequent
  88. #define N_DATADDR(x) \
  89.         (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  90.         : (page_size+(((x).a_text-1) & ~(page_size-1))))
  91. #endif
  92. #endif
  93.  
  94. #ifndef N_BSSADDR
  95. #ifdef vax
  96. #define N_BSSADDR(x) (N_DATADDR(x)+(x).a_data)
  97. #endif
  98. #endif
  99.  
  100. /*
  101.  * Ok.  Following are the relocation information macros.  If your
  102.  * system cannot use the default set (below), you must define all of these:
  103.  
  104.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  105.  * of structure that is stored in the relocation info section of your
  106.  * a.out files.  Often this is defined in the a.out.h for your system.
  107.  *
  108.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  109.  * <whatever> to be relocated.  *Must be an lvalue*.
  110.  *
  111.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  112.  * external symbol (1), or was it fully resolved upon entering the
  113.  * loader (0) in which case some combination of the value in memory
  114.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  115.  * what the value of the relocation actually was.  *Must be an lvalue*.
  116.  *
  117.  *   RELOC_SYMBOL (rval): For an external relocation, this is the
  118.  * index of its symbol in the symbol table.  *Must be an lvalue*.
  119.  *
  120.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  121.  * pc relative.
  122.  *
  123.  *   RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
  124.  * present in the memory location to be relocated is *subtracted*
  125.  * from the relocation value, to produce the final result.
  126.  * By default, this is always 0.
  127.  *
  128.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  129.  * an extra value to be added to the relocation value based on the
  130.  * individual relocation entry.  *Must be an lvalue if defined*.
  131.  *
  132.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  133.  * final relocation value before putting it where it belongs.
  134.  *
  135.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  136.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  137.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  138.  * do everything in terms of the bit operators below), but having this
  139.  * macro could end up producing better code on machines without fancy
  140.  * bit twiddling.  Also, it's easier to understand/code big/little
  141.  * endian distinctions with this macro.
  142.  *
  143.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  144.  * with the bits of the relocation value.  It may be assumed by the
  145.  * code that the relocation value will fit into this many bits.  This
  146.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  147.  *
  148.  *
  149.  *        Things I haven't implemented
  150.  *        ----------------------------
  151.  *
  152.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  153.  *
  154.  *    Pc relative relocation for External references.
  155.  *
  156.  *
  157.  */
  158.  
  159. #if defined(sun) && defined(sparc)
  160. /* Sparc (Sun 4) macros */
  161. #undef relocation_info
  162. #define relocation_info                    reloc_info_sparc
  163. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  164. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  165. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  166. #define RELOC_MEMORY_SUB_P(r)        0
  167. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  168. #define RELOC_PCREL_P(r)             \
  169.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  170. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  171. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  172. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  173.  
  174. /* Note that these are very dependent on the order of the enums in
  175.    enum reloc_type (in a.out.h); if they change the following must be
  176.    changed */
  177. /* Also note that the last few may be incorrect; I have no information */
  178. static int reloc_target_rightshift[] = {
  179.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  180. };
  181. static int reloc_target_size[] = {
  182.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  183. };
  184. static int reloc_target_bitsize[] = {
  185.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  186. };
  187.  
  188. #endif
  189.  
  190. #if defined(sequent)
  191. #define RELOC_ADDRESS(r)        ((r)->r_address)
  192. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  193. #define RELOC_SYMBOL(r)            ((r)->r_symbolnum)
  194. #define RELOC_MEMORY_SUB_P(r)        ((r)->r_bsr)
  195. #undef RELOC_ADD_EXTRA
  196. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  197. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  198. #endif
  199.  
  200. /* Default macros */
  201. #ifndef RELOC_ADDRESS
  202. #define RELOC_ADDRESS(r)        ((r)->r_address)
  203. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  204. #define RELOC_SYMBOL(r)            ((r)->r_symbolnum)
  205. #define RELOC_MEMORY_SUB_P(r)        0
  206. #undef RELOC_ADD_EXTRA
  207. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  208. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  209. #endif
  210.  
  211. /* Size of a page; obtained from the operating system. */
  212.  
  213. static int page_size;
  214.  
  215. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  216. symbol *_dld_symtab[TABSIZE];
  217.  
  218. /* Count the number of global symbols referenced and not defined.  */
  219. int dld_undefined_sym_count = 0;
  220.  
  221. /* internal format of relocation info entry. */
  222. struct dld_reloc_info {
  223.     /* corresponding symbol table entry. */
  224.     symbol *sp;
  225.  
  226.     /* The real relocation info entry.
  227.        This is an ugly design.  In the original relocation_info structure.
  228.        The corresponding symbol definition is located by an index to the
  229.        nlist array.  This array is not kept online, and so this index
  230.        must be replace by the address of the corresponding symbol table
  231.        kept online.  However, on most machine this index takes only 24bits,
  232.        which is not large enough (in general) to hold a pointer.  So we
  233.        need a structure definition with a sp field as shown above.  The
  234.        complete original relocation info entry is kept here.  This is not
  235.        very space economical (24 bits are wasted).  But since different
  236.        system has different format for STRUCT RELOCATION_INFO, I'd rather
  237.        let the include file <a.out.h> take care of the difference, than
  238.        using a separate definition for each system. */
  239.     struct relocation_info reloc_info;
  240. };
  241.  
  242. /* Format of __.SYMDEF:
  243.    First, a longword containing the size of the 'symdef' data that follows.
  244.    Second, zero or more 'symdef' structures.
  245.    Third, a word containing the length of symbol name strings.
  246.    Fourth, zero or more symbol name strings (each followed by a zero).  */
  247.  
  248. struct symdef {
  249.   int symbol_name_string_index;
  250.   int library_member_offset;
  251. };
  252.  
  253. /* variable for saving the environment */
  254. jmp_buf _dld_env;
  255.  
  256. /* pointer to the lastest (newest) file entry */
  257. struct file_entry *_dld_latest_entry = 0;
  258.  
  259. /* dummy file_entry to hold all "dangling" symbols. */
  260. struct file_entry *_dld_dummy_entry = 0;
  261.  
  262. /* To avoid close a file and then open the same file again, the following
  263.    two variables remember the file that is currently open.  Both are zero
  264.    if no file is open.
  265.  */
  266. static struct file_entry *input_file = 0;
  267. static int input_desc = 0;
  268.  
  269. /* global variables to return the error code to the caller */
  270. int dld_errno;
  271.  
  272. /* true if the executable flags are up-to-date */
  273. char _dld_exec_flags_valid;
  274.  
  275. /* Miscellaneous routines */
  276.  
  277. /* save the error code in dld_errno */
  278. static void
  279. fatal (errno)
  280. register int errno;
  281. {
  282.     dld_errno = errno;
  283.     longjmp (_dld_env, 1);
  284. } /* fatal */
  285.  
  286.  
  287. /* Like malloc but get fatal error if memory is exhausted.  */
  288.  
  289. int
  290. _dld_malloc (size)
  291. int size;
  292. {
  293.     register int result = malloc (size);
  294.     if (!result)
  295.     fatal (DLD_ENOMEMORY);
  296.     return result;
  297. } /* _dld_malloc */
  298.  
  299.  
  300. /* Return a newly-allocated string
  301.    whose contents concatenate the strings S1, S2, S3.  */
  302.  
  303. static char *
  304. concat (s1, s2, s3)
  305. char *s1, *s2, *s3;
  306. {
  307.     register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  308.     register char *result = (char *) _dld_malloc (len1 + len2 + len3 + 1);
  309.     
  310.     strcpy (result, s1);
  311.     strcpy (result + len1, s2);
  312.     strcpy (result + len1 + len2, s3);
  313.     result[len1 + len2 + len3] = 0;
  314.     
  315.     return result;
  316. } /* concat */
  317.  
  318.  
  319. /* Add a new entry to the file chain if it is not already there.
  320.    Return 0 if no actual insertion is needed, otherwise, return 1. */
  321. static int
  322. insert_entry (head, entry)
  323. struct file_chain **head;
  324. register struct file_entry *entry;
  325. {
  326.     register struct file_chain *fc = *head;
  327.     
  328.     while (fc)  {
  329.         if (fc->entry == entry)
  330.             return 0;
  331.         else fc = fc->next;
  332.     }
  333.     
  334.     fc = (struct file_chain *) _dld_malloc (sizeof (struct file_chain));
  335.  
  336.     fc->next = *head;
  337.     fc->entry = entry;
  338.     *head = fc;
  339.     return 1;
  340. } /* insert_entry */
  341.  
  342.  
  343. /* Close the input file that is now open.  */
  344.  
  345. static void
  346. file_close ()
  347. {
  348.   if (input_desc) close (input_desc);
  349.   input_desc = 0;
  350.   input_file = 0;
  351. } /* file_close */
  352.  
  353. /* Open the input file specified by 'entry', and return a descriptor.
  354.    The open file is remembered; if the same file is opened twice in a row,
  355.    a new open is not actually done.  */
  356.  
  357. static int
  358. file_open (entry)
  359. register struct file_entry *entry;
  360. {
  361.     register int desc;
  362.     
  363.     if (entry == 0) fatal (DLD_ENOFILE);
  364.     
  365.     if (entry->superfile)
  366.     return file_open (entry->superfile);
  367.     
  368.     if (entry == input_file)
  369.     return input_desc;
  370.     
  371.     if (input_file) file_close ();
  372.     
  373.     desc = open (entry->filename, O_RDONLY, 0);
  374.     
  375.     if (desc > 0) {
  376.     input_file = entry;
  377.     input_desc = desc;
  378.     return desc;
  379.     }
  380.     
  381.     fatal (DLD_ENOFILE);
  382. } /* file_open */
  383.  
  384. /* Medium-level input routines for rel files.  */
  385.  
  386. /* Read a file's header into the proper place in the file_entry.
  387.    DESC is the descriptor on which the file is open.
  388.    ENTRY is the file's entry.  */
  389.  
  390. static void
  391. read_header (desc, entry)
  392. int desc;
  393. register struct file_entry *entry;
  394. {
  395.     register int len;
  396.     struct exec *loc = &entry->header;
  397.     
  398.     lseek (desc, entry->starting_offset, 0);
  399.     len = read (desc, loc, sizeof (struct exec));
  400.     if (len != sizeof (struct exec))
  401.     fatal (DLD_EBADHEADER);
  402.     if (N_BADMAG (*loc))
  403.     fatal (DLD_EBADMAGIC);
  404.     
  405.     entry->header_read_flag = 1;
  406. } /* read_header */
  407.  
  408. #ifdef atarist
  409. #include <st-out.h>
  410.  
  411. static void
  412. st_read_header (desc, entry)
  413. int desc;
  414. register struct file_entry *entry;
  415. {
  416.     register int len;
  417.     struct exec *loc = &entry->header;
  418.     struct aexec file_header;
  419.     
  420.     lseek (desc, 0L, 0);
  421.     len = read (desc, &file_header, sizeof (struct aexec));
  422.     if (len != sizeof (struct aexec))
  423.     fatal (DLD_EBADHEADER);
  424.     if (A_BADMAG (file_header))
  425.     fatal (DLD_EBADMAGIC);
  426.     if (!file_header.a_AZero2)
  427.     fatal (DLD_ENOSTRINGS);
  428.  
  429.     bzero (loc, sizeof (struct exec));
  430.     loc->a_magic = NMAGIC;    /* flag different header size */
  431.     loc->a_text = file_header.a_text;
  432.     loc->a_data = file_header.a_data;
  433.     loc->a_bss = file_header.a_bss;
  434.     loc->a_syms = file_header.a_syms - file_header.a_AZero2;
  435.     entry->header_read_flag = 1;
  436. } /* st_read_header */
  437. #endif /* atarist */
  438.  
  439. /* Read the symbols of file ENTRY into core.
  440.    Assume it is already open, on descriptor DESC.
  441.    Also read the length of the string table, which follows the symbol table,
  442.    but don't read the contents of the string table.  */
  443.  
  444. static void
  445. read_entry_symbols (desc, entry)
  446. struct file_entry *entry;
  447. int desc;
  448. {
  449.     int str_size;
  450.     
  451.     if (!entry->header_read_flag)
  452.     read_header (desc, entry);
  453.     
  454.     if (entry->header.a_syms <= 0)
  455.     fatal (DLD_ENOSYMBOLS);
  456.     
  457.     entry->symbols = (struct nlist *) _dld_malloc (entry->header.a_syms);
  458.     
  459.     lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  460.     if (entry->header.a_syms !=
  461.     read (desc, entry->symbols, entry->header.a_syms)) {
  462.     free (entry->symbols);
  463.     entry->symbols = 0;
  464.     fatal (DLD_ENOSYMBOLS);
  465.     }
  466.     
  467.     lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  468.     if (sizeof str_size != read (desc, &str_size, sizeof str_size)) {
  469.     free (entry->symbols);
  470.     entry->symbols = 0;
  471.     fatal (DLD_ENOSTRINGS);
  472.     }
  473.     
  474.     entry->string_size = str_size;
  475. } /* read_entry_symboles */
  476.  
  477.  
  478. /* Read the string table of file ENTRY into core.
  479.    Assume it is already open, on descriptor DESC. */
  480.  
  481. static void
  482. read_entry_strings (desc, entry)
  483. struct file_entry *entry;
  484. int desc;
  485. {
  486.     if (!entry->header_read_flag)
  487.     read_header (desc, entry);
  488.     
  489.     entry->strings = (char *) _dld_malloc (entry->string_size);
  490.  
  491.     lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  492.     if (entry->string_size != read (desc, entry->strings,
  493.                     entry->string_size)) {
  494.     free (entry->strings);
  495.     entry->strings = 0;
  496.     fatal (DLD_ENOSTRINGS);
  497.     }
  498.     
  499. } /* read_entry_strings */
  500.  
  501.  
  502. /* Verify the validity of the relocation information.
  503.    DATA_SIZE is the length of the contents.
  504.    RELOC_INFO is the address of the relocation info, in core.
  505.    RELOC_SIZE is its length in bytes.
  506.    If everything is ok, return the number of external relocation entry.
  507.    Otherwise, return -1. */
  508. int
  509. reloc_info_ok (data_size, reloc_info, reloc_size, sym_size)
  510. register int data_size;
  511. register struct relocation_info *reloc_info;
  512. register int reloc_size;
  513. register int sym_size;
  514. {
  515.     register struct relocation_info *p = reloc_info;
  516.     register struct relocation_info *end = p +
  517.     reloc_size / sizeof (struct relocation_info);
  518.     register int extern_count = 0;  /* number of external relocation */
  519.     
  520.     for (; p < end; p++) {
  521.     register int symbolnum = RELOC_SYMBOL(p);
  522.     
  523.     if (RELOC_ADDRESS(p) >= data_size)
  524.         return -1;
  525.     if (RELOC_EXTERN_P(p)) {
  526.         extern_count++;
  527.         if (symbolnum * sizeof (struct nlist) >= sym_size)
  528.         return -1;
  529.     } else if (symbolnum != N_TEXT && symbolnum != (N_TEXT | N_EXT) &&
  530.            symbolnum != N_DATA && symbolnum != (N_DATA | N_EXT) &&
  531.            symbolnum != N_BSS  && symbolnum != (N_BSS | N_EXT))
  532.         return -1;
  533.  
  534.     if (RELOC_TARGET_SIZE(p) > 2) return -1;
  535.     }
  536.     return extern_count++;
  537.     
  538. } /* reloc_info_ok */
  539.  
  540. /* symbol table management */
  541.  
  542. /* Compute the hash code for symbol name KEY.  */
  543.  
  544. static int
  545. hash_string (key)
  546. char *key;
  547. {
  548.     register char *cp;
  549.     register int k;
  550.     
  551.     cp = key;
  552.     k = 0;
  553.     while (*cp)
  554.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  555.     
  556.     return k;
  557. } /* hash_string */
  558.  
  559.  
  560. /* Get the symbol table entry for the global symbol named KEY.
  561.    Create one if there is none.  */
  562.  
  563. symbol *
  564. _dld_getsym (key)
  565. char *key;
  566. {
  567.     register int hashval;
  568.     register symbol *bp;
  569.     
  570.     /* Determine the proper bucket.  */
  571.     
  572.     hashval = hash_string (key) % TABSIZE;
  573.     
  574.     /* Search the bucket.  */
  575.     
  576.     for (bp = _dld_symtab[hashval]; bp; bp = bp->link)
  577.     if (! strcmp (key, bp->name))
  578.         return bp;
  579.     
  580.     /* Nothing was found; create a new symbol table entry.  */
  581.     
  582.     bp = (symbol *) _dld_malloc (sizeof (symbol));
  583.     bzero (bp, sizeof (symbol));
  584.     bp->name = (char *) _dld_malloc (strlen (key) + 1);
  585.     strcpy (bp->name, key);
  586.     
  587.     /* Add the entry to the bucket.  */
  588.     
  589.     bp->link = _dld_symtab[hashval];
  590.     _dld_symtab[hashval] = bp;
  591.     
  592.     return bp;
  593. } /* _dld_getsym */
  594.  
  595.  
  596. /* Like `_dld_getsym' but return 0 if the symbol is not already known.  */
  597.  
  598. symbol *
  599. _dld_getsym_soft (key)
  600. char *key;
  601. {
  602.     register int hashval;
  603.     register symbol *bp;
  604.     
  605.     /* Determine which bucket.  */
  606.     
  607.     hashval = hash_string (key) % TABSIZE;
  608.     
  609.     /* Search the bucket.  */
  610.     
  611.     for (bp = _dld_symtab[hashval]; bp; bp = bp->link)
  612.     if (! strcmp (key, bp->name))
  613.         return bp;
  614.     
  615.     return 0;
  616. } /* _dld_getsym_soft */
  617.  
  618. /* Enter one global symbol in the hash table.
  619.    NLIST_P points to the `struct nlist' read from the file
  620.    that describes the global symbol.  NAME is the symbol's name.
  621.    ENTRY is the file entry for the file the symbol comes from.
  622.  
  623.    The `struct nlist' is modified by making n_name point back to the
  624.    corresponding symbols.
  625.  
  626.    For the common definition, the symbol is considered defined by the
  627.    first entry that defines it.  The problem here is that uninitialized
  628.    global variables are all treated as common definitions.  Strictly
  629.    speacking, in a program there should be exactly one definition
  630.    (initialized or uninitialized) of any global variable and all other
  631.    files that reference that variable should declare it with the 'extern'
  632.    keyword.  However, most compiler allows the omission of the 'extern'
  633.    keyword, and let the linker map all these definitions to the same
  634.    location.  In other words, multiple definitions of the same symbol is
  635.    allowed.  Now, when one of the files that defines such symbol is to be
  636.    unlinked, should this symbol become undefined?  Or should it remain in
  637.    core?  The decision made is to treat the symbol defined by the first
  638.    file that defines it.  All subsequence definitions of the same symbol
  639.    will be treated as extern references.  However, if any of the
  640.    following definition defines the symbol as initialized global, it will
  641.    be considered multiple definition and be treated as an error.
  642.  
  643. */
  644. void
  645. _dld_enter_global_ref (entry, nlist_p, name)
  646. struct file_entry *entry;
  647. register struct nlist *nlist_p;
  648. char *name;
  649. {
  650.     register symbol *sp = _dld_getsym (name);
  651.     register int type = nlist_p->n_type;
  652.     register int common = (type == (N_UNDF | N_EXT) && nlist_p->n_value);
  653.     int oldref = sp->referenced;
  654.     int olddef = sp->defined;
  655.  
  656.     nlist_p->n_un.n_name = (char *) sp;
  657.     
  658.     sp->referenced = 1;
  659.  
  660.     /* common definition */
  661.     if (common && !olddef)
  662. #ifndef linux
  663.     type = N_COMM | N_EXT;
  664. #else
  665.     type = N_TYPE | N_EXT;
  666. #endif
  667.  
  668.     if (type != (N_UNDF | N_EXT)) {
  669.     /* definition of a symbol */
  670.     
  671.     if (olddef)
  672.         fatal (DLD_EMULTDEFS);
  673.     else {
  674.         sp->defined = type;
  675.         sp->value = common ? (long) _dld_malloc (nlist_p->n_value) :
  676.         nlist_p->n_value; 
  677.         if (common) bzero (sp->value, nlist_p->n_value);
  678.         sp->defined_by = entry;
  679.     }
  680.  
  681.     if (oldref && !olddef) {
  682.         register struct file_chain *p = sp->referenced_by;
  683.         register struct file_chain *prev = 0;
  684.         
  685.         dld_undefined_sym_count--;
  686.         
  687.         /* We do not set up references link to and from the dummy
  688.            entry, nor do we touch the ref_count of it.
  689.            Furthermore, if a symbol referenced by the dummy entry is
  690.            defined, we remove all connections between the dummy entry
  691.            and this symbol. */
  692.         /* If the dummy entry references anything other than a library
  693.            member, this reference is removed. */
  694.         while (p) {
  695.         if (entry != _dld_dummy_entry) {
  696.             if (p->entry == _dld_dummy_entry && entry->superfile)
  697.             entry->ref_count++;
  698.             else if (insert_entry (&(p->entry->refs), entry) &&
  699.                  insert_entry (&(entry->refs_by), p->entry) &&
  700.                  p->entry != entry)
  701.             entry->ref_count++;
  702.         }
  703.  
  704.         if (p->entry == _dld_dummy_entry) {
  705.  
  706.             /* we force this to be an entry directly linked-in by
  707.                the user (top-level module). */
  708.             entry->already_unlink = 0;
  709.             del_link_list_elt (sp->referenced_by, prev, p, next);
  710.             continue;
  711.         } else p->entry->undefined_symbol_count--;
  712.         
  713.         prev = p;
  714.         p = p->next;
  715.         }
  716.     }
  717.     } else {
  718.     /* this is just a reference */
  719.     if (sp->defined_by && sp->defined_by != _dld_dummy_entry)
  720.         if (entry != _dld_dummy_entry) {
  721.         if (insert_entry (&(entry->refs), sp->defined_by) &&
  722.             entry != sp->defined_by)
  723.             sp->defined_by->ref_count++;
  724.         } else if (sp->defined_by->superfile) {
  725.         sp->defined_by->ref_count++;
  726.         sp->defined_by->already_unlink = 0;
  727.         }
  728.     
  729.     if (entry != _dld_dummy_entry) {
  730.         if (insert_entry (&(sp->referenced_by), entry) && !sp->defined)
  731.         entry->undefined_symbol_count++;
  732.     } else {
  733.         if (!sp->defined)
  734.         insert_entry (&(sp->referenced_by), entry);
  735.     }
  736.     
  737.     if (!oldref) {
  738.         dld_undefined_sym_count++;
  739.         sp->defined = 0;
  740.         sp->value = 0;
  741.     }
  742.     }
  743. }
  744.  
  745.  
  746. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  747.  
  748. static void
  749. enter_file_symbols (entry)
  750. struct file_entry *entry;
  751. {
  752.     register struct nlist *p,
  753.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  754.     
  755.     for (p = entry->symbols; p < end; p++)
  756. #ifdef linux
  757.     if (p->n_type & N_EXT) {
  758.         if (p->n_type == (N_INDR | N_EXT)) {
  759.           symbol *sp;
  760.  
  761.           /* Enter an indirect symbol and the following undefined symbol
  762.          as it's reference (see ".../linux/a.out.h") */
  763.  
  764.           /* First enter the referenced symbol */
  765.           _dld_enter_global_ref (entry, p+1, (p+1)->n_un.n_strx + entry->strings);
  766.           /* Get the address of the corresponding 'struct glosym' */
  767.           sp = (symbol *)((p+1)->n_un.n_name);
  768.           /* Now enter the indirect symbol (alias) */
  769.           _dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
  770.           /* Make 'indirect' point to the referenced symbol */
  771.           ((symbol *)(p->n_un.n_name))->indirect = sp;
  772.           p++;
  773.         }
  774.         else
  775.           _dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
  776.     }
  777. #else
  778.     if (p->n_type & N_EXT)
  779.         _dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
  780. #endif
  781. }
  782.  
  783. /* remove all symbols that are no longer needed */
  784. static void
  785. cleanup_symtab ()
  786. {
  787.     register int i;
  788.  
  789.     for (i = 0; i < TABSIZE; i++) {
  790.     register symbol *sp = _dld_symtab[i];
  791.     register symbol *prev_sp = 0;
  792.  
  793.     while (sp) {
  794.         register struct file_chain *p = sp->referenced_by;
  795.         register struct file_chain *prev = 0;
  796.         register int obsolete = (sp->defined_by &&
  797.                      sp->defined_by->ref_count == 0);
  798.         
  799.         while (p)
  800.         if (p->entry->ref_count == 0) {
  801.             del_link_list_elt (sp->referenced_by, prev, p, next);
  802.         } else {
  803.             if (obsolete)
  804.             p->entry->undefined_symbol_count++;
  805.  
  806.             prev = p;
  807.             p = p->next;
  808.         }
  809.  
  810.         if (obsolete) {
  811. #ifndef linux
  812.         if (sp->defined == (N_COMM | N_EXT) && sp->value)
  813. #else
  814.         if (sp->defined == (N_TYPE | N_EXT) && sp->value)
  815. #endif
  816.             free (sp->value);
  817.         if (sp->referenced_by == 0) {
  818.             free (sp->name);
  819.             del_link_list_elt (_dld_symtab[i], prev_sp, sp, link);
  820.             continue;
  821.         } else {
  822.             dld_undefined_sym_count++;
  823.             sp->defined = 0;
  824.             sp->defined_by = 0;
  825.             sp->value = 0;
  826.         }
  827.         }
  828.  
  829.         if (sp->defined == 0 && sp->referenced_by == 0) {
  830.         dld_undefined_sym_count--;
  831.         free (sp->name);
  832. #ifndef linux
  833.         if (sp->defined == (N_COMM | N_EXT) && sp->value)
  834. #else
  835.         if (sp->defined == (N_TYPE | N_EXT) && sp->value)
  836. #endif
  837.             free (sp->value);
  838.         del_link_list_elt (_dld_symtab[i], prev_sp, sp, link);
  839.         continue;
  840.         }
  841.  
  842.         prev_sp = sp;
  843.         sp = sp->link;
  844.     }
  845.     }
  846. } /* cleanup_symtab */
  847.  
  848. /* Searching libraries */
  849.  
  850. static struct file_entry *decode_library_subfile ();
  851. static void linear_library (), symdef_library ();
  852. static int subfile_wanted_p ();
  853. static void read_text_and_data ();
  854.  
  855. /* Search the library ENTRY, already open on descriptor DESC.
  856.    This means deciding which library members to load,
  857.    making a chain of `struct file_entry' for those members,
  858.    and entering their global symbols in the hash table.  */
  859.  
  860. static void
  861. search_library (desc, entry)
  862. int desc;
  863. struct file_entry *entry;
  864. {
  865.     int member_length;
  866.     register char *name;
  867.     register struct file_entry *subentry;
  868.     
  869.     if (!dld_undefined_sym_count) return;
  870.     
  871.     /* Examine its first member, which starts SARMAG bytes in.  */
  872.     subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  873.     if (!subentry) return;
  874.     
  875.     name = subentry->filename;
  876.     free (subentry);
  877.     
  878.     /* Search via __.SYMDEF if that exists, else linearly.  */
  879.     
  880. #ifdef linux
  881.     if (!strcmp (name, "__.SYMDEF/"))
  882. #else
  883.     if (!strcmp (name, "__.SYMDEF"))
  884. #endif
  885.     symdef_library (desc, entry, member_length);
  886.     else
  887.     linear_library (desc, entry);
  888.     
  889.     free (name);
  890. } /* search_library */
  891.  
  892. /* Construct and return a file_entry for a library member.
  893.    The library's file_entry is library_entry, and the library is open on DESC.
  894.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  895.    We store the length of the member into *LENGTH_LOC.  */
  896.  
  897. static struct file_entry *
  898. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  899. int desc;
  900. struct file_entry *library_entry;
  901. int subfile_offset;
  902. int *length_loc;
  903. {
  904.     int bytes_read;
  905.     register int namelen;
  906.     int member_length;
  907.     register char *name;
  908.     struct ar_hdr hdr1;
  909.     register struct file_entry *subentry;
  910.     
  911.     lseek (desc, subfile_offset, 0);
  912.     
  913.     bytes_read = read (desc, &hdr1, sizeof hdr1);
  914.     if (!bytes_read)
  915.     return 0;        /* end of archive */
  916.     
  917.     if (sizeof hdr1 != bytes_read)
  918.     fatal (DLD_EBADLIBRARY);
  919.     
  920.     if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  921.     fatal (DLD_EBADLIBRARY);
  922.     
  923.     subentry = (struct file_entry *) _dld_malloc (sizeof (struct file_entry));
  924.     bzero (subentry, sizeof (struct file_entry));
  925.     
  926.     for (namelen = 0;
  927.      namelen < sizeof hdr1.ar_name
  928.      && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ';
  929.      namelen++);
  930.     
  931.     name = (char *) _dld_malloc (namelen+1);
  932.     strncpy (name, hdr1.ar_name, namelen);
  933.     name[namelen] = 0;
  934.     
  935.     subentry->filename = name;
  936.     subentry->local_sym_name = name;
  937.     subentry->starting_offset = subfile_offset + sizeof hdr1;
  938.     subentry->superfile = library_entry;
  939.     subentry->total_size = member_length;
  940.     
  941.     (*length_loc) = member_length;
  942.     
  943.     return subentry;
  944. } /* decode_library_subfile */
  945.  
  946.  
  947. /* Search a library that has a __.SYMDEF member.
  948.    DESC is a descriptor on which the library is open.
  949.      The file pointer is assumed to point at the __.SYMDEF data.
  950.    ENTRY is the library's file_entry.
  951.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  952.  
  953. static void
  954. symdef_library (desc, entry, member_length)
  955. int desc;
  956. struct file_entry *entry;
  957. int member_length;
  958. {
  959.     int *symdef_data = (int *) _dld_malloc (member_length);
  960.     register struct symdef *symdef_base;
  961.     char *sym_name_base;
  962.     int number_of_symdefs;
  963.     int length_of_strings;
  964.     int not_finished;
  965.     int bytes_read;
  966.     register int i;
  967.     struct file_entry *prev = 0;
  968.     int prev_offset = 0;
  969.     
  970.     bytes_read = read (desc, symdef_data, member_length);
  971.     if (bytes_read != member_length) {
  972.     free (symdef_data);
  973.     fatal (DLD_EBADLIBRARY);
  974.     }
  975.  
  976.     /* Ugly kludge here:  save the address for symdef_data in
  977.        entry->strings so that if fatal() is called, this memory block will
  978.        be freed by clean_up().  If no error occurs, it is freed before this
  979.        function returns.  I use entry->strings just for convenience and
  980.        don't want to add an extra entry in struct file_entry.  
  981.        Originally storage for symdef_data was
  982.        allocated by alloca(), but I want to avoid the use of that. */
  983.  
  984.     entry->strings = (char *) symdef_data;
  985.     
  986.     number_of_symdefs = *symdef_data / sizeof (struct symdef);
  987.     if (number_of_symdefs < 0 ||
  988.     number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) >=
  989.     member_length)
  990.     fatal (DLD_EBADLIBRARY);
  991.     
  992.     symdef_base = (struct symdef *) (symdef_data + 1);
  993.     length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  994.     
  995.     if (length_of_strings < 0
  996.     || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  997.     + 2 * sizeof (int) != member_length)
  998.     fatal (DLD_EBADLIBRARY);
  999.     
  1000.     sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  1001.     
  1002.     /* Check all the string indexes for validity.  */
  1003.     
  1004.     for (i = 0; i < number_of_symdefs; i++) {
  1005.     register int index = symdef_base[i].symbol_name_string_index;
  1006.     if (index < 0 || index >= length_of_strings
  1007.         || (index && *(sym_name_base + index - 1)))
  1008.         fatal (DLD_EBADLIBRARY);
  1009.     }
  1010.     
  1011.     /* Search the symdef data for members to load.
  1012.        Do this until one whole pass finds nothing to load.  */
  1013.     
  1014.     not_finished = 1;
  1015.     while (not_finished) {
  1016.     not_finished = 0;
  1017.         
  1018.     /* Scan all the symbols mentioned in the symdef for ones that we need.
  1019.        Load the library members that contain such symbols.  */
  1020.         
  1021.     for (i = 0; i < number_of_symdefs && dld_undefined_sym_count; i++)
  1022.         if (symdef_base[i].symbol_name_string_index >= 0) {
  1023.         register symbol *sp;
  1024.             
  1025.         sp = _dld_getsym_soft (sym_name_base +
  1026.                        symdef_base[i].symbol_name_string_index);
  1027.             
  1028.         /* If we find a symbol that appears to be needed,
  1029.            think carefully about the archive member that
  1030.            the symbol is in.  */
  1031.             
  1032.         if (sp && sp->referenced && !sp->defined) {
  1033.             int junk;
  1034.             register int j;
  1035.             register int offset = symdef_base[i].library_member_offset;
  1036.             struct file_entry *subentry;
  1037.                 
  1038.             /* Don't think carefully about any archive member
  1039.                more than once in a given pass.  */
  1040.             
  1041.             if (prev_offset == offset)
  1042.             continue;
  1043.             prev_offset = offset;
  1044.             
  1045.             /* Read the symbol table of the archive member.  */
  1046.             
  1047.             subentry = decode_library_subfile (desc, entry, offset, &junk);
  1048.             if (prev)
  1049.             prev->chain = subentry;
  1050.             else entry->subfiles = subentry;
  1051.                 
  1052.             read_entry_symbols (desc, subentry);
  1053.             read_entry_strings (desc, subentry);
  1054.                 
  1055.             /* Now scan the symbol table and decide whether to load.  */
  1056.             if (!subfile_wanted_p (subentry)) {
  1057.             if (prev)
  1058.                 prev->chain = 0;
  1059.             else entry->subfiles = 0;
  1060.             free (subentry->filename);
  1061.             free (subentry->symbols);
  1062.             free (subentry->strings);
  1063.             free (subentry);
  1064.             } else {
  1065.             /* This member is needed; load it.
  1066.                Since we are loading something on this pass,
  1067.                we must make another pass through the symdef data.  */
  1068.             
  1069.             not_finished = 1;
  1070.                     
  1071.             /* all library members are considered unlinked
  1072.                already so that they will be garbage collected
  1073.                whenever no other modules reference them. */
  1074.             subentry->already_unlink = 1;
  1075.  
  1076.             enter_file_symbols (subentry);
  1077.             free (subentry->strings);
  1078.             subentry->strings = 0;
  1079.                     
  1080.             read_text_and_data (desc, subentry);
  1081.             
  1082.             prev = subentry;
  1083.                     
  1084.             /* Clear out this member's symbols from the symdef data
  1085.                so that following passes won't waste time on them.  */
  1086.                     
  1087.             for (j = 0; j < number_of_symdefs; j++) {
  1088.                 if (symdef_base[j].library_member_offset == offset)
  1089.                 symdef_base[j].symbol_name_string_index = -1;
  1090.             }
  1091.             }
  1092.         }
  1093.         }
  1094.     }
  1095.  
  1096.     /* refer to "Ugly kludge" above. */
  1097.     free (entry->strings);
  1098.     entry->strings = 0;
  1099. } /* symdef_library */ 
  1100.  
  1101.  
  1102. /* Search a library that has no __.SYMDEF.
  1103.    ENTRY is the library's file_entry.
  1104.    DESC is the descriptor it is open on.  */
  1105.  
  1106. static void
  1107. linear_library (desc, entry)
  1108. int desc;
  1109. struct file_entry *entry;
  1110. {
  1111.     register struct file_entry *prev = 0;
  1112.     register int this_subfile_offset = SARMAG;
  1113.     
  1114.     while (dld_undefined_sym_count) {
  1115.     int member_length;
  1116.     register struct file_entry *subentry;
  1117.         
  1118.     subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
  1119.         
  1120.     if (!subentry) return;
  1121.         
  1122.     read_entry_symbols (desc, subentry);
  1123.     read_entry_strings (desc, subentry);
  1124.         
  1125.     if (!subfile_wanted_p (subentry)) {
  1126.         free (subentry->filename);
  1127.         free (subentry->symbols);
  1128.         free (subentry->strings);
  1129.         free (subentry);
  1130.     } else {
  1131.         subentry->already_unlink = 1;
  1132.         enter_file_symbols (subentry);
  1133.         free (subentry->strings);
  1134.         subentry->strings = 0;
  1135.             
  1136.         read_text_and_data (desc, subentry);
  1137.         
  1138.         if (prev)
  1139.         prev->chain = subentry;
  1140.         else entry->subfiles = subentry;
  1141.         prev = subentry;
  1142.     }
  1143.         
  1144.     this_subfile_offset += member_length + sizeof (struct ar_hdr);
  1145.     if (this_subfile_offset & 1) this_subfile_offset++;
  1146.     }
  1147. } /* linear_library */
  1148.  
  1149.  
  1150. /* ENTRY is an entry for a library member.
  1151.    Its symbols have been read into core, but not entered.
  1152.    Return nonzero if we ought to load this member.  */
  1153.  
  1154. static int
  1155. subfile_wanted_p (entry)
  1156. struct file_entry *entry;
  1157. {
  1158.     register struct nlist *p;
  1159.     register struct nlist *end
  1160.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1161.     
  1162.     for (p = entry->symbols; p < end; p++) {
  1163.     register int type = p->n_type;
  1164.         
  1165.     if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value)) {
  1166.         register char *name = p->n_un.n_strx + entry->strings;
  1167.         register symbol *sp = _dld_getsym_soft (name);
  1168.             
  1169.         /* If this symbol has not been hashed, we can't be looking
  1170.            for it. */
  1171.             
  1172.         if (!sp) continue;
  1173.             
  1174.         /* We are looking for definition of external symbols or common
  1175.            blocks that have not been defined but have been referenced */
  1176.         
  1177.         if (sp->referenced && !sp->defined &&
  1178.         (type != (N_UNDF | N_EXT) || p->n_value))
  1179.         return 1;
  1180.     }
  1181.     }
  1182.     
  1183.     return 0;
  1184. } /* subfile_wanted_p */
  1185.  
  1186. /* Relocate the addresses of the file's symbols.  */
  1187. static void
  1188. relocate_symbol_address (entry)
  1189. register struct file_entry *entry;
  1190. {
  1191.     register struct nlist *p;
  1192.     register struct nlist *end
  1193.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1194.     register int text_relocation, data_relocation, bss_relocation;
  1195.  
  1196.     text_relocation = entry->text_start_address;
  1197.     data_relocation = entry->data_start_address - entry->header.a_text;
  1198.     bss_relocation = entry->bss_start_address - entry->header.a_text -
  1199.     entry->header.a_data;
  1200.     
  1201.     for (p = entry->symbols; p < end; p++) {
  1202.     /* If this belongs to a section,
  1203.        update it by the section's start address */
  1204.         
  1205.     register int type = p->n_type;
  1206.     register symbol *sp;
  1207.     
  1208.     if ((type & N_EXT) == 0) continue;
  1209.     
  1210.     sp = (symbol *) p->n_un.n_name;
  1211.     if (type == (N_TEXT | N_EXT))
  1212.         sp->value += text_relocation;
  1213.     else if (type == (N_DATA | N_EXT))
  1214.         /* A symbol whose value is in the data section
  1215.            is present in the input file as if the data section
  1216.            started at an address equal to the length of the
  1217.            file's text.  */
  1218.         sp->value += data_relocation;
  1219.     else if (type == (N_BSS | N_EXT))
  1220.         /* likewise for symbols with value in BSS.  */
  1221.         sp->value += bss_relocation;
  1222.     }
  1223. } /* relocate_symbol_address */
  1224.  
  1225.  
  1226. /* Actually performs the relocation of local symbols.
  1227.    Do it once and for all for each entry.
  1228.    Then keep only those with external references online.
  1229.    This function is copied almost directly from perform_relocation (). */
  1230. static void
  1231. do_local_relocation (data, pc_relocation, reloc_info, dld_reloc_p, reloc_size,
  1232.              entry)
  1233. char *data;
  1234. struct relocation_info *reloc_info;
  1235. struct dld_reloc_info *dld_reloc_p;
  1236. struct file_entry *entry;
  1237. int pc_relocation;
  1238. int reloc_size;
  1239. {
  1240.     register struct relocation_info *p = reloc_info;
  1241.  
  1242.     struct relocation_info *end
  1243.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  1244.  
  1245.     int text_relocation = entry->text_start_address;
  1246.     int data_relocation = entry->data_start_address - entry->header.a_text;
  1247.     int bss_relocation
  1248.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  1249.     
  1250.     for (; p < end; p++) {
  1251.     register int relocation = 0;
  1252.     register int addr = RELOC_ADDRESS(p);
  1253.     register int symbolnum = RELOC_SYMBOL(p);
  1254.  
  1255. #if defined(sun) && defined(sparc)
  1256.     register unsigned int mask = 0;
  1257. #else
  1258.     register int length = RELOC_TARGET_SIZE(p);
  1259. #endif    
  1260.         
  1261.     if (RELOC_EXTERN_P(p)) {
  1262.         register int symindex = symbolnum * sizeof (struct nlist);
  1263.  
  1264.         dld_reloc_p->sp = ((symbol *)
  1265.                    (((struct nlist *)
  1266.                  (((char *)entry->symbols) + symindex))
  1267.                 ->n_un.n_name));
  1268.         bcopy (p, &(dld_reloc_p->reloc_info),
  1269.            sizeof (struct relocation_info));
  1270.         dld_reloc_p++;
  1271.         continue;
  1272.     } else switch (symbolnum) {
  1273.         case N_TEXT:
  1274.         case N_TEXT | N_EXT:
  1275.             relocation = text_relocation;
  1276.         break;
  1277.             
  1278.         case N_DATA:
  1279.         case N_DATA | N_EXT:
  1280.         /* A word that points to beginning of the the data section
  1281.            initially contains not 0 but rather the "address" of
  1282.            that section in the input file, which is the length of
  1283.            the file's text.  */
  1284.         relocation = data_relocation;
  1285.         break;
  1286.             
  1287.         case N_BSS:
  1288.         case N_BSS | N_EXT:
  1289.         /* Similarly, an input word pointing to the beginning of
  1290.            the bss initially contains the length of text plus data
  1291.            of the file.  */
  1292.         relocation = bss_relocation;
  1293.         break;
  1294.             
  1295.         case N_ABS:
  1296.         case N_ABS | N_EXT:
  1297.         /* just in case */
  1298.         break;
  1299.         
  1300.         default:
  1301.         break;
  1302.     }
  1303.  
  1304.     if (RELOC_PCREL_P(p))
  1305.         relocation -= pc_relocation;
  1306.  
  1307. #if defined(sun) && defined(sparc)
  1308.  
  1309.     relocation += RELOC_ADD_EXTRA(p);
  1310.  
  1311.     relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  1312.  
  1313.     /* Unshifted mask for relocation */
  1314.     mask = 1 << (RELOC_TARGET_BITSIZE(p) - 1);
  1315.     mask |= mask - 1;
  1316.     relocation &= mask;
  1317.  
  1318.     switch (RELOC_TARGET_SIZE(p)) {
  1319.         case 0:
  1320.             *(char *) (data + addr) &= ~mask;
  1321.         *(char *) (data + addr) |= relocation;
  1322.         break;
  1323.             
  1324.         case 1:
  1325.             *(short *) (data + addr) &= ~mask;
  1326.         *(short *) (data + addr) |= relocation;
  1327.         break;
  1328.             
  1329.         case 2:
  1330.             *(long *) (data + addr) &= ~mask;
  1331.         *(long *) (data + addr) |= relocation;
  1332.         break;
  1333.  
  1334.         default: break;
  1335.     }
  1336.  
  1337. #else    
  1338.     
  1339.     switch (length) {
  1340.         case 0: *(char *) (data + addr) += relocation;
  1341.             break;
  1342.             
  1343.         case 1: *(short *) (data + addr) += relocation;
  1344.             break;
  1345.             
  1346.         case 2: *(int *) (data + addr) += relocation;
  1347.             break;
  1348.  
  1349.         default: break;
  1350.     }
  1351. #endif
  1352.     }
  1353. } /* do_local_relocation */
  1354.  
  1355.  
  1356. /* Read the relocation information of file ENTRY into core.
  1357.    Assume it is already open, on descriptor DESC.
  1358.    Then relocate all the local (non-external) symbols.  Save only the
  1359.    relocation info for the external symbol references.
  1360.    Update entry->header.a_{trsize,drsize} to reflect the new relocation
  1361.    table size (in bytes).
  1362. */
  1363. static void
  1364. relocate_local_refs (desc, entry)
  1365. int desc;
  1366. struct file_entry *entry;
  1367. {
  1368.     int text_offset;
  1369.  
  1370.     /* number of relocation info that describes an external references. */
  1371.     int tr_entry_count = 0, dr_entry_count = 0;    
  1372.                      
  1373.     struct relocation_info *reloc_buf;
  1374.  
  1375.     if (!entry->header_read_flag)
  1376.     read_header (desc, entry);
  1377.  
  1378.     text_offset = entry->starting_offset + N_TXTOFF (entry->header);
  1379.  
  1380.     /* For the text segment */
  1381.     
  1382.     if (entry->header.a_trsize) {
  1383.     reloc_buf = (struct relocation_info *)
  1384.         _dld_malloc (entry->header.a_trsize);
  1385.  
  1386.     lseek (desc, text_offset + entry->header.a_text +
  1387.            entry->header.a_data, 0);
  1388.     if (entry->header.a_trsize !=
  1389.         read (desc, reloc_buf, entry->header.a_trsize)) {
  1390.         free (reloc_buf);
  1391.         fatal (DLD_ENOTXTRELOC);
  1392.     }
  1393.     if ((tr_entry_count =
  1394.          reloc_info_ok (entry->header.a_text, reloc_buf,
  1395.                 entry->header.a_trsize, entry->header.a_syms)
  1396.          ) == -1) {
  1397.         free (reloc_buf);
  1398.         fatal (DLD_EBADRELOC);
  1399.     } else {
  1400.         entry->text_reloc = (struct dld_reloc_info *)
  1401.         _dld_malloc (tr_entry_count * sizeof (struct dld_reloc_info));
  1402.         do_local_relocation (entry->text_start_address,
  1403.                  entry->text_start_address,
  1404.                  reloc_buf, entry->text_reloc,
  1405.                  entry->header.a_trsize, entry);
  1406.         free (reloc_buf);
  1407.     }
  1408.     }
  1409.  
  1410.     /* For the data segment */
  1411.     
  1412.     if (entry->header.a_drsize) {
  1413.     reloc_buf = (struct relocation_info *)
  1414.         _dld_malloc (entry->header.a_drsize);
  1415.  
  1416.     lseek (desc, text_offset + entry->header.a_text +
  1417.            entry->header.a_data + entry->header.a_trsize, 0);
  1418.     if (entry->header.a_drsize !=
  1419.         read (desc, reloc_buf, entry->header.a_drsize)) {
  1420.         free (reloc_buf);
  1421.         fatal (DLD_ENODATRELOC);
  1422.     }
  1423.     if ((dr_entry_count =
  1424.          reloc_info_ok (entry->header.a_data, reloc_buf,
  1425.                 entry->header.a_drsize, entry->header.a_syms)
  1426.          ) == -1) {
  1427.         free (reloc_buf);
  1428.         fatal (DLD_EBADRELOC);
  1429.     } else {
  1430.         entry->data_reloc = (struct dld_reloc_info *)
  1431.         _dld_malloc (dr_entry_count * sizeof (struct dld_reloc_info));
  1432.         do_local_relocation (entry->data_start_address,
  1433.                  entry->data_start_address -
  1434.                  entry->header.a_data, 
  1435.                  reloc_buf, entry->data_reloc,
  1436.                  entry->header.a_drsize, entry);
  1437.         free (reloc_buf);
  1438.     }
  1439.     }
  1440.  
  1441.     entry->header.a_trsize = tr_entry_count * sizeof (struct dld_reloc_info);
  1442.     entry->header.a_drsize = dr_entry_count * sizeof (struct dld_reloc_info);
  1443.  
  1444.     /* Free the nlist array. */
  1445.     if (entry->symbols) {
  1446.     free (entry->symbols);
  1447.     entry->symbols = 0;
  1448.     }
  1449.     
  1450. } /* relocate_local_refs */
  1451.  
  1452.  
  1453. /* Relocate ENTRY's text or data section contents.
  1454.    DATA is the address of the contents, in core.
  1455.    DATA_SIZE is the length of the contents.
  1456.    PC_RELOCATION is the difference between the address of the contents
  1457.      in the output file and its address in the input file.
  1458.    RELOC_INFO is the address of the relocation info, in core.
  1459.    RELOC_SIZE is its length in bytes.
  1460.    REVERSE is true when an 'un-relocation' is to be done.
  1461. */   
  1462.  
  1463. static void
  1464. perform_relocation (data, pc_relocation, reloc_info, reloc_size, reverse)
  1465. char *data;
  1466. struct dld_reloc_info *reloc_info;
  1467. int pc_relocation;
  1468. int reloc_size;
  1469. int reverse;
  1470. {
  1471.     register struct dld_reloc_info *p = reloc_info;
  1472.  
  1473.     struct dld_reloc_info *end
  1474.     = reloc_info + reloc_size / sizeof (struct dld_reloc_info);
  1475.     
  1476. #if defined(sun) && defined(sparc)
  1477.     if (reverse) return;
  1478. #endif
  1479.     
  1480.     for (; p < end; p++) {
  1481. #ifdef linux
  1482.     register int relocation;
  1483.     register symbol *sp = p->sp;
  1484. #else
  1485.       register int relocation = p->sp->value;
  1486. #endif
  1487.     register struct relocation_info *r = &(p->reloc_info);
  1488.     register int addr = RELOC_ADDRESS(r);
  1489.     
  1490. #if defined(sun) && defined(sparc)
  1491.     register unsigned int mask = 0;
  1492. #else
  1493.     register int length = RELOC_TARGET_SIZE(r);
  1494. #endif    
  1495.         
  1496. #ifdef linux
  1497.     while (sp->indirect) sp = sp->indirect;
  1498.     relocation = sp->value;
  1499. #endif
  1500.  
  1501.     if (RELOC_PCREL_P(r))
  1502.         relocation -= pc_relocation;
  1503.  
  1504. #if defined(sun) && defined(sparc)
  1505.  
  1506.     relocation += RELOC_ADD_EXTRA(r);
  1507.  
  1508.     relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
  1509.  
  1510.     /* Unshifted mask for relocation */
  1511.     mask = 1 << (RELOC_TARGET_BITSIZE(r) - 1);
  1512.     mask |= mask - 1;
  1513.     relocation &= mask;
  1514.  
  1515.     switch (RELOC_TARGET_SIZE(r)) {
  1516.         case 0:
  1517.             *(char *) (data + addr) &= ~mask;
  1518.         *(char *) (data + addr) |= relocation;
  1519.         break;
  1520.             
  1521.         case 1:
  1522.             *(short *) (data + addr) &= ~mask;
  1523.         *(short *) (data + addr) |= relocation;
  1524.         break;
  1525.             
  1526.         case 2:
  1527.             *(long *) (data + addr) &= ~mask;
  1528.         *(long *) (data + addr) |= relocation;
  1529.         break;
  1530.  
  1531.         default: break;
  1532.     }
  1533.  
  1534. #else    
  1535.     
  1536.     if (reverse) relocation = - relocation;
  1537.         
  1538.     switch (length) {
  1539.       case 0:
  1540.         if (RELOC_MEMORY_SUB_P(r))
  1541.         *(char *) (data + addr) = relocation - *(char *) (data + addr);
  1542.         else *(char *) (data + addr) += relocation;
  1543.         break;
  1544.             
  1545.       case 1:
  1546.         if (RELOC_MEMORY_SUB_P(r))
  1547.         *(short *) (data + addr) =
  1548.             relocation - *(short *) (data + addr);
  1549.         else *(char *) (data + addr) += relocation;
  1550.         break;
  1551.             
  1552.       case 2: 
  1553.         if (RELOC_MEMORY_SUB_P(r))
  1554.         *(int *) (data + addr) = relocation - *(int *) (data + addr);
  1555.         else *(int *) (data + addr) += relocation;
  1556.         break;
  1557.       default: break;
  1558.     }
  1559. #endif
  1560.     }
  1561. } /* perform_relocation */
  1562.  
  1563. /* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
  1564. #ifdef atarist
  1565. #define ABSOLUTE_FILENAME_P(fname)    ((fname[0] == '/') || \
  1566.     (fname[0] && (fname[1] == ':')))
  1567. #else
  1568. #define ABSOLUTE_FILENAME_P(fname)    (fname[0] == '/')
  1569. #endif /* atarist */
  1570.  
  1571. /* given a file name, create an appropriate file_entry for it */
  1572. static struct file_entry *
  1573. make_entry (filename)
  1574. char *filename;
  1575. {
  1576.     register struct file_entry *entry =
  1577.     (struct file_entry *) _dld_malloc (sizeof (struct file_entry));
  1578.  
  1579.     bzero (entry, sizeof (struct file_entry));
  1580.     entry->filename = entry->local_sym_name =
  1581.     (char *) _dld_malloc (strlen (filename) + 1);
  1582.     strcpy (entry->local_sym_name, filename);
  1583.     
  1584.     if (!ABSOLUTE_FILENAME_P(filename)) {
  1585.     char name[MAXPATHLEN];
  1586.     entry->filename = concat (getwd(name), "/", filename);
  1587.     }
  1588.     entry->chain = _dld_latest_entry;
  1589.     entry->ref_count = 1;
  1590.     return entry;
  1591. } /* make_entry */
  1592.  
  1593.  
  1594. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1595.    If it is a library, search it and load the appropriate members
  1596.    (which means calling this function recursively on those members).  */
  1597.  
  1598. static void
  1599. read_file_symbols (desc, entry, load_text)
  1600. register int desc;
  1601. register struct file_entry *entry;
  1602. int load_text;                /* used only by dld_init */
  1603. {
  1604.     register int len;
  1605.     int magicnum;
  1606.     
  1607.     len = read (desc, &magicnum, sizeof magicnum);
  1608.     if (len != sizeof magicnum)
  1609.     fatal (DLD_EBADHEADER);
  1610.     
  1611. #ifdef atarist
  1612.     if (!load_text || !N_BADMAG (*((struct exec *)&magicnum))) {
  1613. #else
  1614.     if (!N_BADMAG (*((struct exec *)&magicnum))) {
  1615. #endif
  1616.     read_entry_symbols (desc, entry);
  1617.     read_entry_strings (desc, entry);
  1618.     enter_file_symbols (entry);
  1619.     free (entry->strings);
  1620.     entry->strings = 0;
  1621.     if (load_text) read_text_and_data (desc, entry);
  1622.     } else {
  1623.     char armag[SARMAG];
  1624.     
  1625.     lseek (desc, 0, 0);
  1626.     if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1627.         fatal (DLD_EBADOBJECT);
  1628.     entry->library_flag = 1;
  1629.     search_library (desc, entry);
  1630.     }
  1631. } /* read_file_symbols */
  1632.  
  1633.  
  1634. /* Allocate memory for all text, data and bss segments and read them in
  1635.    from the file. */
  1636. static void
  1637. read_text_and_data (desc, entry)
  1638. int desc;
  1639. register struct file_entry *entry;
  1640. {
  1641.     register size = entry->header.a_text + entry->header.a_data +
  1642.     entry->header.a_bss;
  1643.  
  1644.     entry->text_start_address = _dld_malloc (size);
  1645.     if (size - entry->header.a_text > 0) {
  1646.     entry->data_start_address = entry->text_start_address +
  1647.         entry->header.a_text;
  1648.     entry->bss_start_address = entry->data_start_address +
  1649.         entry->header.a_data;
  1650.     } else entry->data_start_address = entry->bss_start_address = 0;
  1651.     
  1652.  
  1653.     /* Read text and data sections into core.
  1654.        Note that the bss segment does not actually take up space in the
  1655.        object file, so its size must be subtracted from SIZE */
  1656.     
  1657.     lseek (desc, entry->starting_offset + N_TXTOFF(entry->header), 0);
  1658.     size -= entry->header.a_bss;
  1659.     if (size != read (desc, entry->text_start_address, size)) {
  1660.     free (entry->text_start_address);
  1661.     entry->text_start_address = entry->data_start_address =
  1662.         entry->bss_start_address = 0;
  1663.     fatal (DLD_ENODATA);
  1664.     }
  1665.  
  1666.     /* zero the bss segment */
  1667.     if (entry->header.a_bss)
  1668.     bzero ((void *) entry->bss_start_address, entry->header.a_bss);
  1669. } /* read_text_and_data */
  1670.     
  1671.  
  1672. /* Allocate memory for the text and data segments and relocate all local
  1673.    symbols */
  1674. static void
  1675. relocate_entry_symbols (desc, entry)
  1676. int desc;
  1677. register struct file_entry *entry;
  1678. {
  1679.     /* Compute start addresses of each sections and symbols.  */
  1680.     
  1681.     if (entry->library_flag) {
  1682.     register struct file_entry *subentry = entry->subfiles;
  1683.     for (; subentry; subentry = subentry->chain) {
  1684.         relocate_symbol_address (subentry);
  1685.         relocate_local_refs (desc, subentry);
  1686.     }
  1687.     } else {
  1688.     relocate_symbol_address (entry);
  1689.     relocate_local_refs (desc, entry);
  1690.     }
  1691. } /* relocate_entry_symbols */
  1692.     
  1693.  
  1694. /*  Find all modules have all external references defined but not resolved. */
  1695. void
  1696. _dld_patch_all_files (entry)
  1697. register struct file_entry *entry;
  1698. {
  1699.     while (entry) {
  1700.     if (entry->library_flag)
  1701.         _dld_patch_all_files (entry->subfiles);
  1702.     else if (!entry->all_symbols_resolved_flag &&
  1703.          entry->undefined_symbol_count == 0) {
  1704.         /* entry whose global references have just been resolved */
  1705.         perform_relocation (entry->text_start_address,
  1706.                 entry->text_start_address,
  1707.                 entry->text_reloc,
  1708.                 entry->header.a_trsize, 0);
  1709.         perform_relocation (entry->data_start_address,
  1710.                 entry->data_start_address -
  1711.                 entry->header.a_data,
  1712.                 entry->data_reloc,
  1713.                 entry->header.a_drsize, 0);
  1714.         entry->all_symbols_resolved_flag = 1;
  1715.     } else if (entry->all_symbols_resolved_flag &&
  1716.            entry->undefined_symbol_count != 0) {
  1717.         /* entry that has some of its global references being
  1718.            "un-defined" */
  1719.         perform_relocation (entry->text_start_address,
  1720.                 entry->text_start_address,
  1721.                 entry->text_reloc,
  1722.                 entry->header.a_trsize, 1);
  1723.         perform_relocation (entry->data_start_address,
  1724.                 entry->data_start_address -
  1725.                 entry->header.a_data,
  1726.                 entry->data_reloc,
  1727.                 entry->header.a_drsize, 1);
  1728.         entry->all_symbols_resolved_flag = 0;
  1729.     }
  1730.  
  1731.     entry = entry->chain;
  1732.     }
  1733. } /* _dld_patch_all_files */
  1734.  
  1735.  
  1736. /*
  1737.  * reset the executable_flag of the given entry, and then recursively
  1738.  * propagate this to all modules that reference symbols in this entry.
  1739.  */
  1740. static void
  1741. invalidate (entry)
  1742. struct file_entry *entry;
  1743. {
  1744.     register struct file_chain *p;
  1745.     
  1746.     if (entry == 0)
  1747.     return;
  1748.     
  1749.     entry->executable_flag = 0;
  1750.  
  1751.     for (p = entry->refs_by; p; p = p->next)
  1752.     if (p->entry->executable_flag)
  1753.         invalidate (p->entry);
  1754. } /* invalidate */
  1755.  
  1756. /*
  1757.  * For all modules loaded, determine which of them can be safely executed.
  1758.  * For those that can, set the flag executable_flag.
  1759.  */
  1760. static void
  1761. find_all_executable_modules ()
  1762. {
  1763.     register struct file_entry *p;
  1764.  
  1765.     /* set all executable flags */
  1766.     for (p = _dld_latest_entry; p ; p = p->chain) {
  1767.     if (p->library_flag) {
  1768.         register struct file_entry *q = p->subfiles;
  1769.         while (q) {
  1770.         q->executable_flag = q->all_symbols_resolved_flag;
  1771.         q = q->chain;
  1772.         }
  1773.     }
  1774.     p->executable_flag = p->all_symbols_resolved_flag;
  1775.     }
  1776.     
  1777.     /* invalidate those modules that are not (yet) executable. */
  1778.     for (p = _dld_latest_entry; p; p = p->chain) {
  1779.     if (p->library_flag) {
  1780.         register struct file_entry *q = p->subfiles;
  1781.         while (q) {
  1782.         if (!q->all_symbols_resolved_flag && q->refs_by)
  1783.             invalidate (q);
  1784.         q = q->chain;
  1785.         }
  1786.     } else if (!p->all_symbols_resolved_flag && p->refs_by)
  1787.         invalidate (p);
  1788.     }
  1789.     _dld_exec_flags_valid = 1;
  1790. } /* find_all_executable_modules */
  1791.  
  1792.  
  1793. /* remove all reference pointers *TO* ENTRY */
  1794. static void 
  1795. remove_cross_references (head_of_chain, entry)
  1796. struct file_entry *head_of_chain, *entry;
  1797. {
  1798.     register struct file_entry *ep = head_of_chain;
  1799.     
  1800.     while (ep) {
  1801.     register struct file_chain *p = ep->refs;
  1802.     
  1803.     if (ep->library_flag)
  1804.         remove_cross_references (ep->subfiles, entry);
  1805.     else {
  1806.         register struct file_chain *prev = 0;
  1807.         
  1808.         while (p) {
  1809.         if (p->entry == entry || p->entry->superfile == entry) {
  1810.             del_link_list_elt (ep->refs, prev, p, next);
  1811.         } else {
  1812.             prev = p;
  1813.             p = p->next;
  1814.         }
  1815.         }
  1816.     }
  1817.     ep = ep->chain;
  1818.     }
  1819. } /* remove_cross_references */
  1820.  
  1821.  
  1822. /* remove all cross reference pointers related to an obsolete file entry.
  1823.    It is assumed that the ref_count of this entry is zero.
  1824.    Also clear up all other entries that become obsolete when this entry is
  1825.    removed. */
  1826. static void
  1827. cleanup_obsolete_entries (entry)
  1828. struct file_entry *entry;
  1829. {
  1830.     register struct file_chain *p;
  1831.  
  1832.     if (entry->ref_count != 0) return;
  1833.  
  1834.     if (entry->library_flag) {
  1835.     register struct file_entry *subentry = entry->subfiles;
  1836.  
  1837.     for (; subentry; subentry = subentry->chain)
  1838.         cleanup_obsolete_entries (subentry);
  1839.     }
  1840.  
  1841.     p = entry->refs;
  1842.     entry->refs = 0;            /* to prevent loop; might be redundant*/
  1843.  
  1844.     while (p) {
  1845.     register struct file_chain *next = p->next;
  1846.     
  1847.     if (p->entry->ref_count)    /* ref_count may already be set to zero */
  1848.         (p->entry->ref_count)--;
  1849.     if (p->entry->ref_count == 0)
  1850.         cleanup_obsolete_entries (p->entry);
  1851.     free (p);
  1852.     p = next;
  1853.     }
  1854. } /* cleanup_obsolete_entries */
  1855.  
  1856.  
  1857. /* remove all memory blocks assigned for ENTRY,
  1858.    return the pointers to the next entry in chain */
  1859. static struct file_entry *
  1860. kill_entry (entry)
  1861. register struct file_entry *entry;
  1862. {
  1863.     register struct file_chain *p;
  1864.  
  1865.     if (entry->library_flag) {
  1866.     register struct file_entry *subentry = entry->subfiles;
  1867.     while (subentry)
  1868.         subentry = kill_entry (subentry);
  1869.     }
  1870.     
  1871.     p = entry->refs;
  1872.     while (p) {
  1873.     register struct file_chain *next_chain = p->next;
  1874.     free (p);
  1875.     p = next_chain;
  1876.     }
  1877.  
  1878.     p = entry->refs_by;
  1879.     while (p) {
  1880.     register struct file_chain *next_chain = p->next;
  1881.     free (p);
  1882.     p = next_chain;
  1883.     }
  1884.  
  1885.     if (entry->local_sym_name) free (entry->local_sym_name);
  1886.  
  1887.     if (entry->filename != entry->local_sym_name && entry->filename)
  1888.     free (entry->filename);
  1889.  
  1890.     if (entry->symbols) free (entry->symbols);
  1891.  
  1892.     if (entry->strings) free (entry->strings);
  1893.     
  1894.     if (entry->text_reloc) free (entry->text_reloc);
  1895.  
  1896.     if (entry->data_reloc) free (entry->data_reloc);
  1897.  
  1898.     if (entry->text_start_address) free (entry->text_start_address);
  1899.  
  1900.     {
  1901.     register struct file_entry *next = entry->chain;
  1902.     free (entry);
  1903.     return (next);
  1904.     }
  1905. } /* kill_entry */
  1906.  
  1907.  
  1908. /* clean all data structures so that they return to the original states
  1909.    after last call to dld */
  1910. static void
  1911. clean_up ()
  1912. {
  1913.  
  1914.     if (!_dld_latest_entry)
  1915.     return;
  1916.  
  1917.     _dld_latest_entry->ref_count = 0;
  1918.     if (_dld_latest_entry->library_flag) {
  1919.     register struct file_entry *subentry = _dld_latest_entry->subfiles;
  1920.     for (; subentry; subentry = subentry->chain)
  1921.         subentry->ref_count = 0;
  1922.     }
  1923.     
  1924.     remove_cross_references (_dld_latest_entry, _dld_latest_entry);
  1925.  
  1926.     cleanup_obsolete_entries (_dld_latest_entry);
  1927.  
  1928.     cleanup_symtab ();
  1929.     
  1930.     _dld_latest_entry = kill_entry (_dld_latest_entry);
  1931. } /* clean_up */
  1932.  
  1933.  
  1934. /* Actually perform the unlink operation.
  1935.    Search through the list of file entries, unlink those whose ref_count is
  1936.    zero. */
  1937. static struct file_entry *
  1938. do_unlink (entry)
  1939. struct file_entry *entry;
  1940. {
  1941.     register struct file_entry *p = entry;
  1942.     register struct file_entry *prev = 0;
  1943.     
  1944.     if (p == 0) return p;
  1945.  
  1946.     while (p) {
  1947.     if (p->library_flag) {
  1948.         p->subfiles = do_unlink (p->subfiles);
  1949.  
  1950.         /* if all subentries are gone, remove itself */
  1951.         if (p->subfiles == 0)
  1952.         p->ref_count = 0;
  1953.     }
  1954.  
  1955.     if (p->ref_count == 0) {
  1956.         register struct file_entry *next;
  1957.         
  1958.         next = kill_entry (p);
  1959.     
  1960.         if (prev == 0) {
  1961.         entry = next;
  1962.         p = entry;
  1963.         } else {
  1964.         prev->chain = next;
  1965.         p = prev->chain;
  1966.         }
  1967.     } else {
  1968.         prev = p;
  1969.         p = p->chain;
  1970.     }
  1971.     }
  1972.     _dld_exec_flags_valid = 0;
  1973.     return entry;
  1974. } /* do_unlink */
  1975.  
  1976. int
  1977. dld_init (myname)
  1978. char *myname;
  1979. {
  1980.     int desc;
  1981.     
  1982.     page_size = getpagesize ();
  1983.     bzero (_dld_symtab, TABSIZE * sizeof(symbol *));
  1984.     _dld_latest_entry = 0;
  1985.     _dld_dummy_entry = 0;
  1986.     dld_undefined_sym_count = 0;
  1987.     _dld_exec_flags_valid = 0;
  1988.     
  1989.     if (myname == 0) {
  1990.     dld_errno = DLD_ENOFILE;
  1991.     return dld_errno;
  1992.     }
  1993.     
  1994.     if (setjmp (_dld_env)) {
  1995.     clean_up ();
  1996.     file_close ();
  1997.     return dld_errno;
  1998.     }
  1999.     _dld_latest_entry = make_entry (myname);
  2000.     desc = file_open (_dld_latest_entry);
  2001. #ifdef atarist
  2002.     st_read_header (desc, _dld_latest_entry);
  2003. #endif /* atarist */
  2004.     read_file_symbols (desc, _dld_latest_entry, 0);
  2005.  
  2006. #ifdef atarist
  2007.     /* do a consistency check of data in entry against data in _base */
  2008.     if ((_dld_latest_entry->header.a_text != _base->p_tlen) ||
  2009.     (_dld_latest_entry->header.a_data != _base->p_dlen) ||
  2010.     (_dld_latest_entry->header.a_bss != _base->p_blen)) {
  2011.     if (_dld_latest_entry->symbols) {
  2012.         free (_dld_latest_entry->symbols);
  2013.         _dld_latest_entry->symbols = 0;
  2014.     }
  2015.     file_close ();
  2016.     dld_errno = DLD_EBADHEADER;
  2017.     return dld_errno;
  2018.     }
  2019.     _dld_latest_entry->text_start_address = (int) _base->p_tbase;
  2020.     _dld_latest_entry->data_start_address = (int) _base->p_dbase;
  2021.     _dld_latest_entry->bss_start_address = (int) _base->p_bbase;
  2022.     relocate_symbol_address (_dld_latest_entry);
  2023. #endif /* atarist */
  2024.  
  2025.     if (_dld_latest_entry->symbols) {
  2026.     free (_dld_latest_entry->symbols);
  2027.     _dld_latest_entry->symbols = 0;
  2028.     }
  2029.     
  2030.     file_close ();
  2031.  
  2032.     _dld_latest_entry->undefined_symbol_count = 0;
  2033.     _dld_latest_entry->all_symbols_resolved_flag = 1;
  2034.     
  2035.     return 0;
  2036. } /* dld_init */
  2037.  
  2038. int
  2039. dld_link (object_file)
  2040. char *object_file;
  2041. {
  2042.     register int desc;
  2043.     struct file_entry *old_latest_entry = _dld_latest_entry;
  2044.  
  2045.     if (setjmp (_dld_env)) {
  2046.     if (old_latest_entry != _dld_latest_entry)
  2047.         clean_up ();
  2048.     file_close ();
  2049.     return dld_errno;
  2050.     }
  2051.  
  2052.     dld_errno = 0;
  2053.     file_close ();            /* file might be opened in the last
  2054.                        call */
  2055.     
  2056.     if (object_file == 0) {
  2057.     dld_errno = DLD_ENOFILE;
  2058.     return dld_errno;
  2059.     }
  2060.     
  2061.     _dld_latest_entry = make_entry (object_file);
  2062.  
  2063.     desc = file_open (_dld_latest_entry);
  2064.     
  2065.     read_file_symbols (desc, _dld_latest_entry, 1);
  2066.  
  2067.     if (_dld_latest_entry->library_flag && _dld_latest_entry->subfiles == 0) {
  2068.     free (_dld_latest_entry->local_sym_name);
  2069.     if (_dld_latest_entry->filename != _dld_latest_entry->local_sym_name)
  2070.         free (_dld_latest_entry->filename);
  2071.  
  2072.     _dld_latest_entry->filename = NULL;
  2073.     _dld_latest_entry->local_sym_name = NULL;
  2074.     
  2075.     _dld_latest_entry = kill_entry (_dld_latest_entry);
  2076.     file_close ();
  2077.     return 0;
  2078.     }
  2079.     
  2080.     relocate_entry_symbols (desc, _dld_latest_entry);
  2081.     
  2082.     file_close ();
  2083.     
  2084.     _dld_patch_all_files (_dld_latest_entry);
  2085.  
  2086.     _dld_exec_flags_valid = 0;
  2087.     return 0;
  2088. } /* dld_link */
  2089.  
  2090.  
  2091. /* return the location of the given symbol without prepending a '_'. */
  2092. unsigned long
  2093. dld_get_bare_symbol (name)
  2094. char *name;
  2095. {
  2096.     register symbol *sp;
  2097.     
  2098.     if (name == 0)
  2099.     return 0;
  2100.  
  2101.     sp = _dld_getsym_soft (name);
  2102.  
  2103. #ifdef linux
  2104.     if (sp) {
  2105.       while (sp->indirect) sp = sp->indirect;
  2106.       if (sp->defined)
  2107.       return sp->value;
  2108.     }
  2109. #else
  2110.     if (sp)
  2111.     if (sp->defined)
  2112.         return sp->value;
  2113. #endif
  2114.       return 0;
  2115. } /* dld_get_bare_symbol */
  2116.  
  2117.     
  2118. /* given a file_entry, unlink that (and all its decendents).  Modules still
  2119.    referenced by the remainings will not be unlinked.
  2120.    ENTRY is assumed to be a valid pointer to a file_entry structure.
  2121.    if FORCE is true, remove this entry event regardless if it is still
  2122.    referenced by others */
  2123. void
  2124. _dld_unlink_entry (entry, force)
  2125. struct file_entry *entry;
  2126. int force;
  2127. {
  2128.     if (force) {
  2129.     entry->ref_count = 0;
  2130.     if (entry->library_flag) {
  2131.         register struct file_entry *subentry = entry->subfiles;
  2132.         for (; subentry; subentry = subentry->chain)
  2133.         subentry->ref_count = 0;
  2134.     }
  2135.     } else {
  2136.     if (entry->library_flag || entry->already_unlink)
  2137.         return;
  2138.     entry->ref_count--;
  2139.     entry->already_unlink = 1;
  2140.     }
  2141.  
  2142.     if (entry->ref_count == 0) {
  2143.     remove_cross_references (_dld_latest_entry, entry);
  2144.  
  2145.     cleanup_obsolete_entries (entry);
  2146.  
  2147.     cleanup_symtab ();
  2148.  
  2149.     _dld_latest_entry = do_unlink (_dld_latest_entry);
  2150.     }
  2151.  
  2152.     _dld_patch_all_files (_dld_latest_entry);
  2153.     _dld_exec_flags_valid = 0;
  2154. } /* _dld_unlink_entry */
  2155.  
  2156.  
  2157. /*
  2158.  * return true if the named function can be safely exeucted.
  2159.  */
  2160. int
  2161. dld_function_executable_p (name)
  2162. char name[];
  2163. {
  2164.     register symbol *sp;
  2165.     register char *p;
  2166.  
  2167.     if (name == 0)
  2168.     return 0;
  2169.  
  2170.     if (setjmp (_dld_env))
  2171.     return 0;
  2172.  
  2173.     /* prepend an '_' to name, as required by the C convention */
  2174.     p = (char *) _dld_malloc (strlen(name) + 2);
  2175.     *p = '_';
  2176.     strcpy (p+1, name);
  2177.     sp = _dld_getsym_soft (p);
  2178.     free (p);
  2179.  
  2180. #ifdef linux
  2181.     if (sp && (sp->defined == (N_EXT | N_TEXT) ||
  2182.            sp->defined == (N_EXT | N_INDR))) {
  2183. #else
  2184.     if (sp && sp->defined == (N_EXT | N_TEXT)) {
  2185. #endif
  2186.     register struct file_entry *fe = sp->defined_by;
  2187.  
  2188.     if (fe == 0) return 0;
  2189.     
  2190. #ifdef linux
  2191.     while (sp->indirect) sp = sp->indirect;
  2192.     if (sp->defined != (N_EXT | N_TEXT)) return 0;
  2193. #endif
  2194.  
  2195.     if (!_dld_exec_flags_valid)
  2196.         find_all_executable_modules ();
  2197.  
  2198.     return fe->executable_flag;
  2199.     }
  2200.  
  2201. #ifdef linux
  2202.     if (sp && sp->defined == (N_EXT | N_ABS))
  2203.     return 1;
  2204. #endif
  2205.  
  2206.     return 0;
  2207. } /* dld_function_executable_p */
  2208.