Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Add --force option to ignore PDB mismatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwhite committed Aug 14, 2017
1 parent 956976c commit caf602b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
13 changes: 12 additions & 1 deletion src/ducible/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ struct OptionNames<char> {
const char* dryrunLong = "--dryrun";
const char* dryrunShort = "-n";
const char* dashDash = "--";
const char* forceLong = "--force";
const char* forceShort = "-f";
};

template<>
Expand All @@ -106,6 +108,8 @@ struct OptionNames<wchar_t> {
const wchar_t* dryrunLong = L"--dryrun";
const wchar_t* dryrunShort = L"-n";
const wchar_t* dashDash = L"--";
const wchar_t* forceLong = L"--force";
const wchar_t* forceShort = L"-f";
};

/**
Expand All @@ -123,6 +127,7 @@ class CommandOptions
const CharT* image;
const CharT* pdb;
bool dryrun;
bool force;

CommandOptions() : image(NULL), pdb(NULL), dryrun(false) {}

Expand Down Expand Up @@ -171,6 +176,9 @@ class CommandOptions
else if (arg == opt.dryrunLong || arg == opt.dryrunShort) {
dryrun = true;
}
else if (arg == opt.forceLong || arg == opt.forceShort) {
force = true;
}
else if (arg.length() > 0 && arg.front() == '-') {
throw UnknownOption<CharT>(argv[i]);
}
Expand Down Expand Up @@ -221,6 +229,9 @@ Optional arguments:
--help, -h Prints this help.
--dryrun, -n No files are modified, only what would have been patched are
printed.
--force, -f Proceed even if the PDB signatures don't match. Useful if you
already know an image is compatible with a PDB even though the
signatures don't match.
)";

template<typename CharT = char>
Expand Down Expand Up @@ -254,7 +265,7 @@ int ducible(int argc, CharT** argv)
}

try {
patchImage(opts.image, opts.pdb, opts.dryrun);
patchImage(opts.image, opts.pdb, opts.dryrun, opts.force);
}
catch (const InvalidImage& error) {
std::cerr << "Error: Invalid image (" << error.why() << ")\n";
Expand Down
27 changes: 14 additions & 13 deletions src/ducible/patch_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ void patchNamesStream(MsfMemoryStream* stream) {
* Patches the PDB header stream.
*/
void patchHeaderStream(MsfFile& msf, MsfMemoryStream* stream, const CV_INFO_PDB70* pdbInfo,
uint32_t timestamp, const uint8_t signature[16]) {
uint32_t timestamp, const uint8_t signature[16], bool force) {

uint8_t* data = stream->data();
const uint8_t* dataEnd = stream->data() + stream->length();
Expand All @@ -374,8 +374,9 @@ void patchHeaderStream(MsfFile& msf, MsfMemoryStream* stream, const CV_INFO_PDB7
if (header->version < PdbVersion::vc70)
throw InvalidPdb("unsupported PDB implementation version");

// Check that this PDB matches what the PE file expects
if (!pdbInfo || !matchingSignatures(*pdbInfo, *header))
// Check that this PDB matches what the PE file expects. Don't do the check
// if `force` was specified.
if (!force && (!pdbInfo || !matchingSignatures(*pdbInfo, *header)))
throw InvalidPdb("PE and PDB signatures do not match");

// Patch the PDB header stream
Expand Down Expand Up @@ -726,7 +727,7 @@ void patchPublicSymbolStream(MsfMemoryStream* stream) {
* Rewrites a PDB, eliminating non-determinism.
*/
void patchPDB(MsfFile& msf, const CV_INFO_PDB70* pdbInfo,
uint32_t timestamp, const uint8_t signature[16]) {
uint32_t timestamp, const uint8_t signature[16], bool force) {

msf.replaceStream((size_t)PdbStreamType::streamTable, nullptr);

Expand All @@ -738,7 +739,7 @@ void patchPDB(MsfFile& msf, const CV_INFO_PDB70* pdbInfo,
auto pdbHeaderStream = std::shared_ptr<MsfMemoryStream>(
new MsfMemoryStream(origPdbHeaderStream.get()));

patchHeaderStream(msf, pdbHeaderStream.get(), pdbInfo, timestamp, signature);
patchHeaderStream(msf, pdbHeaderStream.get(), pdbInfo, timestamp, signature, force);

msf.replaceStream((size_t)PdbStreamType::header, pdbHeaderStream);

Expand Down Expand Up @@ -784,7 +785,7 @@ void patchPDB(MsfFile& msf, const CV_INFO_PDB70* pdbInfo,
*/
template<typename CharT>
void patchPDB(const CharT* pdbPath, const CV_INFO_PDB70* pdbInfo,
uint32_t timestamp, const uint8_t signature[16], bool dryrun) {
uint32_t timestamp, const uint8_t signature[16], bool dryrun, bool force) {

auto tmpPdbPath = getTempPdbPath(pdbPath);

Expand All @@ -794,7 +795,7 @@ void patchPDB(const CharT* pdbPath, const CV_INFO_PDB70* pdbInfo,

MsfFile msf(pdb);

patchPDB(msf, pdbInfo, timestamp, signature);
patchPDB(msf, pdbInfo, timestamp, signature, force);

// Write out the rewritten PDB to disk.
msf.write(tmpPdb);
Expand All @@ -810,7 +811,7 @@ void patchPDB(const CharT* pdbPath, const CV_INFO_PDB70* pdbInfo,
}

template<typename CharT>
void patchImageImpl(const CharT* imagePath, const CharT* pdbPath, bool dryrun) {
void patchImageImpl(const CharT* imagePath, const CharT* pdbPath, bool dryrun, bool force) {
MemMap image(imagePath);

uint8_t* buf = (uint8_t*)image.buf();
Expand Down Expand Up @@ -855,7 +856,7 @@ void patchImageImpl(const CharT* imagePath, const CharT* pdbPath, bool dryrun) {

// Patch the PDB file.
if (pdbPath) {
patchPDB(pdbPath, pdbInfo, pe.timestamp, pe.pdbSignature, dryrun);
patchPDB(pdbPath, pdbInfo, pe.timestamp, pe.pdbSignature, dryrun, force);
}

// Patch the ilk file with the new PDB signature. If we don't do this,
Expand All @@ -871,14 +872,14 @@ void patchImageImpl(const CharT* imagePath, const CharT* pdbPath, bool dryrun) {

#if defined(_WIN32) && defined(UNICODE)

void patchImage(const wchar_t* imagePath, const wchar_t* pdbPath, bool dryrun) {
patchImageImpl(imagePath, pdbPath, dryrun);
void patchImage(const wchar_t* imagePath, const wchar_t* pdbPath, bool dryrun, bool force) {
patchImageImpl(imagePath, pdbPath, dryrun, force);
}

#else

void patchImage(const char* imagePath, const char* pdbPath, bool dryrun) {
patchImageImpl(imagePath, pdbPath, dryrun);
void patchImage(const char* imagePath, const char* pdbPath, bool dryrun, bool force) {
patchImageImpl(imagePath, pdbPath, dryrun, force);
}

#endif
16 changes: 13 additions & 3 deletions src/ducible/patch_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@
*/
#if defined(_WIN32) && defined(UNICODE)

void patchImage(const wchar_t* imagePath, const wchar_t* pdbPath, bool dryrun = true);
void patchImage(
const wchar_t* imagePath,
const wchar_t* pdbPath,
bool dryrun = true,
bool force = false
);

#else

void patchImage(const char* imagePath, const char* pdbPath, bool dryrun = true);
void patchImage(
const char* imagePath,
const char* pdbPath,
bool dryrun = true,
bool force = false
);

#endif
#endif

0 comments on commit caf602b

Please # to comment.