Second attempt to integrate Paul K's changes.
[unix-history] / gnu / usr.bin / ld / ld.c
index a22d256..5b93e9e 100644 (file)
@@ -29,13 +29,10 @@ static char sccsid[] = "@(#)ld.c    6.10 (Berkeley) 5/22/91";
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /* Written by Richard Stallman with some help from Eric Albert.
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /* Written by Richard Stallman with some help from Eric Albert.
-   Set, indirect, and warning symbol features added by Randy Smith.
-
-   NOTE: Set and indirect symbols are no longer supported by this
-   version. (pk) */
+   Set, indirect, and warning symbol features added by Randy Smith. */
 
 /*
 
 /*
- *     $Id: ld.c,v 1.10 1993/11/01 16:26:13 pk Exp $
+ *     $Id: ld.c,v 1.10 1993/11/22 19:04:40 jkh Exp $
  */
    
 /* Define how to initialize system-dependent header fields.  */
  */
    
 /* Define how to initialize system-dependent header fields.  */
@@ -76,6 +73,10 @@ int  force_common_definition;
 /* 1 => assign jmp slots to text symbols in shared objects even if non-PIC */
 int    force_alias_definition;
 
 /* 1 => assign jmp slots to text symbols in shared objects even if non-PIC */
 int    force_alias_definition;
 
+/* 1 => some files contain PIC code, affects relocation bits
+       if `relocatable_output'. */
+int    pic_code_seen;
+
 /*
  * Which symbols should be stripped (omitted from the output): none, all, or
  * debugger symbols.
 /*
  * Which symbols should be stripped (omitted from the output): none, all, or
  * debugger symbols.
@@ -179,6 +180,9 @@ main(argc, argv)
        make_executable = 1;
        force_executable = 0;
        link_mode = DYNAMIC;
        make_executable = 1;
        force_executable = 0;
        link_mode = DYNAMIC;
+#ifdef SUNOS4
+       link_mode |= SILLYARCHIVE;
+#endif
        soversion = LD_VERSION_BSD;
 
        /* Initialize the cumulative counts of symbols.  */
        soversion = LD_VERSION_BSD;
 
        /* Initialize the cumulative counts of symbols.  */
@@ -204,6 +208,10 @@ main(argc, argv)
        building_shared_object =
                (!relocatable_output && (link_mode & SHAREABLE));
 
        building_shared_object =
                (!relocatable_output && (link_mode & SHAREABLE));
 
+       if (building_shared_object && entry_symbol) {
+               fatal("`-Bshareable' and `-e' options are mutually exclusive");
+       }
+
        /* Create the symbols `etext', `edata' and `end'.  */
        symtab_init(relocatable_output);
 
        /* Create the symbols `etext', `edata' and `end'.  */
        symtab_init(relocatable_output);
 
@@ -385,6 +393,12 @@ decode_command(argc, argv)
                                link_mode |= FORCEARCHIVE;
                        else if (strcmp(string, "shareable") == 0)
                                link_mode |= SHAREABLE;
                                link_mode |= FORCEARCHIVE;
                        else if (strcmp(string, "shareable") == 0)
                                link_mode |= SHAREABLE;
