Annotation of binutils/ld.c, revision 1.1.1.2

1.1       root        1: /* Linker `ld' for GNU
                      2:    Copyright (C) 1988 Free Software Foundation, Inc.
                      3: 
                      4:                       NO WARRANTY
                      5: 
                      6:   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
                      7: NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
                      8: WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
                      9: RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
                     10: WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
                     11: BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
                     12: FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
                     13: AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
                     14: DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
                     15: CORRECTION.
                     16: 
                     17:  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
                     18: STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
                     19: WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
                     20: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
                     21: OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
                     22: USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
                     23: DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
                     24: A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
                     25: PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
                     26: DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
                     27: 
                     28:                GENERAL PUBLIC LICENSE TO COPY
                     29: 
                     30:   1. You may copy and distribute verbatim copies of this source file
                     31: as you receive it, in any medium, provided that you conspicuously
                     32: and appropriately publish on each copy a valid copyright notice
                     33: "Copyright (C) 1988 Free Software Foundation, Inc.", and include
                     34: following the copyright notice a verbatim copy of the above disclaimer
                     35: of warranty and of this License.
                     36: 
                     37:   2. You may modify your copy or copies of this source file or
                     38: any portion of it, and copy and distribute such modifications under
                     39: the terms of Paragraph 1 above, provided that you also do the following:
                     40: 
                     41:     a) cause the modified files to carry prominent notices stating
                     42:     that you changed the files and the date of any change; and
                     43: 
                     44:     b) cause the whole of any work that you distribute or publish,
                     45:     that in whole or in part contains or is a derivative of this
                     46:     program or any part thereof, to be licensed at no charge to all
                     47:     third parties on terms identical to those contained in this
                     48:     License Agreement (except that you may choose to grant more extensive
                     49:     warranty protection to some or all third parties, at your option).
                     50: 
                     51:     c) You may charge a distribution fee for the physical act of
                     52:     transferring a copy, and you may at your option offer warranty
                     53:     protection in exchange for a fee.
                     54: 
                     55: Mere aggregation of another unrelated program with this program (or its
                     56: derivative) on a volume of a storage or distribution medium does not bring
                     57: the other program under the scope of these terms.
                     58: 
                     59:   3. You may copy and distribute this program (or a portion or derivative
                     60: of it, under Paragraph 2) in object code or executable form under the terms
                     61: of Paragraphs 1 and 2 above provided that you also do one of the following:
                     62: 
                     63:     a) accompany it with the complete corresponding machine-readable
                     64:     source code, which must be distributed under the terms of
                     65:     Paragraphs 1 and 2 above; or,
                     66: 
                     67:     b) accompany it with a written offer, valid for at least three
                     68:     years, to give any third party free (except for a nominal
                     69:     shipping charge) a complete machine-readable copy of the
                     70:     corresponding source code, to be distributed under the terms of
                     71:     Paragraphs 1 and 2 above; or,
                     72: 
                     73:     c) accompany it with the information you received as to where the
                     74:     corresponding source code may be obtained.  (This alternative is
                     75:     allowed only for noncommercial distribution and only if you
                     76:     received the program in object code or executable form alone.)
                     77: 
                     78: For an executable file, complete source code means all the source code for
                     79: all modules it contains; but, as a special exception, it need not include
                     80: source code for modules which are standard libraries that accompany the
                     81: operating system on which the executable file runs.
                     82: 
                     83:   4. You may not copy, sublicense, distribute or transfer this program
                     84: except as expressly provided under this License Agreement.  Any attempt
                     85: otherwise to copy, sublicense, distribute or transfer this program is void and
                     86: your rights to use the program under this License agreement shall be
                     87: automatically terminated.  However, parties who have received computer
                     88: software programs from you with this License Agreement will not have
                     89: their licenses terminated so long as such parties remain in full compliance.
                     90: 
                     91:   5. If you wish to incorporate parts of this program into other free
                     92: programs whose distribution conditions are different, write to the Free
                     93: Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
                     94: worked out a simple rule that can be stated here, but we will often permit
                     95: this.  We will be guided by the two goals of preserving the free status of
                     96: all derivatives of our free software and of promoting the sharing and reuse of
                     97: software.
                     98: 
                     99:  In other words, you are welcome to use, share and improve this program.
                    100:  You are forbidden to forbid anyone else to use, share and improve
                    101:  what you give them.   Help stamp out software-hoarding!  */
                    102: 
1.1.1.2 ! root      103: /* Written by Richard Stallman with some help from Eric Albert.
        !           104:    Set and indirect features added by Randy Smith.  */
1.1       root      105: 
                    106: #include <a.out.h>
1.1.1.2 ! root      107: #include <stab.h>
1.1       root      108: #include <ar.h>
                    109: #include <stdio.h>
                    110: #include <sys/types.h>
                    111: #include <strings.h>
                    112: #include <sys/stat.h>
                    113: #include "symseg.h"
                    114: #include <sys/file.h>
                    115: 
                    116: #define min(a,b) ((a) < (b) ? (a) : (b))
                    117: 
1.1.1.2 ! root      118: /* Macro to control the number of undefined references printed */
        !           119: #define MAX_UREFS_PRINTED      10
        !           120: 
