Implement SIZEOF_HEADERS, section constraints, other minor linker
[external/binutils.git] / gold / script-sections.h
1 // script-sections.h -- linker script SECTIONS for gold   -*- C++ -*-
2
3 // Copyright 2008 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 <vector>
30
31 namespace gold
32 {
33
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 Output_section_definition;
40 class Output_section;
41 class Output_segment;
42
43 class Script_sections
44 {
45  public:
46   Script_sections();
47
48   // Start a SECTIONS clause.
49   void
50   start_sections();
51
52   // Finish a SECTIONS clause.
53   void
54   finish_sections();
55
56   // Return whether we ever saw a SECTIONS clause.  If we did, then
57   // all section layout needs to go through this class.
58   bool
59   saw_sections_clause() const
60   { return this->saw_sections_clause_; }
61
62   // Return whether we are currently processing a SECTIONS clause.
63   bool
64   in_sections_clause() const
65   { return this->in_sections_clause_; }
66
67   // Start processing entries for an output section.
68   void
69   start_output_section(const char* name, size_t namelen,
70                        const Parser_output_section_header*);
71
72   // Finish processing entries for an output section.
73   void
74   finish_output_section(const Parser_output_section_trailer*);
75
76   // Add a data item to the current output section.
77   void
78   add_data(int size, bool is_signed, Expression* val);
79
80   // Add a symbol to be defined.
81   void
82   add_symbol_assignment(const char* name, size_t length, Expression* value,
83                         bool provide, bool hidden);
84
85   // Add an assignment to the special dot symbol.
86   void
87   add_dot_assignment(Expression* value);
88
89   // Add an assertion.
90   void
91   add_assertion(Expression* check, const char* message, size_t messagelen);
92
93   // Add a setting for the fill value.
94   void
95   add_fill(Expression* val);
96
97   // Add an input section specification.
98   void
99   add_input_section(const Input_section_spec* spec, bool keep);
100
101   // Add any symbols we are defining to the symbol table.
102   void
103   add_symbols_to_table(Symbol_table*);
104
105   // Finalize symbol values and check assertions.
106   void
107   finalize_symbols(Symbol_table* symtab, const Layout* layout);
108
109   // Find the name of the output section to use for an input file name
110   // and section name.  This returns a name, and sets
111   // *OUTPUT_SECTION_SLOT to point to the address where the actual
112   // output section may be stored.
113   // 1) If the input section should be discarded, this returns NULL
114   //    and sets *OUTPUT_SECTION_SLOT to NULL.
115   // 2) If the input section is mapped by the SECTIONS clause, this
116   //    returns the name to use for the output section (in permanent
117   //    storage), and sets *OUTPUT_SECTION_SLOT to point to where the
118   //    output section should be stored.  **OUTPUT_SECTION_SLOT will be
119   //    non-NULL if we have seen this output section already.
120   // 3) If the input section is not mapped by the SECTIONS clause,
121   //    this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
122   //    NULL.
123   const char*
124   output_section_name(const char* file_name, const char* section_name,
125                       Output_section*** output_section_slot);
126
127   // Place a marker for an orphan output section into the SECTIONS
128   // clause.
129   void
130   place_orphan(Output_section* os);
131
132   // Set the addresses of all the output sections.  Return the segment
133   // which holds the file header and segment headers, if any.
134   Output_segment*
135   set_section_addresses(Symbol_table*, Layout*);
136
137   // Return the number of segments we expect to create based on the
138   // SECTIONS clause.
139   size_t
140   expected_segment_count(const Layout*) const;
141
142   // Print the contents to the FILE.  This is for debugging.
143   void
144   print(FILE*) const;
145
146  private:
147   typedef std::vector<Sections_element*> Sections_elements;
148
149   // Create segments.
150   Output_segment*
151   create_segments(Layout*);
152
153   // Create PT_NOTE and PT_TLS segments.
154   void
155   create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
156
157   // Return whether the section is a BSS section.
158   static bool
159   is_bss_section(const Output_section*);
160
161   // True if we ever saw a SECTIONS clause.
162   bool saw_sections_clause_;
163   // True if we are currently processing a SECTIONS clause.
164   bool in_sections_clause_;
165   // The list of elements in the SECTIONS clause.
166   Sections_elements* sections_elements_;
167   // The current output section, if there is one.
168   Output_section_definition* output_section_;
169 };
170
171 } // End namespace gold.
172
173 #endif // !defined(GOLD_SCRIPT_SECTIONS_H