+#ifdef SUN_COMPAT
+                       else if (strcmp(string, "silly") == 0)
+                               link_mode |= SILLYARCHIVE;
+                       else if (strcmp(string, "~silly") == 0)
+                               link_mode &= ~SILLYARCHIVE;
+#endif
                }
                if (argv[i][1] == 'A') {
                        if (p != file_table)
                }
                if (argv[i][1] == 'A') {
                        if (p != file_table)
@@ -479,6 +493,10 @@ decode_option(swt, arg)
                return;
        if (!strcmp(swt + 1, "assert"))
                return;
                return;
        if (!strcmp(swt + 1, "assert"))
                return;
+#ifdef SUN_COMPAT
+       if (!strcmp(swt + 1, "Bsilly"))
+               return;
+#endif
        if (!strcmp(swt + 1, "Ttext")) {
                text_start = parse(arg, "%x", "invalid argument to -Ttext");
                T_flag_specified = 1;
        if (!strcmp(swt + 1, "Ttext")) {
                text_start = parse(arg, "%x", "invalid argument to -Ttext");
                T_flag_specified = 1;
@@ -546,10 +564,11 @@ decode_option(swt, arg)
        case 'Q':
                magic = oldmagic = QMAGIC;
                return;
        case 'Q':
                magic = oldmagic = QMAGIC;
                return;
+#endif
        case 'Z':
        case 'Z':
-               magic = oldmagic = ZMAGIC;
+               magic = ZMAGIC;
+               oldmagic = 0;
                return;
                return;
-#endif
 
        case 'o':
                output_filename = arg;
 
        case 'o':
                output_filename = arg;
@@ -611,7 +630,7 @@ decode_option(swt, arg)
                return;
 
        case 'z':
                return;
 
        case 'z':
-               magic = ZMAGIC;
+               oldmagic = magic = ZMAGIC;
                return;
 
        default:
                return;
 
        default:
@@ -638,12 +657,38 @@ each_file(function, arg)
 
        for (i = 0; i < number_of_files; i++) {
                register struct file_entry *entry = &file_table[i];
 
        for (i = 0; i < number_of_files; i++) {
                register struct file_entry *entry = &file_table[i];
-               if (entry->library_flag) {
-                       register struct file_entry *subentry = entry->subfiles;
-                       for (; subentry; subentry = subentry->chain)
-                               (*function) (subentry, arg);
-               } else
+               register struct file_entry *subentry;
+
+               if (entry->scrapped)
+                       continue;
+
+               if (!entry->library_flag)
                        (*function) (entry, arg);
                        (*function) (entry, arg);
+
+               subentry = entry->subfiles;
+               for (; subentry; subentry = subentry->chain) {
+                       if (subentry->scrapped)
+                               continue;
+                       (*function) (subentry, arg);
+               }
+
+#ifdef SUN_COMPAT
+               if (entry->silly_archive) {
+
+                       if (!entry->is_dynamic)
+                               error("Silly");
+
+                       if (!entry->silly_archive->library_flag)
+                               error("Sillier");
+
+                       subentry = entry->silly_archive->subfiles;
+                       for (; subentry; subentry = subentry->chain) {
+                               if (subentry->scrapped)
+                                       continue;
+                               (*function) (subentry, arg);
+                       }
+               }
+#endif
        }
 }
 
        }
 }
 
@@ -666,11 +711,16 @@ check_each_file(function, arg)
 
        for (i = 0; i < number_of_files; i++) {
                register struct file_entry *entry = &file_table[i];
 
        for (i = 0; i < number_of_files; i++) {
                register struct file_entry *entry = &file_table[i];
+               if (entry->scrapped)
+                       continue;
                if (entry->library_flag) {
                        register struct file_entry *subentry = entry->subfiles;
                if (entry->library_flag) {
                        register struct file_entry *subentry = entry->subfiles;
-                       for (; subentry; subentry = subentry->chain)
+                       for (; subentry; subentry = subentry->chain) {
+                               if (subentry->scrapped)
+                                       continue;
                                if (return_val = (*function) (subentry, arg))
                                        return return_val;
                                if (return_val = (*function) (subentry, arg))
                                        return return_val;
+                       }
                } else if (return_val = (*function) (entry, arg))
                        return return_val;
        }
                } else if (return_val = (*function) (entry, arg))
                        return return_val;
        }
@@ -688,14 +738,41 @@ each_full_file(function, arg)
 
        for (i = 0; i < number_of_files; i++) {
                register struct file_entry *entry = &file_table[i];
 
        for (i = 0; i < number_of_files; i++) {
                register struct file_entry *entry = &file_table[i];
-               if (entry->just_syms_flag || entry->is_dynamic)
+               register struct file_entry *subentry;
+
+               if (entry->scrapped || entry->just_syms_flag)
                        continue;
                        continue;
-               if (entry->library_flag) {
-                       register struct file_entry *subentry = entry->subfiles;
-                       for (; subentry; subentry = subentry->chain)
+
+#ifdef SUN_COMPAT
+               if (entry->silly_archive) {
+
+                       if (!entry->is_dynamic)
+                               error("Silly");
+
+                       if (!entry->silly_archive->library_flag)
+                               error("Sillier");
+
+                       subentry = entry->silly_archive->subfiles;
+                       for (; subentry; subentry = subentry->chain) {
+                               if (subentry->scrapped)
+                                       continue;
                                (*function) (subentry, arg);
                                (*function) (subentry, arg);
-               } else
+                       }
+               }
+#endif
+               if (entry->is_dynamic)
+                       continue;
+
+               if (!entry->library_flag)
                        (*function) (entry, arg);
                        (*function) (entry, arg);
+
+               subentry = entry->subfiles;
+               for (; subentry; subentry = subentry->chain) {
+                       if (subentry->scrapped)
+                               continue;
+                       (*function) (subentry, arg);
+               }
+
        }
 }
 
        }
 }
 
@@ -720,7 +797,7 @@ file_open (entry)
 {
        register int desc;
 
 {
        register int desc;
 
-       if (entry->superfile)
+       if (entry->superfile && entry->superfile->library_flag)
                return file_open (entry->superfile);
 
        if (entry == input_file)
                return file_open (entry->superfile);
 
        if (entry == input_file)
@@ -818,6 +895,9 @@ read_entry_symbols (desc, entry)
                entry->symbols[i].next = NULL;
                entry->symbols[i].gotslot_offset = -1;
                entry->symbols[i].gotslot_claimed = 0;
                entry->symbols[i].next = NULL;
                entry->symbols[i].gotslot_offset = -1;
                entry->symbols[i].gotslot_claimed = 0;
+               entry->symbols[i].write = 0;
+               entry->symbols[i].is_L_symbol = 0;
+               entry->symbols[i].rename = 0;
        }
 
        entry->strings_offset = N_STROFF(entry->header) +
        }
 
        entry->strings_offset = N_STROFF(entry->header) +
@@ -842,7 +922,8 @@ read_entry_strings (desc, entry)
        int buffer;
 
        if (!entry->header_read_flag || !entry->strings_offset)
        int buffer;
 
        if (!entry->header_read_flag || !entry->strings_offset)
-               fatal("internal error: %s", "cannot read string table");
+               fatal_with_file("internal error: cannot read string table for ",
+                                       entry);
 
        if (lseek (desc, entry->strings_offset, L_SET) != entry->strings_offset)
                fatal_with_file ("read_strings: lseek failure ", entry);
 
        if (lseek (desc, entry->strings_offset, L_SET) != entry->strings_offset)
                fatal_with_file ("read_strings: lseek failure ", entry);
@@ -920,7 +1001,7 @@ read_entry_relocation (desc, entry)
 \f
 /* Read in the symbols of all input files.  */
 
 \f
 /* Read in the symbols of all input files.  */
 
-void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
+void read_file_symbols (), read_entry_symbols ();
 void enter_file_symbols (), enter_global_ref ();
 
 void
 void enter_file_symbols (), enter_global_ref ();
 
 void
@@ -969,8 +1050,10 @@ read_file_symbols (entry)
                                return;
                        }
                        entry->is_dynamic = 1;
                                return;
                        }
                        entry->is_dynamic = 1;
