diff --git a/gnu/usr.bin/ld/Makefile b/gnu/usr.bin/ld/Makefile index 93e98a10a08f..db8be96f63df 100644 --- a/gnu/usr.bin/ld/Makefile +++ b/gnu/usr.bin/ld/Makefile @@ -1,15 +1,6 @@ -# $NetBSD: Makefile,v 1.14 1997/03/24 19:54:35 perry Exp $ +# $NetBSD: Makefile,v 1.15 1997/04/16 16:49:28 christos Exp $ -PROG= 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_ARCH) - -LDADD+= -lgnumalloc -DPADD+= /usr/lib/libgnumalloc.a - -SUBDIR= ldconfig ldd - -.PATH: $(.CURDIR)/$(MACHINE_ARCH) +SUBDIR= ld ldconfig ldd .include # for NOPIC definition diff --git a/gnu/usr.bin/ld/Makefile.inc b/gnu/usr.bin/ld/Makefile.inc new file mode 100644 index 000000000000..f31028e187b3 --- /dev/null +++ b/gnu/usr.bin/ld/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.1 1997/04/16 16:49:29 christos Exp $ + +CFLAGS += -I$(.CURDIR)/../common -I$(.CURDIR)/../arch/$(MACHINE_ARCH) + +.PATH: $(.CURDIR)/../common $(.CURDIR)/../arch/$(MACHINE_ARCH) diff --git a/gnu/usr.bin/ld/cplus-dem.c b/gnu/usr.bin/ld/cplus-dem.c deleted file mode 100644 index 808db5e1cdfe..000000000000 --- a/gnu/usr.bin/ld/cplus-dem.c +++ /dev/null @@ -1,975 +0,0 @@ -/*- - * This code is derived from software copyrighted by the Free Software - * Foundation. - */ - -#ifndef lint -/*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++ - Copyright (C) 1989 Free Software Foundation, Inc. - written by James Clark (jjc@jclark.uucp) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 1, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ - -/* This is for g++ 1.36.1 (November 6 version). It will probably - require changes for any other version. - - Modified for g++ 1.36.2 (November 18 version). */ - -/* This file exports one function - - char *cplus_demangle (const char *name) - - If `name' is a mangled function name produced by g++, then - a pointer to a malloced string giving a C++ representation - of the name will be returned; otherwise NULL will be returned. - It is the caller's responsibility to free the string which - is returned. - - For example, - - cplus_demangle ("_foo__1Ai") - - returns - - "A::foo(int)" - - This file imports xmalloc and xrealloc, which are like malloc and - realloc except that they generate a fatal error if there is no - available memory. */ - -/* #define nounderscore 1 /* define this is names don't start with _ */ - -#include -#include - -#ifdef USG -#include -#include -#else -#include -#define memcpy(s1, s2, n) bcopy ((s2), (s1), (n)) -#define memcmp(s1, s2, n) bcmp ((s2), (s1), (n)) -#define strchr index -#define strrchr rindex -#endif - -#ifndef __STDC__ -#define const -#endif - -#ifdef __STDC__ -extern char *cplus_demangle (const char *type); -#else -extern char *cplus_demangle (); -#endif - -#ifdef __STDC__ -extern char *xmalloc (int); -extern char *xrealloc (char *, int); -#else -extern char *xmalloc (); -extern char *xrealloc (); -#endif - -static char **typevec = 0; -static int ntypes = 0; -static int typevec_size = 0; - -static struct { - const char *in; - const char *out; -} optable[] = { - "new", " new", - "delete", " delete", - "ne", "!=", - "eq", "==", - "ge", ">=", - "gt", ">", - "le", "<=", - "lt", "<", - "plus", "+", - "minus", "-", - "mult", "*", - "convert", "+", /* unary + */ - "negate", "-", /* unary - */ - "trunc_mod", "%", - "trunc_div", "/", - "truth_andif", "&&", - "truth_orif", "||", - "truth_not", "!", - "postincrement", "++", - "postdecrement", "--", - "bit_ior", "|", - "bit_xor", "^", - "bit_and", "&", - "bit_not", "~", - "call", "()", - "cond", "?:", - "alshift", "<<", - "arshift", ">>", - "component", "->", - "indirect", "*", - "method_call", "->()", - "addr", "&", /* unary & */ - "array", "[]", - "nop", "", /* for operator= */ -}; - -/* Beware: these aren't '\0' terminated. */ - -typedef struct { - char *b; /* pointer to start of string */ - char *p; /* pointer after last character */ - char *e; /* pointer after end of allocated space */ -} string; - -#ifdef __STDC__ -static void string_need (string *s, int n); -static void string_delete (string *s); -static void string_init (string *s); -static void string_clear (string *s); -static int string_empty (string *s); -static void string_append (string *p, const char *s); -static void string_appends (string *p, string *s); -static void string_appendn (string *p, const char *s, int n); -static void string_prepend (string *p, const char *s); -#if 0 -static void string_prepends (string *p, string *s); -#endif -static void string_prependn (string *p, const char *s, int n); -static int get_count (const char **type, int *count); -static int do_args (const char **type, string *decl); -static int do_type (const char **type, string *result); -static int do_arg (const char **type, string *result); -static int do_args (const char **type, string *decl); -static void munge_function_name (string *name); -static void remember_type (const char *type, int len); -#else -static void string_need (); -static void string_delete (); -static void string_init (); -static void string_clear (); -static int string_empty (); -static void string_append (); -static void string_appends (); -static void string_appendn (); -static void string_prepend (); -static void string_prepends (); -static void string_prependn (); -static int get_count (); -static int do_args (); -static int do_type (); -static int do_arg (); -static int do_args (); -static void munge_function_name (); -static void remember_type (); -#endif - -char * -cplus_demangle (type) - const char *type; -{ - string decl; - int n; - int success = 0; - int constructor = 0; - int const_flag = 0; - int i; - const char *p; -#ifndef LONGERNAMES - const char *premangle; -#endif - - if (type == NULL || *type == '\0') - return NULL; -#ifndef nounderscore - if (*type++ != '_') - return NULL; -#endif - p = type; - while (*p != '\0' && !(*p == '_' && p[1] == '_')) - p++; - if (*p == '\0') - { - /* destructor */ - if (type[0] == '_' && type[1] == '$' && type[2] == '_') - { - int n = (strlen (type) - 3)*2 + 3 + 2 + 1; - char *tem = (char *) xmalloc (n); - strcpy (tem, type + 3); - strcat (tem, "::~"); - strcat (tem, type + 3); - strcat (tem, "()"); - return tem; - } - /* static data member */ - if (*type != '_' && (p = strchr (type, '$')) != NULL) - { - int n = strlen (type) + 2; - char *tem = (char *) xmalloc (n); - memcpy (tem, type, p - type); - strcpy (tem + (p - type), "::"); - strcpy (tem + (p - type) + 2, p + 1); - return tem; - } - /* virtual table */ - if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == '$') - { - int n = strlen (type + 4) + 14 + 1; - char *tem = (char *) xmalloc (n); - strcpy (tem, type + 4); - strcat (tem, " virtual table"); - return tem; - } - return NULL; - } - - string_init (&decl); - - if (p == type) - { - if (!isdigit (p[2])) - { - string_delete (&decl); - return NULL; - } - constructor = 1; - } - else - { - string_appendn (&decl, type, p - type); - munge_function_name (&decl); - } - p += 2; - -#ifndef LONGERNAMES - premangle = p; -#endif - switch (*p) - { - case 'C': - /* a const member function */ - if (!isdigit (p[1])) - { - string_delete (&decl); - return NULL; - } - p += 1; - const_flag = 1; - /* fall through */ - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - n = 0; - do - { - n *= 10; - n += *p - '0'; - p += 1; - } - while (isdigit (*p)); - if (strlen (p) < n) - { - string_delete (&decl); - return NULL; - } - if (constructor) - { - string_appendn (&decl, p, n); - string_append (&decl, "::"); - string_appendn (&decl, p, n); - } - else - { - string_prepend (&decl, "::"); - string_prependn (&decl, p, n); - } - p += n; -#ifndef LONGERNAMES - remember_type (premangle, p - premangle); -#endif - success = do_args (&p, &decl); - if (const_flag) - string_append (&decl, " const"); - break; - case 'F': - p += 1; - success = do_args (&p, &decl); - break; - } - - for (i = 0; i < ntypes; i++) - if (typevec[i] != NULL) - free (typevec[i]); - ntypes = 0; - if (typevec != NULL) - { - free ((char *)typevec); - typevec = NULL; - typevec_size = 0; - } - - if (success) - { - string_appendn (&decl, "", 1); - return decl.b; - } - else - { - string_delete (&decl); - return NULL; - } -} - -static int -get_count (type, count) - const char **type; - int *count; -{ - if (!isdigit (**type)) - return 0; - *count = **type - '0'; - *type += 1; - /* see flush_repeats in cplus-method.c */ - if (isdigit (**type)) - { - const char *p = *type; - int n = *count; - do - { - n *= 10; - n += *p - '0'; - p += 1; - } - while (isdigit (*p)); - if (*p == '_') - { - *type = p + 1; - *count = n; - } - } - return 1; -} - -/* result will be initialised here; it will be freed on failure */ - -static int -do_type (type, result) - const char **type; - string *result; -{ - int n; - int done; - int non_empty = 0; - int success; - string decl; - const char *remembered_type; - - string_init (&decl); - string_init (result); - - done = 0; - success = 1; - while (success && !done) - { - int member; - switch (**type) - { - case 'P': - *type += 1; - string_prepend (&decl, "*"); - break; - - case 'R': - *type += 1; - string_prepend (&decl, "&"); - break; - - case 'T': - *type += 1; - if (!get_count (type, &n) || n >= ntypes) - success = 0; - else - { - remembered_type = typevec[n]; - type = &remembered_type; - } - break; - - case 'F': - *type += 1; - if (!string_empty (&decl) && decl.b[0] == '*') - { - string_prepend (&decl, "("); - string_append (&decl, ")"); - } - if (!do_args (type, &decl) || **type != '_') - success = 0; - else - *type += 1; - break; - - case 'M': - case 'O': - { - int constp = 0; - int volatilep = 0; - - member = **type == 'M'; - *type += 1; - if (!isdigit (**type)) - { - success = 0; - break; - } - n = 0; - do - { - n *= 10; - n += **type - '0'; - *type += 1; - } - while (isdigit (**type)); - if (strlen (*type) < n) - { - success = 0; - break; - } - string_append (&decl, ")"); - string_prepend (&decl, "::"); - string_prependn (&decl, *type, n); - string_prepend (&decl, "("); - *type += n; - if (member) - { - if (**type == 'C') - { - *type += 1; - constp = 1; - } - if (**type == 'V') - { - *type += 1; - volatilep = 1; - } - if (*(*type)++ != 'F') - { - success = 0; - break; - } - } - if ((member && !do_args (type, &decl)) || **type != '_') - { - success = 0; - break; - } - *type += 1; - if (constp) - { - if (non_empty) - string_append (&decl, " "); - else - non_empty = 1; - string_append (&decl, "const"); - } - if (volatilep) - { - if (non_empty) - string_append (&decl, " "); - else - non_empty = 1; - string_append (&decl, "volatilep"); - } - break; - } - - case 'C': - if ((*type)[1] == 'P') - { - *type += 1; - if (!string_empty (&decl)) - string_prepend (&decl, " "); - string_prepend (&decl, "const"); - break; - } - - /* fall through */ - default: - done = 1; - break; - } - } - - done = 0; - non_empty = 0; - while (success && !done) - { - switch (**type) - { - case 'C': - *type += 1; - if (non_empty) - string_append (result, " "); - else - non_empty = 1; - string_append (result, "const"); - break; - case 'U': - *type += 1; - if (non_empty) - string_append (result, " "); - else - non_empty = 1; - string_append (result, "unsigned"); - break; - case 'V': - *type += 1; - if (non_empty) - string_append (result, " "); - else - non_empty = 1; - string_append (result, "volatile"); - break; - default: - done = 1; - break; - } - } - - if (success) - switch (**type) - { - case '\0': - case '_': - break; - case 'v': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "void"); - break; - case 'x': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "long long"); - break; - case 'l': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "long"); - break; - case 'i': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "int"); - break; - case 's': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "short"); - break; - case 'c': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "char"); - break; - case 'r': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "long double"); - break; - case 'd': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "double"); - break; - case 'f': - *type += 1; - if (non_empty) - string_append (result, " "); - string_append (result, "float"); - break; - case 'G': - *type += 1; - if (!isdigit (**type)) - { - success = 0; - break; - } - /* fall through */ - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - n = 0; - do - { - n *= 10; - n += **type - '0'; - *type += 1; - } - while (isdigit (**type)); - if (strlen (*type) < n) - { - success = 0; - break; - } - if (non_empty) - string_append (result, " "); - string_appendn (result, *type, n); - *type += n; - break; - default: - success = 0; - break; - } - - if (success) - { - if (!string_empty (&decl)) - { - string_append (result, " "); - string_appends (result, &decl); - } - string_delete (&decl); - return 1; - } - else - { - string_delete (&decl); - string_delete (result); - return 0; - } -} - -/* `result' will be initialised in do_type; it will be freed on failure */ - -static int -do_arg (type, result) - const char **type; - string *result; -{ - const char *start = *type; - - if (!do_type (type, result)) - return 0; - remember_type (start, *type - start); - return 1; -} - -static void -remember_type (start, len) - const char *start; - int len; -{ - char *tem; - - if (ntypes >= typevec_size) - { - if (typevec_size == 0) - { - typevec_size = 3; - typevec = (char **) xmalloc (sizeof (char*)*typevec_size); - } - else - { - typevec_size *= 2; - typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size); - } - } - tem = (char *) xmalloc (len + 1); - memcpy (tem, start, len); - tem[len] = '\0'; - typevec[ntypes++] = tem; -} - -/* `decl' must be already initialised, usually non-empty; - it won't be freed on failure */ - -static int -do_args (type, decl) - const char **type; - string *decl; -{ - string arg; - int need_comma = 0; - - string_append (decl, "("); - - while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v') - { - if (**type == 'N') - { - int r; - int t; - *type += 1; - if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes) - return 0; - while (--r >= 0) - { - const char *tem = typevec[t]; - if (need_comma) - string_append (decl, ", "); - if (!do_arg (&tem, &arg)) - return 0; - string_appends (decl, &arg); - string_delete (&arg); - need_comma = 1; - } - } - else - { - if (need_comma) - string_append (decl, ", "); - if (!do_arg (type, &arg)) - return 0; - string_appends (decl, &arg); - string_delete (&arg); - need_comma = 1; - } - } - - if (**type == 'v') - *type += 1; - else if (**type == 'e') - { - *type += 1; - if (need_comma) - string_append (decl, ","); - string_append (decl, "..."); - } - - string_append (decl, ")"); - return 1; -} - -static void -munge_function_name (name) - string *name; -{ - if (!string_empty (name) && name->p - name->b >= 3 - && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == '$') - { - int i; - /* see if it's an assignment expression */ - if (name->p - name->b >= 10 /* op$assign_ */ - && memcmp (name->b + 3, "assign_", 7) == 0) - { - for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++) - { - int len = name->p - name->b - 10; - if (strlen (optable[i].in) == len - && memcmp (optable[i].in, name->b + 10, len) == 0) - { - string_clear (name); - string_append (name, "operator"); - string_append (name, optable[i].out); - string_append (name, "="); - return; - } - } - } - else - { - for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++) - { - int len = name->p - name->b - 3; - if (strlen (optable[i].in) == len - && memcmp (optable[i].in, name->b + 3, len) == 0) - { - string_clear (name); - string_append (name, "operator"); - string_append (name, optable[i].out); - return; - } - } - } - return; - } - else if (!string_empty (name) && name->p - name->b >= 5 - && memcmp (name->b, "type$", 5) == 0) - { - /* type conversion operator */ - string type; - const char *tem = name->b + 5; - if (do_type (&tem, &type)) - { - string_clear (name); - string_append (name, "operator "); - string_appends (name, &type); - string_delete (&type); - return; - } - } -} - -/* a mini string-handling package */ - -static void -string_need (s, n) - string *s; - int n; -{ - if (s->b == NULL) - { - if (n < 32) - n = 32; - s->p = s->b = (char *) xmalloc (n); - s->e = s->b + n; - } - else if (s->e - s->p < n) - { - int tem = s->p - s->b; - n += tem; - n *= 2; - s->b = (char *) xrealloc (s->b, n); - s->p = s->b + tem; - s->e = s->b + n; - } -} - -static void -string_delete (s) - string *s; -{ - if (s->b != NULL) - { - free (s->b); - s->b = s->e = s->p = NULL; - } -} - -static void -string_init (s) - string *s; -{ - s->b = s->p = s->e = NULL; -} - -static void -string_clear (s) - string *s; -{ - s->p = s->b; -} - -static int -string_empty (s) - string *s; -{ - return s->b == s->p; -} - -static void -string_append (p, s) - string *p; - const char *s; -{ - int n; - if (s == NULL || *s == '\0') - return; - n = strlen (s); - string_need (p, n); - memcpy (p->p, s, n); - p->p += n; -} - -static void -string_appends (p, s) - string *p, *s; -{ - int n; - if (s->b == s->p) - return; - n = s->p - s->b; - string_need (p, n); - memcpy (p->p, s->b, n); - p->p += n; -} - -static void -string_appendn (p, s, n) - string *p; - const char *s; - int n; -{ - if (n == 0) - return; - string_need (p, n); - memcpy (p->p, s, n); - p->p += n; -} - -static void -string_prepend (p, s) - string *p; - const char *s; -{ - if (s == NULL || *s == '\0') - return; - string_prependn (p, s, strlen (s)); -} - -#if 0 -static void -string_prepends (p, s) - string *p, *s; -{ - if (s->b == s->p) - return; - string_prependn (p, s->b, s->p - s->b); -} -#endif - -static void -string_prependn (p, s, n) - string *p; - const char *s; - int n; -{ - char *q; - - if (n == 0) - return; - string_need (p, n); - for (q = p->p - 1; q >= p->b; q--) - q[n] = q[0]; - memcpy (p->b, s, n); - p->p += n; -} diff --git a/gnu/usr.bin/ld/etc.c b/gnu/usr.bin/ld/etc.c deleted file mode 100644 index 4d69ee578949..000000000000 --- a/gnu/usr.bin/ld/etc.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * $Id: etc.c,v 1.7 1994/06/10 15:16:04 pk Exp $ - */ - -#include -#include -#include - -/* - * Like malloc but get fatal error if memory is exhausted. - */ -void * -xmalloc(size) - size_t size; -{ - register void *result = (void *)malloc(size); - - if (!result) - errx(1, "virtual memory exhausted"); - - return result; -} - -/* - * Like realloc but get fatal error if memory is exhausted. - */ -void * -xrealloc(ptr, size) - void *ptr; - size_t size; -{ - register void *result; - - if (ptr == NULL) - result = (void *)malloc(size); - else - result = (void *)realloc(ptr, size); - - if (!result) - errx(1, "virtual memory exhausted"); - - return result; -} - -/* - * Return a newly-allocated string whose contents concatenate - * the strings S1, S2, S3. - */ -char * -concat(s1, s2, s3) - const 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; -} - diff --git a/gnu/usr.bin/ld/ld.1 b/gnu/usr.bin/ld/ld.1 deleted file mode 100644 index 0f16b87973c3..000000000000 --- a/gnu/usr.bin/ld/ld.1 +++ /dev/null @@ -1,259 +0,0 @@ -.\" -.\" 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 without 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.11 1996/12/04 00:11:23 perry Exp $ -.\" -.Dd October 14, 1993 -.Dt LD 1 -.Os NetBSD -.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 Ns 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 Ns Ar library-specifier -.Op Fl L Ns Ar library-search-path -.Op Fl nostdlib -.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 symbol-file is taken as a base for link-editing the object files -on the command line. -.It Fl 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 Ns 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 -.Po see the \& -.Fl l -option -.Pc -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 -.Fl B Ns Ar static . -.It Fl B Ns Ar static -The counterpart of -.Fl B Ns Ar dynamic . -This option turns off dynamic linking for -all library specifiers until a -.Fl B Ns Ar dynamic -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 Ns Ar shareable -Instructs the linker to build a shared object from the object files rather -than a normal executable image. -.It Fl B Ns 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 Ns 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 B Ns Ar silly -Search for -.Em \.sa -silly archive companions of shared objects. Useful for compatibility with -version 3 shared objects. -.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 Ns Ar path -Add -.Ar path -to the list of directories to search for libraries specified with the -.Ar -l -option. -.It Fl l Ns Ar lib-spec -This option specifies a library to be considered for inclusion in the -output. If the -.Fl B Ns Ar dynamic -option is in effect, a shared library of the -form lib.so.m.n -.Po where \& -.Em m -is the major, and -.Em n -is the minor version number, respectively -.Pc 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 -.Fl B Ns Ar static -option is in effect, an archive of the form lib.a is looked for in -the library search path. -.It Fl M -Produce output about the mapping of segments of the input files and the -values assigned to -.Pq global -symbols in the output file. -.It Fl N -Produce a -.Dv OMAGIC -output file. -.It Fl n -Produce a -.Dv NMAGIC -output file. -.It Fl nostdlib -Do not search the built-in path -.Po -usually -.Dq /usr/lib -.Pc -for -.Fl l -specified libraries. -.It Fl o Ar filename -Specifies the name of the output file. Defaults to -.Dq a.out . -.It Fl Q -Produce a -.Dv QMAGIC -output file. -.It Fl r -Produce relocatable object file, suitable for another pass through -.Nm ld . -.It Fl R -Record the given path within the executable for run-time libary search. -This only applies to dynamically linked executables. -.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 -.Pq 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 Ar symbol -Trace the manipulations inflicted on -.Ar symbol -.It Fl z -Make a -.Dv ZMAGIC -output file. This is the default. -.Sh ENVIRONMENT -.Nm -utilizes the following environment variables: -.Bl -tag -width "LD_LIBRARY_PATH" -.It Ev LD_LIBRARY_PATH -This colon-separated list of directories is inserted into the search -path for libraries following any directories specified via -.Fl L -options and preceding the built-in path. -.It Ev LD_NOSTD_PATH -When set, do not search the built-in path for libraries. -This is an alternative to the -.Fl nostdlib -command-line flag. -.El -.Sh FILES -.Sh SEE ALSO -.Xr ldconfig 1 , -.Xr ld.so 1 , -.Xr link 5 -.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 BUGS -Shared objects are not properly checked for undefined symbols. -.Pp -Cascading of shared object defeats the -.Dq -Bstatic -option. -.Pp -All shared objects presented to -.Nm ld -are marked for run-time loading in the output file, even if no symbols -are needed from them. -.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 deleted file mode 100644 index 73d4a6e3be43..000000000000 --- a/gnu/usr.bin/ld/ld.c +++ /dev/null @@ -1,3746 +0,0 @@ -/*- - * This code is derived from software copyrighted by the Free Software - * Foundation. - * - * Modified 1991 by Donn Seeley at UUNET Technologies, Inc. - * - * Modified 1993 by Paul Kranenburg, Erasmus University - */ - -#ifndef lint -static char sccsid[] = "@(#)ld.c 6.10 (Berkeley) 5/22/91"; -#endif /* not lint */ - -/* Linker `ld' for GNU - Copyright (C) 1988 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 1, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - 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. */ - -/* - * $Id: ld.c,v 1.50 1997/04/08 23:16:27 pk Exp $ - */ - -/* Define how to initialize system-dependent header fields. */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ld.h" - -/* 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; -int number_of_files; - -/* 1 => write relocation into output file so can re-input it later. */ -int relocatable_output; - -/* 1 => building a shared object, set by `-Bshareable'. */ -int building_shared_object; - -/* 1 => create the output executable. */ -int make_executable; - -/* Force the executable to be output, even if there are non-fatal errors */ -int force_executable; - -/* 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; - -/* 1 => some files contain PIC code, affects relocation bits - if `relocatable_output'. */ -int pic_code_seen; - -/* 1 => segments must be page aligned (ZMAGIC, QMAGIC) */ -int page_align_segments; - -/* 1 => data segment must be page aligned, even if `-n' or `-N' */ -int page_align_data; - -/* 1 => do not use standard library search path */ -int nostdlib; - -/* Version number to put in __DYNAMIC (set by -V) */ -int soversion; - -int text_size; /* total size of text. */ -int text_start; /* start of text */ -int text_pad; /* clear space between text and data */ -int data_size; /* total size of data. */ -int data_start; /* start of data */ -int data_pad; /* part of bss segment as part of data */ - -int bss_size; /* total size of bss. */ -int bss_start; /* start of bss */ - -int text_reloc_size; /* total size of text relocation. */ -int data_reloc_size; /* total size of data relocation. */ - -int rrs_section_type; /* What's in the RRS section */ -int rrs_text_size; /* Size of RRS text additions */ -int rrs_text_start; /* Location of above */ -int rrs_data_size; /* Size of RRS data additions */ -int rrs_data_start; /* Location of above */ - -/* 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; /* start of set element vectors */ -int set_sect_size; /* size of above */ - -int link_mode; /* Current link mode */ -int pic_type; /* PIC type */ - -/* - * 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; /* Output file name. */ -FILE *outstream; /* Output file descriptor. */ -struct exec outheader; /* Output file header. */ -int magic; /* Output file magic. */ -int oldmagic; -int relocatable_output; /* `-r'-ed output */ - -symbol *entry_symbol; /* specified by `-e' */ -int entry_offset; /* program entry if no `-e' given */ - -int page_size; /* Size of a page (machine dependent) */ - -/* - * 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. - */ -symbol **cmdline_references; -int cl_refs_allocated; - -/* - * Which symbols should be stripped (omitted from the output): none, all, or - * debugger symbols. - */ -enum { - STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER -} strip_symbols; - -/* - * 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; - -int global_sym_count; /* # of nlist entries for global symbols */ -int size_sym_count; /* # of N_SIZE nlist entries for output - (relocatable_output only) */ -int local_sym_count; /* # of nlist entries for local symbols. */ -int non_L_local_sym_count; /* # of nlist entries for non-L symbols */ -int debugger_sym_count; /* # of nlist entries for debugger info. */ -int undefined_global_sym_count; /* # of global symbols referenced and - not defined. */ -int undefined_shobj_sym_count; /* # of undefined symbols referenced - by shared objects */ -int multiple_def_count; /* # of multiply defined symbols. */ -int defined_global_sym_count; /* # of defined global symbols. */ -int common_defined_global_count; /* # of common symbols. */ -int undefined_weak_sym_count; /* # of weak symbols referenced and - not defined. */ - -#if notused -int special_sym_count; /* # 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. - */ -#endif -int global_alias_count; /* # of aliased symbols */ -int set_symbol_count; /* # of N_SET* symbols. */ -int set_vector_count; /* # of set vectors in output. */ -int warn_sym_count; /* # of warning symbols encountered. */ -int list_warning_symbols; /* 1 => warning symbols referenced */ - -struct string_list_element *set_element_prefixes; - -int trace_files; /* print names of input files as processed (`-t'). */ -int write_map; /* write a load map (`-M') */ - -/* - * `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; - -/* - * 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; - -/* - * 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; - -long *set_vectors; -int setv_fill_count; - -static void decode_option __P((char *, char *)); -static void decode_command __P((int, char **)); -static int classify_arg __P((char *)); -static void load_symbols __P((void)); -static void enter_global_ref __P((struct localsymbol *, - char *, struct file_entry *)); -static void digest_symbols __P((void)); -static void digest_pass1 __P((void)), digest_pass2 __P((void)); -static void consider_file_section_lengths __P((struct file_entry *)); -static void relocate_file_addresses __P((struct file_entry *)); -static void consider_relocation __P((struct file_entry *, int)); -static void consider_local_symbols __P((struct file_entry *)); -static void perform_relocation __P((char *, int, - struct relocation_info *, int, - struct file_entry *, int)); -static void copy_text __P((struct file_entry *)); -static void copy_data __P((struct file_entry *)); -static void coptxtrel __P((struct file_entry *)); -static void copdatrel __P((struct file_entry *)); -static void write_output __P((void)); -static void write_header __P((void)); -static void write_text __P((void)); -static void write_data __P((void)); -static void write_rel __P((void)); -static void write_syms __P((void)); -static void assign_symbolnums __P((struct file_entry *, int *)); -static void cleanup __P((void)); -static int parse __P((char *, char *, char *)); - - -int -main(argc, argv) - int argc; - char *argv[]; -{ - - /* 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. */ - if (getrlimit(RLIMIT_STACK, &rlim) != 0) - warn("getrlimit"); - else { - rlim.rlim_cur = rlim.rlim_max; - if (setrlimit(RLIMIT_STACK, &rlim) != 0) - warn("setrlimit"); - } - } -#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; - - data_pad = 0; - text_pad = 0; - page_align_segments = 0; - page_align_data = 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; - link_mode = DYNAMIC; -#ifdef SUNOS4 - link_mode |= SILLYARCHIVE; -#endif - soversion = DEFAULT_SOVERSION; - - /* 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; - warn_sym_count = 0; - list_warning_symbols = 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 = (symbol **) - xmalloc(cl_refs_allocated * sizeof(symbol *)); - *cmdline_references = 0; - - /* Completely decode ARGV. */ - decode_command(argc, argv); - - building_shared_object = - (!relocatable_output && (link_mode & SHAREABLE)); - - if (building_shared_object && entry_symbol) { - errx(1,"`-Bshareable' and `-e' options are mutually exclusive"); - } - - /* Create the symbols `etext', `edata' and `end'. */ - symtab_init(relocatable_output); - - /* - * 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. - */ - - if (magic == ZMAGIC || magic == QMAGIC) - page_align_segments = 1; - - md_init_header(&outheader, magic, 0); - - text_size = sizeof(struct exec); - 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(); - - /* - * Print error messages for any missing symbols, for any warning - * symbols, and possibly multiple definitions - */ - make_executable &= do_warnings(stderr); - - /* 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); -} - -/* - * 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. - */ - -static 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 'R': - 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; - } - - return 1; -} - -/* - * Process the command arguments, setting up file_table with an entry for - * each input file, and setting variables according to the options. - */ - -static void -decode_command(argc, argv) - int argc; - char **argv; -{ - register int i; - register struct file_entry *p; - - number_of_files = 0; - output_filename = "a.out"; - - /* - * 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) - errx(1, "no argument following %s", argv[i]); - - decode_option(argv[i], argv[i + 1]); - - if (argv[i][1] == 'l' || argv[i][1] == 'A') - number_of_files++; - - i += code - 1; - } else - number_of_files++; - } - - if (!number_of_files) - errx(1, "no input files"); - - p = file_table = (struct file_entry *) - xmalloc(number_of_files * sizeof(struct file_entry)); - bzero(p, number_of_files * sizeof(struct file_entry)); - - /* Now scan again and fill in file_table. */ - /* All options except -A and -l are ignored here. */ - - for (i = 1; i < argc; i++) { - char *string; - register int code = classify_arg(argv[i]); - - 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; -#ifdef SUN_COMPAT - else if (strcmp(string, "silly") == 0) - link_mode |= SILLYARCHIVE; - else if (strcmp(string, "~silly") == 0) - link_mode &= ~SILLYARCHIVE; -#endif - } - if (argv[i][1] == 'A') { - if (p != file_table) - errx(1, "-A specified before an input file other than the first"); - p->filename = string; - p->local_sym_name = string; - p->flags |= E_JUST_SYMS; - link_mode &= ~DYNAMIC; - p++; - } - if (argv[i][1] == 'l') { - p->filename = string; - p->local_sym_name = concat("-l", string, ""); - p->flags |= E_SEARCH_DIRS; - if (link_mode & DYNAMIC && !relocatable_output) - p->flags |= E_SEARCH_DYNAMIC; - p++; - } - i += code - 1; - } - - /* Now check some option settings for consistency. */ - - if (page_align_segments && - (text_start - text_start_alignment) & (page_size - 1)) - errx(1, "incorrect alignment of text start address"); - - /* Append the standard search directories to the user-specified ones. */ - add_search_path(getenv("LD_LIBRARY_PATH")); - if (!nostdlib && getenv("LD_NOSTD_PATH") == NULL) - std_search_path(); -} - -void -add_cmdline_ref(sp) - symbol *sp; -{ - symbol **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 = (symbol **) - xrealloc(cmdline_references, - cl_refs_allocated * sizeof(symbol *)); - ptr = cmdline_references + diff; - } - *ptr++ = sp; - *ptr = (symbol *)0; -} - -int -set_element_prefixed_p(name) - char *name; -{ - struct string_list_element *p; - int i; - - for (p = set_element_prefixes; p; p = p->next) { - - for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++); - if (p->str[i] == '\0') - return 1; - } - return 0; -} - -/* - * 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. - */ - -static void -decode_option(swt, arg) - register char *swt, *arg; -{ - 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; -#ifdef SUN_COMPAT - if (!strcmp(swt + 1, "Bsilly")) - return; -#endif - if (!strcmp(swt + 1, "Ttext")) { - text_start = parse(arg, "%x", "invalid argument to -Ttext"); - T_flag_specified = 1; - return; - } - 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 (!strcmp(swt + 1, "nostdlib")) { - nostdlib = 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 (swt[2] == 0 || *arg == 'c') - force_common_definition = 1; - else if (*arg == 'p') - force_alias_definition = 1; - else - errx(1, "-d option takes 'c' or 'p' argument"); - return; - - case 'e': - entry_symbol = getsym(arg); - if (!entry_symbol->defined && - !(entry_symbol->flags & GS_REFERENCED)) - undefined_global_sym_count++; - entry_symbol->flags |= GS_REFERENCED; - add_cmdline_ref(entry_symbol); - return; - - case 'l': - return; - - case 'L': - add_search_dir(arg); - return; - - case 'R': - rrs_search_paths = (rrs_search_paths == NULL) - ? strdup(arg) - : concat(rrs_search_paths, ":", arg); - return; - - case 'M': - write_map = 1; - return; - - case 'N': - magic = OMAGIC; - return; - - case 'n': - magic = NMAGIC; - return; - - case 'o': - output_filename = arg; - return; - - case 'p': - page_align_data = 1; - return; - -#ifdef QMAGIC - case 'Q': - magic = QMAGIC; - return; -#endif - - 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->flags & GS_REFERENCED)) - undefined_global_sym_count++; - sp->flags |= GS_REFERENCED; - add_cmdline_ref(sp); - } - return; - -#if 1 - case 'V': - soversion = parse(arg, "%d", "invalid argument to -V"); - return; -#endif - - case 'X': - discard_locals = DISCARD_L; - return; - - case 'x': - discard_locals = DISCARD_ALL; - return; - - case 'y': - { - register symbol *sp = getsym(&swt[2]); - sp->flags |= GS_TRACE; - } - return; - - case 'z': - magic = ZMAGIC; - oldmagic = 0; -#ifdef FreeBSD - netzmagic = 1; -#endif - return; - - case 'Z': - magic = oldmagic = ZMAGIC; -#ifdef FreeBSD - netzmagic = 0; -#endif - return; - - default: - errx(1, "invalid command option `%s'", swt); - } -} - -/* Convenient functions for operating on one or all files being loaded. */ - -/* - * 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 void *arg; -{ - register int i; - - for (i = 0; i < number_of_files; i++) { - register struct file_entry *entry = &file_table[i]; - register struct file_entry *subentry; - - if (entry->flags & E_SCRAPPED) - continue; - - if (!(entry->flags & E_IS_LIBRARY)) - (*function)(entry, arg); - - subentry = entry->subfiles; - for (; subentry; subentry = subentry->chain) { - if (subentry->flags & E_SCRAPPED) - continue; - (*function)(subentry, arg); - } - -#ifdef SUN_COMPAT - if (entry->silly_archive) { - - if (!(entry->flags & E_DYNAMIC)) - warnx("Silly"); - - if (!(entry->silly_archive->flags & E_IS_LIBRARY)) - warnx("Sillier"); - - subentry = entry->silly_archive->subfiles; - for (; subentry; subentry = subentry->chain) { - if (subentry->flags & E_SCRAPPED) - continue; - (*function)(subentry, arg); - } - } -#endif - } -} - -/* - * 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 void *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->flags & E_SCRAPPED) - continue; - if (entry->flags & E_IS_LIBRARY) { - register struct file_entry *subentry = entry->subfiles; - for (; subentry; subentry = subentry->chain) { - if (subentry->flags & E_SCRAPPED) - continue; - if (return_val = (*function)(subentry, arg)) - return return_val; - } - } else if (return_val = (*function)(entry, arg)) - return return_val; - } - return 0; -} - -/* Like `each_file' but ignore files that were just for symbol definitions. */ - -void -each_full_file(function, arg) - register void (*function)(); - register void *arg; -{ - register int i; - - for (i = 0; i < number_of_files; i++) { - register struct file_entry *entry = &file_table[i]; - register struct file_entry *subentry; - - if (entry->flags & (E_SCRAPPED | E_JUST_SYMS)) - continue; - -#ifdef SUN_COMPAT - if (entry->silly_archive) { - - if (!(entry->flags & E_DYNAMIC)) - warnx("Silly"); - - if (!(entry->silly_archive->flags & E_IS_LIBRARY)) - warnx("Sillier"); - - subentry = entry->silly_archive->subfiles; - for (; subentry; subentry = subentry->chain) { - if (subentry->flags & E_SCRAPPED) - continue; - (*function)(subentry, arg); - } - } -#endif - if (entry->flags & E_DYNAMIC) - continue; - - if (!(entry->flags & E_IS_LIBRARY)) - (*function)(entry, arg); - - subentry = entry->subfiles; - for (; subentry; subentry = subentry->chain) { - if (subentry->flags & E_SCRAPPED) - continue; - (*function)(subentry, arg); - } - - } -} - -/* Close the input file that is now open. */ - -void -file_close() -{ - 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. - */ -int -file_open(entry) - register struct file_entry *entry; -{ - register int fd; - - if (entry->superfile && (entry->superfile->flags & E_IS_LIBRARY)) - return file_open(entry->superfile); - - if (entry == input_file) - return input_desc; - - if (input_file) - file_close(); - - if (entry->flags & E_SEARCH_DIRS) { - fd = findlib(entry); - } else - fd = open(entry->filename, O_RDONLY, 0); - - if (fd > 0) { - input_file = entry; - input_desc = fd; - return fd; - } - - if (entry->flags & E_SEARCH_DIRS) - errx(1, "%s: no match", entry->local_sym_name); - else - err(1, "%s", entry->filename); - return fd; -} - -int -text_offset(entry) - struct file_entry *entry; -{ - return entry->starting_offset + N_TXTOFF (entry->header); -} - -/*---------------------------------------------------------------------------*/ - -/* - * Read a file's header into the proper place in the file_entry. FD is the - * descriptor on which the file is open. ENTRY is the file's entry. - */ -void -read_header(fd, entry) - int fd; - struct file_entry *entry; -{ - register int len; - - if (lseek(fd, entry->starting_offset, L_SET) != - entry->starting_offset) - err(1, "%s: read_header: lseek", get_file_name(entry)); - - len = read(fd, &entry->header, sizeof(struct exec)); - if (len != sizeof (struct exec)) - err(1, "%s: read_header: read", get_file_name(entry)); - - md_swapin_exec_hdr(&entry->header); - - if (N_BADMAG (entry->header)) - errx(1, "%s: bad magic", get_file_name(entry)); - - if (N_BADMID(entry->header)) - errx(1, "%s: non-native input file", get_file_name(entry)); - - entry->flags |= E_HEADER_VALID; -} - -/* - * Read the symbols of file ENTRY into core. Assume it is already open, on - * descriptor FD. 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(fd, entry) - struct file_entry *entry; - int fd; -{ - int str_size; - struct nlist *np; - int i; - - if (!(entry->flags & E_HEADER_VALID)) - read_header(fd, entry); - - np = (struct nlist *)alloca(entry->header.a_syms); - entry->nsymbols = entry->header.a_syms / sizeof(struct nlist); - if (entry->nsymbols == 0) - return; - - entry->symbols = (struct localsymbol *) - xmalloc(entry->nsymbols * sizeof(struct localsymbol)); - - if (lseek(fd, N_SYMOFF(entry->header) + entry->starting_offset, L_SET) - != N_SYMOFF(entry->header) + entry->starting_offset) - err(1, "%s: read_symbols: lseek(syms)", get_file_name(entry)); - - if (entry->header.a_syms != read(fd, np, entry->header.a_syms)) - errx(1, "%s: read_symbols: premature end of file in symbols", - get_file_name(entry)); - - md_swapin_symbols(np, entry->header.a_syms / sizeof(struct nlist)); - - 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].entry = entry; - entry->symbols[i].gotslot_offset = -1; - entry->symbols[i].flags = 0; - } - - entry->strings_offset = N_STROFF(entry->header) + - entry->starting_offset; - if (lseek(fd, entry->strings_offset, 0) == (off_t)-1) - err(1, "%s: read_symbols: lseek(strings)", - get_file_name(entry)); - if (sizeof str_size != read(fd, &str_size, sizeof str_size)) - errx(1, "%s: read_symbols: cannot read string table size", - get_file_name(entry)); - - entry->string_size = md_swap_long(str_size); -} - -/* - * Read the string table of file ENTRY open on descriptor FD, into core. - */ -void -read_entry_strings(fd, entry) - struct file_entry *entry; - int fd; -{ - - if (entry->string_size == 0) - return; - - if (!(entry->flags & E_HEADER_VALID) || !entry->strings_offset) - errx(1, "%s: read_strings: string table unavailable", - get_file_name(entry)); - - if (lseek(fd, entry->strings_offset, L_SET) != - entry->strings_offset) - err(1, "%s: read_strings: lseek", - get_file_name(entry)); - - if (read(fd, entry->strings, entry->string_size) != - entry->string_size) - errx(1, "%s: read_strings: premature end of file in strings", - get_file_name(entry)); - - return; -} - -/* Read in the relocation sections of ENTRY if necessary */ - -void -read_entry_relocation(fd, entry) - int fd; - struct file_entry *entry; -{ - register struct relocation_info *reloc; - off_t pos; - - if (!entry->textrel) { - - reloc = (struct relocation_info *) - xmalloc(entry->header.a_trsize); - - pos = text_offset(entry) + - entry->header.a_text + entry->header.a_data; - - if (lseek(fd, pos, L_SET) != pos) - err(1, "%s: read_reloc(text): lseek", - get_file_name(entry)); - - if (read(fd, reloc, entry->header.a_trsize) != - entry->header.a_trsize) - errx(1, "%s: read_reloc(text): premature EOF", - get_file_name(entry)); - - md_swapin_reloc(reloc, entry->header.a_trsize / sizeof(*reloc)); - entry->textrel = reloc; - entry->ntextrel = entry->header.a_trsize / sizeof(*reloc); - - } - - if (!entry->datarel) { - - reloc = (struct relocation_info *) - xmalloc(entry->header.a_drsize); - - pos = text_offset(entry) + entry->header.a_text + - entry->header.a_data + entry->header.a_trsize; - - if (lseek(fd, pos, L_SET) != pos) - err(1, "%s: read_reloc(data): lseek", - get_file_name(entry)); - - if (read(fd, reloc, entry->header.a_drsize) != - entry->header.a_drsize) - errx(1, "%s: read_reloc(data): premature EOF", - get_file_name(entry)); - - md_swapin_reloc(reloc, entry->header.a_drsize / sizeof(*reloc)); - entry->datarel = reloc; - entry->ndatarel = entry->header.a_drsize / sizeof(*reloc); - - } -} - -/*---------------------------------------------------------------------------*/ - -/* - * Read in the symbols of all input files. - */ -static void -load_symbols() -{ - register int i; - - if (trace_files) - fprintf(stderr, "Loading symbols:\n\n"); - - for (i = 0; i < number_of_files; i++) - read_file_symbols(&file_table[i]); - - if (trace_files) - fprintf(stderr, "\n"); -} - -/* - * 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 -read_file_symbols(entry) - register struct file_entry *entry; -{ - register int fd; - register int len; - struct exec hdr; - - fd = file_open(entry); - - len = read(fd, &hdr, sizeof hdr); - if (len != sizeof hdr) - errx(1, "%s: read_file_symbols(header): premature EOF", - get_file_name(entry)); - - md_swapin_exec_hdr(&hdr); - - if (!N_BADMAG (hdr)) { - if (N_IS_DYNAMIC(hdr) && !(entry->flags & E_JUST_SYMS)) { - if (relocatable_output) { - errx(1, - "%s: -r and shared objects currently not supported", - get_file_name(entry)); - return; - } -#if notyet /* Compatibility */ - if (!(N_GETFLAG(hdr) & EX_PIC)) - warnx("%s: EX_PIC not set", - get_file_name(entry)); -#endif - entry->flags |= E_DYNAMIC; - if (entry->superfile || rrs_add_shobj(entry)) - read_shared_object(fd, entry); - else - entry->flags |= E_SCRAPPED; - } else { - if (N_GETFLAG(hdr) & EX_PIC) - pic_code_seen = 1; - read_entry_symbols(fd, entry); - entry->strings = (char *)alloca(entry->string_size); - read_entry_strings(fd, entry); - read_entry_relocation(fd, entry); - enter_file_symbols(entry); - entry->strings = 0; - } - } else { - char armag[SARMAG]; - - lseek (fd, 0, 0); - if (SARMAG != read(fd, armag, SARMAG) || - strncmp (armag, ARMAG, SARMAG)) - errx(1, - "%s: malformed input file (not rel or archive)", - get_file_name(entry)); - entry->flags |= E_IS_LIBRARY; - search_library(fd, entry); - } - - file_close(); -} - - -/* - * Enter the external symbol defs and refs of ENTRY in the hash table. - */ - -void -enter_file_symbols(entry) - struct file_entry *entry; -{ - 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 *msg = p->n_un.n_strx + entry->strings; - - /* Grab the next entry. */ - lsp++; - p = &lsp->nzlist.nlist; - if (p->n_type != (N_UNDF | N_EXT)) { - warnx( - "%s: Warning symbol without external reference following.", - get_file_name(entry)); - make_executable = 0; - lsp--; /* Process normally. */ - } else { - symbol *sp; - char *name = p->n_un.n_strx + entry->strings; - /* Deal with the warning symbol. */ - lsp->flags |= LS_WARNING; - enter_global_ref(lsp, name, entry); - sp = getsym(name); - if (sp->warning == NULL) { - sp->warning = (char *) - xmalloc(strlen(msg)+1); - strcpy(sp->warning, msg); - warn_sym_count++; - } else if (strcmp(sp->warning, msg)) - warnx( - "%s: multiple definitions for warning symbol `%s'", - get_file_name(entry), sp->name); - } - } 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_un.n_strx + entry->strings)[0] == LPREFIX) - lsp->flags |= LS_L_SYMBOL; - } - -} - -/* - * 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 `soref'. For such symbols - * that's all we do at this stage, with the exception of the case where the - * symbol is a common. The `referenced' bit is only set for references from - * relocatable objects. - * - */ - -static void -enter_global_ref(lsp, name, entry) - struct localsymbol *lsp; - char *name; - struct file_entry *entry; -{ - register struct nzlist *nzp = &lsp->nzlist; - register symbol *sp = getsym(name); - register int type = nzp->nz_type; - int oldref = (sp->flags & GS_REFERENCED); - int olddef = sp->defined; - int com = sp->defined && sp->common_size; - - if (type == (N_INDR | N_EXT) && !olddef) { - sp->alias = getsym(entry->strings + (lsp + 1)->nzlist.nz_strx); - if (sp == sp->alias) { - warnx("%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 { - if ((entry->flags & E_DYNAMIC) == 0) - global_alias_count++; - } -#if 0 - if (sp->flags & GS_REFERENCED) - sp->alias->flags |= GS_REFERENCED; -#endif - } - - if (entry->flags & E_DYNAMIC) { - lsp->next = sp->sorefs; - sp->sorefs = lsp; - lsp->symbol = sp; - - /* - * 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->common_size = nzp->nz_value; - sp->defined = N_UNDF | N_EXT; - } else if (com && sp->common_size < nzp->nz_value) { - sp->common_size = nzp->nz_value; - } - } else if (type != (N_UNDF | N_EXT) && !oldref) { - /* - * This is an ex common... - */ - if (com) - common_defined_global_count--; - sp->common_size = 0; - if (sp != dynamic_symbol) - sp->defined = 0; - } - - /* - * Handle size information in shared objects. - */ - if (nzp->nz_size > sp->size) - sp->size = nzp->nz_size; - - if ((lsp->flags & LS_WARNING) && (sp->flags & GS_REFERENCED)) - /* - * Prevent warning symbols from getting - * gratuitously referenced. - */ - list_warning_symbols = 1; - return; - } - - lsp->next = sp->refs; - sp->refs = lsp; - lsp->symbol = sp; - - if (lsp->flags & LS_WARNING) { - /* - * Prevent warning symbols from getting - * gratuitously referenced. - */ - if (sp->flags & GS_REFERENCED) - list_warning_symbols = 1; - return; - } - - if (sp->warning) - list_warning_symbols = 1; - - sp->flags |= GS_REFERENCED; - - if (sp == dynamic_symbol || sp == got_symbol) { - if (type != (N_UNDF | N_EXT) && !(entry->flags & E_JUST_SYMS)) - errx(1,"Linker reserved symbol %s defined as type %x ", - name, type); - return; - } - - if (olddef && N_ISWEAK(&nzp->nlist) && !(sp->flags & GS_WEAK)) { -#ifdef DEBUG - printf("%s: not overridden by weak symbol from %s\n", - sp->name, get_file_name(entry)); -#endif - return; - } - - if (type == (N_SIZE | N_EXT)) { - - if (relocatable_output && nzp->nz_value != 0 && sp->size == 0) - size_sym_count++; - if (sp->size < nzp->nz_value) - sp->size = nzp->nz_value; - - } else if (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 ((sp->flags & GS_WEAK) && !N_ISWEAK(&nzp->nlist)) { - /* - * Upgrade an existing weak definition. - * We fake it by pretending the symbol is undefined; - * must undo any common fiddling, however. - */ - if (!oldref) - errx(1, "internal error: enter_glob_ref: " - "weak symbol not referenced"); - if (!olddef && !com) - undefined_weak_sym_count--; - undefined_global_sym_count++; - sp->defined = type; - sp->flags &= ~GS_WEAK; - olddef = 0; - if (com) - common_defined_global_count--; - com = 0; - sp->common_size = 0; - } - if (oldref && !olddef) { - /* - * It used to be undefined and we're defining it. - */ - undefined_global_sym_count--; - if (sp->flags & GS_WEAK) - /* Used to be a weak reference */ - undefined_weak_sym_count--; - if (undefined_global_sym_count < 0 || - undefined_weak_sym_count < 0) - errx(1, "internal error: enter_glob_ref: " - "undefined_global_sym_count = %d, " - "undefined_weak_sym_count = %d", - undefined_global_sym_count, - undefined_weak_sym_count); - - } - - if (N_ISWEAK(&nzp->nlist)) - /* The definition is weak */ - sp->flags |= GS_WEAK; - - if (!olddef && type == (N_UNDF | N_EXT) && nzp->nz_value) { - /* - * First definition and it's common. - */ - common_defined_global_count++; - sp->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->common_size = 0; - } else if (com && type == (N_UNDF | N_EXT) && - sp->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->common_size = nzp->nz_value; - - if (SET_ELEMENT_P(type) && (!olddef || com)) - set_vector_count++; - - } else if (!oldref && !com) { - /* - * An unreferenced symbol can already be defined - * as common by shared objects. - */ - undefined_global_sym_count++; - if (N_ISWEAK(&nzp->nlist)) { - /* The reference is weak */ - sp->flags |= GS_WEAK; - undefined_weak_sym_count++; - } - } - - if (sp == end_symbol && (entry->flags & E_JUST_SYMS) && - !T_flag_specified) - text_start = nzp->nz_value; - - if (sp->flags & GS_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; - - case N_INDR: - reftype = "alias"; - break; - - case N_SIZE: - reftype = "size spec"; - break; - - default: - reftype = "I don't know this type"; - break; - } - - fprintf(stderr, "symbol %s %s%s in ", sp->name, - (N_ISWEAK(&nzp->nlist))?"weakly ":"", reftype); - print_file_name (entry, stderr); - fprintf(stderr, "\n"); - } -} - -/* - * This returns 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, 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; -} - - -/* - * 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 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. - * - */ - -static void -digest_symbols() -{ - - 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 = (set_symbol_count + 2 * set_vector_count) * - sizeof (unsigned long); - set_vectors = (long *)xmalloc (set_sect_size); - setv_fill_count = 0; - } - - /* Pass 1: check and define symbols */ - defined_global_sym_count = 0; - digest_pass1(); - - each_full_file(consider_relocation, (void *)0); /* Text */ - each_full_file(consider_relocation, (void *)1); /* Data */ - - each_file(consider_local_symbols, (void *)0); - - /* - * 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 (page_align_segments || page_align_data) { - text_pad = PALIGN(text_size, page_size) - text_size; - text_size += text_pad; - } - outheader.a_text = text_size; - - /* - * Make the data segment address start in memory on a suitable - * boundary. - */ - - if (!Tdata_flag_specified) - rrs_data_start = text_start + - DATA_START(outheader) - TEXT_START(outheader); - - data_start = rrs_data_start + rrs_data_size; - if (!relocatable_output) { - set_sect_start = rrs_data_start + data_size; - data_size += MALIGN(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); -printf("set_sect_start = %#x, set_sect_size = %#x\n", - set_sect_start, set_sect_size); -#endif - - /* Compute start addresses of each file's sections and symbols. */ - - each_full_file(relocate_file_addresses, 0); - relocate_rrs_addresses(); - - /* Pass 2: assign values to symbols */ - digest_pass2(); - - 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; - } - /* - * 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 (page_align_segments) - data_pad = PALIGN(data_pad + data_size, page_size) - data_size; - - bss_size -= data_pad; - if (bss_size < 0) - bss_size = 0; - - data_size += data_pad; - - /* - * Calculate total number of symbols that will go into - * the output symbol table (barring DISCARD_* settings). - */ - global_sym_count = defined_global_sym_count + - undefined_global_sym_count; - - if (dynamic_symbol->flags & GS_REFERENCED) - global_sym_count++; - - if (got_symbol->flags & GS_REFERENCED) - global_sym_count++; - - if (relocatable_output || building_shared_object) { - /* For each alias we write out two struct nlists */ - global_sym_count += global_alias_count; - /* Propagate warning symbols; costs two extra struct nlists */ - global_sym_count += 2 * warn_sym_count; - } - - if (relocatable_output) - /* We write out the original N_SIZE symbols */ - global_sym_count += size_sym_count; - -#ifdef DEBUG -printf( -"global symbols %d " -"(defined %d, undefined %d, weak %d, aliases %d, warnings 2 * %d, " -"size symbols %d)\ncommons %d, locals: %d, debug symbols: %d, set_symbols %d\n", - global_sym_count, - defined_global_sym_count, undefined_global_sym_count, - undefined_weak_sym_count, - global_alias_count, warn_sym_count, size_sym_count, - common_defined_global_count, local_sym_count, - debugger_sym_count, set_symbol_count); -#endif -} - -/* - * Determine the definition of each global symbol. - */ -static void -digest_pass1() -{ - - /* - * 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) { - symbol *spsave; - struct localsymbol *lsp; - int defs = 0; - - if (!(sp->flags & GS_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 - - /* Superfluous symbol from shared object */ - continue; - } - if (sp->so_defined) - /* Already examined; must have been an alias */ - continue; - - 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) - errx(1, - "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->mult_defs = 1; - multiple_def_count++; - } - /* Keep count and remember symbol */ - sp->setv_count++; - set_vectors[setv_fill_count++] = (long)p; - if (building_shared_object) { - struct relocation_info reloc; - - /* - * Make sure to relocate the contents - * of this set vector. - */ - bzero(&reloc, sizeof(reloc)); - RELOC_INIT_SEGMENT_RELOC(&reloc); - RELOC_ADDRESS(&reloc) = - setv_fill_count * sizeof(long); - alloc_rrs_segment_reloc(NULL, &reloc); - } - - } else if ((type & N_EXT) && type != (N_UNDF | N_EXT) - && (type & N_TYPE) != N_FN - && (type & N_TYPE) != N_SIZE) { - /* non-common definition */ - if (!N_ISWEAK(p)) - ++defs; - if (defs > 1) { - sp->mult_defs = 1; - multiple_def_count++; - } else if (!N_ISWEAK(p) || - (!sp->def_lsp && !sp->common_size)) { - sp->def_lsp = lsp; - lsp->entry->flags |= E_SYMBOLS_USED; - sp->defined = type; - sp->aux = N_AUX(p); - } - } - } - - /* - * If this symbol has acquired final definition, we're done. - * Commons must be allowed to bind to shared object data - * definitions. - */ - if (sp->defined && - (sp->common_size == 0 || - relocatable_output || building_shared_object)) { - if ((sp->defined & N_TYPE) == N_SETV) - /* Allocate zero entry in set vector */ - setv_fill_count++; - /* - * At this stage, we do not know whether an alias - * is going to be defined for real here, or whether - * it refers to a shared object symbol. The decision - * is deferred until digest_pass2(). - */ - if (!sp->alias) - defined_global_sym_count++; - continue; - } - - 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--; - if (undefined_global_sym_count < 0) - errx(1, - "internal error: digest_pass1,1: %s: undefined_global_sym_count = %d", - sp->name, undefined_global_sym_count); - continue; - } - - spsave=sp; /*XXX*/ - again: - 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 */ - if (sp->common_size) { - /* - * This common has an so defn; switch - * to it iff defn is: data, first-class - * and not weak. - */ - if (N_AUX(p) != AUX_OBJECT || - N_ISWEAK(p) || - (lsp->entry->flags & E_SECONDCLASS)) - continue; - - /* - * Change common to so ref. First, - * downgrade common to undefined. - */ - sp->common_size = 0; - sp->defined = 0; - common_defined_global_count--; - undefined_global_sym_count++; - } - - sp->def_lsp = lsp; - sp->so_defined = type; - sp->aux = N_AUX(p); - if (lsp->entry->flags & E_SECONDCLASS) - /* Keep looking for something better */ - continue; - if (N_ISWEAK(p)) - /* Keep looking for something better */ - continue; - break; - } - } - if (sp->def_lsp) { -#ifdef DEBUG -printf("pass1: SO definition for %s, type %x in %s at %#x\n", - sp->name, sp->so_defined, get_file_name(sp->def_lsp->entry), - sp->def_lsp->nzlist.nz_value); -#endif - sp->def_lsp->entry->flags |= E_SYMBOLS_USED; - if (sp->flags & GS_REFERENCED) { - undefined_global_sym_count--; - } else - sp->flags |= GS_REFERENCED; - if (undefined_global_sym_count < 0) - errx(1, "internal error: digest_pass1,2: " - "%s: undefined_global_sym_count = %d", - sp->name, undefined_global_sym_count); - if (sp->alias && - !(sp->alias->flags & GS_REFERENCED)) { - sp = sp->alias; - goto again; - } - } else if (sp->defined) { - if (sp->common_size == 0) - errx(1, "internal error: digest_pass1,3: " - "%s: not a common: %x", - sp->name, sp->defined); - /* - * Common not bound to shared object data; treat - * it now like other defined symbols were above. - */ - if (!sp->alias) - defined_global_sym_count++; - } - sp=spsave; /*XXX*/ - } END_EACH_SYMBOL; - - if (setv_fill_count != set_sect_size/sizeof(long)) - errx(1, "internal error: allocated set symbol space (%d) " - "doesn't match actual (%d)", - set_sect_size/sizeof(long), setv_fill_count); -} - - -/* - * Scan relocation info in ENTRY for contributions to the RRS section - * of the output file. - */ -static 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; - } - - for (; reloc < end; reloc++) { - - if (relocatable_output) { - lsp = &entry->symbols[reloc->r_symbolnum]; - if (RELOC_BASEREL_P(reloc)) { - pic_code_seen = 1; /* Compatibility */ - if (!RELOC_EXTERN_P(reloc)) - lsp->flags |= LS_RENAME; - } - continue; - } - - /* - * 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; - if (sp->flags & GS_TRACE) { - fprintf(stderr, "symbol %s has jmpslot in %s\n", - sp->name, get_file_name(entry)); - } - alloc_rrs_jmpslot(entry, sp); - - } else if (RELOC_BASEREL_P(reloc)) { - - lsp = &entry->symbols[reloc->r_symbolnum]; - alloc_rrs_gotslot(entry, reloc, lsp); - if (pic_type != PIC_TYPE_NONE && - RELOC_PIC_TYPE(reloc) != pic_type) - errx(1, "%s: illegal reloc type mix", - get_file_name(entry)); - pic_type = RELOC_PIC_TYPE(reloc); - - } 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) - errx(1, "%s: bogus relocation record", - get_file_name(entry)); - - if (sp->alias) - sp = sp->alias; - - /* - * Skip refs to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC - */ - if (sp == got_symbol) { - if (!CHECK_GOT_RELOC(reloc)) - errx(1, - "%s: Unexpected relocation type for GOT symbol", - get_file_name(entry)); - continue; - } - - /* - * This symbol gives rise to a RRS entry - */ - - if (building_shared_object) { - if (sp->flags & GS_TRACE) { - fprintf(stderr, - "symbol %s RRS entry in %s\n", - sp->name, get_file_name(entry)); - } - alloc_rrs_reloc(entry, sp); - continue; - } - - if (force_alias_definition && sp->so_defined && - sp->aux == AUX_FUNC) { - - /* Call to shared library procedure */ - alloc_rrs_jmpslot(entry, sp); - - } else if (sp->size && sp->so_defined && - sp->aux == AUX_OBJECT) { - - /* Reference to shared library data */ - alloc_rrs_cpy_reloc(entry, sp); - sp->defined = N_SIZE; - - } else if (!sp->defined && sp->common_size == 0 && - sp->so_defined) - alloc_rrs_reloc(entry, sp); - - } else { - /* - * Segment relocation. - * Prepare an RRS relocation as these are load - * address dependent. - */ - if (building_shared_object && !RELOC_PCREL_P(reloc)) { - alloc_rrs_segment_reloc(entry, reloc); - } - } - } -} - -/* - * Determine the disposition of each local symbol. - */ -static void -consider_local_symbols(entry) - register struct file_entry *entry; -{ - register struct localsymbol *lsp, *lspend; - - if (entry->flags & E_DYNAMIC) - return; - - lspend = entry->symbols + entry->nsymbols; - - /* - * For each symbol determine whether it should go - * in the output symbol table. - */ - - for (lsp = entry->symbols; lsp < lspend; lsp++) { - register struct nlist *p = &lsp->nzlist.nlist; - register int type = p->n_type; - - if (type == N_WARNING) - continue; - - if (SET_ELEMENT_P (type)) { - /* - * This occurs even if global. These types of - * symbols are never written globally, though - * they are stored globally. - */ - if (relocatable_output) - lsp->flags |= LS_WRITE; - - } else if (!(type & (N_STAB | N_EXT))) { - - /* - * Ordinary local symbol - */ - if ((lsp->flags & LS_RENAME) || ( - discard_locals != DISCARD_ALL && - !(discard_locals == DISCARD_L && - (lsp->flags & LS_L_SYMBOL))) ) { - - lsp->flags |= LS_WRITE; - local_sym_count++; - } - - } else if (!(type & N_EXT)) { - - /* - * Debugger symbol - */ - if (strip_symbols == STRIP_NONE) { - lsp->flags |= LS_WRITE; - debugger_sym_count++; - } - } - } - - /* - * Count one for the local symbol that we generate, - * whose name is the file's name (usually) and whose address - * is the start of the file's text. - */ - if (discard_locals != DISCARD_ALL) - local_sym_count++; -} - -/* - * Accumulate the section sizes of input file ENTRY into the section sizes of - * the output file. - */ -static void -consider_file_section_lengths(entry) - register struct file_entry *entry; -{ - - 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 += MALIGN(entry->header.a_bss); - - text_reloc_size += entry->header.a_trsize; - data_reloc_size += entry->header.a_drsize; -} - -/* - * 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. - */ -static void -relocate_file_addresses(entry) - register struct file_entry *entry; -{ - 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 - - 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; - - /* - * If this belongs to a section, update it - * by the section's start address - */ - - switch (type & N_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; - } - - } - -} - -/* - * Assign a value to each global symbol. - */ -static void -digest_pass2() -{ - FOR_EACH_SYMBOL(i, sp) { - int size; - int align = sizeof(int); - - if (!(sp->flags & GS_REFERENCED)) - continue; - - if (sp->alias && - (relocatable_output || building_shared_object || - (sp->alias->defined && !sp->alias->so_defined))) { - /* - * The alias points at a defined symbol, so it - * must itself be counted as one too, in order to - * compute the correct number of symbol table entries. - */ - if (!sp->defined) { - if (building_shared_object && - !sp->alias->defined) - /* Exclude aliases in shared objects */ - continue; - /* - * Change aliased symbol's definition too. - * These things happen if shared object commons - * or data is going into our symbol table. - */ - if (sp->so_defined != (N_INDR+N_EXT)) - warnx( "pass2: %s: alias isn't", - sp->name); - sp->defined = sp->so_defined; - sp->so_defined = 0; - } - defined_global_sym_count++; - } - - 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, *p, *q; - 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; - if (building_shared_object) { - struct relocation_info reloc; - - bzero(&reloc, sizeof(reloc)); - RELOC_INIT_SEGMENT_RELOC(&reloc); - RELOC_ADDRESS(&reloc) = - (1 + i + length_word_index) * - sizeof(long) - + set_sect_start; - RELOC_TYPE(&reloc) = - (p->n_type - (N_SETA - N_ABS)) & N_TYPE; - claim_rrs_segment_reloc(NULL, &reloc); - } - } - - /* - * Reverse the vector. - */ - p = &set_vectors[length_word_index + 1]; - q = &set_vectors[length_word_index + sp->setv_count]; - while (p < q) { - unsigned long tmp = *p; - *p++ = *q; - *q-- = tmp; - } - - /* Clear terminating entry */ - set_vectors[length_word_index + sp->setv_count + 1] = 0; - continue; - } - - if (sp->def_lsp) { - if (sp->defined && (sp->defined & ~N_EXT) != N_SETV) - sp->value = sp->def_lsp->nzlist.nz_value; - if (sp->so_defined && - (sp->def_lsp->entry->flags & E_SECONDCLASS)) - /* Flag second-hand definitions */ - undefined_global_sym_count++; - if (sp->flags & GS_TRACE) - printf("symbol %s assigned to location %#x\n", - sp->name, sp->value); - } - - /* - * If not -r'ing, allocate common symbols in the BSS section. - */ - if (building_shared_object && !(link_mode & SYMBOLIC)) - /* No common allocation in shared objects */ - continue; - - if ((size = sp->common_size) != 0) { - /* - * It's a common. - */ - if (sp->defined != (N_UNDF + N_EXT)) - errx(1, "%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) - errx(1, "%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; - } - - /* - * 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). - */ - - size = PALIGN(size, sizeof(int)); - - while (align < MAX_ALIGNMENT && !(size & align)) - align <<= 1; - - bss_size = PALIGN(bss_size + data_size + rrs_data_start, align) - - (data_size + rrs_data_start); - - 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); - - } END_EACH_SYMBOL; -} - - -/* -------------------------------------------------------------------*/ - -/* Write the output file */ -void -write_output() -{ - struct stat statbuf; - int filemode; - mode_t u_mask; - - if (lstat(output_filename, &statbuf) == 0) { - if (S_ISREG(statbuf.st_mode)) - (void)unlink(output_filename); - } - - u_mask = umask(0); - (void)umask(u_mask); - - outstream = fopen(output_filename, "w"); - if (outstream == NULL) - err(1, "open: %s", output_filename); - - if (atexit(cleanup)) - err(1, "atexit"); - - if (fstat(fileno(outstream), &statbuf) < 0) - err(1, "fstat: %s", output_filename); - - filemode = statbuf.st_mode; - - if (S_ISREG(statbuf.st_mode) && - chmod(output_filename, filemode & ~0111) == -1) - err(1, "chmod: %s", output_filename); - - /* Output the a.out header. */ - write_header(); - - /* 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 symbol table (both globals and locals). */ - write_syms(); - - /* Output the RSS section */ - write_rrs(); - - if (chmod (output_filename, filemode | (0111 & ~u_mask)) == -1) - err(1, "chmod: %s", output_filename); - - fflush(outstream); - /* Report I/O error such as disk full. */ - if (ferror(outstream) || fclose(outstream) != 0) - err(1, "write_output: %s", output_filename); - outstream = 0; -} - -/* Total number of symbols to be written in the output file. */ -static int nsyms; - -void -write_header() -{ - int flags; - - if (link_mode & SHAREABLE) - /* Output is shared object */ - flags = EX_DYNAMIC | EX_PIC; - else if (relocatable_output && pic_code_seen) - /* Output is relocatable and contains PIC code */ - flags = EX_PIC; - else if (rrs_section_type == RRS_FULL) - /* Output is a dynamic executable */ - flags = EX_DYNAMIC; - else - /* - * Output is a static executable - * or a non-PIC relocatable object - */ - flags = 0; - - if (oldmagic && (flags & EX_DPMASK)) - warnx("Cannot set flag in old magic headers\n"); - - 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 = global_sym_count + local_sym_count + debugger_sym_count; - - if (relocatable_output) - nsyms += set_symbol_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; - } - - md_swapout_exec_hdr(&outheader); - mywrite(&outheader, 1, sizeof(struct exec), outstream); - 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, outstream); -#endif -} - -/* - * Relocate the text segment of each input file - * and write to the output file. - */ -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, outstream); -} - -/* - * 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 fd; - - if (trace_files) - prline_file_name(entry, stderr); - - fd = file_open(entry); - - /* Allocate space for the file's text section */ - bytes = (char *)alloca(entry->header.a_text); - - /* Deal with relocation information however is appropriate */ - if (entry->textrel == NULL) - errx(1, "%s: no text relocation", get_file_name(entry)); - - /* Read the text section into core. */ - if (lseek(fd, text_offset(entry), L_SET) == (off_t)-1) - err(1, "%s: copy_text: lseek", get_file_name(entry)); - if (entry->header.a_text != read(fd, bytes, entry->header.a_text)) - errx(1, "%s: copy_text: premature EOF", get_file_name(entry)); - - /* Relocate the text according to the text relocation. */ - perform_relocation (bytes, entry->header.a_text, - entry->textrel, entry->ntextrel, entry, 0); - - /* Write the relocated text to the output file. */ - mywrite(bytes, entry->header.a_text, 1, outstream); -} - -/* - * Relocate the data segment of each input file - * and write to the output file. - */ - -void -write_data() -{ - off_t pos; - - if (trace_files) - fprintf(stderr, "Copying and relocating data:\n\n"); - - pos = N_DATOFF(outheader) + data_start - rrs_data_start; - if (fseek(outstream, pos, SEEK_SET) != 0) - errx(1, "write_data: fseek"); - - each_full_file(copy_data, 0); - file_close(); - - /* - * Write out the set element vectors. See digest symbols for - * description of length of the set vector section. - */ - - if (set_vector_count) { - swap_longs(set_vectors, set_symbol_count + 2*set_vector_count); - mywrite(set_vectors, set_symbol_count + 2*set_vector_count, - sizeof (unsigned long), outstream); - } - - if (trace_files) - fprintf(stderr, "\n"); - - padfile(data_pad, outstream); -} - -/* - * 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 -copy_data(entry) - struct file_entry *entry; -{ - register char *bytes; - register int fd; - - if (trace_files) - prline_file_name (entry, stderr); - - fd = file_open(entry); - - bytes = (char *)alloca(entry->header.a_data); - - if (entry->datarel == NULL) - errx(1, "%s: no data relocation", get_file_name(entry)); - - if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) == - (off_t)-1) - err(1, "%s: copy_data: lseek", get_file_name(entry)); - if (entry->header.a_data != read(fd, bytes, entry->header.a_data)) - errx(1, "%s: copy_data: premature EOF", get_file_name(entry)); - - perform_relocation(bytes, entry->header.a_data, - entry->datarel, entry->ndatarel, entry, 1); - - mywrite(bytes, entry->header.a_data, 1, outstream); -} - -/* - * 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. - */ - -int pc_relocation; - -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; - - 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; - 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) - errx(1, "%s: relocation address out of range", - get_file_name(entry)); - - if (RELOC_JMPTAB_P(r)) { - - int symindex = RELOC_SYMBOL(r); - struct localsymbol *lsp = &entry->symbols[symindex]; - symbol *sp; - - if (symindex >= entry->nsymbols) - errx(1, "%s: relocation symbolnum out of range", - get_file_name(entry)); - - sp = lsp->symbol; - if (sp == NULL) - errx(1, "%s: bogus relocation record", - get_file_name(entry)); - 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(entry, r, sp, addend); - - } else if (RELOC_BASEREL_P(r)) { - - int symindex = RELOC_SYMBOL(r); - struct localsymbol *lsp = &entry->symbols[symindex]; - - if (symindex >= entry->nsymbols) - errx(1, "%s: relocation symbolnum out of range", - get_file_name(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( - entry, r, lsp, addend); - - } else if (RELOC_EXTERN_P(r)) { - - int symindex = RELOC_SYMBOL(r); - symbol *sp; - - if (symindex >= entry->nsymbols) - errx(1, "%s: relocation symbolnum out of range", - get_file_name(entry)); - - sp = entry->symbols[symindex].symbol; - if (sp == NULL) - errx(1, "%s: bogus relocation record", - get_file_name(entry)); - if (sp->alias) - sp = sp->alias; - - if (relocatable_output) { - relocation = addend; - /* - * In PIC code, we keep the reference to the - * external symbol, even if defined now. - */ - if (!pic_code_seen) - relocation += sp->value; - } else if (sp->defined) { - if (sp->flags & GS_TRACE) { - fprintf(stderr, - "symbol %s defined as %x in %s\n", - sp->name, sp->defined, - get_file_name(entry) ); - } - 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( - entry, r, sp, &relocation)) - continue; - } else if (sp->defined == N_SIZE) { - /* - * If size is known, arrange a - * run-time copy. - */ - if (!sp->size) - errx(1, "Copy item isn't: %s", - sp->name); - - relocation = addend + sp->value; - r->r_address = sp->value; - claim_rrs_cpy_reloc(entry, 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. - */ - if (sp->flags & GS_TRACE) { - fprintf(stderr, - "symbol %s claims RRS in %s%s\n", - sp->name, get_file_name(entry), - (sp->so_defined == (N_TEXT+N_EXT) && - sp->flags & GS_HASJMPSLOT)? - " (JMPSLOT)":""); - } - if (sp->so_defined == (N_TEXT+N_EXT) && - sp->flags & GS_HASJMPSLOT) { - /* - * Claim a jmpslot if one was allocated. - * - * At this point, a jmpslot can only - * result from a shared object reference - * while `force_alias' is in effect. - */ - relocation = addend + - claim_rrs_jmpslot( - entry, r, sp, addend); - } else { - r->r_address += dataseg? - entry->data_start_address: - entry->text_start_address; - relocation = addend; - if ((building_shared_object || - sp->so_defined) && - claim_rrs_reloc(entry, r, sp, - &relocation)) - continue; - } - } - - } 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: - errx(1, "%s: nonexternal relocation invalid", - get_file_name(entry)); - } - - /* - * When building a shared object, these segment - * relocations need a "load address relative" - * RRS fixup. - */ - if (building_shared_object && !RELOC_PCREL_P(r)) { - r->r_address += dataseg? - entry->data_start_address: - entry->text_start_address; - claim_rrs_segment_reloc(entry, r); - } - } - - if (RELOC_PCREL_P(r)) - relocation -= pc_relocation; - - md_relocate(r, relocation, data+addr, relocatable_output); - - } -} - - -/* - * For relocatable_output only: write out the relocation, - * relocating the addresses-to-be-relocated. - */ -void -write_rel() -{ - 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->flags & GS_REFERENCED) - dynamic_symbol->symbolnum = count++; - FOR_EACH_SYMBOL(i, sp) { - if (sp == dynamic_symbol) - continue; - if (sp->warning) - count += 2; - if (!(sp->flags & GS_REFERENCED)) - continue; - sp->symbolnum = count++; - if (sp->size) - count++; - if (sp->alias) - count++; - } END_EACH_SYMBOL; - - if (count != global_sym_count) - errx(1, "internal error: write_rel: count = %d", count); - - each_full_file(assign_symbolnums, &count); - - /* Write out the relocations of all files, remembered from copy_text. */ - each_full_file(coptxtrel, 0); - - if (trace_files) - fprintf(stderr, "\nWriting data relocation:\n\n"); - - each_full_file(copdatrel, 0); - - if (trace_files) - fprintf(stderr, "\n"); -} - - -/* - * Assign symbol ordinal numbers to local symbols in each entry. - */ -static void -assign_symbolnums(entry, countp) - struct file_entry *entry; - int *countp; -{ - struct localsymbol *lsp, *lspend; - int n = *countp; - - lspend = entry->symbols + entry->nsymbols; - - if (discard_locals != DISCARD_ALL) - /* Count the N_FN symbol for this entry */ - n++; - - for (lsp = entry->symbols; lsp < lspend; lsp++) { - if (lsp->flags & LS_WRITE) - lsp->symbolnum = n++; - } - *countp = n; -} - -static void -coptxtrel(entry) - struct file_entry *entry; -{ - 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; - struct localsymbol *lsp; - symbol *sp; - - RELOC_ADDRESS(r) += reloc; - - symindex = RELOC_SYMBOL(r); - lsp = &entry->symbols[symindex]; - - if (!RELOC_EXTERN_P(r)) { - if (!pic_code_seen) - continue; - if (RELOC_BASEREL_P(r)) - RELOC_SYMBOL(r) = lsp->symbolnum; - continue; - } - - if (symindex >= entry->nsymbols) - errx(1, "%s: relocation symbolnum out of range", - get_file_name(entry)); - - sp = lsp->symbol; - -#ifdef N_INDR - /* Resolve indirection. */ - if ((sp->defined & ~N_EXT) == N_INDR) { - if (sp->alias == NULL) - errx(1, "internal error: alias in hyperspace"); - sp = sp->alias; - } -#endif - - /* - * If the symbol is now defined, change the external - * relocation to an internal one. - */ - - if (sp->defined) { - if (!pic_code_seen) { - RELOC_EXTERN_P(r) = 0; - RELOC_SYMBOL(r) = (sp->defined & N_TYPE); - } else - RELOC_SYMBOL(r) = sp->symbolnum; - } else - /* - * Global symbols come first. - */ - RELOC_SYMBOL(r) = sp->symbolnum; - } - md_swapout_reloc(entry->textrel, entry->ntextrel); - mywrite(entry->textrel, entry->ntextrel, - sizeof(struct relocation_info), outstream); -} - -static void -copdatrel(entry) - struct file_entry *entry; -{ - 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; - - 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)) { - if (RELOC_BASEREL_P(r)) - errx(1, "%s: Unsupported relocation type", - get_file_name(entry)); - continue; - } - - symindex = RELOC_SYMBOL(r); - sp = entry->symbols[symindex].symbol; - - if (symindex >= entry->header.a_syms) - errx(1, "%s: relocation symbolnum out of range", - get_file_name(entry)); - -#ifdef N_INDR - /* Resolve indirection. */ - if ((sp->defined & ~N_EXT) == N_INDR) { - if (sp->alias == NULL) - errx(1, "internal error: alias in hyperspace"); - sp = sp->alias; - } -#endif - - symtype = sp->defined & N_TYPE; - - if (!pic_code_seen && ( symtype == N_BSS || - 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; - } - md_swapout_reloc(entry->datarel, entry->ndatarel); - mywrite(entry->datarel, entry->ndatarel, - sizeof(struct relocation_info), outstream); -} - -void write_file_syms __P((struct file_entry *, int *)); -void write_string_table __P((void)); - -/* Offsets and current lengths of symbol and string tables in output file. */ - -static int symtab_offset; -static int symtab_len; - -/* Address in output file where string table starts. */ -static int strtab_offset; - -/* Offset within string table - where the strings in `strtab_vector' should be written. */ -static int strtab_len; - -/* Total size of string table strings allocated so far, - including strings in `strtab_vector'. */ -static int strtab_size; - -/* Vector whose elements are strings to be added to the string table. */ -static char **strtab_vector; - -/* Vector whose elements are the lengths of those strings. */ -static int *strtab_lens; - -/* Index in `strtab_vector' at which the next string will be stored. */ -static 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. - */ - -static int -assign_string_table_index(name) - char *name; -{ - register int index = strtab_size; - register int len = strlen(name) + 1; - - strtab_size += len; - strtab_vector[strtab_index] = name; - strtab_lens[strtab_index++] = len; - - return index; -} - -/* - * 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; - - if (fseek(outstream, strtab_offset + strtab_len, SEEK_SET) != 0) - err(1, "write_string_table: %s: fseek", output_filename); - - for (i = 0; i < strtab_index; i++) { - mywrite(strtab_vector[i], strtab_lens[i], 1, outstream); - strtab_len += strtab_lens[i]; - } -} - -/* Write the symbol table and string table of the output file. */ - -void -write_syms() -{ - /* Number of symbols written so far. */ - 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(global_sym_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; - - symtab_offset = N_SYMOFF(outheader); - symtab_len = 0; - strtab_offset = N_STROFF(outheader); - strtab_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((global_sym_count) * sizeof(char *)); - strtab_lens = (int *)alloca((global_sym_count) * 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->flags & GS_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 &= ~N_EXT; - nl.n_value = dynamic_symbol->value; - nl.n_un.n_strx = assign_string_table_index(dynamic_symbol->name); - *bufp++ = nl; - syms_written++; - } - - /* Scan the symbol hash table, bucket by bucket. */ - - FOR_EACH_SYMBOL(i, sp) { - - if (sp == dynamic_symbol) - /* Already dealt with above */ - continue; - - /* - * Propagate N_WARNING symbols. - */ - if ((relocatable_output || building_shared_object) - && sp->warning) { - nl.n_type = N_WARNING; - nl.n_un.n_strx = assign_string_table_index(sp->warning); - nl.n_value = 0; - nl.n_other = 0; - nl.n_desc = 0; - *bufp++ = nl; - syms_written++; - - nl.n_type = N_UNDF + N_EXT; - nl.n_un.n_strx = assign_string_table_index(sp->name); - nl.n_value = 0; - nl.n_other = 0; - nl.n_desc = 0; - *bufp++ = nl; - syms_written++; - } - - if (!(sp->flags & GS_REFERENCED)) - /* Came from shared object but was not used */ - continue; - - if (sp->so_defined || (sp->alias && sp->alias->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) - continue; - if (!(sp->flags & GS_WEAK)) - warnx("symbol %s remains undefined", sp->name); - } - - if (syms_written >= global_sym_count) - errx(1, - "internal error: number of symbols exceeds alloc'd %d", - global_sym_count); - - /* - * Construct a `struct nlist' for the symbol. - */ - nl.n_other = 0; - nl.n_desc = 0; - - if (sp->defined > 1) { - /* - * defined with known type - */ - if (!relocatable_output && !building_shared_object && - 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_value = sp->alias->value; - nl.n_other = N_OTHER(0, sp->alias->aux); - } else { - int bind = 0; - - if (sp->defined == N_SIZE) - nl.n_type = N_DATA | N_EXT; - else - nl.n_type = sp->defined; - if (nl.n_type == (N_INDR|N_EXT) && - sp->value != 0) - errx(1, "%s: N_INDR has value %#x", - sp->name, sp->value); - nl.n_value = sp->value; - if (sp->def_lsp) - bind = N_BIND(&sp->def_lsp->nzlist.nlist); - nl.n_other = N_OTHER(bind, sp->aux); - } - - } else if (sp->common_size) { - /* - * defined as common but not allocated, - * happens only with -r and not -d, write out - * a common definition. - * - * common condition needs to be before undefined - * condition because unallocated commons are set - * undefined in digest_symbols. - */ - nl.n_type = N_UNDF | N_EXT; - nl.n_value = sp->common_size; - } else if (!sp->defined) { - /* undefined -- legit only if -r */ - nl.n_type = N_UNDF | N_EXT; - nl.n_value = 0; - } else - errx(1, - "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++; - - /* - * Write second symbol of an alias pair. - */ - if (nl.n_type == N_INDR + N_EXT) { - if (sp->alias == NULL) - errx(1, "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; - nl.n_other = 0; - nl.n_desc = 0; - *bufp++ = nl; - syms_written++; - } - - /* - * Write N_SIZE symbol for a symbol with a known size. - */ - if (relocatable_output && sp->size) { - nl.n_type = N_SIZE + N_EXT; - nl.n_un.n_strx = assign_string_table_index(sp->name); - nl.n_value = sp->size; - nl.n_other = 0; - nl.n_desc = 0; - *bufp++ = nl; - syms_written++; - } - -#ifdef DEBUG -printf("writesym(#%d): %s, type %x\n", syms_written, sp->name, sp->defined); -#endif - } END_EACH_SYMBOL; - - if (syms_written != strtab_index || strtab_index != global_sym_count) - errx(1, "internal error: wrong number (%d) of global symbols " - "written into output file, should be %d", - syms_written, global_sym_count); - - /* Output the buffer full of `struct nlist's. */ - - if (fseek(outstream, symtab_offset + symtab_len, SEEK_SET) != 0) - err(1, "write_syms: fseek"); - md_swapout_symbols(buf, bufp - buf); - mywrite(buf, bufp - buf, sizeof(struct nlist), outstream); - symtab_len += sizeof(struct nlist) * (bufp - buf); - - /* Write the strings for the global symbols. */ - write_string_table(); - - /* Write the local symbols defined by the various files. */ - each_file(write_file_syms, (void *)&syms_written); - file_close(); - - if (syms_written != nsyms) - errx(1, "internal error: wrong number of symbols (%d) " - "written into output file, should be %d", - syms_written, nsyms); - - if (symtab_offset + symtab_len != strtab_offset) - errx(1, - "internal error: inconsistent symbol table length: %d vs %s", - symtab_offset + symtab_len, strtab_offset); - - if (fseek(outstream, strtab_offset, SEEK_SET) != 0) - err(1, "write_syms: fseek"); - strtab_size = md_swap_long(strtab_size); - mywrite(&strtab_size, 1, sizeof(int), outstream); -} - - -/* - * 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; -{ - struct localsymbol *lsp, *lspend; - - /* Upper bound on number of syms to be written here. */ - int max_syms = entry->nsymbols + 1; - - /* - * 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)); - - register struct nlist *bufp = buf; - - if (entry->flags & E_DYNAMIC) - return; - - /* - * 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)++; - } - /* 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; - char *name; - - if (!(lsp->flags & LS_WRITE)) - continue; - - if (p->n_un.n_strx == 0) - name = NULL; - else if (!(lsp->flags & LS_RENAME)) - name = p->n_un.n_strx + entry->strings; - else { - char *cp = p->n_un.n_strx + entry->strings; - name = (char *)alloca( - strlen(entry->local_sym_name) + - strlen(cp) + 2 ); - (void)sprintf(name, "%s.%s", entry->local_sym_name, cp); - } - - /* - * If this symbol has a name, allocate space for it - * in the output string table. - */ - - if (name) - p->n_un.n_strx = assign_string_table_index(name); - - /* Output this symbol to the buffer and count it. */ - - *bufp++ = *p; - (*syms_written_addr)++; - } - - /* All the symbols are now in BUF; write them. */ - - if (fseek(outstream, symtab_offset + symtab_len, SEEK_SET) != 0) - err(1, "write local symbols: fseek"); - md_swapout_symbols(buf, bufp - buf); - mywrite(buf, bufp - buf, sizeof(struct nlist), outstream); - symtab_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 disappear anyway. */ -} - -/* - * 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. - */ - -static int -parse(arg, format, error) - char *arg, *format, *error; -{ - int x; - - if (1 != sscanf(arg, format, &x)) - errx(1, error, arg); - return x; -} - -/* - * Output COUNT*ELTSIZE bytes of data at BUF to the descriptor FD. - */ -void -mywrite(buf, count, eltsize, fd) - void *buf; - int count; - int eltsize; - FILE *fd; -{ - - if (fwrite(buf, eltsize, count, fd) != count) - err(1, "write"); -} - -static void -cleanup() -{ - struct stat statbuf; - - if (outstream == 0) - return; - - if (fstat(fileno(outstream), &statbuf) == 0) { - if (S_ISREG(statbuf.st_mode)) - (void)unlink(output_filename); - } -} - -/* - * Output PADDING zero-bytes to descriptor FD. - * PADDING may be negative; in that case, do nothing. - */ -void -padfile(padding, fd) - int padding; - FILE *fd; -{ - register char *buf; - if (padding <= 0) - return; - - buf = (char *)alloca(padding); - bzero(buf, padding); - mywrite(buf, padding, 1, fd); -} diff --git a/gnu/usr.bin/ld/ld.h b/gnu/usr.bin/ld/ld.h deleted file mode 100644 index f84ec273c969..000000000000 --- a/gnu/usr.bin/ld/ld.h +++ /dev/null @@ -1,714 +0,0 @@ -/* - * $Id: ld.h,v 1.19 1996/02/22 00:19:57 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 - -#ifdef __FreeBSD__ -#define FreeBSD -#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) - -/* Define this to specify the default executable format. */ -#ifndef DEFAULT_MAGIC -#ifdef FreeBSD -#define DEFAULT_MAGIC QMAGIC -extern int netzmagic; -#else -#define DEFAULT_MAGIC ZMAGIC -#endif -#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 - * 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) -#define RELOC_PIC_TYPE(r) ((r)->r_baserel? \ - PIC_TYPE_LARGE:PIC_TYPE_NONE) - -#endif - -#ifndef RELOC_INIT_SEGMENT_RELOC -#define RELOC_INIT_SEGMENT_RELOC(r) -#endif - -#ifndef MAX_GOTOFF -#define MAX_GOTOFF(x) (LONG_MAX) -#endif - -#ifndef MIN_GOTOFF -#define MIN_GOTOFF(x) (LONG_MIN) -#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__ */ - -#define N_ISWEAK(p) (N_BIND(p) & BIND_WEAK) - - -typedef struct localsymbol { - struct nzlist nzlist; /* n[z]list from file */ - struct glosym *symbol; /* Corresponding global symbol, - if any */ - struct localsymbol *next; /* List of definitions */ - struct file_entry *entry; /* Backpointer to file */ - long gotslot_offset; /* Position in GOT, if any */ - int symbolnum; /* Position in output nlist */ - int flags; -#define LS_L_SYMBOL 1 /* Local symbol starts with an `L' */ -#define LS_WRITE 2 /* Symbol goes in output symtable */ -#define LS_RENAME 4 /* xlat name to `.' */ -#define LS_HASGOTSLOT 8 /* This symbol has a GOT entry */ -#define LS_WARNING 16 /* Second part of a N_WARNING duo */ -} localsymbol_t; - -/* 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 { - struct glosym *link; /* Next symbol hash bucket. */ - char *name; /* Name of this symbol. */ - long value; /* Value of this symbol */ - localsymbol_t *refs; /* Chain of local symbols from object - files pertaining to this global - symbol */ - localsymbol_t *sorefs;/* Same for local symbols from shared - object files. */ - - char *warning; /* message, from N_WARNING nlists */ - int common_size; /* Common size */ - int symbolnum; /* Symbol index in output symbol table */ - int rrs_symbolnum; /* Symbol index in RRS symbol table */ - - localsymbol_t *def_lsp; /* The local symbol that gave this - global symbol its definition */ - - char defined; /* Definition of this symbol */ - char so_defined; /* Definition of this symbol in a shared - object. These go into the RRS symbol table */ - u_char undef_refs; /* Count of number of "undefined" - messages printed for this symbol */ - u_char mult_defs; /* Same for "multiply defined" symbols */ - struct glosym *alias; /* For symbols of type N_INDR, this - points at the real symbol. */ - int setv_count; /* Number of elements in N_SETV symbols */ - int size; /* Size of this symbol (either from N_SIZE - symbols or a from shared object's RRS */ - int aux; /* Auxiliary type information conveyed in - the `n_other' field of nlists */ - - /* The offset into one of the RRS tables, -1 if not used */ - long jmpslot_offset; - long gotslot_offset; - - long flags; - -#define GS_DEFINED 0x1 /* Symbol has definition (notyetused)*/ -#define GS_REFERENCED 0x2 /* Symbol is referred to by something - interesting */ -#define GS_TRACE 0x4 /* Symbol will be traced */ -#define GS_HASJMPSLOT 0x8 /* */ -#define GS_HASGOTSLOT 0x10 /* Some state bits concerning */ -#define GS_CPYRELOCRESERVED 0x20 /* entries in GOT and PLT tables */ -#define GS_CPYRELOCCLAIMED 0x40 /* */ -#define GS_WEAK 0x80 /* Symbol is weakly defined */ - -} symbol; - -/* Number of buckets in symbol hash table */ -#define SYMTABSIZE 1009 - -/* The symbol hash table: a vector of SYMTABSIZE pointers to struct glosym. */ -extern symbol *symtab[]; -#define FOR_EACH_SYMBOL(i,sp) { \ - int i; \ - for (i = 0; i < SYMTABSIZE; i++) { \ - register symbol *sp; \ - for (sp = symtab[i]; sp; sp = sp->link) - -#define END_EACH_SYMBOL }} - -/* # of global symbols referenced and not defined. */ -extern int undefined_global_sym_count; - -/* # of weak symbols referenced and not defined. */ -extern int undefined_weak_sym_count; - -/* # of undefined symbols referenced by shared objects */ -extern int undefined_shobj_sym_count; - -/* # of multiply defined symbols. */ -extern int multiple_def_count; - -/* # of common symbols. */ -extern int common_defined_global_count; - -/* # of warning symbols encountered. */ -extern int warn_sym_count; -extern int list_warning_symbols; - -/* - * 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; -}; - -extern symbol *entry_symbol; /* the entry symbol, if any */ -extern symbol *edata_symbol; /* the symbol _edata */ -extern symbol *etext_symbol; /* the symbol _etext */ -extern symbol *end_symbol; /* the symbol _end */ -extern symbol *got_symbol; /* the symbol __GLOBAL_OFFSET_TABLE_ */ -extern 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 { - char *filename; /* Name of this file. */ - /* - * 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; - struct exec header; /* The file's a.out header. */ - localsymbol_t *symbols; /* Symbol table of the file. */ - int nsymbols; /* Number of symbols in above array. */ - int string_size; /* Size in bytes of string table. */ - char *strings; /* Pointer to the string table when - in core, NULL otherwise */ - int strings_offset; /* Offset of string table, - (normally N_STROFF() + 4) */ - /* - * Next two used only if `relocatable_output' or if needed for - * output of undefined reference line numbers. - */ - struct relocation_info *textrel; /* Text relocations */ - int ntextrel; /* # of text relocations */ - struct relocation_info *datarel; /* Data relocations */ - int ndatarel; /* # of data relocations */ - - /* - * Relation of this file's segments to the output file. - */ - int text_start_address; /* Start of this file's text segment - in the output file core image. */ - int data_start_address; /* Start of this file's data segment - in the output file core image. */ - int bss_start_address; /* Start of this file's bss segment - in the output file core image. */ - struct file_entry *subfiles; /* For a library, points to chain of - entries for the library members. */ - struct file_entry *superfile; /* For library member, points to the - library's own entry. */ - struct file_entry *chain; /* For library member, points to next - entry for next member. */ - int starting_offset; /* For a library member, offset of the - member within the archive. Zero for - files that are not library members.*/ - int total_size; /* Size of contents of this file, - if library member. */ -#ifdef SUN_COMPAT - struct file_entry *silly_archive;/* For shared libraries which have - a .sa companion */ -#endif - int lib_major, lib_minor; /* Version numbers of a shared object */ - - int flags; -#define E_IS_LIBRARY 1 /* File is a an archive */ -#define E_HEADER_VALID 2 /* File's header has been read */ -#define E_SEARCH_DIRS 4 /* Search directories for file */ -#define E_SEARCH_DYNAMIC 8 /* Search for shared libs allowed */ -#define E_JUST_SYMS 0x10 /* File is used for incremental load */ -#define E_DYNAMIC 0x20 /* File is a shared object */ -#define E_SCRAPPED 0x40 /* Ignore this file */ -#define E_SYMBOLS_USED 0x80 /* Symbols from this entry were used */ -#define E_SECONDCLASS 0x100 /* Shared object is a subsidiary */ -}; - -/* - * Section start addresses. - */ -extern int text_size; /* total size of text. */ -extern int text_start; /* start of text */ -extern int text_pad; /* clear space between text and data */ -extern int data_size; /* total size of data. */ -extern int data_start; /* start of data */ -extern int data_pad; /* part of bss segment within data */ - -extern int bss_size; /* total size of bss. */ -extern int bss_start; /* start of bss */ - -extern int text_reloc_size; /* total size of text relocation. */ -extern int data_reloc_size; /* total size of data relocation. */ - -/* - * 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). - */ -extern int rrs_section_type; /* What's in the RRS section */ -#define RRS_NONE 0 -#define RRS_PARTIAL 1 -#define RRS_FULL 2 -extern int rrs_text_size; /* Size of RRS text additions */ -extern int rrs_text_start; /* Location of above */ -extern int rrs_data_size; /* Size of RRS data additions */ -extern int rrs_data_start; /* Location of above */ -extern char *rrs_search_paths; /* `-L' RT paths */ - -/* Version number to put in __DYNAMIC (set by -V) */ -extern int soversion; -#ifndef DEFAULT_SOVERSION -#define DEFAULT_SOVERSION LD_VERSION_BSD -#endif - -extern int pc_relocation; /* Current PC reloc value */ - -extern int number_of_shobjs; /* # of shared objects linked in */ - -/* Current link mode */ -extern int 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 */ -#define SILLYARCHIVE 16 /* Process .sa companions, if any */ - -extern FILE *outstream; /* Output file. */ -extern struct exec outheader; /* Output file header. */ -extern int magic; /* Output file magic. */ -extern int oldmagic; -extern int relocatable_output; -extern int pic_type; -#define PIC_TYPE_NONE 0 -#define PIC_TYPE_SMALL 1 -#define PIC_TYPE_LARGE 2 - -/* Size of a page. */ -extern int page_size; - -extern char **search_dirs; /* Directories to search for libraries. */ -extern int n_search_dirs; /* Length of above. */ - -extern int write_map; /* write a load map (`-M') */ - -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 enter_file_symbols __P((struct file_entry *)); -void read_file_symbols __P((struct file_entry *)); -int set_element_prefixed_p __P((char *)); -int text_offset __P((struct file_entry *)); -int file_open __P((struct file_entry *)); -void each_file __P((void (*)(), void *)); -void each_full_file __P((void (*)(), void *)); -unsigned long check_each_file __P((unsigned long (*)(), void *)); -void mywrite __P((void *, int, int, FILE *)); -void padfile __P((int, FILE *)); - -/* In warnings.c: */ -void perror_name __P((char *)); -void perror_file __P((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((size_t)); -void *xrealloc __P((void *, size_t)); -char *concat __P((const char *, const char *, const 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 *, int)); -void add_search_dir __P((char *)); -void add_search_path __P((char *)); -void std_search_path __P((void)); -int getdewey __P((int[], char *)); -int cmpndewey __P((int[], int, int[], int)); - -/* In rrs.c: */ -void init_rrs __P((void)); -int rrs_add_shobj __P((struct file_entry *)); -void alloc_rrs_reloc __P((struct file_entry *, symbol *)); -void alloc_rrs_segment_reloc __P((struct file_entry *, struct relocation_info *)); -void alloc_rrs_jmpslot __P((struct file_entry *, symbol *)); -void alloc_rrs_gotslot __P((struct file_entry *, struct relocation_info *, localsymbol_t *)); -void alloc_rrs_cpy_reloc __P((struct file_entry *, symbol *)); - -int claim_rrs_reloc __P((struct file_entry *, struct relocation_info *, symbol *, long *)); -long claim_rrs_jmpslot __P((struct file_entry *, struct relocation_info *, symbol *, long)); -long claim_rrs_gotslot __P((struct file_entry *, struct relocation_info *, struct localsymbol *, long)); -long claim_rrs_internal_gotslot __P((struct file_entry *, struct relocation_info *, struct localsymbol *, long)); -void claim_rrs_cpy_reloc __P((struct file_entry *, struct relocation_info *, symbol *)); -void claim_rrs_segment_reloc __P((struct file_entry *, struct relocation_info *)); -void consider_rrs_section_lengths __P((void)); -void relocate_rrs_addresses __P((void)); -void write_rrs __P((void)); - -/* In .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 *)); -void md_set_breakpoint __P((long, long *)); - -#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__dynamic __P((struct link_dynamic *)); -void swap_section_dispatch_table __P((struct section_dispatch_table *)); -void swap_so_debug __P((struct so_debug *)); -void swapin_sod __P((struct sod *, int)); -void swapout_sod __P((struct sod *, int)); -void swapout_fshash __P((struct fshash *, int)); -#endif diff --git a/gnu/usr.bin/ld/ld/Makefile b/gnu/usr.bin/ld/ld/Makefile new file mode 100644 index 000000000000..178fca935eed --- /dev/null +++ b/gnu/usr.bin/ld/ld/Makefile @@ -0,0 +1,8 @@ +# $NetBSD: Makefile,v 1.1 1997/04/16 16:49:41 christos Exp $ + +PROG= ld +SRCS= ld.c symbol.c lib.c shlib.c warnings.c etc.c rrs.c xbits.c md.c +LDADD+= -lgnumalloc +DPADD+= /usr/lib/libgnumalloc.a + +.include diff --git a/gnu/usr.bin/ld/ldconfig/Makefile b/gnu/usr.bin/ld/ldconfig/Makefile index 587dd766ab89..77965a627fb5 100644 --- a/gnu/usr.bin/ld/ldconfig/Makefile +++ b/gnu/usr.bin/ld/ldconfig/Makefile @@ -1,13 +1,9 @@ -# $NetBSD: Makefile,v 1.10 1995/03/06 04:24:41 cgd Exp $ +# $NetBSD: Makefile,v 1.11 1997/04/16 16:49:42 christos Exp $ PROG= ldconfig SRCS= ldconfig.c shlib.c etc.c -LDDIR?= $(.CURDIR)/.. -CFLAGS+=-I$(LDDIR) -I$(.CURDIR) -I$(LDDIR)/$(MACHINE_ARCH) LDSTATIC=-static BINDIR= /sbin MAN= ldconfig.8 -.PATH: $(LDDIR) $(LDDIR)/$(MACHINE_ARCH) - .include diff --git a/gnu/usr.bin/ld/lib.c b/gnu/usr.bin/ld/lib.c deleted file mode 100644 index 1cb262e376ca..000000000000 --- a/gnu/usr.bin/ld/lib.c +++ /dev/null @@ -1,846 +0,0 @@ -/* - * $Id: lib.c,v 1.16 1995/06/04 21:33:14 pk Exp $ - library routines - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ld.h" - -static void linear_library __P((int, struct file_entry *)); -static void symdef_library __P((int, struct file_entry *, int)); -static struct file_entry *decode_library_subfile __P((int, - struct file_entry *, - int, int *)); - -/* - * Search the library ENTRY, already open on descriptor FD. 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(fd, entry) - int fd; - 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(fd, 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(fd, entry, member_length); - else - linear_library(fd, 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 FD. - * SUBFILE_OFFSET is the byte index in the library of this member's header. - * We store the length of the member into *LENGTH_LOC. - */ - -static struct file_entry * -decode_library_subfile(fd, library_entry, subfile_offset, length_loc) - int fd; - struct file_entry *library_entry; - int subfile_offset; - int *length_loc; -{ - int bytes_read; - register int namelen; - int member_length, content_length; - int starting_offset; - register char *name; - struct ar_hdr hdr1; - register struct file_entry *subentry; - - lseek(fd, subfile_offset, 0); - - bytes_read = read(fd, &hdr1, sizeof hdr1); - if (!bytes_read) - return 0; /* end of archive */ - - if (sizeof hdr1 != bytes_read) - errx(1, "%s: malformed library archive", - get_file_name(library_entry)); - - if (sscanf(hdr1.ar_size, "%d", &member_length) != 1) - errx(1, "%s: malformatted header of archive member: %.*s", - get_file_name(library_entry), - sizeof(hdr1.ar_name), hdr1.ar_name); - - 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++); - - starting_offset = subfile_offset + sizeof hdr1; - content_length = member_length; - -#ifdef AR_EFMT1 - /* - * BSD 4.4 extended AR format: #1/, with name as the - * first bytes of the file - */ - if (strncmp(hdr1.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 && - isdigit(hdr1.ar_name[sizeof(AR_EFMT1) - 1])) { - - namelen = atoi(&hdr1.ar_name[sizeof(AR_EFMT1) - 1]); - name = (char *)xmalloc(namelen + 1); - if (read(fd, name, namelen) != namelen) - errx(1, "%s: malformatted archive member: %.*s", - get_file_name(library_entry), - sizeof(hdr1.ar_name), hdr1.ar_name); - name[namelen] = 0; - content_length -= namelen; - starting_offset += namelen; - } else - -#endif - { - name = (char *)xmalloc(namelen + 1); - strncpy(name, hdr1.ar_name, namelen); - name[namelen] = 0; - } - - subentry->filename = name; - subentry->local_sym_name = name; - subentry->starting_offset = starting_offset; - subentry->superfile = library_entry; - subentry->total_size = content_length; -#if 0 - subentry->symbols = 0; - subentry->strings = 0; - subentry->subfiles = 0; - subentry->chain = 0; - subentry->flags = 0; -#endif - - (*length_loc) = member_length; - - return subentry; -} - -static int subfile_wanted_p __P((struct file_entry *)); - -/* - * Search a library that has a __.SYMDEF member. FD 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. - */ - -static void -symdef_library(fd, entry, member_length) - int fd; - struct file_entry *entry; - int member_length; -{ - int *symdef_data = (int *) xmalloc(member_length); - register struct ranlib *symdef_base; - char *sym_name_base; - int nsymdefs; - 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(fd, symdef_data, member_length); - if (bytes_read != member_length) - errx(1, "%s: malformatted __.SYMDEF", - get_file_name(entry)); - - nsymdefs = md_swap_long(*symdef_data) / sizeof(struct ranlib); - if (nsymdefs < 0 || - nsymdefs * sizeof(struct ranlib) + 2 * sizeof(int) > member_length) - errx(1, "%s: malformatted __.SYMDEF", - get_file_name(entry)); - - symdef_base = (struct ranlib *) (symdef_data + 1); - length_of_strings = md_swap_long(*(int *) (symdef_base + nsymdefs)); - - if (length_of_strings < 0 - || nsymdefs * sizeof(struct ranlib) + length_of_strings - + 2 * sizeof(int) > member_length) - errx(1, "%s: malformatted __.SYMDEF", - get_file_name(entry)); - - sym_name_base = sizeof(int) + (char *) (symdef_base + nsymdefs); - - /* Check all the string indexes for validity. */ - md_swapin_ranlib_hdr(symdef_base, nsymdefs); - for (i = 0; i < nsymdefs; i++) { - register int index = symdef_base[i].ran_un.ran_strx; - if (index < 0 || index >= length_of_strings - || (index && *(sym_name_base + index - 1))) - errx(1, "%s: malformatted __.SYMDEF", - get_file_name(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 < nsymdefs && - ((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 we're not forcing the archive in then we don't - * need to bother if: we've never heard of the symbol, - * or if it is already defined. The last clause causes - * archive members to be searched for definitions - * satisfying undefined shared object symbols. - */ - if (!(link_mode & FORCEARCHIVE) && - (!sp || sp->defined || - (!(sp->flags & GS_REFERENCED) && - !sp->sorefs))) - 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(fd, - entry, offset, &junk); - if (subentry == 0) - errx(1, - "invalid offset for %s in symbol table of %s", - sym_name_base - + symdef_base[i].ran_un.ran_strx, - entry->filename); - - read_entry_symbols(fd, subentry); - subentry->strings = (char *) - alloca(subentry->string_size); - read_entry_strings(fd, subentry); - - /* - * Now scan the symbol table and decide whether to - * load. - */ - - if (!(link_mode & FORCEARCHIVE) && - !subfile_wanted_p(subentry)) { - if (subentry->symbols) - 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(fd, 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 < nsymdefs; 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. - */ - subentry->strings = 0; - } - } - } - - free(symdef_data); -} - -/* - * Search a library that has no __.SYMDEF. ENTRY is the library's file_entry. - * FD is the descriptor it is open on. - */ - -static void -linear_library(fd, entry) - int fd; - 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(fd, entry, - this_subfile_offset, &member_length); - - if (!subentry) - return; - - read_entry_symbols(fd, subentry); - subentry->strings = (char *)alloca(subentry->string_size); - read_entry_strings(fd, subentry); - - if (!(link_mode & FORCEARCHIVE) && - !subfile_wanted_p(subentry)) { - if (subentry->symbols) - free(subentry->symbols); - free(subentry); - } else { - read_entry_relocation(fd, 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. - */ - -static 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->flags & 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->flags & GS_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->common_size) - common_defined_global_count++; - - if (sp->common_size < p->n_value) - sp->common_size = p->n_value; - if (!sp->defined) - undefined_global_sym_count--; - sp->defined = type; - continue; - } - if (sp->flags & GS_WEAK) - /* Weak symbols don't pull archive members */ - continue; - if (write_map) { - print_file_name(entry, stdout); - fprintf(stdout, " needed due to %s\n", sp->name); - } - return 1; - - } else if (!sp->defined && sp->sorefs) { - /* - * Check for undefined symbols or commons - * in shared objects. - */ - struct localsymbol *lsp; - - for (lsp = sp->sorefs; lsp; lsp = lsp->next) { - int type = lsp->nzlist.nlist.n_type; - if ( (type & N_EXT) && - (type & N_STAB) == 0 && - type != (N_UNDF | N_EXT)) - break; /* We don't need it */ - } - if (lsp != NULL) - /* - * We have a worthy definition in a shared - * object that was specified ahead of the - * archive we're examining now. So, punt. - */ - continue; - - /* - * At this point, we have an undefined shared - * object reference. Again, if the archive member - * defines a common we just note the its size. - * Otherwise, the member gets included. - */ - - if (type == (N_UNDF|N_EXT) && p->n_value) { - /* - * New symbol is common, just takes its - * size, but don't load. - */ - sp->common_size = p->n_value; - sp->defined = type; - continue; - } - - /* - * THIS STILL MISSES the case where one shared - * object defines a common and the next defines - * more strongly; fix this someday by making - * `struct glosym' and enter_global_ref() more - * symmetric. - */ - - if (write_map) { - print_file_name(entry, stdout); - fprintf(stdout, - " needed due to shared lib ref %s (%d)\n", - sp->name, - lsp ? lsp->nzlist.nlist.n_type : -1); - } - return 1; - } - } - - return 0; -} - -/* - * Read the symbols of dynamic entity ENTRY into core. Assume it is already - * open, on descriptor FD. - */ -void -read_shared_object(fd, entry) - struct file_entry *entry; - int fd; -{ - struct _dynamic dyn; - struct section_dispatch_table sdt; - struct nlist *np; - struct nzlist *nzp; - int n, i, has_nz = 0; - - if (!(entry->flags & E_HEADER_VALID)) - read_header(fd, entry); - - /* Read DYNAMIC structure (first in data segment) */ - if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) == - (off_t)-1) - err(1, "%s: lseek", get_file_name(entry)); - if (read(fd, &dyn, sizeof dyn) != sizeof dyn) { - errx(1, "%s: premature EOF reading _dynamic", - get_file_name(entry)); - } - md_swapin__dynamic(&dyn); - - /* Check version */ - switch (dyn.d_version) { - default: - errx(1, "%s: unsupported _DYNAMIC version: %d", - get_file_name(entry), dyn.d_version); - break; - case LD_VERSION_SUN: - break; - case LD_VERSION_BSD: - has_nz = 1; - break; - } - - /* Read Section Dispatch Table (from data segment) */ - if (lseek(fd, - text_offset(entry) + (long)dyn.d_un.d_sdt - - (DATA_START(entry->header) - N_DATOFF(entry->header)), - L_SET) == (off_t)-1) - err(1, "%s: lseek", get_file_name(entry)); - if (read(fd, &sdt, sizeof sdt) != sizeof sdt) - errx(1, "%s: premature EOF reading sdt", - get_file_name(entry)); - md_swapin_section_dispatch_table(&sdt); - - /* Read symbols (text segment) */ - n = sdt.sdt_strings - sdt.sdt_nzlist; - 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)); - - if (lseek(fd, - text_offset(entry) + (long)sdt.sdt_nzlist - - (TEXT_START(entry->header) - N_TXTOFF(entry->header)), - L_SET) == (off_t)-1) - err(1, "%s: lseek", get_file_name(entry)); - if (read(fd, (char *)nzp, n) != n) - errx(1, "%s: premature EOF reading symbols ", - get_file_name(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].entry = entry; - entry->symbols[i].gotslot_offset = -1; - entry->symbols[i].flags = 0; - } - - /* Read strings (text segment) */ - n = entry->string_size = sdt.sdt_str_sz; - entry->strings = (char *)alloca(n); - entry->strings_offset = text_offset(entry) + sdt.sdt_strings; - if (lseek(fd, - entry->strings_offset - - (TEXT_START(entry->header) - N_TXTOFF(entry->header)), - L_SET) == (off_t)-1) - err(1, "%s: lseek", get_file_name(entry)); - if (read(fd, entry->strings, n) != n) - errx(1, "%s: premature EOF reading strings", - get_file_name(entry)); - enter_file_symbols (entry); - entry->strings = 0; - - /* - * Load any subsidiary shared objects. - */ - if (sdt.sdt_sods) { - struct sod sod; - off_t offset; - struct file_entry *prev = NULL; - - offset = (off_t)sdt.sdt_sods; - while (1) { - struct file_entry *subentry; - char *libname, name[MAXPATHLEN]; /*XXX*/ - - subentry = (struct file_entry *) - xmalloc(sizeof(struct file_entry)); - bzero(subentry, sizeof(struct file_entry)); - subentry->superfile = entry; - subentry->flags = E_SECONDCLASS; - - if (lseek(fd, - offset - (TEXT_START(entry->header) - - N_TXTOFF(entry->header)), - L_SET) == (off_t)-1) - err(1, "%s: lseek", get_file_name(entry)); - if (read(fd, &sod, sizeof(sod)) != sizeof(sod)) - errx(1, "%s: premature EOF reding sod", - get_file_name(entry)); - md_swapin_sod(&sod, 1); - if (lseek(fd, - (off_t)sod.sod_name - (TEXT_START(entry->header) - - N_TXTOFF(entry->header)), - L_SET) == (off_t)-1) - err(1, "%s: lseek", get_file_name(entry)); - (void)read(fd, name, sizeof(name)); /*XXX*/ - if (sod.sod_library) { - int sod_major = sod.sod_major; - int sod_minor = sod.sod_minor; - - libname = findshlib(name, - &sod_major, &sod_minor, 0); - if (libname == NULL) - errx(1,"no shared -l%s.%d.%d available", - name, sod.sod_major, sod.sod_minor); - subentry->filename = libname; - subentry->local_sym_name = concat("-l", name, ""); - } else { - subentry->filename = strdup(name); - subentry->local_sym_name = strdup(name); - } - read_file_symbols(subentry); - - if (prev) - prev->chain = subentry; - else - entry->subfiles = subentry; - prev = subentry; - fd = file_open(entry); - if ((offset = (off_t)sod.sod_next) == 0) - break; - } - } -#ifdef SUN_COMPAT - if (link_mode & SILLYARCHIVE) { - char *cp, *sa_name; - char armag[SARMAG]; - int fd; - struct file_entry *subentry; - - sa_name = strdup(entry->filename); - if (sa_name == NULL) - goto out; - cp = sa_name + strlen(sa_name) - 1; - while (cp > sa_name) { - if (!isdigit(*cp) && *cp != '.') - break; - --cp; - } - if (cp <= sa_name || *cp != 'o') { - /* Not in `libxxx.so.n.m' form */ - free(sa_name); - goto out; - } - - *cp = 'a'; - if ((fd = open(sa_name, O_RDONLY, 0)) < 0) - goto out; - - /* Read archive magic */ - bzero(armag, SARMAG); - (void)read(fd, armag, SARMAG); - (void)close(fd); - if (strncmp(armag, ARMAG, SARMAG) != 0) { - warnx("%s: malformed silly archive", - get_file_name(entry)); - goto out; - } - - subentry = (struct file_entry *) - xmalloc(sizeof(struct file_entry)); - bzero(subentry, sizeof(struct file_entry)); - - entry->silly_archive = subentry; - subentry->superfile = entry; - subentry->filename = sa_name; - subentry->local_sym_name = sa_name; - subentry->flags |= E_IS_LIBRARY; - search_library(file_open(subentry), subentry); -out: - ; - } -#endif -} - -#undef major -#undef minor - -int -findlib(p) -struct file_entry *p; -{ - int i; - int fd = -1; - int major = -1, minor = -1; - char *cp, *fname = NULL; - - if (!(p->flags & E_SEARCH_DYNAMIC)) - goto dot_a; - - fname = findshlib(p->filename, &major, &minor, 1); - - if (fname && (fd = open(fname, O_RDONLY, 0)) > 0) { - p->filename = fname; - p->lib_major = major; - p->lib_minor = minor; - p->flags &= ~E_SEARCH_DIRS; - return fd; - } - (void)free(fname); - -dot_a: - p->flags &= ~E_SEARCH_DYNAMIC; - 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 *path - = concat(search_dirs[i], "/", fname); - fd = open(path, O_RDONLY, 0); - if (fd > 0) { - p->filename = path; - p->flags &= ~E_SEARCH_DIRS; - break; - } - (void)free(path); - } - (void)free(fname); - return fd; -} - diff --git a/gnu/usr.bin/ld/rrs.c b/gnu/usr.bin/ld/rrs.c deleted file mode 100644 index 1229188ee818..000000000000 --- a/gnu/usr.bin/ld/rrs.c +++ /dev/null @@ -1,1262 +0,0 @@ -/* - * 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 without 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.18 1995/08/04 21:49:08 pk Exp $ - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ld.h" - -static struct _dynamic rrs_dyn; /* defined in link.h */ -static struct so_debug rrs_so_debug; /* defined in link.h */ -static struct section_dispatch_table rrs_sdt; /* 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; -char *rrs_search_paths; /* `-L' RT search */ -static int rrs_search_paths_size; - -static int reserved_rrs_relocs; -static int claimed_rrs_relocs; -static int discarded_rrs_relocs; - -static int number_of_gotslots = 1; -static int number_of_jmpslots = 1; -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 max_got_offset; -static int min_got_offset; -static int got_origin; -static int current_reloc_offset; -static int current_hash_index; -int number_of_shobjs; - -/* Convert a GOT offset into a table entry */ -#define GOTP(off) ((got_t *)((long)rrs_got + got_origin + (off))) - -struct shobj { - struct shobj *next; - struct file_entry *entry; -}; - -/* -RRS text segment: - +-------------------+ <-- sdt_rel (rrs_text_start) - | | - | relocation | - | | - +-------------------+ <-- .sdt_hash - | | - | hash buckets | - | | - +-------------------+ <-- .sdt_nzlist - | | - | symbols | - | | - +-------------------+ <-- .sdt_strings - | | - | strings | - | | - +-------------------+ <-- .sdt_sods - | | - | shobjs | - | | - +-------------------+ - | | - | shobjs strings | <-- .sod_name - | | - +-------------------+ - - -RRS data segment: - - +-------------------+ <-- __DYNAMIC (rrs_data_start) - | | - | _dymamic | - | | - +-------------------+ <-- __DYNAMIC.d_debug - | | - | so_debug | - | | - +-------------------+ <-- __DYNAMIC.d_un.d_sdt - | | - | sdt | - | | - +-------------------+ <-- sdt_got - | | - | _GOT_ | <-- _GLOBAL_OFFSET_TABLE_ - | | ( == sdt_got + got_origin) - | | - +-------------------+ <-- sdt_plt - | | - | PLT | - | | - +-------------------+ -*/ - -/* - * Add NAME to the list of needed run-time objects. - * Return 1 if ENTRY was added to the list. - */ -int -rrs_add_shobj(entry) - struct file_entry *entry; -{ - struct shobj **p; - - for (p = &rrs_shobjs; *p != NULL; p = &(*p)->next) - if (strcmp((*p)->entry->filename, entry->filename) == 0) - return 0; - *p = (struct shobj *)xmalloc(sizeof(struct shobj)); - (*p)->next = NULL; - (*p)->entry = entry; - - number_of_shobjs++; - return 1; -} - -void -alloc_rrs_reloc(entry, sp) - struct file_entry *entry; - symbol *sp; -{ -#ifdef DEBUG -printf("alloc_rrs_reloc: %s in %s\n", sp->name, get_file_name(entry)); -#endif - reserved_rrs_relocs++; -} - -void -alloc_rrs_segment_reloc(entry, r) - struct file_entry *entry; - struct relocation_info *r; -{ -#ifdef DEBUG -printf("alloc_rrs_segment_reloc at %#x in %s\n", - r->r_address, get_file_name(entry)); -#endif - reserved_rrs_relocs++; -} - -void -alloc_rrs_jmpslot(entry, sp) - struct file_entry *entry; - symbol *sp; -{ - if (sp->flags & GS_HASJMPSLOT) - return; - - sp->flags |= GS_HASJMPSLOT; - number_of_jmpslots++; - reserved_rrs_relocs++; -} - -void -alloc_rrs_gotslot(entry, r, lsp) - struct file_entry *entry; - struct relocation_info *r; - struct localsymbol *lsp; -{ - symbol *sp = lsp->symbol; - - if (!RELOC_EXTERN_P(r)) { - - if (sp != NULL) { - warnx("%s: relocation for internal symbol " - "expected at %#x", - get_file_name(entry), RELOC_ADDRESS(r)); - return; - } - - if (!RELOC_STATICS_THROUGH_GOT_P(r)) - /* No need for a GOT slot */ - return; - - if (lsp->flags & LS_HASGOTSLOT) - return; - - lsp->flags |= LS_HASGOTSLOT; - - } else { - - if (sp == NULL) { - warnx("%s: relocation must refer " - "to global symbol at %#x", - get_file_name(entry), RELOC_ADDRESS(r)); - return; - } - - if (sp->alias) - sp = sp->alias; - - if (sp->flags & GS_HASGOTSLOT) - return; - - sp->flags |= GS_HASGOTSLOT; - } - - number_of_gotslots++; - reserved_rrs_relocs++; -} - -void -alloc_rrs_cpy_reloc(entry, sp) - struct file_entry *entry; - symbol *sp; -{ - if (sp->flags & GS_CPYRELOCRESERVED) - return; -#ifdef DEBUG -printf("alloc_rrs_copy: %s in %s\n", sp->name, get_file_name(entry)); -#endif - sp->flags |= GS_CPYRELOCRESERVED; - 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) - errx(1, "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(entry, rp, sp, relocation) - struct file_entry *entry; - struct relocation_info *rp; - symbol *sp; - long *relocation; -{ - struct relocation_info *r = rrs_next_reloc(); - - if (rp->r_address < text_start + text_size) - warnx("%s: RRS text relocation at %#x for \"%s\"", - get_file_name(entry), rp->r_address, sp->name); - -#ifdef DEBUG -printf("claim_rrs_reloc: %s in %s\n", sp->name, get_file_name(entry)); -#endif - r->r_address = rp->r_address; - r->r_symbolnum = sp->rrs_symbolnum; - - if (link_mode & SYMBOLIC) { - if (!sp->defined) - warnx("Cannot reduce symbol \"%s\" in %s", - sp->name, get_file_name(entry)); - 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(entry, rp, sp, addend) - struct file_entry *entry; - struct relocation_info *rp; - symbol *sp; - long addend; -{ - struct relocation_info *r; - - if (!(sp->flags & GS_HASJMPSLOT)) - errx(1, "internal error: " - "%s: claim_rrs_jmpslot: %s: no reservation", - get_file_name(entry), - sp->name); - - if (sp->jmpslot_offset != -1) - return rrs_sdt.sdt_plt + sp->jmpslot_offset; - - sp->jmpslot_offset = current_jmpslot_offset; - current_jmpslot_offset += sizeof(jmpslot_t); - -#ifdef DEBUG -printf("claim_rrs_jmpslot: %s: %s(%d) -> offset %x\n", - get_file_name(entry), - sp->name, sp->rrs_symbolnum, sp->jmpslot_offset); -#endif - - if ((link_mode & SYMBOLIC) || rrs_section_type == RRS_PARTIAL) { - if (!sp->defined) - warnx("Cannot reduce symbol \"%s\" in %s", - sp->name, get_file_name(entry)); - - md_fix_jmpslot( rrs_plt + sp->jmpslot_offset/sizeof(jmpslot_t), - rrs_sdt.sdt_plt + sp->jmpslot_offset, - sp->value); - if (rrs_section_type == RRS_PARTIAL || !JMPSLOT_NEEDS_RELOC) { - /* PLT is self-contained */ - discarded_rrs_relocs++; - return rrs_sdt.sdt_plt + sp->jmpslot_offset; - } - } else { - md_make_jmpslot(rrs_plt + sp->jmpslot_offset/sizeof(jmpslot_t), - sp->jmpslot_offset, - claimed_rrs_relocs); - } - - /* - * Install a run-time relocation for this PLT entry. - */ - r = rrs_next_reloc(); - - RELOC_SYMBOL(r) = sp->rrs_symbolnum; - - r->r_address = (long)rrs_sdt.sdt_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_sdt.sdt_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(entry, rp, lsp, addend) - struct file_entry *entry; - struct relocation_info *rp; - struct localsymbol *lsp; - long addend; -{ - struct relocation_info *r; - symbol *sp = lsp->symbol; - int reloc_type = 0; - - if (sp == NULL) { - return 0; - } - - if (sp->alias) - sp = sp->alias; - - if (!(sp->flags & GS_HASGOTSLOT)) - errx(1, "internal error: " - "%s: claim_rrs_gotslot: %s: no reservation", - get_file_name(entry), sp->name); - - if (sp->gotslot_offset != -1) { -#ifdef DIAGNOSTIC - if (*GOTP(sp->gotslot_offset) != addend + - ((!(link_mode & SHAREABLE) || (link_mode & SYMBOLIC)) - ? sp->value : 0)) - errx(1, "%s: %s: gotslot at %#x is multiple valued, " - "*got = %#x, addend = %#x, sp->value = %#x", - get_file_name(entry), sp->name, - sp->gotslot_offset, - *GOTP(sp->gotslot_offset), addend, sp->value); -#endif - /* This symbol already passed here before. */ - return sp->gotslot_offset; - } - - if (current_got_offset == 0) - /* GOT offset 0 is reserved */ - current_got_offset += sizeof(got_t); - - if (current_got_offset > max_got_offset) - errx(1, "%s: GOT overflow on symbol `%s' at %#x", - get_file_name(entry), sp->name, RELOC_ADDRESS(rp)); - - sp->gotslot_offset = current_got_offset; - current_got_offset += sizeof(got_t); - -#ifdef DEBUG -printf("claim_rrs_gotslot: %s(%d,%#x) slot offset %#x, addend %#x\n", - sp->name, sp->rrs_symbolnum, sp->value, sp->gotslot_offset, addend); -#endif - - if (sp->defined && - (!(link_mode & SHAREABLE) || (link_mode & SYMBOLIC))) { - - /* - * Reduce to just a base-relative translation. - */ - - *GOTP(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. - */ - warnx("Cannot reduce symbol \"%s\" in %s", - sp->name, get_file_name(entry)); - - } else { - - /* - * This gotslot will be updated with symbol value at run-time. - */ - - *GOTP(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) - warnx("Cannot reduce symbol \"%s\" in %s", - sp->name, get_file_name(entry)); - discarded_rrs_relocs++; - 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(); - r->r_address = got_symbol->value + 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 - got_symbol->value; - - if (!(lsp->flags & LS_HASGOTSLOT)) - errx(1, "internal error: " - "%s: claim_rrs_internal_gotslot at %#x: no reservation", - get_file_name(entry), RELOC_ADDRESS(rp)); - - if (lsp->gotslot_offset != -1) { - /* Already claimed */ - if (*GOTP(lsp->gotslot_offset) != addend) - errx(1, "%s: gotslot at %#x is multiple valued", - get_file_name(entry), lsp->gotslot_offset); - return lsp->gotslot_offset; - } - - if (current_got_offset == 0) - /* GOT offset 0 is reserved */ - current_got_offset += sizeof(got_t); - - if (current_got_offset > max_got_offset) - errx(1, "%s: GOT overflow for relocation at %#x", - get_file_name(entry), RELOC_ADDRESS(rp)); - - lsp->gotslot_offset = current_got_offset; - current_got_offset += sizeof(got_t); - - *GOTP(lsp->gotslot_offset) = addend; - -#ifdef DEBUG -printf("claim_rrs_internal_gotslot: %s: slot offset %#x, addend = %#x\n", - get_file_name(entry), lsp->gotslot_offset, addend); -#endif - - if (rrs_section_type == RRS_PARTIAL) { - discarded_rrs_relocs++; - return lsp->gotslot_offset; - } - - /* - * Relocation entry needed for this static GOT entry. - */ - r = rrs_next_reloc(); - r->r_address = got_symbol->value + lsp->gotslot_offset; - RELOC_EXTERN_P(r) = 0; - md_make_gotreloc(rp, r, RELTYPE_RELATIVE); - return lsp->gotslot_offset; -} - -void -claim_rrs_cpy_reloc(entry, rp, sp) - struct file_entry *entry; - struct relocation_info *rp; - symbol *sp; -{ - struct relocation_info *r; - - if (sp->flags & GS_CPYRELOCCLAIMED) - return; - - if (!(sp->flags & GS_CPYRELOCRESERVED)) - errx(1, "internal error: " - "%s: claim_cpy_reloc: %s: no reservation", - get_file_name(entry), sp->name); - -#ifdef DEBUG -printf("claim_rrs_copy: %s: %s -> %x\n", - get_file_name(entry), sp->name, sp->so_defined); -#endif - - r = rrs_next_reloc(); - sp->flags |= GS_CPYRELOCCLAIMED; - 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(entry, rp) - struct file_entry *entry; - struct relocation_info *rp; -{ - struct relocation_info *r = rrs_next_reloc(); - -#ifdef DEBUG -printf("claim_rrs_segment_reloc: %s at %#x\n", - get_file_name(entry), 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_sdt.sdt_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, **shpp; - -#ifdef notyet -/* We run into trouble with this as long as shared object symbols - are not checked for definitions */ - /* - * First, determine the real number of shared objects we need. - */ - for (shpp = &rrs_shobjs; *shpp; shpp = &(*shpp)->next) { - while (*shpp && !((*shpp)->entry->flags & E_SYMBOLS_USED)) { - if (--number_of_shobjs < 0) - errx(1, "internal error: number_of_shobjs < 0"); - *shpp = (*shpp)->next; - } - if (*shpp == NULL) - break; - } -#endif - - /* 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; - if (reserved_rrs_relocs > 0) - errx(1, "internal error: empty RRS has reservations"); - 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->flags & GS_REFERENCED)) - errx(1, "No reference to __DYNAMIC"); - - dynamic_symbol->flags |= GS_REFERENCED; - - if (number_of_gotslots > 1) - got_symbol->flags |= GS_REFERENCED; - - - /* 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 - * and calculate string space. - * Assign number 0 to __DYNAMIC (!! Sun compatibility) - */ - dynamic_symbol->rrs_symbolnum = number_of_rrs_symbols++; - FOR_EACH_SYMBOL(i ,sp) { - if ((link_mode & SHAREABLE) && sp->warning) { - /* Allocate N_WARNING & co */ - rrs_strtab_size += - 2 + strlen(sp->name) + strlen(sp->warning); - number_of_rrs_symbols += 2; - } - - if (!(sp->flags & GS_REFERENCED)) - continue; - - rrs_strtab_size += 1 + strlen(sp->name); - if (sp != dynamic_symbol) - sp->rrs_symbolnum = number_of_rrs_symbols++; - if (sp->alias) { - /* - * (sigh) Always allocate space to hold the - * indirection. At this point there's not - * enough information to decide whether it's - * actually needed or not. - */ - number_of_rrs_symbols++; - rrs_strtab_size += 1 + strlen(sp->alias->name); - } - } 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_sdt.sdt_buckets = number_of_rrs_symbols/4; - if (rrs_sdt.sdt_buckets < 4) - rrs_sdt.sdt_buckets = 4; - - number_of_rrs_hash_entries = rrs_sdt.sdt_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_sdt.sdt_buckets; n++) - rrs_hashtab[n].rh_symbolnum = -1; - current_hash_index = rrs_sdt.sdt_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->flags & GS_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 _dynamic); - rrs_data_size += sizeof(struct so_debug); - rrs_data_size += sizeof(struct section_dispatch_table); - 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; - rrs_search_paths_size = rrs_search_paths - ? strlen(rrs_search_paths) + 1 - : 0; - rrs_search_paths_size = MALIGN(rrs_search_paths_size); - rrs_text_size += rrs_search_paths_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 sod); - rrs_text_size += 1 + strlen(name); - } - - /* Finally, align size */ - rrs_text_size = MALIGN(rrs_text_size); -} - -void -relocate_rrs_addresses() -{ - int gotsize; - - dynamic_symbol->value = 0; - - /* - * Get ready to allocate linkage table offsets. - * First jmpslot is reserved for the run-time binder - * GOT entry at offset 0 is reserved for `__DYNAMIC'. - */ - current_jmpslot_offset = sizeof(jmpslot_t); - current_got_offset = 0; - max_got_offset = MAX_GOTOFF(pic_type); - min_got_offset = MIN_GOTOFF(pic_type); - gotsize = number_of_gotslots * sizeof(got_t); - - if (gotsize + min_got_offset - (int)sizeof(got_t) > max_got_offset) - warnx("Global Offset Table overflow (use `-fPIC')"); - - if (gotsize > max_got_offset) - /* Position at "two-complements" origin */ - current_got_offset += min_got_offset; - - got_origin = -current_got_offset; - - if (rrs_section_type == RRS_NONE) - return; - - if (rrs_section_type == RRS_PARTIAL) { - rrs_sdt.sdt_got = rrs_data_start; - got_symbol->value = rrs_sdt.sdt_got + got_origin; - rrs_sdt.sdt_plt = rrs_sdt.sdt_got + - number_of_gotslots * sizeof(got_t); - return; - } - - /* - * RRS data relocations. - */ - rrs_dyn.d_version = soversion; - rrs_dyn.d_debug = (struct so_debug *) - (rrs_data_start + sizeof(struct _dynamic)); - rrs_dyn.d_un.d_sdt = (struct section_dispatch_table *) - ((long)rrs_dyn.d_debug + sizeof(struct so_debug)); - - rrs_sdt.sdt_got = (long)rrs_dyn.d_un.d_sdt + - sizeof(struct section_dispatch_table); - rrs_sdt.sdt_plt = rrs_sdt.sdt_got + number_of_gotslots*sizeof(got_t); - - /* - * RRS text relocations. - */ - rrs_sdt.sdt_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_sdt.sdt_rel -= N_TXTADDR(outheader); -#endif - - rrs_sdt.sdt_hash = rrs_sdt.sdt_rel + - reserved_rrs_relocs * sizeof(struct relocation_info); - rrs_sdt.sdt_nzlist = rrs_sdt.sdt_hash + - number_of_rrs_hash_entries * sizeof(struct rrs_hash); - rrs_sdt.sdt_strings = rrs_sdt.sdt_nzlist + - number_of_rrs_symbols * rrs_symbol_size; - rrs_sdt.sdt_paths = rrs_search_paths - ? rrs_sdt.sdt_strings + rrs_strtab_size - : 0; - rrs_sdt.sdt_sods = rrs_shobjs - ? rrs_sdt.sdt_strings + rrs_strtab_size + - rrs_search_paths_size - : 0; - rrs_sdt.sdt_filler2 = 0; - rrs_sdt.sdt_str_sz = rrs_strtab_size; - rrs_sdt.sdt_text_sz = text_size; - rrs_sdt.sdt_plt_sz = number_of_jmpslots * sizeof(jmpslot_t); - - /* - * Assign addresses to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC. - * The value `&__DYNAMIC' is in the GOT table at offset 0. - */ - got_symbol->value = rrs_sdt.sdt_got + got_origin; - *GOTP(0) = 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 (fseek(outstream, pos, SEEK_SET) != 0) - err(1, "write_rrs_data: fseek"); - - if (rrs_section_type == RRS_PARTIAL) { - /* - * Only a GOT and PLT are needed. - */ - md_swapout_got(rrs_got, number_of_gotslots); - mywrite(rrs_got, number_of_gotslots, sizeof(got_t), outstream); - - md_swapout_jmpslot(rrs_plt, number_of_jmpslots); - mywrite(rrs_plt, number_of_jmpslots, - sizeof(jmpslot_t), outstream); - - return; - } - - md_swapout__dynamic(&rrs_dyn); - mywrite(&rrs_dyn, 1, sizeof(struct _dynamic), outstream); - - md_swapout_so_debug(&rrs_so_debug); - mywrite(&rrs_so_debug, 1, sizeof(struct so_debug), outstream); - - md_swapout_section_dispatch_table(&rrs_sdt); - mywrite(&rrs_sdt, 1, sizeof(struct section_dispatch_table), outstream); - - md_swapout_got(rrs_got, number_of_gotslots); - mywrite(rrs_got, number_of_gotslots, sizeof(got_t), outstream); - - md_swapout_jmpslot(rrs_plt, number_of_jmpslots); - mywrite(rrs_plt, number_of_jmpslots, sizeof(jmpslot_t), outstream); -} - -void -write_rrs_text() -{ - long pos; - int i; - int symsize; - struct nzlist *nlp; - int offset = 0; - struct shobj *shp; - struct sod *sodp; - int bind; - - if (rrs_section_type == RRS_PARTIAL) - return; - - pos = rrs_text_start + (N_TXTOFF(outheader) - TEXT_START(outheader)); - if (fseek(outstream, pos, SEEK_SET) != 0) - err(1, "write_rrs_text: fseek"); - - /* Write relocation records */ - md_swapout_reloc(rrs_reloc, reserved_rrs_relocs); - mywrite(rrs_reloc, reserved_rrs_relocs, - sizeof(struct relocation_info), outstream); - - /* 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), outstream); - - /* - * 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 == dynamic_symbol) - continue; - - if ((link_mode & SHAREABLE) && sp->warning) { - /* - * Write a N_WARNING duo. - */ - nlp->nz_type = N_WARNING; - nlp->nz_un.n_strx = offset; - nlp->nz_value = 0; - nlp->nz_other = 0; - nlp->nz_desc = 0; - nlp->nz_size = 0; - strcpy(rrs_strtab + offset, sp->warning); - offset += 1 + strlen(sp->warning); - INCR_NLP(nlp); - - nlp->nz_type = N_UNDF + N_EXT; - nlp->nz_un.n_strx = offset; - nlp->nz_value = 0; - nlp->nz_other = 0; - nlp->nz_desc = 0; - nlp->nz_size = 0; - strcpy(rrs_strtab + offset, sp->name); - offset += 1 + strlen(sp->name); - INCR_NLP(nlp); - } - - if (!(sp->flags & GS_REFERENCED)) - continue; - - if ((long)nlp - (long)rrs_symbols >= - number_of_rrs_symbols * rrs_symbol_size) - errx(1, "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; - - bind = (sp->flags & GS_WEAK) ? BIND_WEAK : 0; - - if (sp->defined > 1) { - /* defined with known type */ - if (!(link_mode & SHAREABLE) && - 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. - */ - nlp->nz_type = sp->alias->defined; - nlp->nz_value = sp->alias->value; - nlp->nz_other = N_OTHER(bind, sp->alias->aux); - } else 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; - nlp->nz_other = N_OTHER(bind, sp->aux); - } - if (LD_VERSION_NZLIST_P(soversion)) - nlp->nz_size = sp->size; - } else if (sp->common_size) { - /* - * A common definition. - */ - nlp->nz_type = N_UNDF | N_EXT; - nlp->nz_value = sp->common_size; - nlp->nz_other = N_OTHER(bind, 0); - } else if (!sp->defined) { - /* undefined */ - nlp->nz_type = N_UNDF | N_EXT; - nlp->nz_value = 0; - if (sp->so_defined && sp->jmpslot_offset != -1) { - /* - * A PLT entry. The auxiliary type -- which - * must be AUX_FUNC -- is used by the run-time - * linker to unambiguously resolve function - * address references. - */ - if (sp->aux != AUX_FUNC) - errx(1, "%s: non-function jmpslot", - sp->name); - nlp->nz_other = N_OTHER(bind, sp->aux); - nlp->nz_value = - rrs_sdt.sdt_plt + sp->jmpslot_offset; - } - } else - errx(1, "internal error: %s defined in mysterious way", - sp->name); - - /* Set symbol's name */ - nlp->nz_strx = offset; - strcpy(rrs_strtab + offset, sp->name); - offset += 1 + strlen(sp->name); - - if (sp->alias) { - /* - * Write an extra symbol for indirections (possibly - * just a dummy). - */ - int t = (nlp->nz_type == N_INDR + N_EXT); - - INCR_NLP(nlp); - nlp->nz_type = N_UNDF + (t ? N_EXT : 0); - nlp->nz_un.n_strx = offset; - nlp->nz_value = 0; - nlp->nz_other = 0; - nlp->nz_desc = 0; - nlp->nz_size = 0; - strcpy(rrs_strtab + offset, sp->alias->name); - offset += 1 + strlen(sp->alias->name); - } - - INCR_NLP(nlp); - - } END_EACH_SYMBOL; - - if (MALIGN(offset) != rrs_strtab_size) - errx(1, "internal error: " - "inconsistent RRS string table length: %d, expected %d", - offset, rrs_strtab_size); - - /* 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, outstream); - - /* Write the strings */ - mywrite(rrs_strtab, rrs_strtab_size, 1, outstream); - - /* Write RT search path */ - mywrite(rrs_search_paths, rrs_search_paths_size, 1, outstream); - - /* - * Write the names of the shared objects needed at run-time - */ - pos = rrs_sdt.sdt_sods + number_of_shobjs * sizeof(struct sod); - sodp = (struct sod *)alloca( number_of_shobjs * sizeof(struct sod)); - - for (i = 0, shp = rrs_shobjs; shp; i++, shp = shp->next) { - char *name = shp->entry->local_sym_name; - - if (i >= number_of_shobjs) - errx(1, "internal error: # of link objects exceeds %d", - number_of_shobjs); - - sodp[i].sod_name = pos; - sodp[i].sod_major = shp->entry->lib_major; - sodp[i].sod_minor = shp->entry->lib_minor; - - if (*name == '-' && *(name+1) == 'l') { - name += 2; - sodp[i].sod_library = 1; - } else - sodp[i].sod_library = 0; - - pos += 1 + strlen(name); - sodp[i].sod_next = (i == number_of_shobjs - 1) ? 0 : - (rrs_sdt.sdt_sods + (i+1)*sizeof(struct sod)); - } - - if (i < number_of_shobjs) - errx(1, "internal error: " - "# of link objects less then expected %d", - number_of_shobjs); - - md_swapout_sod(sodp, number_of_shobjs); - mywrite(sodp, number_of_shobjs, sizeof(struct sod), outstream); - - 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, outstream); - } -} - -void -write_rrs() -{ - - /* - * First, do some consistency checks on the RRS segment. - */ - if (rrs_section_type == RRS_NONE) { - if (reserved_rrs_relocs > 1) - errx(1, "internal error: " - "RRS relocs in static program: %d", - reserved_rrs_relocs-1); - return; - } - -#ifdef DEBUG -printf("rrs_relocs: reserved %d claimed %d discarded %d, gotslots %d jmpslots %d\n", - reserved_rrs_relocs, claimed_rrs_relocs, discarded_rrs_relocs, - number_of_gotslots-1, number_of_jmpslots-1); -#endif - - /* Final consistency check */ - if (claimed_rrs_relocs + discarded_rrs_relocs != reserved_rrs_relocs) { - errx(1, "internal error: " - "reserved relocs(%d) != claimed(%d) + discarded(%d)", - reserved_rrs_relocs, - claimed_rrs_relocs, - discarded_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 index c8cf6de996b2..748c13101540 100644 --- a/gnu/usr.bin/ld/rtld/Makefile +++ b/gnu/usr.bin/ld/rtld/Makefile @@ -1,11 +1,10 @@ -# $NetBSD: Makefile,v 1.15 1997/03/24 22:06:36 christos Exp $ +# $NetBSD: Makefile,v 1.16 1997/04/16 16:49:43 christos Exp $ PROG= ld.so SRCS= mdprologue.S rtld.c malloc.c shlib.c etc.c md.c vfprintf.c MAN= rtld.1 -LDDIR?= $(.CURDIR)/.. PICFLAG=-fpic -fno-function-cse -CFLAGS+=-I$(LDDIR) -I$(.CURDIR) -I$(LDDIR)/$(MACHINE_ARCH) $(PICFLAG) -DRTLD -DLIBC_SCCS +CFLAGS+=$(PICFLAG) -DRTLD -DLIBC_SCCS ASFLAGS+=-k LDFLAGS+=-Bshareable -Bsymbolic -assert nosymbolic .if defined(DESTDIR) @@ -19,7 +18,7 @@ MLINKS= rtld.1 ld.so.1 INCS=${HDRS} INCSDIR=/usr/include -.PATH: $(LDDIR) $(LDDIR)/$(MACHINE_ARCH) ${.CURDIR}/../../../../lib/libc/stdio +.PATH: ${.CURDIR}/../../../../lib/libc/stdio $(PROG): $(LD) -o $(PROG) $(LDFLAGS) $(OBJS) $(LDADD) diff --git a/gnu/usr.bin/ld/shlib.c b/gnu/usr.bin/ld/shlib.c deleted file mode 100644 index 4bb82b771a47..000000000000 --- a/gnu/usr.bin/ld/shlib.c +++ /dev/null @@ -1,316 +0,0 @@ -/* - * 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 without 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: shlib.c,v 1.11 1995/06/04 21:56:30 pk Exp $ - */ - -#ifdef sun -char *strsep(); -int isdigit(); -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ld.h" - -/* - * Standard directories to search for files specified by -l. - */ -#ifndef STANDARD_SEARCH_DIRS -#define STANDARD_SEARCH_DIRS "/usr/lib" -#endif - -/* - * Actual vector of library search directories, - * including `-L'ed and LD_LIBRARY_PATH spec'd ones. - */ -char **search_dirs; -int n_search_dirs; - -char *standard_search_dirs[] = { - STANDARD_SEARCH_DIRS -}; - - -void -add_search_dir(name) - char *name; -{ - n_search_dirs++; - search_dirs = (char **) - xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]); - search_dirs[n_search_dirs - 1] = strdup(name); -} - -void -remove_search_dir(name) - char *name; -{ - int n; - - for (n = 0; n < n_search_dirs; n++) { - if (strcmp(search_dirs[n], name)) - continue; - free(search_dirs[n]); - if (n < (n_search_dirs - 1)) - bcopy(&search_dirs[n+1], &search_dirs[n], - (n_search_dirs - n - 1) * sizeof search_dirs[0]); - n_search_dirs--; - } -} - -void -add_search_path(path) -char *path; -{ - register char *cp, *dup; - - if (path == NULL) - return; - - /* Add search directories from `path' */ - path = dup = strdup(path); - while ((cp = strsep(&path, ":")) != NULL) - add_search_dir(cp); - free(dup); -} - -void -remove_search_path(path) -char *path; -{ - register char *cp, *dup; - - if (path == NULL) - return; - - /* Remove search directories from `path' */ - path = dup = strdup(path); - while ((cp = strsep(&path, ":")) != NULL) - remove_search_dir(cp); - free(dup); -} - -void -std_search_path() -{ - int i, n; - - /* 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++; -#ifdef SUNOS_LIB_COMPAT - if (!(isdigit)(*cp)) -#else - if (!isdigit(*cp)) -#endif - 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; -{ - register 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; - - errx(1, "cmpndewey: cant happen"); - return 0; -} - -/* - * 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, do_dot_a) -char *name; -int *majorp, *minorp; -int do_dot_a; -{ - 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; - int found_dot_a = 0; - - if (dd == NULL) - continue; - - while ((dp = readdir(dd)) != NULL) { - int n, might_take_it = 0; - - if (do_dot_a && path == NULL && - dp->d_namlen == len + 2 && - strncmp(dp->d_name, lname, len) == 0 && - (dp->d_name+len)[0] == '.' && - (dp->d_name+len)[1] == 'a') { - - path = concat(search_dirs[i], "/", dp->d_name); - found_dot_a = 1; - } - - 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 && found_dot_a) { /* XXX */ - free(path); - path = NULL; - found_dot_a = 0; - } - - 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); - found_dot_a = 0; - bcopy(tmp, dewey, sizeof(dewey)); - ndewey = n; - *majorp = dewey[0]; - *minorp = dewey[1]; - } - closedir(dd); - - if (found_dot_a) - /* - * There's a .a archive here. - */ - return path; - } - - return path; -} diff --git a/gnu/usr.bin/ld/symbol.c b/gnu/usr.bin/ld/symbol.c deleted file mode 100644 index f23df761ebd0..000000000000 --- a/gnu/usr.bin/ld/symbol.c +++ /dev/null @@ -1,161 +0,0 @@ -/* - * $Id: symbol.c,v 1.8 1994/06/29 11:18:55 pk Exp $ - symbol table routines - */ - -/* Create the symbol table entries for `etext', `edata' and `end'. */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ld.h" - -symbol *symtab[SYMTABSIZE]; /* The symbol table. */ -int num_hash_tab_syms; /* Number of symbols in symbol hash table. */ - -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 */ - -void -symtab_init(relocatable_output) - int relocatable_output; -{ - /* - * Put linker reserved symbols into symbol table. - */ -#ifndef nounderscore -#define ETEXT_SYM "_etext" -#define EDATA_SYM "_edata" -#define END_SYM "_end" -#define DYN_SYM "__DYNAMIC" -#define GOT_SYM "__GLOBAL_OFFSET_TABLE_" -#else -#define ETEXT_SYM "etext" -#define EDATA_SYM "edata" -#define END_SYM "end" -#define DYN_SYM "_DYNAMIC" -#define GOT_SYM "_GLOBAL_OFFSET_TABLE_" -#endif - - dynamic_symbol = getsym(DYN_SYM); - dynamic_symbol->defined = relocatable_output?N_UNDF:(N_DATA | N_EXT); - - got_symbol = getsym(GOT_SYM); - got_symbol->defined = N_DATA | N_EXT; - - if (relocatable_output) - return; - - etext_symbol = getsym(ETEXT_SYM); - edata_symbol = getsym(EDATA_SYM); - end_symbol = getsym(END_SYM); - - etext_symbol->defined = N_TEXT | N_EXT; - edata_symbol->defined = N_DATA | N_EXT; - end_symbol->defined = N_BSS | N_EXT; - - etext_symbol->flags |= GS_REFERENCED; - edata_symbol->flags |= GS_REFERENCED; - end_symbol->flags |= GS_REFERENCED; -} - -/* - * 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) % SYMTABSIZE; - - /* Search the bucket. */ - for (bp = symtab[hashval]; bp; bp = bp->link) - if (strcmp(key, bp->name) == 0) - return bp; - - /* Nothing was found; create a new symbol table entry. */ - bp = (symbol *)xmalloc(sizeof(symbol)); - bp->name = (char *)xmalloc(strlen(key) + 1); - strcpy (bp->name, key); - bp->refs = 0; - bp->defined = 0; - bp->value = 0; - bp->common_size = 0; - bp->warning = 0; - bp->undef_refs = 0; - bp->mult_defs = 0; - bp->alias = 0; - bp->setv_count = 0; - bp->symbolnum = 0; - bp->rrs_symbolnum = 0; - - bp->size = 0; - bp->aux = 0; - bp->sorefs = 0; - bp->so_defined = 0; - bp->def_lsp = 0; - bp->jmpslot_offset = -1; - bp->gotslot_offset = -1; - bp->flags = 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) % SYMTABSIZE; - - /* Search the bucket. */ - for (bp = symtab[hashval]; bp; bp = bp->link) - if (strcmp(key, bp->name) == 0) - return bp; - - return 0; -} diff --git a/gnu/usr.bin/ld/symseg.h b/gnu/usr.bin/ld/symseg.h deleted file mode 100644 index baa8b309a5e0..000000000000 --- a/gnu/usr.bin/ld/symseg.h +++ /dev/null @@ -1,359 +0,0 @@ -/*- - * - * This code is derived from software copyrighted by the Free Software - * Foundation. - * - * from: @(#)symseg.h 5.4 (Berkeley) 4/30/91 - * $Id: symseg.h,v 1.3 1993/12/08 10:14:07 pk Exp $ - */ - -/* GDB symbol table format definitions. - Copyright (C) 1987, 1988 Free Software Foundation, Inc. - -This file is part of GNU CC. - -GNU CC is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 1, or (at your option) -any later version. - -GNU CC is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU CC; see the file COPYING. If not, write to -the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ - -/* Format of GDB symbol table data. - There is one symbol segment for each source file or - independant compilation. These segments are simply concatenated - to form the GDB symbol table. A zero word where the beginning - of a segment is expected indicates there are no more segments. - -Format of a symbol segment: - - The symbol segment begins with a word containing 1 - if it is in the format described here. Other formats may - be designed, with other code numbers. - - The segment contains many objects which point at each other. - The pointers are offsets in bytes from the beginning of the segment. - Thus, each segment can be loaded into core and its pointers relocated - to make valid in-core pointers. - - All the data objects in the segment can be found indirectly from - one of them, the root object, of type `struct symbol_root'. - It appears at the beginning of the segment. - - The total size of the segment, in bytes, appears as the `length' - field of this object. This size includes the size of the - root object. - - All the object data types are defined here to contain pointer types - appropriate for in-core use on a relocated symbol segment. - Casts to and from type int are required for working with - unrelocated symbol segments such as are found in the file. - - The ldsymaddr word is filled in by the loader to contain - the offset (in bytes) within the ld symbol table - of the first nonglobal symbol from this compilation. - This makes it possible to match those symbols - (which contain line number information) reliably with - the segment they go with. - - Core addresses within the program that appear in the symbol segment - are not relocated by the loader. They are inserted by the assembler - and apply to addresses as output by the assembler, so GDB must - relocate them when it loads the symbol segment. It gets the information - on how to relocate from the textrel, datarel, bssrel, databeg and bssbeg - words of the root object. - - The words textrel, datarel and bssrel - are filled in by ld with the amounts to relocate within-the-file - text, data and bss addresses by; databeg and bssbeg can be - used to tell which kind of relocation an address needs. */ - -enum language {language_c}; - -struct symbol_root -{ - int format; /* Data format version */ - int length; /* # bytes in this symbol segment */ - int ldsymoff; /* Offset in ld symtab of this file's syms */ - int textrel; /* Relocation for text addresses */ - int datarel; /* Relocation for data addresses */ - int bssrel; /* Relocation for bss addresses */ - char *filename; /* Name of main source file compiled */ - char *filedir; /* Name of directory it was reached from */ - struct blockvector *blockvector; /* Vector of all symbol-naming blocks */ - struct typevector *typevector; /* Vector of all data types */ - enum language language; /* Code identifying the language used */ - char *version; /* Version info. Not fully specified */ - char *compilation; /* Compilation info. Not fully specified */ - int databeg; /* Address within the file of data start */ - int bssbeg; /* Address within the file of bss start */ - struct sourcevector *sourcevector; /* Vector of line-number info */ -}; - -/* All data types of symbols in the compiled program - are represented by `struct type' objects. - All of these objects are pointed to by the typevector. - The type vector may have empty slots that contain zero. */ - -struct typevector -{ - int length; /* Number of types described */ - struct type *type[1]; -}; - -/* Different kinds of data types are distinguished by the `code' field. */ - -enum type_code -{ - TYPE_CODE_UNDEF, /* Not used; catches errors */ - TYPE_CODE_PTR, /* Pointer type */ - TYPE_CODE_ARRAY, /* Array type, lower bound zero */ - TYPE_CODE_STRUCT, /* C struct or Pascal record */ - TYPE_CODE_UNION, /* C union or Pascal variant part */ - TYPE_CODE_ENUM, /* Enumeration type */ - TYPE_CODE_FUNC, /* Function type */ - TYPE_CODE_INT, /* Integer type */ - TYPE_CODE_FLT, /* Floating type */ - TYPE_CODE_VOID, /* Void type (values zero length) */ - TYPE_CODE_SET, /* Pascal sets */ - TYPE_CODE_RANGE, /* Range (integers within spec'd bounds) */ - TYPE_CODE_PASCAL_ARRAY, /* Array with explicit type of index */ -}; - -/* This appears in a type's flags word for an unsigned integer type. */ -#define TYPE_FLAG_UNSIGNED 1 - -/* Other flag bits are used with GDB. */ - -struct type -{ - /* Code for kind of type */ - enum type_code code; - /* Name of this type, or zero if none. - This is used for printing only. - Type names specified as input are defined by symbols. */ - char *name; - /* Length in bytes of storage for a value of this type */ - int length; - /* For a pointer type, describes the type of object pointed to. - For an array type, describes the type of the elements. - For a function type, describes the type of the value. - Unused otherwise. */ - struct type *target_type; - /* Type that is a pointer to this type. - Zero if no such pointer-to type is known yet. - The debugger may add the address of such a type - if it has to construct one later. */ - struct type *pointer_type; - /* Type that is a function returning this type. - Zero if no such function type is known here. - The debugger may add the address of such a type - if it has to construct one later. */ - struct type *function_type; - /* Flags about this type. */ - short flags; - /* Number of fields described for this type */ - short nfields; - /* For structure and union types, a description of each field. - For set and pascal array types, there is one "field", - whose type is the domain type of the set or array. - For range types, there are two "fields", - the minimum and maximum values (both inclusive). - For enum types, each possible value is described by one "field". - For range types, there are two "fields", that record constant values - (inclusive) for the minimum and maximum. - - Using a pointer to a separate array of fields - allows all types to have the same size, which is useful - because we can allocate the space for a type before - we know what to put in it. */ - struct field - { - /* Position of this field, counting in bits from start of - containing structure. For a function type, this is the - position in the argument list of this argument. - For a range bound or enum value, this is the value itself. */ - int bitpos; - /* Size of this field, in bits, or zero if not packed. - For an unpacked field, the field's type's length - says how many bytes the field occupies. */ - int bitsize; - /* In a struct or enum type, type of this field. - In a function type, type of this argument. - In an array type, the domain-type of the array. */ - struct type *type; - /* Name of field, value or argument. - Zero for range bounds and array domains. */ - char *name; - } *fields; -}; - -/* All of the name-scope contours of the program - are represented by `struct block' objects. - All of these objects are pointed to by the blockvector. - - Each block represents one name scope. - Each lexical context has its own block. - - The first two blocks in the blockvector are special. - The first one contains all the symbols defined in this compilation - whose scope is the entire program linked together. - The second one contains all the symbols whose scope is the - entire compilation excluding other separate compilations. - In C, these correspond to global symbols and static symbols. - - Each block records a range of core addresses for the code that - is in the scope of the block. The first two special blocks - give, for the range of code, the entire range of code produced - by the compilation that the symbol segment belongs to. - - The blocks appear in the blockvector - in order of increasing starting-address, - and, within that, in order of decreasing ending-address. - - This implies that within the body of one function - the blocks appear in the order of a depth-first tree walk. */ - -struct blockvector -{ - /* Number of blocks in the list. */ - int nblocks; - /* The blocks themselves. */ - struct block *block[1]; -}; - -struct block -{ - /* Addresses in the executable code that are in this block. - Note: in an unrelocated symbol segment in a file, - these are always zero. They can be filled in from the - N_LBRAC and N_RBRAC symbols in the loader symbol table. */ - int startaddr, endaddr; - /* The symbol that names this block, - if the block is the body of a function; - otherwise, zero. - Note: In an unrelocated symbol segment in an object file, - this field may be zero even when the block has a name. - That is because the block is output before the name - (since the name resides in a higher block). - Since the symbol does point to the block (as its value), - it is possible to find the block and set its name properly. */ - struct symbol *function; - /* The `struct block' for the containing block, or 0 if none. */ - /* Note that in an unrelocated symbol segment in an object file - this pointer may be zero when the correct value should be - the second special block (for symbols whose scope is one compilation). - This is because the compiler ouptuts the special blocks at the - very end, after the other blocks. */ - struct block *superblock; - /* Number of local symbols. */ - int nsyms; - /* The symbols. */ - struct symbol *sym[1]; -}; - -/* Represent one symbol name; a variable, constant, function or typedef. */ - -/* Different name spaces for symbols. Looking up a symbol specifies - a namespace and ignores symbol definitions in other name spaces. - - VAR_NAMESPACE is the usual namespace. - In C, this contains variables, function names, typedef names - and enum type values. - - STRUCT_NAMESPACE is used in C to hold struct, union and enum type names. - Thus, if `struct foo' is used in a C program, - it produces a symbol named `foo' in the STRUCT_NAMESPACE. - - LABEL_NAMESPACE may be used for names of labels (for gotos); - currently it is not used and labels are not recorded at all. */ - -/* For a non-global symbol allocated statically, - the correct core address cannot be determined by the compiler. - The compiler puts an index number into the symbol's value field. - This index number can be matched with the "desc" field of - an entry in the loader symbol table. */ - -enum namespace -{ - UNDEF_NAMESPACE, VAR_NAMESPACE, STRUCT_NAMESPACE, LABEL_NAMESPACE, -}; - -/* An address-class says where to find the value of the symbol in core. */ - -enum address_class -{ - LOC_UNDEF, /* Not used; catches errors */ - LOC_CONST, /* Value is constant int */ - LOC_STATIC, /* Value is at fixed address */ - LOC_REGISTER, /* Value is in register */ - LOC_ARG, /* Value is at spec'd position in arglist */ - LOC_LOCAL, /* Value is at spec'd pos in stack frame */ - LOC_TYPEDEF, /* Value not used; definition in SYMBOL_TYPE - Symbols in the namespace STRUCT_NAMESPACE - all have this class. */ - LOC_LABEL, /* Value is address in the code */ - LOC_BLOCK, /* Value is address of a `struct block'. - Function names have this class. */ - LOC_EXTERNAL, /* Value is at address not in this compilation. - This is used for .comm symbols - and for extern symbols within functions. - Inside GDB, this is changed to LOC_STATIC once the - real address is obtained from a loader symbol. */ - LOC_CONST_BYTES /* Value is a constant byte-sequence. */ -}; - -struct symbol -{ - /* Symbol name */ - char *name; - /* Name space code. */ - enum namespace namespace; - /* Address class */ - enum address_class class; - /* Data type of value */ - struct type *type; - /* constant value, or address if static, or register number, - or offset in arguments, or offset in stack frame. */ - union - { - long value; - struct block *block; /* for LOC_BLOCK */ - char *bytes; /* for LOC_CONST_BYTES */ - } - value; -}; - -/* Source-file information. - This describes the relation between source files and line numbers - and addresses in the program text. */ - -struct sourcevector -{ - int length; /* Number of source files described */ - struct source *source[1]; /* Descriptions of the files */ -}; - -/* Line number and address of one line. */ - -struct line -{ - int linenum; - int address; -}; - -/* All the information on one source file. */ - -struct source -{ - char *name; /* Name of file */ - int nlines; /* Number of lines that follow */ - struct line lines[1]; /* Information on each line */ -}; diff --git a/gnu/usr.bin/ld/warnings.c b/gnu/usr.bin/ld/warnings.c deleted file mode 100644 index d5b7b2418a4d..000000000000 --- a/gnu/usr.bin/ld/warnings.c +++ /dev/null @@ -1,758 +0,0 @@ -/* - * $Id: warnings.c,v 1.18 1994/12/28 10:37:38 pk Exp $ - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#if __STDC__ -#include -#else -#include -#endif - -#include "ld.h" - -static int reported_undefineds; - -/* - * 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 == NULL) { - fprintf (outfile, "NULL"); - } - - 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 == NULL) { - return (char *)strdup("NULL"); - } - - if (entry->superfile) { - supfile = get_file_name(entry->superfile); - result = (char *) - xmalloc(strlen(supfile) + strlen(entry->filename) + 3); - (void)sprintf(result, "%s(%s)", supfile, entry->filename); - free(supfile); - - } else { - result = (char *)xmalloc(strlen(entry->filename) + 1); - strcpy(result, entry->filename); - } - return result; -} - -/* 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; -{ - fprintf(outfile, "\nFiles:\n\n"); - each_file(describe_file_sections, (void *)outfile); - - fprintf(outfile, "\nGlobal symbols:\n\n"); - FOR_EACH_SYMBOL(i, sp) { - fprintf(outfile, " %s: ", sp->name); - if (!(sp->flags & GS_REFERENCED)) - fprintf(outfile, "unreferenced"); - else if (sp->so_defined) - fprintf(outfile, "sodefined"); - else if (!sp->defined) - fprintf(outfile, "undefined"); - else if (sp->defined == (N_UNDF|N_EXT)) - fprintf(outfile, "common: size %#x", sp->common_size); - else - fprintf(outfile, "type %d, value %#x, size %#x", - sp->defined, sp->value, sp->size); - if (sp->alias) - fprintf(outfile, ", aliased to %s", sp->alias->name); - fprintf(outfile, "\n"); - } END_EACH_SYMBOL; - - each_file(list_file_locals, (void *)outfile); -} - -static void -describe_file_sections(entry, outfile) - struct file_entry *entry; - FILE *outfile; -{ - fprintf(outfile, " "); - print_file_name(entry, outfile); - if (entry->flags & (E_JUST_SYMS | E_DYNAMIC)) - fprintf(outfile, " symbols only\n"); - 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_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 -reloc_cmp(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 *lspend = entry->symbols + entry->nsymbols; - - - current->sym = next->sym; - current->line = next->line; - current->filename = next->filename; - - while (++(next->sym) < lspend) { - - 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; -{ - register struct localsymbol *lsp, *lspend; - struct line_debug_entry *state_pointer, *current, *next, *source; - - state_pointer = (struct line_debug_entry *) - xmalloc(3 * sizeof(*state_pointer)); - - current = state_pointer, - next = state_pointer + 1, - source = state_pointer + 2; /* Used to store source file */ - - lspend = entry->symbols+entry->nsymbols; - - for (lsp = entry->symbols; lsp < lspend; lsp++) - if (lsp->nzlist.nlist.n_type == N_SO) - break; - - if (lsp >= lspend) { - /* 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, *next, *tmp_pointer; - int use_data_symbols; - - current = state_pointer; - next = state_pointer + 1; - - 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 *rp, *erp; - int start_of_segment; - struct localsymbol *start_of_syms; - struct line_debug_entry *state_pointer, *current; - /* 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; - - rp = data_segment ? entry->datarel : entry->textrel; - erp = data_segment ? (rp + entry->ndatarel) : (rp + entry->ntextrel); - start_of_syms = entry->symbols; - start_of_segment = (data_segment ? - entry->data_start_address : - entry->text_start_address); - state_pointer = init_debug_scan(data_segment != 0, entry); - current = state_pointer; - - /* - * We need to sort the relocation info here. Sheesh, so much effort - * for one lousy error optimization. - */ - qsort(rp, erp - rp, sizeof(rp[0]), reloc_cmp); - - for (; rp < erp; rp++) { - register struct localsymbol *lsp; - register symbol *g; - - /* - * If the relocation isn't resolved through a symbol, continue. - */ - if (!RELOC_EXTERN_P(rp)) - continue; - - lsp = &entry->symbols[RELOC_SYMBOL(rp)]; - - /* - * 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 ((g = lsp->symbol) == NULL) - continue; - - if (!(lsp->nzlist.nz_type & N_EXT) && - !SET_ELEMENT_P(lsp->nzlist.nz_type)) { - warnx("internal error: `%s' N_EXT not set", g->name); - continue; - } - - errmsg = 0; - - if (!g->defined && !g->so_defined && list_unresolved_refs) { - /* Mark as being noted by relocation warning pass. */ - SET_BIT(nlist_bitvector, lsp - start_of_syms); - - if (g->undef_refs == 0) - reported_undefineds++; - 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; - - if (BIT_SET_P(nlist_bitvector, lsp - start_of_syms)) - continue; - - /* Mark as being noted by relocation warning pass. */ - SET_BIT(nlist_bitvector, lsp - 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(rp) + 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 nsym; - int i; - char *errfmt, *file_name; - int line_number; - int dont_allow_symbol_name; - u_char *nlist_bitvector; - struct line_debug_entry *text_scan, *data_scan; - - nsym = entry->nsymbols; - nlist_bitvector = (u_char *)alloca((nsym >> 3) + 1); - bzero(nlist_bitvector, (nsym >> 3) + 1); - - /* Read in the strings */ - entry->strings = (char *)alloca(entry->string_size); - read_entry_strings(file_open(entry), entry); - - if (!(entry->flags & E_DYNAMIC)) { - /* Do text warnings based on a scan through the reloc info. */ - do_relocation_warnings(entry, 0, outfile, nlist_bitvector); - - /* Do data warnings based on a scan through the reloc 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 < nsym; i++) { - struct nlist *np; - symbol *g; - - g = entry->symbols[i].symbol; - np = &entry->symbols[i].nzlist.nlist; - - if (g == NULL) - continue; - - if (!(np->n_type & N_EXT) && !SET_ELEMENT_P(np->n_type)) { - warnx("internal error: `%s' N_EXT not set", g->name); - continue; - } - - if (!(g->flags & GS_REFERENCED)) { -#if 0 - /* Check for undefined shobj symbols */ - struct localsymbol *lsp; - register int type; - - for (lsp = g->sorefs; lsp; lsp = lsp->next) { - type = lsp->nzlist.nz_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; - } - - dont_allow_symbol_name = 0; - - if (list_multiple_defs && g->mult_defs) { - - errfmt = "Definition of symbol `%s' (multiply defined)"; - switch (np->n_type) { - case N_TEXT | N_EXT: - line_number = - address_to_line(np->n_value, text_scan); - file_name = text_scan[0].filename; - break; - - case N_DATA | N_EXT: - line_number = - address_to_line(np->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->mult_defs == 2) - continue; - errfmt = - "First set element definition of symbol `%s' (multiply defined)"; - line_number = -1; - break; - - case N_SIZE | N_EXT: - errfmt = - "Size element definition of symbol `%s' (multiply defined)"; - line_number = -1; - break; - - case N_INDR | N_EXT: - errfmt = - "Alias definition of symbol `%s' (multiply defined)"; - line_number = -1; - break; - - case N_UNDF | N_EXT: - /* Don't print out multiple defs at references.*/ - continue; - - default: - warnx("%s: unexpected multiple definitions " - "of symbol `%s', type %#x", - get_file_name(entry), - g->name, np->n_type); - break; - } - - } else if (BIT_SET_P(nlist_bitvector, i)) { - continue; - } else if (list_unresolved_refs && - !g->defined && !g->so_defined) { - - if (g->undef_refs == 0) - reported_undefineds++; - 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->def_lsp && g->def_lsp->entry != entry && - !(entry->flags & E_DYNAMIC) && - g->def_lsp->entry->flags & E_SECONDCLASS) { - fprintf(outfile, - "%s: Undefined symbol `%s' referenced (use %s ?)\n", - get_file_name(entry), - g->name, - g->def_lsp->entry->local_sym_name); - continue; - } 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 (np->n_type != (N_EXT | N_UNDF) || - (entry->symbols[i].flags & LS_WARNING)) - continue; - - errfmt = g->warning; - line_number = -1; - dont_allow_symbol_name = 1; - } else - continue; - - if (line_number == -1) - fprintf(outfile, "%s: ", get_file_name(entry)); - 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 disappear anyway. */ -} - -int -do_warnings(outfile) - FILE *outfile; -{ - - list_unresolved_refs = !relocatable_output && - ( (undefined_global_sym_count - undefined_weak_sym_count) > 0 - || undefined_shobj_sym_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, (void *)outfile); - - if (list_unresolved_refs && - reported_undefineds != - (undefined_global_sym_count - undefined_weak_sym_count)) - warnx("Spurious undefined symbols: " - "# undefined symbols %d, reported %d", - (undefined_global_sym_count - undefined_weak_sym_count), - reported_undefineds); - - 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 deleted file mode 100644 index 632ad2287a49..000000000000 --- a/gnu/usr.bin/ld/xbits.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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 without 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.4 1994/01/29 02:03:18 jtc Exp $ - */ - -/* - * "Generic" byte-swap routines. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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__dynamic(dp) -struct _dynamic *dp; -{ - dp->d_version = md_swap_long(dp->d_version); - dp->d_debug = (struct so_debug *)md_swap_long((long)dp->d_debug); - dp->d_un.d_sdt = (struct section_dispatch_table *) - md_swap_long((long)dp->d_un.d_sdt); - dp->d_entry = (struct ld_entry *)md_swap_long((long)dp->d_entry); -} - -void -swap_section_dispatch_table(sdp) -struct section_dispatch_table *sdp; -{ - swap_longs((long *)sdp, sizeof(*sdp)/sizeof(long)); -} - -void -swap_so_debug(ddp) -struct so_debug *ddp; -{ - swap_longs((long *)ddp, sizeof(*ddp)/sizeof(long)); -} - -void -swapin_sod(sodp, n) -struct sod *sodp; -int n; -{ - unsigned long bits; - - for (; n; n--, sodp++) { - sodp->sod_name = md_swap_long(sodp->sod_name); - sodp->sod_major = md_swap_short(sodp->sod_major); - sodp->sod_minor = md_swap_short(sodp->sod_minor); - sodp->sod_next = md_swap_long(sodp->sod_next); - bits = ((unsigned long *)sodp)[1]; - sodp->sod_library = ((bits >> 24) & 1); - } -} - -void -swapout_sod(sodp, n) -struct sod *sodp; -int n; -{ - unsigned long bits; - - for (; n; n--, sodp++) { - sodp->sod_name = md_swap_long(sodp->sod_name); - sodp->sod_major = md_swap_short(sodp->sod_major); - sodp->sod_minor = md_swap_short(sodp->sod_minor); - sodp->sod_next = md_swap_long(sodp->sod_next); - bits = (unsigned long)(sodp->sod_library) << 24; - ((unsigned long *)sodp)[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); - } -} - diff --git a/libexec/ld.aout_so/Makefile b/libexec/ld.aout_so/Makefile index c8cf6de996b2..748c13101540 100644 --- a/libexec/ld.aout_so/Makefile +++ b/libexec/ld.aout_so/Makefile @@ -1,11 +1,10 @@ -# $NetBSD: Makefile,v 1.15 1997/03/24 22:06:36 christos Exp $ +# $NetBSD: Makefile,v 1.16 1997/04/16 16:49:43 christos Exp $ PROG= ld.so SRCS= mdprologue.S rtld.c malloc.c shlib.c etc.c md.c vfprintf.c MAN= rtld.1 -LDDIR?= $(.CURDIR)/.. PICFLAG=-fpic -fno-function-cse -CFLAGS+=-I$(LDDIR) -I$(.CURDIR) -I$(LDDIR)/$(MACHINE_ARCH) $(PICFLAG) -DRTLD -DLIBC_SCCS +CFLAGS+=$(PICFLAG) -DRTLD -DLIBC_SCCS ASFLAGS+=-k LDFLAGS+=-Bshareable -Bsymbolic -assert nosymbolic .if defined(DESTDIR) @@ -19,7 +18,7 @@ MLINKS= rtld.1 ld.so.1 INCS=${HDRS} INCSDIR=/usr/include -.PATH: $(LDDIR) $(LDDIR)/$(MACHINE_ARCH) ${.CURDIR}/../../../../lib/libc/stdio +.PATH: ${.CURDIR}/../../../../lib/libc/stdio $(PROG): $(LD) -o $(PROG) $(LDFLAGS) $(OBJS) $(LDADD) diff --git a/sbin/ldconfig/Makefile b/sbin/ldconfig/Makefile index 587dd766ab89..77965a627fb5 100644 --- a/sbin/ldconfig/Makefile +++ b/sbin/ldconfig/Makefile @@ -1,13 +1,9 @@ -# $NetBSD: Makefile,v 1.10 1995/03/06 04:24:41 cgd Exp $ +# $NetBSD: Makefile,v 1.11 1997/04/16 16:49:42 christos Exp $ PROG= ldconfig SRCS= ldconfig.c shlib.c etc.c -LDDIR?= $(.CURDIR)/.. -CFLAGS+=-I$(LDDIR) -I$(.CURDIR) -I$(LDDIR)/$(MACHINE_ARCH) LDSTATIC=-static BINDIR= /sbin MAN= ldconfig.8 -.PATH: $(LDDIR) $(LDDIR)/$(MACHINE_ARCH) - .include