1*a9fa9459Szrj // script-sections.h -- linker script SECTIONS for gold -*- C++ -*- 2*a9fa9459Szrj 3*a9fa9459Szrj // Copyright (C) 2008-2016 Free Software Foundation, Inc. 4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>. 5*a9fa9459Szrj 6*a9fa9459Szrj // This file is part of gold. 7*a9fa9459Szrj 8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify 9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by 10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or 11*a9fa9459Szrj // (at your option) any later version. 12*a9fa9459Szrj 13*a9fa9459Szrj // This program is distributed in the hope that it will be useful, 14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*a9fa9459Szrj // GNU General Public License for more details. 17*a9fa9459Szrj 18*a9fa9459Szrj // You should have received a copy of the GNU General Public License 19*a9fa9459Szrj // along with this program; if not, write to the Free Software 20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21*a9fa9459Szrj // MA 02110-1301, USA. 22*a9fa9459Szrj 23*a9fa9459Szrj // This is for the support of the SECTIONS clause in linker scripts. 24*a9fa9459Szrj 25*a9fa9459Szrj #ifndef GOLD_SCRIPT_SECTIONS_H 26*a9fa9459Szrj #define GOLD_SCRIPT_SECTIONS_H 27*a9fa9459Szrj 28*a9fa9459Szrj #include <cstdio> 29*a9fa9459Szrj #include <list> 30*a9fa9459Szrj #include <vector> 31*a9fa9459Szrj 32*a9fa9459Szrj namespace gold 33*a9fa9459Szrj { 34*a9fa9459Szrj 35*a9fa9459Szrj struct Parser_output_section_header; 36*a9fa9459Szrj struct Parser_output_section_trailer; 37*a9fa9459Szrj struct Input_section_spec; 38*a9fa9459Szrj class Expression; 39*a9fa9459Szrj class Sections_element; 40*a9fa9459Szrj class Memory_region; 41*a9fa9459Szrj class Phdrs_element; 42*a9fa9459Szrj class Output_data; 43*a9fa9459Szrj class Output_section_definition; 44*a9fa9459Szrj class Output_section; 45*a9fa9459Szrj class Output_segment; 46*a9fa9459Szrj class Orphan_section_placement; 47*a9fa9459Szrj 48*a9fa9459Szrj class Script_sections 49*a9fa9459Szrj { 50*a9fa9459Szrj public: 51*a9fa9459Szrj // This is a list, not a vector, because we insert orphan sections 52*a9fa9459Szrj // in the middle. 53*a9fa9459Szrj typedef std::list<Sections_element*> Sections_elements; 54*a9fa9459Szrj 55*a9fa9459Szrj // Logical script section types. We map section types returned by the 56*a9fa9459Szrj // parser into these since some section types have the same semantics. 57*a9fa9459Szrj enum Section_type 58*a9fa9459Szrj { 59*a9fa9459Szrj // No section type specified. 60*a9fa9459Szrj ST_NONE, 61*a9fa9459Szrj // Section is NOLOAD. We allocate space in the output but section 62*a9fa9459Szrj // is not loaded in runtime. 63*a9fa9459Szrj ST_NOLOAD, 64*a9fa9459Szrj // No space is allocated to section. 65*a9fa9459Szrj ST_NOALLOC 66*a9fa9459Szrj }; 67*a9fa9459Szrj 68*a9fa9459Szrj Script_sections(); 69*a9fa9459Szrj 70*a9fa9459Szrj // Start a SECTIONS clause. 71*a9fa9459Szrj void 72*a9fa9459Szrj start_sections(); 73*a9fa9459Szrj 74*a9fa9459Szrj // Finish a SECTIONS clause. 75*a9fa9459Szrj void 76*a9fa9459Szrj finish_sections(); 77*a9fa9459Szrj 78*a9fa9459Szrj // Return whether we ever saw a SECTIONS clause. If we did, then 79*a9fa9459Szrj // all section layout needs to go through this class. 80*a9fa9459Szrj bool saw_sections_clause()81*a9fa9459Szrj saw_sections_clause() const 82*a9fa9459Szrj { return this->saw_sections_clause_; } 83*a9fa9459Szrj 84*a9fa9459Szrj // Return whether we are currently processing a SECTIONS clause. 85*a9fa9459Szrj bool in_sections_clause()86*a9fa9459Szrj in_sections_clause() const 87*a9fa9459Szrj { return this->in_sections_clause_; } 88*a9fa9459Szrj 89*a9fa9459Szrj // Return whether we ever saw a PHDRS clause. We ignore the PHDRS 90*a9fa9459Szrj // clause unless we also saw a SECTIONS clause. 91*a9fa9459Szrj bool saw_phdrs_clause()92*a9fa9459Szrj saw_phdrs_clause() const 93*a9fa9459Szrj { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; } 94*a9fa9459Szrj 95*a9fa9459Szrj // Start processing entries for an output section. 96*a9fa9459Szrj void 97*a9fa9459Szrj start_output_section(const char* name, size_t namelen, 98*a9fa9459Szrj const Parser_output_section_header*); 99*a9fa9459Szrj 100*a9fa9459Szrj // Finish processing entries for an output section. 101*a9fa9459Szrj void 102*a9fa9459Szrj finish_output_section(const Parser_output_section_trailer*); 103*a9fa9459Szrj 104*a9fa9459Szrj // Add a data item to the current output section. 105*a9fa9459Szrj void 106*a9fa9459Szrj add_data(int size, bool is_signed, Expression* val); 107*a9fa9459Szrj 108*a9fa9459Szrj // Add a symbol to be defined. 109*a9fa9459Szrj void 110*a9fa9459Szrj add_symbol_assignment(const char* name, size_t length, Expression* value, 111*a9fa9459Szrj bool provide, bool hidden); 112*a9fa9459Szrj 113*a9fa9459Szrj // Add an assignment to the special dot symbol. 114*a9fa9459Szrj void 115*a9fa9459Szrj add_dot_assignment(Expression* value); 116*a9fa9459Szrj 117*a9fa9459Szrj // Add an assertion. 118*a9fa9459Szrj void 119*a9fa9459Szrj add_assertion(Expression* check, const char* message, size_t messagelen); 120*a9fa9459Szrj 121*a9fa9459Szrj // Add a setting for the fill value. 122*a9fa9459Szrj void 123*a9fa9459Szrj add_fill(Expression* val); 124*a9fa9459Szrj 125*a9fa9459Szrj // Add an input section specification. 126*a9fa9459Szrj void 127*a9fa9459Szrj add_input_section(const Input_section_spec* spec, bool keep); 128*a9fa9459Szrj 129*a9fa9459Szrj // Saw DATA_SEGMENT_ALIGN. 130*a9fa9459Szrj void 131*a9fa9459Szrj data_segment_align(); 132*a9fa9459Szrj 133*a9fa9459Szrj // Saw DATA_SEGMENT_RELRO_END. 134*a9fa9459Szrj void 135*a9fa9459Szrj data_segment_relro_end(); 136*a9fa9459Szrj 137*a9fa9459Szrj // Create any required sections. 138*a9fa9459Szrj void 139*a9fa9459Szrj create_sections(Layout*); 140*a9fa9459Szrj 141*a9fa9459Szrj // Add any symbols we are defining to the symbol table. 142*a9fa9459Szrj void 143*a9fa9459Szrj add_symbols_to_table(Symbol_table*); 144*a9fa9459Szrj 145*a9fa9459Szrj // Finalize symbol values and check assertions. 146*a9fa9459Szrj void 147*a9fa9459Szrj finalize_symbols(Symbol_table* symtab, const Layout* layout); 148*a9fa9459Szrj 149*a9fa9459Szrj // Find the name of the output section to use for an input file name 150*a9fa9459Szrj // and section name. This returns a name, and sets 151*a9fa9459Szrj // *OUTPUT_SECTION_SLOT to point to the address where the actual 152*a9fa9459Szrj // output section may be stored. 153*a9fa9459Szrj // 1) If the input section should be discarded, this returns NULL 154*a9fa9459Szrj // and sets *OUTPUT_SECTION_SLOT to NULL. 155*a9fa9459Szrj // 2) If the input section is mapped by the SECTIONS clause, this 156*a9fa9459Szrj // returns the name to use for the output section (in permanent 157*a9fa9459Szrj // storage), and sets *OUTPUT_SECTION_SLOT to point to where the 158*a9fa9459Szrj // output section should be stored. **OUTPUT_SECTION_SLOT will be 159*a9fa9459Szrj // non-NULL if we have seen this output section already. 160*a9fa9459Szrj // 3) If the input section is not mapped by the SECTIONS clause, 161*a9fa9459Szrj // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to 162*a9fa9459Szrj // NULL. 163*a9fa9459Szrj // PSCRIPT_SECTION_TYPE points to a location for returning the section 164*a9fa9459Szrj // type specified in script. This can be SCRIPT_SECTION_TYPE_NONE if 165*a9fa9459Szrj // no type is specified. 166*a9fa9459Szrj // *KEEP indicates whether the section should survive garbage collection. 167*a9fa9459Szrj const char* 168*a9fa9459Szrj output_section_name(const char* file_name, const char* section_name, 169*a9fa9459Szrj Output_section*** output_section_slot, 170*a9fa9459Szrj Section_type* pscript_section_type, 171*a9fa9459Szrj bool* keep); 172*a9fa9459Szrj 173*a9fa9459Szrj // Place a marker for an orphan output section into the SECTIONS 174*a9fa9459Szrj // clause. 175*a9fa9459Szrj void 176*a9fa9459Szrj place_orphan(Output_section* os); 177*a9fa9459Szrj 178*a9fa9459Szrj // Set the addresses of all the output sections. Return the segment 179*a9fa9459Szrj // which holds the file header and segment headers, if any. 180*a9fa9459Szrj Output_segment* 181*a9fa9459Szrj set_section_addresses(Symbol_table*, Layout*); 182*a9fa9459Szrj 183*a9fa9459Szrj // Add a program header definition. 184*a9fa9459Szrj void 185*a9fa9459Szrj add_phdr(const char* name, size_t namelen, unsigned int type, 186*a9fa9459Szrj bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags, 187*a9fa9459Szrj Expression* load_address); 188*a9fa9459Szrj 189*a9fa9459Szrj // Return the number of segments we expect to create based on the 190*a9fa9459Szrj // SECTIONS clause. 191*a9fa9459Szrj size_t 192*a9fa9459Szrj expected_segment_count(const Layout*) const; 193*a9fa9459Szrj 194*a9fa9459Szrj // Add the file header and segment header to non-load segments as 195*a9fa9459Szrj // specified by the PHDRS clause. 196*a9fa9459Szrj void 197*a9fa9459Szrj put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers); 198*a9fa9459Szrj 199*a9fa9459Szrj // Look for an output section by name and return the address, the 200*a9fa9459Szrj // load address, the alignment, and the size. This is used when an 201*a9fa9459Szrj // expression refers to an output section which was not actually 202*a9fa9459Szrj // created. This returns true if the section was found, false 203*a9fa9459Szrj // otherwise. 204*a9fa9459Szrj bool 205*a9fa9459Szrj get_output_section_info(const char* name, uint64_t* address, 206*a9fa9459Szrj uint64_t* load_address, uint64_t* addralign, 207*a9fa9459Szrj uint64_t* size) const; 208*a9fa9459Szrj 209*a9fa9459Szrj // Release all Output_segments. This is used in relaxation. 210*a9fa9459Szrj void 211*a9fa9459Szrj release_segments(); 212*a9fa9459Szrj 213*a9fa9459Szrj // Whether we ever saw a SEGMENT_START expression, the presence of which 214*a9fa9459Szrj // changes the behaviour of -Ttext, -Tdata and -Tbss options. 215*a9fa9459Szrj bool saw_segment_start_expression()216*a9fa9459Szrj saw_segment_start_expression() const 217*a9fa9459Szrj { return this->saw_segment_start_expression_; } 218*a9fa9459Szrj 219*a9fa9459Szrj // Set the flag which indicates whether we saw a SEGMENT_START expression. 220*a9fa9459Szrj void set_saw_segment_start_expression(bool value)221*a9fa9459Szrj set_saw_segment_start_expression(bool value) 222*a9fa9459Szrj { this->saw_segment_start_expression_ = value; } 223*a9fa9459Szrj 224*a9fa9459Szrj // Add a memory region. 225*a9fa9459Szrj void 226*a9fa9459Szrj add_memory_region(const char*, size_t, unsigned int, 227*a9fa9459Szrj Expression*, Expression*); 228*a9fa9459Szrj 229*a9fa9459Szrj // Find a memory region's origin. 230*a9fa9459Szrj Expression* 231*a9fa9459Szrj find_memory_region_origin(const char*, size_t); 232*a9fa9459Szrj 233*a9fa9459Szrj // Find a memory region's length. 234*a9fa9459Szrj Expression* 235*a9fa9459Szrj find_memory_region_length(const char*, size_t); 236*a9fa9459Szrj 237*a9fa9459Szrj // Find a memory region by name. 238*a9fa9459Szrj Memory_region* 239*a9fa9459Szrj find_memory_region(const char*, size_t); 240*a9fa9459Szrj 241*a9fa9459Szrj // Find a memory region that should be used by a given output section. 242*a9fa9459Szrj Memory_region* 243*a9fa9459Szrj find_memory_region(Output_section_definition*, bool, bool, 244*a9fa9459Szrj Output_section_definition**); 245*a9fa9459Szrj 246*a9fa9459Szrj // Returns true if the provide block of memory is contained 247*a9fa9459Szrj // within a memory region. 248*a9fa9459Szrj bool 249*a9fa9459Szrj block_in_region(Symbol_table*, Layout*, uint64_t, uint64_t) const; 250*a9fa9459Szrj 251*a9fa9459Szrj // Set the memory region of the section. 252*a9fa9459Szrj void 253*a9fa9459Szrj set_memory_region(Memory_region*, bool); 254*a9fa9459Szrj 255*a9fa9459Szrj // Print the contents to the FILE. This is for debugging. 256*a9fa9459Szrj void 257*a9fa9459Szrj print(FILE*) const; 258*a9fa9459Szrj 259*a9fa9459Szrj // Used for orphan sections. 260*a9fa9459Szrj typedef Sections_elements::iterator Elements_iterator; 261*a9fa9459Szrj 262*a9fa9459Szrj private: 263*a9fa9459Szrj typedef std::vector<Memory_region*> Memory_regions; 264*a9fa9459Szrj typedef std::vector<Phdrs_element*> Phdrs_elements; 265*a9fa9459Szrj 266*a9fa9459Szrj // Create segments. 267*a9fa9459Szrj Output_segment* 268*a9fa9459Szrj create_segments(Layout*, uint64_t); 269*a9fa9459Szrj 270*a9fa9459Szrj // Create PT_NOTE and PT_TLS segments. 271*a9fa9459Szrj void 272*a9fa9459Szrj create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*); 273*a9fa9459Szrj 274*a9fa9459Szrj // Return whether the section is a BSS section. 275*a9fa9459Szrj static bool 276*a9fa9459Szrj is_bss_section(const Output_section*); 277*a9fa9459Szrj 278*a9fa9459Szrj // Return the total size of the headers. 279*a9fa9459Szrj size_t 280*a9fa9459Szrj total_header_size(Layout* layout) const; 281*a9fa9459Szrj 282*a9fa9459Szrj // Return the amount we have to subtract from the LMA to accomodate 283*a9fa9459Szrj // headers of the given size. 284*a9fa9459Szrj uint64_t 285*a9fa9459Szrj header_size_adjustment(uint64_t lma, size_t sizeof_headers) const; 286*a9fa9459Szrj 287*a9fa9459Szrj // Create the segments from a PHDRS clause. 288*a9fa9459Szrj Output_segment* 289*a9fa9459Szrj create_segments_from_phdrs_clause(Layout* layout, uint64_t); 290*a9fa9459Szrj 291*a9fa9459Szrj // Attach sections to segments from a PHDRS clause. 292*a9fa9459Szrj void 293*a9fa9459Szrj attach_sections_using_phdrs_clause(Layout*); 294*a9fa9459Szrj 295*a9fa9459Szrj // Set addresses of segments from a PHDRS clause. 296*a9fa9459Szrj Output_segment* 297*a9fa9459Szrj set_phdrs_clause_addresses(Layout*, uint64_t); 298*a9fa9459Szrj 299*a9fa9459Szrj // True if we ever saw a SECTIONS clause. 300*a9fa9459Szrj bool saw_sections_clause_; 301*a9fa9459Szrj // True if we are currently processing a SECTIONS clause. 302*a9fa9459Szrj bool in_sections_clause_; 303*a9fa9459Szrj // The list of elements in the SECTIONS clause. 304*a9fa9459Szrj Sections_elements* sections_elements_; 305*a9fa9459Szrj // The current output section, if there is one. 306*a9fa9459Szrj Output_section_definition* output_section_; 307*a9fa9459Szrj // The list of memory regions in the MEMORY clause. 308*a9fa9459Szrj Memory_regions* memory_regions_; 309*a9fa9459Szrj // The list of program headers in the PHDRS clause. 310*a9fa9459Szrj Phdrs_elements* phdrs_elements_; 311*a9fa9459Szrj // Where to put orphan sections. 312*a9fa9459Szrj Orphan_section_placement* orphan_section_placement_; 313*a9fa9459Szrj // A pointer to the last Sections_element when we see 314*a9fa9459Szrj // DATA_SEGMENT_ALIGN. 315*a9fa9459Szrj Sections_elements::iterator data_segment_align_start_; 316*a9fa9459Szrj // Whether we have seen DATA_SEGMENT_ALIGN. 317*a9fa9459Szrj bool saw_data_segment_align_; 318*a9fa9459Szrj // Whether we have seen DATA_SEGMENT_RELRO_END. 319*a9fa9459Szrj bool saw_relro_end_; 320*a9fa9459Szrj // Whether we have seen SEGMENT_START. 321*a9fa9459Szrj bool saw_segment_start_expression_; 322*a9fa9459Szrj // Whether we have created all necessary segments. 323*a9fa9459Szrj bool segments_created_; 324*a9fa9459Szrj }; 325*a9fa9459Szrj 326*a9fa9459Szrj // Attributes for memory regions. 327*a9fa9459Szrj enum 328*a9fa9459Szrj { 329*a9fa9459Szrj MEM_EXECUTABLE = (1 << 0), 330*a9fa9459Szrj MEM_WRITEABLE = (1 << 1), 331*a9fa9459Szrj MEM_READABLE = (1 << 2), 332*a9fa9459Szrj MEM_ALLOCATABLE = (1 << 3), 333*a9fa9459Szrj MEM_INITIALIZED = (1 << 4), 334*a9fa9459Szrj MEM_ATTR_MASK = (1 << 5) - 1 335*a9fa9459Szrj }; 336*a9fa9459Szrj 337*a9fa9459Szrj } // End namespace gold. 338*a9fa9459Szrj 339*a9fa9459Szrj #endif // !defined(GOLD_SCRIPT_SECTIONS_H 340