1 // script-sections.h -- linker script SECTIONS for gold -*- C++ -*-
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 // This is for the support of the SECTIONS clause in linker scripts.
25 #ifndef GOLD_SCRIPT_SECTIONS_H
26 #define GOLD_SCRIPT_SECTIONS_H
34 struct Parser_output_section_header;
35 struct Parser_output_section_trailer;
36 struct Input_section_spec;
38 class Sections_element;
41 class Output_section_definition;
50 // Start a SECTIONS clause.
54 // Finish a SECTIONS clause.
58 // Return whether we ever saw a SECTIONS clause. If we did, then
59 // all section layout needs to go through this class.
61 saw_sections_clause() const
62 { return this->saw_sections_clause_; }
64 // Return whether we are currently processing a SECTIONS clause.
66 in_sections_clause() const
67 { return this->in_sections_clause_; }
69 // Return whether we ever saw a PHDRS clause. We ignore the PHDRS
70 // clause unless we also saw a SECTIONS clause.
72 saw_phdrs_clause() const
73 { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; }
75 // Start processing entries for an output section.
77 start_output_section(const char* name, size_t namelen,
78 const Parser_output_section_header*);
80 // Finish processing entries for an output section.
82 finish_output_section(const Parser_output_section_trailer*);
84 // Add a data item to the current output section.
86 add_data(int size, bool is_signed, Expression* val);
88 // Add a symbol to be defined.
90 add_symbol_assignment(const char* name, size_t length, Expression* value,
91 bool provide, bool hidden);
93 // Add an assignment to the special dot symbol.
95 add_dot_assignment(Expression* value);
99 add_assertion(Expression* check, const char* message, size_t messagelen);
101 // Add a setting for the fill value.
103 add_fill(Expression* val);
105 // Add an input section specification.
107 add_input_section(const Input_section_spec* spec, bool keep);
109 // Saw DATA_SEGMENT_ALIGN.
111 data_segment_align();
113 // Saw DATA_SEGMENT_RELRO_END.
115 data_segment_relro_end();
117 // Create any required sections.
119 create_sections(Layout*);
121 // Add any symbols we are defining to the symbol table.
123 add_symbols_to_table(Symbol_table*);
125 // Finalize symbol values and check assertions.
127 finalize_symbols(Symbol_table* symtab, const Layout* layout);
129 // Find the name of the output section to use for an input file name
130 // and section name. This returns a name, and sets
131 // *OUTPUT_SECTION_SLOT to point to the address where the actual
132 // output section may be stored.
133 // 1) If the input section should be discarded, this returns NULL
134 // and sets *OUTPUT_SECTION_SLOT to NULL.
135 // 2) If the input section is mapped by the SECTIONS clause, this
136 // returns the name to use for the output section (in permanent
137 // storage), and sets *OUTPUT_SECTION_SLOT to point to where the
138 // output section should be stored. **OUTPUT_SECTION_SLOT will be
139 // non-NULL if we have seen this output section already.
140 // 3) If the input section is not mapped by the SECTIONS clause,
141 // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
144 output_section_name(const char* file_name, const char* section_name,
145 Output_section*** output_section_slot);
147 // Place a marker for an orphan output section into the SECTIONS
150 place_orphan(Output_section* os);
152 // Set the addresses of all the output sections. Return the segment
153 // which holds the file header and segment headers, if any.
155 set_section_addresses(Symbol_table*, Layout*);
157 // Add a program header definition.
159 add_phdr(const char* name, size_t namelen, unsigned int type,
160 bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
161 Expression* load_address);
163 // Return the number of segments we expect to create based on the
166 expected_segment_count(const Layout*) const;
168 // Add the file header and segment header to non-load segments as
169 // specified by the PHDRS clause.
171 put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
173 // Look for an output section by name and return the address, the
174 // load address, the alignment, and the size. This is used when an
175 // expression refers to an output section which was not actually
176 // created. This returns true if the section was found, false
179 get_output_section_info(const char* name, uint64_t* address,
180 uint64_t* load_address, uint64_t* addralign,
181 uint64_t* size) const;
183 // Print the contents to the FILE. This is for debugging.
188 typedef std::vector<Sections_element*> Sections_elements;
190 typedef std::vector<Phdrs_element*> Phdrs_elements;
194 create_segments(Layout*);
196 // Create PT_NOTE and PT_TLS segments.
198 create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
200 // Return whether the section is a BSS section.
202 is_bss_section(const Output_section*);
204 // Return the total size of the headers.
206 total_header_size(Layout* layout) const;
208 // Return the amount we have to subtract from the LMA to accomodate
209 // headers of the given size.
211 header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
213 // Create the segments from a PHDRS clause.
215 create_segments_from_phdrs_clause(Layout* layout);
217 // Attach sections to segments from a PHDRS clause.
219 attach_sections_using_phdrs_clause(Layout*);
221 // Set addresses of segments from a PHDRS clause.
223 set_phdrs_clause_addresses(Layout*);
225 // True if we ever saw a SECTIONS clause.
226 bool saw_sections_clause_;
227 // True if we are currently processing a SECTIONS clause.
228 bool in_sections_clause_;
229 // The list of elements in the SECTIONS clause.
230 Sections_elements* sections_elements_;
231 // The current output section, if there is one.
232 Output_section_definition* output_section_;
233 // The list of program headers in the PHDRS clause.
234 Phdrs_elements* phdrs_elements_;
235 // The index of the next Sections_element when we see
236 // DATA_SEGMENT_ALIGN.
237 size_t data_segment_align_index_;
238 // Whether we have seen DATA_SEGMENT_RELRO_END.
242 } // End namespace gold.
244 #endif // !defined(GOLD_SCRIPT_SECTIONS_H