1 // elfcpp_file.h -- file access for elfcpp -*- C++ -*-
3 // Copyright (C) 2006-2022 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));
68 // A simple helper class to recognize if a file has an ELF header.
73 // Maximum header size. The user should try to read this much of
74 // the file when using this class.
76 static const int max_header_size
= Elf_sizes
<64>::ehdr_size
;
78 // Checks if the file contains the ELF magic. Other header fields
82 is_elf_file(const unsigned char* ehdr_buf
, int size
);
84 // Check if EHDR_BUF/BUFSIZE is a valid header of a 32-bit or
85 // 64-bit, little-endian or big-endian ELF file. Assumes
86 // is_elf_file() has been checked to be true. If the header is not
87 // valid, *ERROR contains a human-readable error message. If is is,
88 // *SIZE is set to either 32 or 64, *BIG_ENDIAN is set to indicate
89 // whether the file is big-endian.
92 is_valid_header(const unsigned char* ehdr_buf
, off_t bufsize
,
93 int* size
, bool* big_endian
,
97 // This object is used to read an ELF file.
98 // SIZE: The size of file, 32 or 64.
99 // BIG_ENDIAN: Whether the file is in big-endian format.
100 // FILE: A file reading type as described above.
102 template<int size
, bool big_endian
, typename File
>
106 typedef Elf_file
<size
, big_endian
, File
> This
;
109 static const int ehdr_size
= Elf_sizes
<size
>::ehdr_size
;
110 static const int phdr_size
= Elf_sizes
<size
>::phdr_size
;
111 static const int shdr_size
= Elf_sizes
<size
>::shdr_size
;
112 static const int sym_size
= Elf_sizes
<size
>::sym_size
;
113 static const int rel_size
= Elf_sizes
<size
>::rel_size
;
114 static const int rela_size
= Elf_sizes
<size
>::rela_size
;
116 typedef Ehdr
<size
, big_endian
> Ef_ehdr
;
117 typedef Phdr
<size
, big_endian
> Ef_phdr
;
118 typedef Shdr
<size
, big_endian
> Ef_shdr
;
119 typedef Sym
<size
, big_endian
> Ef_sym
;
121 // Construct an Elf_file given an ELF file header.
122 Elf_file(File
* file
, const Ef_ehdr
& ehdr
)
123 { this->construct(file
, ehdr
); }
125 // Construct an ELF file.
127 Elf_file(File
* file
);
129 // Return the file offset to the section headers.
132 { return this->shoff_
; }
134 // Find the first section with an sh_type field equal to TYPE and
135 // return its index. Returns SHN_UNDEF if there is no such section.
137 find_section_by_type(unsigned int type
);
139 // Return the number of sections.
143 this->initialize_shnum();
150 if (this->shnum_
== 0 && this->shoff_
!= 0)
151 this->file_
->error(_("ELF file has not been initialized yet"
152 " (internal error)"));
156 // Return the section index of the section name string table.
160 this->initialize_shnum();
161 return this->shstrndx_
;
167 if (this->shstrndx_
== SHN_XINDEX
&& this->shoff_
!= 0)
169 this->file_
->error(_("ELF file has not been initialized yet"
170 " (internal error)"));
173 return this->shstrndx_
;
176 // Return the value to subtract from section indexes >=
177 // SHN_LORESERVE. See the comment in initialize_shnum.
181 this->initialize_shnum();
182 return this->large_shndx_offset_
;
186 large_shndx_offset() const
188 if (this->shstrndx_
== SHN_XINDEX
&& this->shoff_
!= 0)
189 this->file_
->error(_("ELF file has not been initialized yet"
190 " (internal error)"));
191 return this->large_shndx_offset_
;
194 // Return the location of the header of section SHNDX.
195 typename
File::Location
196 section_header(unsigned int shndx
)
198 return typename
File::Location(this->section_header_offset(shndx
),
202 // Return the name of section SHNDX.
204 section_name(unsigned int shndx
) const;
206 // Return the location of the contents of section SHNDX.
207 typename
File::Location
208 section_contents(unsigned int shndx
);
210 // Return the size of section SHNDX.
211 typename Elf_types
<size
>::Elf_WXword
212 section_size(unsigned int shndx
);
214 // Return the flags of section SHNDX.
215 typename Elf_types
<size
>::Elf_WXword
216 section_flags(unsigned int shndx
);
218 // Return the address of section SHNDX.
219 typename Elf_types
<size
>::Elf_Addr
220 section_addr(unsigned int shndx
);
222 // Return the type of section SHNDX.
224 section_type(unsigned int shndx
);
226 // Return the link field of section SHNDX.
228 section_link(unsigned int shndx
);
230 // Return the info field of section SHNDX.
232 section_info(unsigned int shndx
);
234 // Return the addralign field of section SHNDX.
235 typename Elf_types
<size
>::Elf_WXword
236 section_addralign(unsigned int shndx
);
239 // Shared constructor code.
241 construct(File
* file
, const Ef_ehdr
& ehdr
);
243 // Initialize shnum_ and shstrndx_.
247 // Return the file offset of the header of section SHNDX.
249 section_header_offset(unsigned int shndx
) const;
251 // The file we are reading.
253 // The file offset to the section headers.
255 // The number of sections.
257 // The section index of the section name string table.
258 unsigned int shstrndx_
;
259 // Offset to add to sections larger than SHN_LORESERVE.
260 int large_shndx_offset_
;
263 // A small wrapper around SHT_STRTAB data mapped to memory. It checks that the
264 // index is not out of bounds and the string is NULL-terminated.
269 // Construct an Elf_strtab for a section with contents *P and size SIZE.
270 Elf_strtab(const unsigned char* p
, size_t size
);
272 // Return the file offset to the section headers.
274 get_c_string(size_t offset
, const char** cstring
) const
276 if (offset
>= this->usable_size_
)
278 *cstring
= this->base_
+ offset
;
283 // Contents of the section mapped to memory.
285 // One larger that the position of the last NULL character in the section.
286 // For valid SHT_STRTAB sections, this is the size of the section.
290 // Inline function definitions.
292 // Check for presence of the ELF magic number.
295 Elf_recognizer::is_elf_file(const unsigned char* ehdr_buf
, int size
)
300 static unsigned char elfmagic
[4] =
302 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
303 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
305 return memcmp(ehdr_buf
, elfmagic
, 4) == 0;
311 // Print a number to a string.
314 internal_printf_int(const char* format
, int arg
)
317 snprintf(buf
, sizeof(buf
), format
, arg
);
318 return std::string(buf
);
321 } // End anonymous namespace.
323 // Check the validity of the ELF header.
326 Elf_recognizer::is_valid_header(
327 const unsigned char* ehdr_buf
,
333 if (bufsize
< elfcpp::EI_NIDENT
)
335 *error
= _("ELF file too short");
339 int v
= ehdr_buf
[elfcpp::EI_VERSION
];
340 if (v
!= elfcpp::EV_CURRENT
)
342 if (v
== elfcpp::EV_NONE
)
343 *error
= _("invalid ELF version 0");
345 *error
= internal_printf_int(_("unsupported ELF version %d"), v
);
349 int c
= ehdr_buf
[elfcpp::EI_CLASS
];
350 if (c
== elfcpp::ELFCLASSNONE
)
352 *error
= _("invalid ELF class 0");
355 else if (c
!= elfcpp::ELFCLASS32
356 && c
!= elfcpp::ELFCLASS64
)
358 *error
= internal_printf_int(_("unsupported ELF class %d"), c
);
362 int d
= ehdr_buf
[elfcpp::EI_DATA
];
363 if (d
== elfcpp::ELFDATANONE
)
365 *error
= _("invalid ELF data encoding");
368 else if (d
!= elfcpp::ELFDATA2LSB
369 && d
!= elfcpp::ELFDATA2MSB
)
371 *error
= internal_printf_int(_("unsupported ELF data encoding %d"), d
);
375 *big_endian
= (d
== elfcpp::ELFDATA2MSB
);
377 if (c
== elfcpp::ELFCLASS32
)
379 if (bufsize
< elfcpp::Elf_sizes
<32>::ehdr_size
)
381 *error
= _("ELF file too short");
388 if (bufsize
< elfcpp::Elf_sizes
<64>::ehdr_size
)
390 *error
= _("ELF file too short");
399 // Template function definitions.
401 // Construct an Elf_file given an ELF file header.
403 template<int size
, bool big_endian
, typename File
>
405 Elf_file
<size
, big_endian
, File
>::construct(File
* file
, const Ef_ehdr
& ehdr
)
408 this->shoff_
= ehdr
.get_e_shoff();
409 this->shnum_
= ehdr
.get_e_shnum();
410 this->shstrndx_
= ehdr
.get_e_shstrndx();
411 this->large_shndx_offset_
= 0;
412 if (ehdr
.get_e_ehsize() != This::ehdr_size
)
413 file
->error(_("bad e_ehsize (%d != %d)"),
414 ehdr
.get_e_ehsize(), This::ehdr_size
);
415 if (ehdr
.get_e_shentsize() != This::shdr_size
)
416 file
->error(_("bad e_shentsize (%d != %d)"),
417 ehdr
.get_e_shentsize(), This::shdr_size
);
420 // Construct an ELF file.
422 template<int size
, bool big_endian
, typename File
>
424 Elf_file
<size
, big_endian
, File
>::Elf_file(File
* file
)
426 typename
File::View
v(file
->view(file_header_offset
, This::ehdr_size
));
427 this->construct(file
, Ef_ehdr(v
.data()));
430 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
432 template<int size
, bool big_endian
, typename File
>
434 Elf_file
<size
, big_endian
, File
>::initialize_shnum()
436 if ((this->shnum_
== 0 || this->shstrndx_
== SHN_XINDEX
)
437 && this->shoff_
!= 0)
439 typename
File::View
v(this->file_
->view(this->shoff_
, This::shdr_size
));
440 Ef_shdr
shdr(v
.data());
442 if (this->shnum_
== 0)
443 this->shnum_
= shdr
.get_sh_size();
445 if (this->shstrndx_
== SHN_XINDEX
)
447 this->shstrndx_
= shdr
.get_sh_link();
449 // Versions of the GNU binutils between 2.12 and 2.18 did
450 // not handle objects with more than SHN_LORESERVE sections
451 // correctly. All large section indexes were offset by
452 // 0x100. Some information can be found here:
453 // http://sourceware.org/bugzilla/show_bug.cgi?id=5900 .
454 // Fortunately these object files are easy to detect, as the
455 // GNU binutils always put the section header string table
456 // near the end of the list of sections. Thus if the
457 // section header string table index is larger than the
458 // number of sections, then we know we have to subtract
459 // 0x100 to get the real section index.
460 if (this->shstrndx_
>= this->shnum_
)
462 if (this->shstrndx_
>= elfcpp::SHN_LORESERVE
+ 0x100)
464 this->large_shndx_offset_
= - 0x100;
465 this->shstrndx_
-= 0x100;
467 if (this->shstrndx_
>= this->shnum_
)
468 this->file_
->error(_("bad shstrndx: %u >= %u"),
469 this->shstrndx_
, this->shnum_
);
475 // Find section with sh_type equal to TYPE and return its index.
476 // Returns SHN_UNDEF if not found.
478 template<int size
, bool big_endian
, typename File
>
480 Elf_file
<size
, big_endian
, File
>::find_section_by_type(unsigned int type
)
482 unsigned int shnum
= this->shnum();
483 typename
File::View
v(this->file_
->view(this->shoff_
,
484 This::shdr_size
* shnum
));
485 for (unsigned int i
= 0; i
< shnum
; i
++)
487 Ef_shdr
shdr(v
.data() + This::shdr_size
* i
);
488 if (shdr
.get_sh_type() == type
)
494 // Return the file offset of the section header of section SHNDX.
496 template<int size
, bool big_endian
, typename File
>
498 Elf_file
<size
, big_endian
, File
>::section_header_offset(unsigned int shndx
) const
500 if (shndx
>= this->shnum())
501 this->file_
->error(_("section_header_offset: bad shndx %u >= %u"),
502 shndx
, this->shnum());
503 return this->shoff_
+ This::shdr_size
* shndx
;
506 // Return the name of section SHNDX.
508 template<int size
, bool big_endian
, typename File
>
510 Elf_file
<size
, big_endian
, File
>::section_name(unsigned int shndx
) const
512 File
* const file
= this->file_
;
514 // Get the section name offset.
515 unsigned int sh_name
;
517 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
519 Ef_shdr
shdr(v
.data());
520 sh_name
= shdr
.get_sh_name();
523 // Get the file offset for the section name string table data.
525 typename Elf_types
<size
>::Elf_WXword shstr_size
;
527 const unsigned int shstrndx
= this->shstrndx_
;
528 typename
File::View
v(file
->view(this->section_header_offset(shstrndx
),
530 Ef_shdr
shstr_shdr(v
.data());
531 shstr_off
= shstr_shdr
.get_sh_offset();
532 shstr_size
= shstr_shdr
.get_sh_size();
535 if (sh_name
>= shstr_size
)
536 file
->error(_("bad section name offset for section %u: %u"),
539 typename
File::View
v(file
->view(shstr_off
, shstr_size
));
541 const unsigned char* datau
= v
.data();
542 const char* data
= reinterpret_cast<const char*>(datau
);
543 const void* p
= ::memchr(data
+ sh_name
, '\0', shstr_size
- sh_name
);
545 file
->error(_("missing null terminator for name of section %u"),
548 size_t len
= static_cast<const char*>(p
) - (data
+ sh_name
);
550 return std::string(data
+ sh_name
, len
);
553 // Return the contents of section SHNDX.
555 template<int size
, bool big_endian
, typename File
>
556 typename
File::Location
557 Elf_file
<size
, big_endian
, File
>::section_contents(unsigned int shndx
)
559 File
* const file
= this->file_
;
561 if (shndx
>= this->shnum())
562 file
->error(_("section_contents: bad shndx %u >= %u"),
563 shndx
, this->shnum());
565 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
567 Ef_shdr
shdr(v
.data());
568 return typename
File::Location(shdr
.get_sh_offset(), shdr
.get_sh_size());
571 // Get the size of section SHNDX.
573 template<int size
, bool big_endian
, typename File
>
574 typename Elf_types
<size
>::Elf_WXword
575 Elf_file
<size
, big_endian
, File
>::section_size(unsigned int shndx
)
577 File
* const file
= this->file_
;
579 if (shndx
>= this->shnum())
580 file
->error(_("section_size: bad shndx %u >= %u"),
581 shndx
, this->shnum());
583 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
586 Ef_shdr
shdr(v
.data());
587 return shdr
.get_sh_size();
590 // Return the section flags of section SHNDX.
592 template<int size
, bool big_endian
, typename File
>
593 typename Elf_types
<size
>::Elf_WXword
594 Elf_file
<size
, big_endian
, File
>::section_flags(unsigned int shndx
)
596 File
* const file
= this->file_
;
598 if (shndx
>= this->shnum())
599 file
->error(_("section_flags: bad shndx %u >= %u"),
600 shndx
, this->shnum());
602 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
605 Ef_shdr
shdr(v
.data());
606 return shdr
.get_sh_flags();
609 // Return the address of section SHNDX.
611 template<int size
, bool big_endian
, typename File
>
612 typename Elf_types
<size
>::Elf_Addr
613 Elf_file
<size
, big_endian
, File
>::section_addr(unsigned int shndx
)
615 File
* const file
= this->file_
;
617 if (shndx
>= this->shnum())
618 file
->error(_("section_flags: bad shndx %u >= %u"),
619 shndx
, this->shnum());
621 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
624 Ef_shdr
shdr(v
.data());
625 return shdr
.get_sh_addr();
628 // Return the type of section SHNDX.
630 template<int size
, bool big_endian
, typename File
>
632 Elf_file
<size
, big_endian
, File
>::section_type(unsigned int shndx
)
634 File
* const file
= this->file_
;
636 if (shndx
>= this->shnum())
637 file
->error(_("section_type: bad shndx %u >= %u"),
638 shndx
, this->shnum());
640 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
643 Ef_shdr
shdr(v
.data());
644 return shdr
.get_sh_type();
647 // Return the sh_link field of section SHNDX.
649 template<int size
, bool big_endian
, typename File
>
651 Elf_file
<size
, big_endian
, File
>::section_link(unsigned int shndx
)
653 File
* const file
= this->file_
;
655 if (shndx
>= this->shnum())
656 file
->error(_("section_link: bad shndx %u >= %u"),
657 shndx
, this->shnum());
659 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
662 Ef_shdr
shdr(v
.data());
663 return shdr
.get_sh_link();
666 // Return the sh_info field of section SHNDX.
668 template<int size
, bool big_endian
, typename File
>
670 Elf_file
<size
, big_endian
, File
>::section_info(unsigned int shndx
)
672 File
* const file
= this->file_
;
674 if (shndx
>= this->shnum())
675 file
->error(_("section_info: bad shndx %u >= %u"),
676 shndx
, this->shnum());
678 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
681 Ef_shdr
shdr(v
.data());
682 return shdr
.get_sh_info();
685 // Return the sh_addralign field of section SHNDX.
687 template<int size
, bool big_endian
, typename File
>
688 typename Elf_types
<size
>::Elf_WXword
689 Elf_file
<size
, big_endian
, File
>::section_addralign(unsigned int shndx
)
691 File
* const file
= this->file_
;
693 if (shndx
>= this->shnum())
694 file
->error(_("section_addralign: bad shndx %u >= %u"),
695 shndx
, this->shnum());
697 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
700 Ef_shdr
shdr(v
.data());
701 return shdr
.get_sh_addralign();
705 Elf_strtab::Elf_strtab(const unsigned char* p
, size_t size
)
707 // Check if the section is NUL-terminated. If it isn't, we ignore
708 // the last part to make sure we don't return non-NUL-terminated
710 while (size
> 0 && p
[size
- 1] != 0)
712 this->base_
= reinterpret_cast<const char*>(p
);
713 this->usable_size_
= size
;
716 } // End namespace elfcpp.
718 #endif // !defined(ELFCPP_FILE_H)