| 1 | // script-sections.h -- linker script SECTIONS for gold -*- C++ -*- |
| 2 | |
| 3 | // Copyright 2008 Free Software Foundation, Inc. |
| 4 | // Written by Ian Lance Taylor <iant@google.com>. |
| 5 | |
| 6 | // This file is part of gold. |
| 7 | |
| 8 | // This program is free software; you can redistribute it and/or modify |
| 9 | // it under the terms of the GNU General Public License as published by |
| 10 | // the Free Software Foundation; either version 3 of the License, or |
| 11 | // (at your option) any later version. |
| 12 | |
| 13 | // This program is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 21 | // MA 02110-1301, USA. |
| 22 | |
| 23 | // This is for the support of the SECTIONS clause in linker scripts. |
| 24 | |
| 25 | #ifndef GOLD_SCRIPT_SECTIONS_H |
| 26 | #define GOLD_SCRIPT_SECTIONS_H |
| 27 | |
| 28 | #include <cstdio> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace gold |
| 32 | { |
| 33 | |
| 34 | struct Parser_output_section_header; |
| 35 | struct Parser_output_section_trailer; |
| 36 | struct Input_section_spec; |
| 37 | class Expression; |
| 38 | class Sections_element; |
| 39 | class Phdrs_element; |
| 40 | class Output_data; |
| 41 | class Output_section_definition; |
| 42 | class Output_section; |
| 43 | class Output_segment; |
| 44 | |
| 45 | class Script_sections |
| 46 | { |
| 47 | public: |
| 48 | Script_sections(); |
| 49 | |
| 50 | // Start a SECTIONS clause. |
| 51 | void |
| 52 | start_sections(); |
| 53 | |
| 54 | // Finish a SECTIONS clause. |
| 55 | void |
| 56 | finish_sections(); |
| 57 | |
| 58 | // Return whether we ever saw a SECTIONS clause. If we did, then |
| 59 | // all section layout needs to go through this class. |
| 60 | bool |
| 61 | saw_sections_clause() const |
| 62 | { return this->saw_sections_clause_; } |
| 63 | |
| 64 | // Return whether we are currently processing a SECTIONS clause. |
| 65 | bool |
| 66 | in_sections_clause() const |
| 67 | { return this->in_sections_clause_; } |
| 68 | |
| 69 | // Return whether we ever saw a PHDRS clause. We ignore the PHDRS |
| 70 | // clause unless we also saw a SECTIONS clause. |
| 71 | bool |
| 72 | saw_phdrs_clause() const |
| 73 | { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; } |
| 74 | |
| 75 | // Start processing entries for an output section. |
| 76 | void |
| 77 | start_output_section(const char* name, size_t namelen, |
| 78 | const Parser_output_section_header*); |
| 79 | |
| 80 | // Finish processing entries for an output section. |
| 81 | void |
| 82 | finish_output_section(const Parser_output_section_trailer*); |
| 83 | |
| 84 | // Add a data item to the current output section. |
| 85 | void |
| 86 | add_data(int size, bool is_signed, Expression* val); |
| 87 | |
| 88 | // Add a symbol to be defined. |
| 89 | void |
| 90 | add_symbol_assignment(const char* name, size_t length, Expression* value, |
| 91 | bool provide, bool hidden); |
| 92 | |
| 93 | // Add an assignment to the special dot symbol. |
| 94 | void |
| 95 | add_dot_assignment(Expression* value); |
| 96 | |
| 97 | // Add an assertion. |
| 98 | void |
| 99 | add_assertion(Expression* check, const char* message, size_t messagelen); |
| 100 | |
| 101 | // Add a setting for the fill value. |
| 102 | void |
| 103 | add_fill(Expression* val); |
| 104 | |
| 105 | // Add an input section specification. |
| 106 | void |
| 107 | add_input_section(const Input_section_spec* spec, bool keep); |
| 108 | |
| 109 | // Create any required sections. |
| 110 | void |
| 111 | create_sections(Layout*); |
| 112 | |
| 113 | // Add any symbols we are defining to the symbol table. |
| 114 | void |
| 115 | add_symbols_to_table(Symbol_table*); |
| 116 | |
| 117 | // Finalize symbol values and check assertions. |
| 118 | void |
| 119 | finalize_symbols(Symbol_table* symtab, const Layout* layout); |
| 120 | |
| 121 | // Find the name of the output section to use for an input file name |
| 122 | // and section name. This returns a name, and sets |
| 123 | // *OUTPUT_SECTION_SLOT to point to the address where the actual |
| 124 | // output section may be stored. |
| 125 | // 1) If the input section should be discarded, this returns NULL |
| 126 | // and sets *OUTPUT_SECTION_SLOT to NULL. |
| 127 | // 2) If the input section is mapped by the SECTIONS clause, this |
| 128 | // returns the name to use for the output section (in permanent |
| 129 | // storage), and sets *OUTPUT_SECTION_SLOT to point to where the |
| 130 | // output section should be stored. **OUTPUT_SECTION_SLOT will be |
| 131 | // non-NULL if we have seen this output section already. |
| 132 | // 3) If the input section is not mapped by the SECTIONS clause, |
| 133 | // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to |
| 134 | // NULL. |
| 135 | const char* |
| 136 | output_section_name(const char* file_name, const char* section_name, |
| 137 | Output_section*** output_section_slot); |
| 138 | |
| 139 | // Place a marker for an orphan output section into the SECTIONS |
| 140 | // clause. |
| 141 | void |
| 142 | place_orphan(Output_section* os); |
| 143 | |
| 144 | // Set the addresses of all the output sections. Return the segment |
| 145 | // which holds the file header and segment headers, if any. |
| 146 | Output_segment* |
| 147 | set_section_addresses(Symbol_table*, Layout*); |
| 148 | |
| 149 | // Add a program header definition. |
| 150 | void |
| 151 | add_phdr(const char* name, size_t namelen, unsigned int type, |
| 152 | bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags, |
| 153 | Expression* load_address); |
| 154 | |
| 155 | // Return the number of segments we expect to create based on the |
| 156 | // SECTIONS clause. |
| 157 | size_t |
| 158 | expected_segment_count(const Layout*) const; |
| 159 | |
| 160 | // Add the file header and segment header to non-load segments as |
| 161 | // specified by the PHDRS clause. |
| 162 | void |
| 163 | put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers); |
| 164 | |
| 165 | // Look for an output section by name and return the address, the |
| 166 | // load address, the alignment, and the size. This is used when an |
| 167 | // expression refers to an output section which was not actually |
| 168 | // created. This returns true if the section was found, false |
| 169 | // otherwise. |
| 170 | bool |
| 171 | get_output_section_info(const char* name, uint64_t* address, |
| 172 | uint64_t* load_address, uint64_t* addralign, |
| 173 | uint64_t* size) const; |
| 174 | |
| 175 | // Print the contents to the FILE. This is for debugging. |
| 176 | void |
| 177 | print(FILE*) const; |
| 178 | |
| 179 | private: |
| 180 | typedef std::vector<Sections_element*> Sections_elements; |
| 181 | |
| 182 | typedef std::vector<Phdrs_element*> Phdrs_elements; |
| 183 | |
| 184 | // Create segments. |
| 185 | Output_segment* |
| 186 | create_segments(Layout*); |
| 187 | |
| 188 | // Create PT_NOTE and PT_TLS segments. |
| 189 | void |
| 190 | create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*); |
| 191 | |
| 192 | // Return whether the section is a BSS section. |
| 193 | static bool |
| 194 | is_bss_section(const Output_section*); |
| 195 | |
| 196 | // Return the total size of the headers. |
| 197 | size_t |
| 198 | total_header_size(Layout* layout) const; |
| 199 | |
| 200 | // Return the amount we have to subtract from the LMA to accomodate |
| 201 | // headers of the given size. |
| 202 | uint64_t |
| 203 | header_size_adjustment(uint64_t lma, size_t sizeof_headers) const; |
| 204 | |
| 205 | // Create the segments from a PHDRS clause. |
| 206 | Output_segment* |
| 207 | create_segments_from_phdrs_clause(Layout* layout); |
| 208 | |
| 209 | // Attach sections to segments from a PHDRS clause. |
| 210 | void |
| 211 | attach_sections_using_phdrs_clause(Layout*); |
| 212 | |
| 213 | // Set addresses of segments from a PHDRS clause. |
| 214 | Output_segment* |
| 215 | set_phdrs_clause_addresses(Layout*); |
| 216 | |
| 217 | // True if we ever saw a SECTIONS clause. |
| 218 | bool saw_sections_clause_; |
| 219 | // True if we are currently processing a SECTIONS clause. |
| 220 | bool in_sections_clause_; |
| 221 | // The list of elements in the SECTIONS clause. |
| 222 | Sections_elements* sections_elements_; |
| 223 | // The current output section, if there is one. |
| 224 | Output_section_definition* output_section_; |
| 225 | // The list of program headers in the PHDRS clause. |
| 226 | Phdrs_elements* phdrs_elements_; |
| 227 | }; |
| 228 | |
| 229 | } // End namespace gold. |
| 230 | |
| 231 | #endif // !defined(GOLD_SCRIPT_SECTIONS_H |