std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
uint64_t &Result) const {
COFFSymbolRef Symb = getCOFFSymbol(Ref);
- const coff_section *Section = nullptr;
- if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
- return EC;
- if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED)
+ if (Symb.isAnyUndefined()) {
+ Result = UnknownAddressOrSize;
+ return object_error::success;
+ }
+ if (Symb.isCommon()) {
Result = UnknownAddressOrSize;
- else if (Section)
+ return object_error::success;
+ }
+ int32_t SectionNumber = Symb.getSectionNumber();
+ if (!COFF::isReservedSectionNumber(SectionNumber)) {
+ const coff_section *Section = nullptr;
+ if (std::error_code EC = getSection(SectionNumber, Section))
+ return EC;
+
Result = Section->VirtualAddress + Symb.getValue();
- else
- Result = Symb.getValue();
+ return object_error::success;
+ }
+
+ Result = Symb.getValue();
return object_error::success;
}
std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
SymbolRef::Type &Result) const {
COFFSymbolRef Symb = getCOFFSymbol(Ref);
+ int32_t SectionNumber = Symb.getSectionNumber();
Result = SymbolRef::ST_Other;
- if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
- Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
+ if (Symb.isAnyUndefined()) {
Result = SymbolRef::ST_Unknown;
} else if (Symb.isFunctionDefinition()) {
Result = SymbolRef::ST_Function;
- } else {
- uint32_t Characteristics = 0;
- if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
- const coff_section *Section = nullptr;
- if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
- return EC;
- Characteristics = Section->Characteristics;
- }
- if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
- ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
+ } else if (Symb.isCommon()) {
+ Result = SymbolRef::ST_Data;
+ } else if (Symb.isFileRecord()) {
+ Result = SymbolRef::ST_File;
+ } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG) {
+ Result = SymbolRef::ST_Debug;
+ } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
+ const coff_section *Section = nullptr;
+ if (std::error_code EC = getSection(SectionNumber, Section))
+ return EC;
+ uint32_t Characteristics = Section->Characteristics;
+ if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
+ Result = SymbolRef::ST_Function;
+ else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
+ COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Result = SymbolRef::ST_Data;
}
return object_error::success;
COFFSymbolRef Symb = getCOFFSymbol(Ref);
uint32_t Result = SymbolRef::SF_None;
- // TODO: Correctly set SF_FormatSpecific, SF_Common
-
- if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
- if (Symb.getValue() == 0)
- Result |= SymbolRef::SF_Undefined;
- else
- Result |= SymbolRef::SF_Common;
- }
-
-
- // TODO: This are certainly too restrictive.
- if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL)
+ if (Symb.isExternal() || Symb.isWeakExternal())
Result |= SymbolRef::SF_Global;
- if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
+ if (Symb.isWeakExternal())
Result |= SymbolRef::SF_Weak;
if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
Result |= SymbolRef::SF_Absolute;
+ if (Symb.isFileRecord())
+ Result |= SymbolRef::SF_FormatSpecific;
+
+ if (Symb.isSectionDefinition())
+ Result |= SymbolRef::SF_FormatSpecific;
+
+ if (Symb.isCommon())
+ Result |= SymbolRef::SF_Common;
+
+ if (Symb.isAnyUndefined())
+ Result |= SymbolRef::SF_Undefined;
+
return Result;
}
std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
uint64_t &Result) 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.
COFFSymbolRef Symb = getCOFFSymbol(Ref);
- const coff_section *Section = nullptr;
- if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
- return EC;
- if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
- if (Symb.getValue() == 0)
- Result = UnknownAddressOrSize;
- else
- Result = Symb.getValue();
- } else if (Section) {
+ if (Symb.isAnyUndefined()) {
+ Result = UnknownAddressOrSize;
+ return object_error::success;
+ }
+ if (Symb.isCommon()) {
+ Result = Symb.getValue();
+ return object_error::success;
+ }
+
+ // Let's attempt to get the size of the symbol by looking at the address of
+ // the symbol after the symbol in question.
+ uint64_t SymbAddr;
+ if (std::error_code EC = getSymbolAddress(Ref, SymbAddr))
+ return EC;
+ int32_t SectionNumber = Symb.getSectionNumber();
+ if (COFF::isReservedSectionNumber(SectionNumber)) {
+ // Absolute and debug symbols aren't sorted in any interesting way.
+ Result = 0;
+ return object_error::success;
+ }
+ const section_iterator SecEnd = section_end();
+ uint64_t AfterAddr = UnknownAddressOrSize;
+ for (const symbol_iterator &SymbI : symbols()) {
+ section_iterator SecI = SecEnd;
+ if (std::error_code EC = SymbI->getSection(SecI))
+ return EC;
+ // Check the symbol's section, skip it if it's in the wrong section.
+ // First, make sure it is in any section.
+ if (SecI == SecEnd)
+ continue;
+ // Second, make sure it is in the same section as the symbol in question.
+ if (!sectionContainsSymbol(SecI->getRawDataRefImpl(), Ref))
+ continue;
+ uint64_t Addr;
+ if (std::error_code EC = SymbI->getAddress(Addr))
+ return EC;
+ // We want to compare our symbol in question with the closest possible
+ // symbol that comes after.
+ if (AfterAddr > Addr && Addr > SymbAddr)
+ AfterAddr = Addr;
+ }
+ if (AfterAddr == UnknownAddressOrSize) {
+ // No symbol comes after this one, assume that everything after our symbol
+ // is part of it.
+ const coff_section *Section = nullptr;
+ if (std::error_code EC = getSection(SectionNumber, Section))
+ return EC;
Result = Section->SizeOfRawData - Symb.getValue();
} else {
- Result = 0;
+ // Take the difference between our symbol and the symbol that comes after
+ // our symbol.
+ Result = AfterAddr - SymbAddr;
}
return object_error::success;
}
uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
- const coff_section *Sec = toSec(Ref);
- return Sec->SizeOfRawData;
+ return getSectionSize(toSec(Ref));
}
std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
bool HasPEHeader = false;
// Check if this is a PE/COFF file.
- if (base()[0] == 0x4d && base()[1] == 0x5a) {
+ if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
// PE/COFF, seek through MS-DOS compatibility stub and 4-byte
// PE signature to find 'normal' COFF header.
- if (!checkSize(Data, EC, 0x3c + 8))
- return;
- CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
- // Check the PE magic bytes. ("PE\0\0")
- if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
- 0) {
- EC = object_error::parse_failed;
- return;
+ const auto *DH = reinterpret_cast<const dos_header *>(base());
+ if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
+ CurPtr = DH->AddressOfNewExeHeader;
+ // Check the PE magic bytes. ("PE\0\0")
+ if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
+ EC = object_error::parse_failed;
+ return;
+ }
+ CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
+ HasPEHeader = true;
}
- CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
- HasPEHeader = true;
}
if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
const uint8_t *DataDirAddr;
uint64_t DataDirSize;
- if (Header->Magic == 0x10b) {
+ if (Header->Magic == COFF::PE32Header::PE32) {
PE32Header = Header;
DataDirAddr = base() + CurPtr + sizeof(pe32_header);
DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
- } else if (Header->Magic == 0x20b) {
+ } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
}
}
+iterator_range<import_directory_iterator>
+COFFObjectFile::import_directories() const {
+ return make_range(import_directory_begin(), import_directory_end());
+}
+
+iterator_range<delay_import_directory_iterator>
+COFFObjectFile::delay_import_directories() const {
+ return make_range(delay_import_directory_begin(),
+ delay_import_directory_end());
+}
+
+iterator_range<export_directory_iterator>
+COFFObjectFile::export_directories() const {
+ return make_range(export_directory_begin(), export_directory_end());
+}
+
std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Res = PE32Header;
return object_error::success;
return object_error::success;
}
+uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
+ // SizeOfRawData and VirtualSize change what they represent depending on
+ // whether or not we have an executable image.
+ //
+ // For object files, SizeOfRawData contains the size of section's data;
+ // VirtualSize is always zero.
+ //
+ // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
+ // actual section size is in VirtualSize. It is possible for VirtualSize to
+ // be greater than SizeOfRawData; the contents past that point should be
+ // considered to be zero.
+ uint32_t SectionSize;
+ if (Sec->VirtualSize)
+ SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
+ else
+ SectionSize = Sec->SizeOfRawData;
+
+ return SectionSize;
+}
+
std::error_code
COFFObjectFile::getSectionContents(const coff_section *Sec,
ArrayRef<uint8_t> &Res) const {
- // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
- // do anything interesting for them.
+ // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
+ // don't do anything interesting for them.
assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
"BSS sections don't have contents!");
// The only thing that we need to verify is that the contents is contained
// within the file bounds. We don't need to make sure it doesn't cover other
// data, as there's nothing that says that is not allowed.
uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
- uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
+ uint32_t SectionSize = getSectionSize(Sec);
+ uintptr_t ConEnd = ConStart + SectionSize;
if (ConEnd > uintptr_t(Data.getBufferEnd()))
return object_error::parse_failed;
- Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
- Sec->SizeOfRawData);
+ Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
return object_error::success;
}
OwningObject);
}
+iterator_range<imported_symbol_iterator>
+ImportDirectoryEntryRef::imported_symbols() const {
+ return make_range(imported_symbol_begin(), imported_symbol_end());
+}
+
std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
uintptr_t IntPtr = 0;
if (std::error_code EC =
OwningObject);
}
+iterator_range<imported_symbol_iterator>
+DelayImportDirectoryEntryRef::imported_symbols() const {
+ return make_range(imported_symbol_begin(), imported_symbol_end());
+}
+
std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
uintptr_t IntPtr = 0;
if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))