1 /* aslink.h */ 2 3 /* 4 * Copyright (C) 1989-2012 Alan R. Baldwin 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * 19 * 20 * Alan R. Baldwin 21 * 721 Berkeley St. 22 * Kent, Ohio 44240 23 * 24 * With enhancements from 25 * John L. Hartman (JLH) 26 * jhartman@compuserve.com 27 */ 28 29 /* 30 * System Include Files 31 */ 32 33 #include <stdlib.h> 34 #include <stdio.h> 35 #include <string.h> 36 #include "sdld.h" 37 38 /* 39 * Local Definitions 40 */ 41 42 #define VERSION "V03.00 + NoICE + sdld" 43 #define COPYRIGHT "2012" 44 45 /* 46 * To include NoICE Debugging set non-zero 47 */ 48 #define NOICE 1 49 50 /* 51 * To include SDCC Debugging set non-zero 52 */ 53 #define SDCDB 1 54 55 /* 56 * The assembler requires certain variables to have 57 * at least 32 bits to allow correct address processing. 58 * 59 * The type INT32 is defined so that compiler dependent 60 * variable sizes may be specified in one place. 61 * 62 * LONGINT is defined when INT32 is 'long' to 63 * select the 'l' forms for format strings. 64 */ 65 66 /* Turbo C++ 3.0 for DOS */ 67 /* 'int' is 16-bits, 'long' is 32-bits */ 68 69 #ifdef __TURBOC__ 70 #define INT32 long 71 #define LONGINT 72 #endif 73 74 /* Symantec C++ V6.x/V7.x for DOS (not DOSX) */ 75 /* 'int' is 16-bits, 'long' is 32-bits */ 76 77 #ifdef __SC__ 78 #define INT32 long 79 #define LONGINT 80 #endif 81 82 /* The DEFAULT is 'int' is 32 bits */ 83 #ifndef INT32 84 #define INT32 int 85 #endif 86 87 88 /*)Module aslink.h 89 * 90 * The module aslink.h contains the definitions for constants, 91 * structures, global variables, and LKxxxx functions 92 * contained in the LKxxxx.c files. 93 */ 94 95 /*)BUILD 96 $(PROGRAM) = ASLINK 97 $(INCLUDE) = ASLINK.H 98 $(FILES) = { 99 LKMAIN.C 100 LKLEX.C 101 LKAREA.C 102 LKBANK.C 103 LKHEAD.C 104 LKSYM.C 105 LKEVAL.C 106 LKDATA.C 107 LKLIST.C 108 LKNOICE.C 109 LKSDCDB.C 110 LKRLOC.C 111 LKRLOC3.C 112 LKLIBR.C 113 LKOUT.C 114 LKS19.C 115 } 116 $(STACK) = 2000 117 */ 118 119 #if defined DECUS 120 /* DECUS C void definition */ 121 /* File/extension seperator */ 122 123 #define VOID char 124 #define FSEPX '.' 125 126 #elif defined PDOS 127 /* PDOS C void definition */ 128 /* File/extension seperator */ 129 130 #define VOID char 131 #define FSEPX ':' 132 133 #elif defined UNIX 134 /* UNIX void definition */ 135 /* File/extension seperator */ 136 137 #define VOID void 138 #define FSEPX '.' 139 #define LKDIRSEP '/' 140 #define LKDIRSEPSTR "/" 141 #define OTHERSYSTEM 142 143 #else 144 /* DOS/WINDOWS void definition */ 145 /* File/extension seperator */ 146 147 #define VOID void 148 #define FSEPX '.' 149 #define LKDIRSEP '\\' 150 #define LKDIRSEPSTR "\\" 151 #define OTHERSYSTEM 152 153 #endif 154 155 /* 156 * PATH_MAX 157 */ 158 #include <limits.h> 159 #ifndef PATH_MAX /* POSIX, but not required */ 160 #if defined(__BORLANDC__) || defined(_MSC_VER) 161 #include <stdlib.h> 162 #define PATH_MAX _MAX_PATH 163 #elif defined(__x86_64__) 164 #define PATH_MAX 4096 165 #else 166 #define PATH_MAX 255 /* define a reasonable value */ 167 #endif 168 #endif 169 170 #ifdef _WIN32 /* WIN32 native */ 171 # define NATIVE_WIN32 1 172 # ifdef __MINGW32__ /* GCC MINGW32 depends on configure */ 173 # include "../../sdccconf.h" 174 # else 175 # include "../../sdcc_vc.h" 176 # define PATH_MAX _MAX_PATH 177 # endif 178 #else /* Assume *nix style system */ 179 # include "../../sdccconf.h" 180 #endif 181 182 #define LKOBJEXT "rel" 183 184 /* 185 * Global symbol types. 186 */ 187 #define S_REF 1 /* referenced */ 188 #define S_DEF 2 /* defined */ 189 190 /* 191 * File types 192 */ 193 #define F_OUT 0 /* File.ixx / File.sxx */ 194 #define F_STD 1 /* stdin */ 195 #define F_LNK 2 /* File.lnk */ 196 #define F_REL 3 /* File.rel */ 197 198 /* 199 * Error definitions 200 */ 201 #define ER_NONE 0 /* No error */ 202 #define ER_WARNING 1 /* Warning */ 203 #define ER_ERROR 2 /* Assembly error */ 204 #define ER_FATAL 3 /* Fatal error */ 205 206 /* 207 * This file defines the format of the 208 * relocatable binary file. 209 */ 210 211 #define NCPS PATH_MAX /* characters per symbol */ 212 #define NINPUT PATH_MAX /* Input buffer size */ 213 #define NHASH (1 << 6) /* Buckets in hash table */ 214 #define HMASK (NHASH - 1) /* Hash mask */ 215 #define NLPP 60 /* Lines per page */ 216 #define NMAX 78 /* IXX/SXX/DBX Buffer Length */ 217 #define IXXMAXBYTES 32 /* NMAX > (2 * IXXMAXBYTES) */ 218 #define SXXMAXBYTES 32 /* NMAX > (2 * SXXMAXBYTES) */ 219 #define DBXMAXBYTES 64 /* NMAX > ( DBXMAXBYTES ) */ 220 #define FILSPC PATH_MAX /* File spec length */ 221 222 #define NDATA 16 /* actual data */ 223 /* 224 * NTXT must be defined to have the same value in 225 * the ASxxxx assemblers and ASLink. 226 * 227 * The R Line coding allows only 4-bits for coding 228 * the T Line index. The MAXIMUM value for NTXT 229 * is 16. It should not be changed. 230 */ 231 #define NTXT 16 /* T values */ 232 233 /* 234 * Opcode Cycle definitions (Must Be The Same In ASxxxx / ASLink) 235 */ 236 #define CYCNT_BGN '[' /* Cycle count begin delimiter */ 237 #define CYCNT_END ']' /* Cycle count end delimiter */ 238 239 /* 240 * Internal ASxxxx Version Variable 241 */ 242 extern int ASxxxx_VERSION; 243 244 245 /* 246 * ASLINK - Version 3 Definitions 247 */ 248 249 /* 250 * The "A3_" area constants define values used in 251 * generating the assembler area output data. 252 * 253 * Area flags 254 * 255 * 7 6 5 4 3 2 1 0 256 * +-----+-----+-----+-----+-----+-----+-----+-----+ 257 * | | | | PAG | ABS | OVR | | | 258 * +-----+-----+-----+-----+-----+-----+-----+-----+ 259 */ 260 261 #define A3_CON 000 /* concatenate */ 262 #define A3_OVR 004 /* overlay */ 263 #define A3_REL 000 /* relocatable */ 264 #define A3_ABS 010 /* absolute */ 265 #define A3_NOPAG 000 /* non-paged */ 266 #define A3_PAG 020 /* paged */ 267 268 /* sdld specific */ 269 /* Additional flags for 8051 address spaces */ 270 #define A_DATA 0000 /* data space (default)*/ 271 #define A_CODE 0040 /* code space */ 272 #define A_XDATA 0100 /* external data space */ 273 #define A_BIT 0200 /* bit addressable space */ 274 275 /* Additional flags for hc08 */ 276 #define A_NOLOAD 0400 /* nonloadable */ 277 #define A_LOAD 0000 /* loadable (default) */ 278 /* end sdld specific */ 279 280 /* 281 * The "R3_" relocation constants define values used in 282 * generating the assembler relocation output data for 283 * areas, symbols, and code. 284 * 285 * 286 * Relocation types. 287 * 288 * 7 6 5 4 3 2 1 0 289 * +-----+-----+-----+-----+-----+-----+-----+-----+ 290 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT | 291 * +-----+-----+-----+-----+-----+-----+-----+-----+ 292 */ 293 294 #define R3_WORD 0x00 /* 16 bit */ 295 #define R3_BYTE 0x01 /* 8 bit */ 296 297 #define R3_AREA 0x00 /* Base type */ 298 #define R3_SYM 0x02 299 300 #define R3_NORM 0x00 /* PC adjust */ 301 #define R3_PCR 0x04 302 303 #define R3_BYT1 0x00 /* Byte count for R_BYTE = 1 */ 304 #define R3_BYTX 0x08 /* Byte count for R_BYTE = X */ 305 306 #define R3_SGND 0x00 /* Signed value */ 307 #define R3_USGN 0x10 /* Unsigned value */ 308 309 #define R3_NOPAG 0x00 /* Page Mode */ 310 #define R3_PAG0 0x20 /* Page '0' */ 311 #define R3_PAG 0x40 /* Page 'nnn' */ 312 313 #define R3_LSB 0x00 /* output low byte */ 314 #define R3_MSB 0x80 /* output high byte */ 315 316 /* 317 * Additional "R3_" functionality is required to support 318 * some microprocesssor architectures. The 'illegal' 319 * "R3_" mode of R3_WORD | R3_BYTX is used as a designator 320 * of the extended R3_ modes. The extended modes replace 321 * the PAGING modes and are being added in an adhoc manner 322 * as follows: 323 * 324 * Extended Mode relocation flags 325 * 326 * 7 6 5 4 3 2 1 0 327 * +-----+-----+-----+-----+-----+-----+-----+-----+ 328 * | MSB | x | x | USGN| 1 | PCR | SYM | 0 | 329 * +-----+-----+-----+-----+-----+-----+-----+-----+ 330 */ 331 332 #define R3_ECHEK 0011 /* Extended Mode Check Bits */ 333 #define R3_EXTND 0010 /* Extended Mode Code */ 334 #define R3_EMASK 0151 /* Extended Mode Mask */ 335 336 /* #define R3_AREA 0000 */ /* Base type */ 337 /* #define R3_SYM 0002 */ 338 339 /* #define R3_NORM 0000 */ /* PC adjust */ 340 /* #define R3_PCR 0004 */ 341 342 /* #define R3_SGND 0000 */ /* Signed value */ 343 /* #define R3_USGN 0020 */ /* Unsigned value */ 344 345 /* #define R3_LSB 0000 */ /* output low byte */ 346 /* #define R3_MSB 0200 */ /* output high byte */ 347 348 #define R3_J11 (R3_WORD|R3_BYTX) /* JLH: 11 bit JMP and CALL (8051) */ 349 #define R3_J19 (R3_WORD|R3_BYTX|R3_MSB) /* BM: 19 bit JMP and CALL (DS80C390) */ 350 #define R_C24 (R3_WORD|R3_BYT1|R3_MSB) /* 24 bit address (DS80C390) */ 351 #define R_J19_MASK (R3_BYTE|R3_BYTX|R3_MSB) 352 353 #define IS_R_J19(x) (((x) & R_J19_MASK) == R3_J19) 354 #define IS_R_J11(x) (((x) & R_J19_MASK) == R3_J11) 355 #define IS_C24(x) (((x) & R_J19_MASK) == R_C24) 356 357 /* sdld specific */ 358 #define R_BYT3 0x100 /* if R3_BYTE is set, this is a 359 * 3 byte address, of which 360 * the linker must select one byte. 361 */ 362 #define R_HIB 0x200 /* If R3_BYTE & R_BYT3 are set, linker 363 * will select byte 3 of the relocated 364 * 24 bit address. 365 */ 366 367 #define R_BIT 0x400 /* Linker will convert from byte-addressable 368 * space to bit-addressable space. 369 */ 370 371 #define R_ESCAPE_MASK 0xf0 /* Used to escape relocation modes 372 * greater than 0xff in the .rel 373 * file. 374 */ 375 /* end sdld specific */ 376 377 378 /* 379 * ASLINK - Version 4 Definitions 380 */ 381 382 /* 383 * The "A4_" area constants define values used in 384 * generating the assembler area output data. 385 * 386 * Area flags 387 * 388 * 7 6 5 4 3 2 1 0 389 * +-----+-----+-----+-----+-----+-----+-----+-----+ 390 * | BNK | SEG | | PAG | ABS | OVR | WL1 | WL0 | 391 * +-----+-----+-----+-----+-----+-----+-----+-----+ 392 */ 393 394 #define A4_BYTE 0x0000 /* 8 bit */ 395 #define A4_WORD 0x0001 /* 16 bit */ 396 397 #define A4_1BYTE 0x0000 /* 1 Byte Word Length */ 398 #define A4_2BYTE 0x0001 /* 2 Byte Word Length */ 399 #define A4_3BYTE 0x0002 /* 3 Byte Word Length */ 400 #define A4_4BYTE 0x0003 /* 4 Byte Word Length */ 401 #define A4_WLMSK 0x0003 /* Word Length Mask */ 402 403 #define A4_CON 0x0400 /* Concatenating */ 404 #define A4_OVR 0x0404 /* Overlaying */ 405 #define A4_REL 0x0800 /* Relocatable */ 406 #define A4_ABS 0x0808 /* absolute */ 407 #define A4_NOPAG 0x1000 /* Non-Paged */ 408 #define A4_PAG 0x1010 /* Paged */ 409 410 #define A4_CSEG 0x4000 /* CSEG */ 411 #define A4_DSEG 0x4040 /* DSEG */ 412 #define A4_NOBNK 0x8000 /* Non-Banked */ 413 #define A4_BNK 0x8080 /* Banked */ 414 415 #define A4_OUT 0x0100 /* Output Code Flag */ 416 417 /* 418 * The "R4_" relocation constants define values used in 419 * generating the assembler relocation output data for 420 * areas, symbols, and code. 421 * 422 * Note: The PAGE modes, PCR modes, Signed, Unsigned, 423 * and MSB codes are mutually exclusive !!! 424 * 425 * 426 * Relocation flags 427 * 428 * 7 6 5 4 3 2 1 0 429 * +-----+-----+-----+-----+-----+-----+-----+-----+ 430 * | SYM | PCR | PAGn| PAG0| USGN| SGND| BYT1| BYT0| 431 * +-----+-----+-----+-----+-----+-----+-----+-----+ 432 */ 433 434 #define R4_BYTE 0x0000 /* 8 bit */ 435 #define R4_WORD 0x0001 /* 16 bit */ 436 437 #define R4_1BYTE 0x0000 /* 1 Byte */ 438 #define R4_2BYTE 0x0001 /* 2 Byte */ 439 #define R4_3BYTE 0x0002 /* 3 Byte */ 440 #define R4_4BYTE 0x0003 /* 4 Byte */ 441 #define R4_BYTES 0x0003 /* Data Size */ 442 443 #define R4_SGND 0x0004 /* Signed */ 444 #define R4_USGN 0x0008 /* Unsigned */ 445 #define R4_OVRF 0x0008 /* Overflow */ 446 447 #define R4_MBRS 0x0004 /* Merge Bit Range Signed */ 448 /* An alias for Signed */ 449 #define R4_MBRU 0x0008 /* Merge Bit Range Unsigned */ 450 /* An alias for Unsigned */ 451 #define R4_MBRO 0x0008 /* Merge Bit Range Overflow */ 452 /* An alias for Unsigned */ 453 454 #define R4_MSB 0x000C /* MSB */ 455 /* Mutually exclusive with Signed / Unsigned */ 456 457 #define R4_AREA 0x0000 /* Base type */ 458 #define R4_SYM 0x0080 459 460 /* 461 * Paging Modes: 462 */ 463 464 #define R4_NOPAG 0x0000 /* Page Mode */ 465 #define R4_PBITS 0x003C /* Paging Bits */ 466 #define R4_PAGE 0x0030 /* Paged Addressing */ 467 #define R4_PAG0 0x0010 /* Page '0' .setdp */ 468 #define R4_PAGN 0x0020 /* Page 'nnn' .setdp */ 469 #define R4_PAGX 0x0030 /* Page 'x', Extended Relocation Mode */ 470 #define R4_PAGX0 0x0030 /* Page 'x', pc + 0 */ 471 #define R4_PAGX1 0x0034 /* Page 'x', pc + 1 */ 472 #define R4_PAGX2 0x0038 /* Page 'x', pc + 2 */ 473 #define R4_PAGX3 0x003C /* Page 'x', pc + 3 */ 474 475 /* 476 * PCR Modes: 477 */ 478 479 #define R4_PCR 0x0040 /* PC adjust (default) */ 480 #define R4_PCRN 0x0050 /* PC adjust (default) no range check */ 481 482 #define R4_PCR0 0x0054 /* PC adjust (offset = 0) */ 483 #define R4_PCR1 0x0060 /* PC adjust (offset = 1) */ 484 #define R4_PCR2 0x0064 /* PC adjust (offset = 2) */ 485 #define R4_PCR3 0x0068 /* PC adjust (offset = 3) */ 486 #define R4_PCR4 0x006C /* PC adjust (offset = 4) */ 487 488 #define R4_PCR0N 0x0058 /* PC adjust (offset = 0) no range check */ 489 #define R4_PCR1N 0x0070 /* PC adjust (offset = 1) no range check */ 490 #define R4_PCR2N 0x0074 /* PC adjust (offset = 2) no range check */ 491 #define R4_PCR3N 0x0078 /* PC adjust (offset = 3) no range check */ 492 #define R4_PCR4N 0x007C /* PC adjust (offset = 4) no range check */ 493 494 /* 495 * Basic Relocation Modes 496 */ 497 498 #define R4_NORM 0x0000 /* No Bit Positioning */ 499 500 501 /* 502 * The defined type 'a_uint' is used for all address and 503 * unsigned variable value calculations. Its size is 504 * required to be at least 32-bits to allow upto 505 * 32-bit addressing or 32-bit value manipulation. 506 */ 507 typedef unsigned INT32 a_uint; 508 509 /* 510 * The defined type 'v_sint' is used for address and 511 * variable value calculations requiring a sign. 512 * Its size is required to be at least 32-bits to allow 513 * upto 32-bit addressing or 32-bit value manipulation. 514 */ 515 typedef signed INT32 v_sint; 516 517 /* 518 * The structures of head, mode, bank, area, areax, and sym 519 * are created as the REL files are read during the first 520 * pass of the linker. The struct head is created upon 521 * encountering a H directive in the REL file. The 522 * structure contains a link to a link file structure 523 * (struct lfile) which describes the file containing the H 524 * directive, a pointer to an array of merge mode 525 * definition pointers, the number of data/code areas 526 * contained in this header segment, the number of 527 * symbols referenced/defined in this header segment, a pointer 528 * to an array of pointers to areax structures (struct areax) 529 * created as each A directive is read, a pointer to an 530 * array of pointers to symbol structures (struct sym) for 531 * all referenced/defined symbols and a pointer to an array 532 * of pointers to bank structures (struct bank) referenced 533 * by this module. As H directives are read 534 * from the REL files a linked list of head structures is 535 * created by placing a link to the new head structure 536 * in the previous head structure. 537 */ 538 struct head 539 { 540 struct head *h_hp; /* Header link */ 541 struct lfile *h_lfile; /* Associated file */ 542 int h_narea; /* # of areas */ 543 struct areax **a_list; /* Area list */ 544 int h_nsym; /* # of symbols */ 545 struct sym **s_list; /* Symbol list */ 546 int h_nbank; /* # of banks */ 547 struct bank **b_list; /* Bank list */ 548 int h_nmode; /* # of modes */ 549 struct mode **m_list; /* Mode list */ 550 char * m_id; /* Module name */ 551 }; 552 553 /* 554 * The MODE structure contains the specification of one of the 555 * assemblers' relocation modes. Each assembler must specify 556 * at least one relocation mode. The relocation specification 557 * allows arbitrarily defined active bits and bit positions. 558 * The 32 element arrays are indexed from 0 to 31. 559 * Index 0 corresponds to bit 0, ..., and 31 corresponds to bit 31 560 * of a normal integer value. 561 * 562 * The value an array element defines if the normal integer bit is 563 * active (bit <7> is set, 0x80) and what destination bit 564 * (bits <4:0>, 0 - 31) should be loaded with this normal 565 * integer bit. 566 * 567 * The specification for a 32-bit integer: 568 * 569 * char mode_[32] = { 570 * 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 571 * 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 572 * 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 573 * 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F 574 * }; 575 * 576 * 577 * The specification for the 11-bit 8051 addressing mode: 578 * 579 * char mode_[32] = { 580 * 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 581 * 0x8D, 0x8E, 0x8F, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 582 * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 583 * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F 584 * }; 585 * 586 * 587 * m_def is the bit relocation definition array. 588 * m_flag indicates that bit position swapping is required. 589 * m_dbits contains the active bit positions for the output. 590 * m_sbits contains the active bit positions for the input. 591 */ 592 struct mode 593 { 594 char m_def[32]; /* Bit Relocation Definition */ 595 int m_flag; /* Bit Swapping Flag */ 596 a_uint m_dbits; /* Destination Bit Mask */ 597 a_uint m_sbits; /* Source Bit Mask */ 598 }; 599 600 /* 601 * The bank structure contains the parameter values for a 602 * specific program or data bank. The bank structure 603 * is a linked list of banks. The initial default bank 604 * is unnamed and is defined in lkdata.c, the next bank structure 605 * will be linked to this structure through the structure 606 * element 'struct bank *b_bp'. The structure contains the 607 * bank name, the bank base address (default = 0), the bank size 608 * (default = 0, whole addressing space), the bank mapping, 609 * and the file name suffix. (default is none) These optional 610 * parameters are from the .bank assembler directive. 611 * The bank structure also contains the bank data output 612 * file specification, file handle pointer and the 613 * bank first output flag. 614 */ 615 struct bank 616 { 617 struct bank *b_bp; /* Bank link */ 618 char * b_id; /* Bank Name */ 619 char * b_fsfx; /* Bank File Suffix / File Specification */ 620 a_uint b_base; /* Bank base address */ 621 a_uint b_size; /* Bank size */ 622 a_uint b_map; /* Bank mapping */ 623 int b_flag; /* Bank flags */ 624 char * b_fspec; /* Bank File Specification */ 625 FILE * b_ofp; /* Bank File Handle */ 626 char * b_ofspec; /* Bank Output File Specification */ 627 int b_oflag; /* Bank has output flag */ 628 int b_rtaflg; /* Bank First Output flag */ 629 }; 630 631 #define B_BASE 0001 /* 'base' address specified */ 632 #define B_SIZE 0002 /* 'size' of bank specified */ 633 #define B_FSFX 0004 /* File suffix specified */ 634 #define B_MAP 0010 /* Mapped Bank Flag */ 635 636 /* 637 * A structure area is created for each 'unique' data/code 638 * area definition found as the REL files are read. The 639 * struct area contains the name of the area, a flag byte 640 * which contains the area attributes (REL/CON/OVR/ABS), 641 * the area base address set flag byte (-b option), and the 642 * area base address and total size which will be filled 643 * in at the end of the first pass through the REL files. 644 * The area structure also contains a link to the bank 645 * this area is a part of and a data output file handle 646 * pointer which is loaded from from the bank structure. 647 * As A directives are read from the REL files a linked 648 * list of unique area structures is created by placing a 649 * link to the new area structure in the previous area structure. 650 */ 651 struct area 652 { 653 struct area *a_ap; /* Area link */ 654 struct areax *a_axp; /* Area extension link */ 655 struct bank *a_bp; /* Bank link */ 656 FILE * a_ofp; /* Area File Handle */ 657 a_uint a_addr; /* Beginning address of area */ 658 a_uint a_size; /* Total size of the area */ 659 int a_bset; /* Area base address set */ 660 /* sdld specific */ 661 a_uint a_unaloc; /* Total number of unallocated bytes, for error reporting */ 662 /* end sdld specific */ 663 int a_flag; /* Flags */ 664 char * a_id; /* Name */ 665 /* sdld specific */ 666 char *a_image; /* Something for hc08/lkelf */ 667 char *a_used; /* Something for hc08/lkelf */ 668 a_uint a_imagesize; /* Something for hc08/lkelf */ 669 /* end sdld specific */ 670 }; 671 672 /* 673 * An areax structure is created for every A directive found 674 * while reading the REL files. The struct areax contains a 675 * link to the 'unique' area structure referenced by the A 676 * directive and to the head structure this area segment is 677 * a part of. The size of this area segment as read from the 678 * A directive is placed in the areax structure. The beginning 679 * address of this segment will be filled in at the end of the 680 * first pass through the REL files. As A directives are read 681 * from the REL files a linked list of areax structures is 682 * created for each unique area. The final areax linked 683 * list has at its head the 'unique' area structure linked 684 * to the linked areax structures (one areax structure for 685 * each A directive for this area). 686 */ 687 struct areax 688 { 689 struct areax *a_axp; /* Area extension link */ 690 struct area *a_bap; /* Base area link */ 691 struct head *a_bhp; /* Base header link */ 692 a_uint a_addr; /* Beginning address of section */ 693 a_uint a_size; /* Size of the area in section */ 694 }; 695 696 /* 697 * A sym structure is created for every unique symbol 698 * referenced/defined while reading the REL files. The 699 * struct sym contains the symbol's name, a flag value 700 * (not used in this linker), a symbol type denoting 701 * referenced/defined, and an address which is loaded 702 * with the relative address within the area in which 703 * the symbol was defined. The sym structure also 704 * contains a link to the area where the symbol was defined. 705 * The sym structures are linked into linked lists using 706 * the symbol link element. 707 */ 708 struct sym 709 { 710 struct sym *s_sp; /* Symbol link */ 711 struct areax *s_axp; /* Symbol area link */ 712 char s_type; /* Symbol subtype */ 713 char s_flag; /* Flag byte */ 714 a_uint s_addr; /* Address */ 715 char *s_id; /* Name (JLH) */ 716 char *m_id; /* Module symbol define in */ 717 }; 718 719 /* 720 * The structure lfile contains a pointer to a 721 * file specification string, an index which points 722 * to the file name (past the 'path'), the file type, 723 * an object output flag, and a link to the next 724 * lfile structure. 725 */ 726 struct lfile 727 { 728 struct lfile *f_flp; /* lfile link */ 729 int f_type; /* File type */ 730 char *f_idp; /* Pointer to file spec */ 731 int f_idx; /* Index to file name */ 732 int f_obj; /* Object output flag */ 733 }; 734 735 /* 736 * The struct base contains a pointer to a 737 * base definition string and a link to the next 738 * base structure. 739 */ 740 struct base 741 { 742 struct base *b_base; /* Base link */ 743 char *b_strp; /* String pointer */ 744 }; 745 746 /* 747 * The struct globl contains a pointer to a 748 * global definition string and a link to the next 749 * global structure. 750 */ 751 struct globl 752 { 753 struct globl *g_globl; /* Global link */ 754 char *g_strp; /* String pointer */ 755 }; 756 757 /* 758 * A structure sdp is created for each 'unique' paged 759 * area definition found as the REL files are read. 760 * As P directives are read from the REL files a linked 761 * list of unique sdp structures is created by placing a 762 * link to the new sdp structure in the previous area structure. 763 */ 764 struct sdp 765 { 766 struct area *s_area; /* Paged Area link */ 767 struct areax *s_areax; /* Paged Area Extension Link */ 768 a_uint s_addr; /* Page address offset */ 769 }; 770 771 /* 772 * The structure rerr is loaded with the information 773 * required to report an error during the linking 774 * process. The structure contains an index value 775 * which selects the areax structure from the header 776 * areax structure list, a mode value which selects 777 * symbol or area relocation, the base address in the 778 * area section, an area/symbol list index value, and 779 * an area/symbol offset value. 780 */ 781 struct rerr 782 { 783 int aindex; /* Linking area */ 784 int mode; /* Relocation mode */ 785 a_uint rtbase; /* Base address in section */ 786 int rindex; /* Area/Symbol reloaction index */ 787 a_uint rval; /* Area/Symbol offset value */ 788 }; 789 790 /* 791 * The structure lbpath is created for each library 792 * path specification input by the -k option. The 793 * lbpath structures are linked into a list using 794 * the next link element. 795 */ 796 struct lbpath { 797 struct lbpath *next; 798 char *path; 799 }; 800 801 /* 802 * The structure lbname is created for all combinations of the 803 * library path specifications (input by the -k option) and the 804 * library file specifications (input by the -l option) that 805 * lead to an existing file. The element path points to 806 * the path string, element libfil points to the library 807 * file string, and the element libspc is the concatenation 808 * of the valid path and libfil strings. 809 * 810 * The lbpath structures are linked into a list 811 * using the next link element. 812 * 813 * Each library file contains a list of object files 814 * that are contained in the particular library. e.g.: 815 * 816 * \iolib\termio 817 * \inilib\termio 818 * 819 * Only one specification per line is allowed. 820 */ 821 struct lbname { 822 struct lbname *next; 823 char *path; 824 char *libfil; 825 char *libspc; 826 int f_obj; 827 }; 828 829 /* 830 * The function fndsym() searches through all combinations of the 831 * library path specifications (input by the -k option) and the 832 * library file specifications (input by the -l option) that 833 * lead to an existing file for a symbol definition. 834 * 835 * The structure lbfile is created for the first library 836 * object file which contains the definition for the 837 * specified undefined symbol. 838 * 839 * The element libspc points to the library file path specification 840 * and element relfil points to the object file specification string. 841 * The element filspc is the complete path/file specification for 842 * the library file to be imported into the linker. The f_obj 843 * flag specifies if the object code from this file is 844 * to be output by the linker. The file specification 845 * may be formed in one of two ways: 846 * 847 * (1) If the library file contained an absolute 848 * path/file specification then this becomes filspc. 849 * (i.e. C:\...) 850 * 851 * (2) If the library file contains a relative path/file 852 * specification then the concatenation of the path 853 * and this file specification becomes filspc. 854 * (i.e. \...) 855 * 856 * The lbpath structures are linked into a list 857 * using the next link element. 858 */ 859 struct lbfile { 860 struct lbfile *next; 861 char *libspc; 862 char *relfil; 863 char *filspc; 864 int f_obj; 865 /* sdld specific */ 866 long offset; 867 unsigned int type; 868 /* end sdld specific */ 869 }; 870 871 /* 872 * External Definitions for all Global Variables 873 */ 874 875 extern char *_abs_; /* = { ". .ABS." }; 876 */ 877 extern int lkerr; /* ASLink error flag 878 */ 879 extern char *ip; /* pointer into the REL file 880 * text line in ib[] 881 */ 882 extern char ib[NINPUT]; /* REL file text line 883 */ 884 extern char *rp; /* pointer into the LST file 885 * text line in rb[] 886 */ 887 extern char rb[NINPUT]; /* LST file text line being 888 * address relocated 889 */ 890 extern char ctype[]; /* array of character types, one per 891 * ASCII/OEM character 892 */ 893 894 /* 895 * Character Type Definitions 896 */ 897 #define SPACE '\000' 898 #define ETC '\000' 899 #define LETTER '\001' 900 #define DIGIT '\002' 901 #define BINOP '\004' 902 #define RAD2 '\010' 903 #define RAD8 '\020' 904 #define RAD10 '\040' 905 #define RAD16 '\100' 906 #define ILL '\200' 907 908 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2 909 #define DGT8 DIGIT|RAD16|RAD10|RAD8 910 #define DGT10 DIGIT|RAD16|RAD10 911 #define LTR16 LETTER|RAD16 912 913 extern char afspec[]; /* The filespec created by afile() 914 */ 915 extern char ccase[]; /* an array of characters which 916 * perform the case translation function 917 */ 918 919 extern struct lfile *filep; /* The pointers (lfile *) filep, 920 * (lfile *) cfp, and (FILE *) sfp 921 * are used in conjunction with 922 * the routine nxtline() to read 923 * aslink commands from 924 * (1) the standard input or 925 * (2) or a command file 926 * and to read the REL files 927 * sequentially as defined by the 928 * aslink input commands. 929 * 930 * The pointer *filep points to the 931 * beginning of a linked list of 932 * lfile structures. 933 */ 934 extern struct lfile *cfp; /* The pointer *cfp points to the 935 * current lfile structure 936 */ 937 extern struct lfile *startp;/* aslink startup file structure 938 */ 939 extern struct lfile *linkp; /* pointer to first lfile structure 940 * containing an input REL file 941 * specification 942 */ 943 extern struct lfile *lfp; /* pointer to current lfile structure 944 * being processed by parse() 945 */ 946 extern struct head *headp; /* The pointer to the first 947 * head structure of a linked list 948 */ 949 extern struct head *hp; /* Pointer to the current 950 * head structure 951 */ 952 extern struct bank *bankp; /* The pointer to the first 953 * bank structure of a linked list 954 */ 955 extern struct bank *bp; /* Pointer to the current 956 * bank structure 957 */ 958 extern struct area *areap; /* The pointer to the first 959 * area structure of a linked list 960 */ 961 extern struct area *ap; /* Pointer to the current 962 * area structure 963 */ 964 extern struct areax *axp; /* Pointer to the current 965 * areax structure 966 */ 967 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH 968 * linked symbol lists 969 */ 970 extern struct base *basep; /* The pointer to the first 971 * base structure 972 */ 973 extern struct base *bsp; /* Pointer to the current 974 * base structure 975 */ 976 extern struct globl *globlp;/* The pointer to the first 977 * globl structure 978 */ 979 extern struct globl *gsp; /* Pointer to the current 980 * globl structure 981 */ 982 extern struct sdp sdp; /* Base Paged structure 983 */ 984 extern struct rerr rerr; /* Structure containing the 985 * linker error information 986 */ 987 extern FILE *nbofp; /* Non Banked Linker Output 988 * File Handle 989 */ 990 extern FILE *ofp; /* Linker Output file handle 991 */ 992 993 #if NOICE 994 extern FILE *jfp; /* NoICE output file handle 995 */ 996 #endif 997 998 extern FILE *mfp; /* Map output file handle 999 */ 1000 extern FILE *rfp; /* File handle for output 1001 * address relocated ASxxxx 1002 * listing file 1003 */ 1004 extern FILE *sfp; /* The file handle sfp points to the 1005 * currently open file 1006 */ 1007 extern FILE *tfp; /* File handle for input 1008 * ASxxxx listing file 1009 */ 1010 1011 #if SDCDB 1012 extern FILE *yfp; /* SDCDB output file handle 1013 */ 1014 #endif 1015 1016 extern int oflag; /* Output file type flag 1017 */ 1018 extern int objflg; /* Linked file/library object output flag 1019 */ 1020 1021 #if NOICE 1022 extern int jflag; /* -j, enable NoICE Debug output 1023 */ 1024 #endif 1025 1026 extern int mflag; /* Map output flag 1027 */ 1028 extern int xflag; /* Map file radix type flag 1029 */ 1030 1031 #if SDCDB 1032 extern int yflag; /* -y, enable SDCC Debug output 1033 */ 1034 #endif 1035 1036 extern int pflag; /* print linker command file flag 1037 */ 1038 extern int uflag; /* Listing relocation flag 1039 */ 1040 extern int wflag; /* Enable wide format listing 1041 */ 1042 extern int zflag; /* Disable symbol case sensitivity 1043 */ 1044 extern int radix; /* current number conversion radix: 1045 * 2 (binary), 8 (octal), 10 (decimal), 1046 * 16 (hexadecimal) 1047 */ 1048 extern int line; /* current line number 1049 */ 1050 extern int page; /* current page number 1051 */ 1052 extern int lop; /* current line number on page 1053 */ 1054 extern int pass; /* linker pass number 1055 */ 1056 extern a_uint pc; /* current relocation address 1057 */ 1058 extern int pcb; /* current bytes per pc word 1059 */ 1060 extern int rtcnt; /* count of elements in the 1061 * rtval[] and rtflg[] arrays 1062 */ 1063 extern a_uint rtval[]; /* data associated with relocation 1064 */ 1065 extern int rtflg[]; /* indicates if rtval[] value is 1066 * to be sent to the output file. 1067 */ 1068 extern int rterr[]; /* indicates if rtval[] value should 1069 * be flagged as a relocation error. 1070 */ 1071 extern char rtbuf[]; /* S19/IHX output buffer 1072 */ 1073 extern struct bank *rtabnk; /* rtbuf[] processing 1074 */ 1075 extern int rtaflg; /* rtbuf[] processing 1076 */ 1077 extern a_uint rtadr0; /* 1078 */ 1079 extern a_uint rtadr1; /* 1080 */ 1081 extern a_uint rtadr2; /* 1082 */ 1083 extern int obj_flag; /* Linked file/library object output flag 1084 */ 1085 extern int a_bytes; /* REL file T Line address length 1086 */ 1087 extern int hilo; /* REL file byte ordering 1088 */ 1089 extern a_uint a_mask; /* Address Mask 1090 */ 1091 extern a_uint s_mask; /* Sign Mask 1092 */ 1093 extern a_uint v_mask; /* Value Mask 1094 */ 1095 extern int gline; /* LST file relocation active 1096 * for current line 1097 */ 1098 extern int gcntr; /* LST file relocation active 1099 * counter 1100 */ 1101 extern struct lbpath *lbphead; /* pointer to the first 1102 * library path structure 1103 */ 1104 extern struct lbname *lbnhead; /* pointer to the first 1105 * library name structure 1106 */ 1107 extern struct lbfile *lbfhead; /* pointer to the first 1108 * library file structure 1109 */ 1110 /* sdld specific */ 1111 extern int sflag; /* JCF: Memory usage output flag 1112 */ 1113 extern int packflag; /* Pack data memory flag 1114 */ 1115 extern int stacksize; /* Stack size 1116 */ 1117 extern int rflag; /* Extended linear address record flag. 1118 */ 1119 extern a_uint iram_size; /* internal ram size 1120 */ 1121 extern long xram_size; /* external ram size 1122 */ 1123 extern long code_size; /* code size 1124 */ 1125 extern char *sdld_output; /* output file name, --output argument 1126 */ 1127 extern char *optsdcc; 1128 extern char *optsdcc_module; 1129 /* sdld 8015 specific */ 1130 extern char idatamap[256]; /* space is unused 1131 */ 1132 /* end sdld 8051 specific */ 1133 /* end sdld specific */ 1134 1135 1136 1137 /* C Library function definitions */ 1138 /* for reference only 1139 extern VOID exit(); 1140 extern int fclose(); 1141 extern char * fgets(); 1142 extern FILE * fopen(); 1143 extern int fprintf(); 1144 extern VOID free(); 1145 extern VOID * malloc(); 1146 extern char putc(); 1147 extern char * sprintf(); 1148 extern char * strcpy(); 1149 extern int strlen(); 1150 extern char * strncpy(); 1151 extern char * strrchr(); 1152 */ 1153 1154 /* Program function definitions */ 1155 1156 #ifdef OTHERSYSTEM 1157 1158 /* lkmain.c */ 1159 extern FILE * afile(char *fn, char *ft, int wf); 1160 extern VOID bassav(void); 1161 extern int fndidx(char *str); 1162 extern int fndext(char *str); 1163 extern VOID gblsav(void); 1164 extern int intsiz(void); 1165 extern VOID iramsav(void); 1166 extern VOID xramsav(void); 1167 extern VOID codesav(void); 1168 extern VOID iramcheck(void); 1169 extern VOID link_main(void); 1170 extern VOID lkexit(int i); 1171 extern int main(int argc, char *argv[]); 1172 extern VOID map(void); 1173 extern VOID sym(void); 1174 extern int parse(void); 1175 extern VOID doparse(void); 1176 extern VOID setarea(void); 1177 extern VOID setgbl(void); 1178 extern VOID usage(int n); 1179 extern VOID copyfile (FILE *dest, FILE *src); 1180 1181 /* lklex.c */ 1182 extern VOID chopcrlf(char *str); 1183 extern char endline(void); 1184 extern int get(void); 1185 extern VOID getfid(char *str, int c); 1186 extern VOID getid(char *id, int c); 1187 extern VOID getSid(char *id); 1188 extern int getmap(int d); 1189 extern int getnb(void); 1190 extern int more(void); 1191 extern int nxtline(void); 1192 extern VOID skip(int c); 1193 extern VOID unget(int c); 1194 1195 /* lkarea.c */ 1196 extern VOID lkparea(char *id); 1197 extern VOID lnkarea(void); 1198 extern VOID lnkarea2(void); 1199 extern VOID newarea(void); 1200 1201 /* lkbank.c */ 1202 extern VOID chkbank(FILE *fp); 1203 extern VOID lkfclose(void); 1204 extern VOID lkfopen(void); 1205 extern VOID lkpbank(char * id); 1206 extern VOID newbank(void); 1207 extern VOID setbank(void); 1208 1209 /* lkhead.c */ 1210 extern VOID module(void); 1211 extern VOID newhead(void); 1212 extern VOID newmode(void); 1213 1214 /* lksym.c */ 1215 extern int hash(char *p, int cflag); 1216 extern struct sym * lkpsym(char *id, int f); 1217 extern char * new(unsigned int n); 1218 extern struct sym * newsym(void); 1219 extern char * strsto(char *str); 1220 extern VOID symdef(FILE *fp); 1221 extern int symeq(char *p1, char *p2, int cflag); 1222 extern VOID syminit(void); 1223 extern VOID symmod(FILE *fp, struct sym *tsp); 1224 extern a_uint symval(struct sym *tsp); 1225 1226 /* lkeval.c */ 1227 extern int digit(int c, int r); 1228 extern a_uint eval(void); 1229 extern a_uint expr(int n); 1230 extern int oprio(int c); 1231 extern a_uint term(void); 1232 1233 /* lklist.c */ 1234 extern int dgt(int rdx, char *str, int n); 1235 extern VOID newpag(FILE *fp); 1236 extern VOID slew(struct area *xp, struct bank *yp); 1237 extern VOID lstarea(struct area *xp, struct bank *yp); 1238 extern VOID lkulist(int i); 1239 extern VOID lkalist(a_uint cpc); 1240 extern VOID lkglist(a_uint cpc, int v, int err); 1241 1242 /* lknoice.c */ 1243 extern VOID NoICEfopen(void); 1244 extern VOID NoICEmagic(void); 1245 extern VOID DefineNoICE(char *name, a_uint value, struct bank *yp); 1246 extern VOID DefineGlobal(char *name, a_uint value, struct bank *yp); 1247 extern VOID DefineScoped(char *name, a_uint value, struct bank *yp); 1248 extern VOID DefineFile(char *name, a_uint value, struct bank *yp); 1249 extern VOID DefineFunction(char *name, a_uint value, struct bank *yp); 1250 extern VOID DefineStaticFunction(char *name, a_uint value, struct bank *yp); 1251 extern VOID DefineEndFunction(a_uint value, struct bank *yp); 1252 extern VOID DefineLine(char *lineString, a_uint value, struct bank *yp); 1253 extern VOID PagedAddress(a_uint value, struct bank *yp); 1254 1255 /* lksdcdb.c */ 1256 extern VOID SDCDBfopen(void); 1257 extern VOID SDCDBcopy(char * str); 1258 extern VOID DefineSDCDB(char *name, a_uint value); 1259 1260 /* lkrloc.c */ 1261 extern a_uint adb_1b(a_uint v, int i); 1262 extern a_uint adb_2b(a_uint v, int i); 1263 extern a_uint adb_3b(a_uint v, int i); 1264 extern a_uint adb_4b(a_uint v, int i); 1265 extern a_uint adb_xb(a_uint v, int i); 1266 extern a_uint evword(void); 1267 extern VOID prntval(FILE *fptr, a_uint v); 1268 extern VOID reloc(int c); 1269 1270 /* lkrloc3.c */ 1271 extern a_uint adb_bit(a_uint v, int i); 1272 extern a_uint adb_24_bit(a_uint v, int i); 1273 extern a_uint adb_24_hi(a_uint v, int i); 1274 extern a_uint adb_24_mid(a_uint v, int i); 1275 extern a_uint adb_24_lo(a_uint v, int i); 1276 extern a_uint adb_hi(a_uint v, int i); 1277 extern a_uint adb_lo(a_uint v, int i); 1278 extern char * errmsg3[]; 1279 extern VOID errdmp3(FILE *fptr, char *str); 1280 extern VOID erpdmp3(FILE *fptr, char *str); 1281 extern VOID rele3(void); 1282 extern VOID reloc3(int c); 1283 extern VOID relt3(void); 1284 extern VOID relr3(void); 1285 extern VOID relp3(void); 1286 extern VOID relerr3(char *str); 1287 extern VOID relerp3(char *str); 1288 extern int vpdkinst(int inst, int addr, int ver); 1289 1290 /* lklibr.c */ 1291 extern int addfile(char *path, char *libfil); 1292 extern VOID addlib(void); 1293 extern VOID addpath(void); 1294 extern int fndsym(char *name); 1295 extern VOID library(void); 1296 extern VOID loadfile(char *filspc); 1297 extern VOID search(void); 1298 1299 /* lkout.c */ 1300 extern VOID lkout(int i); 1301 extern VOID lkflush(void); 1302 extern VOID ixx(int i); 1303 extern VOID iflush(void); 1304 extern VOID dbx(int i); 1305 extern VOID dflush(void); 1306 1307 /* lks19.c */ 1308 extern VOID s19(int i); 1309 extern VOID sflush(void); 1310 1311 /* EEP: lkelf.c */ 1312 extern VOID elf(); 1313 1314 /* JCF: lkmem.c */ 1315 extern int summary(struct area * xp); 1316 extern int summary2(struct area * xp); 1317 1318 /* JCF: lkaomf51.c */ 1319 extern void SaveLinkedFilePath(char * filepath); 1320 extern void CreateAOMF51(void); 1321 1322 /* lkgb.h */ 1323 VOID gb(int in); 1324 VOID gg(int in); 1325 1326 /* strcmpi.h */ 1327 extern int as_strcmpi(const char *s1, const char *s2); 1328 extern int as_strncmpi(const char *s1, const char *s2, size_t n); 1329 1330 #else 1331 1332 #endif 1333