| 1 | /* Linker command language support. |
| 2 | Copyright 1991, 1992 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 1, 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 | #include "bfd.h" |
| 21 | #include "sysdep.h" |
| 22 | |
| 23 | #include "ld.h" |
| 24 | #include "ldmain.h" |
| 25 | #include "ldsym.h" |
| 26 | #include "ldgram.h" |
| 27 | #include "ldwarn.h" |
| 28 | #include "ldlang.h" |
| 29 | #include "ldexp.h" |
| 30 | #include "ldemul.h" |
| 31 | #include "ldlex.h" |
| 32 | #include "ldmisc.h" |
| 33 | #include "ldindr.h" |
| 34 | #include "ldctor.h" |
| 35 | /* FORWARDS */ |
| 36 | static void print_statements PARAMS ((void)); |
| 37 | static void print_statement PARAMS ((lang_statement_union_type *, |
| 38 | lang_output_section_statement_type *)); |
| 39 | |
| 40 | /* LOCALS */ |
| 41 | static struct obstack stat_obstack; |
| 42 | |
| 43 | #define obstack_chunk_alloc ldmalloc |
| 44 | #define obstack_chunk_free free |
| 45 | static CONST char *startup_file; |
| 46 | static lang_statement_list_type input_file_chain; |
| 47 | |
| 48 | /* Points to the last statement in the .data section, so we can add |
| 49 | stuff to the data section without pain */ |
| 50 | static lang_statement_list_type end_of_data_section_statement_list; |
| 51 | |
| 52 | /* List of statements needed to handle constructors */ |
| 53 | extern lang_statement_list_type constructor_list; |
| 54 | |
| 55 | static boolean placed_commons = false; |
| 56 | static lang_output_section_statement_type *default_common_section; |
| 57 | static boolean map_option_f; |
| 58 | static bfd_vma print_dot; |
| 59 | static lang_input_statement_type *first_file; |
| 60 | static lang_statement_list_type lang_output_section_statement; |
| 61 | static CONST char *current_target; |
| 62 | static CONST char *output_target; |
| 63 | static size_t longest_section_name = 8; |
| 64 | static section_userdata_type common_section_userdata; |
| 65 | static lang_statement_list_type statement_list; |
| 66 | |
| 67 | /* EXPORTS */ |
| 68 | boolean relaxing; |
| 69 | lang_output_section_statement_type *abs_output_section; |
| 70 | lang_statement_list_type *stat_ptr = &statement_list; |
| 71 | lang_input_statement_type *script_file = 0; |
| 72 | boolean option_longmap = false; |
| 73 | lang_statement_list_type file_chain = |
| 74 | {0}; |
| 75 | CONST char *entry_symbol = 0; |
| 76 | bfd_size_type largest_section = 0; |
| 77 | boolean lang_has_input_file = false; |
| 78 | lang_output_section_statement_type *create_object_symbols = 0; |
| 79 | boolean had_output_filename = false; |
| 80 | boolean lang_float_flag = false; |
| 81 | |
| 82 | /* IMPORTS */ |
| 83 | extern char *default_target; |
| 84 | |
| 85 | extern unsigned int undefined_global_sym_count; |
| 86 | extern char *current_file; |
| 87 | extern bfd *output_bfd; |
| 88 | extern enum bfd_architecture ldfile_output_architecture; |
| 89 | extern unsigned long ldfile_output_machine; |
| 90 | extern char *ldfile_output_machine_name; |
| 91 | extern ldsym_type *symbol_head; |
| 92 | extern unsigned int commons_pending; |
| 93 | extern args_type command_line; |
| 94 | extern ld_config_type config; |
| 95 | extern boolean had_script; |
| 96 | extern boolean write_map; |
| 97 | |
| 98 | |
| 99 | etree_type *base; /* Relocation base - or null */ |
| 100 | |
| 101 | |
| 102 | #ifdef __STDC__ |
| 103 | #define cat(a,b) a##b |
| 104 | #else |
| 105 | #define cat(a,b) a/**/b |
| 106 | #endif |
| 107 | |
| 108 | #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y) |
| 109 | |
| 110 | #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma) |
| 111 | |
| 112 | #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section)) |
| 113 | |
| 114 | void lang_add_data PARAMS ((int type, union etree_union * exp)); |
| 115 | |
| 116 | PTR |
| 117 | DEFUN (stat_alloc, (size), |
| 118 | size_t size) |
| 119 | { |
| 120 | return obstack_alloc (&stat_obstack, size); |
| 121 | } |
| 122 | static void |
| 123 | DEFUN (print_size, (value), |
| 124 | size_t value) |
| 125 | { |
| 126 | fprintf (config.map_file, "%5x", (unsigned) value); |
| 127 | } |
| 128 | static void |
| 129 | DEFUN (print_alignment, (value), |
| 130 | unsigned int value) |
| 131 | { |
| 132 | fprintf (config.map_file, "2**%1u", value); |
| 133 | } |
| 134 | static void |
| 135 | DEFUN (print_fill, (value), |
| 136 | fill_type value) |
| 137 | { |
| 138 | fprintf (config.map_file, "%04x", (unsigned) value); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static void |
| 143 | DEFUN (print_section, (name), |
| 144 | CONST char *CONST name) |
| 145 | { |
| 146 | fprintf (config.map_file, "%*s", -longest_section_name, name); |
| 147 | } |
| 148 | |
| 149 | /*---------------------------------------------------------------------- |
| 150 | lang_for_each_statement walks the parse tree and calls the provided |
| 151 | function for each node |
| 152 | */ |
| 153 | |
| 154 | static void |
| 155 | DEFUN (lang_for_each_statement_worker, (func, s), |
| 156 | void (*func) ()AND |
| 157 | lang_statement_union_type * s) |
| 158 | { |
| 159 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
| 160 | { |
| 161 | func (s); |
| 162 | |
| 163 | switch (s->header.type) |
| 164 | { |
| 165 | case lang_constructors_statement_enum: |
| 166 | lang_for_each_statement_worker (func, constructor_list.head); |
| 167 | break; |
| 168 | case lang_output_section_statement_enum: |
| 169 | lang_for_each_statement_worker |
| 170 | (func, |
| 171 | s->output_section_statement.children.head); |
| 172 | break; |
| 173 | case lang_wild_statement_enum: |
| 174 | lang_for_each_statement_worker |
| 175 | (func, |
| 176 | s->wild_statement.children.head); |
| 177 | break; |
| 178 | case lang_data_statement_enum: |
| 179 | case lang_object_symbols_statement_enum: |
| 180 | case lang_output_statement_enum: |
| 181 | case lang_target_statement_enum: |
| 182 | case lang_input_section_enum: |
| 183 | case lang_input_statement_enum: |
| 184 | case lang_assignment_statement_enum: |
| 185 | case lang_padding_statement_enum: |
| 186 | case lang_address_statement_enum: |
| 187 | break; |
| 188 | default: |
| 189 | FAIL (); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | DEFUN (lang_for_each_statement, (func), |
| 197 | void (*func) ()) |
| 198 | { |
| 199 | lang_for_each_statement_worker (func, |
| 200 | statement_list.head); |
| 201 | } |
| 202 | |
| 203 | /*----------------------------------------------------------------------*/ |
| 204 | void |
| 205 | DEFUN (lang_list_init, (list), |
| 206 | lang_statement_list_type * list) |
| 207 | { |
| 208 | list->head = (lang_statement_union_type *) NULL; |
| 209 | list->tail = &list->head; |
| 210 | } |
| 211 | |
| 212 | /*---------------------------------------------------------------------- |
| 213 | |
| 214 | build a new statement node for the parse tree |
| 215 | |
| 216 | */ |
| 217 | |
| 218 | static |
| 219 | lang_statement_union_type * |
| 220 | DEFUN (new_statement, (type, size, list), |
| 221 | enum statement_enum type AND |
| 222 | bfd_size_type size AND |
| 223 | lang_statement_list_type * list) |
| 224 | { |
| 225 | lang_statement_union_type *new = (lang_statement_union_type *) |
| 226 | stat_alloc (size); |
| 227 | |
| 228 | new->header.type = type; |
| 229 | new->header.next = (lang_statement_union_type *) NULL; |
| 230 | lang_statement_append (list, new, &new->header.next); |
| 231 | return new; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | Build a new input file node for the language. There are several ways |
| 236 | in which we treat an input file, eg, we only look at symbols, or |
| 237 | prefix it with a -l etc. |
| 238 | |
| 239 | We can be supplied with requests for input files more than once; |
| 240 | they may, for example be split over serveral lines like foo.o(.text) |
| 241 | foo.o(.data) etc, so when asked for a file we check that we havn't |
| 242 | got it already so we don't duplicate the bfd. |
| 243 | |
| 244 | */ |
| 245 | static lang_input_statement_type * |
| 246 | DEFUN (new_afile, (name, file_type, target), |
| 247 | CONST char *CONST name AND |
| 248 | CONST lang_input_file_enum_type file_type AND |
| 249 | CONST char *CONST target) |
| 250 | { |
| 251 | |
| 252 | lang_input_statement_type *p = new_stat (lang_input_statement, |
| 253 | stat_ptr); |
| 254 | |
| 255 | lang_has_input_file = true; |
| 256 | p->target = target; |
| 257 | p->complained = false; |
| 258 | switch (file_type) |
| 259 | { |
| 260 | case lang_input_file_is_symbols_only_enum: |
| 261 | p->filename = name; |
| 262 | p->is_archive = false; |
| 263 | p->real = true; |
| 264 | p->local_sym_name = name; |
| 265 | p->just_syms_flag = true; |
| 266 | p->search_dirs_flag = false; |
| 267 | break; |
| 268 | case lang_input_file_is_fake_enum: |
| 269 | p->filename = name; |
| 270 | p->is_archive = false; |
| 271 | p->real = false; |
| 272 | p->local_sym_name = name; |
| 273 | p->just_syms_flag = false; |
| 274 | p->search_dirs_flag = false; |
| 275 | break; |
| 276 | case lang_input_file_is_l_enum: |
| 277 | p->is_archive = true; |
| 278 | p->filename = name; |
| 279 | p->real = true; |
| 280 | p->local_sym_name = concat ("-l", name, ""); |
| 281 | p->just_syms_flag = false; |
| 282 | p->search_dirs_flag = true; |
| 283 | break; |
| 284 | case lang_input_file_is_search_file_enum: |
| 285 | case lang_input_file_is_marker_enum: |
| 286 | p->filename = name; |
| 287 | p->is_archive = false; |
| 288 | p->real = true; |
| 289 | p->local_sym_name = name; |
| 290 | p->just_syms_flag = false; |
| 291 | p->search_dirs_flag = true; |
| 292 | break; |
| 293 | case lang_input_file_is_file_enum: |
| 294 | p->filename = name; |
| 295 | p->is_archive = false; |
| 296 | p->real = true; |
| 297 | p->local_sym_name = name; |
| 298 | p->just_syms_flag = false; |
| 299 | p->search_dirs_flag = false; |
| 300 | break; |
| 301 | default: |
| 302 | FAIL (); |
| 303 | } |
| 304 | p->asymbols = (asymbol **) NULL; |
| 305 | p->superfile = (lang_input_statement_type *) NULL; |
| 306 | p->next_real_file = (lang_statement_union_type *) NULL; |
| 307 | p->next = (lang_statement_union_type *) NULL; |
| 308 | p->symbol_count = 0; |
| 309 | p->common_output_section = (asection *) NULL; |
| 310 | lang_statement_append (&input_file_chain, |
| 311 | (lang_statement_union_type *) p, |
| 312 | &p->next_real_file); |
| 313 | return p; |
| 314 | } |
| 315 | |
| 316 | lang_input_statement_type * |
| 317 | DEFUN (lang_add_input_file, (name, file_type, target), |
| 318 | CONST char *name AND |
| 319 | lang_input_file_enum_type file_type AND |
| 320 | CONST char *target) |
| 321 | { |
| 322 | /* Look it up or build a new one */ |
| 323 | lang_has_input_file = true; |
| 324 | #if 0 |
| 325 | lang_input_statement_type *p; |
| 326 | |
| 327 | for (p = (lang_input_statement_type *) input_file_chain.head; |
| 328 | p != (lang_input_statement_type *) NULL; |
| 329 | p = (lang_input_statement_type *) (p->next_real_file)) |
| 330 | { |
| 331 | /* Sometimes we have incomplete entries in here */ |
| 332 | if (p->filename != (char *) NULL) |
| 333 | { |
| 334 | if (strcmp (name, p->filename) == 0) |
| 335 | return p; |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | #endif |
| 340 | return new_afile (name, file_type, target); |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | DEFUN (lang_add_keepsyms_file, (filename), |
| 345 | CONST char *filename) |
| 346 | { |
| 347 | extern strip_symbols_type strip_symbols; |
| 348 | if (keepsyms_file != 0) |
| 349 | info ("%X%P error: duplicated keep-symbols-file value\n"); |
| 350 | keepsyms_file = filename; |
| 351 | if (strip_symbols != STRIP_NONE) |
| 352 | info ("%P `-keep-only-symbols-file' overrides `-s' and `-S'\n"); |
| 353 | strip_symbols = STRIP_SOME; |
| 354 | } |
| 355 | |
| 356 | /* Build enough state so that the parser can build its tree */ |
| 357 | void |
| 358 | DEFUN_VOID (lang_init) |
| 359 | { |
| 360 | obstack_begin (&stat_obstack, 1000); |
| 361 | |
| 362 | stat_ptr = &statement_list; |
| 363 | |
| 364 | lang_list_init (stat_ptr); |
| 365 | |
| 366 | lang_list_init (&input_file_chain); |
| 367 | lang_list_init (&lang_output_section_statement); |
| 368 | lang_list_init (&file_chain); |
| 369 | first_file = lang_add_input_file ((char *) NULL, |
| 370 | lang_input_file_is_marker_enum, |
| 371 | (char *) NULL); |
| 372 | abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); |
| 373 | |
| 374 | abs_output_section->bfd_section = &bfd_abs_section; |
| 375 | |
| 376 | } |
| 377 | |
| 378 | /*---------------------------------------------------------------------- |
| 379 | A region is an area of memory declared with the |
| 380 | MEMORY { name:org=exp, len=exp ... } |
| 381 | syntax. |
| 382 | |
| 383 | We maintain a list of all the regions here |
| 384 | |
| 385 | If no regions are specified in the script, then the default is used |
| 386 | which is created when looked up to be the entire data space |
| 387 | */ |
| 388 | |
| 389 | static lang_memory_region_type *lang_memory_region_list; |
| 390 | static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list; |
| 391 | |
| 392 | lang_memory_region_type * |
| 393 | DEFUN (lang_memory_region_lookup, (name), |
| 394 | CONST char *CONST name) |
| 395 | { |
| 396 | |
| 397 | lang_memory_region_type *p = lang_memory_region_list; |
| 398 | |
| 399 | for (p = lang_memory_region_list; |
| 400 | p != (lang_memory_region_type *) NULL; |
| 401 | p = p->next) |
| 402 | { |
| 403 | if (strcmp (p->name, name) == 0) |
| 404 | { |
| 405 | return p; |
| 406 | } |
| 407 | } |
| 408 | if (strcmp (name, "*default*") == 0) |
| 409 | { |
| 410 | /* This is the default region, dig out first one on the list */ |
| 411 | if (lang_memory_region_list != (lang_memory_region_type *) NULL) |
| 412 | { |
| 413 | return lang_memory_region_list; |
| 414 | } |
| 415 | } |
| 416 | { |
| 417 | lang_memory_region_type *new = |
| 418 | (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type))); |
| 419 | |
| 420 | new->name = buystring (name); |
| 421 | new->next = (lang_memory_region_type *) NULL; |
| 422 | |
| 423 | *lang_memory_region_list_tail = new; |
| 424 | lang_memory_region_list_tail = &new->next; |
| 425 | new->origin = 0; |
| 426 | new->length = ~0; |
| 427 | new->current = 0; |
| 428 | new->had_full_message = false; |
| 429 | |
| 430 | return new; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | lang_output_section_statement_type * |
| 436 | DEFUN (lang_output_section_find, (name), |
| 437 | CONST char *CONST name) |
| 438 | { |
| 439 | lang_statement_union_type *u; |
| 440 | lang_output_section_statement_type *lookup; |
| 441 | |
| 442 | for (u = lang_output_section_statement.head; |
| 443 | u != (lang_statement_union_type *) NULL; |
| 444 | u = lookup->next) |
| 445 | { |
| 446 | lookup = &u->output_section_statement; |
| 447 | if (strcmp (name, lookup->name) == 0) |
| 448 | { |
| 449 | return lookup; |
| 450 | } |
| 451 | } |
| 452 | return (lang_output_section_statement_type *) NULL; |
| 453 | } |
| 454 | |
| 455 | lang_output_section_statement_type * |
| 456 | DEFUN (lang_output_section_statement_lookup, (name), |
| 457 | CONST char *CONST name) |
| 458 | { |
| 459 | lang_output_section_statement_type *lookup; |
| 460 | |
| 461 | lookup = lang_output_section_find (name); |
| 462 | if (lookup == (lang_output_section_statement_type *) NULL) |
| 463 | { |
| 464 | |
| 465 | lookup = (lang_output_section_statement_type *) |
| 466 | new_stat (lang_output_section_statement, stat_ptr); |
| 467 | lookup->region = (lang_memory_region_type *) NULL; |
| 468 | lookup->fill = 0; |
| 469 | lookup->block_value = 1; |
| 470 | lookup->name = name; |
| 471 | |
| 472 | lookup->next = (lang_statement_union_type *) NULL; |
| 473 | lookup->bfd_section = (asection *) NULL; |
| 474 | lookup->processed = false; |
| 475 | lookup->loadable = 1; |
| 476 | lookup->addr_tree = (etree_type *) NULL; |
| 477 | lang_list_init (&lookup->children); |
| 478 | |
| 479 | lang_statement_append (&lang_output_section_statement, |
| 480 | (lang_statement_union_type *) lookup, |
| 481 | &lookup->next); |
| 482 | } |
| 483 | return lookup; |
| 484 | } |
| 485 | |
| 486 | /*ARGSUSED*/ |
| 487 | static void |
| 488 | DEFUN (print_flags, (ignore_flags), |
| 489 | int *ignore_flags) |
| 490 | { |
| 491 | fprintf (config.map_file, "("); |
| 492 | #if 0 |
| 493 | if (flags->flag_read) |
| 494 | fprintf (outfile, "R"); |
| 495 | if (flags->flag_write) |
| 496 | fprintf (outfile, "W"); |
| 497 | if (flags->flag_executable) |
| 498 | fprintf (outfile, "X"); |
| 499 | if (flags->flag_loadable) |
| 500 | fprintf (outfile, "L"); |
| 501 | #endif |
| 502 | fprintf (config.map_file, ")"); |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | DEFUN_VOID (lang_map) |
| 507 | { |
| 508 | lang_memory_region_type *m; |
| 509 | |
| 510 | fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n"); |
| 511 | #ifdef HOST_64_BIT |
| 512 | fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n"); |
| 513 | #else |
| 514 | fprintf (config.map_file, |
| 515 | "name\t\torigin length r_size c_size is attributes\n"); |
| 516 | |
| 517 | #endif |
| 518 | for (m = lang_memory_region_list; |
| 519 | m != (lang_memory_region_type *) NULL; |
| 520 | m = m->next) |
| 521 | { |
| 522 | fprintf (config.map_file, "%-16s", m->name); |
| 523 | print_address (m->origin); |
| 524 | print_space (); |
| 525 | print_address (m->length); |
| 526 | print_space (); |
| 527 | print_address (m->old_length); |
| 528 | print_space(); |
| 529 | print_address (m->current - m->origin); |
| 530 | print_space(); |
| 531 | if (m->old_length) |
| 532 | fprintf(config.map_file," %2d%% ", ( m->current - m->origin) * 100 / m->old_length); |
| 533 | print_flags (&m->flags); |
| 534 | fprintf (config.map_file, "\n"); |
| 535 | } |
| 536 | fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n"); |
| 537 | fprintf (config.map_file, "output input virtual\n"); |
| 538 | fprintf (config.map_file, "section section address tsize\n\n"); |
| 539 | |
| 540 | print_statements (); |
| 541 | |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * |
| 546 | */ |
| 547 | static void |
| 548 | DEFUN (init_os, (s), |
| 549 | lang_output_section_statement_type * s) |
| 550 | { |
| 551 | /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/ |
| 552 | section_userdata_type *new = |
| 553 | (section_userdata_type *) |
| 554 | stat_alloc ((bfd_size_type) (sizeof (section_userdata_type))); |
| 555 | |
| 556 | s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); |
| 557 | if (s->bfd_section == (asection *) NULL) |
| 558 | s->bfd_section = bfd_make_section (output_bfd, s->name); |
| 559 | if (s->bfd_section == (asection *) NULL) |
| 560 | { |
| 561 | einfo ("%P%F output format %s cannot represent section called %s\n", |
| 562 | output_bfd->xvec->name, s->name); |
| 563 | } |
| 564 | s->bfd_section->output_section = s->bfd_section; |
| 565 | /* s->bfd_section->flags = s->flags;*/ |
| 566 | |
| 567 | /* We initialize an output sections output offset to minus its own */ |
| 568 | /* vma to allow us to output a section through itself */ |
| 569 | s->bfd_section->output_offset = 0; |
| 570 | get_userdata (s->bfd_section) = (PTR) new; |
| 571 | |
| 572 | } |
| 573 | |
| 574 | /*********************************************************************** |
| 575 | The wild routines. |
| 576 | |
| 577 | These expand statements like *(.text) and foo.o to a list of |
| 578 | explicit actions, like foo.o(.text), bar.o(.text) and |
| 579 | foo.o(.text,.data) . |
| 580 | |
| 581 | The toplevel routine, wild, takes a statement, section, file and |
| 582 | target. If either the section or file is null it is taken to be the |
| 583 | wildcard. Seperate lang_input_section statements are created for |
| 584 | each part of the expanstion, and placed after the statement provided. |
| 585 | |
| 586 | */ |
| 587 | |
| 588 | static void |
| 589 | DEFUN (wild_doit, (ptr, section, output, file), |
| 590 | lang_statement_list_type * ptr AND |
| 591 | asection * section AND |
| 592 | lang_output_section_statement_type * output AND |
| 593 | lang_input_statement_type * file) |
| 594 | { |
| 595 | if (output->bfd_section == (asection *) NULL) |
| 596 | { |
| 597 | init_os (output); |
| 598 | /* Initialize the vma and size to the existing section. This will |
| 599 | be overriden in lang_size_sections unless SEC_NEVER_LOAD gets |
| 600 | set. */ |
| 601 | if (section != (asection *) NULL) |
| 602 | { |
| 603 | bfd_set_section_vma (0, output->bfd_section, |
| 604 | bfd_section_vma (0, section)); |
| 605 | output->bfd_section->_raw_size = section->_raw_size; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (section != (asection *) NULL |
| 610 | && section->output_section == (asection *) NULL) |
| 611 | { |
| 612 | /* Add a section reference to the list */ |
| 613 | lang_input_section_type *new = new_stat (lang_input_section, ptr); |
| 614 | |
| 615 | new->section = section; |
| 616 | new->ifile = file; |
| 617 | section->output_section = output->bfd_section; |
| 618 | |
| 619 | /* Be selective about what the output section inherits from the |
| 620 | input section */ |
| 621 | |
| 622 | section->output_section->flags |= section->flags & ~SEC_NEVER_LOAD; |
| 623 | |
| 624 | if (!output->loadable) |
| 625 | { |
| 626 | /* Turn off load flag */ |
| 627 | output->bfd_section->flags &= ~SEC_LOAD; |
| 628 | output->bfd_section->flags |= SEC_NEVER_LOAD; |
| 629 | } |
| 630 | if (section->alignment_power > output->bfd_section->alignment_power) |
| 631 | { |
| 632 | output->bfd_section->alignment_power = section->alignment_power; |
| 633 | } |
| 634 | /* If supplied an aligmnet, then force it */ |
| 635 | if (output->section_alignment != -1) |
| 636 | { |
| 637 | output->bfd_section->alignment_power = output->section_alignment; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | static asection * |
| 643 | DEFUN (our_bfd_get_section_by_name, (abfd, section), |
| 644 | bfd * abfd AND |
| 645 | CONST char *section) |
| 646 | { |
| 647 | return bfd_get_section_by_name (abfd, section); |
| 648 | } |
| 649 | |
| 650 | static void |
| 651 | DEFUN (wild_section, (ptr, section, file, output), |
| 652 | lang_wild_statement_type * ptr AND |
| 653 | CONST char *section AND |
| 654 | lang_input_statement_type * file AND |
| 655 | lang_output_section_statement_type * output) |
| 656 | { |
| 657 | asection *s; |
| 658 | |
| 659 | if (file->just_syms_flag == false) |
| 660 | { |
| 661 | if (section == (char *) NULL) |
| 662 | { |
| 663 | /* Do the creation to all sections in the file */ |
| 664 | for (s = file->the_bfd->sections; s != (asection *) NULL; s = s->next) |
| 665 | { |
| 666 | /* except for bss */ |
| 667 | if ((s->flags & SEC_IS_COMMON) == 0) |
| 668 | { |
| 669 | wild_doit (&ptr->children, s, output, file); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | /* Do the creation to the named section only */ |
| 676 | wild_doit (&ptr->children, |
| 677 | our_bfd_get_section_by_name (file->the_bfd, section), |
| 678 | output, file); |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* passed a file name (which must have been seen already and added to |
| 684 | the statement tree. We will see if it has been opened already and |
| 685 | had its symbols read. If not then we'll read it. |
| 686 | |
| 687 | Archives are pecuilar here. We may open them once, but if they do |
| 688 | not define anything we need at the time, they won't have all their |
| 689 | symbols read. If we need them later, we'll have to redo it. |
| 690 | */ |
| 691 | static |
| 692 | lang_input_statement_type * |
| 693 | DEFUN (lookup_name, (name), |
| 694 | CONST char *CONST name) |
| 695 | { |
| 696 | lang_input_statement_type *search; |
| 697 | |
| 698 | for (search = (lang_input_statement_type *) input_file_chain.head; |
| 699 | search != (lang_input_statement_type *) NULL; |
| 700 | search = (lang_input_statement_type *) search->next_real_file) |
| 701 | { |
| 702 | if (search->filename == (char *) NULL && name == (char *) NULL) |
| 703 | { |
| 704 | return search; |
| 705 | } |
| 706 | if (search->filename != (char *) NULL && name != (char *) NULL) |
| 707 | { |
| 708 | if (strcmp (search->filename, name) == 0) |
| 709 | { |
| 710 | ldmain_open_file_read_symbol (search); |
| 711 | return search; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /* There isn't an afile entry for this file yet, this must be |
| 717 | because the name has only appeared inside a load script and not |
| 718 | on the command line */ |
| 719 | search = new_afile (name, lang_input_file_is_file_enum, default_target); |
| 720 | ldmain_open_file_read_symbol (search); |
| 721 | return search; |
| 722 | |
| 723 | |
| 724 | } |
| 725 | |
| 726 | static void |
| 727 | DEFUN (wild, (s, section, file, target, output), |
| 728 | lang_wild_statement_type * s AND |
| 729 | CONST char *CONST section AND |
| 730 | CONST char *CONST file AND |
| 731 | CONST char *CONST target AND |
| 732 | lang_output_section_statement_type * output) |
| 733 | { |
| 734 | lang_input_statement_type *f; |
| 735 | |
| 736 | if (file == (char *) NULL) |
| 737 | { |
| 738 | /* Perform the iteration over all files in the list */ |
| 739 | for (f = (lang_input_statement_type *) file_chain.head; |
| 740 | f != (lang_input_statement_type *) NULL; |
| 741 | f = (lang_input_statement_type *) f->next) |
| 742 | { |
| 743 | wild_section (s, section, f, output); |
| 744 | } |
| 745 | /* Once more for the script file */ |
| 746 | wild_section(s, section, script_file, output); |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | /* Perform the iteration over a single file */ |
| 751 | wild_section (s, section, lookup_name (file), output); |
| 752 | } |
| 753 | if (section != (char *) NULL |
| 754 | && strcmp (section, "COMMON") == 0 |
| 755 | && default_common_section == (lang_output_section_statement_type *) NULL) |
| 756 | { |
| 757 | /* Remember the section that common is going to incase we later |
| 758 | get something which doesn't know where to put it */ |
| 759 | default_common_section = output; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | read in all the files |
| 765 | */ |
| 766 | static bfd * |
| 767 | DEFUN (open_output, (name), |
| 768 | CONST char *CONST name) |
| 769 | { |
| 770 | extern unsigned long ldfile_output_machine; |
| 771 | extern enum bfd_architecture ldfile_output_architecture; |
| 772 | |
| 773 | extern CONST char *output_filename; |
| 774 | bfd *output; |
| 775 | |
| 776 | if (output_target == (char *) NULL) |
| 777 | { |
| 778 | if (current_target != (char *) NULL) |
| 779 | output_target = current_target; |
| 780 | else |
| 781 | output_target = default_target; |
| 782 | } |
| 783 | output = bfd_openw (name, output_target); |
| 784 | output_filename = name; |
| 785 | |
| 786 | if (output == (bfd *) NULL) |
| 787 | { |
| 788 | if (bfd_error == invalid_target) |
| 789 | { |
| 790 | einfo ("%P%F target %s not found\n", output_target); |
| 791 | } |
| 792 | einfo ("%P%F problem opening output file %s, %E\n", name); |
| 793 | } |
| 794 | |
| 795 | /* output->flags |= D_PAGED;*/ |
| 796 | |
| 797 | bfd_set_format (output, bfd_object); |
| 798 | bfd_set_arch_mach (output, |
| 799 | ldfile_output_architecture, |
| 800 | ldfile_output_machine); |
| 801 | return output; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | static void |
| 808 | DEFUN (ldlang_open_output, (statement), |
| 809 | lang_statement_union_type * statement) |
| 810 | { |
| 811 | switch (statement->header.type) |
| 812 | { |
| 813 | case lang_output_statement_enum: |
| 814 | output_bfd = open_output (statement->output_statement.name); |
| 815 | ldemul_set_output_arch (); |
| 816 | if (config.magic_demand_paged && !config.relocateable_output) |
| 817 | output_bfd->flags |= D_PAGED; |
| 818 | else |
| 819 | output_bfd->flags &= ~D_PAGED; |
| 820 | if (config.text_read_only) |
| 821 | output_bfd->flags |= WP_TEXT; |
| 822 | else |
| 823 | output_bfd->flags &= ~WP_TEXT; |
| 824 | break; |
| 825 | |
| 826 | case lang_target_statement_enum: |
| 827 | current_target = statement->target_statement.target; |
| 828 | break; |
| 829 | default: |
| 830 | break; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | static void |
| 835 | DEFUN (open_input_bfds, (statement), |
| 836 | lang_statement_union_type * statement) |
| 837 | { |
| 838 | switch (statement->header.type) |
| 839 | { |
| 840 | case lang_target_statement_enum: |
| 841 | current_target = statement->target_statement.target; |
| 842 | break; |
| 843 | case lang_wild_statement_enum: |
| 844 | /* Maybe we should load the file's symbols */ |
| 845 | if (statement->wild_statement.filename) |
| 846 | { |
| 847 | (void) lookup_name (statement->wild_statement.filename); |
| 848 | } |
| 849 | break; |
| 850 | case lang_input_statement_enum: |
| 851 | if (statement->input_statement.real == true) |
| 852 | { |
| 853 | statement->input_statement.target = current_target; |
| 854 | lookup_name (statement->input_statement.filename); |
| 855 | } |
| 856 | break; |
| 857 | default: |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* If there are [COMMONS] statements, put a wild one into the bss section */ |
| 863 | |
| 864 | static void |
| 865 | lang_reasonable_defaults () |
| 866 | { |
| 867 | |
| 868 | |
| 869 | |
| 870 | #if 0 |
| 871 | lang_output_section_statement_lookup (".text"); |
| 872 | lang_output_section_statement_lookup (".data"); |
| 873 | |
| 874 | default_common_section = |
| 875 | lang_output_section_statement_lookup (".bss"); |
| 876 | |
| 877 | |
| 878 | if (placed_commons == false) |
| 879 | { |
| 880 | lang_wild_statement_type *new = |
| 881 | new_stat (lang_wild_statement, |
| 882 | &default_common_section->children); |
| 883 | |
| 884 | new->section_name = "COMMON"; |
| 885 | new->filename = (char *) NULL; |
| 886 | lang_list_init (&new->children); |
| 887 | } |
| 888 | #endif |
| 889 | |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | Add the supplied name to the symbol table as an undefined reference. |
| 894 | Remove items from the chain as we open input bfds |
| 895 | */ |
| 896 | typedef struct ldlang_undef_chain_list |
| 897 | { |
| 898 | struct ldlang_undef_chain_list *next; |
| 899 | char *name; |
| 900 | } ldlang_undef_chain_list_type; |
| 901 | |
| 902 | static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head; |
| 903 | |
| 904 | void |
| 905 | DEFUN (ldlang_add_undef, (name), |
| 906 | CONST char *CONST name) |
| 907 | { |
| 908 | ldlang_undef_chain_list_type *new = |
| 909 | (ldlang_undef_chain_list_type |
| 910 | *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type))); |
| 911 | |
| 912 | new->next = ldlang_undef_chain_list_head; |
| 913 | ldlang_undef_chain_list_head = new; |
| 914 | |
| 915 | new->name = buystring (name); |
| 916 | } |
| 917 | |
| 918 | /* Run through the list of undefineds created above and place them |
| 919 | into the linker hash table as undefined symbols belonging to the |
| 920 | script file. |
| 921 | */ |
| 922 | static void |
| 923 | DEFUN_VOID (lang_place_undefineds) |
| 924 | { |
| 925 | ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head; |
| 926 | |
| 927 | while (ptr != (ldlang_undef_chain_list_type *) NULL) |
| 928 | { |
| 929 | asymbol *def; |
| 930 | asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **))); |
| 931 | |
| 932 | def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd); |
| 933 | *def_ptr = def; |
| 934 | def->name = ptr->name; |
| 935 | def->section = &bfd_und_section; |
| 936 | Q_enter_global_ref (def_ptr, ptr->name); |
| 937 | ptr = ptr->next; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /* Copy important data from out internal form to the bfd way. Also |
| 942 | create a section for the dummy file |
| 943 | */ |
| 944 | |
| 945 | static void |
| 946 | DEFUN_VOID (lang_create_output_section_statements) |
| 947 | { |
| 948 | lang_statement_union_type *os; |
| 949 | |
| 950 | for (os = lang_output_section_statement.head; |
| 951 | os != (lang_statement_union_type *) NULL; |
| 952 | os = os->output_section_statement.next) |
| 953 | { |
| 954 | lang_output_section_statement_type *s = |
| 955 | &os->output_section_statement; |
| 956 | |
| 957 | init_os (s); |
| 958 | } |
| 959 | |
| 960 | } |
| 961 | |
| 962 | static void |
| 963 | DEFUN_VOID (lang_init_script_file) |
| 964 | { |
| 965 | script_file = lang_add_input_file ("command line", |
| 966 | lang_input_file_is_fake_enum, |
| 967 | (char *) NULL); |
| 968 | script_file->the_bfd = bfd_create ("command line", output_bfd); |
| 969 | script_file->symbol_count = 0; |
| 970 | script_file->the_bfd->sections = 0; |
| 971 | |
| 972 | /* The user data of a bfd points to the input statement attatched */ |
| 973 | script_file->the_bfd->usrdata = (void *)script_file; |
| 974 | script_file->common_section = |
| 975 | bfd_make_section(script_file->the_bfd,"COMMON"); |
| 976 | |
| 977 | abs_output_section = |
| 978 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); |
| 979 | |
| 980 | abs_output_section->bfd_section = &bfd_abs_section; |
| 981 | |
| 982 | } |
| 983 | |
| 984 | /* Open input files and attatch to output sections */ |
| 985 | static void |
| 986 | DEFUN (map_input_to_output_sections, (s, target, output_section_statement), |
| 987 | lang_statement_union_type * s AND |
| 988 | CONST char *target AND |
| 989 | lang_output_section_statement_type * output_section_statement) |
| 990 | { |
| 991 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
| 992 | { |
| 993 | switch (s->header.type) |
| 994 | { |
| 995 | |
| 996 | |
| 997 | case lang_wild_statement_enum: |
| 998 | wild (&s->wild_statement, s->wild_statement.section_name, |
| 999 | s->wild_statement.filename, target, |
| 1000 | output_section_statement); |
| 1001 | |
| 1002 | break; |
| 1003 | case lang_constructors_statement_enum: |
| 1004 | map_input_to_output_sections (constructor_list.head, |
| 1005 | target, |
| 1006 | output_section_statement); |
| 1007 | break; |
| 1008 | case lang_output_section_statement_enum: |
| 1009 | map_input_to_output_sections (s->output_section_statement.children.head, |
| 1010 | target, |
| 1011 | &s->output_section_statement); |
| 1012 | break; |
| 1013 | case lang_output_statement_enum: |
| 1014 | break; |
| 1015 | case lang_target_statement_enum: |
| 1016 | target = s->target_statement.target; |
| 1017 | break; |
| 1018 | case lang_fill_statement_enum: |
| 1019 | case lang_input_section_enum: |
| 1020 | case lang_object_symbols_statement_enum: |
| 1021 | case lang_data_statement_enum: |
| 1022 | case lang_assignment_statement_enum: |
| 1023 | case lang_padding_statement_enum: |
| 1024 | break; |
| 1025 | case lang_afile_asection_pair_statement_enum: |
| 1026 | FAIL (); |
| 1027 | break; |
| 1028 | case lang_address_statement_enum: |
| 1029 | /* Mark the specified section with the supplied address */ |
| 1030 | { |
| 1031 | lang_output_section_statement_type *os = |
| 1032 | lang_output_section_statement_lookup |
| 1033 | (s->address_statement.section_name); |
| 1034 | |
| 1035 | os->addr_tree = s->address_statement.address; |
| 1036 | if (os->bfd_section == (asection *) NULL) |
| 1037 | { |
| 1038 | einfo ("%P%F can't set the address of undefined section %s\n", |
| 1039 | s->address_statement.section_name); |
| 1040 | } |
| 1041 | } |
| 1042 | break; |
| 1043 | case lang_input_statement_enum: |
| 1044 | /* A standard input statement, has no wildcards */ |
| 1045 | /* ldmain_open_file_read_symbol(&s->input_statement);*/ |
| 1046 | break; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | static void |
| 1056 | DEFUN (print_output_section_statement, (output_section_statement), |
| 1057 | lang_output_section_statement_type * output_section_statement) |
| 1058 | { |
| 1059 | asection *section = output_section_statement->bfd_section; |
| 1060 | |
| 1061 | print_nl (); |
| 1062 | print_section (output_section_statement->name); |
| 1063 | |
| 1064 | |
| 1065 | if (section) |
| 1066 | { |
| 1067 | print_dot = section->vma; |
| 1068 | print_space (); |
| 1069 | print_section (""); |
| 1070 | print_space (); |
| 1071 | print_address (section->vma); |
| 1072 | print_space (); |
| 1073 | print_size (section->_raw_size); |
| 1074 | print_space(); |
| 1075 | print_size(section->_cooked_size); |
| 1076 | print_space (); |
| 1077 | print_alignment (section->alignment_power); |
| 1078 | print_space (); |
| 1079 | #if 0 |
| 1080 | fprintf (config.map_file, "%s flags", output_section_statement->region->name); |
| 1081 | print_flags (stdout, &output_section_statement->flags); |
| 1082 | #endif |
| 1083 | if (section->flags & SEC_LOAD) |
| 1084 | fprintf (config.map_file, "load "); |
| 1085 | if (section->flags & SEC_ALLOC) |
| 1086 | fprintf (config.map_file, "alloc "); |
| 1087 | if (section->flags & SEC_RELOC) |
| 1088 | fprintf (config.map_file, "reloc "); |
| 1089 | if (section->flags & SEC_HAS_CONTENTS) |
| 1090 | fprintf (config.map_file, "contents "); |
| 1091 | |
| 1092 | } |
| 1093 | else |
| 1094 | { |
| 1095 | fprintf (config.map_file, "No attached output section"); |
| 1096 | } |
| 1097 | print_nl (); |
| 1098 | if (output_section_statement->load_base) |
| 1099 | { |
| 1100 | int b = exp_get_value_int(output_section_statement->load_base, |
| 1101 | 0, "output base", lang_final_phase_enum); |
| 1102 | printf("Output address %08x\n", b); |
| 1103 | } |
| 1104 | if (output_section_statement->section_alignment >= 0 |
| 1105 | || output_section_statement->section_alignment >= 0) |
| 1106 | { |
| 1107 | printf("\t\t\t\t\tforced alignment "); |
| 1108 | if ( output_section_statement->section_alignment >= 0) |
| 1109 | { |
| 1110 | printf("section 2**%d ",output_section_statement->section_alignment ); |
| 1111 | } |
| 1112 | if ( output_section_statement->subsection_alignment >= 0) |
| 1113 | { |
| 1114 | printf("subsection 2**%d ",output_section_statement->subsection_alignment ); |
| 1115 | } |
| 1116 | |
| 1117 | print_nl (); |
| 1118 | } |
| 1119 | print_statement (output_section_statement->children.head, |
| 1120 | output_section_statement); |
| 1121 | |
| 1122 | } |
| 1123 | |
| 1124 | static void |
| 1125 | DEFUN (print_assignment, (assignment, output_section), |
| 1126 | lang_assignment_statement_type * assignment AND |
| 1127 | lang_output_section_statement_type * output_section) |
| 1128 | { |
| 1129 | etree_value_type result; |
| 1130 | |
| 1131 | print_section (""); |
| 1132 | print_space (); |
| 1133 | print_section (""); |
| 1134 | print_space (); |
| 1135 | print_address (print_dot); |
| 1136 | print_space (); |
| 1137 | result = exp_fold_tree (assignment->exp->assign.src, |
| 1138 | output_section, |
| 1139 | lang_final_phase_enum, |
| 1140 | print_dot, |
| 1141 | &print_dot); |
| 1142 | |
| 1143 | if (result.valid) |
| 1144 | { |
| 1145 | print_address (result.value); |
| 1146 | } |
| 1147 | else |
| 1148 | { |
| 1149 | fprintf (config.map_file, "*undefined*"); |
| 1150 | } |
| 1151 | print_space (); |
| 1152 | exp_print_tree (assignment->exp); |
| 1153 | |
| 1154 | fprintf (config.map_file, "\n"); |
| 1155 | } |
| 1156 | |
| 1157 | static void |
| 1158 | DEFUN (print_input_statement, (statm), |
| 1159 | lang_input_statement_type * statm) |
| 1160 | { |
| 1161 | if (statm->filename != (char *) NULL) |
| 1162 | { |
| 1163 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | static void |
| 1168 | DEFUN (print_symbol, (q), |
| 1169 | asymbol * q) |
| 1170 | { |
| 1171 | print_section (""); |
| 1172 | fprintf (config.map_file, " "); |
| 1173 | print_section (""); |
| 1174 | fprintf (config.map_file, " "); |
| 1175 | print_address (outside_symbol_address (q)); |
| 1176 | fprintf (config.map_file, " %s", q->name ? q->name : " "); |
| 1177 | print_nl (); |
| 1178 | } |
| 1179 | |
| 1180 | static void |
| 1181 | DEFUN (print_input_section, (in), |
| 1182 | lang_input_section_type * in) |
| 1183 | { |
| 1184 | asection *i = in->section; |
| 1185 | int size = i->reloc_done ? |
| 1186 | bfd_get_section_size_after_reloc (i) : |
| 1187 | bfd_get_section_size_before_reloc (i); |
| 1188 | |
| 1189 | if (size != 0) |
| 1190 | { |
| 1191 | print_section (""); |
| 1192 | fprintf (config.map_file, " "); |
| 1193 | print_section (i->name); |
| 1194 | fprintf (config.map_file, " "); |
| 1195 | if (i->output_section) |
| 1196 | { |
| 1197 | print_address (i->output_section->vma + i->output_offset); |
| 1198 | fprintf (config.map_file, " "); |
| 1199 | print_size (i->_raw_size); |
| 1200 | fprintf (config.map_file, " "); |
| 1201 | print_size(i->_cooked_size); |
| 1202 | fprintf (config.map_file, " "); |
| 1203 | print_alignment (i->alignment_power); |
| 1204 | fprintf (config.map_file, " "); |
| 1205 | if (in->ifile) |
| 1206 | { |
| 1207 | |
| 1208 | bfd *abfd = in->ifile->the_bfd; |
| 1209 | |
| 1210 | if (in->ifile->just_syms_flag == true) |
| 1211 | { |
| 1212 | fprintf (config.map_file, "symbols only "); |
| 1213 | } |
| 1214 | |
| 1215 | fprintf (config.map_file, " %s ", abfd->xvec->name); |
| 1216 | if (abfd->my_archive != (bfd *) NULL) |
| 1217 | { |
| 1218 | fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename, |
| 1219 | abfd->filename); |
| 1220 | } |
| 1221 | else |
| 1222 | { |
| 1223 | fprintf (config.map_file, "%s", abfd->filename); |
| 1224 | } |
| 1225 | fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd)); |
| 1226 | print_nl (); |
| 1227 | |
| 1228 | /* Find all the symbols in this file defined in this section */ |
| 1229 | |
| 1230 | if (in->ifile->symbol_count) |
| 1231 | { |
| 1232 | asymbol **p; |
| 1233 | |
| 1234 | for (p = in->ifile->asymbols; *p; p++) |
| 1235 | { |
| 1236 | asymbol *q = *p; |
| 1237 | |
| 1238 | if (bfd_get_section (q) == i && q->flags & BSF_GLOBAL) |
| 1239 | { |
| 1240 | print_symbol (q); |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | else |
| 1246 | { |
| 1247 | print_nl (); |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | print_dot = outside_section_address (i) + size; |
| 1252 | } |
| 1253 | else |
| 1254 | { |
| 1255 | fprintf (config.map_file, "No output section allocated\n"); |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | static void |
| 1261 | DEFUN (print_fill_statement, (fill), |
| 1262 | lang_fill_statement_type * fill) |
| 1263 | { |
| 1264 | fprintf (config.map_file, "FILL mask "); |
| 1265 | print_fill (fill->fill); |
| 1266 | } |
| 1267 | |
| 1268 | static void |
| 1269 | DEFUN (print_data_statement, (data), |
| 1270 | lang_data_statement_type * data) |
| 1271 | { |
| 1272 | /* bfd_vma value; */ |
| 1273 | print_section (""); |
| 1274 | print_space (); |
| 1275 | print_section (""); |
| 1276 | print_space (); |
| 1277 | /* ASSERT(print_dot == data->output_vma);*/ |
| 1278 | |
| 1279 | print_address (data->output_vma + data->output_section->vma); |
| 1280 | print_space (); |
| 1281 | print_address (data->value); |
| 1282 | print_space (); |
| 1283 | switch (data->type) |
| 1284 | { |
| 1285 | case BYTE: |
| 1286 | fprintf (config.map_file, "BYTE "); |
| 1287 | print_dot += BYTE_SIZE; |
| 1288 | break; |
| 1289 | case SHORT: |
| 1290 | fprintf (config.map_file, "SHORT "); |
| 1291 | print_dot += SHORT_SIZE; |
| 1292 | break; |
| 1293 | case LONG: |
| 1294 | fprintf (config.map_file, "LONG "); |
| 1295 | print_dot += LONG_SIZE; |
| 1296 | break; |
| 1297 | } |
| 1298 | |
| 1299 | exp_print_tree (data->exp); |
| 1300 | |
| 1301 | fprintf (config.map_file, "\n"); |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | static void |
| 1306 | DEFUN (print_padding_statement, (s), |
| 1307 | lang_padding_statement_type * s) |
| 1308 | { |
| 1309 | print_section (""); |
| 1310 | print_space (); |
| 1311 | print_section ("*fill*"); |
| 1312 | print_space (); |
| 1313 | print_address (s->output_offset + s->output_section->vma); |
| 1314 | print_space (); |
| 1315 | print_size (s->size); |
| 1316 | print_space (); |
| 1317 | print_fill (s->fill); |
| 1318 | print_nl (); |
| 1319 | |
| 1320 | print_dot = s->output_offset + s->output_section->vma + s->size; |
| 1321 | |
| 1322 | } |
| 1323 | |
| 1324 | static void |
| 1325 | DEFUN (print_wild_statement, (w, os), |
| 1326 | lang_wild_statement_type * w AND |
| 1327 | lang_output_section_statement_type * os) |
| 1328 | { |
| 1329 | fprintf (config.map_file, " from "); |
| 1330 | if (w->filename != (char *) NULL) |
| 1331 | { |
| 1332 | fprintf (config.map_file, "%s", w->filename); |
| 1333 | } |
| 1334 | else |
| 1335 | { |
| 1336 | fprintf (config.map_file, "*"); |
| 1337 | } |
| 1338 | if (w->section_name != (char *) NULL) |
| 1339 | { |
| 1340 | fprintf (config.map_file, "(%s)", w->section_name); |
| 1341 | } |
| 1342 | else |
| 1343 | { |
| 1344 | fprintf (config.map_file, "(*)"); |
| 1345 | } |
| 1346 | print_nl (); |
| 1347 | print_statement (w->children.head, os); |
| 1348 | |
| 1349 | } |
| 1350 | static void |
| 1351 | DEFUN (print_statement, (s, os), |
| 1352 | lang_statement_union_type * s AND |
| 1353 | lang_output_section_statement_type * os) |
| 1354 | { |
| 1355 | while (s) |
| 1356 | { |
| 1357 | switch (s->header.type) |
| 1358 | { |
| 1359 | case lang_constructors_statement_enum: |
| 1360 | fprintf (config.map_file, "constructors:\n"); |
| 1361 | print_statement (constructor_list.head, os); |
| 1362 | break; |
| 1363 | case lang_wild_statement_enum: |
| 1364 | print_wild_statement (&s->wild_statement, os); |
| 1365 | break; |
| 1366 | default: |
| 1367 | fprintf (config.map_file, "Fail with %d\n", s->header.type); |
| 1368 | FAIL (); |
| 1369 | break; |
| 1370 | case lang_address_statement_enum: |
| 1371 | fprintf (config.map_file, "address\n"); |
| 1372 | break; |
| 1373 | case lang_object_symbols_statement_enum: |
| 1374 | fprintf (config.map_file, "object symbols\n"); |
| 1375 | break; |
| 1376 | case lang_fill_statement_enum: |
| 1377 | print_fill_statement (&s->fill_statement); |
| 1378 | break; |
| 1379 | case lang_data_statement_enum: |
| 1380 | print_data_statement (&s->data_statement); |
| 1381 | break; |
| 1382 | case lang_input_section_enum: |
| 1383 | print_input_section (&s->input_section); |
| 1384 | break; |
| 1385 | case lang_padding_statement_enum: |
| 1386 | print_padding_statement (&s->padding_statement); |
| 1387 | break; |
| 1388 | case lang_output_section_statement_enum: |
| 1389 | print_output_section_statement (&s->output_section_statement); |
| 1390 | break; |
| 1391 | case lang_assignment_statement_enum: |
| 1392 | print_assignment (&s->assignment_statement, |
| 1393 | os); |
| 1394 | break; |
| 1395 | case lang_target_statement_enum: |
| 1396 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); |
| 1397 | break; |
| 1398 | case lang_output_statement_enum: |
| 1399 | fprintf (config.map_file, "OUTPUT(%s %s)\n", |
| 1400 | s->output_statement.name, |
| 1401 | output_target ? output_target : ""); |
| 1402 | break; |
| 1403 | case lang_input_statement_enum: |
| 1404 | print_input_statement (&s->input_statement); |
| 1405 | break; |
| 1406 | case lang_afile_asection_pair_statement_enum: |
| 1407 | FAIL (); |
| 1408 | break; |
| 1409 | } |
| 1410 | s = s->next; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | static void |
| 1416 | DEFUN_VOID (print_statements) |
| 1417 | { |
| 1418 | print_statement (statement_list.head, |
| 1419 | abs_output_section); |
| 1420 | |
| 1421 | } |
| 1422 | |
| 1423 | static bfd_vma |
| 1424 | DEFUN (insert_pad, (this_ptr, fill, power, output_section_statement, dot), |
| 1425 | lang_statement_union_type ** this_ptr AND |
| 1426 | fill_type fill AND |
| 1427 | unsigned int power AND |
| 1428 | asection * output_section_statement AND |
| 1429 | bfd_vma dot) |
| 1430 | { |
| 1431 | /* Align this section first to the |
| 1432 | input sections requirement, then |
| 1433 | to the output section's requirement. |
| 1434 | If this alignment is > than any seen before, |
| 1435 | then record it too. Perform the alignment by |
| 1436 | inserting a magic 'padding' statement. |
| 1437 | */ |
| 1438 | |
| 1439 | unsigned int alignment_needed = align_power (dot, power) - dot; |
| 1440 | |
| 1441 | if (alignment_needed != 0) |
| 1442 | { |
| 1443 | lang_statement_union_type *new = |
| 1444 | (lang_statement_union_type *) |
| 1445 | stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type))); |
| 1446 | |
| 1447 | /* Link into existing chain */ |
| 1448 | new->header.next = *this_ptr; |
| 1449 | *this_ptr = new; |
| 1450 | new->header.type = lang_padding_statement_enum; |
| 1451 | new->padding_statement.output_section = output_section_statement; |
| 1452 | new->padding_statement.output_offset = |
| 1453 | dot - output_section_statement->vma; |
| 1454 | new->padding_statement.fill = fill; |
| 1455 | new->padding_statement.size = alignment_needed; |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | /* Remember the most restrictive alignment */ |
| 1460 | if (power > output_section_statement->alignment_power) |
| 1461 | { |
| 1462 | output_section_statement->alignment_power = power; |
| 1463 | } |
| 1464 | output_section_statement->_raw_size += alignment_needed; |
| 1465 | return alignment_needed + dot; |
| 1466 | |
| 1467 | } |
| 1468 | |
| 1469 | /* Work out how much this section will move the dot point */ |
| 1470 | static bfd_vma |
| 1471 | DEFUN (size_input_section, (this_ptr, output_section_statement, fill, |
| 1472 | dot, relax), |
| 1473 | lang_statement_union_type ** this_ptr AND |
| 1474 | lang_output_section_statement_type * output_section_statement AND |
| 1475 | unsigned short fill AND |
| 1476 | bfd_vma dot AND |
| 1477 | boolean relax) |
| 1478 | { |
| 1479 | lang_input_section_type *is = &((*this_ptr)->input_section); |
| 1480 | asection *i = is->section; |
| 1481 | |
| 1482 | if (is->ifile->just_syms_flag == false) |
| 1483 | { |
| 1484 | if (output_section_statement->subsection_alignment != -1) |
| 1485 | i->alignment_power = |
| 1486 | output_section_statement->subsection_alignment; |
| 1487 | |
| 1488 | dot = insert_pad (this_ptr, fill, i->alignment_power, |
| 1489 | output_section_statement->bfd_section, dot); |
| 1490 | |
| 1491 | /* remember the largest size so we can malloc the largest area |
| 1492 | needed for the output stage. Only remember the size of sections |
| 1493 | which we will actually allocate */ |
| 1494 | if (((i->flags & |
| 1495 | (SEC_HAS_CONTENTS | SEC_ALLOC)) == (SEC_HAS_CONTENTS | SEC_ALLOC)) |
| 1496 | && (bfd_get_section_size_before_reloc (i) > largest_section)) |
| 1497 | { |
| 1498 | largest_section = bfd_get_section_size_before_reloc (i); |
| 1499 | } |
| 1500 | |
| 1501 | /* Remember where in the output section this input section goes */ |
| 1502 | |
| 1503 | i->output_offset = dot - output_section_statement->bfd_section->vma; |
| 1504 | |
| 1505 | /* Mark how big the output section must be to contain this now |
| 1506 | */ |
| 1507 | if (relax) |
| 1508 | { |
| 1509 | dot += i->_cooked_size; |
| 1510 | } |
| 1511 | else |
| 1512 | { |
| 1513 | dot += i->_raw_size; |
| 1514 | } |
| 1515 | output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma; |
| 1516 | } |
| 1517 | else |
| 1518 | { |
| 1519 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; |
| 1520 | } |
| 1521 | |
| 1522 | return dot; |
| 1523 | } |
| 1524 | |
| 1525 | /* Sizing happens in two passes, first pass we allocate worst case |
| 1526 | stuff. The second pass (if relaxing), we use what we learnt to |
| 1527 | change the size of some relocs from worst case to better |
| 1528 | */ |
| 1529 | static boolean had_relax; |
| 1530 | |
| 1531 | static bfd_vma |
| 1532 | DEFUN (lang_size_sections, (s, output_section_statement, prev, fill, |
| 1533 | dot, relax), |
| 1534 | lang_statement_union_type * s AND |
| 1535 | lang_output_section_statement_type * output_section_statement AND |
| 1536 | lang_statement_union_type ** prev AND |
| 1537 | unsigned short fill AND |
| 1538 | bfd_vma dot AND |
| 1539 | boolean relax) |
| 1540 | { |
| 1541 | /* Size up the sections from their constituent parts */ |
| 1542 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
| 1543 | { |
| 1544 | switch (s->header.type) |
| 1545 | { |
| 1546 | |
| 1547 | case lang_output_section_statement_enum: |
| 1548 | { |
| 1549 | bfd_vma after; |
| 1550 | lang_output_section_statement_type *os = &s->output_section_statement; |
| 1551 | |
| 1552 | /* If this is a shared library section, don't change the size |
| 1553 | and address. */ |
| 1554 | if (os->bfd_section->flags & SEC_SHARED_LIBRARY) |
| 1555 | break; |
| 1556 | |
| 1557 | if (os->bfd_section == &bfd_abs_section) |
| 1558 | { |
| 1559 | /* No matter what happens, an abs section starts at zero */ |
| 1560 | bfd_set_section_vma (0, os->bfd_section, 0); |
| 1561 | } |
| 1562 | else |
| 1563 | { |
| 1564 | if (os->addr_tree == (etree_type *) NULL) |
| 1565 | { |
| 1566 | /* No address specified for this section, get one |
| 1567 | from the region specification |
| 1568 | */ |
| 1569 | if (os->region == (lang_memory_region_type *) NULL) |
| 1570 | { |
| 1571 | os->region = lang_memory_region_lookup ("*default*"); |
| 1572 | } |
| 1573 | dot = os->region->current; |
| 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | etree_value_type r; |
| 1578 | |
| 1579 | r = exp_fold_tree (os->addr_tree, |
| 1580 | abs_output_section, |
| 1581 | lang_allocating_phase_enum, |
| 1582 | dot, &dot); |
| 1583 | if (r.valid == false) |
| 1584 | { |
| 1585 | einfo ("%F%S: non constant address expression for section %s\n", |
| 1586 | os->name); |
| 1587 | } |
| 1588 | dot = r.value; |
| 1589 | } |
| 1590 | /* The section starts here */ |
| 1591 | /* First, align to what the section needs */ |
| 1592 | |
| 1593 | |
| 1594 | dot = align_power (dot, os->bfd_section->alignment_power); |
| 1595 | bfd_set_section_vma (0, os->bfd_section, dot); |
| 1596 | |
| 1597 | if (os->load_base) { |
| 1598 | os->bfd_section->lma |
| 1599 | = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | os->bfd_section->output_offset = 0; |
| 1605 | |
| 1606 | (void) lang_size_sections (os->children.head, os, &os->children.head, |
| 1607 | os->fill, dot, relax); |
| 1608 | /* Ignore the size of the input sections, use the vma and size to */ |
| 1609 | /* align against */ |
| 1610 | |
| 1611 | |
| 1612 | after = ALIGN (os->bfd_section->vma + |
| 1613 | os->bfd_section->_raw_size, |
| 1614 | os->block_value); |
| 1615 | |
| 1616 | |
| 1617 | os->bfd_section->_raw_size = after - os->bfd_section->vma; |
| 1618 | dot = os->bfd_section->vma + os->bfd_section->_raw_size; |
| 1619 | os->processed = true; |
| 1620 | |
| 1621 | /* Replace into region ? */ |
| 1622 | if (os->addr_tree == (etree_type *) NULL |
| 1623 | && os->region != (lang_memory_region_type *) NULL) |
| 1624 | { |
| 1625 | os->region->current = dot; |
| 1626 | /* Make sure this isn't silly */ |
| 1627 | if (( os->region->current |
| 1628 | > os->region->origin + os->region->length) |
| 1629 | || ( os->region->origin > os->region->current )) |
| 1630 | { |
| 1631 | einfo ("%X%P: Region %s is full (%B section %s)\n", |
| 1632 | os->region->name, |
| 1633 | os->bfd_section->owner, |
| 1634 | os->bfd_section->name); |
| 1635 | /* Reset the region pointer */ |
| 1636 | os->region->current = 0; |
| 1637 | |
| 1638 | } |
| 1639 | |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | break; |
| 1644 | case lang_constructors_statement_enum: |
| 1645 | dot = lang_size_sections (constructor_list.head, |
| 1646 | output_section_statement, |
| 1647 | &s->wild_statement.children.head, |
| 1648 | fill, |
| 1649 | dot, relax); |
| 1650 | break; |
| 1651 | |
| 1652 | case lang_data_statement_enum: |
| 1653 | { |
| 1654 | unsigned int size = 0; |
| 1655 | |
| 1656 | s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma; |
| 1657 | s->data_statement.output_section = |
| 1658 | output_section_statement->bfd_section; |
| 1659 | |
| 1660 | switch (s->data_statement.type) |
| 1661 | { |
| 1662 | case LONG: |
| 1663 | size = LONG_SIZE; |
| 1664 | break; |
| 1665 | case SHORT: |
| 1666 | size = SHORT_SIZE; |
| 1667 | break; |
| 1668 | case BYTE: |
| 1669 | size = BYTE_SIZE; |
| 1670 | break; |
| 1671 | |
| 1672 | } |
| 1673 | dot += size; |
| 1674 | output_section_statement->bfd_section->_raw_size += size; |
| 1675 | } |
| 1676 | break; |
| 1677 | |
| 1678 | case lang_wild_statement_enum: |
| 1679 | |
| 1680 | dot = lang_size_sections (s->wild_statement.children.head, |
| 1681 | output_section_statement, |
| 1682 | &s->wild_statement.children.head, |
| 1683 | |
| 1684 | fill, dot, relax); |
| 1685 | |
| 1686 | break; |
| 1687 | |
| 1688 | case lang_object_symbols_statement_enum: |
| 1689 | create_object_symbols = output_section_statement; |
| 1690 | break; |
| 1691 | case lang_output_statement_enum: |
| 1692 | case lang_target_statement_enum: |
| 1693 | break; |
| 1694 | case lang_input_section_enum: |
| 1695 | if (relax) |
| 1696 | { |
| 1697 | relaxing = true; |
| 1698 | |
| 1699 | if( relax_section (prev)) |
| 1700 | had_relax = true; |
| 1701 | relaxing = false; |
| 1702 | |
| 1703 | } |
| 1704 | else { |
| 1705 | (*prev)->input_section.section->_cooked_size = |
| 1706 | (*prev)->input_section.section->_raw_size ; |
| 1707 | |
| 1708 | } |
| 1709 | dot = size_input_section (prev, |
| 1710 | output_section_statement, |
| 1711 | output_section_statement->fill, |
| 1712 | dot, relax); |
| 1713 | break; |
| 1714 | case lang_input_statement_enum: |
| 1715 | break; |
| 1716 | case lang_fill_statement_enum: |
| 1717 | s->fill_statement.output_section = output_section_statement->bfd_section; |
| 1718 | |
| 1719 | fill = s->fill_statement.fill; |
| 1720 | break; |
| 1721 | case lang_assignment_statement_enum: |
| 1722 | { |
| 1723 | bfd_vma newdot = dot; |
| 1724 | |
| 1725 | exp_fold_tree (s->assignment_statement.exp, |
| 1726 | output_section_statement, |
| 1727 | lang_allocating_phase_enum, |
| 1728 | dot, |
| 1729 | &newdot); |
| 1730 | |
| 1731 | if (newdot != dot && !relax) |
| 1732 | /* We've been moved ! so insert a pad */ |
| 1733 | { |
| 1734 | lang_statement_union_type *new = |
| 1735 | (lang_statement_union_type *) |
| 1736 | stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type))); |
| 1737 | |
| 1738 | /* Link into existing chain */ |
| 1739 | new->header.next = *prev; |
| 1740 | *prev = new; |
| 1741 | new->header.type = lang_padding_statement_enum; |
| 1742 | new->padding_statement.output_section = |
| 1743 | output_section_statement->bfd_section; |
| 1744 | new->padding_statement.output_offset = |
| 1745 | dot - output_section_statement->bfd_section->vma; |
| 1746 | new->padding_statement.fill = fill; |
| 1747 | new->padding_statement.size = newdot - dot; |
| 1748 | output_section_statement->bfd_section->_raw_size += |
| 1749 | new->padding_statement.size; |
| 1750 | dot = newdot; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | break; |
| 1755 | default: |
| 1756 | FAIL (); |
| 1757 | break; |
| 1758 | /* This can only get here when relaxing is turned on */ |
| 1759 | case lang_padding_statement_enum: |
| 1760 | |
| 1761 | case lang_address_statement_enum: |
| 1762 | break; |
| 1763 | } |
| 1764 | prev = &s->header.next; |
| 1765 | } |
| 1766 | return dot; |
| 1767 | } |
| 1768 | |
| 1769 | static bfd_vma |
| 1770 | DEFUN (lang_do_assignments, (s, output_section_statement, fill, dot), |
| 1771 | lang_statement_union_type * s AND |
| 1772 | lang_output_section_statement_type * output_section_statement AND |
| 1773 | unsigned short fill AND |
| 1774 | bfd_vma dot) |
| 1775 | { |
| 1776 | |
| 1777 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
| 1778 | { |
| 1779 | switch (s->header.type) |
| 1780 | { |
| 1781 | case lang_constructors_statement_enum: |
| 1782 | dot = lang_do_assignments (constructor_list.head, |
| 1783 | output_section_statement, |
| 1784 | fill, |
| 1785 | dot); |
| 1786 | break; |
| 1787 | |
| 1788 | case lang_output_section_statement_enum: |
| 1789 | { |
| 1790 | lang_output_section_statement_type *os = |
| 1791 | &(s->output_section_statement); |
| 1792 | |
| 1793 | dot = os->bfd_section->vma; |
| 1794 | (void) lang_do_assignments (os->children.head, os, os->fill, dot); |
| 1795 | dot = os->bfd_section->vma + os->bfd_section->_raw_size; |
| 1796 | } |
| 1797 | break; |
| 1798 | case lang_wild_statement_enum: |
| 1799 | |
| 1800 | dot = lang_do_assignments (s->wild_statement.children.head, |
| 1801 | output_section_statement, |
| 1802 | fill, dot); |
| 1803 | |
| 1804 | break; |
| 1805 | |
| 1806 | case lang_object_symbols_statement_enum: |
| 1807 | case lang_output_statement_enum: |
| 1808 | case lang_target_statement_enum: |
| 1809 | #if 0 |
| 1810 | case lang_common_statement_enum: |
| 1811 | #endif |
| 1812 | break; |
| 1813 | case lang_data_statement_enum: |
| 1814 | { |
| 1815 | etree_value_type value; |
| 1816 | |
| 1817 | value = exp_fold_tree (s->data_statement.exp, |
| 1818 | abs_output_section, |
| 1819 | lang_final_phase_enum, dot, &dot); |
| 1820 | s->data_statement.value = value.value; |
| 1821 | if (value.valid == false) |
| 1822 | einfo ("%F%P: Invalid data statement\n"); |
| 1823 | } |
| 1824 | switch (s->data_statement.type) |
| 1825 | { |
| 1826 | case LONG: |
| 1827 | dot += LONG_SIZE; |
| 1828 | break; |
| 1829 | case SHORT: |
| 1830 | dot += SHORT_SIZE; |
| 1831 | break; |
| 1832 | case BYTE: |
| 1833 | dot += BYTE_SIZE; |
| 1834 | break; |
| 1835 | } |
| 1836 | break; |
| 1837 | case lang_input_section_enum: |
| 1838 | { |
| 1839 | asection *in = s->input_section.section; |
| 1840 | |
| 1841 | dot += bfd_get_section_size_before_reloc (in); |
| 1842 | } |
| 1843 | break; |
| 1844 | |
| 1845 | case lang_input_statement_enum: |
| 1846 | break; |
| 1847 | case lang_fill_statement_enum: |
| 1848 | fill = s->fill_statement.fill; |
| 1849 | break; |
| 1850 | case lang_assignment_statement_enum: |
| 1851 | { |
| 1852 | exp_fold_tree (s->assignment_statement.exp, |
| 1853 | output_section_statement, |
| 1854 | lang_final_phase_enum, |
| 1855 | dot, |
| 1856 | &dot); |
| 1857 | } |
| 1858 | |
| 1859 | break; |
| 1860 | case lang_padding_statement_enum: |
| 1861 | dot += s->padding_statement.size; |
| 1862 | break; |
| 1863 | default: |
| 1864 | FAIL (); |
| 1865 | break; |
| 1866 | case lang_address_statement_enum: |
| 1867 | break; |
| 1868 | } |
| 1869 | |
| 1870 | } |
| 1871 | return dot; |
| 1872 | } |
| 1873 | |
| 1874 | |
| 1875 | |
| 1876 | static void |
| 1877 | DEFUN_VOID (lang_relocate_globals) |
| 1878 | { |
| 1879 | |
| 1880 | /* |
| 1881 | Each ldsym_type maintains a chain of pointers to asymbols which |
| 1882 | references the definition. Replace each pointer to the referenence |
| 1883 | with a pointer to only one place, preferably the definition. If |
| 1884 | the defintion isn't available then the common symbol, and if |
| 1885 | there isn't one of them then choose one reference. |
| 1886 | */ |
| 1887 | |
| 1888 | FOR_EACH_LDSYM (lgs) |
| 1889 | { |
| 1890 | asymbol *it; |
| 1891 | |
| 1892 | if (lgs->sdefs_chain) |
| 1893 | { |
| 1894 | it = *(lgs->sdefs_chain); |
| 1895 | } |
| 1896 | else if (lgs->scoms_chain != (asymbol **) NULL) |
| 1897 | { |
| 1898 | it = *(lgs->scoms_chain); |
| 1899 | } |
| 1900 | else if (lgs->srefs_chain != (asymbol **) NULL) |
| 1901 | { |
| 1902 | it = *(lgs->srefs_chain); |
| 1903 | } |
| 1904 | else |
| 1905 | { |
| 1906 | /* This can happen when the command line asked for a symbol to |
| 1907 | be -u */ |
| 1908 | it = (asymbol *) NULL; |
| 1909 | } |
| 1910 | if (it != (asymbol *) NULL) |
| 1911 | { |
| 1912 | asymbol **prev = 0; |
| 1913 | asymbol **ptr = lgs->srefs_chain;; |
| 1914 | if (lgs->flags & SYM_WARNING) |
| 1915 | { |
| 1916 | produce_warnings (lgs, it); |
| 1917 | } |
| 1918 | |
| 1919 | while (ptr != (asymbol **) NULL |
| 1920 | && ptr != prev) |
| 1921 | { |
| 1922 | asymbol *ref = *ptr; |
| 1923 | prev = ptr; |
| 1924 | *ptr = it; |
| 1925 | ptr = (asymbol **) (ref->udata); |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | static void |
| 1934 | DEFUN_VOID (lang_finish) |
| 1935 | { |
| 1936 | ldsym_type *lgs; |
| 1937 | int warn = config.relocateable_output != true; |
| 1938 | if (entry_symbol == (char *) NULL) |
| 1939 | { |
| 1940 | /* No entry has been specified, look for start, but don't warn */ |
| 1941 | entry_symbol = "start"; |
| 1942 | warn =0; |
| 1943 | } |
| 1944 | lgs = ldsym_get_soft (entry_symbol); |
| 1945 | if (lgs && lgs->sdefs_chain) |
| 1946 | { |
| 1947 | asymbol *sy = *(lgs->sdefs_chain); |
| 1948 | |
| 1949 | /* We can set the entry address*/ |
| 1950 | bfd_set_start_address (output_bfd, |
| 1951 | outside_symbol_address (sy)); |
| 1952 | |
| 1953 | } |
| 1954 | else |
| 1955 | { |
| 1956 | /* Can't find anything reasonable, |
| 1957 | use the first address in the text section |
| 1958 | */ |
| 1959 | asection *ts = bfd_get_section_by_name (output_bfd, ".text"); |
| 1960 | if (ts) |
| 1961 | { |
| 1962 | if (warn) |
| 1963 | einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n", |
| 1964 | entry_symbol, ts->vma); |
| 1965 | |
| 1966 | bfd_set_start_address (output_bfd, ts->vma); |
| 1967 | } |
| 1968 | else |
| 1969 | { |
| 1970 | if (warn) |
| 1971 | einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n", |
| 1972 | entry_symbol); |
| 1973 | } |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /* By now we know the target architecture, and we may have an */ |
| 1978 | /* ldfile_output_machine_name */ |
| 1979 | static void |
| 1980 | DEFUN_VOID (lang_check) |
| 1981 | { |
| 1982 | lang_statement_union_type *file; |
| 1983 | bfd *input_bfd; |
| 1984 | unsigned long input_machine; |
| 1985 | enum bfd_architecture input_architecture; |
| 1986 | CONST bfd_arch_info_type *compatible; |
| 1987 | |
| 1988 | for (file = file_chain.head; |
| 1989 | file != (lang_statement_union_type *) NULL; |
| 1990 | file = file->input_statement.next) |
| 1991 | { |
| 1992 | input_bfd = file->input_statement.the_bfd; |
| 1993 | |
| 1994 | input_machine = bfd_get_mach (input_bfd); |
| 1995 | input_architecture = bfd_get_arch (input_bfd); |
| 1996 | |
| 1997 | |
| 1998 | /* Inspect the architecture and ensure we're linking like with |
| 1999 | like */ |
| 2000 | |
| 2001 | compatible = bfd_arch_get_compatible (input_bfd, |
| 2002 | output_bfd); |
| 2003 | |
| 2004 | if (compatible) |
| 2005 | { |
| 2006 | ldfile_output_machine = compatible->mach; |
| 2007 | ldfile_output_architecture = compatible->arch; |
| 2008 | } |
| 2009 | else |
| 2010 | { |
| 2011 | |
| 2012 | info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n", |
| 2013 | bfd_printable_name (input_bfd), input_bfd, |
| 2014 | bfd_printable_name (output_bfd)); |
| 2015 | |
| 2016 | bfd_set_arch_mach (output_bfd, |
| 2017 | input_architecture, |
| 2018 | input_machine); |
| 2019 | } |
| 2020 | |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * run through all the global common symbols and tie them |
| 2026 | * to the output section requested. |
| 2027 | * |
| 2028 | As an experiment we do this 4 times, once for all the byte sizes, |
| 2029 | then all the two bytes, all the four bytes and then everything else |
| 2030 | */ |
| 2031 | |
| 2032 | static void |
| 2033 | DEFUN_VOID (lang_common) |
| 2034 | { |
| 2035 | ldsym_type *lgs; |
| 2036 | size_t power; |
| 2037 | |
| 2038 | if (config.relocateable_output == false || |
| 2039 | command_line.force_common_definition == true) |
| 2040 | { |
| 2041 | for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<= 1) |
| 2042 | { |
| 2043 | for (lgs = symbol_head; |
| 2044 | lgs != (ldsym_type *) NULL; |
| 2045 | lgs = lgs->next) |
| 2046 | { |
| 2047 | asymbol *com; |
| 2048 | unsigned int power_of_two; |
| 2049 | size_t size; |
| 2050 | size_t align; |
| 2051 | |
| 2052 | if (lgs->scoms_chain != (asymbol **) NULL) |
| 2053 | { |
| 2054 | com = *(lgs->scoms_chain); |
| 2055 | size = com->value; |
| 2056 | switch (size) |
| 2057 | { |
| 2058 | case 0: |
| 2059 | case 1: |
| 2060 | align = 1; |
| 2061 | power_of_two = 0; |
| 2062 | break; |
| 2063 | case 2: |
| 2064 | power_of_two = 1; |
| 2065 | align = 2; |
| 2066 | break; |
| 2067 | case 3: |
| 2068 | case 4: |
| 2069 | power_of_two = 2; |
| 2070 | align = 4; |
| 2071 | break; |
| 2072 | case 5: |
| 2073 | case 6: |
| 2074 | case 7: |
| 2075 | case 8: |
| 2076 | power_of_two = 3; |
| 2077 | align = 8; |
| 2078 | break; |
| 2079 | default: |
| 2080 | power_of_two = 4; |
| 2081 | align = 16; |
| 2082 | break; |
| 2083 | } |
| 2084 | if (config.sort_common == false || align == power) |
| 2085 | { |
| 2086 | bfd *symbfd; |
| 2087 | |
| 2088 | /* Change from a common symbol into a definition of |
| 2089 | a symbol */ |
| 2090 | lgs->sdefs_chain = lgs->scoms_chain; |
| 2091 | lgs->scoms_chain = (asymbol **) NULL; |
| 2092 | commons_pending--; |
| 2093 | |
| 2094 | /* Point to the correct common section */ |
| 2095 | symbfd = bfd_asymbol_bfd (com); |
| 2096 | if (com->section == &bfd_com_section) |
| 2097 | com->section = |
| 2098 | ((lang_input_statement_type *) symbfd->usrdata) |
| 2099 | ->common_section; |
| 2100 | else |
| 2101 | { |
| 2102 | CONST char *name; |
| 2103 | asection *newsec; |
| 2104 | |
| 2105 | name = bfd_get_section_name (symbfd, |
| 2106 | com->section); |
| 2107 | newsec = bfd_get_section_by_name (symbfd, |
| 2108 | name); |
| 2109 | /* BFD backend must provide this section. */ |
| 2110 | if (newsec == (asection *) NULL) |
| 2111 | einfo ("%P%F: No output section %s", name); |
| 2112 | com->section = newsec; |
| 2113 | } |
| 2114 | |
| 2115 | /* Fix the size of the common section */ |
| 2116 | |
| 2117 | com->section->_raw_size = |
| 2118 | ALIGN (com->section->_raw_size, align); |
| 2119 | |
| 2120 | /* Remember if this is the biggest alignment ever seen */ |
| 2121 | if (power_of_two > com->section->alignment_power) |
| 2122 | { |
| 2123 | com->section->alignment_power = power_of_two; |
| 2124 | } |
| 2125 | |
| 2126 | /* Symbol stops being common and starts being global, but |
| 2127 | we remember that it was common once. */ |
| 2128 | |
| 2129 | com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON; |
| 2130 | com->value = com->section->_raw_size; |
| 2131 | |
| 2132 | if (write_map && config.map_file) |
| 2133 | { |
| 2134 | fprintf (config.map_file, "Allocating common %s: %x at %x %s\n", |
| 2135 | lgs->name, |
| 2136 | (unsigned) size, |
| 2137 | (unsigned) com->value, |
| 2138 | bfd_asymbol_bfd(com)->filename); |
| 2139 | } |
| 2140 | |
| 2141 | com->section->_raw_size += size; |
| 2142 | |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | run through the input files and ensure that every input |
| 2155 | section has somewhere to go. If one is found without |
| 2156 | a destination then create an input request and place it |
| 2157 | into the statement tree. |
| 2158 | */ |
| 2159 | |
| 2160 | static void |
| 2161 | DEFUN_VOID (lang_place_orphans) |
| 2162 | { |
| 2163 | lang_input_statement_type *file; |
| 2164 | |
| 2165 | for (file = (lang_input_statement_type *) file_chain.head; |
| 2166 | file != (lang_input_statement_type *) NULL; |
| 2167 | file = (lang_input_statement_type *) file->next) |
| 2168 | { |
| 2169 | asection *s; |
| 2170 | |
| 2171 | for (s = file->the_bfd->sections; |
| 2172 | s != (asection *) NULL; |
| 2173 | s = s->next) |
| 2174 | { |
| 2175 | if (s->output_section == (asection *) NULL) |
| 2176 | { |
| 2177 | /* This section of the file is not attatched, root |
| 2178 | around for a sensible place for it to go */ |
| 2179 | |
| 2180 | if (file->common_section == s) |
| 2181 | { |
| 2182 | /* This is a lonely common section which must |
| 2183 | have come from an archive. We attatch to the |
| 2184 | section with the wildcard */ |
| 2185 | if (config.relocateable_output != true |
| 2186 | && command_line.force_common_definition == false) |
| 2187 | { |
| 2188 | if (default_common_section == |
| 2189 | (lang_output_section_statement_type *) NULL) |
| 2190 | { |
| 2191 | info ("%P: No [COMMON] command, defaulting to .bss\n"); |
| 2192 | |
| 2193 | default_common_section = |
| 2194 | lang_output_section_statement_lookup (".bss"); |
| 2195 | |
| 2196 | } |
| 2197 | wild_doit (&default_common_section->children, s, |
| 2198 | default_common_section, file); |
| 2199 | } |
| 2200 | } |
| 2201 | else |
| 2202 | { |
| 2203 | lang_output_section_statement_type *os = |
| 2204 | lang_output_section_statement_lookup (s->name); |
| 2205 | |
| 2206 | wild_doit (&os->children, s, os, file); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | |
| 2214 | void |
| 2215 | DEFUN (lang_set_flags, (ptr, flags), |
| 2216 | int *ptr AND |
| 2217 | CONST char *flags) |
| 2218 | { |
| 2219 | boolean state = false; |
| 2220 | |
| 2221 | *ptr = 0; |
| 2222 | while (*flags) |
| 2223 | { |
| 2224 | if (*flags == '!') |
| 2225 | { |
| 2226 | state = false; |
| 2227 | flags++; |
| 2228 | } |
| 2229 | else |
| 2230 | state = true; |
| 2231 | switch (*flags) |
| 2232 | { |
| 2233 | case 'R': |
| 2234 | /* ptr->flag_read = state; */ |
| 2235 | break; |
| 2236 | case 'W': |
| 2237 | /* ptr->flag_write = state; */ |
| 2238 | break; |
| 2239 | case 'X': |
| 2240 | /* ptr->flag_executable= state;*/ |
| 2241 | break; |
| 2242 | case 'L': |
| 2243 | case 'I': |
| 2244 | /* ptr->flag_loadable= state;*/ |
| 2245 | break; |
| 2246 | default: |
| 2247 | einfo ("%P%F illegal syntax in flags\n"); |
| 2248 | break; |
| 2249 | } |
| 2250 | flags++; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | |
| 2255 | |
| 2256 | void |
| 2257 | DEFUN (lang_for_each_file, (func), |
| 2258 | void (*func) PARAMS ((lang_input_statement_type *))) |
| 2259 | { |
| 2260 | lang_input_statement_type *f; |
| 2261 | |
| 2262 | for (f = (lang_input_statement_type *) file_chain.head; |
| 2263 | f != (lang_input_statement_type *) NULL; |
| 2264 | f = (lang_input_statement_type *) f->next) |
| 2265 | { |
| 2266 | func (f); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | void |
| 2272 | DEFUN (lang_for_each_input_section, (func), |
| 2273 | void (*func) PARAMS ((bfd * ab, asection * as))) |
| 2274 | { |
| 2275 | lang_input_statement_type *f; |
| 2276 | |
| 2277 | for (f = (lang_input_statement_type *) file_chain.head; |
| 2278 | f != (lang_input_statement_type *) NULL; |
| 2279 | f = (lang_input_statement_type *) f->next) |
| 2280 | { |
| 2281 | asection *s; |
| 2282 | |
| 2283 | for (s = f->the_bfd->sections; |
| 2284 | s != (asection *) NULL; |
| 2285 | s = s->next) |
| 2286 | { |
| 2287 | func (f->the_bfd, s); |
| 2288 | } |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | |
| 2294 | void |
| 2295 | DEFUN (ldlang_add_file, (entry), |
| 2296 | lang_input_statement_type * entry) |
| 2297 | { |
| 2298 | |
| 2299 | lang_statement_append (&file_chain, |
| 2300 | (lang_statement_union_type *) entry, |
| 2301 | &entry->next); |
| 2302 | } |
| 2303 | |
| 2304 | void |
| 2305 | DEFUN (lang_add_output, (name), |
| 2306 | CONST char *name) |
| 2307 | { |
| 2308 | lang_output_statement_type *new = new_stat (lang_output_statement, |
| 2309 | stat_ptr); |
| 2310 | |
| 2311 | new->name = name; |
| 2312 | had_output_filename = true; |
| 2313 | } |
| 2314 | |
| 2315 | |
| 2316 | static lang_output_section_statement_type *current_section; |
| 2317 | |
| 2318 | static int topower(x) |
| 2319 | int x; |
| 2320 | { |
| 2321 | unsigned int i = 1; |
| 2322 | int l; |
| 2323 | if (x < 0) return -1; |
| 2324 | for (l = 0; l < 32; l++) |
| 2325 | { |
| 2326 | if (i >= x) return l; |
| 2327 | i<<=1; |
| 2328 | } |
| 2329 | return 0; |
| 2330 | } |
| 2331 | void |
| 2332 | DEFUN (lang_enter_output_section_statement, |
| 2333 | (output_section_statement_name, |
| 2334 | address_exp, |
| 2335 | flags, |
| 2336 | block_value, |
| 2337 | align, subalign, base), |
| 2338 | char *output_section_statement_name AND |
| 2339 | etree_type * address_exp AND |
| 2340 | int flags AND |
| 2341 | bfd_vma block_value AND |
| 2342 | etree_type *align AND |
| 2343 | etree_type *subalign AND |
| 2344 | etree_type *base) |
| 2345 | { |
| 2346 | lang_output_section_statement_type *os; |
| 2347 | |
| 2348 | current_section = |
| 2349 | os = |
| 2350 | lang_output_section_statement_lookup (output_section_statement_name); |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | /* Add this statement to tree */ |
| 2355 | /* add_statement(lang_output_section_statement_enum, |
| 2356 | output_section_statement);*/ |
| 2357 | /* Make next things chain into subchain of this */ |
| 2358 | |
| 2359 | if (os->addr_tree == |
| 2360 | (etree_type *) NULL) |
| 2361 | { |
| 2362 | os->addr_tree = |
| 2363 | address_exp; |
| 2364 | } |
| 2365 | os->flags = flags; |
| 2366 | if (flags & SEC_NEVER_LOAD) |
| 2367 | os->loadable = 0; |
| 2368 | else |
| 2369 | os->loadable = 1; |
| 2370 | os->block_value = block_value ? block_value : 1; |
| 2371 | stat_ptr = &os->children; |
| 2372 | |
| 2373 | os->subsection_alignment = topower( |
| 2374 | exp_get_value_int(subalign, -1, |
| 2375 | "subsection alignment", |
| 2376 | 0)); |
| 2377 | os->section_alignment = topower( |
| 2378 | exp_get_value_int(align, -1, |
| 2379 | "section alignment", 0)); |
| 2380 | |
| 2381 | os->load_base = base; |
| 2382 | } |
| 2383 | |
| 2384 | |
| 2385 | void |
| 2386 | DEFUN_VOID (lang_final) |
| 2387 | { |
| 2388 | if (had_output_filename == false) |
| 2389 | { |
| 2390 | extern CONST char *output_filename; |
| 2391 | |
| 2392 | lang_add_output (output_filename); |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /* Reset the current counters in the regions */ |
| 2397 | static void |
| 2398 | DEFUN_VOID (reset_memory_regions) |
| 2399 | { |
| 2400 | lang_memory_region_type *p = lang_memory_region_list; |
| 2401 | |
| 2402 | for (p = lang_memory_region_list; |
| 2403 | p != (lang_memory_region_type *) NULL; |
| 2404 | p = p->next) |
| 2405 | { |
| 2406 | p->old_length = p->current - p->origin; |
| 2407 | p->current = p->origin; |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | |
| 2413 | asymbol * |
| 2414 | DEFUN (create_symbol, (name, flags, section), |
| 2415 | CONST char *name AND |
| 2416 | flagword flags AND |
| 2417 | asection * section) |
| 2418 | { |
| 2419 | extern lang_input_statement_type *script_file; |
| 2420 | asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **))); |
| 2421 | |
| 2422 | /* Add this definition to script file */ |
| 2423 | asymbol *def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd); |
| 2424 | def->name = buystring (name); |
| 2425 | def->udata = 0; |
| 2426 | def->flags = flags; |
| 2427 | def->section = section; |
| 2428 | *def_ptr = def; |
| 2429 | Q_enter_global_ref (def_ptr, name); |
| 2430 | return def; |
| 2431 | } |
| 2432 | |
| 2433 | void |
| 2434 | DEFUN_VOID (lang_process) |
| 2435 | { |
| 2436 | |
| 2437 | if (had_script == false) |
| 2438 | { |
| 2439 | parse_line (ldemul_get_script (), 1); |
| 2440 | } |
| 2441 | lang_reasonable_defaults (); |
| 2442 | current_target = default_target; |
| 2443 | |
| 2444 | lang_for_each_statement (ldlang_open_output); /* Open the output file */ |
| 2445 | /* For each output section statement, create a section in the output |
| 2446 | file */ |
| 2447 | lang_create_output_section_statements (); |
| 2448 | |
| 2449 | /* Create a dummy bfd for the script */ |
| 2450 | lang_init_script_file (); |
| 2451 | |
| 2452 | /* Add to the hash table all undefineds on the command line */ |
| 2453 | lang_place_undefineds (); |
| 2454 | |
| 2455 | /* Create a bfd for each input file */ |
| 2456 | current_target = default_target; |
| 2457 | lang_for_each_statement (open_input_bfds); |
| 2458 | |
| 2459 | /* Run through the contours of the script and attatch input sections |
| 2460 | to the correct output sections |
| 2461 | */ |
| 2462 | find_constructors (); |
| 2463 | map_input_to_output_sections (statement_list.head, (char *) NULL, |
| 2464 | (lang_output_section_statement_type *) NULL); |
| 2465 | |
| 2466 | |
| 2467 | /* Find any sections not attatched explicitly and handle them */ |
| 2468 | lang_place_orphans (); |
| 2469 | |
| 2470 | /* Size up the common data */ |
| 2471 | lang_common (); |
| 2472 | |
| 2473 | ldemul_before_allocation (); |
| 2474 | |
| 2475 | |
| 2476 | #if 0 |
| 2477 | had_relax = true; |
| 2478 | while (had_relax) |
| 2479 | { |
| 2480 | |
| 2481 | had_relax = false; |
| 2482 | |
| 2483 | lang_size_sections (statement_list.head, |
| 2484 | (lang_output_section_statement_type *) NULL, |
| 2485 | &(statement_list.head), 0, (bfd_vma) 0, true); |
| 2486 | /* FIXME. Until the code in relax is fixed so that it only reads in |
| 2487 | stuff once, we cant iterate since there is no way for the linker to |
| 2488 | know what has been patched and what hasn't */ |
| 2489 | break; |
| 2490 | |
| 2491 | } |
| 2492 | #endif |
| 2493 | |
| 2494 | /* Now run around and relax if we can */ |
| 2495 | if (command_line.relax) |
| 2496 | { |
| 2497 | /* First time round is a trial run to get the 'worst case' addresses of the |
| 2498 | objects if there was no relaxing */ |
| 2499 | lang_size_sections (statement_list.head, |
| 2500 | (lang_output_section_statement_type *) NULL, |
| 2501 | &(statement_list.head), 0, (bfd_vma) 0, false); |
| 2502 | |
| 2503 | |
| 2504 | |
| 2505 | /* Move the global symbols around so the second pass of relaxing can |
| 2506 | see them */ |
| 2507 | lang_relocate_globals (); |
| 2508 | |
| 2509 | reset_memory_regions (); |
| 2510 | |
| 2511 | /* Do all the assignments, now that we know the final restingplaces |
| 2512 | of all the symbols */ |
| 2513 | |
| 2514 | lang_do_assignments (statement_list.head, |
| 2515 | abs_output_section, |
| 2516 | 0, (bfd_vma) 0); |
| 2517 | |
| 2518 | |
| 2519 | /* Perform another relax pass - this time we know where the |
| 2520 | globals are, so can make better guess */ |
| 2521 | lang_size_sections (statement_list.head, |
| 2522 | (lang_output_section_statement_type *) NULL, |
| 2523 | &(statement_list.head), 0, (bfd_vma) 0, true); |
| 2524 | |
| 2525 | |
| 2526 | |
| 2527 | } |
| 2528 | |
| 2529 | else |
| 2530 | { |
| 2531 | /* Size up the sections */ |
| 2532 | lang_size_sections (statement_list.head, |
| 2533 | abs_output_section, |
| 2534 | &(statement_list.head), 0, (bfd_vma) 0, false); |
| 2535 | |
| 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | /* See if anything special should be done now we know how big |
| 2540 | everything is */ |
| 2541 | ldemul_after_allocation (); |
| 2542 | |
| 2543 | /* Do all the assignments, now that we know the final restingplaces |
| 2544 | of all the symbols */ |
| 2545 | |
| 2546 | lang_do_assignments (statement_list.head, |
| 2547 | abs_output_section, |
| 2548 | 0, (bfd_vma) 0); |
| 2549 | |
| 2550 | |
| 2551 | /* Move the global symbols around */ |
| 2552 | lang_relocate_globals (); |
| 2553 | |
| 2554 | /* Make sure that we're not mixing architectures */ |
| 2555 | |
| 2556 | lang_check (); |
| 2557 | |
| 2558 | /* Final stuffs */ |
| 2559 | lang_finish (); |
| 2560 | } |
| 2561 | |
| 2562 | /* EXPORTED TO YACC */ |
| 2563 | |
| 2564 | void |
| 2565 | DEFUN (lang_add_wild, (section_name, filename), |
| 2566 | CONST char *CONST section_name AND |
| 2567 | CONST char *CONST filename) |
| 2568 | { |
| 2569 | lang_wild_statement_type *new = new_stat (lang_wild_statement, |
| 2570 | stat_ptr); |
| 2571 | |
| 2572 | if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0) |
| 2573 | { |
| 2574 | placed_commons = true; |
| 2575 | } |
| 2576 | if (filename != (char *) NULL) |
| 2577 | { |
| 2578 | lang_has_input_file = true; |
| 2579 | } |
| 2580 | new->section_name = section_name; |
| 2581 | new->filename = filename; |
| 2582 | lang_list_init (&new->children); |
| 2583 | } |
| 2584 | |
| 2585 | void |
| 2586 | DEFUN (lang_section_start, (name, address), |
| 2587 | CONST char *name AND |
| 2588 | etree_type * address) |
| 2589 | { |
| 2590 | lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr); |
| 2591 | |
| 2592 | ad->section_name = name; |
| 2593 | ad->address = address; |
| 2594 | } |
| 2595 | |
| 2596 | void |
| 2597 | DEFUN (lang_add_entry, (name), |
| 2598 | CONST char *name) |
| 2599 | { |
| 2600 | entry_symbol = name; |
| 2601 | } |
| 2602 | |
| 2603 | void |
| 2604 | DEFUN (lang_add_target, (name), |
| 2605 | CONST char *name) |
| 2606 | { |
| 2607 | lang_target_statement_type *new = new_stat (lang_target_statement, |
| 2608 | stat_ptr); |
| 2609 | |
| 2610 | new->target = name; |
| 2611 | |
| 2612 | } |
| 2613 | |
| 2614 | void |
| 2615 | DEFUN (lang_add_map, (name), |
| 2616 | CONST char *name) |
| 2617 | { |
| 2618 | while (*name) |
| 2619 | { |
| 2620 | switch (*name) |
| 2621 | { |
| 2622 | case 'F': |
| 2623 | map_option_f = true; |
| 2624 | break; |
| 2625 | } |
| 2626 | name++; |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | void |
| 2631 | DEFUN (lang_add_fill, (exp), |
| 2632 | int exp) |
| 2633 | { |
| 2634 | lang_fill_statement_type *new = new_stat (lang_fill_statement, |
| 2635 | stat_ptr); |
| 2636 | |
| 2637 | new->fill = exp; |
| 2638 | } |
| 2639 | |
| 2640 | void |
| 2641 | DEFUN (lang_add_data, (type, exp), |
| 2642 | int type AND |
| 2643 | union etree_union *exp) |
| 2644 | { |
| 2645 | |
| 2646 | lang_data_statement_type *new = new_stat (lang_data_statement, |
| 2647 | stat_ptr); |
| 2648 | |
| 2649 | new->exp = exp; |
| 2650 | new->type = type; |
| 2651 | |
| 2652 | } |
| 2653 | |
| 2654 | void |
| 2655 | DEFUN (lang_add_assignment, (exp), |
| 2656 | etree_type * exp) |
| 2657 | { |
| 2658 | lang_assignment_statement_type *new = new_stat (lang_assignment_statement, |
| 2659 | stat_ptr); |
| 2660 | |
| 2661 | new->exp = exp; |
| 2662 | } |
| 2663 | |
| 2664 | void |
| 2665 | DEFUN (lang_add_attribute, (attribute), |
| 2666 | enum statement_enum attribute) |
| 2667 | { |
| 2668 | new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); |
| 2669 | } |
| 2670 | |
| 2671 | void |
| 2672 | DEFUN (lang_startup, (name), |
| 2673 | CONST char *name) |
| 2674 | { |
| 2675 | if (startup_file != (char *) NULL) |
| 2676 | { |
| 2677 | einfo ("%P%FMultiple STARTUP files\n"); |
| 2678 | } |
| 2679 | first_file->filename = name; |
| 2680 | first_file->local_sym_name = name; |
| 2681 | |
| 2682 | startup_file = name; |
| 2683 | } |
| 2684 | |
| 2685 | void |
| 2686 | DEFUN (lang_float, (maybe), |
| 2687 | boolean maybe) |
| 2688 | { |
| 2689 | lang_float_flag = maybe; |
| 2690 | } |
| 2691 | |
| 2692 | void |
| 2693 | DEFUN (lang_leave_output_section_statement, (fill, memspec), |
| 2694 | bfd_vma fill AND |
| 2695 | CONST char *memspec) |
| 2696 | { |
| 2697 | current_section->fill = fill; |
| 2698 | current_section->region = lang_memory_region_lookup (memspec); |
| 2699 | stat_ptr = &statement_list; |
| 2700 | |
| 2701 | /* We remember if we are closing a .data section, since we use it to |
| 2702 | store constructors in */ |
| 2703 | if (strcmp (current_section->name, ".data") == 0) |
| 2704 | { |
| 2705 | end_of_data_section_statement_list = statement_list; |
| 2706 | |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | /* |
| 2711 | Create an absolute symbol with the given name with the value of the |
| 2712 | address of first byte of the section named. |
| 2713 | |
| 2714 | If the symbol already exists, then do nothing. |
| 2715 | */ |
| 2716 | void |
| 2717 | DEFUN (lang_abs_symbol_at_beginning_of, (section, name), |
| 2718 | CONST char *section AND |
| 2719 | CONST char *name) |
| 2720 | { |
| 2721 | if (ldsym_undefined (name)) |
| 2722 | { |
| 2723 | asection *s = bfd_get_section_by_name (output_bfd, section); |
| 2724 | asymbol *def = create_symbol (name, |
| 2725 | BSF_GLOBAL | BSF_EXPORT, |
| 2726 | &bfd_abs_section); |
| 2727 | |
| 2728 | if (s != (asection *) NULL) |
| 2729 | { |
| 2730 | def->value = s->vma; |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | def->value = 0; |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | /* |
| 2740 | Create an absolute symbol with the given name with the value of the |
| 2741 | address of the first byte after the end of the section named. |
| 2742 | |
| 2743 | If the symbol already exists, then do nothing. |
| 2744 | */ |
| 2745 | void |
| 2746 | DEFUN (lang_abs_symbol_at_end_of, (section, name), |
| 2747 | CONST char *section AND |
| 2748 | CONST char *name) |
| 2749 | { |
| 2750 | if (ldsym_undefined (name)) |
| 2751 | { |
| 2752 | asection *s = bfd_get_section_by_name (output_bfd, section); |
| 2753 | |
| 2754 | /* Add a symbol called _end */ |
| 2755 | asymbol *def = create_symbol (name, |
| 2756 | BSF_GLOBAL | BSF_EXPORT, |
| 2757 | &bfd_abs_section); |
| 2758 | |
| 2759 | if (s != (asection *) NULL) |
| 2760 | { |
| 2761 | def->value = s->vma + s->_raw_size; |
| 2762 | } |
| 2763 | else |
| 2764 | { |
| 2765 | def->value = 0; |
| 2766 | } |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | void |
| 2771 | DEFUN (lang_statement_append, (list, element, field), |
| 2772 | lang_statement_list_type * list AND |
| 2773 | lang_statement_union_type * element AND |
| 2774 | lang_statement_union_type ** field) |
| 2775 | { |
| 2776 | *(list->tail) = element; |
| 2777 | list->tail = field; |
| 2778 | } |
| 2779 | |
| 2780 | /* Set the output format type */ |
| 2781 | void |
| 2782 | DEFUN (lang_add_output_format, (format), |
| 2783 | CONST char *format) |
| 2784 | { |
| 2785 | output_target = format; |
| 2786 | } |
| 2787 | |