1.1       root      121: /* Size of a page; obtained from the operating system.  */
                    122: 
                    123: int page_size;
                    124: 
                    125: /* System dependencies */
                    126: 
                    127: /* Define this if names etext, edata and end should not start with `_'.  */
                    128: /* #define nounderscore 1 */
                    129: 
                    130: /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
                    131:    whose native format is different.  */
                    132: /* #define NON_NATIVE */
                    133: 
                    134: /* Define this to specify the default executable format.  */
                    135: 
                    136: #ifdef hpux
                    137: #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
                    138: #endif
                    139: 
                    140: #ifndef DEFAULT_MAGIC
                    141: #define DEFAULT_MAGIC ZMAGIC
                    142: #endif
                    143: 
                    144: /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
                    145: 
                    146: #ifndef N_TXTADDR
                    147: #ifdef vax
                    148: #define N_TXTADDR(X) 0
                    149: #endif
                    150: #endif
                    151: 
                    152: #ifndef N_DATADDR
                    153: #ifdef vax
                    154: #define N_DATADDR(x) \
                    155:        (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
                    156:        : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
                    157: #endif
                    158: #endif
                    159: 
                    160: #ifdef hpux
                    161: #define getpagesize() EXEC_PAGESIZE
                    162: #endif
                    163: 
                    164: /* Define how to initialize system-dependent header fields.  */
                    165: #ifdef sun
                    166: #define INITIALIZE_HEADER outheader.a_machtype = M_68020
                    167: #endif
                    168: #ifdef hpux
                    169: #define INITIALIZE_HEADER outheader.a_machtype = HP9000S200_ID
                    170: #endif
                    171: 
1.1.1.2 ! root      172: /* Special global symbol types understood by GNU LD.  */
        !           173: 
        !           174: /* The following type indicates the definition of a symbol as being
        !           175:    an indirect reference to another symbol.  The other symbol
        !           176:    appears as an undefined reference, immediately following this symbol.
        !           177: 
        !           178:    Indirection is asymmetrical.  The other symbol's value will be used
        !           179:    to satisfy requests for the indirect symbol, but not vice versa.
        !           180:    If the other symbol does not have a definition, libraries will
        !           181:    be searched to find a definition.  */
        !           182: #ifndef N_INDR
        !           183: #define N_INDR 0xa
        !           184: #endif
        !           185: 
        !           186: /* The following symbols refer to set elements.
        !           187:    All the N_SET[ATDB] symbols with the same name form one set.
        !           188:    Space is allocated for the set in the text section, and each set
        !           189:    element's value is stored into one word of the space.
        !           190:    The first word of the space is the length of the set (number of elements).
        !           191: 
        !           192:    The address of the set is made into an N_SETV symbol
        !           193:    whose name is the same as the name of the set.
        !           194:    This symbol acts like a N_TEXT global symbol
        !           195:    in that it can satisfy undefined external references.  */
        !           196: 
        !           197: #ifndef N_SETA
        !           198: #define        N_SETA  0x14            /* Absolute set element symbol */
        !           199: #endif                         /* This is input to LD, in a .o file.  */
        !           200: 
        !           201: #ifndef N_SETT
        !           202: #define        N_SETT  0x16            /* Text set element symbol */
        !           203: #endif                         /* This is input to LD, in a .o file.  */
        !           204: 
        !           205: #ifndef N_SETD
        !           206: #define        N_SETD  0x18            /* Data set element symbol */
        !           207: #endif                         /* This is input to LD, in a .o file.  */
        !           208: 
        !           209: #ifndef N_SETB
        !           210: #define        N_SETB  0x1A            /* Bss set element symbol */
        !           211: #endif                         /* This is input to LD, in a .o file.  */
        !           212: 
        !           213: /* Macros dealing with the set element symbols defined in a.out.h */
        !           214: #define        SET_ELEMENT_P(x)        ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
        !           215: #define TYPE_OF_SET_ELEMENT(x) ((x)-N_SETA+N_ABS)
        !           216: 
        !           217: #ifndef N_SETV
        !           218: #define N_SETV 0x1C            /* Pointer to set vector in text area.  */
        !           219: #endif                         /* This is output from LD.  */
        !           220: 
1.1       root      221: /* Symbol table */
                    222: 
                    223: /* Global symbol data is recorded in these structures,
                    224:    one for each global symbol.
                    225:    They are found via hashing in 'symtab', which points to a vector of buckets.
                    226:    Each bucket is a chain of these structures through the link field.  */
                    227: 
                    228: typedef
                    229:   struct glosym
                    230:     {
                    231:       /* Pointer to next symbol in this symbol's hash bucket.  */
                    232:       struct glosym *link;
                    233:       /* Name of this symbol.  */
                    234:       char *name;
                    235:       /* Value of this symbol as a global symbol.  */
                    236:       long value;
                    237:       /* Chain of external 'nlist's in files for this symbol, both defs and refs.  */
                    238:       struct nlist *refs;
                    239:       /* Nonzero means definitions of this symbol as common have been seen,
                    240:         and the value here is the largest size specified by any of them.  */
                    241:       int max_common_size;
                    242:       /* For relocatable_output, records the index of this global sym in the
                    243:         symbol table to be written, with the first global sym given index 0.  */
                    244:       int def_count;
                    245:       /* Nonzero means a definition of this global symbol is known to exist.
                    246:         Library members should not be loaded on its account.  */
                    247:       char defined;
                    248:       /* Nonzero means a reference to this global symbol has been seen
                    249:         in a file that is surely being loaded.
1.1.1.2 ! root      250:         A value higher than 1 is the n_type code for the symbol's
        !           251:         definition.  */
1.1       root      252:       char referenced;
1.1.1.2 ! root      253:       /* A count of the number of undefined references printed for a
        !           254:         specific symbol.  See list_unresolved_references for more
        !           255:         info */
        !           256:       unsigned char undef_refs;
1.1       root      257:       /* Nonzero means print a message at all refs or defs of this symbol */
                    258:       char trace;
                    259:     }
                    260:   symbol;
                    261: 
                    262: /* Number of buckets in symbol hash table */
                    263: #define        TABSIZE 1009
                    264: 
                    265: /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
                    266: symbol *symtab[TABSIZE];
                    267: 
                    268: /* Number of symbols in symbol hash table. */
                    269: int num_hash_tab_syms = 0;
                    270: 
                    271: /* Count the number of nlist entries that are for local symbols.
                    272:    This count and the three following counts
                    273:    are incremented as as symbols are entered in the symbol table.  */
                    274: int local_sym_count;
                    275: 
                    276: /* Count number of nlist entries that are for local symbols
                    277:    whose names don't start with L. */
                    278: int non_L_local_sym_count;
                    279: 
                    280: /* Count the number of nlist entries for debugger info.  */
                    281: int debugger_sym_count;
                    282: 
                    283: /* Count the number of global symbols referenced and not defined.  */
                    284: int undefined_global_sym_count;
                    285: 
                    286: /* Count the number of defined global symbols.
                    287:    Each symbol is counted only once
                    288:    regardless of how many different nlist entries refer to it,
                    289:    since the output file will need only one nlist entry for it.
                    290:    This count is computed by `digest_symbols';
                    291:    it is undefined while symbols are being loaded. */
                    292: int defined_global_sym_count;
                    293: 
1.1.1.2 ! root      294: /* Count the number of set element type symbols and the number of
        !           295:    separate vectors which these symbols will fit into.  See the
        !           296:    GNU a.out.h for more info.
        !           297:    This count is computed by 'enter_file_symbols' */
        !           298: int set_symbol_count;
        !           299: int set_vector_count;
        !           300: 
        !           301: /* Count the number of definitions done indirectly (ie. done relative
        !           302:    to the value of some other symbol. */
        !           303: int global_indirect_count;
        !           304: 
1.1       root      305: /* Total number of symbols to be written in the output file.
                    306:    Computed by digest_symbols from the variables above.  */
                    307: int nsyms;
                    308: 
                    309: 
                    310: /* Nonzero means ptr to symbol entry for symbol to use as start addr.
                    311:    -e sets this.  */
                    312: symbol *entry_symbol;
                    313: 
                    314: symbol *edata_symbol;   /* the symbol _edata */
                    315: symbol *etext_symbol;   /* the symbol _etext */
                    316: symbol *end_symbol;    /* the symbol _end */
                    317: 
                    318: /* Each input file, and each library member ("subfile") being loaded,
                    319:    has a `file_entry' structure for it.
                    320: 
                    321:    For files specified by command args, these are contained in the vector
                    322:    which `file_table' points to.
                    323: 
                    324:    For library members, they are dynamically allocated,
                    325:    and chained through the `chain' field.
                    326:    The chain is found in the `subfiles' field of the `file_entry'.
                    327:    The `file_entry' objects for the members have `superfile' fields pointing
                    328:    to the one for the library.  */
                    329: 
                    330: struct file_entry {
                    331:   /* Name of this file.  */
                    332:   char *filename;
                    333:   /* Name to use for the symbol giving address of text start */
                    334:   /* Usually the same as filename, but for a file spec'd with -l
                    335:      this is the -l switch itself rather than the filename.  */
                    336:   char *local_sym_name;
                    337: 
                    338:   /* Describe the layout of the contents of the file */
                    339: 
                    340:   /* The file's a.out header.  */
                    341:   struct exec header;
                    342:   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
                    343:   int symseg_offset;
                    344: 
                    345:   /* Describe data from the file loaded into core */
                    346: 
                    347:   /* Symbol table of the file.  */
                    348:   struct nlist *symbols;
                    349:   /* Size in bytes of string table.  */
                    350:   int string_size;
                    351:   /* Pointer to the string table.
                    352:      The string table is not kept in core all the time,
                    353:      but when it is in core, its address is here.  */
                    354:   char *strings;
                    355: 
1.1.1.2 ! root      356:   /* Next two used only if `relocatable_output' or if needed for */
        !           357:   /* output of undefined reference line numbers. */
1.1       root      358: 
                    359:   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
                    360:   struct relocation_info *textrel;
                    361:   /* Data reloc info saved by `write_data' for `copdatrel'.  */
                    362:   struct relocation_info *datarel;
                    363: 
                    364:   /* Relation of this file's segments to the output file */
                    365: 
                    366:   /* Start of this file's text seg in the output file core image.  */
                    367:   int text_start_address;
                    368:   /* Start of this file's data seg in the output file core image.  */
                    369:   int data_start_address;
                    370:   /* Start of this file's bss seg in the output file core image.  */
                    371:   int bss_start_address;
                    372:   /* Offset in bytes in the output file symbol table
                    373:      of the first local symbol for this file.  Set by `write_file_symbols'.  */
                    374:   int local_syms_offset;                                  
                    375: 
                    376:   /* For library members only */
                    377: 
                    378:   /* For a library, points to chain of entries for the library members.  */
                    379:   struct file_entry *subfiles;
                    380:   /* For a library member, offset of the member within the archive.
                    381:      Zero for files that are not library members.  */
                    382:   int starting_offset;
                    383:   /* Size of contents of this file, if library member.  */
                    384:   int total_size;
                    385:   /* For library member, points to the library's own entry.  */
                    386:   struct file_entry *superfile;
                    387:   /* For library member, points to next entry for next member.  */
                    388:   struct file_entry *chain;
                    389: 
                    390:   /* 1 if file is a library. */
                    391:   char library_flag;
                    392: 
                    393:   /* 1 if file's header has been read into this structure.  */
                    394:   char header_read_flag;
                    395: 
                    396:   /* 1 means search a set of directories for this file.  */
                    397:   char search_dirs_flag;
                    398: 
                    399:   /* 1 means this is base file of incremental load.
                    400:      Do not load this file's text or data.
                    401:      Also default text_start to after this file's bss. */
                    402:   char just_syms_flag;
                    403: };
                    404: 
                    405: /* Vector of entries for input files specified by arguments.
                    406:    These are all the input files except for members of specified libraries.  */
                    407: struct file_entry *file_table;
                    408: 
                    409: /* Length of that vector.  */
                    410: int number_of_files;
                    411: 
                    412: /* When loading the text and data, we can avoid doing a close
                    413:    and another open between members of the same library.
                    414: 
                    415:    These two variables remember the file that is currently open.
                    416:    Both are zero if no file is open.
                    417: 
                    418:    See `each_file' and `file_close'.  */
                    419: 
                    420: struct file_entry *input_file;
                    421: int input_desc;
                    422: 
                    423: /* The name of the file to write; "a.out" by default.  */
                    424: 
                    425: char *output_filename;
                    426: 
                    427: /* Descriptor for writing that file with `mywrite'.  */
                    428: 
                    429: int outdesc;
                    430: 
                    431: /* Header for that file (filled in by `write_header').  */
                    432: 
                    433: struct exec outheader;
                    434: 
                    435: /* The following are computed by `digest_symbols'.  */
                    436: 
                    437: int text_size;         /* total size of text of all input files.  */
                    438: int data_size;         /* total size of data of all input files.  */
                    439: int bss_size;          /* total size of bss of all input files.  */
                    440: int text_reloc_size;   /* total size of text relocation of all input files.  */
1.1.1.2 ! root      441: int data_reloc_size;   /* total size of data relocation of all input */
        !           442:                        /* files.  */
        !           443: 
        !           444: /* Specifications of start and length of the area reserved at the end
        !           445:    of the text segment for the set vectors.  Computed in 'digest_symbols' */
        !           446: int set_sect_start;
        !           447: int set_sect_size;
        !           448: 
        !           449: /* Pointer for in core storage for the above vectors, before they are
        !           450:    written. */
        !           451: unsigned long *set_vectors;
1.1       root      452: 
                    453: /* Amount of cleared space to leave between the text and data segments.  */
                    454: 
                    455: int text_pad;
                    456: 
                    457: /* Amount of bss segment to include as part of the data segment.  */
                    458: 
                    459: int data_pad;
                    460: 
                    461: /* Format of __.SYMDEF:
                    462:    First, a longword containing the size of the 'symdef' data that follows.
                    463:    Second, zero or more 'symdef' structures.
                    464:    Third, a word containing the length of symbol name strings.
                    465:    Fourth, zero or more symbol name strings (each followed by a zero).  */
                    466: 
                    467: struct symdef {
                    468:   int symbol_name_string_index;
                    469:   int library_member_offset;
                    470: };
                    471: 
                    472: /* Record most of the command options.  */
                    473: 
                    474: /* Address we assume the text section will be loaded at.
                    475:    We relocate symbols and text and data for this, but we do not
                    476:    write any padding in the output file for it.  */
                    477: int text_start;
                    478: 
                    479: /* Offset of default entry-pc within the text section.  */
                    480: int entry_offset;
                    481: 
                    482: /* Address we decide the data section will be loaded at.  */
                    483: int data_start;
                    484: 
                    485: /* `text-start' address is normally this much plus a page boundary.
                    486:    This is not a user option; it is fixed for each system.  */
                    487: int text_start_alignment;
                    488: 
                    489: /* Nonzero if -T was specified in the command line.
                    490:    This prevents text_start from being set later to default values.  */
                    491: int T_flag_specified;
                    492: 
                    493: /* Nonzero if -Tdata was specified in the command line.
                    494:    This prevents data_start from being set later to default values.  */
                    495: int Tdata_flag_specified;
                    496: 
                    497: /* Size to pad data section up to.
                    498:    We simply increase the size of the data section, padding with zeros,
                    499:    and reduce the size of the bss section to match.  */
                    500: int specified_data_size;
                    501: 
                    502: /* Magic number to use for the output file, set by switch.  */
                    503: int magic;
                    504: 
                    505: /* Nonzero means print names of input files as processed.  */
                    506: int trace_files;
                    507: 
                    508: /* Which symbols should be stripped (omitted from the output):
                    509:    none, all, or debugger symbols.  */
                    510: enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
                    511: 
                    512: /* Which local symbols should be omitted:
                    513:    none, all, or those starting with L.
                    514:    This is irrelevant if STRIP_NONE.  */
                    515: enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
                    516: 
                    517: /* 1 => write load map.  */
                    518: int write_map;
                    519: 
                    520: /* 1 => write relocation into output file so can re-input it later.  */
                    521: int relocatable_output;
                    522: 
                    523: /* 1 => assign space to common symbols even if `relocatable_output'.  */
                    524: int force_common_definition;
                    525: 
                    526: /* Standard directories to search for files specified by -l.  */
                    527: char *standard_search_dirs[] =
                    528: #ifdef NON_NATIVE
                    529:   {"/usr/local/lib/gnu"};
                    530: #else
                    531:   {"/lib", "/usr/lib", "/usr/local/lib"};
                    532: #endif
                    533: 
                    534: /* Actual vector of directories to search;
                    535:    this contains those specified with -L plus the standard ones.  */
                    536: char **search_dirs;
                    537: 
                    538: /* Length of the vector `search_dirs'.  */
                    539: int n_search_dirs;
                    540: 
1.1.1.2 ! root      541: /* Non zero means to create the output executable. */
1.1       root      542: /* Cleared by nonfatal errors.  */
                    543: int make_executable;
                    544: 
1.1.1.2 ! root      545: /* Force the executable to be output, even if there are non-fatal
        !           546:    errors */
        !           547: int force_executable;
        !           548: 
        !           549: void bcopy (), bzero ();
        !           550: int malloc (), realloc (), alloca ();
        !           551: int free ();
        !           552: 
        !           553: int xmalloc ();
        !           554: int xrealloc ();
        !           555: void fatal ();
        !           556: void fatal_with_file ();
        !           557: void perror_name ();
        !           558: void perror_file ();
        !           559: void error ();
        !           560: 
1.1       root      561: void digest_symbols ();
                    562: void print_symbols ();
                    563: void load_symbols ();
                    564: void decode_command ();
                    565: void list_undefined_symbols ();
1.1.1.2 ! root      566: void list_unresolved_references ();
1.1       root      567: void write_output ();
                    568: void write_header ();
                    569: void write_text ();
1.1.1.2 ! root      570: void read_file_relocation ();
1.1       root      571: void write_data ();
                    572: void write_rel ();
                    573: void write_syms ();
1.1.1.2 ! root      574: void write_symsegs ();
        !           575: void mywrite ();
        !           576: void symtab_init ();
1.1       root      577: void padfile ();
                    578: char *concat ();
1.1.1.2 ! root      579: char *get_file_name ();
1.1       root      580: symbol *getsym (), *getsym_soft ();
                    581: 
1.1.1.2 ! root      582: int
1.1       root      583: main (argc, argv)
                    584:      char **argv;
                    585:      int argc;
                    586: {
                    587:   page_size = getpagesize ();
                    588: 
                    589:   /* Clear the cumulative info on the output file.  */
                    590: 
                    591:   text_size = 0;
                    592:   data_size = 0;
                    593:   bss_size = 0;
                    594:   text_reloc_size = 0;
                    595:   data_reloc_size = 0;
                    596: 
                    597:   data_pad = 0;
                    598:   text_pad = 0;
                    599: 
                    600:   /* Initialize the data about options.  */
                    601: 
                    602:   specified_data_size = 0;
                    603:   strip_symbols = STRIP_NONE;
                    604:   trace_files = 0;
                    605:   discard_locals = DISCARD_NONE;
                    606:   entry_symbol = 0;
                    607:   write_map = 0;
                    608:   relocatable_output = 0;
                    609:   force_common_definition = 0;
                    610:   T_flag_specified = 0;
                    611:   Tdata_flag_specified = 0;
                    612:   magic = DEFAULT_MAGIC;
                    613:   make_executable = 1;
1.1.1.2 ! root      614:   force_executable = 0;
1.1       root      615: 
                    616:   /* Initialize the cumulative counts of symbols.  */
                    617: 
                    618:   local_sym_count = 0;
                    619:   non_L_local_sym_count = 0;
                    620:   debugger_sym_count = 0;
                    621:   undefined_global_sym_count = 0;
1.1.1.2 ! root      622:   set_symbol_count = 0;
        !           623:   set_vector_count = 0;
        !           624:   global_indirect_count = 0;
1.1       root      625: 
                    626:   /* Completely decode ARGV.  */
                    627: 
                    628:   decode_command (argc, argv);
                    629: 
                    630:   /* Create the symbols `etext', `edata' and `end'.  */
                    631: 
                    632:   if (!relocatable_output)
                    633:     symtab_init ();
                    634: 
                    635:   /* Determine whether to count the header as part of
                    636:      the text size, and initialize the text size accordingly.
                    637:      This depends on the kind of system and on the output format selected.  */
                    638: 
                    639:   outheader.a_magic = magic;
                    640: #ifdef INITIALIZE_HEADER
                    641:   INITIALIZE_HEADER;
                    642: #endif
                    643: 
                    644:   text_size = sizeof (struct exec) - N_TXTOFF (outheader);
                    645:   if (text_size < 0)
                    646:     text_size = 0;
                    647:   entry_offset = text_size;
                    648: 
                    649:   if (!T_flag_specified && !relocatable_output)
                    650:     text_start = N_TXTADDR (outheader);
                    651: 
                    652:   /* The text-start address is normally this far past a page boundary.  */
                    653:   text_start_alignment = text_start % page_size;
                    654: 
                    655:   /* Load symbols of all input files.
                    656:      Also search all libraries and decide which library members to load.  */
                    657: 
                    658:   load_symbols ();
                    659: 
                    660:   /* Compute where each file's sections go, and relocate symbols.  */
                    661: 
                    662:   digest_symbols ();
                    663: 
                    664:   /* Print error messages for any missing symbols.  */
                    665: 
                    666:   if (!relocatable_output)
                    667:     list_undefined_symbols (stderr);
                    668: 
                    669:   /* Print a map, if requested.  */
                    670: 
                    671:   if (write_map) print_symbols (stdout);
                    672: 
                    673:   /* Write the output file.  */
                    674: 
1.1.1.2 ! root      675:   if (make_executable || force_executable)
        !           676:     write_output ();
1.1       root      677: 
                    678:   return 0;
                    679: }
                    680: 
                    681: void decode_option ();
                    682: 
                    683: /* Analyze a command line argument.
                    684:    Return 0 if the argument is a filename.
                    685:    Return 1 if the argument is a option complete in itself.
                    686:    Return 2 if the argument is a option which uses an argument.
                    687: 
                    688:    Thus, the value is the number of consecutive arguments
                    689:    that are part of options.  */
                    690: 
                    691: int
                    692: classify_arg (arg)
                    693:      register char *arg;
                    694: {
                    695:   if (*arg != '-') return 0;
                    696:   switch (arg[1])
                    697:     {
                    698:     case 'A':
                    699:     case 'D':
                    700:     case 'e':
                    701:     case 'L':
                    702:     case 'l':
                    703:     case 'o':
                    704:     case 'u':
                    705:     case 'y':
                    706:       if (arg[2])
                    707:        return 1;
                    708:       return 2;
                    709: 
                    710:     case 'T':
                    711:       if (arg[2] == 0)
                    712:        return 2;
                    713:       if (! strcmp (&arg[2], "text"))
                    714:        return 2;
                    715:       if (! strcmp (&arg[2], "data"))
                    716:        return 2;
                    717:       return 1;
                    718:     }
                    719: 
                    720:   return 1;
                    721: }
                    722: 
                    723: /* Process the command arguments,
                    724:    setting up file_table with an entry for each input file,
                    725:    and setting variables according to the options.  */
                    726: 
                    727: void
                    728: decode_command (argc, argv)
                    729:      char **argv;
                    730:      int argc;
                    731: {
                    732:   register int i;
                    733:   register struct file_entry *p;
                    734: 
                    735:   number_of_files = 0;
                    736:   output_filename = "a.out";
                    737: 
                    738:   n_search_dirs = 0;
                    739:   search_dirs = (char **) xmalloc (sizeof (char *));
                    740: 
                    741:   /* First compute number_of_files so we know how long to make file_table.  */
                    742:   /* Also process most options completely.  */
                    743: 
                    744:   for (i = 1; i < argc; i++)
                    745:     {
                    746:       register int code = classify_arg (argv[i]);
                    747:       if (code)
                    748:        {
                    749:          if (i + code > argc)
                    750:            fatal ("no argument following %s\n", argv[i]);
                    751: 
                    752:          decode_option (argv[i], argv[i+1]);
                    753: 
                    754:          if (argv[i][1] == 'l' || argv[i][1] == 'A')
                    755:            number_of_files++;
                    756: 
                    757:          i += code - 1;
                    758:        }
                    759:       else
                    760:        number_of_files++;
                    761:     }
                    762: 
                    763:   if (!number_of_files)
                    764:     fatal ("no input files", 0);
                    765: 
                    766:   p = file_table
                    767:     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
                    768:   bzero (p, number_of_files * sizeof (struct file_entry));
                    769: 
                    770:   /* Now scan again and fill in file_table.  */
                    771:   /* All options except -A and -l are ignored here.  */
                    772: 
                    773:   for (i = 1; i < argc; i++)
                    774:     {
                    775:       register int code = classify_arg (argv[i]);
                    776: 
                    777:       if (code)
                    778:        {
                    779:          char *string;
                    780:          if (code == 2)
                    781:            string = argv[i+1];
                    782:          else
                    783:            string = &argv[i][2];
                    784: 
                    785:          if (argv[i][1] == 'A')
                    786:            {
                    787:              if (p != file_table)
                    788:                fatal ("-A specified before an input file other than the first");
                    789: 
                    790:              p->filename = string;
                    791:              p->local_sym_name = string;
                    792:              p->just_syms_flag = 1;
                    793:              p++;
                    794:            }
                    795:          if (argv[i][1] == 'l')
                    796:            {
                    797:              p->filename = concat ("lib", string, ".a");
                    798:              p->local_sym_name = concat ("-l", string, "");
                    799:              p->search_dirs_flag = 1;
                    800:              p++;
                    801:            }
                    802:          i += code - 1;
                    803:        }
                    804:       else
                    805:        {
                    806:          p->filename = argv[i];
                    807:          p->local_sym_name = argv[i];
                    808:          p++;
                    809:        }
                    810:     }
                    811: 
                    812:   /* Now check some option settings for consistency.  */
                    813: 
                    814:   if ((magic == ZMAGIC || magic == NMAGIC)
                    815:       && (text_start - text_start_alignment) & (page_size - 1))
                    816:     fatal ("-T argument not multiple of page size, with sharable output", 0);
                    817: 
                    818:   /* Append the standard search directories to the user-specified ones.  */
                    819:   {
                    820:     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
                    821:     n_search_dirs += n;
                    822:     search_dirs
                    823:       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
                    824:     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
                    825:           n * sizeof (char *));
                    826:   }
                    827: }
                    828: 
1.1.1.2 ! root      829: int parse ();
        !           830: 
1.1       root      831: /* Record an option and arrange to act on it later.
                    832:    ARG should be the following command argument,
                    833:    which may or may not be used by this option.
                    834: 
                    835:    The `l' and `A' options are ignored here since they actually
                    836:    specify input files.  */
                    837: 
                    838: void
                    839: decode_option (swt, arg)
                    840:      register char *swt, *arg;
                    841: {
1.1.1.2 ! root      842:   if (! strcmp (swt + 1, "Ttext"))
1.1       root      843:     {
                    844:       text_start = parse (arg, "%x", "invalid argument to -Ttext");
                    845:       T_flag_specified = 1;
                    846:       return;
                    847:     }
1.1.1.2 ! root      848:   if (! strcmp (swt + 1, "Tdata"))
1.1       root      849:     {
                    850:       data_start = parse (arg, "%x", "invalid argument to -Tdata");
                    851:       Tdata_flag_specified = 1;
                    852:       return;
                    853:     }
1.1.1.2 ! root      854:   if (! strcmp (swt + 1, "noinhibit-exec"))
        !           855:     {
        !           856:       force_executable = 1;
        !           857:       return;
        !           858:     }
1.1       root      859: 
                    860:   if (swt[2] != 0)
                    861:     arg = &swt[2];
                    862: 
                    863:   switch (swt[1])
                    864:     {
                    865:     case 'A':
                    866:       return;
                    867: 
                    868:     case 'D':
                    869:       specified_data_size = parse (arg, "%x", "invalid argument to -D");
                    870:       return;
                    871: 
                    872:     case 'd':
                    873:       force_common_definition = 1;
                    874:       return;
                    875: 
                    876:     case 'e':
                    877:       entry_symbol = getsym (arg);
                    878:       if (!entry_symbol->defined && !entry_symbol->referenced)
                    879:        undefined_global_sym_count++;
                    880:       entry_symbol->referenced = 1;
                    881:       return;
                    882: 
                    883:     case 'l':
                    884:       return;
                    885: 
                    886:     case 'L':
                    887:       n_search_dirs++;
                    888:       search_dirs
                    889:        = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
                    890:       search_dirs[n_search_dirs - 1] = arg;
                    891:       return;
                    892:       
                    893:     case 'M':
                    894:       write_map = 1;
                    895:       return;
                    896: 
                    897:     case 'N':
                    898:       magic = OMAGIC;
                    899:       return;
                    900: 
                    901:     case 'n':
                    902:       magic = NMAGIC;
                    903:       return;
                    904: 
                    905:     case 'o':
                    906:       output_filename = arg;
                    907:       return;
                    908: 
                    909:     case 'r':
                    910:       relocatable_output = 1;
                    911:       magic = OMAGIC;
                    912:       text_start = 0;
                    913:       return;
                    914: 
                    915:     case 'S':
                    916:       strip_symbols = STRIP_DEBUGGER;
                    917:       return;
                    918: 
                    919:     case 's':
                    920:       strip_symbols = STRIP_ALL;
                    921:       return;
                    922: 
                    923:     case 'T':
                    924:       text_start = parse (arg, "%x", "invalid argument to -T");
                    925:       T_flag_specified = 1;
                    926:       return;
                    927: 
                    928:     case 't':
                    929:       trace_files = 1;
                    930:       return;
                    931: 
                    932:     case 'u':
                    933:       {
                    934:        register symbol *sp = getsym (arg);
                    935:        if (!sp->defined && !sp->referenced) undefined_global_sym_count++;
                    936:        sp->referenced = 1;
                    937:       }
                    938:       return;
                    939: 
                    940:     case 'X':
                    941:       discard_locals = DISCARD_L;
                    942:       return;
                    943: 
                    944:     case 'x':
                    945:       discard_locals = DISCARD_ALL;
                    946:       return;
                    947: 
                    948:     case 'y':
                    949:       {
                    950:        register symbol *sp = getsym (&swt[2]);
                    951:        sp->trace = 1;
                    952:       }
                    953:       return;
                    954: 
                    955:     case 'z':
                    956:       magic = ZMAGIC;
                    957:       return;
                    958: 
                    959:     default:
                    960:       fatal ("invalid command option `%s'", swt);
                    961:     }
                    962: }
                    963: 
1.1.1.2 ! root      964: /** Convenient functions for operating on one or all files being */
        !           965:  /** loaded.  */
        !           966: void print_file_name ();
