summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin
diff options
context:
space:
mode:
authorpaul <paul@FreeBSD.org>1993-11-03 23:41:59 +0000
committerpaul <paul@FreeBSD.org>1993-11-03 23:41:59 +0000
commit66fdbc00cfad3be604d07da78b916f4c04f96dbc (patch)
treeae9c655d7f7352058dd703d580c1f70430c1e5f7 /gnu/usr.bin
parent7f9e326f196003559252a070bd773ee08a76b244 (diff)
downloadFreeBSD-src-66fdbc00cfad3be604d07da78b916f4c04f96dbc.zip
FreeBSD-src-66fdbc00cfad3be604d07da78b916f4c04f96dbc.tar.gz
Imported NetBSD's ld for shared libs.
Diffstat (limited to 'gnu/usr.bin')
-rw-r--r--gnu/usr.bin/ld/Makefile15
-rw-r--r--gnu/usr.bin/ld/cplus-dem.c3
-rw-r--r--gnu/usr.bin/ld/etc.c211
-rw-r--r--gnu/usr.bin/ld/i386/md-static-funcs.c15
-rw-r--r--gnu/usr.bin/ld/i386/md.c329
-rw-r--r--gnu/usr.bin/ld/i386/md.h198
-rw-r--r--gnu/usr.bin/ld/i386/mdprologue.S124
-rw-r--r--gnu/usr.bin/ld/ld.1192
-rw-r--r--gnu/usr.bin/ld/ld.1aout192
-rw-r--r--gnu/usr.bin/ld/ld.c6550
-rw-r--r--gnu/usr.bin/ld/ld.h890
-rw-r--r--gnu/usr.bin/ld/ldconfig/Makefile13
-rw-r--r--gnu/usr.bin/ld/ldconfig/ldconfig.898
-rw-r--r--gnu/usr.bin/ld/ldconfig/ldconfig.c411
-rw-r--r--gnu/usr.bin/ld/ldd/Makefile7
-rw-r--r--gnu/usr.bin/ld/ldd/ldd.125
-rw-r--r--gnu/usr.bin/ld/ldd/ldd.c139
-rw-r--r--gnu/usr.bin/ld/lib.c640
-rw-r--r--gnu/usr.bin/ld/rrs.c1064
-rw-r--r--gnu/usr.bin/ld/rtld/Makefile24
-rw-r--r--gnu/usr.bin/ld/rtld/md-prologue.c39
-rw-r--r--gnu/usr.bin/ld/rtld/rtld.c1026
-rw-r--r--gnu/usr.bin/ld/shlib.c203
-rw-r--r--gnu/usr.bin/ld/sparc/md-static-funcs.c35
-rw-r--r--gnu/usr.bin/ld/sparc/md.c279
-rw-r--r--gnu/usr.bin/ld/sparc/md.h270
-rw-r--r--gnu/usr.bin/ld/sparc/mdprologue.S101
-rw-r--r--gnu/usr.bin/ld/symbol.c148
-rw-r--r--gnu/usr.bin/ld/symseg.h3
-rw-r--r--gnu/usr.bin/ld/warnings.c755
-rw-r--r--gnu/usr.bin/ld/xbits.c166
31 files changed, 10007 insertions, 4158 deletions
diff --git a/gnu/usr.bin/ld/Makefile b/gnu/usr.bin/ld/Makefile
index 6816916..ff87a39 100644
--- a/gnu/usr.bin/ld/Makefile
+++ b/gnu/usr.bin/ld/Makefile
@@ -1,8 +1,15 @@
-# @(#)Makefile 6.2 (Berkeley) 4/30/91
+# $Id: Makefile,v 1.8 1993/11/03 13:01:36 cgd Exp $
+#
PROG= ld
-SRCS= ld.c cplus-dem.c
-NOMAN= noman
-LINKS= ${BINDIR}/ld ${BINDIR}/../libexec/ld++
+SRCS= ld.c symbol.c lib.c shlib.c warnings.c etc.c rrs.c xbits.c md.c
+CFLAGS += -g -I$(.CURDIR) -I$(.CURDIR)/$(MACHINE)
+
+LDADD+= -lgnumalloc
+DPADD+= /usr/lib/libgnumalloc.a
+
+SUBDIR= ldconfig ldd rtld
+
+.PATH: $(.CURDIR)/$(MACHINE)
.include <bsd.prog.mk>
diff --git a/gnu/usr.bin/ld/cplus-dem.c b/gnu/usr.bin/ld/cplus-dem.c
index c24ca7a..808db5e 100644
--- a/gnu/usr.bin/ld/cplus-dem.c
+++ b/gnu/usr.bin/ld/cplus-dem.c
@@ -4,7 +4,8 @@
*/
#ifndef lint
-static char sccsid[] = "@(#)cplus-dem.c 5.4 (Berkeley) 4/30/91";
+/*static char sccsid[] = "from: @(#)cplus-dem.c 5.4 (Berkeley) 4/30/91";*/
+static char rcsid[] = "$Id: cplus-dem.c,v 1.2 1993/08/01 18:46:58 mycroft Exp $";
#endif /* not lint */
/* Demangler for GNU C++
diff --git a/gnu/usr.bin/ld/etc.c b/gnu/usr.bin/ld/etc.c
new file mode 100644
index 0000000..33c6bf2
--- /dev/null
+++ b/gnu/usr.bin/ld/etc.c
@@ -0,0 +1,211 @@
+/*
+ * $Id: etc.c,v 1.2 1993/10/21 00:52:52 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <ar.h>
+#include <ranlib.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+#include "ld.h"
+
+/*
+ * Report a nonfatal error.
+ */
+
+void
+#if __STDC__
+error(char *fmt, ...)
+#else
+error(fmt, va_alist)
+ char *fmt;
+ va_dcl
+#endif
+{
+ va_list ap;
+#if __STDC__
+ va_start(ap, fmt);
+#else
+ va_start(ap);
+#endif
+ (void)fprintf(stderr, "%s: ", progname);
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, "\n");
+ va_end(ap);
+}
+
+/*
+ * Report a fatal error.
+ */
+
+void
+#if __STDC__
+fatal(char *fmt, ...)
+#else
+fatal(fmt, va_alist)
+ char *fmt;
+ va_dcl
+#endif
+{
+ va_list ap;
+#if __STDC__
+ va_start(ap, fmt);
+#else
+ va_start(ap);
+#endif
+ (void)fprintf(stderr, "%s: ", progname);
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, "\n");
+ va_end(ap);
+
+ if (outdesc >= 0)
+ unlink(output_filename);
+ exit(1);
+}
+
+
+/*
+ * Return a newly-allocated string whose contents concatenate
+ * the strings S1, S2, S3.
+ */
+
+char *
+concat(s1, s2, s3)
+ char *s1, *s2, *s3;
+{
+ register int len1 = strlen (s1),
+ len2 = strlen (s2),
+ len3 = strlen (s3);
+
+ register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
+
+ strcpy (result, s1);
+ strcpy (result + len1, s2);
+ strcpy (result + len1 + len2, s3);
+ result[len1 + len2 + len3] = 0;
+
+ return result;
+}
+
+/* Parse the string ARG using scanf format FORMAT, and return the result.
+ If it does not parse, report fatal error
+ generating the error message using format string ERROR and ARG as arg. */
+
+int
+parse(arg, format, error)
+ char *arg, *format, *error;
+{
+ int x;
+ if (1 != sscanf (arg, format, &x))
+ fatal (error, arg);
+ return x;
+}
+
+/* Like malloc but get fatal error if memory is exhausted. */
+
+void *
+xmalloc(size)
+ int size;
+{
+ register void *result = (void *)malloc (size);
+
+ if (!result)
+ fatal ("virtual memory exhausted", 0);
+
+ return result;
+}
+
+/* Like realloc but get fatal error if memory is exhausted. */
+
+void *
+xrealloc(ptr, size)
+ void *ptr;
+ int size;
+{
+ register void *result;
+
+ if (ptr == NULL)
+ result = (void *)malloc (size);
+ else
+ result = (void *)realloc (ptr, size);
+
+ if (!result)
+ fatal ("virtual memory exhausted", 0);
+
+ return result;
+}
+
+
+#ifdef USG
+void
+bzero(p, n)
+ char *p;
+{
+ memset (p, 0, n);
+}
+
+void
+bcopy(from, to, n)
+ char *from, *to;
+{
+ memcpy (to, from, n);
+}
+#endif
+
+
+/* These must move */
+
+#ifndef RTLD
+/*
+ * Output COUNT*ELTSIZE bytes of data at BUF to the descriptor DESC.
+ */
+void
+mywrite (buf, count, eltsize, desc)
+ char *buf;
+ int count;
+ int eltsize;
+ int desc;
+{
+ register int val;
+ register int bytes = count * eltsize;
+
+ while (bytes > 0) {
+ val = write (desc, buf, bytes);
+ if (val <= 0)
+ perror(output_filename);
+ buf += val;
+ bytes -= val;
+ }
+}
+
+/* Output PADDING zero-bytes to descriptor OUTDESC.
+ PADDING may be negative; in that case, do nothing. */
+
+void
+padfile (padding, outdesc)
+ int padding;
+ int outdesc;
+{
+ register char *buf;
+ if (padding <= 0)
+ return;
+
+ buf = (char *) alloca (padding);
+ bzero (buf, padding);
+ mywrite (buf, padding, 1, outdesc);
+}
+#endif
diff --git a/gnu/usr.bin/ld/i386/md-static-funcs.c b/gnu/usr.bin/ld/i386/md-static-funcs.c
new file mode 100644
index 0000000..2cd0768
--- /dev/null
+++ b/gnu/usr.bin/ld/i386/md-static-funcs.c
@@ -0,0 +1,15 @@
+
+/*
+ * Called by ld.so when onanating.
+ * This *must* be a static function, so it is not called through a jmpslot.
+ */
+static void
+md_relocate_simple(r, relocation, addr)
+struct relocation_info *r;
+long relocation;
+char *addr;
+{
+if (r->r_relative)
+ *(long *)addr += relocation;
+}
+
diff --git a/gnu/usr.bin/ld/i386/md.c b/gnu/usr.bin/ld/i386/md.c
new file mode 100644
index 0000000..bfe12e9
--- /dev/null
+++ b/gnu/usr.bin/ld/i386/md.c
@@ -0,0 +1,329 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: md.c,v 1.2 1993/10/27 00:54:58 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+
+#include "ld.h"
+
+/*
+ * Get relocation addend corresponding to relocation record RP
+ * from address ADDR
+ */
+long
+md_get_addend(rp, addr)
+struct relocation_info *rp;
+unsigned char *addr;
+{
+ switch (RELOC_TARGET_SIZE(rp)) {
+ case 0:
+ return get_byte(addr);
+ break;
+ case 1:
+ return get_short(addr);
+ break;
+ case 2:
+ return get_long(addr);
+ break;
+ }
+}
+
+/*
+ * Put RELOCATION at ADDR according to relocation record RP.
+ */
+void
+md_relocate(rp, relocation, addr, relocatable_output)
+struct relocation_info *rp;
+long relocation;
+unsigned char *addr;
+{
+ switch (RELOC_TARGET_SIZE(rp)) {
+ case 0:
+ put_byte(addr, relocation);
+ break;
+ case 1:
+ put_short(addr, relocation);
+ break;
+ case 2:
+ put_long(addr, relocation);
+ break;
+ default:
+ fatal("Unsupported relocation size: %x", RELOC_TARGET_SIZE(rp));
+ }
+}
+
+/*
+ * Initialize (output) exec header such that useful values are
+ * obtained from subsequent N_*() macro evaluations.
+ */
+void
+md_init_header(hp, magic, flags)
+struct exec *hp;
+int magic, flags;
+{
+ N_SETMAGIC((*hp), magic, MID_I386, flags);
+
+ /* TEXT_START depends on the value of outheader.a_entry. */
+ if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
+ hp->a_entry = PAGSIZ;
+}
+
+/*
+ * Machine dependent part of claim_rrs_reloc().
+ * Set RRS relocation type.
+ */
+int
+md_make_reloc(rp, r, type)
+struct relocation_info *rp, *r;
+int type;
+{
+ /* Relocation size */
+ r->r_length = rp->r_length;
+
+ if (RELOC_PCREL_P(rp))
+ r->r_pcrel = 1;
+
+ if (type & RELTYPE_RELATIVE)
+ r->r_relative = 1;
+
+ if (type & RELTYPE_COPY)
+ r->r_copy = 1;
+
+ return 0;
+}
+
+/*
+ * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
+ * to the binder slot (which is at offset 0 of the PLT).
+ */
+void
+md_make_jmpslot(sp, offset, index)
+jmpslot_t *sp;
+long offset;
+long index;
+{
+ /*
+ * i386 PC-relative "fixed point" is located right after the
+ * instruction it pertains to.
+ */
+ u_long fudge = - (sizeof(sp->opcode) + sizeof(sp->addr) + offset);
+
+ sp->opcode = CALL;
+#if 0
+ sp->addr = fudge;
+#else
+ sp->addr[0] = fudge & 0xffff;
+ sp->addr[1] = fudge >> 16;
+#endif
+ sp->reloc_index = index;
+}
+
+/*
+ * Set up a "direct" transfer (ie. not through the run-time binder) from
+ * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
+ * and by `ld.so' after resolving the symbol.
+ * On the i386, we use the JMP instruction which is PC relative, so no
+ * further RRS relocations will be necessary for such a jmpslot.
+ */
+void
+md_fix_jmpslot(sp, offset, addr)
+jmpslot_t *sp;
+long offset;
+u_long addr;
+{
+ u_long fudge = addr - (sizeof(sp->opcode) + sizeof(sp->addr) + offset);
+
+ sp->opcode = JUMP;
+#if 0
+ sp->addr = fudge;
+#else
+ sp->addr[0] = fudge & 0xffff;
+ sp->addr[1] = fudge >> 16;
+#endif
+ sp->reloc_index = 0;
+}
+
+/*
+ * Update the relocation record for a RRS jmpslot.
+ */
+void
+md_make_jmpreloc(rp, r, type)
+struct relocation_info *rp, *r;
+int type;
+{
+ jmpslot_t *sp;
+
+ /*
+ * Fix relocation address to point to the correct
+ * location within this jmpslot.
+ */
+ r->r_address += sizeof(sp->opcode);
+
+ /* Relocation size */
+ r->r_length = 2;
+
+ /* Set relocation type */
+ r->r_jmptable = 1;
+ if (type & RELTYPE_RELATIVE)
+ r->r_relative = 1;
+
+}
+
+/*
+ * Set relocation type for a RRS GOT relocation.
+ */
+void
+md_make_gotreloc(rp, r, type)
+struct relocation_info *rp, *r;
+int type;
+{
+ r->r_baserel = 1;
+ if (type & RELTYPE_RELATIVE)
+ r->r_relative = 1;
+
+ /* Relocation size */
+ r->r_length = 2;
+}
+
+/*
+ * Set relocation type for a RRS copy operation.
+ */
+void
+md_make_cpyreloc(rp, r)
+struct relocation_info *rp, *r;
+{
+ /* Relocation size */
+ r->r_length = 2;
+
+ r->r_copy = 1;
+}
+
+
+#ifdef NEED_SWAP
+
+/*
+ * Byte swap routines for cross-linking.
+ */
+
+void
+md_swapin_exec_hdr(h)
+struct exec *h;
+{
+ int skip = 0;
+
+ if (!N_BADMAG(*h))
+ skip = 1;
+
+ swap_longs((long *)h + skip, sizeof(*h)/sizeof(long) - skip);
+}
+
+void
+md_swapout_exec_hdr(h)
+struct exec *h;
+{
+ /* NetBSD: Always leave magic alone */
+ int skip = 1;
+#if 0
+ if (N_GETMAGIC(*h) == OMAGIC)
+ skip = 0;
+#endif
+
+ swap_longs((long *)h + skip, sizeof(*h)/sizeof(long) - skip);
+}
+
+
+void
+md_swapin_reloc(r, n)
+struct relocation_info *r;
+int n;
+{
+ int bits;
+
+ for (; n; n--, r++) {
+ r->r_address = md_swap_long(r->r_address);
+ bits = ((int *)r)[1];
+ r->r_symbolnum = md_swap_long(bits & 0xffffff00);
+ r->r_pcrel = (bits & 1);
+ r->r_length = ((bits >> 1) & 3);
+ r->r_extern = ((bits >> 3) & 1);
+ r->r_baserel = ((bits >> 4) & 1);
+ r->r_jmptable = ((bits >> 5) & 1);
+ r->r_relative = ((bits >> 6) & 1);
+#ifdef N_SIZE
+ r->r_copy = ((bits >> 7) & 1);
+#endif
+ }
+}
+
+void
+md_swapout_reloc(r, n)
+struct relocation_info *r;
+int n;
+{
+ int bits;
+
+ for (; n; n--, r++) {
+ r->r_address = md_swap_long(r->r_address);
+ bits = (md_swap_long(r->r_symbolnum) & 0xffffff00);
+ bits |= (r->r_pcrel & 1);
+ bits |= ((r->r_length << 1) & 6);
+ bits |= ((r->r_extern << 3) & 8);
+ bits |= ((r->r_baserel << 4) & 0x10);
+ bits |= ((r->r_jmptable << 5) & 0x20);
+ bits |= ((r->r_relative << 6) & 0x40);
+#ifdef N_SIZE
+ bits |= ((r->r_copy << 7) & 0x80);
+#endif
+ ((int *)r)[1] = bits;
+ }
+}
+
+void
+md_swapout_jmpslot(j, n)
+jmpslot_t *j;
+int n;
+{
+ for (; n; n--, j++) {
+ j->opcode = md_swap_short(j->opcode);
+ j->addr[0] = md_swap_short(j->addr[0]);
+ j->addr[1] = md_swap_short(j->addr[1]);
+ j->reloc_index = md_swap_short(j->reloc_index);
+ }
+}
+
+#endif /* NEED_SWAP */
diff --git a/gnu/usr.bin/ld/i386/md.h b/gnu/usr.bin/ld/i386/md.h
new file mode 100644
index 0000000..ecd1815
--- /dev/null
+++ b/gnu/usr.bin/ld/i386/md.h
@@ -0,0 +1,198 @@
+/*
+ * $Id: md.h,v 1.3 1993/10/24 00:52:40 pk Exp $ - I386 dependent definitions
+ */
+
+
+#if defined(CROSS_LINKER) && defined(XHOST) && XHOST==sparc
+
+#define NEED_SWAP
+
+#endif
+
+#define MAX_ALIGNMENT (sizeof (long))
+
+#ifdef NetBSD
+#define PAGSIZ __LDPGSZ
+#else
+#define PAGSIZ 4096
+#endif
+
+#define N_SET_FLAG(ex,f) N_SETMAGIC(ex,N_GETMAGIC(ex), MID_MACHINE, \
+ N_GETFLAG(ex)|(f))
+
+#define N_IS_DYNAMIC(ex) ((N_GETFLAG(ex) & EX_DYNAMIC))
+
+/*
+ * Should be handled by a.out.h ?
+ */
+#define N_ADJUST(ex) (((ex).a_entry < PAGSIZ) ? -PAGSIZ : 0)
+#define TEXT_START(ex) (N_TXTADDR(ex) + N_ADJUST(ex))
+#define DATA_START(ex) (N_DATADDR(ex) + N_ADJUST(ex))
+
+#define RELOC_STATICS_THROUGH_GOT_P(r) (0)
+#define JMPSLOT_NEEDS_RELOC (0)
+
+#define md_got_reloc(r) (0)
+
+#define md_get_rt_segment_addend(r,a) md_get_addend(r,a)
+
+/* Width of a Global Offset Table entry */
+#define GOT_ENTRY_SIZE 4
+typedef long got_t;
+
+typedef struct jmpslot {
+ u_short opcode;
+ u_short addr[2];
+ u_short reloc_index;
+#define JMPSLOT_RELOC_MASK 0xffff
+} jmpslot_t;
+
+#define NOP 0x90
+#define CALL 0xe890 /* NOP + CALL opcode */
+#define JUMP 0xe990 /* NOP + JMP opcode */
+#define TRAP 0xcc /* INT 3 */
+
+/*
+ * Byte swap defs for cross linking
+ */
+
+#if !defined(NEED_SWAP)
+
+#define md_swapin_exec_hdr(h)
+#define md_swapout_exec_hdr(h)
+#define md_swapin_symbols(s,n)
+#define md_swapout_symbols(s,n)
+#define md_swapin_zsymbols(s,n)
+#define md_swapout_zsymbols(s,n)
+#define md_swapin_reloc(r,n)
+#define md_swapout_reloc(r,n)
+#define md_swapin_link_dynamic(l)
+#define md_swapout_link_dynamic(l)
+#define md_swapin_link_dynamic_2(l)
+#define md_swapout_link_dynamic_2(l)
+#define md_swapin_ld_debug(d)
+#define md_swapout_ld_debug(d)
+#define md_swapin_rrs_hash(f,n)
+#define md_swapout_rrs_hash(f,n)
+#define md_swapin_link_object(l,n)
+#define md_swapout_link_object(l,n)
+#define md_swapout_jmpslot(j,n)
+#define md_swapout_got(g,n)
+#define md_swapin_ranlib_hdr(h,n)
+#define md_swapout_ranlib_hdr(h,n)
+
+#endif /* NEED_SWAP */
+
+#ifdef CROSS_LINKER
+
+#ifdef NEED_SWAP
+
+/* Define IO byte swapping routines */
+
+void md_swapin_exec_hdr __P((struct exec *));
+void md_swapout_exec_hdr __P((struct exec *));
+void md_swapin_reloc __P((struct relocation_info *, int));
+void md_swapout_reloc __P((struct relocation_info *, int));
+void md_swapout_jmpslot __P((jmpslot_t *, int));
+
+#define md_swapin_symbols(s,n) swap_symbols(s,n)
+#define md_swapout_symbols(s,n) swap_symbols(s,n)
+#define md_swapin_zsymbols(s,n) swap_zsymbols(s,n)
+#define md_swapout_zsymbols(s,n) swap_zsymbols(s,n)
+#define md_swapin_link_dynamic(l) swap_link_dynamic(l)
+#define md_swapout_link_dynamic(l) swap_link_dynamic(l)
+#define md_swapin_link_dynamic_2(l) swap_link_dynamic_2(l)
+#define md_swapout_link_dynamic_2(l) swap_link_dynamic_2(l)
+#define md_swapin_ld_debug(d) swap_ld_debug(d)
+#define md_swapout_ld_debug(d) swap_ld_debug(d)
+#define md_swapin_rrs_hash(f,n) swap_rrs_hash(f,n)
+#define md_swapout_rrs_hash(f,n) swap_rrs_hash(f,n)
+#define md_swapin_link_object(l,n) swapin_link_object(l,n)
+#define md_swapout_link_object(l,n) swapout_link_object(l,n)
+#define md_swapout_got(g,n) swap_longs((long*)(g),n)
+#define md_swapin_ranlib_hdr(h,n) swap_ranlib_hdr(h,n)
+#define md_swapout_ranlib_hdr(h,n) swap_ranlib_hdr(h,n)
+
+#define md_swap_short(x) ( (((x) >> 8) & 0xff) | (((x) & 0xff) << 8) )
+
+#define md_swap_long(x) ( (((x) >> 24) & 0xff ) | (((x) >> 8 ) & 0xff00 ) | \
+ (((x) << 8 ) & 0xff0000) | (((x) << 24) & 0xff000000))
+
+#define get_byte(p) ( ((unsigned char *)(p))[0] )
+
+#define get_short(p) ( ( ((unsigned char *)(p))[1] << 8) | \
+ ( ((unsigned char *)(p))[0] ) \
+ )
+#define get_long(p) ( ( ((unsigned char *)(p))[3] << 24) | \
+ ( ((unsigned char *)(p))[2] << 16) | \
+ ( ((unsigned char *)(p))[1] << 8 ) | \
+ ( ((unsigned char *)(p))[0] ) \
+ )
+
+#define put_byte(p, v) { ((unsigned char *)(p))[0] = ((unsigned long)(v)); }
+
+#define put_short(p, v) { ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#define put_long(p, v) { ((unsigned char *)(p))[3] = \
+ ((((unsigned long)(v)) >> 24) & 0xff); \
+ ((unsigned char *)(p))[2] = \
+ ((((unsigned long)(v)) >> 16) & 0xff); \
+ ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#else /* We need not swap, but must pay attention to alignment: */
+
+#define md_swap_short(x) (x)
+#define md_swap_long(x) (x)
+
+#define get_byte(p) ( ((unsigned char *)(p))[0] )
+
+#define get_short(p) ( ( ((unsigned char *)(p))[0] << 8) | \
+ ( ((unsigned char *)(p))[1] ) \
+ )
+
+#define get_long(p) ( ( ((unsigned char *)(p))[0] << 24) | \
+ ( ((unsigned char *)(p))[1] << 16) | \
+ ( ((unsigned char *)(p))[2] << 8 ) | \
+ ( ((unsigned char *)(p))[3] ) \
+ )
+
+
+#define put_byte(p, v) { ((unsigned char *)(p))[0] = ((unsigned long)(v)); }
+
+#define put_short(p, v) { ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#define put_long(p, v) { ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) >> 24) & 0xff); \
+ ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) >> 16) & 0xff); \
+ ((unsigned char *)(p))[2] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[3] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#endif /* NEED_SWAP */
+
+#else /* Not a cross linker: use native */
+
+#define md_swap_short(x) (x)
+#define md_swap_long(x) (x)
+
+#define get_byte(where) (*(char *)(where))
+#define get_short(where) (*(short *)(where))
+#define get_long(where) (*(long *)(where))
+
+#define put_byte(where,what) (*(char *)(where) = (what))
+#define put_short(where,what) (*(short *)(where) = (what))
+#define put_long(where,what) (*(long *)(where) = (what))
+
+#endif /* CROSS_LINKER */
+
diff --git a/gnu/usr.bin/ld/i386/mdprologue.S b/gnu/usr.bin/ld/i386/mdprologue.S
new file mode 100644
index 0000000..3b7a6da
--- /dev/null
+++ b/gnu/usr.bin/ld/i386/mdprologue.S
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: mdprologue.S,v 1.1 1993/10/16 21:53:16 pk Exp $
+ */
+
+/*
+ * i386 run-time link editor entry points.
+ */
+
+#include <sys/syscall.h>
+#define LCALL(x,y) .byte 0x9a ; .long y; .word x
+
+ .text
+ .globl _binder, _binder_entry
+
+/*
+ * _rtl(int version, struct crt_ldso *crtp)
+ */
+#define FRAME 12 /* Size of stack frame */
+
+
+_rtl: # crt0 calls us here
+ pushl %ebp # Allocate stack frame
+ movl %esp, %ebp
+ subl $FRAME, %esp
+ pushl %ebx
+ call 1f # PIC function prologue
+1:
+ popl %ebx
+ addl $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx
+
+ movl 12(%ebp), %eax # Extract data from interface structure
+ movl (%eax),%eax # base address of ld.so (first field)
+ # setup arguments for rtld()
+ movl (%ebx), %ecx # 1st entry in GOT is our __DYNAMIC
+ addl %eax, %ecx # add load address
+ pushl %ecx # 3rd arg
+ pushl 12(%ebp) # 2nd arg == &crt.
+ pushl 8(%ebp) # 1st arg == version
+ addl _rtld@GOT(%ebx), %eax # relocate address of function
+ call %eax # _rtld(version, crtp, DYNAMIC)
+ addl $12,%esp # pop arguments
+
+ movl (-FRAME-4)(%ebp), %ebx # restore %ebx
+ leave # remove stack frame,
+ ret # let's rock
+
+ # First call to a procedure generally comes through here for
+ # binding.
+
+_binder_entry:
+ pushl %ebp # setup a stack frame
+ movl %esp, %ebp
+ pusha # save all regs
+
+ movl $0, %eax # clear
+ movl 4(%ebp), %esi # return address in PLT
+ movw (%esi), %ax # get hold of relocation number
+ subl $6, %esi # make it point to the jmpslot
+
+ pushl %eax # pushd arguments
+ pushl %esi #
+ call _binder@PLT # _binder(rpc, index)
+ addl $8, %esp # pop arguments
+ movl %eax, 4(%ebp) # return value from _binder() == actual
+ # address of function
+ popa # restore regs
+ leave # remove our stack frame
+ ret
+
+ # Special system call stubs which return real and effective user and group
+ # id's. Saves overhead of making separate calls for each.
+ # !! Relies on compatability option in BSD 4.three-and-a-half
+
+ .globl _getreuid, _getregid
+_getreuid:
+ lea SYS_getuid, %eax
+ LCALL(7,0)
+ jc out
+ movl 4(%esp), %ecx # get 1st arg
+ movl %eax, (%ecx) # put value in it
+ movl 8(%esp), %ecx # same for 2nd arg
+ movl %edx, (%ecx) #
+ ret # done
+
+_getregid:
+ lea SYS_getgid, %eax
+ LCALL(7,0)
+ jc out
+ movl 4(%esp), %ecx # get 1st arg
+ movl %eax, (%ecx) # put value in it
+ movl 8(%esp), %ecx # same for 2nd arg
+ movl %edx, (%ecx) #
+ ret # done
+
+out: jmp cerror@PLT # Call common error routine
+
diff --git a/gnu/usr.bin/ld/ld.1 b/gnu/usr.bin/ld/ld.1
new file mode 100644
index 0000000..a41714e
--- /dev/null
+++ b/gnu/usr.bin/ld/ld.1
@@ -0,0 +1,192 @@
+.\"
+.\" Copyright (c) 1993 Paul Kranenburg
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by Paul Kranenburg.
+.\" 3. The name of the author may not be used to endorse or promote products
+.\" derived from this software withough specific prior written permission
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $Id: ld.1,v 1.3 1993/11/02 22:56:51 pk Exp $
+.\"
+.Dd October 14, 1993
+.Dt LD 8
+.Os NetBSD 0.9
+.Sh NAME
+.Nm ld
+.Nd link editor
+.Sh SYNOPSIS
+.Nm ld
+.Op Fl MNnrSstXxz
+.Bk -words
+.Op Fl A Ar symbol-file
+.Op Fl assert Ar keyword
+.Op Fl B Ar linkmode
+.Op Fl D Ar datasize
+.Op Fl d Ar c
+.Op Fl d Ar p
+.Op Fl e Ar entry
+.Op Fl l Ar library-specifier
+.Op Fl L Ar library-search-path
+.Op Fl o Ar filename
+.Op Fl T Ar address
+.Op Fl u Ar symbol
+.Op Fl V Ar shlib-version
+.Op Fl y Ar symbol
+.Ek
+.Sh DESCRIPTION
+.Nm
+combines the object and archive files given on the command line into a new
+object file. The output object file is either an executable program, a
+shared object suitable for loading at run-time, or an object file that can
+once again be processed by
+.Nm ld.
+Object files and archives are processed in the order given on the command line.
+.Pp
+The options are as follows:
+.Pp
+.Bl -tag -width indent
+.It Fl A Ar symbol-file
+The the symbol-file is taken as a base for link-editing the object files
+on the command line.
+.It \-assert Ar keyword
+This option has currently no effect. It is here for compatibility with
+SunOS ld. All conditions which would cause a Sun assertion to fail will
+currently always cause error or warning messages from
+.Nm ld\&.
+.It Fl B Ar dynamic
+Specifies that linking against dynamic libraries can take place. If a library
+specifier of the form -lx appears on the command line,
+.Nm ld
+searches for a library of the from libx.so.n.m (see the
+.Ar l
+option) according to the search rules in effect. If such a file can not be
+found a traditional archive is looked for.
+This options can appear anywhere on the command line and is complementary
+to -Bstatic.
+.It Fl B Ar static
+The counterpart of -Bdynamic. This options turns off dynamic linking for
+all library specifiers until a -Bdynamic is once again given. Any explicitly
+mentioned shared object encountered on the command line while this option is
+in effect is flagged as an error.
+.It Fl B Ar shareable
+Instructs the linker to build a shared object from the object files rather
+than a normal executable image.
+.It Fl B Ar symbolic
+This option causes all symbolic references in the output to be resolved in
+this link-edit session. The only remaining run-time relocation requirements are
+.Em base-relative
+relocations, ie. translation with respect to the load address. Failure to
+resolve any symbolic reference causes an error to be reported.
+.It Fl B Ar forcearchive
+Force all members of archives to be loaded, whether or not such members
+contribute a definition to any plain object files. Useful for making a
+shared library from an archive of PIC objects without having to unpack
+the archive.
+.It Fl D Ar data-size
+Set the size of the data segment. For sanity's sake, this should be larger
+than the cumulative data sizes of the input files.
+.It Fl d Ar c
+Force allocation of commons even producing relocatable output.
+.It Fl d Ar p
+Force alias definitions of procedure calls in non-PIC code. Useful to
+obtain shareable code in the presence of run-time relocations as such
+calls will be re-directed through the Procedure Linkage Table (see
+.Xr link 5)
+.It Fl e Ar entry-symbol
+Specifies the entry symbol for an executable.
+.It Fl L Ar path
+Add
+.Ar path
+to the list of directories to search for libraries specified with the
+.Ar -l
+option.
+.It Fl l Ar lib-spec
+This option specifies a library to be considered for inclusion in the
+output. If the -Bdynamic option is in effect, a shared library of the
+form lib<spec>.so.m.n (where
+.Em m
+is the major, and
+.Em n
+is the minor version number, respectively) is searched for first. The
+library with the highest version found in the search path is selected.
+If no shared library is found or the -Bstatic options is in effect,
+an archive of the form lib<spec>.a is looked for in the library seach path.
+.It Fl M
+Produce output about the mapping of segments of the input files and the
+values assigned to (global) symbols in the output file.
+.It Fl N
+Produce a OMAGIC output file.
+.It Fl n
+Produce a NMAGIC output file.
+.It Fl o Ar filename
+Specifies the name of the output file. Defaults to
+.Dq a.out.
+.It Fl r
+Produce relocatable object file, suitable for another pass through
+.Nm ld.
+.It Fl S
+Strip all debugger symbols from the output.
+.It Fl s
+Strip all symbols from the output.
+.It Fl T
+Specifies the start address of the text segment, with respect to which
+all input files will be relocated.
+.It Fl t
+Leave a trace of the input files as they are processed.
+.It Fl u Ar symbol
+Force
+.Ar symbol
+to be marked as undefined. Useful to force loading of an archive member
+in the absence of any other references to that member.
+.It Fl V Ar version
+Put the given version number into the output shared library (if one is
+created). Useful to make shared libaries compatible with other operating
+systems. Eg. SunOS 4.x libraries use version number 3. Defaults to 8.
+.It Fl X
+Discard local symbols in the input files that start with the letter
+.Dq L
+.It Fl x
+Discard all local symbols in the input files.
+.It Fl y symbol
+Trace the manipulations inflicted on
+.Ar symbol
+.It Fl z
+Make a ZMAGIC output file. This is the default.
+.Sh FILES
+.Sh SEE ALSO
+.Xr ldconfig 1 ,
+.Xr link 5
+.Sh BUGS
+Spurious
+.Dq undefined symbols errors
+may be reported for symbols originating in shared libraries. This occurs
+when there is also at least one genuine undefined symbol to report.
+.Sh CAVEATS
+An entry point must now explicitly be given if the output is intended to be
+a normal executable program. This was not the case for the previous version of
+.Nm ld\&.
+.Sh HISTORY
+The shared library model employed by
+.Nm ld
+appeared first in SunOS 4.0
diff --git a/gnu/usr.bin/ld/ld.1aout b/gnu/usr.bin/ld/ld.1aout
new file mode 100644
index 0000000..a41714e
--- /dev/null
+++ b/gnu/usr.bin/ld/ld.1aout
@@ -0,0 +1,192 @@
+.\"
+.\" Copyright (c) 1993 Paul Kranenburg
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by Paul Kranenburg.
+.\" 3. The name of the author may not be used to endorse or promote products
+.\" derived from this software withough specific prior written permission
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $Id: ld.1,v 1.3 1993/11/02 22:56:51 pk Exp $
+.\"
+.Dd October 14, 1993
+.Dt LD 8
+.Os NetBSD 0.9
+.Sh NAME
+.Nm ld
+.Nd link editor
+.Sh SYNOPSIS
+.Nm ld
+.Op Fl MNnrSstXxz
+.Bk -words
+.Op Fl A Ar symbol-file
+.Op Fl assert Ar keyword
+.Op Fl B Ar linkmode
+.Op Fl D Ar datasize
+.Op Fl d Ar c
+.Op Fl d Ar p
+.Op Fl e Ar entry
+.Op Fl l Ar library-specifier
+.Op Fl L Ar library-search-path
+.Op Fl o Ar filename
+.Op Fl T Ar address
+.Op Fl u Ar symbol
+.Op Fl V Ar shlib-version
+.Op Fl y Ar symbol
+.Ek
+.Sh DESCRIPTION
+.Nm
+combines the object and archive files given on the command line into a new
+object file. The output object file is either an executable program, a
+shared object suitable for loading at run-time, or an object file that can
+once again be processed by
+.Nm ld.
+Object files and archives are processed in the order given on the command line.
+.Pp
+The options are as follows:
+.Pp
+.Bl -tag -width indent
+.It Fl A Ar symbol-file
+The the symbol-file is taken as a base for link-editing the object files
+on the command line.
+.It \-assert Ar keyword
+This option has currently no effect. It is here for compatibility with
+SunOS ld. All conditions which would cause a Sun assertion to fail will
+currently always cause error or warning messages from
+.Nm ld\&.
+.It Fl B Ar dynamic
+Specifies that linking against dynamic libraries can take place. If a library
+specifier of the form -lx appears on the command line,
+.Nm ld
+searches for a library of the from libx.so.n.m (see the
+.Ar l
+option) according to the search rules in effect. If such a file can not be
+found a traditional archive is looked for.
+This options can appear anywhere on the command line and is complementary
+to -Bstatic.
+.It Fl B Ar static
+The counterpart of -Bdynamic. This options turns off dynamic linking for
+all library specifiers until a -Bdynamic is once again given. Any explicitly
+mentioned shared object encountered on the command line while this option is
+in effect is flagged as an error.
+.It Fl B Ar shareable
+Instructs the linker to build a shared object from the object files rather
+than a normal executable image.
+.It Fl B Ar symbolic
+This option causes all symbolic references in the output to be resolved in
+this link-edit session. The only remaining run-time relocation requirements are
+.Em base-relative
+relocations, ie. translation with respect to the load address. Failure to
+resolve any symbolic reference causes an error to be reported.
+.It Fl B Ar forcearchive
+Force all members of archives to be loaded, whether or not such members
+contribute a definition to any plain object files. Useful for making a
+shared library from an archive of PIC objects without having to unpack
+the archive.
+.It Fl D Ar data-size
+Set the size of the data segment. For sanity's sake, this should be larger
+than the cumulative data sizes of the input files.
+.It Fl d Ar c
+Force allocation of commons even producing relocatable output.
+.It Fl d Ar p
+Force alias definitions of procedure calls in non-PIC code. Useful to
+obtain shareable code in the presence of run-time relocations as such
+calls will be re-directed through the Procedure Linkage Table (see
+.Xr link 5)
+.It Fl e Ar entry-symbol
+Specifies the entry symbol for an executable.
+.It Fl L Ar path
+Add
+.Ar path
+to the list of directories to search for libraries specified with the
+.Ar -l
+option.
+.It Fl l Ar lib-spec
+This option specifies a library to be considered for inclusion in the
+output. If the -Bdynamic option is in effect, a shared library of the
+form lib<spec>.so.m.n (where
+.Em m
+is the major, and
+.Em n
+is the minor version number, respectively) is searched for first. The
+library with the highest version found in the search path is selected.
+If no shared library is found or the -Bstatic options is in effect,
+an archive of the form lib<spec>.a is looked for in the library seach path.
+.It Fl M
+Produce output about the mapping of segments of the input files and the
+values assigned to (global) symbols in the output file.
+.It Fl N
+Produce a OMAGIC output file.
+.It Fl n
+Produce a NMAGIC output file.
+.It Fl o Ar filename
+Specifies the name of the output file. Defaults to
+.Dq a.out.
+.It Fl r
+Produce relocatable object file, suitable for another pass through
+.Nm ld.
+.It Fl S
+Strip all debugger symbols from the output.
+.It Fl s
+Strip all symbols from the output.
+.It Fl T
+Specifies the start address of the text segment, with respect to which
+all input files will be relocated.
+.It Fl t
+Leave a trace of the input files as they are processed.
+.It Fl u Ar symbol
+Force
+.Ar symbol
+to be marked as undefined. Useful to force loading of an archive member
+in the absence of any other references to that member.
+.It Fl V Ar version
+Put the given version number into the output shared library (if one is
+created). Useful to make shared libaries compatible with other operating
+systems. Eg. SunOS 4.x libraries use version number 3. Defaults to 8.
+.It Fl X
+Discard local symbols in the input files that start with the letter
+.Dq L
+.It Fl x
+Discard all local symbols in the input files.
+.It Fl y symbol
+Trace the manipulations inflicted on
+.Ar symbol
+.It Fl z
+Make a ZMAGIC output file. This is the default.
+.Sh FILES
+.Sh SEE ALSO
+.Xr ldconfig 1 ,
+.Xr link 5
+.Sh BUGS
+Spurious
+.Dq undefined symbols errors
+may be reported for symbols originating in shared libraries. This occurs
+when there is also at least one genuine undefined symbol to report.
+.Sh CAVEATS
+An entry point must now explicitly be given if the output is intended to be
+a normal executable program. This was not the case for the previous version of
+.Nm ld\&.
+.Sh HISTORY
+The shared library model employed by
+.Nm ld
+appeared first in SunOS 4.0
diff --git a/gnu/usr.bin/ld/ld.c b/gnu/usr.bin/ld/ld.c
index f871f00..a22d256 100644
--- a/gnu/usr.bin/ld/ld.c
+++ b/gnu/usr.bin/ld/ld.c
@@ -3,6 +3,8 @@
* Foundation.
*
* Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
+ *
+ * Modified 1993 by Paul Kranenburg, Erasmus University
*/
#ifndef lint
@@ -27,3172 +29,1849 @@ 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.
- Set, indirect, and warning symbol features added by Randy Smith. */
+ Set, indirect, and warning symbol features added by Randy Smith.
+
+ NOTE: Set and indirect symbols are no longer supported by this
+ version. (pk) */
+
+/*
+ * $Id: ld.c,v 1.10 1993/11/01 16:26:13 pk Exp $
+ */
/* Define how to initialize system-dependent header fields. */
-#include <ar.h>
+#include <sys/param.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
+#include <ar.h>
+#include <ranlib.h>
#include <a.out.h>
#include <stab.h>
#include <string.h>
+#include <strings.h>
-/* symseg.h defines the obsolete GNU debugging format; we should nuke it. */
-#define CORE_ADDR unsigned long /* For symseg.h */
-#include "symseg.h"
+#include "ld.h"
-#define N_SET_MAGIC(exec, val) ((exec).a_magic = val)
+int building_shared_object;
-/* If compiled with GNU C, use the built-in alloca */
-#ifdef __GNUC__
-#define alloca __builtin_alloca
-#endif
-
-#define min(a,b) ((a) < (b) ? (a) : (b))
-
-/* Macro to control the number of undefined references printed */
-#define MAX_UREFS_PRINTED 10
+/* 1 => write relocation into output file so can re-input it later. */
+int relocatable_output;
-/* Size of a page; obtained from the operating system. */
+/* Non zero means to create the output executable. */
+/* Cleared by nonfatal errors. */
+int make_executable;
-int page_size;
+/* Force the executable to be output, even if there are non-fatal errors */
+int force_executable;
-/* Name this program was invoked by. */
+/* 1 => assign space to common symbols even if `relocatable_output'. */
+int force_common_definition;
+
+/* 1 => assign jmp slots to text symbols in shared objects even if non-PIC */
+int force_alias_definition;
-char *progname;
-
-/* System dependencies */
+/*
+ * Which symbols should be stripped (omitted from the output): none, all, or
+ * debugger symbols.
+ */
+enum {
+ STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER
+} strip_symbols;
-/* Define this to specify the default executable format. */
+/*
+ * Which local symbols should be omitted: none, all, or those starting with L.
+ * This is irrelevant if STRIP_NONE.
+ */
+enum {
+ DISCARD_NONE, DISCARD_ALL, DISCARD_L
+} discard_locals;
-#ifndef DEFAULT_MAGIC
-#define DEFAULT_MAGIC ZMAGIC
-#endif
+/* Nonzero means print names of input files as processed. */
+int trace_files;
-#ifdef hp300
-#define INITIALIZE_HEADER outheader.a_mid = MID_HP300
-#endif
+/* Magic number to use for the output file, set by switch. */
+int magic;
/*
- * Ok. Following are the relocation information macros. If your
- * system should not be able to use the default set (below), you must
- * define the following:
-
- * relocation_info: This must be typedef'd (or #define'd) to the type
- * of structure that is stored in the relocation info section of your
- * a.out files. Often this is defined in the a.out.h for your system.
- *
- * RELOC_ADDRESS (rval): Offset into the current section of the
- * <whatever> to be relocated. *Must be an lvalue*.
- *
- * RELOC_EXTERN_P (rval): Is this relocation entry based on an
- * external symbol (1), or was it fully resolved upon entering the
- * loader (0) in which case some combination of the value in memory
- * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
- * what the value of the relocation actually was. *Must be an lvalue*.
- *
- * RELOC_TYPE (rval): If this entry was fully resolved upon
- * entering the loader, what type should it be relocated as?
- *
- * RELOC_SYMBOL (rval): If this entry was not fully resolved upon
- * entering the loader, what is the index of it's symbol in the symbol
- * table? *Must be a lvalue*.
- *
- * RELOC_MEMORY_ADD_P (rval): This should return true if the final
- * relocation value output here should be added to memory, or if the
- * section of memory described should simply be set to the relocation
- * value.
- *
- * RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
- * an extra value to be added to the relocation value based on the
- * individual relocation entry. *Must be an lvalue if defined*.
- *
- * RELOC_PCREL_P (rval): True if the relocation value described is
- * pc relative.
- *
- * RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
- * final relocation value before putting it where it belongs.
- *
- * RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
- * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
- * == 1; 4 bytes == 2, and etc. This is somewhat redundant (we could
- * do everything in terms of the bit operators below), but having this
- * macro could end up producing better code on machines without fancy
- * bit twiddling. Also, it's easier to understand/code big/little
- * endian distinctions with this macro.
- *
- * RELOC_TARGET_BITPOS (rval): The starting bit position within the
- * object described in RELOC_TARGET_SIZE in which the relocation value
- * will go.
- *
- * RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
- * with the bits of the relocation value. It may be assumed by the
- * code that the relocation value will fit into this many bits. This
- * may be larger than RELOC_TARGET_SIZE if such be useful.
- *
- *
- * Things I haven't implemented
- * ----------------------------
- *
- * Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
- *
- * Pc relative relocation for External references.
- *
- *
+ * `text-start' address is normally this much plus a page boundary.
+ * This is not a user option; it is fixed for each system.
*/
+int text_start_alignment;
-/* The following #if has been modifed for cross compilation */
-/* It originally read: #if defined(sun) && defined(sparc) */
-/* Marc Ullman, Stanford University Nov. 1 1989 */
-#if defined(sun) && (TARGET == SUN4)
-/* Sparc (Sun 4) macros */
-#undef relocation_info
-#define relocation_info reloc_info_sparc
-#define RELOC_ADDRESS(r) ((r)->r_address)
-#define RELOC_EXTERN_P(r) ((r)->r_extern)
-#define RELOC_TYPE(r) ((r)->r_index)
-#define RELOC_SYMBOL(r) ((r)->r_index)
-#define RELOC_MEMORY_SUB_P(r) 0
-#define RELOC_MEMORY_ADD_P(r) 0
-#define RELOC_ADD_EXTRA(r) ((r)->r_addend)
-#define RELOC_PCREL_P(r) \
- ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
-#define RELOC_VALUE_RIGHTSHIFT(r) (reloc_target_rightshift[(r)->r_type])
-#define RELOC_TARGET_SIZE(r) (reloc_target_size[(r)->r_type])
-#define RELOC_TARGET_BITPOS(r) 0
-#define RELOC_TARGET_BITSIZE(r) (reloc_target_bitsize[(r)->r_type])
-
-/* Note that these are very dependent on the order of the enums in
- enum reloc_type (in a.out.h); if they change the following must be
- changed */
-/* Also note that the last few may be incorrect; I have no information */
-static int reloc_target_rightshift[] = {
- 0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
-};
-static int reloc_target_size[] = {
- 0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
-};
-static int reloc_target_bitsize[] = {
- 8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
-};
-
-#define MAX_ALIGNMENT (sizeof (double))
-#endif
-
-/* Default macros */
-#ifndef RELOC_ADDRESS
-#define RELOC_ADDRESS(r) ((r)->r_address)
-#define RELOC_EXTERN_P(r) ((r)->r_extern)
-#define RELOC_TYPE(r) ((r)->r_symbolnum)
-#define RELOC_SYMBOL(r) ((r)->r_symbolnum)
-#define RELOC_MEMORY_SUB_P(r) 0
-#define RELOC_MEMORY_ADD_P(r) 1
-#undef RELOC_ADD_EXTRA
-#define RELOC_PCREL_P(r) ((r)->r_pcrel)
-#define RELOC_VALUE_RIGHTSHIFT(r) 0
-#define RELOC_TARGET_SIZE(r) ((r)->r_length)
-#define RELOC_TARGET_BITPOS(r) 0
-#define RELOC_TARGET_BITSIZE(r) 32
-#endif
+/*
+ * Nonzero if -T was specified in the command line.
+ * This prevents text_start from being set later to default values.
+ */
+int T_flag_specified;
+
+/*
+ * Nonzero if -Tdata was specified in the command line.
+ * This prevents data_start from being set later to default values.
+ */
+int Tdata_flag_specified;
-#ifndef MAX_ALIGNMENT
-#define MAX_ALIGNMENT (sizeof (int))
-#endif
+/*
+ * Size to pad data section up to.
+ * We simply increase the size of the data section, padding with zeros,
+ * and reduce the size of the bss section to match.
+ */
+int specified_data_size;
-#ifdef nounderscore
-#define LPREFIX '.'
-#else
-#define LPREFIX 'L'
-#endif
+long *set_vectors;
+int setv_fill_count;
-#ifndef TEXT_START
-#define TEXT_START(x) N_TXTADDR(x)
-#endif
-
-/* Special global symbol types understood by GNU LD. */
+int
+main(argc, argv)
+ char **argv;
+ int argc;
+{
-/* The following type indicates the definition of a symbol as being
- an indirect reference to another symbol. The other symbol
- appears as an undefined reference, immediately following this symbol.
+ if ((progname = strrchr(argv[0], '/')) == NULL)
+ progname = argv[0];
+ else
+ progname++;
- Indirection is asymmetrical. The other symbol's value will be used
- to satisfy requests for the indirect symbol, but not vice versa.
- If the other symbol does not have a definition, libraries will
- be searched to find a definition.
+ /* Added this to stop ld core-dumping on very large .o files. */
+#ifdef RLIMIT_STACK
+ /* Get rid of any avoidable limit on stack size. */
+ {
+ struct rlimit rlim;
- So, for example, the following two lines placed in an assembler
- input file would result in an object file which would direct gnu ld
- to resolve all references to symbol "foo" as references to symbol
- "bar".
+ /* Set the stack limit huge so that alloca does not fail. */
+ getrlimit(RLIMIT_STACK, &rlim);
+ rlim.rlim_cur = rlim.rlim_max;
+ setrlimit(RLIMIT_STACK, &rlim);
+ }
+#endif /* RLIMIT_STACK */
+
+ page_size = PAGSIZ;
+
+ /* Clear the cumulative info on the output file. */
+
+ text_size = 0;
+ data_size = 0;
+ bss_size = 0;
+ text_reloc_size = 0;
+ data_reloc_size = 0;
- .stabs "_foo",11,0,0,0
- .stabs "_bar",1,0,0,0
+ data_pad = 0;
+ text_pad = 0;
- Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)). */
+ /* Initialize the data about options. */
-#ifndef N_INDR
-#define N_INDR 0xa
-#endif
+ specified_data_size = 0;
+ strip_symbols = STRIP_NONE;
+ trace_files = 0;
+ discard_locals = DISCARD_NONE;
+ entry_symbol = 0;
+ write_map = 0;
+ relocatable_output = 0;
+ force_common_definition = 0;
+ T_flag_specified = 0;
+ Tdata_flag_specified = 0;
+ magic = DEFAULT_MAGIC;
+ make_executable = 1;
+ force_executable = 0;
+ link_mode = DYNAMIC;
+ soversion = LD_VERSION_BSD;
-/* The following symbols refer to set elements. These are expected
- only in input to the loader; they should not appear in loader
- output (unless relocatable output is requested). To be recognized
- by the loader, the input symbols must have their N_EXT bit set.
- All the N_SET[ATDB] symbols with the same name form one set. The
- loader collects all of these elements at load time and outputs a
- vector for each name.
- Space (an array of 32 bit words) is allocated for the set in the
- data section, and the n_value field of each set element value is
- stored into one word of the array.
- The first word of the array is the length of the set (number of
- elements). The last word of the vector is set to zero for possible
- use by incremental loaders. The array is ordered by the linkage
- order; the first symbols which the linker encounters will be first
- in the array.
-
- In C syntax this looks like:
-
- struct set_vector {
- unsigned int length;
- unsigned int vector[length];
- unsigned int always_zero;
- };
-
- Before being placed into the array, each element is relocated
- according to its type. This allows the loader to create an array
- of pointers to objects automatically. N_SETA type symbols will not
- be relocated.
-
- The address of the set is made into an N_SETV symbol
- whose name is the same as the name of the set.
- This symbol acts like a N_DATA global symbol
- in that it can satisfy undefined external references.
-
- For the purposes of determining whether or not to load in a library
- file, set element definitions are not considered "real
- definitions"; they will not cause the loading of a library
- member.
-
- If relocatable output is requested, none of this processing is
- done. The symbols are simply relocated and passed through to the
- output file.
-
- So, for example, the following three lines of assembler code
- (whether in one file or scattered between several different ones)
- will produce a three element vector (total length is five words;
- see above), referenced by the symbol "_xyzzy", which will have the
- addresses of the routines _init1, _init2, and _init3.
-
- *NOTE*: If symbolic addresses are used in the n_value field of the
- defining .stabs, those symbols must be defined in the same file as
- that containing the .stabs.
-
- .stabs "_xyzzy",23,0,0,_init1
- .stabs "_xyzzy",23,0,0,_init2
- .stabs "_xyzzy",23,0,0,_init3
-
- Note that (23 == (N_SETT | N_EXT)). */
-
-#ifndef N_SETA
-#define N_SETA 0x14 /* Absolute set element symbol */
-#endif /* This is input to LD, in a .o file. */
-
-#ifndef N_SETT
-#define N_SETT 0x16 /* Text set element symbol */
-#endif /* This is input to LD, in a .o file. */
-
-#ifndef N_SETD
-#define N_SETD 0x18 /* Data set element symbol */
-#endif /* This is input to LD, in a .o file. */
-
-#ifndef N_SETB
-#define N_SETB 0x1A /* Bss set element symbol */
-#endif /* This is input to LD, in a .o file. */
-
-/* Macros dealing with the set element symbols defined in a.out.h */
-#define SET_ELEMENT_P(x) ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
-#define TYPE_OF_SET_ELEMENT(x) ((x)-N_SETA+N_ABS)
-
-#ifndef N_SETV
-#define N_SETV 0x1C /* Pointer to set vector in data area. */
-#endif /* This is output from LD. */
-
-/* If a this type of symbol is encountered, its name is a warning
- message to print each time the symbol referenced by the next symbol
- table entry is referenced.
-
- This feature may be used to allow backwards compatibility with
- certain functions (eg. gets) but to discourage programmers from
- their use.
-
- So if, for example, you wanted to have ld print a warning whenever
- the function "gets" was used in their C program, you would add the
- following to the assembler file in which gets is defined:
-
- .stabs "Obsolete function \"gets\" referenced",30,0,0,0
- .stabs "_gets",1,0,0,0
-
- These .stabs do not necessarily have to be in the same file as the
- gets function, they simply must exist somewhere in the compilation. */
-
-#ifndef N_WARNING
-#define N_WARNING 0x1E /* Warning message to print if symbol
- included */
-#endif /* This is input to ld */
-
-#ifndef __GNU_STAB__
-
-/* Line number for the data section. This is to be used to describe
- the source location of a variable declaration. */
-#ifndef N_DSLINE
-#define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
-#endif
+ /* Initialize the cumulative counts of symbols. */
-/* Line number for the bss section. This is to be used to describe
- the source location of a variable declaration. */
-#ifndef N_BSLINE
-#define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
-#endif
+ local_sym_count = 0;
+ non_L_local_sym_count = 0;
+ debugger_sym_count = 0;
+ undefined_global_sym_count = 0;
+ warning_count = 0;
+ multiple_def_count = 0;
+ common_defined_global_count = 0;
-#endif /* not __GNU_STAB__ */
-
-/* Symbol table */
-
-/* Global symbol data is recorded in these structures,
- one for each global symbol.
- They are found via hashing in 'symtab', which points to a vector of buckets.
- Each bucket is a chain of these structures through the link field. */
-
-typedef
- struct glosym
- {
- /* Pointer to next symbol in this symbol's hash bucket. */
- struct glosym *link;
- /* Name of this symbol. */
- char *name;
- /* Value of this symbol as a global symbol. */
- long value;
- /* Chain of external 'nlist's in files for this symbol, both defs
- and refs. */
- struct nlist *refs;
- /* Any warning message that might be associated with this symbol
- from an N_WARNING symbol encountered. */
- char *warning;
- /* Nonzero means definitions of this symbol as common have been seen,
- and the value here is the largest size specified by any of them. */
- int max_common_size;
- /* For relocatable_output, records the index of this global sym in the
- symbol table to be written, with the first global sym given index 0.*/
- int def_count;
- /* Nonzero means a definition of this global symbol is known to exist.
- Library members should not be loaded on its account. */
- char defined;
- /* Nonzero means a reference to this global symbol has been seen
- in a file that is surely being loaded.
- A value higher than 1 is the n_type code for the symbol's
- definition. */
- char referenced;
- /* A count of the number of undefined references printed for a
- specific symbol. If a symbol is unresolved at the end of
- digest_symbols (and the loading run is supposed to produce
- relocatable output) do_file_warnings keeps track of how many
- unresolved reference error messages have been printed for
- each symbol here. When the number hits MAX_UREFS_PRINTED,
- messages stop. */
- unsigned char undef_refs;
- /* 1 means that this symbol has multiple definitions. 2 means
- that it has multiple definitions, and some of them are set
- elements, one of which has been printed out already. */
- unsigned char multiply_defined;
- /* Nonzero means print a message at all refs or defs of this symbol */
- char trace;
- }
- symbol;
-
-/* Demangler for C++. */
-extern char *cplus_demangle ();
-
-/* Demangler function to use. */
-char *(*demangler)() = NULL;
-
-/* Number of buckets in symbol hash table */
-#define TABSIZE 1009
-
-/* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
-symbol *symtab[TABSIZE];
-
-/* Number of symbols in symbol hash table. */
-int num_hash_tab_syms = 0;
-
-/* Count the number of nlist entries that are for local symbols.
- This count and the three following counts
- are incremented as as symbols are entered in the symbol table. */
-int local_sym_count;
-
-/* Count number of nlist entries that are for local symbols
- whose names don't start with L. */
-int non_L_local_sym_count;
-
-/* Count the number of nlist entries for debugger info. */
-int debugger_sym_count;
-
-/* Count the number of global symbols referenced and not defined. */
-int undefined_global_sym_count;
-
-/* Count the number of global symbols multiply defined. */
-int multiple_def_count;
-
-/* Count the number of defined global symbols.
- Each symbol is counted only once
- regardless of how many different nlist entries refer to it,
- since the output file will need only one nlist entry for it.
- This count is computed by `digest_symbols';
- it is undefined while symbols are being loaded. */
-int defined_global_sym_count;
-
-/* Count the number of symbols defined through common declarations.
- This count is kept in symdef_library, linear_library, and
- enter_global_ref. It is incremented when the defined flag is set
- in a symbol because of a common definition, and decremented when
- the symbol is defined "for real" (ie. by something besides a common
- definition). */
-int common_defined_global_count;
-
-/* Count the number of set element type symbols and the number of
- separate vectors which these symbols will fit into. See the
- GNU a.out.h for more info.
- This count is computed by 'enter_file_symbols' */
-int set_symbol_count;
-int set_vector_count;
-
-/* Define a linked list of strings which define symbols which should
- be treated as set elements even though they aren't. Any symbol
- with a prefix matching one of these should be treated as a set
- element.
-
- This is to make up for deficiencies in many assemblers which aren't
- willing to pass any stabs through to the loader which they don't
- understand. */
-struct string_list_element {
- char *str;
- struct string_list_element *next;
-};
-
-struct string_list_element *set_element_prefixes;
-
-/* Count the number of definitions done indirectly (ie. done relative
- to the value of some other symbol. */
-int global_indirect_count;
-
-/* Count the number of warning symbols encountered. */
-int warning_count;
-
-/* Total number of symbols to be written in the output file.
- Computed by digest_symbols from the variables above. */
-int nsyms;
-
-
-/* Nonzero means ptr to symbol entry for symbol to use as start addr.
- -e sets this. */
-symbol *entry_symbol;
-
-symbol *edata_symbol; /* the symbol _edata */
-symbol *etext_symbol; /* the symbol _etext */
-symbol *end_symbol; /* the symbol _end */
-
-/* Each input file, and each library member ("subfile") being loaded,
- has a `file_entry' structure for it.
-
- For files specified by command args, these are contained in the vector
- which `file_table' points to.
-
- For library members, they are dynamically allocated,
- and chained through the `chain' field.
- The chain is found in the `subfiles' field of the `file_entry'.
- The `file_entry' objects for the members have `superfile' fields pointing
- to the one for the library. */
-
-struct file_entry {
- /* Name of this file. */
- char *filename;
- /* Name to use for the symbol giving address of text start */
- /* Usually the same as filename, but for a file spec'd with -l
- this is the -l switch itself rather than the filename. */
- char *local_sym_name;
-
- /* Describe the layout of the contents of the file */
-
- /* The file's a.out header. */
- struct exec header;
- /* Offset in file of GDB symbol segment, or 0 if there is none. */
- int symseg_offset;
-
- /* Describe data from the file loaded into core */
-
- /* Symbol table of the file. */
- struct nlist *symbols;
- /* Size in bytes of string table. */
- int string_size;
- /* Pointer to the string table.
- The string table is not kept in core all the time,
- but when it is in core, its address is here. */
- char *strings;
-
- /* Next two used only if `relocatable_output' or if needed for */
- /* output of undefined reference line numbers. */
-
- /* Text reloc info saved by `write_text' for `coptxtrel'. */
- struct relocation_info *textrel;
- /* Data reloc info saved by `write_data' for `copdatrel'. */
- struct relocation_info *datarel;
-
- /* Relation of this file's segments to the output file */
-
- /* Start of this file's text seg in the output file core image. */
- int text_start_address;
- /* Start of this file's data seg in the output file core image. */
- int data_start_address;
- /* Start of this file's bss seg in the output file core image. */
- int bss_start_address;
- /* Offset in bytes in the output file symbol table
- of the first local symbol for this file. Set by `write_file_symbols'. */
- int local_syms_offset;
-
- /* For library members only */
-
- /* For a library, points to chain of entries for the library members. */
- struct file_entry *subfiles;
- /* For a library member, offset of the member within the archive.
- Zero for files that are not library members. */
- int starting_offset;
- /* Size of contents of this file, if library member. */
- int total_size;
- /* For library member, points to the library's own entry. */
- struct file_entry *superfile;
- /* For library member, points to next entry for next member. */
- struct file_entry *chain;
-
- /* 1 if file is a library. */
- char library_flag;
-
- /* 1 if file's header has been read into this structure. */
- char header_read_flag;
-
- /* 1 means search a set of directories for this file. */
- char search_dirs_flag;
-
- /* 1 means this is base file of incremental load.
- Do not load this file's text or data.
- Also default text_start to after this file's bss. */
- char just_syms_flag;
-};
-
-/* Vector of entries for input files specified by arguments.
- These are all the input files except for members of specified libraries. */
-struct file_entry *file_table;
-
-/* Length of that vector. */
-int number_of_files;
-
-/* When loading the text and data, we can avoid doing a close
- and another open between members of the same library.
+ /* Keep a list of symbols referenced from the command line */
+ cl_refs_allocated = 10;
+ cmdline_references
+ = (struct glosym **) xmalloc(cl_refs_allocated
+ * sizeof(struct glosym *));
+ *cmdline_references = 0;
- These two variables remember the file that is currently open.
- Both are zero if no file is open.
+ /* Completely decode ARGV. */
+ decode_command(argc, argv);
- See `each_file' and `file_close'. */
+ building_shared_object =
+ (!relocatable_output && (link_mode & SHAREABLE));
-struct file_entry *input_file;
-int input_desc;
+ /* Create the symbols `etext', `edata' and `end'. */
+ symtab_init(relocatable_output);
-/* The name of the file to write; "a.out" by default. */
+ /* Prepare for the run-time linking support. */
+ init_rrs();
-char *output_filename;
+ /*
+ * Determine whether to count the header as part of the text size,
+ * and initialize the text size accordingly. This depends on the kind
+ * of system and on the output format selected.
+ */
-/* Descriptor for writing that file with `mywrite'. */
+ md_init_header(&outheader, magic, 0);
-int outdesc;
+ text_size = sizeof(struct exec);
+ text_size -= N_TXTOFF(outheader);
-/* Header for that file (filled in by `write_header'). */
+ if (text_size < 0)
+ text_size = 0;
+ entry_offset = text_size;
-struct exec outheader;
+ if (!T_flag_specified && !relocatable_output)
+ text_start = TEXT_START(outheader);
-#ifdef COFF_ENCAPSULATE
-struct coffheader coffheader;
-int need_coff_header;
-#endif
+ /* The text-start address is normally this far past a page boundary. */
+ text_start_alignment = text_start % page_size;
-/* The following are computed by `digest_symbols'. */
+ /*
+ * Load symbols of all input files. Also search all libraries and
+ * decide which library members to load.
+ */
+ load_symbols();
-int text_size; /* total size of text of all input files. */
-int data_size; /* total size of data of all input files. */
-int bss_size; /* total size of bss of all input files. */
-int text_reloc_size; /* total size of text relocation of all input files. */
-int data_reloc_size; /* total size of data relocation of all input */
- /* files. */
+ /* Compute where each file's sections go, and relocate symbols. */
+ digest_symbols();
-/* Specifications of start and length of the area reserved at the end
- of the text segment for the set vectors. Computed in 'digest_symbols' */
-int set_sect_start;
-int set_sect_size;
+ /*
+ * Print error messages for any missing symbols, for any warning
+ * symbols, and possibly multiple definitions
+ */
+ make_executable = do_warnings(stderr);
-/* Pointer for in core storage for the above vectors, before they are
- written. */
-unsigned long *set_vectors;
+ /* Print a map, if requested. */
+ if (write_map)
+ print_symbols(stdout);
-/* Amount of cleared space to leave between the text and data segments. */
+ /* Write the output file. */
+ if (make_executable || force_executable)
+ write_output();
-int text_pad;
+ exit(!make_executable);
+}
-/* Amount of bss segment to include as part of the data segment. */
+void decode_option();
-int data_pad;
+/*
+ * Analyze a command line argument. Return 0 if the argument is a filename.
+ * Return 1 if the argument is a option complete in itself. Return 2 if the
+ * argument is a option which uses an argument.
+ *
+ * Thus, the value is the number of consecutive arguments that are part of
+ * options.
+ */
-/* Format of __.SYMDEF:
- First, a longword containing the size of the 'symdef' data that follows.
- Second, zero or more 'symdef' structures.
- Third, a longword containing the length of symbol name strings.
- Fourth, zero or more symbol name strings (each followed by a null). */
+int
+classify_arg(arg)
+ register char *arg;
+{
+ if (*arg != '-')
+ return 0;
+ switch (arg[1]) {
+ case 'a':
+ if (!strcmp(&arg[2], "ssert"))
+ return 2;
+ case 'A':
+ case 'D':
+ case 'e':
+ case 'L':
+ case 'l':
+ case 'o':
+ case 'u':
+ case 'V':
+ case 'y':
+ if (arg[2])
+ return 1;
+ return 2;
+
+ case 'B':
+ if (!strcmp(&arg[2], "static"))
+ return 1;
+ if (!strcmp(&arg[2], "dynamic"))
+ return 1;
+
+ case 'T':
+ if (arg[2] == 0)
+ return 2;
+ if (!strcmp(&arg[2], "text"))
+ return 2;
+ if (!strcmp(&arg[2], "data"))
+ return 2;
+ return 1;
+ }
-struct symdef {
- int symbol_name_string_index;
- int library_member_offset;
-};
-
-/* Record most of the command options. */
+ return 1;
+}
-/* Address we assume the text section will be loaded at.
- We relocate symbols and text and data for this, but we do not
- write any padding in the output file for it. */
-int text_start;
+/*
+ * Process the command arguments, setting up file_table with an entry for
+ * each input file, and setting variables according to the options.
+ */
-/* Offset of default entry-pc within the text section. */
-int entry_offset;
+void
+decode_command(argc, argv)
+ char **argv;
+ int argc;
+{
+ register int i;
+ register struct file_entry *p;
+ char *cp;
-/* Address we decide the data section will be loaded at. */
-int data_start;
+ number_of_files = 0;
+ output_filename = "a.out";
-/* `text-start' address is normally this much plus a page boundary.
- This is not a user option; it is fixed for each system. */
-int text_start_alignment;
+ /*
+ * First compute number_of_files so we know how long to make
+ * file_table.
+ * Also process most options completely.
+ */
-/* Nonzero if -T was specified in the command line.
- This prevents text_start from being set later to default values. */
-int T_flag_specified;
+ for (i = 1; i < argc; i++) {
+ register int code = classify_arg(argv[i]);
+ if (code) {
+ if (i + code > argc)
+ fatal("no argument following %s\n", argv[i]);
-/* Nonzero if -Tdata was specified in the command line.
- This prevents data_start from being set later to default values. */
-int Tdata_flag_specified;
+ decode_option(argv[i], argv[i + 1]);
-/* Size to pad data section up to.
- We simply increase the size of the data section, padding with zeros,
- and reduce the size of the bss section to match. */
-int specified_data_size;
+ if (argv[i][1] == 'l' || argv[i][1] == 'A')
+ number_of_files++;
-/* Magic number to use for the output file, set by switch. */
-int magic;
+ i += code - 1;
+ } else
+ number_of_files++;
+ }
-/* Nonzero means print names of input files as processed. */
-int trace_files;
+ if (!number_of_files)
+ fatal("no input files");
-/* Which symbols should be stripped (omitted from the output):
- none, all, or debugger symbols. */
-enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
+ p = file_table = (struct file_entry *)
+ xmalloc(number_of_files * sizeof(struct file_entry));
+ bzero(p, number_of_files * sizeof(struct file_entry));
-/* Which local symbols should be omitted:
- none, all, or those starting with L.
- This is irrelevant if STRIP_NONE. */
-enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
+ /* Now scan again and fill in file_table. */
+ /* All options except -A and -l are ignored here. */
-/* 1 => write load map. */
-int write_map;
+ for (i = 1; i < argc; i++) {
+ char *string;
+ register int code = classify_arg(argv[i]);
-/* 1 => write relocation into output file so can re-input it later. */
-int relocatable_output;
+ if (code == 0) {
+ p->filename = argv[i];
+ p->local_sym_name = argv[i];
+ p++;
+ continue;
+ }
+ if (code == 2)
+ string = argv[i + 1];
+ else
+ string = &argv[i][2];
+
+ if (argv[i][1] == 'B') {
+ if (strcmp(string, "static") == 0)
+ link_mode &= ~DYNAMIC;
+ else if (strcmp(string, "dynamic") == 0)
+ link_mode |= DYNAMIC;
+ else if (strcmp(string, "symbolic") == 0)
+ link_mode |= SYMBOLIC;
+ else if (strcmp(string, "forcearchive") == 0)
+ link_mode |= FORCEARCHIVE;
+ else if (strcmp(string, "shareable") == 0)
+ link_mode |= SHAREABLE;
+ }
+ if (argv[i][1] == 'A') {
+ if (p != file_table)
+ fatal("-A specified before an input file other than the first");
+ p->filename = string;
+ p->local_sym_name = string;
+ p->just_syms_flag = 1;
+ p++;
+ }
+ if (argv[i][1] == 'l') {
+ p->filename = string;
+ p->local_sym_name = concat("-l", string, "");
+ p->search_dirs_flag = 1;
+ if (link_mode & DYNAMIC && !relocatable_output)
+ p->search_dynamic_flag = 1;
+ p++;
+ }
+ i += code - 1;
+ }
-/* 1 => assign space to common symbols even if `relocatable_output'. */
-int force_common_definition;
-
-/* Standard directories to search for files specified by -l. */
-char *standard_search_dirs[] =
-#ifdef STANDARD_SEARCH_DIRS
- {STANDARD_SEARCH_DIRS};
-#else
-#ifdef NON_NATIVE
- {"/usr/local/lib/gnu"};
-#else
- {"/lib", "/usr/lib", "/usr/local/lib"};
-#endif
-#endif
+ /* Now check some option settings for consistency. */
-/* Actual vector of directories to search;
- this contains those specified with -L plus the standard ones. */
-char **search_dirs;
+ if ((magic != OMAGIC)
+ && (text_start - text_start_alignment) & (page_size - 1))
+ fatal("-T argument not multiple of page size, with sharable output");
-/* Length of the vector `search_dirs'. */
-int n_search_dirs;
+ /* Append the standard search directories to the user-specified ones. */
+ std_search_dirs(getenv("LD_LIBRARY_PATH"));
+}
-/* Non zero means to create the output executable. */
-/* Cleared by nonfatal errors. */
-int make_executable;
-
-/* Force the executable to be output, even if there are non-fatal
- errors */
-int force_executable;
-
-/* Keep a list of any symbols referenced from the command line (so
- that error messages for these guys can be generated). This list is
- zero terminated. */
-struct glosym **cmdline_references;
-int cl_refs_allocated;
-
-void bcopy (), bzero ();
-int malloc (), realloc ();
-#ifndef alloca
-int alloca ();
-#endif
-int free ();
-
-int xmalloc ();
-int xrealloc ();
-void fatal ();
-void fatal_with_file ();
-void perror_name ();
-void perror_file ();
-void error ();
-
-void digest_symbols ();
-void print_symbols ();
-void load_symbols ();
-void decode_command ();
-void list_undefined_symbols ();
-void list_unresolved_references ();
-void write_output ();
-void write_header ();
-void write_text ();
-void read_file_relocation ();
-void write_data ();
-void write_rel ();
-void write_syms ();
-void write_symsegs ();
-void mywrite ();
-void symtab_init ();
-void padfile ();
-char *concat ();
-char *get_file_name ();
-symbol *getsym (), *getsym_soft ();
-
-int
-main (argc, argv)
- char **argv;
- int argc;
+void
+add_cmdline_ref(sp)
+ struct glosym *sp;
{
-/* Added this to stop ld core-dumping on very large .o files. */
-#ifdef RLIMIT_STACK
- /* Get rid of any avoidable limit on stack size. */
- {
- struct rlimit rlim;
-
- /* Set the stack limit huge so that alloca does not fail. */
- getrlimit (RLIMIT_STACK, &rlim);
- rlim.rlim_cur = rlim.rlim_max;
- setrlimit (RLIMIT_STACK, &rlim);
- }
-#endif /* RLIMIT_STACK */
-
- page_size = getpagesize ();
- progname = argv[0];
-
- /* Clear the cumulative info on the output file. */
-
- text_size = 0;
- data_size = 0;
- bss_size = 0;
- text_reloc_size = 0;
- data_reloc_size = 0;
-
- data_pad = 0;
- text_pad = 0;
-
- /* Initialize the data about options. */
-
- specified_data_size = 0;
- strip_symbols = STRIP_NONE;
- trace_files = 0;
- discard_locals = DISCARD_NONE;
- entry_symbol = 0;
- write_map = 0;
- relocatable_output = 0;
- force_common_definition = 0;
- T_flag_specified = 0;
- Tdata_flag_specified = 0;
- magic = DEFAULT_MAGIC;
- make_executable = 1;
- force_executable = 0;
- set_element_prefixes = 0;
-
- /* Initialize the cumulative counts of symbols. */
-
- local_sym_count = 0;
- non_L_local_sym_count = 0;
- debugger_sym_count = 0;
- undefined_global_sym_count = 0;
- set_symbol_count = 0;
- set_vector_count = 0;
- global_indirect_count = 0;
- warning_count = 0;
- multiple_def_count = 0;
- common_defined_global_count = 0;
-
- /* Keep a list of symbols referenced from the command line */
- cl_refs_allocated = 10;
- cmdline_references
- = (struct glosym **) xmalloc (cl_refs_allocated
- * sizeof(struct glosym *));
- *cmdline_references = 0;
-
- /* Completely decode ARGV. */
-
- decode_command (argc, argv);
-
- /* Create the symbols `etext', `edata' and `end'. */
-
- if (!relocatable_output)
- symtab_init ();
-
- /* Determine whether to count the header as part of
- the text size, and initialize the text size accordingly.
- This depends on the kind of system and on the output format selected. */
-
- N_SET_MAGIC (outheader, magic);
-#ifdef INITIALIZE_HEADER
- INITIALIZE_HEADER;
-#endif
-
- text_size = sizeof (struct exec);
-#ifdef COFF_ENCAPSULATE
- if (relocatable_output == 0 && file_table[0].just_syms_flag == 0)
- {
- need_coff_header = 1;
- /* set this flag now, since it will change the values of N_TXTOFF, etc */
- N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
- text_size += sizeof (struct coffheader);
- }
-#endif
-
- text_size -= N_TXTOFF (outheader);
-
- if (text_size < 0)
- text_size = 0;
- entry_offset = text_size;
-
- if (!T_flag_specified && !relocatable_output)
- text_start = TEXT_START (outheader);
-
- /* The text-start address is normally this far past a page boundary. */
- text_start_alignment = text_start % page_size;
-
- /* Load symbols of all input files.
- Also search all libraries and decide which library members to load. */
-
- load_symbols ();
-
- /* Compute where each file's sections go, and relocate symbols. */
-
- digest_symbols ();
+ struct glosym **ptr;
- /* Print error messages for any missing symbols, for any warning
- symbols, and possibly multiple definitions */
+ for (ptr = cmdline_references;
+ ptr < cmdline_references + cl_refs_allocated && *ptr;
+ ptr++);
- do_warnings (stderr);
+ if (ptr >= cmdline_references + cl_refs_allocated - 1) {
+ int diff = ptr - cmdline_references;
- /* Print a map, if requested. */
-
- if (write_map) print_symbols (stdout);
-
- /* Write the output file. */
-
- if (make_executable || force_executable)
- write_output ();
-
- exit (!make_executable);
+ cl_refs_allocated *= 2;
+ cmdline_references = (struct glosym **)
+ xrealloc(cmdline_references,
+ cl_refs_allocated * sizeof(struct glosym *));
+ ptr = cmdline_references + diff;
+ }
+ *ptr++ = sp;
+ *ptr = (struct glosym *) 0;
}
-
-void decode_option ();
-
-/* Analyze a command line argument.
- Return 0 if the argument is a filename.
- Return 1 if the argument is a option complete in itself.
- Return 2 if the argument is a option which uses an argument.
-
- Thus, the value is the number of consecutive arguments
- that are part of options. */
int
-classify_arg (arg)
- register char *arg;
+set_element_prefixed_p(name)
+ char *name;
{
- if (*arg != '-') return 0;
- switch (arg[1])
- {
- case 'A':
- case 'D':
- case 'e':
- case 'L':
- case 'l':
- case 'o':
- case 'u':
- case 'V':
- case 'y':
- if (arg[2])
- return 1;
- return 2;
+ struct string_list_element *p;
+ int i;
- case 'B':
- if (! strcmp (&arg[2], "static"))
- return 1;
+ for (p = set_element_prefixes; p; p = p->next) {
- case 'T':
- if (arg[2] == 0)
- return 2;
- if (! strcmp (&arg[2], "text"))
- return 2;
- if (! strcmp (&arg[2], "data"))
- return 2;
- return 1;
- }
-
- return 1;
+ for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++);
+ if (p->str[i] == '\0')
+ return 1;
+ }
+ return 0;
}
-/* Process the command arguments,
- setting up file_table with an entry for each input file,
- and setting variables according to the options. */
+/*
+ * Record an option and arrange to act on it later. ARG should be the
+ * following command argument, which may or may not be used by this option.
+ *
+ * The `l' and `A' options are ignored here since they actually specify input
+ * files.
+ */
void
-decode_command (argc, argv)
- char **argv;
- int argc;
-{
- register int i;
- register struct file_entry *p;
- char *cp;
-
- number_of_files = 0;
- output_filename = "a.out";
-
- n_search_dirs = 0;
- search_dirs = (char **) xmalloc (sizeof (char *));
-
- /* First compute number_of_files so we know how long to make file_table. */
- /* Also process most options completely. */
-
- for (i = 1; i < argc; i++)
- {
- register int code = classify_arg (argv[i]);
- if (code)
- {
- if (i + code > argc)
- fatal ("no argument following %s\n", argv[i]);
-
- decode_option (argv[i], argv[i+1]);
-
- if (argv[i][1] == 'l' || argv[i][1] == 'A')
- number_of_files++;
-
- i += code - 1;
+decode_option(swt, arg)
+ register char *swt, *arg;
+{
+ /* We get Bstatic from gcc on suns. */
+ if (!strcmp(swt + 1, "Bstatic"))
+ return;
+ if (!strcmp(swt + 1, "Bdynamic"))
+ return;
+ if (!strcmp(swt + 1, "Bsymbolic"))
+ return;
+ if (!strcmp(swt + 1, "Bforcearchive"))
+ return;
+ if (!strcmp(swt + 1, "Bshareable"))
+ return;
+ if (!strcmp(swt + 1, "assert"))
+ return;
+ if (!strcmp(swt + 1, "Ttext")) {
+ text_start = parse(arg, "%x", "invalid argument to -Ttext");
+ T_flag_specified = 1;
+ return;
}
- else
- number_of_files++;
- }
-
- if (!number_of_files)
- fatal ("no input files", 0);
+ if (!strcmp(swt + 1, "Tdata")) {
+ rrs_data_start = parse(arg, "%x", "invalid argument to -Tdata");
+ Tdata_flag_specified = 1;
+ return;
+ }
+ if (!strcmp(swt + 1, "noinhibit-exec")) {
+ force_executable = 1;
+ return;
+ }
+ if (swt[2] != 0)
+ arg = &swt[2];
+
+ switch (swt[1]) {
+ case 'A':
+ return;
+
+ case 'D':
+ specified_data_size = parse(arg, "%x", "invalid argument to -D");
+ return;
+
+ case 'd':
+ if (*arg == 'c')
+ force_common_definition = 1;
+ else if (*arg == 'p')
+ force_alias_definition = 1;
+ else
+ fatal("-d option takes 'c' or 'p' argument");
+ return;
- p = file_table
- = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
- bzero (p, number_of_files * sizeof (struct file_entry));
+ case 'e':
+ entry_symbol = getsym(arg);
+ if (!entry_symbol->defined && !entry_symbol->referenced)
+ undefined_global_sym_count++;
+ entry_symbol->referenced = 1;
+ add_cmdline_ref(entry_symbol);
+ return;
- /* Now scan again and fill in file_table. */
- /* All options except -A and -l are ignored here. */
+ case 'l':
+ return;
- for (i = 1; i < argc; i++)
- {
- register int code = classify_arg (argv[i]);
+ case 'L':
+ add_search_dir(arg);
+ return;
- if (code)
- {
- char *string;
- if (code == 2)
- string = argv[i+1];
- else
- string = &argv[i][2];
-
- if (argv[i][1] == 'A')
- {
- if (p != file_table)
- fatal ("-A specified before an input file other than the first");
-
- p->filename = string;
- p->local_sym_name = string;
- p->just_syms_flag = 1;
- p++;
- }
- if (argv[i][1] == 'l')
- {
- if (cp = rindex(string, '/'))
- {
- *cp++ = '\0';
- cp = concat (string, "/lib", cp);
- p->filename = concat (cp, ".a", "");
- }
- else
- p->filename = concat ("lib", string, ".a");
-
- p->local_sym_name = concat ("-l", string, "");
- p->search_dirs_flag = 1;
- p++;
- }
- i += code - 1;
- }
- else
- {
- p->filename = argv[i];
- p->local_sym_name = argv[i];
- p++;
- }
- }
+ case 'M':
+ write_map = 1;
+ return;
- /* Now check some option settings for consistency. */
+ case 'N':
+ magic = OMAGIC;
+ return;
#ifdef NMAGIC
- if ((magic == ZMAGIC || magic == NMAGIC)
-#else
- if ((magic == ZMAGIC)
+ case 'n':
+ magic = NMAGIC;
+ return;
#endif
- && (text_start - text_start_alignment) & (page_size - 1))
- fatal ("-T argument not multiple of page size, with sharable output", 0);
-
- /* Append the standard search directories to the user-specified ones. */
- {
- int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
- n_search_dirs += n;
- search_dirs
- = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
- bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
- n * sizeof (char *));
- }
-}
-
-void
-add_cmdline_ref (sp)
- struct glosym *sp;
-{
- struct glosym **ptr;
-
- for (ptr = cmdline_references;
- ptr < cmdline_references + cl_refs_allocated && *ptr;
- ptr++)
- ;
-
- if (ptr >= cmdline_references + cl_refs_allocated - 1)
- {
- int diff = ptr - cmdline_references;
-
- cl_refs_allocated *= 2;
- cmdline_references = (struct glosym **)
- xrealloc (cmdline_references,
- cl_refs_allocated * sizeof (struct glosym *));
- ptr = cmdline_references + diff;
- }
-
- *ptr++ = sp;
- *ptr = (struct glosym *) 0;
-}
+#ifdef QMAGIC
+ case 'Q':
+ magic = oldmagic = QMAGIC;
+ return;
+ case 'Z':
+ magic = oldmagic = ZMAGIC;
+ return;
+#endif
-int
-set_element_prefixed_p (name)
- char *name;
-{
- struct string_list_element *p;
- int i;
+ case 'o':
+ output_filename = arg;
+ return;
- for (p = set_element_prefixes; p; p = p->next)
- {
- for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
- ;
+ case 'r':
+ relocatable_output = 1;
+ magic = OMAGIC;
+ text_start = 0;
+ return;
- if (p->str[i] == '\0')
- return 1;
- }
- return 0;
-}
+ case 'S':
+ strip_symbols = STRIP_DEBUGGER;
+ return;
-int parse ();
+ case 's':
+ strip_symbols = STRIP_ALL;
+ return;
-/* Record an option and arrange to act on it later.
- ARG should be the following command argument,
- which may or may not be used by this option.
+ case 'T':
+ text_start = parse(arg, "%x", "invalid argument to -T");
+ T_flag_specified = 1;
+ return;
- The `l' and `A' options are ignored here since they actually
- specify input files. */
+ case 't':
+ trace_files = 1;
+ return;
-void
-decode_option (swt, arg)
- register char *swt, *arg;
-{
- /* We get Bstatic from gcc on suns. */
- if (! strcmp (swt + 1, "Bstatic"))
- return;
- if (! strcmp (swt + 1, "Ttext"))
- {
- text_start = parse (arg, "%x", "invalid argument to -Ttext");
- T_flag_specified = 1;
- return;
- }
- if (! strcmp (swt + 1, "Tdata"))
- {
- data_start = parse (arg, "%x", "invalid argument to -Tdata");
- Tdata_flag_specified = 1;
- return;
- }
- if (! strcmp (swt + 1, "noinhibit-exec"))
- {
- force_executable = 1;
- return;
- }
-
- if (swt[2] != 0)
- arg = &swt[2];
-
- switch (swt[1])
- {
- case 'A':
- return;
-
- case 'D':
- specified_data_size = parse (arg, "%x", "invalid argument to -D");
- return;
-
- case 'd':
- force_common_definition = 1;
- return;
-
- case 'e':
- entry_symbol = getsym (arg);
- if (!entry_symbol->defined && !entry_symbol->referenced)
- undefined_global_sym_count++;
- entry_symbol->referenced = 1;
- add_cmdline_ref (entry_symbol);
- return;
-
- case 'l':
- /* If linking with libg++, use the C++ demangler. */
- if (arg != NULL && strcmp (arg, "g++") == 0)
- demangler = cplus_demangle;
- return;
- magic = OMAGIC;
-
- case 'L':
- n_search_dirs++;
- search_dirs
- = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
- search_dirs[n_search_dirs - 1] = arg;
- return;
-
- case 'M':
- write_map = 1;
- return;
-
- case 'N':
- magic = OMAGIC;
- return;
+ case 'u':
+ {
+ register symbol *sp = getsym(arg);
-#ifdef NMAGIC
- case 'n':
- magic = NMAGIC;
- return;
+ if (!sp->defined && !sp->referenced)
+ undefined_global_sym_count++;
+ sp->referenced = 1;
+ add_cmdline_ref(sp);
+ }
+ return;
+
+#if 1
+ case 'V':
+ soversion = parse(arg, "%d", "invalid argument to -V");
+ return;
#endif
- case 'o':
- output_filename = arg;
- return;
+ case 'X':
+ discard_locals = DISCARD_L;
+ return;
-#ifdef QMAGIC
- case 'q':
- magic = QMAGIC;
- return;
-#endif
+ case 'x':
+ discard_locals = DISCARD_ALL;
+ return;
- case 'r':
- relocatable_output = 1;
- magic = OMAGIC;
- text_start = 0;
- return;
-
- case 'S':
- strip_symbols = STRIP_DEBUGGER;
- return;
-
- case 's':
- strip_symbols = STRIP_ALL;
- return;
-
- case 'T':
- text_start = parse (arg, "%x", "invalid argument to -T");
- T_flag_specified = 1;
- return;
-
- case 't':
- trace_files = 1;
- return;
-
- case 'u':
- {
- register symbol *sp = getsym (arg);
- if (!sp->defined && !sp->referenced)
- undefined_global_sym_count++;
- sp->referenced = 1;
- add_cmdline_ref (sp);
- }
- return;
-
- case 'V':
- {
- struct string_list_element *new
- = (struct string_list_element *)
- xmalloc (sizeof (struct string_list_element));
-
- new->str = arg;
- new->next = set_element_prefixes;
- set_element_prefixes = new;
- return;
- }
-
- case 'X':
- discard_locals = DISCARD_L;
- return;
-
- case 'x':
- discard_locals = DISCARD_ALL;
- return;
-
- case 'y':
- {
- register symbol *sp = getsym (&swt[2]);
- sp->trace = 1;
- }
- return;
-
- case 'z':
- magic = ZMAGIC;
- return;
-
- default:
- fatal ("invalid command option `%s'", swt);
- }
+ case 'y':
+ {
+ register symbol *sp = getsym(&swt[2]);
+ sp->trace = 1;
+ }
+ return;
+
+ case 'z':
+ magic = ZMAGIC;
+ return;
+
+ default:
+ fatal("invalid command option `%s'", swt);
+ }
}
-/** Convenient functions for operating on one or all files being */
- /** loaded. */
-void print_file_name ();
-
-/* Call FUNCTION on each input file entry.
- Do not call for entries for libraries;
- instead, call once for each library member that is being loaded.
+/* Convenient functions for operating on one or all files being loaded. */
- FUNCTION receives two arguments: the entry, and ARG. */
+/*
+ * Call FUNCTION on each input file entry. Do not call for entries for
+ * libraries; instead, call once for each library member that is being
+ * loaded.
+ *
+ * FUNCTION receives two arguments: the entry, and ARG.
+ */
void
-each_file (function, arg)
- register void (*function)();
- register int arg;
-{
- register int 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);
+each_file(function, arg)
+ register void (*function) ();
+ register int arg;
+{
+ register int 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
+ (*function) (entry, arg);
}
- else
- (*function) (entry, arg);
- }
}
-/* Call FUNCTION on each input file entry until it returns a non-zero
- value. Return this value.
- Do not call for entries for libraries;
- instead, call once for each library member that is being loaded.
-
- FUNCTION receives two arguments: the entry, and ARG. It must be a
- function returning unsigned long (though this can probably be fudged). */
+/*
+ * Call FUNCTION on each input file entry until it returns a non-zero value.
+ * Return this value. Do not call for entries for libraries; instead, call
+ * once for each library member that is being loaded.
+ *
+ * FUNCTION receives two arguments: the entry, and ARG. It must be a function
+ * returning unsigned long (though this can probably be fudged).
+ */
unsigned long
-check_each_file (function, arg)
- register unsigned long (*function)();
- register int arg;
-{
- register int i;
- register unsigned long return_val;
-
- 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)
- if (return_val = (*function) (subentry, arg))
- return return_val;
+check_each_file(function, arg)
+ register unsigned long (*function) ();
+ register int arg;
+{
+ register int i;
+ register unsigned long return_val;
+
+ 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)
+ 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;
- }
- return 0;
+ return 0;
}
/* Like `each_file' but ignore files that were just for symbol definitions. */
void
-each_full_file (function, arg)
- register void (*function)();
- register int arg;
-{
- register int i;
-
- for (i = 0; i < number_of_files; i++)
- {
- register struct file_entry *entry = &file_table[i];
- if (entry->just_syms_flag)
- continue;
- if (entry->library_flag)
- {
- register struct file_entry *subentry = entry->subfiles;
- for (; subentry; subentry = subentry->chain)
- (*function) (subentry, arg);
+each_full_file(function, arg)
+ register void (*function) ();
+ register int arg;
+{
+ register int 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)
+ continue;
+ if (entry->library_flag) {
+ register struct file_entry *subentry = entry->subfiles;
+ for (; subentry; subentry = subentry->chain)
+ (*function) (subentry, arg);
+ } else
+ (*function) (entry, arg);
}
- else
- (*function) (entry, arg);
- }
}
/* Close the input file that is now open. */
void
-file_close ()
+file_close()
{
- close (input_desc);
- input_desc = 0;
- input_file = 0;
+ close(input_desc);
+ input_desc = 0;
+ input_file = 0;
}
-/* Open the input file specified by 'entry', and return a descriptor.
- The open file is remembered; if the same file is opened twice in a row,
- a new open is not actually done. */
-
+/*
+ * Open the input file specified by 'entry', and return a descriptor. The
+ * open file is remembered; if the same file is opened twice in a row, a new
+ * open is not actually done.
+ */
int
file_open (entry)
register struct file_entry *entry;
{
- register int desc;
+ register int desc;
- if (entry->superfile)
- return file_open (entry->superfile);
+ if (entry->superfile)
+ return file_open (entry->superfile);
- if (entry == input_file)
- return input_desc;
+ if (entry == input_file)
+ return input_desc;
- if (input_file) file_close ();
+ if (input_file) file_close ();
- if (entry->search_dirs_flag)
- {
- int i;
+ if (entry->search_dirs_flag) {
+ desc = findlib(entry);
+ } else
+ desc = open (entry->filename, O_RDONLY, 0);
- for (i = 0; i < n_search_dirs; i++)
- {
- register char *string
- = concat (search_dirs[i], "/", entry->filename);
- desc = open (string, O_RDONLY, 0);
- if (desc > 0)
- {
- entry->filename = string;
- entry->search_dirs_flag = 0;
- break;
- }
- free (string);
+ if (desc > 0) {
+ input_file = entry;
+ input_desc = desc;
+ return desc;
}
- }
- else
- desc = open (entry->filename, O_RDONLY, 0);
-
- if (desc > 0)
- {
- input_file = entry;
- input_desc = desc;
- return desc;
- }
-
- perror_file (entry);
- /* NOTREACHED */
-}
-
-/* Print the filename of ENTRY on OUTFILE (a stdio stream),
- and then a newline. */
-
-void
-prline_file_name (entry, outfile)
- struct file_entry *entry;
- FILE *outfile;
-{
- print_file_name (entry, outfile);
- fprintf (outfile, "\n");
-}
-
-/* Print the filename of ENTRY on OUTFILE (a stdio stream). */
-void
-print_file_name (entry, outfile)
- struct file_entry *entry;
- FILE *outfile;
-{
- if (entry->superfile)
- {
- print_file_name (entry->superfile, outfile);
- fprintf (outfile, "(%s)", entry->filename);
- }
- else
- fprintf (outfile, "%s", entry->filename);
+ perror_file (entry);
+ /* NOTREACHED */
}
-/* Return the filename of entry as a string (malloc'd for the purpose) */
-
-char *
-get_file_name (entry)
+int
+text_offset (entry)
struct file_entry *entry;
{
- char *result, *supfile;
- if (entry->superfile)
- {
- supfile = get_file_name (entry->superfile);
- result = (char *) xmalloc (strlen (supfile)
- + strlen (entry->filename) + 3);
- sprintf (result, "%s(%s)", supfile, entry->filename);
- free (supfile);
- }
- else
- {
- result = (char *) xmalloc (strlen (entry->filename) + 1);
- strcpy (result, entry->filename);
- }
- return result;
+ return entry->starting_offset + N_TXTOFF (entry->header);
}
/* Medium-level input routines for rel files. */
-/* Read a file's header into the proper place in the file_entry.
- DESC is the descriptor on which the file is open.
- ENTRY is the file's entry. */
-
+/*
+ * Read a file's header into the proper place in the file_entry. DESC is the
+ * descriptor on which the file is open. ENTRY is the file's entry.
+ */
void
read_header (desc, entry)
int desc;
register struct file_entry *entry;
{
- register int len;
- struct exec *loc = (struct exec *) &entry->header;
+ register int len;
+ struct exec *loc = (struct exec *) &entry->header;
- lseek (desc, entry->starting_offset, 0);
-#ifdef COFF_ENCAPSULATE
- if (entry->just_syms_flag)
- lseek (desc, sizeof(coffheader), 1);
-#endif
- len = read (desc, loc, sizeof (struct exec));
- if (len != sizeof (struct exec))
- fatal_with_file ("failure reading header of ", entry);
- if (N_BADMAG (*loc))
- fatal_with_file ("bad magic number in ", entry);
+ if (lseek (desc, entry->starting_offset, L_SET) !=
+ entry->starting_offset)
+ fatal_with_file("read_header: lseek failure ", entry);
- entry->header_read_flag = 1;
-}
+ len = read (desc, &entry->header, sizeof (struct exec));
+ if (len != sizeof (struct exec))
+ fatal_with_file ("failure reading header of ", entry);
-/* Read the symbols of file ENTRY into core.
- Assume it is already open, on descriptor DESC.
- Also read the length of the string table, which follows the symbol table,
- but don't read the contents of the string table. */
+ md_swapin_exec_hdr(&entry->header);
+ if (N_BADMAG (*loc))
+ fatal_with_file ("bad magic number in ", entry);
+
+ entry->header_read_flag = 1;
+}
+
+/*
+ * Read the symbols of file ENTRY into core. Assume it is already open, on
+ * descriptor DESC. Also read the length of the string table, which follows
+ * the symbol table, but don't read the contents of the string table.
+ */
void
read_entry_symbols (desc, entry)
struct file_entry *entry;
int desc;
{
- int str_size;
+ int str_size;
+ struct nlist *np;
+ int i;
- if (!entry->header_read_flag)
- read_header (desc, entry);
+ if (!entry->header_read_flag)
+ read_header (desc, entry);
- entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
+ np = (struct nlist *) alloca (entry->header.a_syms);
+ entry->nsymbols = entry->header.a_syms / sizeof(struct nlist);
+ entry->symbols = (struct localsymbol *)
+ xmalloc(entry->nsymbols * sizeof(struct localsymbol));
- lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
- if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
- fatal_with_file ("premature end of file in symbols of ", entry);
+ if (lseek(desc, N_SYMOFF(entry->header) + entry->starting_offset, L_SET)
+ != N_SYMOFF(entry->header) + entry->starting_offset)
+ fatal_with_file ("read_symbols(h): lseek failure ", entry);
- lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
- if (sizeof str_size != read (desc, &str_size, sizeof str_size))
- fatal_with_file ("bad string table size in ", entry);
+ if (entry->header.a_syms != read (desc, np, entry->header.a_syms))
+ fatal_with_file ("premature end of file in symbols of ", entry);
- entry->string_size = str_size;
-}
+ md_swapin_symbols(np, entry->header.a_syms / sizeof(struct nlist));
-/* Read the string table of file ENTRY into core.
- Assume it is already open, on descriptor DESC.
- Also record whether a GDB symbol segment follows the string table. */
+ for (i = 0; i < entry->nsymbols; i++) {
+ entry->symbols[i].nzlist.nlist = *np++;
+ entry->symbols[i].nzlist.nz_size = 0;
+ entry->symbols[i].symbol = NULL;
+ entry->symbols[i].next = NULL;
+ entry->symbols[i].gotslot_offset = -1;
+ entry->symbols[i].gotslot_claimed = 0;
+ }
+
+ entry->strings_offset = N_STROFF(entry->header) +
+ entry->starting_offset;
+ if (lseek(desc, entry->strings_offset, 0) == (off_t)-1)
+ fatal_with_file ("read_symbols(s): lseek failure ", entry);
+ if (sizeof str_size != read (desc, &str_size, sizeof str_size))
+ fatal_with_file ("bad string table size in ", entry);
+
+ entry->string_size = md_swap_long(str_size);
+}
+/*
+ * Read the string table of file ENTRY into core. Assume it is already open,
+ * on descriptor DESC.
+ */
void
read_entry_strings (desc, entry)
struct file_entry *entry;
int desc;
{
- int buffer;
+ int buffer;
- if (!entry->header_read_flag)
- read_header (desc, entry);
+ if (!entry->header_read_flag || !entry->strings_offset)
+ fatal("internal error: %s", "cannot read string table");
- lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
- if (entry->string_size != read (desc, entry->strings, entry->string_size))
- fatal_with_file ("premature end of file in strings of ", entry);
+ if (lseek (desc, entry->strings_offset, L_SET) != entry->strings_offset)
+ fatal_with_file ("read_strings: lseek failure ", entry);
+
+ if (entry->string_size !=
+ read (desc, entry->strings, entry->string_size))
+ fatal_with_file ("premature end of file in strings of ", entry);
-#if 0
- /* While we are here, see if the file has a symbol segment at the end.
- For a separate file, just try reading some more.
- For a library member, compare current pos against total size. */
- if (entry->superfile)
- {
- if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
- return;
- }
- else
- {
- buffer = read (desc, &buffer, sizeof buffer);
- if (buffer == 0)
return;
- if (buffer != sizeof buffer)
- fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
- }
-#endif
- /* Don't try to do anything with symsegs. */
- return;
-#if 0
- /* eliminate warning of `statement not reached'. */
- entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
-#endif
}
-
-/* Read in the symbols of all input files. */
-void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
-void enter_file_symbols (), enter_global_ref (), search_library ();
+/* DEAD - Read in all of the relocation information */
void
-load_symbols ()
+read_relocation ()
{
- register int i;
-
- if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
-
- for (i = 0; i < number_of_files; i++)
- {
- register struct file_entry *entry = &file_table[i];
- read_file_symbols (entry);
- }
-
- if (trace_files) fprintf (stderr, "\n");
+ each_full_file (read_entry_relocation, 0);
}
-/* If ENTRY is a rel file, read its symbol and string sections into core.
- If it is a library, search it and load the appropriate members
- (which means calling this function recursively on those members). */
+/* Read in the relocation sections of ENTRY if necessary */
void
-read_file_symbols (entry)
- register struct file_entry *entry;
+read_entry_relocation (desc, entry)
+ int desc;
+ struct file_entry *entry;
{
- register int desc;
- register int len;
- struct exec hdr;
-
- desc = file_open (entry);
+ register struct relocation_info *reloc;
+ off_t pos;
-#ifdef COFF_ENCAPSULATE
- if (entry->just_syms_flag)
- lseek (desc, sizeof(coffheader),0);
-#endif
+ if (!entry->textrel) {
- len = read (desc, &hdr, sizeof hdr);
- if (len != sizeof hdr)
- fatal_with_file ("failure reading header of ", entry);
-
- if (!N_BADMAG (hdr))
- {
- read_entry_symbols (desc, entry);
- entry->strings = (char *) alloca (entry->string_size);
- read_entry_strings (desc, entry);
- enter_file_symbols (entry);
- entry->strings = 0;
- }
- else
- {
- char armag[SARMAG];
-
- lseek (desc, 0, 0);
- if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
- fatal_with_file ("malformed input file (not rel or archive) ", entry);
- entry->library_flag = 1;
- search_library (desc, entry);
- }
-
- file_close ();
-}
-
-/* Enter the external symbol defs and refs of ENTRY in the hash table. */
+ reloc = (struct relocation_info *)
+ xmalloc(entry->header.a_trsize);
-void
-enter_file_symbols (entry)
- struct file_entry *entry;
-{
- register struct nlist
- *p,
- *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
+ pos = text_offset(entry) +
+ entry->header.a_text + entry->header.a_data;
- if (trace_files) prline_file_name (entry, stderr);
+ if (lseek(desc, pos, L_SET) != pos)
+ fatal_with_file("read_reloc(t): lseek failure ", entry);
- for (p = entry->symbols; p < end; p++)
- {
- if (p->n_type == (N_SETV | N_EXT)) continue;
- if (set_element_prefixes
- && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
- p->n_type += (N_SETA - N_ABS);
+ if (entry->header.a_trsize !=
+ read(desc, reloc, entry->header.a_trsize)) {
+ fatal_with_file (
+ "premature eof in text relocation of ", entry);
+ }
+ md_swapin_reloc(reloc, entry->header.a_trsize / sizeof(*reloc));
+ entry->textrel = reloc;
+ entry->ntextrel = entry->header.a_trsize / sizeof(*reloc);
- if (SET_ELEMENT_P (p->n_type))
- {
- set_symbol_count++;
- if (!relocatable_output)
- enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
- }
- else if (p->n_type == N_WARNING)
- {
- char *name = p->n_un.n_strx + entry->strings;
-
- /* Grab the next entry. */
- p++;
- if (p->n_type != (N_UNDF | N_EXT))
- {
- fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
- progname, entry->filename);
- make_executable = 0;
- p--; /* Process normally. */
- }
- else
- {
- symbol *sp;
- char *sname = p->n_un.n_strx + entry->strings;
- /* Deal with the warning symbol. */
- enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
- sp = getsym (sname);
- sp->warning = (char *) xmalloc (strlen(name) + 1);
- strcpy (sp->warning, name);
- warning_count++;
- }
}
- else if (p->n_type & N_EXT)
- enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
- else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
- {
- if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
- non_L_local_sym_count++;
- local_sym_count++;
- }
- else 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. */
-
- local_sym_count++;
- non_L_local_sym_count++;
-}
-
-/* Enter one global symbol in the hash table.
- NLIST_P points to the `struct nlist' read from the file
- that describes the global symbol. NAME is the symbol's name.
- ENTRY is the file entry for the file the symbol comes from.
-
- The `struct nlist' is modified by placing it on a chain of
- all such structs that refer to the same global symbol.
- This chain starts in the `refs' field of the symbol table entry
- and is chained through the `n_name'. */
-
-void
-enter_global_ref (nlist_p, name, entry)
- register struct nlist *nlist_p;
- char *name;
- struct file_entry *entry;
-{
- register symbol *sp = getsym (name);
- register int type = nlist_p->n_type;
- int oldref = sp->referenced;
- int olddef = sp->defined;
- nlist_p->n_un.n_name = (char *) sp->refs;
- sp->refs = nlist_p;
+ if (!entry->datarel) {
- sp->referenced = 1;
- if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
- {
- if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
- sp->defined = type;
+ reloc = (struct relocation_info *)
+ xmalloc(entry->header.a_drsize);
- if (oldref && !olddef)
- /* It used to be undefined and we're defining it. */
- undefined_global_sym_count--;
+ pos = text_offset(entry) + entry->header.a_text +
+ entry->header.a_data + entry->header.a_trsize;
- if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
- {
- /* First definition and it's common. */
- common_defined_global_count++;
- sp->max_common_size = nlist_p->n_value;
- }
- else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
- {
- /* It used to be common and we're defining it as
- something else. */
- common_defined_global_count--;
- sp->max_common_size = 0;
- }
- else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
- && sp->max_common_size < nlist_p->n_value)
- /* It used to be common and this is a new common entry to
- which we need to pay attention. */
- sp->max_common_size = nlist_p->n_value;
-
- /* Are we defining it as a set element? */
- if (SET_ELEMENT_P (type)
- && (!olddef || (olddef && sp->max_common_size)))
- set_vector_count++;
- /* As an indirection? */
- else if (type == (N_INDR | N_EXT))
- {
- /* Indirect symbols value should be modified to point
- a symbol being equivalenced to. */
- nlist_p->n_value
- = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
- + entry->strings);
- if ((symbol *) nlist_p->n_value == sp)
- {
- /* Somebody redefined a symbol to be itself. */
- fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
- entry->filename, name);
- /* Rewrite this symbol as being a global text symbol
- with value 0. */
- nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
- nlist_p->n_value = 0;
- /* Don't make the output executable. */
- make_executable = 0;
- }
- else
- global_indirect_count++;
- }
- }
- else
- if (!oldref)
-#ifndef DOLLAR_KLUDGE
- undefined_global_sym_count++;
-#else
- {
- if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
- {
- /* This is an (ISI?) $-conditional; skip it */
- sp->referenced = 0;
- if (sp->trace)
- {
- fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
- print_file_name (entry, stderr);
- fprintf (stderr, "\n");
- }
- return;
- }
- else
- undefined_global_sym_count++;
- }
-#endif
+ if (lseek(desc, pos, L_SET) != pos)
+ fatal_with_file("read_reloc(d): lseek failure ", entry);
- if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
- text_start = nlist_p->n_value;
+ if (entry->header.a_drsize !=
+ read (desc, reloc, entry->header.a_drsize)) {
+ fatal_with_file (
+ "premature eof in data relocation of ", entry);
+ }
+ md_swapin_reloc(reloc, entry->header.a_drsize / sizeof(*reloc));
+ entry->datarel = reloc;
+ entry->ndatarel = entry->header.a_drsize / sizeof(*reloc);
- if (sp->trace)
- {
- register char *reftype;
- switch (type & N_TYPE)
- {
- case N_UNDF:
- if (nlist_p->n_value)
- reftype = "defined as common";
- else reftype = "referenced";
- break;
-
- case N_ABS:
- reftype = "defined as absolute";
- break;
-
- case N_TEXT:
- reftype = "defined in text section";
- break;
-
- case N_DATA:
- reftype = "defined in data section";
- break;
-
- case N_BSS:
- reftype = "defined in BSS section";
- break;
-
- case N_SETT:
- reftype = "is a text set element";
- break;
-
- case N_SETD:
- reftype = "is a data set element";
- break;
-
- case N_SETB:
- reftype = "is a BSS set element";
- break;
-
- case N_SETA:
- reftype = "is an absolute set element";
- break;
-
- case N_SETV:
- reftype = "defined in data section as vector";
- break;
-
- case N_INDR:
- reftype = (char *) alloca (23
- + strlen ((nlist_p + 1)->n_un.n_strx
- + entry->strings));
- sprintf (reftype, "defined equivalent to %s",
- (nlist_p + 1)->n_un.n_strx + entry->strings);
- break;
-
-#ifdef sequent
- case N_SHUNDF:
- reftype = "shared undf";
- break;
-
-/* These conflict with cases above.
- case N_SHDATA:
- reftype = "shared data";
- break;
-
- case N_SHBSS:
- reftype = "shared BSS";
- break;
-*/
- default:
- reftype = "I don't know this type";
- break;
-#endif
}
-
- fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
- print_file_name (entry, stderr);
- fprintf (stderr, "\n");
- }
-}
-
-/* This return 0 if the given file entry's symbol table does *not*
- contain the nlist point entry, and it returns the files entry
- pointer (cast to unsigned long) if it does. */
-
-unsigned long
-contains_symbol (entry, n_ptr)
- struct file_entry *entry;
- register struct nlist *n_ptr;
-{
- if (n_ptr >= entry->symbols &&
- n_ptr < (entry->symbols
- + (entry->header.a_syms / sizeof (struct nlist))))
- return (unsigned long) entry;
- return 0;
}
-/* Searching libraries */
-
-struct file_entry *decode_library_subfile ();
-void linear_library (), symdef_library ();
+/* Read in the symbols of all input files. */
-/* Search the library ENTRY, already open on descriptor DESC.
- This means deciding which library members to load,
- making a chain of `struct file_entry' for those members,
- and entering their global symbols in the hash table. */
+void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
+void enter_file_symbols (), enter_global_ref ();
void
-search_library (desc, entry)
- int desc;
- struct file_entry *entry;
+load_symbols ()
{
- int member_length;
- register char *name;
- register struct file_entry *subentry;
-
- if (!undefined_global_sym_count) return;
-
- /* Examine its first member, which starts SARMAG bytes in. */
- subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
- if (!subentry) return;
-
- name = subentry->filename;
- free (subentry);
+ register int i;
- /* Search via __.SYMDEF if that exists, else linearly. */
+ if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
- if (!strcmp (name, "__.SYMDEF"))
- symdef_library (desc, entry, member_length);
- else
- linear_library (desc, entry);
-}
-
-/* Construct and return a file_entry for a library member.
- The library's file_entry is library_entry, and the library is open on DESC.
- SUBFILE_OFFSET is the byte index in the library of this member's header.
- We store the length of the member into *LENGTH_LOC. */
+ for (i = 0; i < number_of_files; i++) {
+ register struct file_entry *entry = &file_table[i];
+ read_file_symbols (entry);
+ }
-struct file_entry *
-decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
- int desc;
- struct file_entry *library_entry;
- int subfile_offset;
- int *length_loc;
-{
- int bytes_read;
- register int namelen;
- int member_length;
- register char *name;
- struct ar_hdr hdr1;
- register struct file_entry *subentry;
-
- lseek (desc, subfile_offset, 0);
-
- bytes_read = read (desc, &hdr1, sizeof hdr1);
- if (!bytes_read)
- return 0; /* end of archive */
-
- if (sizeof hdr1 != bytes_read)
- fatal_with_file ("malformed library archive ", library_entry);
-
- if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
- fatal_with_file ("malformatted header of archive member in ", library_entry);
-
- subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
- bzero (subentry, sizeof (struct file_entry));
-
- for (namelen = 0;
- namelen < sizeof hdr1.ar_name
- && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
- && hdr1.ar_name[namelen] != '/';
- namelen++);
-
- name = (char *) xmalloc (namelen+1);
- strncpy (name, hdr1.ar_name, namelen);
- name[namelen] = 0;
-
- subentry->filename = name;
- subentry->local_sym_name = name;
- subentry->symbols = 0;
- subentry->strings = 0;
- subentry->subfiles = 0;
- subentry->starting_offset = subfile_offset + sizeof hdr1;
- subentry->superfile = library_entry;
- subentry->library_flag = 0;
- subentry->header_read_flag = 0;
- subentry->just_syms_flag = 0;
- subentry->chain = 0;
- subentry->total_size = member_length;
-
- (*length_loc) = member_length;
-
- return subentry;
+ if (trace_files) fprintf (stderr, "\n");
}
-
-int subfile_wanted_p ();
-/* Search a library that has a __.SYMDEF member.
- DESC is a descriptor on which the library is open.
- The file pointer is assumed to point at the __.SYMDEF data.
- ENTRY is the library's file_entry.
- MEMBER_LENGTH is the length of the __.SYMDEF data. */
+/*
+ * If ENTRY is a rel file, read its symbol and string sections into core. If
+ * it is a library, search it and load the appropriate members (which means
+ * calling this function recursively on those members).
+ */
void
-symdef_library (desc, entry, member_length)
- int desc;
- struct file_entry *entry;
- int member_length;
+read_file_symbols (entry)
+ register struct file_entry *entry;
{
- int *symdef_data = (int *) xmalloc (member_length);
- register struct symdef *symdef_base;
- char *sym_name_base;
- int number_of_symdefs;
- int length_of_strings;
- int not_finished;
- int bytes_read;
- register int i;
- struct file_entry *prev = 0;
- int prev_offset = 0;
-
- bytes_read = read (desc, symdef_data, member_length);
- if (bytes_read != member_length)
- fatal_with_file ("malformatted __.SYMDEF in ", entry);
-
- number_of_symdefs = *symdef_data / sizeof (struct symdef);
- if (number_of_symdefs < 0 ||
- number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
- fatal_with_file ("malformatted __.SYMDEF in ", entry);
-
- symdef_base = (struct symdef *) (symdef_data + 1);
- length_of_strings = *(int *) (symdef_base + number_of_symdefs);
-
- if (length_of_strings < 0
- || number_of_symdefs * sizeof (struct symdef) + length_of_strings
- + 2 * sizeof (int) > member_length)
- fatal_with_file ("malformatted __.SYMDEF in ", entry);
-
- sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
-
- /* Check all the string indexes for validity. */
-
- for (i = 0; i < number_of_symdefs; i++)
- {
- register int index = symdef_base[i].symbol_name_string_index;
- if (index < 0 || index >= length_of_strings
- || (index && *(sym_name_base + index - 1)))
- fatal_with_file ("malformatted __.SYMDEF in ", entry);
- }
-
- /* Search the symdef data for members to load.
- Do this until one whole pass finds nothing to load. */
-
- not_finished = 1;
- while (not_finished)
- {
- not_finished = 0;
-
- /* Scan all the symbols mentioned in the symdef for ones that we need.
- Load the library members that contain such symbols. */
-
- for (i = 0;
- (i < number_of_symdefs
- && (undefined_global_sym_count || common_defined_global_count));
- i++)
- if (symdef_base[i].symbol_name_string_index >= 0)
- {
- register symbol *sp;
-
- sp = getsym_soft (sym_name_base
- + symdef_base[i].symbol_name_string_index);
-
- /* If we find a symbol that appears to be needed, think carefully
- about the archive member that the symbol is in. */
-
- if (sp && ((sp->referenced && !sp->defined)
- || (sp->defined && sp->max_common_size)))
- {
- int junk;
- register int j;
- register int offset = symdef_base[i].library_member_offset;
- struct file_entry *subentry;
-
- /* Don't think carefully about any archive member
- more than once in a given pass. */
-
- if (prev_offset == offset)
- continue;
- prev_offset = offset;
-
- /* Read the symbol table of the archive member. */
-
- subentry = decode_library_subfile (desc, entry, offset, &junk);
- if (subentry == 0)
- fatal ("invalid offset for %s in symbol table of %s",
- sym_name_base
- + symdef_base[i].symbol_name_string_index,
- entry->filename);
- read_entry_symbols (desc, subentry);
- subentry->strings = (char *) malloc (subentry->string_size);
- read_entry_strings (desc, subentry);
-
- /* Now scan the symbol table and decide whether to load. */
-
- if (!subfile_wanted_p (subentry))
- {
- free (subentry->symbols);
- free (subentry);
- }
- else
- {
- /* This member is needed; load it.
- Since we are loading something on this pass,
- we must make another pass through the symdef data. */
-
- not_finished = 1;
-
- enter_file_symbols (subentry);
-
- if (prev)
- prev->chain = subentry;
- else entry->subfiles = subentry;
- prev = subentry;
-
- /* Clear out this member's symbols from the symdef data
- so that following passes won't waste time on them. */
-
- for (j = 0; j < number_of_symdefs; j++)
- {
- if (symdef_base[j].library_member_offset == offset)
- symdef_base[j].symbol_name_string_index = -1;
- }
- }
-
- /* We'll read the strings again if we need them again. */
- free (subentry->strings);
- subentry->strings = 0;
- }
- }
- }
-
- free (symdef_data);
+ register int desc;
+ register int len;
+ struct exec hdr;
+
+ desc = file_open (entry);
+
+ len = read (desc, &hdr, sizeof hdr);
+ if (len != sizeof hdr)
+ fatal_with_file ("failure reading header of ", entry);
+
+ md_swapin_exec_hdr(&hdr);
+
+ if (!N_BADMAG (hdr)) {
+ if (N_IS_DYNAMIC(hdr)) {
+ if (relocatable_output) {
+ fatal_with_file(
+ "-r and shared objects currently not supported ",
+ entry);
+ return;
+ }
+ entry->is_dynamic = 1;
+ read_shared_object(desc, entry);
+ rrs_add_shobj(entry);
+ } else {
+ read_entry_symbols (desc, entry);
+ entry->strings = (char *) alloca (entry->string_size);
+ read_entry_strings (desc, entry);
+ read_entry_relocation(desc, entry);
+ enter_file_symbols (entry);
+ entry->strings = 0;
+ }
+ } else {
+ char armag[SARMAG];
+
+ lseek (desc, 0, 0);
+ if (SARMAG != read (desc, armag, SARMAG) ||
+ strncmp (armag, ARMAG, SARMAG))
+ fatal_with_file(
+ "malformed input file (not rel or archive) ", entry);
+ entry->library_flag = 1;
+ search_library (desc, entry);
+ }
+
+ file_close ();
}
-/* Search a library that has no __.SYMDEF.
- ENTRY is the library's file_entry.
- DESC is the descriptor it is open on. */
+/* Enter the external symbol defs and refs of ENTRY in the hash table. */
void
-linear_library (desc, entry)
- int desc;
+enter_file_symbols (entry)
struct file_entry *entry;
{
- register struct file_entry *prev = 0;
- register int this_subfile_offset = SARMAG;
-
- while (undefined_global_sym_count || common_defined_global_count)
- {
- int member_length;
- register struct file_entry *subentry;
-
- subentry = decode_library_subfile (desc, entry, this_subfile_offset,
- &member_length);
-
- if (!subentry) return;
-
- read_entry_symbols (desc, subentry);
- subentry->strings = (char *) alloca (subentry->string_size);
- read_entry_strings (desc, subentry);
-
- if (!subfile_wanted_p (subentry))
- {
- free (subentry->symbols);
- free (subentry);
+ struct localsymbol *lsp, *lspend;
+
+ if (trace_files) prline_file_name (entry, stderr);
+
+ lspend = entry->symbols + entry->nsymbols;
+
+ for (lsp = entry->symbols; lsp < lspend; lsp++) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+
+ if (p->n_type == (N_SETV | N_EXT))
+ continue;
+
+ /*
+ * Turn magically prefixed symbols into set symbols of
+ * a corresponding type.
+ */
+ if (set_element_prefixes &&
+ set_element_prefixed_p(entry->strings+lsp->nzlist.nz_strx))
+ lsp->nzlist.nz_type += (N_SETA - N_ABS);
+
+ if (SET_ELEMENT_P(p->n_type)) {
+ set_symbol_count++;
+ if (!relocatable_output)
+ enter_global_ref(lsp,
+ p->n_un.n_strx + entry->strings, entry);
+ } else if (p->n_type == N_WARNING) {
+ char *name = p->n_un.n_strx + entry->strings;
+
+ /* Grab the next entry. */
+ p++;
+ if (p->n_type != (N_UNDF | N_EXT)) {
+ error("Warning symbol found in %s without external reference following.",
+ get_file_name(entry));
+ make_executable = 0;
+ p--; /* Process normally. */
+ } else {
+ symbol *sp;
+ char *sname = p->n_un.n_strx + entry->strings;
+ /* Deal with the warning symbol. */
+ enter_global_ref(lsp,
+ p->n_un.n_strx + entry->strings, entry);
+ sp = getsym (sname);
+ sp->warning = (char *)xmalloc(strlen(name)+1);
+ strcpy (sp->warning, name);
+ warning_count++;
+ }
+ } 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
- {
- enter_file_symbols (subentry);
- if (prev)
- prev->chain = subentry;
- else entry->subfiles = subentry;
- prev = subentry;
- subentry->strings = 0; /* Since space will dissapear on return */
- }
+ /*
+ * 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.
+ */
- this_subfile_offset += member_length + sizeof (struct ar_hdr);
- if (this_subfile_offset & 1) this_subfile_offset++;
- }
+ if (!entry->is_dynamic) {
+ local_sym_count++;
+ non_L_local_sym_count++;
+ }
}
-
-/* ENTRY is an entry for a library member.
- Its symbols have been read into core, but not entered.
- Return nonzero if we ought to load this member. */
-int
-subfile_wanted_p (entry)
+/*
+ * Enter one global symbol in the hash table. LSP points to the `struct
+ * localsymbol' from the file that describes the global symbol. NAME is the
+ * symbol's name. ENTRY is the file entry for the file the symbol comes from.
+ *
+ * LSP is put on the chain of all such structs that refer to the same symbol.
+ * 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
+ * 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.
+ *
+ */
+
+void
+enter_global_ref (lsp, name, entry)
+ struct localsymbol *lsp;
+ char *name;
struct file_entry *entry;
{
- register struct nlist *p;
- register struct nlist *end
- = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
-#ifdef DOLLAR_KLUDGE
- register int dollar_cond = 0;
-#endif
+ register struct nzlist *nzp = &lsp->nzlist;
+ register symbol *sp = getsym (name);
+ register int type = nzp->nz_type;
+ int oldref = sp->referenced;
+ int olddef = sp->defined;
+ int com = sp->defined && sp->max_common_size;
+
+ if (type == (N_INDR | N_EXT)) {
+ sp->alias = getsym(entry->strings + (lsp + 1)->nzlist.nz_strx);
+ if (sp == sp->alias) {
+ error("%s: %s is alias for itself",
+ get_file_name(entry), name);
+ /* Rewrite symbol as global text symbol with value 0 */
+ lsp->nzlist.nz_type = N_TEXT|N_EXT;
+ lsp->nzlist.nz_value = 0;
+ make_executable = 0;
+ } else
+ global_alias_count++;
+ }
- for (p = entry->symbols; p < end; p++)
- {
- register int type = p->n_type;
- register char *name = p->n_un.n_strx + entry->strings;
+ if (entry->is_dynamic) {
+ lsp->next = sp->sorefs;
+ sp->sorefs = lsp;
+
+ /*
+ * Handle commons from shared objects:
+ * 1) If symbol hitherto undefined, turn it into a common.
+ * 2) If symbol already common, update size if necessary.
+ */
+/*XXX - look at case where commons are only in shared objects */
+ if (type == (N_UNDF | N_EXT) && nzp->nz_value) {
+ if (!olddef) {
+ if (oldref)
+ undefined_global_sym_count--;
+ common_defined_global_count++;
+ sp->max_common_size = nzp->nz_value;
+ sp->defined = N_UNDF | N_EXT;
+ } else if (com && sp->max_common_size < nzp->nz_value) {
+ sp->max_common_size = nzp->nz_value;
+ }
+ }
- /* If the symbol has an interesting definition, we could
- potentially want it. */
- if (type & N_EXT
- && (type != (N_UNDF | N_EXT) || p->n_value
+ /*
+ * Handle size information in shared objects.
+ */
+ if (nzp->nz_size > sp->size)
+ sp->size = nzp->nz_size;
-#ifdef DOLLAR_KLUDGE
- || name[1] == '$'
-#endif
- )
- && !SET_ELEMENT_P (type)
- && !set_element_prefixed_p (name))
- {
- register symbol *sp = getsym_soft (name);
-
-#ifdef DOLLAR_KLUDGE
- if (name[1] == '$')
- {
- sp = getsym_soft (&name[2]);
- dollar_cond = 1;
- if (!sp) continue;
- if (sp->referenced)
- {
- if (write_map)
- {
- print_file_name (entry, stdout);
- fprintf (stdout, " needed due to $-conditional %s\n", name);
- }
- return 1;
- }
- continue;
- }
-#endif
+ lsp->symbol = sp;
+ return;
+ }
+
+ lsp->next = sp->refs;
+ sp->refs = lsp;
+ lsp->symbol = sp;
- /* If this symbol has not been hashed, we can't be looking for it. */
+ sp->referenced = 1;
- if (!sp) continue;
+ if (sp == dynamic_symbol || sp == got_symbol) {
+ if (type != (N_UNDF | N_EXT) && !entry->just_syms_flag)
+ fatal("Linker reserved symbol %s defined as type %x ",
+ name, type);
+ return;
+ }
- if ((sp->referenced && !sp->defined)
- || (sp->defined && sp->max_common_size && (type & N_TEXT) == 0))
- {
- /* This is a symbol we are looking for. It is either
- not yet defined or defined as a common. */
-#ifdef DOLLAR_KLUDGE
- if (dollar_cond) continue;
+#ifdef N_SIZE
+ if (type == (N_SIZE | N_EXT)) {
+ if (sp->size < nzp->nz_value)
+ sp->size = nzp->nz_value;
+ } else
#endif
- if (type == (N_UNDF | N_EXT))
- {
- /* Symbol being defined as common.
- Remember this, but don't load subfile just for this. */
-
- /* If it didn't used to be common, up the count of
- common symbols. */
- if (!sp->max_common_size)
- common_defined_global_count++;
-
- if (sp->max_common_size < p->n_value)
- sp->max_common_size = p->n_value;
- if (!sp->defined)
- undefined_global_sym_count--;
- sp->defined = 1;
- continue;
+ if (type != (N_UNDF | N_EXT) || nzp->nz_value) {
+
+ /*
+ * Set `->defined' here, so commons and undefined globals
+ * can be counted correctly.
+ */
+ if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
+ sp->defined = type;
+
+ if (oldref && !olddef)
+ /*
+ * It used to be undefined and we're defining it.
+ */
+ undefined_global_sym_count--;
+
+ if (!olddef && type == (N_UNDF | N_EXT) && nzp->nz_value) {
+ /*
+ * First definition and it's common.
+ */
+ common_defined_global_count++;
+ sp->max_common_size = nzp->nz_value;
+ } else if (com && type != (N_UNDF | N_EXT)) {
+ /*
+ * It used to be common and we're defining
+ * it as something else.
+ */
+ common_defined_global_count--;
+ sp->max_common_size = 0;
+ } else if (com && type == (N_UNDF | N_EXT)
+ && sp->max_common_size < nzp->nz_value)
+ /*
+ * It used to be common and this is a new common entry
+ * to which we need to pay attention.
+ */
+ sp->max_common_size = nzp->nz_value;
+
+ if (SET_ELEMENT_P(type) && (!olddef || com))
+ set_vector_count++;
+
+ } else if (!oldref)
+ undefined_global_sym_count++;
+
+
+ if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
+ text_start = nzp->nz_value;
+
+ if (sp->trace) {
+ register char *reftype;
+ switch (type & N_TYPE) {
+ case N_UNDF:
+ reftype = nzp->nz_value?
+ "defined as common":"referenced";
+ break;
+
+ case N_ABS:
+ reftype = "defined as absolute";
+ break;
+
+ case N_TEXT:
+ reftype = "defined in text section";
+ break;
+
+ case N_DATA:
+ reftype = "defined in data section";
+ break;
+
+ case N_BSS:
+ reftype = "defined in BSS section";
+ break;
+
+ default:
+ reftype = "I don't know this type";
+ break;
}
- if (write_map)
- {
- print_file_name (entry, stdout);
- fprintf (stdout, " needed due to %s\n", sp->name);
- }
- return 1;
- }
+ fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
+ print_file_name (entry, stderr);
+ fprintf (stderr, "\n");
}
- }
+}
+
+/*
+ * This return 0 if the given file entry's symbol table does *not* contain
+ * the nlist point entry, and it returns the files entry pointer (cast to
+ * unsigned long) if it does.
+ */
- return 0;
+unsigned long
+contains_symbol (entry, np)
+ struct file_entry *entry;
+ register struct nlist *np;
+{
+ if (np >= &entry->symbols->nzlist.nlist &&
+ np < &(entry->symbols + entry->nsymbols)->nzlist.nlist)
+ return (unsigned long) entry;
+ return 0;
}
-
+
+
void consider_file_section_lengths (), relocate_file_addresses ();
+void consider_relocation();
-/* Having entered all the global symbols and found the sizes of sections
- of all files to be linked, make all appropriate deductions from this data.
+/*
+ * Having entered all the global symbols and found the sizes of sections of
+ * all files to be linked, make all appropriate deductions from this data.
+ *
+ * We propagate global symbol values from definitions to references. We compute
+ * the layout of the output file and where each input file's contents fit
+ * into it.
+ *
+ * This is now done in several stages.
+ *
+ * 1) All global symbols are examined for definitions in relocatable (.o)
+ * files. The symbols' type is set according to the definition found,
+ * but its value can not yet be determined. In stead, we keep a pointer
+ * to the file entry's localsymbol that bequeathed the global symbol with
+ * its definition. Also, multiple (incompatible) definitions are checked
+ * for in this pass. If no definition comes forward, the set of local
+ * 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.
+ *
+ * 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).
+ *
+ * 4) Finally, all symbols are relocated according according to the start
+ * of the entry they are part of. Then global symbols are assigned their
+ * final values. Also, space for commons and imported data are allocated
+ * during this pass, if the link mode in effect so demands.
+ *
+ */
- We propagate global symbol values from definitions to references.
- We compute the layout of the output file and where each input file's
- contents fit into it. */
+void digest_pass1(), digest_pass2();
void
digest_symbols ()
{
- register int i;
- int setv_fill_count;
- if (trace_files)
- fprintf (stderr, "Digesting symbol information:\n\n");
+ if (trace_files)
+ fprintf(stderr, "Digesting symbol information:\n\n");
+
+ if (!relocatable_output) {
+ /*
+ * The set sector size is the number of set elements + a word
+ * for each symbol for the length word at the beginning of
+ * the vector, plus a word for each symbol for a zero at the
+ * end of the vector (for incremental linking).
+ */
+ set_sect_size = (2 * set_symbol_count + set_vector_count) *
+ sizeof (unsigned long);
+ set_vectors = (unsigned long *) xmalloc (set_sect_size);
+ setv_fill_count = 0;
+ }
- /* Compute total size of sections */
+ /* Pass 1: check and define symbols */
+ defined_global_sym_count = 0;
+ digest_pass1();
- each_file (consider_file_section_lengths, 0);
+ if (!relocatable_output) {
+ each_full_file(consider_relocation, 0); /* Text */
+ each_full_file(consider_relocation, 1); /* Data */
+ }
- /* If necessary, pad text section to full page in the file.
- Include the padding in the text segment size. */
+ /*
+ * Compute total size of sections.
+ * RRS data is the first output data section, RRS text is the last
+ * text section. Thus, DATA_START is calculated from RRS_DATA_START
+ * and RRS_DATA_SIZE, while RRS_TEXT_START is derived from TEXT_START
+ * and TEXT_SIZE.
+ */
+ consider_rrs_section_lengths();
+ each_full_file(consider_file_section_lengths, 0);
+ rrs_text_start = text_start + text_size;
+ text_size += rrs_text_size;
+ data_size += rrs_data_size;
+
+ /*
+ * If necessary, pad text section to full page in the file. Include
+ * the padding in the text segment size.
+ */
+
+ if (magic == ZMAGIC) {
+ int text_end = text_size + N_TXTOFF(outheader);
+ text_pad = PALIGN(text_end, page_size) - text_end;
+ text_size += text_pad;
+ }
+ outheader.a_text = text_size;
- if (magic == ZMAGIC || magic == QMAGIC)
- {
- int text_end = text_size + N_TXTOFF (outheader);
- text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
- text_size += text_pad;
- }
+ /*
+ * Make the data segment address start in memory on a suitable
+ * boundary.
+ */
-#ifdef _N_BASEADDR
- /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */
- outheader.a_entry = N_PAGSIZ (outheader);
-#endif
+ if (!Tdata_flag_specified)
+ rrs_data_start = text_start +
+ DATA_START(outheader) - TEXT_START(outheader);
- outheader.a_text = text_size;
-#ifdef sequent
- outheader.a_text += N_ADDRADJ (outheader);
+ data_start = rrs_data_start + rrs_data_size;
+ if (!relocatable_output) {
+ set_sect_start = rrs_data_start + data_size;
+ data_size += set_sect_size;
+ }
+ bss_start = rrs_data_start + data_size;
+
+#ifdef DEBUG
+printf("textstart = %#x, textsize = %#x, rrs_text_start = %#x, rrs_text_size %#x\n",
+ text_start, text_size, rrs_text_start, rrs_text_size);
+printf("datastart = %#x, datasize = %#x, rrs_data_start %#x, rrs_data_size %#x\n",
+ data_start, data_size, rrs_data_start, rrs_data_size);
+printf("bssstart = %#x, bsssize = %#x\n",
+ bss_start, bss_size);
#endif
- /* Make the data segment address start in memory on a suitable boundary. */
-
- if (! Tdata_flag_specified)
- data_start = N_DATADDR (outheader) + text_start - TEXT_START (outheader);
-
- /* Set up the set element vector */
-
- if (!relocatable_output)
- {
- /* The set sector size is the number of set elements + a word
- for each symbol for the length word at the beginning of the
- vector, plus a word for each symbol for a zero at the end of
- the vector (for incremental linking). */
- set_sect_size
- = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
- set_sect_start = data_start + data_size;
- data_size += set_sect_size;
- set_vectors = (unsigned long *) xmalloc (set_sect_size);
- setv_fill_count = 0;
- }
-
- /* Compute start addresses of each file's sections and symbols. */
-
- each_full_file (relocate_file_addresses, 0);
-
- /* Now, for each symbol, verify that it is defined globally at most once.
- Put the global value into the symbol entry.
- Common symbols are allocated here, in the BSS section.
- Each defined symbol is given a '->defined' field
- which is the correct N_ code for its definition,
- except in the case of common symbols with -r.
- Then make all the references point at the symbol entry
- instead of being chained together. */
-
- defined_global_sym_count = 0;
-
- for (i = 0; i < TABSIZE; i++)
- {
- register symbol *sp;
- for (sp = symtab[i]; sp; sp = sp->link)
- {
- /* For each symbol */
- register struct nlist *p, *next;
- int defs = 0, com = sp->max_common_size;
- struct nlist *first_definition;
- for (p = sp->refs; p; p = next)
- {
- register int type = p->n_type;
-
- if (SET_ELEMENT_P (type))
- {
- if (relocatable_output)
- fatal ("internal: global ref to set element with -r");
- if (!defs++)
- {
- sp->value = set_sect_start
- + setv_fill_count++ * sizeof (unsigned long);
- sp->defined = N_SETV | N_EXT;
- first_definition = p;
- }
- else if ((sp->defined & ~N_EXT) != N_SETV)
- {
- sp->multiply_defined = 1;
- multiple_def_count++;
- }
- set_vectors[setv_fill_count++] = p->n_value;
- }
- else if ((type & N_EXT) && type != (N_UNDF | N_EXT)
- && (type & N_TYPE) != N_FN)
- {
- /* non-common definition */
- if (defs++ && sp->value != p->n_value)
- {
- sp->multiply_defined = 1;
- multiple_def_count++;
- }
- sp->value = p->n_value;
- sp->defined = type;
- first_definition = p;
- }
- next = (struct nlist *) p->n_un.n_name;
- p->n_un.n_name = (char *) sp;
- }
- /* Allocate as common if defined as common and not defined for real */
- if (com && !defs)
- {
- if (!relocatable_output || force_common_definition)
- {
- int align = sizeof (int);
-
- /* Round up to nearest sizeof (int). I don't know
- whether this is necessary or not (given that
- alignment is taken care of later), but it's
- traditional, so I'll leave it in. Note that if
- this size alignment is ever removed, ALIGN above
- will have to be initialized to 1 instead of
- sizeof (int). */
-
- com = (com + sizeof (int) - 1) & (- sizeof (int));
+ /* Compute start addresses of each file's sections and symbols. */
- while (!(com & align))
- align <<= 1;
+ each_full_file(relocate_file_addresses, 0);
+ relocate_rrs_addresses();
- align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
-
- bss_size = ((((bss_size + data_size + data_start)
- + (align - 1)) & (- align))
- - data_size - data_start);
-
- sp->value = data_start + data_size + bss_size;
- sp->defined = N_BSS | N_EXT;
- bss_size += com;
- if (write_map)
- printf ("Allocating common %s: %x at %x\n",
- sp->name, com, sp->value);
- }
- else
- {
- sp->defined = 0;
- undefined_global_sym_count++;
- }
- }
- /* Set length word at front of vector and zero byte at end.
- Reverse the vector itself to put it in file order. */
- if ((sp->defined & ~N_EXT) == N_SETV)
- {
- unsigned long length_word_index
- = (sp->value - set_sect_start) / sizeof (unsigned long);
- unsigned long i, tmp;
-
- set_vectors[length_word_index]
- = setv_fill_count - 1 - length_word_index;
-
- /* Reverse the vector. */
- for (i = 1;
- i < (setv_fill_count - length_word_index - 1) / 2 + 1;
- i++)
- {
- tmp = set_vectors[length_word_index + i];
- set_vectors[length_word_index + i]
- = set_vectors[setv_fill_count - i];
- set_vectors[setv_fill_count - i] = tmp;
- }
+ /* Pass 2: assign values to symbols */
+ digest_pass2();
- set_vectors[setv_fill_count++] = 0;
- }
- if (sp->defined)
- defined_global_sym_count++;
+ if (end_symbol) { /* These are null if -r. */
+ etext_symbol->value = text_start + text_size - text_pad;
+ edata_symbol->value = rrs_data_start + data_size;
+ end_symbol->value = rrs_data_start + data_size + bss_size;
}
- }
-
- if (end_symbol) /* These are null if -r. */
- {
- etext_symbol->value = text_size + text_start;
- edata_symbol->value = data_start + data_size;
- end_symbol->value = data_start + data_size + bss_size;
- }
-
- /* Figure the data_pad now, so that it overlaps with the bss addresses. */
-
- if (specified_data_size && specified_data_size > data_size)
- data_pad = specified_data_size - data_size;
-
- if (magic == ZMAGIC || magic == QMAGIC)
- data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
- - data_size;
-
- bss_size -= data_pad;
- if (bss_size < 0) bss_size = 0;
+ /*
+ * Figure the data_pad now, so that it overlaps with the bss
+ * addresses.
+ */
- data_size += data_pad;
-}
-
-/* Accumulate the section sizes of input file ENTRY
- into the section sizes of the output file. */
+ if (specified_data_size && specified_data_size > data_size)
+ data_pad = specified_data_size - data_size;
-void
-consider_file_section_lengths (entry)
- register struct file_entry *entry;
-{
- if (entry->just_syms_flag)
- return;
-
- entry->text_start_address = text_size;
- /* If there were any vectors, we need to chop them off */
- text_size += entry->header.a_text;
- entry->data_start_address = data_size;
- data_size += entry->header.a_data;
- entry->bss_start_address = bss_size;
- bss_size += entry->header.a_bss;
-
- text_reloc_size += entry->header.a_trsize;
- data_reloc_size += entry->header.a_drsize;
-}
+ if (magic == ZMAGIC)
+ data_pad = PALIGN(data_pad + data_size, page_size) - data_size;
-/* 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. */
+ bss_size -= data_pad;
+ if (bss_size < 0)
+ bss_size = 0;
-void
-relocate_file_addresses (entry)
- register struct file_entry *entry;
-{
- entry->text_start_address += text_start;
- /* Note that `data_start' and `data_size' have not yet been
- adjusted for `data_pad'. If they had been, we would get the wrong
- results here. */
- entry->data_start_address += data_start;
- entry->bss_start_address += data_start + data_size;
-
- {
- register struct nlist *p;
- register struct nlist *end
- = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
-
- for (p = entry->symbols; p < end; p++)
- {
- /* If this belongs to a section, update it by the section's start address */
- register int type = p->n_type & N_TYPE;
-
- switch (type)
- {
- case N_TEXT:
- case N_SETT:
- p->n_value += entry->text_start_address;
- break;
- case N_DATA:
- case N_SETV:
- case N_SETD:
- /* A symbol whose value is in the data section
- is present in the input file as if the data section
- started at an address equal to the length of the file's text. */
- p->n_value += entry->data_start_address - entry->header.a_text;
- break;
- case N_BSS:
- case N_SETB:
- /* likewise for symbols with value in BSS. */
- p->n_value += entry->bss_start_address
- - entry->header.a_text - entry->header.a_data;
- break;
- }
- }
- }
+ data_size += data_pad;
}
-
-void describe_file_sections (), list_file_locals ();
-
-/* Print a complete or partial map of the output file. */
void
-print_symbols (outfile)
- FILE *outfile;
-{
- register int i;
-
- fprintf (outfile, "\nFiles:\n\n");
+digest_pass1()
+{
+
+ /*
+ * Now, for each symbol, verify that it is defined globally at most
+ * once within relocatable files (except when building a shared lib).
+ * and set the `defined' field if there is a definition.
+ *
+ * Then check the shared object symbol chain for any remaining
+ * undefined symbols. Set the `so_defined' field for any
+ * definition find this way.
+ */
+ FOR_EACH_SYMBOL(i, sp) {
+ struct localsymbol *lsp;
+ int defs = 0;
+
+ if (!sp->referenced) {
+#if 0
+ /* Check for undefined symbols in shared objects */
+ int type;
+ for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
+ type = lsp->nzlist.nlist.n_type;
+ if ((type & N_EXT) && type != (N_UNDF | N_EXT))
+ break;
+ }
+ if ((type & N_EXT) && type == (N_UNDF | N_EXT))
+ undefined_shobj_sym_count++;
+#endif
- each_file (describe_file_sections, outfile);
+ /* Superfluous symbol from shared object */
+ continue;
+ }
- fprintf (outfile, "\nGlobal symbols:\n\n");
+ if (sp == got_symbol || sp == dynamic_symbol)
+ continue;
+
+ for (lsp = sp->refs; lsp; lsp = lsp->next) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+ register int type = p->n_type;
+
+ if (SET_ELEMENT_P(type)) {
+ if (relocatable_output)
+ fatal(
+ "internal error: global ref to set el %s with -r",
+ sp->name);
+ if (!defs++) {
+ sp->defined = N_SETV | N_EXT;
+ sp->value =
+ setv_fill_count++ * sizeof(long);
+ } else if ((sp->defined & N_TYPE) != N_SETV) {
+ sp->multiply_defined = 1;
+ multiple_def_count++;
+ }
+ /* Keep count and remember symbol */
+ sp->setv_count++;
+ set_vectors[setv_fill_count++] = (long)p;
+
+ } else if ((type & N_EXT) && type != (N_UNDF | N_EXT)
+ && (type & N_TYPE) != N_FN
+ && (type & N_TYPE) != N_SIZE) {
+ /* non-common definition */
+ if (defs++ && sp->value != p->n_value
+ && entry_symbol) {
+ sp->multiply_defined = 1;
+ multiple_def_count++;
+ }
+ sp->def_nlist = p;
+ sp->defined = type;
+ }
+ }
- for (i = 0; i < TABSIZE; i++)
- {
- register symbol *sp;
- for (sp = symtab[i]; sp; sp = sp->link)
- {
- if (sp->defined == 1)
- fprintf (outfile, " %s: common, length 0x%x\n", sp->name, sp->max_common_size);
- if (sp->defined)
- fprintf (outfile, " %s: 0x%x\n", sp->name, sp->value);
- else if (sp->referenced)
- fprintf (outfile, " %s: undefined\n", sp->name);
- }
- }
+ if (sp->defined) {
+ if ((sp->defined & N_TYPE) == N_SETV)
+ /* Allocate zero entry in set vector */
+ setv_fill_count++;
+ defined_global_sym_count++;
+ continue;
+ }
- each_file (list_file_locals, outfile);
-}
+ if (relocatable_output)
+ /* We're done */
+ continue;
+
+ /*
+ * Still undefined, search the shared object symbols for a
+ * definition. This symbol must go into the RRS.
+ */
+ if (building_shared_object) {
+ /* Just punt for now */
+ undefined_global_sym_count--;
+ continue;
+ }
-void
-describe_file_sections (entry, outfile)
- struct file_entry *entry;
- FILE *outfile;
-{
- fprintf (outfile, " ");
- print_file_name (entry, outfile);
- if (entry->just_syms_flag)
- fprintf (outfile, " symbols only\n", 0);
- else
- fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
- entry->text_start_address, entry->header.a_text,
- entry->data_start_address, entry->header.a_data,
- entry->bss_start_address, entry->header.a_bss);
+ for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+ register int type = p->n_type;
+
+ if ((type & N_EXT) && type != (N_UNDF | N_EXT)
+ && (type & N_TYPE) != N_FN) {
+ /* non-common definition */
+ sp->def_nlist = p;
+ sp->so_defined = type;
+ undefined_global_sym_count--;
+#ifdef DEBUG
+printf("shr: %s gets defined to %x with value %x\n", sp->name, type, sp->value);
+#endif
+ break;
+ }
+ }
+ } END_EACH_SYMBOL;
}
void
-list_file_locals (entry, outfile)
- struct file_entry *entry;
- FILE *outfile;
-{
- register struct nlist
- *p,
- *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
+digest_pass2()
+{
+ /*
+ * Assign each symbol its final value.
+ * If not -r'ing, allocate common symbols in the BSS section.
+ */
+
+ FOR_EACH_SYMBOL(i, sp) {
+ int size;
+ int align = sizeof(int);
+
+ if (!sp->referenced)
+ continue;
+
+ if ((sp->defined & N_TYPE) == N_SETV) {
+ /*
+ * Set length word at front of vector and zero byte
+ * at end. Reverse the vector itself to put it in
+ * file order.
+ */
+ unsigned long i, tmp;
+ unsigned long length_word_index =
+ sp->value / sizeof(long);
+
+ /* Relocate symbol value */
+ sp->value += set_sect_start;
+
+ set_vectors[length_word_index] = sp->setv_count;
+
+ /*
+ * Relocate vector to final address.
+ */
+ for (i = 0; i < sp->setv_count; i++) {
+ struct nlist *p = (struct nlist *)
+ set_vectors[1+i+length_word_index];
+
+ set_vectors[1+i+length_word_index] = p->n_value;
+ }
+
+ /*
+ * Reverse the vector.
+ */
+ for (i = 1; i < (sp->setv_count - 1)/2 + 1; i++) {
+
+ tmp = set_vectors[length_word_index + i];
+ set_vectors[length_word_index + i] =
+ set_vectors[length_word_index + sp->setv_count + 1 - i];
+ set_vectors[length_word_index + sp->setv_count + 1 - i] = tmp;
+ }
+
+ /* Clear terminating entry */
+ set_vectors[length_word_index + sp->setv_count + 1] = 0;
+ continue;
+ }
- entry->strings = (char *) alloca (entry->string_size);
- read_entry_strings (file_open (entry), entry);
- fprintf (outfile, "\nLocal symbols of ");
- print_file_name (entry, outfile);
- fprintf (outfile, ":\n\n");
+ if (sp->defined && sp->def_nlist &&
+ ((sp->defined & ~N_EXT) != N_SETV))
+ sp->value = sp->def_nlist->n_value;
+
+ if (building_shared_object && !(link_mode & SYMBOLIC))
+ /* No common allocation in shared objects */
+ continue;
+
+ if ((size = sp->max_common_size) != 0) {
+ /*
+ * It's a common.
+ */
+ if (sp->defined != (N_UNDF + N_EXT))
+ fatal("%s: common isn't", sp->name);
+
+ } else if ((size = sp->size) != 0 && sp->defined == N_SIZE) {
+ /*
+ * It's data from shared object with size info.
+ */
+ if (sp->so_defined != (N_DATA + N_EXT))
+ fatal("%s: Bogus N_SIZE item", sp->name);
+
+ } else
+ /*
+ * It's neither
+ */
+ continue;
+
+
+ if (relocatable_output && !force_common_definition) {
+ sp->defined = 0;
+ undefined_global_sym_count++;
+ defined_global_sym_count--;
+ continue;
+ }
- for (p = entry->symbols; p < end; p++)
- /* If this is a definition,
- update it if necessary by this file's start address. */
- if (!(p->n_type & (N_STAB | N_EXT)))
- fprintf (outfile, " %s: 0x%x\n",
- entry->strings + p->n_un.n_strx, p->n_value);
+ /*
+ * Round up to nearest sizeof (int). I don't know whether
+ * this is necessary or not (given that alignment is taken
+ * care of later), but it's traditional, so I'll leave it in.
+ * Note that if this size alignment is ever removed, ALIGN
+ * above will have to be initialized to 1 instead of sizeof
+ * (int).
+ */
- entry->strings = 0; /* All done with them. */
-}
+ size = PALIGN(size, sizeof(int));
-
-/* Static vars for do_warnings and subroutines of it */
-int list_unresolved_refs; /* List unresolved refs */
-int list_warning_symbols; /* List warning syms */
-int list_multiple_defs; /* List multiple definitions */
+ while (!(size & align))
+ align <<= 1;
-/*
- * Structure for communication between do_file_warnings and it's
- * helper routines. Will in practice be an array of three of these:
- * 0) Current line, 1) Next line, 2) Source file info.
- */
-struct line_debug_entry
-{
- int line;
- char *filename;
- struct nlist *sym;
-};
+ align = align > MAX_ALIGNMENT ?
+ MAX_ALIGNMENT : align;
-void qsort ();
-/*
- * Helper routines for do_file_warnings.
- */
+ bss_size = PALIGN(bss_size + data_size + rrs_data_start, align)
+ - (data_size + rrs_data_start);
-/* Return an integer less than, equal to, or greater than 0 as per the
- relation between the two relocation entries. Used by qsort. */
+ sp->value = rrs_data_start + data_size + bss_size;
+ if (sp->defined == (N_UNDF | N_EXT))
+ sp->defined = N_BSS | N_EXT;
+ else {
+ sp->so_defined = 0;
+ defined_global_sym_count++;
+ }
+ bss_size += size;
+ if (write_map)
+ printf("Allocating %s %s: %x at %x\n",
+ sp->defined==(N_BSS|N_EXT)?"common":"data",
+ sp->name, size, sp->value);
-int
-relocation_entries_relation (rel1, rel2)
- struct relocation_info *rel1, *rel2;
-{
- return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
+ } END_EACH_SYMBOL;
}
-/* Moves to the next debugging symbol in the file. USE_DATA_SYMBOLS
- determines the type of the debugging symbol to look for (DSLINE or
- SLINE). STATE_POINTER keeps track of the old and new locatiosn in
- the file. It assumes that state_pointer[1] is valid; ie
- that it.sym points into some entry in the symbol table. If
- state_pointer[1].sym == 0, this routine should not be called. */
-
-int
-next_debug_entry (use_data_symbols, state_pointer)
- register int use_data_symbols;
- /* Next must be passed by reference! */
- struct line_debug_entry state_pointer[3];
-{
- register struct line_debug_entry
- *current = state_pointer,
- *next = state_pointer + 1,
- /* Used to store source file */
- *source = state_pointer + 2;
- struct file_entry *entry = (struct file_entry *) source->sym;
-
- current->sym = next->sym;
- current->line = next->line;
- current->filename = next->filename;
-
- while (++(next->sym) < (entry->symbols
- + entry->header.a_syms/sizeof (struct nlist)))
- {
- /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
- * may look negative...therefore, must mask to low bits
- */
- switch (next->sym->n_type & 0xff)
- {
- case N_SLINE:
- if (use_data_symbols) continue;
- next->line = next->sym->n_desc;
- return 1;
- case N_DSLINE:
- if (!use_data_symbols) continue;
- next->line = next->sym->n_desc;
- return 1;
-#ifdef HAVE_SUN_STABS
- case N_EINCL:
- next->filename = source->filename;
- continue;
-#endif
- case N_SO:
- source->filename = next->sym->n_un.n_strx + entry->strings;
- source->line++;
-#ifdef HAVE_SUN_STABS
- case N_BINCL:
-#endif
- case N_SOL:
- next->filename
- = next->sym->n_un.n_strx + entry->strings;
- default:
- continue;
+/*
+ * Scan relocation info in ENTRY for contributions to the dynamic section of
+ * the output file.
+ */
+void
+consider_relocation (entry, dataseg)
+ struct file_entry *entry;
+ int dataseg;
+{
+ struct relocation_info *reloc, *end;
+ struct localsymbol *lsp;
+ symbol *sp;
+
+ if (dataseg == 0) {
+ /* Text relocations */
+ reloc = entry->textrel;
+ end = entry->textrel + entry->ntextrel;
+ } else {
+ /* Data relocations */
+ reloc = entry->datarel;
+ end = entry->datarel + entry->ndatarel;
}
- }
- next->sym = (struct nlist *) 0;
- return 0;
-}
-
-/* Create a structure to save the state of a scan through the debug
- symbols. USE_DATA_SYMBOLS is set if we should be scanning for
- DSLINE's instead of SLINE's. entry is the file entry which points
- at the symbols to use. */
-struct line_debug_entry *
-init_debug_scan (use_data_symbols, entry)
- int use_data_symbols;
- struct file_entry *entry;
-{
- struct line_debug_entry
- *state_pointer
- = (struct line_debug_entry *)
- xmalloc (3 * sizeof (struct line_debug_entry));
- register struct line_debug_entry
- *current = state_pointer,
- *next = state_pointer + 1,
- *source = state_pointer + 2; /* Used to store source file */
-
- struct nlist *tmp;
-
- for (tmp = entry->symbols;
- tmp < (entry->symbols
- + entry->header.a_syms/sizeof (struct nlist));
- tmp++)
- if (tmp->n_type == (int) N_SO)
- break;
-
- if (tmp >= (entry->symbols
- + entry->header.a_syms/sizeof (struct nlist)))
- {
- /* I believe this translates to "We lose" */
- current->filename = next->filename = entry->filename;
- current->line = next->line = -1;
- current->sym = next->sym = (struct nlist *) 0;
- return state_pointer;
- }
-
- next->line = source->line = 0;
- next->filename = source->filename
- = (tmp->n_un.n_strx + entry->strings);
- source->sym = (struct nlist *) entry;
- next->sym = tmp;
-
- next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
-
- if (!next->sym) /* No line numbers for this section; */
- /* setup output results as appropriate */
- {
- if (source->line)
- {
- current->filename = source->filename = entry->filename;
- current->line = -1; /* Don't print lineno */
- }
- else
- {
- current->filename = source->filename;
- current->line = 0;
+ for (; reloc < end; reloc++) {
+
+ /*
+ * First, do the PIC specific relocs.
+ * r_relative and r_copy should not occur at this point
+ * (we do output them). The others break down to these
+ * combinations:
+ *
+ * jmptab: extern: needs jmp slot
+ * !extern: "intersegment" jump/call,
+ * should get resolved in output
+ *
+ * baserel: extern: need GOT entry
+ * !extern: may need GOT entry,
+ * machine dependent
+ *
+ * baserel's always refer to symbol through `r_symbolnum'
+ * whether extern or not. Internal baserels refer to statics
+ * that must be accessed either *through* the GOT table like
+ * global data, or by means of an offset from the GOT table.
+ * The macro RELOC_STATICS_THROUGH_GOT_P() determines which
+ * applies, since this is a machine (compiler?) dependent
+ * addressing mode.
+ */
+
+ if (RELOC_JMPTAB_P(reloc)) {
+
+ if (!RELOC_EXTERN_P(reloc))
+ continue;
+
+ lsp = &entry->symbols[reloc->r_symbolnum];
+ sp = lsp->symbol;
+ if (sp->alias)
+ sp = sp->alias;
+ alloc_rrs_jmpslot(sp);
+
+ } else if (RELOC_BASEREL_P(reloc)) {
+
+ lsp = &entry->symbols[reloc->r_symbolnum];
+ alloc_rrs_gotslot(reloc, lsp);
+
+ } else if (RELOC_EXTERN_P(reloc)) {
+
+ /*
+ * Non-PIC relocations.
+ * If the definition comes from a shared object
+ * we need a relocation entry in RRS.
+ *
+ * If the .so definition is N_TEXT a jmpslot is
+ * allocated.
+ *
+ * If it is N_DATA we allocate an address in BSS (?)
+ * and arrange for the data to be copied at run-time.
+ * The symbol is temporarily marked with N_SIZE in
+ * the `defined' field, so we know what to do in
+ * pass2() and during actual relocation. We convert
+ * the type back to something real again when writing
+ * out the symbols.
+ *
+ */
+ lsp = &entry->symbols[reloc->r_symbolnum];
+ sp = lsp->symbol;
+ if (sp == NULL)
+ fatal_with_file(
+ "internal error, sp==NULL", entry);
+
+ if (sp->alias)
+ sp = sp->alias;
+
+ /*
+ * Skip refs to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC
+ */
+ if (sp == got_symbol) {
+ if (!CHECK_GOT_RELOC(reloc))
+ fatal_with_file(
+ "Unexpected relocation type ", entry);
+ continue;
+ }
+
+ /*
+ * This symbol gives rise to a RRS entry
+ */
+
+ if (building_shared_object) {
+ alloc_rrs_reloc(sp);
+ continue;
+ }
+
+ if (force_alias_definition &&
+ sp->so_defined == N_TEXT + N_EXT) {
+
+ /* Call to shared library procedure */
+ alloc_rrs_jmpslot(sp);
+
+ } else if (sp->size &&
+ sp->so_defined == N_DATA + N_EXT) {
+
+ /* Reference to shared library data */
+ alloc_rrs_cpy_reloc(sp);
+ sp->defined = N_SIZE;
+
+ } else if (!sp->defined && sp->max_common_size == 0)
+ alloc_rrs_reloc(sp);
+
+ } else {
+ /*
+ * Segment relocation.
+ * Prepare an RRS relocation as these are load
+ * address dependent.
+ */
+ if (building_shared_object) {
+ alloc_rrs_segment_reloc(reloc);
+ }
+ }
}
- return state_pointer;
- }
-
-
- next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
-
- return state_pointer;
-}
-
-/* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
- which describes the current location in the implied scan through
- the debug symbols within the file which ADDRESS is within, and
- returns the source line number which corresponds to ADDRESS. */
-
-int
-address_to_line (address, state_pointer)
- unsigned long address;
- /* Next must be passed by reference! */
- struct line_debug_entry state_pointer[3];
-{
- struct line_debug_entry
- *current = state_pointer,
- *next = state_pointer + 1;
- struct line_debug_entry *tmp_pointer;
-
- int use_data_symbols;
-
- if (next->sym)
- use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
- else
- return current->line;
-
- /* Go back to the beginning if we've already passed it. */
- if (current->sym->n_value > address)
- {
- tmp_pointer = init_debug_scan (use_data_symbols,
- (struct file_entry *)
- ((state_pointer + 2)->sym));
- state_pointer[0] = tmp_pointer[0];
- state_pointer[1] = tmp_pointer[1];
- state_pointer[2] = tmp_pointer[2];
- free (tmp_pointer);
- }
-
- /* If we're still in a bad way, return -1, meaning invalid line. */
- if (current->sym->n_value > address)
- return -1;
-
- while (next->sym
- && next->sym->n_value <= address
- && next_debug_entry (use_data_symbols, state_pointer))
- ;
- return current->line;
}
-
-/* Macros for manipulating bitvectors. */
-#define BIT_SET_P(bv, index) ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
-#define SET_BIT(bv, index) ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
-
-/* This routine will scan through the relocation data of file ENTRY,
- printing out references to undefined symbols and references to
- symbols defined in files with N_WARNING symbols. If DATA_SEGMENT
- is non-zero, it will scan the data relocation segment (and use
- N_DSLINE symbols to track line number); otherwise it will scan the
- text relocation segment. Warnings will be printed on the output
- stream OUTFILE. Eventually, every nlist symbol mapped through will
- be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
- we scan the nlists themselves. */
-
-do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
- struct file_entry *entry;
- int data_segment;
- FILE *outfile;
- unsigned char *nlist_bitvector;
+/*
+ * Accumulate the section sizes of input file ENTRY into the section sizes of
+ * the output file.
+ */
+void
+consider_file_section_lengths (entry)
+ register struct file_entry *entry;
{
- struct relocation_info
- *reloc_start = data_segment ? entry->datarel : entry->textrel,
- *reloc;
- int reloc_size
- = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
- / sizeof (struct relocation_info));
- int start_of_segment
- = (data_segment ? entry->data_start_address : entry->text_start_address);
- struct nlist *start_of_syms = entry->symbols;
- struct line_debug_entry *state_pointer
- = init_debug_scan (data_segment != 0, entry);
- register struct line_debug_entry
- *current = state_pointer;
- /* Assigned to generally static values; should not be written into. */
- char *errfmt;
- /* Assigned to alloca'd values cand copied into; should be freed
- when done. */
- char *errmsg;
- int invalidate_line_number;
-
- /* We need to sort the relocation info here. Sheesh, so much effort
- for one lousy error optimization. */
-
- qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
- relocation_entries_relation);
-
- for (reloc = reloc_start;
- reloc < (reloc_start + reloc_size);
- reloc++)
- {
- register struct nlist *s;
- register symbol *g;
-
- /* If the relocation isn't resolved through a symbol, continue */
- if (!RELOC_EXTERN_P(reloc))
- continue;
-
- s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
-
- /* Local symbols shouldn't ever be used by relocation info, so
- the next should be safe.
- This is, of course, wrong. References to local BSS symbols can be
- the targets of relocation info, and they can (must) be
- resolved through symbols. However, these must be defined properly,
- (the assembler would have caught it otherwise), so we can
- ignore these cases. */
- if (!(s->n_type & N_EXT))
- continue;
-
- g = (symbol *) s->n_un.n_name;
- errmsg = 0;
-
- if (!g->defined && list_unresolved_refs) /* Reference */
- {
- /* Mark as being noted by relocation warning pass. */
- SET_BIT (nlist_bitvector, s - start_of_syms);
-
- if (g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */
- continue;
-
- /* Undefined symbol which we should mention */
-
- if (++(g->undef_refs) == MAX_UREFS_PRINTED)
- {
- errfmt = "More undefined symbol %s refs follow";
- invalidate_line_number = 1;
- }
- else
- {
- errfmt = "Undefined symbol %s referenced from %s segment";
- invalidate_line_number = 0;
- }
- }
- else /* Defined */
- {
- /* Potential symbol warning here */
- if (!g->warning) continue;
-
- /* Mark as being noted by relocation warning pass. */
- SET_BIT (nlist_bitvector, s - start_of_syms);
-
- errfmt = 0;
- errmsg = g->warning;
- invalidate_line_number = 0;
- }
-
+ if (entry->just_syms_flag)
+ return;
- /* If errfmt == 0, errmsg has already been defined. */
- if (errfmt != 0)
- {
- char *nm;
-
- if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
- nm = g->name;
- errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
- sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
- if (nm != g->name)
- free (nm);
- }
-
- address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
- state_pointer);
+ entry->text_start_address = text_size;
+ /* If there were any vectors, we need to chop them off */
+ text_size += entry->header.a_text;
+ entry->data_start_address = data_size;
+ data_size += entry->header.a_data;
+ entry->bss_start_address = bss_size;
+ bss_size += entry->header.a_bss;
- if (current->line >=0)
- fprintf (outfile, "%s:%d: %s\n", current->filename,
- invalidate_line_number ? 0 : current->line, errmsg);
- else
- fprintf (outfile, "%s: %s\n", current->filename, errmsg);
-
- if (errfmt != 0)
- free (errmsg);
- }
-
- free (state_pointer);
+ text_reloc_size += entry->header.a_trsize;
+ data_reloc_size += entry->header.a_drsize;
}
-/* Print on OUTFILE a list of all warnings generated by references
- and/or definitions in the file ENTRY. List source file and line
- number if possible, just the .o file if not. */
-
+/*
+ * 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.
+ */
void
-do_file_warnings (entry, outfile)
- struct file_entry *entry;
- FILE *outfile;
+relocate_file_addresses (entry)
+ register struct file_entry *entry;
{
- int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
- unsigned char *nlist_bitvector
- = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
- struct line_debug_entry *text_scan, *data_scan;
- int i;
- char *errfmt, *file_name;
- int line_number;
- int dont_allow_symbol_name;
-
- bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
-
- /* Read in the files strings if they aren't available */
- if (!entry->strings)
- {
- int desc;
-
- entry->strings = (char *) alloca (entry->string_size);
- desc = file_open (entry);
- read_entry_strings (desc, entry);
- }
-
- read_file_relocation (entry);
-
- /* Do text warnings based on a scan through the relocation info. */
- do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
-
- /* Do data warnings based on a scan through the relocation info. */
- do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
-
- /* Scan through all of the nlist entries in this file and pick up
- anything that the scan through the relocation stuff didn't. */
-
- text_scan = init_debug_scan (0, entry);
- data_scan = init_debug_scan (1, entry);
-
- for (i = 0; i < number_of_syms; i++)
- {
- struct nlist *s;
- struct glosym *g;
-
- s = entry->symbols + i;
-
- if (!(s->n_type & N_EXT))
- continue;
-
- g = (symbol *) s->n_un.n_name;
- dont_allow_symbol_name = 0;
-
- if (list_multiple_defs && g->multiply_defined)
- {
- errfmt = "Definition of symbol %s (multiply defined)";
- switch (s->n_type)
- {
- case N_TEXT | N_EXT:
- line_number = address_to_line (s->n_value, text_scan);
- file_name = text_scan[0].filename;
- break;
- case N_DATA | N_EXT:
- line_number = address_to_line (s->n_value, data_scan);
- file_name = data_scan[0].filename;
- break;
- case N_SETA | N_EXT:
- case N_SETT | N_EXT:
- case N_SETD | N_EXT:
- case N_SETB | N_EXT:
- if (g->multiply_defined == 2)
- continue;
- errfmt = "First set element definition of symbol %s (multiply defined)";
- break;
- default:
- continue; /* Don't print out multiple defs
- at references. */
- }
- }
- else if (BIT_SET_P (nlist_bitvector, i))
- continue;
- else if (list_unresolved_refs && !g->defined)
- {
- if (g->undef_refs >= MAX_UREFS_PRINTED)
- continue;
-
- if (++(g->undef_refs) == MAX_UREFS_PRINTED)
- errfmt = "More undefined \"%s\" refs follow";
- else
- errfmt = "Undefined symbol \"%s\" referenced";
- line_number = -1;
- }
- else if (g->warning)
- {
- /* There are two cases in which we don't want to
- do this. The first is if this is a definition instead of
- a reference. The second is if it's the reference used by
- the warning stabs itself. */
- if (s->n_type != (N_EXT | N_UNDF)
- || (i && (s-1)->n_type == N_WARNING))
- continue;
-
- errfmt = g->warning;
- line_number = -1;
- dont_allow_symbol_name = 1;
- }
- else
- continue;
-
- if (line_number == -1)
- fprintf (outfile, "%s: ", entry->filename);
- else
- fprintf (outfile, "%s:%d: ", file_name, line_number);
+ register struct localsymbol *lsp, *lspend;
+
+ entry->text_start_address += text_start;
+ /*
+ * Note that `data_start' and `data_size' have not yet been
+ * adjusted for `data_pad'. If they had been, we would get the wrong
+ * results here.
+ */
+ entry->data_start_address += data_start;
+ entry->bss_start_address += bss_start;
+#ifdef DEBUG
+printf("%s: datastart: %#x, bss %#x\n", get_file_name(entry),
+ entry->data_start_address, entry->bss_start_address);
+#endif
- if (dont_allow_symbol_name)
- fprintf (outfile, "%s", errfmt);
- else
- {
- char *nm;
- if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL)
- {
- fprintf (outfile, errfmt, nm);
- free (nm);
- }
- else
- fprintf (outfile, errfmt, g->name);
+ lspend = entry->symbols + entry->nsymbols;
+
+ for (lsp = entry->symbols; lsp < lspend; lsp++) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+ /*
+ * If this belongs to a section, update it
+ * by the section's start address
+ */
+ register int type = p->n_type & N_TYPE;
+
+ switch (type) {
+ case N_TEXT:
+ case N_SETT:
+ p->n_value += entry->text_start_address;
+ break;
+ case N_DATA:
+ case N_SETD:
+ case N_SETV:
+ /*
+ * A symbol whose value is in the data section is
+ * present in the input file as if the data section
+ * started at an address equal to the length of the
+ * file's text.
+ */
+ p->n_value += entry->data_start_address -
+ entry->header.a_text;
+ break;
+ case N_BSS:
+ case N_SETB:
+ /* likewise for symbols with value in BSS. */
+ p->n_value += entry->bss_start_address
+ - entry->header.a_text - entry->header.a_data;
+ break;
+ }
}
-
- fputc ('\n', outfile);
- }
- free (text_scan);
- free (data_scan);
- entry->strings = 0; /* Since it will dissapear anyway. */
-}
-
-do_warnings (outfile)
- FILE *outfile;
-{
- list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
- list_warning_symbols = warning_count;
- list_multiple_defs = multiple_def_count != 0;
-
- if (!(list_unresolved_refs ||
- list_warning_symbols ||
- list_multiple_defs ))
- /* No need to run this routine */
- return;
-
- each_file (do_file_warnings, outfile);
-
- if (list_unresolved_refs || list_multiple_defs)
- make_executable = 0;
}
/* Write the output file */
@@ -3200,168 +1879,117 @@ do_warnings (outfile)
void
write_output ()
{
- struct stat statbuf;
- int filemode;
+ struct stat statbuf;
+ int filemode;
+
+ if (lstat(output_filename, &statbuf) != -1) {
+ if (!S_ISDIR(statbuf.st_mode))
+ (void)unlink(output_filename);
+ }
- (void) unlink (output_filename);
- outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
- if (outdesc < 0) perror_name (output_filename);
+ outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+ if (outdesc < 0)
+ perror_name (output_filename);
- if (fstat (outdesc, &statbuf) < 0)
- perror_name (output_filename);
+ if (fstat (outdesc, &statbuf) < 0)
+ perror_name (output_filename);
- filemode = statbuf.st_mode;
+ filemode = statbuf.st_mode;
- chmod (output_filename, filemode & ~0111);
+ chmod (output_filename, filemode & ~0111);
- /* Output the a.out header. */
- write_header ();
+ /* Output the a.out header. */
+ write_header ();
- /* Output the text and data segments, relocating as we go. */
- write_text ();
- write_data ();
+ /* Output the text and data segments, relocating as we go. */
+ write_text ();
+ write_data ();
- /* Output the merged relocation info, if requested with `-r'. */
- if (relocatable_output)
- write_rel ();
+ /* Output the merged relocation info, if requested with `-r'. */
+ if (relocatable_output)
+ write_rel ();
- /* Output the symbol table (both globals and locals). */
- write_syms ();
+ /* Output the symbol table (both globals and locals). */
+ write_syms ();
- /* Copy any GDB symbol segments from input files. */
- write_symsegs ();
+ /* Output the RSS section */
+ write_rrs ();
- close (outdesc);
+ close (outdesc);
- if (chmod (output_filename, filemode | 0111) == -1)
- perror_name (output_filename);
+ if (chmod (output_filename, filemode | 0111) == -1)
+ perror_name (output_filename);
}
void modify_location (), perform_relocation (), copy_text (), copy_data ();
+/* Total number of symbols to be written in the output file. */
+int nsyms;
+
void
write_header ()
{
- N_SET_MAGIC (outheader, magic);
- outheader.a_text = text_size;
-#ifdef sequent
- outheader.a_text += N_ADDRADJ (outheader);
- if (entry_symbol == 0)
- entry_symbol = getsym("start");
-#endif
- outheader.a_data = data_size;
- outheader.a_bss = bss_size;
- outheader.a_entry = (entry_symbol ? entry_symbol->value
- : text_start + entry_offset);
-#ifdef COFF_ENCAPSULATE
- if (need_coff_header)
- {
- /* We are encapsulating BSD format within COFF format. */
- struct coffscn *tp, *dp, *bp;
-
- tp = &coffheader.scns[0];
- dp = &coffheader.scns[1];
- bp = &coffheader.scns[2];
-
- strcpy (tp->s_name, ".text");
- tp->s_paddr = text_start;
- tp->s_vaddr = text_start;
- tp->s_size = text_size;
- tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
- tp->s_relptr = 0;
- tp->s_lnnoptr = 0;
- tp->s_nreloc = 0;
- tp->s_nlnno = 0;
- tp->s_flags = 0x20;
- strcpy (dp->s_name, ".data");
- dp->s_paddr = data_start;
- dp->s_vaddr = data_start;
- dp->s_size = data_size;
- dp->s_scnptr = tp->s_scnptr + tp->s_size;
- dp->s_relptr = 0;
- dp->s_lnnoptr = 0;
- dp->s_nreloc = 0;
- dp->s_nlnno = 0;
- dp->s_flags = 0x40;
- strcpy (bp->s_name, ".bss");
- bp->s_paddr = dp->s_vaddr + dp->s_size;
- bp->s_vaddr = bp->s_paddr;
- bp->s_size = bss_size;
- bp->s_scnptr = 0;
- bp->s_relptr = 0;
- bp->s_lnnoptr = 0;
- bp->s_nreloc = 0;
- bp->s_nlnno = 0;
- bp->s_flags = 0x80;
-
- coffheader.f_magic = COFF_MAGIC;
- coffheader.f_nscns = 3;
- /* store an unlikely time so programs can
- * tell that there is a bsd header
- */
- coffheader.f_timdat = 1;
- coffheader.f_symptr = 0;
- coffheader.f_nsyms = 0;
- coffheader.f_opthdr = 28;
- coffheader.f_flags = 0x103;
- /* aouthdr */
- coffheader.magic = ZMAGIC;
- coffheader.vstamp = 0;
- coffheader.tsize = tp->s_size;
- coffheader.dsize = dp->s_size;
- coffheader.bsize = bp->s_size;
- coffheader.entry = outheader.a_entry;
- coffheader.text_start = tp->s_vaddr;
- coffheader.data_start = dp->s_vaddr;
- }
-#endif
+ int flags = (rrs_section_type == RRS_FULL) ? EX_DYNAMIC : 0;
+
+ N_SET_FLAG (outheader, flags);
+ outheader.a_text = text_size;
+ outheader.a_data = data_size;
+ outheader.a_bss = bss_size;
+ outheader.a_entry = (entry_symbol ? entry_symbol->value
+ : text_start + entry_offset);
+
+ 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++;
+ }
-#ifdef INITIALIZE_HEADER
- INITIALIZE_HEADER;
-#endif
+ if (strip_symbols == STRIP_NONE)
+ nsyms += debugger_sym_count;
- 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;
- /* One extra for following reference on indirects */
- if (relocatable_output)
- nsyms += set_symbol_count + global_indirect_count;
- }
-
- if (strip_symbols == STRIP_NONE)
- nsyms += debugger_sym_count;
-
- outheader.a_syms = nsyms * sizeof (struct nlist);
-
- if (relocatable_output)
- {
- outheader.a_trsize = text_reloc_size;
- outheader.a_drsize = data_reloc_size;
- }
- else
- {
- outheader.a_trsize = 0;
- outheader.a_drsize = 0;
- }
-
-#ifdef COFF_ENCAPSULATE
- if (need_coff_header)
- mywrite (&coffheader, sizeof coffheader, 1, outdesc);
+#ifdef DEBUG
+printf("defined globals: %d, undefined globals %d, locals: %d (non_L: %d), \
+debug symbols: %d, special: %d --> nsyms %d\n",
+ defined_global_sym_count, undefined_global_sym_count,
+ local_sym_count, non_L_local_sym_count, debugger_sym_count,
+ special_sym_count, nsyms);
#endif
- mywrite (&outheader, sizeof (struct exec), 1, outdesc);
- /* Output whatever padding is required in the executable file
- between the header and the start of the text. */
+ outheader.a_syms = nsyms * sizeof (struct nlist);
+
+ if (relocatable_output) {
+ outheader.a_trsize = text_reloc_size;
+ outheader.a_drsize = data_reloc_size;
+ } else {
+ outheader.a_trsize = 0;
+ outheader.a_drsize = 0;
+ }
+
+ md_swapout_exec_hdr(&outheader);
+ mywrite (&outheader, sizeof (struct exec), 1, outdesc);
+ md_swapin_exec_hdr(&outheader);
+
+ /*
+ * Output whatever padding is required in the executable file
+ * between the header and the start of the text.
+ */
#ifndef COFF_ENCAPSULATE
- padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
+ padfile (N_TXTOFF(outheader) - sizeof outheader, outdesc);
#endif
}
@@ -3371,125 +1999,55 @@ write_header ()
void
write_text ()
{
- if (trace_files)
- fprintf (stderr, "Copying and relocating text:\n\n");
-
- each_full_file (copy_text, 0);
- file_close ();
-
- if (trace_files)
- fprintf (stderr, "\n");
-
- padfile (text_pad, outdesc);
-}
-
-int
-text_offset (entry)
- struct file_entry *entry;
-{
- return entry->starting_offset + N_TXTOFF (entry->header);
-}
-/* Read in all of the relocation information */
+ if (trace_files)
+ fprintf (stderr, "Copying and relocating text:\n\n");
-void
-read_relocation ()
-{
- each_full_file (read_file_relocation, 0);
-}
+ each_full_file (copy_text, 0);
+ file_close ();
-/* Read in the relocation sections of ENTRY if necessary */
+ if (trace_files)
+ fprintf (stderr, "\n");
-void
-read_file_relocation (entry)
- struct file_entry *entry;
-{
- register struct relocation_info *reloc;
- int desc;
- int read_return;
-
- desc = -1;
- if (!entry->textrel)
- {
- reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
- desc = file_open (entry);
- lseek (desc,
- text_offset (entry) + entry->header.a_text + entry->header.a_data,
- L_SET);
- if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
- {
- fprintf (stderr, "Return from read: %d\n", read_return);
- fatal_with_file ("premature eof in text relocation of ", entry);
- }
- entry->textrel = reloc;
- }
-
- if (!entry->datarel)
- {
- reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
- if (desc == -1) desc = file_open (entry);
- lseek (desc,
- text_offset (entry) + entry->header.a_text
- + entry->header.a_data + entry->header.a_trsize,
- L_SET);
- if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
- fatal_with_file ("premature eof in data relocation of ", entry);
- entry->datarel = reloc;
- }
+ padfile (text_pad, outdesc);
}
-/* Read the text segment contents of ENTRY, relocate them,
- and write the result to the output file.
- If `-r', save the text relocation for later reuse. */
-
+/*
+ * Read the text segment contents of ENTRY, relocate them, and write the
+ * result to the output file. If `-r', save the text relocation for later
+ * reuse.
+ */
void
copy_text (entry)
struct file_entry *entry;
{
- register char *bytes;
- register int desc;
- register struct relocation_info *reloc;
-
- if (trace_files)
- prline_file_name (entry, stderr);
-
- desc = file_open (entry);
-
- /* Allocate space for the file's text section */
+ register char *bytes;
+ register int desc;
- bytes = (char *) alloca (entry->header.a_text);
+ if (trace_files)
+ prline_file_name (entry, stderr);
- /* Deal with relocation information however is appropriate */
+ desc = file_open (entry);
- if (entry->textrel) reloc = entry->textrel;
- else if (relocatable_output)
- {
- read_file_relocation (entry);
- reloc = entry->textrel;
- }
- else
- {
- reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
- lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
- if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
- fatal_with_file ("premature eof in text relocation of ", entry);
- }
+ /* Allocate space for the file's text section */
+ bytes = (char *) alloca (entry->header.a_text);
- /* Read the text section into core. */
+ /* Deal with relocation information however is appropriate */
+ if (entry->textrel == NULL)
+ fatal_with_file("no text relocation of ", entry);
- lseek (desc, text_offset (entry), 0);
- if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
- fatal_with_file ("premature eof in text section of ", entry);
+ /* Read the text section into core. */
+ lseek (desc, text_offset (entry), 0);
+ if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
+ fatal_with_file ("premature eof in text section of ", entry);
- /* Relocate the text according to the text relocation. */
+ /* Relocate the text according to the text relocation. */
+ perform_relocation (bytes, entry->header.a_text,
+ entry->textrel, entry->ntextrel, entry, 0);
- perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
- reloc, entry->header.a_trsize, entry);
-
- /* Write the relocated text to the output file. */
-
- mywrite (bytes, 1, entry->header.a_text, outdesc);
+ /* Write the relocated text to the output file. */
+ mywrite (bytes, 1, entry->header.a_text, outdesc);
}
/* Relocate the data segment of each input file
@@ -3498,298 +2056,309 @@ copy_text (entry)
void
write_data ()
{
- if (trace_files)
- fprintf (stderr, "Copying and relocating data:\n\n");
+ long pos;
- each_full_file (copy_data, 0);
- file_close ();
+ if (trace_files)
+ fprintf (stderr, "Copying and relocating data:\n\n");
- /* Write out the set element vectors. See digest symbols for
- description of length of the set vector section. */
+ pos = N_DATOFF(outheader) + data_start - rrs_data_start;
+ if (lseek(outdesc, pos, L_SET) != pos)
+ fatal("write_data: lseek: cant position data offset");
- if (set_vector_count)
- mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
- sizeof (unsigned long), outdesc);
+ each_full_file (copy_data, 0);
+ file_close ();
- if (trace_files)
- fprintf (stderr, "\n");
+ /*
+ * Write out the set element vectors. See digest symbols for
+ * description of length of the set vector section.
+ */
- padfile (data_pad, outdesc);
-}
+ if (set_vector_count) {
+ swap_longs(set_vectors, 2 * set_symbol_count + set_vector_count);
+ mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
+ sizeof (unsigned long), outdesc);
+ }
-/* Read the data segment contents of ENTRY, relocate them,
- and write the result to the output file.
- If `-r', save the data relocation for later reuse.
- See comments in `copy_text'. */
+ if (trace_files)
+ fprintf (stderr, "\n");
-void
-copy_data (entry)
- struct file_entry *entry;
-{
- register struct relocation_info *reloc;
- register char *bytes;
- register int desc;
-
- if (trace_files)
- prline_file_name (entry, stderr);
-
- desc = file_open (entry);
-
- bytes = (char *) alloca (entry->header.a_data);
-
- if (entry->datarel) reloc = entry->datarel;
- else if (relocatable_output) /* Will need this again */
- {
- read_file_relocation (entry);
- reloc = entry->datarel;
- }
- else
- {
- reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
- lseek (desc, text_offset (entry) + entry->header.a_text
- + entry->header.a_data + entry->header.a_trsize,
- 0);
- if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
- fatal_with_file ("premature eof in data relocation of ", entry);
- }
-
- lseek (desc, text_offset (entry) + entry->header.a_text, 0);
- if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
- fatal_with_file ("premature eof in data section of ", entry);
-
- perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
- entry->header.a_data, reloc, entry->header.a_drsize, entry);
-
- mywrite (bytes, 1, entry->header.a_data, outdesc);
+ padfile (data_pad, outdesc);
}
-
-/* Relocate ENTRY's text or data section contents.
- DATA is the address of the contents, in core.
- DATA_SIZE is the length of the contents.
- PC_RELOCATION is the difference between the address of the contents
- in the output file and its address in the input file.
- RELOC_INFO is the address of the relocation info, in core.
- RELOC_SIZE is its length in bytes. */
-/* This version is about to be severly hacked by Randy. Hope it
- works afterwards. */
+
+/*
+ * Read the data segment contents of ENTRY, relocate them, and write the
+ * result to the output file. If `-r', save the data relocation for later
+ * reuse. See comments in `copy_text'.
+ */
void
-perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
- char *data;
- struct relocation_info *reloc_info;
+copy_data (entry)
struct file_entry *entry;
- int pc_relocation;
- int data_size;
- int reloc_size;
{
- register struct relocation_info *p = reloc_info;
- struct relocation_info *end
- = reloc_info + reloc_size / sizeof (struct relocation_info);
- int text_relocation = entry->text_start_address;
- int data_relocation = entry->data_start_address - entry->header.a_text;
- int bss_relocation
- = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
-
- for (; p < end; p++)
- {
- register int relocation = 0;
- register int addr = RELOC_ADDRESS(p);
- register unsigned int mask = 0;
-
- if (addr >= data_size)
- fatal_with_file ("relocation address out of range in ", entry);
-
- if (RELOC_EXTERN_P(p))
- {
- int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
- symbol *sp = ((symbol *)
- (((struct nlist *)
- (((char *)entry->symbols) + symindex))
- ->n_un.n_name));
-
-#ifdef N_INDR
- /* Resolve indirection */
- if ((sp->defined & ~N_EXT) == N_INDR)
- sp = (symbol *) sp->value;
-#endif
+ register char *bytes;
+ register int desc;
- if (symindex >= entry->header.a_syms)
- fatal_with_file ("relocation symbolnum out of range in ", entry);
+ if (trace_files)
+ prline_file_name (entry, stderr);
- /* If the symbol is undefined, leave it at zero. */
- if (! sp->defined)
- relocation = 0;
- else
- relocation = sp->value;
- }
- else switch (RELOC_TYPE(p))
- {
- case N_TEXT:
- case N_TEXT | N_EXT:
- relocation = text_relocation;
- break;
-
- case N_DATA:
- case N_DATA | N_EXT:
- /* A word that points to beginning of the the data section
- initially contains not 0 but rather the "address" of that section
- in the input file, which is the length of the file's text. */
- relocation = data_relocation;
- break;
-
- case N_BSS:
- case N_BSS | N_EXT:
- /* Similarly, an input word pointing to the beginning of the bss
- initially contains the length of text plus data of the file. */
- relocation = bss_relocation;
- break;
-
- case N_ABS:
- case N_ABS | N_EXT:
- /* Don't know why this code would occur, but apparently it does. */
- break;
+ desc = file_open (entry);
- default:
- fatal_with_file ("nonexternal relocation code invalid in ", entry);
- }
+ bytes = (char *)alloca(entry->header.a_data);
-#ifdef RELOC_ADD_EXTRA
- relocation += RELOC_ADD_EXTRA(p);
- if (relocatable_output)
- {
- /* Non-PC relative relocations which are absolute
- or which have become non-external now have fixed
- relocations. Set the ADD_EXTRA of this relocation
- to be the relocation we have now determined. */
- if (! RELOC_PCREL_P (p))
- {
- if ((int)p->r_type <= RELOC_32
- || RELOC_EXTERN_P (p) == 0)
- RELOC_ADD_EXTRA (p) = relocation;
- }
- /* External PC-relative relocations continue to move around;
- update their relocations by the amount they have moved
- so far. */
- else if (RELOC_EXTERN_P (p))
- RELOC_ADD_EXTRA (p) -= pc_relocation;
- continue;
- }
-#endif
+ if (entry->datarel == NULL)
+ fatal_with_file("no data relocation of ", entry);
- if (RELOC_PCREL_P(p))
- relocation -= pc_relocation;
+ lseek (desc, text_offset (entry) + entry->header.a_text, 0);
+ if (entry->header.a_data != read(desc, bytes, entry->header.a_data))
+ fatal_with_file ("premature eof in data section of ", entry);
- relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
+ perform_relocation (bytes, entry->header.a_data,
+ entry->datarel, entry->ndatarel, entry, 1);
- /* Unshifted mask for relocation */
- mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
- mask |= mask - 1;
- relocation &= mask;
+ mywrite (bytes, 1, entry->header.a_data, outdesc);
+}
+
+/*
+ * Relocate ENTRY's text or data section contents. DATA is the address of the
+ * contents, in core. DATA_SIZE is the length of the contents. PC_RELOCATION
+ * is the difference between the address of the contents in the output file
+ * and its address in the input file. RELOC is the address of the
+ * relocation info, in core. NRELOC says how many there are.
+ */
+void
+perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
+ char *data;
+ int data_size;
+ struct relocation_info *reloc;
+ int nreloc;
+ struct file_entry *entry;
+ int dataseg;
+{
+ register struct relocation_info *r = reloc;
+ struct relocation_info *end = reloc + nreloc;
+
+ text_relocation = entry->text_start_address;
+ data_relocation = entry->data_start_address - entry->header.a_text;
+ bss_relocation = entry->bss_start_address -
+ entry->header.a_text - entry->header.a_data;
+ pc_relocation = dataseg?
+ entry->data_start_address - entry->header.a_text:
+ entry->text_start_address;
+
+ for (; r < end; r++) {
+ int addr = RELOC_ADDRESS(r);
+ long addend = md_get_addend(r, data+addr);
+ long relocation;
+
+ /*
+ * Loop over the relocations again as we did in
+ * consider_relocation(), claiming the reserved RRS
+ * relocations.
+ */
+
+ if (addr >= data_size)
+ fatal_with_file(
+ "relocation address out of range in ", entry);
+
+ if (RELOC_JMPTAB_P(r)) {
+
+ int symindex = RELOC_SYMBOL(r);
+ struct localsymbol *lsp = &entry->symbols[symindex];
+ symbol *sp;
+
+ if (symindex >= entry->nsymbols)
+ fatal_with_file(
+ "relocation symbolnum out of range in ", entry);
+
+ sp = lsp->symbol;
+ if (sp->alias)
+ sp = sp->alias;
+
+ if (relocatable_output)
+ relocation = addend;
+ else if (!RELOC_EXTERN_P(r)) {
+ relocation = addend +
+ data_relocation - text_relocation;
+ } else
+ relocation = addend +
+ claim_rrs_jmpslot(r, sp, addend);
+
+ } else if (RELOC_BASEREL_P(r)) {
+
+ int symindex = RELOC_SYMBOL(r);
+ struct localsymbol *lsp = &entry->symbols[symindex];
+
+ if (symindex >= entry->nsymbols)
+ fatal_with_file(
+ "relocation symbolnum out of range in ", entry);
+
+ if (relocatable_output)
+ relocation = addend;
+ else if (!RELOC_EXTERN_P(r))
+ relocation = claim_rrs_internal_gotslot(entry,
+ r, lsp, addend);
+ else
+ relocation = claim_rrs_gotslot(r, lsp, addend);
+
+ } else if (RELOC_EXTERN_P(r)) {
+
+ int symindex = RELOC_SYMBOL(r);
+ symbol *sp;
+
+ if (symindex >= entry->nsymbols)
+ fatal_with_file(
+ "relocation symbolnum out of range in ", entry);
+
+ sp = entry->symbols[symindex].symbol;
+ if (sp->alias)
+ sp = sp->alias;
+
+ if (relocatable_output) {
+ relocation = addend + sp->value;
+ } else if (sp->defined) {
+ if (sp == got_symbol) {
+ /* Handle _GOT_ refs */
+ relocation = addend + sp->value
+ + md_got_reloc(r);
+ } else if (building_shared_object) {
+ /*
+ * Normal (non-PIC) relocation needs
+ * to be converted into an RRS reloc
+ * when building a shared object.
+ */
+ r->r_address += dataseg?
+ entry->data_start_address:
+ entry->text_start_address;
+ relocation = addend;
+ if (claim_rrs_reloc(r, sp, &relocation))
+ continue;
+ } else if (sp->defined == N_SIZE) {
+ /*
+ * If size is known, arrange a
+ * run-time copy.
+ */
+ if (!sp->size)
+ fatal("Copy item isn't: %s",
+ sp->name);
+
+ relocation = addend + sp->value;
+ r->r_address = sp->value;
+ claim_rrs_cpy_reloc(r, sp);
+ } else
+ /* Plain old relocation */
+ relocation = addend + sp->value;
+ } else {
+ /*
+ * If the symbol is undefined, we relocate it
+ * in a way similar to -r case. We use an
+ * RRS relocation to resolve the symbol at
+ * run-time. The r_address field is updated
+ * to reflect the changed position in the
+ * output file.
+ *
+ * In case the symbol is defined in a shared
+ * object as N_TEXT or N_DATA, an appropriate
+ * jmpslot or copy relocation is generated.
+ */
+ switch (sp->so_defined) {
+
+ case N_TEXT+N_EXT:
+ /*
+ * Claim a jmpslot if one was
+ * allocated (dependent on
+ * `force_alias_flag').
+ */
+
+ if (sp->jmpslot_offset == -1)
+ goto undefined;
+
+ relocation = addend +
+ claim_rrs_jmpslot(r, sp, addend);
+ break;
+
+ case N_DATA+N_EXT:
+ /*FALLTHROUGH*/
+ case 0:
+ undefined:
+ r->r_address += dataseg?
+ entry->data_start_address:
+ entry->text_start_address;
+ relocation = addend;
+ if (claim_rrs_reloc(r, sp, &relocation))
+ continue;
+ break;
+
+ case N_BSS+N_EXT:
+printf("%s: BSS found in so_defined\n", sp->name);
+ /*break;*/
+
+ default:
+ fatal("%s: shobj symbol with unknown type %#x", sp->name, sp->so_defined);
+ break;
+ }
+ }
+
+ } else {
+
+ switch (RELOC_TYPE(r)) {
+ case N_TEXT:
+ case N_TEXT | N_EXT:
+ relocation = addend + text_relocation;
+ break;
+
+ case N_DATA:
+ case N_DATA | N_EXT:
+ /*
+ * A word that points to beginning of the the
+ * data section initially contains not 0 but
+ * rather the "address" of that section in
+ * the input file, which is the length of the
+ * file's text.
+ */
+ relocation = addend + data_relocation;
+ break;
+
+ case N_BSS:
+ case N_BSS | N_EXT:
+ /*
+ * Similarly, an input word pointing to the
+ * beginning of the bss initially contains
+ * the length of text plus data of the file.
+ */
+ relocation = addend + bss_relocation;
+ break;
+
+ case N_ABS:
+ case N_ABS | N_EXT:
+ /*
+ * Don't know why this code would occur, but
+ * apparently it does.
+ */
+ break;
+
+ default:
+ fatal_with_file(
+ "nonexternal relocation code invalid in ", entry);
+ }
+
+ /*
+ * When building a shared object, these segment
+ * relocations need a "load address relative"
+ * RRS fixup.
+ */
+ if (building_shared_object) {
+ r->r_address += dataseg?
+ entry->data_start_address:
+ entry->text_start_address;
+ claim_rrs_segment_reloc(r);
+ }
+ }
- /* Shift everything up to where it's going to be used */
- relocation <<= RELOC_TARGET_BITPOS(p);
- mask <<= RELOC_TARGET_BITPOS(p);
+ if (RELOC_PCREL_P(r))
+ relocation -= pc_relocation;
- switch (RELOC_TARGET_SIZE(p))
- {
- case 0:
- if (RELOC_MEMORY_SUB_P(p))
- relocation -= mask & *(char *) (data + addr);
- else if (RELOC_MEMORY_ADD_P(p))
- relocation += mask & *(char *) (data + addr);
- *(char *) (data + addr) &= ~mask;
- *(char *) (data + addr) |= relocation;
- break;
-
- case 1:
-#ifdef tahoe
- if (((int) data + addr & 1) == 0)
- {
-#endif
- if (RELOC_MEMORY_SUB_P(p))
- relocation -= mask & *(short *) (data + addr);
- else if (RELOC_MEMORY_ADD_P(p))
- relocation += mask & *(short *) (data + addr);
- *(short *) (data + addr) &= ~mask;
- *(short *) (data + addr) |= relocation;
-#ifdef tahoe
- }
- /*
- * The CCI Power 6 (aka Tahoe) architecture has byte-aligned
- * instruction operands but requires data accesses to be aligned.
- * Brain-damage...
- */
- else
- {
- unsigned char *da = (unsigned char *) (data + addr);
- unsigned short s = da[0] << 8 | da[1];
-
- if (RELOC_MEMORY_SUB_P(p))
- relocation -= mask & s;
- else if (RELOC_MEMORY_ADD_P(p))
- relocation += mask & s;
- s &= ~mask;
- s |= relocation;
- da[0] = s >> 8;
- da[1] = s;
- }
-#endif
- break;
+ md_relocate(r, relocation, data+addr, relocatable_output);
- case 2:
-#ifndef _CROSS_TARGET_ARCH
-#ifdef tahoe
- if (((int) data + addr & 3) == 0)
- {
-#endif
- if (RELOC_MEMORY_SUB_P(p))
- relocation -= mask & *(long *) (data + addr);
- else if (RELOC_MEMORY_ADD_P(p))
- relocation += mask & *(long *) (data + addr);
- *(long *) (data + addr) &= ~mask;
- *(long *) (data + addr) |= relocation;
-#ifdef tahoe
- }
- else
- {
- unsigned char *da = (unsigned char *) (data + addr);
- unsigned long l = da[0] << 24 | da[1] << 16 | da[2] << 8 | da[3];
-
- if (RELOC_MEMORY_SUB_P(p))
- relocation -= mask & l;
- else if (RELOC_MEMORY_ADD_P(p))
- relocation += mask & l;
- l &= ~mask;
- l |= relocation;
- da[0] = l >> 24;
- da[1] = l >> 16;
- da[2] = l >> 8;
- da[3] = l;
- }
-#endif
-#else
- /* Handle long word alignment requirements of SPARC architecture */
- /* WARNING: This fix makes an assumption on byte ordering */
- /* Marc Ullman, Stanford University Nov. 1 1989 */
- if (RELOC_MEMORY_SUB_P(p)) {
- relocation -= mask &
- ((*(unsigned short *) (data + addr) << 16) |
- *(unsigned short *) (data + addr + 2));
- } else if (RELOC_MEMORY_ADD_P(p)) {
- relocation += mask &
- ((*(unsigned short *) (data + addr) << 16) |
- *(unsigned short *) (data + addr + 2));
- }
- *(unsigned short *) (data + addr) &= (~mask >> 16);
- *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff);
- *(unsigned short *) (data + addr) |= (relocation >> 16);
- *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff);
-#endif
- break;
-
- default:
- fatal_with_file ("Unimplemented relocation field length in ", entry);
}
- }
}
/* For relocatable_output only: write out the relocation,
@@ -3800,164 +2369,170 @@ void coptxtrel (), copdatrel ();
void
write_rel ()
{
- register int i;
- register int count = 0;
-
- if (trace_files)
- fprintf (stderr, "Writing text relocation:\n\n");
-
- /* Assign each global symbol a sequence number, giving the order
- in which `write_syms' will write it.
- This is so we can store the proper symbolnum fields
- in relocation entries we write. */
-
- for (i = 0; i < TABSIZE; i++)
- {
- symbol *sp;
- for (sp = symtab[i]; sp; sp = sp->link)
- if (sp->referenced || sp->defined)
- {
- sp->def_count = count++;
- /* Leave room for the reference required by N_INDR, if
- necessary. */
- if ((sp->defined & ~N_EXT) == N_INDR)
- count++;
- }
- }
- /* 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 + global_indirect_count)
- fatal ("internal error");
-
- /* Write out the relocations of all files, remembered from copy_text. */
-
- each_full_file (coptxtrel, 0);
-
- if (trace_files)
- fprintf (stderr, "\nWriting data relocation:\n\n");
-
- each_full_file (copdatrel, 0);
-
- if (trace_files)
- fprintf (stderr, "\n");
+ register int count = 0;
+
+ if (trace_files)
+ fprintf (stderr, "Writing text relocation:\n\n");
+
+ /*
+ * Assign each global symbol a sequence number, giving the order
+ * in which `write_syms' will write it.
+ * This is so we can store the proper symbolnum fields
+ * in relocation entries we write.
+ *
+
+ /* BLECH - Assign number 0 to __DYNAMIC (!! Sun compatibility) */
+
+ if (dynamic_symbol->referenced)
+ dynamic_symbol->symbolnum = count++;
+ FOR_EACH_SYMBOL(i, sp) {
+ if (sp != dynamic_symbol && sp->referenced) {
+ sp->symbolnum = count++;
+ }
+ } 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)
+ fatal ("internal error: write_rel: count = %d", count);
+
+ /* Write out the relocations of all files, remembered from copy_text. */
+ each_full_file (coptxtrel, 0);
+
+ if (trace_files)
+ fprintf (stderr, "\nWriting data relocation:\n\n");
+
+ each_full_file (copdatrel, 0);
+
+ if (trace_files)
+ fprintf (stderr, "\n");
}
void
-coptxtrel (entry)
- struct file_entry *entry;
+coptxtrel(entry)
+ struct file_entry *entry;
{
- register struct relocation_info *p, *end;
- register int reloc = entry->text_start_address;
-
- p = entry->textrel;
- end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
- while (p < end)
- {
- RELOC_ADDRESS(p) += reloc;
- if (RELOC_EXTERN_P(p))
- {
- register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
- symbol *symptr = ((symbol *)
- (((struct nlist *)
- (((char *)entry->symbols) + symindex))
- ->n_un.n_name));
+ register struct relocation_info *r, *end;
+ register int reloc = entry->text_start_address;
+
+ r = entry->textrel;
+ end = r + entry->ntextrel;
+
+ for (; r < end; r++) {
+ register int symindex;
+ symbol *sp;
- if (symindex >= entry->header.a_syms)
- fatal_with_file ("relocation symbolnum out of range in ", entry);
+ RELOC_ADDRESS(r) += reloc;
+
+ if (!RELOC_EXTERN_P(r))
+ continue;
+
+ symindex = RELOC_SYMBOL(r);
+ sp = entry->symbols[symindex].symbol;
+
+ if (symindex >= entry->nsymbols)
+ fatal_with_file(
+ "relocation symbolnum out of range in ", entry);
#ifdef N_INDR
- /* Resolve indirection. */
- if ((symptr->defined & ~N_EXT) == N_INDR)
- symptr = (symbol *) symptr->value;
+ /* Resolve indirection. */
+ if ((sp->defined & ~N_EXT) == N_INDR) {
+ if (sp->alias == NULL)
+ fatal("internal error: alias in hyperspace");
+ sp = sp->alias;
+ }
#endif
- /* If the symbol is now defined, change the external relocation
- to an internal one. */
+ /*
+ * If the symbol is now defined, change the external
+ * relocation to an internal one.
+ */
- if (symptr->defined)
- {
- RELOC_EXTERN_P(p) = 0;
- RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
+ if (sp->defined) {
+ RELOC_EXTERN_P(r) = 0;
+ RELOC_SYMBOL(r) = (sp->defined & N_TYPE);
#ifdef RELOC_ADD_EXTRA
- /* If we aren't going to be adding in the value in
- memory on the next pass of the loader, then we need
- to add it in from the relocation entry. Otherwise
- the work we did in this pass is lost. */
- if (!RELOC_MEMORY_ADD_P(p))
- RELOC_ADD_EXTRA (p) += symptr->value;
+ /*
+ * If we aren't going to be adding in the
+ * value in memory on the next pass of the
+ * loader, then we need to add it in from the
+ * relocation entry. Otherwise the work we
+ * did in this pass is lost.
+ */
+ if (!RELOC_MEMORY_ADD_P(r))
+ RELOC_ADD_EXTRA(r) += sp->value;
#endif
- }
- else
- /* Debugger symbols come first, so have to start this
- after them. */
- RELOC_SYMBOL(p) = (symptr->def_count + nsyms
- - defined_global_sym_count
- - undefined_global_sym_count
- - global_indirect_count);
+ } else
+ /*
+ * Global symbols come first.
+ */
+ RELOC_SYMBOL(r) = sp->symbolnum;
}
- p++;
- }
- mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
+ md_swapout_reloc(entry->textrel, entry->ntextrel);
+ mywrite(entry->textrel, entry->ntextrel,
+ sizeof(struct relocation_info), outdesc);
}
void
-copdatrel (entry)
- struct file_entry *entry;
+copdatrel(entry)
+ struct file_entry *entry;
{
- register struct relocation_info *p, *end;
- /* Relocate the address of the relocation.
- Old address is relative to start of the input file's data section.
- New address is relative to start of the output file's data section. */
- register int reloc = entry->data_start_address - text_size;
-
- p = entry->datarel;
- end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
- while (p < end)
- {
- RELOC_ADDRESS(p) += reloc;
- if (RELOC_EXTERN_P(p))
- {
- register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
- symbol *symptr = ((symbol *)
- (((struct nlist *)
- (((char *)entry->symbols) + symindex))
- ->n_un.n_name));
- int symtype;
+ register struct relocation_info *r, *end;
+ /*
+ * Relocate the address of the relocation. Old address is relative to
+ * start of the input file's data section. New address is relative to
+ * start of the output file's data section.
+ */
+ register int reloc = entry->data_start_address - text_size;
- if (symindex >= entry->header.a_syms)
- fatal_with_file ("relocation symbolnum out of range in ", entry);
+ r = entry->datarel;
+ end = r + entry->ndatarel;
+
+ for (; r < end; r++) {
+ register int symindex;
+ symbol *sp;
+ int symtype;
+
+ RELOC_ADDRESS(r) += reloc;
+
+ if (!RELOC_EXTERN_P(r))
+ continue;
+
+ symindex = RELOC_SYMBOL(r);
+ sp = entry->symbols[symindex].symbol;
+
+ if (symindex >= entry->header.a_syms)
+ fatal_with_file(
+ "relocation symbolnum out of range in ", entry);
#ifdef N_INDR
- /* Resolve indirection. */
- if ((symptr->defined & ~N_EXT) == N_INDR)
- symptr = (symbol *) symptr->value;
+ /* Resolve indirection. */
+ if ((sp->defined & ~N_EXT) == N_INDR) {
+ if (sp->alias == NULL)
+ fatal("internal error: alias in hyperspace");
+ sp = sp->alias;
+ }
#endif
- symtype = symptr->defined & N_TYPE;
-
- if (force_common_definition
- || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
- {
- RELOC_EXTERN_P(p) = 0;
- RELOC_SYMBOL(p) = symtype;
- }
- else
- /* Debugger symbols come first, so have to start this
- after them. */
- RELOC_SYMBOL(p)
- = (((symbol *)
- (((struct nlist *)
- (((char *)entry->symbols) + symindex))
- ->n_un.n_name))
- ->def_count
- + nsyms - defined_global_sym_count
- - undefined_global_sym_count
- - global_indirect_count);
+ symtype = sp->defined & N_TYPE;
+
+ if (force_common_definition ||
+ symtype == N_DATA ||
+ symtype == N_TEXT ||
+ symtype == N_ABS) {
+ RELOC_EXTERN_P(r) = 0;
+ RELOC_SYMBOL(r) = symtype;
+ } else
+ /*
+ * Global symbols come first.
+ */
+ RELOC_SYMBOL(r) =
+ entry->symbols[symindex].symbol->symbolnum;
}
- p++;
- }
- mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
+ md_swapout_reloc(entry->datarel, entry->ndatarel);
+ mywrite(entry->datarel, entry->ndatarel,
+ sizeof(struct relocation_info), outdesc);
}
void write_file_syms ();
@@ -3988,712 +2563,383 @@ int *strtab_lens;
/* Index in `strtab_vector' at which the next string will be stored. */
int strtab_index;
-/* Add the string NAME to the output file string table.
- Record it in `strtab_vector' to be output later.
- Return the index within the string table that this string will have. */
+/*
+ * Add the string NAME to the output file string table. Record it in
+ * `strtab_vector' to be output later. Return the index within the string
+ * table that this string will have.
+ */
int
-assign_string_table_index (name)
- char *name;
+assign_string_table_index(name)
+ char *name;
{
- register int index = strtab_size;
- register int len = strlen (name) + 1;
+ register int index = strtab_size;
+ register int len = strlen(name) + 1;
- strtab_size += len;
- strtab_vector[strtab_index] = name;
- strtab_lens[strtab_index++] = len;
+ strtab_size += len;
+ strtab_vector[strtab_index] = name;
+ strtab_lens[strtab_index++] = len;
- return index;
+ return index;
}
-FILE *outstream = (FILE *) 0;
-
-/* Write the contents of `strtab_vector' into the string table.
- This is done once for each file's local&debugger symbols
- and once for the global symbols. */
+FILE *outstream = (FILE *) 0;
+/*
+ * Write the contents of `strtab_vector' into the string table. This is done
+ * once for each file's local&debugger symbols and once for the global
+ * symbols.
+ */
void
write_string_table ()
{
- register int i;
+ register int i;
- lseek (outdesc, string_table_offset + string_table_len, 0);
+ lseek (outdesc, string_table_offset + string_table_len, 0);
- if (!outstream)
- outstream = fdopen (outdesc, "w");
+ if (!outstream)
+ outstream = fdopen (outdesc, "w");
- for (i = 0; i < strtab_index; i++)
- {
- fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
- string_table_len += strtab_lens[i];
- }
+ for (i = 0; i < strtab_index; i++) {
+ fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
+ string_table_len += strtab_lens[i];
+ }
- fflush (outstream);
+ fflush (outstream);
- /* Report I/O error such as disk full. */
- if (ferror (outstream))
- perror_name (output_filename);
+ /* Report I/O error such as disk full. */
+ if (ferror (outstream))
+ perror_name (output_filename);
}
/* Write the symbol table and string table of the output file. */
void
-write_syms ()
-{
- /* Number of symbols written so far. */
- int syms_written = 0;
- register int i;
- register symbol *sp;
-
- /* 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 ((defined_global_sym_count
- + undefined_global_sym_count
- + global_indirect_count)
- * sizeof (struct nlist));
- /* Pointer for storing into BUF. */
- register struct nlist *bufp = buf;
-
- /* Size of string table includes the bytes that store the size. */
- strtab_size = sizeof strtab_size;
-
- symbol_table_offset = N_SYMOFF (outheader);
- symbol_table_len = 0;
- string_table_offset = N_STROFF (outheader);
- string_table_len = strtab_size;
-
- if (strip_symbols == STRIP_ALL)
- return;
-
- /* Write the local symbols defined by the various files. */
-
- each_file (write_file_syms, &syms_written);
- file_close ();
-
- /* Now write out the global symbols. */
-
- /* Allocate two vectors that record the data to generate the string
- table from the global symbols written so far. This must include
- extra space for the references following indirect outputs. */
-
- strtab_vector = (char **) alloca ((num_hash_tab_syms
- + global_indirect_count) * sizeof (char *));
- strtab_lens = (int *) alloca ((num_hash_tab_syms
- + global_indirect_count) * sizeof (int));
- strtab_index = 0;
-
- /* Scan the symbol hash table, bucket by bucket. */
-
- for (i = 0; i < TABSIZE; i++)
- for (sp = symtab[i]; sp; sp = sp->link)
- {
- struct nlist nl;
-
- nl.n_other = 0;
- nl.n_desc = 0;
-
- /* Compute a `struct nlist' for the symbol. */
-
- if (sp->defined || sp->referenced)
- {
- /* common condition needs to be before undefined condition */
- /* because unallocated commons are set undefined in */
- /* digest_symbols */
- if (sp->defined > 1) /* defined with known type */
- {
- /* If the target of an indirect symbol has been
- defined and we are outputting an executable,
- resolve the indirection; it's no longer needed */
- if (!relocatable_output
- && ((sp->defined & N_TYPE) == N_INDR)
- && (((symbol *) sp->value)->defined > 1))
- {
- symbol *newsp = (symbol *) sp->value;
- nl.n_type = newsp->defined;
- nl.n_value = newsp->value;
- }
+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;
+
+ /*
+ * 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));
+ /* Pointer for storing into BUF. */
+ register struct nlist *bufp = buf;
+
+ /* Size of string table includes the bytes that store the size. */
+ strtab_size = sizeof strtab_size;
+
+ symbol_table_offset = N_SYMOFF(outheader);
+ symbol_table_len = 0;
+ string_table_offset = N_STROFF(outheader);
+ string_table_len = strtab_size;
+
+ if (strip_symbols == STRIP_ALL)
+ return;
+
+ /* First, write out the global symbols. */
+
+ /*
+ * Allocate two vectors that record the data to generate the string
+ * table from the global symbols written so far. This must include
+ * 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_index = 0;
+
+ /*
+ * __DYNAMIC symbol *must* be first for Sun compatibility, as Sun's
+ * ld.so reads the shared object's first symbol. This means that
+ * (Sun's) shared libraries cannot be stripped! (We only assume
+ * that __DYNAMIC is the first item in the data segment)
+ *
+ * If defined (ie. not relocatable_output), make it look
+ * like an internal symbol.
+ */
+ if (dynamic_symbol->referenced) {
+ nl.n_other = 0;
+ nl.n_desc = 0;
+ nl.n_type = dynamic_symbol->defined;
+ if (nl.n_type == N_UNDF)
+ nl.n_type |= N_EXT;
else
- {
- nl.n_type = sp->defined;
- if (sp->defined != (N_INDR | N_EXT))
- nl.n_value = sp->value;
- else
- nl.n_value = 0;
- }
- }
- else if (sp->max_common_size) /* defined as common but not allocated. */
- {
- /* happens only with -r and not -d */
- /* write out a common definition */
- nl.n_type = N_UNDF | N_EXT;
- nl.n_value = sp->max_common_size;
- }
- else if (!sp->defined) /* undefined -- legit only if -r */
- {
- nl.n_type = N_UNDF | N_EXT;
- nl.n_value = 0;
- }
- else
- fatal ("internal error: %s defined in mysterious way", sp->name);
-
- /* Allocate string table space for the symbol name. */
-
- nl.n_un.n_strx = assign_string_table_index (sp->name);
-
- /* Output to the buffer and count it. */
-
- *bufp++ = nl;
- syms_written++;
- if (nl.n_type == (N_INDR | N_EXT))
- {
- struct nlist xtra_ref;
- xtra_ref.n_type = N_EXT | N_UNDF;
- xtra_ref.n_un.n_strx
- = assign_string_table_index (((symbol *) sp->value)->name);
- xtra_ref.n_other = 0;
- xtra_ref.n_desc = 0;
- xtra_ref.n_value = 0;
- *bufp++ = xtra_ref;
+ nl.n_type &= ~N_EXT;
+ nl.n_value = dynamic_symbol->value;
+ nl.n_un.n_strx = assign_string_table_index(dynamic_symbol->name);
+ *bufp++ = nl;
syms_written++;
- }
- }
- }
-
- /* Output the buffer full of `struct nlist's. */
-
- lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
- mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
- symbol_table_len += sizeof (struct nlist) * (bufp - buf);
-
- if (syms_written != nsyms)
- fatal ("internal error: wrong number of symbols written into output file", 0);
-
- if (symbol_table_offset + symbol_table_len != string_table_offset)
- fatal ("internal error: inconsistent symbol table length", 0);
-
- /* Now the total string table size is known, so write it.
- We are already positioned at the right place in the file. */
-
- mywrite (&strtab_size, sizeof (int), 1, outdesc); /* we're at right place */
-
- /* Write the strings for the global symbols. */
-
- write_string_table ();
-}
-
-/* Write the local and debugger symbols of file ENTRY.
- Increment *SYMS_WRITTEN_ADDR for each symbol that is written. */
-
-/* Note that we do not combine identical names of local symbols.
- dbx or gdb would be confused if we did that. */
-
-void
-write_file_syms (entry, syms_written_addr)
- struct file_entry *entry;
- int *syms_written_addr;
-{
- register struct nlist *p = entry->symbols;
- register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
-
- /* Buffer to accumulate all the syms before writing them.
- It has one extra slot for the local symbol we generate here. */
- struct nlist *buf
- = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
- register struct nlist *bufp = buf;
-
- /* Upper bound on number of syms to be written here. */
- int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
-
- /* Make tables that record, for each symbol, its name and its name's length.
- The elements are filled in by `assign_string_table_index'. */
-
- strtab_vector = (char **) alloca (max_syms * sizeof (char *));
- strtab_lens = (int *) alloca (max_syms * sizeof (int));
- strtab_index = 0;
-
- /* Generate a local symbol for the start of this file's text. */
-
- if (discard_locals != DISCARD_ALL)
- {
- struct nlist nl;
-
- nl.n_type = N_FN | N_EXT;
- nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
- nl.n_value = entry->text_start_address;
- nl.n_desc = 0;
- nl.n_other = 0;
- *bufp++ = nl;
- (*syms_written_addr)++;
- entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
- }
-
- /* Read the file's string table. */
-
- entry->strings = (char *) alloca (entry->string_size);
- read_entry_strings (file_open (entry), entry);
-
- for (; p < end; p++)
- {
- register int type = p->n_type;
- register int write = 0;
-
- /* WRITE gets 1 for a non-global symbol that should be written. */
-
-
- 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);
-
- 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);
-
- /* Output this symbol to the buffer and count it. */
-
- *bufp++ = *p;
- (*syms_written_addr)++;
- }
- }
-
- /* All the symbols are now in BUF; write them. */
-
- lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
- mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
- symbol_table_len += sizeof (struct nlist) * (bufp - buf);
-
- /* Write the string-table data for the symbols just written,
- using the data in vectors `strtab_vector' and `strtab_lens'. */
-
- write_string_table ();
- entry->strings = 0; /* Since it will dissapear anyway. */
-}
-
-/* Copy any GDB symbol segments from the input files to the output file.
- The contents of the symbol segment is copied without change
- except that we store some information into the beginning of it. */
-
-void write_file_symseg ();
-
-void
-write_symsegs ()
-{
- each_file (write_file_symseg, 0);
-}
-
-void
-write_file_symseg (entry)
- struct file_entry *entry;
-{
- char buffer[4096];
- struct symbol_root root;
- int indesc;
- int len;
-
- if (entry->symseg_offset == 0)
- return;
-
- /* This entry has a symbol segment. Read the root of the segment. */
-
- indesc = file_open (entry);
- lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
- if (sizeof root != read (indesc, &root, sizeof root))
- fatal_with_file ("premature end of file in symbol segment of ", entry);
-
- /* Store some relocation info into the root. */
-
- root.ldsymoff = entry->local_syms_offset;
- root.textrel = entry->text_start_address;
- root.datarel = entry->data_start_address - entry->header.a_text;
- root.bssrel = entry->bss_start_address
- - entry->header.a_text - entry->header.a_data;
- root.databeg = entry->data_start_address - root.datarel;
- root.bssbeg = entry->bss_start_address - root.bssrel;
-
- /* Write the modified root into the output file. */
-
- mywrite (&root, sizeof root, 1, outdesc);
-
- /* Copy the rest of the symbol segment unchanged. */
-
- if (entry->superfile)
- {
- /* Library member: number of bytes to copy is determined
- from the member's total size. */
-
- int total = entry->total_size - entry->symseg_offset - sizeof root;
-
- while (total > 0)
- {
- len = read (indesc, buffer, min (sizeof buffer, total));
-
- if (len != min (sizeof buffer, total))
- fatal_with_file ("premature end of file in symbol segment of ", entry);
- total -= len;
- mywrite (buffer, len, 1, outdesc);
- }
- }
- else
- {
- /* A separate file: copy until end of file. */
-
- while (len = read (indesc, buffer, sizeof buffer))
- {
- mywrite (buffer, len, 1, outdesc);
- if (len < sizeof buffer)
- break;
}
- }
- file_close ();
-}
-
-/* Create the symbol table entries for `etext', `edata' and `end'. */
+ /* Scan the symbol hash table, bucket by bucket. */
+
+ FOR_EACH_SYMBOL(i, sp) {
+ if (sp == dynamic_symbol)
+ /* Already dealt with above */
+ continue;
+
+ if (!sp->referenced)
+ /* Came from shared object but was not used */
+ continue;
+
+ if (sp->so_defined)
+ /*
+ * Definition came from shared object,
+ * don't mention it here
+ */
+ continue;
+
+ if (!sp->defined && !relocatable_output) {
+ /*
+ * We're building a shared object and there
+ * are still undefined symbols. Don't output
+ * these, symbol was discounted in digest_pass1()
+ * (they are in the RRS symbol table).
+ */
+ if (!building_shared_object)
+ error("symbol %s remains undefined", sp->name);
+ continue;
+ }
-void
-symtab_init ()
-{
-#ifndef nounderscore
- edata_symbol = getsym ("_edata");
- etext_symbol = getsym ("_etext");
- end_symbol = getsym ("_end");
-#else
- edata_symbol = getsym ("edata");
- etext_symbol = getsym ("etext");
- end_symbol = getsym ("end");
-#endif
+ /* Construct a `struct nlist' for the symbol. */
+
+ nl.n_other = 0;
+ nl.n_desc = 0;
+
+ /*
+ * common condition needs to be before undefined
+ * condition because unallocated commons are set
+ * undefined in digest_symbols
+ */
+ if (sp->defined > 1) {
+ /* defined with known type */
+
+ if (!relocatable_output && sp->alias &&
+ sp->alias->defined > 1) {
+ /*
+ * If the target of an indirect symbol has
+ * been defined and we are outputting an
+ * executable, resolve the indirection; it's
+ * no longer needed
+ */
+ nl.n_type = sp->alias->defined;
+ nl.n_type = sp->alias->value;
+ } else if (sp->defined == N_SIZE)
+ nl.n_type = N_DATA | N_EXT;
+ else
+ nl.n_type = sp->defined;
+ nl.n_value = sp->value;
+ } else if (sp->max_common_size) {
+ /*
+ * defined as common but not allocated,
+ * happens only with -r and not -d, write out
+ * a common definition
+ */
+ nl.n_type = N_UNDF | N_EXT;
+ nl.n_value = sp->max_common_size;
+ } else if (!sp->defined) {
+ /* undefined -- legit only if -r */
+ nl.n_type = N_UNDF | N_EXT;
+ nl.n_value = 0;
+ } else
+ fatal(
+ "internal error: %s defined in mysterious way",
+ sp->name);
+
+ /*
+ * Allocate string table space for the symbol name.
+ */
+
+ nl.n_un.n_strx = assign_string_table_index(sp->name);
+
+ /* Output to the buffer and count it. */
+
+ if (syms_written >= non_local_syms)
+ fatal(
+ "internal error: number of symbols exceeds allocated %d",
+ non_local_syms);
+ *bufp++ = nl;
+ syms_written++;
-#ifdef sun
- {
- symbol *dynamic_symbol = getsym ("__DYNAMIC");
- dynamic_symbol->defined = N_ABS | N_EXT;
- dynamic_symbol->referenced = 1;
- dynamic_symbol->value = 0;
- }
-#endif
+ if (nl.n_type == N_INDR + N_EXT) {
+ if (sp->alias == NULL)
+ fatal("internal error: alias in hyperspace");
+ nl.n_type = N_UNDF + N_EXT;
+ nl.n_un.n_strx =
+ assign_string_table_index(sp->alias->name);
+ nl.n_value = 0;
+ *bufp++ = nl;
+ syms_written++;
+ }
-#ifdef sequent
- {
- symbol *_387_flt_symbol = getsym ("_387_flt");
- _387_flt_symbol->defined = N_ABS | N_EXT;
- _387_flt_symbol->referenced = 1;
- _387_flt_symbol->value = 0;
- }
+#ifdef DEBUG
+printf("writesym(#%d): %s, type %x\n", syms_written, sp->name, sp->defined);
#endif
+ } END_EACH_SYMBOL;
- edata_symbol->defined = N_DATA | N_EXT;
- etext_symbol->defined = N_TEXT | N_EXT;
- end_symbol->defined = N_BSS | N_EXT;
-
- edata_symbol->referenced = 1;
- etext_symbol->referenced = 1;
- end_symbol->referenced = 1;
-}
-
-/* Compute the hash code for symbol name KEY. */
-
-int
-hash_string (key)
- char *key;
-{
- register char *cp;
- register int k;
-
- cp = key;
- k = 0;
- while (*cp)
- k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
-
- return k;
-}
-
-/* Get the symbol table entry for the global symbol named KEY.
- Create one if there is none. */
-
-symbol *
-getsym (key)
- char *key;
-{
- register int hashval;
- register symbol *bp;
-
- /* Determine the proper bucket. */
-
- hashval = hash_string (key) % TABSIZE;
-
- /* Search the bucket. */
-
- for (bp = symtab[hashval]; bp; bp = bp->link)
- if (! strcmp (key, bp->name))
- return bp;
-
- /* Nothing was found; create a new symbol table entry. */
-
- bp = (symbol *) xmalloc (sizeof (symbol));
- bp->refs = 0;
- bp->name = (char *) xmalloc (strlen (key) + 1);
- strcpy (bp->name, key);
- bp->defined = 0;
- bp->referenced = 0;
- bp->trace = 0;
- bp->value = 0;
- bp->max_common_size = 0;
- bp->warning = 0;
- bp->undef_refs = 0;
- bp->multiply_defined = 0;
-
- /* Add the entry to the bucket. */
-
- bp->link = symtab[hashval];
- symtab[hashval] = bp;
-
- ++num_hash_tab_syms;
-
- return bp;
-}
-
-/* Like `getsym' but return 0 if the symbol is not already known. */
-
-symbol *
-getsym_soft (key)
- char *key;
-{
- register int hashval;
- register symbol *bp;
-
- /* Determine which bucket. */
-
- hashval = hash_string (key) % TABSIZE;
-
- /* Search the bucket. */
-
- for (bp = symtab[hashval]; bp; bp = bp->link)
- if (! strcmp (key, bp->name))
- return bp;
-
- return 0;
-}
-
-/* Report a fatal error.
- STRING is a printf format string and ARG is one arg for it. */
-
-void
-fatal (string, arg)
- char *string, *arg;
-{
- fprintf (stderr, "ld: ");
- fprintf (stderr, string, arg);
- fprintf (stderr, "\n");
- exit (1);
-}
+ if (syms_written != strtab_index || strtab_index != non_local_syms)
+ fatal("internal error:\
+wrong number (%d) of global symbols written into output file, should be %d",
+ syms_written, non_local_syms);
-/* Report a fatal error. The error message is STRING
- followed by the filename of ENTRY. */
-
-void
-fatal_with_file (string, entry)
- char *string;
- struct file_entry *entry;
-{
- fprintf (stderr, "ld: ");
- fprintf (stderr, string);
- print_file_name (entry, stderr);
- fprintf (stderr, "\n");
- exit (1);
-}
+ /* Output the buffer full of `struct nlist's. */
-/* Report a fatal error using the message for the last failed system call,
- followed by the string NAME. */
+ lseek(outdesc, symbol_table_offset + symbol_table_len, 0);
+ md_swapout_symbols(buf, bufp - buf);
+ mywrite(buf, bufp - buf, sizeof(struct nlist), outdesc);
+ symbol_table_len += sizeof(struct nlist) * (bufp - buf);
-void
-perror_name (name)
- char *name;
-{
- extern int errno, sys_nerr;
- extern char *sys_errlist[];
- char *s;
-
- if (errno < sys_nerr)
- s = concat ("", sys_errlist[errno], " for %s");
- else
- s = "cannot open %s";
- fatal (s, name);
-}
+ /* Write the strings for the global symbols. */
+ write_string_table();
-/* Report a fatal error using the message for the last failed system call,
- followed by the name of file ENTRY. */
+ /* Write the local symbols defined by the various files. */
+ each_file(write_file_syms, &syms_written);
+ file_close();
-void
-perror_file (entry)
- struct file_entry *entry;
-{
- extern int errno, sys_nerr;
- extern char *sys_errlist[];
- char *s;
-
- if (errno < sys_nerr)
- s = concat ("", sys_errlist[errno], " for ");
- else
- s = "cannot open ";
- fatal_with_file (s, entry);
-}
+ if (syms_written != nsyms)
+ fatal("internal error:\
+wrong number of symbols (%d) written into output file, should be %d",
+ syms_written, nsyms);
-/* Report a nonfatal error.
- STRING is a format for printf, and ARG1 ... ARG3 are args for it. */
+ if (symbol_table_offset + symbol_table_len != string_table_offset)
+ fatal(
+ "internal error: inconsistent symbol table length: %d vs %s",
+ symbol_table_offset + symbol_table_len, string_table_offset);
-void
-error (string, arg1, arg2, arg3)
- char *string, *arg1, *arg2, *arg3;
-{
- fprintf (stderr, "%s: ", progname);
- fprintf (stderr, string, arg1, arg2, arg3);
- fprintf (stderr, "\n");
+ lseek(outdesc, string_table_offset, 0);
+ strtab_size = md_swap_long(strtab_size);
+ mywrite(&strtab_size, sizeof(int), 1, outdesc);
}
-
-/* Output COUNT*ELTSIZE bytes of data at BUF
- to the descriptor DESC. */
-
-void
-mywrite (buf, count, eltsize, desc)
- char *buf;
- int count;
- int eltsize;
- int desc;
-{
- register int val;
- register int bytes = count * eltsize;
-
- while (bytes > 0)
- {
- val = write (desc, buf, bytes);
- if (val <= 0)
- perror_name (output_filename);
- buf += val;
- bytes -= val;
- }
-}
-/* Output PADDING zero-bytes to descriptor OUTDESC.
- PADDING may be negative; in that case, do nothing. */
+/*
+ * Write the local and debugger symbols of file ENTRY. Increment
+ * *SYMS_WRITTEN_ADDR for each symbol that is written.
+ */
+/*
+ * Note that we do not combine identical names of local symbols. dbx or gdb
+ * would be confused if we did that.
+ */
void
-padfile (padding, outdesc)
- int padding;
- int outdesc;
-{
- register char *buf;
- if (padding <= 0)
- return;
-
- buf = (char *) alloca (padding);
- bzero (buf, padding);
- mywrite (buf, padding, 1, outdesc);
-}
-
-/* Return a newly-allocated string
- whose contents concatenate the strings S1, S2, S3. */
-
-char *
-concat (s1, s2, s3)
- char *s1, *s2, *s3;
+write_file_syms(entry, syms_written_addr)
+ struct file_entry *entry;
+ int *syms_written_addr;
{
- register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
- register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
-
- strcpy (result, s1);
- strcpy (result + len1, s2);
- strcpy (result + len1 + len2, s3);
- result[len1 + len2 + len3] = 0;
-
- return result;
-}
+ struct localsymbol *lsp, *lspend;
-/* Parse the string ARG using scanf format FORMAT, and return the result.
- If it does not parse, report fatal error
- generating the error message using format string ERROR and ARG as arg. */
+ /* Upper bound on number of syms to be written here. */
+ int max_syms = entry->nsymbols + 1;
-int
-parse (arg, format, error)
- char *arg, *format;
-{
- int x;
- if (1 != sscanf (arg, format, &x))
- fatal (error, arg);
- return x;
-}
+ /*
+ * Buffer to accumulate all the syms before writing them. It has one
+ * extra slot for the local symbol we generate here.
+ */
+ struct nlist *buf = (struct nlist *)
+ alloca(max_syms * sizeof(struct nlist));
-/* Like malloc but get fatal error if memory is exhausted. */
+ register struct nlist *bufp = buf;
-int
-xmalloc (size)
- int size;
-{
- register int result = malloc (size);
- if (!result)
- fatal ("virtual memory exhausted", 0);
- return result;
-}
+ if (entry->is_dynamic)
+ return;
-/* Like realloc but get fatal error if memory is exhausted. */
+ /*
+ * Make tables that record, for each symbol, its name and its name's
+ * length. The elements are filled in by `assign_string_table_index'.
+ */
-int
-xrealloc (ptr, size)
- char *ptr;
- int size;
-{
- register int result = realloc (ptr, size);
- if (!result)
- fatal ("virtual memory exhausted", 0);
- return result;
-}
-
-#ifdef USG
+ strtab_vector = (char **) alloca(max_syms * sizeof(char *));
+ strtab_lens = (int *) alloca(max_syms * sizeof(int));
+ strtab_index = 0;
-void
-bzero (p, n)
- char *p;
-{
- memset (p, 0, n);
-}
+ /* Generate a local symbol for the start of this file's text. */
-void
-bcopy (from, to, n)
- char *from, *to;
-{
- memcpy (to, from, n);
-}
+ if (discard_locals != DISCARD_ALL) {
+ struct nlist nl;
-getpagesize ()
-{
- return (4096);
-}
+ nl.n_type = N_FN | N_EXT;
+ nl.n_un.n_strx = assign_string_table_index(entry->local_sym_name);
+ nl.n_value = entry->text_start_address;
+ nl.n_desc = 0;
+ nl.n_other = 0;
+ *bufp++ = nl;
+ (*syms_written_addr)++;
+ entry->local_syms_offset = *syms_written_addr * sizeof(struct nlist);
+ }
+ /* Read the file's string table. */
+
+ entry->strings = (char *) alloca(entry->string_size);
+ read_entry_strings(file_open(entry), entry);
+
+ lspend = entry->symbols + entry->nsymbols;
+
+ for (lsp = entry->symbols; lsp < lspend; lsp++) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+ register int type = p->n_type;
+ register int write = 0;
+
+ /*
+ * WRITE gets 1 for a non-global symbol that should be
+ * written.
+ */
+
+ 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);
+
+ /* Output this symbol to the buffer and count it. */
+
+ *bufp++ = *p;
+ (*syms_written_addr)++;
+ }
+ }
-#endif
+ /* All the symbols are now in BUF; write them. */
-#if defined(sun) && (TARGET == SUN4)
+ lseek(outdesc, symbol_table_offset + symbol_table_len, 0);
+ md_swapout_symbols(buf, bufp - buf);
+ mywrite(buf, bufp - buf, sizeof(struct nlist), outdesc);
+ symbol_table_len += sizeof(struct nlist) * (bufp - buf);
-/* Don't use local pagesize to build for Sparc. */
+ /*
+ * Write the string-table data for the symbols just written, using
+ * the data in vectors `strtab_vector' and `strtab_lens'.
+ */
-getpagesize ()
-{
- return (8192);
+ write_string_table();
+ entry->strings = 0; /* Since it will disappear anyway. */
}
-#endif
diff --git a/gnu/usr.bin/ld/ld.h b/gnu/usr.bin/ld/ld.h
new file mode 100644
index 0000000..687b7a3
--- /dev/null
+++ b/gnu/usr.bin/ld/ld.h
@@ -0,0 +1,890 @@
+/* $Id: ld.h,v 1.4 1993/11/01 16:26:16 pk Exp $ */
+/*-
+ * This code is derived from software copyrighted by the Free Software
+ * Foundation.
+ *
+ * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
+ */
+
+#define SUN_COMPAT
+
+#ifndef N_SIZE
+#define N_SIZE 0xc
+#endif
+
+#ifndef min
+#define min(a,b) ((a) < (b) ? (a) : (b))
+#endif
+
+#ifndef __P
+#ifndef __STDC__
+#define __P(a) ()
+#else
+#define __P(a) a
+#endif
+#endif
+
+/* If compiled with GNU C, use the built-in alloca */
+#if defined(__GNUC__) || defined(sparc)
+#define alloca __builtin_alloca
+#endif
+
+#include "md.h"
+#include "link.h"
+
+/* Macro to control the number of undefined references printed */
+#define MAX_UREFS_PRINTED 10
+
+/* Align to power-of-two boundary */
+#define PALIGN(x,p) (((x) + (u_long)(p) - 1) & (-(u_long)(p)))
+
+/* Align to machine dependent boundary */
+#define MALIGN(x) PALIGN(x,MAX_ALIGNMENT)
+
+/* Size of a page; obtained from the operating system. */
+
+int page_size;
+
+/* Name this program was invoked by. */
+
+char *progname;
+
+/* System dependencies */
+
+/* Define this to specify the default executable format. */
+
+#ifndef DEFAULT_MAGIC
+#define DEFAULT_MAGIC ZMAGIC
+#endif
+
+#ifdef QMAGIC
+int oldmagic;
+#endif
+
+
+/*
+ * Ok. Following are the relocation information macros. If your
+ * system should not be able to use the default set (below), you must
+ * define the following:
+
+ * relocation_info: This must be typedef'd (or #define'd) to the type
+ * of structure that is stored in the relocation info section of your
+ * a.out files. Often this is defined in the a.out.h for your system.
+ *
+ * RELOC_ADDRESS (rval): Offset into the current section of the
+ * <whatever> to be relocated. *Must be an lvalue*.
+ *
+ * RELOC_EXTERN_P (rval): Is this relocation entry based on an
+ * external symbol (1), or was it fully resolved upon entering the
+ * loader (0) in which case some combination of the value in memory
+ * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
+ * what the value of the relocation actually was. *Must be an lvalue*.
+ *
+ * RELOC_TYPE (rval): If this entry was fully resolved upon
+ * entering the loader, what type should it be relocated as?
+ *
+ * RELOC_SYMBOL (rval): If this entry was not fully resolved upon
+ * entering the loader, what is the index of it's symbol in the symbol
+ * table? *Must be a lvalue*.
+ *
+ * RELOC_MEMORY_ADD_P (rval): This should return true if the final
+ * relocation value output here should be added to memory, or if the
+ * section of memory described should simply be set to the relocation
+ * value.
+ *
+ * RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
+ * an extra value to be added to the relocation value based on the
+ * individual relocation entry. *Must be an lvalue if defined*.
+ *
+ * RELOC_PCREL_P (rval): True if the relocation value described is
+ * pc relative.
+ *
+ * RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
+ * final relocation value before putting it where it belongs.
+ *
+ * RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
+ * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
+ * == 1; 4 bytes == 2, and etc. This is somewhat redundant (we could
+ * do everything in terms of the bit operators below), but having this
+ * macro could end up producing better code on machines without fancy
+ * bit twiddling. Also, it's easier to understand/code big/little
+ * endian distinctions with this macro.
+ *
+ * RELOC_TARGET_BITPOS (rval): The starting bit position within the
+ * object described in RELOC_TARGET_SIZE in which the relocation value
+ * will go.
+ *
+ * RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
+ * with the bits of the relocation value. It may be assumed by the
+ * code that the relocation value will fit into this many bits. This
+ * may be larger than RELOC_TARGET_SIZE if such be useful.
+ *
+ *
+ * Things I haven't implemented
+ * ----------------------------
+ *
+ * Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
+ *
+ * Pc relative relocation for External references.
+ *
+ *
+ */
+
+
+/* Default macros */
+#ifndef RELOC_ADDRESS
+
+#define RELOC_ADDRESS(r) ((r)->r_address)
+#define RELOC_EXTERN_P(r) ((r)->r_extern)
+#define RELOC_TYPE(r) ((r)->r_symbolnum)
+#define RELOC_SYMBOL(r) ((r)->r_symbolnum)
+#define RELOC_MEMORY_SUB_P(r) 0
+#define RELOC_MEMORY_ADD_P(r) 1
+#undef RELOC_ADD_EXTRA
+#define RELOC_PCREL_P(r) ((r)->r_pcrel)
+#define RELOC_VALUE_RIGHTSHIFT(r) 0
+#if defined(RTLD) && defined(SUN_COMPAT)
+#define RELOC_TARGET_SIZE(r) (2) /* !!!!! Sun BUG compatible */
+#else
+#define RELOC_TARGET_SIZE(r) ((r)->r_length)
+#endif
+#define RELOC_TARGET_BITPOS(r) 0
+#define RELOC_TARGET_BITSIZE(r) 32
+
+#define RELOC_JMPTAB_P(r) ((r)->r_jmptable)
+#define RELOC_BASEREL_P(r) ((r)->r_baserel)
+#define RELOC_RELATIVE_P(r) ((r)->r_relative)
+#define RELOC_COPY_P(r) ((r)->r_copy)
+#define RELOC_LAZY_P(r) ((r)->r_jmptable)
+
+#define CHECK_GOT_RELOC(r) ((r)->r_pcrel)
+
+#endif
+
+/*
+ * Internal representation of relocation types
+ */
+#define RELTYPE_EXTERN 1
+#define RELTYPE_JMPSLOT 2
+#define RELTYPE_BASEREL 4
+#define RELTYPE_RELATIVE 8
+#define RELTYPE_COPY 16
+
+#ifdef nounderscore
+#define LPREFIX '.'
+#else
+#define LPREFIX 'L'
+#endif
+
+#ifndef TEXT_START
+#define TEXT_START(x) N_TXTADDR(x)
+#endif
+
+#ifndef DATA_START
+#define DATA_START(x) N_DATADDR(x)
+#endif
+
+/* If a this type of symbol is encountered, its name is a warning
+ message to print each time the symbol referenced by the next symbol
+ table entry is referenced.
+
+ This feature may be used to allow backwards compatibility with
+ certain functions (eg. gets) but to discourage programmers from
+ their use.
+
+ So if, for example, you wanted to have ld print a warning whenever
+ the function "gets" was used in their C program, you would add the
+ following to the assembler file in which gets is defined:
+
+ .stabs "Obsolete function \"gets\" referenced",30,0,0,0
+ .stabs "_gets",1,0,0,0
+
+ These .stabs do not necessarily have to be in the same file as the
+ gets function, they simply must exist somewhere in the compilation. */
+
+#ifndef N_WARNING
+#define N_WARNING 0x1E /* Warning message to print if symbol
+ included */
+#endif /* This is input to ld */
+
+/* Special global symbol types understood by GNU LD. */
+
+/* The following type indicates the definition of a symbol as being
+ an indirect reference to another symbol. The other symbol
+ appears as an undefined reference, immediately following this symbol.
+
+ Indirection is asymmetrical. The other symbol's value will be used
+ to satisfy requests for the indirect symbol, but not vice versa.
+ If the other symbol does not have a definition, libraries will
+ be searched to find a definition.
+
+ So, for example, the following two lines placed in an assembler
+ input file would result in an object file which would direct gnu ld
+ to resolve all references to symbol "foo" as references to symbol
+ "bar".
+
+ .stabs "_foo",11,0,0,0
+ .stabs "_bar",1,0,0,0
+
+ Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)). */
+
+#ifndef N_INDR
+#define N_INDR 0xa
+#endif
+
+/* The following symbols refer to set elements. These are expected
+ only in input to the loader; they should not appear in loader
+ output (unless relocatable output is requested). To be recognized
+ by the loader, the input symbols must have their N_EXT bit set.
+ All the N_SET[ATDB] symbols with the same name form one set. The
+ loader collects all of these elements at load time and outputs a
+ vector for each name.
+ Space (an array of 32 bit words) is allocated for the set in the
+ data section, and the n_value field of each set element value is
+ stored into one word of the array.
+ The first word of the array is the length of the set (number of
+ elements). The last word of the vector is set to zero for possible
+ use by incremental loaders. The array is ordered by the linkage
+ order; the first symbols which the linker encounters will be first
+ in the array.
+
+ In C syntax this looks like:
+
+ struct set_vector {
+ unsigned int length;
+ unsigned int vector[length];
+ unsigned int always_zero;
+ };
+
+ Before being placed into the array, each element is relocated
+ according to its type. This allows the loader to create an array
+ of pointers to objects automatically. N_SETA type symbols will not
+ be relocated.
+
+ The address of the set is made into an N_SETV symbol
+ whose name is the same as the name of the set.
+ This symbol acts like a N_DATA global symbol
+ in that it can satisfy undefined external references.
+
+ For the purposes of determining whether or not to load in a library
+ file, set element definitions are not considered "real
+ definitions"; they will not cause the loading of a library
+ member.
+
+ If relocatable output is requested, none of this processing is
+ done. The symbols are simply relocated and passed through to the
+ output file.
+
+ So, for example, the following three lines of assembler code
+ (whether in one file or scattered between several different ones)
+ will produce a three element vector (total length is five words;
+ see above), referenced by the symbol "_xyzzy", which will have the
+ addresses of the routines _init1, _init2, and _init3.
+
+ *NOTE*: If symbolic addresses are used in the n_value field of the
+ defining .stabs, those symbols must be defined in the same file as
+ that containing the .stabs.
+
+ .stabs "_xyzzy",23,0,0,_init1
+ .stabs "_xyzzy",23,0,0,_init2
+ .stabs "_xyzzy",23,0,0,_init3
+
+ Note that (23 == (N_SETT | N_EXT)). */
+
+#ifndef N_SETA
+#define N_SETA 0x14 /* Absolute set element symbol */
+#endif /* This is input to LD, in a .o file. */
+
+#ifndef N_SETT
+#define N_SETT 0x16 /* Text set element symbol */
+#endif /* This is input to LD, in a .o file. */
+
+#ifndef N_SETD
+#define N_SETD 0x18 /* Data set element symbol */
+#endif /* This is input to LD, in a .o file. */
+
+#ifndef N_SETB
+#define N_SETB 0x1A /* Bss set element symbol */
+#endif /* This is input to LD, in a .o file. */
+
+/* Macros dealing with the set element symbols defined in a.out.h */
+#define SET_ELEMENT_P(x) ((x) >= N_SETA && (x) <= (N_SETB|N_EXT))
+#define TYPE_OF_SET_ELEMENT(x) ((x) - N_SETA + N_ABS)
+
+#ifndef N_SETV
+#define N_SETV 0x1C /* Pointer to set vector in data area. */
+#endif /* This is output from LD. */
+
+
+#ifndef __GNU_STAB__
+
+/* Line number for the data section. This is to be used to describe
+ the source location of a variable declaration. */
+#ifndef N_DSLINE
+#define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
+#endif
+
+/* Line number for the bss section. This is to be used to describe
+ the source location of a variable declaration. */
+#ifndef N_BSLINE
+#define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
+#endif
+
+#endif /* not __GNU_STAB__ */
+
+/* Symbol table */
+
+/*
+ * Global symbol data is recorded in these structures, one for each global
+ * symbol. They are found via hashing in 'symtab', which points to a vector
+ * of buckets. Each bucket is a chain of these structures through the link
+ * field.
+ */
+
+typedef struct glosym {
+ /* Pointer to next symbol in this symbol's hash bucket. */
+ struct glosym *link;
+ /* Name of this symbol. */
+ char *name;
+ /* Value of this symbol as a global symbol. */
+ long value;
+ /*
+ * Chain of external 'nlist's in files for this symbol, both defs and
+ * refs.
+ */
+ struct localsymbol *refs;
+ /*
+ * Any warning message that might be associated with this symbol from
+ * an N_WARNING symbol encountered.
+ */
+ char *warning;
+ /*
+ * Nonzero means definitions of this symbol as common have been seen,
+ * and the value here is the largest size specified by any of them.
+ */
+ int max_common_size;
+ /*
+ * For relocatable_output, records the index of this global sym in
+ * the symbol table to be written, with the first global sym given
+ * index 0.
+ */
+ int symbolnum;
+ /*
+ * For dynamically linked output, records the index in the RRS
+ * symbol table.
+ */
+ int rrs_symbolnum;
+ /*
+ * Nonzero means a definition of this global symbol is known to
+ * exist. Library members should not be loaded on its account.
+ */
+ char defined;
+ /*
+ * Nonzero means a reference to this global symbol has been seen in a
+ * file that is surely being loaded. A value higher than 1 is the
+ * n_type code for the symbol's definition.
+ */
+ char referenced;
+ /*
+ * A count of the number of undefined references printed for a
+ * specific symbol. If a symbol is unresolved at the end of
+ * digest_symbols (and the loading run is supposed to produce
+ * relocatable output) do_file_warnings keeps track of how many
+ * unresolved reference error messages have been printed for each
+ * symbol here. When the number hits MAX_UREFS_PRINTED, messages
+ * stop.
+ */
+ unsigned char undef_refs;
+ /*
+ * 1 means that this symbol has multiple definitions. 2 means that
+ * it has multiple definitions, and some of them are set elements,
+ * one of which has been printed out already.
+ */
+ unsigned char multiply_defined;
+ /* Nonzero means print a message at all refs or defs of this symbol */
+ char trace;
+
+ /*
+ * For symbols of type N_INDR, this points at the real symbol.
+ */
+ struct glosym *alias;
+
+ /*
+ * Count number of elements in set vector if symbol is of type N_SETV
+ */
+ int setv_count;
+
+ /* Dynamic lib support */
+
+ /*
+ * Nonzero means a definition of this global symbol has been found
+ * in a shared object. These symbols do not go into the symbol
+ * section of the resulting a.out file. They *do* go into the
+ * dynamic link information segment.
+ */
+ char so_defined;
+
+ /* Size of symbol as determined by N_SIZE 'nlist's in object files */
+ int size;
+
+ /*
+ * Chain of external 'nlist's in shared objects for this symbol, both
+ * defs and refs.
+ */
+ struct localsymbol *sorefs;
+
+ /* The offset into one of the RRS tables, -1 if not used */
+ long jmpslot_offset;
+ char jmpslot_claimed;
+
+ long gotslot_offset;
+ char gotslot_claimed;
+
+ char cpyreloc_reserved;
+ char cpyreloc_claimed;
+
+ /* The local symbol that gave this global symbol its definition */
+ struct nlist *def_nlist;
+} symbol;
+
+/* Number of buckets in symbol hash table */
+#define TABSIZE 1009
+
+/* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
+symbol *symtab[TABSIZE];
+#define FOR_EACH_SYMBOL(i,sp) { \
+ int i; \
+ for (i = 0; i < TABSIZE; i++) { \
+ register symbol *sp; \
+ for (sp = symtab[i]; sp; sp = sp->link)
+
+#define END_EACH_SYMBOL }}
+
+/* Number of symbols in symbol hash table. */
+int num_hash_tab_syms;
+
+/* Count the number of nlist entries that are for local symbols.
+ This count and the three following counts
+ are incremented as as symbols are entered in the symbol table. */
+int local_sym_count;
+
+/* Count number of nlist entries that are for local symbols
+ whose names don't start with L. */
+int non_L_local_sym_count;
+
+/* Count the number of nlist entries for debugger info. */
+int debugger_sym_count;
+
+/* Count the number of global symbols referenced and not defined. */
+int undefined_global_sym_count;
+
+/* Count the number of symbols referenced from shared objects and not defined */
+int undefined_shobj_sym_count;
+
+/* Count the number of global symbols multiply defined. */
+int multiple_def_count;
+
+/* Count the number of defined global symbols.
+ Each symbol is counted only once
+ regardless of how many different nlist entries refer to it,
+ since the output file will need only one nlist entry for it.
+ This count is computed by `digest_symbols';
+ it is undefined while symbols are being loaded. */
+int defined_global_sym_count;
+
+/* Count the number of symbols defined through common declarations.
+ This count is kept in symdef_library, linear_library, and
+ enter_global_ref. It is incremented when the defined flag is set
+ in a symbol because of a common definition, and decremented when
+ the symbol is defined "for real" (ie. by something besides a common
+ definition). */
+int common_defined_global_count;
+
+/* Count the number of linker defined symbols.
+ XXX - Currently, only __DYNAMIC and _G_O_T_ go here if required,
+ perhaps _etext, _edata and _end should go here too */
+int special_sym_count;
+
+/* Count number of aliased symbols */
+int global_alias_count;
+
+/* Count number of set element type symbols and the number of separate
+ vectors which these symbols will fit into */
+int set_symbol_count;
+int set_vector_count;
+
+/* Define a linked list of strings which define symbols which should
+ be treated as set elements even though they aren't. Any symbol
+ with a prefix matching one of these should be treated as a set
+ element.
+
+ This is to make up for deficiencies in many assemblers which aren't
+ willing to pass any stabs through to the loader which they don't
+ understand. */
+struct string_list_element {
+ char *str;
+ struct string_list_element *next;
+};
+
+struct string_list_element *set_element_prefixes;
+
+/* Count the number of warning symbols encountered. */
+int warning_count;
+
+/* 1 => write load map. */
+int write_map;
+
+/* 1 => write relocation into output file so can re-input it later. */
+int relocatable_output;
+
+/* Nonzero means ptr to symbol entry for symbol to use as start addr.
+ -e sets this. */
+symbol *entry_symbol;
+
+symbol *edata_symbol; /* the symbol _edata */
+symbol *etext_symbol; /* the symbol _etext */
+symbol *end_symbol; /* the symbol _end */
+symbol *got_symbol; /* the symbol __GLOBAL_OFFSET_TABLE_ */
+symbol *dynamic_symbol; /* the symbol __DYNAMIC */
+
+
+/*
+ * Each input file, and each library member ("subfile") being loaded, has a
+ * `file_entry' structure for it.
+ *
+ * For files specified by command args, these are contained in the vector which
+ * `file_table' points to.
+ *
+ * For library members, they are dynamically allocated, and chained through the
+ * `chain' field. The chain is found in the `subfiles' field of the
+ * `file_entry'. The `file_entry' objects for the members have `superfile'
+ * fields pointing to the one for the library.
+ */
+
+struct file_entry {
+ /* Name of this file. */
+ char *filename;
+
+ /*
+ * Name to use for the symbol giving address of text start Usually
+ * the same as filename, but for a file spec'd with -l this is the -l
+ * switch itself rather than the filename.
+ */
+ char *local_sym_name;
+
+ /* Describe the layout of the contents of the file */
+
+ /* The file's a.out header. */
+ struct exec header;
+ /* Offset in file of GDB symbol segment, or 0 if there is none. */
+ int symseg_offset;
+
+ /* Describe data from the file loaded into core */
+
+ /*
+ * Symbol table of the file.
+ * We need access to the global symbol early, ie. before
+ * symbols are asssigned there final values. gotslot_offset is
+ * here because GOT entries may be generated for local symbols.
+ */
+ struct localsymbol {
+ struct nzlist nzlist;
+ struct glosym *symbol;
+ struct localsymbol *next;
+ long gotslot_offset;
+ char gotslot_claimed;
+ } *symbols;
+
+ /* Number of symbols in above array. */
+ int nsymbols;
+
+ /* Size in bytes of string table. */
+ int string_size;
+
+ /*
+ * Pointer to the string table. The string table is not kept in core
+ * all the time, but when it is in core, its address is here.
+ */
+ char *strings;
+
+ /* Offset of string table (normally N_STROFF() + 4) */
+ int strings_offset;
+
+ /* Next two used only if `relocatable_output' or if needed for */
+ /* output of undefined reference line numbers. */
+
+ /* Text reloc info saved by `write_text' for `coptxtrel'. */
+ struct relocation_info *textrel;
+ int ntextrel;
+
+ /* Data reloc info saved by `write_data' for `copdatrel'. */
+ struct relocation_info *datarel;
+ int ndatarel;
+
+ /* Relation of this file's segments to the output file */
+
+ /* Start of this file's text seg in the output file core image. */
+ int text_start_address;
+
+ /* Start of this file's data seg in the output file core image. */
+ int data_start_address;
+
+ /* Start of this file's bss seg in the output file core image. */
+ int bss_start_address;
+ /*
+ * Offset in bytes in the output file symbol table of the first local
+ * symbol for this file. Set by `write_file_symbols'.
+ */
+ int local_syms_offset;
+
+ /* For library members only */
+
+ /* For a library, points to chain of entries for the library members. */
+ struct file_entry *subfiles;
+
+ /*
+ * For a library member, offset of the member within the archive.
+ * Zero for files that are not library members.
+ */
+ int starting_offset;
+
+ /* Size of contents of this file, if library member. */
+ int total_size;
+
+ /* For library member, points to the library's own entry. */
+ struct file_entry *superfile;
+
+ /* For library member, points to next entry for next member. */
+ struct file_entry *chain;
+
+ /* 1 if file is a library. */
+ char library_flag;
+
+ /* 1 if file's header has been read into this structure. */
+ char header_read_flag;
+
+ /* 1 means search a set of directories for this file. */
+ char search_dirs_flag;
+
+ /*
+ * 1 means this is base file of incremental load. Do not load this
+ * file's text or data. Also default text_start to after this file's
+ * bss.
+ */
+ char just_syms_flag;
+
+ /* 1 means search for dynamic libraries (dependent on -B switch) */
+ char search_dynamic_flag;
+
+ /* version numbers of selected shared library */
+ int lib_major, lib_minor;
+
+ /* This entry is a shared object */
+ char is_dynamic;
+};
+
+typedef struct localsymbol localsymbol_t;
+
+/* Vector of entries for input files specified by arguments.
+ These are all the input files except for members of specified libraries. */
+struct file_entry *file_table;
+
+/* Length of that vector. */
+int number_of_files;
+
+/* Current link mode */
+#define DYNAMIC 1 /* Consider shared libraries */
+#define SYMBOLIC 2 /* Force symbolic resolution */
+#define FORCEARCHIVE 4 /* Force inclusion of all members
+ of archives */
+#define SHAREABLE 8 /* Build a shared object */
+int link_mode;
+
+/*
+ * Runtime Relocation Section (RRS).
+ * This describes the data structures that go into the output text and data
+ * segments to support the run-time linker. The RRS can be empty (plain old
+ * static linking), or can just exist of GOT and PLT entries (in case of
+ * statically linked PIC code).
+ */
+
+int rrs_section_type;
+#define RRS_NONE 0
+#define RRS_PARTIAL 1
+#define RRS_FULL 2
+
+int rrs_text_size;
+int rrs_data_size;
+int rrs_text_start;
+int rrs_data_start;
+
+/* Version number to put in __DYNAMIC (set by -V) */
+int soversion;
+
+/* When loading the text and data, we can avoid doing a close
+ and another open between members of the same library.
+
+ These two variables remember the file that is currently open.
+ Both are zero if no file is open.
+
+ See `each_file' and `file_close'. */
+
+struct file_entry *input_file;
+int input_desc;
+
+/* The name of the file to write; "a.out" by default. */
+
+char *output_filename;
+
+/* Descriptor for writing that file with `mywrite'. */
+
+int outdesc;
+
+/* Header for that file (filled in by `write_header'). */
+
+struct exec outheader;
+
+/* The following are computed by `digest_symbols'. */
+
+int text_size; /* total size of text of all input files. */
+int data_size; /* total size of data of all input files. */
+int bss_size; /* total size of bss of all input files. */
+int text_reloc_size; /* total size of text relocation of all input files. */
+int data_reloc_size; /* total size of data relocation of all input files. */
+
+/* Relocation offsets set by perform_relocation(). Defined globaly here
+ because some of the RRS routines need access to them */
+int text_relocation;
+int data_relocation;
+int bss_relocation;
+int pc_relocation;
+
+/* Specifications of start and length of the area reserved at the end
+ of the data segment for the set vectors. Computed in 'digest_symbols' */
+int set_sect_start;
+int set_sect_size;
+
+/* Amount of cleared space to leave between the text and data segments. */
+int text_pad;
+
+/* Amount of bss segment to include as part of the data segment. */
+int data_pad;
+
+
+/* Record most of the command options. */
+
+/* Address we assume the text section will be loaded at.
+ We relocate symbols and text and data for this, but we do not
+ write any padding in the output file for it. */
+int text_start;
+
+/* Offset of default entry-pc within the text section. */
+int entry_offset;
+
+/* Address we decide the data section will be loaded at. */
+int data_start;
+int bss_start;
+
+/* Keep a list of any symbols referenced from the command line (so
+ that error messages for these guys can be generated). This list is
+ zero terminated. */
+struct glosym **cmdline_references;
+int cl_refs_allocated;
+
+/*
+ * Actual vector of directories to search; this contains those specified with
+ * -L plus the standard ones.
+ */
+char **search_dirs;
+
+/* Length of the vector `search_dirs'. */
+int n_search_dirs;
+
+void digest_symbols __P((void));
+void load_symbols __P((void));
+void decode_command __P((int, char **));
+void read_header __P((int, struct file_entry *));
+void read_entry_symbols __P((int, struct file_entry *));
+void read_entry_strings __P((int, struct file_entry *));
+void read_entry_relocation __P((int, struct file_entry *));
+void write_output __P((void));
+void write_header __P((void));
+void write_text __P((void));
+void write_data __P((void));
+void write_rel __P((void));
+void write_syms __P((void));
+void write_symsegs __P((void));
+void mywrite ();
+
+/* In warnings.c: */
+void perror_name __P((char *));
+void perror_file __P((struct file_entry *));
+void fatal_with_file __P((char *, struct file_entry *, ...));
+void print_symbols __P((FILE *));
+char *get_file_name __P((struct file_entry *));
+void print_file_name __P((struct file_entry *, FILE *));
+void prline_file_name __P((struct file_entry *, FILE *));
+int do_warnings __P((FILE *));
+
+/* In etc.c: */
+void *xmalloc __P((int));
+void *xrealloc __P((void *, int));
+void fatal __P((char *, ...));
+void error __P((char *, ...));
+void padfile __P((int,int));
+char *concat __P((char *, char *, char *));
+int parse __P((char *, char *, char *));
+
+/* In symbol.c: */
+void symtab_init __P((int));
+symbol *getsym __P((char *)), *getsym_soft __P((char *));
+
+/* In lib.c: */
+void search_library __P((int, struct file_entry *));
+void read_shared_object __P((int, struct file_entry *));
+int findlib __P((struct file_entry *));
+
+/* In shlib.c: */
+char *findshlib __P((char *, int *, int *));
+void add_search_dir __P((char *));
+void std_search_dirs __P((char *));
+
+/* In rrs.c: */
+void init_rrs __P((void));
+void rrs_add_shobj __P((struct file_entry *));
+void alloc_rrs_reloc __P((symbol *));
+void alloc_rrs_segment_reloc __P((struct relocation_info *));
+void alloc_rrs_jmpslot __P((symbol *));
+void alloc_rrs_gotslot __P((struct relocation_info *, localsymbol_t *));
+void alloc_rrs_copy_reloc __P((symbol *));
+
+/* In <md>.c */
+void md_init_header __P((struct exec *, int, int));
+long md_get_addend __P((struct relocation_info *, unsigned char *));
+void md_relocate __P((struct relocation_info *, long, unsigned char *, int));
+void md_make_jmpslot __P((jmpslot_t *, long, long));
+void md_fix_jmpslot __P((jmpslot_t *, long, u_long));
+int md_make_reloc __P((struct relocation_info *, struct relocation_info *, int));
+void md_make_jmpreloc __P((struct relocation_info *, struct relocation_info *, int));
+void md_make_gotreloc __P((struct relocation_info *, struct relocation_info *, int));
+void md_make_copyreloc __P((struct relocation_info *, struct relocation_info *));
+
+#ifdef NEED_SWAP
+void md_swapin_exec_hdr __P((struct exec *));
+void md_swapout_exec_hdr __P((struct exec *));
+void md_swapin_reloc __P((struct relocation_info *, int));
+void md_swapout_reloc __P((struct relocation_info *, int));
+void md_swapout_jmpslot __P((jmpslot_t *, int));
+
+/* In xbits.c: */
+void swap_longs __P((long *, int));
+void swap_symbols __P((struct nlist *, int));
+void swap_zsymbols __P((struct nzlist *, int));
+void swap_ranlib_hdr __P((struct ranlib *, int));
+void swap_link_dynamic __P((struct link_dynamic *));
+void swap_link_dynamic_2 __P((struct link_dynamic_2 *));
+void swap_ld_debug __P((struct ld_debug *));
+void swapin_link_object __P((struct link_object *, int));
+void swapout_link_object __P((struct link_object *, int));
+void swapout_fshash __P((struct fshash *, int));
+#endif
diff --git a/gnu/usr.bin/ld/ldconfig/Makefile b/gnu/usr.bin/ld/ldconfig/Makefile
new file mode 100644
index 0000000..e655e26
--- /dev/null
+++ b/gnu/usr.bin/ld/ldconfig/Makefile
@@ -0,0 +1,13 @@
+# $Id: Makefile,v 1.2 1993/11/03 05:20:49 cgd Exp $
+
+PROG= ldconfig
+SRCS= ldconfig.c shlib.c etc.c
+LDDIR?= $(.CURDIR)/..
+LDFLAGS += -static
+CFLAGS += -I$(LDDIR) -I$(.CURDIR) -I$(LDDIR)/$(MACHINE) -O
+BINDIR= ${DESTDIR}/sbin
+MAN8 = ldconfig.0
+
+.PATH: $(LDDIR) $(LDDIR)/$(MACHINE)
+
+.include <bsd.prog.mk>
diff --git a/gnu/usr.bin/ld/ldconfig/ldconfig.8 b/gnu/usr.bin/ld/ldconfig/ldconfig.8
new file mode 100644
index 0000000..e363269
--- /dev/null
+++ b/gnu/usr.bin/ld/ldconfig/ldconfig.8
@@ -0,0 +1,98 @@
+.Dd October 3, 1993
+.Dt LDCONFIG 8
+.Os NetBSD 0.9
+.Sh NAME
+.Nm ldconfig
+.Nd configure the shared library cache
+.Sh SYNOPSIS
+.Nm ldconfig
+.Op Fl rsv
+.Op Ar directory Ar ...
+.Sh DESCRIPTION
+.Nm
+is used to prepare a set of
+.Dq hints
+for use by the run-time linker
+.Xr ld.so
+to facilitate quick lookup of shared libraries available in multiple
+directories. It scans a set of built-in system directories and any
+.Ar directories
+specified on the command line (in the given order) looking for shared
+libraries and stores the results in the file
+.Xr /var/run/ld.so.hints
+to forstall the overhead that would otherwise result from the
+directory search operations
+.Xr ld.so
+would have to perform to load the required shared libraries.
+.Pp
+The shared libraries so found will be automatically available for loading
+if needed by the program being prepared for execution. This obviates the need
+for storing search paths within the executable.
+.Pp
+The
+.Ev LD_LIBRARY_PATH
+environment variable can be used to override the use of
+directories (or the order thereof) from the cache or to specify additional
+directories where shared libraries might be found.
+.Ev LD_LIBRARY_PATH
+is a
+.Sq \:
+separated list of directory paths which are searched by
+.Xr ld.so
+when it needs to load a shared library. It can be viewed as the run-time
+equivalent of the
+.Fl L
+switch of
+.Xr ld.
+.Pp
+.Nm Ldconfig
+is typically run as part of the boot sequence.
+.Pp
+The following options recognized by
+.Nm ldconfig:
+.Bl -tag -width indent
+.It Fl r
+Lists the current contents of
+.Xr ld.so.hints
+on the standard output. The hints file will not be modified.
+.It Fl s
+Do not scan
+.Nm ldconfig
+'s builtin system directories
+.Sq /usr/lib
+and
+.Sq /usr/local/lib
+for shared libraries.
+.It Fl v
+Switch on verbose mode.
+.Sh Security
+Special care must be taken when loading shared libraries into the address
+space of
+.Ev set-user-Id
+programs. Whenever such a program is run,
+.Xr ld.so
+will only load shared libraries from the
+.Ev ld.so.hints
+file. In particular, the
+.Ev LD_LIBRARY_PATH
+is not used to search for libraries. Thus, the role of ldconfig is dual. In
+addition to building a set of hints for quick lookup, it also serves to
+specify the trusted collection of directories from which shared objects can
+be safely loaded. It is presumed that the set of directories specified to
+.Nm ldconfig
+are under control of the system's administrator.
+.Xr ld.so
+further assists set-user-Id programs by erasing the
+.Ev LD_LIBRARY_PATH
+from the environment.
+
+.Sh FILES
+.Xr /var/run/ld.so.hints
+.Sh SEE ALSO
+.Xr ld 1 ,
+.Xr link 5
+.Sh HISTORY
+A
+.Nm
+utility first appeared in SunOS 4.0, it appeared in its current form
+in NetBSD 0.9a.
diff --git a/gnu/usr.bin/ld/ldconfig/ldconfig.c b/gnu/usr.bin/ld/ldconfig/ldconfig.c
new file mode 100644
index 0000000..b4b9981
--- /dev/null
+++ b/gnu/usr.bin/ld/ldconfig/ldconfig.c
@@ -0,0 +1,411 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: ldconfig.c,v 1.1 1993/10/23 00:17:03 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <ar.h>
+#include <ranlib.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+#include <dirent.h>
+
+#include "ld.h"
+
+#undef major
+#undef minor
+
+char *progname;
+static int verbose;
+static int nostd;
+static int justread;
+
+#define MAXDEWEY 8
+struct shlib_list {
+ /* Internal list of shared libraries found */
+ char *name;
+ char *path;
+ int dewey[MAXDEWEY];
+ int ndewey;
+#define major dewey[0]
+#define minor dewey[1]
+ struct shlib_list *next;
+};
+
+static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
+
+static void enter __P((char *, char *, char *, int *, int));
+static int dodir __P((char *));
+static int build_hints __P((void));
+
+int
+main(argc, argv)
+int argc;
+char *argv[];
+{
+ int i, c;
+ int rval = 0;
+ extern int optind;
+
+ if ((progname = strrchr(argv[0], '/')) == NULL)
+ progname = argv[0];
+ else
+ progname++;
+
+ while ((c = getopt(argc, argv, "rsv")) != EOF) {
+ switch (c) {
+ case 'v':
+ verbose = 1;
+ break;
+ case 's':
+ nostd = 1;
+ break;
+ case 'r':
+ justread = 1;
+ break;
+ default:
+ fprintf(stderr, "Usage: %s [-v] [dir ...]\n", progname);
+ exit(1);
+ break;
+ }
+ }
+
+ if (justread)
+ return listhints();
+
+ if (!nostd)
+ std_search_dirs(NULL);
+
+ for (i = 0; i < n_search_dirs; i++)
+ rval |= dodir(search_dirs[i]);
+
+ for (i = optind; i < argc; i++)
+ rval |= dodir(argv[i]);
+
+ rval |= build_hints();
+
+ return rval;
+}
+
+int
+dodir(dir)
+char *dir;
+{
+ DIR *dd;
+ struct dirent *dp;
+ char name[MAXPATHLEN], rest[MAXPATHLEN];
+ int dewey[MAXDEWEY], ndewey;
+
+ if ((dd = opendir(dir)) == NULL) {
+ perror(dir);
+ return -1;
+ }
+
+ while ((dp = readdir(dd)) != NULL) {
+ int n;
+
+ name[0] = rest[0] = '\0';
+
+ n = sscanf(dp->d_name, "lib%[^.].so.%s",
+ name, rest);
+
+ if (n < 2 || rest[0] == '\0')
+ continue;
+
+ ndewey = getdewey(dewey, rest);
+ enter(dir, dp->d_name, name, dewey, ndewey);
+ }
+
+ return 0;
+}
+
+static void
+enter(dir, file, name, dewey, ndewey)
+char *dir, *file, *name;
+int dewey[], ndewey;
+{
+ struct shlib_list *shp;
+
+ for (shp = shlib_head; shp; shp = shp->next) {
+ if (strcmp(name, shp->name) != 0 || major != shp->major)
+ continue;
+
+ /* Name matches existing entry */
+ if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
+
+ /* Update this entry with higher versioned lib */
+ if (verbose)
+ printf("Updating lib%s.%d.%d to %s/%s\n",
+ shp->name, shp->major, shp->minor,
+ dir, file);
+
+ free(shp->name);
+ shp->name = strdup(name);
+ free(shp->path);
+ shp->path = concat(dir, "/", file);
+ bcopy(dewey, shp->dewey, sizeof(shp->dewey));
+ shp->ndewey = ndewey;
+ }
+ break;
+ }
+
+ if (shp)
+ /* Name exists: older version or just updated */
+ return;
+
+ /* Allocate new list element */
+ if (verbose)
+ printf("Adding %s/%s\n", dir, file);
+
+ shp = (struct shlib_list *)xmalloc(sizeof *shp);
+ shp->name = strdup(name);
+ shp->path = concat(dir, "/", file);
+ bcopy(dewey, shp->dewey, MAXDEWEY);
+ shp->ndewey = ndewey;
+ shp->next = NULL;
+
+ *shlib_tail = shp;
+ shlib_tail = &shp->next;
+}
+
+
+#if DEBUG
+/* test */
+#undef _PATH_LD_HINTS
+#define _PATH_LD_HINTS "./ld.so.hints"
+#endif
+
+int
+hinthash(cp, vmajor, vminor)
+char *cp;
+int vmajor, vminor;
+{
+ int k = 0;
+
+ while (*cp)
+ k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
+
+ k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
+ k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
+
+ return k;
+}
+
+int
+build_hints()
+{
+ struct hints_header hdr;
+ struct hints_bucket *blist;
+ struct shlib_list *shp;
+ char *strtab;
+ int i, n, str_index = 0;
+ int strtab_sz = 0; /* Total length of strings */
+ int nhints = 0; /* Total number of hints */
+ int fd;
+ char *tmpfile;
+
+ for (shp = shlib_head; shp; shp = shp->next) {
+ strtab_sz += 1 + strlen(shp->name);
+ strtab_sz += 1 + strlen(shp->path);
+ nhints++;
+ }
+
+ /* Fill hints file header */
+ hdr.hh_magic = HH_MAGIC;
+ hdr.hh_version = LD_HINTS_VERSION_1;
+ hdr.hh_nbucket = 1 * nhints;
+ n = hdr.hh_nbucket * sizeof(struct hints_bucket);
+ hdr.hh_hashtab = sizeof(struct hints_header);
+ hdr.hh_strtab = hdr.hh_hashtab + n;
+ hdr.hh_strtab_sz = strtab_sz;
+ hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
+
+ if (verbose)
+ printf("Totals: entries %d, buckets %d, string size %d\n",
+ nhints, hdr.hh_nbucket, strtab_sz);
+
+ /* Allocate buckets and string table */
+ blist = (struct hints_bucket *)xmalloc(n);
+ bzero((char *)blist, n);
+ for (i = 0; i < hdr.hh_nbucket; i++)
+ /* Empty all buckets */
+ blist[i].hi_next = -1;
+
+ strtab = (char *)xmalloc(strtab_sz);
+
+ /* Enter all */
+ for (shp = shlib_head; shp; shp = shp->next) {
+ struct hints_bucket *bp;
+
+ bp = blist +
+ (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
+
+ if (bp->hi_pathx) {
+ int i;
+
+ for (i = 0; i < hdr.hh_nbucket; i++) {
+ if (blist[i].hi_pathx == 0)
+ break;
+ }
+ if (i == hdr.hh_nbucket) {
+ fprintf(stderr, "Bummer!\n");
+ return -1;
+ }
+ while (bp->hi_next != -1)
+ bp = &blist[bp->hi_next];
+ bp->hi_next = i;
+ bp = blist + i;
+ }
+
+ /* Insert strings in string table */
+ bp->hi_namex = str_index;
+ strcpy(strtab + str_index, shp->name);
+ str_index += 1 + strlen(shp->name);
+
+ bp->hi_pathx = str_index;
+ strcpy(strtab + str_index, shp->path);
+ str_index += 1 + strlen(shp->path);
+
+ /* Copy versions */
+ bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
+ bp->hi_ndewey = shp->ndewey;
+ }
+
+ tmpfile = concat(_PATH_LD_HINTS, "+", "");
+ if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+
+ mywrite(&hdr, 1, sizeof(struct hints_header), fd);
+ mywrite(blist, hdr.hh_nbucket, sizeof(struct hints_bucket), fd);
+ mywrite(strtab, strtab_sz, 1, fd);
+
+ if (close(fd) != 0) {
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+
+ /* Now, install real file */
+ if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+
+ if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+listhints()
+{
+ int fd;
+ caddr_t addr;
+ long msize;
+ struct hints_header *hdr;
+ struct hints_bucket *blist;
+ char *strtab;
+ int i;
+
+ if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+
+ msize = PAGSIZ;
+ addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
+
+ if (addr == (caddr_t)-1) {
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+
+ hdr = (struct hints_header *)addr;
+ if (HH_BADMAG(*hdr)) {
+ fprintf(stderr, "%s: Bad magic: %d\n");
+ return -1;
+ }
+
+ if (hdr->hh_version != LD_HINTS_VERSION_1) {
+ fprintf(stderr, "Unsupported version: %d\n", hdr->hh_version);
+ return -1;
+ }
+
+ if (hdr->hh_ehints > msize) {
+ if (mmap(addr+msize, hdr->hh_ehints - msize,
+ PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
+ fd, msize) != (caddr_t)(addr+msize)) {
+
+ perror(_PATH_LD_HINTS);
+ return -1;
+ }
+ }
+ close(fd);
+
+ blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
+ strtab = (char *)(addr + hdr->hh_strtab);
+
+ printf("%s:\n", _PATH_LD_HINTS);
+ for (i = 0; i < hdr->hh_nbucket; i++) {
+ struct hints_bucket *bp = &blist[i];
+
+ /* Sanity check */
+ if (bp->hi_namex >= hdr->hh_strtab_sz) {
+ fprintf(stderr, "Bad name index: %#x\n", bp->hi_namex);
+ return -1;
+ }
+ if (bp->hi_pathx >= hdr->hh_strtab_sz) {
+ fprintf(stderr, "Bad path index: %#x\n", bp->hi_pathx);
+ return -1;
+ }
+
+ printf("\t-l%s.%d.%d => %s\n",
+ strtab + bp->hi_namex, bp->hi_major, bp->hi_minor,
+ strtab + bp->hi_pathx);
+ }
+
+ return 0;
+}
+
diff --git a/gnu/usr.bin/ld/ldd/Makefile b/gnu/usr.bin/ld/ldd/Makefile
new file mode 100644
index 0000000..ded0797
--- /dev/null
+++ b/gnu/usr.bin/ld/ldd/Makefile
@@ -0,0 +1,7 @@
+# $Id: Makefile,v 1.2 1993/10/22 21:10:42 pk Exp $
+
+PROG= ldd
+SRCS= ldd.c
+BINDIR= ${DESTDIR}/usr/bin
+
+.include <bsd.prog.mk>
diff --git a/gnu/usr.bin/ld/ldd/ldd.1 b/gnu/usr.bin/ld/ldd/ldd.1
new file mode 100644
index 0000000..100468e
--- /dev/null
+++ b/gnu/usr.bin/ld/ldd/ldd.1
@@ -0,0 +1,25 @@
+.Dd October 22, 1993
+.Dt LDD 1
+.Os NetBSD 0.9
+.Sh NAME
+.Nm ldd
+.Nd list dynamic object dependencies
+.Sh SYNOPSIS
+.Nm ldd
+.Op Ar filename Ar ...
+.Sh DESCRIPTION
+.Nm ldd
+displays all shared objects that are needed to run the given program.
+Contrary to nm(1), the list includes
+.Dq indirect
+depedencies that are the result of needed shared objects which themselves
+depend on yet other shared objects.
+.Sh SEE ALSO
+.Xr ld 1 ,
+.Xr ld.so 1 ,
+.Xr nm 1
+.Sh HISTORY
+A
+.Nm ldd
+utility first appeared in SunOS 4.0, it appeared in its current form
+in NetBSD 0.9a.
diff --git a/gnu/usr.bin/ld/ldd/ldd.c b/gnu/usr.bin/ld/ldd/ldd.c
new file mode 100644
index 0000000..65b0bc1
--- /dev/null
+++ b/gnu/usr.bin/ld/ldd/ldd.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: ldd.c,v 1.3 1993/10/31 14:54:29 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <sys/wait.h>
+#include <a.out.h>
+
+static char *progname;
+
+void
+usage()
+{
+ fprintf(stderr, "Usage: %s <filename> ...\n", progname);
+}
+
+int
+main(argc, argv)
+int argc;
+char *argv[];
+{
+ int rval = 0;
+ int c;
+ extern int optind;
+
+ if ((progname = strrchr(argv[0], '/')) == NULL)
+ progname = argv[0];
+ else
+ progname++;
+
+ while ((c = getopt(argc, argv, "")) != EOF) {
+ switch (c) {
+ default:
+ usage();
+ exit(1);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc <= 0) {
+ usage();
+ exit(1);
+ }
+
+ /* ld.so magic */
+ setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
+
+ while (argc--) {
+ int fd;
+ struct exec hdr;
+ int status;
+
+ if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
+ perror(*argv);
+ rval |= 1;
+ argv++;
+ continue;
+ }
+ if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
+ !(N_GETFLAG(hdr) & EX_DYNAMIC)) {
+ fprintf(stderr, "%s: not a dynamic executable\n",
+ *argv);
+ (void)close(fd);
+ rval |= 1;
+ argv++;
+ continue;
+ }
+ (void)close(fd);
+
+ printf("%s:\n", *argv);
+
+ switch (fork()) {
+ case -1:
+ perror("fork");
+ exit(1);
+ break;
+ default:
+ if (wait(&status) <= 0)
+ perror("wait");
+
+ if (WIFSIGNALED(status)) {
+ fprintf(stderr, "%s: signal %d\n",
+ *argv, WTERMSIG(status));
+ rval |= 1;
+ } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
+ fprintf(stderr, "%s: exit status %d\n",
+ *argv, WEXITSTATUS(status));
+ rval |= 1;
+ }
+ break;
+ case 0:
+ rval != execl(*argv, *argv, NULL) != 0;
+ perror(*argv);
+ _exit(1);
+ }
+ argv++;
+ }
+
+ return rval;
+}
diff --git a/gnu/usr.bin/ld/lib.c b/gnu/usr.bin/ld/lib.c
new file mode 100644
index 0000000..2ce8efb
--- /dev/null
+++ b/gnu/usr.bin/ld/lib.c
@@ -0,0 +1,640 @@
+/*
+ * $Id: lib.c,v 1.3 1993/11/01 16:26:17 pk Exp $ - library routines
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <ar.h>
+#include <ranlib.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+#include <dirent.h>
+
+#include "ld.h"
+
+char **search_dirs;
+
+/* Length of the vector `search_dirs'. */
+int n_search_dirs;
+
+struct file_entry *decode_library_subfile();
+void linear_library(), symdef_library();
+
+/*
+ * Search the library ENTRY, already open on descriptor DESC. This means
+ * deciding which library members to load, making a chain of `struct
+ * file_entry' for those members, and entering their global symbols in the
+ * hash table.
+ */
+
+void
+search_library(desc, entry)
+ int desc;
+ struct file_entry *entry;
+{
+ int member_length;
+ register char *name;
+ register struct file_entry *subentry;
+
+ if (!(link_mode & FORCEARCHIVE) && !undefined_global_sym_count)
+ return;
+
+ /* Examine its first member, which starts SARMAG bytes in. */
+ subentry = decode_library_subfile(desc, entry, SARMAG, &member_length);
+ if (!subentry)
+ return;
+
+ name = subentry->filename;
+ free(subentry);
+
+ /* Search via __.SYMDEF if that exists, else linearly. */
+
+ if (!strcmp(name, "__.SYMDEF"))
+ symdef_library(desc, entry, member_length);
+ else
+ linear_library(desc, entry);
+}
+
+/*
+ * Construct and return a file_entry for a library member. The library's
+ * file_entry is library_entry, and the library is open on DESC.
+ * SUBFILE_OFFSET is the byte index in the library of this member's header.
+ * We store the length of the member into *LENGTH_LOC.
+ */
+
+struct file_entry *
+decode_library_subfile(desc, library_entry, subfile_offset, length_loc)
+ int desc;
+ struct file_entry *library_entry;
+ int subfile_offset;
+ int *length_loc;
+{
+ int bytes_read;
+ register int namelen;
+ int member_length;
+ register char *name;
+ struct ar_hdr hdr1;
+ register struct file_entry *subentry;
+
+ lseek(desc, subfile_offset, 0);
+
+ bytes_read = read(desc, &hdr1, sizeof hdr1);
+ if (!bytes_read)
+ return 0; /* end of archive */
+
+ if (sizeof hdr1 != bytes_read)
+ fatal_with_file("malformed library archive ", library_entry);
+
+ if (sscanf(hdr1.ar_size, "%d", &member_length) != 1)
+ fatal_with_file("malformatted header of archive member in ", library_entry);
+
+ subentry = (struct file_entry *) xmalloc(sizeof(struct file_entry));
+ bzero(subentry, sizeof(struct file_entry));
+
+ for (namelen = 0;
+ namelen < sizeof hdr1.ar_name
+ && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
+ && hdr1.ar_name[namelen] != '/';
+ namelen++);
+
+ name = (char *) xmalloc(namelen + 1);
+ strncpy(name, hdr1.ar_name, namelen);
+ name[namelen] = 0;
+
+ subentry->filename = name;
+ subentry->local_sym_name = name;
+ subentry->symbols = 0;
+ subentry->strings = 0;
+ subentry->subfiles = 0;
+ subentry->starting_offset = subfile_offset + sizeof hdr1;
+ subentry->superfile = library_entry;
+ subentry->library_flag = 0;
+ subentry->header_read_flag = 0;
+ subentry->just_syms_flag = 0;
+ subentry->chain = 0;
+ subentry->total_size = member_length;
+
+ (*length_loc) = member_length;
+
+ return subentry;
+}
+
+int subfile_wanted_p();
+
+/*
+ * Search a library that has a __.SYMDEF member. DESC is a descriptor on
+ * which the library is open. The file pointer is assumed to point at the
+ * __.SYMDEF data. ENTRY is the library's file_entry. MEMBER_LENGTH is the
+ * length of the __.SYMDEF data.
+ */
+
+void
+symdef_library(desc, entry, member_length)
+ int desc;
+ struct file_entry *entry;
+ int member_length;
+{
+ int *symdef_data = (int *) xmalloc(member_length);
+ register struct ranlib *symdef_base;
+ char *sym_name_base;
+ int number_of_symdefs;
+ int length_of_strings;
+ int not_finished;
+ int bytes_read;
+ register int i;
+ struct file_entry *prev = 0;
+ int prev_offset = 0;
+
+ bytes_read = read(desc, symdef_data, member_length);
+ if (bytes_read != member_length)
+ fatal_with_file("malformatted __.SYMDEF in ", entry);
+
+ number_of_symdefs = md_swap_long(*symdef_data) / sizeof(struct ranlib);
+ if (number_of_symdefs < 0 ||
+ number_of_symdefs * sizeof(struct ranlib) + 2 * sizeof(int) > member_length)
+ fatal_with_file("malformatted __.SYMDEF in ", entry);
+
+ symdef_base = (struct ranlib *) (symdef_data + 1);
+ length_of_strings = md_swap_long(*(int *) (symdef_base + number_of_symdefs));
+
+ if (length_of_strings < 0
+ || number_of_symdefs * sizeof(struct ranlib) + length_of_strings
+ + 2 * sizeof(int) > member_length)
+ fatal_with_file("malformatted __.SYMDEF in ", entry);
+
+ sym_name_base = sizeof(int) + (char *) (symdef_base + number_of_symdefs);
+
+ /* Check all the string indexes for validity. */
+ md_swapin_ranlib_hdr(symdef_base, number_of_symdefs);
+ for (i = 0; i < number_of_symdefs; i++) {
+ register int index = symdef_base[i].ran_un.ran_strx;
+ if (index < 0 || index >= length_of_strings
+ || (index && *(sym_name_base + index - 1)))
+ fatal_with_file("malformatted __.SYMDEF in ", entry);
+ }
+
+ /*
+ * Search the symdef data for members to load. Do this until one
+ * whole pass finds nothing to load.
+ */
+
+ not_finished = 1;
+ while (not_finished) {
+
+ not_finished = 0;
+
+ /*
+ * Scan all the symbols mentioned in the symdef for ones that
+ * we need. Load the library members that contain such
+ * symbols.
+ */
+
+ for (i = 0; (i < number_of_symdefs &&
+ ((link_mode & FORCEARCHIVE) ||
+ undefined_global_sym_count ||
+ common_defined_global_count)); i++) {
+
+ register symbol *sp;
+ int junk;
+ register int j;
+ register int offset = symdef_base[i].ran_off;
+ struct file_entry *subentry;
+
+
+ if (symdef_base[i].ran_un.ran_strx < 0)
+ continue;
+
+ sp = getsym_soft(sym_name_base
+ + symdef_base[i].ran_un.ran_strx);
+
+ /*
+ * If we find a symbol that appears to be needed,
+ * think carefully about the archive member that the
+ * symbol is in.
+ */
+
+ /*
+ * Per Mike Karels' recommendation, we no longer load
+ * library files if the only reference(s) that would
+ * be satisfied are 'common' references. This
+ * prevents some problems with name pollution (e.g. a
+ * global common 'utime' linked to a function).
+ */
+ if (!(link_mode & FORCEARCHIVE) &&
+ (!sp || !sp->referenced || sp->defined))
+ continue;
+
+ /*
+ * Don't think carefully about any archive member
+ * more than once in a given pass.
+ */
+
+ if (prev_offset == offset)
+ continue;
+ prev_offset = offset;
+
+ /*
+ * Read the symbol table of the archive member.
+ */
+
+ subentry = decode_library_subfile(desc,
+ entry, offset, &junk);
+ if (subentry == 0)
+ fatal(
+ "invalid offset for %s in symbol table of %s",
+ sym_name_base
+ + symdef_base[i].ran_un.ran_strx,
+ entry->filename);
+ read_entry_symbols(desc, subentry);
+ subentry->strings = (char *)
+ malloc(subentry->string_size);
+ read_entry_strings(desc, subentry);
+
+ /*
+ * Now scan the symbol table and decide whether to
+ * load.
+ */
+
+ if (!(link_mode & FORCEARCHIVE) &&
+ !subfile_wanted_p(subentry)) {
+ free(subentry->symbols);
+ free(subentry);
+ } else {
+ /*
+ * This member is needed; load it. Since we
+ * are loading something on this pass, we
+ * must make another pass through the symdef
+ * data.
+ */
+
+ not_finished = 1;
+
+ read_entry_relocation(desc, subentry);
+ enter_file_symbols(subentry);
+
+ if (prev)
+ prev->chain = subentry;
+ else
+ entry->subfiles = subentry;
+ prev = subentry;
+
+ /*
+ * Clear out this member's symbols from the
+ * symdef data so that following passes won't
+ * waste time on them.
+ */
+
+ for (j = 0; j < number_of_symdefs; j++) {
+ if (symdef_base[j].ran_off == offset)
+ symdef_base[j].ran_un.ran_strx = -1;
+ }
+ }
+
+ /*
+ * We'll read the strings again if we need them
+ * again.
+ */
+ free(subentry->strings);
+ subentry->strings = 0;
+ }
+ }
+
+ free(symdef_data);
+}
+
+/*
+ * Search a library that has no __.SYMDEF. ENTRY is the library's file_entry.
+ * DESC is the descriptor it is open on.
+ */
+
+void
+linear_library(desc, entry)
+ int desc;
+ struct file_entry *entry;
+{
+ register struct file_entry *prev = 0;
+ register int this_subfile_offset = SARMAG;
+
+ while ((link_mode & FORCEARCHIVE) ||
+ undefined_global_sym_count || common_defined_global_count) {
+
+ int member_length;
+ register struct file_entry *subentry;
+
+ subentry = decode_library_subfile(desc, entry, this_subfile_offset,
+ &member_length);
+
+ if (!subentry)
+ return;
+
+ read_entry_symbols(desc, subentry);
+ subentry->strings = (char *) alloca(subentry->string_size);
+ read_entry_strings(desc, subentry);
+
+ if (!(link_mode & FORCEARCHIVE) &&
+ !subfile_wanted_p(subentry)) {
+ free(subentry->symbols);
+ free(subentry);
+ } else {
+ read_entry_relocation(desc, subentry);
+ enter_file_symbols(subentry);
+
+ if (prev)
+ prev->chain = subentry;
+ else
+ entry->subfiles = subentry;
+ prev = subentry;
+ subentry->strings = 0; /* Since space will dissapear
+ * on return */
+ }
+
+ this_subfile_offset += member_length + sizeof(struct ar_hdr);
+ if (this_subfile_offset & 1)
+ this_subfile_offset++;
+ }
+}
+
+/*
+ * ENTRY is an entry for a library member. Its symbols have been read into
+ * core, but not entered. Return nonzero if we ought to load this member.
+ */
+
+int
+subfile_wanted_p(entry)
+ struct file_entry *entry;
+{
+ struct localsymbol *lsp, *lspend;
+#ifdef DOLLAR_KLUDGE
+ register int dollar_cond = 0;
+#endif
+
+ lspend = entry->symbols + entry->nsymbols;
+
+ for (lsp = entry->symbols; lsp < lspend; lsp++) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+ register int type = p->n_type;
+ register char *name = p->n_un.n_strx + entry->strings;
+ register symbol *sp = getsym_soft(name);
+
+ /*
+ * If the symbol has an interesting definition, we could
+ * potentially want it.
+ */
+ if (! (type & N_EXT)
+ || (type == (N_UNDF | N_EXT) && p->n_value == 0
+
+#ifdef DOLLAR_KLUDGE
+ && name[1] != '$'
+#endif
+ )
+#ifdef SET_ELEMENT_P
+ || SET_ELEMENT_P(type)
+ || set_element_prefixed_p(name)
+#endif
+ )
+ continue;
+
+
+#ifdef DOLLAR_KLUDGE
+ if (name[1] == '$') {
+ sp = getsym_soft(&name[2]);
+ dollar_cond = 1;
+ if (!sp)
+ continue;
+ if (sp->referenced) {
+ if (write_map) {
+ print_file_name(entry, stdout);
+ fprintf(stdout, " needed due to $-conditional %s\n", name);
+ }
+ return 1;
+ }
+ continue;
+ }
+#endif
+
+ /*
+ * If this symbol has not been hashed, we can't be
+ * looking for it.
+ */
+
+ if (!sp)
+ continue;
+
+ /*
+ * We don't load a file if it merely satisfies a
+ * common reference (see explanation above in
+ * symdef_library()).
+ */
+ if (sp->referenced && !sp->defined) {
+ /*
+ * This is a symbol we are looking for. It
+ * is either not yet defined or defined as a
+ * common.
+ */
+#ifdef DOLLAR_KLUDGE
+ if (dollar_cond)
+ continue;
+#endif
+ if (type == (N_UNDF | N_EXT)) {
+ /*
+ * Symbol being defined as common.
+ * Remember this, but don't load
+ * subfile just for this.
+ */
+
+ /*
+ * If it didn't used to be common, up
+ * the count of common symbols.
+ */
+ if (!sp->max_common_size)
+ common_defined_global_count++;
+
+ if (sp->max_common_size < p->n_value)
+ sp->max_common_size = p->n_value;
+ if (!sp->defined)
+ undefined_global_sym_count--;
+ sp->defined = type;
+ continue;
+ }
+ if (write_map) {
+ print_file_name(entry, stdout);
+ fprintf(stdout, " needed due to %s\n", sp->name);
+ }
+ return 1;
+ } else {
+ struct localsymbol *lsp;
+ int defs = 0;
+
+ /* Check for undefined symbols in shared objects */
+ for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
+ type = lsp->nzlist.nlist.n_type;
+ if ( (type & N_EXT) &&
+ (type & N_STAB) == 0 &&
+ type != (N_UNDF | N_EXT))
+ goto xxx;
+ }
+ if (write_map) {
+ print_file_name(entry, stdout);
+ fprintf(stdout, " needed due to shared lib ref %s\n", sp->name);
+ }
+ return 1;
+ xxx: ;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Read the symbols of dynamic entity ENTRY into core. Assume it is already
+ * open, on descriptor DESC.
+ */
+void
+read_shared_object (desc, entry)
+ struct file_entry *entry;
+ int desc;
+{
+ struct link_dynamic dyn;
+ struct link_dynamic_2 dyn2;
+ struct nlist *np;
+ struct nzlist *nzp;
+ int n, i, has_nz = 0;
+
+ if (!entry->header_read_flag)
+ read_header (desc, entry);
+
+ /* Read DYNAMIC structure (first in data segment) */
+ lseek (desc,
+ text_offset (entry) + entry->header.a_text,
+ L_SET);
+ if (read(desc, &dyn, sizeof dyn) != sizeof dyn) {
+ fatal_with_file (
+ "premature eof in data segment of ", entry);
+ }
+ md_swapin_link_dynamic(&dyn);
+
+ /* Check version */
+ switch (dyn.ld_version) {
+ default:
+ fatal_with_file( "unsupported _DYNAMIC version ", entry);
+ break;
+ case LD_VERSION_SUN:
+ break;
+ case LD_VERSION_BSD:
+ has_nz = 1;
+ break;
+ }
+
+ /* Read link_dynamic_2 struct (from data segment) */
+ lseek (desc,
+ text_offset(entry) + dyn.ld_un.ld_2,
+ L_SET);
+ if (read(desc, &dyn2, sizeof dyn2) != sizeof dyn2) {
+ fatal_with_file( "premature eof in data segment of ", entry);
+ }
+ md_swapin_link_dynamic_2(&dyn2);
+
+ /* Read symbols (text segment) */
+ n = dyn2.ld_strings - dyn2.ld_symbols;
+ entry->nsymbols = n /
+ (has_nz ? sizeof(struct nzlist) : sizeof(struct nlist));
+ nzp = (struct nzlist *)(np = (struct nlist *) alloca (n));
+ entry->symbols = (struct localsymbol *)
+ xmalloc(entry->nsymbols * sizeof(struct localsymbol));
+ lseek(desc, text_offset (entry) + dyn2.ld_symbols, L_SET);
+ if (read(desc, (char *)nzp, n) != n) {
+ fatal_with_file(
+ "premature eof while reading dyn syms ", entry);
+ }
+ if (has_nz)
+ md_swapin_zsymbols(nzp, entry->nsymbols);
+ else
+ md_swapin_symbols(np, entry->nsymbols);
+
+ /* Convert to structs localsymbol */
+ for (i = 0; i < entry->nsymbols; i++) {
+ if (has_nz) {
+ entry->symbols[i].nzlist = *nzp++;
+ } else {
+ entry->symbols[i].nzlist.nlist = *np++;
+ entry->symbols[i].nzlist.nz_size = 0;
+ }
+ entry->symbols[i].symbol = NULL;
+ entry->symbols[i].next = NULL;
+ entry->symbols[i].gotslot_offset = -1;
+ }
+
+ /* Read strings (text segment) */
+ n = entry->string_size = dyn2.ld_str_sz;
+ entry->strings = (char *) alloca(n);
+ entry->strings_offset = text_offset (entry) + dyn2.ld_strings;
+ lseek(desc, entry->strings_offset, L_SET);
+ if (read(desc, entry->strings, n) != n) {
+ fatal_with_file(
+ "premature eof while reading dyn strings ", entry);
+ }
+ enter_file_symbols (entry);
+ entry->strings = 0;
+
+ /* TODO: examine needed shared objects */
+ if (dyn2.ld_need) {
+ }
+}
+
+#undef major
+#undef minor
+
+int
+findlib(p)
+struct file_entry *p;
+{
+ int desc;
+ int i;
+ int len;
+ int major = -1, minor = -1;
+ char *cp, *fname = NULL;
+
+ if (p->search_dynamic_flag == 0)
+ goto dot_a;
+
+ fname = findshlib(p->filename, &major, &minor);
+
+ if (fname && (desc = open (fname, O_RDONLY, 0)) > 0) {
+ p->filename = fname;
+ p->lib_major = major;
+ p->lib_minor = minor;
+ p->search_dirs_flag = 0;
+ return desc;
+ }
+ free (fname);
+
+dot_a:
+ p->search_dynamic_flag = 0;
+ if (cp = strrchr(p->filename, '/')) {
+ *cp++ = '\0';
+ fname = concat(concat(p->filename, "/lib", cp), ".a", "");
+ *(--cp) = '/';
+ } else
+ fname = concat("lib", p->filename, ".a");
+
+ for (i = 0; i < n_search_dirs; i++) {
+ register char *string
+ = concat (search_dirs[i], "/", fname);
+ desc = open (string, O_RDONLY, 0);
+ if (desc > 0) {
+ p->filename = string;
+ p->search_dirs_flag = 0;
+ break;
+ }
+ free (string);
+ }
+ return desc;
+}
+
diff --git a/gnu/usr.bin/ld/rrs.c b/gnu/usr.bin/ld/rrs.c
new file mode 100644
index 0000000..edd2ffa
--- /dev/null
+++ b/gnu/usr.bin/ld/rrs.c
@@ -0,0 +1,1064 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: rrs.c,v 1.5 1993/11/01 16:26:18 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <ar.h>
+#include <ranlib.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+#include <strings.h>
+
+#include "ld.h"
+
+static struct link_dynamic rrs_dyn; /* defined in link.h */
+static struct ld_debug rrs_ld_debug; /* defined in link.h */
+static struct link_dynamic_2 rrs_dyn2; /* defined in link.h */
+static got_t *rrs_got;
+static jmpslot_t *rrs_plt; /* defined in md.h */
+static struct relocation_info *rrs_reloc;
+static struct nzlist *rrs_symbols; /* RRS symbol table */
+static char *rrs_strtab; /* RRS strings */
+static struct rrs_hash *rrs_hashtab; /* RT hash table */
+static struct shobj *rrs_shobjs;
+
+static int reserved_rrs_relocs;
+static int claimed_rrs_relocs;
+
+static int number_of_gotslots;
+static int number_of_jmpslots;
+static int number_of_rrs_hash_entries;
+static int number_of_rrs_symbols;
+static int rrs_strtab_size;
+static int rrs_symbol_size;
+
+static int current_jmpslot_offset;
+static int current_got_offset;
+static int current_reloc_offset;
+static int current_hash_index;
+static int number_of_shobjs;
+
+struct shobj {
+ struct shobj *next;
+ struct file_entry *entry;
+};
+
+/*
+RRS text segment:
+ +-------------------+ <-- ld_rel (rrs_text_start)
+ | |
+ | relocation |
+ | |
+ +-------------------+ <-- <link_dynamic_2>.ld_hash
+ | |
+ | hash buckets |
+ | |
+ +-------------------+ <-- <link_dynamic_2>.ld_stab
+ | |
+ | symbols |
+ | |
+ +-------------------+ <-- <link_dynamic_2>.ld_strings
+ | |
+ | strings |
+ | |
+ +-------------------+ <-- <link_dynamic_2>.ld_need
+ | |
+ | shobjs |
+ | |
+ +-------------------+
+ | |
+ | shobjs strings | <-- <shobj>.lo_name
+ | |
+ +-------------------+
+
+
+RRS data segment:
+
+ +-------------------+ <-- __DYNAMIC (rrs_data_start)
+ | |
+ | link_dymamic |
+ | |
+ +-------------------+ <-- __DYNAMIC.ldd
+ | |
+ | ld_debug |
+ | |
+ +-------------------+ <-- __DYNAMIC.ld_un.ld_2
+ | |
+ | link_dymamic_2 |
+ | |
+ +-------------------+ <-- _GLOBAL_OFFSET_TABLE_ (ld_got)
+ | |
+ | _GOT_ |
+ | |
+ +-------------------+ <-- ld_plt
+ | |
+ | PLT |
+ | |
+ +-------------------+
+*/
+
+/*
+ * Initialize RRS
+ */
+
+void
+init_rrs()
+{
+ reserved_rrs_relocs = 0;
+ claimed_rrs_relocs = 0;
+
+ number_of_rrs_symbols = 0;
+ rrs_strtab_size = 0;
+
+ /* First jmpslot reserved for run-time binder */
+ current_jmpslot_offset = sizeof(jmpslot_t);
+ number_of_jmpslots = 1;
+
+ /* First gotslot reserved for __DYNAMIC */
+ current_got_offset = sizeof(got_t);
+ number_of_gotslots = 1;
+
+ current_reloc_offset = 0;
+}
+
+/*
+ * Add NAME to the list of needed run-time objects.
+ */
+void
+rrs_add_shobj(entry)
+struct file_entry *entry;
+{
+ struct shobj **p;
+
+ for (p = &rrs_shobjs; *p != NULL; p = &(*p)->next);
+ *p = (struct shobj *)xmalloc(sizeof(struct shobj));
+ (*p)->next = NULL;
+ (*p)->entry = entry;
+
+ number_of_shobjs++;
+}
+
+void
+alloc_rrs_reloc(sp)
+symbol *sp;
+{
+#ifdef DEBUG
+printf("alloc_rrs_reloc: %s\n", sp->name);
+#endif
+ reserved_rrs_relocs++;
+}
+
+void
+alloc_rrs_segment_reloc(r)
+struct relocation_info *r;
+{
+#ifdef DEBUG
+printf("alloc_rrs_segment_reloc at %#x\n", r->r_address);
+#endif
+ reserved_rrs_relocs++;
+}
+
+void
+alloc_rrs_jmpslot(sp)
+symbol *sp;
+{
+ if (sp->jmpslot_offset == -1) {
+ sp->jmpslot_offset = current_jmpslot_offset;
+ current_jmpslot_offset += sizeof(jmpslot_t);
+ number_of_jmpslots++;
+ if (!(link_mode & SYMBOLIC) || JMPSLOT_NEEDS_RELOC) {
+ reserved_rrs_relocs++;
+ }
+ }
+}
+
+void
+alloc_rrs_gotslot(r, lsp)
+struct relocation_info *r;
+struct localsymbol *lsp;
+{
+ symbol *sp = lsp->symbol;
+
+ if (!RELOC_EXTERN_P(r)) {
+
+ if (sp != NULL)
+ fatal("internal error: lsp->symbol not NULL");
+
+ if (!RELOC_STATICS_THROUGH_GOT_P(r))
+ /* No need for a GOT slot */
+ return;
+
+ if (lsp->gotslot_offset == -1) {
+ lsp->gotslot_offset = current_got_offset;
+ current_got_offset += sizeof(got_t);
+ number_of_gotslots++;
+ /*
+ * Now, see if slot needs run-time fixing
+ * If the load address is known (entry_symbol), this
+ * slot will have its final value set by `claim_got'
+ */
+ if ((link_mode & SHAREABLE) || (link_mode & SYMBOLIC))
+ reserved_rrs_relocs++;
+ }
+
+ } else if (sp->gotslot_offset == -1) {
+
+ if (sp->alias)
+ sp = sp->alias;
+
+ /*
+ * External symbols always get a relocation entry
+ */
+ sp->gotslot_offset = current_got_offset;
+ reserved_rrs_relocs++;
+ current_got_offset += sizeof(got_t);
+ number_of_gotslots++;
+ }
+
+}
+
+void
+alloc_rrs_cpy_reloc(sp)
+symbol *sp;
+{
+ if (sp->cpyreloc_reserved)
+ return;
+#ifdef DEBUG
+printf("alloc_rrs_copy: %s\n", sp->name);
+#endif
+ sp->cpyreloc_reserved = 1;
+ reserved_rrs_relocs++;
+}
+
+static struct relocation_info *
+rrs_next_reloc()
+{
+ struct relocation_info *r;
+
+ r = rrs_reloc + claimed_rrs_relocs++;
+ if (claimed_rrs_relocs > reserved_rrs_relocs)
+ fatal("internal error: RRS relocs exceed allocation %d",
+ reserved_rrs_relocs);
+ return r;
+}
+
+/*
+ * Claim a RRS relocation as a result of a regular (ie. non-PIC)
+ * relocation record in a rel file.
+ *
+ * Return 1 if the output file needs no further updating.
+ * Return 0 if the relocation value pointed to by RELOCATION must
+ * written to a.out.
+ */
+int
+claim_rrs_reloc(rp, sp, relocation)
+struct relocation_info *rp;
+symbol *sp;
+long *relocation;
+{
+ struct relocation_info *r = rrs_next_reloc();
+
+ if (rp->r_address < text_start + text_size)
+ error("RRS text relocation at %#x (symbol %s)",
+ rp->r_address, sp->name);
+
+#ifdef DEBUG
+printf("claim_rrs_reloc: %s\n", sp->name);
+#endif
+ r->r_address = rp->r_address;
+ r->r_symbolnum = sp->rrs_symbolnum;
+
+ if (link_mode & SYMBOLIC) {
+ if (!sp->defined)
+ error("Cannot reduce symbol %s", sp->name);
+ RELOC_EXTERN_P(r) = 0;
+ *relocation += sp->value;
+ (void) md_make_reloc(rp, r, RELTYPE_RELATIVE);
+ return 0;
+ } else {
+ RELOC_EXTERN_P(r) = 1;
+ return md_make_reloc(rp, r, RELTYPE_EXTERN);
+ }
+}
+
+/*
+ * Claim a jmpslot. Setup RRS relocation if claimed for the first time.
+ */
+long
+claim_rrs_jmpslot(rp, sp, addend)
+struct relocation_info *rp;
+symbol *sp;
+long addend;
+{
+ struct relocation_info *r;
+
+ if (sp->jmpslot_claimed)
+ return rrs_dyn2.ld_plt + sp->jmpslot_offset;
+
+#ifdef DEBUG
+printf("claim_rrs_jmpslot: %s(%d) -> offset %x (textreloc %#x)\n",
+ sp->name, sp->rrs_symbolnum, sp->jmpslot_offset, text_relocation);
+#endif
+
+ if (sp->jmpslot_offset == -1)
+ fatal(
+ "internal error: claim_rrs_jmpslot: %s: jmpslot_offset == -1\n",
+ sp->name);
+
+ if ((link_mode & SYMBOLIC) || rrs_section_type == RRS_PARTIAL) {
+ if (!sp->defined)
+ error("Cannot reduce symbol %s", sp->name);
+
+ md_fix_jmpslot( rrs_plt + sp->jmpslot_offset/sizeof(jmpslot_t),
+ rrs_dyn2.ld_plt + sp->jmpslot_offset,
+ sp->value);
+ if (!JMPSLOT_NEEDS_RELOC) {
+ return rrs_dyn2.ld_plt + sp->jmpslot_offset;
+ }
+ } else {
+ md_make_jmpslot( rrs_plt + sp->jmpslot_offset/sizeof(jmpslot_t),
+ sp->jmpslot_offset,
+ claimed_rrs_relocs);
+ }
+
+ if (rrs_section_type == RRS_PARTIAL)
+ /* PLT is self-contained */
+ return rrs_dyn2.ld_plt + sp->jmpslot_offset;
+
+ /*
+ * Install a run-time relocation for this PLT entry.
+ */
+ r = rrs_next_reloc();
+ sp->jmpslot_claimed = 1;
+
+ RELOC_SYMBOL(r) = sp->rrs_symbolnum;
+
+ r->r_address = (long)rrs_dyn2.ld_plt + sp->jmpslot_offset;
+
+ if (link_mode & SYMBOLIC) {
+ RELOC_EXTERN_P(r) = 0;
+ md_make_jmpreloc(rp, r, RELTYPE_RELATIVE);
+ } else {
+ RELOC_EXTERN_P(r) = 1;
+ md_make_jmpreloc(rp, r, 0);
+ }
+
+ return rrs_dyn2.ld_plt + sp->jmpslot_offset;
+}
+
+/*
+ * Claim GOT entry for a global symbol. If this is the first relocation
+ * claiming the entry, setup a RRS relocation for it.
+ * Return offset into the GOT allocated to this symbol.
+ */
+long
+claim_rrs_gotslot(rp, lsp, addend)
+struct relocation_info *rp;
+struct localsymbol *lsp;
+long addend;
+{
+ struct relocation_info *r;
+ symbol *sp = lsp->symbol;
+ int reloc_type = 0;
+
+ if (sp->alias)
+ sp = sp->alias;
+
+#ifdef DEBUG
+printf("claim_rrs_gotslot: %s(%d) slot offset %#x, addend %#x\n",
+ sp->name, sp->rrs_symbolnum, sp->gotslot_offset, addend);
+#endif
+ if (sp->gotslot_offset == -1)
+ fatal(
+ "internal error: claim_rrs_gotslot: %s: gotslot_offset == -1\n",
+ sp->name);
+
+ if (sp->gotslot_claimed)
+ /* This symbol already passed here before. */
+ return sp->gotslot_offset;
+
+ if (sp->defined &&
+ (!(link_mode & SHAREABLE) || (link_mode & SYMBOLIC))) {
+
+ /*
+ * Reduce to just a base-relative translation.
+ */
+
+ *(got_t *)((long)rrs_got + sp->gotslot_offset) =
+ sp->value + addend;
+ reloc_type = RELTYPE_RELATIVE;
+
+ } else if ((link_mode & SYMBOLIC) || rrs_section_type == RRS_PARTIAL) {
+ /*
+ * SYMBOLIC: all symbols must be known.
+ * RRS_PARTIAL: we don't link against shared objects,
+ * so again all symbols must be known.
+ */
+ error("Cannot reduce symbol %s", sp->name);
+
+ } else {
+
+ /*
+ * This gotslot will be updated with symbol value at run-rime.
+ */
+
+ *(got_t *)((long)rrs_got + sp->gotslot_offset) = addend;
+ }
+
+ if (rrs_section_type == RRS_PARTIAL) {
+ /*
+ * Base address is known, gotslot should be fully
+ * relocated by now.
+ * NOTE: RRS_PARTIAL implies !SHAREABLE.
+ */
+ if (!sp->defined)
+ error("Cannot reduce symbol %s", sp->name);
+ return sp->gotslot_offset;
+ }
+
+ /*
+ * Claim a relocation entry.
+ * If symbol is defined and in "main" (!SHAREABLE)
+ * we still put out a relocation as we cannot easily
+ * undo the allocation.
+ * `RELTYPE_RELATIVE' relocations have the external bit off
+ * as no symbol need be looked up at run-time.
+ */
+ r = rrs_next_reloc();
+ sp->gotslot_claimed = 1;
+ r->r_address = rrs_dyn2.ld_got + sp->gotslot_offset;
+ RELOC_SYMBOL(r) = sp->rrs_symbolnum;
+ RELOC_EXTERN_P(r) = !(reloc_type == RELTYPE_RELATIVE);
+ md_make_gotreloc(rp, r, reloc_type);
+
+ return sp->gotslot_offset;
+}
+
+/*
+ * Claim a GOT entry for a static symbol. Return offset of the
+ * allocated GOT entry. If RELOC_STATICS_THROUGH_GOT_P is in effect
+ * return the offset of the symbol with respect to the *location* of
+ * the GOT.
+ */
+long
+claim_rrs_internal_gotslot(entry, rp, lsp, addend)
+struct file_entry *entry;
+struct relocation_info *rp;
+struct localsymbol *lsp;
+long addend;
+{
+ struct relocation_info *r;
+
+ addend += lsp->nzlist.nz_value;
+
+ if (!RELOC_STATICS_THROUGH_GOT_P(r))
+ return addend - rrs_dyn2.ld_got;
+
+#ifdef DEBUG
+printf("claim_rrsinternal__gotslot: slot offset %#x, addend = %#x\n",
+ lsp->gotslot_offset, addend);
+#endif
+
+ if (lsp->gotslot_offset == -1)
+ fatal(
+ "internal error: claim_rrs_internal_gotslot: slot_offset == -1\n");
+
+ if (lsp->gotslot_claimed)
+ /* Already done */
+ return lsp->gotslot_offset;
+
+ *(long *)((long)rrs_got + lsp->gotslot_offset) = addend;
+
+ if (!(link_mode & SHAREABLE))
+ return lsp->gotslot_offset;
+
+ /*
+ * Relocation entry needed for this static GOT entry.
+ */
+ r = rrs_next_reloc();
+ lsp->gotslot_claimed = 1;
+ r->r_address = rrs_dyn2.ld_got + lsp->gotslot_offset;
+ RELOC_EXTERN_P(r) = 0;
+ md_make_gotreloc(rp, r, RELTYPE_RELATIVE);
+ return lsp->gotslot_offset;
+}
+
+void
+claim_rrs_cpy_reloc(rp, sp)
+struct relocation_info *rp;
+symbol *sp;
+{
+ struct relocation_info *r;
+
+ if (sp->cpyreloc_claimed)
+ return;
+
+ if (!sp->cpyreloc_reserved)
+ fatal("internal error: claim_cpy_reloc: %s: no reservation\n",
+ sp->name);
+
+#ifdef DEBUG
+printf("claim_rrs_copy: %s -> %x\n", sp->name, sp->so_defined);
+#endif
+
+ r = rrs_next_reloc();
+ sp->cpyreloc_claimed = 1;
+ r->r_address = rp->r_address;
+ RELOC_SYMBOL(r) = sp->rrs_symbolnum;
+ RELOC_EXTERN_P(r) = RELOC_EXTERN_P(rp);
+ md_make_cpyreloc(rp, r);
+}
+
+void
+claim_rrs_segment_reloc(rp)
+struct relocation_info *rp;
+{
+ struct relocation_info *r = rrs_next_reloc();
+
+#ifdef DEBUG
+printf("claim_rrs_segment_reloc: %x\n", rp->r_address);
+#endif
+ r->r_address = rp->r_address;
+ RELOC_TYPE(r) = RELOC_TYPE(rp);
+ RELOC_EXTERN_P(r) = 0;
+ md_make_reloc(rp, r, RELTYPE_RELATIVE);
+
+}
+
+/*
+ * Fill the RRS hash table for the given symbol name.
+ * NOTE: the hash value computation must match the one in rtld.
+ */
+void
+rrs_insert_hash(cp, index)
+char *cp;
+int index;
+{
+ int hashval = 0;
+ struct rrs_hash *hp;
+
+ for (; *cp; cp++)
+ hashval = (hashval << 1) + *cp;
+
+ hashval = (hashval & 0x7fffffff) % rrs_dyn2.ld_buckets;
+
+ /* Get to the bucket */
+ hp = rrs_hashtab + hashval;
+ if (hp->rh_symbolnum == -1) {
+ /* Empty bucket, use it */
+ hp->rh_symbolnum = index;
+ hp->rh_next = 0;
+ return;
+ }
+
+ while (hp->rh_next != 0)
+ hp = rrs_hashtab + hp->rh_next;
+
+ hp->rh_next = current_hash_index++;
+ hp = rrs_hashtab + hp->rh_next;
+ hp->rh_symbolnum = index;
+ hp->rh_next = 0;
+}
+
+/*
+ * There are two interesting cases to consider here.
+ *
+ * 1) No shared objects were loaded, but there were PIC input rel files.
+ * In this case we must output a _GLOBAL_OFFSET_TABLE_ but no other
+ * RRS data. Also, the entries in the GOT must be fully resolved.
+ *
+ * 2) It's a genuine dynamically linked program, so the whole RRS scoop
+ * goes into a.out.
+ */
+void
+consider_rrs_section_lengths()
+{
+ int n;
+ struct shobj *shp;
+ int symbolsize;
+
+ /* First, determine what of the RRS we want */
+
+ if (relocatable_output)
+ rrs_section_type = RRS_NONE;
+ else if (link_mode & SHAREABLE)
+ rrs_section_type = RRS_FULL;
+ else if (number_of_shobjs == 0 /*&& !(link_mode & DYNAMIC)*/) {
+ /*
+ * First slots in both tables are reserved
+ * hence the "> 1" condition
+ */
+ if (number_of_gotslots > 1 || number_of_jmpslots > 1)
+ rrs_section_type = RRS_PARTIAL;
+ else
+ rrs_section_type = RRS_NONE;
+ } else
+ rrs_section_type = RRS_FULL;
+
+ if (rrs_section_type == RRS_NONE) {
+ got_symbol->defined = 0;
+ return;
+ }
+
+ rrs_symbol_size = LD_VERSION_NZLIST_P(soversion) ?
+ sizeof(struct nzlist) : sizeof(struct nlist);
+
+ /*
+ * If there is an entry point, __DYNAMIC must be referenced (usually
+ * from crt0), as this is the method used to determine whether the
+ * run-time linker must be called.
+ */
+ if (!(link_mode & SHAREABLE) && !dynamic_symbol->referenced)
+ fatal("No reference to __DYNAMIC");
+
+ dynamic_symbol->referenced = 1;
+
+ if (number_of_gotslots > 1)
+ got_symbol->referenced = 1;
+
+
+ /* Next, allocate relocs, got and plt */
+ n = reserved_rrs_relocs * sizeof(struct relocation_info);
+ rrs_reloc = (struct relocation_info *)xmalloc(n);
+ bzero(rrs_reloc, n);
+
+ n = number_of_gotslots * sizeof(got_t);
+ rrs_got = (got_t *)xmalloc(n);
+ bzero(rrs_got, n);
+
+ n = number_of_jmpslots * sizeof(jmpslot_t);
+ rrs_plt = (jmpslot_t *)xmalloc(n);
+ bzero(rrs_plt, n);
+
+ /* Initialize first jmpslot */
+ md_fix_jmpslot(rrs_plt, 0, 0);
+
+ if (rrs_section_type == RRS_PARTIAL) {
+ rrs_data_size = number_of_gotslots * sizeof(got_t);
+ rrs_data_size += number_of_jmpslots * sizeof(jmpslot_t);
+ return;
+ }
+
+ /*
+ * Walk the symbol table, assign RRS symbol numbers
+ * Assign number 0 to __DYNAMIC (!! Sun compatibility)
+ */
+ dynamic_symbol->rrs_symbolnum = number_of_rrs_symbols++;
+ FOR_EACH_SYMBOL(i ,sp) {
+ if (sp->referenced) {
+ rrs_strtab_size += 1 + strlen(sp->name);
+ if (sp != dynamic_symbol)
+ sp->rrs_symbolnum = number_of_rrs_symbols++;
+ }
+ } END_EACH_SYMBOL;
+
+ /*
+ * Now that we know how many RRS symbols there are going to be,
+ * allocate and initialize the RRS symbol hash table.
+ */
+ rrs_dyn2.ld_buckets = number_of_rrs_symbols/4;
+ if (rrs_dyn2.ld_buckets < 4)
+ rrs_dyn2.ld_buckets = 4;
+
+ number_of_rrs_hash_entries = rrs_dyn2.ld_buckets + number_of_rrs_symbols;
+ rrs_hashtab = (struct rrs_hash *)xmalloc(
+ number_of_rrs_hash_entries * sizeof(struct rrs_hash));
+ for (n = 0; n < rrs_dyn2.ld_buckets; n++)
+ rrs_hashtab[n].rh_symbolnum = -1;
+ current_hash_index = rrs_dyn2.ld_buckets;
+
+ /*
+ * Get symbols into hash table now, so we can fine tune the size
+ * of the latter. We adjust the value of `number_of_rrs_hash_entries'
+ * to the number of hash link slots actually used.
+ */
+ FOR_EACH_SYMBOL(i ,sp) {
+ if (sp->referenced)
+ rrs_insert_hash(sp->name, sp->rrs_symbolnum);
+ } END_EACH_SYMBOL;
+ number_of_rrs_hash_entries = current_hash_index;
+
+ /*
+ * Calculate RRS section sizes.
+ */
+ rrs_data_size = sizeof(struct link_dynamic);
+ rrs_data_size += sizeof(struct ld_debug);
+ rrs_data_size += sizeof(struct link_dynamic_2);
+ rrs_data_size += number_of_gotslots * sizeof(got_t);
+ rrs_data_size += number_of_jmpslots * sizeof(jmpslot_t);
+ rrs_data_size = MALIGN(rrs_data_size);
+
+ rrs_text_size = reserved_rrs_relocs * sizeof(struct relocation_info);
+ rrs_text_size += number_of_rrs_hash_entries * sizeof(struct rrs_hash);
+ rrs_text_size += number_of_rrs_symbols * rrs_symbol_size;
+
+ /* Align strings size */
+ rrs_strtab_size = MALIGN(rrs_strtab_size);
+ rrs_text_size += rrs_strtab_size;
+
+ /* Process needed shared objects */
+ for (shp = rrs_shobjs; shp; shp = shp->next) {
+ char *name = shp->entry->local_sym_name;
+
+ if (*name == '-' && *(name+1) == 'l')
+ name += 2;
+
+ rrs_text_size += sizeof(struct link_object);
+ rrs_text_size += 1 + strlen(name);
+ }
+
+ /* Finally, align size */
+ rrs_text_size = MALIGN(rrs_text_size);
+}
+
+void
+relocate_rrs_addresses()
+{
+
+ dynamic_symbol->value = 0;
+
+ if (rrs_section_type == RRS_NONE)
+ return;
+
+ if (rrs_section_type == RRS_PARTIAL) {
+ got_symbol->value = rrs_dyn2.ld_got = rrs_data_start;
+ rrs_dyn2.ld_plt = rrs_dyn2.ld_got +
+ number_of_gotslots * sizeof(got_t);
+ return;
+ }
+
+ /*
+ * RRS data relocations.
+ */
+ rrs_dyn.ld_version = soversion;
+ rrs_dyn.ldd = (struct ld_debug *)
+ (rrs_data_start + sizeof(struct link_dynamic));
+ rrs_dyn.ld_un.ld_2 = (struct link_dynamic_2 *)
+ ((long)rrs_dyn.ldd + sizeof(struct ld_debug));
+
+ rrs_dyn2.ld_got = (long)rrs_dyn.ld_un.ld_2 +
+ sizeof(struct link_dynamic_2);
+ rrs_dyn2.ld_plt = rrs_dyn2.ld_got + number_of_gotslots*sizeof(got_t);
+
+ /*
+ * RRS text relocations.
+ */
+ rrs_dyn2.ld_rel = rrs_text_start;
+ /*
+ * Sun BUG compatibility alert.
+ * Main program's RRS text values are relative to TXTADDR? WHY??
+ */
+#ifdef SUN_COMPAT
+ if (soversion == LD_VERSION_SUN && !(link_mode & SHAREABLE))
+ rrs_dyn2.ld_rel -= N_TXTADDR(outheader);
+#endif
+
+ rrs_dyn2.ld_hash = rrs_dyn2.ld_rel +
+ reserved_rrs_relocs * sizeof(struct relocation_info);
+ rrs_dyn2.ld_symbols = rrs_dyn2.ld_hash +
+ number_of_rrs_hash_entries * sizeof(struct rrs_hash);
+ rrs_dyn2.ld_strings = rrs_dyn2.ld_symbols +
+ number_of_rrs_symbols * rrs_symbol_size;
+ rrs_dyn2.ld_str_sz = rrs_strtab_size;
+ rrs_dyn2.ld_text_sz = text_size;
+ rrs_dyn2.ld_plt_sz = number_of_jmpslots * sizeof(jmpslot_t);
+
+ rrs_dyn2.ld_need = rrs_shobjs ? rrs_dyn2.ld_strings+rrs_strtab_size : 0;
+ rrs_dyn2.ld_stab_hash = 0;
+ rrs_dyn2.ld_rules = 0;
+
+ /*
+ * Assign addresses to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC
+ * &__DYNAMIC is also in the first GOT entry.
+ */
+ got_symbol->value = rrs_dyn2.ld_got;
+
+ *rrs_got = dynamic_symbol->value = rrs_data_start;
+
+}
+
+void
+write_rrs_data()
+{
+ long pos;
+
+ if (rrs_section_type == RRS_NONE)
+ return;
+
+ pos = rrs_data_start + (N_DATOFF(outheader) - DATA_START(outheader));
+ if (lseek(outdesc, pos, L_SET) != pos)
+ fatal("write_rrs_data: cant position in output file");
+
+ if (rrs_section_type == RRS_PARTIAL) {
+ /*
+ * Only a GOT and PLT are needed.
+ */
+ if (number_of_gotslots <= 1)
+ fatal("write_rrs_data: # gotslots <= 1");
+
+ md_swapout_got(rrs_got, number_of_gotslots);
+ mywrite(rrs_got, number_of_gotslots,
+ sizeof(got_t), outdesc);
+
+ if (number_of_jmpslots <= 1)
+ fatal("write_rrs_data: # jmpslots <= 1");
+
+ md_swapout_jmpslot(rrs_plt, number_of_jmpslots);
+ mywrite(rrs_plt, number_of_jmpslots,
+ sizeof(jmpslot_t), outdesc);
+ return;
+ }
+
+ md_swapout_link_dynamic(&rrs_dyn);
+ mywrite(&rrs_dyn, 1, sizeof(struct link_dynamic), outdesc);
+
+ md_swapout_ld_debug(&rrs_ld_debug);
+ mywrite(&rrs_ld_debug, 1, sizeof(struct ld_debug), outdesc);
+
+ md_swapout_link_dynamic_2(&rrs_dyn2);
+ mywrite(&rrs_dyn2, 1, sizeof(struct link_dynamic_2), outdesc);
+
+ md_swapout_got(rrs_got, number_of_gotslots);
+ mywrite(rrs_got, number_of_gotslots, sizeof(got_t), outdesc);
+
+ md_swapout_jmpslot(rrs_plt, number_of_jmpslots);
+ mywrite(rrs_plt, number_of_jmpslots, sizeof(jmpslot_t), outdesc);
+}
+
+void
+write_rrs_text()
+{
+ long pos;
+ int i;
+ int symsize;
+ struct nzlist *nlp;
+ int offset = 0;
+ struct shobj *shp;
+ struct link_object *lo;
+
+ if (rrs_section_type == RRS_PARTIAL)
+ return;
+
+ pos = rrs_text_start + (N_TXTOFF(outheader) - TEXT_START(outheader));
+ if (lseek(outdesc, pos, L_SET) != pos)
+ fatal("write_rrs_text: cant position in output file");
+
+ /* Write relocation records */
+ md_swapout_reloc(rrs_reloc, reserved_rrs_relocs);
+ mywrite(rrs_reloc, reserved_rrs_relocs,
+ sizeof(struct relocation_info), outdesc);
+
+ /* Write the RRS symbol hash tables */
+ md_swapout_rrs_hash(rrs_hashtab, number_of_rrs_hash_entries);
+ mywrite(rrs_hashtab, number_of_rrs_hash_entries, sizeof(struct rrs_hash), outdesc);
+
+ /*
+ * Determine size of an RRS symbol entry, allocate space
+ * to collect them in.
+ */
+ symsize = number_of_rrs_symbols * rrs_symbol_size;
+ nlp = rrs_symbols = (struct nzlist *)alloca(symsize);
+ rrs_strtab = (char *)alloca(rrs_strtab_size);
+
+#define INCR_NLP(p) ((p) = (struct nzlist *)((long)(p) + rrs_symbol_size))
+
+ /* __DYNAMIC symbol *must* be first for Sun compatibility */
+ nlp->nz_desc = nlp->nz_other = 0;
+ if (LD_VERSION_NZLIST_P(soversion))
+ nlp->nz_size = 0;
+ nlp->nz_type = dynamic_symbol->defined;
+ nlp->nz_value = dynamic_symbol->value;
+ nlp->nz_value = dynamic_symbol->value;
+ nlp->nz_strx = offset;
+ strcpy(rrs_strtab + offset, dynamic_symbol->name);
+ offset += 1 + strlen(dynamic_symbol->name);
+ INCR_NLP(nlp);
+
+ /*
+ * Now, for each global symbol, construct a nzlist element
+ * for inclusion in the RRS symbol table.
+ */
+ FOR_EACH_SYMBOL(i, sp) {
+
+ if (!sp->referenced || sp == dynamic_symbol)
+ continue;
+
+ if ((long)nlp - (long)rrs_symbols >=
+ number_of_rrs_symbols * rrs_symbol_size)
+ fatal(
+ "internal error: rrs symbols exceed allocation %d ",
+ number_of_rrs_symbols);
+
+ nlp->nz_desc = 0;
+ nlp->nz_other = 0;
+ if (LD_VERSION_NZLIST_P(soversion))
+ nlp->nz_size = 0;
+
+ if (sp->defined > 1) {
+ /* defined with known type */
+ if (sp->defined == N_SIZE) {
+ /*
+ * Make sure this symbol isn't going
+ * to define anything.
+ */
+ nlp->nz_type = N_UNDF;
+ nlp->nz_value = 0;
+ } else {
+ nlp->nz_type = sp->defined;
+ nlp->nz_value = sp->value;
+ }
+ if (LD_VERSION_NZLIST_P(soversion))
+ nlp->nz_size = sp->size;
+ } else if (sp->max_common_size) {
+ /*
+ * a common definition
+ */
+ nlp->nz_type = N_UNDF | N_EXT;
+ nlp->nz_value = sp->max_common_size;
+ } else if (!sp->defined) {
+ /* undefined */
+ nlp->nz_type = N_UNDF | N_EXT;
+ nlp->nz_value = 0;
+ } else
+ fatal(
+ "internal error: %s defined in mysterious way",
+ sp->name);
+
+ nlp->nz_strx = offset;
+ strcpy(rrs_strtab + offset, sp->name);
+ offset += 1 + strlen(sp->name);
+
+ INCR_NLP(nlp);
+
+ } END_EACH_SYMBOL;
+
+ if (MALIGN(offset) != rrs_strtab_size)
+ fatal(
+ "internal error: inconsistent RRS string table length: %d",
+ offset);
+
+ /* Write the symbol table */
+ if (rrs_symbol_size == sizeof(struct nlist))
+ md_swapout_symbols(rrs_symbols, number_of_rrs_symbols);
+ else
+ md_swapout_zsymbols(rrs_symbols, number_of_rrs_symbols);
+ mywrite(rrs_symbols, symsize, 1, outdesc);
+
+ /* Write the strings */
+ mywrite(rrs_strtab, rrs_strtab_size, 1, outdesc);
+
+ /*
+ * Write the names of the shared objects needed at run-time
+ */
+ pos = rrs_dyn2.ld_need + number_of_shobjs * sizeof(struct link_object);
+ lo = (struct link_object *)alloca(
+ number_of_shobjs * sizeof(struct link_object));
+
+ for (i = 0, shp = rrs_shobjs; shp; i++, shp = shp->next) {
+ char *name = shp->entry->local_sym_name;
+
+ if (shp == NULL)
+ fatal("internal error: shp == NULL");
+
+ lo[i].lo_name = pos;
+ lo[i].lo_major = shp->entry->lib_major;
+ lo[i].lo_minor = shp->entry->lib_minor;
+
+ if (*name == '-' && *(name+1) == 'l') {
+ name += 2;
+ lo[i].lo_library = 1;
+ } else
+ lo[i].lo_library = 0;
+
+ pos += 1 + strlen(name);
+ lo[i].lo_next = (i == number_of_shobjs - 1) ? 0 :
+ (rrs_dyn2.ld_need + (i+1)*sizeof(struct link_object));
+
+ }
+ if (shp != NULL)
+ fatal("internal error: shp != NULL");
+
+ md_swapout_link_object(lo, number_of_shobjs);
+ mywrite(lo, number_of_shobjs, sizeof(struct link_object), outdesc);
+
+ for (i = 0, shp = rrs_shobjs; shp; i++, shp = shp->next) {
+ char *name = shp->entry->local_sym_name;
+
+ if (*name == '-' && *(name+1) == 'l') {
+ name += 2;
+ }
+
+ mywrite(name, strlen(name) + 1, 1, outdesc);
+ }
+}
+
+void
+write_rrs()
+{
+
+ /*
+ * First, do some consistency checks on the RRS segment.
+ */
+ if (rrs_section_type == RRS_NONE) {
+ if (reserved_rrs_relocs > 1)
+ fatal(
+ "internal error: RRS relocs in static program: %d",
+ reserved_rrs_relocs-1);
+ return;
+ }
+
+#ifdef DEBUG
+printf("rrs_relocs %d, gotslots %d, jmpslots %d\n",
+ reserved_rrs_relocs, number_of_gotslots-1, number_of_jmpslots-1);
+#endif
+
+ if (claimed_rrs_relocs != reserved_rrs_relocs) {
+/*
+ fatal("internal error: reserved relocs(%d) != claimed(%d)",
+ reserved_rrs_relocs, claimed_rrs_relocs);
+*/
+ printf("FIX:internal error: reserved relocs(%d) != claimed(%d)\n",
+ reserved_rrs_relocs, claimed_rrs_relocs);
+ }
+
+ /* Write the RRS segments. */
+ write_rrs_text ();
+ write_rrs_data ();
+}
diff --git a/gnu/usr.bin/ld/rtld/Makefile b/gnu/usr.bin/ld/rtld/Makefile
new file mode 100644
index 0000000..ea02608
--- /dev/null
+++ b/gnu/usr.bin/ld/rtld/Makefile
@@ -0,0 +1,24 @@
+# $Id: Makefile,v 1.2 1993/10/27 00:55:24 pk Exp $
+
+PROG= ld.so
+SRCS= mdprologue.S rtld.c shlib.c etc.c md.c
+NOMAN= noman
+LDDIR?= $(.CURDIR)/..
+#PICFLAG=-pic
+PICFLAG=-fpic
+CFLAGS += -I$(LDDIR) -I$(.CURDIR) -I$(LDDIR)/$(MACHINE) -O $(PICFLAG) -DRTLD
+LDFLAGS = -Bshareable -Bsymbolic -assert nosymbolic
+LIBS = -lc_pic
+BINDIR= /usr/libexec
+
+.PATH: $(LDDIR) $(LDDIR)/$(MACHINE)
+
+.SUFFIXES: .S
+
+$(PROG):
+ $(LD) -o $(PROG) $(LDFLAGS) $(OBJS) $(LIBS)
+
+.S.o:
+ $(CPP) $(.IMPSRC) | $(AS) -k -o $(.TARGET) -
+
+.include <bsd.prog.mk>
diff --git a/gnu/usr.bin/ld/rtld/md-prologue.c b/gnu/usr.bin/ld/rtld/md-prologue.c
new file mode 100644
index 0000000..dae455e
--- /dev/null
+++ b/gnu/usr.bin/ld/rtld/md-prologue.c
@@ -0,0 +1,39 @@
+/*
+ * rtld entry pseudo code - turn into assembler and tweak it
+ */
+
+#include <sys/types.h>
+#include <sys/types.h>
+#include <a.out.h>
+#include "link.h"
+#include "md.h"
+
+extern long _GOT_[];
+extern void (*rtld)();
+extern void (*binder())();
+
+void
+rtld_entry(version, crtp)
+int version;
+struct crt *crtp;
+{
+ register struct link_dynamic *dp;
+ register void (*f)();
+
+ /* __DYNAMIC is first entry in GOT */
+ dp = (struct link_dynamic *) (_GOT_[0]+crtp->crt_ba);
+
+ f = (void (*)())((long)rtld + crtp->crt_ba);
+ (*f)(version, crtp, dp);
+}
+
+void
+binder_entry()
+{
+ extern int PC;
+ struct jmpslot *sp;
+ void (*func)();
+
+ func = binder(PC, sp->reloc_index & 0x003fffff);
+ (*func)();
+}
diff --git a/gnu/usr.bin/ld/rtld/rtld.c b/gnu/usr.bin/ld/rtld/rtld.c
new file mode 100644
index 0000000..433820a
--- /dev/null
+++ b/gnu/usr.bin/ld/rtld/rtld.c
@@ -0,0 +1,1026 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: rtld.c,v 1.7 1993/11/03 21:35:54 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/mman.h>
+#ifndef BSD
+#define MAP_COPY MAP_PRIVATE
+#define MAP_FILE 0
+#define MAP_ANON 0
+#endif
+#include <fcntl.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+#include "ld.h"
+
+#ifndef BSD /* Need do better than this */
+#define NEED_DEV_ZERO 1
+#endif
+
+/*
+ * Loader private data, hung off link_map->lm_lpd
+ */
+struct lm_private {
+ int lpd_version;
+ struct link_map *lpd_parent;
+#ifdef SUN_COMPAT
+ long lpd_offset; /* Correction for Sun main programs */
+#endif
+};
+
+#ifdef SUN_COMPAT
+#define LM_OFFSET(lmp) (((struct lm_private *)((lmp)->lm_lpd))->lpd_offset)
+#else
+#define LM_OFFSET(lmp) (0)
+#endif
+
+/* Base address for link_dynamic_2 entries */
+#define LM_LDBASE(lmp) (lmp->lm_addr + LM_OFFSET(lmp))
+
+/* Start of text segment */
+#define LM_TXTADDR(lmp) (lmp->lm_addr == (caddr_t)0 ? PAGSIZ : 0)
+
+/* Start of run-time relocation_info */
+#define LM_REL(lmp) ((struct relocation_info *) \
+ (lmp->lm_addr + LM_OFFSET(lmp) + LD_REL((lmp)->lm_ld)))
+
+/* Start of symbols */
+#define LM_SYMBOL(lmp, i) ((struct nzlist *) \
+ (lmp->lm_addr + LM_OFFSET(lmp) + LD_SYMBOL((lmp)->lm_ld) + \
+ i * (LD_VERSION_NZLIST_P(lmp->lm_ld->ld_version) ? \
+ sizeof(struct nzlist) : sizeof(struct nlist))))
+
+/* Start of hash table */
+#define LM_HASH(lmp) ((struct rrs_hash *) \
+ (lmp->lm_addr + LM_OFFSET(lmp) + LD_HASH((lmp)->lm_ld)))
+
+/* Start of strings */
+#define LM_STRINGS(lmp) ((char *) \
+ (lmp->lm_addr + LM_OFFSET(lmp) + LD_STRINGS((lmp)->lm_ld)))
+
+/* End of text */
+#define LM_ETEXT(lmp) ((char *) \
+ (lmp->lm_addr + LM_TXTADDR(lmp) + LD_TEXTSZ((lmp)->lm_ld)))
+
+/* PLT is in data segment, so don't use LM_OFFSET here */
+#define LM_PLT(lmp) ((jmpslot_t *) \
+ (lmp->lm_addr + LD_PLT((lmp)->lm_ld)))
+
+/* Parent of link map */
+#define LM_PARENT(lmp) (((struct lm_private *)((lmp)->lm_lpd))->lpd_parent)
+
+char **environ;
+int errno;
+uid_t uid, euid;
+gid_t gid, egid;
+int careful;
+
+struct link_map *link_map_head, *main_map;
+struct link_map **link_map_tail = &link_map_head;
+struct rt_symbol *rt_symbol_head;
+
+static int dlopen(), dlclose(), dlsym();
+
+static struct ld_entry ld_entry = {
+ dlopen, dlclose, dlsym
+};
+
+static void xprintf __P((char *, ...));
+static void init_brk __P((void));
+static void load_maps __P((struct crt_ldso *));
+static void map_object __P((struct link_object *, struct link_map *));
+static void alloc_link_map __P(( char *, struct link_object *,
+ struct link_map *, caddr_t,
+ struct link_dynamic *));
+static void check_text_reloc __P(( struct relocation_info *,
+ struct link_map *,
+ caddr_t));
+static void reloc_maps __P((void));
+static void reloc_copy __P((void));
+static char *rtfindlib __P((char *, int, int, int *));
+void binder_entry __P((void));
+long binder __P((jmpslot_t *));
+static struct nzlist *lookup __P((char *, struct link_map **));
+static struct rt_symbol *lookup_rts __P((char *));
+static struct rt_symbol *enter_rts __P((char *, long, int, caddr_t, long));
+
+#include "md-static-funcs.c"
+
+/*
+ * Called from assembler stub that has set up crtp (passed from crt0)
+ * and dp (our __DYNAMIC).
+ */
+void
+rtld(version, crtp, dp)
+int version;
+struct crt_ldso *crtp;
+struct link_dynamic *dp;
+{
+ int n;
+ int nreloc; /* # of ld.so relocations */
+ struct relocation_info *reloc;
+ char **envp;
+
+ /* Check version */
+ if (version != CRT_VERSION_BSD && version != CRT_VERSION_SUN)
+ return;
+
+ /* Fixup __DYNAMIC structure */
+ (long)dp->ld_un.ld_2 += crtp->crt_ba;
+
+ /* Be careful not to use .div routine from library */
+ for ( nreloc = 0, n = LD_RELSZ(dp);
+ n > 0;
+ n -= sizeof(struct relocation_info) ) nreloc++;
+
+
+ /* Relocate ourselves */
+ for ( reloc = (struct relocation_info *)
+ (dp->ld_un.ld_2->ld_rel + crtp->crt_ba);
+ nreloc;
+ nreloc--, reloc++) {
+
+ register long addr = reloc->r_address + crtp->crt_ba;
+
+ md_relocate_simple(reloc, crtp->crt_ba, addr);
+ }
+
+ progname = "ld.so";
+
+ /* Setup out (private) environ variable */
+ environ = crtp->crt_ep;
+
+ /* Get user and group identifiers */
+ uid = getuid(); euid = geteuid();
+ gid = getgid(); egid = getegid();
+
+ careful = (uid != euid) || (gid != egid);
+
+ if (careful) {
+ unsetenv("LD_LIBRARY_PATH");
+ unsetenv("LD_PRELOAD");
+ unsetenv("LD_RUN_PATH"); /* In case we ever implement this */
+ }
+
+ /* Setup directory search */
+ std_search_dirs(getenv("LD_LIBRARY_PATH"));
+
+ /* Load required objects into the process address space */
+ load_maps(crtp);
+
+ /* Relocate all loaded objects according to their RRS segments */
+ reloc_maps();
+ reloc_copy();
+
+ /* Fill in some field in main's __DYNAMIC structure */
+ crtp->crt_dp->ld_entry = &ld_entry;
+ crtp->crt_dp->ldd->ldd_cp = rt_symbol_head;
+}
+
+
+static void
+load_maps(crtp)
+struct crt_ldso *crtp;
+{
+ struct link_map *lmp;
+ int tracing = (int)getenv("LD_TRACE_LOADED_OBJECTS");
+
+ /* Handle LD_PRELOAD's here */
+
+ /* Make an entry for the main program */
+ alloc_link_map("main", (struct link_object *)0, (struct link_map *)0,
+ (caddr_t)0, crtp->crt_dp);
+
+ for (lmp = link_map_head; lmp; lmp = lmp->lm_next) {
+ struct link_object *lop;
+ long next = 0;
+
+ if (lmp->lm_ld)
+ next = LD_NEED(lmp->lm_ld);
+
+ while (next) {
+ lop = (struct link_object *) (LM_LDBASE(lmp) + next);
+ map_object(lop, lmp);
+ next = lop->lo_next;
+ }
+ }
+
+ if (! tracing)
+ return;
+
+ for (lmp = link_map_head; lmp; lmp = lmp->lm_next) {
+ struct link_object *lop;
+ char *name, *path;
+
+ if ((lop = lmp->lm_lop) == NULL)
+ continue;
+
+ name = lop->lo_name + LM_LDBASE(LM_PARENT(lmp));
+
+ if ((path = lmp->lm_name) == NULL)
+ path = "not found";
+
+ if (lop->lo_library)
+ printf("\t-l%s.%d => %s\n", name, lop->lo_major, path);
+ else
+ printf("\t%s => %s\n", name, path);
+ }
+
+ _exit(0);
+}
+
+/*
+ * Allocate a new link map for an shared object NAME loaded at ADDR as a
+ * result of the presence of link object LOP in the link map PARENT.
+ */
+static void
+alloc_link_map(name, lop, parent, addr, dp)
+char *name;
+struct link_map *parent;
+struct link_object *lop;
+caddr_t addr;
+struct link_dynamic *dp;
+{
+ struct link_map *lmp;
+ struct lm_private *lmpp;
+
+ lmpp = (struct lm_private *)xmalloc(sizeof(struct lm_private));
+ lmp = (struct link_map *)xmalloc(sizeof(struct link_map));
+ lmp->lm_next = NULL;
+ *link_map_tail = lmp;
+ link_map_tail = &lmp->lm_next;
+
+ lmp->lm_addr = addr;
+ lmp->lm_name = name;
+ lmp->lm_lop = lop;
+ lmp->lm_ld = dp;
+ lmp->lm_lpd = (caddr_t)lmpp;
+
+/*XXX*/ if (addr == 0) main_map = lmp;
+
+ lmpp->lpd_parent = parent;
+
+#ifdef SUN_COMPAT
+ lmpp->lpd_offset =
+ (addr == 0 && dp->ld_version == LD_VERSION_SUN) ? PAGSIZ : 0;
+#endif
+}
+
+/*
+ * Map object identified by link object LOP which was found
+ * in link map LMP.
+ */
+static void
+map_object(lop, lmp)
+struct link_object *lop;
+struct link_map *lmp;
+{
+ struct link_dynamic *dp;
+ char *path, *name = (char *)(lop->lo_name + LM_LDBASE(lmp));
+ int fd;
+ caddr_t addr;
+ struct exec hdr;
+ int usehints = 0;
+
+ if (lop->lo_library) {
+ usehints = 1;
+again:
+ path = rtfindlib(name, lop->lo_major, lop->lo_minor, &usehints);
+ if (path == NULL)
+ fatal("Cannot find lib%s.so.%d.%d\n",
+ name, lop->lo_major, lop->lo_minor);
+ } else {
+ path = name;
+ }
+
+ fd = open(path, O_RDONLY, 0);
+ if (fd == -1) {
+ if (usehints) {
+ usehints = 0;
+ goto again;
+ }
+ fatal("%s not found", path);
+ }
+
+ if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+ fatal("%s: Cannot read exec header", path);
+ }
+
+ if (N_BADMAG(hdr))
+ fatal("%s: Incorrect format", path);
+
+ if ((addr = mmap(0, hdr.a_text + hdr.a_data,
+ PROT_READ|PROT_EXEC,
+ MAP_FILE|MAP_COPY, fd, 0)) == (caddr_t)-1)
+ fatal("Cannot map %s text\n", path);
+
+ if (mmap(addr + hdr.a_text, hdr.a_data,
+ PROT_READ|PROT_WRITE|PROT_EXEC,
+ MAP_FILE|MAP_FIXED|MAP_COPY,
+ fd, hdr.a_text) == (caddr_t)-1)
+ fatal("Cannot map %s data", path);
+
+ close(fd);
+
+ fd = -1;
+#ifdef NEED_DEV_ZERO
+ if ((fd = open("/dev/zero", O_RDWR, 0)) == -1)
+ perror("/dev/zero");
+#endif
+ if (hdr.a_bss && mmap(addr + hdr.a_text + hdr.a_data, hdr.a_bss,
+ PROT_READ|PROT_WRITE|PROT_EXEC,
+ MAP_ANON|MAP_FIXED|MAP_COPY,
+ fd, hdr.a_text + hdr.a_data) == (caddr_t)-1)
+ fatal("Cannot map %s bss", path);
+
+#ifdef NEED_DEV_ZERO
+ close(fd);
+#endif
+
+ /* Assume _DYNAMIC is the first data item */
+ dp = (struct link_dynamic *)(addr+hdr.a_text);
+
+ /* Fixup __DYNAMIC structure */
+ (long)dp->ld_un.ld_2 += (long)addr;
+
+ alloc_link_map(path, lop, lmp, addr, dp);
+
+}
+
+static void
+reloc_maps()
+{
+ struct link_map *lmp;
+
+ for (lmp = link_map_head; lmp; lmp = lmp->lm_next) {
+
+ struct link_dynamic *dp = lmp->lm_ld;
+ struct relocation_info *r = LM_REL(lmp);
+ struct relocation_info *rend = r + LD_RELSZ(dp)/sizeof(*r);
+
+ if (LD_PLTSZ(dp))
+ md_fix_jmpslot(LM_PLT(lmp),
+ (long)LM_PLT(lmp), (long)binder_entry);
+
+ for (; r < rend; r++) {
+ char *sym;
+ caddr_t addr = lmp->lm_addr + r->r_address;
+
+ check_text_reloc(r, lmp, addr);
+
+ if (RELOC_EXTERN_P(r)) {
+ struct link_map *src_map;
+ struct nzlist *np;
+ long relocation = md_get_addend(r, addr);
+
+ if (RELOC_LAZY_P(r))
+ continue;
+
+ sym = LM_STRINGS(lmp) +
+ LM_SYMBOL(lmp,RELOC_SYMBOL(r))->nz_strx;
+
+ np = lookup(sym, &src_map);
+ if (np == NULL)
+ fatal("Undefined symbol in %s: %s\n",
+ lmp->lm_name, sym);
+
+ /*
+ * Found symbol definition.
+ * If it's in a link map, adjust value
+ * according to the load address of that map.
+ * Otherwise it's a run-time allocated common
+ * whose value is already up-to-date.
+ */
+ relocation += np->nz_value;
+ if (src_map)
+ relocation += (long)src_map->lm_addr;
+
+ if (RELOC_PCREL_P(r))
+ relocation -= (long)lmp->lm_addr;
+
+ if (RELOC_COPY_P(r) && src_map) {
+#if DEBUG
+xprintf("RELOCATE(%s) copy: from %s at %#x(%#x+%#x) to %s at %#x, reloc = %#x, size %d\n",
+lmp->lm_name, src_map->lm_name, src_map->lm_addr + np->nz_value,
+src_map->lm_addr, np->nz_value, sym, addr, relocation, np->nz_size);
+#endif
+ (void)enter_rts(sym,
+ (long)addr,
+ N_DATA + N_EXT,
+ src_map->lm_addr + np->nz_value,
+ np->nz_size);
+ continue;
+ }
+#if DEBUG
+xprintf("RELOCATE(%s) external: %s at %#x, reloc = %#x\n", lmp->lm_name, sym, addr, relocation);
+#endif
+ md_relocate(r, relocation, addr, 0);
+
+ } else {
+#if DEBUG
+xprintf("RELOCATE(%s) internal at %#x, reloc = %#x\n", lmp->lm_name, addr, md_get_rt_segment_addend(r,addr));
+#endif
+ md_relocate(r,
+#ifdef SUN_COMPAT
+ md_get_rt_segment_addend(r, addr)
+#else
+ md_get_addend(r, addr)
+#endif
+ + (long)lmp->lm_addr, addr, 0);
+ }
+
+ }
+
+ }
+}
+
+static void
+reloc_copy()
+{
+ struct rt_symbol *rtsp;
+
+ for (rtsp = rt_symbol_head; rtsp; rtsp = rtsp->rt_next)
+ if (rtsp->rt_sp->nz_type == N_DATA + N_EXT) {
+#ifdef DEBUG
+xprintf("reloc_copy: from %#x to %#x, size %d\n",
+rtsp->rt_srcaddr, rtsp->rt_sp->nz_value, rtsp->rt_sp->nz_size);
+#endif
+ bcopy(rtsp->rt_srcaddr, (caddr_t)rtsp->rt_sp->nz_value,
+ rtsp->rt_sp->nz_size);
+ }
+}
+
+static void
+check_text_reloc(r, lmp, addr)
+struct relocation_info *r;
+struct link_map *lmp;
+caddr_t addr;
+{
+ char *sym;
+
+ if (addr >= LM_ETEXT(lmp))
+ return;
+
+ if (RELOC_EXTERN_P(r))
+ sym = LM_STRINGS(lmp) +
+ LM_SYMBOL(lmp, RELOC_SYMBOL(r))->nz_strx;
+ else
+ sym = "";
+
+#ifdef DEBUG
+ fprintf(stderr, "ld.so: warning: non pure code in %s at %x (%s)\n",
+ lmp->lm_name, r->r_address, sym);
+#endif
+
+ if (mprotect( lmp->lm_addr + LM_TXTADDR(lmp),
+ LD_TEXTSZ(lmp->lm_ld),
+ PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
+
+ perror("mprotect"),
+ fatal("Cannot enable writes to %s\n", lmp->lm_name);
+ }
+
+ lmp->lm_rwt = 1;
+}
+
+static struct nzlist *
+lookup(name, src_map)
+char *name;
+struct link_map **src_map;
+{
+ long common_size = 0;
+ struct link_map *lmp;
+ struct rt_symbol *rtsp;
+
+ *src_map = NULL;
+
+ if ((rtsp = lookup_rts(name)) != NULL)
+ return rtsp->rt_sp;
+
+ /*
+ * Search all maps for a definition of NAME
+ */
+ for (lmp = link_map_head; lmp; lmp = lmp->lm_next) {
+ int buckets = LD_BUCKETS(lmp->lm_ld);
+ long hashval = 0;
+ struct rrs_hash *hp;
+ char *cp;
+ struct nzlist *np;
+
+ /*
+ * Compute bucket in which the symbol might be found.
+ */
+ for (cp = name; *cp; cp++)
+ hashval = (hashval << 1) + *cp;
+
+ hashval = (hashval & 0x7fffffff) % buckets;
+
+ hp = LM_HASH(lmp) + hashval;
+ if (hp->rh_symbolnum == -1)
+ /* Nothing in this bucket */
+ continue;
+
+ while (hp) {
+ np = LM_SYMBOL(lmp, hp->rh_symbolnum);
+ cp = LM_STRINGS(lmp) + np->nz_strx;
+ if (strcmp(cp, name) == 0)
+ break;
+ if (hp->rh_next == 0)
+ hp = NULL;
+ else
+ hp = LM_HASH(lmp) + hp->rh_next;
+ }
+ if (hp == NULL)
+ /* Nothing in this bucket */
+ continue;
+
+ /*
+ * We have a symbol with the name we're looking for.
+ */
+
+ if (np->nz_value == 0)
+ /* It's not a definition */
+ continue;
+
+ if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
+ /* It's a common, note value and continue search */
+ if (common_size < np->nz_value)
+ common_size = np->nz_value;
+ continue;
+ }
+
+ *src_map = lmp;
+ return np;
+ }
+
+ if (common_size == 0)
+ /* Not found */
+ return NULL;
+
+ /*
+ * It's a common, enter into run-time common symbol table.
+ */
+ rtsp = enter_rts(name, (long)calloc(1, common_size),
+ N_UNDF + N_EXT, 0, common_size);
+
+#if DEBUG
+xprintf("Allocating common: %s size %d at %#x\n", name, common_size, rtsp->rt_sp->nz_value);
+#endif
+
+ return rtsp->rt_sp;
+}
+
+
+/*
+ * This routine is called from the jumptable to resolve
+ * procedure calls to shared objects.
+ */
+long
+binder(jsp)
+jmpslot_t *jsp;
+{
+ struct link_map *lmp, *src_map;
+ long addr;
+ char *sym;
+ struct nzlist *np;
+ int index;
+
+ /*
+ * Find the PLT map that contains JSP.
+ */
+ for (lmp = link_map_head; lmp; lmp = lmp->lm_next) {
+ if (LM_PLT(lmp) < jsp &&
+ jsp < LM_PLT(lmp) + LD_PLTSZ(lmp->lm_ld)/sizeof(*jsp))
+ break;
+ }
+
+ if (lmp == NULL)
+ fatal("Call to binder from unknown location: %#x\n", jsp);
+
+ index = jsp->reloc_index & JMPSLOT_RELOC_MASK;
+
+ /* Get the local symbol this jmpslot refers to */
+ sym = LM_STRINGS(lmp) +
+ LM_SYMBOL(lmp,RELOC_SYMBOL(&LM_REL(lmp)[index]))->nz_strx;
+
+ np = lookup(sym, &src_map);
+ if (np == NULL)
+ fatal("Undefined symbol \"%s\" called from %s at %#x", sym,
+ lmp->lm_name, jsp);
+
+ /* Fixup jmpslot so future calls transfer directly to target */
+ addr = np->nz_value;
+ if (src_map)
+ addr += (long)src_map->lm_addr;
+
+ md_fix_jmpslot(jsp, (long)jsp, addr);
+
+#if DEBUG
+xprintf(" BINDER: %s located at = %#x in %s\n", sym, addr, src_map->lm_name);
+#endif
+ return addr;
+}
+
+
+/*
+ * Run-time common symbol table.
+ */
+
+#define RTC_TABSIZE 57
+static struct rt_symbol *rt_symtab[RTC_TABSIZE];
+
+/*
+ * Compute hash value for run-time symbol table
+ */
+static int
+hash_string(key)
+ char *key;
+{
+ register char *cp;
+ register int k;
+
+ cp = key;
+ k = 0;
+ while (*cp)
+ k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
+
+ return k;
+}
+
+/*
+ * Lookup KEY in the run-time common symbol table.
+ */
+
+static struct rt_symbol *
+lookup_rts(key)
+ char *key;
+{
+ register int hashval;
+ register struct rt_symbol *rtsp;
+
+ /* Determine which bucket. */
+
+ hashval = hash_string(key) % RTC_TABSIZE;
+
+ /* Search the bucket. */
+
+ for (rtsp = rt_symtab[hashval]; rtsp; rtsp = rtsp->rt_link)
+ if (strcmp(key, rtsp->rt_sp->nz_name) == 0)
+ return rtsp;
+
+ return NULL;
+}
+
+static struct rt_symbol *
+enter_rts(name, value, type, srcaddr, size)
+ char *name;
+ long value;
+ int type;
+ caddr_t srcaddr;
+ long size;
+{
+ register int hashval;
+ register struct rt_symbol *rtsp, **rpp;
+
+ /* Determine which bucket */
+ hashval = hash_string(name) % RTC_TABSIZE;
+
+ /* Find end of bucket */
+ for (rpp = &rt_symtab[hashval]; *rpp; rpp = &(*rpp)->rt_link)
+ ;
+
+ /* Allocate new common symbol */
+ rtsp = (struct rt_symbol *)malloc(sizeof(struct rt_symbol));
+ rtsp->rt_sp = (struct nzlist *)malloc(sizeof(struct nzlist));
+ rtsp->rt_sp->nz_name = strdup(name);
+ rtsp->rt_sp->nz_value = value;
+ rtsp->rt_sp->nz_type = type;
+ rtsp->rt_sp->nz_size = size;
+ rtsp->rt_srcaddr = srcaddr;
+ rtsp->rt_link = NULL;
+
+ /* Link onto linear list as well */
+ rtsp->rt_next = rt_symbol_head;
+ rt_symbol_head = rtsp;
+
+ *rpp = rtsp;
+
+ return rtsp;
+}
+
+static struct hints_header *hheader;
+static struct hints_bucket *hbuckets;
+static char *hstrtab;
+
+#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1)
+
+static void
+maphints()
+{
+ caddr_t addr;
+ long msize;
+ int fd;
+
+ if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
+ hheader = (struct hints_header *)-1;
+ return;
+ }
+
+ msize = PAGSIZ;
+ addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
+
+ if (addr == (caddr_t)-1) {
+ hheader = (struct hints_header *)-1;
+ return;
+ }
+
+ hheader = (struct hints_header *)addr;
+ if (HH_BADMAG(*hheader)) {
+ munmap(addr, msize);
+ hheader = (struct hints_header *)-1;
+ return;
+ }
+
+ if (hheader->hh_version != LD_HINTS_VERSION_1) {
+ munmap(addr, msize);
+ hheader = (struct hints_header *)-1;
+ return;
+ }
+
+ if (hheader->hh_ehints > msize) {
+ if (mmap(addr+msize, hheader->hh_ehints - msize,
+ PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
+ fd, msize) != (caddr_t)(addr+msize)) {
+
+ munmap((caddr_t)hheader, msize);
+ hheader = (struct hints_header *)-1;
+ return;
+ }
+ }
+ close(fd);
+
+ hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
+ hstrtab = (char *)(addr + hheader->hh_strtab);
+}
+
+int
+hinthash(cp, vmajor, vminor)
+char *cp;
+int vmajor, vminor;
+{
+ int k = 0;
+
+ while (*cp)
+ k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
+
+ k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
+ k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
+
+ return k;
+}
+
+#undef major
+#undef minor
+
+static char *
+findhint(name, major, minor, preferred_path)
+char *name;
+int major, minor;
+char *preferred_path;
+{
+ struct hints_bucket *bp;
+
+ bp = hbuckets + (hinthash(name, major, minor) % hheader->hh_nbucket);
+
+ while (1) {
+ /* Sanity check */
+ if (bp->hi_namex >= hheader->hh_strtab_sz) {
+ fprintf(stderr, "Bad name index: %#x\n", bp->hi_namex);
+ break;
+ }
+ if (bp->hi_pathx >= hheader->hh_strtab_sz) {
+ fprintf(stderr, "Bad path index: %#x\n", bp->hi_pathx);
+ break;
+ }
+
+ if (strcmp(name, hstrtab + bp->hi_namex) == 0) {
+ /* It's `name', check version numbers */
+ if (bp->hi_major == major &&
+ (bp->hi_ndewey < 2 || bp->hi_minor == minor)) {
+ if (preferred_path == NULL ||
+ strcmp(preferred_path,
+ hstrtab + bp->hi_pathx) == 0) {
+ return hstrtab + bp->hi_pathx;
+ }
+ }
+ }
+
+ if (bp->hi_next == -1)
+ break;
+
+ /* Move on to next in bucket */
+ bp = &hbuckets[bp->hi_next];
+ }
+
+ /* No hints available for name */
+ return NULL;
+}
+
+static char *
+rtfindlib(name, major, minor, usehints)
+char *name;
+int major, minor;
+int *usehints;
+{
+ char *hint;
+ char *cp, *ld_path = getenv("LD_LIBRARY_PATH");
+
+ if (hheader == NULL)
+ maphints();
+
+ if (!HINTS_VALID || !(*usehints)) {
+ *usehints = 0;
+ return (char *)findshlib(name, &major, &minor);
+ }
+
+ if (ld_path != NULL) {
+ /* Prefer paths from LD_LIBRARY_PATH */
+ while ((cp = strtok(ld_path, ":")) != NULL) {
+
+ ld_path = NULL;
+ hint = findhint(name, major, minor, cp);
+ if (hint)
+ return hint;
+ }
+ } else {
+ /* No LD_LIBRARY_PATH, check default */
+ hint = findhint(name, major, minor, NULL);
+ if (hint)
+ return hint;
+ }
+
+ /* No hints available for name */
+ *usehints = 0;
+ return (char *)findshlib(name, &major, &minor);
+}
+
+static int
+dlopen(name, mode)
+char *name;
+int mode;
+{
+ xprintf("dlopen(%s, %x)\n", name, mode);
+ return -1;
+}
+
+static int
+dlclose(fd)
+int fd;
+{
+ xprintf("dlclose(%d)\n", fd);
+ return -1;
+}
+
+static int
+dlsym(fd, sym)
+int fd;
+char *sym;
+{
+ xprintf("dlsym(%d, %s)\n", fd, sym);
+ return 0;
+}
+
+/*
+ * Private heap functions.
+ */
+
+static caddr_t curbrk;
+
+static void
+init_brk()
+{
+ struct rlimit rlim;
+ char *cp, **cpp = environ;
+
+ if (getrlimit(RLIMIT_STACK, &rlim) < 0) {
+ xprintf("ld.so: brk: getrlimit failure\n");
+ _exit(1);
+ }
+
+ /*
+ * Walk to the top of stack
+ */
+ if (*cpp) {
+ while (*cpp) cpp++;
+ cp = *--cpp;
+ while (*cp) cp++;
+ } else
+ cp = (char *)&cp;
+
+ curbrk = (caddr_t)
+ (((long)(cp - 1 - rlim.rlim_cur) + PAGSIZ) & ~(PAGSIZ - 1));
+}
+
+void
+#if __STDC__
+xprintf(char *fmt, ...)
+#else
+xprintf(fmt, va_alist)
+char *fmt;
+#endif
+{
+ char buf[256];
+ va_list ap;
+#if __STDC__
+ va_start(ap, fmt);
+#else
+ va_start(ap);
+#endif
+
+ vsprintf(buf, fmt, ap);
+ (void)write(1, buf, strlen(buf));
+ va_end(ap);
+}
+
+caddr_t
+sbrk(incr)
+int incr;
+{
+ int fd = -1;
+ caddr_t oldbrk;
+
+ if (curbrk == 0)
+ init_brk();
+
+#if DEBUG
+xprintf("sbrk: incr = %#x, curbrk = %#x\n", incr, curbrk);
+#endif
+ if (incr == 0)
+ return curbrk;
+
+ incr = (incr + PAGSIZ - 1) & ~(PAGSIZ - 1);
+
+#ifdef NEED_DEV_ZERO
+ fd = open("/dev/zero", O_RDWR, 0);
+ if (fd == -1)
+ perror("/dev/zero");
+#endif
+
+ if (mmap(curbrk, incr,
+ PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_FIXED|MAP_COPY, fd, 0) == (caddr_t)-1) {
+ perror("Cannot map anonymous memory");
+ }
+
+#ifdef NEED_DEV_ZERO
+ close(fd);
+#endif
+
+ oldbrk = curbrk;
+ curbrk += incr;
+
+ return oldbrk;
+}
diff --git a/gnu/usr.bin/ld/shlib.c b/gnu/usr.bin/ld/shlib.c
new file mode 100644
index 0000000..4be8354
--- /dev/null
+++ b/gnu/usr.bin/ld/shlib.c
@@ -0,0 +1,203 @@
+/*
+ * $Id: shlib.c,v 1.3 1993/10/23 00:34:26 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <string.h>
+#include <dirent.h>
+#include <a.out.h>
+
+#include "ld.h"
+
+/*
+ * Standard directories to search for files specified by -l.
+ */
+#ifndef STANDARD_SEARCH_DIRS
+#define STANDARD_SEARCH_DIRS "/usr/lib", "/usr/local/lib"
+#endif
+
+char *standard_search_dirs[] = {
+ STANDARD_SEARCH_DIRS
+};
+
+int n_search_dirs;
+
+void
+add_search_dir(name)
+ char *name;
+{
+ n_search_dirs++;
+ search_dirs = (char **)xrealloc(search_dirs,
+ n_search_dirs * sizeof(char *));
+ search_dirs[n_search_dirs - 1] = strdup(name);
+}
+
+void
+std_search_dirs(paths)
+char *paths;
+{
+ char *cp;
+ int i, n;
+
+ if (paths != NULL)
+ /* Add search directories from `paths' */
+ while ((cp = strtok(paths, ":")) != NULL) {
+ paths = NULL;
+ add_search_dir(cp);
+ }
+
+ /* Append standard search directories */
+ n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
+ for (i = 0; i < n; i++)
+ add_search_dir(standard_search_dirs[i]);
+}
+
+/*
+ * Return true if CP points to a valid dewey number.
+ * Decode and leave the result in the array DEWEY.
+ * Return the number of decoded entries in DEWEY.
+ */
+
+int
+getdewey(dewey, cp)
+int dewey[];
+char *cp;
+{
+ int i, n;
+
+ for (n = 0, i = 0; i < MAXDEWEY; i++) {
+ if (*cp == '\0')
+ break;
+
+ if (*cp == '.') cp++;
+ if (!isdigit(*cp))
+ return 0;
+
+ dewey[n++] = strtol(cp, &cp, 10);
+ }
+
+ return n;
+}
+
+/*
+ * Compare two dewey arrays.
+ * Return -1 if `d1' represents a smaller value than `d2'.
+ * Return 1 if `d1' represents a greater value than `d2'.
+ * Return 0 if equal.
+ */
+int
+cmpndewey(d1, n1, d2, n2)
+int d1[], d2[];
+int n1, n2;
+{
+ int i;
+
+ for (i = 0; i < n1 && i < n2; i++) {
+ if (d1[i] < d2[i])
+ return -1;
+ if (d1[i] > d2[i])
+ return 1;
+ }
+
+ if (n1 == n2)
+ return 0;
+
+ if (i == n1)
+ return -1;
+
+ if (i == n2)
+ return 1;
+}
+
+/*
+ * Search directories for a shared library matching the given
+ * major and minor version numbers.
+ *
+ * MAJOR == -1 && MINOR == -1 --> find highest version
+ * MAJOR != -1 && MINOR == -1 --> find highest minor version
+ * MAJOR == -1 && MINOR != -1 --> invalid
+ * MAJOR != -1 && MINOR != -1 --> find highest micro version
+ */
+
+/* Not interested in devices right now... */
+#undef major
+#undef minor
+
+char *
+findshlib(name, majorp, minorp)
+char *name;
+int *majorp, *minorp;
+{
+ int dewey[MAXDEWEY];
+ int ndewey;
+ int tmp[MAXDEWEY];
+ int i;
+ int len;
+ char *lname, *path = NULL;
+ int major = *majorp, minor = *minorp;
+
+ len = strlen(name);
+ lname = (char *)alloca(len + sizeof("lib"));
+ sprintf(lname, "lib%s", name);
+ len += 3;
+
+ ndewey = 0;
+
+ for (i = 0; i < n_search_dirs; i++) {
+ DIR *dd = opendir(search_dirs[i]);
+ struct dirent *dp;
+
+ if (dd == NULL)
+ continue;
+
+ while ((dp = readdir(dd)) != NULL) {
+ int n, j, might_take_it = 0;
+
+ if (dp->d_namlen < len + 4)
+ continue;
+ if (strncmp(dp->d_name, lname, len) != 0)
+ continue;
+ if (strncmp(dp->d_name+len, ".so.", 4) != 0)
+ continue;
+
+ if ((n = getdewey(tmp, dp->d_name+len+4)) == 0)
+ continue;
+
+ if (major == -1 && minor == -1) {
+ might_take_it = 1;
+ } else if (major != -1 && minor == -1) {
+ if (tmp[0] == major)
+ might_take_it = 1;
+ } else if (major != -1 && minor != -1) {
+ if (tmp[0] == major)
+ if (n == 1 || tmp[1] >= minor)
+ might_take_it = 1;
+ }
+
+ if (!might_take_it)
+ continue;
+
+ if (cmpndewey(tmp, n, dewey, ndewey) <= 0)
+ continue;
+
+ /* We have a better version */
+ if (path)
+ free(path);
+ path = concat(search_dirs[i], "/", dp->d_name);
+ bcopy(tmp, dewey, sizeof(dewey));
+ ndewey = n;
+ *majorp = dewey[0];
+ *minorp = dewey[1];
+ }
+ closedir(dd);
+ }
+
+ return path;
+}
diff --git a/gnu/usr.bin/ld/sparc/md-static-funcs.c b/gnu/usr.bin/ld/sparc/md-static-funcs.c
new file mode 100644
index 0000000..99e6240
--- /dev/null
+++ b/gnu/usr.bin/ld/sparc/md-static-funcs.c
@@ -0,0 +1,35 @@
+
+/*
+ * Simple SPARC relocations for the benefit of self-relocation of ld.so
+ * avoiding the use of global variables (ie. reloc_bitshift[] et. al.).
+ * Only types supported are RELOC_32 and RELOC_RELATIVE.
+ *
+ * This *must* be a static function, so it is not called through a jmpslot.
+ */
+static void
+md_relocate_simple(r, relocation, addr)
+struct relocation_info *r;
+long relocation;
+char *addr;
+{
+ register unsigned long mask;
+ register unsigned long shift;
+
+ switch (r->r_type) {
+ case RELOC_32:
+ mask = 0xffffffff;
+ shift = 0;
+ break;
+ case RELOC_RELATIVE:
+ mask = 0x003fffff;
+ shift = 10;
+ break;
+ }
+ relocation += (*(long *)addr & mask) << shift;
+ relocation >>= shift;
+ relocation &= mask;
+
+ *(long *) (addr) &= ~mask;
+ *(long *) (addr) |= relocation;
+}
+
diff --git a/gnu/usr.bin/ld/sparc/md.c b/gnu/usr.bin/ld/sparc/md.c
new file mode 100644
index 0000000..2fe093b
--- /dev/null
+++ b/gnu/usr.bin/ld/sparc/md.c
@@ -0,0 +1,279 @@
+/*
+ * $Id: md.c,v 1.2 1993/10/27 00:56:17 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+
+#include "ld.h"
+
+/*
+ * Relocation masks and sizes for the Sparc architecture.
+ *
+ * Note that these are very dependent on the order of the enums in
+ * enum reloc_type (in a.out.h); if they change the following must be
+ * changed.
+ * Also, note that RELOC_RELATIVE is handled as if it were a RELOC_HI22.
+ * This should work provided that relocations values have zeroes in their
+ * least significant 10 bits. As RELOC_RELATIVE is used only to relocate
+ * with load address values - which are page aligned - this condition is
+ * fulfilled as long as the system's page size is > 1024 (and a power of 2).
+ */
+static int reloc_target_rightshift[] = {
+ 0, 0, 0, /* RELOC_8, _16, _32 */
+ 0, 0, 0, 2, 2, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
+ 10, 0, /* HI22, _22 */
+ 0, 0, /* RELOC_13, _LO10 */
+ 0, 0, /* _SFA_BASE, _SFA_OFF13 */
+ 0, 0, 10, /* _BASE10, _BASE13, _BASE22 */
+ 0, 10, /* _PC10, _PC22 */
+ 2, 0, /* _JMP_TBL, _SEGOFF16 */
+ 0, 0, 0 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
+};
+static int reloc_target_size[] = {
+ 0, 1, 2, /* RELOC_8, _16, _32 */
+ 0, 1, 2, 2, 2, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
+ 2, 2, /* HI22, _22 */
+ 2, 2, /* RELOC_13, _LO10 */
+ 2, 2, /* _SFA_BASE, _SFA_OFF13 */
+ 2, 2, 2, /* _BASE10, _BASE13, _BASE22 */
+ 2, 2, /* _PC10, _PC22 */
+ 2, 0, /* _JMP_TBL, _SEGOFF16 */
+ 2, 0, 2 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
+};
+static int reloc_target_bitsize[] = {
+ 8, 16, 32, /* RELOC_8, _16, _32 */
+ 8, 16, 32, 30, 22, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
+ 22, 22, /* HI22, _22 */
+ 13, 10, /* RELOC_13, _LO10 */
+ 32, 32, /* _SFA_BASE, _SFA_OFF13 */
+ 10, 13, 22, /* _BASE10, _BASE13, _BASE22 */
+ 10, 22, /* _PC10, _PC22 */
+ 30, 0, /* _JMP_TBL, _SEGOFF16 */
+ 32, 0, 22 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
+};
+
+
+/*
+ * Get relocation addend corresponding to relocation record RP
+ * ADDR unused by SPARC impl.
+ */
+long
+md_get_addend(r, addr)
+struct relocation_info *r;
+unsigned char *addr;
+{
+ return r->r_addend;
+}
+
+void
+md_relocate(r, relocation, addr, relocatable_output)
+struct relocation_info *r;
+long relocation;
+unsigned char *addr;
+int relocatable_output;
+{
+ register unsigned long mask;
+
+ if (relocatable_output) {
+ /*
+ * Non-PC relative relocations which are absolute or
+ * which have become non-external now have fixed
+ * relocations. Set the ADD_EXTRA of this relocation
+ * to be the relocation we have now determined.
+ */
+ if (!RELOC_PCREL_P(r)) {
+ if ((int) r->r_type <= RELOC_32
+ || RELOC_EXTERN_P(r) == 0)
+ RELOC_ADD_EXTRA(r) = relocation;
+ } else if (RELOC_EXTERN_P(r))
+ /*
+ * External PC-relative relocations continue
+ * to move around; update their relocations
+ * by the amount they have moved so far.
+ */
+ RELOC_ADD_EXTRA(r) -= pc_relocation;
+ return;
+ }
+
+ relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
+
+ /* Unshifted mask for relocation */
+ mask = 1 << RELOC_TARGET_BITSIZE(r) - 1;
+ mask |= mask - 1;
+ relocation &= mask;
+
+ /* Shift everything up to where it's going to be used */
+ relocation <<= RELOC_TARGET_BITPOS(r);
+ mask <<= RELOC_TARGET_BITPOS(r);
+
+ switch (RELOC_TARGET_SIZE(r)) {
+ case 0:
+ if (RELOC_MEMORY_ADD_P(r))
+ relocation += (mask & *(u_char *) (addr));
+ *(u_char *) (addr) &= ~mask;
+ *(u_char *) (addr) |= relocation;
+ break;
+
+ case 1:
+ if (RELOC_MEMORY_ADD_P(r))
+ relocation += (mask & *(u_short *) (addr));
+ *(u_short *) (addr) &= ~mask;
+ *(u_short *) (addr) |= relocation;
+ break;
+
+ case 2:
+ if (RELOC_MEMORY_ADD_P(r))
+ relocation += (mask & *(u_long *) (addr));
+ *(u_long *) (addr) &= ~mask;
+ *(u_long *) (addr) |= relocation;
+ break;
+ default:
+ fatal( "Unimplemented relocation field length in");
+ }
+}
+
+/*
+ * Initialize (output) exec header such that useful values are
+ * obtained from subsequent N_*() macro evaluations.
+ */
+void
+md_init_header(hp, magic, flags)
+struct exec *hp;
+int magic, flags;
+{
+ hp->a_magic = magic;
+ hp->a_machtype = M_SPARC;
+ hp->a_toolversion = 1;
+ hp->a_dynamic = ((flags) & EX_DYNAMIC);
+
+ /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */
+ if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
+ hp->a_entry = N_PAGSIZ(*hp);
+}
+
+/*
+ * Machine dependent part of claim_rrs_reloc().
+ * On the Sparc the relocation offsets are stored in the r_addend member.
+ */
+int
+md_make_reloc(rp, r, type)
+struct relocation_info *rp, *r;
+int type;
+{
+ r->r_type = rp->r_type;
+ r->r_addend = rp->r_addend;
+
+#if 1
+ /*
+ * This wouldn't be strictly necessary - we could record the
+ * location value "in situ" in stead of in the r_addend field -
+ * but we are being Sun compatible here. Besides, Sun's ld.so
+ * has a bug that prevents it from handling this alternate method
+ */
+ if (RELOC_PCREL_P(rp))
+ r->r_addend -= pc_relocation;
+#endif
+
+ return 1;
+}
+
+/*
+ * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
+ * to the binder slot (which is at offset 0 of the PLT).
+ */
+void
+md_make_jmpslot(sp, offset, index)
+jmpslot_t *sp;
+long offset;
+long index;
+{
+ u_long fudge = (u_long) -(sizeof(sp->opcode1) + offset);
+ sp->opcode1 = SAVE;
+ /* The following is a RELOC_WDISP30 relocation */
+ sp->opcode2 = CALL | ((fudge >> 2) & 0x3fffffff);
+ sp->reloc_index = NOP | index;
+}
+
+/*
+ * Set up a "direct" transfer (ie. not through the run-time binder) from
+ * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
+ * and by `ld.so' after resolving the symbol.
+ * On the i386, we use the JMP instruction which is PC relative, so no
+ * further RRS relocations will be necessary for such a jmpslot.
+ *
+ * OFFSET unused on Sparc.
+ */
+void
+md_fix_jmpslot(sp, offset, addr)
+jmpslot_t *sp;
+long offset;
+u_long addr;
+{
+ /*
+ * Here comes a RELOC_{LO10,HI22} relocation pair
+ * The resulting code is:
+ * sethi %hi(addr), %g1
+ * jmp %g1+%lo(addr)
+ * nop ! delay slot
+ */
+ sp->opcode1 = SETHI | ((addr >> 10) & 0x003fffff);
+ sp->opcode2 = JMP | (addr & 0x000003ff);
+ sp->reloc_index = NOP;
+}
+
+/*
+ * Update the relocation record for a jmpslot.
+ */
+void
+md_make_jmpreloc(rp, r, type)
+struct relocation_info *rp, *r;
+int type;
+{
+ if (type & RELTYPE_RELATIVE)
+ r->r_type = RELOC_RELATIVE;
+ else
+ r->r_type = RELOC_JMP_SLOT;
+
+ r->r_addend = rp->r_addend;
+}
+
+/*
+ * Set relocation type for a GOT RRS relocation.
+ */
+void
+md_make_gotreloc(rp, r, type)
+struct relocation_info *rp, *r;
+int type;
+{
+ /*
+ * GOT value resolved (symbolic or entry point): R_32
+ * GOT not resolved: GLOB_DAT
+ *
+ * NOTE: I don't think it makes a difference.
+ */
+ if (type & RELTYPE_RELATIVE)
+ r->r_type = RELOC_32;
+ else
+ r->r_type = RELOC_GLOB_DAT;
+
+ r->r_addend = 0;
+}
+
+/*
+ * Set relocation type for a RRS copy operation.
+ */
+void
+md_make_cpyreloc(rp, r)
+struct relocation_info *rp, *r;
+{
+ r->r_type = RELOC_COPY_DAT;
+ r->r_addend = 0;
+}
+
diff --git a/gnu/usr.bin/ld/sparc/md.h b/gnu/usr.bin/ld/sparc/md.h
new file mode 100644
index 0000000..e81d184
--- /dev/null
+++ b/gnu/usr.bin/ld/sparc/md.h
@@ -0,0 +1,270 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: md.h,v 1.3 1993/10/24 00:48:20 pk Exp $
+ */
+
+/*
+ * SPARC machine dependent definitions
+ */
+
+
+#define MAX_ALIGNMENT (sizeof (double))
+
+#ifdef NetBSD
+#define PAGSIZ __LDPGSZ
+
+#define N_SET_FLAG(ex,f) N_SETMAGIC(ex,N_GETMAGIC(ex), \
+ MID_MACHINE, N_GETFLAG(ex)|(f))
+#define N_IS_DYNAMIC(ex) ((N_GETFLAG(ex) & EX_DYNAMIC))
+
+#else
+
+/* Get the SunOS a.out and relocation nomenclature */
+#define EX_DYNAMIC 1
+
+#define N_IS_DYNAMIC(ex) ((ex).a_dynamic)
+
+#define N_SET_FLAG(ex, f) { \
+ (ex).a_dynamic = ((f) & EX_DYNAMIC); \
+}
+
+#undef relocation_info
+#define relocation_info reloc_info_sparc
+#define r_symbolnum r_index
+#endif /* NetBSD */
+
+/* Sparc (Sun 4) macros */
+#define RELOC_ADDRESS(r) ((r)->r_address)
+#define RELOC_EXTERN_P(r) ((r)->r_extern)
+#define RELOC_TYPE(r) ((r)->r_symbolnum)
+#define RELOC_SYMBOL(r) ((r)->r_symbolnum)
+#define RELOC_MEMORY_SUB_P(r) 0
+#ifdef RTLD
+/* XXX - consider this making SUN_COMPAT --> repercussions on rrs.c */
+#define RELOC_MEMORY_ADD_P(r) 1
+#else
+#define RELOC_MEMORY_ADD_P(r) 0
+#endif
+#define RELOC_ADD_EXTRA(r) ((r)->r_addend)
+#define RELOC_PCREL_P(r) \
+ (((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22) \
+ || ((r)->r_type == RELOC_PC10 || (r)->r_type == RELOC_PC22) \
+ || (r)->r_type == RELOC_JMP_TBL)
+#define RELOC_VALUE_RIGHTSHIFT(r) (reloc_target_rightshift[(r)->r_type])
+#define RELOC_TARGET_SIZE(r) (reloc_target_size[(r)->r_type])
+#define RELOC_TARGET_BITPOS(r) 0
+#define RELOC_TARGET_BITSIZE(r) (reloc_target_bitsize[(r)->r_type])
+
+#define RELOC_JMPTAB_P(r) ((r)->r_type == RELOC_JMP_TBL)
+
+#define RELOC_BASEREL_P(r) \
+ ((r)->r_type >= RELOC_BASE10 && (r)->r_type <= RELOC_BASE22)
+
+#define RELOC_RELATIVE_P(r) ((r)->r_type == RELOC_RELATIVE)
+#define RELOC_COPY_DAT (RELOC_RELATIVE+1) /*XXX*/
+#define RELOC_COPY_P(r) ((r)->r_type == RELOC_COPY_DAT)
+#define RELOC_LAZY_P(r) ((r)->r_type == RELOC_JMP_SLOT)
+
+#define RELOC_STATICS_THROUGH_GOT_P(r) (1)
+#define JMPSLOT_NEEDS_RELOC (1)
+
+#define CHECK_GOT_RELOC(r) \
+ ((r)->r_type == RELOC_PC10 || (r)->r_type == RELOC_PC22)
+
+#define md_got_reloc(r) (-(r)->r_address)
+
+#ifdef SUN_COMPAT
+/*
+ * Sun plays games with `r_addend'
+ */
+#define md_get_rt_segment_addend(r,a) (0)
+#endif
+
+/* Width of a Global Offset Table entry */
+typedef long got_t;
+
+typedef struct jmpslot {
+ u_long opcode1;
+ u_long opcode2;
+ u_long reloc_index;
+#define JMPSLOT_RELOC_MASK (0x003fffff) /* 22 bits */
+} jmpslot_t;
+
+#define SAVE 0x9de3bfa0 /* Build stack frame (opcode1) */
+#define SETHI 0x03000000 /* %hi(addr) -> %g1 (opcode1) */
+#define CALL 0x40000000 /* Call instruction (opcode2) */
+#define JMP 0x81c06000 /* Jump %g1 instruction (opcode2) */
+#define NOP 0x01000000 /* Delay slot NOP for (reloc_index) */
+
+
+/*
+ * Byte swap defs for cross linking
+ */
+
+#if !defined(NEED_SWAP)
+
+#define md_swapin_exec_hdr(h)
+#define md_swapout_exec_hdr(h)
+#define md_swapin_symbols(s,n)
+#define md_swapout_symbols(s,n)
+#define md_swapin_zsymbols(s,n)
+#define md_swapout_zsymbols(s,n)
+#define md_swapin_reloc(r,n)
+#define md_swapout_reloc(r,n)
+#define md_swapin_link_dynamic(l)
+#define md_swapout_link_dynamic(l)
+#define md_swapin_link_dynamic_2(l)
+#define md_swapout_link_dynamic_2(l)
+#define md_swapin_ld_debug(d)
+#define md_swapout_ld_debug(d)
+#define md_swapin_rrs_hash(f,n)
+#define md_swapout_rrs_hash(f,n)
+#define md_swapin_link_object(l,n)
+#define md_swapout_link_object(l,n)
+#define md_swapout_jmpslot(j,n)
+#define md_swapout_got(g,n)
+#define md_swapin_ranlib_hdr(h,n)
+#define md_swapout_ranlib_hdr(h,n)
+
+#endif /* NEED_SWAP */
+
+#ifdef CROSS_LINKER
+
+#ifdef NEED_SWAP
+
+/* Define IO byte swapping routines */
+
+void md_swapin_exec_hdr __P((struct exec *));
+void md_swapout_exec_hdr __P((struct exec *));
+void md_swapin_reloc __P((struct relocation_info *, int));
+void md_swapout_reloc __P((struct relocation_info *, int));
+void md_swapout_jmpslot __P((jmpslot_t *, int));
+
+#define md_swapin_symbols(s,n) swap_symbols(s,n)
+#define md_swapout_symbols(s,n) swap_symbols(s,n)
+#define md_swapin_zsymbols(s,n) swap_zsymbols(s,n)
+#define md_swapout_zsymbols(s,n) swap_zsymbols(s,n)
+#define md_swapin_link_dynamic(l) swap_link_dynamic(l)
+#define md_swapout_link_dynamic(l) swap_link_dynamic(l)
+#define md_swapin_link_dynamic_2(l) swap_link_dynamic_2(l)
+#define md_swapout_link_dynamic_2(l) swap_link_dynamic_2(l)
+#define md_swapin_ld_debug(d) swap_ld_debug(d)
+#define md_swapout_ld_debug(d) swap_ld_debug(d)
+#define md_swapin_rrs_hash(f,n) swap_rrs_hash(f,n)
+#define md_swapout_rrs_hash(f,n) swap_rrs_hash(f,n)
+#define md_swapin_link_object(l,n) swapin_link_object(l,n)
+#define md_swapout_link_object(l,n) swapout_link_object(l,n)
+#define md_swapout_got(g,n) swap_longs((long*)(g),n)
+#define md_swapin_ranlib_hdr(h,n) swap_ranlib_hdr(h,n)
+#define md_swapout_ranlib_hdr(h,n) swap_ranlib_hdr(h,n)
+
+#define md_swap_short(x) ( (((x) >> 8) & 0xff) | (((x) & 0xff) << 8) )
+
+#define md_swap_long(x) ( (((x) >> 24) & 0xff ) | (((x) >> 8 ) & 0xff00 ) | \
+ (((x) << 8 ) & 0xff0000) | (((x) << 24) & 0xff000000))
+
+#define get_byte(p) ( ((unsigned char *)(p))[0] )
+
+#define get_short(p) ( ( ((unsigned char *)(p))[1] << 8) | \
+ ( ((unsigned char *)(p))[0] ) \
+ )
+#define get_long(p) ( ( ((unsigned char *)(p))[3] << 24) | \
+ ( ((unsigned char *)(p))[2] << 16) | \
+ ( ((unsigned char *)(p))[1] << 8 ) | \
+ ( ((unsigned char *)(p))[0] ) \
+ )
+
+#define put_byte(p, v) { ((unsigned char *)(p))[0] = ((unsigned long)(v)); }
+
+#define put_short(p, v) { ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#define put_long(p, v) { ((unsigned char *)(p))[3] = \
+ ((((unsigned long)(v)) >> 24) & 0xff); \
+ ((unsigned char *)(p))[2] = \
+ ((((unsigned long)(v)) >> 16) & 0xff); \
+ ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#else /* We need not swap, but must pay attention to alignment: */
+
+#define md_swap_short(x) (x)
+#define md_swap_long(x) (x)
+
+#define get_byte(p) ( ((unsigned char *)(p))[0] )
+
+#define get_short(p) ( ( ((unsigned char *)(p))[0] << 8) | \
+ ( ((unsigned char *)(p))[1] ) \
+ )
+
+#define get_long(p) ( ( ((unsigned char *)(p))[0] << 24) | \
+ ( ((unsigned char *)(p))[1] << 16) | \
+ ( ((unsigned char *)(p))[2] << 8 ) | \
+ ( ((unsigned char *)(p))[3] ) \
+ )
+
+
+#define put_byte(p, v) { ((unsigned char *)(p))[0] = ((unsigned long)(v)); }
+
+#define put_short(p, v) { ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#define put_long(p, v) { ((unsigned char *)(p))[0] = \
+ ((((unsigned long)(v)) >> 24) & 0xff); \
+ ((unsigned char *)(p))[1] = \
+ ((((unsigned long)(v)) >> 16) & 0xff); \
+ ((unsigned char *)(p))[2] = \
+ ((((unsigned long)(v)) >> 8) & 0xff); \
+ ((unsigned char *)(p))[3] = \
+ ((((unsigned long)(v)) ) & 0xff); }
+
+#endif /* NEED_SWAP */
+
+#else /* Not a cross linker: use native */
+
+#define md_swap_short(x) (x)
+#define md_swap_long(x) (x)
+
+#define get_byte(where) (*(char *)(where))
+#define get_short(where) (*(short *)(where))
+#define get_long(where) (*(long *)(where))
+
+#define put_byte(where,what) (*(char *)(where) = (what))
+#define put_short(where,what) (*(short *)(where) = (what))
+#define put_long(where,what) (*(long *)(where) = (what))
+
+#endif /* CROSS_LINKER */
+
diff --git a/gnu/usr.bin/ld/sparc/mdprologue.S b/gnu/usr.bin/ld/sparc/mdprologue.S
new file mode 100644
index 0000000..8ad60b0
--- /dev/null
+++ b/gnu/usr.bin/ld/sparc/mdprologue.S
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: mdprologue.S,v 1.1 1993/10/16 21:54:36 pk Exp $
+ */
+
+/*
+ * SPARC run-time link editor entry points.
+ */
+
+#define CRT_VERSION_SUN 1
+
+ .seg "text" ! [internal]
+ .proc 16
+ .global _rtld_entry
+_rtld_entry:
+!#PROLOGUE# 0
+ save %sp,-96,%sp
+L.1B:
+ call L.2B
+ sethi %hi((__GLOBAL_OFFSET_TABLE_-(L.1B-.))),%l7
+L.2B:
+!#PROLOGUE# 1
+ or %l7,%lo((__GLOBAL_OFFSET_TABLE_-(L.1B-.))),%l7
+ add %l7,%o7,%l7
+
+ cmp %i0, CRT_VERSION_SUN ! is crtp passed in Sun style,
+ bne 1f ! ie. relative to stack frame ?
+ nop
+ add %i1, %fp, %i1 ! if so, adjust to absolute address
+1:
+ ld [%i1], %o3 ! load base address (crtp->crt_ba)
+ ld [%l7], %o2 ! get rtld's __DYNAMIC address
+ ! from 1st GOT entry
+ add %o2, %o3, %o2 ! relocate and make it 3rd arg.
+
+ ld [%l7 + _rtld], %g1 ! get address of rtld()
+ add %g1, %o3, %g1 ! relocate
+
+ mov %i1, %o1 ! set up args, #2: crtp
+ call %g1 ! rtld(version, crtp, dp)
+ mov %i0, %o0 ! arg #1: version
+
+ ret
+ restore
+ .seg "data" ! [internal]
+
+ .seg "text"
+ .global _binder_entry
+_binder_entry:
+!#PROLOGUE# 0
+ save %sp,-96,%sp
+!L.1C:
+! call L.2C
+! sethi %hi((__GLOBAL_OFFSET_TABLE_-(L.1C-.))),%l7
+!L.2C:
+! or %l7,%lo((__GLOBAL_OFFSET_TABLE_-(L.1C-.))),%l7
+!#PROLOGUE# 1
+
+ sub %i7, 4, %o0 ! get to jmpslot through pc
+ ld [%i7+4], %o1 ! get relocation index
+ sethi %hi(0x3fffff), %o2 ! -> reloc_index & 0x003fffff
+ or %o2, %lo(0x3fffff), %o2 ! [internal]
+ call _binder ! and call binder(jsp, reloc_index)
+ and %o1, %o2, %o1
+
+ mov %o0, %g1 ! return value == function address
+
+ restore ! get rid of our context
+ jmp %g1 ! and go.
+ restore ! and the jmpslot's
+ nop
+
+ .seg "data" ! [internal]
+
diff --git a/gnu/usr.bin/ld/symbol.c b/gnu/usr.bin/ld/symbol.c
new file mode 100644
index 0000000..899786c
--- /dev/null
+++ b/gnu/usr.bin/ld/symbol.c
@@ -0,0 +1,148 @@
+/*
+ * $Id: symbol.c,v 1.3 1993/11/01 16:26:19 pk Exp $ - symbol table routines
+ */
+
+/* Create the symbol table entries for `etext', `edata' and `end'. */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+
+#include "ld.h"
+
+void
+symtab_init (relocatable_output)
+int relocatable_output;
+{
+ /*
+ * Put linker reserved symbols into symbol table.
+ */
+
+ dynamic_symbol = getsym ("__DYNAMIC");
+ dynamic_symbol->defined = relocatable_output?N_UNDF:(N_DATA | N_EXT);
+ dynamic_symbol->referenced = 0;
+ dynamic_symbol->value = 0;
+
+ got_symbol = getsym ("__GLOBAL_OFFSET_TABLE_");
+ got_symbol->defined = N_DATA | N_EXT;
+ got_symbol->referenced = 0;
+ got_symbol->value = 0;
+
+ if (relocatable_output)
+ return;
+
+#ifndef nounderscore
+ edata_symbol = getsym ("_edata");
+ etext_symbol = getsym ("_etext");
+ end_symbol = getsym ("_end");
+#else
+ edata_symbol = getsym ("edata");
+ etext_symbol = getsym ("etext");
+ end_symbol = getsym ("end");
+#endif
+ edata_symbol->defined = N_DATA | N_EXT;
+ etext_symbol->defined = N_TEXT | N_EXT;
+ end_symbol->defined = N_BSS | N_EXT;
+
+ edata_symbol->referenced = 1;
+ etext_symbol->referenced = 1;
+ end_symbol->referenced = 1;
+}
+
+/* Compute the hash code for symbol name KEY. */
+
+int
+hash_string (key)
+ char *key;
+{
+ register char *cp;
+ register int k;
+
+ cp = key;
+ k = 0;
+ while (*cp)
+ k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
+
+ return k;
+}
+
+/* Get the symbol table entry for the global symbol named KEY.
+ Create one if there is none. */
+
+symbol *
+getsym(key)
+ char *key;
+{
+ register int hashval;
+ register symbol *bp;
+
+ /* Determine the proper bucket. */
+ hashval = hash_string (key) % TABSIZE;
+
+ /* Search the bucket. */
+ for (bp = symtab[hashval]; bp; bp = bp->link)
+ if (! strcmp (key, bp->name))
+ return bp;
+
+ /* Nothing was found; create a new symbol table entry. */
+ bp = (symbol *) xmalloc (sizeof (symbol));
+ bp->refs = 0;
+ bp->name = (char *) xmalloc (strlen (key) + 1);
+ strcpy (bp->name, key);
+ bp->defined = 0;
+ bp->referenced = 0;
+ bp->trace = 0;
+ bp->value = 0;
+ bp->max_common_size = 0;
+ bp->warning = 0;
+ bp->undef_refs = 0;
+ bp->multiply_defined = 0;
+ bp->alias = 0;
+ bp->setv_count = 0;
+
+ bp->size = 0;
+ bp->sorefs = 0;
+ bp->so_defined = 0;
+ bp->def_nlist = 0;
+ bp->jmpslot_offset = -1;
+ bp->gotslot_offset = -1;
+ bp->jmpslot_claimed = 0;
+ bp->gotslot_claimed = 0;
+ bp->cpyreloc_reserved = 0;
+ bp->cpyreloc_claimed = 0;
+
+ /* Add the entry to the bucket. */
+ bp->link = symtab[hashval];
+ symtab[hashval] = bp;
+
+ ++num_hash_tab_syms;
+
+ return bp;
+}
+
+/* Like `getsym' but return 0 if the symbol is not already known. */
+
+symbol *
+getsym_soft (key)
+ char *key;
+{
+ register int hashval;
+ register symbol *bp;
+
+ /* Determine which bucket. */
+
+ hashval = hash_string (key) % TABSIZE;
+
+ /* Search the bucket. */
+
+ for (bp = symtab[hashval]; bp; bp = bp->link)
+ if (! strcmp (key, bp->name))
+ return bp;
+
+ return 0;
+}
diff --git a/gnu/usr.bin/ld/symseg.h b/gnu/usr.bin/ld/symseg.h
index fe9939f..5e11b0b 100644
--- a/gnu/usr.bin/ld/symseg.h
+++ b/gnu/usr.bin/ld/symseg.h
@@ -3,7 +3,8 @@
* This code is derived from software copyrighted by the Free Software
* Foundation.
*
- * @(#)symseg.h 5.4 (Berkeley) 4/30/91
+ * from: @(#)symseg.h 5.4 (Berkeley) 4/30/91
+ * $Id: symseg.h,v 1.2 1993/08/01 18:46:59 mycroft Exp $
*/
/* GDB symbol table format definitions.
diff --git a/gnu/usr.bin/ld/warnings.c b/gnu/usr.bin/ld/warnings.c
new file mode 100644
index 0000000..abce79e
--- /dev/null
+++ b/gnu/usr.bin/ld/warnings.c
@@ -0,0 +1,755 @@
+/*
+ * $Id: warnings.c,v 1.3 1993/11/01 16:26:20 pk Exp $
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <ar.h>
+#include <ranlib.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+#include "ld.h"
+
+/*
+ * Print the filename of ENTRY on OUTFILE (a stdio stream),
+ * and then a newline.
+ */
+
+void
+prline_file_name (entry, outfile)
+ struct file_entry *entry;
+ FILE *outfile;
+{
+ print_file_name (entry, outfile);
+ fprintf (outfile, "\n");
+}
+
+/*
+ * Print the filename of ENTRY on OUTFILE (a stdio stream).
+ */
+
+void
+print_file_name (entry, outfile)
+ struct file_entry *entry;
+ FILE *outfile;
+{
+ if (entry->superfile) {
+ print_file_name (entry->superfile, outfile);
+ fprintf (outfile, "(%s)", entry->filename);
+ } else
+ fprintf (outfile, "%s", entry->filename);
+}
+
+/*
+ * Return the filename of entry as a string (malloc'd for the purpose)
+ */
+
+char *
+get_file_name (entry)
+ struct file_entry *entry;
+{
+ char *result, *supfile;
+
+ if (entry->superfile) {
+ supfile = get_file_name (entry->superfile);
+ result = (char *) xmalloc (strlen(supfile)
+ + strlen(entry->filename) + 3);
+ sprintf (result, "%s(%s)", supfile, entry->filename);
+ free (supfile);
+
+ } else {
+ result = (char *) xmalloc (strlen (entry->filename) + 1);
+ strcpy (result, entry->filename);
+ }
+ return result;
+}
+
+/*
+ * Report a fatal error. The error message is STRING followed by the
+ * filename of ENTRY.
+ */
+void
+#if __STDC__
+fatal_with_file (char *fmt, struct file_entry *entry, ...)
+#else
+fatal_with_file (fmt, entry, va_alist)
+ char *fmt;
+ struct file_entry *entry;
+ va_dcl
+#endif
+{
+ va_list ap;
+#if __STDC__
+ va_start(ap, fmt);
+#else
+ va_start(ap);
+#endif
+ (void)fprintf(stderr, "%s: ", progname);
+ (void)vfprintf(stderr, fmt, ap);
+ print_file_name (entry, stderr);
+ (void)fprintf(stderr, "\n");
+
+ va_end(ap);
+ exit (1);
+}
+
+/*
+ * Report a fatal error using the message for the last failed system call,
+ * followed by the string NAME.
+ */
+void
+perror_name (name)
+ char *name;
+{
+ extern int errno, sys_nerr;
+ extern char *sys_errlist[];
+ char *s;
+
+ if (errno < sys_nerr)
+ s = concat ("", sys_errlist[errno], " for %s");
+ else
+ s = "cannot open %s";
+ fatal (s, name);
+}
+
+/*
+ * Report a fatal error using the message for the last failed system call,
+ * followed by the name of file ENTRY.
+ */
+void
+perror_file (entry)
+ struct file_entry *entry;
+{
+ extern int errno, sys_nerr;
+ extern char *sys_errlist[];
+ char *s;
+
+ if (errno < sys_nerr)
+ s = concat ("", sys_errlist[errno], " for ");
+ else
+ s = "cannot open ";
+ fatal_with_file (s, entry);
+}
+
+
+/* Print a complete or partial map of the output file. */
+
+static void describe_file_sections __P((struct file_entry *, FILE *));
+static void list_file_locals __P((struct file_entry *, FILE *));
+
+void
+print_symbols(outfile)
+ FILE *outfile;
+{
+ register int i;
+
+ fprintf(outfile, "\nFiles:\n\n");
+
+ each_file(describe_file_sections, outfile);
+
+ fprintf(outfile, "\nGlobal symbols:\n\n");
+
+ for (i = 0; i < TABSIZE; i++) {
+ register symbol *sp;
+ for (sp = symtab[i]; sp; sp = sp->link) {
+ if (sp->defined == 1)
+ fprintf(outfile, " %s: common, length %#x\n",
+ sp->name, sp->max_common_size);
+ if ( sp->referenced) {
+ if (sp->defined)
+ fprintf(outfile, " %s: %#x %#x\n",
+ sp->name, sp->value, sp->size);
+ else
+ fprintf(outfile, " %s: undefined\n",
+ sp->name);
+ } else
+ fprintf(outfile, " %s: unreferenced\n",
+ sp->name);
+ }
+ }
+
+ each_file(list_file_locals, outfile);
+}
+
+static void
+describe_file_sections(entry, outfile)
+ struct file_entry *entry;
+ FILE *outfile;
+{
+ fprintf(outfile, " ");
+ print_file_name(entry, outfile);
+ if (entry->just_syms_flag)
+ fprintf(outfile, " symbols only\n", 0);
+ else
+ fprintf(outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
+ entry->text_start_address, entry->header.a_text,
+ entry->data_start_address, entry->header.a_data,
+ entry->bss_start_address, entry->header.a_bss);
+}
+
+static void
+list_file_locals (entry, outfile)
+ struct file_entry *entry;
+ FILE *outfile;
+{
+ struct localsymbol *lsp, *lspend;
+
+ entry->strings = (char *) alloca (entry->string_size);
+ read_entry_strings (file_open (entry), entry);
+
+ fprintf (outfile, "\nLocal symbols of ");
+ print_file_name (entry, outfile);
+ fprintf (outfile, ":\n\n");
+
+ lspend = entry->symbols + entry->nsymbols;
+ for (lsp = entry->symbols; lsp < lspend; lsp++) {
+ register struct nlist *p = &lsp->nzlist.nlist;
+ /*
+ * If this is a definition,
+ * update it if necessary by this file's start address.
+ */
+ if (!(p->n_type & (N_STAB | N_EXT)))
+ fprintf(outfile, " %s: 0x%x\n",
+ entry->strings + p->n_un.n_strx, p->n_value);
+ }
+
+ entry->strings = 0; /* All done with them. */
+}
+
+
+/* Static vars for do_warnings and subroutines of it */
+static int list_unresolved_refs; /* List unresolved refs */
+static int list_warning_symbols; /* List warning syms */
+static int list_multiple_defs; /* List multiple definitions */
+
+static struct line_debug_entry *init_debug_scan __P((int, struct file_entry *));
+static int next_debug_entry __P((int, struct line_debug_entry *));
+
+/*
+ * Structure for communication between do_file_warnings and it's
+ * helper routines. Will in practice be an array of three of these:
+ * 0) Current line, 1) Next line, 2) Source file info.
+ */
+struct line_debug_entry
+{
+ int line;
+ char *filename;
+ struct localsymbol *sym;
+};
+
+/*
+ * Helper routines for do_file_warnings.
+ */
+
+/* Return an integer less than, equal to, or greater than 0 as per the
+ relation between the two relocation entries. Used by qsort. */
+
+static int
+relocation_entries_relation (rel1, rel2)
+ struct relocation_info *rel1, *rel2;
+{
+ return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
+}
+
+/* Moves to the next debugging symbol in the file. USE_DATA_SYMBOLS
+ determines the type of the debugging symbol to look for (DSLINE or
+ SLINE). STATE_POINTER keeps track of the old and new locatiosn in
+ the file. It assumes that state_pointer[1] is valid; ie
+ that it.sym points into some entry in the symbol table. If
+ state_pointer[1].sym == 0, this routine should not be called. */
+
+static int
+next_debug_entry (use_data_symbols, state_pointer)
+ register int use_data_symbols;
+ /* Next must be passed by reference! */
+ struct line_debug_entry state_pointer[3];
+{
+ register struct line_debug_entry
+ *current = state_pointer,
+ *next = state_pointer + 1,
+ /* Used to store source file */
+ *source = state_pointer + 2;
+
+ struct file_entry *entry = (struct file_entry *) source->sym;
+ struct localsymbol *endp = entry->symbols + entry->nsymbols;
+
+
+ current->sym = next->sym;
+ current->line = next->line;
+ current->filename = next->filename;
+
+ while (++(next->sym) < endp) {
+
+ struct nlist *np = &next->sym->nzlist.nlist;
+
+ /*
+ * n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80,
+ * so may look negative...therefore, must mask to low bits
+ */
+ switch (np->n_type & 0xff) {
+ case N_SLINE:
+ if (use_data_symbols) continue;
+ next->line = np->n_desc;
+ return 1;
+ case N_DSLINE:
+ if (!use_data_symbols) continue;
+ next->line = np->n_desc;
+ return 1;
+#ifdef HAVE_SUN_STABS
+ case N_EINCL:
+ next->filename = source->filename;
+ continue;
+#endif
+ case N_SO:
+ source->filename = np->n_un.n_strx + entry->strings;
+ source->line++;
+#ifdef HAVE_SUN_STABS
+ case N_BINCL:
+#endif
+ case N_SOL:
+ next->filename = np->n_un.n_strx + entry->strings;
+ default:
+ continue;
+ }
+ }
+ next->sym = (struct localsymbol *)0;
+ return 0;
+}
+
+/*
+ * Create a structure to save the state of a scan through the debug symbols.
+ * USE_DATA_SYMBOLS is set if we should be scanning for DSLINE's instead of
+ * SLINE's. entry is the file entry which points at the symbols to use.
+ */
+
+static struct line_debug_entry *
+init_debug_scan(use_data_symbols, entry)
+ int use_data_symbols;
+ struct file_entry *entry;
+{
+ struct localsymbol *lsp;
+ struct line_debug_entry *state_pointer = (struct line_debug_entry *)
+ xmalloc(3 * sizeof(struct line_debug_entry));
+
+ register struct line_debug_entry
+ *current = state_pointer,
+ *next = state_pointer + 1,
+ *source = state_pointer + 2; /* Used to store source file */
+
+
+ for (lsp = entry->symbols; lsp < entry->symbols+entry->nsymbols; lsp++)
+ if (lsp->nzlist.nlist.n_type == N_SO)
+ break;
+
+ if (lsp >= entry->symbols + entry->nsymbols) {
+ /* I believe this translates to "We lose" */
+ current->filename = next->filename = entry->filename;
+ current->line = next->line = -1;
+ current->sym = next->sym = (struct localsymbol *) 0;
+ return state_pointer;
+ }
+ next->line = source->line = 0;
+ next->filename = source->filename
+ = (lsp->nzlist.nlist.n_un.n_strx + entry->strings);
+ source->sym = (struct localsymbol *) entry;
+ next->sym = lsp;
+
+ /* To setup next */
+ next_debug_entry(use_data_symbols, state_pointer);
+
+ if (!next->sym) { /* No line numbers for this section; */
+ /* setup output results as appropriate */
+ if (source->line) {
+ current->filename = source->filename = entry->filename;
+ current->line = -1; /* Don't print lineno */
+ } else {
+ current->filename = source->filename;
+ current->line = 0;
+ }
+ return state_pointer;
+ }
+ /* To setup current */
+ next_debug_entry(use_data_symbols, state_pointer);
+
+ return state_pointer;
+}
+
+/*
+ * Takes an ADDRESS (in either text or data space) and a STATE_POINTER which
+ * describes the current location in the implied scan through the debug
+ * symbols within the file which ADDRESS is within, and returns the source
+ * line number which corresponds to ADDRESS.
+ */
+
+static int
+address_to_line(address, state_pointer)
+ unsigned long address;
+/* Next must be passed by reference! */
+ struct line_debug_entry state_pointer[3];
+{
+ struct line_debug_entry
+ *current = state_pointer, *next = state_pointer + 1;
+ struct line_debug_entry *tmp_pointer;
+
+ int use_data_symbols;
+
+ if (next->sym)
+ use_data_symbols =
+ (next->sym->nzlist.nlist.n_type & N_TYPE) == N_DATA;
+ else
+ return current->line;
+
+ /* Go back to the beginning if we've already passed it. */
+ if (current->sym->nzlist.nlist.n_value > address) {
+ tmp_pointer = init_debug_scan(use_data_symbols,
+ (struct file_entry *)
+ ((state_pointer + 2)->sym));
+ state_pointer[0] = tmp_pointer[0];
+ state_pointer[1] = tmp_pointer[1];
+ state_pointer[2] = tmp_pointer[2];
+ free(tmp_pointer);
+ }
+ /* If we're still in a bad way, return -1, meaning invalid line. */
+ if (current->sym->nzlist.nlist.n_value > address)
+ return -1;
+
+ while (next->sym
+ && next->sym->nzlist.nlist.n_value <= address
+ && next_debug_entry(use_data_symbols, state_pointer));
+ return current->line;
+}
+
+
+/* Macros for manipulating bitvectors. */
+#define BIT_SET_P(bv, index) ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
+#define SET_BIT(bv, index) ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
+
+/*
+ * This routine will scan through the relocation data of file ENTRY, printing
+ * out references to undefined symbols and references to symbols defined in
+ * files with N_WARNING symbols. If DATA_SEGMENT is non-zero, it will scan
+ * the data relocation segment (and use N_DSLINE symbols to track line
+ * number); otherwise it will scan the text relocation segment. Warnings
+ * will be printed on the output stream OUTFILE. Eventually, every nlist
+ * symbol mapped through will be marked in the NLIST_BITVECTOR, so we don't
+ * repeat ourselves when we scan the nlists themselves.
+ */
+
+static void
+do_relocation_warnings(entry, data_segment, outfile, nlist_bitvector)
+ struct file_entry *entry;
+ int data_segment;
+ FILE *outfile;
+ unsigned char *nlist_bitvector;
+{
+ struct relocation_info *reloc, *reloc_start =
+ data_segment ? entry->datarel : entry->textrel;
+
+ int reloc_size = (data_segment ? entry->ndatarel : entry->ntextrel);
+ int start_of_segment = (data_segment ?
+ entry->data_start_address :
+ entry->text_start_address);
+ struct localsymbol *start_of_syms = entry->symbols;
+ struct line_debug_entry *state_pointer =
+ init_debug_scan(data_segment != 0, entry);
+
+ register struct line_debug_entry *current = state_pointer;
+
+ /* Assigned to generally static values; should not be written into. */
+ char *errfmt;
+
+ /*
+ * Assigned to alloca'd values cand copied into; should be freed when
+ * done.
+ */
+ char *errmsg;
+ int invalidate_line_number;
+
+ /*
+ * We need to sort the relocation info here. Sheesh, so much effort
+ * for one lousy error optimization.
+ */
+
+ qsort(reloc_start, reloc_size, sizeof(struct relocation_info),
+ relocation_entries_relation);
+
+ for (reloc = reloc_start;
+ reloc < (reloc_start + reloc_size);
+ reloc++) {
+ register struct localsymbol *s;
+ register symbol *g;
+
+ /*
+ * If the relocation isn't resolved through a symbol,
+ * continue
+ */
+ if (!RELOC_EXTERN_P(reloc))
+ continue;
+
+ s = &entry->symbols[RELOC_SYMBOL(reloc)];
+
+ /*
+ * Local symbols shouldn't ever be used by relocation info,
+ * so the next should be safe. This is, of course, wrong.
+ * References to local BSS symbols can be the targets of
+ * relocation info, and they can (must) be resolved through
+ * symbols. However, these must be defined properly, (the
+ * assembler would have caught it otherwise), so we can
+ * ignore these cases.
+ */
+ if (!(s->nzlist.nz_type & N_EXT))
+ continue;
+
+ g = s->symbol;
+ errmsg = 0;
+
+ if (!g->defined && !g->so_defined && list_unresolved_refs) { /* Reference */
+ /* Mark as being noted by relocation warning pass. */
+ SET_BIT(nlist_bitvector, s - start_of_syms);
+
+ if (g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */
+ continue;
+
+ /* Undefined symbol which we should mention */
+
+ if (++(g->undef_refs) == MAX_UREFS_PRINTED) {
+ errfmt = "More undefined symbol %s refs follow";
+ invalidate_line_number = 1;
+ } else {
+ errfmt = "Undefined symbol %s referenced from %s segment";
+ invalidate_line_number = 0;
+ }
+ } else { /* Defined */
+ /* Potential symbol warning here */
+ if (!g->warning)
+ continue;
+
+ /* Mark as being noted by relocation warning pass. */
+ SET_BIT(nlist_bitvector, s - start_of_syms);
+
+ errfmt = 0;
+ errmsg = g->warning;
+ invalidate_line_number = 0;
+ }
+
+
+ /* If errfmt == 0, errmsg has already been defined. */
+ if (errfmt != 0) {
+ char *nm;
+
+ nm = g->name;
+ errmsg = (char *) xmalloc(strlen(errfmt) + strlen(nm) + 1);
+ sprintf(errmsg, errfmt, nm, data_segment ? "data" : "text");
+ if (nm != g->name)
+ free(nm);
+ }
+ address_to_line(RELOC_ADDRESS(reloc) + start_of_segment,
+ state_pointer);
+
+ if (current->line >= 0)
+ fprintf(outfile, "%s:%d: %s\n", current->filename,
+ invalidate_line_number ? 0 : current->line, errmsg);
+ else
+ fprintf(outfile, "%s: %s\n", current->filename, errmsg);
+
+ if (errfmt != 0)
+ free(errmsg);
+ }
+
+ free(state_pointer);
+}
+
+/*
+ * Print on OUTFILE a list of all warnings generated by references and/or
+ * definitions in the file ENTRY. List source file and line number if
+ * possible, just the .o file if not.
+ */
+
+void
+do_file_warnings (entry, outfile)
+ struct file_entry *entry;
+ FILE *outfile;
+{
+ int number_of_syms = entry->nsymbols;
+ unsigned char *nlist_bitvector = (unsigned char *)
+ alloca ((number_of_syms >> 3) + 1);
+ struct line_debug_entry *text_scan, *data_scan;
+ int i;
+ char *errfmt, *file_name;
+ int line_number;
+ int dont_allow_symbol_name;
+
+ bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
+
+ /* Read in the files strings if they aren't available */
+ if (!entry->strings) {
+ int desc;
+
+ entry->strings = (char *) alloca (entry->string_size);
+ desc = file_open (entry);
+ read_entry_strings (desc, entry);
+ }
+
+ if (! entry->is_dynamic) {
+ /* Do text warnings based on a scan through the relocation info. */
+ do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
+
+ /* Do data warnings based on a scan through the relocation info. */
+ do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
+ }
+
+ /* Scan through all of the nlist entries in this file and pick up
+ anything that the scan through the relocation stuff didn't. */
+
+ text_scan = init_debug_scan (0, entry);
+ data_scan = init_debug_scan (1, entry);
+
+ for (i = 0; i < number_of_syms; i++) {
+ struct nlist *s;
+ struct glosym *g;
+
+ g = entry->symbols[i].symbol;
+ s = &entry->symbols[i].nzlist.nlist;
+
+ if (!(s->n_type & N_EXT))
+ continue;
+
+ if (!g->referenced) {
+#if 0
+ /* Check for undefined shobj symbols */
+ struct localsymbol *lsp;
+ register int type;
+
+ for (lsp = g->dynrefs; lsp; lsp = lsp->next) {
+ type = lsp->nlist.n_type;
+ if ((type & N_EXT) &&
+ type != (N_UNDF | N_EXT)) {
+ break;
+ }
+ }
+ if (type == (N_UNDF | N_EXT)) {
+ fprintf(stderr,
+ "Undefined symbol %s referenced from %s\n",
+ g->name,
+ get_file_name(entry));
+ }
+#endif
+ continue;
+ }
+
+ if (undefined_global_sym_count == 0)
+ /* No undefined symbols in rel files */
+ continue;
+
+ dont_allow_symbol_name = 0;
+
+ if (list_multiple_defs && g->multiply_defined) {
+ errfmt = "Definition of symbol %s (multiply defined)";
+ switch (s->n_type) {
+ case N_TEXT | N_EXT:
+ line_number = address_to_line (s->n_value, text_scan);
+ file_name = text_scan[0].filename;
+ break;
+ case N_DATA | N_EXT:
+ line_number = address_to_line (s->n_value, data_scan);
+ file_name = data_scan[0].filename;
+ break;
+#if 0
+ case N_SETA | N_EXT:
+ case N_SETT | N_EXT:
+ case N_SETD | N_EXT:
+ case N_SETB | N_EXT:
+ if (g->multiply_defined == 2)
+ continue;
+ errfmt = "First set element definition of symbol %s (multiply defined)";
+ break;
+#endif
+ default:
+ /* Don't print out multiple defs
+ at references.*/
+ continue;
+ }
+
+ } else if (BIT_SET_P (nlist_bitvector, i))
+ continue;
+ else if (list_unresolved_refs && !g->defined) {
+ if (g->undef_refs >= MAX_UREFS_PRINTED)
+ continue;
+
+ if (++(g->undef_refs) == MAX_UREFS_PRINTED)
+ errfmt = "More undefined \"%s\" refs follow";
+ else
+ errfmt = "Undefined symbol \"%s\" referenced";
+ line_number = -1;
+ } else if (g->warning) {
+ /*
+ * There are two cases in which we don't want to do
+ * this. The first is if this is a definition instead
+ * do a reference. The second is if it's the reference
+ * used by the warning stabs itself.
+ */
+ if (s->n_type != (N_EXT | N_UNDF)
+ || (i && (s-1)->n_type == N_WARNING))
+ continue;
+
+ errfmt = g->warning;
+ line_number = -1;
+ dont_allow_symbol_name = 1;
+ } else
+ continue;
+
+ if (line_number == -1)
+ fprintf (outfile, "%s: ", entry->filename);
+ else
+ fprintf (outfile, "%s:%d: ", file_name, line_number);
+
+ if (dont_allow_symbol_name)
+ fprintf (outfile, "%s", errfmt);
+ else
+ fprintf (outfile, errfmt, g->name);
+
+ fputc ('\n', outfile);
+ }
+ free (text_scan);
+ free (data_scan);
+ entry->strings = 0; /* Since it will dissapear anyway. */
+}
+
+int
+do_warnings(outfile)
+ FILE *outfile;
+{
+ list_unresolved_refs = !relocatable_output &&
+ (undefined_global_sym_count || undefined_shobj_sym_count);
+ list_warning_symbols = warning_count;
+ list_multiple_defs = multiple_def_count != 0;
+
+ if (!(list_unresolved_refs ||
+ list_warning_symbols || list_multiple_defs))
+ /* No need to run this routine */
+ return 1;
+
+ if (entry_symbol && !entry_symbol->defined)
+ fprintf (outfile, "Undefined entry symbol %s\n",
+ entry_symbol->name);
+ each_file (do_file_warnings, outfile);
+
+ if (list_unresolved_refs || list_multiple_defs)
+ return 0;
+
+ return 1;
+}
+
diff --git a/gnu/usr.bin/ld/xbits.c b/gnu/usr.bin/ld/xbits.c
new file mode 100644
index 0000000..4f85db1
--- /dev/null
+++ b/gnu/usr.bin/ld/xbits.c
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id: xbits.c,v 1.1 1993/10/16 21:52:37 pk Exp $
+ */
+
+/*
+ * "Generic" byte-swap routines.
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <fcntl.h>
+#include <ar.h>
+#include <ranlib.h>
+#include <a.out.h>
+#include <stab.h>
+#include <string.h>
+
+#include "ld.h"
+
+void
+swap_longs(lp, n)
+int n;
+long *lp;
+{
+ for (; n > 0; n--, lp++)
+ *lp = md_swap_long(*lp);
+}
+
+void
+swap_symbols(s, n)
+struct nlist *s;
+int n;
+{
+ for (; n; n--, s++) {
+ s->n_un.n_strx = md_swap_long(s->n_un.n_strx);
+ s->n_desc = md_swap_short(s->n_desc);
+ s->n_value = md_swap_long(s->n_value);
+ }
+}
+
+void
+swap_zsymbols(s, n)
+struct nzlist *s;
+int n;
+{
+ for (; n; n--, s++) {
+ s->nz_strx = md_swap_long(s->nz_strx);
+ s->nz_desc = md_swap_short(s->nz_desc);
+ s->nz_value = md_swap_long(s->nz_value);
+ s->nz_size = md_swap_long(s->nz_size);
+ }
+}
+
+
+void
+swap_ranlib_hdr(rlp, n)
+struct ranlib *rlp;
+int n;
+{
+ for (; n; n--, rlp++) {
+ rlp->ran_un.ran_strx = md_swap_long(rlp->ran_un.ran_strx);
+ rlp->ran_off = md_swap_long(rlp->ran_off);
+ }
+}
+
+void
+swap_link_dynamic(dp)
+struct link_dynamic *dp;
+{
+ dp->ld_version = md_swap_long(dp->ld_version);
+ dp->ldd = (struct ld_debug *)md_swap_long((long)dp->ldd);
+ dp->ld_un.ld_2 = (struct link_dynamic_2 *)md_swap_long((long)dp->ld_un.ld_2);
+ dp->ld_entry = (struct ld_entry *)md_swap_long((long)dp->ld_entry);
+}
+
+void
+swap_link_dynamic_2(ldp)
+struct link_dynamic_2 *ldp;
+{
+ swap_longs((long *)ldp, sizeof(*ldp)/sizeof(long));
+}
+
+void
+swap_ld_debug(lddp)
+struct ld_debug *lddp;
+{
+ swap_longs((long *)lddp, sizeof(*lddp)/sizeof(long));
+}
+
+void
+swapin_link_object(lop, n)
+struct link_object *lop;
+int n;
+{
+ unsigned long bits;
+
+ for (; n; n--, lop++) {
+ lop->lo_name = md_swap_long(lop->lo_name);
+ lop->lo_major = md_swap_short(lop->lo_major);
+ lop->lo_minor = md_swap_short(lop->lo_minor);
+ lop->lo_next = md_swap_long(lop->lo_next);
+ bits = ((unsigned long *)lop)[1];
+ lop->lo_library = ((bits >> 24) & 1);
+ }
+}
+
+void
+swapout_link_object(lop, n)
+struct link_object *lop;
+int n;
+{
+ unsigned long bits;
+
+ for (; n; n--, lop++) {
+ lop->lo_name = md_swap_long(lop->lo_name);
+ lop->lo_major = md_swap_short(lop->lo_major);
+ lop->lo_minor = md_swap_short(lop->lo_minor);
+ lop->lo_next = md_swap_long(lop->lo_next);
+ bits = (unsigned long)(lop->lo_library) << 24;
+ ((unsigned long *)lop)[1] = bits;
+ }
+}
+
+void
+swap_rrs_hash(fsp, n)
+struct rrs_hash *fsp;
+int n;
+{
+ for (; n; n--, fsp++) {
+ fsp->rh_symbolnum = md_swap_long(fsp->rh_symbolnum);
+ fsp->rh_next = md_swap_long(fsp->rh_next);
+ }
+}
+
OpenPOWER on IntegriCloud