home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GEMini Atari
/
GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso
/
zip
/
gnu
/
utlsrc33.lzh
/
UTLSRC33
/
LD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-30
|
179KB
|
6,411 lines
/* 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. */
#ifdef BYTE_SWAP
#define SWAP4(y) (((unsigned)(y)>>24) + (((unsigned)(y)>>8)&0xff00) + \
(((unsigned)(y)<<8)&0xff0000) + ((unsigned)(y)<<24))
#define SWAP2(y) ((((unsigned)(y)&0xff00)>>8) + (((unsigned)(y)&0x00ff)<<8))
#endif /* BYTE_SWAP */
#ifdef CROSSATARI
# ifdef atarist
# undef atarist
# endif
# ifdef atariminix
# undef atariminix
# endif
#endif
#include "PatchLev.h"
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
#include <ar.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#ifdef COFF_ENCAPSULATE
#include "a.out.encap.h"
#else
#include <a.out.h>
#endif
#else
#ifdef CROSSATARI
# include "gnu-out.h"
# include "gnu-ar.h"
# ifdef MINIX
# include "minix-out.h"
# else
# include "st-out.h"
# endif /* MINIX */
# include <stdio.h>
# include <sys/types.h>
# include <string.h>
# include <sys/stat.h>
# include <sys/file.h>
# include <ctype.h>
# ifndef __GNUC__
# include <time.h>
# endif
#else
# ifdef atariminix
# include <stdio.h>
# include <sys/types.h>
# include <string.h>
# include <sys/stat.h>
# include <ctype.h>
# if 0
/* if you are *not* using a kernel with */
/* simon poole fs patches use this */
# define EMUL_OPEN3
# include "open3.h"
# else
# include <fcntl.h>
# endif
# include "gnu-out.h"
# include "minix-out.h"
# include "gnu-ar.h" /* we use the Gnu ar for minix, not minix ar */
# else /* atarist */
# include "gnu-out.h"
# include <st-out.h>
# include "gnu-ar.h"
# include <stdio.h>
# include <types.h>
# include <string.h>
# include <stat.h>
# include <file.h>
# include <ctype.h>
# include <unistd.h>
# endif
#endif /* atarist */
#endif
/* If compiled with GNU C, use the built-in alloca */
#ifdef __GNUC__
# ifndef atariminix
# define alloca __builtin_alloca
# endif
#endif
/* Always use the GNU version of debugging symbol type codes, if possible. */
#include "stab.h"
#define CORE_ADDR unsigned long /* For symseg.h */
#include "symseg.h"
#ifdef USG
#include <string.h>
#else
#include <strings.h>
#endif
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
/* Determine whether we should attempt to handle (minimally)
N_BINCL and N_EINCL. */
#if (!(defined(atarist) || defined(atariminix) || defined(CROSSATARI)))
#if defined (__GNU_STAB__) || defined (N_BINCL)
#define HAVE_SUN_STABS
#endif
#endif
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
/* stuff used when emitting reloc info here */
#define RBUF_SIZE 1024
#ifdef CROSSATARI
unsigned char rbuf[RBUF_SIZE];
#else
char rbuf[RBUF_SIZE];
#endif
int rbuf_size = 0; /* how many bytes are in there now */
long rbuf_last_pc = -1; /* last rel pc; -1 means none yet */
#ifdef atarist /* and a stack */
long _stksize = -1L;
#endif
#endif
#ifdef min
#undef min
#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 if names etext, edata and end should not start with `_'. */
/* #define nounderscore 1 */
/* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
whose native format is different. */
/* #define NON_NATIVE */
/* Define this to specify the default executable format. */
#ifndef CROSSHPUX
#ifdef hpux
#define DEFAULT_MAGIC NMAGIC /* hpux bugs screw ZMAGIC */
#endif
#endif
#ifndef DEFAULT_MAGIC
#define DEFAULT_MAGIC ZMAGIC
#endif
/* Ordinary 4.3bsd lacks these macros in a.out.h. */
#ifndef N_TXTADDR
#ifdef vax
#define N_TXTADDR(X) 0
#endif
#ifdef is68k
#define N_TXTADDR(x) (sizeof (struct exec))
#endif
#endif
#ifndef N_DATADDR
#ifdef vax
#define N_DATADDR(x) \
(((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
: (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
#endif
#ifdef is68k
#define SEGMENT_SIZE 0x20000
#define N_DATADDR(x) \
(((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
: (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
#endif
#endif
#ifndef CROSSHPUX
#ifdef hpux
#define getpagesize() EXEC_PAGESIZE
#endif
#endif
#if (defined(atarist) || defined(atariminix) || defined(CROSSATARI))
/* kludgerama, not really used */
#define getpagesize() 2
#endif
/* Define how to initialize system-dependent header fields. */
#ifndef CROSSATARI
#ifdef sun
#ifdef sparc
#define INITIALIZE_HEADER \
{outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
#endif
#if defined(mc68010) || defined(m68010) || (TARGET == SUN2)
#define INITIALIZE_HEADER outheader.a_machtype = M_68010
#endif
#ifndef INITIALIZE_HEADER
#define INITIALIZE_HEADER outheader.a_machtype = M_68020
#endif
#endif
#ifdef is68k
#ifdef M_68020
/* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
a_machtype field and so won't recognize the magic number. To keep
binary compatibility for now, just ignore it */
#define INITIALIZE_HEADER outheader.a_machtype = 0;
#endif
#endif
#ifndef CROSSHPUX
#ifdef hpux
#define INITIALIZE_HEADER outheader.a_machtype = HP9000S200_ID
#endif
#endif
#ifdef i386
#define INITIALIZE_HEADER outheader.a_machtype = M_386
#endif
#endif /* CROSSATARAI */
#ifdef is68k
/* This enables code to take care of an ugly hack in the ISI OS.
If a symbol beings with _$, then the object file is included only
if the rest of the symbol name has been referenced. */
#define DOLLAR_KLUDGE
#endif
/*
* Alloca include.
*/
#if defined(sun) && defined(sparc)
#ifdef alloca
#undef alloca
#endif
#include <alloca.h>
#endif
#ifndef L_SET
#define L_SET 0
#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.
*
* 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.
*
*
*/
#if defined(sun) && defined(sparc) && !defined(CROSSATARI)
/* 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_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,
};
#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_ADD_P(r) 1
/* #define RELOC_ADD_EXTRA(r) 0 */ /* Don't need to define */
#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
#if defined(atarist) || defined(CROSSATARI)
#define RELOC_ADD_EXTRA(r) \
((!RELOC_PCREL_P(r) && RELOC_TARGET_SIZE(r) < 2) \
? BASE_OFFSET-text_size : 0)
#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. */
#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 text 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 file 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;
/* 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)() = cplus_demangle;
/* 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 defined global symbols.
Each symbol is counted only once
regardless of how many different nlist entries refer to it,
since the output file will need only one nlist entry for it.
This count is computed by `digest_symbols';
it is undefined while symbols are being loaded. */
int defined_global_sym_count;
/* Count the number of symbols defined through common declarations.
This count is kept in symdef_library, linear_library, and
enter_global_ref. It is incremented when the defined flag is set
in a symbol because of a common definition, and decremented when
the symbol is defined "for real" (ie. by something besides a common
definition). */
int common_defined_global_count;
/* Count the number of set element type symbols and the number of
separate vectors which these symbols will fit into. See the
GNU a.out.h for more info.
This count is computed by 'enter_file_symbols' */
int set_symbol_count;
int set_vector_count;
/* Count the number of definitions done indirectly (ie. done relative
to the value of some other symbol. */
int global_indirect_count;
/* Count the number of warning symbols encountered. */
int warning_count;
/* Total number of symbols to be written in the output file.
Computed by digest_symbols from the variables above. */
int nsyms;
/* Nonzero means ptr to symbol entry for symbol to use as start addr.
-e sets this. */
symbol *entry_symbol;
symbol *edata_symbol; /* the symbol _edata */
symbol *etext_symbol; /* the symbol _etext */
symbol *end_symbol; /* the symbol _end */
/* Each input file, and each library member ("subfile") being loaded,
has a `file_entry' structure for it.
For files specified by command args, these are contained in the vector
which `file_table' points to.
For library members, they are dynamically allocated,
and chained through the `chain' field.
The chain is found in the `subfiles' field of the `file_entry'.
The `file_entry' objects for the members have `superfile' fields pointing
to the one for the library. */
struct file_entry {
/* Name of this file. */
char *filename;
/* Name to use for the symbol giving address of text start */
/* Usually the same as filename, but for a file spec'd with -l
this is the -l switch itself rather than the filename. */
char *local_sym_name;
/* Describe the layout of the contents of the file */
/* The file's a.out header. */
struct exec header;
/* Offset in file of GDB symbol segment, or 0 if there is none. */
int symseg_offset;
/* Describe data from the file loaded into core */
/* Symbol table of the file. */
struct nlist *symbols;
/* Size in bytes of string table. */
int string_size;
/* Pointer to the string table.
The string table is not kept in core all the time,
but when it is in core, its address is here. */
char *strings;
/* Pointer to any warning specified in this file by an N_WARNING
type symbol */
char *warning;
/* Next two used only if `relocatable_output' or if needed for */
/* output of undefined reference line numbers. */
/* Text reloc info saved by `write_text' for `coptxtrel'. */
struct relocation_info *textrel;
/* Data reloc info saved by `write_data' for `copdatrel'. */
struct relocation_info *datarel;
/* Relation of this file's segments to the output file */
/* Start of this file's text seg in the output file core image. */
int text_start_address;
/* Start of this file's data seg in the output file core image. */
int data_start_address;
/* Start of this file's bss seg in the output file core image. */
int bss_start_address;
/* Offset in bytes in the output file symbol table of the first
local symbol for this file. Set by `write_file_symbols'. */
int local_syms_offset;
/* For library members only */
/* For a library, points to chain of entries for the library members. */
struct file_entry *subfiles;
/* For a library member, offset of the member within the archive.
Zero for files that are not library members. */
int starting_offset;
/* Size of contents of this file, if library member. */
int total_size;
/* For library member, points to the library's own entry. */
struct file_entry *superfile;
/* For library member, points to next entry for next member. */
struct file_entry *chain;
/* 1 if file is a library. */
char library_flag;
/* 1 if file's header has been read into this structure. */
char header_read_flag;
/* 1 means search a set of directories for this file. */
char search_dirs_flag;
/* 1 means this is base file of incremental load.
Do not load this file's text or data.
Also default text_start to after this file's bss. */
char just_syms_flag;
};
/* Vector of entries for input files specified by arguments.
These are all the input files except for members of specified libraries. */
struct file_entry *file_table;
/* Length of that vector. */
int number_of_files;
/* When loading the text and data, we can avoid doing a close
and another open between members of the same library.
These two variables remember the file that is currently open.
Both are NULL if no file is open.
See `each_file' and `file_close'. */
struct file_entry *input_file;
FILE *input_desc = NULL;
/* The name of the file to write; "a.out" by default. */
char *output_filename;
/* Descriptor for writing that file with `mywrite'. */
FILE *outdesc = NULL;
/* Header for that file (filled in by `write_header'). */
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
struct aexec outheader;
#else
struct exec outheader;
#endif
#ifdef COFF_ENCAPSULATE
struct coffheader coffheader;
int need_coff_header;
#endif
/* The following are computed by `digest_symbols'. */
int text_size; /* total size of text of all input files. */
int data_size; /* total size of data of all input files. */
int bss_size; /* total size of bss of all input files. */
int text_reloc_size; /* total size of text relocation of all input files. */
int data_reloc_size; /* total size of data relocation of all input */
/* files. */
/* Specifications of start and length of the area reserved at the end
of the text segment for the set vectors. Computed in 'digest_symbols' */
int set_sect_start;
int set_sect_size;
/* Pointer for in core storage for the above vectors, before they are
written. */
unsigned long *set_vectors;
/* Amount of cleared space to leave between the text and data segments. */
int text_pad;
/* Amount of bss segment to include as part of the data segment. */
int data_pad;
/* Format of __.SYMDEF:
First, a longword containing the size of the 'symdef' data that follows.
Second, zero or more 'symdef' structures.
Third, a word containing the length of symbol name strings.
Fourth, zero or more symbol name strings (each followed by a zero). */
struct symdef {
int symbol_name_string_index;
int library_member_offset;
};
/* Record most of the command options. */
/* Address we assume the text section will be loaded at.
We relocate symbols and text and data for this, but we do not
write any padding in the output file for it. */
int text_start;
/* Offset of default entry-pc within the text section. */
int entry_offset;
/* Address we decide the data section will be loaded at. */
int data_start;
/* `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;
/* Magic number to use for the output file, set by switch. */
#ifdef MINIX
long magic;
#else
int magic;
#endif /* MINIX */
/* Nonzero means print names of input files as processed. */
int trace_files;
/* 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;
/* 1 => write load map. */
int write_map;
/* 1 => write relocation into output file so can re-input it later. */
int relocatable_output;
/* 1 => assign space to common symbols even if `relocatable_output'. */
int force_common_definition;
/* 1 => atari minix special, write symbol table for mdb in out.h format */
#if (defined(MINIX) || defined(atariminix))
/* 1 => write out.h format output file too */
int write_minix_out = 0;
FILE *minix_out_filep;
char minix_out_filename[80]; /* name = strcat(output_filename, ".sym") */
void do_write_minix_out();
#endif
/* Standard directories to search for files specified by -l. */
#ifdef atarist
char *standard_search_dirs[] = {"\\gnu\\lib", "\\gnu"};
#else
#ifdef CROSSATARI
char *standard_search_dirs[] = {CROSSLIB};
#else
#ifdef atariminix
char *standard_search_dirs[] = {"/usr/local/lib"};
#else
char *standard_search_dirs[] = {"/lib", "/usr/lib", "/usr/local/lib"};
#endif
#endif
#endif
#if defined(atarist) || defined(CROSSATARI)
/* Write GST compatible symboltable. This format allows the symbols
to be upto 22 bytes long */
int gst_symboltable;
/* This variable count the number of extended symboltable entries.
This is done to correct the size of the symboltable in the header. */
int gst_symbols;
#endif
/* Actual vector of directories to search;
this contains those specified with -L plus the standard ones. */
char **search_dirs;
/* Length of the vector `search_dirs'. */
int n_search_dirs;
/* Non zero means to create the output executable. */
/* Cleared by nonfatal errors. */
int make_executable;
/* Force the executable to be output, even if there are non-fatal
errors */
int force_executable;
/* Keep a list of any symbols referenced from the command line (so
that error messages for these guys can be generated). This list is
zero terminated. */
struct glosym **cmdline_references;
int cl_refs_allocated;
/*
* Structure for communication between do_file_warnings and it's
* helper routines. Will in practice be an array of three of these:
* 0) Current line, 1) Next line, 2) Source file info.
*/
struct line_debug_entry
{
int line;
char *filename;
struct nlist *sym;
};
#if defined(__STDC__) || defined(__cplusplus)
# define P_(s) s
#else
# define P_(s) ()
#endif
/* ld.c */
int main P_((int argc, char **argv));
int classify_arg P_((char *arg));
int next_indirect_name P_((FILE *f, char *buf));
int decode_indirect_file P_((char *iname));
struct file_entry *process_indirect_file P_((struct file_entry *p, char *iname));
void decode_command P_((int argc, char **argv));
void add_cmdline_ref P_((struct glosym *sp));
void decode_option P_((char *swt, char *arg));
void each_file P_((void (*function )(), void *arg));
unsigned long check_each_file P_((unsigned long (*function )(), void *arg));
void each_full_file P_((void (*function )(), void *arg));
void file_close P_((void));
FILE *file_open P_((struct file_entry *entry));
void prline_file_name P_((struct file_entry *entry, FILE *outfile));
void print_file_name P_((struct file_entry *entry, FILE *outfile));
char *get_file_name P_((struct file_entry *entry));
void read_header P_((FILE *desc, struct file_entry *entry));
void read_entry_symbols P_((FILE *desc, struct file_entry *entry));
void read_entry_strings P_((FILE *desc, struct file_entry *entry));
void load_symbols P_((void));
void read_file_symbols P_((struct file_entry *entry));
void enter_file_symbols P_((struct file_entry *entry));
void enter_global_ref P_((struct nlist *nlist_p, char *name, struct file_entry *entry));
unsigned long contains_symbol P_((struct file_entry *entry, struct nlist *n_ptr));
void search_library P_((FILE *desc, struct file_entry *entry));
struct file_entry *decode_library_subfile P_((FILE *desc, struct file_entry *library_entry, int subfile_offset, int *length_loc));
void symdef_library P_((FILE *desc, struct file_entry *entry, int member_length));
void linear_library P_((FILE *desc, struct file_entry *entry));
int subfile_wanted_p P_((struct file_entry *entry));
void digest_symbols P_((void));
void consider_file_section_lengths P_((struct file_entry *entry));
void relocate_file_addresses P_((struct file_entry *entry));
void print_symbols P_((FILE *outfile));
void describe_file_sections P_((struct file_entry *entry, FILE *outfile));
void list_file_locals P_((struct file_entry *entry, FILE *outfile));
void do_file_warnings P_((struct file_entry *entry, FILE *outfile));
int relocation_entries_relation P_((struct relocation_info *rel1, struct relocation_info *rel2));
int next_debug_entry P_((int use_data_symbols, struct line_debug_entry state_pointer[]));
int address_to_line P_((unsigned long address, struct line_debug_entry state_pointer[]));
struct line_debug_entry *init_debug_scan P_((int use_data_symbols, struct file_entry *entry));
void mark_flagged_symbols P_((struct file_entry *entry, FILE *outfile));
void do_warnings P_((FILE *outfile));
void print_files_defining_symbol P_((struct nlist *first_def, struct nlist *rest_of_refs));
void write_output P_((void));
void write_header P_((void));
void write_text P_((void));
int text_offset P_((struct file_entry *entry));
void read_relocation P_((void));
void read_file_relocation P_((struct file_entry *entry));
void copy_text P_((struct file_entry *entry));
void write_data P_((void));
void copy_data P_((struct file_entry *entry));
void perform_relocation P_((char *data, int pc_relocation, int data_size,
struct relocation_info *reloc_info, int reloc_size,
struct file_entry *entry, int pure_text));
void write_rel P_((void));
void flush_atari_rbuf P_((void));
int reloc_less_p P_((struct relocation_info *reloc_1, struct relocation_info *reloc_2));
void emit_atari_text_relocs P_((struct file_entry *entry));
void emit_atari_data_relocs P_((struct file_entry *entry));
void emit_atari_set_vectors P_((void));
void coptxtrel P_((struct file_entry *entry));
void copdatrel P_((struct file_entry *entry));
void emit_atari_reloc_byte P_((unsigned int b));
int emit_atari_reloc P_((long rel_pc));
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
#if (!(defined(MINIX) || defined(atariminix)))
void write_atari_sym P_((struct nlist *p, char *str));
#else
void write_atari_sym P_((struct nlist *p, char *str, int gflag));
#endif
void write_atari_syms P_((struct file_entry *entry, int *syms_written_addr));
#else /* for Gnu/Unix */
void write_string_table P_((void));
void write_file_syms P_((struct file_entry *entry, int *syms_written_addr));
#endif /* (defined(CROSSATARI) || defined(atarist) || defined(atariminix)) */
void write_syms P_((void));
int assign_string_table_index P_((char *name));
void write_symsegs P_((void));
void write_file_symseg P_((struct file_entry *entry));
void symtab_init P_((void));
int hash_string P_((char *key));
symbol * getsym P_((char *key));
symbol * getsym_soft P_((char *key));
void fatal P_((char *string, ...));
void fatal_with_file P_((char *string, struct file_entry *entry));
void perror_name P_((char *name));
void perror_file P_((struct file_entry *entry));
void mywrite P_((void *buf, int count, int eltsize, FILE *desc));
void padfile P_((int padding, FILE *outdesc));
char *concat P_((char *s1, char *s2, char *s3));
int parse P_((char *arg, char *format, char *error));
void *xmalloc P_((int size));
void *xrealloc P_((void *ptr, int size));
int write_minix_sym P_((struct nlist *p, char *str, int gflag, int fflag));
void write_minix_syms P_((struct file_entry *entry, int *syms_written_addr));
void do_write_minix_out P_((void));
unsigned short minix_swap_short P_((unsigned char s[]));
long minix_swap_long P_((unsigned char s[]));
#undef P_
#ifndef bcopy
void bcopy (), bzero ();
#endif
void *malloc (), *realloc ();
#ifndef alloca
int alloca ();
#endif
int free ();
void error ();
int
main (argc, argv)
char **argv;
int argc;
{
#ifdef atarist
_binmode(1);
#endif
page_size = getpagesize ();
progname = argv[0];
/* Clear the cumulative info on the output file. */
text_size = 0;
data_size = 0;
bss_size = 0;
text_reloc_size = 0;
data_reloc_size = 0;
data_pad = 0;
text_pad = 0;
/* Initialize the data about options. */
#if defined(atarist) || defined(CROSSATARI)
gst_symboltable = 0;
gst_symbols = 0;
#endif
specified_data_size = 0;
strip_symbols = STRIP_NONE;
trace_files = 0;
discard_locals = DISCARD_NONE;
entry_symbol = 0;
write_map = 0;
relocatable_output = 0;
force_common_definition = 0;
T_flag_specified = 0;
Tdata_flag_specified = 0;
magic = DEFAULT_MAGIC;
make_executable = 1;
force_executable = 0;
/* Initialize the cumulative counts of symbols. */
local_sym_count = 0;
non_L_local_sym_count = 0;
debugger_sym_count = 0;
undefined_global_sym_count = 0;
set_symbol_count = 0;
set_vector_count = 0;
global_indirect_count = 0;
warning_count = 0;
common_defined_global_count = 0;
/* Keep a list of symbols referenced from the command line */
cl_refs_allocated = 10;
cmdline_references =
(struct glosym **) xmalloc (cl_refs_allocated
* sizeof(struct glosym *));
*cmdline_references = 0;
/* Completely decode ARGV. */
decode_command (argc, argv);
#if (defined(MINIX) || defined(atariminix))
if(write_minix_out == 1)
{
strcpy(minix_out_filename, output_filename);
strcat(minix_out_filename, ".sym");
/* if(strlen(minix_out_filename) - strlen(output_filename) > 14)
then you are up s*it creek;
*/
}
#endif
/* Create the symbols `etext', `edata' and `end'. */
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
if (!relocatable_output)
#endif /* always do this on atari */
symtab_init ();
/* Determine whether to count the header as part of
the text size, and initialize the text size accordingly.
This depends on the kind of system and on the output format selected. */
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
outheader.a_magic = CMAGIC;
#else
outheader.a_magic = magic;
#endif
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
#ifdef INITIALIZE_HEADER
INITIALIZE_HEADER;
#endif
#endif
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
text_size = sizeof (struct aexec);
#else
text_size = sizeof (struct exec);
#endif
#ifdef COFF_ENCAPSULATE
if (relocatable_output == 0)
{
need_coff_header = 1;
/* set A_ENCAP now, since it will change the values of N_TXTOFF, etc */
outheader.a_flags |= A_ENCAP;
text_size += sizeof (struct coffheader);
}
#endif
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
text_size -= A_TXTOFF (outheader);
#else
text_size -= N_TXTOFF (outheader);
#endif
if (text_size < 0)
text_size = 0;
entry_offset = text_size;
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
if (!T_flag_specified && !relocatable_output)
text_start = N_TXTADDR (outheader);
#endif
/* 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 */
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);
}
void decode_option ();
/* Analyze a command line argument.
Return 0 if the argument is a filename.
Return 1 if the argument is a option complete in itself.
Return 2 if the argument is a option which uses an argument.
Thus, the value is the number of consecutive arguments
that are part of options. */
int
classify_arg (arg)
register char *arg;
{
if (*arg != '-') return 0;
switch (arg[1])
{
case 'A':
case 'D':
case 'e':
case 'L':
case 'o':
case 'u':
case 'y':
if (arg[2])
return 1;
return 2;
case 'l':
if (arg[2])
return 1;
return 2;
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;
}
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
/* things to grok indirect files */
int next_indirect_name(f, buf)
FILE * f;
char * buf;
{
char c;
char * s = buf;
for (*buf = '\0' ; (((c = fgetc(f)) != EOF) && (isspace(c))) ; )
; /* skip whitespace */
if (c == EOF) return(0); /* lose */
*s++ = c;
for ( ; (((c = fgetc(f)) != EOF) && (!isspace(c))) ; )
*s++ = c;
*s = '\0'; /* finish it */
return((strlen(buf) > 0)); /* win if saw any */
}
int decode_indirect_file(iname)
/* (declare (values n-new-files)) */
char * iname;
{
char fn[80]; /* name buffer */
int i;
FILE * ifile;
#ifdef DEBUG
fprintf(stderr, "decode_indirect_file('%s')\n", iname);
#endif
if ((ifile = fopen(iname, "r")) == NULL)
{
fprintf (stderr, "Can't open indirect file '%s'\n", iname);
return(0);
}
for (i = 0 ; next_indirect_name(ifile, fn) ; )
i++; /* count files */
fclose(ifile);
#ifdef DEBUG
fprintf(stderr, " ->%d\n", i);
#endif
return(i); /* return file count */
}
struct file_entry * process_indirect_file(p, iname)
/* (declare (values updated_pointer)) */
struct file_entry * p;
char * iname;
{
char fn[80]; /* name buffer */
FILE * ifile;
#ifdef DEBUG
fprintf(stderr, "process_indirect_file(%X, '%s')\n", p, iname);
#endif
if ((ifile = fopen(iname, "r")) == NULL)
{
fprintf (stderr, "Can't open indirect file '%s'\n", iname);
return(0);
}
for ( ; next_indirect_name(ifile, fn) ; )
{
#ifdef DEBUG
fprintf(stderr, " file '%s'\n", fn);
#endif
p->filename = concat(fn, "", "");
p->local_sym_name = p->filename;
p++;
}
fclose(ifile);
#ifdef DEBUG
fprintf(stderr, " ->%X\n", p);
#endif
return(p); /* return new pointer */
}
#endif
/* Process the command arguments,
setting up file_table with an entry for each input file,
and setting variables according to the options. */
void
decode_command (argc, argv)
char **argv;
int argc;
{
register int i;
register struct file_entry *p;
number_of_files = 0;
output_filename = "a.out";
n_search_dirs = 0;
search_dirs = (char **) xmalloc (sizeof (char *));
/* First compute number_of_files so we know how long to make file_table. */
/* Also process most options completely. */
for (i = 1; i < argc; i++)
{
register int code = classify_arg (argv[i]);
if (code)
{
if (i + code > argc)
fatal ("no argument following %s\n", argv[i]);
decode_option (argv[i], argv[i+1]);
if (argv[i][1] == 'l' || argv[i][1] == 'A')
number_of_files++;
i += code - 1;
}
else
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
if (argv[i][0] == '@') /* indirect file? */
number_of_files += decode_indirect_file(&argv[i][1]);
else
#endif
number_of_files++;
}
if (!number_of_files)
fatal ("no input files", 0);
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++)
{
register int code = classify_arg (argv[i]);
if (code)
{
char *string;
if (code == 2)
string = argv[i+1];
else
string = &argv[i][2];
if (argv[i][1] == 'A')
{
if (p != file_table)
fatal (
"-A specified before an input file other than the first", 0);
p->filename = string;
p->local_sym_name = string;
p->just_syms_flag = 1;
p++;
}
if (argv[i][1] == 'l')
{
#if (defined(atarist) || (defined(CROSSATARI) && (!defined(MINIX))))
{
char * ext = rindex(string, '.');
if (ext)
p->filename = concat(string, "", ""); /* just use it */
else
p->filename = concat(string, "", ".olb");
}
#else
p->filename = concat ("lib", string, ".a");
#endif
p->local_sym_name = concat ("-l", string, "");
p->search_dirs_flag = 1;
p++;
}
i += code - 1;
}
else
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
if (argv[i][0] == '@')
p = process_indirect_file(p, &argv[i][1]);
else
#endif
{
p->filename = argv[i];
p->local_sym_name = argv[i];
p++;
}
}
/* Now check some option settings for consistency. */
if ((magic == ZMAGIC || magic == NMAGIC)
&& (text_start - text_start_alignment) & (page_size - 1))
fatal ("-T argument not multiple of page size, with sharable output", 0);
#if (defined(atarist) || defined(atariminix) || defined(CROSSATARI))
{
/* see if there's an env var that says where to search for things */
extern char *getenv();
char *value = getenv("GNULIB");
/* Allow GNULIB to contain more than one directory, so you can take
* advantage to your lib RAM-Disk/Harddisk/Floppy(;-)
* The dirs are delimited by ';' or ','. [br]
*/
#ifdef DEBUG
fprintf(stderr, "env->%X\n", value);
#endif
if (value) {
char *glib = (char *)alloca(strlen(value) + 1), *cp, *cpp;
strcpy(glib, value);
#ifdef DEBUG
fprintf(stderr, "glib->'%s'\n", glib);
#endif
cp = cpp = glib;
while( *cpp != '\0' ) {
#ifdef atarist
while( (*cp != ';') && (*cp != ',') && (*cp != '\0') )
#else
while( (*cp != ':') && (*cp != '\0') )
#endif
cp++;
if( *cp != '\0' )
*cp++ = '\0'; /* terminate string and advance to next */
/* else "*(cpp = cp) = '\0'" =>> end */
n_search_dirs++;
search_dirs =
(char **) xrealloc((char *)search_dirs,
n_search_dirs*sizeof(char *));
search_dirs[n_search_dirs-1] = concat(cpp, "", "");
#ifdef DEBUG
fprintf(stderr, "... '%s'\n", search_dirs[n_search_dirs-1]);
#endif
cpp = cp; /* ptr to next entry or '\0' (see above) */
}
}
}
#endif /* Atari ST special */
/* Append the standard search directories to the user-specified ones. */
{
int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
n_search_dirs += n;
search_dirs
= (char **) xrealloc ((char *)search_dirs,
n_search_dirs * sizeof (char *));
bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
n * sizeof (char *));
}
}
void
add_cmdline_ref (sp)
struct glosym *sp;
{
struct glosym **ptr;
for (ptr = cmdline_references;
ptr < cmdline_references + cl_refs_allocated && *ptr;
ptr++)
;
if (ptr == cmdline_references + cl_refs_allocated)
{
int diff = ptr - cmdline_references;
cl_refs_allocated *= 2;
cmdline_references = (struct glosym **)
xrealloc ((char *)cmdline_references,
cl_refs_allocated * sizeof (struct glosym *));
ptr = cmdline_references + diff;
}
*ptr++ = sp;
*ptr = (struct glosym *) 0;
}
int parse ();
/* Record an option and arrange to act on it later.
ARG should be the following command argument,
which may or may not be used by this option.
The `l' and `A' options are ignored here since they actually
specify input files. */
void
decode_option (swt, arg)
register char *swt, *arg;
{
if (! strcmp (swt + 1, "Ttext"))
{
text_start = parse (arg, "%x", "invalid argument to -Ttext");
T_flag_specified = 1;
return;
}
if (! strcmp (swt + 1, "Tdata"))
{
data_start = parse (arg, "%x", "invalid argument to -Tdata");
Tdata_flag_specified = 1;
return;
}
if (! strcmp (swt + 1, "noinhibit-exec"))
{
force_executable = 1;
return;
}
if (swt[2] != 0)
arg = &swt[2];
switch (swt[1])
{
case 'A':
return;
case 'D':
specified_data_size = parse (arg, "%x", "invalid argument to -D");
return;
case 'd':
force_common_definition = 1;
return;
case 'e':
entry_symbol = getsym (arg);
if (!entry_symbol->defined && !entry_symbol->referenced)
undefined_global_sym_count++;
entry_symbol->referenced = 1;
add_cmdline_ref (entry_symbol);
return;
#if defined(atarist) || defined(CROSSATARI)
case 'G':
gst_symboltable = 1;
return;
#endif
case 'l':
return;
case 'L':
n_search_dirs++;
search_dirs
= (char **) xrealloc ((char *)search_dirs,
n_search_dirs * sizeof (char *));
search_dirs[n_search_dirs - 1] = arg;
return;
case 'M':
write_map = 1;
return;
case 'N':
magic = OMAGIC;
return;
case 'n':
magic = NMAGIC;
return;
case 'o':
output_filename = arg;
return;
case 'r':
#if !(defined(atarist) || defined(CROSSATARI) || defined(atariminix))
relocatable_output = 1;
magic = OMAGIC;
text_start = 0;
#endif
return;
case 'S':
strip_symbols = STRIP_DEBUGGER;
return;
case 's':
strip_symbols = STRIP_ALL;
return;
case 'T':
text_start = parse (arg, "%x", "invalid argument to -T");
T_flag_specified = 1;
return;
case 't':
trace_files = 1;
return;
case 'u':
{
register symbol *sp = getsym (arg);
if (!sp->defined && !sp->referenced)
undefined_global_sym_count++;
sp->referenced = 1;
add_cmdline_ref (sp);
}
return;
#if defined(atarist) || defined(CROSSATARI)
case 'v':
#ifndef __GNUC__
{
extern time_t time();
extern char *ctime();
time_t t = time(0L);
fprintf (stderr, "`gcc-ld' cross-linker Patchlevel %s for GNU, Atari ST version (%s)\n",
PatchLevel, ctime(&t));
}
#else
fprintf (stderr, "`gcc-ld' linker Patchlevel %s for GNU, Atari ST version (%s %s)\n",
PatchLevel, __DATE__, __TIME__);
#endif
return;
#endif /* atarist */
case 'X':
discard_locals = DISCARD_L;
return;
case 'x':
discard_locals = DISCARD_ALL;
return;
case 'y':
{
register symbol *sp = getsym (&swt[2]);
sp->trace = 1;
}
return;
#if (defined(MINIX) || defined(atariminix))
case 'z': /* ok we needed a letter */
write_minix_out = 1;
return;
#else
case 'z':
#if !(defined(atarist) || defined(CROSSATARI))
magic = ZMAGIC;
#endif
return;
#endif
default:
fatal ("invalid command option `%s'", swt);
}
}
/** Convenient functions for operating on one or all files being */
/** loaded. */
void print_file_name ();
/* Call FUNCTION on each input file entry.
Do not call for entries for libraries;
instead, call once for each library member that is being loaded.
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];
if (entry->library_flag)
{
register struct file_entry *subentry = entry->subfiles;
for (; subentry; subentry = subentry->chain)
(*function) (subentry, arg);
}
else
(*function) (entry, arg);
}
}
/* Call FUNCTION on each input file entry until it returns a non-zero
value. Return this value.
Do not call for entries for libraries;
instead, call once for each library member that is being loaded.
FUNCTION receives two arguments: the entry, and ARG. It must be a
function returning unsigned long (though this can probably be fudged). */
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->library_flag)
{
register struct file_entry *subentry = entry->subfiles;
for (; subentry; subentry = subentry->chain)
if (return_val = (*function) (subentry, arg))
return return_val;
}
else
if (return_val = (*function) (entry, arg))
return return_val;
}
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];
if (entry->just_syms_flag)
continue;
if (entry->library_flag)
{
register struct file_entry *subentry = entry->subfiles;
for (; subentry; subentry = subentry->chain)
(*function) (subentry, arg);
}
else
(*function) (entry, arg);
}
}
/* Close the input file that is now open. */
void
file_close ()
{
fclose (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. */
FILE *
file_open (entry)
register struct file_entry *entry;
{
register FILE *desc = NULL;
if (entry->superfile)
return file_open (entry->superfile);
if (entry == input_file)
return input_desc;
if (input_file) file_close ();
if (entry->search_dirs_flag && n_search_dirs)
{
/* register char **p = search_dirs; */
int i;
for (i = 0; i < n_search_dirs; i++)
{
register char *string
#ifndef atarist
= concat (search_dirs[i], "/", entry->filename);
#else
= concat (search_dirs[i], "\\", entry->filename);
#endif
desc = fopen (string, "r");
if (desc != NULL)
{
#ifdef _IOFBF
if(setvbuf(desc, NULL, _IOFBF, 128L * 1024))
perror_name(string);
#endif
entry->filename = string;
entry->search_dirs_flag = 0;
break;
}
free (string);
}
}
else
desc = fopen (entry->filename, "r");
if (desc != NULL)
{
#ifdef _IOFBF
if(setvbuf(desc, NULL, _IOFBF, 128L * 1024))
perror_name(entry->filename);
#endif
input_file = entry;
input_desc = desc;
return desc;
}
perror_file (entry);
/* NOTREACHED */
}
/* Print the filename of ENTRY on OUTFILE (a stdio stream),
and then a newline. */
void
prline_file_name (entry, outfile)
struct file_entry *entry;
FILE *outfile;
{
print_file_name (entry, outfile);
fprintf (outfile, "\n");
}
/* Print the filename of ENTRY on OUTFILE (a stdio stream). */
void
print_file_name (entry, outfile)
struct file_entry *entry;
FILE *outfile;
{
if (entry->superfile)
{
print_file_name (entry->superfile, outfile);
fprintf (outfile, "(%s)", entry->filename);
}
else
fprintf (outfile, "%s", entry->filename);
}
/* Return the filename of entry as a string (malloc'd for the purpose) */
char *
get_file_name (entry)
struct file_entry *entry;
{
char *result, *supfile;
if (entry->superfile)
{
supfile = get_file_name (entry->superfile);
result = (char *) xmalloc (strlen (supfile)
+ strlen (entry->filename) + 3);
sprintf (result, "%s(%s)", supfile, entry->filename);
free (supfile);
}
else
{
result = (char *) xmalloc (strlen (entry->filename) + 1);
strcpy (result, entry->filename);
}
return result;
}
/* Medium-level input routines for rel files. */
/* Read a file's header into the proper place in the file_entry.
DESC is the descriptor on which the file is open.
ENTRY is the file's entry. */
void
read_header (desc, entry)
FILE *desc;
register struct file_entry *entry;
{
register int len;
struct exec *loc = (struct exec *) &entry->header;
fseek (desc, entry->starting_offset, 0);
len = fread (loc, 1, sizeof (struct exec), desc);
if (len != sizeof (struct exec))
fatal_with_file ("failure reading header of ", entry);
#ifdef BYTE_SWAP
loc->a_info = SWAP4(loc->a_info); /* always four (4) bytes */
if (sizeof(loc->a_text)==4) /* does this need to be ? */
{ loc->a_text = SWAP4(loc->a_text);
loc->a_data = SWAP4(loc->a_data);
loc->a_bss = SWAP4(loc->a_bss);
loc->a_syms = SWAP4(loc->a_syms);
loc->a_entry = SWAP4(loc->a_entry);
loc->a_trsize = SWAP4(loc->a_trsize);
loc->a_drsize = SWAP4(loc->a_drsize); }
else
{ loc->a_text = SWAP4(loc->a_text);
loc->a_data = SWAP4(loc->a_data);
loc->a_bss = SWAP4(loc->a_bss);
loc->a_syms = SWAP4(loc->a_syms);
loc->a_entry = SWAP4(loc->a_entry);
loc->a_trsize = SWAP4(loc->a_trsize);
loc->a_drsize = SWAP4(loc->a_drsize); }
#endif /* BYTE_SWAP */
if (N_BADMAG (*loc))
fatal_with_file ("bad magic number in ", entry);
entry->header_read_flag = 1;
}
/* Read the symbols of file ENTRY into core.
Assume it is already open, on descriptor DESC.
Also read the length of the string table, which follows the symbol table,
but don't read the contents of the string table. */
void
read_entry_symbols (desc, entry)
struct file_entry *entry;
FILE *desc;
{
int str_size;
if (!entry->header_read_flag)
read_header (desc, entry);
entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
fseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
if (entry->header.a_syms != fread (entry->symbols, 1, entry->header.a_syms, desc))
fatal_with_file ("premature end of file in symbols of ", entry);
fseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
if (sizeof str_size != fread (&str_size, 1, sizeof str_size, desc))
fatal_with_file ("bad string table size in ", entry); /***dohicky***/
#ifdef BYTE_SWAP
if (sizeof(str_size)==4)
str_size = SWAP4(str_size);
else
str_size = SWAP2(str_size);
#endif /* BYTE_SWAP */
entry->string_size = str_size;
}
/* Read the string table of file ENTRY into core.
Assume it is already open, on descriptor DESC.
Also record whether a GDB symbol segment follows the string table. */
void
read_entry_strings (desc, entry)
struct file_entry *entry;
FILE *desc;
{
int buffer;
if (!entry->header_read_flag)
read_header (desc, entry);
fseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
if (entry->string_size != fread (entry->strings, 1, entry->string_size, desc))
fatal_with_file ("premature end of file in strings of ", entry);
/* While we are here, see if the file has a symbol segment at the end.
For a separate file, just try reading some more.
For a library member, compare current pos against total size. */
if (entry->superfile)
{
if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
return;
}
else
{
buffer = fread (&buffer, 1, sizeof buffer, desc);
if (buffer == 0)
return;
if (buffer != sizeof buffer)
fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
}
entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
}
/* Read in the symbols of all input files. */
void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
void enter_file_symbols (), enter_global_ref (), search_library ();
void
load_symbols ()
{
register int i;
if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
for (i = 0; i < number_of_files; i++)
{
register struct file_entry *entry = &file_table[i];
read_file_symbols (entry);
}
if (trace_files) fprintf (stderr, "\n");
}
/* 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). */
/* fixed for byte swapping -- ko */
void
read_file_symbols (entry)
register struct file_entry *entry;
{
register FILE *desc;
register int len;
int magicnum;
desc = file_open (entry);
len = fread ( &magicnum, 1, sizeof magicnum, desc);
if (len != sizeof magicnum)
fatal_with_file ("failure reading header of ", entry);
#ifdef BYTE_SWAP
if (sizeof(magicnum)==4) /* does this need to be so? ko */
magicnum = SWAP4(magicnum);
else
magicnum = SWAP2(magicnum);
#endif
if (!N_BADMAG (*((struct exec *)&magicnum)))
{
read_entry_symbols (desc, entry);
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (desc, entry);
enter_file_symbols (entry);
entry->strings = 0;
}
else
{
char armag[SARMAG];
fseek (desc, 0, 0);
if (SARMAG != fread (armag, 1, SARMAG, desc) || strncmp (armag, ARMAG, SARMAG))
fatal_with_file ("malformed input file (not rel or archive) ", entry);
entry->library_flag = 1;
search_library (desc, entry);
}
file_close ();
}
/* Enter the external symbol defs and refs of ENTRY in the hash table. */
void
enter_file_symbols (entry)
struct file_entry *entry;
{
register struct nlist
*p,
*end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
#if 0
int lowest_set_vector = -1;
#endif
if (trace_files) prline_file_name (entry, stderr);
for (p = entry->symbols; p < end; p++)
#ifdef BYTE_SWAP
{ /* convert the n_list ??? */
if (entry->superfile==NULL) {
p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
p->n_desc = SWAP2(p->n_desc);
p->n_value = SWAP4(p->n_value); }
#endif /* BYTE_SWAP */
#if 0
/* Currently N_SETV symbols shouldn't be in input to the loader, */
/* but I'll leave this here in case they ever are */
if ((p->n_type & ~N_EXT) == N_SETV &&
(lowest_set_vector = -1 || lowest_set_vector > p->n_value))
lowest_set_vector = p->n_value;
else
#endif
if (SET_ELEMENT_P (p->n_type))
{
set_symbol_count++;
if (!relocatable_output)
enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
}
else if (p->n_type == N_WARNING)
{
char *name = p->n_un.n_strx + entry->strings;
/* Grab the next entry. */
p++;
#ifdef BYTE_SWAP
if (entry->superfile==NULL) {
p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
p->n_desc = SWAP2(p->n_desc);
p->n_value = SWAP4(p->n_value); }
#endif /* BYTE_SWAP */
if (p->n_type != (N_UNDF | N_EXT))
{
fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
progname, entry->filename);
make_executable = 0;
p--; /* Process normally. */
}
else
{
symbol *sp;
char *sname = p->n_un.n_strx + entry->strings;
/* Deal with the warning symbol. */
enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
sp = getsym (sname);
sp->warning = (char *) xmalloc (strlen(name) + 1);
strcpy (sp->warning, name);
warning_count++;
}
}
else if (p->n_type & N_EXT)
enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
{
if ((p->n_un.n_strx + entry->strings)[0] != 'L')
non_L_local_sym_count++;
local_sym_count++;
}
else debugger_sym_count++;
#ifdef BYTE_SWAP
}
#endif /* BYTE_SWAP */
#if 0
entry->set_vector_offset = lowest_set_vector;
#endif
/* Count one for the local symbol that we generate,
whose name is the file's name (usually) and whose address
is the start of the file's text. */
local_sym_count++;
non_L_local_sym_count++;
}
/* Enter one global symbol in the hash table.
NLIST_P points to the `struct nlist' read from the file
that describes the global symbol. NAME is the symbol's name.
ENTRY is the file entry for the file the symbol comes from.
The `struct nlist' is modified by placing it on a chain of
all such structs that refer to the same global symbol.
This chain starts in the `refs' field of the symbol table entry
and is chained through the `n_name'. */
void
enter_global_ref (nlist_p, name, entry)
register struct nlist *nlist_p;
char *name;
struct file_entry *entry;
{
register symbol *sp = getsym (name);
register int type = nlist_p->n_type;
int oldref = sp->referenced;
int olddef = sp->defined;
nlist_p->n_un.n_name = (char *) sp->refs;
sp->refs = nlist_p;
sp->referenced = 1;
if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
{
if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
sp->defined = type;
if (oldref && !olddef)
/* It used to be undefined and we're defining it. */
undefined_global_sym_count--;
#if 0
/* If this is a common definition, keep track of largest
common definition seen for this symbol. */
if (type == (N_UNDF | N_EXT)
&& sp->max_common_size < nlist_p->n_value)
sp->max_common_size = nlist_p->n_value;
#endif
if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
{
/* First definition and it's common. */
common_defined_global_count++;
sp->max_common_size = nlist_p->n_value;
}
else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
{
/* It used to be common and we're defining it as
something else. */
common_defined_global_count--;
sp->max_common_size = 0;
}
else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
&& sp->max_common_size < nlist_p->n_value)
/* It used to be common and this is a new common entry to
which we need to pay attention. */
sp->max_common_size = nlist_p->n_value;
/* Are we defining it as a set element? */
#if 0
if (SET_ELEMENT_P (type)) /* potential fix but i dont see why ++jrb */
#endif
if (SET_ELEMENT_P (type)
&& (!olddef || (olddef && sp->max_common_size)))
set_vector_count++;
/* Indirect symbols value should be modified to point
a symbol being equivalenced to. */
if (type == (N_INDR | N_EXT))
{
nlist_p->n_value =
(unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
+ entry->strings);
if ((symbol *) nlist_p->n_value == sp)
{
/* Somebody redefined a symbol to be itself. */
fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
entry->filename, name);
/* Rewrite this symbol as being a global text symbol
with value 0. */
nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
nlist_p->n_value = 0;
/* Don't make the output executable. */
make_executable = 0;
}
else
global_indirect_count++;
}
}
else
if (!oldref)
#ifndef DOLLAR_KLUDGE
undefined_global_sym_count++;
#else
{
if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
{
/* This is an (ISI?) $-conditional; skip it */
sp->referenced = 0;
if (sp->trace)
{
fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
print_file_name (entry, stderr);
fprintf (stderr, "\n");
}
return;
}
else
undefined_global_sym_count++;
}
#endif
if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
text_start = nlist_p->n_value;
if (sp->trace)
{
register char *reftype;
switch (type & ~N_EXT)
{
case N_UNDF:
if (nlist_p->n_value)
reftype = "defined as common";
else reftype = "referenced";
break;
case N_ABS:
reftype = "defined as absolute";
break;
case N_TEXT:
reftype = "defined in text section";
break;
case N_DATA:
reftype = "defined in data section";
break;
case N_BSS:
reftype = "defined in BSS section";
break;
case N_SETT:
reftype = "is a text set element";
break;
case N_SETD:
reftype = "is a data set element";
break;
case N_SETB:
reftype = "is a BSS set element";
break;
case N_SETA:
reftype = "is an absolute set element";
break;
case N_SETV:
reftype = "defined in text section as vector";
break;
case N_INDR:
reftype = (char *) alloca (23
+ strlen ((nlist_p + 1)->n_un.n_strx
+ entry->strings));
sprintf (reftype, "defined equivalent to %s",
(nlist_p + 1)->n_un.n_strx + entry->strings);
break;
default:
reftype = "I don't know this type";
break;
}
fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
print_file_name (entry, stderr);
fprintf (stderr, "\n");
}
}
/* This return 0 if the given file entry's symbol table does *not*
contain the nlist point entry, and it returns the files entry
pointer (cast to unsigned long) if it does. */
unsigned long
contains_symbol (entry, n_ptr)
struct file_entry *entry;
register struct nlist *n_ptr;
{
if (n_ptr >= entry->symbols &&
n_ptr < (entry->symbols
+ (entry->header.a_syms / sizeof (struct nlist))))
return (unsigned long) entry;
return 0;
}
/* Searching libraries */
struct file_entry *decode_library_subfile ();
void linear_library (), symdef_library ();
/* Search the library ENTRY, already open on descriptor DESC.
This means deciding which library members to load,
making a chain of `struct file_entry' for those members,
and entering their global symbols in the hash table. */
void
search_library (desc, entry)
FILE *desc;
struct file_entry *entry;
{
int member_length;
register char *name;
register struct file_entry *subentry;
if (!undefined_global_sym_count) return;
/* Examine its first member, which starts SARMAG bytes in. */
subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
if (!subentry) return;
name = subentry->filename;
free (subentry);
/* Search via __.SYMDEF if that exists, else linearly. */
if (!strcmp (name, "__.SYMDEF"))
symdef_library (desc, entry, member_length);
else
linear_library (desc, entry);
}
/* Construct and return a file_entry for a library member.
The library's file_entry is library_entry, and the library is open on DESC.
SUBFILE_OFFSET is the byte index in the library of this member's header.
We store the length of the member into *LENGTH_LOC. */
struct file_entry *
decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
FILE *desc;
struct file_entry *library_entry;
int subfile_offset;
int *length_loc;
{
int bytes_read;
register int namelen;
int member_length;
register char *name;
struct ar_hdr hdr1;
register struct file_entry *subentry;
fseek (desc, subfile_offset, 0);
#if (defined(WORD_ALIGNED) && defined(CROSSATARI))
bytes_read = fread (hdr1.ar_name, 1,
sizeof (hdr1.ar_name), desc);
bytes_read += fread (hdr1.ar_size, 1,
sizeof (hdr1.ar_size), desc);
bytes_read += fread (hdr1.ar_date, 1,
sizeof (hdr1.ar_date), desc);
bytes_read += fread (hdr1.ar_mode, 1,
sizeof (hdr1.ar_mode), desc);
bytes_read += fread (hdr1.ar_uid, 1,
sizeof (hdr1.ar_uid), desc);
bytes_read += fread (hdr1.ar_gid, 1,
sizeof (hdr1.ar_gid), desc);
bytes_read += fread (hdr1.ar_fmag, 1,
sizeof (hdr1.ar_fmag), desc);
#else
bytes_read = fread (&hdr1, 1, sizeof hdr1, desc);
#endif
if (!bytes_read)
return 0; /* end of archive */
#if (defined(WORD_ALIGNED) && defined(CROSSATARI))
if (bytes_read != (sizeof (hdr1.ar_name) +
sizeof (hdr1.ar_size) +
sizeof (hdr1.ar_date) +
sizeof (hdr1.ar_mode) +
sizeof (hdr1.ar_uid) +
sizeof (hdr1.ar_gid) +
sizeof (hdr1.ar_fmag)))
fatal_with_file ("malformed library archive ", library_entry);
#else
if (sizeof hdr1 != bytes_read)
fatal_with_file ("malformed library archive ", library_entry);
#endif
if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
fatal_with_file ("malformatted header of archive member in ", library_entry);
subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
bzero (subentry, sizeof (struct file_entry));
for (namelen = 0;
namelen < sizeof hdr1.ar_name
&& hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
&& hdr1.ar_name[namelen] != '/';
namelen++);
name = (char *) xmalloc (namelen+1);
strncpy (name, hdr1.ar_name, namelen);
name[namelen] = 0;
subentry->filename = name;
subentry->local_sym_name = name;
subentry->symbols = 0;
subentry->strings = 0;
subentry->subfiles = 0;
#if (defined(WORD_ALIGNED) && defined(CROSSATARI))
subentry->starting_offset = subfile_offset + (sizeof (hdr1.ar_name) +
sizeof (hdr1.ar_size) +
sizeof (hdr1.ar_date) +
sizeof (hdr1.ar_mode) +
sizeof (hdr1.ar_uid) +
sizeof (hdr1.ar_gid) +
sizeof (hdr1.ar_fmag)) ;
#else
subentry->starting_offset = subfile_offset + sizeof hdr1;
#endif
subentry->superfile = library_entry;
subentry->library_flag = 0;
subentry->header_read_flag = 0;
subentry->just_syms_flag = 0;
subentry->chain = 0;
subentry->total_size = member_length;
(*length_loc) = member_length;
return subentry;
}
int subfile_wanted_p ();
/* Search a library that has a __.SYMDEF member.
DESC is a descriptor on which the library is open.
The file pointer is assumed to point at the __.SYMDEF data.
ENTRY is the library's file_entry.
MEMBER_LENGTH is the length of the __.SYMDEF data. */
void
symdef_library (desc, entry, member_length)
FILE *desc;
struct file_entry *entry;
int member_length;
{
int *symdef_data = (int *) xmalloc (member_length);
register struct symdef *symdef_base;
char *sym_name_base;
int number_of_symdefs;
int length_of_strings;
int not_finished;
int bytes_read;
register int i;
struct file_entry *prev = 0;
int prev_offset = 0;
bytes_read = fread ( symdef_data, 1, member_length, desc);
if (bytes_read != member_length)
fatal_with_file ("malformatted __.SYMDEF in ", entry);
#ifdef BYTE_SWAP
if (sizeof(*symdef_data)==4)
*symdef_data = SWAP4(*symdef_data);
else
*symdef_data = SWAP2(*symdef_data);
#endif /* BYTE_SWAP */
number_of_symdefs = *symdef_data / sizeof (struct symdef);
if (number_of_symdefs < 0 ||
number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
fatal_with_file ("malformatted __.SYMDEF in ", entry);
symdef_base = (struct symdef *) (symdef_data + 1);
#ifdef BYTE_SWAP
*(int *) (symdef_base + number_of_symdefs) = SWAP4(*(int *) (symdef_base + number_of_symdefs));
#endif /* BYTE_SWAP */
length_of_strings = *(int *) (symdef_base + number_of_symdefs);
if (length_of_strings < 0
|| number_of_symdefs * sizeof (struct symdef) + length_of_strings
+ 2 * sizeof (int) != member_length)
fatal_with_file ("malformatted __.SYMDEF in ", entry);
sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
/* Check all the string indexes for validity. */
for (i = 0; i < number_of_symdefs; i++)
{
register int index;
#ifdef BYTE_SWAP
if (sizeof(symdef_base[i].symbol_name_string_index)==4)
symdef_base[i].symbol_name_string_index = SWAP4(symdef_base[i].symbol_name_string_index);
else
symdef_base[i].symbol_name_string_index = SWAP2(symdef_base[i].symbol_name_string_index);
#endif /* BYTE_SWAP */
index = symdef_base[i].symbol_name_string_index;
if (index < 0 || index >= length_of_strings
|| (index && *(sym_name_base + index - 1)))
fatal_with_file ("malformatted __.SYMDEF in ", entry);
#ifdef BYTE_SWAP
if(sizeof(symdef_base[i].library_member_offset) == 4)
symdef_base[i].library_member_offset = SWAP4(symdef_base[i].library_member_offset);
else
symdef_base[i].library_member_offset = SWAP2(symdef_base[i].library_member_offset);
#endif
}
/* Search the symdef data for members to load.
Do this until one whole pass finds nothing to load. */
not_finished = 1;
while (not_finished)
{
not_finished = 0;
/* Scan all the symbols mentioned in the symdef for ones that we need.
Load the library members that contain such symbols. */
for (i = 0;
(i < number_of_symdefs
&& (undefined_global_sym_count || common_defined_global_count));
i++)
if (symdef_base[i].symbol_name_string_index >= 0)
{
register symbol *sp;
sp = getsym_soft (sym_name_base
+ symdef_base[i].symbol_name_string_index);
/* If we find a symbol that appears to be needed, think carefully
about the archive member that the symbol is in. */
if (sp && ((sp->referenced && !sp->defined)
|| (sp->defined && sp->max_common_size)))
{
int junk;
register int j;
register int offset;
struct file_entry *subentry;
offset = symdef_base[i].library_member_offset;
/* Don't think carefully about any archive member
more than once in a given pass. */
if (prev_offset == offset)
continue;
prev_offset = offset;
/* Read the symbol table of the archive member. */
subentry = decode_library_subfile (desc, entry, offset, &junk);
if (subentry == 0)
fatal ("invalid offset for %s in symbol table of %s",
sym_name_base
+ symdef_base[i].symbol_name_string_index,
entry->filename);
read_entry_symbols (desc, subentry);
subentry->strings = (char *) malloc (subentry->string_size);
read_entry_strings (desc, subentry);
/* Now scan the symbol table and decide whether to load. */
if (!subfile_wanted_p (subentry))
{
free (subentry->symbols);
free (subentry->strings);
free (subentry);
}
else
{
/* This member is needed; load it.
Since we are loading something on this pass,
we must make another pass through the symdef data. */
not_finished = 1;
enter_file_symbols (subentry);
if (prev)
prev->chain = subentry;
else entry->subfiles = subentry;
prev = subentry;
/* Clear out this member's symbols from the symdef data
so that following passes won't waste time on them. */
for (j = 0; j < number_of_symdefs; j++)
{
if (symdef_base[j].library_member_offset == offset)
symdef_base[j].symbol_name_string_index = -1;
}
/* We'll read the strings again if we need them again. */
free (subentry->strings);
subentry->strings = 0;
}
}
}
}
free (symdef_data);
}
/* Search a library that has no __.SYMDEF.
ENTRY is the library's file_entry.
DESC is the descriptor it is open on. */
void
linear_library (desc, entry)
FILE *desc;
struct file_entry *entry;
{
register struct file_entry *prev = 0;
register int this_subfile_offset = SARMAG;
while (undefined_global_sym_count || common_defined_global_count)
{
int member_length;
register struct file_entry *subentry;
subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
if (!subentry) return;
read_entry_symbols (desc, subentry);
subentry->strings = (char *) alloca (subentry->string_size);
read_entry_strings (desc, subentry);
if (!subfile_wanted_p (subentry))
{
free (subentry->symbols);
free (subentry);
}
else
{
enter_file_symbols (subentry);
if (prev)
prev->chain = subentry;
else
entry->subfiles = subentry;
prev = subentry;
subentry->strings = 0; /* Since space will dissapear on return */
}
this_subfile_offset += member_length + sizeof (struct ar_hdr);
if (this_subfile_offset & 1) this_subfile_offset++;
}
}
/* ENTRY is an entry for a library member.
Its symbols have been read into core, but not entered.
Return nonzero if we ought to load this member. */
int
subfile_wanted_p (entry)
struct file_entry *entry;
{
register struct nlist *p;
register struct nlist *end
= entry->symbols + entry->header.a_syms / sizeof (struct nlist);
#ifdef DOLLAR_KLUDGE
register int dollar_cond = 0;
#endif
for (p = entry->symbols; p < end; p++)
{
register int type;
register char * name;
#ifdef BYTE_SWAP /* convert the n_list ??? */
p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
p->n_desc = SWAP2(p->n_desc);
p->n_value = SWAP4(p->n_value);
#endif /* BYTE_SWAP */
type = p->n_type;
name = p->n_un.n_strx + entry->strings;
if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value
#ifdef DOLLAR_KLUDGE
|| name[1] == '$'
#endif
))
{
register symbol *sp = getsym_soft (name);
#ifdef DOLLAR_KLUDGE
if (name[1] == '$')
{
sp = getsym_soft (&name[2]);
dollar_cond = 1;
if (!sp) continue;
if (sp->referenced)
{
if (write_map)
{
print_file_name (entry, stdout);
fprintf (stdout, " needed due to $-conditional %s\n", name);
}
#ifdef BYTE_SWAP /* finish converting n_list */
for (p++;p < end; p++)
{
p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
p->n_desc = SWAP2(p->n_desc);
p->n_value = SWAP4(p->n_value);
}
#endif /* BYTE SWAP */
return 1;
}
continue;
}
#endif
/* If this symbol has not been hashed, we can't be looking for it. */
if (!sp) continue;
/* Note: There has been a lot of discussion about what to
when a common definition was previously seen (i.e. when
sp->max_common_size > 0).
The latest solution is to treat a previous common definition
(wrt to subfile_wanted_p) no differently from a real definition.
This has the advantage of simplicity and consistency: a common
definition is just like a common definition (consistent
with strict ANSI C) except that we allow duplicate definitions.
Possible disadvantage: May not be the best choice for Fortran,
though it is consistent with the standard.
An earlier solution:
We wanted to see a common definition in the subfile,
and note its size, but ignore any other definition
if the symbol was already defined (even as a common).
This meant that if there were multiple common definitions,
the final definition would use the largest size of any of them,
as it should. But if there was a common definition and another
definition, like "int pipe;" in a program and "int pipe() {}"
in the library, only the common would be used.
Disadvantage: a poorly justified kludge.
Another previous solution:
If the symbol already had a definition as a common symbol,
we would want this subfile if some other subfile of the
same library that we already need anyway also used the symbol.
This seemed like an even more ad hoc decision.
It would also cause subfiles to be pulled in that would
then conflict with previous entries. I.e. you couldn't
have: ld ... start.o libc.a ... if libc.a contained start.o.
Other hybrid solutions were also considered.
*/
if (sp->referenced && !sp->defined)
{
/* This is a symbol we are looking for. */
#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. */
common_defined_global_count++;
sp->max_common_size = p->n_value;
undefined_global_sym_count--;
sp->defined = 1;
continue;
}
if (write_map)
{
char *nm;
if (demangler == NULL || (nm = (*demangler)(sp->name)) == NULL)
nm = sp->name;
print_file_name (entry, stdout);
fprintf (stdout, " needed due to %s\n", nm);
if(nm != sp->name)
free(nm);
}
#ifdef BYTE_SWAP /* finish converting n_list */
for (p++;p < end; p++)
{
p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
p->n_desc = SWAP2(p->n_desc);
p->n_value = SWAP4(p->n_value);
}
#endif /* BYTE SWAP */
return 1;
}
}
}
return 0;
}
void consider_file_section_lengths (), relocate_file_addresses ();
void print_files_defining_symbol ();
/* 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. */
void
digest_symbols ()
{
register int i;
int setv_fill_count;
if (trace_files)
fprintf (stderr, "Digesting symbol information:\n\n");
/* Compute total size of sections */
each_file (consider_file_section_lengths, 0);
/* Setup the set element vector */
if (!relocatable_output)
{
set_sect_size =
(set_symbol_count + 2 * set_vector_count) * sizeof (unsigned long);
set_sect_start = text_start + text_size;
text_size += set_sect_size;
set_vectors = (unsigned long *) xmalloc (set_sect_size);
setv_fill_count = 0;
}
/* If necessary, pad text section to full page in the file.
Include the padding in the text segment size. */
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
if (magic == NMAGIC || magic == ZMAGIC)
{
int text_end = text_size + N_TXTOFF (outheader);
text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
text_size += text_pad;
}
#endif
outheader.a_text = text_size;
/* Make the data segment address start in memory on a suitable boundary. */
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
/* set beginning of executable's data segment to immediately after
text segment. This is right for the type 0x601A Tos header
and also for the standard Stminix header
*/
if (! Tdata_flag_specified)
data_start = text_start + text_size;
#else
if (! Tdata_flag_specified)
data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
#endif
/* Compute start addresses of each file's sections and symbols. */
each_full_file (relocate_file_addresses, 0);
/* Now, for each symbol, verify that it is defined globally at most once.
Put the global value into the symbol entry.
Common symbols are allocated here, in the BSS section.
Each defined symbol is given a '->defined' field
which is the correct N_ code for its definition,
except in the case of common symbols with -r.
Then make all the references point at the symbol entry
instead of being chained together. */
defined_global_sym_count = 0;
for (i = 0; i < TABSIZE; i++)
{
register symbol *sp;
for (sp = symtab[i]; sp; sp = sp->link)
{
/* For each symbol */
register struct nlist *p, *next;
int defs = 0, com = sp->max_common_size, erred = 0;
struct nlist *first_definition;
for (p = sp->refs; p; p = next)
{
register int type = p->n_type;
#if 0
if ((type & ~N_EXT) == N_SETV) continue;
#endif
if (SET_ELEMENT_P (type))
{
if (relocatable_output)
fatal ("internal: global ref to set element with -r", 0);
if (!defs++)
{
sp->value = set_sect_start
+ setv_fill_count++ * sizeof (unsigned long);
sp->defined = N_SETV | N_EXT;
first_definition = p;
}
else if ((sp->defined & ~N_EXT) != N_SETV)
{
make_executable = 0;
error ("text symbol `%s' redefined as set vector. Files:",
sp->name);
print_files_defining_symbol (first_definition, p);
}
set_vectors[setv_fill_count++] = p->n_value;
}
else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
{
/* non-common definition */
if (defs++ && sp->value != p->n_value)
if (!erred++)
{
make_executable = 0;
error ("multiple definitions of symbol `%s'. Files:",
sp->name);
print_files_defining_symbol (first_definition, p);
}
sp->value = p->n_value;
sp->defined = type;
first_definition = p;
}
next = (struct nlist *) p->n_un.n_name;
p->n_un.n_name = (char *) sp;
}
/* Allocate as common if defined as common and not defined for real */
if (com && !defs)
{
if (!relocatable_output || force_common_definition)
{
com = (com + sizeof (int) - 1) & (- sizeof (int));
sp->value = data_start + data_size + bss_size;
sp->defined = N_BSS | N_EXT;
bss_size += com;
if (write_map)
{
char *nm;
if (demangler == NULL || (nm = (*demangler)(sp->name)) == NULL)
nm = sp->name;
printf ("Allocating common %s: %x at %x\n",
nm, com, sp->value);
if(nm != sp->name)
free(nm);
}
}
else
{
sp->defined = 0;
undefined_global_sym_count++;
}
}
/* Set length word at front of vector and add zero at the end.
Also reverse the vector to put it in file order. */
if ((sp->defined & ~N_EXT) == N_SETV)
{
unsigned long length_word_index =
(sp->value - set_sect_start) / sizeof (unsigned long);
unsigned long i, tmp;
set_vectors[length_word_index]
= setv_fill_count - 1 - length_word_index;
/* Reverse the vector. */
for (i = 1;
i < (setv_fill_count - length_word_index - 1) / 2 + 1;
i++)
{
tmp = set_vectors[length_word_index + i];
set_vectors[length_word_index + i]
= set_vectors[setv_fill_count - i];
set_vectors[setv_fill_count - i] = tmp;
}
set_vectors[setv_fill_count++] = 0;
}
if (sp->defined)
defined_global_sym_count++;
}
}
if (end_symbol) /* These are null if -r. */
{
etext_symbol->value = text_size + text_start;
edata_symbol->value = data_start + data_size;
end_symbol->value = data_start + data_size + bss_size;
}
/* Figure the data_pad now, so that it overlaps with the bss addresses. */
if (specified_data_size && specified_data_size > data_size)
data_pad = specified_data_size - data_size;
if (magic == ZMAGIC)
data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
- data_size;
bss_size -= data_pad;
if (bss_size < 0) bss_size = 0;
data_size += data_pad;
}
/* Accumulate the section sizes of input file ENTRY
into the section sizes of the output file. */
void
consider_file_section_lengths (entry)
register struct file_entry *entry;
{
if (entry->just_syms_flag)
return;
entry->text_start_address = text_size;
/* If there were any vectors, we need to chop them off */
#if 0
/* There should never be set vectors in input to the loader */
if (entry->set_vector_offset == -1)
#endif
text_size += entry->header.a_text;
#if 0
else
text_size += entry->set_vector_offset - N_TXTOFF (entry->header);
#endif
entry->data_start_address = data_size;
data_size += entry->header.a_data;
entry->bss_start_address = bss_size;
bss_size += entry->header.a_bss;
/* dont know if this is still needed, left it in for safety sake ++jrb */
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
/* something's probably wrong elsewhere that forces this kludge.
if bss_size turns out to be odd, add one to it */
if (bss_size & 1)
bss_size++;
#endif
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. */
void
relocate_file_addresses (entry)
register struct file_entry *entry;
{
entry->text_start_address += text_start;
/* Note that `data_start' and `data_size' have not yet been
adjusted for `data_pad'. If they had been, we would get the wrong
results here. */
entry->data_start_address += data_start;
entry->bss_start_address += data_start + data_size;
{
register struct nlist *p;
register struct nlist *end
= entry->symbols + entry->header.a_syms / sizeof (struct nlist);
for (p = entry->symbols; p < end; p++)
{
/* If this belongs to a section, update it by the section's start address */
register int type = p->n_type & N_TYPE;
switch (type)
{
case N_TEXT:
case N_SETT:
p->n_value += entry->text_start_address;
break;
case N_DATA:
case N_SETV:
case N_SETD:
/* A symbol whose value is in the data section
is present in the input file as if the data section
started at an address equal to the length of the file's text. */
p->n_value += entry->data_start_address - entry->header.a_text;
break;
case N_BSS:
case N_SETB:
/* likewise for symbols with value in BSS. */
p->n_value += entry->bss_start_address
- entry->header.a_text - entry->header.a_data;
break;
}
}
}
}
void describe_file_sections (), list_file_locals ();
/* Print a complete or partial map of the output file. */
void
print_symbols (outfile)
FILE *outfile;
{
register int i;
fprintf (outfile, "\nFiles:\n\n");
each_file (describe_file_sections, outfile);
fprintf (outfile, "\nGlobal symbols:\n\n");
for (i = 0; i < TABSIZE; i++)
{
register symbol *sp;
for (sp = symtab[i]; sp; sp = sp->link)
{
char *nm = NULL;
if ((sp->defined || sp->referenced) && (demangler != NULL))
nm = (*demangler)(sp->name);
if(nm == NULL)
nm = sp->name;
if (sp->defined == 1)
fprintf (outfile, " %s: common, length 0x%x\n", nm, sp->max_common_size);
if (sp->defined)
fprintf (outfile, " %s: 0x%x\n", nm, sp->value);
else if (sp->referenced)
fprintf (outfile, " %s: undefined\n", nm);
if(nm != sp->name)
free(nm);
}
}
each_file (list_file_locals, outfile);
}
void
describe_file_sections (entry, outfile)
struct file_entry *entry;
FILE *outfile;
{
fprintf (outfile, " ");
print_file_name (entry, outfile);
if (entry->just_syms_flag)
fprintf (outfile, " symbols only\n");
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);
}
void
list_file_locals (entry, outfile)
struct file_entry *entry;
FILE *outfile;
{
register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
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");
for (p = entry->symbols; p < end; p++)
/* If this is a definition,
update it if necessary by this file's start address. */
if (!((p->n_type & (N_STAB | N_EXT)) || SET_ELEMENT_P(p->n_type)))
{
char *s = entry->strings + p->n_un.n_strx;
char *nm;
if (demangler == NULL || (nm = (*demangler)(s)) == NULL)
nm = s;
fprintf (outfile, " %s: 0x%x\n", nm, p->n_value);
if(nm != s)
free(nm);
}
entry->strings = 0; /* All done with them. */
}
/* Static vars for do_warnings and subroutines of it */
int list_unresolved_refs; /* List unresolved refs */
int list_warning_symbols; /* List warning syms */
int list_multple_defs; /* List multiple definitions */
void qsort ();
struct line_debug_entry *init_debug_scan ();
int next_debug_entry ();
int address_to_line ();
int relocation_entries_relation ();
/* 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;
{
struct relocation_info *txt_reloc, *data_reloc;
struct line_debug_entry *state_pointer;
register struct line_debug_entry *current, *next, *source;
char *errfmt; /* Assigned to generally */
/* static values; should not */
/* be written into */
char *errmsg; /* Assigned to malloc'd values */
/* and copied into; should be */
/* freed when done */
int invalidate_line_number;
/* Read in the files strings if they aren't available */
if (!entry->strings)
{
FILE *desc;
entry->strings = (char *) alloca (entry->string_size);
desc = file_open (entry);
read_entry_strings (desc, entry);
}
state_pointer = init_debug_scan (0, entry);
current = state_pointer;
next = state_pointer + 1;
source = state_pointer + 2;
read_file_relocation (entry);
/* We need to sort the relocation info here. Sheesh, so much effort
for one lousy error optimization. */
qsort (entry->textrel,
entry->header.a_trsize/sizeof (struct relocation_info),
sizeof (struct relocation_info),
relocation_entries_relation);
for (txt_reloc = entry->textrel;
txt_reloc < (entry->textrel
+ entry->header.a_trsize/sizeof (struct relocation_info));
txt_reloc++)
{
register struct nlist *s;
register symbol *g;
int s_index;
/* If the relocation isn't resolved through a symbol, continue */
if (!RELOC_EXTERN_P(txt_reloc))
continue;
s_index = RELOC_SYMBOL(txt_reloc);
if (s_index < 0 )
fatal_with_file ("bad symbol in relocation table of ", entry);
s = &entry->symbols[s_index];
/* 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 compiler would have caught it otherwise), so we can
ignore these cases. */
if (!(s->n_type & N_EXT))
continue;
g = (symbol *) s->n_un.n_name;
if (!g->defined) /* Reference */
{
if (!list_unresolved_refs || /* Don't list any */
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 text";
invalidate_line_number = 0;
}
}
else /* Defined */
{
/* Potential symbol warning here */
if (!g->warning) continue;
errfmt = g->warning;
invalidate_line_number = 0;
}
/* If errfmt == 0, errmsg has already been defined. */
if (errfmt != 0)
{
char *nm;
if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
nm = g->name;
errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
sprintf (errmsg, errfmt, nm);
if (nm != g->name)
free (nm);
}
address_to_line ( (RELOC_ADDRESS (txt_reloc)
+ entry->text_start_address),
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);
free (errmsg);
}
free (state_pointer);
state_pointer = init_debug_scan (1, entry);
current = state_pointer;
next = state_pointer + 1;
source = state_pointer + 2;
/*
* Once again, time to sort the relocation info.
*/
qsort (entry->datarel,
entry->header.a_drsize/sizeof (struct relocation_info),
sizeof (struct relocation_info),
relocation_entries_relation);
for (data_reloc = entry->datarel;
data_reloc < (entry->datarel
+ entry->header.a_drsize/sizeof (struct relocation_info));
data_reloc++)
{
register struct nlist *s;
register symbol *g;
int s_index;
/* If the relocation isn't resolved through a symbol, continue */
if (!RELOC_EXTERN_P(data_reloc))
continue;
s_index = RELOC_SYMBOL(data_reloc);
if (s_index < 0 )
fatal_with_file ("bad symbol in relocation table of ", entry);
s = &entry->symbols[s_index];
g = (symbol *) s->n_un.n_name;
if (!g->defined) /* Reference */
{
if (!list_unresolved_refs || /* Don't list any */
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 data";
invalidate_line_number = 0;
}
}
else /* Defined */
{
/* Potential symbol warning here */
if (!g->warning) continue;
errfmt = g->warning;
invalidate_line_number = 0;
}
errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
sprintf(errmsg, errfmt, g->name);
address_to_line ( (RELOC_ADDRESS (data_reloc)
+ entry->text_start_address),
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);
free (errmsg);
}
/* now scan in the non-tex/data sections for undefs */
{
struct nlist *s,
*end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
symbol *g;
for (s = entry->symbols; s < end; s++)
{
if (!(s->n_type & N_EXT))
continue;
g = (symbol *) s->n_un.n_name;
if(g->defined)
continue;
if (g->referenced && (g->undef_refs == 0))
{
char *nm = NULL;
if(demangler)
nm = (*demangler)(g->name);
if(nm == NULL)
nm = g->name;
print_file_name (entry, outfile);
fprintf (outfile, ": Undefined symbol \"%s\"\n", nm);
if(nm != g->name) free(nm);
}
}
}
entry->strings = 0;
}
/*
* 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.
*/
int
relocation_entries_relation (rel1, rel2)
struct relocation_info *rel1, *rel2;
{
return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
}
/*
* Takes the values in state_pointer and moves onward to the next
* debug line symbol. It assumes that state_pointer[1] is valid; ie
* that it.sym points into some entry in the symbol table. If
* state_pointer[1].sym == 0, this routine should not be called.
*/
int
next_debug_entry (use_data_symbols, state_pointer)
register int use_data_symbols;
struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
{
register struct line_debug_entry
*current = state_pointer,
*next = state_pointer + 1,
*source = state_pointer + 2; /* Used to store source file */
struct file_entry *entry = (struct file_entry *) source->sym;
current->sym = next->sym;
current->line = next->line;
current->filename = next->filename;
while (++(next->sym) < (entry->symbols
+ entry->header.a_syms/sizeof (struct nlist)))
{
switch (next->sym->n_type)
{
case N_SLINE:
if (use_data_symbols) continue;
next->line = next->sym->n_desc;
return 1;
case N_DSLINE:
if (!use_data_symbols) continue;
next->line = next->sym->n_desc;
return 1;
#ifdef HAVE_SUN_STABS
case N_EINCL:
next->filename = source->filename;
continue;
#endif
case N_SO:
source->filename = next->sym->n_un.n_strx + entry->strings;
source->line++;
#ifdef HAVE_SUN_STABS
case N_BINCL:
#endif
case N_SOL:
next->filename =
next->sym->n_un.n_strx + entry->strings;
default:
continue;
}
}
next->sym = (struct nlist *) 0;
return 0;
}
int
address_to_line (address, state_pointer)
unsigned long address;
/* Next must be passed by reference! */
struct line_debug_entry state_pointer[3];
{
struct line_debug_entry
*current = state_pointer,
*next = state_pointer + 1;
struct line_debug_entry *tmp_pointer;
int use_data_symbols;
if (next->sym)
use_data_symbols = (next->sym->n_type & ~N_EXT) == N_DATA;
else
return current->line;
/* Go back to the beginning if we've already passed it. */
if (current->sym->n_value > address)
{
tmp_pointer = init_debug_scan (use_data_symbols,
(struct file_entry *)
((state_pointer + 2)->sym));
state_pointer[0] = tmp_pointer[0];
state_pointer[1] = tmp_pointer[1];
state_pointer[2] = tmp_pointer[2];
free (tmp_pointer);
}
/* If we're still in a bad way, return -1, meaning invalid line. */
if (current->sym->n_value > address)
return -1;
while (next->sym
&& next->sym->n_value <= address
&& next_debug_entry (use_data_symbols, state_pointer))
;
return current->line;
}
struct line_debug_entry *
init_debug_scan (use_data_symbols, entry)
int use_data_symbols;
struct file_entry *entry;
{
struct line_debug_entry
*state_pointer =
(struct line_debug_entry *) xmalloc (3 * sizeof (struct line_debug_entry));
register struct line_debug_entry
*current = state_pointer,
*next = state_pointer + 1,
*source = state_pointer + 2; /* Used to store source file */
struct nlist *tmp;
for (tmp = entry->symbols;
tmp < (entry->symbols
+ entry->header.a_syms/sizeof (struct nlist));
tmp++)
if (tmp->n_type == (int) N_SO) break;
if (tmp >= (entry->symbols
+ entry->header.a_syms/sizeof (struct nlist)))
{
/* I believe this translates to "We lose" */
current->filename = next->filename = entry->filename;
current->line = next->line = -1;
current->sym = next->sym = (struct nlist *) 0;
return state_pointer;
}
next->line = source->line = 0;
next->filename = source->filename
= (tmp->n_un.n_strx + entry->strings);
source->sym = (struct nlist *) entry;
next->sym = tmp;
next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
if (!next->sym) /* No line numbers for this section; */
/* setup output results as appropriate */
{
if (source->line)
{
current->filename = source->filename = entry->filename;
current->line = -1; /* Don't print lineno */
}
else
{
current->filename = source->filename;
current->line = 0;
}
return state_pointer;
}
next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
return state_pointer;
}
void
mark_flagged_symbols (entry, outfile)
struct file_entry *entry;
FILE *outfile;
{
struct nlist *p;
if (!entry->warning) return;
for (p = entry->symbols;
p < entry->symbols + (entry->header.a_syms / sizeof(struct nlist));
p++)
{
unsigned char type = p->n_type;
struct glosym *sym;
/* Ignore locals and references */
if (!(type & N_EXT) || type == N_EXT) continue;
sym = (struct glosym *) p->n_un.n_name;
if (sym->referenced)
((struct glosym *) p->n_un.n_name)->warning = entry->warning;
}
}
void
do_warnings (outfile)
FILE *outfile;
{
struct glosym **clrefs;
list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
list_warning_symbols = warning_count;
list_multple_defs = 0; /* Not currently done here */
if (!(list_unresolved_refs ||
list_warning_symbols ||
list_multple_defs ))
/* No need to run this routine */
return;
if (list_warning_symbols)
each_full_file (mark_flagged_symbols, outfile);
each_full_file (do_file_warnings, outfile);
if (!relocatable_output)
for (clrefs = cmdline_references;
clrefs < cmdline_references + cl_refs_allocated && *clrefs;
clrefs++)
if ((*clrefs)->referenced && !(*clrefs)->defined)
fprintf(stderr, "Error: Unresolved reference to symbol %s\n",
(*clrefs)->name);
if (entry_symbol && !entry_symbol->defined)
fprintf(stderr, "%s: error: Entry symbol `%s' never defined.\n",
progname, entry_symbol->name);
fprintf (outfile, "\n");
if (list_unresolved_refs || list_multple_defs)
make_executable = 0;
}
/* Print the files which have the definitions for a given symbol.
FIRST_DEF is the first nlist entry which defines the symbol, and
REST_OF_REFS is a chain of nlist entries which may or may not be
definitions for this symbol, but which are all references for the
symbol.
We do the job in this clumsy fashion because of the state our data
structures are in in the middle of digest_symbols (from which this
is called). */
void
print_files_defining_symbol (first_def, rest_of_refs)
struct nlist *first_def, *rest_of_refs;
{
struct file_entry *holder;
struct nlist *n_ptr;
if (holder =
(struct file_entry *) check_each_file (contains_symbol, first_def))
{
fprintf (stderr, " ");
prline_file_name (holder, stderr);
}
else
fatal ("internal: file not found containing nlist entry", 0);
for (n_ptr = rest_of_refs;
n_ptr;
n_ptr = (struct nlist *) n_ptr->n_un.n_name)
if (n_ptr->n_type & ~N_EXT)
if (holder =
(struct file_entry *) check_each_file (contains_symbol, n_ptr))
{
fprintf (stderr, " ");
prline_file_name (holder, stderr);
}
else
fatal ("internal: file not found containing nlist entry", 0);
}
/* Write the output file */
void
write_output ()
{
struct stat statbuf;
int filemode;
/* #ifdef __OLD_OPEN_P__ */
#if defined(atariminix)
/* this is for the benefit people who have a minix kernel without 3 arg open
support (ie. no poole fs). previously it was taken care of
by emul_open3, but i threw that away, and now assume you have
open3 support in the kernel. so this code is just a quick and
dirty hack to get a working gcc-ld so that you can then bootstrap
a kernel with the poole fs
*/
unlink(output_filename); /* never mind if it does'nt exist */
outdesc = fopen (output_filename, "w+");
if (outdesc == NULL) perror_name (output_filename);
#else
outdesc = fopen (output_filename, "w+");
if (outdesc == NULL) perror_name (output_filename);
#endif
#ifdef _IOFBF
if(setvbuf(outdesc, NULL, _IOFBF, 128L * 1024))
perror_name(output_filename);
#endif
if (fstat (fileno(outdesc), &statbuf) < 0)
perror_name (output_filename);
filemode = statbuf.st_mode;
#ifndef atarist
chmod (output_filename, filemode & ~0111);
#endif
/* Output the a.out header. */
write_header ();
/* Output the text and data segments, relocating as we go. */
write_text ();
write_data ();
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
/* Output the merged relocation info, if requested with `-r'. */
if (relocatable_output)
write_rel ();
/* Output the symbol table (both globals and locals). */
write_syms ();
#else
/* atari has symtab section before relocs */
write_syms();
write_rel();
if (gst_symboltable && gst_symbols)
{
fseek (outdesc, 0L, 0); /* seek to beginning of file */
write_header (); /* rewrite header */
}
#endif
/* Copy any GDB symbol segments from input files. */
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
/* see sym-ld.c for atari */
write_symsegs ();
#endif
fclose (outdesc);
#ifndef atarist
chmod (output_filename, filemode | 0111);
#endif
#if (defined(MINIX) || defined(atariminix))
/* write out.h format stuff out for MDB if requested */
if(write_minix_out == 1)
do_write_minix_out();
#endif
}
void modify_location (), perform_relocation (), copy_text (), copy_data ();
void
write_header ()
{
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
outheader.a_magic = magic;
#endif
outheader.a_text = text_size;
outheader.a_data = data_size;
outheader.a_bss = bss_size;
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
outheader.a_entry = (entry_symbol ? entry_symbol->value
: text_start + entry_offset);
#else
#if (defined(MINIX) || defined(atariminix))
outheader.a_versn = A_VERSION;
outheader.a_entry = 0;
{
register long stack = 0x00010000L - (outheader.a_text +
outheader.a_data +
outheader.a_bss);
while(stack < 0) stack += 0x00010000L;
outheader.a_msize = stack + (outheader.a_text + outheader.a_data +
outheader.a_bss);
}
#else
/* TOS */
outheader.a_AZero1 = 0; /* good a place as any to do this */
outheader.a_ldflgs = 0; /* fastload is NOT the default */
if (magic == NMAGIC)
outheader.a_ldflgs = F_SHTEXT; /* sharable text */
outheader.a_isreloc = ISRELOCINFO; /* means reloc present (!) */
#endif
#endif
#ifdef COFF_ENCAPSULATE
if (need_coff_header)
{
/* We are encapsulating BSD format within COFF format. */
struct coffscn *tp, *dp, *bp;
tp = &coffheader.scns[0];
dp = &coffheader.scns[1];
bp = &coffheader.scns[2];
strcpy (tp->s_name, ".text");
tp->s_paddr = text_start;
tp->s_vaddr = text_start;
tp->s_size = text_size;
tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
tp->s_relptr = 0;
tp->s_lnnoptr = 0;
tp->s_nreloc = 0;
tp->s_nlnno = 0;
tp->s_flags = 0x20;
strcpy (dp->s_name, ".data");
dp->s_paddr = data_start;
dp->s_vaddr = data_start;
dp->s_size = data_size;
dp->s_scnptr = tp->s_scnptr + tp->s_size;
dp->s_relptr = 0;
dp->s_lnnoptr = 0;
dp->s_nreloc = 0;
dp->s_nlnno = 0;
dp->s_flags = 0x40;
strcpy (bp->s_name, ".bss");
bp->s_paddr = dp->s_vaddr + dp->s_size;
bp->s_vaddr = bp->s_paddr;
bp->s_size = bss_size;
bp->s_scnptr = 0;
bp->s_relptr = 0;
bp->s_lnnoptr = 0;
bp->s_nreloc = 0;
bp->s_nlnno = 0;
bp->s_flags = 0x80;
coffheader.f_magic = COFF_MAGIC;
coffheader.f_nscns = 3;
coffheader.f_timdat = 0;
coffheader.f_symptr = 0;
coffheader.f_nsyms = 0;
coffheader.f_opthdr = 28;
coffheader.f_flags = 0x103;
/* aouthdr */
coffheader.magic = ZMAGIC;
coffheader.vstamp = 0;
coffheader.tsize = tp->s_size;
coffheader.dsize = dp->s_size;
coffheader.bsize = bp->s_size;
coffheader.entry = outheader.a_entry;
coffheader.text_start = tp->s_vaddr;
coffheader.data_start = dp->s_vaddr;
}
#endif
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
#ifdef INITIALIZE_HEADER
INITIALIZE_HEADER;
#endif
#endif
if (strip_symbols == STRIP_ALL)
nsyms = 0;
else
{
nsyms = (defined_global_sym_count
+ undefined_global_sym_count);
if (discard_locals == DISCARD_L)
nsyms += non_L_local_sym_count;
else if (discard_locals == DISCARD_NONE)
nsyms += local_sym_count;
/* One extra for following reference on indirects */
if (relocatable_output)
nsyms += set_symbol_count + global_indirect_count;
#if defined(atarist) || defined(CROSSATARI)
nsyms += set_symbol_count; /* Cos we always output them if possible */
#endif
}
if (strip_symbols == STRIP_NONE)
nsyms += debugger_sym_count;
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
outheader.a_syms = nsyms * sizeof (struct nlist);
#else
# ifndef WORD_ALIGNED
outheader.a_syms = (nsyms + gst_symbols) * sizeof (struct asym);
# else
# ifdef MINIX
outheader.a_syms = (nsyms + gst_symbols) * 16;
# else
outheader.a_syms = (nsyms + gst_symbols) * 14;
# endif
# endif
#endif
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
if (relocatable_output)
{
outheader.a_trsize = text_reloc_size;
outheader.a_drsize = data_reloc_size;
}
else
{
outheader.a_trsize = 0;
outheader.a_drsize = 0;
}
#endif
#ifdef COFF_ENCAPSULATE
if (need_coff_header)
mywrite (&coffheader, sizeof coffheader, 1, outdesc);
#endif
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
mywrite (&outheader, sizeof (struct exec), 1, outdesc);
#else
#ifdef BYTE_SWAP
outheader.a_magic = SWAP2(outheader.a_magic);
outheader.a_text = SWAP4(outheader.a_text);
outheader.a_data = SWAP4(outheader.a_data);
outheader.a_bss = SWAP4(outheader.a_bss);
outheader.a_syms = SWAP4(outheader.a_syms);
outheader.a_AZero1 = SWAP4(outheader.a_AZero1);
outheader.a_ldflgs = SWAP4(outheader.a_ldflgs); /* only do this once */
outheader.a_isreloc = SWAP2(outheader.a_isreloc);
#endif /* BYTE_SWAP */
# ifndef WORD_ALIGNED
mywrite ((char *)&outheader, sizeof outheader, 1, outdesc);
# else
# ifndef MINIX
mywrite (&outheader.a_magic , sizeof outheader.a_magic , 1, outdesc);
mywrite (&outheader.a_text , sizeof outheader.a_text , 1, outdesc);
mywrite (&outheader.a_data , sizeof outheader.a_data , 1, outdesc);
mywrite (&outheader.a_bss , sizeof outheader.a_bss , 1, outdesc);
mywrite (&outheader.a_syms , sizeof outheader.a_syms , 1, outdesc);
mywrite (&outheader.a_AZero1 , sizeof outheader.a_AZero1 , 1, outdesc);
mywrite (&outheader.a_AZero2 , sizeof outheader.a_AZero2 , 1, outdesc);
mywrite (&outheader.a_isreloc, sizeof outheader.a_isreloc, 1, outdesc);
# else
mywrite (&outheader.a_magic , sizeof outheader.a_magic , 1, outdesc);
mywrite (&outheader.a_versn , sizeof outheader.a_versn , 1, outdesc);
mywrite (&outheader.a_text , sizeof outheader.a_text , 1, outdesc);
mywrite (&outheader.a_data , sizeof outheader.a_data , 1, outdesc);
mywrite (&outheader.a_bss , sizeof outheader.a_bss , 1, outdesc);
mywrite (&outheader.a_entry , sizeof outheader.a_entry , 1, outdesc);
mywrite (&outheader.a_msize , sizeof outheader.a_msize , 1, outdesc);
mywrite (&outheader.a_syms , sizeof outheader.a_syms , 1, outdesc);
# endif
# endif
#endif
#ifdef BYTE_SWAP /* switch it back !!! */
outheader.a_magic = SWAP2(outheader.a_magic);
outheader.a_text = SWAP4(outheader.a_text);
outheader.a_data = SWAP4(outheader.a_data);
outheader.a_bss = SWAP4(outheader.a_bss);
outheader.a_syms = SWAP4(outheader.a_syms);
outheader.a_AZero1 = SWAP4(outheader.a_AZero1);
outheader.a_ldflgs = SWAP4(outheader.a_ldflgs); /* only do this once */
outheader.a_isreloc = SWAP2(outheader.a_isreloc);
#endif /* BYTE_SWAP */
/* Output whatever padding is required in the executable file
between the header and the start of the text. */
#ifndef COFF_ENCAPSULATE
# if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
# endif
#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 ();
/* Write out the set element vectors */
if (set_vector_count)
mywrite ((char *) set_vectors,
set_symbol_count + 2 * set_vector_count,
sizeof (unsigned long), outdesc);
if (trace_files)
fprintf (stderr, "\n");
padfile (text_pad, outdesc);
}
int
text_offset (entry)
struct file_entry *entry;
{
return entry->starting_offset + N_TXTOFF (entry->header);
}
/* Read in all of the relocation information */
void
read_relocation ()
{
each_full_file (read_file_relocation, 0);
}
/* Read in the relocation sections of ENTRY if necessary */
void
read_file_relocation (entry)
struct file_entry *entry;
{
register struct relocation_info *reloc;
FILE *desc;
int read_return;
desc = NULL;
if (!entry->textrel)
{
reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
desc = file_open (entry);
fseek (desc,
text_offset (entry) + entry->header.a_text + entry->header.a_data,
0);
if (entry->header.a_trsize != (read_return = fread (reloc, 1, entry->header.a_trsize, desc)))
{
fprintf (stderr, "Return from read: %d\n", read_return);
fatal_with_file ("premature eof in text relocatino of ", entry);
}
entry->textrel = reloc;
}
if (!entry->datarel)
{
reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
if (desc == NULL) desc = file_open (entry);
fseek (desc,
text_offset (entry) + entry->header.a_text
+ entry->header.a_data + entry->header.a_trsize,
0);
if (entry->header.a_drsize != fread (reloc, 1, entry->header.a_drsize, desc))
fatal_with_file ("premature eof in text relocation of ", entry);
entry->datarel = reloc;
}
}
/* 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 FILE *desc;
register struct relocation_info *reloc;
if (trace_files)
prline_file_name (entry, stderr);
desc = file_open (entry);
/* Allocate space for the file's text section */
bytes = (char *) alloca (entry->header.a_text);
/* Deal with relocation information however is appropriate */
if (entry->textrel) reloc = entry->textrel;
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
else if (1) /* always keep it on atari */
#else
else if (relocatable_output)
#endif
{
read_file_relocation (entry);
reloc = entry->textrel;
}
else
{
reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
fseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
if (entry->header.a_trsize != fread (reloc, 1, entry->header.a_trsize, desc))
fatal_with_file ("premature eof in text relocation of ", entry);
}
/* Read the text section into core. */
fseek (desc, text_offset (entry), 0);
if (entry->header.a_text != fread ( bytes, 1, entry->header.a_text, desc))
fatal_with_file ("premature eof in text section of ", entry);
/* Relocate the text according to the text relocation. */
perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
reloc, entry->header.a_trsize, entry, (magic == NMAGIC));
/* Write the relocated text to the output file. */
mywrite (bytes, 1, entry->header.a_text, outdesc);
}
/* Relocate the data segment of each input file
and write to the output file. */
void
write_data ()
{
if (trace_files)
fprintf (stderr, "Copying and relocating data:\n\n");
each_full_file (copy_data, 0);
file_close ();
if (trace_files)
fprintf (stderr, "\n");
padfile (data_pad, outdesc);
}
/* Read the data segment contents of ENTRY, relocate them,
and write the result to the output file.
If `-r', save the data relocation for later reuse.
See comments in `copy_text'. */
void
copy_data (entry)
struct file_entry *entry;
{
register struct relocation_info *reloc;
register char *bytes;
register FILE *desc;
if (trace_files)
prline_file_name (entry, stderr);
desc = file_open (entry);
bytes = (char *) alloca (entry->header.a_data);
if (entry->datarel) reloc = entry->datarel;
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
else if (1)
#else
else if (relocatable_output) /* Will need this again */
#endif
{
read_file_relocation (entry);
reloc = entry->datarel;
}
else
{
reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
fseek (desc, text_offset (entry) + entry->header.a_text
+ entry->header.a_data + entry->header.a_trsize,
0);
if (entry->header.a_drsize != fread (reloc, 1, entry->header.a_drsize, desc))
fatal_with_file ("premature eof in data relocation of ", entry);
}
fseek (desc, text_offset (entry) + entry->header.a_text, 0);
if (entry->header.a_data != fread (bytes, 1, entry->header.a_data, desc))
fatal_with_file ("premature eof in data section of ", entry);
perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
entry->header.a_data, reloc, entry->header.a_drsize,
entry, 0);
mywrite (bytes, 1, entry->header.a_data, outdesc);
}
static symbol *
retsym (p, entry)
struct relocation_info *p;
struct file_entry *entry;
{
register int symindex;
register symbol *sp;
symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
if (symindex >= entry->header.a_syms)
fatal_with_file ("relocation symbolnum out of range in ", entry);
sp = ((symbol *) (((struct nlist *) (((char *)entry->symbols) + symindex))
->n_un.n_name));
#ifdef N_INDR
/* Resolve indirection */
if ((sp->defined & ~N_EXT) == N_INDR)
sp = (symbol *) sp->value;
#endif
return sp;
}
static char *
retsymname (p, entry, addr)
struct relocation_info *p;
struct file_entry *entry;
int addr;
{
register struct nlist *n;
if (RELOC_EXTERN_P(p)) {
register symbol *sp = retsym(p, entry);
return sp->name;
}
for (n=entry->symbols;
n<(struct nlist *)((long)entry->symbols + entry->header.a_syms); n++)
if (n->n_value == addr)
return(entry->strings + n->n_un.n_strx);
return "(static symbol)";
}
/* Relocate ENTRY's text or data section contents.
DATA is the address of the contents, in core.
DATA_SIZE is the length of the contents.
PC_RELOCATION is the difference between the address of the contents
in the output file and its address in the input file.
RELOC_INFO is the address of the relocation info, in core.
RELOC_SIZE is its length in bytes. */
/* This version is about to be severley hacked by Randy. Hope it
works afterwards. */
void
perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size,
entry, pure_text)
char *data;
struct relocation_info *reloc_info;
struct file_entry *entry;
int pc_relocation;
int data_size;
int reloc_size;
int pure_text;
{
register struct relocation_info *p = reloc_info;
struct relocation_info *end
= reloc_info + reloc_size / sizeof (struct relocation_info);
int text_relocation = entry->text_start_address;
int data_relocation = entry->data_start_address - entry->header.a_text;
int bss_relocation
= entry->bss_start_address - entry->header.a_text - entry->header.a_data;
for (; p < end; p++)
{
register int relocation = 0;
register int addr;
register unsigned int mask = 0;
int oldreloc;
#ifdef BYTE_SWAP /* swap relocation info */
{ /* BITFIELD ALERT BITFIELD ALERT */
char *p1,*p2,p3;
p->r_address = SWAP4(p->r_address);
/* terrible hack to fix bit-fields !!! */
p1 = (char *)&p->r_address + 4;
p2 = p1+2;
p3= *p1; *p1= *p2; *p2=p3; /* adjust r_symbolnum */
p1+= 3; /* adjust everything else */
*p1 = ((*p1&0xf0)>>4)+((*p1&0x0f)<<4); /* swap padding into correct spot */
*p1 = (*p1&0x6)+((*p1&0x8)>>3)+((*p1&0x1)<<3); /* adjust other field */
}
#endif /* BYTE_SWAP */
addr = RELOC_ADDRESS(p);
if (addr >= data_size)
fatal_with_file ("relocation address out of range in ", entry);
if (RELOC_EXTERN_P(p))
{
register symbol *sp;
sp = retsym(p, entry);
/* If the symbol is undefined, leave it at zero. */
if (! sp->defined)
relocation = 0;
else
relocation = sp->value;
}
else switch (RELOC_TYPE(p))
{
case N_TEXT:
case N_TEXT | N_EXT:
relocation = text_relocation;
break;
case N_DATA:
case N_DATA | N_EXT:
/* A word that points to beginning of the the data section
initially contains not 0 but rather the "address" of that section
in the input file, which is the length of the file's text. */
relocation = data_relocation;
break;
case N_BSS:
case N_BSS | N_EXT:
/* Similarly, an input word pointing to the beginning of the bss
initially contains the length of text plus data of the file. */
relocation = bss_relocation;
break;
case N_ABS:
case N_ABS | N_EXT:
/* Don't know why this code would occur, but apparently it does. */
break;
default:
fatal_with_file ("nonexternal relocation code invalid in ", entry);
}
if (RELOC_PCREL_P(p))
relocation -= pc_relocation;
oldreloc = relocation;
#ifdef RELOC_ADD_EXTRA
relocation += RELOC_ADD_EXTRA(p);
#endif
relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
/* Unshifted mask for relocation */
mask = 1 << (RELOC_TARGET_BITSIZE(p) - 1);
mask |= mask - 1;
relocation &= mask;
/* Shift everything up to where it's going to be used */
relocation <<= RELOC_TARGET_BITPOS(p);
mask <<= RELOC_TARGET_BITPOS(p);
switch (RELOC_TARGET_SIZE(p))
{
case 0:
if (RELOC_MEMORY_ADD_P(p))
relocation += mask & *(char *) (data + addr);
*(char *) (data + addr) &= ~mask;
*(char *) (data + addr) |= relocation;
break;
case 1:
{
register int word;
if (RELOC_MEMORY_ADD_P(p)) {
if (RELOC_PCREL_P(p)||RELOC_EXTERN_P(p))
word = *(short *) (data + addr);
else
word = *(unsigned short *) (data + addr);
#ifdef BYTE_SWAP
word = SWAP2(word);
#endif
relocation += mask & word;
}
if (relocation > 32767 || relocation < -32768) {
if (!RELOC_EXTERN_P(p))
if (!entry->strings) {
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (file_open (entry), entry);
}
error ("fatal error: relocation out of range for %s in %s",
retsymname(p, entry, oldreloc+word), entry->filename);
make_executable = 0;
}
word &= ~mask;
word |= relocation;
#ifdef BYTE_SWAP
word = SWAP2(word);
#endif
*(short *) (data + addr) = word;
}
break;
case 2:
{
register unsigned long word;
#ifndef WORD_ALIGNED
word = *(long *) (data + addr);
#ifdef BYTE_SWAP
word = SWAP4(word);
#endif /* BYTE_SWAP */
#else
word = ((unsigned long)(unsigned char)(data[addr])) << 24;
word |= ((unsigned long)(unsigned char)(data[addr+1])) << 16;
word |= ((unsigned long)(unsigned char)(data[addr+2])) << 8;
word |= ((unsigned long)(unsigned char)(data[addr+3]));
#ifdef BYTE_SWAP
/* NOTE: We dont need to swap 4 because we are constructing
the word here */
#endif
#endif
if (RELOC_MEMORY_ADD_P(p))
relocation += mask & word;
if (pure_text && relocation > text_size+text_start) {
if (!RELOC_EXTERN_P(p))
if (!entry->strings) {
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (file_open (entry), entry);
}
error ("absolute reference to %s from pure-text in %s",
retsymname(p, entry, relocation), entry->filename);
make_executable = 0; /* set exit code */
}
word &= ~mask;
word |= relocation;
#if (defined(BYTE_SWAP) && (!defined(WORD_ALIGNED)))
word = SWAP4(word);
#endif
#ifndef WORD_ALIGNED
*(long *) (data + addr) = word;
#else
data[addr+3] = word;
word >>= 8;
data[addr+2] = word;
word >>= 8;
data[addr+1] = word;
word >>= 8;
data[addr] = word;
#endif
}
break;
default:
fatal_with_file ("Unimplemented relocation field length in ", entry);
}
#if 0 /*BYTE_SWAP*/ /* swap relocation info */
{ /* BITFIELD ALERT BITFIELD ALERT */ /* switch it back */
char *p1,*p2,p3;
p->r_address = SWAP4(p->r_address);
/* terrible hack to fix bit-fields !!! */
p1 = (char *)&p->r_address + 4;
p2 = p1+2;
p3= *p1; *p1= *p2; *p2=p3; /* adjust r_symbolnum */
p1+= 3; /* adjust everything else */
*p1 = ((*p1&0xf0)>>4)+((*p1&0x0f)<<4); /* swap padding into correct spot */
*p1 = (*p1&0x6)+((*p1&0x8)>>3)+((*p1&0x1)<<3); /* adjust other fields */
}
#endif /* BYTE_SWAP */
}
entry->strings = 0;
}
/* For relocatable_output only: write out the relocation,
relocating the addresses-to-be-relocated. */
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
void emit_atari_text_relocs (), emit_atari_data_relocs ();
void emit_atari_set_vectors();
void flush_atari_rbuf();
void emit_atari_reloc_byte();
#else
void coptxtrel (), copdatrel ();
#endif
void
write_rel ()
{
register int i;
register int count = 0;
if (trace_files)
fprintf (stderr, "Writing text relocation:\n\n");
/* Assign each global symbol a sequence number, giving the order
in which `write_syms' will write it.
This is so we can store the proper symbolnum fields
in relocation entries we write. */
for (i = 0; i < TABSIZE; i++)
{
symbol *sp;
for (sp = symtab[i]; sp; sp = sp->link)
if (sp->referenced || sp->defined)
sp->def_count = count++;
}
if (count != defined_global_sym_count + undefined_global_sym_count)
fatal ("internal error", 0);
/* Write out the relocations of all files, remembered from copy_text. */
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
each_full_file (coptxtrel, 0);
#else
each_full_file (emit_atari_text_relocs, 0);
emit_atari_set_vectors();
#endif
if (trace_files)
fprintf (stderr, "\nWriting data relocation:\n\n");
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
each_full_file (copdatrel, 0);
#else
each_full_file (emit_atari_data_relocs, 0);
if (rbuf_last_pc == -1)
{
emit_atari_reloc_byte(0);
emit_atari_reloc_byte(0);
emit_atari_reloc_byte(0);
}
emit_atari_reloc_byte(0); /* end of relocs */
flush_atari_rbuf();
#endif
if (trace_files)
fprintf (stderr, "\n");
}
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
/* code here for dumping atari style reloc info */
void flush_atari_rbuf()
{
/* fprintf(stderr, "flush\n"); */
if(rbuf_size > 0)
fwrite(rbuf, 1, rbuf_size, outdesc);
rbuf_size = 0;
}
void emit_atari_reloc_byte(b)
unsigned int b;
{
unsigned char byte = b & 0xFF;
/*fprintf(stderr, " %X\n", byte);*/
if (rbuf_size >= RBUF_SIZE)
flush_atari_rbuf();
rbuf[rbuf_size++] = byte;
}
int emit_atari_reloc(rel_pc)
long rel_pc;
{
long diff;
/* fprintf(stderr, "reloc at %X\n", rel_pc); */
if (rbuf_last_pc == -1) /* first time thru? */
{
emit_atari_reloc_byte(rel_pc >> 24);
emit_atari_reloc_byte(rel_pc >> 16);
emit_atari_reloc_byte(rel_pc >> 8);
emit_atari_reloc_byte(rel_pc);
}
else
{
if (rel_pc <= rbuf_last_pc)
{
fprintf(stderr, "Relocs out of order; last = %X curr = %X\n",
rbuf_last_pc, rel_pc);
/* exit(-999); */
return(0);
}
diff = rel_pc - rbuf_last_pc;
while (diff > 254)
{
emit_atari_reloc_byte(1);
diff -= 254;
}
emit_atari_reloc_byte(diff);
}
rbuf_last_pc = rel_pc;
return(1);
}
int reloc_less_p(reloc_1, reloc_2)
struct relocation_info * reloc_1, * reloc_2;
{
#if 0
return(reloc_1->r_address < reloc_2->r_address);
#else
return(reloc_2->r_address - reloc_1->r_address);
#endif
}
void emit_atari_text_relocs(entry)
struct file_entry *entry;
{
int reloc_num;
int n_relocs = entry->header.a_trsize / sizeof (struct relocation_info);
long text_base = entry->text_start_address;
qsort(entry->textrel, n_relocs, sizeof (struct relocation_info),
reloc_less_p);
for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
if ((entry->textrel[reloc_num].r_length == 2) &&
!emit_atari_reloc(entry->textrel[reloc_num].r_address + text_base))
fprintf(stderr, " while processing text rel %d/%d of entry %s\n",
reloc_num, n_relocs, entry->filename);
}
void emit_atari_data_relocs(entry)
struct file_entry *entry;
{
int reloc_num;
int n_relocs = entry->header.a_drsize / sizeof (struct relocation_info);
long data_base = entry->data_start_address;
qsort(entry->datarel, n_relocs, sizeof (struct relocation_info),
reloc_less_p);
for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
if (!emit_atari_reloc(entry->datarel[reloc_num].r_address + data_base))
fprintf(stderr, " while processing data rel %d/%d of entry %s\n",
reloc_num, n_relocs, entry->filename);
}
/* Handle set vectors. This kludge should only be necessary for C++.
* Skip through the set vectors, outputting relocation for each vector.
*/
void emit_atari_set_vectors()
{
unsigned long *current_element = set_vectors;
int num_vectors = 0, loop;
long num_elements = 0;
if (set_sect_size != 0) {
current_element = set_vectors;
while (num_elements < (set_sect_size / sizeof(unsigned long))) {
num_vectors = *current_element++; /* Number of vectors for current sym */
num_elements++;
for (loop = 1; loop <= num_vectors; loop++) {
emit_atari_reloc(set_sect_start + num_elements++ * sizeof (unsigned long));
current_element++;
}
current_element++; num_elements++; /* There's a zero at the end. */
}
}
}
#endif
#if (!((defined(CROSSATARI) || defined(atarist) || defined(atariminix))))
void
coptxtrel (entry)
struct file_entry *entry;
{
register struct relocation_info *p, *end;
register int reloc = entry->text_start_address;
p = entry->textrel;
end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
while (p < end)
{
RELOC_ADDRESS(p) += reloc;
if (RELOC_EXTERN_P(p))
{
register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
symbol *symptr = ((symbol *)
(((struct nlist *)
(((char *)entry->symbols) + symindex))
->n_un.n_name));
if (symindex >= entry->header.a_syms)
fatal_with_file ("relocation symbolnum out of range in ", entry);
/* If the symbol is now defined, change the external relocation
to an internal one. */
if (symptr->defined)
{
RELOC_EXTERN_P(p) = 0;
RELOC_SYMBOL(p) = (symptr->defined & ~N_EXT);
}
else
RELOC_SYMBOL(p) = (symptr->def_count + nsyms
- defined_global_sym_count
- undefined_global_sym_count);
}
p++;
}
mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
}
void
copdatrel (entry)
struct file_entry *entry;
{
register struct relocation_info *p, *end;
/* Relocate the address of the relocation.
Old address is relative to start of the input file's data section.
New address is relative to start of the output file's data section. */
register int reloc = entry->data_start_address - text_size;
p = entry->datarel;
end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
while (p < end)
{
RELOC_ADDRESS(p) += reloc;
if (RELOC_EXTERN_P(p))
{
register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
symbol *symptr = ((symbol *)
(((struct nlist *)
(((char *)entry->symbols) + symindex))
->n_un.n_name));
int symtype = symptr->defined & ~N_EXT;
if (symindex >= entry->header.a_syms)
fatal_with_file ("relocation symbolnum out of range in ", entry);
if (force_common_definition
|| symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
{
RELOC_EXTERN_P(p) = 0;
RELOC_SYMBOL(p) = symtype;
}
else
RELOC_SYMBOL(p)
= (((symbol *)
(((struct nlist *)
(((char *)entry->symbols) + symindex))
->n_un.n_name))
->def_count
+ nsyms - defined_global_sym_count
- undefined_global_sym_count);
}
p++;
}
mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
}
#endif /* ! atari */
void write_file_syms ();
void write_string_table ();
#if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
/* this is all different on ataris */
#if (!(defined(MINIX) || defined(atariminix)))
/* atari TOS version */
#define N_ATARI_FILENAME 0x1E
void write_atari_sym(p, str)
register struct nlist * p;
char * str;
{
struct asym sym;
int i;
char *ptr;
/* fprintf(stderr, "sym %2ld %s\n", strlen(str), str); */
switch (p->n_type & N_TYPE)
{
case N_UNDF: sym.a_type = A_UNDF; break;
case N_ABS: sym.a_type = A_EQU; break; /* ??? */
case N_SETT:
case N_TEXT: sym.a_type = A_TEXT; break;
case N_SETV:
case N_SETD:
case N_DATA: sym.a_type = A_DATA; break;
case N_SETB:
case N_BSS: sym.a_type = A_BSS; break;
case N_ATARI_FILENAME: sym.a_type = A_TFILE; break;
default: /* fake it */
sym.a_type = A_UNDF;
bzero(sym.a_name, sizeof(sym.a_name));
strncpy(sym.a_name, str, sizeof(sym.a_name));
i = 0;
goto fake_out;
/* figure out rest of this later */
}
if (p->n_type & N_EXT) /* differenciate between local/global syms */
sym.a_type |= A_GLOBL;
ptr = &(sym.a_name[0]);
i = sizeof(sym.a_name);
while (--i >= 0 && ('\0' != (*ptr++ = *str)))
str++;
/* if i >= 0 then *str == '\0' and if i == 0 there is nothing to fill */
if (i > 0) /* we are done - fill it with 0 */
{
do {
*ptr++ = '\0';
} while (--i > 0);
}
else if (*str && gst_symboltable) /* if more to write */
{
sym.a_type |= A_LNAM;
i = sizeof sym;
gst_symbols++;
}
fake_out:
sym.a_value = p->n_value;
#ifndef WORD_ALIGNED
mywrite((char *)&sym, sizeof sym, 1, outdesc);
if (i > 0)
{
(void) strncpy ((char *) &sym, str, (size_t)i);
mywrite((char *)&sym, sizeof sym, 1, outdesc);
}
#else
mywrite(sym.a_name , sizeof sym.a_name , 1, outdesc);
mywrite(&sym.a_type , sizeof sym.a_type , 1, outdesc);
mywrite(&sym.a_value, sizeof sym.a_value, 1, outdesc);
if (i > 0)
{
(void) strncpy ((char *) &sym, str, (size_t)i);
mywrite(&sym, 14, 1, outdesc);
}
#endif
}
void write_atari_syms(entry, syms_written_addr)
struct file_entry * entry;
int * syms_written_addr;
{
register struct nlist *p = entry->symbols;
register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
/* int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1; */
/* Read the file's string table. */
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (file_open (entry), entry);
/* Generate a local symbol for the start of this file's text. */
if (discard_locals != DISCARD_ALL)
{
struct nlist nl;
nl.n_type = N_ATARI_FILENAME;
/* 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; */
write_atari_sym(&nl, entry->local_sym_name);
(*syms_written_addr)++;
/* necessary? */
entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
}
for (; p < end; p++)
{
register int type = p->n_type;
register int write = 0;
int is_a_set_ele;
/* WRITE gets 1 for a non-global symbol that should be written. */
if ((is_a_set_ele = SET_ELEMENT_P (type))) /* This occurs even if global. These */
/* types of symbols are never written */
/* globally, though they are stored */
/* globally. */
write = 1;
else if (!(type & (N_STAB | N_EXT)))
/* ordinary local symbol */
write = (discard_locals != DISCARD_ALL)
&& !(discard_locals == DISCARD_L &&
(p->n_un.n_strx + entry->strings)[0] == 'L');
else if (!(type & N_EXT))
/* debugger symbol */
write = (strip_symbols == STRIP_NONE);
if (write)
{
/* If this symbol has a name, write it */
if (p->n_un.n_strx)
{
write_atari_sym(p, ((is_a_set_ele)?
((symbol *) p->n_un.n_name)->name : p->n_un.n_strx + entry->strings));
#if 0
"SET_ELEMENT" : p->n_un.n_strx + entry->strings));
#endif
(*syms_written_addr)++;
}
}
}
entry->strings = NULL;
}
void write_syms ()
{
int n_syms_written = 0;
register symbol *sp;
int i;
if (strip_symbols == STRIP_ALL)
return;
each_file(write_atari_syms, &n_syms_written);
/* Now write out the global symbols. */
/* Scan the symbol hash table, bucket by bucket. */
for (i = 0; i < TABSIZE; i++)
for (sp = symtab[i]; sp; sp = sp->link)
{
struct nlist nl;
nl.n_other = 0;
nl.n_desc = 0;
/* Compute a `struct nlist' for the symbol. */
if (sp->defined || sp->referenced)
{
if (!sp->defined) /* undefined -- legit only if -r */
{
nl.n_type = N_UNDF | N_EXT;
nl.n_value = 0;
}
else if (sp->defined > 1) /* defined with known type */
{
/* If the target of an indirect symbol has been
defined and we are outputting an executable,
resolve the indirection; it's no longer needed */
if (!relocatable_output
&& ((sp->defined & ~N_EXT) == N_INDR)
&& (((symbol *) sp->value)->defined > 1))
{
symbol *newsp = (symbol *) sp->value;
nl.n_type = newsp->defined;
nl.n_value = newsp->value;
}
else
{
nl.n_type = sp->defined;
if (sp->defined != (N_INDR | N_EXT))
nl.n_value = sp->value;
else
nl.n_value = 0;
}
}
else if (sp->max_common_size) /* defined as common but not allocated. */
{
/* happens only with -r and not -d */
/* write out a common definition */
nl.n_type = N_UNDF | N_EXT;
nl.n_value = sp->max_common_size;
}
else
fatal ("internal error: %s defined in mysterious way", sp->name);
/* write and count it. */
#ifdef BYTE_SWAP
nl.n_un.n_strx = SWAP4(nl.n_un.n_strx);
nl.n_desc = SWAP2(nl.n_desc);
nl.n_value = SWAP4(nl.n_value);
#endif
write_atari_sym(&nl, sp->name);
n_syms_written++;
}
}
/* this is really stretching it, but we have to fake it */
if(n_syms_written < nsyms)
{
struct nlist nl;
nl.n_value = 0;
nl.n_type = (0x7ffff & N_TYPE); /* make sure it falls into the default */
/* case in write_atari_sym() */
while(n_syms_written < nsyms)
{
#ifdef BYTE_SWAP
nl.n_un.n_strx = SWAP4(nl.n_un.n_strx);
nl.n_desc = SWAP2(nl.n_desc);
nl.n_value = SWAP4(nl.n_value);
#endif
write_atari_sym(&nl, "FAKEEAKF");
n_syms_written++;
}
}
if (n_syms_written != nsyms)
fprintf(stderr, "Bogon alert! wrote %d syms, expected to write %d\n",
n_syms_written, nsyms);
}
#else /* Atari Minix */
void write_atari_sym(p, str, gflag)
register struct nlist * p;
char * str;
int gflag;
{
struct asym sym;
int i;
/* fprintf(stderr, "sym %s\n", str); */
for (i = 0 ; ((i < 8) && str[i]) ; i++)
sym.a_name[i] = str[i];
for ( ; i < 8 ; i++)
sym.a_name[i] = ' ';
switch (p->n_type & N_TYPE)
{
case N_UNDF: sym.a_sclass = A_UNDF; break;
case N_ABS: sym.a_sclass = A_ABS; break;
case N_TEXT: sym.a_sclass = A_TEXT; break;
case N_DATA: sym.a_sclass = A_DATA; break;
case N_BSS: sym.a_sclass = A_BSS; break;
default: sym.a_sclass = 0;
}
if(gflag)
sym.a_sclass |= A_EXT;
sym.a_value = p->n_value;
sym.a_numaux = sym.a_type = 0;
#ifndef WORD_ALIGNED
mywrite(&sym, sizeof sym, 1, outdesc);
#else
mywrite(&sym.a_name , sizeof sym.a_name , 1, outdesc);
mywrite(&sym.a_value, sizeof sym.a_value, 1, outdesc);
mywrite(&sym.a_sclass, sizeof sym.a_sclass, 1, outdesc);
mywrite(&sym.a_numaux , sizeof sym.a_numaux , 1, outdesc);
mywrite(&sym.a_type , sizeof sym.a_type , 1, outdesc);
#endif
}
void write_atari_syms(entry, syms_written_addr)
struct file_entry * entry;
int * syms_written_addr;
{
register struct nlist *p = entry->symbols;
register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
/* Read the file's string table. */
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (file_open (entry), entry);
/* Generate a local symbol for the start of this file's text. */
if (discard_locals != DISCARD_ALL)
{
struct nlist nl;
nl.n_type = N_TEXT;
/* 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; */
write_atari_sym(&nl, entry->local_sym_name, 0);
(*syms_written_addr)++;
/* necessary? */
entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
}
for (; p < end; p++)
{
register int type = p->n_type;
register int write = 0;
/* WRITE gets 1 for a non-global symbol that should be written. */
if (!(type & (N_STAB | N_EXT)))
/* ordinary local symbol */
write = (discard_locals != DISCARD_ALL)
&& !(discard_locals == DISCARD_L &&
(p->n_un.n_strx + entry->strings)[0] == 'L');
else if (!(type & N_EXT))
/* debugger symbol */
/* write = (strip_symbols == STRIP_NONE); */ write = 0;
if (write)
{
/* If this symbol has a name, write it */
if (p->n_un.n_strx)
{
write_atari_sym(p, p->n_un.n_strx + entry->strings, 0);
(*syms_written_addr)++;
}
}
}
entry->strings = NULL;
}
void write_syms ()
{
int n_syms_written = 0;
register symbol *sp;
int i;
if (strip_symbols == STRIP_ALL)
return;
each_file(write_atari_syms, &n_syms_written);
/* Now write out the global symbols. */
/* Scan the symbol hash table, bucket by bucket. */
for (i = 0; i < TABSIZE; i++)
for (sp = symtab[i]; sp; sp = sp->link)
{
struct nlist nl;
nl.n_other = 0;
nl.n_desc = 0;
/* Compute a `struct nlist' for the symbol. */
if (sp->defined || sp->referenced)
{
if (!sp->defined) /* undefined -- legit only if -r */
{
nl.n_type = N_UNDF | N_EXT;
nl.n_value = 0;
}
else if (sp->defined > 1) /* defined with known type */
{
nl.n_type = sp->defined;
nl.n_value = sp->value;
}
else if (sp->max_common_size) /* defined as common but not allocated. */
{
/* happens only with -r and not -d */
/* write out a common definition */
nl.n_type = N_UNDF | N_EXT;
nl.n_value = sp->max_common_size;
}
else
fatal ("internal error: %s defined in mysterious way", sp->name);
/* write and count it. */
write_atari_sym(&nl, sp->name, 1);
n_syms_written++;
}
}
if (n_syms_written != nsyms)
{
register long pos;
#if 0
fprintf(stderr, "Bogon alert! wrote %d syms, expected to write %d\n",
n_syms_written, nsyms);
#endif
/* go patch header */
#ifndef WORD_ALIGNED
n_syms_written = n_syms_written * sizeof(struct asym);
#else
n_syms_written = n_syms_written * 16;
#endif
pos = ftell(outdesc);
fseek(outdesc, 28L, 0);
mywrite(&n_syms_written, 4, 1, outdesc);
fseek(outdesc, pos, 0);
}
}
#endif /* MINIX */
#else /* for Gnu/Unix */
/* Offsets and current lengths of symbol and string tables in output file. */
int symbol_table_offset;
int symbol_table_len;
/* Address in output file where string table starts. */
int string_table_offset;
/* Offset within string table
where the strings in `strtab_vector' should be written. */
int string_table_len;
/* Total size of string table strings allocated so far,
including strings in `strtab_vector'. */
int strtab_size;
/* Vector whose elements are strings to be added to the string table. */
char **strtab_vector;
/* Vector whose elements are the lengths of those strings. */
int *strtab_lens;
/* Index in `strtab_vector' at which the next string will be stored. */
int strtab_index;
/* Add the string NAME to the output file string table.
Record it in `strtab_vector' to be output later.
Return the index within the string table that this string will have. */
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;
}
FILE *outstream = (FILE *) 0;
/* Write the contents of `strtab_vector' into the string table.
This is done once for each file's local&debugger symbols
and once for the global symbols. */
void
write_string_table ()
{
register int i;
fseek (outdesc, string_table_offset + string_table_len, 0);
if (!outstream)
outstream = outdesc;
for (i = 0; i < strtab_index; i++)
{
fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
string_table_len += strtab_lens[i];
}
fflush (outstream);
/* Report I/O error such as disk full. */
if (ferror (outstream))
perror_name (output_filename);
}
/* Write the symbol table and string table of the output file. */
void
write_syms ()
{
/* Number of symbols written so far. */
int syms_written = 0;
register int i;
register symbol *sp;
/* Buffer big enough for all the global symbols. One
extra struct for each indirect symbol to hold the extra reference
following. */
struct nlist *buf
= (struct nlist *) alloca ((defined_global_sym_count
+ undefined_global_sym_count
+ global_indirect_count)
* sizeof (struct nlist));
/* Pointer for storing into BUF. */
register struct nlist *bufp = buf;
/* Size of string table includes the bytes that store the size. */
strtab_size = sizeof strtab_size;
symbol_table_offset = N_SYMOFF (outheader);
symbol_table_len = 0;
string_table_offset = N_STROFF (outheader);
string_table_len = strtab_size;
if (strip_symbols == STRIP_ALL)
return;
/* Write the local symbols defined by the various files. */
each_file (write_file_syms, &syms_written);
file_close ();
/* Now write out the global symbols. */
/* Allocate two vectors that record the data to generate the string
table from the global symbols written so far. This must include
extra space for the references following indirect outputs. */
strtab_vector = (char **) alloca ((num_hash_tab_syms
+ global_indirect_count) * sizeof (char *));
strtab_lens = (int *) alloca ((num_hash_tab_syms
+ global_indirect_count) * sizeof (int));
strtab_index = 0;
/* Scan the symbol hash table, bucket by bucket. */
for (i = 0; i < TABSIZE; i++)
for (sp = symtab[i]; sp; sp = sp->link)
{
struct nlist nl;
nl.n_other = 0;
nl.n_desc = 0;
/* Compute a `struct nlist' for the symbol. */
if (sp->defined || sp->referenced)
{
/* common condition needs to be before undefined condition */
/* because unallocated commons are set undefined in */
/* digest_symbols */
if (sp->defined > 1) /* defined with known type */
{
/* If the target of an indirect symbol has been
defined and we are outputting an executable,
resolve the indirection; it's no longer needed */
if (!relocatable_output
&& ((sp->defined & ~N_EXT) == N_INDR)
&& (((symbol *) sp->value)->defined > 1))
{
symbol *newsp = (symbol *) sp->value;
nl.n_type = newsp->defined;
nl.n_value = newsp->value;
}
else
{
nl.n_type = sp->defined;
if (sp->defined != (N_INDR | N_EXT))
nl.n_value = sp->value;
else
nl.n_value = 0;
}
}
else if (sp->max_common_size) /* defined as common but not allocated. */
{
/* happens only with -r and not -d */
/* write out a common definition */
nl.n_type = N_UNDF | N_EXT;
nl.n_value = sp->max_common_size;
}
else if (!sp->defined) /* undefined -- legit only if -r */
{
nl.n_type = N_UNDF | N_EXT;
nl.n_value = 0;
}
else
fatal ("internal error: %s defined in mysterious way", sp->name);
/* Allocate string table space for the symbol name. */
nl.n_un.n_strx = assign_string_table_index (sp->name);
/* Output to the buffer and count it. */
*bufp++ = nl;
syms_written++;
if (nl.n_type == (N_INDR | N_EXT))
{
struct nlist xtra_ref;
xtra_ref.n_type = N_EXT | N_UNDF;
xtra_ref.n_un.n_strx =
assign_string_table_index (((symbol *) sp->value)->name);
xtra_ref.n_other = 0;
xtra_ref.n_desc = 0;
xtra_ref.n_value = 0;
*bufp++ = xtra_ref;
syms_written++;
}
}
}
/* Output the buffer full of `struct nlist's. */
fseek (outdesc, symbol_table_offset + symbol_table_len, 0);
mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
symbol_table_len += sizeof (struct nlist) * (bufp - buf);
if (syms_written != nsyms)
fatal ("internal error: wrong number of symbols written into output file", 0);
if (symbol_table_offset + symbol_table_len != string_table_offset)
fatal ("internal error: inconsistent symbol table length", 0);
/* Now the total string table size is known, so write it.
We are already positioned at the right place in the file. */
mywrite (&strtab_size, sizeof (int), 1, outdesc); /* we're at right place */
/* Write the strings for the global symbols. */
write_string_table ();
}
/* Write the local and debugger symbols of file ENTRY.
Increment *SYMS_WRITTEN_ADDR for each symbol that is written. */
/* Note that we do not combine identical names of local symbols.
dbx or gdb would be confused if we did that. */
void
write_file_syms (entry, syms_written_addr)
struct file_entry *entry;
int *syms_written_addr;
{
register struct nlist *p = entry->symbols;
register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
/* Buffer to accumulate all the syms before writing them.
It has one extra slot for the local symbol we generate here. */
struct nlist *buf
= (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
register struct nlist *bufp = buf;
/* Upper bound on number of syms to be written here. */
int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
/* Make tables that record, for each symbol, its name and its name's length.
The elements are filled in by `assign_string_table_index'. */
strtab_vector = (char **) alloca (max_syms * sizeof (char *));
strtab_lens = (int *) alloca (max_syms * sizeof (int));
strtab_index = 0;
/* Generate a local symbol for the start of this file's text. */
if (discard_locals != DISCARD_ALL)
{
struct nlist nl;
nl.n_type = N_TEXT;
nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
nl.n_value = entry->text_start_address;
nl.n_desc = 0;
nl.n_other = 0;
*bufp++ = nl;
(*syms_written_addr)++;
entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
}
/* Read the file's string table. */
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (file_open (entry), entry);
for (; p < end; p++)
{
register int type = p->n_type;
register int write = 0;
/* WRITE gets 1 for a non-global symbol that should be written. */
if (SET_ELEMENT_P (type)) /* This occurs even if global. These */
/* types of symbols are never written */
/* globally, though they are stored */
/* globally. */
write = relocatable_output;
else if (!(type & (N_STAB | N_EXT)))
/* ordinary local symbol */
write = ((discard_locals != DISCARD_ALL)
&& !(discard_locals == DISCARD_L &&
(p->n_un.n_strx + entry->strings)[0] == 'L')
&& type != N_WARNING);
else if (!(type & N_EXT))
/* debugger symbol */
write = (strip_symbols == STRIP_NONE);
if (write)
{
/* If this symbol has a name,
allocate space for it in the output string table. */
#if 0 /* Following no longer true */
/* Sigh. If this is a set element, it has been entered in */
/* the global symbol table *and* is being output as a */
/* local. This means that p->n_un.n_strx has been */
/* trashed. */
if (SET_ELEMENT_P (type))
p->n_un.n_strx =
assign_string_table_index (((symbol *) p->n_un.n_name)->name);
else
#endif
if (p->n_un.n_strx)
p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
+ entry->strings);
/* Output this symbol to the buffer and count it. */
*bufp++ = *p;
(*syms_written_addr)++;
}
}
/* All the symbols are now in BUF; write them. */
fseek (outdesc, symbol_table_offset + symbol_table_len, 0);
mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
symbol_table_len += sizeof (struct nlist) * (bufp - buf);
/* Write the string-table data for the symbols just written,
using the data in vectors `strtab_vector' and `strtab_lens'. */
write_string_table ();
entry->strings = 0; /* Since it will dissapear anyway. */
}
#endif /* not atari st */
/* Copy any GDB symbol segments from the input files to the output file.
The contents of the symbol segment is copied without change
except that we store some information into the beginning of it. */
void write_file_symseg ();
void
write_symsegs ()
{
each_file (write_file_symseg, 0);
}
void
write_file_symseg (entry)
struct file_entry *entry;
{
/* this appears to be completely wrong for ST */
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
char buffer[4096];
struct symbol_root root;
FILE *indesc;
int len;
if (entry->symseg_offset == 0)
return;
/* This entry has a symbol segment. Read the root of the segment. */
indesc = file_open (entry);
fseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
if (sizeof root != fread (&root, 1, sizeof root, indesc))
fatal_with_file ("premature end of file in symbol segment of ", entry);
/* Store some relocation info into the root. */
root.ldsymoff = entry->local_syms_offset;
root.textrel = entry->text_start_address;
root.datarel = entry->data_start_address - entry->header.a_text;
root.bssrel = entry->bss_start_address
- entry->header.a_text - entry->header.a_data;
root.databeg = entry->data_start_address - root.datarel;
root.bssbeg = entry->bss_start_address - root.bssrel;
/* Write the modified root into the output file. */
mywrite (&root, sizeof root, 1, outdesc);
/* Copy the rest of the symbol segment unchanged. */
if (entry->superfile)
{
/* Library member: number of bytes to copy is determined
from the member's total size. */
int total = entry->total_size - entry->symseg_offset - sizeof root;
while (total > 0)
{
len = fread (buffer, 1, min (sizeof buffer, total), indesc);
if (len != min (sizeof buffer, total))
fatal_with_file ("premature end of file in symbol segment of ", entry);
total -= len;
mywrite (buffer, len, 1, outdesc);
}
}
else
{
/* A separate file: copy until end of file. */
while (len = fread (buffer, 1, sizeof buffer, indesc))
{
mywrite (buffer, len, 1, outdesc);
if (len < sizeof buffer)
break;
}
}
file_close ();
#endif
}
/* Create the symbol table entries for `etext', `edata' and `end'. */
void
symtab_init ()
{
#ifndef nounderscore
edata_symbol = getsym ("_edata");
etext_symbol = getsym ("_etext");
end_symbol = getsym ("_end");
#else
edata_symbol = getsym ("edata");
etext_symbol = getsym ("etext");
end_symbol = getsym ("end");
#endif
edata_symbol->defined = N_DATA | N_EXT;
etext_symbol->defined = N_TEXT | N_EXT;
end_symbol->defined = N_BSS | N_EXT;
edata_symbol->referenced = 1;
etext_symbol->referenced = 1;
end_symbol->referenced = 1;
}
/* Compute the hash code for symbol name KEY. */
int
hash_string (key)
char *key;
{
register char *cp;
register int k;
cp = key;
k = 0;
while (*cp)
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
return k;
}
/* Get the symbol table entry for the global symbol named KEY.
Create one if there is none. */
symbol *
getsym (key)
char *key;
{
register int hashval;
register symbol *bp;
/* Determine the proper bucket. */
hashval = hash_string (key) % TABSIZE;
/* Search the bucket. */
for (bp = symtab[hashval]; bp; bp = bp->link)
if (! strcmp (key, bp->name))
return bp;
/* Nothing was found; create a new symbol table entry. */
bp = (symbol *) xmalloc (sizeof (symbol));
bp->refs = 0;
bp->name = (char *) xmalloc (strlen (key) + 1);
strcpy (bp->name, key);
bp->defined = 0;
bp->referenced = 0;
bp->trace = 0;
bp->value = 0;
bp->max_common_size = 0;
bp->warning = 0;
bp->undef_refs = 0;
bp->def_count = 0;
/* Add the entry to the bucket. */
bp->link = symtab[hashval];
symtab[hashval] = bp;
++num_hash_tab_syms;
return bp;
}
/* Like `getsym' but return 0 if the symbol is not already known. */
symbol *
getsym_soft (key)
char *key;
{
register int hashval;
register symbol *bp;
/* Determine which bucket. */
hashval = hash_string (key) % TABSIZE;
/* Search the bucket. */
for (bp = symtab[hashval]; bp; bp = bp->link)
if (! strcmp (key, bp->name))
return bp;
return 0;
}
/* Report a fatal error.
STRING is a printf format string and ARG is one arg for it. */
#if __STDC__
void
fatal (char *string, ...)
{
va_list ap;
va_start(ap, string);
fprintf (stderr, "ld: ");
vfprintf (stderr, string, ap);
fprintf (stderr, "\n");
va_end(ap);
exit (1);
}
#else
void
fatal (va_alist)
va_dcl
{
va_list ap;
char *string;
va_start(ap);
string = va_arg(ap, char *);
fprintf (stderr, "ld: ");
vfprintf (stderr, string, ap);
fprintf (stderr, "\n");
va_end(ap);
exit (1);
}
#endif
/* Report a fatal error. The error message is STRING
followed by the filename of ENTRY. */
void
fatal_with_file (string, entry)
char *string;
struct file_entry *entry;
{
fprintf (stderr, "ld: ");
fprintf (stderr, string);
print_file_name (entry, stderr);
fprintf (stderr, "\n");
exit (1);
}
/* Report a fatal error using the message for the last failed system call,
followed by the string NAME. */
void
perror_name (name)
char *name;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat ("", sys_errlist[errno], " for %s");
else
s = "cannot open %s";
fatal (s, name);
}
/* Report a fatal error using the message for the last failed system call,
followed by the name of file ENTRY. */
void
perror_file (entry)
struct file_entry *entry;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat ("", sys_errlist[errno], " for ");
else
s = "cannot open ";
fatal_with_file (s, entry);
}
/* Report a nonfatal error.
STRING is a format for printf, and ARG1 ... ARG3 are args for it. */
void
error (string, arg1, arg2, arg3)
char *string, *arg1, *arg2, *arg3;
{
fprintf (stderr, "%s: ", progname);
fprintf (stderr, string, arg1, arg2, arg3);
fprintf (stderr, "\n");
}
/* Output COUNT*ELTSIZE bytes of data at BUF
to the descriptor DESC. */
void
mywrite (buf, count, eltsize, desc)
void *buf;
int count;
int eltsize;
FILE *desc;
{
if((eltsize != 0) && (count != 0))
{
if(count != fwrite(buf, eltsize, count, desc))
perror_name(output_filename);
}
}
/* Output PADDING zero-bytes to descriptor OUTDESC.
PADDING may be negative; in that case, do nothing. */
void
padfile (padding, outdesc)
int padding;
FILE *outdesc;
{
/* not round here, you don't... */
#if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
register char *buf;
if (padding <= 0)
return;
buf = (char *) alloca (padding);
bzero (buf, padding);
mywrite (buf, padding, 1, outdesc);
#endif
}
/* Return a newly-allocated string
whose contents concatenate the strings S1, S2, S3. */
char *
concat (s1, s2, s3)
char *s1, *s2, *s3;
{
register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
strcpy (result + len1 + len2, s3);
result[len1 + len2 + len3] = 0;
return result;
}
/* Parse the string ARG using scanf format FORMAT, and return the result.
If it does not parse, report fatal error
generating the error message using format string ERROR and ARG as arg. */
int
parse (arg, format, error)
char *arg, *format, *error;
{
int x;
if (1 != sscanf (arg, format, &x))
fatal (error, arg);
return x;
}
/* Like malloc but get fatal error if memory is exhausted. */
void *
xmalloc (size)
int size;
{
register void *result = malloc (size);
if (!result)
fatal ("virtual memory exhausted", 0);
return result;
}
/* Like realloc but get fatal error if memory is exhausted. */
void *
xrealloc (ptr, size)
void *ptr;
int size;
{
register void *result = realloc (ptr, size);
if (!result)
fatal ("virtual memory exhausted", 0);
return result;
}
#ifdef USG
void
bzero (p, n)
char *p;
{
memset (p, 0, n);
}
void
bcopy (from, to, n)
char *from, *to;
{
memcpy (to, from, n);
}
#ifndef pyr
getpagesize ()
{
return (4096);
}
#endif
#endif
#if (defined(MINIX) || defined(atariminix))
/* Write the symbol table and string table into minix_out_file. */
#ifdef SZ_HEAD
#undef SZ_HEAD /* conflict with def in out.h */
#endif
#ifdef SF_HEAD
#undef SF_HEAD /* conflict with def in out.h */
#endif
#include <out.h> /* format of output of /usr/lib/ld -- minixSt update#9 */
FILE *minix_outname_file, *minix_strings_file;
unsigned short minix_nname;
long minix_nchar;
unsigned short minix_swap_short();
long minix_swap_long();
write_minix_sym(p, str, gflag, fflag)
register struct nlist * p;
char * str;
int gflag, fflag;
{
struct outname sym;
int i;
/* fprintf(stderr, "sym %s\n", str); */
sym.on_foff = minix_nchar; /* we will patch in offset later */
for(i = 0; str[i]; i++)
{
putc(str[i], minix_strings_file);
minix_nchar++;
}
putc(0, minix_strings_file);
minix_nchar++;
switch (p->n_type & N_TYPE)
{
case N_UNDF: sym.on_type = S_TYP & S_UND; break;
case N_ABS: sym.on_type = S_TYP & S_ABS; break;
case N_TEXT: sym.on_type = S_TYP & S_MIN; break;
case N_DATA: sym.on_type = S_TYP & (S_MIN + 2); break;
case N_BSS: sym.on_type = S_TYP & (S_MIN + 3); break;
default: sym.on_type = 0;
/* figure out rest of this later */
}
if(gflag == 1)
sym.on_type |= S_EXT;
if(fflag == 1)
sym.on_type |= S_ETC & S_FIL;
sym.on_type = minix_swap_short(&sym.on_type);
sym.on_desc = 0;
sym.on_valu = p->n_value;
sym.on_valu = minix_swap_long(&sym.on_valu);
#ifndef WORD_ALIGNED
fwrite(&sym, sizeof(sym), 1, minix_outname_file);
#else
fwrite(&sym.on_foff, sizeof sym.on_foff, 1, minix_outname_file);
fwrite(&sym.on_type, sizeof sym.on_type, 1, minix_outname_file);
fwrite(&sym.on_desc, sizeof sym.on_desc, 1, minix_outname_file);
fwrite(&sym.on_valu, sizeof sym.on_valu, 1, minix_outname_file);
#endif
minix_nname++;
}
void write_minix_syms(entry, syms_written_addr)
struct file_entry * entry;
int * syms_written_addr;
{
register struct nlist *p = entry->symbols;
register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
/* Read the file's string table. */
entry->strings = (char *) alloca (entry->string_size);
read_entry_strings (file_open (entry), entry);
/* Generate a local symbol for the start of this file's text. */
{
struct nlist nl;
nl.n_type = N_TEXT;
/* 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; */
write_minix_sym(&nl, entry->local_sym_name, 0, 1);
(*syms_written_addr)++;
/* necessary? */
entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
}
for (; p < end; p++)
{
register int type = p->n_type;
register int write = 0;
/* WRITE gets 1 for a non-global symbol that should be written. */
if (!(type & (N_STAB | N_EXT)))
/* ordinary local symbol */
write = !((p->n_un.n_strx + entry->strings)[0] == 'L');
else if (!(type & N_EXT))
/* debugger symbol */
write = 0;
if (write)
{
/* If this symbol has a name, write it */
if (p->n_un.n_strx)
{
write_minix_sym(p, p->n_un.n_strx + entry->strings, 0, 0);
(*syms_written_addr)++;
}
}
}
entry->strings = NULL;
}
void do_write_minix_out ()
{
int n_syms_written = 0;
register symbol *sp;
int i;
char minix_outname_name[10], minix_strings_name[10];
struct outhead oh;
struct outname sym;
long string_off;
/* create two tmp files, one for outname structs one for strings, */
/* later these two are concatenated into minix_out_file and unlinked */
strcpy(minix_outname_name, "ldnXXXXXX");
strcpy(minix_strings_name, "ldsXXXXXX");
mktemp(minix_outname_name);
mktemp(minix_strings_name);
if(((minix_outname_file = fopen(minix_outname_name, "w"))
== (FILE *)NULL) ||
((minix_strings_file = fopen(minix_strings_name, "w"))
== (FILE *)NULL))
fatal("Cannot creat tmp files", (char *)NULL);
/* make up a out.h format header */
oh.oh_magic = O_MAGIC;
oh.oh_magic = minix_swap_short(&oh.oh_magic);
oh.oh_stamp = O_STAMP;
oh.oh_stamp = minix_swap_short(&oh.oh_stamp);
oh.oh_flags = 0;
oh.oh_nsect = 0;
oh.oh_nrelo = 0;
oh.oh_nname = 0; /* will be patched */
oh.oh_nemit = 0;
oh.oh_nchar = 0; /* will be patched */
#ifndef WORD_ALIGNED
fwrite(&oh, sizeof(oh), 1, minix_outname_file);
#else
fwrite(&oh.oh_magic, sizeof oh.oh_magic, 1, minix_outname_file);
fwrite(&oh.oh_stamp, sizeof oh.oh_stamp, 1, minix_outname_file);
fwrite(&oh.oh_flags, sizeof oh.oh_flags, 1, minix_outname_file);
fwrite(&oh.oh_nsect, sizeof oh.oh_nsect, 1, minix_outname_file);
fwrite(&oh.oh_nrelo, sizeof oh.oh_nrelo, 1, minix_outname_file);
fwrite(&oh.oh_nname, sizeof oh.oh_nname, 1, minix_outname_file);
fwrite(&oh.oh_nemit, sizeof oh.oh_nemit, 1, minix_outname_file);
fwrite(&oh.oh_nchar, sizeof oh.oh_nchar, 1, minix_outname_file);
#endif
minix_nname = 0;
minix_nchar = 0L;
each_file(write_minix_syms, &n_syms_written);
/* Now write out the global symbols. */
/* Scan the symbol hash table, bucket by bucket. */
for (i = 0; i < TABSIZE; i++)
for (sp = symtab[i]; sp; sp = sp->link)
{
struct nlist nl;
nl.n_other = 0;
nl.n_desc = 0;
/* Compute a `struct nlist' for the symbol. */
if (sp->defined || sp->referenced)
{
if (!sp->defined) /* undefined -- legit only if -r */
{
nl.n_type = N_UNDF | N_EXT;
nl.n_value = 0;
}
else if (sp->defined > 1) /* defined with known type */
{
nl.n_type = sp->defined;
nl.n_value = sp->value;
}
else if (sp->max_common_size) /* defined as common but not allocated. */
{
/* happens only with -r and not -d */
/* write out a common definition */
nl.n_type = N_UNDF | N_EXT;
nl.n_value = sp->max_common_size;
}
else
fatal ("internal error: %s defined in mysterious way", sp->name);
/* write and count it. */
write_minix_sym(&nl, sp->name, 1, 0);
n_syms_written++;
}
}
#if 0
if (n_syms_written != nsyms)
fprintf(stderr, "Bogon alert! wrote %d syms, expected to write %d\n",
n_syms_written, nsyms);
#endif
/* close the temp files, open minix_out_file, and concat */
fclose(minix_strings_file);
#ifndef WORD_ALIGNED
string_off = minix_nname * sizeof(sym) + sizeof(oh);
#else
string_off = minix_nname * 12 + 20;
#endif
/* patch in oh_nname and oh_nchar */
fseek(minix_outname_file, 10L, 0);
minix_nname = minix_swap_short(&minix_nname);
fwrite(&minix_nname, sizeof(ushort), 1, minix_outname_file);
fseek(minix_outname_file, 16L, 0);
minix_nchar = minix_swap_long(&minix_nchar);
fwrite(&minix_nchar, sizeof(long), 1, minix_outname_file);
fclose(minix_outname_file);
if(((minix_outname_file = fopen(minix_outname_name, "r"))
== (FILE *)NULL) ||
((minix_strings_file = fopen(minix_strings_name, "r"))
== (FILE *)NULL))
fatal("Cannot open tmp files for read", (char *)NULL);
if((minix_out_filep = fopen(minix_out_filename, "w")) == (FILE *)NULL)
fatal("Cannot create %s", minix_out_filename);
#ifndef WORD_ALIGNED
fread(&oh, sizeof(oh), 1, minix_outname_file);
fwrite(&oh, sizeof(oh), 1, minix_out_filep);
while(fread(&sym, sizeof(sym), 1, minix_outname_file) == 1)
{
sym.on_foff += string_off;
sym.on_foff = minix_swap_long(&sym.on_foff);
fwrite(&sym, sizeof(sym), 1, minix_out_filep);
}
#else
fread(&oh, 20, 1, minix_outname_file);
fwrite(&oh, 20, 1, minix_out_filep);
while(fread(&sym.on_foff, sizeof sym.on_foff, 1, minix_outname_file) == 1)
{
fread(&sym.on_type, sizeof sym.on_type, 1, minix_outname_file);
fread(&sym.on_desc, sizeof sym.on_desc, 1, minix_outname_file);
fread(&sym.on_valu, sizeof sym.on_valu, 1, minix_outname_file);
sym.on_foff += string_off;
sym.on_foff = minix_swap_long(&sym.on_foff);
fwrite(&sym.on_foff, sizeof sym.on_foff, 1, minix_out_filep);
fwrite(&sym.on_type, sizeof sym.on_type, 1, minix_out_filep);
fwrite(&sym.on_desc, sizeof sym.on_desc, 1, minix_out_filep);
fwrite(&sym.on_valu, sizeof sym.on_valu, 1, minix_out_filep);
}
#endif
#if 0
fprintf(stderr,"Curr %ld Str_off %ld\n", ftell(minix_out_filep), string_off);
#endif
while((i = getc(minix_strings_file)) != EOF)
putc(i, minix_out_filep);
fclose(minix_strings_file);
fclose(minix_outname_file);
fclose(minix_out_filep);
unlink(minix_strings_name);
unlink(minix_outname_name);
}
unsigned short minix_swap_short(s)
unsigned char s[];
{
unsigned short i = (s[1] << 8) | s[0];
return i;
}
long minix_swap_long(s)
unsigned char s[];
{
unsigned long i = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
return i;
}
#endif