1.1       root      967: 
                    968: /* Call FUNCTION on each input file entry.
                    969:    Do not call for entries for libraries;
                    970:    instead, call once for each library member that is being loaded.
1.1.1.2 ! root      971:    
1.1       root      972:    FUNCTION receives two arguments: the entry, and ARG.  */
                    973: 
                    974: void
                    975: each_file (function, arg)
                    976:      register void (*function)();
                    977:      register int arg;
                    978: {
                    979:   register int i;
1.1.1.2 ! root      980:   
1.1       root      981:   for (i = 0; i < number_of_files; i++)
                    982:     {
                    983:       register struct file_entry *entry = &file_table[i];
                    984:       if (entry->library_flag)
                    985:         {
                    986:          register struct file_entry *subentry = entry->subfiles;
                    987:          for (; subentry; subentry = subentry->chain)
                    988:            (*function) (subentry, arg);
                    989:        }
                    990:       else
                    991:        (*function) (entry, arg);
                    992:     }
                    993: }
                    994: 
1.1.1.2 ! root      995: /* Call FUNCTION on each input file entry until it returns a non-zero
        !           996:    value.  Return this value.
        !           997:    Do not call for entries for libraries;
        !           998:    instead, call once for each library member that is being loaded.
        !           999:    
        !          1000:    FUNCTION receives two arguments: the entry, and ARG.  It must be a
        !          1001:    function returning unsigned long (though this can probably be fudged). */
        !          1002: 
        !          1003: unsigned long
        !          1004: check_each_file (function, arg)
        !          1005:      register unsigned long (*function)();
        !          1006:      register int arg;
        !          1007: {
        !          1008:   register int i;
        !          1009:   register unsigned long return_val;
        !          1010:   
        !          1011:   for (i = 0; i < number_of_files; i++)
        !          1012:     {
        !          1013:       register struct file_entry *entry = &file_table[i];
        !          1014:       if (entry->library_flag)
        !          1015:         {
        !          1016:          register struct file_entry *subentry = entry->subfiles;
        !          1017:          for (; subentry; subentry = subentry->chain)
        !          1018:            if (return_val = (*function) (subentry, arg))
        !          1019:              return return_val;
        !          1020:        }
        !          1021:       else
        !          1022:        if (return_val = (*function) (entry, arg))
        !          1023:          return return_val;
        !          1024:     }
        !          1025:   return 0;
        !          1026: }
        !          1027: 
1.1       root     1028: /* Like `each_file' but ignore files that were just for symbol definitions.  */
                   1029: 
                   1030: void
                   1031: each_full_file (function, arg)
                   1032:      register void (*function)();
                   1033:      register int arg;
                   1034: {
                   1035:   register int i;
                   1036: 
                   1037:   for (i = 0; i < number_of_files; i++)
                   1038:     {
                   1039:       register struct file_entry *entry = &file_table[i];
                   1040:       if (entry->just_syms_flag)
                   1041:        continue;
                   1042:       if (entry->library_flag)
                   1043:         {
                   1044:          register struct file_entry *subentry = entry->subfiles;
                   1045:          for (; subentry; subentry = subentry->chain)
                   1046:            (*function) (subentry, arg);
                   1047:        }
                   1048:       else
                   1049:        (*function) (entry, arg);
                   1050:     }
                   1051: }
                   1052: 
                   1053: /* Close the input file that is now open.  */
                   1054: 
                   1055: void
                   1056: file_close ()
                   1057: {
                   1058:   close (input_desc);
                   1059:   input_desc = 0;
                   1060:   input_file = 0;
                   1061: }
                   1062: 
                   1063: /* Open the input file specified by 'entry', and return a descriptor.
                   1064:    The open file is remembered; if the same file is opened twice in a row,
                   1065:    a new open is not actually done.  */
                   1066: 
                   1067: int
                   1068: file_open (entry)
                   1069:      register struct file_entry *entry;
                   1070: {
                   1071:   register int desc;
                   1072: 
                   1073:   if (entry->superfile)
                   1074:     return file_open (entry->superfile);
                   1075: 
                   1076:   if (entry == input_file)
                   1077:     return input_desc;
                   1078: 
                   1079:   if (input_file) file_close ();
                   1080: 
                   1081:   if (entry->search_dirs_flag)
                   1082:     {
                   1083:       register char **p = search_dirs;
                   1084:       int i;
                   1085: 
                   1086:       for (i = 0; i < n_search_dirs; i++)
                   1087:        {
                   1088:          register char *string
                   1089:            = concat (search_dirs[i], "/", entry->filename);
                   1090:          desc = open (string, O_RDONLY, 0);
                   1091:          if (desc > 0)
                   1092:            {
                   1093:              entry->filename = string;
                   1094:              entry->search_dirs_flag = 0;
                   1095:              break;
                   1096:            }
                   1097:          free (string);
                   1098:        }
                   1099:     }
                   1100:   else
                   1101:     desc = open (entry->filename, O_RDONLY, 0);
                   1102: 
                   1103:   if (desc > 0)
                   1104:     {
                   1105:       input_file = entry;
                   1106:       input_desc = desc;
                   1107:       return desc;
                   1108:     }
                   1109: 
                   1110:   perror_file (entry);
1.1.1.2 ! root     1111:   /* NOTREACHED */
1.1       root     1112: }
                   1113: 
                   1114: /* Print the filename of ENTRY on OUTFILE (a stdio stream),
                   1115:    and then a newline.  */
                   1116: 
1.1.1.2 ! root     1117: void
1.1       root     1118: prline_file_name (entry, outfile)
                   1119:      struct file_entry *entry;
                   1120:      FILE *outfile;
                   1121: {
                   1122:   print_file_name (entry, outfile);
                   1123:   fprintf (outfile, "\n");
                   1124: }
                   1125: 
                   1126: /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
                   1127: 
1.1.1.2 ! root     1128: void
1.1       root     1129: print_file_name (entry, outfile)
                   1130:      struct file_entry *entry;
                   1131:      FILE *outfile;
                   1132: {
                   1133:   if (entry->superfile)
                   1134:     {
                   1135:       print_file_name (entry->superfile, outfile);
                   1136:       fprintf (outfile, "(%s)", entry->filename);
                   1137:     }
                   1138:   else
                   1139:     fprintf (outfile, "%s", entry->filename);
                   1140: }
1.1.1.2 ! root     1141: 
        !          1142: /* Return the filename of entry as a string (malloc'd for the purpose) */
        !          1143: 
        !          1144: char *
        !          1145: get_file_name (entry)
        !          1146:      struct file_entry *entry;
        !          1147: {
        !          1148:   char *result, *supfile;
        !          1149:   if (entry->superfile)
        !          1150:     {
        !          1151:       supfile = get_file_name (entry->superfile);
        !          1152:       result = (char *) xmalloc (strlen (supfile)
        !          1153:                                 + strlen (entry->filename) + 3);
        !          1154:       sprintf (result, "%s(%s)", supfile, entry->filename);
        !          1155:       free (supfile);
        !          1156:     }
        !          1157:   else
        !          1158:     {
        !          1159:       result = (char *) xmalloc (strlen (entry->filename) + 1);
        !          1160:       strcpy (result, entry->filename);
        !          1161:     }
        !          1162:   return result;
        !          1163: }
1.1       root     1164: 
                   1165: /* Medium-level input routines for rel files.  */
                   1166: 
                   1167: /* Read a file's header into the proper place in the file_entry.
                   1168:    DESC is the descriptor on which the file is open.
                   1169:    ENTRY is the file's entry.  */
                   1170: 
                   1171: void
                   1172: read_header (desc, entry)
                   1173:      int desc;
                   1174:      register struct file_entry *entry;
                   1175: {
                   1176:   register int len;
                   1177:   struct exec *loc = &entry->header;
                   1178: 
                   1179:   lseek (desc, entry->starting_offset, 0);
                   1180:   len = read (desc, loc, sizeof (struct exec));
                   1181:   if (len != sizeof (struct exec))
                   1182:     fatal_with_file ("failure reading header of ", entry);
                   1183:   if (N_BADMAG (*loc))
                   1184:     fatal_with_file ("bad magic number in ", entry);
                   1185: 
                   1186:   entry->header_read_flag = 1;
                   1187: }
                   1188: 
                   1189: /* Read the symbols of file ENTRY into core.
                   1190:    Assume it is already open, on descriptor DESC.
                   1191:    Also read the length of the string table, which follows the symbol table,
                   1192:    but don't read the contents of the string table.  */
                   1193: 
                   1194: void
                   1195: read_entry_symbols (desc, entry)
                   1196:      struct file_entry *entry;
                   1197:      int desc;
                   1198: {
                   1199:   int str_size;
                   1200: 
                   1201:   if (!entry->header_read_flag)
                   1202:     read_header (desc, entry);
                   1203: 
                   1204:   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
                   1205: 
                   1206:   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
                   1207:   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
                   1208:     fatal_with_file ("premature end of file in symbols of ", entry);
                   1209: 
                   1210:   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
                   1211:   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
                   1212:     fatal_with_file ("bad string table size in ", entry);
                   1213: 
                   1214:   entry->string_size = str_size;
                   1215: }
                   1216: 
                   1217: /* Read the string table of file ENTRY into core.
                   1218:    Assume it is already open, on descriptor DESC.
                   1219:    Also record whether a GDB symbol segment follows the string table.  */
                   1220: 
                   1221: void
                   1222: read_entry_strings (desc, entry)
                   1223:      struct file_entry *entry;
                   1224:      int desc;
                   1225: {
                   1226:   int buffer;
                   1227: 
                   1228:   if (!entry->header_read_flag)
                   1229:     read_header (desc, entry);
                   1230: 
                   1231:   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
                   1232:   if (entry->string_size != read (desc, entry->strings, entry->string_size))
                   1233:     fatal_with_file ("premature end of file in strings of ", entry);
                   1234: 
                   1235:   /* While we are here, see if the file has a symbol segment at the end.
                   1236:      For a separate file, just try reading some more.
                   1237:      For a library member, compare current pos against total size.  */
                   1238:   if (entry->superfile)
                   1239:     {
                   1240:       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
                   1241:        return;
                   1242:     }
                   1243:   else
                   1244:     {
                   1245:       buffer = read (desc, &buffer, sizeof buffer);
                   1246:       if (buffer == 0)
                   1247:        return;
                   1248:       if (buffer != sizeof buffer)
                   1249:        fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
                   1250:     }
                   1251: 
                   1252:   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
                   1253: }
                   1254: 
                   1255: /* Read in the symbols of all input files.  */
                   1256: 
                   1257: void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
                   1258: void enter_file_symbols (), enter_global_ref (), search_library ();
                   1259: 
                   1260: void
                   1261: load_symbols ()
                   1262: {
                   1263:   register int i;
                   1264: 
                   1265:   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
                   1266: 
                   1267:   for (i = 0; i < number_of_files; i++)
                   1268:     {
                   1269:       register struct file_entry *entry = &file_table[i];
                   1270:       read_file_symbols (entry);
                   1271:     }
                   1272: 
                   1273:   if (trace_files) fprintf (stderr, "\n");
                   1274: }
                   1275: 
                   1276: /* If ENTRY is a rel file, read its symbol and string sections into core.
                   1277:    If it is a library, search it and load the appropriate members
                   1278:    (which means calling this function recursively on those members).  */
                   1279: 
                   1280: void
                   1281: read_file_symbols (entry)
                   1282:      register struct file_entry *entry;
                   1283: {
                   1284:   register int desc;
                   1285:   register int len;
                   1286:   int magicnum;
                   1287: 
                   1288:   desc = file_open (entry);
                   1289: 
                   1290:   len = read (desc, &magicnum, sizeof magicnum);
                   1291:   if (len != sizeof magicnum)
                   1292:     fatal_with_file ("failure reading header of ", entry);
                   1293: 
                   1294:   if (!N_BADMAG (*((struct exec *)&magicnum)))
                   1295:     {
                   1296:       read_entry_symbols (desc, entry);
                   1297:       entry->strings = (char *) alloca (entry->string_size);
                   1298:       read_entry_strings (desc, entry);
                   1299:       enter_file_symbols (entry);
                   1300:       entry->strings = 0;
                   1301:     }
                   1302:   else
                   1303:     {
                   1304:       char armag[SARMAG];
                   1305: 
                   1306:       lseek (desc, 0, 0);
                   1307:       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
                   1308:        fatal_with_file ("malformed input file (not rel or archive) ", entry);
                   1309:       entry->library_flag = 1;
                   1310:       search_library (desc, entry);
                   1311:     }
                   1312: 
                   1313:   file_close ();
                   1314: }
                   1315: 
                   1316: /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
                   1317: 
                   1318: void
                   1319: enter_file_symbols (entry)
                   1320:      struct file_entry *entry;
                   1321: {
                   1322:    register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
1.1.1.2 ! root     1323:    int lowest_set_vector = -1;
1.1       root     1324: 
                   1325:   if (trace_files) prline_file_name (entry, stderr);
                   1326: 
                   1327:   for (p = entry->symbols; p < end; p++)
1.1.1.2 ! root     1328: #if 0
        !          1329:     /* Currently N_SETV symbols shouldn't be in input to the loader, */
        !          1330:     /* but I'll leave this here in case they ever are */
        !          1331:     if ((p->n_type & ~N_EXT) == N_SETV &&
        !          1332:        (lowest_set_vector = -1 || lowest_set_vector > p->n_value))
        !          1333:       lowest_set_vector = p->n_value;
        !          1334:     else
        !          1335: #endif
        !          1336:     if (SET_ELEMENT_P (p->n_type))
        !          1337:       {
        !          1338:        set_symbol_count++;
        !          1339:        if (!relocatable_output)
        !          1340:          enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
        !          1341:       }
        !          1342:     else if (p->n_type & N_EXT)
1.1       root     1343:       enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
                   1344:     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
                   1345:       {
                   1346:        if ((p->n_un.n_strx + entry->strings)[0] != 'L')
                   1347:          non_L_local_sym_count++;
                   1348:        local_sym_count++;
                   1349:       }
                   1350:     else debugger_sym_count++;
                   1351: 
1.1.1.2 ! root     1352: #if 0   
        !          1353:   entry->set_vector_offset = lowest_set_vector;
        !          1354: #endif
        !          1355: 
1.1       root     1356:    /* Count one for the local symbol that we generate,
                   1357:       whose name is the file's name (usually) and whose address
                   1358:       is the start of the file's text.  */
                   1359: 
                   1360:   local_sym_count++;
                   1361:   non_L_local_sym_count++;
                   1362: }
                   1363: 
                   1364: /* Enter one global symbol in the hash table.
                   1365:    NLIST_P points to the `struct nlist' read from the file
                   1366:    that describes the global symbol.  NAME is the symbol's name.
                   1367:    ENTRY is the file entry for the file the symbol comes from.
                   1368: 
                   1369:    The `struct nlist' is modified by placing it on a chain of
                   1370:    all such structs that refer to the same global symbol.
                   1371:    This chain starts in the `refs' field of the symbol table entry
                   1372:    and is chained through the `n_name'.  */
                   1373: 
                   1374: void
                   1375: enter_global_ref (nlist_p, name, entry)
                   1376:      register struct nlist *nlist_p;
                   1377:      char *name;
                   1378:      struct file_entry *entry;
                   1379: {
                   1380:   register symbol *sp = getsym (name);
                   1381:   register int type = nlist_p->n_type;
                   1382:   int oldref = sp->referenced;
                   1383:   int olddef = sp->defined;
                   1384: 
                   1385:   nlist_p->n_un.n_name = (char *) sp->refs;
                   1386:   sp->refs = nlist_p;
                   1387: 
                   1388:   sp->referenced = 1;
                   1389:   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
                   1390:     {
                   1391:       sp->defined = 1;
                   1392:       if (oldref && !olddef) undefined_global_sym_count--;
                   1393:       /* If this is a common definition, keep track of largest
                   1394:         common definition seen for this symbol.  */
                   1395:       if (type == (N_UNDF | N_EXT)
                   1396:          && sp->max_common_size < nlist_p->n_value)
                   1397:        sp->max_common_size = nlist_p->n_value;
1.1.1.2 ! root     1398:       if (SET_ELEMENT_P (type) && !olddef)
        !          1399:        set_vector_count++;
        !          1400:       /* Indirect symbols value should be modified to point
        !          1401:         a symbol being equivalenced to. */
        !          1402:       if (type == (N_INDR | N_EXT))
        !          1403:        {
        !          1404:          nlist_p->n_value =
        !          1405:            (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
        !          1406:                                   + entry->strings);
        !          1407:          global_indirect_count++;
        !          1408:        }
1.1       root     1409:     }
                   1410:   else
                   1411:     if (!oldref) undefined_global_sym_count++;
                   1412: 
                   1413:   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
                   1414:     text_start = nlist_p->n_value;
                   1415: 
                   1416:   if (sp->trace)
                   1417:     {
                   1418:       register char *reftype;
                   1419:       switch (type & N_TYPE)
                   1420:        {
                   1421:        case N_UNDF:
                   1422:          if (nlist_p->n_value)
                   1423:            reftype = "defined as common";
                   1424:          else reftype = "referenced";
                   1425:          break;
                   1426: 
                   1427:        case N_ABS:
                   1428:          reftype = "defined as absolute";
                   1429:          break;
                   1430: 
                   1431:        case N_TEXT:
                   1432:          reftype = "defined in text section";
                   1433:          break;
                   1434: 
                   1435:        case N_DATA:
                   1436:          reftype = "defined in data section";
                   1437:          break;
                   1438: 
                   1439:        case N_BSS:
                   1440:          reftype = "defined in BSS section";
                   1441:          break;
1.1.1.2 ! root     1442: 
        !          1443:        case N_SETT:
        !          1444:          reftype = "is a text set element";
        !          1445:          break;
        !          1446:          
        !          1447:        case N_SETD:
        !          1448:          reftype = "is a data set element";
        !          1449:          break;
        !          1450:          
        !          1451:        case N_SETB:
        !          1452:          reftype = "is a BSS set element";
        !          1453:          break;
        !          1454:          
        !          1455:        case N_SETA:
        !          1456:          reftype = "is an absolute set element";
        !          1457:          break;
        !          1458: 
        !          1459:        case N_SETV:
        !          1460:          reftype = "defined in text section as vector";
        !          1461:          break;
        !          1462:          
        !          1463:        case N_INDR:
        !          1464:          reftype = (char *) alloca (23
        !          1465:                                     + strlen ((nlist_p + 1)->n_un.n_strx
        !          1466:                                               + entry->strings));
        !          1467:          sprintf (reftype, "defined equivalent to %s",
        !          1468:                   (nlist_p + 1)->n_un.n_strx + entry->strings);
        !          1469:          break;
1.1       root     1470:        }
                   1471: 
                   1472:       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
                   1473:       print_file_name (entry, stderr);
                   1474:       fprintf (stderr, "\n");
                   1475:     }
                   1476: }