-                       read_shared_object(desc, entry);
-                       rrs_add_shobj(entry);
+                       if (entry->superfile || rrs_add_shobj(entry))
+                               read_shared_object(desc, entry);
+                       else
+                               entry->scrapped = 1;
                } else {
                        read_entry_symbols (desc, entry);
                        entry->strings = (char *) alloca (entry->string_size);
                } else {
                        read_entry_symbols (desc, entry);
                        entry->strings = (char *) alloca (entry->string_size);
@@ -1049,25 +1132,11 @@ enter_file_symbols (entry)
                } else if (p->n_type & N_EXT) {
                        enter_global_ref(lsp,
                                p->n_un.n_strx + entry->strings, entry);
                } else if (p->n_type & N_EXT) {
                        enter_global_ref(lsp,
                                p->n_un.n_strx + entry->strings, entry);
-               } else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT))
-                                                       && !entry->is_dynamic) {
-                       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
-                               non_L_local_sym_count++;
-                       local_sym_count++;
-               } else if (!entry->is_dynamic)
-                       debugger_sym_count++;
+               } else if (p->n_un.n_strx &&
+                               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
+                       lsp->is_L_symbol = 1;
        }
 
        }
 
-       /*
-        * Count one for the local symbol that we generate,
-        * whose name is the file's name (usually) and whose address
-        * is the start of the file's text.
-        */
-
-       if (!entry->is_dynamic) {
-               local_sym_count++;
-               non_L_local_sym_count++;
-       }
 }
 
 /*
 }
 
 /*
@@ -1079,7 +1148,7 @@ enter_file_symbols (entry)
  * This chain starts in the `refs' for symbols from relocatable objects. A
  * backpointer to the global symbol is kept in LSP.
  *
  * This chain starts in the `refs' for symbols from relocatable objects. A
  * backpointer to the global symbol is kept in LSP.
  *
- * Symbols from shared objects are linked through `dynref'. For such symbols
+ * Symbols from shared objects are linked through `soref'. For such symbols
  * that's all we do at this stage, with the exception of the case where the
  * symbol is a common. The `referenced' bit is only set for references from
  * relocatable objects.
  * that's all we do at this stage, with the exception of the case where the
  * symbol is a common. The `referenced' bit is only set for references from
  * relocatable objects.
@@ -1159,6 +1228,8 @@ enter_global_ref (lsp, name, entry)
 
 #ifdef N_SIZE
        if (type == (N_SIZE | N_EXT)) {
 
 #ifdef N_SIZE
        if (type == (N_SIZE | N_EXT)) {
+               if (relocatable_output && nzp->nz_value != 0 && sp->size == 0)
+                       size_sym_count++;
                if (sp->size < nzp->nz_value)
                        sp->size = nzp->nz_value;
        } else
                if (sp->size < nzp->nz_value)
                        sp->size = nzp->nz_value;
        } else
@@ -1284,7 +1355,7 @@ void consider_relocation();
  *    symbols originating from shared objects is searched for a definition.
  *
  * 2) Then the relocation information of each relocatable file is examined
  *    symbols originating from shared objects is searched for a definition.
  *
  * 2) Then the relocation information of each relocatable file is examined
- *    for for possible contributions to the RRS section.
+ *    for possible contributions to the RRS section.
  *
  * 3) When this is done, the sizes and start addresses are set of all segments
  *    that will appear in the output file (including the RRS segment).
  *
  * 3) When this is done, the sizes and start addresses are set of all segments
  *    that will appear in the output file (including the RRS segment).
@@ -1322,10 +1393,8 @@ digest_symbols ()
        defined_global_sym_count = 0;
        digest_pass1();
 
        defined_global_sym_count = 0;
        digest_pass1();
 
-       if (!relocatable_output) {
-               each_full_file(consider_relocation, 0); /* Text */
-               each_full_file(consider_relocation, 1); /* Data */
-       }
+       each_full_file(consider_relocation, 0); /* Text */
+       each_full_file(consider_relocation, 1); /* Data */
 
        /*
         * Compute total size of sections.
 
        /*
         * Compute total size of sections.
@@ -1406,6 +1475,23 @@ printf("bssstart = %#x, bsssize = %#x\n",
                bss_size = 0;
 
        data_size += data_pad;
                bss_size = 0;
 
        data_size += data_pad;
+
+       /*
+        * Calculate total number of symbols that will go into
+        * the output symbol table (barring DISCARD_* settings).
+        */
+       global_sym_count = defined_global_sym_count +
+                                       undefined_global_sym_count;
+
+       if (dynamic_symbol->referenced)
+               global_sym_count++;
+
+       if (got_symbol->referenced)
+               global_sym_count++;
+
+       if (relocatable_output)
+               /* For each alias we write out two struct nlists */
+               global_sym_count += global_alias_count + size_sym_count;
 }
 
 void
 }
 
 void
@@ -1507,7 +1593,7 @@ digest_pass1()
                        register int    type = p->n_type;
 
                        if ((type & N_EXT) && type != (N_UNDF | N_EXT)
                        register int    type = p->n_type;
 
                        if ((type & N_EXT) && type != (N_UNDF | N_EXT)
-                           && (type & N_TYPE) != N_FN) {
+                                               && (type & N_TYPE) != N_FN) {
                                /* non-common definition */
                                sp->def_nlist = p;
                                sp->so_defined = type;
                                /* non-common definition */
                                sp->def_nlist = p;
                                sp->so_defined = type;
@@ -1597,7 +1683,7 @@ digest_pass2()
                        /*
                         * It's data from shared object with size info.
                         */
                        /*
                         * It's data from shared object with size info.
                         */
-                       if (sp->so_defined != (N_DATA + N_EXT))
+                       if (!sp->so_defined)
                                fatal("%s: Bogus N_SIZE item", sp->name);
 
                } else
                                fatal("%s: Bogus N_SIZE item", sp->name);
 
                } else
