1 /*- 2 * This code is derived from software copyrighted by the Free Software 3 * Foundation. 4 * 5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc. 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)ld.c 6.11 (Berkeley) 07/06/91"; 10 #endif /* not lint */ 11 12 /* Linker `ld' for GNU 13 Copyright (C) 1988 Free Software Foundation, Inc. 14 15 This program is free software; you can redistribute it and/or modify 16 it under the terms of the GNU General Public License as published by 17 the Free Software Foundation; either version 1, or (at your option) 18 any later version. 19 20 This program is distributed in the hope that it will be useful, 21 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 GNU General Public License for more details. 24 25 You should have received a copy of the GNU General Public License 26 along with this program; if not, write to the Free Software 27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 28 29 /* Written by Richard Stallman with some help from Eric Albert. 30 Set, indirect, and warning symbol features added by Randy Smith. */ 31 32 /* Define how to initialize system-dependent header fields. */ 33 34 #include <ar.h> 35 #include <stdio.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <sys/file.h> 39 #include <sys/time.h> 40 #include <sys/resource.h> 41 #include <fcntl.h> 42 #include <a.out.h> 43 #include <stab.h> 44 #include <string.h> 45 46 /* symseg.h defines the obsolete GNU debugging format; we should nuke it. */ 47 #define CORE_ADDR unsigned long /* For symseg.h */ 48 #include "symseg.h" 49 50 #define N_SET_MAGIC(exec, val) ((exec).a_magic = val) 51 52 /* If compiled with GNU C, use the built-in alloca */ 53 #ifdef __GNUC__ 54 #define alloca __builtin_alloca 55 #endif 56 57 #define min(a,b) ((a) < (b) ? (a) : (b)) 58 59 /* Macro to control the number of undefined references printed */ 60 #define MAX_UREFS_PRINTED 10 61 62 /* Size of a page; obtained from the operating system. */ 63 64 int page_size; 65 66 /* Name this program was invoked by. */ 67 68 char *progname; 69 70 /* System dependencies */ 71 72 /* Define this to specify the default executable format. */ 73 74 #ifndef DEFAULT_MAGIC 75 #define DEFAULT_MAGIC ZMAGIC 76 #endif 77 78 #ifdef hp300 79 #define INITIALIZE_HEADER outheader.a_mid = MID_HP300 80 #endif 81 82 /* 83 * Ok. Following are the relocation information macros. If your 84 * system should not be able to use the default set (below), you must 85 * define the following: 86 87 * relocation_info: This must be typedef'd (or #define'd) to the type 88 * of structure that is stored in the relocation info section of your 89 * a.out files. Often this is defined in the a.out.h for your system. 90 * 91 * RELOC_ADDRESS (rval): Offset into the current section of the 92 * <whatever> to be relocated. *Must be an lvalue*. 93 * 94 * RELOC_EXTERN_P (rval): Is this relocation entry based on an 95 * external symbol (1), or was it fully resolved upon entering the 96 * loader (0) in which case some combination of the value in memory 97 * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains 98 * what the value of the relocation actually was. *Must be an lvalue*. 99 * 100 * RELOC_TYPE (rval): If this entry was fully resolved upon 101 * entering the loader, what type should it be relocated as? 102 * 103 * RELOC_SYMBOL (rval): If this entry was not fully resolved upon 104 * entering the loader, what is the index of it's symbol in the symbol 105 * table? *Must be a lvalue*. 106 * 107 * RELOC_MEMORY_ADD_P (rval): This should return true if the final 108 * relocation value output here should be added to memory, or if the 109 * section of memory described should simply be set to the relocation 110 * value. 111 * 112 * RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives 113 * an extra value to be added to the relocation value based on the 114 * individual relocation entry. *Must be an lvalue if defined*. 115 * 116 * RELOC_PCREL_P (rval): True if the relocation value described is 117 * pc relative. 118 * 119 * RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the 120 * final relocation value before putting it where it belongs. 121 * 122 * RELOC_TARGET_SIZE (rval): log to the base 2 of the number of 123 * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes 124 * == 1; 4 bytes == 2, and etc. This is somewhat redundant (we could 125 * do everything in terms of the bit operators below), but having this 126 * macro could end up producing better code on machines without fancy 127 * bit twiddling. Also, it's easier to understand/code big/little 128 * endian distinctions with this macro. 129 * 130 * RELOC_TARGET_BITPOS (rval): The starting bit position within the 131 * object described in RELOC_TARGET_SIZE in which the relocation value 132 * will go. 133 * 134 * RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced 135 * with the bits of the relocation value. It may be assumed by the 136 * code that the relocation value will fit into this many bits. This 137 * may be larger than RELOC_TARGET_SIZE if such be useful. 138 * 139 * 140 * Things I haven't implemented 141 * ---------------------------- 142 * 143 * Values for RELOC_TARGET_SIZE other than 0, 1, or 2. 144 * 145 * Pc relative relocation for External references. 146 * 147 * 148 */ 149 150 /* The following #if has been modifed for cross compilation */ 151 /* It originally read: #if defined(sun) && defined(sparc) */ 152 /* Marc Ullman, Stanford University Nov. 1 1989 */ 153 #if defined(sun) && (TARGET == SUN4) 154 /* Sparc (Sun 4) macros */ 155 #undef relocation_info 156 #define relocation_info reloc_info_sparc 157 #define RELOC_ADDRESS(r) ((r)->r_address) 158 #define RELOC_EXTERN_P(r) ((r)->r_extern) 159 #define RELOC_TYPE(r) ((r)->r_index) 160 #define RELOC_SYMBOL(r) ((r)->r_index) 161 #define RELOC_MEMORY_SUB_P(r) 0 162 #define RELOC_MEMORY_ADD_P(r) 0 163 #define RELOC_ADD_EXTRA(r) ((r)->r_addend) 164 #define RELOC_PCREL_P(r) \ 165 ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22) 166 #define RELOC_VALUE_RIGHTSHIFT(r) (reloc_target_rightshift[(r)->r_type]) 167 #define RELOC_TARGET_SIZE(r) (reloc_target_size[(r)->r_type]) 168 #define RELOC_TARGET_BITPOS(r) 0 169 #define RELOC_TARGET_BITSIZE(r) (reloc_target_bitsize[(r)->r_type]) 170 171 /* Note that these are very dependent on the order of the enums in 172 enum reloc_type (in a.out.h); if they change the following must be 173 changed */ 174 /* Also note that the last few may be incorrect; I have no information */ 175 static int reloc_target_rightshift[] = { 176 0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0, 177 }; 178 static int reloc_target_size[] = { 179 0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 180 }; 181 static int reloc_target_bitsize[] = { 182 8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16, 183 }; 184 185 #define MAX_ALIGNMENT (sizeof (double)) 186 #endif 187 188 /* Default macros */ 189 #ifndef RELOC_ADDRESS 190 #define RELOC_ADDRESS(r) ((r)->r_address) 191 #define RELOC_EXTERN_P(r) ((r)->r_extern) 192 #define RELOC_TYPE(r) ((r)->r_symbolnum) 193 #define RELOC_SYMBOL(r) ((r)->r_symbolnum) 194 #define RELOC_MEMORY_SUB_P(r) 0 195 #define RELOC_MEMORY_ADD_P(r) 1 196 #undef RELOC_ADD_EXTRA 197 #define RELOC_PCREL_P(r) ((r)->r_pcrel) 198 #define RELOC_VALUE_RIGHTSHIFT(r) 0 199 #define RELOC_TARGET_SIZE(r) ((r)->r_length) 200 #define RELOC_TARGET_BITPOS(r) 0 201 #define RELOC_TARGET_BITSIZE(r) 32 202 #endif 203 204 #ifndef MAX_ALIGNMENT 205 #define MAX_ALIGNMENT (sizeof (int)) 206 #endif 207 208 #ifdef nounderscore 209 #define LPREFIX '.' 210 #else 211 #define LPREFIX 'L' 212 #endif 213 214 #ifndef TEXT_START 215 #define TEXT_START(x) N_TXTADDR(x) 216 #endif 217 218 /* Special global symbol types understood by GNU LD. */ 219 220 /* The following type indicates the definition of a symbol as being 221 an indirect reference to another symbol. The other symbol 222 appears as an undefined reference, immediately following this symbol. 223 224 Indirection is asymmetrical. The other symbol's value will be used 225 to satisfy requests for the indirect symbol, but not vice versa. 226 If the other symbol does not have a definition, libraries will 227 be searched to find a definition. 228 229 So, for example, the following two lines placed in an assembler 230 input file would result in an object file which would direct gnu ld 231 to resolve all references to symbol "foo" as references to symbol 232 "bar". 233 234 .stabs "_foo",11,0,0,0 235 .stabs "_bar",1,0,0,0 236 237 Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)). */ 238 239 #ifndef N_INDR 240 #define N_INDR 0xa 241 #endif 242 243 /* The following symbols refer to set elements. These are expected 244 only in input to the loader; they should not appear in loader 245 output (unless relocatable output is requested). To be recognized 246 by the loader, the input symbols must have their N_EXT bit set. 247 All the N_SET[ATDB] symbols with the same name form one set. The 248 loader collects all of these elements at load time and outputs a 249 vector for each name. 250 Space (an array of 32 bit words) is allocated for the set in the 251 data section, and the n_value field of each set element value is 252 stored into one word of the array. 253 The first word of the array is the length of the set (number of 254 elements). The last word of the vector is set to zero for possible 255 use by incremental loaders. The array is ordered by the linkage 256 order; the first symbols which the linker encounters will be first 257 in the array. 258 259 In C syntax this looks like: 260 261 struct set_vector { 262 unsigned int length; 263 unsigned int vector[length]; 264 unsigned int always_zero; 265 }; 266 267 Before being placed into the array, each element is relocated 268 according to its type. This allows the loader to create an array 269 of pointers to objects automatically. N_SETA type symbols will not 270 be relocated. 271 272 The address of the set is made into an N_SETV symbol 273 whose name is the same as the name of the set. 274 This symbol acts like a N_DATA global symbol 275 in that it can satisfy undefined external references. 276 277 For the purposes of determining whether or not to load in a library 278 file, set element definitions are not considered "real 279 definitions"; they will not cause the loading of a library 280 member. 281 282 If relocatable output is requested, none of this processing is 283 done. The symbols are simply relocated and passed through to the 284 output file. 285 286 So, for example, the following three lines of assembler code 287 (whether in one file or scattered between several different ones) 288 will produce a three element vector (total length is five words; 289 see above), referenced by the symbol "_xyzzy", which will have the 290 addresses of the routines _init1, _init2, and _init3. 291 292 *NOTE*: If symbolic addresses are used in the n_value field of the 293 defining .stabs, those symbols must be defined in the same file as 294 that containing the .stabs. 295 296 .stabs "_xyzzy",23,0,0,_init1 297 .stabs "_xyzzy",23,0,0,_init2 298 .stabs "_xyzzy",23,0,0,_init3 299 300 Note that (23 == (N_SETT | N_EXT)). */ 301 302 #ifndef N_SETA 303 #define N_SETA 0x14 /* Absolute set element symbol */ 304 #endif /* This is input to LD, in a .o file. */ 305 306 #ifndef N_SETT 307 #define N_SETT 0x16 /* Text set element symbol */ 308 #endif /* This is input to LD, in a .o file. */ 309 310 #ifndef N_SETD 311 #define N_SETD 0x18 /* Data set element symbol */ 312 #endif /* This is input to LD, in a .o file. */ 313 314 #ifndef N_SETB 315 #define N_SETB 0x1A /* Bss set element symbol */ 316 #endif /* This is input to LD, in a .o file. */ 317 318 /* Macros dealing with the set element symbols defined in a.out.h */ 319 #define SET_ELEMENT_P(x) ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT)) 320 #define TYPE_OF_SET_ELEMENT(x) ((x)-N_SETA+N_ABS) 321 322 #ifndef N_SETV 323 #define N_SETV 0x1C /* Pointer to set vector in data area. */ 324 #endif /* This is output from LD. */ 325 326 /* If a this type of symbol is encountered, its name is a warning 327 message to print each time the symbol referenced by the next symbol 328 table entry is referenced. 329 330 This feature may be used to allow backwards compatibility with 331 certain functions (eg. gets) but to discourage programmers from 332 their use. 333 334 So if, for example, you wanted to have ld print a warning whenever 335 the function "gets" was used in their C program, you would add the 336 following to the assembler file in which gets is defined: 337 338 .stabs "Obsolete function \"gets\" referenced",30,0,0,0 339 .stabs "_gets",1,0,0,0 340 341 These .stabs do not necessarily have to be in the same file as the 342 gets function, they simply must exist somewhere in the compilation. */ 343 344 #ifndef N_WARNING 345 #define N_WARNING 0x1E /* Warning message to print if symbol 346 included */ 347 #endif /* This is input to ld */ 348 349 #ifndef __GNU_STAB__ 350 351 /* Line number for the data section. This is to be used to describe 352 the source location of a variable declaration. */ 353 #ifndef N_DSLINE 354 #define N_DSLINE (N_SLINE+N_DATA-N_TEXT) 355 #endif 356 357 /* Line number for the bss section. This is to be used to describe 358 the source location of a variable declaration. */ 359 #ifndef N_BSLINE 360 #define N_BSLINE (N_SLINE+N_BSS-N_TEXT) 361 #endif 362 363 #endif /* not __GNU_STAB__ */ 364 365 /* Symbol table */ 366 367 /* Global symbol data is recorded in these structures, 368 one for each global symbol. 369 They are found via hashing in 'symtab', which points to a vector of buckets. 370 Each bucket is a chain of these structures through the link field. */ 371 372 typedef 373 struct glosym 374 { 375 /* Pointer to next symbol in this symbol's hash bucket. */ 376 struct glosym *link; 377 /* Name of this symbol. */ 378 char *name; 379 /* Value of this symbol as a global symbol. */ 380 long value; 381 /* Chain of external 'nlist's in files for this symbol, both defs 382 and refs. */ 383 struct nlist *refs; 384 /* Any warning message that might be associated with this symbol 385 from an N_WARNING symbol encountered. */ 386 char *warning; 387 /* Nonzero means definitions of this symbol as common have been seen, 388 and the value here is the largest size specified by any of them. */ 389 int max_common_size; 390 /* For relocatable_output, records the index of this global sym in the 391 symbol table to be written, with the first global sym given index 0.*/ 392 int def_count; 393 /* Nonzero means a definition of this global symbol is known to exist. 394 Library members should not be loaded on its account. */ 395 char defined; 396 /* Nonzero means a reference to this global symbol has been seen 397 in a file that is surely being loaded. 398 A value higher than 1 is the n_type code for the symbol's 399 definition. */ 400 char referenced; 401 /* A count of the number of undefined references printed for a 402 specific symbol. If a symbol is unresolved at the end of 403 digest_symbols (and the loading run is supposed to produce 404 relocatable output) do_file_warnings keeps track of how many 405 unresolved reference error messages have been printed for 406 each symbol here. When the number hits MAX_UREFS_PRINTED, 407 messages stop. */ 408 unsigned char undef_refs; 409 /* 1 means that this symbol has multiple definitions. 2 means 410 that it has multiple definitions, and some of them are set 411 elements, one of which has been printed out already. */ 412 unsigned char multiply_defined; 413 /* Nonzero means print a message at all refs or defs of this symbol */ 414 char trace; 415 } 416 symbol; 417 418 /* Demangler for C++. */ 419 extern char *cplus_demangle (); 420 421 /* Demangler function to use. */ 422 char *(*demangler)() = NULL; 423 424 /* Number of buckets in symbol hash table */ 425 #define TABSIZE 1009 426 427 /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */ 428 symbol *symtab[TABSIZE]; 429 430 /* Number of symbols in symbol hash table. */ 431 int num_hash_tab_syms = 0; 432 433 /* Count the number of nlist entries that are for local symbols. 434 This count and the three following counts 435 are incremented as as symbols are entered in the symbol table. */ 436 int local_sym_count; 437 438 /* Count number of nlist entries that are for local symbols 439 whose names don't start with L. */ 440 int non_L_local_sym_count; 441 442 /* Count the number of nlist entries for debugger info. */ 443 int debugger_sym_count; 444 445 /* Count the number of global symbols referenced and not defined. */ 446 int undefined_global_sym_count; 447 448 /* Count the number of global symbols multiply defined. */ 449 int multiple_def_count; 450 451 /* Count the number of defined global symbols. 452 Each symbol is counted only once 453 regardless of how many different nlist entries refer to it, 454 since the output file will need only one nlist entry for it. 455 This count is computed by `digest_symbols'; 456 it is undefined while symbols are being loaded. */ 457 int defined_global_sym_count; 458 459 /* Count the number of symbols defined through common declarations. 460 This count is kept in symdef_library, linear_library, and 461 enter_global_ref. It is incremented when the defined flag is set 462 in a symbol because of a common definition, and decremented when 463 the symbol is defined "for real" (ie. by something besides a common 464 definition). */ 465 int common_defined_global_count; 466 467 /* Count the number of set element type symbols and the number of 468 separate vectors which these symbols will fit into. See the 469 GNU a.out.h for more info. 470 This count is computed by 'enter_file_symbols' */ 471 int set_symbol_count; 472 int set_vector_count; 473 474 /* Define a linked list of strings which define symbols which should 475 be treated as set elements even though they aren't. Any symbol 476 with a prefix matching one of these should be treated as a set 477 element. 478 479 This is to make up for deficiencies in many assemblers which aren't 480 willing to pass any stabs through to the loader which they don't 481 understand. */ 482 struct string_list_element { 483 char *str; 484 struct string_list_element *next; 485 }; 486 487 struct string_list_element *set_element_prefixes; 488 489 /* Count the number of definitions done indirectly (ie. done relative 490 to the value of some other symbol. */ 491 int global_indirect_count; 492 493 /* Count the number of warning symbols encountered. */ 494 int warning_count; 495 496 /* Total number of symbols to be written in the output file. 497 Computed by digest_symbols from the variables above. */ 498 int nsyms; 499 500 501 /* Nonzero means ptr to symbol entry for symbol to use as start addr. 502 -e sets this. */ 503 symbol *entry_symbol; 504 505 symbol *edata_symbol; /* the symbol _edata */ 506 symbol *etext_symbol; /* the symbol _etext */ 507 symbol *end_symbol; /* the symbol _end */ 508 509 /* Each input file, and each library member ("subfile") being loaded, 510 has a `file_entry' structure for it. 511 512 For files specified by command args, these are contained in the vector 513 which `file_table' points to. 514 515 For library members, they are dynamically allocated, 516 and chained through the `chain' field. 517 The chain is found in the `subfiles' field of the `file_entry'. 518 The `file_entry' objects for the members have `superfile' fields pointing 519 to the one for the library. */ 520 521 struct file_entry { 522 /* Name of this file. */ 523 char *filename; 524 /* Name to use for the symbol giving address of text start */ 525 /* Usually the same as filename, but for a file spec'd with -l 526 this is the -l switch itself rather than the filename. */ 527 char *local_sym_name; 528 529 /* Describe the layout of the contents of the file */ 530 531 /* The file's a.out header. */ 532 struct exec header; 533 /* Offset in file of GDB symbol segment, or 0 if there is none. */ 534 int symseg_offset; 535 536 /* Describe data from the file loaded into core */ 537 538 /* Symbol table of the file. */ 539 struct nlist *symbols; 540 /* Size in bytes of string table. */ 541 int string_size; 542 /* Pointer to the string table. 543 The string table is not kept in core all the time, 544 but when it is in core, its address is here. */ 545 char *strings; 546 547 /* Next two used only if `relocatable_output' or if needed for */ 548 /* output of undefined reference line numbers. */ 549 550 /* Text reloc info saved by `write_text' for `coptxtrel'. */ 551 struct relocation_info *textrel; 552 /* Data reloc info saved by `write_data' for `copdatrel'. */ 553 struct relocation_info *datarel; 554 555 /* Relation of this file's segments to the output file */ 556 557 /* Start of this file's text seg in the output file core image. */ 558 int text_start_address; 559 /* Start of this file's data seg in the output file core image. */ 560 int data_start_address; 561 /* Start of this file's bss seg in the output file core image. */ 562 int bss_start_address; 563 /* Offset in bytes in the output file symbol table 564 of the first local symbol for this file. Set by `write_file_symbols'. */ 565 int local_syms_offset; 566 567 /* For library members only */ 568 569 /* For a library, points to chain of entries for the library members. */ 570 struct file_entry *subfiles; 571 /* For a library member, offset of the member within the archive. 572 Zero for files that are not library members. */ 573 int starting_offset; 574 /* Size of contents of this file, if library member. */ 575 int total_size; 576 /* For library member, points to the library's own entry. */ 577 struct file_entry *superfile; 578 /* For library member, points to next entry for next member. */ 579 struct file_entry *chain; 580 581 /* 1 if file is a library. */ 582 char library_flag; 583 584 /* 1 if file's header has been read into this structure. */ 585 char header_read_flag; 586 587 /* 1 means search a set of directories for this file. */ 588 char search_dirs_flag; 589 590 /* 1 means this is base file of incremental load. 591 Do not load this file's text or data. 592 Also default text_start to after this file's bss. */ 593 char just_syms_flag; 594 }; 595 596 /* Vector of entries for input files specified by arguments. 597 These are all the input files except for members of specified libraries. */ 598 struct file_entry *file_table; 599 600 /* Length of that vector. */ 601 int number_of_files; 602 603 /* When loading the text and data, we can avoid doing a close 604 and another open between members of the same library. 605 606 These two variables remember the file that is currently open. 607 Both are zero if no file is open. 608 609 See `each_file' and `file_close'. */ 610 611 struct file_entry *input_file; 612 int input_desc; 613 614 /* The name of the file to write; "a.out" by default. */ 615 616 char *output_filename; 617 618 /* Descriptor for writing that file with `mywrite'. */ 619 620 int outdesc; 621 622 /* Header for that file (filled in by `write_header'). */ 623 624 struct exec outheader; 625 626 #ifdef COFF_ENCAPSULATE 627 struct coffheader coffheader; 628 int need_coff_header; 629 #endif 630 631 /* The following are computed by `digest_symbols'. */ 632 633 int text_size; /* total size of text of all input files. */ 634 int data_size; /* total size of data of all input files. */ 635 int bss_size; /* total size of bss of all input files. */ 636 int text_reloc_size; /* total size of text relocation of all input files. */ 637 int data_reloc_size; /* total size of data relocation of all input */ 638 /* files. */ 639 640 /* Specifications of start and length of the area reserved at the end 641 of the text segment for the set vectors. Computed in 'digest_symbols' */ 642 int set_sect_start; 643 int set_sect_size; 644 645 /* Pointer for in core storage for the above vectors, before they are 646 written. */ 647 unsigned long *set_vectors; 648 649 /* Amount of cleared space to leave between the text and data segments. */ 650 651 int text_pad; 652 653 /* Amount of bss segment to include as part of the data segment. */ 654 655 int data_pad; 656 657 /* Format of __.SYMDEF: 658 First, a longword containing the size of the 'symdef' data that follows. 659 Second, zero or more 'symdef' structures. 660 Third, a longword containing the length of symbol name strings. 661 Fourth, zero or more symbol name strings (each followed by a null). */ 662 663 struct symdef { 664 int symbol_name_string_index; 665 int library_member_offset; 666 }; 667 668 /* Record most of the command options. */ 669 670 /* Address we assume the text section will be loaded at. 671 We relocate symbols and text and data for this, but we do not 672 write any padding in the output file for it. */ 673 int text_start; 674 675 /* Offset of default entry-pc within the text section. */ 676 int entry_offset; 677 678 /* Address we decide the data section will be loaded at. */ 679 int data_start; 680 681 /* `text-start' address is normally this much plus a page boundary. 682 This is not a user option; it is fixed for each system. */ 683 int text_start_alignment; 684 685 /* Nonzero if -T was specified in the command line. 686 This prevents text_start from being set later to default values. */ 687 int T_flag_specified; 688 689 /* Nonzero if -Tdata was specified in the command line. 690 This prevents data_start from being set later to default values. */ 691 int Tdata_flag_specified; 692 693 /* Size to pad data section up to. 694 We simply increase the size of the data section, padding with zeros, 695 and reduce the size of the bss section to match. */ 696 int specified_data_size; 697 698 /* Magic number to use for the output file, set by switch. */ 699 int magic; 700 701 /* Nonzero means print names of input files as processed. */ 702 int trace_files; 703 704 /* Which symbols should be stripped (omitted from the output): 705 none, all, or debugger symbols. */ 706 enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols; 707 708 /* Which local symbols should be omitted: 709 none, all, or those starting with L. 710 This is irrelevant if STRIP_NONE. */ 711 enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals; 712 713 /* 1 => write load map. */ 714 int write_map; 715 716 /* 1 => write relocation into output file so can re-input it later. */ 717 int relocatable_output; 718 719 /* 1 => assign space to common symbols even if `relocatable_output'. */ 720 int force_common_definition; 721 722 /* Standard directories to search for files specified by -l. */ 723 char *standard_search_dirs[] = 724 #ifdef STANDARD_SEARCH_DIRS 725 {STANDARD_SEARCH_DIRS}; 726 #else 727 #ifdef NON_NATIVE 728 {"/usr/local/lib/gnu"}; 729 #else 730 {"/lib", "/usr/lib", "/usr/local/lib"}; 731 #endif 732 #endif 733 734 /* Actual vector of directories to search; 735 this contains those specified with -L plus the standard ones. */ 736 char **search_dirs; 737 738 /* Length of the vector `search_dirs'. */ 739 int n_search_dirs; 740 741 /* Non zero means to create the output executable. */ 742 /* Cleared by nonfatal errors. */ 743 int make_executable; 744 745 /* Force the executable to be output, even if there are non-fatal 746 errors */ 747 int force_executable; 748 749 /* Keep a list of any symbols referenced from the command line (so 750 that error messages for these guys can be generated). This list is 751 zero terminated. */ 752 struct glosym **cmdline_references; 753 int cl_refs_allocated; 754 755 void bcopy (), bzero (); 756 int malloc (), realloc (); 757 #ifndef alloca 758 int alloca (); 759 #endif 760 int free (); 761 762 int xmalloc (); 763 int xrealloc (); 764 void fatal (); 765 void fatal_with_file (); 766 void perror_name (); 767 void perror_file (); 768 void error (); 769 770 void digest_symbols (); 771 void print_symbols (); 772 void load_symbols (); 773 void decode_command (); 774 void list_undefined_symbols (); 775 void list_unresolved_references (); 776 void write_output (); 777 void write_header (); 778 void write_text (); 779 void read_file_relocation (); 780 void write_data (); 781 void write_rel (); 782 void write_syms (); 783 void write_symsegs (); 784 void mywrite (); 785 void symtab_init (); 786 void padfile (); 787 char *concat (); 788 char *get_file_name (); 789 symbol *getsym (), *getsym_soft (); 790 791 int 792 main (argc, argv) 793 char **argv; 794 int argc; 795 { 796 /* Added this to stop ld core-dumping on very large .o files. */ 797 #ifdef RLIMIT_STACK 798 /* Get rid of any avoidable limit on stack size. */ 799 { 800 struct rlimit rlim; 801 802 /* Set the stack limit huge so that alloca does not fail. */ 803 getrlimit (RLIMIT_STACK, &rlim); 804 rlim.rlim_cur = rlim.rlim_max; 805 setrlimit (RLIMIT_STACK, &rlim); 806 } 807 #endif /* RLIMIT_STACK */ 808 809 page_size = getpagesize (); 810 progname = argv[0]; 811 812 /* Clear the cumulative info on the output file. */ 813 814 text_size = 0; 815 data_size = 0; 816 bss_size = 0; 817 text_reloc_size = 0; 818 data_reloc_size = 0; 819 820 data_pad = 0; 821 text_pad = 0; 822 823 /* Initialize the data about options. */ 824 825 specified_data_size = 0; 826 strip_symbols = STRIP_NONE; 827 trace_files = 0; 828 discard_locals = DISCARD_NONE; 829 entry_symbol = 0; 830 write_map = 0; 831 relocatable_output = 0; 832 force_common_definition = 0; 833 T_flag_specified = 0; 834 Tdata_flag_specified = 0; 835 magic = DEFAULT_MAGIC; 836 make_executable = 1; 837 force_executable = 0; 838 set_element_prefixes = 0; 839 840 /* Initialize the cumulative counts of symbols. */ 841 842 local_sym_count = 0; 843 non_L_local_sym_count = 0; 844 debugger_sym_count = 0; 845 undefined_global_sym_count = 0; 846 set_symbol_count = 0; 847 set_vector_count = 0; 848 global_indirect_count = 0; 849 warning_count = 0; 850 multiple_def_count = 0; 851 common_defined_global_count = 0; 852 853 /* Keep a list of symbols referenced from the command line */ 854 cl_refs_allocated = 10; 855 cmdline_references 856 = (struct glosym **) xmalloc (cl_refs_allocated 857 * sizeof(struct glosym *)); 858 *cmdline_references = 0; 859 860 /* Completely decode ARGV. */ 861 862 decode_command (argc, argv); 863 864 /* Create the symbols `etext', `edata' and `end'. */ 865 866 if (!relocatable_output) 867 symtab_init (); 868 869 /* Determine whether to count the header as part of 870 the text size, and initialize the text size accordingly. 871 This depends on the kind of system and on the output format selected. */ 872 873 N_SET_MAGIC (outheader, magic); 874 #ifdef INITIALIZE_HEADER 875 INITIALIZE_HEADER; 876 #endif 877 878 text_size = sizeof (struct exec); 879 #ifdef COFF_ENCAPSULATE 880 if (relocatable_output == 0 && file_table[0].just_syms_flag == 0) 881 { 882 need_coff_header = 1; 883 /* set this flag now, since it will change the values of N_TXTOFF, etc */ 884 N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE); 885 text_size += sizeof (struct coffheader); 886 } 887 #endif 888 889 text_size -= N_TXTOFF (outheader); 890 891 if (text_size < 0) 892 text_size = 0; 893 entry_offset = text_size; 894 895 if (!T_flag_specified && !relocatable_output) 896 text_start = TEXT_START (outheader); 897 898 /* The text-start address is normally this far past a page boundary. */ 899 text_start_alignment = text_start % page_size; 900 901 /* Load symbols of all input files. 902 Also search all libraries and decide which library members to load. */ 903 904 load_symbols (); 905 906 /* Compute where each file's sections go, and relocate symbols. */ 907 908 digest_symbols (); 909 910 /* Print error messages for any missing symbols, for any warning 911 symbols, and possibly multiple definitions */ 912 913 do_warnings (stderr); 914 915 /* Print a map, if requested. */ 916 917 if (write_map) print_symbols (stdout); 918 919 /* Write the output file. */ 920 921 if (make_executable || force_executable) 922 write_output (); 923 924 exit (!make_executable); 925 } 926 927 void decode_option (); 928 929 /* Analyze a command line argument. 930 Return 0 if the argument is a filename. 931 Return 1 if the argument is a option complete in itself. 932 Return 2 if the argument is a option which uses an argument. 933 934 Thus, the value is the number of consecutive arguments 935 that are part of options. */ 936 937 int 938 classify_arg (arg) 939 register char *arg; 940 { 941 if (*arg != '-') return 0; 942 switch (arg[1]) 943 { 944 case 'A': 945 case 'D': 946 case 'e': 947 case 'L': 948 case 'l': 949 case 'o': 950 case 'u': 951 case 'V': 952 case 'y': 953 if (arg[2]) 954 return 1; 955 return 2; 956 957 case 'B': 958 if (! strcmp (&arg[2], "static")) 959 return 1; 960 961 case 'T': 962 if (arg[2] == 0) 963 return 2; 964 if (! strcmp (&arg[2], "text")) 965 return 2; 966 if (! strcmp (&arg[2], "data")) 967 return 2; 968 return 1; 969 } 970 971 return 1; 972 } 973 974 /* Process the command arguments, 975 setting up file_table with an entry for each input file, 976 and setting variables according to the options. */ 977 978 void 979 decode_command (argc, argv) 980 char **argv; 981 int argc; 982 { 983 register int i; 984 register struct file_entry *p; 985 char *cp; 986 987 number_of_files = 0; 988 output_filename = "a.out"; 989 990 n_search_dirs = 0; 991 search_dirs = (char **) xmalloc (sizeof (char *)); 992 993 /* First compute number_of_files so we know how long to make file_table. */ 994 /* Also process most options completely. */ 995 996 for (i = 1; i < argc; i++) 997 { 998 register int code = classify_arg (argv[i]); 999 if (code) 1000 { 1001 if (i + code > argc) 1002 fatal ("no argument following %s\n", argv[i]); 1003 1004 decode_option (argv[i], argv[i+1]); 1005 1006 if (argv[i][1] == 'l' || argv[i][1] == 'A') 1007 number_of_files++; 1008 1009 i += code - 1; 1010 } 1011 else 1012 number_of_files++; 1013 } 1014 1015 if (!number_of_files) 1016 fatal ("no input files", 0); 1017 1018 p = file_table 1019 = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry)); 1020 bzero (p, number_of_files * sizeof (struct file_entry)); 1021 1022 /* Now scan again and fill in file_table. */ 1023 /* All options except -A and -l are ignored here. */ 1024 1025 for (i = 1; i < argc; i++) 1026 { 1027 register int code = classify_arg (argv[i]); 1028 1029 if (code) 1030 { 1031 char *string; 1032 if (code == 2) 1033 string = argv[i+1]; 1034 else 1035 string = &argv[i][2]; 1036 1037 if (argv[i][1] == 'A') 1038 { 1039 if (p != file_table) 1040 fatal ("-A specified before an input file other than the first"); 1041 1042 p->filename = string; 1043 p->local_sym_name = string; 1044 p->just_syms_flag = 1; 1045 p++; 1046 } 1047 if (argv[i][1] == 'l') 1048 { 1049 if (cp = rindex(string, '/')) 1050 { 1051 *cp++ = '\0'; 1052 cp = concat (string, "/lib", cp); 1053 p->filename = concat (cp, ".a", ""); 1054 } 1055 else 1056 p->filename = concat ("lib", string, ".a"); 1057 1058 p->local_sym_name = concat ("-l", string, ""); 1059 p->search_dirs_flag = 1; 1060 p++; 1061 } 1062 i += code - 1; 1063 } 1064 else 1065 { 1066 p->filename = argv[i]; 1067 p->local_sym_name = argv[i]; 1068 p++; 1069 } 1070 } 1071 1072 /* Now check some option settings for consistency. */ 1073 1074 #ifdef NMAGIC 1075 if ((magic == ZMAGIC || magic == NMAGIC) 1076 #else 1077 if ((magic == ZMAGIC) 1078 #endif 1079 && (text_start - text_start_alignment) & (page_size - 1)) 1080 fatal ("-T argument not multiple of page size, with sharable output", 0); 1081 1082 /* Append the standard search directories to the user-specified ones. */ 1083 { 1084 int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0]; 1085 n_search_dirs += n; 1086 search_dirs 1087 = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *)); 1088 bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n], 1089 n * sizeof (char *)); 1090 } 1091 } 1092 1093 1094 void 1095 add_cmdline_ref (sp) 1096 struct glosym *sp; 1097 { 1098 struct glosym **ptr; 1099 1100 for (ptr = cmdline_references; 1101 ptr < cmdline_references + cl_refs_allocated && *ptr; 1102 ptr++) 1103 ; 1104 1105 if (ptr >= cmdline_references + cl_refs_allocated - 1) 1106 { 1107 int diff = ptr - cmdline_references; 1108 1109 cl_refs_allocated *= 2; 1110 cmdline_references = (struct glosym **) 1111 xrealloc (cmdline_references, 1112 cl_refs_allocated * sizeof (struct glosym *)); 1113 ptr = cmdline_references + diff; 1114 } 1115 1116 *ptr++ = sp; 1117 *ptr = (struct glosym *) 0; 1118 } 1119 1120 int 1121 set_element_prefixed_p (name) 1122 char *name; 1123 { 1124 struct string_list_element *p; 1125 int i; 1126 1127 for (p = set_element_prefixes; p; p = p->next) 1128 { 1129 for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++) 1130 ; 1131 1132 if (p->str[i] == '\0') 1133 return 1; 1134 } 1135 return 0; 1136 } 1137 1138 int parse (); 1139 1140 /* Record an option and arrange to act on it later. 1141 ARG should be the following command argument, 1142 which may or may not be used by this option. 1143 1144 The `l' and `A' options are ignored here since they actually 1145 specify input files. */ 1146 1147 void 1148 decode_option (swt, arg) 1149 register char *swt, *arg; 1150 { 1151 /* We get Bstatic from gcc on suns. */ 1152 if (! strcmp (swt + 1, "Bstatic")) 1153 return; 1154 if (! strcmp (swt + 1, "Ttext")) 1155 { 1156 text_start = parse (arg, "%x", "invalid argument to -Ttext"); 1157 T_flag_specified = 1; 1158 return; 1159 } 1160 if (! strcmp (swt + 1, "Tdata")) 1161 { 1162 data_start = parse (arg, "%x", "invalid argument to -Tdata"); 1163 Tdata_flag_specified = 1; 1164 return; 1165 } 1166 if (! strcmp (swt + 1, "noinhibit-exec")) 1167 { 1168 force_executable = 1; 1169 return; 1170 } 1171 1172 if (swt[2] != 0) 1173 arg = &swt[2]; 1174 1175 switch (swt[1]) 1176 { 1177 case 'A': 1178 return; 1179 1180 case 'D': 1181 specified_data_size = parse (arg, "%x", "invalid argument to -D"); 1182 return; 1183 1184 case 'd': 1185 force_common_definition = 1; 1186 return; 1187 1188 case 'e': 1189 entry_symbol = getsym (arg); 1190 if (!entry_symbol->defined && !entry_symbol->referenced) 1191 undefined_global_sym_count++; 1192 entry_symbol->referenced = 1; 1193 add_cmdline_ref (entry_symbol); 1194 return; 1195 1196 case 'l': 1197 /* If linking with libg++, use the C++ demangler. */ 1198 if (arg != NULL && strcmp (arg, "g++") == 0) 1199 demangler = cplus_demangle; 1200 return; 1201 1202 case 'L': 1203 n_search_dirs++; 1204 search_dirs 1205 = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *)); 1206 search_dirs[n_search_dirs - 1] = arg; 1207 return; 1208 1209 case 'M': 1210 write_map = 1; 1211 return; 1212 1213 case 'N': 1214 magic = OMAGIC; 1215 return; 1216 1217 #ifdef NMAGIC 1218 case 'n': 1219 magic = NMAGIC; 1220 return; 1221 #endif 1222 1223 case 'o': 1224 output_filename = arg; 1225 return; 1226 1227 case 'r': 1228 relocatable_output = 1; 1229 magic = OMAGIC; 1230 text_start = 0; 1231 return; 1232 1233 case 'S': 1234 strip_symbols = STRIP_DEBUGGER; 1235 return; 1236 1237 case 's': 1238 strip_symbols = STRIP_ALL; 1239 return; 1240 1241 case 'T': 1242 text_start = parse (arg, "%x", "invalid argument to -T"); 1243 T_flag_specified = 1; 1244 return; 1245 1246 case 't': 1247 trace_files = 1; 1248 return; 1249 1250 case 'u': 1251 { 1252 register symbol *sp = getsym (arg); 1253 if (!sp->defined && !sp->referenced) 1254 undefined_global_sym_count++; 1255 sp->referenced = 1; 1256 add_cmdline_ref (sp); 1257 } 1258 return; 1259 1260 case 'V': 1261 { 1262 struct string_list_element *new 1263 = (struct string_list_element *) 1264 xmalloc (sizeof (struct string_list_element)); 1265 1266 new->str = arg; 1267 new->next = set_element_prefixes; 1268 set_element_prefixes = new; 1269 return; 1270 } 1271 1272 case 'X': 1273 discard_locals = DISCARD_L; 1274 return; 1275 1276 case 'x': 1277 discard_locals = DISCARD_ALL; 1278 return; 1279 1280 case 'y': 1281 { 1282 register symbol *sp = getsym (&swt[2]); 1283 sp->trace = 1; 1284 } 1285 return; 1286 1287 case 'z': 1288 magic = ZMAGIC; 1289 return; 1290 1291 default: 1292 fatal ("invalid command option `%s'", swt); 1293 } 1294 } 1295 1296 /** Convenient functions for operating on one or all files being */ 1297 /** loaded. */ 1298 void print_file_name (); 1299 1300 /* Call FUNCTION on each input file entry. 1301 Do not call for entries for libraries; 1302 instead, call once for each library member that is being loaded. 1303 1304 FUNCTION receives two arguments: the entry, and ARG. */ 1305 1306 void 1307 each_file (function, arg) 1308 register void (*function)(); 1309 register int arg; 1310 { 1311 register int i; 1312 1313 for (i = 0; i < number_of_files; i++) 1314 { 1315 register struct file_entry *entry = &file_table[i]; 1316 if (entry->library_flag) 1317 { 1318 register struct file_entry *subentry = entry->subfiles; 1319 for (; subentry; subentry = subentry->chain) 1320 (*function) (subentry, arg); 1321 } 1322 else 1323 (*function) (entry, arg); 1324 } 1325 } 1326 1327 /* Call FUNCTION on each input file entry until it returns a non-zero 1328 value. Return this value. 1329 Do not call for entries for libraries; 1330 instead, call once for each library member that is being loaded. 1331 1332 FUNCTION receives two arguments: the entry, and ARG. It must be a 1333 function returning unsigned long (though this can probably be fudged). */ 1334 1335 unsigned long 1336 check_each_file (function, arg) 1337 register unsigned long (*function)(); 1338 register int arg; 1339 { 1340 register int i; 1341 register unsigned long return_val; 1342 1343 for (i = 0; i < number_of_files; i++) 1344 { 1345 register struct file_entry *entry = &file_table[i]; 1346 if (entry->library_flag) 1347 { 1348 register struct file_entry *subentry = entry->subfiles; 1349 for (; subentry; subentry = subentry->chain) 1350 if (return_val = (*function) (subentry, arg)) 1351 return return_val; 1352 } 1353 else 1354 if (return_val = (*function) (entry, arg)) 1355 return return_val; 1356 } 1357 return 0; 1358 } 1359 1360 /* Like `each_file' but ignore files that were just for symbol definitions. */ 1361 1362 void 1363 each_full_file (function, arg) 1364 register void (*function)(); 1365 register int arg; 1366 { 1367 register int i; 1368 1369 for (i = 0; i < number_of_files; i++) 1370 { 1371 register struct file_entry *entry = &file_table[i]; 1372 if (entry->just_syms_flag) 1373 continue; 1374 if (entry->library_flag) 1375 { 1376 register struct file_entry *subentry = entry->subfiles; 1377 for (; subentry; subentry = subentry->chain) 1378 (*function) (subentry, arg); 1379 } 1380 else 1381 (*function) (entry, arg); 1382 } 1383 } 1384 1385 /* Close the input file that is now open. */ 1386 1387 void 1388 file_close () 1389 { 1390 close (input_desc); 1391 input_desc = 0; 1392 input_file = 0; 1393 } 1394 1395 /* Open the input file specified by 'entry', and return a descriptor. 1396 The open file is remembered; if the same file is opened twice in a row, 1397 a new open is not actually done. */ 1398 1399 int 1400 file_open (entry) 1401 register struct file_entry *entry; 1402 { 1403 register int desc; 1404 1405 if (entry->superfile) 1406 return file_open (entry->superfile); 1407 1408 if (entry == input_file) 1409 return input_desc; 1410 1411 if (input_file) file_close (); 1412 1413 if (entry->search_dirs_flag) 1414 { 1415 int i; 1416 1417 for (i = 0; i < n_search_dirs; i++) 1418 { 1419 register char *string 1420 = concat (search_dirs[i], "/", entry->filename); 1421 desc = open (string, O_RDONLY, 0); 1422 if (desc > 0) 1423 { 1424 entry->filename = string; 1425 entry->search_dirs_flag = 0; 1426 break; 1427 } 1428 free (string); 1429 } 1430 } 1431 else 1432 desc = open (entry->filename, O_RDONLY, 0); 1433 1434 if (desc > 0) 1435 { 1436 input_file = entry; 1437 input_desc = desc; 1438 return desc; 1439 } 1440 1441 perror_file (entry); 1442 /* NOTREACHED */ 1443 } 1444 1445 /* Print the filename of ENTRY on OUTFILE (a stdio stream), 1446 and then a newline. */ 1447 1448 void 1449 prline_file_name (entry, outfile) 1450 struct file_entry *entry; 1451 FILE *outfile; 1452 { 1453 print_file_name (entry, outfile); 1454 fprintf (outfile, "\n"); 1455 } 1456 1457 /* Print the filename of ENTRY on OUTFILE (a stdio stream). */ 1458 1459 void 1460 print_file_name (entry, outfile) 1461 struct file_entry *entry; 1462 FILE *outfile; 1463 { 1464 if (entry->superfile) 1465 { 1466 print_file_name (entry->superfile, outfile); 1467 fprintf (outfile, "(%s)", entry->filename); 1468 } 1469 else 1470 fprintf (outfile, "%s", entry->filename); 1471 } 1472 1473 /* Return the filename of entry as a string (malloc'd for the purpose) */ 1474 1475 char * 1476 get_file_name (entry) 1477 struct file_entry *entry; 1478 { 1479 char *result, *supfile; 1480 if (entry->superfile) 1481 { 1482 supfile = get_file_name (entry->superfile); 1483 result = (char *) xmalloc (strlen (supfile) 1484 + strlen (entry->filename) + 3); 1485 sprintf (result, "%s(%s)", supfile, entry->filename); 1486 free (supfile); 1487 } 1488 else 1489 { 1490 result = (char *) xmalloc (strlen (entry->filename) + 1); 1491 strcpy (result, entry->filename); 1492 } 1493 return result; 1494 } 1495 1496 /* Medium-level input routines for rel files. */ 1497 1498 /* Read a file's header into the proper place in the file_entry. 1499 DESC is the descriptor on which the file is open. 1500 ENTRY is the file's entry. */ 1501 1502 void 1503 read_header (desc, entry) 1504 int desc; 1505 register struct file_entry *entry; 1506 { 1507 register int len; 1508 struct exec *loc = (struct exec *) &entry->header; 1509 1510 lseek (desc, entry->starting_offset, 0); 1511 #ifdef COFF_ENCAPSULATE 1512 if (entry->just_syms_flag) 1513 lseek (desc, sizeof(coffheader), 1); 1514 #endif 1515 len = read (desc, loc, sizeof (struct exec)); 1516 if (len != sizeof (struct exec)) 1517 fatal_with_file ("failure reading header of ", entry); 1518 if (N_BADMAG (*loc)) 1519 fatal_with_file ("bad magic number in ", entry); 1520 1521 entry->header_read_flag = 1; 1522 } 1523 1524 /* Read the symbols of file ENTRY into core. 1525 Assume it is already open, on descriptor DESC. 1526 Also read the length of the string table, which follows the symbol table, 1527 but don't read the contents of the string table. */ 1528 1529 void 1530 read_entry_symbols (desc, entry) 1531 struct file_entry *entry; 1532 int desc; 1533 { 1534 int str_size; 1535 1536 if (!entry->header_read_flag) 1537 read_header (desc, entry); 1538 1539 entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms); 1540 1541 lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0); 1542 if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms)) 1543 fatal_with_file ("premature end of file in symbols of ", entry); 1544 1545 lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0); 1546 if (sizeof str_size != read (desc, &str_size, sizeof str_size)) 1547 fatal_with_file ("bad string table size in ", entry); 1548 1549 entry->string_size = str_size; 1550 } 1551 1552 /* Read the string table of file ENTRY into core. 1553 Assume it is already open, on descriptor DESC. 1554 Also record whether a GDB symbol segment follows the string table. */ 1555 1556 void 1557 read_entry_strings (desc, entry) 1558 struct file_entry *entry; 1559 int desc; 1560 { 1561 int buffer; 1562 1563 if (!entry->header_read_flag) 1564 read_header (desc, entry); 1565 1566 lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0); 1567 if (entry->string_size != read (desc, entry->strings, entry->string_size)) 1568 fatal_with_file ("premature end of file in strings of ", entry); 1569 1570 /* While we are here, see if the file has a symbol segment at the end. 1571 For a separate file, just try reading some more. 1572 For a library member, compare current pos against total size. */ 1573 if (entry->superfile) 1574 { 1575 if (entry->total_size == N_STROFF (entry->header) + entry->string_size) 1576 return; 1577 } 1578 else 1579 { 1580 buffer = read (desc, &buffer, sizeof buffer); 1581 if (buffer == 0) 1582 return; 1583 if (buffer != sizeof buffer) 1584 fatal_with_file ("premature end of file in GDB symbol segment of ", entry); 1585 } 1586 /* Don't try to do anything with symsegs. */ 1587 return; 1588 #if 0 1589 /* eliminate warning of `statement not reached'. */ 1590 entry->symseg_offset = N_STROFF (entry->header) + entry->string_size; 1591 #endif 1592 } 1593 1594 /* Read in the symbols of all input files. */ 1595 1596 void read_file_symbols (), read_entry_symbols (), read_entry_strings (); 1597 void enter_file_symbols (), enter_global_ref (), search_library (); 1598 1599 void 1600 load_symbols () 1601 { 1602 register int i; 1603 1604 if (trace_files) fprintf (stderr, "Loading symbols:\n\n"); 1605 1606 for (i = 0; i < number_of_files; i++) 1607 { 1608 register struct file_entry *entry = &file_table[i]; 1609 read_file_symbols (entry); 1610 } 1611 1612 if (trace_files) fprintf (stderr, "\n"); 1613 } 1614 1615 /* If ENTRY is a rel file, read its symbol and string sections into core. 1616 If it is a library, search it and load the appropriate members 1617 (which means calling this function recursively on those members). */ 1618 1619 void 1620 read_file_symbols (entry) 1621 register struct file_entry *entry; 1622 { 1623 register int desc; 1624 register int len; 1625 struct exec hdr; 1626 1627 desc = file_open (entry); 1628 1629 #ifdef COFF_ENCAPSULATE 1630 if (entry->just_syms_flag) 1631 lseek (desc, sizeof(coffheader),0); 1632 #endif 1633 1634 len = read (desc, &hdr, sizeof hdr); 1635 if (len != sizeof hdr) 1636 fatal_with_file ("failure reading header of ", entry); 1637 1638 if (!N_BADMAG (hdr)) 1639 { 1640 read_entry_symbols (desc, entry); 1641 entry->strings = (char *) alloca (entry->string_size); 1642 read_entry_strings (desc, entry); 1643 enter_file_symbols (entry); 1644 entry->strings = 0; 1645 } 1646 else 1647 { 1648 char armag[SARMAG]; 1649 1650 lseek (desc, 0, 0); 1651 if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG)) 1652 fatal_with_file ("malformed input file (not rel or archive) ", entry); 1653 entry->library_flag = 1; 1654 search_library (desc, entry); 1655 } 1656 1657 file_close (); 1658 } 1659 1660 /* Enter the external symbol defs and refs of ENTRY in the hash table. */ 1661 1662 void 1663 enter_file_symbols (entry) 1664 struct file_entry *entry; 1665 { 1666 register struct nlist 1667 *p, 1668 *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 1669 1670 if (trace_files) prline_file_name (entry, stderr); 1671 1672 for (p = entry->symbols; p < end; p++) 1673 { 1674 if (p->n_type == (N_SETV | N_EXT)) continue; 1675 if (set_element_prefixes 1676 && set_element_prefixed_p (p->n_un.n_strx + entry->strings)) 1677 p->n_type += (N_SETA - N_ABS); 1678 1679 if (SET_ELEMENT_P (p->n_type)) 1680 { 1681 set_symbol_count++; 1682 if (!relocatable_output) 1683 enter_global_ref (p, p->n_un.n_strx + entry->strings, entry); 1684 } 1685 else if (p->n_type == N_WARNING) 1686 { 1687 char *name = p->n_un.n_strx + entry->strings; 1688 1689 /* Grab the next entry. */ 1690 p++; 1691 if (p->n_type != (N_UNDF | N_EXT)) 1692 { 1693 fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n", 1694 progname, entry->filename); 1695 make_executable = 0; 1696 p--; /* Process normally. */ 1697 } 1698 else 1699 { 1700 symbol *sp; 1701 char *sname = p->n_un.n_strx + entry->strings; 1702 /* Deal with the warning symbol. */ 1703 enter_global_ref (p, p->n_un.n_strx + entry->strings, entry); 1704 sp = getsym (sname); 1705 sp->warning = (char *) xmalloc (strlen(name) + 1); 1706 strcpy (sp->warning, name); 1707 warning_count++; 1708 } 1709 } 1710 else if (p->n_type & N_EXT) 1711 enter_global_ref (p, p->n_un.n_strx + entry->strings, entry); 1712 else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT))) 1713 { 1714 if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX) 1715 non_L_local_sym_count++; 1716 local_sym_count++; 1717 } 1718 else debugger_sym_count++; 1719 } 1720 1721 /* Count one for the local symbol that we generate, 1722 whose name is the file's name (usually) and whose address 1723 is the start of the file's text. */ 1724 1725 local_sym_count++; 1726 non_L_local_sym_count++; 1727 } 1728 1729 /* Enter one global symbol in the hash table. 1730 NLIST_P points to the `struct nlist' read from the file 1731 that describes the global symbol. NAME is the symbol's name. 1732 ENTRY is the file entry for the file the symbol comes from. 1733 1734 The `struct nlist' is modified by placing it on a chain of 1735 all such structs that refer to the same global symbol. 1736 This chain starts in the `refs' field of the symbol table entry 1737 and is chained through the `n_name'. */ 1738 1739 void 1740 enter_global_ref (nlist_p, name, entry) 1741 register struct nlist *nlist_p; 1742 char *name; 1743 struct file_entry *entry; 1744 { 1745 register symbol *sp = getsym (name); 1746 register int type = nlist_p->n_type; 1747 int oldref = sp->referenced; 1748 int olddef = sp->defined; 1749 1750 nlist_p->n_un.n_name = (char *) sp->refs; 1751 sp->refs = nlist_p; 1752 1753 sp->referenced = 1; 1754 if (type != (N_UNDF | N_EXT) || nlist_p->n_value) 1755 { 1756 if (!sp->defined || sp->defined == (N_UNDF | N_EXT)) 1757 sp->defined = type; 1758 1759 if (oldref && !olddef) 1760 /* It used to be undefined and we're defining it. */ 1761 undefined_global_sym_count--; 1762 1763 if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value) 1764 { 1765 /* First definition and it's common. */ 1766 common_defined_global_count++; 1767 sp->max_common_size = nlist_p->n_value; 1768 } 1769 else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT)) 1770 { 1771 /* It used to be common and we're defining it as 1772 something else. */ 1773 common_defined_global_count--; 1774 sp->max_common_size = 0; 1775 } 1776 else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT) 1777 && sp->max_common_size < nlist_p->n_value) 1778 /* It used to be common and this is a new common entry to 1779 which we need to pay attention. */ 1780 sp->max_common_size = nlist_p->n_value; 1781 1782 /* Are we defining it as a set element? */ 1783 if (SET_ELEMENT_P (type) 1784 && (!olddef || (olddef && sp->max_common_size))) 1785 set_vector_count++; 1786 /* As an indirection? */ 1787 else if (type == (N_INDR | N_EXT)) 1788 { 1789 /* Indirect symbols value should be modified to point 1790 a symbol being equivalenced to. */ 1791 nlist_p->n_value 1792 = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx 1793 + entry->strings); 1794 if ((symbol *) nlist_p->n_value == sp) 1795 { 1796 /* Somebody redefined a symbol to be itself. */ 1797 fprintf (stderr, "%s: Symbol %s indirected to itself.\n", 1798 entry->filename, name); 1799 /* Rewrite this symbol as being a global text symbol 1800 with value 0. */ 1801 nlist_p->n_type = sp->defined = N_TEXT | N_EXT; 1802 nlist_p->n_value = 0; 1803 /* Don't make the output executable. */ 1804 make_executable = 0; 1805 } 1806 else 1807 global_indirect_count++; 1808 } 1809 } 1810 else 1811 if (!oldref) 1812 #ifndef DOLLAR_KLUDGE 1813 undefined_global_sym_count++; 1814 #else 1815 { 1816 if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$') 1817 { 1818 /* This is an (ISI?) $-conditional; skip it */ 1819 sp->referenced = 0; 1820 if (sp->trace) 1821 { 1822 fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name); 1823 print_file_name (entry, stderr); 1824 fprintf (stderr, "\n"); 1825 } 1826 return; 1827 } 1828 else 1829 undefined_global_sym_count++; 1830 } 1831 #endif 1832 1833 if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified) 1834 text_start = nlist_p->n_value; 1835 1836 if (sp->trace) 1837 { 1838 register char *reftype; 1839 switch (type & N_TYPE) 1840 { 1841 case N_UNDF: 1842 if (nlist_p->n_value) 1843 reftype = "defined as common"; 1844 else reftype = "referenced"; 1845 break; 1846 1847 case N_ABS: 1848 reftype = "defined as absolute"; 1849 break; 1850 1851 case N_TEXT: 1852 reftype = "defined in text section"; 1853 break; 1854 1855 case N_DATA: 1856 reftype = "defined in data section"; 1857 break; 1858 1859 case N_BSS: 1860 reftype = "defined in BSS section"; 1861 break; 1862 1863 case N_SETT: 1864 reftype = "is a text set element"; 1865 break; 1866 1867 case N_SETD: 1868 reftype = "is a data set element"; 1869 break; 1870 1871 case N_SETB: 1872 reftype = "is a BSS set element"; 1873 break; 1874 1875 case N_SETA: 1876 reftype = "is an absolute set element"; 1877 break; 1878 1879 case N_SETV: 1880 reftype = "defined in data section as vector"; 1881 break; 1882 1883 case N_INDR: 1884 reftype = (char *) alloca (23 1885 + strlen ((nlist_p + 1)->n_un.n_strx 1886 + entry->strings)); 1887 sprintf (reftype, "defined equivalent to %s", 1888 (nlist_p + 1)->n_un.n_strx + entry->strings); 1889 break; 1890 1891 #ifdef sequent 1892 case N_SHUNDF: 1893 reftype = "shared undf"; 1894 break; 1895 1896 /* These conflict with cases above. 1897 case N_SHDATA: 1898 reftype = "shared data"; 1899 break; 1900 1901 case N_SHBSS: 1902 reftype = "shared BSS"; 1903 break; 1904 */ 1905 default: 1906 reftype = "I don't know this type"; 1907 break; 1908 #endif 1909 } 1910 1911 fprintf (stderr, "symbol %s %s in ", sp->name, reftype); 1912 print_file_name (entry, stderr); 1913 fprintf (stderr, "\n"); 1914 } 1915 } 1916 1917 /* This return 0 if the given file entry's symbol table does *not* 1918 contain the nlist point entry, and it returns the files entry 1919 pointer (cast to unsigned long) if it does. */ 1920 1921 unsigned long 1922 contains_symbol (entry, n_ptr) 1923 struct file_entry *entry; 1924 register struct nlist *n_ptr; 1925 { 1926 if (n_ptr >= entry->symbols && 1927 n_ptr < (entry->symbols 1928 + (entry->header.a_syms / sizeof (struct nlist)))) 1929 return (unsigned long) entry; 1930 return 0; 1931 } 1932 1933 1934 /* Searching libraries */ 1935 1936 struct file_entry *decode_library_subfile (); 1937 void linear_library (), symdef_library (); 1938 1939 /* Search the library ENTRY, already open on descriptor DESC. 1940 This means deciding which library members to load, 1941 making a chain of `struct file_entry' for those members, 1942 and entering their global symbols in the hash table. */ 1943 1944 void 1945 search_library (desc, entry) 1946 int desc; 1947 struct file_entry *entry; 1948 { 1949 int member_length; 1950 register char *name; 1951 register struct file_entry *subentry; 1952 1953 if (!undefined_global_sym_count) return; 1954 1955 /* Examine its first member, which starts SARMAG bytes in. */ 1956 subentry = decode_library_subfile (desc, entry, SARMAG, &member_length); 1957 if (!subentry) return; 1958 1959 name = subentry->filename; 1960 free (subentry); 1961 1962 /* Search via __.SYMDEF if that exists, else linearly. */ 1963 1964 if (!strcmp (name, "__.SYMDEF")) 1965 symdef_library (desc, entry, member_length); 1966 else 1967 linear_library (desc, entry); 1968 } 1969 1970 /* Construct and return a file_entry for a library member. 1971 The library's file_entry is library_entry, and the library is open on DESC. 1972 SUBFILE_OFFSET is the byte index in the library of this member's header. 1973 We store the length of the member into *LENGTH_LOC. */ 1974 1975 struct file_entry * 1976 decode_library_subfile (desc, library_entry, subfile_offset, length_loc) 1977 int desc; 1978 struct file_entry *library_entry; 1979 int subfile_offset; 1980 int *length_loc; 1981 { 1982 int bytes_read; 1983 register int namelen; 1984 int member_length; 1985 register char *name; 1986 struct ar_hdr hdr1; 1987 register struct file_entry *subentry; 1988 1989 lseek (desc, subfile_offset, 0); 1990 1991 bytes_read = read (desc, &hdr1, sizeof hdr1); 1992 if (!bytes_read) 1993 return 0; /* end of archive */ 1994 1995 if (sizeof hdr1 != bytes_read) 1996 fatal_with_file ("malformed library archive ", library_entry); 1997 1998 if (sscanf (hdr1.ar_size, "%d", &member_length) != 1) 1999 fatal_with_file ("malformatted header of archive member in ", library_entry); 2000 2001 subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry)); 2002 bzero (subentry, sizeof (struct file_entry)); 2003 2004 for (namelen = 0; 2005 namelen < sizeof hdr1.ar_name 2006 && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ' 2007 && hdr1.ar_name[namelen] != '/'; 2008 namelen++); 2009 2010 name = (char *) xmalloc (namelen+1); 2011 strncpy (name, hdr1.ar_name, namelen); 2012 name[namelen] = 0; 2013 2014 subentry->filename = name; 2015 subentry->local_sym_name = name; 2016 subentry->symbols = 0; 2017 subentry->strings = 0; 2018 subentry->subfiles = 0; 2019 subentry->starting_offset = subfile_offset + sizeof hdr1; 2020 subentry->superfile = library_entry; 2021 subentry->library_flag = 0; 2022 subentry->header_read_flag = 0; 2023 subentry->just_syms_flag = 0; 2024 subentry->chain = 0; 2025 subentry->total_size = member_length; 2026 2027 (*length_loc) = member_length; 2028 2029 return subentry; 2030 } 2031 2032 int subfile_wanted_p (); 2033 2034 /* Search a library that has a __.SYMDEF member. 2035 DESC is a descriptor on which the library is open. 2036 The file pointer is assumed to point at the __.SYMDEF data. 2037 ENTRY is the library's file_entry. 2038 MEMBER_LENGTH is the length of the __.SYMDEF data. */ 2039 2040 void 2041 symdef_library (desc, entry, member_length) 2042 int desc; 2043 struct file_entry *entry; 2044 int member_length; 2045 { 2046 int *symdef_data = (int *) xmalloc (member_length); 2047 register struct symdef *symdef_base; 2048 char *sym_name_base; 2049 int number_of_symdefs; 2050 int length_of_strings; 2051 int not_finished; 2052 int bytes_read; 2053 register int i; 2054 struct file_entry *prev = 0; 2055 int prev_offset = 0; 2056 2057 bytes_read = read (desc, symdef_data, member_length); 2058 if (bytes_read != member_length) 2059 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2060 2061 number_of_symdefs = *symdef_data / sizeof (struct symdef); 2062 if (number_of_symdefs < 0 || 2063 number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length) 2064 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2065 2066 symdef_base = (struct symdef *) (symdef_data + 1); 2067 length_of_strings = *(int *) (symdef_base + number_of_symdefs); 2068 2069 if (length_of_strings < 0 2070 || number_of_symdefs * sizeof (struct symdef) + length_of_strings 2071 + 2 * sizeof (int) > member_length) 2072 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2073 2074 sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs); 2075 2076 /* Check all the string indexes for validity. */ 2077 2078 for (i = 0; i < number_of_symdefs; i++) 2079 { 2080 register int index = symdef_base[i].symbol_name_string_index; 2081 if (index < 0 || index >= length_of_strings 2082 || (index && *(sym_name_base + index - 1))) 2083 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2084 } 2085 2086 /* Search the symdef data for members to load. 2087 Do this until one whole pass finds nothing to load. */ 2088 2089 not_finished = 1; 2090 while (not_finished) 2091 { 2092 not_finished = 0; 2093 2094 /* Scan all the symbols mentioned in the symdef for ones that we need. 2095 Load the library members that contain such symbols. */ 2096 2097 for (i = 0; 2098 (i < number_of_symdefs 2099 && (undefined_global_sym_count || common_defined_global_count)); 2100 i++) 2101 if (symdef_base[i].symbol_name_string_index >= 0) 2102 { 2103 register symbol *sp; 2104 2105 sp = getsym_soft (sym_name_base 2106 + symdef_base[i].symbol_name_string_index); 2107 2108 /* If we find a symbol that appears to be needed, think carefully 2109 about the archive member that the symbol is in. */ 2110 2111 /* 2112 * Per Mike Karels' recommendation, we no longer load library 2113 * files if the only reference(s) that would be satisfied are 2114 * 'common' references. This prevents some problems with name 2115 * pollution (e.g. a global common 'utime' linked to a function). 2116 */ 2117 if (sp && sp->referenced && !sp->defined) 2118 { 2119 int junk; 2120 register int j; 2121 register int offset = symdef_base[i].library_member_offset; 2122 struct file_entry *subentry; 2123 2124 /* Don't think carefully about any archive member 2125 more than once in a given pass. */ 2126 2127 if (prev_offset == offset) 2128 continue; 2129 prev_offset = offset; 2130 2131 /* Read the symbol table of the archive member. */ 2132 2133 subentry = decode_library_subfile (desc, entry, offset, &junk); 2134 if (subentry == 0) 2135 fatal ("invalid offset for %s in symbol table of %s", 2136 sym_name_base 2137 + symdef_base[i].symbol_name_string_index, 2138 entry->filename); 2139 read_entry_symbols (desc, subentry); 2140 subentry->strings = (char *) malloc (subentry->string_size); 2141 read_entry_strings (desc, subentry); 2142 2143 /* Now scan the symbol table and decide whether to load. */ 2144 2145 if (!subfile_wanted_p (subentry)) 2146 { 2147 free (subentry->symbols); 2148 free (subentry); 2149 } 2150 else 2151 { 2152 /* This member is needed; load it. 2153 Since we are loading something on this pass, 2154 we must make another pass through the symdef data. */ 2155 2156 not_finished = 1; 2157 2158 enter_file_symbols (subentry); 2159 2160 if (prev) 2161 prev->chain = subentry; 2162 else entry->subfiles = subentry; 2163 prev = subentry; 2164 2165 /* Clear out this member's symbols from the symdef data 2166 so that following passes won't waste time on them. */ 2167 2168 for (j = 0; j < number_of_symdefs; j++) 2169 { 2170 if (symdef_base[j].library_member_offset == offset) 2171 symdef_base[j].symbol_name_string_index = -1; 2172 } 2173 } 2174 2175 /* We'll read the strings again if we need them again. */ 2176 free (subentry->strings); 2177 subentry->strings = 0; 2178 } 2179 } 2180 } 2181 2182 free (symdef_data); 2183 } 2184 2185 /* Search a library that has no __.SYMDEF. 2186 ENTRY is the library's file_entry. 2187 DESC is the descriptor it is open on. */ 2188 2189 void 2190 linear_library (desc, entry) 2191 int desc; 2192 struct file_entry *entry; 2193 { 2194 register struct file_entry *prev = 0; 2195 register int this_subfile_offset = SARMAG; 2196 2197 while (undefined_global_sym_count || common_defined_global_count) 2198 { 2199 int member_length; 2200 register struct file_entry *subentry; 2201 2202 subentry = decode_library_subfile (desc, entry, this_subfile_offset, 2203 &member_length); 2204 2205 if (!subentry) return; 2206 2207 read_entry_symbols (desc, subentry); 2208 subentry->strings = (char *) alloca (subentry->string_size); 2209 read_entry_strings (desc, subentry); 2210 2211 if (!subfile_wanted_p (subentry)) 2212 { 2213 free (subentry->symbols); 2214 free (subentry); 2215 } 2216 else 2217 { 2218 enter_file_symbols (subentry); 2219 2220 if (prev) 2221 prev->chain = subentry; 2222 else entry->subfiles = subentry; 2223 prev = subentry; 2224 subentry->strings = 0; /* Since space will dissapear on return */ 2225 } 2226 2227 this_subfile_offset += member_length + sizeof (struct ar_hdr); 2228 if (this_subfile_offset & 1) this_subfile_offset++; 2229 } 2230 } 2231 2232 /* ENTRY is an entry for a library member. 2233 Its symbols have been read into core, but not entered. 2234 Return nonzero if we ought to load this member. */ 2235 2236 int 2237 subfile_wanted_p (entry) 2238 struct file_entry *entry; 2239 { 2240 register struct nlist *p; 2241 register struct nlist *end 2242 = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2243 #ifdef DOLLAR_KLUDGE 2244 register int dollar_cond = 0; 2245 #endif 2246 2247 for (p = entry->symbols; p < end; p++) 2248 { 2249 register int type = p->n_type; 2250 register char *name = p->n_un.n_strx + entry->strings; 2251 2252 /* If the symbol has an interesting definition, we could 2253 potentially want it. */ 2254 if (type & N_EXT 2255 && (type != (N_UNDF | N_EXT) || p->n_value 2256 2257 #ifdef DOLLAR_KLUDGE 2258 || name[1] == '$' 2259 #endif 2260 ) 2261 && !SET_ELEMENT_P (type) 2262 && !set_element_prefixed_p (name)) 2263 { 2264 register symbol *sp = getsym_soft (name); 2265 2266 #ifdef DOLLAR_KLUDGE 2267 if (name[1] == '$') 2268 { 2269 sp = getsym_soft (&name[2]); 2270 dollar_cond = 1; 2271 if (!sp) continue; 2272 if (sp->referenced) 2273 { 2274 if (write_map) 2275 { 2276 print_file_name (entry, stdout); 2277 fprintf (stdout, " needed due to $-conditional %s\n", name); 2278 } 2279 return 1; 2280 } 2281 continue; 2282 } 2283 #endif 2284 2285 /* If this symbol has not been hashed, we can't be looking for it. */ 2286 2287 if (!sp) continue; 2288 2289 /* 2290 * We don't load a file if it merely satisfies a common reference 2291 * (see explanation above in symdef_library()). 2292 */ 2293 if (sp->referenced && !sp->defined) 2294 { 2295 /* This is a symbol we are looking for. It is either 2296 not yet defined or defined as a common. */ 2297 #ifdef DOLLAR_KLUDGE 2298 if (dollar_cond) continue; 2299 #endif 2300 if (type == (N_UNDF | N_EXT)) 2301 { 2302 /* Symbol being defined as common. 2303 Remember this, but don't load subfile just for this. */ 2304 2305 /* If it didn't used to be common, up the count of 2306 common symbols. */ 2307 if (!sp->max_common_size) 2308 common_defined_global_count++; 2309 2310 if (sp->max_common_size < p->n_value) 2311 sp->max_common_size = p->n_value; 2312 if (!sp->defined) 2313 undefined_global_sym_count--; 2314 sp->defined = 1; 2315 continue; 2316 } 2317 2318 if (write_map) 2319 { 2320 print_file_name (entry, stdout); 2321 fprintf (stdout, " needed due to %s\n", sp->name); 2322 } 2323 return 1; 2324 } 2325 } 2326 } 2327 2328 return 0; 2329 } 2330 2331 void consider_file_section_lengths (), relocate_file_addresses (); 2332 2333 /* Having entered all the global symbols and found the sizes of sections 2334 of all files to be linked, make all appropriate deductions from this data. 2335 2336 We propagate global symbol values from definitions to references. 2337 We compute the layout of the output file and where each input file's 2338 contents fit into it. */ 2339 2340 void 2341 digest_symbols () 2342 { 2343 register int i; 2344 int setv_fill_count; 2345 2346 if (trace_files) 2347 fprintf (stderr, "Digesting symbol information:\n\n"); 2348 2349 /* Compute total size of sections */ 2350 2351 each_file (consider_file_section_lengths, 0); 2352 2353 /* If necessary, pad text section to full page in the file. 2354 Include the padding in the text segment size. */ 2355 2356 if (magic == ZMAGIC) 2357 { 2358 int text_end = text_size + N_TXTOFF (outheader); 2359 text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end; 2360 text_size += text_pad; 2361 } 2362 2363 #ifdef _N_BASEADDR 2364 /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */ 2365 outheader.a_entry = N_PAGSIZ (outheader); 2366 #endif 2367 2368 outheader.a_text = text_size; 2369 #ifdef sequent 2370 outheader.a_text += N_ADDRADJ (outheader); 2371 #endif 2372 2373 /* Make the data segment address start in memory on a suitable boundary. */ 2374 2375 if (! Tdata_flag_specified) 2376 data_start = N_DATADDR (outheader) + text_start - TEXT_START (outheader); 2377 2378 /* Set up the set element vector */ 2379 2380 if (!relocatable_output) 2381 { 2382 /* The set sector size is the number of set elements + a word 2383 for each symbol for the length word at the beginning of the 2384 vector, plus a word for each symbol for a zero at the end of 2385 the vector (for incremental linking). */ 2386 set_sect_size 2387 = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long); 2388 set_sect_start = data_start + data_size; 2389 data_size += set_sect_size; 2390 set_vectors = (unsigned long *) xmalloc (set_sect_size); 2391 setv_fill_count = 0; 2392 } 2393 2394 /* Compute start addresses of each file's sections and symbols. */ 2395 2396 each_full_file (relocate_file_addresses, 0); 2397 2398 /* Now, for each symbol, verify that it is defined globally at most once. 2399 Put the global value into the symbol entry. 2400 Common symbols are allocated here, in the BSS section. 2401 Each defined symbol is given a '->defined' field 2402 which is the correct N_ code for its definition, 2403 except in the case of common symbols with -r. 2404 Then make all the references point at the symbol entry 2405 instead of being chained together. */ 2406 2407 defined_global_sym_count = 0; 2408 2409 for (i = 0; i < TABSIZE; i++) 2410 { 2411 register symbol *sp; 2412 for (sp = symtab[i]; sp; sp = sp->link) 2413 { 2414 /* For each symbol */ 2415 register struct nlist *p, *next; 2416 int defs = 0, com = sp->max_common_size; 2417 struct nlist *first_definition; 2418 for (p = sp->refs; p; p = next) 2419 { 2420 register int type = p->n_type; 2421 2422 if (SET_ELEMENT_P (type)) 2423 { 2424 if (relocatable_output) 2425 fatal ("internal: global ref to set element with -r"); 2426 if (!defs++) 2427 { 2428 sp->value = set_sect_start 2429 + setv_fill_count++ * sizeof (unsigned long); 2430 sp->defined = N_SETV | N_EXT; 2431 first_definition = p; 2432 } 2433 else if ((sp->defined & ~N_EXT) != N_SETV) 2434 { 2435 sp->multiply_defined = 1; 2436 multiple_def_count++; 2437 } 2438 set_vectors[setv_fill_count++] = p->n_value; 2439 } 2440 else if ((type & N_EXT) && type != (N_UNDF | N_EXT)) 2441 { 2442 /* non-common definition */ 2443 if (defs++ && sp->value != p->n_value) 2444 { 2445 sp->multiply_defined = 1; 2446 multiple_def_count++; 2447 } 2448 sp->value = p->n_value; 2449 sp->defined = type; 2450 first_definition = p; 2451 } 2452 next = (struct nlist *) p->n_un.n_name; 2453 p->n_un.n_name = (char *) sp; 2454 } 2455 /* Allocate as common if defined as common and not defined for real */ 2456 if (com && !defs) 2457 { 2458 if (!relocatable_output || force_common_definition) 2459 { 2460 int align = sizeof (int); 2461 2462 /* Round up to nearest sizeof (int). I don't know 2463 whether this is necessary or not (given that 2464 alignment is taken care of later), but it's 2465 traditional, so I'll leave it in. Note that if 2466 this size alignment is ever removed, ALIGN above 2467 will have to be initialized to 1 instead of 2468 sizeof (int). */ 2469 2470 com = (com + sizeof (int) - 1) & (- sizeof (int)); 2471 2472 while (!(com & align)) 2473 align <<= 1; 2474 2475 align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align; 2476 2477 bss_size = ((((bss_size + data_size + data_start) 2478 + (align - 1)) & (- align)) 2479 - data_size - data_start); 2480 2481 sp->value = data_start + data_size + bss_size; 2482 sp->defined = N_BSS | N_EXT; 2483 bss_size += com; 2484 if (write_map) 2485 printf ("Allocating common %s: %x at %x\n", 2486 sp->name, com, sp->value); 2487 } 2488 else 2489 { 2490 sp->defined = 0; 2491 undefined_global_sym_count++; 2492 } 2493 } 2494 /* Set length word at front of vector and zero byte at end. 2495 Reverse the vector itself to put it in file order. */ 2496 if ((sp->defined & ~N_EXT) == N_SETV) 2497 { 2498 unsigned long length_word_index 2499 = (sp->value - set_sect_start) / sizeof (unsigned long); 2500 unsigned long i, tmp; 2501 2502 set_vectors[length_word_index] 2503 = setv_fill_count - 1 - length_word_index; 2504 2505 /* Reverse the vector. */ 2506 for (i = 1; 2507 i < (setv_fill_count - length_word_index - 1) / 2 + 1; 2508 i++) 2509 { 2510 tmp = set_vectors[length_word_index + i]; 2511 set_vectors[length_word_index + i] 2512 = set_vectors[setv_fill_count - i]; 2513 set_vectors[setv_fill_count - i] = tmp; 2514 } 2515 2516 set_vectors[setv_fill_count++] = 0; 2517 } 2518 if (sp->defined) 2519 defined_global_sym_count++; 2520 } 2521 } 2522 2523 if (end_symbol) /* These are null if -r. */ 2524 { 2525 etext_symbol->value = text_size + text_start; 2526 edata_symbol->value = data_start + data_size; 2527 end_symbol->value = data_start + data_size + bss_size; 2528 } 2529 2530 /* Figure the data_pad now, so that it overlaps with the bss addresses. */ 2531 2532 if (specified_data_size && specified_data_size > data_size) 2533 data_pad = specified_data_size - data_size; 2534 2535 if (magic == ZMAGIC) 2536 data_pad = ((data_pad + data_size + page_size - 1) & (- page_size)) 2537 - data_size; 2538 2539 bss_size -= data_pad; 2540 if (bss_size < 0) bss_size = 0; 2541 2542 data_size += data_pad; 2543 } 2544 2545 /* Accumulate the section sizes of input file ENTRY 2546 into the section sizes of the output file. */ 2547 2548 void 2549 consider_file_section_lengths (entry) 2550 register struct file_entry *entry; 2551 { 2552 if (entry->just_syms_flag) 2553 return; 2554 2555 entry->text_start_address = text_size; 2556 /* If there were any vectors, we need to chop them off */ 2557 text_size += entry->header.a_text; 2558 entry->data_start_address = data_size; 2559 data_size += entry->header.a_data; 2560 entry->bss_start_address = bss_size; 2561 bss_size += entry->header.a_bss; 2562 2563 text_reloc_size += entry->header.a_trsize; 2564 data_reloc_size += entry->header.a_drsize; 2565 } 2566 2567 /* Determine where the sections of ENTRY go into the output file, 2568 whose total section sizes are already known. 2569 Also relocate the addresses of the file's local and debugger symbols. */ 2570 2571 void 2572 relocate_file_addresses (entry) 2573 register struct file_entry *entry; 2574 { 2575 entry->text_start_address += text_start; 2576 /* Note that `data_start' and `data_size' have not yet been 2577 adjusted for `data_pad'. If they had been, we would get the wrong 2578 results here. */ 2579 entry->data_start_address += data_start; 2580 entry->bss_start_address += data_start + data_size; 2581 2582 { 2583 register struct nlist *p; 2584 register struct nlist *end 2585 = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2586 2587 for (p = entry->symbols; p < end; p++) 2588 { 2589 /* If this belongs to a section, update it by the section's start address */ 2590 register int type = p->n_type & N_TYPE; 2591 2592 switch (type) 2593 { 2594 case N_TEXT: 2595 case N_SETT: 2596 p->n_value += entry->text_start_address; 2597 break; 2598 case N_DATA: 2599 case N_SETV: 2600 case N_SETD: 2601 /* A symbol whose value is in the data section 2602 is present in the input file as if the data section 2603 started at an address equal to the length of the file's text. */ 2604 p->n_value += entry->data_start_address - entry->header.a_text; 2605 break; 2606 case N_BSS: 2607 case N_SETB: 2608 /* likewise for symbols with value in BSS. */ 2609 p->n_value += entry->bss_start_address 2610 - entry->header.a_text - entry->header.a_data; 2611 break; 2612 } 2613 } 2614 } 2615 } 2616 2617 void describe_file_sections (), list_file_locals (); 2618 2619 /* Print a complete or partial map of the output file. */ 2620 2621 void 2622 print_symbols (outfile) 2623 FILE *outfile; 2624 { 2625 register int i; 2626 2627 fprintf (outfile, "\nFiles:\n\n"); 2628 2629 each_file (describe_file_sections, outfile); 2630 2631 fprintf (outfile, "\nGlobal symbols:\n\n"); 2632 2633 for (i = 0; i < TABSIZE; i++) 2634 { 2635 register symbol *sp; 2636 for (sp = symtab[i]; sp; sp = sp->link) 2637 { 2638 if (sp->defined == 1) 2639 fprintf (outfile, " %s: common, length 0x%x\n", sp->name, sp->max_common_size); 2640 if (sp->defined) 2641 fprintf (outfile, " %s: 0x%x\n", sp->name, sp->value); 2642 else if (sp->referenced) 2643 fprintf (outfile, " %s: undefined\n", sp->name); 2644 } 2645 } 2646 2647 each_file (list_file_locals, outfile); 2648 } 2649 2650 void 2651 describe_file_sections (entry, outfile) 2652 struct file_entry *entry; 2653 FILE *outfile; 2654 { 2655 fprintf (outfile, " "); 2656 print_file_name (entry, outfile); 2657 if (entry->just_syms_flag) 2658 fprintf (outfile, " symbols only\n", 0); 2659 else 2660 fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n", 2661 entry->text_start_address, entry->header.a_text, 2662 entry->data_start_address, entry->header.a_data, 2663 entry->bss_start_address, entry->header.a_bss); 2664 } 2665 2666 void 2667 list_file_locals (entry, outfile) 2668 struct file_entry *entry; 2669 FILE *outfile; 2670 { 2671 register struct nlist 2672 *p, 2673 *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2674 2675 entry->strings = (char *) alloca (entry->string_size); 2676 read_entry_strings (file_open (entry), entry); 2677 2678 fprintf (outfile, "\nLocal symbols of "); 2679 print_file_name (entry, outfile); 2680 fprintf (outfile, ":\n\n"); 2681 2682 for (p = entry->symbols; p < end; p++) 2683 /* If this is a definition, 2684 update it if necessary by this file's start address. */ 2685 if (!(p->n_type & (N_STAB | N_EXT))) 2686 fprintf (outfile, " %s: 0x%x\n", 2687 entry->strings + p->n_un.n_strx, p->n_value); 2688 2689 entry->strings = 0; /* All done with them. */ 2690 } 2691 2692 2693 /* Static vars for do_warnings and subroutines of it */ 2694 int list_unresolved_refs; /* List unresolved refs */ 2695 int list_warning_symbols; /* List warning syms */ 2696 int list_multiple_defs; /* List multiple definitions */ 2697 2698 /* 2699 * Structure for communication between do_file_warnings and it's 2700 * helper routines. Will in practice be an array of three of these: 2701 * 0) Current line, 1) Next line, 2) Source file info. 2702 */ 2703 struct line_debug_entry 2704 { 2705 int line; 2706 char *filename; 2707 struct nlist *sym; 2708 }; 2709 2710 void qsort (); 2711 /* 2712 * Helper routines for do_file_warnings. 2713 */ 2714 2715 /* Return an integer less than, equal to, or greater than 0 as per the 2716 relation between the two relocation entries. Used by qsort. */ 2717 2718 int 2719 relocation_entries_relation (rel1, rel2) 2720 struct relocation_info *rel1, *rel2; 2721 { 2722 return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2); 2723 } 2724 2725 /* Moves to the next debugging symbol in the file. USE_DATA_SYMBOLS 2726 determines the type of the debugging symbol to look for (DSLINE or 2727 SLINE). STATE_POINTER keeps track of the old and new locatiosn in 2728 the file. It assumes that state_pointer[1] is valid; ie 2729 that it.sym points into some entry in the symbol table. If 2730 state_pointer[1].sym == 0, this routine should not be called. */ 2731 2732 int 2733 next_debug_entry (use_data_symbols, state_pointer) 2734 register int use_data_symbols; 2735 /* Next must be passed by reference! */ 2736 struct line_debug_entry state_pointer[3]; 2737 { 2738 register struct line_debug_entry 2739 *current = state_pointer, 2740 *next = state_pointer + 1, 2741 /* Used to store source file */ 2742 *source = state_pointer + 2; 2743 struct file_entry *entry = (struct file_entry *) source->sym; 2744 2745 current->sym = next->sym; 2746 current->line = next->line; 2747 current->filename = next->filename; 2748 2749 while (++(next->sym) < (entry->symbols 2750 + entry->header.a_syms/sizeof (struct nlist))) 2751 { 2752 /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so 2753 * may look negative...therefore, must mask to low bits 2754 */ 2755 switch (next->sym->n_type & 0xff) 2756 { 2757 case N_SLINE: 2758 if (use_data_symbols) continue; 2759 next->line = next->sym->n_desc; 2760 return 1; 2761 case N_DSLINE: 2762 if (!use_data_symbols) continue; 2763 next->line = next->sym->n_desc; 2764 return 1; 2765 #ifdef HAVE_SUN_STABS 2766 case N_EINCL: 2767 next->filename = source->filename; 2768 continue; 2769 #endif 2770 case N_SO: 2771 source->filename = next->sym->n_un.n_strx + entry->strings; 2772 source->line++; 2773 #ifdef HAVE_SUN_STABS 2774 case N_BINCL: 2775 #endif 2776 case N_SOL: 2777 next->filename 2778 = next->sym->n_un.n_strx + entry->strings; 2779 default: 2780 continue; 2781 } 2782 } 2783 next->sym = (struct nlist *) 0; 2784 return 0; 2785 } 2786 2787 /* Create a structure to save the state of a scan through the debug 2788 symbols. USE_DATA_SYMBOLS is set if we should be scanning for 2789 DSLINE's instead of SLINE's. entry is the file entry which points 2790 at the symbols to use. */ 2791 2792 struct line_debug_entry * 2793 init_debug_scan (use_data_symbols, entry) 2794 int use_data_symbols; 2795 struct file_entry *entry; 2796 { 2797 struct line_debug_entry 2798 *state_pointer 2799 = (struct line_debug_entry *) 2800 xmalloc (3 * sizeof (struct line_debug_entry)); 2801 register struct line_debug_entry 2802 *current = state_pointer, 2803 *next = state_pointer + 1, 2804 *source = state_pointer + 2; /* Used to store source file */ 2805 2806 struct nlist *tmp; 2807 2808 for (tmp = entry->symbols; 2809 tmp < (entry->symbols 2810 + entry->header.a_syms/sizeof (struct nlist)); 2811 tmp++) 2812 if (tmp->n_type == (int) N_SO) 2813 break; 2814 2815 if (tmp >= (entry->symbols 2816 + entry->header.a_syms/sizeof (struct nlist))) 2817 { 2818 /* I believe this translates to "We lose" */ 2819 current->filename = next->filename = entry->filename; 2820 current->line = next->line = -1; 2821 current->sym = next->sym = (struct nlist *) 0; 2822 return state_pointer; 2823 } 2824 2825 next->line = source->line = 0; 2826 next->filename = source->filename 2827 = (tmp->n_un.n_strx + entry->strings); 2828 source->sym = (struct nlist *) entry; 2829 next->sym = tmp; 2830 2831 next_debug_entry (use_data_symbols, state_pointer); /* To setup next */ 2832 2833 if (!next->sym) /* No line numbers for this section; */ 2834 /* setup output results as appropriate */ 2835 { 2836 if (source->line) 2837 { 2838 current->filename = source->filename = entry->filename; 2839 current->line = -1; /* Don't print lineno */ 2840 } 2841 else 2842 { 2843 current->filename = source->filename; 2844 current->line = 0; 2845 } 2846 return state_pointer; 2847 } 2848 2849 2850 next_debug_entry (use_data_symbols, state_pointer); /* To setup current */ 2851 2852 return state_pointer; 2853 } 2854 2855 /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER 2856 which describes the current location in the implied scan through 2857 the debug symbols within the file which ADDRESS is within, and 2858 returns the source line number which corresponds to ADDRESS. */ 2859 2860 int 2861 address_to_line (address, state_pointer) 2862 unsigned long address; 2863 /* Next must be passed by reference! */ 2864 struct line_debug_entry state_pointer[3]; 2865 { 2866 struct line_debug_entry 2867 *current = state_pointer, 2868 *next = state_pointer + 1; 2869 struct line_debug_entry *tmp_pointer; 2870 2871 int use_data_symbols; 2872 2873 if (next->sym) 2874 use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA; 2875 else 2876 return current->line; 2877 2878 /* Go back to the beginning if we've already passed it. */ 2879 if (current->sym->n_value > address) 2880 { 2881 tmp_pointer = init_debug_scan (use_data_symbols, 2882 (struct file_entry *) 2883 ((state_pointer + 2)->sym)); 2884 state_pointer[0] = tmp_pointer[0]; 2885 state_pointer[1] = tmp_pointer[1]; 2886 state_pointer[2] = tmp_pointer[2]; 2887 free (tmp_pointer); 2888 } 2889 2890 /* If we're still in a bad way, return -1, meaning invalid line. */ 2891 if (current->sym->n_value > address) 2892 return -1; 2893 2894 while (next->sym 2895 && next->sym->n_value <= address 2896 && next_debug_entry (use_data_symbols, state_pointer)) 2897 ; 2898 return current->line; 2899 } 2900 2901 2902 /* Macros for manipulating bitvectors. */ 2903 #define BIT_SET_P(bv, index) ((bv)[(index) >> 3] & 1 << ((index) & 0x7)) 2904 #define SET_BIT(bv, index) ((bv)[(index) >> 3] |= 1 << ((index) & 0x7)) 2905 2906 /* This routine will scan through the relocation data of file ENTRY, 2907 printing out references to undefined symbols and references to 2908 symbols defined in files with N_WARNING symbols. If DATA_SEGMENT 2909 is non-zero, it will scan the data relocation segment (and use 2910 N_DSLINE symbols to track line number); otherwise it will scan the 2911 text relocation segment. Warnings will be printed on the output 2912 stream OUTFILE. Eventually, every nlist symbol mapped through will 2913 be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when 2914 we scan the nlists themselves. */ 2915 2916 do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector) 2917 struct file_entry *entry; 2918 int data_segment; 2919 FILE *outfile; 2920 unsigned char *nlist_bitvector; 2921 { 2922 struct relocation_info 2923 *reloc_start = data_segment ? entry->datarel : entry->textrel, 2924 *reloc; 2925 int reloc_size 2926 = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize) 2927 / sizeof (struct relocation_info)); 2928 int start_of_segment 2929 = (data_segment ? entry->data_start_address : entry->text_start_address); 2930 struct nlist *start_of_syms = entry->symbols; 2931 struct line_debug_entry *state_pointer 2932 = init_debug_scan (data_segment != 0, entry); 2933 register struct line_debug_entry 2934 *current = state_pointer; 2935 /* Assigned to generally static values; should not be written into. */ 2936 char *errfmt; 2937 /* Assigned to alloca'd values cand copied into; should be freed 2938 when done. */ 2939 char *errmsg; 2940 int invalidate_line_number; 2941 2942 /* We need to sort the relocation info here. Sheesh, so much effort 2943 for one lousy error optimization. */ 2944 2945 qsort (reloc_start, reloc_size, sizeof (struct relocation_info), 2946 relocation_entries_relation); 2947 2948 for (reloc = reloc_start; 2949 reloc < (reloc_start + reloc_size); 2950 reloc++) 2951 { 2952 register struct nlist *s; 2953 register symbol *g; 2954 2955 /* If the relocation isn't resolved through a symbol, continue */ 2956 if (!RELOC_EXTERN_P(reloc)) 2957 continue; 2958 2959 s = &(entry->symbols[RELOC_SYMBOL(reloc)]); 2960 2961 /* Local symbols shouldn't ever be used by relocation info, so 2962 the next should be safe. 2963 This is, of course, wrong. References to local BSS symbols can be 2964 the targets of relocation info, and they can (must) be 2965 resolved through symbols. However, these must be defined properly, 2966 (the assembler would have caught it otherwise), so we can 2967 ignore these cases. */ 2968 if (!(s->n_type & N_EXT)) 2969 continue; 2970 2971 g = (symbol *) s->n_un.n_name; 2972 errmsg = 0; 2973 2974 if (!g->defined && list_unresolved_refs) /* Reference */ 2975 { 2976 /* Mark as being noted by relocation warning pass. */ 2977 SET_BIT (nlist_bitvector, s - start_of_syms); 2978 2979 if (g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */ 2980 continue; 2981 2982 /* Undefined symbol which we should mention */ 2983 2984 if (++(g->undef_refs) == MAX_UREFS_PRINTED) 2985 { 2986 errfmt = "More undefined symbol %s refs follow"; 2987 invalidate_line_number = 1; 2988 } 2989 else 2990 { 2991 errfmt = "Undefined symbol %s referenced from %s segment"; 2992 invalidate_line_number = 0; 2993 } 2994 } 2995 else /* Defined */ 2996 { 2997 /* Potential symbol warning here */ 2998 if (!g->warning) continue; 2999 3000 /* Mark as being noted by relocation warning pass. */ 3001 SET_BIT (nlist_bitvector, s - start_of_syms); 3002 3003 errfmt = 0; 3004 errmsg = g->warning; 3005 invalidate_line_number = 0; 3006 } 3007 3008 3009 /* If errfmt == 0, errmsg has already been defined. */ 3010 if (errfmt != 0) 3011 { 3012 char *nm; 3013 3014 if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL) 3015 nm = g->name; 3016 errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1); 3017 sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text"); 3018 if (nm != g->name) 3019 free (nm); 3020 } 3021 3022 address_to_line (RELOC_ADDRESS (reloc) + start_of_segment, 3023 state_pointer); 3024 3025 if (current->line >=0) 3026 fprintf (outfile, "%s:%d: %s\n", current->filename, 3027 invalidate_line_number ? 0 : current->line, errmsg); 3028 else 3029 fprintf (outfile, "%s: %s\n", current->filename, errmsg); 3030 3031 if (errfmt != 0) 3032 free (errmsg); 3033 } 3034 3035 free (state_pointer); 3036 } 3037 3038 /* Print on OUTFILE a list of all warnings generated by references 3039 and/or definitions in the file ENTRY. List source file and line 3040 number if possible, just the .o file if not. */ 3041 3042 void 3043 do_file_warnings (entry, outfile) 3044 struct file_entry *entry; 3045 FILE *outfile; 3046 { 3047 int number_of_syms = entry->header.a_syms / sizeof (struct nlist); 3048 unsigned char *nlist_bitvector 3049 = (unsigned char *) alloca ((number_of_syms >> 3) + 1); 3050 struct line_debug_entry *text_scan, *data_scan; 3051 int i; 3052 char *errfmt, *file_name; 3053 int line_number; 3054 int dont_allow_symbol_name; 3055 3056 bzero (nlist_bitvector, (number_of_syms >> 3) + 1); 3057 3058 /* Read in the files strings if they aren't available */ 3059 if (!entry->strings) 3060 { 3061 int desc; 3062 3063 entry->strings = (char *) alloca (entry->string_size); 3064 desc = file_open (entry); 3065 read_entry_strings (desc, entry); 3066 } 3067 3068 read_file_relocation (entry); 3069 3070 /* Do text warnings based on a scan through the relocation info. */ 3071 do_relocation_warnings (entry, 0, outfile, nlist_bitvector); 3072 3073 /* Do data warnings based on a scan through the relocation info. */ 3074 do_relocation_warnings (entry, 1, outfile, nlist_bitvector); 3075 3076 /* Scan through all of the nlist entries in this file and pick up 3077 anything that the scan through the relocation stuff didn't. */ 3078 3079 text_scan = init_debug_scan (0, entry); 3080 data_scan = init_debug_scan (1, entry); 3081 3082 for (i = 0; i < number_of_syms; i++) 3083 { 3084 struct nlist *s; 3085 struct glosym *g; 3086 3087 s = entry->symbols + i; 3088 3089 if (!(s->n_type & N_EXT)) 3090 continue; 3091 3092 g = (symbol *) s->n_un.n_name; 3093 dont_allow_symbol_name = 0; 3094 3095 if (list_multiple_defs && g->multiply_defined) 3096 { 3097 errfmt = "Definition of symbol %s (multiply defined)"; 3098 switch (s->n_type) 3099 { 3100 case N_TEXT | N_EXT: 3101 line_number = address_to_line (s->n_value, text_scan); 3102 file_name = text_scan[0].filename; 3103 break; 3104 case N_DATA | N_EXT: 3105 line_number = address_to_line (s->n_value, data_scan); 3106 file_name = data_scan[0].filename; 3107 break; 3108 case N_SETA | N_EXT: 3109 case N_SETT | N_EXT: 3110 case N_SETD | N_EXT: 3111 case N_SETB | N_EXT: 3112 if (g->multiply_defined == 2) 3113 continue; 3114 errfmt = "First set element definition of symbol %s (multiply defined)"; 3115 break; 3116 default: 3117 continue; /* Don't print out multiple defs 3118 at references. */ 3119 } 3120 } 3121 else if (BIT_SET_P (nlist_bitvector, i)) 3122 continue; 3123 else if (list_unresolved_refs && !g->defined) 3124 { 3125 if (g->undef_refs >= MAX_UREFS_PRINTED) 3126 continue; 3127 3128 if (++(g->undef_refs) == MAX_UREFS_PRINTED) 3129 errfmt = "More undefined \"%s\" refs follow"; 3130 else 3131 errfmt = "Undefined symbol \"%s\" referenced"; 3132 line_number = -1; 3133 } 3134 else if (g->warning) 3135 { 3136 /* There are two cases in which we don't want to 3137 do this. The first is if this is a definition instead of 3138 a reference. The second is if it's the reference used by 3139 the warning stabs itself. */ 3140 if (s->n_type != (N_EXT | N_UNDF) 3141 || (i && (s-1)->n_type == N_WARNING)) 3142 continue; 3143 3144 errfmt = g->warning; 3145 line_number = -1; 3146 dont_allow_symbol_name = 1; 3147 } 3148 else 3149 continue; 3150 3151 if (line_number == -1) 3152 fprintf (outfile, "%s: ", entry->filename); 3153 else 3154 fprintf (outfile, "%s:%d: ", file_name, line_number); 3155 3156 if (dont_allow_symbol_name) 3157 fprintf (outfile, "%s", errfmt); 3158 else 3159 { 3160 char *nm; 3161 if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL) 3162 { 3163 fprintf (outfile, errfmt, nm); 3164 free (nm); 3165 } 3166 else 3167 fprintf (outfile, errfmt, g->name); 3168 } 3169 3170 fputc ('\n', outfile); 3171 } 3172 free (text_scan); 3173 free (data_scan); 3174 entry->strings = 0; /* Since it will dissapear anyway. */ 3175 } 3176 3177 do_warnings (outfile) 3178 FILE *outfile; 3179 { 3180 list_unresolved_refs = !relocatable_output && undefined_global_sym_count; 3181 list_warning_symbols = warning_count; 3182 list_multiple_defs = multiple_def_count != 0; 3183 3184 if (!(list_unresolved_refs || 3185 list_warning_symbols || 3186 list_multiple_defs )) 3187 /* No need to run this routine */ 3188 return; 3189 3190 each_file (do_file_warnings, outfile); 3191 3192 if (list_unresolved_refs || list_multiple_defs) 3193 make_executable = 0; 3194 } 3195 3196 /* Write the output file */ 3197 3198 void 3199 write_output () 3200 { 3201 struct stat statbuf; 3202 int filemode; 3203 3204 (void) unlink (output_filename); 3205 outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 3206 if (outdesc < 0) perror_name (output_filename); 3207 3208 if (fstat (outdesc, &statbuf) < 0) 3209 perror_name (output_filename); 3210 3211 filemode = statbuf.st_mode; 3212 3213 chmod (output_filename, filemode & ~0111); 3214 3215 /* Output the a.out header. */ 3216 write_header (); 3217 3218 /* Output the text and data segments, relocating as we go. */ 3219 write_text (); 3220 write_data (); 3221 3222 /* Output the merged relocation info, if requested with `-r'. */ 3223 if (relocatable_output) 3224 write_rel (); 3225 3226 /* Output the symbol table (both globals and locals). */ 3227 write_syms (); 3228 3229 /* Copy any GDB symbol segments from input files. */ 3230 write_symsegs (); 3231 3232 close (outdesc); 3233 3234 if (chmod (output_filename, filemode | 0111) == -1) 3235 perror_name (output_filename); 3236 } 3237 3238 void modify_location (), perform_relocation (), copy_text (), copy_data (); 3239 3240 void 3241 write_header () 3242 { 3243 N_SET_MAGIC (outheader, magic); 3244 outheader.a_text = text_size; 3245 #ifdef sequent 3246 outheader.a_text += N_ADDRADJ (outheader); 3247 if (entry_symbol == 0) 3248 entry_symbol = getsym("start"); 3249 #endif 3250 outheader.a_data = data_size; 3251 outheader.a_bss = bss_size; 3252 outheader.a_entry = (entry_symbol ? entry_symbol->value 3253 : text_start + entry_offset); 3254 #ifdef COFF_ENCAPSULATE 3255 if (need_coff_header) 3256 { 3257 /* We are encapsulating BSD format within COFF format. */ 3258 struct coffscn *tp, *dp, *bp; 3259 3260 tp = &coffheader.scns[0]; 3261 dp = &coffheader.scns[1]; 3262 bp = &coffheader.scns[2]; 3263 3264 strcpy (tp->s_name, ".text"); 3265 tp->s_paddr = text_start; 3266 tp->s_vaddr = text_start; 3267 tp->s_size = text_size; 3268 tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec); 3269 tp->s_relptr = 0; 3270 tp->s_lnnoptr = 0; 3271 tp->s_nreloc = 0; 3272 tp->s_nlnno = 0; 3273 tp->s_flags = 0x20; 3274 strcpy (dp->s_name, ".data"); 3275 dp->s_paddr = data_start; 3276 dp->s_vaddr = data_start; 3277 dp->s_size = data_size; 3278 dp->s_scnptr = tp->s_scnptr + tp->s_size; 3279 dp->s_relptr = 0; 3280 dp->s_lnnoptr = 0; 3281 dp->s_nreloc = 0; 3282 dp->s_nlnno = 0; 3283 dp->s_flags = 0x40; 3284 strcpy (bp->s_name, ".bss"); 3285 bp->s_paddr = dp->s_vaddr + dp->s_size; 3286 bp->s_vaddr = bp->s_paddr; 3287 bp->s_size = bss_size; 3288 bp->s_scnptr = 0; 3289 bp->s_relptr = 0; 3290 bp->s_lnnoptr = 0; 3291 bp->s_nreloc = 0; 3292 bp->s_nlnno = 0; 3293 bp->s_flags = 0x80; 3294 3295 coffheader.f_magic = COFF_MAGIC; 3296 coffheader.f_nscns = 3; 3297 /* store an unlikely time so programs can 3298 * tell that there is a bsd header 3299 */ 3300 coffheader.f_timdat = 1; 3301 coffheader.f_symptr = 0; 3302 coffheader.f_nsyms = 0; 3303 coffheader.f_opthdr = 28; 3304 coffheader.f_flags = 0x103; 3305 /* aouthdr */ 3306 coffheader.magic = ZMAGIC; 3307 coffheader.vstamp = 0; 3308 coffheader.tsize = tp->s_size; 3309 coffheader.dsize = dp->s_size; 3310 coffheader.bsize = bp->s_size; 3311 coffheader.entry = outheader.a_entry; 3312 coffheader.text_start = tp->s_vaddr; 3313 coffheader.data_start = dp->s_vaddr; 3314 } 3315 #endif 3316 3317 #ifdef INITIALIZE_HEADER 3318 INITIALIZE_HEADER; 3319 #endif 3320 3321 if (strip_symbols == STRIP_ALL) 3322 nsyms = 0; 3323 else 3324 { 3325 nsyms = (defined_global_sym_count 3326 + undefined_global_sym_count); 3327 if (discard_locals == DISCARD_L) 3328 nsyms += non_L_local_sym_count; 3329 else if (discard_locals == DISCARD_NONE) 3330 nsyms += local_sym_count; 3331 /* One extra for following reference on indirects */ 3332 if (relocatable_output) 3333 nsyms += set_symbol_count + global_indirect_count; 3334 } 3335 3336 if (strip_symbols == STRIP_NONE) 3337 nsyms += debugger_sym_count; 3338 3339 outheader.a_syms = nsyms * sizeof (struct nlist); 3340 3341 if (relocatable_output) 3342 { 3343 outheader.a_trsize = text_reloc_size; 3344 outheader.a_drsize = data_reloc_size; 3345 } 3346 else 3347 { 3348 outheader.a_trsize = 0; 3349 outheader.a_drsize = 0; 3350 } 3351 3352 #ifdef COFF_ENCAPSULATE 3353 if (need_coff_header) 3354 mywrite (&coffheader, sizeof coffheader, 1, outdesc); 3355 #endif 3356 mywrite (&outheader, sizeof (struct exec), 1, outdesc); 3357 3358 /* Output whatever padding is required in the executable file 3359 between the header and the start of the text. */ 3360 3361 #ifndef COFF_ENCAPSULATE 3362 padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc); 3363 #endif 3364 } 3365 3366 /* Relocate the text segment of each input file 3367 and write to the output file. */ 3368 3369 void 3370 write_text () 3371 { 3372 if (trace_files) 3373 fprintf (stderr, "Copying and relocating text:\n\n"); 3374 3375 each_full_file (copy_text, 0); 3376 file_close (); 3377 3378 if (trace_files) 3379 fprintf (stderr, "\n"); 3380 3381 padfile (text_pad, outdesc); 3382 } 3383 3384 int 3385 text_offset (entry) 3386 struct file_entry *entry; 3387 { 3388 return entry->starting_offset + N_TXTOFF (entry->header); 3389 } 3390 3391 /* Read in all of the relocation information */ 3392 3393 void 3394 read_relocation () 3395 { 3396 each_full_file (read_file_relocation, 0); 3397 } 3398 3399 /* Read in the relocation sections of ENTRY if necessary */ 3400 3401 void 3402 read_file_relocation (entry) 3403 struct file_entry *entry; 3404 { 3405 register struct relocation_info *reloc; 3406 int desc; 3407 int read_return; 3408 3409 desc = -1; 3410 if (!entry->textrel) 3411 { 3412 reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize); 3413 desc = file_open (entry); 3414 lseek (desc, 3415 text_offset (entry) + entry->header.a_text + entry->header.a_data, 3416 L_SET); 3417 if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize))) 3418 { 3419 fprintf (stderr, "Return from read: %d\n", read_return); 3420 fatal_with_file ("premature eof in text relocation of ", entry); 3421 } 3422 entry->textrel = reloc; 3423 } 3424 3425 if (!entry->datarel) 3426 { 3427 reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize); 3428 if (desc == -1) desc = file_open (entry); 3429 lseek (desc, 3430 text_offset (entry) + entry->header.a_text 3431 + entry->header.a_data + entry->header.a_trsize, 3432 L_SET); 3433 if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize)) 3434 fatal_with_file ("premature eof in data relocation of ", entry); 3435 entry->datarel = reloc; 3436 } 3437 } 3438 3439 /* Read the text segment contents of ENTRY, relocate them, 3440 and write the result to the output file. 3441 If `-r', save the text relocation for later reuse. */ 3442 3443 void 3444 copy_text (entry) 3445 struct file_entry *entry; 3446 { 3447 register char *bytes; 3448 register int desc; 3449 register struct relocation_info *reloc; 3450 3451 if (trace_files) 3452 prline_file_name (entry, stderr); 3453 3454 desc = file_open (entry); 3455 3456 /* Allocate space for the file's text section */ 3457 3458 bytes = (char *) alloca (entry->header.a_text); 3459 3460 /* Deal with relocation information however is appropriate */ 3461 3462 if (entry->textrel) reloc = entry->textrel; 3463 else if (relocatable_output) 3464 { 3465 read_file_relocation (entry); 3466 reloc = entry->textrel; 3467 } 3468 else 3469 { 3470 reloc = (struct relocation_info *) alloca (entry->header.a_trsize); 3471 lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0); 3472 if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize)) 3473 fatal_with_file ("premature eof in text relocation of ", entry); 3474 } 3475 3476 /* Read the text section into core. */ 3477 3478 lseek (desc, text_offset (entry), 0); 3479 if (entry->header.a_text != read (desc, bytes, entry->header.a_text)) 3480 fatal_with_file ("premature eof in text section of ", entry); 3481 3482 3483 /* Relocate the text according to the text relocation. */ 3484 3485 perform_relocation (bytes, entry->text_start_address, entry->header.a_text, 3486 reloc, entry->header.a_trsize, entry); 3487 3488 /* Write the relocated text to the output file. */ 3489 3490 mywrite (bytes, 1, entry->header.a_text, outdesc); 3491 } 3492 3493 /* Relocate the data segment of each input file 3494 and write to the output file. */ 3495 3496 void 3497 write_data () 3498 { 3499 if (trace_files) 3500 fprintf (stderr, "Copying and relocating data:\n\n"); 3501 3502 each_full_file (copy_data, 0); 3503 file_close (); 3504 3505 /* Write out the set element vectors. See digest symbols for 3506 description of length of the set vector section. */ 3507 3508 if (set_vector_count) 3509 mywrite (set_vectors, 2 * set_symbol_count + set_vector_count, 3510 sizeof (unsigned long), outdesc); 3511 3512 if (trace_files) 3513 fprintf (stderr, "\n"); 3514 3515 padfile (data_pad, outdesc); 3516 } 3517 3518 /* Read the data segment contents of ENTRY, relocate them, 3519 and write the result to the output file. 3520 If `-r', save the data relocation for later reuse. 3521 See comments in `copy_text'. */ 3522 3523 void 3524 copy_data (entry) 3525 struct file_entry *entry; 3526 { 3527 register struct relocation_info *reloc; 3528 register char *bytes; 3529 register int desc; 3530 3531 if (trace_files) 3532 prline_file_name (entry, stderr); 3533 3534 desc = file_open (entry); 3535 3536 bytes = (char *) alloca (entry->header.a_data); 3537 3538 if (entry->datarel) reloc = entry->datarel; 3539 else if (relocatable_output) /* Will need this again */ 3540 { 3541 read_file_relocation (entry); 3542 reloc = entry->datarel; 3543 } 3544 else 3545 { 3546 reloc = (struct relocation_info *) alloca (entry->header.a_drsize); 3547 lseek (desc, text_offset (entry) + entry->header.a_text 3548 + entry->header.a_data + entry->header.a_trsize, 3549 0); 3550 if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize)) 3551 fatal_with_file ("premature eof in data relocation of ", entry); 3552 } 3553 3554 lseek (desc, text_offset (entry) + entry->header.a_text, 0); 3555 if (entry->header.a_data != read (desc, bytes, entry->header.a_data)) 3556 fatal_with_file ("premature eof in data section of ", entry); 3557 3558 perform_relocation (bytes, entry->data_start_address - entry->header.a_text, 3559 entry->header.a_data, reloc, entry->header.a_drsize, entry); 3560 3561 mywrite (bytes, 1, entry->header.a_data, outdesc); 3562 } 3563 3564 /* Relocate ENTRY's text or data section contents. 3565 DATA is the address of the contents, in core. 3566 DATA_SIZE is the length of the contents. 3567 PC_RELOCATION is the difference between the address of the contents 3568 in the output file and its address in the input file. 3569 RELOC_INFO is the address of the relocation info, in core. 3570 RELOC_SIZE is its length in bytes. */ 3571 /* This version is about to be severly hacked by Randy. Hope it 3572 works afterwards. */ 3573 void 3574 perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry) 3575 char *data; 3576 struct relocation_info *reloc_info; 3577 struct file_entry *entry; 3578 int pc_relocation; 3579 int data_size; 3580 int reloc_size; 3581 { 3582 register struct relocation_info *p = reloc_info; 3583 struct relocation_info *end 3584 = reloc_info + reloc_size / sizeof (struct relocation_info); 3585 int text_relocation = entry->text_start_address; 3586 int data_relocation = entry->data_start_address - entry->header.a_text; 3587 int bss_relocation 3588 = entry->bss_start_address - entry->header.a_text - entry->header.a_data; 3589 3590 for (; p < end; p++) 3591 { 3592 register int relocation = 0; 3593 register int addr = RELOC_ADDRESS(p); 3594 register unsigned int mask = 0; 3595 3596 if (addr >= data_size) 3597 fatal_with_file ("relocation address out of range in ", entry); 3598 3599 if (RELOC_EXTERN_P(p)) 3600 { 3601 int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist); 3602 symbol *sp = ((symbol *) 3603 (((struct nlist *) 3604 (((char *)entry->symbols) + symindex)) 3605 ->n_un.n_name)); 3606 3607 #ifdef N_INDR 3608 /* Resolve indirection */ 3609 if ((sp->defined & ~N_EXT) == N_INDR) 3610 sp = (symbol *) sp->value; 3611 #endif 3612 3613 if (symindex >= entry->header.a_syms) 3614 fatal_with_file ("relocation symbolnum out of range in ", entry); 3615 3616 /* If the symbol is undefined, leave it at zero. */ 3617 if (! sp->defined) 3618 relocation = 0; 3619 else 3620 relocation = sp->value; 3621 } 3622 else switch (RELOC_TYPE(p)) 3623 { 3624 case N_TEXT: 3625 case N_TEXT | N_EXT: 3626 relocation = text_relocation; 3627 break; 3628 3629 case N_DATA: 3630 case N_DATA | N_EXT: 3631 /* A word that points to beginning of the the data section 3632 initially contains not 0 but rather the "address" of that section 3633 in the input file, which is the length of the file's text. */ 3634 relocation = data_relocation; 3635 break; 3636 3637 case N_BSS: 3638 case N_BSS | N_EXT: 3639 /* Similarly, an input word pointing to the beginning of the bss 3640 initially contains the length of text plus data of the file. */ 3641 relocation = bss_relocation; 3642 break; 3643 3644 case N_ABS: 3645 case N_ABS | N_EXT: 3646 /* Don't know why this code would occur, but apparently it does. */ 3647 break; 3648 3649 default: 3650 fatal_with_file ("nonexternal relocation code invalid in ", entry); 3651 } 3652 3653 #ifdef RELOC_ADD_EXTRA 3654 relocation += RELOC_ADD_EXTRA(p); 3655 if (relocatable_output) 3656 { 3657 /* Non-PC relative relocations which are absolute 3658 or which have become non-external now have fixed 3659 relocations. Set the ADD_EXTRA of this relocation 3660 to be the relocation we have now determined. */ 3661 if (! RELOC_PCREL_P (p)) 3662 { 3663 if ((int)p->r_type <= RELOC_32 3664 || RELOC_EXTERN_P (p) == 0) 3665 RELOC_ADD_EXTRA (p) = relocation; 3666 } 3667 /* External PC-relative relocations continue to move around; 3668 update their relocations by the amount they have moved 3669 so far. */ 3670 else if (RELOC_EXTERN_P (p)) 3671 RELOC_ADD_EXTRA (p) -= pc_relocation; 3672 continue; 3673 } 3674 #endif 3675 3676 if (RELOC_PCREL_P(p)) 3677 relocation -= pc_relocation; 3678 3679 relocation >>= RELOC_VALUE_RIGHTSHIFT(p); 3680 3681 /* Unshifted mask for relocation */ 3682 mask = 1 << RELOC_TARGET_BITSIZE(p) - 1; 3683 mask |= mask - 1; 3684 relocation &= mask; 3685 3686 /* Shift everything up to where it's going to be used */ 3687 relocation <<= RELOC_TARGET_BITPOS(p); 3688 mask <<= RELOC_TARGET_BITPOS(p); 3689 3690 switch (RELOC_TARGET_SIZE(p)) 3691 { 3692 case 0: 3693 if (RELOC_MEMORY_SUB_P(p)) 3694 relocation -= mask & *(char *) (data + addr); 3695 else if (RELOC_MEMORY_ADD_P(p)) 3696 relocation += mask & *(char *) (data + addr); 3697 *(char *) (data + addr) &= ~mask; 3698 *(char *) (data + addr) |= relocation; 3699 break; 3700 3701 case 1: 3702 #ifdef tahoe 3703 if (((int) data + addr & 1) == 0) 3704 { 3705 #endif 3706 if (RELOC_MEMORY_SUB_P(p)) 3707 relocation -= mask & *(short *) (data + addr); 3708 else if (RELOC_MEMORY_ADD_P(p)) 3709 relocation += mask & *(short *) (data + addr); 3710 *(short *) (data + addr) &= ~mask; 3711 *(short *) (data + addr) |= relocation; 3712 #ifdef tahoe 3713 } 3714 /* 3715 * The CCI Power 6 (aka Tahoe) architecture has byte-aligned 3716 * instruction operands but requires data accesses to be aligned. 3717 * Brain-damage... 3718 */ 3719 else 3720 { 3721 unsigned char *da = (unsigned char *) (data + addr); 3722 unsigned short s = da[0] << 8 | da[1]; 3723 3724 if (RELOC_MEMORY_SUB_P(p)) 3725 relocation -= mask & s; 3726 else if (RELOC_MEMORY_ADD_P(p)) 3727 relocation += mask & s; 3728 s &= ~mask; 3729 s |= relocation; 3730 da[0] = s >> 8; 3731 da[1] = s; 3732 } 3733 #endif 3734 break; 3735 3736 case 2: 3737 #ifndef _CROSS_TARGET_ARCH 3738 #ifdef tahoe 3739 if (((int) data + addr & 3) == 0) 3740 { 3741 #endif 3742 if (RELOC_MEMORY_SUB_P(p)) 3743 relocation -= mask & *(long *) (data + addr); 3744 else if (RELOC_MEMORY_ADD_P(p)) 3745 relocation += mask & *(long *) (data + addr); 3746 *(long *) (data + addr) &= ~mask; 3747 *(long *) (data + addr) |= relocation; 3748 #ifdef tahoe 3749 } 3750 else 3751 { 3752 unsigned char *da = (unsigned char *) (data + addr); 3753 unsigned long l = da[0] << 24 | da[1] << 16 | da[2] << 8 | da[3]; 3754 3755 if (RELOC_MEMORY_SUB_P(p)) 3756 relocation -= mask & l; 3757 else if (RELOC_MEMORY_ADD_P(p)) 3758 relocation += mask & l; 3759 l &= ~mask; 3760 l |= relocation; 3761 da[0] = l >> 24; 3762 da[1] = l >> 16; 3763 da[2] = l >> 8; 3764 da[3] = l; 3765 } 3766 #endif 3767 #else 3768 /* Handle long word alignment requirements of SPARC architecture */ 3769 /* WARNING: This fix makes an assumption on byte ordering */ 3770 /* Marc Ullman, Stanford University Nov. 1 1989 */ 3771 if (RELOC_MEMORY_SUB_P(p)) { 3772 relocation -= mask & 3773 ((*(unsigned short *) (data + addr) << 16) | 3774 *(unsigned short *) (data + addr + 2)); 3775 } else if (RELOC_MEMORY_ADD_P(p)) { 3776 relocation += mask & 3777 ((*(unsigned short *) (data + addr) << 16) | 3778 *(unsigned short *) (data + addr + 2)); 3779 } 3780 *(unsigned short *) (data + addr) &= (~mask >> 16); 3781 *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff); 3782 *(unsigned short *) (data + addr) |= (relocation >> 16); 3783 *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff); 3784 #endif 3785 break; 3786 3787 default: 3788 fatal_with_file ("Unimplemented relocation field length in ", entry); 3789 } 3790 } 3791 } 3792 3793 /* For relocatable_output only: write out the relocation, 3794 relocating the addresses-to-be-relocated. */ 3795 3796 void coptxtrel (), copdatrel (); 3797 3798 void 3799 write_rel () 3800 { 3801 register int i; 3802 register int count = 0; 3803 3804 if (trace_files) 3805 fprintf (stderr, "Writing text relocation:\n\n"); 3806 3807 /* Assign each global symbol a sequence number, giving the order 3808 in which `write_syms' will write it. 3809 This is so we can store the proper symbolnum fields 3810 in relocation entries we write. */ 3811 3812 for (i = 0; i < TABSIZE; i++) 3813 { 3814 symbol *sp; 3815 for (sp = symtab[i]; sp; sp = sp->link) 3816 if (sp->referenced || sp->defined) 3817 { 3818 sp->def_count = count++; 3819 /* Leave room for the reference required by N_INDR, if 3820 necessary. */ 3821 if ((sp->defined & ~N_EXT) == N_INDR) 3822 count++; 3823 } 3824 } 3825 /* Correct, because if (relocatable_output), we will also be writing 3826 whatever indirect blocks we have. */ 3827 if (count != defined_global_sym_count 3828 + undefined_global_sym_count + global_indirect_count) 3829 fatal ("internal error"); 3830 3831 /* Write out the relocations of all files, remembered from copy_text. */ 3832 3833 each_full_file (coptxtrel, 0); 3834 3835 if (trace_files) 3836 fprintf (stderr, "\nWriting data relocation:\n\n"); 3837 3838 each_full_file (copdatrel, 0); 3839 3840 if (trace_files) 3841 fprintf (stderr, "\n"); 3842 } 3843 3844 void 3845 coptxtrel (entry) 3846 struct file_entry *entry; 3847 { 3848 register struct relocation_info *p, *end; 3849 register int reloc = entry->text_start_address; 3850 3851 p = entry->textrel; 3852 end = (struct relocation_info *) (entry->header.a_trsize + (char *) p); 3853 while (p < end) 3854 { 3855 RELOC_ADDRESS(p) += reloc; 3856 if (RELOC_EXTERN_P(p)) 3857 { 3858 register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist); 3859 symbol *symptr = ((symbol *) 3860 (((struct nlist *) 3861 (((char *)entry->symbols) + symindex)) 3862 ->n_un.n_name)); 3863 3864 if (symindex >= entry->header.a_syms) 3865 fatal_with_file ("relocation symbolnum out of range in ", entry); 3866 3867 #ifdef N_INDR 3868 /* Resolve indirection. */ 3869 if ((symptr->defined & ~N_EXT) == N_INDR) 3870 symptr = (symbol *) symptr->value; 3871 #endif 3872 3873 /* If the symbol is now defined, change the external relocation 3874 to an internal one. */ 3875 3876 if (symptr->defined) 3877 { 3878 RELOC_EXTERN_P(p) = 0; 3879 RELOC_SYMBOL(p) = (symptr->defined & N_TYPE); 3880 #ifdef RELOC_ADD_EXTRA 3881 /* If we aren't going to be adding in the value in 3882 memory on the next pass of the loader, then we need 3883 to add it in from the relocation entry. Otherwise 3884 the work we did in this pass is lost. */ 3885 if (!RELOC_MEMORY_ADD_P(p)) 3886 RELOC_ADD_EXTRA (p) += symptr->value; 3887 #endif 3888 } 3889 else 3890 /* Debugger symbols come first, so have to start this 3891 after them. */ 3892 RELOC_SYMBOL(p) = (symptr->def_count + nsyms 3893 - defined_global_sym_count 3894 - undefined_global_sym_count 3895 - global_indirect_count); 3896 } 3897 p++; 3898 } 3899 mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc); 3900 } 3901 3902 void 3903 copdatrel (entry) 3904 struct file_entry *entry; 3905 { 3906 register struct relocation_info *p, *end; 3907 /* Relocate the address of the relocation. 3908 Old address is relative to start of the input file's data section. 3909 New address is relative to start of the output file's data section. */ 3910 register int reloc = entry->data_start_address - text_size; 3911 3912 p = entry->datarel; 3913 end = (struct relocation_info *) (entry->header.a_drsize + (char *) p); 3914 while (p < end) 3915 { 3916 RELOC_ADDRESS(p) += reloc; 3917 if (RELOC_EXTERN_P(p)) 3918 { 3919 register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist); 3920 symbol *symptr = ((symbol *) 3921 (((struct nlist *) 3922 (((char *)entry->symbols) + symindex)) 3923 ->n_un.n_name)); 3924 int symtype; 3925 3926 if (symindex >= entry->header.a_syms) 3927 fatal_with_file ("relocation symbolnum out of range in ", entry); 3928 3929 #ifdef N_INDR 3930 /* Resolve indirection. */ 3931 if ((symptr->defined & ~N_EXT) == N_INDR) 3932 symptr = (symbol *) symptr->value; 3933 #endif 3934 3935 symtype = symptr->defined & N_TYPE; 3936 3937 if (force_common_definition 3938 || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS) 3939 { 3940 RELOC_EXTERN_P(p) = 0; 3941 RELOC_SYMBOL(p) = symtype; 3942 } 3943 else 3944 /* Debugger symbols come first, so have to start this 3945 after them. */ 3946 RELOC_SYMBOL(p) 3947 = (((symbol *) 3948 (((struct nlist *) 3949 (((char *)entry->symbols) + symindex)) 3950 ->n_un.n_name)) 3951 ->def_count 3952 + nsyms - defined_global_sym_count 3953 - undefined_global_sym_count 3954 - global_indirect_count); 3955 } 3956 p++; 3957 } 3958 mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc); 3959 } 3960 3961 void write_file_syms (); 3962 void write_string_table (); 3963 3964 /* Offsets and current lengths of symbol and string tables in output file. */ 3965 3966 int symbol_table_offset; 3967 int symbol_table_len; 3968 3969 /* Address in output file where string table starts. */ 3970 int string_table_offset; 3971 3972 /* Offset within string table 3973 where the strings in `strtab_vector' should be written. */ 3974 int string_table_len; 3975 3976 /* Total size of string table strings allocated so far, 3977 including strings in `strtab_vector'. */ 3978 int strtab_size; 3979 3980 /* Vector whose elements are strings to be added to the string table. */ 3981 char **strtab_vector; 3982 3983 /* Vector whose elements are the lengths of those strings. */ 3984 int *strtab_lens; 3985 3986 /* Index in `strtab_vector' at which the next string will be stored. */ 3987 int strtab_index; 3988 3989 /* Add the string NAME to the output file string table. 3990 Record it in `strtab_vector' to be output later. 3991 Return the index within the string table that this string will have. */ 3992 3993 int 3994 assign_string_table_index (name) 3995 char *name; 3996 { 3997 register int index = strtab_size; 3998 register int len = strlen (name) + 1; 3999 4000 strtab_size += len; 4001 strtab_vector[strtab_index] = name; 4002 strtab_lens[strtab_index++] = len; 4003 4004 return index; 4005 } 4006 4007 FILE *outstream = (FILE *) 0; 4008 4009 /* Write the contents of `strtab_vector' into the string table. 4010 This is done once for each file's local&debugger symbols 4011 and once for the global symbols. */ 4012 4013 void 4014 write_string_table () 4015 { 4016 register int i; 4017 4018 lseek (outdesc, string_table_offset + string_table_len, 0); 4019 4020 if (!outstream) 4021 outstream = fdopen (outdesc, "w"); 4022 4023 for (i = 0; i < strtab_index; i++) 4024 { 4025 fwrite (strtab_vector[i], 1, strtab_lens[i], outstream); 4026 string_table_len += strtab_lens[i]; 4027 } 4028 4029 fflush (outstream); 4030 4031 /* Report I/O error such as disk full. */ 4032 if (ferror (outstream)) 4033 perror_name (output_filename); 4034 } 4035 4036 /* Write the symbol table and string table of the output file. */ 4037 4038 void 4039 write_syms () 4040 { 4041 /* Number of symbols written so far. */ 4042 int syms_written = 0; 4043 register int i; 4044 register symbol *sp; 4045 4046 /* Buffer big enough for all the global symbols. One 4047 extra struct for each indirect symbol to hold the extra reference 4048 following. */ 4049 struct nlist *buf 4050 = (struct nlist *) alloca ((defined_global_sym_count 4051 + undefined_global_sym_count 4052 + global_indirect_count) 4053 * sizeof (struct nlist)); 4054 /* Pointer for storing into BUF. */ 4055 register struct nlist *bufp = buf; 4056 4057 /* Size of string table includes the bytes that store the size. */ 4058 strtab_size = sizeof strtab_size; 4059 4060 symbol_table_offset = N_SYMOFF (outheader); 4061 symbol_table_len = 0; 4062 string_table_offset = N_STROFF (outheader); 4063 string_table_len = strtab_size; 4064 4065 if (strip_symbols == STRIP_ALL) 4066 return; 4067 4068 /* Write the local symbols defined by the various files. */ 4069 4070 each_file (write_file_syms, &syms_written); 4071 file_close (); 4072 4073 /* Now write out the global symbols. */ 4074 4075 /* Allocate two vectors that record the data to generate the string 4076 table from the global symbols written so far. This must include 4077 extra space for the references following indirect outputs. */ 4078 4079 strtab_vector = (char **) alloca ((num_hash_tab_syms 4080 + global_indirect_count) * sizeof (char *)); 4081 strtab_lens = (int *) alloca ((num_hash_tab_syms 4082 + global_indirect_count) * sizeof (int)); 4083 strtab_index = 0; 4084 4085 /* Scan the symbol hash table, bucket by bucket. */ 4086 4087 for (i = 0; i < TABSIZE; i++) 4088 for (sp = symtab[i]; sp; sp = sp->link) 4089 { 4090 struct nlist nl; 4091 4092 nl.n_other = 0; 4093 nl.n_desc = 0; 4094 4095 /* Compute a `struct nlist' for the symbol. */ 4096 4097 if (sp->defined || sp->referenced) 4098 { 4099 /* common condition needs to be before undefined condition */ 4100 /* because unallocated commons are set undefined in */ 4101 /* digest_symbols */ 4102 if (sp->defined > 1) /* defined with known type */ 4103 { 4104 /* If the target of an indirect symbol has been 4105 defined and we are outputting an executable, 4106 resolve the indirection; it's no longer needed */ 4107 if (!relocatable_output 4108 && ((sp->defined & N_TYPE) == N_INDR) 4109 && (((symbol *) sp->value)->defined > 1)) 4110 { 4111 symbol *newsp = (symbol *) sp->value; 4112 nl.n_type = newsp->defined; 4113 nl.n_value = newsp->value; 4114 } 4115 else 4116 { 4117 nl.n_type = sp->defined; 4118 if (sp->defined != (N_INDR | N_EXT)) 4119 nl.n_value = sp->value; 4120 else 4121 nl.n_value = 0; 4122 } 4123 } 4124 else if (sp->max_common_size) /* defined as common but not allocated. */ 4125 { 4126 /* happens only with -r and not -d */ 4127 /* write out a common definition */ 4128 nl.n_type = N_UNDF | N_EXT; 4129 nl.n_value = sp->max_common_size; 4130 } 4131 else if (!sp->defined) /* undefined -- legit only if -r */ 4132 { 4133 nl.n_type = N_UNDF | N_EXT; 4134 nl.n_value = 0; 4135 } 4136 else 4137 fatal ("internal error: %s defined in mysterious way", sp->name); 4138 4139 /* Allocate string table space for the symbol name. */ 4140 4141 nl.n_un.n_strx = assign_string_table_index (sp->name); 4142 4143 /* Output to the buffer and count it. */ 4144 4145 *bufp++ = nl; 4146 syms_written++; 4147 if (nl.n_type == (N_INDR | N_EXT)) 4148 { 4149 struct nlist xtra_ref; 4150 xtra_ref.n_type = N_EXT | N_UNDF; 4151 xtra_ref.n_un.n_strx 4152 = assign_string_table_index (((symbol *) sp->value)->name); 4153 xtra_ref.n_other = 0; 4154 xtra_ref.n_desc = 0; 4155 xtra_ref.n_value = 0; 4156 *bufp++ = xtra_ref; 4157 syms_written++; 4158 } 4159 } 4160 } 4161 4162 /* Output the buffer full of `struct nlist's. */ 4163 4164 lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 4165 mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc); 4166 symbol_table_len += sizeof (struct nlist) * (bufp - buf); 4167 4168 if (syms_written != nsyms) 4169 fatal ("internal error: wrong number of symbols written into output file", 0); 4170 4171 if (symbol_table_offset + symbol_table_len != string_table_offset) 4172 fatal ("internal error: inconsistent symbol table length", 0); 4173 4174 /* Now the total string table size is known, so write it. 4175 We are already positioned at the right place in the file. */ 4176 4177 mywrite (&strtab_size, sizeof (int), 1, outdesc); /* we're at right place */ 4178 4179 /* Write the strings for the global symbols. */ 4180 4181 write_string_table (); 4182 } 4183 4184 /* Write the local and debugger symbols of file ENTRY. 4185 Increment *SYMS_WRITTEN_ADDR for each symbol that is written. */ 4186 4187 /* Note that we do not combine identical names of local symbols. 4188 dbx or gdb would be confused if we did that. */ 4189 4190 void 4191 write_file_syms (entry, syms_written_addr) 4192 struct file_entry *entry; 4193 int *syms_written_addr; 4194 { 4195 register struct nlist *p = entry->symbols; 4196 register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist); 4197 4198 /* Buffer to accumulate all the syms before writing them. 4199 It has one extra slot for the local symbol we generate here. */ 4200 struct nlist *buf 4201 = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist)); 4202 register struct nlist *bufp = buf; 4203 4204 /* Upper bound on number of syms to be written here. */ 4205 int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1; 4206 4207 /* Make tables that record, for each symbol, its name and its name's length. 4208 The elements are filled in by `assign_string_table_index'. */ 4209 4210 strtab_vector = (char **) alloca (max_syms * sizeof (char *)); 4211 strtab_lens = (int *) alloca (max_syms * sizeof (int)); 4212 strtab_index = 0; 4213 4214 /* Generate a local symbol for the start of this file's text. */ 4215 4216 if (discard_locals != DISCARD_ALL) 4217 { 4218 struct nlist nl; 4219 4220 nl.n_type = N_FN | N_EXT; 4221 nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); 4222 nl.n_value = entry->text_start_address; 4223 nl.n_desc = 0; 4224 nl.n_other = 0; 4225 *bufp++ = nl; 4226 (*syms_written_addr)++; 4227 entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist); 4228 } 4229 4230 /* Read the file's string table. */ 4231 4232 entry->strings = (char *) alloca (entry->string_size); 4233 read_entry_strings (file_open (entry), entry); 4234 4235 for (; p < end; p++) 4236 { 4237 register int type = p->n_type; 4238 register int write = 0; 4239 4240 /* WRITE gets 1 for a non-global symbol that should be written. */ 4241 4242 4243 if (SET_ELEMENT_P (type)) /* This occurs even if global. These */ 4244 /* types of symbols are never written */ 4245 /* globally, though they are stored */ 4246 /* globally. */ 4247 write = relocatable_output; 4248 else if (!(type & (N_STAB | N_EXT))) 4249 /* ordinary local symbol */ 4250 write = ((discard_locals != DISCARD_ALL) 4251 && !(discard_locals == DISCARD_L && 4252 (p->n_un.n_strx + entry->strings)[0] == LPREFIX) 4253 && type != N_WARNING); 4254 else if (!(type & N_EXT)) 4255 /* debugger symbol */ 4256 write = (strip_symbols == STRIP_NONE); 4257 4258 if (write) 4259 { 4260 /* If this symbol has a name, 4261 allocate space for it in the output string table. */ 4262 4263 if (p->n_un.n_strx) 4264 p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx 4265 + entry->strings); 4266 4267 /* Output this symbol to the buffer and count it. */ 4268 4269 *bufp++ = *p; 4270 (*syms_written_addr)++; 4271 } 4272 } 4273 4274 /* All the symbols are now in BUF; write them. */ 4275 4276 lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 4277 mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc); 4278 symbol_table_len += sizeof (struct nlist) * (bufp - buf); 4279 4280 /* Write the string-table data for the symbols just written, 4281 using the data in vectors `strtab_vector' and `strtab_lens'. */ 4282 4283 write_string_table (); 4284 entry->strings = 0; /* Since it will dissapear anyway. */ 4285 } 4286 4287 /* Copy any GDB symbol segments from the input files to the output file. 4288 The contents of the symbol segment is copied without change 4289 except that we store some information into the beginning of it. */ 4290 4291 void write_file_symseg (); 4292 4293 void 4294 write_symsegs () 4295 { 4296 each_file (write_file_symseg, 0); 4297 } 4298 4299 void 4300 write_file_symseg (entry) 4301 struct file_entry *entry; 4302 { 4303 char buffer[4096]; 4304 struct symbol_root root; 4305 int indesc; 4306 int len; 4307 4308 if (entry->symseg_offset == 0) 4309 return; 4310 4311 /* This entry has a symbol segment. Read the root of the segment. */ 4312 4313 indesc = file_open (entry); 4314 lseek (indesc, entry->symseg_offset + entry->starting_offset, 0); 4315 if (sizeof root != read (indesc, &root, sizeof root)) 4316 fatal_with_file ("premature end of file in symbol segment of ", entry); 4317 4318 /* Store some relocation info into the root. */ 4319 4320 root.ldsymoff = entry->local_syms_offset; 4321 root.textrel = entry->text_start_address; 4322 root.datarel = entry->data_start_address - entry->header.a_text; 4323 root.bssrel = entry->bss_start_address 4324 - entry->header.a_text - entry->header.a_data; 4325 root.databeg = entry->data_start_address - root.datarel; 4326 root.bssbeg = entry->bss_start_address - root.bssrel; 4327 4328 /* Write the modified root into the output file. */ 4329 4330 mywrite (&root, sizeof root, 1, outdesc); 4331 4332 /* Copy the rest of the symbol segment unchanged. */ 4333 4334 if (entry->superfile) 4335 { 4336 /* Library member: number of bytes to copy is determined 4337 from the member's total size. */ 4338 4339 int total = entry->total_size - entry->symseg_offset - sizeof root; 4340 4341 while (total > 0) 4342 { 4343 len = read (indesc, buffer, min (sizeof buffer, total)); 4344 4345 if (len != min (sizeof buffer, total)) 4346 fatal_with_file ("premature end of file in symbol segment of ", entry); 4347 total -= len; 4348 mywrite (buffer, len, 1, outdesc); 4349 } 4350 } 4351 else 4352 { 4353 /* A separate file: copy until end of file. */ 4354 4355 while (len = read (indesc, buffer, sizeof buffer)) 4356 { 4357 mywrite (buffer, len, 1, outdesc); 4358 if (len < sizeof buffer) 4359 break; 4360 } 4361 } 4362 4363 file_close (); 4364 } 4365 4366 /* Create the symbol table entries for `etext', `edata' and `end'. */ 4367 4368 void 4369 symtab_init () 4370 { 4371 #ifndef nounderscore 4372 edata_symbol = getsym ("_edata"); 4373 etext_symbol = getsym ("_etext"); 4374 end_symbol = getsym ("_end"); 4375 #else 4376 edata_symbol = getsym ("edata"); 4377 etext_symbol = getsym ("etext"); 4378 end_symbol = getsym ("end"); 4379 #endif 4380 4381 #ifdef sun 4382 { 4383 symbol *dynamic_symbol = getsym ("__DYNAMIC"); 4384 dynamic_symbol->defined = N_ABS | N_EXT; 4385 dynamic_symbol->referenced = 1; 4386 dynamic_symbol->value = 0; 4387 } 4388 #endif 4389 4390 #ifdef sequent 4391 { 4392 symbol *_387_flt_symbol = getsym ("_387_flt"); 4393 _387_flt_symbol->defined = N_ABS | N_EXT; 4394 _387_flt_symbol->referenced = 1; 4395 _387_flt_symbol->value = 0; 4396 } 4397 #endif 4398 4399 edata_symbol->defined = N_DATA | N_EXT; 4400 etext_symbol->defined = N_TEXT | N_EXT; 4401 end_symbol->defined = N_BSS | N_EXT; 4402 4403 edata_symbol->referenced = 1; 4404 etext_symbol->referenced = 1; 4405 end_symbol->referenced = 1; 4406 } 4407 4408 /* Compute the hash code for symbol name KEY. */ 4409 4410 int 4411 hash_string (key) 4412 char *key; 4413 { 4414 register char *cp; 4415 register int k; 4416 4417 cp = key; 4418 k = 0; 4419 while (*cp) 4420 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 4421 4422 return k; 4423 } 4424 4425 /* Get the symbol table entry for the global symbol named KEY. 4426 Create one if there is none. */ 4427 4428 symbol * 4429 getsym (key) 4430 char *key; 4431 { 4432 register int hashval; 4433 register symbol *bp; 4434 4435 /* Determine the proper bucket. */ 4436 4437 hashval = hash_string (key) % TABSIZE; 4438 4439 /* Search the bucket. */ 4440 4441 for (bp = symtab[hashval]; bp; bp = bp->link) 4442 if (! strcmp (key, bp->name)) 4443 return bp; 4444 4445 /* Nothing was found; create a new symbol table entry. */ 4446 4447 bp = (symbol *) xmalloc (sizeof (symbol)); 4448 bp->refs = 0; 4449 bp->name = (char *) xmalloc (strlen (key) + 1); 4450 strcpy (bp->name, key); 4451 bp->defined = 0; 4452 bp->referenced = 0; 4453 bp->trace = 0; 4454 bp->value = 0; 4455 bp->max_common_size = 0; 4456 bp->warning = 0; 4457 bp->undef_refs = 0; 4458 bp->multiply_defined = 0; 4459 4460 /* Add the entry to the bucket. */ 4461 4462 bp->link = symtab[hashval]; 4463 symtab[hashval] = bp; 4464 4465 ++num_hash_tab_syms; 4466 4467 return bp; 4468 } 4469 4470 /* Like `getsym' but return 0 if the symbol is not already known. */ 4471 4472 symbol * 4473 getsym_soft (key) 4474 char *key; 4475 { 4476 register int hashval; 4477 register symbol *bp; 4478 4479 /* Determine which bucket. */ 4480 4481 hashval = hash_string (key) % TABSIZE; 4482 4483 /* Search the bucket. */ 4484 4485 for (bp = symtab[hashval]; bp; bp = bp->link) 4486 if (! strcmp (key, bp->name)) 4487 return bp; 4488 4489 return 0; 4490 } 4491 4492 /* Report a fatal error. 4493 STRING is a printf format string and ARG is one arg for it. */ 4494 4495 void 4496 fatal (string, arg) 4497 char *string, *arg; 4498 { 4499 fprintf (stderr, "ld: "); 4500 fprintf (stderr, string, arg); 4501 fprintf (stderr, "\n"); 4502 exit (1); 4503 } 4504 4505 /* Report a fatal error. The error message is STRING 4506 followed by the filename of ENTRY. */ 4507 4508 void 4509 fatal_with_file (string, entry) 4510 char *string; 4511 struct file_entry *entry; 4512 { 4513 fprintf (stderr, "ld: "); 4514 fprintf (stderr, string); 4515 print_file_name (entry, stderr); 4516 fprintf (stderr, "\n"); 4517 exit (1); 4518 } 4519 4520 /* Report a fatal error using the message for the last failed system call, 4521 followed by the string NAME. */ 4522 4523 void 4524 perror_name (name) 4525 char *name; 4526 { 4527 extern int errno, sys_nerr; 4528 extern char *sys_errlist[]; 4529 char *s; 4530 4531 if (errno < sys_nerr) 4532 s = concat ("", sys_errlist[errno], " for %s"); 4533 else 4534 s = "cannot open %s"; 4535 fatal (s, name); 4536 } 4537 4538 /* Report a fatal error using the message for the last failed system call, 4539 followed by the name of file ENTRY. */ 4540 4541 void 4542 perror_file (entry) 4543 struct file_entry *entry; 4544 { 4545 extern int errno, sys_nerr; 4546 extern char *sys_errlist[]; 4547 char *s; 4548 4549 if (errno < sys_nerr) 4550 s = concat ("", sys_errlist[errno], " for "); 4551 else 4552 s = "cannot open "; 4553 fatal_with_file (s, entry); 4554 } 4555 4556 /* Report a nonfatal error. 4557 STRING is a format for printf, and ARG1 ... ARG3 are args for it. */ 4558 4559 void 4560 error (string, arg1, arg2, arg3) 4561 char *string, *arg1, *arg2, *arg3; 4562 { 4563 fprintf (stderr, "%s: ", progname); 4564 fprintf (stderr, string, arg1, arg2, arg3); 4565 fprintf (stderr, "\n"); 4566 } 4567 4568 4569 /* Output COUNT*ELTSIZE bytes of data at BUF 4570 to the descriptor DESC. */ 4571 4572 void 4573 mywrite (buf, count, eltsize, desc) 4574 char *buf; 4575 int count; 4576 int eltsize; 4577 int desc; 4578 { 4579 register int val; 4580 register int bytes = count * eltsize; 4581 4582 while (bytes > 0) 4583 { 4584 val = write (desc, buf, bytes); 4585 if (val <= 0) 4586 perror_name (output_filename); 4587 buf += val; 4588 bytes -= val; 4589 } 4590 } 4591 4592 /* Output PADDING zero-bytes to descriptor OUTDESC. 4593 PADDING may be negative; in that case, do nothing. */ 4594 4595 void 4596 padfile (padding, outdesc) 4597 int padding; 4598 int outdesc; 4599 { 4600 register char *buf; 4601 if (padding <= 0) 4602 return; 4603 4604 buf = (char *) alloca (padding); 4605 bzero (buf, padding); 4606 mywrite (buf, padding, 1, outdesc); 4607 } 4608 4609 /* Return a newly-allocated string 4610 whose contents concatenate the strings S1, S2, S3. */ 4611 4612 char * 4613 concat (s1, s2, s3) 4614 char *s1, *s2, *s3; 4615 { 4616 register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); 4617 register char *result = (char *) xmalloc (len1 + len2 + len3 + 1); 4618 4619 strcpy (result, s1); 4620 strcpy (result + len1, s2); 4621 strcpy (result + len1 + len2, s3); 4622 result[len1 + len2 + len3] = 0; 4623 4624 return result; 4625 } 4626 4627 /* Parse the string ARG using scanf format FORMAT, and return the result. 4628 If it does not parse, report fatal error 4629 generating the error message using format string ERROR and ARG as arg. */ 4630 4631 int 4632 parse (arg, format, error) 4633 char *arg, *format; 4634 { 4635 int x; 4636 if (1 != sscanf (arg, format, &x)) 4637 fatal (error, arg); 4638 return x; 4639 } 4640 4641 /* Like malloc but get fatal error if memory is exhausted. */ 4642 4643 int 4644 xmalloc (size) 4645 int size; 4646 { 4647 register int result = malloc (size); 4648 if (!result) 4649 fatal ("virtual memory exhausted", 0); 4650 return result; 4651 } 4652 4653 /* Like realloc but get fatal error if memory is exhausted. */ 4654 4655 int 4656 xrealloc (ptr, size) 4657 char *ptr; 4658 int size; 4659 { 4660 register int result = realloc (ptr, size); 4661 if (!result) 4662 fatal ("virtual memory exhausted", 0); 4663 return result; 4664 } 4665 4666 #ifdef USG 4667 4668 void 4669 bzero (p, n) 4670 char *p; 4671 { 4672 memset (p, 0, n); 4673 } 4674 4675 void 4676 bcopy (from, to, n) 4677 char *from, *to; 4678 { 4679 memcpy (to, from, n); 4680 } 4681 4682 getpagesize () 4683 { 4684 return (4096); 4685 } 4686 4687 #endif 4688 4689 #if defined(sun) && (TARGET == SUN4) 4690 4691 /* Don't use local pagesize to build for Sparc. */ 4692 4693 getpagesize () 4694 { 4695 return (8192); 4696 } 4697 #endif 4698