1.1.1.2 ! root     1477: 
        !          1478: /* This return 0 if the given file entry's symbol table does *not*
        !          1479:    contain the nlist point entry, and it returns the files entry
        !          1480:    pointer (cast to unsigned long) if it does. */
        !          1481: 
        !          1482: unsigned long
        !          1483: contains_symbol (entry, n_ptr)
        !          1484:      struct file_entry *entry;
        !          1485:      register struct nlist *n_ptr;
        !          1486: {
        !          1487:   if (n_ptr >= entry->symbols &&
        !          1488:       n_ptr < (entry->symbols
        !          1489:               + (entry->header.a_syms / sizeof (struct nlist))))
        !          1490:     return (unsigned long) entry;
        !          1491:   return 0;
        !          1492: }
        !          1493: 
1.1       root     1494: 
                   1495: /* Searching libraries */
                   1496: 
                   1497: struct file_entry *decode_library_subfile ();
                   1498: void linear_library (), symdef_library ();
                   1499: 
                   1500: /* Search the library ENTRY, already open on descriptor DESC.
                   1501:    This means deciding which library members to load,
                   1502:    making a chain of `struct file_entry' for those members,
                   1503:    and entering their global symbols in the hash table.  */
                   1504: 
                   1505: void
                   1506: search_library (desc, entry)
                   1507:      int desc;
                   1508:      struct file_entry *entry;
                   1509: {
                   1510:   int member_length;
                   1511:   register char *name;
                   1512:   register struct file_entry *subentry;
                   1513: 
                   1514:   if (!undefined_global_sym_count) return;
                   1515: 
                   1516:   /* Examine its first member, which starts SARMAG bytes in.  */
                   1517:   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
                   1518:   if (!subentry) return;
                   1519: 
                   1520:   name = subentry->filename;
                   1521:   free (subentry);
                   1522: 
                   1523:   /* Search via __.SYMDEF if that exists, else linearly.  */
                   1524: 
                   1525:   if (!strcmp (name, "__.SYMDEF"))
                   1526:     symdef_library (desc, entry, member_length);
                   1527:   else
                   1528:     linear_library (desc, entry);
                   1529: }
                   1530: 
                   1531: /* Construct and return a file_entry for a library member.
                   1532:    The library's file_entry is library_entry, and the library is open on DESC.
                   1533:    SUBFILE_OFFSET is the byte index in the library of this member's header.
                   1534:    We store the length of the member into *LENGTH_LOC.  */
                   1535: 
                   1536: struct file_entry *
                   1537: decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
                   1538:      int desc;
                   1539:      struct file_entry *library_entry;
                   1540:      int subfile_offset;
                   1541:      int *length_loc;
                   1542: {
                   1543:   int bytes_read;
                   1544:   register int namelen;
                   1545:   int member_length;
                   1546:   register char *name;
                   1547:   struct ar_hdr hdr1;
                   1548:   register struct file_entry *subentry;
                   1549: 
                   1550:   lseek (desc, subfile_offset, 0);
                   1551: 
                   1552:   bytes_read = read (desc, &hdr1, sizeof hdr1);
                   1553:   if (!bytes_read)
                   1554:     return 0;          /* end of archive */
                   1555: 
                   1556:   if (sizeof hdr1 != bytes_read)
                   1557:     fatal_with_file ("malformed library archive ", library_entry);
                   1558: 
                   1559:   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
                   1560:     fatal_with_file ("malformatted header of archive member in ", library_entry);
                   1561: 
                   1562:   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
                   1563:   bzero (subentry, sizeof (struct file_entry));
                   1564: 
                   1565:   for (namelen = 0;
                   1566:        namelen < sizeof hdr1.ar_name
                   1567:        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ';
                   1568:        namelen++);
                   1569: 
                   1570:   name = (char *) xmalloc (namelen+1);
                   1571:   strncpy (name, hdr1.ar_name, namelen);
                   1572:   name[namelen] = 0;
                   1573: 
                   1574:   subentry->filename = name;
                   1575:   subentry->local_sym_name = name;
                   1576:   subentry->symbols = 0;
                   1577:   subentry->strings = 0;
                   1578:   subentry->subfiles = 0;
                   1579:   subentry->starting_offset = subfile_offset + sizeof hdr1;
                   1580:   subentry->superfile = library_entry;
                   1581:   subentry->library_flag = 0;
                   1582:   subentry->header_read_flag = 0;
                   1583:   subentry->just_syms_flag = 0;
                   1584:   subentry->chain = 0;
                   1585:   subentry->total_size = member_length;
                   1586: 
                   1587:   (*length_loc) = member_length;
                   1588: 
                   1589:   return subentry;
                   1590: }
                   1591: 
1.1.1.2 ! root     1592: int subfile_wanted_p ();
        !          1593: 
1.1       root     1594: /* Search a library that has a __.SYMDEF member.
                   1595:    DESC is a descriptor on which the library is open.
                   1596:      The file pointer is assumed to point at the __.SYMDEF data.
                   1597:    ENTRY is the library's file_entry.
                   1598:    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
                   1599: 
                   1600: void
                   1601: symdef_library (desc, entry, member_length)
                   1602:      int desc;
                   1603:      struct file_entry *entry;
                   1604:      int member_length;
                   1605: {
                   1606:   int *symdef_data = (int *) xmalloc (member_length);
                   1607:   register struct symdef *symdef_base;
                   1608:   char *sym_name_base;
                   1609:   int number_of_symdefs;
                   1610:   int length_of_strings;
                   1611:   int not_finished;
                   1612:   int bytes_read;
                   1613:   register int i;
                   1614:   struct file_entry *prev = 0;
                   1615:   int prev_offset = 0;
                   1616: 
                   1617:   bytes_read = read (desc, symdef_data, member_length);
                   1618:   if (bytes_read != member_length)
                   1619:     fatal_with_file ("malformatted __.SYMDEF in ", entry);
                   1620: 
                   1621:   number_of_symdefs = *symdef_data / sizeof (struct symdef);
                   1622:   if (number_of_symdefs < 0 ||
                   1623:        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
                   1624:     fatal_with_file ("malformatted __.SYMDEF in ", entry);
                   1625: 
                   1626:   symdef_base = (struct symdef *) (symdef_data + 1);
                   1627:   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
                   1628: 
                   1629:   if (length_of_strings < 0
                   1630:       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
                   1631:          + 2 * sizeof (int) != member_length)
                   1632:     fatal_with_file ("malformatted __.SYMDEF in ", entry);
                   1633: 
                   1634:   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
                   1635: 
                   1636:   /* Check all the string indexes for validity.  */
                   1637: 
                   1638:   for (i = 0; i < number_of_symdefs; i++)
                   1639:     {
                   1640:       register int index = symdef_base[i].symbol_name_string_index;
                   1641:       if (index < 0 || index >= length_of_strings
                   1642:          || (index && *(sym_name_base + index - 1)))
                   1643:        fatal_with_file ("malformatted __.SYMDEF in ", entry);
                   1644:     }
                   1645: 
                   1646:   /* Search the symdef data for members to load.
                   1647:      Do this until one whole pass finds nothing to load.  */
                   1648: 
                   1649:   not_finished = 1;
                   1650:   while (not_finished)
                   1651:     {
                   1652:       not_finished = 0;
                   1653: 
                   1654:       /* Scan all the symbols mentioned in the symdef for ones that we need.
                   1655:         Load the library members that contain such symbols.  */
                   1656: 
                   1657:       for (i = 0; i < number_of_symdefs && undefined_global_sym_count; i++)
                   1658:        if (symdef_base[i].symbol_name_string_index >= 0)
                   1659:          {
                   1660:            register symbol *sp;
                   1661: 
                   1662:            sp = getsym_soft (sym_name_base
                   1663:                              + symdef_base[i].symbol_name_string_index);
                   1664: 
                   1665:            /* If we find a symbol that appears to be needed, think carefully
                   1666:               about the archive member that the symbol is in.  */
                   1667: 
                   1668:            if (sp && sp->referenced && !sp->defined)
                   1669:              {
                   1670:                int junk;
                   1671:                register int j;
                   1672:                register int offset = symdef_base[i].library_member_offset;
                   1673:                struct file_entry *subentry;
                   1674: 
                   1675:                /* Don't think carefully about any archive member
                   1676:                   more than once in a given pass.  */
                   1677: 
                   1678:                if (prev_offset == offset)
                   1679:                  continue;
                   1680:                prev_offset = offset;
                   1681: 
                   1682:                /* Read the symbol table of the archive member.  */
                   1683: 
                   1684:                subentry = decode_library_subfile (desc, entry, offset, &junk);
                   1685:                read_entry_symbols (desc, subentry);
                   1686:                subentry->strings = (char *) malloc (subentry->string_size);
                   1687:                read_entry_strings (desc, subentry);
                   1688: 
                   1689:                /* Now scan the symbol table and decide whether to load.  */
                   1690: 
                   1691:                if (!subfile_wanted_p (subentry))
                   1692:                  {
                   1693:                    free (subentry->symbols);
                   1694:                    free (subentry);
                   1695:                  }
                   1696:                else
                   1697:                  {
                   1698:                    /* This member is needed; load it.
                   1699:                       Since we are loading something on this pass,
                   1700:                       we must make another pass through the symdef data.  */
                   1701: 
                   1702:                    not_finished = 1;
                   1703: 
                   1704:                    enter_file_symbols (subentry);
                   1705: 
                   1706:                    if (prev)
                   1707:                      prev->chain = subentry;
                   1708:                    else entry->subfiles = subentry;
                   1709:                    prev = subentry;
                   1710: 
                   1711:                    /* Clear out this member's symbols from the symdef data
                   1712:                       so that following passes won't waste time on them.  */
                   1713: 
                   1714:                    for (j = 0; j < number_of_symdefs; j++)
                   1715:                      {
                   1716:                        if (symdef_base[j].library_member_offset == offset)
                   1717:                          symdef_base[j].symbol_name_string_index = -1;
                   1718:                      }
                   1719:                  }
                   1720: 
                   1721:                /* We'll read the strings again if we need them again.  */
                   1722:                free (subentry->strings);
                   1723:              }
                   1724:          }
                   1725:     }
                   1726: 
                   1727:   free (symdef_data);
                   1728: }
                   1729: 
                   1730: /* Search a library that has no __.SYMDEF.
                   1731:    ENTRY is the library's file_entry.
                   1732:    DESC is the descriptor it is open on.  */
                   1733: 
                   1734: void
                   1735: linear_library (desc, entry)
                   1736:      int desc;
                   1737:      struct file_entry *entry;
                   1738: {
                   1739:   register struct file_entry *prev = 0;
                   1740:   register int this_subfile_offset = SARMAG;
                   1741: 
                   1742:   while (undefined_global_sym_count)
                   1743:     {
                   1744:       int member_length;
                   1745:       register struct file_entry *subentry;
                   1746: 
                   1747:       subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
                   1748: 
                   1749:       if (!subentry) return;
                   1750: 
                   1751:       read_entry_symbols (desc, subentry);
                   1752:       subentry->strings = (char *) alloca (subentry->string_size);
                   1753:       read_entry_strings (desc, subentry);
                   1754: 
                   1755:       if (!subfile_wanted_p (subentry))
                   1756:        {
                   1757:          free (subentry->symbols);
                   1758:          free (subentry);
                   1759:        }
                   1760:       else
                   1761:        {
                   1762:          enter_file_symbols (subentry);
                   1763: 
                   1764:          if (prev)
                   1765:            prev->chain = subentry;
                   1766:          else entry->subfiles = subentry;
                   1767:          prev = subentry;
                   1768:        }
                   1769: 
                   1770:       this_subfile_offset += member_length + sizeof (struct ar_hdr);
                   1771:       if (this_subfile_offset & 1) this_subfile_offset++;
                   1772:     }
                   1773: }
                   1774: 
                   1775: /* ENTRY is an entry for a library member.
                   1776:    Its symbols have been read into core, but not entered.
                   1777:    Return nonzero if we ought to load this member.  */
                   1778: 
                   1779: int
                   1780: subfile_wanted_p (entry)
                   1781:      struct file_entry *entry;
                   1782: {
                   1783:   register struct nlist *p;
                   1784:   register struct nlist *end
                   1785:     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
                   1786: 
                   1787:   for (p = entry->symbols; p < end; p++)
                   1788:     {
                   1789:       register int type = p->n_type;
                   1790: 
                   1791:       if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value))
                   1792:        {
                   1793:          register char *name = p->n_un.n_strx + entry->strings;
                   1794:          register symbol *sp = getsym_soft (name);
                   1795: 
                   1796:          /* If this symbol has not been hashed, we can't be looking for it. */
                   1797: 
                   1798:          if (!sp) continue;
                   1799: 
                   1800:          if (sp->referenced && !sp->defined)
                   1801:            {
                   1802:              /* This is a symbol we are looking for.  */
                   1803:              if (type == (N_UNDF | N_EXT))
                   1804:                {
                   1805:                  /* Symbol being defined as common.
                   1806:                     Remember this, but don't load subfile just for this.  */
                   1807: 
                   1808:                  if (sp->max_common_size < p->n_value)
                   1809:                    sp->max_common_size = p->n_value;
                   1810:                  if (!sp->defined)
                   1811:                    undefined_global_sym_count--;
                   1812:                  sp->defined = 1;
                   1813:                  continue;
                   1814:                }
                   1815: 
                   1816:              if (write_map)
                   1817:                {
                   1818:                  print_file_name (entry, stdout);
                   1819:                  fprintf (stdout, " needed due to %s\n", sp->name);
                   1820:                }
                   1821:              return 1;
                   1822:            }
                   1823:        }
                   1824:     }
                   1825: 
                   1826:   return 0;
                   1827: }
                   1828: 
                   1829: void consider_file_section_lengths (), relocate_file_addresses ();
