1 // elfcpp_file.h -- file access for elfcpp -*- C++ -*-
3 // This header file defines the class Elf_file which can be used to
4 // read useful data from an ELF file. The functions here are all
5 // templates which take a file interface object as a parameter. This
6 // type must have a subtype View. This type must support two methods:
7 // View view(off_t file_offset, off_t data_size)
8 // returns a View for the specified part of the file.
9 // void error(const char* printf_format, ...)
10 // prints an error message and does not return. The subtype View must
12 // const unsigned char* data()
13 // which returns a pointer to a buffer containing the requested data.
14 // This general interface is used to read data from the file. Objects
15 // of type View will never survive longer than the elfcpp function.
17 // Some of these functions must return a reference to part of the
18 // file. To use these, the file interface must support a subtype
20 // Location(off_t file_offset, off_t data_size)
21 // To use this in conjunction with the accessors types Shdr, etc., the
22 // file interface should support an overload of view:
23 // View view(Location)
24 // This permits writing
25 // elfcpp::Shdr shdr(file, ef.section_header(n));
36 // This object is used to read an ELF file.
37 // SIZE: The size of file, 32 or 64.
38 // BIG_ENDIAN: Whether the file is in big-endian format.
39 // FILE: A file reading type as described above.
41 template<int size
, bool big_endian
, typename File
>
45 typedef Elf_file
<size
, big_endian
, File
> This
;
48 static const int ehdr_size
= Elf_sizes
<size
>::ehdr_size
;
49 static const int phdr_size
= Elf_sizes
<size
>::phdr_size
;
50 static const int shdr_size
= Elf_sizes
<size
>::shdr_size
;
51 static const int sym_size
= Elf_sizes
<size
>::sym_size
;
52 static const int rel_size
= Elf_sizes
<size
>::rel_size
;
53 static const int rela_size
= Elf_sizes
<size
>::rela_size
;
55 typedef Ehdr
<size
, big_endian
> Ef_ehdr
;
56 typedef Phdr
<size
, big_endian
> Ef_phdr
;
57 typedef Shdr
<size
, big_endian
> Ef_shdr
;
58 typedef Sym
<size
, big_endian
> Ef_sym
;
60 // Construct an Elf_file given an ELF file header.
61 Elf_file(File
* file
, const Ef_ehdr
& ehdr
)
62 { this->construct(file
, ehdr
); }
64 // Construct an ELF file.
68 // Return the file offset to the section headers.
71 { return this->shoff_
; }
73 // Return the number of sections.
77 this->initialize_shnum();
81 // Return the section index of the section name string table.
85 this->initialize_shnum();
86 return this->shstrndx_
;
89 // Return the location of the header of section SHNDX.
90 typename
File::Location
91 section_header(unsigned int shndx
)
93 return typename
File::Location(this->section_header_offset(shndx
),
97 // Return the name of section SHNDX.
99 section_name(unsigned int shndx
);
101 // Return the location of the contents of section SHNDX.
102 typename
File::Location
103 section_contents(unsigned int shndx
);
105 // Return the flags of section SHNDX.
106 typename Elf_types
<size
>::Elf_WXword
107 section_flags(unsigned int shndx
);
110 // Shared constructor code.
112 construct(File
* file
, const Ef_ehdr
& ehdr
);
114 // Initialize shnum_ and shstrndx_.
118 // Return the file offset of the header of section SHNDX.
120 section_header_offset(unsigned int shndx
);
122 // The file we are reading.
124 // The file offset to the section headers.
126 // The number of sections.
128 // The section index of the section name string table.
129 unsigned int shstrndx_
;
132 // Template function definitions.
134 // Construct an Elf_file given an ELF file header.
136 template<int size
, bool big_endian
, typename File
>
138 Elf_file
<size
, big_endian
, File
>::construct(File
* file
, const Ef_ehdr
& ehdr
)
141 this->shoff_
= ehdr
.get_e_shoff();
142 this->shnum_
= ehdr
.get_e_shnum();
143 this->shstrndx_
= ehdr
.get_e_shstrndx();
144 if (ehdr
.get_e_ehsize() != This::ehdr_size
)
145 file
->error(_("bad e_ehsize (%d != %d)"),
146 ehdr
.get_e_ehsize(), This::ehdr_size
);
147 if (ehdr
.get_e_shentsize() != This::shdr_size
)
148 file
->error(_("bad e_shentsize (%d != %d)"),
149 ehdr
.get_e_shentsize(), This::shdr_size
);
152 // Construct an ELF file.
154 template<int size
, bool big_endian
, typename File
>
156 Elf_file
<size
, big_endian
, File
>::Elf_file(File
* file
)
158 typename
File::View
v(file
->view(file_header_offset
, This::ehdr_size
));
159 this->construct(file
, Ef_ehdr(v
.data()));
162 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
164 template<int size
, bool big_endian
, typename File
>
166 Elf_file
<size
, big_endian
, File
>::initialize_shnum()
168 if ((this->shnum_
== 0 || this->shstrndx_
== SHN_XINDEX
)
169 && this->shoff_
!= 0)
171 typename
File::View
v(this->file_
->view(this->shoff_
, This::shdr_size
));
172 Ef_shdr
shdr(v
.data());
173 if (this->shnum_
== 0)
174 this->shnum_
= shdr
.get_sh_size();
175 if (this->shstrndx_
== SHN_XINDEX
)
176 this->shstrndx_
= shdr
.get_sh_link();
180 // Return the file offset of the section header of section SHNDX.
182 template<int size
, bool big_endian
, typename File
>
184 Elf_file
<size
, big_endian
, File
>::section_header_offset(unsigned int shndx
)
186 if (shndx
>= this->shnum())
187 this->file_
->error(_("section_header_offset: bad shndx %u >= %u"),
188 shndx
, this->shnum());
189 return this->shoff_
+ This::shdr_size
* shndx
;
192 // Return the name of section SHNDX.
194 template<int size
, bool big_endian
, typename File
>
196 Elf_file
<size
, big_endian
, File
>::section_name(unsigned int shndx
)
198 File
* const file
= this->file_
;
200 // Get the section name offset.
201 unsigned int sh_name
;
203 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
205 Ef_shdr
shdr(v
.data());
206 sh_name
= shdr
.get_sh_name();
209 // Get the file offset for the section name string table data.
213 const unsigned int shstrndx
= this->shstrndx_
;
214 typename
File::View
v(file
->view(this->section_header_offset(shstrndx
),
216 Ef_shdr
shstr_shdr(v
.data());
217 shstr_off
= shstr_shdr
.get_sh_offset();
218 shstr_size
= shstr_shdr
.get_sh_size();
221 if (sh_name
>= shstr_size
)
222 file
->error(_("bad section name offset for section %u: %u"),
225 typename
File::View
v(file
->view(shstr_off
, shstr_size
));
227 const unsigned char* datau
= v
.data();
228 const char* data
= reinterpret_cast<const char*>(datau
);
229 const void* p
= ::memchr(data
+ sh_name
, '\0', shstr_size
- sh_name
);
231 file
->error(_("missing null terminator for name of section %u"),
234 size_t len
= static_cast<const char*>(p
) - (data
+ sh_name
);
236 return std::string(data
+ sh_name
, len
);
239 // Return the contents of section SHNDX.
241 template<int size
, bool big_endian
, typename File
>
242 typename
File::Location
243 Elf_file
<size
, big_endian
, File
>::section_contents(unsigned int shndx
)
245 File
* const file
= this->file_
;
247 if (shndx
>= this->shnum())
248 file
->error(_("section_contents: bad shndx %u >= %u"),
249 shndx
, this->shnum());
251 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
253 Ef_shdr
shdr(v
.data());
254 return typename
File::Location(shdr
.get_sh_offset(), shdr
.get_sh_size());
257 // Return the section flags of section SHNDX.
259 template<int size
, bool big_endian
, typename File
>
260 typename Elf_types
<size
>::Elf_WXword
261 Elf_file
<size
, big_endian
, File
>::section_flags(unsigned int shndx
)
263 File
* const file
= this->file_
;
265 if (shndx
>= this->shnum())
266 file
->error(_("section_flags: bad shndx %u >= %u"),
267 shndx
, this->shnum());
269 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
272 Ef_shdr
shdr(v
.data());
273 return shdr
.get_sh_flags();
276 } // End namespace elfcpp.
278 #endif // !defined(ELFCPP_FILE_H)