2017-05-20 03:49:19 +02:00
|
|
|
//===-- WindowsResource.cpp -------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-20 03:49:19 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the .res file class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/WindowsResource.h"
|
2017-06-09 19:34:30 +02:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2017-12-18 23:10:14 +01:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2017-06-09 19:34:30 +02:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2019-04-25 01:26:30 +02:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2017-06-09 19:34:30 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <queue>
|
2017-05-20 03:49:19 +02:00
|
|
|
#include <system_error>
|
|
|
|
|
2017-06-16 23:13:24 +02:00
|
|
|
using namespace llvm;
|
2017-06-17 00:00:42 +02:00
|
|
|
using namespace object;
|
2017-06-16 23:13:24 +02:00
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
|
|
|
|
2017-05-30 20:19:06 +02:00
|
|
|
#define RETURN_IF_ERROR(X) \
|
|
|
|
if (auto EC = X) \
|
2017-05-30 21:36:58 +02:00
|
|
|
return EC;
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2019-08-30 08:56:33 +02:00
|
|
|
#define UNWRAP_REF_OR_RETURN(Name, Expr) \
|
|
|
|
auto Name##OrErr = Expr; \
|
|
|
|
if (!Name##OrErr) \
|
|
|
|
return Name##OrErr.takeError(); \
|
|
|
|
const auto &Name = *Name##OrErr;
|
|
|
|
|
|
|
|
#define UNWRAP_OR_RETURN(Name, Expr) \
|
|
|
|
auto Name##OrErr = Expr; \
|
|
|
|
if (!Name##OrErr) \
|
|
|
|
return Name##OrErr.takeError(); \
|
|
|
|
auto Name = *Name##OrErr;
|
|
|
|
|
2017-05-30 20:19:06 +02:00
|
|
|
const uint32_t MIN_HEADER_SIZE = 7 * sizeof(uint32_t) + 2 * sizeof(uint16_t);
|
|
|
|
|
2017-06-13 02:19:43 +02:00
|
|
|
// COFF files seem to be inconsistent with alignment between sections, just use
|
|
|
|
// 8-byte because it makes everyone happy.
|
|
|
|
const uint32_t SECTION_ALIGNMENT = sizeof(uint64_t);
|
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
WindowsResource::WindowsResource(MemoryBufferRef Source)
|
|
|
|
: Binary(Binary::ID_WinRes, Source) {
|
2017-07-08 05:06:10 +02:00
|
|
|
size_t LeadingSize = WIN_RES_MAGIC_SIZE + WIN_RES_NULL_ENTRY_SIZE;
|
2017-05-20 03:49:19 +02:00
|
|
|
BBS = BinaryByteStream(Data.getBuffer().drop_front(LeadingSize),
|
|
|
|
support::little);
|
|
|
|
}
|
|
|
|
|
2019-05-02 03:52:24 +02:00
|
|
|
// static
|
2017-05-20 03:49:19 +02:00
|
|
|
Expected<std::unique_ptr<WindowsResource>>
|
|
|
|
WindowsResource::createWindowsResource(MemoryBufferRef Source) {
|
2017-07-08 05:06:10 +02:00
|
|
|
if (Source.getBufferSize() < WIN_RES_MAGIC_SIZE + WIN_RES_NULL_ENTRY_SIZE)
|
2017-05-20 03:49:19 +02:00
|
|
|
return make_error<GenericBinaryError>(
|
2019-05-02 03:52:24 +02:00
|
|
|
Source.getBufferIdentifier() + ": too small to be a resource file",
|
2017-05-20 03:49:19 +02:00
|
|
|
object_error::invalid_file_type);
|
|
|
|
std::unique_ptr<WindowsResource> Ret(new WindowsResource(Source));
|
2020-02-10 16:06:45 +01:00
|
|
|
return std::move(Ret);
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Expected<ResourceEntryRef> WindowsResource::getHeadEntry() {
|
2017-08-24 04:36:50 +02:00
|
|
|
if (BBS.getLength() < sizeof(WinResHeaderPrefix) + sizeof(WinResHeaderSuffix))
|
2019-05-02 03:52:24 +02:00
|
|
|
return make_error<EmptyResError>(getFileName() + " contains no entries",
|
2017-08-24 04:36:50 +02:00
|
|
|
object_error::unexpected_eof);
|
|
|
|
return ResourceEntryRef::create(BinaryStreamRef(BBS), this);
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ResourceEntryRef::ResourceEntryRef(BinaryStreamRef Ref,
|
2017-08-24 04:36:50 +02:00
|
|
|
const WindowsResource *Owner)
|
2019-05-02 03:52:24 +02:00
|
|
|
: Reader(Ref), Owner(Owner) {}
|
2017-08-24 04:36:50 +02:00
|
|
|
|
|
|
|
Expected<ResourceEntryRef>
|
|
|
|
ResourceEntryRef::create(BinaryStreamRef BSR, const WindowsResource *Owner) {
|
|
|
|
auto Ref = ResourceEntryRef(BSR, Owner);
|
|
|
|
if (auto E = Ref.loadNext())
|
2020-02-10 16:06:45 +01:00
|
|
|
return std::move(E);
|
2017-08-24 04:36:50 +02:00
|
|
|
return Ref;
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Error ResourceEntryRef::moveNext(bool &End) {
|
|
|
|
// Reached end of all the entries.
|
|
|
|
if (Reader.bytesRemaining() == 0) {
|
|
|
|
End = true;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
RETURN_IF_ERROR(loadNext());
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:19:06 +02:00
|
|
|
static Error readStringOrId(BinaryStreamReader &Reader, uint16_t &ID,
|
|
|
|
ArrayRef<UTF16> &Str, bool &IsString) {
|
|
|
|
uint16_t IDFlag;
|
|
|
|
RETURN_IF_ERROR(Reader.readInteger(IDFlag));
|
|
|
|
IsString = IDFlag != 0xffff;
|
|
|
|
|
|
|
|
if (IsString) {
|
|
|
|
Reader.setOffset(
|
|
|
|
Reader.getOffset() -
|
|
|
|
sizeof(uint16_t)); // Re-read the bytes which we used to check the flag.
|
|
|
|
RETURN_IF_ERROR(Reader.readWideString(Str));
|
|
|
|
} else
|
|
|
|
RETURN_IF_ERROR(Reader.readInteger(ID));
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
Error ResourceEntryRef::loadNext() {
|
2017-07-08 05:06:10 +02:00
|
|
|
const WinResHeaderPrefix *Prefix;
|
|
|
|
RETURN_IF_ERROR(Reader.readObject(Prefix));
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2017-07-08 05:06:10 +02:00
|
|
|
if (Prefix->HeaderSize < MIN_HEADER_SIZE)
|
2019-05-02 03:52:24 +02:00
|
|
|
return make_error<GenericBinaryError>(Owner->getFileName() +
|
|
|
|
": header size too small",
|
2017-05-30 20:19:06 +02:00
|
|
|
object_error::parse_failed);
|
|
|
|
|
|
|
|
RETURN_IF_ERROR(readStringOrId(Reader, TypeID, Type, IsStringType));
|
|
|
|
|
|
|
|
RETURN_IF_ERROR(readStringOrId(Reader, NameID, Name, IsStringName));
|
|
|
|
|
2017-07-08 05:06:10 +02:00
|
|
|
RETURN_IF_ERROR(Reader.padToAlignment(WIN_RES_HEADER_ALIGNMENT));
|
2017-05-30 20:19:06 +02:00
|
|
|
|
|
|
|
RETURN_IF_ERROR(Reader.readObject(Suffix));
|
|
|
|
|
2017-07-08 05:06:10 +02:00
|
|
|
RETURN_IF_ERROR(Reader.readArray(Data, Prefix->DataSize));
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2017-07-08 05:06:10 +02:00
|
|
|
RETURN_IF_ERROR(Reader.padToAlignment(WIN_RES_DATA_ALIGNMENT));
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
[LLD] [COFF] Implement MinGW default manifest handling
In mingw environments, resources are normally compiled to resource
object files directly, instead of letting the linker convert them to
COFF format.
Since some time, GCC supports the notion of a default manifest object.
When invoking the linker, GCC looks for the default manifest object
file, and if found in the expected path, it is added to linker commands.
The default manifest is one that indicates support for the latest known
versions of windows, to implicitly unlock the modern behaviours of certain
APIs.
Not all mingw/gcc distributions include this file, but e.g. in msys2,
the default manifest object is distributed in a separate package (which
can be but might not always be installed).
This means that even if user projects only use one single resource
object file, the linker can end up with two resource object files,
and thus needs to support merging them.
The default manifest has a language id of zero, and GNU ld has got
logic for dropping a manifest with a zero language id, if there's
another manifest present with a nonzero language id. If there are
multiple manifests with a nonzero language id, the merging process
errors out.
Differential Revision: https://reviews.llvm.org/D66825
llvm-svn: 370974
2019-09-04 22:34:00 +02:00
|
|
|
WindowsResourceParser::WindowsResourceParser(bool MinGW)
|
|
|
|
: Root(false), MinGW(MinGW) {}
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2019-04-25 01:26:30 +02:00
|
|
|
void printResourceTypeName(uint16_t TypeID, raw_ostream &OS) {
|
|
|
|
switch (TypeID) {
|
|
|
|
case 1: OS << "CURSOR (ID 1)"; break;
|
|
|
|
case 2: OS << "BITMAP (ID 2)"; break;
|
|
|
|
case 3: OS << "ICON (ID 3)"; break;
|
|
|
|
case 4: OS << "MENU (ID 4)"; break;
|
|
|
|
case 5: OS << "DIALOG (ID 5)"; break;
|
|
|
|
case 6: OS << "STRINGTABLE (ID 6)"; break;
|
|
|
|
case 7: OS << "FONTDIR (ID 7)"; break;
|
|
|
|
case 8: OS << "FONT (ID 8)"; break;
|
|
|
|
case 9: OS << "ACCELERATOR (ID 9)"; break;
|
|
|
|
case 10: OS << "RCDATA (ID 10)"; break;
|
|
|
|
case 11: OS << "MESSAGETABLE (ID 11)"; break;
|
|
|
|
case 12: OS << "GROUP_CURSOR (ID 12)"; break;
|
|
|
|
case 14: OS << "GROUP_ICON (ID 14)"; break;
|
|
|
|
case 16: OS << "VERSIONINFO (ID 16)"; break;
|
|
|
|
case 17: OS << "DLGINCLUDE (ID 17)"; break;
|
|
|
|
case 19: OS << "PLUGPLAY (ID 19)"; break;
|
|
|
|
case 20: OS << "VXD (ID 20)"; break;
|
|
|
|
case 21: OS << "ANICURSOR (ID 21)"; break;
|
|
|
|
case 22: OS << "ANIICON (ID 22)"; break;
|
|
|
|
case 23: OS << "HTML (ID 23)"; break;
|
|
|
|
case 24: OS << "MANIFEST (ID 24)"; break;
|
|
|
|
default: OS << "ID " << TypeID; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 02:51:41 +02:00
|
|
|
static bool convertUTF16LEToUTF8String(ArrayRef<UTF16> Src, std::string &Out) {
|
|
|
|
if (!sys::IsBigEndianHost)
|
|
|
|
return convertUTF16ToUTF8String(Src, Out);
|
|
|
|
|
|
|
|
std::vector<UTF16> EndianCorrectedSrc;
|
|
|
|
EndianCorrectedSrc.resize(Src.size() + 1);
|
|
|
|
llvm::copy(Src, EndianCorrectedSrc.begin() + 1);
|
|
|
|
EndianCorrectedSrc[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED;
|
|
|
|
return convertUTF16ToUTF8String(makeArrayRef(EndianCorrectedSrc), Out);
|
|
|
|
}
|
|
|
|
|
2019-05-02 23:21:55 +02:00
|
|
|
static std::string makeDuplicateResourceError(
|
|
|
|
const ResourceEntryRef &Entry, StringRef File1, StringRef File2) {
|
2019-04-24 13:42:59 +02:00
|
|
|
std::string Ret;
|
|
|
|
raw_string_ostream OS(Ret);
|
|
|
|
|
|
|
|
OS << "duplicate resource:";
|
|
|
|
|
|
|
|
OS << " type ";
|
|
|
|
if (Entry.checkTypeString()) {
|
|
|
|
std::string UTF8;
|
2019-04-29 02:51:41 +02:00
|
|
|
if (!convertUTF16LEToUTF8String(Entry.getTypeString(), UTF8))
|
2019-04-24 13:42:59 +02:00
|
|
|
UTF8 = "(failed conversion from UTF16)";
|
|
|
|
OS << '\"' << UTF8 << '\"';
|
2019-04-25 01:26:30 +02:00
|
|
|
} else
|
|
|
|
printResourceTypeName(Entry.getTypeID(), OS);
|
2019-04-24 13:42:59 +02:00
|
|
|
|
|
|
|
OS << "/name ";
|
|
|
|
if (Entry.checkNameString()) {
|
|
|
|
std::string UTF8;
|
2019-04-29 02:51:41 +02:00
|
|
|
if (!convertUTF16LEToUTF8String(Entry.getNameString(), UTF8))
|
2019-04-24 13:42:59 +02:00
|
|
|
UTF8 = "(failed conversion from UTF16)";
|
|
|
|
OS << '\"' << UTF8 << '\"';
|
|
|
|
} else {
|
|
|
|
OS << "ID " << Entry.getNameID();
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "/language " << Entry.getLanguage() << ", in " << File1 << " and in "
|
|
|
|
<< File2;
|
|
|
|
|
2019-05-02 23:21:55 +02:00
|
|
|
return OS.str();
|
2019-04-24 13:42:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:33 +02:00
|
|
|
static void printStringOrID(const WindowsResourceParser::StringOrID &S,
|
|
|
|
raw_string_ostream &OS, bool IsType, bool IsID) {
|
|
|
|
if (S.IsString) {
|
|
|
|
std::string UTF8;
|
|
|
|
if (!convertUTF16LEToUTF8String(S.String, UTF8))
|
|
|
|
UTF8 = "(failed conversion from UTF16)";
|
|
|
|
OS << '\"' << UTF8 << '\"';
|
|
|
|
} else if (IsType)
|
|
|
|
printResourceTypeName(S.ID, OS);
|
|
|
|
else if (IsID)
|
|
|
|
OS << "ID " << S.ID;
|
|
|
|
else
|
|
|
|
OS << S.ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string makeDuplicateResourceError(
|
|
|
|
const std::vector<WindowsResourceParser::StringOrID> &Context,
|
|
|
|
StringRef File1, StringRef File2) {
|
|
|
|
std::string Ret;
|
|
|
|
raw_string_ostream OS(Ret);
|
|
|
|
|
|
|
|
OS << "duplicate resource:";
|
|
|
|
|
|
|
|
if (Context.size() >= 1) {
|
|
|
|
OS << " type ";
|
|
|
|
printStringOrID(Context[0], OS, /* IsType */ true, /* IsID */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Context.size() >= 2) {
|
|
|
|
OS << "/name ";
|
|
|
|
printStringOrID(Context[1], OS, /* IsType */ false, /* IsID */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Context.size() >= 3) {
|
|
|
|
OS << "/language ";
|
|
|
|
printStringOrID(Context[2], OS, /* IsType */ false, /* IsID */ false);
|
|
|
|
}
|
|
|
|
OS << ", in " << File1 << " and in " << File2;
|
|
|
|
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
[LLD] [COFF] Implement MinGW default manifest handling
In mingw environments, resources are normally compiled to resource
object files directly, instead of letting the linker convert them to
COFF format.
Since some time, GCC supports the notion of a default manifest object.
When invoking the linker, GCC looks for the default manifest object
file, and if found in the expected path, it is added to linker commands.
The default manifest is one that indicates support for the latest known
versions of windows, to implicitly unlock the modern behaviours of certain
APIs.
Not all mingw/gcc distributions include this file, but e.g. in msys2,
the default manifest object is distributed in a separate package (which
can be but might not always be installed).
This means that even if user projects only use one single resource
object file, the linker can end up with two resource object files,
and thus needs to support merging them.
The default manifest has a language id of zero, and GNU ld has got
logic for dropping a manifest with a zero language id, if there's
another manifest present with a nonzero language id. If there are
multiple manifests with a nonzero language id, the merging process
errors out.
Differential Revision: https://reviews.llvm.org/D66825
llvm-svn: 370974
2019-09-04 22:34:00 +02:00
|
|
|
// MinGW specific. Remove default manifests (with language zero) if there are
|
|
|
|
// other manifests present, and report an error if there are more than one
|
|
|
|
// manifest with a non-zero language code.
|
|
|
|
// GCC has the concept of a default manifest resource object, which gets
|
|
|
|
// linked in implicitly if present. This default manifest has got language
|
|
|
|
// id zero, and should be dropped silently if there's another manifest present.
|
|
|
|
// If the user resources surprisignly had a manifest with language id zero,
|
|
|
|
// we should also ignore the duplicate default manifest.
|
|
|
|
void WindowsResourceParser::cleanUpManifests(
|
|
|
|
std::vector<std::string> &Duplicates) {
|
|
|
|
auto TypeIt = Root.IDChildren.find(/* RT_MANIFEST */ 24);
|
|
|
|
if (TypeIt == Root.IDChildren.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
TreeNode *TypeNode = TypeIt->second.get();
|
|
|
|
auto NameIt =
|
|
|
|
TypeNode->IDChildren.find(/* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 1);
|
|
|
|
if (NameIt == TypeNode->IDChildren.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
TreeNode *NameNode = NameIt->second.get();
|
|
|
|
if (NameNode->IDChildren.size() <= 1)
|
|
|
|
return; // None or one manifest present, all good.
|
|
|
|
|
|
|
|
// If we have more than one manifest, drop the language zero one if present,
|
|
|
|
// and check again.
|
|
|
|
auto LangZeroIt = NameNode->IDChildren.find(0);
|
|
|
|
if (LangZeroIt != NameNode->IDChildren.end() &&
|
|
|
|
LangZeroIt->second->IsDataNode) {
|
|
|
|
uint32_t RemovedIndex = LangZeroIt->second->DataIndex;
|
|
|
|
NameNode->IDChildren.erase(LangZeroIt);
|
|
|
|
Data.erase(Data.begin() + RemovedIndex);
|
|
|
|
Root.shiftDataIndexDown(RemovedIndex);
|
|
|
|
|
|
|
|
// If we're now down to one manifest, all is good.
|
|
|
|
if (NameNode->IDChildren.size() <= 1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// More than one non-language-zero manifest
|
|
|
|
auto FirstIt = NameNode->IDChildren.begin();
|
|
|
|
uint32_t FirstLang = FirstIt->first;
|
|
|
|
TreeNode *FirstNode = FirstIt->second.get();
|
|
|
|
auto LastIt = NameNode->IDChildren.rbegin();
|
|
|
|
uint32_t LastLang = LastIt->first;
|
|
|
|
TreeNode *LastNode = LastIt->second.get();
|
|
|
|
Duplicates.push_back(
|
|
|
|
("duplicate non-default manifests with languages " + Twine(FirstLang) +
|
|
|
|
" in " + InputFilenames[FirstNode->Origin] + " and " + Twine(LastLang) +
|
|
|
|
" in " + InputFilenames[LastNode->Origin])
|
|
|
|
.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore duplicates of manifests with language zero (the default manifest),
|
|
|
|
// in case the user has provided a manifest with that language id. See
|
|
|
|
// the function comment above for context. Only returns true if MinGW is set
|
|
|
|
// to true.
|
|
|
|
bool WindowsResourceParser::shouldIgnoreDuplicate(
|
|
|
|
const ResourceEntryRef &Entry) const {
|
|
|
|
return MinGW && !Entry.checkTypeString() &&
|
|
|
|
Entry.getTypeID() == /* RT_MANIFEST */ 24 &&
|
|
|
|
!Entry.checkNameString() &&
|
|
|
|
Entry.getNameID() == /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 1 &&
|
|
|
|
Entry.getLanguage() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowsResourceParser::shouldIgnoreDuplicate(
|
|
|
|
const std::vector<StringOrID> &Context) const {
|
|
|
|
return MinGW && Context.size() == 3 && !Context[0].IsString &&
|
|
|
|
Context[0].ID == /* RT_MANIFEST */ 24 && !Context[1].IsString &&
|
|
|
|
Context[1].ID == /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 1 &&
|
|
|
|
!Context[2].IsString && Context[2].ID == 0;
|
|
|
|
}
|
|
|
|
|
2019-05-02 23:21:55 +02:00
|
|
|
Error WindowsResourceParser::parse(WindowsResource *WR,
|
|
|
|
std::vector<std::string> &Duplicates) {
|
2017-05-30 20:19:06 +02:00
|
|
|
auto EntryOrErr = WR->getHeadEntry();
|
2017-08-24 04:36:50 +02:00
|
|
|
if (!EntryOrErr) {
|
|
|
|
auto E = EntryOrErr.takeError();
|
|
|
|
if (E.isA<EmptyResError>()) {
|
|
|
|
// Check if the .res file contains no entries. In this case we don't have
|
|
|
|
// to throw an error but can rather just return without parsing anything.
|
|
|
|
// This applies for files which have a valid PE header magic and the
|
|
|
|
// mandatory empty null resource entry. Files which do not fit this
|
|
|
|
// criteria would have already been filtered out by
|
|
|
|
// WindowsResource::createWindowsResource().
|
|
|
|
consumeError(std::move(E));
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
return E;
|
|
|
|
}
|
2017-05-30 20:19:06 +02:00
|
|
|
|
|
|
|
ResourceEntryRef Entry = EntryOrErr.get();
|
2019-08-30 08:55:54 +02:00
|
|
|
uint32_t Origin = InputFilenames.size();
|
2020-01-28 20:23:46 +01:00
|
|
|
InputFilenames.push_back(std::string(WR->getFileName()));
|
2017-05-30 20:19:06 +02:00
|
|
|
bool End = false;
|
|
|
|
while (!End) {
|
2017-06-13 02:16:32 +02:00
|
|
|
|
2019-08-30 08:55:54 +02:00
|
|
|
TreeNode *Node;
|
2019-08-30 08:56:02 +02:00
|
|
|
bool IsNewNode = Root.addEntry(Entry, Origin, Data, StringTable, Node);
|
2019-05-02 23:21:55 +02:00
|
|
|
if (!IsNewNode) {
|
[LLD] [COFF] Implement MinGW default manifest handling
In mingw environments, resources are normally compiled to resource
object files directly, instead of letting the linker convert them to
COFF format.
Since some time, GCC supports the notion of a default manifest object.
When invoking the linker, GCC looks for the default manifest object
file, and if found in the expected path, it is added to linker commands.
The default manifest is one that indicates support for the latest known
versions of windows, to implicitly unlock the modern behaviours of certain
APIs.
Not all mingw/gcc distributions include this file, but e.g. in msys2,
the default manifest object is distributed in a separate package (which
can be but might not always be installed).
This means that even if user projects only use one single resource
object file, the linker can end up with two resource object files,
and thus needs to support merging them.
The default manifest has a language id of zero, and GNU ld has got
logic for dropping a manifest with a zero language id, if there's
another manifest present with a nonzero language id. If there are
multiple manifests with a nonzero language id, the merging process
errors out.
Differential Revision: https://reviews.llvm.org/D66825
llvm-svn: 370974
2019-09-04 22:34:00 +02:00
|
|
|
if (!shouldIgnoreDuplicate(Entry))
|
|
|
|
Duplicates.push_back(makeDuplicateResourceError(
|
|
|
|
Entry, InputFilenames[Node->Origin], WR->getFileName()));
|
2019-05-02 23:21:55 +02:00
|
|
|
}
|
2017-06-13 02:16:32 +02:00
|
|
|
|
2017-05-30 20:19:06 +02:00
|
|
|
RETURN_IF_ERROR(Entry.moveNext(End));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:33 +02:00
|
|
|
Error WindowsResourceParser::parse(ResourceSectionRef &RSR, StringRef Filename,
|
|
|
|
std::vector<std::string> &Duplicates) {
|
|
|
|
UNWRAP_REF_OR_RETURN(BaseTable, RSR.getBaseTable());
|
|
|
|
uint32_t Origin = InputFilenames.size();
|
2020-01-28 20:23:46 +01:00
|
|
|
InputFilenames.push_back(std::string(Filename));
|
2019-08-30 08:56:33 +02:00
|
|
|
std::vector<StringOrID> Context;
|
|
|
|
return addChildren(Root, RSR, BaseTable, Origin, Context, Duplicates);
|
|
|
|
}
|
|
|
|
|
2017-06-13 20:17:36 +02:00
|
|
|
void WindowsResourceParser::printTree(raw_ostream &OS) const {
|
|
|
|
ScopedPrinter Writer(OS);
|
2017-05-30 20:19:06 +02:00
|
|
|
Root.print(Writer, "Resource Tree");
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:02 +02:00
|
|
|
bool WindowsResourceParser::TreeNode::addEntry(
|
|
|
|
const ResourceEntryRef &Entry, uint32_t Origin,
|
|
|
|
std::vector<std::vector<uint8_t>> &Data,
|
|
|
|
std::vector<std::vector<UTF16>> &StringTable, TreeNode *&Result) {
|
|
|
|
TreeNode &TypeNode = addTypeNode(Entry, StringTable);
|
|
|
|
TreeNode &NameNode = TypeNode.addNameNode(Entry, StringTable);
|
|
|
|
return NameNode.addLanguageNode(Entry, Origin, Data, Result);
|
2017-05-30 20:19:06 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:33 +02:00
|
|
|
Error WindowsResourceParser::addChildren(TreeNode &Node,
|
|
|
|
ResourceSectionRef &RSR,
|
|
|
|
const coff_resource_dir_table &Table,
|
|
|
|
uint32_t Origin,
|
|
|
|
std::vector<StringOrID> &Context,
|
|
|
|
std::vector<std::string> &Duplicates) {
|
|
|
|
|
|
|
|
for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
|
|
|
|
i++) {
|
|
|
|
UNWRAP_REF_OR_RETURN(Entry, RSR.getTableEntry(Table, i));
|
|
|
|
TreeNode *Child;
|
|
|
|
|
|
|
|
if (Entry.Offset.isSubDir()) {
|
|
|
|
|
|
|
|
// Create a new subdirectory and recurse
|
|
|
|
if (i < Table.NumberOfNameEntries) {
|
|
|
|
UNWRAP_OR_RETURN(NameString, RSR.getEntryNameString(Entry));
|
|
|
|
Child = &Node.addNameChild(NameString, StringTable);
|
|
|
|
Context.push_back(StringOrID(NameString));
|
|
|
|
} else {
|
|
|
|
Child = &Node.addIDChild(Entry.Identifier.ID);
|
|
|
|
Context.push_back(StringOrID(Entry.Identifier.ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
UNWRAP_REF_OR_RETURN(NextTable, RSR.getEntrySubDir(Entry));
|
|
|
|
Error E =
|
|
|
|
addChildren(*Child, RSR, NextTable, Origin, Context, Duplicates);
|
|
|
|
if (E)
|
|
|
|
return E;
|
|
|
|
Context.pop_back();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Data leaves are supposed to have a numeric ID as identifier (language).
|
|
|
|
if (Table.NumberOfNameEntries > 0)
|
|
|
|
return createStringError(object_error::parse_failed,
|
|
|
|
"unexpected string key for data object");
|
|
|
|
|
|
|
|
// Try adding a data leaf
|
|
|
|
UNWRAP_REF_OR_RETURN(DataEntry, RSR.getEntryData(Entry));
|
|
|
|
TreeNode *Child;
|
|
|
|
Context.push_back(StringOrID(Entry.Identifier.ID));
|
|
|
|
bool Added = Node.addDataChild(Entry.Identifier.ID, Table.MajorVersion,
|
|
|
|
Table.MinorVersion, Table.Characteristics,
|
|
|
|
Origin, Data.size(), Child);
|
|
|
|
if (Added) {
|
|
|
|
UNWRAP_OR_RETURN(Contents, RSR.getContents(DataEntry));
|
|
|
|
Data.push_back(ArrayRef<uint8_t>(
|
|
|
|
reinterpret_cast<const uint8_t *>(Contents.data()),
|
|
|
|
Contents.size()));
|
|
|
|
} else {
|
[LLD] [COFF] Implement MinGW default manifest handling
In mingw environments, resources are normally compiled to resource
object files directly, instead of letting the linker convert them to
COFF format.
Since some time, GCC supports the notion of a default manifest object.
When invoking the linker, GCC looks for the default manifest object
file, and if found in the expected path, it is added to linker commands.
The default manifest is one that indicates support for the latest known
versions of windows, to implicitly unlock the modern behaviours of certain
APIs.
Not all mingw/gcc distributions include this file, but e.g. in msys2,
the default manifest object is distributed in a separate package (which
can be but might not always be installed).
This means that even if user projects only use one single resource
object file, the linker can end up with two resource object files,
and thus needs to support merging them.
The default manifest has a language id of zero, and GNU ld has got
logic for dropping a manifest with a zero language id, if there's
another manifest present with a nonzero language id. If there are
multiple manifests with a nonzero language id, the merging process
errors out.
Differential Revision: https://reviews.llvm.org/D66825
llvm-svn: 370974
2019-09-04 22:34:00 +02:00
|
|
|
if (!shouldIgnoreDuplicate(Context))
|
|
|
|
Duplicates.push_back(makeDuplicateResourceError(
|
|
|
|
Context, InputFilenames[Child->Origin], InputFilenames.back()));
|
2019-08-30 08:56:33 +02:00
|
|
|
}
|
|
|
|
Context.pop_back();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:02 +02:00
|
|
|
WindowsResourceParser::TreeNode::TreeNode(uint32_t StringIndex)
|
|
|
|
: StringIndex(StringIndex) {}
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
WindowsResourceParser::TreeNode::TreeNode(uint16_t MajorVersion,
|
|
|
|
uint16_t MinorVersion,
|
2019-04-24 13:42:59 +02:00
|
|
|
uint32_t Characteristics,
|
2019-08-30 08:56:02 +02:00
|
|
|
uint32_t Origin, uint32_t DataIndex)
|
|
|
|
: IsDataNode(true), DataIndex(DataIndex), MajorVersion(MajorVersion),
|
|
|
|
MinorVersion(MinorVersion), Characteristics(Characteristics),
|
|
|
|
Origin(Origin) {}
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
std::unique_ptr<WindowsResourceParser::TreeNode>
|
2019-08-30 08:56:02 +02:00
|
|
|
WindowsResourceParser::TreeNode::createStringNode(uint32_t Index) {
|
|
|
|
return std::unique_ptr<TreeNode>(new TreeNode(Index));
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<WindowsResourceParser::TreeNode>
|
|
|
|
WindowsResourceParser::TreeNode::createIDNode() {
|
2019-08-30 08:56:02 +02:00
|
|
|
return std::unique_ptr<TreeNode>(new TreeNode(0));
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<WindowsResourceParser::TreeNode>
|
|
|
|
WindowsResourceParser::TreeNode::createDataNode(uint16_t MajorVersion,
|
|
|
|
uint16_t MinorVersion,
|
2019-04-24 13:42:59 +02:00
|
|
|
uint32_t Characteristics,
|
2019-08-30 08:56:02 +02:00
|
|
|
uint32_t Origin,
|
|
|
|
uint32_t DataIndex) {
|
|
|
|
return std::unique_ptr<TreeNode>(new TreeNode(
|
|
|
|
MajorVersion, MinorVersion, Characteristics, Origin, DataIndex));
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2019-08-30 08:56:02 +02:00
|
|
|
WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addTypeNode(
|
|
|
|
const ResourceEntryRef &Entry,
|
|
|
|
std::vector<std::vector<UTF16>> &StringTable) {
|
2017-05-30 20:19:06 +02:00
|
|
|
if (Entry.checkTypeString())
|
2019-08-30 08:56:02 +02:00
|
|
|
return addNameChild(Entry.getTypeString(), StringTable);
|
2017-05-30 20:19:06 +02:00
|
|
|
else
|
2019-04-23 20:46:53 +02:00
|
|
|
return addIDChild(Entry.getTypeID());
|
2017-05-30 20:19:06 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:02 +02:00
|
|
|
WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addNameNode(
|
|
|
|
const ResourceEntryRef &Entry,
|
|
|
|
std::vector<std::vector<UTF16>> &StringTable) {
|
2017-05-30 20:19:06 +02:00
|
|
|
if (Entry.checkNameString())
|
2019-08-30 08:56:02 +02:00
|
|
|
return addNameChild(Entry.getNameString(), StringTable);
|
2017-05-30 20:19:06 +02:00
|
|
|
else
|
2019-04-23 20:46:53 +02:00
|
|
|
return addIDChild(Entry.getNameID());
|
2017-05-30 20:19:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 13:42:59 +02:00
|
|
|
bool WindowsResourceParser::TreeNode::addLanguageNode(
|
2019-08-30 08:56:02 +02:00
|
|
|
const ResourceEntryRef &Entry, uint32_t Origin,
|
|
|
|
std::vector<std::vector<uint8_t>> &Data, TreeNode *&Result) {
|
|
|
|
bool Added = addDataChild(Entry.getLanguage(), Entry.getMajorVersion(),
|
|
|
|
Entry.getMinorVersion(), Entry.getCharacteristics(),
|
|
|
|
Origin, Data.size(), Result);
|
|
|
|
if (Added)
|
|
|
|
Data.push_back(Entry.getData());
|
|
|
|
return Added;
|
2017-05-30 20:19:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 13:42:59 +02:00
|
|
|
bool WindowsResourceParser::TreeNode::addDataChild(
|
2019-04-23 20:46:53 +02:00
|
|
|
uint32_t ID, uint16_t MajorVersion, uint16_t MinorVersion,
|
2019-08-30 08:56:02 +02:00
|
|
|
uint32_t Characteristics, uint32_t Origin, uint32_t DataIndex,
|
|
|
|
TreeNode *&Result) {
|
|
|
|
auto NewChild = createDataNode(MajorVersion, MinorVersion, Characteristics,
|
|
|
|
Origin, DataIndex);
|
2019-04-24 13:42:59 +02:00
|
|
|
auto ElementInserted = IDChildren.emplace(ID, std::move(NewChild));
|
|
|
|
Result = ElementInserted.first->second.get();
|
|
|
|
return ElementInserted.second;
|
2019-04-23 20:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addIDChild(
|
|
|
|
uint32_t ID) {
|
|
|
|
auto Child = IDChildren.find(ID);
|
|
|
|
if (Child == IDChildren.end()) {
|
|
|
|
auto NewChild = createIDNode();
|
2017-05-30 20:19:06 +02:00
|
|
|
WindowsResourceParser::TreeNode &Node = *NewChild;
|
|
|
|
IDChildren.emplace(ID, std::move(NewChild));
|
|
|
|
return Node;
|
|
|
|
} else
|
|
|
|
return *(Child->second);
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:56:02 +02:00
|
|
|
WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addNameChild(
|
|
|
|
ArrayRef<UTF16> NameRef, std::vector<std::vector<UTF16>> &StringTable) {
|
2017-05-30 20:19:06 +02:00
|
|
|
std::string NameString;
|
2019-04-29 02:51:41 +02:00
|
|
|
convertUTF16LEToUTF8String(NameRef, NameString);
|
2017-05-30 20:19:06 +02:00
|
|
|
|
|
|
|
auto Child = StringChildren.find(NameString);
|
|
|
|
if (Child == StringChildren.end()) {
|
2019-08-30 08:56:02 +02:00
|
|
|
auto NewChild = createStringNode(StringTable.size());
|
|
|
|
StringTable.push_back(NameRef);
|
2017-05-30 20:19:06 +02:00
|
|
|
WindowsResourceParser::TreeNode &Node = *NewChild;
|
|
|
|
StringChildren.emplace(NameString, std::move(NewChild));
|
|
|
|
return Node;
|
|
|
|
} else
|
|
|
|
return *(Child->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceParser::TreeNode::print(ScopedPrinter &Writer,
|
|
|
|
StringRef Name) const {
|
|
|
|
ListScope NodeScope(Writer, Name);
|
|
|
|
for (auto const &Child : StringChildren) {
|
|
|
|
Child.second->print(Writer, Child.first);
|
|
|
|
}
|
|
|
|
for (auto const &Child : IDChildren) {
|
2017-05-31 00:29:06 +02:00
|
|
|
Child.second->print(Writer, to_string(Child.first));
|
2017-05-30 20:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 19:34:30 +02:00
|
|
|
// This function returns the size of the entire resource tree, including
|
|
|
|
// directory tables, directory entries, and data entries. It does not include
|
|
|
|
// the directory strings or the relocations of the .rsrc section.
|
|
|
|
uint32_t WindowsResourceParser::TreeNode::getTreeSize() const {
|
|
|
|
uint32_t Size = (IDChildren.size() + StringChildren.size()) *
|
2017-06-17 00:00:42 +02:00
|
|
|
sizeof(coff_resource_dir_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
// Reached a node pointing to a data entry.
|
|
|
|
if (IsDataNode) {
|
2017-06-17 00:00:42 +02:00
|
|
|
Size += sizeof(coff_resource_data_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the node does not point to data, it must have a directory table pointing
|
|
|
|
// to other nodes.
|
2017-06-17 00:00:42 +02:00
|
|
|
Size += sizeof(coff_resource_dir_table);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
for (auto const &Child : StringChildren) {
|
|
|
|
Size += Child.second->getTreeSize();
|
|
|
|
}
|
|
|
|
for (auto const &Child : IDChildren) {
|
|
|
|
Size += Child.second->getTreeSize();
|
|
|
|
}
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
[LLD] [COFF] Implement MinGW default manifest handling
In mingw environments, resources are normally compiled to resource
object files directly, instead of letting the linker convert them to
COFF format.
Since some time, GCC supports the notion of a default manifest object.
When invoking the linker, GCC looks for the default manifest object
file, and if found in the expected path, it is added to linker commands.
The default manifest is one that indicates support for the latest known
versions of windows, to implicitly unlock the modern behaviours of certain
APIs.
Not all mingw/gcc distributions include this file, but e.g. in msys2,
the default manifest object is distributed in a separate package (which
can be but might not always be installed).
This means that even if user projects only use one single resource
object file, the linker can end up with two resource object files,
and thus needs to support merging them.
The default manifest has a language id of zero, and GNU ld has got
logic for dropping a manifest with a zero language id, if there's
another manifest present with a nonzero language id. If there are
multiple manifests with a nonzero language id, the merging process
errors out.
Differential Revision: https://reviews.llvm.org/D66825
llvm-svn: 370974
2019-09-04 22:34:00 +02:00
|
|
|
// Shift DataIndex of all data children with an Index greater or equal to the
|
|
|
|
// given one, to fill a gap from removing an entry from the Data vector.
|
|
|
|
void WindowsResourceParser::TreeNode::shiftDataIndexDown(uint32_t Index) {
|
|
|
|
if (IsDataNode && DataIndex >= Index) {
|
|
|
|
DataIndex--;
|
|
|
|
} else {
|
|
|
|
for (auto &Child : IDChildren)
|
|
|
|
Child.second->shiftDataIndexDown(Index);
|
|
|
|
for (auto &Child : StringChildren)
|
|
|
|
Child.second->shiftDataIndexDown(Index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 19:34:30 +02:00
|
|
|
class WindowsResourceCOFFWriter {
|
|
|
|
public:
|
2017-06-19 20:49:05 +02:00
|
|
|
WindowsResourceCOFFWriter(COFF::MachineTypes MachineType,
|
2017-06-09 19:34:30 +02:00
|
|
|
const WindowsResourceParser &Parser, Error &E);
|
2019-06-11 13:26:50 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> write(uint32_t TimeDateStamp);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void performFileLayout();
|
|
|
|
void performSectionOneLayout();
|
|
|
|
void performSectionTwoLayout();
|
2019-06-11 13:26:50 +02:00
|
|
|
void writeCOFFHeader(uint32_t TimeDateStamp);
|
2017-06-09 19:34:30 +02:00
|
|
|
void writeFirstSectionHeader();
|
|
|
|
void writeSecondSectionHeader();
|
|
|
|
void writeFirstSection();
|
|
|
|
void writeSecondSection();
|
|
|
|
void writeSymbolTable();
|
|
|
|
void writeStringTable();
|
|
|
|
void writeDirectoryTree();
|
|
|
|
void writeDirectoryStringTable();
|
|
|
|
void writeFirstSectionRelocations();
|
2018-01-09 18:26:06 +01:00
|
|
|
std::unique_ptr<WritableMemoryBuffer> OutputBuffer;
|
2017-06-16 23:13:24 +02:00
|
|
|
char *BufferStart;
|
2017-06-13 02:19:43 +02:00
|
|
|
uint64_t CurrentOffset = 0;
|
2017-07-05 21:04:33 +02:00
|
|
|
COFF::MachineTypes MachineType;
|
2017-06-09 19:34:30 +02:00
|
|
|
const WindowsResourceParser::TreeNode &Resources;
|
|
|
|
const ArrayRef<std::vector<uint8_t>> Data;
|
|
|
|
uint64_t FileSize;
|
|
|
|
uint32_t SymbolTableOffset;
|
|
|
|
uint32_t SectionOneSize;
|
|
|
|
uint32_t SectionOneOffset;
|
|
|
|
uint32_t SectionOneRelocations;
|
|
|
|
uint32_t SectionTwoSize;
|
|
|
|
uint32_t SectionTwoOffset;
|
|
|
|
const ArrayRef<std::vector<UTF16>> StringTable;
|
|
|
|
std::vector<uint32_t> StringTableOffsets;
|
|
|
|
std::vector<uint32_t> DataOffsets;
|
|
|
|
std::vector<uint32_t> RelocationAddresses;
|
|
|
|
};
|
|
|
|
|
|
|
|
WindowsResourceCOFFWriter::WindowsResourceCOFFWriter(
|
2017-06-19 20:49:05 +02:00
|
|
|
COFF::MachineTypes MachineType, const WindowsResourceParser &Parser,
|
|
|
|
Error &E)
|
|
|
|
: MachineType(MachineType), Resources(Parser.getTree()),
|
|
|
|
Data(Parser.getData()), StringTable(Parser.getStringTable()) {
|
2017-06-09 19:34:30 +02:00
|
|
|
performFileLayout();
|
2017-07-08 05:06:10 +02:00
|
|
|
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 17:22:28 +02:00
|
|
|
OutputBuffer = WritableMemoryBuffer::getNewMemBuffer(
|
|
|
|
FileSize, "internal .obj file created from .res files");
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::performFileLayout() {
|
|
|
|
// Add size of COFF header.
|
2017-06-17 00:00:42 +02:00
|
|
|
FileSize = COFF::Header16Size;
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
// one .rsrc section header for directory tree, another for resource data.
|
2017-06-17 00:00:42 +02:00
|
|
|
FileSize += 2 * COFF::SectionSize;
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
performSectionOneLayout();
|
|
|
|
performSectionTwoLayout();
|
|
|
|
|
|
|
|
// We have reached the address of the symbol table.
|
|
|
|
SymbolTableOffset = FileSize;
|
|
|
|
|
2017-06-17 00:00:42 +02:00
|
|
|
FileSize += COFF::Symbol16Size; // size of the @feat.00 symbol.
|
|
|
|
FileSize += 4 * COFF::Symbol16Size; // symbol + aux for each section.
|
|
|
|
FileSize += Data.size() * COFF::Symbol16Size; // 1 symbol per resource.
|
2017-06-09 19:34:30 +02:00
|
|
|
FileSize += 4; // four null bytes for the string table.
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::performSectionOneLayout() {
|
|
|
|
SectionOneOffset = FileSize;
|
|
|
|
|
|
|
|
SectionOneSize = Resources.getTreeSize();
|
|
|
|
uint32_t CurrentStringOffset = SectionOneSize;
|
|
|
|
uint32_t TotalStringTableSize = 0;
|
|
|
|
for (auto const &String : StringTable) {
|
|
|
|
StringTableOffsets.push_back(CurrentStringOffset);
|
|
|
|
uint32_t StringSize = String.size() * sizeof(UTF16) + sizeof(uint16_t);
|
|
|
|
CurrentStringOffset += StringSize;
|
|
|
|
TotalStringTableSize += StringSize;
|
|
|
|
}
|
|
|
|
SectionOneSize += alignTo(TotalStringTableSize, sizeof(uint32_t));
|
|
|
|
|
|
|
|
// account for the relocations of section one.
|
|
|
|
SectionOneRelocations = FileSize + SectionOneSize;
|
|
|
|
FileSize += SectionOneSize;
|
2017-06-17 00:00:42 +02:00
|
|
|
FileSize +=
|
|
|
|
Data.size() * COFF::RelocationSize; // one relocation for each resource.
|
2017-06-13 02:19:43 +02:00
|
|
|
FileSize = alignTo(FileSize, SECTION_ALIGNMENT);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::performSectionTwoLayout() {
|
|
|
|
// add size of .rsrc$2 section, which contains all resource data on 8-byte
|
|
|
|
// alignment.
|
|
|
|
SectionTwoOffset = FileSize;
|
|
|
|
SectionTwoSize = 0;
|
|
|
|
for (auto const &Entry : Data) {
|
|
|
|
DataOffsets.push_back(SectionTwoSize);
|
2017-06-17 00:00:42 +02:00
|
|
|
SectionTwoSize += alignTo(Entry.size(), sizeof(uint64_t));
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
FileSize += SectionTwoSize;
|
2017-06-13 02:19:43 +02:00
|
|
|
FileSize = alignTo(FileSize, SECTION_ALIGNMENT);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 13:26:50 +02:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
|
|
|
WindowsResourceCOFFWriter::write(uint32_t TimeDateStamp) {
|
2018-01-09 18:26:06 +01:00
|
|
|
BufferStart = OutputBuffer->getBufferStart();
|
2017-06-09 19:34:30 +02:00
|
|
|
|
2019-06-11 13:26:50 +02:00
|
|
|
writeCOFFHeader(TimeDateStamp);
|
2017-06-09 19:34:30 +02:00
|
|
|
writeFirstSectionHeader();
|
|
|
|
writeSecondSectionHeader();
|
|
|
|
writeFirstSection();
|
|
|
|
writeSecondSection();
|
|
|
|
writeSymbolTable();
|
|
|
|
writeStringTable();
|
|
|
|
|
2017-06-19 20:49:05 +02:00
|
|
|
return std::move(OutputBuffer);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 09:54:42 +02:00
|
|
|
// According to COFF specification, if the Src has a size equal to Dest,
|
|
|
|
// it's okay to *not* copy the trailing zero.
|
|
|
|
static void coffnamecpy(char (&Dest)[COFF::NameSize], StringRef Src) {
|
|
|
|
assert(Src.size() <= COFF::NameSize &&
|
2020-02-15 08:58:40 +01:00
|
|
|
"Src is larger than COFF::NameSize");
|
|
|
|
assert((Src.size() == COFF::NameSize || Dest[Src.size()] == '\0') &&
|
|
|
|
"Dest not zeroed upon initialization");
|
|
|
|
memcpy(Dest, Src.data(), Src.size());
|
2019-08-21 09:54:42 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 13:26:50 +02:00
|
|
|
void WindowsResourceCOFFWriter::writeCOFFHeader(uint32_t TimeDateStamp) {
|
2017-06-09 19:34:30 +02:00
|
|
|
// Write the COFF header.
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Header = reinterpret_cast<coff_file_header *>(BufferStart);
|
2017-11-10 23:27:41 +01:00
|
|
|
Header->Machine = MachineType;
|
2017-06-09 19:34:30 +02:00
|
|
|
Header->NumberOfSections = 2;
|
2019-06-11 13:26:50 +02:00
|
|
|
Header->TimeDateStamp = TimeDateStamp;
|
2017-06-09 19:34:30 +02:00
|
|
|
Header->PointerToSymbolTable = SymbolTableOffset;
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 17:22:28 +02:00
|
|
|
// One symbol for every resource plus 2 for each section and 1 for @feat.00
|
2017-06-09 19:34:30 +02:00
|
|
|
Header->NumberOfSymbols = Data.size() + 5;
|
|
|
|
Header->SizeOfOptionalHeader = 0;
|
2019-06-11 13:26:50 +02:00
|
|
|
// cvtres.exe sets 32BIT_MACHINE even for 64-bit machine types. Match it.
|
2017-06-17 00:00:42 +02:00
|
|
|
Header->Characteristics = COFF::IMAGE_FILE_32BIT_MACHINE;
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeFirstSectionHeader() {
|
|
|
|
// Write the first section header.
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_file_header);
|
|
|
|
auto *SectionOneHeader =
|
|
|
|
reinterpret_cast<coff_section *>(BufferStart + CurrentOffset);
|
2019-08-21 09:54:42 +02:00
|
|
|
coffnamecpy(SectionOneHeader->Name, ".rsrc$01");
|
2017-06-09 19:34:30 +02:00
|
|
|
SectionOneHeader->VirtualSize = 0;
|
|
|
|
SectionOneHeader->VirtualAddress = 0;
|
|
|
|
SectionOneHeader->SizeOfRawData = SectionOneSize;
|
|
|
|
SectionOneHeader->PointerToRawData = SectionOneOffset;
|
|
|
|
SectionOneHeader->PointerToRelocations = SectionOneRelocations;
|
|
|
|
SectionOneHeader->PointerToLinenumbers = 0;
|
|
|
|
SectionOneHeader->NumberOfRelocations = Data.size();
|
|
|
|
SectionOneHeader->NumberOfLinenumbers = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
SectionOneHeader->Characteristics += COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
|
|
SectionOneHeader->Characteristics += COFF::IMAGE_SCN_MEM_READ;
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeSecondSectionHeader() {
|
|
|
|
// Write the second section header.
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_section);
|
|
|
|
auto *SectionTwoHeader =
|
|
|
|
reinterpret_cast<coff_section *>(BufferStart + CurrentOffset);
|
2019-08-21 09:54:42 +02:00
|
|
|
coffnamecpy(SectionTwoHeader->Name, ".rsrc$02");
|
2017-06-09 19:34:30 +02:00
|
|
|
SectionTwoHeader->VirtualSize = 0;
|
|
|
|
SectionTwoHeader->VirtualAddress = 0;
|
|
|
|
SectionTwoHeader->SizeOfRawData = SectionTwoSize;
|
|
|
|
SectionTwoHeader->PointerToRawData = SectionTwoOffset;
|
|
|
|
SectionTwoHeader->PointerToRelocations = 0;
|
|
|
|
SectionTwoHeader->PointerToLinenumbers = 0;
|
|
|
|
SectionTwoHeader->NumberOfRelocations = 0;
|
|
|
|
SectionTwoHeader->NumberOfLinenumbers = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
SectionTwoHeader->Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
|
|
SectionTwoHeader->Characteristics += COFF::IMAGE_SCN_MEM_READ;
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeFirstSection() {
|
|
|
|
// Write section one.
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_section);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
writeDirectoryTree();
|
|
|
|
writeDirectoryStringTable();
|
|
|
|
writeFirstSectionRelocations();
|
2017-06-13 02:19:43 +02:00
|
|
|
|
|
|
|
CurrentOffset = alignTo(CurrentOffset, SECTION_ALIGNMENT);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeSecondSection() {
|
|
|
|
// Now write the .rsrc$02 section.
|
|
|
|
for (auto const &RawDataEntry : Data) {
|
2018-11-17 02:44:25 +01:00
|
|
|
llvm::copy(RawDataEntry, BufferStart + CurrentOffset);
|
2017-06-13 02:19:43 +02:00
|
|
|
CurrentOffset += alignTo(RawDataEntry.size(), sizeof(uint64_t));
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
2017-06-13 02:19:43 +02:00
|
|
|
|
|
|
|
CurrentOffset = alignTo(CurrentOffset, SECTION_ALIGNMENT);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeSymbolTable() {
|
|
|
|
// Now write the symbol table.
|
|
|
|
// First, the feat symbol.
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
|
2019-08-21 09:54:42 +02:00
|
|
|
coffnamecpy(Symbol->Name.ShortName, "@feat.00");
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->Value = 0x11;
|
|
|
|
Symbol->SectionNumber = 0xffff;
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL;
|
|
|
|
Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->NumberOfAuxSymbols = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_symbol16);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
// Now write the .rsrc1 symbol + aux.
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
|
2019-08-21 09:54:42 +02:00
|
|
|
coffnamecpy(Symbol->Name.ShortName, ".rsrc$01");
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->Value = 0;
|
|
|
|
Symbol->SectionNumber = 1;
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL;
|
|
|
|
Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->NumberOfAuxSymbols = 1;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_symbol16);
|
|
|
|
auto *Aux = reinterpret_cast<coff_aux_section_definition *>(BufferStart +
|
|
|
|
CurrentOffset);
|
2017-06-09 19:34:30 +02:00
|
|
|
Aux->Length = SectionOneSize;
|
|
|
|
Aux->NumberOfRelocations = Data.size();
|
|
|
|
Aux->NumberOfLinenumbers = 0;
|
|
|
|
Aux->CheckSum = 0;
|
|
|
|
Aux->NumberLowPart = 0;
|
|
|
|
Aux->Selection = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_aux_section_definition);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
// Now write the .rsrc2 symbol + aux.
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
|
2019-08-21 09:54:42 +02:00
|
|
|
coffnamecpy(Symbol->Name.ShortName, ".rsrc$02");
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->Value = 0;
|
|
|
|
Symbol->SectionNumber = 2;
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL;
|
|
|
|
Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->NumberOfAuxSymbols = 1;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_symbol16);
|
|
|
|
Aux = reinterpret_cast<coff_aux_section_definition *>(BufferStart +
|
|
|
|
CurrentOffset);
|
2017-06-09 19:34:30 +02:00
|
|
|
Aux->Length = SectionTwoSize;
|
|
|
|
Aux->NumberOfRelocations = 0;
|
|
|
|
Aux->NumberOfLinenumbers = 0;
|
|
|
|
Aux->CheckSum = 0;
|
|
|
|
Aux->NumberLowPart = 0;
|
|
|
|
Aux->Selection = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_aux_section_definition);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
// Now write a symbol for each relocation.
|
|
|
|
for (unsigned i = 0; i < Data.size(); i++) {
|
2017-12-18 23:10:14 +01:00
|
|
|
auto RelocationName = formatv("$R{0:X-6}", i & 0xffffff).sstr<COFF::NameSize>();
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
|
2019-08-21 09:54:42 +02:00
|
|
|
coffnamecpy(Symbol->Name.ShortName, RelocationName);
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->Value = DataOffsets[i];
|
2017-06-30 20:16:35 +02:00
|
|
|
Symbol->SectionNumber = 2;
|
2017-06-17 00:00:42 +02:00
|
|
|
Symbol->Type = COFF::IMAGE_SYM_DTYPE_NULL;
|
|
|
|
Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
|
2017-06-09 19:34:30 +02:00
|
|
|
Symbol->NumberOfAuxSymbols = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_symbol16);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeStringTable() {
|
|
|
|
// Just 4 null bytes for the string table.
|
2017-06-13 22:36:19 +02:00
|
|
|
auto COFFStringTable = reinterpret_cast<void *>(BufferStart + CurrentOffset);
|
|
|
|
memset(COFFStringTable, 0, 4);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeDirectoryTree() {
|
|
|
|
// Traverse parsed resource tree breadth-first and write the corresponding
|
|
|
|
// COFF objects.
|
|
|
|
std::queue<const WindowsResourceParser::TreeNode *> Queue;
|
|
|
|
Queue.push(&Resources);
|
2017-06-17 00:00:42 +02:00
|
|
|
uint32_t NextLevelOffset =
|
|
|
|
sizeof(coff_resource_dir_table) + (Resources.getStringChildren().size() +
|
|
|
|
Resources.getIDChildren().size()) *
|
|
|
|
sizeof(coff_resource_dir_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
std::vector<const WindowsResourceParser::TreeNode *> DataEntriesTreeOrder;
|
|
|
|
uint32_t CurrentRelativeOffset = 0;
|
|
|
|
|
|
|
|
while (!Queue.empty()) {
|
|
|
|
auto CurrentNode = Queue.front();
|
|
|
|
Queue.pop();
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Table = reinterpret_cast<coff_resource_dir_table *>(BufferStart +
|
|
|
|
CurrentOffset);
|
2017-06-09 19:34:30 +02:00
|
|
|
Table->Characteristics = CurrentNode->getCharacteristics();
|
|
|
|
Table->TimeDateStamp = 0;
|
|
|
|
Table->MajorVersion = CurrentNode->getMajorVersion();
|
|
|
|
Table->MinorVersion = CurrentNode->getMinorVersion();
|
|
|
|
auto &IDChildren = CurrentNode->getIDChildren();
|
|
|
|
auto &StringChildren = CurrentNode->getStringChildren();
|
|
|
|
Table->NumberOfNameEntries = StringChildren.size();
|
|
|
|
Table->NumberOfIDEntries = IDChildren.size();
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_resource_dir_table);
|
|
|
|
CurrentRelativeOffset += sizeof(coff_resource_dir_table);
|
2017-06-09 19:34:30 +02:00
|
|
|
|
|
|
|
// Write the directory entries immediately following each directory table.
|
|
|
|
for (auto const &Child : StringChildren) {
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Entry = reinterpret_cast<coff_resource_dir_entry *>(BufferStart +
|
|
|
|
CurrentOffset);
|
2017-07-08 01:23:53 +02:00
|
|
|
Entry->Identifier.setNameOffset(
|
|
|
|
StringTableOffsets[Child.second->getStringIndex()]);
|
2017-06-09 19:34:30 +02:00
|
|
|
if (Child.second->checkIsDataNode()) {
|
|
|
|
Entry->Offset.DataEntryOffset = NextLevelOffset;
|
2017-06-17 00:00:42 +02:00
|
|
|
NextLevelOffset += sizeof(coff_resource_data_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
DataEntriesTreeOrder.push_back(Child.second.get());
|
|
|
|
} else {
|
|
|
|
Entry->Offset.SubdirOffset = NextLevelOffset + (1 << 31);
|
2017-06-17 00:00:42 +02:00
|
|
|
NextLevelOffset += sizeof(coff_resource_dir_table) +
|
2017-06-09 19:34:30 +02:00
|
|
|
(Child.second->getStringChildren().size() +
|
|
|
|
Child.second->getIDChildren().size()) *
|
2017-06-17 00:00:42 +02:00
|
|
|
sizeof(coff_resource_dir_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
Queue.push(Child.second.get());
|
|
|
|
}
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_resource_dir_entry);
|
|
|
|
CurrentRelativeOffset += sizeof(coff_resource_dir_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
for (auto const &Child : IDChildren) {
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Entry = reinterpret_cast<coff_resource_dir_entry *>(BufferStart +
|
|
|
|
CurrentOffset);
|
2017-06-09 19:34:30 +02:00
|
|
|
Entry->Identifier.ID = Child.first;
|
|
|
|
if (Child.second->checkIsDataNode()) {
|
|
|
|
Entry->Offset.DataEntryOffset = NextLevelOffset;
|
2017-06-17 00:00:42 +02:00
|
|
|
NextLevelOffset += sizeof(coff_resource_data_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
DataEntriesTreeOrder.push_back(Child.second.get());
|
|
|
|
} else {
|
|
|
|
Entry->Offset.SubdirOffset = NextLevelOffset + (1 << 31);
|
2017-06-17 00:00:42 +02:00
|
|
|
NextLevelOffset += sizeof(coff_resource_dir_table) +
|
2017-06-09 19:34:30 +02:00
|
|
|
(Child.second->getStringChildren().size() +
|
|
|
|
Child.second->getIDChildren().size()) *
|
2017-06-17 00:00:42 +02:00
|
|
|
sizeof(coff_resource_dir_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
Queue.push(Child.second.get());
|
|
|
|
}
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_resource_dir_entry);
|
|
|
|
CurrentRelativeOffset += sizeof(coff_resource_dir_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocationAddresses.resize(Data.size());
|
|
|
|
// Now write all the resource data entries.
|
|
|
|
for (auto DataNodes : DataEntriesTreeOrder) {
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Entry = reinterpret_cast<coff_resource_data_entry *>(BufferStart +
|
|
|
|
CurrentOffset);
|
2017-06-09 19:34:30 +02:00
|
|
|
RelocationAddresses[DataNodes->getDataIndex()] = CurrentRelativeOffset;
|
|
|
|
Entry->DataRVA = 0; // Set to zero because it is a relocation.
|
|
|
|
Entry->DataSize = Data[DataNodes->getDataIndex()].size();
|
|
|
|
Entry->Codepage = 0;
|
|
|
|
Entry->Reserved = 0;
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_resource_data_entry);
|
|
|
|
CurrentRelativeOffset += sizeof(coff_resource_data_entry);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeDirectoryStringTable() {
|
|
|
|
// Now write the directory string table for .rsrc$01
|
|
|
|
uint32_t TotalStringTableSize = 0;
|
2017-06-13 23:05:42 +02:00
|
|
|
for (auto &String : StringTable) {
|
2017-06-09 19:34:30 +02:00
|
|
|
uint16_t Length = String.size();
|
2017-06-13 22:53:31 +02:00
|
|
|
support::endian::write16le(BufferStart + CurrentOffset, Length);
|
2017-06-13 02:19:43 +02:00
|
|
|
CurrentOffset += sizeof(uint16_t);
|
|
|
|
auto *Start = reinterpret_cast<UTF16 *>(BufferStart + CurrentOffset);
|
2018-11-17 02:44:25 +01:00
|
|
|
llvm::copy(String, Start);
|
2017-06-13 02:19:43 +02:00
|
|
|
CurrentOffset += Length * sizeof(UTF16);
|
2017-06-09 19:34:30 +02:00
|
|
|
TotalStringTableSize += Length * sizeof(UTF16) + sizeof(uint16_t);
|
|
|
|
}
|
2017-06-13 02:19:43 +02:00
|
|
|
CurrentOffset +=
|
2017-06-09 19:34:30 +02:00
|
|
|
alignTo(TotalStringTableSize, sizeof(uint32_t)) - TotalStringTableSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsResourceCOFFWriter::writeFirstSectionRelocations() {
|
|
|
|
|
|
|
|
// Now write the relocations for .rsrc$01
|
|
|
|
// Five symbols already in table before we start, @feat.00 and 2 for each
|
|
|
|
// .rsrc section.
|
|
|
|
uint32_t NextSymbolIndex = 5;
|
|
|
|
for (unsigned i = 0; i < Data.size(); i++) {
|
2017-06-17 00:00:42 +02:00
|
|
|
auto *Reloc =
|
|
|
|
reinterpret_cast<coff_relocation *>(BufferStart + CurrentOffset);
|
2017-06-09 19:34:30 +02:00
|
|
|
Reloc->VirtualAddress = RelocationAddresses[i];
|
|
|
|
Reloc->SymbolTableIndex = NextSymbolIndex++;
|
|
|
|
switch (MachineType) {
|
2017-06-16 23:13:24 +02:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_ARMNT:
|
2017-06-17 00:00:42 +02:00
|
|
|
Reloc->Type = COFF::IMAGE_REL_ARM_ADDR32NB;
|
2017-06-09 19:34:30 +02:00
|
|
|
break;
|
2017-06-16 23:13:24 +02:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_AMD64:
|
2017-06-17 00:00:42 +02:00
|
|
|
Reloc->Type = COFF::IMAGE_REL_AMD64_ADDR32NB;
|
2017-06-09 19:34:30 +02:00
|
|
|
break;
|
2017-06-16 23:13:24 +02:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_I386:
|
2017-06-17 00:00:42 +02:00
|
|
|
Reloc->Type = COFF::IMAGE_REL_I386_DIR32NB;
|
2017-06-09 19:34:30 +02:00
|
|
|
break;
|
2017-11-10 23:27:41 +01:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_ARM64:
|
|
|
|
Reloc->Type = COFF::IMAGE_REL_ARM64_ADDR32NB;
|
|
|
|
break;
|
2017-06-09 19:34:30 +02:00
|
|
|
default:
|
2017-11-10 23:27:41 +01:00
|
|
|
llvm_unreachable("unknown machine type");
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
2017-06-17 00:00:42 +02:00
|
|
|
CurrentOffset += sizeof(coff_relocation);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:49:05 +02:00
|
|
|
Expected<std::unique_ptr<MemoryBuffer>>
|
|
|
|
writeWindowsResourceCOFF(COFF::MachineTypes MachineType,
|
2019-06-11 13:26:50 +02:00
|
|
|
const WindowsResourceParser &Parser,
|
|
|
|
uint32_t TimeDateStamp) {
|
2017-06-09 19:34:30 +02:00
|
|
|
Error E = Error::success();
|
2017-06-19 20:49:05 +02:00
|
|
|
WindowsResourceCOFFWriter Writer(MachineType, Parser, E);
|
2017-06-09 19:34:30 +02:00
|
|
|
if (E)
|
2020-02-10 16:06:45 +01:00
|
|
|
return std::move(E);
|
2019-06-11 13:26:50 +02:00
|
|
|
return Writer.write(TimeDateStamp);
|
2017-06-09 19:34:30 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
} // namespace object
|
|
|
|
} // namespace llvm
|