1.1.1.2 ! root     1830: void print_files_defining_symbol ();
1.1       root     1831: 
                   1832: /* Having entered all the global symbols and found the sizes of sections
                   1833:    of all files to be linked, make all appropriate deductions from this data.
                   1834: 
                   1835:    We propagate global symbol values from definitions to references.
                   1836:    We compute the layout of the output file and where each input file's
                   1837:    contents fit into it.  */
                   1838: 
                   1839: void
                   1840: digest_symbols ()
                   1841: {
                   1842:   register int i;
1.1.1.2 ! root     1843:   int setv_fill_count;
1.1       root     1844: 
                   1845:   if (trace_files)
                   1846:     fprintf (stderr, "Digesting symbol information:\n\n");
                   1847: 
                   1848:   /* Compute total size of sections */
                   1849: 
                   1850:   each_file (consider_file_section_lengths, 0);
                   1851: 
1.1.1.2 ! root     1852:   /* Setup the set element vector */
        !          1853: 
        !          1854:   if (!relocatable_output)
        !          1855:     {
        !          1856:       set_sect_size =
        !          1857:        (set_symbol_count + set_vector_count) * sizeof (unsigned long);
        !          1858:       set_sect_start = text_start + text_size; 
        !          1859:       text_size += set_sect_size;
        !          1860:       set_vectors = (unsigned long *) xmalloc (set_sect_size);
        !          1861:       setv_fill_count = 0;
        !          1862:     }
1.1       root     1863:   /* If necessary, pad text section to full page in the file.
                   1864:      Include the padding in the text segment size.  */
                   1865: 
                   1866:   if (magic == NMAGIC || magic == ZMAGIC)
                   1867:     {
                   1868:       int text_end = text_size + N_TXTOFF (outheader);
                   1869:       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
                   1870:       text_size += text_pad;
                   1871:     }
                   1872: 
                   1873:   outheader.a_text = text_size;
                   1874: 
                   1875:   /* Make the data segment address start in memory on a suitable boundary.  */
                   1876: 
                   1877:   if (! Tdata_flag_specified)
                   1878:     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
                   1879: 
                   1880:   /* Compute start addresses of each file's sections and symbols.  */
                   1881: 
                   1882:   each_full_file (relocate_file_addresses, 0);
                   1883: 
                   1884:   /* Now, for each symbol, verify that it is defined globally at most once.
                   1885:      Put the global value into the symbol entry.
                   1886:      Common symbols are allocated here, in the BSS section.
                   1887:      Each defined symbol is given a '->defined' field
                   1888:       which is the correct N_ code for its definition,
                   1889:       except in the case of common symbols with -r.
                   1890:      Then make all the references point at the symbol entry
                   1891:      instead of being chained together. */
                   1892: 
                   1893:   defined_global_sym_count = 0;
                   1894: 
                   1895:   for (i = 0; i < TABSIZE; i++)
                   1896:     {
                   1897:       register symbol *sp;
                   1898:       for (sp = symtab[i]; sp; sp = sp->link)
                   1899:        {
                   1900:          register struct nlist *p, *next;
                   1901:          int defs = 0, com = sp->max_common_size, erred = 0;
1.1.1.2 ! root     1902:          struct nlist *first_definition;
1.1       root     1903:          for (p = sp->refs; p; p = next)
                   1904:            {
                   1905:              register int type = p->n_type;
1.1.1.2 ! root     1906: 
        !          1907: #if 0
        !          1908:              if ((type & ~N_EXT) == N_SETV) continue;
        !          1909: #endif
        !          1910:              if (SET_ELEMENT_P (type))
        !          1911:                {
        !          1912:                  if (relocatable_output)
        !          1913:                    fatal ("internal: global ref to set element with -r");
        !          1914:                  if (!defs++)
        !          1915:                    {
        !          1916:                      sp->value = set_sect_start
        !          1917:                        + setv_fill_count++ * sizeof (unsigned long);
        !          1918:                      sp->defined = N_SETV | N_EXT;
        !          1919:                      first_definition = p;
        !          1920:                    }
        !          1921:                  else if ((sp->defined & ~N_EXT) != N_SETV)
        !          1922:                    {
        !          1923:                      make_executable = 0;
        !          1924:                      error ("text symbol %s redifined as set vector.  Files:",
        !          1925:                             sp->name);
        !          1926:                      print_files_defining_symbol (first_definition, p);
        !          1927:                    }
        !          1928:                  set_vectors[setv_fill_count++] = p->n_value;
        !          1929:                }
        !          1930:              else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
1.1       root     1931:                {
                   1932:                  /* non-common definition */
                   1933:                  if (defs++ && sp->value != p->n_value)
                   1934:                    if (!erred++)
                   1935:                      {
                   1936:                        make_executable = 0;
1.1.1.2 ! root     1937:                        error ("multiple definitions of symbol %s. Files:",
        !          1938:                               sp->name);
        !          1939:                        print_files_defining_symbol (first_definition, p);
1.1       root     1940:                      }
                   1941:                  sp->value = p->n_value;
                   1942:                  sp->defined = type;
1.1.1.2 ! root     1943:                  first_definition = p;
1.1       root     1944:                }
                   1945:              next = (struct nlist *) p->n_un.n_name;
                   1946:              p->n_un.n_name = (char *) sp;
                   1947:            }
                   1948:          /* Allocate as common if defined as common and not defined for real */
                   1949:          if (com && !defs)
                   1950:            {
                   1951:              if (!relocatable_output || force_common_definition)
                   1952:                {
                   1953:                  com = (com + sizeof (int) - 1) & (- sizeof (int));
                   1954:     
                   1955:                  sp->value = data_start + data_size + bss_size;
                   1956:                  sp->defined = N_BSS | N_EXT;
                   1957:                  bss_size += com;
                   1958:                  if (write_map)
                   1959:                    printf ("Allocating common %s: %x at %x\n",
                   1960:                            sp->name, com, sp->value);
                   1961:                }
1.1.1.2 ! root     1962:              else
        !          1963:                {
        !          1964:                  sp->defined = 0;
        !          1965:                  undefined_global_sym_count++;
        !          1966:                }
1.1       root     1967:            }
1.1.1.2 ! root     1968:          /* Set length word at front of vector */
        !          1969:          if ((sp->defined & ~N_EXT) == N_SETV)
        !          1970:            {
        !          1971:              unsigned long length_word_index =
        !          1972:                (sp->value - set_sect_start) / sizeof (unsigned long);
        !          1973:              
        !          1974:              set_vectors[length_word_index] = setv_fill_count - 1 - length_word_index;
        !          1975:            }         
1.1       root     1976:          if (sp->defined)
                   1977:            defined_global_sym_count++;
                   1978:        }
                   1979:     }
                   1980: 
                   1981:   if (end_symbol)              /* These are null if -r.  */
                   1982:     {
                   1983:       etext_symbol->value = text_size + text_start;
                   1984:       edata_symbol->value = data_start + data_size;
                   1985:       end_symbol->value = data_start + data_size + bss_size;
                   1986:     }
                   1987: 
                   1988:   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
                   1989: 
                   1990:   if (specified_data_size && specified_data_size > data_size)
                   1991:     data_pad = specified_data_size - data_size;
                   1992: 
                   1993:   if (magic == ZMAGIC)
                   1994:     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
                   1995:                - data_size;
                   1996: 
                   1997:   bss_size -= data_pad;
                   1998:   if (bss_size < 0) bss_size = 0;
                   1999: 
                   2000:   data_size += data_pad;
                   2001: }
                   2002: 
                   2003: /* Accumulate the section sizes of input file ENTRY
                   2004:    into the section sizes of the output file.  */
                   2005: 
                   2006: void
                   2007: consider_file_section_lengths (entry)
                   2008:      register struct file_entry *entry;
                   2009: {
                   2010:   if (entry->just_syms_flag)
                   2011:     return;
                   2012: 
                   2013:   entry->text_start_address = text_size;
1.1.1.2 ! root     2014:   /* If there were any vectors, we need to chop them off */
        !          2015: #if 0
        !          2016:   /* There should never be set vectors in input to the loader */
        !          2017:   if (entry->set_vector_offset == -1)
        !          2018: #endif
1.1       root     2019:   text_size += entry->header.a_text;
1.1.1.2 ! root     2020: #if 0
        !          2021:   else
        !          2022:     text_size += entry->set_vector_offset - N_TXTOFF (entry->header);
        !          2023: #endif
1.1       root     2024:   entry->data_start_address = data_size;
                   2025:   data_size += entry->header.a_data;
                   2026:   entry->bss_start_address = bss_size;
                   2027:   bss_size += entry->header.a_bss;
                   2028: 
                   2029:   text_reloc_size += entry->header.a_trsize;
                   2030:   data_reloc_size += entry->header.a_drsize;
                   2031: }
                   2032: 
                   2033: /* Determine where the sections of ENTRY go into the output file,
                   2034:    whose total section sizes are already known.
                   2035:    Also relocate the addresses of the file's local and debugger symbols.  */
                   2036: 
                   2037: void
                   2038: relocate_file_addresses (entry)
                   2039:      register struct file_entry *entry;
                   2040: {
                   2041:   entry->text_start_address += text_start;
                   2042:   /* Note that `data_start' and `data_size' have not yet been
                   2043:      adjusted for `data_pad'.  If they had been, we would get the wrong
                   2044:      results here.  */
                   2045:   entry->data_start_address += data_start;
                   2046:   entry->bss_start_address += data_start + data_size;
                   2047: 
                   2048:   {
                   2049:     register struct nlist *p;
                   2050:     register struct nlist *end
                   2051:       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
                   2052: 
                   2053:     for (p = entry->symbols; p < end; p++)
                   2054:       {
                   2055:        /* If this belongs to a section, update it by the section's start address */
                   2056:        register int type = p->n_type & N_TYPE;
                   2057: 
1.1.1.2 ! root     2058:        switch (type) {
        !          2059:        case N_TEXT:
        !          2060:        case N_SETV:
        !          2061:        case N_SETT:
1.1       root     2062:          p->n_value += entry->text_start_address;
1.1.1.2 ! root     2063:          break;
        !          2064:        case N_DATA:
        !          2065:        case N_SETD:
1.1       root     2066:          /* A symbol whose value is in the data section
                   2067:             is present in the input file as if the data section
                   2068:             started at an address equal to the length of the file's text.  */
                   2069:          p->n_value += entry->data_start_address - entry->header.a_text;
1.1.1.2 ! root     2070:          break;
        !          2071:        case N_BSS:
        !          2072:        case N_SETB:
1.1       root     2073:          /* likewise for symbols with value in BSS.  */
                   2074:          p->n_value += entry->bss_start_address
                   2075:                         - entry->header.a_text - entry->header.a_data;
1.1.1.2 ! root     2076:          break;
        !          2077:        }
1.1       root     2078:       }
                   2079:   }
                   2080: }
                   2081: 
                   2082: void describe_file_sections (), list_file_locals ();
                   2083: 
                   2084: /* Print a complete or partial map of the output file.  */
                   2085: 
                   2086: void
                   2087: print_symbols (outfile)
                   2088:      FILE *outfile;
                   2089: {
                   2090:   register int i;
                   2091: 
                   2092:   fprintf (outfile, "\nFiles:\n\n");
                   2093: 
                   2094:   each_file (describe_file_sections, outfile);
                   2095: 
                   2096:   fprintf (outfile, "\nGlobal symbols:\n\n");
                   2097: 
                   2098:   for (i = 0; i < TABSIZE; i++)
                   2099:     {
                   2100:       register symbol *sp;
                   2101:       for (sp = symtab[i]; sp; sp = sp->link)
                   2102:        {
                   2103:          if (sp->defined == 1)
                   2104:            fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
                   2105:          if (sp->defined)
                   2106:            fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
                   2107:          else if (sp->referenced)
                   2108:            fprintf (outfile, "  %s: undefined\n", sp->name);
                   2109:        }
                   2110:     }
                   2111: 
                   2112:   each_file (list_file_locals, outfile);
                   2113: }
                   2114: 
                   2115: void
                   2116: describe_file_sections (entry, outfile)
                   2117:      struct file_entry *entry;
                   2118:      FILE *outfile;
                   2119: {
                   2120:   fprintf (outfile, "  ");
                   2121:   print_file_name (entry, outfile);
                   2122:   if (entry->just_syms_flag)
                   2123:     fprintf (outfile, " symbols only\n", 0);
                   2124:   else
                   2125:     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
                   2126:             entry->text_start_address, entry->header.a_text,
                   2127:             entry->data_start_address, entry->header.a_data,
                   2128:             entry->bss_start_address, entry->header.a_bss);
                   2129: }
                   2130: 
                   2131: void
                   2132: list_file_locals (entry, outfile)
                   2133:      struct file_entry *entry;
                   2134:      FILE *outfile;
                   2135: {
                   2136:   register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
                   2137: 
                   2138:   entry->strings = (char *) alloca (entry->string_size);
                   2139:   read_entry_strings (file_open (entry), entry);
                   2140: 
                   2141:   fprintf (outfile, "\nLocal symbols of ");
                   2142:   print_file_name (entry, outfile);
                   2143:   fprintf (outfile, ":\n\n");
                   2144: 
                   2145:   for (p = entry->symbols; p < end; p++)
                   2146:     /* If this is a definition, 
                   2147:        update it if necessary by this file's start address.  */
                   2148:     if (!(p->n_type & (N_STAB | N_EXT)))
                   2149:       fprintf (outfile, "  %s: 0x%x\n",
                   2150:               entry->strings + p->n_un.n_strx, p->n_value);
                   2151: }
                   2152: 
1.1.1.2 ! root     2153: /* Print on OUTFILE a list of all of the references to undefined
        !          2154:    global symbols.  Assume that there will not be any if
        !          2155:    undefined_global_sym_count is null.  If it isn't we have an error,
        !          2156:    so clear `make_executable'. */
1.1       root     2157: void
                   2158: list_undefined_symbols (outfile)
                   2159:      FILE *outfile;
                   2160: {
1.1.1.2 ! root     2161:   if (undefined_global_sym_count) {
        !          2162:     each_full_file (list_unresolved_references, outfile);
        !          2163:     fprintf (outfile, "\n");
        !          2164:     make_executable = 0;
        !          2165:   }
        !          2166: }
1.1       root     2167: 
1.1.1.2 ! root     2168: /*
        !          2169:  * Structure for communication between list_unresolved refs and it's
        !          2170:  * helper routines.  Will in practice be an array of three of these:
        !          2171:  * 0) Current line, 1) Next line, 2) Source file info.
        !          2172:  */
        !          2173: struct line_debug_entry {
        !          2174:   int line;
        !          2175:   char *filename;
        !          2176:   struct nlist *sym;
        !          2177: };
        !          2178: 
        !          2179: void qsort ();
        !          2180: 
        !          2181: struct line_debug_entry *init_debug_scan ();
        !          2182: int next_debug_entry ();
        !          2183: int address_to_line ();
        !          2184: int relocation_entries_relation ();
        !          2185: 
        !          2186: /* Print on OUTFILE a list of all references to undefined global
        !          2187:    symbols in the file ENTRY.  List source file and line number if
        !          2188:    possible, just the .o file if not. */
        !          2189: 
        !          2190: void
        !          2191: list_unresolved_references (entry, outfile)
        !          2192:      struct file_entry *entry;
        !          2193:      FILE *outfile;
        !          2194: {
        !          2195:   struct relocation_info *txt_reloc, *data_reloc;
        !          2196:   struct line_debug_entry *state_pointer;
        !          2197:   register struct line_debug_entry *current, *next, *source;
        !          2198: 
        !          2199:   /* Read in the files strings if they aren't available */
        !          2200:   if (!entry->strings) {
        !          2201:     int desc;
        !          2202:     
        !          2203:     entry->strings = (char *) alloca (entry->string_size);
        !          2204:     desc = file_open (entry);
        !          2205:     read_entry_strings (desc, entry);
        !          2206:   }
        !          2207: 
        !          2208:   state_pointer = init_debug_scan (0, entry);
        !          2209:   current = state_pointer;
        !          2210:   next = state_pointer + 1;
        !          2211:   source = state_pointer + 2;
        !          2212:   
        !          2213:   read_file_relocation (entry);
        !          2214: 
        !          2215:   /* We need to sort the relocation info here.  Sheesh, so much effort
        !          2216:      for one lousy error optimization. */
        !          2217: 
        !          2218:   qsort (entry->textrel,
        !          2219:         entry->header.a_trsize/sizeof (struct relocation_info),
        !          2220:         sizeof (struct relocation_info),
        !          2221:         relocation_entries_relation);
        !          2222: 
        !          2223:   for (txt_reloc = entry->textrel;
        !          2224:        txt_reloc < (entry->textrel
        !          2225:                    + entry->header.a_trsize/sizeof (struct relocation_info));
        !          2226:        txt_reloc++)
1.1       root     2227:     {
1.1.1.2 ! root     2228:       register struct nlist *s;
        !          2229:       register symbol *g;
        !          2230:       
        !          2231:       if (!txt_reloc->r_extern) continue;
        !          2232:       s = &(entry->symbols[txt_reloc->r_symbolnum]);
        !          2233:       g = (symbol *) s->n_un.n_name;
        !          2234:       if (s->n_type & N_EXT && !g->defined)
1.1       root     2235:        {
1.1.1.2 ! root     2236:          if (g->undef_refs < MAX_UREFS_PRINTED)
1.1       root     2237:            {
1.1.1.2 ! root     2238:              address_to_line (txt_reloc->r_address + entry->text_start_address,
        !          2239:                               state_pointer);
        !          2240:              g->undef_refs++;
        !          2241:              if (g->undef_refs == MAX_UREFS_PRINTED)
        !          2242:                {
        !          2243:                  if (current->line >=0)
        !          2244:                    fprintf (outfile, "%s:0:More undefined symbol %s refs follow\n",
        !          2245:                             current->filename, g->name);
        !          2246:                  else
        !          2247:                    fprintf (outfile, "%s: More undefined symbol %s refs follow\n",
        !          2248:                             current->filename, g->name);
        !          2249:                }
        !          2250:              else
        !          2251:                {
        !          2252:                  if (current->line >= 0)
        !          2253:                    fprintf (outfile, "%s:%d: Undefined symbol %s referenced from text\n",
        !          2254:                             current->filename, current->line, g->name);
        !          2255:                  else
        !          2256:                    fprintf (outfile, "%s: Undefined symbol %s referenced from text\n",
        !          2257:                             current->filename, g->name);
        !          2258:                }
1.1       root     2259:            }
                   2260:        }
                   2261:     }
1.1.1.2 ! root     2262:       
        !          2263:   free (state_pointer);
        !          2264:   state_pointer = init_debug_scan (1, entry);
        !          2265:   current = state_pointer;
        !          2266:   next = state_pointer + 1;
        !          2267:   source = state_pointer + 2;
        !          2268: 
        !          2269:   /*
        !          2270:    * Once again, time to sort the relocation info.
        !          2271:    */
        !          2272: 
        !          2273:   qsort (entry->datarel,
        !          2274:         entry->header.a_drsize/sizeof (struct relocation_info),
        !          2275:         sizeof (struct relocation_info),
        !          2276:         relocation_entries_relation);
        !          2277: 
        !          2278:   for (data_reloc = entry->datarel;
        !          2279:        data_reloc < (entry->datarel
        !          2280:                     + entry->header.a_drsize/sizeof (struct relocation_info));
        !          2281:        data_reloc++)
        !          2282:     {
        !          2283:       register struct nlist *s;
        !          2284:       register symbol *g;
        !          2285:       
        !          2286:       if (!data_reloc->r_extern) continue;
        !          2287:       s = &(entry->symbols[data_reloc->r_symbolnum]);
        !          2288:       g = (symbol *) s->n_un.n_name;
        !          2289:       if (s->n_type & N_EXT && !g->defined)
        !          2290:        {
        !          2291:          address_to_line (data_reloc->r_address + entry->data_start_address,
        !          2292:                           state_pointer);
        !          2293:          if (current->line >= 0)
        !          2294:            fprintf (outfile, "%s:%d: Undefined symbol %s referenced from data\n",
        !          2295:                     current->filename, current->line, g->name);
        !          2296:          else
        !          2297:            fprintf (outfile, "%s: Undefined symbol %s referenced from data\n",
        !          2298:                     current->filename, g->name);
        !          2299:        }
        !          2300:     }
        !          2301: }
        !          2302: /*
        !          2303:  * Helper routines for list_unresolved references.
        !          2304:  */
