]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - elfcpp/elfcpp_file.h
Add licensing text to every source file.
[thirdparty/binutils-gdb.git] / elfcpp / elfcpp_file.h
1 // elfcpp_file.h -- file access for elfcpp -*- C++ -*-
2
3 // Copyright 2006, 2007, Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of elfcpp.
7
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
12
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
21
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
26
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30 // 02110-1301, USA.
31
32 // This header file defines the class Elf_file which can be used to
33 // read useful data from an ELF file. The functions here are all
34 // templates which take a file interface object as a parameter. This
35 // type must have a subtype View. This type must support two methods:
36 // View view(off_t file_offset, off_t data_size)
37 // returns a View for the specified part of the file.
38 // void error(const char* printf_format, ...)
39 // prints an error message and does not return. The subtype View must
40 // support a method
41 // const unsigned char* data()
42 // which returns a pointer to a buffer containing the requested data.
43 // This general interface is used to read data from the file. Objects
44 // of type View will never survive longer than the elfcpp function.
45
46 // Some of these functions must return a reference to part of the
47 // file. To use these, the file interface must support a subtype
48 // Location:
49 // Location(off_t file_offset, off_t data_size)
50 // To use this in conjunction with the accessors types Shdr, etc., the
51 // file interface should support an overload of view:
52 // View view(Location)
53 // This permits writing
54 // elfcpp::Shdr shdr(file, ef.section_header(n));
55
56 #ifndef ELFPCP_FILE_H
57 #define ELFCPP_FILE_H
58
59 #include <string>
60 #include <cstring>
61
62 namespace elfcpp
63 {
64
65 // This object is used to read an ELF file.
66 // SIZE: The size of file, 32 or 64.
67 // BIG_ENDIAN: Whether the file is in big-endian format.
68 // FILE: A file reading type as described above.
69
70 template<int size, bool big_endian, typename File>
71 class Elf_file
72 {
73 private:
74 typedef Elf_file<size, big_endian, File> This;
75
76 public:
77 static const int ehdr_size = Elf_sizes<size>::ehdr_size;
78 static const int phdr_size = Elf_sizes<size>::phdr_size;
79 static const int shdr_size = Elf_sizes<size>::shdr_size;
80 static const int sym_size = Elf_sizes<size>::sym_size;
81 static const int rel_size = Elf_sizes<size>::rel_size;
82 static const int rela_size = Elf_sizes<size>::rela_size;
83
84 typedef Ehdr<size, big_endian> Ef_ehdr;
85 typedef Phdr<size, big_endian> Ef_phdr;
86 typedef Shdr<size, big_endian> Ef_shdr;
87 typedef Sym<size, big_endian> Ef_sym;
88
89 // Construct an Elf_file given an ELF file header.
90 Elf_file(File* file, const Ef_ehdr& ehdr)
91 { this->construct(file, ehdr); }
92
93 // Construct an ELF file.
94 inline
95 Elf_file(File* file);
96
97 // Return the file offset to the section headers.
98 off_t
99 shoff() const
100 { return this->shoff_; }
101
102 // Return the number of sections.
103 unsigned int
104 shnum()
105 {
106 this->initialize_shnum();
107 return this->shnum_;
108 }
109
110 // Return the section index of the section name string table.
111 unsigned int
112 shstrndx()
113 {
114 this->initialize_shnum();
115 return this->shstrndx_;
116 }
117
118 // Return the location of the header of section SHNDX.
119 typename File::Location
120 section_header(unsigned int shndx)
121 {
122 return typename File::Location(this->section_header_offset(shndx),
123 shdr_size);
124 }
125
126 // Return the name of section SHNDX.
127 std::string
128 section_name(unsigned int shndx);
129
130 // Return the location of the contents of section SHNDX.
131 typename File::Location
132 section_contents(unsigned int shndx);
133
134 // Return the flags of section SHNDX.
135 typename Elf_types<size>::Elf_WXword
136 section_flags(unsigned int shndx);
137
138 // Return the type of section SHNDX.
139 Elf_Word
140 section_type(unsigned int shndx);
141
142 private:
143 // Shared constructor code.
144 void
145 construct(File* file, const Ef_ehdr& ehdr);
146
147 // Initialize shnum_ and shstrndx_.
148 void
149 initialize_shnum();
150
151 // Return the file offset of the header of section SHNDX.
152 off_t
153 section_header_offset(unsigned int shndx);
154
155 // The file we are reading.
156 File* file_;
157 // The file offset to the section headers.
158 off_t shoff_;
159 // The number of sections.
160 unsigned int shnum_;
161 // The section index of the section name string table.
162 unsigned int shstrndx_;
163 };
164
165 // Template function definitions.
166
167 // Construct an Elf_file given an ELF file header.
168
169 template<int size, bool big_endian, typename File>
170 void
171 Elf_file<size, big_endian, File>::construct(File* file, const Ef_ehdr& ehdr)
172 {
173 this->file_ = file;
174 this->shoff_ = ehdr.get_e_shoff();
175 this->shnum_ = ehdr.get_e_shnum();
176 this->shstrndx_ = ehdr.get_e_shstrndx();
177 if (ehdr.get_e_ehsize() != This::ehdr_size)
178 file->error(_("bad e_ehsize (%d != %d)"),
179 ehdr.get_e_ehsize(), This::ehdr_size);
180 if (ehdr.get_e_shentsize() != This::shdr_size)
181 file->error(_("bad e_shentsize (%d != %d)"),
182 ehdr.get_e_shentsize(), This::shdr_size);
183 }
184
185 // Construct an ELF file.
186
187 template<int size, bool big_endian, typename File>
188 inline
189 Elf_file<size, big_endian, File>::Elf_file(File* file)
190 {
191 typename File::View v(file->view(file_header_offset, This::ehdr_size));
192 this->construct(file, Ef_ehdr(v.data()));
193 }
194
195 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
196
197 template<int size, bool big_endian, typename File>
198 void
199 Elf_file<size, big_endian, File>::initialize_shnum()
200 {
201 if ((this->shnum_ == 0 || this->shstrndx_ == SHN_XINDEX)
202 && this->shoff_ != 0)
203 {
204 typename File::View v(this->file_->view(this->shoff_, This::shdr_size));
205 Ef_shdr shdr(v.data());
206 if (this->shnum_ == 0)
207 this->shnum_ = shdr.get_sh_size();
208 if (this->shstrndx_ == SHN_XINDEX)
209 this->shstrndx_ = shdr.get_sh_link();
210 }
211 }
212
213 // Return the file offset of the section header of section SHNDX.
214
215 template<int size, bool big_endian, typename File>
216 off_t
217 Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
218 {
219 if (shndx >= this->shnum())
220 this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
221 shndx, this->shnum());
222 return this->shoff_ + This::shdr_size * shndx;
223 }
224
225 // Return the name of section SHNDX.
226
227 template<int size, bool big_endian, typename File>
228 std::string
229 Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
230 {
231 File* const file = this->file_;
232
233 // Get the section name offset.
234 unsigned int sh_name;
235 {
236 typename File::View v(file->view(this->section_header_offset(shndx),
237 This::shdr_size));
238 Ef_shdr shdr(v.data());
239 sh_name = shdr.get_sh_name();
240 }
241
242 // Get the file offset for the section name string table data.
243 off_t shstr_off;
244 off_t shstr_size;
245 {
246 const unsigned int shstrndx = this->shstrndx_;
247 typename File::View v(file->view(this->section_header_offset(shstrndx),
248 This::shdr_size));
249 Ef_shdr shstr_shdr(v.data());
250 shstr_off = shstr_shdr.get_sh_offset();
251 shstr_size = shstr_shdr.get_sh_size();
252 }
253
254 if (sh_name >= shstr_size)
255 file->error(_("bad section name offset for section %u: %u"),
256 shndx, sh_name);
257
258 typename File::View v(file->view(shstr_off, shstr_size));
259
260 const unsigned char* datau = v.data();
261 const char* data = reinterpret_cast<const char*>(datau);
262 const void* p = ::memchr(data + sh_name, '\0', shstr_size - sh_name);
263 if (p == NULL)
264 file->error(_("missing null terminator for name of section %u"),
265 shndx);
266
267 size_t len = static_cast<const char*>(p) - (data + sh_name);
268
269 return std::string(data + sh_name, len);
270 }
271
272 // Return the contents of section SHNDX.
273
274 template<int size, bool big_endian, typename File>
275 typename File::Location
276 Elf_file<size, big_endian, File>::section_contents(unsigned int shndx)
277 {
278 File* const file = this->file_;
279
280 if (shndx >= this->shnum())
281 file->error(_("section_contents: bad shndx %u >= %u"),
282 shndx, this->shnum());
283
284 typename File::View v(file->view(this->section_header_offset(shndx),
285 This::shdr_size));
286 Ef_shdr shdr(v.data());
287 return typename File::Location(shdr.get_sh_offset(), shdr.get_sh_size());
288 }
289
290 // Return the section flags of section SHNDX.
291
292 template<int size, bool big_endian, typename File>
293 typename Elf_types<size>::Elf_WXword
294 Elf_file<size, big_endian, File>::section_flags(unsigned int shndx)
295 {
296 File* const file = this->file_;
297
298 if (shndx >= this->shnum())
299 file->error(_("section_flags: bad shndx %u >= %u"),
300 shndx, this->shnum());
301
302 typename File::View v(file->view(this->section_header_offset(shndx),
303 This::shdr_size));
304
305 Ef_shdr shdr(v.data());
306 return shdr.get_sh_flags();
307 }
308
309 // Return the type of section SHNDX.
310
311 template<int size, bool big_endian, typename File>
312 Elf_Word
313 Elf_file<size, big_endian, File>::section_type(unsigned int shndx)
314 {
315 File* const file = this->file_;
316
317 if (shndx >= this->shnum())
318 file->error(_("section_type: bad shndx %u >= %u"),
319 shndx, this->shnum());
320
321 typename File::View v(file->view(this->section_header_offset(shndx),
322 This::shdr_size));
323
324 Ef_shdr shdr(v.data());
325 return shdr.get_sh_type();
326 }
327
328 } // End namespace elfcpp.
329
330 #endif // !defined(ELFCPP_FILE_H)