1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * Map file parsing. 33 */ 34 #include <fcntl.h> 35 #include <string.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <sys/stat.h> 39 #include <errno.h> 40 #include <limits.h> 41 #include <dirent.h> 42 #include <ctype.h> 43 #include <elfcap.h> 44 #include <debug.h> 45 #include "msg.h" 46 #include "_libld.h" 47 48 #if defined(_ELF64) 49 #define STRTOADDR strtoull 50 #define XWORD_MAX ULLONG_MAX 51 #else /* Elf32 */ 52 #define STRTOADDR strtoul 53 #define XWORD_MAX UINT_MAX 54 #endif /* _ELF64 */ 55 56 typedef enum { 57 TK_STRING, TK_COLON, TK_SEMICOLON, TK_EQUAL, 58 TK_ATSIGN, TK_DASH, TK_LEFTBKT, TK_RIGHTBKT, 59 TK_PIPE, TK_EOF 60 } Token; /* Possible return values from gettoken. */ 61 62 63 static char *Mapspace; /* Malloc space holding map file. */ 64 static ulong_t Line_num; /* Current map file line number. */ 65 static char *Start_tok; /* First character of current token. */ 66 static char *nextchr; /* Next char in mapfile to examine. */ 67 68 /* 69 * Convert a string to lowercase. 70 */ 71 static void 72 lowercase(char *str) 73 { 74 while (*str = tolower(*str)) 75 str++; 76 } 77 78 /* 79 * Get a token from the mapfile. 80 */ 81 static Token 82 gettoken(Ofl_desc *ofl, const char *mapfile) 83 { 84 static char oldchr = '\0'; /* Char at end of current token. */ 85 char *end; /* End of the current token. */ 86 87 /* Cycle through the characters looking for tokens. */ 88 for (;;) { 89 if (oldchr != '\0') { 90 *nextchr = oldchr; 91 oldchr = '\0'; 92 } 93 if (!isascii(*nextchr) || 94 (!isprint(*nextchr) && !isspace(*nextchr) && 95 (*nextchr != '\0'))) { 96 eprintf(ofl->ofl_lml, ERR_FATAL, 97 MSG_INTL(MSG_MAP_ILLCHAR), mapfile, 98 EC_XWORD(Line_num), *((uchar_t *)nextchr)); 99 /* LINTED */ 100 return ((Token)S_ERROR); 101 } 102 switch (*nextchr) { 103 case '\0': /* End of file. */ 104 return (TK_EOF); 105 case ' ': /* White space. */ 106 case '\t': 107 nextchr++; 108 break; 109 case '\n': /* White space too, but bump line number. */ 110 nextchr++; 111 Line_num++; 112 break; 113 case '#': /* Comment. */ 114 while (*nextchr != '\n' && *nextchr != '\0') 115 nextchr++; 116 break; 117 case ':': 118 nextchr++; 119 return (TK_COLON); 120 case ';': 121 nextchr++; 122 return (TK_SEMICOLON); 123 case '=': 124 nextchr++; 125 return (TK_EQUAL); 126 case '@': 127 nextchr++; 128 return (TK_ATSIGN); 129 case '-': 130 nextchr++; 131 return (TK_DASH); 132 case '|': 133 nextchr++; 134 return (TK_PIPE); 135 case '{': 136 nextchr++; 137 return (TK_LEFTBKT); 138 case '}': 139 nextchr++; 140 return (TK_RIGHTBKT); 141 case '"': 142 Start_tok = ++nextchr; 143 if (((end = strpbrk(nextchr, 144 MSG_ORIG(MSG_MAP_TOK_1))) == NULL) || 145 (*end != '"')) { 146 eprintf(ofl->ofl_lml, ERR_FATAL, 147 MSG_INTL(MSG_MAP_NOTERM), mapfile, 148 EC_XWORD(Line_num)); 149 /* LINTED */ 150 return ((Token)S_ERROR); 151 } 152 *end = '\0'; 153 nextchr = end + 1; 154 return (TK_STRING); 155 default: /* string. */ 156 Start_tok = nextchr; /* CSTYLED */ 157 end = strpbrk(nextchr, MSG_ORIG(MSG_MAP_TOK_2)); 158 if (end == NULL) 159 nextchr = Start_tok + strlen(Start_tok); 160 else { 161 nextchr = end; 162 oldchr = *nextchr; 163 *nextchr = '\0'; 164 } 165 return (TK_STRING); 166 } 167 } 168 } 169 170 /* 171 * Process a hardware/software capabilities segment declaration definition. 172 * hwcap_1 = val,... [ OVERRIDE ] 173 * sfcap_1 = val,... [ OVERRIDE ] 174 * 175 * The values can be defined as a list of machine specify tokens, or numerics. 176 * Tokens are representations of the sys/auxv_$MACH.h capabilities, for example: 177 * 178 * #define AV_386_FPU 0x0001 is represented as FPU 179 * #define AV_386_TSC 0x0002 " " " " TSC 180 * 181 * Or, the above two capabilities could be represented as V0x3. Note, the 182 * OVERRIDE flag is used to insure that only those values provided via this 183 * mapfile entry are recorded in the final image, ie. this overrides any 184 * hardware capabilities that may be defined in the objects read as part of this 185 * link-edit. Specifying: 186 * 187 * V0x0 OVERRIDE 188 * 189 * effectively removes any capabilities information from the final image. 190 */ 191 static uintptr_t 192 map_cap(const char *mapfile, Word type, Ofl_desc *ofl) 193 { 194 Token tok; /* Current token. */ 195 Xword number; 196 int used = 0; 197 198 while ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 199 /* LINTED */ 200 if (tok == (Token)S_ERROR) 201 return (S_ERROR); 202 if (tok == TK_EOF) { 203 eprintf(ofl->ofl_lml, ERR_FATAL, 204 MSG_INTL(MSG_MAP_PREMEOF), mapfile, 205 EC_XWORD(Line_num)); 206 return (S_ERROR); 207 } 208 if (tok != TK_STRING) { 209 eprintf(ofl->ofl_lml, ERR_FATAL, 210 MSG_INTL(MSG_MAP_EXPSEGATT), mapfile, 211 EC_XWORD(Line_num)); 212 return (S_ERROR); 213 } 214 215 lowercase(Start_tok); 216 217 /* 218 * First, determine if the token represents the reserved 219 * OVERRIDE keyword. 220 */ 221 if (strncmp(Start_tok, MSG_ORIG(MSG_MAP_OVERRIDE), 222 MSG_MAP_OVERRIDE_SIZE) == 0) { 223 if (type == CA_SUNW_HW_1) 224 ofl->ofl_flags1 |= FLG_OF1_OVHWCAP; 225 else 226 ofl->ofl_flags1 |= FLG_OF1_OVSFCAP; 227 used++; 228 continue; 229 } 230 231 /* 232 * Next, determine if the token represents a machine specific 233 * hardware capability, or a generic software capability. 234 */ 235 if (type == CA_SUNW_HW_1) { 236 if ((number = (Xword)hwcap_1_str2val(Start_tok, 237 M_MACH)) != 0) { 238 ofl->ofl_hwcap_1 |= number; 239 used++; 240 continue; 241 } 242 } else { 243 if ((number = (Xword)sfcap_1_str2val(Start_tok, 244 M_MACH)) != 0) { 245 ofl->ofl_sfcap_1 |= number; 246 used++; 247 continue; 248 } 249 } 250 251 /* 252 * Next, determine if the token represents a numeric value. 253 */ 254 if (Start_tok[0] == 'v') { 255 char *end_tok; 256 257 errno = 0; 258 number = (Xword)strtoul(&Start_tok[1], &end_tok, 0); 259 if (errno) { 260 int err = errno; 261 eprintf(ofl->ofl_lml, ERR_FATAL, 262 MSG_INTL(MSG_MAP_BADCAPVAL), 263 mapfile, EC_XWORD(Line_num), Start_tok, 264 strerror(err)); 265 return (S_ERROR); 266 } 267 if (end_tok != strchr(Start_tok, '\0')) { 268 eprintf(ofl->ofl_lml, ERR_FATAL, 269 MSG_INTL(MSG_MAP_BADCAPVAL), mapfile, 270 EC_XWORD(Line_num), Start_tok, 271 MSG_INTL(MSG_MAP_NOBADFRM)); 272 return (S_ERROR); 273 } 274 275 if (type == CA_SUNW_HW_1) 276 ofl->ofl_hwcap_1 |= number; 277 else 278 ofl->ofl_sfcap_1 |= number; 279 used++; 280 continue; 281 } 282 283 /* 284 * We have an unknown token. 285 */ 286 used++; 287 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_MAP_UNKCAPATTR), 288 mapfile, EC_XWORD(Line_num), Start_tok); 289 return (S_ERROR); 290 } 291 292 /* 293 * Catch any empty declarations, and indicate any software capabilities 294 * have been initialized if necessary. 295 */ 296 if (used == 0) { 297 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_MAP_EMPTYCAP), 298 mapfile, EC_XWORD(Line_num)); 299 } else if (type == CA_SUNW_SF_1) { 300 Lword badsf1; 301 302 /* 303 * Note, hardware capabilities, beyond the tokens that are 304 * presently known, can be accepted using the V0xXXX notation, 305 * and as these simply get or'd into the output image, we allow 306 * any values to be supplied. Software capability tokens 307 * however, have an algorithm of acceptance and update (see 308 * sf1_cap() in files.c). Therefore only allow software 309 * capabilities that are known. 310 */ 311 if ((badsf1 = (ofl->ofl_sfcap_1 & ~SF1_SUNW_MASK)) != 0) { 312 eprintf(ofl->ofl_lml, ERR_WARNING, 313 MSG_INTL(MSG_MAP_BADSF1), mapfile, 314 EC_XWORD(Line_num), EC_LWORD(badsf1)); 315 ofl->ofl_sfcap_1 &= SF1_SUNW_MASK; 316 } 317 if (ofl->ofl_sfcap_1 == SF1_SUNW_FPUSED) { 318 eprintf(ofl->ofl_lml, ERR_WARNING, 319 MSG_INTL(MSG_FIL_BADSF1), mapfile, 320 EC_XWORD(Line_num), EC_LWORD(SF1_SUNW_FPUSED)); 321 ofl->ofl_sfcap_1 = 0; 322 } 323 } 324 return (1); 325 } 326 327 /* 328 * Process a mapfile segment declaration definition. 329 * segment_name = segment_attribute; 330 * segment_attribute : segment_type segment_flags virtual_addr 331 * physical_addr length alignment 332 */ 333 static uintptr_t 334 map_equal(const char *mapfile, Sg_desc *sgp, Ofl_desc *ofl) 335 { 336 Token tok; /* Current token. */ 337 Boolean b_type = FALSE; /* True if seg types found. */ 338 Boolean b_flags = FALSE; /* True if seg flags found. */ 339 Boolean b_len = FALSE; /* True if seg length found. */ 340 Boolean b_round = FALSE; /* True if seg rounding found. */ 341 Boolean b_vaddr = FALSE; /* True if seg virtual addr found. */ 342 Boolean b_paddr = FALSE; /* True if seg physical addr found. */ 343 Boolean b_align = FALSE; /* True if seg alignment found. */ 344 345 while ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 346 /* LINTED */ 347 if (tok == (Token)S_ERROR) 348 return (S_ERROR); 349 if (tok == TK_EOF) { 350 eprintf(ofl->ofl_lml, ERR_FATAL, 351 MSG_INTL(MSG_MAP_PREMEOF), mapfile, 352 EC_XWORD(Line_num)); 353 return (S_ERROR); 354 } 355 if (tok != TK_STRING) { 356 eprintf(ofl->ofl_lml, ERR_FATAL, 357 MSG_INTL(MSG_MAP_EXPSEGATT), mapfile, 358 EC_XWORD(Line_num)); 359 return (S_ERROR); 360 } 361 362 lowercase(Start_tok); 363 364 /* 365 * Segment type. Presently there can only be multiple 366 * PT_LOAD and PT_NOTE segments, other segment types are 367 * only defined in seg_desc[]. 368 */ 369 if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_LOAD)) == 0) { 370 if (b_type) { 371 eprintf(ofl->ofl_lml, ERR_FATAL, 372 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 373 EC_XWORD(Line_num), 374 MSG_INTL(MSG_MAP_SEGTYP)); 375 return (S_ERROR); 376 } 377 if ((sgp->sg_flags & FLG_SG_TYPE) && 378 (sgp->sg_phdr.p_type != PT_LOAD)) 379 eprintf(ofl->ofl_lml, ERR_WARNING, 380 MSG_INTL(MSG_MAP_REDEFATT), mapfile, 381 EC_XWORD(Line_num), 382 MSG_INTL(MSG_MAP_SEGTYP), sgp->sg_name); 383 sgp->sg_phdr.p_type = PT_LOAD; 384 sgp->sg_flags |= FLG_SG_TYPE; 385 b_type = TRUE; 386 } else if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_STACK)) == 0) { 387 if (b_type) { 388 eprintf(ofl->ofl_lml, ERR_FATAL, 389 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 390 EC_XWORD(Line_num), 391 MSG_INTL(MSG_MAP_SEGTYP)); 392 return (S_ERROR); 393 } 394 sgp->sg_phdr.p_type = PT_SUNWSTACK; 395 sgp->sg_flags |= (FLG_SG_TYPE|FLG_SG_EMPTY); 396 b_type = TRUE; 397 } else if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_NOTE)) == 0) { 398 if (b_type) { 399 eprintf(ofl->ofl_lml, ERR_FATAL, 400 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 401 EC_XWORD(Line_num), 402 MSG_INTL(MSG_MAP_SEGTYP)); 403 return (S_ERROR); 404 } 405 if ((sgp->sg_flags & FLG_SG_TYPE) && 406 (sgp->sg_phdr.p_type != PT_NOTE)) 407 eprintf(ofl->ofl_lml, ERR_WARNING, 408 MSG_INTL(MSG_MAP_REDEFATT), mapfile, 409 EC_XWORD(Line_num), 410 MSG_INTL(MSG_MAP_SEGTYP), sgp->sg_name); 411 sgp->sg_phdr.p_type = PT_NOTE; 412 sgp->sg_flags |= FLG_SG_TYPE; 413 b_type = TRUE; 414 } 415 416 417 /* Segment Flags. */ 418 419 else if (*Start_tok == '?') { 420 Word tmp_flags = 0; 421 char *flag_tok = Start_tok + 1; 422 423 if (b_flags) { 424 eprintf(ofl->ofl_lml, ERR_FATAL, 425 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 426 EC_XWORD(Line_num), 427 MSG_INTL(MSG_MAP_SEGFLAG)); 428 return (S_ERROR); 429 } 430 431 /* 432 * If ? has nothing following leave the flags cleared, 433 * otherwise or in any flags specified. 434 */ 435 if (*flag_tok) { 436 while (*flag_tok) { 437 switch (*flag_tok) { 438 case 'r': 439 tmp_flags |= PF_R; 440 break; 441 case 'w': 442 tmp_flags |= PF_W; 443 break; 444 case 'x': 445 tmp_flags |= PF_X; 446 break; 447 case 'e': 448 sgp->sg_flags |= FLG_SG_EMPTY; 449 break; 450 case 'o': 451 sgp->sg_flags |= FLG_SG_ORDER; 452 ofl->ofl_flags |= 453 FLG_OF_SEGORDER; 454 break; 455 case 'n': 456 sgp->sg_flags |= FLG_SG_NOHDR; 457 break; 458 default: 459 eprintf(ofl->ofl_lml, ERR_FATAL, 460 MSG_INTL(MSG_MAP_UNKSEGFLG), 461 mapfile, EC_XWORD(Line_num), 462 *flag_tok); 463 return (S_ERROR); 464 } 465 flag_tok++; 466 } 467 } 468 /* 469 * Warn when changing flags except when we're 470 * adding or removing "X" from a RW PT_LOAD 471 * segment. 472 */ 473 if ((sgp->sg_flags & FLG_SG_FLAGS) && 474 (sgp->sg_phdr.p_flags != tmp_flags) && 475 !(sgp->sg_phdr.p_type == PT_LOAD && 476 (tmp_flags & (PF_R|PF_W)) == (PF_R|PF_W) && 477 (tmp_flags ^ sgp->sg_phdr.p_flags) == PF_X)) { 478 eprintf(ofl->ofl_lml, ERR_WARNING, 479 MSG_INTL(MSG_MAP_REDEFATT), mapfile, 480 EC_XWORD(Line_num), 481 MSG_INTL(MSG_MAP_SEGFLAG), sgp->sg_name); 482 } 483 sgp->sg_flags |= FLG_SG_FLAGS; 484 sgp->sg_phdr.p_flags = tmp_flags; 485 b_flags = TRUE; 486 } 487 488 489 /* Segment address, length, alignment or rounding number. */ 490 491 else if ((Start_tok[0] == 'l') || (Start_tok[0] == 'v') || 492 (Start_tok[0] == 'a') || (Start_tok[0] == 'p') || 493 (Start_tok[0] == 'r')) { 494 char *end_tok; 495 Xword number; 496 497 if ((number = (Xword)STRTOADDR(&Start_tok[1], &end_tok, 498 0)) >= XWORD_MAX) { 499 eprintf(ofl->ofl_lml, ERR_FATAL, 500 MSG_INTL(MSG_MAP_SEGADDR), mapfile, 501 EC_XWORD(Line_num), Start_tok, 502 MSG_INTL(MSG_MAP_EXCLIMIT)); 503 return (S_ERROR); 504 } 505 506 if (end_tok != strchr(Start_tok, '\0')) { 507 eprintf(ofl->ofl_lml, ERR_FATAL, 508 MSG_INTL(MSG_MAP_SEGADDR), mapfile, 509 EC_XWORD(Line_num), Start_tok, 510 MSG_INTL(MSG_MAP_NOBADFRM)); 511 return (S_ERROR); 512 } 513 514 switch (*Start_tok) { 515 case 'l': 516 if (b_len) { 517 eprintf(ofl->ofl_lml, ERR_FATAL, 518 MSG_INTL(MSG_MAP_MOREONCE), 519 mapfile, EC_XWORD(Line_num), 520 MSG_INTL(MSG_MAP_SEGLEN)); 521 return (S_ERROR); 522 } 523 if ((sgp->sg_flags & FLG_SG_LENGTH) && 524 (sgp->sg_length != number)) 525 eprintf(ofl->ofl_lml, ERR_WARNING, 526 MSG_INTL(MSG_MAP_REDEFATT), 527 mapfile, EC_XWORD(Line_num), 528 MSG_INTL(MSG_MAP_SEGLEN), 529 sgp->sg_name); 530 sgp->sg_length = number; 531 sgp->sg_flags |= FLG_SG_LENGTH; 532 b_len = TRUE; 533 break; 534 case 'r': 535 if (b_round) { 536 eprintf(ofl->ofl_lml, ERR_FATAL, 537 MSG_INTL(MSG_MAP_MOREONCE), 538 mapfile, EC_XWORD(Line_num), 539 MSG_INTL(MSG_MAP_SEGROUND)); 540 return (S_ERROR); 541 } 542 if ((sgp->sg_flags & FLG_SG_ROUND) && 543 (sgp->sg_round != number)) 544 eprintf(ofl->ofl_lml, ERR_WARNING, 545 MSG_INTL(MSG_MAP_REDEFATT), 546 mapfile, EC_XWORD(Line_num), 547 MSG_INTL(MSG_MAP_SEGROUND), 548 sgp->sg_name); 549 sgp->sg_round = number; 550 sgp->sg_flags |= FLG_SG_ROUND; 551 b_round = TRUE; 552 break; 553 case 'v': 554 if (b_vaddr) { 555 eprintf(ofl->ofl_lml, ERR_FATAL, 556 MSG_INTL(MSG_MAP_MOREONCE), 557 mapfile, EC_XWORD(Line_num), 558 MSG_INTL(MSG_MAP_SEGVADDR)); 559 return (S_ERROR); 560 } 561 if ((sgp->sg_flags & FLG_SG_VADDR) && 562 (sgp->sg_phdr.p_vaddr != number)) 563 eprintf(ofl->ofl_lml, ERR_WARNING, 564 MSG_INTL(MSG_MAP_REDEFATT), 565 mapfile, EC_XWORD(Line_num), 566 MSG_INTL(MSG_MAP_SEGVADDR), 567 sgp->sg_name); 568 /* LINTED */ 569 sgp->sg_phdr.p_vaddr = (Addr)number; 570 sgp->sg_flags |= FLG_SG_VADDR; 571 ofl->ofl_flags1 |= FLG_OF1_VADDR; 572 b_vaddr = TRUE; 573 break; 574 case 'p': 575 if (b_paddr) { 576 eprintf(ofl->ofl_lml, ERR_FATAL, 577 MSG_INTL(MSG_MAP_MOREONCE), 578 mapfile, EC_XWORD(Line_num), 579 MSG_INTL(MSG_MAP_SEGPHYS)); 580 return (S_ERROR); 581 } 582 if ((sgp->sg_flags & FLG_SG_PADDR) && 583 (sgp->sg_phdr.p_paddr != number)) 584 eprintf(ofl->ofl_lml, ERR_WARNING, 585 MSG_INTL(MSG_MAP_REDEFATT), 586 mapfile, EC_XWORD(Line_num), 587 MSG_INTL(MSG_MAP_SEGPHYS), 588 sgp->sg_name); 589 /* LINTED */ 590 sgp->sg_phdr.p_paddr = (Addr)number; 591 sgp->sg_flags |= FLG_SG_PADDR; 592 b_paddr = TRUE; 593 break; 594 case 'a': 595 if (b_align) { 596 eprintf(ofl->ofl_lml, ERR_FATAL, 597 MSG_INTL(MSG_MAP_MOREONCE), 598 mapfile, EC_XWORD(Line_num), 599 MSG_INTL(MSG_MAP_SEGALIGN)); 600 return (S_ERROR); 601 } 602 if ((sgp->sg_flags & FLG_SG_ALIGN) && 603 (sgp->sg_phdr.p_align != number)) 604 eprintf(ofl->ofl_lml, ERR_WARNING, 605 MSG_INTL(MSG_MAP_REDEFATT), 606 mapfile, EC_XWORD(Line_num), 607 MSG_INTL(MSG_MAP_SEGALIGN), 608 sgp->sg_name); 609 /* LINTED */ 610 sgp->sg_phdr.p_align = (Xword)number; 611 sgp->sg_flags |= FLG_SG_ALIGN; 612 b_align = TRUE; 613 break; 614 } 615 } else { 616 eprintf(ofl->ofl_lml, ERR_FATAL, 617 MSG_INTL(MSG_MAP_UNKSEGATT), mapfile, 618 EC_XWORD(Line_num), Start_tok); 619 return (S_ERROR); 620 } 621 } 622 623 /* 624 * Segment reservations are only allowable for executables. In addition 625 * they must have an associated address, size, no permisions, 626 * and are only meaningful for LOAD segments (the last failure 627 * we can correct, hence the warning condition). 628 */ 629 if ((sgp->sg_flags & FLG_SG_EMPTY) && 630 (sgp->sg_phdr.p_type != PT_SUNWSTACK)) { 631 if (!(ofl->ofl_flags & FLG_OF_EXEC)) { 632 eprintf(ofl->ofl_lml, ERR_FATAL, 633 MSG_INTL(MSG_MAP_SEGEMPEXE), mapfile, 634 EC_XWORD(Line_num)); 635 return (S_ERROR); 636 } 637 if (sgp->sg_phdr.p_flags != 0) { 638 eprintf(ofl->ofl_lml, ERR_FATAL, 639 MSG_INTL(MSG_MAP_SEGEMNOPERM), mapfile, 640 EC_XWORD(Line_num), 641 EC_WORD(sgp->sg_phdr.p_flags)); 642 return (S_ERROR); 643 } 644 if ((sgp->sg_flags & (FLG_SG_LENGTH | FLG_SG_VADDR)) != 645 (FLG_SG_LENGTH | FLG_SG_VADDR)) { 646 eprintf(ofl->ofl_lml, ERR_FATAL, 647 MSG_INTL(MSG_MAP_SEGEMPATT), mapfile, 648 EC_XWORD(Line_num)); 649 return (S_ERROR); 650 } 651 if (sgp->sg_phdr.p_type != PT_LOAD) { 652 eprintf(ofl->ofl_lml, ERR_WARNING, 653 MSG_INTL(MSG_MAP_SEGEMPLOAD), mapfile, 654 EC_XWORD(Line_num)); 655 sgp->sg_phdr.p_type = PT_LOAD; 656 } 657 } 658 659 /* 660 * All segment attributes have now been scanned. Certain flags do not 661 * make sense if this is not a loadable segment, fix if necessary. 662 * Note, if the segment is of type PT_NULL it must be new, and any 663 * defaults will be applied back in ld_map_parse(). 664 * When clearing an attribute leave the flag set as an indicator for 665 * later entries re-specifying the same segment. 666 */ 667 if (sgp->sg_phdr.p_type != PT_NULL && sgp->sg_phdr.p_type != PT_LOAD) { 668 const char *fmt; 669 670 if (sgp->sg_phdr.p_type == PT_SUNWSTACK) 671 fmt = MSG_INTL(MSG_MAP_NOSTACK1); 672 else 673 fmt = MSG_INTL(MSG_MAP_NONLOAD); 674 675 if ((sgp->sg_flags & FLG_SG_FLAGS) && 676 (sgp->sg_phdr.p_type != PT_SUNWSTACK)) { 677 if (sgp->sg_phdr.p_flags != 0) { 678 eprintf(ofl->ofl_lml, ERR_WARNING, 679 MSG_INTL(MSG_MAP_NONLOAD), mapfile, 680 EC_XWORD(Line_num), 681 MSG_INTL(MSG_MAP_SEGFLAG)); 682 sgp->sg_phdr.p_flags = 0; 683 } 684 } 685 if (sgp->sg_flags & FLG_SG_LENGTH) 686 if (sgp->sg_length != 0) { 687 eprintf(ofl->ofl_lml, ERR_WARNING, 688 fmt, mapfile, EC_XWORD(Line_num), 689 MSG_INTL(MSG_MAP_SEGLEN)); 690 sgp->sg_length = 0; 691 } 692 if (sgp->sg_flags & FLG_SG_ROUND) 693 if (sgp->sg_round != 0) { 694 eprintf(ofl->ofl_lml, ERR_WARNING, 695 fmt, mapfile, EC_XWORD(Line_num), 696 MSG_INTL(MSG_MAP_SEGROUND)); 697 sgp->sg_round = 0; 698 } 699 if (sgp->sg_flags & FLG_SG_VADDR) { 700 if (sgp->sg_phdr.p_vaddr != 0) { 701 eprintf(ofl->ofl_lml, ERR_WARNING, 702 fmt, mapfile, EC_XWORD(Line_num), 703 MSG_INTL(MSG_MAP_SEGVADDR)); 704 sgp->sg_phdr.p_vaddr = 0; 705 } 706 } 707 if (sgp->sg_flags & FLG_SG_PADDR) 708 if (sgp->sg_phdr.p_paddr != 0) { 709 eprintf(ofl->ofl_lml, ERR_WARNING, 710 fmt, mapfile, EC_XWORD(Line_num), 711 MSG_INTL(MSG_MAP_SEGPHYS)); 712 sgp->sg_phdr.p_paddr = 0; 713 } 714 if (sgp->sg_flags & FLG_SG_ALIGN) 715 if (sgp->sg_phdr.p_align != 0) { 716 eprintf(ofl->ofl_lml, ERR_WARNING, 717 fmt, mapfile, EC_XWORD(Line_num), 718 MSG_INTL(MSG_MAP_SEGALIGN)); 719 sgp->sg_phdr.p_align = 0; 720 } 721 } 722 return (1); 723 } 724 725 726 /* 727 * Process a mapfile mapping directives definition. 728 * segment_name : section_attribute [ : file_name ] 729 * segment_attribute : section_name section_type section_flags; 730 */ 731 static uintptr_t 732 map_colon(Ofl_desc *ofl, const char *mapfile, Ent_desc *enp) 733 { 734 Token tok; /* Current token. */ 735 736 Boolean b_name = FALSE; 737 Boolean b_type = FALSE; 738 Boolean b_attr = FALSE; 739 Boolean b_bang = FALSE; 740 static Xword index = 0; 741 742 743 while (((tok = gettoken(ofl, mapfile)) != TK_COLON) && 744 (tok != TK_SEMICOLON)) { 745 /* LINTED */ 746 if (tok == (Token)S_ERROR) 747 return (S_ERROR); 748 if (tok == TK_EOF) { 749 eprintf(ofl->ofl_lml, ERR_FATAL, 750 MSG_INTL(MSG_MAP_PREMEOF), mapfile, 751 EC_XWORD(Line_num)); 752 return (S_ERROR); 753 } 754 755 /* Segment type. */ 756 757 if (*Start_tok == '$') { 758 if (b_type) { 759 eprintf(ofl->ofl_lml, ERR_FATAL, 760 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 761 EC_XWORD(Line_num), 762 MSG_INTL(MSG_MAP_SECTYP)); 763 return (S_ERROR); 764 } 765 b_type = TRUE; 766 Start_tok++; 767 lowercase(Start_tok); 768 if (strcmp(Start_tok, MSG_ORIG(MSG_STR_PROGBITS)) == 0) 769 enp->ec_type = SHT_PROGBITS; 770 else if (strcmp(Start_tok, 771 MSG_ORIG(MSG_STR_SYMTAB)) == 0) 772 enp->ec_type = SHT_SYMTAB; 773 else if (strcmp(Start_tok, 774 MSG_ORIG(MSG_STR_DYNSYM)) == 0) 775 enp->ec_type = SHT_DYNSYM; 776 else if (strcmp(Start_tok, 777 MSG_ORIG(MSG_STR_STRTAB)) == 0) 778 enp->ec_type = SHT_STRTAB; 779 else if ((strcmp(Start_tok, 780 MSG_ORIG(MSG_STR_REL)) == 0) || 781 (strcmp(Start_tok, MSG_ORIG(MSG_STR_RELA)) == 0)) 782 enp->ec_type = M_REL_SHT_TYPE; 783 else if (strcmp(Start_tok, MSG_ORIG(MSG_STR_HASH)) == 0) 784 enp->ec_type = SHT_HASH; 785 else if (strcmp(Start_tok, MSG_ORIG(MSG_STR_LIB)) == 0) 786 enp->ec_type = SHT_SHLIB; 787 else if (strcmp(Start_tok, 788 MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 0) 789 enp->ec_type = SHT_DYNAMIC; 790 else if (strcmp(Start_tok, MSG_ORIG(MSG_STR_NOTE)) == 0) 791 enp->ec_type = SHT_NOTE; 792 else if (strcmp(Start_tok, 793 MSG_ORIG(MSG_STR_NOBITS)) == 0) 794 enp->ec_type = SHT_NOBITS; 795 else { 796 eprintf(ofl->ofl_lml, ERR_FATAL, 797 MSG_INTL(MSG_MAP_UNKSECTYP), mapfile, 798 EC_XWORD(Line_num), Start_tok); 799 return (S_ERROR); 800 } 801 802 /* 803 * Segment flags. 804 * If a segment flag is specified then the appropriate bit is 805 * set in the ec_attrmask, the ec_attrbits fields determine 806 * whether the attrmask fields must be tested true or false 807 * ie. for ?A the attrmask is set and the attrbit is set, 808 * for ?!A the attrmask is set and the attrbit is clear. 809 */ 810 } else if (*Start_tok == '?') { 811 if (b_attr) { 812 eprintf(ofl->ofl_lml, ERR_FATAL, 813 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 814 EC_XWORD(Line_num), 815 MSG_INTL(MSG_MAP_SECFLAG)); 816 return (S_ERROR); 817 } 818 b_attr = TRUE; 819 b_bang = FALSE; 820 Start_tok++; 821 lowercase(Start_tok); 822 for (; *Start_tok != '\0'; Start_tok++) 823 switch (*Start_tok) { 824 case '!': 825 if (b_bang) { 826 eprintf(ofl->ofl_lml, ERR_FATAL, 827 MSG_INTL(MSG_MAP_BADFLAG), 828 mapfile, EC_XWORD(Line_num), 829 Start_tok); 830 return (S_ERROR); 831 } 832 b_bang = TRUE; 833 break; 834 case 'a': 835 if (enp->ec_attrmask & SHF_ALLOC) { 836 eprintf(ofl->ofl_lml, ERR_FATAL, 837 MSG_INTL(MSG_MAP_BADFLAG), 838 mapfile, EC_XWORD(Line_num), 839 Start_tok); 840 return (S_ERROR); 841 } 842 enp->ec_attrmask |= SHF_ALLOC; 843 if (!b_bang) 844 enp->ec_attrbits |= SHF_ALLOC; 845 b_bang = FALSE; 846 break; 847 case 'w': 848 if (enp->ec_attrmask & SHF_WRITE) { 849 eprintf(ofl->ofl_lml, ERR_FATAL, 850 MSG_INTL(MSG_MAP_BADFLAG), 851 mapfile, EC_XWORD(Line_num), 852 Start_tok); 853 return (S_ERROR); 854 } 855 enp->ec_attrmask |= SHF_WRITE; 856 if (!b_bang) 857 enp->ec_attrbits |= SHF_WRITE; 858 b_bang = FALSE; 859 break; 860 case 'x': 861 if (enp->ec_attrmask & SHF_EXECINSTR) { 862 eprintf(ofl->ofl_lml, ERR_FATAL, 863 MSG_INTL(MSG_MAP_BADFLAG), 864 mapfile, EC_XWORD(Line_num), 865 Start_tok); 866 return (S_ERROR); 867 } 868 enp->ec_attrmask |= SHF_EXECINSTR; 869 if (!b_bang) 870 enp->ec_attrbits |= SHF_EXECINSTR; 871 b_bang = FALSE; 872 break; 873 default: 874 eprintf(ofl->ofl_lml, ERR_FATAL, 875 MSG_INTL(MSG_MAP_BADFLAG), 876 mapfile, EC_XWORD(Line_num), 877 Start_tok); 878 return (S_ERROR); 879 } 880 /* 881 * Section name. 882 */ 883 } else { 884 if (b_name) { 885 eprintf(ofl->ofl_lml, ERR_FATAL, 886 MSG_INTL(MSG_MAP_MOREONCE), mapfile, 887 EC_XWORD(Line_num), 888 MSG_INTL(MSG_MAP_SECNAME)); 889 return (S_ERROR); 890 } 891 b_name = TRUE; 892 if ((enp->ec_name = 893 libld_malloc(strlen(Start_tok) + 1)) == 0) 894 return (S_ERROR); 895 (void) strcpy((char *)enp->ec_name, Start_tok); 896 /* 897 * get the index for text reordering 898 */ 899 /* LINTED */ 900 enp->ec_ndx = (Word)++index; 901 } 902 } 903 if (tok == TK_COLON) { 904 /* 905 * File names. 906 */ 907 while ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 908 char *file; 909 910 /* LINTED */ 911 if (tok == (Token)S_ERROR) 912 return (S_ERROR); 913 if (tok == TK_EOF) { 914 eprintf(ofl->ofl_lml, ERR_FATAL, 915 MSG_INTL(MSG_MAP_PREMEOF), mapfile, 916 EC_XWORD(Line_num)); 917 return (S_ERROR); 918 } 919 if (tok != TK_STRING) { 920 eprintf(ofl->ofl_lml, ERR_FATAL, 921 MSG_INTL(MSG_MAP_MALFORM), mapfile, 922 EC_XWORD(Line_num)); 923 return (S_ERROR); 924 } 925 if ((file = 926 libld_malloc(strlen(Start_tok) + 1)) == 0) 927 return (S_ERROR); 928 (void) strcpy(file, Start_tok); 929 if (list_appendc(&(enp->ec_files), file) == 0) 930 return (S_ERROR); 931 } 932 } 933 return (1); 934 } 935 936 /* 937 * Obtain a pseudo input file descriptor to assign to a mapfile. This is 938 * required any time a symbol is generated. First traverse the input file 939 * descriptors looking for a match. As all mapfile processing occurs before 940 * any real input file processing this list is going to be small and we don't 941 * need to do any filename clash checking. 942 */ 943 static Ifl_desc * 944 map_ifl(const char *mapfile, Ofl_desc *ofl) 945 { 946 Ifl_desc *ifl; 947 Listnode *lnp; 948 949 for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, ifl)) 950 if (strcmp(ifl->ifl_name, mapfile) == 0) 951 return (ifl); 952 953 if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == 0) 954 return ((Ifl_desc *)S_ERROR); 955 ifl->ifl_name = mapfile; 956 ifl->ifl_flags = (FLG_IF_MAPFILE | FLG_IF_NEEDED | FLG_IF_FILEREF); 957 if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == 0) 958 return ((Ifl_desc *)S_ERROR); 959 ifl->ifl_ehdr->e_type = ET_REL; 960 961 if (list_appendc(&ofl->ofl_objs, ifl) == 0) 962 return ((Ifl_desc *)S_ERROR); 963 else 964 return (ifl); 965 } 966 967 /* 968 * Process a mapfile size symbol definition. 969 * segment_name @ symbol_name; 970 */ 971 static uintptr_t 972 map_atsign(const char *mapfile, Sg_desc *sgp, Ofl_desc *ofl) 973 { 974 Sym *sym; /* New symbol pointer */ 975 Sym_desc *sdp; /* New symbol node pointer */ 976 Ifl_desc *ifl; /* Dummy input file structure */ 977 Token tok; /* Current token. */ 978 avl_index_t where; 979 980 if ((tok = gettoken(ofl, mapfile)) != TK_STRING) { 981 /* LINTED */ 982 if (tok != (Token)S_ERROR) 983 eprintf(ofl->ofl_lml, ERR_FATAL, 984 MSG_INTL(MSG_MAP_EXPSYM_1), mapfile, 985 EC_XWORD(Line_num)); 986 return (S_ERROR); 987 } 988 989 if (sgp->sg_sizesym != NULL) { 990 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_MAP_SEGSIZE), 991 mapfile, EC_XWORD(Line_num), sgp->sg_name); 992 return (S_ERROR); 993 } 994 995 /* 996 * Make sure we have a pseudo file descriptor to associate to the 997 * symbol. 998 */ 999 if ((ifl = map_ifl(mapfile, ofl)) == (Ifl_desc *)S_ERROR) 1000 return (S_ERROR); 1001 1002 /* 1003 * Make sure the symbol doesn't already exist. It is possible that the 1004 * symbol has been scoped or versioned, in which case it does exist 1005 * but we can freely update it here. 1006 */ 1007 if ((sdp = ld_sym_find(Start_tok, SYM_NOHASH, &where, ofl)) == NULL) { 1008 char *name; 1009 Word hval; 1010 1011 if ((name = libld_malloc(strlen(Start_tok) + 1)) == 0) 1012 return (S_ERROR); 1013 (void) strcpy(name, Start_tok); 1014 1015 if ((sym = libld_calloc(sizeof (Sym), 1)) == 0) 1016 return (S_ERROR); 1017 sym->st_shndx = SHN_ABS; 1018 sym->st_size = 0; 1019 sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 1020 1021 DBG_CALL(Dbg_map_size_new(ofl->ofl_lml, name)); 1022 /* LINTED */ 1023 hval = (Word)elf_hash(name); 1024 if ((sdp = ld_sym_enter(name, sym, hval, ifl, ofl, 0, SHN_ABS, 1025 (FLG_SY_SPECSEC | FLG_SY_GLOBREF), 0, &where)) == 1026 (Sym_desc *)S_ERROR) 1027 return (S_ERROR); 1028 sdp->sd_flags &= ~FLG_SY_CLEAN; 1029 DBG_CALL(Dbg_map_symbol(ofl, sdp)); 1030 } else { 1031 sym = sdp->sd_sym; 1032 1033 if (sym->st_shndx == SHN_UNDEF) { 1034 sdp->sd_shndx = sym->st_shndx = SHN_ABS; 1035 sdp->sd_flags |= FLG_SY_SPECSEC; 1036 sym->st_size = 0; 1037 sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 1038 1039 sdp->sd_flags &= ~FLG_SY_MAPREF; 1040 1041 DBG_CALL(Dbg_map_size_old(ofl, sdp)); 1042 } else { 1043 eprintf(ofl->ofl_lml, ERR_FATAL, 1044 MSG_INTL(MSG_MAP_SYMDEF), mapfile, 1045 EC_XWORD(Line_num), demangle(sdp->sd_name), 1046 sdp->sd_file->ifl_name); 1047 return (S_ERROR); 1048 } 1049 } 1050 1051 /* 1052 * Assign the symbol to the segment. 1053 */ 1054 sgp->sg_sizesym = sdp; 1055 1056 if (gettoken(ofl, mapfile) != TK_SEMICOLON) { 1057 /* LINTED */ 1058 if (tok != (Token)S_ERROR) 1059 eprintf(ofl->ofl_lml, ERR_FATAL, 1060 MSG_INTL(MSG_MAP_EXPSCOL), mapfile, 1061 EC_XWORD(Line_num)); 1062 return (S_ERROR); 1063 } else 1064 return (1); 1065 } 1066 1067 1068 static uintptr_t 1069 map_pipe(Ofl_desc *ofl, const char *mapfile, Sg_desc *sgp) 1070 { 1071 char *sec_name; /* section name */ 1072 Token tok; /* current token. */ 1073 Sec_order *sc_order; 1074 static Word index = 0; /* used to maintain a increasing */ 1075 /* index for section ordering. */ 1076 1077 if ((tok = gettoken(ofl, mapfile)) != TK_STRING) { 1078 /* LINTED */ 1079 if (tok != (Token)S_ERROR) 1080 eprintf(ofl->ofl_lml, ERR_FATAL, 1081 MSG_INTL(MSG_MAP_EXPSEC), mapfile, 1082 EC_XWORD(Line_num)); 1083 return (S_ERROR); 1084 } 1085 1086 if ((sec_name = libld_malloc(strlen(Start_tok) + 1)) == 0) 1087 return (S_ERROR); 1088 (void) strcpy(sec_name, Start_tok); 1089 1090 if ((sc_order = libld_malloc(sizeof (Sec_order))) == 0) 1091 return (S_ERROR); 1092 1093 sc_order->sco_secname = sec_name; 1094 sc_order->sco_index = ++index; 1095 1096 if (list_appendc(&(sgp->sg_secorder), sc_order) == 0) 1097 return (S_ERROR); 1098 1099 DBG_CALL(Dbg_map_pipe(ofl->ofl_lml, sgp, sec_name, index)); 1100 1101 if ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 1102 /* LINTED */ 1103 if (tok != (Token)S_ERROR) 1104 eprintf(ofl->ofl_lml, ERR_FATAL, 1105 MSG_INTL(MSG_MAP_EXPSCOL), mapfile, 1106 EC_XWORD(Line_num)); 1107 return (S_ERROR); 1108 } 1109 1110 return (1); 1111 } 1112 1113 1114 /* 1115 * Process a mapfile library specification definition. 1116 * shared_object_name - shared object definition 1117 * shared object definition : [ shared object type [ = SONAME ]] 1118 * [ versions ]; 1119 */ 1120 static uintptr_t 1121 map_dash(const char *mapfile, char *name, Ofl_desc *ofl) 1122 { 1123 char *version; 1124 Token tok; 1125 Sdf_desc *sdf; 1126 Sdv_desc *sdv; 1127 enum { 1128 MD_NONE = 0, 1129 MD_SPECVERS, 1130 MD_ADDVERS, 1131 MD_NEEDED 1132 } dolkey = MD_NONE; 1133 1134 1135 /* 1136 * If a shared object definition for this file already exists use it, 1137 * otherwise allocate a new descriptor. 1138 */ 1139 if ((sdf = sdf_find(name, &ofl->ofl_socntl)) == 0) { 1140 if ((sdf = sdf_add(name, &ofl->ofl_socntl)) == 1141 (Sdf_desc *)S_ERROR) 1142 return (S_ERROR); 1143 sdf->sdf_rfile = mapfile; 1144 } 1145 1146 /* 1147 * Get the shared object descriptor string. 1148 */ 1149 while ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 1150 /* LINTED */ 1151 if (tok == (Token)S_ERROR) 1152 return (S_ERROR); 1153 if (tok == TK_EOF) { 1154 eprintf(ofl->ofl_lml, ERR_FATAL, 1155 MSG_INTL(MSG_MAP_PREMEOF), mapfile, 1156 EC_XWORD(Line_num)); 1157 return (S_ERROR); 1158 } 1159 if ((tok != TK_STRING) && (tok != TK_EQUAL)) { 1160 eprintf(ofl->ofl_lml, ERR_FATAL, 1161 MSG_INTL(MSG_MAP_EXPSO), mapfile, 1162 EC_XWORD(Line_num)); 1163 return (S_ERROR); 1164 } 1165 1166 /* 1167 * Determine if the library type is accompanied with a SONAME 1168 * definition. 1169 */ 1170 if (tok == TK_EQUAL) { 1171 if ((tok = gettoken(ofl, mapfile)) != TK_STRING) { 1172 /* LINTED */ 1173 if (tok == (Token)S_ERROR) 1174 return (S_ERROR); 1175 else { 1176 eprintf(ofl->ofl_lml, ERR_FATAL, 1177 MSG_INTL(MSG_MAP_EXPSO), mapfile, 1178 EC_XWORD(Line_num)); 1179 return (S_ERROR); 1180 } 1181 } 1182 switch (dolkey) { 1183 case MD_NEEDED: 1184 if (sdf->sdf_flags & FLG_SDF_SONAME) { 1185 eprintf(ofl->ofl_lml, ERR_WARNING, 1186 MSG_INTL(MSG_MAP_MULSONAME), 1187 mapfile, EC_XWORD(Line_num), name, 1188 sdf->sdf_soname, Start_tok); 1189 dolkey = MD_NONE; 1190 continue; 1191 } 1192 if ((sdf->sdf_soname = 1193 libld_malloc(strlen(Start_tok) + 1)) == 0) 1194 return (S_ERROR); 1195 (void) strcpy((char *)sdf->sdf_soname, 1196 Start_tok); 1197 sdf->sdf_flags |= FLG_SDF_SONAME; 1198 break; 1199 case MD_SPECVERS: 1200 case MD_ADDVERS: 1201 if ((sdv = libld_calloc( 1202 sizeof (Sdv_desc), 1)) == 0) 1203 return (S_ERROR); 1204 1205 if (dolkey == MD_SPECVERS) 1206 sdf->sdf_flags |= FLG_SDF_SPECVER; 1207 else 1208 sdf->sdf_flags |= FLG_SDF_ADDVER; 1209 1210 if ((sdf->sdf_flags & (FLG_SDF_SPECVER | 1211 FLG_SDF_ADDVER)) == (FLG_SDF_SPECVER | 1212 FLG_SDF_ADDVER)) { 1213 eprintf(ofl->ofl_lml, ERR_FATAL, 1214 MSG_INTL(MSG_MAP_INCOMFLG), 1215 mapfile, EC_XWORD(Line_num), 1216 sdf->sdf_name); 1217 return (S_ERROR); 1218 } 1219 if ((version = 1220 libld_malloc(strlen(Start_tok) + 1)) == 0) 1221 return (S_ERROR); 1222 (void) strcpy(version, Start_tok); 1223 sdv->sdv_name = version; 1224 sdv->sdv_ref = mapfile; 1225 if (list_appendc(&sdf->sdf_verneed, sdv) == 0) 1226 return (S_ERROR); 1227 break; 1228 case MD_NONE: 1229 eprintf(ofl->ofl_lml, ERR_FATAL, 1230 MSG_INTL(MSG_MAP_UNEXTOK), mapfile, 1231 EC_XWORD(Line_num), '='); 1232 return (S_ERROR); 1233 } 1234 dolkey = MD_NONE; 1235 continue; 1236 } 1237 1238 /* 1239 * A shared object type has been specified. This may also be 1240 * accompanied by an SONAME redefinition (see above). 1241 */ 1242 if (*Start_tok == '$') { 1243 if (dolkey != MD_NONE) { 1244 eprintf(ofl->ofl_lml, ERR_FATAL, 1245 MSG_INTL(MSG_MAP_UNEXTOK), mapfile, 1246 EC_XWORD(Line_num), '$'); 1247 return (S_ERROR); 1248 } 1249 Start_tok++; 1250 lowercase(Start_tok); 1251 if (strcmp(Start_tok, 1252 MSG_ORIG(MSG_MAP_NEED)) == 0) 1253 dolkey = MD_NEEDED; 1254 else if (strcmp(Start_tok, 1255 MSG_ORIG(MSG_MAP_SPECVERS)) == 0) 1256 dolkey = MD_SPECVERS; 1257 else if (strcmp(Start_tok, 1258 MSG_ORIG(MSG_MAP_ADDVERS)) == 0) 1259 dolkey = MD_ADDVERS; 1260 else { 1261 eprintf(ofl->ofl_lml, ERR_FATAL, 1262 MSG_INTL(MSG_MAP_UNKSOTYP), mapfile, 1263 EC_XWORD(Line_num), Start_tok); 1264 return (S_ERROR); 1265 } 1266 continue; 1267 } 1268 1269 /* 1270 * shared object version requirement. 1271 */ 1272 if ((version = libld_malloc(strlen(Start_tok) + 1)) == 0) 1273 return (S_ERROR); 1274 (void) strcpy(version, Start_tok); 1275 if ((sdv = libld_calloc(sizeof (Sdv_desc), 1)) == 0) 1276 return (S_ERROR); 1277 sdv->sdv_name = version; 1278 sdv->sdv_ref = mapfile; 1279 sdf->sdf_flags |= FLG_SDF_SELECT; 1280 if (list_appendc(&sdf->sdf_vers, sdv) == 0) 1281 return (S_ERROR); 1282 } 1283 1284 DBG_CALL(Dbg_map_dash(ofl->ofl_lml, name, sdf)); 1285 return (1); 1286 } 1287 1288 1289 /* 1290 * Process a symbol definition list 1291 * version name { 1292 * local: 1293 * hidden: 1294 * symbol { = (FUNC | OBJECT) 1295 * (PARENT | EXTERN) 1296 * ($ABS | $COMMON) A V 1297 * }; 1298 * *; 1299 * global: 1300 * default: 1301 * symbolic: 1302 * protected: 1303 * symbol { = (FUNC | OBJECT) 1304 * (PARENT | EXTERN) 1305 * (DIRECT | NODIRECT) 1306 * ([FILTER | AUXILIARY] name) 1307 * ($ABS | $COMMON) A V 1308 * }; 1309 * } [references]; 1310 */ 1311 #define SYMNO 50 /* Symbol block allocation amount */ 1312 #define FLG_SCOPE_LOCL 0 /* local scope flag */ 1313 #define FLG_SCOPE_GLOB 1 /* global scope flag */ 1314 #define FLG_SCOPE_SYMB 2 /* symbolic scope flag */ 1315 #define FLG_SCOPE_ELIM 3 /* eliminate symbol from symtabs */ 1316 1317 static uintptr_t 1318 map_version(const char *mapfile, char *name, Ofl_desc *ofl) 1319 { 1320 Token tok; 1321 Sym *sym; 1322 int scope = FLG_SCOPE_GLOB; 1323 Ver_desc *vdp; 1324 Word hash; 1325 Ifl_desc *ifl; 1326 avl_index_t where; 1327 1328 /* 1329 * If we're generating segments within the image then any symbol 1330 * reductions will be processed (ie. applied to relocations and symbol 1331 * table entries). Otherwise (when creating a relocatable object) any 1332 * versioning information is simply recorded for use in a later 1333 * (segment generating) link-edit. 1334 */ 1335 if (ofl->ofl_flags & FLG_OF_RELOBJ) 1336 ofl->ofl_flags |= FLG_OF_VERDEF; 1337 1338 /* 1339 * If this is a new mapfile reference generate an input file descriptor 1340 * to represent it. Otherwise this must simply be a new version within 1341 * the mapfile we've previously been processing, in this case continue 1342 * to use the original input file descriptor. 1343 */ 1344 if ((ifl = map_ifl(mapfile, ofl)) == (Ifl_desc *)S_ERROR) 1345 return (S_ERROR); 1346 1347 /* 1348 * If no version descriptors have yet been set up, initialize a base 1349 * version to represent the output file itself. This `base' version 1350 * catches any internally generated symbols (_end, _etext, etc.) and 1351 * serves to initialize the output version descriptor count. 1352 */ 1353 if (ofl->ofl_vercnt == 0) { 1354 if (ld_vers_base(ofl) == (Ver_desc *)S_ERROR) 1355 return (S_ERROR); 1356 } 1357 1358 /* 1359 * If this definition has an associated version name then generate a 1360 * new version descriptor and an associated version symbol index table. 1361 */ 1362 if (name) { 1363 ofl->ofl_flags |= FLG_OF_VERDEF; 1364 1365 /* 1366 * Traverse the present version descriptor list to see if there 1367 * is already one of the same name, otherwise create a new one. 1368 */ 1369 /* LINTED */ 1370 hash = (Word)elf_hash(name); 1371 if ((vdp = ld_vers_find(name, hash, &ofl->ofl_verdesc)) == 0) { 1372 if ((vdp = ld_vers_desc(name, hash, 1373 &ofl->ofl_verdesc)) == (Ver_desc *)S_ERROR) 1374 return (S_ERROR); 1375 } 1376 1377 /* 1378 * Initialize any new version with an index, the file from which 1379 * it was first referenced, and a WEAK flag (indicates that 1380 * there are no symbols assigned to it yet). 1381 */ 1382 if (vdp->vd_ndx == 0) { 1383 /* LINTED */ 1384 vdp->vd_ndx = (Half)++ofl->ofl_vercnt; 1385 vdp->vd_file = ifl; 1386 vdp->vd_flags = VER_FLG_WEAK; 1387 } 1388 } else { 1389 /* 1390 * If a version definition hasn't been specified assign any 1391 * symbols to the base version. 1392 */ 1393 vdp = (Ver_desc *)ofl->ofl_verdesc.head->data; 1394 } 1395 1396 /* 1397 * Scan the mapfile entry picking out scoping and symbol definitions. 1398 */ 1399 while ((tok = gettoken(ofl, mapfile)) != TK_RIGHTBKT) { 1400 Sym_desc * sdp; 1401 Word shndx = SHN_UNDEF; 1402 uchar_t type = STT_NOTYPE; 1403 Addr value = 0, size = 0; 1404 char *_name, *filtee = 0; 1405 Word sym_flags = 0; 1406 Half sym_flags1 = 0; 1407 uint_t filter = 0, dftflag; 1408 1409 if ((tok != TK_STRING) && (tok != TK_COLON)) { 1410 eprintf(ofl->ofl_lml, ERR_FATAL, 1411 MSG_INTL(MSG_MAP_EXPSYM_2), mapfile, 1412 EC_XWORD(Line_num)); 1413 return (S_ERROR); 1414 } 1415 1416 if ((_name = libld_malloc(strlen(Start_tok) + 1)) == 0) 1417 return (S_ERROR); 1418 (void) strcpy(_name, Start_tok); 1419 1420 if ((tok != TK_COLON) && 1421 /* LINTED */ 1422 (tok = gettoken(ofl, mapfile)) == (Token)S_ERROR) 1423 return (S_ERROR); 1424 1425 /* 1426 * Turn off the WEAK flag to indicate that definitions are 1427 * associated with this version. It would probably be more 1428 * accurate to only remove this flag with the specification of 1429 * global symbols, however setting it here allows enough slop 1430 * to compensate for the various user inputs we've seen so far. 1431 * Only if a closed version is specified (i.e., "SUNW_1.x {};") 1432 * will a user get a weak version (which is how we document the 1433 * creation of weak versions). 1434 */ 1435 vdp->vd_flags &= ~VER_FLG_WEAK; 1436 1437 switch (tok) { 1438 case TK_COLON: 1439 /* 1440 * Establish a new scope. All symbols added by this 1441 * mapfile are actually global entries. They will be 1442 * reduced to locals during sym_update(). 1443 */ 1444 if ((strcmp(MSG_ORIG(MSG_STR_LOCAL), 1445 _name) == 0) || 1446 (strcmp(MSG_ORIG(MSG_MAP_HIDDEN), _name) == 0)) 1447 scope = FLG_SCOPE_LOCL; 1448 else if ((strcmp(MSG_ORIG(MSG_MAP_GLOBAL), 1449 _name) == 0) || 1450 (strcmp(MSG_ORIG(MSG_MAP_DEFAULT), _name) == 0)) 1451 scope = FLG_SCOPE_GLOB; 1452 else if ((strcmp(MSG_ORIG(MSG_STR_SYMBOLIC), 1453 _name) == 0) || 1454 (strcmp(MSG_ORIG(MSG_MAP_PROTECTED), _name) == 0)) 1455 scope = FLG_SCOPE_SYMB; 1456 else if (strcmp(MSG_ORIG(MSG_STR_ELIMINATE), _name) 1457 == 0) 1458 scope = FLG_SCOPE_ELIM; 1459 else { 1460 eprintf(ofl->ofl_lml, ERR_FATAL, 1461 MSG_INTL(MSG_MAP_UNKSYMSCO), mapfile, 1462 EC_XWORD(Line_num), _name); 1463 return (S_ERROR); 1464 } 1465 continue; 1466 1467 case TK_EQUAL: 1468 /* 1469 * A full blown symbol definition follows. 1470 * Determine the symbol type and any virtual address or 1471 * alignment specified and then fall through to process 1472 * the entire symbols information. 1473 */ 1474 while ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 1475 /* 1476 * If we had previously seen a filter or 1477 * auxiliary filter requirement, the next string 1478 * is the filtee itself. 1479 */ 1480 if (filter) { 1481 if (filtee) { 1482 eprintf(ofl->ofl_lml, ERR_FATAL, 1483 MSG_INTL(MSG_MAP_MULTFILTEE), 1484 mapfile, EC_XWORD(Line_num), 1485 _name); 1486 return (S_ERROR); 1487 } 1488 if ((filtee = libld_malloc( 1489 strlen(Start_tok) + 1)) == 0) 1490 return (S_ERROR); 1491 (void) strcpy(filtee, Start_tok); 1492 filter = 0; 1493 continue; 1494 } 1495 1496 /* 1497 * Determine any Value or Size attributes. 1498 */ 1499 lowercase(Start_tok); 1500 if (Start_tok[0] == 'v' || 1501 Start_tok[0] == 's') { 1502 char *end_tok; 1503 Lword number; 1504 1505 if ((number = (Lword)STRTOADDR( 1506 &Start_tok[1], &end_tok, 0)) == 1507 XWORD_MAX) { 1508 eprintf(ofl->ofl_lml, ERR_FATAL, 1509 MSG_INTL(MSG_MAP_SEGADDR), 1510 mapfile, EC_XWORD(Line_num), 1511 Start_tok, 1512 MSG_INTL(MSG_MAP_EXCLIMIT)); 1513 return (S_ERROR); 1514 } 1515 1516 if (end_tok != 1517 strchr(Start_tok, '\0')) { 1518 eprintf(ofl->ofl_lml, ERR_FATAL, 1519 MSG_INTL(MSG_MAP_SEGADDR), 1520 mapfile, EC_XWORD(Line_num), 1521 Start_tok, 1522 MSG_INTL(MSG_MAP_NOBADFRM)); 1523 return (S_ERROR); 1524 } 1525 1526 switch (*Start_tok) { 1527 case 'v': 1528 if (value) { 1529 eprintf(ofl->ofl_lml, ERR_FATAL, 1530 MSG_INTL(MSG_MAP_MOREONCE), 1531 mapfile, EC_XWORD(Line_num), 1532 MSG_INTL(MSG_MAP_SYMVAL)); 1533 return (S_ERROR); 1534 } 1535 /* LINTED */ 1536 value = (Addr)number; 1537 break; 1538 case 's': 1539 if (size) { 1540 eprintf(ofl->ofl_lml, ERR_FATAL, 1541 MSG_INTL(MSG_MAP_MOREONCE), 1542 mapfile, EC_XWORD(Line_num), 1543 MSG_INTL(MSG_MAP_SYMSIZE)); 1544 return (S_ERROR); 1545 } 1546 /* LINTED */ 1547 size = (Addr)number; 1548 break; 1549 } 1550 1551 } else if (strcmp(Start_tok, 1552 MSG_ORIG(MSG_MAP_FUNCTION)) == 0) { 1553 shndx = SHN_ABS; 1554 sym_flags |= FLG_SY_SPECSEC; 1555 type = STT_FUNC; 1556 } else if (strcmp(Start_tok, 1557 MSG_ORIG(MSG_MAP_DATA)) == 0) { 1558 shndx = SHN_ABS; 1559 sym_flags |= FLG_SY_SPECSEC; 1560 type = STT_OBJECT; 1561 } else if (strcmp(Start_tok, 1562 MSG_ORIG(MSG_MAP_COMMON)) == 0) { 1563 shndx = SHN_COMMON; 1564 sym_flags |= FLG_SY_SPECSEC; 1565 type = STT_OBJECT; 1566 } else if (strcmp(Start_tok, 1567 MSG_ORIG(MSG_MAP_PARENT)) == 0) { 1568 sym_flags |= FLG_SY_PARENT; 1569 ofl->ofl_flags |= FLG_OF_SYMINFO; 1570 } else if (strcmp(Start_tok, 1571 MSG_ORIG(MSG_MAP_EXTERN)) == 0) { 1572 sym_flags |= FLG_SY_EXTERN; 1573 ofl->ofl_flags |= FLG_OF_SYMINFO; 1574 } else if (strcmp(Start_tok, 1575 MSG_ORIG(MSG_MAP_DIRECT)) == 0) { 1576 sym_flags1 |= FLG_SY1_DIR; 1577 ofl->ofl_flags |= FLG_OF_SYMINFO; 1578 } else if (strcmp(Start_tok, 1579 MSG_ORIG(MSG_MAP_NODIRECT)) == 0) { 1580 sym_flags1 |= FLG_SY1_NDIR; 1581 ofl->ofl_flags |= FLG_OF_SYMINFO; 1582 ofl->ofl_flags1 |= FLG_OF1_NDIRECT; 1583 ofl->ofl_dtflags_1 |= DF_1_NODIRECT; 1584 } else if (strcmp(Start_tok, 1585 MSG_ORIG(MSG_MAP_FILTER)) == 0) { 1586 dftflag = filter = FLG_SY_STDFLTR; 1587 sym_flags |= FLG_SY_STDFLTR; 1588 ofl->ofl_flags |= FLG_OF_SYMINFO; 1589 continue; 1590 } else if (strcmp(Start_tok, 1591 MSG_ORIG(MSG_MAP_AUXILIARY)) == 0) { 1592 dftflag = filter = FLG_SY_AUXFLTR; 1593 sym_flags |= FLG_SY_AUXFLTR; 1594 ofl->ofl_flags |= FLG_OF_SYMINFO; 1595 continue; 1596 } else { 1597 eprintf(ofl->ofl_lml, ERR_FATAL, 1598 MSG_INTL(MSG_MAP_UNKSYMDEF), 1599 mapfile, EC_XWORD(Line_num), 1600 Start_tok); 1601 return (S_ERROR); 1602 } 1603 } 1604 /* FALLTHROUGH */ 1605 1606 case TK_SEMICOLON: 1607 /* 1608 * The special auto-reduction directive `*' can be 1609 * specified in local scope and indicates that all 1610 * symbols processed that are not explicitly defined to 1611 * be global are to be reduced to local scope in the 1612 * output image. This also applies that a version 1613 * definition is created as the user has effectively 1614 * defined an interface. 1615 */ 1616 if (*_name == '*') { 1617 if (scope == FLG_SCOPE_LOCL) 1618 ofl->ofl_flags |= 1619 (FLG_OF_AUTOLCL | FLG_OF_VERDEF); 1620 else if (scope == FLG_SCOPE_ELIM) { 1621 ofl->ofl_flags |= FLG_OF_VERDEF; 1622 ofl->ofl_flags1 |= FLG_OF1_AUTOELM; 1623 } 1624 continue; 1625 } 1626 1627 /* 1628 * Add the new symbol. It should be noted that all 1629 * symbols added by the mapfile start out with global 1630 * scope, thus they will fall through the normal symbol 1631 * resolution process. Symbols defined as locals will 1632 * be reduced in scope after all input file processing. 1633 */ 1634 /* LINTED */ 1635 hash = (Word)elf_hash(_name); 1636 DBG_CALL(Dbg_map_version(ofl->ofl_lml, name, _name, 1637 scope)); 1638 if ((sdp = ld_sym_find(_name, hash, &where, 1639 ofl)) == NULL) { 1640 if ((sym = libld_calloc(sizeof (Sym), 1)) == 0) 1641 return (S_ERROR); 1642 1643 /* 1644 * Make sure any parent or external declarations 1645 * fall back to references. 1646 */ 1647 if (sym_flags & (FLG_SY_PARENT | FLG_SY_EXTERN)) 1648 sym->st_shndx = shndx = SHN_UNDEF; 1649 else 1650 sym->st_shndx = (Half)shndx; 1651 1652 sym->st_value = value; 1653 sym->st_size = size; 1654 sym->st_info = ELF_ST_INFO(STB_GLOBAL, type); 1655 1656 if ((sdp = ld_sym_enter(_name, sym, hash, ifl, 1657 ofl, 0, shndx, sym_flags, sym_flags1, 1658 &where)) == (Sym_desc *)S_ERROR) 1659 return (S_ERROR); 1660 1661 sdp->sd_flags &= ~FLG_SY_CLEAN; 1662 1663 /* 1664 * Identify any references. FLG_SY_MAPREF is 1665 * turned off once a relocatable object with 1666 * the same symbol is found, thus the existance 1667 * of FLG_SY_MAPREF at symbol validation is 1668 * used to flag undefined/mispelt entries. 1669 */ 1670 if (sym->st_shndx == SHN_UNDEF) 1671 sdp->sd_flags |= 1672 (FLG_SY_MAPREF | FLG_SY_GLOBREF); 1673 1674 } else { 1675 int conflict = 0; 1676 1677 sym = sdp->sd_sym; 1678 1679 /* 1680 * If this symbol already exists, make sure this 1681 * definition doesn't conflict with the former. 1682 * Provided it doesn't, multiple definitions can 1683 * augment each other. 1684 */ 1685 if (sym->st_value) { 1686 if (value && (sym->st_value != value)) 1687 conflict = 1; 1688 } else 1689 sym->st_value = value; 1690 1691 if (sym->st_size) { 1692 if (size && (sym->st_size != size)) 1693 conflict = 2; 1694 } else 1695 sym->st_size = size; 1696 1697 if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) { 1698 if ((type != STT_NOTYPE) && 1699 (ELF_ST_TYPE(sym->st_info) != type)) 1700 conflict = 3; 1701 } else 1702 sym->st_info = 1703 ELF_ST_INFO(STB_GLOBAL, type); 1704 1705 if (sym->st_shndx != SHN_UNDEF) { 1706 if ((shndx != SHN_UNDEF) && 1707 (sym->st_shndx != shndx)) 1708 conflict = 4; 1709 } else 1710 sdp->sd_shndx = sym->st_shndx = shndx; 1711 1712 if ((sdp->sd_flags1 & FLG_SY1_LOCL) && 1713 ((scope != FLG_SCOPE_LOCL) && 1714 (scope != FLG_SCOPE_ELIM))) 1715 conflict = 5; 1716 if ((sdp->sd_flags1 & FLG_SY1_GLOB) && 1717 (scope != FLG_SCOPE_GLOB) && 1718 (scope != FLG_SCOPE_SYMB)) 1719 conflict = 6; 1720 if ((sdp->sd_flags1 & FLG_SY1_GLOB) && 1721 (sdp->sd_aux->sa_overndx != 1722 VER_NDX_GLOBAL) && 1723 (vdp->vd_ndx != VER_NDX_GLOBAL) && 1724 (sdp->sd_aux->sa_overndx != vdp->vd_ndx)) 1725 conflict = 7; 1726 1727 if (conflict) { 1728 eprintf(ofl->ofl_lml, ERR_FATAL, 1729 MSG_INTL(MSG_MAP_SYMDEF), mapfile, 1730 EC_XWORD(Line_num), demangle(_name), 1731 sdp->sd_file->ifl_name); 1732 return (S_ERROR); 1733 } 1734 1735 /* 1736 * If this mapfile entry supplies a definition, 1737 * indicate that the symbol is now used. 1738 */ 1739 if (shndx != SHN_UNDEF) 1740 sdp->sd_flags |= FLG_SY_MAPUSED; 1741 } 1742 1743 /* 1744 * Indicate the new symbols scope. 1745 */ 1746 if (scope == FLG_SCOPE_LOCL) { 1747 sdp->sd_flags1 |= FLG_SY1_LOCL; 1748 sdp->sd_sym->st_other = STV_HIDDEN; 1749 if (ofl->ofl_flags1 & FLG_OF1_REDLSYM) 1750 sdp->sd_flags1 |= FLG_SY1_ELIM; 1751 1752 } else if (scope == FLG_SCOPE_ELIM) { 1753 sdp->sd_flags1 |= (FLG_SY1_LOCL | FLG_SY1_ELIM); 1754 sdp->sd_sym->st_other = STV_HIDDEN; 1755 } else { 1756 sdp->sd_flags |= sym_flags; 1757 sdp->sd_flags1 |= (sym_flags1 | FLG_SY1_GLOB); 1758 1759 if (scope == FLG_SCOPE_SYMB) { 1760 sdp->sd_flags1 |= FLG_SY1_PROT; 1761 sdp->sd_sym->st_other = STV_PROTECTED; 1762 } 1763 1764 /* 1765 * Record the present version index for later 1766 * potential versioning. 1767 */ 1768 if ((sdp->sd_aux->sa_overndx == 0) || 1769 (sdp->sd_aux->sa_overndx == VER_NDX_GLOBAL)) 1770 sdp->sd_aux->sa_overndx = vdp->vd_ndx; 1771 vdp->vd_flags |= FLG_VER_REFER; 1772 } 1773 1774 /* 1775 * If we've encountered a symbol definition simulate 1776 * that an input file has been processed - this allows 1777 * things like filters to be created purely from a 1778 * mapfile. 1779 */ 1780 if (type != STT_NOTYPE) 1781 ofl->ofl_objscnt++; 1782 DBG_CALL(Dbg_map_symbol(ofl, sdp)); 1783 1784 /* 1785 * If this symbol has an associated filtee, record the 1786 * filtee string and associate the string index with the 1787 * symbol. This is used later to associate the syminfo 1788 * information with the necessary .dynamic entry. 1789 */ 1790 if (filter && (filtee == 0)) { 1791 eprintf(ofl->ofl_lml, ERR_FATAL, 1792 MSG_INTL(MSG_MAP_NOFILTER), mapfile, 1793 EC_XWORD(Line_num), _name); 1794 return (S_ERROR); 1795 } 1796 1797 if (filtee) { 1798 Dfltr_desc * dftp; 1799 Sfltr_desc sft; 1800 Aliste off = 0, _off; 1801 1802 /* 1803 * Make sure we don't duplicate any filtee 1804 * strings, and create a new descriptor if 1805 * necessary. 1806 */ 1807 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, _off, 1808 dftp)) { 1809 if ((dftflag != dftp->dft_flag) || 1810 (strcmp(dftp->dft_str, filtee))) 1811 continue; 1812 off = _off; 1813 break; 1814 } 1815 if (off == 0) { 1816 Dfltr_desc dft; 1817 1818 dft.dft_str = filtee; 1819 dft.dft_flag = dftflag; 1820 dft.dft_ndx = 0; 1821 1822 if ((dftp = 1823 alist_append(&(ofl->ofl_dtsfltrs), 1824 &dft, sizeof (Dfltr_desc), 1825 AL_CNT_DFLTR)) == 0) 1826 return (S_ERROR); 1827 1828 off = (Aliste)((char *)dftp - 1829 (char *)ofl->ofl_dtsfltrs); 1830 } 1831 1832 /* 1833 * Create a new filter descriptor for this 1834 * symbol. 1835 */ 1836 sft.sft_sdp = sdp; 1837 sft.sft_off = off; 1838 1839 if (alist_append(&(ofl->ofl_symfltrs), 1840 &sft, sizeof (Sfltr_desc), 1841 AL_CNT_SFLTR) == 0) 1842 return (S_ERROR); 1843 } 1844 break; 1845 1846 default: 1847 eprintf(ofl->ofl_lml, ERR_FATAL, 1848 MSG_INTL(MSG_MAP_EXPSCOL), mapfile, 1849 EC_XWORD(Line_num)); 1850 return (S_ERROR); 1851 } 1852 } 1853 1854 /* 1855 * Determine if any version references are provided after the close 1856 * bracket. 1857 */ 1858 while ((tok = gettoken(ofl, mapfile)) != TK_SEMICOLON) { 1859 Ver_desc *_vdp; 1860 char *_name; 1861 1862 if (tok != TK_STRING) { 1863 /* LINTED */ 1864 if (tok != (Token)S_ERROR) 1865 eprintf(ofl->ofl_lml, ERR_FATAL, 1866 MSG_INTL(MSG_MAP_EXPVERS), mapfile, 1867 EC_XWORD(Line_num)); 1868 return (S_ERROR); 1869 } 1870 1871 name = Start_tok; 1872 if (vdp->vd_ndx == VER_NDX_GLOBAL) { 1873 eprintf(ofl->ofl_lml, ERR_WARNING, 1874 MSG_INTL(MSG_MAP_UNEXDEP), mapfile, 1875 EC_XWORD(Line_num), name); 1876 continue; 1877 } 1878 1879 /* 1880 * Generate a new version descriptor if it doesn't already 1881 * exist. 1882 */ 1883 /* LINTED */ 1884 hash = (Word)elf_hash(name); 1885 if ((_vdp = ld_vers_find(name, hash, &ofl->ofl_verdesc)) == 0) { 1886 if ((_name = libld_malloc(strlen(name) + 1)) == 0) 1887 return (S_ERROR); 1888 (void) strcpy(_name, name); 1889 1890 if ((_vdp = ld_vers_desc(_name, hash, 1891 &ofl->ofl_verdesc)) == (Ver_desc *)S_ERROR) 1892 return (S_ERROR); 1893 } 1894 1895 /* 1896 * Add the new version descriptor to the parent version 1897 * descriptors reference list. Indicate the version descriptors 1898 * first reference (used for error disgnostics if undefined 1899 * version dependencies remain). 1900 */ 1901 if (ld_vers_find(name, hash, &vdp->vd_deps) == 0) 1902 if (list_appendc(&vdp->vd_deps, _vdp) == 0) 1903 return (S_ERROR); 1904 1905 if (_vdp->vd_ref == 0) 1906 _vdp->vd_ref = vdp; 1907 } 1908 return (1); 1909 } 1910 1911 /* 1912 * Sort the segment list by increasing virtual address. 1913 */ 1914 uintptr_t 1915 ld_sort_seg_list(Ofl_desc *ofl) 1916 { 1917 List seg1, seg2; 1918 Listnode *lnp1, *lnp2, *lnp3; 1919 Sg_desc *sgp1, *sgp2; 1920 1921 seg1.head = seg1.tail = seg2.head = seg2.tail = NULL; 1922 1923 /* 1924 * Add the .phdr and .interp segments to our list. These segments must 1925 * occur before any PT_LOAD segments (refer exec/elf/elf.c). 1926 */ 1927 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp1)) { 1928 Word type = sgp1->sg_phdr.p_type; 1929 1930 if ((type == PT_PHDR) || (type == PT_INTERP)) 1931 if (list_appendc(&seg1, sgp1) == 0) 1932 return (S_ERROR); 1933 } 1934 1935 /* 1936 * Add the loadable segments to another list in sorted order. 1937 */ 1938 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp1)) { 1939 DBG_CALL(Dbg_map_sort_orig(ofl->ofl_lml, sgp1)); 1940 if (sgp1->sg_phdr.p_type != PT_LOAD) 1941 continue; 1942 if (!(sgp1->sg_flags & FLG_SG_VADDR) || 1943 (sgp1->sg_flags & FLG_SG_EMPTY)) { 1944 if (list_appendc(&seg2, sgp1) == 0) 1945 return (S_ERROR); 1946 } else { 1947 if (seg2.head == NULL) { 1948 if (list_appendc(&seg2, sgp1) == 0) 1949 return (S_ERROR); 1950 continue; 1951 } 1952 lnp3 = NULL; 1953 for (LIST_TRAVERSE(&seg2, lnp2, sgp2)) { 1954 if (!(sgp2->sg_flags & FLG_SG_VADDR) || 1955 (sgp2->sg_flags & FLG_SG_EMPTY)) { 1956 if (lnp3 == NULL) { 1957 if (list_prependc(&seg2, 1958 sgp1) == 0) 1959 return (S_ERROR); 1960 } else { 1961 if (list_insertc(&seg2, 1962 sgp1, lnp3) == 0) 1963 return (S_ERROR); 1964 } 1965 lnp3 = NULL; 1966 break; 1967 } 1968 if (sgp1->sg_phdr.p_vaddr < 1969 sgp2->sg_phdr.p_vaddr) { 1970 if (lnp3 == NULL) { 1971 if (list_prependc(&seg2, 1972 sgp1) == 0) 1973 return (S_ERROR); 1974 } else { 1975 if (list_insertc(&seg2, 1976 sgp1, lnp3) == 0) 1977 return (S_ERROR); 1978 } 1979 lnp3 = NULL; 1980 break; 1981 } else if (sgp1->sg_phdr.p_vaddr > 1982 sgp2->sg_phdr.p_vaddr) { 1983 lnp3 = lnp2; 1984 } else { 1985 eprintf(ofl->ofl_lml, ERR_FATAL, 1986 MSG_INTL(MSG_MAP_SEGSAME), 1987 sgp1->sg_name, sgp2->sg_name); 1988 return (S_ERROR); 1989 } 1990 } 1991 if (lnp3 != NULL) 1992 if (list_appendc(&seg2, sgp1) == 0) 1993 return (S_ERROR); 1994 } 1995 } 1996 1997 /* 1998 * add the sorted loadable segments to our list 1999 */ 2000 for (LIST_TRAVERSE(&seg2, lnp1, sgp1)) { 2001 if (list_appendc(&seg1, sgp1) == 0) 2002 return (S_ERROR); 2003 } 2004 2005 /* 2006 * add all other segments to our list 2007 */ 2008 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp1)) { 2009 Word type = sgp1->sg_phdr.p_type; 2010 2011 if ((type != PT_PHDR) && (type != PT_INTERP) && 2012 (type != PT_LOAD)) 2013 if (list_appendc(&seg1, sgp1) == 0) 2014 return (S_ERROR); 2015 } 2016 ofl->ofl_segs.head = ofl->ofl_segs.tail = NULL; 2017 2018 /* 2019 * Now rebuild the original list and process all of the 2020 * segment/section ordering information if present. 2021 */ 2022 for (LIST_TRAVERSE(&seg1, lnp1, sgp1)) { 2023 DBG_CALL(Dbg_map_sort_fini(ofl->ofl_lml, sgp1)); 2024 if (list_appendc(&ofl->ofl_segs, sgp1) == 0) 2025 return (S_ERROR); 2026 } 2027 return (1); 2028 } 2029 2030 2031 2032 /* 2033 * Parse the mapfile. 2034 */ 2035 uintptr_t 2036 ld_map_parse(const char *mapfile, Ofl_desc *ofl) 2037 { 2038 struct stat stat_buf; /* stat of mapfile */ 2039 int mapfile_fd; /* descriptor for mapfile */ 2040 Listnode *lnp1; /* node pointer */ 2041 Listnode *lnp2; /* node pointer */ 2042 Sg_desc *sgp1; /* seg descriptor being manipulated */ 2043 Sg_desc *sgp2; /* temp segment descriptor pointer */ 2044 Ent_desc *enp; /* Segment entrance criteria. */ 2045 Token tok; /* current token. */ 2046 Listnode *e_next = NULL; 2047 /* next place for entrance criterion */ 2048 Boolean new_segment; /* If true, defines new segment. */ 2049 char *name; 2050 static int num_stack = 0; /* number of stack segment */ 2051 int err; 2052 2053 DBG_CALL(Dbg_map_parse(ofl->ofl_lml, mapfile)); 2054 2055 /* 2056 * Determine if we're dealing with a file or a directory. 2057 */ 2058 if (stat(mapfile, &stat_buf) == -1) { 2059 err = errno; 2060 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_STAT), 2061 mapfile, strerror(err)); 2062 return (S_ERROR); 2063 } 2064 if (S_ISDIR(stat_buf.st_mode)) { 2065 DIR *dirp; 2066 struct dirent *denp; 2067 2068 /* 2069 * Open the directory and interpret each visible file as a 2070 * mapfile. 2071 */ 2072 if ((dirp = opendir(mapfile)) == 0) 2073 return (1); 2074 2075 while ((denp = readdir(dirp)) != NULL) { 2076 char path[PATH_MAX]; 2077 2078 /* 2079 * Ignore any hidden filenames. Construct the full 2080 * pathname to the new mapfile. 2081 */ 2082 if (*denp->d_name == '.') 2083 continue; 2084 (void) snprintf(path, PATH_MAX, MSG_ORIG(MSG_STR_PATH), 2085 mapfile, denp->d_name); 2086 if (ld_map_parse(path, ofl) == S_ERROR) 2087 return (S_ERROR); 2088 } 2089 (void) closedir(dirp); 2090 return (1); 2091 } else if (!S_ISREG(stat_buf.st_mode)) { 2092 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_NOTREG), 2093 mapfile); 2094 return (S_ERROR); 2095 } 2096 2097 /* 2098 * We read the entire mapfile into memory. 2099 */ 2100 if ((Mapspace = libld_malloc(stat_buf.st_size + 1)) == 0) 2101 return (S_ERROR); 2102 if ((mapfile_fd = open(mapfile, O_RDONLY)) == -1) { 2103 err = errno; 2104 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), 2105 mapfile, strerror(err)); 2106 return (S_ERROR); 2107 } 2108 2109 if (read(mapfile_fd, Mapspace, stat_buf.st_size) != stat_buf.st_size) { 2110 err = errno; 2111 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_READ), 2112 mapfile, strerror(err)); 2113 return (S_ERROR); 2114 } 2115 Mapspace[stat_buf.st_size] = '\0'; 2116 nextchr = Mapspace; 2117 2118 /* 2119 * Set up any global variables, the line number counter and file name. 2120 */ 2121 Line_num = 1; 2122 2123 /* 2124 * We now parse the mapfile until the gettoken routine returns EOF. 2125 */ 2126 while ((tok = gettoken(ofl, mapfile)) != TK_EOF) { 2127 int ndx = -1; 2128 2129 /* 2130 * Don't know which segment yet. 2131 */ 2132 sgp1 = NULL; 2133 2134 /* 2135 * At this point we are at the beginning of a line, and the 2136 * variable `Start_tok' points to the first string on the line. 2137 * All mapfile entries start with some string token except it 2138 * is possible for a scoping definition to start with `{'. 2139 */ 2140 if (tok == TK_LEFTBKT) { 2141 if (map_version(mapfile, (char *)0, ofl) == S_ERROR) 2142 return (S_ERROR); 2143 continue; 2144 } 2145 if (tok != TK_STRING) { 2146 /* LINTED */ 2147 if (tok != (Token)S_ERROR) 2148 eprintf(ofl->ofl_lml, ERR_FATAL, 2149 MSG_INTL(MSG_MAP_EXPSEGNAM), mapfile, 2150 EC_XWORD(Line_num)); 2151 return (S_ERROR); 2152 } 2153 2154 /* 2155 * Save the initial token. 2156 */ 2157 if ((name = libld_malloc(strlen(Start_tok) + 1)) == 0) 2158 return (S_ERROR); 2159 (void) strcpy(name, Start_tok); 2160 2161 /* 2162 * Now check the second character on the line. The special `-' 2163 * and `{' characters do not involve any segment manipulation so 2164 * we handle them first. 2165 */ 2166 if ((tok = gettoken(ofl, mapfile)) == TK_DASH) { 2167 if (map_dash(mapfile, name, ofl) == S_ERROR) 2168 return (S_ERROR); 2169 continue; 2170 } 2171 if (tok == TK_LEFTBKT) { 2172 if (map_version(mapfile, name, ofl) == S_ERROR) 2173 return (S_ERROR); 2174 continue; 2175 } 2176 2177 /* 2178 * If we're here we need to interpret the first string as a 2179 * segment name. Find the segment named in the token. 2180 */ 2181 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp2)) { 2182 ndx++; 2183 if (strcmp(sgp2->sg_name, name) == 0) { 2184 sgp1 = sgp2; 2185 sgp2->sg_flags &= ~FLG_SG_DISABLED; 2186 new_segment = FALSE; 2187 break; 2188 } 2189 } 2190 2191 /* 2192 * If the second token is a '|' then we had better 2193 * of found a segment. It is illegal to perform 2194 * section within segment ordering before the segment 2195 * has been declared. 2196 */ 2197 if (tok == TK_PIPE) { 2198 if (sgp1 == NULL) { 2199 eprintf(ofl->ofl_lml, ERR_FATAL, 2200 MSG_INTL(MSG_MAP_SECINSEG), mapfile, 2201 EC_XWORD(Line_num), name); 2202 return (S_ERROR); 2203 } else { 2204 if (map_pipe(ofl, mapfile, sgp1) == S_ERROR) 2205 return (S_ERROR); 2206 continue; 2207 } 2208 } 2209 2210 /* 2211 * If segment is still NULL then it does not exist. Create a 2212 * new segment, and leave its values as 0 so that map_equal() 2213 * can detect changing attributes. 2214 */ 2215 if (sgp1 == NULL) { 2216 if ((sgp1 = libld_calloc(sizeof (Sg_desc), 2217 1)) == 0) 2218 return (S_ERROR); 2219 sgp1->sg_phdr.p_type = PT_NULL; 2220 sgp1->sg_name = name; 2221 new_segment = TRUE; 2222 ndx = -1; 2223 } 2224 2225 if ((strcmp(sgp1->sg_name, MSG_ORIG(MSG_STR_INTERP)) == 0) || 2226 (strcmp(sgp1->sg_name, MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 2227 0)) { 2228 eprintf(ofl->ofl_lml, ERR_FATAL, 2229 MSG_INTL(MSG_MAP_SEGRESV), mapfile, 2230 EC_XWORD(Line_num)); 2231 return (S_ERROR); 2232 } 2233 2234 /* 2235 * Now check the second token from the input line. 2236 */ 2237 if (tok == TK_EQUAL) { 2238 if (strcmp(sgp1->sg_name, 2239 MSG_ORIG(MSG_STR_HWCAP_1)) == 0) { 2240 if (map_cap(mapfile, CA_SUNW_HW_1, 2241 ofl) == S_ERROR) 2242 return (S_ERROR); 2243 DBG_CALL(Dbg_cap_mapfile(ofl->ofl_lml, 2244 CA_SUNW_HW_1, ofl->ofl_hwcap_1, M_MACH)); 2245 2246 } else if (strcmp(sgp1->sg_name, 2247 MSG_ORIG(MSG_STR_SFCAP_1)) == 0) { 2248 if (map_cap(mapfile, CA_SUNW_SF_1, 2249 ofl) == S_ERROR) 2250 return (S_ERROR); 2251 DBG_CALL(Dbg_cap_mapfile(ofl->ofl_lml, 2252 CA_SUNW_SF_1, ofl->ofl_sfcap_1, M_MACH)); 2253 2254 } else { 2255 if (map_equal(mapfile, sgp1, ofl) == S_ERROR) 2256 return (S_ERROR); 2257 ofl->ofl_flags |= FLG_OF_SEGSORT; 2258 DBG_CALL(Dbg_map_set_equal(new_segment)); 2259 } 2260 } else if (tok == TK_COLON) { 2261 /* 2262 * If this is an existing segment reservation, sections 2263 * can't be assigned to it. 2264 */ 2265 if ((new_segment == FALSE) && 2266 (sgp1->sg_flags & FLG_SG_EMPTY)) { 2267 eprintf(ofl->ofl_lml, ERR_FATAL, 2268 MSG_INTL(MSG_MAP_SEGEMPSEC), mapfile, 2269 EC_XWORD(Line_num)); 2270 return (S_ERROR); 2271 } 2272 2273 /* 2274 * We are looking at a new entrance criteria line. 2275 * Note that entrance criteria are added in the order 2276 * they are found in the map file, but are placed 2277 * before any default criteria. 2278 */ 2279 if ((enp = libld_calloc(sizeof (Ent_desc), 1)) == 0) 2280 return (S_ERROR); 2281 enp->ec_segment = sgp1; 2282 if (e_next == NULL) { 2283 if ((e_next = list_prependc(&ofl->ofl_ents, 2284 enp)) == 0) 2285 return (S_ERROR); 2286 } else { 2287 if ((e_next = list_insertc(&ofl->ofl_ents, 2288 enp, e_next)) == 0) 2289 return (S_ERROR); 2290 } 2291 2292 if (map_colon(ofl, mapfile, enp) == S_ERROR) 2293 return (S_ERROR); 2294 ofl->ofl_flags |= FLG_OF_SEGSORT; 2295 DBG_CALL(Dbg_map_ent(ofl->ofl_lml, new_segment, 2296 enp, ofl)); 2297 } else if (tok == TK_ATSIGN) { 2298 if (map_atsign(mapfile, sgp1, ofl) == S_ERROR) 2299 return (S_ERROR); 2300 DBG_CALL(Dbg_map_set_atsign(new_segment)); 2301 } else { 2302 /* LINTED */ 2303 if (tok != (Token)S_ERROR) { 2304 eprintf(ofl->ofl_lml, ERR_FATAL, 2305 MSG_INTL(MSG_MAP_EXPEQU), mapfile, 2306 EC_XWORD(Line_num)); 2307 return (S_ERROR); 2308 } 2309 } 2310 2311 /* 2312 * Having completed parsing an entry in the map file determine 2313 * if the segment to which it applies is new. 2314 */ 2315 if (new_segment) { 2316 int src_type, dst_type; 2317 2318 /* 2319 * If specific fields have not been supplied via 2320 * map_equal(), make sure defaults are supplied. 2321 */ 2322 if (sgp1->sg_phdr.p_type == PT_NULL) { 2323 sgp1->sg_phdr.p_type = PT_LOAD; 2324 sgp1->sg_flags |= FLG_SG_TYPE; 2325 } 2326 if ((sgp1->sg_phdr.p_type == PT_LOAD) && 2327 (!(sgp1->sg_flags & FLG_SG_FLAGS))) { 2328 sgp1->sg_phdr.p_flags = PF_R + PF_W + PF_X; 2329 sgp1->sg_flags |= FLG_SG_FLAGS; 2330 } 2331 2332 /* 2333 * Determine where the new segment should be inserted 2334 * in the seg_desc[] list. Presently the user can 2335 * only add a LOAD or NOTE segment. Note that these 2336 * segments must be added after any PT_PHDR and 2337 * PT_INTERP (refer Generic ABI, Page 5-4). 2338 */ 2339 switch (sgp1->sg_phdr.p_type) { 2340 case PT_LOAD: 2341 if (sgp1->sg_flags & FLG_SG_EMPTY) 2342 src_type = 4; 2343 else 2344 src_type = 3; 2345 break; 2346 case PT_SUNWSTACK: 2347 src_type = 8; 2348 if (++num_stack >= 2) { 2349 /* 2350 * Currently the number of sunw_stack 2351 * segment is limited to 1. 2352 */ 2353 eprintf(ofl->ofl_lml, ERR_WARNING, 2354 MSG_INTL(MSG_MAP_NOSTACK2), 2355 mapfile, EC_XWORD(Line_num)); 2356 continue; 2357 } 2358 break; 2359 case PT_NOTE: 2360 src_type = 9; 2361 break; 2362 default: 2363 eprintf(ofl->ofl_lml, ERR_FATAL, 2364 MSG_INTL(MSG_MAP_UNKSEGTYP), mapfile, 2365 EC_XWORD(Line_num), 2366 EC_WORD(sgp1->sg_phdr.p_type)); 2367 return (S_ERROR); 2368 } 2369 lnp2 = NULL; 2370 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp2)) { 2371 ndx++; 2372 switch (sgp2->sg_phdr.p_type) { 2373 case PT_PHDR: 2374 dst_type = 0; 2375 break; 2376 case PT_INTERP: 2377 dst_type = 1; 2378 break; 2379 case PT_SUNWCAP: 2380 dst_type = 2; 2381 break; 2382 case PT_LOAD: 2383 dst_type = 3; 2384 break; 2385 case PT_DYNAMIC: 2386 dst_type = 5; 2387 break; 2388 case PT_SUNWDTRACE: 2389 dst_type = 6; 2390 break; 2391 case PT_SHLIB: 2392 dst_type = 7; 2393 break; 2394 case PT_SUNWSTACK: 2395 dst_type = 8; 2396 break; 2397 case PT_NOTE: 2398 dst_type = 9; 2399 break; 2400 case PT_SUNWBSS: 2401 dst_type = 10; 2402 break; 2403 case PT_TLS: 2404 dst_type = 11; 2405 break; 2406 case PT_NULL: 2407 dst_type = 12; 2408 break; 2409 default: 2410 eprintf(ofl->ofl_lml, ERR_FATAL, 2411 MSG_INTL(MSG_MAP_UNKSEGTYP), 2412 mapfile, EC_XWORD(Line_num), 2413 EC_WORD(sgp2->sg_phdr.p_type)); 2414 return (S_ERROR); 2415 } 2416 if (src_type <= dst_type) { 2417 if (lnp2 == NULL) { 2418 if (list_prependc( 2419 &ofl->ofl_segs, sgp1) == 0) 2420 return (S_ERROR); 2421 } else { 2422 if (list_insertc(&ofl->ofl_segs, 2423 sgp1, lnp2) == 0) 2424 return (S_ERROR); 2425 } 2426 break; 2427 } 2428 lnp2 = lnp1; 2429 } 2430 } 2431 DBG_CALL(Dbg_map_seg(ofl, ndx, sgp1)); 2432 } 2433 2434 /* 2435 * If the output file is a static file without an interpreter 2436 * and if any virtual address is specified, then assume $n flag 2437 * for backward compatiblity. 2438 */ 2439 if (!(ofl->ofl_flags & FLG_OF_DYNAMIC) && 2440 !(ofl->ofl_flags & FLG_OF_RELOBJ) && 2441 !(ofl->ofl_osinterp) && 2442 (ofl->ofl_flags1 & FLG_OF1_VADDR)) 2443 ofl->ofl_flags1 |= FLG_OF1_NOHDR; 2444 2445 /* 2446 * If the output file is a relocatable file, 2447 * then ?N have no effect. Knock it off. 2448 */ 2449 if (ofl->ofl_flags & FLG_OF_RELOBJ) 2450 ofl->ofl_flags1 &= ~FLG_OF1_NOHDR; 2451 2452 return (1); 2453 } 2454