llvm.org GIT mirror llvm / 4d9c539
[Object][ELF] Add a maximum alignment. This is used by createELFObjectFile to create a properly aligned reader. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171520 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 7 years ago
7 changed file(s) with 522 addition(s) and 442 deletion(s). Raw diff Collapse all Expand all
3232 namespace llvm {
3333 namespace object {
3434
35 using support::endianness;
36
37 template
38 struct MaximumAlignment {
39 enum {value = AlignOf::Alignment > max_align ? max_align
40 : AlignOf::Alignment};
41 };
42
3543 // Subclasses of ELFObjectFile may need this for template instantiation
3644 inline std::pair
3745 getElfArchType(MemoryBuffer *Object) {
4250 }
4351
4452 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
45 template<support::endianness target_endianness>
53 template<endianness target_endianness, std::size_t max_alignment>
4654 struct ELFDataTypeTypedefHelperCommon {
4755 typedef support::detail::packed_endian_specific_integral
48 support::aligned> Elf_Half;
56
57 MaximumAlignment::value> Elf_Half;
4958 typedef support::detail::packed_endian_specific_integral
50 support::aligned> Elf_Word;
59
60 MaximumAlignment::value> Elf_Word;
5161 typedef support::detail::packed_endian_specific_integral
52 support::aligned> Elf_Sword;
62
63 MaximumAlignment::value> Elf_Sword;
5364 typedef support::detail::packed_endian_specific_integral
54 support::aligned> Elf_Xword;
65
66 MaximumAlignment::value> Elf_Xword;
5567 typedef support::detail::packed_endian_specific_integral
56 Elf_Sxword;
57 };
58
59 template
68
69 MaximumAlignment::value> Elf_Sxword;
70 };
71
72 template
6073 struct ELFDataTypeTypedefHelper;
6174
6275 /// ELF 32bit types.
63 template
64 struct ELFDataTypeTypedefHelper
65 : ELFDataTypeTypedefHelperCommon {
76 template
77 struct ELFDataTypeTypedefHelper
78 : ELFDataTypeTypedefHelperCommon {
6679 typedef uint32_t value_type;
6780 typedef support::detail::packed_endian_specific_integral
68 support::aligned> Elf_Addr;
81
82 MaximumAlignment::value> Elf_Addr;
6983 typedef support::detail::packed_endian_specific_integral
70 support::aligned> Elf_Off;
84
85 MaximumAlignment::value> Elf_Off;
7186 };
7287
7388 /// ELF 64bit types.
74 template
75 struct ELFDataTypeTypedefHelper
76 : ELFDataTypeTypedefHelperCommon{
89 template
90 struct ELFDataTypeTypedefHelper
91 : ELFDataTypeTypedefHelperCommon{
7792 typedef uint64_t value_type;
7893 typedef support::detail::packed_endian_specific_integral
79 support::aligned> Elf_Addr;
94
95 MaximumAlignment::value> Elf_Addr;
8096 typedef support::detail::packed_endian_specific_integral
81 support::aligned> Elf_Off;
97
98 MaximumAlignment::value> Elf_Off;
8299 };
83100
84101 // I really don't like doing this, but the alternative is copypasta.
85 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
86 typedef typename \
87 ELFDataTypeTypedefHelper::Elf_Addr Elf_Addr; \
88 typedef typename \
89 ELFDataTypeTypedefHelper::Elf_Off Elf_Off; \
90 typedef typename \
91 ELFDataTypeTypedefHelper::Elf_Half Elf_Half; \
92 typedef typename \
93 ELFDataTypeTypedefHelper::Elf_Word Elf_Word; \
94 typedef typename \
95 ELFDataTypeTypedefHelper::Elf_Sword Elf_Sword; \
96 typedef typename \
97 ELFDataTypeTypedefHelper::Elf_Xword Elf_Xword; \
98 typedef typename \
99 ELFDataTypeTypedefHelper::Elf_Sxword Elf_Sxword;
102 #define LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits) \
103 typedef typename ELFDataTypeTypedefHelper \
104 ::Elf_Addr Elf_Addr; \
105 typedef typename ELFDataTypeTypedefHelper \
106 ::Elf_Off Elf_Off; \
107 typedef typename ELFDataTypeTypedefHelper \
108 ::Elf_Half Elf_Half; \
109 typedef typename ELFDataTypeTypedefHelper \
110 ::Elf_Word Elf_Word; \
111 typedef typename ELFDataTypeTypedefHelper \
112 ::Elf_Sword Elf_Sword; \
113 typedef typename ELFDataTypeTypedefHelper \
114 ::Elf_Xword Elf_Xword; \
115 typedef typename ELFDataTypeTypedefHelper \
116 ::Elf_Sxword Elf_Sxword;
100117
101118 // Section header.
102 template<support::endianness target_endianness, bool is64Bits>
119 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
103120 struct Elf_Shdr_Base;
104121
105 template
106 struct Elf_Shdr_Base {
107 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
122 template
123 struct Elf_Shdr_Base {
124 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
108125 Elf_Word sh_name; // Section name (index into string table)
109126 Elf_Word sh_type; // Section type (SHT_*)
110127 Elf_Word sh_flags; // Section flags (SHF_*)
117134 Elf_Word sh_entsize; // Size of records contained within the section
118135 };
119136
120 template
121 struct Elf_Shdr_Base {
122 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
137 template
138 struct Elf_Shdr_Base {
139 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
123140 Elf_Word sh_name; // Section name (index into string table)
124141 Elf_Word sh_type; // Section type (SHT_*)
125142 Elf_Xword sh_flags; // Section flags (SHF_*)
132149 Elf_Xword sh_entsize; // Size of records contained within the section
133150 };
134151
135 template
136 struct Elf_Shdr_Impl : Elf_Shdr_Base {
137 using Elf_Shdr_Base::sh_entsize;
138 using Elf_Shdr_Base::sh_size;
152 template
153 struct Elf_Shdr_Impl
154 : Elf_Shdr_Base {
155 using Elf_Shdr_Base::sh_entsize;
156 using Elf_Shdr_Base::sh_size;
139157
140158 /// @brief Get the number of entities this section contains if it has any.
141159 unsigned getEntityCount() const {
145163 }
146164 };
147165
148 template<support::endianness target_endianness, bool is64Bits>
166 template< endianness target_endianness
167 , std::size_t max_alignment
168 , bool is64Bits>
149169 struct Elf_Sym_Base;
150170
151 template
152 struct Elf_Sym_Base {
153 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
171 template
172 struct Elf_Sym_Base {
173 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
154174 Elf_Word st_name; // Symbol name (index into string table)
155175 Elf_Addr st_value; // Value or address associated with the symbol
156176 Elf_Word st_size; // Size of the symbol
159179 Elf_Half st_shndx; // Which section (header table index) it's defined in
160180 };
161181
162 template
163 struct Elf_Sym_Base {
164 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
182 template
183 struct Elf_Sym_Base {
184 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
165185 Elf_Word st_name; // Symbol name (index into string table)
166186 unsigned char st_info; // Symbol's type and binding attributes
167187 unsigned char st_other; // Must be zero; reserved
170190 Elf_Xword st_size; // Size of the symbol
171191 };
172192
173 template
174 struct Elf_Sym_Impl : Elf_Sym_Base {
175 using Elf_Sym_Base::st_info;
193 template
194 struct Elf_Sym_Impl
195 : Elf_Sym_Base {
196 using Elf_Sym_Base::st_info;
176197
177198 // These accessors and mutators correspond to the ELF32_ST_BIND,
178199 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
187208
188209 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
189210 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
190 template<support::endianness target_endianness, bool is64Bits>
211 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
191212 struct Elf_Versym_Impl {
192 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
213 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
193214 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
194215 };
195216
196 template<support::endianness target_endianness, bool is64Bits>
217 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
197218 struct Elf_Verdaux_Impl;
198219
199220 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
200221 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
201 template<support::endianness target_endianness, bool is64Bits>
222 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
202223 struct Elf_Verdef_Impl {
203 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
204 typedef Elf_Verdaux_Impl Elf_Verdaux;
224 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
225 typedef
226 Elf_Verdaux_Impl Elf_Verdaux;
205227 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
206228 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
207229 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
218240
219241 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
220242 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
221 template<support::endianness target_endianness, bool is64Bits>
243 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
222244 struct Elf_Verdaux_Impl {
223 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
245 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
224246 Elf_Word vda_name; // Version name (offset in string table)
225247 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
226248 };
227249
228250 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
229251 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
230 template<support::endianness target_endianness, bool is64Bits>
252 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
231253 struct Elf_Verneed_Impl {
232 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
254 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
233255 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
234256 Elf_Half vn_cnt; // Number of associated Vernaux entries
235257 Elf_Word vn_file; // Library name (string table offset)
239261
240262 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
241263 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
242 template<support::endianness target_endianness, bool is64Bits>
264 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
243265 struct Elf_Vernaux_Impl {
244 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
266 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
245267 Elf_Word vna_hash; // Hash of dependency name
246268 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
247269 Elf_Half vna_other; // Version index, used in .gnu.version entries
251273
252274 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
253275 /// table section (.dynamic) look like.
254 template<support::endianness target_endianness, bool is64Bits>
276 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
255277 struct Elf_Dyn_Base;
256278
257 template
258 struct Elf_Dyn_Base {
259 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
279 template
280 struct Elf_Dyn_Base {
281 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
260282 Elf_Sword d_tag;
261283 union {
262284 Elf_Word d_val;
264286 } d_un;
265287 };
266288
267 template
268 struct Elf_Dyn_Base {
269 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
289 template
290 struct Elf_Dyn_Base {
291 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
270292 Elf_Sxword d_tag;
271293 union {
272294 Elf_Xword d_val;
275297 };
276298
277299 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
278 template
279 struct Elf_Dyn_Impl : Elf_Dyn_Base {
280 using Elf_Dyn_Base::d_tag;
281 using Elf_Dyn_Base::d_un;
300 template
301 struct Elf_Dyn_Impl : Elf_Dyn_Base {
302 using Elf_Dyn_Base::d_tag;
303 using Elf_Dyn_Base::d_un;
282304 int64_t getTag() const { return d_tag; }
283305 uint64_t getVal() const { return d_un.d_val; }
284306 uint64_t getPtr() const { return d_un.ptr; }
285307 };
286308
287 template<support::endianness target_endianness, bool is64Bits>
309 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
288310 class ELFObjectFile;
289311
290312 // DynRefImpl: Reference to an entry in the dynamic table
291313 // This is an ELF-specific interface.
292 template<support::endianness target_endianness, bool is64Bits>
314 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
293315 class DynRefImpl {
294 typedef Elf_Dyn_Impl Elf_Dyn;
295 typedef ELFObjectFile OwningType;
316 typedef Elf_Dyn_Impl Elf_Dyn;
317 typedef ELFObjectFile OwningType;
296318
297319 DataRefImpl DynPimpl;
298320 const OwningType *OwningObject;
314336 };
315337
316338 // Elf_Rel: Elf Relocation
317 template<support::endianness target_endianness, bool is64Bits, bool isRela>
339 template< endianness target_endianness
340 , std::size_t max_alignment
341 , bool is64Bits
342 , bool isRela>
318343 struct Elf_Rel_Base;
319344
320 template
321 struct Elf_Rel_Base {
322 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
345 template
346 struct Elf_Rel_Base {
347 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
323348 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
324349 Elf_Word r_info; // Symbol table index and type of relocation to apply
325350 };
326351
327 template
328 struct Elf_Rel_Base {
329 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
352 template
353 struct Elf_Rel_Base {
354 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
330355 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
331356 Elf_Xword r_info; // Symbol table index and type of relocation to apply
332357 };
333358
334 template
335 struct Elf_Rel_Base {
336 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
359 template
360 struct Elf_Rel_Base {
361 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
337362 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
338363 Elf_Word r_info; // Symbol table index and type of relocation to apply
339364 Elf_Sword r_addend; // Compute value for relocatable field by adding this
340365 };
341366
342 template
343 struct Elf_Rel_Base {
344 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
367 template
368 struct Elf_Rel_Base {
369 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
345370 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
346371 Elf_Xword r_info; // Symbol table index and type of relocation to apply
347372 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
348373 };
349374
350 template<support::endianness target_endianness, bool is64Bits, bool isRela>
375 template< endianness target_endianness
376 , std::size_t max_alignment
377 , bool is64Bits
378 , bool isRela>
351379 struct Elf_Rel_Impl;
352380
353 template
354 struct Elf_Rel_Impl
355 : Elf_Rel_Base {
356 using Elf_Rel_Base::r_info;
357 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
381 template
382 struct Elf_Rel_Impl
383 : Elf_Rel_Base {
384 using Elf_Rel_Base::r_info;
385 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
358386
359387 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
360388 // and ELF64_R_INFO macros defined in the ELF specification:
369397 }
370398 };
371399
372 template
373 struct Elf_Rel_Impl
374 : Elf_Rel_Base {
375 using Elf_Rel_Base::r_info;
376 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
400 template
401 struct Elf_Rel_Impl
402 : Elf_Rel_Base {
403 using Elf_Rel_Base::r_info;
404 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
377405
378406 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
379407 // and ELF32_R_INFO macros defined in the ELF specification:
386414 }
387415 };
388416
389 template<support::endianness target_endianness, bool is64Bits>
417 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
390418 struct Elf_Ehdr_Impl {
391 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
419 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
392420 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
393421 Elf_Half e_type; // Type of file (see ET_*)
394422 Elf_Half e_machine; // Required architecture for this file (see EM_*)
411439 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
412440 };
413441
414 template<support::endianness target_endianness, bool is64Bits>
442 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
415443 struct Elf_Phdr;
416444
417 template
418 struct Elf_Phdr {
419 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
445 template
446 struct Elf_Phdr {
447 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
420448 Elf_Word p_type; // Type of segment
421449 Elf_Off p_offset; // FileOffset where segment is located, in bytes
422 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
450 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
423451 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
424452 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
425453 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
427455 Elf_Word p_align; // Segment alignment constraint
428456 };
429457
430 template
431 struct Elf_Phdr {
432 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
458 template
459 struct Elf_Phdr {
460 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
433461 Elf_Word p_type; // Type of segment
434462 Elf_Word p_flags; // Segment flags
435463 Elf_Off p_offset; // FileOffset where segment is located, in bytes
436 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
464 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
437465 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
438466 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
439467 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
440468 Elf_Word p_align; // Segment alignment constraint
441469 };
442470
443 template<support::endianness target_endianness, bool is64Bits>
471 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
444472 class ELFObjectFile : public ObjectFile {
445 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
446
447 typedef Elf_Ehdr_Impl Elf_Ehdr;
448 typedef Elf_Shdr_Impl Elf_Shdr;
449 typedef Elf_Sym_Impl Elf_Sym;
450 typedef Elf_Dyn_Impl Elf_Dyn;
451 typedef Elf_Rel_Impl Elf_Rel;
452 typedef Elf_Rel_Impl Elf_Rela;
453 typedef Elf_Verdef_Impl Elf_Verdef;
454 typedef Elf_Verdaux_Impl Elf_Verdaux;
455 typedef Elf_Verneed_Impl Elf_Verneed;
456 typedef Elf_Vernaux_Impl Elf_Vernaux;
457 typedef Elf_Versym_Impl Elf_Versym;
458 typedef DynRefImpl DynRef;
473 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
474
475 typedef Elf_Ehdr_Impl Elf_Ehdr;
476 typedef Elf_Shdr_Impl Elf_Shdr;
477 typedef Elf_Sym_Impl Elf_Sym;
478 typedef Elf_Dyn_Impl Elf_Dyn;
479 typedef
480 Elf_Rel_Impl Elf_Rel;
481 typedef
482 Elf_Rel_Impl Elf_Rela;
483 typedef
484 Elf_Verdef_Impl Elf_Verdef;
485 typedef
486 Elf_Verdaux_Impl Elf_Verdaux;
487 typedef
488 Elf_Verneed_Impl Elf_Verneed;
489 typedef
490 Elf_Vernaux_Impl Elf_Vernaux;
491 typedef
492 Elf_Versym_Impl Elf_Versym;
493 typedef DynRefImpl DynRef;
459494 typedef content_iterator dyn_iterator;
460495
461496 protected:
622657 section_iterator &Res) const;
623658 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
624659
625 friend class DynRefImplis64Bits>;
660 friend class DynRefImplmax_alignment, is64Bits>;
626661 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
627662
628663 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
728763
729764 // Iterate through the version definitions, and place each Elf_Verdef
730765 // in the VersionMap according to its index.
731 template
732 void ELFObjectFile::
766 template
767 void ELFObjectFile::
733768 LoadVersionDefs(const Elf_Shdr *sec) const {
734769 unsigned vd_size = sec->sh_size; // Size of section in bytes
735770 unsigned vd_count = sec->sh_info; // Number of Verdef entries
754789
755790 // Iterate through the versions needed section, and place each Elf_Vernaux
756791 // in the VersionMap according to its index.
757 template
758 void ELFObjectFile::
792 template
793 void ELFObjectFile::
759794 LoadVersionNeeds(const Elf_Shdr *sec) const {
760795 unsigned vn_size = sec->sh_size; // Size of section in bytes
761796 unsigned vn_count = sec->sh_info; // Number of Verneed entries
787822 }
788823 }
789824
790 template
791 void ELFObjectFile::LoadVersionMap() const {
825 template
826 void ELFObjectFile
827 ::LoadVersionMap() const {
792828 // If there is no dynamic symtab or version table, there is nothing to do.
793829 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
794830 return;
809845 LoadVersionNeeds(dot_gnu_version_r_sec);
810846 }
811847
812 template
813 void ELFObjectFile is64Bits>
848 template is64Bits>
849 void ELFObjectFile
814850 ::validateSymbol(DataRefImpl Symb) const {
815851 const Elf_Sym *symb = getSymbol(Symb);
816852 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
828864 report_fatal_error("Symb must point to a valid symbol!");
829865 }
830866
831 template
832 error_code ELFObjectFile is64Bits>
867 template is64Bits>
868 error_code ELFObjectFile
833869 ::getSymbolNext(DataRefImpl Symb,
834870 SymbolRef &Result) const {
835871 validateSymbol(Symb);
856892 return object_error::success;
857893 }
858894
859 template
860 error_code ELFObjectFile is64Bits>
895 template is64Bits>
896 error_code ELFObjectFile
861897 ::getSymbolName(DataRefImpl Symb,
862898 StringRef &Result) const {
863899 validateSymbol(Symb);
865901 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
866902 }
867903
868 template
869 error_code ELFObjectFile is64Bits>
904 template is64Bits>
905 error_code ELFObjectFile
870906 ::getSymbolVersion(SymbolRef SymRef,
871907 StringRef &Version,
872908 bool &IsDefault) const {
877913 Version, IsDefault);
878914 }
879915
880 template
881 ELF::Elf64_Word ELFObjectFile is64Bits>
916 template is64Bits>
917 ELF::Elf64_Word ELFObjectFile
882918 ::getSymbolTableIndex(const Elf_Sym *symb) const {
883919 if (symb->st_shndx == ELF::SHN_XINDEX)
884920 return ExtendedSymbolTable.lookup(symb);
885921 return symb->st_shndx;
886922 }
887923
888 template
889 const typename ELFObjectFile::Elf_Shdr *
890 ELFObjectFile is64Bits>
924 template is64Bits>
925 const typename ELFObjectFile
926 ::Elf_Shdr *
927 ELFObjectFile
891928 ::getSection(const Elf_Sym *symb) const {
892929 if (symb->st_shndx == ELF::SHN_XINDEX)
893930 return getSection(ExtendedSymbolTable.lookup(symb));
896933 return getSection(symb->st_shndx);
897934 }
898935
899 template
900 const typename ELFObjectFile::Elf_Shdr *
901 ELFObjectFile is64Bits>
936 template is64Bits>
937 const typename ELFObjectFile
938 ::Elf_Shdr *
939 ELFObjectFile
902940 ::getElfSection(section_iterator &It) const {
903941 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
904942 return reinterpret_cast(ShdrRef.p);
905943 }
906944
907 template
908 const typename ELFObjectFile::Elf_Sym *
909 ELFObjectFile is64Bits>
945 template is64Bits>
946 const typename ELFObjectFile
947 ::Elf_Sym *
948 ELFObjectFile
910949 ::getElfSymbol(symbol_iterator &It) const {
911950 return getSymbol(It->getRawDataRefImpl());
912951 }
913952
914 template
915 const typename ELFObjectFile::Elf_Sym *
916 ELFObjectFile is64Bits>
953 template is64Bits>
954 const typename ELFObjectFile
955 ::Elf_Sym *
956 ELFObjectFile
917957 ::getElfSymbol(uint32_t index) const {
918958 DataRefImpl SymbolData;
919959 SymbolData.d.a = index;
921961 return getSymbol(SymbolData);
922962 }
923963
924 template
925 error_code ELFObjectFile is64Bits>
964 template is64Bits>
965 error_code ELFObjectFile
926966 ::getSymbolFileOffset(DataRefImpl Symb,
927967 uint64_t &Result) const {
928968 validateSymbol(Symb);
956996 }
957997 }
958998
959 template
960 error_code ELFObjectFile is64Bits>
999 template is64Bits>
1000 error_code ELFObjectFile
9611001 ::getSymbolAddress(DataRefImpl Symb,
9621002 uint64_t &Result) const {
9631003 validateSymbol(Symb);
10001040 }
10011041 }
10021042
1003 template
1004 error_code ELFObjectFile is64Bits>
1043 template is64Bits>
1044 error_code ELFObjectFile
10051045 ::getSymbolSize(DataRefImpl Symb,
10061046 uint64_t &Result) const {
10071047 validateSymbol(Symb);
10121052 return object_error::success;
10131053 }
10141054
1015 template
1016 error_code ELFObjectFile is64Bits>
1055 template is64Bits>
1056 error_code ELFObjectFile
10171057 ::getSymbolNMTypeChar(DataRefImpl Symb,
10181058 char &Result) const {
10191059 validateSymbol(Symb);
10771117 return object_error::success;
10781118 }
10791119
1080 template
1081 error_code ELFObjectFile is64Bits>
1120 template is64Bits>
1121 error_code ELFObjectFile
10821122 ::getSymbolType(DataRefImpl Symb,
10831123 SymbolRef::Type &Result) const {
10841124 validateSymbol(Symb);
11091149 return object_error::success;
11101150 }
11111151
1112 template
1113 error_code ELFObjectFile is64Bits>
1152 template is64Bits>
1153 error_code ELFObjectFile
11141154 ::getSymbolFlags(DataRefImpl Symb,
11151155 uint32_t &Result) const {
11161156 validateSymbol(Symb);
11441184 return object_error::success;
11451185 }
11461186
1147 template
1148 error_code ELFObjectFile is64Bits>
1187 template is64Bits>
1188 error_code ELFObjectFile
11491189 ::getSymbolSection(DataRefImpl Symb,
11501190 section_iterator &Res) const {
11511191 validateSymbol(Symb);
11611201 return object_error::success;
11621202 }
11631203
1164 template
1165 error_code ELFObjectFile is64Bits>
1204 template is64Bits>
1205 error_code ELFObjectFile
11661206 ::getSymbolValue(DataRefImpl Symb,
11671207 uint64_t &Val) const {
11681208 validateSymbol(Symb);
11711211 return object_error::success;
11721212 }
11731213
1174 template
1175 error_code ELFObjectFile is64Bits>
1214 template is64Bits>
1215 error_code ELFObjectFile
11761216 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
11771217 const uint8_t *sec = reinterpret_cast(Sec.p);
11781218 sec += Header->e_shentsize;
11811221 return object_error::success;
11821222 }
11831223
1184 template
1185 error_code ELFObjectFile is64Bits>
1224 template is64Bits>
1225 error_code ELFObjectFile
11861226 ::getSectionName(DataRefImpl Sec,
11871227 StringRef &Result) const {
11881228 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
11901230 return object_error::success;
11911231 }
11921232
1193 template
1194 error_code ELFObjectFile is64Bits>
1233 template is64Bits>
1234 error_code ELFObjectFile
11951235 ::getSectionAddress(DataRefImpl Sec,
11961236 uint64_t &Result) const {
11971237 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
11991239 return object_error::success;
12001240 }
12011241
1202 template
1203 error_code ELFObjectFile is64Bits>
1242 template is64Bits>
1243 error_code ELFObjectFile
12041244 ::getSectionSize(DataRefImpl Sec,
12051245 uint64_t &Result) const {
12061246 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12081248 return object_error::success;
12091249 }
12101250
1211 template
1212 error_code ELFObjectFile is64Bits>
1251 template is64Bits>
1252 error_code ELFObjectFile
12131253 ::getSectionContents(DataRefImpl Sec,
12141254 StringRef &Result) const {
12151255 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12181258 return object_error::success;
12191259 }
12201260
1221 template
1222 error_code ELFObjectFile is64Bits>
1261 template is64Bits>
1262 error_code ELFObjectFile
12231263 ::getSectionContents(const Elf_Shdr *Sec,
12241264 StringRef &Result) const {
12251265 const char *start = (const char*)base() + Sec->sh_offset;
12271267 return object_error::success;
12281268 }
12291269
1230 template
1231 error_code ELFObjectFile is64Bits>
1270 template is64Bits>
1271 error_code ELFObjectFile
12321272 ::getSectionAlignment(DataRefImpl Sec,
12331273 uint64_t &Result) const {
12341274 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12361276 return object_error::success;
12371277 }
12381278
1239 template
1240 error_code ELFObjectFile is64Bits>
1279 template is64Bits>
1280 error_code ELFObjectFile
12411281 ::isSectionText(DataRefImpl Sec,
12421282 bool &Result) const {
12431283 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12481288 return object_error::success;
12491289 }
12501290
1251 template
1252 error_code ELFObjectFile is64Bits>
1291 template is64Bits>
1292 error_code ELFObjectFile
12531293 ::isSectionData(DataRefImpl Sec,
12541294 bool &Result) const {
12551295 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12611301 return object_error::success;
12621302 }
12631303
1264 template
1265 error_code ELFObjectFile is64Bits>
1304 template is64Bits>
1305 error_code ELFObjectFile
12661306 ::isSectionBSS(DataRefImpl Sec,
12671307 bool &Result) const {
12681308 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12741314 return object_error::success;
12751315 }
12761316
1277 template
1278 error_code ELFObjectFile is64Bits>
1317 template is64Bits>
1318 error_code ELFObjectFile
12791319 ::isSectionRequiredForExecution(DataRefImpl Sec,
12801320 bool &Result) const {
12811321 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12861326 return object_error::success;
12871327 }
12881328
1289 template
1290 error_code ELFObjectFile is64Bits>
1329 template is64Bits>
1330 error_code ELFObjectFile
12911331 ::isSectionVirtual(DataRefImpl Sec,
12921332 bool &Result) const {
12931333 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
12981338 return object_error::success;
12991339 }
13001340
1301 template
1302 error_code ELFObjectFile is64Bits>
1341 template is64Bits>
1342 error_code ELFObjectFile
13031343 ::isSectionZeroInit(DataRefImpl Sec,
13041344 bool &Result) const {
13051345 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
13091349 return object_error::success;
13101350 }
13111351
1312 template
1313 error_code ELFObjectFile is64Bits>
1352 template is64Bits>
1353 error_code ELFObjectFile
13141354 ::isSectionReadOnlyData(DataRefImpl Sec,
13151355 bool &Result) const {
13161356 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
13211361 return object_error::success;
13221362 }
13231363
1324 template
1325 error_code ELFObjectFile is64Bits>
1364 template is64Bits>
1365 error_code ELFObjectFile
13261366 ::sectionContainsSymbol(DataRefImpl Sec,
13271367 DataRefImpl Symb,
13281368 bool &Result) const {
13311371 return object_error::success;
13321372 }
13331373
1334 template
1335 relocation_iterator ELFObjectFile is64Bits>
1374 template is64Bits>
1375 relocation_iterator ELFObjectFile
13361376 ::getSectionRelBegin(DataRefImpl Sec) const {
13371377 DataRefImpl RelData;
13381378 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
13451385 return relocation_iterator(RelocationRef(RelData, this));
13461386 }
13471387
1348 template
1349 relocation_iterator ELFObjectFile is64Bits>
1388 template is64Bits>
1389 relocation_iterator ELFObjectFile
13501390 ::getSectionRelEnd(DataRefImpl Sec) const {
13511391 DataRefImpl RelData;
13521392 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
13631403 }
13641404
13651405 // Relocations
1366 template
1367 error_code ELFObjectFile is64Bits>
1406 template is64Bits>
1407 error_code ELFObjectFile
13681408 ::getRelocationNext(DataRefImpl Rel,
13691409 RelocationRef &Result) const {
13701410 ++Rel.w.c;
13921432 return object_error::success;
13931433 }
13941434
1395 template
1396 error_code ELFObjectFile is64Bits>
1435 template is64Bits>
1436 error_code ELFObjectFile
13971437 ::getRelocationSymbol(DataRefImpl Rel,
13981438 SymbolRef &Result) const {
13991439 uint32_t symbolIdx;
14201460 return object_error::success;
14211461 }
14221462
1423 template
1424 error_code ELFObjectFile is64Bits>
1463 template is64Bits>
1464 error_code ELFObjectFile
14251465 ::getRelocationAddress(DataRefImpl Rel,
14261466 uint64_t &Result) const {
14271467 uint64_t offset;
14431483 return object_error::success;
14441484 }
14451485
1446 template
1447 error_code ELFObjectFile is64Bits>
1486 template is64Bits>
1487 error_code ELFObjectFile
14481488 ::getRelocationOffset(DataRefImpl Rel,
14491489 uint64_t &Result) const {
14501490 uint64_t offset;
14661506 return object_error::success;
14671507 }
14681508
1469 template
1470 error_code ELFObjectFile is64Bits>
1509 template is64Bits>
1510 error_code ELFObjectFile
14711511 ::getRelocationType(DataRefImpl Rel,
14721512 uint64_t &Result) const {
14731513 const Elf_Shdr *sec = getSection(Rel.w.b);
14891529 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
14901530 case ELF::enum: res = #enum; break;
14911531
1492 template
1493 error_code ELFObjectFile is64Bits>
1532 template is64Bits>
1533 error_code ELFObjectFile
14941534 ::getRelocationTypeName(DataRefImpl Rel,
14951535 SmallVectorImpl &Result) const {
14961536 const Elf_Shdr *sec = getSection(Rel.w.b);
18311871
18321872 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
18331873
1834 template
1835 error_code ELFObjectFile is64Bits>
1874 template is64Bits>
1875 error_code ELFObjectFile
18361876 ::getRelocationAdditionalInfo(DataRefImpl Rel,
18371877 int64_t &Result) const {
18381878 const Elf_Shdr *sec = getSection(Rel.w.b);
18501890 }
18511891 }
18521892
1853 template
1854 error_code ELFObjectFile is64Bits>
1893 template is64Bits>
1894 error_code ELFObjectFile
18551895 ::getRelocationValueString(DataRefImpl Rel,
18561896 SmallVectorImpl &Result) const {
18571897 const Elf_Shdr *sec = getSection(Rel.w.b);
19211961 }
19221962
19231963 // Verify that the last byte in the string table in a null.
1924 template
1925 void ELFObjectFile is64Bits>
1964 template is64Bits>
1965 void ELFObjectFile
19261966 ::VerifyStrTab(const Elf_Shdr *sh) const {
19271967 const char *strtab = (const char*)base() + sh->sh_offset;
19281968 if (strtab[sh->sh_size - 1] != 0)
19301970 report_fatal_error("String table must end with a null terminator!");
19311971 }
19321972
1933 template
1934 ELFObjectFile::ELFObjectFile(MemoryBuffer *Object
1935 , error_code &ec)
1973 template
1974 ELFObjectFile
1975 ::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
19361976 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
19371977 Object, ec)
19381978 , isDyldELFObject(false)
20922132 }
20932133
20942134 // Get the symbol table index in the symtab section given a symbol
2095 template
2096 uint64_t ELFObjectFile is64Bits>
2135 template is64Bits>
2136 uint64_t ELFObjectFile
20972137 ::getSymbolIndex(const Elf_Sym *Sym) const {
20982138 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
20992139 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
21062146 return SymOffset / SymTab->sh_entsize;
21072147 }
21082148
2109 template
2110 symbol_iterator ELFObjectFile is64Bits>
2149 template is64Bits>
2150 symbol_iterator ELFObjectFile
21112151 ::begin_symbols() const {
21122152 DataRefImpl SymbolData;
21132153 if (SymbolTableSections.size() <= 1) {
21202160 return symbol_iterator(SymbolRef(SymbolData, this));
21212161 }
21222162
2123 template
2124 symbol_iterator ELFObjectFile is64Bits>
2163 template is64Bits>
2164 symbol_iterator ELFObjectFile
21252165 ::end_symbols() const {
21262166 DataRefImpl SymbolData;
21272167 SymbolData.d.a = std::numeric_limits::max();
21292169 return symbol_iterator(SymbolRef(SymbolData, this));
21302170 }
21312171
2132 template
2133 symbol_iterator ELFObjectFile is64Bits>
2172 template is64Bits>
2173 symbol_iterator ELFObjectFile
21342174 ::begin_dynamic_symbols() const {
21352175 DataRefImpl SymbolData;
21362176 if (SymbolTableSections[0] == NULL) {
21432183 return symbol_iterator(SymbolRef(SymbolData, this));
21442184 }
21452185
2146 template
2147 symbol_iterator ELFObjectFile is64Bits>
2186 template is64Bits>
2187 symbol_iterator ELFObjectFile
21482188 ::end_dynamic_symbols() const {
21492189 DataRefImpl SymbolData;
21502190 SymbolData.d.a = std::numeric_limits::max();
21522192 return symbol_iterator(SymbolRef(SymbolData, this));
21532193 }
21542194
2155 template
2156 section_iterator ELFObjectFile is64Bits>
2195 template is64Bits>
2196 section_iterator ELFObjectFile
21572197 ::begin_sections() const {
21582198 DataRefImpl ret;
21592199 ret.p = reinterpret_cast(base() + Header->e_shoff);
21602200 return section_iterator(SectionRef(ret, this));
21612201 }
21622202
2163 template
2164 section_iterator ELFObjectFile is64Bits>
2203 template is64Bits>
2204 section_iterator ELFObjectFile
21652205 ::end_sections() const {
21662206 DataRefImpl ret;
21672207 ret.p = reinterpret_cast(base()
21702210 return section_iterator(SectionRef(ret, this));
21712211 }
21722212
2173 template
2174 typename ELFObjectFile::dyn_iterator
2175 ELFObjectFile::begin_dynamic_table() const {
2213 template
2214 typename ELFObjectFile::dyn_iterator
2215 ELFObjectFile
2216 ::begin_dynamic_table() const {
21762217 DataRefImpl DynData;
21772218 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
21782219 DynData.d.a = std::numeric_limits::max();
21822223 return dyn_iterator(DynRef(DynData, this));
21832224 }
21842225
2185 template
2186 typename ELFObjectFile::dyn_iterator
2187 ELFObjectFile is64Bits>
2226 template is64Bits>
2227 typename ELFObjectFile::dyn_iterator
2228 ELFObjectFile
21882229 ::end_dynamic_table() const {
21892230 DataRefImpl DynData;
21902231 DynData.d.a = std::numeric_limits::max();
21912232 return dyn_iterator(DynRef(DynData, this));
21922233 }
21932234
2194 template
2195 error_code ELFObjectFile is64Bits>
2235 template is64Bits>
2236 error_code ELFObjectFile
21962237 ::getDynNext(DataRefImpl DynData,
21972238 DynRef &Result) const {
21982239 ++DynData.d.a;
22072248 return object_error::success;
22082249 }
22092250
2210 template<support::endianness target_endianness, bool is64Bits>
2251 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
22112252 StringRef
2212 ELFObjectFileis64Bits>::getLoadName() const {
2253 ELFObjectFilemax_alignment, is64Bits>::getLoadName() const {
22132254 if (!dt_soname) {
22142255 // Find the DT_SONAME entry
22152256 dyn_iterator it = begin_dynamic_table();
22332274 return dt_soname;
22342275 }
22352276
2236 template
2237 library_iterator ELFObjectFile is64Bits>
2277 template is64Bits>
2278 library_iterator ELFObjectFile
22382279 ::begin_libraries_needed() const {
22392280 // Find the first DT_NEEDED entry
22402281 dyn_iterator i = begin_dynamic_table();
22512292 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
22522293 }
22532294
2254 template
2255 error_code ELFObjectFile is64Bits>
2295 template is64Bits>
2296 error_code ELFObjectFile
22562297 ::getLibraryNext(DataRefImpl Data,
22572298 LibraryRef &Result) const {
22582299 // Use the same DataRefImpl format as DynRef.
22802321 return object_error::success;
22812322 }
22822323
2283 template
2284 error_code ELFObjectFile is64Bits>
2324 template is64Bits>
2325 error_code ELFObjectFile
22852326 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
22862327 dyn_iterator i = dyn_iterator(DynRef(Data, this));
22872328 if (i == end_dynamic_table())
23012342 return object_error::success;
23022343 }
23032344
2304 template
2305 library_iterator ELFObjectFile is64Bits>
2345 template is64Bits>
2346 library_iterator ELFObjectFile
23062347 ::end_libraries_needed() const {
23072348 dyn_iterator e = end_dynamic_table();
23082349 // Use the same DataRefImpl format as DynRef.
23092350 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
23102351 }
23112352
2312 template
2313 uint8_t ELFObjectFile::getBytesInAddress() const {
2353 template
2354 uint8_t ELFObjectFile
2355 ::getBytesInAddress() const {
23142356 return is64Bits ? 8 : 4;
23152357 }
23162358
2317 template
2318 StringRef ELFObjectFile is64Bits>
2359 template is64Bits>
2360 StringRef ELFObjectFile
23192361 ::getFileFormatName() const {
23202362 switch(Header->e_ident[ELF::EI_CLASS]) {
23212363 case ELF::ELFCLASS32:
23482390 }
23492391 }
23502392
2351 template
2352 unsigned ELFObjectFile::getArch() const {
2393 template
2394 unsigned ELFObjectFile
2395 ::getArch() const {
23532396 switch(Header->e_machine) {
23542397 case ELF::EM_386:
23552398 return Triple::x86;
23692412 }
23702413 }
23712414
2372 template
2373 uint64_t ELFObjectFile::getNumSections() const {
2415 template
2416 uint64_t ELFObjectFile
2417 ::getNumSections() const {
23742418 assert(Header && "Header not initialized!");
23752419 if (Header->e_shnum == ELF::SHN_UNDEF) {
23762420 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
23792423 return Header->e_shnum;
23802424 }
23812425
2382 template<support::endianness target_endianness, bool is64Bits>
2426 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
23832427 uint64_t
2384 ELFObjectFileis64Bits>::getStringTableIndex() const {
2428 ELFObjectFilemax_alignment, is64Bits>
2429 ::getStringTableIndex() const {
23852430 if (Header->e_shnum == ELF::SHN_UNDEF) {
23862431 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
23872432 return SectionHeaderTable->sh_link;
23912436 return Header->e_shstrndx;
23922437 }
23932438
2394
2395 template<support::endianness target_endianness, bool is64Bits>
2439 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
23962440 template
23972441 inline const T *
2398 ELFObjectFile::getEntry(uint16_t Section,
2399 uint32_t Entry) const {
2442 ELFObjectFile
2443 ::getEntry(uint16_t Section, uint32_t Entry) const {
24002444 return getEntry(getSection(Section), Entry);
24012445 }
24022446
2403 template<support::endianness target_endianness, bool is64Bits>
2447 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
24042448 template
24052449 inline const T *
2406 ELFObjectFile::getEntry(const Elf_Shdr * Section,
2407 uint32_t Entry) const {
2450 ELFObjectFile
2451 ::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
24082452 return reinterpret_cast(
24092453 base()
24102454 + Section->sh_offset
24112455 + (Entry * Section->sh_entsize));
24122456 }
24132457
2414 template
2415 const typename ELFObjectFile::Elf_Sym *
2416 ELFObjectFile::getSymbol(DataRefImpl Symb) const {
2458 template
2459 const typename ELFObjectFile
2460 ::Elf_Sym *
2461 ELFObjectFile
2462 ::getSymbol(DataRefImpl Symb) const {
24172463 return getEntry(SymbolTableSections[Symb.d.b], Symb.d.a);
24182464 }
24192465
2420 template
2421 const typename ELFObjectFile::Elf_Dyn *
2422 ELFObjectFile::getDyn(DataRefImpl DynData) const {
2466 template
2467 const typename ELFObjectFile
2468 ::Elf_Dyn *
2469 ELFObjectFile
2470 ::getDyn(DataRefImpl DynData) const {
24232471 return getEntry(dot_dynamic_sec, DynData.d.a);
24242472 }
24252473
2426 template
2427 const typename ELFObjectFile::Elf_Rel *
2428 ELFObjectFile::getRel(DataRefImpl Rel) const {
2474 template
2475 const typename ELFObjectFile
2476 ::Elf_Rel *
2477 ELFObjectFile
2478 ::getRel(DataRefImpl Rel) const {
24292479 return getEntry(Rel.w.b, Rel.w.c);
24302480 }
24312481
2432 template
2433 const typename ELFObjectFile::Elf_Rela *
2434 ELFObjectFile::getRela(DataRefImpl Rela) const {
2482 template
2483 const typename ELFObjectFile
2484 ::Elf_Rela *
2485 ELFObjectFile
2486 ::getRela(DataRefImpl Rela) const {
24352487 return getEntry(Rela.w.b, Rela.w.c);
24362488 }
24372489
2438 template
2439 const typename ELFObjectFile::Elf_Shdr *
2440 ELFObjectFile::getSection(DataRefImpl Symb) const {
2490 template
2491 const typename ELFObjectFile
2492 ::Elf_Shdr *
2493 ELFObjectFile
2494 ::getSection(DataRefImpl Symb) const {
24412495 const Elf_Shdr *sec = getSection(Symb.d.b);
24422496 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
24432497 // FIXME: Proper error handling.
24452499 return sec;
24462500 }
24472501
2448 template
2449 const typename ELFObjectFile::Elf_Shdr *
2450 ELFObjectFile::getSection(uint32_t index) const {
2502 template
2503 const typename ELFObjectFile
2504 ::Elf_Shdr *
2505 ELFObjectFile
2506 ::getSection(uint32_t index) const {
24512507 if (index == 0)
24522508 return 0;
24532509 if (!SectionHeaderTable || index >= getNumSections())
24592515 + (index * Header->e_shentsize));
24602516 }
24612517
2462 template
2463 const char *ELFObjectFile is64Bits>
2518 template is64Bits>
2519 const char *ELFObjectFile
24642520 ::getString(uint32_t section,
24652521 ELF::Elf32_Word offset) const {
24662522 return getString(getSection(section), offset);
24672523 }
24682524
2469 template
2470 const char *ELFObjectFile is64Bits>
2525 template is64Bits>
2526 const char *ELFObjectFile
24712527 ::getString(const Elf_Shdr *section,
24722528 ELF::Elf32_Word offset) const {
24732529 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
24772533 return (const char *)base() + section->sh_offset + offset;
24782534 }
24792535
2480 template
2481 error_code ELFObjectFile is64Bits>
2536 template is64Bits>
2537 error_code ELFObjectFile
24822538 ::getSymbolName(const Elf_Shdr *section,
24832539 const Elf_Sym *symb,
24842540 StringRef &Result) const {
25012557 return object_error::success;
25022558 }
25032559
2504 template
2505 error_code ELFObjectFile is64Bits>
2560 template is64Bits>
2561 error_code ELFObjectFile
25062562 ::getSectionName(const Elf_Shdr *section,
25072563 StringRef &Result) const {
25082564 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
25092565 return object_error::success;
25102566 }
25112567
2512 template
2513 error_code ELFObjectFile is64Bits>
2568 template is64Bits>
2569 error_code ELFObjectFile
25142570 ::getSymbolVersion(const Elf_Shdr *section,
25152571 const Elf_Sym *symb,
25162572 StringRef &Version,
25922648 return object_error::success;
25932649 }
25942650
2595 template
2596 inline DynRefImpl is64Bits>
2651 template is64Bits>
2652 inline DynRefImpl
25972653 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
25982654 : DynPimpl(DynP)
25992655 , OwningObject(Owner) {}
26002656
2601 template
2602 inline bool DynRefImpl is64Bits>
2657 template is64Bits>
2658 inline bool DynRefImpl
26032659 ::operator==(const DynRefImpl &Other) const {
26042660 return DynPimpl == Other.DynPimpl;
26052661 }
26062662
2607 template
2608 inline bool DynRefImpl is64Bits>
2663 template is64Bits>
2664 inline bool DynRefImpl
26092665 ::operator <(const DynRefImpl &Other) const {
26102666 return DynPimpl < Other.DynPimpl;
26112667 }
26122668
2613 template
2614 inline error_code DynRefImpl is64Bits>
2669 template is64Bits>
2670 inline error_code DynRefImpl
26152671 ::getNext(DynRefImpl &Result) const {
26162672 return OwningObject->getDynNext(DynPimpl, Result);
26172673 }
26182674
2619 template
2620 inline int64_t DynRefImpl is64Bits>
2675 template is64Bits>
2676 inline int64_t DynRefImpl
26212677 ::getTag() const {
26222678 return OwningObject->getDyn(DynPimpl)->d_tag;
26232679 }
26242680
2625 template
2626 inline uint64_t DynRefImpl is64Bits>
2681 template is64Bits>
2682 inline uint64_t DynRefImpl
26272683 ::getVal() const {
26282684 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
26292685 }
26302686
2631 template
2632 inline uint64_t DynRefImpl is64Bits>
2687 template is64Bits>
2688 inline uint64_t DynRefImpl
26332689 ::getPtr() const {
26342690 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
26352691 }
26362692
2637 template
2638 inline DataRefImpl DynRefImpl is64Bits>
2693 template is64Bits>
2694 inline DataRefImpl DynRefImpl
26392695 ::getRawDataRefImpl() const {
26402696 return DynPimpl;
26412697 }
26472703 StringRef &Version,
26482704 bool &IsDefault) {
26492705 // Little-endian 32-bit
2650 if (const ELFObjectFile *ELFObj =
2651 dyn_cast >(Obj))
2706 if (const ELFObjectFile *ELFObj =
2707 dyn_cast >(Obj))
26522708 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
26532709
26542710 // Big-endian 32-bit
2655 if (const ELFObjectFile *ELFObj =
2656 dyn_cast >(Obj))
2711 if (const ELFObjectFile *ELFObj =
2712 dyn_cast >(Obj))
26572713 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
26582714
26592715 // Little-endian 64-bit
2660 if (const ELFObjectFile *ELFObj =
2661 dyn_cast >(Obj))
2716 if (const ELFObjectFile *ELFObj =
2717 dyn_cast >(Obj))
26622718 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
26632719
26642720 // Big-endian 64-bit
2665 if (const ELFObjectFile *ELFObj =
2666 dyn_cast >(Obj))
2721 if (const ELFObjectFile *ELFObj =
2722 dyn_cast >(Obj))
26672723 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
26682724
26692725 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
2727 using namespace llvm;
2828 using namespace llvm::object;
2929
30 using support::endianness;
31
3032 namespace {
3133
3234 static inline
3739 return Err;
3840 }
3941
40 template
41 class DyldELFObject : public ELFObjectFile {
42 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
43
44 typedef Elf_Shdr_Impl Elf_Shdr;
45 typedef Elf_Sym_Impl Elf_Sym;
46 typedef Elf_Rel_Impl Elf_Rel;
47 typedef Elf_Rel_Impl Elf_Rela;
48
49 typedef Elf_Ehdr_Impl Elf_Ehdr;
42 template
43 class DyldELFObject
44 : public ELFObjectFile {
45 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
46
47 typedef Elf_Shdr_Impl Elf_Shdr;
48 typedef Elf_Sym_Impl Elf_Sym;
49 typedef
50 Elf_Rel_Impl Elf_Rel;
51 typedef
52 Elf_Rel_Impl Elf_Rela;
53
54 typedef Elf_Ehdr_Impl Elf_Ehdr;
5055
5156 typedef typename ELFDataTypeTypedefHelper<
52 target_endianness, is64Bits>::value_type addr_type;
57 target_endianness, max_alignment, is64Bits>::value_type addr_type;
5358
5459 public:
5560 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
5964
6065 // Methods for type inquiry through isa, cast and dyn_cast
6166 static inline bool classof(const Binary *v) {
62 return (isa >(v)
63 && classof(cast >(v)));
67 return (isa >(v)
68 && classof(cast
69 >(v)));
6470 }
6571 static inline bool classof(
66 const ELFObjectFileis64Bits> *v) {
72 const ELFObjectFilemax_alignment, is64Bits> *v) {
6773 return v->isDyldType();
6874 }
6975 };
7076
71 template<support::endianness target_endianness, bool is64Bits>
77 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
7278 class ELFObjectImage : public ObjectImageCommon {
7379 protected:
74 DyldELFObjectis64Bits> *DyldObj;
80 DyldELFObjectmax_alignment, is64Bits> *DyldObj;
7581 bool Registered;
7682
7783 public:
7884 ELFObjectImage(ObjectBuffer *Input,
79 DyldELFObject, is64Bits> *Obj)
85 DyldELFObject, is64Bits> *Obj)
8086 : ObjectImageCommon(Input, Obj),
8187 DyldObj(Obj),
8288 Registered(false) {}
112118 // The MemoryBuffer passed into this constructor is just a wrapper around the
113119 // actual memory. Ultimately, the Binary parent class will take ownership of
114120 // this MemoryBuffer object but not the underlying memory.
115 template
116 DyldELFObject::DyldELFObject(MemoryBuffer *Wrapper,
117 error_code &ec)
118 : ELFObjectFile(Wrapper, ec) {
121 template
122 DyldELFObject
123 ::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
124 : ELFObjectFile(Wrapper, ec) {
119125 this->isDyldELFObject = true;
120126 }
121127
122 template
123 void DyldELFObject::updateSectionAddress(
124 const SectionRef &Sec,
125 uint64_t Addr) {
128 template
129 void DyldELFObject
130 ::updateSectionAddress(const SectionRef &Sec, uint64_t Addr) {
126131 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
127132 Elf_Shdr *shdr = const_cast(
128133 reinterpret_cast(ShdrRef.p));
132137 shdr->sh_addr = static_cast(Addr);
133138 }
134139
135 template
136 void DyldELFObject::updateSymbolAddress(
137 const SymbolRef &SymRef,
138 uint64_t Addr) {
140 template
141 void DyldELFObject
142 ::updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr){
139143
140144 Elf_Sym *sym = const_cast(
141 ELFObjectFile::
142 getSymbol(SymRef.getRawDataRefImpl()));
145 ELFObjectFile
146 ::getSymbol(SymRef.getRawDataRefImpl()));
143147
144148 // This assumes the address passed in matches the target address bitness
145149 // The template-based type cast handles everything else.
147151 }
148152
149153 } // namespace
150
151154
152155 namespace llvm {
153156
160163 error_code ec;
161164
162165 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
163 DyldELFObject *Obj =
164 new DyldELFObject(Buffer->getMemBuffer(), ec);
165 return new ELFObjectImage(Buffer, Obj);
166 DyldELFObject *Obj =
167 new DyldELFObject(Buffer->getMemBuffer(), ec);
168 return new ELFObjectImage(Buffer, Obj);
166169 }
167170 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
168 DyldELFObject *Obj =
169 new DyldELFObject(Buffer->getMemBuffer(), ec);
170 return new ELFObjectImage(Buffer, Obj);
171 DyldELFObject *Obj =
172 new DyldELFObject(Buffer->getMemBuffer(), ec);
173 return new ELFObjectImage(Buffer, Obj);
171174 }
172175 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
173 DyldELFObject *Obj =
174 new DyldELFObject(Buffer->getMemBuffer(), ec);
175 return new ELFObjectImage(Buffer, Obj);
176 DyldELFObject *Obj =
177 new DyldELFObject(Buffer->getMemBuffer(), ec);
178 return new ELFObjectImage(Buffer, Obj);
176179 }
177180 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
178 DyldELFObject *Obj =
179 new DyldELFObject(Buffer->getMemBuffer(), ec);
180 return new ELFObjectImage(Buffer, Obj);
181 DyldELFObject *Obj =
182 new DyldELFObject(Buffer->getMemBuffer(), ec);
183 return new ELFObjectImage(Buffer, Obj);
181184 }
182185 else
183186 llvm_unreachable("Unexpected ELF format");
206209 case ELF::R_X86_64_32S: {
207210 Value += Addend;
208211 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
209 (Type == ELF::R_X86_64_32S &&
212 (Type == ELF::R_X86_64_32S &&
210213 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
211214 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
212215 uint32_t *Target = reinterpret_cast(Section.Address + Offset);
287290 default:
288291 llvm_unreachable("Not implemented relocation type!");
289292
290 // Write a 32bit value to relocation address, taking into account the
293 // Write a 32bit value to relocation address, taking into account the
291294 // implicit addend encoded in the target.
292295 case ELF::R_ARM_TARGET1 :
293296 case ELF::R_ARM_ABS32 :
298301 // Last 4 bit should be shifted.
299302 case ELF::R_ARM_MOVW_ABS_NC :
300303 // We are not expecting any other addend in the relocation address.
301 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
304 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
302305 // non-contiguous fields.
303306 assert((*TargetPtr & 0x000F0FFF) == 0);
304307 Value = Value & 0xFFFF;
548551 } break;
549552 }
550553 }
551
552554
553555 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
554556 uint64_t Offset,
629631 // Default to 'true' in case isText fails (though it never does).
630632 bool isCode = true;
631633 si->isText(isCode);
632 Value.SectionID = findOrEmitSection(Obj,
633 (*si),
634 isCode,
634 Value.SectionID = findOrEmitSection(Obj,
635 (*si),
636 isCode,
635637 ObjSectionToID);
636638 Value.Addend = Addend;
637639 break;
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Object/ELF.h"
14 #include "llvm/Support/MathExtras.h"
1415
1516 namespace llvm {
1617
2122 std::pair Ident = getElfArchType(Object);
2223 error_code ec;
2324
25 std::size_t MaxAlignment =
26 1ULL << CountTrailingZeros_64(uintptr_t(Object->getBufferStart()));
27
2428 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
25 return new ELFObjectFile(Object, ec);
29 if (MaxAlignment >= 4)
30 return new ELFObjectFile(Object, ec);
31 else if (MaxAlignment >= 2)
32 return new ELFObjectFile(Object, ec);
33 else
34 llvm_unreachable("Invalid alignment for ELF file!");
2635 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
27 return new ELFObjectFile(Object, ec);
36 if (MaxAlignment >= 4)
37 return new ELFObjectFile(Object, ec);
38 else if (MaxAlignment >= 2)
39 return new ELFObjectFile(Object, ec);
40 else
41 llvm_unreachable("Invalid alignment for ELF file!");
2842 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
29 return new ELFObjectFile(Object, ec);
43 if (MaxAlignment >= 8)
44 return new ELFObjectFile(Object, ec);
45 else if (MaxAlignment >= 2)
46 return new ELFObjectFile(Object, ec);
47 else
48 llvm_unreachable("Invalid alignment for ELF file!");
3049 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
31 ELFObjectFile *result =
32 new ELFObjectFile(Object, ec);
33 return result;
50 if (MaxAlignment >= 8)
51 return new ELFObjectFile(Object, ec);
52 else if (MaxAlignment >= 2)
53 return new ELFObjectFile(Object, ec);
54 else
55 llvm_unreachable("Invalid alignment for ELF file!");
3456 }
3557
3658 report_fatal_error("Buffer is not an ELF object file!");
+0
-40
test/Object/archive-long-index.donttest less more
None #
1 # Check if the index is appearing properly in the output file
2 #
3 RUN: llvm-nm -s %p/Inputs/liblong_filenames.a | FileCheck -check-prefix=CHECKIDX %s
4
5 CHECKIDX: Archive map
6 CHECKIDX: abcdefghijklmnopqrstuvwxyz12345678 in 1.o
7 CHECKIDX: main in 1.o
8 CHECKIDX: fn1 in 2.o
9 CHECKIDX: fn3 in 3.o
10 CHECKIDX: fn1 in 3.o
11 CHECKIDX: shankar in 4.o
12 CHECKIDX: a in 5.o
13 CHECKIDX: b in 6.o
14 CHECKIDX: a in abcdefghijklmnopqrstuvwxyz1.o
15 CHECKIDX: b in abcdefghijklmnopqrstuvwxyz2.o
16 CHECKIDX: bda in abcdefghijklmnopqrstuvwxyz2.o
17 CHECKIDX: b in abcdefghijklmnopq.o
18 CHECKIDX: 1.o:
19 CHECKIDX: 00000000 D abcdefghijklmnopqrstuvwxyz12345678
20 CHECKIDX: U bda
21 CHECKIDX: 00000000 T main
22 CHECKIDX: 2.o:
23 CHECKIDX: 00000000 T fn1
24 CHECKIDX: 3.o:
25 CHECKIDX: 0000000b T fn1
26 CHECKIDX: 00000000 T fn3
27 CHECKIDX: 4.o:
28 CHECKIDX: C shankar
29 CHECKIDX: 5.o:
30 CHECKIDX: C a
31 CHECKIDX: 6.o:
32 CHECKIDX: C b
33 CHECKIDX: abcdefghijklmnopqrstuvwxyz1.o:
34 CHECKIDX: C a
35 CHECKIDX: abcdefghijklmnopqrstuvwxyz2.o:
36 CHECKIDX: C b
37 CHECKIDX: 00000000 T bda
38 CHECKIDX: abcdefghijklmnopq.o:
39 CHECKIDX: C b
0 #
1 # Check if the index is appearing properly in the output file
2 #
3 RUN: llvm-nm -s %p/Inputs/liblong_filenames.a | FileCheck -check-prefix=CHECKIDX %s
4
5 CHECKIDX: Archive map
6 CHECKIDX: abcdefghijklmnopqrstuvwxyz12345678 in 1.o
7 CHECKIDX: main in 1.o
8 CHECKIDX: fn1 in 2.o
9 CHECKIDX: fn3 in 3.o
10 CHECKIDX: fn1 in 3.o
11 CHECKIDX: shankar in 4.o
12 CHECKIDX: a in 5.o
13 CHECKIDX: b in 6.o
14 CHECKIDX: a in abcdefghijklmnopqrstuvwxyz1.o
15 CHECKIDX: b in abcdefghijklmnopqrstuvwxyz2.o
16 CHECKIDX: bda in abcdefghijklmnopqrstuvwxyz2.o
17 CHECKIDX: b in abcdefghijklmnopq.o
18 CHECKIDX: 1.o:
19 CHECKIDX: 00000000 D abcdefghijklmnopqrstuvwxyz12345678
20 CHECKIDX: U bda
21 CHECKIDX: 00000000 T main
22 CHECKIDX: 2.o:
23 CHECKIDX: 00000000 T fn1
24 CHECKIDX: 3.o:
25 CHECKIDX: 0000000b T fn1
26 CHECKIDX: 00000000 T fn3
27 CHECKIDX: 4.o:
28 CHECKIDX: C shankar
29 CHECKIDX: 5.o:
30 CHECKIDX: C a
31 CHECKIDX: 6.o:
32 CHECKIDX: C b
33 CHECKIDX: abcdefghijklmnopqrstuvwxyz1.o:
34 CHECKIDX: C a
35 CHECKIDX: abcdefghijklmnopqrstuvwxyz2.o:
36 CHECKIDX: C b
37 CHECKIDX: 00000000 T bda
38 CHECKIDX: abcdefghijklmnopq.o:
39 CHECKIDX: C b
+0
-12
test/Object/simple-archive.donttest less more
None #
1 # Check if the index is appearing properly in the output file
2 #
3 RUN: llvm-nm -s %p/Inputs/libsimple_archive.a | FileCheck -check-prefix=CHECKIDX %s
4
5 CHECKIDX: Archive map
6 CHECKIDX: abcdefghijklmnopqrstuvwxyz12345678 in 1.o
7 CHECKIDX: main in 1.o
8 CHECKIDX: 1.o:
9 CHECKIDX: 00000000 D abcdefghijklmnopqrstuvwxyz12345678
10 CHECKIDX: U fn1
11 CHECKIDX: 00000000 T main
0 #
1 # Check if the index is appearing properly in the output file
2 #
3 RUN: llvm-nm -s %p/Inputs/libsimple_archive.a | FileCheck -check-prefix=CHECKIDX %s
4
5 CHECKIDX: Archive map
6 CHECKIDX: abcdefghijklmnopqrstuvwxyz12345678 in 1.o
7 CHECKIDX: main in 1.o
8 CHECKIDX: 1.o:
9 CHECKIDX: 00000000 D abcdefghijklmnopqrstuvwxyz12345678
10 CHECKIDX: U fn1
11 CHECKIDX: 00000000 T main