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 (c) 2010, Oracle and/or its affiliates. All rights reserved. 27 * 28 * Copyright 2019 Joyent, Inc. 29 */ 30 31 /* 32 * Map file parsing (Shared Core Code). 33 */ 34 #include <fcntl.h> 35 #include <stdio.h> 36 #include <unistd.h> 37 #include <sys/stat.h> 38 #include <errno.h> 39 #include <limits.h> 40 #include <dirent.h> 41 #include <ctype.h> 42 #include <debug.h> 43 #include "msg.h" 44 #include "_libld.h" 45 #include "_map.h" 46 47 /* 48 * There are two styles of mapfile supported by the link-editor: 49 * 50 * 1) The original System V defined syntax, as augmented at Sun 51 * from Solaris 2.0 through Solaris 10. This style is also known 52 * as version 1. 53 * 54 * 2) A newer syntax, currently at version 2. 55 * 56 * The original syntax uses special characters (=, :, -, |, etc) as 57 * operators to indicate the operation being specified. Over the years, 58 * this syntax has been problematic: 59 * 60 * 1) Too cryptic: It's hard for people to remember which character 61 * means what. 62 * 63 * 2) Limited expansion potential: There only a few special characters 64 * available on the keyboard for new features, and it is difficult to 65 * add options to existing ones. 66 * 67 * Adding new features into this framework (2) have the effect of 68 * making the syntax even more cryptic (1). The newer syntax addresses 69 * these issues by moving to an extendible identifier based syntax that 70 * allows new features to be added without complicating old ones. 71 * 72 * The new syntax uses the following terminology: 73 * 74 * - Control directives are the directives that start with a '$'. 75 * They control how the mapfile is interpreted. We use the 'cdir_' 76 * prefix on functions and variables related to these directives. 77 * 78 * - Conditional Expressions are the expressions found in $if and $elif 79 * control directives. They evaluate to boolean true/false values. 80 * We use the 'cexp_' prefix for functions and variables related to 81 * these expressions. 82 * 83 * - Regular Directives are names (SYMBOL, VERSION, etc) that convey 84 * directions to the link-editor for building the output object. 85 * 86 * This file contains core code used by both mapfile styles: File management, 87 * lexical analysis, and other shared core functionality. It also contains 88 * the code for control directives, as they are intrinsically part of 89 * lexical analysis --- this is disabled when processing Sysv mapfiles. 90 */ 91 92 /* 93 * We use a stack of cdir_level_t structs to manage $if/$elif/$else/$endif 94 * processing. At each level, we keep track of the information needed to 95 * determine whether or not to process nested input lines or skip them, 96 * along with information needed to report errors. 97 */ 98 typedef struct { 99 Lineno cdl_if_lineno; /* Line number of opening $if */ 100 Lineno cdl_else_lineno; /* 0, or line on which $else seen */ 101 int cdl_done; /* True if no longer accepts input */ 102 int cdl_pass; /* True if currently accepting input */ 103 } cdir_level_t; 104 105 /* Operators in the expressions accepted by $if/$elif */ 106 typedef enum { 107 CEXP_OP_NONE, /* Not an operator */ 108 CEXP_OP_AND, /* && */ 109 CEXP_OP_OR, /* || */ 110 CEXP_OP_NEG, /* ! */ 111 CEXP_OP_OPAR, /* ( */ 112 CEXP_OP_CPAR /* ) */ 113 } cexp_op_t; 114 115 /* 116 * Type of conditional expression identifier AVL tree nodes 117 */ 118 typedef struct cexp_name_node { 119 avl_node_t ceid_avlnode; /* AVL book-keeping */ 120 const char *ceid_name; /* boolean identifier name */ 121 } cexp_id_node_t; 122 123 124 /* 125 * Declare a "stack" type, containing a pointer to data, a count of 126 * allocated, and currently used items in the stack. The data type 127 * is specified as the _type argument. 128 */ 129 #define STACK(_type) \ 130 struct { \ 131 _type *stk_s; /* Stack array */ \ 132 size_t stk_n; /* Current stack depth */ \ 133 size_t stk_n_alloc; /* # of elements pointed at by s */ \ 134 } 135 136 /* 137 * The following type represents a "generic" stack, where the data 138 * type is (void). This type is never instantiated. However, it has 139 * the same struct layout as any other STACK(), and is therefore a good 140 * generic type that can be used for stack_resize(). 141 */ 142 typedef STACK(void) generic_stack_t; 143 144 /* 145 * Ensure that the stack has enough room to push one more item 146 */ 147 #define STACK_RESERVE(_stack, _n_default) \ 148 (((_stack).stk_n < (_stack).stk_n_alloc) || \ 149 stack_resize((generic_stack_t *)&(_stack).stk_s, _n_default, \ 150 sizeof (*(_stack).stk_s))) 151 152 /* 153 * Reset a stack to empty. 154 */ 155 #define STACK_RESET(_stack) (_stack).stk_n = 0; 156 157 /* 158 * True if stack is empty, False otherwise. 159 */ 160 #define STACK_IS_EMPTY(_stack) ((_stack).stk_n == 0) 161 162 /* 163 * Push a value onto a stack. Caller must ensure that stack has room. 164 * This macro is intended to be used as the LHS of an assignment, the 165 * RHS of which is the value: 166 * 167 * STACK_PUSH(stack) = value; 168 */ 169 #define STACK_PUSH(_stack) (_stack).stk_s[(_stack).stk_n++] 170 171 /* 172 * Pop a value off a stack. Caller must ensure 173 * that stack is not empty. 174 */ 175 #define STACK_POP(_stack) ((_stack).stk_s[--(_stack).stk_n]) 176 177 /* 178 * Access top element on stack without popping. Caller must ensure 179 * that stack is not empty. 180 */ 181 #define STACK_TOP(_stack) (((_stack).stk_s)[(_stack).stk_n - 1]) 182 183 /* 184 * Initial sizes used for the stacks: The stacks are allocated on demand 185 * to these sizes, and then doubled as necessary until they are large enough. 186 * 187 * The ideal size would be large enough that only a single allocation 188 * occurs, and our defaults should generally have that effect. However, 189 * in doing so, we run the risk of a latent error in the resize code going 190 * undetected until triggered by a large task in the field. For this reason, 191 * we set the sizes to the smallest size possible when compiled for debug. 192 */ 193 #ifdef DEBUG 194 #define CDIR_STACK_INIT 1 195 #define CEXP_OP_STACK_INIT 1 196 #define CEXP_VAL_STACK_INIT 1 197 #else 198 #define CDIR_STACK_INIT 16 199 #define CEXP_OP_STACK_INIT 8 200 #define CEXP_VAL_STACK_INIT (CEXP_OP_STACK_INIT * 2) /* 2 vals per binop */ 201 #endif 202 203 204 /* 205 * Persistent state maintained by map module in between calls. 206 * 207 * This is kept as static file scope data, because it is only used 208 * when libld is called by ld, and not by rtld. If that should change, 209 * the code is designed so that it can become reentrant easily: 210 * 211 * - Add a pointer to the output descriptor to a structure of this type, 212 * allocated dynamically on the first call to ld_map_parse(). 213 * - Change all references to lms to instead reference the pointer in 214 * the output descriptor. 215 * 216 * Until then, it is simpler not to expose these details. 217 */ 218 typedef struct { 219 int lms_cdir_valid; /* Allow control dir. on entry to gettoken() */ 220 STACK(cdir_level_t) lms_cdir_stack; /* Conditional input level */ 221 STACK(cexp_op_t) lms_cexp_op_stack; /* Cond. expr operators */ 222 STACK(uchar_t) lms_cexp_val_stack; /* Cond. expr values */ 223 avl_tree_t *lms_cexp_id; 224 } ld_map_state_t; 225 static ld_map_state_t lms; 226 227 228 /* 229 * Version 1 (SysV) syntax dispatch table for ld_map_gettoken(). For each 230 * of the 7-bit ASCII characters, determine how the lexical analyzer 231 * should behave. 232 * 233 * This table must be kept in sync with tkid_attr[] below. 234 * 235 * Identifier Note: 236 * The Linker and Libraries Guide states that the original syntax uses 237 * C identifier rules, allowing '.' to be treated as a letter. However, 238 * the implementation is considerably looser than that: Any character 239 * with an ASCII code (0-127) which is printable and not used to start 240 * another token is allowed to start an identifier, and they are terminated 241 * by any of: space, double quote, tab, newline, ':', ';', '=', or '#'. 242 * The original code has been replaced, but this table encodes the same 243 * rules, to ensure backward compatibility. 244 */ 245 static const mf_tokdisp_t gettok_dispatch_v1 = { 246 TK_OP_EOF, /* 0 - NUL */ 247 TK_OP_ILLCHR, /* 1 - SOH */ 248 TK_OP_ILLCHR, /* 2 - STX */ 249 TK_OP_ILLCHR, /* 3 - ETX */ 250 TK_OP_ILLCHR, /* 4 - EOT */ 251 TK_OP_ILLCHR, /* 5 - ENQ */ 252 TK_OP_ILLCHR, /* 6 - ACK */ 253 TK_OP_ILLCHR, /* 7 - BEL */ 254 TK_OP_ILLCHR, /* 8 - BS */ 255 TK_OP_WS, /* 9 - HT */ 256 TK_OP_NL, /* 10 - NL */ 257 TK_OP_WS, /* 11 - VT */ 258 TK_OP_WS, /* 12 - FF */ 259 TK_OP_WS, /* 13 - CR */ 260 TK_OP_ILLCHR, /* 14 - SO */ 261 TK_OP_ILLCHR, /* 15 - SI */ 262 TK_OP_ILLCHR, /* 16 - DLE */ 263 TK_OP_ILLCHR, /* 17 - DC1 */ 264 TK_OP_ILLCHR, /* 18 - DC2 */ 265 TK_OP_ILLCHR, /* 19 - DC3 */ 266 TK_OP_ILLCHR, /* 20 - DC4 */ 267 TK_OP_ILLCHR, /* 21 - NAK */ 268 TK_OP_ILLCHR, /* 22 - SYN */ 269 TK_OP_ILLCHR, /* 23 - ETB */ 270 TK_OP_ILLCHR, /* 24 - CAN */ 271 TK_OP_ILLCHR, /* 25 - EM */ 272 TK_OP_ILLCHR, /* 26 - SUB */ 273 TK_OP_ILLCHR, /* 27 - ESC */ 274 TK_OP_ILLCHR, /* 28 - FS */ 275 TK_OP_ILLCHR, /* 29 - GS */ 276 TK_OP_ILLCHR, /* 30 - RS */ 277 TK_OP_ILLCHR, /* 31 - US */ 278 TK_OP_WS, /* 32 - SP */ 279 TK_OP_ID, /* 33 - ! */ 280 TK_OP_SIMQUOTE, /* 34 - " */ 281 TK_OP_CMT, /* 35 - # */ 282 TK_OP_ID, /* 36 - $ */ 283 TK_OP_ID, /* 37 - % */ 284 TK_OP_ID, /* 38 - & */ 285 TK_OP_ID, /* 39 - ' */ 286 TK_OP_ID, /* 40 - ( */ 287 TK_OP_ID, /* 41 - ) */ 288 TK_OP_ID, /* 42 - * */ 289 TK_OP_ID, /* 43 - + */ 290 TK_OP_ID, /* 44 - , */ 291 TK_DASH, /* 45 - - */ 292 TK_OP_ID, /* 46 - . */ 293 TK_OP_ID, /* 47 - / */ 294 TK_OP_ID, /* 48 - 0 */ 295 TK_OP_ID, /* 49 - 1 */ 296 TK_OP_ID, /* 50 - 2 */ 297 TK_OP_ID, /* 51 - 3 */ 298 TK_OP_ID, /* 52 - 4 */ 299 TK_OP_ID, /* 53 - 5 */ 300 TK_OP_ID, /* 54 - 6 */ 301 TK_OP_ID, /* 55 - 7 */ 302 TK_OP_ID, /* 56 - 8 */ 303 TK_OP_ID, /* 57 - 9 */ 304 TK_COLON, /* 58 - : */ 305 TK_SEMICOLON, /* 59 - ; */ 306 TK_OP_ID, /* 60 - < */ 307 TK_EQUAL, /* 61 - = */ 308 TK_OP_ID, /* 62 - > */ 309 TK_OP_ID, /* 63 - ? */ 310 TK_ATSIGN, /* 64 - @ */ 311 TK_OP_ID, /* 65 - A */ 312 TK_OP_ID, /* 66 - B */ 313 TK_OP_ID, /* 67 - C */ 314 TK_OP_ID, /* 68 - D */ 315 TK_OP_ID, /* 69 - E */ 316 TK_OP_ID, /* 70 - F */ 317 TK_OP_ID, /* 71 - G */ 318 TK_OP_ID, /* 72 - H */ 319 TK_OP_ID, /* 73 - I */ 320 TK_OP_ID, /* 74 - J */ 321 TK_OP_ID, /* 75 - K */ 322 TK_OP_ID, /* 76 - L */ 323 TK_OP_ID, /* 77 - M */ 324 TK_OP_ID, /* 78 - N */ 325 TK_OP_ID, /* 79 - O */ 326 TK_OP_ID, /* 80 - P */ 327 TK_OP_ID, /* 81 - Q */ 328 TK_OP_ID, /* 82 - R */ 329 TK_OP_ID, /* 83 - S */ 330 TK_OP_ID, /* 84 - T */ 331 TK_OP_ID, /* 85 - U */ 332 TK_OP_ID, /* 86 - V */ 333 TK_OP_ID, /* 87 - W */ 334 TK_OP_ID, /* 88 - X */ 335 TK_OP_ID, /* 89 - Y */ 336 TK_OP_ID, /* 90 - Z */ 337 TK_OP_ID, /* 91 - [ */ 338 TK_OP_ID, /* 92 - \ */ 339 TK_OP_ID, /* 93 - ] */ 340 TK_OP_ID, /* 94 - ^ */ 341 TK_OP_ID, /* 95 - _ */ 342 TK_OP_ID, /* 96 - ` */ 343 TK_OP_ID, /* 97 - a */ 344 TK_OP_ID, /* 98 - b */ 345 TK_OP_ID, /* 99 - c */ 346 TK_OP_ID, /* 100 - d */ 347 TK_OP_ID, /* 101 - e */ 348 TK_OP_ID, /* 102 - f */ 349 TK_OP_ID, /* 103 - g */ 350 TK_OP_ID, /* 104 - h */ 351 TK_OP_ID, /* 105 - i */ 352 TK_OP_ID, /* 106 - j */ 353 TK_OP_ID, /* 107 - k */ 354 TK_OP_ID, /* 108 - l */ 355 TK_OP_ID, /* 109 - m */ 356 TK_OP_ID, /* 110 - n */ 357 TK_OP_ID, /* 111 - o */ 358 TK_OP_ID, /* 112 - p */ 359 TK_OP_ID, /* 113 - q */ 360 TK_OP_ID, /* 114 - r */ 361 TK_OP_ID, /* 115 - s */ 362 TK_OP_ID, /* 116 - t */ 363 TK_OP_ID, /* 117 - u */ 364 TK_OP_ID, /* 118 - v */ 365 TK_OP_ID, /* 119 - w */ 366 TK_OP_ID, /* 120 - x */ 367 TK_OP_ID, /* 121 - y */ 368 TK_OP_ID, /* 122 - z */ 369 TK_LEFTBKT, /* 123 - { */ 370 TK_PIPE, /* 124 - | */ 371 TK_RIGHTBKT, /* 125 - } */ 372 TK_OP_ID, /* 126 - ~ */ 373 TK_OP_ILLCHR, /* 127 - DEL */ 374 }; 375 376 /* 377 * Version 2 syntax dispatch table for ld_map_gettoken(). For each of the 378 * 7-bit ASCII characters, determine how the lexical analyzer should behave. 379 * 380 * This table must be kept in sync with tkid_attr[] below. 381 * 382 * Identifier Note: 383 * We define a letter as being one of the character [A-Z], [a-z], or [_%/.] 384 * A digit is the numbers [0-9], or [$-]. An unquoted identifier is defined 385 * as a letter, followed by any number of letters or digits. This is a loosened 386 * version of the C definition of an identifier. The extra characters not 387 * allowed by C are common in section names and/or file paths. 388 */ 389 static const mf_tokdisp_t gettok_dispatch_v2 = { 390 TK_OP_EOF, /* 0 - NUL */ 391 TK_OP_ILLCHR, /* 1 - SOH */ 392 TK_OP_ILLCHR, /* 2 - STX */ 393 TK_OP_ILLCHR, /* 3 - ETX */ 394 TK_OP_ILLCHR, /* 4 - EOT */ 395 TK_OP_ILLCHR, /* 5 - ENQ */ 396 TK_OP_ILLCHR, /* 6 - ACK */ 397 TK_OP_ILLCHR, /* 7 - BEL */ 398 TK_OP_ILLCHR, /* 8 - BS */ 399 TK_OP_WS, /* 9 - HT */ 400 TK_OP_NL, /* 10 - NL */ 401 TK_OP_WS, /* 11 - VT */ 402 TK_OP_WS, /* 12 - FF */ 403 TK_OP_WS, /* 13 - CR */ 404 TK_OP_ILLCHR, /* 14 - SO */ 405 TK_OP_ILLCHR, /* 15 - SI */ 406 TK_OP_ILLCHR, /* 16 - DLE */ 407 TK_OP_ILLCHR, /* 17 - DC1 */ 408 TK_OP_ILLCHR, /* 18 - DC2 */ 409 TK_OP_ILLCHR, /* 19 - DC3 */ 410 TK_OP_ILLCHR, /* 20 - DC4 */ 411 TK_OP_ILLCHR, /* 21 - NAK */ 412 TK_OP_ILLCHR, /* 22 - SYN */ 413 TK_OP_ILLCHR, /* 23 - ETB */ 414 TK_OP_ILLCHR, /* 24 - CAN */ 415 TK_OP_ILLCHR, /* 25 - EM */ 416 TK_OP_ILLCHR, /* 26 - SUB */ 417 TK_OP_ILLCHR, /* 27 - ESC */ 418 TK_OP_ILLCHR, /* 28 - FS */ 419 TK_OP_ILLCHR, /* 29 - GS */ 420 TK_OP_ILLCHR, /* 30 - RS */ 421 TK_OP_ILLCHR, /* 31 - US */ 422 TK_OP_WS, /* 32 - SP */ 423 TK_BANG, /* 33 - ! */ 424 TK_OP_CQUOTE, /* 34 - " */ 425 TK_OP_CMT, /* 35 - # */ 426 TK_OP_CDIR, /* 36 - $ */ 427 TK_OP_ID, /* 37 - % */ 428 TK_OP_BADCHR, /* 38 - & */ 429 TK_OP_SIMQUOTE, /* 39 - ' */ 430 TK_OP_BADCHR, /* 40 - ( */ 431 TK_OP_BADCHR, /* 41 - ) */ 432 TK_STAR, /* 42 - * */ 433 TK_OP_CEQUAL, /* 43 - + */ 434 TK_OP_BADCHR, /* 44 - , */ 435 TK_OP_CEQUAL, /* 45 - - */ 436 TK_OP_ID, /* 46 - . */ 437 TK_OP_ID, /* 47 - / */ 438 TK_OP_NUM, /* 48 - 0 */ 439 TK_OP_NUM, /* 49 - 1 */ 440 TK_OP_NUM, /* 50 - 2 */ 441 TK_OP_NUM, /* 51 - 3 */ 442 TK_OP_NUM, /* 52 - 4 */ 443 TK_OP_NUM, /* 53 - 5 */ 444 TK_OP_NUM, /* 54 - 6 */ 445 TK_OP_NUM, /* 55 - 7 */ 446 TK_OP_NUM, /* 56 - 8 */ 447 TK_OP_NUM, /* 57 - 9 */ 448 TK_COLON, /* 58 - : */ 449 TK_SEMICOLON, /* 59 - ; */ 450 TK_OP_BADCHR, /* 60 - < */ 451 TK_EQUAL, /* 61 - = */ 452 TK_OP_BADCHR, /* 62 - > */ 453 TK_OP_BADCHR, /* 63 - ? */ 454 TK_OP_BADCHR, /* 64 - @ */ 455 TK_OP_ID, /* 65 - A */ 456 TK_OP_ID, /* 66 - B */ 457 TK_OP_ID, /* 67 - C */ 458 TK_OP_ID, /* 68 - D */ 459 TK_OP_ID, /* 69 - E */ 460 TK_OP_ID, /* 70 - F */ 461 TK_OP_ID, /* 71 - G */ 462 TK_OP_ID, /* 72 - H */ 463 TK_OP_ID, /* 73 - I */ 464 TK_OP_ID, /* 74 - J */ 465 TK_OP_ID, /* 75 - K */ 466 TK_OP_ID, /* 76 - L */ 467 TK_OP_ID, /* 77 - M */ 468 TK_OP_ID, /* 78 - N */ 469 TK_OP_ID, /* 79 - O */ 470 TK_OP_ID, /* 80 - P */ 471 TK_OP_ID, /* 81 - Q */ 472 TK_OP_ID, /* 82 - R */ 473 TK_OP_ID, /* 83 - S */ 474 TK_OP_ID, /* 84 - T */ 475 TK_OP_ID, /* 85 - U */ 476 TK_OP_ID, /* 86 - V */ 477 TK_OP_ID, /* 87 - W */ 478 TK_OP_ID, /* 88 - X */ 479 TK_OP_ID, /* 89 - Y */ 480 TK_OP_ID, /* 90 - Z */ 481 TK_OP_BADCHR, /* 91 - [ */ 482 TK_OP_BADCHR, /* 92 - \ */ 483 TK_OP_BADCHR, /* 93 - ] */ 484 TK_OP_BADCHR, /* 94 - ^ */ 485 TK_OP_ID, /* 95 - _ */ 486 TK_OP_BADCHR, /* 96 - ` */ 487 TK_OP_ID, /* 97 - a */ 488 TK_OP_ID, /* 98 - b */ 489 TK_OP_ID, /* 99 - c */ 490 TK_OP_ID, /* 100 - d */ 491 TK_OP_ID, /* 101 - e */ 492 TK_OP_ID, /* 102 - f */ 493 TK_OP_ID, /* 103 - g */ 494 TK_OP_ID, /* 104 - h */ 495 TK_OP_ID, /* 105 - i */ 496 TK_OP_ID, /* 106 - j */ 497 TK_OP_ID, /* 107 - k */ 498 TK_OP_ID, /* 108 - l */ 499 TK_OP_ID, /* 109 - m */ 500 TK_OP_ID, /* 110 - n */ 501 TK_OP_ID, /* 111 - o */ 502 TK_OP_ID, /* 112 - p */ 503 TK_OP_ID, /* 113 - q */ 504 TK_OP_ID, /* 114 - r */ 505 TK_OP_ID, /* 115 - s */ 506 TK_OP_ID, /* 116 - t */ 507 TK_OP_ID, /* 117 - u */ 508 TK_OP_ID, /* 118 - v */ 509 TK_OP_ID, /* 119 - w */ 510 TK_OP_ID, /* 120 - x */ 511 TK_OP_ID, /* 121 - y */ 512 TK_OP_ID, /* 122 - z */ 513 TK_LEFTBKT, /* 123 - { */ 514 TK_OP_BADCHR, /* 124 - | */ 515 TK_RIGHTBKT, /* 125 - } */ 516 TK_OP_BADCHR, /* 126 - ~ */ 517 TK_OP_ILLCHR, /* 127 - DEL */ 518 }; 519 520 521 /* 522 * Table used to identify unquoted identifiers. Each element of this array 523 * contains a bitmask indicating whether the character it represents starts, 524 * or continues an identifier, for each supported mapfile syntax version. 525 */ 526 static const char tkid_attr[128] = { 527 0, /* 0 - NUL */ 528 TKID_ATTR_CONT(1), /* 1 - SOH */ 529 TKID_ATTR_CONT(1), /* 2 - STX */ 530 TKID_ATTR_CONT(1), /* 3 - ETX */ 531 TKID_ATTR_CONT(1), /* 4 - EOT */ 532 TKID_ATTR_CONT(1), /* 5 - ENQ */ 533 TKID_ATTR_CONT(1), /* 6 - ACK */ 534 TKID_ATTR_CONT(1), /* 7 - BEL */ 535 TKID_ATTR_CONT(1), /* 8 - BS */ 536 0, /* 9 - HT */ 537 0, /* 10 - NL */ 538 TKID_ATTR_CONT(1), /* 11 - VT */ 539 TKID_ATTR_CONT(1), /* 12 - FF */ 540 TKID_ATTR_CONT(1), /* 13 - CR */ 541 TKID_ATTR_CONT(1), /* 14 - SO */ 542 TKID_ATTR_CONT(1), /* 15 - SI */ 543 TKID_ATTR_CONT(1), /* 16 - DLE */ 544 TKID_ATTR_CONT(1), /* 17 - DC1 */ 545 TKID_ATTR_CONT(1), /* 18 - DC2 */ 546 TKID_ATTR_CONT(1), /* 19 - DC3 */ 547 TKID_ATTR_CONT(1), /* 20 - DC4 */ 548 TKID_ATTR_CONT(1), /* 21 - NAK */ 549 TKID_ATTR_CONT(1), /* 22 - SYN */ 550 TKID_ATTR_CONT(1), /* 23 - ETB */ 551 TKID_ATTR_CONT(1), /* 24 - CAN */ 552 TKID_ATTR_CONT(1), /* 25 - EM */ 553 TKID_ATTR_CONT(1), /* 26 - SUB */ 554 TKID_ATTR_CONT(1), /* 27 - ESC */ 555 TKID_ATTR_CONT(1), /* 28 - FS */ 556 TKID_ATTR_CONT(1), /* 29 - GS */ 557 TKID_ATTR_CONT(1), /* 30 - RS */ 558 TKID_ATTR_CONT(1), /* 31 - US */ 559 0, /* 32 - SP */ 560 TKID_ATTR(1), /* 33 - ! */ 561 0, /* 34 - " */ 562 0, /* 35 - # */ 563 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 36 - $ */ 564 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 37 - % */ 565 TKID_ATTR(1), /* 38 - & */ 566 TKID_ATTR(1), /* 39 - ' */ 567 TKID_ATTR(1), /* 40 - ( */ 568 TKID_ATTR(1), /* 41 - ) */ 569 TKID_ATTR(1), /* 42 - * */ 570 TKID_ATTR(1), /* 43 - + */ 571 TKID_ATTR(1), /* 44 - , */ 572 TKID_ATTR_CONT(1) | TKID_ATTR_CONT(2), /* 45 - - */ 573 TKID_ATTR(1) | TKID_ATTR(2), /* 46 - . */ 574 TKID_ATTR(1) | TKID_ATTR(2), /* 47 - / */ 575 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 48 - 0 */ 576 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 49 - 1 */ 577 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 50 - 2 */ 578 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 51 - 3 */ 579 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 52 - 4 */ 580 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 53 - 5 */ 581 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 54 - 6 */ 582 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 55 - 7 */ 583 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 56 - 8 */ 584 TKID_ATTR(1) | TKID_ATTR_CONT(2), /* 57 - 9 */ 585 0, /* 58 - : */ 586 0, /* 59 - ; */ 587 TKID_ATTR(1), /* 60 - < */ 588 0, /* 61 - = */ 589 TKID_ATTR(1), /* 62 - > */ 590 TKID_ATTR(1), /* 63 - ? */ 591 TKID_ATTR_CONT(1), /* 64 - @ */ 592 TKID_ATTR(1) | TKID_ATTR(2), /* 65 - A */ 593 TKID_ATTR(1) | TKID_ATTR(2), /* 66 - B */ 594 TKID_ATTR(1) | TKID_ATTR(2), /* 67 - C */ 595 TKID_ATTR(1) | TKID_ATTR(2), /* 68 - D */ 596 TKID_ATTR(1) | TKID_ATTR(2), /* 69 - E */ 597 TKID_ATTR(1) | TKID_ATTR(2), /* 70 - F */ 598 TKID_ATTR(1) | TKID_ATTR(2), /* 71 - G */ 599 TKID_ATTR(1) | TKID_ATTR(2), /* 72 - H */ 600 TKID_ATTR(1) | TKID_ATTR(2), /* 73 - I */ 601 TKID_ATTR(1) | TKID_ATTR(2), /* 74 - J */ 602 TKID_ATTR(1) | TKID_ATTR(2), /* 75 - K */ 603 TKID_ATTR(1) | TKID_ATTR(2), /* 76 - L */ 604 TKID_ATTR(1) | TKID_ATTR(2), /* 77 - M */ 605 TKID_ATTR(1) | TKID_ATTR(2), /* 78 - N */ 606 TKID_ATTR(1) | TKID_ATTR(2), /* 79 - O */ 607 TKID_ATTR(1) | TKID_ATTR(2), /* 80 - P */ 608 TKID_ATTR(1) | TKID_ATTR(2), /* 81 - Q */ 609 TKID_ATTR(1) | TKID_ATTR(2), /* 82 - R */ 610 TKID_ATTR(1) | TKID_ATTR(2), /* 83 - S */ 611 TKID_ATTR(1) | TKID_ATTR(2), /* 84 - T */ 612 TKID_ATTR(1) | TKID_ATTR(2), /* 85 - U */ 613 TKID_ATTR(1) | TKID_ATTR(2), /* 86 - V */ 614 TKID_ATTR(1) | TKID_ATTR(2), /* 87 - W */ 615 TKID_ATTR(1) | TKID_ATTR(2), /* 88 - X */ 616 TKID_ATTR(1) | TKID_ATTR(2), /* 89 - Y */ 617 TKID_ATTR(1) | TKID_ATTR(2), /* 90 - Z */ 618 TKID_ATTR(1), /* 91 - [ */ 619 TKID_ATTR(1), /* 92 - \ */ 620 TKID_ATTR(1), /* 93 - ] */ 621 TKID_ATTR(1), /* 94 - ^ */ 622 TKID_ATTR(1) | TKID_ATTR(2), /* 95 - _ */ 623 TKID_ATTR(1), /* 96 - ` */ 624 TKID_ATTR(1) | TKID_ATTR(2), /* 97 - a */ 625 TKID_ATTR(1) | TKID_ATTR(2), /* 98 - b */ 626 TKID_ATTR(1) | TKID_ATTR(2), /* 99 - c */ 627 TKID_ATTR(1) | TKID_ATTR(2), /* 100 - d */ 628 TKID_ATTR(1) | TKID_ATTR(2), /* 101 - e */ 629 TKID_ATTR(1) | TKID_ATTR(2), /* 102 - f */ 630 TKID_ATTR(1) | TKID_ATTR(2), /* 103 - g */ 631 TKID_ATTR(1) | TKID_ATTR(2), /* 104 - h */ 632 TKID_ATTR(1) | TKID_ATTR(2), /* 105 - i */ 633 TKID_ATTR(1) | TKID_ATTR(2), /* 106 - j */ 634 TKID_ATTR(1) | TKID_ATTR(2), /* 107 - k */ 635 TKID_ATTR(1) | TKID_ATTR(2), /* 108 - l */ 636 TKID_ATTR(1) | TKID_ATTR(2), /* 109 - m */ 637 TKID_ATTR(1) | TKID_ATTR(2), /* 110 - n */ 638 TKID_ATTR(1) | TKID_ATTR(2), /* 111 - o */ 639 TKID_ATTR(1) | TKID_ATTR(2), /* 112 - p */ 640 TKID_ATTR(1) | TKID_ATTR(2), /* 113 - q */ 641 TKID_ATTR(1) | TKID_ATTR(2), /* 114 - r */ 642 TKID_ATTR(1) | TKID_ATTR(2), /* 115 - s */ 643 TKID_ATTR(1) | TKID_ATTR(2), /* 116 - t */ 644 TKID_ATTR(1) | TKID_ATTR(2), /* 117 - u */ 645 TKID_ATTR(1) | TKID_ATTR(2), /* 118 - v */ 646 TKID_ATTR(1) | TKID_ATTR(2), /* 119 - w */ 647 TKID_ATTR(1) | TKID_ATTR(2), /* 120 - x */ 648 TKID_ATTR(1) | TKID_ATTR(2), /* 121 - y */ 649 TKID_ATTR(1) | TKID_ATTR(2), /* 122 - z */ 650 TKID_ATTR_CONT(1), /* 123 - { */ 651 TKID_ATTR_CONT(1), /* 124 - | */ 652 TKID_ATTR_CONT(1), /* 125 - } */ 653 TKID_ATTR(1), /* 126 - ~ */ 654 TKID_ATTR_CONT(1), /* 127 - DEL */ 655 }; 656 657 658 /* 659 * Advance the given string pointer to the next newline character, 660 * or the terminating NULL if there is none. 661 */ 662 inline static void 663 advance_to_eol(char **str) 664 { 665 char *s = *str; 666 667 while ((*s != '\n') && (*s != '\0')) 668 s++; 669 *str = s; 670 } 671 672 /* 673 * Insert a NULL patch at the given address 674 */ 675 inline static void 676 null_patch_set(char *str, ld_map_npatch_t *np) 677 { 678 np->np_ptr = str; 679 np->np_ch = *str; 680 *str = '\0'; 681 } 682 683 /* 684 * Undo a NULL patch 685 */ 686 inline static void 687 null_patch_undo(ld_map_npatch_t *np) 688 { 689 *np->np_ptr = np->np_ch; 690 } 691 692 /* 693 * Insert a NULL patch at the end of the line containing str. 694 */ 695 static void 696 null_patch_eol(char *str, ld_map_npatch_t *np) 697 { 698 advance_to_eol(&str); 699 null_patch_set(str, np); 700 } 701 702 /* 703 * Locate the end of an unquoted identifier. 704 * 705 * entry: 706 * mf - Mapfile descriptor, positioned to first character 707 * of identifier. 708 * 709 * exit: 710 * If the item pointed at by mf is not an identifier, returns NULL. 711 * Otherwise, returns pointer to character after the last character 712 * of the identifier. 713 */ 714 inline static char * 715 ident_delimit(Mapfile *mf) 716 { 717 char *str = mf->mf_next; 718 ld_map_npatch_t np; 719 int c = *str++; 720 721 /* If not a valid start character, report the error */ 722 if ((c & 0x80) || !(tkid_attr[c] & mf->mf_tkid_start)) { 723 null_patch_set(str, &np); 724 mf_fatal(mf, MSG_INTL(MSG_MAP_BADCHAR), str); 725 null_patch_undo(&np); 726 return (NULL); 727 } 728 729 /* Keep going until we hit a non-continuing character */ 730 for (c = *str; !(c & 0x80) && (tkid_attr[c] & mf->mf_tkid_cont); 731 c = *++str) 732 ; 733 734 return (str); 735 } 736 737 /* 738 * Allocate memory for a stack. 739 * 740 * entry: 741 * stack - Pointer to stack for which memory is required, cast 742 * to the generic stack type. 743 * n_default - Size to use for initial allocation. 744 * elt_size - sizeof(elt), where elt is the actual stack data type. 745 * 746 * exit: 747 * Returns (1) on success. On error (memory allocation), a message 748 * is printed and False (0) is returned. 749 * 750 * note: 751 * The caller casts the pointer to their actual datatype-specific stack 752 * to be a (generic_stack_t *). The C language will give all stack 753 * structs the same size and layout as long as the underlying platform 754 * uses a single integral type for pointers. Hence, this cast is safe, 755 * and lets a generic routine modify data-specific types without being 756 * aware of those types. 757 */ 758 static Boolean 759 stack_resize(generic_stack_t *stack, size_t n_default, size_t elt_size) 760 { 761 size_t new_n_alloc; 762 void *newaddr; 763 764 /* Use initial size first, and double the allocation on each call */ 765 new_n_alloc = (stack->stk_n_alloc == 0) ? 766 n_default : (stack->stk_n_alloc * 2); 767 768 newaddr = libld_realloc(stack->stk_s, new_n_alloc * elt_size); 769 if (newaddr == NULL) 770 return (FALSE); 771 772 stack->stk_s = newaddr; 773 stack->stk_n_alloc = new_n_alloc; 774 return (TRUE); 775 } 776 777 /* 778 * AVL comparison function for cexp_id_node_t items. 779 * 780 * entry: 781 * n1, n2 - pointers to nodes to be compared 782 * 783 * exit: 784 * Returns -1 if (n1 < n2), 0 if they are equal, and 1 if (n1 > n2) 785 */ 786 static int 787 cexp_ident_cmp(const void *n1, const void *n2) 788 { 789 int rc; 790 791 rc = strcmp(((cexp_id_node_t *)n1)->ceid_name, 792 ((cexp_id_node_t *)n2)->ceid_name); 793 794 if (rc > 0) 795 return (1); 796 if (rc < 0) 797 return (-1); 798 return (0); 799 } 800 801 802 /* 803 * Returns True (1) if name is in the conditional expression identifier 804 * AVL tree, and False (0) otherwise. 805 */ 806 static int 807 cexp_ident_test(const char *name) 808 { 809 cexp_id_node_t node; 810 811 node.ceid_name = name; 812 return (avl_find(lms.lms_cexp_id, &node, 0) != NULL); 813 } 814 815 /* 816 * Add a new boolean identifier to the conditional expression identifier 817 * AVL tree. 818 * 819 * entry: 820 * mf - If non-NULL, the mapfile descriptor for the mapfile 821 * containing the $add directive. NULL if this is an 822 * initialization call. 823 * name - Name of identifier. Must point at stable storage that will 824 * not be moved or modified by the caller following this call. 825 * 826 * exit: 827 * On success, True (1) is returned and name has been entered. 828 * On failure, False (0) is returned and an error has been printed. 829 */ 830 static int 831 cexp_ident_add(Mapfile *mf, const char *name) 832 { 833 cexp_id_node_t *node; 834 835 if (mf != NULL) { 836 DBG_CALL(Dbg_map_cexp_id(mf->mf_ofl->ofl_lml, 1, 837 mf->mf_name, mf->mf_lineno, name)); 838 839 /* If is already known, don't do it again */ 840 if (cexp_ident_test(name)) 841 return (1); 842 } 843 844 if ((node = libld_calloc(sizeof (*node), 1)) == NULL) 845 return (0); 846 node->ceid_name = name; 847 avl_add(lms.lms_cexp_id, node); 848 return (1); 849 } 850 851 /* 852 * Remove a boolean identifier from the conditional expression identifier 853 * AVL tree. 854 * 855 * entry: 856 * mf - Mapfile descriptor 857 * name - Name of identifier. 858 * 859 * exit: 860 * If the name was in the tree, it has been removed. If not, 861 * then this routine quietly returns. 862 */ 863 static void 864 cexp_ident_clear(Mapfile *mf, const char *name) 865 { 866 cexp_id_node_t node; 867 cexp_id_node_t *real_node; 868 869 DBG_CALL(Dbg_map_cexp_id(mf->mf_ofl->ofl_lml, 0, 870 mf->mf_name, mf->mf_lineno, name)); 871 872 node.ceid_name = name; 873 real_node = avl_find(lms.lms_cexp_id, &node, 0); 874 if (real_node != NULL) 875 avl_remove(lms.lms_cexp_id, real_node); 876 } 877 878 /* 879 * Initialize the AVL tree that holds the names of the currently defined 880 * boolean identifiers for conditional expressions ($if/$elif). 881 * 882 * entry: 883 * ofl - Output file descriptor 884 * 885 * exit: 886 * On success, TRUE (1) is returned and lms.lms_cexp_id is ready for use. 887 * On failure, FALSE (0) is returned. 888 */ 889 static Boolean 890 cexp_ident_init(void) 891 { 892 /* If already done, use it */ 893 if (lms.lms_cexp_id != NULL) 894 return (TRUE); 895 896 lms.lms_cexp_id = libld_calloc(sizeof (*lms.lms_cexp_id), 1); 897 if (lms.lms_cexp_id == NULL) 898 return (FALSE); 899 avl_create(lms.lms_cexp_id, cexp_ident_cmp, sizeof (cexp_id_node_t), 900 SGSOFFSETOF(cexp_id_node_t, ceid_avlnode)); 901 902 903 /* ELFCLASS */ 904 if (cexp_ident_add(NULL, (ld_targ.t_m.m_class == ELFCLASS32) ? 905 MSG_ORIG(MSG_STR_UELF32) : MSG_ORIG(MSG_STR_UELF64)) == 0) 906 return (FALSE); 907 908 /* Machine */ 909 switch (ld_targ.t_m.m_mach) { 910 case EM_386: 911 case EM_AMD64: 912 if (cexp_ident_add(NULL, MSG_ORIG(MSG_STR_UX86)) == 0) 913 return (FALSE); 914 break; 915 916 case EM_SPARC: 917 case EM_SPARCV9: 918 if (cexp_ident_add(NULL, MSG_ORIG(MSG_STR_USPARC)) == 0) 919 return (FALSE); 920 break; 921 } 922 923 /* true is always defined */ 924 if (cexp_ident_add(NULL, MSG_ORIG(MSG_STR_TRUE)) == 0) 925 return (FALSE); 926 927 return (TRUE); 928 } 929 930 /* 931 * Validate the string starting at mf->mf_next as being a 932 * boolean conditional expression identifier. 933 * 934 * entry: 935 * mf - Mapfile descriptor 936 * len - NULL, or address of variable to receive strlen() of identifier 937 * directive - If (len == NULL), string giving name of directive being 938 * processed. Ignored if (len != NULL). 939 * 940 * exit: 941 * On success: 942 * - If len is NULL, a NULL is inserted following the final 943 * character of the identifier, and the remainder of the string 944 * is tested to ensure it is empty, or only contains whitespace. 945 * - If len is non-NULL, *len is set to the number of characters 946 * in the identifier, and the rest of the string is not modified. 947 * - TRUE (1) is returned 948 * 949 * On failure, returns FALSE (0). 950 */ 951 static Boolean 952 cexp_ident_validate(Mapfile *mf, size_t *len, const char *directive) 953 { 954 char *tail; 955 956 if ((tail = ident_delimit(mf)) == NULL) 957 return (FALSE); 958 959 /* 960 * If len is non-NULL, we simple count the number of characters 961 * consumed by the identifier and are done. If len is NULL, then 962 * ensure there's nothing left but whitespace, and NULL terminate 963 * the identifier to remove it. 964 */ 965 if (len != NULL) { 966 *len = tail - mf->mf_next; 967 } else if (*tail != '\0') { 968 *tail++ = '\0'; 969 while (isspace(*tail)) 970 tail++; 971 if (*tail != '\0') { 972 mf_fatal(mf, MSG_INTL(MSG_MAP_BADEXTRA), directive); 973 return (FALSE); 974 } 975 } 976 977 return (TRUE); 978 } 979 980 /* 981 * Push a new operator onto the conditional expression operator stack. 982 * 983 * entry: 984 * mf - Mapfile descriptor 985 * op - Operator to push 986 * 987 * exit: 988 * On success, TRUE (1) is returned, otherwise FALSE (0). 989 */ 990 static Boolean 991 cexp_push_op(cexp_op_t op) 992 { 993 if (STACK_RESERVE(lms.lms_cexp_op_stack, CEXP_OP_STACK_INIT) == 0) 994 return (FALSE); 995 996 STACK_PUSH(lms.lms_cexp_op_stack) = op; 997 return (TRUE); 998 } 999 1000 /* 1001 * Evaluate the basic operator (non-paren) at the top of lms.lms_cexp_op_stack, 1002 * and push the results on lms.lms_cexp_val_stack. 1003 * 1004 * exit: 1005 * On success, returns TRUE (1). On error, FALSE (0) is returned, 1006 * and the caller is responsible for issuing the error. 1007 */ 1008 static Boolean 1009 cexp_eval_op(void) 1010 { 1011 cexp_op_t op; 1012 uchar_t val; 1013 1014 op = STACK_POP(lms.lms_cexp_op_stack); 1015 switch (op) { 1016 case CEXP_OP_AND: 1017 if (lms.lms_cexp_val_stack.stk_n < 2) 1018 return (FALSE); 1019 val = STACK_POP(lms.lms_cexp_val_stack); 1020 STACK_TOP(lms.lms_cexp_val_stack) = val && 1021 STACK_TOP(lms.lms_cexp_val_stack); 1022 break; 1023 1024 case CEXP_OP_OR: 1025 if (lms.lms_cexp_val_stack.stk_n < 2) 1026 return (FALSE); 1027 val = STACK_POP(lms.lms_cexp_val_stack); 1028 STACK_TOP(lms.lms_cexp_val_stack) = val || 1029 STACK_TOP(lms.lms_cexp_val_stack); 1030 break; 1031 1032 case CEXP_OP_NEG: 1033 if (lms.lms_cexp_val_stack.stk_n < 1) 1034 return (FALSE); 1035 STACK_TOP(lms.lms_cexp_val_stack) = 1036 !STACK_TOP(lms.lms_cexp_val_stack); 1037 break; 1038 default: 1039 return (FALSE); 1040 } 1041 1042 return (TRUE); 1043 } 1044 1045 /* 1046 * Evaluate an expression for a $if/$elif control directive. 1047 * 1048 * entry: 1049 * mf - Mapfile descriptor for NULL terminated string 1050 * containing the expression. 1051 * 1052 * exit: 1053 * The contents of str are modified by this routine. 1054 * One of the following values are returned: 1055 * -1 Syntax error encountered (an error is printed) 1056 * 0 The expression evaluates to False 1057 * 1 The expression evaluates to True. 1058 * 1059 * note: 1060 * A simplified version of Dijkstra's Shunting Yard algorithm is used 1061 * to convert this syntax into postfix form and then evaluate it. 1062 * Our version has no functions and a tiny set of operators. 1063 * 1064 * The expressions consist of boolean identifiers, which can be 1065 * combined using the following operators, listed from highest 1066 * precedence to least: 1067 * 1068 * Operator Meaning 1069 * ------------------------------------------------- 1070 * (expr) sub-expression, non-associative 1071 * ! logical negation, prefix, left associative 1072 * && || logical and/or, binary, left associative 1073 * 1074 * The operands manipulated by these operators are names, consisting of 1075 * a sequence of letters and digits. The first character must be a letter. 1076 * Underscore (_) and period (.) are also considered to be characters. 1077 * An operand is considered True if it is found in our set of known 1078 * names (lms.lms_cexp_id), and False otherwise. 1079 * 1080 * The Shunting Yard algorithm works using two stacks, one for operators, 1081 * and a second for operands. The infix input expression is tokenized from 1082 * left to right and processed in order. Issues of associativity and 1083 * precedence are managed by reducing (poping and evaluating) items with 1084 * higer precedence before pushing additional tokens with lower precedence. 1085 */ 1086 static int 1087 cexp_eval_expr(Mapfile *mf) 1088 { 1089 char *ident; 1090 size_t len; 1091 cexp_op_t new_op = CEXP_OP_AND; /* to catch binop at start */ 1092 ld_map_npatch_t np; 1093 char *str = mf->mf_next; 1094 1095 STACK_RESET(lms.lms_cexp_op_stack); 1096 STACK_RESET(lms.lms_cexp_val_stack); 1097 1098 for (; *str; str++) { 1099 1100 /* Skip whitespace */ 1101 while (isspace(*str)) 1102 str++; 1103 if (!*str) 1104 break; 1105 1106 switch (*str) { 1107 case '&': 1108 case '|': 1109 if (*(str + 1) != *str) 1110 goto token_error; 1111 if ((new_op != CEXP_OP_NONE) && 1112 (new_op != CEXP_OP_CPAR)) { 1113 mf_fatal0(mf, MSG_INTL(MSG_MAP_CEXP_BADOPUSE)); 1114 return (-1); 1115 } 1116 str++; 1117 1118 /* 1119 * As this is a left associative binary operator, we 1120 * need to process all operators of equal or higher 1121 * precedence before pushing the new operator. 1122 */ 1123 while (!STACK_IS_EMPTY(lms.lms_cexp_op_stack)) { 1124 cexp_op_t op = STACK_TOP(lms.lms_cexp_op_stack); 1125 1126 1127 if ((op != CEXP_OP_AND) && (op != CEXP_OP_OR) && 1128 (op != CEXP_OP_NEG)) 1129 break; 1130 1131 if (!cexp_eval_op()) 1132 goto semantic_error; 1133 } 1134 1135 new_op = (*str == '&') ? CEXP_OP_AND : CEXP_OP_OR; 1136 if (!cexp_push_op(new_op)) 1137 return (-1); 1138 break; 1139 1140 case '!': 1141 new_op = CEXP_OP_NEG; 1142 if (!cexp_push_op(new_op)) 1143 return (-1); 1144 break; 1145 1146 case '(': 1147 new_op = CEXP_OP_OPAR; 1148 if (!cexp_push_op(new_op)) 1149 return (-1); 1150 break; 1151 1152 case ')': 1153 new_op = CEXP_OP_CPAR; 1154 1155 /* Evaluate the operator stack until reach '(' */ 1156 while (!STACK_IS_EMPTY(lms.lms_cexp_op_stack) && 1157 (STACK_TOP(lms.lms_cexp_op_stack) != CEXP_OP_OPAR)) 1158 if (!cexp_eval_op()) 1159 goto semantic_error; 1160 1161 /* 1162 * If the top of operator stack is not an open paren, 1163 * when we have an error. In this case, the operator 1164 * stack will be empty due to the loop above. 1165 */ 1166 if (STACK_IS_EMPTY(lms.lms_cexp_op_stack)) 1167 goto unbalpar_error; 1168 lms.lms_cexp_op_stack.stk_n--; /* Pop OPAR */ 1169 break; 1170 1171 default: 1172 /* Ensure there's room to push another operand */ 1173 if (STACK_RESERVE(lms.lms_cexp_val_stack, 1174 CEXP_VAL_STACK_INIT) == 0) 1175 return (0); 1176 new_op = CEXP_OP_NONE; 1177 1178 /* 1179 * Operands cannot be numbers. However, we accept two 1180 * special cases: '0' means false, and '1' is true. 1181 * This is done to support the common C idiom of 1182 * '#if 1' and '#if 0' to conditionalize code under 1183 * development. 1184 */ 1185 if ((*str == '0') || (*str == '1')) { 1186 STACK_PUSH(lms.lms_cexp_val_stack) = 1187 (*str == '1'); 1188 break; 1189 } 1190 1191 /* Look up the identifier */ 1192 ident = mf->mf_next = str; 1193 if (!cexp_ident_validate(mf, &len, NULL)) 1194 return (-1); 1195 str += len - 1; /* loop will advance past final ch */ 1196 null_patch_set(&ident[len], &np); 1197 STACK_PUSH(lms.lms_cexp_val_stack) = 1198 cexp_ident_test(ident); 1199 null_patch_undo(&np); 1200 1201 break; 1202 } 1203 } 1204 1205 /* Evaluate the operator stack until empty */ 1206 while (!STACK_IS_EMPTY(lms.lms_cexp_op_stack)) { 1207 if (STACK_TOP(lms.lms_cexp_op_stack) == CEXP_OP_OPAR) 1208 goto unbalpar_error; 1209 1210 if (!cexp_eval_op()) 1211 goto semantic_error; 1212 } 1213 1214 /* There should be exactly one value left */ 1215 if (lms.lms_cexp_val_stack.stk_n != 1) 1216 goto semantic_error; 1217 1218 /* Final value is the result */ 1219 return (lms.lms_cexp_val_stack.stk_s[0]); 1220 1221 /* Errors issued more than once are handled below, accessed via goto */ 1222 1223 token_error: /* unexpected characters in input stream */ 1224 mf_fatal(mf, MSG_INTL(MSG_MAP_CEXP_TOKERR), str); 1225 return (-1); 1226 1227 semantic_error: /* valid tokens, but in invalid arrangement */ 1228 mf_fatal0(mf, MSG_INTL(MSG_MAP_CEXP_SEMERR)); 1229 return (-1); 1230 1231 unbalpar_error: /* Extra or missing parenthesis */ 1232 mf_fatal0(mf, MSG_INTL(MSG_MAP_CEXP_UNBALPAR)); 1233 return (-1); 1234 } 1235 1236 /* 1237 * Process a mapfile control directive. These directives start with 1238 * the dollar character, and are used to manage details of the mapfile 1239 * itself, such as version and conditional input. 1240 * 1241 * entry: 1242 * mf - Mapfile descriptor 1243 * 1244 * exit: 1245 * Returns TRUE (1) for success, and FALSE (0) on error. In the 1246 * error case, a descriptive error is issued. 1247 */ 1248 static Boolean 1249 cdir_process(Mapfile *mf) 1250 { 1251 typedef enum { /* Directive types */ 1252 CDIR_T_UNKNOWN = 0, /* Unrecognized control directive */ 1253 CDIR_T_ADD, /* $add */ 1254 CDIR_T_CLEAR, /* $clear */ 1255 CDIR_T_ERROR, /* $error */ 1256 CDIR_T_VERSION, /* $mapfile_version */ 1257 CDIR_T_IF, /* $if */ 1258 CDIR_T_ELIF, /* $elif */ 1259 CDIR_T_ELSE, /* $else */ 1260 CDIR_T_ENDIF, /* $endif */ 1261 } cdir_t; 1262 1263 typedef enum { /* Types of arguments accepted by directives */ 1264 ARG_T_NONE, /* Directive takes no arguments */ 1265 ARG_T_EXPR, /* Directive takes a conditional expression */ 1266 ARG_T_ID, /* Conditional expression identifier */ 1267 ARG_T_STR, /* Non-empty string */ 1268 ARG_T_IGN /* Ignore the argument */ 1269 } cdir_arg_t; 1270 1271 typedef struct { 1272 const char *md_name; /* Directive name */ 1273 size_t md_size; /* strlen(md_name) */ 1274 cdir_arg_t md_arg; /* Type of arguments */ 1275 cdir_t md_op; /* CDIR_T_ code */ 1276 } cdir_match_t; 1277 1278 /* Control Directives: The most likely items are listed first */ 1279 static cdir_match_t match_data[] = { 1280 { MSG_ORIG(MSG_STR_CDIR_IF), MSG_STR_CDIR_IF_SIZE, 1281 ARG_T_EXPR, CDIR_T_IF }, 1282 { MSG_ORIG(MSG_STR_CDIR_ENDIF), MSG_STR_CDIR_ENDIF_SIZE, 1283 ARG_T_NONE, CDIR_T_ENDIF }, 1284 { MSG_ORIG(MSG_STR_CDIR_ELSE), MSG_STR_CDIR_ELSE_SIZE, 1285 ARG_T_NONE, CDIR_T_ELSE }, 1286 { MSG_ORIG(MSG_STR_CDIR_ELIF), MSG_STR_CDIR_ELIF_SIZE, 1287 ARG_T_EXPR, CDIR_T_ELIF }, 1288 { MSG_ORIG(MSG_STR_CDIR_ERROR), MSG_STR_CDIR_ERROR_SIZE, 1289 ARG_T_STR, CDIR_T_ERROR }, 1290 { MSG_ORIG(MSG_STR_CDIR_ADD), MSG_STR_CDIR_ADD_SIZE, 1291 ARG_T_ID, CDIR_T_ADD }, 1292 { MSG_ORIG(MSG_STR_CDIR_CLEAR), MSG_STR_CDIR_CLEAR_SIZE, 1293 ARG_T_ID, CDIR_T_CLEAR }, 1294 { MSG_ORIG(MSG_STR_CDIR_MFVER), MSG_STR_CDIR_MFVER_SIZE, 1295 ARG_T_IGN, CDIR_T_VERSION }, 1296 1297 { NULL, 0, 1298 ARG_T_IGN, CDIR_T_UNKNOWN } 1299 }; 1300 1301 cdir_match_t *mdptr; 1302 char *tail; 1303 int expr_eval; /* Result of evaluating ARG_T_EXPR */ 1304 Mapfile arg_mf; 1305 cdir_level_t *level; 1306 int pass, parent_pass; /* Currently accepting input */ 1307 1308 restart: 1309 /* Is the immediate context passing input? */ 1310 pass = STACK_IS_EMPTY(lms.lms_cdir_stack) || 1311 STACK_TOP(lms.lms_cdir_stack).cdl_pass; 1312 1313 /* Is the surrounding (parent) context passing input? */ 1314 parent_pass = (lms.lms_cdir_stack.stk_n <= 1) || 1315 lms.lms_cdir_stack.stk_s[lms.lms_cdir_stack.stk_n - 2].cdl_pass; 1316 1317 1318 for (mdptr = match_data; mdptr->md_name; mdptr++) { 1319 /* Prefix must match, or we move on */ 1320 if (strncmp(mf->mf_next, mdptr->md_name, 1321 mdptr->md_size) != 0) 1322 continue; 1323 tail = mf->mf_next + mdptr->md_size; 1324 1325 /* 1326 * If there isn't whitespace, or a NULL terminator following 1327 * the prefix, then even though our prefix matched, the actual 1328 * token is longer, and we don't have a match. 1329 */ 1330 if (!isspace(*tail) && (*tail != '\0')) 1331 continue; 1332 1333 /* We have matched a valid control directive */ 1334 break; 1335 } 1336 1337 /* Advance input to end of the current line */ 1338 advance_to_eol(&mf->mf_next); 1339 1340 /* 1341 * Set up a temporary mapfile descriptor to reference the 1342 * argument string. The benefit of this second block, is that 1343 * we can advance the real one to the next line now, which allows 1344 * us to return at any time knowing that the input has been moved 1345 * to the proper spot. This simplifies the error cases. 1346 * 1347 * If we had a match, tail points at the start of the string. 1348 * Otherwise, we want to point at the end of the line. 1349 */ 1350 arg_mf = *mf; 1351 if (mdptr->md_name == NULL) 1352 arg_mf.mf_text = arg_mf.mf_next; 1353 else 1354 arg_mf.mf_text = arg_mf.mf_next = tail; 1355 1356 /* 1357 * Null terminate the arguments, and advance the main mapfile 1358 * state block to the next line. 1359 */ 1360 if (*mf->mf_next == '\n') { 1361 *mf->mf_next++ = '\0'; 1362 mf->mf_lineno++; 1363 } 1364 1365 /* Skip leading whitespace to arguments */ 1366 while (isspace(*arg_mf.mf_next)) 1367 arg_mf.mf_next++; 1368 1369 /* Strip off any comment present on the line */ 1370 for (tail = arg_mf.mf_next; *tail; tail++) 1371 if (*tail == '#') { 1372 *tail = '\0'; 1373 break; 1374 } 1375 1376 /* 1377 * Process the arguments as necessary depending on their type. 1378 * If this control directive is nested inside a surrounding context 1379 * that is not currently passing text, then we skip the argument 1380 * evaluation. This follows the behavior of the C preprocessor, 1381 * which only examines enough to detect the operation within 1382 * a disabled section, without issuing errors about the arguments. 1383 */ 1384 if (pass || (parent_pass && (mdptr->md_op == CDIR_T_ELIF))) { 1385 switch (mdptr->md_arg) { 1386 case ARG_T_NONE: 1387 if (*arg_mf.mf_next == '\0') 1388 break; 1389 /* Args are present, but not wanted */ 1390 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_REQNOARG), 1391 mdptr->md_name); 1392 return (FALSE); 1393 1394 case ARG_T_EXPR: 1395 /* Ensure that arguments are present */ 1396 if (*arg_mf.mf_next == '\0') 1397 goto error_reqarg; 1398 expr_eval = cexp_eval_expr(&arg_mf); 1399 if (expr_eval == -1) 1400 return (FALSE); 1401 break; 1402 1403 case ARG_T_ID: 1404 /* Ensure that arguments are present */ 1405 if (*arg_mf.mf_next == '\0') 1406 goto error_reqarg; 1407 if (!cexp_ident_validate(&arg_mf, NULL, 1408 mdptr->md_name)) 1409 return (FALSE); 1410 break; 1411 1412 case ARG_T_STR: 1413 /* Ensure that arguments are present */ 1414 if (*arg_mf.mf_next == '\0') 1415 goto error_reqarg; 1416 /* Remove trailing whitespace */ 1417 tail = arg_mf.mf_next + strlen(arg_mf.mf_next); 1418 while ((tail > arg_mf.mf_next) && 1419 isspace(*(tail -1))) 1420 tail--; 1421 *tail = '\0'; 1422 break; 1423 } 1424 } 1425 1426 /* 1427 * Carry out the specified control directive: 1428 */ 1429 if (!STACK_IS_EMPTY(lms.lms_cdir_stack)) 1430 level = &STACK_TOP(lms.lms_cdir_stack); 1431 1432 switch (mdptr->md_op) { 1433 case CDIR_T_UNKNOWN: /* Unrecognized control directive */ 1434 if (!pass) 1435 break; 1436 mf_fatal0(&arg_mf, MSG_INTL(MSG_MAP_CDIR_BAD)); 1437 return (FALSE); 1438 1439 case CDIR_T_ADD: 1440 if (pass && !cexp_ident_add(&arg_mf, arg_mf.mf_next)) 1441 return (FALSE); 1442 break; 1443 1444 case CDIR_T_CLEAR: 1445 if (pass) 1446 cexp_ident_clear(&arg_mf, arg_mf.mf_next); 1447 break; 1448 1449 case CDIR_T_ERROR: 1450 if (!pass) 1451 break; 1452 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_ERROR), 1453 arg_mf.mf_next); 1454 return (FALSE); 1455 1456 case CDIR_T_VERSION: 1457 /* 1458 * A $mapfile_version control directive can only appear 1459 * as the first directive in a mapfile, and is used to 1460 * determine the syntax for the rest of the file. It's 1461 * too late to be using it here. 1462 */ 1463 if (!pass) 1464 break; 1465 mf_fatal0(&arg_mf, MSG_INTL(MSG_MAP_CDIR_REPVER)); 1466 return (FALSE); 1467 1468 case CDIR_T_IF: 1469 /* Push a new level on the conditional input stack */ 1470 if (STACK_RESERVE(lms.lms_cdir_stack, CDIR_STACK_INIT) == 0) 1471 return (FALSE); 1472 level = &lms.lms_cdir_stack.stk_s[lms.lms_cdir_stack.stk_n++]; 1473 level->cdl_if_lineno = arg_mf.mf_lineno; 1474 level->cdl_else_lineno = 0; 1475 1476 /* 1477 * If previous level is not passing, this level is disabled. 1478 * Otherwise, the expression value determines what happens. 1479 */ 1480 if (pass) { 1481 level->cdl_done = level->cdl_pass = expr_eval; 1482 } else { 1483 level->cdl_done = 1; 1484 level->cdl_pass = 0; 1485 } 1486 break; 1487 1488 case CDIR_T_ELIF: 1489 /* $elif requires an open $if construct */ 1490 if (STACK_IS_EMPTY(lms.lms_cdir_stack)) { 1491 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_NOIF), 1492 MSG_ORIG(MSG_STR_CDIR_ELIF)); 1493 return (FALSE); 1494 } 1495 1496 /* $elif cannot follow $else */ 1497 if (level->cdl_else_lineno > 0) { 1498 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_ELSE), 1499 MSG_ORIG(MSG_STR_CDIR_ELIF), 1500 EC_LINENO(level->cdl_else_lineno)); 1501 return (FALSE); 1502 } 1503 1504 /* 1505 * Accept text from $elif if the level isn't already 1506 * done and the expression evaluates to true. 1507 */ 1508 level->cdl_pass = !level->cdl_done && expr_eval; 1509 if (level->cdl_pass) 1510 level->cdl_done = 1; 1511 break; 1512 1513 case CDIR_T_ELSE: 1514 /* $else requires an open $if construct */ 1515 if (STACK_IS_EMPTY(lms.lms_cdir_stack)) { 1516 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_NOIF), 1517 MSG_ORIG(MSG_STR_CDIR_ELSE)); 1518 return (FALSE); 1519 } 1520 1521 /* There can only be one $else in the chain */ 1522 if (level->cdl_else_lineno > 0) { 1523 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_ELSE), 1524 MSG_ORIG(MSG_STR_CDIR_ELSE), 1525 EC_LINENO(level->cdl_else_lineno)); 1526 return (FALSE); 1527 } 1528 level->cdl_else_lineno = arg_mf.mf_lineno; 1529 1530 /* Accept text from $else if the level isn't already done */ 1531 level->cdl_pass = !level->cdl_done; 1532 level->cdl_done = 1; 1533 break; 1534 1535 case CDIR_T_ENDIF: 1536 /* $endif requires an open $if construct */ 1537 if (STACK_IS_EMPTY(lms.lms_cdir_stack)) { 1538 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_NOIF), 1539 MSG_ORIG(MSG_STR_CDIR_ENDIF)); 1540 return (FALSE); 1541 } 1542 if (--lms.lms_cdir_stack.stk_n > 0) 1543 level = &STACK_TOP(lms.lms_cdir_stack); 1544 break; 1545 1546 default: 1547 return (FALSE); 1548 } 1549 1550 /* Evaluating the control directive above can change pass status */ 1551 expr_eval = STACK_IS_EMPTY(lms.lms_cdir_stack) || 1552 STACK_TOP(lms.lms_cdir_stack).cdl_pass; 1553 if (expr_eval != pass) { 1554 pass = expr_eval; 1555 DBG_CALL(Dbg_map_pass(arg_mf.mf_ofl->ofl_lml, pass, 1556 arg_mf.mf_name, arg_mf.mf_lineno, mdptr->md_name)); 1557 } 1558 1559 /* 1560 * At this point, we have processed a control directive, 1561 * updated our conditional state stack, and the input is 1562 * positioned at the start of the line following the directive. 1563 * If the current level is accepting input, then give control 1564 * back to ld_map_gettoken() to resume its normal operation. 1565 */ 1566 if (pass) 1567 return (TRUE); 1568 1569 /* 1570 * The current level is not accepting input. Only another 1571 * control directive can change this, so read and discard input 1572 * until we encounter one of the following: 1573 * 1574 * EOF: Return and let ld_map_gettoken() report it 1575 * Control Directive: Restart this function / evaluate new directive 1576 */ 1577 while (*mf->mf_next != '\0') { 1578 /* Skip leading whitespace */ 1579 while (isspace_nonl(*mf->mf_next)) 1580 mf->mf_next++; 1581 1582 /* 1583 * Control directives start with a '$'. If we hit 1584 * one, restart the function at this point 1585 */ 1586 if (*mf->mf_next == '$') 1587 goto restart; 1588 1589 /* Not a control directive, so advance input to next line */ 1590 advance_to_eol(&mf->mf_next); 1591 if (*mf->mf_next == '\n') { 1592 mf->mf_lineno++; 1593 mf->mf_next++; 1594 } 1595 } 1596 1597 assert(*mf->mf_next == '\0'); 1598 return (TRUE); 1599 1600 /* 1601 * Control directives that require an argument that is not present 1602 * jump here to report the error and exit. 1603 */ 1604 error_reqarg: 1605 mf_fatal(&arg_mf, MSG_INTL(MSG_MAP_CDIR_REQARG), mdptr->md_name); 1606 return (FALSE); 1607 1608 } 1609 1610 #ifndef _ELF64 1611 /* 1612 * Convert a string to lowercase. 1613 */ 1614 void 1615 ld_map_lowercase(char *str) 1616 { 1617 while (*str = tolower(*str)) 1618 str++; 1619 } 1620 #endif 1621 1622 /* 1623 * Wrappper on strtoul()/strtoull(), adapted to return an Xword. 1624 * 1625 * entry: 1626 * str - Pointer to string to be converted. 1627 * endptr - As documented for strtoul(3C). Either NULL, or 1628 * address of pointer to receive the address of the first 1629 * unused character in str (called "final" in strtoul(3C)). 1630 * ret_value - Address of Xword variable to receive result. 1631 * 1632 * exit: 1633 * On success, *ret_value receives the result, *endptr is updated if 1634 * endptr is non-NULL, and STRTOXWORD_OK is returned. 1635 * On failure, STRTOXWORD_TOBIG is returned if an otherwise valid 1636 * value was too large, and STRTOXWORD_BAD is returned if the string 1637 * is malformed. 1638 */ 1639 ld_map_strtoxword_t 1640 ld_map_strtoxword(const char *restrict str, char **restrict endptr, 1641 Xword *ret_value) 1642 { 1643 #if defined(_ELF64) /* _ELF64 */ 1644 #define FUNC strtoull /* Function to use */ 1645 #define FUNC_MAX ULLONG_MAX /* Largest value returned by FUNC */ 1646 #define XWORD_MAX ULLONG_MAX /* Largest Xword value */ 1647 uint64_t value; /* Variable of FUNC return type */ 1648 #else /* _ELF32 */ 1649 #define FUNC strtoul 1650 #define FUNC_MAX ULONG_MAX 1651 #define XWORD_MAX UINT_MAX 1652 ulong_t value; 1653 #endif 1654 1655 char *endptr_local; /* Used if endptr is NULL */ 1656 1657 if (endptr == NULL) 1658 endptr = &endptr_local; 1659 1660 errno = 0; 1661 value = FUNC(str, endptr, 0); 1662 if ((errno != 0) || (str == *endptr)) { 1663 if (value == FUNC_MAX) 1664 return (STRTOXWORD_TOOBIG); 1665 else 1666 return (STRTOXWORD_BAD); 1667 } 1668 1669 /* 1670 * If this is a 64-bit linker building an ELFCLASS32 object, 1671 * the FUNC return type is a 64-bit value, while an Xword is 1672 * 32-bit. It is possible for FUNC to be able to convert a value 1673 * too large for our return type. 1674 */ 1675 #if FUNC_MAX != XWORD_MAX 1676 if (value > XWORD_MAX) 1677 return (STRTOXWORD_TOOBIG); 1678 #endif 1679 1680 *ret_value = value; 1681 return (STRTOXWORD_OK); 1682 1683 #undef FUNC 1684 #undef FUNC_MAX 1685 #undef XWORD_MAC 1686 } 1687 1688 /* 1689 * Convert the unsigned integer value at the current mapfile input 1690 * into binary form. All numeric values in mapfiles are treated as 1691 * unsigned integers of the appropriate width for an address on the 1692 * given target. Values can be decimal, hex, or octal. 1693 * 1694 * entry: 1695 * str - String to process. 1696 * value - Address of variable to receive resulting value. 1697 * notail - If TRUE, an error is issued if non-whitespace 1698 * characters other than '#' (comment) are found following 1699 * the numeric value before the end of line. 1700 * 1701 * exit: 1702 * On success: 1703 * - *str is advanced to the next character following the value 1704 * - *value receives the value 1705 * - Returns TRUE (1). 1706 * On failure, returns FALSE (0). 1707 */ 1708 static Boolean 1709 ld_map_getint(Mapfile *mf, ld_map_tkval_t *value, Boolean notail) 1710 { 1711 ld_map_strtoxword_t s2xw_ret; 1712 ld_map_npatch_t np; 1713 char *endptr; 1714 char *errstr = mf->mf_next; 1715 1716 value->tkv_int.tkvi_str = mf->mf_next; 1717 s2xw_ret = ld_map_strtoxword(mf->mf_next, &endptr, 1718 &value->tkv_int.tkvi_value); 1719 if (s2xw_ret != STRTOXWORD_OK) { 1720 null_patch_eol(mf->mf_next, &np); 1721 if (s2xw_ret == STRTOXWORD_TOOBIG) 1722 mf_fatal(mf, MSG_INTL(MSG_MAP_VALUELIMIT), errstr); 1723 else 1724 mf_fatal(mf, MSG_INTL(MSG_MAP_MALVALUE), errstr); 1725 null_patch_undo(&np); 1726 return (FALSE); 1727 } 1728 1729 /* Advance position to item following value, skipping whitespace */ 1730 value->tkv_int.tkvi_cnt = endptr - mf->mf_next; 1731 mf->mf_next = endptr; 1732 while (isspace_nonl(*mf->mf_next)) 1733 mf->mf_next++; 1734 1735 /* If requested, ensure there's nothing left */ 1736 if (notail && (*mf->mf_next != '\n') && (*mf->mf_next != '#') && 1737 (*mf->mf_next != '\0')) { 1738 null_patch_eol(mf->mf_next, &np); 1739 mf_fatal(mf, MSG_INTL(MSG_MAP_BADVALUETAIL), errstr); 1740 null_patch_undo(&np); 1741 return (FALSE); 1742 } 1743 1744 return (TRUE); 1745 } 1746 1747 /* 1748 * Convert a an unquoted identifier into a TK_STRING token, using the 1749 * rules for syntax version in use. Used exclusively by ld_map_gettoken(). 1750 * 1751 * entry: 1752 * mf - Mapfile descriptor, positioned to the first character of 1753 * the string. 1754 * flags - Bitmask of options to control ld_map_gettoken()s behavior 1755 * tkv- Address of pointer to variable to receive token value. 1756 * 1757 * exit: 1758 * On success, mf is advanced past the token, tkv is updated with 1759 * the string, and TK_STRING is returned. On error, TK_ERROR is returned. 1760 */ 1761 inline static Token 1762 gettoken_ident(Mapfile *mf, int flags, ld_map_tkval_t *tkv) 1763 { 1764 char *end; 1765 Token tok; 1766 ld_map_npatch_t np; 1767 1768 tkv->tkv_str = mf->mf_next; 1769 if ((end = ident_delimit(mf)) == NULL) 1770 return (TK_ERROR); 1771 mf->mf_next = end; 1772 1773 /* 1774 * One advantage of reading the entire mapfile into memory is that 1775 * we can access the strings within it without having to allocate 1776 * more memory or make copies. In order to do that, we need to NULL 1777 * terminate this identifier. That is going to overwrite the 1778 * following character. The problem this presents is that the next 1779 * character may well be the first character of a subsequent token. 1780 * The solution to this is: 1781 * 1782 * 1) Disallow the case where the next character is able to 1783 * start a string. This is not legal mapfile syntax anyway, 1784 * so catching it here simplifies matters. 1785 * 2) Copy the character into the special mf->mf_next_ch 1786 * 3) The next call to ld_map_gettoken() checks mf->mf_next_ch, 1787 * and if it is non-0, uses it instead of dereferencing the 1788 * mf_next pointer. 1789 */ 1790 tok = (*mf->mf_next & 0x80) ? 1791 TK_OP_ILLCHR : mf->mf_tokdisp[*mf->mf_next]; 1792 switch (tok) { 1793 case TK_OP_BADCHR: 1794 null_patch_eol(mf->mf_next, &np); 1795 mf_fatal(mf, MSG_INTL(MSG_MAP_BADCHAR), mf->mf_next); 1796 null_patch_undo(&np); 1797 return (TK_ERROR); 1798 1799 case TK_OP_SIMQUOTE: 1800 case TK_OP_CQUOTE: 1801 case TK_OP_CDIR: 1802 case TK_OP_NUM: 1803 case TK_OP_ID: 1804 null_patch_eol(mf->mf_next, &np); 1805 mf_fatal(mf, MSG_INTL(MSG_MAP_WSNEEDED), mf->mf_next); 1806 null_patch_undo(&np); 1807 return (TK_ERROR); 1808 } 1809 1810 /* Null terminate, saving the replaced character */ 1811 mf->mf_next_ch = *mf->mf_next; 1812 *mf->mf_next = '\0'; 1813 1814 if (flags & TK_F_STRLC) 1815 ld_map_lowercase(tkv->tkv_str); 1816 return (TK_STRING); 1817 } 1818 1819 /* 1820 * Convert a quoted string into a TK_STRING token, using simple 1821 * quoting rules: 1822 * - Start and end quotes must be present and match 1823 * - There are no special characters or escape sequences. 1824 * This function is used exclusively by ld_map_gettoken(). 1825 * 1826 * entry: 1827 * mf - Mapfile descriptor, positioned to the opening quote character. 1828 * flags - Bitmask of options to control ld_map_gettoken()s behavior 1829 * tkv- Address of pointer to variable to receive token value. 1830 * 1831 * exit: 1832 * On success, mf is advanced past the token, tkv is updated with 1833 * the string, and TK_STRING is returned. On error, TK_ERROR is returned. 1834 */ 1835 inline static Token 1836 gettoken_simquote_str(Mapfile *mf, int flags, ld_map_tkval_t *tkv) 1837 { 1838 char *str, *end; 1839 char quote; 1840 1841 str = mf->mf_next++; 1842 quote = *str; 1843 end = mf->mf_next; 1844 while ((*end != '\0') && (*end != '\n') && (*end != quote)) 1845 end++; 1846 if (*end != quote) { 1847 ld_map_npatch_t np; 1848 1849 null_patch_eol(end, &np); 1850 mf_fatal(mf, MSG_INTL(MSG_MAP_NOTERM), str); 1851 null_patch_undo(&np); 1852 return (TK_ERROR); 1853 } 1854 1855 /* 1856 * end is pointing at the closing quote. We can turn that into NULL 1857 * termination for the string without needing to restore it later. 1858 */ 1859 *end = '\0'; 1860 mf->mf_next = end + 1; 1861 tkv->tkv_str = str + 1; /* Skip opening quote */ 1862 if (flags & TK_F_STRLC) 1863 ld_map_lowercase(tkv->tkv_str); 1864 return (TK_STRING); 1865 } 1866 1867 /* 1868 * Convert a quoted string into a TK_STRING token, using C string literal 1869 * quoting rules: 1870 * - Start and end quotes must be present and match 1871 * - Backslash is an escape, used to introduce special characters 1872 * This function is used exclusively by ld_map_gettoken(). 1873 * 1874 * entry: 1875 * mf - Mapfile descriptor, positioned to the opening quote character. 1876 * flags - Bitmask of options to control ld_map_gettoken()s behavior 1877 * tkv- Address of pointer to variable to receive token value. 1878 * 1879 * exit: 1880 * On success, mf is advanced past the token, tkv is updated with 1881 * the string, and TK_STRING is returned. On error, TK_ERROR is returned. 1882 */ 1883 inline static Token 1884 gettoken_cquote_str(Mapfile *mf, int flags, ld_map_tkval_t *tkv) 1885 { 1886 char *str, *cur, *end; 1887 char quote; 1888 int c; 1889 1890 /* 1891 * This function goes through the quoted string and copies 1892 * it on top of itself, replacing escape sequences with the 1893 * characters they denote. There is always enough room for this, 1894 * because escapes are multi-character sequences that are converted 1895 * to single character results. 1896 */ 1897 str = mf->mf_next++; 1898 quote = *str; 1899 cur = end = mf->mf_next; 1900 for (c = *end++; (c != '\0') && (c != '\n') && (c != quote); 1901 c = *end++) { 1902 if (c == '\\') { 1903 c = conv_translate_c_esc(&end); 1904 if (c == -1) { 1905 mf_fatal(mf, MSG_INTL(MSG_MAP_BADCESC), *end); 1906 return (TK_ERROR); 1907 } 1908 } 1909 *cur++ = c; 1910 } 1911 *cur = '\0'; /* terminate the result */ 1912 if (c != quote) { 1913 ld_map_npatch_t np; 1914 1915 null_patch_eol(end, &np); 1916 mf_fatal(mf, MSG_INTL(MSG_MAP_NOTERM), str); 1917 null_patch_undo(&np); 1918 return (TK_ERROR); 1919 } 1920 1921 /* end is pointing one character past the closing quote */ 1922 mf->mf_next = end; 1923 tkv->tkv_str = str + 1; /* Skip opening quote */ 1924 if (flags & TK_F_STRLC) 1925 ld_map_lowercase(tkv->tkv_str); 1926 return (TK_STRING); 1927 } 1928 1929 /* 1930 * Get a token from the mapfile. 1931 * 1932 * entry: 1933 * mf - Mapfile descriptor 1934 * flags - Bitmask of options to control ld_map_gettoken()s behavior 1935 * tkv- Address of pointer to variable to receive token value. 1936 * 1937 * exit: 1938 * Returns one of the TK_* values, to report the result. If the resulting 1939 * token has a value (TK_STRING / TK_INT), and tkv is non-NULL, tkv 1940 * is filled in with the resulting value. 1941 */ 1942 Token 1943 ld_map_gettoken(Mapfile *mf, int flags, ld_map_tkval_t *tkv) 1944 { 1945 int cdir_allow, ch; 1946 Token tok; 1947 ld_map_npatch_t np; 1948 1949 /* 1950 * Mapfile control directives all start with a '$' character. However, 1951 * they are only valid when they are the first thing on a line. That 1952 * happens on the first call to ld_map_gettoken() for a new a new 1953 * mapfile, as tracked with lms.lms_cdir_valid, and immediately 1954 * following each newline seen in the file. 1955 */ 1956 cdir_allow = lms.lms_cdir_valid; 1957 lms.lms_cdir_valid = 0; 1958 1959 /* Cycle through the characters looking for tokens. */ 1960 for (;;) { 1961 /* 1962 * Process the next character. This is normally *mf->mf_next, 1963 * but if mf->mf_next_ch is non-0, then it contains the 1964 * character, and *mf->mf_next contains a NULL termination 1965 * from the TK_STRING token returned on the previous call. 1966 * 1967 * gettoken_ident() ensures that this is never done to 1968 * a character that starts a string. 1969 */ 1970 if (mf->mf_next_ch == 0) { 1971 ch = *mf->mf_next; 1972 } else { 1973 ch = mf->mf_next_ch; 1974 mf->mf_next_ch = 0; /* Reset */ 1975 } 1976 1977 /* Map the character to a dispatch action */ 1978 tok = (ch & 0x80) ? TK_OP_ILLCHR : mf->mf_tokdisp[ch]; 1979 1980 /* 1981 * Items that require processing are identified as OP tokens. 1982 * We process them, and return a result non-OP token. 1983 * 1984 * Non-OP tokens are single character tokens, and we return 1985 * them immediately. 1986 */ 1987 switch (tok) { 1988 case TK_OP_EOF: 1989 /* If EOFOK is set, quietly report it as TK_EOF */ 1990 if ((flags & TK_F_EOFOK) != 0) 1991 return (TK_EOF); 1992 1993 /* Treat it as a standard error */ 1994 mf_fatal0(mf, MSG_INTL(MSG_MAP_PREMEOF)); 1995 return (TK_ERROR); 1996 1997 case TK_OP_ILLCHR: 1998 mf_fatal(mf, MSG_INTL(MSG_MAP_ILLCHAR), ch); 1999 mf->mf_next++; 2000 return (TK_ERROR); 2001 2002 case TK_OP_BADCHR: 2003 tk_op_badchr: 2004 null_patch_eol(mf->mf_next, &np); 2005 mf_fatal(mf, MSG_INTL(MSG_MAP_BADCHAR), mf->mf_next); 2006 null_patch_undo(&np); 2007 mf->mf_next++; 2008 return (TK_ERROR); 2009 2010 case TK_OP_WS: /* White space */ 2011 mf->mf_next++; 2012 break; 2013 2014 case TK_OP_NL: /* White space too, but bump line number. */ 2015 mf->mf_next++; 2016 mf->mf_lineno++; 2017 cdir_allow = 1; 2018 break; 2019 2020 case TK_OP_SIMQUOTE: 2021 if (flags & TK_F_KEYWORD) 2022 goto tk_op_badkwquote; 2023 return (gettoken_simquote_str(mf, flags, tkv)); 2024 2025 case TK_OP_CQUOTE: 2026 if (flags & TK_F_KEYWORD) { 2027 tk_op_badkwquote: 2028 null_patch_eol(mf->mf_next, &np); 2029 mf_fatal(mf, MSG_INTL(MSG_MAP_BADKWQUOTE), 2030 mf->mf_next); 2031 null_patch_undo(&np); 2032 mf->mf_next++; 2033 return (TK_ERROR); 2034 } 2035 return (gettoken_cquote_str(mf, flags, tkv)); 2036 2037 case TK_OP_CMT: 2038 advance_to_eol(&mf->mf_next); 2039 break; 2040 2041 case TK_OP_CDIR: 2042 /* 2043 * Control directives are only valid at the start 2044 * of a line. 2045 */ 2046 if (!cdir_allow) { 2047 null_patch_eol(mf->mf_next, &np); 2048 mf_fatal(mf, MSG_INTL(MSG_MAP_CDIR_NOTBOL), 2049 mf->mf_next); 2050 null_patch_undo(&np); 2051 mf->mf_next++; 2052 return (TK_ERROR); 2053 } 2054 if (!cdir_process(mf)) 2055 return (TK_ERROR); 2056 break; 2057 2058 case TK_OP_NUM: /* Decimal, hex(0x...), or octal (0...) value */ 2059 if (!ld_map_getint(mf, tkv, FALSE)) 2060 return (TK_ERROR); 2061 return (TK_INT); 2062 2063 case TK_OP_ID: /* Unquoted identifier */ 2064 return (gettoken_ident(mf, flags, tkv)); 2065 2066 case TK_OP_CEQUAL: /* += or -= */ 2067 if (*(mf->mf_next + 1) != '=') 2068 goto tk_op_badchr; 2069 tok = (ch == '+') ? TK_PLUSEQ : TK_MINUSEQ; 2070 mf->mf_next += 2; 2071 return (tok); 2072 2073 default: /* Non-OP token */ 2074 mf->mf_next++; 2075 return (tok); 2076 } 2077 } 2078 } 2079 2080 /* 2081 * Given a token and value returned by ld_map_gettoken(), return a string 2082 * representation of it suitable for use in an error message. 2083 * 2084 * entry: 2085 * tok - Token code. Must not be an OP-token 2086 * tkv - Token value 2087 */ 2088 const char * 2089 ld_map_tokenstr(Token tok, ld_map_tkval_t *tkv, Conv_inv_buf_t *inv_buf) 2090 { 2091 size_t cnt; 2092 2093 switch (tok) { 2094 case TK_ERROR: 2095 return (MSG_ORIG(MSG_STR_ERROR)); 2096 case TK_EOF: 2097 return (MSG_ORIG(MSG_STR_EOF)); 2098 case TK_STRING: 2099 return (tkv->tkv_str); 2100 case TK_COLON: 2101 return (MSG_ORIG(MSG_QSTR_COLON)); 2102 case TK_SEMICOLON: 2103 return (MSG_ORIG(MSG_QSTR_SEMICOLON)); 2104 case TK_EQUAL: 2105 return (MSG_ORIG(MSG_QSTR_EQUAL)); 2106 case TK_PLUSEQ: 2107 return (MSG_ORIG(MSG_QSTR_PLUSEQ)); 2108 case TK_MINUSEQ: 2109 return (MSG_ORIG(MSG_QSTR_MINUSEQ)); 2110 case TK_ATSIGN: 2111 return (MSG_ORIG(MSG_QSTR_ATSIGN)); 2112 case TK_DASH: 2113 return (MSG_ORIG(MSG_QSTR_DASH)); 2114 case TK_LEFTBKT: 2115 return (MSG_ORIG(MSG_QSTR_LEFTBKT)); 2116 case TK_RIGHTBKT: 2117 return (MSG_ORIG(MSG_QSTR_RIGHTBKT)); 2118 case TK_PIPE: 2119 return (MSG_ORIG(MSG_QSTR_PIPE)); 2120 case TK_INT: 2121 cnt = tkv->tkv_int.tkvi_cnt; 2122 if (cnt >= sizeof (inv_buf->buf)) 2123 cnt = sizeof (inv_buf->buf) - 1; 2124 (void) memcpy(inv_buf->buf, tkv->tkv_int.tkvi_str, cnt); 2125 inv_buf->buf[cnt] = '\0'; 2126 return (inv_buf->buf); 2127 case TK_STAR: 2128 return (MSG_ORIG(MSG_QSTR_STAR)); 2129 case TK_BANG: 2130 return (MSG_ORIG(MSG_QSTR_BANG)); 2131 default: 2132 assert(0); 2133 break; 2134 } 2135 2136 /*NOTREACHED*/ 2137 return (MSG_INTL(MSG_MAP_INTERR)); 2138 } 2139 2140 /* 2141 * Advance the input to the first non-empty line, and determine 2142 * the mapfile version. The version is specified by the mapfile 2143 * using a $mapfile_version directive. The original System V 2144 * syntax lacks this directive, and we use that fact to identify 2145 * such files. SysV mapfile are implicitly defined to have version 1. 2146 * 2147 * entry: 2148 * ofl - Output file descriptor 2149 * mf - Mapfile block 2150 * 2151 * exit: 2152 * On success, updates mf->mf_version, and returns TRUE (1). 2153 * On failure, returns FALSE (0). 2154 */ 2155 static Boolean 2156 mapfile_version(Mapfile *mf) 2157 { 2158 char *line_start = mf->mf_next; 2159 Boolean cont = TRUE; 2160 Boolean status = TRUE; /* Assume success */ 2161 Token tok; 2162 2163 mf->mf_version = MFV_SYSV; 2164 2165 /* 2166 * Cycle through the characters looking for tokens. Although the 2167 * true version is not known yet, we use the v2 dispatch table. 2168 * It contains control directives, which we need for this search, 2169 * and the other TK_OP_ tokens we will recognize and act on are the 2170 * same for both tables. 2171 * 2172 * It is important not to process any tokens that would lead to 2173 * a non-OP token: 2174 * 2175 * - The version is required to interpret them 2176 * - Our mapfile descriptor is not fully initialized, 2177 * attempts to run that code will crash the program. 2178 */ 2179 while (cont) { 2180 /* Map the character to a dispatch action */ 2181 tok = (*mf->mf_next & 0x80) ? 2182 TK_OP_ILLCHR : gettok_dispatch_v2[*mf->mf_next]; 2183 2184 switch (tok) { 2185 case TK_OP_WS: /* White space */ 2186 mf->mf_next++; 2187 break; 2188 2189 case TK_OP_NL: /* White space too, but bump line number. */ 2190 mf->mf_next++; 2191 mf->mf_lineno++; 2192 break; 2193 2194 case TK_OP_CMT: 2195 advance_to_eol(&mf->mf_next); 2196 break; 2197 2198 case TK_OP_CDIR: 2199 /* 2200 * Control directives are only valid at the start 2201 * of a line. However, as we have not yet seen 2202 * a token, we do not need to test for this, and 2203 * can safely assume that we are at the start. 2204 */ 2205 if (!strncasecmp(mf->mf_next, 2206 MSG_ORIG(MSG_STR_CDIR_MFVER), 2207 MSG_STR_CDIR_MFVER_SIZE) && 2208 isspace_nonl(*(mf->mf_next + 2209 MSG_STR_CDIR_MFVER_SIZE))) { 2210 ld_map_tkval_t ver; 2211 2212 mf->mf_next += MSG_STR_CDIR_MFVER_SIZE + 1; 2213 if (!ld_map_getint(mf, &ver, TRUE)) { 2214 status = cont = FALSE; 2215 break; 2216 } 2217 /* 2218 * Is it a valid version? Note that we 2219 * intentionally do not allow you to 2220 * specify version 1 using the $mapfile_version 2221 * syntax, because that's reserved to version 2222 * 2 and up. 2223 */ 2224 if ((ver.tkv_int.tkvi_value < 2) || 2225 (ver.tkv_int.tkvi_value >= MFV_NUM)) { 2226 const char *fmt; 2227 2228 fmt = (ver.tkv_int.tkvi_value < 2) ? 2229 MSG_INTL(MSG_MAP_CDIR_BADVDIR) : 2230 MSG_INTL(MSG_MAP_CDIR_BADVER); 2231 mf_fatal(mf, fmt, 2232 EC_WORD(ver.tkv_int.tkvi_value)); 2233 status = cont = FALSE; 2234 break; 2235 } 2236 mf->mf_version = ver.tkv_int.tkvi_value; 2237 cont = FALSE; /* Version recovered. All done */ 2238 break; 2239 } 2240 /* 2241 * Not a version directive. Reset the current position 2242 * to the start of the current line and stop here. 2243 * SysV syntax applies. 2244 */ 2245 mf->mf_next = line_start; 2246 cont = FALSE; 2247 break; 2248 2249 default: 2250 /* 2251 * If we see anything else, then stop at this point. 2252 * The file has System V syntax (version 1), and the 2253 * next token should be interpreted as such. 2254 */ 2255 cont = FALSE; 2256 break; 2257 } 2258 } 2259 2260 return (status); 2261 } 2262 2263 /* 2264 * Parse the mapfile. 2265 */ 2266 Boolean 2267 ld_map_parse(const char *mapfile, Ofl_desc *ofl) 2268 { 2269 struct stat stat_buf; /* stat of mapfile */ 2270 int mapfile_fd; /* descriptor for mapfile */ 2271 int err; 2272 Mapfile *mf; /* Mapfile descriptor */ 2273 size_t name_len; /* strlen(mapfile) */ 2274 2275 /* 2276 * Determine if we're dealing with a file or a directory. 2277 */ 2278 if (stat(mapfile, &stat_buf) == -1) { 2279 err = errno; 2280 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYS_STAT), mapfile, 2281 strerror(err)); 2282 return (FALSE); 2283 } 2284 if (S_ISDIR(stat_buf.st_mode)) { 2285 DIR *dirp; 2286 struct dirent *denp; 2287 2288 /* 2289 * Open the directory and interpret each visible file as a 2290 * mapfile. 2291 */ 2292 if ((dirp = opendir(mapfile)) == NULL) 2293 return (TRUE); 2294 2295 while ((denp = readdir(dirp)) != NULL) { 2296 char path[PATH_MAX]; 2297 2298 /* 2299 * Ignore any hidden filenames. Construct the full 2300 * pathname to the new mapfile. 2301 */ 2302 if (*denp->d_name == '.') 2303 continue; 2304 (void) snprintf(path, PATH_MAX, MSG_ORIG(MSG_STR_PATH), 2305 mapfile, denp->d_name); 2306 if (!ld_map_parse(path, ofl)) 2307 return (FALSE); 2308 } 2309 (void) closedir(dirp); 2310 return (TRUE); 2311 } else if (!S_ISREG(stat_buf.st_mode)) { 2312 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYS_NOTREG), mapfile); 2313 return (FALSE); 2314 } 2315 2316 /* Open file */ 2317 if ((mapfile_fd = open(mapfile, O_RDONLY)) == -1) { 2318 err = errno; 2319 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), mapfile, 2320 strerror(err)); 2321 return (FALSE); 2322 } 2323 2324 /* 2325 * Allocate enough memory to hold the state block, mapfile name, 2326 * and mapfile text. Text has alignment 1, so it can follow the 2327 * state block without padding. 2328 */ 2329 name_len = strlen(mapfile) + 1; 2330 mf = libld_malloc(sizeof (*mf) + name_len + stat_buf.st_size + 1); 2331 if (mf == NULL) 2332 return (FALSE); 2333 mf->mf_ofl = ofl; 2334 mf->mf_name = (char *)(mf + 1); 2335 (void) strcpy(mf->mf_name, mapfile); 2336 mf->mf_text = mf->mf_name + name_len; 2337 if (read(mapfile_fd, mf->mf_text, stat_buf.st_size) != 2338 stat_buf.st_size) { 2339 err = errno; 2340 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYS_READ), mapfile, 2341 strerror(err)); 2342 (void) close(mapfile_fd); 2343 return (FALSE); 2344 } 2345 (void) close(mapfile_fd); 2346 mf->mf_text[stat_buf.st_size] = '\0'; 2347 mf->mf_next = mf->mf_text; 2348 mf->mf_lineno = 1; 2349 mf->mf_next_ch = 0; /* No "lookahead" character yet */ 2350 mf->mf_ec_insndx = 0; /* Insert entrace criteria at top */ 2351 2352 /* 2353 * Read just enough from the mapfile to determine the version, 2354 * and then dispatch to the appropriate code for further processing 2355 */ 2356 if (!mapfile_version(mf)) 2357 return (FALSE); 2358 2359 /* 2360 * Start and continuation masks for unquoted identifier at this 2361 * mapfile version level. 2362 */ 2363 mf->mf_tkid_start = TKID_ATTR_START(mf->mf_version); 2364 mf->mf_tkid_cont = TKID_ATTR_CONT(mf->mf_version); 2365 2366 DBG_CALL(Dbg_map_parse(ofl->ofl_lml, mapfile, mf->mf_version)); 2367 2368 switch (mf->mf_version) { 2369 case MFV_SYSV: 2370 /* Guidance: Use newer mapfile syntax */ 2371 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_MF)) 2372 ld_eprintf(ofl, ERR_GUIDANCE, 2373 MSG_INTL(MSG_GUIDE_MAPFILE), mapfile); 2374 2375 mf->mf_tokdisp = gettok_dispatch_v1; 2376 if (!ld_map_parse_v1(mf)) 2377 return (FALSE); 2378 break; 2379 2380 case MFV_SOLARIS: 2381 mf->mf_tokdisp = gettok_dispatch_v2; 2382 STACK_RESET(lms.lms_cdir_stack); 2383 2384 /* 2385 * If the conditional expression identifier tree has not been 2386 * initialized, set it up. This is only done on the first 2387 * mapfile, because the identifier control directives accumulate 2388 * across all the mapfiles. 2389 */ 2390 if ((lms.lms_cexp_id == NULL) && !cexp_ident_init()) 2391 return (FALSE); 2392 2393 /* 2394 * Tell ld_map_gettoken() we will accept a '$' as starting a 2395 * control directive on the first call. Normally, they are 2396 * only allowed after a newline. 2397 */ 2398 lms.lms_cdir_valid = 1; 2399 2400 if (!ld_map_parse_v2(mf)) 2401 return (FALSE); 2402 2403 /* Did we leave any open $if control directives? */ 2404 if (!STACK_IS_EMPTY(lms.lms_cdir_stack)) { 2405 while (!STACK_IS_EMPTY(lms.lms_cdir_stack)) { 2406 cdir_level_t *level = 2407 &STACK_POP(lms.lms_cdir_stack); 2408 2409 mf_fatal(mf, MSG_INTL(MSG_MAP_CDIR_NOEND), 2410 EC_LINENO(level->cdl_if_lineno)); 2411 } 2412 return (FALSE); 2413 } 2414 break; 2415 } 2416 2417 return (TRUE); 2418 } 2419 2420 /* 2421 * Sort the segment list. This is necessary if a mapfile has set explicit 2422 * virtual addresses for segments, or defined a SEGMENT_ORDER directive. 2423 * 2424 * Only PT_LOAD segments can be assigned a virtual address. These segments can 2425 * be one of two types: 2426 * 2427 * - Standard segments for text, data or bss. These segments will have been 2428 * inserted before the default text (first PT_LOAD) segment. 2429 * 2430 * - Empty (reservation) segments. These segment will have been inserted at 2431 * the end of any default PT_LOAD segments. 2432 * 2433 * Any standard segments that are assigned a virtual address will be sorted, 2434 * and as their definitions precede any default PT_LOAD segments, these segments 2435 * will be assigned sections before any defaults. 2436 * 2437 * Any reservation segments are also sorted amoung themselves, as these segments 2438 * must still follow the standard default segments. 2439 */ 2440 static Boolean 2441 sort_seg_list(Ofl_desc *ofl) 2442 { 2443 APlist *sort_segs = NULL, *load_segs = NULL; 2444 Sg_desc *sgp1; 2445 Aliste idx1; 2446 Aliste nsegs; 2447 2448 2449 /* 2450 * We know the number of elements in the sorted list will be 2451 * the same as the original, so use this as the initial allocation 2452 * size for the replacement aplist. 2453 */ 2454 nsegs = aplist_nitems(ofl->ofl_segs); 2455 2456 2457 /* Add the items below SGID_TEXT to the list */ 2458 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) { 2459 if (sgp1->sg_id >= SGID_TEXT) 2460 break; 2461 2462 if (aplist_append(&sort_segs, sgp1, nsegs) == NULL) 2463 return (FALSE); 2464 } 2465 2466 /* 2467 * If there are any SEGMENT_ORDER items, add them, and set their 2468 * FLG_SG_ORDERED flag to identify them in debug output, and to 2469 * prevent them from being added again below. 2470 */ 2471 for (APLIST_TRAVERSE(ofl->ofl_segs_order, idx1, sgp1)) { 2472 if (aplist_append(&sort_segs, sgp1, nsegs) == NULL) 2473 return (FALSE); 2474 sgp1->sg_flags |= FLG_SG_ORDERED; 2475 } 2476 2477 /* 2478 * Add the loadable segments to another list in sorted order. 2479 */ 2480 DBG_CALL(Dbg_map_sort_title(ofl->ofl_lml, TRUE)); 2481 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) { 2482 DBG_CALL(Dbg_map_sort_seg(ofl->ofl_lml, ELFOSABI_SOLARIS, 2483 ld_targ.t_m.m_mach, sgp1)); 2484 2485 /* Only interested in PT_LOAD items not in SEGMENT_ORDER list */ 2486 if ((sgp1->sg_phdr.p_type != PT_LOAD) || 2487 (sgp1->sg_flags & FLG_SG_ORDERED)) 2488 continue; 2489 2490 /* 2491 * If the loadable segment does not contain a vaddr, simply 2492 * append it to the new list. 2493 */ 2494 if ((sgp1->sg_flags & FLG_SG_P_VADDR) == 0) { 2495 if (aplist_append(&load_segs, sgp1, AL_CNT_SEGMENTS) == 2496 NULL) 2497 return (FALSE); 2498 2499 } else { 2500 Aliste idx2; 2501 Sg_desc *sgp2; 2502 int inserted = 0; 2503 2504 /* 2505 * Traverse the segment list we are creating, looking 2506 * for a segment that defines a vaddr. 2507 */ 2508 for (APLIST_TRAVERSE(load_segs, idx2, sgp2)) { 2509 /* 2510 * Any real segments that contain vaddr's need 2511 * to be sorted. Any reservation segments also 2512 * need to be sorted. However, any reservation 2513 * segments should be placed after any real 2514 * segments. 2515 */ 2516 if (((sgp2->sg_flags & 2517 (FLG_SG_P_VADDR | FLG_SG_EMPTY)) == 0) && 2518 (sgp1->sg_flags & FLG_SG_EMPTY)) 2519 continue; 2520 2521 if ((sgp2->sg_flags & FLG_SG_P_VADDR) && 2522 ((sgp2->sg_flags & FLG_SG_EMPTY) == 2523 (sgp1->sg_flags & FLG_SG_EMPTY))) { 2524 if (sgp1->sg_phdr.p_vaddr == 2525 sgp2->sg_phdr.p_vaddr) { 2526 ld_eprintf(ofl, ERR_FATAL, 2527 MSG_INTL(MSG_MAP_SEGSAME), 2528 sgp1->sg_name, 2529 sgp2->sg_name); 2530 return (FALSE); 2531 } 2532 2533 if (sgp1->sg_phdr.p_vaddr > 2534 sgp2->sg_phdr.p_vaddr) 2535 continue; 2536 } 2537 2538 /* 2539 * Insert this segment before the segment on 2540 * the load_segs list. 2541 */ 2542 if (aplist_insert(&load_segs, sgp1, 2543 AL_CNT_SEGMENTS, idx2) == NULL) 2544 return (FALSE); 2545 inserted = 1; 2546 break; 2547 } 2548 2549 /* 2550 * If the segment being inspected has not been inserted 2551 * in the segment list, simply append it to the list. 2552 */ 2553 if ((inserted == 0) && (aplist_append(&load_segs, 2554 sgp1, AL_CNT_SEGMENTS) == NULL)) 2555 return (FALSE); 2556 } 2557 } 2558 2559 /* 2560 * Add the sorted loadable segments to our initial segment list. 2561 */ 2562 for (APLIST_TRAVERSE(load_segs, idx1, sgp1)) { 2563 if (aplist_append(&sort_segs, sgp1, AL_CNT_SEGMENTS) == NULL) 2564 return (FALSE); 2565 } 2566 2567 /* 2568 * Add all other segments to our list. 2569 */ 2570 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) { 2571 if ((sgp1->sg_id < SGID_TEXT) || 2572 (sgp1->sg_phdr.p_type == PT_LOAD) || 2573 (sgp1->sg_flags & FLG_SG_ORDERED)) 2574 continue; 2575 2576 if (aplist_append(&sort_segs, sgp1, AL_CNT_SEGMENTS) == NULL) 2577 return (FALSE); 2578 } 2579 2580 /* 2581 * Free the original list, and the pt_load list, and use 2582 * the new list as the segment list. 2583 */ 2584 free(ofl->ofl_segs); 2585 if (load_segs) free(load_segs); 2586 ofl->ofl_segs = sort_segs; 2587 2588 if (DBG_ENABLED) { 2589 Dbg_map_sort_title(ofl->ofl_lml, FALSE); 2590 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) { 2591 Dbg_map_sort_seg(ofl->ofl_lml, ELFOSABI_SOLARIS, 2592 ld_targ.t_m.m_mach, sgp1); 2593 } 2594 } 2595 2596 return (TRUE); 2597 } 2598 2599 /* 2600 * After all mapfiles have been processed, this routine is used to 2601 * finish any remaining mapfile related work. 2602 * 2603 * exit: 2604 * Returns TRUE on success, and FALSE on failure. 2605 */ 2606 Boolean 2607 ld_map_post_process(Ofl_desc *ofl) 2608 { 2609 Aliste idx, idx2; 2610 Is_desc *isp; 2611 Sg_desc *sgp; 2612 Ent_desc *enp; 2613 Sg_desc *first_seg = NULL; 2614 2615 2616 DBG_CALL(Dbg_map_post_title(ofl->ofl_lml)); 2617 2618 /* 2619 * Per-segment processing: 2620 * - Identify segments with explicit virtual address 2621 * - Details of input and output section order 2622 */ 2623 for (APLIST_TRAVERSE(ofl->ofl_segs, idx, sgp)) { 2624 /* 2625 * We are looking for segments. Program headers that represent 2626 * segments are required to have a non-NULL name pointer, 2627 * while that those that do not are required to have a 2628 * NULL name pointer. 2629 */ 2630 if (sgp->sg_name == NULL) 2631 continue; 2632 2633 /* Remember the first non-disabled segment */ 2634 if ((first_seg == NULL) && !(sgp->sg_flags & FLG_SG_DISABLED)) 2635 first_seg = sgp; 2636 2637 /* 2638 * If a segment has an explicit virtual address, we will 2639 * need to sort the segments. 2640 */ 2641 if (sgp->sg_flags & FLG_SG_P_VADDR) 2642 ofl->ofl_flags1 |= FLG_OF1_VADDR; 2643 2644 /* 2645 * The FLG_OF_OS_ORDER flag enables the code that does 2646 * output section ordering. Set if the segment has 2647 * a non-empty output section order list. 2648 */ 2649 if (alist_nitems(sgp->sg_os_order) > 0) 2650 ofl->ofl_flags |= FLG_OF_OS_ORDER; 2651 2652 /* 2653 * The version 1 and version 2 syntaxes for input section 2654 * ordering are different and incompatible enough that we 2655 * only allow the use of one or the other for a given segment: 2656 * 2657 * v1) The version 1 syntax has the user set the ?O flag on 2658 * the segment. If this is done, all input sections placed 2659 * via an entrance criteria that has a section name are to 2660 * be sorted, using the order of the entrance criteria 2661 * as the sort key. 2662 * 2663 * v2) The version 2 syntax has the user specify a name for 2664 * the entry criteria, and then provide a list of entry 2665 * criteria names via the IS_ORDER segment attribute. 2666 * Sections placed via the criteria listed in IS_ORDER 2667 * are sorted, and the others are not. 2668 * 2669 * Regardless of the syntax version used, the section sorting 2670 * code expects the following: 2671 * 2672 * - Segments requiring input section sorting have the 2673 * FLG_SG_IS_ORDER flag set 2674 * 2675 * - Entrance criteria referencing the segment that 2676 * participate in input section sorting have a non-zero 2677 * sort key in their ec_ordndx field. 2678 * 2679 * At this point, the following are true: 2680 * 2681 * - All entrance criteria have ec_ordndx set to 0. 2682 * - Segments that require the version 1 behavior have 2683 * the FLG_SG_IS_ORDER flag set, and the segments 2684 * sg_is_order list is empty. 2685 * - Segments that require the version 2 behavior do not 2686 * have FLG_SG_IS_ORDER set, and the sg_is_order list is 2687 * non-empty. This list contains the names of the entrance 2688 * criteria that will participate in input section sorting, 2689 * and their relative order in the list provides the 2690 * sort key to use. 2691 * 2692 * We must detect these two cases, set the FLG_SG_IS_ORDER 2693 * flag as necessary, and fill in all entrance criteria 2694 * sort keys. If any input section sorting is to be done, 2695 * we also set the FLG_OF_IS_ORDER flag on the output descriptor 2696 * to enable the code that does that work. 2697 */ 2698 2699 /* Version 1: ?O flag? */ 2700 if (sgp->sg_flags & FLG_SG_IS_ORDER) { 2701 Word index = 0; 2702 2703 ofl->ofl_flags |= FLG_OF_IS_ORDER; 2704 DBG_CALL(Dbg_map_ent_ord_title(ofl->ofl_lml, 2705 sgp->sg_name)); 2706 2707 /* 2708 * Give each user defined entrance criteria for this 2709 * segment that specifies a section name a 2710 * monotonically increasing sort key. 2711 */ 2712 for (APLIST_TRAVERSE(ofl->ofl_ents, idx2, enp)) 2713 if ((enp->ec_segment == sgp) && 2714 (enp->ec_is_name != NULL) && 2715 ((enp->ec_flags & FLG_EC_BUILTIN) == 0)) 2716 enp->ec_ordndx = ++index; 2717 continue; 2718 } 2719 2720 /* Version 2: SEGMENT IS_ORDER list? */ 2721 if (aplist_nitems(sgp->sg_is_order) > 0) { 2722 Word index = 0; 2723 2724 ofl->ofl_flags |= FLG_OF_IS_ORDER; 2725 DBG_CALL(Dbg_map_ent_ord_title(ofl->ofl_lml, 2726 sgp->sg_name)); 2727 2728 /* 2729 * Give each entrance criteria in the sg_is_order 2730 * list a monotonically increasing sort key. 2731 */ 2732 for (APLIST_TRAVERSE(sgp->sg_is_order, idx2, enp)) { 2733 enp->ec_ordndx = ++index; 2734 enp->ec_segment->sg_flags |= FLG_SG_IS_ORDER; 2735 } 2736 } 2737 } 2738 2739 /* Sort the segment descriptors if necessary */ 2740 if (((ofl->ofl_flags1 & FLG_OF1_VADDR) || 2741 (aplist_nitems(ofl->ofl_segs_order) > 0)) && 2742 !sort_seg_list(ofl)) 2743 return (FALSE); 2744 2745 /* 2746 * If the output file is a static file without an interpreter, and 2747 * if any virtual address is specified, then set the NOHDR flag for 2748 * backward compatibility. 2749 */ 2750 if (!(ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) && 2751 !(ofl->ofl_osinterp) && (ofl->ofl_flags1 & FLG_OF1_VADDR)) 2752 ofl->ofl_dtflags_1 |= DF_1_NOHDR; 2753 2754 if (ofl->ofl_flags & FLG_OF_RELOBJ) { 2755 /* 2756 * NOHDR has no effect on a relocatable file. 2757 * Make sure this flag isn't set. 2758 */ 2759 ofl->ofl_dtflags_1 &= ~DF_1_NOHDR; 2760 } else if (first_seg != NULL) { 2761 /* 2762 * DF_1_NOHDR might have been set globally by the HDR_NOALLOC 2763 * directive. If not, then we want to check the per-segment 2764 * flag for the first loadable segment and propagate it 2765 * if set. 2766 */ 2767 if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) { 2768 /* 2769 * If we sorted the segments, the first segment 2770 * may have changed. 2771 */ 2772 if ((ofl->ofl_flags1 & FLG_OF1_VADDR) || 2773 (aplist_nitems(ofl->ofl_segs_order) > 0)) { 2774 for (APLIST_TRAVERSE(ofl->ofl_segs, idx, sgp)) { 2775 if (sgp->sg_name == NULL) 2776 continue; 2777 if ((sgp->sg_flags & FLG_SG_DISABLED) == 2778 0) { 2779 first_seg = sgp; 2780 break; 2781 } 2782 } 2783 } 2784 2785 /* 2786 * If the per-segment NOHDR flag is set on our first 2787 * segment, then make it take effect. 2788 */ 2789 if (first_seg->sg_flags & FLG_SG_NOHDR) 2790 ofl->ofl_dtflags_1 |= DF_1_NOHDR; 2791 } 2792 2793 /* 2794 * For executable and shared objects, the first segment must 2795 * be loadable unless NOHDR was specified, because the ELF 2796 * header must simultaneously lie at offset 0 of the file and 2797 * be included in the first loadable segment. This isn't 2798 * possible if some other segment type starts the file 2799 */ 2800 if (!(ofl->ofl_dtflags_1 & DF_1_NOHDR) && 2801 (first_seg->sg_phdr.p_type != PT_LOAD)) { 2802 Conv_inv_buf_t inv_buf; 2803 2804 ld_eprintf(ofl, ERR_FATAL, 2805 MSG_INTL(MSG_SEG_FIRNOTLOAD), 2806 conv_phdr_type(ELFOSABI_SOLARIS, ld_targ.t_m.m_mach, 2807 first_seg->sg_phdr.p_type, 0, &inv_buf), 2808 first_seg->sg_name); 2809 return (FALSE); 2810 } 2811 } 2812 2813 /* 2814 * Mapfiles may have been used to create symbol definitions 2815 * with backing storage. Although the backing storage is 2816 * associated with an input section, the association of the 2817 * section to an output section (and segment) is initially 2818 * deferred. Now that all mapfile processing is complete, any 2819 * entrance criteria requirements have been processed, and 2820 * these backing storage sections can be associated with the 2821 * appropriate output section (and segment). 2822 */ 2823 if (ofl->ofl_maptext || ofl->ofl_mapdata) 2824 DBG_CALL(Dbg_sec_backing(ofl->ofl_lml)); 2825 2826 for (APLIST_TRAVERSE(ofl->ofl_maptext, idx, isp)) { 2827 if (ld_place_section(ofl, isp, NULL, 2828 ld_targ.t_id.id_text, NULL) == (Os_desc *)S_ERROR) 2829 return (FALSE); 2830 } 2831 2832 for (APLIST_TRAVERSE(ofl->ofl_mapdata, idx, isp)) { 2833 if (ld_place_section(ofl, isp, NULL, 2834 ld_targ.t_id.id_data, NULL) == (Os_desc *)S_ERROR) 2835 return (FALSE); 2836 } 2837 2838 return (TRUE); 2839 }