From 533c207f9d2da6d913c4b10f6f757fe9d6367b10 Mon Sep 17 00:00:00 2001 From: David Srbecky Date: Wed, 22 Apr 2015 12:20:22 +0100 Subject: Simplify template parameters of Elf classes. The ELF specification defines several types which differ between 32-bit ELF and 64-bit ELF. We used to template all ELF-related methods on all of those types which was very verbose. This CL wraps all the types as typedefs in ElfTypes32 and ElfTypes64. One of those wrappers is then used as the template parameter. Change-Id: I65247c2c79d92a7c4799e988cf3e4a1b10eb4788 --- compiler/elf_builder.h | 121 ++++++++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 51 deletions(-) (limited to 'compiler/elf_builder.h') diff --git a/compiler/elf_builder.h b/compiler/elf_builder.h index 323c933ab1..b67dd26f71 100644 --- a/compiler/elf_builder.h +++ b/compiler/elf_builder.h @@ -26,11 +26,14 @@ namespace art { -template +template class ElfSectionBuilder : public ValueObject { public: + using Elf_Word = typename ElfTypes::Word; + using Elf_Shdr = typename ElfTypes::Shdr; + ElfSectionBuilder(const std::string& sec_name, Elf_Word type, Elf_Word flags, - const ElfSectionBuilder *link, Elf_Word info, + const ElfSectionBuilder *link, Elf_Word info, Elf_Word align, Elf_Word entsize) : section_index_(0), name_(sec_name), link_(link) { memset(§ion_, 0, sizeof(section_)); @@ -75,9 +78,14 @@ class ElfSectionBuilder : public ValueObject { const ElfSectionBuilder* const link_; }; -template -class ElfDynamicBuilder FINAL : public ElfSectionBuilder { +template +class ElfDynamicBuilder FINAL : public ElfSectionBuilder { public: + using Elf_Word = typename ElfTypes::Word; + using Elf_Sword = typename ElfTypes::Sword; + using Elf_Shdr = typename ElfTypes::Shdr; + using Elf_Dyn = typename ElfTypes::Dyn; + void AddDynamicTag(Elf_Sword tag, Elf_Word d_un) { if (tag == DT_NULL) { return; @@ -86,7 +94,7 @@ class ElfDynamicBuilder FINAL : public ElfSectionBuilder* section) { + const ElfSectionBuilder* section) { if (tag == DT_NULL) { return; } @@ -94,9 +102,9 @@ class ElfDynamicBuilder FINAL : public ElfSectionBuilder *link) - : ElfSectionBuilder(sec_name, SHT_DYNAMIC, SHF_ALLOC | SHF_ALLOC, - link, 0, kPageSize, sizeof(Elf_Dyn)) {} + ElfSectionBuilder *link) + : ElfSectionBuilder(sec_name, SHT_DYNAMIC, SHF_ALLOC | SHF_ALLOC, + link, 0, kPageSize, sizeof(Elf_Dyn)) {} ~ElfDynamicBuilder() {} Elf_Word GetSize() const { @@ -129,21 +137,22 @@ class ElfDynamicBuilder FINAL : public ElfSectionBuilder* section_; + const ElfSectionBuilder* section_; Elf_Sword tag_; Elf_Word off_; }; std::vector dynamics_; }; -template -class ElfRawSectionBuilder FINAL : public ElfSectionBuilder { +template +class ElfRawSectionBuilder FINAL : public ElfSectionBuilder { public: + using Elf_Word = typename ElfTypes::Word; + ElfRawSectionBuilder(const std::string& sec_name, Elf_Word type, Elf_Word flags, - const ElfSectionBuilder* link, Elf_Word info, + const ElfSectionBuilder* link, Elf_Word info, Elf_Word align, Elf_Word entsize) - : ElfSectionBuilder(sec_name, type, flags, link, info, align, - entsize) { + : ElfSectionBuilder(sec_name, type, flags, link, info, align, entsize) { } ElfRawSectionBuilder(const ElfRawSectionBuilder&) = default; @@ -161,13 +170,14 @@ class ElfRawSectionBuilder FINAL : public ElfSectionBuilder buf_; }; -template -class ElfOatSectionBuilder FINAL : public ElfSectionBuilder { +template +class ElfOatSectionBuilder FINAL : public ElfSectionBuilder { public: + using Elf_Word = typename ElfTypes::Word; + ElfOatSectionBuilder(const std::string& sec_name, Elf_Word size, Elf_Word offset, Elf_Word type, Elf_Word flags) - : ElfSectionBuilder(sec_name, type, flags, nullptr, 0, kPageSize, - 0), + : ElfSectionBuilder(sec_name, type, flags, nullptr, 0, kPageSize, 0), offset_(offset), size_(size) { } @@ -206,14 +216,17 @@ static inline unsigned elfhash(const char *_name) { return h; } -template -class ElfSymtabBuilder FINAL : public ElfSectionBuilder { +template +class ElfSymtabBuilder FINAL : public ElfSectionBuilder { public: + using Elf_Addr = typename ElfTypes::Addr; + using Elf_Word = typename ElfTypes::Word; + using Elf_Sym = typename ElfTypes::Sym; + // Add a symbol with given name to this symtab. The symbol refers to // 'relative_addr' within the given section and has the given attributes. void AddSymbol(const std::string& name, - const ElfSectionBuilder* section, + const ElfSectionBuilder* section, Elf_Addr addr, bool is_relative, Elf_Word size, @@ -228,14 +241,14 @@ class ElfSymtabBuilder FINAL : public ElfSectionBuilder(sec_name, type, ((alloc) ? SHF_ALLOC : 0U), - &strtab_, 0, sizeof(Elf_Word), - sizeof(Elf_Sym)), str_name_(str_name), - str_type_(str_type), - strtab_(str_name, - str_type, - ((alloc) ? SHF_ALLOC : 0U), - nullptr, 0, 1, 1) { + : ElfSectionBuilder(sec_name, type, ((alloc) ? SHF_ALLOC : 0U), + &strtab_, 0, sizeof(Elf_Word), + sizeof(Elf_Sym)), str_name_(str_name), + str_type_(str_type), + strtab_(str_name, + str_type, + ((alloc) ? SHF_ALLOC : 0U), + nullptr, 0, 1, 1) { } ~ElfSymtabBuilder() {} @@ -365,14 +378,14 @@ class ElfSymtabBuilder FINAL : public ElfSectionBuilder* GetStrTab() { + ElfSectionBuilder* GetStrTab() { return &strtab_; } private: struct ElfSymbolState { const std::string name_; - const ElfSectionBuilder* section_; + const ElfSectionBuilder* section_; Elf_Addr addr_; Elf_Word size_; bool is_relative_; @@ -387,7 +400,7 @@ class ElfSymtabBuilder FINAL : public ElfSectionBuilder symbols_; - ElfSectionBuilder strtab_; + ElfSectionBuilder strtab_; }; template @@ -529,10 +542,18 @@ static inline constexpr Elf_Word NextOffset(const Elf_Shdr& cur, const Elf_Shdr& return RoundUp(prev.sh_size + prev.sh_offset, cur.sh_addralign); } -template +template class ElfBuilder FINAL { public: + using Elf_Addr = typename ElfTypes::Addr; + using Elf_Word = typename ElfTypes::Word; + using Elf_Sword = typename ElfTypes::Sword; + using Elf_Ehdr = typename ElfTypes::Ehdr; + using Elf_Shdr = typename ElfTypes::Shdr; + using Elf_Sym = typename ElfTypes::Sym; + using Elf_Phdr = typename ElfTypes::Phdr; + using Elf_Dyn = typename ElfTypes::Dyn; + ElfBuilder(CodeOutput* oat_writer, File* elf_file, InstructionSet isa, @@ -565,11 +586,11 @@ class ElfBuilder FINAL { } ~ElfBuilder() {} - const ElfOatSectionBuilder& GetTextBuilder() const { + const ElfOatSectionBuilder& GetTextBuilder() const { return text_builder_; } - ElfSymtabBuilder* GetSymtabBuilder() { + ElfSymtabBuilder* GetSymtabBuilder() { return &symtab_builder_; } @@ -1181,12 +1202,11 @@ class ElfBuilder FINAL { } // Adds the given raw section to the builder. It does not take ownership. - void RegisterRawSection(ElfRawSectionBuilder* bld) { + void RegisterRawSection(ElfRawSectionBuilder* bld) { other_builders_.push_back(bld); } - const ElfRawSectionBuilder* - FindRawSection(const char* name) { + const ElfRawSectionBuilder* FindRawSection(const char* name) { for (const auto* other_builder : other_builders_) { if (other_builder->GetName() == name) { return other_builder; @@ -1304,8 +1324,7 @@ class ElfBuilder FINAL { } } - void AssignSectionStr(ElfSectionBuilder* builder, - std::string* strtab) { + void AssignSectionStr(ElfSectionBuilder* builder, std::string* strtab) { builder->GetSection()->sh_name = strtab->size(); *strtab += builder->GetName(); *strtab += '\0'; @@ -1363,15 +1382,15 @@ class ElfBuilder FINAL { std::vector section_ptrs_; std::vector hash_; - ElfOatSectionBuilder text_builder_; - ElfOatSectionBuilder rodata_builder_; - ElfOatSectionBuilder bss_builder_; - ElfSymtabBuilder dynsym_builder_; - ElfSymtabBuilder symtab_builder_; - ElfSectionBuilder hash_builder_; - ElfDynamicBuilder dynamic_builder_; - ElfSectionBuilder shstrtab_builder_; - std::vector*> other_builders_; + ElfOatSectionBuilder text_builder_; + ElfOatSectionBuilder rodata_builder_; + ElfOatSectionBuilder bss_builder_; + ElfSymtabBuilder dynsym_builder_; + ElfSymtabBuilder symtab_builder_; + ElfSectionBuilder hash_builder_; + ElfDynamicBuilder dynamic_builder_; + ElfSectionBuilder shstrtab_builder_; + std::vector*> other_builders_; DISALLOW_COPY_AND_ASSIGN(ElfBuilder); }; -- cgit v1.2.3-59-g8ed1b