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.10 (Berkeley) 05/22/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 if (sp && ((sp->referenced && !sp->defined) 2112 || (sp->defined && sp->max_common_size))) 2113 { 2114 int junk; 2115 register int j; 2116 register int offset = symdef_base[i].library_member_offset; 2117 struct file_entry *subentry; 2118 2119 /* Don't think carefully about any archive member 2120 more than once in a given pass. */ 2121 2122 if (prev_offset == offset) 2123 continue; 2124 prev_offset = offset; 2125 2126 /* Read the symbol table of the archive member. */ 2127 2128 subentry = decode_library_subfile (desc, entry, offset, &junk); 2129 if (subentry == 0) 2130 fatal ("invalid offset for %s in symbol table of %s", 2131 sym_name_base 2132 + symdef_base[i].symbol_name_string_index, 2133 entry->filename); 2134 read_entry_symbols (desc, subentry); 2135 subentry->strings = (char *) malloc (subentry->string_size); 2136 read_entry_strings (desc, subentry); 2137 2138 /* Now scan the symbol table and decide whether to load. */ 2139 2140 if (!subfile_wanted_p (subentry)) 2141 { 2142 free (subentry->symbols); 2143 free (subentry); 2144 } 2145 else 2146 { 2147 /* This member is needed; load it. 2148 Since we are loading something on this pass, 2149 we must make another pass through the symdef data. */ 2150 2151 not_finished = 1; 2152 2153 enter_file_symbols (subentry); 2154 2155 if (prev) 2156 prev->chain = subentry; 2157 else entry->subfiles = subentry; 2158 prev = subentry; 2159 2160 /* Clear out this member's symbols from the symdef data 2161 so that following passes won't waste time on them. */ 2162 2163 for (j = 0; j < number_of_symdefs; j++) 2164 { 2165 if (symdef_base[j].library_member_offset == offset) 2166 symdef_base[j].symbol_name_string_index = -1; 2167 } 2168 } 2169 2170 /* We'll read the strings again if we need them again. */ 2171 free (subentry->strings); 2172 subentry->strings = 0; 2173 } 2174 } 2175 } 2176 2177 free (symdef_data); 2178 } 2179 2180 /* Search a library that has no __.SYMDEF. 2181 ENTRY is the library's file_entry. 2182 DESC is the descriptor it is open on. */ 2183 2184 void 2185 linear_library (desc, entry) 2186 int desc; 2187 struct file_entry *entry; 2188 { 2189 register struct file_entry *prev = 0; 2190 register int this_subfile_offset = SARMAG; 2191 2192 while (undefined_global_sym_count || common_defined_global_count) 2193 { 2194 int member_length; 2195 register struct file_entry *subentry; 2196 2197 subentry = decode_library_subfile (desc, entry, this_subfile_offset, 2198 &member_length); 2199 2200 if (!subentry) return; 2201 2202 read_entry_symbols (desc, subentry); 2203 subentry->strings = (char *) alloca (subentry->string_size); 2204 read_entry_strings (desc, subentry); 2205 2206 if (!subfile_wanted_p (subentry)) 2207 { 2208 free (subentry->symbols); 2209 free (subentry); 2210 } 2211 else 2212 { 2213 enter_file_symbols (subentry); 2214 2215 if (prev) 2216 prev->chain = subentry; 2217 else entry->subfiles = subentry; 2218 prev = subentry; 2219 subentry->strings = 0; /* Since space will dissapear on return */ 2220 } 2221 2222 this_subfile_offset += member_length + sizeof (struct ar_hdr); 2223 if (this_subfile_offset & 1) this_subfile_offset++; 2224 } 2225 } 2226 2227 /* ENTRY is an entry for a library member. 2228 Its symbols have been read into core, but not entered. 2229 Return nonzero if we ought to load this member. */ 2230 2231 int 2232 subfile_wanted_p (entry) 2233 struct file_entry *entry; 2234 { 2235 register struct nlist *p; 2236 register struct nlist *end 2237 = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2238 #ifdef DOLLAR_KLUDGE 2239 register int dollar_cond = 0; 2240 #endif 2241 2242 for (p = entry->symbols; p < end; p++) 2243 { 2244 register int type = p->n_type; 2245 register char *name = p->n_un.n_strx + entry->strings; 2246 2247 /* If the symbol has an interesting definition, we could 2248 potentially want it. */ 2249 if (type & N_EXT 2250 && (type != (N_UNDF | N_EXT) || p->n_value 2251 2252 #ifdef DOLLAR_KLUDGE 2253 || name[1] == '$' 2254 #endif 2255 ) 2256 && !SET_ELEMENT_P (type) 2257 && !set_element_prefixed_p (name)) 2258 { 2259 register symbol *sp = getsym_soft (name); 2260 2261 #ifdef DOLLAR_KLUDGE 2262 if (name[1] == '$') 2263 { 2264 sp = getsym_soft (&name[2]); 2265 dollar_cond = 1; 2266 if (!sp) continue; 2267 if (sp->referenced) 2268 { 2269 if (write_map) 2270 { 2271 print_file_name (entry, stdout); 2272 fprintf (stdout, " needed due to $-conditional %s\n", name); 2273 } 2274 return 1; 2275 } 2276 continue; 2277 } 2278 #endif 2279 2280 /* If this symbol has not been hashed, we can't be looking for it. */ 2281 2282 if (!sp) continue; 2283 2284 if ((sp->referenced && !sp->defined) 2285 || (sp->defined && sp->max_common_size)) 2286 { 2287 /* This is a symbol we are looking for. It is either 2288 not yet defined or defined as a common. */ 2289 #ifdef DOLLAR_KLUDGE 2290 if (dollar_cond) continue; 2291 #endif 2292 if (type == (N_UNDF | N_EXT)) 2293 { 2294 /* Symbol being defined as common. 2295 Remember this, but don't load subfile just for this. */ 2296 2297 /* If it didn't used to be common, up the count of 2298 common symbols. */ 2299 if (!sp->max_common_size) 2300 common_defined_global_count++; 2301 2302 if (sp->max_common_size < p->n_value) 2303 sp->max_common_size = p->n_value; 2304 if (!sp->defined) 2305 undefined_global_sym_count--; 2306 sp->defined = 1; 2307 continue; 2308 } 2309 2310 if (write_map) 2311 { 2312 print_file_name (entry, stdout); 2313 fprintf (stdout, " needed due to %s\n", sp->name); 2314 } 2315 return 1; 2316 } 2317 } 2318 } 2319 2320 return 0; 2321 } 2322 2323 void consider_file_section_lengths (), relocate_file_addresses (); 2324 2325 /* Having entered all the global symbols and found the sizes of sections 2326 of all files to be linked, make all appropriate deductions from this data. 2327 2328 We propagate global symbol values from definitions to references. 2329 We compute the layout of the output file and where each input file's 2330 contents fit into it. */ 2331 2332 void 2333 digest_symbols () 2334 { 2335 register int i; 2336 int setv_fill_count; 2337 2338 if (trace_files) 2339 fprintf (stderr, "Digesting symbol information:\n\n"); 2340 2341 /* Compute total size of sections */ 2342 2343 each_file (consider_file_section_lengths, 0); 2344 2345 /* If necessary, pad text section to full page in the file. 2346 Include the padding in the text segment size. */ 2347 2348 if (magic == ZMAGIC) 2349 { 2350 int text_end = text_size + N_TXTOFF (outheader); 2351 text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end; 2352 text_size += text_pad; 2353 } 2354 2355 #ifdef _N_BASEADDR 2356 /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */ 2357 outheader.a_entry = N_PAGSIZ (outheader); 2358 #endif 2359 2360 outheader.a_text = text_size; 2361 #ifdef sequent 2362 outheader.a_text += N_ADDRADJ (outheader); 2363 #endif 2364 2365 /* Make the data segment address start in memory on a suitable boundary. */ 2366 2367 if (! Tdata_flag_specified) 2368 data_start = N_DATADDR (outheader) + text_start - TEXT_START (outheader); 2369 2370 /* Set up the set element vector */ 2371 2372 if (!relocatable_output) 2373 { 2374 /* The set sector size is the number of set elements + a word 2375 for each symbol for the length word at the beginning of the 2376 vector, plus a word for each symbol for a zero at the end of 2377 the vector (for incremental linking). */ 2378 set_sect_size 2379 = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long); 2380 set_sect_start = data_start + data_size; 2381 data_size += set_sect_size; 2382 set_vectors = (unsigned long *) xmalloc (set_sect_size); 2383 setv_fill_count = 0; 2384 } 2385 2386 /* Compute start addresses of each file's sections and symbols. */ 2387 2388 each_full_file (relocate_file_addresses, 0); 2389 2390 /* Now, for each symbol, verify that it is defined globally at most once. 2391 Put the global value into the symbol entry. 2392 Common symbols are allocated here, in the BSS section. 2393 Each defined symbol is given a '->defined' field 2394 which is the correct N_ code for its definition, 2395 except in the case of common symbols with -r. 2396 Then make all the references point at the symbol entry 2397 instead of being chained together. */ 2398 2399 defined_global_sym_count = 0; 2400 2401 for (i = 0; i < TABSIZE; i++) 2402 { 2403 register symbol *sp; 2404 for (sp = symtab[i]; sp; sp = sp->link) 2405 { 2406 /* For each symbol */ 2407 register struct nlist *p, *next; 2408 int defs = 0, com = sp->max_common_size; 2409 struct nlist *first_definition; 2410 for (p = sp->refs; p; p = next) 2411 { 2412 register int type = p->n_type; 2413 2414 if (SET_ELEMENT_P (type)) 2415 { 2416 if (relocatable_output) 2417 fatal ("internal: global ref to set element with -r"); 2418 if (!defs++) 2419 { 2420 sp->value = set_sect_start 2421 + setv_fill_count++ * sizeof (unsigned long); 2422 sp->defined = N_SETV | N_EXT; 2423 first_definition = p; 2424 } 2425 else if ((sp->defined & ~N_EXT) != N_SETV) 2426 { 2427 sp->multiply_defined = 1; 2428 multiple_def_count++; 2429 } 2430 set_vectors[setv_fill_count++] = p->n_value; 2431 } 2432 else if ((type & N_EXT) && type != (N_UNDF | N_EXT)) 2433 { 2434 /* non-common definition */ 2435 if (defs++ && sp->value != p->n_value) 2436 { 2437 sp->multiply_defined = 1; 2438 multiple_def_count++; 2439 } 2440 sp->value = p->n_value; 2441 sp->defined = type; 2442 first_definition = p; 2443 } 2444 next = (struct nlist *) p->n_un.n_name; 2445 p->n_un.n_name = (char *) sp; 2446 } 2447 /* Allocate as common if defined as common and not defined for real */ 2448 if (com && !defs) 2449 { 2450 if (!relocatable_output || force_common_definition) 2451 { 2452 int align = sizeof (int); 2453 2454 /* Round up to nearest sizeof (int). I don't know 2455 whether this is necessary or not (given that 2456 alignment is taken care of later), but it's 2457 traditional, so I'll leave it in. Note that if 2458 this size alignment is ever removed, ALIGN above 2459 will have to be initialized to 1 instead of 2460 sizeof (int). */ 2461 2462 com = (com + sizeof (int) - 1) & (- sizeof (int)); 2463 2464 while (!(com & align)) 2465 align <<= 1; 2466 2467 align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align; 2468 2469 bss_size = ((((bss_size + data_size + data_start) 2470 + (align - 1)) & (- align)) 2471 - data_size - data_start); 2472 2473 sp->value = data_start + data_size + bss_size; 2474 sp->defined = N_BSS | N_EXT; 2475 bss_size += com; 2476 if (write_map) 2477 printf ("Allocating common %s: %x at %x\n", 2478 sp->name, com, sp->value); 2479 } 2480 else 2481 { 2482 sp->defined = 0; 2483 undefined_global_sym_count++; 2484 } 2485 } 2486 /* Set length word at front of vector and zero byte at end. 2487 Reverse the vector itself to put it in file order. */ 2488 if ((sp->defined & ~N_EXT) == N_SETV) 2489 { 2490 unsigned long length_word_index 2491 = (sp->value - set_sect_start) / sizeof (unsigned long); 2492 unsigned long i, tmp; 2493 2494 set_vectors[length_word_index] 2495 = setv_fill_count - 1 - length_word_index; 2496 2497 /* Reverse the vector. */ 2498 for (i = 1; 2499 i < (setv_fill_count - length_word_index - 1) / 2 + 1; 2500 i++) 2501 { 2502 tmp = set_vectors[length_word_index + i]; 2503 set_vectors[length_word_index + i] 2504 = set_vectors[setv_fill_count - i]; 2505 set_vectors[setv_fill_count - i] = tmp; 2506 } 2507 2508 set_vectors[setv_fill_count++] = 0; 2509 } 2510 if (sp->defined) 2511 defined_global_sym_count++; 2512 } 2513 } 2514 2515 if (end_symbol) /* These are null if -r. */ 2516 { 2517 etext_symbol->value = text_size + text_start; 2518 edata_symbol->value = data_start + data_size; 2519 end_symbol->value = data_start + data_size + bss_size; 2520 } 2521 2522 /* Figure the data_pad now, so that it overlaps with the bss addresses. */ 2523 2524 if (specified_data_size && specified_data_size > data_size) 2525 data_pad = specified_data_size - data_size; 2526 2527 if (magic == ZMAGIC) 2528 data_pad = ((data_pad + data_size + page_size - 1) & (- page_size)) 2529 - data_size; 2530 2531 bss_size -= data_pad; 2532 if (bss_size < 0) bss_size = 0; 2533 2534 data_size += data_pad; 2535 } 2536 2537 /* Accumulate the section sizes of input file ENTRY 2538 into the section sizes of the output file. */ 2539 2540 void 2541 consider_file_section_lengths (entry) 2542 register struct file_entry *entry; 2543 { 2544 if (entry->just_syms_flag) 2545 return; 2546 2547 entry->text_start_address = text_size; 2548 /* If there were any vectors, we need to chop them off */ 2549 text_size += entry->header.a_text; 2550 entry->data_start_address = data_size; 2551 data_size += entry->header.a_data; 2552 entry->bss_start_address = bss_size; 2553 bss_size += entry->header.a_bss; 2554 2555 text_reloc_size += entry->header.a_trsize; 2556 data_reloc_size += entry->header.a_drsize; 2557 } 2558 2559 /* Determine where the sections of ENTRY go into the output file, 2560 whose total section sizes are already known. 2561 Also relocate the addresses of the file's local and debugger symbols. */ 2562 2563 void 2564 relocate_file_addresses (entry) 2565 register struct file_entry *entry; 2566 { 2567 entry->text_start_address += text_start; 2568 /* Note that `data_start' and `data_size' have not yet been 2569 adjusted for `data_pad'. If they had been, we would get the wrong 2570 results here. */ 2571 entry->data_start_address += data_start; 2572 entry->bss_start_address += data_start + data_size; 2573 2574 { 2575 register struct nlist *p; 2576 register struct nlist *end 2577 = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2578 2579 for (p = entry->symbols; p < end; p++) 2580 { 2581 /* If this belongs to a section, update it by the section's start address */ 2582 register int type = p->n_type & N_TYPE; 2583 2584 switch (type) 2585 { 2586 case N_TEXT: 2587 case N_SETT: 2588 p->n_value += entry->text_start_address; 2589 break; 2590 case N_DATA: 2591 case N_SETV: 2592 case N_SETD: 2593 /* A symbol whose value is in the data section 2594 is present in the input file as if the data section 2595 started at an address equal to the length of the file's text. */ 2596 p->n_value += entry->data_start_address - entry->header.a_text; 2597 break; 2598 case N_BSS: 2599 case N_SETB: 2600 /* likewise for symbols with value in BSS. */ 2601 p->n_value += entry->bss_start_address 2602 - entry->header.a_text - entry->header.a_data; 2603 break; 2604 } 2605 } 2606 } 2607 } 2608 2609 void describe_file_sections (), list_file_locals (); 2610 2611 /* Print a complete or partial map of the output file. */ 2612 2613 void 2614 print_symbols (outfile) 2615 FILE *outfile; 2616 { 2617 register int i; 2618 2619 fprintf (outfile, "\nFiles:\n\n"); 2620 2621 each_file (describe_file_sections, outfile); 2622 2623 fprintf (outfile, "\nGlobal symbols:\n\n"); 2624 2625 for (i = 0; i < TABSIZE; i++) 2626 { 2627 register symbol *sp; 2628 for (sp = symtab[i]; sp; sp = sp->link) 2629 { 2630 if (sp->defined == 1) 2631 fprintf (outfile, " %s: common, length 0x%x\n", sp->name, sp->max_common_size); 2632 if (sp->defined) 2633 fprintf (outfile, " %s: 0x%x\n", sp->name, sp->value); 2634 else if (sp->referenced) 2635 fprintf (outfile, " %s: undefined\n", sp->name); 2636 } 2637 } 2638 2639 each_file (list_file_locals, outfile); 2640 } 2641 2642 void 2643 describe_file_sections (entry, outfile) 2644 struct file_entry *entry; 2645 FILE *outfile; 2646 { 2647 fprintf (outfile, " "); 2648 print_file_name (entry, outfile); 2649 if (entry->just_syms_flag) 2650 fprintf (outfile, " symbols only\n", 0); 2651 else 2652 fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n", 2653 entry->text_start_address, entry->header.a_text, 2654 entry->data_start_address, entry->header.a_data, 2655 entry->bss_start_address, entry->header.a_bss); 2656 } 2657 2658 void 2659 list_file_locals (entry, outfile) 2660 struct file_entry *entry; 2661 FILE *outfile; 2662 { 2663 register struct nlist 2664 *p, 2665 *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2666 2667 entry->strings = (char *) alloca (entry->string_size); 2668 read_entry_strings (file_open (entry), entry); 2669 2670 fprintf (outfile, "\nLocal symbols of "); 2671 print_file_name (entry, outfile); 2672 fprintf (outfile, ":\n\n"); 2673 2674 for (p = entry->symbols; p < end; p++) 2675 /* If this is a definition, 2676 update it if necessary by this file's start address. */ 2677 if (!(p->n_type & (N_STAB | N_EXT))) 2678 fprintf (outfile, " %s: 0x%x\n", 2679 entry->strings + p->n_un.n_strx, p->n_value); 2680 2681 entry->strings = 0; /* All done with them. */ 2682 } 2683 2684 2685 /* Static vars for do_warnings and subroutines of it */ 2686 int list_unresolved_refs; /* List unresolved refs */ 2687 int list_warning_symbols; /* List warning syms */ 2688 int list_multiple_defs; /* List multiple definitions */ 2689 2690 /* 2691 * Structure for communication between do_file_warnings and it's 2692 * helper routines. Will in practice be an array of three of these: 2693 * 0) Current line, 1) Next line, 2) Source file info. 2694 */ 2695 struct line_debug_entry 2696 { 2697 int line; 2698 char *filename; 2699 struct nlist *sym; 2700 }; 2701 2702 void qsort (); 2703 /* 2704 * Helper routines for do_file_warnings. 2705 */ 2706 2707 /* Return an integer less than, equal to, or greater than 0 as per the 2708 relation between the two relocation entries. Used by qsort. */ 2709 2710 int 2711 relocation_entries_relation (rel1, rel2) 2712 struct relocation_info *rel1, *rel2; 2713 { 2714 return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2); 2715 } 2716 2717 /* Moves to the next debugging symbol in the file. USE_DATA_SYMBOLS 2718 determines the type of the debugging symbol to look for (DSLINE or 2719 SLINE). STATE_POINTER keeps track of the old and new locatiosn in 2720 the file. It assumes that state_pointer[1] is valid; ie 2721 that it.sym points into some entry in the symbol table. If 2722 state_pointer[1].sym == 0, this routine should not be called. */ 2723 2724 int 2725 next_debug_entry (use_data_symbols, state_pointer) 2726 register int use_data_symbols; 2727 /* Next must be passed by reference! */ 2728 struct line_debug_entry state_pointer[3]; 2729 { 2730 register struct line_debug_entry 2731 *current = state_pointer, 2732 *next = state_pointer + 1, 2733 /* Used to store source file */ 2734 *source = state_pointer + 2; 2735 struct file_entry *entry = (struct file_entry *) source->sym; 2736 2737 current->sym = next->sym; 2738 current->line = next->line; 2739 current->filename = next->filename; 2740 2741 while (++(next->sym) < (entry->symbols 2742 + entry->header.a_syms/sizeof (struct nlist))) 2743 { 2744 /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so 2745 * may look negative...therefore, must mask to low bits 2746 */ 2747 switch (next->sym->n_type & 0xff) 2748 { 2749 case N_SLINE: 2750 if (use_data_symbols) continue; 2751 next->line = next->sym->n_desc; 2752 return 1; 2753 case N_DSLINE: 2754 if (!use_data_symbols) continue; 2755 next->line = next->sym->n_desc; 2756 return 1; 2757 #ifdef HAVE_SUN_STABS 2758 case N_EINCL: 2759 next->filename = source->filename; 2760 continue; 2761 #endif 2762 case N_SO: 2763 source->filename = next->sym->n_un.n_strx + entry->strings; 2764 source->line++; 2765 #ifdef HAVE_SUN_STABS 2766 case N_BINCL: 2767 #endif 2768 case N_SOL: 2769 next->filename 2770 = next->sym->n_un.n_strx + entry->strings; 2771 default: 2772 continue; 2773 } 2774 } 2775 next->sym = (struct nlist *) 0; 2776 return 0; 2777 } 2778 2779 /* Create a structure to save the state of a scan through the debug 2780 symbols. USE_DATA_SYMBOLS is set if we should be scanning for 2781 DSLINE's instead of SLINE's. entry is the file entry which points 2782 at the symbols to use. */ 2783 2784 struct line_debug_entry * 2785 init_debug_scan (use_data_symbols, entry) 2786 int use_data_symbols; 2787 struct file_entry *entry; 2788 { 2789 struct line_debug_entry 2790 *state_pointer 2791 = (struct line_debug_entry *) 2792 xmalloc (3 * sizeof (struct line_debug_entry)); 2793 register struct line_debug_entry 2794 *current = state_pointer, 2795 *next = state_pointer + 1, 2796 *source = state_pointer + 2; /* Used to store source file */ 2797 2798 struct nlist *tmp; 2799 2800 for (tmp = entry->symbols; 2801 tmp < (entry->symbols 2802 + entry->header.a_syms/sizeof (struct nlist)); 2803 tmp++) 2804 if (tmp->n_type == (int) N_SO) 2805 break; 2806 2807 if (tmp >= (entry->symbols 2808 + entry->header.a_syms/sizeof (struct nlist))) 2809 { 2810 /* I believe this translates to "We lose" */ 2811 current->filename = next->filename = entry->filename; 2812 current->line = next->line = -1; 2813 current->sym = next->sym = (struct nlist *) 0; 2814 return state_pointer; 2815 } 2816 2817 next->line = source->line = 0; 2818 next->filename = source->filename 2819 = (tmp->n_un.n_strx + entry->strings); 2820 source->sym = (struct nlist *) entry; 2821 next->sym = tmp; 2822 2823 next_debug_entry (use_data_symbols, state_pointer); /* To setup next */ 2824 2825 if (!next->sym) /* No line numbers for this section; */ 2826 /* setup output results as appropriate */ 2827 { 2828 if (source->line) 2829 { 2830 current->filename = source->filename = entry->filename; 2831 current->line = -1; /* Don't print lineno */ 2832 } 2833 else 2834 { 2835 current->filename = source->filename; 2836 current->line = 0; 2837 } 2838 return state_pointer; 2839 } 2840 2841 2842 next_debug_entry (use_data_symbols, state_pointer); /* To setup current */ 2843 2844 return state_pointer; 2845 } 2846 2847 /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER 2848 which describes the current location in the implied scan through 2849 the debug symbols within the file which ADDRESS is within, and 2850 returns the source line number which corresponds to ADDRESS. */ 2851 2852 int 2853 address_to_line (address, state_pointer) 2854 unsigned long address; 2855 /* Next must be passed by reference! */ 2856 struct line_debug_entry state_pointer[3]; 2857 { 2858 struct line_debug_entry 2859 *current = state_pointer, 2860 *next = state_pointer + 1; 2861 struct line_debug_entry *tmp_pointer; 2862 2863 int use_data_symbols; 2864 2865 if (next->sym) 2866 use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA; 2867 else 2868 return current->line; 2869 2870 /* Go back to the beginning if we've already passed it. */ 2871 if (current->sym->n_value > address) 2872 { 2873 tmp_pointer = init_debug_scan (use_data_symbols, 2874 (struct file_entry *) 2875 ((state_pointer + 2)->sym)); 2876 state_pointer[0] = tmp_pointer[0]; 2877 state_pointer[1] = tmp_pointer[1]; 2878 state_pointer[2] = tmp_pointer[2]; 2879 free (tmp_pointer); 2880 } 2881 2882 /* If we're still in a bad way, return -1, meaning invalid line. */ 2883 if (current->sym->n_value > address) 2884 return -1; 2885 2886 while (next->sym 2887 && next->sym->n_value <= address 2888 && next_debug_entry (use_data_symbols, state_pointer)) 2889 ; 2890 return current->line; 2891 } 2892 2893 2894 /* Macros for manipulating bitvectors. */ 2895 #define BIT_SET_P(bv, index) ((bv)[(index) >> 3] & 1 << ((index) & 0x7)) 2896 #define SET_BIT(bv, index) ((bv)[(index) >> 3] |= 1 << ((index) & 0x7)) 2897 2898 /* This routine will scan through the relocation data of file ENTRY, 2899 printing out references to undefined symbols and references to 2900 symbols defined in files with N_WARNING symbols. If DATA_SEGMENT 2901 is non-zero, it will scan the data relocation segment (and use 2902 N_DSLINE symbols to track line number); otherwise it will scan the 2903 text relocation segment. Warnings will be printed on the output 2904 stream OUTFILE. Eventually, every nlist symbol mapped through will 2905 be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when 2906 we scan the nlists themselves. */ 2907 2908 do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector) 2909 struct file_entry *entry; 2910 int data_segment; 2911 FILE *outfile; 2912 unsigned char *nlist_bitvector; 2913 { 2914 struct relocation_info 2915 *reloc_start = data_segment ? entry->datarel : entry->textrel, 2916 *reloc; 2917 int reloc_size 2918 = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize) 2919 / sizeof (struct relocation_info)); 2920 int start_of_segment 2921 = (data_segment ? entry->data_start_address : entry->text_start_address); 2922 struct nlist *start_of_syms = entry->symbols; 2923 struct line_debug_entry *state_pointer 2924 = init_debug_scan (data_segment != 0, entry); 2925 register struct line_debug_entry 2926 *current = state_pointer; 2927 /* Assigned to generally static values; should not be written into. */ 2928 char *errfmt; 2929 /* Assigned to alloca'd values cand copied into; should be freed 2930 when done. */ 2931 char *errmsg; 2932 int invalidate_line_number; 2933 2934 /* We need to sort the relocation info here. Sheesh, so much effort 2935 for one lousy error optimization. */ 2936 2937 qsort (reloc_start, reloc_size, sizeof (struct relocation_info), 2938 relocation_entries_relation); 2939 2940 for (reloc = reloc_start; 2941 reloc < (reloc_start + reloc_size); 2942 reloc++) 2943 { 2944 register struct nlist *s; 2945 register symbol *g; 2946 2947 /* If the relocation isn't resolved through a symbol, continue */ 2948 if (!RELOC_EXTERN_P(reloc)) 2949 continue; 2950 2951 s = &(entry->symbols[RELOC_SYMBOL(reloc)]); 2952 2953 /* Local symbols shouldn't ever be used by relocation info, so 2954 the next should be safe. 2955 This is, of course, wrong. References to local BSS symbols can be 2956 the targets of relocation info, and they can (must) be 2957 resolved through symbols. However, these must be defined properly, 2958 (the assembler would have caught it otherwise), so we can 2959 ignore these cases. */ 2960 if (!(s->n_type & N_EXT)) 2961 continue; 2962 2963 g = (symbol *) s->n_un.n_name; 2964 errmsg = 0; 2965 2966 if (!g->defined && list_unresolved_refs) /* Reference */ 2967 { 2968 /* Mark as being noted by relocation warning pass. */ 2969 SET_BIT (nlist_bitvector, s - start_of_syms); 2970 2971 if (g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */ 2972 continue; 2973 2974 /* Undefined symbol which we should mention */ 2975 2976 if (++(g->undef_refs) == MAX_UREFS_PRINTED) 2977 { 2978 errfmt = "More undefined symbol %s refs follow"; 2979 invalidate_line_number = 1; 2980 } 2981 else 2982 { 2983 errfmt = "Undefined symbol %s referenced from %s segment"; 2984 invalidate_line_number = 0; 2985 } 2986 } 2987 else /* Defined */ 2988 { 2989 /* Potential symbol warning here */ 2990 if (!g->warning) continue; 2991 2992 /* Mark as being noted by relocation warning pass. */ 2993 SET_BIT (nlist_bitvector, s - start_of_syms); 2994 2995 errfmt = 0; 2996 errmsg = g->warning; 2997 invalidate_line_number = 0; 2998 } 2999 3000 3001 /* If errfmt == 0, errmsg has already been defined. */ 3002 if (errfmt != 0) 3003 { 3004 char *nm; 3005 3006 if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL) 3007 nm = g->name; 3008 errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1); 3009 sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text"); 3010 if (nm != g->name) 3011 free (nm); 3012 } 3013 3014 address_to_line (RELOC_ADDRESS (reloc) + start_of_segment, 3015 state_pointer); 3016 3017 if (current->line >=0) 3018 fprintf (outfile, "%s:%d: %s\n", current->filename, 3019 invalidate_line_number ? 0 : current->line, errmsg); 3020 else 3021 fprintf (outfile, "%s: %s\n", current->filename, errmsg); 3022 3023 if (errfmt != 0) 3024 free (errmsg); 3025 } 3026 3027 free (state_pointer); 3028 } 3029 3030 /* Print on OUTFILE a list of all warnings generated by references 3031 and/or definitions in the file ENTRY. List source file and line 3032 number if possible, just the .o file if not. */ 3033 3034 void 3035 do_file_warnings (entry, outfile) 3036 struct file_entry *entry; 3037 FILE *outfile; 3038 { 3039 int number_of_syms = entry->header.a_syms / sizeof (struct nlist); 3040 unsigned char *nlist_bitvector 3041 = (unsigned char *) alloca ((number_of_syms >> 3) + 1); 3042 struct line_debug_entry *text_scan, *data_scan; 3043 int i; 3044 char *errfmt, *file_name; 3045 int line_number; 3046 int dont_allow_symbol_name; 3047 3048 bzero (nlist_bitvector, (number_of_syms >> 3) + 1); 3049 3050 /* Read in the files strings if they aren't available */ 3051 if (!entry->strings) 3052 { 3053 int desc; 3054 3055 entry->strings = (char *) alloca (entry->string_size); 3056 desc = file_open (entry); 3057 read_entry_strings (desc, entry); 3058 } 3059 3060 read_file_relocation (entry); 3061 3062 /* Do text warnings based on a scan through the relocation info. */ 3063 do_relocation_warnings (entry, 0, outfile, nlist_bitvector); 3064 3065 /* Do data warnings based on a scan through the relocation info. */ 3066 do_relocation_warnings (entry, 1, outfile, nlist_bitvector); 3067 3068 /* Scan through all of the nlist entries in this file and pick up 3069 anything that the scan through the relocation stuff didn't. */ 3070 3071 text_scan = init_debug_scan (0, entry); 3072 data_scan = init_debug_scan (1, entry); 3073 3074 for (i = 0; i < number_of_syms; i++) 3075 { 3076 struct nlist *s; 3077 struct glosym *g; 3078 3079 s = entry->symbols + i; 3080 3081 if (!(s->n_type & N_EXT)) 3082 continue; 3083 3084 g = (symbol *) s->n_un.n_name; 3085 dont_allow_symbol_name = 0; 3086 3087 if (list_multiple_defs && g->multiply_defined) 3088 { 3089 errfmt = "Definition of symbol %s (multiply defined)"; 3090 switch (s->n_type) 3091 { 3092 case N_TEXT | N_EXT: 3093 line_number = address_to_line (s->n_value, text_scan); 3094 file_name = text_scan[0].filename; 3095 break; 3096 case N_DATA | N_EXT: 3097 line_number = address_to_line (s->n_value, data_scan); 3098 file_name = data_scan[0].filename; 3099 break; 3100 case N_SETA | N_EXT: 3101 case N_SETT | N_EXT: 3102 case N_SETD | N_EXT: 3103 case N_SETB | N_EXT: 3104 if (g->multiply_defined == 2) 3105 continue; 3106 errfmt = "First set element definition of symbol %s (multiply defined)"; 3107 break; 3108 default: 3109 continue; /* Don't print out multiple defs 3110 at references. */ 3111 } 3112 } 3113 else if (BIT_SET_P (nlist_bitvector, i)) 3114 continue; 3115 else if (list_unresolved_refs && !g->defined) 3116 { 3117 if (g->undef_refs >= MAX_UREFS_PRINTED) 3118 continue; 3119 3120 if (++(g->undef_refs) == MAX_UREFS_PRINTED) 3121 errfmt = "More undefined \"%s\" refs follow"; 3122 else 3123 errfmt = "Undefined symbol \"%s\" referenced"; 3124 line_number = -1; 3125 } 3126 else if (g->warning) 3127 { 3128 /* There are two cases in which we don't want to 3129 do this. The first is if this is a definition instead of 3130 a reference. The second is if it's the reference used by 3131 the warning stabs itself. */ 3132 if (s->n_type != (N_EXT | N_UNDF) 3133 || (i && (s-1)->n_type == N_WARNING)) 3134 continue; 3135 3136 errfmt = g->warning; 3137 line_number = -1; 3138 dont_allow_symbol_name = 1; 3139 } 3140 else 3141 continue; 3142 3143 if (line_number == -1) 3144 fprintf (outfile, "%s: ", entry->filename); 3145 else 3146 fprintf (outfile, "%s:%d: ", file_name, line_number); 3147 3148 if (dont_allow_symbol_name) 3149 fprintf (outfile, "%s", errfmt); 3150 else 3151 { 3152 char *nm; 3153 if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL) 3154 { 3155 fprintf (outfile, errfmt, nm); 3156 free (nm); 3157 } 3158 else 3159 fprintf (outfile, errfmt, g->name); 3160 } 3161 3162 fputc ('\n', outfile); 3163 } 3164 free (text_scan); 3165 free (data_scan); 3166 entry->strings = 0; /* Since it will dissapear anyway. */ 3167 } 3168 3169 do_warnings (outfile) 3170 FILE *outfile; 3171 { 3172 list_unresolved_refs = !relocatable_output && undefined_global_sym_count; 3173 list_warning_symbols = warning_count; 3174 list_multiple_defs = multiple_def_count != 0; 3175 3176 if (!(list_unresolved_refs || 3177 list_warning_symbols || 3178 list_multiple_defs )) 3179 /* No need to run this routine */ 3180 return; 3181 3182 each_file (do_file_warnings, outfile); 3183 3184 if (list_unresolved_refs || list_multiple_defs) 3185 make_executable = 0; 3186 } 3187 3188 /* Write the output file */ 3189 3190 void 3191 write_output () 3192 { 3193 struct stat statbuf; 3194 int filemode; 3195 3196 (void) unlink (output_filename); 3197 outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 3198 if (outdesc < 0) perror_name (output_filename); 3199 3200 if (fstat (outdesc, &statbuf) < 0) 3201 perror_name (output_filename); 3202 3203 filemode = statbuf.st_mode; 3204 3205 chmod (output_filename, filemode & ~0111); 3206 3207 /* Output the a.out header. */ 3208 write_header (); 3209 3210 /* Output the text and data segments, relocating as we go. */ 3211 write_text (); 3212 write_data (); 3213 3214 /* Output the merged relocation info, if requested with `-r'. */ 3215 if (relocatable_output) 3216 write_rel (); 3217 3218 /* Output the symbol table (both globals and locals). */ 3219 write_syms (); 3220 3221 /* Copy any GDB symbol segments from input files. */ 3222 write_symsegs (); 3223 3224 close (outdesc); 3225 3226 if (chmod (output_filename, filemode | 0111) == -1) 3227 perror_name (output_filename); 3228 } 3229 3230 void modify_location (), perform_relocation (), copy_text (), copy_data (); 3231 3232 void 3233 write_header () 3234 { 3235 N_SET_MAGIC (outheader, magic); 3236 outheader.a_text = text_size; 3237 #ifdef sequent 3238 outheader.a_text += N_ADDRADJ (outheader); 3239 if (entry_symbol == 0) 3240 entry_symbol = getsym("start"); 3241 #endif 3242 outheader.a_data = data_size; 3243 outheader.a_bss = bss_size; 3244 outheader.a_entry = (entry_symbol ? entry_symbol->value 3245 : text_start + entry_offset); 3246 #ifdef COFF_ENCAPSULATE 3247 if (need_coff_header) 3248 { 3249 /* We are encapsulating BSD format within COFF format. */ 3250 struct coffscn *tp, *dp, *bp; 3251 3252 tp = &coffheader.scns[0]; 3253 dp = &coffheader.scns[1]; 3254 bp = &coffheader.scns[2]; 3255 3256 strcpy (tp->s_name, ".text"); 3257 tp->s_paddr = text_start; 3258 tp->s_vaddr = text_start; 3259 tp->s_size = text_size; 3260 tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec); 3261 tp->s_relptr = 0; 3262 tp->s_lnnoptr = 0; 3263 tp->s_nreloc = 0; 3264 tp->s_nlnno = 0; 3265 tp->s_flags = 0x20; 3266 strcpy (dp->s_name, ".data"); 3267 dp->s_paddr = data_start; 3268 dp->s_vaddr = data_start; 3269 dp->s_size = data_size; 3270 dp->s_scnptr = tp->s_scnptr + tp->s_size; 3271 dp->s_relptr = 0; 3272 dp->s_lnnoptr = 0; 3273 dp->s_nreloc = 0; 3274 dp->s_nlnno = 0; 3275 dp->s_flags = 0x40; 3276 strcpy (bp->s_name, ".bss"); 3277 bp->s_paddr = dp->s_vaddr + dp->s_size; 3278 bp->s_vaddr = bp->s_paddr; 3279 bp->s_size = bss_size; 3280 bp->s_scnptr = 0; 3281 bp->s_relptr = 0; 3282 bp->s_lnnoptr = 0; 3283 bp->s_nreloc = 0; 3284 bp->s_nlnno = 0; 3285 bp->s_flags = 0x80; 3286 3287 coffheader.f_magic = COFF_MAGIC; 3288 coffheader.f_nscns = 3; 3289 /* store an unlikely time so programs can 3290 * tell that there is a bsd header 3291 */ 3292 coffheader.f_timdat = 1; 3293 coffheader.f_symptr = 0; 3294 coffheader.f_nsyms = 0; 3295 coffheader.f_opthdr = 28; 3296 coffheader.f_flags = 0x103; 3297 /* aouthdr */ 3298 coffheader.magic = ZMAGIC; 3299 coffheader.vstamp = 0; 3300 coffheader.tsize = tp->s_size; 3301 coffheader.dsize = dp->s_size; 3302 coffheader.bsize = bp->s_size; 3303 coffheader.entry = outheader.a_entry; 3304 coffheader.text_start = tp->s_vaddr; 3305 coffheader.data_start = dp->s_vaddr; 3306 } 3307 #endif 3308 3309 #ifdef INITIALIZE_HEADER 3310 INITIALIZE_HEADER; 3311 #endif 3312 3313 if (strip_symbols == STRIP_ALL) 3314 nsyms = 0; 3315 else 3316 { 3317 nsyms = (defined_global_sym_count 3318 + undefined_global_sym_count); 3319 if (discard_locals == DISCARD_L) 3320 nsyms += non_L_local_sym_count; 3321 else if (discard_locals == DISCARD_NONE) 3322 nsyms += local_sym_count; 3323 /* One extra for following reference on indirects */ 3324 if (relocatable_output) 3325 nsyms += set_symbol_count + global_indirect_count; 3326 } 3327 3328 if (strip_symbols == STRIP_NONE) 3329 nsyms += debugger_sym_count; 3330 3331 outheader.a_syms = nsyms * sizeof (struct nlist); 3332 3333 if (relocatable_output) 3334 { 3335 outheader.a_trsize = text_reloc_size; 3336 outheader.a_drsize = data_reloc_size; 3337 } 3338 else 3339 { 3340 outheader.a_trsize = 0; 3341 outheader.a_drsize = 0; 3342 } 3343 3344 #ifdef COFF_ENCAPSULATE 3345 if (need_coff_header) 3346 mywrite (&coffheader, sizeof coffheader, 1, outdesc); 3347 #endif 3348 mywrite (&outheader, sizeof (struct exec), 1, outdesc); 3349 3350 /* Output whatever padding is required in the executable file 3351 between the header and the start of the text. */ 3352 3353 #ifndef COFF_ENCAPSULATE 3354 padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc); 3355 #endif 3356 } 3357 3358 /* Relocate the text segment of each input file 3359 and write to the output file. */ 3360 3361 void 3362 write_text () 3363 { 3364 if (trace_files) 3365 fprintf (stderr, "Copying and relocating text:\n\n"); 3366 3367 each_full_file (copy_text, 0); 3368 file_close (); 3369 3370 if (trace_files) 3371 fprintf (stderr, "\n"); 3372 3373 padfile (text_pad, outdesc); 3374 } 3375 3376 int 3377 text_offset (entry) 3378 struct file_entry *entry; 3379 { 3380 return entry->starting_offset + N_TXTOFF (entry->header); 3381 } 3382 3383 /* Read in all of the relocation information */ 3384 3385 void 3386 read_relocation () 3387 { 3388 each_full_file (read_file_relocation, 0); 3389 } 3390 3391 /* Read in the relocation sections of ENTRY if necessary */ 3392 3393 void 3394 read_file_relocation (entry) 3395 struct file_entry *entry; 3396 { 3397 register struct relocation_info *reloc; 3398 int desc; 3399 int read_return; 3400 3401 desc = -1; 3402 if (!entry->textrel) 3403 { 3404 reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize); 3405 desc = file_open (entry); 3406 lseek (desc, 3407 text_offset (entry) + entry->header.a_text + entry->header.a_data, 3408 L_SET); 3409 if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize))) 3410 { 3411 fprintf (stderr, "Return from read: %d\n", read_return); 3412 fatal_with_file ("premature eof in text relocation of ", entry); 3413 } 3414 entry->textrel = reloc; 3415 } 3416 3417 if (!entry->datarel) 3418 { 3419 reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize); 3420 if (desc == -1) desc = file_open (entry); 3421 lseek (desc, 3422 text_offset (entry) + entry->header.a_text 3423 + entry->header.a_data + entry->header.a_trsize, 3424 L_SET); 3425 if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize)) 3426 fatal_with_file ("premature eof in data relocation of ", entry); 3427 entry->datarel = reloc; 3428 } 3429 } 3430 3431 /* Read the text segment contents of ENTRY, relocate them, 3432 and write the result to the output file. 3433 If `-r', save the text relocation for later reuse. */ 3434 3435 void 3436 copy_text (entry) 3437 struct file_entry *entry; 3438 { 3439 register char *bytes; 3440 register int desc; 3441 register struct relocation_info *reloc; 3442 3443 if (trace_files) 3444 prline_file_name (entry, stderr); 3445 3446 desc = file_open (entry); 3447 3448 /* Allocate space for the file's text section */ 3449 3450 bytes = (char *) alloca (entry->header.a_text); 3451 3452 /* Deal with relocation information however is appropriate */ 3453 3454 if (entry->textrel) reloc = entry->textrel; 3455 else if (relocatable_output) 3456 { 3457 read_file_relocation (entry); 3458 reloc = entry->textrel; 3459 } 3460 else 3461 { 3462 reloc = (struct relocation_info *) alloca (entry->header.a_trsize); 3463 lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0); 3464 if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize)) 3465 fatal_with_file ("premature eof in text relocation of ", entry); 3466 } 3467 3468 /* Read the text section into core. */ 3469 3470 lseek (desc, text_offset (entry), 0); 3471 if (entry->header.a_text != read (desc, bytes, entry->header.a_text)) 3472 fatal_with_file ("premature eof in text section of ", entry); 3473 3474 3475 /* Relocate the text according to the text relocation. */ 3476 3477 perform_relocation (bytes, entry->text_start_address, entry->header.a_text, 3478 reloc, entry->header.a_trsize, entry); 3479 3480 /* Write the relocated text to the output file. */ 3481 3482 mywrite (bytes, 1, entry->header.a_text, outdesc); 3483 } 3484 3485 /* Relocate the data segment of each input file 3486 and write to the output file. */ 3487 3488 void 3489 write_data () 3490 { 3491 if (trace_files) 3492 fprintf (stderr, "Copying and relocating data:\n\n"); 3493 3494 each_full_file (copy_data, 0); 3495 file_close (); 3496 3497 /* Write out the set element vectors. See digest symbols for 3498 description of length of the set vector section. */ 3499 3500 if (set_vector_count) 3501 mywrite (set_vectors, 2 * set_symbol_count + set_vector_count, 3502 sizeof (unsigned long), outdesc); 3503 3504 if (trace_files) 3505 fprintf (stderr, "\n"); 3506 3507 padfile (data_pad, outdesc); 3508 } 3509 3510 /* Read the data segment contents of ENTRY, relocate them, 3511 and write the result to the output file. 3512 If `-r', save the data relocation for later reuse. 3513 See comments in `copy_text'. */ 3514 3515 void 3516 copy_data (entry) 3517 struct file_entry *entry; 3518 { 3519 register struct relocation_info *reloc; 3520 register char *bytes; 3521 register int desc; 3522 3523 if (trace_files) 3524 prline_file_name (entry, stderr); 3525 3526 desc = file_open (entry); 3527 3528 bytes = (char *) alloca (entry->header.a_data); 3529 3530 if (entry->datarel) reloc = entry->datarel; 3531 else if (relocatable_output) /* Will need this again */ 3532 { 3533 read_file_relocation (entry); 3534 reloc = entry->datarel; 3535 } 3536 else 3537 { 3538 reloc = (struct relocation_info *) alloca (entry->header.a_drsize); 3539 lseek (desc, text_offset (entry) + entry->header.a_text 3540 + entry->header.a_data + entry->header.a_trsize, 3541 0); 3542 if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize)) 3543 fatal_with_file ("premature eof in data relocation of ", entry); 3544 } 3545 3546 lseek (desc, text_offset (entry) + entry->header.a_text, 0); 3547 if (entry->header.a_data != read (desc, bytes, entry->header.a_data)) 3548 fatal_with_file ("premature eof in data section of ", entry); 3549 3550 perform_relocation (bytes, entry->data_start_address - entry->header.a_text, 3551 entry->header.a_data, reloc, entry->header.a_drsize, entry); 3552 3553 mywrite (bytes, 1, entry->header.a_data, outdesc); 3554 } 3555 3556 /* Relocate ENTRY's text or data section contents. 3557 DATA is the address of the contents, in core. 3558 DATA_SIZE is the length of the contents. 3559 PC_RELOCATION is the difference between the address of the contents 3560 in the output file and its address in the input file. 3561 RELOC_INFO is the address of the relocation info, in core. 3562 RELOC_SIZE is its length in bytes. */ 3563 /* This version is about to be severly hacked by Randy. Hope it 3564 works afterwards. */ 3565 void 3566 perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry) 3567 char *data; 3568 struct relocation_info *reloc_info; 3569 struct file_entry *entry; 3570 int pc_relocation; 3571 int data_size; 3572 int reloc_size; 3573 { 3574 register struct relocation_info *p = reloc_info; 3575 struct relocation_info *end 3576 = reloc_info + reloc_size / sizeof (struct relocation_info); 3577 int text_relocation = entry->text_start_address; 3578 int data_relocation = entry->data_start_address - entry->header.a_text; 3579 int bss_relocation 3580 = entry->bss_start_address - entry->header.a_text - entry->header.a_data; 3581 3582 for (; p < end; p++) 3583 { 3584 register int relocation = 0; 3585 register int addr = RELOC_ADDRESS(p); 3586 register unsigned int mask = 0; 3587 3588 if (addr >= data_size) 3589 fatal_with_file ("relocation address out of range in ", entry); 3590 3591 if (RELOC_EXTERN_P(p)) 3592 { 3593 int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist); 3594 symbol *sp = ((symbol *) 3595 (((struct nlist *) 3596 (((char *)entry->symbols) + symindex)) 3597 ->n_un.n_name)); 3598 3599 #ifdef N_INDR 3600 /* Resolve indirection */ 3601 if ((sp->defined & ~N_EXT) == N_INDR) 3602 sp = (symbol *) sp->value; 3603 #endif 3604 3605 if (symindex >= entry->header.a_syms) 3606 fatal_with_file ("relocation symbolnum out of range in ", entry); 3607 3608 /* If the symbol is undefined, leave it at zero. */ 3609 if (! sp->defined) 3610 relocation = 0; 3611 else 3612 relocation = sp->value; 3613 } 3614 else switch (RELOC_TYPE(p)) 3615 { 3616 case N_TEXT: 3617 case N_TEXT | N_EXT: 3618 relocation = text_relocation; 3619 break; 3620 3621 case N_DATA: 3622 case N_DATA | N_EXT: 3623 /* A word that points to beginning of the the data section 3624 initially contains not 0 but rather the "address" of that section 3625 in the input file, which is the length of the file's text. */ 3626 relocation = data_relocation; 3627 break; 3628 3629 case N_BSS: 3630 case N_BSS | N_EXT: 3631 /* Similarly, an input word pointing to the beginning of the bss 3632 initially contains the length of text plus data of the file. */ 3633 relocation = bss_relocation; 3634 break; 3635 3636 case N_ABS: 3637 case N_ABS | N_EXT: 3638 /* Don't know why this code would occur, but apparently it does. */ 3639 break; 3640 3641 default: 3642 fatal_with_file ("nonexternal relocation code invalid in ", entry); 3643 } 3644 3645 #ifdef RELOC_ADD_EXTRA 3646 relocation += RELOC_ADD_EXTRA(p); 3647 if (relocatable_output) 3648 { 3649 /* Non-PC relative relocations which are absolute 3650 or which have become non-external now have fixed 3651 relocations. Set the ADD_EXTRA of this relocation 3652 to be the relocation we have now determined. */ 3653 if (! RELOC_PCREL_P (p)) 3654 { 3655 if ((int)p->r_type <= RELOC_32 3656 || RELOC_EXTERN_P (p) == 0) 3657 RELOC_ADD_EXTRA (p) = relocation; 3658 } 3659 /* External PC-relative relocations continue to move around; 3660 update their relocations by the amount they have moved 3661 so far. */ 3662 else if (RELOC_EXTERN_P (p)) 3663 RELOC_ADD_EXTRA (p) -= pc_relocation; 3664 continue; 3665 } 3666 #endif 3667 3668 if (RELOC_PCREL_P(p)) 3669 relocation -= pc_relocation; 3670 3671 relocation >>= RELOC_VALUE_RIGHTSHIFT(p); 3672 3673 /* Unshifted mask for relocation */ 3674 mask = 1 << RELOC_TARGET_BITSIZE(p) - 1; 3675 mask |= mask - 1; 3676 relocation &= mask; 3677 3678 /* Shift everything up to where it's going to be used */ 3679 relocation <<= RELOC_TARGET_BITPOS(p); 3680 mask <<= RELOC_TARGET_BITPOS(p); 3681 3682 switch (RELOC_TARGET_SIZE(p)) 3683 { 3684 case 0: 3685 if (RELOC_MEMORY_SUB_P(p)) 3686 relocation -= mask & *(char *) (data + addr); 3687 else if (RELOC_MEMORY_ADD_P(p)) 3688 relocation += mask & *(char *) (data + addr); 3689 *(char *) (data + addr) &= ~mask; 3690 *(char *) (data + addr) |= relocation; 3691 break; 3692 3693 case 1: 3694 #ifdef tahoe 3695 if (((int) data + addr & 1) == 0) 3696 { 3697 #endif 3698 if (RELOC_MEMORY_SUB_P(p)) 3699 relocation -= mask & *(short *) (data + addr); 3700 else if (RELOC_MEMORY_ADD_P(p)) 3701 relocation += mask & *(short *) (data + addr); 3702 *(short *) (data + addr) &= ~mask; 3703 *(short *) (data + addr) |= relocation; 3704 #ifdef tahoe 3705 } 3706 /* 3707 * The CCI Power 6 (aka Tahoe) architecture has byte-aligned 3708 * instruction operands but requires data accesses to be aligned. 3709 * Brain-damage... 3710 */ 3711 else 3712 { 3713 unsigned char *da = (unsigned char *) (data + addr); 3714 unsigned short s = da[0] << 8 | da[1]; 3715 3716 if (RELOC_MEMORY_SUB_P(p)) 3717 relocation -= mask & s; 3718 else if (RELOC_MEMORY_ADD_P(p)) 3719 relocation += mask & s; 3720 s &= ~mask; 3721 s |= relocation; 3722 da[0] = s >> 8; 3723 da[1] = s; 3724 } 3725 #endif 3726 break; 3727 3728 case 2: 3729 #ifndef _CROSS_TARGET_ARCH 3730 #ifdef tahoe 3731 if (((int) data + addr & 3) == 0) 3732 { 3733 #endif 3734 if (RELOC_MEMORY_SUB_P(p)) 3735 relocation -= mask & *(long *) (data + addr); 3736 else if (RELOC_MEMORY_ADD_P(p)) 3737 relocation += mask & *(long *) (data + addr); 3738 *(long *) (data + addr) &= ~mask; 3739 *(long *) (data + addr) |= relocation; 3740 #ifdef tahoe 3741 } 3742 else 3743 { 3744 unsigned char *da = (unsigned char *) (data + addr); 3745 unsigned long l = da[0] << 24 | da[1] << 16 | da[2] << 8 | da[3]; 3746 3747 if (RELOC_MEMORY_SUB_P(p)) 3748 relocation -= mask & l; 3749 else if (RELOC_MEMORY_ADD_P(p)) 3750 relocation += mask & l; 3751 l &= ~mask; 3752 l |= relocation; 3753 da[0] = l >> 24; 3754 da[1] = l >> 16; 3755 da[2] = l >> 8; 3756 da[3] = l; 3757 } 3758 #endif 3759 #else 3760 /* Handle long word alignment requirements of SPARC architecture */ 3761 /* WARNING: This fix makes an assumption on byte ordering */ 3762 /* Marc Ullman, Stanford University Nov. 1 1989 */ 3763 if (RELOC_MEMORY_SUB_P(p)) { 3764 relocation -= mask & 3765 ((*(unsigned short *) (data + addr) << 16) | 3766 *(unsigned short *) (data + addr + 2)); 3767 } else if (RELOC_MEMORY_ADD_P(p)) { 3768 relocation += mask & 3769 ((*(unsigned short *) (data + addr) << 16) | 3770 *(unsigned short *) (data + addr + 2)); 3771 } 3772 *(unsigned short *) (data + addr) &= (~mask >> 16); 3773 *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff); 3774 *(unsigned short *) (data + addr) |= (relocation >> 16); 3775 *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff); 3776 #endif 3777 break; 3778 3779 default: 3780 fatal_with_file ("Unimplemented relocation field length in ", entry); 3781 } 3782 } 3783 } 3784 3785 /* For relocatable_output only: write out the relocation, 3786 relocating the addresses-to-be-relocated. */ 3787 3788 void coptxtrel (), copdatrel (); 3789 3790 void 3791 write_rel () 3792 { 3793 register int i; 3794 register int count = 0; 3795 3796 if (trace_files) 3797 fprintf (stderr, "Writing text relocation:\n\n"); 3798 3799 /* Assign each global symbol a sequence number, giving the order 3800 in which `write_syms' will write it. 3801 This is so we can store the proper symbolnum fields 3802 in relocation entries we write. */ 3803 3804 for (i = 0; i < TABSIZE; i++) 3805 { 3806 symbol *sp; 3807 for (sp = symtab[i]; sp; sp = sp->link) 3808 if (sp->referenced || sp->defined) 3809 { 3810 sp->def_count = count++; 3811 /* Leave room for the reference required by N_INDR, if 3812 necessary. */ 3813 if ((sp->defined & ~N_EXT) == N_INDR) 3814 count++; 3815 } 3816 } 3817 /* Correct, because if (relocatable_output), we will also be writing 3818 whatever indirect blocks we have. */ 3819 if (count != defined_global_sym_count 3820 + undefined_global_sym_count + global_indirect_count) 3821 fatal ("internal error"); 3822 3823 /* Write out the relocations of all files, remembered from copy_text. */ 3824 3825 each_full_file (coptxtrel, 0); 3826 3827 if (trace_files) 3828 fprintf (stderr, "\nWriting data relocation:\n\n"); 3829 3830 each_full_file (copdatrel, 0); 3831 3832 if (trace_files) 3833 fprintf (stderr, "\n"); 3834 } 3835 3836 void 3837 coptxtrel (entry) 3838 struct file_entry *entry; 3839 { 3840 register struct relocation_info *p, *end; 3841 register int reloc = entry->text_start_address; 3842 3843 p = entry->textrel; 3844 end = (struct relocation_info *) (entry->header.a_trsize + (char *) p); 3845 while (p < end) 3846 { 3847 RELOC_ADDRESS(p) += reloc; 3848 if (RELOC_EXTERN_P(p)) 3849 { 3850 register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist); 3851 symbol *symptr = ((symbol *) 3852 (((struct nlist *) 3853 (((char *)entry->symbols) + symindex)) 3854 ->n_un.n_name)); 3855 3856 if (symindex >= entry->header.a_syms) 3857 fatal_with_file ("relocation symbolnum out of range in ", entry); 3858 3859 #ifdef N_INDR 3860 /* Resolve indirection. */ 3861 if ((symptr->defined & ~N_EXT) == N_INDR) 3862 symptr = (symbol *) symptr->value; 3863 #endif 3864 3865 /* If the symbol is now defined, change the external relocation 3866 to an internal one. */ 3867 3868 if (symptr->defined) 3869 { 3870 RELOC_EXTERN_P(p) = 0; 3871 RELOC_SYMBOL(p) = (symptr->defined & N_TYPE); 3872 #ifdef RELOC_ADD_EXTRA 3873 /* If we aren't going to be adding in the value in 3874 memory on the next pass of the loader, then we need 3875 to add it in from the relocation entry. Otherwise 3876 the work we did in this pass is lost. */ 3877 if (!RELOC_MEMORY_ADD_P(p)) 3878 RELOC_ADD_EXTRA (p) += symptr->value; 3879 #endif 3880 } 3881 else 3882 /* Debugger symbols come first, so have to start this 3883 after them. */ 3884 RELOC_SYMBOL(p) = (symptr->def_count + nsyms 3885 - defined_global_sym_count 3886 - undefined_global_sym_count 3887 - global_indirect_count); 3888 } 3889 p++; 3890 } 3891 mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc); 3892 } 3893 3894 void 3895 copdatrel (entry) 3896 struct file_entry *entry; 3897 { 3898 register struct relocation_info *p, *end; 3899 /* Relocate the address of the relocation. 3900 Old address is relative to start of the input file's data section. 3901 New address is relative to start of the output file's data section. */ 3902 register int reloc = entry->data_start_address - text_size; 3903 3904 p = entry->datarel; 3905 end = (struct relocation_info *) (entry->header.a_drsize + (char *) p); 3906 while (p < end) 3907 { 3908 RELOC_ADDRESS(p) += reloc; 3909 if (RELOC_EXTERN_P(p)) 3910 { 3911 register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist); 3912 symbol *symptr = ((symbol *) 3913 (((struct nlist *) 3914 (((char *)entry->symbols) + symindex)) 3915 ->n_un.n_name)); 3916 int symtype; 3917 3918 if (symindex >= entry->header.a_syms) 3919 fatal_with_file ("relocation symbolnum out of range in ", entry); 3920 3921 #ifdef N_INDR 3922 /* Resolve indirection. */ 3923 if ((symptr->defined & ~N_EXT) == N_INDR) 3924 symptr = (symbol *) symptr->value; 3925 #endif 3926 3927 symtype = symptr->defined & N_TYPE; 3928 3929 if (force_common_definition 3930 || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS) 3931 { 3932 RELOC_EXTERN_P(p) = 0; 3933 RELOC_SYMBOL(p) = symtype; 3934 } 3935 else 3936 /* Debugger symbols come first, so have to start this 3937 after them. */ 3938 RELOC_SYMBOL(p) 3939 = (((symbol *) 3940 (((struct nlist *) 3941 (((char *)entry->symbols) + symindex)) 3942 ->n_un.n_name)) 3943 ->def_count 3944 + nsyms - defined_global_sym_count 3945 - undefined_global_sym_count 3946 - global_indirect_count); 3947 } 3948 p++; 3949 } 3950 mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc); 3951 } 3952 3953 void write_file_syms (); 3954 void write_string_table (); 3955 3956 /* Offsets and current lengths of symbol and string tables in output file. */ 3957 3958 int symbol_table_offset; 3959 int symbol_table_len; 3960 3961 /* Address in output file where string table starts. */ 3962 int string_table_offset; 3963 3964 /* Offset within string table 3965 where the strings in `strtab_vector' should be written. */ 3966 int string_table_len; 3967 3968 /* Total size of string table strings allocated so far, 3969 including strings in `strtab_vector'. */ 3970 int strtab_size; 3971 3972 /* Vector whose elements are strings to be added to the string table. */ 3973 char **strtab_vector; 3974 3975 /* Vector whose elements are the lengths of those strings. */ 3976 int *strtab_lens; 3977 3978 /* Index in `strtab_vector' at which the next string will be stored. */ 3979 int strtab_index; 3980 3981 /* Add the string NAME to the output file string table. 3982 Record it in `strtab_vector' to be output later. 3983 Return the index within the string table that this string will have. */ 3984 3985 int 3986 assign_string_table_index (name) 3987 char *name; 3988 { 3989 register int index = strtab_size; 3990 register int len = strlen (name) + 1; 3991 3992 strtab_size += len; 3993 strtab_vector[strtab_index] = name; 3994 strtab_lens[strtab_index++] = len; 3995 3996 return index; 3997 } 3998 3999 FILE *outstream = (FILE *) 0; 4000 4001 /* Write the contents of `strtab_vector' into the string table. 4002 This is done once for each file's local&debugger symbols 4003 and once for the global symbols. */ 4004 4005 void 4006 write_string_table () 4007 { 4008 register int i; 4009 4010 lseek (outdesc, string_table_offset + string_table_len, 0); 4011 4012 if (!outstream) 4013 outstream = fdopen (outdesc, "w"); 4014 4015 for (i = 0; i < strtab_index; i++) 4016 { 4017 fwrite (strtab_vector[i], 1, strtab_lens[i], outstream); 4018 string_table_len += strtab_lens[i]; 4019 } 4020 4021 fflush (outstream); 4022 4023 /* Report I/O error such as disk full. */ 4024 if (ferror (outstream)) 4025 perror_name (output_filename); 4026 } 4027 4028 /* Write the symbol table and string table of the output file. */ 4029 4030 void 4031 write_syms () 4032 { 4033 /* Number of symbols written so far. */ 4034 int syms_written = 0; 4035 register int i; 4036 register symbol *sp; 4037 4038 /* Buffer big enough for all the global symbols. One 4039 extra struct for each indirect symbol to hold the extra reference 4040 following. */ 4041 struct nlist *buf 4042 = (struct nlist *) alloca ((defined_global_sym_count 4043 + undefined_global_sym_count 4044 + global_indirect_count) 4045 * sizeof (struct nlist)); 4046 /* Pointer for storing into BUF. */ 4047 register struct nlist *bufp = buf; 4048 4049 /* Size of string table includes the bytes that store the size. */ 4050 strtab_size = sizeof strtab_size; 4051 4052 symbol_table_offset = N_SYMOFF (outheader); 4053 symbol_table_len = 0; 4054 string_table_offset = N_STROFF (outheader); 4055 string_table_len = strtab_size; 4056 4057 if (strip_symbols == STRIP_ALL) 4058 return; 4059 4060 /* Write the local symbols defined by the various files. */ 4061 4062 each_file (write_file_syms, &syms_written); 4063 file_close (); 4064 4065 /* Now write out the global symbols. */ 4066 4067 /* Allocate two vectors that record the data to generate the string 4068 table from the global symbols written so far. This must include 4069 extra space for the references following indirect outputs. */ 4070 4071 strtab_vector = (char **) alloca ((num_hash_tab_syms 4072 + global_indirect_count) * sizeof (char *)); 4073 strtab_lens = (int *) alloca ((num_hash_tab_syms 4074 + global_indirect_count) * sizeof (int)); 4075 strtab_index = 0; 4076 4077 /* Scan the symbol hash table, bucket by bucket. */ 4078 4079 for (i = 0; i < TABSIZE; i++) 4080 for (sp = symtab[i]; sp; sp = sp->link) 4081 { 4082 struct nlist nl; 4083 4084 nl.n_other = 0; 4085 nl.n_desc = 0; 4086 4087 /* Compute a `struct nlist' for the symbol. */ 4088 4089 if (sp->defined || sp->referenced) 4090 { 4091 /* common condition needs to be before undefined condition */ 4092 /* because unallocated commons are set undefined in */ 4093 /* digest_symbols */ 4094 if (sp->defined > 1) /* defined with known type */ 4095 { 4096 /* If the target of an indirect symbol has been 4097 defined and we are outputting an executable, 4098 resolve the indirection; it's no longer needed */ 4099 if (!relocatable_output 4100 && ((sp->defined & N_TYPE) == N_INDR) 4101 && (((symbol *) sp->value)->defined > 1)) 4102 { 4103 symbol *newsp = (symbol *) sp->value; 4104 nl.n_type = newsp->defined; 4105 nl.n_value = newsp->value; 4106 } 4107 else 4108 { 4109 nl.n_type = sp->defined; 4110 if (sp->defined != (N_INDR | N_EXT)) 4111 nl.n_value = sp->value; 4112 else 4113 nl.n_value = 0; 4114 } 4115 } 4116 else if (sp->max_common_size) /* defined as common but not allocated. */ 4117 { 4118 /* happens only with -r and not -d */ 4119 /* write out a common definition */ 4120 nl.n_type = N_UNDF | N_EXT; 4121 nl.n_value = sp->max_common_size; 4122 } 4123 else if (!sp->defined) /* undefined -- legit only if -r */ 4124 { 4125 nl.n_type = N_UNDF | N_EXT; 4126 nl.n_value = 0; 4127 } 4128 else 4129 fatal ("internal error: %s defined in mysterious way", sp->name); 4130 4131 /* Allocate string table space for the symbol name. */ 4132 4133 nl.n_un.n_strx = assign_string_table_index (sp->name); 4134 4135 /* Output to the buffer and count it. */ 4136 4137 *bufp++ = nl; 4138 syms_written++; 4139 if (nl.n_type == (N_INDR | N_EXT)) 4140 { 4141 struct nlist xtra_ref; 4142 xtra_ref.n_type = N_EXT | N_UNDF; 4143 xtra_ref.n_un.n_strx 4144 = assign_string_table_index (((symbol *) sp->value)->name); 4145 xtra_ref.n_other = 0; 4146 xtra_ref.n_desc = 0; 4147 xtra_ref.n_value = 0; 4148 *bufp++ = xtra_ref; 4149 syms_written++; 4150 } 4151 } 4152 } 4153 4154 /* Output the buffer full of `struct nlist's. */ 4155 4156 lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 4157 mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc); 4158 symbol_table_len += sizeof (struct nlist) * (bufp - buf); 4159 4160 if (syms_written != nsyms) 4161 fatal ("internal error: wrong number of symbols written into output file", 0); 4162 4163 if (symbol_table_offset + symbol_table_len != string_table_offset) 4164 fatal ("internal error: inconsistent symbol table length", 0); 4165 4166 /* Now the total string table size is known, so write it. 4167 We are already positioned at the right place in the file. */ 4168 4169 mywrite (&strtab_size, sizeof (int), 1, outdesc); /* we're at right place */ 4170 4171 /* Write the strings for the global symbols. */ 4172 4173 write_string_table (); 4174 } 4175 4176 /* Write the local and debugger symbols of file ENTRY. 4177 Increment *SYMS_WRITTEN_ADDR for each symbol that is written. */ 4178 4179 /* Note that we do not combine identical names of local symbols. 4180 dbx or gdb would be confused if we did that. */ 4181 4182 void 4183 write_file_syms (entry, syms_written_addr) 4184 struct file_entry *entry; 4185 int *syms_written_addr; 4186 { 4187 register struct nlist *p = entry->symbols; 4188 register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist); 4189 4190 /* Buffer to accumulate all the syms before writing them. 4191 It has one extra slot for the local symbol we generate here. */ 4192 struct nlist *buf 4193 = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist)); 4194 register struct nlist *bufp = buf; 4195 4196 /* Upper bound on number of syms to be written here. */ 4197 int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1; 4198 4199 /* Make tables that record, for each symbol, its name and its name's length. 4200 The elements are filled in by `assign_string_table_index'. */ 4201 4202 strtab_vector = (char **) alloca (max_syms * sizeof (char *)); 4203 strtab_lens = (int *) alloca (max_syms * sizeof (int)); 4204 strtab_index = 0; 4205 4206 /* Generate a local symbol for the start of this file's text. */ 4207 4208 if (discard_locals != DISCARD_ALL) 4209 { 4210 struct nlist nl; 4211 4212 nl.n_type = N_FN | N_EXT; 4213 nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); 4214 nl.n_value = entry->text_start_address; 4215 nl.n_desc = 0; 4216 nl.n_other = 0; 4217 *bufp++ = nl; 4218 (*syms_written_addr)++; 4219 entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist); 4220 } 4221 4222 /* Read the file's string table. */ 4223 4224 entry->strings = (char *) alloca (entry->string_size); 4225 read_entry_strings (file_open (entry), entry); 4226 4227 for (; p < end; p++) 4228 { 4229 register int type = p->n_type; 4230 register int write = 0; 4231 4232 /* WRITE gets 1 for a non-global symbol that should be written. */ 4233 4234 4235 if (SET_ELEMENT_P (type)) /* This occurs even if global. These */ 4236 /* types of symbols are never written */ 4237 /* globally, though they are stored */ 4238 /* globally. */ 4239 write = relocatable_output; 4240 else if (!(type & (N_STAB | N_EXT))) 4241 /* ordinary local symbol */ 4242 write = ((discard_locals != DISCARD_ALL) 4243 && !(discard_locals == DISCARD_L && 4244 (p->n_un.n_strx + entry->strings)[0] == LPREFIX) 4245 && type != N_WARNING); 4246 else if (!(type & N_EXT)) 4247 /* debugger symbol */ 4248 write = (strip_symbols == STRIP_NONE); 4249 4250 if (write) 4251 { 4252 /* If this symbol has a name, 4253 allocate space for it in the output string table. */ 4254 4255 if (p->n_un.n_strx) 4256 p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx 4257 + entry->strings); 4258 4259 /* Output this symbol to the buffer and count it. */ 4260 4261 *bufp++ = *p; 4262 (*syms_written_addr)++; 4263 } 4264 } 4265 4266 /* All the symbols are now in BUF; write them. */ 4267 4268 lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 4269 mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc); 4270 symbol_table_len += sizeof (struct nlist) * (bufp - buf); 4271 4272 /* Write the string-table data for the symbols just written, 4273 using the data in vectors `strtab_vector' and `strtab_lens'. */ 4274 4275 write_string_table (); 4276 entry->strings = 0; /* Since it will dissapear anyway. */ 4277 } 4278 4279 /* Copy any GDB symbol segments from the input files to the output file. 4280 The contents of the symbol segment is copied without change 4281 except that we store some information into the beginning of it. */ 4282 4283 void write_file_symseg (); 4284 4285 void 4286 write_symsegs () 4287 { 4288 each_file (write_file_symseg, 0); 4289 } 4290 4291 void 4292 write_file_symseg (entry) 4293 struct file_entry *entry; 4294 { 4295 char buffer[4096]; 4296 struct symbol_root root; 4297 int indesc; 4298 int len; 4299 4300 if (entry->symseg_offset == 0) 4301 return; 4302 4303 /* This entry has a symbol segment. Read the root of the segment. */ 4304 4305 indesc = file_open (entry); 4306 lseek (indesc, entry->symseg_offset + entry->starting_offset, 0); 4307 if (sizeof root != read (indesc, &root, sizeof root)) 4308 fatal_with_file ("premature end of file in symbol segment of ", entry); 4309 4310 /* Store some relocation info into the root. */ 4311 4312 root.ldsymoff = entry->local_syms_offset; 4313 root.textrel = entry->text_start_address; 4314 root.datarel = entry->data_start_address - entry->header.a_text; 4315 root.bssrel = entry->bss_start_address 4316 - entry->header.a_text - entry->header.a_data; 4317 root.databeg = entry->data_start_address - root.datarel; 4318 root.bssbeg = entry->bss_start_address - root.bssrel; 4319 4320 /* Write the modified root into the output file. */ 4321 4322 mywrite (&root, sizeof root, 1, outdesc); 4323 4324 /* Copy the rest of the symbol segment unchanged. */ 4325 4326 if (entry->superfile) 4327 { 4328 /* Library member: number of bytes to copy is determined 4329 from the member's total size. */ 4330 4331 int total = entry->total_size - entry->symseg_offset - sizeof root; 4332 4333 while (total > 0) 4334 { 4335 len = read (indesc, buffer, min (sizeof buffer, total)); 4336 4337 if (len != min (sizeof buffer, total)) 4338 fatal_with_file ("premature end of file in symbol segment of ", entry); 4339 total -= len; 4340 mywrite (buffer, len, 1, outdesc); 4341 } 4342 } 4343 else 4344 { 4345 /* A separate file: copy until end of file. */ 4346 4347 while (len = read (indesc, buffer, sizeof buffer)) 4348 { 4349 mywrite (buffer, len, 1, outdesc); 4350 if (len < sizeof buffer) 4351 break; 4352 } 4353 } 4354 4355 file_close (); 4356 } 4357 4358 /* Create the symbol table entries for `etext', `edata' and `end'. */ 4359 4360 void 4361 symtab_init () 4362 { 4363 #ifndef nounderscore 4364 edata_symbol = getsym ("_edata"); 4365 etext_symbol = getsym ("_etext"); 4366 end_symbol = getsym ("_end"); 4367 #else 4368 edata_symbol = getsym ("edata"); 4369 etext_symbol = getsym ("etext"); 4370 end_symbol = getsym ("end"); 4371 #endif 4372 4373 #ifdef sun 4374 { 4375 symbol *dynamic_symbol = getsym ("__DYNAMIC"); 4376 dynamic_symbol->defined = N_ABS | N_EXT; 4377 dynamic_symbol->referenced = 1; 4378 dynamic_symbol->value = 0; 4379 } 4380 #endif 4381 4382 #ifdef sequent 4383 { 4384 symbol *_387_flt_symbol = getsym ("_387_flt"); 4385 _387_flt_symbol->defined = N_ABS | N_EXT; 4386 _387_flt_symbol->referenced = 1; 4387 _387_flt_symbol->value = 0; 4388 } 4389 #endif 4390 4391 edata_symbol->defined = N_DATA | N_EXT; 4392 etext_symbol->defined = N_TEXT | N_EXT; 4393 end_symbol->defined = N_BSS | N_EXT; 4394 4395 edata_symbol->referenced = 1; 4396 etext_symbol->referenced = 1; 4397 end_symbol->referenced = 1; 4398 } 4399 4400 /* Compute the hash code for symbol name KEY. */ 4401 4402 int 4403 hash_string (key) 4404 char *key; 4405 { 4406 register char *cp; 4407 register int k; 4408 4409 cp = key; 4410 k = 0; 4411 while (*cp) 4412 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 4413 4414 return k; 4415 } 4416 4417 /* Get the symbol table entry for the global symbol named KEY. 4418 Create one if there is none. */ 4419 4420 symbol * 4421 getsym (key) 4422 char *key; 4423 { 4424 register int hashval; 4425 register symbol *bp; 4426 4427 /* Determine the proper bucket. */ 4428 4429 hashval = hash_string (key) % TABSIZE; 4430 4431 /* Search the bucket. */ 4432 4433 for (bp = symtab[hashval]; bp; bp = bp->link) 4434 if (! strcmp (key, bp->name)) 4435 return bp; 4436 4437 /* Nothing was found; create a new symbol table entry. */ 4438 4439 bp = (symbol *) xmalloc (sizeof (symbol)); 4440 bp->refs = 0; 4441 bp->name = (char *) xmalloc (strlen (key) + 1); 4442 strcpy (bp->name, key); 4443 bp->defined = 0; 4444 bp->referenced = 0; 4445 bp->trace = 0; 4446 bp->value = 0; 4447 bp->max_common_size = 0; 4448 bp->warning = 0; 4449 bp->undef_refs = 0; 4450 bp->multiply_defined = 0; 4451 4452 /* Add the entry to the bucket. */ 4453 4454 bp->link = symtab[hashval]; 4455 symtab[hashval] = bp; 4456 4457 ++num_hash_tab_syms; 4458 4459 return bp; 4460 } 4461 4462 /* Like `getsym' but return 0 if the symbol is not already known. */ 4463 4464 symbol * 4465 getsym_soft (key) 4466 char *key; 4467 { 4468 register int hashval; 4469 register symbol *bp; 4470 4471 /* Determine which bucket. */ 4472 4473 hashval = hash_string (key) % TABSIZE; 4474 4475 /* Search the bucket. */ 4476 4477 for (bp = symtab[hashval]; bp; bp = bp->link) 4478 if (! strcmp (key, bp->name)) 4479 return bp; 4480 4481 return 0; 4482 } 4483 4484 /* Report a fatal error. 4485 STRING is a printf format string and ARG is one arg for it. */ 4486 4487 void 4488 fatal (string, arg) 4489 char *string, *arg; 4490 { 4491 fprintf (stderr, "ld: "); 4492 fprintf (stderr, string, arg); 4493 fprintf (stderr, "\n"); 4494 exit (1); 4495 } 4496 4497 /* Report a fatal error. The error message is STRING 4498 followed by the filename of ENTRY. */ 4499 4500 void 4501 fatal_with_file (string, entry) 4502 char *string; 4503 struct file_entry *entry; 4504 { 4505 fprintf (stderr, "ld: "); 4506 fprintf (stderr, string); 4507 print_file_name (entry, stderr); 4508 fprintf (stderr, "\n"); 4509 exit (1); 4510 } 4511 4512 /* Report a fatal error using the message for the last failed system call, 4513 followed by the string NAME. */ 4514 4515 void 4516 perror_name (name) 4517 char *name; 4518 { 4519 extern int errno, sys_nerr; 4520 extern char *sys_errlist[]; 4521 char *s; 4522 4523 if (errno < sys_nerr) 4524 s = concat ("", sys_errlist[errno], " for %s"); 4525 else 4526 s = "cannot open %s"; 4527 fatal (s, name); 4528 } 4529 4530 /* Report a fatal error using the message for the last failed system call, 4531 followed by the name of file ENTRY. */ 4532 4533 void 4534 perror_file (entry) 4535 struct file_entry *entry; 4536 { 4537 extern int errno, sys_nerr; 4538 extern char *sys_errlist[]; 4539 char *s; 4540 4541 if (errno < sys_nerr) 4542 s = concat ("", sys_errlist[errno], " for "); 4543 else 4544 s = "cannot open "; 4545 fatal_with_file (s, entry); 4546 } 4547 4548 /* Report a nonfatal error. 4549 STRING is a format for printf, and ARG1 ... ARG3 are args for it. */ 4550 4551 void 4552 error (string, arg1, arg2, arg3) 4553 char *string, *arg1, *arg2, *arg3; 4554 { 4555 fprintf (stderr, "%s: ", progname); 4556 fprintf (stderr, string, arg1, arg2, arg3); 4557 fprintf (stderr, "\n"); 4558 } 4559 4560 4561 /* Output COUNT*ELTSIZE bytes of data at BUF 4562 to the descriptor DESC. */ 4563 4564 void 4565 mywrite (buf, count, eltsize, desc) 4566 char *buf; 4567 int count; 4568 int eltsize; 4569 int desc; 4570 { 4571 register int val; 4572 register int bytes = count * eltsize; 4573 4574 while (bytes > 0) 4575 { 4576 val = write (desc, buf, bytes); 4577 if (val <= 0) 4578 perror_name (output_filename); 4579 buf += val; 4580 bytes -= val; 4581 } 4582 } 4583 4584 /* Output PADDING zero-bytes to descriptor OUTDESC. 4585 PADDING may be negative; in that case, do nothing. */ 4586 4587 void 4588 padfile (padding, outdesc) 4589 int padding; 4590 int outdesc; 4591 { 4592 register char *buf; 4593 if (padding <= 0) 4594 return; 4595 4596 buf = (char *) alloca (padding); 4597 bzero (buf, padding); 4598 mywrite (buf, padding, 1, outdesc); 4599 } 4600 4601 /* Return a newly-allocated string 4602 whose contents concatenate the strings S1, S2, S3. */ 4603 4604 char * 4605 concat (s1, s2, s3) 4606 char *s1, *s2, *s3; 4607 { 4608 register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); 4609 register char *result = (char *) xmalloc (len1 + len2 + len3 + 1); 4610 4611 strcpy (result, s1); 4612 strcpy (result + len1, s2); 4613 strcpy (result + len1 + len2, s3); 4614 result[len1 + len2 + len3] = 0; 4615 4616 return result; 4617 } 4618 4619 /* Parse the string ARG using scanf format FORMAT, and return the result. 4620 If it does not parse, report fatal error 4621 generating the error message using format string ERROR and ARG as arg. */ 4622 4623 int 4624 parse (arg, format, error) 4625 char *arg, *format; 4626 { 4627 int x; 4628 if (1 != sscanf (arg, format, &x)) 4629 fatal (error, arg); 4630 return x; 4631 } 4632 4633 /* Like malloc but get fatal error if memory is exhausted. */ 4634 4635 int 4636 xmalloc (size) 4637 int size; 4638 { 4639 register int result = malloc (size); 4640 if (!result) 4641 fatal ("virtual memory exhausted", 0); 4642 return result; 4643 } 4644 4645 /* Like realloc but get fatal error if memory is exhausted. */ 4646 4647 int 4648 xrealloc (ptr, size) 4649 char *ptr; 4650 int size; 4651 { 4652 register int result = realloc (ptr, size); 4653 if (!result) 4654 fatal ("virtual memory exhausted", 0); 4655 return result; 4656 } 4657 4658 #ifdef USG 4659 4660 void 4661 bzero (p, n) 4662 char *p; 4663 { 4664 memset (p, 0, n); 4665 } 4666 4667 void 4668 bcopy (from, to, n) 4669 char *from, *to; 4670 { 4671 memcpy (to, from, n); 4672 } 4673 4674 getpagesize () 4675 { 4676 return (4096); 4677 } 4678 4679 #endif 4680 4681 #if defined(sun) && (TARGET == SUN4) 4682 4683 /* Don't use local pagesize to build for Sparc. */ 4684 4685 getpagesize () 4686 { 4687 return (8192); 4688 } 4689 #endif 4690