]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/script-sections.h
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gold / script-sections.h
CommitLineData
494e05f4
ILT
1// script-sections.h -- linker script SECTIONS for gold -*- C++ -*-
2
250d07de 3// Copyright (C) 2008-2021 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{
eb373049 50 public:
0d371ad3
ILT
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
1e5d2fb1
DK
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
58 {
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
66 };
67
494e05f4
ILT
68 Script_sections();
69
70 // Start a SECTIONS clause.
71 void
72 start_sections();
73
74 // Finish a SECTIONS clause.
75 void
76 finish_sections();
77
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_; }
83
84 // Return whether we are currently processing a SECTIONS clause.
85 bool
86 in_sections_clause() const
87 { return this->in_sections_clause_; }
88
1c4f3631
ILT
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; }
94
494e05f4
ILT
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*);
99
100 // Finish processing entries for an output section.
101 void
102 finish_output_section(const Parser_output_section_trailer*);
103
104 // Add a data item to the current output section.
105 void
106 add_data(int size, bool is_signed, Expression* val);
107
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);
a445fddf
ILT
112
113 // Add an assignment to the special dot symbol.
114 void
115 add_dot_assignment(Expression* value);
116
494e05f4
ILT
117 // Add an assertion.
118 void
119 add_assertion(Expression* check, const char* message, size_t messagelen);
120
121 // Add a setting for the fill value.
122 void
123 add_fill(Expression* val);
124
125 // Add an input section specification.
126 void
127 add_input_section(const Input_section_spec* spec, bool keep);
128
2d924fd9
ILT
129 // Saw DATA_SEGMENT_ALIGN.
130 void
131 data_segment_align();
132
133 // Saw DATA_SEGMENT_RELRO_END.
134 void
135 data_segment_relro_end();
136
919ed24c
ILT
137 // Create any required sections.
138 void
139 create_sections(Layout*);
140
a445fddf
ILT
141 // Add any symbols we are defining to the symbol table.
142 void
143 add_symbols_to_table(Symbol_table*);
144
145 // Finalize symbol values and check assertions.
146 void
147 finalize_symbols(Symbol_table* symtab, const Layout* layout);
148
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.
1e5d2fb1
DK
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.
b9b2ae8b 166 // *KEEP indicates whether the section should survive garbage collection.
412ffd83
CC
167 // MATCH_INPUT_SPEC indicates whether the section should be matched
168 // with input section specs or simply against the output section name
169 // (i.e., for linker-created sections like .dynamic).
a445fddf
ILT
170 const char*
171 output_section_name(const char* file_name, const char* section_name,
1e5d2fb1 172 Output_section*** output_section_slot,
b9b2ae8b 173 Section_type* pscript_section_type,
412ffd83 174 bool* keep, bool match_input_spec);
a445fddf
ILT
175
176 // Place a marker for an orphan output section into the SECTIONS
177 // clause.
178 void
179 place_orphan(Output_section* os);
180
181 // Set the addresses of all the output sections. Return the segment
182 // which holds the file header and segment headers, if any.
183 Output_segment*
184 set_section_addresses(Symbol_table*, Layout*);
185
1c4f3631
ILT
186 // Add a program header definition.
187 void
188 add_phdr(const char* name, size_t namelen, unsigned int type,
189 bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
190 Expression* load_address);
191
3802b2dd
ILT
192 // Return the number of segments we expect to create based on the
193 // SECTIONS clause.
194 size_t
195 expected_segment_count(const Layout*) const;
196
1c4f3631
ILT
197 // Add the file header and segment header to non-load segments as
198 // specified by the PHDRS clause.
199 void
200 put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
201
8f2eb564
ILT
202 // Look for an output section by name and return the address, the
203 // load address, the alignment, and the size. This is used when an
204 // expression refers to an output section which was not actually
205 // created. This returns true if the section was found, false
206 // otherwise.
207 bool
208 get_output_section_info(const char* name, uint64_t* address,
209 uint64_t* load_address, uint64_t* addralign,
210 uint64_t* size) const;
211
20e6d0d6
DK
212 // Release all Output_segments. This is used in relaxation.
213 void
214 release_segments();
215
3c12dcdb
DK
216 // Whether we ever saw a SEGMENT_START expression, the presence of which
217 // changes the behaviour of -Ttext, -Tdata and -Tbss options.
218 bool
219 saw_segment_start_expression() const
220 { return this->saw_segment_start_expression_; }
221
222 // Set the flag which indicates whether we saw a SEGMENT_START expression.
223 void
224 set_saw_segment_start_expression(bool value)
225 { this->saw_segment_start_expression_ = value; }
226
7f8cd844
NC
227 // Add a memory region.
228 void
229 add_memory_region(const char*, size_t, unsigned int,
230 Expression*, Expression*);
231
232 // Find a memory region's origin.
233 Expression*
234 find_memory_region_origin(const char*, size_t);
235
236 // Find a memory region's length.
237 Expression*
238 find_memory_region_length(const char*, size_t);
239
ea5cae92 240 // Find a memory region by name.
7f8cd844
NC
241 Memory_region*
242 find_memory_region(const char*, size_t);
243
ea5cae92
NC
244 // Find a memory region that should be used by a given output section.
245 Memory_region*
7c61d651 246 find_memory_region(Output_section_definition*, bool, bool,
ea5cae92
NC
247 Output_section_definition**);
248
249 // Returns true if the provide block of memory is contained
250 // within a memory region.
251 bool
252 block_in_region(Symbol_table*, Layout*, uint64_t, uint64_t) const;
253
7f8cd844
NC
254 // Set the memory region of the section.
255 void
256 set_memory_region(Memory_region*, bool);
257
494e05f4
ILT
258 // Print the contents to the FILE. This is for debugging.
259 void
260 print(FILE*) const;
261
0d371ad3
ILT
262 // Used for orphan sections.
263 typedef Sections_elements::iterator Elements_iterator;
494e05f4 264
0d371ad3 265 private:
7f8cd844 266 typedef std::vector<Memory_region*> Memory_regions;
1c4f3631
ILT
267 typedef std::vector<Phdrs_element*> Phdrs_elements;
268
a445fddf
ILT
269 // Create segments.
270 Output_segment*
f6973bdc 271 create_segments(Layout*, uint64_t);
a445fddf
ILT
272
273 // Create PT_NOTE and PT_TLS segments.
274 void
275 create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
276
277 // Return whether the section is a BSS section.
278 static bool
279 is_bss_section(const Output_section*);
280
1c4f3631
ILT
281 // Return the total size of the headers.
282 size_t
283 total_header_size(Layout* layout) const;
284
5c3024d2 285 // Return the amount we have to subtract from the LMA to accommodate
1c4f3631
ILT
286 // headers of the given size.
287 uint64_t
288 header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
289
290 // Create the segments from a PHDRS clause.
291 Output_segment*
f6973bdc 292 create_segments_from_phdrs_clause(Layout* layout, uint64_t);
1c4f3631
ILT
293
294 // Attach sections to segments from a PHDRS clause.
295 void
296 attach_sections_using_phdrs_clause(Layout*);
297
298 // Set addresses of segments from a PHDRS clause.
299 Output_segment*
f6973bdc 300 set_phdrs_clause_addresses(Layout*, uint64_t);
1c4f3631 301
494e05f4
ILT
302 // True if we ever saw a SECTIONS clause.
303 bool saw_sections_clause_;
304 // True if we are currently processing a SECTIONS clause.
305 bool in_sections_clause_;
306 // The list of elements in the SECTIONS clause.
307 Sections_elements* sections_elements_;
308 // The current output section, if there is one.
309 Output_section_definition* output_section_;
7f8cd844
NC
310 // The list of memory regions in the MEMORY clause.
311 Memory_regions* memory_regions_;
1c4f3631
ILT
312 // The list of program headers in the PHDRS clause.
313 Phdrs_elements* phdrs_elements_;
0d371ad3
ILT
314 // Where to put orphan sections.
315 Orphan_section_placement* orphan_section_placement_;
316 // A pointer to the last Sections_element when we see
2d924fd9 317 // DATA_SEGMENT_ALIGN.
0d371ad3
ILT
318 Sections_elements::iterator data_segment_align_start_;
319 // Whether we have seen DATA_SEGMENT_ALIGN.
320 bool saw_data_segment_align_;
2d924fd9
ILT
321 // Whether we have seen DATA_SEGMENT_RELRO_END.
322 bool saw_relro_end_;
3c12dcdb
DK
323 // Whether we have seen SEGMENT_START.
324 bool saw_segment_start_expression_;
8086551f
CC
325 // Whether we have created all necessary segments.
326 bool segments_created_;
494e05f4
ILT
327};
328
7f8cd844
NC
329// Attributes for memory regions.
330enum
331{
332 MEM_EXECUTABLE = (1 << 0),
333 MEM_WRITEABLE = (1 << 1),
334 MEM_READABLE = (1 << 2),
335 MEM_ALLOCATABLE = (1 << 3),
336 MEM_INITIALIZED = (1 << 4),
337 MEM_ATTR_MASK = (1 << 5) - 1
338};
339
494e05f4
ILT
340} // End namespace gold.
341
342#endif // !defined(GOLD_SCRIPT_SECTIONS_H