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