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