⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ld.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 5 页
字号:
/*- * This code is derived from software copyrighted by the Free Software * Foundation. * * Modified 1991 by Donn Seeley at UUNET Technologies, Inc. */#ifndef lintstatic char sccsid[] = "@(#)ld.c	8.1 (Berkeley) 6/6/93";#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.  */   /* Define how to initialize system-dependent header fields.  */#include <ar.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/file.h>#include <sys/time.h>#include <sys/resource.h>#include <fcntl.h>#include <a.out.h>#include <stab.h>#include <string.h>/* symseg.h defines the obsolete GNU debugging format; we should nuke it.  */#define CORE_ADDR unsigned long	/* For symseg.h */#include "symseg.h"#define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)/* If compiled with GNU C, use the built-in alloca */#ifdef __GNUC__#define alloca __builtin_alloca#endif#define min(a,b) ((a) < (b) ? (a) : (b))/* Macro to control the number of undefined references printed */#define MAX_UREFS_PRINTED	10/* Size of a page; obtained from the operating system.  */int page_size;/* Name this program was invoked by.  */char *progname;/* System dependencies *//* Define this to specify the default executable format.  */#ifndef DEFAULT_MAGIC#define DEFAULT_MAGIC ZMAGIC#endif#if defined(hp300) || defined(luna68k)#define	INITIALIZE_HEADER	outheader.a_mid = MID_HP300#endif#ifdef sparc#ifndef sun#define sun 1#endif#define	INITIALIZE_HEADER \	(outheader.a_mid = MID_SUN_SPARC, outheader.a_toolversion = 1)#endif/* * Ok.  Following are the relocation information macros.  If your * system should not be able to use the default set (below), you must * define the following: *   relocation_info: This must be typedef'd (or #define'd) to the type * of structure that is stored in the relocation info section of your * a.out files.  Often this is defined in the a.out.h for your system. * *   RELOC_ADDRESS (rval): Offset into the current section of the * <whatever> to be relocated.  *Must be an lvalue*. * *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an * external symbol (1), or was it fully resolved upon entering the * loader (0) in which case some combination of the value in memory * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains * what the value of the relocation actually was.  *Must be an lvalue*. * *   RELOC_TYPE (rval): If this entry was fully resolved upon * entering the loader, what type should it be relocated as? * *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon * entering the loader, what is the index of it's symbol in the symbol * table?  *Must be a lvalue*. * *   RELOC_MEMORY_ADD_P (rval): This should return true if the final * relocation value output here should be added to memory, or if the * section of memory described should simply be set to the relocation * value. * *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives * an extra value to be added to the relocation value based on the * individual relocation entry.  *Must be an lvalue if defined*. * *   RELOC_PCREL_P (rval): True if the relocation value described is * pc relative. * *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the * final relocation value before putting it where it belongs. * *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could * do everything in terms of the bit operators below), but having this * macro could end up producing better code on machines without fancy * bit twiddling.  Also, it's easier to understand/code big/little * endian distinctions with this macro. * *   RELOC_TARGET_BITPOS (rval): The starting bit position within the * object described in RELOC_TARGET_SIZE in which the relocation value * will go. * *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced * with the bits of the relocation value.  It may be assumed by the * code that the relocation value will fit into this many bits.  This * may be larger than RELOC_TARGET_SIZE if such be useful. * * *		Things I haven't implemented *		---------------------------- * *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2. * *    Pc relative relocation for External references. * * *//* The following #if has been modifed for cross compilation *//* It originally read:  #if defined(sun) && defined(sparc)  *//* Marc Ullman, Stanford University    Nov. 1 1989  */#if defined(sun) && (TARGET == SUN4)/* Sparc (Sun 4) macros */#undef relocation_info#define relocation_info	                reloc_info_sparc#define RELOC_ADDRESS(r)		((r)->r_address)#define RELOC_EXTERN_P(r)               ((r)->r_extern)#define RELOC_TYPE(r)                   ((r)->r_index)#define RELOC_SYMBOL(r)                 ((r)->r_index)#define RELOC_MEMORY_SUB_P(r)		0#define RELOC_MEMORY_ADD_P(r)           0#define RELOC_ADD_EXTRA(r)              ((r)->r_addend)#define RELOC_PCREL_P(r)             \        ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)#define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])#define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])#define RELOC_TARGET_BITPOS(r)          0#define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])/* Note that these are very dependent on the order of the enums in   enum reloc_type (in a.out.h); if they change the following must be   changed *//* Also note that the last few may be incorrect; I have no information */static int reloc_target_rightshift[] = {  0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,};static int reloc_target_size[] = {  0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,};static int reloc_target_bitsize[] = {  8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,};#define	MAX_ALIGNMENT	(sizeof (double))#endif/* Default macros */#ifndef RELOC_ADDRESS#define RELOC_ADDRESS(r)		((r)->r_address)#define RELOC_EXTERN_P(r)		((r)->r_extern)#define RELOC_TYPE(r)		((r)->r_symbolnum)#define RELOC_SYMBOL(r)		((r)->r_symbolnum)#define RELOC_MEMORY_SUB_P(r)	0#define RELOC_MEMORY_ADD_P(r)	1#undef RELOC_ADD_EXTRA#define RELOC_PCREL_P(r)		((r)->r_pcrel)#define RELOC_VALUE_RIGHTSHIFT(r)	0#define RELOC_TARGET_SIZE(r)		((r)->r_length)#define RELOC_TARGET_BITPOS(r)	0#define RELOC_TARGET_BITSIZE(r)	32#endif#ifndef MAX_ALIGNMENT#define	MAX_ALIGNMENT	(sizeof (int))#endif#ifdef nounderscore#define LPREFIX '.'#else#define LPREFIX 'L'#endif#ifndef TEXT_START#define TEXT_START(x) N_TXTADDR(x)#endif/* 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.  *//* If a this type of symbol is encountered, its name is a warning   message to print each time the symbol referenced by the next symbol   table entry is referenced.   This feature may be used to allow backwards compatibility with   certain functions (eg. gets) but to discourage programmers from   their use.   So if, for example, you wanted to have ld print a warning whenever   the function "gets" was used in their C program, you would add the   following to the assembler file in which gets is defined:	.stabs "Obsolete function \"gets\" referenced",30,0,0,0	.stabs "_gets",1,0,0,0   These .stabs do not necessarily have to be in the same file as the   gets function, they simply must exist somewhere in the compilation.  */#ifndef N_WARNING#define N_WARNING 0x1E		/* Warning message to print if symbol				   included */#endif				/* This is input to ld */#ifndef __GNU_STAB__/* Line number for the data section.  This is to be used to describe   the source location of a variable declaration.  */#ifndef N_DSLINE#define N_DSLINE (N_SLINE+N_DATA-N_TEXT)#endif/* Line number for the bss section.  This is to be used to describe   the source location of a variable declaration.  */#ifndef N_BSLINE#define N_BSLINE (N_SLINE+N_BSS-N_TEXT)#endif#endif /* not __GNU_STAB__ *//* Symbol table *//* Global symbol data is recorded in these structures,   one for each global symbol.   They are found via hashing in 'symtab', which points to a vector of buckets.   Each bucket is a chain of these structures through the link field.  */typedef  struct glosym    {      /* Pointer to next symbol in this symbol's hash bucket.  */      struct glosym *link;      /* Name of this symbol.  */      char *name;      /* Value of this symbol as a global symbol.  */      long value;      /* Chain of external 'nlist's in files for this symbol, both defs	 and refs.  */      struct nlist *refs;      /* Any warning message that might be associated with this symbol         from an N_WARNING symbol encountered. */      char *warning;      /* Nonzero means definitions of this symbol as common have been seen,	 and the value here is the largest size specified by any of them.  */      int max_common_size;      /* For relocatable_output, records the index of this global sym in the	 symbol table to be written, with the first global sym given index 0.*/      int def_count;      /* Nonzero means a definition of this global symbol is known to exist.	 Library members should not be loaded on its account.  */      char defined;      /* Nonzero means a reference to this global symbol has been seen	 in a file that is surely being loaded.	 A value higher than 1 is the n_type code for the symbol's	 definition.  */      char referenced;      /* A count of the number of undefined references printed for a	 specific symbol.  If a symbol is unresolved at the end of	 digest_symbols (and the loading run is supposed to produce	 relocatable output) do_file_warnings keeps track of how many	 unresolved reference error messages have been printed for	 each symbol here.  When the number hits MAX_UREFS_PRINTED,	 messages stop. */      unsigned char undef_refs;      /* 1 means that this symbol has multiple definitions.  2 means         that it has multiple definitions, and some of them are set	 elements, one of which has been printed out already.  */      unsigned char multiply_defined;      /* Nonzero means print a message at all refs or defs of this symbol */      char trace;    }  symbol;/* Demangler for C++. */extern char *cplus_demangle ();/* Demangler function to use. */char *(*demangler)() = NULL;/* Number of buckets in symbol hash table */#define	TABSIZE	1009/* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */symbol *symtab[TABSIZE];/* Number of symbols in symbol hash table. */int num_hash_tab_syms = 0;/* Count the number of nlist entries that are for local symbols.   This count and the three following counts   are incremented as as symbols are entered in the symbol table.  */int local_sym_count;/* Count number of nlist entries that are for local symbols   whose names don't start with L. */int non_L_local_sym_count;/* Count the number of nlist entries for debugger info.  */int debugger_sym_count;/* Count the number of global symbols referenced and not defined.  */int undefined_global_sym_count;/* Count the number of global symbols multiply defined.  */int multiple_def_count;/* Count the number of defined global symbols.   Each symbol is counted only once   regardless of how many different nlist entries refer to it,   since the output file will need only one nlist entry for it.   This count is computed by `digest_symbols';   it is undefined while symbols are being loaded. */int defined_global_sym_count;/* Count the number of symbols defined through common declarations.   This count is kept in symdef_library, linear_library, and   enter_global_ref.  It is incremented when the defined flag is set   in a symbol because of a common definition, and decremented when   the symbol is defined "for real" (ie. by something besides a common

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -