PR binutils/13534
[binutils.git] / gold / script-sections.h
blob5b781524276a6b8d0ab1f38bf77f1829be66e363
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 Memory_region;
41 class Phdrs_element;
42 class Output_data;
43 class Output_section_definition;
44 class Output_section;
45 class Output_segment;
46 class Orphan_section_placement;
48 class Script_sections
50 public:
51 // This is a list, not a vector, because we insert orphan sections
52 // in the middle.
53 typedef std::list<Sections_element*> Sections_elements;
55 // Logical script section types. We map section types returned by the
56 // parser into these since some section types have the same semantics.
57 enum Section_type
59 // No section type specified.
60 ST_NONE,
61 // Section is NOLOAD. We allocate space in the output but section
62 // is not loaded in runtime.
63 ST_NOLOAD,
64 // No space is allocated to section.
65 ST_NOALLOC
68 Script_sections();
70 // Start a SECTIONS clause.
71 void
72 start_sections();
74 // Finish a SECTIONS clause.
75 void
76 finish_sections();
78 // Return whether we ever saw a SECTIONS clause. If we did, then
79 // all section layout needs to go through this class.
80 bool
81 saw_sections_clause() const
82 { return this->saw_sections_clause_; }
84 // Return whether we are currently processing a SECTIONS clause.
85 bool
86 in_sections_clause() const
87 { return this->in_sections_clause_; }
89 // Return whether we ever saw a PHDRS clause. We ignore the PHDRS
90 // clause unless we also saw a SECTIONS clause.
91 bool
92 saw_phdrs_clause() const
93 { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; }
95 // Start processing entries for an output section.
96 void
97 start_output_section(const char* name, size_t namelen,
98 const Parser_output_section_header*);
100 // Finish processing entries for an output section.
101 void
102 finish_output_section(const Parser_output_section_trailer*);
104 // Add a data item to the current output section.
105 void
106 add_data(int size, bool is_signed, Expression* val);
108 // Add a symbol to be defined.
109 void
110 add_symbol_assignment(const char* name, size_t length, Expression* value,
111 bool provide, bool hidden);
113 // Add an assignment to the special dot symbol.
114 void
115 add_dot_assignment(Expression* value);
117 // Add an assertion.
118 void
119 add_assertion(Expression* check, const char* message, size_t messagelen);
121 // Add a setting for the fill value.
122 void
123 add_fill(Expression* val);
125 // Add an input section specification.
126 void
127 add_input_section(const Input_section_spec* spec, bool keep);
129 // Saw DATA_SEGMENT_ALIGN.
130 void
131 data_segment_align();
133 // Saw DATA_SEGMENT_RELRO_END.
134 void
135 data_segment_relro_end();
137 // Create any required sections.
138 void
139 create_sections(Layout*);
141 // Add any symbols we are defining to the symbol table.
142 void
143 add_symbols_to_table(Symbol_table*);
145 // Finalize symbol values and check assertions.
146 void
147 finalize_symbols(Symbol_table* symtab, const Layout* layout);
149 // Find the name of the output section to use for an input file name
150 // and section name. This returns a name, and sets
151 // *OUTPUT_SECTION_SLOT to point to the address where the actual
152 // output section may be stored.
153 // 1) If the input section should be discarded, this returns NULL
154 // and sets *OUTPUT_SECTION_SLOT to NULL.
155 // 2) If the input section is mapped by the SECTIONS clause, this
156 // returns the name to use for the output section (in permanent
157 // storage), and sets *OUTPUT_SECTION_SLOT to point to where the
158 // output section should be stored. **OUTPUT_SECTION_SLOT will be
159 // non-NULL if we have seen this output section already.
160 // 3) If the input section is not mapped by the SECTIONS clause,
161 // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
162 // NULL.
163 // PSCRIPT_SECTION_TYPE points to a location for returning the section
164 // type specified in script. This can be SCRIPT_SECTION_TYPE_NONE if
165 // no type is specified.
166 const char*
167 output_section_name(const char* file_name, const char* section_name,
168 Output_section*** output_section_slot,
169 Section_type* pscript_section_type);
171 // Place a marker for an orphan output section into the SECTIONS
172 // clause.
173 void
174 place_orphan(Output_section* os);
176 // Set the addresses of all the output sections. Return the segment
177 // which holds the file header and segment headers, if any.
178 Output_segment*
179 set_section_addresses(Symbol_table*, Layout*);
181 // Add a program header definition.
182 void
183 add_phdr(const char* name, size_t namelen, unsigned int type,
184 bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
185 Expression* load_address);
187 // Return the number of segments we expect to create based on the
188 // SECTIONS clause.
189 size_t
190 expected_segment_count(const Layout*) const;
192 // Add the file header and segment header to non-load segments as
193 // specified by the PHDRS clause.
194 void
195 put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
197 // Look for an output section by name and return the address, the
198 // load address, the alignment, and the size. This is used when an
199 // expression refers to an output section which was not actually
200 // created. This returns true if the section was found, false
201 // otherwise.
202 bool
203 get_output_section_info(const char* name, uint64_t* address,
204 uint64_t* load_address, uint64_t* addralign,
205 uint64_t* size) const;
207 // Release all Output_segments. This is used in relaxation.
208 void
209 release_segments();
211 // Whether we ever saw a SEGMENT_START expression, the presence of which
212 // changes the behaviour of -Ttext, -Tdata and -Tbss options.
213 bool
214 saw_segment_start_expression() const
215 { return this->saw_segment_start_expression_; }
217 // Set the flag which indicates whether we saw a SEGMENT_START expression.
218 void
219 set_saw_segment_start_expression(bool value)
220 { this->saw_segment_start_expression_ = value; }
222 // Add a memory region.
223 void
224 add_memory_region(const char*, size_t, unsigned int,
225 Expression*, Expression*);
227 // Find a memory region's origin.
228 Expression*
229 find_memory_region_origin(const char*, size_t);
231 // Find a memory region's length.
232 Expression*
233 find_memory_region_length(const char*, size_t);
235 // Find a memory region by name.
236 Memory_region*
237 find_memory_region(const char*, size_t);
239 // Find a memory region that should be used by a given output section.
240 Memory_region*
241 find_memory_region(Output_section_definition*, bool,
242 Output_section_definition**);
244 // Returns true if the provide block of memory is contained
245 // within a memory region.
246 bool
247 block_in_region(Symbol_table*, Layout*, uint64_t, uint64_t) const;
249 // Set the memory region of the section.
250 void
251 set_memory_region(Memory_region*, bool);
253 // Print the contents to the FILE. This is for debugging.
254 void
255 print(FILE*) const;
257 // Used for orphan sections.
258 typedef Sections_elements::iterator Elements_iterator;
260 private:
261 typedef std::vector<Memory_region*> Memory_regions;
262 typedef std::vector<Phdrs_element*> Phdrs_elements;
264 // Create segments.
265 Output_segment*
266 create_segments(Layout*, uint64_t);
268 // Create PT_NOTE and PT_TLS segments.
269 void
270 create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
272 // Return whether the section is a BSS section.
273 static bool
274 is_bss_section(const Output_section*);
276 // Return the total size of the headers.
277 size_t
278 total_header_size(Layout* layout) const;
280 // Return the amount we have to subtract from the LMA to accomodate
281 // headers of the given size.
282 uint64_t
283 header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
285 // Create the segments from a PHDRS clause.
286 Output_segment*
287 create_segments_from_phdrs_clause(Layout* layout, uint64_t);
289 // Attach sections to segments from a PHDRS clause.
290 void
291 attach_sections_using_phdrs_clause(Layout*);
293 // Set addresses of segments from a PHDRS clause.
294 Output_segment*
295 set_phdrs_clause_addresses(Layout*, uint64_t);
297 // True if we ever saw a SECTIONS clause.
298 bool saw_sections_clause_;
299 // True if we are currently processing a SECTIONS clause.
300 bool in_sections_clause_;
301 // The list of elements in the SECTIONS clause.
302 Sections_elements* sections_elements_;
303 // The current output section, if there is one.
304 Output_section_definition* output_section_;
305 // The list of memory regions in the MEMORY clause.
306 Memory_regions* memory_regions_;
307 // The list of program headers in the PHDRS clause.
308 Phdrs_elements* phdrs_elements_;
309 // Where to put orphan sections.
310 Orphan_section_placement* orphan_section_placement_;
311 // A pointer to the last Sections_element when we see
312 // DATA_SEGMENT_ALIGN.
313 Sections_elements::iterator data_segment_align_start_;
314 // Whether we have seen DATA_SEGMENT_ALIGN.
315 bool saw_data_segment_align_;
316 // Whether we have seen DATA_SEGMENT_RELRO_END.
317 bool saw_relro_end_;
318 // Whether we have seen SEGMENT_START.
319 bool saw_segment_start_expression_;
322 // Attributes for memory regions.
323 enum
325 MEM_EXECUTABLE = (1 << 0),
326 MEM_WRITEABLE = (1 << 1),
327 MEM_READABLE = (1 << 2),
328 MEM_ALLOCATABLE = (1 << 3),
329 MEM_INITIALIZED = (1 << 4),
330 MEM_ATTR_MASK = (1 << 5) - 1
333 } // End namespace gold.
335 #endif // !defined(GOLD_SCRIPT_SECTIONS_H