1.1       root     2305: 
1.1.1.2 ! root     2306: /*
        !          2307:  * Return an integer less than, equal to, or greater than 0 as per the
        !          2308:  * relation between the two relocation entries.
        !          2309:  * Used by qsort.
        !          2310:  */
        !          2311: int
        !          2312: relocation_entries_relation (rel1, rel2)
        !          2313:      struct relocation_info *rel1, *rel2;
        !          2314: {
        !          2315:   return rel1->r_address - rel2->r_address;
1.1       root     2316: }
1.1.1.2 ! root     2317: 
        !          2318: /*
        !          2319:  * Takes the values in state_pointer and moves onward to the next
        !          2320:  * debug line symbol.  It assumes that state_pointer[1] is valid; ie
        !          2321:  * that it.sym points into some entry in the symbol table.  If
        !          2322:  * state_pointer[1].sym == 0, this routine should not be called.
        !          2323:  */
        !          2324: 
        !          2325: int
        !          2326: next_debug_entry (use_data_symbols, state_pointer)
        !          2327:      register int use_data_symbols;
        !          2328:      struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
        !          2329: {
        !          2330:   register struct line_debug_entry
        !          2331:     *current = state_pointer,
        !          2332:     *next = state_pointer + 1,
        !          2333:     *source = state_pointer + 2; /* Used to store source file */
        !          2334:   struct file_entry *entry = (struct file_entry *) source->sym;
        !          2335: 
        !          2336:   current->sym = next->sym;
        !          2337:   current->line = next->line;
        !          2338:   current->filename = next->filename;          
        !          2339: 
        !          2340:   while (++(next->sym) < (entry->symbols
        !          2341:                          + entry->header.a_syms/sizeof (struct nlist)))
        !          2342:     {
        !          2343:       switch (next->sym->n_type)               
        !          2344:        {                                               
        !          2345:        case N_SLINE:
        !          2346:          if (use_data_symbols) continue;
        !          2347:          next->line = next->sym->n_desc;
        !          2348:          return 1;
        !          2349:        case N_DSLNE:
        !          2350:          if (!use_data_symbols) continue;
        !          2351:          next->line = next->sym->n_desc;
        !          2352:          return 1;
        !          2353:        case N_EINCL:                           
        !          2354:          next->filename = source->filename;            
        !          2355:          continue;                             
        !          2356:        case N_SO:                              
        !          2357:          source->filename = next->sym->n_un.n_strx + entry->strings;
        !          2358:          source->line++;
        !          2359:        case N_BINCL:                           
        !          2360:        case N_SOL:                             
        !          2361:          next->filename =                      
        !          2362:            next->sym->n_un.n_strx + entry->strings;
        !          2363:        default:                                        
        !          2364:          continue;                             
        !          2365:        }                                               
        !          2366:     }                                          
        !          2367:   next->sym = (struct nlist *) 0;
        !          2368:   return 0;
        !          2369: }
        !          2370:   
        !          2371: int
        !          2372: address_to_line (address, state_pointer)
        !          2373:      unsigned long address;
        !          2374:      struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
        !          2375: {
        !          2376:   struct line_debug_entry
        !          2377:     *current = state_pointer,
        !          2378:     *next = state_pointer + 1;
        !          2379:   
        !          2380:   int use_data_symbols;
        !          2381: 
        !          2382:   if (next->sym)
        !          2383:     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
        !          2384:   
        !          2385:   while (next->sym
        !          2386:         && next->sym->n_value < address
        !          2387:         && next_debug_entry (use_data_symbols, state_pointer))
        !          2388:     ;
        !          2389:   return current->line;
        !          2390: }
        !          2391: 
        !          2392: struct line_debug_entry *
        !          2393: init_debug_scan (use_data_symbols, entry)
        !          2394:      int use_data_symbols;
        !          2395:      struct file_entry *entry;
        !          2396: {
        !          2397:   struct line_debug_entry
        !          2398:     *state_pointer =
        !          2399:       (struct line_debug_entry *) xmalloc (3 * sizeof (struct line_debug_entry));
        !          2400:   register struct line_debug_entry
        !          2401:     *current = state_pointer,
        !          2402:     *next = state_pointer + 1,
        !          2403:     *source = state_pointer + 2; /* Used to store source file */
        !          2404: 
        !          2405:   struct nlist *tmp;
        !          2406: 
        !          2407:   for (tmp = entry->symbols;
        !          2408:        tmp < (entry->symbols
        !          2409:              + entry->header.a_syms/sizeof (struct nlist));
        !          2410:        tmp++)
        !          2411:     if (tmp->n_type == N_SO) break;
        !          2412: 
        !          2413:   if (tmp >= (entry->symbols
        !          2414:              + entry->header.a_syms/sizeof (struct nlist))) {
        !          2415:     /* I believe this translates to "We loose" */
        !          2416:     current->filename = next->filename = entry->filename;
        !          2417:     current->line = next->line = -1;
        !          2418:     current->sym = next->sym = (struct nlist *) 0;
        !          2419:     return state_pointer;
        !          2420:   }
        !          2421: 
        !          2422:   next->line = source->line = 0;
        !          2423:   next->filename = source->filename = (tmp->n_un.n_strx
        !          2424:                      + entry->strings);
        !          2425:   source->sym = (struct nlist *) entry;
        !          2426:   next->sym = tmp;
        !          2427: 
        !          2428:   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
        !          2429: 
        !          2430:   if (!next->sym)              /* No line numbers for this section; */
        !          2431:                                /* setup output results as appropriate */
        !          2432:     {
        !          2433:       if (source->line)
        !          2434:        {
        !          2435:          current->filename = source->filename = entry->filename;
        !          2436:          current->line = -1;   /* Don't print lineno */
        !          2437:        }
        !          2438:       else
        !          2439:        {
        !          2440:          current->filename = source->filename;
        !          2441:          current->line = 0;
        !          2442:        }
        !          2443:       return state_pointer;
        !          2444:     }
        !          2445:        
        !          2446:   
        !          2447:   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
        !          2448: 
        !          2449:   return state_pointer;
        !          2450: }
        !          2451:   
        !          2452: /* Print the files which have the definitions for a given symbol.
        !          2453:    FIRST_DEF is the first nlist entry which defines the symbol, and
        !          2454:    REST_OF_REFS is a chain of nlist entries which may or may not be
        !          2455:    definitions for this symbol, but which are all references for the
        !          2456:    symbol. 
        !          2457: 
        !          2458:    We do the job in this clumsy fashion because of the state our data
        !          2459:    structures are in in the middle of digest_symbols (from which this
        !          2460:    is called). */
        !          2461: 
        !          2462: void
        !          2463: print_files_defining_symbol (first_def, rest_of_refs)
        !          2464:      struct nlist *first_def, *rest_of_refs;
        !          2465: {
        !          2466:   struct file_entry *holder;
        !          2467:   struct nlist *n_ptr;
        !          2468:   
        !          2469:   if (holder =
        !          2470:       (struct file_entry *) check_each_file (contains_symbol, first_def))
        !          2471:     {
        !          2472:       fprintf (stderr, " ");
        !          2473:       prline_file_name (holder, stderr);
        !          2474:     }
        !          2475:   else
        !          2476:     fatal ("internal: file not found containing nlist entry");
        !          2477: 
        !          2478:   for (n_ptr = rest_of_refs;
        !          2479:        n_ptr;
        !          2480:        n_ptr = (struct nlist *) n_ptr->n_un.n_name)
        !          2481:     if (n_ptr->n_type & ~N_EXT)
        !          2482:       if (holder =
        !          2483:          (struct file_entry *) check_each_file (contains_symbol, n_ptr))
        !          2484:        {
        !          2485:          fprintf (stderr, " ");
        !          2486:          prline_file_name (holder, stderr);
        !          2487:        }
        !          2488:       else
        !          2489:        fatal ("internal: file not found containing nlist entry");
        !          2490: }
        !          2491: 
1.1       root     2492: /* Write the output file */ 
                   2493: 
                   2494: void
                   2495: write_output ()
                   2496: {
                   2497:   struct stat statbuf;
                   2498:   int filemode;
                   2499: 
                   2500:   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
                   2501:   if (outdesc < 0) perror_name (output_filename);
                   2502: 
                   2503:   if (fstat (outdesc, &statbuf) < 0)
                   2504:     perror_name (output_filename);
                   2505: 
                   2506:   filemode = statbuf.st_mode;
                   2507: 
                   2508:   chmod (output_filename, filemode & ~0111);
                   2509: 
                   2510:   /* Output the a.out header.  */
                   2511:   write_header ();
                   2512: 
                   2513:   /* Output the text and data segments, relocating as we go.  */
                   2514:   write_text ();
                   2515:   write_data ();
                   2516: 
                   2517:   /* Output the merged relocation info, if requested with `-r'.  */
                   2518:   if (relocatable_output)
                   2519:     write_rel ();
                   2520: 
                   2521:   /* Output the symbol table (both globals and locals).  */
                   2522:   write_syms ();
                   2523: 
                   2524:   /* Copy any GDB symbol segments from input files.  */
                   2525:   write_symsegs ();
                   2526: 
                   2527:   close (outdesc);
                   2528: 
1.1.1.2 ! root     2529:   chmod (output_filename, filemode | 0111);
1.1       root     2530: }
                   2531: 
                   2532: void modify_location (), perform_relocation (), copy_text (), copy_data ();
                   2533: 
                   2534: void
                   2535: write_header ()
                   2536: {
                   2537:   outheader.a_magic = magic;
                   2538: #ifdef INITIALIZE_HEADER
                   2539:   INITIALIZE_HEADER;
                   2540: #endif
                   2541:   outheader.a_text = text_size;
                   2542:   outheader.a_data = data_size;
                   2543:   outheader.a_bss = bss_size;
                   2544:   outheader.a_entry = (entry_symbol ? entry_symbol->value
                   2545:                       : text_start + entry_offset);
                   2546: 
                   2547:   if (strip_symbols == STRIP_ALL)
                   2548:     nsyms = 0;
                   2549:   else
                   2550:     {
1.1.1.2 ! root     2551:       nsyms = (defined_global_sym_count
        !          2552:               + undefined_global_sym_count);
1.1       root     2553:       if (discard_locals == DISCARD_L)
                   2554:        nsyms += non_L_local_sym_count;
                   2555:       else if (discard_locals == DISCARD_NONE)
                   2556:        nsyms += local_sym_count;
1.1.1.2 ! root     2557:       /* One extra for following reference on indirects */
        !          2558:       if (relocatable_output)
        !          2559:        nsyms += set_symbol_count + global_indirect_count;
1.1       root     2560:     }
                   2561: 
                   2562:   if (strip_symbols == STRIP_NONE)
                   2563:     nsyms += debugger_sym_count;
                   2564: 
                   2565:   outheader.a_syms = nsyms * sizeof (struct nlist);
                   2566: 
                   2567:   if (relocatable_output)
                   2568:     {
                   2569:       outheader.a_trsize = text_reloc_size;
                   2570:       outheader.a_drsize = data_reloc_size;
                   2571:     }
                   2572:   else
                   2573:     {
                   2574:       outheader.a_trsize = 0;
                   2575:       outheader.a_drsize = 0;
                   2576:     }
                   2577: 
                   2578:   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
                   2579: 
                   2580:   /* Output whatever padding is required in the executable file
                   2581:      between the header and the start of the text.  */
                   2582: 
                   2583:   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
                   2584: }
                   2585: 
                   2586: /* Relocate the text segment of each input file
                   2587:    and write to the output file.  */
                   2588: 
                   2589: void
                   2590: write_text ()
                   2591: {
                   2592:   if (trace_files)
                   2593:     fprintf (stderr, "Copying and relocating text:\n\n");
                   2594: 
                   2595:   each_full_file (copy_text);
                   2596:   file_close ();
                   2597: 
1.1.1.2 ! root     2598:   /* Write out the set element vectors */
        !          2599:   
        !          2600:   if (set_vector_count)
        !          2601:     mywrite (set_vectors, set_symbol_count + set_vector_count, sizeof (unsigned long), outdesc);
        !          2602: 
1.1       root     2603:   if (trace_files)
                   2604:     fprintf (stderr, "\n");
                   2605: 
                   2606:   padfile (text_pad, outdesc);
                   2607: }
                   2608: 
                   2609: int
                   2610: text_offset (entry)
                   2611:      struct file_entry *entry;
                   2612: {
                   2613:   return entry->starting_offset + N_TXTOFF (entry->header);
                   2614: }
                   2615: 
1.1.1.2 ! root     2616: /* Read in all of the relocation information */
        !          2617: 
        !          2618: void
        !          2619: read_relocation ()
        !          2620: {
        !          2621:   each_full_file (read_file_relocation);
        !          2622: }
        !          2623: 
        !          2624: /* Read in the relocation sections of ENTRY if necessary */
        !          2625: 
        !          2626: void
        !          2627: read_file_relocation (entry)
        !          2628:      struct file_entry *entry;
        !          2629: {
        !          2630:   register struct relocation_info *reloc;
        !          2631:   int desc;
        !          2632:   int read_return;
        !          2633:   
        !          2634:   desc = -1;
        !          2635:   if (!entry->textrel)
        !          2636:     {
        !          2637:       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
        !          2638:       desc = file_open (entry);
        !          2639:       lseek (desc,
        !          2640:             text_offset (entry) + entry->header.a_text + entry->header.a_data,
        !          2641:             L_SET);
        !          2642:       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
        !          2643:        {
        !          2644:          fprintf (stderr, "Return from read: %d\n", read_return);
        !          2645:          fatal_with_file ("premature eof in text relocatino of ", entry);
        !          2646:        }
        !          2647:       entry->textrel = reloc;
        !          2648:     }
        !          2649: 
        !          2650:   if (!entry->datarel) 
        !          2651:     {
        !          2652:       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
        !          2653:       if (desc == -1) desc = file_open (entry);
        !          2654:       lseek (desc,
        !          2655:             text_offset (entry) + entry->header.a_text
        !          2656:             + entry->header.a_data + entry->header.a_trsize,
        !          2657:             L_SET);
        !          2658:       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
        !          2659:        fatal_with_file ("premature eof in text relocatino of ", entry);
        !          2660:       entry->datarel = reloc;
        !          2661:     }
        !          2662: }
        !          2663:       
