* elflink.c (compute_bucket_count): Warning fixes.
[binutils.git] / elfcpp / elfcpp_file.h
blob431f8efabeef8aa7365a1f626c09b0e905dbeac8
1 // elfcpp_file.h -- file access for elfcpp -*- C++ -*-
3 // Copyright 2006, 2007, Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of elfcpp.
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.
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.)
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.
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.
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.
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));
56 #ifndef ELFPCP_FILE_H
57 #define ELFCPP_FILE_H
59 #include <string>
60 #include <cstring>
62 namespace elfcpp
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.
70 template<int size, bool big_endian, typename File>
71 class Elf_file
73 private:
74 typedef Elf_file<size, big_endian, File> This;
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;
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;
89 // Construct an Elf_file given an ELF file header.
90 Elf_file(File* file, const Ef_ehdr& ehdr)
91 { this->construct(file, ehdr); }
93 // Construct an ELF file.
94 inline
95 Elf_file(File* file);
97 // Return the file offset to the section headers.
98 off_t
99 shoff() const
100 { return this->shoff_; }
102 // Return the number of sections.
103 unsigned int
104 shnum()
106 this->initialize_shnum();
107 return this->shnum_;
110 // Return the section index of the section name string table.
111 unsigned int
112 shstrndx()
114 this->initialize_shnum();
115 return this->shstrndx_;
118 // Return the location of the header of section SHNDX.
119 typename File::Location
120 section_header(unsigned int shndx)
122 return typename File::Location(this->section_header_offset(shndx),
123 shdr_size);
126 // Return the name of section SHNDX.
127 std::string
128 section_name(unsigned int shndx);
130 // Return the location of the contents of section SHNDX.
131 typename File::Location
132 section_contents(unsigned int shndx);
134 // Return the size of section SHNDX.
135 typename Elf_types<size>::Elf_WXword
136 section_size(unsigned int shndx);
138 // Return the flags of section SHNDX.
139 typename Elf_types<size>::Elf_WXword
140 section_flags(unsigned int shndx);
142 // Return the address of section SHNDX.
143 typename Elf_types<size>::Elf_Addr
144 section_addr(unsigned int shndx);
146 // Return the type of section SHNDX.
147 Elf_Word
148 section_type(unsigned int shndx);
150 // Return the link field of section SHNDX.
151 Elf_Word
152 section_link(unsigned int shndx);
154 // Return the info field of section SHNDX.
155 Elf_Word
156 section_info(unsigned int shndx);
158 // Return the addralign field of section SHNDX.
159 typename Elf_types<size>::Elf_WXword
160 section_addralign(unsigned int shndx);
162 private:
163 // Shared constructor code.
164 void
165 construct(File* file, const Ef_ehdr& ehdr);
167 // Initialize shnum_ and shstrndx_.
168 void
169 initialize_shnum();
171 // Return the file offset of the header of section SHNDX.
172 off_t
173 section_header_offset(unsigned int shndx);
175 // The file we are reading.
176 File* file_;
177 // The file offset to the section headers.
178 off_t shoff_;
179 // The number of sections.
180 unsigned int shnum_;
181 // The section index of the section name string table.
182 unsigned int shstrndx_;
185 // Template function definitions.
187 // Construct an Elf_file given an ELF file header.
189 template<int size, bool big_endian, typename File>
190 void
191 Elf_file<size, big_endian, File>::construct(File* file, const Ef_ehdr& ehdr)
193 this->file_ = file;
194 this->shoff_ = ehdr.get_e_shoff();
195 this->shnum_ = ehdr.get_e_shnum();
196 this->shstrndx_ = ehdr.get_e_shstrndx();
197 if (ehdr.get_e_ehsize() != This::ehdr_size)
198 file->error(_("bad e_ehsize (%d != %d)"),
199 ehdr.get_e_ehsize(), This::ehdr_size);
200 if (ehdr.get_e_shentsize() != This::shdr_size)
201 file->error(_("bad e_shentsize (%d != %d)"),
202 ehdr.get_e_shentsize(), This::shdr_size);
205 // Construct an ELF file.
207 template<int size, bool big_endian, typename File>
208 inline
209 Elf_file<size, big_endian, File>::Elf_file(File* file)
211 typename File::View v(file->view(file_header_offset, This::ehdr_size));
212 this->construct(file, Ef_ehdr(v.data()));
215 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
217 template<int size, bool big_endian, typename File>
218 void
219 Elf_file<size, big_endian, File>::initialize_shnum()
221 if ((this->shnum_ == 0 || this->shstrndx_ == SHN_XINDEX)
222 && this->shoff_ != 0)
224 typename File::View v(this->file_->view(this->shoff_, This::shdr_size));
225 Ef_shdr shdr(v.data());
226 if (this->shnum_ == 0)
227 this->shnum_ = shdr.get_sh_size();
228 if (this->shstrndx_ == SHN_XINDEX)
229 this->shstrndx_ = shdr.get_sh_link();
233 // Return the file offset of the section header of section SHNDX.
235 template<int size, bool big_endian, typename File>
236 off_t
237 Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
239 if (shndx >= this->shnum())
240 this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
241 shndx, this->shnum());
242 return this->shoff_ + This::shdr_size * shndx;
245 // Return the name of section SHNDX.
247 template<int size, bool big_endian, typename File>
248 std::string
249 Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
251 File* const file = this->file_;
253 // Get the section name offset.
254 unsigned int sh_name;
256 typename File::View v(file->view(this->section_header_offset(shndx),
257 This::shdr_size));
258 Ef_shdr shdr(v.data());
259 sh_name = shdr.get_sh_name();
262 // Get the file offset for the section name string table data.
263 off_t shstr_off;
264 off_t shstr_size;
266 const unsigned int shstrndx = this->shstrndx_;
267 typename File::View v(file->view(this->section_header_offset(shstrndx),
268 This::shdr_size));
269 Ef_shdr shstr_shdr(v.data());
270 shstr_off = shstr_shdr.get_sh_offset();
271 shstr_size = shstr_shdr.get_sh_size();
274 if (sh_name >= shstr_size)
275 file->error(_("bad section name offset for section %u: %u"),
276 shndx, sh_name);
278 typename File::View v(file->view(shstr_off, shstr_size));
280 const unsigned char* datau = v.data();
281 const char* data = reinterpret_cast<const char*>(datau);
282 const void* p = ::memchr(data + sh_name, '\0', shstr_size - sh_name);
283 if (p == NULL)
284 file->error(_("missing null terminator for name of section %u"),
285 shndx);
287 size_t len = static_cast<const char*>(p) - (data + sh_name);
289 return std::string(data + sh_name, len);
292 // Return the contents of section SHNDX.
294 template<int size, bool big_endian, typename File>
295 typename File::Location
296 Elf_file<size, big_endian, File>::section_contents(unsigned int shndx)
298 File* const file = this->file_;
300 if (shndx >= this->shnum())
301 file->error(_("section_contents: bad shndx %u >= %u"),
302 shndx, this->shnum());
304 typename File::View v(file->view(this->section_header_offset(shndx),
305 This::shdr_size));
306 Ef_shdr shdr(v.data());
307 return typename File::Location(shdr.get_sh_offset(), shdr.get_sh_size());
310 // Get the size of section SHNDX.
312 template<int size, bool big_endian, typename File>
313 typename Elf_types<size>::Elf_WXword
314 Elf_file<size, big_endian, File>::section_size(unsigned int shndx)
316 File* const file = this->file_;
318 if (shndx >= this->shnum())
319 file->error(_("section_size: bad shndx %u >= %u"),
320 shndx, this->shnum());
322 typename File::View v(file->view(this->section_header_offset(shndx),
323 This::shdr_size));
325 Ef_shdr shdr(v.data());
326 return shdr.get_sh_size();
329 // Return the section flags of section SHNDX.
331 template<int size, bool big_endian, typename File>
332 typename Elf_types<size>::Elf_WXword
333 Elf_file<size, big_endian, File>::section_flags(unsigned int shndx)
335 File* const file = this->file_;
337 if (shndx >= this->shnum())
338 file->error(_("section_flags: bad shndx %u >= %u"),
339 shndx, this->shnum());
341 typename File::View v(file->view(this->section_header_offset(shndx),
342 This::shdr_size));
344 Ef_shdr shdr(v.data());
345 return shdr.get_sh_flags();
348 // Return the address of section SHNDX.
350 template<int size, bool big_endian, typename File>
351 typename Elf_types<size>::Elf_Addr
352 Elf_file<size, big_endian, File>::section_addr(unsigned int shndx)
354 File* const file = this->file_;
356 if (shndx >= this->shnum())
357 file->error(_("section_flags: bad shndx %u >= %u"),
358 shndx, this->shnum());
360 typename File::View v(file->view(this->section_header_offset(shndx),
361 This::shdr_size));
363 Ef_shdr shdr(v.data());
364 return shdr.get_sh_addr();
367 // Return the type of section SHNDX.
369 template<int size, bool big_endian, typename File>
370 Elf_Word
371 Elf_file<size, big_endian, File>::section_type(unsigned int shndx)
373 File* const file = this->file_;
375 if (shndx >= this->shnum())
376 file->error(_("section_type: bad shndx %u >= %u"),
377 shndx, this->shnum());
379 typename File::View v(file->view(this->section_header_offset(shndx),
380 This::shdr_size));
382 Ef_shdr shdr(v.data());
383 return shdr.get_sh_type();
386 // Return the sh_link field of section SHNDX.
388 template<int size, bool big_endian, typename File>
389 Elf_Word
390 Elf_file<size, big_endian, File>::section_link(unsigned int shndx)
392 File* const file = this->file_;
394 if (shndx >= this->shnum())
395 file->error(_("section_link: bad shndx %u >= %u"),
396 shndx, this->shnum());
398 typename File::View v(file->view(this->section_header_offset(shndx),
399 This::shdr_size));
401 Ef_shdr shdr(v.data());
402 return shdr.get_sh_link();
405 // Return the sh_info field of section SHNDX.
407 template<int size, bool big_endian, typename File>
408 Elf_Word
409 Elf_file<size, big_endian, File>::section_info(unsigned int shndx)
411 File* const file = this->file_;
413 if (shndx >= this->shnum())
414 file->error(_("section_info: bad shndx %u >= %u"),
415 shndx, this->shnum());
417 typename File::View v(file->view(this->section_header_offset(shndx),
418 This::shdr_size));
420 Ef_shdr shdr(v.data());
421 return shdr.get_sh_info();
424 // Return the sh_addralign field of section SHNDX.
426 template<int size, bool big_endian, typename File>
427 typename Elf_types<size>::Elf_WXword
428 Elf_file<size, big_endian, File>::section_addralign(unsigned int shndx)
430 File* const file = this->file_;
432 if (shndx >= this->shnum())
433 file->error(_("section_addralign: bad shndx %u >= %u"),
434 shndx, this->shnum());
436 typename File::View v(file->view(this->section_header_offset(shndx),
437 This::shdr_size));
439 Ef_shdr shdr(v.data());
440 return shdr.get_sh_addralign();
443 } // End namespace elfcpp.
445 #endif // !defined(ELFCPP_FILE_H)