| 1 | %{ |
| 2 | /* Copyright (C) 1991 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GLD, the Gnu Linker. |
| 5 | |
| 6 | GLD is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GLD is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GLD; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | /* |
| 21 | This was written by steve chamberlain |
| 22 | sac@cygnus.com |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | typedef int bfd_vma; |
| 27 | #include <ansidecl.h> |
| 28 | #include "ldgram.h" |
| 29 | |
| 30 | int ldgram_in_defsym; |
| 31 | int ldgram_had_equals; |
| 32 | int ldgram_in_script; |
| 33 | |
| 34 | int hex_mode; |
| 35 | extern int fgetc(); |
| 36 | extern int yyparse(); |
| 37 | |
| 38 | |
| 39 | |
| 40 | char *buystring(); |
| 41 | |
| 42 | int lineno = 1; |
| 43 | int old; |
| 44 | |
| 45 | #undef YY_INPUT |
| 46 | #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size) |
| 47 | #undef YY_FATAL_ERROR |
| 48 | #define YY_FATAL_ERROR ; |
| 49 | #define MAX_INCLUDE_DEPTH 10 |
| 50 | YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; |
| 51 | unsigned int include_stack_ptr = 0; |
| 52 | |
| 53 | |
| 54 | /* FOUR STATES |
| 55 | COMMAND on command line |
| 56 | COMMENT in a C comment |
| 57 | EXPRESSION definiatelyt in an expression |
| 58 | SCRIPT definately in a script |
| 59 | SOMEWHERE either EXPRESSION or SCRIPT |
| 60 | */ |
| 61 | #define RTOKEN(x) { yylval.token = x; return x; } |
| 62 | %} |
| 63 | |
| 64 | %a 4000 |
| 65 | %o 5000 |
| 66 | FILENAMECHAR1 [_a-zA-Z\/\.\\] |
| 67 | FILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\] |
| 68 | FILENAME {FILENAMECHAR}+ |
| 69 | WHITE [ \t\n]+ |
| 70 | |
| 71 | %x COMMAND |
| 72 | %x SCRIPT |
| 73 | %x EXPRESSION |
| 74 | %x COMMENT |
| 75 | %x BOTH |
| 76 | %% |
| 77 | |
| 78 | <COMMAND>"-defsym" { return OPTION_defsym; } |
| 79 | <COMMAND>"-noinhibit_exec" { return OPTION_noinhibit_exec; } |
| 80 | <COMMAND>"-noinhibit-exec" { return OPTION_noinhibit_exec; } |
| 81 | <COMMAND>"-sort_common" { return OPTION_sort_common;} |
| 82 | <COMMAND>"-sort-common" { return OPTION_sort_common;} |
| 83 | <COMMAND>"-format" { return OPTION_format; } |
| 84 | <COMMAND>"-n" { return OPTION_n; } |
| 85 | <COMMAND>"-N" { return OPTION_N; } |
| 86 | <COMMAND>"-r" { return OPTION_r; } |
| 87 | <COMMAND>"-relax" { return OPTION_relax; } |
| 88 | <COMMAND>"-i" { return OPTION_r; } |
| 89 | <COMMAND>"-Ur" { return OPTION_Ur; } |
| 90 | <COMMAND>"-o" { return OPTION_o; } |
| 91 | <COMMAND>"-g" { return OPTION_g; } |
| 92 | <COMMAND>"-e" { return OPTION_e; } |
| 93 | <COMMAND>"-b" { return OPTION_b; } |
| 94 | <COMMAND>"-dc" { return OPTION_dc; } |
| 95 | <COMMAND>"-dp" { return OPTION_dp; } |
| 96 | <COMMAND>"-d" { return OPTION_d; } |
| 97 | <COMMAND>"-v" { return OPTION_v; } |
| 98 | <COMMAND>"-V" { return OPTION_V; } |
| 99 | <COMMAND>"-M" { return OPTION_M; } |
| 100 | <COMMAND>"-Map" { return OPTION_Map;} |
| 101 | <COMMAND>"-t" { return OPTION_t; } |
| 102 | <COMMAND>"-X" { return OPTION_X; } |
| 103 | <COMMAND>"-x" { return OPTION_x; } |
| 104 | <COMMAND>"-c" { return OPTION_c; } |
| 105 | <COMMAND>"-R" { return OPTION_R; } |
| 106 | <COMMAND>"-u" { return OPTION_u; } |
| 107 | <COMMAND>"-s" { return OPTION_s; } |
| 108 | <COMMAND>"-S" { return OPTION_S; } |
| 109 | <COMMAND>"-Bstat" { return OPTION_Bstatic; } |
| 110 | <COMMAND>"-B"{FILENAME} { /* Ignored */ } |
| 111 | <COMMAND>"-l"{FILENAME} { |
| 112 | yylval.name = buystring(yytext+2); |
| 113 | return OPTION_l; |
| 114 | } |
| 115 | |
| 116 | <COMMAND>"-L"{FILENAME} { |
| 117 | yylval.name = buystring(yytext+2); |
| 118 | return OPTION_L; |
| 119 | } |
| 120 | <COMMAND>"-Ttext" { |
| 121 | yylval.name = ".text"; |
| 122 | return OPTION_Texp; |
| 123 | } |
| 124 | <COMMAND>"-Tdata" { |
| 125 | yylval.name = ".data"; |
| 126 | return OPTION_Texp; |
| 127 | } |
| 128 | <COMMAND>"-Tbss" { |
| 129 | yylval.name = ".bss"; |
| 130 | return OPTION_Texp; |
| 131 | } |
| 132 | <COMMAND>"-O"{FILENAME} { |
| 133 | yylval.name = buystring(yytext+2); |
| 134 | return OPTION_Texp; |
| 135 | } |
| 136 | |
| 137 | <COMMAND>"-T"{FILENAME} { |
| 138 | yylval.name = buystring(yytext+2); |
| 139 | return OPTION_Tfile; |
| 140 | } |
| 141 | <COMMAND>"-T" { |
| 142 | return OPTION_T; |
| 143 | } |
| 144 | |
| 145 | <COMMAND>"-F"{FILENAME} { |
| 146 | return OPTION_F; |
| 147 | } |
| 148 | <COMMAND>"-F" { |
| 149 | return OPTION_F; |
| 150 | } |
| 151 | |
| 152 | <COMMAND>"-A"{FILENAME} { |
| 153 | yylval.name = buystring(yytext+2); |
| 154 | return OPTION_Aarch; |
| 155 | } |
| 156 | <BOTH,EXPRESSION>"0x"?([0-9A-Fa-f])+(M|K|m|k)? { |
| 157 | yylval.integer = strtol(yytext,0,hex_mode); |
| 158 | if (yytext[yyleng-1]=='M' |
| 159 | || yytext[yyleng-1] == 'm') { |
| 160 | yylval.integer *= 1024*1024; |
| 161 | } |
| 162 | if (yytext[yyleng-1]=='K' |
| 163 | || yytext[yyleng-1]=='k') { |
| 164 | yylval.integer *= 1024; |
| 165 | } |
| 166 | return INT; |
| 167 | } |
| 168 | <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');} |
| 169 | <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');} |
| 170 | <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);} |
| 171 | <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);} |
| 172 | <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);} |
| 173 | <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);} |
| 174 | <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);} |
| 175 | <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);} |
| 176 | <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);} |
| 177 | <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);} |
| 178 | <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);} |
| 179 | <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);} |
| 180 | <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);} |
| 181 | <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);} |
| 182 | <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);} |
| 183 | <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);} |
| 184 | <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);} |
| 185 | <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);} |
| 186 | <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');} |
| 187 | <BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');} |
| 188 | <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');} |
| 189 | <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');} |
| 190 | <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');} |
| 191 | <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');} |
| 192 | <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');} |
| 193 | <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');} |
| 194 | <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');} |
| 195 | <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');} |
| 196 | <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');} |
| 197 | <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');} |
| 198 | <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');} |
| 199 | <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');} |
| 200 | <BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');} |
| 201 | <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; } |
| 202 | <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); } |
| 203 | <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');} |
| 204 | <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');} |
| 205 | <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');} |
| 206 | <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');} |
| 207 | <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); } |
| 208 | <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');} |
| 209 | <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');} |
| 210 | <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');} |
| 211 | <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);} |
| 212 | <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);} |
| 213 | <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);} |
| 214 | <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);} |
| 215 | <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);} |
| 216 | <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);} |
| 217 | <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);} |
| 218 | <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);} |
| 219 | <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);} |
| 220 | <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);} |
| 221 | <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);} |
| 222 | <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);} |
| 223 | <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);} |
| 224 | <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);} |
| 225 | <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);} |
| 226 | <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);} |
| 227 | <BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);} |
| 228 | <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);} |
| 229 | <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);} |
| 230 | <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);} |
| 231 | <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);} |
| 232 | <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);} |
| 233 | <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);} |
| 234 | <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);} |
| 235 | <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);} |
| 236 | <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);} |
| 237 | <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);} |
| 238 | <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);} |
| 239 | <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);} |
| 240 | <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);} |
| 241 | <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);} |
| 242 | <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);} |
| 243 | <BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);} |
| 244 | <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);} |
| 245 | <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);} |
| 246 | <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);} |
| 247 | <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);} |
| 248 | <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);} |
| 249 | <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);} |
| 250 | <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);} |
| 251 | <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);} |
| 252 | |
| 253 | <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* { |
| 254 | yylval.name = buystring(yytext); |
| 255 | return NAME; |
| 256 | } |
| 257 | <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext); |
| 258 | return NAME; |
| 259 | } |
| 260 | |
| 261 | <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" { |
| 262 | /* No matter the state, quotes |
| 263 | give what's inside */ |
| 264 | yylval.name = buystring(yytext+1); |
| 265 | yylval.name[yyleng-2] = 0; |
| 266 | return NAME; |
| 267 | } |
| 268 | <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;} |
| 269 | <COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t] |
| 270 | |
| 271 | "/*" { old = INITIAL; BEGIN(COMMENT); } |
| 272 | <COMMAND>"/*" { old = COMMAND; BEGIN(COMMENT); } |
| 273 | <BOTH>"/*" { old =BOTH; BEGIN(COMMENT); } |
| 274 | <SCRIPT>"/*" { old = EXPRESSION; BEGIN(COMMENT); } |
| 275 | <EXPRESSION>"/*" { old = SCRIPT; BEGIN(COMMENT); } |
| 276 | "/*" { old = INITIAL; BEGIN(COMMENT); } |
| 277 | <COMMENT>[^*\\n]* |
| 278 | <COMMENT>"*"+[^*/\\n]* |
| 279 | <COMMENT>\\n { ++lineno;} |
| 280 | <COMMENT>"*"+"/" { BEGIN(old); } |
| 281 | |
| 282 | <<EOF>> { |
| 283 | include_stack_ptr--; |
| 284 | |
| 285 | if (include_stack_ptr == 0) |
| 286 | { |
| 287 | yyterminate(); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | yy_switch_to_buffer(include_stack[include_stack_ptr]); |
| 292 | } |
| 293 | BEGIN(COMMAND); |
| 294 | return END; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | %% |
| 305 | |
| 306 | void |
| 307 | DEFUN(lex_push_file,(file), |
| 308 | FILE *file) |
| 309 | { |
| 310 | if (include_stack_ptr >= MAX_INCLUDE_DEPTH) |
| 311 | { |
| 312 | einfo("%F:includes nested too deeply"); |
| 313 | } |
| 314 | include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER; |
| 315 | yyin = file; |
| 316 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
| 317 | |
| 318 | BEGIN(SCRIPT); |
| 319 | } |
| 320 | |
| 321 | YY_BUFFER_STATE |
| 322 | DEFUN(yy_create_string_buffer,(string, size), |
| 323 | CONST char *string AND |
| 324 | int size ) |
| 325 | { |
| 326 | YY_BUFFER_STATE b; |
| 327 | |
| 328 | b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) ); |
| 329 | b->yy_input_file = 0; |
| 330 | |
| 331 | if ( ! b ) |
| 332 | YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); |
| 333 | |
| 334 | b->yy_buf_size = size; |
| 335 | |
| 336 | /* yy_ch_buf has to be 2 characters longer than the size given because |
| 337 | * we need to put in 2 end-of-buffer characters. |
| 338 | */ |
| 339 | b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) ); |
| 340 | |
| 341 | if ( ! b->yy_ch_buf ) |
| 342 | YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); |
| 343 | |
| 344 | |
| 345 | b->yy_ch_buf[0] = '\n'; |
| 346 | strcpy(b->yy_ch_buf+1, string); |
| 347 | b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR; |
| 348 | b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR; |
| 349 | b->yy_n_chars = size+1; |
| 350 | b->yy_buf_pos = &b->yy_ch_buf[1]; |
| 351 | |
| 352 | b->yy_eof_status = EOF_NOT_SEEN; |
| 353 | |
| 354 | return ( b ); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | |
| 359 | void |
| 360 | DEFUN(lex_redirect,( string), |
| 361 | CONST char *string) |
| 362 | { |
| 363 | YY_BUFFER_STATE tmp; |
| 364 | |
| 365 | int len = strlen(string); |
| 366 | yy_init = 0 ; |
| 367 | if (include_stack_ptr >= MAX_INCLUDE_DEPTH) |
| 368 | { |
| 369 | einfo("%F: macros nested too deeply"); |
| 370 | } |
| 371 | include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER; |
| 372 | |
| 373 | tmp = yy_create_string_buffer(string, len); |
| 374 | |
| 375 | yy_switch_to_buffer(tmp); |
| 376 | BEGIN(COMMAND); |
| 377 | yyout = stdout; |
| 378 | } |
| 379 | |
| 380 | int state_stack[20]; |
| 381 | int *state_stack_p = state_stack; |
| 382 | |
| 383 | void |
| 384 | DEFUN_VOID(ldlex_script) |
| 385 | { |
| 386 | *(state_stack_p)++ = yy_start; |
| 387 | |
| 388 | BEGIN(SCRIPT); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | void |
| 393 | DEFUN_VOID(ldlex_expression) |
| 394 | { |
| 395 | *(state_stack_p)++ = yy_start; |
| 396 | BEGIN(EXPRESSION); |
| 397 | |
| 398 | } |
| 399 | void |
| 400 | DEFUN_VOID(ldlex_both) |
| 401 | { |
| 402 | *(state_stack_p)++ = yy_start; |
| 403 | BEGIN(BOTH); |
| 404 | } |
| 405 | void |
| 406 | DEFUN_VOID(ldlex_command) |
| 407 | { |
| 408 | *(state_stack_p)++ = yy_start; |
| 409 | BEGIN(COMMAND); |
| 410 | } |
| 411 | |
| 412 | void |
| 413 | DEFUN_VOID(ldlex_popstate) |
| 414 | { |
| 415 | yy_start = *(--state_stack_p); |
| 416 | } |
| 417 | |
| 418 | yy_input(buf, result, max_size) |
| 419 | char *buf; |
| 420 | int *result; |
| 421 | int max_size; |
| 422 | { |
| 423 | *result = 0; |
| 424 | if (yy_current_buffer->yy_input_file) |
| 425 | { |
| 426 | if (yyin) |
| 427 | if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) |
| 428 | YY_FATAL_ERROR( "read() in flex scanner failed" ); |
| 429 | } |
| 430 | } |
| 431 | |