* gas/arm/thumb2_bcond.d: Allow for varying number of nops at the
[binutils/dougsmingw.git] / gold / script-sections.h
blobc0d1d081c24e21c63574c4a3c9db693a78f6d7a8
1 // script-sections.h -- linker script SECTIONS for gold -*- C++ -*-
3 // Copyright 2008, 2009 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
28 #include <cstdio>
29 #include <list>
30 #include <vector>
32 namespace gold
35 struct Parser_output_section_header;
36 struct Parser_output_section_trailer;
37 struct Input_section_spec;
38 class Expression;
39 class Sections_element;
40 class Phdrs_element;
41 class Output_data;
42 class Output_section_definition;
43 class Output_section;
44 class Output_segment;
45 class Orphan_section_placement;
47 class Script_sections
49 private:
50 // This is a list, not a vector, because we insert orphan sections
51 // in the middle.
52 typedef std::list<Sections_element*> Sections_elements;
54 public:
55 Script_sections();
57 // Start a SECTIONS clause.
58 void
59 start_sections();
61 // Finish a SECTIONS clause.
62 void
63 finish_sections();
65 // Return whether we ever saw a SECTIONS clause. If we did, then
66 // all section layout needs to go through this class.
67 bool
68 saw_sections_clause() const
69 { return this->saw_sections_clause_; }
71 // Return whether we are currently processing a SECTIONS clause.
72 bool
73 in_sections_clause() const
74 { return this->in_sections_clause_; }
76 // Return whether we ever saw a PHDRS clause. We ignore the PHDRS
77 // clause unless we also saw a SECTIONS clause.
78 bool
79 saw_phdrs_clause() const
80 { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; }
82 // Start processing entries for an output section.
83 void
84 start_output_section(const char* name, size_t namelen,
85 const Parser_output_section_header*);
87 // Finish processing entries for an output section.
88 void
89 finish_output_section(const Parser_output_section_trailer*);
91 // Add a data item to the current output section.
92 void
93 add_data(int size, bool is_signed, Expression* val);
95 // Add a symbol to be defined.
96 void
97 add_symbol_assignment(const char* name, size_t length, Expression* value,
98 bool provide, bool hidden);
100 // Add an assignment to the special dot symbol.
101 void
102 add_dot_assignment(Expression* value);
104 // Add an assertion.
105 void
106 add_assertion(Expression* check, const char* message, size_t messagelen);
108 // Add a setting for the fill value.
109 void
110 add_fill(Expression* val);
112 // Add an input section specification.
113 void
114 add_input_section(const Input_section_spec* spec, bool keep);
116 // Saw DATA_SEGMENT_ALIGN.
117 void
118 data_segment_align();
120 // Saw DATA_SEGMENT_RELRO_END.
121 void
122 data_segment_relro_end();
124 // Create any required sections.
125 void
126 create_sections(Layout*);
128 // Add any symbols we are defining to the symbol table.
129 void
130 add_symbols_to_table(Symbol_table*);
132 // Finalize symbol values and check assertions.
133 void
134 finalize_symbols(Symbol_table* symtab, const Layout* layout);
136 // Find the name of the output section to use for an input file name
137 // and section name. This returns a name, and sets
138 // *OUTPUT_SECTION_SLOT to point to the address where the actual
139 // output section may be stored.
140 // 1) If the input section should be discarded, this returns NULL
141 // and sets *OUTPUT_SECTION_SLOT to NULL.
142 // 2) If the input section is mapped by the SECTIONS clause, this
143 // returns the name to use for the output section (in permanent
144 // storage), and sets *OUTPUT_SECTION_SLOT to point to where the
145 // output section should be stored. **OUTPUT_SECTION_SLOT will be
146 // non-NULL if we have seen this output section already.
147 // 3) If the input section is not mapped by the SECTIONS clause,
148 // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
149 // NULL.
150 const char*
151 output_section_name(const char* file_name, const char* section_name,
152 Output_section*** output_section_slot);
154 // Place a marker for an orphan output section into the SECTIONS
155 // clause.
156 void
157 place_orphan(Output_section* os);
159 // Set the addresses of all the output sections. Return the segment
160 // which holds the file header and segment headers, if any.
161 Output_segment*
162 set_section_addresses(Symbol_table*, Layout*);
164 // Add a program header definition.
165 void
166 add_phdr(const char* name, size_t namelen, unsigned int type,
167 bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
168 Expression* load_address);
170 // Return the number of segments we expect to create based on the
171 // SECTIONS clause.
172 size_t
173 expected_segment_count(const Layout*) const;
175 // Add the file header and segment header to non-load segments as
176 // specified by the PHDRS clause.
177 void
178 put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
180 // Look for an output section by name and return the address, the
181 // load address, the alignment, and the size. This is used when an
182 // expression refers to an output section which was not actually
183 // created. This returns true if the section was found, false
184 // otherwise.
185 bool
186 get_output_section_info(const char* name, uint64_t* address,
187 uint64_t* load_address, uint64_t* addralign,
188 uint64_t* size) const;
190 // Release all Output_segments. This is used in relaxation.
191 void
192 release_segments();
194 // Whether we ever saw a SEGMENT_START expression, the presence of which
195 // changes the behaviour of -Ttext, -Tdata and -Tbss options.
196 bool
197 saw_segment_start_expression() const
198 { return this->saw_segment_start_expression_; }
200 // Set the flag which indicates whether we saw a SEGMENT_START expression.
201 void
202 set_saw_segment_start_expression(bool value)
203 { this->saw_segment_start_expression_ = value; }
205 // Print the contents to the FILE. This is for debugging.
206 void
207 print(FILE*) const;
209 // Used for orphan sections.
210 typedef Sections_elements::iterator Elements_iterator;
212 private:
213 typedef std::vector<Phdrs_element*> Phdrs_elements;
215 // Create segments.
216 Output_segment*
217 create_segments(Layout*);
219 // Create PT_NOTE and PT_TLS segments.
220 void
221 create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
223 // Return whether the section is a BSS section.
224 static bool
225 is_bss_section(const Output_section*);
227 // Return the total size of the headers.
228 size_t
229 total_header_size(Layout* layout) const;
231 // Return the amount we have to subtract from the LMA to accomodate
232 // headers of the given size.
233 uint64_t
234 header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
236 // Create the segments from a PHDRS clause.
237 Output_segment*
238 create_segments_from_phdrs_clause(Layout* layout);
240 // Attach sections to segments from a PHDRS clause.
241 void
242 attach_sections_using_phdrs_clause(Layout*);
244 // Set addresses of segments from a PHDRS clause.
245 Output_segment*
246 set_phdrs_clause_addresses(Layout*);
248 // True if we ever saw a SECTIONS clause.
249 bool saw_sections_clause_;
250 // True if we are currently processing a SECTIONS clause.
251 bool in_sections_clause_;
252 // The list of elements in the SECTIONS clause.
253 Sections_elements* sections_elements_;
254 // The current output section, if there is one.
255 Output_section_definition* output_section_;
256 // The list of program headers in the PHDRS clause.
257 Phdrs_elements* phdrs_elements_;
258 // Where to put orphan sections.
259 Orphan_section_placement* orphan_section_placement_;
260 // A pointer to the last Sections_element when we see
261 // DATA_SEGMENT_ALIGN.
262 Sections_elements::iterator data_segment_align_start_;
263 // Whether we have seen DATA_SEGMENT_ALIGN.
264 bool saw_data_segment_align_;
265 // Whether we have seen DATA_SEGMENT_RELRO_END.
266 bool saw_relro_end_;
267 // Whether we have seen SEGMENT_START.
268 bool saw_segment_start_expression_;
271 } // End namespace gold.
273 #endif // !defined(GOLD_SCRIPT_SECTIONS_H