1.1       root     2664: /* Read the text segment contents of ENTRY, relocate them,
                   2665:    and write the result to the output file.
                   2666:    If `-r', save the text relocation for later reuse.  */
                   2667: 
                   2668: void
                   2669: copy_text (entry)
                   2670:      struct file_entry *entry;
                   2671: {
                   2672:   register char *bytes;
                   2673:   register int desc;
                   2674:   register struct relocation_info *reloc;
                   2675: 
                   2676:   if (trace_files)
                   2677:     prline_file_name (entry, stderr);
                   2678: 
                   2679:   desc = file_open (entry);
                   2680: 
1.1.1.2 ! root     2681:   /* Allocate space for the file's text section */
1.1       root     2682: 
                   2683:   bytes = (char *) alloca (entry->header.a_text);
                   2684: 
1.1.1.2 ! root     2685:   /* Deal with relocation information however is appropriate */
        !          2686:   
        !          2687:   if (entry->textrel)  reloc = entry->textrel;
        !          2688:   else if (relocatable_output)
        !          2689:     {
        !          2690:       read_file_relocation (entry);
        !          2691:       reloc = entry->textrel;
        !          2692:     }
1.1       root     2693:   else
1.1.1.2 ! root     2694:     {
        !          2695:       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
        !          2696:       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
        !          2697:       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
        !          2698:        fatal_with_file ("premature eof in text relocation of ", entry);
        !          2699:     }
1.1       root     2700: 
1.1.1.2 ! root     2701:   /* Read the text section into core.  */
1.1       root     2702: 
                   2703:   lseek (desc, text_offset (entry), 0);
                   2704:   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
                   2705:     fatal_with_file ("premature eof in text section of ", entry);
                   2706: 
                   2707: 
                   2708:   /* Relocate the text according to the text relocation.  */
                   2709: 
                   2710:   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
                   2711:                      reloc, entry->header.a_trsize, entry);
                   2712: 
                   2713:   /* Write the relocated text to the output file.  */
                   2714: 
                   2715:   mywrite (bytes, 1, entry->header.a_text, outdesc);
                   2716: }
                   2717: 
                   2718: /* Relocate the data segment of each input file
                   2719:    and write to the output file.  */
                   2720: 
                   2721: void
                   2722: write_data ()
                   2723: {
                   2724:   if (trace_files)
                   2725:     fprintf (stderr, "Copying and relocating data:\n\n");
                   2726: 
                   2727:   each_full_file (copy_data);
                   2728:   file_close ();
                   2729: 
                   2730:   if (trace_files)
                   2731:     fprintf (stderr, "\n");
                   2732: 
                   2733:   padfile (data_pad, outdesc);
                   2734: }
                   2735: 
                   2736: /* Read the data segment contents of ENTRY, relocate them,
                   2737:    and write the result to the output file.
                   2738:    If `-r', save the data relocation for later reuse.
                   2739:    See comments in `copy_text'.  */
                   2740: 
                   2741: void
                   2742: copy_data (entry)
                   2743:      struct file_entry *entry;
                   2744: {
                   2745:   register struct relocation_info *reloc;
                   2746:   register char *bytes;
                   2747:   register int desc;
                   2748: 
                   2749:   if (trace_files)
                   2750:     prline_file_name (entry, stderr);
                   2751: 
                   2752:   desc = file_open (entry);
                   2753: 
                   2754:   bytes = (char *) alloca (entry->header.a_data);
1.1.1.2 ! root     2755:   
        !          2756:   if (entry->datarel) reloc = entry->datarel;
        !          2757:   else if (relocatable_output) /* Will need this again */
        !          2758:     {
        !          2759:       read_file_relocation (entry);
        !          2760:       reloc = entry->datarel;
        !          2761:     }
1.1       root     2762:   else
1.1.1.2 ! root     2763:     {
        !          2764:       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
        !          2765:       lseek (desc, text_offset (entry) + entry->header.a_text
        !          2766:             + entry->header.a_data + entry->header.a_trsize,
        !          2767:             0);
        !          2768:       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
        !          2769:        fatal_with_file ("premature eof in data relocation of ", entry);
        !          2770:     }
        !          2771:   
1.1       root     2772:   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
                   2773:   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
                   2774:     fatal_with_file ("premature eof in data section of ", entry);
                   2775: 
                   2776:   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
                   2777:                      entry->header.a_data, reloc, entry->header.a_drsize, entry);
                   2778: 
                   2779:   mywrite (bytes, 1, entry->header.a_data, outdesc);
                   2780: }
                   2781: 
                   2782: /* Relocate ENTRY's text or data section contents.
                   2783:    DATA is the address of the contents, in core.
                   2784:    DATA_SIZE is the length of the contents.
                   2785:    PC_RELOCATION is the difference between the address of the contents
                   2786:      in the output file and its address in the input file.
                   2787:    RELOC_INFO is the address of the relocation info, in core.
                   2788:    RELOC_SIZE is its length in bytes.  */
                   2789: 
                   2790: void
                   2791: perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
                   2792:      char *data;
                   2793:      struct relocation_info *reloc_info;
                   2794:      struct file_entry *entry;
                   2795:      int pc_relocation;
                   2796:      int data_size;
                   2797:      int reloc_size;
                   2798: {
                   2799:   register struct relocation_info *p = reloc_info;
                   2800:   struct relocation_info *end
                   2801:     = reloc_info + reloc_size / sizeof (struct relocation_info);
                   2802:   int text_relocation = entry->text_start_address;
                   2803:   int data_relocation = entry->data_start_address - entry->header.a_text;
                   2804:   int bss_relocation
                   2805:     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
                   2806: 
                   2807:   for (; p < end; p++)
                   2808:     {
                   2809:       register int relocation = 0;
                   2810:       register int addr = p->r_address;
                   2811:       register int symbolnum = p->r_symbolnum;
                   2812:       register int length = p->r_length;
                   2813: 
                   2814:       if (addr >= data_size)
                   2815:        fatal_with_file ("relocation address out of range in ", entry);
                   2816:       if (p->r_extern)
                   2817:        {
                   2818:          int symindex = symbolnum * sizeof (struct nlist);
                   2819:          symbol *sp = ((symbol *)
                   2820:                        (((struct nlist *)
                   2821:                          (((char *)entry->symbols) + symindex))
                   2822:                         ->n_un.n_name));
                   2823: 
1.1.1.2 ! root     2824:          /* Resolve indirection */
        !          2825:          if ((sp->defined & ~N_EXT) == N_INDR)
        !          2826:            sp = (symbol *) sp->value;
        !          2827: 
1.1       root     2828:          if (symindex >= entry->header.a_syms)
                   2829:            fatal_with_file ("relocation symbolnum out of range in ", entry);
                   2830: 
                   2831:          /* If the symbol is undefined, leave it at zero.  */
                   2832:          if (! sp->defined)
                   2833:            relocation = 0;
                   2834:          else
                   2835:            relocation = sp->value;
                   2836:        }
                   2837:       else switch (symbolnum)
                   2838:        {
                   2839:        case N_TEXT:
                   2840:        case N_TEXT | N_EXT:
                   2841:          relocation = text_relocation;
                   2842:          break;
                   2843: 
                   2844:        case N_DATA:
                   2845:        case N_DATA | N_EXT:
                   2846:          /* A word that points to beginning of the the data section
                   2847:             initially contains not 0 but rather the "address" of that section
                   2848:             in the input file, which is the length of the file's text.  */
                   2849:          relocation = data_relocation;
                   2850:          break;
                   2851: 
                   2852:        case N_BSS:
                   2853:        case N_BSS | N_EXT:
                   2854:          /* Similarly, an input word pointing to the beginning of the bss
                   2855:             initially contains the length of text plus data of the file.  */
                   2856:          relocation = bss_relocation;
                   2857:          break;
                   2858: 
                   2859:        case N_ABS:
                   2860:        case N_ABS | N_EXT:
                   2861:          /* Don't know why this code would occur, but apparently it does.  */
                   2862:          break;
                   2863: 
                   2864:        default:
                   2865:          fatal_with_file ("nonexternal relocation code invalid in ", entry);
                   2866:        }
                   2867: 
                   2868:       if (p->r_pcrel)
                   2869:        relocation -= pc_relocation;
                   2870: 
                   2871:       switch (length)
                   2872:        {
                   2873:        case 0:
                   2874:          *(char *) (data + addr) += relocation;
                   2875:          break;
1.1.1.2 ! root     2876:          
1.1       root     2877:        case 1:
                   2878:          *(short *) (data + addr) += relocation;
                   2879:          break;
1.1.1.2 ! root     2880:          
1.1       root     2881:        case 2:
                   2882:          *(int *) (data + addr) += relocation;
                   2883:          break;
1.1.1.2 ! root     2884:          
1.1       root     2885:        default:
                   2886:          fatal_with_file ("invalid relocation field length in ", entry);
                   2887:        }
                   2888:     }
                   2889: }
                   2890: 
                   2891: /* For relocatable_output only: write out the relocation,
                   2892:    relocating the addresses-to-be-relocated.  */
                   2893: 
                   2894: void coptxtrel (), copdatrel ();
                   2895: 
                   2896: void
                   2897: write_rel ()
                   2898: {
                   2899:   register int i;
                   2900:   register int count = 0;
                   2901: 
                   2902:   if (trace_files)
                   2903:     fprintf (stderr, "Writing text relocation:\n\n");
                   2904: 
                   2905:   /* Assign each global symbol a sequence number, giving the order
                   2906:      in which `write_syms' will write it.
                   2907:      This is so we can store the proper symbolnum fields
                   2908:      in relocation entries we write.  */
                   2909: 
                   2910:   for (i = 0; i < TABSIZE; i++)
                   2911:     {
                   2912:       symbol *sp;
                   2913:       for (sp = symtab[i]; sp; sp = sp->link)
                   2914:        if (sp->referenced || sp->defined)
                   2915:          sp->def_count = count++;
                   2916:     }
                   2917:   if (count != defined_global_sym_count + undefined_global_sym_count)
                   2918:     fatal ("internal error");
                   2919: 
                   2920:   /* Write out the relocations of all files, remembered from copy_text.  */
                   2921: 
                   2922:   each_full_file (coptxtrel);
                   2923: 
                   2924:   if (trace_files)
                   2925:     fprintf (stderr, "\nWriting data relocation:\n\n");
                   2926: 
                   2927:   each_full_file (copdatrel);
                   2928: 
                   2929:   if (trace_files)
                   2930:     fprintf (stderr, "\n");
                   2931: }
                   2932: 
                   2933: void
                   2934: coptxtrel (entry)
                   2935:      struct file_entry *entry;
                   2936: {
                   2937:   register struct relocation_info *p, *end;
                   2938:   register int reloc = entry->text_start_address;
                   2939: 
                   2940:   p = entry->textrel;
                   2941:   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
                   2942:   while (p < end)
                   2943:     {
                   2944:       p->r_address += reloc;
                   2945:       if (p->r_extern)
                   2946:        {
                   2947:          register int symindex = p->r_symbolnum * sizeof (struct nlist);
                   2948:          symbol *symptr = ((symbol *)
                   2949:                            (((struct nlist *)
                   2950:                              (((char *)entry->symbols) + symindex))
                   2951:                             ->n_un.n_name));
                   2952: 
                   2953:          if (symindex >= entry->header.a_syms)
                   2954:            fatal_with_file ("relocation symbolnum out of range in ", entry);
1.1.1.2 ! root     2955: 
        !          2956: #if 0
        !          2957:          /* No longer necessary; not creating N_SETV symbols until we */
        !          2958:          /* *aren't* doing relocatable output */
        !          2959:          /* If we are using relocation with a symbol of type set
        !          2960:             vector, mark it to be assigned to rather than accessed
        !          2961:             relative.  This means that as the symbol goes through
        !          2962:             many changes of value (which it can), we don't have to
        !          2963:             worry about our calculations getting messed up by old values.  */
        !          2964: 
        !          2965:          if ((symptr->defined & ~N_EXT) == N_SETV) p->r_assign = 1;
        !          2966: #endif
        !          2967: 
1.1       root     2968:          /* If the symbol is now defined, change the external relocation
                   2969:             to an internal one.  */
1.1.1.2 ! root     2970: 
        !          2971:          if (symptr->defined
        !          2972: #if 0
        !          2973:              && !(p->r_assign)
        !          2974: #endif
        !          2975:              )
1.1       root     2976:            {
                   2977:              p->r_extern = 0;
                   2978:              p->r_symbolnum = (symptr->defined & N_TYPE);
                   2979:            }
                   2980:          else
1.1.1.2 ! root     2981:              p->r_symbolnum = (symptr->def_count + nsyms
        !          2982:                                - defined_global_sym_count
        !          2983:                                - undefined_global_sym_count);
1.1       root     2984:        }
                   2985:       p++;
                   2986:     }
                   2987:   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
                   2988: }
                   2989: 
                   2990: void
                   2991: copdatrel (entry)
                   2992:      struct file_entry *entry;
                   2993: {
                   2994:   register struct relocation_info *p, *end;
                   2995:   /* Relocate the address of the relocation.
                   2996:      Old address is relative to start of the input file's data section.
                   2997:      New address is relative to start of the output file's data section.  */
                   2998:   register int reloc = entry->data_start_address - text_size;
                   2999: 
                   3000:   p = entry->datarel;
                   3001:   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
                   3002:   while (p < end)
                   3003:     {
                   3004:       p->r_address += reloc;
                   3005:       if (p->r_extern)
                   3006:        {
                   3007:          register int symindex = p->r_symbolnum * sizeof (struct nlist);
                   3008:          symbol *symptr = ((symbol *)
                   3009:                            (((struct nlist *)
                   3010:                              (((char *)entry->symbols) + symindex))
                   3011:                             ->n_un.n_name));
                   3012:          int symtype = symptr->defined & N_TYPE;
                   3013: 
                   3014:          if (symindex >= entry->header.a_syms)
                   3015:            fatal_with_file ("relocation symbolnum out of range in ", entry);
                   3016:          if (force_common_definition
                   3017:              || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
                   3018:            {
                   3019:              p->r_extern = 0;
                   3020:              p->r_symbolnum = symtype;
                   3021:            }
                   3022:          else
                   3023:            p->r_symbolnum
                   3024:              = (((symbol *)
                   3025:                  (((struct nlist *)
                   3026:                    (((char *)entry->symbols) + symindex))
                   3027:                   ->n_un.n_name))
                   3028:                 ->def_count
                   3029:                 + nsyms - defined_global_sym_count
                   3030:                 - undefined_global_sym_count);
                   3031:        }
                   3032:       p++;
                   3033:     }
                   3034:   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
                   3035: }
                   3036: 
                   3037: void write_file_syms ();
                   3038: void write_string_table ();
                   3039: 
                   3040: /* Offsets and current lengths of symbol and string tables in output file. */
                   3041: 
                   3042: int symbol_table_offset;
                   3043: int symbol_table_len;
                   3044: 
                   3045: /* Address in output file where string table starts.  */
                   3046: int string_table_offset;
                   3047: 
                   3048: /* Offset within string table
                   3049:    where the strings in `strtab_vector' should be written.  */
                   3050: int string_table_len;
                   3051: 
                   3052: /* Total size of string table strings allocated so far,
                   3053:    including strings in `strtab_vector'.  */
                   3054: int strtab_size;
                   3055: 
                   3056: /* Vector whose elements are strings to be added to the string table.  */
                   3057: char **strtab_vector;
                   3058: 
                   3059: /* Vector whose elements are the lengths of those strings.  */
                   3060: int *strtab_lens;
                   3061: 
                   3062: /* Index in `strtab_vector' at which the next string will be stored.  */
                   3063: int strtab_index;
                   3064: 
                   3065: /* Add the string NAME to the output file string table.
                   3066:    Record it in `strtab_vector' to be output later.
                   3067:    Return the index within the string table that this string will have.  */
                   3068: 
                   3069: int
                   3070: assign_string_table_index (name)
                   3071:      char *name;
                   3072: {
                   3073:   register int index = strtab_size;
                   3074:   register int len = strlen (name) + 1;
                   3075: 
                   3076:   strtab_size += len;
                   3077:   strtab_vector[strtab_index] = name;
                   3078:   strtab_lens[strtab_index++] = len;
                   3079: 
                   3080:   return index;
                   3081: }
                   3082: 
                   3083: FILE *outstream = (FILE *) 0;
                   3084: 
                   3085: /* Write the contents of `strtab_vector' into the string table.
                   3086:    This is done once for each file's local&debugger symbols
                   3087:    and once for the global symbols.  */
                   3088: 
                   3089: void
                   3090: write_string_table ()
                   3091: {
                   3092:   register int i;
                   3093: 
                   3094:   lseek (outdesc, string_table_offset + string_table_len, 0);
                   3095: 
                   3096:   if (!outstream)
                   3097:     outstream = fdopen (outdesc, "w");
                   3098: 
                   3099:   for (i = 0; i < strtab_index; i++)
                   3100:     {
                   3101:       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
                   3102:       string_table_len += strtab_lens[i];
                   3103:     }
                   3104: 
                   3105:   fflush (outstream);
                   3106: 
                   3107:   /* Report I/O error such as disk full.  */
                   3108:   if (ferror (outstream))
                   3109:     perror_name (output_filename);
                   3110: }
                   3111: 
                   3112: /* Write the symbol table and string table of the output file.  */
                   3113: 
                   3114: void
                   3115: write_syms ()
                   3116: {
                   3117:   /* Number of symbols written so far.  */
                   3118:   int syms_written = 0;
                   3119:   register int i;
                   3120:   register symbol *sp;
                   3121: 
1.1.1.2 ! root     3122:   /* Buffer big enough for all the global symbols.  One
        !          3123:      extra struct for each indirect symbol to hold the extra reference
        !          3124:      following. */
1.1       root     3125:   struct nlist *buf
1.1.1.2 ! root     3126:     = (struct nlist *) alloca ((defined_global_sym_count
        !          3127:                                + undefined_global_sym_count
        !          3128:                                + global_indirect_count)
1.1       root     3129:                               * sizeof (struct nlist));
                   3130:   /* Pointer for storing into BUF.  */
                   3131:   register struct nlist *bufp = buf;
                   3132: 
                   3133:   /* Size of string table includes the bytes that store the size.  */
                   3134:   strtab_size = sizeof strtab_size;
                   3135: 
                   3136:   symbol_table_offset = N_SYMOFF (outheader);
                   3137:   symbol_table_len = 0;
                   3138:   string_table_offset = N_STROFF (outheader);
                   3139:   string_table_len = strtab_size;
                   3140: 
                   3141:   if (strip_symbols == STRIP_ALL)
                   3142:     return;
                   3143: 
                   3144:   /* Write the local symbols defined by the various files.  */
                   3145: 
                   3146:   each_file (write_file_syms, &syms_written);
                   3147:   file_close ();
                   3148: 
                   3149:   /* Now write out the global symbols.  */
                   3150: 
                   3151:   /* Allocate two vectors that record the data to generate the string
1.1.1.2 ! root     3152:      table from the global symbols written so far.  This must include
        !          3153:      extra space for the references following indirect outputs. */
1.1       root     3154: 
1.1.1.2 ! root     3155:   strtab_vector = (char **) alloca ((num_hash_tab_syms
        !          3156:                                     + global_indirect_count) * sizeof (char *));
        !          3157:   strtab_lens = (int *) alloca ((num_hash_tab_syms
        !          3158:                                 + global_indirect_count) * sizeof (int));
1.1       root     3159:   strtab_index = 0;
                   3160: 
                   3161:   /* Scan the symbol hash table, bucket by bucket.  */
                   3162: 
                   3163:   for (i = 0; i < TABSIZE; i++)
                   3164:     for (sp = symtab[i]; sp; sp = sp->link)
                   3165:       {
                   3166:        struct nlist nl;
                   3167: 
                   3168:        nl.n_other = 0;
                   3169:        nl.n_desc = 0;
                   3170: 
                   3171:        /* Compute a `struct nlist' for the symbol.  */
                   3172: 
                   3173:        if (sp->defined || sp->referenced)
                   3174:          {
1.1.1.2 ! root     3175:            /* common condition needs to be before undefined condition */
        !          3176:            /* because unallocated commons are set undefined in */
        !          3177:            /* digest_symbols */
        !          3178:            if (sp->defined > 1) /* defined with known type */
1.1       root     3179:              {
1.1.1.2 ! root     3180:                /* If the target of an indirect symbol has been
        !          3181:                   defined and we are outputting an executable,
        !          3182:                   resolve the indirection; it's no longer needed */
        !          3183:                if (!relocatable_output
        !          3184:                    && ((sp->defined & N_TYPE) == N_INDR)
        !          3185:                    && (((symbol *) sp->value)->defined > 1))
        !          3186:                  {
        !          3187:                    symbol *newsp = (symbol *) sp->value;
        !          3188:                    nl.n_type = newsp->defined;
        !          3189:                    nl.n_value = newsp->value;
        !          3190:                  }
        !          3191:                else
        !          3192:                  {
        !          3193:                    nl.n_type = sp->defined;
        !          3194:                    if (sp->defined != (N_INDR | N_EXT))
        !          3195:                      nl.n_value = sp->value;
        !          3196:                    else
        !          3197:                      nl.n_value = 0;
        !          3198:                  }
1.1       root     3199:              }
                   3200:            else if (sp->max_common_size) /* defined as common but not allocated. */
                   3201:              {
                   3202:                /* happens only with -r and not -d */
                   3203:                /* write out a common definition */
                   3204:                nl.n_type = N_UNDF | N_EXT;
                   3205:                nl.n_value = sp->max_common_size;
                   3206:              }
1.1.1.2 ! root     3207:            else if (!sp->defined)            /* undefined -- legit only if -r */
        !          3208:              {
        !          3209:                nl.n_type = N_UNDF | N_EXT;
        !          3210:                nl.n_value = 0;
        !          3211:              }
        !          3212:            else 
1.1       root     3213:              fatal ("internal error: %s defined in mysterious way", sp->name);
                   3214: 
                   3215:            /* Allocate string table space for the symbol name.  */
                   3216: 
                   3217:            nl.n_un.n_strx = assign_string_table_index (sp->name);
                   3218: 
                   3219:            /* Output to the buffer and count it.  */
                   3220: 
                   3221:            *bufp++ = nl;
                   3222:            syms_written++;
1.1.1.2 ! root     3223:            if (nl.n_type == (N_INDR | N_EXT))
        !          3224:              {
        !          3225:                struct nlist xtra_ref;
        !          3226:                xtra_ref.n_type == N_EXT | N_UNDF;
        !          3227:                xtra_ref.n_un.n_strx =
        !          3228:                  assign_string_table_index (((symbol *) sp->value)->name);
        !          3229:                xtra_ref.n_other = 0;
        !          3230:                xtra_ref.n_desc = 0;
        !          3231:                xtra_ref.n_value = 0;
        !          3232:                *bufp++ = nl;
        !          3233:                syms_written++;
        !          3234:              }
1.1       root     3235:          }
                   3236:       }
                   3237: 
                   3238:   /* Output the buffer full of `struct nlist's.  */
                   3239: 
                   3240:   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
                   3241:   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
                   3242:   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
                   3243: 
                   3244:   if (syms_written != nsyms)
                   3245:     fatal ("internal error: wrong number of symbols written into output file", 0);
                   3246: 
                   3247:   if (symbol_table_offset + symbol_table_len != string_table_offset)
                   3248:     fatal ("internal error: inconsistent symbol table length", 0);
                   3249: 
                   3250:   /* Now the total string table size is known, so write it.
                   3251:      We are already positioned at the right place in the file.  */
                   3252: 
                   3253:   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
                   3254: 
                   3255:   /* Write the strings for the global symbols.  */
                   3256: 
                   3257:   write_string_table ();
                   3258: }
                   3259: 
                   3260: /* Write the local and debugger symbols of file ENTRY.
                   3261:    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
                   3262: 
                   3263: /* Note that we do not combine identical names of local symbols.
                   3264:    dbx or gdb would be confused if we did that.  */
                   3265: 
                   3266: void
                   3267: write_file_syms (entry, syms_written_addr)
                   3268:      struct file_entry *entry;
                   3269:      int *syms_written_addr;
                   3270: {
                   3271:   register struct nlist *p = entry->symbols;
                   3272:   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
                   3273: 
                   3274:   /* Buffer to accumulate all the syms before writing them.
                   3275:      It has one extra slot for the local symbol we generate here.  */
                   3276:   struct nlist *buf
                   3277:     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
                   3278:   register struct nlist *bufp = buf;
                   3279: 
                   3280:   /* Upper bound on number of syms to be written here.  */
                   3281:   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
                   3282: 
                   3283:   /* Make tables that record, for each symbol, its name and its name's length.
                   3284:      The elements are filled in by `assign_string_table_index'.  */
                   3285: 
                   3286:   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
                   3287:   strtab_lens = (int *) alloca (max_syms * sizeof (int));
                   3288:   strtab_index = 0;
                   3289: 
                   3290:   /* Generate a local symbol for the start of this file's text.  */
                   3291: 
                   3292:   if (discard_locals != DISCARD_ALL)
                   3293:     {
                   3294:       struct nlist nl;
                   3295: 
                   3296:       nl.n_type = N_TEXT;
                   3297:       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
                   3298:       nl.n_value = entry->text_start_address;
                   3299:       nl.n_desc = 0;
                   3300:       nl.n_other = 0;
                   3301:       *bufp++ = nl;
                   3302:       (*syms_written_addr)++;
                   3303:       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
                   3304:     }
                   3305: 
                   3306:   /* Read the file's string table.  */
                   3307: 
                   3308:   entry->strings = (char *) alloca (entry->string_size);
                   3309:   read_entry_strings (file_open (entry), entry);
                   3310: 
                   3311:   for (; p < end; p++)
                   3312:     {
                   3313:       register int type = p->n_type;
                   3314:       register int write = 0;
                   3315: 
                   3316:       /* WRITE gets 1 for a non-global symbol that should be written.  */
                   3317: 
1.1.1.2 ! root     3318: 
        !          3319:       if (SET_ELEMENT_P (type))        /* This occurs even if global.  These */
        !          3320:                                /* types of symbols are never written */
        !          3321:                                /* globally, though they are stored */
        !          3322:                                /* globally.  */
        !          3323:         write = relocatable_output;
        !          3324:       else if (!(type & (N_STAB | N_EXT)))
1.1       root     3325:         /* ordinary local symbol */
                   3326:        write = (discard_locals != DISCARD_ALL)
                   3327:                && !(discard_locals == DISCARD_L &&
                   3328:                     (p->n_un.n_strx + entry->strings)[0] == 'L');
                   3329:       else if (!(type & N_EXT))
                   3330:        /* debugger symbol */
                   3331:         write = (strip_symbols == STRIP_NONE);
                   3332: 
                   3333:       if (write)
                   3334:        {
                   3335:          /* If this symbol has a name,
                   3336:             allocate space for it in the output string table.  */
                   3337: 
1.1.1.2 ! root     3338: #if 0                          /* Following no longer true  */
        !          3339:          /* Sigh.  If this is a set element, it has been entered in */
        !          3340:          /* the global symbol table *and* is being output as a */
        !          3341:          /* local.  This means that p->n_un.n_strx has been */
        !          3342:          /* trashed.  */
        !          3343:          if (SET_ELEMENT_P (type))
        !          3344:            p->n_un.n_strx =
        !          3345:              assign_string_table_index (((symbol *) p->n_un.n_name)->name);
        !          3346:          else
        !          3347: #endif
1.1       root     3348:          if (p->n_un.n_strx)
1.1.1.2 ! root     3349:            p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
        !          3350:                                                        + entry->strings);
1.1       root     3351: 
                   3352:          /* Output this symbol to the buffer and count it.  */
                   3353: 
                   3354:          *bufp++ = *p;
                   3355:          (*syms_written_addr)++;
                   3356:        }
                   3357:     }
                   3358: 
                   3359:   /* All the symbols are now in BUF; write them.  */
                   3360: 
                   3361:   lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 
                   3362:   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
                   3363:   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
                   3364: 
                   3365:   /* Write the string-table data for the symbols just written,
                   3366:      using the data in vectors `strtab_vector' and `strtab_lens'.  */
                   3367: 
                   3368:   write_string_table ();
                   3369: }
                   3370: 
                   3371: /* Copy any GDB symbol segments from the input files to the output file.
                   3372:    The contents of the symbol segment is copied without change
                   3373:    except that we store some information into the beginning of it.  */
                   3374: 
                   3375: void write_file_symseg ();
                   3376: 
