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.9 (Berkeley) 05/02/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 986 number_of_files = 0; 987 output_filename = "a.out"; 988 989 n_search_dirs = 0; 990 search_dirs = (char **) xmalloc (sizeof (char *)); 991 992 /* First compute number_of_files so we know how long to make file_table. */ 993 /* Also process most options completely. */ 994 995 for (i = 1; i < argc; i++) 996 { 997 register int code = classify_arg (argv[i]); 998 if (code) 999 { 1000 if (i + code > argc) 1001 fatal ("no argument following %s\n", argv[i]); 1002 1003 decode_option (argv[i], argv[i+1]); 1004 1005 if (argv[i][1] == 'l' || argv[i][1] == 'A') 1006 number_of_files++; 1007 1008 i += code - 1; 1009 } 1010 else 1011 number_of_files++; 1012 } 1013 1014 if (!number_of_files) 1015 fatal ("no input files", 0); 1016 1017 p = file_table 1018 = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry)); 1019 bzero (p, number_of_files * sizeof (struct file_entry)); 1020 1021 /* Now scan again and fill in file_table. */ 1022 /* All options except -A and -l are ignored here. */ 1023 1024 for (i = 1; i < argc; i++) 1025 { 1026 register int code = classify_arg (argv[i]); 1027 1028 if (code) 1029 { 1030 char *string; 1031 if (code == 2) 1032 string = argv[i+1]; 1033 else 1034 string = &argv[i][2]; 1035 1036 if (argv[i][1] == 'A') 1037 { 1038 if (p != file_table) 1039 fatal ("-A specified before an input file other than the first"); 1040 1041 p->filename = string; 1042 p->local_sym_name = string; 1043 p->just_syms_flag = 1; 1044 p++; 1045 } 1046 if (argv[i][1] == 'l') 1047 { 1048 p->filename = concat ("lib", string, ".a"); 1049 p->local_sym_name = concat ("-l", string, ""); 1050 p->search_dirs_flag = 1; 1051 p++; 1052 } 1053 i += code - 1; 1054 } 1055 else 1056 { 1057 p->filename = argv[i]; 1058 p->local_sym_name = argv[i]; 1059 p++; 1060 } 1061 } 1062 1063 /* Now check some option settings for consistency. */ 1064 1065 #ifdef NMAGIC 1066 if ((magic == ZMAGIC || magic == NMAGIC) 1067 #else 1068 if ((magic == ZMAGIC) 1069 #endif 1070 && (text_start - text_start_alignment) & (page_size - 1)) 1071 fatal ("-T argument not multiple of page size, with sharable output", 0); 1072 1073 /* Append the standard search directories to the user-specified ones. */ 1074 { 1075 int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0]; 1076 n_search_dirs += n; 1077 search_dirs 1078 = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *)); 1079 bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n], 1080 n * sizeof (char *)); 1081 } 1082 } 1083 1084 1085 void 1086 add_cmdline_ref (sp) 1087 struct glosym *sp; 1088 { 1089 struct glosym **ptr; 1090 1091 for (ptr = cmdline_references; 1092 ptr < cmdline_references + cl_refs_allocated && *ptr; 1093 ptr++) 1094 ; 1095 1096 if (ptr >= cmdline_references + cl_refs_allocated - 1) 1097 { 1098 int diff = ptr - cmdline_references; 1099 1100 cl_refs_allocated *= 2; 1101 cmdline_references = (struct glosym **) 1102 xrealloc (cmdline_references, 1103 cl_refs_allocated * sizeof (struct glosym *)); 1104 ptr = cmdline_references + diff; 1105 } 1106 1107 *ptr++ = sp; 1108 *ptr = (struct glosym *) 0; 1109 } 1110 1111 int 1112 set_element_prefixed_p (name) 1113 char *name; 1114 { 1115 struct string_list_element *p; 1116 int i; 1117 1118 for (p = set_element_prefixes; p; p = p->next) 1119 { 1120 for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++) 1121 ; 1122 1123 if (p->str[i] == '\0') 1124 return 1; 1125 } 1126 return 0; 1127 } 1128 1129 int parse (); 1130 1131 /* Record an option and arrange to act on it later. 1132 ARG should be the following command argument, 1133 which may or may not be used by this option. 1134 1135 The `l' and `A' options are ignored here since they actually 1136 specify input files. */ 1137 1138 void 1139 decode_option (swt, arg) 1140 register char *swt, *arg; 1141 { 1142 /* We get Bstatic from gcc on suns. */ 1143 if (! strcmp (swt + 1, "Bstatic")) 1144 return; 1145 if (! strcmp (swt + 1, "Ttext")) 1146 { 1147 text_start = parse (arg, "%x", "invalid argument to -Ttext"); 1148 T_flag_specified = 1; 1149 return; 1150 } 1151 if (! strcmp (swt + 1, "Tdata")) 1152 { 1153 data_start = parse (arg, "%x", "invalid argument to -Tdata"); 1154 Tdata_flag_specified = 1; 1155 return; 1156 } 1157 if (! strcmp (swt + 1, "noinhibit-exec")) 1158 { 1159 force_executable = 1; 1160 return; 1161 } 1162 1163 if (swt[2] != 0) 1164 arg = &swt[2]; 1165 1166 switch (swt[1]) 1167 { 1168 case 'A': 1169 return; 1170 1171 case 'D': 1172 specified_data_size = parse (arg, "%x", "invalid argument to -D"); 1173 return; 1174 1175 case 'd': 1176 force_common_definition = 1; 1177 return; 1178 1179 case 'e': 1180 entry_symbol = getsym (arg); 1181 if (!entry_symbol->defined && !entry_symbol->referenced) 1182 undefined_global_sym_count++; 1183 entry_symbol->referenced = 1; 1184 add_cmdline_ref (entry_symbol); 1185 return; 1186 1187 case 'l': 1188 /* If linking with libg++, use the C++ demangler. */ 1189 if (arg != NULL && strcmp (arg, "g++") == 0) 1190 demangler = cplus_demangle; 1191 return; 1192 1193 case 'L': 1194 n_search_dirs++; 1195 search_dirs 1196 = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *)); 1197 search_dirs[n_search_dirs - 1] = arg; 1198 return; 1199 1200 case 'M': 1201 write_map = 1; 1202 return; 1203 1204 case 'N': 1205 magic = OMAGIC; 1206 return; 1207 1208 #ifdef NMAGIC 1209 case 'n': 1210 magic = NMAGIC; 1211 return; 1212 #endif 1213 1214 case 'o': 1215 output_filename = arg; 1216 return; 1217 1218 case 'r': 1219 relocatable_output = 1; 1220 magic = OMAGIC; 1221 text_start = 0; 1222 return; 1223 1224 case 'S': 1225 strip_symbols = STRIP_DEBUGGER; 1226 return; 1227 1228 case 's': 1229 strip_symbols = STRIP_ALL; 1230 return; 1231 1232 case 'T': 1233 text_start = parse (arg, "%x", "invalid argument to -T"); 1234 T_flag_specified = 1; 1235 return; 1236 1237 case 't': 1238 trace_files = 1; 1239 return; 1240 1241 case 'u': 1242 { 1243 register symbol *sp = getsym (arg); 1244 if (!sp->defined && !sp->referenced) 1245 undefined_global_sym_count++; 1246 sp->referenced = 1; 1247 add_cmdline_ref (sp); 1248 } 1249 return; 1250 1251 case 'V': 1252 { 1253 struct string_list_element *new 1254 = (struct string_list_element *) 1255 xmalloc (sizeof (struct string_list_element)); 1256 1257 new->str = arg; 1258 new->next = set_element_prefixes; 1259 set_element_prefixes = new; 1260 return; 1261 } 1262 1263 case 'X': 1264 discard_locals = DISCARD_L; 1265 return; 1266 1267 case 'x': 1268 discard_locals = DISCARD_ALL; 1269 return; 1270 1271 case 'y': 1272 { 1273 register symbol *sp = getsym (&swt[2]); 1274 sp->trace = 1; 1275 } 1276 return; 1277 1278 case 'z': 1279 magic = ZMAGIC; 1280 return; 1281 1282 default: 1283 fatal ("invalid command option `%s'", swt); 1284 } 1285 } 1286 1287 /** Convenient functions for operating on one or all files being */ 1288 /** loaded. */ 1289 void print_file_name (); 1290 1291 /* Call FUNCTION on each input file entry. 1292 Do not call for entries for libraries; 1293 instead, call once for each library member that is being loaded. 1294 1295 FUNCTION receives two arguments: the entry, and ARG. */ 1296 1297 void 1298 each_file (function, arg) 1299 register void (*function)(); 1300 register int arg; 1301 { 1302 register int i; 1303 1304 for (i = 0; i < number_of_files; i++) 1305 { 1306 register struct file_entry *entry = &file_table[i]; 1307 if (entry->library_flag) 1308 { 1309 register struct file_entry *subentry = entry->subfiles; 1310 for (; subentry; subentry = subentry->chain) 1311 (*function) (subentry, arg); 1312 } 1313 else 1314 (*function) (entry, arg); 1315 } 1316 } 1317 1318 /* Call FUNCTION on each input file entry until it returns a non-zero 1319 value. Return this value. 1320 Do not call for entries for libraries; 1321 instead, call once for each library member that is being loaded. 1322 1323 FUNCTION receives two arguments: the entry, and ARG. It must be a 1324 function returning unsigned long (though this can probably be fudged). */ 1325 1326 unsigned long 1327 check_each_file (function, arg) 1328 register unsigned long (*function)(); 1329 register int arg; 1330 { 1331 register int i; 1332 register unsigned long return_val; 1333 1334 for (i = 0; i < number_of_files; i++) 1335 { 1336 register struct file_entry *entry = &file_table[i]; 1337 if (entry->library_flag) 1338 { 1339 register struct file_entry *subentry = entry->subfiles; 1340 for (; subentry; subentry = subentry->chain) 1341 if (return_val = (*function) (subentry, arg)) 1342 return return_val; 1343 } 1344 else 1345 if (return_val = (*function) (entry, arg)) 1346 return return_val; 1347 } 1348 return 0; 1349 } 1350 1351 /* Like `each_file' but ignore files that were just for symbol definitions. */ 1352 1353 void 1354 each_full_file (function, arg) 1355 register void (*function)(); 1356 register int arg; 1357 { 1358 register int i; 1359 1360 for (i = 0; i < number_of_files; i++) 1361 { 1362 register struct file_entry *entry = &file_table[i]; 1363 if (entry->just_syms_flag) 1364 continue; 1365 if (entry->library_flag) 1366 { 1367 register struct file_entry *subentry = entry->subfiles; 1368 for (; subentry; subentry = subentry->chain) 1369 (*function) (subentry, arg); 1370 } 1371 else 1372 (*function) (entry, arg); 1373 } 1374 } 1375 1376 /* Close the input file that is now open. */ 1377 1378 void 1379 file_close () 1380 { 1381 close (input_desc); 1382 input_desc = 0; 1383 input_file = 0; 1384 } 1385 1386 /* Open the input file specified by 'entry', and return a descriptor. 1387 The open file is remembered; if the same file is opened twice in a row, 1388 a new open is not actually done. */ 1389 1390 int 1391 file_open (entry) 1392 register struct file_entry *entry; 1393 { 1394 register int desc; 1395 1396 if (entry->superfile) 1397 return file_open (entry->superfile); 1398 1399 if (entry == input_file) 1400 return input_desc; 1401 1402 if (input_file) file_close (); 1403 1404 if (entry->search_dirs_flag) 1405 { 1406 int i; 1407 1408 for (i = 0; i < n_search_dirs; i++) 1409 { 1410 register char *string 1411 = concat (search_dirs[i], "/", entry->filename); 1412 desc = open (string, O_RDONLY, 0); 1413 if (desc > 0) 1414 { 1415 entry->filename = string; 1416 entry->search_dirs_flag = 0; 1417 break; 1418 } 1419 free (string); 1420 } 1421 } 1422 else 1423 desc = open (entry->filename, O_RDONLY, 0); 1424 1425 if (desc > 0) 1426 { 1427 input_file = entry; 1428 input_desc = desc; 1429 return desc; 1430 } 1431 1432 perror_file (entry); 1433 /* NOTREACHED */ 1434 } 1435 1436 /* Print the filename of ENTRY on OUTFILE (a stdio stream), 1437 and then a newline. */ 1438 1439 void 1440 prline_file_name (entry, outfile) 1441 struct file_entry *entry; 1442 FILE *outfile; 1443 { 1444 print_file_name (entry, outfile); 1445 fprintf (outfile, "\n"); 1446 } 1447 1448 /* Print the filename of ENTRY on OUTFILE (a stdio stream). */ 1449 1450 void 1451 print_file_name (entry, outfile) 1452 struct file_entry *entry; 1453 FILE *outfile; 1454 { 1455 if (entry->superfile) 1456 { 1457 print_file_name (entry->superfile, outfile); 1458 fprintf (outfile, "(%s)", entry->filename); 1459 } 1460 else 1461 fprintf (outfile, "%s", entry->filename); 1462 } 1463 1464 /* Return the filename of entry as a string (malloc'd for the purpose) */ 1465 1466 char * 1467 get_file_name (entry) 1468 struct file_entry *entry; 1469 { 1470 char *result, *supfile; 1471 if (entry->superfile) 1472 { 1473 supfile = get_file_name (entry->superfile); 1474 result = (char *) xmalloc (strlen (supfile) 1475 + strlen (entry->filename) + 3); 1476 sprintf (result, "%s(%s)", supfile, entry->filename); 1477 free (supfile); 1478 } 1479 else 1480 { 1481 result = (char *) xmalloc (strlen (entry->filename) + 1); 1482 strcpy (result, entry->filename); 1483 } 1484 return result; 1485 } 1486 1487 /* Medium-level input routines for rel files. */ 1488 1489 /* Read a file's header into the proper place in the file_entry. 1490 DESC is the descriptor on which the file is open. 1491 ENTRY is the file's entry. */ 1492 1493 void 1494 read_header (desc, entry) 1495 int desc; 1496 register struct file_entry *entry; 1497 { 1498 register int len; 1499 struct exec *loc = (struct exec *) &entry->header; 1500 1501 lseek (desc, entry->starting_offset, 0); 1502 #ifdef COFF_ENCAPSULATE 1503 if (entry->just_syms_flag) 1504 lseek (desc, sizeof(coffheader), 1); 1505 #endif 1506 len = read (desc, loc, sizeof (struct exec)); 1507 if (len != sizeof (struct exec)) 1508 fatal_with_file ("failure reading header of ", entry); 1509 if (N_BADMAG (*loc)) 1510 fatal_with_file ("bad magic number in ", entry); 1511 1512 entry->header_read_flag = 1; 1513 } 1514 1515 /* Read the symbols of file ENTRY into core. 1516 Assume it is already open, on descriptor DESC. 1517 Also read the length of the string table, which follows the symbol table, 1518 but don't read the contents of the string table. */ 1519 1520 void 1521 read_entry_symbols (desc, entry) 1522 struct file_entry *entry; 1523 int desc; 1524 { 1525 int str_size; 1526 1527 if (!entry->header_read_flag) 1528 read_header (desc, entry); 1529 1530 entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms); 1531 1532 lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0); 1533 if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms)) 1534 fatal_with_file ("premature end of file in symbols of ", entry); 1535 1536 lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0); 1537 if (sizeof str_size != read (desc, &str_size, sizeof str_size)) 1538 fatal_with_file ("bad string table size in ", entry); 1539 1540 entry->string_size = str_size; 1541 } 1542 1543 /* Read the string table of file ENTRY into core. 1544 Assume it is already open, on descriptor DESC. 1545 Also record whether a GDB symbol segment follows the string table. */ 1546 1547 void 1548 read_entry_strings (desc, entry) 1549 struct file_entry *entry; 1550 int desc; 1551 { 1552 int buffer; 1553 1554 if (!entry->header_read_flag) 1555 read_header (desc, entry); 1556 1557 lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0); 1558 if (entry->string_size != read (desc, entry->strings, entry->string_size)) 1559 fatal_with_file ("premature end of file in strings of ", entry); 1560 1561 /* While we are here, see if the file has a symbol segment at the end. 1562 For a separate file, just try reading some more. 1563 For a library member, compare current pos against total size. */ 1564 if (entry->superfile) 1565 { 1566 if (entry->total_size == N_STROFF (entry->header) + entry->string_size) 1567 return; 1568 } 1569 else 1570 { 1571 buffer = read (desc, &buffer, sizeof buffer); 1572 if (buffer == 0) 1573 return; 1574 if (buffer != sizeof buffer) 1575 fatal_with_file ("premature end of file in GDB symbol segment of ", entry); 1576 } 1577 /* Don't try to do anything with symsegs. */ 1578 return; 1579 #if 0 1580 /* eliminate warning of `statement not reached'. */ 1581 entry->symseg_offset = N_STROFF (entry->header) + entry->string_size; 1582 #endif 1583 } 1584 1585 /* Read in the symbols of all input files. */ 1586 1587 void read_file_symbols (), read_entry_symbols (), read_entry_strings (); 1588 void enter_file_symbols (), enter_global_ref (), search_library (); 1589 1590 void 1591 load_symbols () 1592 { 1593 register int i; 1594 1595 if (trace_files) fprintf (stderr, "Loading symbols:\n\n"); 1596 1597 for (i = 0; i < number_of_files; i++) 1598 { 1599 register struct file_entry *entry = &file_table[i]; 1600 read_file_symbols (entry); 1601 } 1602 1603 if (trace_files) fprintf (stderr, "\n"); 1604 } 1605 1606 /* If ENTRY is a rel file, read its symbol and string sections into core. 1607 If it is a library, search it and load the appropriate members 1608 (which means calling this function recursively on those members). */ 1609 1610 void 1611 read_file_symbols (entry) 1612 register struct file_entry *entry; 1613 { 1614 register int desc; 1615 register int len; 1616 struct exec hdr; 1617 1618 desc = file_open (entry); 1619 1620 #ifdef COFF_ENCAPSULATE 1621 if (entry->just_syms_flag) 1622 lseek (desc, sizeof(coffheader),0); 1623 #endif 1624 1625 len = read (desc, &hdr, sizeof hdr); 1626 if (len != sizeof hdr) 1627 fatal_with_file ("failure reading header of ", entry); 1628 1629 if (!N_BADMAG (hdr)) 1630 { 1631 read_entry_symbols (desc, entry); 1632 entry->strings = (char *) alloca (entry->string_size); 1633 read_entry_strings (desc, entry); 1634 enter_file_symbols (entry); 1635 entry->strings = 0; 1636 } 1637 else 1638 { 1639 char armag[SARMAG]; 1640 1641 lseek (desc, 0, 0); 1642 if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG)) 1643 fatal_with_file ("malformed input file (not rel or archive) ", entry); 1644 entry->library_flag = 1; 1645 search_library (desc, entry); 1646 } 1647 1648 file_close (); 1649 } 1650 1651 /* Enter the external symbol defs and refs of ENTRY in the hash table. */ 1652 1653 void 1654 enter_file_symbols (entry) 1655 struct file_entry *entry; 1656 { 1657 register struct nlist 1658 *p, 1659 *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 1660 1661 if (trace_files) prline_file_name (entry, stderr); 1662 1663 for (p = entry->symbols; p < end; p++) 1664 { 1665 if (p->n_type == (N_SETV | N_EXT)) continue; 1666 if (set_element_prefixes 1667 && set_element_prefixed_p (p->n_un.n_strx + entry->strings)) 1668 p->n_type += (N_SETA - N_ABS); 1669 1670 if (SET_ELEMENT_P (p->n_type)) 1671 { 1672 set_symbol_count++; 1673 if (!relocatable_output) 1674 enter_global_ref (p, p->n_un.n_strx + entry->strings, entry); 1675 } 1676 else if (p->n_type == N_WARNING) 1677 { 1678 char *name = p->n_un.n_strx + entry->strings; 1679 1680 /* Grab the next entry. */ 1681 p++; 1682 if (p->n_type != (N_UNDF | N_EXT)) 1683 { 1684 fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n", 1685 progname, entry->filename); 1686 make_executable = 0; 1687 p--; /* Process normally. */ 1688 } 1689 else 1690 { 1691 symbol *sp; 1692 char *sname = p->n_un.n_strx + entry->strings; 1693 /* Deal with the warning symbol. */ 1694 enter_global_ref (p, p->n_un.n_strx + entry->strings, entry); 1695 sp = getsym (sname); 1696 sp->warning = (char *) xmalloc (strlen(name) + 1); 1697 strcpy (sp->warning, name); 1698 warning_count++; 1699 } 1700 } 1701 else if (p->n_type & N_EXT) 1702 enter_global_ref (p, p->n_un.n_strx + entry->strings, entry); 1703 else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT))) 1704 { 1705 if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX) 1706 non_L_local_sym_count++; 1707 local_sym_count++; 1708 } 1709 else debugger_sym_count++; 1710 } 1711 1712 /* Count one for the local symbol that we generate, 1713 whose name is the file's name (usually) and whose address 1714 is the start of the file's text. */ 1715 1716 local_sym_count++; 1717 non_L_local_sym_count++; 1718 } 1719 1720 /* Enter one global symbol in the hash table. 1721 NLIST_P points to the `struct nlist' read from the file 1722 that describes the global symbol. NAME is the symbol's name. 1723 ENTRY is the file entry for the file the symbol comes from. 1724 1725 The `struct nlist' is modified by placing it on a chain of 1726 all such structs that refer to the same global symbol. 1727 This chain starts in the `refs' field of the symbol table entry 1728 and is chained through the `n_name'. */ 1729 1730 void 1731 enter_global_ref (nlist_p, name, entry) 1732 register struct nlist *nlist_p; 1733 char *name; 1734 struct file_entry *entry; 1735 { 1736 register symbol *sp = getsym (name); 1737 register int type = nlist_p->n_type; 1738 int oldref = sp->referenced; 1739 int olddef = sp->defined; 1740 1741 nlist_p->n_un.n_name = (char *) sp->refs; 1742 sp->refs = nlist_p; 1743 1744 sp->referenced = 1; 1745 if (type != (N_UNDF | N_EXT) || nlist_p->n_value) 1746 { 1747 if (!sp->defined || sp->defined == (N_UNDF | N_EXT)) 1748 sp->defined = type; 1749 1750 if (oldref && !olddef) 1751 /* It used to be undefined and we're defining it. */ 1752 undefined_global_sym_count--; 1753 1754 if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value) 1755 { 1756 /* First definition and it's common. */ 1757 common_defined_global_count++; 1758 sp->max_common_size = nlist_p->n_value; 1759 } 1760 else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT)) 1761 { 1762 /* It used to be common and we're defining it as 1763 something else. */ 1764 common_defined_global_count--; 1765 sp->max_common_size = 0; 1766 } 1767 else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT) 1768 && sp->max_common_size < nlist_p->n_value) 1769 /* It used to be common and this is a new common entry to 1770 which we need to pay attention. */ 1771 sp->max_common_size = nlist_p->n_value; 1772 1773 /* Are we defining it as a set element? */ 1774 if (SET_ELEMENT_P (type) 1775 && (!olddef || (olddef && sp->max_common_size))) 1776 set_vector_count++; 1777 /* As an indirection? */ 1778 else if (type == (N_INDR | N_EXT)) 1779 { 1780 /* Indirect symbols value should be modified to point 1781 a symbol being equivalenced to. */ 1782 nlist_p->n_value 1783 = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx 1784 + entry->strings); 1785 if ((symbol *) nlist_p->n_value == sp) 1786 { 1787 /* Somebody redefined a symbol to be itself. */ 1788 fprintf (stderr, "%s: Symbol %s indirected to itself.\n", 1789 entry->filename, name); 1790 /* Rewrite this symbol as being a global text symbol 1791 with value 0. */ 1792 nlist_p->n_type = sp->defined = N_TEXT | N_EXT; 1793 nlist_p->n_value = 0; 1794 /* Don't make the output executable. */ 1795 make_executable = 0; 1796 } 1797 else 1798 global_indirect_count++; 1799 } 1800 } 1801 else 1802 if (!oldref) 1803 #ifndef DOLLAR_KLUDGE 1804 undefined_global_sym_count++; 1805 #else 1806 { 1807 if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$') 1808 { 1809 /* This is an (ISI?) $-conditional; skip it */ 1810 sp->referenced = 0; 1811 if (sp->trace) 1812 { 1813 fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name); 1814 print_file_name (entry, stderr); 1815 fprintf (stderr, "\n"); 1816 } 1817 return; 1818 } 1819 else 1820 undefined_global_sym_count++; 1821 } 1822 #endif 1823 1824 if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified) 1825 text_start = nlist_p->n_value; 1826 1827 if (sp->trace) 1828 { 1829 register char *reftype; 1830 switch (type & N_TYPE) 1831 { 1832 case N_UNDF: 1833 if (nlist_p->n_value) 1834 reftype = "defined as common"; 1835 else reftype = "referenced"; 1836 break; 1837 1838 case N_ABS: 1839 reftype = "defined as absolute"; 1840 break; 1841 1842 case N_TEXT: 1843 reftype = "defined in text section"; 1844 break; 1845 1846 case N_DATA: 1847 reftype = "defined in data section"; 1848 break; 1849 1850 case N_BSS: 1851 reftype = "defined in BSS section"; 1852 break; 1853 1854 case N_SETT: 1855 reftype = "is a text set element"; 1856 break; 1857 1858 case N_SETD: 1859 reftype = "is a data set element"; 1860 break; 1861 1862 case N_SETB: 1863 reftype = "is a BSS set element"; 1864 break; 1865 1866 case N_SETA: 1867 reftype = "is an absolute set element"; 1868 break; 1869 1870 case N_SETV: 1871 reftype = "defined in data section as vector"; 1872 break; 1873 1874 case N_INDR: 1875 reftype = (char *) alloca (23 1876 + strlen ((nlist_p + 1)->n_un.n_strx 1877 + entry->strings)); 1878 sprintf (reftype, "defined equivalent to %s", 1879 (nlist_p + 1)->n_un.n_strx + entry->strings); 1880 break; 1881 1882 #ifdef sequent 1883 case N_SHUNDF: 1884 reftype = "shared undf"; 1885 break; 1886 1887 /* These conflict with cases above. 1888 case N_SHDATA: 1889 reftype = "shared data"; 1890 break; 1891 1892 case N_SHBSS: 1893 reftype = "shared BSS"; 1894 break; 1895 */ 1896 default: 1897 reftype = "I don't know this type"; 1898 break; 1899 #endif 1900 } 1901 1902 fprintf (stderr, "symbol %s %s in ", sp->name, reftype); 1903 print_file_name (entry, stderr); 1904 fprintf (stderr, "\n"); 1905 } 1906 } 1907 1908 /* This return 0 if the given file entry's symbol table does *not* 1909 contain the nlist point entry, and it returns the files entry 1910 pointer (cast to unsigned long) if it does. */ 1911 1912 unsigned long 1913 contains_symbol (entry, n_ptr) 1914 struct file_entry *entry; 1915 register struct nlist *n_ptr; 1916 { 1917 if (n_ptr >= entry->symbols && 1918 n_ptr < (entry->symbols 1919 + (entry->header.a_syms / sizeof (struct nlist)))) 1920 return (unsigned long) entry; 1921 return 0; 1922 } 1923 1924 1925 /* Searching libraries */ 1926 1927 struct file_entry *decode_library_subfile (); 1928 void linear_library (), symdef_library (); 1929 1930 /* Search the library ENTRY, already open on descriptor DESC. 1931 This means deciding which library members to load, 1932 making a chain of `struct file_entry' for those members, 1933 and entering their global symbols in the hash table. */ 1934 1935 void 1936 search_library (desc, entry) 1937 int desc; 1938 struct file_entry *entry; 1939 { 1940 int member_length; 1941 register char *name; 1942 register struct file_entry *subentry; 1943 1944 if (!undefined_global_sym_count) return; 1945 1946 /* Examine its first member, which starts SARMAG bytes in. */ 1947 subentry = decode_library_subfile (desc, entry, SARMAG, &member_length); 1948 if (!subentry) return; 1949 1950 name = subentry->filename; 1951 free (subentry); 1952 1953 /* Search via __.SYMDEF if that exists, else linearly. */ 1954 1955 if (!strcmp (name, "__.SYMDEF")) 1956 symdef_library (desc, entry, member_length); 1957 else 1958 linear_library (desc, entry); 1959 } 1960 1961 /* Construct and return a file_entry for a library member. 1962 The library's file_entry is library_entry, and the library is open on DESC. 1963 SUBFILE_OFFSET is the byte index in the library of this member's header. 1964 We store the length of the member into *LENGTH_LOC. */ 1965 1966 struct file_entry * 1967 decode_library_subfile (desc, library_entry, subfile_offset, length_loc) 1968 int desc; 1969 struct file_entry *library_entry; 1970 int subfile_offset; 1971 int *length_loc; 1972 { 1973 int bytes_read; 1974 register int namelen; 1975 int member_length; 1976 register char *name; 1977 struct ar_hdr hdr1; 1978 register struct file_entry *subentry; 1979 1980 lseek (desc, subfile_offset, 0); 1981 1982 bytes_read = read (desc, &hdr1, sizeof hdr1); 1983 if (!bytes_read) 1984 return 0; /* end of archive */ 1985 1986 if (sizeof hdr1 != bytes_read) 1987 fatal_with_file ("malformed library archive ", library_entry); 1988 1989 if (sscanf (hdr1.ar_size, "%d", &member_length) != 1) 1990 fatal_with_file ("malformatted header of archive member in ", library_entry); 1991 1992 subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry)); 1993 bzero (subentry, sizeof (struct file_entry)); 1994 1995 for (namelen = 0; 1996 namelen < sizeof hdr1.ar_name 1997 && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ' 1998 && hdr1.ar_name[namelen] != '/'; 1999 namelen++); 2000 2001 name = (char *) xmalloc (namelen+1); 2002 strncpy (name, hdr1.ar_name, namelen); 2003 name[namelen] = 0; 2004 2005 subentry->filename = name; 2006 subentry->local_sym_name = name; 2007 subentry->symbols = 0; 2008 subentry->strings = 0; 2009 subentry->subfiles = 0; 2010 subentry->starting_offset = subfile_offset + sizeof hdr1; 2011 subentry->superfile = library_entry; 2012 subentry->library_flag = 0; 2013 subentry->header_read_flag = 0; 2014 subentry->just_syms_flag = 0; 2015 subentry->chain = 0; 2016 subentry->total_size = member_length; 2017 2018 (*length_loc) = member_length; 2019 2020 return subentry; 2021 } 2022 2023 int subfile_wanted_p (); 2024 2025 /* Search a library that has a __.SYMDEF member. 2026 DESC is a descriptor on which the library is open. 2027 The file pointer is assumed to point at the __.SYMDEF data. 2028 ENTRY is the library's file_entry. 2029 MEMBER_LENGTH is the length of the __.SYMDEF data. */ 2030 2031 void 2032 symdef_library (desc, entry, member_length) 2033 int desc; 2034 struct file_entry *entry; 2035 int member_length; 2036 { 2037 int *symdef_data = (int *) xmalloc (member_length); 2038 register struct symdef *symdef_base; 2039 char *sym_name_base; 2040 int number_of_symdefs; 2041 int length_of_strings; 2042 int not_finished; 2043 int bytes_read; 2044 register int i; 2045 struct file_entry *prev = 0; 2046 int prev_offset = 0; 2047 2048 bytes_read = read (desc, symdef_data, member_length); 2049 if (bytes_read != member_length) 2050 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2051 2052 number_of_symdefs = *symdef_data / sizeof (struct symdef); 2053 if (number_of_symdefs < 0 || 2054 number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length) 2055 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2056 2057 symdef_base = (struct symdef *) (symdef_data + 1); 2058 length_of_strings = *(int *) (symdef_base + number_of_symdefs); 2059 2060 if (length_of_strings < 0 2061 || number_of_symdefs * sizeof (struct symdef) + length_of_strings 2062 + 2 * sizeof (int) > member_length) 2063 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2064 2065 sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs); 2066 2067 /* Check all the string indexes for validity. */ 2068 2069 for (i = 0; i < number_of_symdefs; i++) 2070 { 2071 register int index = symdef_base[i].symbol_name_string_index; 2072 if (index < 0 || index >= length_of_strings 2073 || (index && *(sym_name_base + index - 1))) 2074 fatal_with_file ("malformatted __.SYMDEF in ", entry); 2075 } 2076 2077 /* Search the symdef data for members to load. 2078 Do this until one whole pass finds nothing to load. */ 2079 2080 not_finished = 1; 2081 while (not_finished) 2082 { 2083 not_finished = 0; 2084 2085 /* Scan all the symbols mentioned in the symdef for ones that we need. 2086 Load the library members that contain such symbols. */ 2087 2088 for (i = 0; 2089 (i < number_of_symdefs 2090 && (undefined_global_sym_count || common_defined_global_count)); 2091 i++) 2092 if (symdef_base[i].symbol_name_string_index >= 0) 2093 { 2094 register symbol *sp; 2095 2096 sp = getsym_soft (sym_name_base 2097 + symdef_base[i].symbol_name_string_index); 2098 2099 /* If we find a symbol that appears to be needed, think carefully 2100 about the archive member that the symbol is in. */ 2101 2102 if (sp && ((sp->referenced && !sp->defined) 2103 || (sp->defined && sp->max_common_size))) 2104 { 2105 int junk; 2106 register int j; 2107 register int offset = symdef_base[i].library_member_offset; 2108 struct file_entry *subentry; 2109 2110 /* Don't think carefully about any archive member 2111 more than once in a given pass. */ 2112 2113 if (prev_offset == offset) 2114 continue; 2115 prev_offset = offset; 2116 2117 /* Read the symbol table of the archive member. */ 2118 2119 subentry = decode_library_subfile (desc, entry, offset, &junk); 2120 if (subentry == 0) 2121 fatal ("invalid offset for %s in symbol table of %s", 2122 sym_name_base 2123 + symdef_base[i].symbol_name_string_index, 2124 entry->filename); 2125 read_entry_symbols (desc, subentry); 2126 subentry->strings = (char *) malloc (subentry->string_size); 2127 read_entry_strings (desc, subentry); 2128 2129 /* Now scan the symbol table and decide whether to load. */ 2130 2131 if (!subfile_wanted_p (subentry)) 2132 { 2133 free (subentry->symbols); 2134 free (subentry); 2135 } 2136 else 2137 { 2138 /* This member is needed; load it. 2139 Since we are loading something on this pass, 2140 we must make another pass through the symdef data. */ 2141 2142 not_finished = 1; 2143 2144 enter_file_symbols (subentry); 2145 2146 if (prev) 2147 prev->chain = subentry; 2148 else entry->subfiles = subentry; 2149 prev = subentry; 2150 2151 /* Clear out this member's symbols from the symdef data 2152 so that following passes won't waste time on them. */ 2153 2154 for (j = 0; j < number_of_symdefs; j++) 2155 { 2156 if (symdef_base[j].library_member_offset == offset) 2157 symdef_base[j].symbol_name_string_index = -1; 2158 } 2159 } 2160 2161 /* We'll read the strings again if we need them again. */ 2162 free (subentry->strings); 2163 subentry->strings = 0; 2164 } 2165 } 2166 } 2167 2168 free (symdef_data); 2169 } 2170 2171 /* Search a library that has no __.SYMDEF. 2172 ENTRY is the library's file_entry. 2173 DESC is the descriptor it is open on. */ 2174 2175 void 2176 linear_library (desc, entry) 2177 int desc; 2178 struct file_entry *entry; 2179 { 2180 register struct file_entry *prev = 0; 2181 register int this_subfile_offset = SARMAG; 2182 2183 while (undefined_global_sym_count || common_defined_global_count) 2184 { 2185 int member_length; 2186 register struct file_entry *subentry; 2187 2188 subentry = decode_library_subfile (desc, entry, this_subfile_offset, 2189 &member_length); 2190 2191 if (!subentry) return; 2192 2193 read_entry_symbols (desc, subentry); 2194 subentry->strings = (char *) alloca (subentry->string_size); 2195 read_entry_strings (desc, subentry); 2196 2197 if (!subfile_wanted_p (subentry)) 2198 { 2199 free (subentry->symbols); 2200 free (subentry); 2201 } 2202 else 2203 { 2204 enter_file_symbols (subentry); 2205 2206 if (prev) 2207 prev->chain = subentry; 2208 else entry->subfiles = subentry; 2209 prev = subentry; 2210 subentry->strings = 0; /* Since space will dissapear on return */ 2211 } 2212 2213 this_subfile_offset += member_length + sizeof (struct ar_hdr); 2214 if (this_subfile_offset & 1) this_subfile_offset++; 2215 } 2216 } 2217 2218 /* ENTRY is an entry for a library member. 2219 Its symbols have been read into core, but not entered. 2220 Return nonzero if we ought to load this member. */ 2221 2222 int 2223 subfile_wanted_p (entry) 2224 struct file_entry *entry; 2225 { 2226 register struct nlist *p; 2227 register struct nlist *end 2228 = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2229 #ifdef DOLLAR_KLUDGE 2230 register int dollar_cond = 0; 2231 #endif 2232 2233 for (p = entry->symbols; p < end; p++) 2234 { 2235 register int type = p->n_type; 2236 register char *name = p->n_un.n_strx + entry->strings; 2237 2238 /* If the symbol has an interesting definition, we could 2239 potentially want it. */ 2240 if (type & N_EXT 2241 && (type != (N_UNDF | N_EXT) || p->n_value 2242 2243 #ifdef DOLLAR_KLUDGE 2244 || name[1] == '$' 2245 #endif 2246 ) 2247 && !SET_ELEMENT_P (type) 2248 && !set_element_prefixed_p (name)) 2249 { 2250 register symbol *sp = getsym_soft (name); 2251 2252 #ifdef DOLLAR_KLUDGE 2253 if (name[1] == '$') 2254 { 2255 sp = getsym_soft (&name[2]); 2256 dollar_cond = 1; 2257 if (!sp) continue; 2258 if (sp->referenced) 2259 { 2260 if (write_map) 2261 { 2262 print_file_name (entry, stdout); 2263 fprintf (stdout, " needed due to $-conditional %s\n", name); 2264 } 2265 return 1; 2266 } 2267 continue; 2268 } 2269 #endif 2270 2271 /* If this symbol has not been hashed, we can't be looking for it. */ 2272 2273 if (!sp) continue; 2274 2275 if ((sp->referenced && !sp->defined) 2276 || (sp->defined && sp->max_common_size)) 2277 { 2278 /* This is a symbol we are looking for. It is either 2279 not yet defined or defined as a common. */ 2280 #ifdef DOLLAR_KLUDGE 2281 if (dollar_cond) continue; 2282 #endif 2283 if (type == (N_UNDF | N_EXT)) 2284 { 2285 /* Symbol being defined as common. 2286 Remember this, but don't load subfile just for this. */ 2287 2288 /* If it didn't used to be common, up the count of 2289 common symbols. */ 2290 if (!sp->max_common_size) 2291 common_defined_global_count++; 2292 2293 if (sp->max_common_size < p->n_value) 2294 sp->max_common_size = p->n_value; 2295 if (!sp->defined) 2296 undefined_global_sym_count--; 2297 sp->defined = 1; 2298 continue; 2299 } 2300 2301 if (write_map) 2302 { 2303 print_file_name (entry, stdout); 2304 fprintf (stdout, " needed due to %s\n", sp->name); 2305 } 2306 return 1; 2307 } 2308 } 2309 } 2310 2311 return 0; 2312 } 2313 2314 void consider_file_section_lengths (), relocate_file_addresses (); 2315 2316 /* Having entered all the global symbols and found the sizes of sections 2317 of all files to be linked, make all appropriate deductions from this data. 2318 2319 We propagate global symbol values from definitions to references. 2320 We compute the layout of the output file and where each input file's 2321 contents fit into it. */ 2322 2323 void 2324 digest_symbols () 2325 { 2326 register int i; 2327 int setv_fill_count; 2328 2329 if (trace_files) 2330 fprintf (stderr, "Digesting symbol information:\n\n"); 2331 2332 /* Compute total size of sections */ 2333 2334 each_file (consider_file_section_lengths, 0); 2335 2336 /* If necessary, pad text section to full page in the file. 2337 Include the padding in the text segment size. */ 2338 2339 if (magic == ZMAGIC) 2340 { 2341 int text_end = text_size + N_TXTOFF (outheader); 2342 text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end; 2343 text_size += text_pad; 2344 } 2345 2346 #ifdef _N_BASEADDR 2347 /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */ 2348 outheader.a_entry = N_PAGSIZ (outheader); 2349 #endif 2350 2351 outheader.a_text = text_size; 2352 #ifdef sequent 2353 outheader.a_text += N_ADDRADJ (outheader); 2354 #endif 2355 2356 /* Make the data segment address start in memory on a suitable boundary. */ 2357 2358 if (! Tdata_flag_specified) 2359 data_start = N_DATADDR (outheader) + text_start - TEXT_START (outheader); 2360 2361 /* Set up the set element vector */ 2362 2363 if (!relocatable_output) 2364 { 2365 /* The set sector size is the number of set elements + a word 2366 for each symbol for the length word at the beginning of the 2367 vector, plus a word for each symbol for a zero at the end of 2368 the vector (for incremental linking). */ 2369 set_sect_size 2370 = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long); 2371 set_sect_start = data_start + data_size; 2372 data_size += set_sect_size; 2373 set_vectors = (unsigned long *) xmalloc (set_sect_size); 2374 setv_fill_count = 0; 2375 } 2376 2377 /* Compute start addresses of each file's sections and symbols. */ 2378 2379 each_full_file (relocate_file_addresses, 0); 2380 2381 /* Now, for each symbol, verify that it is defined globally at most once. 2382 Put the global value into the symbol entry. 2383 Common symbols are allocated here, in the BSS section. 2384 Each defined symbol is given a '->defined' field 2385 which is the correct N_ code for its definition, 2386 except in the case of common symbols with -r. 2387 Then make all the references point at the symbol entry 2388 instead of being chained together. */ 2389 2390 defined_global_sym_count = 0; 2391 2392 for (i = 0; i < TABSIZE; i++) 2393 { 2394 register symbol *sp; 2395 for (sp = symtab[i]; sp; sp = sp->link) 2396 { 2397 /* For each symbol */ 2398 register struct nlist *p, *next; 2399 int defs = 0, com = sp->max_common_size; 2400 struct nlist *first_definition; 2401 for (p = sp->refs; p; p = next) 2402 { 2403 register int type = p->n_type; 2404 2405 if (SET_ELEMENT_P (type)) 2406 { 2407 if (relocatable_output) 2408 fatal ("internal: global ref to set element with -r"); 2409 if (!defs++) 2410 { 2411 sp->value = set_sect_start 2412 + setv_fill_count++ * sizeof (unsigned long); 2413 sp->defined = N_SETV | N_EXT; 2414 first_definition = p; 2415 } 2416 else if ((sp->defined & ~N_EXT) != N_SETV) 2417 { 2418 sp->multiply_defined = 1; 2419 multiple_def_count++; 2420 } 2421 set_vectors[setv_fill_count++] = p->n_value; 2422 } 2423 else if ((type & N_EXT) && type != (N_UNDF | N_EXT)) 2424 { 2425 /* non-common definition */ 2426 if (defs++ && sp->value != p->n_value) 2427 { 2428 sp->multiply_defined = 1; 2429 multiple_def_count++; 2430 } 2431 sp->value = p->n_value; 2432 sp->defined = type; 2433 first_definition = p; 2434 } 2435 next = (struct nlist *) p->n_un.n_name; 2436 p->n_un.n_name = (char *) sp; 2437 } 2438 /* Allocate as common if defined as common and not defined for real */ 2439 if (com && !defs) 2440 { 2441 if (!relocatable_output || force_common_definition) 2442 { 2443 int align = sizeof (int); 2444 2445 /* Round up to nearest sizeof (int). I don't know 2446 whether this is necessary or not (given that 2447 alignment is taken care of later), but it's 2448 traditional, so I'll leave it in. Note that if 2449 this size alignment is ever removed, ALIGN above 2450 will have to be initialized to 1 instead of 2451 sizeof (int). */ 2452 2453 com = (com + sizeof (int) - 1) & (- sizeof (int)); 2454 2455 while (!(com & align)) 2456 align <<= 1; 2457 2458 align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align; 2459 2460 bss_size = ((((bss_size + data_size + data_start) 2461 + (align - 1)) & (- align)) 2462 - data_size - data_start); 2463 2464 sp->value = data_start + data_size + bss_size; 2465 sp->defined = N_BSS | N_EXT; 2466 bss_size += com; 2467 if (write_map) 2468 printf ("Allocating common %s: %x at %x\n", 2469 sp->name, com, sp->value); 2470 } 2471 else 2472 { 2473 sp->defined = 0; 2474 undefined_global_sym_count++; 2475 } 2476 } 2477 /* Set length word at front of vector and zero byte at end. 2478 Reverse the vector itself to put it in file order. */ 2479 if ((sp->defined & ~N_EXT) == N_SETV) 2480 { 2481 unsigned long length_word_index 2482 = (sp->value - set_sect_start) / sizeof (unsigned long); 2483 unsigned long i, tmp; 2484 2485 set_vectors[length_word_index] 2486 = setv_fill_count - 1 - length_word_index; 2487 2488 /* Reverse the vector. */ 2489 for (i = 1; 2490 i < (setv_fill_count - length_word_index - 1) / 2 + 1; 2491 i++) 2492 { 2493 tmp = set_vectors[length_word_index + i]; 2494 set_vectors[length_word_index + i] 2495 = set_vectors[setv_fill_count - i]; 2496 set_vectors[setv_fill_count - i] = tmp; 2497 } 2498 2499 set_vectors[setv_fill_count++] = 0; 2500 } 2501 if (sp->defined) 2502 defined_global_sym_count++; 2503 } 2504 } 2505 2506 if (end_symbol) /* These are null if -r. */ 2507 { 2508 etext_symbol->value = text_size + text_start; 2509 edata_symbol->value = data_start + data_size; 2510 end_symbol->value = data_start + data_size + bss_size; 2511 } 2512 2513 /* Figure the data_pad now, so that it overlaps with the bss addresses. */ 2514 2515 if (specified_data_size && specified_data_size > data_size) 2516 data_pad = specified_data_size - data_size; 2517 2518 if (magic == ZMAGIC) 2519 data_pad = ((data_pad + data_size + page_size - 1) & (- page_size)) 2520 - data_size; 2521 2522 bss_size -= data_pad; 2523 if (bss_size < 0) bss_size = 0; 2524 2525 data_size += data_pad; 2526 } 2527 2528 /* Accumulate the section sizes of input file ENTRY 2529 into the section sizes of the output file. */ 2530 2531 void 2532 consider_file_section_lengths (entry) 2533 register struct file_entry *entry; 2534 { 2535 if (entry->just_syms_flag) 2536 return; 2537 2538 entry->text_start_address = text_size; 2539 /* If there were any vectors, we need to chop them off */ 2540 text_size += entry->header.a_text; 2541 entry->data_start_address = data_size; 2542 data_size += entry->header.a_data; 2543 entry->bss_start_address = bss_size; 2544 bss_size += entry->header.a_bss; 2545 2546 text_reloc_size += entry->header.a_trsize; 2547 data_reloc_size += entry->header.a_drsize; 2548 } 2549 2550 /* Determine where the sections of ENTRY go into the output file, 2551 whose total section sizes are already known. 2552 Also relocate the addresses of the file's local and debugger symbols. */ 2553 2554 void 2555 relocate_file_addresses (entry) 2556 register struct file_entry *entry; 2557 { 2558 entry->text_start_address += text_start; 2559 /* Note that `data_start' and `data_size' have not yet been 2560 adjusted for `data_pad'. If they had been, we would get the wrong 2561 results here. */ 2562 entry->data_start_address += data_start; 2563 entry->bss_start_address += data_start + data_size; 2564 2565 { 2566 register struct nlist *p; 2567 register struct nlist *end 2568 = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2569 2570 for (p = entry->symbols; p < end; p++) 2571 { 2572 /* If this belongs to a section, update it by the section's start address */ 2573 register int type = p->n_type & N_TYPE; 2574 2575 switch (type) 2576 { 2577 case N_TEXT: 2578 case N_SETT: 2579 p->n_value += entry->text_start_address; 2580 break; 2581 case N_DATA: 2582 case N_SETV: 2583 case N_SETD: 2584 /* A symbol whose value is in the data section 2585 is present in the input file as if the data section 2586 started at an address equal to the length of the file's text. */ 2587 p->n_value += entry->data_start_address - entry->header.a_text; 2588 break; 2589 case N_BSS: 2590 case N_SETB: 2591 /* likewise for symbols with value in BSS. */ 2592 p->n_value += entry->bss_start_address 2593 - entry->header.a_text - entry->header.a_data; 2594 break; 2595 } 2596 } 2597 } 2598 } 2599 2600 void describe_file_sections (), list_file_locals (); 2601 2602 /* Print a complete or partial map of the output file. */ 2603 2604 void 2605 print_symbols (outfile) 2606 FILE *outfile; 2607 { 2608 register int i; 2609 2610 fprintf (outfile, "\nFiles:\n\n"); 2611 2612 each_file (describe_file_sections, outfile); 2613 2614 fprintf (outfile, "\nGlobal symbols:\n\n"); 2615 2616 for (i = 0; i < TABSIZE; i++) 2617 { 2618 register symbol *sp; 2619 for (sp = symtab[i]; sp; sp = sp->link) 2620 { 2621 if (sp->defined == 1) 2622 fprintf (outfile, " %s: common, length 0x%x\n", sp->name, sp->max_common_size); 2623 if (sp->defined) 2624 fprintf (outfile, " %s: 0x%x\n", sp->name, sp->value); 2625 else if (sp->referenced) 2626 fprintf (outfile, " %s: undefined\n", sp->name); 2627 } 2628 } 2629 2630 each_file (list_file_locals, outfile); 2631 } 2632 2633 void 2634 describe_file_sections (entry, outfile) 2635 struct file_entry *entry; 2636 FILE *outfile; 2637 { 2638 fprintf (outfile, " "); 2639 print_file_name (entry, outfile); 2640 if (entry->just_syms_flag) 2641 fprintf (outfile, " symbols only\n", 0); 2642 else 2643 fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n", 2644 entry->text_start_address, entry->header.a_text, 2645 entry->data_start_address, entry->header.a_data, 2646 entry->bss_start_address, entry->header.a_bss); 2647 } 2648 2649 void 2650 list_file_locals (entry, outfile) 2651 struct file_entry *entry; 2652 FILE *outfile; 2653 { 2654 register struct nlist 2655 *p, 2656 *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist); 2657 2658 entry->strings = (char *) alloca (entry->string_size); 2659 read_entry_strings (file_open (entry), entry); 2660 2661 fprintf (outfile, "\nLocal symbols of "); 2662 print_file_name (entry, outfile); 2663 fprintf (outfile, ":\n\n"); 2664 2665 for (p = entry->symbols; p < end; p++) 2666 /* If this is a definition, 2667 update it if necessary by this file's start address. */ 2668 if (!(p->n_type & (N_STAB | N_EXT))) 2669 fprintf (outfile, " %s: 0x%x\n", 2670 entry->strings + p->n_un.n_strx, p->n_value); 2671 2672 entry->strings = 0; /* All done with them. */ 2673 } 2674 2675 2676 /* Static vars for do_warnings and subroutines of it */ 2677 int list_unresolved_refs; /* List unresolved refs */ 2678 int list_warning_symbols; /* List warning syms */ 2679 int list_multiple_defs; /* List multiple definitions */ 2680 2681 /* 2682 * Structure for communication between do_file_warnings and it's 2683 * helper routines. Will in practice be an array of three of these: 2684 * 0) Current line, 1) Next line, 2) Source file info. 2685 */ 2686 struct line_debug_entry 2687 { 2688 int line; 2689 char *filename; 2690 struct nlist *sym; 2691 }; 2692 2693 void qsort (); 2694 /* 2695 * Helper routines for do_file_warnings. 2696 */ 2697 2698 /* Return an integer less than, equal to, or greater than 0 as per the 2699 relation between the two relocation entries. Used by qsort. */ 2700 2701 int 2702 relocation_entries_relation (rel1, rel2) 2703 struct relocation_info *rel1, *rel2; 2704 { 2705 return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2); 2706 } 2707 2708 /* Moves to the next debugging symbol in the file. USE_DATA_SYMBOLS 2709 determines the type of the debugging symbol to look for (DSLINE or 2710 SLINE). STATE_POINTER keeps track of the old and new locatiosn in 2711 the file. It assumes that state_pointer[1] is valid; ie 2712 that it.sym points into some entry in the symbol table. If 2713 state_pointer[1].sym == 0, this routine should not be called. */ 2714 2715 int 2716 next_debug_entry (use_data_symbols, state_pointer) 2717 register int use_data_symbols; 2718 /* Next must be passed by reference! */ 2719 struct line_debug_entry state_pointer[3]; 2720 { 2721 register struct line_debug_entry 2722 *current = state_pointer, 2723 *next = state_pointer + 1, 2724 /* Used to store source file */ 2725 *source = state_pointer + 2; 2726 struct file_entry *entry = (struct file_entry *) source->sym; 2727 2728 current->sym = next->sym; 2729 current->line = next->line; 2730 current->filename = next->filename; 2731 2732 while (++(next->sym) < (entry->symbols 2733 + entry->header.a_syms/sizeof (struct nlist))) 2734 { 2735 /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so 2736 * may look negative...therefore, must mask to low bits 2737 */ 2738 switch (next->sym->n_type & 0xff) 2739 { 2740 case N_SLINE: 2741 if (use_data_symbols) continue; 2742 next->line = next->sym->n_desc; 2743 return 1; 2744 case N_DSLINE: 2745 if (!use_data_symbols) continue; 2746 next->line = next->sym->n_desc; 2747 return 1; 2748 #ifdef HAVE_SUN_STABS 2749 case N_EINCL: 2750 next->filename = source->filename; 2751 continue; 2752 #endif 2753 case N_SO: 2754 source->filename = next->sym->n_un.n_strx + entry->strings; 2755 source->line++; 2756 #ifdef HAVE_SUN_STABS 2757 case N_BINCL: 2758 #endif 2759 case N_SOL: 2760 next->filename 2761 = next->sym->n_un.n_strx + entry->strings; 2762 default: 2763 continue; 2764 } 2765 } 2766 next->sym = (struct nlist *) 0; 2767 return 0; 2768 } 2769 2770 /* Create a structure to save the state of a scan through the debug 2771 symbols. USE_DATA_SYMBOLS is set if we should be scanning for 2772 DSLINE's instead of SLINE's. entry is the file entry which points 2773 at the symbols to use. */ 2774 2775 struct line_debug_entry * 2776 init_debug_scan (use_data_symbols, entry) 2777 int use_data_symbols; 2778 struct file_entry *entry; 2779 { 2780 struct line_debug_entry 2781 *state_pointer 2782 = (struct line_debug_entry *) 2783 xmalloc (3 * sizeof (struct line_debug_entry)); 2784 register struct line_debug_entry 2785 *current = state_pointer, 2786 *next = state_pointer + 1, 2787 *source = state_pointer + 2; /* Used to store source file */ 2788 2789 struct nlist *tmp; 2790 2791 for (tmp = entry->symbols; 2792 tmp < (entry->symbols 2793 + entry->header.a_syms/sizeof (struct nlist)); 2794 tmp++) 2795 if (tmp->n_type == (int) N_SO) 2796 break; 2797 2798 if (tmp >= (entry->symbols 2799 + entry->header.a_syms/sizeof (struct nlist))) 2800 { 2801 /* I believe this translates to "We lose" */ 2802 current->filename = next->filename = entry->filename; 2803 current->line = next->line = -1; 2804 current->sym = next->sym = (struct nlist *) 0; 2805 return state_pointer; 2806 } 2807 2808 next->line = source->line = 0; 2809 next->filename = source->filename 2810 = (tmp->n_un.n_strx + entry->strings); 2811 source->sym = (struct nlist *) entry; 2812 next->sym = tmp; 2813 2814 next_debug_entry (use_data_symbols, state_pointer); /* To setup next */ 2815 2816 if (!next->sym) /* No line numbers for this section; */ 2817 /* setup output results as appropriate */ 2818 { 2819 if (source->line) 2820 { 2821 current->filename = source->filename = entry->filename; 2822 current->line = -1; /* Don't print lineno */ 2823 } 2824 else 2825 { 2826 current->filename = source->filename; 2827 current->line = 0; 2828 } 2829 return state_pointer; 2830 } 2831 2832 2833 next_debug_entry (use_data_symbols, state_pointer); /* To setup current */ 2834 2835 return state_pointer; 2836 } 2837 2838 /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER 2839 which describes the current location in the implied scan through 2840 the debug symbols within the file which ADDRESS is within, and 2841 returns the source line number which corresponds to ADDRESS. */ 2842 2843 int 2844 address_to_line (address, state_pointer) 2845 unsigned long address; 2846 /* Next must be passed by reference! */ 2847 struct line_debug_entry state_pointer[3]; 2848 { 2849 struct line_debug_entry 2850 *current = state_pointer, 2851 *next = state_pointer + 1; 2852 struct line_debug_entry *tmp_pointer; 2853 2854 int use_data_symbols; 2855 2856 if (next->sym) 2857 use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA; 2858 else 2859 return current->line; 2860 2861 /* Go back to the beginning if we've already passed it. */ 2862 if (current->sym->n_value > address) 2863 { 2864 tmp_pointer = init_debug_scan (use_data_symbols, 2865 (struct file_entry *) 2866 ((state_pointer + 2)->sym)); 2867 state_pointer[0] = tmp_pointer[0]; 2868 state_pointer[1] = tmp_pointer[1]; 2869 state_pointer[2] = tmp_pointer[2]; 2870 free (tmp_pointer); 2871 } 2872 2873 /* If we're still in a bad way, return -1, meaning invalid line. */ 2874 if (current->sym->n_value > address) 2875 return -1; 2876 2877 while (next->sym 2878 && next->sym->n_value <= address 2879 && next_debug_entry (use_data_symbols, state_pointer)) 2880 ; 2881 return current->line; 2882 } 2883 2884 2885 /* Macros for manipulating bitvectors. */ 2886 #define BIT_SET_P(bv, index) ((bv)[(index) >> 3] & 1 << ((index) & 0x7)) 2887 #define SET_BIT(bv, index) ((bv)[(index) >> 3] |= 1 << ((index) & 0x7)) 2888 2889 /* This routine will scan through the relocation data of file ENTRY, 2890 printing out references to undefined symbols and references to 2891 symbols defined in files with N_WARNING symbols. If DATA_SEGMENT 2892 is non-zero, it will scan the data relocation segment (and use 2893 N_DSLINE symbols to track line number); otherwise it will scan the 2894 text relocation segment. Warnings will be printed on the output 2895 stream OUTFILE. Eventually, every nlist symbol mapped through will 2896 be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when 2897 we scan the nlists themselves. */ 2898 2899 do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector) 2900 struct file_entry *entry; 2901 int data_segment; 2902 FILE *outfile; 2903 unsigned char *nlist_bitvector; 2904 { 2905 struct relocation_info 2906 *reloc_start = data_segment ? entry->datarel : entry->textrel, 2907 *reloc; 2908 int reloc_size 2909 = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize) 2910 / sizeof (struct relocation_info)); 2911 int start_of_segment 2912 = (data_segment ? entry->data_start_address : entry->text_start_address); 2913 struct nlist *start_of_syms = entry->symbols; 2914 struct line_debug_entry *state_pointer 2915 = init_debug_scan (data_segment != 0, entry); 2916 register struct line_debug_entry 2917 *current = state_pointer; 2918 /* Assigned to generally static values; should not be written into. */ 2919 char *errfmt; 2920 /* Assigned to alloca'd values cand copied into; should be freed 2921 when done. */ 2922 char *errmsg; 2923 int invalidate_line_number; 2924 2925 /* We need to sort the relocation info here. Sheesh, so much effort 2926 for one lousy error optimization. */ 2927 2928 qsort (reloc_start, reloc_size, sizeof (struct relocation_info), 2929 relocation_entries_relation); 2930 2931 for (reloc = reloc_start; 2932 reloc < (reloc_start + reloc_size); 2933 reloc++) 2934 { 2935 register struct nlist *s; 2936 register symbol *g; 2937 2938 /* If the relocation isn't resolved through a symbol, continue */ 2939 if (!RELOC_EXTERN_P(reloc)) 2940 continue; 2941 2942 s = &(entry->symbols[RELOC_SYMBOL(reloc)]); 2943 2944 /* Local symbols shouldn't ever be used by relocation info, so 2945 the next should be safe. 2946 This is, of course, wrong. References to local BSS symbols can be 2947 the targets of relocation info, and they can (must) be 2948 resolved through symbols. However, these must be defined properly, 2949 (the assembler would have caught it otherwise), so we can 2950 ignore these cases. */ 2951 if (!(s->n_type & N_EXT)) 2952 continue; 2953 2954 g = (symbol *) s->n_un.n_name; 2955 errmsg = 0; 2956 2957 if (!g->defined && list_unresolved_refs) /* Reference */ 2958 { 2959 /* Mark as being noted by relocation warning pass. */ 2960 SET_BIT (nlist_bitvector, s - start_of_syms); 2961 2962 if (g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */ 2963 continue; 2964 2965 /* Undefined symbol which we should mention */ 2966 2967 if (++(g->undef_refs) == MAX_UREFS_PRINTED) 2968 { 2969 errfmt = "More undefined symbol %s refs follow"; 2970 invalidate_line_number = 1; 2971 } 2972 else 2973 { 2974 errfmt = "Undefined symbol %s referenced from %s segment"; 2975 invalidate_line_number = 0; 2976 } 2977 } 2978 else /* Defined */ 2979 { 2980 /* Potential symbol warning here */ 2981 if (!g->warning) continue; 2982 2983 /* Mark as being noted by relocation warning pass. */ 2984 SET_BIT (nlist_bitvector, s - start_of_syms); 2985 2986 errfmt = 0; 2987 errmsg = g->warning; 2988 invalidate_line_number = 0; 2989 } 2990 2991 2992 /* If errfmt == 0, errmsg has already been defined. */ 2993 if (errfmt != 0) 2994 { 2995 char *nm; 2996 2997 if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL) 2998 nm = g->name; 2999 errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1); 3000 sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text"); 3001 if (nm != g->name) 3002 free (nm); 3003 } 3004 3005 address_to_line (RELOC_ADDRESS (reloc) + start_of_segment, 3006 state_pointer); 3007 3008 if (current->line >=0) 3009 fprintf (outfile, "%s:%d: %s\n", current->filename, 3010 invalidate_line_number ? 0 : current->line, errmsg); 3011 else 3012 fprintf (outfile, "%s: %s\n", current->filename, errmsg); 3013 3014 if (errfmt != 0) 3015 free (errmsg); 3016 } 3017 3018 free (state_pointer); 3019 } 3020 3021 /* Print on OUTFILE a list of all warnings generated by references 3022 and/or definitions in the file ENTRY. List source file and line 3023 number if possible, just the .o file if not. */ 3024 3025 void 3026 do_file_warnings (entry, outfile) 3027 struct file_entry *entry; 3028 FILE *outfile; 3029 { 3030 int number_of_syms = entry->header.a_syms / sizeof (struct nlist); 3031 unsigned char *nlist_bitvector 3032 = (unsigned char *) alloca ((number_of_syms >> 3) + 1); 3033 struct line_debug_entry *text_scan, *data_scan; 3034 int i; 3035 char *errfmt, *file_name; 3036 int line_number; 3037 int dont_allow_symbol_name; 3038 3039 bzero (nlist_bitvector, (number_of_syms >> 3) + 1); 3040 3041 /* Read in the files strings if they aren't available */ 3042 if (!entry->strings) 3043 { 3044 int desc; 3045 3046 entry->strings = (char *) alloca (entry->string_size); 3047 desc = file_open (entry); 3048 read_entry_strings (desc, entry); 3049 } 3050 3051 read_file_relocation (entry); 3052 3053 /* Do text warnings based on a scan through the relocation info. */ 3054 do_relocation_warnings (entry, 0, outfile, nlist_bitvector); 3055 3056 /* Do data warnings based on a scan through the relocation info. */ 3057 do_relocation_warnings (entry, 1, outfile, nlist_bitvector); 3058 3059 /* Scan through all of the nlist entries in this file and pick up 3060 anything that the scan through the relocation stuff didn't. */ 3061 3062 text_scan = init_debug_scan (0, entry); 3063 data_scan = init_debug_scan (1, entry); 3064 3065 for (i = 0; i < number_of_syms; i++) 3066 { 3067 struct nlist *s; 3068 struct glosym *g; 3069 3070 s = entry->symbols + i; 3071 3072 if (!(s->n_type & N_EXT)) 3073 continue; 3074 3075 g = (symbol *) s->n_un.n_name; 3076 dont_allow_symbol_name = 0; 3077 3078 if (list_multiple_defs && g->multiply_defined) 3079 { 3080 errfmt = "Definition of symbol %s (multiply defined)"; 3081 switch (s->n_type) 3082 { 3083 case N_TEXT | N_EXT: 3084 line_number = address_to_line (s->n_value, text_scan); 3085 file_name = text_scan[0].filename; 3086 break; 3087 case N_DATA | N_EXT: 3088 line_number = address_to_line (s->n_value, data_scan); 3089 file_name = data_scan[0].filename; 3090 break; 3091 case N_SETA | N_EXT: 3092 case N_SETT | N_EXT: 3093 case N_SETD | N_EXT: 3094 case N_SETB | N_EXT: 3095 if (g->multiply_defined == 2) 3096 continue; 3097 errfmt = "First set element definition of symbol %s (multiply defined)"; 3098 break; 3099 default: 3100 continue; /* Don't print out multiple defs 3101 at references. */ 3102 } 3103 } 3104 else if (BIT_SET_P (nlist_bitvector, i)) 3105 continue; 3106 else if (list_unresolved_refs && !g->defined) 3107 { 3108 if (g->undef_refs >= MAX_UREFS_PRINTED) 3109 continue; 3110 3111 if (++(g->undef_refs) == MAX_UREFS_PRINTED) 3112 errfmt = "More undefined \"%s\" refs follow"; 3113 else 3114 errfmt = "Undefined symbol \"%s\" referenced"; 3115 line_number = -1; 3116 } 3117 else if (g->warning) 3118 { 3119 /* There are two cases in which we don't want to 3120 do this. The first is if this is a definition instead of 3121 a reference. The second is if it's the reference used by 3122 the warning stabs itself. */ 3123 if (s->n_type != (N_EXT | N_UNDF) 3124 || (i && (s-1)->n_type == N_WARNING)) 3125 continue; 3126 3127 errfmt = g->warning; 3128 line_number = -1; 3129 dont_allow_symbol_name = 1; 3130 } 3131 else 3132 continue; 3133 3134 if (line_number == -1) 3135 fprintf (outfile, "%s: ", entry->filename); 3136 else 3137 fprintf (outfile, "%s:%d: ", file_name, line_number); 3138 3139 if (dont_allow_symbol_name) 3140 fprintf (outfile, "%s", errfmt); 3141 else 3142 { 3143 char *nm; 3144 if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL) 3145 { 3146 fprintf (outfile, errfmt, nm); 3147 free (nm); 3148 } 3149 else 3150 fprintf (outfile, errfmt, g->name); 3151 } 3152 3153 fputc ('\n', outfile); 3154 } 3155 free (text_scan); 3156 free (data_scan); 3157 entry->strings = 0; /* Since it will dissapear anyway. */ 3158 } 3159 3160 do_warnings (outfile) 3161 FILE *outfile; 3162 { 3163 list_unresolved_refs = !relocatable_output && undefined_global_sym_count; 3164 list_warning_symbols = warning_count; 3165 list_multiple_defs = multiple_def_count != 0; 3166 3167 if (!(list_unresolved_refs || 3168 list_warning_symbols || 3169 list_multiple_defs )) 3170 /* No need to run this routine */ 3171 return; 3172 3173 each_file (do_file_warnings, outfile); 3174 3175 if (list_unresolved_refs || list_multiple_defs) 3176 make_executable = 0; 3177 } 3178 3179 /* Write the output file */ 3180 3181 void 3182 write_output () 3183 { 3184 struct stat statbuf; 3185 int filemode; 3186 3187 (void) unlink (output_filename); 3188 outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 3189 if (outdesc < 0) perror_name (output_filename); 3190 3191 if (fstat (outdesc, &statbuf) < 0) 3192 perror_name (output_filename); 3193 3194 filemode = statbuf.st_mode; 3195 3196 chmod (output_filename, filemode & ~0111); 3197 3198 /* Output the a.out header. */ 3199 write_header (); 3200 3201 /* Output the text and data segments, relocating as we go. */ 3202 write_text (); 3203 write_data (); 3204 3205 /* Output the merged relocation info, if requested with `-r'. */ 3206 if (relocatable_output) 3207 write_rel (); 3208 3209 /* Output the symbol table (both globals and locals). */ 3210 write_syms (); 3211 3212 /* Copy any GDB symbol segments from input files. */ 3213 write_symsegs (); 3214 3215 close (outdesc); 3216 3217 if (chmod (output_filename, filemode | 0111) == -1) 3218 perror_name (output_filename); 3219 } 3220 3221 void modify_location (), perform_relocation (), copy_text (), copy_data (); 3222 3223 void 3224 write_header () 3225 { 3226 N_SET_MAGIC (outheader, magic); 3227 outheader.a_text = text_size; 3228 #ifdef sequent 3229 outheader.a_text += N_ADDRADJ (outheader); 3230 if (entry_symbol == 0) 3231 entry_symbol = getsym("start"); 3232 #endif 3233 outheader.a_data = data_size; 3234 outheader.a_bss = bss_size; 3235 outheader.a_entry = (entry_symbol ? entry_symbol->value 3236 : text_start + entry_offset); 3237 #ifdef COFF_ENCAPSULATE 3238 if (need_coff_header) 3239 { 3240 /* We are encapsulating BSD format within COFF format. */ 3241 struct coffscn *tp, *dp, *bp; 3242 3243 tp = &coffheader.scns[0]; 3244 dp = &coffheader.scns[1]; 3245 bp = &coffheader.scns[2]; 3246 3247 strcpy (tp->s_name, ".text"); 3248 tp->s_paddr = text_start; 3249 tp->s_vaddr = text_start; 3250 tp->s_size = text_size; 3251 tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec); 3252 tp->s_relptr = 0; 3253 tp->s_lnnoptr = 0; 3254 tp->s_nreloc = 0; 3255 tp->s_nlnno = 0; 3256 tp->s_flags = 0x20; 3257 strcpy (dp->s_name, ".data"); 3258 dp->s_paddr = data_start; 3259 dp->s_vaddr = data_start; 3260 dp->s_size = data_size; 3261 dp->s_scnptr = tp->s_scnptr + tp->s_size; 3262 dp->s_relptr = 0; 3263 dp->s_lnnoptr = 0; 3264 dp->s_nreloc = 0; 3265 dp->s_nlnno = 0; 3266 dp->s_flags = 0x40; 3267 strcpy (bp->s_name, ".bss"); 3268 bp->s_paddr = dp->s_vaddr + dp->s_size; 3269 bp->s_vaddr = bp->s_paddr; 3270 bp->s_size = bss_size; 3271 bp->s_scnptr = 0; 3272 bp->s_relptr = 0; 3273 bp->s_lnnoptr = 0; 3274 bp->s_nreloc = 0; 3275 bp->s_nlnno = 0; 3276 bp->s_flags = 0x80; 3277 3278 coffheader.f_magic = COFF_MAGIC; 3279 coffheader.f_nscns = 3; 3280 /* store an unlikely time so programs can 3281 * tell that there is a bsd header 3282 */ 3283 coffheader.f_timdat = 1; 3284 coffheader.f_symptr = 0; 3285 coffheader.f_nsyms = 0; 3286 coffheader.f_opthdr = 28; 3287 coffheader.f_flags = 0x103; 3288 /* aouthdr */ 3289 coffheader.magic = ZMAGIC; 3290 coffheader.vstamp = 0; 3291 coffheader.tsize = tp->s_size; 3292 coffheader.dsize = dp->s_size; 3293 coffheader.bsize = bp->s_size; 3294 coffheader.entry = outheader.a_entry; 3295 coffheader.text_start = tp->s_vaddr; 3296 coffheader.data_start = dp->s_vaddr; 3297 } 3298 #endif 3299 3300 #ifdef INITIALIZE_HEADER 3301 INITIALIZE_HEADER; 3302 #endif 3303 3304 if (strip_symbols == STRIP_ALL) 3305 nsyms = 0; 3306 else 3307 { 3308 nsyms = (defined_global_sym_count 3309 + undefined_global_sym_count); 3310 if (discard_locals == DISCARD_L) 3311 nsyms += non_L_local_sym_count; 3312 else if (discard_locals == DISCARD_NONE) 3313 nsyms += local_sym_count; 3314 /* One extra for following reference on indirects */ 3315 if (relocatable_output) 3316 nsyms += set_symbol_count + global_indirect_count; 3317 } 3318 3319 if (strip_symbols == STRIP_NONE) 3320 nsyms += debugger_sym_count; 3321 3322 outheader.a_syms = nsyms * sizeof (struct nlist); 3323 3324 if (relocatable_output) 3325 { 3326 outheader.a_trsize = text_reloc_size; 3327 outheader.a_drsize = data_reloc_size; 3328 } 3329 else 3330 { 3331 outheader.a_trsize = 0; 3332 outheader.a_drsize = 0; 3333 } 3334 3335 #ifdef COFF_ENCAPSULATE 3336 if (need_coff_header) 3337 mywrite (&coffheader, sizeof coffheader, 1, outdesc); 3338 #endif 3339 mywrite (&outheader, sizeof (struct exec), 1, outdesc); 3340 3341 /* Output whatever padding is required in the executable file 3342 between the header and the start of the text. */ 3343 3344 #ifndef COFF_ENCAPSULATE 3345 padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc); 3346 #endif 3347 } 3348 3349 /* Relocate the text segment of each input file 3350 and write to the output file. */ 3351 3352 void 3353 write_text () 3354 { 3355 if (trace_files) 3356 fprintf (stderr, "Copying and relocating text:\n\n"); 3357 3358 each_full_file (copy_text, 0); 3359 file_close (); 3360 3361 if (trace_files) 3362 fprintf (stderr, "\n"); 3363 3364 padfile (text_pad, outdesc); 3365 } 3366 3367 int 3368 text_offset (entry) 3369 struct file_entry *entry; 3370 { 3371 return entry->starting_offset + N_TXTOFF (entry->header); 3372 } 3373 3374 /* Read in all of the relocation information */ 3375 3376 void 3377 read_relocation () 3378 { 3379 each_full_file (read_file_relocation, 0); 3380 } 3381 3382 /* Read in the relocation sections of ENTRY if necessary */ 3383 3384 void 3385 read_file_relocation (entry) 3386 struct file_entry *entry; 3387 { 3388 register struct relocation_info *reloc; 3389 int desc; 3390 int read_return; 3391 3392 desc = -1; 3393 if (!entry->textrel) 3394 { 3395 reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize); 3396 desc = file_open (entry); 3397 lseek (desc, 3398 text_offset (entry) + entry->header.a_text + entry->header.a_data, 3399 L_SET); 3400 if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize))) 3401 { 3402 fprintf (stderr, "Return from read: %d\n", read_return); 3403 fatal_with_file ("premature eof in text relocation of ", entry); 3404 } 3405 entry->textrel = reloc; 3406 } 3407 3408 if (!entry->datarel) 3409 { 3410 reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize); 3411 if (desc == -1) desc = file_open (entry); 3412 lseek (desc, 3413 text_offset (entry) + entry->header.a_text 3414 + entry->header.a_data + entry->header.a_trsize, 3415 L_SET); 3416 if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize)) 3417 fatal_with_file ("premature eof in data relocation of ", entry); 3418 entry->datarel = reloc; 3419 } 3420 } 3421 3422 /* Read the text segment contents of ENTRY, relocate them, 3423 and write the result to the output file. 3424 If `-r', save the text relocation for later reuse. */ 3425 3426 void 3427 copy_text (entry) 3428 struct file_entry *entry; 3429 { 3430 register char *bytes; 3431 register int desc; 3432 register struct relocation_info *reloc; 3433 3434 if (trace_files) 3435 prline_file_name (entry, stderr); 3436 3437 desc = file_open (entry); 3438 3439 /* Allocate space for the file's text section */ 3440 3441 bytes = (char *) alloca (entry->header.a_text); 3442 3443 /* Deal with relocation information however is appropriate */ 3444 3445 if (entry->textrel) reloc = entry->textrel; 3446 else if (relocatable_output) 3447 { 3448 read_file_relocation (entry); 3449 reloc = entry->textrel; 3450 } 3451 else 3452 { 3453 reloc = (struct relocation_info *) alloca (entry->header.a_trsize); 3454 lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0); 3455 if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize)) 3456 fatal_with_file ("premature eof in text relocation of ", entry); 3457 } 3458 3459 /* Read the text section into core. */ 3460 3461 lseek (desc, text_offset (entry), 0); 3462 if (entry->header.a_text != read (desc, bytes, entry->header.a_text)) 3463 fatal_with_file ("premature eof in text section of ", entry); 3464 3465 3466 /* Relocate the text according to the text relocation. */ 3467 3468 perform_relocation (bytes, entry->text_start_address, entry->header.a_text, 3469 reloc, entry->header.a_trsize, entry); 3470 3471 /* Write the relocated text to the output file. */ 3472 3473 mywrite (bytes, 1, entry->header.a_text, outdesc); 3474 } 3475 3476 /* Relocate the data segment of each input file 3477 and write to the output file. */ 3478 3479 void 3480 write_data () 3481 { 3482 if (trace_files) 3483 fprintf (stderr, "Copying and relocating data:\n\n"); 3484 3485 each_full_file (copy_data, 0); 3486 file_close (); 3487 3488 /* Write out the set element vectors. See digest symbols for 3489 description of length of the set vector section. */ 3490 3491 if (set_vector_count) 3492 mywrite (set_vectors, 2 * set_symbol_count + set_vector_count, 3493 sizeof (unsigned long), outdesc); 3494 3495 if (trace_files) 3496 fprintf (stderr, "\n"); 3497 3498 padfile (data_pad, outdesc); 3499 } 3500 3501 /* Read the data segment contents of ENTRY, relocate them, 3502 and write the result to the output file. 3503 If `-r', save the data relocation for later reuse. 3504 See comments in `copy_text'. */ 3505 3506 void 3507 copy_data (entry) 3508 struct file_entry *entry; 3509 { 3510 register struct relocation_info *reloc; 3511 register char *bytes; 3512 register int desc; 3513 3514 if (trace_files) 3515 prline_file_name (entry, stderr); 3516 3517 desc = file_open (entry); 3518 3519 bytes = (char *) alloca (entry->header.a_data); 3520 3521 if (entry->datarel) reloc = entry->datarel; 3522 else if (relocatable_output) /* Will need this again */ 3523 { 3524 read_file_relocation (entry); 3525 reloc = entry->datarel; 3526 } 3527 else 3528 { 3529 reloc = (struct relocation_info *) alloca (entry->header.a_drsize); 3530 lseek (desc, text_offset (entry) + entry->header.a_text 3531 + entry->header.a_data + entry->header.a_trsize, 3532 0); 3533 if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize)) 3534 fatal_with_file ("premature eof in data relocation of ", entry); 3535 } 3536 3537 lseek (desc, text_offset (entry) + entry->header.a_text, 0); 3538 if (entry->header.a_data != read (desc, bytes, entry->header.a_data)) 3539 fatal_with_file ("premature eof in data section of ", entry); 3540 3541 perform_relocation (bytes, entry->data_start_address - entry->header.a_text, 3542 entry->header.a_data, reloc, entry->header.a_drsize, entry); 3543 3544 mywrite (bytes, 1, entry->header.a_data, outdesc); 3545 } 3546 3547 /* Relocate ENTRY's text or data section contents. 3548 DATA is the address of the contents, in core. 3549 DATA_SIZE is the length of the contents. 3550 PC_RELOCATION is the difference between the address of the contents 3551 in the output file and its address in the input file. 3552 RELOC_INFO is the address of the relocation info, in core. 3553 RELOC_SIZE is its length in bytes. */ 3554 /* This version is about to be severly hacked by Randy. Hope it 3555 works afterwards. */ 3556 void 3557 perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry) 3558 char *data; 3559 struct relocation_info *reloc_info; 3560 struct file_entry *entry; 3561 int pc_relocation; 3562 int data_size; 3563 int reloc_size; 3564 { 3565 register struct relocation_info *p = reloc_info; 3566 struct relocation_info *end 3567 = reloc_info + reloc_size / sizeof (struct relocation_info); 3568 int text_relocation = entry->text_start_address; 3569 int data_relocation = entry->data_start_address - entry->header.a_text; 3570 int bss_relocation 3571 = entry->bss_start_address - entry->header.a_text - entry->header.a_data; 3572 3573 for (; p < end; p++) 3574 { 3575 register int relocation = 0; 3576 register int addr = RELOC_ADDRESS(p); 3577 register unsigned int mask = 0; 3578 3579 if (addr >= data_size) 3580 fatal_with_file ("relocation address out of range in ", entry); 3581 3582 if (RELOC_EXTERN_P(p)) 3583 { 3584 int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist); 3585 symbol *sp = ((symbol *) 3586 (((struct nlist *) 3587 (((char *)entry->symbols) + symindex)) 3588 ->n_un.n_name)); 3589 3590 #ifdef N_INDR 3591 /* Resolve indirection */ 3592 if ((sp->defined & ~N_EXT) == N_INDR) 3593 sp = (symbol *) sp->value; 3594 #endif 3595 3596 if (symindex >= entry->header.a_syms) 3597 fatal_with_file ("relocation symbolnum out of range in ", entry); 3598 3599 /* If the symbol is undefined, leave it at zero. */ 3600 if (! sp->defined) 3601 relocation = 0; 3602 else 3603 relocation = sp->value; 3604 } 3605 else switch (RELOC_TYPE(p)) 3606 { 3607 case N_TEXT: 3608 case N_TEXT | N_EXT: 3609 relocation = text_relocation; 3610 break; 3611 3612 case N_DATA: 3613 case N_DATA | N_EXT: 3614 /* A word that points to beginning of the the data section 3615 initially contains not 0 but rather the "address" of that section 3616 in the input file, which is the length of the file's text. */ 3617 relocation = data_relocation; 3618 break; 3619 3620 case N_BSS: 3621 case N_BSS | N_EXT: 3622 /* Similarly, an input word pointing to the beginning of the bss 3623 initially contains the length of text plus data of the file. */ 3624 relocation = bss_relocation; 3625 break; 3626 3627 case N_ABS: 3628 case N_ABS | N_EXT: 3629 /* Don't know why this code would occur, but apparently it does. */ 3630 break; 3631 3632 default: 3633 fatal_with_file ("nonexternal relocation code invalid in ", entry); 3634 } 3635 3636 #ifdef RELOC_ADD_EXTRA 3637 relocation += RELOC_ADD_EXTRA(p); 3638 if (relocatable_output) 3639 { 3640 /* Non-PC relative relocations which are absolute 3641 or which have become non-external now have fixed 3642 relocations. Set the ADD_EXTRA of this relocation 3643 to be the relocation we have now determined. */ 3644 if (! RELOC_PCREL_P (p)) 3645 { 3646 if ((int)p->r_type <= RELOC_32 3647 || RELOC_EXTERN_P (p) == 0) 3648 RELOC_ADD_EXTRA (p) = relocation; 3649 } 3650 /* External PC-relative relocations continue to move around; 3651 update their relocations by the amount they have moved 3652 so far. */ 3653 else if (RELOC_EXTERN_P (p)) 3654 RELOC_ADD_EXTRA (p) -= pc_relocation; 3655 continue; 3656 } 3657 #endif 3658 3659 if (RELOC_PCREL_P(p)) 3660 relocation -= pc_relocation; 3661 3662 relocation >>= RELOC_VALUE_RIGHTSHIFT(p); 3663 3664 /* Unshifted mask for relocation */ 3665 mask = 1 << RELOC_TARGET_BITSIZE(p) - 1; 3666 mask |= mask - 1; 3667 relocation &= mask; 3668 3669 /* Shift everything up to where it's going to be used */ 3670 relocation <<= RELOC_TARGET_BITPOS(p); 3671 mask <<= RELOC_TARGET_BITPOS(p); 3672 3673 switch (RELOC_TARGET_SIZE(p)) 3674 { 3675 case 0: 3676 if (RELOC_MEMORY_SUB_P(p)) 3677 relocation -= mask & *(char *) (data + addr); 3678 else if (RELOC_MEMORY_ADD_P(p)) 3679 relocation += mask & *(char *) (data + addr); 3680 *(char *) (data + addr) &= ~mask; 3681 *(char *) (data + addr) |= relocation; 3682 break; 3683 3684 case 1: 3685 #ifdef tahoe 3686 if (((int) data + addr & 1) == 0) 3687 { 3688 #endif 3689 if (RELOC_MEMORY_SUB_P(p)) 3690 relocation -= mask & *(short *) (data + addr); 3691 else if (RELOC_MEMORY_ADD_P(p)) 3692 relocation += mask & *(short *) (data + addr); 3693 *(short *) (data + addr) &= ~mask; 3694 *(short *) (data + addr) |= relocation; 3695 #ifdef tahoe 3696 } 3697 /* 3698 * The CCI Power 6 (aka Tahoe) architecture has byte-aligned 3699 * instruction operands but requires data accesses to be aligned. 3700 * Brain-damage... 3701 */ 3702 else 3703 { 3704 unsigned char *da = (unsigned char *) (data + addr); 3705 unsigned short s = da[0] << 8 | da[1]; 3706 3707 if (RELOC_MEMORY_SUB_P(p)) 3708 relocation -= mask & s; 3709 else if (RELOC_MEMORY_ADD_P(p)) 3710 relocation += mask & s; 3711 s &= ~mask; 3712 s |= relocation; 3713 da[0] = s >> 8; 3714 da[1] = s; 3715 } 3716 #endif 3717 break; 3718 3719 case 2: 3720 #ifndef _CROSS_TARGET_ARCH 3721 #ifdef tahoe 3722 if (((int) data + addr & 3) == 0) 3723 { 3724 #endif 3725 if (RELOC_MEMORY_SUB_P(p)) 3726 relocation -= mask & *(long *) (data + addr); 3727 else if (RELOC_MEMORY_ADD_P(p)) 3728 relocation += mask & *(long *) (data + addr); 3729 *(long *) (data + addr) &= ~mask; 3730 *(long *) (data + addr) |= relocation; 3731 #ifdef tahoe 3732 } 3733 else 3734 { 3735 unsigned char *da = (unsigned char *) (data + addr); 3736 unsigned long l = da[0] << 24 | da[1] << 16 | da[2] << 8 | da[3]; 3737 3738 if (RELOC_MEMORY_SUB_P(p)) 3739 relocation -= mask & l; 3740 else if (RELOC_MEMORY_ADD_P(p)) 3741 relocation += mask & l; 3742 l &= ~mask; 3743 l |= relocation; 3744 da[0] = l >> 24; 3745 da[1] = l >> 16; 3746 da[2] = l >> 8; 3747 da[3] = l; 3748 } 3749 #endif 3750 #else 3751 /* Handle long word alignment requirements of SPARC architecture */ 3752 /* WARNING: This fix makes an assumption on byte ordering */ 3753 /* Marc Ullman, Stanford University Nov. 1 1989 */ 3754 if (RELOC_MEMORY_SUB_P(p)) { 3755 relocation -= mask & 3756 ((*(unsigned short *) (data + addr) << 16) | 3757 *(unsigned short *) (data + addr + 2)); 3758 } else if (RELOC_MEMORY_ADD_P(p)) { 3759 relocation += mask & 3760 ((*(unsigned short *) (data + addr) << 16) | 3761 *(unsigned short *) (data + addr + 2)); 3762 } 3763 *(unsigned short *) (data + addr) &= (~mask >> 16); 3764 *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff); 3765 *(unsigned short *) (data + addr) |= (relocation >> 16); 3766 *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff); 3767 #endif 3768 break; 3769 3770 default: 3771 fatal_with_file ("Unimplemented relocation field length in ", entry); 3772 } 3773 } 3774 } 3775 3776 /* For relocatable_output only: write out the relocation, 3777 relocating the addresses-to-be-relocated. */ 3778 3779 void coptxtrel (), copdatrel (); 3780 3781 void 3782 write_rel () 3783 { 3784 register int i; 3785 register int count = 0; 3786 3787 if (trace_files) 3788 fprintf (stderr, "Writing text relocation:\n\n"); 3789 3790 /* Assign each global symbol a sequence number, giving the order 3791 in which `write_syms' will write it. 3792 This is so we can store the proper symbolnum fields 3793 in relocation entries we write. */ 3794 3795 for (i = 0; i < TABSIZE; i++) 3796 { 3797 symbol *sp; 3798 for (sp = symtab[i]; sp; sp = sp->link) 3799 if (sp->referenced || sp->defined) 3800 { 3801 sp->def_count = count++; 3802 /* Leave room for the reference required by N_INDR, if 3803 necessary. */ 3804 if ((sp->defined & ~N_EXT) == N_INDR) 3805 count++; 3806 } 3807 } 3808 /* Correct, because if (relocatable_output), we will also be writing 3809 whatever indirect blocks we have. */ 3810 if (count != defined_global_sym_count 3811 + undefined_global_sym_count + global_indirect_count) 3812 fatal ("internal error"); 3813 3814 /* Write out the relocations of all files, remembered from copy_text. */ 3815 3816 each_full_file (coptxtrel, 0); 3817 3818 if (trace_files) 3819 fprintf (stderr, "\nWriting data relocation:\n\n"); 3820 3821 each_full_file (copdatrel, 0); 3822 3823 if (trace_files) 3824 fprintf (stderr, "\n"); 3825 } 3826 3827 void 3828 coptxtrel (entry) 3829 struct file_entry *entry; 3830 { 3831 register struct relocation_info *p, *end; 3832 register int reloc = entry->text_start_address; 3833 3834 p = entry->textrel; 3835 end = (struct relocation_info *) (entry->header.a_trsize + (char *) p); 3836 while (p < end) 3837 { 3838 RELOC_ADDRESS(p) += reloc; 3839 if (RELOC_EXTERN_P(p)) 3840 { 3841 register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist); 3842 symbol *symptr = ((symbol *) 3843 (((struct nlist *) 3844 (((char *)entry->symbols) + symindex)) 3845 ->n_un.n_name)); 3846 3847 if (symindex >= entry->header.a_syms) 3848 fatal_with_file ("relocation symbolnum out of range in ", entry); 3849 3850 #ifdef N_INDR 3851 /* Resolve indirection. */ 3852 if ((symptr->defined & ~N_EXT) == N_INDR) 3853 symptr = (symbol *) symptr->value; 3854 #endif 3855 3856 /* If the symbol is now defined, change the external relocation 3857 to an internal one. */ 3858 3859 if (symptr->defined) 3860 { 3861 RELOC_EXTERN_P(p) = 0; 3862 RELOC_SYMBOL(p) = (symptr->defined & N_TYPE); 3863 #ifdef RELOC_ADD_EXTRA 3864 /* If we aren't going to be adding in the value in 3865 memory on the next pass of the loader, then we need 3866 to add it in from the relocation entry. Otherwise 3867 the work we did in this pass is lost. */ 3868 if (!RELOC_MEMORY_ADD_P(p)) 3869 RELOC_ADD_EXTRA (p) += symptr->value; 3870 #endif 3871 } 3872 else 3873 /* Debugger symbols come first, so have to start this 3874 after them. */ 3875 RELOC_SYMBOL(p) = (symptr->def_count + nsyms 3876 - defined_global_sym_count 3877 - undefined_global_sym_count 3878 - global_indirect_count); 3879 } 3880 p++; 3881 } 3882 mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc); 3883 } 3884 3885 void 3886 copdatrel (entry) 3887 struct file_entry *entry; 3888 { 3889 register struct relocation_info *p, *end; 3890 /* Relocate the address of the relocation. 3891 Old address is relative to start of the input file's data section. 3892 New address is relative to start of the output file's data section. */ 3893 register int reloc = entry->data_start_address - text_size; 3894 3895 p = entry->datarel; 3896 end = (struct relocation_info *) (entry->header.a_drsize + (char *) p); 3897 while (p < end) 3898 { 3899 RELOC_ADDRESS(p) += reloc; 3900 if (RELOC_EXTERN_P(p)) 3901 { 3902 register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist); 3903 symbol *symptr = ((symbol *) 3904 (((struct nlist *) 3905 (((char *)entry->symbols) + symindex)) 3906 ->n_un.n_name)); 3907 int symtype; 3908 3909 if (symindex >= entry->header.a_syms) 3910 fatal_with_file ("relocation symbolnum out of range in ", entry); 3911 3912 #ifdef N_INDR 3913 /* Resolve indirection. */ 3914 if ((symptr->defined & ~N_EXT) == N_INDR) 3915 symptr = (symbol *) symptr->value; 3916 #endif 3917 3918 symtype = symptr->defined & N_TYPE; 3919 3920 if (force_common_definition 3921 || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS) 3922 { 3923 RELOC_EXTERN_P(p) = 0; 3924 RELOC_SYMBOL(p) = symtype; 3925 } 3926 else 3927 /* Debugger symbols come first, so have to start this 3928 after them. */ 3929 RELOC_SYMBOL(p) 3930 = (((symbol *) 3931 (((struct nlist *) 3932 (((char *)entry->symbols) + symindex)) 3933 ->n_un.n_name)) 3934 ->def_count 3935 + nsyms - defined_global_sym_count 3936 - undefined_global_sym_count 3937 - global_indirect_count); 3938 } 3939 p++; 3940 } 3941 mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc); 3942 } 3943 3944 void write_file_syms (); 3945 void write_string_table (); 3946 3947 /* Offsets and current lengths of symbol and string tables in output file. */ 3948 3949 int symbol_table_offset; 3950 int symbol_table_len; 3951 3952 /* Address in output file where string table starts. */ 3953 int string_table_offset; 3954 3955 /* Offset within string table 3956 where the strings in `strtab_vector' should be written. */ 3957 int string_table_len; 3958 3959 /* Total size of string table strings allocated so far, 3960 including strings in `strtab_vector'. */ 3961 int strtab_size; 3962 3963 /* Vector whose elements are strings to be added to the string table. */ 3964 char **strtab_vector; 3965 3966 /* Vector whose elements are the lengths of those strings. */ 3967 int *strtab_lens; 3968 3969 /* Index in `strtab_vector' at which the next string will be stored. */ 3970 int strtab_index; 3971 3972 /* Add the string NAME to the output file string table. 3973 Record it in `strtab_vector' to be output later. 3974 Return the index within the string table that this string will have. */ 3975 3976 int 3977 assign_string_table_index (name) 3978 char *name; 3979 { 3980 register int index = strtab_size; 3981 register int len = strlen (name) + 1; 3982 3983 strtab_size += len; 3984 strtab_vector[strtab_index] = name; 3985 strtab_lens[strtab_index++] = len; 3986 3987 return index; 3988 } 3989 3990 FILE *outstream = (FILE *) 0; 3991 3992 /* Write the contents of `strtab_vector' into the string table. 3993 This is done once for each file's local&debugger symbols 3994 and once for the global symbols. */ 3995 3996 void 3997 write_string_table () 3998 { 3999 register int i; 4000 4001 lseek (outdesc, string_table_offset + string_table_len, 0); 4002 4003 if (!outstream) 4004 outstream = fdopen (outdesc, "w"); 4005 4006 for (i = 0; i < strtab_index; i++) 4007 { 4008 fwrite (strtab_vector[i], 1, strtab_lens[i], outstream); 4009 string_table_len += strtab_lens[i]; 4010 } 4011 4012 fflush (outstream); 4013 4014 /* Report I/O error such as disk full. */ 4015 if (ferror (outstream)) 4016 perror_name (output_filename); 4017 } 4018 4019 /* Write the symbol table and string table of the output file. */ 4020 4021 void 4022 write_syms () 4023 { 4024 /* Number of symbols written so far. */ 4025 int syms_written = 0; 4026 register int i; 4027 register symbol *sp; 4028 4029 /* Buffer big enough for all the global symbols. One 4030 extra struct for each indirect symbol to hold the extra reference 4031 following. */ 4032 struct nlist *buf 4033 = (struct nlist *) alloca ((defined_global_sym_count 4034 + undefined_global_sym_count 4035 + global_indirect_count) 4036 * sizeof (struct nlist)); 4037 /* Pointer for storing into BUF. */ 4038 register struct nlist *bufp = buf; 4039 4040 /* Size of string table includes the bytes that store the size. */ 4041 strtab_size = sizeof strtab_size; 4042 4043 symbol_table_offset = N_SYMOFF (outheader); 4044 symbol_table_len = 0; 4045 string_table_offset = N_STROFF (outheader); 4046 string_table_len = strtab_size; 4047 4048 if (strip_symbols == STRIP_ALL) 4049 return; 4050 4051 /* Write the local symbols defined by the various files. */ 4052 4053 each_file (write_file_syms, &syms_written); 4054 file_close (); 4055 4056 /* Now write out the global symbols. */ 4057 4058 /* Allocate two vectors that record the data to generate the string 4059 table from the global symbols written so far. This must include 4060 extra space for the references following indirect outputs. */ 4061 4062 strtab_vector = (char **) alloca ((num_hash_tab_syms 4063 + global_indirect_count) * sizeof (char *)); 4064 strtab_lens = (int *) alloca ((num_hash_tab_syms 4065 + global_indirect_count) * sizeof (int)); 4066 strtab_index = 0; 4067 4068 /* Scan the symbol hash table, bucket by bucket. */ 4069 4070 for (i = 0; i < TABSIZE; i++) 4071 for (sp = symtab[i]; sp; sp = sp->link) 4072 { 4073 struct nlist nl; 4074 4075 nl.n_other = 0; 4076 nl.n_desc = 0; 4077 4078 /* Compute a `struct nlist' for the symbol. */ 4079 4080 if (sp->defined || sp->referenced) 4081 { 4082 /* common condition needs to be before undefined condition */ 4083 /* because unallocated commons are set undefined in */ 4084 /* digest_symbols */ 4085 if (sp->defined > 1) /* defined with known type */ 4086 { 4087 /* If the target of an indirect symbol has been 4088 defined and we are outputting an executable, 4089 resolve the indirection; it's no longer needed */ 4090 if (!relocatable_output 4091 && ((sp->defined & N_TYPE) == N_INDR) 4092 && (((symbol *) sp->value)->defined > 1)) 4093 { 4094 symbol *newsp = (symbol *) sp->value; 4095 nl.n_type = newsp->defined; 4096 nl.n_value = newsp->value; 4097 } 4098 else 4099 { 4100 nl.n_type = sp->defined; 4101 if (sp->defined != (N_INDR | N_EXT)) 4102 nl.n_value = sp->value; 4103 else 4104 nl.n_value = 0; 4105 } 4106 } 4107 else if (sp->max_common_size) /* defined as common but not allocated. */ 4108 { 4109 /* happens only with -r and not -d */ 4110 /* write out a common definition */ 4111 nl.n_type = N_UNDF | N_EXT; 4112 nl.n_value = sp->max_common_size; 4113 } 4114 else if (!sp->defined) /* undefined -- legit only if -r */ 4115 { 4116 nl.n_type = N_UNDF | N_EXT; 4117 nl.n_value = 0; 4118 } 4119 else 4120 fatal ("internal error: %s defined in mysterious way", sp->name); 4121 4122 /* Allocate string table space for the symbol name. */ 4123 4124 nl.n_un.n_strx = assign_string_table_index (sp->name); 4125 4126 /* Output to the buffer and count it. */ 4127 4128 *bufp++ = nl; 4129 syms_written++; 4130 if (nl.n_type == (N_INDR | N_EXT)) 4131 { 4132 struct nlist xtra_ref; 4133 xtra_ref.n_type = N_EXT | N_UNDF; 4134 xtra_ref.n_un.n_strx 4135 = assign_string_table_index (((symbol *) sp->value)->name); 4136 xtra_ref.n_other = 0; 4137 xtra_ref.n_desc = 0; 4138 xtra_ref.n_value = 0; 4139 *bufp++ = xtra_ref; 4140 syms_written++; 4141 } 4142 } 4143 } 4144 4145 /* Output the buffer full of `struct nlist's. */ 4146 4147 lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 4148 mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc); 4149 symbol_table_len += sizeof (struct nlist) * (bufp - buf); 4150 4151 if (syms_written != nsyms) 4152 fatal ("internal error: wrong number of symbols written into output file", 0); 4153 4154 if (symbol_table_offset + symbol_table_len != string_table_offset) 4155 fatal ("internal error: inconsistent symbol table length", 0); 4156 4157 /* Now the total string table size is known, so write it. 4158 We are already positioned at the right place in the file. */ 4159 4160 mywrite (&strtab_size, sizeof (int), 1, outdesc); /* we're at right place */ 4161 4162 /* Write the strings for the global symbols. */ 4163 4164 write_string_table (); 4165 } 4166 4167 /* Write the local and debugger symbols of file ENTRY. 4168 Increment *SYMS_WRITTEN_ADDR for each symbol that is written. */ 4169 4170 /* Note that we do not combine identical names of local symbols. 4171 dbx or gdb would be confused if we did that. */ 4172 4173 void 4174 write_file_syms (entry, syms_written_addr) 4175 struct file_entry *entry; 4176 int *syms_written_addr; 4177 { 4178 register struct nlist *p = entry->symbols; 4179 register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist); 4180 4181 /* Buffer to accumulate all the syms before writing them. 4182 It has one extra slot for the local symbol we generate here. */ 4183 struct nlist *buf 4184 = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist)); 4185 register struct nlist *bufp = buf; 4186 4187 /* Upper bound on number of syms to be written here. */ 4188 int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1; 4189 4190 /* Make tables that record, for each symbol, its name and its name's length. 4191 The elements are filled in by `assign_string_table_index'. */ 4192 4193 strtab_vector = (char **) alloca (max_syms * sizeof (char *)); 4194 strtab_lens = (int *) alloca (max_syms * sizeof (int)); 4195 strtab_index = 0; 4196 4197 /* Generate a local symbol for the start of this file's text. */ 4198 4199 if (discard_locals != DISCARD_ALL) 4200 { 4201 struct nlist nl; 4202 4203 nl.n_type = N_FN | N_EXT; 4204 nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); 4205 nl.n_value = entry->text_start_address; 4206 nl.n_desc = 0; 4207 nl.n_other = 0; 4208 *bufp++ = nl; 4209 (*syms_written_addr)++; 4210 entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist); 4211 } 4212 4213 /* Read the file's string table. */ 4214 4215 entry->strings = (char *) alloca (entry->string_size); 4216 read_entry_strings (file_open (entry), entry); 4217 4218 for (; p < end; p++) 4219 { 4220 register int type = p->n_type; 4221 register int write = 0; 4222 4223 /* WRITE gets 1 for a non-global symbol that should be written. */ 4224 4225 4226 if (SET_ELEMENT_P (type)) /* This occurs even if global. These */ 4227 /* types of symbols are never written */ 4228 /* globally, though they are stored */ 4229 /* globally. */ 4230 write = relocatable_output; 4231 else if (!(type & (N_STAB | N_EXT))) 4232 /* ordinary local symbol */ 4233 write = ((discard_locals != DISCARD_ALL) 4234 && !(discard_locals == DISCARD_L && 4235 (p->n_un.n_strx + entry->strings)[0] == LPREFIX) 4236 && type != N_WARNING); 4237 else if (!(type & N_EXT)) 4238 /* debugger symbol */ 4239 write = (strip_symbols == STRIP_NONE); 4240 4241 if (write) 4242 { 4243 /* If this symbol has a name, 4244 allocate space for it in the output string table. */ 4245 4246 if (p->n_un.n_strx) 4247 p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx 4248 + entry->strings); 4249 4250 /* Output this symbol to the buffer and count it. */ 4251 4252 *bufp++ = *p; 4253 (*syms_written_addr)++; 4254 } 4255 } 4256 4257 /* All the symbols are now in BUF; write them. */ 4258 4259 lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 4260 mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc); 4261 symbol_table_len += sizeof (struct nlist) * (bufp - buf); 4262 4263 /* Write the string-table data for the symbols just written, 4264 using the data in vectors `strtab_vector' and `strtab_lens'. */ 4265 4266 write_string_table (); 4267 entry->strings = 0; /* Since it will dissapear anyway. */ 4268 } 4269 4270 /* Copy any GDB symbol segments from the input files to the output file. 4271 The contents of the symbol segment is copied without change 4272 except that we store some information into the beginning of it. */ 4273 4274 void write_file_symseg (); 4275 4276 void 4277 write_symsegs () 4278 { 4279 each_file (write_file_symseg, 0); 4280 } 4281 4282 void 4283 write_file_symseg (entry) 4284 struct file_entry *entry; 4285 { 4286 char buffer[4096]; 4287 struct symbol_root root; 4288 int indesc; 4289 int len; 4290 4291 if (entry->symseg_offset == 0) 4292 return; 4293 4294 /* This entry has a symbol segment. Read the root of the segment. */ 4295 4296 indesc = file_open (entry); 4297 lseek (indesc, entry->symseg_offset + entry->starting_offset, 0); 4298 if (sizeof root != read (indesc, &root, sizeof root)) 4299 fatal_with_file ("premature end of file in symbol segment of ", entry); 4300 4301 /* Store some relocation info into the root. */ 4302 4303 root.ldsymoff = entry->local_syms_offset; 4304 root.textrel = entry->text_start_address; 4305 root.datarel = entry->data_start_address - entry->header.a_text; 4306 root.bssrel = entry->bss_start_address 4307 - entry->header.a_text - entry->header.a_data; 4308 root.databeg = entry->data_start_address - root.datarel; 4309 root.bssbeg = entry->bss_start_address - root.bssrel; 4310 4311 /* Write the modified root into the output file. */ 4312 4313 mywrite (&root, sizeof root, 1, outdesc); 4314 4315 /* Copy the rest of the symbol segment unchanged. */ 4316 4317 if (entry->superfile) 4318 { 4319 /* Library member: number of bytes to copy is determined 4320 from the member's total size. */ 4321 4322 int total = entry->total_size - entry->symseg_offset - sizeof root; 4323 4324 while (total > 0) 4325 { 4326 len = read (indesc, buffer, min (sizeof buffer, total)); 4327 4328 if (len != min (sizeof buffer, total)) 4329 fatal_with_file ("premature end of file in symbol segment of ", entry); 4330 total -= len; 4331 mywrite (buffer, len, 1, outdesc); 4332 } 4333 } 4334 else 4335 { 4336 /* A separate file: copy until end of file. */ 4337 4338 while (len = read (indesc, buffer, sizeof buffer)) 4339 { 4340 mywrite (buffer, len, 1, outdesc); 4341 if (len < sizeof buffer) 4342 break; 4343 } 4344 } 4345 4346 file_close (); 4347 } 4348 4349 /* Create the symbol table entries for `etext', `edata' and `end'. */ 4350 4351 void 4352 symtab_init () 4353 { 4354 #ifndef nounderscore 4355 edata_symbol = getsym ("_edata"); 4356 etext_symbol = getsym ("_etext"); 4357 end_symbol = getsym ("_end"); 4358 #else 4359 edata_symbol = getsym ("edata"); 4360 etext_symbol = getsym ("etext"); 4361 end_symbol = getsym ("end"); 4362 #endif 4363 4364 #ifdef sun 4365 { 4366 symbol *dynamic_symbol = getsym ("__DYNAMIC"); 4367 dynamic_symbol->defined = N_ABS | N_EXT; 4368 dynamic_symbol->referenced = 1; 4369 dynamic_symbol->value = 0; 4370 } 4371 #endif 4372 4373 #ifdef sequent 4374 { 4375 symbol *_387_flt_symbol = getsym ("_387_flt"); 4376 _387_flt_symbol->defined = N_ABS | N_EXT; 4377 _387_flt_symbol->referenced = 1; 4378 _387_flt_symbol->value = 0; 4379 } 4380 #endif 4381 4382 edata_symbol->defined = N_DATA | N_EXT; 4383 etext_symbol->defined = N_TEXT | N_EXT; 4384 end_symbol->defined = N_BSS | N_EXT; 4385 4386 edata_symbol->referenced = 1; 4387 etext_symbol->referenced = 1; 4388 end_symbol->referenced = 1; 4389 } 4390 4391 /* Compute the hash code for symbol name KEY. */ 4392 4393 int 4394 hash_string (key) 4395 char *key; 4396 { 4397 register char *cp; 4398 register int k; 4399 4400 cp = key; 4401 k = 0; 4402 while (*cp) 4403 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 4404 4405 return k; 4406 } 4407 4408 /* Get the symbol table entry for the global symbol named KEY. 4409 Create one if there is none. */ 4410 4411 symbol * 4412 getsym (key) 4413 char *key; 4414 { 4415 register int hashval; 4416 register symbol *bp; 4417 4418 /* Determine the proper bucket. */ 4419 4420 hashval = hash_string (key) % TABSIZE; 4421 4422 /* Search the bucket. */ 4423 4424 for (bp = symtab[hashval]; bp; bp = bp->link) 4425 if (! strcmp (key, bp->name)) 4426 return bp; 4427 4428 /* Nothing was found; create a new symbol table entry. */ 4429 4430 bp = (symbol *) xmalloc (sizeof (symbol)); 4431 bp->refs = 0; 4432 bp->name = (char *) xmalloc (strlen (key) + 1); 4433 strcpy (bp->name, key); 4434 bp->defined = 0; 4435 bp->referenced = 0; 4436 bp->trace = 0; 4437 bp->value = 0; 4438 bp->max_common_size = 0; 4439 bp->warning = 0; 4440 bp->undef_refs = 0; 4441 bp->multiply_defined = 0; 4442 4443 /* Add the entry to the bucket. */ 4444 4445 bp->link = symtab[hashval]; 4446 symtab[hashval] = bp; 4447 4448 ++num_hash_tab_syms; 4449 4450 return bp; 4451 } 4452 4453 /* Like `getsym' but return 0 if the symbol is not already known. */ 4454 4455 symbol * 4456 getsym_soft (key) 4457 char *key; 4458 { 4459 register int hashval; 4460 register symbol *bp; 4461 4462 /* Determine which bucket. */ 4463 4464 hashval = hash_string (key) % TABSIZE; 4465 4466 /* Search the bucket. */ 4467 4468 for (bp = symtab[hashval]; bp; bp = bp->link) 4469 if (! strcmp (key, bp->name)) 4470 return bp; 4471 4472 return 0; 4473 } 4474 4475 /* Report a fatal error. 4476 STRING is a printf format string and ARG is one arg for it. */ 4477 4478 void 4479 fatal (string, arg) 4480 char *string, *arg; 4481 { 4482 fprintf (stderr, "ld: "); 4483 fprintf (stderr, string, arg); 4484 fprintf (stderr, "\n"); 4485 exit (1); 4486 } 4487 4488 /* Report a fatal error. The error message is STRING 4489 followed by the filename of ENTRY. */ 4490 4491 void 4492 fatal_with_file (string, entry) 4493 char *string; 4494 struct file_entry *entry; 4495 { 4496 fprintf (stderr, "ld: "); 4497 fprintf (stderr, string); 4498 print_file_name (entry, stderr); 4499 fprintf (stderr, "\n"); 4500 exit (1); 4501 } 4502 4503 /* Report a fatal error using the message for the last failed system call, 4504 followed by the string NAME. */ 4505 4506 void 4507 perror_name (name) 4508 char *name; 4509 { 4510 extern int errno, sys_nerr; 4511 extern char *sys_errlist[]; 4512 char *s; 4513 4514 if (errno < sys_nerr) 4515 s = concat ("", sys_errlist[errno], " for %s"); 4516 else 4517 s = "cannot open %s"; 4518 fatal (s, name); 4519 } 4520 4521 /* Report a fatal error using the message for the last failed system call, 4522 followed by the name of file ENTRY. */ 4523 4524 void 4525 perror_file (entry) 4526 struct file_entry *entry; 4527 { 4528 extern int errno, sys_nerr; 4529 extern char *sys_errlist[]; 4530 char *s; 4531 4532 if (errno < sys_nerr) 4533 s = concat ("", sys_errlist[errno], " for "); 4534 else 4535 s = "cannot open "; 4536 fatal_with_file (s, entry); 4537 } 4538 4539 /* Report a nonfatal error. 4540 STRING is a format for printf, and ARG1 ... ARG3 are args for it. */ 4541 4542 void 4543 error (string, arg1, arg2, arg3) 4544 char *string, *arg1, *arg2, *arg3; 4545 { 4546 fprintf (stderr, "%s: ", progname); 4547 fprintf (stderr, string, arg1, arg2, arg3); 4548 fprintf (stderr, "\n"); 4549 } 4550 4551 4552 /* Output COUNT*ELTSIZE bytes of data at BUF 4553 to the descriptor DESC. */ 4554 4555 void 4556 mywrite (buf, count, eltsize, desc) 4557 char *buf; 4558 int count; 4559 int eltsize; 4560 int desc; 4561 { 4562 register int val; 4563 register int bytes = count * eltsize; 4564 4565 while (bytes > 0) 4566 { 4567 val = write (desc, buf, bytes); 4568 if (val <= 0) 4569 perror_name (output_filename); 4570 buf += val; 4571 bytes -= val; 4572 } 4573 } 4574 4575 /* Output PADDING zero-bytes to descriptor OUTDESC. 4576 PADDING may be negative; in that case, do nothing. */ 4577 4578 void 4579 padfile (padding, outdesc) 4580 int padding; 4581 int outdesc; 4582 { 4583 register char *buf; 4584 if (padding <= 0) 4585 return; 4586 4587 buf = (char *) alloca (padding); 4588 bzero (buf, padding); 4589 mywrite (buf, padding, 1, outdesc); 4590 } 4591 4592 /* Return a newly-allocated string 4593 whose contents concatenate the strings S1, S2, S3. */ 4594 4595 char * 4596 concat (s1, s2, s3) 4597 char *s1, *s2, *s3; 4598 { 4599 register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); 4600 register char *result = (char *) xmalloc (len1 + len2 + len3 + 1); 4601 4602 strcpy (result, s1); 4603 strcpy (result + len1, s2); 4604 strcpy (result + len1 + len2, s3); 4605 result[len1 + len2 + len3] = 0; 4606 4607 return result; 4608 } 4609 4610 /* Parse the string ARG using scanf format FORMAT, and return the result. 4611 If it does not parse, report fatal error 4612 generating the error message using format string ERROR and ARG as arg. */ 4613 4614 int 4615 parse (arg, format, error) 4616 char *arg, *format; 4617 { 4618 int x; 4619 if (1 != sscanf (arg, format, &x)) 4620 fatal (error, arg); 4621 return x; 4622 } 4623 4624 /* Like malloc but get fatal error if memory is exhausted. */ 4625 4626 int 4627 xmalloc (size) 4628 int size; 4629 { 4630 register int result = malloc (size); 4631 if (!result) 4632 fatal ("virtual memory exhausted", 0); 4633 return result; 4634 } 4635 4636 /* Like realloc but get fatal error if memory is exhausted. */ 4637 4638 int 4639 xrealloc (ptr, size) 4640 char *ptr; 4641 int size; 4642 { 4643 register int result = realloc (ptr, size); 4644 if (!result) 4645 fatal ("virtual memory exhausted", 0); 4646 return result; 4647 } 4648 4649 #ifdef USG 4650 4651 void 4652 bzero (p, n) 4653 char *p; 4654 { 4655 memset (p, 0, n); 4656 } 4657 4658 void 4659 bcopy (from, to, n) 4660 char *from, *to; 4661 { 4662 memcpy (to, from, n); 4663 } 4664 4665 getpagesize () 4666 { 4667 return (4096); 4668 } 4669 4670 #endif 4671 4672 #if defined(sun) && (TARGET == SUN4) 4673 4674 /* Don't use local pagesize to build for Sparc. */ 4675 4676 getpagesize () 4677 { 4678 return (8192); 4679 } 4680 #endif 4681