Add __ImageBase symbol to pe.em and pep.em.
[binutils.git] / gold / merge.h
blobcc554f26f4dc3cc964a22dbfed692f826cab93b1
1 // merge.h -- handle section merging for gold -*- C++ -*-
3 // Copyright 2006, 2007 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 #ifndef GOLD_MERGE_H
24 #define GOLD_MERGE_H
26 #include <climits>
28 #include "stringpool.h"
29 #include "output.h"
31 namespace gold
34 // A general class for SHF_MERGE data, to hold functions shared by
35 // fixed-size constant data and string data.
37 class Output_merge_base : public Output_section_data
39 public:
40 Output_merge_base(uint64_t entsize, uint64_t addralign)
41 : Output_section_data(addralign), merge_map_(), entsize_(entsize)
42 { }
44 // Return the output address for an input address.
45 bool
46 do_output_address(const Relobj* object, unsigned int shndx, off_t offset,
47 uint64_t output_section_address, uint64_t* poutput) const;
49 protected:
50 // Return the entry size.
51 uint64_t
52 entsize() const
53 { return this->entsize_; }
55 // Add a mapping from an OFFSET in input section SHNDX in object
56 // OBJECT to an OUTPUT_OFFSET in the output section.
57 void
58 add_mapping(Relobj* object, unsigned int shndx, off_t offset,
59 off_t output_offset);
61 private:
62 // We build a mapping from OBJECT/SHNDX/OFFSET to an offset in the
63 // output section.
64 struct Merge_key
66 const Relobj* object;
67 unsigned int shndx;
68 off_t offset;
71 struct Merge_key_less
73 bool
74 operator()(const Merge_key&, const Merge_key&) const;
77 typedef std::map<Merge_key, off_t, Merge_key_less> Merge_map;
79 // A mapping from input object/section/offset to offset in output
80 // section.
81 Merge_map merge_map_;
83 // The entry size. For fixed-size constants, this is the size of
84 // the constants. For strings, this is the size of a character.
85 uint64_t entsize_;
88 // Handle SHF_MERGE sections with fixed-size constant data.
90 class Output_merge_data : public Output_merge_base
92 public:
93 Output_merge_data(uint64_t entsize, uint64_t addralign)
94 : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
95 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
96 { }
98 // Add an input section.
99 bool
100 do_add_input_section(Relobj* object, unsigned int shndx);
102 // Set the final data size.
103 void
104 do_set_address(uint64_t, off_t);
106 // Write the data to the file.
107 void
108 do_write(Output_file*);
110 private:
111 // We build a hash table of the fixed-size constants. Each constant
112 // is stored as a pointer into the section data we are accumulating.
114 // A key in the hash table. This is an offset in the section
115 // contents we are building.
116 typedef off_t Merge_data_key;
118 // Compute the hash code. To do this we need a pointer back to the
119 // object holding the data.
120 class Merge_data_hash
122 public:
123 Merge_data_hash(const Output_merge_data* pomd)
124 : pomd_(pomd)
127 size_t
128 operator()(Merge_data_key) const;
130 private:
131 const Output_merge_data* pomd_;
134 friend class Merge_data_hash;
136 // Compare two entries in the hash table for equality. To do this
137 // we need a pointer back to the object holding the data. Note that
138 // we now have a pointer to the object stored in two places in the
139 // hash table. Fixing this would require specializing the hash
140 // table, which would be hard to do portably.
141 class Merge_data_eq
143 public:
144 Merge_data_eq(const Output_merge_data* pomd)
145 : pomd_(pomd)
148 bool
149 operator()(Merge_data_key k1, Merge_data_key k2) const;
151 private:
152 const Output_merge_data* pomd_;
155 friend class Merge_data_eq;
157 // The type of the hash table.
158 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
159 Merge_data_hashtable;
161 // Given a hash table key, which is just an offset into the section
162 // data, return a pointer to the corresponding constant.
163 const unsigned char*
164 constant(Merge_data_key k) const
166 gold_assert(k >= 0 && k < this->len_);
167 return this->p_ + k;
170 // Add a constant to the output.
171 void
172 add_constant(const unsigned char*);
174 // The accumulated data.
175 unsigned char* p_;
176 // The length of the accumulated data.
177 off_t len_;
178 // The size of the allocated buffer.
179 size_t alc_;
180 // The hash table.
181 Merge_data_hashtable hashtable_;
184 // Handle SHF_MERGE sections with string data. This is a template
185 // based on the type of the characters in the string.
187 template<typename Char_type>
188 class Output_merge_string : public Output_merge_base
190 public:
191 Output_merge_string(uint64_t addralign)
192 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
193 merged_strings_()
195 gold_assert(addralign <= sizeof(Char_type));
196 this->stringpool_.set_no_zero_null();
199 // Add an input section.
200 bool
201 do_add_input_section(Relobj* object, unsigned int shndx);
203 // Set the final data size.
204 void
205 do_set_address(uint64_t, off_t);
207 // Write the data to the file.
208 void
209 do_write(Output_file*);
211 private:
212 // As we see input sections, we build a mapping from object, section
213 // index and offset to strings.
214 struct Merged_string
216 // The input object where the string was found.
217 Relobj* object;
218 // The input section in the input object.
219 unsigned int shndx;
220 // The offset in the input section.
221 off_t offset;
222 // The string itself, a pointer into a Stringpool.
223 const Char_type* string;
225 Merged_string(Relobj *objecta, unsigned int shndxa, off_t offseta,
226 const Char_type* stringa)
227 : object(objecta), shndx(shndxa), offset(offseta), string(stringa)
231 typedef std::vector<Merged_string> Merged_strings;
233 // As we see the strings, we add them to a Stringpool.
234 Stringpool_template<Char_type> stringpool_;
235 // Map from a location in an input object to an entry in the
236 // Stringpool.
237 Merged_strings merged_strings_;
240 } // End namespace gold.
242 #endif // !defined(GOLD_MERGE_H)