@@ -1675,6 +1761,16 @@ consider_relocation (entry, dataseg)
 
        for (; reloc < end; reloc++) {
 
 
        for (; reloc < end; reloc++) {
 
+               if (relocatable_output) {
+                       lsp = &entry->symbols[reloc->r_symbolnum];
+                       if (RELOC_BASEREL_P(reloc)) {
+                               pic_code_seen = 1;
+                               if (!RELOC_EXTERN_P(reloc))
+                                       lsp->rename = 1;
+                       }
+                       continue;
+               }
+
                /*
                 * First, do the PIC specific relocs.
                 * r_relative and r_copy should not occur at this point
                /*
                 * First, do the PIC specific relocs.
                 * r_relative and r_copy should not occur at this point
@@ -1707,12 +1803,12 @@ consider_relocation (entry, dataseg)
                        sp = lsp->symbol;
                        if (sp->alias)
                                sp = sp->alias;
                        sp = lsp->symbol;
                        if (sp->alias)
                                sp = sp->alias;
-                       alloc_rrs_jmpslot(sp);
+                       alloc_rrs_jmpslot(entry, sp);
 
                } else if (RELOC_BASEREL_P(reloc)) {
 
                        lsp = &entry->symbols[reloc->r_symbolnum];
 
                } else if (RELOC_BASEREL_P(reloc)) {
 
                        lsp = &entry->symbols[reloc->r_symbolnum];
-                       alloc_rrs_gotslot(reloc, lsp);
+                       alloc_rrs_gotslot(entry, reloc, lsp);
 
                } else if (RELOC_EXTERN_P(reloc)) {
 
 
                } else if (RELOC_EXTERN_P(reloc)) {
 
@@ -1757,25 +1853,44 @@ consider_relocation (entry, dataseg)
                         */
 
                        if (building_shared_object) {
                         */
 
                        if (building_shared_object) {
-                               alloc_rrs_reloc(sp);
+                               alloc_rrs_reloc(entry, sp);
                                continue;
                        }
 
                                continue;
                        }
 
-                       if (force_alias_definition &&
+                       /*
+                        * Only allocate an alias for function calls. Use
+                        * sp->size here as a heuristic to discriminate
+                        * between function definitions and data residing
+                        * in the text segment.
+                        * NOTE THAT THE COMPILER MUST NOT GENERATE ".size"
+                        * DIRECTIVES FOR FUNCTIONS.
+                        * In the future we might go for ".type" directives.
+                        */
+                       if (force_alias_definition && sp->size == 0 &&
                                        sp->so_defined == N_TEXT + N_EXT) {
 
                                /* Call to shared library procedure */
                                        sp->so_defined == N_TEXT + N_EXT) {
 
                                /* Call to shared library procedure */
-                               alloc_rrs_jmpslot(sp);
+                               alloc_rrs_jmpslot(entry, sp);
+#define EXPERIMENTAL
+#ifdef EXPERIMENTAL
+                               if (!RELOC_PCREL_P(reloc)) {
+#ifdef DEBUG
+printf("%s: FUNC flag set\n", sp->name);
+#endif
+                                       sp->aux = RRS_FUNC;
+                               }
+#endif
 
                        } else if (sp->size &&
 
                        } else if (sp->size &&
-                                       sp->so_defined == N_DATA + N_EXT) {
+                                       (sp->so_defined == N_DATA + N_EXT ||
+                                       sp->so_defined == N_TEXT + N_EXT)) {
 
                                /* Reference to shared library data */
 
                                /* Reference to shared library data */
-                               alloc_rrs_cpy_reloc(sp);
+                               alloc_rrs_cpy_reloc(entry, sp);
                                sp->defined = N_SIZE;
 
                        } else if (!sp->defined && sp->max_common_size == 0)
                                sp->defined = N_SIZE;
 
                        } else if (!sp->defined && sp->max_common_size == 0)
-                               alloc_rrs_reloc(sp);
+                               alloc_rrs_reloc(entry, sp);
 
                } else {
                        /*
 
                } else {
                        /*
@@ -1784,7 +1899,7 @@ consider_relocation (entry, dataseg)
                         * address dependent.
                         */
                        if (building_shared_object) {
                         * address dependent.
                         */
                        if (building_shared_object) {
-                               alloc_rrs_segment_reloc(reloc);
+                               alloc_rrs_segment_reloc(entry, reloc);
                        }
                }
        }
                        }
                }
        }
