ldlang.c revision 60484
1221828Sgrehan/* Linker command language support. 2221828Sgrehan Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 3221828Sgrehan Free Software Foundation, Inc. 4221828Sgrehan 5221828SgrehanThis file is part of GLD, the Gnu Linker. 6221828Sgrehan 7221828SgrehanGLD is free software; you can redistribute it and/or modify 8221828Sgrehanit under the terms of the GNU General Public License as published by 9221828Sgrehanthe Free Software Foundation; either version 2, or (at your option) 10221828Sgrehanany later version. 11221828Sgrehan 12221828SgrehanGLD is distributed in the hope that it will be useful, 13221828Sgrehanbut WITHOUT ANY WARRANTY; without even the implied warranty of 14221828SgrehanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15221828SgrehanGNU General Public License for more details. 16221828Sgrehan 17221828SgrehanYou should have received a copy of the GNU General Public License 18221828Sgrehanalong with GLD; see the file COPYING. If not, write to the Free 19221828SgrehanSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 20221828Sgrehan02111-1307, USA. */ 21221828Sgrehan 22221828Sgrehan#include "bfd.h" 23221828Sgrehan#include "sysdep.h" 24221828Sgrehan#include "libiberty.h" 25221828Sgrehan#include "obstack.h" 26221828Sgrehan#include "bfdlink.h" 27221828Sgrehan 28221828Sgrehan#include "ld.h" 29221828Sgrehan#include "ldmain.h" 30221828Sgrehan#include "ldgram.h" 31221828Sgrehan#include "ldexp.h" 32221828Sgrehan#include "ldlang.h" 33234695Sgrehan#include "ldemul.h" 34221828Sgrehan#include "ldlex.h" 35221828Sgrehan#include "ldmisc.h" 36221828Sgrehan#include "ldctor.h" 37221828Sgrehan#include "ldfile.h" 38221828Sgrehan#include "fnmatch.h" 39221828Sgrehan#include "demangle.h" 40221828Sgrehan 41221828Sgrehan#include <ctype.h> 42256072Sneel 43221828Sgrehan/* FORWARDS */ 44221828Sgrehanstatic lang_statement_union_type *new_statement PARAMS ((enum statement_enum, 45221828Sgrehan size_t, 46221828Sgrehan lang_statement_list_type*)); 47221828Sgrehan 48256072Sneel 49256072Sneel/* LOCALS */ 50256072Sneelstatic struct obstack stat_obstack; 51256072Sneel 52256072Sneel#define obstack_chunk_alloc xmalloc 53256072Sneel#define obstack_chunk_free free 54221828Sgrehanstatic CONST char *startup_file; 55261275Sjhbstatic lang_statement_list_type input_file_chain; 56221828Sgrehanstatic boolean placed_commons = false; 57221828Sgrehanstatic lang_output_section_statement_type *default_common_section; 58241489Sneelstatic boolean map_option_f; 59262350Sjhbstatic bfd_vma print_dot; 60221914Sjhbstatic lang_input_statement_type *first_file; 61256072Sneelstatic lang_statement_list_type lang_output_section_statement; 62221828Sgrehanstatic CONST char *current_target; 63221828Sgrehanstatic CONST char *output_target; 64261088Sjhbstatic lang_statement_list_type statement_list; 65268976Sjhbstatic struct lang_phdr *lang_phdr_list; 66261088Sjhb 67268976Sjhbstatic void lang_for_each_statement_worker 68256072Sneel PARAMS ((void (*func) (lang_statement_union_type *), 69242275Sneel lang_statement_union_type *s)); 70221828Sgrehanstatic lang_input_statement_type *new_afile 71221828Sgrehan PARAMS ((const char *name, lang_input_file_enum_type file_type, 72268891Sjhb const char *target, boolean add_to_list)); 73268891Sjhbstatic void init_os PARAMS ((lang_output_section_statement_type *s)); 74261088Sjhbstatic void exp_init_os PARAMS ((etree_type *)); 75261088Sjhbstatic void section_already_linked PARAMS ((bfd *, asection *, PTR)); 76221828Sgrehanstatic struct bfd_hash_entry *already_linked_newfunc 77276429Sneel PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, 78284894Sneel const char *string)); 79221828Sgrehanstatic void already_linked_table_init PARAMS ((void)); 80221828Sgrehanstatic void already_linked_table_free PARAMS ((void)); 81242065Sneelstatic boolean wildcardp PARAMS ((const char *)); 82221828Sgrehanstatic lang_statement_union_type *wild_sort 83221828Sgrehan PARAMS ((lang_wild_statement_type *, lang_input_statement_type *, 84221828Sgrehan asection *)); 85221828Sgrehanstatic lang_input_statement_type *lookup_name PARAMS ((const char *name)); 86221828Sgrehanstatic void load_symbols PARAMS ((lang_input_statement_type *entry, 87221828Sgrehan lang_statement_list_type *)); 88270071Sgrehanstatic void wild PARAMS ((lang_wild_statement_type *s, 89270071Sgrehan const char *section, const char *file, 90270071Sgrehan const char *target, 91270071Sgrehan lang_output_section_statement_type *output)); 92270071Sgrehanstatic bfd *open_output PARAMS ((const char *name)); 93270071Sgrehanstatic void ldlang_open_output PARAMS ((lang_statement_union_type *statement)); 94270071Sgrehanstatic void open_input_bfds 95221828Sgrehan PARAMS ((lang_statement_union_type *statement, boolean)); 96270071Sgrehanstatic void lang_reasonable_defaults PARAMS ((void)); 97270071Sgrehanstatic void lang_place_undefineds PARAMS ((void)); 98270071Sgrehanstatic void map_input_to_output_sections 99284900Sneel PARAMS ((lang_statement_union_type *s, 100270071Sgrehan const char *target, 101270071Sgrehan lang_output_section_statement_type *output_section_statement)); 102270159Sgrehanstatic void print_output_section_statement 103270071Sgrehan PARAMS ((lang_output_section_statement_type *output_section_statement)); 104270071Sgrehanstatic void print_assignment 105270071Sgrehan PARAMS ((lang_assignment_statement_type *assignment, 106284894Sneel lang_output_section_statement_type *output_section)); 107284894Sneelstatic void print_input_statement PARAMS ((lang_input_statement_type *statm)); 108284894Sneelstatic boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR)); 109270071Sgrehanstatic void print_input_section PARAMS ((lang_input_section_type *in)); 110270071Sgrehanstatic void print_fill_statement PARAMS ((lang_fill_statement_type *fill)); 111270071Sgrehanstatic void print_data_statement PARAMS ((lang_data_statement_type *data)); 112270071Sgrehanstatic void print_address_statement PARAMS ((lang_address_statement_type *)); 113284894Sneelstatic void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc)); 114221828Sgrehanstatic void print_padding_statement PARAMS ((lang_padding_statement_type *s)); 115221828Sgrehanstatic void print_wild_statement 116270071Sgrehan PARAMS ((lang_wild_statement_type *w, 117242065Sneel lang_output_section_statement_type *os)); 118242065Sneelstatic void print_group 119242065Sneel PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *)); 120256072Sneelstatic void print_statement PARAMS ((lang_statement_union_type *s, 121241489Sneel lang_output_section_statement_type *os)); 122256072Sneelstatic void print_statement_list PARAMS ((lang_statement_union_type *s, 123256072Sneel lang_output_section_statement_type *os)); 124256072Sneelstatic void print_statements PARAMS ((void)); 125256072Sneelstatic bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr, 126256072Sneel fill_type fill, unsigned int power, 127256072Sneel asection *output_section_statement, 128221828Sgrehan bfd_vma dot)); 129221828Sgrehanstatic bfd_vma size_input_section 130270071Sgrehan PARAMS ((lang_statement_union_type **this_ptr, 131270071Sgrehan lang_output_section_statement_type *output_section_statement, 132270071Sgrehan fill_type fill, bfd_vma dot, boolean relax)); 133270071Sgrehanstatic void lang_finish PARAMS ((void)); 134270071Sgrehanstatic void ignore_bfd_errors PARAMS ((const char *, ...)); 135270071Sgrehanstatic void lang_check PARAMS ((void)); 136221828Sgrehanstatic void lang_common PARAMS ((void)); 137270071Sgrehanstatic boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR)); 138270071Sgrehanstatic void lang_place_orphans PARAMS ((void)); 139270071Sgrehanstatic int topower PARAMS ((int)); 140270071Sgrehanstatic void lang_set_startof PARAMS ((void)); 141270071Sgrehanstatic void reset_memory_regions PARAMS ((void)); 142270071Sgrehanstatic void lang_record_phdrs PARAMS ((void)); 143276429Sneelstatic void lang_gc_wild 144284894Sneel PARAMS ((lang_wild_statement_type *, const char *, const char *)); 145270071Sgrehanstatic void lang_gc_sections_1 PARAMS ((lang_statement_union_type *)); 146270071Sgrehanstatic void lang_gc_sections PARAMS ((void)); 147270071Sgrehanstatic void lang_do_version_exports_section PARAMS ((void)); 148270071Sgrehanstatic void lang_check_section_addresses PARAMS ((void)); 149270071Sgrehan 150270071Sgrehantypedef void (*callback_t) PARAMS ((lang_wild_statement_type *, 151270071Sgrehan asection *, lang_input_statement_type *, 152270071Sgrehan void *)); 153270071Sgrehanstatic void walk_wild_section 154270071Sgrehan PARAMS ((lang_wild_statement_type *, const char *, 155256072Sneel lang_input_statement_type *, callback_t, void *)); 156270071Sgrehanstatic void walk_wild_file 157270071Sgrehan PARAMS ((lang_wild_statement_type *, const char *, 158270071Sgrehan lang_input_statement_type *, callback_t, void *)); 159221828Sgrehan 160221828Sgrehanstatic int get_target PARAMS ((const bfd_target *, void *)); 161249396Sneelstatic void stricpy PARAMS ((char *, char *)); 162249396Sneelstatic void strcut PARAMS ((char *, char *)); 163221828Sgrehanstatic int name_compare PARAMS ((char *, char *)); 164266339Sjhbstatic int closest_target_match PARAMS ((const bfd_target *, void *)); 165221828Sgrehanstatic char * get_first_input_target PARAMS ((void)); 166261275Sjhb 167221828Sgrehan/* EXPORTS */ 168256072Sneellang_output_section_statement_type *abs_output_section; 169284900Sneellang_statement_list_type *stat_ptr = &statement_list; 170284900Sneellang_statement_list_type file_chain = { NULL, NULL }; 171221828Sgrehanconst char *entry_symbol = NULL; 172256072Sneelboolean entry_from_cmdline; 173256072Sneelboolean lang_has_input_file = false; 174256072Sneelboolean had_output_filename = false; 175256072Sneelboolean lang_float_flag = false; 176221828Sgrehanboolean delete_output_file_on_failure = false; 177221828Sgrehanstruct lang_nocrossrefs *nocrossref_list; 178221828Sgrehan 179221828Sgrehanetree_type *base; /* Relocation base - or null */ 180221828Sgrehan 181221828Sgrehan 182221828Sgrehan#if defined(__STDC__) || defined(ALMOST_STDC) 183221828Sgrehan#define cat(a,b) a##b 184221828Sgrehan#else 185221828Sgrehan#define cat(a,b) a/**/b 186221828Sgrehan#endif 187221828Sgrehan 188266339Sjhb#define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y) 189266339Sjhb 190266339Sjhb#define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma) 191266339Sjhb 192221828Sgrehan#define outside_symbol_address(q) ((q)->value + outside_section_address(q->section)) 193245021Sneel 194245021Sneel#define SECTION_NAME_MAP_LENGTH (16) 195221828Sgrehan 196221828SgrehanPTR 197221828Sgrehanstat_alloc (size) 198221828Sgrehan size_t size; 199248389Sneel{ 200221828Sgrehan return obstack_alloc (&stat_obstack, size); 201266339Sjhb} 202266339Sjhb 203268935Sjhb/*---------------------------------------------------------------------- 204268935Sjhb Generic traversal routines for finding matching sections. 205268935Sjhb*/ 206268935Sjhb 207268935Sjhbstatic void 208268935Sjhbwalk_wild_section (ptr, section, file, callback, data) 209268935Sjhb lang_wild_statement_type *ptr; 210268935Sjhb const char *section; 211268935Sjhb lang_input_statement_type *file; 212268935Sjhb callback_t callback; 213266339Sjhb void *data; 214266339Sjhb{ 215266339Sjhb /* Don't process sections from files which were excluded. */ 216266339Sjhb if (ptr->exclude_filename_list != NULL) 217276403Sneel { 218276403Sneel struct name_list *list_tmp; 219276403Sneel for (list_tmp = ptr->exclude_filename_list; list_tmp; list_tmp = list_tmp->next) 220276403Sneel { 221276403Sneel boolean match; 222284899Sneel 223284899Sneel if (wildcardp (list_tmp->name)) 224284899Sneel match = fnmatch (list_tmp->name, file->filename, 0) == 0 ? true : false; 225284899Sneel else 226284899Sneel match = strcmp (list_tmp->name, file->filename) == 0 ? true : false; 227284900Sneel 228284900Sneel if (match) 229284900Sneel return; 230284900Sneel } 231284900Sneel } 232284900Sneel 233284900Sneel if (file->just_syms_flag == false) 234284900Sneel { 235284900Sneel register asection *s; 236284900Sneel boolean wildcard; 237284900Sneel 238284900Sneel if (section == NULL) 239284900Sneel wildcard = false; 240284900Sneel else 241284900Sneel wildcard = wildcardp (section); 242284900Sneel 243284900Sneel for (s = file->the_bfd->sections; s != NULL; s = s->next) 244284900Sneel { 245284900Sneel boolean match; 246284900Sneel 247284900Sneel if (section == NULL) 248284900Sneel match = true; 249221828Sgrehan else 250270071Sgrehan { 251221828Sgrehan const char *name; 252266339Sjhb 253266339Sjhb name = bfd_get_section_name (file->the_bfd, s); 254266339Sjhb if (wildcard) 255270071Sgrehan match = fnmatch (section, name, 0) == 0 ? true : false; 256270071Sgrehan else 257270071Sgrehan match = strcmp (section, name) == 0 ? true : false; 258270071Sgrehan } 259221828Sgrehan 260221828Sgrehan if (match) 261221828Sgrehan (*callback) (ptr, s, file, data); 262270071Sgrehan } 263221828Sgrehan } 264221828Sgrehan} 265270071Sgrehan 266270071Sgrehan/* Handle a wild statement for a single file F. */ 267270071Sgrehan 268270071Sgrehanstatic void 269221828Sgrehanwalk_wild_file (s, section, f, callback, data) 270221828Sgrehan lang_wild_statement_type *s; 271270071Sgrehan const char *section; 272270071Sgrehan lang_input_statement_type *f; 273270071Sgrehan callback_t callback; 274270071Sgrehan void *data; 275270071Sgrehan{ 276270071Sgrehan if (f->the_bfd == NULL 277270071Sgrehan || ! bfd_check_format (f->the_bfd, bfd_archive)) 278270071Sgrehan walk_wild_section (s, section, f, callback, data); 279270071Sgrehan else 280270071Sgrehan { 281266339Sjhb bfd *member; 282267447Sjhb 283284900Sneel /* This is an archive file. We must map each member of the 284270159Sgrehan archive separately. */ 285270071Sgrehan member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL); 286270071Sgrehan while (member != NULL) 287270071Sgrehan { 288267427Sjhb /* When lookup_name is called, it will call the add_symbols 289234695Sgrehan entry point for the archive. For each element of the 290270071Sgrehan archive which is included, BFD will call ldlang_add_file, 291221828Sgrehan which will set the usrdata field of the member to the 292221828Sgrehan lang_input_statement. */ 293276403Sneel if (member->usrdata != NULL) 294276403Sneel { 295276403Sneel walk_wild_section (s, section, 296276403Sneel (lang_input_statement_type *) member->usrdata, 297276403Sneel callback, data); 298276403Sneel } 299276403Sneel 300240894Sneel member = bfd_openr_next_archived_file (f->the_bfd, member); 301240894Sneel } 302240894Sneel } 303240894Sneel} 304240894Sneel 305240894Sneelstatic void 306240894Sneelwalk_wild (s, section, file, callback, data) 307240894Sneel lang_wild_statement_type *s; 308240894Sneel const char *section; 309240894Sneel const char *file; 310240894Sneel callback_t callback; 311240894Sneel void *data; 312240894Sneel{ 313261275Sjhb if (file == (char *) NULL) 314261275Sjhb { 315261275Sjhb /* Perform the iteration over all files in the list. */ 316261275Sjhb LANG_FOR_EACH_INPUT_STATEMENT (f) 317261275Sjhb { 318261275Sjhb walk_wild_file (s, section, f, callback, data); 319221828Sgrehan } 320221828Sgrehan } 321221828Sgrehan else if (wildcardp (file)) 322221828Sgrehan { 323221828Sgrehan LANG_FOR_EACH_INPUT_STATEMENT (f) 324242275Sneel { 325221828Sgrehan if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0) 326266339Sjhb walk_wild_file (s, section, f, callback, data); 327266339Sjhb } 328266339Sjhb } 329266339Sjhb else 330221828Sgrehan { 331221828Sgrehan lang_input_statement_type *f; 332221828Sgrehan 333221828Sgrehan /* Perform the iteration over a single file. */ 334221828Sgrehan f = lookup_name (file); 335221828Sgrehan walk_wild_file (s, section, f, callback, data); 336221828Sgrehan } 337221828Sgrehan} 338221828Sgrehan 339221828Sgrehan/*---------------------------------------------------------------------- 340221828Sgrehan lang_for_each_statement walks the parse tree and calls the provided 341261275Sjhb function for each node 342221828Sgrehan*/ 343266339Sjhb 344221828Sgrehanstatic void 345221828Sgrehanlang_for_each_statement_worker (func, s) 346221828Sgrehan void (*func) PARAMS ((lang_statement_union_type *)); 347221828Sgrehan lang_statement_union_type *s; 348221828Sgrehan{ 349221828Sgrehan for (; s != (lang_statement_union_type *) NULL; s = s->next) 350221828Sgrehan { 351221828Sgrehan func (s); 352221828Sgrehan 353221828Sgrehan switch (s->header.type) 354284899Sneel { 355267070Sjhb case lang_constructors_statement_enum: 356221828Sgrehan lang_for_each_statement_worker (func, constructor_list.head); 357249396Sneel break; 358249396Sneel case lang_output_section_statement_enum: 359221828Sgrehan lang_for_each_statement_worker 360221828Sgrehan (func, 361241454Sneel s->output_section_statement.children.head); 362241454Sneel break; 363261275Sjhb case lang_wild_statement_enum: 364241454Sneel lang_for_each_statement_worker 365266339Sjhb (func, 366266339Sjhb s->wild_statement.children.head); 367241454Sneel break; 368253854Sgrehan case lang_group_statement_enum: 369253854Sgrehan lang_for_each_statement_worker (func, 370253854Sgrehan s->group_statement.children.head); 371253854Sgrehan break; 372253854Sgrehan case lang_data_statement_enum: 373253854Sgrehan case lang_reloc_statement_enum: 374241454Sneel case lang_object_symbols_statement_enum: 375221828Sgrehan case lang_output_statement_enum: 376221828Sgrehan case lang_target_statement_enum: 377221828Sgrehan case lang_input_section_enum: 378221828Sgrehan case lang_input_statement_enum: 379221828Sgrehan case lang_assignment_statement_enum: 380221828Sgrehan case lang_padding_statement_enum: 381221828Sgrehan case lang_address_statement_enum: 382221828Sgrehan case lang_fill_statement_enum: 383221828Sgrehan break; 384221828Sgrehan default: 385221828Sgrehan FAIL (); 386221828Sgrehan break; 387221828Sgrehan } 388221828Sgrehan } 389221828Sgrehan} 390245704Sneel 391245704Sneelvoid 392245704Sneellang_for_each_statement (func) 393245704Sneel void (*func) PARAMS ((lang_statement_union_type *)); 394245704Sneel{ 395245704Sneel lang_for_each_statement_worker (func, 396245704Sneel statement_list.head); 397221828Sgrehan} 398245704Sneel 399221828Sgrehan/*----------------------------------------------------------------------*/ 400221828Sgrehanvoid 401270071Sgrehanlang_list_init (list) 402270071Sgrehan lang_statement_list_type *list; 403270071Sgrehan{ 404270071Sgrehan list->head = (lang_statement_union_type *) NULL; 405270071Sgrehan list->tail = &list->head; 406270071Sgrehan} 407270071Sgrehan 408270071Sgrehan/*---------------------------------------------------------------------- 409270071Sgrehan 410270071Sgrehan build a new statement node for the parse tree 411270071Sgrehan 412276429Sneel */ 413284894Sneel 414284894Sneelstatic 415270071Sgrehanlang_statement_union_type * 416270071Sgrehannew_statement (type, size, list) 417270071Sgrehan enum statement_enum type; 418270071Sgrehan size_t size; 419270071Sgrehan lang_statement_list_type * list; 420270071Sgrehan{ 421270071Sgrehan lang_statement_union_type *new = (lang_statement_union_type *) 422270071Sgrehan stat_alloc (size); 423270071Sgrehan 424270071Sgrehan new->header.type = type; 425249396Sneel new->header.next = (lang_statement_union_type *) NULL; 426249396Sneel lang_statement_append (list, new, &new->header.next); 427221828Sgrehan return new; 428221828Sgrehan} 429256072Sneel 430221828Sgrehan/* 431249396Sneel Build a new input file node for the language. There are several ways 432249396Sneel in which we treat an input file, eg, we only look at symbols, or 433249396Sneel prefix it with a -l etc. 434249396Sneel 435249396Sneel We can be supplied with requests for input files more than once; 436249396Sneel they may, for example be split over serveral lines like foo.o(.text) 437249396Sneel foo.o(.data) etc, so when asked for a file we check that we havn't 438221828Sgrehan got it already so we don't duplicate the bfd. 439249396Sneel 440221828Sgrehan */ 441276429Sneelstatic lang_input_statement_type * 442256072Sneelnew_afile (name, file_type, target, add_to_list) 443256072Sneel CONST char *name; 444256072Sneel lang_input_file_enum_type file_type; 445221828Sgrehan CONST char *target; 446221828Sgrehan boolean add_to_list; 447270071Sgrehan{ 448266339Sjhb lang_input_statement_type *p; 449266339Sjhb 450221828Sgrehan if (add_to_list) 451270071Sgrehan p = new_stat (lang_input_statement, stat_ptr); 452221828Sgrehan else 453249396Sneel { 454249396Sneel p = ((lang_input_statement_type *) 455221828Sgrehan stat_alloc (sizeof (lang_input_statement_type))); 456221828Sgrehan p->header.next = NULL; 457241178Sneel } 458256072Sneel 459241178Sneel lang_has_input_file = true; 460241178Sneel p->target = target; 461256072Sneel switch (file_type) 462256072Sneel { 463241362Sneel case lang_input_file_is_symbols_only_enum: 464256072Sneel p->filename = name; 465241178Sneel p->is_archive = false; 466241178Sneel p->real = true; 467270071Sgrehan p->local_sym_name = name; 468270071Sgrehan p->just_syms_flag = true; 469221828Sgrehan p->search_dirs_flag = false; 470221828Sgrehan break; 471221828Sgrehan case lang_input_file_is_fake_enum: 472221828Sgrehan p->filename = name; 473221828Sgrehan p->is_archive = false; 474256072Sneel p->real = false; 475256072Sneel p->local_sym_name = name; 476256072Sneel p->just_syms_flag = false; 477284894Sneel p->search_dirs_flag = false; 478284894Sneel break; 479284894Sneel case lang_input_file_is_l_enum: 480284894Sneel p->is_archive = true; 481276429Sneel p->filename = name; 482268891Sjhb p->real = true; 483261088Sjhb p->local_sym_name = concat ("-l", name, (const char *) NULL); 484268891Sjhb p->just_syms_flag = false; 485261088Sjhb p->search_dirs_flag = true; 486261088Sjhb break; 487270071Sgrehan case lang_input_file_is_marker_enum: 488270071Sgrehan p->filename = name; 489221828Sgrehan p->is_archive = false; 490270071Sgrehan p->real = false; 491241178Sneel p->local_sym_name = name; 492270071Sgrehan p->just_syms_flag = false; 493270071Sgrehan p->search_dirs_flag = true; 494270071Sgrehan break; 495221828Sgrehan case lang_input_file_is_search_file_enum: 496270071Sgrehan p->filename = name; 497221828Sgrehan p->is_archive = false; 498270071Sgrehan p->real = true; 499270071Sgrehan p->local_sym_name = name; 500270071Sgrehan p->just_syms_flag = false; 501270071Sgrehan p->search_dirs_flag = true; 502221828Sgrehan break; 503270071Sgrehan case lang_input_file_is_file_enum: 504270071Sgrehan p->filename = name; 505270071Sgrehan p->is_archive = false; 506270071Sgrehan p->real = true; 507221828Sgrehan p->local_sym_name = name; 508221828Sgrehan p->just_syms_flag = false; 509221828Sgrehan p->search_dirs_flag = false; 510270071Sgrehan break; 511270071Sgrehan default: 512270071Sgrehan FAIL (); 513270071Sgrehan } 514270071Sgrehan p->the_bfd = (bfd *) NULL; 515270071Sgrehan p->asymbols = (asymbol **) NULL; 516270071Sgrehan p->next_real_file = (lang_statement_union_type *) NULL; 517270071Sgrehan p->next = (lang_statement_union_type *) NULL; 518270071Sgrehan p->symbol_count = 0; 519270071Sgrehan p->dynamic = config.dynamic_link; 520270071Sgrehan p->whole_archive = whole_archive; 521270071Sgrehan p->loaded = false; 522270071Sgrehan lang_statement_append (&input_file_chain, 523270071Sgrehan (lang_statement_union_type *) p, 524270071Sgrehan &p->next_real_file); 525270071Sgrehan return p; 526270071Sgrehan} 527270071Sgrehan 528270071Sgrehanlang_input_statement_type * 529221828Sgrehanlang_add_input_file (name, file_type, target) 530221828Sgrehan CONST char *name; 531221828Sgrehan lang_input_file_enum_type file_type; 532221828Sgrehan CONST char *target; 533221828Sgrehan{ 534221828Sgrehan lang_has_input_file = true; 535221828Sgrehan return new_afile (name, file_type, target, true); 536221828Sgrehan} 537221828Sgrehan 538256072Sneel/* Build enough state so that the parser can build its tree */ 539221828Sgrehanvoid 540256072Sneellang_init () 541256072Sneel{ 542256072Sneel obstack_begin (&stat_obstack, 1000); 543256072Sneel 544221828Sgrehan stat_ptr = &statement_list; 545221828Sgrehan 546221828Sgrehan lang_list_init (stat_ptr); 547221828Sgrehan 548221828Sgrehan lang_list_init (&input_file_chain); 549221828Sgrehan lang_list_init (&lang_output_section_statement); 550256072Sneel lang_list_init (&file_chain); 551256072Sneel first_file = lang_add_input_file ((char *) NULL, 552221828Sgrehan lang_input_file_is_marker_enum, 553221828Sgrehan (char *) NULL); 554256072Sneel abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); 555256072Sneel 556241041Sneel abs_output_section->bfd_section = bfd_abs_section_ptr; 557241041Sneel 558241041Sneel} 559241041Sneel 560241041Sneel/*---------------------------------------------------------------------- 561241041Sneel A region is an area of memory declared with the 562241041Sneel MEMORY { name:org=exp, len=exp ... } 563241041Sneel syntax. 564256072Sneel 565241041Sneel We maintain a list of all the regions here 566241041Sneel 567256072Sneel If no regions are specified in the script, then the default is used 568256072Sneel which is created when looked up to be the entire data space 569256072Sneel*/ 570256072Sneel 571241041Sneelstatic lang_memory_region_type *lang_memory_region_list; 572241041Sneelstatic lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list; 573221828Sgrehan 574241041Sneellang_memory_region_type * 575221828Sgrehanlang_memory_region_lookup (name) 576256072Sneel CONST char *CONST name; 577256072Sneel{ 578256072Sneel lang_memory_region_type *p; 579256072Sneel 580221828Sgrehan for (p = lang_memory_region_list; 581241041Sneel p != (lang_memory_region_type *) NULL; 582241041Sneel p = p->next) 583221828Sgrehan { 584241041Sneel if (strcmp (p->name, name) == 0) 585241041Sneel { 586241041Sneel return p; 587256072Sneel } 588256072Sneel } 589256072Sneel 590241041Sneel#if 0 591241041Sneel /* This code used to always use the first region in the list as the 592241041Sneel default region. I changed it to instead use a region 593241041Sneel encompassing all of memory as the default region. This permits 594241041Sneel NOLOAD sections to work reasonably without requiring a region. 595221828Sgrehan People should specify what region they mean, if they really want 596241041Sneel a region. */ 597241041Sneel if (strcmp (name, "*default*") == 0) 598221828Sgrehan { 599241041Sneel if (lang_memory_region_list != (lang_memory_region_type *) NULL) 600241041Sneel { 601221828Sgrehan return lang_memory_region_list; 602241041Sneel } 603241041Sneel } 604241041Sneel#endif 605241041Sneel 606241041Sneel { 607241041Sneel lang_memory_region_type *new = 608241041Sneel (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type)); 609221828Sgrehan 610221828Sgrehan new->name = buystring (name); 611221828Sgrehan new->next = (lang_memory_region_type *) NULL; 612241178Sneel 613221828Sgrehan *lang_memory_region_list_tail = new; 614256072Sneel lang_memory_region_list_tail = &new->next; 615256072Sneel new->origin = 0; 616256072Sneel new->flags = 0; 617241178Sneel new->not_flags = 0; 618256072Sneel new->length = ~(bfd_size_type)0; 619256072Sneel new->current = 0; 620256072Sneel new->had_full_message = false; 621241178Sneel 622256072Sneel return new; 623256072Sneel } 624256072Sneel} 625256072Sneel 626256072Sneel 627270159Sgrehanlang_memory_region_type * 628270159Sgrehanlang_memory_default (section) 629270159Sgrehan asection *section; 630270159Sgrehan{ 631270159Sgrehan lang_memory_region_type *p; 632270159Sgrehan 633270159Sgrehan flagword sec_flags = section->flags; 634270159Sgrehan 635270159Sgrehan /* Override SEC_DATA to mean a writable section. */ 636270159Sgrehan if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) 637270159Sgrehan sec_flags |= SEC_DATA; 638270159Sgrehan 639270159Sgrehan for (p = lang_memory_region_list; 640270159Sgrehan p != (lang_memory_region_type *) NULL; 641270159Sgrehan p = p->next) 642256072Sneel { 643256072Sneel if ((p->flags & sec_flags) != 0 644256072Sneel && (p->not_flags & sec_flags) == 0) 645256072Sneel { 646256072Sneel return p; 647256072Sneel } 648256072Sneel } 649256072Sneel return lang_memory_region_lookup ("*default*"); 650256072Sneel} 651256072Sneel 652256072Sneellang_output_section_statement_type * 653256072Sneellang_output_section_find (name) 654256072Sneel CONST char *CONST name; 655256072Sneel{ 656256072Sneel lang_statement_union_type *u; 657256072Sneel lang_output_section_statement_type *lookup; 658256072Sneel 659256072Sneel for (u = lang_output_section_statement.head; 660256072Sneel u != (lang_statement_union_type *) NULL; 661256072Sneel u = lookup->next) 662256072Sneel { 663256072Sneel lookup = &u->output_section_statement; 664256072Sneel if (strcmp (name, lookup->name) == 0) 665256072Sneel { 666256072Sneel return lookup; 667256072Sneel } 668256072Sneel } 669256072Sneel return (lang_output_section_statement_type *) NULL; 670256072Sneel} 671256072Sneel 672256072Sneellang_output_section_statement_type * 673256072Sneellang_output_section_statement_lookup (name) 674256072Sneel CONST char *CONST name; 675256072Sneel{ 676256072Sneel lang_output_section_statement_type *lookup; 677256072Sneel 678256072Sneel lookup = lang_output_section_find (name); 679256072Sneel if (lookup == (lang_output_section_statement_type *) NULL) 680241178Sneel { 681241178Sneel 682256072Sneel lookup = (lang_output_section_statement_type *) 683256072Sneel new_stat (lang_output_section_statement, stat_ptr); 684256072Sneel lookup->region = (lang_memory_region_type *) NULL; 685256072Sneel lookup->lma_region = (lang_memory_region_type *) NULL; 686241362Sneel lookup->fill = 0; 687256072Sneel lookup->block_value = 1; 688241362Sneel lookup->name = name; 689256072Sneel 690256072Sneel lookup->next = (lang_statement_union_type *) NULL; 691256072Sneel lookup->bfd_section = (asection *) NULL; 692241178Sneel lookup->processed = false; 693256072Sneel lookup->sectype = normal_section; 694256072Sneel lookup->addr_tree = (etree_type *) NULL; 695256072Sneel lang_list_init (&lookup->children); 696256072Sneel 697256072Sneel lookup->memspec = (CONST char *) NULL; 698256072Sneel lookup->flags = 0; 699256072Sneel lookup->subsection_alignment = -1; 700256072Sneel lookup->section_alignment = -1; 701256072Sneel lookup->load_base = (union etree_union *) NULL; 702256072Sneel lookup->phdrs = NULL; 703256072Sneel 704256072Sneel lang_statement_append (&lang_output_section_statement, 705256072Sneel (lang_statement_union_type *) lookup, 706256072Sneel &lookup->next); 707256072Sneel } 708256072Sneel return lookup; 709256072Sneel} 710256072Sneel 711256072Sneelstatic void 712256072Sneellang_map_flags (flag) 713256072Sneel flagword flag; 714256072Sneel{ 715256072Sneel if (flag & SEC_ALLOC) 716256072Sneel minfo ("a"); 717256072Sneel 718256072Sneel if (flag & SEC_CODE) 719256072Sneel minfo ("x"); 720256072Sneel 721256072Sneel if (flag & SEC_READONLY) 722256072Sneel minfo ("r"); 723256072Sneel 724256072Sneel if (flag & SEC_DATA) 725256072Sneel minfo ("w"); 726256072Sneel 727256072Sneel if (flag & SEC_LOAD) 728256072Sneel minfo ("l"); 729256072Sneel} 730256072Sneel 731256072Sneelvoid 732241178Sneellang_map () 733241178Sneel{ 734256072Sneel lang_memory_region_type *m; 735256072Sneel 736256072Sneel minfo (_("\nMemory Configuration\n\n")); 737256072Sneel fprintf (config.map_file, "%-16s %-18s %-18s %s\n", 738256072Sneel _("Name"), _("Origin"), _("Length"), _("Attributes")); 739256072Sneel 740256072Sneel for (m = lang_memory_region_list; 741256072Sneel m != (lang_memory_region_type *) NULL; 742256072Sneel m = m->next) 743256072Sneel { 744256072Sneel char buf[100]; 745256072Sneel int len; 746256072Sneel 747256072Sneel fprintf (config.map_file, "%-16s ", m->name); 748256072Sneel 749256072Sneel sprintf_vma (buf, m->origin); 750256072Sneel minfo ("0x%s ", buf); 751256072Sneel len = strlen (buf); 752256072Sneel while (len < 16) 753256072Sneel { 754221828Sgrehan print_space (); 755256072Sneel ++len; 756267070Sjhb } 757256072Sneel 758256072Sneel minfo ("0x%V", m->length); 759221828Sgrehan if (m->flags || m->not_flags) 760256072Sneel { 761256072Sneel#ifndef BFD64 762221828Sgrehan minfo (" "); 763256072Sneel#endif 764256072Sneel if (m->flags) 765256072Sneel { 766256072Sneel print_space (); 767256072Sneel lang_map_flags (m->flags); 768256072Sneel } 769241362Sneel 770256072Sneel if (m->not_flags) 771256072Sneel { 772256072Sneel minfo (" !"); 773256072Sneel lang_map_flags (m->not_flags); 774256072Sneel } 775241362Sneel } 776267070Sjhb 777256072Sneel print_nl (); 778256072Sneel } 779270159Sgrehan 780256072Sneel fprintf (config.map_file, _("\nLinker script and memory map\n\n")); 781241362Sneel 782256072Sneel print_statements (); 783256072Sneel} 784256072Sneel 785241041Sneel/* Initialize an output section. */ 786256072Sneel 787256072Sneelstatic void 788256072Sneelinit_os (s) 789256072Sneel lang_output_section_statement_type *s; 790256072Sneel{ 791221828Sgrehan section_userdata_type *new; 792221828Sgrehan 793256072Sneel if (s->bfd_section != NULL) 794256072Sneel return; 795256072Sneel 796221828Sgrehan if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) 797256072Sneel einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME); 798256072Sneel 799221828Sgrehan new = ((section_userdata_type *) 800256072Sneel stat_alloc (sizeof (section_userdata_type))); 801256072Sneel 802256072Sneel s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); 803241148Sneel if (s->bfd_section == (asection *) NULL) 804256072Sneel s->bfd_section = bfd_make_section (output_bfd, s->name); 805256072Sneel if (s->bfd_section == (asection *) NULL) 806256072Sneel { 807256072Sneel einfo (_("%P%F: output format %s cannot represent section called %s\n"), 808256072Sneel output_bfd->xvec->name, s->name); 809256072Sneel } 810256072Sneel s->bfd_section->output_section = s->bfd_section; 811256072Sneel 812256072Sneel /* We initialize an output sections output offset to minus its own */ 813256072Sneel /* vma to allow us to output a section through itself */ 814221828Sgrehan s->bfd_section->output_offset = 0; 815221828Sgrehan get_userdata (s->bfd_section) = (PTR) new; 816256072Sneel 817256072Sneel /* If there is a base address, make sure that any sections it might 818256072Sneel mention are initialized. */ 819256072Sneel if (s->addr_tree != NULL) 820256072Sneel exp_init_os (s->addr_tree); 821256072Sneel} 822256072Sneel 823256072Sneel/* Make sure that all output sections mentioned in an expression are 824256072Sneel initialized. */ 825256072Sneel 826221828Sgrehanstatic void 827221828Sgrehanexp_init_os (exp) 828221828Sgrehan etree_type *exp; 829221828Sgrehan{ 830221828Sgrehan switch (exp->type.node_class) 831221828Sgrehan { 832221828Sgrehan case etree_assign: 833221828Sgrehan exp_init_os (exp->assign.src); 834256072Sneel break; 835256072Sneel 836256072Sneel case etree_binary: 837221828Sgrehan exp_init_os (exp->binary.lhs); 838221828Sgrehan exp_init_os (exp->binary.rhs); 839221828Sgrehan break; 840221828Sgrehan 841221828Sgrehan case etree_trinary: 842221828Sgrehan exp_init_os (exp->trinary.cond); 843221828Sgrehan exp_init_os (exp->trinary.lhs); 844256072Sneel exp_init_os (exp->trinary.rhs); 845256072Sneel break; 846256072Sneel 847256072Sneel case etree_unary: 848256072Sneel exp_init_os (exp->unary.child); 849256072Sneel break; 850256072Sneel 851256072Sneel case etree_name: 852256072Sneel switch (exp->type.node_code) 853256072Sneel { 854256072Sneel case ADDR: 855256072Sneel case LOADADDR: 856256072Sneel case SIZEOF: 857256072Sneel { 858256072Sneel lang_output_section_statement_type *os; 859256072Sneel 860256072Sneel os = lang_output_section_find (exp->name.name); 861256072Sneel if (os != NULL && os->bfd_section == NULL) 862256072Sneel init_os (os); 863256072Sneel } 864256072Sneel } 865256072Sneel break; 866256072Sneel 867256072Sneel default: 868256072Sneel break; 869256072Sneel } 870256072Sneel} 871221828Sgrehan 872221828Sgrehan/* Sections marked with the SEC_LINK_ONCE flag should only be linked 873221828Sgrehan once into the output. This routine checks each section, and 874221828Sgrehan arrange to discard it if a section of the same name has already 875221828Sgrehan been linked. If the section has COMDAT information, then it uses 876221828Sgrehan that to decide whether the section should be included. This code 877221828Sgrehan assumes that all relevant sections have the SEC_LINK_ONCE flag set; 878221828Sgrehan that is, it does not depend solely upon the section name. 879221828Sgrehan section_already_linked is called via bfd_map_over_sections. */ 880221828Sgrehan 881221828Sgrehan/* This is the shape of the elements inside the already_linked hash 882221828Sgrehan table. It maps a name onto a list of already_linked elements with 883221828Sgrehan the same name. It's possible to get more than one element in a 884284894Sneel list if the COMDAT sections have different names. */ 885221828Sgrehan 886284894Sneelstruct already_linked_hash_entry 887284894Sneel{ 888221828Sgrehan struct bfd_hash_entry root; 889284894Sneel struct already_linked *entry; 890221828Sgrehan}; 891221828Sgrehan 892221828Sgrehanstruct already_linked 893221828Sgrehan{ 894221828Sgrehan struct already_linked *next; 895284894Sneel asection *sec; 896284894Sneel}; 897284894Sneel 898284894Sneel/* The hash table. */ 899284894Sneel 900284894Sneelstatic struct bfd_hash_table already_linked_table; 901284894Sneel 902284894Sneel/*ARGSUSED*/ 903284894Sneelstatic void 904221828Sgrehansection_already_linked (abfd, sec, data) 905221828Sgrehan bfd *abfd; 906221828Sgrehan asection *sec; 907221828Sgrehan PTR data; 908221828Sgrehan{ 909221828Sgrehan lang_input_statement_type *entry = (lang_input_statement_type *) data; 910221828Sgrehan flagword flags; 911221828Sgrehan const char *name; 912221828Sgrehan struct already_linked *l; 913221828Sgrehan struct already_linked_hash_entry *already_linked_list; 914221828Sgrehan 915221828Sgrehan /* If we are only reading symbols from this object, then we want to 916221828Sgrehan discard all sections. */ 917221828Sgrehan if (entry->just_syms_flag) 918221828Sgrehan { 919221828Sgrehan sec->output_section = bfd_abs_section_ptr; 920221828Sgrehan sec->output_offset = sec->vma; 921221828Sgrehan return; 922221828Sgrehan } 923221828Sgrehan 924221828Sgrehan flags = bfd_get_section_flags (abfd, sec); 925221828Sgrehan 926221828Sgrehan if ((flags & SEC_LINK_ONCE) == 0) 927221828Sgrehan return; 928221828Sgrehan 929221828Sgrehan /* FIXME: When doing a relocateable link, we may have trouble 930221828Sgrehan copying relocations in other sections that refer to local symbols 931221828Sgrehan in the section being discarded. Those relocations will have to 932221828Sgrehan be converted somehow; as of this writing I'm not sure that any of 933221828Sgrehan the backends handle that correctly. 934221828Sgrehan 935221828Sgrehan It is tempting to instead not discard link once sections when 936221828Sgrehan doing a relocateable link (technically, they should be discarded 937221828Sgrehan whenever we are building constructors). However, that fails, 938221828Sgrehan because the linker winds up combining all the link once sections 939221828Sgrehan into a single large link once section, which defeats the purpose 940221828Sgrehan of having link once sections in the first place. 941221828Sgrehan 942221828Sgrehan Also, not merging link once sections in a relocateable link 943221828Sgrehan causes trouble for MIPS ELF, which relies in link once semantics 944221828Sgrehan to handle the .reginfo section correctly. */ 945221828Sgrehan 946221828Sgrehan name = bfd_get_section_name (abfd, sec); 947221828Sgrehan 948221828Sgrehan already_linked_list = 949221828Sgrehan ((struct already_linked_hash_entry *) 950221828Sgrehan bfd_hash_lookup (&already_linked_table, name, true, false)); 951221828Sgrehan 952221828Sgrehan for (l = already_linked_list->entry; l != NULL; l = l->next) 953221828Sgrehan { 954221828Sgrehan if (sec->comdat == NULL 955221828Sgrehan || l->sec->comdat == NULL 956221828Sgrehan || strcmp (sec->comdat->name, l->sec->comdat->name) == 0) 957221828Sgrehan { 958221828Sgrehan /* The section has already been linked. See if we should 959221828Sgrehan issue a warning. */ 960221828Sgrehan switch (flags & SEC_LINK_DUPLICATES) 961221828Sgrehan { 962221828Sgrehan default: 963221828Sgrehan abort (); 964221828Sgrehan 965221828Sgrehan case SEC_LINK_DUPLICATES_DISCARD: 966221828Sgrehan break; 967221828Sgrehan 968221828Sgrehan case SEC_LINK_DUPLICATES_ONE_ONLY: 969234695Sgrehan if (sec->comdat == NULL) 970234695Sgrehan einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"), 971242122Sneel abfd, name); 972242122Sneel else 973221828Sgrehan einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"), 974234695Sgrehan abfd, name, sec->comdat->name); 975242122Sneel break; 976267427Sjhb 977267427Sjhb case SEC_LINK_DUPLICATES_SAME_CONTENTS: 978267427Sjhb /* FIXME: We should really dig out the contents of both 979267427Sjhb sections and memcmp them. The COFF/PE spec says that 980242122Sneel the Microsoft linker does not implement this 981242122Sneel correctly, so I'm not going to bother doing it 982242122Sneel either. */ 983242122Sneel /* Fall through. */ 984242122Sneel case SEC_LINK_DUPLICATES_SAME_SIZE: 985221828Sgrehan if (bfd_section_size (abfd, sec) 986221828Sgrehan != bfd_section_size (l->sec->owner, l->sec)) 987221828Sgrehan einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"), 988221828Sgrehan abfd, name); 989221828Sgrehan break; 990221828Sgrehan } 991242122Sneel 992242122Sneel /* Set the output_section field so that wild_doit does not 993242122Sneel create a lang_input_section structure for this section. 994267427Sjhb Since there might be a symbol in the section being 995267427Sjhb discarded, we must retain a pointer to the section which 996267427Sjhb we are really going to use. */ 997267427Sjhb sec->output_section = bfd_abs_section_ptr; 998267427Sjhb if (sec->comdat != NULL) 999267427Sjhb sec->comdat->sec = l->sec; 1000242122Sneel 1001242122Sneel return; 1002234695Sgrehan } 1003221828Sgrehan } 1004221828Sgrehan 1005221828Sgrehan /* This is the first section with this name. Record it. Allocate 1006248389Sneel the memory from the same obstack as the hash table is kept in. */ 1007242065Sneel 1008256072Sneel l = ((struct already_linked *) 1009284900Sneel bfd_hash_allocate (&already_linked_table, sizeof *l)); 1010266393Sjhb 1011256072Sneel l->sec = sec; 1012284900Sneel l->next = already_linked_list->entry; 1013256072Sneel already_linked_list->entry = l; 1014256072Sneel} 1015284900Sneel 1016256072Sneel/* Support routines for the hash table used by section_already_linked, 1017256072Sneel initialize the table, fill in an entry and remove the table. */ 1018256072Sneel 1019266393Sjhbstatic struct bfd_hash_entry * 1020266393Sjhbalready_linked_newfunc (entry, table, string) 1021266393Sjhb struct bfd_hash_entry *entry ATTRIBUTE_UNUSED; 1022266393Sjhb struct bfd_hash_table *table; 1023266393Sjhb const char *string ATTRIBUTE_UNUSED; 1024284900Sneel{ 1025284900Sneel struct already_linked_hash_entry *ret = 1026284900Sneel bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry)); 1027284900Sneel 1028284900Sneel ret->entry = NULL; 1029266393Sjhb 1030284900Sneel return (struct bfd_hash_entry *) ret; 1031266393Sjhb} 1032266393Sjhb 1033266393Sjhbstatic void 1034266393Sjhbalready_linked_table_init () 1035266393Sjhb{ 1036266393Sjhb if (! bfd_hash_table_init_n (&already_linked_table, 1037266393Sjhb already_linked_newfunc, 1038266393Sjhb 42)) 1039266393Sjhb einfo (_("%P%F: Failed to create hash table\n")); 1040266393Sjhb} 1041266393Sjhb 1042266393Sjhbstatic void 1043266393Sjhbalready_linked_table_free () 1044266393Sjhb{ 1045256072Sneel bfd_hash_table_free (&already_linked_table); 1046256072Sneel} 1047256072Sneel 1048256072Sneel/* The wild routines. 1049256072Sneel 1050256072Sneel These expand statements like *(.text) and foo.o to a list of 1051256072Sneel explicit actions, like foo.o(.text), bar.o(.text) and 1052256072Sneel foo.o(.text, .data). */ 1053256072Sneel 1054256072Sneel/* Return true if the PATTERN argument is a wildcard pattern. 1055256072Sneel Although backslashes are treated specially if a pattern contains 1056256072Sneel wildcards, we do not consider the mere presence of a backslash to 1057256072Sneel be enough to cause the the pattern to be treated as a wildcard. 1058256072Sneel That lets us handle DOS filenames more naturally. */ 1059256072Sneel 1060256072Sneelstatic boolean 1061256072Sneelwildcardp (pattern) 1062256072Sneel const char *pattern; 1063256072Sneel{ 1064266393Sjhb const char *s; 1065266393Sjhb 1066266393Sjhb for (s = pattern; *s != '\0'; ++s) 1067284900Sneel if (*s == '?' 1068284900Sneel || *s == '*' 1069284900Sneel || *s == '[') 1070266393Sjhb return true; 1071266393Sjhb return false; 1072266393Sjhb} 1073256072Sneel 1074266393Sjhb/* Add SECTION to the output section OUTPUT. Do this by creating a 1075256072Sneel lang_input_section statement which is placed at PTR. FILE is the 1076266393Sjhb input file which holds SECTION. */ 1077266393Sjhb 1078266393Sjhbvoid 1079266393Sjhbwild_doit (ptr, section, output, file) 1080256072Sneel lang_statement_list_type *ptr; 1081256072Sneel asection *section; 1082256072Sneel lang_output_section_statement_type *output; 1083256072Sneel lang_input_statement_type *file; 1084256072Sneel{ 1085256072Sneel flagword flags; 1086256072Sneel boolean discard; 1087266393Sjhb 1088256072Sneel flags = bfd_get_section_flags (section->owner, section); 1089256072Sneel 1090256072Sneel discard = false; 1091256072Sneel 1092284900Sneel /* If we are doing a final link, discard sections marked with 1093256072Sneel SEC_EXCLUDE. */ 1094256072Sneel if (! link_info.relocateable 1095256072Sneel && (flags & SEC_EXCLUDE) != 0) 1096284900Sneel discard = true; 1097256072Sneel 1098256072Sneel /* Discard input sections which are assigned to a section named 1099256072Sneel DISCARD_SECTION_NAME. */ 1100266339Sjhb if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) 1101266339Sjhb discard = true; 1102266339Sjhb 1103266339Sjhb /* Discard debugging sections if we are stripping debugging 1104266339Sjhb information. */ 1105266339Sjhb if ((link_info.strip == strip_debugger || link_info.strip == strip_all) 1106266339Sjhb && (flags & SEC_DEBUGGING) != 0) 1107266339Sjhb discard = true; 1108266339Sjhb 1109266339Sjhb if (discard) 1110266339Sjhb { 1111266339Sjhb if (section->output_section == NULL) 1112266339Sjhb { 1113266339Sjhb /* This prevents future calls from assigning this section. */ 1114266339Sjhb section->output_section = bfd_abs_section_ptr; 1115266339Sjhb } 1116266339Sjhb return; 1117266339Sjhb } 1118266339Sjhb 1119266339Sjhb if (section->output_section == NULL) 1120266339Sjhb { 1121266339Sjhb boolean first; 1122266339Sjhb lang_input_section_type *new; 1123266339Sjhb flagword flags; 1124266339Sjhb 1125266339Sjhb if (output->bfd_section == NULL) 1126266339Sjhb { 1127266339Sjhb init_os (output); 1128266339Sjhb first = true; 1129266339Sjhb } 1130266339Sjhb else 1131266339Sjhb first = false; 1132266339Sjhb 1133266339Sjhb /* Add a section reference to the list */ 1134266339Sjhb new = new_stat (lang_input_section, ptr); 1135266339Sjhb 1136266339Sjhb new->section = section; 1137266339Sjhb new->ifile = file; 1138266339Sjhb section->output_section = output->bfd_section; 1139266339Sjhb 1140266339Sjhb flags = section->flags; 1141266339Sjhb 1142266339Sjhb /* We don't copy the SEC_NEVER_LOAD flag from an input section 1143266339Sjhb to an output section, because we want to be able to include a 1144266339Sjhb SEC_NEVER_LOAD section in the middle of an otherwise loaded 1145266339Sjhb section (I don't know why we want to do this, but we do). 1146266339Sjhb build_link_order in ldwrite.c handles this case by turning 1147266339Sjhb the embedded SEC_NEVER_LOAD section into a fill. */ 1148266339Sjhb 1149266339Sjhb flags &= ~ SEC_NEVER_LOAD; 1150266339Sjhb 1151266339Sjhb /* If final link, don't copy the SEC_LINK_ONCE flags, they've 1152266339Sjhb already been processed. One reason to do this is that on pe 1153266339Sjhb format targets, .text$foo sections go into .text and it's odd 1154266339Sjhb to see .text with SEC_LINK_ONCE set. */ 1155266339Sjhb 1156266339Sjhb if (! link_info.relocateable) 1157256072Sneel flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES); 1158256072Sneel 1159256072Sneel /* If this is not the first input section, and the SEC_READONLY 1160256072Sneel flag is not currently set, then don't set it just because the 1161262350Sjhb input section has it set. */ 1162256072Sneel 1163256072Sneel if (! first && (section->output_section->flags & SEC_READONLY) == 0) 1164268935Sjhb flags &= ~ SEC_READONLY; 1165284894Sneel 1166256072Sneel section->output_section->flags |= flags; 1167268935Sjhb 1168268935Sjhb /* If SEC_READONLY is not set in the input section, then clear 1169256072Sneel it from the output section. */ 1170268935Sjhb if ((section->flags & SEC_READONLY) == 0) 1171268935Sjhb section->output_section->flags &= ~SEC_READONLY; 1172256072Sneel 1173256072Sneel switch (output->sectype) 1174268935Sjhb { 1175268935Sjhb case normal_section: 1176268935Sjhb break; 1177268935Sjhb case dsect_section: 1178268935Sjhb case copy_section: 1179268935Sjhb case info_section: 1180268935Sjhb case overlay_section: 1181268935Sjhb output->bfd_section->flags &= ~SEC_ALLOC; 1182268935Sjhb break; 1183268935Sjhb case noload_section: 1184284900Sneel output->bfd_section->flags &= ~SEC_LOAD; 1185268935Sjhb output->bfd_section->flags |= SEC_NEVER_LOAD; 1186268935Sjhb break; 1187268935Sjhb } 1188268935Sjhb 1189268935Sjhb /* Copy over SEC_SMALL_DATA. */ 1190268935Sjhb if (section->flags & SEC_SMALL_DATA) 1191268935Sjhb section->output_section->flags |= SEC_SMALL_DATA; 1192268935Sjhb 1193268935Sjhb if (section->alignment_power > output->bfd_section->alignment_power) 1194256072Sneel output->bfd_section->alignment_power = section->alignment_power; 1195270159Sgrehan 1196270159Sgrehan /* If supplied an aligment, then force it. */ 1197270159Sgrehan if (output->section_alignment != -1) 1198270159Sgrehan output->bfd_section->alignment_power = output->section_alignment; 1199268935Sjhb } 1200268935Sjhb} 1201268935Sjhb 1202268935Sjhb/* Handle wildcard sorting. This returns the lang_input_section which 1203268935Sjhb should follow the one we are going to create for SECTION and FILE, 1204268935Sjhb based on the sorting requirements of WILD. It returns NULL if the 1205268935Sjhb new section should just go at the end of the current list. */ 1206268935Sjhb 1207268935Sjhbstatic lang_statement_union_type * 1208268935Sjhbwild_sort (wild, file, section) 1209268935Sjhb lang_wild_statement_type *wild; 1210268935Sjhb lang_input_statement_type *file; 1211268935Sjhb asection *section; 1212268935Sjhb{ 1213268935Sjhb const char *section_name; 1214268935Sjhb lang_statement_union_type *l; 1215268935Sjhb 1216268935Sjhb if (! wild->filenames_sorted && ! wild->sections_sorted) 1217268935Sjhb return NULL; 1218268935Sjhb 1219268935Sjhb section_name = bfd_get_section_name (file->the_bfd, section); 1220256072Sneel for (l = wild->children.head; l != NULL; l = l->next) 1221284900Sneel { 1222270159Sgrehan lang_input_section_type *ls; 1223270159Sgrehan 1224270159Sgrehan if (l->header.type != lang_input_section_enum) 1225270159Sgrehan continue; 1226270159Sgrehan ls = &l->input_section; 1227284900Sneel 1228256072Sneel /* Sorting by filename takes precedence over sorting by section 1229256072Sneel name. */ 1230268935Sjhb 1231268935Sjhb if (wild->filenames_sorted) 1232268935Sjhb { 1233268935Sjhb const char *fn, *ln; 1234256072Sneel boolean fa, la; 1235256072Sneel int i; 1236268935Sjhb 1237268935Sjhb /* The PE support for the .idata section as generated by 1238266339Sjhb dlltool assumes that files will be sorted by the name of 1239256072Sneel the archive and then the name of the file within the 1240256072Sneel archive. */ 1241256072Sneel 1242256072Sneel if (file->the_bfd != NULL 1243262350Sjhb && bfd_my_archive (file->the_bfd) != NULL) 1244256072Sneel { 1245256072Sneel fn = bfd_get_filename (bfd_my_archive (file->the_bfd)); 1246256072Sneel fa = true; 1247256072Sneel } 1248256072Sneel else 1249256072Sneel { 1250256072Sneel fn = file->filename; 1251256072Sneel fa = false; 1252256072Sneel } 1253284894Sneel 1254284894Sneel if (ls->ifile->the_bfd != NULL 1255284894Sneel && bfd_my_archive (ls->ifile->the_bfd) != NULL) 1256256072Sneel { 1257256072Sneel ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd)); 1258256072Sneel la = true; 1259256072Sneel } 1260256072Sneel else 1261256072Sneel { 1262256072Sneel ln = ls->ifile->filename; 1263256072Sneel la = false; 1264276349Sneel } 1265276349Sneel 1266276349Sneel i = strcmp (fn, ln); 1267276349Sneel if (i > 0) 1268256072Sneel continue; 1269276349Sneel else if (i < 0) 1270256072Sneel break; 1271256072Sneel 1272256072Sneel if (fa || la) 1273256072Sneel { 1274256072Sneel if (fa) 1275261088Sjhb fn = file->filename; 1276261088Sjhb if (la) 1277256072Sneel ln = ls->ifile->filename; 1278256072Sneel 1279256072Sneel i = strcmp (fn, ln); 1280256072Sneel if (i > 0) 1281256072Sneel continue; 1282256072Sneel else if (i < 0) 1283256072Sneel break; 1284256072Sneel } 1285262350Sjhb } 1286256072Sneel 1287256072Sneel /* Here either the files are not sorted by name, or we are 1288256072Sneel looking at the sections for this file. */ 1289256072Sneel 1290284899Sneel if (wild->sections_sorted) 1291268976Sjhb { 1292261088Sjhb if (strcmp (section_name, 1293261088Sjhb bfd_get_section_name (ls->ifile->the_bfd, 1294270159Sgrehan ls->section)) 1295284900Sneel < 0) 1296256072Sneel break; 1297256072Sneel } 1298256072Sneel } 1299256072Sneel 1300284900Sneel return l; 1301284900Sneel} 1302284900Sneel 1303256072Sneel/* Expand a wild statement for a particular FILE. SECTION may be 1304256072Sneel NULL, in which case it is a wild card. */ 1305284899Sneel 1306270159Sgrehanstatic void 1307256072Sneeloutput_section_callback (ptr, section, file, output) 1308268976Sjhb lang_wild_statement_type *ptr; 1309270159Sgrehan asection *section; 1310256072Sneel lang_input_statement_type *file; 1311276349Sneel void *output; 1312276349Sneel{ 1313256072Sneel lang_statement_union_type *before; 1314276403Sneel 1315284899Sneel /* If the wild pattern was marked KEEP, the member sections 1316284900Sneel should be as well. */ 1317276403Sneel if (ptr->keep_sections) 1318276403Sneel section->flags |= SEC_KEEP; 1319276403Sneel 1320276403Sneel before = wild_sort (ptr, file, section); 1321284900Sneel 1322276403Sneel /* Here BEFORE points to the lang_input_section which 1323284900Sneel should follow the one we are about to add. If BEFORE 1324284900Sneel is NULL, then the section should just go at the end 1325256072Sneel of the current list. */ 1326284900Sneel 1327284900Sneel if (before == NULL) 1328284900Sneel wild_doit (&ptr->children, section, 1329284900Sneel (lang_output_section_statement_type *) output, 1330284900Sneel file); 1331284900Sneel else 1332256072Sneel { 1333276403Sneel lang_statement_list_type list; 1334284900Sneel lang_statement_union_type **pp; 1335276403Sneel 1336284900Sneel lang_list_init (&list); 1337284900Sneel wild_doit (&list, section, 1338284900Sneel (lang_output_section_statement_type *) output, 1339284900Sneel file); 1340276403Sneel 1341261088Sjhb /* If we are discarding the section, LIST.HEAD will 1342261088Sjhb be NULL. */ 1343261088Sjhb if (list.head != NULL) 1344261088Sjhb { 1345261088Sjhb ASSERT (list.head->next == NULL); 1346261088Sjhb 1347261088Sjhb for (pp = &ptr->children.head; 1348261088Sjhb *pp != before; 1349261088Sjhb pp = &(*pp)->next) 1350261088Sjhb ASSERT (*pp != NULL); 1351261088Sjhb 1352262350Sjhb list.head->next = *pp; 1353256072Sneel *pp = list.head; 1354256072Sneel } 1355256072Sneel } 1356270159Sgrehan} 1357270159Sgrehan 1358256072Sneel/* This is passed a file name which must have been seen already and 1359256072Sneel added to the statement tree. We will see if it has been opened 1360256072Sneel already and had its symbols read. If not then we'll read it. */ 1361256072Sneel 1362268935Sjhbstatic lang_input_statement_type * 1363268935Sjhblookup_name (name) 1364268935Sjhb const char *name; 1365268935Sjhb{ 1366268935Sjhb lang_input_statement_type *search; 1367268935Sjhb 1368268935Sjhb for (search = (lang_input_statement_type *) input_file_chain.head; 1369268935Sjhb search != (lang_input_statement_type *) NULL; 1370268935Sjhb search = (lang_input_statement_type *) search->next_real_file) 1371268935Sjhb { 1372268935Sjhb if (search->filename == (char *) NULL && name == (char *) NULL) 1373268935Sjhb return search; 1374268935Sjhb if (search->filename != (char *) NULL 1375268935Sjhb && name != (char *) NULL 1376268935Sjhb && strcmp (search->filename, name) == 0) 1377268935Sjhb break; 1378268935Sjhb } 1379268935Sjhb 1380268935Sjhb if (search == (lang_input_statement_type *) NULL) 1381268935Sjhb search = new_afile (name, lang_input_file_is_file_enum, default_target, 1382268935Sjhb false); 1383268935Sjhb 1384268935Sjhb /* If we have already added this file, or this file is not real 1385268935Sjhb (FIXME: can that ever actually happen?) or the name is NULL 1386268935Sjhb (FIXME: can that ever actually happen?) don't add this file. */ 1387268935Sjhb if (search->loaded 1388268935Sjhb || ! search->real 1389284900Sneel || search->filename == (const char *) NULL) 1390268935Sjhb return search; 1391284900Sneel 1392268935Sjhb load_symbols (search, (lang_statement_list_type *) NULL); 1393268935Sjhb 1394268935Sjhb return search; 1395268935Sjhb} 1396268935Sjhb 1397268935Sjhb/* Get the symbols for an input file. */ 1398268935Sjhb 1399268935Sjhbstatic void 1400268935Sjhbload_symbols (entry, place) 1401268935Sjhb lang_input_statement_type *entry; 1402268935Sjhb lang_statement_list_type *place; 1403268935Sjhb{ 1404268935Sjhb char **matching; 1405268935Sjhb 1406268935Sjhb if (entry->loaded) 1407268935Sjhb return; 1408268935Sjhb 1409268935Sjhb ldfile_open_file (entry); 1410268935Sjhb 1411268935Sjhb if (! bfd_check_format (entry->the_bfd, bfd_archive) 1412268935Sjhb && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) 1413268935Sjhb { 1414284900Sneel bfd_error_type err; 1415284900Sneel lang_statement_list_type *hold; 1416284900Sneel 1417284900Sneel err = bfd_get_error (); 1418284900Sneel if (err == bfd_error_file_ambiguously_recognized) 1419284900Sneel { 1420284900Sneel char **p; 1421284900Sneel 1422284900Sneel einfo (_("%B: file not recognized: %E\n"), entry->the_bfd); 1423284900Sneel einfo (_("%B: matching formats:"), entry->the_bfd); 1424284900Sneel for (p = matching; *p != NULL; p++) 1425284900Sneel einfo (" %s", *p); 1426284900Sneel einfo ("%F\n"); 1427221828Sgrehan } 1428268935Sjhb else if (err != bfd_error_file_not_recognized 1429268935Sjhb || place == NULL) 1430268935Sjhb einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd); 1431268935Sjhb 1432268935Sjhb bfd_close (entry->the_bfd); 1433268935Sjhb entry->the_bfd = NULL; 1434268935Sjhb 1435268935Sjhb /* See if the emulation has some special knowledge. */ 1436268935Sjhb 1437268935Sjhb if (ldemul_unrecognized_file (entry)) 1438268935Sjhb return; 1439268935Sjhb 1440268935Sjhb /* Try to interpret the file as a linker script. */ 1441268935Sjhb 1442268935Sjhb ldfile_open_command_file (entry->filename); 1443268935Sjhb 1444268935Sjhb hold = stat_ptr; 1445268935Sjhb stat_ptr = place; 1446268935Sjhb 1447268935Sjhb ldfile_assumed_script = true; 1448268935Sjhb parser_input = input_script; 1449268935Sjhb yyparse (); 1450268935Sjhb ldfile_assumed_script = false; 1451268935Sjhb 1452268935Sjhb stat_ptr = hold; 1453268935Sjhb 1454268935Sjhb return; 1455268935Sjhb } 1456268935Sjhb 1457268935Sjhb if (ldemul_recognized_file (entry)) 1458268935Sjhb return; 1459268935Sjhb 1460268935Sjhb /* We don't call ldlang_add_file for an archive. Instead, the 1461268935Sjhb add_symbols entry point will call ldlang_add_file, via the 1462268935Sjhb add_archive_element callback, for each element of the archive 1463268935Sjhb which is used. */ 1464268935Sjhb switch (bfd_get_format (entry->the_bfd)) 1465268935Sjhb { 1466268935Sjhb default: 1467268935Sjhb break; 1468268935Sjhb 1469270074Sgrehan case bfd_object: 1470270074Sgrehan ldlang_add_file (entry); 1471270074Sgrehan if (trace_files || trace_file_tries) 1472270074Sgrehan info_msg ("%I\n", entry); 1473270074Sgrehan break; 1474270074Sgrehan 1475270074Sgrehan case bfd_archive: 1476270074Sgrehan if (entry->whole_archive) 1477270074Sgrehan { 1478270074Sgrehan bfd *member = bfd_openr_next_archived_file (entry->the_bfd, 1479270074Sgrehan (bfd *) NULL); 1480270074Sgrehan while (member != NULL) 1481270074Sgrehan { 1482270074Sgrehan if (! bfd_check_format (member, bfd_object)) 1483270074Sgrehan einfo (_("%F%B: object %B in archive is not object\n"), 1484284900Sneel entry->the_bfd, member); 1485284900Sneel if (! ((*link_info.callbacks->add_archive_element) 1486284900Sneel (&link_info, member, "--whole-archive"))) 1487284900Sneel abort (); 1488284900Sneel if (! bfd_link_add_symbols (member, &link_info)) 1489284900Sneel einfo (_("%F%B: could not read symbols: %E\n"), member); 1490284900Sneel member = bfd_openr_next_archived_file (entry->the_bfd, 1491284900Sneel member); 1492284900Sneel } 1493284900Sneel 1494284900Sneel entry->loaded = true; 1495284900Sneel 1496270074Sgrehan return; 1497270074Sgrehan } 1498270074Sgrehan } 1499270074Sgrehan 1500270074Sgrehan if (! bfd_link_add_symbols (entry->the_bfd, &link_info)) 1501270074Sgrehan einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd); 1502270074Sgrehan 1503270074Sgrehan entry->loaded = true; 1504270074Sgrehan} 1505270074Sgrehan 1506270074Sgrehan 1507268935Sjhb 1508221828Sgrehan/* Handle a wild statement. SECTION or FILE or both may be NULL, 1509221828Sgrehan indicating that it is a wildcard. Separate lang_input_section 1510284900Sneel statements are created for each part of the expansion; they are 1511256072Sneel added after the wild statement S. OUTPUT is the output section. */ 1512221828Sgrehan 1513221828Sgrehanstatic void 1514284894Sneelwild (s, section, file, target, output) 1515242065Sneel lang_wild_statement_type *s; 1516262350Sjhb const char *section; 1517256072Sneel const char *file; 1518221828Sgrehan const char *target ATTRIBUTE_UNUSED; 1519221828Sgrehan lang_output_section_statement_type *output; 1520221828Sgrehan{ 1521221828Sgrehan walk_wild (s, section, file, output_section_callback, (void *) output); 1522221828Sgrehan 1523221828Sgrehan if (section != (char *) NULL 1524270070Sgrehan && strcmp (section, "COMMON") == 0 1525270070Sgrehan && default_common_section == NULL) 1526270070Sgrehan { 1527270070Sgrehan /* Remember the section that common is going to in case we later 1528270070Sgrehan get something which doesn't know where to put it. */ 1529270070Sgrehan default_common_section = output; 1530256072Sneel } 1531221828Sgrehan} 1532256072Sneel 1533284900Sneel/* Return true iff target is the sought target. */ 1534284900Sneelstatic int 1535284900Sneelget_target (target, data) 1536242065Sneel const bfd_target * target; 1537221828Sgrehan void * data; 1538221828Sgrehan{ 1539256072Sneel const char * sought = (const char *) data; 1540256072Sneel 1541256072Sneel return strcmp (target->name, sought) == 0; 1542221828Sgrehan} 1543221828Sgrehan 1544221828Sgrehan/* Like strcpy() but convert to lower case as well. */ 1545221914Sjhbstatic void 1546221828Sgrehanstricpy (dest, src) 1547221828Sgrehan char * dest; 1548241489Sneel char * src; 1549256072Sneel{ 1550284900Sneel char c; 1551256072Sneel 1552241489Sneel while ((c = * src ++) != 0) 1553221828Sgrehan { 1554221828Sgrehan if (isupper ((unsigned char) c)) 1555221828Sgrehan c = tolower (c); 1556221828Sgrehan 1557221828Sgrehan * dest ++ = c; 1558221828Sgrehan } 1559256072Sneel 1560262350Sjhb * dest = 0; 1561284894Sneel} 1562256072Sneel 1563284900Sneel/* Remove the first occurance of needle (if any) in haystack 1564284900Sneel from haystack. */ 1565284900Sneelstatic void 1566268935Sjhbstrcut (haystack, needle) 1567268935Sjhb char * haystack; 1568268935Sjhb char * needle; 1569266339Sjhb{ 1570266339Sjhb haystack = strstr (haystack, needle); 1571266339Sjhb 1572266339Sjhb if (haystack) 1573266339Sjhb { 1574266339Sjhb char * src; 1575266339Sjhb 1576266339Sjhb for (src = haystack + strlen (needle); * src;) 1577256072Sneel * haystack ++ = * src ++; 1578262350Sjhb 1579262350Sjhb * haystack = 0; 1580256072Sneel } 1581256072Sneel} 1582256072Sneel 1583256072Sneel/* Compare two target format name strings. 1584256072Sneel Return a value indicating how "similar" they are. */ 1585256072Sneelstatic int 1586256072Sneelname_compare (first, second) 1587268976Sjhb char * first; 1588268976Sjhb char * second; 1589268976Sjhb{ 1590268976Sjhb char * copy1; 1591276349Sneel char * copy2; 1592276349Sneel int result; 1593276349Sneel 1594276349Sneel copy1 = xmalloc (strlen (first) + 1); 1595256072Sneel copy2 = xmalloc (strlen (second) + 1); 1596262350Sjhb 1597256072Sneel /* Convert the names to lower case. */ 1598242065Sneel stricpy (copy1, first); 1599256072Sneel stricpy (copy2, second); 1600242065Sneel 1601284894Sneel /* Remove and endian strings from the name. */ 1602242065Sneel strcut (copy1, "big"); 1603242065Sneel strcut (copy1, "little"); 1604284900Sneel strcut (copy2, "big"); 1605284900Sneel strcut (copy2, "little"); 1606256072Sneel 1607256072Sneel /* Return a value based on how many characters match, 1608221828Sgrehan starting from the beginning. If both strings are 1609221828Sgrehan the same then return 10 * their length. */ 1610221828Sgrehan for (result = 0; copy1 [result] == copy2 [result]; result ++) 1611221828Sgrehan if (copy1 [result] == 0) 1612284894Sneel { 1613284894Sneel result *= 10; 1614284894Sneel break; 1615284894Sneel } 1616284894Sneel 1617284894Sneel free (copy1); 1618284894Sneel free (copy2); 1619284894Sneel 1620284894Sneel return result; 1621284894Sneel} 1622284894Sneel 1623284894Sneel/* Set by closest_target_match() below. */ 1624284894Sneelstatic const bfd_target * winner; 1625284894Sneel 1626284894Sneel/* Scan all the valid bfd targets looking for one that has the endianness 1627284894Sneel requirement that was specified on the command line, and is the nearest 1628284894Sneel match to the original output target. */ 1629284894Sneelstatic int 1630284894Sneelclosest_target_match (target, data) 1631284894Sneel const bfd_target * target; 1632284894Sneel void * data; 1633284894Sneel{ 1634284894Sneel const bfd_target * original = (const bfd_target *) data; 1635284894Sneel 1636284894Sneel if (command_line.endian == ENDIAN_BIG && target->byteorder != BFD_ENDIAN_BIG) 1637284894Sneel return 0; 1638284894Sneel 1639284894Sneel if (command_line.endian == ENDIAN_LITTLE && target->byteorder != BFD_ENDIAN_LITTLE) 1640284894Sneel return 0; 1641284894Sneel 1642284894Sneel /* Must be the same flavour. */ 1643284894Sneel if (target->flavour != original->flavour) 1644284894Sneel return 0; 1645284894Sneel 1646284894Sneel /* If we have not found a potential winner yet, then record this one. */ 1647284894Sneel if (winner == NULL) 1648284894Sneel { 1649284894Sneel winner = target; 1650284894Sneel return 0; 1651284894Sneel } 1652284894Sneel 1653284894Sneel /* Oh dear, we now have two potential candidates for a successful match. 1654284894Sneel Compare their names and choose the better one. */ 1655270159Sgrehan if (name_compare (target->name, original->name) > name_compare (winner->name, original->name)) 1656270159Sgrehan winner = target; 1657270159Sgrehan 1658270159Sgrehan /* Keep on searching until wqe have checked them all. */ 1659270159Sgrehan return 0; 1660270159Sgrehan} 1661270159Sgrehan 1662270159Sgrehan/* Return the BFD target format of the first input file. */ 1663270159Sgrehanstatic char * 1664270159Sgrehanget_first_input_target () 1665270159Sgrehan{ 1666270159Sgrehan char * target = NULL; 1667270159Sgrehan 1668270159Sgrehan LANG_FOR_EACH_INPUT_STATEMENT (s) 1669270159Sgrehan { 1670270159Sgrehan if (s->header.type == lang_input_statement_enum 1671270159Sgrehan && s->real) 1672270159Sgrehan { 1673270159Sgrehan ldfile_open_file (s); 1674270159Sgrehan 1675270159Sgrehan if (s->the_bfd != NULL 1676270159Sgrehan && bfd_check_format (s->the_bfd, bfd_object)) 1677270159Sgrehan { 1678270159Sgrehan target = bfd_get_target (s->the_bfd); 1679270159Sgrehan 1680270159Sgrehan if (target != NULL) 1681270159Sgrehan break; 1682270159Sgrehan } 1683270159Sgrehan } 1684270159Sgrehan } 1685270159Sgrehan 1686270159Sgrehan return target; 1687270159Sgrehan} 1688270159Sgrehan 1689270159Sgrehan/* Open the output file. */ 1690270159Sgrehan 1691270159Sgrehanstatic bfd * 1692270159Sgrehanopen_output (name) 1693270159Sgrehan const char * name; 1694270159Sgrehan{ 1695270159Sgrehan bfd * output; 1696270159Sgrehan 1697270159Sgrehan /* Has the user told us which output format to use ? */ 1698270159Sgrehan if (output_target == (char *) NULL) 1699270159Sgrehan { 1700270159Sgrehan /* No - has the current target been set to something other than the default ? */ 1701270159Sgrehan if (current_target != default_target) 1702270159Sgrehan output_target = current_target; 1703270159Sgrehan 1704270159Sgrehan /* No - can we determine the format of the first input file ? */ 1705270159Sgrehan else 1706270159Sgrehan { 1707270159Sgrehan output_target = get_first_input_target (); 1708270159Sgrehan 1709270159Sgrehan /* Failed - use the default output target. */ 1710270159Sgrehan if (output_target == NULL) 1711270159Sgrehan output_target = default_target; 1712270159Sgrehan } 1713270159Sgrehan } 1714270159Sgrehan 1715270159Sgrehan /* Has the user requested a particular endianness on the command line ? */ 1716270159Sgrehan if (command_line.endian != ENDIAN_UNSET) 1717270159Sgrehan { 1718270159Sgrehan const bfd_target * target; 1719270159Sgrehan enum bfd_endian desired_endian; 1720270159Sgrehan 1721270159Sgrehan /* Get the chosen target. */ 1722270159Sgrehan target = bfd_search_for_target (get_target, (void *) output_target); 1723270159Sgrehan 1724270159Sgrehan if (command_line.endian == ENDIAN_BIG) 1725270159Sgrehan desired_endian = BFD_ENDIAN_BIG; 1726270159Sgrehan else 1727270159Sgrehan desired_endian = BFD_ENDIAN_LITTLE; 1728270159Sgrehan 1729270159Sgrehan /* See if the target has the wrong endianness. This should not happen 1730270159Sgrehan if the linker script has provided big and little endian alternatives, 1731270159Sgrehan but some scrips don't do this. */ 1732270159Sgrehan if (target->byteorder != desired_endian) 1733270159Sgrehan { 1734270159Sgrehan /* If it does, then see if the target provides 1735270159Sgrehan an alternative with the correct endianness. */ 1736270159Sgrehan if (target->alternative_target != NULL 1737270159Sgrehan && (target->alternative_target->byteorder == desired_endian)) 1738270159Sgrehan output_target = target->alternative_target->name; 1739270159Sgrehan else 1740270159Sgrehan { 1741270159Sgrehan /* Try to find a target as similar as possible to the default 1742270159Sgrehan target, but which has the desired endian characteristic. */ 1743270159Sgrehan (void) bfd_search_for_target (closest_target_match, (void *) target); 1744270159Sgrehan 1745270159Sgrehan /* Oh dear - we could not find any targets that satisfy our requirements. */ 1746270159Sgrehan if (winner == NULL) 1747270159Sgrehan einfo (_("%P: warning: could not find any targets that match endianness requirement\n")); 1748270159Sgrehan else 1749270159Sgrehan output_target = winner->name; 1750270159Sgrehan } 1751270159Sgrehan } 1752270159Sgrehan } 1753270159Sgrehan 1754270159Sgrehan output = bfd_openw (name, output_target); 1755270159Sgrehan 1756270159Sgrehan if (output == (bfd *) NULL) 1757270159Sgrehan { 1758270159Sgrehan if (bfd_get_error () == bfd_error_invalid_target) 1759270159Sgrehan einfo (_("%P%F: target %s not found\n"), output_target); 1760270159Sgrehan 1761270159Sgrehan einfo (_("%P%F: cannot open output file %s: %E\n"), name); 1762270159Sgrehan } 1763270159Sgrehan 1764270159Sgrehan delete_output_file_on_failure = true; 1765270159Sgrehan 1766270159Sgrehan /* output->flags |= D_PAGED;*/ 1767270159Sgrehan 1768270159Sgrehan if (! bfd_set_format (output, bfd_object)) 1769270159Sgrehan einfo (_("%P%F:%s: can not make object file: %E\n"), name); 1770270159Sgrehan if (! bfd_set_arch_mach (output, 1771270159Sgrehan ldfile_output_architecture, 1772270159Sgrehan ldfile_output_machine)) 1773270159Sgrehan einfo (_("%P%F:%s: can not set architecture: %E\n"), name); 1774270159Sgrehan 1775270159Sgrehan link_info.hash = bfd_link_hash_table_create (output); 1776270159Sgrehan if (link_info.hash == (struct bfd_link_hash_table *) NULL) 1777270159Sgrehan einfo (_("%P%F: can not create link hash table: %E\n")); 1778270159Sgrehan 1779270159Sgrehan bfd_set_gp_size (output, g_switch_value); 1780270159Sgrehan return output; 1781270159Sgrehan} 1782270159Sgrehan 1783270159Sgrehanstatic void 1784284894Sneelldlang_open_output (statement) 1785270159Sgrehan lang_statement_union_type * statement; 1786284894Sneel{ 1787270159Sgrehan switch (statement->header.type) 1788284894Sneel { 1789270159Sgrehan case lang_output_statement_enum: 1790270159Sgrehan ASSERT (output_bfd == (bfd *) NULL); 1791270159Sgrehan output_bfd = open_output (statement->output_statement.name); 1792270159Sgrehan ldemul_set_output_arch (); 1793270159Sgrehan if (config.magic_demand_paged && !link_info.relocateable) 1794270159Sgrehan output_bfd->flags |= D_PAGED; 1795270159Sgrehan else 1796270159Sgrehan output_bfd->flags &= ~D_PAGED; 1797270159Sgrehan if (config.text_read_only) 1798270159Sgrehan output_bfd->flags |= WP_TEXT; 1799270159Sgrehan else 1800270159Sgrehan output_bfd->flags &= ~WP_TEXT; 1801270159Sgrehan if (link_info.traditional_format) 1802270159Sgrehan output_bfd->flags |= BFD_TRADITIONAL_FORMAT; 1803270159Sgrehan else 1804270159Sgrehan output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; 1805270159Sgrehan break; 1806270159Sgrehan 1807270159Sgrehan case lang_target_statement_enum: 1808270159Sgrehan current_target = statement->target_statement.target; 1809270159Sgrehan break; 1810270159Sgrehan default: 1811270159Sgrehan break; 1812270159Sgrehan } 1813284894Sneel} 1814270159Sgrehan 1815270159Sgrehan/* Open all the input files. */ 1816270159Sgrehan 1817270159Sgrehanstatic void 1818270159Sgrehanopen_input_bfds (s, force) 1819270159Sgrehan lang_statement_union_type *s; 1820270159Sgrehan boolean force; 1821270159Sgrehan{ 1822270159Sgrehan for (; s != (lang_statement_union_type *) NULL; s = s->next) 1823270159Sgrehan { 1824270159Sgrehan switch (s->header.type) 1825270159Sgrehan { 1826270159Sgrehan case lang_constructors_statement_enum: 1827270159Sgrehan open_input_bfds (constructor_list.head, force); 1828270159Sgrehan break; 1829270159Sgrehan case lang_output_section_statement_enum: 1830270159Sgrehan open_input_bfds (s->output_section_statement.children.head, force); 1831270159Sgrehan break; 1832270159Sgrehan case lang_wild_statement_enum: 1833270159Sgrehan /* Maybe we should load the file's symbols */ 1834270159Sgrehan if (s->wild_statement.filename 1835270159Sgrehan && ! wildcardp (s->wild_statement.filename)) 1836270159Sgrehan (void) lookup_name (s->wild_statement.filename); 1837270159Sgrehan open_input_bfds (s->wild_statement.children.head, force); 1838270159Sgrehan break; 1839270159Sgrehan case lang_group_statement_enum: 1840270159Sgrehan { 1841270159Sgrehan struct bfd_link_hash_entry *undefs; 1842270159Sgrehan 1843270159Sgrehan /* We must continually search the entries in the group 1844270159Sgrehan until no new symbols are added to the list of undefined 1845270159Sgrehan symbols. */ 1846270159Sgrehan 1847270159Sgrehan do 1848270159Sgrehan { 1849270159Sgrehan undefs = link_info.hash->undefs_tail; 1850270159Sgrehan open_input_bfds (s->group_statement.children.head, true); 1851284894Sneel } 1852284894Sneel while (undefs != link_info.hash->undefs_tail); 1853221828Sgrehan } 1854267427Sjhb break; 1855284900Sneel case lang_target_statement_enum: 1856284894Sneel current_target = s->target_statement.target; 1857267427Sjhb break; 1858221828Sgrehan case lang_input_statement_enum: 1859221828Sgrehan if (s->input_statement.real) 1860221828Sgrehan { 1861284894Sneel lang_statement_list_type add; 1862221828Sgrehan 1863221828Sgrehan s->input_statement.target = current_target; 1864270159Sgrehan 1865270159Sgrehan /* If we are being called from within a group, and this 1866270159Sgrehan is an archive which has already been searched, then 1867270159Sgrehan force it to be researched. */ 1868270159Sgrehan if (force 1869284894Sneel && s->input_statement.loaded 1870270159Sgrehan && bfd_check_format (s->input_statement.the_bfd, 1871270159Sgrehan bfd_archive)) 1872267427Sjhb s->input_statement.loaded = false; 1873221828Sgrehan 1874267427Sjhb lang_list_init (&add); 1875267427Sjhb 1876284894Sneel load_symbols (&s->input_statement, &add); 1877267427Sjhb 1878267427Sjhb if (add.head != NULL) 1879267427Sjhb { 1880284900Sneel *add.tail = s->next; 1881284900Sneel s->next = add.head; 1882284900Sneel } 1883284900Sneel } 1884284900Sneel break; 1885284900Sneel default: 1886284900Sneel break; 1887284900Sneel } 1888284900Sneel } 1889284900Sneel} 1890284894Sneel 1891284894Sneel/* If there are [COMMONS] statements, put a wild one into the bss section */ 1892284894Sneel 1893284894Sneelstatic void 1894284894Sneellang_reasonable_defaults () 1895284894Sneel{ 1896284894Sneel#if 0 1897284894Sneel lang_output_section_statement_lookup (".text"); 1898284894Sneel lang_output_section_statement_lookup (".data"); 1899284894Sneel 1900284894Sneel default_common_section = 1901284894Sneel lang_output_section_statement_lookup (".bss"); 1902284894Sneel 1903267427Sjhb 1904284894Sneel if (placed_commons == false) 1905284894Sneel { 1906284894Sneel lang_wild_statement_type *new = 1907284894Sneel new_stat (lang_wild_statement, 1908267427Sjhb &default_common_section->children); 1909221828Sgrehan 1910221828Sgrehan new->section_name = "COMMON"; 1911270159Sgrehan new->filename = (char *) NULL; 1912270159Sgrehan lang_list_init (&new->children); 1913270159Sgrehan } 1914267427Sjhb#endif 1915270159Sgrehan 1916284894Sneel} 1917267427Sjhb 1918270159Sgrehan/* 1919284894Sneel Add the supplied name to the symbol table as an undefined reference. 1920270159Sgrehan Remove items from the chain as we open input bfds 1921284894Sneel */ 1922284894Sneeltypedef struct ldlang_undef_chain_list 1923267427Sjhb{ 1924267427Sjhb struct ldlang_undef_chain_list *next; 1925267427Sjhb char *name; 1926267427Sjhb} ldlang_undef_chain_list_type; 1927270159Sgrehan 1928268976Sjhbstatic ldlang_undef_chain_list_type *ldlang_undef_chain_list_head; 1929270159Sgrehan 1930268976Sjhbvoid 1931268976Sjhbldlang_add_undef (name) 1932270159Sgrehan CONST char *CONST name; 1933268976Sjhb{ 1934268976Sjhb ldlang_undef_chain_list_type *new = 1935268976Sjhb ((ldlang_undef_chain_list_type *) 1936268976Sjhb stat_alloc (sizeof (ldlang_undef_chain_list_type))); 1937268976Sjhb 1938268976Sjhb new->next = ldlang_undef_chain_list_head; 1939270159Sgrehan ldlang_undef_chain_list_head = new; 1940268976Sjhb 1941268976Sjhb new->name = buystring (name); 1942248389Sneel} 1943241982Sneel 1944221828Sgrehan/* Run through the list of undefineds created above and place them 1945241982Sneel into the linker hash table as undefined symbols belonging to the 1946221828Sgrehan script file. 1947241982Sneel*/ 1948221828Sgrehanstatic void 1949241982Sneellang_place_undefineds () 1950221828Sgrehan{ 1951221828Sgrehan ldlang_undef_chain_list_type *ptr; 1952241982Sneel 1953241982Sneel for (ptr = ldlang_undef_chain_list_head; 1954241982Sneel ptr != (ldlang_undef_chain_list_type *) NULL; 1955266339Sjhb ptr = ptr->next) 1956241982Sneel { 1957221828Sgrehan struct bfd_link_hash_entry *h; 1958221828Sgrehan 1959221828Sgrehan h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true); 1960241982Sneel if (h == (struct bfd_link_hash_entry *) NULL) 1961241982Sneel einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); 1962241982Sneel if (h->type == bfd_link_hash_new) 1963241982Sneel { 1964241982Sneel h->type = bfd_link_hash_undefined; 1965241982Sneel h->u.undef.abfd = NULL; 1966241982Sneel bfd_link_add_undef (link_info.hash, h); 1967241982Sneel } 1968241982Sneel } 1969241982Sneel} 1970241982Sneel 1971241982Sneel/* Open input files and attatch to output sections */ 1972241982Sneelstatic void 1973241982Sneelmap_input_to_output_sections (s, target, output_section_statement) 1974241982Sneel lang_statement_union_type * s; 1975241982Sneel CONST char *target; 1976241982Sneel lang_output_section_statement_type * output_section_statement; 1977241982Sneel{ 1978241982Sneel for (; s != (lang_statement_union_type *) NULL; s = s->next) 1979241982Sneel { 1980241982Sneel switch (s->header.type) 1981241982Sneel { 1982241982Sneel 1983241982Sneel 1984241982Sneel case lang_wild_statement_enum: 1985241982Sneel wild (&s->wild_statement, s->wild_statement.section_name, 1986241982Sneel s->wild_statement.filename, target, 1987241982Sneel output_section_statement); 1988241982Sneel 1989268891Sjhb break; 1990268891Sjhb case lang_constructors_statement_enum: 1991241982Sneel map_input_to_output_sections (constructor_list.head, 1992268891Sjhb target, 1993268891Sjhb output_section_statement); 1994268891Sjhb break; 1995268891Sjhb case lang_output_section_statement_enum: 1996268891Sjhb map_input_to_output_sections (s->output_section_statement.children.head, 1997268891Sjhb target, 1998268891Sjhb &s->output_section_statement); 1999268891Sjhb break; 2000268891Sjhb case lang_output_statement_enum: 2001268891Sjhb break; 2002268891Sjhb case lang_target_statement_enum: 2003268891Sjhb target = s->target_statement.target; 2004268891Sjhb break; 2005268891Sjhb case lang_group_statement_enum: 2006268891Sjhb map_input_to_output_sections (s->group_statement.children.head, 2007268891Sjhb target, 2008268891Sjhb output_section_statement); 2009268891Sjhb break; 2010268891Sjhb case lang_fill_statement_enum: 2011268891Sjhb case lang_input_section_enum: 2012268891Sjhb case lang_object_symbols_statement_enum: 2013268891Sjhb case lang_data_statement_enum: 2014268891Sjhb case lang_reloc_statement_enum: 2015268891Sjhb case lang_padding_statement_enum: 2016268891Sjhb case lang_input_statement_enum: 2017268891Sjhb if (output_section_statement != NULL 2018268891Sjhb && output_section_statement->bfd_section == NULL) 2019268891Sjhb init_os (output_section_statement); 2020268891Sjhb break; 2021268891Sjhb case lang_assignment_statement_enum: 2022268891Sjhb if (output_section_statement != NULL 2023268891Sjhb && output_section_statement->bfd_section == NULL) 2024268891Sjhb init_os (output_section_statement); 2025268891Sjhb 2026268891Sjhb /* Make sure that any sections mentioned in the assignment 2027268891Sjhb are initialized. */ 2028268891Sjhb exp_init_os (s->assignment_statement.exp); 2029268891Sjhb break; 2030268891Sjhb case lang_afile_asection_pair_statement_enum: 2031268891Sjhb FAIL (); 2032268891Sjhb break; 2033268891Sjhb case lang_address_statement_enum: 2034268891Sjhb /* Mark the specified section with the supplied address */ 2035268891Sjhb { 2036268891Sjhb lang_output_section_statement_type *os = 2037221828Sgrehan lang_output_section_statement_lookup 2038221828Sgrehan (s->address_statement.section_name); 2039221828Sgrehan 2040221828Sgrehan if (os->bfd_section == NULL) 2041221828Sgrehan init_os (os); 2042221828Sgrehan os->addr_tree = s->address_statement.address; 2043221828Sgrehan } 2044221828Sgrehan break; 2045221828Sgrehan } 2046221828Sgrehan } 2047221828Sgrehan} 2048221828Sgrehan 2049221828Sgrehanstatic void 2050221828Sgrehanprint_output_section_statement (output_section_statement) 2051221828Sgrehan lang_output_section_statement_type * output_section_statement; 2052221828Sgrehan{ 2053221828Sgrehan asection *section = output_section_statement->bfd_section; 2054221828Sgrehan int len; 2055221828Sgrehan 2056221828Sgrehan if (output_section_statement != abs_output_section) 2057221828Sgrehan { 2058221828Sgrehan minfo ("\n%s", output_section_statement->name); 2059221828Sgrehan 2060221828Sgrehan if (section != NULL) 2061221828Sgrehan { 2062221828Sgrehan print_dot = section->vma; 2063221828Sgrehan 2064221828Sgrehan len = strlen (output_section_statement->name); 2065221828Sgrehan if (len >= SECTION_NAME_MAP_LENGTH - 1) 2066261088Sjhb { 2067261088Sjhb print_nl (); 2068261088Sjhb len = 0; 2069261088Sjhb } 2070261088Sjhb while (len < SECTION_NAME_MAP_LENGTH) 2071261088Sjhb { 2072261088Sjhb print_space (); 2073261088Sjhb ++len; 2074261088Sjhb } 2075261088Sjhb 2076261088Sjhb minfo ("0x%V %W", section->vma, section->_raw_size); 2077261088Sjhb 2078261088Sjhb if (output_section_statement->load_base != NULL) 2079261088Sjhb { 2080221828Sgrehan bfd_vma addr; 2081221828Sgrehan 2082221828Sgrehan addr = exp_get_abs_int (output_section_statement->load_base, 0, 2083246188Sneel "load base", lang_final_phase_enum); 2084246188Sneel minfo (_(" load address 0x%V"), addr); 2085221828Sgrehan } 2086221828Sgrehan } 2087221828Sgrehan 2088246188Sneel print_nl (); 2089246188Sneel } 2090246188Sneel 2091246188Sneel print_statement_list (output_section_statement->children.head, 2092246188Sneel output_section_statement); 2093246188Sneel} 2094246188Sneel 2095221828Sgrehanstatic void 2096246188Sneelprint_assignment (assignment, output_section) 2097246188Sneel lang_assignment_statement_type * assignment; 2098246188Sneel lang_output_section_statement_type * output_section; 2099221828Sgrehan{ 2100246188Sneel int i; 2101246188Sneel etree_value_type result; 2102246188Sneel 2103246188Sneel for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) 2104246188Sneel print_space (); 2105221828Sgrehan 2106246188Sneel result = exp_fold_tree (assignment->exp->assign.src, output_section, 2107246188Sneel lang_final_phase_enum, print_dot, &print_dot); 2108246188Sneel if (result.valid_p) 2109246188Sneel minfo ("0x%V", result.value + result.section->bfd_section->vma); 2110246188Sneel else 2111221828Sgrehan { 2112246188Sneel minfo ("*undef* "); 2113246188Sneel#ifdef BFD64 2114221828Sgrehan minfo (" "); 2115246188Sneel#endif 2116246188Sneel } 2117246188Sneel 2118221828Sgrehan minfo (" "); 2119221828Sgrehan 2120221828Sgrehan exp_print_tree (assignment->exp); 2121221828Sgrehan 2122221828Sgrehan print_nl (); 2123221828Sgrehan} 2124221828Sgrehan 2125221828Sgrehanstatic void 2126221828Sgrehanprint_input_statement (statm) 2127221828Sgrehan lang_input_statement_type * statm; 2128221828Sgrehan{ 2129241489Sneel if (statm->filename != (char *) NULL) 2130266393Sjhb { 2131266393Sjhb fprintf (config.map_file, "LOAD %s\n", statm->filename); 2132221828Sgrehan } 2133241489Sneel} 2134221828Sgrehan 2135221828Sgrehan/* Print all symbols defined in a particular section. This is called 2136221828Sgrehan via bfd_link_hash_traverse. */ 2137221828Sgrehan 2138221828Sgrehanstatic boolean 2139221828Sgrehanprint_one_symbol (hash_entry, ptr) 2140221828Sgrehan struct bfd_link_hash_entry *hash_entry; 2141241489Sneel PTR ptr; 2142284900Sneel{ 2143241489Sneel asection *sec = (asection *) ptr; 2144241489Sneel 2145241489Sneel if ((hash_entry->type == bfd_link_hash_defined 2146221828Sgrehan || hash_entry->type == bfd_link_hash_defweak) 2147221828Sgrehan && sec == hash_entry->u.def.section) 2148241489Sneel { 2149249879Sgrehan int i; 2150221828Sgrehan 2151221828Sgrehan for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) 2152241489Sneel print_space (); 2153221828Sgrehan minfo ("0x%V ", 2154221828Sgrehan (hash_entry->u.def.value 2155221828Sgrehan + hash_entry->u.def.section->output_offset 2156221828Sgrehan + hash_entry->u.def.section->output_section->vma)); 2157221828Sgrehan 2158221828Sgrehan minfo (" %T\n", hash_entry->root.string); 2159241489Sneel } 2160241489Sneel 2161249879Sgrehan return true; 2162249879Sgrehan} 2163241489Sneel 2164221828Sgrehan/* Print information about an input section to the map file. */ 2165241489Sneel 2166221828Sgrehanstatic void 2167221828Sgrehanprint_input_section (in) 2168270070Sgrehan lang_input_section_type * in; 2169221828Sgrehan{ 2170221828Sgrehan asection *i = in->section; 2171221828Sgrehan bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size; 2172270070Sgrehan unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2173270070Sgrehan ldfile_output_machine); 2174266339Sjhb if (size != 0) 2175270070Sgrehan { 2176270070Sgrehan print_space (); 2177270070Sgrehan 2178266339Sjhb minfo ("%s", i->name); 2179266339Sjhb 2180270070Sgrehan if (i->output_section != NULL) 2181221828Sgrehan { 2182221828Sgrehan int len; 2183223621Sgrehan 2184221828Sgrehan len = 1 + strlen (i->name); 2185221828Sgrehan if (len >= SECTION_NAME_MAP_LENGTH - 1) 2186221828Sgrehan { 2187221828Sgrehan print_nl (); 2188221828Sgrehan len = 0; 2189221828Sgrehan } 2190270070Sgrehan while (len < SECTION_NAME_MAP_LENGTH) 2191270070Sgrehan { 2192270070Sgrehan print_space (); 2193270070Sgrehan ++len; 2194270070Sgrehan } 2195270070Sgrehan 2196270070Sgrehan minfo ("0x%V %W %B\n", 2197221828Sgrehan i->output_section->vma + i->output_offset, size / opb, 2198221828Sgrehan i->owner); 2199221828Sgrehan 2200221828Sgrehan if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size) 2201221828Sgrehan { 2202221828Sgrehan len = SECTION_NAME_MAP_LENGTH + 3; 2203240922Sneel#ifdef BFD64 2204240922Sneel len += 16; 2205240922Sneel#else 2206240922Sneel len += 8; 2207240922Sneel#endif 2208240922Sneel while (len > 0) 2209240922Sneel { 2210240922Sneel print_space (); 2211240922Sneel --len; 2212240922Sneel } 2213240922Sneel 2214240922Sneel minfo (_("%W (size before relaxing)\n"), i->_raw_size); 2215240922Sneel } 2216240922Sneel 2217240922Sneel bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i); 2218240922Sneel 2219240922Sneel print_dot = i->output_section->vma + i->output_offset + size / opb; 2220240922Sneel } 2221248392Sneel } 2222240922Sneel} 2223240922Sneel 2224240922Sneelstatic void 2225240922Sneelprint_fill_statement (fill) 2226240943Sneel lang_fill_statement_type * fill; 2227240943Sneel{ 2228240922Sneel fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill); 2229240922Sneel} 2230241489Sneel 2231262350Sjhbstatic void 2232262350Sjhbprint_data_statement (data) 2233262350Sjhb lang_data_statement_type * data; 2234262350Sjhb{ 2235262350Sjhb int i; 2236262350Sjhb bfd_vma addr; 2237262350Sjhb bfd_size_type size; 2238284900Sneel const char *name; 2239284900Sneel unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2240241489Sneel ldfile_output_machine); 2241241489Sneel 2242241489Sneel for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) 2243241489Sneel print_space (); 2244266393Sjhb 2245266393Sjhb addr = data->output_vma; 2246266339Sjhb if (data->output_section != NULL) 2247266393Sjhb addr += data->output_section->vma; 2248266339Sjhb 2249266339Sjhb switch (data->type) 2250266393Sjhb { 2251266339Sjhb default: 2252266393Sjhb abort (); 2253266393Sjhb case BYTE: 2254266393Sjhb size = BYTE_SIZE; 2255266393Sjhb name = "BYTE"; 2256266393Sjhb break; 2257266393Sjhb case SHORT: 2258266393Sjhb size = SHORT_SIZE; 2259266393Sjhb name = "SHORT"; 2260266339Sjhb break; 2261266393Sjhb case LONG: 2262266393Sjhb size = LONG_SIZE; 2263266393Sjhb name = "LONG"; 2264266393Sjhb break; 2265266393Sjhb case QUAD: 2266242065Sneel size = QUAD_SIZE; 2267284900Sneel name = "QUAD"; 2268284900Sneel break; 2269284900Sneel case SQUAD: 2270284900Sneel size = QUAD_SIZE; 2271284900Sneel name = "SQUAD"; 2272284900Sneel break; 2273284900Sneel } 2274284900Sneel 2275284900Sneel minfo ("0x%V %W %s 0x%v", addr, size, name, data->value); 2276242065Sneel 2277241489Sneel if (data->exp->type.node_class != etree_value) 2278256072Sneel { 2279256072Sneel print_space (); 2280256072Sneel exp_print_tree (data->exp); 2281256072Sneel } 2282256072Sneel 2283256072Sneel print_nl (); 2284256072Sneel 2285261088Sjhb print_dot = addr + size / opb; 2286261088Sjhb 2287261088Sjhb} 2288261088Sjhb 2289261088Sjhb/* Print an address statement. These are generated by options like 2290261088Sjhb -Ttext. */ 2291261088Sjhb 2292261088Sjhbstatic void 2293261088Sjhbprint_address_statement (address) 2294266339Sjhb lang_address_statement_type *address; 2295266339Sjhb{ 2296266339Sjhb minfo (_("Address of section %s set to "), address->section_name); 2297266339Sjhb exp_print_tree (address->address); 2298266339Sjhb print_nl (); 2299266339Sjhb} 2300266339Sjhb 2301266339Sjhb/* Print a reloc statement. */ 2302266339Sjhb 2303266339Sjhbstatic void 2304266339Sjhbprint_reloc_statement (reloc) 2305266339Sjhb lang_reloc_statement_type *reloc; 2306266339Sjhb{ 2307266339Sjhb int i; 2308266339Sjhb bfd_vma addr; 2309266339Sjhb bfd_size_type size; 2310266339Sjhb unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2311266339Sjhb ldfile_output_machine); 2312266339Sjhb 2313266339Sjhb for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) 2314266339Sjhb print_space (); 2315266339Sjhb 2316266339Sjhb addr = reloc->output_vma; 2317266339Sjhb if (reloc->output_section != NULL) 2318266339Sjhb addr += reloc->output_section->vma; 2319266339Sjhb 2320266339Sjhb size = bfd_get_reloc_size (reloc->howto); 2321266339Sjhb 2322266339Sjhb minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name); 2323266339Sjhb 2324266339Sjhb if (reloc->name != NULL) 2325266339Sjhb minfo ("%s+", reloc->name); 2326266339Sjhb else 2327266339Sjhb minfo ("%s+", reloc->section->name); 2328266339Sjhb 2329266339Sjhb exp_print_tree (reloc->addend_exp); 2330266339Sjhb 2331266339Sjhb print_nl (); 2332266339Sjhb 2333266339Sjhb print_dot = addr + size / opb; 2334266339Sjhb} 2335266339Sjhb 2336266339Sjhbstatic void 2337266339Sjhbprint_padding_statement (s) 2338266339Sjhb lang_padding_statement_type *s; 2339266339Sjhb{ 2340266339Sjhb int len; 2341266339Sjhb bfd_vma addr; 2342268891Sjhb unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2343268891Sjhb ldfile_output_machine); 2344268891Sjhb 2345268891Sjhb minfo (" *fill*"); 2346268891Sjhb 2347268891Sjhb len = sizeof " *fill*" - 1; 2348268891Sjhb while (len < SECTION_NAME_MAP_LENGTH) 2349268891Sjhb { 2350268891Sjhb print_space (); 2351268891Sjhb ++len; 2352268891Sjhb } 2353268891Sjhb 2354268976Sjhb addr = s->output_offset; 2355276429Sneel if (s->output_section != NULL) 2356276429Sneel addr += s->output_section->vma; 2357276429Sneel minfo ("0x%V %W", addr, s->size); 2358276429Sneel 2359276429Sneel if (s->fill != 0) 2360276429Sneel minfo (" %u", s->fill); 2361276429Sneel 2362284894Sneel print_nl (); 2363284894Sneel 2364284894Sneel print_dot = addr + s->size / opb; 2365284894Sneel} 2366284894Sneel 2367284894Sneelstatic void 2368284894Sneelprint_wild_statement (w, os) 2369268976Sjhb lang_wild_statement_type * w; 2370268976Sjhb lang_output_section_statement_type * os; 2371268976Sjhb{ 2372268976Sjhb print_space (); 2373268976Sjhb 2374268976Sjhb if (w->filenames_sorted) 2375268976Sjhb minfo ("SORT("); 2376268976Sjhb if (w->exclude_filename_list != NULL) 2377268976Sjhb { 2378268976Sjhb name_list *tmp; 2379268976Sjhb minfo ("EXCLUDE_FILE ( %s", w->exclude_filename_list->name); 2380268976Sjhb for (tmp=w->exclude_filename_list->next; tmp; tmp = tmp->next) 2381268976Sjhb minfo (", %s", tmp->name); 2382268976Sjhb minfo (")"); 2383268976Sjhb } 2384268976Sjhb if (w->filename != NULL) 2385270074Sgrehan minfo ("%s", w->filename); 2386270159Sgrehan else 2387270159Sgrehan minfo ("*"); 2388270159Sgrehan if (w->filenames_sorted) 2389270159Sgrehan minfo (")"); 2390270159Sgrehan 2391270074Sgrehan minfo ("("); 2392270159Sgrehan if (w->sections_sorted) 2393270159Sgrehan minfo ("SORT("); 2394270159Sgrehan if (w->section_name != NULL) 2395270159Sgrehan minfo ("%s", w->section_name); 2396270159Sgrehan else 2397270159Sgrehan minfo ("*"); 2398270159Sgrehan if (w->sections_sorted) 2399270159Sgrehan minfo (")"); 2400270159Sgrehan minfo (")"); 2401270159Sgrehan 2402284900Sneel print_nl (); 2403270159Sgrehan 2404270159Sgrehan print_statement_list (w->children.head, os); 2405270159Sgrehan} 2406270159Sgrehan 2407270159Sgrehan/* Print a group statement. */ 2408270159Sgrehan 2409270159Sgrehanstatic void 2410270159Sgrehanprint_group (s, os) 2411270159Sgrehan lang_group_statement_type *s; 2412270159Sgrehan lang_output_section_statement_type *os; 2413270159Sgrehan{ 2414270159Sgrehan fprintf (config.map_file, "START GROUP\n"); 2415284900Sneel print_statement_list (s->children.head, os); 2416284900Sneel fprintf (config.map_file, "END GROUP\n"); 2417270159Sgrehan} 2418270159Sgrehan 2419270159Sgrehan/* Print the list of statements in S. 2420270159Sgrehan This can be called for any statement type. */ 2421270159Sgrehan 2422270159Sgrehanstatic void 2423270159Sgrehanprint_statement_list (s, os) 2424270159Sgrehan lang_statement_union_type *s; 2425270159Sgrehan lang_output_section_statement_type *os; 2426270159Sgrehan{ 2427270159Sgrehan while (s != NULL) 2428270159Sgrehan { 2429270159Sgrehan print_statement (s, os); 2430270159Sgrehan s = s->next; 2431270159Sgrehan } 2432270159Sgrehan} 2433270159Sgrehan 2434270159Sgrehan/* Print the first statement in statement list S. 2435270159Sgrehan This can be called for any statement type. */ 2436270159Sgrehan 2437270159Sgrehanstatic void 2438284900Sneelprint_statement (s, os) 2439270159Sgrehan lang_statement_union_type *s; 2440284900Sneel lang_output_section_statement_type *os; 2441270159Sgrehan{ 2442270159Sgrehan switch (s->header.type) 2443270159Sgrehan { 2444270159Sgrehan default: 2445270159Sgrehan fprintf (config.map_file, _("Fail with %d\n"), s->header.type); 2446270159Sgrehan FAIL (); 2447270159Sgrehan break; 2448270159Sgrehan case lang_constructors_statement_enum: 2449270159Sgrehan if (constructor_list.head != NULL) 2450270159Sgrehan { 2451270159Sgrehan if (constructors_sorted) 2452270159Sgrehan minfo (" SORT (CONSTRUCTORS)\n"); 2453270159Sgrehan else 2454270159Sgrehan minfo (" CONSTRUCTORS\n"); 2455270159Sgrehan print_statement_list (constructor_list.head, os); 2456270159Sgrehan } 2457270159Sgrehan break; 2458270159Sgrehan case lang_wild_statement_enum: 2459270159Sgrehan print_wild_statement (&s->wild_statement, os); 2460270159Sgrehan break; 2461270159Sgrehan case lang_address_statement_enum: 2462270159Sgrehan print_address_statement (&s->address_statement); 2463270159Sgrehan break; 2464270159Sgrehan case lang_object_symbols_statement_enum: 2465270159Sgrehan minfo (" CREATE_OBJECT_SYMBOLS\n"); 2466270159Sgrehan break; 2467270159Sgrehan case lang_fill_statement_enum: 2468270159Sgrehan print_fill_statement (&s->fill_statement); 2469270159Sgrehan break; 2470270159Sgrehan case lang_data_statement_enum: 2471270159Sgrehan print_data_statement (&s->data_statement); 2472270159Sgrehan break; 2473270159Sgrehan case lang_reloc_statement_enum: 2474270159Sgrehan print_reloc_statement (&s->reloc_statement); 2475270159Sgrehan break; 2476270159Sgrehan case lang_input_section_enum: 2477270159Sgrehan print_input_section (&s->input_section); 2478270159Sgrehan break; 2479270074Sgrehan case lang_padding_statement_enum: 2480270074Sgrehan print_padding_statement (&s->padding_statement); 2481270074Sgrehan break; 2482270074Sgrehan case lang_output_section_statement_enum: 2483270074Sgrehan print_output_section_statement (&s->output_section_statement); 2484270074Sgrehan break; 2485270074Sgrehan case lang_assignment_statement_enum: 2486270074Sgrehan print_assignment (&s->assignment_statement, os); 2487270074Sgrehan break; 2488270074Sgrehan case lang_target_statement_enum: 2489270074Sgrehan fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); 2490270074Sgrehan break; 2491270074Sgrehan case lang_output_statement_enum: 2492270074Sgrehan minfo ("OUTPUT(%s", s->output_statement.name); 2493270074Sgrehan if (output_target != NULL) 2494270074Sgrehan minfo (" %s", output_target); 2495270074Sgrehan minfo (")\n"); 2496270074Sgrehan break; 2497270074Sgrehan case lang_input_statement_enum: 2498270074Sgrehan print_input_statement (&s->input_statement); 2499270074Sgrehan break; 2500270074Sgrehan case lang_group_statement_enum: 2501270074Sgrehan print_group (&s->group_statement, os); 2502270074Sgrehan break; 2503270074Sgrehan case lang_afile_asection_pair_statement_enum: 2504270074Sgrehan FAIL (); 2505270074Sgrehan break; 2506270074Sgrehan } 2507270074Sgrehan} 2508 2509static void 2510print_statements () 2511{ 2512 print_statement_list (statement_list.head, abs_output_section); 2513} 2514 2515/* Print the first N statements in statement list S to STDERR. 2516 If N == 0, nothing is printed. 2517 If N < 0, the entire list is printed. 2518 Intended to be called from GDB. */ 2519 2520void 2521dprint_statement (s, n) 2522 lang_statement_union_type * s; 2523 int n; 2524{ 2525 FILE *map_save = config.map_file; 2526 2527 config.map_file = stderr; 2528 2529 if (n < 0) 2530 print_statement_list (s, abs_output_section); 2531 else 2532 { 2533 while (s && --n >= 0) 2534 { 2535 print_statement (s, abs_output_section); 2536 s = s->next; 2537 } 2538 } 2539 2540 config.map_file = map_save; 2541} 2542 2543static bfd_vma 2544insert_pad (this_ptr, fill, power, output_section_statement, dot) 2545 lang_statement_union_type ** this_ptr; 2546 fill_type fill; 2547 unsigned int power; 2548 asection * output_section_statement; 2549 bfd_vma dot; 2550{ 2551 /* Align this section first to the 2552 input sections requirement, then 2553 to the output section's requirement. 2554 If this alignment is > than any seen before, 2555 then record it too. Perform the alignment by 2556 inserting a magic 'padding' statement. 2557 */ 2558 2559 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2560 ldfile_output_machine); 2561 unsigned int alignment_needed = align_power (dot, power) - dot; 2562 2563 if (alignment_needed != 0) 2564 { 2565 lang_statement_union_type *new = 2566 ((lang_statement_union_type *) 2567 stat_alloc (sizeof (lang_padding_statement_type))); 2568 2569 /* Link into existing chain */ 2570 new->header.next = *this_ptr; 2571 *this_ptr = new; 2572 new->header.type = lang_padding_statement_enum; 2573 new->padding_statement.output_section = output_section_statement; 2574 new->padding_statement.output_offset = 2575 dot - output_section_statement->vma; 2576 new->padding_statement.fill = fill; 2577 new->padding_statement.size = alignment_needed * opb; 2578 } 2579 2580 2581 /* Remember the most restrictive alignment */ 2582 if (power > output_section_statement->alignment_power) 2583 { 2584 output_section_statement->alignment_power = power; 2585 } 2586 output_section_statement->_raw_size += alignment_needed * opb; 2587 2588 return dot + alignment_needed; 2589} 2590 2591/* Work out how much this section will move the dot point */ 2592static bfd_vma 2593size_input_section (this_ptr, output_section_statement, fill, dot, relax) 2594 lang_statement_union_type ** this_ptr; 2595 lang_output_section_statement_type * output_section_statement; 2596 fill_type fill; 2597 bfd_vma dot; 2598 boolean relax ATTRIBUTE_UNUSED; 2599{ 2600 lang_input_section_type *is = &((*this_ptr)->input_section); 2601 asection *i = is->section; 2602 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2603 ldfile_output_machine); 2604 2605 if (is->ifile->just_syms_flag == false) 2606 { 2607 if (output_section_statement->subsection_alignment != -1) 2608 i->alignment_power = 2609 output_section_statement->subsection_alignment; 2610 2611 dot = insert_pad (this_ptr, fill, i->alignment_power, 2612 output_section_statement->bfd_section, dot); 2613 2614 /* Remember where in the output section this input section goes */ 2615 2616 i->output_offset = dot - output_section_statement->bfd_section->vma; 2617 2618 /* Mark how big the output section must be to contain this now 2619 */ 2620 if (i->_cooked_size != 0) 2621 dot += i->_cooked_size / opb; 2622 else 2623 dot += i->_raw_size / opb; 2624 output_section_statement->bfd_section->_raw_size = 2625 (dot - output_section_statement->bfd_section->vma) * opb; 2626 } 2627 else 2628 { 2629 i->output_offset = i->vma - output_section_statement->bfd_section->vma; 2630 } 2631 2632 return dot; 2633} 2634 2635#define IGNORE_SECTION(bfd, s) \ 2636 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \ 2637 || bfd_section_size (bfd, s) == 0) 2638 2639/* Check to see if any allocated sections overlap with other allocated 2640 sections. This can happen when the linker script specifically specifies 2641 the output section addresses of the two sections. */ 2642static void 2643lang_check_section_addresses () 2644{ 2645 asection * s; 2646 unsigned opb = bfd_octets_per_byte (output_bfd); 2647 2648 /* Scan all sections in the output list. */ 2649 for (s = output_bfd->sections; s != NULL; s = s->next) 2650 { 2651 asection * os; 2652 2653 /* Ignore sections which are not loaded or which have no contents. */ 2654 if (IGNORE_SECTION (output_bfd, s)) 2655 continue; 2656 2657 /* Once we reach section 's' stop our seach. This prevents two 2658 warning messages from being produced, one for 'section A overlaps 2659 section B' and one for 'section B overlaps section A'. */ 2660 for (os = output_bfd->sections; os != s; os = os->next) 2661 { 2662 bfd_vma s_start; 2663 bfd_vma s_end; 2664 bfd_vma os_start; 2665 bfd_vma os_end; 2666 2667 /* Only consider loadable sections with real contents. */ 2668 if (IGNORE_SECTION (output_bfd, os)) 2669 continue; 2670 2671 /* We must check the sections' LMA addresses not their 2672 VMA addresses because overlay sections can have 2673 overlapping VMAs but they must have distinct LMAs. */ 2674 s_start = bfd_section_lma (output_bfd, s); 2675 os_start = bfd_section_lma (output_bfd, os); 2676 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1; 2677 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1; 2678 2679 /* Look for an overlap. */ 2680 if ((s_end < os_start) || (s_start > os_end)) 2681 continue; 2682 2683 einfo ( 2684_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"), 2685 s->name, s_start, s_end, os->name, os_start, os_end); 2686 2687 /* Once we have found one overlap for this section, 2688 stop looking for others. */ 2689 break; 2690 } 2691 } 2692} 2693 2694/* This variable indicates whether bfd_relax_section should be called 2695 again. */ 2696 2697static boolean relax_again; 2698 2699/* Make sure the new address is within the region. We explicitly permit the 2700 current address to be at the exact end of the region when the address is 2701 non-zero, in case the region is at the end of addressable memory and the 2702 calculation wraps around. */ 2703 2704static void 2705os_region_check (os, region, tree, base) 2706 lang_output_section_statement_type *os; 2707 struct memory_region_struct *region; 2708 etree_type *tree; 2709 bfd_vma base; 2710{ 2711 if ((region->current < region->origin 2712 || (region->current - region->origin > region->length)) 2713 && ((region->current != region->origin + region->length) 2714 || base == 0)) 2715 { 2716 if (tree != (etree_type *) NULL) 2717 { 2718 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"), 2719 region->current, 2720 os->bfd_section->owner, 2721 os->bfd_section->name, 2722 region->name); 2723 } 2724 else 2725 { 2726 einfo (_("%X%P: region %s is full (%B section %s)\n"), 2727 region->name, 2728 os->bfd_section->owner, 2729 os->bfd_section->name); 2730 } 2731 /* Reset the region pointer. */ 2732 region->current = region->origin; 2733 } 2734} 2735 2736/* Set the sizes for all the output sections. */ 2737 2738bfd_vma 2739lang_size_sections (s, output_section_statement, prev, fill, dot, relax) 2740 lang_statement_union_type * s; 2741 lang_output_section_statement_type * output_section_statement; 2742 lang_statement_union_type ** prev; 2743 fill_type fill; 2744 bfd_vma dot; 2745 boolean relax; 2746{ 2747 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 2748 ldfile_output_machine); 2749 2750 /* Size up the sections from their constituent parts. */ 2751 for (; s != (lang_statement_union_type *) NULL; s = s->next) 2752 { 2753 switch (s->header.type) 2754 { 2755 case lang_output_section_statement_enum: 2756 { 2757 bfd_vma after; 2758 lang_output_section_statement_type *os = &s->output_section_statement; 2759 2760 if (os->bfd_section == NULL) 2761 /* This section was never actually created. */ 2762 break; 2763 2764 /* If this is a COFF shared library section, use the size and 2765 address from the input section. FIXME: This is COFF 2766 specific; it would be cleaner if there were some other way 2767 to do this, but nothing simple comes to mind. */ 2768 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) 2769 { 2770 asection * input; 2771 2772 if (os->children.head == NULL 2773 || os->children.head->next != NULL 2774 || os->children.head->header.type != lang_input_section_enum) 2775 einfo (_("%P%X: Internal error on COFF shared library section %s\n"), 2776 os->name); 2777 2778 input = os->children.head->input_section.section; 2779 bfd_set_section_vma (os->bfd_section->owner, 2780 os->bfd_section, 2781 bfd_section_vma (input->owner, input)); 2782 os->bfd_section->_raw_size = input->_raw_size; 2783 break; 2784 } 2785 2786 if (bfd_is_abs_section (os->bfd_section)) 2787 { 2788 /* No matter what happens, an abs section starts at zero. */ 2789 ASSERT (os->bfd_section->vma == 0); 2790 } 2791 else 2792 { 2793 if (os->addr_tree == (etree_type *) NULL) 2794 { 2795 /* No address specified for this section, get one 2796 from the region specification. */ 2797 if (os->region == (lang_memory_region_type *) NULL 2798 || (((bfd_get_section_flags (output_bfd, os->bfd_section) 2799 & (SEC_ALLOC | SEC_LOAD)) != 0) 2800 && os->region->name[0] == '*' 2801 && strcmp (os->region->name, "*default*") == 0)) 2802 { 2803 os->region = lang_memory_default (os->bfd_section); 2804 } 2805 2806 /* If a loadable section is using the default memory 2807 region, and some non default memory regions were 2808 defined, issue a warning. */ 2809 if ((bfd_get_section_flags (output_bfd, os->bfd_section) 2810 & (SEC_ALLOC | SEC_LOAD)) != 0 2811 && ! link_info.relocateable 2812 && strcmp (os->region->name, "*default*") == 0 2813 && lang_memory_region_list != NULL 2814 && (strcmp (lang_memory_region_list->name, "*default*") != 0 2815 || lang_memory_region_list->next != NULL)) 2816 einfo (_("%P: warning: no memory region specified for section `%s'\n"), 2817 bfd_get_section_name (output_bfd, os->bfd_section)); 2818 2819 dot = os->region->current; 2820 2821 if (os->section_alignment == -1) 2822 { 2823 bfd_vma olddot; 2824 2825 olddot = dot; 2826 dot = align_power (dot, os->bfd_section->alignment_power); 2827 2828 if (dot != olddot && config.warn_section_align) 2829 einfo (_("%P: warning: changing start of section %s by %u bytes\n"), 2830 os->name, (unsigned int) (dot - olddot)); 2831 } 2832 } 2833 else 2834 { 2835 etree_value_type r; 2836 2837 r = exp_fold_tree (os->addr_tree, 2838 abs_output_section, 2839 lang_allocating_phase_enum, 2840 dot, &dot); 2841 if (r.valid_p == false) 2842 { 2843 einfo (_("%F%S: non constant address expression for section %s\n"), 2844 os->name); 2845 } 2846 dot = r.value + r.section->bfd_section->vma; 2847 } 2848 2849 /* The section starts here. 2850 First, align to what the section needs. */ 2851 2852 if (os->section_alignment != -1) 2853 dot = align_power (dot, os->section_alignment); 2854 2855 bfd_set_section_vma (0, os->bfd_section, dot); 2856 2857 os->bfd_section->output_offset = 0; 2858 } 2859 2860 (void) lang_size_sections (os->children.head, os, &os->children.head, 2861 os->fill, dot, relax); 2862 2863 /* put the section within the requested block size, or align at 2864 the block boundary */ 2865 after = ALIGN_N (os->bfd_section->vma 2866 + os->bfd_section->_raw_size / opb, 2867 /* The coercion here is important, see ld.h. */ 2868 (bfd_vma) os->block_value); 2869 2870 if (bfd_is_abs_section (os->bfd_section)) 2871 ASSERT (after == os->bfd_section->vma); 2872 else 2873 os->bfd_section->_raw_size = 2874 (after - os->bfd_section->vma) * opb; 2875 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb; 2876 os->processed = true; 2877 2878 /* Update dot in the region ? 2879 We only do this if the section is going to be allocated, 2880 since unallocated sections do not contribute to the region's 2881 overall size in memory. 2882 2883 If the SEC_NEVER_LOAD bit is not set, it will affect the 2884 addresses of sections after it. We have to update 2885 dot. */ 2886 if (os->region != (lang_memory_region_type *) NULL 2887 && ((bfd_get_section_flags (output_bfd, os->bfd_section) 2888 & SEC_NEVER_LOAD) == 0 2889 || (bfd_get_section_flags (output_bfd, os->bfd_section) 2890 & (SEC_ALLOC | SEC_LOAD)))) 2891 { 2892 os->region->current = dot; 2893 2894 /* Make sure the new address is within the region. */ 2895 os_region_check (os, os->region, os->addr_tree, 2896 os->bfd_section->vma); 2897 2898 /* if there's no load address specified, use the run region as 2899 the load region */ 2900 if (os->lma_region == NULL && os->load_base == NULL) 2901 os->lma_region = os->region; 2902 2903 if (os->lma_region != NULL) 2904 { 2905 if (os->load_base != NULL) 2906 { 2907 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n")); 2908 } 2909 else 2910 { 2911 /* don't allocate twice */ 2912 if (os->lma_region != os->region) 2913 { 2914 /* set load_base, which will be handled later */ 2915 os->load_base = exp_intop (os->lma_region->current); 2916 os->lma_region->current += 2917 os->bfd_section->_raw_size / opb; 2918 os_region_check (os, os->lma_region, NULL, 2919 os->bfd_section->lma); 2920 } 2921 } 2922 } 2923 } 2924 } 2925 break; 2926 2927 case lang_constructors_statement_enum: 2928 dot = lang_size_sections (constructor_list.head, 2929 output_section_statement, 2930 &s->wild_statement.children.head, 2931 fill, 2932 dot, relax); 2933 break; 2934 2935 case lang_data_statement_enum: 2936 { 2937 unsigned int size = 0; 2938 2939 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma; 2940 s->data_statement.output_section = 2941 output_section_statement->bfd_section; 2942 2943 switch (s->data_statement.type) 2944 { 2945 default: 2946 abort(); 2947 case QUAD: 2948 case SQUAD: 2949 size = QUAD_SIZE; 2950 break; 2951 case LONG: 2952 size = LONG_SIZE; 2953 break; 2954 case SHORT: 2955 size = SHORT_SIZE; 2956 break; 2957 case BYTE: 2958 size = BYTE_SIZE; 2959 break; 2960 } 2961 if (size < opb) 2962 size = opb; 2963 dot += size / opb; 2964 output_section_statement->bfd_section->_raw_size += size; 2965 /* The output section gets contents, and then we inspect for 2966 any flags set in the input script which override any ALLOC. */ 2967 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS; 2968 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) { 2969 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD; 2970 } 2971 } 2972 break; 2973 2974 case lang_reloc_statement_enum: 2975 { 2976 int size; 2977 2978 s->reloc_statement.output_vma = 2979 dot - output_section_statement->bfd_section->vma; 2980 s->reloc_statement.output_section = 2981 output_section_statement->bfd_section; 2982 size = bfd_get_reloc_size (s->reloc_statement.howto); 2983 dot += size / opb; 2984 output_section_statement->bfd_section->_raw_size += size; 2985 } 2986 break; 2987 2988 case lang_wild_statement_enum: 2989 2990 dot = lang_size_sections (s->wild_statement.children.head, 2991 output_section_statement, 2992 &s->wild_statement.children.head, 2993 2994 fill, dot, relax); 2995 2996 break; 2997 2998 case lang_object_symbols_statement_enum: 2999 link_info.create_object_symbols_section = 3000 output_section_statement->bfd_section; 3001 break; 3002 case lang_output_statement_enum: 3003 case lang_target_statement_enum: 3004 break; 3005 case lang_input_section_enum: 3006 { 3007 asection *i; 3008 3009 i = (*prev)->input_section.section; 3010 if (! relax) 3011 { 3012 if (i->_cooked_size == 0) 3013 i->_cooked_size = i->_raw_size; 3014 } 3015 else 3016 { 3017 boolean again; 3018 3019 if (! bfd_relax_section (i->owner, i, &link_info, &again)) 3020 einfo (_("%P%F: can't relax section: %E\n")); 3021 if (again) 3022 relax_again = true; 3023 } 3024 dot = size_input_section (prev, 3025 output_section_statement, 3026 output_section_statement->fill, 3027 dot, relax); 3028 } 3029 break; 3030 case lang_input_statement_enum: 3031 break; 3032 case lang_fill_statement_enum: 3033 s->fill_statement.output_section = output_section_statement->bfd_section; 3034 3035 fill = s->fill_statement.fill; 3036 break; 3037 case lang_assignment_statement_enum: 3038 { 3039 bfd_vma newdot = dot; 3040 3041 exp_fold_tree (s->assignment_statement.exp, 3042 output_section_statement, 3043 lang_allocating_phase_enum, 3044 dot, 3045 &newdot); 3046 3047 if (newdot != dot) 3048 { 3049 /* The assignment changed dot. Insert a pad. */ 3050 if (output_section_statement == abs_output_section) 3051 { 3052 /* If we don't have an output section, then just adjust 3053 the default memory address. */ 3054 lang_memory_region_lookup ("*default*")->current = newdot; 3055 } 3056 else if (!relax) 3057 { 3058 lang_statement_union_type *new = 3059 ((lang_statement_union_type *) 3060 stat_alloc (sizeof (lang_padding_statement_type))); 3061 3062 /* Link into existing chain. */ 3063 new->header.next = *prev; 3064 *prev = new; 3065 new->header.type = lang_padding_statement_enum; 3066 new->padding_statement.output_section = 3067 output_section_statement->bfd_section; 3068 new->padding_statement.output_offset = 3069 dot - output_section_statement->bfd_section->vma; 3070 new->padding_statement.fill = fill; 3071 new->padding_statement.size = (newdot - dot) * opb; 3072 output_section_statement->bfd_section->_raw_size += 3073 new->padding_statement.size; 3074 } 3075 3076 dot = newdot; 3077 } 3078 } 3079 break; 3080 3081 case lang_padding_statement_enum: 3082 /* If we are relaxing, and this is not the first pass, some 3083 padding statements may have been inserted during previous 3084 passes. We may have to move the padding statement to a new 3085 location if dot has a different value at this point in this 3086 pass than it did at this point in the previous pass. */ 3087 s->padding_statement.output_offset = 3088 dot - output_section_statement->bfd_section->vma; 3089 dot += s->padding_statement.size / opb; 3090 output_section_statement->bfd_section->_raw_size += 3091 s->padding_statement.size; 3092 break; 3093 3094 case lang_group_statement_enum: 3095 dot = lang_size_sections (s->group_statement.children.head, 3096 output_section_statement, 3097 &s->group_statement.children.head, 3098 fill, dot, relax); 3099 break; 3100 3101 default: 3102 FAIL (); 3103 break; 3104 3105 /* This can only get here when relaxing is turned on. */ 3106 3107 case lang_address_statement_enum: 3108 break; 3109 } 3110 prev = &s->header.next; 3111 } 3112 return dot; 3113} 3114 3115bfd_vma 3116lang_do_assignments (s, output_section_statement, fill, dot) 3117 lang_statement_union_type * s; 3118 lang_output_section_statement_type * output_section_statement; 3119 fill_type fill; 3120 bfd_vma dot; 3121{ 3122 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 3123 ldfile_output_machine); 3124 3125 for (; s != (lang_statement_union_type *) NULL; s = s->next) 3126 { 3127 switch (s->header.type) 3128 { 3129 case lang_constructors_statement_enum: 3130 dot = lang_do_assignments (constructor_list.head, 3131 output_section_statement, 3132 fill, 3133 dot); 3134 break; 3135 3136 case lang_output_section_statement_enum: 3137 { 3138 lang_output_section_statement_type *os = 3139 &(s->output_section_statement); 3140 3141 if (os->bfd_section != NULL) 3142 { 3143 dot = os->bfd_section->vma; 3144 (void) lang_do_assignments (os->children.head, os, 3145 os->fill, dot); 3146 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb; 3147 3148 } 3149 if (os->load_base) 3150 { 3151 /* If nothing has been placed into the output section then 3152 it won't have a bfd_section. */ 3153 if (os->bfd_section) 3154 { 3155 os->bfd_section->lma 3156 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum); 3157 } 3158 } 3159 } 3160 break; 3161 case lang_wild_statement_enum: 3162 3163 dot = lang_do_assignments (s->wild_statement.children.head, 3164 output_section_statement, 3165 fill, dot); 3166 3167 break; 3168 3169 case lang_object_symbols_statement_enum: 3170 case lang_output_statement_enum: 3171 case lang_target_statement_enum: 3172#if 0 3173 case lang_common_statement_enum: 3174#endif 3175 break; 3176 case lang_data_statement_enum: 3177 { 3178 etree_value_type value; 3179 3180 value = exp_fold_tree (s->data_statement.exp, 3181 abs_output_section, 3182 lang_final_phase_enum, dot, &dot); 3183 s->data_statement.value = value.value; 3184 if (value.valid_p == false) 3185 einfo (_("%F%P: invalid data statement\n")); 3186 } 3187 { 3188 unsigned int size; 3189 switch (s->data_statement.type) 3190 { 3191 default: 3192 abort(); 3193 case QUAD: 3194 case SQUAD: 3195 size = QUAD_SIZE; 3196 break; 3197 case LONG: 3198 size = LONG_SIZE; 3199 break; 3200 case SHORT: 3201 size = SHORT_SIZE; 3202 break; 3203 case BYTE: 3204 size = BYTE_SIZE; 3205 break; 3206 } 3207 if (size < opb) 3208 size = opb; 3209 dot += size / opb; 3210 } 3211 break; 3212 3213 case lang_reloc_statement_enum: 3214 { 3215 etree_value_type value; 3216 3217 value = exp_fold_tree (s->reloc_statement.addend_exp, 3218 abs_output_section, 3219 lang_final_phase_enum, dot, &dot); 3220 s->reloc_statement.addend_value = value.value; 3221 if (value.valid_p == false) 3222 einfo (_("%F%P: invalid reloc statement\n")); 3223 } 3224 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb; 3225 break; 3226 3227 case lang_input_section_enum: 3228 { 3229 asection *in = s->input_section.section; 3230 3231 if (in->_cooked_size != 0) 3232 dot += in->_cooked_size / opb; 3233 else 3234 dot += in->_raw_size / opb; 3235 } 3236 break; 3237 3238 case lang_input_statement_enum: 3239 break; 3240 case lang_fill_statement_enum: 3241 fill = s->fill_statement.fill; 3242 break; 3243 case lang_assignment_statement_enum: 3244 { 3245 exp_fold_tree (s->assignment_statement.exp, 3246 output_section_statement, 3247 lang_final_phase_enum, 3248 dot, 3249 &dot); 3250 } 3251 3252 break; 3253 case lang_padding_statement_enum: 3254 dot += s->padding_statement.size / opb; 3255 break; 3256 3257 case lang_group_statement_enum: 3258 dot = lang_do_assignments (s->group_statement.children.head, 3259 output_section_statement, 3260 fill, dot); 3261 3262 break; 3263 3264 default: 3265 FAIL (); 3266 break; 3267 case lang_address_statement_enum: 3268 break; 3269 } 3270 3271 } 3272 return dot; 3273} 3274 3275/* Fix any .startof. or .sizeof. symbols. When the assemblers see the 3276 operator .startof. (section_name), it produces an undefined symbol 3277 .startof.section_name. Similarly, when it sees 3278 .sizeof. (section_name), it produces an undefined symbol 3279 .sizeof.section_name. For all the output sections, we look for 3280 such symbols, and set them to the correct value. */ 3281 3282static void 3283lang_set_startof () 3284{ 3285 asection *s; 3286 3287 if (link_info.relocateable) 3288 return; 3289 3290 for (s = output_bfd->sections; s != NULL; s = s->next) 3291 { 3292 const char *secname; 3293 char *buf; 3294 struct bfd_link_hash_entry *h; 3295 3296 secname = bfd_get_section_name (output_bfd, s); 3297 buf = xmalloc (10 + strlen (secname)); 3298 3299 sprintf (buf, ".startof.%s", secname); 3300 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true); 3301 if (h != NULL && h->type == bfd_link_hash_undefined) 3302 { 3303 h->type = bfd_link_hash_defined; 3304 h->u.def.value = bfd_get_section_vma (output_bfd, s); 3305 h->u.def.section = bfd_abs_section_ptr; 3306 } 3307 3308 sprintf (buf, ".sizeof.%s", secname); 3309 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true); 3310 if (h != NULL && h->type == bfd_link_hash_undefined) 3311 { 3312 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 3313 ldfile_output_machine); 3314 h->type = bfd_link_hash_defined; 3315 if (s->_cooked_size != 0) 3316 h->u.def.value = s->_cooked_size / opb; 3317 else 3318 h->u.def.value = s->_raw_size / opb; 3319 h->u.def.section = bfd_abs_section_ptr; 3320 } 3321 3322 free (buf); 3323 } 3324} 3325 3326static void 3327lang_finish () 3328{ 3329 struct bfd_link_hash_entry *h; 3330 boolean warn; 3331 3332 if (link_info.relocateable || link_info.shared) 3333 warn = false; 3334 else 3335 warn = true; 3336 3337 if (entry_symbol == (char *) NULL) 3338 { 3339 /* No entry has been specified. Look for start, but don't warn 3340 if we don't find it. */ 3341 entry_symbol = "start"; 3342 warn = false; 3343 } 3344 3345 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true); 3346 if (h != (struct bfd_link_hash_entry *) NULL 3347 && (h->type == bfd_link_hash_defined 3348 || h->type == bfd_link_hash_defweak) 3349 && h->u.def.section->output_section != NULL) 3350 { 3351 bfd_vma val; 3352 3353 val = (h->u.def.value 3354 + bfd_get_section_vma (output_bfd, 3355 h->u.def.section->output_section) 3356 + h->u.def.section->output_offset); 3357 if (! bfd_set_start_address (output_bfd, val)) 3358 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol); 3359 } 3360 else 3361 { 3362 bfd_vma val; 3363 CONST char *send; 3364 3365 /* We couldn't find the entry symbol. Try parsing it as a 3366 number. */ 3367 val = bfd_scan_vma (entry_symbol, &send, 0); 3368 if (*send == '\0') 3369 { 3370 if (! bfd_set_start_address (output_bfd, val)) 3371 einfo (_("%P%F: can't set start address\n")); 3372 } 3373 else 3374 { 3375 asection *ts; 3376 3377 /* Can't find the entry symbol, and it's not a number. Use 3378 the first address in the text section. */ 3379 ts = bfd_get_section_by_name (output_bfd, ".text"); 3380 if (ts != (asection *) NULL) 3381 { 3382 if (warn) 3383 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"), 3384 entry_symbol, bfd_get_section_vma (output_bfd, ts)); 3385 if (! bfd_set_start_address (output_bfd, 3386 bfd_get_section_vma (output_bfd, 3387 ts))) 3388 einfo (_("%P%F: can't set start address\n")); 3389 } 3390 else 3391 { 3392 if (warn) 3393 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"), 3394 entry_symbol); 3395 } 3396 } 3397 } 3398} 3399 3400/* This is a small function used when we want to ignore errors from 3401 BFD. */ 3402 3403static void 3404#ifdef ANSI_PROTOTYPES 3405ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...) 3406#else 3407ignore_bfd_errors (s) 3408 const char *s ATTRIBUTE_UNUSED; 3409#endif 3410{ 3411 /* Don't do anything. */ 3412} 3413 3414/* Check that the architecture of all the input files is compatible 3415 with the output file. Also call the backend to let it do any 3416 other checking that is needed. */ 3417 3418static void 3419lang_check () 3420{ 3421 lang_statement_union_type *file; 3422 bfd *input_bfd; 3423 CONST bfd_arch_info_type *compatible; 3424 3425 for (file = file_chain.head; 3426 file != (lang_statement_union_type *) NULL; 3427 file = file->input_statement.next) 3428 { 3429 input_bfd = file->input_statement.the_bfd; 3430 compatible = bfd_arch_get_compatible (input_bfd, 3431 output_bfd); 3432 if (compatible == NULL) 3433 { 3434 if (command_line.warn_mismatch) 3435 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"), 3436 bfd_printable_name (input_bfd), input_bfd, 3437 bfd_printable_name (output_bfd)); 3438 } 3439 else 3440 { 3441 bfd_error_handler_type pfn = NULL; 3442 3443 /* If we aren't supposed to warn about mismatched input 3444 files, temporarily set the BFD error handler to a 3445 function which will do nothing. We still want to call 3446 bfd_merge_private_bfd_data, since it may set up 3447 information which is needed in the output file. */ 3448 if (! command_line.warn_mismatch) 3449 pfn = bfd_set_error_handler (ignore_bfd_errors); 3450 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd)) 3451 { 3452 if (command_line.warn_mismatch) 3453 einfo (_("%E%X: failed to merge target specific data of file %B\n"), 3454 input_bfd); 3455 } 3456 if (! command_line.warn_mismatch) 3457 bfd_set_error_handler (pfn); 3458 } 3459 } 3460} 3461 3462/* Look through all the global common symbols and attach them to the 3463 correct section. The -sort-common command line switch may be used 3464 to roughly sort the entries by size. */ 3465 3466static void 3467lang_common () 3468{ 3469 if (link_info.relocateable 3470 && ! command_line.force_common_definition) 3471 return; 3472 3473 if (! config.sort_common) 3474 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL); 3475 else 3476 { 3477 int power; 3478 3479 for (power = 4; power >= 0; power--) 3480 bfd_link_hash_traverse (link_info.hash, lang_one_common, 3481 (PTR) &power); 3482 } 3483} 3484 3485/* Place one common symbol in the correct section. */ 3486 3487static boolean 3488lang_one_common (h, info) 3489 struct bfd_link_hash_entry *h; 3490 PTR info; 3491{ 3492 unsigned int power_of_two; 3493 bfd_vma size; 3494 asection *section; 3495 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 3496 ldfile_output_machine); 3497 3498 if (h->type != bfd_link_hash_common) 3499 return true; 3500 3501 size = h->u.c.size; 3502 power_of_two = h->u.c.p->alignment_power; 3503 3504 if (config.sort_common 3505 && power_of_two < (unsigned int) *(int *) info) 3506 return true; 3507 3508 section = h->u.c.p->section; 3509 3510 /* Increase the size of the section. */ 3511 section->_cooked_size = ALIGN_N ((section->_cooked_size + opb - 1) / opb, 3512 (bfd_size_type) (1 << power_of_two)) * opb; 3513 3514 /* Adjust the alignment if necessary. */ 3515 if (power_of_two > section->alignment_power) 3516 section->alignment_power = power_of_two; 3517 3518 /* Change the symbol from common to defined. */ 3519 h->type = bfd_link_hash_defined; 3520 h->u.def.section = section; 3521 h->u.def.value = section->_cooked_size; 3522 3523 /* Increase the size of the section. */ 3524 section->_cooked_size += size; 3525 3526 /* Make sure the section is allocated in memory, and make sure that 3527 it is no longer a common section. */ 3528 section->flags |= SEC_ALLOC; 3529 section->flags &= ~ SEC_IS_COMMON; 3530 3531 if (config.map_file != NULL) 3532 { 3533 static boolean header_printed; 3534 int len; 3535 char *name; 3536 char buf[50]; 3537 3538 if (! header_printed) 3539 { 3540 minfo (_("\nAllocating common symbols\n")); 3541 minfo (_("Common symbol size file\n\n")); 3542 header_printed = true; 3543 } 3544 3545 name = demangle (h->root.string); 3546 minfo ("%s", name); 3547 len = strlen (name); 3548 free (name); 3549 3550 if (len >= 19) 3551 { 3552 print_nl (); 3553 len = 0; 3554 } 3555 while (len < 20) 3556 { 3557 print_space (); 3558 ++len; 3559 } 3560 3561 minfo ("0x"); 3562 if (size <= 0xffffffff) 3563 sprintf (buf, "%lx", (unsigned long) size); 3564 else 3565 sprintf_vma (buf, size); 3566 minfo ("%s", buf); 3567 len = strlen (buf); 3568 3569 while (len < 16) 3570 { 3571 print_space (); 3572 ++len; 3573 } 3574 3575 minfo ("%B\n", section->owner); 3576 } 3577 3578 return true; 3579} 3580 3581/* 3582run through the input files and ensure that every input 3583section has somewhere to go. If one is found without 3584a destination then create an input request and place it 3585into the statement tree. 3586*/ 3587 3588static void 3589lang_place_orphans () 3590{ 3591 LANG_FOR_EACH_INPUT_STATEMENT (file) 3592 { 3593 asection *s; 3594 3595 for (s = file->the_bfd->sections; 3596 s != (asection *) NULL; 3597 s = s->next) 3598 { 3599 if (s->output_section == (asection *) NULL) 3600 { 3601 /* This section of the file is not attatched, root 3602 around for a sensible place for it to go */ 3603 3604 if (file->just_syms_flag) 3605 { 3606 /* We are only retrieving symbol values from this 3607 file. We want the symbols to act as though the 3608 values in the file are absolute. */ 3609 s->output_section = bfd_abs_section_ptr; 3610 s->output_offset = s->vma; 3611 } 3612 else if (strcmp (s->name, "COMMON") == 0) 3613 { 3614 /* This is a lonely common section which must have 3615 come from an archive. We attach to the section 3616 with the wildcard. */ 3617 if (! link_info.relocateable 3618 || command_line.force_common_definition) 3619 { 3620 if (default_common_section == NULL) 3621 { 3622#if 0 3623 /* This message happens when using the 3624 svr3.ifile linker script, so I have 3625 disabled it. */ 3626 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n")); 3627#endif 3628 default_common_section = 3629 lang_output_section_statement_lookup (".bss"); 3630 3631 } 3632 wild_doit (&default_common_section->children, s, 3633 default_common_section, file); 3634 } 3635 } 3636 else if (ldemul_place_orphan (file, s)) 3637 ; 3638 else 3639 { 3640 lang_output_section_statement_type *os = 3641 lang_output_section_statement_lookup (s->name); 3642 3643 wild_doit (&os->children, s, os, file); 3644 } 3645 } 3646 } 3647 } 3648} 3649 3650 3651void 3652lang_set_flags (ptr, flags, invert) 3653 lang_memory_region_type *ptr; 3654 CONST char *flags; 3655 int invert; 3656{ 3657 flagword *ptr_flags; 3658 3659 ptr_flags = invert ? &ptr->not_flags : &ptr->flags; 3660 while (*flags) 3661 { 3662 switch (*flags) 3663 { 3664 case 'A': case 'a': 3665 *ptr_flags |= SEC_ALLOC; 3666 break; 3667 3668 case 'R': case 'r': 3669 *ptr_flags |= SEC_READONLY; 3670 break; 3671 3672 case 'W': case 'w': 3673 *ptr_flags |= SEC_DATA; 3674 break; 3675 3676 case 'X': case 'x': 3677 *ptr_flags |= SEC_CODE; 3678 break; 3679 3680 case 'L': case 'l': 3681 case 'I': case 'i': 3682 *ptr_flags |= SEC_LOAD; 3683 break; 3684 3685 default: 3686 einfo (_("%P%F: invalid syntax in flags\n")); 3687 break; 3688 } 3689 flags++; 3690 } 3691} 3692 3693/* Call a function on each input file. This function will be called 3694 on an archive, but not on the elements. */ 3695 3696void 3697lang_for_each_input_file (func) 3698 void (*func) PARAMS ((lang_input_statement_type *)); 3699{ 3700 lang_input_statement_type *f; 3701 3702 for (f = (lang_input_statement_type *) input_file_chain.head; 3703 f != NULL; 3704 f = (lang_input_statement_type *) f->next_real_file) 3705 func (f); 3706} 3707 3708/* Call a function on each file. The function will be called on all 3709 the elements of an archive which are included in the link, but will 3710 not be called on the archive file itself. */ 3711 3712void 3713lang_for_each_file (func) 3714 void (*func) PARAMS ((lang_input_statement_type *)); 3715{ 3716 LANG_FOR_EACH_INPUT_STATEMENT (f) 3717 { 3718 func (f); 3719 } 3720} 3721 3722#if 0 3723 3724/* Not used. */ 3725 3726void 3727lang_for_each_input_section (func) 3728 void (*func) PARAMS ((bfd * ab, asection * as)); 3729{ 3730 LANG_FOR_EACH_INPUT_STATEMENT (f) 3731 { 3732 asection * s; 3733 3734 for (s = f->the_bfd->sections; 3735 s != (asection *) NULL; 3736 s = s->next) 3737 { 3738 func (f->the_bfd, s); 3739 } 3740 } 3741} 3742 3743#endif 3744 3745void 3746ldlang_add_file (entry) 3747 lang_input_statement_type * entry; 3748{ 3749 bfd **pp; 3750 3751 lang_statement_append (&file_chain, 3752 (lang_statement_union_type *) entry, 3753 &entry->next); 3754 3755 /* The BFD linker needs to have a list of all input BFDs involved in 3756 a link. */ 3757 ASSERT (entry->the_bfd->link_next == (bfd *) NULL); 3758 ASSERT (entry->the_bfd != output_bfd); 3759 for (pp = &link_info.input_bfds; 3760 *pp != (bfd *) NULL; 3761 pp = &(*pp)->link_next) 3762 ; 3763 *pp = entry->the_bfd; 3764 entry->the_bfd->usrdata = (PTR) entry; 3765 bfd_set_gp_size (entry->the_bfd, g_switch_value); 3766 3767 /* Look through the sections and check for any which should not be 3768 included in the link. We need to do this now, so that we can 3769 notice when the backend linker tries to report multiple 3770 definition errors for symbols which are in sections we aren't 3771 going to link. FIXME: It might be better to entirely ignore 3772 symbols which are defined in sections which are going to be 3773 discarded. This would require modifying the backend linker for 3774 each backend which might set the SEC_LINK_ONCE flag. If we do 3775 this, we should probably handle SEC_EXCLUDE in the same way. */ 3776 3777 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry); 3778} 3779 3780void 3781lang_add_output (name, from_script) 3782 CONST char *name; 3783 int from_script; 3784{ 3785 /* Make -o on command line override OUTPUT in script. */ 3786 if (had_output_filename == false || !from_script) 3787 { 3788 output_filename = name; 3789 had_output_filename = true; 3790 } 3791} 3792 3793 3794static lang_output_section_statement_type *current_section; 3795 3796static int 3797topower (x) 3798 int x; 3799{ 3800 unsigned int i = 1; 3801 int l; 3802 3803 if (x < 0) 3804 return -1; 3805 3806 for (l = 0; l < 32; l++) 3807 { 3808 if (i >= (unsigned int) x) 3809 return l; 3810 i <<= 1; 3811 } 3812 3813 return 0; 3814} 3815 3816void 3817lang_enter_output_section_statement (output_section_statement_name, 3818 address_exp, sectype, block_value, 3819 align, subalign, ebase) 3820 const char *output_section_statement_name; 3821 etree_type * address_exp; 3822 enum section_type sectype; 3823 bfd_vma block_value; 3824 etree_type *align; 3825 etree_type *subalign; 3826 etree_type *ebase; 3827{ 3828 lang_output_section_statement_type *os; 3829 3830 current_section = 3831 os = 3832 lang_output_section_statement_lookup (output_section_statement_name); 3833 3834 3835 3836 /* Add this statement to tree */ 3837 /* add_statement(lang_output_section_statement_enum, 3838 output_section_statement);*/ 3839 /* Make next things chain into subchain of this */ 3840 3841 if (os->addr_tree == 3842 (etree_type *) NULL) 3843 { 3844 os->addr_tree = 3845 address_exp; 3846 } 3847 os->sectype = sectype; 3848 if (sectype != noload_section) 3849 os->flags = SEC_NO_FLAGS; 3850 else 3851 os->flags = SEC_NEVER_LOAD; 3852 os->block_value = block_value ? block_value : 1; 3853 stat_ptr = &os->children; 3854 3855 os->subsection_alignment = topower( 3856 exp_get_value_int(subalign, -1, 3857 "subsection alignment", 3858 0)); 3859 os->section_alignment = topower( 3860 exp_get_value_int(align, -1, 3861 "section alignment", 0)); 3862 3863 os->load_base = ebase; 3864} 3865 3866 3867void 3868lang_final () 3869{ 3870 lang_output_statement_type *new = 3871 new_stat (lang_output_statement, stat_ptr); 3872 3873 new->name = output_filename; 3874} 3875 3876/* Reset the current counters in the regions */ 3877static void 3878reset_memory_regions () 3879{ 3880 lang_memory_region_type *p = lang_memory_region_list; 3881 3882 for (p = lang_memory_region_list; 3883 p != (lang_memory_region_type *) NULL; 3884 p = p->next) 3885 { 3886 p->old_length = (bfd_size_type) (p->current - p->origin); 3887 p->current = p->origin; 3888 } 3889} 3890 3891/* Expand a wild statement for a particular FILE, marking its sections KEEP 3892 as needed. SECTION may be NULL, in which case it is a wild card. */ 3893 3894static void 3895gc_section_callback (ptr, section, file, data) 3896 lang_wild_statement_type *ptr; 3897 asection *section; 3898 lang_input_statement_type *file ATTRIBUTE_UNUSED; 3899 void *data ATTRIBUTE_UNUSED; 3900{ 3901 /* If the wild pattern was marked KEEP, the member sections 3902 should be as well. */ 3903 if (ptr->keep_sections) 3904 section->flags |= SEC_KEEP; 3905} 3906 3907/* Handle a wild statement, marking it against GC. SECTION or FILE or both 3908 may be NULL, indicating that it is a wildcard. */ 3909 3910static void 3911lang_gc_wild (s, section, file) 3912 lang_wild_statement_type *s; 3913 const char *section; 3914 const char *file; 3915{ 3916 walk_wild (s, section, file, gc_section_callback, NULL); 3917} 3918 3919/* Iterate over sections marking them against GC. */ 3920 3921static void 3922lang_gc_sections_1 (s) 3923 lang_statement_union_type * s; 3924{ 3925 for (; s != (lang_statement_union_type *) NULL; s = s->next) 3926 { 3927 switch (s->header.type) 3928 { 3929 case lang_wild_statement_enum: 3930 lang_gc_wild (&s->wild_statement, 3931 s->wild_statement.section_name, 3932 s->wild_statement.filename); 3933 break; 3934 case lang_constructors_statement_enum: 3935 lang_gc_sections_1 (constructor_list.head); 3936 break; 3937 case lang_output_section_statement_enum: 3938 lang_gc_sections_1 (s->output_section_statement.children.head); 3939 break; 3940 case lang_group_statement_enum: 3941 lang_gc_sections_1 (s->group_statement.children.head); 3942 break; 3943 default: 3944 break; 3945 } 3946 } 3947} 3948 3949static void 3950lang_gc_sections () 3951{ 3952 struct bfd_link_hash_entry *h; 3953 ldlang_undef_chain_list_type *ulist, fake_list_start; 3954 3955 /* Keep all sections so marked in the link script. */ 3956 3957 lang_gc_sections_1 (statement_list.head); 3958 3959 /* Keep all sections containing symbols undefined on the command-line. 3960 Handle the entry symbol at the same time. */ 3961 3962 if (entry_symbol != NULL) 3963 { 3964 fake_list_start.next = ldlang_undef_chain_list_head; 3965 fake_list_start.name = (char *) entry_symbol; 3966 ulist = &fake_list_start; 3967 } 3968 else 3969 ulist = ldlang_undef_chain_list_head; 3970 3971 for (; ulist; ulist = ulist->next) 3972 { 3973 h = bfd_link_hash_lookup (link_info.hash, ulist->name, 3974 false, false, false); 3975 3976 if (h != (struct bfd_link_hash_entry *) NULL 3977 && (h->type == bfd_link_hash_defined 3978 || h->type == bfd_link_hash_defweak) 3979 && ! bfd_is_abs_section (h->u.def.section)) 3980 { 3981 h->u.def.section->flags |= SEC_KEEP; 3982 } 3983 } 3984 3985 bfd_gc_sections (output_bfd, &link_info); 3986} 3987 3988void 3989lang_process () 3990{ 3991 lang_reasonable_defaults (); 3992 current_target = default_target; 3993 3994 lang_for_each_statement (ldlang_open_output); /* Open the output file */ 3995 3996 ldemul_create_output_section_statements (); 3997 3998 /* Add to the hash table all undefineds on the command line */ 3999 lang_place_undefineds (); 4000 4001 already_linked_table_init (); 4002 4003 /* Create a bfd for each input file */ 4004 current_target = default_target; 4005 open_input_bfds (statement_list.head, false); 4006 4007 ldemul_after_open (); 4008 4009 already_linked_table_free (); 4010 4011 /* Make sure that we're not mixing architectures. We call this 4012 after all the input files have been opened, but before we do any 4013 other processing, so that any operations merge_private_bfd_data 4014 does on the output file will be known during the rest of the 4015 link. */ 4016 lang_check (); 4017 4018 /* Handle .exports instead of a version script if we're told to do so. */ 4019 if (command_line.version_exports_section) 4020 lang_do_version_exports_section (); 4021 4022 /* Build all sets based on the information gathered from the input 4023 files. */ 4024 ldctor_build_sets (); 4025 4026 /* Remove unreferenced sections if asked to. */ 4027 if (command_line.gc_sections) 4028 lang_gc_sections (); 4029 4030 /* Size up the common data */ 4031 lang_common (); 4032 4033 /* Run through the contours of the script and attach input sections 4034 to the correct output sections 4035 */ 4036 map_input_to_output_sections (statement_list.head, (char *) NULL, 4037 (lang_output_section_statement_type *) NULL); 4038 4039 4040 /* Find any sections not attached explicitly and handle them */ 4041 lang_place_orphans (); 4042 4043 ldemul_before_allocation (); 4044 4045 /* We must record the program headers before we try to fix the 4046 section positions, since they will affect SIZEOF_HEADERS. */ 4047 lang_record_phdrs (); 4048 4049 /* Now run around and relax if we can */ 4050 if (command_line.relax) 4051 { 4052 /* First time round is a trial run to get the 'worst case' 4053 addresses of the objects if there was no relaxing. */ 4054 lang_size_sections (statement_list.head, 4055 abs_output_section, 4056 &(statement_list.head), 0, (bfd_vma) 0, false); 4057 4058 /* Keep relaxing until bfd_relax_section gives up. */ 4059 do 4060 { 4061 reset_memory_regions (); 4062 4063 relax_again = false; 4064 4065 /* Note: pe-dll.c does something like this also. If you find 4066 you need to change this code, you probably need to change 4067 pe-dll.c also. DJ */ 4068 4069 /* Do all the assignments with our current guesses as to 4070 section sizes. */ 4071 lang_do_assignments (statement_list.head, 4072 abs_output_section, 4073 (fill_type) 0, (bfd_vma) 0); 4074 4075 /* Perform another relax pass - this time we know where the 4076 globals are, so can make better guess. */ 4077 lang_size_sections (statement_list.head, 4078 abs_output_section, 4079 &(statement_list.head), 0, (bfd_vma) 0, true); 4080 } 4081 while (relax_again); 4082 } 4083 else 4084 { 4085 /* Size up the sections. */ 4086 lang_size_sections (statement_list.head, 4087 abs_output_section, 4088 &(statement_list.head), 0, (bfd_vma) 0, false); 4089 } 4090 4091 /* See if anything special should be done now we know how big 4092 everything is. */ 4093 ldemul_after_allocation (); 4094 4095 /* Fix any .startof. or .sizeof. symbols. */ 4096 lang_set_startof (); 4097 4098 /* Do all the assignments, now that we know the final restingplaces 4099 of all the symbols */ 4100 4101 lang_do_assignments (statement_list.head, 4102 abs_output_section, 4103 (fill_type) 0, (bfd_vma) 0); 4104 4105 /* Make sure that the section addresses make sense. */ 4106 if (! link_info.relocateable 4107 && command_line.check_section_addresses) 4108 lang_check_section_addresses (); 4109 4110 /* Final stuffs */ 4111 4112 ldemul_finish (); 4113 lang_finish (); 4114} 4115 4116/* EXPORTED TO YACC */ 4117 4118void 4119lang_add_wild (section_name, sections_sorted, filename, filenames_sorted, 4120 keep_sections, exclude_filename_list) 4121 const char *const section_name; 4122 boolean sections_sorted; 4123 const char *const filename; 4124 boolean filenames_sorted; 4125 boolean keep_sections; 4126 struct name_list *exclude_filename_list; 4127{ 4128 lang_wild_statement_type *new = new_stat (lang_wild_statement, 4129 stat_ptr); 4130 4131 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0) 4132 { 4133 placed_commons = true; 4134 } 4135 if (filename != NULL && ! wildcardp (filename)) 4136 { 4137 lang_has_input_file = true; 4138 } 4139 new->section_name = section_name; 4140 new->sections_sorted = sections_sorted; 4141 new->filename = filename; 4142 new->filenames_sorted = filenames_sorted; 4143 new->keep_sections = keep_sections; 4144 new->exclude_filename_list = exclude_filename_list; 4145 lang_list_init (&new->children); 4146} 4147 4148void 4149lang_section_start (name, address) 4150 CONST char *name; 4151 etree_type * address; 4152{ 4153 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr); 4154 4155 ad->section_name = name; 4156 ad->address = address; 4157} 4158 4159/* Set the start symbol to NAME. CMDLINE is nonzero if this is called 4160 because of a -e argument on the command line, or zero if this is 4161 called by ENTRY in a linker script. Command line arguments take 4162 precedence. */ 4163 4164void 4165lang_add_entry (name, cmdline) 4166 CONST char *name; 4167 boolean cmdline; 4168{ 4169 if (entry_symbol == NULL 4170 || cmdline 4171 || ! entry_from_cmdline) 4172 { 4173 entry_symbol = name; 4174 entry_from_cmdline = cmdline; 4175 } 4176} 4177 4178void 4179lang_add_target (name) 4180 CONST char *name; 4181{ 4182 lang_target_statement_type *new = new_stat (lang_target_statement, 4183 stat_ptr); 4184 4185 new->target = name; 4186 4187} 4188 4189void 4190lang_add_map (name) 4191 CONST char *name; 4192{ 4193 while (*name) 4194 { 4195 switch (*name) 4196 { 4197 case 'F': 4198 map_option_f = true; 4199 break; 4200 } 4201 name++; 4202 } 4203} 4204 4205void 4206lang_add_fill (exp) 4207 int exp; 4208{ 4209 lang_fill_statement_type *new = new_stat (lang_fill_statement, 4210 stat_ptr); 4211 4212 new->fill = exp; 4213} 4214 4215void 4216lang_add_data (type, exp) 4217 int type; 4218 union etree_union *exp; 4219{ 4220 4221 lang_data_statement_type *new = new_stat (lang_data_statement, 4222 stat_ptr); 4223 4224 new->exp = exp; 4225 new->type = type; 4226 4227} 4228 4229/* Create a new reloc statement. RELOC is the BFD relocation type to 4230 generate. HOWTO is the corresponding howto structure (we could 4231 look this up, but the caller has already done so). SECTION is the 4232 section to generate a reloc against, or NAME is the name of the 4233 symbol to generate a reloc against. Exactly one of SECTION and 4234 NAME must be NULL. ADDEND is an expression for the addend. */ 4235 4236void 4237lang_add_reloc (reloc, howto, section, name, addend) 4238 bfd_reloc_code_real_type reloc; 4239 reloc_howto_type *howto; 4240 asection *section; 4241 const char *name; 4242 union etree_union *addend; 4243{ 4244 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); 4245 4246 p->reloc = reloc; 4247 p->howto = howto; 4248 p->section = section; 4249 p->name = name; 4250 p->addend_exp = addend; 4251 4252 p->addend_value = 0; 4253 p->output_section = NULL; 4254 p->output_vma = 0; 4255} 4256 4257lang_assignment_statement_type * 4258lang_add_assignment (exp) 4259 etree_type * exp; 4260{ 4261 lang_assignment_statement_type *new = new_stat (lang_assignment_statement, 4262 stat_ptr); 4263 4264 new->exp = exp; 4265 return new; 4266} 4267 4268void 4269lang_add_attribute (attribute) 4270 enum statement_enum attribute; 4271{ 4272 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); 4273} 4274 4275void 4276lang_startup (name) 4277 CONST char *name; 4278{ 4279 if (startup_file != (char *) NULL) 4280 { 4281 einfo (_("%P%Fmultiple STARTUP files\n")); 4282 } 4283 first_file->filename = name; 4284 first_file->local_sym_name = name; 4285 first_file->real = true; 4286 4287 startup_file = name; 4288} 4289 4290void 4291lang_float (maybe) 4292 boolean maybe; 4293{ 4294 lang_float_flag = maybe; 4295} 4296 4297void 4298lang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec) 4299 bfd_vma fill; 4300 const char *memspec; 4301 struct lang_output_section_phdr_list *phdrs; 4302 const char *lma_memspec; 4303{ 4304 current_section->fill = fill; 4305 current_section->region = lang_memory_region_lookup (memspec); 4306 if (strcmp (lma_memspec, "*default*") != 0) 4307 { 4308 current_section->lma_region = lang_memory_region_lookup (lma_memspec); 4309 /* if no runtime region has been given, but the load region has been, 4310 use the load region */ 4311 if (strcmp (memspec, "*default*") == 0) 4312 current_section->region = lang_memory_region_lookup (lma_memspec); 4313 } 4314 current_section->phdrs = phdrs; 4315 stat_ptr = &statement_list; 4316} 4317 4318/* 4319 Create an absolute symbol with the given name with the value of the 4320 address of first byte of the section named. 4321 4322 If the symbol already exists, then do nothing. 4323*/ 4324void 4325lang_abs_symbol_at_beginning_of (secname, name) 4326 const char *secname; 4327 const char *name; 4328{ 4329 struct bfd_link_hash_entry *h; 4330 4331 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true); 4332 if (h == (struct bfd_link_hash_entry *) NULL) 4333 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); 4334 4335 if (h->type == bfd_link_hash_new 4336 || h->type == bfd_link_hash_undefined) 4337 { 4338 asection *sec; 4339 4340 h->type = bfd_link_hash_defined; 4341 4342 sec = bfd_get_section_by_name (output_bfd, secname); 4343 if (sec == (asection *) NULL) 4344 h->u.def.value = 0; 4345 else 4346 h->u.def.value = bfd_get_section_vma (output_bfd, sec); 4347 4348 h->u.def.section = bfd_abs_section_ptr; 4349 } 4350} 4351 4352/* 4353 Create an absolute symbol with the given name with the value of the 4354 address of the first byte after the end of the section named. 4355 4356 If the symbol already exists, then do nothing. 4357*/ 4358void 4359lang_abs_symbol_at_end_of (secname, name) 4360 const char *secname; 4361 const char *name; 4362{ 4363 struct bfd_link_hash_entry *h; 4364 4365 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true); 4366 if (h == (struct bfd_link_hash_entry *) NULL) 4367 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); 4368 4369 if (h->type == bfd_link_hash_new 4370 || h->type == bfd_link_hash_undefined) 4371 { 4372 asection *sec; 4373 4374 h->type = bfd_link_hash_defined; 4375 4376 sec = bfd_get_section_by_name (output_bfd, secname); 4377 if (sec == (asection *) NULL) 4378 h->u.def.value = 0; 4379 else 4380 h->u.def.value = (bfd_get_section_vma (output_bfd, sec) 4381 + bfd_section_size (output_bfd, sec) / 4382 bfd_octets_per_byte (output_bfd)); 4383 4384 h->u.def.section = bfd_abs_section_ptr; 4385 } 4386} 4387 4388void 4389lang_statement_append (list, element, field) 4390 lang_statement_list_type * list; 4391 lang_statement_union_type * element; 4392 lang_statement_union_type ** field; 4393{ 4394 *(list->tail) = element; 4395 list->tail = field; 4396} 4397 4398/* Set the output format type. -oformat overrides scripts. */ 4399 4400void 4401lang_add_output_format (format, big, little, from_script) 4402 const char *format; 4403 const char *big; 4404 const char *little; 4405 int from_script; 4406{ 4407 if (output_target == NULL || !from_script) 4408 { 4409 if (command_line.endian == ENDIAN_BIG 4410 && big != NULL) 4411 format = big; 4412 else if (command_line.endian == ENDIAN_LITTLE 4413 && little != NULL) 4414 format = little; 4415 4416 output_target = format; 4417 } 4418} 4419 4420/* Enter a group. This creates a new lang_group_statement, and sets 4421 stat_ptr to build new statements within the group. */ 4422 4423void 4424lang_enter_group () 4425{ 4426 lang_group_statement_type *g; 4427 4428 g = new_stat (lang_group_statement, stat_ptr); 4429 lang_list_init (&g->children); 4430 stat_ptr = &g->children; 4431} 4432 4433/* Leave a group. This just resets stat_ptr to start writing to the 4434 regular list of statements again. Note that this will not work if 4435 groups can occur inside anything else which can adjust stat_ptr, 4436 but currently they can't. */ 4437 4438void 4439lang_leave_group () 4440{ 4441 stat_ptr = &statement_list; 4442} 4443 4444/* Add a new program header. This is called for each entry in a PHDRS 4445 command in a linker script. */ 4446 4447void 4448lang_new_phdr (name, type, filehdr, phdrs, at, flags) 4449 const char *name; 4450 etree_type *type; 4451 boolean filehdr; 4452 boolean phdrs; 4453 etree_type *at; 4454 etree_type *flags; 4455{ 4456 struct lang_phdr *n, **pp; 4457 4458 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr)); 4459 n->next = NULL; 4460 n->name = name; 4461 n->type = exp_get_value_int (type, 0, "program header type", 4462 lang_final_phase_enum); 4463 n->filehdr = filehdr; 4464 n->phdrs = phdrs; 4465 n->at = at; 4466 n->flags = flags; 4467 4468 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) 4469 ; 4470 *pp = n; 4471} 4472 4473/* Record the program header information in the output BFD. FIXME: We 4474 should not be calling an ELF specific function here. */ 4475 4476static void 4477lang_record_phdrs () 4478{ 4479 unsigned int alc; 4480 asection **secs; 4481 struct lang_output_section_phdr_list *last; 4482 struct lang_phdr *l; 4483 lang_statement_union_type *u; 4484 4485 alc = 10; 4486 secs = (asection **) xmalloc (alc * sizeof (asection *)); 4487 last = NULL; 4488 for (l = lang_phdr_list; l != NULL; l = l->next) 4489 { 4490 unsigned int c; 4491 flagword flags; 4492 bfd_vma at; 4493 4494 c = 0; 4495 for (u = lang_output_section_statement.head; 4496 u != NULL; 4497 u = u->output_section_statement.next) 4498 { 4499 lang_output_section_statement_type *os; 4500 struct lang_output_section_phdr_list *pl; 4501 4502 os = &u->output_section_statement; 4503 4504 pl = os->phdrs; 4505 if (pl != NULL) 4506 last = pl; 4507 else 4508 { 4509 if (os->sectype == noload_section 4510 || os->bfd_section == NULL 4511 || (os->bfd_section->flags & SEC_ALLOC) == 0) 4512 continue; 4513 pl = last; 4514 } 4515 4516 if (os->bfd_section == NULL) 4517 continue; 4518 4519 for (; pl != NULL; pl = pl->next) 4520 { 4521 if (strcmp (pl->name, l->name) == 0) 4522 { 4523 if (c >= alc) 4524 { 4525 alc *= 2; 4526 secs = ((asection **) 4527 xrealloc (secs, alc * sizeof (asection *))); 4528 } 4529 secs[c] = os->bfd_section; 4530 ++c; 4531 pl->used = true; 4532 } 4533 } 4534 } 4535 4536 if (l->flags == NULL) 4537 flags = 0; 4538 else 4539 flags = exp_get_vma (l->flags, 0, "phdr flags", 4540 lang_final_phase_enum); 4541 4542 if (l->at == NULL) 4543 at = 0; 4544 else 4545 at = exp_get_vma (l->at, 0, "phdr load address", 4546 lang_final_phase_enum); 4547 4548 if (! bfd_record_phdr (output_bfd, l->type, 4549 l->flags == NULL ? false : true, 4550 flags, 4551 l->at == NULL ? false : true, 4552 at, l->filehdr, l->phdrs, c, secs)) 4553 einfo (_("%F%P: bfd_record_phdr failed: %E\n")); 4554 } 4555 4556 free (secs); 4557 4558 /* Make sure all the phdr assignments succeeded. */ 4559 for (u = lang_output_section_statement.head; 4560 u != NULL; 4561 u = u->output_section_statement.next) 4562 { 4563 struct lang_output_section_phdr_list *pl; 4564 4565 if (u->output_section_statement.bfd_section == NULL) 4566 continue; 4567 4568 for (pl = u->output_section_statement.phdrs; 4569 pl != NULL; 4570 pl = pl->next) 4571 if (! pl->used && strcmp (pl->name, "NONE") != 0) 4572 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), 4573 u->output_section_statement.name, pl->name); 4574 } 4575} 4576 4577/* Record a list of sections which may not be cross referenced. */ 4578 4579void 4580lang_add_nocrossref (l) 4581 struct lang_nocrossref *l; 4582{ 4583 struct lang_nocrossrefs *n; 4584 4585 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); 4586 n->next = nocrossref_list; 4587 n->list = l; 4588 nocrossref_list = n; 4589 4590 /* Set notice_all so that we get informed about all symbols. */ 4591 link_info.notice_all = true; 4592} 4593 4594/* Overlay handling. We handle overlays with some static variables. */ 4595 4596/* The overlay virtual address. */ 4597static etree_type *overlay_vma; 4598 4599/* The overlay load address. */ 4600static etree_type *overlay_lma; 4601 4602/* Whether nocrossrefs is set for this overlay. */ 4603static int overlay_nocrossrefs; 4604 4605/* An expression for the maximum section size seen so far. */ 4606static etree_type *overlay_max; 4607 4608/* A list of all the sections in this overlay. */ 4609 4610struct overlay_list 4611{ 4612 struct overlay_list *next; 4613 lang_output_section_statement_type *os; 4614}; 4615 4616static struct overlay_list *overlay_list; 4617 4618/* Start handling an overlay. */ 4619 4620void 4621lang_enter_overlay (vma_expr, lma_expr, nocrossrefs) 4622 etree_type *vma_expr; 4623 etree_type *lma_expr; 4624 int nocrossrefs; 4625{ 4626 /* The grammar should prevent nested overlays from occurring. */ 4627 ASSERT (overlay_vma == NULL 4628 && overlay_lma == NULL 4629 && overlay_list == NULL 4630 && overlay_max == NULL); 4631 4632 overlay_vma = vma_expr; 4633 overlay_lma = lma_expr; 4634 overlay_nocrossrefs = nocrossrefs; 4635} 4636 4637/* Start a section in an overlay. We handle this by calling 4638 lang_enter_output_section_statement with the correct VMA and LMA. */ 4639 4640void 4641lang_enter_overlay_section (name) 4642 const char *name; 4643{ 4644 struct overlay_list *n; 4645 etree_type *size; 4646 4647 lang_enter_output_section_statement (name, overlay_vma, normal_section, 4648 0, 0, 0, overlay_lma); 4649 4650 /* If this is the first section, then base the VMA and LMA of future 4651 sections on this one. This will work correctly even if `.' is 4652 used in the addresses. */ 4653 if (overlay_list == NULL) 4654 { 4655 overlay_vma = exp_nameop (ADDR, name); 4656 overlay_lma = exp_nameop (LOADADDR, name); 4657 } 4658 4659 /* Remember the section. */ 4660 n = (struct overlay_list *) xmalloc (sizeof *n); 4661 n->os = current_section; 4662 n->next = overlay_list; 4663 overlay_list = n; 4664 4665 size = exp_nameop (SIZEOF, name); 4666 4667 /* Adjust the LMA for the next section. */ 4668 overlay_lma = exp_binop ('+', overlay_lma, size); 4669 4670 /* Arrange to work out the maximum section end address. */ 4671 if (overlay_max == NULL) 4672 overlay_max = size; 4673 else 4674 overlay_max = exp_binop (MAX_K, overlay_max, size); 4675} 4676 4677/* Finish a section in an overlay. There isn't any special to do 4678 here. */ 4679 4680void 4681lang_leave_overlay_section (fill, phdrs) 4682 bfd_vma fill; 4683 struct lang_output_section_phdr_list *phdrs; 4684{ 4685 const char *name; 4686 char *clean, *s2; 4687 const char *s1; 4688 char *buf; 4689 4690 name = current_section->name; 4691 4692 lang_leave_output_section_statement (fill, "*default*", 4693 phdrs, "*default*"); 4694 4695 /* Define the magic symbols. */ 4696 4697 clean = xmalloc (strlen (name) + 1); 4698 s2 = clean; 4699 for (s1 = name; *s1 != '\0'; s1++) 4700 if (isalnum ((unsigned char) *s1) || *s1 == '_') 4701 *s2++ = *s1; 4702 *s2 = '\0'; 4703 4704 buf = xmalloc (strlen (clean) + sizeof "__load_start_"); 4705 sprintf (buf, "__load_start_%s", clean); 4706 lang_add_assignment (exp_assop ('=', buf, 4707 exp_nameop (LOADADDR, name))); 4708 4709 buf = xmalloc (strlen (clean) + sizeof "__load_stop_"); 4710 sprintf (buf, "__load_stop_%s", clean); 4711 lang_add_assignment (exp_assop ('=', buf, 4712 exp_binop ('+', 4713 exp_nameop (LOADADDR, name), 4714 exp_nameop (SIZEOF, name)))); 4715 4716 free (clean); 4717} 4718 4719/* Finish an overlay. If there are any overlay wide settings, this 4720 looks through all the sections in the overlay and sets them. */ 4721 4722void 4723lang_leave_overlay (fill, memspec, phdrs, lma_memspec) 4724 bfd_vma fill; 4725 const char *memspec; 4726 struct lang_output_section_phdr_list *phdrs; 4727 const char *lma_memspec; 4728{ 4729 lang_memory_region_type *region; 4730 lang_memory_region_type *lma_region; 4731 struct overlay_list *l; 4732 struct lang_nocrossref *nocrossref; 4733 4734 if (memspec == NULL) 4735 region = NULL; 4736 else 4737 region = lang_memory_region_lookup (memspec); 4738 4739 if (lma_memspec == NULL) 4740 lma_region = NULL; 4741 else 4742 lma_region = lang_memory_region_lookup (lma_memspec); 4743 4744 nocrossref = NULL; 4745 4746 l = overlay_list; 4747 while (l != NULL) 4748 { 4749 struct overlay_list *next; 4750 4751 if (fill != 0 && l->os->fill == 0) 4752 l->os->fill = fill; 4753 if (region != NULL && l->os->region == NULL) 4754 l->os->region = region; 4755 if (lma_region != NULL && l->os->lma_region == NULL) 4756 l->os->lma_region = lma_region; 4757 if (phdrs != NULL && l->os->phdrs == NULL) 4758 l->os->phdrs = phdrs; 4759 4760 if (overlay_nocrossrefs) 4761 { 4762 struct lang_nocrossref *nc; 4763 4764 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc); 4765 nc->name = l->os->name; 4766 nc->next = nocrossref; 4767 nocrossref = nc; 4768 } 4769 4770 next = l->next; 4771 free (l); 4772 l = next; 4773 } 4774 4775 if (nocrossref != NULL) 4776 lang_add_nocrossref (nocrossref); 4777 4778 /* Update . for the end of the overlay. */ 4779 lang_add_assignment (exp_assop ('=', ".", 4780 exp_binop ('+', overlay_vma, overlay_max))); 4781 4782 overlay_vma = NULL; 4783 overlay_lma = NULL; 4784 overlay_nocrossrefs = 0; 4785 overlay_list = NULL; 4786 overlay_max = NULL; 4787} 4788 4789/* Version handling. This is only useful for ELF. */ 4790 4791/* This global variable holds the version tree that we build. */ 4792 4793struct bfd_elf_version_tree *lang_elf_version_info; 4794 4795static int 4796lang_vers_match_lang_c (expr, sym) 4797 struct bfd_elf_version_expr *expr; 4798 const char *sym; 4799{ 4800 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') 4801 return 1; 4802 return fnmatch (expr->pattern, sym, 0) == 0; 4803} 4804 4805static int 4806lang_vers_match_lang_cplusplus (expr, sym) 4807 struct bfd_elf_version_expr *expr; 4808 const char *sym; 4809{ 4810 char *alt_sym; 4811 int result; 4812 4813 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') 4814 return 1; 4815 4816 alt_sym = cplus_demangle(sym, /* DMGL_NO_TPARAMS */ 0); 4817 if (!alt_sym) 4818 { 4819 /* cplus_demangle (also) returns NULL when it is not a C++ symbol. 4820 Should we early out false in this case? */ 4821 result = fnmatch (expr->pattern, sym, 0) == 0; 4822 } 4823 else 4824 { 4825 result = fnmatch (expr->pattern, alt_sym, 0) == 0; 4826 free (alt_sym); 4827 } 4828 4829 return result; 4830} 4831 4832static int 4833lang_vers_match_lang_java (expr, sym) 4834 struct bfd_elf_version_expr *expr; 4835 const char *sym; 4836{ 4837 char *alt_sym; 4838 int result; 4839 4840 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') 4841 return 1; 4842 4843 alt_sym = cplus_demangle(sym, DMGL_JAVA); 4844 if (!alt_sym) 4845 { 4846 /* cplus_demangle (also) returns NULL when it is not a Java symbol. 4847 Should we early out false in this case? */ 4848 result = fnmatch (expr->pattern, sym, 0) == 0; 4849 } 4850 else 4851 { 4852 result = fnmatch (expr->pattern, alt_sym, 0) == 0; 4853 free (alt_sym); 4854 } 4855 4856 return result; 4857} 4858 4859/* This is called for each variable name or match expression. */ 4860 4861struct bfd_elf_version_expr * 4862lang_new_vers_regex (orig, new, lang) 4863 struct bfd_elf_version_expr *orig; 4864 const char *new; 4865 const char *lang; 4866{ 4867 struct bfd_elf_version_expr *ret; 4868 4869 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); 4870 ret->next = orig; 4871 ret->pattern = new; 4872 4873 if (lang == NULL || strcasecmp (lang, "C") == 0) 4874 ret->match = lang_vers_match_lang_c; 4875 else if (strcasecmp (lang, "C++") == 0) 4876 ret->match = lang_vers_match_lang_cplusplus; 4877 else if (strcasecmp (lang, "Java") == 0) 4878 ret->match = lang_vers_match_lang_java; 4879 else 4880 { 4881 einfo (_("%X%P: unknown language `%s' in version information\n"), 4882 lang); 4883 ret->match = lang_vers_match_lang_c; 4884 } 4885 4886 return ret; 4887} 4888 4889/* This is called for each set of variable names and match 4890 expressions. */ 4891 4892struct bfd_elf_version_tree * 4893lang_new_vers_node (globals, locals) 4894 struct bfd_elf_version_expr *globals; 4895 struct bfd_elf_version_expr *locals; 4896{ 4897 struct bfd_elf_version_tree *ret; 4898 4899 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret); 4900 ret->next = NULL; 4901 ret->name = NULL; 4902 ret->vernum = 0; 4903 ret->globals = globals; 4904 ret->locals = locals; 4905 ret->deps = NULL; 4906 ret->name_indx = (unsigned int) -1; 4907 ret->used = 0; 4908 return ret; 4909} 4910 4911/* This static variable keeps track of version indices. */ 4912 4913static int version_index; 4914 4915/* This is called when we know the name and dependencies of the 4916 version. */ 4917 4918void 4919lang_register_vers_node (name, version, deps) 4920 const char *name; 4921 struct bfd_elf_version_tree *version; 4922 struct bfd_elf_version_deps *deps; 4923{ 4924 struct bfd_elf_version_tree *t, **pp; 4925 struct bfd_elf_version_expr *e1; 4926 4927 /* Make sure this node has a unique name. */ 4928 for (t = lang_elf_version_info; t != NULL; t = t->next) 4929 if (strcmp (t->name, name) == 0) 4930 einfo (_("%X%P: duplicate version tag `%s'\n"), name); 4931 4932 /* Check the global and local match names, and make sure there 4933 aren't any duplicates. */ 4934 4935 for (e1 = version->globals; e1 != NULL; e1 = e1->next) 4936 { 4937 for (t = lang_elf_version_info; t != NULL; t = t->next) 4938 { 4939 struct bfd_elf_version_expr *e2; 4940 4941 for (e2 = t->locals; e2 != NULL; e2 = e2->next) 4942 if (strcmp (e1->pattern, e2->pattern) == 0) 4943 einfo (_("%X%P: duplicate expression `%s' in version information\n"), 4944 e1->pattern); 4945 } 4946 } 4947 4948 for (e1 = version->locals; e1 != NULL; e1 = e1->next) 4949 { 4950 for (t = lang_elf_version_info; t != NULL; t = t->next) 4951 { 4952 struct bfd_elf_version_expr *e2; 4953 4954 for (e2 = t->globals; e2 != NULL; e2 = e2->next) 4955 if (strcmp (e1->pattern, e2->pattern) == 0) 4956 einfo (_("%X%P: duplicate expression `%s' in version information\n"), 4957 e1->pattern); 4958 } 4959 } 4960 4961 version->deps = deps; 4962 version->name = name; 4963 ++version_index; 4964 version->vernum = version_index; 4965 4966 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next) 4967 ; 4968 *pp = version; 4969} 4970 4971/* This is called when we see a version dependency. */ 4972 4973struct bfd_elf_version_deps * 4974lang_add_vers_depend (list, name) 4975 struct bfd_elf_version_deps *list; 4976 const char *name; 4977{ 4978 struct bfd_elf_version_deps *ret; 4979 struct bfd_elf_version_tree *t; 4980 4981 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); 4982 ret->next = list; 4983 4984 for (t = lang_elf_version_info; t != NULL; t = t->next) 4985 { 4986 if (strcmp (t->name, name) == 0) 4987 { 4988 ret->version_needed = t; 4989 return ret; 4990 } 4991 } 4992 4993 einfo (_("%X%P: unable to find version dependency `%s'\n"), name); 4994 4995 return ret; 4996} 4997 4998static void 4999lang_do_version_exports_section () 5000{ 5001 struct bfd_elf_version_expr *greg = NULL, *lreg; 5002 5003 LANG_FOR_EACH_INPUT_STATEMENT (is) 5004 { 5005 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); 5006 char *contents, *p; 5007 bfd_size_type len; 5008 5009 if (sec == NULL) 5010 continue; 5011 5012 len = bfd_section_size (is->the_bfd, sec); 5013 contents = xmalloc (len); 5014 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) 5015 einfo (_("%X%P: unable to read .exports section contents"), sec); 5016 5017 p = contents; 5018 while (p < contents+len) 5019 { 5020 greg = lang_new_vers_regex (greg, p, NULL); 5021 p = strchr (p, '\0') + 1; 5022 } 5023 5024 /* Do not free the contents, as we used them creating the regex. */ 5025 5026 /* Do not include this section in the link. */ 5027 bfd_set_section_flags (is->the_bfd, sec, 5028 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE); 5029 } 5030 5031 lreg = lang_new_vers_regex (NULL, "*", NULL); 5032 lang_register_vers_node (command_line.version_exports_section, 5033 lang_new_vers_node (greg, lreg), NULL); 5034} 5035