bfd/
[binutils.git] / gold / script-sections.h
blob73bf33f42bb9d13842166338a3db1634b41019ad
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
28 #include <cstdio>
29 #include <vector>
31 namespace gold
34 struct Parser_output_section_header;
35 struct Parser_output_section_trailer;
36 struct Input_section_spec;
37 class Expression;
38 class Sections_element;
39 class Phdrs_element;
40 class Output_data;
41 class Output_section_definition;
42 class Output_section;
43 class Output_segment;
45 class Script_sections
47 public:
48 Script_sections();
50 // Start a SECTIONS clause.
51 void
52 start_sections();
54 // Finish a SECTIONS clause.
55 void
56 finish_sections();
58 // Return whether we ever saw a SECTIONS clause. If we did, then
59 // all section layout needs to go through this class.
60 bool
61 saw_sections_clause() const
62 { return this->saw_sections_clause_; }
64 // Return whether we are currently processing a SECTIONS clause.
65 bool
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.
71 bool
72 saw_phdrs_clause() const
73 { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; }
75 // Start processing entries for an output section.
76 void
77 start_output_section(const char* name, size_t namelen,
78 const Parser_output_section_header*);
80 // Finish processing entries for an output section.
81 void
82 finish_output_section(const Parser_output_section_trailer*);
84 // Add a data item to the current output section.
85 void
86 add_data(int size, bool is_signed, Expression* val);
88 // Add a symbol to be defined.
89 void
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.
94 void
95 add_dot_assignment(Expression* value);
97 // Add an assertion.
98 void
99 add_assertion(Expression* check, const char* message, size_t messagelen);
101 // Add a setting for the fill value.
102 void
103 add_fill(Expression* val);
105 // Add an input section specification.
106 void
107 add_input_section(const Input_section_spec* spec, bool keep);
109 // Create any required sections.
110 void
111 create_sections(Layout*);
113 // Add any symbols we are defining to the symbol table.
114 void
115 add_symbols_to_table(Symbol_table*);
117 // Finalize symbol values and check assertions.
118 void
119 finalize_symbols(Symbol_table* symtab, const Layout* layout);
121 // Find the name of the output section to use for an input file name
122 // and section name. This returns a name, and sets
123 // *OUTPUT_SECTION_SLOT to point to the address where the actual
124 // output section may be stored.
125 // 1) If the input section should be discarded, this returns NULL
126 // and sets *OUTPUT_SECTION_SLOT to NULL.
127 // 2) If the input section is mapped by the SECTIONS clause, this
128 // returns the name to use for the output section (in permanent
129 // storage), and sets *OUTPUT_SECTION_SLOT to point to where the
130 // output section should be stored. **OUTPUT_SECTION_SLOT will be
131 // non-NULL if we have seen this output section already.
132 // 3) If the input section is not mapped by the SECTIONS clause,
133 // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
134 // NULL.
135 const char*
136 output_section_name(const char* file_name, const char* section_name,
137 Output_section*** output_section_slot);
139 // Place a marker for an orphan output section into the SECTIONS
140 // clause.
141 void
142 place_orphan(Output_section* os);
144 // Set the addresses of all the output sections. Return the segment
145 // which holds the file header and segment headers, if any.
146 Output_segment*
147 set_section_addresses(Symbol_table*, Layout*);
149 // Add a program header definition.
150 void
151 add_phdr(const char* name, size_t namelen, unsigned int type,
152 bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
153 Expression* load_address);
155 // Return the number of segments we expect to create based on the
156 // SECTIONS clause.
157 size_t
158 expected_segment_count(const Layout*) const;
160 // Add the file header and segment header to non-load segments as
161 // specified by the PHDRS clause.
162 void
163 put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
165 // Look for an output section by name and return the address, the
166 // load address, the alignment, and the size. This is used when an
167 // expression refers to an output section which was not actually
168 // created. This returns true if the section was found, false
169 // otherwise.
170 bool
171 get_output_section_info(const char* name, uint64_t* address,
172 uint64_t* load_address, uint64_t* addralign,
173 uint64_t* size) const;
175 // Print the contents to the FILE. This is for debugging.
176 void
177 print(FILE*) const;
179 private:
180 typedef std::vector<Sections_element*> Sections_elements;
182 typedef std::vector<Phdrs_element*> Phdrs_elements;
184 // Create segments.
185 Output_segment*
186 create_segments(Layout*);
188 // Create PT_NOTE and PT_TLS segments.
189 void
190 create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
192 // Return whether the section is a BSS section.
193 static bool
194 is_bss_section(const Output_section*);
196 // Return the total size of the headers.
197 size_t
198 total_header_size(Layout* layout) const;
200 // Return the amount we have to subtract from the LMA to accomodate
201 // headers of the given size.
202 uint64_t
203 header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
205 // Create the segments from a PHDRS clause.
206 Output_segment*
207 create_segments_from_phdrs_clause(Layout* layout);
209 // Attach sections to segments from a PHDRS clause.
210 void
211 attach_sections_using_phdrs_clause(Layout*);
213 // Set addresses of segments from a PHDRS clause.
214 Output_segment*
215 set_phdrs_clause_addresses(Layout*);
217 // True if we ever saw a SECTIONS clause.
218 bool saw_sections_clause_;
219 // True if we are currently processing a SECTIONS clause.
220 bool in_sections_clause_;
221 // The list of elements in the SECTIONS clause.
222 Sections_elements* sections_elements_;
223 // The current output section, if there is one.
224 Output_section_definition* output_section_;
225 // The list of program headers in the PHDRS clause.
226 Phdrs_elements* phdrs_elements_;
229 } // End namespace gold.
231 #endif // !defined(GOLD_SCRIPT_SECTIONS_H