1.1.1.2 ! root     3377: void
1.1       root     3378: write_symsegs ()
                   3379: {
                   3380:   each_file (write_file_symseg, 0);
                   3381: }
                   3382: 
                   3383: void
                   3384: write_file_symseg (entry)
                   3385:      struct file_entry *entry;
                   3386: {
                   3387:   char buffer[4096];
                   3388:   struct symbol_root root;
                   3389:   int indesc;
                   3390:   int len;
                   3391: 
                   3392:   if (entry->symseg_offset == 0)
                   3393:     return;
                   3394: 
                   3395:   /* This entry has a symbol segment.  Read the root of the segment.  */
                   3396: 
                   3397:   indesc = file_open (entry);
                   3398:   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
                   3399:   if (sizeof root != read (indesc, &root, sizeof root))
                   3400:     fatal_with_file ("premature end of file in symbol segment of ", entry);
                   3401: 
                   3402:   /* Store some relocation info into the root.  */
                   3403: 
                   3404:   root.ldsymoff = entry->local_syms_offset;
                   3405:   root.textrel = entry->text_start_address;
                   3406:   root.datarel = entry->data_start_address - entry->header.a_text;
                   3407:   root.bssrel = entry->bss_start_address
                   3408:     - entry->header.a_text - entry->header.a_data;
                   3409:   root.databeg = entry->data_start_address - root.datarel;
                   3410:   root.bssbeg = entry->bss_start_address - root.bssrel;
                   3411: 
                   3412:   /* Write the modified root into the output file.  */
                   3413: 
                   3414:   mywrite (&root, sizeof root, 1, outdesc);
                   3415: 
                   3416:   /* Copy the rest of the symbol segment unchanged.  */
                   3417: 
                   3418:   if (entry->superfile)
                   3419:     {
                   3420:       /* Library member: number of bytes to copy is determined
                   3421:         from the member's total size.  */
                   3422: 
                   3423:       int total = entry->total_size - entry->symseg_offset - sizeof root;
                   3424: 
                   3425:       while (total > 0)
                   3426:        {
                   3427:          len = read (indesc, buffer, min (sizeof buffer, total));
                   3428: 
                   3429:          if (len != min (sizeof buffer, total))
                   3430:            fatal_with_file ("premature end of file in symbol segment of ", entry);
                   3431:          total -= len;
                   3432:          mywrite (buffer, len, 1, outdesc);
                   3433:        }
                   3434:     }
                   3435:   else
                   3436:     {
                   3437:       /* A separate file: copy until end of file.  */
                   3438: 
                   3439:       while (len = read (indesc, buffer, sizeof buffer))
                   3440:        {
                   3441:          mywrite (buffer, len, 1, outdesc);
                   3442:          if (len < sizeof buffer)
                   3443:            break;
                   3444:        }
                   3445:     }
                   3446: 
                   3447:   file_close ();
                   3448: }
                   3449: 
                   3450: /* Create the symbol table entries for `etext', `edata' and `end'.  */
                   3451: 
1.1.1.2 ! root     3452: void
1.1       root     3453: symtab_init ()
                   3454: {
                   3455: #ifndef nounderscore
                   3456:   edata_symbol = getsym ("_edata");
                   3457:   etext_symbol = getsym ("_etext");
                   3458:   end_symbol = getsym ("_end");
                   3459: #else
                   3460:   edata_symbol = getsym ("edata");
                   3461:   etext_symbol = getsym ("etext");
                   3462:   end_symbol = getsym ("end");
                   3463: #endif
                   3464: 
                   3465:   edata_symbol->defined = N_DATA | N_EXT;
                   3466:   etext_symbol->defined = N_TEXT | N_EXT;
                   3467:   end_symbol->defined = N_BSS | N_EXT;
                   3468: 
                   3469:   edata_symbol->referenced = 1;
                   3470:   etext_symbol->referenced = 1;
                   3471:   end_symbol->referenced = 1;
                   3472: }
                   3473: 
                   3474: /* Compute the hash code for symbol name KEY.  */
                   3475: 
                   3476: int
                   3477: hash_string (key)
                   3478:      char *key;
                   3479: {
                   3480:   register char *cp;
                   3481:   register int k;
                   3482: 
                   3483:   cp = key;
                   3484:   k = 0;
                   3485:   while (*cp)
                   3486:     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
                   3487: 
                   3488:   return k;
                   3489: }
                   3490: 
                   3491: /* Get the symbol table entry for the global symbol named KEY.
                   3492:    Create one if there is none.  */
                   3493: 
                   3494: symbol *
                   3495: getsym (key)
                   3496:      char *key;
                   3497: {
                   3498:   register int hashval;
                   3499:   register symbol *bp;
                   3500: 
                   3501:   /* Determine the proper bucket.  */
                   3502: 
                   3503:   hashval = hash_string (key) % TABSIZE;
                   3504: 
                   3505:   /* Search the bucket.  */
                   3506: 
                   3507:   for (bp = symtab[hashval]; bp; bp = bp->link)
                   3508:     if (! strcmp (key, bp->name))
                   3509:       return bp;
                   3510: 
                   3511:   /* Nothing was found; create a new symbol table entry.  */
                   3512: 
                   3513:   bp = (symbol *) xmalloc (sizeof (symbol));
                   3514:   bp->refs = 0;
                   3515:   bp->name = (char *) xmalloc (strlen (key) + 1);
                   3516:   strcpy (bp->name, key);
                   3517:   bp->defined = 0;
                   3518:   bp->referenced = 0;
                   3519:   bp->trace = 0;
                   3520:   bp->value = 0;
                   3521:   bp->max_common_size = 0;
                   3522: 
                   3523:   /* Add the entry to the bucket.  */
                   3524: 
                   3525:   bp->link = symtab[hashval];
                   3526:   symtab[hashval] = bp;
                   3527: 
                   3528:   ++num_hash_tab_syms;
                   3529: 
                   3530:   return bp;
                   3531: }
                   3532: 
                   3533: /* Like `getsym' but return 0 if the symbol is not already known.  */
                   3534: 
                   3535: symbol *
                   3536: getsym_soft (key)
                   3537:      char *key;
                   3538: {
                   3539:   register int hashval;
                   3540:   register symbol *bp;
                   3541: 
                   3542:   /* Determine which bucket.  */
                   3543: 
                   3544:   hashval = hash_string (key) % TABSIZE;
                   3545: 
                   3546:   /* Search the bucket.  */
                   3547: 
                   3548:   for (bp = symtab[hashval]; bp; bp = bp->link)
                   3549:     if (! strcmp (key, bp->name))
                   3550:       return bp;
                   3551: 
                   3552:   return 0;
                   3553: }
                   3554: 
                   3555: /* Report a fatal error.
                   3556:    STRING is a printf format string and ARG is one arg for it.  */
                   3557: 
1.1.1.2 ! root     3558: void
1.1       root     3559: fatal (string, arg)
                   3560:      char *string, *arg;
                   3561: {
                   3562:   fprintf (stderr, "ld: ");
                   3563:   fprintf (stderr, string, arg);
                   3564:   fprintf (stderr, "\n");
                   3565:   exit (1);
                   3566: }
                   3567: 
                   3568: /* Report a fatal error.  The error message is STRING
                   3569:    followed by the filename of ENTRY.  */
                   3570: 
1.1.1.2 ! root     3571: void
1.1       root     3572: fatal_with_file (string, entry)
                   3573:      char *string;
                   3574:      struct file_entry *entry;
                   3575: {
                   3576:   fprintf (stderr, "ld: ");
                   3577:   fprintf (stderr, string);
                   3578:   print_file_name (entry, stderr);
                   3579:   fprintf (stderr, "\n");
                   3580:   exit (1);
                   3581: }
                   3582: 
                   3583: /* Report a fatal error using the message for the last failed system call,
                   3584:    followed by the string NAME.  */
                   3585: 
1.1.1.2 ! root     3586: void
1.1       root     3587: perror_name (name)
                   3588:      char *name;
                   3589: {
                   3590:   extern int errno, sys_nerr;
                   3591:   extern char *sys_errlist[];
                   3592:   char *s;
                   3593: 
                   3594:   if (errno < sys_nerr)
                   3595:     s = concat ("", sys_errlist[errno], " for %s");
                   3596:   else
                   3597:     s = "cannot open %s";
                   3598:   fatal (s, name);
                   3599: }
                   3600: 
                   3601: /* Report a fatal error using the message for the last failed system call,
                   3602:    followed by the name of file ENTRY.  */
                   3603: 
1.1.1.2 ! root     3604: void
1.1       root     3605: perror_file (entry)
                   3606:      struct file_entry *entry;
                   3607: {
                   3608:   extern int errno, sys_nerr;
                   3609:   extern char *sys_errlist[];
                   3610:   char *s;
                   3611: 
                   3612:   if (errno < sys_nerr)
                   3613:     s = concat ("", sys_errlist[errno], " for ");
                   3614:   else
                   3615:     s = "cannot open ";
                   3616:   fatal_with_file (s, entry);
                   3617: }
                   3618: 
                   3619: /* Report a nonfatal error.
                   3620:    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
                   3621: 
1.1.1.2 ! root     3622: void
1.1       root     3623: error (string, arg1, arg2, arg3)
                   3624:      char *string, *arg1, *arg2, *arg3;
                   3625: {
                   3626:   fprintf (stderr, string, arg1, arg2, arg3);
                   3627:   fprintf (stderr, "\n");
                   3628: }
1.1.1.2 ! root     3629: 
1.1       root     3630: 
                   3631: /* Output COUNT*ELTSIZE bytes of data at BUF
                   3632:    to the descriptor DESC.  */
                   3633: 
1.1.1.2 ! root     3634: void
1.1       root     3635: mywrite (buf, count, eltsize, desc)
                   3636:      char *buf;
                   3637:      int count;
                   3638:      int eltsize;
                   3639:      int desc;
                   3640: {
                   3641:   register int val;
                   3642:   register int bytes = count * eltsize;
                   3643: 
                   3644:   while (bytes > 0)
                   3645:     {
                   3646:       val = write (desc, buf, bytes);
                   3647:       if (val <= 0)
                   3648:        perror_name (output_filename);
                   3649:       buf += val;
                   3650:       bytes -= val;
                   3651:     }
                   3652: }
                   3653: 
                   3654: /* Output PADDING zero-bytes to descriptor OUTDESC.
                   3655:    PADDING may be negative; in that case, do nothing.  */
                   3656: 
                   3657: void
                   3658: padfile (padding, outdesc)
                   3659:      int padding;
                   3660:      int outdesc;
                   3661: {
                   3662:   register char *buf;
                   3663:   if (padding <= 0)
                   3664:     return;
                   3665: 
                   3666:   buf = (char *) alloca (padding);
                   3667:   bzero (buf, padding);
                   3668:   mywrite (buf, padding, 1, outdesc);
                   3669: }
                   3670: 
                   3671: /* Return a newly-allocated string
                   3672:    whose contents concatenate the strings S1, S2, S3.  */
                   3673: 
                   3674: char *
                   3675: concat (s1, s2, s3)
                   3676:      char *s1, *s2, *s3;
                   3677: {
                   3678:   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
                   3679:   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
                   3680: 
                   3681:   strcpy (result, s1);
                   3682:   strcpy (result + len1, s2);
                   3683:   strcpy (result + len1 + len2, s3);
                   3684:   result[len1 + len2 + len3] = 0;
                   3685: 
                   3686:   return result;
                   3687: }
                   3688: 
                   3689: /* Parse the string ARG using scanf format FORMAT, and return the result.
                   3690:    If it does not parse, report fatal error
                   3691:    generating the error message using format string ERROR and ARG as arg.  */
                   3692: 
                   3693: int
                   3694: parse (arg, format, error)
                   3695:      char *arg, *format;
                   3696: {
                   3697:   int x;
                   3698:   if (1 != sscanf (arg, format, &x))
                   3699:     fatal (error, arg);
                   3700:   return x;
                   3701: }
                   3702: 
                   3703: /* Like malloc but get fatal error if memory is exhausted.  */
                   3704: 
                   3705: int
                   3706: xmalloc (size)
                   3707:      int size;
                   3708: {
                   3709:   register int result = malloc (size);
                   3710:   if (!result)
                   3711:     fatal ("virtual memory exhausted", 0);
                   3712:   return result;
                   3713: }
                   3714: 
                   3715: /* Like realloc but get fatal error if memory is exhausted.  */
                   3716: 
                   3717: int
                   3718: xrealloc (ptr, size)
                   3719:      char *ptr;
                   3720:      int size;
                   3721: {
                   3722:   register int result = realloc (ptr, size);
                   3723:   if (!result)
                   3724:     fatal ("virtual memory exhausted", 0);
                   3725:   return result;
                   3726: }

unix.superglobalmegacorp.com