diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/BinaryFormat/Magic.cpp | 3 | ||||
-rw-r--r-- | lib/Object/WindowsResource.cpp | 24 |
2 files changed, 17 insertions, 10 deletions
diff --git a/lib/BinaryFormat/Magic.cpp b/lib/BinaryFormat/Magic.cpp index b19a07a9066..5d0a71fc780 100644 --- a/lib/BinaryFormat/Magic.cpp +++ b/lib/BinaryFormat/Magic.cpp @@ -51,8 +51,7 @@ file_magic llvm::identify_magic(StringRef Magic) { return file_magic::coff_import_library; } // Windows resource file - if (Magic.size() >= sizeof(COFF::WinResMagic) && - memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0) + if (startswith(Magic, "\0\0\0\0\x20\0\0\0\xFF")) return file_magic::windows_resource; // 0x0000 = COFF unknown machine type if (Magic[1] == 0) diff --git a/lib/Object/WindowsResource.cpp b/lib/Object/WindowsResource.cpp index 1371eacdf8f..f675a8389e6 100644 --- a/lib/Object/WindowsResource.cpp +++ b/lib/Object/WindowsResource.cpp @@ -36,19 +36,23 @@ const uint32_t MIN_HEADER_SIZE = 7 * sizeof(uint32_t) + 2 * sizeof(uint16_t); // 8-byte because it makes everyone happy. const uint32_t SECTION_ALIGNMENT = sizeof(uint64_t); +static const size_t ResourceMagicSize = 16; + +static const size_t NullEntrySize = 16; + uint32_t WindowsResourceParser::TreeNode::StringCount = 0; uint32_t WindowsResourceParser::TreeNode::DataCount = 0; WindowsResource::WindowsResource(MemoryBufferRef Source) : Binary(Binary::ID_WinRes, Source) { - size_t LeadingSize = WIN_RES_MAGIC_SIZE + WIN_RES_NULL_ENTRY_SIZE; + size_t LeadingSize = ResourceMagicSize + NullEntrySize; BBS = BinaryByteStream(Data.getBuffer().drop_front(LeadingSize), support::little); } Expected<std::unique_ptr<WindowsResource>> WindowsResource::createWindowsResource(MemoryBufferRef Source) { - if (Source.getBufferSize() < WIN_RES_MAGIC_SIZE + WIN_RES_NULL_ENTRY_SIZE) + if (Source.getBufferSize() < ResourceMagicSize + NullEntrySize) return make_error<GenericBinaryError>( "File too small to be a resource file", object_error::invalid_file_type); @@ -101,10 +105,12 @@ static Error readStringOrId(BinaryStreamReader &Reader, uint16_t &ID, } Error ResourceEntryRef::loadNext() { - const WinResHeaderPrefix *Prefix; - RETURN_IF_ERROR(Reader.readObject(Prefix)); + uint32_t DataSize; + RETURN_IF_ERROR(Reader.readInteger(DataSize)); + uint32_t HeaderSize; + RETURN_IF_ERROR(Reader.readInteger(HeaderSize)); - if (Prefix->HeaderSize < MIN_HEADER_SIZE) + if (HeaderSize < MIN_HEADER_SIZE) return make_error<GenericBinaryError>("Header size is too small.", object_error::parse_failed); @@ -112,13 +118,13 @@ Error ResourceEntryRef::loadNext() { RETURN_IF_ERROR(readStringOrId(Reader, NameID, Name, IsStringName)); - RETURN_IF_ERROR(Reader.padToAlignment(WIN_RES_HEADER_ALIGNMENT)); + RETURN_IF_ERROR(Reader.padToAlignment(sizeof(uint32_t))); RETURN_IF_ERROR(Reader.readObject(Suffix)); - RETURN_IF_ERROR(Reader.readArray(Data, Prefix->DataSize)); + RETURN_IF_ERROR(Reader.readArray(Data, DataSize)); - RETURN_IF_ERROR(Reader.padToAlignment(WIN_RES_DATA_ALIGNMENT)); + RETURN_IF_ERROR(Reader.padToAlignment(sizeof(uint32_t))); return Error::success(); } @@ -462,6 +468,8 @@ void WindowsResourceCOFFWriter::writeFirstSectionHeader() { SectionOneHeader->PointerToLinenumbers = 0; SectionOneHeader->NumberOfRelocations = Data.size(); SectionOneHeader->NumberOfLinenumbers = 0; + SectionOneHeader->Characteristics = COFF::IMAGE_SCN_ALIGN_1BYTES; + SectionOneHeader->Characteristics += COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; SectionOneHeader->Characteristics += COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; SectionOneHeader->Characteristics += COFF::IMAGE_SCN_MEM_READ; } |