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