@@ -1816,13 +1931,15 @@ consider_file_section_lengths (entry)
 /*
  * Determine where the sections of ENTRY go into the output file,
  * whose total section sizes are already known.
 /*
  * Determine where the sections of ENTRY go into the output file,
  * whose total section sizes are already known.
- * Also relocate the addresses of the file's local and debugger symbols.
+ * Also relocate the addresses of the file's local and debugger symbols
+ * and determine which of the local symbols will make it into the
+ * output symbol table.
  */
 void
 relocate_file_addresses (entry)
      register struct file_entry *entry;
 {
  */
 void
 relocate_file_addresses (entry)
      register struct file_entry *entry;
 {
-       register struct localsymbol *lsp, *lspend;
+       register struct localsymbol     *lsp, *lspend;
 
        entry->text_start_address += text_start;
        /*
 
        entry->text_start_address += text_start;
        /*
@@ -1841,13 +1958,14 @@ printf("%s: datastart: %#x, bss %#x\n", get_file_name(entry),
 
        for (lsp = entry->symbols; lsp < lspend; lsp++) {
                register struct nlist *p = &lsp->nzlist.nlist;
 
        for (lsp = entry->symbols; lsp < lspend; lsp++) {
                register struct nlist *p = &lsp->nzlist.nlist;
+               register int type = p->n_type;
+
                /*
                 * If this belongs to a section, update it
                 * by the section's start address
                 */
                /*
                 * If this belongs to a section, update it
                 * by the section's start address
                 */
-               register int type = p->n_type & N_TYPE;
 
 
-               switch (type) {
+               switch (type & N_TYPE) {
                case N_TEXT:
                case N_SETT:
                        p->n_value += entry->text_start_address;
                case N_TEXT:
                case N_SETT:
                        p->n_value += entry->text_start_address;
@@ -1871,7 +1989,54 @@ printf("%s: datastart: %#x, bss %#x\n", get_file_name(entry),
                                - entry->header.a_text - entry->header.a_data;
                break;
                }
                                - entry->header.a_text - entry->header.a_data;
                break;
                }
+
+               /*
+                * See if this symbol should be in the output symbol table.
+                */
+
+               if (type == N_WARNING)
+                       continue;
+
+               if (SET_ELEMENT_P (type)) {
+                       /*
+                        * This occurs even if global. These types of
+                        * symbols are never written globally, though
+                        * they are stored globally.
+                        */
+                       lsp->write = relocatable_output;
+
+               } else if (!(type & (N_STAB | N_EXT))) {
+
+                       /*
+                        * Ordinary local symbol
+                        */
+                       lsp->write = (lsp->rename || (
+                                       discard_locals != DISCARD_ALL &&
+                                       !(discard_locals == DISCARD_L &&
+                                                   lsp->is_L_symbol)));
+                       if (lsp->write)
+                               local_sym_count++;
+
+               } else if (!(type & N_EXT)) {
+
+                       /*
+                        * Debugger symbol
+                        */
+                       lsp->write = (strip_symbols == STRIP_NONE);
+                       if (lsp->write)
+                               debugger_sym_count++;
+
+               }
        }
        }
+
+       /*
+        * Count one for the local symbol that we generate,
+        * whose name is the file's name (usually) and whose address
+        * is the start of the file's text.
+        */
+       if (discard_locals != DISCARD_ALL)
+               local_sym_count++;
+
 }
 \f
 /* Write the output file */
 }
 \f
 /* Write the output file */
@@ -1931,7 +2096,7 @@ write_header ()
 {
        int flags = (rrs_section_type == RRS_FULL) ? EX_DYNAMIC : 0;
 
 {
        int flags = (rrs_section_type == RRS_FULL) ? EX_DYNAMIC : 0;
 
-       N_SET_FLAG (outheader, flags);
+       if (!oldmagic) N_SET_FLAG (outheader, flags);
        outheader.a_text = text_size;
        outheader.a_data = data_size;
        outheader.a_bss = bss_size;
        outheader.a_text = text_size;
        outheader.a_data = data_size;
        outheader.a_bss = bss_size;
@@ -1940,33 +2105,19 @@ write_header ()
 
        if (strip_symbols == STRIP_ALL)
                nsyms = 0;
 
        if (strip_symbols == STRIP_ALL)
                nsyms = 0;
-       else {
-               nsyms = (defined_global_sym_count + undefined_global_sym_count);
-               if (discard_locals == DISCARD_L)
-                       nsyms += non_L_local_sym_count;
-               else if (discard_locals == DISCARD_NONE)
-                       nsyms += local_sym_count;
-
-               if (relocatable_output)
-                       /* For each alias we write out two struct nlists */
-                       nsyms += set_symbol_count + global_alias_count;
-
-               if (dynamic_symbol->referenced)
-                       nsyms++, special_sym_count++;
-
-               if (got_symbol->referenced)
-                       nsyms++, special_sym_count++;
-       }
+       else
+               nsyms = global_sym_count + local_sym_count + debugger_sym_count;
 
 
-       if (strip_symbols == STRIP_NONE)
-               nsyms += debugger_sym_count;
+       if (relocatable_output)
+               nsyms += set_symbol_count;
 
 
-#ifdef DEBUG
-printf("defined globals: %d, undefined globals %d, locals: %d (non_L: %d), \
-debug symbols: %d, special: %d --> nsyms %d\n",
+printf("global symbols %d (defined %d, undefined %d), locals: %d, \
+debug symbols: %d, set_symbols %d, aliases %d --> nsyms %d\n",
+       global_sym_count,
        defined_global_sym_count, undefined_global_sym_count,
        defined_global_sym_count, undefined_global_sym_count,
-       local_sym_count, non_L_local_sym_count, debugger_sym_count,
-       special_sym_count, nsyms);
+       local_sym_count, debugger_sym_count,
+       set_symbol_count, global_alias_count, nsyms);
+#ifdef DEBUG
 #endif
 
        outheader.a_syms = nsyms * sizeof (struct nlist);
 #endif
 
        outheader.a_syms = nsyms * sizeof (struct nlist);
@@ -2180,7 +2331,7 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
                                        data_relocation - text_relocation;
                        } else
                                relocation = addend +
                                        data_relocation - text_relocation;
                        } else
                                relocation = addend +
