/* * Copyright 2011, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ELF_TYPES_H #define ELF_TYPES_H #include "utils/traits.h" #include #include // ELF structure forward declarations template class ELFHeader; template class ELFObject; template class ELFProgramHeader; template class ELFReloc; template class ELFRelocRel; // For TypeTraits template class ELFRelocRela; // For TypeTraits template class ELFSection; template class ELFSectionBits; template class ELFSectionHeader; template class ELFSectionHeaderTable; template class ELFSectionNoBits; template class ELFSectionProgBits; template class ELFSectionRelTable; template class ELFSectionStrTab; template class ELFSectionSymTab; template class ELFSymbol; // Note: Following TypeTraits specialization MUST be compliant to the // System V Application Binary Interface, Chap 4, Data Representation. TYPE_TRAITS_SPECIALIZE(ELFHeader<32> , 52, 4) TYPE_TRAITS_SPECIALIZE(ELFHeader<64> , 64, 8) TYPE_TRAITS_SPECIALIZE(ELFProgramHeader<32> , 32, 4) TYPE_TRAITS_SPECIALIZE(ELFProgramHeader<64> , 56, 8) TYPE_TRAITS_SPECIALIZE(ELFSectionHeader<32> , 40, 4) TYPE_TRAITS_SPECIALIZE(ELFSectionHeader<64> , 64, 8) TYPE_TRAITS_SPECIALIZE(ELFSymbol<32> , 16, 4) TYPE_TRAITS_SPECIALIZE(ELFSymbol<64> , 24, 8) TYPE_TRAITS_SPECIALIZE(ELFRelocRel<32> , 8, 4) TYPE_TRAITS_SPECIALIZE(ELFRelocRel<64> , 16, 8) TYPE_TRAITS_SPECIALIZE(ELFRelocRela<32> , 12, 4) TYPE_TRAITS_SPECIALIZE(ELFRelocRela<64> , 24, 8) // ELF primitive type wrappers namespace detail { #define ELF_TYPE_WRAPPER(TYPE, IMPL) \ struct TYPE { \ IMPL value; \ \ TYPE() : value(0) { } \ TYPE(IMPL val) : value(val) { } \ \ TYPE &operator=(TYPE const &with) { value = with.value; return *this; } \ TYPE &operator=(IMPL val) { value = val; return *this; } \ \ operator IMPL() const { return value; } \ }; ELF_TYPE_WRAPPER(ELFHalf , uint16_t) ELF_TYPE_WRAPPER(ELFWord , uint32_t) ELF_TYPE_WRAPPER(ELFSword , int32_t) ELF_TYPE_WRAPPER(ELFXword , uint64_t) ELF_TYPE_WRAPPER(ELFSxword , int64_t) ELF_TYPE_WRAPPER(ELF32Address , uint32_t) ELF_TYPE_WRAPPER(ELF32Offset , uint32_t) ELF_TYPE_WRAPPER(ELF64Address , uint64_t) ELF_TYPE_WRAPPER(ELF64Offset , uint64_t) #undef ELF_TYPE_WRAPPER extern llvm::raw_ostream &operator<<(llvm::raw_ostream &, ELF32Address const &); extern llvm::raw_ostream &operator<<(llvm::raw_ostream &, ELF32Offset const &); extern llvm::raw_ostream &operator<<(llvm::raw_ostream &, ELF64Address const &); extern llvm::raw_ostream &operator<<(llvm::raw_ostream &, ELF64Offset const &); } // Note: Following TypeTraits specialization MUST be compliant to the // System V Application Binary Interface, Chap 4, Data Representation. TYPE_TRAITS_SPECIALIZE(detail::ELFHalf , 2, 2) TYPE_TRAITS_SPECIALIZE(detail::ELFWord , 4, 4) TYPE_TRAITS_SPECIALIZE(detail::ELFSword , 4, 4) TYPE_TRAITS_SPECIALIZE(detail::ELFXword , 8, 8) TYPE_TRAITS_SPECIALIZE(detail::ELFSxword , 8, 8) TYPE_TRAITS_SPECIALIZE(detail::ELF32Address , 4, 4) TYPE_TRAITS_SPECIALIZE(detail::ELF32Offset , 4, 4) TYPE_TRAITS_SPECIALIZE(detail::ELF64Address , 8, 8) TYPE_TRAITS_SPECIALIZE(detail::ELF64Offset , 8, 8) template struct ELFPrimitiveTypes; template <> struct ELFPrimitiveTypes<32> { typedef detail::ELF32Address address; typedef detail::ELF32Offset offset; typedef unsigned char byte; typedef detail::ELFHalf half; typedef detail::ELFWord word; typedef detail::ELFSword sword; typedef detail::ELFWord relinfo; typedef detail::ELFSword addend; typedef detail::ELFWord symsize; // Note: Don't use these types. They are not in the specification of // ELF 32. However, we need these typedefs to define the type introduce // macro. typedef void xword; typedef void sxword; }; template <> struct ELFPrimitiveTypes<64> { typedef detail::ELF64Address address; typedef detail::ELF64Offset offset; typedef unsigned char byte; typedef detail::ELFHalf half; typedef detail::ELFWord word; typedef detail::ELFSword sword; typedef detail::ELFXword xword; typedef detail::ELFSxword sxword; typedef detail::ELFXword relinfo; typedef detail::ELFSxword addend; typedef detail::ELFXword symsize; }; // Macros to introduce these ELF types to a specific scope #define ELF_STRUCT_TYPE_INTRO_TO_SCOPE(BITWIDTH) \ typedef ELFHeader ELFHeaderTy; \ typedef ELFObject ELFObjectTy; \ typedef ELFProgramHeader ELFProgramHeaderTy; \ typedef ELFReloc ELFRelocTy; \ typedef ELFRelocRel ELFRelocRelTy; \ typedef ELFRelocRela ELFRelocRelaTy; \ typedef ELFSection ELFSectionTy; \ typedef ELFSectionBits ELFSectionBitsTy; \ typedef ELFSectionHeader ELFSectionHeaderTy; \ typedef ELFSectionHeaderTable ELFSectionHeaderTableTy; \ typedef ELFSectionNoBits ELFSectionNoBitsTy; \ typedef ELFSectionProgBits ELFSectionProgBitsTy; \ typedef ELFSectionRelTable ELFSectionRelTableTy; \ typedef ELFSectionStrTab ELFSectionStrTabTy; \ typedef ELFSectionSymTab ELFSectionSymTabTy; \ typedef ELFSymbol ELFSymbolTy; #define ELF_TYPE_INTRO_TO_TEMPLATE_SCOPE(BITWIDTH) \ /* ELF structures */ \ ELF_STRUCT_TYPE_INTRO_TO_SCOPE(BITWIDTH) \ \ /* ELF primitives */ \ typedef typename ELFPrimitiveTypes::address addr_t; \ typedef typename ELFPrimitiveTypes::offset offset_t; \ typedef typename ELFPrimitiveTypes::byte byte_t; \ typedef typename ELFPrimitiveTypes::half half_t; \ typedef typename ELFPrimitiveTypes::word word_t; \ typedef typename ELFPrimitiveTypes::sword sword_t; \ typedef typename ELFPrimitiveTypes::xword xword_t; \ typedef typename ELFPrimitiveTypes::sxword sxword_t; \ typedef typename ELFPrimitiveTypes::relinfo relinfo_t; \ typedef typename ELFPrimitiveTypes::addend addend_t; \ typedef typename ELFPrimitiveTypes::symsize symsize_t; #define ELF_TYPE_INTRO_TO_SCOPE(BITWIDTH) \ /* ELF structures */ \ ELF_STRUCT_TYPE_INTRO_TO_SCOPE(BITWIDTH) \ \ /* ELF primitives */ \ typedef ELFPrimitiveTypes::address addr_t; \ typedef ELFPrimitiveTypes::offset offset_t; \ typedef ELFPrimitiveTypes::byte byte_t; \ typedef ELFPrimitiveTypes::half half_t; \ typedef ELFPrimitiveTypes::word word_t; \ typedef ELFPrimitiveTypes::sword sword_t; \ typedef ELFPrimitiveTypes::xword xword_t; \ typedef ELFPrimitiveTypes::sxword sxword_t; \ typedef ELFPrimitiveTypes::relinfo relinfo_t; \ typedef ELFPrimitiveTypes::addend addend_t; \ typedef ELFPrimitiveTypes::symsize symsize_t; #endif // ELF_TYPES_H