From 7acdb4d237181976b04e72f6a6c329c3b2604440 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 21 Jan 2011 02:27:02 +0000 Subject: [PATCH] Object: Fix type punned pointer issues by making DataRefImpl a union and using intptr_t. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123962 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ObjectFile.h | 15 ++++- lib/Object/COFFObjectFile.cpp | 51 +++++++++------- lib/Object/ELFObjectFile.cpp | 100 +++++++++++++------------------ 3 files changed, 82 insertions(+), 84 deletions(-) diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index b47dc69ede1..eee9d447cdd 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -16,6 +16,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/DataTypes.h" +#include namespace llvm { @@ -25,7 +26,19 @@ class StringRef; namespace object { class ObjectFile; -typedef uint64_t DataRefImpl; + +union DataRefImpl { + struct { + uint32_t a, b; + } d; + intptr_t p; +}; + +static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) { + // Check bitwise identical. This is the only legal way to compare a union w/o + // knowing which member is in use. + return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0; +} /// SymbolRef - This is a value type class that represents a single symbol in /// the list of symbols in the object file. diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index c564d6a030d..cfee82a0b21 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -128,13 +128,14 @@ public: } // end namespace SymbolRef COFFObjectFile::getSymbolNext(DataRefImpl Symb) const { - const coff_symbol *symb = *reinterpret_cast(&Symb); + const coff_symbol *symb = reinterpret_cast(Symb.p); symb += 1 + symb->NumberOfAuxSymbols; - return SymbolRef(DataRefImpl(symb), this); + Symb.p = reinterpret_cast(symb); + return SymbolRef(Symb, this); } StringRef COFFObjectFile::getSymbolName(DataRefImpl Symb) const { - const coff_symbol *symb = *reinterpret_cast(&Symb); + const coff_symbol *symb = reinterpret_cast(Symb.p); // Check for string table entry. First 4 bytes are 0. if (symb->Name.Offset.Zeroes == 0) { uint32_t Offset = symb->Name.Offset.Offset; @@ -149,7 +150,7 @@ StringRef COFFObjectFile::getSymbolName(DataRefImpl Symb) const { } uint64_t COFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { - const coff_symbol *symb = *reinterpret_cast(&Symb); + const coff_symbol *symb = reinterpret_cast(Symb.p); const coff_section *Section = getSection(symb->SectionNumber); char Type = getSymbolNMTypeChar(Symb); if (Type == 'U' || Type == 'w') @@ -163,7 +164,7 @@ uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Symb) const { // FIXME: Return the correct size. This requires looking at all the symbols // in the same section as this symbol, and looking for either the next // symbol, or the end of the section. - const coff_symbol *symb = *reinterpret_cast(&Symb); + const coff_symbol *symb = reinterpret_cast(Symb.p); const coff_section *Section = getSection(symb->SectionNumber); char Type = getSymbolNMTypeChar(Symb); if (Type == 'U' || Type == 'w') @@ -174,7 +175,7 @@ uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Symb) const { } char COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb) const { - const coff_symbol *symb = *reinterpret_cast(&Symb); + const coff_symbol *symb = reinterpret_cast(Symb.p); char ret = StringSwitch(getSymbolName(Symb)) .StartsWith(".debug", 'N') .StartsWith(".sxdata", 'N') @@ -236,13 +237,14 @@ bool COFFObjectFile::isSymbolInternal(DataRefImpl Symb) const { } SectionRef COFFObjectFile::getSectionNext(DataRefImpl Sec) const { - const coff_section *sec = *reinterpret_cast(&Sec); + const coff_section *sec = reinterpret_cast(Sec.p); sec += 1; - return SectionRef(DataRefImpl(sec), this); + Sec.p = reinterpret_cast(sec); + return SectionRef(Sec, this); } StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const { - const coff_section *sec = *reinterpret_cast(&Sec); + const coff_section *sec = reinterpret_cast(Sec.p); StringRef name; if (sec->Name[7] == 0) // Null terminated, let ::strlen figure out the length. @@ -263,23 +265,23 @@ StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const { } uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Sec) const { - const coff_section *sec = *reinterpret_cast(&Sec); + const coff_section *sec = reinterpret_cast(Sec.p); return sec->VirtualAddress; } uint64_t COFFObjectFile::getSectionSize(DataRefImpl Sec) const { - const coff_section *sec = *reinterpret_cast(&Sec); + const coff_section *sec = reinterpret_cast(Sec.p); return sec->SizeOfRawData; } StringRef COFFObjectFile::getSectionContents(DataRefImpl Sec) const { - const coff_section *sec = *reinterpret_cast(&Sec); + const coff_section *sec = reinterpret_cast(Sec.p); return StringRef(reinterpret_cast(base + sec->PointerToRawData), sec->SizeOfRawData); } bool COFFObjectFile::isSectionText(DataRefImpl Sec) const { - const coff_section *sec = *reinterpret_cast(&Sec); + const coff_section *sec = reinterpret_cast(Sec.p); return sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; } @@ -300,29 +302,32 @@ COFFObjectFile::COFFObjectFile(MemoryBuffer *Object) } ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const { - return symbol_iterator( - SymbolRef(DataRefImpl(SymbolTable), this)); + DataRefImpl ret; + ret.p = reinterpret_cast(SymbolTable); + return symbol_iterator(SymbolRef(ret, this)); } ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const { // The symbol table ends where the string table begins. - return symbol_iterator( - SymbolRef(DataRefImpl(StringTable), this)); + DataRefImpl ret; + ret.p = reinterpret_cast(StringTable); + return symbol_iterator(SymbolRef(ret, this)); } ObjectFile::section_iterator COFFObjectFile::begin_sections() const { - return section_iterator( - SectionRef(DataRefImpl(SectionTable), this)); + DataRefImpl ret; + ret.p = reinterpret_cast(SectionTable); + return section_iterator(SectionRef(ret, this)); } ObjectFile::section_iterator COFFObjectFile::end_sections() const { - return section_iterator( - SectionRef( - DataRefImpl((void *)(SectionTable + Header->NumberOfSections)), this)); + DataRefImpl ret; + ret.p = reinterpret_cast(SectionTable + Header->NumberOfSections); + return section_iterator(SectionRef(ret, this)); } uint8_t COFFObjectFile::getBytesInAddress() const { - return 4; + return getArch() == Triple::x86_64 ? 8 : 4; } StringRef COFFObjectFile::getFileFormatName() const { diff --git a/lib/Object/ELFObjectFile.cpp b/lib/Object/ELFObjectFile.cpp index 8a232d5214f..682be770f48 100644 --- a/lib/Object/ELFObjectFile.cpp +++ b/lib/Object/ELFObjectFile.cpp @@ -128,8 +128,7 @@ struct Elf_Shdr_Impl : Elf_Shdr_Base { unsigned getEntityCount() const { if (sh_entsize == 0) return 0; - else - return sh_size / sh_entsize; + return sh_size / sh_entsize; } }; } @@ -176,14 +175,6 @@ struct Elf_Sym_Impl : Elf_Sym_Base { }; } -namespace { -struct ELFDataRefImpl { - uint32_t SymbolIndex; - uint16_t SymbolTableSectionIndex; - uint16_t Unused; -}; -} - namespace { template class ELFObjectFile : public ObjectFile { @@ -261,10 +252,8 @@ public: template void ELFObjectFile ::validateSymbol(DataRefImpl Symb) const { - const ELFDataRefImpl SymbolData = *reinterpret_cast(&Symb); const Elf_Sym *symb = getSymbol(Symb); - const Elf_Shdr *SymbolTableSection = - SymbolTableSections[SymbolData.SymbolTableSectionIndex]; + const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; // FIXME: We really need to do proper error handling in the case of an invalid // input file. Because we don't use exceptions, I think we'll just pass // an error object around. @@ -283,20 +272,18 @@ template SymbolRef ELFObjectFile ::getSymbolNext(DataRefImpl Symb) const { validateSymbol(Symb); - ELFDataRefImpl &SymbolData = *reinterpret_cast(&Symb); - const Elf_Shdr *SymbolTableSection = - SymbolTableSections[SymbolData.SymbolTableSectionIndex]; + const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; - ++SymbolData.SymbolIndex; + ++Symb.d.a; // Check to see if we are at the end of this symbol table. - if (SymbolData.SymbolIndex >= SymbolTableSection->getEntityCount()) { + if (Symb.d.a >= SymbolTableSection->getEntityCount()) { // We are at the end. If there are other symbol tables, jump to them. - ++SymbolData.SymbolTableSectionIndex; - SymbolData.SymbolIndex = 1; // The 0th symbol in ELF is fake. + ++Symb.d.b; + Symb.d.a = 1; // The 0th symbol in ELF is fake. // Otherwise return the terminator. - if (SymbolData.SymbolTableSectionIndex >= SymbolTableSections.size()) { - SymbolData.SymbolIndex = std::numeric_limits::max(); - SymbolData.SymbolTableSectionIndex = std::numeric_limits::max(); + if (Symb.d.b >= SymbolTableSections.size()) { + Symb.d.a = std::numeric_limits::max(); + Symb.d.b = std::numeric_limits::max(); } } @@ -426,40 +413,37 @@ bool ELFObjectFile template SectionRef ELFObjectFile ::getSectionNext(DataRefImpl Sec) const { - const uint8_t *sec = *reinterpret_cast(&Sec); + const uint8_t *sec = reinterpret_cast(Sec.p); sec += Header->e_shentsize; - return SectionRef(DataRefImpl(sec), this); + Sec.p = reinterpret_cast(sec); + return SectionRef(Sec, this); } template StringRef ELFObjectFile ::getSectionName(DataRefImpl Sec) const { - const Elf_Shdr *sec = - *reinterpret_cast(&Sec); + const Elf_Shdr *sec = reinterpret_cast(Sec.p); return StringRef(getString(dot_shstrtab_sec, sec->sh_name)); } template uint64_t ELFObjectFile ::getSectionAddress(DataRefImpl Sec) const { - const Elf_Shdr *sec = - *reinterpret_cast(&Sec); + const Elf_Shdr *sec = reinterpret_cast(Sec.p); return sec->sh_addr; } template uint64_t ELFObjectFile ::getSectionSize(DataRefImpl Sec) const { - const Elf_Shdr *sec = - *reinterpret_cast(&Sec); + const Elf_Shdr *sec = reinterpret_cast(Sec.p); return sec->sh_size; } template StringRef ELFObjectFile ::getSectionContents(DataRefImpl Sec) const { - const Elf_Shdr *sec = - *reinterpret_cast(&Sec); + const Elf_Shdr *sec = reinterpret_cast(Sec.p); const char *start = (char*)base + sec->sh_offset; return StringRef(start, sec->sh_size); } @@ -467,8 +451,7 @@ StringRef ELFObjectFile template bool ELFObjectFile ::isSectionText(DataRefImpl Sec) const { - const Elf_Shdr *sec = - *reinterpret_cast(&Sec); + const Elf_Shdr *sec = reinterpret_cast(Sec.p); if (sec->sh_flags & ELF::SHF_EXECINSTR) return true; return false; @@ -538,49 +521,49 @@ ELFObjectFile::ELFObjectFile(MemoryBuffer *Object) template ObjectFile::symbol_iterator ELFObjectFile ::begin_symbols() const { - ELFDataRefImpl SymbolData; + DataRefImpl SymbolData; memset(&SymbolData, 0, sizeof(SymbolData)); if (SymbolTableSections.size() == 0) { - SymbolData.SymbolIndex = std::numeric_limits::max(); - SymbolData.SymbolTableSectionIndex = std::numeric_limits::max(); + SymbolData.d.a = std::numeric_limits::max(); + SymbolData.d.b = std::numeric_limits::max(); } else { - SymbolData.SymbolIndex = 1; // The 0th symbol in ELF is fake. - SymbolData.SymbolTableSectionIndex = 0; + SymbolData.d.a = 1; // The 0th symbol in ELF is fake. + SymbolData.d.b = 0; } - return symbol_iterator( - SymbolRef(DataRefImpl(*reinterpret_cast(&SymbolData)), this)); + return symbol_iterator(SymbolRef(SymbolData, this)); } template ObjectFile::symbol_iterator ELFObjectFile ::end_symbols() const { - ELFDataRefImpl SymbolData; + DataRefImpl SymbolData; memset(&SymbolData, 0, sizeof(SymbolData)); - SymbolData.SymbolIndex = std::numeric_limits::max(); - SymbolData.SymbolTableSectionIndex = std::numeric_limits::max(); - return symbol_iterator( - SymbolRef(DataRefImpl(*reinterpret_cast(&SymbolData)), this)); + SymbolData.d.a = std::numeric_limits::max(); + SymbolData.d.b = std::numeric_limits::max(); + return symbol_iterator(SymbolRef(SymbolData, this)); } template ObjectFile::section_iterator ELFObjectFile ::begin_sections() const { - return section_iterator( - SectionRef(DataRefImpl(base + Header->e_shoff), this)); + DataRefImpl ret; + ret.p = reinterpret_cast(base + Header->e_shoff); + return section_iterator(SectionRef(ret, this)); } template ObjectFile::section_iterator ELFObjectFile ::end_sections() const { - return section_iterator( - SectionRef(DataRefImpl(base - + Header->e_shoff - + (Header->e_shentsize * Header->e_shnum)), this)); + DataRefImpl ret; + ret.p = reinterpret_cast(base + + Header->e_shoff + + (Header->e_shentsize * Header->e_shnum)); + return section_iterator(SectionRef(ret, this)); } template uint8_t ELFObjectFile::getBytesInAddress() const { - return 4; + return is64Bits ? 8 : 4; } template @@ -626,20 +609,17 @@ unsigned ELFObjectFile::getArch() const { template const typename ELFObjectFile::Elf_Sym * ELFObjectFile::getSymbol(DataRefImpl Symb) const { - const ELFDataRefImpl SymbolData = *reinterpret_cast(&Symb); - const Elf_Shdr *sec = - SymbolTableSections[SymbolData.SymbolTableSectionIndex]; + const Elf_Shdr *sec = SymbolTableSections[Symb.d.b]; return reinterpret_cast( base + sec->sh_offset - + (SymbolData.SymbolIndex * sec->sh_entsize)); + + (Symb.d.a * sec->sh_entsize)); } template const typename ELFObjectFile::Elf_Shdr * ELFObjectFile::getSection(DataRefImpl Symb) const { - const ELFDataRefImpl SymbolData = *reinterpret_cast(&Symb); - const Elf_Shdr *sec = getSection(SymbolData.SymbolTableSectionIndex); + const Elf_Shdr *sec = getSection(Symb.d.b); if (sec->sh_type != ELF::SHT_SYMTAB) // FIXME: Proper error handling. report_fatal_error("Invalid symbol table section!"); -- 2.34.1