windows7下提⽰is_not
VS2008 在 W INDOWS7 B ETA7000下编译出错-T HE BINARY IS NOT A VALID W INDOWS IMAGE.
更新:新装的7048 X64没出现这个问题了,原因还不知道
前些天装上了W INDOWS7B ETA7000X86,却发现原先的项⽬编译会出现链接错误:
MT.EXE : GENERAL ERROR C101008D:F AILED TO WRITE THE UPDATED MANIFEST TO THE RESOURCE OF FILE "R ELEASE\T EST.EXE".T HE BINARY IS NOT A VALID W INDOWS IMAGE.
是MT.EXE在嵌⼊MANIFEST⽂件的时候遇到错误,从⽹上找到的解决⽅案,是在链接器设置选项中,启⽤R ANDOMIZED B ASE A DDRESS ,如下图:
原因:
JOHN ROBBINS' BLOG
S ATURDAY,J ANUARY 24,20095:48PM JROBBINS
T HE C ASE OF THE C ORRUPT PE B INARIES
A FTER INSTALLING W INDOWS 7,I ALSO INSTALLED THE W INDOWS 7 SDK AS I WANTED TO POKE AROUND THE UPDATED HEADERS AND DOCUMENTATION FILES TO SEE WHAT WAS NEW AT A LOW LEVEL.
A DDITIONALLY,I WANTED TO MAKE SURE ALL MY CODE COMPILED AGAINST THE NEW HEADERS AND LIBRARIES IN CASE SOMEONE TAKING MY NATIVE DEBUGGING CLASS TRIED IT AND RAN INTO PROBLEMS.A FTER MANY YEARS OF THE SDK TEAM COMPLETELY IGNORING V ISUAL S TUDIO, THE W INDOWS 7SDK INSTALLATION NOW LOOKS FOR V ISUAL S TUDIO AND PROPERLY INTEGRATES WITH IT (I BELIEVE THIS STARTED WITH THE V ISTA SDK).A FTER HUNDREDS OF THOUSANDS OF EMAILS OVER THE YEARS FROM PEOPLE WHO COULDN'T COMPILE CODE FROM MY BOOKS AND COLUMNS BECAUSE THEY
HADN'T GONE THROUGH THE MANUAL GYRATIONS TO INTEGRATE THE LATEST SDK WITH THE DEVELOPMENT ENVIRONMENT, IT'S A HUGE HELP.
A S I HAVE ALL MY BUILDS AUTOMATED,I LET RIP AND GOT A BUILD FAILURE ON A FEW RELEASE BUILD
美缝的好处与弊端
X86AND X64BINARIES.T HE FAILURE WAS LIKE THE FOLLOWING IN ALL THOSE CASES:
MT.EXE : GENERAL ERROR C101008D:F AILED TO WRITE THE UPDATED MANIFEST TO THE RESOURCE OF FILE "..\..\..\..\R ELEASE\FTS IMP T EST.EXE".T HE BINARY IS NOT A VALID W INDOWS IMAGE.
MT.EXE IS THE TOOL USED TO EMBED THE MANIFEST INTO YOUR BINARIES.
A S I WAS RUNNING A BETA OS,I WONDERED IF THIS WAS A PROBLEM WITH ANTI-VIRUS,SO I DISABLED E T RUST AND TRIED THE BUILD AGAIN BUT STILL GOT THE SAME ERROR.
A S I'M USING V ISUAL S TUDIO 2008SP1,CL.EXE AND LINK.EXE ARE DOING ALL THE MAIN WORK OF COMPILING, BUT MT.EXE COMES FROM THE SDK.S INCE THIS CODE COMPILED CORRECTLY ON MY V ISTA COMPUTER WITH THE V ISTA SDK INSTALLED,MY NEXT STEP W
大圆点怎么打
AS TO SEE IF THIS WAS POSSIBLY A PROBLEM WITH MT.EXE OR IF CL.EXE AND LINK.EXE WERE EXPOSING A BUG IN THE OPERATING SYSTEM DLL S THEY WERE USING.I UNINSTALLED THE W INDOWS 7 SDK,WHICH REVERTED V ISUAL S TUDIO 2008SP1TO USING THE V ISTA SDK.G IVING THE RECOMPILES A GO,I GOT THE SAME ERROR FROM MT.EXE.I VERIFIED THAT I WAS IN FACT USING THE MT.EXE FROM THE V ISTA SDK.O NE THING THAT WAS CONFUSING TO ME WAS THAT MT.EXE FROM THE V
ISTA SDK AND W INDOWS 7SDK BOTH REPORT THE SAME VERSION NUMBER,BUT THE BINARIES ARE DIFFERENT SIZES.
L OOKING CLOSER AT WHAT BINARIES WERE GETTING CORRUPT,IT WAS ONLY FOUR OUT OF 56.EXE FILES IN MY BUILD.I NTERESTINGLY, THEY WERE ALL CONSOLE APPLICATIONS THAT WERE UNIT TESTS.
F IRIN
G UP V ISUAL S TUDIO,I CREATED THE CANONICAL TEST CONSOLE APPLICATION,
H ELLO W ORLD,AND VERIFIED THAT IT COMPILED.L OOKING AT THE BUILD.HTM FILE,
I SAW THAT H ELLO W ORLD HAD THE FOLLOWING IN IT:
C REATING COMMAN
D LIN
E "MT.EXE @C:\J UNK\CRUFT\H ELLO W ORLD\R ELEASE\RSP00000840485968.RS P /NOLOGO"
C REATING TEMPORARY FILE "C:\J UNK\CRUFT\H ELLO W ORLD\R ELEASE\BAT00000940485968.BAT " WITH CONTENTS
[
@ECHO M ANIFEST RESOURCE LAST UPDATED AT %TIME% ON %DATE%>.\R ELEASE\MT.DEP
]
F OR TWO SECONDS,I WAS A LITTLE CONFUSED ABOUT THE TEMPORARY FILE CREATION BECAUSE MY FAILIN
G BUILDS DIDN'T HAVE THAT.W HAT THE OUTPUT TOLD ME WAS THAT THE TEMPORARY FILE INDICATING MT.EXE CREATES THE RESOURCE UPDATE TIME FILE AFTER IT RUNS SUCCESSFULLY.
断隔M Y HYPOTHESIS AT THIS POINT WAS THAT EITHER LINK.EXE WAS CREATING A CORRUPT BINARY BEFORE MT.EXE WORKED ON IT, OR IT WAS MT.EXE CORRUPTING THE BINARY ITSELF.U SING ONE OF MY PROJECTS THAT PRODUCED A CORRUPT BINARY,I COPIED OUT THE COMMAND LINES FOR CL.EXE,LINK.EXE, AND MT.EXE FROM ITS BUILD.HTM AND RAN THEM DIRECTLY FROM A BATCH FILE AT THE COMMAND LINE (PROPERLY SET UP WITH VCVARS.BAT).I WANTED TO LOOK AT WHAT WAS IN THE P ORTABLE E XECUTABLE (PE) DATA TO SEE IF ANYTHING WAS AMISS.B Y THE WAY, YOU NEED TO READ M ATT P IETREK'S DEFINITIVE "A N I N-D EPTH L OOK INTO THE W IN32 P ORTABLE E XECUTABLE F ILE F ORMAT" P ARTS 1 AND P ART 2 YOU'LL LEARN A TON ABOUT HOW W INDOWS WORKS.
R UNNING DUMPBIN.EXE/HEADERS ON THE RESULTING EXE ALLOWED ME TO LOOK AT THE MAIN PORTIONS.B ECAUSE MT.EXE PUTS THE MANIFEST INTO THE RESOURCE SECTION OF THE BINARY,I PAID SPECIAL ATTENTION TO IT:
SECTION HEADER#4
.RSRC NAME
0 VIRTUAL SIZE
银河系中心黑洞4000 VIRTUAL ADDRESS (00404000 TO 00403FFF)
0 SIZE OF RAW DATA
0 FILE POINTER TO RAW DATA
0 FILE POINTER TO RELOCATION TABLE
0 FILE POINTER TO LINE NUMBERS
0 NUMBER OF RELOCATIONS
0 NUMBER OF LINE NUMBERS
40000040 FLAGS
I NITIALIZED D ATA
导致英语短语
R EAD O NLY
D O YOU SE
E THE PROBLEM?H ERE'S A GOOD RESOURCE SECTION FOR COMPARISON:
SECTION HEADER#4
.RSRC NAME
2B0 VIRTUAL SIZE
4000 VIRTUAL ADDRESS (00404000 TO 004042AF)
400 SIZE OF RAW DATA
1800 FILE POINTER TO RAW DATA (00001800 TO 00001BFF)
0 FILE POINTER TO RELOCATION TABLE
0 FILE POINTER TO LINE NUMBERS
0 NUMBER OF RELOCATIONS
0 NUMBER OF LINE NUMBERS
40000040 FLAGS
I NITIALIZED D ATA
R EAD O NLY
I NTERESTING!T HERE'S A RESOURCE SECTION,BUT IT IS OBVIOUSLY CORRUPT BECAUSE SOMETHING NOT FILLING OUT THE RAW DATA INFORMATION.A S THE MANIFEST GOES IN THE RESOURCE SECTION,
MT.EXE REALLY CAN'T ADD TO A SECTION WHOSE RAW DATA STARTS AT ZERO.寄宿证明
S INCE THE APP WIZARD GENERATED PROJECT IS NOT PRODUCING CORRUPT BINARIES BUT I HAVE SEVERAL PROJECTS THAT ARE,IT'S TIME TO LOOK AT THE LINK.EXE AND MT.EXE SWITCHES TO SEE WHAT MY PROJECTS HAVE SET THAT ARE TRIPPING EITHER OF THOSE TOOLS UP.T HE EASY WAY FOR ME TO DO THAT IS LOOK AT THE .VCPROJ FILES IN
THE FANTASTIC,AND FREE, S OURCE G EAR D IFF M ERGE.(I CAN'T RAVE ENOUGH ABOUT D IFF M ERGE!)
D IFF M ERG
E POINTED ME WRITE TO THE .VCPROJ,VCL INKER T OOL R ANDOMIZED B ASE A DDRESS ATTRIBUTE AS THE ONLY REAL DIFFERENCE BETWEEN THE FILES.I N THE APP WIZARD PROJECT, THE VALUE IS 1,FOR MY BAD PROJECT,THE VALUE IS 2.A CHECK IN THE PROJECT PROPERTIES SHOWS A VALUE O
F 2MAPS TO THE LINKER SWITCH /DYNAMICBASE:NO.I N MY BAD PROJECT,I CHANGE THE VALUE TO
/DYNAMICBASE AND THE PREVIOUSLY CORRUPT RELEASE BUILD BUILT AND WORKED PERFECTLY.
O NCE I FIX ALL MY BROKEN PROJECTS TO USE /DYNAMICBASE INSTEAD OF /DYNAMICBASE:NO,ALL MY BUILD PROBLEMS GO AWAY.A S I MENTIONED EARLIER, THE CORRUPT BUILDS HAPPENED TO BE SEVERAL OF MY UNIT TESTS,WHICH IS WHERE YOU MIGHT SEE IT
AS WELL.T HE EXACT SITUATION I WAS GETTING THE CORRUPT BINARY IS AS FOLLOWS:
O N W INDOWS 7 WITH V ISUAL S TUDIO 2008SP1
T HERE'S NO RESOURCE FILE IN THE BINARY.
I T'S A RELEASE BUILD
Y OU ARE SPECIFICALLY NOT SETTING /DYNAMICBASE TO THE LINKER
A FTER I SPENT THE 20MINUTES TRACKING THIS PROBLEM DOWN,I THOUGHT MAYBE I SHOULD READ THE W
INDOWS 7SDK R EAD M E FILE TO SEE IF THIS WAS A KNOWN ISSUE.S ECTION 5.3.6,TITLED "P ROBLEM R UNNING MT.EXE ON W INDOWS 7
小记者自我介绍B ETA," SAYS MT.EXE FAILS IF THE .EXE DOES NOT CONTAIN A RESOURCE (.RSRC) SECTION AND THE WORK AROUND IS TO ADD AN EMPTY .R
C FILE TO YOUR PROJECT TO WORK AROUN
D TH
E PROBLEM.
A FTER SMACKING MY FOREHEAD WONDERING IF THIS WAS THE EXACT BUG I WAS RUNNING INTO,I TOOK THE BATCH FILE WHERE I RAN ALL THE CL.EXE,LINKER.EXE,AND MT.EXE COMMANDS DIRECTLY AND REMOVED ALL THE /MANIFEST RELATED SWITCHES FROM THE LINK.EXE COMMAND LINE.I ALSO COMMENTED OUT CALL TO MT.EXE AND REBUILT.O F COURSE,WITH MT.EXE COMPLETELY OUT OF THE WAY, THE BINARY WASN'T CORRUPT.
I THINK I'M SEEING A MANIFESTATION (PUN INTENDED!)OF THE BUG
MENTIONED IN SECTION 5.3.6OF THE R EAD M E.A LITTLE EXPERIMENTATION COMPILING AND LINKING WITH THE BARE MINIMUM SWITCHES NECESSARY SHOWS THAT THE LINKER DEFAULTS TO /DYNAMICBASE:NO BECAUSE THE OPTIONAL HEADER DLL CHARACTERISTICS DOES NOT SHOW D YNAMIC BASE WHEN YOU DUMP THE BINARY.
W HILE I THOUGHT I WAS GOING TO HAVE A NICE C ASE O F… STORY, IT TURNS OUT THAT I
SHOULD HAVE READ THE W INDOWS 7SDK R EAD M E FIRST, WHICH IS ALWAYS A VERY WISE IDEA WHEN DEALING WITH BETAS.T HERE'S NOTHING LIKE REPRODUCING A KNOWN BUG.
H OWEVER,I WAS ABLE TO FIND AN ADDITIONAL WORKAROUND TO THE MT.EXE BUG BY SETTING THE
/DYNAMICBASE SWITCH.I N MY SITUATION THAT WAS BETTER BECAUSE IF I ADDED A .RC FILE TO ALL THE UNIT TESTS,I WAS GOING TO HAVE TO ADD THOSE FILES TO MY CODE INSTALLS AND PATCHES.
F ILED UNDER: J OHN R OBBINS, D EBUGGING
>此地无银三百两下一句