-                                       claim_rrs_jmpslot(r, sp, addend);
+                                       claim_rrs_jmpslot(entry, r, sp, addend);
 
                } else if (RELOC_BASEREL_P(r)) {
 
 
                } else if (RELOC_BASEREL_P(r)) {
 
@@ -2197,7 +2348,8 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
                                relocation = claim_rrs_internal_gotslot(entry,
                                                                r, lsp, addend);
                        else
                                relocation = claim_rrs_internal_gotslot(entry,
                                                                r, lsp, addend);
                        else
-                               relocation = claim_rrs_gotslot(r, lsp, addend);
+                               relocation = claim_rrs_gotslot(entry,
+                                                               r, lsp, addend);
 
                } else if (RELOC_EXTERN_P(r)) {
 
 
                } else if (RELOC_EXTERN_P(r)) {
 
@@ -2213,7 +2365,13 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
                                sp = sp->alias;
 
                        if (relocatable_output) {
                                sp = sp->alias;
 
                        if (relocatable_output) {
-                               relocation = addend + sp->value;
+                               relocation = addend;
+                               /*
+                                * In PIC code, we keep the reference to the
+                                * external symbol, even if defined now.
+                                */
+                               if (!pic_code_seen)
+                                       relocation += sp->value;
                        } else if (sp->defined) {
                                if (sp == got_symbol) {
                                        /* Handle _GOT_ refs */
                        } else if (sp->defined) {
                                if (sp == got_symbol) {
                                        /* Handle _GOT_ refs */
@@ -2229,7 +2387,8 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
                                                entry->data_start_address:
                                                entry->text_start_address;
                                        relocation = addend;
                                                entry->data_start_address:
                                                entry->text_start_address;
                                        relocation = addend;
-                                       if (claim_rrs_reloc(r, sp, &relocation))
+                                       if (claim_rrs_reloc(entry, r,
+                                                       sp, &relocation))
                                                continue;
                                } else if (sp->defined == N_SIZE) {
                                        /*
                                                continue;
                                } else if (sp->defined == N_SIZE) {
                                        /*
@@ -2242,7 +2401,7 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
 
                                        relocation = addend + sp->value;
                                        r->r_address = sp->value;
 
                                        relocation = addend + sp->value;
                                        r->r_address = sp->value;
-                                       claim_rrs_cpy_reloc(r, sp);
+                                       claim_rrs_cpy_reloc(entry, r, sp);
                                } else
                                        /* Plain old relocation */
                                        relocation = addend + sp->value;
                                } else
                                        /* Plain old relocation */
                                        relocation = addend + sp->value;
@@ -2272,7 +2431,8 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
                                                goto undefined;
 
                                        relocation = addend +
                                                goto undefined;
 
                                        relocation = addend +
-                                               claim_rrs_jmpslot(r, sp, addend);
+                                               claim_rrs_jmpslot(entry, r,
+                                                               sp, addend);
                                        break;
 
                                case N_DATA+N_EXT:
                                        break;
 
                                case N_DATA+N_EXT:
@@ -2283,7 +2443,8 @@ perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
                                                entry->data_start_address:
                                                entry->text_start_address;
                                        relocation = addend;
                                                entry->data_start_address:
                                                entry->text_start_address;
                                        relocation = addend;
-                                       if (claim_rrs_reloc(r, sp, &relocation))
+                                       if (claim_rrs_reloc(entry, r,
+                                                       sp, &relocation))
                                                continue;
                                        break;
 
                                                continue;
                                        break;
 
@@ -2349,7 +2510,7 @@ printf("%s: BSS found in so_defined\n", sp->name);
                                r->r_address += dataseg?
                                        entry->data_start_address:
                                        entry->text_start_address;
                                r->r_address += dataseg?
                                        entry->data_start_address:
                                        entry->text_start_address;
-                               claim_rrs_segment_reloc(r);
+                               claim_rrs_segment_reloc(entry, r);
                        }
                }
 
                        }
                }
 
@@ -2364,12 +2525,12 @@ printf("%s: BSS found in so_defined\n", sp->name);
 /* For relocatable_output only: write out the relocation,
    relocating the addresses-to-be-relocated.  */
 
 /* For relocatable_output only: write out the relocation,
    relocating the addresses-to-be-relocated.  */
 
-void coptxtrel (), copdatrel ();
+void coptxtrel(), copdatrel(), assign_symbolnums();
 
 void
 write_rel ()
 {
 
 void
 write_rel ()
 {
-       register int count = 0;
+       int count = 0;
 
        if (trace_files)
                fprintf (stderr, "Writing text relocation:\n\n");
 
        if (trace_files)
                fprintf (stderr, "Writing text relocation:\n\n");
@@ -2388,15 +2549,18 @@ write_rel ()
        FOR_EACH_SYMBOL(i, sp) {
                if (sp != dynamic_symbol && sp->referenced) {
                        sp->symbolnum = count++;
        FOR_EACH_SYMBOL(i, sp) {
                if (sp != dynamic_symbol && sp->referenced) {
                        sp->symbolnum = count++;
+                       if (sp->size)
+                               count++;
+                       if (sp->alias)
+                               count++;
                }
        } END_EACH_SYMBOL;
 
                }
        } END_EACH_SYMBOL;
 
-       /* Correct, because if (relocatable_output), we will also be writing
-       whatever indirect blocks we have.  */
-       if (count != defined_global_sym_count + undefined_global_sym_count
-                                                       + special_sym_count)
+       if (count != global_sym_count)
                fatal ("internal error: write_rel: count = %d", count);
 
                fatal ("internal error: write_rel: count = %d", count);
 
+       each_full_file (assign_symbolnums, &count);
+
        /* Write out the relocations of all files, remembered from copy_text. */
        each_full_file (coptxtrel, 0);
 
        /* Write out the relocations of all files, remembered from copy_text. */
        each_full_file (coptxtrel, 0);
 
@@ -2409,6 +2573,30 @@ write_rel ()
                fprintf (stderr, "\n");
 }
 
                fprintf (stderr, "\n");
 }
 
+/*
+ * Assign symbol ordinal numbers to local symbols in each entry.
+ */
+void
+assign_symbolnums(entry, countp)
+       struct file_entry       *entry;
+       int                     *countp;
+{
+       struct localsymbol      *lsp, *lspend;
+       int                     n = *countp;
+
+       lspend = entry->symbols + entry->nsymbols;
+
+       if (discard_locals != DISCARD_ALL)
+               /* Count the N_FN symbol for this entry */
+               n++;
+
+       for (lsp = entry->symbols; lsp < lspend; lsp++) {
+               if (lsp->write)
+                       lsp->symbolnum = n++;
+       }
+       *countp = n;
+}
+
 void
 coptxtrel(entry)
        struct file_entry *entry;
 void
 coptxtrel(entry)
        struct file_entry *entry;
