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
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
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
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));
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
>
74 typedef Elf_file
<size
, big_endian
, File
> This
;
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.
97 // Return the file offset to the section headers.
100 { return this->shoff_
; }
102 // Return the number of sections.
106 this->initialize_shnum();
110 // Return the section index of the section name string table.
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
),
126 // Return the name of section SHNDX.
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 type of section SHNDX.
144 section_type(unsigned int shndx
);
146 // Return the link field of section SHNDX.
148 section_link(unsigned int shndx
);
150 // Return the info field of section SHNDX.
152 section_info(unsigned int shndx
);
154 // Return the addralign field of section SHNDX.
155 typename Elf_types
<size
>::Elf_WXword
156 section_addralign(unsigned int shndx
);
159 // Shared constructor code.
161 construct(File
* file
, const Ef_ehdr
& ehdr
);
163 // Initialize shnum_ and shstrndx_.
167 // Return the file offset of the header of section SHNDX.
169 section_header_offset(unsigned int shndx
);
171 // The file we are reading.
173 // The file offset to the section headers.
175 // The number of sections.
177 // The section index of the section name string table.
178 unsigned int shstrndx_
;
181 // Template function definitions.
183 // Construct an Elf_file given an ELF file header.
185 template<int size
, bool big_endian
, typename File
>
187 Elf_file
<size
, big_endian
, File
>::construct(File
* file
, const Ef_ehdr
& ehdr
)
190 this->shoff_
= ehdr
.get_e_shoff();
191 this->shnum_
= ehdr
.get_e_shnum();
192 this->shstrndx_
= ehdr
.get_e_shstrndx();
193 if (ehdr
.get_e_ehsize() != This::ehdr_size
)
194 file
->error(_("bad e_ehsize (%d != %d)"),
195 ehdr
.get_e_ehsize(), This::ehdr_size
);
196 if (ehdr
.get_e_shentsize() != This::shdr_size
)
197 file
->error(_("bad e_shentsize (%d != %d)"),
198 ehdr
.get_e_shentsize(), This::shdr_size
);
201 // Construct an ELF file.
203 template<int size
, bool big_endian
, typename File
>
205 Elf_file
<size
, big_endian
, File
>::Elf_file(File
* file
)
207 typename
File::View
v(file
->view(file_header_offset
, This::ehdr_size
));
208 this->construct(file
, Ef_ehdr(v
.data()));
211 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
213 template<int size
, bool big_endian
, typename File
>
215 Elf_file
<size
, big_endian
, File
>::initialize_shnum()
217 if ((this->shnum_
== 0 || this->shstrndx_
== SHN_XINDEX
)
218 && this->shoff_
!= 0)
220 typename
File::View
v(this->file_
->view(this->shoff_
, This::shdr_size
));
221 Ef_shdr
shdr(v
.data());
222 if (this->shnum_
== 0)
223 this->shnum_
= shdr
.get_sh_size();
224 if (this->shstrndx_
== SHN_XINDEX
)
225 this->shstrndx_
= shdr
.get_sh_link();
229 // Return the file offset of the section header of section SHNDX.
231 template<int size
, bool big_endian
, typename File
>
233 Elf_file
<size
, big_endian
, File
>::section_header_offset(unsigned int shndx
)
235 if (shndx
>= this->shnum())
236 this->file_
->error(_("section_header_offset: bad shndx %u >= %u"),
237 shndx
, this->shnum());
238 return this->shoff_
+ This::shdr_size
* shndx
;
241 // Return the name of section SHNDX.
243 template<int size
, bool big_endian
, typename File
>
245 Elf_file
<size
, big_endian
, File
>::section_name(unsigned int shndx
)
247 File
* const file
= this->file_
;
249 // Get the section name offset.
250 unsigned int sh_name
;
252 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
254 Ef_shdr
shdr(v
.data());
255 sh_name
= shdr
.get_sh_name();
258 // Get the file offset for the section name string table data.
262 const unsigned int shstrndx
= this->shstrndx_
;
263 typename
File::View
v(file
->view(this->section_header_offset(shstrndx
),
265 Ef_shdr
shstr_shdr(v
.data());
266 shstr_off
= shstr_shdr
.get_sh_offset();
267 shstr_size
= shstr_shdr
.get_sh_size();
270 if (sh_name
>= shstr_size
)
271 file
->error(_("bad section name offset for section %u: %u"),
274 typename
File::View
v(file
->view(shstr_off
, shstr_size
));
276 const unsigned char* datau
= v
.data();
277 const char* data
= reinterpret_cast<const char*>(datau
);
278 const void* p
= ::memchr(data
+ sh_name
, '\0', shstr_size
- sh_name
);
280 file
->error(_("missing null terminator for name of section %u"),
283 size_t len
= static_cast<const char*>(p
) - (data
+ sh_name
);
285 return std::string(data
+ sh_name
, len
);
288 // Return the contents of section SHNDX.
290 template<int size
, bool big_endian
, typename File
>
291 typename
File::Location
292 Elf_file
<size
, big_endian
, File
>::section_contents(unsigned int shndx
)
294 File
* const file
= this->file_
;
296 if (shndx
>= this->shnum())
297 file
->error(_("section_contents: bad shndx %u >= %u"),
298 shndx
, this->shnum());
300 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
302 Ef_shdr
shdr(v
.data());
303 return typename
File::Location(shdr
.get_sh_offset(), shdr
.get_sh_size());
306 // Get the size of section SHNDX.
308 template<int size
, bool big_endian
, typename File
>
309 typename Elf_types
<size
>::Elf_WXword
310 Elf_file
<size
, big_endian
, File
>::section_size(unsigned int shndx
)
312 File
* const file
= this->file_
;
314 if (shndx
>= this->shnum())
315 file
->error(_("section_size: bad shndx %u >= %u"),
316 shndx
, this->shnum());
318 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
321 Ef_shdr
shdr(v
.data());
322 return shdr
.get_sh_size();
325 // Return the section flags of section SHNDX.
327 template<int size
, bool big_endian
, typename File
>
328 typename Elf_types
<size
>::Elf_WXword
329 Elf_file
<size
, big_endian
, File
>::section_flags(unsigned int shndx
)
331 File
* const file
= this->file_
;
333 if (shndx
>= this->shnum())
334 file
->error(_("section_flags: bad shndx %u >= %u"),
335 shndx
, this->shnum());
337 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
340 Ef_shdr
shdr(v
.data());
341 return shdr
.get_sh_flags();
344 // Return the type of section SHNDX.
346 template<int size
, bool big_endian
, typename File
>
348 Elf_file
<size
, big_endian
, File
>::section_type(unsigned int shndx
)
350 File
* const file
= this->file_
;
352 if (shndx
>= this->shnum())
353 file
->error(_("section_type: bad shndx %u >= %u"),
354 shndx
, this->shnum());
356 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
359 Ef_shdr
shdr(v
.data());
360 return shdr
.get_sh_type();
363 // Return the sh_link field of section SHNDX.
365 template<int size
, bool big_endian
, typename File
>
367 Elf_file
<size
, big_endian
, File
>::section_link(unsigned int shndx
)
369 File
* const file
= this->file_
;
371 if (shndx
>= this->shnum())
372 file
->error(_("section_link: bad shndx %u >= %u"),
373 shndx
, this->shnum());
375 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
378 Ef_shdr
shdr(v
.data());
379 return shdr
.get_sh_link();
382 // Return the sh_info field of section SHNDX.
384 template<int size
, bool big_endian
, typename File
>
386 Elf_file
<size
, big_endian
, File
>::section_info(unsigned int shndx
)
388 File
* const file
= this->file_
;
390 if (shndx
>= this->shnum())
391 file
->error(_("section_info: bad shndx %u >= %u"),
392 shndx
, this->shnum());
394 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
397 Ef_shdr
shdr(v
.data());
398 return shdr
.get_sh_info();
401 // Return the sh_addralign field of section SHNDX.
403 template<int size
, bool big_endian
, typename File
>
404 typename Elf_types
<size
>::Elf_WXword
405 Elf_file
<size
, big_endian
, File
>::section_addralign(unsigned int shndx
)
407 File
* const file
= this->file_
;
409 if (shndx
>= this->shnum())
410 file
->error(_("section_addralign: bad shndx %u >= %u"),
411 shndx
, this->shnum());
413 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
416 Ef_shdr
shdr(v
.data());
417 return shdr
.get_sh_addralign();
420 } // End namespace elfcpp.
422 #endif // !defined(ELFCPP_FILE_H)
This page took 0.038697 seconds and 5 git commands to generate.