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