@@ -2420,21 +2608,29 @@ coptxtrel(entry)
        end = r + entry->ntextrel;
 
        for (; r < end; r++) {
        end = r + entry->ntextrel;
 
        for (; r < end; r++) {
-               register int  symindex;
-               symbol       *sp;
+               register int            symindex;
+               struct localsymbol      *lsp;
+               symbol                  *sp;
 
                RELOC_ADDRESS(r) += reloc;
 
 
                RELOC_ADDRESS(r) += reloc;
 
-               if (!RELOC_EXTERN_P(r))
-                       continue;
-
                symindex = RELOC_SYMBOL(r);
                symindex = RELOC_SYMBOL(r);
-               sp = entry->symbols[symindex].symbol;
+               lsp = &entry->symbols[symindex];
+
+               if (!RELOC_EXTERN_P(r)) {
+                       if (!pic_code_seen)
+                               continue;
+                       if (RELOC_BASEREL_P(r))
+                               RELOC_SYMBOL(r) = lsp->symbolnum;
+                       continue;
+               }
 
                if (symindex >= entry->nsymbols)
                        fatal_with_file(
                        "relocation symbolnum out of range in ", entry);
 
 
                if (symindex >= entry->nsymbols)
                        fatal_with_file(
                        "relocation symbolnum out of range in ", entry);
 
+               sp = lsp->symbol;
+
 #ifdef N_INDR
                /* Resolve indirection.  */
                if ((sp->defined & ~N_EXT) == N_INDR) {
 #ifdef N_INDR
                /* Resolve indirection.  */
                if ((sp->defined & ~N_EXT) == N_INDR) {
@@ -2450,8 +2646,11 @@ coptxtrel(entry)
                 */
 
                if (sp->defined) {
                 */
 
                if (sp->defined) {
-                       RELOC_EXTERN_P(r) = 0;
-                       RELOC_SYMBOL(r) = (sp->defined & N_TYPE);
+                       if (!pic_code_seen) {
+                               RELOC_EXTERN_P(r) = 0;
+                               RELOC_SYMBOL(r) = (sp->defined & N_TYPE);
+                       } else
+                               RELOC_SYMBOL(r) = sp->symbolnum;
 #ifdef RELOC_ADD_EXTRA
                        /*
                         * If we aren't going to be adding in the
 #ifdef RELOC_ADD_EXTRA
                        /*
                         * If we aren't going to be adding in the
@@ -2496,8 +2695,13 @@ copdatrel(entry)
 
                RELOC_ADDRESS(r) += reloc;
 
 
                RELOC_ADDRESS(r) += reloc;
 
-               if (!RELOC_EXTERN_P(r))
+               if (!RELOC_EXTERN_P(r)) {
+                       if (RELOC_BASEREL_P(r))
+                               fatal_with_file(
+                                   "Unsupported relocation type in ",
+                                       entry);
                        continue;
                        continue;
+               }
 
                symindex = RELOC_SYMBOL(r);
                sp = entry->symbols[symindex].symbol;
 
                symindex = RELOC_SYMBOL(r);
                sp = entry->symbols[symindex].symbol;
@@ -2517,10 +2721,10 @@ copdatrel(entry)
 
                symtype = sp->defined & N_TYPE;
 
 
                symtype = sp->defined & N_TYPE;
 
-               if (force_common_definition ||
+               if (!pic_code_seen && (force_common_definition ||
                                        symtype == N_DATA ||
                                        symtype == N_TEXT ||
                                        symtype == N_DATA ||
                                        symtype == N_TEXT ||
-                                       symtype == N_ABS) {
+                                       symtype == N_ABS)) {
                        RELOC_EXTERN_P(r) = 0;
                        RELOC_SYMBOL(r) = symtype;
                } else
                        RELOC_EXTERN_P(r) = 0;
                        RELOC_SYMBOL(r) = symtype;
                } else
@@ -2535,8 +2739,8 @@ copdatrel(entry)
                                sizeof(struct relocation_info), outdesc);
 }
 \f
                                sizeof(struct relocation_info), outdesc);
 }
 \f
-void write_file_syms ();
-void write_string_table ();
+void write_file_syms __P((struct file_entry *, int *));
+void write_string_table __P((void));
 
 /* Offsets and current lengths of symbol and string tables in output file. */
 
 
 /* Offsets and current lengths of symbol and string tables in output file. */
 
@@ -2618,10 +2822,6 @@ void
 write_syms()
 {
        /* Number of symbols written so far.  */
 write_syms()
 {
        /* Number of symbols written so far.  */
-       int             non_local_syms = defined_global_sym_count
-                                       + undefined_global_sym_count
-                                       + global_alias_count
-                                       + special_sym_count;
        int             syms_written = 0;
        struct nlist    nl;
 
        int             syms_written = 0;
        struct nlist    nl;
 
@@ -2629,8 +2829,8 @@ write_syms()
         * Buffer big enough for all the global symbols.  One extra struct
         * for each indirect symbol to hold the extra reference following.
         */
         * Buffer big enough for all the global symbols.  One extra struct
         * for each indirect symbol to hold the extra reference following.
         */
-       struct nlist   *buf
-               = (struct nlist *)alloca(non_local_syms * sizeof(struct nlist));
+       struct nlist   *buf = (struct nlist *)
+                               alloca(global_sym_count * sizeof(struct nlist));
        /* Pointer for storing into BUF.  */
        register struct nlist *bufp = buf;
 
        /* Pointer for storing into BUF.  */
        register struct nlist *bufp = buf;
 
@@ -2653,8 +2853,8 @@ write_syms()
         * extra space for the references following indirect outputs.
         */
 
         * extra space for the references following indirect outputs.
         */
 
-       strtab_vector = (char **) alloca((non_local_syms) * sizeof(char *));
-       strtab_lens = (int *) alloca((non_local_syms) * sizeof(int));
+       strtab_vector = (char **) alloca((global_sym_count) * sizeof(char *));
+       strtab_lens = (int *) alloca((global_sym_count) * sizeof(int));
        strtab_index = 0;
 
        /*
        strtab_index = 0;
 
        /*
@@ -2763,10 +2963,10 @@ write_syms()
 
                /* Output to the buffer and count it.  */
 
 
                /* Output to the buffer and count it.  */
 
-               if (syms_written >= non_local_syms)
+               if (syms_written >= global_sym_count)
                        fatal(
                        "internal error: number of symbols exceeds allocated %d",
                        fatal(
                        "internal error: number of symbols exceeds allocated %d",
-                               non_local_syms);
+                               global_sym_count);
                *bufp++ = nl;
                syms_written++;
 
                *bufp++ = nl;
                syms_written++;
 
@@ -2777,6 +2977,18 @@ write_syms()
                        nl.n_un.n_strx =
                                assign_string_table_index(sp->alias->name);
                        nl.n_value = 0;
                        nl.n_un.n_strx =
                                assign_string_table_index(sp->alias->name);
                        nl.n_value = 0;
+                       nl.n_other = 0;
+                       nl.n_desc = 0;
+                       *bufp++ = nl;
+                       syms_written++;
+               }
+
+               if (relocatable_output && sp->size) {
+                       nl.n_type = N_SIZE + N_EXT;
+                       nl.n_un.n_strx = assign_string_table_index(sp->name);
+                       nl.n_value = sp->size;
+                       nl.n_other = 0;
+                       nl.n_desc = 0;
                        *bufp++ = nl;
                        syms_written++;
                }
                        *bufp++ = nl;
                        syms_written++;
                }
@@ -2786,10 +2998,10 @@ printf("writesym(#%d): %s, type %x\n", syms_written, sp->name, sp->defined);
 #endif
        } END_EACH_SYMBOL;
 
 #endif
        } END_EACH_SYMBOL;
 
-       if (syms_written != strtab_index || strtab_index != non_local_syms)
+       if (syms_written != strtab_index || strtab_index != global_sym_count)
                fatal("internal error:\
 wrong number (%d) of global symbols written into output file, should be %d",
                fatal("internal error:\
 wrong number (%d) of global symbols written into output file, should be %d",
-                               syms_written, non_local_syms);
+                               syms_written, global_sym_count);
 
        /* Output the buffer full of `struct nlist's.  */
 
 
        /* Output the buffer full of `struct nlist's.  */
 
@@ -2873,7 +3085,9 @@ write_file_syms(entry, syms_written_addr)
                nl.n_other = 0;
                *bufp++ = nl;
                (*syms_written_addr)++;
                nl.n_other = 0;
                *bufp++ = nl;
                (*syms_written_addr)++;
+#if 0
                entry->local_syms_offset = *syms_written_addr * sizeof(struct nlist);
                entry->local_syms_offset = *syms_written_addr * sizeof(struct nlist);
+#endif
        }
        /* Read the file's string table.  */
 
        }
        /* Read the file's string table.  */
 
@@ -2886,46 +3100,35 @@ write_file_syms(entry, syms_written_addr)
                register struct nlist *p = &lsp->nzlist.nlist;
                register int    type = p->n_type;
                register int    write = 0;
                register struct nlist *p = &lsp->nzlist.nlist;
                register int    type = p->n_type;
                register int    write = 0;
+               char            *name;
+
+               if (! lsp->write)
+                       continue;
+
+               if (p->n_un.n_strx == 0)
+                       name = NULL;
+               else if (lsp->rename == 0)
+                       name = p->n_un.n_strx + entry->strings;
+               else {
+                       char *cp = p->n_un.n_strx + entry->strings;
+                       name = (char *)alloca(
+                                       strlen(entry->local_sym_name) +
+                                       strlen(cp) + 2 );
+                       (void)sprintf(name, "%s.%s", entry->local_sym_name, cp);
+               }
 
                /*
 
                /*
-                * WRITE gets 1 for a non-global symbol that should be
-                * written.
+                * If this symbol has a name, allocate space for it
+                * in the output string table.
                 */
 
                 */
 
-               if (SET_ELEMENT_P (type))
-                       /*
-                        * This occurs even if global. These types of
-                        * symbols are never written globally, though
-                        * they are stored globally.
-                        */
-                       write = relocatable_output;
-               else if (!(type & (N_STAB | N_EXT)))
-                       /* ordinary local symbol */
-                       write = ((discard_locals != DISCARD_ALL)
-                                && !(discard_locals == DISCARD_L &&
-                           (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
-                                && type != N_WARNING);
-               else if (!(type & N_EXT))
-                       /* debugger symbol */
-                       write = (strip_symbols == STRIP_NONE) &&
-                               !(discard_locals == DISCARD_L &&
-                               (p->n_un.n_strx + entry->strings)[0] == LPREFIX);
-
-               if (write) {
-                       /*
-                        * If this symbol has a name, allocate space for it
-                        * in the output string table.
-                        */
-
-                       if (p->n_un.n_strx)
-                               p->n_un.n_strx = assign_string_table_index(
-                                               p->n_un.n_strx + entry->strings);
+               if (name)
+                       p->n_un.n_strx = assign_string_table_index(name);
 
 
-                       /* Output this symbol to the buffer and count it.  */
+               /* Output this symbol to the buffer and count it.  */
 
 
-                       *bufp++ = *p;
-                       (*syms_written_addr)++;
-               }
+               *bufp++ = *p;
+               (*syms_written_addr)++;
        }
 
        /* All the symbols are now in BUF; write them.  */
        }
 
        /* All the symbols are now in BUF; write them.  */